1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

Rewritten GDT descriptors

This commit is contained in:
Adrien Bourmault 2021-03-01 16:07:39 +01:00
parent 3326b4a18b
commit 3c1e97c157
Signed by: neox
GPG Key ID: 6EB408FE0ACEC664
6 changed files with 43 additions and 34 deletions

View File

@ -32,6 +32,7 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#define IOMAP_SIZE (8 * 1024) #define IOMAP_SIZE (8 * 1024)
#define TSS_TYPE 0x9
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
@ -92,6 +93,24 @@ struct GdtPtr_t
ulong base; // address of the first entry ulong base; // address of the first entry
} __attribute__((__packed__)); } __attribute__((__packed__));
enum // For GdtEntry_t.accessed
{
ACCESSEDSEG = 1 << 0,
READABLE = 1 << 1,
CONFORMING = 1 << 2,
RESERVED = 1 << 3,
RESERVED2 = 1 << 4,
USER = 1 << 5,
USER2 = 1 << 6,
PRESENTSEG = 1 << 7
};
enum // For GdtEntry_t.flags
{
LONG = 1 << 5,
LEGACY = 1 << 6,
};
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// //

View File

@ -113,5 +113,7 @@ void BtDoSanityChecks(uint mbMagic) {
DebugLog("Kernel successfully loaded at %p\n", DebugLog("Kernel successfully loaded at %p\n",
BtLoaderInfo.kernelAddr); BtLoaderInfo.kernelAddr);
DebugLog("Code segment : %#x\n",
BtLoaderInfo.codeSegment);
} }

View File

@ -39,17 +39,12 @@ KeJumpToUserspace:
; rsi = entry point in user space ; rsi = entry point in user space
; rdx = user space stack ; rdx = user space stack
mov rax, 0x18 ; Selector 0x18 (User Data) + RPL 3
mov ds, rax
mov es, rax
; Build a fake iret frame ; Build a fake iret frame
push rax ; Selector 0x18 (User Data) + RPL 3 push QWORD 0x10 ; Selector 0x20 (User Data) + RPL 3
push rdx ; User space stack push rdx ; User space stack
push QWORD 0x202 ; rflags = interrupt enable + reserved bit push QWORD 0x202 ; rflags = interrupt enable + reserved bit
push QWORD 0x20 ; Selector 0x20 (User Code) + RPL 3 push QWORD 0x08 ; Selector 0x18 (User Code) + RPL 3
push rsi ; Entry point in user space push rsi ; Entry point in user space
iretq iretq

View File

@ -43,27 +43,20 @@ void MmInitGdt(void)
memzero((void *)&tss, sizeof(tss)); memzero((void *)&tss, sizeof(tss));
// Kernel codeseg // Kernel codeseg
gdt[1].lowLimit = 0x0; gdt[1].access = PRESENTSEG | RESERVED | RESERVED2;
gdt[1].access = 0x9A; gdt[1].flags = LONG;
gdt[1].flags = 0x20;
// Kernel dataseg // Kernel dataseg
gdt[2].lowLimit = 0x0; gdt[2].access = PRESENTSEG;
gdt[2].access = 0x92;
gdt[2].flags = 0x00;
// User dataseg
gdt[3].lowLimit = 0x0;
gdt[3].access = 0xF2;
gdt[3].flags = 0x20;
// User codeseg // User codeseg
gdt[4].lowLimit = 0x0; gdt[3].access = PRESENTSEG | RESERVED | RESERVED2 | USER | USER2;
gdt[4].access = 0xFA; gdt[3].flags = LONG;
gdt[4].flags = 0x20;
tssDesc.access = 0x89; // User dataseg
tssDesc.flags = 0x40; gdt[4].access = PRESENTSEG | USER | USER2;
tssDesc.access = TSS_TYPE | PRESENTSEG;
tssDesc.lowBase = (ulong)&tss & 0xFFFF; tssDesc.lowBase = (ulong)&tss & 0xFFFF;
tssDesc.middleBase = ((ulong)&tss >> 16) & 0xFF; tssDesc.middleBase = ((ulong)&tss >> 16) & 0xFF;
tssDesc.highBase = ((ulong)&tss >> 24) & 0xFF; tssDesc.highBase = ((ulong)&tss >> 24) & 0xFF;
@ -82,9 +75,9 @@ void MmInitGdt(void)
DebugLog("Null descriptor : %#p\n", &gdt[0]); DebugLog("Null descriptor : %#p\n", &gdt[0]);
DebugLog("Kernel code descriptor : %#p\n", &gdt[1]); DebugLog("Kernel code descriptor : %#p\n", &gdt[1]);
DebugLog("Kernel data descriptor : %#p\n", &gdt[2]); DebugLog("Kernel data descriptor : %#p\n", &gdt[2]);
DebugLog("User data descriptor : %#p\n", &gdt[3]); DebugLog("User code descriptor : %#p\n", &gdt[3]);
DebugLog("User code descriptor : %#p\n", &gdt[4]); DebugLog("User data descriptor : %#p\n", &gdt[4]);
DebugLog("tss : %#p\n", &gdt[5]); DebugLog("Task-switch descriptor : %#p\n", &gdt[5]);
DebugLog("ist1 : %#p\n", tss.ist1); DebugLog("ist1 : %#p\n", tss.ist1);
DebugLog("ist2 : %#p\n", tss.ist2); DebugLog("ist2 : %#p\n", tss.ist2);
DebugLog("ist3 : %#p\n", tss.ist3); DebugLog("ist3 : %#p\n", tss.ist3);

View File

@ -331,7 +331,7 @@ error_t CmdSyscallTest(int argc, char **argv, char *cmdline)
error_t CmdRing3Test(int argc, char **argv, char *cmdline) error_t CmdRing3Test(int argc, char **argv, char *cmdline)
{ {
size_t size = 1*KB; size_t size = 1*KPAGESIZE;
void *entryPoint = (void*)USERSPACE; void *entryPoint = (void*)USERSPACE;
ulong flags = PRESENT | READWRITE | USERMODE; ulong flags = PRESENT | READWRITE | USERMODE;
@ -348,7 +348,7 @@ error_t CmdRing3Test(int argc, char **argv, char *cmdline)
return ENOMEM; return ENOMEM;
} }
size = 1*KB; size = 1*KPAGESIZE;
void *stackAddr = (void*)USERSPACE + 4*KPAGESIZE; void *stackAddr = (void*)USERSPACE + 4*KPAGESIZE;
flags = PRESENT | READWRITE | USERMODE; flags = PRESENT | READWRITE | USERMODE;
@ -365,15 +365,15 @@ error_t CmdRing3Test(int argc, char **argv, char *cmdline)
return ENOMEM; return ENOMEM;
} }
KernLog("Entrypoint : %p, stack address : %p\n", entryPoint, stackAddr); KernLog("Entrypoint : %p, stack address : %p\n", entryPoint, stackAddr+size);
uchar *code = (uchar*)entryPoint; uchar *code = (uchar*)entryPoint;
/* *code = 0x90; // nop*/ *code = 0x90; // nop
/* *(code+1) = 0xEB; // jmp*/ *(code+1) = 0xEB; // jmp
/* *(code+2) = 0xFD; // short 0x0*/ *(code+2) = 0xFD; // short 0x0
KeJumpToUserspace(0, entryPoint, stackAddr); KeJumpToUserspace(0, entryPoint, stackAddr+size);
return EOK; return EOK;
} }