proc: add context switching
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
c01b1d7eee
commit
a73fc47664
4 changed files with 46 additions and 17 deletions
|
|
@ -1,6 +1,42 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0 */
|
/* SPDX-License-Identifier: GPL-2.0 */
|
||||||
.intel_syntax noprefix
|
.intel_syntax noprefix
|
||||||
|
|
||||||
|
.macro push_all
|
||||||
|
push r15
|
||||||
|
push r14
|
||||||
|
push r13
|
||||||
|
push r12
|
||||||
|
push r11
|
||||||
|
push r10
|
||||||
|
push r9
|
||||||
|
push r8
|
||||||
|
push rbp
|
||||||
|
push rsi
|
||||||
|
push rdi
|
||||||
|
push rdx
|
||||||
|
push rcx
|
||||||
|
push rbx
|
||||||
|
push rax
|
||||||
|
.endm
|
||||||
|
|
||||||
|
.macro pop_all
|
||||||
|
pop rax
|
||||||
|
pop rbx
|
||||||
|
pop rcx
|
||||||
|
pop rdx
|
||||||
|
pop rdi
|
||||||
|
pop rsi
|
||||||
|
pop rbp
|
||||||
|
pop r8
|
||||||
|
pop r9
|
||||||
|
pop r10
|
||||||
|
pop r11
|
||||||
|
pop r12
|
||||||
|
pop r13
|
||||||
|
pop r14
|
||||||
|
pop r15
|
||||||
|
.endm
|
||||||
|
|
||||||
.global _syscall_enter
|
.global _syscall_enter
|
||||||
_syscall_enter:
|
_syscall_enter:
|
||||||
swapgs
|
swapgs
|
||||||
|
|
@ -9,40 +45,25 @@ _syscall_enter:
|
||||||
/* load kernel stack */
|
/* load kernel stack */
|
||||||
mov rsp, gs:[8]
|
mov rsp, gs:[8]
|
||||||
|
|
||||||
/* save registers */
|
/* save registers into struct pt_regs */
|
||||||
push r15
|
push gs:[16] /* save user RSP */
|
||||||
push r14
|
push_all
|
||||||
push r13
|
|
||||||
push r12
|
|
||||||
push r11
|
|
||||||
push r10
|
|
||||||
push r9
|
|
||||||
push r8
|
|
||||||
push rsi
|
|
||||||
push rdi
|
|
||||||
push rdx
|
|
||||||
push rbx
|
|
||||||
push rax
|
|
||||||
|
|
||||||
|
mov rdi, rsp
|
||||||
call syscall
|
call syscall
|
||||||
|
|
||||||
/* restore registers */
|
/* restore registers */
|
||||||
pop rax
|
pop_all
|
||||||
pop rbx
|
add rsp, 8
|
||||||
pop rdx
|
mov rsp, [rsp] /* load user RSP */
|
||||||
pop rdi
|
|
||||||
pop rsi
|
|
||||||
pop r8
|
|
||||||
pop r9
|
|
||||||
pop r10
|
|
||||||
pop r11
|
|
||||||
pop r12
|
|
||||||
pop r13
|
|
||||||
pop r14
|
|
||||||
pop r15
|
|
||||||
|
|
||||||
/* restore user stack */
|
|
||||||
mov rsp, gs:[16]
|
|
||||||
|
|
||||||
swapgs
|
swapgs
|
||||||
sysretq
|
sysretq
|
||||||
|
|
||||||
|
.global switch_to
|
||||||
|
switch_to:
|
||||||
|
push_all
|
||||||
|
/* 8: struct thread stack_addr offset */
|
||||||
|
mov [rdi + 8], rsp
|
||||||
|
mov rsp, [rsi + 8]
|
||||||
|
pop_all
|
||||||
|
ret
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
|
|
||||||
|
#define IA32_STAR 0xC0000081
|
||||||
|
#define IA32_LSTAR 0xC0000082
|
||||||
|
#define IA32_FMASK 0xC0000084
|
||||||
#define IA32_GS_BASE 0xC0000101
|
#define IA32_GS_BASE 0xC0000101
|
||||||
|
|
||||||
u64 rdmsr(u32 msr);
|
u64 rdmsr(u32 msr);
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,11 @@ struct cpu_local {
|
||||||
struct pt_regs {
|
struct pt_regs {
|
||||||
u64 rax;
|
u64 rax;
|
||||||
u64 rbx;
|
u64 rbx;
|
||||||
|
u64 rcx;
|
||||||
u64 rdx;
|
u64 rdx;
|
||||||
u64 rdi;
|
u64 rdi;
|
||||||
u64 rsi;
|
u64 rsi;
|
||||||
|
u64 rbp;
|
||||||
u64 r8;
|
u64 r8;
|
||||||
u64 r9;
|
u64 r9;
|
||||||
u64 r10;
|
u64 r10;
|
||||||
|
|
@ -26,6 +28,8 @@ struct pt_regs {
|
||||||
u64 r13;
|
u64 r13;
|
||||||
u64 r14;
|
u64 r14;
|
||||||
u64 r15;
|
u64 r15;
|
||||||
|
u64 rsp;
|
||||||
|
u64 rip;
|
||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
#endif /* _ARCH_PROC_H_ */
|
#endif /* _ARCH_PROC_H_ */
|
||||||
|
|
|
||||||
|
|
@ -22,16 +22,17 @@ struct vm_area {
|
||||||
|
|
||||||
struct proc;
|
struct proc;
|
||||||
struct thread {
|
struct thread {
|
||||||
struct proc *proc;
|
|
||||||
tid_t tid;
|
tid_t tid;
|
||||||
|
|
||||||
|
vaddr_t stack_addr;
|
||||||
|
|
||||||
u64 state;
|
u64 state;
|
||||||
bool in_kernel;
|
bool in_kernel;
|
||||||
|
|
||||||
vaddr_t user_stack;
|
|
||||||
vaddr_t kernel_stack;
|
|
||||||
|
|
||||||
u64 tls_base;
|
u64 tls_base;
|
||||||
|
|
||||||
|
struct proc *proc;
|
||||||
struct thread *next;
|
struct thread *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue