Skip to content

Commit e5391a5

Browse files
committed
arch: prepare crate for non-x86_64 code
- Refactored crate to support addition of non-x86_64 architectures - Labeled code accordingly - non-x86_64 code was moved outside x86_64 folder - Adjusted vmm related code Signed-off-by: Diana Popa <[email protected]>
1 parent 5ce9a30 commit e5391a5

File tree

4 files changed

+56
-46
lines changed

4 files changed

+56
-46
lines changed

arch/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ use std::result;
1414

1515
#[derive(Debug, PartialEq)]
1616
pub enum Error {
17+
#[cfg(target_arch = "x86_64")]
18+
/// X86_64 specific error triggered during system configuration.
19+
X86_64Setup(x86_64::Error),
1720
/// The zero page extends past the end of guest_mem.
1821
ZeroPagePastRamEnd,
1922
/// Error writing the zero page of guest memory.
2023
ZeroPageSetup,
21-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
22-
/// X86_64 specific error triggered during system configuration.
23-
X86_64Setup(x86_64::Error),
2424
}
2525
pub type Result<T> = result::Result<T, Error>;
2626

2727
// 1MB. We don't put anything above here except the kernel itself.
2828
pub const HIMEM_START: usize = 0x100000;
2929

30-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
31-
pub mod x86_64;
32-
impl From<x86_64::Error> for Error {
33-
fn from(e: x86_64::Error) -> Error {
34-
Error::X86_64Setup(e)
35-
}
36-
}
30+
#[cfg(target_arch = "x86_64")]
31+
mod x86_64;
32+
33+
#[cfg(target_arch = "x86_64")]
34+
pub use x86_64::{
35+
arch_memory_regions, configure_system, get_32bit_gap_start as get_reserved_mem_addr,
36+
interrupts, regs, CMDLINE_START, COMMAND_LINE_SIZE, ZERO_PAGE_START,
37+
};

arch/src/x86_64/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ pub enum Error {
2323
MpTableSetup(mptable::Error),
2424
}
2525

26-
// Magic addresses used to lay out x86_64 VMs.
26+
impl From<Error> for super::Error {
27+
fn from(e: Error) -> super::Error {
28+
super::Error::X86_64Setup(e)
29+
}
30+
}
2731

28-
// Initial stack for the boot CPU.
29-
pub const BOOT_STACK_POINTER: usize = 0x8ff0;
32+
// Magic addresses used to lay out x86_64 VMs.
3033

3134
// Kernel command line.
3235
pub const CMDLINE_START: usize = 0x20000;
33-
pub const CMDLINE_MAX_SIZE: usize = 0x10000;
36+
// As per arch/x86/include/asm/setup.h.
37+
pub const COMMAND_LINE_SIZE: usize = 2048;
3438

3539
// The 'zero page', a.k.a linux kernel bootparams.
3640
pub const ZERO_PAGE_START: usize = 0x7000;

vmm/src/lib.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use std::time::Duration;
6161
use libc::{c_void, siginfo_t};
6262
use timerfd::{ClockId, SetTimeFlags, TimerFd, TimerState};
6363

64-
use arch::x86_64;
6564
use device_manager::legacy::LegacyDeviceManager;
6665
use device_manager::mmio::MMIODeviceManager;
6766
use devices::virtio;
@@ -814,7 +813,7 @@ impl Vmm {
814813
memory_model::GuestMemoryError::MemoryNotInitialized,
815814
))?
816815
<< 20;
817-
let arch_mem_regions = x86_64::arch_memory_regions(mem_size);
816+
let arch_mem_regions = arch::arch_memory_regions(mem_size);
818817
self.guest_memory =
819818
Some(GuestMemory::new(&arch_mem_regions).map_err(StartMicrovmError::GuestMemory)?);
820819
Ok(())
@@ -834,11 +833,11 @@ impl Vmm {
834833
.ok_or(StartMicrovmError::GuestMemory(
835834
memory_model::GuestMemoryError::MemoryNotInitialized,
836835
))?;
836+
837837
// Instantiate the MMIO device manager.
838-
// 'mmio_base' address has to be an address which is protected by the kernel, in this case
839-
// the start of the x86 specific gap of memory (currently hardcoded at 768MiB).
838+
// 'mmio_base' address has to be an address which is protected by the kernel.
840839
let mut device_manager =
841-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
840+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
842841

843842
self.attach_block_devices(&mut device_manager)?;
844843
self.attach_net_devices(&mut device_manager)?;
@@ -1093,7 +1092,7 @@ impl Vmm {
10931092
.vm_config
10941093
.vcpu_count
10951094
.ok_or(StartMicrovmError::VcpusNotConfigured)?;
1096-
x86_64::configure_system(
1095+
arch::configure_system(
10971096
vm_memory,
10981097
kernel_config.cmdline_addr,
10991098
cmdline_cstring.to_bytes().len() + 1,
@@ -1384,7 +1383,7 @@ impl Vmm {
13841383
let kernel_file = File::open(kernel_image_path).map_err(|_| {
13851384
VmmActionError::BootSource(ErrorKind::User, BootSourceConfigError::InvalidKernelPath)
13861385
})?;
1387-
let mut cmdline = kernel_cmdline::Cmdline::new(x86_64::CMDLINE_MAX_SIZE);
1386+
let mut cmdline = kernel_cmdline::Cmdline::new(arch::COMMAND_LINE_SIZE);
13881387
cmdline
13891388
.insert_str(kernel_cmdline.unwrap_or(String::from(DEFAULT_KERNEL_CMDLINE)))
13901389
.map_err(|_| {
@@ -1397,7 +1396,7 @@ impl Vmm {
13971396
let kernel_config = KernelConfig {
13981397
kernel_file,
13991398
cmdline,
1400-
cmdline_addr: GuestAddress(x86_64::CMDLINE_START),
1399+
cmdline_addr: GuestAddress(arch::CMDLINE_START),
14011400
};
14021401
self.configure_kernel(kernel_config);
14031402

@@ -1856,12 +1855,12 @@ mod tests {
18561855
let kernel_path = String::from(kernel_file_temp.path().to_path_buf().to_str().unwrap());
18571856
let kernel_file = File::open(kernel_path).unwrap();
18581857

1859-
let mut cmdline = kernel_cmdline::Cmdline::new(x86_64::CMDLINE_MAX_SIZE);
1858+
let mut cmdline = kernel_cmdline::Cmdline::new(arch::COMMAND_LINE_SIZE);
18601859
assert!(cmdline.insert_str(DEFAULT_KERNEL_CMDLINE).is_ok());
18611860
let kernel_cfg = KernelConfig {
18621861
cmdline,
18631862
kernel_file,
1864-
cmdline_addr: GuestAddress(x86_64::CMDLINE_START),
1863+
cmdline_addr: GuestAddress(arch::CMDLINE_START),
18651864
};
18661865
self.configure_kernel(kernel_cfg);
18671866
}
@@ -2423,7 +2422,7 @@ mod tests {
24232422

24242423
let guest_mem = vmm.guest_memory.clone().unwrap();
24252424
let mut device_manager =
2426-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
2425+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
24272426
assert!(vmm.attach_block_devices(&mut device_manager).is_ok());
24282427
assert!(vmm.get_kernel_cmdline_str().contains("root=/dev/vda"));
24292428

@@ -2447,7 +2446,7 @@ mod tests {
24472446

24482447
let guest_mem = vmm.guest_memory.clone().unwrap();
24492448
let mut device_manager =
2450-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
2449+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
24512450
assert!(vmm.attach_block_devices(&mut device_manager).is_ok());
24522451
assert!(vmm
24532452
.get_kernel_cmdline_str()
@@ -2475,7 +2474,7 @@ mod tests {
24752474

24762475
let guest_mem = vmm.guest_memory.clone().unwrap();
24772476
let mut device_manager =
2478-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
2477+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
24792478
assert!(vmm.attach_block_devices(&mut device_manager).is_ok());
24802479
// Test that kernel commandline does not contain either /dev/vda or PARTUUID.
24812480
assert!(!vmm.get_kernel_cmdline_str().contains("root=PARTUUID="));
@@ -2509,7 +2508,7 @@ mod tests {
25092508

25102509
let guest_mem = vmm.guest_memory.clone().unwrap();
25112510
let mut device_manager =
2512-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
2511+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
25132512

25142513
// test create network interface
25152514
let network_interface = NetworkInterfaceConfig {
@@ -2551,7 +2550,7 @@ mod tests {
25512550
// Test valid kernel path and invalid cmdline.
25522551
let kernel_file = NamedTempFile::new().expect("Failed to create temporary kernel file.");
25532552
let kernel_path = String::from(kernel_file.path().to_path_buf().to_str().unwrap());
2554-
let invalid_cmdline = String::from_utf8(vec![b'X'; x86_64::CMDLINE_MAX_SIZE + 1]).unwrap();
2553+
let invalid_cmdline = String::from_utf8(vec![b'X'; arch::COMMAND_LINE_SIZE + 1]).unwrap();
25552554
assert!(vmm
25562555
.configure_boot_source(kernel_path.clone(), Some(invalid_cmdline))
25572556
.is_err());
@@ -2605,14 +2604,14 @@ mod tests {
26052604

26062605
let guest_mem = vmm.guest_memory.clone().unwrap();
26072606
let mut device_manager =
2608-
MMIODeviceManager::new(guest_mem.clone(), x86_64::get_32bit_gap_start() as u64);
2607+
MMIODeviceManager::new(guest_mem.clone(), arch::get_reserved_mem_addr() as u64);
26092608

26102609
let dummy_box = Box::new(DummyDevice { dummy: 0 });
26112610
// Use a dummy command line as it is not used in this test.
26122611
let _addr = device_manager
26132612
.register_device(
26142613
dummy_box,
2615-
&mut kernel_cmdline::Cmdline::new(x86_64::CMDLINE_MAX_SIZE),
2614+
&mut kernel_cmdline::Cmdline::new(arch::COMMAND_LINE_SIZE),
26162615
Some(scratch_id.clone()),
26172616
)
26182617
.unwrap();

vmm/src/vstate.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ extern crate sys_util;
1313
use std::result;
1414

1515
use super::KvmContext;
16-
use arch::x86_64;
1716
use cpuid::{c3_template, filter_cpuid, t2_template};
1817
use kvm::*;
1918
use logger::{LogOption, LOGGER};
2019
use logger::{Metric, METRICS};
2120
use memory_model::{GuestAddress, GuestMemory, GuestMemoryError};
2221
use sys_util::EventFd;
2322
use vmm_config::machine_config::{CpuFeaturesTemplate, VmConfig};
24-
use x86_64::{interrupts, regs};
2523

2624
pub const KVM_TSS_ADDRESS: usize = 0xfffbd000;
25+
26+
// Initial stack for the boot CPU.
27+
const BOOT_STACK_POINTER: usize = 0x8ff0;
2728
const KVM_MEM_LOG_DIRTY_PAGES: u32 = 0x1;
2829

2930
/// Errors associated with the wrappers over KVM ioctls.
@@ -48,17 +49,21 @@ pub enum Error {
4849
/// The number of configured slots is bigger than the maximum reported by KVM.
4950
NotEnoughMemorySlots,
5051
/// Cannot set the local interruption due to bad configuration.
51-
LocalIntConfiguration(interrupts::Error),
52+
LocalIntConfiguration(arch::interrupts::Error),
5253
/// Cannot set the memory regions.
5354
SetUserMemoryRegion(sys_util::Error),
55+
#[cfg(target_arch = "x86_64")]
5456
/// Error configuring the MSR registers
55-
MSRSConfiguration(regs::Error),
57+
MSRSConfiguration(arch::regs::Error),
58+
#[cfg(target_arch = "x86_64")]
5659
/// Error configuring the general purpose registers
57-
REGSConfiguration(regs::Error),
60+
REGSConfiguration(arch::regs::Error),
61+
#[cfg(target_arch = "x86_64")]
5862
/// Error configuring the special registers
59-
SREGSConfiguration(regs::Error),
63+
SREGSConfiguration(arch::regs::Error),
64+
#[cfg(target_arch = "x86_64")]
6065
/// Error configuring the floating point related registers
61-
FPUConfiguration(regs::Error),
66+
FPUConfiguration(arch::regs::Error),
6267
/// Cannot configure the IRQ.
6368
Irq(sys_util::Error),
6469
}
@@ -173,7 +178,8 @@ impl Vcpu {
173178
})
174179
}
175180

176-
/// /// Configures the vcpu and should be called once per vcpu from the vcpu's thread.
181+
#[cfg(target_arch = "x86_64")]
182+
/// Configures the vcpu and should be called once per vcpu from the vcpu's thread.
177183
///
178184
/// # Arguments
179185
///
@@ -217,21 +223,21 @@ impl Vcpu {
217223
.set_cpuid2(&self.cpuid)
218224
.map_err(Error::SetSupportedCpusFailed)?;
219225

220-
x86_64::regs::setup_msrs(&self.fd).map_err(Error::MSRSConfiguration)?;
226+
arch::regs::setup_msrs(&self.fd).map_err(Error::MSRSConfiguration)?;
221227
// Safe to unwrap because this method is called after the VM is configured
222228
let vm_memory = vm
223229
.get_memory()
224230
.ok_or(Error::GuestMemory(GuestMemoryError::MemoryNotInitialized))?;
225-
x86_64::regs::setup_regs(
231+
arch::regs::setup_regs(
226232
&self.fd,
227233
kernel_start_addr.offset() as u64,
228-
x86_64::BOOT_STACK_POINTER as u64,
229-
x86_64::ZERO_PAGE_START as u64,
234+
BOOT_STACK_POINTER as u64,
235+
arch::ZERO_PAGE_START as u64,
230236
)
231237
.map_err(Error::REGSConfiguration)?;
232-
x86_64::regs::setup_fpu(&self.fd).map_err(Error::FPUConfiguration)?;
233-
x86_64::regs::setup_sregs(vm_memory, &self.fd).map_err(Error::SREGSConfiguration)?;
234-
x86_64::interrupts::set_lint(&self.fd).map_err(Error::LocalIntConfiguration)?;
238+
arch::regs::setup_fpu(&self.fd).map_err(Error::FPUConfiguration)?;
239+
arch::regs::setup_sregs(vm_memory, &self.fd).map_err(Error::SREGSConfiguration)?;
240+
arch::interrupts::set_lint(&self.fd).map_err(Error::LocalIntConfiguration)?;
235241
Ok(())
236242
}
237243

0 commit comments

Comments
 (0)