Syscalls working as expected

This commit is contained in:
Alex
2022-11-15 15:09:52 +02:00
parent 5dd496a73f
commit c2628019b7
10 changed files with 61 additions and 53 deletions

View File

@ -79,7 +79,6 @@ extern "C" __attribute__((naked, used, no_stack_protector)) void SystemCallHandl
void InitializeSystemCalls()
{
wrmsr(MSR_EFER, rdmsr(MSR_EFER) | 1);
// TODO: THIS IS NOT WORKING! USER CODE AND USER DATA SHOULD BE CS:0x23 AND SS:0x1B
wrmsr(MSR_STAR, ((uint64_t)(GDT_KERNEL_CODE) << 32) | ((uint64_t)(GDT_KERNEL_DATA | 3) << 48));
wrmsr(MSR_LSTAR, (uint64_t)SystemCallHandlerStub);
wrmsr(MSR_SYSCALL_MASK, (uint64_t)(1 << 9));

View File

@ -7,10 +7,10 @@ SystemCallHandlerStub:
mov [gs:0x8], rsp ; CPUData->TempStack
mov rsp, [gs:0x0] ; CPUData->SystemCallStack
push qword 0x23 ; user data segment
push qword 0x1b ; user data segment
push qword [gs:0x8] ; saved stack
push r11 ; saved rflags
push qword 0x1b ; user code segment
push qword 0x23 ; user code segment
push rcx ; Current RIP
cld

View File

@ -44,20 +44,6 @@ namespace GlobalDescriptorTable
.Flags = {.Raw = 0x0},
.BaseHigh = 0x0},
// user code
{.Length = 0x0,
.BaseLow = 0x0,
.BaseMiddle = 0x0,
.Access = {.A = 0,
.RW = 1,
.DC = 0,
.E = 1,
.S = 1,
.DPL = 3,
.P = 1},
.Flags = {.Unknown = 0x0, .L = 1},
.BaseHigh = 0x0},
// user data
{.Length = 0x0,
.BaseLow = 0x0,
@ -72,6 +58,20 @@ namespace GlobalDescriptorTable
.Flags = {.Raw = 0x0},
.BaseHigh = 0x0},
// user code
{.Length = 0x0,
.BaseLow = 0x0,
.BaseMiddle = 0x0,
.Access = {.A = 0,
.RW = 1,
.DC = 0,
.E = 1,
.S = 1,
.DPL = 3,
.P = 1},
.Flags = {.Unknown = 0x0, .L = 1},
.BaseHigh = 0x0},
// tss
{}};

View File

@ -55,16 +55,10 @@ namespace InterruptDescriptorTable
"pushq %r13\n"
"pushq %r14\n"
"pushq %r15\n"
// push ds segment
"movq %ds, %rax\n"
"pushq %rax\n"
"movq %rsp, %rdi\n"
"call ExceptionHandler\n"
// pop ds segment
"popq %rax\n"
// pop all registers
"popq %r15\n"
"popq %r14\n"
@ -104,16 +98,10 @@ namespace InterruptDescriptorTable
"pushq %r13\n"
"pushq %r14\n"
"pushq %r15\n"
// push ds segment
"movq %ds, %rax\n"
"pushq %rax\n"
"movq %rsp, %rdi\n"
"call MainInterruptHandler\n"
// pop ds segment
"popq %rax\n"
"popq %r15\n"
"popq %r14\n"
"popq %r13\n"

View File

@ -117,8 +117,8 @@ namespace GlobalDescriptorTable
GlobalDescriptorTableEntry Null;
GlobalDescriptorTableEntry Code;
GlobalDescriptorTableEntry Data;
GlobalDescriptorTableEntry UserCode;
GlobalDescriptorTableEntry UserData;
GlobalDescriptorTableEntry UserCode;
TaskStateSegmentEntry TaskStateSegment;
} __attribute__((packed)) GlobalDescriptorTableEntries;