cpuswitch.S revision 1.85.2.1 1 /* $NetBSD: cpuswitch.S,v 1.85.2.1 2015/04/06 15:17:52 skrll Exp $ */
2
3 /*
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37 /*
38 * Copyright (c) 1994-1998 Mark Brinicombe.
39 * Copyright (c) 1994 Brini.
40 * All rights reserved.
41 *
42 * This code is derived from software written for Brini by Mark Brinicombe
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Brini.
55 * 4. The name of the company nor the name of the author may be used to
56 * endorse or promote products derived from this software without specific
57 * prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
60 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * RiscBSD kernel project
72 *
73 * cpuswitch.S
74 *
75 * cpu switching functions
76 *
77 * Created : 15/10/94
78 */
79
80 #include "opt_armfpe.h"
81 #include "opt_arm32_pmap.h"
82 #include "opt_multiprocessor.h"
83 #include "opt_cpuoptions.h"
84 #include "opt_lockdebug.h"
85
86 #include "assym.h"
87 #include <arm/asm.h>
88 #include <arm/locore.h>
89
90 RCSID("$NetBSD: cpuswitch.S,v 1.85.2.1 2015/04/06 15:17:52 skrll Exp $")
91
92 /* LINTSTUB: include <sys/param.h> */
93
94 #undef IRQdisable
95 #undef IRQenable
96
97 /*
98 * New experimental definitions of IRQdisable and IRQenable
99 * These keep FIQ's enabled since FIQ's are special.
100 */
101
102 #ifdef _ARM_ARCH_6
103 #define IRQdisable cpsid i
104 #define IRQenable cpsie i
105 #else
106 #define IRQdisable \
107 mrs r14, cpsr ; \
108 orr r14, r14, #(I32_bit) ; \
109 msr cpsr_c, r14
110
111 #define IRQenable \
112 mrs r14, cpsr ; \
113 bic r14, r14, #(I32_bit) ; \
114 msr cpsr_c, r14
115
116 #endif
117
118 .text
119
120 /*
121 * struct lwp *
122 * cpu_switchto(struct lwp *current, struct lwp *next)
123 *
124 * Switch to the specified next LWP
125 * Arguments:
126 *
127 * r0 'struct lwp *' of the current LWP (or NULL if exiting)
128 * r1 'struct lwp *' of the LWP to switch to
129 * r2 returning
130 */
131 ENTRY(cpu_switchto)
132 mov ip, sp
133 push {r4-r7, ip, lr}
134
135 /* move lwps into caller saved registers */
136 mov r6, r1
137 mov r4, r0
138
139 #ifdef TPIDRPRW_IS_CURCPU
140 GET_CURCPU(r5)
141 #elif defined(TPIDRPRW_IS_CURLWP)
142 mrc p15, 0, r0, c13, c0, 4 /* get old lwp (r4 maybe 0) */
143 ldr r5, [r0, #(L_CPU)] /* get cpu from old lwp */
144 #elif !defined(MULTIPROCESSOR)
145 ldr r5, [r6, #L_CPU] /* get cpu from new lwp */
146 #else
147 #error curcpu() method not defined
148 #endif
149
150 /* rem: r4 = old lwp */
151 /* rem: r5 = curcpu() */
152 /* rem: r6 = new lwp */
153
154 #ifndef __HAVE_UNNESTED_INTRS
155 IRQdisable
156 #endif
157
158 #ifdef MULTIPROCESSOR
159 str r5, [r6, #(L_CPU)]
160 #else
161 /* l->l_cpu initialized in fork1() for single-processor */
162 #endif
163
164 #if defined(TPIDRPRW_IS_CURLWP)
165 mcr p15, 0, r6, c13, c0, 4 /* set current lwp */
166 #endif
167 /* We have a new curlwp now so make a note it */
168 str r6, [r5, #(CI_CURLWP)]
169
170 /* At this point we can allow IRQ's again. */
171 #ifndef __HAVE_UNNESTED_INTRS
172 IRQenable
173 #endif
174
175 /* rem: r4 = old lwp */
176 /* rem: r5 = curcpu() */
177 /* rem: r6 = new lwp */
178 /* rem: interrupts are enabled */
179
180 /*
181 * If the old lwp on entry to cpu_switchto was zero then the
182 * process that called it was exiting. This means that we do
183 * not need to save the current context. Instead we can jump
184 * straight to restoring the context for the new process.
185 */
186 teq r4, #0
187 beq .Ldo_switch
188
189 /* rem: r4 = old lwp */
190 /* rem: r5 = curcpu() */
191 /* rem: r6 = new lwp */
192 /* rem: interrupts are enabled */
193
194 /* Save old context */
195
196 /* Get the user structure for the old lwp. */
197 ldr r7, [r4, #(L_PCB)]
198
199 /* Save all the registers in the old lwp's pcb */
200 #if defined(_ARM_ARCH_DWORD_OK)
201 strd r8, r9, [r7, #(PCB_R8)]
202 strd r10, r11, [r7, #(PCB_R10)]
203 strd r12, r13, [r7, #(PCB_R12)]
204 #else
205 add r0, r7, #(PCB_R8)
206 stmia r0, {r8-r13}
207 #endif
208
209 #ifdef _ARM_ARCH_6
210 /*
211 * Save user read/write thread/process id register
212 */
213 mrc p15, 0, r0, c13, c0, 2
214 str r0, [r7, #(PCB_USER_PID_RW)]
215 #endif
216 /*
217 * NOTE: We can now use r8-r13 until it is time to restore
218 * them for the new process.
219 */
220
221 /* rem: r4 = old lwp */
222 /* rem: r5 = curcpu() */
223 /* rem: r6 = new lwp */
224 /* rem: interrupts are enabled */
225
226 /* Restore saved context */
227
228 .Ldo_switch:
229 /* Get the new pcb */
230 ldr r7, [r6, #(L_PCB)]
231
232 /* rem: r4 = old lwp */
233 /* rem: r5 = curcpu() */
234 /* rem: r6 = new lwp */
235 /* rem: r7 = new pcb */
236 /* rem: interrupts are enabled */
237
238 /*
239 * If we are switching to a system lwp, don't bother restoring
240 * thread or vfp registers and skip the ras check.
241 */
242 ldr r0, [r6, #(L_FLAG)]
243 tst r0, #(LW_SYSTEM)
244 bne .Lswitch_do_restore
245
246 #ifdef _ARM_ARCH_6
247 /*
248 * Restore user thread/process id registers
249 */
250 ldr r0, [r7, #(PCB_USER_PID_RW)]
251 mcr p15, 0, r0, c13, c0, 2
252 ldr r0, [r6, #(L_PRIVATE)]
253 mcr p15, 0, r0, c13, c0, 3
254 #endif
255
256 #ifdef FPU_VFP
257 /*
258 * If we have a VFP, we need to load FPEXC.
259 */
260 ldr r0, [r5, #(CI_VFP_ID)]
261 cmp r0, #0
262 ldrne r0, [r7, #(PCB_VFP_FPEXC)]
263 vmsrne fpexc, r0
264 #endif
265
266 /*
267 * Check for restartable atomic sequences (RAS).
268 */
269
270 ldr r0, [r6, #(L_PROC)] /* fetch the proc for ras_lookup */
271 ldr r2, [r0, #(P_RASLIST)]
272 cmp r2, #0 /* p->p_nras == 0? */
273 beq .Lswitch_do_restore
274
275 /* we can use r8 since we haven't restored saved registers yet. */
276 ldr r8, [r6, #(L_MD_TF)] /* r1 = trapframe (used below) */
277 ldr r1, [r8, #(TF_PC)] /* second ras_lookup() arg */
278 bl _C_LABEL(ras_lookup)
279 cmn r0, #1 /* -1 means "not in a RAS" */
280 strne r0, [r8, #(TF_PC)]
281
282 /* rem: r4 = old lwp */
283 /* rem: r5 = curcpu() */
284 /* rem: r6 = new lwp */
285 /* rem: r7 = new pcb */
286
287 .Lswitch_do_restore:
288 /* Restore all the saved registers */
289 #ifdef __XSCALE__
290 ldr r8, [r7, #(PCB_R8)]
291 ldr r9, [r7, #(PCB_R9)]
292 ldr r10, [r7, #(PCB_R10)]
293 ldr r11, [r7, #(PCB_R11)]
294 ldr r12, [r7, #(PCB_R12)]
295 ldr r13, [r7, #(PCB_KSP)] /* sp */
296 #elif defined(_ARM_ARCH_DWORD_OK)
297 ldrd r8, r9, [r7, #(PCB_R8)]
298 ldrd r10, r11, [r7, #(PCB_R10)]
299 ldrd r12, r13, [r7, #(PCB_R12)] /* sp */
300 #else
301 add r0, r7, #PCB_R8
302 ldmia r0, {r8-r13}
303 #endif
304
305 /* Record the old lwp for pmap_activate()'s benefit */
306 #ifndef ARM_MMU_EXTENDED
307 str r4, [r5, #CI_LASTLWP]
308 #endif
309
310 /* cpu_switchto returns the old lwp */
311 mov r0, r4
312 /* lwp_trampoline expects new lwp as its second argument */
313 mov r1, r6
314
315 #ifdef _ARM_ARCH_7
316 clrex /* cause any subsequent STREX* to fail */
317 #endif
318
319 /*
320 * Pull the registers that got pushed when cpu_switchto() was called,
321 * and return.
322 */
323 pop {r4-r7, ip, pc}
324
325 END(cpu_switchto)
326
327 ENTRY_NP(lwp_trampoline)
328 /*
329 * cpu_switchto gives us:
330 * arg0(r0) = old lwp
331 * arg1(r1) = new lwp
332 * setup by cpu_lwp_fork:
333 * r4 = func to call
334 * r5 = arg to func
335 * r6 = <unused>
336 * r7 = spsr mode
337 */
338 bl _C_LABEL(lwp_startup)
339
340 mov fp, #0 /* top stack frame */
341 mov r0, r5
342 mov r1, sp
343 #ifdef _ARM_ARCH_5
344 blx r4
345 #else
346 mov lr, pc
347 mov pc, r4
348 #endif
349
350 GET_CPSR(r0)
351 CPSID_I(r0, r0) /* Kill irq's */
352
353 GET_CURCPU(r4) /* for DO_AST */
354 DO_AST_AND_RESTORE_ALIGNMENT_FAULTS
355 PULLFRAME
356
357 movs pc, lr /* Exit */
358 END(lwp_trampoline)
359
360 AST_ALIGNMENT_FAULT_LOCALS
361
362 #ifdef __HAVE_FAST_SOFTINTS
363 /*
364 * Called at IPL_HIGH
365 * r0 = new lwp
366 * r1 = ipl for softint_dispatch
367 */
368 ENTRY_NP(softint_switch)
369 push {r4, r6, r7, lr}
370
371 ldr r7, [r0, #L_CPU] /* get curcpu */
372 #if defined(TPIDRPRW_IS_CURLWP)
373 mrc p15, 0, r4, c13, c0, 4 /* get old lwp */
374 #else
375 ldr r4, [r7, #(CI_CURLWP)] /* get old lwp */
376 #endif
377 mrs r6, cpsr /* we need to save this */
378
379 /*
380 * If the soft lwp blocks, it needs to return to softint_tramp
381 */
382 mov r2, sp /* think ip */
383 adr r3, softint_tramp /* think lr */
384 push {r2-r3}
385 push {r4-r7}
386
387 mov r5, r0 /* save new lwp */
388
389 ldr r2, [r4, #(L_PCB)] /* get old lwp's pcb */
390
391 /* Save all the registers into the old lwp's pcb */
392 #if defined(__XSCALE__) || defined(_ARM_ARCH_6)
393 strd r8, r9, [r2, #(PCB_R8)]
394 strd r10, r11, [r2, #(PCB_R10)]
395 strd r12, r13, [r2, #(PCB_R12)]
396 #else
397 add r3, r2, #(PCB_R8)
398 stmia r3, {r8-r13}
399 #endif
400
401 #ifdef _ARM_ARCH_6
402 /*
403 * Save user read/write thread/process id register in cause it was
404 * set in userland.
405 */
406 mrc p15, 0, r0, c13, c0, 2
407 str r0, [r2, #(PCB_USER_PID_RW)]
408 #endif
409
410 /* this is an invariant so load before disabling intrs */
411 ldr r2, [r5, #(L_PCB)] /* get new lwp's pcb */
412
413 #ifndef __HAVE_UNNESTED_INTRS
414 IRQdisable
415 #endif
416 /*
417 * We're switching to a bound LWP so its l_cpu is already correct.
418 */
419 #if defined(TPIDRPRW_IS_CURLWP)
420 mcr p15, 0, r5, c13, c0, 4 /* save new lwp */
421 #endif
422 str r5, [r7, #(CI_CURLWP)] /* save new lwp */
423
424 /*
425 * Normally, we'd get {r8-r13} but since this is a softint lwp
426 * its existing state doesn't matter. We start the stack just
427 * below the trapframe.
428 */
429 ldr sp, [r5, #(L_MD_TF)] /* get new lwp's stack ptr */
430
431 /* At this point we can allow IRQ's again. */
432 #ifndef __HAVE_UNNESTED_INTRS
433 IRQenable
434 #endif
435
436 /* r1 still has ipl */
437 mov r0, r4 /* r0 has pinned (old) lwp */
438 bl _C_LABEL(softint_dispatch)
439 /*
440 * If we've returned, we need to change everything back and return.
441 */
442 ldr r2, [r4, #(L_PCB)] /* get pinned lwp's pcb */
443
444 #ifndef __HAVE_UNNESTED_INTRS
445 IRQdisable
446 #endif
447 /*
448 * We don't need to restore all the registers since another lwp was
449 * never executed. But we do need the SP from the formerly pinned lwp.
450 */
451
452 #if defined(TPIDRPRW_IS_CURLWP)
453 mcr p15, 0, r4, c13, c0, 4 /* restore pinned lwp */
454 #endif
455 str r4, [r7, #(CI_CURLWP)] /* restore pinned lwp */
456 ldr sp, [r2, #(PCB_KSP)] /* now running on the old stack. */
457
458 /* At this point we can allow IRQ's again. */
459 msr cpsr_c, r6
460
461 /*
462 * Grab the registers that got pushed at the start and return.
463 */
464 pop {r4-r7, ip, lr} /* eat switch frame */
465 pop {r4, r6, r7, pc} /* pop stack and return */
466
467 END(softint_switch)
468
469 /*
470 * r0 = previous LWP (the soft lwp)
471 * r4 = original LWP (the current lwp)
472 * r6 = original CPSR
473 * r7 = curcpu()
474 */
475 ENTRY_NP(softint_tramp)
476 ldr r3, [r7, #(CI_MTX_COUNT)] /* readust after mi_switch */
477 add r3, r3, #1
478 str r3, [r7, #(CI_MTX_COUNT)]
479
480 mov r3, #0 /* tell softint_dispatch */
481 str r3, [r0, #(L_CTXSWTCH)] /* the soft lwp blocked */
482
483 msr cpsr_c, r6 /* restore interrupts */
484 pop {r4, r6, r7, pc} /* pop stack and return */
485 END(softint_tramp)
486 #endif /* __HAVE_FAST_SOFTINTS */
487