Skip to content

Commit

Permalink
Refactor identity paging
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgian committed Jun 30, 2023
1 parent b7fa004 commit 9b2a49b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
14 changes: 1 addition & 13 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,7 @@ pub extern "C" fn _start() -> ! {

//setup paging
unsafe {
let table0 = PageTable::new(0x0);
let table1 = PageTable::new(0x0040_0000);
let table2 = PageTable::new(0x0080_0000);
let table3 = PageTable::new(0x00C0_0000);
//let table2 = PageTable::test();
PAGING.set_table(0, &table0);
PAGING.set_table(1, &table1);
PAGING.set_table(2, &table2);
PAGING.set_table(3, &table3);

let table = PageTable::new(0x00C0_0000);
PAGING.set_table(4, &table);

PAGING.identity();
PAGING.enable();
}

Expand Down
25 changes: 19 additions & 6 deletions kernel/src/memory/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub static mut PAGING: PageDirectory = PageDirectory {
entries: [0x00000002; 1024],
};

pub static mut TABLES: [PageTable; 16] = [NULL_TABLE; 16];

pub static NULL_TABLE: PageTable = PageTable {
entries: [0; 1024],
};

#[repr(align(4096))]
pub struct PageDirectory {
pub entries: [u32; 1024],
Expand All @@ -26,22 +32,29 @@ impl PageDirectory {
in("eax") address);
}
}

//indentity page first 16MiB
pub fn identity(&mut self) {
unsafe {
for i in 0..4 {
TABLES[i].set((0x0040_0000 * i) as u32);
PAGING.set_table(i, &TABLES[i]);
}
}
}
}

#[derive(Copy, Clone, Debug)]
#[repr(align(4096))]
pub struct PageTable {
pub entries: [u32; 1024],
}

impl PageTable {
pub fn new(from: u32) -> Self {
let mut table = Self { entries: [0; 1024] };

pub fn set(&mut self, from: u32) {
for i in 0..1024 {
//0b011 (supervisor, write, present)
table.entries[i] = (((i * 0x1000) + from as usize) | 0b011) as u32;
self.entries[i] = (((i * 0x1000) + from as usize) | 0b011) as u32;
}

table
}
}

0 comments on commit 9b2a49b

Please sign in to comment.