blob: 00c6cf9b4755730167d9a6a70f5c7236874a3244 [file] [log] [blame]
Rocky Phaguraafb7a812020-07-21 14:48:48 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
Arthur Heymans0ab98d52022-04-07 19:14:48 +02003#include "assert.h"
Kyösti Mälkki84935f72021-01-11 20:13:34 +02004#include <acpi/acpi_gnvs.h>
Arthur Heymans96451a72021-10-28 15:14:18 +02005#include <stddef.h>
Rocky Phaguraafb7a812020-07-21 14:48:48 -07006#include <stdint.h>
7#include <string.h>
8#include <rmodule.h>
Raul E Rangelc5160982022-02-24 16:02:49 -07009#include <cbmem.h>
Rocky Phaguraafb7a812020-07-21 14:48:48 -070010#include <cpu/x86/smm.h>
11#include <commonlib/helpers.h>
12#include <console/console.h>
13#include <security/intel/stm/SmmStm.h>
14
15#define FXSAVE_SIZE 512
16#define SMM_CODE_SEGMENT_SIZE 0x10000
17/* FXSAVE area during relocation. While it may not be strictly needed the
18 SMM stub code relies on the FXSAVE area being non-zero to enable SSE
19 instructions within SMM mode. */
20static uint8_t fxsave_area_relocation[CONFIG_MAX_CPUS][FXSAVE_SIZE]
21__attribute__((aligned(16)));
22
23/*
24 * Components that make up the SMRAM:
25 * 1. Save state - the total save state memory used
26 * 2. Stack - stacks for the CPUs in the SMM handler
27 * 3. Stub - SMM stub code for calling into handler
28 * 4. Handler - C-based SMM handler.
29 *
30 * The components are assumed to consist of one consecutive region.
31 */
32
Rocky Phaguraafb7a812020-07-21 14:48:48 -070033/*
34 * The stub is the entry point that sets up protected mode and stacks for each
35 * CPU. It then calls into the SMM handler module. It is encoded as an rmodule.
36 */
37extern unsigned char _binary_smmstub_start[];
38
39/* Per CPU minimum stack size. */
40#define SMM_MINIMUM_STACK_SIZE 32
41
42struct cpu_smm_info {
43 uint8_t active;
44 uintptr_t smbase;
45 uintptr_t entry;
46 uintptr_t ss_start;
47 uintptr_t code_start;
48 uintptr_t code_end;
49};
50struct cpu_smm_info cpus[CONFIG_MAX_CPUS] = { 0 };
51
52/*
53 * This method creates a map of all the CPU entry points, save state locations
54 * and the beginning and end of code segments for each CPU. This map is used
55 * during relocation to properly align as many CPUs that can fit into the SMRAM
56 * region. For more information on how SMRAM works, refer to the latest Intel
57 * developer's manuals (volume 3, chapter 34). SMRAM is divided up into the
58 * following regions:
59 * +-----------------+ Top of SMRAM
60 * | | <- MSEG, FXSAVE
61 * +-----------------+
62 * | common |
63 * | smi handler | 64K
64 * | |
65 * +-----------------+
66 * | CPU 0 code seg |
67 * +-----------------+
68 * | CPU 1 code seg |
69 * +-----------------+
70 * | CPU x code seg |
71 * +-----------------+
72 * | |
73 * | |
74 * +-----------------+
75 * | stacks |
76 * +-----------------+ <- START of SMRAM
77 *
78 * The code below checks when a code segment is full and begins placing the remainder
79 * CPUs in the lower segments. The entry point for each CPU is smbase + 0x8000
80 * and save state is smbase + 0x8000 + (0x8000 - state save size). Save state
81 * area grows downward into the CPUs entry point. Therefore staggering too many
82 * CPUs in one 32K block will corrupt CPU0's entry code as the save states move
83 * downward.
84 * input : smbase of first CPU (all other CPUs
85 * will go below this address)
86 * input : num_cpus in the system. The map will
87 * be created from 0 to num_cpus.
88 */
Arthur Heymans0ab98d52022-04-07 19:14:48 +020089static int smm_create_map(const uintptr_t smbase, const unsigned int num_cpus,
90 const struct smm_loader_params *params)
Rocky Phaguraafb7a812020-07-21 14:48:48 -070091{
Rocky Phaguraafb7a812020-07-21 14:48:48 -070092 struct rmodule smm_stub;
Arthur Heymans0ab98d52022-04-07 19:14:48 +020093
94 if (ARRAY_SIZE(cpus) < num_cpus) {
95 printk(BIOS_ERR, "%s: increase MAX_CPUS in Kconfig\n", __func__);
96 return 0;
97 }
Rocky Phaguraafb7a812020-07-21 14:48:48 -070098
99 if (rmodule_parse(&_binary_smmstub_start, &smm_stub)) {
100 printk(BIOS_ERR, "%s: unable to get SMM module size\n", __func__);
101 return 0;
102 }
103
Arthur Heymans0ab98d52022-04-07 19:14:48 +0200104 /*
105 * How many CPUs can fit into one 64K segment?
106 * Make sure that the first stub does not overlap with the last save state of a segment.
107 */
108 const size_t stub_size = rmodule_memory_size(&smm_stub);
109 const size_t needed_ss_size = MAX(params->real_cpu_save_state_size, stub_size);
110 const size_t cpus_per_segment =
111 (SMM_CODE_SEGMENT_SIZE - SMM_ENTRY_OFFSET - stub_size) / needed_ss_size;
112
113 if (cpus_per_segment == 0) {
114 printk(BIOS_ERR, "%s: CPUs won't fit in segment. Broken stub or save state size\n",
115 __func__);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700116 return 0;
117 }
118
Arthur Heymans0ab98d52022-04-07 19:14:48 +0200119 for (unsigned int i = 0; i < num_cpus; i++) {
120 if (i % cpus_per_segment == 0)
121 printk(BIOS_DEBUG, "-------------NEW CODE SEGMENT --------------\n");
Arthur Heymansff69b6f2022-04-07 18:33:50 +0200122 printk(BIOS_DEBUG, "CPU 0x%x\n", i);
Arthur Heymans0ab98d52022-04-07 19:14:48 +0200123 /* We copy the same stub for each CPU so they all need the same 'smbase'. */
124 const size_t segment_number = i / cpus_per_segment;
125 cpus[i].smbase = smbase - SMM_CODE_SEGMENT_SIZE * segment_number
126 - needed_ss_size * (i % cpus_per_segment);
127 cpus[i].entry = cpus[i].smbase + SMM_ENTRY_OFFSET;
128 cpus[i].ss_start = cpus[i].smbase + SMM_CODE_SEGMENT_SIZE - needed_ss_size;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700129 cpus[i].code_start = cpus[i].entry;
130 cpus[i].code_end = cpus[i].entry + stub_size;
Arthur Heymans0ab98d52022-04-07 19:14:48 +0200131 printk(BIOS_DEBUG, " Stub [0x%lx-0x%lx[\n", cpus[i].code_start,
Arthur Heymansff69b6f2022-04-07 18:33:50 +0200132 cpus[i].code_end);
Arthur Heymans0ab98d52022-04-07 19:14:48 +0200133 printk(BIOS_DEBUG, " Save state [0x%lx-0x%lx[\n",
134 cpus[i].ss_start + needed_ss_size - params->real_cpu_save_state_size,
135 cpus[i].ss_start + needed_ss_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700136 cpus[i].active = 1;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700137 }
138
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700139 return 1;
140}
141
142/*
143 * This method expects the smm relocation map to be complete.
144 * This method does not read any HW registers, it simply uses a
145 * map that was created during SMM setup.
146 * input: cpu_num - cpu number which is used as an index into the
147 * map to return the smbase
148 */
149u32 smm_get_cpu_smbase(unsigned int cpu_num)
150{
151 if (cpu_num < CONFIG_MAX_CPUS) {
152 if (cpus[cpu_num].active)
153 return cpus[cpu_num].smbase;
154 }
155 return 0;
156}
157
158/*
159 * This method assumes that at least 1 CPU has been set up from
160 * which it will place other CPUs below its smbase ensuring that
161 * save state does not clobber the first CPUs init code segment. The init
162 * code which is the smm stub code is the same for all CPUs. They enter
163 * smm, setup stacks (based on their apic id), enter protected mode
164 * and then jump to the common smi handler. The stack is allocated
165 * at the beginning of smram (aka tseg base, not smbase). The stack
166 * pointer for each CPU is calculated by using its apic id
167 * (code is in smm_stub.s)
168 * Each entry point will now have the same stub code which, sets up the CPU
169 * stack, enters protected mode and then jumps to the smi handler. It is
170 * important to enter protected mode before the jump because the "jump to
171 * address" might be larger than the 20bit address supported by real mode.
172 * SMI entry right now is in real mode.
173 * input: smbase - this is the smbase of the first cpu not the smbase
174 * where tseg starts (aka smram_start). All CPUs code segment
175 * and stack will be below this point except for the common
176 * SMI handler which is one segment above
177 * input: num_cpus - number of cpus that need relocation including
178 * the first CPU (though its code is already loaded)
179 * input: top of stack (stacks work downward by default in Intel HW)
180 * output: return -1, if runtime smi code could not be installed. In
181 * this case SMM will not work and any SMI's generated will
182 * cause a CPU shutdown or general protection fault because
183 * the appropriate smi handling code was not installed
184 */
185
186static int smm_place_entry_code(uintptr_t smbase, unsigned int num_cpus,
Arthur Heymans9ddd9002020-12-03 11:02:42 +0100187 uintptr_t stack_top, const struct smm_loader_params *params)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700188{
189 unsigned int i;
190 unsigned int size;
Arthur Heymansdfff5c22021-02-15 23:39:01 +0100191
192 /*
193 * Ensure there was enough space and the last CPUs smbase
194 * did not encroach upon the stack. Stack top is smram start
195 * + size of stack.
196 */
197 if (cpus[num_cpus].active) {
Arthur Heymansad0116c2021-02-15 23:46:06 +0100198 if (cpus[num_cpus - 1].smbase + SMM_ENTRY_OFFSET < stack_top) {
Arthur Heymansdfff5c22021-02-15 23:39:01 +0100199 printk(BIOS_ERR, "%s: stack encroachment\n", __func__);
Paul Menzel2ea95952021-05-16 19:53:43 +0200200 printk(BIOS_ERR, "%s: smbase %lx, stack_top %lx\n",
Arthur Heymansdfff5c22021-02-15 23:39:01 +0100201 __func__, cpus[num_cpus].smbase, stack_top);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700202 return 0;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700203 }
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700204 }
205
Paul Menzel2ea95952021-05-16 19:53:43 +0200206 printk(BIOS_INFO, "%s: smbase %lx, stack_top %lx\n",
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700207 __func__, cpus[num_cpus-1].smbase, stack_top);
208
209 /* start at 1, the first CPU stub code is already there */
210 size = cpus[0].code_end - cpus[0].code_start;
211 for (i = 1; i < num_cpus; i++) {
212 memcpy((int *)cpus[i].code_start, (int *)cpus[0].code_start, size);
213 printk(BIOS_DEBUG,
Paul Menzel2ea95952021-05-16 19:53:43 +0200214 "SMM Module: placing smm entry code at %lx, cpu # 0x%x\n",
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700215 cpus[i].code_start, i);
Paul Menzel2ea95952021-05-16 19:53:43 +0200216 printk(BIOS_DEBUG, "%s: copying from %lx to %lx 0x%x bytes\n",
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700217 __func__, cpus[0].code_start, cpus[i].code_start, size);
218 }
219 return 1;
220}
221
Arthur Heymans96451a72021-10-28 15:14:18 +0200222static uintptr_t stack_top;
223static size_t g_stack_size;
224
225int smm_setup_stack(const uintptr_t perm_smbase, const size_t perm_smram_size,
226 const unsigned int total_cpus, const size_t stack_size)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700227{
Arthur Heymans96451a72021-10-28 15:14:18 +0200228 /* Need a minimum stack size and alignment. */
229 if (stack_size <= SMM_MINIMUM_STACK_SIZE || (stack_size & 3) != 0) {
230 printk(BIOS_ERR, "%s: need minimum stack size\n", __func__);
231 return -1;
232 }
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700233
Arthur Heymans96451a72021-10-28 15:14:18 +0200234 const size_t total_stack_size = total_cpus * stack_size;
235 if (total_stack_size >= perm_smram_size) {
236 printk(BIOS_ERR, "%s: Stack won't fit smram\n", __func__);
237 return -1;
238 }
239 stack_top = perm_smbase + total_stack_size;
240 g_stack_size = stack_size;
241 return 0;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700242}
243
244/*
245 * Place the staggered entry points for each CPU. The entry points are
246 * staggered by the per CPU SMM save state size extending down from
247 * SMM_ENTRY_OFFSET.
248 */
249static int smm_stub_place_staggered_entry_points(char *base,
250 const struct smm_loader_params *params, const struct rmodule *smm_stub)
251{
252 size_t stub_entry_offset;
253 int rc = 1;
254 stub_entry_offset = rmodule_entry_offset(smm_stub);
255 /* Each CPU now has its own stub code, which enters protected mode,
256 * sets up the stack, and then jumps to common SMI handler
257 */
258 if (params->num_concurrent_save_states > 1 || stub_entry_offset != 0) {
Arthur Heymans9ddd9002020-12-03 11:02:42 +0100259 rc = smm_place_entry_code((uintptr_t)base,
260 params->num_concurrent_save_states,
Arthur Heymans96451a72021-10-28 15:14:18 +0200261 stack_top, params);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700262 }
263 return rc;
264}
265
266/*
267 * The stub setup code assumes it is completely contained within the
268 * default SMRAM size (0x10000) for the default SMI handler (entry at
269 * 0x30000), but no assumption should be made for the permanent SMI handler.
270 * The placement of CPU entry points for permanent handler are determined
271 * by the number of CPUs in the system and the amount of SMRAM.
Arthur Heymanse6c35232021-02-16 13:19:18 +0100272 * There are potentially 2 regions to place
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700273 * within the default SMRAM size:
274 * 1. Save state areas
275 * 2. Stub code
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700276 *
Arthur Heymanse6c35232021-02-16 13:19:18 +0100277 * The save state always lives at the top of the CPUS smbase (and the entry
278 * point is at offset 0x8000). This allows only a certain number of CPUs with
279 * staggered entry points until the save state area comes down far enough to
280 * overwrite/corrupt the entry code (stub code). Therefore, an SMM map is
281 * created to avoid this corruption, see smm_create_map() above.
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700282 * This module setup code works for the default (0x30000) SMM handler setup and the
283 * permanent SMM handler.
Arthur Heymanse6c35232021-02-16 13:19:18 +0100284 * The CPU stack is decided at runtime in the stub and is treaded as a continuous
285 * region. As this might not fit the default SMRAM region, the same region used
Arthur Heymans96451a72021-10-28 15:14:18 +0200286 * by the permanent handler can be used during relocation.
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700287 */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200288static int smm_module_setup_stub(const uintptr_t smbase, const size_t smm_size,
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700289 struct smm_loader_params *params,
Arthur Heymans96451a72021-10-28 15:14:18 +0200290 void *const fxsave_area)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700291{
292 size_t total_save_state_size;
293 size_t smm_stub_size;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200294 uintptr_t smm_stub_loc;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700295 size_t size;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200296 uintptr_t base;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700297 size_t i;
298 struct smm_stub_params *stub_params;
299 struct rmodule smm_stub;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700300 base = smbase;
301 size = smm_size;
302
303 /* The number of concurrent stacks cannot exceed CONFIG_MAX_CPUS. */
Arthur Heymans2412c812021-10-28 15:19:39 +0200304 if (params->num_cpus > CONFIG_MAX_CPUS) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700305 printk(BIOS_ERR, "%s: not enough stacks\n", __func__);
306 return -1;
307 }
308
309 /* Fail if can't parse the smm stub rmodule. */
310 if (rmodule_parse(&_binary_smmstub_start, &smm_stub)) {
311 printk(BIOS_ERR, "%s: unable to parse smm stub\n", __func__);
312 return -1;
313 }
314
315 /* Adjust remaining size to account for save state. */
316 total_save_state_size = params->per_cpu_save_state_size *
317 params->num_concurrent_save_states;
318 if (total_save_state_size > size) {
319 printk(BIOS_ERR,
320 "%s: more state save space needed:need -> %zx:available->%zx\n",
321 __func__, total_save_state_size, size);
322 return -1;
323 }
324
325 size -= total_save_state_size;
326
327 /* The save state size encroached over the first SMM entry point. */
Arthur Heymansad0116c2021-02-15 23:46:06 +0100328 if (size <= SMM_ENTRY_OFFSET) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700329 printk(BIOS_ERR, "%s: encroachment over SMM entry point\n", __func__);
Paul Menzel85ac0672021-05-16 20:01:43 +0200330 printk(BIOS_ERR, "%s: state save size: %zx : smm_entry_offset -> %zx\n",
Arthur Heymansad0116c2021-02-15 23:46:06 +0100331 __func__, size, (size_t)SMM_ENTRY_OFFSET);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700332 return -1;
333 }
334
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700335 smm_stub_size = rmodule_memory_size(&smm_stub);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700336
337 /* Put the stub at the main entry point */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200338 smm_stub_loc = base + SMM_ENTRY_OFFSET;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700339
340 /* Stub is too big to fit. */
Arthur Heymansad0116c2021-02-15 23:46:06 +0100341 if (smm_stub_size > (size - SMM_ENTRY_OFFSET)) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700342 printk(BIOS_ERR, "%s: stub is too big to fit\n", __func__);
343 return -1;
344 }
345
Arthur Heymans96451a72021-10-28 15:14:18 +0200346 if (stack_top == 0) {
Arthur Heymans1e1d5d62021-03-16 13:44:05 +0100347 printk(BIOS_ERR, "%s: error assigning stacks\n", __func__);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700348 return -1;
349 }
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700350 /* Load the stub. */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200351 if (rmodule_load((void *)smm_stub_loc, &smm_stub)) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700352 printk(BIOS_ERR, "%s: load module failed\n", __func__);
353 return -1;
354 }
355
Arthur Heymanscfd32242021-10-28 13:59:54 +0200356 if (!smm_stub_place_staggered_entry_points((void *)base, params, &smm_stub)) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700357 printk(BIOS_ERR, "%s: staggered entry points failed\n", __func__);
358 return -1;
359 }
360
361 /* Setup the parameters for the stub code. */
362 stub_params = rmodule_parameters(&smm_stub);
Arthur Heymans96451a72021-10-28 15:14:18 +0200363 stub_params->stack_top = stack_top;
364 stub_params->stack_size = g_stack_size;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700365 stub_params->c_handler = (uintptr_t)params->handler;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700366 stub_params->fxsave_area = (uintptr_t)fxsave_area;
367 stub_params->fxsave_area_size = FXSAVE_SIZE;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700368
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700369 printk(BIOS_DEBUG,
370 "%s: stack_top = 0x%x\n", __func__, stub_params->stack_top);
Arthur Heymans96451a72021-10-28 15:14:18 +0200371 printk(BIOS_DEBUG, "%s: per cpu stack_size = 0x%x\n",
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700372 __func__, stub_params->stack_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700373 printk(BIOS_DEBUG, "%s: runtime.start32_offset = 0x%x\n", __func__,
Arthur Heymans1dfa46e2021-02-15 16:19:33 +0100374 stub_params->start32_offset);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700375 printk(BIOS_DEBUG, "%s: runtime.smm_size = 0x%zx\n",
376 __func__, smm_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700377
378 /* Initialize the APIC id to CPU number table to be 1:1 */
Arthur Heymans2412c812021-10-28 15:19:39 +0200379 for (i = 0; i < params->num_cpus; i++)
Arthur Heymansed4be452021-02-15 13:20:35 +0100380 stub_params->apic_id_to_cpu[i] = i;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700381
382 /* Allow the initiator to manipulate SMM stub parameters. */
Arthur Heymansed4be452021-02-15 13:20:35 +0100383 params->stub_params = stub_params;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700384
Arthur Heymanscfd32242021-10-28 13:59:54 +0200385 printk(BIOS_DEBUG, "SMM Module: stub loaded at %lx. Will call %p\n",
Arthur Heymans166d2ac2021-03-16 14:26:26 +0100386 smm_stub_loc, params->handler);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700387 return 0;
388}
389
390/*
391 * smm_setup_relocation_handler assumes the callback is already loaded in
392 * memory. i.e. Another SMM module isn't chained to the stub. The other
393 * assumption is that the stub will be entered from the default SMRAM
394 * location: 0x30000 -> 0x40000.
395 */
Arthur Heymans96451a72021-10-28 15:14:18 +0200396int smm_setup_relocation_handler(struct smm_loader_params *params)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700397{
Arthur Heymanscfd32242021-10-28 13:59:54 +0200398 uintptr_t smram = SMM_DEFAULT_BASE;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700399 printk(BIOS_SPEW, "%s: enter\n", __func__);
400 /* There can't be more than 1 concurrent save state for the relocation
401 * handler because all CPUs default to 0x30000 as SMBASE. */
402 if (params->num_concurrent_save_states > 1)
403 return -1;
404
405 /* A handler has to be defined to call for relocation. */
406 if (params->handler == NULL)
407 return -1;
408
409 /* Since the relocation handler always uses stack, adjust the number
410 * of concurrent stack users to be CONFIG_MAX_CPUS. */
Arthur Heymans2412c812021-10-28 15:19:39 +0200411 if (params->num_cpus == 0)
412 params->num_cpus = CONFIG_MAX_CPUS;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700413
John Zhao457c6612021-04-21 10:13:17 -0700414 printk(BIOS_SPEW, "%s: exit\n", __func__);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700415 return smm_module_setup_stub(smram, SMM_DEFAULT_SIZE,
Arthur Heymans96451a72021-10-28 15:14:18 +0200416 params, fxsave_area_relocation);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700417}
418
Arthur Heymansb4ba2892021-10-28 16:48:36 +0200419static int smm_load_module_aseg(const uintptr_t smram_base, const size_t smram_size,
420 struct smm_loader_params *params);
421
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700422/*
423 *The SMM module is placed within the provided region in the following
424 * manner:
425 * +-----------------+ <- smram + size
426 * | BIOS resource |
427 * | list (STM) |
428 * +-----------------+
429 * | fxsave area |
430 * +-----------------+
431 * | smi handler |
432 * | ... |
433 * +-----------------+ <- cpu0
434 * | stub code | <- cpu1
435 * | stub code | <- cpu2
436 * | stub code | <- cpu3, etc
437 * | |
438 * | |
439 * | |
440 * | stacks |
441 * +-----------------+ <- smram start
442
443 * It should be noted that this algorithm will not work for
444 * SMM_DEFAULT_SIZE SMRAM regions such as the A segment. This algorithm
445 * expects a region large enough to encompass the handler and stacks
446 * as well as the SMM_DEFAULT_SIZE.
447 */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200448int smm_load_module(const uintptr_t smram_base, const size_t smram_size,
449 struct smm_loader_params *params)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700450{
451 struct rmodule smm_mod;
Arthur Heymans823b1a82021-02-15 16:02:10 +0100452 struct smm_runtime *handler_mod_params;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700453 size_t total_stack_size;
454 size_t handler_size;
455 size_t module_alignment;
456 size_t alignment_size;
457 size_t fxsave_size;
458 void *fxsave_area;
459 size_t total_size = 0;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200460 uintptr_t base; /* The base for the permanent handler */
Raul E Rangelc5160982022-02-24 16:02:49 -0700461 const struct cbmem_entry *cbmemc;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200462
Arthur Heymansb4ba2892021-10-28 16:48:36 +0200463 if (CONFIG(SMM_ASEG))
464 return smm_load_module_aseg(smram_base, smram_size, params);
465
Arthur Heymanscfd32242021-10-28 13:59:54 +0200466 if (smram_size <= SMM_DEFAULT_SIZE)
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700467 return -1;
468
469 /* Load main SMI handler at the top of SMRAM
470 * everything else will go below
471 */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200472 base = smram_base;
473 base += smram_size;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700474
475 /* Fail if can't parse the smm rmodule. */
476 if (rmodule_parse(&_binary_smm_start, &smm_mod))
477 return -1;
478
479 /* Clear SMM region */
480 if (CONFIG(DEBUG_SMI))
Arthur Heymanscfd32242021-10-28 13:59:54 +0200481 memset((void *)smram_base, 0xcd, smram_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700482
Arthur Heymans96451a72021-10-28 15:14:18 +0200483 total_stack_size = stack_top - smram_base;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700484 total_size += total_stack_size;
485 /* Stacks are the base of SMRAM */
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700486
487 /* MSEG starts at the top of SMRAM and works down */
488 if (CONFIG(STM)) {
489 base -= CONFIG_MSEG_SIZE + CONFIG_BIOS_RESOURCE_LIST_SIZE;
490 total_size += CONFIG_MSEG_SIZE + CONFIG_BIOS_RESOURCE_LIST_SIZE;
491 }
492
493 /* FXSAVE goes below MSEG */
494 if (CONFIG(SSE)) {
Arthur Heymans2412c812021-10-28 15:19:39 +0200495 fxsave_size = FXSAVE_SIZE * params->num_cpus;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200496 fxsave_area = (char *)base - fxsave_size;
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700497 base -= fxsave_size;
498 total_size += fxsave_size;
499 } else {
500 fxsave_size = 0;
501 fxsave_area = NULL;
502 }
503
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700504 handler_size = rmodule_memory_size(&smm_mod);
505 base -= handler_size;
506 total_size += handler_size;
507 module_alignment = rmodule_load_alignment(&smm_mod);
Arthur Heymanscfd32242021-10-28 13:59:54 +0200508 alignment_size = module_alignment - (base % module_alignment);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700509 if (alignment_size != module_alignment) {
510 handler_size += alignment_size;
511 base += alignment_size;
512 }
513
514 printk(BIOS_DEBUG,
515 "%s: total_smm_space_needed %zx, available -> %zx\n",
Arthur Heymanscfd32242021-10-28 13:59:54 +0200516 __func__, total_size, smram_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700517
518 /* Does the required amount of memory exceed the SMRAM region size? */
Arthur Heymanscfd32242021-10-28 13:59:54 +0200519 if (total_size > smram_size) {
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700520 printk(BIOS_ERR, "%s: need more SMRAM\n", __func__);
521 return -1;
522 }
523 if (handler_size > SMM_CODE_SEGMENT_SIZE) {
524 printk(BIOS_ERR, "%s: increase SMM_CODE_SEGMENT_SIZE: handler_size = %zx\n",
525 __func__, handler_size);
526 return -1;
527 }
528
Arthur Heymanscfd32242021-10-28 13:59:54 +0200529 if (rmodule_load((void *)base, &smm_mod))
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700530 return -1;
531
532 params->handler = rmodule_entry(&smm_mod);
Arthur Heymans823b1a82021-02-15 16:02:10 +0100533 handler_mod_params = rmodule_parameters(&smm_mod);
Arthur Heymanscfd32242021-10-28 13:59:54 +0200534 handler_mod_params->smbase = smram_base;
535 handler_mod_params->smm_size = smram_size;
Arthur Heymans478f3d82021-02-15 19:39:01 +0100536 handler_mod_params->save_state_size = params->real_cpu_save_state_size;
Arthur Heymans2412c812021-10-28 15:19:39 +0200537 handler_mod_params->num_cpus = params->num_cpus;
Arthur Heymans823b1a82021-02-15 16:02:10 +0100538 handler_mod_params->gnvs_ptr = (uintptr_t)acpi_get_gnvs();
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700539
Raul E Rangelc5160982022-02-24 16:02:49 -0700540 if (CONFIG(CONSOLE_CBMEM) && (cbmemc = cbmem_entry_find(CBMEM_ID_CONSOLE))) {
541 handler_mod_params->cbmemc = cbmem_entry_start(cbmemc);
542 handler_mod_params->cbmemc_size = cbmem_entry_size(cbmemc);
543 } else {
544 handler_mod_params->cbmemc = 0;
545 handler_mod_params->cbmemc_size = 0;
546 }
547
Arthur Heymanscfd32242021-10-28 13:59:54 +0200548 printk(BIOS_DEBUG, "%s: smram_start: 0x%lx\n", __func__, smram_base);
549 printk(BIOS_DEBUG, "%s: smram_end: %lx\n", __func__, smram_base + smram_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700550 printk(BIOS_DEBUG, "%s: handler start %p\n",
551 __func__, params->handler);
552 printk(BIOS_DEBUG, "%s: handler_size %zx\n",
553 __func__, handler_size);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700554 printk(BIOS_DEBUG, "%s: fxsave_area %p\n",
555 __func__, fxsave_area);
556 printk(BIOS_DEBUG, "%s: fxsave_size %zx\n",
557 __func__, fxsave_size);
558 printk(BIOS_DEBUG, "%s: CONFIG_MSEG_SIZE 0x%x\n",
559 __func__, CONFIG_MSEG_SIZE);
560 printk(BIOS_DEBUG, "%s: CONFIG_BIOS_RESOURCE_LIST_SIZE 0x%x\n",
561 __func__, CONFIG_BIOS_RESOURCE_LIST_SIZE);
562
Arthur Heymans823b1a82021-02-15 16:02:10 +0100563 printk(BIOS_DEBUG, "%s: handler_mod_params.smbase = 0x%x\n", __func__,
564 handler_mod_params->smbase);
565 printk(BIOS_DEBUG, "%s: per_cpu_save_state_size = 0x%x\n", __func__,
566 handler_mod_params->save_state_size);
567 printk(BIOS_DEBUG, "%s: num_cpus = 0x%x\n", __func__, handler_mod_params->num_cpus);
Raul E Rangelc5160982022-02-24 16:02:49 -0700568 printk(BIOS_DEBUG, "%s: cbmemc = %p, cbmemc_size = %#x\n", __func__,
569 handler_mod_params->cbmemc, handler_mod_params->cbmemc_size);
Arthur Heymans823b1a82021-02-15 16:02:10 +0100570 printk(BIOS_DEBUG, "%s: total_save_state_size = 0x%x\n", __func__,
571 (handler_mod_params->save_state_size * handler_mod_params->num_cpus));
572
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700573 /* CPU 0 smbase goes first, all other CPUs
574 * will be staggered below
575 */
576 base -= SMM_CODE_SEGMENT_SIZE;
Arthur Heymanscfd32242021-10-28 13:59:54 +0200577 printk(BIOS_DEBUG, "%s: cpu0 entry: %lx\n", __func__, base);
Arthur Heymansdfff5c22021-02-15 23:39:01 +0100578
Arthur Heymanscfd32242021-10-28 13:59:54 +0200579 if (!smm_create_map(base, params->num_concurrent_save_states, params)) {
Arthur Heymansdfff5c22021-02-15 23:39:01 +0100580 printk(BIOS_ERR, "%s: Error creating CPU map\n", __func__);
581 return -1;
582 }
583
Arthur Heymans2412c812021-10-28 15:19:39 +0200584 for (int i = 0; i < params->num_cpus; i++) {
Arthur Heymans64d9e852021-02-15 18:55:40 +0100585 handler_mod_params->save_state_top[i] =
586 cpus[i].ss_start + params->per_cpu_save_state_size;
587 }
588
Arthur Heymans96451a72021-10-28 15:14:18 +0200589 return smm_module_setup_stub(base, smram_size, params, fxsave_area);
Rocky Phaguraafb7a812020-07-21 14:48:48 -0700590}
Arthur Heymansb4ba2892021-10-28 16:48:36 +0200591
592/*
593 *The SMM module is placed within the provided region in the following
594 * manner:
595 * +-----------------+ <- smram + size == 0x10000
596 * | save states |
597 * +-----------------+
598 * | fxsave area |
599 * +-----------------+
600 * | smi handler |
601 * | ... |
602 * +-----------------+ <- cpu0
603 * | stub code | <- cpu1
604 * | stub code | <- cpu2
605 * | stub code | <- cpu3, etc
606 * | |
607 * | |
608 * | |
609 * | stacks |
610 * +-----------------+ <- smram start = 0xA0000
611 */
612static int smm_load_module_aseg(const uintptr_t smram_base, const size_t smram_size,
613 struct smm_loader_params *params)
614{
615 struct rmodule smm_mod;
616 struct smm_runtime *handler_mod_params;
617
618 if (smram_size != SMM_DEFAULT_SIZE)
619 return -1;
620
621 if (smram_base != SMM_BASE)
622 return -1;
623
624 /* Fail if can't parse the smm rmodule. */
625 if (rmodule_parse(&_binary_smm_start, &smm_mod))
626 return -1;
627
628 if (!smm_create_map(smram_base, params->num_concurrent_save_states, params)) {
629 printk(BIOS_ERR, "%s: Error creating CPU map\n", __func__);
630 return -1;
631 }
632
633 const uintptr_t entry0_end = cpus[0].code_end;
634 const uintptr_t save_state_base = cpus[params->num_cpus - 1].ss_start;
635 const size_t fxsave_size = FXSAVE_SIZE * params->num_cpus;
636 const uintptr_t fxsave_base = ALIGN_DOWN(save_state_base - fxsave_size, 16);
637
638 if (fxsave_base <= entry0_end) {
639 printk(BIOS_ERR, "%s, fxsave %lx won't fit smram\n", __func__, fxsave_base);
640 return -1;
641 }
642
643 const size_t handler_size = rmodule_memory_size(&smm_mod);
644 const size_t module_alignment = rmodule_load_alignment(&smm_mod);
645 const uintptr_t module_base = ALIGN_DOWN(fxsave_base - handler_size, module_alignment);
646
647 if (module_base <= entry0_end) {
648 printk(BIOS_ERR, "%s, module won't fit smram\n", __func__);
649 return -1;
650 }
651
652 if (rmodule_load((void *)module_base, &smm_mod))
653 return -1;
654
655 params->handler = rmodule_entry(&smm_mod);
656 handler_mod_params = rmodule_parameters(&smm_mod);
657 handler_mod_params->smbase = smram_base;
658 handler_mod_params->smm_size = smram_size;
659 handler_mod_params->save_state_size = params->real_cpu_save_state_size;
660 handler_mod_params->num_cpus = params->num_cpus;
661 handler_mod_params->gnvs_ptr = (uintptr_t)acpi_get_gnvs();
662
663 for (int i = 0; i < params->num_cpus; i++) {
664 handler_mod_params->save_state_top[i] =
665 cpus[i].ss_start + params->per_cpu_save_state_size;
666 }
667
668 printk(BIOS_DEBUG, "%s: smram_start: 0x%lx\n", __func__, smram_base);
669 printk(BIOS_DEBUG, "%s: smram_end: %lx\n", __func__, smram_base + smram_size);
670 printk(BIOS_DEBUG, "%s: handler start %p\n", __func__, params->handler);
671 printk(BIOS_DEBUG, "%s: handler_size %zx\n", __func__, handler_size);
672 printk(BIOS_DEBUG, "%s: fxsave_area %lx\n", __func__, fxsave_base);
673 printk(BIOS_DEBUG, "%s: fxsave_size %zx\n", __func__, fxsave_size);
674
675 printk(BIOS_DEBUG, "%s: handler_mod_params.smbase = 0x%x\n", __func__,
676 handler_mod_params->smbase);
677 printk(BIOS_DEBUG, "%s: per_cpu_save_state_size = 0x%x\n", __func__,
678 handler_mod_params->save_state_size);
679 printk(BIOS_DEBUG, "%s: num_cpus = 0x%x\n", __func__, handler_mod_params->num_cpus);
680 printk(BIOS_DEBUG, "%s: total_save_state_size = 0x%x\n", __func__,
681 (handler_mod_params->save_state_size * handler_mod_params->num_cpus));
682
683 return smm_module_setup_stub(smram_base, smram_size, params, (void *)fxsave_base);
684}