3 * Copyright 2024 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * For reference see "AMD64 Architecture Programmer's Manual Volume 2",
31 * Document 24593-Rev. 3.31-July 2019 Chapter 5.3.4
33 * Page table attributes: WB, User+Supervisor, Present, Writeable, Accessed, Dirty
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)
50 .section .bss.main_page_table
51 .global main_page_table
56 .section .bss.extra_page_table
57 .global extra_page_table
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.
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
79 * Thoroughly test ANY changes to this function in BOTH 32-bit and 64-bit boot environments.
83 .section .text.init_page_table
84 .globl init_page_table
85 .type init_page_table, @function
95 mov $(_PRES + _RW + _US + _PS + _A + _D), %eax
97 mov $main_page_table, %esi
100 movl %eax, (%rsi, %rcx, 8)
101 movl $0, 4(%rsi, %rcx, 8)
108 mov $main_page_table, %eax
109 add $(_PRES + _RW + _US + _A), %eax
111 mov $extra_page_table, %esi
113 fill_extra_page_table:
114 movl %eax, (%rsi, %rcx, 8)
115 movl $0, 4(%rsi, %rcx, 8)
119 jb fill_extra_page_table
121 mov $extra_page_table, %eax
126 mov $(_PRES + _RW + _US + _PS + _A + _D), %eax
129 mov $main_page_table, %esi
132 mov %eax, (%rsi, %rcx, 8)
133 mov %ebx, 4(%rsi, %rcx, 8)
134 add $0x40000000, %eax
135 cmp $0x40000000, %eax
143 mov $main_page_table, %eax
146 or $(_PRES + _RW + _US + _A), %eax