1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-22 16:16:16 +04:00

update kernel to 2018 edition

This commit is contained in:
WangRunji 2018-11-19 17:21:06 +08:00
parent 4495622317
commit 56fcad245a
42 changed files with 203 additions and 229 deletions

15
kernel/Cargo.lock generated
View File

@ -179,11 +179,6 @@ dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "remove_dir_all"
version = "0.5.1"
@ -226,11 +221,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "simple-filesystem"
version = "0.0.1"
source = "git+https://github.com/wangrunji0408/SimpleFileSystem-Rust?branch=multi-thread#24a51faff266744d83c1ef60328b28906d2b525c"
source = "git+https://github.com/wangrunji0408/SimpleFileSystem-Rust?branch=multi-thread#249383f7e3f195e0a8ce858f40394e9d5f7f58e0"
dependencies = [
"bit-vec 0.5.0 (git+https://github.com/AltSysrq/bit-vec.git)",
"spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -249,7 +244,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "static_assertions"
version = "0.2.5"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -287,7 +282,6 @@ dependencies = [
"once 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"pc-keyboard 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"riscv 0.3.0 (git+https://github.com/riscv-and-rust-and-decaf/riscv)",
"simple-filesystem 0.0.1 (git+https://github.com/wangrunji0408/SimpleFileSystem-Rust?branch=multi-thread)",
"spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
@ -425,7 +419,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07"
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
"checksum raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "30a9d219c32c9132f7be513c18be77c9881c7107d2ab5569d205a6a0f0e6dc7d"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
"checksum riscv 0.3.0 (git+https://github.com/riscv-and-rust-and-decaf/riscv)" = "<none>"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
@ -434,7 +427,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum simple-filesystem 0.0.1 (git+https://github.com/wangrunji0408/SimpleFileSystem-Rust?branch=multi-thread)" = "<none>"
"checksum skeptic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "061203a849117b0f7090baf8157aa91dac30545208fbb85166ac58b4ca33d89c"
"checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f"
"checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5"
"checksum static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "389ce475f424f267dbed6479cbd8f126c5e1afb053b0acdaa019c74305fc65d1"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum uart_16550 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "269f953d8de3226f7c065c589c7b4a3e83d10a419c7c3b5e2e0f197e6acc966e"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"

View File

@ -2,6 +2,7 @@
name = "ucore"
version = "0.1.0"
authors = ["Runji Wang <wangrunji0408@163.com>"]
edition = "2018"
[features]
link_user_program = []
@ -36,7 +37,6 @@ apic = { git = "https://github.com/wangrunji0408/APIC-Rust" }
#apic = { path = "../crate/apic" }
x86_64 = "0.3"
raw-cpuid = "6.0"
redox_syscall = "0.1"
uart_16550 = "0.1"
pc-keyboard = "0.3"

View File

@ -1,4 +1,4 @@
use super::super::riscv::register::*;
use riscv::register::*;
#[derive(Clone)]
#[repr(C)]

View File

@ -1,6 +1,6 @@
use consts::MAX_CPU_NUM;
use crate::consts::MAX_CPU_NUM;
use core::ptr::{read_volatile, write_volatile};
use memory::*;
use crate::memory::*;
static mut STARTED: [bool; MAX_CPU_NUM] = [false; MAX_CPU_NUM];
@ -15,7 +15,7 @@ pub fn id() -> usize {
}
pub fn send_ipi(cpu_id: usize) {
super::bbl::sbi::send_ipi(1 << cpu_id);
bbl::sbi::send_ipi(1 << cpu_id);
}
pub unsafe fn has_started(cpu_id: usize) -> bool {
@ -31,6 +31,6 @@ pub unsafe fn start_others(hart_mask: usize) {
}
pub fn halt() {
use super::riscv::asm::wfi;
use riscv::asm::wfi;
unsafe { wfi() }
}

View File

@ -1,7 +1,7 @@
use super::riscv::register::*;
use riscv::register::*;
pub use self::context::*;
use ::memory::{InactivePageTable0, memory_set_record};
use memory::MemorySet;
use crate::memory::{MemorySet, InactivePageTable0, memory_set_record};
use log::*;
#[path = "context.rs"]
mod context;
@ -71,7 +71,7 @@ pub unsafe fn restore(flags: usize) {
*/
#[no_mangle]
pub extern fn rust_trap(tf: &mut TrapFrame) {
use super::riscv::register::scause::{Trap, Interrupt as I, Exception as E};
use riscv::register::scause::{Trap, Interrupt as I, Exception as E};
trace!("Interrupt @ CPU{}: {:?} ", super::cpu::id(), tf.scause.cause());
match tf.scause.cause() {
Trap::Interrupt(I::SupervisorExternal) => serial(),
@ -82,18 +82,18 @@ pub extern fn rust_trap(tf: &mut TrapFrame) {
Trap::Exception(E::LoadPageFault) => page_fault(tf),
Trap::Exception(E::StorePageFault) => page_fault(tf),
Trap::Exception(E::InstructionPageFault) => page_fault(tf),
_ => ::trap::error(tf),
_ => crate::trap::error(tf),
}
trace!("Interrupt end");
}
fn serial() {
::trap::serial(super::io::getchar());
crate::trap::serial(super::io::getchar());
}
fn ipi() {
debug!("IPI");
super::bbl::sbi::clear_ipi();
bbl::sbi::clear_ipi();
}
/*
@ -101,7 +101,7 @@ fn ipi() {
* process timer interrupt
*/
fn timer() {
::trap::timer();
crate::trap::timer();
super::timer::set_next();
}
@ -113,7 +113,7 @@ fn timer() {
*/
fn syscall(tf: &mut TrapFrame) {
tf.sepc += 4; // Must before syscall, because of fork.
let ret = ::syscall::syscall(tf.x[10], [tf.x[11], tf.x[12], tf.x[13], tf.x[14], tf.x[15], tf.x[16]], tf);
let ret = crate::syscall::syscall(tf.x[10], [tf.x[11], tf.x[12], tf.x[13], tf.x[14], tf.x[15], tf.x[16]], tf);
tf.x[10] = ret as usize;
}
@ -125,7 +125,7 @@ fn syscall(tf: &mut TrapFrame) {
*/
fn illegal_inst(tf: &mut TrapFrame) {
if !emulate_mul_div(tf) {
::trap::error(tf);
crate::trap::error(tf);
}
}
@ -139,8 +139,8 @@ fn page_fault(tf: &mut TrapFrame) {
let addr = stval::read();
trace!("\nEXCEPTION: Page Fault @ {:#x}", addr);
if !::memory::page_fault_handler(addr) {
::trap::error(tf);
if !crate::memory::page_fault_handler(addr) {
crate::trap::error(tf);
}
}

View File

@ -1,6 +1,6 @@
use core::fmt::{Write, Result, Arguments};
use core::ptr::{read_volatile, write_volatile};
use super::bbl::sbi;
use bbl::sbi;
struct SerialPort;

View File

@ -1,7 +1,8 @@
use core::{slice, mem};
use memory::{active_table, FRAME_ALLOCATOR, init_heap, MemoryArea, MemoryAttr, MemorySet};
use super::riscv::{addr::*, register::sstatus};
use riscv::{addr::*, register::sstatus};
use ucore_memory::PAGE_SIZE;
use log::*;
use crate::memory::{active_table, FRAME_ALLOCATOR, init_heap, MemoryArea, MemoryAttr, MemorySet};
/*
* @brief:
@ -36,7 +37,7 @@ pub fn init_other() {
fn init_frame_allocator() {
use bit_allocator::BitAlloc;
use core::ops::Range;
use consts::{MEMORY_OFFSET, MEMORY_END};
use crate::consts::{MEMORY_OFFSET, MEMORY_END};
let mut ba = FRAME_ALLOCATOR.lock();
ba.insert(to_range(end as usize + PAGE_SIZE, MEMORY_END));

View File

@ -1,6 +1,3 @@
extern crate riscv;
extern crate bbl;
pub mod io;
pub mod interrupt;
pub mod timer;
@ -21,21 +18,21 @@ pub extern fn rust_main(hartid: usize, dtb: usize, hart_mask: usize) -> ! {
unreachable!();
}
::logging::init();
crate::logging::init();
interrupt::init();
memory::init();
timer::init();
::process::init();
crate::process::init();
unsafe { cpu::start_others(hart_mask); }
::kmain();
crate::kmain();
}
fn others_main() -> ! {
interrupt::init();
memory::init_other();
timer::init();
::kmain();
crate::kmain();
}
#[cfg(feature = "no_bbl")]

View File

@ -1,14 +1,15 @@
use consts::{KERNEL_P2_INDEX, RECURSIVE_INDEX};
use crate::consts::{KERNEL_P2_INDEX, RECURSIVE_INDEX};
// Depends on kernel
use memory::{active_table, alloc_frame, dealloc_frame};
use super::riscv::addr::*;
use super::riscv::asm::{sfence_vma, sfence_vma_all};
use super::riscv::paging::{Mapper, PageTable as RvPageTable, PageTableEntry, PageTableFlags as EF, RecursivePageTable};
use super::riscv::paging::{FrameAllocator, FrameDeallocator};
use super::riscv::register::satp;
use crate::memory::{active_table, alloc_frame, dealloc_frame};
use riscv::addr::*;
use riscv::asm::{sfence_vma, sfence_vma_all};
use riscv::paging::{Mapper, PageTable as RvPageTable, PageTableEntry, PageTableFlags as EF, RecursivePageTable};
use riscv::paging::{FrameAllocator, FrameDeallocator};
use riscv::register::satp;
use ucore_memory::memory_set::*;
use ucore_memory::PAGE_SIZE;
use ucore_memory::paging::*;
use log::*;
/*
* @param:
@ -30,7 +31,7 @@ pub fn setup_page_table(frame: Frame) {
p2.map_identity(KERNEL_P2_INDEX + 1, EF::VALID | EF::READABLE | EF::WRITABLE | EF::EXECUTABLE);
p2.map_identity(KERNEL_P2_INDEX + 2, EF::VALID | EF::READABLE | EF::WRITABLE | EF::EXECUTABLE);
use super::riscv::register::satp;
use riscv::register::satp;
unsafe { satp::set(satp::Mode::Sv32, 0, frame); }
sfence_vma_all();
info!("setup init page table end");

View File

@ -1,5 +1,6 @@
use super::riscv::register::*;
use super::bbl::sbi;
use riscv::register::*;
use bbl::sbi;
use log::*;
/*
* @brief:

View File

@ -1,5 +1,5 @@
use super::apic::{LocalApic, XApic};
use super::raw_cpuid::CpuId;
use apic::{LocalApic, XApic};
use raw_cpuid::CpuId;
/// Exit qemu
/// See: https://wiki.osdev.org/Shutdown

View File

@ -1,14 +1,11 @@
extern crate pc_keyboard;
use spin::Mutex;
use x86_64::instructions::port::Port;
use self::pc_keyboard::{Keyboard, ScancodeSet1, DecodedKey, layouts};
use pc_keyboard::{Keyboard, ScancodeSet1, DecodedKey, layouts};
use lazy_static::lazy_static;
pub fn init() {
assert_has_not_been_called!("keyboard::init must be called only once");
use arch::interrupt::consts::*;
use arch::interrupt::enable_irq;
use crate::arch::interrupt::consts::*;
use crate::arch::interrupt::enable_irq;
enable_irq(IRQ_KBD);
}

View File

@ -1,4 +1,4 @@
extern crate syscall as redox_syscall;
use once::*;
pub mod vga;
pub mod serial;

View File

@ -1,19 +1,23 @@
// Copy from Redox
use super::redox_syscall::io::*;
use x86_64::instructions::port::Port;
use spin::Mutex;
use once::*;
use log::*;
static MASTER: Mutex<Pic> = Mutex::new(Pic::new(0x20));
static SLAVE: Mutex<Pic> = Mutex::new(Pic::new(0xA0));
pub fn disable() {
// Mask all interrupts (Copy from xv6 x86_64)
MASTER.lock().cmd.write(0xFF);
SLAVE.lock().cmd.write(0xFF);
unsafe {
MASTER.lock().cmd.write(0xFF);
SLAVE.lock().cmd.write(0xFF);
}
info!("pic: disabled");
}
pub fn init() {
pub unsafe fn init() {
assert_has_not_been_called!("pic::init must be called only once");
let mut master = MASTER.lock();
@ -46,8 +50,7 @@ pub fn init() {
info!("pic: init end");
}
pub fn enable_irq(irq: u8)
{
pub fn enable_irq(irq: u8) {
match irq {
_ if irq < 8 => MASTER.lock().mask_set(irq),
_ if irq < 16 => SLAVE.lock().mask_set(irq-8),
@ -64,35 +67,35 @@ pub fn ack(irq: u8) {
}
struct Pic {
cmd: Pio<u8>,
data: Pio<u8>,
cmd: Port<u8>,
data: Port<u8>,
}
impl Pic {
const fn new(port: u16) -> Pic {
Pic {
cmd: Pio::new(port),
data: Pio::new(port + 1),
cmd: Port::new(port),
data: Port::new(port + 1),
}
}
fn ack(&mut self) {
self.cmd.write(0x20);
unsafe { self.cmd.write(0x20); }
}
fn mask_set(&mut self, irq: u8) {
assert!(irq < 8);
let mut mask = self.data.read();
mask |= 1 << irq;
self.data.write(mask);
unsafe {
let mask = self.data.read() | 1 << irq;
self.data.write(mask);
}
}
fn mask_clear(&mut self, irq: u8) {
assert!(irq < 8);
let mut mask = self.data.read();
mask &= !(1 << irq);
self.data.write(mask);
unsafe {
let mask = self.data.read() & !(1 << irq);
self.data.write(mask);
}
}
}

View File

@ -1,4 +1,6 @@
use super::redox_syscall::io::{Io, Pio};
use x86_64::instructions::port::Port;
use log::*;
use once::*;
pub fn init() {
assert_has_not_been_called!("pit::init must be called only once");
@ -7,26 +9,28 @@ pub fn init() {
}
struct Pit {
chan0: Pio<u8>,
chan1: Pio<u8>,
chan2: Pio<u8>,
command: Pio<u8>,
chan0: Port<u8>,
chan1: Port<u8>,
chan2: Port<u8>,
command: Port<u8>,
}
impl Pit {
const fn new(port: u16) -> Self {
Pit {
chan0: Pio::new(port),
chan1: Pio::new(port+1),
chan2: Pio::new(port+2),
command: Pio::new(port+3),
chan0: Port::new(port),
chan1: Port::new(port + 1),
chan2: Port::new(port + 2),
command: Port::new(port + 3),
}
}
pub fn init(&mut self, freq: u32) {
self.command.write(TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
let div = Pit::divisor(freq);
self.chan0.write((div & 0xFF) as u8);
self.chan0.write((div >> 8) as u8);
unsafe {
self.command.write(TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
let div = Pit::divisor(freq);
self.chan0.write((div & 0xFF) as u8);
self.chan0.write((div >> 8) as u8);
}
}
fn divisor(freq: u32) -> u16 {
let div = (TIMER_FREQ + freq / 2) / freq;

View File

@ -1,10 +1,10 @@
// Copy from Redox
extern crate uart_16550;
use core::fmt::{self, Write};
use super::redox_syscall::io::{Io, Pio};
use x86_64::instructions::port::Port;
use spin::Mutex;
use self::uart_16550::SerialPort;
use uart_16550::SerialPort;
use once::*;
pub static COM1: Mutex<SerialPort> = Mutex::new(SerialPort::new(0x3F8));
pub static COM2: Mutex<SerialPort> = Mutex::new(SerialPort::new(0x2F8));
@ -14,7 +14,7 @@ pub fn init() {
COM1.lock().init();
COM2.lock().init();
use arch::interrupt::{enable_irq, consts::{IRQ_COM1, IRQ_COM2}};
use crate::arch::interrupt::{enable_irq, consts::{IRQ_COM1, IRQ_COM2}};
enable_irq(IRQ_COM1);
enable_irq(IRQ_COM2);
}
@ -26,7 +26,7 @@ pub trait SerialRead {
impl SerialRead for SerialPort {
fn receive(&mut self) -> u8 {
unsafe {
let ports = self as *mut _ as *mut [Pio<u8>; 6];
let ports = self as *mut _ as *mut [Port<u8>; 6];
let line_sts = &(*ports)[5];
let data = &(*ports)[0];
data.read()

View File

@ -1,10 +1,11 @@
use consts::KERNEL_OFFSET;
use core::ptr::Unique;
use core::fmt;
use spin::Mutex;
use volatile::Volatile;
use x86_64::instructions::port::Port;
use logging::Color;
use crate::logging::Color;
use crate::consts::KERNEL_OFFSET;
use lazy_static::lazy_static;
pub const VGA_BUFFER: Unique<VgaBuffer> = unsafe {
Unique::new_unchecked((KERNEL_OFFSET + 0xb8000) as *mut _)

View File

@ -1,11 +1,12 @@
use alloc::boxed::Box;
use consts::MAX_CPU_NUM;
use crate::consts::MAX_CPU_NUM;
use core::fmt;
use core::fmt::Debug;
use spin::{Mutex, MutexGuard, Once};
use x86_64::{PrivilegeLevel, VirtAddr};
use x86_64::structures::gdt::*;
use x86_64::structures::tss::TaskStateSegment;
use log::*;
/// Alloc TSS & GDT at kernel heap, then init and load it.
/// The double fault stack will be allocated at kernel heap too.
@ -66,7 +67,7 @@ pub struct Cpu {
impl Cpu {
pub fn current() -> MutexGuard<'static, Cpu> {
CPUS[super::cpu::id()].try().unwrap().lock()
CPUS[super::cpu::id()].r#try().unwrap().lock()
}
/// 设置从Ring3跳到Ring0时自动切换栈的地址

View File

@ -1,4 +1,5 @@
use x86_64::structures::idt::*;
use lazy_static::lazy_static;
pub fn init() {
IDT.load();
@ -6,8 +7,8 @@ pub fn init() {
lazy_static! {
static ref IDT: Idt = {
use arch::interrupt::consts::*;
use arch::gdt::DOUBLE_FAULT_IST_INDEX;
use crate::arch::interrupt::consts::*;
use crate::arch::gdt::DOUBLE_FAULT_IST_INDEX;
use x86_64::PrivilegeLevel;
use core::mem::transmute;

View File

@ -66,6 +66,7 @@
use super::consts::*;
use super::TrapFrame;
use log::*;
global_asm!(include_str!("trap.asm"));
global_asm!(include_str!("vector.asm"));
@ -82,7 +83,7 @@ pub extern fn rust_trap(tf: &mut TrapFrame) {
let irq = tf.trap_num as u8 - T_IRQ0;
super::ack(irq); // must ack before switching
match irq {
IRQ_TIMER => ::trap::timer(),
IRQ_TIMER => crate::trap::timer(),
IRQ_KBD => keyboard(),
IRQ_COM1 => com1(),
IRQ_COM2 => com2(),
@ -112,7 +113,7 @@ fn page_fault(tf: &mut TrapFrame) {
let addr: usize;
unsafe { asm!("mov %cr2, $0" : "=r" (addr)); }
if ::memory::page_fault_handler(addr) {
if crate::memory::page_fault_handler(addr) {
return;
}
error!("\nEXCEPTION: Page Fault @ {:#x}, code: {:#x}", addr, tf.error_code);
@ -120,21 +121,21 @@ fn page_fault(tf: &mut TrapFrame) {
}
fn keyboard() {
use arch::driver::keyboard;
use crate::arch::driver::keyboard;
trace!("\nInterupt: Keyboard");
if let Some(c) = keyboard::receive() {
::trap::serial(c);
crate::trap::serial(c);
}
}
fn com1() {
use arch::driver::serial::*;
use crate::arch::driver::serial::*;
trace!("\nInterupt: COM1");
::trap::serial(COM1.lock().receive() as char);
crate::trap::serial(COM1.lock().receive() as char);
}
fn com2() {
use arch::driver::serial::*;
use crate::arch::driver::serial::*;
trace!("\nInterupt: COM2");
COM2.lock().receive();
}
@ -144,7 +145,7 @@ fn ide() {
}
fn to_user(tf: &mut TrapFrame) {
use arch::gdt;
use crate::arch::gdt;
info!("\nInterupt: To User");
tf.cs = gdt::UCODE_SELECTOR.0 as usize;
tf.ss = gdt::UDATA_SELECTOR.0 as usize;
@ -152,7 +153,7 @@ fn to_user(tf: &mut TrapFrame) {
}
fn to_kernel(tf: &mut TrapFrame) {
use arch::gdt;
use crate::arch::gdt;
info!("\nInterupt: To Kernel");
tf.cs = gdt::KCODE_SELECTOR.0 as usize;
tf.ss = gdt::KDATA_SELECTOR.0 as usize;
@ -160,25 +161,23 @@ fn to_kernel(tf: &mut TrapFrame) {
fn syscall(tf: &mut TrapFrame) {
trace!("\nInterupt: Syscall {:#x?}", tf.rax);
use syscall::syscall;
let ret = syscall(tf.rax, [tf.rdi, tf.rsi, tf.rdx, tf.rcx, tf.r8, tf.r9], tf);
let ret = crate::syscall::syscall(tf.rax, [tf.rdi, tf.rsi, tf.rdx, tf.rcx, tf.r8, tf.r9], tf);
tf.rax = ret as usize;
}
fn syscall32(tf: &mut TrapFrame) {
trace!("\nInterupt: Syscall {:#x?}", tf.rax);
use syscall::syscall;
let ret = syscall(tf.rax, [tf.rdx, tf.rcx, tf.rbx, tf.rdi, tf.rsi, 0], tf);
let ret = crate::syscall::syscall(tf.rax, [tf.rdx, tf.rcx, tf.rbx, tf.rdi, tf.rsi, 0], tf);
tf.rax = ret as usize;
}
fn error(tf: &TrapFrame) {
::trap::error(tf);
crate::trap::error(tf);
}
#[no_mangle]
pub extern fn set_return_rsp(tf: &TrapFrame) {
use arch::gdt::Cpu;
use crate::arch::gdt::Cpu;
use core::mem::size_of;
if tf.cs & 0x3 == 3 {
Cpu::current().set_ring0_rsp(tf as *const _ as usize + size_of::<TrapFrame>());

View File

@ -1,13 +1,11 @@
use x86_64;
pub mod consts;
mod handler;
mod trapframe;
pub use self::trapframe::*;
pub use self::handler::*;
use super::apic::*;
use consts::KERNEL_OFFSET;
use apic::*;
use crate::consts::KERNEL_OFFSET;
#[inline(always)]
pub unsafe fn enable() {

View File

@ -36,7 +36,7 @@ pub struct TrapFrame {
/// 用于在内核栈中构造新线程的中断帧
impl TrapFrame {
fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, rsp: usize) -> Self {
use arch::gdt;
use crate::arch::gdt;
let mut tf = TrapFrame::default();
tf.rdi = arg;
tf.cs = gdt::KCODE_SELECTOR.0 as usize;
@ -47,7 +47,7 @@ impl TrapFrame {
tf
}
fn new_user_thread(entry_addr: usize, rsp: usize, is32: bool) -> Self {
use arch::gdt;
use crate::arch::gdt;
let mut tf = TrapFrame::default();
tf.cs = if is32 { gdt::UCODE32_SELECTOR.0 } else { gdt::UCODE_SELECTOR.0 } as usize;
tf.rip = entry_addr;
@ -103,7 +103,6 @@ extern {
/// The entry point of new thread
extern fn forkret() {
debug!("forkret");
// Will return to `trapret`
}

View File

@ -1,10 +1,12 @@
use bit_allocator::BitAlloc;
use consts::KERNEL_OFFSET;
use crate::consts::KERNEL_OFFSET;
// Depends on kernel
use memory::{FRAME_ALLOCATOR, init_heap, active_table};
use crate::memory::{FRAME_ALLOCATOR, init_heap, active_table};
use super::{BootInfo, MemoryRegionType};
use ucore_memory::PAGE_SIZE;
use ucore_memory::paging::*;
use once::*;
use log::*;
pub fn init(boot_info: &BootInfo) {
assert_has_not_been_called!("memory::init must be called only once");

View File

@ -1,10 +1,7 @@
extern crate bootloader;
extern crate apic;
extern crate raw_cpuid;
use self::bootloader::bootinfo::{BootInfo, MemoryRegionType};
use bootloader::bootinfo::{BootInfo, MemoryRegionType};
use core::sync::atomic::*;
use consts::KERNEL_OFFSET;
use log::*;
use crate::consts::KERNEL_OFFSET;
pub mod driver;
pub mod cpu;
@ -30,7 +27,7 @@ pub extern "C" fn _start(boot_info: &'static BootInfo) -> ! {
}
// First init log mod, so that we can print log info.
::logging::init();
crate::logging::init();
info!("Hello world!");
info!("{:#?}", boot_info);
@ -47,11 +44,11 @@ pub extern "C" fn _start(boot_info: &'static BootInfo) -> ! {
driver::init();
::process::init();
crate::process::init();
AP_CAN_INIT.store(true, Ordering::Relaxed);
::kmain();
crate::kmain();
}
/// The entry point for other processors
@ -59,5 +56,5 @@ fn other_start() -> ! {
idt::init();
gdt::init();
cpu::init();
::kmain();
crate::kmain();
}

View File

@ -1,6 +1,6 @@
use bit_allocator::{BitAlloc, BitAlloc64K};
// Depends on kernel
use memory::{active_table, alloc_frame, dealloc_frame};
use crate::memory::{active_table, alloc_frame, dealloc_frame};
use spin::{Mutex, MutexGuard};
use ucore_memory::cow::CowExt;
use ucore_memory::memory_set::*;
@ -12,6 +12,7 @@ use x86_64::registers::control::{Cr3, Cr3Flags};
use x86_64::structures::paging::{Mapper, PageTable as x86PageTable, PageTableEntry, PageTableFlags as EF, RecursivePageTable};
use x86_64::structures::paging::{FrameAllocator, FrameDeallocator, Page, PageRange, PhysFrame as Frame, Size4KiB};
use x86_64::ux::u9;
use log::*;
pub trait PageExt {
fn of_addr(address: usize) -> Self;

View File

@ -1,6 +1,6 @@
#![allow(dead_code)]
pub use arch::consts::*;
pub use crate::arch::consts::*;
pub const MAX_CPU_NUM: usize = 8;
pub const MAX_PROCESS_NUM: usize = 128;

View File

@ -1,15 +1,14 @@
use simple_filesystem::*;
use alloc::{boxed::Box, sync::Arc, string::String, collections::VecDeque, vec::Vec};
#[cfg(target_arch = "x86_64")]
use arch::driver::ide;
use sync::Condvar;
use sync::SpinNoIrqLock as Mutex;
use core::any::Any;
use core::slice;
use ::memory::{InactivePageTable0, memory_set_record};
use memory::MemorySet;
use process::context::memory_set_map_swappable;
use lazy_static::lazy_static;
use crate::memory::{MemorySet, InactivePageTable0, memory_set_record};
use crate::process::context::memory_set_map_swappable;
#[cfg(target_arch = "x86_64")]
use crate::arch::driver::ide;
use crate::sync::Condvar;
use crate::sync::SpinNoIrqLock as Mutex;
// Hard link user program
#[cfg(target_arch = "riscv32")]

View File

@ -2,23 +2,21 @@
use core::panic::PanicInfo;
use core::alloc::Layout;
use log::*;
#[lang = "eh_personality"]
extern fn eh_personality() {
}
#[panic_handler]
#[no_mangle]
pub fn panic(info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
let location = info.location().unwrap();
let message = info.message().unwrap();
error!("\n\nPANIC in {} at line {}\n {}", location.file(), location.line(), message);
use arch::cpu::halt;
loop { halt() }
loop { crate::arch::cpu::halt() }
}
#[lang = "oom"]
#[no_mangle]
pub fn oom(_: Layout) -> ! {
fn oom(_: Layout) -> ! {
panic!("out of memory");
}

View File

@ -5,43 +5,23 @@
#![feature(naked_functions)]
#![feature(asm)]
#![feature(optin_builtin_traits)]
#![feature(panic_handler)]
#![feature(panic_info_message)]
#![feature(global_asm)]
#![feature(compiler_builtins_lib)]
#![feature(raw)]
#![feature(vec_resize_default)]
#![feature(extern_crate_item_prelude)]
#![no_std]
#[macro_use]
// just keep it ...
extern crate alloc;
extern crate bit_allocator;
extern crate bit_field;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
extern crate linked_list_allocator;
#[macro_use]
extern crate log;
#[macro_use]
extern crate once;
extern crate simple_filesystem;
extern crate spin;
extern crate ucore_memory;
extern crate ucore_process;
extern crate volatile;
#[cfg(target_arch = "x86_64")]
extern crate x86_64;
extern crate xmas_elf;
pub use process::{processor, new_kernel_context};
pub use crate::process::{processor, new_kernel_context};
use ucore_process::thread;
use linked_list_allocator::LockedHeap;
#[macro_use] // print!
pub mod logging;
mod logging;
mod memory;
mod lang;
mod util;
@ -63,7 +43,7 @@ pub mod arch;
pub mod arch;
pub fn kmain() -> ! {
process::processor().run();
processor().run();
// thread::test::local_key();
// thread::test::unpack();

View File

@ -1,6 +1,7 @@
use core::fmt;
use log::{self, Level, LevelFilter, Log, Metadata, Record};
use spin::Mutex;
use lazy_static::lazy_static;
lazy_static! {
static ref log_mutex: Mutex<()> = Mutex::new(());
@ -42,13 +43,13 @@ macro_rules! with_color {
}
fn print_in_color(args: fmt::Arguments, color: Color) {
use arch::io;
use crate::arch::io;
let mutex = log_mutex.lock();
io::putfmt(with_color!(args, color));
}
pub fn print(args: fmt::Arguments) {
use arch::io;
use crate::arch::io;
let mutex = log_mutex.lock();
io::putfmt(args);
}

View File

@ -2,5 +2,4 @@
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
#[macro_use]
extern crate ucore;
use ucore;

View File

@ -1,16 +1,16 @@
pub use arch::paging::*;
pub use crate::arch::paging::*;
use bit_allocator::{BitAlloc, BitAlloc4K, BitAlloc64K};
use consts::MEMORY_OFFSET;
//use spin::{Mutex, MutexGuard};
use crate::consts::MEMORY_OFFSET;
use super::HEAP_ALLOCATOR;
use ucore_memory::{*, paging::PageTable};
use ucore_memory::cow::CowExt;
pub use ucore_memory::memory_set::{MemoryArea, MemoryAttr, MemorySet as MemorySet_, InactivePageTable};
use ucore_memory::swap::*;
//use process::{processor, PROCESSOR};
use process::{process};
use sync::{SpinNoIrqLock, SpinNoIrq, MutexGuard};
use crate::process::{process};
use crate::sync::{SpinNoIrqLock, SpinNoIrq, MutexGuard};
use alloc::collections::VecDeque;
use lazy_static::*;
use log::*;
pub type MemorySet = MemorySet_<InactivePageTable0>;
@ -27,7 +27,7 @@ lazy_static! {
pub static ref FRAME_ALLOCATOR: SpinNoIrqLock<FrameAlloc> = SpinNoIrqLock::new(FrameAlloc::default());
}
// record the user memory set for pagefault function (swap in/out and frame delayed allocate) temporarily when page fault in new_user() or fork() function
// after the process is set we can use use processor() to get the inactive page table
// after the process is set we can use use crate::processor() to get the inactive page table
lazy_static! {
pub static ref MEMORY_SET_RECORD: SpinNoIrqLock<VecDeque<usize>> = SpinNoIrqLock::new(VecDeque::default());
}
@ -162,7 +162,7 @@ pub fn page_fault_handler(addr: usize) -> bool {
}
pub fn init_heap() {
use consts::KERNEL_HEAP_SIZE;
use crate::consts::KERNEL_HEAP_SIZE;
static mut HEAP: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
unsafe { HEAP_ALLOCATOR.lock().init(HEAP.as_ptr() as usize, KERNEL_HEAP_SIZE); }
info!("heap init end");

View File

@ -1,14 +1,14 @@
use arch::interrupt::{TrapFrame, Context as ArchContext};
use memory::{MemoryArea, MemoryAttr, MemorySet, KernelStack, active_table_swap, alloc_frame};
use crate::arch::interrupt::{TrapFrame, Context as ArchContext};
use crate::memory::{MemoryArea, MemoryAttr, MemorySet, KernelStack, active_table_swap, alloc_frame, InactivePageTable0, memory_set_record};
use xmas_elf::{ElfFile, header, program::{Flags, ProgramHeader, Type}};
use core::fmt::{Debug, Error, Formatter};
use ucore_process::Context;
use alloc::{boxed::Box, collections::BTreeMap, vec::Vec, sync::Arc, string::String};
use ucore_memory::{Page};
use ::memory::{InactivePageTable0, memory_set_record};
use ucore_memory::memory_set::*;
use ucore_process::Context;
use simple_filesystem::file::File;
use spin::Mutex;
use log::*;
// TODO: avoid pub
@ -72,7 +72,7 @@ impl ContextImpl {
assert_eq!(elf.header.pt2.type_().as_type(), header::Type::Executable, "ELF is not executable");
// User stack
use consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER32_STACK_OFFSET};
use crate::consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER32_STACK_OFFSET};
let (ustack_buttom, mut ustack_top) = match is32 {
true => (USER32_STACK_OFFSET, USER32_STACK_OFFSET + USER_STACK_SIZE),
false => (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE),

View File

@ -1,11 +1,12 @@
use spin::Mutex;
pub use self::context::ContextImpl;
pub use ucore_process::*;
use consts::{MAX_CPU_NUM, MAX_PROCESS_NUM};
use arch::cpu;
use crate::consts::{MAX_CPU_NUM, MAX_PROCESS_NUM};
use crate::arch::cpu;
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use sync::Condvar;
use crate::sync::Condvar;
use core::sync::atomic::*;
use log::*;
pub mod context;
pub fn init() {
@ -25,7 +26,7 @@ pub fn init() {
for i in 0..4 {
manager.add(ContextImpl::new_kernel(idle, i), 0);
}
::shell::run_user_shell();
crate::shell::run_user_shell();
info!("process init end");
}

View File

@ -2,8 +2,8 @@
use alloc::string::String;
use alloc::vec::Vec;
use fs::{ROOT_INODE, INodeExt};
use process::*;
use crate::fs::{ROOT_INODE, INodeExt};
use crate::process::*;
pub fn run_user_shell() {
let inode = ROOT_INODE.lookup("sh").unwrap();
@ -56,5 +56,5 @@ fn get_line() -> String {
}
fn get_char() -> char {
::fs::STDIN.pop()
crate::fs::STDIN.pop()
}

View File

@ -1,4 +0,0 @@
pub struct cpu {
pub id: usize
}

View File

@ -1,6 +1,6 @@
use alloc::collections::VecDeque;
use super::*;
use thread;
use crate::thread;
#[derive(Default)]
pub struct Condvar {

View File

@ -88,7 +88,7 @@ pub mod test {
use alloc::boxed::Box;
use super::*;
use thread;
use crate::thread;
fn smoke() {
let (tx, rx) = channel::<i32>();

View File

@ -26,7 +26,7 @@
//! `MutexSupport`提供了若干接口,它们会在操作锁的不同时间点被调用。
//! 注意这个接口实际是取了几种实现的并集,并不是很通用。
use arch::interrupt;
use crate::arch::interrupt;
use core::cell::UnsafeCell;
use core::fmt;
use core::ops::{Deref, DerefMut};

View File

@ -4,9 +4,11 @@
use alloc::{sync::Arc, vec::Vec};
use core::time::Duration;
use sync::Condvar;
use sync::ThreadLock as Mutex;
use thread;
use crate::sync::Condvar;
use crate::sync::ThreadLock as Mutex;
use crate::thread;
use alloc::vec;
use log::*;
struct Philosopher {
name: &'static str,

View File

@ -1,15 +1,15 @@
//! System call
use arch::interrupt::TrapFrame;
use process::*;
use thread;
use util;
use simple_filesystem::{INode, file::File, FileInfo, FileType};
use core::{slice, str};
use alloc::sync::Arc;
use alloc::{sync::Arc, vec::Vec, string::String};
use spin::Mutex;
use alloc::vec::Vec;
use alloc::string::String;
use log::*;
use bitflags::bitflags;
use crate::arch::interrupt::TrapFrame;
use crate::process::*;
use crate::thread;
use crate::util;
/// System call dispatcher
pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> i32 {
@ -48,7 +48,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> i32 {
_ => {
error!("unknown syscall id: {:#x?}, args: {:x?}", id, args);
::trap::error(tf);
crate::trap::error(tf);
}
};
match ret {
@ -79,11 +79,11 @@ fn sys_open(path: *const u8, flags: usize) -> SysResult {
let flags = VfsFlags::from_ucore_flags(flags);
info!("open: path: {:?}, flags: {:?}", path, flags);
let (fd, inode) = match path {
"stdin:" => (0, ::fs::STDIN.clone() as Arc<INode>),
"stdout:" => (1, ::fs::STDOUT.clone() as Arc<INode>),
"stdin:" => (0, crate::fs::STDIN.clone() as Arc<INode>),
"stdout:" => (1, crate::fs::STDOUT.clone() as Arc<INode>),
_ => {
let fd = (3..).find(|i| !process().files.contains_key(i)).unwrap();
let inode = ::fs::ROOT_INODE.lookup(path)?;
let inode = crate::fs::ROOT_INODE.lookup(path)?;
(fd, inode)
}
};
@ -154,6 +154,7 @@ fn sys_fork(tf: &TrapFrame) -> SysResult {
fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
// TODO: check ptr
loop {
use alloc::vec;
let wait_procs = match pid {
0 => processor().manager().get_children(thread::current().id()),
_ => vec![pid],
@ -199,7 +200,7 @@ fn sys_exec(name: *const u8, argc: usize, argv: *const *const u8, tf: &mut TrapF
// Read program file
let path = args[0].as_str();
let inode = ::fs::ROOT_INODE.lookup(path)?;
let inode = crate::fs::ROOT_INODE.lookup(path)?;
let size = inode.info()?.size;
let mut buf = Vec::with_capacity(size);
unsafe { buf.set_len(size); }
@ -262,7 +263,7 @@ fn sys_sleep(time: usize) -> SysResult {
}
fn sys_get_time() -> SysResult {
unsafe { Ok(::trap::TICK as i32) }
unsafe { Ok(crate::trap::TICK as i32) }
}
fn sys_lab6_set_priority(priority: usize) -> SysResult {

View File

@ -1,6 +1,7 @@
use process::*;
use arch::interrupt::TrapFrame;
use arch::cpu;
use crate::process::*;
use crate::arch::interrupt::TrapFrame;
use crate::arch::cpu;
use log::*;
pub static mut TICK: usize = 0;
@ -22,5 +23,5 @@ pub fn error(tf: &TrapFrame) -> ! {
}
pub fn serial(c: char) {
::fs::STDIN.push(c);
crate::fs::STDIN.push(c);
}