diff --git a/kernel-rs/multiboot2-elf64 b/kernel-rs/multiboot2-elf64 index 4425c197..73530c41 160000 --- a/kernel-rs/multiboot2-elf64 +++ b/kernel-rs/multiboot2-elf64 @@ -1 +1 @@ -Subproject commit 4425c197f10d5bcada47eabb8b2f93a8e2422937 +Subproject commit 73530c41902a6991ca54ad6f12d05519d4870403 diff --git a/kernel-rs/src/arch/x86/boot.asm b/kernel-rs/src/arch/x86/boot.asm index 1be8474b..f2fab7b6 100644 --- a/kernel-rs/src/arch/x86/boot.asm +++ b/kernel-rs/src/arch/x86/boot.asm @@ -12,12 +12,9 @@ start: call check_multiboot call set_up_page_tables - call enable_pse - ; call enable_paging ; load the new gdt lgdt [GDTR.ptr] - jmp GDTR.gdt_cs:x86_start check_multiboot: @@ -34,42 +31,13 @@ set_up_page_tables: or eax, 0b11 ; present + writable mov [p2_table + 1023 * 4], eax - ; map each P2 entry to a huge 4MiB page - mov ecx, 0 ; counter variable + ; map ecx-th P2 entry to a huge page that starts at address 2MiB*ecx + mov eax, 0b10000011 ; huge + present + writable + mov [p2_table], eax ; map ecx-th entry -.map_p2_table: - ; map ecx-th P2 entry to a huge page that starts at address 2MiB*ecx - mov eax, 0x400000 ; 4MiB - mul ecx ; start address of ecx-th page - or eax, 0b10000011 ; huge + present + writable - mov [p2_table + ecx * 4], eax ; map ecx-th entry - - inc ecx ; increase counter - cmp ecx, 20 ; if counter == 1023, the whole P2 table is mapped - jne .map_p2_table ; else map the next entry - - mov eax, p2_table - mov cr3, eax - ret - -; PSE (Page Size Extension) allows huge pages to exist -enable_pse: - ; enable PSE in the cr4 register - mov eax, cr4 - or eax, 1 << 4 - mov cr4, eax - - ret - -enable_paging: - ; load P2 to cr3 register (cpu uses this to access the P2 table) - - ; enable paging in the cr0 register - mov eax, cr0 - or eax, 1 << 31 - mov cr0, eax - - ret + mov eax, p2_table + mov cr3, eax + ret error: mov dword [0xb8000], 0x4f524f45 @@ -84,9 +52,9 @@ HALT: section .bss align 4096 p2_table: - resb 4096 + resb 4096 stack_bottom: - resb 4096 * 8 + resb 4096 * 8 stack_top: section .gdt diff --git a/kernel-rs/src/cpuio.rs b/kernel-rs/src/cpuio.rs index ffd6ca9b..57575a17 100644 --- a/kernel-rs/src/cpuio.rs +++ b/kernel-rs/src/cpuio.rs @@ -38,9 +38,10 @@ pub fn outl(port: u16, value: u32) { unsafe {asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile")}; } +/// Halt system pub fn halt() -> ! { - unsafe {asm!("cli")};//TODO sure here ? + unsafe {asm!("cli" : : : : "volatile")}; loop { - unsafe {asm!("hlt")}; //TODO volatile ????? + unsafe {asm!("hlt" : : : : "volatile")}; } } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 83e48b4e..c92dfc7c 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -40,24 +40,12 @@ fn init_kernel(multiboot_info_addr: usize) -> Result <(), &'static str> { else { acpi::init()?; } - enable_paging(); - enable_write_protect_bit(); memory::init(&boot_info); vga::init(); Ok(()) } -fn enable_paging() { - use x86::registers::control::{Cr0, Cr0Flags}; - unsafe { Cr0::write(Cr0::read() | Cr0Flags::PAGING) }; -} - -fn enable_write_protect_bit() { - use x86::registers::control::{Cr0, Cr0Flags}; - unsafe { Cr0::write(Cr0::read() | Cr0Flags::WRITE_PROTECT) }; -} - #[no_mangle] pub extern fn kmain(multiboot_info_addr: usize) -> ! { if let Err(msg) = init_kernel(multiboot_info_addr) { diff --git a/kernel-rs/src/memory/mod.rs b/kernel-rs/src/memory/mod.rs index c2a6c244..7294cce2 100644 --- a/kernel-rs/src/memory/mod.rs +++ b/kernel-rs/src/memory/mod.rs @@ -18,6 +18,10 @@ pub trait FrameAllocator { /// memory initialisation should only be called once pub fn init(boot_info: &multiboot2::BootInformation) { + use x86::registers::control::{Cr0, Cr4, Cr0Flags, Cr4Flags}; + Cr4::add(Cr4Flags::PSE); + Cr0::add(Cr0Flags::PAGING | Cr0Flags::WRITE_PROTECT); + let elf_sections_tag = boot_info.elf_sections_tag().unwrap(); let memory_map_tag = boot_info.memory_map_tag().unwrap(); @@ -33,7 +37,7 @@ pub fn init(boot_info: &multiboot2::BootInformation) { let mut frame_allocator = self::AreaFrameAllocator::new( kernel_start as usize, kernel_end as usize, - boot_info.start_address(), boot_info.start_address(), + boot_info.start_address(), boot_info.end_address(), memory_map_tag.memory_areas()); let mut active_table = paging::remap_the_kernel(&mut frame_allocator, diff --git a/kernel-rs/src/memory/paging/mapper.rs b/kernel-rs/src/memory/paging/mapper.rs index 408149ee..319f8827 100644 --- a/kernel-rs/src/memory/paging/mapper.rs +++ b/kernel-rs/src/memory/paging/mapper.rs @@ -37,7 +37,6 @@ impl Mapper { let offset = virtual_address.as_u32() % PAGE_SIZE as u32; self.translate_page(Page::containing_address(virtual_address)) .map(|frame| frame.start_address() + offset) - } /// virtual page to physical frame translation @@ -50,13 +49,13 @@ impl Mapper { if p2_entry.flags().contains(PageTableFlags::HUGE_PAGE) { // TODO 4MiB alignment check return Some(start_frame + u32::from(page.p1_index())); - } + } } None - }; + }; p1.and_then(|p1| p1[page.p1_index()].pointed_frame()) - .or_else(huge_page) + .or_else(huge_page) } /// map a virtual page to a physical frame in the page tables diff --git a/kernel-rs/src/memory/paging/table.rs b/kernel-rs/src/memory/paging/table.rs index 16060b05..b390b99a 100644 --- a/kernel-rs/src/memory/paging/table.rs +++ b/kernel-rs/src/memory/paging/table.rs @@ -8,9 +8,9 @@ pub trait RecTable fn next_table(&self, index: usize) -> Option<&PageTable>; fn next_table_mut(&mut self, index: usize) -> Option<&mut PageTable>; fn next_table_create(&mut self, - index: usize, - allocator: &mut A) - -> &mut PageTable; + index: usize, + allocator: &mut A) + -> &mut PageTable; } impl RecTable for PageTable @@ -36,8 +36,8 @@ impl RecTable for PageTable } fn next_table_create(&mut self, - index: usize, - allocator: &mut A) -> &mut PageTable + index: usize, + allocator: &mut A) -> &mut PageTable where A: FrameAllocator { if self.next_table(index).is_none() { diff --git a/kernel-rs/src/memory/paging/temporary_page.rs b/kernel-rs/src/memory/paging/temporary_page.rs index 33756ba0..c7e9b094 100644 --- a/kernel-rs/src/memory/paging/temporary_page.rs +++ b/kernel-rs/src/memory/paging/temporary_page.rs @@ -18,17 +18,17 @@ impl TemporaryPage { } } - /// Maps the temporary page to the given frame in the active table. + /// Maps the temporary page to the given frame in the active table. /// Returns the start address of the temporary page. pub fn map(&mut self, frame: PhysFrame, active_table: &mut ActivePageTable) -> VirtAddr { assert!(active_table.translate_page(self.page).is_none(), - "temporary page is already mapped"); + "temporary page is already mapped"); active_table.map_to(self.page, frame, PageTableFlags::WRITABLE, &mut self.allocator); // this kind of check should be done in a test routine assert!(active_table.translate_page(self.page).is_some(), - "temporary page was not mapped"); + "temporary page was not mapped"); self.page.start_address() } @@ -37,14 +37,14 @@ impl TemporaryPage { active_table.unmap(self.page, &mut self.allocator) } - /// Maps the temporary page to the given page table frame in the active + /// Maps the temporary page to the given page table frame in the active /// table. Returns a reference to the now mapped table. pub fn map_table_frame(&mut self, - frame: PhysFrame, - active_table: &mut ActivePageTable) - -> &mut PageTable { - unsafe { &mut *(self.map(frame, active_table).as_u32() as *mut PageTable) } - } + frame: PhysFrame, + active_table: &mut ActivePageTable) + -> &mut PageTable { + unsafe { &mut *(self.map(frame, active_table).as_u32() as *mut PageTable) } + } } struct TinyAllocator([Option; 1]); diff --git a/kernel-rs/src/vga/mod.rs b/kernel-rs/src/vga/mod.rs index 26013c44..dc87c0d9 100644 --- a/kernel-rs/src/vga/mod.rs +++ b/kernel-rs/src/vga/mod.rs @@ -214,7 +214,5 @@ pub fn init() { format_args!("{: ^80}", r#" | : ;| : .' "#), format_args!("{: ^80}", r#" ' ,/ ; | .' "#), format_args!("{: ^80}", r#" '--' `---' "#)); - set_color!(); unsafe { VGA.prompt(); } - unsafe { VGA.flush(); } } diff --git a/kernel-rs/x86 b/kernel-rs/x86 index eae47083..7b8227e3 160000 --- a/kernel-rs/x86 +++ b/kernel-rs/x86 @@ -1 +1 @@ -Subproject commit eae470839b1ff232dbc4af5389e9a0b4fffe4b30 +Subproject commit 7b8227e36afe68ed2d518a2c7cede0466878ca9d