-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.rs
397 lines (344 loc) · 9.33 KB
/
init.rs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#![no_std]
#![no_main]
#![feature(naked_functions)]
#![feature(vec_push_within_capacity)]
#![feature(fn_align)]
mod abi;
mod fdt;
extern crate alloc;
use alloc::vec::Vec;
use fdt::Fdt;
mod panic_alloc {
struct PanicAllocator;
use alloc::alloc::GlobalAlloc;
use alloc::alloc::Layout;
unsafe impl GlobalAlloc for PanicAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
panic!("Must use allocator_api");
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
panic!("Must use allocator api");
}
}
#[global_allocator]
static GLOBAL: PanicAllocator = PanicAllocator;
}
#[naked]
#[no_mangle]
#[link_section = ".text._start"]
#[repr(align(4096))]
unsafe extern "C" fn _start() {
core::arch::naked_asm!(
"nop",
".align 11",
"1:",
"nop",
"j 1b",
".align 12",
"addi sp, sp, -16",
"tail {}",
sym rust_start,
);
}
fn syscall(
a0: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a7: abi::Syscall,
) -> Result<(), abi::SysError> {
let ret: usize;
unsafe {
core::arch::asm!(
"ecall",
lateout("a0") ret,
in("a0") a0,
in("a1") a1,
in("a2") a2,
in("a3") a3,
in("a4") a4,
in("a5") a5,
in("a7") -(a7.0 as isize),
clobber_abi("C")
);
}
match ret {
0 => Ok(()),
e => Err(unsafe { core::mem::transmute(e) }),
}
}
#[inline(never)]
fn linux_sys_exit_group() {
unsafe {
//core::arch::asm!("ecall", in("a7") 94);
core::arch::asm!("c.slli a7, 2", "c.jalr a7", in("a7") 94);
}
}
fn switch(interval: usize) {}
const PAGE_SIZE: usize = 0x1000;
const PAGE_SHIFT: usize = 12;
#[repr(transparent)]
struct CapAddr(usize);
#[repr(transparent)]
struct NullCap(CapAddr);
#[repr(transparent)]
struct MemCap(CapAddr);
#[repr(transparent)]
struct TaskCap(CapAddr);
impl MemCap {
const SPLIT: usize = 3;
fn split(self, dest: NullCap, line: usize) -> Result<(Self, Self), abi::SysError> {
syscall(self.0 .0, dest.0 .0, line, 0, 0, 0, abi::Syscall::MemSplit)?;
Ok((self, MemCap(dest.0)))
}
}
fn cap_id(addr: usize) {
syscall(addr, 0, 0, 0, 0, 0, abi::Syscall::CapIdentify);
}
impl TaskCap {
fn map_mem(
&self,
mem: MemCap,
addr: usize,
level: usize,
prot: abi::Prot,
) -> Result<NullCap, abi::SysError> {
syscall(
self.0 .0,
mem.0 .0,
addr,
level,
prot as usize,
0,
abi::Syscall::TaskMapMem,
)
.map(|_| NullCap(mem.0))
}
fn unmap_mem(&self, mem: NullCap, addr: usize, level: usize) -> Result<MemCap, abi::SysError> {
syscall(
self.0 .0,
mem.0 .0,
addr,
level,
0,
0,
abi::Syscall::TaskUnmapMem,
)
.map(|_| MemCap(mem.0))
}
fn map_cap(&self, mem: MemCap, addr: usize, level: usize) {
todo!();
}
fn unmap_cap(&self, mem: NullCap, addr: usize, level: usize) -> MemCap {
todo!();
}
}
unsafe fn parse_initial_total_size(ptr: *const ()) -> u32 {
u32::from_be(core::ptr::read((ptr as *const u32).add(1)))
}
extern "C" fn rust_start(fdt: usize) {
eprintln!("Hello, World!");
println!("Beep Boop, I'm a computer!");
use core::fmt::Write;
Con.write_str("Sup!\n").unwrap();
let task = TaskCap(CapAddr(0));
eprintln!("fdt: {:x}", fdt);
let init_mem = MemCap(CapAddr(1));
let rest_mem = NullCap(CapAddr(2));
// the fdt address is 8 bytes aligned (according to the device tree spec)
// thus the totalsize and magic will not stradle two pages since they are
// both 4 byte fields
let fdt_aligned_base = fdt & !(PAGE_SIZE - 1);
let fdt_aligned_offset = fdt & (PAGE_SIZE - 1);
eprintln!("rest_mem:");
cap_id(3);
eprintln!("init_mem:");
cap_id(2);
let (init_mem, rest_mem) = init_mem.split(rest_mem, fdt_aligned_base).unwrap();
let fdt_start_mem = NullCap(CapAddr(3));
let (fdt_start_mem, rest_mem) = rest_mem.split(fdt_start_mem, PAGE_SIZE).unwrap();
cap_id(0);
cap_id(1);
cap_id(2);
cap_id(3);
cap_id(4);
let mut rest_mem = rest_mem;
let fdt = unsafe {
let null_cap = task
.map_mem(fdt_start_mem, 0x200000 - 4096, 52, abi::Prot::Read)
.unwrap();
println!("eee");
let ptr = ((0x200000 - 4096) as *const ()).byte_add(fdt_aligned_offset);
let size = parse_initial_total_size(ptr) as usize;
println!("{}", size);
let size_aligned = (size + fdt_aligned_offset + 4096) & !(4095);
println!("{}", size_aligned);
let mem_cap = task.unmap_mem(null_cap, 0x200000 - 4096, 52).unwrap();
let mut null_cap = task
.map_mem(mem_cap, 0x200000 - size_aligned, 52, abi::Prot::Read)
.unwrap();
let mut written = PAGE_SIZE;
while written < size_aligned {
let (fdt_page, rest) = rest_mem.split(null_cap, PAGE_SIZE).unwrap();
rest_mem = rest;
null_cap = task
.map_mem(
fdt_page,
0x200000 - size_aligned + written,
52,
abi::Prot::Read,
)
.unwrap();
written += 4096
}
let ptr =
((0x200000 - size_aligned) as *const ()).byte_add(fdt_aligned_offset) as *const u8;
eprintln!("mapped whole fdt at addr {:p}", ptr);
core::slice::from_raw_parts(ptr, size)
};
let fdt = Fdt::new(fdt);
eprintln!("{:#?}", fdt);
/*
for mem in fdt.root().get_all("memory") {
println!("{:#?}", mem);
}
let chosen = fdt.chosen();
eprintln!("{:#?}", chosen);
let reserved = fdt.get_node("/reserved-memory").unwrap();
eprintln!("{:#?}", reserved);
for node in reserved.nodes() {
println!("{:#?}", node);
let reg = node.get_prop("reg").unwrap();
let a: &[u8; 8] = reg[0..8].try_into().unwrap();
let a = usize::from_be_bytes(a.clone());
let b: &[u8; 8] = reg[8..16].try_into().unwrap();
let b = usize::from_be_bytes(b.clone());
println!("{:16x?}", a);
println!("{:16x?}", b);
}
*/
panic!();
// TODO: page frame allocator
// TODO: create a new thread
// switch to that thread
// linux_sys_exit_group();
// let mut v = Vec::<u8>::new();
// v.try_reserve(1).unwrap();
// v.push_within_capacity(1).unwrap();
// eprintln!("{:?}", v);
loop {}
}
pub struct Con;
impl core::fmt::Write for Con {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
fn write_vals(vals: [usize; 6]) -> core::fmt::Result {
unsafe {
syscall(
vals[0],
vals[1],
vals[2],
vals[3],
vals[4],
vals[5],
abi::Syscall::ConWrite,
)
.or(Err(core::fmt::Error))
}
}
let mut idx = 0;
let mut vals = [0; 6];
let mut shift = 0;
for b in s.bytes() {
vals[idx] |= (b as usize) << shift;
shift += 8;
if shift == 64 {
shift = 0;
idx += 1;
}
if idx == 4 {
idx = 0;
write_vals(vals)?;
vals = [0; 6];
}
}
if idx != 0 || shift != 0 {
write_vals(vals)?;
}
Ok(())
}
}
pub fn _print(args: core::fmt::Arguments) {
use core::fmt::Write;
Con.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! eprint {
() => ();
($($arg:tt)*) => ($crate::print!("\x1B[91m{}\x1B[0m", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! eprintln {
() => ($crate::eprint!("\n"));
($($arg:tt)*) => ($crate::eprint!("{}\n", format_args!($($arg)*)));
}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
eprintln!("{}", info);
loop {}
}
struct CapAlloc {
brk: usize,
}
enum AllocError {}
impl CapAlloc {
fn map_more(inc: usize) {}
fn map_less(dec: usize) {}
}
/*
struct PageVec<T> {
ptr: NonZero<T>,
capacity: usize,
len: usize
}
impl PageVec<T> {
fn try_push(&mut self, t: T) -> Result<(), ()> {
if self.len < self.capacity {
unsafe {
ptr.add(self.len).write(t);
}
self.len += 1;
Ok(())
} else {
Err(())
}
}
fn grow(&mut self, mem: MemCap) {
}
fn shrink(&mut self) -> MemCap {
}
}
struct CapHeap {
let free_list: PageVec<usize>
}
impl CapHeap {
fn alloc(&mut self) -> NullCap {
}
}
*/
// struct PageAlloc<'a> {
// }
// given a list of regions
// split regions up into power of two sections
// initialise seperate buddys for each section
// have agregate allocator object