blob: 7a6e3ee73898fb1152e1169281a167b6dd0cd111 [file] [log] [blame]
Furquan Shaikhabde3b52014-08-26 15:39:51 -07001/*
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 Durbin5add4352014-09-17 11:43:20 -050027#include <arch/spintable.h>
Aaron Durbine702be62014-09-18 14:23:59 -050028#include <arch/stages.h>
Furquan Shaikhabde3b52014-08-26 15:39:51 -070029#include <console/console.h>
30#include <rmodule.h>
31#include <string.h>
32
33/* SECMON entry point encoded as an rmodule */
34extern unsigned char _binary_secmon_start[];
35
36typedef void (*secmon_entry_t)(struct secmon_params *);
37
38void __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
45static 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 Durbin5add4352014-09-17 11:43:20 -050080struct secmon_runit {
81 secmon_entry_t entry;
Aaron Durbinbe3e2382014-11-05 10:48:16 -060082 struct secmon_params params;
Aaron Durbin5add4352014-09-17 11:43:20 -050083};
84
85static void secmon_start(void *arg)
Furquan Shaikhabde3b52014-08-26 15:39:51 -070086{
Furquan Shaikhabde3b52014-08-26 15:39:51 -070087 uint32_t scr;
Aaron Durbine702be62014-09-18 14:23:59 -050088 secmon_entry_t entry;
Aaron Durbinbe3e2382014-11-05 10:48:16 -060089 struct secmon_params *p;
Aaron Durbin5add4352014-09-17 11:43:20 -050090 struct secmon_runit *r = arg;
Furquan Shaikhabde3b52014-08-26 15:39:51 -070091
Aaron Durbine702be62014-09-18 14:23:59 -050092 entry = r->entry;
Aaron Durbinbe3e2382014-11-05 10:48:16 -060093 p = &r->params;
Aaron Durbine702be62014-09-18 14:23:59 -050094
Aaron Durbinbe3e2382014-11-05 10:48:16 -060095 /* Obtain secondary entry point for non-BSP CPUs. */
96 if (!cpu_is_bsp())
Aaron Durbine702be62014-09-18 14:23:59 -050097 entry = secondary_entry_point(entry);
Furquan Shaikhabde3b52014-08-26 15:39:51 -070098
Aaron Durbine702be62014-09-18 14:23:59 -050099 printk(BIOS_DEBUG, "CPU%x entering secure monitor %p.\n",
100 cpu_info()->id, entry);
Furquan Shaikhabde3b52014-08-26 15:39:51 -0700101
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 Durbine702be62014-09-18 14:23:59 -0500109 entry(p);
Aaron Durbin5add4352014-09-17 11:43:20 -0500110}
111
Aaron Durbinbe3e2382014-11-05 10:48:16 -0600112static void fill_secmon_params(struct secmon_params *p,
113 void (*bsp_entry)(void *), void *bsp_arg)
Aaron Durbin5add4352014-09-17 11:43:20 -0500114{
115 const struct spintable_attributes *spin_attrs;
Aaron Durbinbe3e2382014-11-05 10:48:16 -0600116
117 memset(p, 0, sizeof(*p));
118
Aaron Durbin931a2182014-11-05 11:19:21 -0600119 p->online_cpus = cpus_online();
120
Aaron Durbinbe3e2382014-11-05 10:48:16 -0600121 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
132void secmon_run(void (*entry)(void *), void *cb_tables)
133{
Aaron Durbin5add4352014-09-17 11:43:20 -0500134 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 Durbinbe3e2382014-11-05 10:48:16 -0600155 fill_secmon_params(&runit.params, entry, cb_tables);
Aaron Durbin5add4352014-09-17 11:43:20 -0500156
157 arch_run_on_all_cpus_but_self_async(&action);
158 secmon_start(&runit);
Furquan Shaikhabde3b52014-08-26 15:39:51 -0700159}