Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Google Inc |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | * |
| 21 | * secmon_loader.c: Responsible for loading the rmodule, providing entry point |
| 22 | * and parameter location for the rmodule. |
| 23 | */ |
| 24 | |
| 25 | #include <arch/lib_helpers.h> |
| 26 | #include <arch/secmon.h> |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 27 | #include <arch/spintable.h> |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 28 | #include <arch/stages.h> |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 29 | #include <console/console.h> |
| 30 | #include <rmodule.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | /* SECMON entry point encoded as an rmodule */ |
| 34 | extern unsigned char _binary_secmon_start[]; |
| 35 | |
| 36 | typedef void (*secmon_entry_t)(struct secmon_params *); |
| 37 | |
| 38 | void __attribute__((weak)) soc_get_secmon_base_size(uint64_t *secmon_base, size_t *secmon_size) |
| 39 | { |
| 40 | /* Default weak implementation initializes to 0 */ |
| 41 | *secmon_base = 0; |
| 42 | *secmon_size = 0; |
| 43 | } |
| 44 | |
| 45 | static secmon_entry_t secmon_load_rmodule(void) |
| 46 | { |
| 47 | struct rmodule secmon_mod; |
| 48 | uint64_t secmon_base; |
| 49 | size_t secmon_size; |
| 50 | |
| 51 | /* Get base address and size of the area available for secure monitor |
| 52 | * rmodule. |
| 53 | */ |
| 54 | soc_get_secmon_base_size(&secmon_base, &secmon_size); |
| 55 | |
| 56 | if ((secmon_base == 0) || (secmon_size == 0)) { |
| 57 | printk(BIOS_ERR, "ARM64: secmon_base / secmon_size invalid\n"); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | printk(BIOS_DEBUG,"secmon_base:%lx,secmon_size:%lx\n", |
| 62 | (unsigned long)secmon_base, (unsigned long)secmon_size); |
| 63 | |
| 64 | /* Fail if can't parse secmon module */ |
| 65 | if (rmodule_parse(&_binary_secmon_start, &secmon_mod)) { |
| 66 | printk(BIOS_ERR, "ARM64: secmon_mod not found\n"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | /* Load rmodule at secmon_base */ |
| 71 | if (rmodule_load((void *)secmon_base, &secmon_mod)) { |
| 72 | printk(BIOS_ERR, "ARM64:secmon_mod cannot load\n"); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | /* Identify the entry point for secure monitor */ |
| 77 | return rmodule_entry(&secmon_mod); |
| 78 | } |
| 79 | |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 80 | struct secmon_runit { |
| 81 | secmon_entry_t entry; |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 82 | struct secmon_params params; |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static void secmon_start(void *arg) |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 86 | { |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 87 | uint32_t scr; |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 88 | secmon_entry_t entry; |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 89 | struct secmon_params *p; |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 90 | struct secmon_runit *r = arg; |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 91 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 92 | entry = r->entry; |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 93 | p = &r->params; |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 94 | |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 95 | /* Obtain secondary entry point for non-BSP CPUs. */ |
| 96 | if (!cpu_is_bsp()) |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 97 | entry = secondary_entry_point(entry); |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 98 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 99 | printk(BIOS_DEBUG, "CPU%x entering secure monitor %p.\n", |
| 100 | cpu_info()->id, entry); |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 101 | |
| 102 | /* We want to enforce the following policies: |
| 103 | * NS bit is set for lower EL |
| 104 | */ |
| 105 | scr = raw_read_scr_el3(); |
| 106 | scr |= SCR_NS; |
| 107 | raw_write_scr_el3(scr); |
| 108 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame] | 109 | entry(p); |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 112 | static void fill_secmon_params(struct secmon_params *p, |
| 113 | void (*bsp_entry)(void *), void *bsp_arg) |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 114 | { |
| 115 | const struct spintable_attributes *spin_attrs; |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 116 | |
| 117 | memset(p, 0, sizeof(*p)); |
| 118 | |
Aaron Durbin | 931a218 | 2014-11-05 11:19:21 -0600 | [diff] [blame] | 119 | p->online_cpus = cpus_online(); |
| 120 | |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 121 | spin_attrs = spintable_get_attributes(); |
| 122 | |
| 123 | if (spin_attrs != NULL) { |
| 124 | p->secondary.run = spin_attrs->entry; |
| 125 | p->secondary.arg = spin_attrs->addr; |
| 126 | } |
| 127 | |
| 128 | p->bsp.run = bsp_entry; |
| 129 | p->bsp.arg = bsp_arg; |
| 130 | } |
| 131 | |
| 132 | void secmon_run(void (*entry)(void *), void *cb_tables) |
| 133 | { |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 134 | static struct secmon_runit runit; |
| 135 | struct cpu_action action = { |
| 136 | .run = secmon_start, |
| 137 | .arg = &runit, |
| 138 | }; |
| 139 | |
| 140 | printk(BIOS_SPEW, "payload jump @ %p\n", entry); |
| 141 | |
| 142 | if (get_current_el() != EL3) { |
| 143 | printk(BIOS_DEBUG, "Secmon Error: Can only be loaded in EL3\n"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | runit.entry = secmon_load_rmodule(); |
| 148 | |
| 149 | if (runit.entry == NULL) |
| 150 | die("ARM64 Error: secmon load error"); |
| 151 | |
| 152 | printk(BIOS_DEBUG, "ARM64: Loaded the el3 monitor...jumping to %p\n", |
| 153 | runit.entry); |
| 154 | |
Aaron Durbin | be3e238 | 2014-11-05 10:48:16 -0600 | [diff] [blame] | 155 | fill_secmon_params(&runit.params, entry, cb_tables); |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 156 | |
| 157 | arch_run_on_all_cpus_but_self_async(&action); |
| 158 | secmon_start(&runit); |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 159 | } |