Compare commits

...

46 Commits

Author SHA1 Message Date
Philipp Oppermann
3d485535ae Refactor: Move interrupt wakers/operations into separate modules 2020-03-09 08:25:24 +01:00
Philipp Oppermann
08582948c5 Let executor enable interrupts 2020-03-08 14:51:51 +01:00
Philipp Oppermann
b3ba0ba4e9 Use new enable_interrupts_and_hlt function of x86_64 2020-03-08 14:50:58 +01:00
Philipp Oppermann
c3ea4190ca Merge branch 'post-11' into post-12-async-await 2020-03-08 14:48:33 +01:00
Philipp Oppermann
83b67df8af Merge branch 'post-10' into post-11 2020-03-08 14:39:38 +01:00
Philipp Oppermann
714d9cef7d Merge branch 'post-10' into post-11 2020-03-08 14:30:38 +01:00
Philipp Oppermann
3735ae51c9 Merge branch 'post-10' into post-11 2020-03-08 14:29:03 +01:00
Philipp Oppermann
f75d63853f Refactor Executor::run into separate methods 2020-03-08 13:49:36 +01:00
Philipp Oppermann
378159ce76 Simplify executor by removing separate Spawner type 2020-03-08 13:07:43 +01:00
Philipp Oppermann
a430d5ef06 Merge branch 'post-10' into post-11 2020-03-06 11:58:45 +01:00
Philipp Oppermann
a5ff4261a0 Handle timer interrupts using async task too 2020-02-28 19:09:15 +01:00
Philipp Oppermann
ea83d905fe Fix wrong calculation of task ID
Pin implements Deref too, so by dereferencing only once we get a `&Box` reference. Since Box also implements Future, the type of `future_ref` was still correct. To avoid this error in the future, we added an assertion to catch duplicate task IDson `pending_tasks.insert()`.
2020-02-28 19:08:31 +01:00
Philipp Oppermann
786a7a6922 Use crossbeam-queue and AtomicWaker for async keypress handling 2020-02-28 17:52:51 +01:00
Philipp Oppermann
6329274f02 Create a basic executor based on crossbeam_queue 2020-02-28 11:42:36 +01:00
Philipp Oppermann
f4331daa25 Merge branch 'post-10' into post-11 2020-02-26 12:48:22 +01:00
Philipp Oppermann
921dd54207 Merge branch 'post-10' into post-11 2020-02-26 12:22:10 +01:00
Philipp Oppermann
3a6d3153a4 Don't panic on overflow in allocator; return null pointer instead (#738) 2020-02-04 09:47:39 +01:00
Philipp Oppermann
9fb6c1d0bd Merge branch 'post-10' into post-11 2020-02-04 09:39:02 +01:00
Philipp Oppermann
00fedc801e Use LinkedListAllocator::lock instead of self.inner.lock() 2020-01-30 13:03:34 +01:00
Philipp Oppermann
0f74db4812 Implement align_up using align_offset from Rust's standard library (#723)
Improve `align_up` performance using a bitmask
2020-01-28 10:39:14 +01:00
Philipp Oppermann
93fd330ab9 Use bitmask instead of align_offset 2020-01-28 10:29:53 +01:00
Philipp Oppermann
3ad5f117c2 Use checked addition for allocator implementations (#726) 2020-01-27 13:25:08 +01:00
Philipp Oppermann
d1678f5a96 Implement align_up using align_offset from Rust's standard library 2020-01-22 11:35:29 +01:00
Philipp Oppermann
002d6f255f Set FixedSizeBlockAllocator as global allocator 2020-01-20 14:09:00 +01:00
Philipp Oppermann
6c3bf0b10f Implement GlobalAlloc::dealloc 2020-01-20 14:07:47 +01:00
Philipp Oppermann
7a792f5cb0 Implement GlobalAlloc::alloc 2020-01-20 14:07:02 +01:00
Philipp Oppermann
93b4dcf434 Add skeleton for GlobalAlloc implementation 2020-01-20 14:06:26 +01:00
Philipp Oppermann
821dd2adb4 Add function to calculate the list index 2020-01-20 14:05:24 +01:00
Philipp Oppermann
d636939b51 Add FixedSizeBlockAllocator::fallback_alloc method 2020-01-20 14:05:01 +01:00
Philipp Oppermann
9b7326541e Add FixedSizeBlockAllocator type 2020-01-20 14:04:13 +01:00
Philipp Oppermann
4f234b67ef Add ListNode type and BLOCK_SIZES constant 2020-01-20 14:02:57 +01:00
Philipp Oppermann
7381e11f3c Create a new fixed_size_block allocator submodule 2020-01-20 14:01:59 +01:00
Philipp Oppermann
a9fe65a0ce Use new LinkedListAllocator 2020-01-10 13:04:46 +01:00
Philipp Oppermann
2001814119 Implement LinkedListAllocator::size_align 2020-01-10 13:00:16 +01:00
Philipp Oppermann
a5c50e7408 Implement GlobalAlloc for LinkedListAllocator 2020-01-10 11:52:04 +01:00
Philipp Oppermann
70a52c291d Implement alloc_from_region 2020-01-10 11:48:56 +01:00
Philipp Oppermann
c56bfa27e4 Implement find_region 2020-01-10 11:46:10 +01:00
Philipp Oppermann
55aec9ebf3 Apply rustfmt to ListNode::new function 2020-01-10 11:44:38 +01:00
Philipp Oppermann
2e1d132a9a Implement add_free_region 2020-01-10 11:44:17 +01:00
Philipp Oppermann
63e8577d77 Create a basic LinkedListAllocator type 2020-01-10 11:42:04 +01:00
Philipp Oppermann
75d826bf69 Add a test that memory is reused with a long lived allocation
This test fails for the bump allocator because it can only free the complete heap at once, which is prevented by the single long-lived allocation.
2020-01-09 15:45:38 +01:00
Philipp Oppermann
45be3f0648 Use our BumpAllocator instead of linked_list_allocator crate 2020-01-09 15:37:43 +01:00
Philipp Oppermann
055c560a7a Add an align_up function 2020-01-09 15:36:06 +01:00
Philipp Oppermann
e87044a7ee Implement GlobalAlloc for BumpAllocator 2020-01-09 15:35:03 +01:00
Philipp Oppermann
08d2289dad Add a Locked wrapper type that can be used to implement GlobalAlloc 2020-01-09 15:34:04 +01:00
Philipp Oppermann
7c84dbaa1d Create a basic BumpAllocator type 2020-01-09 15:25:37 +01:00
16 changed files with 786 additions and 28 deletions

79
Cargo.lock generated
View File

@@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # 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]] [[package]]
name = "bit_field" name = "bit_field"
version = "0.9.0" version = "0.9.0"
@@ -17,6 +23,9 @@ name = "blog_os"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bootloader", "bootloader",
"conquer-once",
"crossbeam-queue",
"futures-util",
"lazy_static", "lazy_static",
"linked_list_allocator", "linked_list_allocator",
"pc-keyboard", "pc-keyboard",
@@ -33,12 +42,76 @@ version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3ed4f735c4e455ba86a3d2939b1c0729414153642106c9d035693355630a42c" 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]] [[package]]
name = "cpuio" name = "cpuio"
version = "0.2.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22b8e308ccfc5acf3b82f79c0eac444cf6114cb2ac67a230ca6c177210068daa" 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]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -81,6 +154,12 @@ dependencies = [
"cpuio", "cpuio",
] ]
[[package]]
name = "pin-utils"
version = "0.1.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"

View File

@@ -26,6 +26,19 @@ linked_list_allocator = "0.8.0"
version = "1.0" version = "1.0"
features = ["spin_no_std"] 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] [package.metadata.bootimage]
test-args = [ test-args = [

View File

@@ -1,6 +1,6 @@
use alloc::alloc::{GlobalAlloc, Layout}; use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut; use core::ptr::null_mut;
use linked_list_allocator::LockedHeap; use fixed_size_block::FixedSizeBlockAllocator;
use x86_64::{ use x86_64::{
structures::paging::{ structures::paging::{
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
@@ -8,11 +8,15 @@ use x86_64::{
VirtAddr, 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_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
#[global_allocator] #[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty(); static ALLOCATOR: Locked<FixedSizeBlockAllocator> = Locked::new(FixedSizeBlockAllocator::new());
pub fn init_heap( pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>, mapper: &mut impl Mapper<Size4KiB>,
@@ -52,3 +56,27 @@ unsafe impl GlobalAlloc for Dummy {
panic!("dealloc should be never called") 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
View 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;
}
}
}

View 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);
}
}
}
}

View 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(&region, 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
View 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
View File

@@ -0,0 +1,6 @@
pub mod keyboard;
pub mod timer;
pub fn init() {
keyboard::init();
}

55
src/driver/timer.rs Normal file
View 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!(".");
}
}

View File

@@ -1,4 +1,4 @@
use crate::{gdt, hlt_loop, print, println}; use crate::{gdt, hlt_loop, println};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use pic8259_simple::ChainedPics; use pic8259_simple::ChainedPics;
use spin; use spin;
@@ -72,7 +72,8 @@ extern "x86-interrupt" fn double_fault_handler(
} }
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) { extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
print!("."); crate::driver::timer::tick();
unsafe { unsafe {
PICS.lock() PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8()); .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) { 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; 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 mut port = Port::new(0x60);
let scancode: u8 = unsafe { port.read() }; 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) { crate::driver::keyboard::keyboard_scancode(scancode);
match key {
DecodedKey::Unicode(character) => print!("{}", character),
DecodedKey::RawKey(key) => print!("{:?}", key),
}
}
}
unsafe { unsafe {
PICS.lock() PICS.lock()

View File

@@ -3,6 +3,12 @@
#![feature(custom_test_frameworks)] #![feature(custom_test_frameworks)]
#![feature(abi_x86_interrupt)] #![feature(abi_x86_interrupt)]
#![feature(alloc_error_handler)] #![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)] #![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]
@@ -11,17 +17,23 @@ extern crate alloc;
use core::panic::PanicInfo; use core::panic::PanicInfo;
pub mod allocator; pub mod allocator;
pub mod driver;
pub mod gdt; pub mod gdt;
pub mod interrupts; pub mod interrupts;
pub mod memory; pub mod memory;
pub mod serial; pub mod serial;
pub mod task;
pub mod vga_buffer; pub mod vga_buffer;
pub fn init() { pub fn init() {
gdt::init(); gdt::init();
interrupts::init_idt(); interrupts::init_idt();
unsafe { interrupts::PICS.lock().initialize() }; 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()]) { pub fn test_runner(tests: &[&dyn Fn()]) {

View File

@@ -26,6 +26,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) }; let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed"); allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
blog_os::init_heap_structures();
// allocate a number on the heap // allocate a number on the heap
let heap_value = Box::new(41); let heap_value = Box::new(41);
@@ -51,11 +52,24 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
Rc::strong_count(&cloned_reference) Rc::strong_count(&cloned_reference)
); );
#[cfg(test)] use blog_os::task::executor::Executor;
test_main();
println!("It did not crash!"); let mut executor = Executor::new();
blog_os::hlt_loop(); 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. /// This function is called on panic.
@@ -71,3 +85,15 @@ fn panic(info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
blog_os::test_panic_handler(info) 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
View 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);
}
}

View 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
View File

@@ -0,0 +1,8 @@
pub(crate) use interrupt_wakeups::interrupt_wake;
pub mod executor;
mod interrupt_wakeups;
pub fn init() {
interrupt_wakeups::init();
}

View File

@@ -58,6 +58,18 @@ fn many_boxes() {
serial_println!("[ok]"); 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] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
blog_os::test_panic_handler(info) blog_os::test_panic_handler(info)