2016-08-14 19:45:47 +02:00
|
|
|
//! Architecture support for x86_64
|
|
|
|
|
|
|
|
#![feature(asm)]
|
2016-08-14 20:08:42 +02:00
|
|
|
#![feature(concat_idents)]
|
2016-08-14 19:45:47 +02:00
|
|
|
#![feature(const_fn)]
|
|
|
|
#![feature(core_intrinsics)]
|
2016-09-01 19:51:33 +02:00
|
|
|
#![feature(drop_types_in_const)]
|
2016-08-15 22:34:20 +02:00
|
|
|
#![feature(lang_items)]
|
2016-08-14 19:45:47 +02:00
|
|
|
#![feature(naked_functions)]
|
2016-08-19 22:53:16 +02:00
|
|
|
#![feature(thread_local)]
|
2016-08-15 19:29:53 +02:00
|
|
|
#![feature(unique)]
|
2016-08-14 19:45:47 +02:00
|
|
|
#![no_std]
|
|
|
|
|
2016-08-15 23:27:32 +02:00
|
|
|
extern crate hole_list_allocator as allocator;
|
2016-09-01 22:39:45 +02:00
|
|
|
|
2016-08-14 19:45:47 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
2016-09-01 19:51:33 +02:00
|
|
|
extern crate ransid;
|
2016-08-16 00:29:54 +02:00
|
|
|
extern crate spin;
|
2016-09-08 05:16:30 +02:00
|
|
|
pub extern crate x86;
|
2016-08-14 19:45:47 +02:00
|
|
|
|
2016-09-11 02:48:27 +02:00
|
|
|
// Because the memory map is so important to not be aliased, it is defined here, in one place
|
2016-09-12 21:06:00 +02:00
|
|
|
// The lower 256 PML4 entries are reserved for userspace
|
|
|
|
// Each PML4 entry references up to 512 GB of memory
|
|
|
|
// The upper 256 are reserved for the kernel
|
|
|
|
/// The size of a single PML4
|
|
|
|
pub const PML4_SIZE: usize = 0x0000_0080_0000_0000;
|
|
|
|
|
|
|
|
/// Offset of recursive paging
|
|
|
|
pub const RECURSIVE_PAGE_OFFSET: usize = (-(PML4_SIZE as isize)) as usize;
|
|
|
|
|
|
|
|
/// Offset of kernel
|
|
|
|
pub const KERNEL_OFFSET: usize = RECURSIVE_PAGE_OFFSET - PML4_SIZE;
|
|
|
|
|
|
|
|
/// Offset to kernel heap
|
|
|
|
pub const KERNEL_HEAP_OFFSET: usize = KERNEL_OFFSET - PML4_SIZE;
|
|
|
|
/// Size of kernel heap
|
|
|
|
pub const KERNEL_HEAP_SIZE: usize = 64 * 1024 * 1024; // 64 MB
|
|
|
|
|
|
|
|
/// Offset to kernel percpu variables
|
|
|
|
pub const KERNEL_PERCPU_OFFSET: usize = KERNEL_HEAP_OFFSET - PML4_SIZE;
|
|
|
|
|
|
|
|
/// Offset to user heap
|
|
|
|
pub const USER_HEAP_OFFSET: usize = PML4_SIZE;
|
|
|
|
|
2016-09-11 02:48:27 +02:00
|
|
|
|
2016-08-14 19:45:47 +02:00
|
|
|
/// Print to console
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! print {
|
|
|
|
($($arg:tt)*) => ({
|
2016-08-18 01:24:10 +02:00
|
|
|
use core::fmt::Write;
|
2016-09-02 01:08:43 +02:00
|
|
|
let mut console = $crate::console::CONSOLE.lock();
|
|
|
|
let _ = write!(console, $($arg)*);
|
2016-08-14 19:45:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print with new line to console
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! println {
|
|
|
|
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
|
|
|
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
|
|
|
}
|
|
|
|
|
2016-08-14 20:08:42 +02:00
|
|
|
/// Create an interrupt function that can safely run rust code
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! interrupt {
|
|
|
|
($name:ident, $func:block) => {
|
|
|
|
#[naked]
|
|
|
|
pub unsafe extern fn $name () {
|
2016-08-17 02:04:15 +02:00
|
|
|
#[inline(never)]
|
2016-08-14 20:08:42 +02:00
|
|
|
unsafe fn inner() {
|
|
|
|
$func
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push scratch registers
|
2016-09-08 23:53:45 +02:00
|
|
|
asm!("push rax
|
2016-08-14 20:08:42 +02:00
|
|
|
push rcx
|
|
|
|
push rdx
|
|
|
|
push rdi
|
|
|
|
push rsi
|
|
|
|
push r8
|
|
|
|
push r9
|
|
|
|
push r10
|
2016-09-08 23:45:26 +02:00
|
|
|
push r11
|
|
|
|
push fs
|
|
|
|
mov rax, 0x18
|
|
|
|
mov fs, ax"
|
2016-08-14 20:08:42 +02:00
|
|
|
: : : : "intel", "volatile");
|
|
|
|
|
|
|
|
// Call inner rust function
|
|
|
|
inner();
|
|
|
|
|
|
|
|
// Pop scratch registers and return
|
2016-09-08 23:53:45 +02:00
|
|
|
asm!("pop fs
|
2016-09-08 23:45:26 +02:00
|
|
|
pop r11
|
2016-08-14 20:08:42 +02:00
|
|
|
pop r10
|
|
|
|
pop r9
|
|
|
|
pop r8
|
|
|
|
pop rsi
|
|
|
|
pop rdi
|
|
|
|
pop rdx
|
|
|
|
pop rcx
|
|
|
|
pop rax
|
|
|
|
iretq"
|
|
|
|
: : : : "intel", "volatile");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:04:15 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! interrupt_error {
|
|
|
|
($name:ident, $func:block) => {
|
|
|
|
#[naked]
|
|
|
|
pub unsafe extern fn $name () {
|
|
|
|
#[inline(never)]
|
|
|
|
unsafe fn inner() {
|
|
|
|
$func
|
|
|
|
}
|
|
|
|
|
2016-08-18 01:40:18 +02:00
|
|
|
// Push scratch registers
|
2016-09-08 23:53:45 +02:00
|
|
|
asm!("push rax
|
2016-08-17 02:04:15 +02:00
|
|
|
push rcx
|
|
|
|
push rdx
|
|
|
|
push rdi
|
|
|
|
push rsi
|
|
|
|
push r8
|
|
|
|
push r9
|
|
|
|
push r10
|
2016-09-08 23:45:26 +02:00
|
|
|
push r11
|
|
|
|
push fs
|
|
|
|
mov rax, 0x18
|
|
|
|
mov fs, ax"
|
2016-08-17 02:04:15 +02:00
|
|
|
: : : : "intel", "volatile");
|
|
|
|
|
|
|
|
// Call inner rust function
|
|
|
|
inner();
|
|
|
|
|
|
|
|
// Pop scratch registers, error code, and return
|
2016-09-08 23:53:45 +02:00
|
|
|
asm!("pop fs
|
2016-09-08 23:45:26 +02:00
|
|
|
pop r11
|
2016-08-17 02:04:15 +02:00
|
|
|
pop r10
|
|
|
|
pop r9
|
|
|
|
pop r8
|
|
|
|
pop rsi
|
|
|
|
pop rdi
|
|
|
|
pop rdx
|
|
|
|
pop rcx
|
|
|
|
pop rax
|
|
|
|
add rsp, 8
|
|
|
|
iretq"
|
|
|
|
: : : : "intel", "volatile");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-16 02:37:58 +02:00
|
|
|
/// ACPI table parsing
|
|
|
|
pub mod acpi;
|
|
|
|
|
2016-09-01 19:51:33 +02:00
|
|
|
/// Console handling
|
|
|
|
pub mod console;
|
|
|
|
|
2016-08-23 03:56:35 +02:00
|
|
|
/// Context switching
|
|
|
|
pub mod context;
|
|
|
|
|
2016-09-01 01:45:21 +02:00
|
|
|
/// Devices
|
|
|
|
pub mod device;
|
2016-08-25 04:10:55 +02:00
|
|
|
|
2016-08-15 19:29:53 +02:00
|
|
|
/// Memcpy, memmove, etc.
|
|
|
|
pub mod externs;
|
|
|
|
|
2016-08-14 19:45:47 +02:00
|
|
|
/// Global descriptor table
|
|
|
|
pub mod gdt;
|
|
|
|
|
|
|
|
/// Interrupt descriptor table
|
|
|
|
pub mod idt;
|
|
|
|
|
|
|
|
/// IO Handling
|
|
|
|
pub mod io;
|
|
|
|
|
|
|
|
/// Interrupt instructions
|
|
|
|
pub mod interrupt;
|
|
|
|
|
2016-08-15 19:29:53 +02:00
|
|
|
/// Memory management
|
|
|
|
pub mod memory;
|
|
|
|
|
|
|
|
/// Paging
|
|
|
|
pub mod paging;
|
2016-08-14 19:45:47 +02:00
|
|
|
|
2016-08-15 22:34:20 +02:00
|
|
|
/// Panic
|
|
|
|
pub mod panic;
|
|
|
|
|
2016-08-15 23:32:33 +02:00
|
|
|
/// Initialization and start function
|
|
|
|
pub mod start;
|