Skip to content

Commit

Permalink
sysenter: set kernel stack on enter_usermode
Browse files Browse the repository at this point in the history
Currently the same stack would be used by all threads, which is a recipe
for disaster. Instead, set the kernel stack for sysenter when entering a
usermode task. Because ktf does not do context switching, this msr write
only happens once.

Signed-off-by: Johannes Wikner <[email protected]>
  • Loading branch information
sktt committed Aug 28, 2023
1 parent f3e9d02 commit 9dbf6c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions arch/x86/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
SET_CR3 user_cr3
.endm

.macro WRITE_MSR msr_reg value
mov \msr_reg, %_ASM_CX
mov \value, %_ASM_AX
mov \value, %_ASM_DX
shr $32, %_ASM_DX
wrmsr
.endm

.macro exception_handler sym vec has_error_code
ENTRY(entry_\sym)
enter_from_usermode
Expand Down Expand Up @@ -173,9 +181,15 @@ ENTRY(enter_usermode)
PUSHF

/* Save stack pointer onto per-cpu */
mov %_ASM_DX, %gs:(usermode_private)
mov %_ASM_SP, %gs:usermode_private

syscall_to_usermode
/* provide sysenter the kernel stack to be used */
push %_ASM_DX
WRITE_MSR $MSR_SYSENTER_ESP, %_ASM_SP
pop %_ASM_DX

/* Move to user stack */
mov %_ASM_DX, %_ASM_SP

/* SS + SP */
push $__USER_DS
Expand All @@ -189,6 +203,8 @@ ENTRY(enter_usermode)
/* CS + IP */
push $__USER_CS
push $usermode_stub

enter_to_usermode
IRET
END_FUNC(enter_usermode)

Expand Down
2 changes: 1 addition & 1 deletion common/usermode.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static void init_syscall(void) {

static void init_sysenter(void) {
wrmsr(MSR_SYSENTER_CS, _ul(__KERN_CS));
wrmsr(MSR_SYSENTER_ESP, _ul(get_free_page_top(GFP_KERNEL | GFP_USER)));
wrmsr(MSR_SYSENTER_ESP, _ul(0)); /* configure the stack on enter_usermode instead */
wrmsr(MSR_SYSENTER_EIP, _ul(&sysenter_handler_entry));
}

Expand Down

0 comments on commit 9dbf6c3

Please sign in to comment.