amd64: add syscall entry mechanism
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
a73fc47664
commit
d7477ae860
8 changed files with 103 additions and 87 deletions
|
|
@ -8,4 +8,3 @@ obj-y += exception.o
|
||||||
obj-y += gdt.o
|
obj-y += gdt.o
|
||||||
obj-y += idt.o
|
obj-y += idt.o
|
||||||
obj-y += syscall.o
|
obj-y += syscall.o
|
||||||
obj-y += trap.o
|
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,13 @@ void arch_early_init()
|
||||||
|
|
||||||
void arch_init()
|
void arch_init()
|
||||||
{
|
{
|
||||||
|
void _syscall_enter();
|
||||||
|
|
||||||
gdt_init();
|
gdt_init();
|
||||||
idt_init();
|
idt_init();
|
||||||
|
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
|
||||||
|
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
|
||||||
|
wrmsr(IA32_FMASK, RFLAGS_IF);
|
||||||
}
|
}
|
||||||
|
|
||||||
void arch_ap_startup()
|
void arch_ap_startup()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0 */
|
/* SPDX-License-Identifier: GPL-2.0 */
|
||||||
.intel_syntax noprefix
|
.intel_syntax noprefix
|
||||||
|
|
||||||
|
#define THREAD_RSP_OFF 8
|
||||||
|
|
||||||
.macro push_all
|
.macro push_all
|
||||||
push r15
|
push r15
|
||||||
|
|
@ -37,9 +39,40 @@
|
||||||
pop r15
|
pop r15
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
|
.macro gen_irq_entry name
|
||||||
|
.global \name
|
||||||
|
\name:
|
||||||
|
test qword ptr [rsp + 8], 3
|
||||||
|
jz 1f
|
||||||
|
swapgs
|
||||||
|
1:
|
||||||
|
.endm
|
||||||
|
|
||||||
|
.macro exception nr
|
||||||
|
gen_irq_entry _exception_\nr
|
||||||
|
mov rdi, \nr
|
||||||
|
call exception_handler
|
||||||
|
iretq
|
||||||
|
.endm
|
||||||
|
|
||||||
|
.macro exception_err nr
|
||||||
|
gen_irq_entry _exception_\nr
|
||||||
|
mov rdi, \nr
|
||||||
|
pop rsi
|
||||||
|
iretq
|
||||||
|
.endm
|
||||||
|
|
||||||
|
.macro irq nr
|
||||||
|
gen_irq_entry _irq_\nr
|
||||||
|
mov rdi, \nr
|
||||||
|
call irq_handler
|
||||||
|
iretq
|
||||||
|
.endm
|
||||||
|
|
||||||
.global _syscall_enter
|
.global _syscall_enter
|
||||||
_syscall_enter:
|
_syscall_enter:
|
||||||
swapgs
|
swapgs
|
||||||
|
|
||||||
/* save user stack */
|
/* save user stack */
|
||||||
mov gs:[16], rsp
|
mov gs:[16], rsp
|
||||||
/* load kernel stack */
|
/* load kernel stack */
|
||||||
|
|
@ -62,8 +95,58 @@ _syscall_enter:
|
||||||
.global switch_to
|
.global switch_to
|
||||||
switch_to:
|
switch_to:
|
||||||
push_all
|
push_all
|
||||||
/* 8: struct thread stack_addr offset */
|
mov [rdi + THREAD_RSP_OFF], rsp
|
||||||
mov [rdi + 8], rsp
|
mov rsp, [rsi + THREAD_RSP_OFF]
|
||||||
mov rsp, [rsi + 8]
|
|
||||||
pop_all
|
pop_all
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
/* exceptions */
|
||||||
|
exception 0x00
|
||||||
|
exception 0x01
|
||||||
|
exception 0x02
|
||||||
|
exception 0x03
|
||||||
|
exception 0x04
|
||||||
|
exception 0x05
|
||||||
|
exception 0x06
|
||||||
|
exception 0x07
|
||||||
|
exception_err 0x08
|
||||||
|
exception 0x09
|
||||||
|
exception_err 0x0A
|
||||||
|
exception_err 0x0B
|
||||||
|
exception_err 0x0C
|
||||||
|
exception_err 0x0D
|
||||||
|
exception_err 0x0E
|
||||||
|
exception 0x0F
|
||||||
|
exception 0x10
|
||||||
|
exception_err 0x11
|
||||||
|
exception 0x12
|
||||||
|
exception 0x13
|
||||||
|
exception 0x14
|
||||||
|
exception_err 0x15
|
||||||
|
exception 0x16
|
||||||
|
exception 0x17
|
||||||
|
exception 0x18
|
||||||
|
exception 0x19
|
||||||
|
exception 0x1A
|
||||||
|
exception 0x1B
|
||||||
|
exception 0x1C
|
||||||
|
exception_err 0x1D
|
||||||
|
exception_err 0x1E
|
||||||
|
exception 0x1F
|
||||||
|
|
||||||
|
irq 0x00
|
||||||
|
irq 0x01
|
||||||
|
irq 0x02
|
||||||
|
irq 0x03
|
||||||
|
irq 0x04
|
||||||
|
irq 0x05
|
||||||
|
irq 0x06
|
||||||
|
irq 0x07
|
||||||
|
irq 0x08
|
||||||
|
irq 0x09
|
||||||
|
irq 0x0A
|
||||||
|
irq 0x0B
|
||||||
|
irq 0x0C
|
||||||
|
irq 0x0D
|
||||||
|
irq 0x0E
|
||||||
|
irq 0x0F
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,12 @@
|
||||||
#ifndef _ARCH_PROC_H_
|
#ifndef _ARCH_PROC_H_
|
||||||
#define _ARCH_PROC_H_
|
#define _ARCH_PROC_H_
|
||||||
|
|
||||||
#include <kernel/proc.h>
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/types.h>
|
||||||
|
|
||||||
struct proc;
|
struct thread;
|
||||||
struct cpu_local {
|
struct cpu_local {
|
||||||
struct proc *current_proc;
|
struct thread *cur_thread;
|
||||||
vaddr_t kernel_rsp;
|
vaddr_t kernel_rsp;
|
||||||
vaddr_t user_rsp;
|
vaddr_t user_rsp;
|
||||||
u32 cpu_id;
|
u32 cpu_id;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
#include <kernel/syscall.h>
|
#include <kernel/syscall.h>
|
||||||
#include <arch/proc.h>
|
#include <arch/proc.h>
|
||||||
|
|
||||||
i64 syscall(struct pt_regs *regs)
|
static inline i64 _do_syscall(struct pt_regs *regs)
|
||||||
{
|
{
|
||||||
syscall_t c = get_syscall(regs->rax);
|
syscall_t c = get_syscall(regs->rax);
|
||||||
if (!c)
|
if (!c)
|
||||||
|
|
@ -10,3 +10,8 @@ i64 syscall(struct pt_regs *regs)
|
||||||
|
|
||||||
return c(regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
return c(regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void syscall(struct pt_regs *regs)
|
||||||
|
{
|
||||||
|
regs->rax = _do_syscall(regs);
|
||||||
|
}
|
||||||
|
|
|
||||||
76
amd64/trap.S
76
amd64/trap.S
|
|
@ -1,76 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
.intel_syntax noprefix
|
|
||||||
|
|
||||||
.macro exception nr
|
|
||||||
.global _exception_\nr
|
|
||||||
_exception_\nr:
|
|
||||||
mov rdi, \nr
|
|
||||||
call exception_handler
|
|
||||||
iretq
|
|
||||||
.endm
|
|
||||||
|
|
||||||
.macro exception_err nr
|
|
||||||
.global _exception_\nr
|
|
||||||
_exception_\nr:
|
|
||||||
mov rdi, \nr
|
|
||||||
pop rsi
|
|
||||||
iretq
|
|
||||||
.endm
|
|
||||||
|
|
||||||
.macro irq nr
|
|
||||||
.global _irq_\nr
|
|
||||||
_irq_\nr:
|
|
||||||
mov rdi, \nr
|
|
||||||
call irq_handler
|
|
||||||
iretq
|
|
||||||
.endm
|
|
||||||
|
|
||||||
exception 0x00
|
|
||||||
exception 0x01
|
|
||||||
exception 0x02
|
|
||||||
exception 0x03
|
|
||||||
exception 0x04
|
|
||||||
exception 0x05
|
|
||||||
exception 0x06
|
|
||||||
exception 0x07
|
|
||||||
exception_err 0x08
|
|
||||||
exception 0x09
|
|
||||||
exception_err 0x0A
|
|
||||||
exception_err 0x0B
|
|
||||||
exception_err 0x0C
|
|
||||||
exception_err 0x0D
|
|
||||||
exception_err 0x0E
|
|
||||||
exception 0x0F
|
|
||||||
exception 0x10
|
|
||||||
exception_err 0x11
|
|
||||||
exception 0x12
|
|
||||||
exception 0x13
|
|
||||||
exception 0x14
|
|
||||||
exception_err 0x15
|
|
||||||
exception 0x16
|
|
||||||
exception 0x17
|
|
||||||
exception 0x18
|
|
||||||
exception 0x19
|
|
||||||
exception 0x1A
|
|
||||||
exception 0x1B
|
|
||||||
exception 0x1C
|
|
||||||
exception_err 0x1D
|
|
||||||
exception_err 0x1E
|
|
||||||
exception 0x1F
|
|
||||||
|
|
||||||
irq 0x00
|
|
||||||
irq 0x01
|
|
||||||
irq 0x02
|
|
||||||
irq 0x03
|
|
||||||
irq 0x04
|
|
||||||
irq 0x05
|
|
||||||
irq 0x06
|
|
||||||
irq 0x07
|
|
||||||
irq 0x08
|
|
||||||
irq 0x09
|
|
||||||
irq 0x0A
|
|
||||||
irq 0x0B
|
|
||||||
irq 0x0C
|
|
||||||
irq 0x0D
|
|
||||||
irq 0x0E
|
|
||||||
irq 0x0F
|
|
||||||
|
|
@ -29,7 +29,6 @@ struct thread {
|
||||||
u64 state;
|
u64 state;
|
||||||
bool in_kernel;
|
bool in_kernel;
|
||||||
|
|
||||||
|
|
||||||
u64 tls_base;
|
u64 tls_base;
|
||||||
|
|
||||||
struct proc *proc;
|
struct proc *proc;
|
||||||
|
|
@ -49,5 +48,6 @@ struct proc {
|
||||||
};
|
};
|
||||||
|
|
||||||
void proc_init();
|
void proc_init();
|
||||||
|
void switch_to(struct thread *from, struct thread *to);
|
||||||
|
|
||||||
#endif /* _PROC_H_ */
|
#endif /* _PROC_H_ */
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include <kernel/proc.h>
|
#include <kernel/proc.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
mem_cache_t *proc_slab;
|
mem_cache_t *proc_slab;
|
||||||
mem_cache_t *thread_slab;
|
mem_cache_t *thread_slab;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue