diff --git a/kernel-rs/Makefile b/kernel-rs/Makefile index 5a421239..7bc433c7 100644 --- a/kernel-rs/Makefile +++ b/kernel-rs/Makefile @@ -6,8 +6,8 @@ TARGET ?= $(ARCH)-$(OS) all: @printf "make kernel\t# build kernel binary\n" - @printf "make iso\t# build iso cdrom with grub\n" - @printf "make qemu\t# run qemu+gdb in tmux window\n" + @printf "make iso\t# build iso cdrom\n" + @printf "make qemu\t# run iso in qemu\n" ## COMPILE ASM (nasm) asm_source := $(wildcard src/arch/$(ARCH)/*.asm) @@ -35,11 +35,12 @@ clean: .PHONY: clean kernel iso $(rust_os) +# Bootloader recipes +ISO := $(kernel:.bin=.iso) +iso: $(ISO) +include mk/grub.mk + # Emulation recipes include mk/qemu.mk -# Bootloader recipes -include mk/grub.mk -iso: $(grub-iso) - kernel: $(kernel) diff --git a/kernel-rs/mk/grub.mk b/kernel-rs/mk/grub.mk index 80d40dfa..2cd521b8 100644 --- a/kernel-rs/mk/grub.mk +++ b/kernel-rs/mk/grub.mk @@ -1,8 +1,7 @@ -grub-iso := $(kernel:.bin=.iso) grub-cfg := src/arch/$(ARCH)/grub.cfg isodir := build/isofiles -$(grub-iso): $(kernel) $(grub-cfg) Makefile +$(ISO): $(kernel) $(grub-cfg) Makefile @mkdir -p $(isodir)/boot/grub @cp $(grub-cfg) $(isodir)/boot/grub @cp $(kernel) $(isodir)/boot/$(OS) diff --git a/kernel-rs/mk/qemu.mk b/kernel-rs/mk/qemu.mk index c99cce58..7dd71894 100644 --- a/kernel-rs/mk/qemu.mk +++ b/kernel-rs/mk/qemu.mk @@ -1,35 +1,24 @@ -ifeq ($(shell whoami), jack) - PORT_MONIT := 4242 - PORT_GDB := 4244 -else - PORT_MONIT := 4342 - PORT_GDB := 4344 -endif +QEMU_SOCKET := /tmp/qemu.sock +QEMU_MONITOR := socat - unix-connect:$(QEMU_SOCKET) +QEMU_GDB_PORT := 4242 -QEMU := qemu-system-i386\ - -cdrom build/bluesnow-x86.iso\ - -S\ - -enable-kvm\ - -curses\ - -gdb tcp::$(PORT_GDB)\ - -monitor telnet:127.0.0.1:$(PORT_MONIT),server,nowait qemu: - $(QEMU) + qemu-system-i386\ + -cdrom $(ISO)\ + -S\ + -enable-kvm\ + -curses\ + -gdb tcp::$(QEMU_GDB_PORT)\ + -monitor unix:${QEMU_SOCKET},server,nowait -GDB := gdb -q\ - -symbols "$(kernel)" \ - -ex "target remote :$(PORT_GDB)"\ - -ex "set arch i386" -gdb: - $(GDB) +qemu-gdb: + gdb -q\ + -symbols "$(kernel)" \ + -ex "target remote :$(QEMU_GDB_PORT)"\ + -ex "set arch i386" -MONITOR := telnet 127.0.0.1 $(PORT_MONIT);\ - kill \`ps -x | grep \"[g]db -q\" | cut -d \ -f 1 \`;\ - kill \`ps -x | grep \"[g]db -q\" | cut -d \ -f 2 \` -monitor: - telnet 127.0.0.1 $(PORT_MONIT) - -#not using this anymore -william: - @tmux info >&- || { echo -e "\033[38;5;16mPlease run inside a tmux session\033[0m" ; exit 1; } - @tmux new-window 'tmux split-window -h "$(MONITOR)"; tmux split-window -fv "$(GDB)"; tmux select-pane -t 1; tmux resize-pane -x 80 -y 25; $(QEMU)' +qemu-monitor: + $(QEMU_MONITOR) +qemu-reload: + echo "change ide1-cd0 $(ISO)" | $(QEMU_MONITOR) &>- + echo "system_reset" | $(QEMU_MONITOR) &>- diff --git a/kernel-rs/src/arch/x86/interrupt/irq.rs b/kernel-rs/src/arch/x86/interrupt/irq.rs index 7385a455..0a6e349c 100644 --- a/kernel-rs/src/arch/x86/interrupt/irq.rs +++ b/kernel-rs/src/arch/x86/interrupt/irq.rs @@ -56,9 +56,6 @@ interrupt!(0, pit, { let sum = offset.1 + PIT_RATE; offset.1 = sum % 1_000_000; offset.0 += sum / 1_000_000; - if sum > 1_000_000 { - // fprintln!("uptime: {}s", offset.0); - } } unsafe { pic::MASTER.ack() }; }); diff --git a/kernel-rs/src/console.rs b/kernel-rs/src/console.rs index e7e0d974..59f981ec 100644 --- a/kernel-rs/src/console.rs +++ b/kernel-rs/src/console.rs @@ -2,6 +2,7 @@ extern crate core; // extern crate multiboot2; use acpi; +use time; use keyboard::PS2; use core::char; use vga::*; @@ -28,6 +29,9 @@ fn dispatch(command: &str) -> Result<(), &'static str> { "overflow" => self::overflow(), "page_fault" => self::page_fault(), + // time + "uptime" => self::uptime(), + _ => Err("Command unknown. (h|help for help)"), } } @@ -59,6 +63,13 @@ fn help() -> Result<(), &'static str> { Ok(()) } +fn uptime() -> Result<(), &'static str> { + let mut offset = time::OFFSET.lock(); + fprintln!("{}s", offset.0 + offset.1 / 1_000_000); + flush!(); + Ok(()) +} + use x86::instructions::halt; /// Reboot the kernel /// diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index f323022c..4d4b3fbe 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -54,10 +54,11 @@ pub fn kmain() -> ! { // memory init after heap is available memory::init_noncore(); - // vga is *not* cpu specific I think + // load vga after core because is *not* cpu specific I think vga::init(); - scheduling::schedule(); + // scheduler WIP + // scheduling::schedule(); unreachable!(); } diff --git a/kernel-rs/src/scheduling/mod.rs b/kernel-rs/src/scheduling/mod.rs index e1708787..0c05f51e 100644 --- a/kernel-rs/src/scheduling/mod.rs +++ b/kernel-rs/src/scheduling/mod.rs @@ -1,9 +1,10 @@ mod process; +mod sleep; + mod fifo; use spin::Mutex; -pub use self::process::*; - +pub use self::process::*; lazy_static! { pub static ref SCHEDULER: Mutex = Mutex::new({ let init_process: u32 = self::init as *const () as u32; @@ -13,6 +14,7 @@ lazy_static! { }); } +/// Scheduler algorithm needs to implement this pub trait Scheduler { fn add_task(&mut self, ip: u32); fn next(&mut self) -> Option; @@ -42,7 +44,9 @@ pub fn fork() -> i32 { 0 } -pub fn sleep() {} +pub fn sleep() { + +} pub fn init() { fprintln!("init first line"); diff --git a/kernel-rs/src/scheduling/sleep.rs b/kernel-rs/src/scheduling/sleep.rs index ceb57610..ff4c75d7 100644 --- a/kernel-rs/src/scheduling/sleep.rs +++ b/kernel-rs/src/scheduling/sleep.rs @@ -4,3 +4,38 @@ //! managing these //! //! inspired from https://wiki.osdev.org/Blocking_Process + +use alloc::VecDeque; +use super::*; + +struct Sleeper { + process: Process, + // ms + ticks: u32, +} + +/// osdev calls this a delta queue but google doesnt know +struct DeltaQueue { + queue: VecDeque, +} + +impl DeltaQueue { + pub fn insert(&mut self, process: Process, ticks: u32) { + let sleeper = Sleeper { process, ticks }; + } + + /// decreases timer on the list and returns the number + /// of finished sleepers + pub fn tick(&mut self) -> u32 { + let mut i: u32 = 0; + while let Some(link) = self.queue.get_mut(i as usize) { + // find how many links have 0 ticks left + if link.ticks > 0 { + link.ticks -= 1; + break; + } + i += 1; + } + i + } +}