From 559a90ad8b901f6068fac5c8624873aafe6025bb Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Tue, 20 Jun 2017 17:55:33 +0200 Subject: [PATCH] Format using rustfmt-nightly --- src/interrupts/mod.rs | 52 ++++++++++++++++++----------- src/memory/area_frame_allocator.rs | 19 ++++++----- src/memory/mod.rs | 46 +++++++++++++++---------- src/memory/paging/entry.rs | 4 ++- src/memory/paging/mapper.rs | 25 +++++++------- src/memory/paging/mod.rs | 50 ++++++++++++++++----------- src/memory/paging/table.rs | 36 ++++++++++++-------- src/memory/paging/temporary_page.rs | 21 +++++++----- src/memory/stack_allocator.rs | 11 +++--- src/vga_buffer.rs | 15 ++++----- 10 files changed, 168 insertions(+), 111 deletions(-) diff --git a/src/interrupts/mod.rs b/src/interrupts/mod.rs index 13a5253e..c4da624a 100644 --- a/src/interrupts/mod.rs +++ b/src/interrupts/mod.rs @@ -43,14 +43,16 @@ pub fn init(memory_controller: &mut MemoryController) { use x86_64::instructions::tables::load_tss; use x86_64::VirtualAddress; - let double_fault_stack = - memory_controller.alloc_stack(1).expect("could not allocate double fault stack"); + let double_fault_stack = memory_controller + .alloc_stack(1) + .expect("could not allocate double fault stack"); let tss = TSS.call_once(|| { - let mut tss = TaskStateSegment::new(); - tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] = VirtualAddress(double_fault_stack.top()); - tss - }); + let mut tss = TaskStateSegment::new(); + tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] = + VirtualAddress(double_fault_stack.top()); + tss + }); let mut code_selector = SegmentSelector(0); let mut tss_selector = SegmentSelector(0); @@ -78,29 +80,41 @@ extern "x86-interrupt" fn divide_by_zero_handler(stack_frame: &mut ExceptionStac } extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut ExceptionStackFrame) { - println!("\nEXCEPTION: BREAKPOINT at {:#x}\n{:#?}", - stack_frame.instruction_pointer, - stack_frame); + println!( + "\nEXCEPTION: BREAKPOINT at {:#x}\n{:#?}", + stack_frame.instruction_pointer, + stack_frame + ); } extern "x86-interrupt" fn invalid_opcode_handler(stack_frame: &mut ExceptionStackFrame) { - println!("\nEXCEPTION: INVALID OPCODE at {:#x}\n{:#?}", - stack_frame.instruction_pointer, - stack_frame); + println!( + "\nEXCEPTION: INVALID OPCODE at {:#x}\n{:#?}", + stack_frame.instruction_pointer, + stack_frame + ); loop {} } -extern "x86-interrupt" fn page_fault_handler(stack_frame: &mut ExceptionStackFrame, error_code: PageFaultErrorCode) { +extern "x86-interrupt" fn page_fault_handler( + stack_frame: &mut ExceptionStackFrame, + error_code: PageFaultErrorCode, +) { use x86_64::registers::control_regs; - println!("\nEXCEPTION: PAGE FAULT while accessing {:#x}\nerror code: \ - {:?}\n{:#?}", - control_regs::cr2(), - error_code, - stack_frame); + println!( + "\nEXCEPTION: PAGE FAULT while accessing {:#x}\nerror code: \ + {:?}\n{:#?}", + control_regs::cr2(), + error_code, + stack_frame + ); loop {} } -extern "x86-interrupt" fn double_fault_handler(stack_frame: &mut ExceptionStackFrame, _error_code: u64) { +extern "x86-interrupt" fn double_fault_handler( + stack_frame: &mut ExceptionStackFrame, + _error_code: u64, +) { println!("\nEXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); loop {} } diff --git a/src/memory/area_frame_allocator.rs b/src/memory/area_frame_allocator.rs index 60269c1f..2afa3130 100644 --- a/src/memory/area_frame_allocator.rs +++ b/src/memory/area_frame_allocator.rs @@ -26,12 +26,13 @@ pub struct AreaFrameAllocator { } impl AreaFrameAllocator { - pub fn new(kernel_start: usize, - kernel_end: usize, - multiboot_start: usize, - multiboot_end: usize, - memory_areas: MemoryAreaIter) - -> AreaFrameAllocator { + pub fn new( + kernel_start: usize, + kernel_end: usize, + multiboot_start: usize, + multiboot_end: usize, + memory_areas: MemoryAreaIter, + ) -> AreaFrameAllocator { let mut allocator = AreaFrameAllocator { next_free_frame: Frame::containing_address(0), current_area: None, @@ -49,9 +50,9 @@ impl AreaFrameAllocator { self.current_area = self.areas .clone() .filter(|area| { - let address = area.base_addr + area.length - 1; - Frame::containing_address(address as usize) >= self.next_free_frame - }) + let address = area.base_addr + area.length - 1; + Frame::containing_address(address as usize) >= self.next_free_frame + }) .min_by_key(|area| area.base_addr); if let Some(area) = self.current_area { diff --git a/src/memory/mod.rs b/src/memory/mod.rs index e1355735..c903a771 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -23,31 +23,41 @@ pub fn init(boot_info: &BootInformation) -> MemoryController { assert_has_not_been_called!("memory::init must be called only once"); let memory_map_tag = boot_info.memory_map_tag().expect("Memory map tag required"); - let elf_sections_tag = boot_info.elf_sections_tag().expect("Elf sections tag required"); + let elf_sections_tag = boot_info + .elf_sections_tag() + .expect("Elf sections tag required"); - let kernel_start = elf_sections_tag.sections() + let kernel_start = elf_sections_tag + .sections() .filter(|s| s.is_allocated()) .map(|s| s.addr) .min() .unwrap(); - let kernel_end = elf_sections_tag.sections() + let kernel_end = elf_sections_tag + .sections() .filter(|s| s.is_allocated()) .map(|s| s.addr + s.size) .max() .unwrap(); - println!("kernel start: {:#x}, kernel end: {:#x}", - kernel_start, - kernel_end); - println!("multiboot start: {:#x}, multiboot end: {:#x}", - boot_info.start_address(), - boot_info.end_address()); + println!( + "kernel start: {:#x}, kernel end: {:#x}", + kernel_start, + kernel_end + ); + println!( + "multiboot start: {:#x}, multiboot end: {:#x}", + boot_info.start_address(), + boot_info.end_address() + ); - let mut frame_allocator = AreaFrameAllocator::new(kernel_start as usize, - kernel_end as usize, - boot_info.start_address(), - boot_info.end_address(), - memory_map_tag.memory_areas()); + let mut frame_allocator = AreaFrameAllocator::new( + kernel_start as usize, + kernel_end as usize, + boot_info.start_address(), + boot_info.end_address(), + memory_map_tag.memory_areas(), + ); let mut active_table = paging::remap_the_kernel(&mut frame_allocator, boot_info); @@ -83,9 +93,11 @@ pub struct MemoryController { impl MemoryController { pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option { - let &mut MemoryController { ref mut active_table, - ref mut frame_allocator, - ref mut stack_allocator } = self; + let &mut MemoryController { + ref mut active_table, + ref mut frame_allocator, + ref mut stack_allocator, + } = self; stack_allocator.alloc_stack(active_table, frame_allocator, size_in_pages) } } diff --git a/src/memory/paging/entry.rs b/src/memory/paging/entry.rs index 5a34e273..9e3ba4e2 100644 --- a/src/memory/paging/entry.rs +++ b/src/memory/paging/entry.rs @@ -27,7 +27,9 @@ impl Entry { pub fn pointed_frame(&self) -> Option { if self.flags().contains(PRESENT) { - Some(Frame::containing_address(self.0 as usize & 0x000fffff_fffff000)) + Some(Frame::containing_address( + self.0 as usize & 0x000fffff_fffff000, + )) } else { None } diff --git a/src/memory/paging/mapper.rs b/src/memory/paging/mapper.rs index 5f542e3c..4c5ef311 100644 --- a/src/memory/paging/mapper.rs +++ b/src/memory/paging/mapper.rs @@ -32,11 +32,8 @@ impl Mapper { pub fn translate(&self, virtual_address: VirtualAddress) -> Option { let offset = virtual_address % PAGE_SIZE; - self.translate_page(Page::containing_address(virtual_address)).map(|frame| { - frame.number * - PAGE_SIZE + - offset - }) + self.translate_page(Page::containing_address(virtual_address)) + .map(|frame| frame.number * PAGE_SIZE + offset) } pub fn translate_page(&self, page: Page) -> Option { @@ -51,9 +48,9 @@ impl Mapper { // address must be 1GiB aligned assert!(start_frame.number % (ENTRY_COUNT * ENTRY_COUNT) == 0); return Some(Frame { - number: start_frame.number + page.p2_index() * ENTRY_COUNT + - page.p1_index(), - }); + number: start_frame.number + page.p2_index() * ENTRY_COUNT + + page.p1_index(), + }); } } if let Some(p2) = p3.next_table(page.p3_index()) { @@ -78,7 +75,8 @@ impl Mapper { } pub fn map_to(&mut self, page: Page, frame: Frame, flags: EntryFlags, allocator: &mut A) - where A: FrameAllocator + where + A: FrameAllocator, { let mut p3 = self.p4_mut().next_table_create(page.p4_index(), allocator); let mut p2 = p3.next_table_create(page.p3_index(), allocator); @@ -89,21 +87,24 @@ impl Mapper { } pub fn map(&mut self, page: Page, flags: EntryFlags, allocator: &mut A) - where A: FrameAllocator + where + A: FrameAllocator, { let frame = allocator.allocate_frame().expect("out of memory"); self.map_to(page, frame, flags, allocator) } pub fn identity_map(&mut self, frame: Frame, flags: EntryFlags, allocator: &mut A) - where A: FrameAllocator + where + A: FrameAllocator, { let page = Page::containing_address(frame.start_address()); self.map_to(page, frame, flags, allocator) } pub fn unmap(&mut self, page: Page, allocator: &mut A) - where A: FrameAllocator + where + A: FrameAllocator, { use x86_64::VirtualAddress; use x86_64::instructions::tlb; diff --git a/src/memory/paging/mod.rs b/src/memory/paging/mod.rs index ba23c269..91358528 100644 --- a/src/memory/paging/mod.rs +++ b/src/memory/paging/mod.rs @@ -31,9 +31,11 @@ pub struct Page { impl Page { pub fn containing_address(address: VirtualAddress) -> Page { - assert!(address < 0x0000_8000_0000_0000 || address >= 0xffff_8000_0000_0000, - "invalid address: 0x{:x}", - address); + assert!( + address < 0x0000_8000_0000_0000 || address >= 0xffff_8000_0000_0000, + "invalid address: 0x{:x}", + address + ); Page { number: address / PAGE_SIZE } } @@ -113,11 +115,13 @@ impl ActivePageTable { ActivePageTable { mapper: Mapper::new() } } - pub fn with(&mut self, - table: &mut InactivePageTable, - temporary_page: &mut temporary_page::TemporaryPage, // new - f: F) - where F: FnOnce(&mut Mapper) + pub fn with( + &mut self, + table: &mut InactivePageTable, + temporary_page: &mut temporary_page::TemporaryPage, // new + f: F, + ) where + F: FnOnce(&mut Mapper), { use x86_64::registers::control_regs; use x86_64::instructions::tlb; @@ -162,10 +166,11 @@ pub struct InactivePageTable { } impl InactivePageTable { - pub fn new(frame: Frame, - active_table: &mut ActivePageTable, - temporary_page: &mut TemporaryPage) - -> InactivePageTable { + pub fn new( + frame: Frame, + active_table: &mut ActivePageTable, + temporary_page: &mut TemporaryPage, + ) -> InactivePageTable { { let table = temporary_page.map_table_frame(frame.clone(), active_table); table.zero(); @@ -178,7 +183,8 @@ impl InactivePageTable { } pub fn remap_the_kernel(allocator: &mut A, boot_info: &BootInformation) -> ActivePageTable - where A: FrameAllocator +where + A: FrameAllocator, { let mut temporary_page = TemporaryPage::new(Page { number: 0xcafebabe }, allocator); @@ -189,7 +195,9 @@ pub fn remap_the_kernel(allocator: &mut A, boot_info: &BootInformation) -> Ac }; active_table.with(&mut new_table, &mut temporary_page, |mapper| { - let elf_sections_tag = boot_info.elf_sections_tag().expect("Memory map tag required"); + let elf_sections_tag = boot_info + .elf_sections_tag() + .expect("Memory map tag required"); // identity map the allocated kernel sections for section in elf_sections_tag.sections() { @@ -198,11 +206,15 @@ pub fn remap_the_kernel(allocator: &mut A, boot_info: &BootInformation) -> Ac continue; } - assert!(section.addr as usize % PAGE_SIZE == 0, - "sections need to be page aligned"); - println!("mapping section at addr: {:#x}, size: {:#x}", - section.addr, - section.size); + assert!( + section.addr as usize % PAGE_SIZE == 0, + "sections need to be page aligned" + ); + println!( + "mapping section at addr: {:#x}, size: {:#x}", + section.addr, + section.size + ); let flags = EntryFlags::from_elf_section_flags(section); diff --git a/src/memory/paging/table.rs b/src/memory/paging/table.rs index fdab3efc..ec370894 100644 --- a/src/memory/paging/table.rs +++ b/src/memory/paging/table.rs @@ -21,7 +21,8 @@ pub struct Table { } impl Table - where L: TableLevel +where + L: TableLevel, { pub fn zero(&mut self) { for entry in self.entries.iter_mut() { @@ -31,7 +32,8 @@ impl Table } impl Table - where L: HierarchicalLevel +where + L: HierarchicalLevel, { fn next_table_address(&self, index: usize) -> Option { let entry_flags = self[index].flags(); @@ -44,22 +46,28 @@ impl Table } pub fn next_table(&self, index: usize) -> Option<&Table> { - self.next_table_address(index).map(|address| unsafe { &*(address as *const _) }) + self.next_table_address(index) + .map(|address| unsafe { &*(address as *const _) }) } pub fn next_table_mut(&mut self, index: usize) -> Option<&mut Table> { - self.next_table_address(index).map(|address| unsafe { &mut *(address as *mut _) }) + self.next_table_address(index) + .map(|address| unsafe { &mut *(address as *mut _) }) } - pub fn next_table_create(&mut self, - index: usize, - allocator: &mut A) - -> &mut Table - where A: FrameAllocator + pub fn next_table_create( + &mut self, + index: usize, + allocator: &mut A, + ) -> &mut Table + where + A: FrameAllocator, { if self.next_table(index).is_none() { - assert!(!self.entries[index].flags().contains(HUGE_PAGE), - "mapping code does not support huge pages"); + assert!( + !self.entries[index].flags().contains(HUGE_PAGE), + "mapping code does not support huge pages" + ); let frame = allocator.allocate_frame().expect("no frames available"); self.entries[index].set(frame, PRESENT | WRITABLE); self.next_table_mut(index).unwrap().zero(); @@ -69,7 +77,8 @@ impl Table } impl Index for Table - where L: TableLevel +where + L: TableLevel, { type Output = Entry; @@ -79,7 +88,8 @@ impl Index for Table } impl IndexMut for Table - where L: TableLevel +where + L: TableLevel, { fn index_mut(&mut self, index: usize) -> &mut Entry { &mut self.entries[index] diff --git a/src/memory/paging/temporary_page.rs b/src/memory/paging/temporary_page.rs index 0934576e..e404b183 100644 --- a/src/memory/paging/temporary_page.rs +++ b/src/memory/paging/temporary_page.rs @@ -18,7 +18,8 @@ pub struct TemporaryPage { impl TemporaryPage { pub fn new(page: Page, allocator: &mut A) -> TemporaryPage - where A: FrameAllocator + where + A: FrameAllocator, { TemporaryPage { page: page, @@ -31,18 +32,21 @@ impl TemporaryPage { pub fn map(&mut self, frame: Frame, active_table: &mut ActivePageTable) -> VirtualAddress { use super::entry::WRITABLE; - assert!(active_table.translate_page(self.page).is_none(), - "temporary page is already mapped"); + assert!( + active_table.translate_page(self.page).is_none(), + "temporary page is already mapped" + ); active_table.map_to(self.page, frame, WRITABLE, &mut self.allocator); self.page.start_address() } /// 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: Frame, - active_table: &mut ActivePageTable) - -> &mut Table { + pub fn map_table_frame( + &mut self, + frame: Frame, + active_table: &mut ActivePageTable, + ) -> &mut Table { unsafe { &mut *(self.map(frame, active_table) as *mut Table) } } @@ -56,7 +60,8 @@ struct TinyAllocator([Option; 3]); impl TinyAllocator { fn new(allocator: &mut A) -> TinyAllocator - where A: FrameAllocator + where + A: FrameAllocator, { let mut f = || allocator.allocate_frame(); let frames = [f(), f(), f()]; diff --git a/src/memory/stack_allocator.rs b/src/memory/stack_allocator.rs index 0cbaee7d..e69fcabf 100644 --- a/src/memory/stack_allocator.rs +++ b/src/memory/stack_allocator.rs @@ -12,11 +12,12 @@ impl StackAllocator { } impl StackAllocator { - pub fn alloc_stack(&mut self, - active_table: &mut ActivePageTable, - frame_allocator: &mut FA, - size_in_pages: usize) - -> Option { + pub fn alloc_stack( + &mut self, + active_table: &mut ActivePageTable, + frame_allocator: &mut FA, + size_in_pages: usize, + ) -> Option { if size_in_pages == 0 { return None; /* a zero sized stack makes no sense */ } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index 2ddf2832..2a321e61 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -16,11 +16,10 @@ const BUFFER_HEIGHT: usize = 25; const BUFFER_WIDTH: usize = 80; pub static WRITER: Mutex = Mutex::new(Writer { - column_position: 0, - color_code: ColorCode::new(Color::LightGreen, - Color::Black), - buffer: unsafe { Unique::new(0xb8000 as *mut _) }, - }); + column_position: 0, + color_code: ColorCode::new(Color::LightGreen, Color::Black), + buffer: unsafe { Unique::new(0xb8000 as *mut _) }, +}); macro_rules! println { ($fmt:expr) => (print!(concat!($fmt, "\n"))); @@ -86,9 +85,9 @@ impl Writer { let color_code = self.color_code; self.buffer().chars[row][col].write(ScreenChar { - ascii_character: byte, - color_code: color_code, - }); + ascii_character: byte, + color_code: color_code, + }); self.column_position += 1; } }