-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathaarch64.rs
More file actions
236 lines (205 loc) · 7.57 KB
/
aarch64.rs
File metadata and controls
236 lines (205 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use core::{arch::asm, fmt::Write, mem, slice};
use uefi::status::Result;
use crate::{
KernelArgs,
arch::{ENTRY_ADDRESS_MASK, PAGE_ENTRIES, PF_PRESENT, PF_TABLE, PHYS_OFFSET},
logger::LOGGER,
};
use super::super::{OsEfi, memory_map::memory_map};
unsafe fn dump_page_tables(table_phys: u64, table_virt: u64, table_level: u64) {
unsafe {
let entries = slice::from_raw_parts(table_phys as *const u64, PAGE_ENTRIES);
for (i, entry) in entries.iter().enumerate() {
let phys = entry & ENTRY_ADDRESS_MASK;
let flags = entry & !ENTRY_ADDRESS_MASK;
if flags & PF_PRESENT == 0 {
continue;
}
let mut shift = 39u64;
for _ in 0..table_level {
shift -= 9;
print!("\t");
}
let virt = table_virt + (i as u64) << shift;
println!(
"index {} virt {:#x}: phys {:#x} flags {:#x}",
i, virt, phys, flags
);
if table_level < 3 && flags & PF_TABLE == PF_TABLE {
dump_page_tables(phys, virt, table_level + 1);
}
}
}
}
unsafe extern "C" fn kernel_entry(
page_phys: usize,
stack: u64,
func: u64,
args: *const KernelArgs,
) -> ! {
unsafe {
// Read memory map and exit boot services
memory_map().exit_boot_services();
let currentel: u64;
asm!(
"mrs {0}, currentel", // Read current exception level
out(reg) currentel,
);
if currentel == (2 << 2) {
// Need to drop from EL2 to EL1
// Allow access to timers
asm!(
"mrs {0}, cnthctl_el2",
"orr {0}, {0}, #0x3",
"msr cnthctl_el2, {0}",
"msr cntvoff_el2, xzr",
out(reg) _
);
// Initialize ID registers
asm!(
"mrs {0}, midr_el1",
"msr vpidr_el2, {0}",
"mrs {0}, mpidr_el1",
"msr vmpidr_el2, {0}",
out(reg) _
);
// Disable traps
asm!(
"msr cptr_el2, {0}",
"msr hstr_el2, xzr",
in(reg) 0x33FF as u64
);
// Enable floating point
asm!(
"msr cpacr_el1, {0}",
in(reg) (3 << 20) as u64
);
// Set EL1 system control register
asm!(
"msr sctlr_el1, {0}",
in(reg) 0x30d00800 as u64
);
// Set EL1 stack and VBAR
asm!(
"mov {0}, sp",
"msr sp_el1, {0}",
"mrs {0}, vbar_el2",
"msr vbar_el1, {0}",
out(reg) _
);
// Configure execution state of EL1 as aarch64 and disable hypervisor call.
asm!(
"msr hcr_el2, {0}",
in(reg) ((1u64 << 31) | (1u64 << 29)),
);
// Set saved program status register
asm!(
"msr spsr_el2, {0}",
in(reg) 0x3C5 as u64
);
// Switch to EL1
asm!(
"adr {0}, 1f",
"msr elr_el2, {0}",
"eret",
"1:",
out(reg) _
);
} else if currentel == (1 << 2) {
// Already in EL1
} else {
//TODO: what to do if not EL2 or already EL1?
loop {
asm!("wfi");
}
}
// Disable MMU
asm!(
"mrs {0}, sctlr_el1", // Read system control register
"bic {0}, {0}, 1", // Clear MMU enable bit
"msr sctlr_el1, {0}", // Write system control register
"isb", // Instruction sync barrier
out(reg) _,
);
// Set MAIR
// You can think about MAIRs as of an array with 8 elements each of 8 bits long.
// You can store inside MAIRs up to 8 attributes sets and reffer them by the index 0..7 stored in INDX (AttrIndx) field of the table descriptor.
// https://lowenware.com/blog/aarch64-mmu-programming/
// https://developer.arm.com/documentation/102376/0200/Describing-memory-in-AArch64
// https://developer.arm.com/documentation/ddi0595/2021-06/AArch64-Registers/MAIR-EL1--Memory-Attribute-Indirection-Register--EL1-
// Attribute 0 (0xFF) - normal memory, caches are enabled
// Attribute 1 (0x44) - normal memory, caches are disabled. Atomics wouldn't work here if memory doesn't support exclusive access (most real hardware don't)
// Attribute 2 (0x00) - nGnRnE device memory, caches are disabled, gathering, re-ordering, and early write acknowledgement aren't allowed.
asm!(
"msr mair_el1, {0}",
in(reg) 0x00000000000044FF as u64, // MAIR: Arrange for Device, Normal Non-Cache, Normal Write-Back access types
);
// Set TCR
asm!(
"mrs {1}, id_aa64mmfr0_el1", // Read memory model feature register
"bfi {0}, {1}, #32, #3",
"msr tcr_el1, {0}", // Write translation control register
"isb", // Instruction sync barrier
in(reg) 0x1085100510u64, // TCR: (TxSZ, ASID_16, TG1_4K, Cache Attrs, SMP Attrs)
out(reg) _,
);
// Set page tables
asm!(
"dsb sy", // Data sync barrier
"msr ttbr1_el1, {0}", // Set higher half page table
"msr ttbr0_el1, {0}", // Set lower half page table
"isb", // Instruction sync barrier
"dsb ishst", // Data sync barrier, only for stores, and only for inner shareable domain
"tlbi vmalle1is", // Invalidate TLB
"dsb ish", // Dta sync bariar, only for inner shareable domain
"isb", // Instruction sync barrier
in(reg) page_phys,
);
// Enable MMU
asm!(
"mrs {2}, sctlr_el1", // Read system control register
"bic {2}, {2}, {0}", // Clear bits
"orr {2}, {2}, {1}", // Set bits
"msr sctlr_el1, {2}", // Write system control register
"isb", // Instruction sync barrier
in(reg) 0x32802c2u64, // Clear SCTLR bits: (EE, EOE, IESB, WXN, UMA, ITD, THEE, A)
in(reg) 0x3485d13du64, // Set SCTLR bits: (LSMAOE, nTLSMD, UCI, SPAN, nTWW, nTWI, UCT, DZE, I, SED, SA0, SA, C, M, CP15BEN)
out(reg) _,
);
// Set stack
asm!("mov sp, {}", in(reg) stack);
// Call kernel entry
let entry_fn: extern "C" fn(*const KernelArgs) -> ! = mem::transmute(func);
entry_fn(args);
}
}
pub fn main() -> Result<()> {
LOGGER.init();
let mut os = OsEfi::new();
// Disable cursor
let _ = (os.st.ConsoleOut.EnableCursor)(os.st.ConsoleOut, false);
let currentel: u64;
unsafe {
asm!(
"mrs {0}, currentel", // Read current exception level
out(reg) currentel,
);
}
log::info!("Currently in EL{}", (currentel >> 2) & 3);
let (page_phys, func, args) = crate::main(&mut os);
unsafe {
let stack = args.stack_base + args.stack_size + PHYS_OFFSET;
// dump_page_tables(page_phys as _, 0, 0);
println!(
"kernel_entry({:#x}, {:#x}, {:#x}, {:p})",
page_phys, stack, func, &args
);
println!("{:#x?}", args);
kernel_entry(page_phys, stack, func, &args);
}
}
pub fn disable_interrupts() {
unsafe {
asm!("msr daifset, #2");
}
}