blob: 9cc6582ffea2a921ca32c18748e2366e62a6f812 [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.
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 Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000019 */
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 Georgice2564a2015-09-05 20:21:24 +020028#include <kconfig.h>
29#include <config.h>
30#define LAPIC_BASE_MSR 0x1b
31
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000032/*
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 Reinauer14e22772010-04-27 06:56:47 +000044 * | SMM Entry Node 0 (+ stack) |
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000045 * +--------------------------------+ 0xa8000
Stefan Reinauer14e22772010-04-27 06:56:47 +000046 * | SMM Entry Node 1 (+ stack) |
47 * | SMM Entry Node 2 (+ stack) |
48 * | SMM Entry Node 3 (+ stack) |
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000049 * | ... |
50 * +--------------------------------+ 0xa7400
51 * | |
52 * | SMM Handler |
53 * | |
54 * +--------------------------------+ 0xa0000
55 *
56 */
57
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000058#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 Reinauer14e22772010-04-27 06:56:47 +000062 * lives right at 0xa0000, so the offset is 0.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000063 */
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 Georgice2564a2015-09-05 20:21:24 +020081#define SMM_START 0xa0000
82#define SMM_END 0xb0000
83#if SMM_END <= SMM_START
84#error invalid SMM configuration
85#endif
Aaron Durbine73dae42015-03-29 22:16:55 -050086.global smm_handler_start
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000087smm_handler_start:
Patrick Georgice2564a2015-09-05 20:21:24 +020088#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
1011:
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
116untampered_lapic:
117#endif
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000118 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
1301:
Stefan Reinauer31286852011-10-15 11:23:04 -0700131 /* flush the cache after disabling it */
132 wbinvd
133
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000134 /* 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 Reinauer14e22772010-04-27 06:56:47 +0000146
Alexandru Gagniuc53072d82014-04-12 21:57:18 -0500147 /* 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'Callaghaneaab6302014-11-21 03:31:02 +1100150#if IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_TN) || IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_RL)
Alexandru Gagniuc53072d82014-04-12 21:57:18 -0500151 /* LAPIC IDs start from 0x10; map that to the proper core index */
152 subl $0x10, %ecx
153#endif
154
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000155 /* 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 Reinauer14e22772010-04-27 06:56:47 +0000161 /* We put the stack for each core right above
162 * its SMM entry point. Core 0 starts at 0xa8000,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000163 * 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
197smm_gdtptr16:
198 .word smm_gdt_end - smm_gdt - 1
199 .long smm_gdt - smm_handler_start + 0xa0000 + SMM_HANDLER_OFFSET
200
201.code32
202
203smm_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 Reinauer14e22772010-04-27 06:56:47 +0000208 .word 0xffff, 0x0000
209 .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, 4GB limit */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000210
211 /* gdt selector 0x10, flat data segment */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000212 .word 0xffff, 0x0000
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000213 .byte 0x00, 0x93, 0xcf, 0x00
214
215smm_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 Reinauer14e22772010-04-27 06:56:47 +0000221 * for simplicity. But SMM Entry needs to be different due to the
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000222 * 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
236jumptable:
237 /* core 3 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238 ljmp $0xa000, $SMM_HANDLER_OFFSET
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000239.align 1024, 0x00
240 /* core 2 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000241 ljmp $0xa000, $SMM_HANDLER_OFFSET
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000242.align 1024, 0x00
243 /* core 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000244 ljmp $0xa000, $SMM_HANDLER_OFFSET
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000245.align 1024, 0x00
246 /* core 0 */
247 ljmp $0xa000, $SMM_HANDLER_OFFSET
248.align 1024, 0x00