Skip to content

Commit

Permalink
Rework driver subsystem
Browse files Browse the repository at this point in the history
- Remove the panic version of the GPIO and UART driver. While they were a neat
  idea, it proved tedious to drag them along different tutorials where the
  virtual memory situation kept on changing. Actually, not much is lost, since
  the benefit was only of theoretical nature until now, since everything is
  still single-threaded with NullLocks. It is still possible to re-introduce
  them later.

- Refactor driver bringup starting with tutorial 14. Instantiating the drivers
  only when we are already capable of using the remapped MMIO address makes the
  kernel a lot more robust, and the drivers need not care whether their MMIO
  addresses are good to use already or not.

- Use console and irq_manager references from the generic kernel code. This
  improves decoupling from the BSP, and is needed as a basis for tutorial 14.
  • Loading branch information
andre-richter committed May 16, 2022
1 parent 4ab609b commit fec4f9b
Show file tree
Hide file tree
Showing 284 changed files with 4,839 additions and 4,643 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
**/kernel8.img

node_modules
.bundle
.vendor

Gemfile.lock
package-lock.json
package*.json
19 changes: 16 additions & 3 deletions 03_hacky_hello_world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ diff -uNr 02_runtime_init/src/bsp/raspberrypi.rs 03_hacky_hello_world/src/bsp/ra
diff -uNr 02_runtime_init/src/console.rs 03_hacky_hello_world/src/console.rs
--- 02_runtime_init/src/console.rs
+++ 03_hacky_hello_world/src/console.rs
@@ -0,0 +1,19 @@
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: MIT OR Apache-2.0
+//
+// Copyright (c) 2018-2022 Andre Richter <[email protected]>
+
+//! System console.
+
+use crate::bsp;
+
+//--------------------------------------------------------------------------------------------------
+// Public Definitions
+//--------------------------------------------------------------------------------------------------
Expand All @@ -208,6 +210,17 @@ diff -uNr 02_runtime_init/src/console.rs 03_hacky_hello_world/src/console.rs
+ /// intention.
+ pub use core::fmt::Write;
+}
+
+//--------------------------------------------------------------------------------------------------
+// Public Code
+//--------------------------------------------------------------------------------------------------
+
+/// Return a reference to the console.
+///
+/// This is the global console used by all printing macros.
+pub fn console() -> impl interface::Write {
+ bsp::console::console()
+}

diff -uNr 02_runtime_init/src/main.rs 03_hacky_hello_world/src/main.rs
--- 02_runtime_init/src/main.rs
Expand Down Expand Up @@ -317,7 +330,7 @@ diff -uNr 02_runtime_init/src/print.rs 03_hacky_hello_world/src/print.rs
+
+//! Printing.
+
+use crate::{bsp, console};
+use crate::console;
+use core::fmt;
+
+//--------------------------------------------------------------------------------------------------
Expand All @@ -328,7 +341,7 @@ diff -uNr 02_runtime_init/src/print.rs 03_hacky_hello_world/src/print.rs
+pub fn _print(args: fmt::Arguments) {
+ use console::interface::Write;
+
+ bsp::console::console().write_fmt(args).unwrap();
+ console::console().write_fmt(args).unwrap();
+}
+
+/// Prints without a newline.
Expand Down
13 changes: 13 additions & 0 deletions 03_hacky_hello_world/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! System console.
use crate::bsp;

//--------------------------------------------------------------------------------------------------
// Public Definitions
//--------------------------------------------------------------------------------------------------
Expand All @@ -17,3 +19,14 @@ pub mod interface {
/// intention.
pub use core::fmt::Write;
}

//--------------------------------------------------------------------------------------------------
// Public Code
//--------------------------------------------------------------------------------------------------

/// Return a reference to the console.
///
/// This is the global console used by all printing macros.
pub fn console() -> impl interface::Write {
bsp::console::console()
}
4 changes: 2 additions & 2 deletions 03_hacky_hello_world/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Printing.
use crate::{bsp, console};
use crate::console;
use core::fmt;

//--------------------------------------------------------------------------------------------------
Expand All @@ -15,7 +15,7 @@ use core::fmt;
pub fn _print(args: fmt::Arguments) {
use console::interface::Write;

bsp::console::console().write_fmt(args).unwrap();
console::console().write_fmt(args).unwrap();
}

/// Prints without a newline.
Expand Down
46 changes: 34 additions & 12 deletions 04_safe_globals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ diff -uNr 03_hacky_hello_world/src/bsp/raspberrypi/console.rs 04_safe_globals/sr
}

Ok(())
@@ -41,7 +80,37 @@
@@ -41,7 +80,39 @@
// Public Code
//--------------------------------------------------------------------------------------------------

Expand All @@ -164,9 +164,9 @@ diff -uNr 03_hacky_hello_world/src/bsp/raspberrypi/console.rs 04_safe_globals/sr
/// Return a reference to the console.
-pub fn console() -> impl console::interface::Write {
- QEMUOutput {}
+pub fn console() -> &'static impl console::interface::All {
+pub fn console() -> &'static dyn console::interface::All {
+ &QEMU_OUTPUT
+}
}
+
+//------------------------------------------------------------------------------
+// OS Interface Code
Expand All @@ -187,12 +187,14 @@ diff -uNr 03_hacky_hello_world/src/bsp/raspberrypi/console.rs 04_safe_globals/sr
+ fn chars_written(&self) -> usize {
+ self.inner.lock(|inner| inner.chars_written)
+ }
}
+}
+
+impl console::interface::All for QEMUOutput {}

diff -uNr 03_hacky_hello_world/src/console.rs 04_safe_globals/src/console.rs
--- 03_hacky_hello_world/src/console.rs
+++ 04_safe_globals/src/console.rs
@@ -10,10 +10,22 @@
@@ -12,12 +12,24 @@

/// Console interfaces.
pub mod interface {
Expand All @@ -218,7 +220,17 @@ diff -uNr 03_hacky_hello_world/src/console.rs 04_safe_globals/src/console.rs
+ }
+
+ /// Trait alias for a full-fledged console.
+ pub trait All = Write + Statistics;
+ pub trait All: Write + Statistics {}
}

//--------------------------------------------------------------------------------------------------
@@ -27,6 +39,6 @@
/// Return a reference to the console.
///
/// This is the global console used by all printing macros.
-pub fn console() -> impl interface::Write {
+pub fn console() -> &'static dyn interface::All {
bsp::console::console()
}

diff -uNr 03_hacky_hello_world/src/main.rs 04_safe_globals/src/main.rs
Expand All @@ -240,25 +252,35 @@ diff -uNr 03_hacky_hello_world/src/main.rs 04_safe_globals/src/main.rs

/// Early init code.
///
@@ -124,7 +126,15 @@
@@ -124,7 +126,12 @@
///
/// - Only a single core must be active and running this function.
unsafe fn kernel_init() -> ! {
- println!("Hello from Rust!");
+ use console::interface::Statistics;
+ use console::console;

- panic!("Stopping here.")
+ println!("[0] Hello from Rust!");
+
+ println!(
+ "[1] Chars written: {}",
+ bsp::console::console().chars_written()
+ );
+ println!("[1] Chars written: {}", console().chars_written());
+
+ println!("[2] Stopping here.");
+ cpu::wait_forever()
}

diff -uNr 03_hacky_hello_world/src/print.rs 04_safe_globals/src/print.rs
--- 03_hacky_hello_world/src/print.rs
+++ 04_safe_globals/src/print.rs
@@ -13,8 +13,6 @@

#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
- use console::interface::Write;
-
console::console().write_fmt(args).unwrap();
}


diff -uNr 03_hacky_hello_world/src/synchronization.rs 04_safe_globals/src/synchronization.rs
--- 03_hacky_hello_world/src/synchronization.rs
+++ 04_safe_globals/src/synchronization.rs
Expand Down
4 changes: 3 additions & 1 deletion 04_safe_globals/src/bsp/raspberrypi/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl QEMUOutput {
}

/// Return a reference to the console.
pub fn console() -> &'static impl console::interface::All {
pub fn console() -> &'static dyn console::interface::All {
&QEMU_OUTPUT
}

Expand All @@ -114,3 +114,5 @@ impl console::interface::Statistics for QEMUOutput {
self.inner.lock(|inner| inner.chars_written)
}
}

impl console::interface::All for QEMUOutput {}
15 changes: 14 additions & 1 deletion 04_safe_globals/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! System console.
use crate::bsp;

//--------------------------------------------------------------------------------------------------
// Public Definitions
//--------------------------------------------------------------------------------------------------
Expand All @@ -27,5 +29,16 @@ pub mod interface {
}

/// Trait alias for a full-fledged console.
pub trait All = Write + Statistics;
pub trait All: Write + Statistics {}
}

//--------------------------------------------------------------------------------------------------
// Public Code
//--------------------------------------------------------------------------------------------------

/// Return a reference to the console.
///
/// This is the global console used by all printing macros.
pub fn console() -> &'static dyn interface::All {
bsp::console::console()
}
7 changes: 2 additions & 5 deletions 04_safe_globals/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,11 @@ mod synchronization;
///
/// - Only a single core must be active and running this function.
unsafe fn kernel_init() -> ! {
use console::interface::Statistics;
use console::console;

println!("[0] Hello from Rust!");

println!(
"[1] Chars written: {}",
bsp::console::console().chars_written()
);
println!("[1] Chars written: {}", console().chars_written());

println!("[2] Stopping here.");
cpu::wait_forever()
Expand Down
6 changes: 2 additions & 4 deletions 04_safe_globals/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Printing.
use crate::{bsp, console};
use crate::console;
use core::fmt;

//--------------------------------------------------------------------------------------------------
Expand All @@ -13,9 +13,7 @@ use core::fmt;

#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use console::interface::Write;

bsp::console::console().write_fmt(args).unwrap();
console::console().write_fmt(args).unwrap();
}

/// Prints without a newline.
Expand Down
Loading

0 comments on commit fec4f9b

Please sign in to comment.