Add a reboot command (#328)

* Add a reboot command

* Add STOP syscall

* Add ctrl-alt-del reboot keys

* Add comment to keyboard shortcut
This commit is contained in:
Vincent Ollivier 2022-04-12 09:55:53 +02:00 committed by GitHub
parent 41eaae9fbe
commit 18bfaf93d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 7 deletions

View File

@ -1,5 +1,7 @@
# MOROS Syscalls
This list is unstable and subject to change between versions of MOROS.
## EXIT (0x1)
```rust
@ -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`.

View File

@ -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};

View File

@ -1,4 +1,5 @@
use crate::sys;
use crate::api::syscall;
use core::sync::atomic::{AtomicBool, Ordering};
use lazy_static::lazy_static;
@ -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 {
@ -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),
_ => {},
};

View File

@ -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!();
}

View File

@ -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;

View File

@ -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();
@ -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
}

View File

@ -1,4 +1,4 @@
use crate::{sys, usr};
use crate::usr;
use crate::api::syscall;
use crate::api::console::Style;
@ -6,6 +6,7 @@ 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) }
}

View File

@ -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;

12
src/usr/reboot.rs Normal file
View File

@ -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) }
}

View File

@ -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)]
@ -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),