From ac9b661395970dd48c3085e50c688255081903af Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 3 May 2019 18:36:39 +0200 Subject: [PATCH 1/2] Update to x86_64 0.6.0 --- Cargo.lock | 22 +++++++++++++++++++++- Cargo.toml | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 370d630a..25cf43d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,7 +27,7 @@ dependencies = [ "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "uart_16550 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "volatile 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", - "x86_64 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", + "x86_64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -43,6 +43,11 @@ dependencies = [ "xmas-elf 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.35" @@ -284,6 +289,19 @@ dependencies = [ "ux 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "x86_64" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "array-init 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ux 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "xmas-elf" version = "0.6.2" @@ -302,6 +320,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum bootloader 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8654b1ebbd38d2a8687a451ad53466d01b5edc9d75ec63d676525a6103d77151" +"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" "checksum cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)" = "5e5f3fee5eeb60324c2781f1e41286bdee933850fff9b3c672587fed5ec58c83" "checksum fixedvec 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7c6c16d316ccdac21a4dd648e314e76facbbaf316e83ca137d0857a9c07419d0" "checksum font8x8 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b81d84c3c978af7d05d31a2198af4b9ba956d819d15d8f6d58fc150e33f8dc1f" @@ -336,5 +355,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum x86_64 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f9258d7e2dd25008d69e8c9e9ee37865887a5e1e3d06a62f1cb3f6c209e6f177" "checksum x86_64 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1d0a8201f52d2c7b373c7243dcdfb27c0dd5012f221ef6a126f507ee82005204" +"checksum x86_64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d69bf2d256c74df90fcc68aaf99862dd205310609e9d56247a5c82ead2f28a93" "checksum xmas-elf 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "22678df5df766e8d1e5d609da69f0c3132d794edf6ab5e75e7abcd2270d4cf58" "checksum zero 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5f1bc8a6b2005884962297587045002d8cfb8dcec9db332f4ca216ddc5de82c5" diff --git a/Cargo.toml b/Cargo.toml index d984b6ae..2fce8e43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ harness = false bootloader = "0.6.0" volatile = "0.2.3" spin = "0.4.9" -x86_64 = "0.5.2" +x86_64 = "0.6.0" uart_16550 = "0.2.0" [dependencies.lazy_static] From 7796d4c14adc7037d9076a2f9940ee19ca1300fd Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 3 May 2019 18:41:01 +0200 Subject: [PATCH 2/2] FrameAllocator is an `unsafe trait` now Make `BootInfoFrameAllocator` unsafe because the caller must guarantee that the given memory map is valid. --- src/main.rs | 2 +- src/memory.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c31a92b5..10b38955 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { blog_os::init(); let mut mapper = unsafe { memory::init(boot_info.physical_memory_offset) }; - let mut frame_allocator = BootInfoFrameAllocator::init(&boot_info.memory_map); + let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) }; // map a previously unmapped page let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000)); diff --git a/src/memory.rs b/src/memory.rs index 2e110fce..626f4fdb 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -59,7 +59,7 @@ pub fn create_example_mapping( /// A FrameAllocator that always returns `None`. pub struct EmptyFrameAllocator; -impl FrameAllocator for EmptyFrameAllocator { +unsafe impl FrameAllocator for EmptyFrameAllocator { fn allocate_frame(&mut self) -> Option { None } @@ -73,7 +73,11 @@ pub struct BootInfoFrameAllocator { impl BootInfoFrameAllocator { /// Create a FrameAllocator from the passed memory map. - pub fn init(memory_map: &'static MemoryMap) -> Self { + /// + /// This function is unsafe because the caller must guarantee that the passed + /// memory map is valid. The main requirement is that all frames that are marked + /// as `USABLE` in it are really unused. + pub unsafe fn init(memory_map: &'static MemoryMap) -> Self { BootInfoFrameAllocator { memory_map, next: 0, @@ -94,7 +98,7 @@ impl BootInfoFrameAllocator { } } -impl FrameAllocator for BootInfoFrameAllocator { +unsafe impl FrameAllocator for BootInfoFrameAllocator { fn allocate_frame(&mut self) -> Option { let frame = self.usable_frames().nth(self.next); self.next += 1;