| 1 |
/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 |
|
| 3 |
#include <cpu/x86/gdt.h> |
| 4 |
#include <cpu/x86/post_code.h> |
| 5 |
|
| 6 |
/* Place the stack in the bss section. It's not necessary to define it in |
| 7 |
* the linker script. */ |
| 8 |
.section .bss, "aw", @nobits |
| 9 |
.global _stack |
| 10 |
.global _estack |
| 11 |
.global _stack_size |
| 12 |
|
| 13 |
.align 16 |
| 14 |
_stack: |
| 15 |
.space CONFIG_STACK_SIZE |
| 16 |
_estack: |
| 17 |
.set _stack_size, _estack - _stack |
| 18 |
|
| 19 |
.section ".text._start", "ax", @progbits |
| 20 |
#if ENV_X86_64 |
| 21 |
.code64 |
| 22 |
#else |
| 23 |
.code32 |
| 24 |
#endif |
| 25 |
.globl _start |
| 26 |
_start: |
| 27 |
cli |
| 28 |
#if ENV_X86_64 |
| 29 |
movabs $gdtaddr, %rax |
| 30 |
lgdt (%rax) |
| 31 |
#else |
| 32 |
lgdt %cs:gdtaddr |
| 33 |
ljmp $GDT_CODE_SEG, $1f |
| 34 |
#endif |
| 35 |
1: movl $GDT_DATA_SEG, %eax |
| 36 |
movl %eax, %ds |
| 37 |
movl %eax, %es |
| 38 |
movl %eax, %ss |
| 39 |
xor %eax, %eax /* zero out the gs and fs segment index */ |
| 40 |
movl %eax, %fs |
| 41 |
movl %eax, %gs /* Will be used for cpu_info */ |
| 42 |
#if ENV_X86_64 |
| 43 |
mov $GDT_CODE_SEG64, %ecx |
| 44 |
call SetCodeSelector |
| 45 |
#endif |
| 46 |
|
| 47 |
post_code(POSTCODE_ENTRY_C_START) /* post 13 */ |
| 48 |
|
| 49 |
cld |
| 50 |
|
| 51 |
#if ENV_X86_64 |
| 52 |
mov %rdi, %rax |
| 53 |
movabs %rax, _cbmem_top_ptr |
| 54 |
movabs $_stack, %rdi |
| 55 |
#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 |
| 59 |
leal _stack, %edi |
| 60 |
#endif |
| 61 |
|
| 62 |
#if ENV_X86_64 |
| 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 |
movabs $_estack, %rcx |
| 69 |
sub %rdi, %rcx |
| 70 |
shr $3, %rcx /* it is 64 bit aligned, right? */ |
| 71 |
movq $0xDEADBEEFDEADBEEF, %rax |
| 72 |
rep |
| 73 |
stosq |
| 74 |
|
| 75 |
/* Set new stack with enforced alignment. */ |
| 76 |
movabs $_estack, %rsp |
| 77 |
movq $(0xfffffffffffffff0), %rax |
| 78 |
and %rax, %rsp |
| 79 |
#else |
| 80 |
/** poison the stack. Code should not count on the |
| 81 |
* stack being full of zeros. This stack poisoning |
| 82 |
* recently uncovered a bug in the broadcast SIPI |
| 83 |
* code. |
| 84 |
*/ |
| 85 |
movl $_estack, %ecx |
| 86 |
subl %edi, %ecx |
| 87 |
shrl $2, %ecx /* it is 32 bit aligned, right? */ |
| 88 |
movl $0xDEADBEEF, %eax |
| 89 |
rep |
| 90 |
stosl |
| 91 |
|
| 92 |
/* Set new stack with enforced alignment. */ |
| 93 |
movl $_estack, %esp |
| 94 |
andl $(0xfffffff0), %esp |
| 95 |
#endif |
| 96 |
|
| 97 |
/* |
| 98 |
* Now we are finished. Memory is up, data is copied and |
| 99 |
* bss is cleared. Now we call the main routine and |
| 100 |
* let it do the rest. |
| 101 |
*/ |
| 102 |
post_code(POSTCODE_PRE_HARDWAREMAIN) /* post 6e */ |
| 103 |
|
| 104 |
#if CONFIG(ASAN_IN_RAMSTAGE) |
| 105 |
call asan_init |
| 106 |
#endif |
| 107 |
|
| 108 |
#if CONFIG(GDB_WAIT) |
| 109 |
call gdb_hw_init |
| 110 |
call gdb_stub_breakpoint |
| 111 |
#endif |
| 112 |
call main |
| 113 |
/* NOTREACHED */ |
| 114 |
.Lhlt: |
| 115 |
post_code(POSTCODE_DEAD_CODE) /* post ee */ |
| 116 |
hlt |
| 117 |
jmp .Lhlt |
| 118 |
|
| 119 |
#if CONFIG(GDB_WAIT) |
| 120 |
|
| 121 |
.globl gdb_stub_breakpoint |
| 122 |
gdb_stub_breakpoint: |
| 123 |
#if ENV_X86_64 |
| 124 |
pop %rax /* Return address */ |
| 125 |
pushfq |
| 126 |
mov %cs, %rbx |
| 127 |
push %rbx |
| 128 |
push %rax /* Return address */ |
| 129 |
push $0 /* No error code */ |
| 130 |
push $32 /* vector 32 is user defined */ |
| 131 |
#else |
| 132 |
popl %eax /* Return address */ |
| 133 |
pushfl |
| 134 |
pushl %cs |
| 135 |
pushl %eax /* Return address */ |
| 136 |
pushl $0 /* No error code */ |
| 137 |
pushl $32 /* vector 32 is user defined */ |
| 138 |
#endif |
| 139 |
jmp int_hand |
| 140 |
#endif |
| 141 |
|
| 142 |
.globl gdt, gdt_end |
| 143 |
.global per_cpu_segment_descriptors, per_cpu_segment_selector |
| 144 |
|
| 145 |
gdtaddr: |
| 146 |
.word gdt_end - gdt - 1 |
| 147 |
#if ENV_X86_64 |
| 148 |
.quad gdt |
| 149 |
#else |
| 150 |
.long gdt /* we know the offset */ |
| 151 |
#endif |
| 152 |
|
| 153 |
.data |
| 154 |
|
| 155 |
/* |
| 156 |
* This is the gdt for coreboot's ramstage. |
| 157 |
* It is different from the gdt in ASM part of coreboot |
| 158 |
* which is defined in gdt_init.S |
| 159 |
* |
| 160 |
* When the machine is initially started, we use a very simple |
| 161 |
* gdt from ROM (that in gdt_init.S) which only contains those |
| 162 |
* entries we need for protected mode and long mode. |
| 163 |
* |
| 164 |
* When we're executing code from RAM, we want to do more complex |
| 165 |
* stuff, like initializing PCI option ROMs in real mode, or doing |
| 166 |
* a resume from a suspend to RAM, which happens in real mode. |
| 167 |
* |
| 168 |
* Keep in sync with 'cpu/x86/gdt.h'. |
| 169 |
*/ |
| 170 |
gdt: |
| 171 |
/* selgdt 0, unused */ |
| 172 |
.word 0x0000, 0x0000 /* dummy */ |
| 173 |
.byte 0x00, 0x00, 0x00, 0x00 |
| 174 |
|
| 175 |
/* selgdt 0x08, flat code segment */ |
| 176 |
.word 0xffff, 0x0000 |
| 177 |
.byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, So we get 4Gbytes |
| 178 |
for limit */ |
| 179 |
|
| 180 |
/* selgdt 0x10, flat data segment */ |
| 181 |
.word 0xffff, 0x0000 |
| 182 |
.byte 0x00, 0x93, 0xcf, 0x00 |
| 183 |
|
| 184 |
/* selgdt 0x18, flat code segment (64-bit) */ |
| 185 |
.word 0xffff, 0x0000 |
| 186 |
.byte 0x00, 0x9b, 0xaf, 0x00 |
| 187 |
|
| 188 |
/* gdt selector 0x20 tss segment, used by STM */ |
| 189 |
.word 0xffff, 0x0000 |
| 190 |
.byte 0x00, 0x8b, 0x80, 0x00 |
| 191 |
|
| 192 |
/* The next two entries are used for executing ACPI S3 RESUME and VGA option ROMs */ |
| 193 |
|
| 194 |
/* selgdt 0x28 16 bit 64k code at 0x00000000 */ |
| 195 |
.word 0xffff, 0x0000 |
| 196 |
.byte 0, 0x9a, 0, 0 |
| 197 |
|
| 198 |
/* selgdt 0x30 16 bit 64k data at 0x00000000 */ |
| 199 |
.word 0xffff, 0x0000 |
| 200 |
.byte 0, 0x92, 0, 0 |
| 201 |
|
| 202 |
/* The next entry is used for VGA option ROMs. See x86_asm.S */ |
| 203 |
|
| 204 |
/* |
| 205 |
* selgdt 0x38, flat code segment 16 bits |
| 206 |
* |
| 207 |
* FIXME: It's only used in %ds (therefore used like a data segment). |
| 208 |
* The PCI Specification doesn't enforce this. |
| 209 |
* Is this a workaround for broken Option ROMs? |
| 210 |
*/ |
| 211 |
.word 0xffff, 0x0000 |
| 212 |
.byte 0x00, 0x9b, 0x8f, 0x00 /* G=1 and 0x0f, So we get 4Gbytes for |
| 213 |
* limit |
| 214 |
*/ |
| 215 |
|
| 216 |
per_cpu_segment_descriptors: |
| 217 |
.rept CONFIG_MAX_CPUS |
| 218 |
/* flat data segment */ |
| 219 |
.word 0xffff, 0x0000 |
| 220 |
.byte 0x00, 0x93, 0xcf, 0x00 |
| 221 |
.endr |
| 222 |
gdt_end: |
| 223 |
|
| 224 |
/* Segment selector pointing to the first per_cpu_segment_descriptor. */ |
| 225 |
per_cpu_segment_selector: |
| 226 |
.long per_cpu_segment_descriptors - gdt |
| 227 |
|
| 228 |
.section ".text._start", "ax", @progbits |
| 229 |
#if ENV_X86_64 |
| 230 |
SetCodeSelector: |
| 231 |
# save rsp because iret will align it to a 16 byte boundary |
| 232 |
mov %rsp, %rdx |
| 233 |
|
| 234 |
# use iret to jump to a 64-bit offset in a new code segment |
| 235 |
# iret will pop cs:rip, flags, then ss:rsp |
| 236 |
mov %ss, %ax # need to push ss.. |
| 237 |
push %rax # push ss instruction not valid in x64 mode, |
| 238 |
# so use ax |
| 239 |
push %rsp |
| 240 |
pushfq |
| 241 |
push %rcx # cx is code segment selector from caller |
| 242 |
movabs $setCodeSelectorLongJump, %rax |
| 243 |
push %rax |
| 244 |
|
| 245 |
# the iret will continue at next instruction, with the new cs value |
| 246 |
# loaded |
| 247 |
iretq |
| 248 |
|
| 249 |
setCodeSelectorLongJump: |
| 250 |
# restore rsp, it might not have been 16-byte aligned on entry |
| 251 |
mov %rdx, %rsp |
| 252 |
ret |
| 253 |
#endif |