From 4043c8b5b751a94b7ee6acab3a230d623b6590f8 Mon Sep 17 00:00:00 2001 From: Matt Harvey Date: Fri, 13 Aug 2021 14:26:43 -0700 Subject: [PATCH] Mutex guards kata-uart-client read and write Change-Id: I5757dcf68f64359b743f458f336a813aeb43c069 GitOrigin-RevId: 1c6c85f95c6509cc915df1b82076942cd540c441 --- .../components/DebugConsole/DebugConsole.camkes | 2 ++ .../DebugConsole/kata-uart-client/src/lib.rs | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/system/components/DebugConsole/DebugConsole.camkes b/apps/system/components/DebugConsole/DebugConsole.camkes index 2ff735e..fb569a2 100644 --- a/apps/system/components/DebugConsole/DebugConsole.camkes +++ b/apps/system/components/DebugConsole/DebugConsole.camkes @@ -9,9 +9,11 @@ component DebugConsole { dataport Buf tx_dataport; uses dataport_io_inf uart_tx; + has mutex tx_mutex; dataport Buf rx_dataport; uses dataport_io_inf uart_rx; + has mutex rx_mutex; provides LoggerInterface logger; uses ProcessControlInterface proc_ctrl; diff --git a/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs b/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs index 2185a76..9f0c152 100644 --- a/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs @@ -7,15 +7,19 @@ use kata_io as io; // C interface to external UART driver. extern "C" { static rx_dataport: *mut cty::c_uchar; - static tx_dataport: *mut cty::c_uchar; fn uart_rx_update(n: cty::size_t); + fn rx_mutex_lock(); + fn rx_mutex_unlock(); + + static tx_dataport: *mut cty::c_uchar; fn uart_tx_update(n: cty::size_t); + fn tx_mutex_lock(); + fn tx_mutex_unlock(); } // Console logging interface. #[no_mangle] pub extern "C" fn logger_log(msg: *const cstr_core::c_char) { - // TODO(sleffler): is the uart driver ok w/ multiple writers? let output: &mut dyn io::Write = &mut self::Tx {}; unsafe { let _ = writeln!(output, "{}", CStr::from_ptr(msg).to_str().unwrap()); @@ -27,9 +31,11 @@ pub struct Rx {} impl io::Read for Rx { fn read(&mut self, buf: &mut [u8]) -> Result { unsafe { + rx_mutex_lock(); uart_rx_update(buf.len()); let port = core::slice::from_raw_parts(rx_dataport, buf.len()); buf.copy_from_slice(&port); + rx_mutex_unlock(); } Ok(buf.len()) } @@ -40,9 +46,11 @@ pub struct Tx {} impl io::Write for Tx { fn write(&mut self, buf: &[u8]) -> Result { unsafe { + tx_mutex_lock(); let port = core::slice::from_raw_parts_mut(tx_dataport, buf.len()); port.copy_from_slice(buf); uart_tx_update(buf.len()); + tx_mutex_unlock(); } Ok(buf.len()) }