os/kernelhwsrv/kernel/eka/nkern/x86/ncsched.cia
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\nkern\x86\ncsched.cia
    15 // 
    16 //
    17 
    18 #include <x86.h>
    19 
    20 #if defined(KSCHED)
    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);
    27 #endif
    28 
    29 const TLinAddr TScheduler_Reschedule = (TLinAddr)&TScheduler::Reschedule;
    30 const TLinAddr TheScheduler_iRescheduleNeededFlag = (TLinAddr)&TheScheduler.iRescheduleNeededFlag;
    31 const TLinAddr NKern_FastCounter = (TLinAddr)&NKern::FastCounter;
    32 
    33 __NAKED__ void TScheduler::YieldTo(NThreadBase*)
    34 	{
    35 	//
    36 	// Enter with kernel locked, interrupts can be on or off
    37 	// Exit with kernel unlocked, interrupts off
    38 	//
    39 	asm("mov byte ptr [%a0], 1" : : "i"(&TheScheduler.iRescheduleNeededFlag));
    40 	asm("call %a0" : : "i"(TScheduler_Reschedule));
    41 	asm("ret");
    42 	}
    43 
    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));
    45 
    46 
    47 
    48 /***************************************************************************
    49 * Reschedule
    50 * Enter with:
    51 *		Kernel locked, interrupts enabled or disabled
    52 * Return with:
    53 *		Kernel unlocked, interrupts disabled
    54 *		EAX=0 if no reschedule occurred, 1 if it did
    55 ***************************************************************************/
    56 __NAKED__ void TScheduler::Reschedule()
    57 	{
    58 	asm("push 0");
    59 	asm("cli");
    60 	asm("start_resched:");
    61 	asm("cmp byte ptr [%a0], 0" : : "i"(&TheScheduler.iDfcPendingFlag));
    62 	asm("jz resched_no_dfcs");
    63 
    64 	asm("mov ecx, %0": :"i"(&TheScheduler)); 
    65 #ifdef __GCC32__
    66 	asm("push ecx");
    67 	asm("call __ZN10TScheduler9QueueDfcsEv");
    68 	asm("add esp,4"); 
    69 #else
    70 	TheScheduler.QueueDfcs();
    71 #endif
    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");
    76 	asm("sti");
    77 	asm("push fs");
    78 	asm("push gs");
    79 	asm("push ebp");
    80 	asm("push edi");
    81 	asm("lea edi, %a0": : "i"(&TheScheduler));
    82 	asm("push esi");
    83 	asm("push ebx");
    84 	asm("mov eax, cr0");
    85 	asm("push eax");
    86 
    87 	asm("mov eax, [edi+4]");
    88 	asm("test eax, eax");
    89 	asm("jz resched_pri_lt_32");
    90 	asm("bsr ecx, eax");
    91 	asm("add cl, 32");
    92 	asm("jmp resched_1");
    93 	asm("resched_pri_lt_32:");
    94 	asm("mov eax, [edi]");
    95 	asm("bsr ecx, eax");
    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
   105 	asm("no_other:");
   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
   112 
   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");
   116 #ifdef __GCC32__
   117 	asm("mov eax, [edi+%0]" : : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
   118 #else
   119 	_asm mov eax, [edi]TheScheduler.iLock.iHoldingThread
   120 #endif
   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
   124 #ifdef __GCC32__
   125 	asm("mov eax,1");
   126 	asm("mov [edi+%0],eax" : : "i"(_FOFF(TScheduler, iLock) + _FOFF(NFastMutex,iWaiting)));
   127 #else
   128 	_asm mov [edi]TheScheduler.iLock.iWaiting, 1
   129 #endif
   130 	ASM_DEBUG1(ImpSysHeld,ebx)
   131 
   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)
   137 
   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));
   143 	asm("sub eax, edx");
   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));
   146 #endif
   147 #ifdef BTRACE_CPU_USAGE
   148 	asm("cmp byte ptr [%a0], 0": : "i"(&TheScheduler.iCpuUsageFilter));
   149 	asm("jz no_trace");
   150 	asm("push [%a0]": : "i"(&TheScheduler.iCurrentThread));
   151 	asm("push 0");
   152 	asm("push %0": : "i"(new_thread_trace_header));
   153 	asm("call dword ptr [%a0]": : "i"(&TheScheduler.iBTraceHandler));
   154 	asm("pop eax");
   155 	asm("pop eax");
   156 	asm("pop eax");
   157 	asm("no_trace:");
   158 #endif
   159 
   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
   163 	asm("cmp ebx, esi");
   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
   167 	asm("clts");
   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
   170 	asm("fwait");
   171 
   172 	asm("no_fpu:");
   173 	asm("same_thread:");
   174 
   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
   180 
   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:");
   186 	asm("pop eax");
   187 	asm("mov cr0, eax");
   188 	asm("pop ebx");
   189 	asm("pop esi");
   190 	asm("pop edi");
   191 	asm("pop ebp");
   192 	asm("pop gs");
   193 	asm("pop fs");
   194 	asm("cli");
   195 	asm("lea eax, %a0": : "i"(&TheScheduler.iRescheduleNeededFlag));
   196 	asm("cmp dword ptr [eax], 0");
   197 	asm("jnz start_resched");
   198 	asm("mov eax,0");
   199 	asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
   200 	asm("pop eax");
   201 	asm("ret");
   202 
   203 	asm("round_robin:");
   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
   211 	ASM_DEBUG1(RR,ebx);
   212 	asm("jmp no_other");
   213 
   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");
   219 	asm("mov ebx, edx");
   220 	asm("jmp resched_do_thread_switch");
   221 
   222 	asm("holds_fast_mutex:");
   223 #ifdef __GCC32__
   224 	asm("lea ecx, [edi+%0]": : "i"_FOFF(TScheduler,iLock));
   225 #else
   226 	_asm lea ecx, [edi]TheScheduler.iLock
   227 #endif
   228 	asm("cmp eax, ecx");
   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");
   232 #ifdef __GCC32__
   233 	asm("cmp dword ptr [edi+%0], 0": : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
   234 #else
   235 	_asm cmp [edi]TheScheduler.iLock.iHoldingThread, 0
   236 #endif
   237 	asm("jz resched_do_thread_switch");
   238 
   239 
   240 	asm("rr_holds_fast_mutex:");
   241 #ifdef __GCC32__
   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));
   245 	asm("pop edx");
   246 #else
   247 	_asm mov [eax]NFastMutex.iWaiting, 1
   248 #endif
   249 	asm("jmp resched_do_thread_switch");
   250 
   251 	asm("resched_not_needed:");
   252 	asm("xor eax, eax");
   253 	asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
   254 	asm("pop eax");
   255 	asm("ret");
   256 	}
   257 
   258 
   259 /** Disable interrupts to the specified level
   260 
   261 If aLevel = 0 does not affect interrupt state
   262 If aLevel <>0 disables all maskable interrupts.
   263 
   264 @param	aLevel level to which to disable
   265 @return	Cookie to pass into RestoreInterrupts()
   266 */
   267 EXPORT_C __NAKED__ TInt NKern::DisableInterrupts(TInt /*aLevel*/)
   268 	{
   269 	asm("pushfd");
   270 	asm("mov ecx, [esp+4]");
   271 	asm("pop eax");
   272 	asm("and eax, 0x200");
   273 	asm("test ecx, ecx");
   274 	asm("jz disable_ints_0");
   275 	asm("cli");
   276 	asm("disable_ints_0:");
   277 	asm("ret");
   278 	}
   279 
   280 
   281 /** Disable all maskable interrupts
   282 
   283 @return	Cookie to pass into RestoreInterrupts()
   284 */
   285 EXPORT_C __NAKED__ TInt NKern::DisableAllInterrupts()
   286 	{
   287 	asm("pushfd");
   288 	asm("pop eax");
   289 	asm("and eax, 0x200");
   290 	asm("cli");
   291 	asm("ret");
   292 	}
   293 
   294 
   295 /** Restore interrupt mask to state preceding a DisableInterrupts() call
   296 
   297 @param	aLevel Cookie returned by Disable(All)Interrupts()
   298 */
   299 EXPORT_C __NAKED__ void NKern::RestoreInterrupts(TInt aLevel)
   300 	{
   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
   304 	asm("ret");
   305 	asm("restore_irq_off:");
   306 	asm("cli");
   307 	asm("ret");
   308 	}
   309 
   310 
   311 /** Enable all maskable interrupts
   312 
   313 @internalComponent
   314 */
   315 EXPORT_C __NAKED__ void NKern::EnableAllInterrupts()
   316 	{
   317 	asm("sti");
   318 	asm("ret");
   319 	}
   320 
   321 
   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.
   325 
   326 @pre	Thread or IDFC context. Don't call from ISRs.
   327 */
   328 EXPORT_C __NAKED__ void NKern::Unlock()
   329 	{
   330 	asm("xor eax, eax");
   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");
   336 	asm("inc eax");
   337 	asm("mov dword ptr [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
   338 	asm("call %a0" : : "i"(TScheduler_Reschedule));
   339 	asm("sti");
   340 	asm("unlock_no_resched:");
   341 	asm("ret");
   342 	}
   343 
   344 
   345 /**	Locks the kernel
   346 Increments iKernCSLocked, thereby deferring IDFCs and preemption.
   347 
   348 @pre	Thread or IDFC context. Don't call from ISRs.
   349 */
   350 EXPORT_C __NAKED__ void NKern::Lock()
   351 	{
   352 	asm("inc dword ptr [%a0]": : "i"(&TheScheduler.iKernCSLocked));
   353 	asm("ret");
   354 	}
   355 
   356 
   357 /**	Locks the kernel and returns a pointer to the current thread
   358 Increments iKernCSLocked, thereby deferring IDFCs and preemption.
   359 
   360 @pre	Thread or IDFC context. Don't call from ISRs.
   361 */
   362 EXPORT_C __NAKED__ NThread* NKern::LockC()
   363 	{
   364 	asm("inc dword ptr [%a0]": :"i"(&TheScheduler.iKernCSLocked));
   365 	asm("mov eax, [%a0]": :"i"(&TheScheduler.iCurrentThread));
   366 	asm("ret");
   367 	}
   368 
   369 
   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.
   373 
   374 @return	Nonzero if a reschedule actually occurred, zero if not.
   375 @pre	Thread or IDFC context. Don't call from ISRs.
   376 */
   377 EXPORT_C __NAKED__ TInt NKern::PreemptionPoint()
   378 	{
   379 	asm("mov ecx, %0": : "i"(TheScheduler_iRescheduleNeededFlag));
   380 	asm("xor eax, eax");
   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));
   387 	asm("sti");
   388 
   389 	asm("preemption_point_no_resched:");
   390 	asm("ret");
   391 	}