2018-04-02 11:28:32 +04:00
|
|
|
|
#![feature(ptr_internals)]
|
2017-04-11 17:02:21 +04:00
|
|
|
|
#![feature(lang_items)]
|
2017-04-12 21:16:04 +04:00
|
|
|
|
#![feature(const_fn)]
|
2017-11-19 15:41:20 +04:00
|
|
|
|
#![feature(alloc)]
|
2017-11-19 15:54:47 +04:00
|
|
|
|
#![feature(const_unique_new, const_atomic_usize_new)]
|
2017-04-12 21:20:15 +04:00
|
|
|
|
#![feature(unique)]
|
2017-11-19 15:50:16 +04:00
|
|
|
|
#![feature(allocator_api)]
|
2017-11-19 16:13:18 +04:00
|
|
|
|
#![feature(global_allocator)]
|
2017-04-18 17:14:27 +04:00
|
|
|
|
#![feature(abi_x86_interrupt)]
|
2018-04-05 18:36:39 +04:00
|
|
|
|
#![feature(iterator_step_by)]
|
2018-04-17 11:32:53 +04:00
|
|
|
|
#![feature(unboxed_closures)]
|
2018-04-25 21:00:32 +04:00
|
|
|
|
#![feature(naked_functions)]
|
2018-04-18 17:53:39 +04:00
|
|
|
|
#![feature(asm)]
|
2018-06-04 21:32:21 +04:00
|
|
|
|
#![feature(optin_builtin_traits)]
|
2018-06-19 19:43:40 +04:00
|
|
|
|
#![feature(panic_implementation)]
|
|
|
|
|
#![feature(panic_info_message)]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#![feature(universal_impl_trait)]
|
2017-04-11 17:02:21 +04:00
|
|
|
|
#![no_std]
|
|
|
|
|
|
2017-11-19 15:41:20 +04:00
|
|
|
|
|
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2017-11-19 15:41:20 +04:00
|
|
|
|
extern crate alloc;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate bit_allocator;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate bit_field;
|
2017-04-13 20:27:39 +04:00
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2017-04-13 20:27:39 +04:00
|
|
|
|
extern crate bitflags;
|
2018-04-18 17:53:39 +04:00
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2017-04-18 17:15:44 +04:00
|
|
|
|
extern crate lazy_static;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate linked_list_allocator;
|
2018-05-19 14:42:08 +04:00
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-19 14:42:08 +04:00
|
|
|
|
extern crate log;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate multiboot2;
|
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate once;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate rlibc;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-18 07:49:27 +04:00
|
|
|
|
extern crate simple_filesystem;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate spin;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate syscall as redox_syscall;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-19 20:24:07 +04:00
|
|
|
|
extern crate uart_16550;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-23 15:11:41 +04:00
|
|
|
|
extern crate ucore_memory;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate volatile;
|
|
|
|
|
#[macro_use]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate x86_64;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
extern crate xmas_elf;
|
|
|
|
|
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
pub use arch::interrupt::{rust_trap, set_return_rsp};
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-06-04 20:34:34 +04:00
|
|
|
|
use linked_list_allocator::LockedHeap;
|
2018-04-04 16:56:56 +04:00
|
|
|
|
|
2018-04-04 20:58:23 +04:00
|
|
|
|
#[macro_use] // print!
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-04 20:58:23 +04:00
|
|
|
|
mod io;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2017-04-13 19:59:12 +04:00
|
|
|
|
mod memory;
|
2018-04-04 16:56:56 +04:00
|
|
|
|
mod lang;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-05 18:36:39 +04:00
|
|
|
|
mod util;
|
2018-04-26 20:03:24 +04:00
|
|
|
|
#[macro_use]
|
|
|
|
|
mod macros;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-12 16:57:56 +04:00
|
|
|
|
mod consts;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-26 17:53:20 +04:00
|
|
|
|
mod process;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-12 23:41:41 +04:00
|
|
|
|
mod syscall;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-18 07:49:27 +04:00
|
|
|
|
mod fs;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-31 16:26:25 +04:00
|
|
|
|
mod thread;
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-05-31 16:23:26 +04:00
|
|
|
|
mod sync;
|
2017-04-12 21:16:04 +04:00
|
|
|
|
|
2018-04-04 16:56:56 +04:00
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
|
#[path = "arch/x86_64/mod.rs"]
|
|
|
|
|
mod arch;
|
|
|
|
|
|
2018-05-13 11:06:44 +04:00
|
|
|
|
/// The entry point of Rust kernel
|
2017-04-11 17:02:21 +04:00
|
|
|
|
#[no_mangle]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-18 10:26:34 +04:00
|
|
|
|
pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! {
|
2018-05-22 19:48:39 +04:00
|
|
|
|
arch::idt::init();
|
2018-05-19 14:42:08 +04:00
|
|
|
|
io::init();
|
2018-04-18 10:26:34 +04:00
|
|
|
|
|
2017-11-19 16:16:33 +04:00
|
|
|
|
// ATTENTION: we have a very small stack and no guard page
|
2017-04-12 21:35:08 +04:00
|
|
|
|
println!("Hello World{}", "!");
|
2017-04-11 21:30:19 +04:00
|
|
|
|
|
2018-04-04 16:56:56 +04:00
|
|
|
|
let boot_info = unsafe { multiboot2::load(multiboot_information_address) };
|
2018-06-20 07:08:47 +04:00
|
|
|
|
let rsdt_addr = boot_info.rsdp_v1_tag().unwrap().rsdt_address();
|
2017-04-13 19:57:33 +04:00
|
|
|
|
|
2017-11-19 16:16:33 +04:00
|
|
|
|
// set up guard page and map the heap pages
|
2018-06-02 21:10:20 +04:00
|
|
|
|
let mut kernel_memory = memory::init(boot_info);
|
2017-11-19 17:16:33 +04:00
|
|
|
|
|
2018-04-18 10:26:34 +04:00
|
|
|
|
arch::gdt::init();
|
2017-04-18 17:17:43 +04:00
|
|
|
|
|
2018-06-23 18:30:57 +04:00
|
|
|
|
memory::test::cow();
|
2018-04-09 17:20:47 +04:00
|
|
|
|
test!(global_allocator);
|
|
|
|
|
test!(guard_page);
|
2018-04-12 19:46:44 +04:00
|
|
|
|
test!(find_mp);
|
|
|
|
|
|
2018-06-20 07:08:47 +04:00
|
|
|
|
let acpi = arch::driver::init(rsdt_addr, |addr: usize, count: usize| {
|
2018-06-23 18:30:57 +04:00
|
|
|
|
use memory::*;
|
2018-06-02 21:10:20 +04:00
|
|
|
|
kernel_memory.push(MemoryArea::new_identity(addr, addr + count * 0x1000, MemoryAttr::default(), "acpi"))
|
|
|
|
|
});
|
2018-04-17 11:32:53 +04:00
|
|
|
|
|
2018-06-16 21:41:43 +04:00
|
|
|
|
arch::smp::start_other_cores(&acpi, &mut kernel_memory);
|
2018-06-02 21:10:20 +04:00
|
|
|
|
process::init(kernel_memory);
|
2018-04-26 17:53:20 +04:00
|
|
|
|
|
2018-05-18 07:49:27 +04:00
|
|
|
|
fs::load_sfs();
|
|
|
|
|
|
2018-04-27 17:49:01 +04:00
|
|
|
|
unsafe{ arch::interrupt::enable(); }
|
2018-04-18 17:53:39 +04:00
|
|
|
|
|
2018-06-01 07:47:58 +04:00
|
|
|
|
// thread::test::unpack();
|
2018-06-04 21:32:21 +04:00
|
|
|
|
// sync::test::philosopher_using_mutex();
|
|
|
|
|
// sync::test::philosopher_using_monitor();
|
|
|
|
|
sync::mpsc::test::test_all();
|
2018-06-01 07:47:58 +04:00
|
|
|
|
|
2018-05-12 20:47:16 +04:00
|
|
|
|
// 直接进入用户态暂不可用:内核代码用户不可访问
|
|
|
|
|
// unsafe{
|
|
|
|
|
// use arch::syscall;
|
|
|
|
|
// // 在用户模式下触发时钟中断,会导致GPF
|
|
|
|
|
// // (可能是由于没有合理分离栈)
|
|
|
|
|
// no_interrupt!({
|
|
|
|
|
// syscall::switch_to_user();
|
|
|
|
|
// println!("Now in user mode");
|
|
|
|
|
// syscall::switch_to_kernel();
|
|
|
|
|
// println!("Now in kernel mode");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2018-04-18 17:53:39 +04:00
|
|
|
|
|
2018-06-03 15:29:11 +04:00
|
|
|
|
loop {}
|
2018-04-15 21:04:23 +04:00
|
|
|
|
|
2018-04-09 17:20:47 +04:00
|
|
|
|
test_end!();
|
2018-04-18 10:26:34 +04:00
|
|
|
|
unreachable!();
|
2017-04-11 20:25:51 +04:00
|
|
|
|
}
|
2017-04-11 17:02:21 +04:00
|
|
|
|
|
2018-05-13 11:06:44 +04:00
|
|
|
|
/// The entry point for another processors
|
2018-04-18 08:26:21 +04:00
|
|
|
|
#[no_mangle]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-18 10:26:34 +04:00
|
|
|
|
pub extern "C" fn other_main() -> ! {
|
|
|
|
|
arch::gdt::init();
|
|
|
|
|
arch::idt::init();
|
2018-04-18 10:55:09 +04:00
|
|
|
|
arch::driver::apic::other_init();
|
2018-04-18 11:22:06 +04:00
|
|
|
|
let cpu_id = arch::driver::apic::lapic_id();
|
2018-06-02 21:10:20 +04:00
|
|
|
|
let ms = unsafe { arch::smp::notify_started(cpu_id) };
|
2018-06-03 07:22:28 +04:00
|
|
|
|
ms.switch();
|
|
|
|
|
println!("Hello world! from CPU {}!", arch::driver::apic::lapic_id());
|
2018-04-26 17:53:20 +04:00
|
|
|
|
// unsafe{ let a = *(0xdeadbeaf as *const u8); } // Page fault
|
2018-04-18 10:26:34 +04:00
|
|
|
|
loop {}
|
2018-04-18 08:26:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 11:06:44 +04:00
|
|
|
|
/// Global heap allocator
|
|
|
|
|
///
|
|
|
|
|
/// Available after `memory::init()`.
|
|
|
|
|
///
|
|
|
|
|
/// It should be defined in memory mod, but in Rust `global_allocator` must be in root mod.
|
2017-11-19 16:13:18 +04:00
|
|
|
|
#[global_allocator]
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2017-11-19 17:16:33 +04:00
|
|
|
|
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
2018-04-09 17:20:47 +04:00
|
|
|
|
|
2018-07-03 18:27:55 +04:00
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-04-09 17:20:47 +04:00
|
|
|
|
mod test {
|
|
|
|
|
pub fn global_allocator() {
|
|
|
|
|
for i in 0..10000 {
|
|
|
|
|
format!("Some String");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn find_mp() {
|
|
|
|
|
use arch;
|
|
|
|
|
let mp = arch::driver::mp::find_mp();
|
|
|
|
|
assert!(mp.is_some());
|
|
|
|
|
}
|
|
|
|
|
pub fn guard_page() {
|
|
|
|
|
use x86_64;
|
|
|
|
|
// invoke a breakpoint exception
|
2018-06-16 21:22:43 +04:00
|
|
|
|
unsafe { asm!("int 3"::::"intel" "volatile"); }
|
2018-04-09 17:20:47 +04:00
|
|
|
|
|
|
|
|
|
fn stack_overflow() {
|
|
|
|
|
stack_overflow(); // for each recursion, the return address is pushed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trigger a stack overflow
|
|
|
|
|
stack_overflow();
|
|
|
|
|
|
|
|
|
|
println!("It did not crash!");
|
|
|
|
|
}
|
|
|
|
|
}
|