# SteinsOS Document ## Introduction [SteinsOS](https://github.com/0x59616e/SteinsOS) 是一個以 Rust 寫成、支援單核 Armv8 的作業系統,並具備以下特徵: - [non-preemptive kernel](https://en.wikipedia.org/wiki/Kernel_preemption) - Preemptive multi-tasking - File system - Virutal memory - [Buddy memory allocation](https://en.wikipedia.org/wiki/Buddy_memory_allocation) 這份文件將會著重在 SteinsOS 內部的設計。有關於作業系統的原理,請參閱以下資料: - [Operating Systems: Three Easy Pieces](https://pages.cs.wisc.edu/~remzi/OSTEP/) - [MIT 6.S081 Fall 2020](https://pdos.csail.mit.edu/6.828/2020/schedule.html) ## Memory Allocator 為什麼開頭就先講 Memory Allocator ? 因為,有 Memory Allocator,就可以用 [`alloc`](https://doc.rust-lang.org/alloc/index.html) 裡各式各樣的 [collections](https://doc.rust-lang.org/alloc/collections/index.html),非常方便。 - Buddy Allocator SteinsOS 裡有兩種 Memory allocator,第一種是 [Buddy Allocator](https://en.wikipedia.org/wiki/Buddy_memory_allocation)。 Buddy Allocator 的好處,在於能夠分配連續、對齊的記憶體分頁。之後會再深入說明。 程式碼請參見[這裡](https://github.com/0x59616e/SteinsOS/blob/main/kernel/src/mm/buddyallocator.rs) - Slab Allocator SteinsOS 裡的第二種 Memory Allocator 是 Slab Allocator 當然,其複雜度遠遠不及 Linux 的 Slab ,請各位別誤會了。 Slab Allocator 會向 Buddy Allocator 批發一些分頁,把它切成特定大小,再分發出去。 查看[程式碼](https://github.com/0x59616e/SteinsOS/blob/main/kernel/src/mm/slaballocator.rs) 註解可發現,總共有八種大小,最大到 1024 bytes。 如果超過 1024 bytes,Slab allocator 會把請求丟給 Buddy allocator: ```rust= if layout.size() > MAXIMUM_SLAB_SIZE { return BuddyAllocator.alloc(layout); } ``` 在一開始提到,實作 Memory allocator 是為了滿足 [`alloc`](https://doc.rust-lang.org/alloc/index.html) 的需求。 那麼,給 [`alloc`](https://doc.rust-lang.org/alloc/index.html) 用的是哪個 Allocator 呢? 查看 [kernel/src/lib.rs](https://github.com/0x59616e/SteinsOS/blob/main/kernel/src/lib.rs) ```rust= use mm::slaballocator::SlabAllocator; #[global_allocator] static ALLOCATOR: SlabAllocator = SlabAllocator; ``` 是 Slab。 Buddy 用來管裡整個記憶體,而 Slab 是為了滿足 `alloc` 的需要。 畢竟,你總不能連 8 bytes 的記憶體分配請求,都給予一整個 4096 bytes 的分頁吧。 有了這兩種 Allocator,我們就可以盡情使用 [`alloc`](https://doc.rust-lang.org/alloc/index.html) 裡各式 [`collections`](https://doc.rust-lang.org/alloc/collections/index.html) 了 ## Virtual Memory ## To be continued - stack overflow ## Reference [Operating Systems: Three Easy Pieces](https://pages.cs.wisc.edu/~remzi/OSTEP/) [Writing an OS in Rust](https://os.phil-opp.com/) [Redox OS](https://gitlab.redox-os.org/redox-os/redox) [xv6](https://gitlab.redox-os.org/redox-os/redox) [Arm Architecture Reference Manual Armv8, for A-profile architecture](https://developer.arm.com/documentation/ddi0487/gb)