-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x86_64+examples: add fs/gs save/restore and kernel interface
- Loading branch information
Showing
15 changed files
with
282 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "example-x86_64-tls-base" | ||
description = "x86_64 TLS base (FS/GS) interface example" | ||
version = "0.0.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
build = "build.rs" | ||
|
||
[dependencies.oro] | ||
path = "../../../oro" | ||
features = ["module", "panic_debug_out_v0"] | ||
|
||
[build-dependencies.oro] | ||
path = "../../../oro" | ||
features = ["build"] | ||
default-features = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
::oro::build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use oro::debug_out_v0_println as println; | ||
|
||
#[cfg(not(target_arch = "x86_64"))] | ||
#[no_mangle] | ||
fn main() { | ||
println!("This example only works on x86_64"); | ||
} | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
#[no_mangle] | ||
fn main() { | ||
use core::arch::asm; | ||
|
||
use oro::{id::iface::KERNEL_X86_64_TLS_BASE_V0, key, syscall_get, syscall_set}; | ||
|
||
static THE_ANSWER: u64 = 42; | ||
|
||
println!("local answer: {THE_ANSWER}"); | ||
|
||
let fsbase_name: u64 = syscall_get!( | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
0, | ||
key!("name"), | ||
) | ||
.expect("failed to get FS base name"); | ||
|
||
println!("fsbase name: {:?}", oro::Key(&fsbase_name)); | ||
|
||
let fsbase = syscall_get!( | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
0, | ||
key!("base"), | ||
) | ||
.expect("failed to get initial FS base"); | ||
|
||
println!("initial FS base: {fsbase:#016x}"); | ||
|
||
syscall_set!( | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
0, | ||
key!("base"), | ||
&THE_ANSWER as *const _ as u64 | ||
) | ||
.expect("failed to set FS base"); | ||
|
||
println!("set FS base to: {:#016x}", &THE_ANSWER as *const _ as u64); | ||
|
||
let fsbase = syscall_get!( | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
KERNEL_X86_64_TLS_BASE_V0, | ||
0, | ||
key!("base"), | ||
) | ||
.expect("failed to get final FS base"); | ||
|
||
println!("final FS base: {fsbase:#016x}"); | ||
|
||
let answer: u64; | ||
unsafe { | ||
asm!("mov rax, fs:[0]", out("rax") answer); | ||
} | ||
|
||
println!("read FS base: {answer}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! x86_64-specific kernel interfaces. | ||
use oro_kernel::{iface::kernel::KernelInterface, table::Table}; | ||
use oro_mem::alloc::boxed::Box; | ||
|
||
/// Registers the x86_64-specific kernel interfaces. | ||
pub(crate) fn register_kernel_interfaces(table: &mut Table<Box<dyn KernelInterface<crate::Arch>>>) { | ||
macro_rules! register_all { | ||
($($id:ident => $iface_mod:literal $iface:ident),* $(,)?) => { | ||
$({ | ||
#[path = $iface_mod] | ||
mod ifacemod; | ||
|
||
table.insert(::oro::id::iface::$id, Box::new(ifacemod::$iface)); | ||
})* | ||
}; | ||
} | ||
|
||
register_all! { | ||
KERNEL_X86_64_TLS_BASE_V0 => "tls_base_v0.rs" TlsBaseV0, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! x86_64 TLS base pointer (FS/GS) interface. | ||
//! Kernel interface for querying the ring's interfaces | ||
//! based on the interface type. | ||
use crate::Arch as A; | ||
use oro::{key, syscall::Error as SysError}; | ||
use oro_kernel::{syscall::InterfaceResponse, tab::Tab, thread::Thread, iface::kernel::KernelInterface}; | ||
|
||
/// Version 0 of TLS base (FS/GS) kernel interface for x86_64. | ||
#[repr(transparent)] | ||
pub struct TlsBaseV0; | ||
|
||
impl KernelInterface<A> for TlsBaseV0 { | ||
fn get(&self, thread: &Tab<Thread<A>>, index: u64, key: u64) -> InterfaceResponse { | ||
match key { | ||
key!("name") => { | ||
match index { | ||
0 => InterfaceResponse::ok(key!("fs")), | ||
1 => InterfaceResponse::ok(key!("gs")), | ||
_ => InterfaceResponse::immediate(SysError::BadIndex, 0), | ||
} | ||
}, | ||
key!("base") => { | ||
match index { | ||
0 => { | ||
debug_assert_eq!(crate::asm::get_fs_msr(), thread.with(|ctx| ctx.handle().fsbase)); | ||
InterfaceResponse::ok(crate::asm::get_fs_msr()) | ||
} | ||
1 => { | ||
debug_assert_eq!(crate::asm::get_gs_msr(), thread.with(|ctx| ctx.handle().gsbase)); | ||
InterfaceResponse::ok(crate::asm::get_gs_msr()) | ||
} | ||
_ => InterfaceResponse::immediate(SysError::BadIndex, 0), | ||
} | ||
}, | ||
_ => InterfaceResponse::immediate(SysError::BadKey, 0), | ||
} | ||
} | ||
|
||
fn set( | ||
&self, | ||
thread: &Tab<Thread<A>>, | ||
index: u64, | ||
key: u64, | ||
value: u64, | ||
) -> InterfaceResponse { | ||
match key { | ||
key!("name") => InterfaceResponse::immediate(SysError::ReadOnly, 0), | ||
key!("base") => { | ||
match index { | ||
0 => { | ||
// Set the FS base pointer. Will get picked up by the | ||
// next context switch. | ||
thread.with_mut(|ctx| { | ||
ctx.handle_mut().fsbase = value; | ||
}); | ||
}, | ||
1 => { | ||
// Set the GS base pointer. Will get picked up by the | ||
// next context switch. | ||
thread.with_mut(|ctx| { | ||
ctx.handle_mut().gsbase = value; | ||
}); | ||
}, | ||
_ => return InterfaceResponse::immediate(SysError::BadIndex, 0), | ||
} | ||
|
||
InterfaceResponse::ok(0) | ||
} | ||
_ => InterfaceResponse::immediate(SysError::BadKey, 0), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.