blob: 5b7052ef5820a3530047f723d7c5ded5cb7ad8f3 [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
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
Aaron Durbin633f1122013-02-06 15:28:40 -060020
Julius Wernerec5e5e02014-08-20 15:29:56 -070021 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +020022#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -070023 .code64
24#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000025 .code32
Stefan Reinauer96938852015-06-18 01:23:48 -070026#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000027 .globl _start
28_start:
29 cli
Patrick Rudolphadcf7822020-08-27 20:50:18 +020030#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020031 movabs $gdtaddr, %rax
32 lgdt (%rax)
33#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000034 lgdt %cs:gdtaddr
Patrick Rudolph776da082019-10-25 08:09:33 +020035 ljmp $RAM_CODE_SEG, $1f
Stefan Reinauer96938852015-06-18 01:23:48 -070036#endif
Patrick Rudolph776da082019-10-25 08:09:33 +0200371: movl $RAM_DATA_SEG, %eax
Eric Biederman8ca8d762003-04-22 19:02:15 +000038 movl %eax, %ds
39 movl %eax, %es
40 movl %eax, %ss
Raul E Rangelea5c3112021-09-21 10:17:24 -060041 xor %eax, %eax /* zero out the gs and fs segment index */
Eric Biederman8ca8d762003-04-22 19:02:15 +000042 movl %eax, %fs
Raul E Rangelea5c3112021-09-21 10:17:24 -060043 movl %eax, %gs /* Will be used for cpu_info */
Patrick Rudolphadcf7822020-08-27 20:50:18 +020044#if ENV_X86_64
Patrick Rudolph776da082019-10-25 08:09:33 +020045 mov $RAM_CODE_SEG64, %ecx
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +010046 call SetCodeSelector
Stefan Reinauer96938852015-06-18 01:23:48 -070047#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000048
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +000049 post_code(POST_ENTRY_C_START) /* post 13 */
Eric Biederman8ca8d762003-04-22 19:02:15 +000050
arch import user (historical)6ca76362005-07-06 17:17:25 +000051 cld
Eric Biederman8ca8d762003-04-22 19:02:15 +000052
Patrick Rudolphadcf7822020-08-27 20:50:18 +020053#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020054 mov %rdi, %rax
55 movabs %rax, _cbmem_top_ptr
56 movabs $_stack, %rdi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020057#else
58 /* The return argument is at 0(%esp), the calling argument at 4(%esp) */
59 movl 4(%esp), %eax
60 movl %eax, _cbmem_top_ptr
Patrick Rudolphd0239092021-06-11 21:24:10 +020061 leal _stack, %edi
Arthur Heymans7c9a0e82019-10-23 17:02:50 +020062#endif
63
Aaron Durbin633f1122013-02-06 15:28:40 -060064 /** poison the stack. Code should not count on the
65 * stack being full of zeros. This stack poisoning
66 * recently uncovered a bug in the broadcast SIPI
67 * code.
68 */
Aaron Durbin633f1122013-02-06 15:28:40 -060069 movl $_estack, %ecx
70 subl %edi, %ecx
71 shrl $2, %ecx /* it is 32 bit aligned, right? */
72 movl $0xDEADBEEF, %eax
73 rep
74 stosl
75
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030076 /* Set new stack with enforced alignment. */
Eric Biederman8ca8d762003-04-22 19:02:15 +000077 movl $_estack, %esp
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030078 andl $(~(CONFIG_STACK_SIZE-1)), %esp
Eric Biederman8ca8d762003-04-22 19:02:15 +000079
Eric Biederman8ca8d762003-04-22 19:02:15 +000080 /*
81 * Now we are finished. Memory is up, data is copied and
82 * bss is cleared. Now we call the main routine and
83 * let it do the rest.
Stefan Reinauer607cdf62010-04-26 12:08:51 +000084 */
Subrata Banik38e4a2d2021-05-05 19:34:07 +053085 post_code(POST_PRE_HARDWAREMAIN) /* post 6e */
Eric Biederman8ca8d762003-04-22 19:02:15 +000086
Kyösti Mälkki4796c322017-03-15 08:07:22 +020087 andl $0xFFFFFFF0, %esp
88
Harshit Sharma9c88fb82020-06-17 20:19:00 -070089#if CONFIG(ASAN_IN_RAMSTAGE)
90 call asan_init
91#endif
92
Julius Wernercd49cce2019-03-05 16:53:33 -080093#if CONFIG(GDB_WAIT)
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +030094 call gdb_hw_init
Denis 'GNUtoo' Cariklie4cece02012-06-22 15:56:37 +020095 call gdb_stub_breakpoint
96#endif
Stefan Reinauer6adef082013-05-09 16:30:06 -070097 call main
Stefan Reinauer607cdf62010-04-26 12:08:51 +000098 /* NOTREACHED */
Eric Biederman8ca8d762003-04-22 19:02:15 +000099.Lhlt:
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000100 post_code(POST_DEAD_CODE) /* post ee */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000101 hlt
102 jmp .Lhlt
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000103
Julius Wernercd49cce2019-03-05 16:53:33 -0800104#if CONFIG(GDB_WAIT)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000105
106 .globl gdb_stub_breakpoint
107gdb_stub_breakpoint:
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200108#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700109 pop %rax /* Return address */
110 pushfl
111 push %cs
112 push %rax /* Return address */
113 push $0 /* No error code */
114 push $32 /* vector 32 is user defined */
115#else
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000116 popl %eax /* Return address */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000117 pushfl
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000118 pushl %cs
119 pushl %eax /* Return address */
120 pushl $0 /* No error code */
121 pushl $32 /* vector 32 is user defined */
Stefan Reinauer96938852015-06-18 01:23:48 -0700122#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000123 jmp int_hand
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000124#endif
125
Aaron Durbin4b032e42018-04-20 01:39:30 -0600126 .globl gdt, gdt_end
Raul E Rangelb2346a52021-09-22 14:56:51 -0600127 .global per_cpu_segment_descriptors, per_cpu_segment_selector
Eric Biederman8ca8d762003-04-22 19:02:15 +0000128
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129gdtaddr:
Aaron Durbina146d582013-02-08 16:56:51 -0600130 .word gdt_end - gdt - 1
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200131#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700132 .quad gdt
133#else
Li-Ta Lof84926e2004-11-04 18:36:06 +0000134 .long gdt /* we know the offset */
Stefan Reinauer96938852015-06-18 01:23:48 -0700135#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000136
Raul E Rangelb2346a52021-09-22 14:56:51 -0600137 .data
Li-Ta Lof84926e2004-11-04 18:36:06 +0000138
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000139 /* This is the gdt for GCC part of coreboot.
Arthur Heymans1cb9cd52019-11-28 16:05:08 +0100140 * It is different from the gdt in ASM part of coreboot
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200141 * which is defined in gdt_init.S
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000142 *
143 * When the machine is initially started, we use a very simple
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200144 * gdt from ROM (that in gdt_init.S) which only contains those
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000145 * entries we need for protected mode.
146 *
147 * When we're executing code from RAM, we want to do more complex
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200148 * stuff, like initializing PCI option ROMs in real mode, or doing
149 * a resume from a suspend to RAM.
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000150 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151gdt:
Li-Ta Lof84926e2004-11-04 18:36:06 +0000152 /* selgdt 0, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 .word 0x0000, 0x0000 /* dummy */
154 .byte 0x00, 0x00, 0x00, 0x00
155
Li-Ta Lof84926e2004-11-04 18:36:06 +0000156 /* selgdt 8, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000157 .word 0x0000, 0x0000 /* dummy */
158 .byte 0x00, 0x00, 0x00, 0x00
159
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000160 /* selgdt 0x10, flat code segment */
161 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700162 .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
163 * limit
164 */
Li-Ta Lof84926e2004-11-04 18:36:06 +0000165
166 /* selgdt 0x18, flat data segment */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000167 .word 0xffff, 0x0000
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200168#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700169 .byte 0x00, 0x92, 0xcf, 0x00
170#else
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000171 .byte 0x00, 0x93, 0xcf, 0x00
Stefan Reinauer96938852015-06-18 01:23:48 -0700172#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000173
Li-Ta Lof84926e2004-11-04 18:36:06 +0000174 /* selgdt 0x20, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175 .word 0x0000, 0x0000 /* dummy */
176 .byte 0x00, 0x00, 0x00, 0x00
177
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000178 /* The next two entries are used for executing VGA option ROMs */
179
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000180 /* selgdt 0x28 16 bit 64k code at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100181 .word 0xffff, 0x0000
182 .byte 0, 0x9a, 0, 0
Stefan Reinauerf8a5c6e2009-05-29 13:08:27 +0000183
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000184 /* selgdt 0x30 16 bit 64k data at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100185 .word 0xffff, 0x0000
186 .byte 0, 0x92, 0, 0
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000187
188 /* The next two entries are used for ACPI S3 RESUME */
189
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000190 /* selgdt 0x38, flat data segment 16 bit */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000191 .word 0x0000, 0x0000 /* dummy */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700192 .byte 0x00, 0x93, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
193 * limit
194 */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000195
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000196 /* selgdt 0x40, flat code segment 16 bit */
197 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700198 .byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
199 * limit
200 */
Stefan Reinauer96938852015-06-18 01:23:48 -0700201
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200202#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700203 /* selgdt 0x48, flat x64 code segment */
204 .word 0xffff, 0x0000
205 .byte 0x00, 0x9b, 0xaf, 0x00
206#endif
Raul E Rangelb2346a52021-09-22 14:56:51 -0600207per_cpu_segment_descriptors:
208 .rept CONFIG_MAX_CPUS
209 /* flat data segment */
210 .word 0xffff, 0x0000
211#if ENV_X86_64
212 .byte 0x00, 0x92, 0xcf, 0x00
213#else
214 .byte 0x00, 0x93, 0xcf, 0x00
215#endif
216 .endr
Eric Biederman8ca8d762003-04-22 19:02:15 +0000217gdt_end:
218
Raul E Rangelb2346a52021-09-22 14:56:51 -0600219/* Segment selector pointing to the first per_cpu_segment_descriptor. */
220per_cpu_segment_selector:
221 .long per_cpu_segment_descriptors - gdt
Raul E Rangelb2346a52021-09-22 14:56:51 -0600222
Patrick Georgi546f29d2016-01-22 12:43:43 +0100223 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200224#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700225SetCodeSelector:
Martin Rothe3690102016-01-06 15:21:02 -0700226 # save rsp because iret will align it to a 16 byte boundary
Patrick Georgi0302b062016-01-22 12:26:52 +0100227 mov %rsp, %rdx
Stefan Reinauer96938852015-06-18 01:23:48 -0700228
Martin Rothe3690102016-01-06 15:21:02 -0700229 # use iret to jump to a 64-bit offset in a new code segment
230 # iret will pop cs:rip, flags, then ss:rsp
Patrick Georgi0302b062016-01-22 12:26:52 +0100231 mov %ss, %ax # need to push ss..
Martin Roth0949e732021-10-01 14:28:22 -0600232 push %rax # push ss instruction not valid in x64 mode,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700233 # so use ax
Patrick Georgi0302b062016-01-22 12:26:52 +0100234 push %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700235 pushfq
Patrick Georgi0302b062016-01-22 12:26:52 +0100236 push %rcx # cx is code segment selector from caller
Patrick Rudolphd0239092021-06-11 21:24:10 +0200237 movabs $setCodeSelectorLongJump, %rax
Patrick Georgi0302b062016-01-22 12:26:52 +0100238 push %rax
Stefan Reinauer96938852015-06-18 01:23:48 -0700239
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700240 # the iret will continue at next instruction, with the new cs value
241 # loaded
Martin Rothe3690102016-01-06 15:21:02 -0700242 iretq
Stefan Reinauer96938852015-06-18 01:23:48 -0700243
244setCodeSelectorLongJump:
Martin Rothe3690102016-01-06 15:21:02 -0700245 # restore rsp, it might not have been 16-byte aligned on entry
Patrick Georgi0302b062016-01-22 12:26:52 +0100246 mov %rdx, %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700247 ret
Stefan Reinauer96938852015-06-18 01:23:48 -0700248#endif