This commit is contained in:
parent
91e15d0746
commit
2b147549ee
9 changed files with 28 additions and 27 deletions
12
kernel/src/cpu/instructions.rs
Normal file
12
kernel/src/cpu/instructions.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use core::arch::asm;
|
||||||
|
|
||||||
|
pub fn enable_user_memory_access() {
|
||||||
|
unsafe {
|
||||||
|
asm!("stac");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn disable_user_memory_access() {
|
||||||
|
unsafe {
|
||||||
|
asm!("clac");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod cpu;
|
|
||||||
pub mod gdt;
|
pub mod gdt;
|
||||||
pub mod idt;
|
pub mod idt;
|
||||||
|
pub mod instructions;
|
||||||
pub mod isr;
|
pub mod isr;
|
||||||
pub mod paging;
|
pub mod paging;
|
||||||
|
pub mod startup;
|
||||||
|
|
|
@ -250,10 +250,10 @@ impl AddressSpace {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_page_table() -> Box<PageTable> {
|
pub fn create_page_table() -> Box<PageTable> {
|
||||||
return Box::new(PageTable {
|
Box::new(PageTable {
|
||||||
entries_phys: [PageEntry(0); 512],
|
entries_phys: [PageEntry(0); 512],
|
||||||
entries_virt: [const { None }; 512],
|
entries_virt: [const { None }; 512],
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
fn get_free_frame() -> u64 {
|
fn get_free_frame() -> u64 {
|
||||||
let frames_vec = PHYSICAL_FRAMES.lock();
|
let frames_vec = PHYSICAL_FRAMES.lock();
|
||||||
|
@ -321,9 +321,9 @@ pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u
|
||||||
address_space.pml4.set(pml4).unwrap_or_else(|_| panic!());
|
address_space.pml4.set(pml4).unwrap_or_else(|_| panic!());
|
||||||
for i in 256..512 {
|
for i in 256..512 {
|
||||||
if i == 511 {
|
if i == 511 {
|
||||||
address_space.create_table_entry(i as i32, -1, -1, false, true, false);
|
address_space.create_table_entry(i, -1, -1, false, true, false);
|
||||||
} else {
|
} else {
|
||||||
address_space.create_table_entry(i as i32, -1, -1, false, false, false);
|
address_space.create_table_entry(i, -1, -1, false, false, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i in text_start..text_end {
|
for i in text_start..text_end {
|
||||||
|
|
|
@ -50,7 +50,7 @@ pub fn set_cpu_flags() {
|
||||||
// System Call Extensions
|
// System Call Extensions
|
||||||
asm!("rdmsr; bts rax, 0; wrmsr", in("rcx") 0xc0000080_u64, out("rax") _, out("rdx") _);
|
asm!("rdmsr; bts rax, 0; wrmsr", in("rcx") 0xc0000080_u64, out("rax") _, out("rdx") _);
|
||||||
asm!("wrmsr", in("rcx") 0xc0000081_u64, in("rax") 0, in("rdx") 8 | 16 << 16);
|
asm!("wrmsr", in("rcx") 0xc0000081_u64, in("rax") 0, in("rdx") 8 | 16 << 16);
|
||||||
asm!("wrmsr", in("rcx") 0xc0000082_u64, in("rax") syscall, in("rdx") syscall as u64 >> 32);
|
asm!("wrmsr", in("rcx") 0xc0000082_u64, in("rax") syscall, in("rdx") syscall as usize as u64 >> 32);
|
||||||
// Clear IF and DF
|
// Clear IF and DF
|
||||||
asm!("wrmsr", in("rcx") 0xc0000084_u64, in("rax") 1 << 9 | 1 << 10, in("rdx") 0);
|
asm!("wrmsr", in("rcx") 0xc0000084_u64, in("rax") 1 << 9 | 1 << 10, in("rdx") 0);
|
||||||
}
|
}
|
||||||
|
@ -59,13 +59,3 @@ pub fn set_cpu_flags() {
|
||||||
asm!("wrmsr", in("rcx") 0xc0000102_u64, in("rax") cpudata_address, in("rdx") cpudata_address >> 32);
|
asm!("wrmsr", in("rcx") 0xc0000102_u64, in("rax") cpudata_address, in("rdx") cpudata_address >> 32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn enable_user_memory_access() {
|
|
||||||
unsafe {
|
|
||||||
asm!("stac");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn disable_user_memory_access() {
|
|
||||||
unsafe {
|
|
||||||
asm!("clac");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -14,7 +14,7 @@ use core::{
|
||||||
use acpi::AcpiTables;
|
use acpi::AcpiTables;
|
||||||
use acpica_rs::{AcpiEnableSubsystem, AcpiInitializeObjects, AcpiInitializeSubsystem, AcpiInitializeTables, AcpiLoadTables, ACPI_FULL_INITIALIZATION};
|
use acpica_rs::{AcpiEnableSubsystem, AcpiInitializeObjects, AcpiInitializeSubsystem, AcpiInitializeTables, AcpiLoadTables, ACPI_FULL_INITIALIZATION};
|
||||||
use alloc::format;
|
use alloc::format;
|
||||||
use cpu::{cpu::set_cpu_flags, gdt::setup_gdt, idt::setup_idt, paging::setup_paging};
|
use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging, startup::set_cpu_flags};
|
||||||
use kernel_common::{
|
use kernel_common::{
|
||||||
instructions::{cli, hlt},
|
instructions::{cli, hlt},
|
||||||
loader_struct::{FramebufferInfo, LoaderStruct, LoaderStructMemoryRegion, LOADER_STRUCT_MAGIC},
|
loader_struct::{FramebufferInfo, LoaderStruct, LoaderStructMemoryRegion, LOADER_STRUCT_MAGIC},
|
||||||
|
|
|
@ -5,7 +5,7 @@ use log::info;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cpu::{
|
cpu::{
|
||||||
cpu::{disable_user_memory_access, enable_user_memory_access},
|
instructions::{disable_user_memory_access, enable_user_memory_access},
|
||||||
paging::USER_END,
|
paging::USER_END,
|
||||||
},
|
},
|
||||||
sys::{lapic::get_current_lapic_id, task::CURRENT_TASKS},
|
sys::{lapic::get_current_lapic_id, task::CURRENT_TASKS},
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use core::{ptr::copy, sync::atomic::Ordering};
|
use core::{ptr::copy, sync::atomic::Ordering};
|
||||||
|
|
||||||
use alloc::{vec, vec::Vec};
|
|
||||||
use kernel_common::{instructions::pause, paging::PageTable};
|
use kernel_common::{instructions::pause, paging::PageTable};
|
||||||
|
|
||||||
use crate::{cpu::isr::ISR_INVALIDATE_TLB, BROADCASTED_PANIC};
|
use crate::{cpu::isr::ISR_INVALIDATE_TLB, BROADCASTED_PANIC};
|
||||||
|
@ -10,7 +9,7 @@ use super::{
|
||||||
lapic::{get_current_lapic_id, send_ipi, BSP_LAPIC_ID, LAPICS, NEXT_LAPIC_ID},
|
lapic::{get_current_lapic_id, send_ipi, BSP_LAPIC_ID, LAPICS, NEXT_LAPIC_ID},
|
||||||
process::get_kernel_process,
|
process::get_kernel_process,
|
||||||
sync::RawSpinlock,
|
sync::RawSpinlock,
|
||||||
task::{ALL_APS_STARTED, STACK_SIZE, STARTING_AP_ID},
|
task::{allocate_stack, ALL_APS_STARTED, STARTING_AP_ID},
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -26,7 +25,7 @@ const IPI_STARTUP: u32 = 0x600;
|
||||||
static INVALIDATE_TLB_LOCK: RawSpinlock = RawSpinlock::new();
|
static INVALIDATE_TLB_LOCK: RawSpinlock = RawSpinlock::new();
|
||||||
|
|
||||||
pub fn start_aps() {
|
pub fn start_aps() {
|
||||||
let stack: Vec<u8> = vec![0; STACK_SIZE];
|
let stack = allocate_stack();
|
||||||
let pml4_phys_addr;
|
let pml4_phys_addr;
|
||||||
{
|
{
|
||||||
let kernel_proc = get_kernel_process();
|
let kernel_proc = get_kernel_process();
|
||||||
|
@ -45,7 +44,7 @@ pub fn start_aps() {
|
||||||
*pml4_addr = pml4_phys_addr;
|
*pml4_addr = pml4_phys_addr;
|
||||||
let stack_offset = (&raw const trampoline_stack).offset_from(src_ptr);
|
let stack_offset = (&raw const trampoline_stack).offset_from(src_ptr);
|
||||||
let stack_addr = (0x1000 + stack_offset) as *mut u64;
|
let stack_addr = (0x1000 + stack_offset) as *mut u64;
|
||||||
*stack_addr = stack.as_ptr() as u64 + STACK_SIZE as u64;
|
*stack_addr = stack.address;
|
||||||
address_space.map_range(0x1000, 0x1000, 0x1000, false, true, true, false);
|
address_space.map_range(0x1000, 0x1000, 0x1000, false, true, true, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use core::{error::Error, ptr::copy};
|
use core::{error::Error, ptr::copy};
|
||||||
|
|
||||||
use alloc::{boxed::Box, string::String, vec::Vec};
|
use alloc::{boxed::Box, string::String, vec, vec::Vec};
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
|
|
||||||
use crate::cpu::{
|
use crate::cpu::{
|
||||||
cpu::{disable_user_memory_access, enable_user_memory_access},
|
instructions::{disable_user_memory_access, enable_user_memory_access},
|
||||||
paging::USER_END,
|
paging::USER_END,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -43,8 +43,7 @@ fn copy_from_user(start: u64, size: usize) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||||
return Err("Invalid copy requested".into());
|
return Err("Invalid copy requested".into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut buffer = Vec::with_capacity(size);
|
let mut buffer: Vec<u8> = vec![0; size];
|
||||||
buffer.resize(size, 0);
|
|
||||||
enable_user_memory_access();
|
enable_user_memory_access();
|
||||||
unsafe {
|
unsafe {
|
||||||
copy(start as *const u8, buffer.as_mut_ptr(), size);
|
copy(start as *const u8, buffer.as_mut_ptr(), size);
|
||||||
|
|
|
@ -59,7 +59,7 @@ pub struct CPUData {
|
||||||
pub user_stack: u64,
|
pub user_stack: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STACK_SIZE: usize = 64 * 1024;
|
const STACK_SIZE: usize = 64 * 1024;
|
||||||
static NEXT_TASK_ID: AtomicUsize = AtomicUsize::new(2);
|
static NEXT_TASK_ID: AtomicUsize = AtomicUsize::new(2);
|
||||||
pub static CURRENT_TASKS: Spinlock<[Option<Task>; 256]> = Spinlock::new([const { None }; 256]);
|
pub static CURRENT_TASKS: Spinlock<[Option<Task>; 256]> = Spinlock::new([const { None }; 256]);
|
||||||
static RFLAGS: AtomicU64 = AtomicU64::new(0);
|
static RFLAGS: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue