mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
46 Commits
0df06162c8
...
post-12-as
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d485535ae | ||
|
|
08582948c5 | ||
|
|
b3ba0ba4e9 | ||
|
|
c3ea4190ca | ||
|
|
83b67df8af | ||
|
|
714d9cef7d | ||
|
|
3735ae51c9 | ||
|
|
f75d63853f | ||
|
|
378159ce76 | ||
|
|
a430d5ef06 | ||
|
|
a5ff4261a0 | ||
|
|
ea83d905fe | ||
|
|
786a7a6922 | ||
|
|
6329274f02 | ||
|
|
f4331daa25 | ||
|
|
921dd54207 | ||
|
|
3a6d3153a4 | ||
|
|
9fb6c1d0bd | ||
|
|
00fedc801e | ||
|
|
0f74db4812 | ||
|
|
93fd330ab9 | ||
|
|
3ad5f117c2 | ||
|
|
d1678f5a96 | ||
|
|
002d6f255f | ||
|
|
6c3bf0b10f | ||
|
|
7a792f5cb0 | ||
|
|
93b4dcf434 | ||
|
|
821dd2adb4 | ||
|
|
d636939b51 | ||
|
|
9b7326541e | ||
|
|
4f234b67ef | ||
|
|
7381e11f3c | ||
|
|
a9fe65a0ce | ||
|
|
2001814119 | ||
|
|
a5c50e7408 | ||
|
|
70a52c291d | ||
|
|
c56bfa27e4 | ||
|
|
55aec9ebf3 | ||
|
|
2e1d132a9a | ||
|
|
63e8577d77 | ||
|
|
75d826bf69 | ||
|
|
45be3f0648 | ||
|
|
055c560a7a | ||
|
|
e87044a7ee | ||
|
|
08d2289dad | ||
|
|
7c84dbaa1d |
79
Cargo.lock
generated
79
Cargo.lock
generated
@@ -1,5 +1,11 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.9.0"
|
||||
@@ -17,6 +23,9 @@ name = "blog_os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"conquer-once",
|
||||
"crossbeam-queue",
|
||||
"futures-util",
|
||||
"lazy_static",
|
||||
"linked_list_allocator",
|
||||
"pc-keyboard",
|
||||
@@ -33,12 +42,76 @@ version = "0.8.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3ed4f735c4e455ba86a3d2939b1c0729414153642106c9d035693355630a42c"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "conquer-once"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f7644600a548ecad74e4a918392af1798f7dd045be610be3203b9e129b4f98f"
|
||||
dependencies = [
|
||||
"conquer-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "conquer-util"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "654fb2472cc369d311c547103a1fa81d467bef370ae7a0680f65939895b1182a"
|
||||
|
||||
[[package]]
|
||||
name = "cpuio"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22b8e308ccfc5acf3b82f79c0eac444cf6114cb2ac67a230ca6c177210068daa"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f25592f769825e89b92358db00d26f965761e094951ac44d3663ef25b7ac464a"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b0a34e53cf6cdcd0178aa573aed466b646eb3db769570841fda0c7ede375a27"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22766cf25d64306bedf0384da004d05c9974ab104fcc4528f1236181c18004c5"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"pin-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@@ -81,6 +154,12 @@ dependencies = [
|
||||
"cpuio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-utils"
|
||||
version = "0.1.0-alpha.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
|
||||
13
Cargo.toml
13
Cargo.toml
@@ -26,6 +26,19 @@ linked_list_allocator = "0.8.0"
|
||||
version = "1.0"
|
||||
features = ["spin_no_std"]
|
||||
|
||||
[dependencies.crossbeam-queue]
|
||||
version = "0.2.1"
|
||||
default-features = false
|
||||
features = ["alloc"]
|
||||
|
||||
[dependencies.futures-util]
|
||||
version = "0.3.4"
|
||||
default-features = false
|
||||
features = ["alloc", "async-await"]
|
||||
|
||||
[dependencies.conquer-once]
|
||||
version = "0.2.0"
|
||||
default-features = false
|
||||
|
||||
[package.metadata.bootimage]
|
||||
test-args = [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
use core::ptr::null_mut;
|
||||
use linked_list_allocator::LockedHeap;
|
||||
use fixed_size_block::FixedSizeBlockAllocator;
|
||||
use x86_64::{
|
||||
structures::paging::{
|
||||
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
|
||||
@@ -8,11 +8,15 @@ use x86_64::{
|
||||
VirtAddr,
|
||||
};
|
||||
|
||||
pub mod bump;
|
||||
pub mod fixed_size_block;
|
||||
pub mod linked_list;
|
||||
|
||||
pub const HEAP_START: usize = 0x_4444_4444_0000;
|
||||
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||
static ALLOCATOR: Locked<FixedSizeBlockAllocator> = Locked::new(FixedSizeBlockAllocator::new());
|
||||
|
||||
pub fn init_heap(
|
||||
mapper: &mut impl Mapper<Size4KiB>,
|
||||
@@ -52,3 +56,27 @@ unsafe impl GlobalAlloc for Dummy {
|
||||
panic!("dealloc should be never called")
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around spin::Mutex to permit trait implementations.
|
||||
pub struct Locked<A> {
|
||||
inner: spin::Mutex<A>,
|
||||
}
|
||||
|
||||
impl<A> Locked<A> {
|
||||
pub const fn new(inner: A) -> Self {
|
||||
Locked {
|
||||
inner: spin::Mutex::new(inner),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lock(&self) -> spin::MutexGuard<A> {
|
||||
self.inner.lock()
|
||||
}
|
||||
}
|
||||
|
||||
/// Align the given address `addr` upwards to alignment `align`.
|
||||
///
|
||||
/// Requires that `align` is a power of two.
|
||||
fn align_up(addr: usize, align: usize) -> usize {
|
||||
(addr + align - 1) & !(align - 1)
|
||||
}
|
||||
|
||||
61
src/allocator/bump.rs
Normal file
61
src/allocator/bump.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use super::{align_up, Locked};
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
use core::ptr;
|
||||
|
||||
pub struct BumpAllocator {
|
||||
heap_start: usize,
|
||||
heap_end: usize,
|
||||
next: usize,
|
||||
allocations: usize,
|
||||
}
|
||||
|
||||
impl BumpAllocator {
|
||||
/// Creates a new empty bump allocator.
|
||||
pub const fn new() -> Self {
|
||||
BumpAllocator {
|
||||
heap_start: 0,
|
||||
heap_end: 0,
|
||||
next: 0,
|
||||
allocations: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the bump allocator with the given heap bounds.
|
||||
///
|
||||
/// This method is unsafe because the caller must ensure that the given
|
||||
/// memory range is unused. Also, this method must be called only once.
|
||||
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||
self.heap_start = heap_start;
|
||||
self.heap_end = heap_start.saturating_add(heap_size);
|
||||
self.next = heap_start;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl GlobalAlloc for Locked<BumpAllocator> {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
let mut bump = self.lock(); // get a mutable reference
|
||||
|
||||
let alloc_start = align_up(bump.next, layout.align());
|
||||
let alloc_end = match alloc_start.checked_add(layout.size()) {
|
||||
Some(end) => end,
|
||||
None => return ptr::null_mut(),
|
||||
};
|
||||
|
||||
if alloc_end > bump.heap_end {
|
||||
ptr::null_mut() // out of memory
|
||||
} else {
|
||||
bump.next = alloc_end;
|
||||
bump.allocations += 1;
|
||||
alloc_start as *mut u8
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
||||
let mut bump = self.lock(); // get a mutable reference
|
||||
|
||||
bump.allocations -= 1;
|
||||
if bump.allocations == 0 {
|
||||
bump.next = bump.heap_start;
|
||||
}
|
||||
}
|
||||
}
|
||||
102
src/allocator/fixed_size_block.rs
Normal file
102
src/allocator/fixed_size_block.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use super::Locked;
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
use core::{
|
||||
mem,
|
||||
ptr::{self, NonNull},
|
||||
};
|
||||
|
||||
/// The block sizes to use.
|
||||
///
|
||||
/// The sizes must each be power of 2 because they are also used as
|
||||
/// the block alignment (alignments must be always powers of 2).
|
||||
const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048];
|
||||
|
||||
/// Choose an appropriate block size for the given layout.
|
||||
///
|
||||
/// Returns an index into the `BLOCK_SIZES` array.
|
||||
fn list_index(layout: &Layout) -> Option<usize> {
|
||||
let required_block_size = layout.size().max(layout.align());
|
||||
BLOCK_SIZES.iter().position(|&s| s >= required_block_size)
|
||||
}
|
||||
|
||||
struct ListNode {
|
||||
next: Option<&'static mut ListNode>,
|
||||
}
|
||||
|
||||
pub struct FixedSizeBlockAllocator {
|
||||
list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()],
|
||||
fallback_allocator: linked_list_allocator::Heap,
|
||||
}
|
||||
|
||||
impl FixedSizeBlockAllocator {
|
||||
/// Creates an empty FixedSizeBlockAllocator.
|
||||
pub const fn new() -> Self {
|
||||
FixedSizeBlockAllocator {
|
||||
list_heads: [None; BLOCK_SIZES.len()],
|
||||
fallback_allocator: linked_list_allocator::Heap::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the allocator with the given heap bounds.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the given
|
||||
/// heap bounds are valid and that the heap is unused. This method must be
|
||||
/// called only once.
|
||||
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||
self.fallback_allocator.init(heap_start, heap_size);
|
||||
}
|
||||
|
||||
/// Allocates using the fallback allocator.
|
||||
fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 {
|
||||
match self.fallback_allocator.allocate_first_fit(layout) {
|
||||
Ok(ptr) => ptr.as_ptr(),
|
||||
Err(_) => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl GlobalAlloc for Locked<FixedSizeBlockAllocator> {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
let mut allocator = self.lock();
|
||||
match list_index(&layout) {
|
||||
Some(index) => {
|
||||
match allocator.list_heads[index].take() {
|
||||
Some(node) => {
|
||||
allocator.list_heads[index] = node.next.take();
|
||||
node as *mut ListNode as *mut u8
|
||||
}
|
||||
None => {
|
||||
// no block exists in list => allocate new block
|
||||
let block_size = BLOCK_SIZES[index];
|
||||
// only works if all block sizes are a power of 2
|
||||
let block_align = block_size;
|
||||
let layout = Layout::from_size_align(block_size, block_align).unwrap();
|
||||
allocator.fallback_alloc(layout)
|
||||
}
|
||||
}
|
||||
}
|
||||
None => allocator.fallback_alloc(layout),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
let mut allocator = self.lock();
|
||||
match list_index(&layout) {
|
||||
Some(index) => {
|
||||
let new_node = ListNode {
|
||||
next: allocator.list_heads[index].take(),
|
||||
};
|
||||
// verify that block has size and alignment required for storing node
|
||||
assert!(mem::size_of::<ListNode>() <= BLOCK_SIZES[index]);
|
||||
assert!(mem::align_of::<ListNode>() <= BLOCK_SIZES[index]);
|
||||
let new_node_ptr = ptr as *mut ListNode;
|
||||
new_node_ptr.write(new_node);
|
||||
allocator.list_heads[index] = Some(&mut *new_node_ptr);
|
||||
}
|
||||
None => {
|
||||
let ptr = NonNull::new(ptr).unwrap();
|
||||
allocator.fallback_allocator.deallocate(ptr, layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
145
src/allocator/linked_list.rs
Normal file
145
src/allocator/linked_list.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
use super::{align_up, Locked};
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
use core::{mem, ptr};
|
||||
|
||||
struct ListNode {
|
||||
size: usize,
|
||||
next: Option<&'static mut ListNode>,
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
const fn new(size: usize) -> Self {
|
||||
ListNode { size, next: None }
|
||||
}
|
||||
|
||||
fn start_addr(&self) -> usize {
|
||||
self as *const Self as usize
|
||||
}
|
||||
|
||||
fn end_addr(&self) -> usize {
|
||||
self.start_addr() + self.size
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LinkedListAllocator {
|
||||
head: ListNode,
|
||||
}
|
||||
|
||||
impl LinkedListAllocator {
|
||||
/// Creates an empty LinkedListAllocator.
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
head: ListNode::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the allocator with the given heap bounds.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the given
|
||||
/// heap bounds are valid and that the heap is unused. This method must be
|
||||
/// called only once.
|
||||
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
|
||||
self.add_free_region(heap_start, heap_size);
|
||||
}
|
||||
|
||||
/// Adds the given memory region to the front of the list.
|
||||
unsafe fn add_free_region(&mut self, addr: usize, size: usize) {
|
||||
// ensure that the freed region is capable of holding ListNode
|
||||
assert!(align_up(addr, mem::align_of::<ListNode>()) == addr);
|
||||
assert!(size >= mem::size_of::<ListNode>());
|
||||
|
||||
// create a new list node and append it at the start of the list
|
||||
let mut node = ListNode::new(size);
|
||||
node.next = self.head.next.take();
|
||||
let node_ptr = addr as *mut ListNode;
|
||||
node_ptr.write(node);
|
||||
self.head.next = Some(&mut *node_ptr)
|
||||
}
|
||||
|
||||
/// Looks for a free region with the given size and alignment and removes
|
||||
/// it from the list.
|
||||
///
|
||||
/// Returns a tuple of the list node and the start address of the allocation.
|
||||
fn find_region(&mut self, size: usize, align: usize) -> Option<(&'static mut ListNode, usize)> {
|
||||
// reference to current list node, updated for each iteration
|
||||
let mut current = &mut self.head;
|
||||
// look for a large enough memory region in linked list
|
||||
while let Some(ref mut region) = current.next {
|
||||
if let Ok(alloc_start) = Self::alloc_from_region(®ion, size, align) {
|
||||
// region suitable for allocation -> remove node from list
|
||||
let next = region.next.take();
|
||||
let ret = Some((current.next.take().unwrap(), alloc_start));
|
||||
current.next = next;
|
||||
return ret;
|
||||
} else {
|
||||
// region not suitable -> continue with next region
|
||||
current = current.next.as_mut().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// no suitable region found
|
||||
None
|
||||
}
|
||||
|
||||
/// Try to use the given region for an allocation with given size and alignment.
|
||||
///
|
||||
/// Returns the allocation start address on success.
|
||||
fn alloc_from_region(region: &ListNode, size: usize, align: usize) -> Result<usize, ()> {
|
||||
let alloc_start = align_up(region.start_addr(), align);
|
||||
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
|
||||
|
||||
if alloc_end > region.end_addr() {
|
||||
// region too small
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let excess_size = region.end_addr() - alloc_end;
|
||||
if excess_size > 0 && excess_size < mem::size_of::<ListNode>() {
|
||||
// rest of region too small to hold a ListNode (required because the
|
||||
// allocation splits the region in a used and a free part)
|
||||
return Err(());
|
||||
}
|
||||
|
||||
// region suitable for allocation
|
||||
Ok(alloc_start)
|
||||
}
|
||||
|
||||
/// Adjust the given layout so that the resulting allocated memory
|
||||
/// region is also capable of storing a `ListNode`.
|
||||
///
|
||||
/// Returns the adjusted size and alignment as a (size, align) tuple.
|
||||
fn size_align(layout: Layout) -> (usize, usize) {
|
||||
let layout = layout
|
||||
.align_to(mem::align_of::<ListNode>())
|
||||
.expect("adjusting alignment failed")
|
||||
.pad_to_align();
|
||||
let size = layout.size().max(mem::size_of::<ListNode>());
|
||||
(size, layout.align())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl GlobalAlloc for Locked<LinkedListAllocator> {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
// perform layout adjustments
|
||||
let (size, align) = LinkedListAllocator::size_align(layout);
|
||||
let mut allocator = self.lock();
|
||||
|
||||
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
|
||||
let alloc_end = alloc_start.checked_add(size).expect("overflow");
|
||||
let excess_size = region.end_addr() - alloc_end;
|
||||
if excess_size > 0 {
|
||||
allocator.add_free_region(alloc_end, excess_size);
|
||||
}
|
||||
alloc_start as *mut u8
|
||||
} else {
|
||||
ptr::null_mut()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
// perform layout adjustments
|
||||
let (size, _) = LinkedListAllocator::size_align(layout);
|
||||
|
||||
self.lock().add_free_region(ptr as usize, size)
|
||||
}
|
||||
}
|
||||
75
src/driver/keyboard.rs
Normal file
75
src/driver/keyboard.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use crate::{print, println, task::interrupt_wake};
|
||||
use conquer_once::spin::OnceCell;
|
||||
use core::future::Future;
|
||||
use core::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use futures_util::task::AtomicWaker;
|
||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
||||
|
||||
pub fn init() {
|
||||
SCANCODE_QUEUE
|
||||
.try_init_once(|| ArrayQueue::new(10))
|
||||
.expect("failed to init scancode queue");
|
||||
}
|
||||
|
||||
/// Called by the keyboard interrupt handler
|
||||
///
|
||||
/// Must not block (including spinlocks).
|
||||
pub(crate) fn keyboard_scancode(scancode: u8) {
|
||||
let scancode_queue = SCANCODE_QUEUE
|
||||
.try_get()
|
||||
.expect("scancode queue not initialized");
|
||||
if let Err(_) = scancode_queue.push(scancode) {
|
||||
println!("WARNING: dropping keyboard input");
|
||||
}
|
||||
if let Some(waker) = WAKER.take() {
|
||||
interrupt_wake(waker);
|
||||
}
|
||||
}
|
||||
|
||||
fn next_scancode() -> impl Future<Output = u8> {
|
||||
NextScancode
|
||||
}
|
||||
|
||||
struct NextScancode;
|
||||
|
||||
impl Future for NextScancode {
|
||||
type Output = u8;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<u8> {
|
||||
let scancodes = SCANCODE_QUEUE
|
||||
.try_get()
|
||||
.expect("scancode queue not initialized");
|
||||
// fast path
|
||||
if let Ok(scancode) = scancodes.pop() {
|
||||
return Poll::Ready(scancode);
|
||||
}
|
||||
|
||||
WAKER.register(&cx.waker());
|
||||
match scancodes.pop() {
|
||||
Ok(scancode) => Poll::Ready(scancode),
|
||||
Err(crossbeam_queue::PopError) => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn keyboard_task() {
|
||||
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore);
|
||||
|
||||
loop {
|
||||
if let Ok(Some(key_event)) = keyboard.add_byte(next_scancode().await) {
|
||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
||||
match key {
|
||||
DecodedKey::Unicode(character) => print!("{}", character),
|
||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/driver/mod.rs
Normal file
6
src/driver/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod keyboard;
|
||||
pub mod timer;
|
||||
|
||||
pub fn init() {
|
||||
keyboard::init();
|
||||
}
|
||||
55
src/driver/timer.rs
Normal file
55
src/driver/timer.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use crate::{print, task::interrupt_wake};
|
||||
use core::future::Future;
|
||||
use core::{
|
||||
pin::Pin,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use futures_util::task::AtomicWaker;
|
||||
|
||||
static TICKS: AtomicU64 = AtomicU64::new(0);
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
|
||||
/// Called by the timer interrupt handler
|
||||
///
|
||||
/// Must not block (including spinlocks).
|
||||
pub(crate) fn tick() {
|
||||
TICKS.fetch_add(1, Ordering::Release);
|
||||
if let Some(waker) = WAKER.take() {
|
||||
interrupt_wake(waker);
|
||||
}
|
||||
}
|
||||
|
||||
fn next_tick() -> impl Future<Output = u64> {
|
||||
static NEXT_TICK: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
NextTick {
|
||||
ticks: NEXT_TICK.fetch_add(1, Ordering::Release),
|
||||
}
|
||||
}
|
||||
|
||||
struct NextTick {
|
||||
ticks: u64,
|
||||
}
|
||||
|
||||
impl Future for NextTick {
|
||||
type Output = u64;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<u64> {
|
||||
WAKER.register(&cx.waker());
|
||||
let current_ticks = TICKS.load(Ordering::Acquire);
|
||||
if self.ticks < current_ticks {
|
||||
self.ticks += 1;
|
||||
Poll::Ready(self.ticks)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn timer_task() {
|
||||
loop {
|
||||
next_tick().await;
|
||||
print!(".");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{gdt, hlt_loop, print, println};
|
||||
use crate::{gdt, hlt_loop, println};
|
||||
use lazy_static::lazy_static;
|
||||
use pic8259_simple::ChainedPics;
|
||||
use spin;
|
||||
@@ -72,7 +72,8 @@ extern "x86-interrupt" fn double_fault_handler(
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||
print!(".");
|
||||
crate::driver::timer::tick();
|
||||
|
||||
unsafe {
|
||||
PICS.lock()
|
||||
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
||||
@@ -80,28 +81,11 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptSt
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||
use spin::Mutex;
|
||||
use x86_64::instructions::port::Port;
|
||||
|
||||
lazy_static! {
|
||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> = Mutex::new(
|
||||
Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore)
|
||||
);
|
||||
}
|
||||
|
||||
let mut keyboard = KEYBOARD.lock();
|
||||
let mut port = Port::new(0x60);
|
||||
|
||||
let scancode: u8 = unsafe { port.read() };
|
||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
||||
match key {
|
||||
DecodedKey::Unicode(character) => print!("{}", character),
|
||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crate::driver::keyboard::keyboard_scancode(scancode);
|
||||
|
||||
unsafe {
|
||||
PICS.lock()
|
||||
|
||||
14
src/lib.rs
14
src/lib.rs
@@ -3,6 +3,12 @@
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![feature(abi_x86_interrupt)]
|
||||
#![feature(alloc_error_handler)]
|
||||
#![feature(const_fn)]
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(wake_trait)]
|
||||
#![feature(const_in_array_repeat_expressions)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(asm)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
@@ -11,17 +17,23 @@ extern crate alloc;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
pub mod allocator;
|
||||
pub mod driver;
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod task;
|
||||
pub mod vga_buffer;
|
||||
|
||||
pub fn init() {
|
||||
gdt::init();
|
||||
interrupts::init_idt();
|
||||
unsafe { interrupts::PICS.lock().initialize() };
|
||||
x86_64::instructions::interrupts::enable();
|
||||
}
|
||||
|
||||
pub fn init_heap_structures() {
|
||||
task::init();
|
||||
driver::init();
|
||||
}
|
||||
|
||||
pub fn test_runner(tests: &[&dyn Fn()]) {
|
||||
|
||||
34
src/main.rs
34
src/main.rs
@@ -26,6 +26,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
|
||||
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||
blog_os::init_heap_structures();
|
||||
|
||||
// allocate a number on the heap
|
||||
let heap_value = Box::new(41);
|
||||
@@ -51,11 +52,24 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
Rc::strong_count(&cloned_reference)
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
use blog_os::task::executor::Executor;
|
||||
|
||||
println!("It did not crash!");
|
||||
blog_os::hlt_loop();
|
||||
let mut executor = Executor::new();
|
||||
executor.spawn(bar());
|
||||
|
||||
executor.spawn(async {
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
});
|
||||
|
||||
executor.spawn(async {
|
||||
println!("It did not crash!");
|
||||
});
|
||||
|
||||
executor.spawn(blog_os::driver::timer::timer_task());
|
||||
executor.spawn(blog_os::driver::keyboard::keyboard_task());
|
||||
|
||||
executor.run();
|
||||
}
|
||||
|
||||
/// This function is called on panic.
|
||||
@@ -71,3 +85,15 @@ fn panic(info: &PanicInfo) -> ! {
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
blog_os::test_panic_handler(info)
|
||||
}
|
||||
|
||||
async fn test() -> u32 {
|
||||
42
|
||||
}
|
||||
|
||||
async fn foo() -> u32 {
|
||||
test().await * 2
|
||||
}
|
||||
|
||||
async fn bar() {
|
||||
println!("foo result: {}", foo().await);
|
||||
}
|
||||
|
||||
127
src/task/executor.rs
Normal file
127
src/task/executor.rs
Normal file
@@ -0,0 +1,127 @@
|
||||
use super::interrupt_wakeups::interrupt_wakeups;
|
||||
use crate::println;
|
||||
use alloc::{
|
||||
boxed::Box,
|
||||
collections::{BTreeMap, VecDeque},
|
||||
sync::Arc,
|
||||
task::Wake,
|
||||
};
|
||||
use core::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use crossbeam_queue::SegQueue;
|
||||
|
||||
pub type Task = Pin<Box<dyn Future<Output = ()>>>;
|
||||
type TaskId = usize;
|
||||
|
||||
pub struct Executor {
|
||||
task_queue: VecDeque<Task>,
|
||||
wake_queue: Arc<SegQueue<TaskId>>,
|
||||
pending_tasks: BTreeMap<TaskId, Task>,
|
||||
}
|
||||
|
||||
impl Executor {
|
||||
pub fn new() -> Self {
|
||||
Executor {
|
||||
task_queue: VecDeque::new(),
|
||||
wake_queue: Arc::new(SegQueue::new()),
|
||||
pending_tasks: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn(&mut self, task: impl Future<Output = ()> + 'static) {
|
||||
self.task_queue.push_back(Box::pin(task))
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> ! {
|
||||
loop {
|
||||
self.run_ready_tasks();
|
||||
self.apply_interrupt_wakeups();
|
||||
self.wake_waiting_tasks();
|
||||
self.hlt_if_idle();
|
||||
}
|
||||
}
|
||||
|
||||
fn run_ready_tasks(&mut self) {
|
||||
while let Some(mut task) = self.task_queue.pop_front() {
|
||||
let waker = self.create_waker(&task).into();
|
||||
let mut context = Context::from_waker(&waker);
|
||||
match task.as_mut().poll(&mut context) {
|
||||
Poll::Ready(()) => {} // task done
|
||||
Poll::Pending => {
|
||||
// add task to pending_tasks and wait for wakeup
|
||||
let task_id = Self::task_id(&task);
|
||||
if self.pending_tasks.insert(task_id, task).is_some() {
|
||||
panic!("Task with same ID already in pending_tasks");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Invoke wakers for tasks woken by interrupts
|
||||
///
|
||||
/// The interrupt handlers can't invoke the waker directly since wakers
|
||||
/// might execute arbitrary code, e.g. allocate, which should not be done
|
||||
/// in interrupt handlers to avoid deadlocks.
|
||||
fn apply_interrupt_wakeups(&mut self) {
|
||||
while let Ok(waker) = interrupt_wakeups().pop() {
|
||||
waker.wake();
|
||||
}
|
||||
}
|
||||
|
||||
fn wake_waiting_tasks(&mut self) {
|
||||
while let Ok(task_id) = self.wake_queue.pop() {
|
||||
if let Some(task) = self.pending_tasks.remove(&task_id) {
|
||||
self.task_queue.push_back(task);
|
||||
} else {
|
||||
println!("WARNING: woken task not found in pending_tasks");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes the `hlt` instruction if there are no ready tasks
|
||||
fn hlt_if_idle(&self) {
|
||||
if self.task_queue.is_empty() {
|
||||
// disable interrupts to avoid races
|
||||
x86_64::instructions::interrupts::disable();
|
||||
// check if relevant interrupts occured since the last check
|
||||
if interrupt_wakeups().is_empty() {
|
||||
// no interrupts occured -> hlt to wait for next interrupt
|
||||
x86_64::instructions::interrupts::enable_interrupts_and_hlt();
|
||||
} else {
|
||||
// there were some new wakeups -> continue execution
|
||||
x86_64::instructions::interrupts::enable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn task_id(task: &Task) -> TaskId {
|
||||
let future_ref: &dyn Future<Output = ()> = &**task;
|
||||
future_ref as *const _ as *const () as usize
|
||||
}
|
||||
|
||||
fn create_waker(&self, task: &Task) -> Arc<Waker> {
|
||||
Arc::new(Waker {
|
||||
wake_queue: self.wake_queue.clone(),
|
||||
task_id: Self::task_id(task),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Waker {
|
||||
wake_queue: Arc<SegQueue<TaskId>>,
|
||||
task_id: TaskId,
|
||||
}
|
||||
|
||||
impl Wake for Waker {
|
||||
fn wake(self: Arc<Self>) {
|
||||
self.wake_by_ref();
|
||||
}
|
||||
|
||||
fn wake_by_ref(self: &Arc<Self>) {
|
||||
self.wake_queue.push(self.task_id);
|
||||
}
|
||||
}
|
||||
25
src/task/interrupt_wakeups.rs
Normal file
25
src/task/interrupt_wakeups.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use crate::println;
|
||||
use conquer_once::spin::OnceCell;
|
||||
use core::task::Waker;
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
|
||||
static INTERRUPT_WAKEUPS: OnceCell<ArrayQueue<Waker>> = OnceCell::uninit();
|
||||
|
||||
pub fn init() {
|
||||
INTERRUPT_WAKEUPS
|
||||
.try_init_once(|| ArrayQueue::new(10))
|
||||
.expect("failed to init interrupt wakeup queue");
|
||||
}
|
||||
|
||||
/// Queues a waker for waking in an interrupt-safe way
|
||||
pub(crate) fn interrupt_wake(waker: Waker) {
|
||||
if let Err(_) = interrupt_wakeups().push(waker) {
|
||||
println!("WARNING: dropping interrupt wakeup");
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn interrupt_wakeups() -> &'static ArrayQueue<Waker> {
|
||||
INTERRUPT_WAKEUPS
|
||||
.try_get()
|
||||
.expect("interrupt wakeup queue not initialized")
|
||||
}
|
||||
8
src/task/mod.rs
Normal file
8
src/task/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub(crate) use interrupt_wakeups::interrupt_wake;
|
||||
|
||||
pub mod executor;
|
||||
mod interrupt_wakeups;
|
||||
|
||||
pub fn init() {
|
||||
interrupt_wakeups::init();
|
||||
}
|
||||
@@ -58,6 +58,18 @@ fn many_boxes() {
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn many_boxes_long_lived() {
|
||||
serial_print!("many_boxes_long_lived... ");
|
||||
let long_lived = Box::new(1); // new
|
||||
for i in 0..HEAP_SIZE {
|
||||
let x = Box::new(i);
|
||||
assert_eq!(*x, i);
|
||||
}
|
||||
assert_eq!(*long_lived, 1); // new
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
blog_os::test_panic_handler(info)
|
||||
|
||||
Reference in New Issue
Block a user