mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
33
src/lib.rs
33
src/lib.rs
@@ -28,24 +28,28 @@ mod vga_buffer;
|
|||||||
mod memory;
|
mod memory;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern fn rust_main(multiboot_information_address: usize) {
|
pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
||||||
// ATTENTION: we have a very small stack and no guard page
|
// ATTENTION: we have a very small stack and no guard page
|
||||||
vga_buffer::clear_screen();
|
vga_buffer::clear_screen();
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
let boot_info = unsafe{ multiboot2::load(multiboot_information_address) };
|
let boot_info = unsafe { multiboot2::load(multiboot_information_address) };
|
||||||
let memory_map_tag = boot_info.memory_map_tag().expect("Memory map tag required");
|
let memory_map_tag = boot_info.memory_map_tag().expect("Memory map tag required");
|
||||||
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");
|
||||||
|
|
||||||
println!("memory areas:");
|
println!("memory areas:");
|
||||||
for area in memory_map_tag.memory_areas() {
|
for area in memory_map_tag.memory_areas() {
|
||||||
println!(" start: 0x{:x}, length: 0x{:x}", area.base_addr, area.length);
|
println!(" start: 0x{:x}, length: 0x{:x}",
|
||||||
|
area.base_addr,
|
||||||
|
area.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("kernel sections:");
|
println!("kernel sections:");
|
||||||
for section in elf_sections_tag.sections() {
|
for section in elf_sections_tag.sections() {
|
||||||
println!(" addr: 0x{:x}, size: 0x{:x}, flags: 0x{:x}",
|
println!(" addr: 0x{:x}, size: 0x{:x}, flags: 0x{:x}",
|
||||||
section.addr, section.size, section.flags);
|
section.addr,
|
||||||
|
section.size,
|
||||||
|
section.flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
let kernel_start = elf_sections_tag.sections().map(|s| s.addr).min().unwrap();
|
let kernel_start = elf_sections_tag.sections().map(|s| s.addr).min().unwrap();
|
||||||
@@ -54,25 +58,32 @@ pub extern fn rust_main(multiboot_information_address: usize) {
|
|||||||
let multiboot_start = multiboot_information_address;
|
let multiboot_start = multiboot_information_address;
|
||||||
let multiboot_end = multiboot_start + (boot_info.total_size as usize);
|
let multiboot_end = multiboot_start + (boot_info.total_size as usize);
|
||||||
|
|
||||||
println!("kernel start: 0x{:x}, kernel end: 0x{:x}", kernel_start, kernel_end);
|
println!("kernel start: 0x{:x}, kernel end: 0x{:x}",
|
||||||
println!("multiboot start: 0x{:x}, multiboot end: 0x{:x}", multiboot_start, multiboot_end);
|
kernel_start,
|
||||||
|
kernel_end);
|
||||||
|
println!("multiboot start: 0x{:x}, multiboot end: 0x{:x}",
|
||||||
|
multiboot_start,
|
||||||
|
multiboot_end);
|
||||||
|
|
||||||
let mut frame_allocator = memory::AreaFrameAllocator::new(kernel_start as usize,
|
let mut frame_allocator = memory::AreaFrameAllocator::new(kernel_start as usize,
|
||||||
kernel_end as usize, multiboot_start, multiboot_end, memory_map_tag.memory_areas());
|
kernel_end as usize,
|
||||||
|
multiboot_start,
|
||||||
|
multiboot_end,
|
||||||
|
memory_map_tag.memory_areas());
|
||||||
|
|
||||||
memory::test_paging(&mut frame_allocator);
|
memory::test_paging(&mut frame_allocator);
|
||||||
|
|
||||||
loop{}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[lang = "eh_personality"]
|
#[lang = "eh_personality"]
|
||||||
extern fn eh_personality() {}
|
extern "C" fn eh_personality() {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[lang = "panic_fmt"]
|
#[lang = "panic_fmt"]
|
||||||
extern fn panic_fmt(fmt: core::fmt::Arguments, file: &str, line: u32) -> ! {
|
extern "C" fn panic_fmt(fmt: core::fmt::Arguments, file: &str, line: u32) -> ! {
|
||||||
println!("\n\nPANIC in {} at line {}:", file, line);
|
println!("\n\nPANIC in {} at line {}:", file, line);
|
||||||
println!(" {}", fmt);
|
println!(" {}", fmt);
|
||||||
loop{}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,12 @@ pub struct AreaFrameAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AreaFrameAllocator {
|
impl AreaFrameAllocator {
|
||||||
pub fn new(kernel_start: usize, kernel_end: usize, multiboot_start: usize, multiboot_end: usize,
|
pub fn new(kernel_start: usize,
|
||||||
memory_areas: MemoryAreaIter) -> AreaFrameAllocator
|
kernel_end: usize,
|
||||||
{
|
multiboot_start: usize,
|
||||||
|
multiboot_end: usize,
|
||||||
|
memory_areas: MemoryAreaIter)
|
||||||
|
-> AreaFrameAllocator {
|
||||||
let mut allocator = AreaFrameAllocator {
|
let mut allocator = AreaFrameAllocator {
|
||||||
next_free_frame: Frame::containing_address(0),
|
next_free_frame: Frame::containing_address(0),
|
||||||
current_area: None,
|
current_area: None,
|
||||||
@@ -34,10 +37,14 @@ impl AreaFrameAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn choose_next_area(&mut self) {
|
fn choose_next_area(&mut self) {
|
||||||
self.current_area = self.areas.clone().filter(|area| {
|
self.current_area = self.areas
|
||||||
let address = area.base_addr + area.length - 1;
|
.clone()
|
||||||
Frame::containing_address(address as usize) >= self.next_free_frame
|
.filter(|area| {
|
||||||
}).min_by_key(|area| area.base_addr);
|
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 {
|
if let Some(area) = self.current_area {
|
||||||
let start_frame = Frame::containing_address(area.base_addr as usize);
|
let start_frame = Frame::containing_address(area.base_addr as usize);
|
||||||
@@ -53,7 +60,7 @@ impl FrameAllocator for AreaFrameAllocator {
|
|||||||
if let Some(area) = self.current_area {
|
if let Some(area) = self.current_area {
|
||||||
// "clone" the frame to return it if it's free. Frame doesn't
|
// "clone" the frame to return it if it's free. Frame doesn't
|
||||||
// implement Clone, but we can construct an identical frame.
|
// implement Clone, but we can construct an identical frame.
|
||||||
let frame = Frame{ number: self.next_free_frame.number };
|
let frame = Frame { number: self.next_free_frame.number };
|
||||||
|
|
||||||
// the last frame of the current area
|
// the last frame of the current area
|
||||||
let current_area_last_frame = {
|
let current_area_last_frame = {
|
||||||
@@ -66,10 +73,10 @@ impl FrameAllocator for AreaFrameAllocator {
|
|||||||
self.choose_next_area();
|
self.choose_next_area();
|
||||||
} else if frame >= self.kernel_start && frame <= self.kernel_end {
|
} else if frame >= self.kernel_start && frame <= self.kernel_end {
|
||||||
// `frame` is used by the kernel
|
// `frame` is used by the kernel
|
||||||
self.next_free_frame = Frame{ number: self.kernel_end.number + 1 };
|
self.next_free_frame = Frame { number: self.kernel_end.number + 1 };
|
||||||
} else if frame >= self.multiboot_start && frame <= self.multiboot_end {
|
} else if frame >= self.multiboot_start && frame <= self.multiboot_end {
|
||||||
// `frame` is used by the multiboot information structure
|
// `frame` is used by the multiboot information structure
|
||||||
self.next_free_frame = Frame{ number: self.multiboot_end.number + 1 };
|
self.next_free_frame = Frame { number: self.multiboot_end.number + 1 };
|
||||||
} else {
|
} else {
|
||||||
// frame is unused, increment `next_free_frame` and return it
|
// frame is unused, increment `next_free_frame` and return it
|
||||||
self.next_free_frame.number += 1;
|
self.next_free_frame.number += 1;
|
||||||
@@ -82,5 +89,7 @@ impl FrameAllocator for AreaFrameAllocator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deallocate_frame(&mut self, _frame: Frame) {unimplemented!()}
|
fn deallocate_frame(&mut self, _frame: Frame) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,9 +139,9 @@ impl RecursivePageTable {
|
|||||||
.expect("mapping code does not support huge pages");
|
.expect("mapping code does not support huge pages");
|
||||||
let frame = p1[page.p1_index()].pointed_frame().unwrap();
|
let frame = p1[page.p1_index()].pointed_frame().unwrap();
|
||||||
p1[page.p1_index()].set_unused();
|
p1[page.p1_index()].set_unused();
|
||||||
unsafe { ::x86::tlb::flush(page.start_address() )};
|
unsafe { ::x86::tlb::flush(page.start_address()) };
|
||||||
// TODO free p(1,2,3) table if empty
|
// TODO free p(1,2,3) table if empty
|
||||||
//allocator.deallocate_frame(frame);
|
// allocator.deallocate_frame(frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,9 +170,8 @@ pub fn test_paging<A>(allocator: &mut A)
|
|||||||
println!("next free frame: {:?}", allocator.allocate_frame());
|
println!("next free frame: {:?}", allocator.allocate_frame());
|
||||||
|
|
||||||
// test unmap
|
// test unmap
|
||||||
println!("{:#x}", unsafe {
|
println!("{:#x}",
|
||||||
*(Page::containing_address(addr).start_address() as *const u64)
|
unsafe { *(Page::containing_address(addr).start_address() as *const u64) });
|
||||||
});
|
|
||||||
page_table.unmap(Page::containing_address(addr), allocator);
|
page_table.unmap(Page::containing_address(addr), allocator);
|
||||||
println!("None = {:?}", page_table.translate(addr));
|
println!("None = {:?}", page_table.translate(addr));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const BUFFER_WIDTH: usize = 80;
|
|||||||
pub static WRITER: Mutex<Writer> = Mutex::new(Writer {
|
pub static WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||||
column_position: 0,
|
column_position: 0,
|
||||||
color_code: ColorCode::new(Color::LightGreen, Color::Black),
|
color_code: ColorCode::new(Color::LightGreen, Color::Black),
|
||||||
buffer: unsafe{Unique::new(0xb8000 as *mut _)},
|
buffer: unsafe { Unique::new(0xb8000 as *mut _) },
|
||||||
});
|
});
|
||||||
|
|
||||||
macro_rules! println {
|
macro_rules! println {
|
||||||
@@ -46,22 +46,22 @@ pub fn clear_screen() {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum Color {
|
pub enum Color {
|
||||||
Black = 0,
|
Black = 0,
|
||||||
Blue = 1,
|
Blue = 1,
|
||||||
Green = 2,
|
Green = 2,
|
||||||
Cyan = 3,
|
Cyan = 3,
|
||||||
Red = 4,
|
Red = 4,
|
||||||
Magenta = 5,
|
Magenta = 5,
|
||||||
Brown = 6,
|
Brown = 6,
|
||||||
LightGray = 7,
|
LightGray = 7,
|
||||||
DarkGray = 8,
|
DarkGray = 8,
|
||||||
LightBlue = 9,
|
LightBlue = 9,
|
||||||
LightGreen = 10,
|
LightGreen = 10,
|
||||||
LightCyan = 11,
|
LightCyan = 11,
|
||||||
LightRed = 12,
|
LightRed = 12,
|
||||||
Pink = 13,
|
Pink = 13,
|
||||||
Yellow = 14,
|
Yellow = 14,
|
||||||
White = 15,
|
White = 15,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Writer {
|
pub struct Writer {
|
||||||
@@ -91,15 +91,15 @@ impl Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn buffer(&mut self) -> &mut Buffer {
|
fn buffer(&mut self) -> &mut Buffer {
|
||||||
unsafe{self.buffer.get_mut()}
|
unsafe { self.buffer.get_mut() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_line(&mut self) {
|
fn new_line(&mut self) {
|
||||||
for row in 0..(BUFFER_HEIGHT-1) {
|
for row in 0..(BUFFER_HEIGHT - 1) {
|
||||||
let buffer = self.buffer();
|
let buffer = self.buffer();
|
||||||
buffer.chars[row] = buffer.chars[row + 1]
|
buffer.chars[row] = buffer.chars[row + 1]
|
||||||
}
|
}
|
||||||
self.clear_row(BUFFER_HEIGHT-1);
|
self.clear_row(BUFFER_HEIGHT - 1);
|
||||||
self.column_position = 0;
|
self.column_position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ impl Writer {
|
|||||||
impl fmt::Write for Writer {
|
impl fmt::Write for Writer {
|
||||||
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
|
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
|
||||||
for byte in s.bytes() {
|
for byte in s.bytes() {
|
||||||
self.write_byte(byte)
|
self.write_byte(byte)
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user