Debian Codegraph @

src/cpu/x86/64bit/exit32.inc

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * For dropping from long mode to protected mode.
5 *
6 * For reference see "AMD64 ArchitectureProgrammer's Manual Volume 2",
7 * Document 24593-Rev. 3.31-July 2019 Chapter 5.3
8 *
9 * Clobbers: rax, rbx, rcx, rdx
10 */
11 .code64
12
13 #include <cpu/x86/gdt.h>
14 #include <cpu/x86/msr.h>
15 #include <cpu/x86/cr.h>
16
17 drop_longmode:
18 #if !ENV_CACHE_AS_RAM
19 /* Ensure cache is clean. */
20 wbinvd
21 #endif
22 /* Set 32-bit code segment and ss */
23 mov $GDT_CODE_SEG, %rcx
24 /* SetCodeSelector32 will drop us to protected mode on return */
25 call SetCodeSelector32
26
27 /* Skip SetCodeSelector32 */
28 .code32
29 jmp __longmode_compatibility
30
31 .align 8
32 .code64
33 SetCodeSelector32:
34 # pop the return address from stack
35 pop %rbx
36
37 # save rsp because we need to push it after ss
38 mov %rsp, %rdx
39
40 # use iret to jump to a 32-bit offset in a new code segment
41 # iret will pop cs:rip, flags, then ss:rsp
42 mov %ss, %ax # need to push ss, but push ss instruction
43 push %rax # not valid in x64 mode, so use ax
44 push %rdx # the rsp to load
45 pushfq # push rflags
46 push %rcx # cx is code segment selector from caller
47 push %rbx # push the IP for the next instruction
48
49 # the iretq will behave like ret, with the new cs/ss value loaded
50 iretq
51
52 .align 4
53 .code32
54 __longmode_compatibility:
55 /* Running in 32-bit compatibility mode */
56
57 /* Use flat data segment */
58 movl $GDT_DATA_SEG, %eax
59 movl %eax, %ds
60 movl %eax, %es
61 movl %eax, %ss
62 movl %eax, %fs
63
64 /* Disable paging. */
65 movl %cr0, %eax
66 andl $(~CR0_PG), %eax
67 movl %eax, %cr0
68
69 /* Disable long mode. */
70 movl $(IA32_EFER), %ecx
71 rdmsr
72 andl $(~EFER_LME), %eax
73 wrmsr
74
75 /* Disable PAE. */
76 movl %cr4, %eax
77 andl $(~CR4_PAE), %eax
78 movl %eax, %cr4
79
80 /* Clear page table register */
81 xor %eax, %eax
82 movl %eax, %cr3
83
84 __longmode_exit: