From 119057ea74b0770eb3c61480b449705da4432edd Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Tue, 26 Jan 2021 09:48:26 +0100 Subject: [PATCH] feat(shim): add logging Signed-off-by: Harald Hoyer --- internal/shim-sev/src/syscall.rs | 31 ++++++++++++++++++++++++++++++- internal/syscall/src/base.rs | 4 ++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/shim-sev/src/syscall.rs b/internal/shim-sev/src/syscall.rs index 5efa026c..3a8bad86 100644 --- a/internal/shim-sev/src/syscall.rs +++ b/internal/shim-sev/src/syscall.rs @@ -7,10 +7,11 @@ use crate::allocator::ALLOCATOR; use crate::asm::_enarx_asm_triple_fault; use crate::attestation::SEV_SECRET; use crate::eprintln; -use crate::hostcall::{HostCall, HOST_CALL_ALLOC}; +use crate::hostcall::{shim_write_all, HostCall, HostFd, HOST_CALL_ALLOC}; use crate::paging::SHIM_PAGETABLE; use crate::payload::{NEXT_BRK_RWLOCK, NEXT_MMAP_RWLOCK}; use core::convert::TryFrom; +use core::fmt::{self, Write}; use core::mem::size_of; use core::ops::{Deref, DerefMut}; use primordial::{Address, Register}; @@ -133,6 +134,19 @@ extern "sysv64" fn syscall_rust( let ret = h.syscall(a, b, c, d, e, f, nr); + if nr != libc::SYS_write as _ && usize::from(a) != libc::STDERR_FILENO as _ { + match ret { + Err(e) => match e { + libc::EINVAL => eprintln!("= EINVAL"), + libc::EAGAIN => eprintln!("= EAGAIN"), + libc::EFAULT => eprintln!("= EFAULT"), + libc::ENOSYS => eprintln!("= ENOSYS"), + e => eprintln!("= Err({})", e), + }, + Ok([rax, _]) => eprintln!("= {}", usize::from(rax)), + } + } + match ret { Err(e) => X8664DoubleReturn { rax: e.checked_neg().unwrap() as _, @@ -152,6 +166,17 @@ struct Handler { argv: [usize; 6], } +impl fmt::Write for Handler { + fn write_str(&mut self, s: &str) -> fmt::Result { + shim_write_all( + unsafe { HostFd::from_raw_fd(libc::STDOUT_FILENO) }, + s.as_bytes(), + ) + .map_err(|_| fmt::Error)?; + Ok(()) + } +} + impl AddressValidator for Handler { #[inline(always)] fn validate_const_mem_fn(&self, _ptr: *const (), _size: usize) -> bool { @@ -215,6 +240,10 @@ impl BaseSyscallHandler for Handler { eprintln!(")"); } + + fn log(&mut self, args: fmt::Arguments) { + let _ = self.write_fmt(args); + } } impl EnarxSyscallHandler for Handler { diff --git a/internal/syscall/src/base.rs b/internal/syscall/src/base.rs index 9f3c5f32..afc9db83 100644 --- a/internal/syscall/src/base.rs +++ b/internal/syscall/src/base.rs @@ -2,6 +2,7 @@ //! basic syscall handler functions +use core::fmt; use primordial::Register; use sallyport::{Cursor, Request, Result}; @@ -38,4 +39,7 @@ pub trait BaseSyscallHandler { /// Output tracing information about the syscall fn trace(&mut self, name: &str, argc: usize); + + /// Log debug output + fn log(&mut self, _args: fmt::Arguments) {} }