blob: 4d837642344e9909f90e35377a7c8911c7bae90a [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>
Furquan Shaikhabde3b52014-08-26 15:39:51 -070028#include <console/console.h>
29#include <rmodule.h>
30#include <string.h>
31
32/* SECMON entry point encoded as an rmodule */
33extern unsigned char _binary_secmon_start[];
34
35typedef void (*secmon_entry_t)(struct secmon_params *);
36
37void __attribute__((weak)) soc_get_secmon_base_size(uint64_t *secmon_base, size_t *secmon_size)
38{
39 /* Default weak implementation initializes to 0 */
40 *secmon_base = 0;
41 *secmon_size = 0;
42}
43
44static secmon_entry_t secmon_load_rmodule(void)
45{
46 struct rmodule secmon_mod;
47 uint64_t secmon_base;
48 size_t secmon_size;
49
50 /* Get base address and size of the area available for secure monitor
51 * rmodule.
52 */
53 soc_get_secmon_base_size(&secmon_base, &secmon_size);
54
55 if ((secmon_base == 0) || (secmon_size == 0)) {
56 printk(BIOS_ERR, "ARM64: secmon_base / secmon_size invalid\n");
57 return NULL;
58 }
59
60 printk(BIOS_DEBUG,"secmon_base:%lx,secmon_size:%lx\n",
61 (unsigned long)secmon_base, (unsigned long)secmon_size);
62
63 /* Fail if can't parse secmon module */
64 if (rmodule_parse(&_binary_secmon_start, &secmon_mod)) {
65 printk(BIOS_ERR, "ARM64: secmon_mod not found\n");
66 return NULL;
67 }
68
69 /* Load rmodule at secmon_base */
70 if (rmodule_load((void *)secmon_base, &secmon_mod)) {
71 printk(BIOS_ERR, "ARM64:secmon_mod cannot load\n");
72 return NULL;
73 }
74
75 /* Identify the entry point for secure monitor */
76 return rmodule_entry(&secmon_mod);
77}
78
Aaron Durbin5add4352014-09-17 11:43:20 -050079struct secmon_runit {
80 secmon_entry_t entry;
81 struct secmon_params bsp_params;
82 struct secmon_params secondary_params;
83};
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 Durbin5add4352014-09-17 11:43:20 -050088 struct secmon_params *p = NULL;
89 struct secmon_runit *r = arg;
Furquan Shaikhabde3b52014-08-26 15:39:51 -070090
Aaron Durbin5add4352014-09-17 11:43:20 -050091 if (cpu_is_bsp())
92 p = &r->bsp_params;
93 else if (r->secondary_params.entry != NULL)
94 p = &r->secondary_params;
Furquan Shaikhabde3b52014-08-26 15:39:51 -070095
Aaron Durbin5add4352014-09-17 11:43:20 -050096 printk(BIOS_DEBUG, "CPU%x entering secure monitor.\n", cpu_info()->id);
Furquan Shaikhabde3b52014-08-26 15:39:51 -070097
98 /* We want to enforce the following policies:
99 * NS bit is set for lower EL
100 */
101 scr = raw_read_scr_el3();
102 scr |= SCR_NS;
103 raw_write_scr_el3(scr);
104
Aaron Durbin5add4352014-09-17 11:43:20 -0500105 r->entry(p);
106}
107
108void secmon_run(void (*entry)(void *), void *cb_tables)
109{
110 const struct spintable_attributes *spin_attrs;
111 static struct secmon_runit runit;
112 struct cpu_action action = {
113 .run = secmon_start,
114 .arg = &runit,
115 };
116
117 printk(BIOS_SPEW, "payload jump @ %p\n", entry);
118
119 if (get_current_el() != EL3) {
120 printk(BIOS_DEBUG, "Secmon Error: Can only be loaded in EL3\n");
121 return;
122 }
123
124 runit.entry = secmon_load_rmodule();
125
126 if (runit.entry == NULL)
127 die("ARM64 Error: secmon load error");
128
129 printk(BIOS_DEBUG, "ARM64: Loaded the el3 monitor...jumping to %p\n",
130 runit.entry);
131
132 runit.bsp_params.entry = entry;
133 runit.bsp_params.arg = cb_tables;
134 runit.bsp_params.elx_el = EL2;
135 runit.bsp_params.elx_mode = SPSR_USE_L;
136 runit.secondary_params.elx_el = EL2;
137 runit.secondary_params.elx_mode = SPSR_USE_L;
138
139 spin_attrs = spintable_get_attributes();
140
141 if (spin_attrs != NULL) {
142 runit.secondary_params.entry = spin_attrs->entry;
143 runit.secondary_params.arg = spin_attrs->addr;
144 }
145
146 arch_run_on_all_cpus_but_self_async(&action);
147 secmon_start(&runit);
Furquan Shaikhabde3b52014-08-26 15:39:51 -0700148}