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