Skip to content

Commit

Permalink
Add a reboot command (#328)
Browse files Browse the repository at this point in the history
* Add a reboot command

* Add STOP syscall

* Add ctrl-alt-del reboot keys

* Add comment to keyboard shortcut
  • Loading branch information
vinc authored Apr 12, 2022
1 parent 41eaae9 commit 18bfaf9
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 7 deletions.
10 changes: 10 additions & 0 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# MOROS Syscalls

This list is unstable and subject to change between versions of MOROS.

## EXIT (0x1)

```rust
Expand Down Expand Up @@ -71,3 +73,11 @@ pub fn realtime() -> f64
```rust
pub fn delete(path: &str) -> isize
```

## STOP (0xD)

```rust
pub fn stop(code: usize)
```

The system will reboot with `0xcafe` and halt with `0xdead`.
8 changes: 8 additions & 0 deletions src/api/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ pub fn spawn(path: &str) {
unsafe { syscall!(SPAWN, ptr, len) };
}

pub fn reboot() {
unsafe { syscall!(STOP, 0xcafe) };
}

pub fn halt() {
unsafe { syscall!(STOP, 0xdead) };
}

#[test_case]
fn test_file() {
use crate::sys::fs::{mount_mem, format_mem, dismount, OpenFlag};
Expand Down
12 changes: 10 additions & 2 deletions src/sys/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sys;
use crate::api::syscall;

use core::sync::atomic::{AtomicBool, Ordering};
use lazy_static::lazy_static;
Expand All @@ -9,6 +10,8 @@ use x86_64::instructions::port::Port;
lazy_static! {
pub static ref KEYBOARD: Mutex<Option<KeyboardLayout>> = Mutex::new(None);
}
pub static ALT: AtomicBool = AtomicBool::new(false);
pub static CTRL: AtomicBool = AtomicBool::new(false);
pub static SHIFT: AtomicBool = AtomicBool::new(false);

pub enum KeyboardLayout {
Expand Down Expand Up @@ -78,18 +81,23 @@ fn interrupt_handler() {
let scancode = read_scancode();
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
match key_event.code {
KeyCode::AltLeft | KeyCode::AltRight => ALT.store(key_event.state == KeyState::Down, Ordering::Relaxed),
KeyCode::ShiftLeft | KeyCode::ShiftRight => SHIFT.store(key_event.state == KeyState::Down, Ordering::Relaxed),
KeyCode::ControlLeft | KeyCode::ControlRight => CTRL.store(key_event.state == KeyState::Down, Ordering::Relaxed),
_ => {}
}
let is_shifted = SHIFT.load(Ordering::Relaxed);
let is_alt = ALT.load(Ordering::Relaxed);
let is_ctrl = CTRL.load(Ordering::Relaxed);
let is_shift = SHIFT.load(Ordering::Relaxed);

if let Some(key) = keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode('\u{7f}') if is_alt && is_ctrl => syscall::reboot(), // Ctrl-Alt-Del
DecodedKey::RawKey(KeyCode::ArrowUp) => send_csi('A'),
DecodedKey::RawKey(KeyCode::ArrowDown) => send_csi('B'),
DecodedKey::RawKey(KeyCode::ArrowRight) => send_csi('C'),
DecodedKey::RawKey(KeyCode::ArrowLeft) => send_csi('D'),
DecodedKey::Unicode('\t') if is_shifted => send_csi('Z'), // Convert Shift+Tab into Backtab
DecodedKey::Unicode('\t') if is_shift => send_csi('Z'), // Convert Shift-Tab into Backtab
DecodedKey::Unicode(c) => send_key(c),
_ => {},
};
Expand Down
3 changes: 3 additions & 0 deletions src/sys/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub fn dispatcher(n: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
service::spawn(path);
0
}
number::STOP => {
service::stop(arg1)
}
_ => {
unimplemented!();
}
Expand Down
1 change: 1 addition & 0 deletions src/sys/syscall/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub const SLEEP: usize = 0x9;
pub const UPTIME: usize = 0xA;
pub const REALTIME: usize = 0xB;
pub const DELETE: usize = 0xC;
pub const STOP: usize = 0xD;
19 changes: 19 additions & 0 deletions src/sys/syscall/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::sys::fs::FileInfo;
use crate::sys::fs::FileIO;
use crate::sys::process::Process;
use alloc::vec;
use core::arch::asm;

pub fn exit(_code: usize) -> usize {
sys::process::exit();
Expand Down Expand Up @@ -102,3 +103,21 @@ pub fn spawn(path: &str) -> isize {
}
-1
}

pub fn stop(code: usize) -> usize {
match code {
0xcafe => { // Reboot
unsafe {
asm!(
"xor rax, rax",
"mov cr3, rax"
);
}
}
0xdead => { // Halt
sys::acpi::shutdown();
}
_ => {}
}
0
}
5 changes: 3 additions & 2 deletions src/usr/halt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{sys, usr};
use crate::usr;
use crate::api::syscall;
use crate::api::console::Style;

pub fn main(_args: &[&str]) -> usr::shell::ExitCode {
let csi_color = Style::color("Yellow");
let csi_reset = Style::reset();
println!("{}MOROS has reached its fate, the system is now halting.{}", csi_color, csi_reset);
sys::acpi::shutdown();
syscall::sleep(0.5);
syscall::halt();
loop { syscall::sleep(1.0) }
}
1 change: 1 addition & 0 deletions src/usr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod pow;
pub mod print;
pub mod r#move;
pub mod read;
pub mod reboot;
pub mod shell;
pub mod sleep;
pub mod tcp;
Expand Down
12 changes: 12 additions & 0 deletions src/usr/reboot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::usr;
use crate::api::syscall;
use crate::api::console::Style;

pub fn main(_args: &[&str]) -> usr::shell::ExitCode {
let csi_color = Style::color("Yellow");
let csi_reset = Style::reset();
println!("{}MOROS has reached its fate, the system is now rebooting.{}", csi_color, csi_reset);
syscall::sleep(0.5);
syscall::reboot();
loop { syscall::sleep(1.0) }
}
7 changes: 4 additions & 3 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use alloc::vec::Vec;
use alloc::string::String;

// TODO: Scan /bin
const AUTOCOMPLETE_COMMANDS: [&str; 37] = [
const AUTOCOMPLETE_COMMANDS: [&str; 38] = [
"2048", "base64", "calc", "clear", "colors", "copy", "date", "delete", "dhcp", "disk", "edit",
"env", "exit", "geotime", "goto", "halt", "help", "hex", "host", "http", "httpd", "install",
"keyboard", "lisp", "list", "memory", "move", "net", "pci", "print", "read", "shell", "sleep",
"tcp", "user", "vga", "write"
"keyboard", "lisp", "list", "memory", "move", "net", "pci", "print", "read", "reboot", "shell",
"sleep", "tcp", "user", "vga", "write"
];

#[repr(u8)]
Expand Down Expand Up @@ -248,6 +248,7 @@ pub fn exec(cmd: &str) -> ExitCode {
"date" => usr::date::main(&args),
"env" => usr::env::main(&args),
"halt" => usr::halt::main(&args),
"reboot" => usr::reboot::main(&args),
"hex" => usr::hex::main(&args),
"net" => usr::net::main(&args),
"dhcp" => usr::dhcp::main(&args),
Expand Down

0 comments on commit 18bfaf9

Please sign in to comment.