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.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\nkern\x86\ncsched.cia
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <x86.h>
sl@0
    19
sl@0
    20
#if defined(KSCHED)
sl@0
    21
extern "C" void __DebugMsgWaitForAnyRequest();
sl@0
    22
extern "C" void __DebugMsgResched(int a);
sl@0
    23
extern "C" void __DebugMsgInitSelection(int a);
sl@0
    24
extern "C" void __DebugMsgRR(int a);
sl@0
    25
extern "C" void __DebugMsgBlockedFM(int a);
sl@0
    26
extern "C" void __DebugMsgImpSysHeld(int a);
sl@0
    27
#endif
sl@0
    28
sl@0
    29
const TLinAddr TScheduler_Reschedule = (TLinAddr)&TScheduler::Reschedule;
sl@0
    30
const TLinAddr TheScheduler_iRescheduleNeededFlag = (TLinAddr)&TheScheduler.iRescheduleNeededFlag;
sl@0
    31
const TLinAddr NKern_FastCounter = (TLinAddr)&NKern::FastCounter;
sl@0
    32
sl@0
    33
__NAKED__ void TScheduler::YieldTo(NThreadBase*)
sl@0
    34
	{
sl@0
    35
	//
sl@0
    36
	// Enter with kernel locked, interrupts can be on or off
sl@0
    37
	// Exit with kernel unlocked, interrupts off
sl@0
    38
	//
sl@0
    39
	asm("mov byte ptr [%a0], 1" : : "i"(&TheScheduler.iRescheduleNeededFlag));
sl@0
    40
	asm("call %a0" : : "i"(TScheduler_Reschedule));
sl@0
    41
	asm("ret");
sl@0
    42
	}
sl@0
    43
sl@0
    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));
sl@0
    45
sl@0
    46
sl@0
    47
sl@0
    48
/***************************************************************************
sl@0
    49
* Reschedule
sl@0
    50
* Enter with:
sl@0
    51
*		Kernel locked, interrupts enabled or disabled
sl@0
    52
* Return with:
sl@0
    53
*		Kernel unlocked, interrupts disabled
sl@0
    54
*		EAX=0 if no reschedule occurred, 1 if it did
sl@0
    55
***************************************************************************/
sl@0
    56
__NAKED__ void TScheduler::Reschedule()
sl@0
    57
	{
sl@0
    58
	asm("push 0");
sl@0
    59
	asm("cli");
sl@0
    60
	asm("start_resched:");
sl@0
    61
	asm("cmp byte ptr [%a0], 0" : : "i"(&TheScheduler.iDfcPendingFlag));
sl@0
    62
	asm("jz resched_no_dfcs");
sl@0
    63
sl@0
    64
	asm("mov ecx, %0": :"i"(&TheScheduler)); 
sl@0
    65
#ifdef __GCC32__
sl@0
    66
	asm("push ecx");
sl@0
    67
	asm("call __ZN10TScheduler9QueueDfcsEv");
sl@0
    68
	asm("add esp,4"); 
sl@0
    69
#else
sl@0
    70
	TheScheduler.QueueDfcs();
sl@0
    71
#endif
sl@0
    72
	asm("resched_no_dfcs:");
sl@0
    73
	asm("cmp byte ptr [%a0], 0" : : "i"(&TheScheduler.iRescheduleNeededFlag));
sl@0
    74
	asm("jz resched_not_needed");
sl@0
    75
	asm("mov byte ptr [esp], 1");
sl@0
    76
	asm("sti");
sl@0
    77
	asm("push fs");
sl@0
    78
	asm("push gs");
sl@0
    79
	asm("push ebp");
sl@0
    80
	asm("push edi");
sl@0
    81
	asm("lea edi, %a0": : "i"(&TheScheduler));
sl@0
    82
	asm("push esi");
sl@0
    83
	asm("push ebx");
sl@0
    84
	asm("mov eax, cr0");
sl@0
    85
	asm("push eax");
sl@0
    86
sl@0
    87
	asm("mov eax, [edi+4]");
sl@0
    88
	asm("test eax, eax");
sl@0
    89
	asm("jz resched_pri_lt_32");
sl@0
    90
	asm("bsr ecx, eax");
sl@0
    91
	asm("add cl, 32");
sl@0
    92
	asm("jmp resched_1");
sl@0
    93
	asm("resched_pri_lt_32:");
sl@0
    94
	asm("mov eax, [edi]");
sl@0
    95
	asm("bsr ecx, eax");
sl@0
    96
	asm("resched_1:");						// ecx=highest ready priority
sl@0
    97
	asm("mov byte ptr [edi+%0], 0" : : "i"_FOFF(TScheduler,iRescheduleNeededFlag)); // mov byte ptr [edi]TScheduler.iRescheduleNeededFlag, 0
sl@0
    98
	asm("lea esi, [edi+ecx*4+8]");			// esi points to corresponding queue
sl@0
    99
	asm("mov ebx, [esi]");					// ebx points to highest priority thread
sl@0
   100
	ASM_DEBUG1(InitSelection,ebx)
sl@0
   101
	asm("cmp dword ptr [ebx+%0], 0" : :"i"_FOFF(NThreadBase,iTime));	// check if timeslice expired
sl@0
   102
	asm("jnz no_other");					// skip if not
sl@0
   103
	asm("cmp ebx, [ebx]");					// else check for other threads at this priority
sl@0
   104
	asm("jnz round_robin");					// branch if there are
sl@0
   105
	asm("no_other:");
sl@0
   106
	asm("mov eax, [ebx+%0]" : : "i"_FOFF(NThreadBase,iHeldFastMutex));
sl@0
   107
	asm("test eax, eax");					// does this thread hold a fast mutex?
sl@0
   108
	asm("jnz holds_fast_mutex");			// branch if it does
sl@0
   109
	asm("mov eax, [ebx+%0]" : : "i"_FOFF(NThreadBase,iWaitFastMutex));
sl@0
   110
	asm("test eax, eax");					// is thread blocked on a fast mutex?
sl@0
   111
	asm("jnz resched_blocked");				// branch if it is
sl@0
   112
sl@0
   113
	asm("resched_not_blocked:");
sl@0
   114
	asm("test byte ptr [ebx+10], 1");		// test for implicit system lock
sl@0
   115
	asm("jz resched_no_imp_sys");
sl@0
   116
#ifdef __GCC32__
sl@0
   117
	asm("mov eax, [edi+%0]" : : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
sl@0
   118
#else
sl@0
   119
	_asm mov eax, [edi]TheScheduler.iLock.iHoldingThread
sl@0
   120
#endif
sl@0
   121
	asm("test eax, eax");
sl@0
   122
	asm("jz resched_imp_sys_ok");
sl@0
   123
	asm("mov ebx, eax");					// system lock held so switch to holding thread
sl@0
   124
#ifdef __GCC32__
sl@0
   125
	asm("mov eax,1");
sl@0
   126
	asm("mov [edi+%0],eax" : : "i"(_FOFF(TScheduler, iLock) + _FOFF(NFastMutex,iWaiting)));
sl@0
   127
#else
sl@0
   128
	_asm mov [edi]TheScheduler.iLock.iWaiting, 1
sl@0
   129
#endif
sl@0
   130
	ASM_DEBUG1(ImpSysHeld,ebx)
sl@0
   131
sl@0
   132
	asm("resched_no_imp_sys:");
sl@0
   133
	asm("resched_imp_sys_ok:");
sl@0
   134
	asm("resched_do_thread_switch:");
sl@0
   135
	// EBX->new thread, EDI->TheScheduler
sl@0
   136
	ASM_DEBUG1(Resched,ebx)
sl@0
   137
sl@0
   138
#ifdef MONITOR_THREAD_CPU_TIME
sl@0
   139
	asm("call %a0" : :"i"(NKern_FastCounter));
sl@0
   140
	asm("mov ecx, [edi+%0]" : : "i"_FOFF(TScheduler,iCurrentThread));
sl@0
   141
	asm("mov edx, [ecx+%0]" : : "i"_FOFF(NThreadBase,iLastStartTime));
sl@0
   142
	asm("mov [ebx+%0], eax" : : "i"_FOFF(NThreadBase,iLastStartTime));
sl@0
   143
	asm("sub eax, edx");
sl@0
   144
	asm("add dword ptr ([ecx+%0]), eax" : : "i"_FOFF(NThreadBase,iTotalCpuTime));
sl@0
   145
	asm("adc dword ptr ([ecx+4+%0]), 0" : : "i"_FOFF(NThreadBase,iTotalCpuTime));
sl@0
   146
#endif
sl@0
   147
#ifdef BTRACE_CPU_USAGE
sl@0
   148
	asm("cmp byte ptr [%a0], 0": : "i"(&TheScheduler.iCpuUsageFilter));
sl@0
   149
	asm("jz no_trace");
sl@0
   150
	asm("push [%a0]": : "i"(&TheScheduler.iCurrentThread));
sl@0
   151
	asm("push 0");
sl@0
   152
	asm("push %0": : "i"(new_thread_trace_header));
sl@0
   153
	asm("call dword ptr [%a0]": : "i"(&TheScheduler.iBTraceHandler));
sl@0
   154
	asm("pop eax");
sl@0
   155
	asm("pop eax");
sl@0
   156
	asm("pop eax");
sl@0
   157
	asm("no_trace:");
sl@0
   158
#endif
sl@0
   159
sl@0
   160
	asm("mov esi, [edi+%0]": :"i"_FOFF(TScheduler,iCurrentThread)); // ESI -> original thread
sl@0
   161
	asm("mov [esi+%0], esp": :"i"_FOFF(NThreadBase,iSavedSP));		// Save original thread stack pointer
sl@0
   162
	asm("mov [edi+%0], ebx": :"i"_FOFF(TScheduler,iCurrentThread));	// EBX -> new thread, update current thread
sl@0
   163
	asm("cmp ebx, esi");
sl@0
   164
	asm("je same_thread");
sl@0
   165
	asm("test byte ptr [esp], 8");									// test thread's TS flag
sl@0
   166
	asm("jnz no_fpu");												// if set, thread did not use FPU
sl@0
   167
	asm("clts");
sl@0
   168
	asm("fnsave [esi+%0]": :"i"_FOFF(NThread,iCoprocessorState));	// else thread did use FPU - save its state
sl@0
   169
	asm("or byte ptr [esp], 8");									// set TS flag so thread aborts next time it uses FPU
sl@0
   170
	asm("fwait");
sl@0
   171
sl@0
   172
	asm("no_fpu:");
sl@0
   173
	asm("same_thread:");
sl@0
   174
sl@0
   175
	asm("mov esp, [ebx+%0]": :"i"_FOFF(NThreadBase,iSavedSP));		// Load new thread stack pointer
sl@0
   176
	asm("mov eax, [ebx+%0]": :"i"_FOFF(NThreadBase,iStackBase));
sl@0
   177
	asm("add eax, [ebx+%0]": :"i"_FOFF(NThreadBase,iStackSize));
sl@0
   178
	asm("mov ecx, dword ptr [%a0]": :"i"(&X86_TSS_Ptr));
sl@0
   179
	asm("mov [ecx+%0], eax": :"i"_FOFF(TX86Tss,iEsp0));				// set ESP0 to top of new thread supervisor stack
sl@0
   180
sl@0
   181
	asm("test byte ptr [ebx+%0], 2": :"i"_FOFF(TPriListLink,iSpare2)); // test for address space switch
sl@0
   182
	asm("jz resched_no_as_switch");
sl@0
   183
	asm("call [edi+%0]": :"i"_FOFF(TScheduler,iProcessHandler));	// call handler with
sl@0
   184
																	// EBX=pointer to new thread, EDI->scheduler, preserves ESI, EDI
sl@0
   185
	asm("resched_no_as_switch:");
sl@0
   186
	asm("pop eax");
sl@0
   187
	asm("mov cr0, eax");
sl@0
   188
	asm("pop ebx");
sl@0
   189
	asm("pop esi");
sl@0
   190
	asm("pop edi");
sl@0
   191
	asm("pop ebp");
sl@0
   192
	asm("pop gs");
sl@0
   193
	asm("pop fs");
sl@0
   194
	asm("cli");
sl@0
   195
	asm("lea eax, %a0": : "i"(&TheScheduler.iRescheduleNeededFlag));
sl@0
   196
	asm("cmp dword ptr [eax], 0");
sl@0
   197
	asm("jnz start_resched");
sl@0
   198
	asm("mov eax,0");
sl@0
   199
	asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   200
	asm("pop eax");
sl@0
   201
	asm("ret");
sl@0
   202
sl@0
   203
	asm("round_robin:");
sl@0
   204
	asm("mov eax, [ebx+%0]": : "i"_FOFF(NThreadBase,iHeldFastMutex));
sl@0
   205
	asm("test eax, eax");					// does this thread hold a fast mutex?
sl@0
   206
	asm("jnz rr_holds_fast_mutex");			// branch if it does
sl@0
   207
	asm("mov eax, [ebx+%0]": : "i"_FOFF(NThreadBase,iTimeslice));
sl@0
   208
	asm("mov [ebx+%0], eax": : "i"_FOFF(NThreadBase,iTime)); // else new timeslice for this thread next time
sl@0
   209
	asm("mov ebx, [ebx]");					// candidate thread = next thread in round-robin order
sl@0
   210
	asm("mov [esi], ebx");					// the latter is now the first at this priority
sl@0
   211
	ASM_DEBUG1(RR,ebx);
sl@0
   212
	asm("jmp no_other");
sl@0
   213
sl@0
   214
	asm("resched_blocked:");
sl@0
   215
	ASM_DEBUG1(BlockedFM,eax)
sl@0
   216
	asm("mov edx, [eax+%0]": : "i"_FOFF(NFastMutex,iHoldingThread));
sl@0
   217
	asm("test edx, edx");
sl@0
   218
	asm("jz resched_not_blocked");
sl@0
   219
	asm("mov ebx, edx");
sl@0
   220
	asm("jmp resched_do_thread_switch");
sl@0
   221
sl@0
   222
	asm("holds_fast_mutex:");
sl@0
   223
#ifdef __GCC32__
sl@0
   224
	asm("lea ecx, [edi+%0]": : "i"_FOFF(TScheduler,iLock));
sl@0
   225
#else
sl@0
   226
	_asm lea ecx, [edi]TheScheduler.iLock
sl@0
   227
#endif
sl@0
   228
	asm("cmp eax, ecx");
sl@0
   229
	asm("je resched_do_thread_switch");
sl@0
   230
	asm("test byte ptr [ebx+10], 1");		// test for implicit system lock
sl@0
   231
	asm("jz resched_do_thread_switch");
sl@0
   232
#ifdef __GCC32__
sl@0
   233
	asm("cmp dword ptr [edi+%0], 0": : "i"(_FOFF(TScheduler,iLock) + _FOFF(NFastMutex,iHoldingThread)));
sl@0
   234
#else
sl@0
   235
	_asm cmp [edi]TheScheduler.iLock.iHoldingThread, 0
sl@0
   236
#endif
sl@0
   237
	asm("jz resched_do_thread_switch");
sl@0
   238
sl@0
   239
sl@0
   240
	asm("rr_holds_fast_mutex:");
sl@0
   241
#ifdef __GCC32__
sl@0
   242
	asm("push edx");		// storing an immediate value to an C-offset address appears to be 
sl@0
   243
	asm("mov edx,1");		// impossible in GCC, so we use edx instead
sl@0
   244
	asm("mov [eax+%0], edx": : "i"_FOFF(NFastMutex,iWaiting));
sl@0
   245
	asm("pop edx");
sl@0
   246
#else
sl@0
   247
	_asm mov [eax]NFastMutex.iWaiting, 1
sl@0
   248
#endif
sl@0
   249
	asm("jmp resched_do_thread_switch");
sl@0
   250
sl@0
   251
	asm("resched_not_needed:");
sl@0
   252
	asm("xor eax, eax");
sl@0
   253
	asm("mov [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   254
	asm("pop eax");
sl@0
   255
	asm("ret");
sl@0
   256
	}
sl@0
   257
sl@0
   258
sl@0
   259
/** Disable interrupts to the specified level
sl@0
   260
sl@0
   261
If aLevel = 0 does not affect interrupt state
sl@0
   262
If aLevel <>0 disables all maskable interrupts.
sl@0
   263
sl@0
   264
@param	aLevel level to which to disable
sl@0
   265
@return	Cookie to pass into RestoreInterrupts()
sl@0
   266
*/
sl@0
   267
EXPORT_C __NAKED__ TInt NKern::DisableInterrupts(TInt /*aLevel*/)
sl@0
   268
	{
sl@0
   269
	asm("pushfd");
sl@0
   270
	asm("mov ecx, [esp+4]");
sl@0
   271
	asm("pop eax");
sl@0
   272
	asm("and eax, 0x200");
sl@0
   273
	asm("test ecx, ecx");
sl@0
   274
	asm("jz disable_ints_0");
sl@0
   275
	asm("cli");
sl@0
   276
	asm("disable_ints_0:");
sl@0
   277
	asm("ret");
sl@0
   278
	}
sl@0
   279
sl@0
   280
sl@0
   281
/** Disable all maskable interrupts
sl@0
   282
sl@0
   283
@return	Cookie to pass into RestoreInterrupts()
sl@0
   284
*/
sl@0
   285
EXPORT_C __NAKED__ TInt NKern::DisableAllInterrupts()
sl@0
   286
	{
sl@0
   287
	asm("pushfd");
sl@0
   288
	asm("pop eax");
sl@0
   289
	asm("and eax, 0x200");
sl@0
   290
	asm("cli");
sl@0
   291
	asm("ret");
sl@0
   292
	}
sl@0
   293
sl@0
   294
sl@0
   295
/** Restore interrupt mask to state preceding a DisableInterrupts() call
sl@0
   296
sl@0
   297
@param	aLevel Cookie returned by Disable(All)Interrupts()
sl@0
   298
*/
sl@0
   299
EXPORT_C __NAKED__ void NKern::RestoreInterrupts(TInt aLevel)
sl@0
   300
	{
sl@0
   301
	asm("test byte ptr [esp+5], 2");	// test saved I flag
sl@0
   302
	asm("jz restore_irq_off");			// jump if clear
sl@0
   303
	asm("sti");							// else reenable interrupts
sl@0
   304
	asm("ret");
sl@0
   305
	asm("restore_irq_off:");
sl@0
   306
	asm("cli");
sl@0
   307
	asm("ret");
sl@0
   308
	}
sl@0
   309
sl@0
   310
sl@0
   311
/** Enable all maskable interrupts
sl@0
   312
sl@0
   313
@internalComponent
sl@0
   314
*/
sl@0
   315
EXPORT_C __NAKED__ void NKern::EnableAllInterrupts()
sl@0
   316
	{
sl@0
   317
	asm("sti");
sl@0
   318
	asm("ret");
sl@0
   319
	}
sl@0
   320
sl@0
   321
sl@0
   322
/**	Unlocks the kernel
sl@0
   323
Decrements iKernCSLocked; if it becomes zero and IDFCs or a reschedule are
sl@0
   324
pending, calls the scheduler to process them.
sl@0
   325
sl@0
   326
@pre	Thread or IDFC context. Don't call from ISRs.
sl@0
   327
*/
sl@0
   328
EXPORT_C __NAKED__ void NKern::Unlock()
sl@0
   329
	{
sl@0
   330
	asm("xor eax, eax");
sl@0
   331
	asm("dec dword ptr [%a0]": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   332
	asm("lea edx, %a0": : "i"(&TheScheduler.iRescheduleNeededFlag));
sl@0
   333
	asm("jnz unlock_no_resched");
sl@0
   334
	asm("cmp eax, [edx]");
sl@0
   335
	asm("jz unlock_no_resched");
sl@0
   336
	asm("inc eax");
sl@0
   337
	asm("mov dword ptr [%a0], eax": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   338
	asm("call %a0" : : "i"(TScheduler_Reschedule));
sl@0
   339
	asm("sti");
sl@0
   340
	asm("unlock_no_resched:");
sl@0
   341
	asm("ret");
sl@0
   342
	}
sl@0
   343
sl@0
   344
sl@0
   345
/**	Locks the kernel
sl@0
   346
Increments iKernCSLocked, thereby deferring IDFCs and preemption.
sl@0
   347
sl@0
   348
@pre	Thread or IDFC context. Don't call from ISRs.
sl@0
   349
*/
sl@0
   350
EXPORT_C __NAKED__ void NKern::Lock()
sl@0
   351
	{
sl@0
   352
	asm("inc dword ptr [%a0]": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   353
	asm("ret");
sl@0
   354
	}
sl@0
   355
sl@0
   356
sl@0
   357
/**	Locks the kernel and returns a pointer to the current thread
sl@0
   358
Increments iKernCSLocked, thereby deferring IDFCs and preemption.
sl@0
   359
sl@0
   360
@pre	Thread or IDFC context. Don't call from ISRs.
sl@0
   361
*/
sl@0
   362
EXPORT_C __NAKED__ NThread* NKern::LockC()
sl@0
   363
	{
sl@0
   364
	asm("inc dword ptr [%a0]": :"i"(&TheScheduler.iKernCSLocked));
sl@0
   365
	asm("mov eax, [%a0]": :"i"(&TheScheduler.iCurrentThread));
sl@0
   366
	asm("ret");
sl@0
   367
	}
sl@0
   368
sl@0
   369
sl@0
   370
/**	Allows IDFCs and rescheduling if they are pending.
sl@0
   371
If IDFCs or a reschedule are pending and iKernCSLocked is exactly equal to 1
sl@0
   372
calls the scheduler to process the IDFCs and possibly reschedule.
sl@0
   373
sl@0
   374
@return	Nonzero if a reschedule actually occurred, zero if not.
sl@0
   375
@pre	Thread or IDFC context. Don't call from ISRs.
sl@0
   376
*/
sl@0
   377
EXPORT_C __NAKED__ TInt NKern::PreemptionPoint()
sl@0
   378
	{
sl@0
   379
	asm("mov ecx, %0": : "i"(TheScheduler_iRescheduleNeededFlag));
sl@0
   380
	asm("xor eax, eax");
sl@0
   381
	asm("cmp eax, [ecx]");
sl@0
   382
	asm("jz preemption_point_no_resched");
sl@0
   383
	asm("cmp dword ptr [%a0], 1": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   384
	asm("jne preemption_point_no_resched");
sl@0
   385
	asm("call %a0" : : "i"(TScheduler_Reschedule));
sl@0
   386
	asm("mov dword ptr [%a0], 1": : "i"(&TheScheduler.iKernCSLocked));
sl@0
   387
	asm("sti");
sl@0
   388
sl@0
   389
	asm("preemption_point_no_resched:");
sl@0
   390
	asm("ret");
sl@0
   391
	}