ptos: ps+hal: Add thread control block init
Signed-off-by: Chloe M <chloe@mensia.org>
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: MD thread management
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <hal/thread.h>
|
||||||
|
#include <hal/page.h>
|
||||||
|
#include <ps/thread.h>
|
||||||
|
#include <machine/gdt.h>
|
||||||
|
#include <mm/vm.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Allocate a kernel stack for a thread
|
||||||
|
*/
|
||||||
|
static VOID *
|
||||||
|
ThreadAllocKStack(VOID)
|
||||||
|
{
|
||||||
|
MM_RANGE StackRange;
|
||||||
|
PT_STATUS Status;
|
||||||
|
|
||||||
|
Status = MmAllocatePages(1, &StackRange);
|
||||||
|
if (PT_ERROR(Status)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RANGE_PTR(&StackRange);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize a thread control block
|
||||||
|
*
|
||||||
|
* @Thread: Thread to initialize TCB of
|
||||||
|
* @Tcb: Thread control block to initialize
|
||||||
|
* @Ip: Instruction pointer value to start off with
|
||||||
|
*
|
||||||
|
* TODO: Support user threads
|
||||||
|
*/
|
||||||
|
static PT_STATUS
|
||||||
|
ThreadInitTcb(THREAD *Thread, TCB *Tcb, UPTR Ip)
|
||||||
|
{
|
||||||
|
TRAP_FRAME *Frame;
|
||||||
|
|
||||||
|
if (Thread == NULL || Tcb == NULL) {
|
||||||
|
return STATUS_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread->StackBase = (UPTR)ThreadAllocKStack();
|
||||||
|
if (Thread->StackBase == 0) {
|
||||||
|
return STATUS_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Zero the whole frame */
|
||||||
|
Frame = &Tcb->FrameSnapshot;
|
||||||
|
RtlMemSet(Frame, 0, sizeof(*Frame));
|
||||||
|
|
||||||
|
/* Initialize only whats needed */
|
||||||
|
Frame->Rflags = 0x202;
|
||||||
|
Frame->Rsp = Thread->StackBase + PAGESIZE;
|
||||||
|
Frame->Rip = Ip;
|
||||||
|
Frame->CodeSeg = GDT_KCODE;
|
||||||
|
Frame->StackSeg = GDT_KDATA;
|
||||||
|
return STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
PT_STATUS
|
||||||
|
HalThreadInit(THREAD *Thread, ULONG Flags, UPTR Ip)
|
||||||
|
{
|
||||||
|
TCB *Tcb;
|
||||||
|
|
||||||
|
if (Thread == NULL) {
|
||||||
|
return STATUS_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tcb = &Thread->Tcb;
|
||||||
|
ThreadInitTcb(Thread, Tcb, Ip);
|
||||||
|
return STATUS_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: Thread control block
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MACHINE_TCB_H_
|
||||||
|
#define _MACHINE_TCB_H_ 1
|
||||||
|
|
||||||
|
#include <ptdef.h>
|
||||||
|
#include <machine/frame.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The thread control block contains machine specific thread
|
||||||
|
* information.
|
||||||
|
*
|
||||||
|
* @FrameSnapshot: Processor state snapshot
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
TRAP_FRAME FrameSnapshot;
|
||||||
|
} TCB;
|
||||||
|
|
||||||
|
#endif /* !_MACHINE_TCB_H_ */
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: Thread control block bridge
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _HAL_TCB_H_
|
||||||
|
#define _HAL_TCB_H_ 1
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This header must define the TCB structure for MD
|
||||||
|
* thread information.
|
||||||
|
*/
|
||||||
|
#include <machine/tcb.h>
|
||||||
|
|
||||||
|
#endif /* !_HAL_TCB_H_ */
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: HAL thread management
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _HAL_THREAD_H_
|
||||||
|
#define _HAL_THREAD_H_ 1
|
||||||
|
|
||||||
|
#include <ps/thread.h>
|
||||||
|
#include <ptapi/status.h>
|
||||||
|
#include <ptdef.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize machine specific thread information
|
||||||
|
*
|
||||||
|
* @Thread: Thread to initialize
|
||||||
|
* @Flags: Optional init flags
|
||||||
|
* @Ip: Instruction pointer value to start off with
|
||||||
|
*/
|
||||||
|
PT_STATUS HalThreadInit(THREAD *Thread, ULONG Flags, UPTR Ip);
|
||||||
|
|
||||||
|
#endif /* !_HAL_THREAD_H_ */
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#define _PS_THREAD_H_ 1
|
#define _PS_THREAD_H_ 1
|
||||||
|
|
||||||
#include <ptapi/status.h>
|
#include <ptapi/status.h>
|
||||||
|
#include <hal/tcb.h>
|
||||||
#include <ptdef.h>
|
#include <ptdef.h>
|
||||||
#include <queue.h>
|
#include <queue.h>
|
||||||
|
|
||||||
@@ -22,11 +23,15 @@ struct _PROCESS;
|
|||||||
* processes.
|
* processes.
|
||||||
*
|
*
|
||||||
* @Tid: Thread ID
|
* @Tid: Thread ID
|
||||||
|
* @Tcb: Thread control block
|
||||||
|
* @StackBase: Execution stack base
|
||||||
* @Parent: Parent process
|
* @Parent: Parent process
|
||||||
* @ThreadLink: Thread queue link
|
* @ThreadLink: Thread queue link
|
||||||
*/
|
*/
|
||||||
typedef struct _THREAD {
|
typedef struct _THREAD {
|
||||||
UQUAD Tid;
|
UQUAD Tid;
|
||||||
|
TCB Tcb;
|
||||||
|
UPTR StackBase;
|
||||||
struct _PROCESS *Parent;
|
struct _PROCESS *Parent;
|
||||||
TAILQ_ENTRY(_THREAD) ThreadLink;
|
TAILQ_ENTRY(_THREAD) ThreadLink;
|
||||||
} THREAD;
|
} THREAD;
|
||||||
@@ -36,8 +41,12 @@ typedef struct _THREAD {
|
|||||||
*
|
*
|
||||||
* @Parent: Parent of thread to create
|
* @Parent: Parent of thread to create
|
||||||
* @Flags: Optional init flags
|
* @Flags: Optional init flags
|
||||||
|
* @Ip: Instruction pointer value to start off with
|
||||||
* @Result: Result is written here
|
* @Result: Result is written here
|
||||||
*/
|
*/
|
||||||
PT_STATUS PsCreateThread(struct _PROCESS *Parent, ULONG Flags, THREAD **Result);
|
PT_STATUS PsCreateThread(
|
||||||
|
struct _PROCESS *Parent, ULONG Flags,
|
||||||
|
UPTR Ip, THREAD **Result
|
||||||
|
);
|
||||||
|
|
||||||
#endif /* !_PS_THREAD_H_ */
|
#endif /* !_PS_THREAD_H_ */
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include <ps/thread.h>
|
#include <ps/thread.h>
|
||||||
#include <ps/process.h>
|
#include <ps/process.h>
|
||||||
|
#include <hal/thread.h>
|
||||||
#include <ex/pool.h>
|
#include <ex/pool.h>
|
||||||
#include <atomic.h>
|
#include <atomic.h>
|
||||||
|
|
||||||
@@ -15,9 +16,10 @@
|
|||||||
static UQUAD NextThreadTid = 0;
|
static UQUAD NextThreadTid = 0;
|
||||||
|
|
||||||
PT_STATUS
|
PT_STATUS
|
||||||
PsCreateThread(struct _PROCESS *Parent, ULONG Flags, THREAD **Result)
|
PsCreateThread(struct _PROCESS *Parent, ULONG Flags, UPTR Ip, THREAD **Result)
|
||||||
{
|
{
|
||||||
THREAD *Thread;
|
THREAD *Thread;
|
||||||
|
PT_STATUS Status;
|
||||||
|
|
||||||
if (Parent == NULL || Result == NULL) {
|
if (Parent == NULL || Result == NULL) {
|
||||||
return STATUS_INVALID_PARAM;
|
return STATUS_INVALID_PARAM;
|
||||||
@@ -35,6 +37,12 @@ PsCreateThread(struct _PROCESS *Parent, ULONG Flags, THREAD **Result)
|
|||||||
|
|
||||||
Thread->Tid = AtomicIncQuad(&NextThreadTid);
|
Thread->Tid = AtomicIncQuad(&NextThreadTid);
|
||||||
Thread->Parent = Parent;
|
Thread->Parent = Parent;
|
||||||
|
Status = HalThreadInit(Thread, Flags, Ip);
|
||||||
|
if (PT_ERROR(Status)) {
|
||||||
|
ExFreePoolWithTag(Thread, THREAD_POOL_TAG);
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
*Result = Thread;
|
*Result = Thread;
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user