ptos/amd64: ps: Add support for CPL 3 processes

Signed-off-by: Chloe M <chloe@mensia.org>
This commit is contained in:
2026-07-15 01:10:39 -04:00
parent bda2c510cd
commit 85927ce8c1
4 changed files with 30 additions and 7 deletions
+10
View File
@@ -20,8 +20,12 @@ HalContextSwitch(TRAP_FRAME *Frame)
PROCESS *NextParent; PROCESS *NextParent;
TCB *ThisTcb, *NextTcb; TCB *ThisTcb, *NextTcb;
KPCR *Kpcr; KPCR *Kpcr;
MCB *Mcb;
TSS_ENTRY *Tss;
UPTR KStackTop;
Kpcr = HalKpcrCurrent(); Kpcr = HalKpcrCurrent();
Mcb = &Kpcr->Mcb;
ThisThread = Kpcr->CurrentThread; ThisThread = Kpcr->CurrentThread;
NextThread = NULL; NextThread = NULL;
@@ -54,6 +58,12 @@ HalContextSwitch(TRAP_FRAME *Frame)
NextParent = NextThread->Parent; NextParent = NextThread->Parent;
HalMmuWriteVas(&NextParent->Vas); HalMmuWriteVas(&NextParent->Vas);
/* Update the task state segment with kstack */
KStackTop = NextTcb->KStackBase + NextTcb->KStackLength;
Tss = Mcb->Tss;
Tss->Rsp0Low = KStackTop & 0xFFFFFFFF;
Tss->Rsp0High = (KStackTop >> 32) & 0xFFFFFFFF;
/* Context switch here */ /* Context switch here */
Kpcr->CurrentThread = NextThread; Kpcr->CurrentThread = NextThread;
NextTcb = &NextThread->Tcb; NextTcb = &NextThread->Tcb;
+16 -2
View File
@@ -9,6 +9,7 @@
#include <hal/thread.h> #include <hal/thread.h>
#include <hal/page.h> #include <hal/page.h>
#include <ps/thread.h> #include <ps/thread.h>
#include <ps/process.h>
#include <machine/gdt.h> #include <machine/gdt.h>
#include <machine/lapic.h> #include <machine/lapic.h>
#include <mm/vm.h> #include <mm/vm.h>
@@ -44,16 +45,25 @@ static PT_STATUS
ThreadInitTcb(THREAD *Thread, TCB *Tcb, UPTR Ip) ThreadInitTcb(THREAD *Thread, TCB *Tcb, UPTR Ip)
{ {
TRAP_FRAME *Frame; TRAP_FRAME *Frame;
PROCESS *Parent;
if (Thread == NULL || Tcb == NULL) { if (Thread == NULL || Tcb == NULL) {
return STATUS_INVALID_PARAM; return STATUS_INVALID_PARAM;
} }
Parent = Thread->Parent;
Thread->StackBase = (UPTR)ThreadAllocKStack(); Thread->StackBase = (UPTR)ThreadAllocKStack();
if (Thread->StackBase == 0) { if (Thread->StackBase == 0) {
return STATUS_NO_MEMORY; return STATUS_NO_MEMORY;
} }
Tcb->KStackLength = PAGESIZE;
Tcb->KStackBase = (UPTR)ThreadAllocKStack();
if (Tcb->KStackBase == 0) {
/* TODO: Free Thread->StackBase */
return STATUS_NO_MEMORY;
}
/* Zero the whole frame */ /* Zero the whole frame */
Frame = &Tcb->FrameSnapshot; Frame = &Tcb->FrameSnapshot;
RtlMemSet(Frame, 0, sizeof(*Frame)); RtlMemSet(Frame, 0, sizeof(*Frame));
@@ -62,8 +72,12 @@ ThreadInitTcb(THREAD *Thread, TCB *Tcb, UPTR Ip)
Frame->Rflags = 0x202; Frame->Rflags = 0x202;
Frame->Rsp = Thread->StackBase + PAGESIZE; Frame->Rsp = Thread->StackBase + PAGESIZE;
Frame->Rip = Ip; Frame->Rip = Ip;
Frame->CodeSeg = GDT_KCODE; Frame->CodeSeg = ISSET(Parent->Flags, PS_USER)
Frame->StackSeg = GDT_KDATA; ? GDT_UCODE | 3
: GDT_KCODE;
Frame->StackSeg = ISSET(Parent->Flags, PS_USER)
? GDT_UDATA | 3
: GDT_KDATA;
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
+4
View File
@@ -17,9 +17,13 @@
* information. * information.
* *
* @FrameSnapshot: Processor state snapshot * @FrameSnapshot: Processor state snapshot
* @KStackBase: TSS RSP0 base
* @KStackLength: TSS RSP0 length
*/ */
typedef struct { typedef struct {
TRAP_FRAME FrameSnapshot; TRAP_FRAME FrameSnapshot;
UPTR KStackBase;
USIZE KStackLength;
} TCB; } TCB;
#endif /* !_MACHINE_TCB_H_ */ #endif /* !_MACHINE_TCB_H_ */
-5
View File
@@ -25,11 +25,6 @@ PsCreateProcess(const CHAR *Name, ULONG Flags, PROCESS **Result)
return STATUS_INVALID_PARAM; return STATUS_INVALID_PARAM;
} }
/* TODO: Support user-level processes */
if (ISSET(Flags, PS_USER)) {
return STATUS_NOT_SUPPORTED;
}
NameLen = RtlStrLen(Name); NameLen = RtlStrLen(Name);
if (NameLen >= PS_NAME_MAX - 1) { if (NameLen >= PS_NAME_MAX - 1) {
return STATUS_NAME_TOO_LONG; return STATUS_NAME_TOO_LONG;