Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 18 | * Foundation, Inc. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* NOTE: This handler assumes the SMM window goes from 0xa0000 |
| 22 | * to 0xaffff. In fact, at least on Intel Core CPUs (i945 chipset) |
| 23 | * the SMM window is 128K big, covering 0xa0000 to 0xbffff. |
| 24 | * So there is a lot of potential for growth in here. Let's stick |
| 25 | * to 64k if we can though. |
| 26 | */ |
| 27 | |
Patrick Georgi | ce2564a | 2015-09-05 20:21:24 +0200 | [diff] [blame^] | 28 | #include <kconfig.h> |
| 29 | #include <config.h> |
| 30 | #define LAPIC_BASE_MSR 0x1b |
| 31 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 32 | /* |
| 33 | * +--------------------------------+ 0xaffff |
| 34 | * | Save State Map Node 0 | |
| 35 | * | Save State Map Node 1 | |
| 36 | * | Save State Map Node 2 | |
| 37 | * | Save State Map Node 3 | |
| 38 | * | ... | |
| 39 | * +--------------------------------+ 0xaf000 |
| 40 | * | | |
| 41 | * | | |
| 42 | * | | |
| 43 | * +--------------------------------+ 0xa8400 |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 44 | * | SMM Entry Node 0 (+ stack) | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 45 | * +--------------------------------+ 0xa8000 |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 46 | * | SMM Entry Node 1 (+ stack) | |
| 47 | * | SMM Entry Node 2 (+ stack) | |
| 48 | * | SMM Entry Node 3 (+ stack) | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 49 | * | ... | |
| 50 | * +--------------------------------+ 0xa7400 |
| 51 | * | | |
| 52 | * | SMM Handler | |
| 53 | * | | |
| 54 | * +--------------------------------+ 0xa0000 |
| 55 | * |
| 56 | */ |
| 57 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 58 | #define LAPIC_ID 0xfee00020 |
| 59 | |
| 60 | /* SMM_HANDLER_OFFSET is the 16bit offset within the ASEG |
| 61 | * at which smm_handler_start lives. At the moment the handler |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 62 | * lives right at 0xa0000, so the offset is 0. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
| 65 | #define SMM_HANDLER_OFFSET 0x0000 |
| 66 | |
| 67 | /* initially SMM is some sort of real mode. Let gcc know |
| 68 | * how to treat the SMM handler stub |
| 69 | */ |
| 70 | |
| 71 | .section ".handler", "a", @progbits |
| 72 | |
| 73 | .code16 |
| 74 | |
| 75 | /** |
| 76 | * SMM code to enable protected mode and jump to the |
| 77 | * C-written function void smi_handler(u32 smm_revision) |
| 78 | * |
| 79 | * All the bad magic is not all that bad after all. |
| 80 | */ |
Patrick Georgi | ce2564a | 2015-09-05 20:21:24 +0200 | [diff] [blame^] | 81 | #define SMM_START 0xa0000 |
| 82 | #define SMM_END 0xb0000 |
| 83 | #if SMM_END <= SMM_START |
| 84 | #error invalid SMM configuration |
| 85 | #endif |
Aaron Durbin | e73dae4 | 2015-03-29 22:16:55 -0500 | [diff] [blame] | 86 | .global smm_handler_start |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 87 | smm_handler_start: |
Patrick Georgi | ce2564a | 2015-09-05 20:21:24 +0200 | [diff] [blame^] | 88 | #if IS_ENABLED(CONFIG_SMM_LAPIC_REMAP_MITIGATION) |
| 89 | /* Check if the LAPIC register block overlaps with SMM. |
| 90 | * This block needs to work without data accesses because they |
| 91 | * may be routed into the LAPIC register block. |
| 92 | * Code accesses, on the other hand, are never routed to LAPIC, |
| 93 | * which is what makes this work in the first place. |
| 94 | */ |
| 95 | mov $LAPIC_BASE_MSR, %ecx |
| 96 | rdmsr |
| 97 | and $(~0xfff), %eax |
| 98 | sub $(SMM_START), %eax |
| 99 | cmp $(SMM_END - SMM_START), %eax |
| 100 | ja untampered_lapic |
| 101 | 1: |
| 102 | /* emit "Crash" on serial */ |
| 103 | mov $(CONFIG_TTYS0_BASE), %dx |
| 104 | mov $'C', %al |
| 105 | out %al, (%dx) |
| 106 | mov $'r', %al |
| 107 | out %al, (%dx) |
| 108 | mov $'a', %al |
| 109 | out %al, (%dx) |
| 110 | mov $'s', %al |
| 111 | out %al, (%dx) |
| 112 | mov $'h', %al |
| 113 | out %al, (%dx) |
| 114 | /* now crash for real */ |
| 115 | ud2 |
| 116 | untampered_lapic: |
| 117 | #endif |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 118 | movw $(smm_gdtptr16 - smm_handler_start + SMM_HANDLER_OFFSET), %bx |
| 119 | data32 lgdt %cs:(%bx) |
| 120 | |
| 121 | movl %cr0, %eax |
| 122 | andl $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */ |
| 123 | orl $0x60000001, %eax /* CD, NW, PE = 1 */ |
| 124 | movl %eax, %cr0 |
| 125 | |
| 126 | /* Enable protected mode */ |
| 127 | data32 ljmp $0x08, $1f |
| 128 | |
| 129 | .code32 |
| 130 | 1: |
Stefan Reinauer | 3128685 | 2011-10-15 11:23:04 -0700 | [diff] [blame] | 131 | /* flush the cache after disabling it */ |
| 132 | wbinvd |
| 133 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 134 | /* Use flat data segment */ |
| 135 | movw $0x10, %ax |
| 136 | movw %ax, %ds |
| 137 | movw %ax, %es |
| 138 | movw %ax, %ss |
| 139 | movw %ax, %fs |
| 140 | movw %ax, %gs |
| 141 | |
| 142 | /* Get this CPU's LAPIC ID */ |
| 143 | movl $LAPIC_ID, %esi |
| 144 | movl (%esi), %ecx |
| 145 | shr $24, %ecx |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 146 | |
Alexandru Gagniuc | 53072d8 | 2014-04-12 21:57:18 -0500 | [diff] [blame] | 147 | /* This is an ugly hack, and we should find a way to read the CPU index |
| 148 | * without relying on the LAPIC ID. |
| 149 | */ |
Edward O'Callaghan | eaab630 | 2014-11-21 03:31:02 +1100 | [diff] [blame] | 150 | #if IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_TN) || IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_RL) |
Alexandru Gagniuc | 53072d8 | 2014-04-12 21:57:18 -0500 | [diff] [blame] | 151 | /* LAPIC IDs start from 0x10; map that to the proper core index */ |
| 152 | subl $0x10, %ecx |
| 153 | #endif |
| 154 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 155 | /* calculate stack offset by multiplying the APIC ID |
| 156 | * by 1024 (0x400), and save that offset in ebp. |
| 157 | */ |
| 158 | shl $10, %ecx |
| 159 | movl %ecx, %ebp |
| 160 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 161 | /* We put the stack for each core right above |
| 162 | * its SMM entry point. Core 0 starts at 0xa8000, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 163 | * we spare 0x10 bytes for the jump to be sure. |
| 164 | */ |
| 165 | movl $0xa8010, %eax |
| 166 | subl %ecx, %eax /* subtract offset, see above */ |
| 167 | movl %eax, %ebx /* Save bottom of stack in ebx */ |
| 168 | |
| 169 | #define SMM_STACK_SIZE (0x400 - 0x10) |
| 170 | /* clear stack */ |
| 171 | cld |
| 172 | movl %eax, %edi |
| 173 | movl $(SMM_STACK_SIZE >> 2), %ecx |
| 174 | xorl %eax, %eax |
| 175 | rep stosl |
| 176 | |
| 177 | /* set new stack */ |
| 178 | addl $SMM_STACK_SIZE, %ebx |
| 179 | movl %ebx, %esp |
| 180 | |
| 181 | /* Get SMM revision */ |
| 182 | movl $0xa8000 + 0x7efc, %ebx /* core 0 address */ |
| 183 | subl %ebp, %ebx /* subtract core X offset */ |
| 184 | movl (%ebx), %eax |
| 185 | pushl %eax |
| 186 | |
| 187 | /* Call 32bit C handler */ |
| 188 | call smi_handler |
| 189 | |
| 190 | /* To return, just do rsm. It will "clean up" protected mode */ |
| 191 | rsm |
| 192 | |
| 193 | .code16 |
| 194 | |
| 195 | .align 4, 0xff |
| 196 | |
| 197 | smm_gdtptr16: |
| 198 | .word smm_gdt_end - smm_gdt - 1 |
| 199 | .long smm_gdt - smm_handler_start + 0xa0000 + SMM_HANDLER_OFFSET |
| 200 | |
| 201 | .code32 |
| 202 | |
| 203 | smm_gdt: |
| 204 | /* The first GDT entry can not be used. Keep it zero */ |
| 205 | .long 0x00000000, 0x00000000 |
| 206 | |
| 207 | /* gdt selector 0x08, flat code segment */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 208 | .word 0xffff, 0x0000 |
| 209 | .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, 4GB limit */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 210 | |
| 211 | /* gdt selector 0x10, flat data segment */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 212 | .word 0xffff, 0x0000 |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 213 | .byte 0x00, 0x93, 0xcf, 0x00 |
| 214 | |
| 215 | smm_gdt_end: |
| 216 | |
| 217 | |
| 218 | .section ".jumptable", "a", @progbits |
| 219 | |
| 220 | /* This is the SMM jump table. All cores use the same SMM handler |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 221 | * for simplicity. But SMM Entry needs to be different due to the |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 222 | * save state area. The jump table makes sure all CPUs jump into the |
| 223 | * real handler on SMM entry. |
| 224 | */ |
| 225 | |
| 226 | /* This code currently supports up to 4 CPU cores. If more than 4 CPU cores |
| 227 | * shall be used, below table has to be updated, as well as smm.ld |
| 228 | */ |
| 229 | |
| 230 | /* GNU AS/LD will always generate code that assumes CS is 0xa000. In reality |
| 231 | * CS will be set to SMM_BASE[19:4] though. Knowing that the smm handler is the |
| 232 | * first thing in the ASEG, we do a far jump here, to set CS to 0xa000. |
| 233 | */ |
| 234 | |
| 235 | .code16 |
| 236 | jumptable: |
| 237 | /* core 3 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 238 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 239 | .align 1024, 0x00 |
| 240 | /* core 2 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 241 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 242 | .align 1024, 0x00 |
| 243 | /* core 1 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 244 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 245 | .align 1024, 0x00 |
| 246 | /* core 0 */ |
| 247 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
| 248 | .align 1024, 0x00 |