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; |
| 82 | struct secmon_params bsp_params; |
| 83 | struct secmon_params secondary_params; |
| 84 | }; |
| 85 | |
| 86 | static void secmon_start(void *arg) |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 87 | { |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 88 | uint32_t scr; |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame^] | 89 | secmon_entry_t entry; |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 90 | struct secmon_params *p = NULL; |
| 91 | struct secmon_runit *r = arg; |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 92 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame^] | 93 | entry = r->entry; |
| 94 | |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 95 | if (cpu_is_bsp()) |
| 96 | p = &r->bsp_params; |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame^] | 97 | else { |
| 98 | entry = secondary_entry_point(entry); |
| 99 | if (r->secondary_params.entry != NULL) |
| 100 | p = &r->secondary_params; |
| 101 | } |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 102 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame^] | 103 | printk(BIOS_DEBUG, "CPU%x entering secure monitor %p.\n", |
| 104 | cpu_info()->id, entry); |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 105 | |
| 106 | /* We want to enforce the following policies: |
| 107 | * NS bit is set for lower EL |
| 108 | */ |
| 109 | scr = raw_read_scr_el3(); |
| 110 | scr |= SCR_NS; |
| 111 | raw_write_scr_el3(scr); |
| 112 | |
Aaron Durbin | e702be6 | 2014-09-18 14:23:59 -0500 | [diff] [blame^] | 113 | entry(p); |
Aaron Durbin | 5add435 | 2014-09-17 11:43:20 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void secmon_run(void (*entry)(void *), void *cb_tables) |
| 117 | { |
| 118 | const struct spintable_attributes *spin_attrs; |
| 119 | static struct secmon_runit runit; |
| 120 | struct cpu_action action = { |
| 121 | .run = secmon_start, |
| 122 | .arg = &runit, |
| 123 | }; |
| 124 | |
| 125 | printk(BIOS_SPEW, "payload jump @ %p\n", entry); |
| 126 | |
| 127 | if (get_current_el() != EL3) { |
| 128 | printk(BIOS_DEBUG, "Secmon Error: Can only be loaded in EL3\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | runit.entry = secmon_load_rmodule(); |
| 133 | |
| 134 | if (runit.entry == NULL) |
| 135 | die("ARM64 Error: secmon load error"); |
| 136 | |
| 137 | printk(BIOS_DEBUG, "ARM64: Loaded the el3 monitor...jumping to %p\n", |
| 138 | runit.entry); |
| 139 | |
| 140 | runit.bsp_params.entry = entry; |
| 141 | runit.bsp_params.arg = cb_tables; |
| 142 | runit.bsp_params.elx_el = EL2; |
| 143 | runit.bsp_params.elx_mode = SPSR_USE_L; |
| 144 | runit.secondary_params.elx_el = EL2; |
| 145 | runit.secondary_params.elx_mode = SPSR_USE_L; |
| 146 | |
| 147 | spin_attrs = spintable_get_attributes(); |
| 148 | |
| 149 | if (spin_attrs != NULL) { |
| 150 | runit.secondary_params.entry = spin_attrs->entry; |
| 151 | runit.secondary_params.arg = spin_attrs->addr; |
| 152 | } |
| 153 | |
| 154 | arch_run_on_all_cpus_but_self_async(&action); |
| 155 | secmon_start(&runit); |
Furquan Shaikh | abde3b5 | 2014-08-26 15:39:51 -0700 | [diff] [blame] | 156 | } |