Skip to content

Commit

Permalink
VirtualCPU::cmd: Take descriptive arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Jan 3, 2022
1 parent 40b9e08 commit 1affe92
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/linux/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::consts::*;
use crate::linux::virtio::*;
use crate::linux::KVM;
use crate::vm::HypervisorResult;
use crate::vm::SysCmdsize;
use crate::vm::SysCmdval;
use crate::vm::VcpuStopReason;
use crate::vm::VirtualCPU;
use kvm_bindings::*;
Expand Down Expand Up @@ -348,12 +350,16 @@ impl VirtualCPU for UhyveCPU {
UHYVE_PORT_CMDSIZE => {
let data_addr: usize =
unsafe { (*(addr.as_ptr() as *const u32)) as usize };
self.cmdsize(self.host_address(data_addr));
let args_ptr = self.host_address(data_addr);
let syssize = unsafe { &mut *(args_ptr as *mut SysCmdsize) };
self.cmdsize(syssize);
}
UHYVE_PORT_CMDVAL => {
let data_addr: usize =
unsafe { (*(addr.as_ptr() as *const u32)) as usize };
self.cmdval(self.host_address(data_addr));
let args_ptr = self.host_address(data_addr);
let syscmdval = unsafe { &*(args_ptr as *const SysCmdval) };
self.cmdval(syscmdval);
}
UHYVE_PORT_NETWRITE => {
match &self.tx {
Expand Down
10 changes: 8 additions & 2 deletions src/macos/x86_64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate::consts::*;
use crate::macos::x86_64::ioapic::IoApic;
use crate::vm::HypervisorResult;
use crate::vm::SysCmdsize;
use crate::vm::SysCmdval;
use crate::vm::VcpuStopReason;
use crate::vm::VirtualCPU;
use burst::x86::{disassemble_64, InstructionOperation, OperandType};
Expand Down Expand Up @@ -743,13 +745,17 @@ impl VirtualCPU for UhyveCPU {
UHYVE_PORT_CMDSIZE => {
let data_addr: u64 =
self.vcpu.read_register(&Register::RAX)? & 0xFFFFFFFF;
self.cmdsize(self.host_address(data_addr as usize));
let args_ptr = self.host_address(data_addr as usize);
let syssize = unsafe { &mut *(args_ptr as *mut SysCmdsize) };
self.cmdsize(syssize);
self.vcpu.write_register(&Register::RIP, rip + len)?;
}
UHYVE_PORT_CMDVAL => {
let data_addr: u64 =
self.vcpu.read_register(&Register::RAX)? & 0xFFFFFFFF;
self.cmdval(self.host_address(data_addr as usize));
let args_ptr = self.host_address(data_addr as usize);
let syscmdval = unsafe { &*(args_ptr as *const SysCmdval) };
self.cmdval(syscmdval);
self.vcpu.write_register(&Register::RIP, rip + len)?;
}
UHYVE_PORT_EXIT => {
Expand Down
11 changes: 4 additions & 7 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ const MAX_ARGC: usize = 128;
const MAX_ENVC: usize = 128;

#[repr(C, packed)]
struct SysCmdsize {
pub struct SysCmdsize {
argc: i32,
argsz: [i32; MAX_ARGC],
envc: i32,
envsz: [i32; MAX_ENVC],
}

#[repr(C, packed)]
struct SysCmdval {
pub struct SysCmdval {
argv: *const u8,
envp: *const u8,
}
Expand Down Expand Up @@ -140,8 +140,7 @@ pub trait VirtualCPU {
/// Returns the (host) path of the kernel binary.
fn kernel_path(&self) -> &Path;

fn cmdsize(&self, args_ptr: usize) {
let syssize = unsafe { &mut *(args_ptr as *mut SysCmdsize) };
fn cmdsize(&self, syssize: &mut SysCmdsize) {
syssize.argc = 0;
syssize.envc = 0;

Expand Down Expand Up @@ -185,9 +184,7 @@ pub trait VirtualCPU {
}

/// Copies the arguments end environment of the application into the VM's memory.
fn cmdval(&self, args_ptr: usize) {
let syscmdval = unsafe { &*(args_ptr as *const SysCmdval) };

fn cmdval(&self, syscmdval: &SysCmdval) {
let mut counter: i32 = 0;
let argv = self.host_address(syscmdval.argv as usize);
let mut found_separator = false;
Expand Down

0 comments on commit 1affe92

Please sign in to comment.