Move syscall handler to SystemCalls.cpp

This commit is contained in:
Alex 2023-04-04 03:25:25 +03:00
parent d2c241fcb6
commit f81922f63d
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 45 additions and 129 deletions

View File

@ -29,68 +29,58 @@ extern "C" uint64_t SystemCallsHandler(SyscallsFrame *regs);
extern "C" void SystemCallHandlerStub(); extern "C" void SystemCallHandlerStub();
extern "C" __attribute__((naked, used, no_stack_protector)) void SystemCallHandlerStub_broken() extern "C" __naked __used __no_stack_protector void SystemCallHandlerStub()
{ {
// asmv( asmv("swapgs\n"
// // "cmp $0x08, 0x8(%rsp)\n"
// // "je 1f\n"
// "swapgs\n"
// // "1:\n"
// "mov %rsp, 0x8(%gs)\n" // CPUData->TempStack "mov %rsp, %gs:0x8\n" // CPUData->TempStack
// "mov 0x0(%gs), %rsp\n" // CPUData->SystemCallStack "mov %gs:0x0, %rsp\n" // CPUData->SystemCallStack
// "push $0x1b\n" // user data segment "push $0x1b\n" // user data segment
// "push 0x8(%gs)\n" // saved stack "push %gs:0x8\n" // saved stack
// "push %r11\n" // saved rflags "push %r11\n" // saved rflags
// "push $0x23\n" // user code segment "push $0x23\n" // user code segment
// "push %rcx\n" // Current RIP "push %rcx\n" // Current RIP
// "push %rax\n" "push %rax\n"
// "push %rbx\n" "push %rbx\n"
// "push %rcx\n" "push %rcx\n"
// "push %rdx\n" "push %rdx\n"
// "push %rsi\n" "push %rsi\n"
// "push %rdi\n" "push %rdi\n"
// "push %rbp\n" "push %rbp\n"
// "push %r8\n" "push %r8\n"
// "push %r9\n" "push %r9\n"
// "push %r10\n" "push %r10\n"
// "push %r11\n" "push %r11\n"
// "push %r12\n" "push %r12\n"
// "push %r13\n" "push %r13\n"
// "push %r14\n" "push %r14\n"
// "push %r15\n" "push %r15\n"
// "mov %rsp, %rdi\n" "mov %rsp, %rdi\n"
// "mov $0, %rbp\n" "mov $0, %rbp\n"
// "call SystemCallsHandler\n" "call SystemCallsHandler\n"
// "pop %r15\n" "pop %r15\n"
// "pop %r14\n" "pop %r14\n"
// "pop %r13\n" "pop %r13\n"
// "pop %r12\n" "pop %r12\n"
// "pop %r11\n" "pop %r11\n"
// "pop %r10\n" "pop %r10\n"
// "pop %r9\n" "pop %r9\n"
// "pop %r8\n" "pop %r8\n"
// "pop %rbp\n" "pop %rbp\n"
// "pop %rdi\n" "pop %rdi\n"
// "pop %rsi\n" "pop %rsi\n"
// "pop %rdx\n" "pop %rdx\n"
// "pop %rcx\n" "pop %rcx\n"
// "pop %rbx\n" "pop %rbx\n"
// /* "pop %rax\n" */
// "mov 0x8(%gs), %rsp\n" // CPUData->TempStack "mov %gs:0x8, %rsp\n" // CPUData->TempStack
// // "cmp $0x08, 0x8(%rsp)\n" "swapgs\n"
// // "je 1f\n" "sti\n"
// "swapgs\n" "sysretq\n");
// // "1:\n"
// "sti\n"
// "sysretq\n");
} }
void InitializeSystemCalls() void InitializeSystemCalls()

View File

@ -1,74 +0,0 @@
; This file is part of Fennix Kernel.
;
; Fennix Kernel is free software: you can redistribute it and/or
; modify it under the terms of the GNU General Public License as
; published by the Free Software Foundation, either version 3 of
; the License, or (at your option) any later version.
;
; Fennix Kernel is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
[BITS 64]
%macro PushAllSC 0
push rax
push rbx
push rcx
push rdx
push rsi
push rdi
push rbp
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
%endmacro
%macro PopAllSC 0
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rbp
pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
%endmacro
ALIGN 4096
extern SystemCallsHandler
global SystemCallHandlerStub
SystemCallHandlerStub:
swapgs ; Swap gs and kernelgs
mov [gs:0x8], rsp ; CPUData->TempStack
mov rsp, [gs:0x0] ; CPUData->SystemCallStack
push qword 0x1b ; User data segment
push qword [gs:0x8] ; Saved stack
push r11 ; Saved rflags
push qword 0x23 ; User code segment
push rcx ; Current instruction pointer
cld ; Clear direction flag
PushAllSC ; Push all registers
mov rdi, rsp ; Pass pointer to registers
mov rbp, 0 ; Pass 0 as return address
call SystemCallsHandler ; Call system call handler
PopAllSC ; Pop all registers except rax
mov rsp, [gs:0x8] ; Restore stack
swapgs ; Swap back gs and kernelgs
sti ; Enable interrupts
o64 sysret ; Return to user mode