Commit 65e58166 authored by Nifou's avatar Nifou

Remove the struct Context and the file registers.rs which are now useless

parent 11e60df0
......@@ -16,99 +16,6 @@
*/
//! The context of a process (its registers, its stack, the SSE state, ...).
//! See [OSDEV](https://wiki.osdev.org/Context_Switching)
use alloc::{
prelude::v1::Box,
alloc::Layout,
};
use core::alloc::GlobalAlloc;
use crate::memory::heap::HEAP_ALLOCATOR;
use super::registers::*;
/// The size of SSE, x87 FPU, and MMX states in memory (in the FX register)
pub const FX_AREA_SIZE: usize = 512;
#[derive(Clone)]
/// The context of a process (its registers and its stack).
pub struct Context {
regs: Registers,
sse_state: u64,
}
impl Context {
/// Create a new context
pub fn new(rip: usize, rsp: usize) -> Self {
let mut regs = Registers::default();
regs.rsp = rsp; // Set stack
regs.rip = rip; // Set entry
Self {
regs,
sse_state: unsafe {
Box::into_raw(Box::from_raw(HEAP_ALLOCATOR.alloc(Layout::from_size_align_unchecked(512, 16)) as *mut [u8; 512])) as u64
},
}
}
/// Save the current registers in this context
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();
if let Some((rip, rsp)) = saved {
self.regs.rip = rip;
self.regs.rsp = rsp;
}
self.regs.r8 = R8::get();
self.regs.r9 = R9::get();
self.regs.r10 = R10::get();
self.regs.r11 = R11::get();
self.regs.rflags = Rflags::get();
// Save the SSE state
Fx::save(self.sse_state);
}
/// Load the registers of this context to the registers of the CPU
pub fn load(&self, first_time: bool) {
Rax::set(self.regs.rax);
Rbx::set(self.regs.rbx);
Rcx::set(self.regs.rcx);
Rdx::set(self.regs.rdx);
Rsi::set(self.regs.rsi);
Rdi::set(self.regs.rdi);
R8::set(self.regs.r8);
R9::set(self.regs.r9);
R10::set(self.regs.r10);
R11::set(self.regs.r11);
Rflags::set(self.regs.rflags);
// Load the SSE state
if first_time {
Fx::init();
} else {
Fx::restore(self.sse_state);
}
}
/// Get the rip register (which indicates the next address which will be executed)
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) -> usize {
self.regs.rsp
}
}
#[allow(dead_code)]
#[repr(packed)]
#[derive(Clone, Copy)]
......
......@@ -26,7 +26,6 @@
pub mod context;
pub mod memory;
pub mod process;
pub mod registers;
pub mod thread;
pub mod info;
pub mod algo;
......
/*
* Copyright (C) 2018-2020 Nicolas Fouquet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This file contains helper functions to use the CPU registers easier
#[derive(Debug)]
/// Rax registers
pub struct Rax();
impl Rax {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rax, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rax" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rbx register
pub struct Rbx();
impl Rbx {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rbx, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rbx" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rcx register
pub struct Rcx();
impl Rcx {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rcx, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rcx" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rdx register
pub struct Rdx();
impl Rdx {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rdx, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rdx" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rsi register
pub struct Rsi();
impl Rsi {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rsi, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rsi" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rdi register
pub struct Rdi();
impl Rdi {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rdi, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rdi" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R8 register
pub struct R8();
impl R8 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r8, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r8" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R9 register
pub struct R9();
impl R9 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r9, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r9" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R10 register
pub struct R10();
impl R10 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r10, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r10" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R11 register
pub struct R11();
impl R11 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r11, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r11" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R12 register
pub struct R12();
impl R12 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r12, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r12" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R13 register
pub struct R13();
impl R13 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r13, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r13" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R14 register
pub struct R14();
impl R14 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r14, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r14" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// R15 register
pub struct R15();
impl R15 {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov r15, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, r15" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rbp register
pub struct Rbp();
impl Rbp {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("mov rbp, $0" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("mov $0, rbp" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Rflags register
pub struct Rflags();
impl Rflags {
/// Set the value of this register
pub fn set(value: usize) {
unsafe {
llvm_asm!("push $0 ; popfq" :
: "r"(value)
: "memory"
: "intel", "volatile");
}
}
/// Get the value of this register
pub fn get() -> usize {
let mut value;
unsafe {
llvm_asm!("pushfq ; pop $0" : "=r"(value)
:
: "memory"
: "intel", "volatile");
}
value
}
}
#[derive(Debug)]
/// Fx register
pub struct Fx();
impl Fx {
/// Restore the SSE state
pub fn restore(addr: u64) {
unsafe {
llvm_asm!("fxrstor [$0]"
:
: "r"(addr)
: "memory"
: "intel", "volatile");
}
}
/// Save the SSE state
pub fn save(addr: u64) {
unsafe {
llvm_asm!("fxsave [$0]"
:
: "r"(addr)
: "memory"
: "intel", "volatile");
}
}
/// Init the SSE state
pub fn init() {
unsafe { llvm_asm!("fninit") }
}
}
......@@ -2,7 +2,6 @@
#![no_std]
use core::panic::PanicInfo;
use core::fmt::{Write, Result};
use core::sync::atomic::{Ordering, AtomicUsize};
const SYSCALL_WRITE: usize = 1;
......@@ -38,14 +37,17 @@ macro_rules! println {
concat!($fmt, "\n"), $($arg)*));
}
pub static COUNTER: AtomicUsize = AtomicUsize::new(0);
#[no_mangle]
pub extern "C" fn main(_: i32, _: *const *const i8) -> i32 {
// This function is interrupted by the timer interrupt handler but it resumes at the same place
// because the context is saved
let mut i = 0;
loop {
print!("{}", COUNTER.fetch_add(1, Ordering::Relaxed));
// Try to don't mess up your terminal ;-)
if i % 10 == 0 {
print!("{}-", i);
}
i += 1;
}
}
......