| 1 |
|
| 2 |
|
| 3 |
#include <arch/io.h> |
| 4 |
#include <arch/exception.h> |
| 5 |
#include <commonlib/region.h> |
| 6 |
#include <console/cbmem_console.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <cpu/cpu.h> |
| 9 |
#include <cpu/x86/smm.h> |
| 10 |
#include <rmodule.h> |
| 11 |
#include <types.h> |
| 12 |
#include <security/intel/stm/SmmStm.h> |
| 13 |
|
| 14 |
#if CONFIG(SPI_FLASH_SMM) |
| 15 |
#include <spi-generic.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
static int do_driver_init = 1; |
| 19 |
|
| 20 |
typedef enum { SMI_LOCKED, SMI_UNLOCKED } smi_semaphore; |
| 21 |
|
| 22 |
|
| 23 |
static volatile |
| 24 |
__attribute__((aligned(4))) smi_semaphore smi_handler_status = SMI_UNLOCKED; |
| 25 |
|
| 26 |
static const volatile |
| 27 |
__attribute((aligned(4), __section__(".module_parameters"))) struct smm_runtime smm_runtime; |
| 28 |
|
| 29 |
static int smi_obtain_lock(void) |
| 30 |
{ |
| 31 |
u8 ret = SMI_LOCKED; |
| 32 |
|
| 33 |
asm volatile ( |
| 34 |
"movb %2, %%al\n" |
| 35 |
"xchgb %%al, %1\n" |
| 36 |
"movb %%al, %0\n" |
| 37 |
: "=g" (ret), "=m" (smi_handler_status) |
| 38 |
: "g" (SMI_LOCKED) |
| 39 |
: "eax" |
| 40 |
); |
| 41 |
|
| 42 |
return (ret == SMI_UNLOCKED); |
| 43 |
} |
| 44 |
|
| 45 |
static void smi_release_lock(void) |
| 46 |
{ |
| 47 |
asm volatile ( |
| 48 |
"movb %1, %%al\n" |
| 49 |
"xchgb %%al, %0\n" |
| 50 |
: "=m" (smi_handler_status) |
| 51 |
: "g" (SMI_UNLOCKED) |
| 52 |
: "eax" |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
#if CONFIG(RUNTIME_CONFIGURABLE_SMM_LOGLEVEL) |
| 57 |
int get_console_loglevel(void) |
| 58 |
{ |
| 59 |
return smm_runtime.smm_log_level; |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
void smm_get_smmstore_com_buffer(uintptr_t *base, size_t *size) |
| 64 |
{ |
| 65 |
*base = smm_runtime.smmstore_com_buffer_base; |
| 66 |
*size = smm_runtime.smmstore_com_buffer_size; |
| 67 |
} |
| 68 |
|
| 69 |
void smm_get_cbmemc_buffer(void **buffer_out, size_t *size_out) |
| 70 |
{ |
| 71 |
*buffer_out = smm_runtime.cbmemc; |
| 72 |
*size_out = smm_runtime.cbmemc_size; |
| 73 |
} |
| 74 |
|
| 75 |
void io_trap_handler(int smif) |
| 76 |
{ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
printk(BIOS_DEBUG, "SMI function trap 0x%x: ", smif); |
| 81 |
|
| 82 |
if (mainboard_io_trap_handler(smif)) |
| 83 |
return; |
| 84 |
|
| 85 |
printk(BIOS_DEBUG, "Unknown function\n"); |
| 86 |
} |
| 87 |
|
| 88 |
static u32 pci_orig; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static void smi_backup_pci_address(void) |
| 94 |
{ |
| 95 |
pci_orig = inl(0xcf8); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static void smi_restore_pci_address(void) |
| 102 |
{ |
| 103 |
outl(pci_orig, 0xcf8); |
| 104 |
} |
| 105 |
|
| 106 |
struct global_nvs *gnvs; |
| 107 |
|
| 108 |
void *smm_get_save_state(int cpu) |
| 109 |
{ |
| 110 |
if (cpu >= smm_runtime.num_cpus) |
| 111 |
return NULL; |
| 112 |
|
| 113 |
return (void *)(smm_runtime.save_state_top[cpu] - |
| 114 |
(smm_runtime.save_state_size - STM_PSD_SIZE)); |
| 115 |
} |
| 116 |
|
| 117 |
uint32_t smm_revision(void) |
| 118 |
{ |
| 119 |
const uintptr_t save_state = (uintptr_t)(smm_get_save_state(0)); |
| 120 |
|
| 121 |
return *(uint32_t *)(save_state + smm_runtime.save_state_size |
| 122 |
- SMM_REVISION_OFFSET_FROM_TOP); |
| 123 |
} |
| 124 |
|
| 125 |
bool smm_region_overlaps_handler(const struct region *r) |
| 126 |
{ |
| 127 |
const struct region r_smm = region_create(smm_runtime.smbase, smm_runtime.smm_size); |
| 128 |
const struct region r_aseg = region_create(SMM_BASE, SMM_DEFAULT_SIZE); |
| 129 |
|
| 130 |
return region_overlap(&r_smm, r) || region_overlap(&r_aseg, r); |
| 131 |
} |
| 132 |
|
| 133 |
asmlinkage void smm_handler_start(void *arg) |
| 134 |
{ |
| 135 |
const struct smm_module_params *p; |
| 136 |
int cpu; |
| 137 |
uintptr_t actual_canary; |
| 138 |
uintptr_t expected_canary; |
| 139 |
|
| 140 |
p = arg; |
| 141 |
cpu = p->cpu; |
| 142 |
expected_canary = (uintptr_t)p->canary; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
gnvs = (void *)(uintptr_t)smm_runtime.gnvs_ptr; |
| 147 |
|
| 148 |
if (cpu >= CONFIG_MAX_CPUS) { |
| 149 |
|
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
if (!smi_obtain_lock()) { |
| 155 |
|
| 156 |
|
| 157 |
while (smi_handler_status == SMI_LOCKED) { |
| 158 |
asm volatile ( |
| 159 |
".byte 0xf3, 0x90\n" |
| 160 |
); |
| 161 |
} |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
smi_backup_pci_address(); |
| 166 |
|
| 167 |
smm_soc_early_init(); |
| 168 |
|
| 169 |
console_init(); |
| 170 |
|
| 171 |
printk(BIOS_SPEW, "\nSMI# #%d\n", cpu); |
| 172 |
|
| 173 |
if (CONFIG(DEBUG_SMI) && CONFIG(CONSOLE_SERIAL)) |
| 174 |
exception_init(); |
| 175 |
|
| 176 |
|
| 177 |
if (do_driver_init) { |
| 178 |
#if CONFIG(SPI_FLASH_SMM) |
| 179 |
spi_init(); |
| 180 |
#endif |
| 181 |
do_driver_init = 0; |
| 182 |
} |
| 183 |
|
| 184 |
cpu_smi_handler(); |
| 185 |
northbridge_smi_handler(); |
| 186 |
southbridge_smi_handler(); |
| 187 |
|
| 188 |
smi_restore_pci_address(); |
| 189 |
|
| 190 |
actual_canary = *p->canary; |
| 191 |
|
| 192 |
if (actual_canary != expected_canary) { |
| 193 |
printk(BIOS_DEBUG, "canary 0x%lx != 0x%lx\n", actual_canary, |
| 194 |
expected_canary); |
| 195 |
|
| 196 |
|
| 197 |
if (CONFIG(DEBUG_SMI)) |
| 198 |
die("SMM Handler caused a stack overflow\n"); |
| 199 |
} |
| 200 |
|
| 201 |
smm_soc_exit(); |
| 202 |
|
| 203 |
smi_release_lock(); |
| 204 |
|
| 205 |
|
| 206 |
southbridge_smi_set_eos(); |
| 207 |
} |
| 208 |
|
| 209 |
#if CONFIG(SMM_PCI_RESOURCE_STORE) |
| 210 |
const volatile struct smm_pci_resource_info *smm_get_pci_resource_store(void) |
| 211 |
{ |
| 212 |
return &smm_runtime.pci_resources[0]; |
| 213 |
} |
| 214 |
#endif |
| 215 |
|
| 216 |
RMODULE_ENTRY(smm_handler_start); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
int __weak mainboard_io_trap_handler(int smif) { return 0; } |
| 223 |
void __weak cpu_smi_handler(void) {} |
| 224 |
void __weak northbridge_smi_handler(void) {} |
| 225 |
void __weak southbridge_smi_handler(void) {} |
| 226 |
void __weak mainboard_smi_gpi(u32 gpi_sts) {} |
| 227 |
int __weak mainboard_smi_apmc(u8 data) { return 0; } |
| 228 |
void __weak mainboard_smi_sleep(u8 slp_typ) {} |
| 229 |
void __weak mainboard_smi_finalize(void) {} |
| 230 |
|
| 231 |
void __weak smm_soc_early_init(void) {} |
| 232 |
void __weak smm_soc_exit(void) {} |