mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
8 Commits
post-12-sc
...
post-12-wi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d21a78dc0b | ||
|
|
0186f65ece | ||
|
|
2772abc8eb | ||
|
|
51f90236a5 | ||
|
|
90abd5c8c5 | ||
|
|
a6273614e4 | ||
|
|
3a2a468a0b | ||
|
|
05f6abd261 |
67
Cargo.lock
generated
67
Cargo.lock
generated
@@ -24,6 +24,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bootloader",
|
"bootloader",
|
||||||
"conquer-once",
|
"conquer-once",
|
||||||
|
"cooked-waker",
|
||||||
"crossbeam-queue",
|
"crossbeam-queue",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
@@ -38,7 +39,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bootloader"
|
name = "bootloader"
|
||||||
version = "0.8.8"
|
version = "0.8.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "152a28c753e229e037e910b4cd4cd16a90c53dd9a67fd751fa304b4b4a03970c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
@@ -61,6 +64,27 @@ 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 = "654fb2472cc369d311c547103a1fa81d467bef370ae7a0680f65939895b1182a"
|
checksum = "654fb2472cc369d311c547103a1fa81d467bef370ae7a0680f65939895b1182a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cooked-waker"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0032e8be0680b63daf14b0fd7fd57f85fd6897951e110b041d32e839a831914"
|
||||||
|
dependencies = [
|
||||||
|
"cooked-waker-derive",
|
||||||
|
"stowaway",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cooked-waker-derive"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "74ded02b04a8ff53ea7328cd71297cde598bee70d165ad92512bdb7e20be9d74"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpuio"
|
name = "cpuio"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -158,6 +182,24 @@ version = "0.1.0-alpha.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
|
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -179,6 +221,23 @@ dependencies = [
|
|||||||
"lock_api",
|
"lock_api",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "stowaway"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32b13d61e782863c0eaeeaf7e8e580dc956a625851fd2fb73e5e92db9601c891"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uart_16550"
|
name = "uart_16550"
|
||||||
version = "0.2.4"
|
version = "0.2.4"
|
||||||
@@ -189,6 +248,12 @@ dependencies = [
|
|||||||
"x86_64",
|
"x86_64",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "volatile"
|
name = "volatile"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ name = "stack_overflow"
|
|||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bootloader = { version = "0.8.0", features = ["map_physical_memory"]}
|
bootloader = { version = "0.8.8", features = ["map_physical_memory"]}
|
||||||
volatile = "0.2.6"
|
volatile = "0.2.6"
|
||||||
spin = "0.5.2"
|
spin = "0.5.2"
|
||||||
x86_64 = "0.9.6"
|
x86_64 = "0.9.6"
|
||||||
@@ -21,6 +21,7 @@ uart_16550 = "0.2.0"
|
|||||||
pic8259_simple = "0.1.1"
|
pic8259_simple = "0.1.1"
|
||||||
pc-keyboard = "0.5.0"
|
pc-keyboard = "0.5.0"
|
||||||
linked_list_allocator = "0.8.0"
|
linked_list_allocator = "0.8.0"
|
||||||
|
cooked-waker = "1.0.2"
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
@@ -38,7 +39,7 @@ default-features = false
|
|||||||
[dependencies.futures-util]
|
[dependencies.futures-util]
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["alloc", "async-await"]
|
features = ["alloc"]
|
||||||
|
|
||||||
[package.metadata.bootimage]
|
[package.metadata.bootimage]
|
||||||
test-args = [
|
test-args = [
|
||||||
|
|||||||
@@ -80,28 +80,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) {
|
crate::task::keyboard::add_scancode(scancode);
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
PICS.lock()
|
PICS.lock()
|
||||||
|
|||||||
@@ -6,8 +6,6 @@
|
|||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
#![feature(alloc_layout_extra)]
|
#![feature(alloc_layout_extra)]
|
||||||
#![feature(const_in_array_repeat_expressions)]
|
#![feature(const_in_array_repeat_expressions)]
|
||||||
#![feature(wake_trait)]
|
|
||||||
#![feature(async_closure)]
|
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ entry_point!(kernel_main);
|
|||||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||||
use blog_os::allocator;
|
use blog_os::allocator;
|
||||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||||
use blog_os::task::{simple_executor::SimpleExecutor, Task, keyboard};
|
use blog_os::task::{keyboard, simple_executor::SimpleExecutor, Task};
|
||||||
use x86_64::VirtAddr;
|
use x86_64::VirtAddr;
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|||||||
@@ -1,11 +1,32 @@
|
|||||||
use conquer_once::spin::OnceCell;
|
|
||||||
use crossbeam_queue::ArrayQueue;
|
|
||||||
use futures_util::stream::{Stream, StreamExt};
|
|
||||||
use core::{pin::Pin, task::{Context, Poll}};
|
|
||||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
|
||||||
use crate::print;
|
use crate::print;
|
||||||
|
use crate::println;
|
||||||
|
use conquer_once::spin::OnceCell;
|
||||||
|
use core::{
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use crossbeam_queue::ArrayQueue;
|
||||||
|
use futures_util::stream::StreamExt;
|
||||||
|
use futures_util::{stream::Stream, task::AtomicWaker};
|
||||||
|
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||||
|
|
||||||
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
||||||
|
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||||
|
|
||||||
|
/// Called by the keyboard interrupt handler
|
||||||
|
///
|
||||||
|
/// Must not block or allocate.
|
||||||
|
pub(crate) fn add_scancode(scancode: u8) {
|
||||||
|
if let Ok(queue) = SCANCODE_QUEUE.try_get() {
|
||||||
|
if let Err(_) = queue.push(scancode) {
|
||||||
|
println!("WARNING: scancode queue full; dropping keyboard input");
|
||||||
|
} else {
|
||||||
|
WAKER.wake();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("WARNING: scancode queue uninitialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ScancodeStream {
|
pub struct ScancodeStream {
|
||||||
_private: (),
|
_private: (),
|
||||||
@@ -13,21 +34,27 @@ pub struct ScancodeStream {
|
|||||||
|
|
||||||
impl ScancodeStream {
|
impl ScancodeStream {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
SCANCODE_QUEUE.try_init_once(|| ArrayQueue::new(100))
|
SCANCODE_QUEUE
|
||||||
|
.try_init_once(|| ArrayQueue::new(100))
|
||||||
.expect("ScancodeStream::new should only be called once");
|
.expect("ScancodeStream::new should only be called once");
|
||||||
ScancodeStream {
|
ScancodeStream { _private: () }
|
||||||
_private: (),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stream for ScancodeStream {
|
impl Stream for ScancodeStream {
|
||||||
type Item = u8;
|
type Item = u8;
|
||||||
|
|
||||||
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<u8>> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<u8>> {
|
||||||
let queue = SCANCODE_QUEUE
|
let queue = SCANCODE_QUEUE
|
||||||
.try_get()
|
.try_get()
|
||||||
.expect("scancode queue not initialized");
|
.expect("scancode queue not initialized");
|
||||||
|
|
||||||
|
// fast path
|
||||||
|
if let Ok(scancode) = queue.pop() {
|
||||||
|
return Poll::Ready(Some(scancode));
|
||||||
|
}
|
||||||
|
|
||||||
|
WAKER.register(&cx.waker());
|
||||||
match queue.pop() {
|
match queue.pop() {
|
||||||
Ok(scancode) => Poll::Ready(Some(scancode)),
|
Ok(scancode) => Poll::Ready(Some(scancode)),
|
||||||
Err(crossbeam_queue::PopError) => Poll::Pending,
|
Err(crossbeam_queue::PopError) => Poll::Pending,
|
||||||
|
|||||||
@@ -19,4 +19,8 @@ impl Task {
|
|||||||
fn poll(&mut self, context: &mut Context) -> Poll<()> {
|
fn poll(&mut self, context: &mut Context) -> Poll<()> {
|
||||||
self.future.as_mut().poll(context)
|
self.future.as_mut().poll(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn id(&self) -> usize {
|
||||||
|
&*self.future as *const _ as *const () as usize
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,24 @@
|
|||||||
use super::Task;
|
use super::Task;
|
||||||
use alloc::{collections::VecDeque, sync::Arc, task::Wake};
|
use alloc::{
|
||||||
use core::task::{Context, Poll, Waker};
|
collections::{BTreeMap, VecDeque},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
use cooked_waker::IntoWaker;
|
||||||
|
use core::task::{Context, Poll};
|
||||||
|
use crossbeam_queue::ArrayQueue;
|
||||||
|
|
||||||
pub struct SimpleExecutor {
|
pub struct SimpleExecutor {
|
||||||
task_queue: VecDeque<Task>,
|
task_queue: VecDeque<Task>,
|
||||||
|
waiting_tasks: BTreeMap<usize, Task>,
|
||||||
|
wake_queue: Arc<ArrayQueue<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimpleExecutor {
|
impl SimpleExecutor {
|
||||||
pub fn new() -> SimpleExecutor {
|
pub fn new() -> SimpleExecutor {
|
||||||
SimpleExecutor {
|
SimpleExecutor {
|
||||||
task_queue: VecDeque::new(),
|
task_queue: VecDeque::new(),
|
||||||
|
waiting_tasks: BTreeMap::new(),
|
||||||
|
wake_queue: Arc::new(ArrayQueue::new(100)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,27 +27,60 @@ impl SimpleExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self) {
|
||||||
|
loop {
|
||||||
|
self.handle_wakeups();
|
||||||
|
self.run_ready_tasks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_wakeups(&mut self) {
|
||||||
|
while let Ok(task_id) = self.wake_queue.pop() {
|
||||||
|
if let Some(task) = self.waiting_tasks.remove(&task_id) {
|
||||||
|
self.task_queue.push_back(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_ready_tasks(&mut self) {
|
||||||
while let Some(mut task) = self.task_queue.pop_front() {
|
while let Some(mut task) = self.task_queue.pop_front() {
|
||||||
let waker = DummyWaker.to_waker();
|
let waker = TaskWaker {
|
||||||
|
task_id: task.id(),
|
||||||
|
wake_queue: self.wake_queue.clone(),
|
||||||
|
}
|
||||||
|
.into_waker();
|
||||||
let mut context = Context::from_waker(&waker);
|
let mut context = Context::from_waker(&waker);
|
||||||
match task.poll(&mut context) {
|
match task.poll(&mut context) {
|
||||||
Poll::Ready(()) => {} // task done
|
Poll::Ready(()) => {} // task done
|
||||||
Poll::Pending => self.task_queue.push_back(task),
|
Poll::Pending => {
|
||||||
|
if self.waiting_tasks.insert(task.id(), task).is_some() {
|
||||||
|
panic!("Same task inserted into waiting_tasks twice");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DummyWaker;
|
#[derive(Debug, Clone, IntoWaker)]
|
||||||
|
struct TaskWaker {
|
||||||
|
task_id: usize,
|
||||||
|
wake_queue: Arc<ArrayQueue<usize>>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Wake for DummyWaker {
|
impl TaskWaker {
|
||||||
fn wake(self: Arc<Self>) {
|
fn wake_task(&self) {
|
||||||
// do nothing
|
self.wake_queue.push(self.task_id).expect("wake queue full");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DummyWaker {
|
impl cooked_waker::WakeRef for TaskWaker {
|
||||||
fn to_waker(self) -> Waker {
|
fn wake_by_ref(&self) {
|
||||||
Waker::from(Arc::new(self))
|
self.wake_task();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl cooked_waker::Wake for TaskWaker {
|
||||||
|
fn wake(self) {
|
||||||
|
self.wake_task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user