Unverified Commit d793a9b1 authored by jschwe's avatar jschwe Committed by GitHub
Browse files

Refactor libhermit-rs for rust edition 2018 (#59)

* Refactor: Fix Clippy warnings

- Added support for empty println!()
- use short hand initialization when possible
- replace if x==false with if !x
- replace unwrap_or(function_call()) with unwrap_or_else(|| function_call())
- and others

* Update to Rust edition 2018
 - Mostly just adds crate::
parent af02690e
......@@ -11,6 +11,7 @@ readme = "README.md"
keywords = ["unikernel", "libos"]
categories = ["os"]
repository = "https://github.com/hermitcore/libhermit-rs"
edition = "2018"
description = """
RustyHermit - A Rust-based, lightweight unikernel
"""
......
......@@ -16,14 +16,14 @@ pub mod serial;
pub mod stubs;
pub mod systemtime;
use arch::aarch64::kernel::percore::*;
use arch::aarch64::kernel::serial::SerialPort;
pub use arch::aarch64::kernel::stubs::*;
pub use arch::aarch64::kernel::systemtime::get_boot_time;
use crate::arch::aarch64::kernel::percore::*;
use crate::arch::aarch64::kernel::serial::SerialPort;
pub use crate::arch::aarch64::kernel::stubs::*;
pub use crate::arch::aarch64::kernel::systemtime::get_boot_time;
use crate::environment;
use crate::kernel_message_buffer;
use crate::synch::spinlock::Spinlock;
use core::ptr;
use environment;
use kernel_message_buffer;
use synch::spinlock::Spinlock;
const SERIAL_PORT_BAUDRATE: u32 = 115200;
......
......@@ -5,8 +5,8 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use crate::scheduler::PerCoreScheduler;
use core::ptr;
use scheduler::PerCoreScheduler;
#[no_mangle]
pub static mut PERCORE: PerCoreVariables = PerCoreVariables::new(0);
......
......@@ -26,12 +26,12 @@
include!(concat!(env!("CARGO_TARGET_DIR"), "/config.rs"));
use crate::arch::aarch64::kernel::percore::*;
use crate::arch::aarch64::kernel::processor;
use crate::scheduler::task::{Task, TaskFrame, TaskTLS};
use alloc::rc::Rc;
use arch::aarch64::kernel::percore::*;
use arch::aarch64::kernel::processor;
use core::cell::RefCell;
use core::{mem, ptr};
use scheduler::task::{Task, TaskFrame, TaskTLS};
extern "C" {
static tls_start: u8;
......
......@@ -24,12 +24,12 @@ impl SerialPort {
// LF newline characters need to be extended to CRLF over a real serial port.
if byte == b'\n' {
unsafe {
volatile_store(port, b'\r');
std::ptr::write_volatile(port, b'\r');
}
}
unsafe {
volatile_store(port, byte);
std::ptr::write_volatile(port, byte);
}
}
......
......@@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use environment;
use crate::environment;
extern "C" {
static mut boot_gtod: u64;
......
......@@ -5,14 +5,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::aarch64::kernel::percore::*;
use arch::aarch64::kernel::processor;
use arch::aarch64::mm::physicalmem;
use arch::aarch64::mm::virtualmem;
use crate::arch::aarch64::kernel::percore::*;
use crate::arch::aarch64::kernel::processor;
use crate::arch::aarch64::mm::physicalmem;
use crate::arch::aarch64::mm::virtualmem;
use crate::mm;
use crate::scheduler;
use core::marker::PhantomData;
use core::{fmt, ptr, usize};
use mm;
use scheduler;
extern "C" {
#[linkage = "extern_weak"]
......
......@@ -5,11 +5,11 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::aarch64::mm::paging::{BasePageSize, PageSize};
use collections::Node;
use mm;
use mm::freelist::{FreeList, FreeListEntry};
use mm::{MM_LOCK, POOL};
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::collections::Node;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::mm::{MM_LOCK, POOL};
extern "C" {
static limit: usize;
......
......@@ -5,11 +5,11 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::aarch64::mm::paging::{BasePageSize, PageSize};
use collections::Node;
use mm;
use mm::freelist::{FreeList, FreeListEntry};
use mm::{MM_LOCK, POOL};
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::collections::Node;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::mm::{MM_LOCK, POOL};
static mut KERNEL_FREE_LIST: FreeList = FreeList::new();
......
......@@ -15,58 +15,60 @@ pub mod x86_64;
// Export our platform-specific modules.
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::*;
pub use crate::arch::aarch64::*;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::stubs::{set_oneshot_timer, switch, wakeup_core};
pub use crate::arch::aarch64::kernel::stubs::{set_oneshot_timer, switch, wakeup_core};
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::{
pub use crate::arch::aarch64::kernel::{
application_processor_init, boot_application_processors, boot_processor_init,
get_processor_count, message_output_init, output_message_byte,
};
#[cfg(target_arch = "aarch64")]
use arch::aarch64::kernel::percore::core_scheduler;
use crate::arch::aarch64::kernel::percore::core_scheduler;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::percore;
pub use crate::arch::aarch64::kernel::percore;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::scheduler;
pub use crate::arch::aarch64::kernel::scheduler;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::processor;
pub use crate::arch::aarch64::kernel::processor;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::irq;
pub use crate::arch::aarch64::kernel::irq;
#[cfg(target_arch = "aarch64")]
pub use arch::aarch64::kernel::systemtime::get_boot_time;
pub use crate::arch::aarch64::kernel::systemtime::get_boot_time;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::*;
pub use crate::arch::x86_64::*;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::apic::{set_oneshot_timer, wakeup_core};
pub use crate::arch::x86_64::kernel::apic::{set_oneshot_timer, wakeup_core};
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::gdt::set_current_kernel_stack;
pub use crate::arch::x86_64::kernel::gdt::set_current_kernel_stack;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::irq;
pub use crate::arch::x86_64::kernel::irq;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::percore;
pub use crate::arch::x86_64::kernel::percore;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::processor;
pub use crate::arch::x86_64::kernel::processor;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::scheduler;
pub use crate::arch::x86_64::kernel::scheduler;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::switch::switch;
pub use crate::arch::x86_64::kernel::switch::switch;
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::systemtime::get_boot_time;
pub use crate::arch::x86_64::kernel::systemtime::get_boot_time;
#[cfg(not(test))]
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::{
pub use crate::arch::x86_64::kernel::{
application_processor_init, boot_application_processors, boot_processor_init,
};
#[cfg(target_arch = "x86_64")]
pub use arch::x86_64::kernel::{get_processor_count, message_output_init, output_message_byte};
pub use crate::arch::x86_64::kernel::{
get_processor_count, message_output_init, output_message_byte,
};
......@@ -5,11 +5,10 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::x86_64::mm::paging;
use arch::x86_64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
use arch::x86_64::mm::virtualmem;
use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
use crate::arch::x86_64::mm::{paging, virtualmem};
use crate::x86::io::*;
use core::{mem, slice, str};
use x86::io::*;
/// Memory at this physical address is supposed to contain a pointer to the Extended BIOS Data Area (EBDA).
const EBDA_PTR_LOCATION: usize = 0x0000_040E;
......
......@@ -5,31 +5,26 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use alloc::boxed::Box;
use alloc::vec::Vec;
use arch;
use crate::arch;
#[cfg(feature = "acpi")]
use arch::x86_64::kernel::acpi;
use arch::x86_64::kernel::idt;
use arch::x86_64::kernel::irq;
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::processor;
use crate::arch::x86_64::kernel::acpi;
#[cfg(not(test))]
use arch::x86_64::kernel::smp_boot_code::SMP_BOOT_CODE;
use arch::x86_64::kernel::BOOT_INFO;
use arch::x86_64::mm::paging;
use arch::x86_64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
use arch::x86_64::mm::virtualmem;
use config::*;
use crate::arch::x86_64::kernel::smp_boot_code::SMP_BOOT_CODE;
use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
use crate::arch::x86_64::mm::{paging, virtualmem};
use crate::config::*;
use crate::environment;
use crate::mm;
use crate::scheduler;
use crate::scheduler::CoreId;
use crate::x86::controlregs::*;
use crate::x86::msr::*;
use alloc::boxed::Box;
use alloc::vec::Vec;
use arch::x86_64::kernel::{idt, irq, percore::*, processor, BOOT_INFO};
use core::convert::TryInto;
use core::sync::atomic::spin_loop_hint;
use core::{cmp, fmt, mem, ptr, u32};
use environment;
use mm;
use scheduler;
use scheduler::CoreId;
use x86::controlregs::*;
use x86::msr::*;
const APIC_ICR2: usize = 0x0310;
......
......@@ -5,12 +5,12 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use crate::syscalls::fs::{FileError, FilePerms, PosixFile, PosixFileSystem, SeekWhence};
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::{fmt, u32, u8};
use syscalls::fs::{FileError, FilePerms, PosixFile, PosixFileSystem, SeekWhence};
// response out layout eg @ https://github.com/zargony/fuse-rs/blob/bf6d1cf03f3277e35b580f3c7b9999255d72ecf3/src/ll/request.rs#L44
// op in/out sizes/layout: https://github.com/hanwen/go-fuse/blob/204b45dba899dfa147235c255908236d5fde2d32/fuse/opcode.go#L439
......
......@@ -6,17 +6,17 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use crate::arch::x86_64::kernel::percore::*;
use crate::arch::x86_64::kernel::BOOT_INFO;
use crate::config::*;
use crate::x86::bits64::segmentation::*;
use crate::x86::bits64::task::*;
use crate::x86::dtables::{self, DescriptorTablePointer};
use crate::x86::segmentation::*;
use crate::x86::task::*;
use crate::x86::Ring;
use alloc::boxed::Box;
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::BOOT_INFO;
use config::*;
use core::mem;
use x86::bits64::segmentation::*;
use x86::bits64::task::*;
use x86::dtables::{self, DescriptorTablePointer};
use x86::segmentation::*;
use x86::task::*;
use x86::Ring;
pub const GDT_NULL: u16 = 0;
pub const GDT_KERNEL_CODE: u16 = 1;
......@@ -44,7 +44,7 @@ struct Gdt {
pub fn init() {
unsafe {
// Dynamically allocate memory for the GDT.
GDT = ::mm::allocate(mem::size_of::<Gdt>(), false) as *mut Gdt;
GDT = crate::mm::allocate(mem::size_of::<Gdt>(), false) as *mut Gdt;
// The NULL descriptor is always the first entry.
(*GDT).entries[GDT_NULL as usize] = Descriptor::NULL;
......@@ -96,7 +96,7 @@ pub fn add_current_core() {
// Allocate all ISTs for this core.
// Every task later gets its own IST1, so the IST1 allocated here is only used by the Idle task.
for i in 0..IST_ENTRIES {
let ist = ::mm::allocate(KERNEL_STACK_SIZE, true);
let ist = crate::mm::allocate(KERNEL_STACK_SIZE, true);
boxed_tss.ist[i] = (ist + KERNEL_STACK_SIZE - 0x10) as u64;
}
......
......@@ -8,12 +8,12 @@
#![allow(dead_code)]
use arch::x86_64::kernel::gdt;
use crate::arch::x86_64::kernel::gdt;
use crate::x86::bits64::paging::VAddr;
use crate::x86::dtables::{self, DescriptorTablePointer};
use crate::x86::segmentation::{SegmentSelector, SystemDescriptorTypes64};
use crate::x86::Ring;
use core::sync::atomic::{AtomicBool, Ordering};
use x86::bits64::paging::VAddr;
use x86::dtables::{self, DescriptorTablePointer};
use x86::segmentation::{SegmentSelector, SystemDescriptorTypes64};
use x86::Ring;
/// An interrupt gate descriptor.
///
......@@ -84,7 +84,7 @@ impl IdtEntry {
base_lo: ((handler.as_usize() as u64) & 0xFFFF) as u16,
base_hi: handler.as_usize() as u64 >> 16,
selector: gdt_code_selector,
ist_index: ist_index,
ist_index,
flags: dpl as u8 | ty.pack() | (1 << 7),
reserved1: 0,
}
......
......@@ -6,14 +6,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::x86_64::kernel::apic;
use arch::x86_64::kernel::idt;
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::processor;
use arch::x86_64::mm::paging;
use crate::arch::x86_64::kernel::apic;
use crate::arch::x86_64::kernel::idt;
use crate::arch::x86_64::kernel::percore::*;
use crate::arch::x86_64::kernel::processor;
use crate::arch::x86_64::mm::paging;
use crate::scheduler;
use crate::x86::bits64::rflags;
use core::fmt;
use scheduler;
use x86::bits64::rflags;
// Derived from Philipp Oppermann's blog
// => https://github.com/phil-opp/blog_os/blob/master/src/interrupts/mod.rs
......
......@@ -35,14 +35,14 @@ pub mod virtio;
pub mod virtio_fs;
pub mod virtio_net;
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::serial::SerialPort;
use crate::arch::x86_64::kernel::percore::*;
use crate::arch::x86_64::kernel::serial::SerialPort;
use crate::environment;
use crate::kernel_message_buffer;
#[cfg(feature = "newlib")]
use core::slice;
use core::{intrinsics, ptr};
use environment;
use kernel_message_buffer;
const SERIAL_PORT_BAUDRATE: u32 = 115_200;
......@@ -94,11 +94,7 @@ static mut COM1: SerialPort = SerialPort::new(0x3f8);
pub fn has_ipdevice() -> bool {
let ip = unsafe { core::ptr::read_volatile(&(*BOOT_INFO).hcip) };
if ip[0] == 255 && ip[1] == 255 && ip[2] == 255 && ip[3] == 255 {
false
} else {
true
}
!(ip[0] == 255 && ip[1] == 255 && ip[2] == 255 && ip[3] == 255)
}
#[cfg(not(feature = "newlib"))]
......@@ -292,8 +288,8 @@ pub fn boot_processor_init() {
vga::init();
}
::mm::init();
::mm::print_information();
crate::mm::init();
crate::mm::print_information();
environment::init();
gdt::init();
gdt::add_current_core();
......
......@@ -6,17 +6,17 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use crate::arch::x86_64::kernel::pci_ids::{CLASSES, VENDORS};
use crate::arch::x86_64::kernel::virtio;
use crate::arch::x86_64::kernel::virtio_fs::VirtioFsDriver;
use crate::arch::x86_64::kernel::virtio_net::VirtioNetDriver;
use crate::synch::spinlock::SpinlockIrqSave;
use crate::x86::io::*;
use alloc::rc::Rc;
use alloc::vec::Vec;
use arch::x86_64::kernel::pci_ids::{CLASSES, VENDORS};
use arch::x86_64::kernel::virtio;
use arch::x86_64::kernel::virtio_fs::VirtioFsDriver;
use arch::x86_64::kernel::virtio_net::VirtioNetDriver;
use core::cell::RefCell;
use core::convert::TryInto;
use core::{fmt, u32, u8};
use synch::spinlock::SpinlockIrqSave;
use x86::io::*;
// TODO: should these be pub? currently needed since used in virtio.rs maybe use getter methods to be more flexible.
pub const PCI_MAX_BUS_NUMBER: u8 = 32;
......@@ -249,7 +249,7 @@ fn parse_bars(bus: u8, device: u8, vendor_id: u16, device_id: u16) -> Vec<PciBar
}
}
return bars;
bars
}
impl PciAdapter {
......@@ -274,10 +274,10 @@ impl PciAdapter {
let interrupt_info = read_config(bus, device, PCI_INTERRUPT_REGISTER);
Some(Self {
bus: bus,
device: device,
vendor_id: vendor_id,
device_id: device_id,
bus,
device,
vendor_id,
device_id,
class_id: (class_ids >> 24) as u8,
subclass_id: (class_ids >> 16) as u8,
programming_interface_id: (class_ids >> 8) as u8,
......@@ -294,33 +294,33 @@ impl PciAdapter {
/// Returns the bar at bar-register baridx.
pub fn get_bar(&self, baridx: u8) -> Option<PciBar> {
for bar in &self.base_addresses {
match bar {
PciBar::IO(bar) => {
if bar.index == baridx {
return Some(PciBar::IO(*bar));
for pci_bar in &self.base_addresses {
match pci_bar {
PciBar::IO(pci_bar) => {
if pci_bar.index == baridx {
return Some(PciBar::IO(*pci_bar));
}
}
PciBar::Memory(bar) => {
if bar.index == baridx {
return Some(PciBar::Memory(*bar));
PciBar::Memory(pci_bar) => {
if pci_bar.index == baridx {
return Some(PciBar::Memory(*pci_bar));
}
}
}
}
return None;
None
}
/// Memory maps pci bar with specified index to identical location in virtual memory.
/// no_cache determines if we set the `Cache Disable` flag in the page-table-entry.
/// Returns (virtual-pointer, size) if successful, else None (if bar non-existent or IOSpace)
pub fn memory_map_bar(&self, index: u8, no_cache: bool) -> Option<(usize, usize)> {
let bar = match self.get_bar(index) {
let pci_bar = match self.get_bar(index) {
Some(PciBar::IO(_)) => {
warn!("Cannot map IOBar!");
return None;
}
Some(PciBar::Memory(bar)) => bar,
Some(PciBar::Memory(mem_bar)) => mem_bar,
None => {
warn!("Memory bar not found!");
return None;
......@@ -329,32 +329,32 @@ impl PciAdapter {
debug!(
"Mapping bar {} at 0x{:x} with length 0x{:x}",
index, bar.addr, bar.size
index, pci_bar.addr, pci_bar.size
);
if bar.width != 64 {
if pci_bar.width != 64 {
warn!("Currently only mapping of 64 bit bars is supported!");
return None;
}
if !bar.prefetchable {
if !pci_bar.prefetchable {
warn!("Currently only mapping of prefetchable bars is supported!")
}
// Since the bios/bootloader manages the physical address space, the address got from the bar is unique and not overlapping.
// We therefore do not need to reserve any additional memory in our kernel.
// Map bar into RW^X virtual memory
let physical_address = bar.addr;
let virtual_address = ::mm::map(physical_address, bar.size, true, false, no_cache);
let physical_address = pci_bar.addr;
let virtual_address = crate::mm::map(physical_address, pci_bar.size, true, false, no_cache);
Some((virtual_address, bar.size))
Some((virtual_address, pci_bar.size))
}
}
impl fmt::Display for PciBar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (typ, addr, size) = match self {
PciBar::IO(bar) => ("IOBar", bar.addr as usize, bar.size as usize),
PciBar::Memory(bar) => ("MemoryBar", bar.addr, bar.size),
PciBar::IO(io_bar) => ("IOBar", io_bar.addr as usize, io_bar.size as usize),
PciBar::Memory(mem_bar) => ("MemoryBar", mem_bar.addr, mem_bar.size),
};
write!(f, "{}: 0x{:x} (size 0x{:x})", typ, addr, size)?;
......@@ -417,8 +417,8 @@ impl fmt::Display for PciAdapter {
write!(f, ", IRQ {}", self.irq)?;
}
for bar in &self.base_addresses {
write!(f, ", {}", bar)?;
for pci_bar in &self.base_addresses {
write!(f, ", {}", pci_bar)?;
}
Ok(())
......
......@@ -5,11 +5,11 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use arch::x86_64::kernel::BOOT_INFO;
use crate::arch::x86_64::kernel::BOOT_INFO;
use crate::scheduler::{CoreId, PerCoreScheduler};
use crate::x86::bits64::task::TaskStateSegment;