blob: b6375b1243c8666d420346d170e5c46cdb0c5857 [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
Raul E Rangelda68c9d2021-09-22 10:45:51 -06003#include <cpu/x86/cpu_info.S.inc>
Stefan Reinauer5f5436f2010-04-25 20:42:02 +00004#include <cpu/x86/post_code.h>
Patrick Rudolph776da082019-10-25 08:09:33 +02005#include <arch/ram_segs.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00006
Aaron Durbin633f1122013-02-06 15:28:40 -06007/* Place the stack in the bss section. It's not necessary to define it in the
8 * the linker script. */
9 .section .bss, "aw", @nobits
10.global _stack
11.global _estack
Julius Werner82d16b12020-12-30 15:51:10 -080012.global _stack_size
Aaron Durbin633f1122013-02-06 15:28:40 -060013
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030014/* Stack alignment is not enforced with rmodule loader, reserve one
15 * extra CPU such that alignment can be enforced on entry. */
Aaron Durbin633f1122013-02-06 15:28:40 -060016.align CONFIG_STACK_SIZE
17_stack:
Kyösti Mälkki2fbb6772018-05-15 19:50:20 +030018.space (CONFIG_MAX_CPUS+1)*CONFIG_STACK_SIZE
Aaron Durbin633f1122013-02-06 15:28:40 -060019_estack:
Julius Werner82d16b12020-12-30 15:51:10 -080020.set _stack_size, _estack - _stack
Aaron Durbin633f1122013-02-06 15:28:40 -060021
Julius Wernerec5e5e02014-08-20 15:29:56 -070022 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +020023#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -070024 .code64
25#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000026 .code32
Stefan Reinauer96938852015-06-18 01:23:48 -070027#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 .globl _start
29_start:
30 cli
Patrick Rudolphadcf7822020-08-27 20:50:18 +020031#if ENV_X86_64
Patrick Rudolphd0239092021-06-11 21:24:10 +020032 movabs $gdtaddr, %rax
33 lgdt (%rax)
34#else
Eric Biederman8ca8d762003-04-22 19:02:15 +000035 lgdt %cs:gdtaddr
Patrick Rudolph776da082019-10-25 08:09:33 +020036 ljmp $RAM_CODE_SEG, $1f
Stefan Reinauer96938852015-06-18 01:23:48 -070037#endif
Patrick Rudolph776da082019-10-25 08:09:33 +0200381: movl $RAM_DATA_SEG, %eax
Eric Biederman8ca8d762003-04-22 19:02:15 +000039 movl %eax, %ds
40 movl %eax, %es
41 movl %eax, %ss
42 movl %eax, %fs
43 movl %eax, %gs
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
Raul E Rangelda68c9d2021-09-22 10:45:51 -060080 push_cpu_info
Eric Biederman8ca8d762003-04-22 19:02:15 +000081
Eric Biederman8ca8d762003-04-22 19:02:15 +000082 /*
83 * Now we are finished. Memory is up, data is copied and
84 * bss is cleared. Now we call the main routine and
85 * let it do the rest.
Stefan Reinauer607cdf62010-04-26 12:08:51 +000086 */
Subrata Banik38e4a2d2021-05-05 19:34:07 +053087 post_code(POST_PRE_HARDWAREMAIN) /* post 6e */
Eric Biederman8ca8d762003-04-22 19:02:15 +000088
Kyösti Mälkki4796c322017-03-15 08:07:22 +020089 andl $0xFFFFFFF0, %esp
90
Harshit Sharma9c88fb82020-06-17 20:19:00 -070091#if CONFIG(ASAN_IN_RAMSTAGE)
92 call asan_init
93#endif
94
Julius Wernercd49cce2019-03-05 16:53:33 -080095#if CONFIG(GDB_WAIT)
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +030096 call gdb_hw_init
Denis 'GNUtoo' Cariklie4cece02012-06-22 15:56:37 +020097 call gdb_stub_breakpoint
98#endif
Stefan Reinauer6adef082013-05-09 16:30:06 -070099 call main
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000100 /* NOTREACHED */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000101.Lhlt:
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000102 post_code(POST_DEAD_CODE) /* post ee */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000103 hlt
104 jmp .Lhlt
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000105
Julius Wernercd49cce2019-03-05 16:53:33 -0800106#if CONFIG(GDB_WAIT)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000107
108 .globl gdb_stub_breakpoint
109gdb_stub_breakpoint:
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200110#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700111 pop %rax /* Return address */
112 pushfl
113 push %cs
114 push %rax /* Return address */
115 push $0 /* No error code */
116 push $32 /* vector 32 is user defined */
117#else
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000118 popl %eax /* Return address */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000119 pushfl
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000120 pushl %cs
121 pushl %eax /* Return address */
122 pushl $0 /* No error code */
123 pushl $32 /* vector 32 is user defined */
Stefan Reinauer96938852015-06-18 01:23:48 -0700124#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000125 jmp int_hand
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000126#endif
127
Aaron Durbin4b032e42018-04-20 01:39:30 -0600128 .globl gdt, gdt_end
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129
Eric Biederman8ca8d762003-04-22 19:02:15 +0000130gdtaddr:
Aaron Durbina146d582013-02-08 16:56:51 -0600131 .word gdt_end - gdt - 1
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200132#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700133 .quad gdt
134#else
Li-Ta Lof84926e2004-11-04 18:36:06 +0000135 .long gdt /* we know the offset */
Stefan Reinauer96938852015-06-18 01:23:48 -0700136#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000138 .data
Li-Ta Lof84926e2004-11-04 18:36:06 +0000139
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000140 /* This is the gdt for GCC part of coreboot.
Arthur Heymans1cb9cd52019-11-28 16:05:08 +0100141 * It is different from the gdt in ASM part of coreboot
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200142 * which is defined in gdt_init.S
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000143 *
144 * When the machine is initially started, we use a very simple
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200145 * gdt from ROM (that in gdt_init.S) which only contains those
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000146 * entries we need for protected mode.
147 *
148 * When we're executing code from RAM, we want to do more complex
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200149 * stuff, like initializing PCI option ROMs in real mode, or doing
150 * a resume from a suspend to RAM.
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000151 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000152gdt:
Li-Ta Lof84926e2004-11-04 18:36:06 +0000153 /* selgdt 0, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154 .word 0x0000, 0x0000 /* dummy */
155 .byte 0x00, 0x00, 0x00, 0x00
156
Li-Ta Lof84926e2004-11-04 18:36:06 +0000157 /* selgdt 8, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158 .word 0x0000, 0x0000 /* dummy */
159 .byte 0x00, 0x00, 0x00, 0x00
160
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000161 /* selgdt 0x10, flat code segment */
162 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700163 .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
164 * limit
165 */
Li-Ta Lof84926e2004-11-04 18:36:06 +0000166
167 /* selgdt 0x18, flat data segment */
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000168 .word 0xffff, 0x0000
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200169#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700170 .byte 0x00, 0x92, 0xcf, 0x00
171#else
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000172 .byte 0x00, 0x93, 0xcf, 0x00
Stefan Reinauer96938852015-06-18 01:23:48 -0700173#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174
Li-Ta Lof84926e2004-11-04 18:36:06 +0000175 /* selgdt 0x20, unused */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 .word 0x0000, 0x0000 /* dummy */
177 .byte 0x00, 0x00, 0x00, 0x00
178
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000179 /* The next two entries are used for executing VGA option ROMs */
180
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000181 /* selgdt 0x28 16 bit 64k code at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100182 .word 0xffff, 0x0000
183 .byte 0, 0x9a, 0, 0
Stefan Reinauerf8a5c6e2009-05-29 13:08:27 +0000184
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000185 /* selgdt 0x30 16 bit 64k data at 0x00000000 */
Elyes HAOUAS2ea751a2018-12-27 09:21:02 +0100186 .word 0xffff, 0x0000
187 .byte 0, 0x92, 0, 0
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000188
189 /* The next two entries are used for ACPI S3 RESUME */
190
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000191 /* selgdt 0x38, flat data segment 16 bit */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000192 .word 0x0000, 0x0000 /* dummy */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700193 .byte 0x00, 0x93, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
194 * limit
195 */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000196
Stefan Reinauer607cdf62010-04-26 12:08:51 +0000197 /* selgdt 0x40, flat code segment 16 bit */
198 .word 0xffff, 0x0000
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700199 .byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for
200 * limit
201 */
Stefan Reinauer96938852015-06-18 01:23:48 -0700202
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200203#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700204 /* selgdt 0x48, flat x64 code segment */
205 .word 0xffff, 0x0000
206 .byte 0x00, 0x9b, 0xaf, 0x00
207#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000208gdt_end:
209
Patrick Georgi546f29d2016-01-22 12:43:43 +0100210 .section ".text._start", "ax", @progbits
Patrick Rudolphadcf7822020-08-27 20:50:18 +0200211#if ENV_X86_64
Stefan Reinauer96938852015-06-18 01:23:48 -0700212SetCodeSelector:
Martin Rothe3690102016-01-06 15:21:02 -0700213 # save rsp because iret will align it to a 16 byte boundary
Patrick Georgi0302b062016-01-22 12:26:52 +0100214 mov %rsp, %rdx
Stefan Reinauer96938852015-06-18 01:23:48 -0700215
Martin Rothe3690102016-01-06 15:21:02 -0700216 # use iret to jump to a 64-bit offset in a new code segment
217 # iret will pop cs:rip, flags, then ss:rsp
Patrick Georgi0302b062016-01-22 12:26:52 +0100218 mov %ss, %ax # need to push ss..
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700219 push %rax # push ss instuction not valid in x64 mode,
220 # so use ax
Patrick Georgi0302b062016-01-22 12:26:52 +0100221 push %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700222 pushfq
Patrick Georgi0302b062016-01-22 12:26:52 +0100223 push %rcx # cx is code segment selector from caller
Patrick Rudolphd0239092021-06-11 21:24:10 +0200224 movabs $setCodeSelectorLongJump, %rax
Patrick Georgi0302b062016-01-22 12:26:52 +0100225 push %rax
Stefan Reinauer96938852015-06-18 01:23:48 -0700226
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700227 # the iret will continue at next instruction, with the new cs value
228 # loaded
Martin Rothe3690102016-01-06 15:21:02 -0700229 iretq
Stefan Reinauer96938852015-06-18 01:23:48 -0700230
231setCodeSelectorLongJump:
Martin Rothe3690102016-01-06 15:21:02 -0700232 # restore rsp, it might not have been 16-byte aligned on entry
Patrick Georgi0302b062016-01-22 12:26:52 +0100233 mov %rdx, %rsp
Martin Rothe3690102016-01-06 15:21:02 -0700234 ret
Stefan Reinauer96938852015-06-18 01:23:48 -0700235#endif