Skip to content

Commit

Permalink
ARM: rename x86_64 crate to arch
Browse files Browse the repository at this point in the history
Also,
* move the autogenerated code in a different crate named `arch_gen`
* x86_64 code code inside `arch` and `arch_gen` is labeled as such

Signed-off-by: Diana Popa <dpopa@amazon.com>
  • Loading branch information
dianpopa authored and acatangiu committed Jan 18, 2019
1 parent 56eb015 commit 55ecc7d
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 237 deletions.
38 changes: 23 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ sys_util = { path = "../sys_util" }
vmm = { path = "../vmm" }

[dev-dependencies]
arch = { path = "../arch" }
devices = { path = "../devices" }
kernel = { path = "../kernel" }
memory_model = { path = "../memory_model" }
net_util = { path = "../net_util" }
rate_limiter = { path = "../rate_limiter" }
x86_64 = { path = "../x86_64" }

[features]
vsock = ["vmm/vsock"]
4 changes: 2 additions & 2 deletions api_server/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ impl PartialEq for ParsedRequest {

#[cfg(test)]
mod tests {
extern crate arch;
extern crate devices;
extern crate kernel;
extern crate memory_model;
extern crate net_util;
extern crate x86_64;

use self::devices::virtio::net::Error as VirtioNetError;
use self::memory_model::GuestMemoryError;
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {
check_error_response(vmm_resp, StatusCode::InternalServerError);
let vmm_resp = VmmActionError::StartMicrovm(
ErrorKind::Internal,
StartMicrovmError::ConfigureSystem(x86_64::Error::E820Configuration),
StartMicrovmError::ConfigureSystem(arch::Error::ZeroPagePastRamEnd),
);
check_error_response(vmm_resp, StatusCode::InternalServerError);
let vmm_resp = VmmActionError::StartMicrovm(
Expand Down
3 changes: 2 additions & 1 deletion x86_64/Cargo.toml → arch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "x86_64"
name = "arch"
version = "0.1.0"
authors = ["The Chromium OS Authors"]

Expand All @@ -8,6 +8,7 @@ byteorder = "=1.2.1"
kvm_wrapper = ">=0.1.0"
libc = ">=0.2.39"

arch_gen = { path = "../arch_gen" }
kvm = { path = "../kvm" }
memory_model = { path = "../memory_model" }
sys_util = { path = "../sys_util" }
Expand Down
36 changes: 36 additions & 0 deletions arch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

extern crate byteorder;
extern crate kvm_wrapper;
extern crate libc;

extern crate arch_gen;
extern crate kvm;
extern crate memory_model;
extern crate sys_util;

use std::result;

#[derive(Debug, PartialEq)]
pub enum Error {
/// The zero page extends past the end of guest_mem.
ZeroPagePastRamEnd,
/// Error writing the zero page of guest memory.
ZeroPageSetup,
#[cfg(target_arch = "x86_64")]
/// X86_64 specific error triggered during system configuration.
X86_64Setup(x86_64::Error),
}
pub type Result<T> = result::Result<T, Error>;

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

#[cfg(target_arch = "x86_64")]
pub mod x86_64;
impl From<x86_64::Error> for Error {
fn from(e: x86_64::Error) -> Error {
Error::X86_64Setup(e)
}
}
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions arch/src/x86_64/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.

/// Magic addresses externally used to lay out x86_64 VMs.

/// Initial stack for the boot CPU.
pub const BOOT_STACK_START: usize = 0x8000;
pub const BOOT_STACK_POINTER: usize = 0x8ff0;

/// Kernel command line start address.
pub const CMDLINE_START: usize = 0x20000;
/// Kernel command line start address maximum size.
pub const CMDLINE_MAX_SIZE: usize = 0x10000;

/// The 'zero page', a.k.a linux kernel bootparams.
pub const ZERO_PAGE_START: usize = 0x7000;
86 changes: 28 additions & 58 deletions x86_64/src/lib.rs → arch/src/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.

extern crate byteorder;
extern crate kvm;
extern crate kvm_wrapper;
extern crate libc;
extern crate memory_model;
extern crate sys_util;

#[allow(dead_code)]
#[allow(non_upper_case_globals)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
mod bootparam;
// boot_params is just a series of ints, it is safe to initialize it.
unsafe impl memory_model::DataInit for bootparam::boot_params {}

#[allow(dead_code)]
#[allow(non_upper_case_globals)]
mod msr_index;

#[allow(dead_code)]
#[allow(non_upper_case_globals)]
#[allow(non_camel_case_types)]
mod mpspec;
// These mpspec types are only data, reading them from data is a safe initialization.
unsafe impl memory_model::DataInit for mpspec::mpc_bus {}
unsafe impl memory_model::DataInit for mpspec::mpc_cpu {}
unsafe impl memory_model::DataInit for mpspec::mpc_intsrc {}
unsafe impl memory_model::DataInit for mpspec::mpc_ioapic {}
unsafe impl memory_model::DataInit for mpspec::mpc_table {}
unsafe impl memory_model::DataInit for mpspec::mpc_lintsrc {}
unsafe impl memory_model::DataInit for mpspec::mpf_intel {}

mod gdt;
pub mod interrupts;
pub mod layout;
mod mptable;
pub mod regs;

use std::mem;
use std::result;

use bootparam::boot_params;
use bootparam::E820_RAM;
use arch_gen::x86::bootparam::{boot_params, E820_RAM};
use memory_model::{GuestAddress, GuestMemory};

pub use interrupts::Error as IntError;
pub use mptable::Error as MpTableError;
pub use regs::Error as RegError;
// Where BIOS/VGA magic would live on a real PC.
const EBDA_START: u64 = 0x9fc00;
const FIRST_ADDR_PAST_32BITS: usize = (1 << 32);
const MEM_32BIT_GAP_SIZE: usize = (768 << 20);

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Error {
/// Invalid e820 setup params.
E820Configuration,
/// Error writing MP table to memory.
MpTableSetup(MpTableError),
/// The zero page extends past the end of guest_mem.
ZeroPagePastRamEnd,
/// Error writing the zero page of guest memory.
ZeroPageSetup,
MpTableSetup(mptable::Error),
}
pub type Result<T> = result::Result<T, Error>;

const FIRST_ADDR_PAST_32BITS: usize = (1 << 32);
const MEM_32BIT_GAP_SIZE: usize = (768 << 20);

/// Returns a Vec of the valid memory addresses.
/// These should be used to configure the GuestMemory structure for the platform.
Expand Down Expand Up @@ -96,8 +55,7 @@ pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
regions
}

/// X86 specific memory hole/ memory mapped devices/ reserved area.
///
/// X86 specific memory hole/memory mapped devices/reserved area.
pub fn get_32bit_gap_start() -> usize {
FIRST_ADDR_PAST_32BITS - MEM_32BIT_GAP_SIZE
}
Expand All @@ -115,15 +73,15 @@ pub fn configure_system(
cmdline_addr: GuestAddress,
cmdline_size: usize,
num_cpus: u8,
) -> Result<()> {
) -> super::Result<()> {
const KERNEL_BOOT_FLAG_MAGIC: u16 = 0xaa55;
const KERNEL_HDR_MAGIC: u32 = 0x53726448;
const KERNEL_LOADER_OTHER: u8 = 0xff;
const KERNEL_MIN_ALIGNMENT_BYTES: u32 = 0x1000000; // Must be non-zero.
let first_addr_past_32bits = GuestAddress(FIRST_ADDR_PAST_32BITS);
let end_32bit_gap_start = GuestAddress(get_32bit_gap_start());

let himem_start = GuestAddress(layout::HIMEM_START);
let himem_start = GuestAddress(super::HIMEM_START);

// Note that this puts the mptable at the last 1k of Linux's 640k base RAM
mptable::setup_mptable(guest_mem, num_cpus).map_err(Error::MpTableSetup)?;
Expand All @@ -137,7 +95,7 @@ pub fn configure_system(
params.hdr.cmdline_size = cmdline_size as u32;
params.hdr.kernel_alignment = KERNEL_MIN_ALIGNMENT_BYTES;

add_e820_entry(&mut params, 0, layout::EBDA_START, E820_RAM)?;
add_e820_entry(&mut params, 0, EBDA_START, E820_RAM)?;

let mem_end = guest_mem.end_addr();
if mem_end < end_32bit_gap_start {
Expand Down Expand Up @@ -167,17 +125,22 @@ pub fn configure_system(
let zero_page_addr = GuestAddress(layout::ZERO_PAGE_START);
guest_mem
.checked_offset(zero_page_addr, mem::size_of::<boot_params>())
.ok_or(Error::ZeroPagePastRamEnd)?;
.ok_or(super::Error::ZeroPagePastRamEnd)?;
guest_mem
.write_obj_at_addr(params, zero_page_addr)
.map_err(|_| Error::ZeroPageSetup)?;
.map_err(|_| super::Error::ZeroPageSetup)?;

Ok(())
}

/// Add an e820 region to the e820 map.
/// Returns Ok(()) if successful, or an error if there is no space left in the map.
fn add_e820_entry(params: &mut boot_params, addr: u64, size: u64, mem_type: u32) -> Result<()> {
fn add_e820_entry(
params: &mut boot_params,
addr: u64,
size: u64,
mem_type: u32,
) -> Result<(), Error> {
if params.e820_entries >= params.e820_map.len() as u8 {
return Err(Error::E820Configuration);
}
Expand All @@ -193,7 +156,7 @@ fn add_e820_entry(params: &mut boot_params, addr: u64, size: u64, mem_type: u32)
#[cfg(test)]
mod tests {
use super::*;
use bootparam::e820entry;
use arch_gen::x86::bootparam::e820entry;

#[test]
fn regions_lt_4gb() {
Expand Down Expand Up @@ -223,7 +186,14 @@ mod tests {
fn test_system_configuration() {
let no_vcpus = 4;
let gm = GuestMemory::new(&vec![(GuestAddress(0), 0x10000)]).unwrap();
assert!(configure_system(&gm, GuestAddress(0), 0, 1).is_err());
let config_err = configure_system(&gm, GuestAddress(0), 0, 1);
assert!(config_err.is_err());
assert_eq!(
config_err.unwrap_err(),
super::super::Error::X86_64Setup(super::Error::MpTableSetup(
mptable::Error::NotEnoughMemory
))
);

// Now assigning some memory that falls before the 32bit memory hole.
let mem_size = 128 << 20;
Expand Down
Loading

0 comments on commit 55ecc7d

Please sign in to comment.