-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework arch-specific console abstraction
Signed-off-by: Martin Kröning <[email protected]>
- Loading branch information
Showing
9 changed files
with
125 additions
and
105 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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
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 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