Unverified Commit 8a00f5a3 authored by PhoeniX's avatar PhoeniX

Fix syscalls

parent 451007aa
......@@ -281,8 +281,8 @@ void Interrupts::init() {
gdt->ents[0] = GDT_ENT();
gdt->ents[1] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0xA, 0, 1, 1, 0, 1, 0, 1);
gdt->ents[2] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0x2, 0, 1, 1, 0, 0, 1, 1);
gdt->ents[3] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0xA, 3, 1, 1, 0, 1, 0, 1);
gdt->ents[4] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0x2, 3, 1, 1, 0, 0, 1, 1);
gdt->ents[3] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0x2, 3, 1, 1, 0, 0, 1, 1);
gdt->ents[4] = GDT_ENT(0, 0xFFFFFFFFFFFFFFFF, 0xA, 3, 1, 1, 0, 1, 0, 1);
for (uint32_t idx = 0; idx < ncpu; idx++) {
void *stack = Pagetable::alloc();
uintptr_t stack_ptr = uintptr_t(stack) + 0x1000;
......
......@@ -70,7 +70,7 @@ x64_entry:
mov (%rcx,%r9,8), %rsp
# Jump back to SMP initializer
mov %rsp, %rbp
xor %rbp, %rbp
jmpq *%rdx
.align 8
......
......@@ -17,8 +17,12 @@ static void syscall_puts(uintptr_t strptr) {
static void syscall_exit(int code) {
ProcessManager *manager = ProcessManager::getManager();
Process *process = manager->currentProcess();
process->exit(code);
ProcessManager::process_loop();
asm volatile(
"callq _ZN7Process4exitEi;"
"sti;"
"jmp _ZN14ProcessManager12process_loopEv"
::"D"(process), "S"(code)
);
}
#define SYSCALL_ENT(name) { \
......@@ -131,9 +135,8 @@ void Syscall::setup() {
asm volatile(
"mov %%cr3, %%rax; mov %%rax, 2 + _wrapper_mov_cr3(%%rip)":::"%rax"
);
wrmsr(MSR_STAR,
uint64_t(USER_CS) << 48 |
uint64_t(KERNEL_CS) << 32);
wrmsr(MSR_STAR, uint64_t(0x10) << 48 | uint64_t(0x8) << 32);
wrmsr(MSR_LSTAR, uintptr_t(wrapper));
wrmsr(MSR_SFMASK, MSR_SFMASK_IE);
wrmsr(MSR_EFER, rdmsr(MSR_EFER) | MSR_EFER_SCE);
}
static const uint32_t MSR_EFER = 0xC0000080;
static const uint32_t MSR_STAR = 0xC0000081;
static const uint32_t MSR_LSTAR = 0xC0000082;
static const uint32_t MSR_SFMASK = 0xC0000084;
static const uint64_t MSR_SFMASK_IE = 1 << 9;
static const uint64_t MSR_EFER_SCE = 1 << 0;
static const uint16_t KERNEL_CS = 8;
static const uint16_t USER_CS = 24;
static inline void wrmsr(uint32_t msr_id, uint64_t msr_value) {
asm volatile("wrmsr"::"c"(msr_id), "A"(msr_value), "d"(msr_value >> 32));
......
......@@ -145,14 +145,20 @@ uintptr_t Process::linkLibrary(const char* funcname) {
uint64_t syscall_id;
if ((syscall_id = Syscall::callByName(funcname)) != 0) {
struct {
uint8_t sbp[4];
uint8_t pushac11[4];
uint8_t movabs[2];
uint64_t syscall_id;
uint8_t syscall[2];
uint8_t popac11b[5];
uint8_t ret;
} PACKED call = {
{ 0x55, 0x48, 0x89, 0xe5 },
{ 0x50, 0x51, 0x41, 0x53 },
{ 0x48, 0xb8 },
syscall_id,
{ 0x0f, 0x05 },
{ 0x41, 0x5b, 0x59, 0x58, 0x5d },
0xc3
};
ptr = addSection(SectionTypeCode, sizeof(call));
......
......@@ -47,7 +47,6 @@ bool ProcessManager::SwitchProcess(intcb_regs *regs) {
asm volatile("lea process_loop_top(%%rip), %q0":"=r"(looptop));
processSwitchMutex.lock();
if (regs->dpl == 0 &&
cpuThreads[regs->cpuid] == 0 &&
(regs->rip < loopbase || regs->rip >= looptop)) {
processSwitchMutex.release();
return false;
......@@ -81,9 +80,9 @@ bool ProcessManager::SwitchProcess(intcb_regs *regs) {
Thread *th = thread->thread;
*regs = {
regs->cpuid, uintptr_t(thread->process->pagetable),
th->regs.rip, 0x18,
th->regs.rip, 0x20,
th->regs.rflags,
th->regs.rsp, 0x20,
th->regs.rsp, 0x18,
3,
th->regs.rax, th->regs.rcx, th->regs.rdx, th->regs.rbx,
th->regs.rbp, th->regs.rsi, th->regs.rdi,
......@@ -96,6 +95,7 @@ bool ProcessManager::SwitchProcess(intcb_regs *regs) {
bool ProcessManager::HandleFault(
uint32_t intr, uint32_t code, intcb_regs *regs) {
if (regs->dpl == 0) return false;
uint64_t t = EnterCritical();
processSwitchMutex.lock();
QueuedThread *thread = cpuThreads[regs->cpuid];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment