-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: modularize console implementation for late UEFI output #324
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d63aeb4
fix(arch): output whole slices of bytes
mkroening 9c280dc
fix(riscv64): write whole slices of bytes
mkroening 88e2dce
fix(uefi): enable console struct
mkroening 32e5356
fix(uefi): remove unused stubs
mkroening 95224fb
refactor: move none and uefi modules into os module
mkroening cc376d8
refactor(arch): use cfg-if
mkroening b579697
refactor: move console into os module
mkroening da3883d
feat: rework arch-specific console abstraction
mkroening e546969
refactor(x86_64): move firecracker and multiboot into modules
mkroening c3b0e26
feat(uefi): allow printing after exiting boot services
mkroening dc1b965
feat(uefi): allow logging after exiting boot services
mkroening 652bdba
style: import log macros
mkroening d9f1caf
refactor: move allocator into os::none module
mkroening File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use core::ptr::NonNull; | ||
|
||
use hermit_dtb::Dtb; | ||
|
||
pub struct Console { | ||
stdout: NonNull<u8>, | ||
} | ||
|
||
fn stdout() -> u32 { | ||
/// Physical address of UART0 at Qemu's virt emulation | ||
const SERIAL_PORT_ADDRESS: u32 = 0x09000000; | ||
|
||
let dtb = unsafe { | ||
Dtb::from_raw(sptr::from_exposed_addr(super::DEVICE_TREE as usize)) | ||
.expect(".dtb file has invalid header") | ||
}; | ||
|
||
let property = dtb.get_property("/chosen", "stdout-path"); | ||
let uart_address = if let Some(stdout) = property { | ||
let stdout = core::str::from_utf8(stdout) | ||
.unwrap() | ||
.trim_matches(char::from(0)); | ||
if let Some(pos) = stdout.find('@') { | ||
let len = stdout.len(); | ||
u32::from_str_radix(&stdout[pos + 1..len], 16).unwrap_or(SERIAL_PORT_ADDRESS) | ||
} else { | ||
SERIAL_PORT_ADDRESS | ||
} | ||
} else { | ||
SERIAL_PORT_ADDRESS | ||
}; | ||
uart_address | ||
} | ||
|
||
impl Console { | ||
pub fn write_bytes(&mut self, bytes: &[u8]) { | ||
for byte in bytes.iter().copied() { | ||
unsafe { | ||
self.stdout.as_ptr().write_volatile(byte); | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn get_stdout(&self) -> NonNull<u8> { | ||
self.stdout | ||
} | ||
|
||
pub(super) fn set_stdout(&mut self, stdout: NonNull<u8>) { | ||
self.stdout = stdout; | ||
} | ||
} | ||
|
||
impl Default for Console { | ||
fn default() -> Self { | ||
let stdout = NonNull::new(stdout() as *mut u8).unwrap(); | ||
Self { stdout } | ||
} | ||
} | ||
|
||
unsafe impl Send for Console {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +0,0 @@ | ||
pub struct SerialPort { | ||
port_address: u32, | ||
} | ||
|
||
impl SerialPort { | ||
pub const fn new(port_address: u32) -> Self { | ||
Self { port_address } | ||
} | ||
|
||
pub unsafe fn set_port(&mut self, addr: u32) { | ||
unsafe { | ||
core::ptr::write_volatile(&mut self.port_address, addr); | ||
} | ||
} | ||
|
||
pub unsafe fn get_port(&self) -> u32 { | ||
unsafe { core::ptr::read_volatile(&self.port_address) } | ||
} | ||
|
||
pub fn write_byte(&self, byte: u8) { | ||
unsafe { | ||
let port = | ||
sptr::from_exposed_addr_mut(core::ptr::read_volatile(&self.port_address) as usize); | ||
|
||
// LF newline characters need to be extended to CRLF over a real serial port. | ||
if byte == b'\n' { | ||
core::ptr::write_volatile(port, b'\r'); | ||
} | ||
|
||
core::ptr::write_volatile(port, byte); | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
#[cfg(target_arch = "aarch64")] | ||
pub use crate::arch::aarch64::*; | ||
#[cfg(target_arch = "riscv64")] | ||
pub use crate::arch::riscv64::*; | ||
#[cfg(all(target_arch = "x86_64", target_os = "none"))] | ||
pub use crate::arch::x86_64::*; | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
pub mod aarch64; | ||
|
||
#[cfg(target_arch = "riscv64")] | ||
pub mod riscv64; | ||
|
||
#[cfg(all(target_arch = "x86_64", target_os = "none"))] | ||
pub mod x86_64; | ||
cfg_if::cfg_if! { | ||
if #[cfg(target_arch = "aarch64")] { | ||
mod aarch64; | ||
pub use self::aarch64::*; | ||
} else if #[cfg(target_arch = "riscv64")] { | ||
mod riscv64; | ||
pub use self::riscv64::*; | ||
} else if #[cfg(all(target_arch = "x86_64"))] { | ||
mod x86_64; | ||
pub use self::x86_64::*; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use sbi_rt::Physical; | ||
use sptr::Strict; | ||
|
||
#[derive(Default)] | ||
pub struct Console(()); | ||
|
||
impl Console { | ||
pub fn write_bytes(&mut self, bytes: &[u8]) { | ||
sbi_rt::console_write(Physical::new(bytes.len(), bytes.as_ptr().addr(), 0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use uart_16550::SerialPort; | ||
|
||
pub struct Console { | ||
serial_port: SerialPort, | ||
} | ||
|
||
impl Console { | ||
pub fn write_bytes(&mut self, bytes: &[u8]) { | ||
for byte in bytes.iter().copied() { | ||
self.serial_port.send(byte); | ||
} | ||
} | ||
} | ||
|
||
impl Default for Console { | ||
fn default() -> Self { | ||
let mut serial_port = unsafe { SerialPort::new(0x3F8) }; | ||
serial_port.init(); | ||
Self { serial_port } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this fn name a little confusing. Maybe something like
get_stdout
orstdout_addr
would be more self-explanatory. A doc-comment might also help.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. Though, just adding “get” does not help much, I think. I did not rework this code, I only moved it around.
I am planning a proper rework of this while migrating from
hermit-dtb
tofdt
.