diff --git a/.cargo/config.toml b/.cargo/config.toml index 422bf9d..058189f 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,4 @@ [unstable] build-std = ["core", "compiler_builtins", "alloc"] build-std-features = ["compiler-builtins-mem"] +bindeps = true diff --git a/Cargo.lock b/Cargo.lock index 7b5e62d..edc8840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "atest" @@ -30,6 +30,9 @@ version = "0.4.0" [[package]] name = "felix-bootloader" version = "0.4.0" +dependencies = [ + "libfelix", +] [[package]] name = "felix-kernel" diff --git a/Cargo.toml b/Cargo.toml index 7772359..45e36a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ resolver = "2" [workspace.package] version = "0.4.0" authors = ["Gianmatteo Palmieri "] -edition = "2021" +edition = "2024" [profile.dev] panic = "abort" diff --git a/boot/build.rs b/boot/build.rs index 66b7cf4..9e81965 100644 --- a/boot/build.rs +++ b/boot/build.rs @@ -7,4 +7,4 @@ fn main() { "cargo:rustc-link-arg-bins=--script={}", local_path.join("linker.ld").display() ); -} +} \ No newline at end of file diff --git a/boot/src/main.rs b/boot/src/main.rs index 585c2ad..f0acfaa 100644 --- a/boot/src/main.rs +++ b/boot/src/main.rs @@ -14,11 +14,11 @@ const BOOTLOADER_SIZE: u16 = 64; //bootloader size in sectors //set data segments to zero and setup stack global_asm!(include_str!("boot.asm")); -extern "C" { +unsafe extern "C" { static _bootloader_start: u16; } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn main() -> ! { clear(); @@ -56,7 +56,7 @@ fn print(message: &str) { "2:", "lodsb", //load a byte (next character) from si to al "or al, al", //bitwise or on al, if al is null set zf to true - "jz 1f", //if zf is true (end of string) jump to end + "jz 3f", //if zf is true (end of string) jump to end "mov ah, 0x0e", "mov bh, 0", @@ -64,7 +64,7 @@ fn print(message: &str) { "int 0x10", //tell the bios to write content of al to screen "jmp 2b", //start again - "1:", + "3:", in(reg) message.as_ptr()); } } @@ -76,7 +76,7 @@ fn jump(address: *const u16) { } } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn fail() -> ! { print("[!] Failed loading bootloader!"); diff --git a/bootloader/Cargo.toml b/bootloader/Cargo.toml index 0e20598..b1d119a 100644 --- a/bootloader/Cargo.toml +++ b/bootloader/Cargo.toml @@ -5,3 +5,6 @@ authors.workspace = true edition.workspace = true [dependencies] + +[dependencies.libfelix] +path = "../lib" diff --git a/bootloader/src/disk.rs b/bootloader/src/disk.rs index 02a7364..9618a7e 100644 --- a/bootloader/src/disk.rs +++ b/bootloader/src/disk.rs @@ -4,10 +4,9 @@ use core::arch::asm; use core::mem; +use libfelix::mutex::Mutex; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut DISK: Disk = Disk { lba: 0, buffer: 0 }; +pub static mut DISK: Mutex = Mutex::new(Disk { lba: 0, buffer: 0 }); const SECTOR_SIZE: u64 = 512; diff --git a/bootloader/src/main.rs b/bootloader/src/main.rs index ff39b3a..7e0c7bd 100644 --- a/bootloader/src/main.rs +++ b/bootloader/src/main.rs @@ -29,8 +29,8 @@ fn panic(info: &PanicInfo) -> ! { } //bootloader entry point -#[no_mangle] -#[link_section = ".start"] +#[unsafe(no_mangle)] +#[unsafe(link_section = ".start")] pub extern "C" fn _start() -> ! { //uncomment to enable splashscreen //clear!(); @@ -46,10 +46,14 @@ pub extern "C" fn _start() -> ! { print!("[!] Loading kernel"); unsafe { - DISK.init(KERNEL_LBA, KERNEL_BUFFER); - DISK.read_sectors(KERNEL_SIZE, KERNEL_TARGET); + + (*(&raw mut DISK)).acquire_mut().init(KERNEL_LBA, KERNEL_BUFFER); + (*(&raw mut DISK)).free(); + + (*(&raw mut DISK)).acquire_mut().read_sectors(KERNEL_SIZE, KERNEL_TARGET); + (*(&raw mut DISK)).free(); } - + println!("[!] Kernel loaded to memory."); //load dgt @@ -65,7 +69,7 @@ pub extern "C" fn _start() -> ! { loop {} } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn fail() -> ! { println!("[!] Read fail!"); diff --git a/bootloader/src/print.rs b/bootloader/src/print.rs index d2a070a..8bb214a 100644 --- a/bootloader/src/print.rs +++ b/bootloader/src/print.rs @@ -4,10 +4,9 @@ use core::arch::asm; use core::fmt; +use libfelix::mutex::Mutex; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut PRINTER: Printer = Printer {}; +pub static mut PRINTER: Mutex = Mutex::new(Printer {}); pub struct Printer {} @@ -80,14 +79,18 @@ macro_rules! println { pub fn _print(args: fmt::Arguments) { use core::fmt::Write; unsafe { - PRINTER.write_fmt(args).unwrap(); + let printer = (*(&raw mut PRINTER)).acquire_mut(); + printer.write_fmt(args).unwrap(); + (*(&raw mut PRINTER)).free(); } } #[allow(dead_code)] pub fn _clear() { unsafe { - PRINTER.clear(); + let printer = (*(&raw mut PRINTER)).acquire_mut(); + printer.clear(); + (*(&raw mut PRINTER)).free(); } } diff --git a/kernel/src/drivers/disk.rs b/kernel/src/drivers/disk.rs index 6c967a2..ae38a76 100644 --- a/kernel/src/drivers/disk.rs +++ b/kernel/src/drivers/disk.rs @@ -2,10 +2,9 @@ //Driver for ATA disk supporting PIO MODE use core::arch::asm; +use libfelix::mutex::Mutex; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut DISK: Disk = Disk { enabled: false }; +pub static mut DISK: Mutex = Mutex::new(Disk { enabled: false }); //controller registers ports const DATA_REGISTER: u16 = 0x1f0; diff --git a/kernel/src/drivers/keyboard.rs b/kernel/src/drivers/keyboard.rs index e6ef571..53567c5 100644 --- a/kernel/src/drivers/keyboard.rs +++ b/kernel/src/drivers/keyboard.rs @@ -3,11 +3,10 @@ use crate::drivers::pic::PICS; use crate::shell::shell::SHELL; -use core::arch::asm; +use crate::libfelix::mutex::Mutex; +use core::arch::{asm, naked_asm}; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut KEYBOARD: Keyboard = Keyboard { lshift: false }; +pub static mut KEYBOARD: Mutex = Mutex::new(Keyboard { lshift: false }); pub const KEYBOARD_INT: u8 = 33; pub const KEYBAORD_CONTROLLER: u8 = 0x60; @@ -18,30 +17,27 @@ pub struct Keyboard { } //keyboard handler -#[naked] +#[unsafe(naked)] pub extern "C" fn keyboard() { - unsafe { - //push charset to keyboard handler before calling - asm!( - "push 0x6d6e6276", - "push 0x63787a6c", - "push 0x6b6a6867", - "push 0x66647361", - "push 0x706f6975", - "push 0x79747265", - "push 0x77713039", - "push 0x38373635", - "push 0x34333231", - "call keyboard_handler", - "add esp, 36", - "iretd", - options(noreturn) - ); - } + //push charset to keyboard handler before calling + naked_asm!( + "push 0x6d6e6276", + "push 0x63787a6c", + "push 0x6b6a6867", + "push 0x66647361", + "push 0x706f6975", + "push 0x79747265", + "push 0x77713039", + "push 0x38373635", + "push 0x34333231", + "call keyboard_handler", + "add esp, 36", + "iretd", + ); } #[allow(improper_ctypes_definitions)] -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn keyboard_handler(charset: [u8; CHAR_COUNT]) { //read scancode from keyboard controller let scancode: u8; @@ -51,30 +47,36 @@ pub extern "C" fn keyboard_handler(charset: [u8; CHAR_COUNT]) { //notify pics end of interrupt PICS.end_interrupt(KEYBOARD_INT); - + unsafe { + let keyboard = &mut (*(&raw mut KEYBOARD)); + let shell = &mut (*(&raw mut SHELL)); match scancode { //press left shift 0x2a => { - KEYBOARD.lshift = true; + keyboard.acquire_mut().lshift = true; + keyboard.free(); return; } //release left shift 0xaa => { - KEYBOARD.lshift = false; + keyboard.acquire_mut().lshift = false; + keyboard.free(); return; } //backspace 0x0e => { - SHELL.backspace(); + shell.acquire_mut().backspace(); + shell.free(); return; } //enter 0x1c => { - SHELL.enter(); + shell.acquire_mut().enter(); + shell.free(); return; } @@ -87,8 +89,10 @@ pub extern "C" fn keyboard_handler(charset: [u8; CHAR_COUNT]) { if key != '\0' { unsafe { - SHELL.add(key); - } + let shell = &mut (*(&raw mut SHELL)); + shell.acquire_mut().add(key); + shell.free(); + } } } @@ -110,9 +114,12 @@ fn scancode_to_char(scancode: u8, charset: [u8; CHAR_COUNT]) -> char { key = charset[index] as char; unsafe { - if KEYBOARD.lshift { + let keyboard = &mut (*(&raw mut KEYBOARD)); + //let keyboard = (*(&raw mut KEYBOARD)).acquire(); + if keyboard.acquire().lshift { key = key.to_ascii_uppercase(); } + keyboard.free(); } } diff --git a/kernel/src/filesystem/fat.rs b/kernel/src/filesystem/fat.rs index 98e15e1..f73f595 100755 --- a/kernel/src/filesystem/fat.rs +++ b/kernel/src/filesystem/fat.rs @@ -1,8 +1,9 @@ //FAT16 FILESYSTEM IMPLEMENTATION use crate::drivers::disk::DISK; +use crate::libfelix::mutex::Mutex; use core::mem; -use libfelix::mutex::Mutex; + pub static mut FAT: Mutex = Mutex::new(FatDriver { header: NULL_HEADER, @@ -124,7 +125,8 @@ impl FatDriver { let sectors: u16 = 1; unsafe { - DISK.read(target, lba, sectors); + (*(&raw mut DISK)).acquire_mut().read(target, lba, sectors); + (*(&raw mut DISK)).free(); } } @@ -144,7 +146,8 @@ impl FatDriver { let sectors: u16 = size / self.header.bytes_per_sector; unsafe { - DISK.read(target, lba, sectors); + (*(&raw mut DISK)).acquire_mut().read(target, lba, sectors); + (*(&raw mut DISK)).free(); } } @@ -183,7 +186,8 @@ impl FatDriver { let sectors: u16 = 1; unsafe { - DISK.read(target, lba, sectors); + (*(&raw mut DISK)).acquire_mut().read(target, lba, sectors); + (*(&raw mut DISK)).free(); } } @@ -201,7 +205,8 @@ impl FatDriver { let sectors: u16 = self.header.sectors_per_cluster as u16; unsafe { - DISK.read(target, lba, sectors); + (*(&raw mut DISK)).acquire().read(target, lba, sectors); + (*(&raw mut DISK)).free(); } } @@ -223,7 +228,8 @@ impl FatDriver { let sectors: u16 = self.header.sectors_per_cluster as u16; unsafe { - DISK.read(current_target, lba, sectors); + (*(&raw mut DISK)).acquire().read(current_target, lba, sectors); + (*(&raw mut DISK)).free(); } next_cluster = self.table[next_cluster as usize]; diff --git a/kernel/src/interrupts/exceptions.rs b/kernel/src/interrupts/exceptions.rs index fa09cf9..d3363cb 100644 --- a/kernel/src/interrupts/exceptions.rs +++ b/kernel/src/interrupts/exceptions.rs @@ -1,9 +1,8 @@ -use core::arch::asm; - +use core::arch::{naked_asm}; //CPU EXCEPTIONS HANDLERS //handle excpetion based on interrupt number -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn exception_handler(int: u32, eip: u32, cs: u32, eflags: u32) { match int { 0x00 => { @@ -33,80 +32,62 @@ pub extern "C" fn exception_handler(int: u32, eip: u32, cs: u32, eflags: u32) { loop {} } -#[naked] +#[unsafe(naked)] pub extern "C" fn div_error() { - unsafe { - asm!( - "push 0x00", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0x00", + "call exception_handler", + "add esp, 4", + "iretd", + ); } -#[naked] +#[unsafe(naked)] pub extern "C" fn invalid_opcode() { - unsafe { - asm!( - "push 0x06", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0x06", + "call exception_handler", + "add esp, 4", + "iretd", + ); } -#[naked] +#[unsafe(naked)] pub extern "C" fn double_fault() { - unsafe { - asm!( - "push 0x08", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0x08", + "call exception_handler", + "add esp, 4", + "iretd", + ); } -#[naked] +#[unsafe(naked)] pub extern "C" fn general_protection_fault() { - unsafe { - asm!( - "push 0x0d", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0x0d", + "call exception_handler", + "add esp, 4", + "iretd", + ); } -#[naked] +#[unsafe(naked)] pub extern "C" fn page_fault() { - unsafe { - asm!( - "push 0x0e", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0x0e", + "call exception_handler", + "add esp, 4", + "iretd", + ); } -#[naked] +#[unsafe(naked)] pub extern "C" fn generic_handler() { - unsafe { - asm!( - "push 0xff", - "call exception_handler", - "add esp, 4", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push 0xff", + "call exception_handler", + "add esp, 4", + "iretd", + ); } diff --git a/kernel/src/interrupts/idt.rs b/kernel/src/interrupts/idt.rs index 099a3d8..c579fb5 100644 --- a/kernel/src/interrupts/idt.rs +++ b/kernel/src/interrupts/idt.rs @@ -1,14 +1,13 @@ //INTERRUPT DESCRIPTOR TABLE use crate::interrupts::exceptions; +use crate::libfelix::mutex::Mutex; use core::arch::asm; use core::mem::size_of; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut IDT: InterruptDescriptorTable = InterruptDescriptorTable { +pub static mut IDT: Mutex = Mutex::new(InterruptDescriptorTable { entries: [IDT_ENTRY; IDT_ENTRIES], -}; +}); const IDT_ENTRIES: usize = 256; diff --git a/kernel/src/interrupts/timer.rs b/kernel/src/interrupts/timer.rs index 88d6243..e57e83e 100644 --- a/kernel/src/interrupts/timer.rs +++ b/kernel/src/interrupts/timer.rs @@ -4,7 +4,7 @@ use crate::drivers::pic::PICS; use crate::multitasking::task::CPUState; use crate::multitasking::task::TASK_MANAGER; -use core::arch::asm; +use core::arch::{naked_asm}; use crate::memory::paging::PAGING; use crate::memory::paging::TABLES; @@ -15,54 +15,51 @@ const APP_TARGET: u32 = 0x00a0_0000; const APP_SIZE: u32 = 0x0001_0000; //TIMER IRQ -#[naked] +#[unsafe(naked)] pub extern "C" fn timer() { - unsafe { - asm!( - //disable interrupts - "cli", - //save registers - "push ebp", - "push edi", - "push esi", - "push edx", - "push ecx", - "push ebx", - "push eax", - //call c function with esp as argument - "push esp", - "call timer_handler", - //set esp to return value of c func - "mov esp, eax", - //restore registers - "pop eax", - "pop ebx", - "pop ecx", - "pop edx", - "pop esi", - "pop edi", - "pop ebp", - //re-enable interrupts - "sti", - //return irq - "iretd", - options(noreturn) - ); - } + naked_asm!( + //disable interrupts + "cli", + //save registers + "push ebp", + "push edi", + "push esi", + "push edx", + "push ecx", + "push ebx", + "push eax", + //call c function with esp as argument + "push esp", + "call timer_handler", + //set esp to return value of c func + "mov esp, eax", + //restore registers + "pop eax", + "pop ebx", + "pop ecx", + "pop edx", + "pop esi", + "pop edi", + "pop ebp", + //re-enable interrupts + "sti", + //return irq + "iretd", + ); } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn timer_handler(esp: u32) -> u32 { //trigger scheduler and return the esp returned by scheduler unsafe { - let new_esp: u32 = TASK_MANAGER.schedule(esp as *mut CPUState) as u32; + let new_esp: u32 = (*(&raw mut TASK_MANAGER)).schedule(esp as *mut CPUState) as u32; - let slot = TASK_MANAGER.get_current_slot(); + let slot = (*(&raw mut TASK_MANAGER)).get_current_slot(); let target = APP_TARGET + (slot as u32 * APP_SIZE); //map table 8 (0x02000000) to the address where the executable is loaded TABLES[8].set(target); - PAGING.set_table(8, &TABLES[8]); + (*(&raw mut PAGING)).set_table(8, &TABLES[8]); PICS.end_interrupt(TIMER_INT); diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 5959cc7..743eac3 100755 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -1,7 +1,5 @@ #![no_std] #![no_main] -#![feature(naked_functions)] -#![feature(pointer_byte_offsets)] extern crate alloc; @@ -41,60 +39,67 @@ const STACK_START: u32 = KERNEL_START + KERNEL_SIZE + STACK_SIZE; const VERSION: &str = env!("CARGO_PKG_VERSION"); //KERNEL ENTRY POINT -#[no_mangle] -#[link_section = ".start"] +#[unsafe(no_mangle)] +#[unsafe(link_section = ".start")] pub extern "C" fn _start() -> ! { unsafe { //setup stack asm!("mov esp, {}", in(reg) STACK_START); //setup paging - PAGING.identity(); - PAGING.enable(); + (*(&raw mut PAGING)).identity(); + (*(&raw mut PAGING)).enable(); //bochs magic breakpoint asm!("xchg bx, bx"); //setup idt - IDT.init(); //init idt - IDT.add_exceptions(); //add CPU exceptions to idt - IDT.add( + let idt = (*(&raw mut IDT)).acquire_mut(); + idt.init(); //init idt + idt.add_exceptions(); //add CPU exceptions to idt + idt.add( interrupts::timer::TIMER_INT as usize, interrupts::timer::timer as u32, ); //add timer interrupt to idt - IDT.add( + idt.add( syscalls::handler::SYSCALL_INT as usize, syscalls::handler::syscall as u32, ); //add system call handler interrupt - IDT.add( + idt.add( drivers::keyboard::KEYBOARD_INT as usize, drivers::keyboard::keyboard as u32, ); //add keyboard interrupt to idt - IDT.load(); //load idt + idt.load(); //load idt + (*(&raw mut FAT)).free(); //init programmable interrupt controllers PICS.init(); //enable ata disk if present - DISK.check(); - + let disk = (*(&raw mut DISK)).acquire_mut(); + disk.check(); + (*(&raw mut DISK)).free(); + //init filesystem - if DISK.enabled { - let fat = FAT.acquire_mut(); + if disk.enabled { + //let mutable_reference = (*(&raw mut GLOBAL)).assume_init_mut(); + let fat = (*(&raw mut FAT)).acquire_mut(); fat.load_header(); fat.load_table(); fat.load_entries(); - FAT.free(); + (*(&raw mut FAT)).free(); } //print name, version and copyright print_info(); //init felix shell - SHELL.init(); + let shell_mutex = (*(&raw mut SHELL)).acquire_mut(); + shell_mutex.init(); + (*(&raw mut SHELL)).free(); //init multitasking - TASK_MANAGER.init(); + (*(&raw mut TASK_MANAGER)).init(); //bochs magic breakpoint asm!("xchg bx, bx"); @@ -115,7 +120,8 @@ fn panic(info: &PanicInfo) -> ! { fn print_info() { unsafe { - PRINTER.set_colors(0xf, 0); + (*(&raw mut PRINTER)).acquire_mut().set_colors(0xf, 0); + (*(&raw mut PRINTER)).free(); } libfelix::println!(); @@ -126,6 +132,7 @@ fn print_info() { libfelix::println!(); unsafe { - PRINTER.reset_colors(); + (*(&raw mut PRINTER)).acquire_mut().reset_colors(); + (*(&raw mut PRINTER)).free(); } } diff --git a/kernel/src/memory/paging.rs b/kernel/src/memory/paging.rs index e03afed..e665aa3 100644 --- a/kernel/src/memory/paging.rs +++ b/kernel/src/memory/paging.rs @@ -34,9 +34,10 @@ impl PageDirectory { //indentity page first 32MiB pub fn identity(&mut self) { unsafe { + let paging = &mut (*(&raw mut PAGING)); for i in 0..8 { TABLES[i].set((0x0040_0000 * i) as u32); - PAGING.set_table(i, &TABLES[i]); + paging.set_table(i, &TABLES[i]); } } } diff --git a/kernel/src/shell/shell.rs b/kernel/src/shell/shell.rs index effda02..9b76661 100755 --- a/kernel/src/shell/shell.rs +++ b/kernel/src/shell/shell.rs @@ -3,6 +3,7 @@ use crate::filesystem::fat::FAT; use crate::multitasking::task::TASK_MANAGER; use crate::syscalls::print::PRINTER; +use crate::libfelix::mutex::Mutex; use crate::memory::paging::PAGING; use crate::memory::paging::TABLES; @@ -21,13 +22,11 @@ run - loads file as task and adds it to the task list ps - lists running tasks rt - removes specified task"; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut SHELL: Shell = Shell { +pub static mut SHELL: Mutex = Mutex::new(Shell { buffer: [0 as char; 256], arg: [0 as char; 11], cursor: 0, -}; +}); const PROMPT: &str = "felix> "; @@ -44,10 +43,14 @@ impl Shell { self.cursor = 0; unsafe { - PRINTER.set_colors(0xc, 0); + let printer = &mut (*(&raw mut PRINTER)); + printer.acquire_mut().set_colors(0xc, 0); + printer.free(); + libfelix::print!("{}", PROMPT); - PRINTER.reset_colors(); + printer.acquire_mut().reset_colors(); + printer.free(); } } @@ -66,7 +69,9 @@ impl Shell { self.cursor -= 1; unsafe { - PRINTER.delete(); + let printer = &mut (*(&raw mut PRINTER)); + printer.acquire_mut().delete(); + printer.free(); } } } @@ -76,10 +81,10 @@ impl Shell { //e9 port hack, new line unsafe { asm!("out dx, al", in("dx") 0xe9 as u16, in("al") '\n' as u8); - } - unsafe { - PRINTER.new_line(); + let printer = &mut (*(&raw mut PRINTER)); + printer.acquire_mut().new_line(); + printer.free(); } self.interpret(); @@ -97,13 +102,15 @@ impl Shell { //list root directory _b if self.is_command("ls") => unsafe { - FAT.acquire().list_entries(); - FAT.free(); + let fat = &mut (*(&raw mut FAT)); + fat.acquire_mut().list_entries(); + fat.free(); + }, //list running tasks _b if self.is_command("ps") => unsafe { - TASK_MANAGER.list_tasks(); + (*(&raw mut TASK_MANAGER)).list_tasks(); }, //remove runing task @@ -116,7 +123,7 @@ impl Shell { //convert first char of arg to id let id = ((b[3] as u8) - 0x30) as usize; - TASK_MANAGER.remove_task(id); + (*(&raw mut TASK_MANAGER)).remove_task(id); //TASK_MANAGER.remove_current_task(); }, @@ -136,13 +143,13 @@ impl Shell { match a { 'a' => { - TASK_MANAGER.add_dummy_task_a(); + (*(&raw mut TASK_MANAGER)).add_dummy_task_a(); } 'b' => { - TASK_MANAGER.add_dummy_task_b(); + (*(&raw mut TASK_MANAGER)).add_dummy_task_b(); } 'c' => { - TASK_MANAGER.add_dummy_task_c(); + (*(&raw mut TASK_MANAGER)).add_dummy_task_c(); } _ => { libfelix::println!("Specify test a, b, or c!"); @@ -170,23 +177,25 @@ impl Shell { for i in 4..15 { self.arg[i - 4] = b[i]; } - let fat = FAT.acquire(); - - let entry = fat.search_file(&self.arg); - - if entry.name[0] != 0 { - fat.read_file_to_buffer(entry); - - for c in fat.buffer { - if c != 0 { - libfelix::print!("{}", c as char); - } - } - libfelix::println!(); - } else { - libfelix::println!("File not found!"); - } - FAT.free(); + unsafe { + let fat = (*(&raw mut FAT)).acquire(); + + let entry = fat.search_file(&self.arg); + + if entry.name[0] != 0 { + fat.read_file_to_buffer(entry); + + for c in fat.buffer { + if c != 0 { + libfelix::print!("{}", c as char); + } + } + libfelix::println!(); + } else { + libfelix::println!("File not found!"); + } + (*(&raw mut FAT)).free(); + } } //loads an executable as a task @@ -194,32 +203,32 @@ impl Shell { for i in 4..15 { self.arg[i - 4] = b[i]; } - let fat = FAT.acquire(); - - let entry = fat.search_file(&self.arg); - if entry.name[0] != 0 { - let slot = TASK_MANAGER.get_free_slot(); - let target = APP_TARGET + (slot as u32 * APP_SIZE); - - //map table 8 (0x02000000) to the address where the executable is loaded - TABLES[8].set(target); - PAGING.set_table(8, &TABLES[8]); - - fat.read_file_to_target(&entry, target as *mut u32); - - unsafe { - let signature = *(target as *mut u32); - - if signature == APP_SIGNATURE { - TASK_MANAGER.add_task((target + 4) as u32); - } else { - libfelix::println!("File is not a valid executable!"); - } - } - } else { - libfelix::println!("Program not found!"); - } - FAT.free(); + unsafe { + let fat = (*(&raw mut FAT)).acquire(); + + let entry = fat.search_file(&self.arg); + if entry.name[0] != 0 { + let slot = (*(&raw mut TASK_MANAGER)).get_free_slot(); + let target = APP_TARGET + (slot as u32 * APP_SIZE); + + //map table 8 (0x02000000) to the address where the executable is loaded + TABLES[8].set(target); + (*(&raw mut PAGING)).set_table(8, &TABLES[8]); + + fat.read_file_to_target(&entry, target as *mut u32); + + let signature = *(target as *mut u32); + + if signature == APP_SIGNATURE { + (*(&raw mut TASK_MANAGER)).add_task((target + 4) as u32); + } else { + libfelix::println!("File is not a valid executable!"); + } + } else { + libfelix::println!("Program not found!"); + } + (*(&raw mut FAT)).free(); + } } pub fn is_command(&self, command: &str) -> bool { diff --git a/kernel/src/syscalls/handler.rs b/kernel/src/syscalls/handler.rs index 214a593..66c3ab0 100644 --- a/kernel/src/syscalls/handler.rs +++ b/kernel/src/syscalls/handler.rs @@ -2,8 +2,8 @@ use crate::drivers::pic::PICS; use crate::multitasking::task::TASK_MANAGER; -use crate::syscalls::print; -use core::arch::asm; +use crate::syscalls::print::{PRINTER, Printer}; +use core::arch::{naked_asm}; use core::slice; use core::str; @@ -11,23 +11,20 @@ use core::str; pub const SYSCALL_INT: u8 = 0x80; //SYSCALL IRQ, calls local function using cdecl calling convention -#[naked] +#[unsafe(naked)] pub extern "C" fn syscall() { - unsafe { - asm!( - "push eax", - "push ebx", - "push ecx", - "call syscall_handler", - "add esp, 12", - "iretd", - options(noreturn) - ); - } + naked_asm!( + "push eax", + "push ebx", + "push ecx", + "call syscall_handler", + "add esp, 12", + "iretd", + ); } //handle syscalls, get syscall number from eax register -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn syscall_handler(ecx: u32, ebx: u32, eax: u32) { unsafe { match eax { @@ -37,13 +34,14 @@ pub extern "C" fn syscall_handler(ecx: u32, ebx: u32, eax: u32) { let slice = slice::from_raw_parts(ebx as *const u8, ecx as usize); str::from_utf8(slice) }; - - print::PRINTER.prints(s.unwrap()); + let mutex_printer: &mut Printer = (*(&raw mut PRINTER)).acquire_mut(); + mutex_printer.prints(s.unwrap()); + (*(&raw mut PRINTER)).free(); } //SYSCALL 1, remove current active task 1 => { - TASK_MANAGER.remove_current_task(); + (*(&raw mut TASK_MANAGER)).remove_current_task(); } _ => {} diff --git a/kernel/src/syscalls/print.rs b/kernel/src/syscalls/print.rs index 3dbe484..73713b3 100644 --- a/kernel/src/syscalls/print.rs +++ b/kernel/src/syscalls/print.rs @@ -2,15 +2,14 @@ //Manages text output by directly writing to VGA video memory use core::arch::asm; +use libfelix::mutex::Mutex; -//Warning! Mutable static here -//TODO: Implement a mutex to get safe access to this -pub static mut PRINTER: Printer = Printer { +pub static mut PRINTER: Mutex = Mutex::new(Printer { x: 0, y: 0, foreground: 0x7, background: 0, -}; +}); const WIDTH: u16 = 80; const HEIGHT: u16 = 25; diff --git a/lib/src/print.rs b/lib/src/print.rs index 09f05d3..1734c3d 100755 --- a/lib/src/print.rs +++ b/lib/src/print.rs @@ -51,6 +51,6 @@ macro_rules! println { pub fn _print(args: fmt::Arguments) { use core::fmt::Write; unsafe { - PRINTER.write_fmt(args).unwrap(); + (*(&raw mut PRINTER)).write_fmt(args).unwrap(); } } diff --git a/x86_16-felix.json b/x86_16-felix.json index 0665c45..ef9c260 100644 --- a/x86_16-felix.json +++ b/x86_16-felix.json @@ -1,7 +1,7 @@ { "arch": "x86", "cpu": "i386", - "data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128", + "data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128", "dynamic-linking": false, "executables": true, "linker-flavor": "ld.lld", @@ -17,4 +17,4 @@ "os": "none", "vendor": "unknown", "relocation-model": "static" -} \ No newline at end of file +} diff --git a/x86_32-felix.json b/x86_32-felix.json index a72eac8..227250d 100644 --- a/x86_32-felix.json +++ b/x86_32-felix.json @@ -1,7 +1,7 @@ { "arch": "x86", "cpu": "i386", - "data-layout": "e-m:e-i32:32-f80:128-n8:16:32-S128-p:32:32", + "data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128", "dynamic-linking": false, "executables": true, "linker-flavor": "ld.lld", @@ -17,5 +17,6 @@ "os": "none", "vendor": "unknown", "relocation-model": "static", - "features": "+soft-float,-sse,-mmx" -} \ No newline at end of file + "features": "+soft-float,-sse,-mmx", + "rustc-abi": "x86-softfloat" +}