1 
2 #ifndef __SYS_THREAD_H__
3 #define __SYS_THREAD_H__
4 
5 #include <sys/queue.h>
6 #include <sys/handle.h>
7 #include <sys/ktimer.h>
8 #include <sys/waitchannel.h>
9 
10 struct Thread;
11 typedef struct Thread Thread;
12 struct Process;
13 typedef struct Process Process;
14 
15 #include <sys/semaphore.h>
16 #include <sys/mutex.h>
17 #include <sys/cv.h>
18 
19 #include <machine/pmap.h>
20 #include <machine/thread.h>
21 
22 typedef TAILQ_HEAD(ProcessQueue, Process) ProcessQueue;
23 typedef TAILQ_HEAD(ThreadQueue, Thread) ThreadQueue;
24 
25 #define SCHED_STATE_NULL	0
26 #define SCHED_STATE_RUNNABLE	1
27 #define SCHED_STATE_RUNNING	2
28 #define SCHED_STATE_WAITING	3
29 #define SCHED_STATE_ZOMBIE	4
30 
31 typedef struct Thread {
32     ThreadArch		arch;
33     AS			*space;
34     uintptr_t		kstack;
35     uintptr_t		ustack;
36     uint64_t		tid;
37     uint64_t		refCount;
38     // Process
39     struct Process	*proc;
40     TAILQ_ENTRY(Thread)	threadList;
41     // Scheduler
42     int			schedState;
43     TAILQ_ENTRY(Thread)	schedQueue;
44     KTimerEvent		*timerEvt;	// Timer event for wakeups
45     uintptr_t		exitValue;
46     TAILQ_ENTRY(Thread)	semaQueue;	// Semaphore Queue
47     // Wait Channels
48     WaitChannel		*chan;
49     TAILQ_ENTRY(Thread)	chanQueue;
50     // Statistics
51     uint64_t		ctxSwitches;
52     uint64_t		userTime;
53     uint64_t		kernTime;
54     uint64_t		waitTime;
55     uint64_t		waitStart;
56 } Thread;
57 
58 #define PROCESS_HANDLE_SLOTS	128
59 #define PROCESS_TITLE_LENGTH	128
60 
61 #define PROC_STATE_NULL		0
62 #define PROC_STATE_READY	1
63 #define PROC_STATE_ZOMBIE	2
64 
65 typedef struct Process {
66     uint64_t			pid;
67     AS				*space;
68     Spinlock			lock;
69     uintptr_t			entrypoint;
70     uint64_t			nextThreadID;
71     uintptr_t			ustackNext;	// Next user stack
72     TAILQ_ENTRY(Process)	processList;
73     uint64_t			refCount;
74     char			title[PROCESS_TITLE_LENGTH];
75     int				procState;
76     uint64_t			exitCode;
77     // Process
78     Process			*parent;
79     TAILQ_ENTRY(Process)	siblingList;
80     ProcessQueue		childrenList;
81     ProcessQueue		zombieProc;
82     Mutex			zombieProcLock;
83     CV				zombieProcCV;
84     CV				zombieProcPCV;
85     // Threads
86     uint64_t			threads;
87     ThreadQueue			threadList;
88     Semaphore			zombieSemaphore;
89     ThreadQueue			zombieQueue;
90     // Handles
91     uint64_t			nextFD;
92     HandleQueue			handles[PROCESS_HANDLE_SLOTS];
93 } Process;
94 
95 // General
96 void Thread_Init();
97 void Thread_InitAP();
98 
99 // Process functions
100 Process *Process_Create(Process *parent, const char *title);
101 Process *Process_Lookup(uint64_t pid);
102 void Process_Retain(Process *proc);
103 void Process_Release(Process *proc);
104 uint64_t Process_Wait(Process *proc, uint64_t pid);
105 
106 #define TID_ANY		0xFFFFFFFF
107 
108 // Thread functions
109 Thread *Thread_Create(Process *proc);
110 Thread *Thread_KThreadCreate(void (*f)(void*), void *arg);
111 Thread *Thread_UThreadCreate(Thread *oldThr, uint64_t rip, uint64_t arg);
112 Thread *Thread_Lookup(Process *proc, uint64_t tid);
113 void Thread_Retain(Thread *thr);
114 void Thread_Release(Thread *thr);
115 uint64_t Thread_Wait(Thread *thr, uint64_t tid);
116 
117 // Scheduler functions
118 Thread *Sched_Current();
119 void Sched_SetRunnable(Thread *thr);
120 void Sched_SetWaiting(Thread *thr);
121 void Sched_SetZombie(Thread *thr);
122 void Sched_Scheduler();
123 
124 // Debugging
125 void Process_Dump(Process *proc);
126 void Thread_Dump(Thread *thr);
127 
128 // Platform functions
129 void Thread_InitArch(Thread *thr);
130 void Thread_SetupKThread(Thread *thr, void (*f)(),
131 			 uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
132 void Thread_SetupUThread(Thread *thr, uint64_t rip, uint64_t arg);
133 void Thread_SwitchArch(Thread *oldthr, Thread *newthr);
134 
135 // Handle Functions
136 void Handle_Init(Process *proc);
137 void Handle_Destroy(Process *proc);
138 uint64_t Handle_Add(Process *proc, Handle *handle);
139 void Handle_Remove(Process *proc, Handle *handle);
140 Handle *Handle_Lookup(Process *proc, uint64_t fd);
141 
142 // Copy_In/Copy_Out Functions
143 int Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len);
144 int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len);
145 int Copy_StrIn(uintptr_t fromuser, void *tokernel, uintptr_t len);
146 int Copy_StrOut(void *fromkernel, uintptr_t touser, uintptr_t len);
147 
148 #endif /* __SYS_THREAD_H__ */
149 
150