Kernel - API Change - Proc_SpawnWorker return thread pointer
[tpg/acess2.git] / KernelLand / Kernel / arch / armv7 / proc.c
1 /*
2  * Acess2
3  * - By John Hodge (thePowersGang)
4  *
5  * arch/arm7/proc.c
6  * - ARM7 Process Switching
7  */
8 #include <acess.h>
9 #include <threads_int.h>
10 #include <hal_proc.h>
11
12 // === IMPORTS ===
13 extern tThread  gThreadZero;
14 extern tProcess gProcessZero;
15 extern void     SwitchTask(Uint32 NewSP, Uint32 *OldSP, Uint32 NewIP, Uint32 *OldIP, Uint32 MemPtr);
16 extern void     KernelThreadHeader(void);       // Actually takes args on stack
17 extern void     Proc_int_DropToUser(Uint32 IP, Uint32 SP) NORETURN __attribute__((long_call));
18 extern Uint32   Proc_int_SwapUserSP(Uint32 NewSP);
19 extern Uint32   Proc_CloneInt(Uint32 *SP, Uint32 *MemPtr);
20 extern tVAddr   MM_NewKStack(int bGlobal);      // TODO: Move out into a header
21 extern tVAddr   MM_NewUserStack(void);
22 extern char     kernel_table0[];
23
24 // === PROTOTYPES ===
25 void    Proc_IdleThread(void *unused);
26
27 // === GLOBALS ===
28 tThread *gpCurrentThread = &gThreadZero;
29 tThread *gpIdleThread = NULL;
30
31 // === CODE ===
32 void ArchThreads_Init(void)
33 {
34         gProcessZero.MemState.Base = (tPAddr)&kernel_table0 - KERNEL_BASE;
35 }
36
37 void Proc_IdleThread(void *unused)
38 {
39         Threads_SetPriority(gpIdleThread, -1);
40         for(;;) {
41                 Proc_Reschedule();
42                 __asm__ __volatile__ ("wfi");
43         }
44 }
45
46 void Proc_Start(void)
47 {
48         tTID    tid;
49
50         tid = Proc_NewKThread( Proc_IdleThread, NULL );
51         gpIdleThread = Threads_GetThread(tid);
52         gpIdleThread->ThreadName = (char*)"Idle Thread";
53 }
54
55 int GetCPUNum(void)
56 {
57         return 0;
58 }
59
60 tThread *Proc_GetCurThread(void)
61 {
62         return gpCurrentThread;
63 }
64
65 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
66 {
67         Uint32  *usr_sp;
68          int    i;
69         const char      **envp;
70         tVAddr  delta;
71
72 //      Log_Debug("Proc", "Proc_StartUser: (Entrypoint=%p, Base=%p, ArgC=%i, ArgV=%p, DataSize=0x%x)",
73 //              Entrypoint, Base, ArgC, ArgV, DataSize);
74
75         // Write data to the user's stack
76         usr_sp = (void*)MM_NewUserStack();
77         usr_sp -= (DataSize+3)/4;
78         memcpy(usr_sp, ArgV, DataSize);
79         free(ArgV);
80
81         // Adjust user's copy of the arguments
82         delta = (tVAddr)usr_sp -  (tVAddr)ArgV;
83         ArgV = (void*)usr_sp;
84         for(i = 0; ArgV[i]; i ++)       ArgV[i] += delta;
85         envp = &ArgV[i+1];
86         for(i = 0; envp[i]; i ++)       envp[i] += delta;
87         
88         *--usr_sp = (Uint32)envp;
89         *--usr_sp = (Uint32)ArgV;
90         *--usr_sp = (Uint32)ArgC;
91         *--usr_sp = Base;
92         
93         // Drop to user code
94         Log_Debug("Proc", "Proc_int_DropToUser(%p, %p)", Entrypoint, usr_sp);
95         Proc_int_DropToUser(Entrypoint, (Uint32)usr_sp);
96 }
97
98 void Proc_ClearProcess(tProcess *Process)
99 {
100         Log_Warning("Proc", "TODO: Nuke address space etc");
101 }
102
103 void Proc_ClearThread(tThread *Thread)
104 {
105 }
106
107 tTID Proc_Clone(Uint Flags)
108 {
109         tThread *new;
110         Uint32  pc, sp, mem;
111
112         new = Threads_CloneTCB(Flags);
113         if(!new)        return -1;
114
115         // Actual clone magic
116         pc = Proc_CloneInt(&sp, &mem);
117         if(pc == 0) {
118                 Log("Proc_Clone: In child");
119                 return 0;
120         }
121         
122         new->SavedState.IP = pc;
123         new->SavedState.SP = sp;
124         new->SavedState.UserSP = Proc_int_SwapUserSP(0);
125         new->SavedState.UserIP = Proc_GetCurThread()->SavedState.UserIP;
126         new->Process->MemState.Base = mem;
127
128         Threads_AddActive(new);
129
130         return new->TID;
131 }
132
133 tThread *Proc_SpawnWorker( void (*Fnc)(void*), void *Ptr )
134 {
135         tThread *new;
136         Uint32  sp;
137
138         new = Threads_CloneThreadZero();
139         if(!new)        return -1;
140         if(new->ThreadName)     free(new->ThreadName);
141         new->ThreadName = NULL;
142
143         new->KernelStack = MM_NewKStack(1);
144         if(!new->KernelStack) {
145                 // TODO: Delete thread
146                 Log_Error("Proc", "Unable to allocate kernel stack");
147                 return NULL;
148         }       
149
150         sp = new->KernelStack;
151         
152         *(Uint32*)(sp -= 4) = (Uint)Ptr;
153         *(Uint32*)(sp -= 4) = (Uint)Fnc;
154         *(Uint32*)(sp -= 4) = (Uint)new;
155
156         new->SavedState.SP = sp;
157         new->SavedState.IP = (Uint)KernelThreadHeader;
158
159         Threads_AddActive(new);
160
161         return new;
162 }
163
164 tTID Proc_NewKThread( void (*Fnc)(void*), void *Ptr )
165 {
166         tThread *new;
167         Uint32  sp;
168
169         new = Threads_CloneTCB(0);
170         if(!new)        return -1;
171         free(new->ThreadName);
172         new->ThreadName = NULL;
173
174         // TODO: Non-shared stack
175         new->KernelStack = MM_NewKStack(1);
176         if(!new->KernelStack) {
177                 // TODO: Delete thread
178                 Log_Error("Proc", "Unable to allocate kernel stack");
179                 return -1;
180         }       
181
182         sp = new->KernelStack;
183         
184         *(Uint32*)(sp -= 4) = (Uint)Ptr;
185         *(Uint32*)(sp -= 4) = (Uint)Fnc;
186         *(Uint32*)(sp -= 4) = (Uint)new;
187
188         new->SavedState.SP = sp;
189         new->SavedState.IP = (Uint)KernelThreadHeader;
190
191         Threads_AddActive(new);
192
193         return new->TID;
194 }
195
196 void Proc_CallFaultHandler(tThread *Thread)
197 {
198
199 }
200
201 void Proc_Reschedule(void)
202 {
203         tThread *cur, *next;
204
205         cur = gpCurrentThread;
206
207         next = Threads_GetNextToRun(0, cur);
208         if(!next)       next = gpIdleThread;
209         if(!next || next == cur)        return;
210
211         Log("Switching to %p (%i %s) IP=%p SP=%p TTBR0=%p UsrSP=%p",
212                 next, next->TID, next->ThreadName,
213                 next->SavedState.IP, next->SavedState.SP, next->Process->MemState.Base,
214                 next->SavedState.UserSP
215                 );
216
217         Log("Requested by %p", __builtin_return_address(0));
218         
219         gpCurrentThread = next;
220
221         cur->SavedState.UserSP = Proc_int_SwapUserSP( next->SavedState.UserSP );
222
223         SwitchTask(
224                 next->SavedState.SP, &cur->SavedState.SP,
225                 next->SavedState.IP, &cur->SavedState.IP,
226                 next->Process->MemState.Base
227                 );
228         
229 }
230
231 void Proc_DumpThreadCPUState(tThread *Thread)
232 {
233         
234 }
235

UCC git Repository :: git.ucc.asn.au