blob: 3ef03b3346c3222fbb2dd61834f26cda2b7172a7 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth9df9e9392016-01-12 15:55:28 -07002
Stefan Reinauer5f5436f2010-04-25 20:42:02 +00003#include <cpu/x86/post_code.h>
Patrick Rudolph776da082019-10-25 08:09:33 +02004#include <arch/ram_segs.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00005
Aaron Durbin633f1122013-02-06 15:28:40 -06006/* Place the stack in the bss section. It's not necessary to define it in the
7 * the linker script. */
8 .section .bss, "aw", @nobits
9.global _stack
10.global _estack
Julius Werner82d16b12020-12-30 15:51:10 -080011.global _stack_size
Aaron Durbin633f1122013-02-06 15:28:40 -060012
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030013/* Stack alignment is not enforced with rmodule loader, reserve one
14 * extra CPU such that alignment can be enforced on entry. */
Aaron Durbin633f1122013-02-06 15:28:40 -060015.align CONFIG_STACK_SIZE
16_stack:
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030017.space (CONFIG_MAX_CPUS+1)*CONFIG_STACK_SIZE
Aaron Durbin633f1122013-02-06 15:28:40 -060018_estack:
Julius Werner82d16b12020-12-30 15:51:10 -080019.set _stack_size, _estack - _stack
Julius Wernercd49cce2019-03-05 16:53:33 -080020#if CONFIG(COOP_MULTITASKING)
Aaron Durbin38c326d2013-05-06 12:22:23 -050021.global thread_stacks
22thread_stacks:
23.space CONFIG_STACK_SIZE*CONFIG_NUM_THREADS
24#endif
Aaron Durbin633f1122013-02-06 15:28:40 -060025
Julius Wernerec5e5e02014-08-20 15:29:56 -070026 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +020027#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -070028 .code64
29#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000030 .code32
Stefan Reinauer96938852015-06-18 01:23:48 -070031#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000032 .globl _start
33_start:
34 cli
Patrick Rudolphadcf7822020-08-27 20:50:18 +020035#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020036 movabs $gdtaddr, %rax
37 lgdt (%rax)
38#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000039 lgdt %cs:gdtaddr
Patrick Rudolph776da082019-10-25 08:09:33 +020040 ljmp $RAM_CODE_SEG, $1f
Stefan Reinauer96938852015-06-18 01:23:48 -070041#endif
Patrick Rudolph776da082019-10-25 08:09:33 +0200421: movl $RAM_DATA_SEG, %eax
Eric Biederman8ca8d762003-04-22 19:02:15 +000043 movl %eax, %ds
44 movl %eax, %es
45 movl %eax, %ss
46 movl %eax, %fs
47 movl %eax, %gs
Patrick Rudolphadcf7822020-08-27 20:50:18 +020048#if ENV_X86_64
Patrick Rudolph776da082019-10-25 08:09:33 +020049 mov $RAM_CODE_SEG64, %ecx
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +010050 call SetCodeSelector
Stefan Reinauer96938852015-06-18 01:23:48 -070051#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000052
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +000053 post_code(POST_ENTRY_C_START) /* post 13 */
Eric Biederman8ca8d762003-04-22 19:02:15 +000054
arch import user (historical)6ca76362005-07-06 17:17:25 +000055 cld
Eric Biederman8ca8d762003-04-22 19:02:15 +000056
Patrick Rudolphadcf7822020-08-27 20:50:18 +020057#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020058 mov %rdi, %rax
59 movabs %rax, _cbmem_top_ptr
60 movabs $_stack, %rdi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020061#else
62 /* The return argument is at 0(%esp), the calling argument at 4(%esp) */
63 movl 4(%esp), %eax
64 movl %eax, _cbmem_top_ptr
Patrick Rudolphd0239092021-06-11 21:24:10 +020065 leal _stack, %edi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020066#endif
67
Aaron Durbin633f1122013-02-06 15:28:40 -060068 /** poison the stack. Code should not count on the
69 * stack being full of zeros. This stack poisoning
70 * recently uncovered a bug in the broadcast SIPI
71 * code.
72 */
Aaron Durbin633f1122013-02-06 15:28:40 -060073 movl $_estack, %ecx
74 subl %edi, %ecx
75 shrl $2, %ecx /* it is 32 bit aligned, right? */
76 movl $0xDEADBEEF, %eax
77 rep
78 stosl
79
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030080 /* Set new stack with enforced alignment. */
Eric Biederman8ca8d762003-04-22 19:02:15 +000081 movl $_estack, %esp
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030082 andl $(~(CONFIG_STACK_SIZE-1)), %esp
Eric Biederman8ca8d762003-04-22 19:02:15 +000083
Julius Wernercd49cce2019-03-05 16:53:33 -080084#if CONFIG(COOP_MULTITASKING)
Aaron Durbin38c326d2013-05-06 12:22:23 -050085 /* Push the thread pointer. */
Stefan Reinauer96938852015-06-18 01:23:48 -070086 push $0
Aaron Durbin38c326d2013-05-06 12:22:23 -050087#endif
Elyes HAOUAS777ea892016-07-29 07:40:41 +020088 /* Push the CPU index and struct CPU */
Stefan Reinauer96938852015-06-18 01:23:48 -070089 push $0
90 push $0
Eric Biederman8ca8d762003-04-22 19:02:15 +000091
Eric Biederman8ca8d762003-04-22 19:02:15 +000092 /*
93 * Now we are finished. Memory is up, data is copied and
94 * bss is cleared. Now we call the main routine and
95 * let it do the rest.
Stefan Reinauer607cdf62010-04-26 12:08:51 +000096 */
Subrata Banik38e4a2d2021-05-05 19:34:07 +053097 post_code(POST_PRE_HARDWAREMAIN) /* post 6e */
Eric Biederman8ca8d762003-04-22 19:02:15 +000098
Kyösti Mälkki4796c322017-03-15 08:07:22 +020099 andl $0xFFFFFFF0, %esp
100
Harshit Sharma9c88fb82020-06-17 20:19:00 -0700101#if CONFIG(ASAN_IN_RAMSTAGE)
102 call asan_init
103#endif
104
Julius Wernercd49cce2019-03-05 16:53:33 -0800105#if CONFIG(GDB_WAIT)
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300106 call gdb_hw_init
Denis 'GNUtoo' Cariklie4cece02012-06-22 15:56:37 +0200107 call gdb_stub_breakpoint
108#endif
Stefan Reinauer6adef082013-05-09 16:30:06 -0700109 call main
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000110 /* NOTREACHED */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000111.Lhlt:
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000112 post_code(POST_DEAD_CODE) /* post ee */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000113 hlt
114 jmp .Lhlt
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000115
Julius Wernercd49cce2019-03-05 16:53:33 -0800116#if CONFIG(GDB_WAIT)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000117
118 .globl gdb_stub_breakpoint
119gdb_stub_breakpoint:
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200120#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700121 pop %rax /* Return address */
122 pushfl
123 push %cs
124 push %rax /* Return address */
125 push $0 /* No error code */
126 push $32 /* vector 32 is user defined */
127#else
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000128 popl %eax /* Return address */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000129 pushfl
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000130 pushl %cs
131 pushl %eax /* Return address */
132 pushl $0 /* No error code */
133 pushl $32 /* vector 32 is user defined */
Stefan Reinauer96938852015-06-18 01:23:48 -0700134#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000135 jmp int_hand
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000136#endif
137
Aaron Durbin4b032e42018-04-20 01:39:30 -0600138 .globl gdt, gdt_end
Eric Biederman8ca8d762003-04-22 19:02:15 +0000139
Eric Biederman8ca8d762003-04-22 19:02:15 +0000140gdtaddr:
Aaron Durbina146d582013-02-08 16:56:51 -0600141 .word gdt_end - gdt - 1
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200142#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700143 .quad gdt
144#else
Li-Ta Lof84926e2004-11-04 18:36:06 +0000145 .long gdt /* we know the offset */
Stefan Reinauer96938852015-06-18 01:23:48 -0700146#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000148 .data
Li-Ta Lof84926e2004-11-04 18:36:06 +0000149
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000150 /* This is the gdt for GCC part of coreboot.
Arthur Heymans1cb9cd52019-11-28 16:05:08 +0100151 * It is different from the gdt in ASM part of coreboot
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200152 * which is defined in gdt_init.S
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000153 *
154 * When the machine is initially started, we use a very simple
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200155 * gdt from ROM (that in gdt_init.S) which only contains those
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000156 * entries we need for protected mode.
157 *
158 * When we're executing code from RAM, we want to do more complex
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200159 * stuff, like initializing PCI option ROMs in real mode, or doing
160 * a resume from a suspend to RAM.
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000161 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000162gdt:
Li-Ta Lof84926e2004-11-04 18:36:06 +0000163 /* selgdt 0, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000164 .word 0x0000, 0x0000 /* dummy */
165 .byte 0x00, 0x00, 0x00, 0x00
166
Li-Ta Lof84926e2004-11-04 18:36:06 +0000167 /* selgdt 8, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 .word 0x0000, 0x0000 /* dummy */
169 .byte 0x00, 0x00, 0x00, 0x00
170
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000171 /* selgdt 0x10, flat code segment */
172 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700173 .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
174 * limit
175 */
Li-Ta Lof84926e2004-11-04 18:36:06 +0000176
177 /* selgdt 0x18, flat data segment */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000178 .word 0xffff, 0x0000
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200179#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700180 .byte 0x00, 0x92, 0xcf, 0x00
181#else
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000182 .byte 0x00, 0x93, 0xcf, 0x00
Stefan Reinauer96938852015-06-18 01:23:48 -0700183#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184
Li-Ta Lof84926e2004-11-04 18:36:06 +0000185 /* selgdt 0x20, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186 .word 0x0000, 0x0000 /* dummy */
187 .byte 0x00, 0x00, 0x00, 0x00
188
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000189 /* The next two entries are used for executing VGA option ROMs */
190
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000191 /* selgdt 0x28 16 bit 64k code at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100192 .word 0xffff, 0x0000
193 .byte 0, 0x9a, 0, 0
Stefan Reinauerf8a5c6e2009-05-29 13:08:27 +0000194
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000195 /* selgdt 0x30 16 bit 64k data at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100196 .word 0xffff, 0x0000
197 .byte 0, 0x92, 0, 0
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000198
199 /* The next two entries are used for ACPI S3 RESUME */
200
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000201 /* selgdt 0x38, flat data segment 16 bit */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000202 .word 0x0000, 0x0000 /* dummy */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700203 .byte 0x00, 0x93, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
204 * limit
205 */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000206
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000207 /* selgdt 0x40, flat code segment 16 bit */
208 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700209 .byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
210 * limit
211 */
Stefan Reinauer96938852015-06-18 01:23:48 -0700212
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200213#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700214 /* selgdt 0x48, flat x64 code segment */
215 .word 0xffff, 0x0000
216 .byte 0x00, 0x9b, 0xaf, 0x00
217#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000218gdt_end:
219
Patrick Georgi546f29d2016-01-22 12:43:43 +0100220 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200221#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700222SetCodeSelector:
Martin Rothe3690102016-01-06 15:21:02 -0700223 # save rsp because iret will align it to a 16 byte boundary
Patrick Georgi0302b062016-01-22 12:26:52 +0100224 mov %rsp, %rdx
Stefan Reinauer96938852015-06-18 01:23:48 -0700225
Martin Rothe3690102016-01-06 15:21:02 -0700226 # use iret to jump to a 64-bit offset in a new code segment
227 # iret will pop cs:rip, flags, then ss:rsp
Patrick Georgi0302b062016-01-22 12:26:52 +0100228 mov %ss, %ax # need to push ss..
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700229 push %rax # push ss instuction not valid in x64 mode,
230 # so use ax
Patrick Georgi0302b062016-01-22 12:26:52 +0100231 push %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700232 pushfq
Patrick Georgi0302b062016-01-22 12:26:52 +0100233 push %rcx # cx is code segment selector from caller
Patrick Rudolphd0239092021-06-11 21:24:10 +0200234 movabs $setCodeSelectorLongJump, %rax
Patrick Georgi0302b062016-01-22 12:26:52 +0100235 push %rax
Stefan Reinauer96938852015-06-18 01:23:48 -0700236
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700237 # the iret will continue at next instruction, with the new cs value
238 # loaded
Martin Rothe3690102016-01-06 15:21:02 -0700239 iretq
Stefan Reinauer96938852015-06-18 01:23:48 -0700240
241setCodeSelectorLongJump:
Martin Rothe3690102016-01-06 15:21:02 -0700242 # restore rsp, it might not have been 16-byte aligned on entry
Patrick Georgi0302b062016-01-22 12:26:52 +0100243 mov %rdx, %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700244 ret
Stefan Reinauer96938852015-06-18 01:23:48 -0700245#endif