3 * - By John Hodge (thePowersGang)
6 * - ARM7 Process Switching
9 #include <threads_int.h>
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[];
25 void Proc_IdleThread(void *unused);
28 tThread *gpCurrentThread = &gThreadZero;
29 tThread *gpIdleThread = NULL;
32 void ArchThreads_Init(void)
34 gProcessZero.MemState.Base = (tPAddr)&kernel_table0 - KERNEL_BASE;
37 void Proc_IdleThread(void *unused)
39 Threads_SetPriority(gpIdleThread, -1);
42 __asm__ __volatile__ ("wfi");
50 tid = Proc_NewKThread( Proc_IdleThread, NULL );
51 gpIdleThread = Threads_GetThread(tid);
52 gpIdleThread->ThreadName = (char*)"Idle Thread";
60 tThread *Proc_GetCurThread(void)
62 return gpCurrentThread;
65 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
72 // Log_Debug("Proc", "Proc_StartUser: (Entrypoint=%p, Base=%p, ArgC=%i, ArgV=%p, DataSize=0x%x)",
73 // Entrypoint, Base, ArgC, ArgV, DataSize);
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);
81 // Adjust user's copy of the arguments
82 delta = (tVAddr)usr_sp - (tVAddr)ArgV;
84 for(i = 0; ArgV[i]; i ++) ArgV[i] += delta;
86 for(i = 0; envp[i]; i ++) envp[i] += delta;
88 *--usr_sp = (Uint32)envp;
89 *--usr_sp = (Uint32)ArgV;
90 *--usr_sp = (Uint32)ArgC;
94 Log_Debug("Proc", "Proc_int_DropToUser(%p, %p)", Entrypoint, usr_sp);
95 Proc_int_DropToUser(Entrypoint, (Uint32)usr_sp);
98 void Proc_ClearProcess(tProcess *Process)
100 Log_Warning("Proc", "TODO: Nuke address space etc");
103 void Proc_ClearThread(tThread *Thread)
107 tTID Proc_Clone(Uint Flags)
112 new = Threads_CloneTCB(Flags);
115 // Actual clone magic
116 pc = Proc_CloneInt(&sp, &mem);
118 Log("Proc_Clone: In child");
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;
128 Threads_AddActive(new);
133 tThread *Proc_SpawnWorker( void (*Fnc)(void*), void *Ptr )
138 new = Threads_CloneThreadZero();
140 if(new->ThreadName) free(new->ThreadName);
141 new->ThreadName = NULL;
143 new->KernelStack = MM_NewKStack(1);
144 if(!new->KernelStack) {
145 // TODO: Delete thread
146 Log_Error("Proc", "Unable to allocate kernel stack");
150 sp = new->KernelStack;
152 *(Uint32*)(sp -= 4) = (Uint)Ptr;
153 *(Uint32*)(sp -= 4) = (Uint)Fnc;
154 *(Uint32*)(sp -= 4) = (Uint)new;
156 new->SavedState.SP = sp;
157 new->SavedState.IP = (Uint)KernelThreadHeader;
159 Threads_AddActive(new);
164 tTID Proc_NewKThread( void (*Fnc)(void*), void *Ptr )
169 new = Threads_CloneTCB(0);
171 free(new->ThreadName);
172 new->ThreadName = NULL;
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");
182 sp = new->KernelStack;
184 *(Uint32*)(sp -= 4) = (Uint)Ptr;
185 *(Uint32*)(sp -= 4) = (Uint)Fnc;
186 *(Uint32*)(sp -= 4) = (Uint)new;
188 new->SavedState.SP = sp;
189 new->SavedState.IP = (Uint)KernelThreadHeader;
191 Threads_AddActive(new);
196 void Proc_CallFaultHandler(tThread *Thread)
201 void Proc_Reschedule(void)
205 cur = gpCurrentThread;
207 next = Threads_GetNextToRun(0, cur);
208 if(!next) next = gpIdleThread;
209 if(!next || next == cur) return;
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
217 Log("Requested by %p", __builtin_return_address(0));
219 gpCurrentThread = next;
221 cur->SavedState.UserSP = Proc_int_SwapUserSP( next->SavedState.UserSP );
224 next->SavedState.SP, &cur->SavedState.SP,
225 next->SavedState.IP, &cur->SavedState.IP,
226 next->Process->MemState.Base
231 void Proc_DumpThreadCPUState(tThread *Thread)