Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

WIP feat(shim): add logging #308

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion internal/shim-sev/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 _,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/syscall/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//! basic syscall handler functions

use core::fmt;
use primordial::Register;
use sallyport::{Cursor, Request, Result};

Expand Down Expand Up @@ -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) {}
}