Commit 2d2ed341 authored by Nifou's avatar Nifou

Merge the struct `SyscallStack` with the struct `Registers`

parent 0d4fb894
......@@ -113,7 +113,7 @@ impl<'a> ElfLoader<'a> {
/// Load the binary in memory
pub fn load(&mut self) {
let pid = Process::new(self.binary.entry, self.info());
let pid = Process::new(self.binary.entry as usize, self.info());
let mut processes = PROCESSES.write();
let process = processes.get_mut(&pid).unwrap();
......
......@@ -46,7 +46,7 @@ pub extern "x86-interrupt" fn timer_interrupt_handler(stack_frame: &mut Interrup
crate::memory::switch_to_kernel_page_table();
// The current process is interrupted, save the instruction and the stack pointers
PROCESSOR.tick(Some((stack_frame.instruction_pointer.as_u64(), stack_frame.stack_pointer.as_u64())));
PROCESSOR.tick(Some((stack_frame.instruction_pointer.as_u64() as usize, stack_frame.stack_pointer.as_u64() as usize)));
}
/// The keyboard interrupt handler
......
......@@ -14,61 +14,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Syscall management. There are organized in some subcategorie:
//! Syscall management. There are organized in some subcategories:
//! * **Process** for syscalls used for tasking (exit, ...)
//! * **Filesystem** for syscalls used to manage the filesystem (open, read, write, close, ...)
//! * **Memory** for syscalls used to manage the memory (mmap, brk, ...)
//! * **Misc** for the others
use x86::msr;
use crate::tasking::context::Registers;
pub mod process;
pub mod fs;
pub mod memory;
pub mod misc;
#[allow(dead_code)]
#[repr(packed)]
#[derive(Debug, Clone, Copy)]
/// The stack which contains the registers saved and restored during a syscall
pub struct SyscallStack {
/// The R11 register
pub r11: usize,
/// The R10 register
pub r10: usize,
/// The R9 register
pub r9: usize,
/// The R8 register
pub r8: usize,
/// The RSI register
pub rsi: usize,
/// The RDI register
pub rdi: usize,
/// The RDX register
pub rdx: usize,
/// The RCX register
pub rcx: usize,
/// The RBX register
pub rbx: usize,
/// The RAX register
pub rax: usize,
/// The RIP register (instruction pointer)
pub rip: usize,
/// The CS register (code segment)
pub cs: usize,
/// The RFLAGS register (which contains [various flags](https://en.wikipedia.org/wiki/FLAGS_register))
pub rflags: usize,
}
/// Init the syscalls by setting some [model specific registers](https://en.wikipedia.org/wiki/Model-specific_register).
pub unsafe fn init() {
use crate::gdt;
......@@ -91,7 +50,7 @@ pub unsafe fn init() {
/// * Restores the register's values
pub unsafe extern fn syscall() {
#[inline(never)]
unsafe fn inner(stack: &'static mut SyscallStack) -> usize {
unsafe fn inner(stack: &'static mut Registers) -> usize {
Syscall::new(stack).syscall()
}
......@@ -126,7 +85,7 @@ pub unsafe extern fn syscall() {
llvm_asm!("" : "={rsp}"(rsp) : : : "intel", "volatile");
// Call the inner function
let _a = inner(&mut *(rsp as *mut SyscallStack));
let _a = inner(&mut *(rsp as *mut Registers));
// The return value of the syscall (RAX) is already set because the return value
// of a function is stored in RAX, so nothing to do!
......@@ -155,12 +114,12 @@ const ARCH_PRCTL: usize = 158;
/// A structure used to call the right handler for each syscall
pub struct Syscall {
stack: &'static mut SyscallStack,
stack: &'static mut Registers,
}
impl Syscall {
/// Create a new Syscall
pub fn new(stack: &'static mut SyscallStack) -> Self {
pub fn new(stack: &'static mut Registers) -> Self {
Self {
stack,
}
......
......@@ -38,7 +38,7 @@ pub struct Context {
impl Context {
/// Create a new context
pub fn new(rip: u64, rsp: u64) -> Self {
pub fn new(rip: usize, rsp: usize) -> Self {
let mut regs = Registers::default();
regs.rsp = rsp; // Set stack
regs.rip = rip; // Set entry
......@@ -52,14 +52,13 @@ impl Context {
}
/// Save the current registers in this context
pub fn save(&mut self, saved: Option<(u64, u64)>) {
pub fn save(&mut self, saved: Option<(usize, usize)>) {
self.regs.rax = Rax::get();
self.regs.rbx = Rbx::get();
self.regs.rcx = Rcx::get();
self.regs.rdx = Rdx::get();
self.regs.rsi = Rsi::get();
self.regs.rdi = Rdi::get();
self.regs.rbp = Rbp::get();
if let Some((rip, rsp)) = saved {
self.regs.rip = rip;
......@@ -70,10 +69,6 @@ impl Context {
self.regs.r9 = R9::get();
self.regs.r10 = R10::get();
self.regs.r11 = R11::get();
self.regs.r12 = R12::get();
self.regs.r13 = R13::get();
self.regs.r14 = R14::get();
self.regs.r15 = R15::get();
self.regs.rflags = Rflags::get();
// Save the SSE state
......@@ -88,16 +83,11 @@ impl Context {
Rdx::set(self.regs.rdx);
Rsi::set(self.regs.rsi);
Rdi::set(self.regs.rdi);
Rbp::set(self.regs.rbp);
R8::set(self.regs.r8);
R9::set(self.regs.r9);
R10::set(self.regs.r10);
R11::set(self.regs.r11);
R12::set(self.regs.r12);
R13::set(self.regs.r13);
R14::set(self.regs.r14);
R15::set(self.regs.r15);
Rflags::set(self.regs.rflags);
// Load the SSE state
......@@ -109,99 +99,85 @@ impl Context {
}
/// Get the rip register (which indicates the next address which will be executed)
pub fn get_rip(&self) -> u64 {
pub fn get_rip(&self) -> usize {
self.regs.rip
}
/// Get the rsp register (which indicates the address of the stack in the memory)
pub fn get_rsp(&self) -> u64 {
pub fn get_rsp(&self) -> usize {
self.regs.rsp
}
}
#[allow(dead_code)]
#[repr(packed)]
#[derive(PartialEq, Clone, Copy, Debug)]
/// A sructure which contains all registers of the CPU
pub struct Registers {
/// The RAX register
pub rax: u64,
/// The RBX register
pub rbx: u64,
/// The RCX register
pub rcx: u64,
/// The RDX register
pub rdx: u64,
/// The R11 register
pub r11: usize,
/// The RDI register
pub rdi: u64,
/// The R10 register
pub r10: usize,
/// The RSI register
pub rsi: u64,
/// The R9 register
pub r9: usize,
/// The R8 register
pub r8: u64,
pub r8: usize,
/// The R9 register
pub r9: u64,
/// The R10 register
pub r10: u64,
/// The RSI register
pub rsi: usize,
/// The R11 register
pub r11: u64,
/// The RDI register
pub rdi: usize,
/// The R12 register
pub r12: u64,
/// The RDX register
pub rdx: usize,
/// The R13 register
pub r13: u64,
/// The RCX register
pub rcx: usize,
/// The R14 register
pub r14: u64,
/// The RBX register
pub rbx: usize,
/// The R15 register
pub r15: u64,
/// The RAX register
pub rax: usize,
/// The RSP register
pub rsp: u64,
/// The RIP register (instruction pointer)
pub rip: usize,
/// The RBP register
pub rbp: u64,
/// The CS register (code segment)
pub cs: usize,
/// The RIP register
pub rip: u64,
/// The RFLAGS register (which contains [various flags](https://en.wikipedia.org/wiki/FLAGS_register))
pub rflags: usize,
/// The RFLAGS register
pub rflags: u64,
/// The RSP register (the stack pointer)
pub rsp: usize,
/// The FX register
pub fx: usize,
/// The DS register (the data segment)
pub ds: usize,
}
impl core::default::Default for Registers {
fn default() -> Self {
Self {
r15: 0x0,
r14: 0x0,
r13: 0x0,
r12: 0x0,
r11: 0x0,
r10: 0x0,
r9: 0x0,
r8: 0x0,
rsi: 0x0,
rsp: 0x0,
rbp: 0x0,
rdi: 0x0,
rdx: 0x0,
rcx: 0x0,
rbx: 0x0,
rax: 0x0,
rip: 0x0,
fx: 0x0,
cs: 0x0,
rflags: 0x0,
rsp: 0x0,
ds: 0x0,
}
}
}
......@@ -70,12 +70,12 @@ pub struct Process {
impl Process {
/// Create a new process
pub fn new(rip: u64, mut info: ProcessInfo) -> Pid {
pub fn new(rip: usize, mut info: ProcessInfo) -> Pid {
// We can use a static address for the stack because the content of it will be switched
// with the switch of page tables
let mut processes = PROCESSES.write();
let mappings = memory::map(STACK_ADDR - STACK_SIZE as u64, STACK_ADDR, Flags::PRESENT | Flags::WRITABLE);
let rsp = info.push_at(STACK_ADDR as usize) as u64;
let rsp = info.push_at(STACK_ADDR as usize);
let mut process = Self {
pid: Pid(NEXT_PID.fetch_add(1, Ordering::Relaxed)),
......
......@@ -76,7 +76,7 @@ impl Processor {
/// Switch to the next thread
///
/// We assume that we are using the kernel's page table
pub fn tick(&self, saved: Option<(u64, u64)>) {
pub fn tick(&self, saved: Option<(usize, usize)>) {
let processes = PROCESSES.read();
let mut inner = self.inner();
......@@ -124,7 +124,7 @@ impl Processor {
}
/// Jump in usermode to a thread
pub fn jmp(&self, rip: u64, rsp: u64, arg1: u64, arg2: u64) {
pub fn jmp(&self, rip: usize, rsp: usize, arg1: u64, arg2: u64) {
use crate::gdt;
unsafe {
......
......@@ -21,7 +21,7 @@
pub struct Rax();
impl Rax {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rax, $0" :
: "r"(value)
......@@ -31,7 +31,7 @@ impl Rax {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rax" : "=r"(value)
......@@ -48,7 +48,7 @@ impl Rax {
pub struct Rbx();
impl Rbx {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rbx, $0" :
: "r"(value)
......@@ -58,7 +58,7 @@ impl Rbx {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rbx" : "=r"(value)
......@@ -75,7 +75,7 @@ impl Rbx {
pub struct Rcx();
impl Rcx {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rcx, $0" :
: "r"(value)
......@@ -85,7 +85,7 @@ impl Rcx {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rcx" : "=r"(value)
......@@ -102,7 +102,7 @@ impl Rcx {
pub struct Rdx();
impl Rdx {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rdx, $0" :
: "r"(value)
......@@ -112,7 +112,7 @@ impl Rdx {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rdx" : "=r"(value)
......@@ -129,7 +129,7 @@ impl Rdx {
pub struct Rsi();
impl Rsi {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rsi, $0" :
: "r"(value)
......@@ -139,7 +139,7 @@ impl Rsi {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rsi" : "=r"(value)
......@@ -156,7 +156,7 @@ impl Rsi {
pub struct Rdi();
impl Rdi {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rdi, $0" :
: "r"(value)
......@@ -166,7 +166,7 @@ impl Rdi {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rdi" : "=r"(value)
......@@ -183,7 +183,7 @@ impl Rdi {
pub struct R8();
impl R8 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r8, $0" :
: "r"(value)
......@@ -193,7 +193,7 @@ impl R8 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r8" : "=r"(value)
......@@ -210,7 +210,7 @@ impl R8 {
pub struct R9();
impl R9 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r9, $0" :
: "r"(value)
......@@ -220,7 +220,7 @@ impl R9 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r9" : "=r"(value)
......@@ -237,7 +237,7 @@ impl R9 {
pub struct R10();
impl R10 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r10, $0" :
: "r"(value)
......@@ -247,7 +247,7 @@ impl R10 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r10" : "=r"(value)
......@@ -264,7 +264,7 @@ impl R10 {
pub struct R11();
impl R11 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r11, $0" :
: "r"(value)
......@@ -274,7 +274,7 @@ impl R11 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r11" : "=r"(value)
......@@ -291,7 +291,7 @@ impl R11 {
pub struct R12();
impl R12 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r12, $0" :
: "r"(value)
......@@ -301,7 +301,7 @@ impl R12 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r12" : "=r"(value)
......@@ -318,7 +318,7 @@ impl R12 {
pub struct R13();
impl R13 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r13, $0" :
: "r"(value)
......@@ -328,7 +328,7 @@ impl R13 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r13" : "=r"(value)
......@@ -345,7 +345,7 @@ impl R13 {
pub struct R14();
impl R14 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r14, $0" :
: "r"(value)
......@@ -355,7 +355,7 @@ impl R14 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r14" : "=r"(value)
......@@ -372,7 +372,7 @@ impl R14 {
pub struct R15();
impl R15 {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r15, $0" :
: "r"(value)
......@@ -382,7 +382,7 @@ impl R15 {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r15" : "=r"(value)
......@@ -399,7 +399,7 @@ impl R15 {
pub struct Rbp();
impl Rbp {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rbp, $0" :
: "r"(value)
......@@ -409,7 +409,7 @@ impl Rbp {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rbp" : "=r"(value)
......@@ -426,7 +426,7 @@ impl Rbp {
pub struct Rflags();
impl Rflags {
/// Set the value of this register
pub fn set(value: u64) {
pub fn set(value: usize) {
unsafe {
llvm_asm!("push $0 ; popfq" :
: "r"(value)
......@@ -436,7 +436,7 @@ impl Rflags {
}
/// Get the value of this register
pub fn get() -> u64 {
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("pushfq ; pop $0" : "=r"(value)
......
......@@ -46,7 +46,7 @@ pub struct Thread {
impl Thread {
/// Create a new Thread
pub fn new(process: Pid, rip: u64, rsp: u64) -> Self {
pub fn new(process: Pid, rip: usize, rsp: usize) -> Self {
Self {