Martin Roth | 9df9e939 | 2016-01-12 15:55:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
Stefan Reinauer | 5f5436f | 2010-04-25 20:42:02 +0000 | [diff] [blame] | 14 | #include <cpu/x86/post_code.h> |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame^] | 15 | #include <arch/ram_segs.h> |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 16 | |
Aaron Durbin | 633f112 | 2013-02-06 15:28:40 -0600 | [diff] [blame] | 17 | /* Place the stack in the bss section. It's not necessary to define it in the |
| 18 | * the linker script. */ |
| 19 | .section .bss, "aw", @nobits |
| 20 | .global _stack |
| 21 | .global _estack |
| 22 | |
Kyösti Mälkki | 2fbb677 | 2018-05-15 19:50:20 +0300 | [diff] [blame] | 23 | /* Stack alignment is not enforced with rmodule loader, reserve one |
| 24 | * extra CPU such that alignment can be enforced on entry. */ |
Aaron Durbin | 633f112 | 2013-02-06 15:28:40 -0600 | [diff] [blame] | 25 | .align CONFIG_STACK_SIZE |
| 26 | _stack: |
Kyösti Mälkki | 2fbb677 | 2018-05-15 19:50:20 +0300 | [diff] [blame] | 27 | .space (CONFIG_MAX_CPUS+1)*CONFIG_STACK_SIZE |
Aaron Durbin | 633f112 | 2013-02-06 15:28:40 -0600 | [diff] [blame] | 28 | _estack: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 29 | #if CONFIG(COOP_MULTITASKING) |
Aaron Durbin | 38c326d | 2013-05-06 12:22:23 -0500 | [diff] [blame] | 30 | .global thread_stacks |
| 31 | thread_stacks: |
| 32 | .space CONFIG_STACK_SIZE*CONFIG_NUM_THREADS |
| 33 | #endif |
Aaron Durbin | 633f112 | 2013-02-06 15:28:40 -0600 | [diff] [blame] | 34 | |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 35 | .section ".text._start", "ax", @progbits |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 36 | #ifdef __x86_64__ |
| 37 | .code64 |
| 38 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 39 | .code32 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 40 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 41 | .globl _start |
| 42 | _start: |
| 43 | cli |
| 44 | lgdt %cs:gdtaddr |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 45 | #ifndef __x86_64__ |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame^] | 46 | ljmp $RAM_CODE_SEG, $1f |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 47 | #endif |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame^] | 48 | 1: movl $RAM_DATA_SEG, %eax |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | movl %eax, %ds |
| 50 | movl %eax, %es |
| 51 | movl %eax, %ss |
| 52 | movl %eax, %fs |
| 53 | movl %eax, %gs |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 54 | #ifdef __x86_64__ |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame^] | 55 | mov $RAM_CODE_SEG64, %ecx |
Elyes HAOUAS | 2ea751a | 2018-12-27 09:21:02 +0100 | [diff] [blame] | 56 | call SetCodeSelector |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 57 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 59 | post_code(POST_ENTRY_C_START) /* post 13 */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 60 | |
arch import user (historical) | 6ca7636 | 2005-07-06 17:17:25 +0000 | [diff] [blame] | 61 | cld |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 62 | |
Aaron Durbin | 633f112 | 2013-02-06 15:28:40 -0600 | [diff] [blame] | 63 | /** poison the stack. Code should not count on the |
| 64 | * stack being full of zeros. This stack poisoning |
| 65 | * recently uncovered a bug in the broadcast SIPI |
| 66 | * code. |
| 67 | */ |
| 68 | leal _stack, %edi |
| 69 | 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älkki | 2fbb677 | 2018-05-15 19:50:20 +0300 | [diff] [blame] | 76 | /* Set new stack with enforced alignment. */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 77 | movl $_estack, %esp |
Kyösti Mälkki | 2fbb677 | 2018-05-15 19:50:20 +0300 | [diff] [blame] | 78 | andl $(~(CONFIG_STACK_SIZE-1)), %esp |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 79 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 80 | #if CONFIG(COOP_MULTITASKING) |
Aaron Durbin | 38c326d | 2013-05-06 12:22:23 -0500 | [diff] [blame] | 81 | /* Push the thread pointer. */ |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 82 | push $0 |
Aaron Durbin | 38c326d | 2013-05-06 12:22:23 -0500 | [diff] [blame] | 83 | #endif |
Elyes HAOUAS | 777ea89 | 2016-07-29 07:40:41 +0200 | [diff] [blame] | 84 | /* Push the CPU index and struct CPU */ |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 85 | push $0 |
| 86 | push $0 |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 87 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Now we are finished. Memory is up, data is copied and |
| 90 | * bss is cleared. Now we call the main routine and |
| 91 | * let it do the rest. |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 92 | */ |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 93 | post_code(POST_PRE_HARDWAREMAIN) /* post fe */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 94 | |
Kyösti Mälkki | 4796c32 | 2017-03-15 08:07:22 +0200 | [diff] [blame] | 95 | andl $0xFFFFFFF0, %esp |
| 96 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 97 | #if CONFIG(GDB_WAIT) |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 98 | call gdb_hw_init |
Denis 'GNUtoo' Carikli | e4cece0 | 2012-06-22 15:56:37 +0200 | [diff] [blame] | 99 | call gdb_stub_breakpoint |
| 100 | #endif |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 101 | call main |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 102 | /* NOTREACHED */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 103 | .Lhlt: |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 104 | post_code(POST_DEAD_CODE) /* post ee */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 105 | hlt |
| 106 | jmp .Lhlt |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 107 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 108 | #if CONFIG(GDB_WAIT) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 109 | |
| 110 | .globl gdb_stub_breakpoint |
| 111 | gdb_stub_breakpoint: |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 112 | #ifdef __x86_64__ |
| 113 | pop %rax /* Return address */ |
| 114 | pushfl |
| 115 | push %cs |
| 116 | push %rax /* Return address */ |
| 117 | push $0 /* No error code */ |
| 118 | push $32 /* vector 32 is user defined */ |
| 119 | #else |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 120 | popl %eax /* Return address */ |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 121 | pushfl |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 122 | pushl %cs |
| 123 | pushl %eax /* Return address */ |
| 124 | pushl $0 /* No error code */ |
| 125 | pushl $32 /* vector 32 is user defined */ |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 126 | #endif |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 127 | jmp int_hand |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
Aaron Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 130 | .globl gdt, gdt_end |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 131 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 132 | gdtaddr: |
Aaron Durbin | a146d58 | 2013-02-08 16:56:51 -0600 | [diff] [blame] | 133 | .word gdt_end - gdt - 1 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 134 | #ifdef __x86_64__ |
| 135 | .quad gdt |
| 136 | #else |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 137 | .long gdt /* we know the offset */ |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 138 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 139 | |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 140 | .data |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 141 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 142 | /* This is the gdt for GCC part of coreboot. |
| 143 | * It is different from the gdt in ROMCC/ASM part of coreboot |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 144 | * which is defined in entry32.inc |
| 145 | * |
| 146 | * When the machine is initially started, we use a very simple |
Elyes HAOUAS | 777ea89 | 2016-07-29 07:40:41 +0200 | [diff] [blame] | 147 | * gdt from ROM (that in entry32.inc) which only contains those |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 148 | * entries we need for protected mode. |
| 149 | * |
| 150 | * When we're executing code from RAM, we want to do more complex |
Elyes HAOUAS | 777ea89 | 2016-07-29 07:40:41 +0200 | [diff] [blame] | 151 | * stuff, like initializing PCI option ROMs in real mode, or doing |
| 152 | * a resume from a suspend to RAM. |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 153 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 154 | gdt: |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 155 | /* selgdt 0, unused */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 156 | .word 0x0000, 0x0000 /* dummy */ |
| 157 | .byte 0x00, 0x00, 0x00, 0x00 |
| 158 | |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 159 | /* selgdt 8, unused */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 160 | .word 0x0000, 0x0000 /* dummy */ |
| 161 | .byte 0x00, 0x00, 0x00, 0x00 |
| 162 | |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 163 | /* selgdt 0x10, flat code segment */ |
| 164 | .word 0xffff, 0x0000 |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 165 | .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for |
| 166 | * limit |
| 167 | */ |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 168 | |
| 169 | /* selgdt 0x18, flat data segment */ |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 170 | .word 0xffff, 0x0000 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 171 | #ifdef __x86_64__ |
| 172 | .byte 0x00, 0x92, 0xcf, 0x00 |
| 173 | #else |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 174 | .byte 0x00, 0x93, 0xcf, 0x00 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 175 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 176 | |
Li-Ta Lo | f84926e | 2004-11-04 18:36:06 +0000 | [diff] [blame] | 177 | /* selgdt 0x20, unused */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 178 | .word 0x0000, 0x0000 /* dummy */ |
| 179 | .byte 0x00, 0x00, 0x00, 0x00 |
| 180 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 181 | /* The next two entries are used for executing VGA option ROMs */ |
| 182 | |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 183 | /* selgdt 0x28 16 bit 64k code at 0x00000000 */ |
Elyes HAOUAS | 2ea751a | 2018-12-27 09:21:02 +0100 | [diff] [blame] | 184 | .word 0xffff, 0x0000 |
| 185 | .byte 0, 0x9a, 0, 0 |
Stefan Reinauer | f8a5c6e | 2009-05-29 13:08:27 +0000 | [diff] [blame] | 186 | |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 187 | /* selgdt 0x30 16 bit 64k data at 0x00000000 */ |
Elyes HAOUAS | 2ea751a | 2018-12-27 09:21:02 +0100 | [diff] [blame] | 188 | .word 0xffff, 0x0000 |
| 189 | .byte 0, 0x92, 0, 0 |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 190 | |
| 191 | /* The next two entries are used for ACPI S3 RESUME */ |
| 192 | |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 193 | /* selgdt 0x38, flat data segment 16 bit */ |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 194 | .word 0x0000, 0x0000 /* dummy */ |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 195 | .byte 0x00, 0x93, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for |
| 196 | * limit |
| 197 | */ |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 198 | |
Stefan Reinauer | 607cdf6 | 2010-04-26 12:08:51 +0000 | [diff] [blame] | 199 | /* selgdt 0x40, flat code segment 16 bit */ |
| 200 | .word 0xffff, 0x0000 |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 201 | .byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for |
| 202 | * limit |
| 203 | */ |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 204 | |
| 205 | #ifdef __x86_64__ |
| 206 | /* selgdt 0x48, flat x64 code segment */ |
| 207 | .word 0xffff, 0x0000 |
| 208 | .byte 0x00, 0x9b, 0xaf, 0x00 |
| 209 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 210 | gdt_end: |
| 211 | |
Patrick Georgi | 546f29d | 2016-01-22 12:43:43 +0100 | [diff] [blame] | 212 | .section ".text._start", "ax", @progbits |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 213 | #ifdef __x86_64__ |
| 214 | SetCodeSelector: |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 215 | # save rsp because iret will align it to a 16 byte boundary |
Patrick Georgi | 0302b06 | 2016-01-22 12:26:52 +0100 | [diff] [blame] | 216 | mov %rsp, %rdx |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 217 | |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 218 | # use iret to jump to a 64-bit offset in a new code segment |
| 219 | # iret will pop cs:rip, flags, then ss:rsp |
Patrick Georgi | 0302b06 | 2016-01-22 12:26:52 +0100 | [diff] [blame] | 220 | mov %ss, %ax # need to push ss.. |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 221 | push %rax # push ss instuction not valid in x64 mode, |
| 222 | # so use ax |
Patrick Georgi | 0302b06 | 2016-01-22 12:26:52 +0100 | [diff] [blame] | 223 | push %rsp |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 224 | pushfq |
Patrick Georgi | 0302b06 | 2016-01-22 12:26:52 +0100 | [diff] [blame] | 225 | push %rcx # cx is code segment selector from caller |
| 226 | mov $setCodeSelectorLongJump, %rax |
| 227 | push %rax |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 228 | |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 229 | # the iret will continue at next instruction, with the new cs value |
| 230 | # loaded |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 231 | iretq |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 232 | |
| 233 | setCodeSelectorLongJump: |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 234 | # restore rsp, it might not have been 16-byte aligned on entry |
Patrick Georgi | 0302b06 | 2016-01-22 12:26:52 +0100 | [diff] [blame] | 235 | mov %rdx, %rsp |
Martin Roth | e369010 | 2016-01-06 15:21:02 -0700 | [diff] [blame] | 236 | ret |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 237 | |
| 238 | .previous |
| 239 | .code64 |
| 240 | #else |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 241 | .previous |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 242 | .code32 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 243 | #endif |