Skip to content

Commit

Permalink
Restructures execution engine (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Oct 16, 2023
1 parent 94cf76f commit 3ae9711
Show file tree
Hide file tree
Showing 26 changed files with 1,723 additions and 1,612 deletions.
8 changes: 4 additions & 4 deletions src/kernel/src/arnd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ops::DerefMut;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

/// Random number generator based on
/// https://github.com/freebsd/freebsd-src/blob/release/9.1.0/sys/libkern/arc4random.c.
Expand All @@ -9,16 +9,16 @@ pub struct Arnd {
}

impl Arnd {
pub fn new() -> Self {
pub fn new() -> Arc<Self> {
let mut sbox = [0u8; 256];

for (i, e) in sbox.iter_mut().enumerate() {
*e = i as u8;
}

Self {
Arc::new(Self {
state: Mutex::new(State { i: 0, j: 0, sbox }),
}
})
}

pub fn rand_bytes(&self, buf: &mut [u8]) {
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/src/ee/llvm/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::llvm::module::LlvmModule;
use thiserror::Error;

/// Contains states for lifting a module.
pub(super) struct Codegen<'a, 'b: 'a> {
pub(super) struct Codegen<'a> {
input: Disassembler<'a>,
output: &'a mut LlvmModule<'b>,
output: &'a mut LlvmModule,
}

impl<'a, 'b: 'a> Codegen<'a, 'b> {
pub fn new(input: Disassembler<'a>, output: &'a mut LlvmModule<'b>) -> Self {
impl<'a> Codegen<'a> {
pub fn new(input: Disassembler<'a>, output: &'a mut LlvmModule) -> Self {
Self { input, output }
}

Expand Down
79 changes: 56 additions & 23 deletions src/kernel/src/ee/llvm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
use self::codegen::Codegen;
use super::{EntryArg, ExecutionEngine};
use super::{ExecutionEngine, SysErr, SysIn, SysOut};
use crate::disasm::Disassembler;
use crate::fs::VPathBuf;
use crate::llvm::Llvm;
use crate::rtld::{Module, RuntimeLinker};
use std::ops::Deref;
use crate::rtld::Module;
use std::sync::Arc;
use thiserror::Error;

mod codegen;

/// An implementation of [`ExecutionEngine`] using JIT powered by LLVM IR.
#[derive(Debug)]
pub struct LlvmEngine {
llvm: &'static Llvm,
rtld: &'static RuntimeLinker,
llvm: Arc<Llvm>,
}

impl LlvmEngine {
pub fn new(llvm: &'static Llvm, rtld: &'static RuntimeLinker) -> Self {
Self { llvm, rtld }
}

pub fn lift_initial_modules(&mut self) -> Result<(), LiftError> {
for module in self.rtld.list().deref() {
// TODO: Store the lifted module somewhere.
self.lift(module)?;
}

Ok(())
pub fn new(llvm: &Arc<Llvm>) -> Arc<Self> {
Arc::new(Self { llvm: llvm.clone() })
}

fn lift(
&self,
module: &Module,
) -> Result<crate::llvm::module::ExecutionEngine<'static>, LiftError> {
module: &Module<Self>,
) -> Result<crate::llvm::module::ExecutionEngine, LiftError> {
// Get a list of public functions.
let path = module.path();
let targets = match module.entry() {
Expand Down Expand Up @@ -74,20 +65,62 @@ impl LlvmEngine {
}

impl ExecutionEngine for LlvmEngine {
type RunErr = RunError;
type RawFn = RawFn;
type SetupModuleErr = SetupModuleError;
type GetFunctionErr = GetFunctionError;

fn register_syscall<O>(
&self,
id: u32,
o: &Arc<O>,
h: fn(&Arc<O>, &SysIn) -> Result<SysOut, SysErr>,
) {
todo!()
}

fn setup_module<E>(&self, md: &mut Module<E>) -> Result<(), Self::SetupModuleErr>
where
E: ExecutionEngine,
{
todo!()
}

unsafe fn run(&mut self, arg: EntryArg) -> Result<(), Self::RunErr> {
unsafe fn get_function<E>(
&self,
md: &Arc<Module<E>>,
addr: usize,
) -> Result<Arc<Self::RawFn>, Self::GetFunctionErr>
where
E: ExecutionEngine,
{
todo!()
}
}

/// Represent an error when [`LlvmEngine::run()`] is failed.
/// An implementation of [`ExecutionEngine::RawFn`].
pub struct RawFn {}

impl super::RawFn for RawFn {
fn addr(&self) -> usize {
todo!()
}

unsafe fn exec1<R, A>(&self, a: A) -> R {
todo!()
}
}

/// An implementation of [`ExecutionEngine::SetupModuleErr`].
#[derive(Debug, Error)]
pub enum SetupModuleError {}

/// An implementation of [`ExecutionEngine::GetFunctionErr`].
#[derive(Debug, Error)]
pub enum RunError {}
pub enum GetFunctionError {}

/// Represents an error when module lifting is failed.
#[derive(Debug, Error)]
pub enum LiftError {
enum LiftError {
#[error("cannot disassemble function {1:#018x} on {0}")]
DisassembleFailed(VPathBuf, usize, #[source] crate::disasm::DisassembleError),

Expand Down
Loading

0 comments on commit 3ae9711

Please sign in to comment.