Debian Codegraph @

payloads/libpayload/arch/x86/pt.S

raw
1 /*
2 *
3 * Copyright 2024 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * For reference see "AMD64 Architecture Programmer's Manual Volume 2",
31 * Document 24593-Rev. 3.31-July 2019 Chapter 5.3.4
32 *
33 * Page table attributes: WB, User+Supervisor, Present, Writeable, Accessed, Dirty
34 */
35
36 .section .bss
37 #define _PRES (1ULL << 0)
38 #define _RW (1ULL << 1)
39 #define _US (1ULL << 2)
40 #define _A (1ULL << 5)
41 #define _D (1ULL << 6)
42 #define _PS (1ULL << 7)
43
44 .section .bss.pml4e
45 .global pml4e
46 .align 4096
47 pml4e:
48 .skip 8
49
50 .section .bss.main_page_table
51 .global main_page_table
52 .align 4096
53 main_page_table:
54 .skip 8192
55
56 .section .bss.extra_page_table
57 .global extra_page_table
58 .align 4096
59 extra_page_table:
60 .skip 32
61
62 /*
63 * WARNING: 32-bit/64-bit Mode Compatibility for Page Table Initialization
64 * This `init_page_table` function is designed to work in both 32-bit protected
65 * mode AND 64-bit long mode.
66 *
67 * Key Considerations:
68 * - Assembly Instructions: Use ONLY instructions that have the SAME binary representation
69 * in both 32-bit and 64-bit modes.
70 * - `.code64` Directive: We're compiling with `.code64` to ensure the assembler uses
71 * the correct 64-bit version of instructions (e.g., `inc`).
72 * - Register Notation:
73 * - Use 64-bit register names (like `%rsi`) for register-indirect addressing to avoid
74 * incorrect address size prefixes.
75 * - It's safe to use `%esi` with `mov` instructions, as the high 32 bits are zeroed
76 * in 64-bit mode.
77 *
78 * IMPORTANT:
79 * Thoroughly test ANY changes to this function in BOTH 32-bit and 64-bit boot environments.
80 */
81
82 .code64
83 .section .text.init_page_table
84 .globl init_page_table
85 .type init_page_table, @function
86
87 init_page_table:
88 mov $0x80000001, %eax
89 cpuid
90 test $(1 << 26), %edx
91 jnz setup_1gb
92
93 setup_2mb:
94 mov $2048, %edi
95 mov $(_PRES + _RW + _US + _PS + _A + _D), %eax
96 mov $0, %ecx
97 mov $main_page_table, %esi
98
99 loop_2mb:
100 movl %eax, (%rsi, %rcx, 8)
101 movl $0, 4(%rsi, %rcx, 8)
102 add $0x200000, %eax
103 inc %ecx
104 cmp %edi, %ecx
105 jb loop_2mb
106
107 mov $4, %edi
108 mov $main_page_table, %eax
109 add $(_PRES + _RW + _US + _A), %eax
110 mov $0, %ecx
111 mov $extra_page_table, %esi
112
113 fill_extra_page_table:
114 movl %eax, (%rsi, %rcx, 8)
115 movl $0, 4(%rsi, %rcx, 8)
116 add $4096, %eax
117 inc %ecx
118 cmp %edi, %ecx
119 jb fill_extra_page_table
120
121 mov $extra_page_table, %eax
122 jmp leave
123
124 setup_1gb:
125 mov $512, %edi
126 mov $(_PRES + _RW + _US + _PS + _A + _D), %eax
127 mov $0, %ebx
128 mov $0, %ecx
129 mov $main_page_table, %esi
130
131 loop_1gb:
132 mov %eax, (%rsi, %rcx, 8)
133 mov %ebx, 4(%rsi, %rcx, 8)
134 add $0x40000000, %eax
135 cmp $0x40000000, %eax
136 ja no_overflow_1gb
137 inc %ebx
138 no_overflow_1gb:
139 inc %ecx
140 cmp %edi, %ecx
141 jb loop_1gb
142
143 mov $main_page_table, %eax
144
145 leave:
146 or $(_PRES + _RW + _US + _A), %eax
147 mov %eax, pml4e
148
149 ret