-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba8f1fc
commit 4b4d8a5
Showing
22 changed files
with
349 additions
and
67 deletions.
There are no files selected for viewing
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
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
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,54 @@ | ||
.section .text | ||
syscall_entry: | ||
swapgs // switch to kernel gs | ||
mov gs:[offset __PERCPU_USER_RSP_OFFSET], rsp // save user rsp | ||
mov rsp, gs:[offset __PERCPU_TSS + {tss_rsp0_offset}] // switch to kernel stack | ||
|
||
sub rsp, 8 // skip user ss | ||
push gs:[offset __PERCPU_USER_RSP_OFFSET] // user rsp | ||
push r11 // rflags | ||
mov [rsp - 2 * 8], rcx // rip | ||
sub rsp, 4 * 8 // skip until general registers | ||
|
||
push r15 | ||
push r14 | ||
push r13 | ||
push r12 | ||
push r11 | ||
push r10 | ||
push r9 | ||
push r8 | ||
push rdi | ||
push rsi | ||
push rbp | ||
push rbx | ||
push rdx | ||
push rcx | ||
push rax | ||
|
||
mov rdi, rsp | ||
call x86_syscall_handler | ||
|
||
pop rax | ||
pop rcx | ||
pop rdx | ||
pop rbx | ||
pop rbp | ||
pop rsi | ||
pop rdi | ||
pop r8 | ||
pop r9 | ||
pop r10 | ||
pop r11 | ||
pop r12 | ||
pop r13 | ||
pop r14 | ||
pop r15 | ||
|
||
add rsp, 7 * 8 | ||
mov rcx, [rsp - 5 * 8] // rip | ||
mov r11, [rsp - 3 * 8] // rflags | ||
mov rsp, [rsp - 2 * 8] // user rsp | ||
|
||
swapgs | ||
sysretq |
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,51 @@ | ||
use x86_64::addr::VirtAddr; | ||
use x86_64::registers::model_specific::{Efer, EferFlags, KernelGsBase, LStar, SFMask, Star}; | ||
use x86_64::registers::rflags::RFlags; | ||
use x86_64::structures::tss::TaskStateSegment; | ||
|
||
use super::{GdtStruct, TrapFrame}; | ||
|
||
#[no_mangle] | ||
#[percpu::def_percpu] | ||
static USER_RSP_OFFSET: usize = 0; | ||
|
||
core::arch::global_asm!( | ||
include_str!("syscall.S"), | ||
tss_rsp0_offset = const core::mem::offset_of!(TaskStateSegment, privilege_stack_table), | ||
); | ||
|
||
#[no_mangle] | ||
pub(super) fn x86_syscall_handler(tf: &mut TrapFrame) { | ||
info!( | ||
"syscall {} [{}, {}, {}, {}]", | ||
tf.rax, tf.rdi, tf.rsi, tf.rdx, tf.rdx | ||
); | ||
} | ||
|
||
/// Initializes syscall support and setups the syscall handler. | ||
pub fn init_syscall() { | ||
extern "C" { | ||
fn syscall_entry(); | ||
} | ||
unsafe { | ||
LStar::write(VirtAddr::new(syscall_entry as usize as _)); | ||
Star::write( | ||
GdtStruct::UCODE64_SELECTOR, | ||
GdtStruct::UDATA_SELECTOR, | ||
GdtStruct::KCODE64_SELECTOR, | ||
GdtStruct::KDATA_SELECTOR, | ||
) | ||
.unwrap(); | ||
SFMask::write( | ||
RFlags::TRAP_FLAG | ||
| RFlags::INTERRUPT_FLAG | ||
| RFlags::DIRECTION_FLAG | ||
| RFlags::IOPL_LOW | ||
| RFlags::IOPL_HIGH | ||
| RFlags::NESTED_TASK | ||
| RFlags::ALIGNMENT_CHECK, | ||
); // TF | IF | DF | IOPL | AC | NT (0x47700) | ||
Efer::update(|efer| *efer |= EferFlags::SYSTEM_CALL_EXTENSIONS); | ||
KernelGsBase::write(VirtAddr::new(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
Oops, something went wrong.