mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
58 Commits
post-12-wi
...
post-12-as
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d485535ae | ||
|
|
08582948c5 | ||
|
|
b3ba0ba4e9 | ||
|
|
c3ea4190ca | ||
|
|
83b67df8af | ||
|
|
02c36bae29 | ||
|
|
70deb3168e | ||
|
|
9d4a960bcb | ||
|
|
564ba9f9ac | ||
|
|
8a4f3ab2a8 | ||
|
|
e402af005b | ||
|
|
d7e505da9e | ||
|
|
714d9cef7d | ||
|
|
7b0d8f05ba | ||
|
|
3735ae51c9 | ||
|
|
f230ee4258 | ||
|
|
a5d409b777 | ||
|
|
33df0bb2a3 | ||
|
|
9a61e1724b | ||
|
|
dbe1353fd2 | ||
|
|
813bc41a74 | ||
|
|
2039cd7a64 | ||
|
|
f75d63853f | ||
|
|
378159ce76 | ||
|
|
a430d5ef06 | ||
|
|
39f14b6d0b | ||
|
|
dd1daf2652 | ||
|
|
a5ff4261a0 | ||
|
|
ea83d905fe | ||
|
|
786a7a6922 | ||
|
|
6329274f02 | ||
|
|
f4331daa25 | ||
|
|
9a415c85b9 | ||
|
|
79a0172ace | ||
|
|
9af6337f67 | ||
|
|
725e03b5c5 | ||
|
|
16afb268f8 | ||
|
|
5ef73aaa30 | ||
|
|
ebb862de2a | ||
|
|
d588dca953 | ||
|
|
f23269ed93 | ||
|
|
46264d08ca | ||
|
|
921dd54207 | ||
|
|
c7cc718ae3 | ||
|
|
8fc746555d | ||
|
|
9324ea45a5 | ||
|
|
d35ab51629 | ||
|
|
de509e058f | ||
|
|
2a8f499f73 | ||
|
|
2634bb2d37 | ||
|
|
3a6d3153a4 | ||
|
|
9fb6c1d0bd | ||
|
|
5ed04baab0 | ||
|
|
00fedc801e | ||
|
|
0f74db4812 | ||
|
|
93fd330ab9 | ||
|
|
3ad5f117c2 | ||
|
|
d1678f5a96 |
162
Cargo.lock
generated
162
Cargo.lock
generated
@@ -1,13 +1,10 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "array-init"
|
||||
version = "0.0.4"
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23589ecb866b460d3a0f1278834750268c607e8e28a1b982c907219f3178cd72"
|
||||
dependencies = [
|
||||
"nodrop",
|
||||
]
|
||||
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
@@ -26,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,30 +33,85 @@ dependencies = [
|
||||
"spin",
|
||||
"uart_16550",
|
||||
"volatile",
|
||||
"x86_64 0.8.1",
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bootloader"
|
||||
version = "0.8.3"
|
||||
version = "0.8.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d596849a47f28abdea62d7a6a25c4f6e69c3d9b09b0a2877db6e9cda004ca993"
|
||||
checksum = "c3ed4f735c4e455ba86a3d2939b1c0729414153642106c9d035693355630a42c"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.2.3"
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "conquer-once"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f7644600a548ecad74e4a918392af1798f7dd045be610be3203b9e129b4f98f"
|
||||
dependencies = [
|
||||
"rustc_version",
|
||||
"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"
|
||||
@@ -68,24 +123,27 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "linked_list_allocator"
|
||||
version = "0.6.4"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47314ec1d29aa869ee7cb5a5be57be9b1055c56567d59c3fb6689926743e0bea"
|
||||
checksum = "18c618c431dfe4419afbe22852f6aceffbc17bd82ba0a18b982def291000824c"
|
||||
dependencies = [
|
||||
"spin",
|
||||
"spinning_top",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nodrop"
|
||||
version = "0.1.14"
|
||||
name = "lock_api"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
||||
checksum = "79b2de95ecb4691949fea4716ca53cdbcfccb2c612e19644a8bad05edcf9f47b"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pc-keyboard"
|
||||
version = "0.3.1"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fff50ab09ba31bcebc0669f4e64c0952fae1acdca9e6e0587e68e4e8443808ac"
|
||||
checksum = "c48392db76c4e9a69e0b3be356c5f97ebb7b14413c5e4fd0af4755dbf86e2fce"
|
||||
|
||||
[[package]]
|
||||
name = "pic8259_simple"
|
||||
@@ -97,28 +155,16 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
name = "pin-utils"
|
||||
version = "0.1.0-alpha.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
dependencies = [
|
||||
"semver-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver-parser"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
@@ -127,20 +173,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "uart_16550"
|
||||
version = "0.2.1"
|
||||
name = "spinning_top"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "803ea8cb602dbb32c1a657a866d2dd79fe7dbeab0fb2ac667cb4dcc7de12a58b"
|
||||
checksum = "32d801a3a53bcf5071f85fef8d5cab9e5f638fc5580a37e6eb7aba4b37438d24"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"x86_64 0.7.7",
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ux"
|
||||
version = "0.1.3"
|
||||
name = "uart_16550"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88dfeb711b61ce620c0cb6fd9f8e3e678622f0c971da2a63c4b3e25e88ed012f"
|
||||
checksum = "d44b0f30cb82b0fbc15b78ade1064226529ad52028bc8cb8accb98ff6f3d7131"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "volatile"
|
||||
@@ -150,25 +199,10 @@ checksum = "6af0edf5b4faacc31fc51159244d78d65ec580f021afcef7bd53c04aeabc7f29"
|
||||
|
||||
[[package]]
|
||||
name = "x86_64"
|
||||
version = "0.7.7"
|
||||
version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f27d9168654aee1b0c1b73746caeb4aa33248f8b8c8f6e100e697fcc2a794b2"
|
||||
checksum = "4206b60c9f99766329b66962aa8ddc01df6c7edd02edc046b7a69d5df9fcdbcf"
|
||||
dependencies = [
|
||||
"array-init",
|
||||
"bit_field",
|
||||
"bitflags",
|
||||
"cast",
|
||||
"ux",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "x86_64"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f21672dcbed52bc09eea030d189600c0189c66c97bc5b31779eb780e064a201f"
|
||||
dependencies = [
|
||||
"array-init",
|
||||
"bit_field",
|
||||
"bitflags",
|
||||
"cast",
|
||||
]
|
||||
|
||||
19
Cargo.toml
19
Cargo.toml
@@ -16,16 +16,29 @@ harness = false
|
||||
bootloader = { version = "0.8.0", features = ["map_physical_memory"]}
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
x86_64 = "0.8.1"
|
||||
x86_64 = "0.9.6"
|
||||
uart_16550 = "0.2.0"
|
||||
pic8259_simple = "0.1.1"
|
||||
pc-keyboard = "0.3.1"
|
||||
linked_list_allocator = "0.6.4"
|
||||
pc-keyboard = "0.5.0"
|
||||
linked_list_allocator = "0.8.0"
|
||||
|
||||
[dependencies.lazy_static]
|
||||
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 = [
|
||||
|
||||
@@ -21,7 +21,7 @@ static ALLOCATOR: Locked<FixedSizeBlockAllocator> = Locked::new(FixedSizeBlockAl
|
||||
pub fn init_heap(
|
||||
mapper: &mut impl Mapper<Size4KiB>,
|
||||
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
||||
) -> Result<(), MapToError> {
|
||||
) -> Result<(), MapToError<Size4KiB>> {
|
||||
let page_range = {
|
||||
let heap_start = VirtAddr::new(HEAP_START as u64);
|
||||
let heap_end = heap_start + HEAP_SIZE - 1u64;
|
||||
@@ -74,11 +74,9 @@ impl<A> Locked<A> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
let remainder = addr % align;
|
||||
if remainder == 0 {
|
||||
addr // addr already aligned
|
||||
} else {
|
||||
addr - remainder + align
|
||||
}
|
||||
(addr + align - 1) & !(align - 1)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ impl BumpAllocator {
|
||||
/// 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 + heap_size;
|
||||
self.heap_end = heap_start.saturating_add(heap_size);
|
||||
self.next = heap_start;
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,10 @@ unsafe impl GlobalAlloc for Locked<BumpAllocator> {
|
||||
let mut bump = self.lock(); // get a mutable reference
|
||||
|
||||
let alloc_start = align_up(bump.next, layout.align());
|
||||
let alloc_end = alloc_start + layout.size();
|
||||
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
|
||||
|
||||
@@ -86,7 +86,7 @@ impl LinkedListAllocator {
|
||||
/// 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 + size;
|
||||
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
|
||||
|
||||
if alloc_end > region.end_addr() {
|
||||
// region too small
|
||||
@@ -122,10 +122,10 @@ 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.inner.lock();
|
||||
let mut allocator = self.lock();
|
||||
|
||||
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
|
||||
let alloc_end = alloc_start + size;
|
||||
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);
|
||||
@@ -140,6 +140,6 @@ unsafe impl GlobalAlloc for Locked<LinkedListAllocator> {
|
||||
// perform layout adjustments
|
||||
let (size, _) = LinkedListAllocator::size_align(layout);
|
||||
|
||||
self.inner.lock().add_free_region(ptr as usize, size)
|
||||
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,27 +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, 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));
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
11
src/lib.rs
11
src/lib.rs
@@ -5,7 +5,10 @@
|
||||
#![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"]
|
||||
|
||||
@@ -14,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()]) {
|
||||
|
||||
28
src/main.rs
28
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)
|
||||
);
|
||||
|
||||
use blog_os::task::executor::Executor;
|
||||
|
||||
let mut executor = Executor::new();
|
||||
executor.spawn(bar());
|
||||
|
||||
executor.spawn(async {
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
});
|
||||
|
||||
executor.spawn(async {
|
||||
println!("It did not crash!");
|
||||
blog_os::hlt_loop();
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user