Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\nkern\x86\ncsched.cia
21 extern "C" void __DebugMsgWaitForAnyRequest();
22 extern "C" void __DebugMsgResched(int a);
23 extern "C" void __DebugMsgInitSelection(int a);
24 extern "C" void __DebugMsgRR(int a);
25 extern "C" void __DebugMsgBlockedFM(int a);
26 extern "C" void __DebugMsgImpSysHeld(int a);
29 const TLinAddr TScheduler_Reschedule = (TLinAddr)&TScheduler::Reschedule;
30 const TLinAddr TheScheduler_iRescheduleNeededFlag = (TLinAddr)&TheScheduler.iRescheduleNeededFlag;
31 const TLinAddr NKern_FastCounter = (TLinAddr)&NKern::FastCounter;
33 __NAKED__ void TScheduler::YieldTo(NThreadBase*)
36 // Enter with kernel locked, interrupts can be on or off
37 // Exit with kernel unlocked, interrupts off
39 asm("mov byte ptr [%a0], 1" : : "i"(&TheScheduler.iRescheduleNeededFlag));
40 asm("call %a0" : : "i"(TScheduler_Reschedule));
44 const TUint32 new_thread_trace_header = ((8<<BTrace::ESizeIndex) + (BTrace::EContextIdPresent<<BTrace::EFlagsIndex*8) + (BTrace::ECpuUsage<<BTrace::ECategoryIndex*8) + (BTrace::ENewThreadContext<<BTrace::ESubCategoryIndex*8));
48 /***************************************************************************
51 * Kernel locked, interrupts enabled or disabled
53 * Kernel unlocked, interrupts disabled
54 * EAX=0 if no reschedule occurred, 1 if it did
55 ***************************************************************************/
56 __NAKED__ void TScheduler::Reschedule()
60 asm("start_resched:");
61 asm("cmp byte ptr [%a0], 0" : : "i"(&TheScheduler.iDfcPendingFlag));
62 asm("jz resched_no_dfcs");
64 asm("mov ecx, %0": :"i"(&TheScheduler));
67 asm("call __ZN10TScheduler9QueueDfcsEv");
70 TheScheduler.QueueDfcs();
72 asm("resched_no_dfcs:");
73 asm("cmp byte ptr [%a0], 0" : : "i"(&TheScheduler.iRescheduleNeededFlag));
74 asm("jz resched_not_needed");
75 asm("mov byte ptr [esp], 1");
81 asm("lea edi, %a0": : "i"(&TheScheduler));
87 asm("mov eax, [edi+4]");
89 asm("jz resched_pri_lt_32");
93 asm("resched_pri_lt_32:");
94 asm("mov eax, [edi]");
96 asm("resched_1:"); // ecx=highest ready priority
97 asm("mov byte ptr [edi+%0], 0" : : "i"_FOFF(TScheduler,iRescheduleNeededFlag)); // mov byte ptr [edi]TScheduler.iRescheduleNeededFlag, 0
98 asm("lea esi, [edi+ecx*4+8]"); // esi points to corresponding queue
99 asm("mov ebx, [esi]"); // ebx points to highest priority thread
100 ASM_DEBUG1(InitSelection,ebx)
101 asm("cmp dword ptr [ebx+%0], 0" : :"i"_FOFF(NThreadBase,iTime)); // check if timeslice expired
102 asm("jnz no_other"); // skip if not
103 asm("cmp ebx, [ebx]"); // else check for other threads at this priority
104 asm("jnz round_robin"); // branch if there are
106 asm("mov eax, [ebx+%0]" : : "i"_FOFF(NThreadBase,iHeldFastMutex));
107 asm("test eax, eax"); // does this thread hold a fast mutex?
108 asm("jnz holds_fast_mutex"); // branch if it does
109 asm("mov eax, [ebx+%0]" : : "i"_FOFF(NThreadBase,iWaitFastMutex));
110 asm("test eax, eax"); // is thread blocked on a fast mutex?
111 asm("jnz resched_blocked"); // branch if it is
113 asm("resched_not_blocked:");
114 asm("test byte ptr [ebx+10], 1"); // test for implicit system lock
115 asm("jz resched_no_imp_sys");
117 asm("mov eax, [edi+%0]" : : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
119 _asm mov eax, [edi]TheScheduler.iLock.iHoldingThread
121 asm("test eax, eax");
122 asm("jz resched_imp_sys_ok");
123 asm("mov ebx, eax"); // system lock held so switch to holding thread
126 asm("mov [edi+%0],eax" : : "i"(_FOFF(TScheduler, iLock) + _FOFF(NFastMutex,iWaiting)));
128 _asm mov [edi]TheScheduler.iLock.iWaiting, 1
130 ASM_DEBUG1(ImpSysHeld,ebx)
132 asm("resched_no_imp_sys:");
133 asm("resched_imp_sys_ok:");
134 asm("resched_do_thread_switch:");
135 // EBX->new thread, EDI->TheScheduler
136 ASM_DEBUG1(Resched,ebx)
138 #ifdef MONITOR_THREAD_CPU_TIME
139 asm("call %a0" : :"i"(NKern_FastCounter));
140 asm("mov ecx, [edi+%0]" : : "i"_FOFF(TScheduler,iCurrentThread));
141 asm("mov edx, [ecx+%0]" : : "i"_FOFF(NThreadBase,iLastStartTime));
142 asm("mov [ebx+%0], eax" : : "i"_FOFF(NThreadBase,iLastStartTime));
144 asm("add dword ptr ([ecx+%0]), eax" : : "i"_FOFF(NThreadBase,iTotalCpuTime));
145 asm("adc dword ptr ([ecx+4+%0]), 0" : : "i"_FOFF(NThreadBase,iTotalCpuTime));
147 #ifdef BTRACE_CPU_USAGE
148 asm("cmp byte ptr [%a0], 0": : "i"(&TheScheduler.iCpuUsageFilter));
150 asm("push [%a0]": : "i"(&TheScheduler.iCurrentThread));
152 asm("push %0": : "i"(new_thread_trace_header));
153 asm("call dword ptr [%a0]": : "i"(&TheScheduler.iBTraceHandler));
160 asm("mov esi, [edi+%0]": :"i"_FOFF(TScheduler,iCurrentThread)); // ESI -> original thread
161 asm("mov [esi+%0], esp": :"i"_FOFF(NThreadBase,iSavedSP)); // Save original thread stack pointer
162 asm("mov [edi+%0], ebx": :"i"_FOFF(TScheduler,iCurrentThread)); // EBX -> new thread, update current thread
164 asm("je same_thread");
165 asm("test byte ptr [esp], 8"); // test thread's TS flag
166 asm("jnz no_fpu"); // if set, thread did not use FPU
168 asm("fnsave [esi+%0]": :"i"_FOFF(NThread,iCoprocessorState)); // else thread did use FPU - save its state
169 asm("or byte ptr [esp], 8"); // set TS flag so thread aborts next time it uses FPU
175 asm("mov esp, [ebx+%0]": :"i"_FOFF(NThreadBase,iSavedSP)); // Load new thread stack pointer
176 asm("mov eax, [ebx+%0]": :"i"_FOFF(NThreadBase,iStackBase));
177 asm("add eax, [ebx+%0]": :"i"_FOFF(NThreadBase,iStackSize));
178 asm("mov ecx, dword ptr [%a0]": :"i"(&X86_TSS_Ptr));
179 asm("mov [ecx+%0], eax": :"i"_FOFF(TX86Tss,iEsp0)); // set ESP0 to top of new thread supervisor stack
181 asm("test byte ptr [ebx+%0], 2": :"i"_FOFF(TPriListLink,iSpare2)); // test for address space switch
182 asm("jz resched_no_as_switch");
183 asm("call [edi+%0]": :"i"_FOFF(TScheduler,iProcessHandler)); // call handler with
184 // EBX=pointer to new thread, EDI->scheduler, preserves ESI, EDI
185 asm("resched_no_as_switch:");
195 asm("lea eax, %a0": : "i"(&TheScheduler.iRescheduleNeededFlag));
196 asm("cmp dword ptr [eax], 0");
197 asm("jnz start_resched");
199 asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
204 asm("mov eax, [ebx+%0]": : "i"_FOFF(NThreadBase,iHeldFastMutex));
205 asm("test eax, eax"); // does this thread hold a fast mutex?
206 asm("jnz rr_holds_fast_mutex"); // branch if it does
207 asm("mov eax, [ebx+%0]": : "i"_FOFF(NThreadBase,iTimeslice));
208 asm("mov [ebx+%0], eax": : "i"_FOFF(NThreadBase,iTime)); // else new timeslice for this thread next time
209 asm("mov ebx, [ebx]"); // candidate thread = next thread in round-robin order
210 asm("mov [esi], ebx"); // the latter is now the first at this priority
214 asm("resched_blocked:");
215 ASM_DEBUG1(BlockedFM,eax)
216 asm("mov edx, [eax+%0]": : "i"_FOFF(NFastMutex,iHoldingThread));
217 asm("test edx, edx");
218 asm("jz resched_not_blocked");
220 asm("jmp resched_do_thread_switch");
222 asm("holds_fast_mutex:");
224 asm("lea ecx, [edi+%0]": : "i"_FOFF(TScheduler,iLock));
226 _asm lea ecx, [edi]TheScheduler.iLock
229 asm("je resched_do_thread_switch");
230 asm("test byte ptr [ebx+10], 1"); // test for implicit system lock
231 asm("jz resched_do_thread_switch");
233 asm("cmp dword ptr [edi+%0], 0": : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
235 _asm cmp [edi]TheScheduler.iLock.iHoldingThread, 0
237 asm("jz resched_do_thread_switch");
240 asm("rr_holds_fast_mutex:");
242 asm("push edx"); // storing an immediate value to an C-offset address appears to be
243 asm("mov edx,1"); // impossible in GCC, so we use edx instead
244 asm("mov [eax+%0], edx": : "i"_FOFF(NFastMutex,iWaiting));
247 _asm mov [eax]NFastMutex.iWaiting, 1
249 asm("jmp resched_do_thread_switch");
251 asm("resched_not_needed:");
253 asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
259 /** Disable interrupts to the specified level
261 If aLevel = 0 does not affect interrupt state
262 If aLevel <>0 disables all maskable interrupts.
264 @param aLevel level to which to disable
265 @return Cookie to pass into RestoreInterrupts()
267 EXPORT_C __NAKED__ TInt NKern::DisableInterrupts(TInt /*aLevel*/)
270 asm("mov ecx, [esp+4]");
272 asm("and eax, 0x200");
273 asm("test ecx, ecx");
274 asm("jz disable_ints_0");
276 asm("disable_ints_0:");
281 /** Disable all maskable interrupts
283 @return Cookie to pass into RestoreInterrupts()
285 EXPORT_C __NAKED__ TInt NKern::DisableAllInterrupts()
289 asm("and eax, 0x200");
295 /** Restore interrupt mask to state preceding a DisableInterrupts() call
297 @param aLevel Cookie returned by Disable(All)Interrupts()
299 EXPORT_C __NAKED__ void NKern::RestoreInterrupts(TInt aLevel)
301 asm("test byte ptr [esp+5], 2"); // test saved I flag
302 asm("jz restore_irq_off"); // jump if clear
303 asm("sti"); // else reenable interrupts
305 asm("restore_irq_off:");
311 /** Enable all maskable interrupts
315 EXPORT_C __NAKED__ void NKern::EnableAllInterrupts()
322 /** Unlocks the kernel
323 Decrements iKernCSLocked; if it becomes zero and IDFCs or a reschedule are
324 pending, calls the scheduler to process them.
326 @pre Thread or IDFC context. Don't call from ISRs.
328 EXPORT_C __NAKED__ void NKern::Unlock()
331 asm("dec dword ptr [%a0]": : "i"(&TheScheduler.iKernCSLocked));
332 asm("lea edx, %a0": : "i"(&TheScheduler.iRescheduleNeededFlag));
333 asm("jnz unlock_no_resched");
334 asm("cmp eax, [edx]");
335 asm("jz unlock_no_resched");
337 asm("mov dword ptr [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
338 asm("call %a0" : : "i"(TScheduler_Reschedule));
340 asm("unlock_no_resched:");
346 Increments iKernCSLocked, thereby deferring IDFCs and preemption.
348 @pre Thread or IDFC context. Don't call from ISRs.
350 EXPORT_C __NAKED__ void NKern::Lock()
352 asm("inc dword ptr [%a0]": : "i"(&TheScheduler.iKernCSLocked));
357 /** Locks the kernel and returns a pointer to the current thread
358 Increments iKernCSLocked, thereby deferring IDFCs and preemption.
360 @pre Thread or IDFC context. Don't call from ISRs.
362 EXPORT_C __NAKED__ NThread* NKern::LockC()
364 asm("inc dword ptr [%a0]": :"i"(&TheScheduler.iKernCSLocked));
365 asm("mov eax, [%a0]": :"i"(&TheScheduler.iCurrentThread));
370 /** Allows IDFCs and rescheduling if they are pending.
371 If IDFCs or a reschedule are pending and iKernCSLocked is exactly equal to 1
372 calls the scheduler to process the IDFCs and possibly reschedule.
374 @return Nonzero if a reschedule actually occurred, zero if not.
375 @pre Thread or IDFC context. Don't call from ISRs.
377 EXPORT_C __NAKED__ TInt NKern::PreemptionPoint()
379 asm("mov ecx, %0": : "i"(TheScheduler_iRescheduleNeededFlag));
381 asm("cmp eax, [ecx]");
382 asm("jz preemption_point_no_resched");
383 asm("cmp dword ptr [%a0], 1": : "i"(&TheScheduler.iKernCSLocked));
384 asm("jne preemption_point_no_resched");
385 asm("call %a0" : : "i"(TScheduler_Reschedule));
386 asm("mov dword ptr [%a0], 1": : "i"(&TheScheduler.iKernCSLocked));
389 asm("preemption_point_no_resched:");