author | William Roberts <williamr@symbian.org> |
Wed, 21 Jul 2010 14:14:06 +0100 | |
changeset 217 | 96e53742b989 |
parent 201 | 43365a9b78a3 |
child 221 | 39b39e1a406e |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2008-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\nkernsmp\arm\ncsched.cia |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
// NThreadBase member data |
|
19 |
#define __INCLUDE_NTHREADBASE_DEFINES__ |
|
20 |
||
21 |
// TDfc member data |
|
22 |
#define __INCLUDE_TDFC_DEFINES__ |
|
23 |
||
24 |
#include <e32cia.h> |
|
25 |
#include <arm.h> |
|
26 |
#include "nkern.h" |
|
27 |
#include <arm_gic.h> |
|
28 |
#include <arm_scu.h> |
|
29 |
#include <arm_tmr.h> |
|
30 |
//#include <highrestimer.h> |
|
31 |
//#include "emievents.h" |
|
32 |
||
33 |
#ifdef _DEBUG |
|
34 |
#define ASM_KILL_LINK(rp,rs) asm("mov "#rs", #0xdf ");\ |
|
35 |
asm("orr "#rs", "#rs", "#rs", lsl #8 ");\ |
|
36 |
asm("orr "#rs", "#rs", "#rs", lsl #16 ");\ |
|
37 |
asm("str "#rs", ["#rp"] ");\ |
|
38 |
asm("str "#rs", ["#rp", #4] "); |
|
39 |
#else |
|
40 |
#define ASM_KILL_LINK(rp,rs) |
|
41 |
#endif |
|
42 |
||
43 |
#define ALIGN_STACK_START \ |
|
44 |
asm("mov r12, sp"); \ |
|
45 |
asm("tst sp, #4"); \ |
|
46 |
asm("subeq sp, sp, #4"); \ |
|
47 |
asm("str r12, [sp,#-4]!") |
|
48 |
||
49 |
#define ALIGN_STACK_END \ |
|
50 |
asm("ldr sp, [sp]") |
|
51 |
||
52 |
||
53 |
//#define __DEBUG_BAD_ADDR |
|
54 |
||
55 |
extern "C" void NewThreadTrace(NThread* a); |
|
56 |
extern "C" void send_accumulated_resched_ipis(); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
57 |
extern "C" void wake_up_for_ipi(TSubScheduler*, TInt); |
0 | 58 |
|
59 |
||
60 |
__NAKED__ void TScheduler::Reschedule() |
|
61 |
{ |
|
62 |
// |
|
63 |
// Enter in mode_svc with kernel locked, interrupts can be on or off |
|
64 |
// Exit in mode_svc with kernel unlocked, interrupts off |
|
65 |
// NOTE: STACK ALIGNMENT UNKNOWN ON ENTRY |
|
66 |
// NOTE: R4-R11 are modified |
|
67 |
// |
|
68 |
asm("mov r2, sp "); // bit 0 will be reschedule flag |
|
69 |
asm("bic sp, sp, #4 "); // align stack |
|
70 |
GET_RWNO_TID(,r0) // r0->TSubScheduler |
|
71 |
asm("stmfd sp!, {r2,lr} "); // save original SP/resched flag, return address |
|
72 |
__ASM_CLI(); // interrupts off |
|
73 |
asm("ldr r1, [r0, #%a0]" : : "i" (_FOFF(TSubScheduler,iDfcPendingFlag)&~3)); // check iDfcPendingFlag and iExIDfcPendingFlag |
|
74 |
asm("mov r11, r0 "); // r11->TSubScheduler |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
75 |
asm("ldr r10, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iSSX.iLocalTimerAddr)); // r10->CPU local timer |
0 | 76 |
|
77 |
asm("start_resched: "); |
|
78 |
asm("movs r1, r1, lsr #16 "); // check if IDFCs or ExIDFCs pending |
|
79 |
||
80 |
asm("blne " CSM_ZN13TSubScheduler9QueueDfcsEv); // queue any pending DFCs |
|
81 |
asm("ldrb r1, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iRescheduleNeededFlag)); |
|
82 |
asm("ldr r3, [sp, #0] "); |
|
83 |
asm("mrs r2, spsr "); // r2=spsr_svc |
|
84 |
asm("cmp r1, #0 "); // check if a reschedule is required |
|
85 |
asm("beq no_resched_needed "); // branch out if not |
|
86 |
__ASM_STI(); // interrupts back on |
|
87 |
asm("orr r3, r3, #1 "); |
|
88 |
asm("str r3, [sp, #0] "); // set resched flag |
|
89 |
asm("stmfd sp!, {r0,r2} "); // store SPSR_SVC |
|
90 |
asm("ldr r5, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread)); |
|
91 |
#ifdef __CPU_ARMV7 |
|
92 |
asm("mrc p14, 6, r7, c1, c0, 0 "); // r7 = TEEHBR |
|
93 |
#else |
|
94 |
asm("mov r7, #0 "); |
|
95 |
#endif |
|
96 |
GET_RWRO_TID(,r8); // r8 = User RO Thread ID |
|
97 |
GET_RWRW_TID(,r9); // r9 = User RW Thread ID |
|
98 |
#ifdef __CPU_HAS_VFP |
|
99 |
VFP_FMRX(,0,VFP_XREG_FPEXC); // r0 = FPEXC |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
asm("bic r0, r0, #%a0" : : "i" ((TInt)VFP_FPEXC_EN) ); // Store FPEXC with VFP disabled in case this thread runs on a different core next time |
0 | 101 |
#else |
102 |
asm("mov r0, #0 "); |
|
103 |
#endif |
|
104 |
GET_CAR(, r1); // r1 = CAR |
|
105 |
asm("mrc p15, 0, r12, c3, c0, 0 "); // r12 = DACR |
|
106 |
asm("ldr r4, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iInitialThread)); |
|
107 |
||
108 |
// Save auxiliary registers |
|
109 |
// R0=FPEXC, R1=CAR, R7=TEEHBR, R8=RWROTID, R9=RWRWTID, R12=DACR |
|
110 |
asm("sub sp, sp, #%a0" : : "i" _FOFF(SThreadReschedStack,iSpare)); |
|
111 |
asm("str sp, [r5, #%a0]" : : "i" _FOFF(NThread,iSavedSP)); // store original thread's stack pointer |
|
112 |
asm("stmia sp, {r0-r1,r7-r9,r12} "); |
|
113 |
||
114 |
// We must move to a temporary stack before selecting the next thread. |
|
115 |
// This is because another CPU may begin executing this thread before the |
|
116 |
// select_next_thread() function returns and our stack would then be |
|
117 |
// corrupted. We use the stack belonging to this CPU's initial thread since |
|
118 |
// we are guaranteed that will never run on another CPU. |
|
119 |
asm("ldr sp, [r4, #%a0]" : : "i" _FOFF(NThread,iSavedSP)); |
|
120 |
||
121 |
asm("select_thread: "); |
|
122 |
asm("mov r0, r11 "); |
|
123 |
asm("bl " CSM_ZN13TSubScheduler16SelectNextThreadEv ); // also sets r0->iCurrentThread |
|
124 |
#ifdef BTRACE_CPU_USAGE |
|
125 |
asm("ldr r2, __BTraceFilter "); |
|
126 |
#endif |
|
127 |
asm("movs r3, r0 "); // r3 = new thread (might be 0) |
|
128 |
asm("ldrne sp, [r0, #%a0]" : : "i" _FOFF(NThread,iSavedSP)); // if a thread has been selected, move to its stack |
|
129 |
asm("beq no_thread "); // branch out if no thread is ready |
|
130 |
||
131 |
#ifdef BTRACE_CPU_USAGE |
|
132 |
asm("ldrb r1, [r2, #4] "); // check category 4 trace |
|
133 |
asm("cmp r1, #0 "); |
|
134 |
asm("beq 1f "); |
|
135 |
asm("stmfd sp!, {r0-r3} "); |
|
136 |
asm("bl NewThreadTrace "); |
|
137 |
asm("ldmfd sp!, {r0-r3} "); |
|
138 |
asm("1: "); |
|
139 |
#endif // BTRACE_CPU_USAGE |
|
140 |
||
141 |
asm("cmp r3, r5 "); // same thread? |
|
142 |
asm("beq same_thread "); |
|
143 |
asm("ldrb r1, [r3, #%a0]" : : "i" _FOFF(NThreadBase, i_ThrdAttr)); |
|
144 |
asm("ldr r4, [r11, #%a0]" : : "i" _FOFF(TSubScheduler, iScheduler)); |
|
145 |
asm("mov r2, r3, lsr #6 "); // r2=current thread>>6 |
|
146 |
asm("tst r1, #%a0" : : "i" ((TInt)KThreadAttAddressSpace)); // address space required? |
|
147 |
asm("ldrne r4, [r4, #%a0]" : : "i" _FOFF(TScheduler,iProcessHandler)); // if so, get pointer to process handler |
|
148 |
||
149 |
// we are doing a thread switch so restore new thread's auxiliary registers |
|
150 |
// R0=FPEXC, R1=CAR, R7=TEEHBR, R8=RWROTID, R9=RWRWTID, R12=DACR |
|
151 |
asm("ldmia sp, {r0-r1,r7-r9,r12} "); |
|
152 |
||
153 |
#ifdef __CPU_ARMV7 |
|
154 |
asm("mcr p14, 6, r7, c1, c0, 0 "); // r7 = TEEHBR |
|
155 |
#endif |
|
156 |
SET_RWRO_TID(,r8); // r8 = User RO Thread ID |
|
157 |
SET_RWRW_TID(,r9); // r9 = User RW Thread ID |
|
158 |
#ifdef __CPU_HAS_VFP |
|
159 |
VFP_FMXR(,VFP_XREG_FPEXC,0); // r0 = FPEXC |
|
160 |
#endif |
|
161 |
SET_CAR(, r1); // r1 = CAR |
|
162 |
asm("mcr p15, 0, r12, c3, c0, 0 "); // r12 = DACR |
|
163 |
||
164 |
asm("beq no_as_switch "); // skip if address space change not required |
|
165 |
||
166 |
// Do address space switching |
|
167 |
// Handler called with: |
|
168 |
// r11->subscheduler, r3->current thread |
|
169 |
// r9->new address space, r5->old address space |
|
170 |
// Must preserve r10,r11,r3, can modify other registers |
|
171 |
asm("ldr r5, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iAddressSpace)); // get current address space ptr |
|
172 |
asm("ldr r9, [r3, #%a0]" : : "i" _FOFF(NThreadBase,iAddressSpace)); // get new address space ptr |
|
173 |
asm("adr lr, as_switch_return "); |
|
174 |
__JUMP(, r4); |
|
175 |
||
176 |
asm("no_as_switch: "); |
|
177 |
asm("mrc p15, 0, r4, c13, c0, 1 "); // r4 = CONTEXTID (threadID:ASID) |
|
178 |
asm("and r4, r4, #0xff "); // isolate ASID |
|
179 |
asm("orr r2, r4, r2, lsl #8 "); // r2 = new ContextID (new thread ID : ASID) |
|
180 |
__DATA_SYNC_BARRIER_Z__(r12); // needed before change to ContextID |
|
181 |
asm("mcr p15, 0, r2, c13, c0, 1 "); // set ContextID (ASID + debugging thread ID) |
|
182 |
__INST_SYNC_BARRIER__(r12); |
|
183 |
#ifdef __CPU_NEEDS_BTAC_FLUSH_AFTER_ASID_CHANGE |
|
184 |
asm("mcr p15, 0, r12, c7, c5, 6 "); // flush BTAC |
|
185 |
#endif |
|
186 |
||
187 |
asm("as_switch_return: "); |
|
188 |
asm("same_thread: "); |
|
189 |
asm("add sp, sp, #%a0" : : "i" _FOFF(SThreadReschedStack,iSpare)); // step past auxiliary registers |
|
190 |
asm("ldmib sp!, {r2,r12} "); // r2=SPSR_SVC, r12=original SP + resched flag |
|
191 |
__ASM_CLI(); // interrupts off |
|
192 |
asm("ldr r1, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iRescheduleNeededFlag)); |
|
193 |
asm("msr spsr, r2 "); // restore spsr_svc |
|
194 |
asm("mov r0, r11 "); |
|
195 |
asm("mov r2, r12 "); // r2 = original SP + reschedule flag |
|
196 |
asm("cmp r1, #0 "); // check for more IDFCs and/or another reschedule |
|
197 |
asm("bne start_resched "); // loop if required |
|
198 |
asm("ldr r14, [r3, #%a0]" : : "i" _FOFF(NThreadBase,iCsFunction)); |
|
199 |
asm("ldr r12, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
200 |
asm("str r1, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
201 |
asm("cmp r14, #%a0" : : "i" ((TInt)NThreadBase::ECSDivertPending)); |
|
202 |
asm("ldr lr, [sp, #4] "); // restore R10, R11, return address |
|
203 |
asm("bic sp, r2, #3 "); // restore initial unaligned stack pointer |
|
204 |
asm("and r2, r2, #1 "); // r2 = reschedule flag |
|
205 |
asm("beq resched_thread_divert "); |
|
206 |
||
207 |
// Return with: R0=&SubScheduler, R1=0, R2=TRUE if reschedule occurred, R3=iCurrentThread |
|
208 |
// R12=iReschedIPIs |
|
209 |
__JUMP(, lr); |
|
210 |
||
211 |
asm("no_resched_needed: "); |
|
212 |
asm("ldr r3, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread)); |
|
213 |
asm("ldr r12, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
214 |
asm("mov r0, r11 "); |
|
215 |
asm("str r1, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
216 |
asm("ldr r2, [r3, #%a0]" : : "i" _FOFF(NThreadBase,iCsFunction)); |
|
217 |
asm("cmp r2, #%a0" : : "i" ((TInt)NThreadBase::ECSDivertPending)); |
|
218 |
asm("ldmfd sp, {r2,lr} "); // r2 = original SP + reschedule flag, restore lr |
|
219 |
asm("bic sp, r2, #3 "); // restore initial unaligned stack pointer |
|
220 |
asm("and r2, r2, #1 "); // r2 = reschedule flag |
|
221 |
asm("beq resched_thread_divert "); |
|
222 |
||
223 |
// Return with: R0=&SubScheduler, R1=0, R2=TRUE if reschedule occurred, R3=iCurrentThread |
|
224 |
// R12=iReschedIPIs |
|
225 |
__JUMP(, lr); |
|
226 |
||
227 |
asm("resched_thread_divert: "); |
|
228 |
asm("mov r1, #1 "); |
|
229 |
asm("str r1, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
230 |
asm("bic sp, sp, #4 "); // align stack |
|
231 |
asm("stmfd sp!, {r0-r5,r12,lr} "); // save registers for diagnostic purposes |
|
232 |
asm("mov r4, r3 "); // don't really need to bother about registers since thread is exiting |
|
233 |
||
234 |
// need to send any outstanding reschedule IPIs |
|
235 |
asm("cmp r12, #0 "); |
|
236 |
asm("blne " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
237 |
||
238 |
__ASM_STI(); |
|
239 |
asm("ldrb r1, [r4, #%a0]" : : "i" _FOFF(NThreadBase,iFastMutexDefer)); |
|
240 |
asm("cmp r1, #1 "); |
|
241 |
asm("bne 1f "); |
|
242 |
__ASM_CRASH(); |
|
243 |
asm("1: "); |
|
244 |
asm("mov r2, #0 "); |
|
245 |
asm("strb r2, [r4, #%a0]" : : "i" _FOFF(NThreadBase,iFastMutexDefer)); |
|
246 |
asm("mov r0, r4 "); |
|
247 |
asm("bl " CSM_ZN11NThreadBase4ExitEv ); |
|
248 |
__ASM_CRASH(); // shouldn't get here |
|
249 |
||
250 |
// There is no thread ready to run |
|
251 |
// R11->TSubScheduler, R1=unknown, R2=0, R3=__BTraceFilter, R12=unknown |
|
252 |
asm("no_thread: "); |
|
253 |
__ASM_CLI(); |
|
254 |
asm("ldr r12, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
255 |
asm("mov r0, r11 "); |
|
256 |
asm("cmp r12, #0 "); |
|
257 |
asm("blne " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
258 |
__ASM_STI(); |
|
259 |
__DATA_SYNC_BARRIER_Z__(r1); |
|
260 |
ARM_WFI; |
|
261 |
asm("no_thread2: "); |
|
262 |
asm("ldr r1, [r11, #%a0]" : : "i" (_FOFF(TSubScheduler,iDfcPendingFlag)&~3)); // check iDfcPendingFlag and iExIDfcPendingFlag |
|
263 |
asm("mov r0, r11 "); |
|
264 |
asm("movs r1, r1, lsr #16 "); |
|
265 |
asm("beq no_thread "); |
|
266 |
asm("bl " CSM_ZN13TSubScheduler9QueueDfcsEv); // queue any pending DFCs |
|
267 |
asm("ldrb r1, [r11, #%a0]" : : "i" _FOFF(TSubScheduler,iRescheduleNeededFlag)); |
|
268 |
asm("cmp r1, #0 "); // check if a reschedule is required |
|
269 |
asm("beq no_thread2 "); |
|
270 |
asm("b select_thread "); |
|
271 |
||
272 |
||
273 |
||
274 |
/****************************************************************************** |
|
275 |
Missed out stuff: |
|
276 |
EMI EVENT LOGGING |
|
277 |
__CPU_ARM1136_ERRATUM_351912_FIXED |
|
278 |
Debug hooks in the scheduler |
|
279 |
******************************************************************************/ |
|
280 |
||
281 |
asm("__BTraceFilter: "); |
|
282 |
asm(".word %a0 " : : "i" ((TInt)&BTraceData.iFilter[0])); |
|
283 |
}; |
|
284 |
||
285 |
||
286 |
/** |
|
287 |
* Returns the range of linear memory which inserting the scheduler hooks needs to modify. |
|
288 |
* |
|
289 |
* @param aStart Set to the lowest memory address which needs to be modified. |
|
290 |
* @param aEnd Set to the highest memory address +1 which needs to be modified. |
|
291 |
||
292 |
@pre Kernel must be locked. |
|
293 |
@pre Call in a thread context. |
|
294 |
@pre Interrupts must be enabled. |
|
295 |
*/ |
|
296 |
EXPORT_C __NAKED__ void NKern::SchedulerHooks(TLinAddr& aStart, TLinAddr& aEnd) |
|
297 |
{ |
|
298 |
#if 0 |
|
299 |
ASM_CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC); |
|
300 |
#ifdef __DEBUGGER_SUPPORT__ |
|
301 |
asm("adr r2,resched_trampoline_hook_address"); |
|
302 |
asm("str r2,[r0]"); |
|
303 |
asm("adr r2,resched_trampoline_hook_address+4"); |
|
304 |
asm("str r2,[r1]"); |
|
305 |
#else |
|
306 |
asm("mov r2,#0"); |
|
307 |
asm("str r2,[r0]"); |
|
308 |
asm("str r2,[r1]"); |
|
309 |
#endif |
|
310 |
#endif |
|
311 |
__JUMP(,lr); |
|
312 |
}; |
|
313 |
||
314 |
||
315 |
/** |
|
316 |
* Modifies the scheduler code so that it can call the function set by |
|
317 |
* NKern::SetRescheduleCallback(). |
|
318 |
* |
|
319 |
* This requires that the region of memory indicated by NKern::SchedulerHooks() is writable. |
|
320 |
||
321 |
@pre Kernel must be locked. |
|
322 |
@pre Call in a thread context. |
|
323 |
@pre Interrupts must be enabled. |
|
324 |
*/ |
|
325 |
EXPORT_C __NAKED__ void NKern::InsertSchedulerHooks() |
|
326 |
{ |
|
327 |
#if 0 |
|
328 |
ASM_CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC); |
|
329 |
#ifdef __DEBUGGER_SUPPORT__ |
|
330 |
asm("adr r0,resched_trampoline_hook_address"); |
|
331 |
asm("adr r1,resched_trampoline"); |
|
332 |
asm("sub r1, r1, r0"); |
|
333 |
asm("sub r1, r1, #8"); |
|
334 |
asm("mov r1, r1, asr #2"); |
|
335 |
asm("add r1, r1, #0xea000000"); // r1 = a branch instruction from resched_trampoline_hook_address to resched_trampoline |
|
336 |
||
337 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) |
|
338 |
// These platforms have shadow memory in non-writable page. We cannot use the standard |
|
339 |
// Epoc::CopyToShadowMemory interface as we hold Kernel lock here. |
|
340 |
// Instead, we'll temporarily disable access permission checking in MMU by switching |
|
341 |
// domain#0 into Manager Mode (see Domain Access Control Register). |
|
342 |
asm("mrs r12, CPSR "); // save cpsr setting and ... |
|
343 |
CPSIDAIF; // ...disable interrupts |
|
344 |
asm("mrc p15, 0, r2, c3, c0, 0 "); // read DACR |
|
345 |
asm("orr r3, r2, #3"); // domain #0 is the first two bits. manager mode is 11b |
|
346 |
asm("mcr p15, 0, r3, c3, c0, 0 "); // write DACR |
|
347 |
asm("str r1,[r0]"); |
|
348 |
asm("mcr p15, 0, r2, c3, c0, 0 "); // write back the original value of DACR |
|
349 |
asm("msr CPSR_cxsf, r12 "); // restore cpsr setting (re-enable interrupts) |
|
350 |
#else |
|
351 |
asm("str r1,[r0]"); |
|
352 |
#endif |
|
353 |
||
354 |
#endif |
|
355 |
#endif |
|
356 |
__JUMP(,lr); |
|
357 |
}; |
|
358 |
||
359 |
||
360 |
/** |
|
361 |
* Reverts the modification of the Scheduler code performed by NKern::InsertSchedulerHooks() |
|
362 |
* |
|
363 |
* This requires that the region of memory indicated by NKern::SchedulerHooks() is writable. |
|
364 |
||
365 |
@pre Kernel must be locked. |
|
366 |
@pre Call in a thread context. |
|
367 |
@pre Interrupts must be enabled. |
|
368 |
*/ |
|
369 |
EXPORT_C __NAKED__ void NKern::RemoveSchedulerHooks() |
|
370 |
{ |
|
371 |
#if 0 |
|
372 |
ASM_CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC); |
|
373 |
#ifdef __DEBUGGER_SUPPORT__ |
|
374 |
asm("adr r0,resched_trampoline_hook_address"); |
|
375 |
asm("ldr r1,resched_trampoline_unhook_data"); |
|
376 |
||
377 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) |
|
378 |
// See comments above in InsertSchedulerHooks |
|
379 |
asm("mrs r12, CPSR "); // save cpsr setting and ... |
|
380 |
CPSIDAIF; // ...disable interrupts |
|
381 |
asm("mrc p15, 0, r2, c3, c0, 0 "); // read DACR |
|
382 |
asm("orr r3, r2, #3"); // domain #0 is the first two bits. manager mode is 11b |
|
383 |
asm("mcr p15, 0, r3, c3, c0, 0 "); // write DACR |
|
384 |
asm("str r1,[r0]"); |
|
385 |
asm("mcr p15, 0, r2, c3, c0, 0 "); // write back the original value of DACR |
|
386 |
asm("msr CPSR_cxsf, r12 "); // restore cpsr setting (re-enable interrupts) |
|
387 |
#else |
|
388 |
asm("str r1,[r0]"); |
|
389 |
#endif |
|
390 |
||
391 |
#endif |
|
392 |
#endif |
|
393 |
__JUMP(,lr); |
|
394 |
}; |
|
395 |
||
396 |
||
397 |
/** |
|
398 |
* Set the function which is to be called on every thread reschedule. |
|
399 |
* |
|
400 |
* @param aCallback Pointer to callback function, or NULL to disable callback. |
|
401 |
||
402 |
@pre Kernel must be locked. |
|
403 |
@pre Call in a thread context. |
|
404 |
@pre Interrupts must be enabled. |
|
405 |
*/ |
|
406 |
EXPORT_C __NAKED__ void NKern::SetRescheduleCallback(TRescheduleCallback /*aCallback*/) |
|
407 |
{ |
|
408 |
#if 0 |
|
409 |
ASM_CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC); |
|
410 |
#ifdef __DEBUGGER_SUPPORT__ |
|
411 |
asm("ldr r1, __TheScheduler "); |
|
412 |
asm("str r0, [r1, #%a0]" : : "i" _FOFF(TScheduler,iRescheduleHook)); |
|
413 |
#endif |
|
414 |
#endif |
|
415 |
__JUMP(,lr); |
|
416 |
}; |
|
417 |
||
418 |
||
419 |
||
420 |
/** Disables interrupts to specified level. |
|
421 |
||
422 |
Note that if we are not disabling all interrupts we must lock the kernel |
|
423 |
here, otherwise a high priority interrupt which is still enabled could |
|
424 |
cause a reschedule and the new thread could then reenable interrupts. |
|
425 |
||
426 |
@param aLevel Interrupts are disbabled up to and including aLevel. On ARM, |
|
427 |
level 1 stands for IRQ only and level 2 stands for IRQ and FIQ. |
|
428 |
@return CPU-specific value passed to RestoreInterrupts. |
|
429 |
||
430 |
@pre 1 <= aLevel <= maximum level (CPU-specific) |
|
431 |
||
432 |
@see NKern::RestoreInterrupts() |
|
433 |
*/ |
|
434 |
EXPORT_C __NAKED__ TInt NKern::DisableInterrupts(TInt /*aLevel*/) |
|
435 |
{ |
|
436 |
#ifdef __FIQ_IS_UNCONTROLLED__ |
|
437 |
asm("mrs r1, cpsr "); |
|
438 |
asm("cmp r0, #0 "); |
|
439 |
asm("beq 1f "); |
|
440 |
__ASM_CLI(); |
|
441 |
asm("1: "); |
|
442 |
asm("and r0, r1, #%a0" : : "i" ((TInt)KAllInterruptsMask)); /* return original CPSR I and F bits */ |
|
443 |
__JUMP(, lr); |
|
444 |
#else |
|
445 |
asm("cmp r0, #1 "); |
|
446 |
asm("bhi " CSM_ZN5NKern20DisableAllInterruptsEv); // if level>1, disable all |
|
447 |
asm("mrs r2, cpsr "); // r2=original CPSR |
|
448 |
asm("bcc 1f "); // skip if level=0 |
|
449 |
__ASM_CLI(); // Disable all interrupts to prevent migration |
|
450 |
GET_RWNO_TID(,r12); // r12 -> TSubScheduler |
|
451 |
asm("ldr r3, [r12, #%a0]!" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
452 |
asm("and r0, r2, #0xc0 "); |
|
453 |
asm("cmp r3, #0 "); // test if kernel locked |
|
454 |
asm("addeq r3, r3, #1 "); // if not, lock the kernel |
|
455 |
asm("streq r3, [r12] "); |
|
456 |
asm("orreq r0, r0, #0x80000000 "); // and set top bit to indicate kernel locked |
|
457 |
__ASM_STI2(); // reenable FIQs only |
|
458 |
__JUMP(, lr); |
|
459 |
asm("1: "); |
|
460 |
asm("and r0, r2, #0xc0 "); |
|
461 |
__JUMP(, lr); |
|
462 |
#endif |
|
463 |
} |
|
464 |
||
465 |
||
466 |
/** Disables all interrupts (e.g. both IRQ and FIQ on ARM). |
|
467 |
||
468 |
@return CPU-specific value passed to NKern::RestoreInterrupts(). |
|
469 |
||
470 |
@see NKern::RestoreInterrupts() |
|
471 |
*/ |
|
472 |
EXPORT_C __NAKED__ TInt NKern::DisableAllInterrupts() |
|
473 |
{ |
|
474 |
asm("mrs r1, cpsr "); |
|
475 |
asm("and r0, r1, #%a0" : : "i" ((TInt)KAllInterruptsMask)); /* return original CPSR I and F bits */ |
|
476 |
__ASM_CLI(); |
|
477 |
__JUMP(,lr); |
|
478 |
} |
|
479 |
||
480 |
||
481 |
/** Enables all interrupts (e.g. IRQ and FIQ on ARM). |
|
482 |
||
483 |
This function never unlocks the kernel. So it must be used |
|
484 |
only to complement NKern::DisableAllInterrupts. Never use it |
|
485 |
to complement NKern::DisableInterrupts. |
|
486 |
||
487 |
@see NKern::DisableInterrupts() |
|
488 |
@see NKern::DisableAllInterrupts() |
|
489 |
||
490 |
@internalComponent |
|
491 |
*/ |
|
492 |
EXPORT_C __NAKED__ void NKern::EnableAllInterrupts() |
|
493 |
{ |
|
494 |
__ASM_STI(); |
|
495 |
__JUMP(,lr); |
|
496 |
} |
|
497 |
||
498 |
||
499 |
/** Restores interrupts to previous level and unlocks the kernel if it was |
|
500 |
locked when disabling them. |
|
501 |
||
502 |
@param aRestoreData CPU-specific data returned from NKern::DisableInterrupts |
|
503 |
or NKern::DisableAllInterrupts specifying the previous interrupt level. |
|
504 |
||
505 |
@see NKern::DisableInterrupts() |
|
506 |
@see NKern::DisableAllInterrupts() |
|
507 |
*/ |
|
508 |
EXPORT_C __NAKED__ void NKern::RestoreInterrupts(TInt /*aRestoreData*/) |
|
509 |
{ |
|
510 |
asm("tst r0, r0 "); // test state of top bit of aLevel |
|
511 |
asm("mrs r1, cpsr "); |
|
512 |
asm("and r0, r0, #%a0" : : "i" ((TInt)KAllInterruptsMask)); |
|
513 |
asm("bic r1, r1, #%a0" : : "i" ((TInt)KAllInterruptsMask)); |
|
514 |
asm("orr r1, r1, r0 "); // replace I and F bits with those supplied |
|
515 |
asm("msr cpsr_c, r1 "); // flags are unchanged (in particular N) |
|
516 |
__JUMP(pl,lr); // if top bit of aLevel clear, finished |
|
517 |
||
518 |
// if top bit of aLevel set, fall through to unlock the kernel |
|
519 |
} |
|
520 |
||
521 |
||
522 |
/** Unlocks the kernel. |
|
523 |
||
524 |
Decrements iKernLockCount for current CPU; if it becomes zero and IDFCs |
|
525 |
or a reschedule are pending, calls the scheduler to process them. |
|
526 |
Must be called in mode_svc. |
|
527 |
||
528 |
@pre Call either in a thread or an IDFC context. |
|
529 |
@pre Do not call from an ISR. |
|
530 |
*/ |
|
531 |
EXPORT_C __NAKED__ void NKern::Unlock() |
|
532 |
{ |
|
533 |
ASM_CHECK_PRECONDITIONS(MASK_NOT_ISR); |
|
534 |
||
535 |
GET_RWNO_TID(,r0) // r0=&SubScheduler() |
|
536 |
__ASM_CLI(); // interrupts off |
|
537 |
asm("ldr r1, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
538 |
asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
539 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iRescheduleNeededFlag)); |
|
540 |
asm("subs r3, r1, #1 "); |
|
541 |
asm("strne r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
542 |
asm("bne 0f "); // kernel still locked -> return |
|
543 |
asm("cmp r2, #0 "); // check for DFCs or reschedule |
|
544 |
asm("bne 1f "); |
|
545 |
asm("cmp r12, #0 "); // IPIs outstanding? |
|
546 |
asm("str r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); // unlock the kernel |
|
547 |
asm("bne 2f "); |
|
548 |
asm("0: "); |
|
549 |
__ASM_STI(); // interrupts back on |
|
550 |
__JUMP(,lr); |
|
551 |
||
552 |
// need to run IDFCs and/or reschedule |
|
553 |
asm("1: "); |
|
554 |
asm("stmfd sp!, {r0,r4-r11,lr} "); |
|
555 |
asm("bl " CSM_ZN10TScheduler10RescheduleEv ); |
|
556 |
asm(".global nkern_unlock_resched_return "); |
|
557 |
asm("nkern_unlock_resched_return: "); |
|
558 |
||
559 |
// need to send any outstanding reschedule IPIs |
|
560 |
asm("cmp r12, #0 "); |
|
561 |
asm("blne " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
562 |
asm("ldmfd sp!, {r0,r4-r11,lr} "); |
|
563 |
__ASM_STI(); |
|
564 |
__JUMP(,lr); |
|
565 |
||
566 |
asm("2: "); |
|
567 |
asm("stmfd sp!, {r0,lr} "); |
|
568 |
asm("bl " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
569 |
asm("ldmfd sp!, {r0,lr} "); |
|
570 |
__ASM_STI(); |
|
571 |
__JUMP(,lr); |
|
572 |
} |
|
573 |
||
574 |
||
575 |
/** Locks the kernel. |
|
576 |
||
577 |
Increments iKernLockCount for the current CPU, thereby deferring IDFCs |
|
578 |
and preemption. Must be called in mode_svc. |
|
579 |
||
580 |
@pre Call either in a thread or an IDFC context. |
|
581 |
@pre Do not call from an ISR. |
|
582 |
*/ |
|
583 |
EXPORT_C __NAKED__ void NKern::Lock() |
|
584 |
{ |
|
585 |
ASM_CHECK_PRECONDITIONS(MASK_NOT_ISR); |
|
586 |
||
587 |
__ASM_CLI(); |
|
588 |
GET_RWNO_TID(,r12); |
|
589 |
asm("ldr r3, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
590 |
asm("add r3, r3, #1 "); // lock the kernel |
|
591 |
asm("str r3, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
592 |
__ASM_STI(); |
|
593 |
__JUMP(,lr); |
|
594 |
} |
|
595 |
||
596 |
||
597 |
/** Locks the kernel and returns a pointer to the current thread. |
|
598 |
||
599 |
Increments iKernLockCount for the current CPU, thereby deferring IDFCs |
|
600 |
and preemption. Must be called in mode_svc. |
|
601 |
||
602 |
@pre Call either in a thread or an IDFC context. |
|
603 |
@pre Do not call from an ISR. |
|
604 |
*/ |
|
605 |
EXPORT_C __NAKED__ NThread* NKern::LockC() |
|
606 |
{ |
|
607 |
ASM_CHECK_PRECONDITIONS(MASK_NOT_ISR); |
|
608 |
||
609 |
__ASM_CLI(); |
|
610 |
GET_RWNO_TID(,r12); |
|
611 |
asm("ldr r3, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
612 |
asm("ldr r0, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread)); |
|
613 |
asm("add r3, r3, #1 "); // lock the kernel |
|
614 |
asm("str r3, [r12, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
615 |
__ASM_STI(); |
|
616 |
__JUMP(,lr); |
|
617 |
} |
|
618 |
||
619 |
||
620 |
/** Allows IDFCs and rescheduling if they are pending. |
|
621 |
||
622 |
If IDFCs or a reschedule are pending and iKernCSLocked is exactly equal to 1 |
|
623 |
calls the scheduler to process the IDFCs and possibly reschedule. |
|
624 |
Must be called in mode_svc. |
|
625 |
||
626 |
@return Nonzero if a reschedule actually occurred, zero if not. |
|
627 |
||
628 |
@pre Call either in a thread or an IDFC context. |
|
629 |
@pre Do not call from an ISR. |
|
630 |
*/ |
|
631 |
EXPORT_C __NAKED__ TInt NKern::PreemptionPoint() |
|
632 |
{ |
|
633 |
ASM_CHECK_PRECONDITIONS(MASK_NOT_ISR); |
|
634 |
||
635 |
GET_RWNO_TID(,r0) // r0=&SubScheduler() |
|
636 |
asm("ldr r1, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
637 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iRescheduleNeededFlag)); |
|
638 |
asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
639 |
asm("cmp r1, #1 "); |
|
640 |
asm("bgt 0f "); // if locked more than once return FALSE |
|
641 |
asm("cmp r2, #0 "); // locked once and IDFCs/reschedule pending? |
|
642 |
asm("bne 1f "); // skip if so |
|
643 |
asm("cmp r12, #0 "); // locked once and resched IPIs outstanding? |
|
644 |
asm("bne 2f "); // skip if so |
|
645 |
asm("0: "); |
|
646 |
asm("mov r0, #0 "); |
|
647 |
__JUMP(, lr); // else return FALSE |
|
648 |
||
649 |
// need to run IDFCs and/or reschedule |
|
650 |
asm("1: "); |
|
651 |
asm("stmfd sp!, {r1,r4-r11,lr} "); |
|
652 |
asm("bl " CSM_ZN10TScheduler10RescheduleEv ); |
|
653 |
asm(".global nkern_preemption_point_resched_return "); |
|
654 |
asm("nkern_preemption_point_resched_return: "); |
|
655 |
asm("str r2, [sp] "); |
|
656 |
asm("mov r2, #1 "); |
|
657 |
asm("str r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iKernLockCount)); |
|
658 |
||
659 |
// need to send any outstanding reschedule IPIs |
|
660 |
asm("cmp r12, #0 "); |
|
661 |
asm("blne " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
662 |
asm("ldmfd sp!, {r0,r4-r11,lr} "); // return TRUE if reschedule occurred |
|
663 |
__ASM_STI(); |
|
664 |
__JUMP(, lr); |
|
665 |
||
666 |
asm("2: "); |
|
667 |
asm("stmfd sp!, {r2,lr} "); |
|
668 |
asm("bl " CSM_CFUNC(send_accumulated_resched_ipis)); |
|
669 |
asm("ldmfd sp!, {r0,lr} "); // return TRUE if reschedule occurred |
|
670 |
__ASM_STI(); |
|
671 |
__JUMP(, lr); |
|
672 |
} |
|
673 |
||
674 |
||
675 |
#ifdef __CPU_HAS_VFP |
|
676 |
// Do the actual VFP context save |
|
677 |
__NAKED__ void VfpContextSave(void*) |
|
678 |
{ |
|
679 |
VFP_FMRX(,1,VFP_XREG_FPEXC); |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
680 |
asm("tst r1, #%a0" : : "i" ((TInt)VFP_FPEXC_EN) ); // Check to see if VFP in use |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
681 |
__JUMP(eq, lr); // Return immediately if not |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
682 |
asm("tst r1, #%a0" : : "i" ((TInt)VFP_FPEXC_EX) ); // Check to see if an exception has occurred |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
683 |
asm("beq 1f "); // Skip ahead if not |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
684 |
asm("bic r1, r1, #%a0" : : "i" ((TInt)VFP_FPEXC_EX)); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
685 |
VFP_FMXR(,VFP_XREG_FPEXC,1); // Reset exception flag |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
686 |
asm("orr r1, r1, #%a0" : : "i" ((TInt)VFP_FPEXC_EX)); // But store it for later |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
687 |
asm("1: "); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
688 |
|
0 | 689 |
|
690 |
VFP_FMRX(,2,VFP_XREG_FPSCR); |
|
691 |
asm("stmia r0!, {r2} "); // Save FPSCR |
|
692 |
||
693 |
#ifndef __VFP_V3 |
|
694 |
VFP_FMRX(,2,VFP_XREG_FPINST); |
|
695 |
VFP_FMRX(,3,VFP_XREG_FPINST2); |
|
696 |
asm("stmia r0!, {r2-r3} "); // Save FPINST, FPINST2 |
|
697 |
#endif |
|
698 |
||
699 |
VFP_FSTMIADW(CC_AL,0,0,16); // Save D0 - D15 |
|
700 |
||
701 |
#ifdef __VFP_V3 |
|
702 |
VFP_FMRX(,2,VFP_XREG_MVFR0); |
|
703 |
asm("tst r2, #%a0" : : "i" ((TInt)VFP_MVFR0_ASIMD32)); // Check to see if all 32 Advanced SIMD registers are present |
|
704 |
asm("beq 0f "); // Skip ahead if not |
|
705 |
GET_CAR(,r2); |
|
706 |
asm("tst r2, #%a0" : : "i" ((TInt)VFP_CPACR_D32DIS)); // Check to see if access to the upper 16 registers is disabled |
|
707 |
VFP_FSTMIADW(CC_EQ,0,16,16); // If not then save D16 - D31 |
|
708 |
#endif |
|
709 |
||
710 |
asm("0: "); |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
711 |
asm("bic r1, r1, #%a0" : : "i" ((TInt)VFP_FPEXC_EN) ); |
0 | 712 |
VFP_FMXR(,VFP_XREG_FPEXC,1); // Disable VFP |
713 |
||
714 |
__JUMP(,lr); |
|
715 |
} |
|
716 |
#endif |
|
717 |
||
718 |
||
719 |
/** Check if the kernel is locked the specified number of times. |
|
720 |
||
721 |
@param aCount The number of times the kernel should be locked |
|
722 |
If zero, tests if it is locked at all |
|
723 |
@return TRUE if the tested condition is true. |
|
724 |
||
725 |
@internalTechnology |
|
726 |
*/ |
|
727 |
EXPORT_C __NAKED__ TBool NKern::KernelLocked(TInt /*aCount*/) |
|
728 |
{ |
|
729 |
asm("mrs r12, cpsr "); |
|
730 |
__ASM_CLI(); |
|
731 |
GET_RWNO_TID(,r3); |
|
732 |
asm("movs r1, r0 "); // r1 = aCount |
|
733 |
asm("ldr r0, [r3, #%a0]" : : "i" _FOFF(TSubScheduler, iKernLockCount)); |
|
734 |
asm("moveq r1, r0 "); // if aCount=0, aCount=iKernLockCount |
|
735 |
asm("cmp r1, r0 "); // |
|
736 |
asm("movne r0, #0 "); // if aCount!=iKernLockCount, return FALSE else return iKernLockCount |
|
737 |
asm("msr cpsr, r12 "); |
|
738 |
__JUMP(,lr); |
|
739 |
} |
|
740 |
||
741 |
||
742 |
// Only call this if thread migration is disabled, i.e. |
|
743 |
// interrupts disabled, kernel locked or current thread in 'freeze cpu' mode |
|
744 |
extern "C" __NAKED__ TSubScheduler& SubScheduler() |
|
745 |
{ |
|
746 |
GET_RWNO_TID(,r0); |
|
747 |
__JUMP(,lr); |
|
748 |
} |
|
749 |
||
750 |
/** Returns the NThread control block for the currently scheduled thread. |
|
751 |
||
752 |
Note that this is the calling thread if called from a thread context, or the |
|
753 |
interrupted thread if called from an interrupt context. |
|
754 |
||
755 |
@return A pointer to the NThread for the currently scheduled thread. |
|
756 |
||
757 |
@pre Call in any context. |
|
758 |
*/ |
|
759 |
EXPORT_C __NAKED__ NThread* NKern::CurrentThread() |
|
760 |
{ |
|
761 |
asm("mrs r12, cpsr "); |
|
762 |
__ASM_CLI(); |
|
763 |
GET_RWNO_TID(,r0); |
|
764 |
asm("cmp r0, #0 "); |
|
765 |
asm("ldrne r0, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread)); |
|
766 |
asm("msr cpsr, r12 "); |
|
767 |
__JUMP(,lr); |
|
768 |
} |
|
769 |
||
770 |
||
771 |
/** Returns the NThread control block for the currently scheduled thread. |
|
772 |
||
773 |
Note that this is the calling thread if called from a thread context, or the |
|
774 |
interrupted thread if called from an interrupt context. |
|
775 |
||
776 |
@return A pointer to the NThread for the currently scheduled thread. |
|
777 |
||
778 |
@pre Call with migration disabled - i.e. from an ISR, IDFC, with interrupts |
|
779 |
disabled or with preemption disabled. |
|
780 |
*/ |
|
781 |
extern "C" __NAKED__ NThread* NCurrentThreadL() |
|
782 |
{ |
|
783 |
GET_RWNO_TID(,r0); |
|
784 |
asm("ldr r0, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iCurrentThread)); |
|
785 |
__JUMP(,lr); |
|
786 |
} |
|
787 |
||
788 |
||
789 |
/** Returns the CPU number of the calling CPU. |
|
790 |
||
791 |
@return the CPU number of the calling CPU. |
|
792 |
||
793 |
@pre Call in any context. |
|
794 |
*/ |
|
795 |
EXPORT_C __NAKED__ TInt NKern::CurrentCpu() |
|
796 |
{ |
|
797 |
asm("mrs r12, cpsr "); |
|
798 |
__ASM_CLI(); |
|
799 |
GET_RWNO_TID(,r0); |
|
800 |
asm("cmp r0, #0 "); |
|
801 |
asm("ldrne r0, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iCpuNum)); |
|
802 |
asm("msr cpsr, r12 "); |
|
803 |
__JUMP(,lr); |
|
804 |
} |
|
805 |
||
806 |
||
807 |
/** Returns the current processor context type (thread, IDFC or interrupt). |
|
808 |
||
809 |
@return A value from NKern::TContext enumeration (but never EEscaped). |
|
810 |
||
811 |
@pre Call in any context. |
|
812 |
||
813 |
@see NKern::TContext |
|
814 |
*/ |
|
815 |
EXPORT_C __NAKED__ TInt NKern::CurrentContext() |
|
816 |
{ |
|
817 |
asm("mrs r1, cpsr "); |
|
818 |
__ASM_CLI(); // interrupts off to stop migration |
|
819 |
GET_RWNO_TID(,r3); // r3 = &SubScheduler() |
|
820 |
asm("mov r0, #2 "); // 2 = interrupt |
|
821 |
asm("and r2, r1, #0x1f "); // r1 = mode |
|
822 |
asm("cmp r2, #0x13 "); |
|
823 |
asm("ldreqb r0, [r3, #%a0]" : : "i" _FOFF(TSubScheduler,iInIDFC)); |
|
824 |
asm("bne 0f "); // if not svc, must be interrupt |
|
825 |
asm("cmp r0, #0 "); |
|
826 |
asm("movne r0, #1 "); // if iInIDFC, return 1 else return 0 |
|
827 |
asm("0: "); |
|
828 |
asm("msr cpsr, r1 "); // restore interrupts |
|
829 |
__JUMP(,lr); |
|
830 |
} |
|
831 |
||
832 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
833 |
extern "C" __NAKED__ void send_irq_ipi(TSubScheduler*, TInt) |
0 | 834 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
835 |
asm("tst r1, #%a0" : : "i" ((TInt)EQueueEvent_WakeUp) ); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
836 |
asm("bne " CSM_CFUNC(wake_up_for_ipi)); |
0 | 837 |
__DATA_SYNC_BARRIER_Z__(r3); // need DSB before sending any IPI |
838 |
asm("ldr r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iCpuMask)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
839 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); // we assume i_GicDistAddr is the same for all CPUs |
0 | 840 |
asm("mov r1, #%a0" : : "i" ((TInt)TRANSFERRED_IRQ_VECTOR)); |
841 |
asm("orr r1, r1, r3, lsl #16 "); |
|
842 |
asm("str r1, [r2, #%a0]" : : "i" _FOFF(GicDistributor, iSoftIrq)); // trigger IPIs |
|
843 |
__JUMP(,lr); |
|
844 |
} |
|
845 |
||
846 |
// Send any outstanding reschedule IPIs when the kernel is unlocked on this CPU. |
|
847 |
// Call with interrupts disabled, R0->TSubScheduler, R12=R0->iReschedIPIs |
|
848 |
// Return with R0 unaltered. |
|
849 |
extern "C" __NAKED__ void send_accumulated_resched_ipis() |
|
850 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
851 |
asm("ldr r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iScheduler)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
852 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
853 |
asm("ldr r1, [r3, #%a0]" : : "i" _FOFF(TScheduler, iThreadAcceptCpus)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
854 |
asm("bics r1, r12, r1 "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
855 |
asm("bne 2f "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
856 |
asm("1: "); |
0 | 857 |
asm("mov r1, #0 "); |
858 |
asm("str r1, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iReschedIPIs)); |
|
859 |
__DATA_SYNC_BARRIER__(r1); // need DSB before sending any IPI |
|
860 |
asm("mov r1, r12, lsl #16 "); |
|
861 |
// asm("orr r1, r1, #%a0" : : "i" ((TInt)RESCHED_IPI_VECTOR)); RESCHED_IPI_VECTOR=0 |
|
862 |
asm("str r1, [r2, #%a0]" : : "i" _FOFF(GicDistributor, iSoftIrq)); // trigger IPIs |
|
863 |
__JUMP(,lr); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
864 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
865 |
asm("2: "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
866 |
asm("stmfd sp!, {r0,lr} "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
867 |
asm("mov r0, r3 "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
868 |
asm("mov r1, r12 "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
869 |
asm("bl ReschedInactiveCpus__10TSchedulerUl "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
870 |
asm("mov r12, r0 "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
871 |
asm("ldmfd sp!, {r0,lr} "); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
872 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
873 |
asm("b 1b "); |
0 | 874 |
} |
875 |
||
876 |
// Send a reschedule IPI to the specified CPU |
|
877 |
extern "C" __NAKED__ void send_resched_ipi(TInt /*aCpu*/) |
|
878 |
{ |
|
879 |
GET_RWNO_TID(,r3); |
|
880 |
__DATA_SYNC_BARRIER_Z__(r2); // need DSB before sending any IPI |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
881 |
asm("ldr r2, [r3, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); // we assume i_GicDistAddr is the same for all CPUs |
0 | 882 |
ASM_DEBUG1(SendReschedIPI,r0); |
883 |
asm("mov r1, #0x10000 "); |
|
884 |
asm("mov r1, r1, lsl r0 "); // 0x10000<<aCpu |
|
885 |
// asm("orr r1, r1, #%a0" : : "i" ((TInt)RESCHED_IPI_VECTOR)); RESCHED_IPI_VECTOR=0 |
|
886 |
asm("str r1, [r2, #%a0]" : : "i" _FOFF(GicDistributor, iSoftIrq)); // trigger IPIs |
|
887 |
__JUMP(,lr); |
|
888 |
} |
|
889 |
||
890 |
// Send a reschedule IPI to the current processor |
|
891 |
// *** DON'T DO ANY TRACING OR INSTRUMENTATION *** |
|
892 |
extern "C" __NAKED__ void send_self_resched_ipi() |
|
893 |
{ |
|
894 |
GET_RWNO_TID(,r3); |
|
895 |
__DATA_SYNC_BARRIER_Z__(r2); // need DSB before sending any IPI |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
896 |
asm("ldr r2, [r3, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); // we assume i_GicDistAddr is the same for all CPUs |
0 | 897 |
asm("mov r1, #0x02000000 "); // target = requesting CPU only |
898 |
// asm("orr r1, r1, #%a0" : : "i" ((TInt)RESCHED_IPI_VECTOR)); RESCHED_IPI_VECTOR=0 |
|
899 |
asm("str r1, [r2, #%a0]" : : "i" _FOFF(GicDistributor, iSoftIrq)); // trigger IPI |
|
900 |
__JUMP(,lr); |
|
901 |
} |
|
902 |
||
903 |
extern "C" __NAKED__ void send_resched_ipi_and_wait(TInt /*aCpu*/) |
|
904 |
{ |
|
905 |
asm("ldr r1, __TheSubSchedulers "); |
|
906 |
asm("mov r2, #0x10000 "); |
|
907 |
asm("mov r2, r2, lsl r0 "); // 0x10000<<aCpu |
|
908 |
ASM_DEBUG1(SendReschedIPIAndWait,r0); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
909 |
asm("add r0, r1, r0, lsl #%a0 " : : "i" ((TInt)KSubSchedulerShift)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
910 |
asm("ldr r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iGicDistAddr)); // we assume i_GicDistAddr is the same for all CPUs |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
911 |
asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iIrqCount)); |
0 | 912 |
__DATA_SYNC_BARRIER_Z__(r1); // make sure i_IrqCount is read before IPI is sent |
913 |
// asm("orr r2, r2, #%a0" : : "i" ((TInt)RESCHED_IPI_VECTOR)); RESCHED_IPI_VECTOR=0 |
|
914 |
asm("str r2, [r3, #%a0]" : : "i" _FOFF(GicDistributor, iSoftIrq)); // trigger IPIs |
|
915 |
__DATA_SYNC_BARRIER__(r1); // make sure IPI has been sent |
|
916 |
asm("1: "); |
|
917 |
asm("ldrb r1, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iRescheduleNeededFlag)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
918 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iIrqNestCount)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
919 |
asm("ldr r3, [r0, #%a0]" : : "i" _FOFF(TSubScheduler, iSSX.iIrqCount)); |
0 | 920 |
asm("cmp r1, #0 "); |
921 |
asm("beq 0f "); // iRescheduleNeededFlag not set -> wait |
|
922 |
asm("cmp r2, #0 "); |
|
923 |
asm("bge 2f "); // if other CPU is in an ISR, finish |
|
924 |
asm("cmp r3, r12 "); // if not, has i_IrqCount changed? |
|
925 |
asm("0: "); |
|
926 |
ARM_WFEcc(CC_EQ); // if not, wait for something to happen ... |
|
927 |
asm("beq 1b "); // ... and loop |
|
928 |
asm("2: "); |
|
929 |
__DATA_MEMORY_BARRIER__(r1); // make sure subsequent memory accesses don't jump the gun |
|
930 |
// guaranteed to observe final thread state after this |
|
931 |
__JUMP(,lr); |
|
932 |
||
933 |
asm("__TheSubSchedulers: "); |
|
934 |
asm(".word TheSubSchedulers "); |
|
935 |
} |
|
936 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
937 |
|
0 | 938 |
/* If the current thread is subject to timeslicing, update its remaining time |
939 |
from the current CPU's local timer. Don't stop the timer. |
|
940 |
If the remaining time is negative, save it as zero. |
|
941 |
*/ |
|
942 |
__NAKED__ void TSubScheduler::SaveTimesliceTimer(NThreadBase* /*aThread*/) |
|
943 |
{ |
|
944 |
asm("ldr r3, [r1, #%a0]" : : "i" _FOFF(NThreadBase,iTime)); |
|
945 |
asm("ldrb r12, [r1, #%a0]" : : "i" _FOFF(NThreadBase,i_NThread_Initial)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
946 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iSSX.iLocalTimerAddr)); |
0 | 947 |
asm("cmp r3, #0 "); |
948 |
asm("ble 0f "); // thread isn't timesliced or timeslice already expired so skip |
|
949 |
asm("cmp r12, #0 "); |
|
950 |
asm("bne 0f "); // initial (i.e. idle) thread, so skip |
|
951 |
asm("ldr r3, [r2, #%a0]" : : "i" _FOFF(ArmLocalTimer,iTimerCount)); |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
952 |
asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iSSX.iTimerFreqRI.iI.iM)); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
953 |
asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(TSubScheduler,iSSX.iTimerFreqRI.iI.iX)); |
0 | 954 |
asm("cmp r3, #0 "); |
955 |
asm("movmi r0, #0 "); // if timer count is negative, save zero |
|
956 |
asm("bmi 1f "); |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
957 |
asm("mov r2, r2, lsl #16 "); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
958 |
asm("mov r2, r2, asr #16 "); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
959 |
asm("umull r0, r3, r12, r3 "); // scale up to max timer clock (R3:R0) - need to shift right by -iX |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
960 |
asm("rsb r2, r2, #0 "); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
961 |
asm("rsb r12, r2, #32 "); |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
962 |
asm("movs r0, r0, lsr r2 "); // r0 >>= iSSX.iTimerFreqRI.iI.iX, C = last bit shifted off (rounding) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
963 |
asm("orr r0, r0, r3, lsl r12 "); // bottom bits from r3 into top bits of r0 |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
964 |
asm("adcs r0, r0, #0 "); // round using last bit shifted off |
0 | 965 |
asm("1: "); |
966 |
asm("str r0, [r1, #%a0]" : : "i" _FOFF(NThreadBase,iTime)); |
|
967 |
asm("0: "); |
|
968 |
__JUMP(,lr); |
|
969 |
} |
|
970 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
971 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
972 |
#if defined(__UTT_MACHINE_CODED__) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
973 |
#if defined(__NKERN_TIMESTAMP_USE_LOCAL_TIMER__) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
974 |
#error Use of local timer for NKern::Timestamp() no longer supported |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
975 |
#else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
976 |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
977 |
#error UpdateThreadTimes assembler out of date! |
0 | 978 |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
979 |
#endif |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
980 |
#endif // __UTT_MACHINE_CODED__ |