blob: 02ddf75d12a73426eb67962e3dd402c14a32e15c [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
Elyes HAOUASf79f7752021-12-12 21:43:13 +01006/* Place the stack in the bss section. It's not necessary to define it in
Aaron Durbin633f1122013-02-06 15:28:40 -06007 * 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
Arthur Heymans4d75dbd2022-11-01 23:57:24 +010013.align 16
Aaron Durbin633f1122013-02-06 15:28:40 -060014_stack:
Arthur Heymans4d75dbd2022-11-01 23:57:24 +010015.space CONFIG_STACK_SIZE
Aaron Durbin633f1122013-02-06 15:28:40 -060016_estack:
Julius Werner82d16b12020-12-30 15:51:10 -080017.set _stack_size, _estack - _stack
Aaron Durbin633f1122013-02-06 15:28:40 -060018
Julius Wernerec5e5e02014-08-20 15:29:56 -070019 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +020020#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -070021 .code64
22#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 .code32
Stefan Reinauer96938852015-06-18 01:23:48 -070024#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000025 .globl _start
26_start:
27 cli
Patrick Rudolphadcf7822020-08-27 20:50:18 +020028#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020029 movabs $gdtaddr, %rax
30 lgdt (%rax)
31#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000032 lgdt %cs:gdtaddr
Patrick Rudolph776da082019-10-25 08:09:33 +020033 ljmp $RAM_CODE_SEG, $1f
Stefan Reinauer96938852015-06-18 01:23:48 -070034#endif
Patrick Rudolph776da082019-10-25 08:09:33 +0200351: movl $RAM_DATA_SEG, %eax
Eric Biederman8ca8d762003-04-22 19:02:15 +000036 movl %eax, %ds
37 movl %eax, %es
38 movl %eax, %ss
Raul E Rangelea5c3112021-09-21 10:17:24 -060039 xor %eax, %eax /* zero out the gs and fs segment index */
Eric Biederman8ca8d762003-04-22 19:02:15 +000040 movl %eax, %fs
Raul E Rangelea5c3112021-09-21 10:17:24 -060041 movl %eax, %gs /* Will be used for cpu_info */
Patrick Rudolphadcf7822020-08-27 20:50:18 +020042#if ENV_X86_64
Patrick Rudolph776da082019-10-25 08:09:33 +020043 mov $RAM_CODE_SEG64, %ecx
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +010044 call SetCodeSelector
Stefan Reinauer96938852015-06-18 01:23:48 -070045#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000046
lilacious40cb3fe2023-06-21 23:24:14 +020047 post_code(POSTCODE_ENTRY_C_START) /* post 13 */
Eric Biederman8ca8d762003-04-22 19:02:15 +000048
arch import user (historical)6ca76362005-07-06 17:17:25 +000049 cld
Eric Biederman8ca8d762003-04-22 19:02:15 +000050
Patrick Rudolphadcf7822020-08-27 20:50:18 +020051#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020052 mov %rdi, %rax
53 movabs %rax, _cbmem_top_ptr
54 movabs $_stack, %rdi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020055#else
56 /* The return argument is at 0(%esp), the calling argument at 4(%esp) */
57 movl 4(%esp), %eax
58 movl %eax, _cbmem_top_ptr
Patrick Rudolphd0239092021-06-11 21:24:10 +020059 leal _stack, %edi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020060#endif
61
Aaron Durbin633f1122013-02-06 15:28:40 -060062 /** poison the stack. Code should not count on the
63 * stack being full of zeros. This stack poisoning
64 * recently uncovered a bug in the broadcast SIPI
65 * code.
66 */
Aaron Durbin633f1122013-02-06 15:28:40 -060067 movl $_estack, %ecx
68 subl %edi, %ecx
69 shrl $2, %ecx /* it is 32 bit aligned, right? */
70 movl $0xDEADBEEF, %eax
71 rep
72 stosl
73
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030074 /* Set new stack with enforced alignment. */
Eric Biederman8ca8d762003-04-22 19:02:15 +000075 movl $_estack, %esp
Arthur Heymans4d75dbd2022-11-01 23:57:24 +010076 andl $(0xfffffff0), %esp
Eric Biederman8ca8d762003-04-22 19:02:15 +000077
Eric Biederman8ca8d762003-04-22 19:02:15 +000078 /*
79 * Now we are finished. Memory is up, data is copied and
80 * bss is cleared. Now we call the main routine and
81 * let it do the rest.
Stefan Reinauer607cdf62010-04-26 12:08:51 +000082 */
lilacious40cb3fe2023-06-21 23:24:14 +020083 post_code(POSTCODE_PRE_HARDWAREMAIN) /* post 6e */
Eric Biederman8ca8d762003-04-22 19:02:15 +000084
Kyösti Mälkki4796c322017-03-15 08:07:22 +020085 andl $0xFFFFFFF0, %esp
86
Harshit Sharma9c88fb82020-06-17 20:19:00 -070087#if CONFIG(ASAN_IN_RAMSTAGE)
88 call asan_init
89#endif
90
Julius Wernercd49cce2019-03-05 16:53:33 -080091#if CONFIG(GDB_WAIT)
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +030092 call gdb_hw_init
Denis 'GNUtoo' Cariklie4cece02012-06-22 15:56:37 +020093 call gdb_stub_breakpoint
94#endif
Stefan Reinauer6adef082013-05-09 16:30:06 -070095 call main
Stefan Reinauer607cdf62010-04-26 12:08:51 +000096 /* NOTREACHED */
Eric Biederman8ca8d762003-04-22 19:02:15 +000097.Lhlt:
lilacious40cb3fe2023-06-21 23:24:14 +020098 post_code(POSTCODE_DEAD_CODE) /* post ee */
Eric Biederman8ca8d762003-04-22 19:02:15 +000099 hlt
100 jmp .Lhlt
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000101
Julius Wernercd49cce2019-03-05 16:53:33 -0800102#if CONFIG(GDB_WAIT)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000103
104 .globl gdb_stub_breakpoint
105gdb_stub_breakpoint:
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200106#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700107 pop %rax /* Return address */
108 pushfl
109 push %cs
110 push %rax /* Return address */
111 push $0 /* No error code */
112 push $32 /* vector 32 is user defined */
113#else
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000114 popl %eax /* Return address */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000115 pushfl
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000116 pushl %cs
117 pushl %eax /* Return address */
118 pushl $0 /* No error code */
119 pushl $32 /* vector 32 is user defined */
Stefan Reinauer96938852015-06-18 01:23:48 -0700120#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000121 jmp int_hand
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000122#endif
123
Aaron Durbin4b032e42018-04-20 01:39:30 -0600124 .globl gdt, gdt_end
Raul E Rangelb2346a52021-09-22 14:56:51 -0600125 .global per_cpu_segment_descriptors, per_cpu_segment_selector
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126
Eric Biederman8ca8d762003-04-22 19:02:15 +0000127gdtaddr:
Aaron Durbina146d582013-02-08 16:56:51 -0600128 .word gdt_end - gdt - 1
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200129#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700130 .quad gdt
131#else
Li-Ta Lof84926e2004-11-04 18:36:06 +0000132 .long gdt /* we know the offset */
Stefan Reinauer96938852015-06-18 01:23:48 -0700133#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000134
Raul E Rangelb2346a52021-09-22 14:56:51 -0600135 .data
Li-Ta Lof84926e2004-11-04 18:36:06 +0000136
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000137 /* This is the gdt for GCC part of coreboot.
Arthur Heymans1cb9cd52019-11-28 16:05:08 +0100138 * It is different from the gdt in ASM part of coreboot
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200139 * which is defined in gdt_init.S
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000140 *
141 * When the machine is initially started, we use a very simple
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200142 * gdt from ROM (that in gdt_init.S) which only contains those
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000143 * entries we need for protected mode.
144 *
145 * When we're executing code from RAM, we want to do more complex
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200146 * stuff, like initializing PCI option ROMs in real mode, or doing
147 * a resume from a suspend to RAM.
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000148 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149gdt:
Li-Ta Lof84926e2004-11-04 18:36:06 +0000150 /* selgdt 0, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151 .word 0x0000, 0x0000 /* dummy */
152 .byte 0x00, 0x00, 0x00, 0x00
153
Li-Ta Lof84926e2004-11-04 18:36:06 +0000154 /* selgdt 8, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000155 .word 0x0000, 0x0000 /* dummy */
156 .byte 0x00, 0x00, 0x00, 0x00
157
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000158 /* selgdt 0x10, flat code segment */
159 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700160 .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
161 * limit
162 */
Li-Ta Lof84926e2004-11-04 18:36:06 +0000163
164 /* selgdt 0x18, flat data segment */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000165 .word 0xffff, 0x0000
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200166#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700167 .byte 0x00, 0x92, 0xcf, 0x00
168#else
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000169 .byte 0x00, 0x93, 0xcf, 0x00
Stefan Reinauer96938852015-06-18 01:23:48 -0700170#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171
Li-Ta Lof84926e2004-11-04 18:36:06 +0000172 /* selgdt 0x20, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000173 .word 0x0000, 0x0000 /* dummy */
174 .byte 0x00, 0x00, 0x00, 0x00
175
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000176 /* The next two entries are used for executing VGA option ROMs */
177
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000178 /* selgdt 0x28 16 bit 64k code at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100179 .word 0xffff, 0x0000
180 .byte 0, 0x9a, 0, 0
Stefan Reinauerf8a5c6e2009-05-29 13:08:27 +0000181
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000182 /* selgdt 0x30 16 bit 64k data at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100183 .word 0xffff, 0x0000
184 .byte 0, 0x92, 0, 0
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000185
186 /* The next two entries are used for ACPI S3 RESUME */
187
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000188 /* selgdt 0x38, flat data segment 16 bit */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000189 .word 0x0000, 0x0000 /* dummy */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700190 .byte 0x00, 0x93, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
191 * limit
192 */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000193
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000194 /* selgdt 0x40, flat code segment 16 bit */
195 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700196 .byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
197 * limit
198 */
Stefan Reinauer96938852015-06-18 01:23:48 -0700199
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200200#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700201 /* selgdt 0x48, flat x64 code segment */
202 .word 0xffff, 0x0000
203 .byte 0x00, 0x9b, 0xaf, 0x00
204#endif
Raul E Rangelb2346a52021-09-22 14:56:51 -0600205per_cpu_segment_descriptors:
206 .rept CONFIG_MAX_CPUS
207 /* flat data segment */
208 .word 0xffff, 0x0000
209#if ENV_X86_64
210 .byte 0x00, 0x92, 0xcf, 0x00
211#else
212 .byte 0x00, 0x93, 0xcf, 0x00
213#endif
214 .endr
Eric Biederman8ca8d762003-04-22 19:02:15 +0000215gdt_end:
216
Raul E Rangelb2346a52021-09-22 14:56:51 -0600217/* Segment selector pointing to the first per_cpu_segment_descriptor. */
218per_cpu_segment_selector:
219 .long per_cpu_segment_descriptors - gdt
Raul E Rangelb2346a52021-09-22 14:56:51 -0600220
Patrick Georgi546f29d2016-01-22 12:43:43 +0100221 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200222#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700223SetCodeSelector:
Martin Rothe3690102016-01-06 15:21:02 -0700224 # save rsp because iret will align it to a 16 byte boundary
Patrick Georgi0302b062016-01-22 12:26:52 +0100225 mov %rsp, %rdx
Stefan Reinauer96938852015-06-18 01:23:48 -0700226
Martin Rothe3690102016-01-06 15:21:02 -0700227 # use iret to jump to a 64-bit offset in a new code segment
228 # iret will pop cs:rip, flags, then ss:rsp
Patrick Georgi0302b062016-01-22 12:26:52 +0100229 mov %ss, %ax # need to push ss..
Martin Roth0949e732021-10-01 14:28:22 -0600230 push %rax # push ss instruction not valid in x64 mode,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700231 # so use ax
Patrick Georgi0302b062016-01-22 12:26:52 +0100232 push %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700233 pushfq
Patrick Georgi0302b062016-01-22 12:26:52 +0100234 push %rcx # cx is code segment selector from caller
Patrick Rudolphd0239092021-06-11 21:24:10 +0200235 movabs $setCodeSelectorLongJump, %rax
Patrick Georgi0302b062016-01-22 12:26:52 +0100236 push %rax
Stefan Reinauer96938852015-06-18 01:23:48 -0700237
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700238 # the iret will continue at next instruction, with the new cs value
239 # loaded
Martin Rothe3690102016-01-06 15:21:02 -0700240 iretq
Stefan Reinauer96938852015-06-18 01:23:48 -0700241
242setCodeSelectorLongJump:
Martin Rothe3690102016-01-06 15:21:02 -0700243 # restore rsp, it might not have been 16-byte aligned on entry
Patrick Georgi0302b062016-01-22 12:26:52 +0100244 mov %rdx, %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700245 ret
Stefan Reinauer96938852015-06-18 01:23:48 -0700246#endif