Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 3 | #include "assert.h" |
Kyösti Mälkki | 84935f7 | 2021-01-11 20:13:34 +0200 | [diff] [blame] | 4 | #include <acpi/acpi_gnvs.h> |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 5 | #include <stddef.h> |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | #include <string.h> |
| 8 | #include <rmodule.h> |
Raul E Rangel | c516098 | 2022-02-24 16:02:49 -0700 | [diff] [blame] | 9 | #include <cbmem.h> |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 10 | #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. */ |
| 20 | static 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 33 | /* |
| 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 | */ |
| 37 | extern unsigned char _binary_smmstub_start[]; |
| 38 | |
| 39 | /* Per CPU minimum stack size. */ |
| 40 | #define SMM_MINIMUM_STACK_SIZE 32 |
| 41 | |
| 42 | struct 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 | }; |
| 50 | struct 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 Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 89 | static int smm_create_map(const uintptr_t smbase, const unsigned int num_cpus, |
| 90 | const struct smm_loader_params *params) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 91 | { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 92 | struct rmodule smm_stub; |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 93 | |
| 94 | if (ARRAY_SIZE(cpus) < num_cpus) { |
| 95 | printk(BIOS_ERR, "%s: increase MAX_CPUS in Kconfig\n", __func__); |
| 96 | return 0; |
| 97 | } |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 98 | |
| 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 Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 104 | /* |
| 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 119 | 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 Heymans | ff69b6f | 2022-04-07 18:33:50 +0200 | [diff] [blame] | 122 | printk(BIOS_DEBUG, "CPU 0x%x\n", i); |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 123 | /* 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 129 | cpus[i].code_start = cpus[i].entry; |
| 130 | cpus[i].code_end = cpus[i].entry + stub_size; |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 131 | printk(BIOS_DEBUG, " Stub [0x%lx-0x%lx[\n", cpus[i].code_start, |
Arthur Heymans | ff69b6f | 2022-04-07 18:33:50 +0200 | [diff] [blame] | 132 | cpus[i].code_end); |
Arthur Heymans | 0ab98d5 | 2022-04-07 19:14:48 +0200 | [diff] [blame^] | 133 | 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 136 | cpus[i].active = 1; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 139 | 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 | */ |
| 149 | u32 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 | |
| 186 | static int smm_place_entry_code(uintptr_t smbase, unsigned int num_cpus, |
Arthur Heymans | 9ddd900 | 2020-12-03 11:02:42 +0100 | [diff] [blame] | 187 | uintptr_t stack_top, const struct smm_loader_params *params) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 188 | { |
| 189 | unsigned int i; |
| 190 | unsigned int size; |
Arthur Heymans | dfff5c2 | 2021-02-15 23:39:01 +0100 | [diff] [blame] | 191 | |
| 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 Heymans | ad0116c | 2021-02-15 23:46:06 +0100 | [diff] [blame] | 198 | if (cpus[num_cpus - 1].smbase + SMM_ENTRY_OFFSET < stack_top) { |
Arthur Heymans | dfff5c2 | 2021-02-15 23:39:01 +0100 | [diff] [blame] | 199 | printk(BIOS_ERR, "%s: stack encroachment\n", __func__); |
Paul Menzel | 2ea9595 | 2021-05-16 19:53:43 +0200 | [diff] [blame] | 200 | printk(BIOS_ERR, "%s: smbase %lx, stack_top %lx\n", |
Arthur Heymans | dfff5c2 | 2021-02-15 23:39:01 +0100 | [diff] [blame] | 201 | __func__, cpus[num_cpus].smbase, stack_top); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 202 | return 0; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 203 | } |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Paul Menzel | 2ea9595 | 2021-05-16 19:53:43 +0200 | [diff] [blame] | 206 | printk(BIOS_INFO, "%s: smbase %lx, stack_top %lx\n", |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 207 | __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 Menzel | 2ea9595 | 2021-05-16 19:53:43 +0200 | [diff] [blame] | 214 | "SMM Module: placing smm entry code at %lx, cpu # 0x%x\n", |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 215 | cpus[i].code_start, i); |
Paul Menzel | 2ea9595 | 2021-05-16 19:53:43 +0200 | [diff] [blame] | 216 | printk(BIOS_DEBUG, "%s: copying from %lx to %lx 0x%x bytes\n", |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 217 | __func__, cpus[0].code_start, cpus[i].code_start, size); |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 222 | static uintptr_t stack_top; |
| 223 | static size_t g_stack_size; |
| 224 | |
| 225 | int 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 227 | { |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 228 | /* 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 233 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 234 | 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 242 | } |
| 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 | */ |
| 249 | static 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 Heymans | 9ddd900 | 2020-12-03 11:02:42 +0100 | [diff] [blame] | 259 | rc = smm_place_entry_code((uintptr_t)base, |
| 260 | params->num_concurrent_save_states, |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 261 | stack_top, params); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 262 | } |
| 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 Heymans | e6c3523 | 2021-02-16 13:19:18 +0100 | [diff] [blame] | 272 | * There are potentially 2 regions to place |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 273 | * within the default SMRAM size: |
| 274 | * 1. Save state areas |
| 275 | * 2. Stub code |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 276 | * |
Arthur Heymans | e6c3523 | 2021-02-16 13:19:18 +0100 | [diff] [blame] | 277 | * 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 282 | * This module setup code works for the default (0x30000) SMM handler setup and the |
| 283 | * permanent SMM handler. |
Arthur Heymans | e6c3523 | 2021-02-16 13:19:18 +0100 | [diff] [blame] | 284 | * 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 Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 286 | * by the permanent handler can be used during relocation. |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 287 | */ |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 288 | static int smm_module_setup_stub(const uintptr_t smbase, const size_t smm_size, |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 289 | struct smm_loader_params *params, |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 290 | void *const fxsave_area) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 291 | { |
| 292 | size_t total_save_state_size; |
| 293 | size_t smm_stub_size; |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 294 | uintptr_t smm_stub_loc; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 295 | size_t size; |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 296 | uintptr_t base; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 297 | size_t i; |
| 298 | struct smm_stub_params *stub_params; |
| 299 | struct rmodule smm_stub; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 300 | base = smbase; |
| 301 | size = smm_size; |
| 302 | |
| 303 | /* The number of concurrent stacks cannot exceed CONFIG_MAX_CPUS. */ |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 304 | if (params->num_cpus > CONFIG_MAX_CPUS) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 305 | 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 Heymans | ad0116c | 2021-02-15 23:46:06 +0100 | [diff] [blame] | 328 | if (size <= SMM_ENTRY_OFFSET) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 329 | printk(BIOS_ERR, "%s: encroachment over SMM entry point\n", __func__); |
Paul Menzel | 85ac067 | 2021-05-16 20:01:43 +0200 | [diff] [blame] | 330 | printk(BIOS_ERR, "%s: state save size: %zx : smm_entry_offset -> %zx\n", |
Arthur Heymans | ad0116c | 2021-02-15 23:46:06 +0100 | [diff] [blame] | 331 | __func__, size, (size_t)SMM_ENTRY_OFFSET); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 335 | smm_stub_size = rmodule_memory_size(&smm_stub); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 336 | |
| 337 | /* Put the stub at the main entry point */ |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 338 | smm_stub_loc = base + SMM_ENTRY_OFFSET; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 339 | |
| 340 | /* Stub is too big to fit. */ |
Arthur Heymans | ad0116c | 2021-02-15 23:46:06 +0100 | [diff] [blame] | 341 | if (smm_stub_size > (size - SMM_ENTRY_OFFSET)) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 342 | printk(BIOS_ERR, "%s: stub is too big to fit\n", __func__); |
| 343 | return -1; |
| 344 | } |
| 345 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 346 | if (stack_top == 0) { |
Arthur Heymans | 1e1d5d6 | 2021-03-16 13:44:05 +0100 | [diff] [blame] | 347 | printk(BIOS_ERR, "%s: error assigning stacks\n", __func__); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 348 | return -1; |
| 349 | } |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 350 | /* Load the stub. */ |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 351 | if (rmodule_load((void *)smm_stub_loc, &smm_stub)) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 352 | printk(BIOS_ERR, "%s: load module failed\n", __func__); |
| 353 | return -1; |
| 354 | } |
| 355 | |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 356 | if (!smm_stub_place_staggered_entry_points((void *)base, params, &smm_stub)) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 357 | 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 Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 363 | stub_params->stack_top = stack_top; |
| 364 | stub_params->stack_size = g_stack_size; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 365 | stub_params->c_handler = (uintptr_t)params->handler; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 366 | stub_params->fxsave_area = (uintptr_t)fxsave_area; |
| 367 | stub_params->fxsave_area_size = FXSAVE_SIZE; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 368 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 369 | printk(BIOS_DEBUG, |
| 370 | "%s: stack_top = 0x%x\n", __func__, stub_params->stack_top); |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 371 | printk(BIOS_DEBUG, "%s: per cpu stack_size = 0x%x\n", |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 372 | __func__, stub_params->stack_size); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 373 | printk(BIOS_DEBUG, "%s: runtime.start32_offset = 0x%x\n", __func__, |
Arthur Heymans | 1dfa46e | 2021-02-15 16:19:33 +0100 | [diff] [blame] | 374 | stub_params->start32_offset); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 375 | printk(BIOS_DEBUG, "%s: runtime.smm_size = 0x%zx\n", |
| 376 | __func__, smm_size); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 377 | |
| 378 | /* Initialize the APIC id to CPU number table to be 1:1 */ |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 379 | for (i = 0; i < params->num_cpus; i++) |
Arthur Heymans | ed4be45 | 2021-02-15 13:20:35 +0100 | [diff] [blame] | 380 | stub_params->apic_id_to_cpu[i] = i; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 381 | |
| 382 | /* Allow the initiator to manipulate SMM stub parameters. */ |
Arthur Heymans | ed4be45 | 2021-02-15 13:20:35 +0100 | [diff] [blame] | 383 | params->stub_params = stub_params; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 384 | |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 385 | printk(BIOS_DEBUG, "SMM Module: stub loaded at %lx. Will call %p\n", |
Arthur Heymans | 166d2ac | 2021-03-16 14:26:26 +0100 | [diff] [blame] | 386 | smm_stub_loc, params->handler); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 387 | 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 Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 396 | int smm_setup_relocation_handler(struct smm_loader_params *params) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 397 | { |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 398 | uintptr_t smram = SMM_DEFAULT_BASE; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 399 | 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 Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 411 | if (params->num_cpus == 0) |
| 412 | params->num_cpus = CONFIG_MAX_CPUS; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 413 | |
John Zhao | 457c661 | 2021-04-21 10:13:17 -0700 | [diff] [blame] | 414 | printk(BIOS_SPEW, "%s: exit\n", __func__); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 415 | return smm_module_setup_stub(smram, SMM_DEFAULT_SIZE, |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 416 | params, fxsave_area_relocation); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Arthur Heymans | b4ba289 | 2021-10-28 16:48:36 +0200 | [diff] [blame] | 419 | static int smm_load_module_aseg(const uintptr_t smram_base, const size_t smram_size, |
| 420 | struct smm_loader_params *params); |
| 421 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 422 | /* |
| 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 448 | int smm_load_module(const uintptr_t smram_base, const size_t smram_size, |
| 449 | struct smm_loader_params *params) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 450 | { |
| 451 | struct rmodule smm_mod; |
Arthur Heymans | 823b1a8 | 2021-02-15 16:02:10 +0100 | [diff] [blame] | 452 | struct smm_runtime *handler_mod_params; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 453 | 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 460 | uintptr_t base; /* The base for the permanent handler */ |
Raul E Rangel | c516098 | 2022-02-24 16:02:49 -0700 | [diff] [blame] | 461 | const struct cbmem_entry *cbmemc; |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 462 | |
Arthur Heymans | b4ba289 | 2021-10-28 16:48:36 +0200 | [diff] [blame] | 463 | if (CONFIG(SMM_ASEG)) |
| 464 | return smm_load_module_aseg(smram_base, smram_size, params); |
| 465 | |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 466 | if (smram_size <= SMM_DEFAULT_SIZE) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 467 | return -1; |
| 468 | |
| 469 | /* Load main SMI handler at the top of SMRAM |
| 470 | * everything else will go below |
| 471 | */ |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 472 | base = smram_base; |
| 473 | base += smram_size; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 474 | |
| 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 481 | memset((void *)smram_base, 0xcd, smram_size); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 482 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 483 | total_stack_size = stack_top - smram_base; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 484 | total_size += total_stack_size; |
| 485 | /* Stacks are the base of SMRAM */ |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 486 | |
| 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 Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 495 | fxsave_size = FXSAVE_SIZE * params->num_cpus; |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 496 | fxsave_area = (char *)base - fxsave_size; |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 497 | base -= fxsave_size; |
| 498 | total_size += fxsave_size; |
| 499 | } else { |
| 500 | fxsave_size = 0; |
| 501 | fxsave_area = NULL; |
| 502 | } |
| 503 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 504 | 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 508 | alignment_size = module_alignment - (base % module_alignment); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 509 | 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 516 | __func__, total_size, smram_size); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 517 | |
| 518 | /* Does the required amount of memory exceed the SMRAM region size? */ |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 519 | if (total_size > smram_size) { |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 520 | 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 529 | if (rmodule_load((void *)base, &smm_mod)) |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 530 | return -1; |
| 531 | |
| 532 | params->handler = rmodule_entry(&smm_mod); |
Arthur Heymans | 823b1a8 | 2021-02-15 16:02:10 +0100 | [diff] [blame] | 533 | handler_mod_params = rmodule_parameters(&smm_mod); |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 534 | handler_mod_params->smbase = smram_base; |
| 535 | handler_mod_params->smm_size = smram_size; |
Arthur Heymans | 478f3d8 | 2021-02-15 19:39:01 +0100 | [diff] [blame] | 536 | handler_mod_params->save_state_size = params->real_cpu_save_state_size; |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 537 | handler_mod_params->num_cpus = params->num_cpus; |
Arthur Heymans | 823b1a8 | 2021-02-15 16:02:10 +0100 | [diff] [blame] | 538 | handler_mod_params->gnvs_ptr = (uintptr_t)acpi_get_gnvs(); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 539 | |
Raul E Rangel | c516098 | 2022-02-24 16:02:49 -0700 | [diff] [blame] | 540 | 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 Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 548 | 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 550 | 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 554 | 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 Heymans | 823b1a8 | 2021-02-15 16:02:10 +0100 | [diff] [blame] | 563 | 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 Rangel | c516098 | 2022-02-24 16:02:49 -0700 | [diff] [blame] | 568 | printk(BIOS_DEBUG, "%s: cbmemc = %p, cbmemc_size = %#x\n", __func__, |
| 569 | handler_mod_params->cbmemc, handler_mod_params->cbmemc_size); |
Arthur Heymans | 823b1a8 | 2021-02-15 16:02:10 +0100 | [diff] [blame] | 570 | 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 Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 573 | /* CPU 0 smbase goes first, all other CPUs |
| 574 | * will be staggered below |
| 575 | */ |
| 576 | base -= SMM_CODE_SEGMENT_SIZE; |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 577 | printk(BIOS_DEBUG, "%s: cpu0 entry: %lx\n", __func__, base); |
Arthur Heymans | dfff5c2 | 2021-02-15 23:39:01 +0100 | [diff] [blame] | 578 | |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 579 | if (!smm_create_map(base, params->num_concurrent_save_states, params)) { |
Arthur Heymans | dfff5c2 | 2021-02-15 23:39:01 +0100 | [diff] [blame] | 580 | printk(BIOS_ERR, "%s: Error creating CPU map\n", __func__); |
| 581 | return -1; |
| 582 | } |
| 583 | |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 584 | for (int i = 0; i < params->num_cpus; i++) { |
Arthur Heymans | 64d9e85 | 2021-02-15 18:55:40 +0100 | [diff] [blame] | 585 | handler_mod_params->save_state_top[i] = |
| 586 | cpus[i].ss_start + params->per_cpu_save_state_size; |
| 587 | } |
| 588 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 589 | return smm_module_setup_stub(base, smram_size, params, fxsave_area); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 590 | } |
Arthur Heymans | b4ba289 | 2021-10-28 16:48:36 +0200 | [diff] [blame] | 591 | |
| 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 | */ |
| 612 | static 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 | } |