Debian Codegraph @

src/arch/x86/wakeup.S

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cpu/x86/gdt.h>
4
5 #define WAKEUP_BASE 0x600
6 #define RELOCATED(x) (x - __wakeup + WAKEUP_BASE)
7
8 /* CR0 bits */
9 #define PE (1 << 0)
10
11 #if ENV_X86_64
12 .code64
13 #else
14 .code32
15 #endif
16
17 .globl __wakeup
18 __wakeup:
19 #if ENV_X86_64
20 /* When called in x86_64 mode, the resume vector is in %rdi
21 * instead of the stack, save it in 4(%rsp) for the 32-bit code.
22 * It's OK to overwrite the return address at (%rsp) because this
23 * function doesn't return.
24 */
25 mov %edi, 4(%rsp)
26
27 xor %rax,%rax
28 mov %ss, %ax
29 push %rax
30 mov %rsp, %rax
31 add $8, %rax
32 push %rax
33 pushfq
34 push $GDT_CODE_SEG
35 lea 3(%rip), %rax
36 push %rax
37 iretq
38
39 .code32
40
41 /* disable paging */
42 mov %cr0, %eax
43 btc $31, %eax
44 mov %eax, %cr0
45
46 /* disable long mode */
47 mov $0xC0000080, %ecx
48 rdmsr
49 btc $8, %eax
50 wrmsr
51 #endif
52 /* First prepare the jmp to the resume vector */
53 mov 0x4(%esp), %eax /* vector */
54 /* last 4 bits of linear addr are taken as offset */
55 andw $0x0f, %ax
56 movw %ax, (__wakeup_offset)
57 mov 0x4(%esp), %eax
58 /* the rest is taken as segment */
59 shr $4, %eax
60 movw %ax, (__wakeup_segment)
61
62 /* Activate the right segment descriptor real mode. */
63 ljmp $GDT_CODE16_SEG, $RELOCATED(1f)
64 1:
65 .code16
66 /* 16 bit code from here on... */
67
68 /* Load the segment registers w/ properly configured
69 * segment descriptors. They will retain these
70 * configurations (limits, writability, etc.) once
71 * protected mode is turned off.
72 */
73 mov $GDT_DATA16_SEG, %ax
74 mov %ax, %ds
75 mov %ax, %es
76 mov %ax, %fs
77 mov %ax, %gs
78 mov %ax, %ss
79
80 /* Turn off protection */
81 movl %cr0, %eax
82 andl $~PE, %eax
83 movl %eax, %cr0
84
85 /* Now really going into real mode */
86 ljmp $0, $RELOCATED(1f)
87 1:
88 movw $0x0, %ax
89 movw %ax, %ds
90 movw %ax, %es
91 movw %ax, %ss
92 movw %ax, %fs
93 movw %ax, %gs
94
95 /* This is a FAR JMP to the OS waking vector. The C code changed
96 * the address to be correct.
97 */
98 .byte 0xea
99
100 __wakeup_offset = RELOCATED(.)
101 .word 0x0000
102
103 __wakeup_segment = RELOCATED(.)
104 .word 0x0000
105
106 .globl __wakeup_size
107 __wakeup_size:
108 .long . - __wakeup