kernel/eka/memmodel/epoc/flexible/arm/xsched.cia
changeset 9 96e5fb8b040d
child 8 538db54a451d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     1 // Copyright (c) 1997-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 //
       
    15 #define INCLUDED_FROM_ASM
       
    16 
       
    17 #include <e32cia.h>
       
    18 #include <arm_mem.h>
       
    19 #include "nk_cpu.h"
       
    20 #include "xdefs.h"
       
    21 #include "cache_maintenance.h"
       
    22 
       
    23 #define iMState		iWaitLink.iSpare1
       
    24 
       
    25 //#define __DEBUG_BAD_ADDR
       
    26 
       
    27 #ifdef __REQUEST_COMPLETE_MACHINE_CODED__
       
    28 #if defined(_DEBUG)
       
    29 extern "C" void __DebugMsgRequestComplete(TInt a0, TInt a1, TInt a2);
       
    30 extern "C" void __DebugMsgReqCompleteWrite(TInt a0, TInt a1, TInt a2);
       
    31 #endif
       
    32 
       
    33 __NAKED__ void DoRequestComplete(DThread* /*aThread*/, TRequestStatus* /*aStatus*/, TInt /*aReason*/)
       
    34 	{
       
    35 	// r0 -> DThread, r1 -> TRequestStatus in user space, r2 = aReason
       
    36 
       
    37 	ASM_ASSERT_DATA_PAGING_SAFE;
       
    38 	
       
    39 #ifdef BTRACE_REQUESTS
       
    40 	asm("stmdb sp!,{r0-r3,lr}");
       
    41 	asm("mov r3,r2");											// arg3 = aReason
       
    42 	asm("mov r2,r1");											// arg2 = aStatus
       
    43 	asm("add r1,r0,#%a0" : : "i" _FOFF(DThread,iNThread));		// arg1 = &this->iNThread
       
    44 	asm("ldr r0,_threadReqequestCompleteTraceHeader");			// arg0 = header
       
    45 	asm("bl " CSM_ZN6BTrace4OutXEmmmm);
       
    46 	asm("ldmia sp!,{r0-r3,lr}");
       
    47 #endif
       
    48 
       
    49 	ASM_DEBUG3(RequestComplete,r0,r1,r2);
       
    50 
       
    51 	asm("ldrb r12, [r0, #%a0]" : : "i" _FOFF(DThread,iMState));
       
    52 #ifdef __SMP__
       
    53 	GET_RWNO_TID(,r3);		// NOTE: Can't migrate because we hold system lock
       
    54 #else
       
    55 	asm("ldr r3, __TheScheduler ");
       
    56 #endif
       
    57 	asm("stmfd sp!, {r4-r9} ");
       
    58 	asm("add r6, r0, #%a0" : : "i" _FOFF(DThread,iNThread));				// r6->target NThread
       
    59 #ifdef __SMP__
       
    60 	asm("ldr r4, [r3, #%a0]" : : "i" _FOFF(TSubScheduler,iAddressSpace));	// r4->current process
       
    61 #else
       
    62 	asm("ldr r4, [r3, #%a0]" : : "i" _FOFF(TScheduler,iAddressSpace));		// r4->current process
       
    63 #endif
       
    64 	asm("ldr r0, [r0, #%a0]" : : "i" _FOFF(DThread,iOwningProcess));		// r0->target process
       
    65 	asm("cmp r12, #%a0" : : "i" (DThread::EDead));	// test if iMState=EDead
       
    66 	asm("beq req_complete_dead_thread ");	// if it is, finished
       
    67 #ifdef __SMP__
       
    68 	asm("ldr r5, [r3, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread));	// r5->current NThread
       
    69 #else
       
    70 	asm("ldr r5, [r3, #%a0]" : : "i" _FOFF(TScheduler,iCurrentThread));		// r5->current NThread
       
    71 #endif
       
    72 
       
    73 	asm("mrc p15, 0, r7, c2, c0, 0 ");		// save TTBR0
       
    74 	asm("ldr r9, [r0, #%a0]" : : "i" _FOFF(DMemModelProcess,iPageDir));		// r9 -> target process page directory
       
    75 	asm("and r12, r7, #%a0" : : "i" ((TInt)KTTBRExtraBitsMask));			// r12 = page table cache attributes
       
    76 	// Get the os asid of the aThread's owning process, no need to open a reference 
       
    77 	// as aThread is not dead and the system lock is held so it can't be made dead.
       
    78 	asm("ldr r8, [r0, #%a0]" : : "i" _FOFF(DMemModelProcess,iOsAsid));		// r8 = target ASID
       
    79 	asm("orr r9, r9, r12 ");				// r9 = new TTBR0 value
       
    80 
       
    81 	__ASM_CLI(); // disable all interrupts
       
    82 	__ASM_SET_ADDRESS_SPACE(r8,r9,r12);
       
    83 #ifdef __SMP__
       
    84 	asm("str r0, [r3, #%a0]" : : "i" _FOFF(TSubScheduler,iAddressSpace));
       
    85 #else
       
    86 	asm("str r0, [r3, #%a0]" : : "i" _FOFF(TScheduler,iAddressSpace));
       
    87 #endif
       
    88 	asm("str r0, [r5, #%a0]" : : "i" _FOFF(NThread,iAddressSpace));
       
    89 	__ASM_STI(); // enable all interrupts
       
    90 
       
    91 	USER_MEMORY_GUARD_OFF(,r12,r12);
       
    92 	asm(".global __magic_address_reqc ");	// NOTE: Z flag always clear here
       
    93 	asm("__magic_address_reqc: ");			// this instruction is magically immune from exceptions
       
    94 	asm("strt r2, [r1] ");					// store completion code with user permissions
       
    95 											// MAY MODIFY Z and R12
       
    96 	USER_MEMORY_GUARD_ON(,r12,r12);
       
    97 
       
    98 	asm("mrs r9, cpsr ");					// save CPSR and Z flag which indicates whether write succeeded
       
    99 	// Read current process's os asid no reference required as it can't be freed as it is the current process. 
       
   100 	asm("ldr r2, [r4, #%a0]" : : "i" _FOFF(DMemModelProcess,iOsAsid));		// r2 = current process ASID
       
   101 
       
   102 	__ASM_CLI(); // disable all interrupts
       
   103 	__ASM_SET_ADDRESS_SPACE(r2,r7,r12);
       
   104 #ifdef __SMP__
       
   105 	asm("str r4, [r3, #%a0]" : : "i" _FOFF(TSubScheduler,iAddressSpace));
       
   106 #else
       
   107 	asm("str r4, [r3, #%a0]" : : "i" _FOFF(TScheduler,iAddressSpace));
       
   108 #endif
       
   109 	asm("str r4, [r5, #%a0]" : : "i" _FOFF(NThread,iAddressSpace));
       
   110 
       
   111 	asm("msr cpsr, r9 ");					// restore interrupts and Z flag
       
   112 
       
   113 #ifdef __DEBUG_BAD_ADDR
       
   114 	asm("moveq r12, #0xde000000 ");
       
   115 	asm("streq r12, [r12, #0xaf] ");		//HACK-CRASH SYSTEM IF WRITE FAILED
       
   116 #endif
       
   117 
       
   118 	asm("movne r0, r6 ");					// if write OK, r0->iNThread
       
   119 	asm("req_complete_dead_thread: ");		// NOTE: Z flag set if thread dead
       
   120 	asm("ldmfd sp!, {r4-r9} ");				// restore registers whether OK or not
       
   121 	asm("movne r1, #0 ");
       
   122 	asm("bne " CSM_ZN5NKern19ThreadRequestSignalEP7NThreadP10NFastMutex );
       
   123 	asm("b " CSM_ZN5NKern12UnlockSystemEv);	// if any error, unlock system and return
       
   124 
       
   125 #ifdef BTRACE_REQUESTS
       
   126 	asm("_threadReqequestCompleteTraceHeader:");
       
   127 	asm(".word %a0" : : "i" (BTRACE_HEADER_C(16,BTrace::ERequests,BTrace::ERequestComplete)));
       
   128 #endif
       
   129 	}
       
   130 #endif	//	__REQUEST_COMPLETE_MACHINE_CODED__
       
   131 
       
   132 
       
   133 GLDEF_C __NAKED__ void DoProcessSwitch()
       
   134 	{
       
   135 	// Enter and return with kernel locked
       
   136 	// UP: r0->scheduler, r3->current thread r2 = ContextID>>8
       
   137 	// SMP: r11->subscheduler, r3->current thread r2 = ContextID>>8
       
   138 	// r5->old process, r9->new process
       
   139 	// UP: Must preserve r0,r3, can modify other registers
       
   140 	// SMP: Must preserve r3,r10,r11, can modify other registers
       
   141 
       
   142 	// Read the new process os asid, no need to open a reference as it is about 
       
   143 	// to become the current thread.
       
   144 	asm("ldr r4, [r9, #%a0]" : : "i" _FOFF(DMemModelProcess, iOsAsid));
       
   145 	asm("ldr r6, [r3, #%a0]" : : "i" (_FOFF(DMemModelThread, iAliasLinAddr)-_FOFF(DThread, iNThread)) );
       
   146 	asm("cmp r5, r9 ");					// check if current address space correct
       
   147 	asm("orr r2, r4, r2, lsl #8 ");		// r2 = ContextID :  ASID
       
   148 	asm("beq same_process");			// skip if address space change not required
       
   149 
       
   150 	// switching to another process...
       
   151 	asm("ldr r7, [r9, #%a0]" : : "i" _FOFF(DMemModelProcess, iPageDir));
       
   152 	asm("mrc p15, 0, r8, c2, c0, 0 ");	// read current TTBR0
       
   153 	asm("and r8, r8, #%a0" : : "i" ((TInt)KTTBRExtraBitsMask));
       
   154 	asm("orr r7, r7, r8 ");
       
   155 	// r7 now = new TTBR0
       
   156 
       
   157 	__ASM_SET_ADDRESS_SPACE(r2,r7,r12);
       
   158 #ifdef __SMP__
       
   159 	asm("str r9, [r11, #%a0]" : : "i" _FOFF(TSubScheduler, iAddressSpace));
       
   160 #else
       
   161 	asm("str r9, [r0, #%a0]" : : "i" _FOFF(TScheduler, iAddressSpace));
       
   162 #endif
       
   163 
       
   164 	asm("cmp r6, #0");					// check if thread has an alias in place
       
   165 	asm("bne got_alias");
       
   166 	__JUMP(,lr);
       
   167 
       
   168 	// 'switching' to same process, just update debug thread ID...
       
   169 	asm("same_process:");
       
   170 	__DATA_SYNC_BARRIER_Z__(r12);		// needed before change to ContextID
       
   171 	asm("mcr p15, 0, r2, c13, c0, 1 ");	// set ASID + debugging thread ID
       
   172 	__INST_SYNC_BARRIER__(r12);
       
   173 	asm("cmp r6, #0");					// check if thread has an alias in place
       
   174 	asm("bne got_alias");
       
   175 	__JUMP(,lr);
       
   176 
       
   177 	// restore alias...
       
   178 	asm("got_alias:");
       
   179 	asm("ldr r8, [r3, #%a0]" : : "i" (_FOFF(DMemModelThread, iAliasPdePtr)-_FOFF(DThread, iNThread)) );
       
   180 	asm("ldr r7, [r3, #%a0]" : : "i" (_FOFF(DMemModelThread, iAliasPde)-_FOFF(DThread, iNThread)) );
       
   181 	asm("orr r6, r6, r4");				// put ASID into address for TLB flush later...
       
   182 	asm("str r7, [r8]");				// restore PDE for alias
       
   183 	
       
   184 	CACHE_MAINTENANCE_PDE_PTE_UPDATED(r8);
       
   185 
       
   186 #if defined(__CPU_ARM11MP__) // why?...
       
   187 	asm("mcr p15, 0, r6, c8, c7, 3 ");	// flush TLB for aliased page
       
   188 #else
       
   189 	asm("mcr p15, 0, r6, c8, c7, 1 ");	// flush TLB for aliased page
       
   190 #endif
       
   191 	asm("mov r8,#0");
       
   192 	asm("mcr p15, 0, r8, c7, c5, 6 ");	// BPIALL (Branch predictor invalidate all)
       
   193 	__DATA_SYNC_BARRIER__(r8);
       
   194 	__INST_SYNC_BARRIER__(r8);
       
   195 	__JUMP(,lr);
       
   196 
       
   197 #ifndef __SMP__
       
   198 	asm("__TheScheduler: ");
       
   199 	asm(".word TheScheduler ");
       
   200 #endif
       
   201 	};
       
   202 
       
   203 /**
       
   204 Restore the address space of the current thread so it matches its owning process.
       
   205 */
       
   206 __NAKED__ void DMemModelThread::RestoreAddressSpace()
       
   207 	{
       
   208 	asm("stmfd sp!, {lr} ");
       
   209 #ifdef __SMP__
       
   210 	GET_RWNO_TID(,r12);
       
   211 	asm("ldr r3, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread));	// r3->current NThread
       
   212 #else
       
   213 	asm("ldr r12, __TheScheduler ");										// r12->TheScheduler
       
   214 	asm("ldr r3, [r12, #%a0]" : : "i" _FOFF(TScheduler,iCurrentThread));	// r3->current NThread
       
   215 #endif
       
   216 	asm("ldr r0, [r3, #%a0]" : : "i" (-_FOFF(DThread,iNThread)+_FOFF(DThread,iOwningProcess)));		// r0->process
       
   217 
       
   218 	// switch address space to process r0...
       
   219 	asm("ldr r1, [r0, #%a0]" : : "i" _FOFF(DMemModelProcess,iPageDir));	// r1->process page directory
       
   220 	asm("mrc p15, 0, r2, c2, c0, 0 ");
       
   221 	asm("and r2, r2, #%a0" : : "i" ((TInt)KTTBRExtraBitsMask));
       
   222 	asm("orr r1, r1, r2 ");
       
   223 	// Get the os asid of the current process, no need to open a reference as it is the current process.
       
   224 	asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(DMemModelProcess,iOsAsid));		// r2 = ASID
       
   225 
       
   226 	__ASM_CLI(); // disable all interrupts
       
   227 	__ASM_SET_ADDRESS_SPACE(r2,r1,lr);
       
   228 #ifdef __SMP__
       
   229 	asm("str r0, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iAddressSpace));
       
   230 #else
       
   231 	asm("str r0, [r12, #%a0]" : : "i" _FOFF(TScheduler,iAddressSpace));
       
   232 #endif
       
   233 	asm("str r0, [r3, #%a0]" : : "i" _FOFF(NThread,iAddressSpace));
       
   234 	__ASM_STI(); // enable all interrupts
       
   235 
       
   236 	__POPRET("");
       
   237 	}