|
| 1 | +use super::{sched, sched::Context}; |
| 2 | +use crate::{memory::stack::KernelStack, posix::errno::EResult, process::task::Task}; |
| 3 | +use alloc::boxed::Box; |
| 4 | +use core::{arch::asm, mem::offset_of}; |
| 5 | + |
| 6 | +#[repr(C)] |
| 7 | +#[derive(Default, Debug, Clone)] |
| 8 | +pub struct Executor { |
| 9 | + pub rsp: u64, |
| 10 | + pub fpu_region: Box<[u8]>, |
| 11 | + pub ds: u16, |
| 12 | + pub es: u16, |
| 13 | + pub fs: u16, |
| 14 | + pub gs: u16, |
| 15 | + pub fsbase: u64, |
| 16 | + pub gsbase: u64, |
| 17 | + pub restarted: bool, |
| 18 | +} |
| 19 | + |
| 20 | +impl Executor { |
| 21 | + pub fn new_kernel( |
| 22 | + entry: extern "C" fn(usize, usize), |
| 23 | + arg1: usize, |
| 24 | + arg2: usize, |
| 25 | + stack: &KernelStack, |
| 26 | + ) -> EResult<Self> { |
| 27 | + let mut this = Self::default(); |
| 28 | + sched::init_task(&mut this, entry, arg1, arg2, stack, false)?; |
| 29 | + Ok(this) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[repr(C)] |
| 34 | +pub struct ForkedImage { |
| 35 | + rbx: usize, |
| 36 | + rbp: usize, |
| 37 | + r12: usize, |
| 38 | + r13: usize, |
| 39 | + r14: usize, |
| 40 | + r15: usize, |
| 41 | + rsp: usize, |
| 42 | + rip: usize, |
| 43 | + rflags: usize, |
| 44 | +} |
| 45 | + |
| 46 | +impl ForkedImage { |
| 47 | + pub const fn ip(&self) -> usize { |
| 48 | + self.rip |
| 49 | + } |
| 50 | + |
| 51 | + pub const fn sp(&self) -> usize { |
| 52 | + self.rsp |
| 53 | + } |
| 54 | + |
| 55 | + pub const fn flags(&self) -> usize { |
| 56 | + self.rflags |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +pub struct FrameImage<'a> { |
| 61 | + frame: &'a mut Context, |
| 62 | +} |
| 63 | + |
| 64 | +impl<'a> FrameImage<'a> { |
| 65 | + pub fn new(frame: &'a mut Context) -> Self { |
| 66 | + Self { frame } |
| 67 | + } |
| 68 | + |
| 69 | + pub const fn frame(&self) -> &Context { |
| 70 | + self.frame |
| 71 | + } |
| 72 | + |
| 73 | + pub fn frame_mut(&mut self) -> &mut Context { |
| 74 | + self.frame |
| 75 | + } |
| 76 | + |
| 77 | + pub const fn ip(&self) -> usize { |
| 78 | + self.frame.rip as usize |
| 79 | + } |
| 80 | + |
| 81 | + pub const fn sp(&self) -> usize { |
| 82 | + self.frame.rsp as usize |
| 83 | + } |
| 84 | + |
| 85 | + pub const fn flags(&self) -> usize { |
| 86 | + self.frame.rflags as usize |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +pub unsafe fn switch(from: *const Task, to: *const Task) -> *mut Task { |
| 91 | + unsafe { sched::switch(from, to) } |
| 92 | +} |
| 93 | + |
| 94 | +pub fn run_on_stack_raw( |
| 95 | + stack_top: usize, |
| 96 | + entry: extern "C" fn(usize, usize) -> !, |
| 97 | + arg: usize, |
| 98 | +) -> ! { |
| 99 | + sched::run_on_stack_raw(stack_top, entry, arg) |
| 100 | +} |
| 101 | + |
| 102 | +pub fn fork_current(entry: extern "C" fn(*const ForkedImage, usize) -> !, arg: usize) { |
| 103 | + let mut image = ForkedImage { |
| 104 | + rbx: 0, |
| 105 | + rbp: 0, |
| 106 | + r12: 0, |
| 107 | + r13: 0, |
| 108 | + r14: 0, |
| 109 | + r15: 0, |
| 110 | + rsp: 0, |
| 111 | + rip: 0, |
| 112 | + rflags: 0, |
| 113 | + }; |
| 114 | + |
| 115 | + unsafe { |
| 116 | + asm!( |
| 117 | + "mov [{image} + {rbx}], rbx", |
| 118 | + "mov [{image} + {rbp}], rbp", |
| 119 | + "mov [{image} + {r12}], r12", |
| 120 | + "mov [{image} + {r13}], r13", |
| 121 | + "mov [{image} + {r14}], r14", |
| 122 | + "mov [{image} + {r15}], r15", |
| 123 | + "mov [{image} + {rsp}], rsp", |
| 124 | + "lea rax, [rip + 2f]", |
| 125 | + "mov [{image} + {rip}], rax", |
| 126 | + "pushfq", |
| 127 | + "pop [{image} + {rflags}]", |
| 128 | + "mov rdi, {image}", |
| 129 | + "mov rsi, {arg}", |
| 130 | + "call {entry}", |
| 131 | + "ud2", |
| 132 | + "2:", |
| 133 | + image = in(reg) &mut image, |
| 134 | + arg = in(reg) arg, |
| 135 | + entry = in(reg) entry, |
| 136 | + rbx = const offset_of!(ForkedImage, rbx), |
| 137 | + rbp = const offset_of!(ForkedImage, rbp), |
| 138 | + r12 = const offset_of!(ForkedImage, r12), |
| 139 | + r13 = const offset_of!(ForkedImage, r13), |
| 140 | + r14 = const offset_of!(ForkedImage, r14), |
| 141 | + r15 = const offset_of!(ForkedImage, r15), |
| 142 | + rsp = const offset_of!(ForkedImage, rsp), |
| 143 | + rip = const offset_of!(ForkedImage, rip), |
| 144 | + rflags = const offset_of!(ForkedImage, rflags), |
| 145 | + out("rax") _, |
| 146 | + out("rdi") _, |
| 147 | + out("rsi") _, |
| 148 | + out("rdx") _, |
| 149 | + out("rcx") _, |
| 150 | + out("r8") _, |
| 151 | + out("r9") _, |
| 152 | + out("r10") _, |
| 153 | + out("r11") _, |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments