1 /* $NetBSD: cpuswitch.S,v 1.41 2003/11/15 08:44:18 scw Exp $ */
4 * Copyright 2003 Wasabi Systems, Inc.
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
38 * Copyright (c) 1994-1998 Mark Brinicombe.
39 * Copyright (c) 1994 Brini.
40 * All rights reserved.
42 * This code is derived from software written for Brini by Mark Brinicombe
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
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.
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
71 * RiscBSD kernel project
75 * cpu switching functions
83 #include <machine/asm.h>
84 #include <machine/asmacros.h>
85 #include <machine/armreg.h>
86 __FBSDID("$FreeBSD$");
90 * New experimental definitions of IRQdisable and IRQenable
91 * These keep FIQ's enabled since FIQ's are special.
94 #define DOMAIN_CLIENT 0x01
97 orr r14, r14, #(I32_bit) ; \
102 bic r14, r14, #(I32_bit) ; \
106 * These are used for switching the translation table/DACR.
107 * Since the vector page can be invalid for a short time, we must
108 * disable both regular IRQs *and* FIQs.
110 * XXX: This is not necessary if the vector table is relocated.
112 #define IRQdisableALL \
114 orr r14, r14, #(I32_bit | F32_bit) ; \
117 #define IRQenableALL \
119 bic r14, r14, #(I32_bit | F32_bit) ; \
123 .word _C_LABEL(__pcpu) + PC_CURPCB
125 .word _C_LABEL(cpufuncs)
126 .Lblock_userspace_access:
127 .word _C_LABEL(block_userspace_access)
129 .word _C_LABEL(cpu_do_powersave)
131 .word _C_LABEL(blocked_lock)
139 ldr r7, [r5, #(TD_PCB)] /* r7 = new thread's PCB */
141 /* Switch to lwp0 context */
145 ldr pc, [r9, #CF_IDCACHE_WBINV_ALL]
146 ldr r0, [r7, #(PCB_PL1VEC)]
147 ldr r1, [r7, #(PCB_DACR)]
149 * r0 = Pointer to L1 slot for vector_page (or NULL)
158 * Ensure the vector table is accessible by fixing up lwp0's L1
160 cmp r0, #0 /* No need to fixup vector table? */
161 ldrne r3, [r0] /* But if yes, fetch current value */
162 ldrne r2, [r7, #(PCB_L1VEC)] /* Fetch new vector_page value */
163 mcr p15, 0, r1, c3, c0, 0 /* Update DACR for lwp0's context */
164 cmpne r3, r2 /* Stuffing the same value? */
165 strne r2, [r0] /* Store if not. */
167 #ifdef PMAP_INCLUDE_PTE_SYNC
169 * Need to sync the cache to make sure that last store is
170 * visible to the MMU.
174 ldrne pc, [r9, #CF_DCACHE_WB_RANGE]
175 #endif /* PMAP_INCLUDE_PTE_SYNC */
178 * Note: We don't do the same optimisation as cpu_switch() with
179 * respect to avoiding flushing the TLB if we're switching to
180 * the same L1 since this process' VM space may be about to go
181 * away, so we don't want *any* turds left in the TLB.
184 /* Switch the memory to the new process */
185 ldr r0, [r7, #(PCB_PAGEDIR)]
187 ldr pc, [r9, #CF_CONTEXT_SWITCH]
189 /* Restore all the save registers */
194 ldr r8, [r7, #(PCB_R8)]
195 ldr r9, [r7, #(PCB_R9)]
196 ldr r10, [r7, #(PCB_R10)]
197 ldr r11, [r7, #(PCB_R11)]
198 ldr r12, [r7, #(PCB_R12)]
199 ldr r13, [r7, #(PCB_SP)]
202 /* We have a new curthread now so make a note it */
207 ldr r6, [r5, #(TD_MD + MD_TP)]
208 ldr r4, =ARM_TP_ADDRESS
210 ldr r6, [r5, #(TD_MD + MD_RAS_START)]
211 str r6, [r4, #4] /* ARM_RAS_START */
212 ldr r6, [r5, #(TD_MD + MD_RAS_END)]
213 str r6, [r4, #8] /* ARM_RAS_END */
215 /* Hook in a new pcb */
219 ldmfd sp!, {r4-r7, pc}
222 stmfd sp!, {r4-r7, lr}
223 mov r6, r2 /* Save the mutex */
226 /* rem: r0 = old lwp */
227 /* rem: interrupts are disabled */
229 #ifdef MULTIPROCESSOR
230 /* XXX use curcpu() */
231 ldr r2, .Lcpu_info_store
232 str r2, [r6, #(L_CPU)]
235 /* Process is now on a processor. */
237 /* We have a new curthread now so make a note it */
241 /* Hook in a new pcb */
243 ldr r2, [r1, #TD_PCB]
246 /* rem: r1 = new process */
247 /* rem: interrupts are enabled */
249 /* Stage two : Save old context */
251 /* Get the user structure for the old thread. */
252 ldr r2, [r0, #(TD_PCB)]
253 mov r4, r0 /* Save the old thread. */
255 /* Save all the registers in the old thread's pcb */
257 add r7, r2, #(PCB_R8)
260 strd r8, [r2, #(PCB_R8)]
261 strd r10, [r2, #(PCB_R10)]
262 strd r12, [r2, #(PCB_R12)]
264 str pc, [r2, #(PCB_PC)]
267 * NOTE: We can now use r8-r13 until it is time to restore
268 * them for the new process.
270 /* Store the old tp */
271 ldr r3, =ARM_TP_ADDRESS
273 str r9, [r0, #(TD_MD + MD_TP)]
275 str r9, [r0, #(TD_MD + MD_RAS_START)]
277 str r9, [r0, #(TD_MD + MD_RAS_END)]
280 ldr r9, [r1, #(TD_MD + MD_TP)]
282 ldr r9, [r1, #(TD_MD + MD_RAS_START)]
284 ldr r9, [r1, #(TD_MD + MD_RAS_END)]
287 /* Get the user structure for the new process in r9 */
288 ldr r9, [r1, #(TD_PCB)]
294 * We can do that, since
295 * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
297 orr r8, r3, #(PSR_UND32_MODE)
300 str sp, [r2, #(PCB_UND_SP)]
302 msr cpsr_c, r3 /* Restore the old mode */
303 /* rem: r8 = old PCB */
304 /* rem: r9 = new PCB */
305 /* rem: interrupts are enabled */
307 /* What else needs to be saved Only FPA stuff when that is supported */
309 /* Third phase : restore saved context */
311 /* rem: r8 = old PCB */
312 /* rem: r9 = new PCB */
313 /* rem: interrupts are enabled */
315 ldr r5, [r9, #(PCB_DACR)] /* r5 = new DACR */
316 mov r2, #DOMAIN_CLIENT
317 cmp r5, r2, lsl #(PMAP_DOMAIN_KERNEL * 2) /* Sw to kernel thread? */
318 beq .Lcs_context_switched /* Yup. Don't flush cache */
319 mrc p15, 0, r0, c3, c0, 0 /* r0 = old DACR */
321 * Get the new L1 table pointer into r11. If we're switching to
322 * an LWP with the same address space as the outgoing one, we can
323 * skip the cache purge and the TTB load.
325 * To avoid data dep stalls that would happen anyway, we try
326 * and get some useful work done in the mean time.
328 mrc p15, 0, r10, c2, c0, 0 /* r10 = old L1 */
329 ldr r11, [r9, #(PCB_PAGEDIR)] /* r11 = new L1 */
332 teq r10, r11 /* Same L1? */
333 cmpeq r0, r5 /* Same DACR? */
334 beq .Lcs_context_switched /* yes! */
337 * Definately need to flush the cache.
342 ldr pc, [r1, #CF_IDCACHE_WBINV_ALL]
343 .Lcs_cache_purge_skipped:
345 /* rem: r9 = new PCB */
346 /* rem: r10 = old L1 */
347 /* rem: r11 = new L1 */
350 ldr r7, [r9, #(PCB_PL1VEC)]
353 * Ensure the vector table is accessible by fixing up the L1
355 cmp r7, #0 /* No need to fixup vector table? */
356 ldrne r2, [r7] /* But if yes, fetch current value */
357 ldrne r0, [r9, #(PCB_L1VEC)] /* Fetch new vector_page value */
358 mcr p15, 0, r5, c3, c0, 0 /* Update DACR for new context */
359 cmpne r2, r0 /* Stuffing the same value? */
360 #ifndef PMAP_INCLUDE_PTE_SYNC
361 strne r0, [r7] /* Nope, update it */
364 str r0, [r7] /* Otherwise, update it */
367 * Need to sync the cache to make sure that last store is
368 * visible to the MMU.
374 ldr pc, [r2, #CF_DCACHE_WB_RANGE]
377 #endif /* PMAP_INCLUDE_PTE_SYNC */
379 cmp r10, r11 /* Switching to the same L1? */
381 beq .Lcs_same_l1 /* Yup. */
383 * Do a full context switch, including full TLB flush.
387 ldr pc, [r10, #CF_CONTEXT_SWITCH]
389 b .Lcs_context_switched
392 * We're switching to a different process in the same L1.
393 * In this situation, we only need to flush the TLB for the
394 * vector_page mapping, and even then only if r7 is non-NULL.
398 movne r0, #0 /* We *know* vector_page's VA is 0x0 */
400 ldrne pc, [r10, #CF_TLB_FLUSHID_SE]
402 * We can do that, since
403 * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
406 .Lcs_context_switched:
408 /* Release the old thread */
409 str r6, [r4, #TD_LOCK]
410 ldr r6, .Lblocked_lock
415 ldr r4, [r3, #TD_LOCK]
419 /* XXXSCW: Safe to re-enable FIQs here */
421 /* rem: r9 = new PCB */
425 * We can do that, since
426 * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
428 orr r2, r3, #(PSR_UND32_MODE)
431 ldr sp, [r9, #(PCB_UND_SP)]
433 msr cpsr_c, r3 /* Restore the old mode */
434 /* Restore all the save registers */
438 sub r7, r7, #PCB_R8 /* restore PCB pointer */
441 ldr r8, [r7, #(PCB_R8)]
442 ldr r9, [r7, #(PCB_R9)]
443 ldr r10, [r7, #(PCB_R10)]
444 ldr r11, [r7, #(PCB_R11)]
445 ldr r12, [r7, #(PCB_R12)]
446 ldr r13, [r7, #(PCB_SP)]
450 /* rem: r7 = new pcb */
453 add r0, r7, #(USER_SIZE) & 0x00ff
454 add r0, r0, #(USER_SIZE) & 0xff00
455 bl _C_LABEL(arm_fpe_core_changecontext)
458 /* rem: r5 = new lwp's proc */
460 /* rem: r7 = new PCB */
465 * Pull the registers that got pushed when either savectx() or
466 * cpu_switch() was called and return.
468 ldmfd sp!, {r4-r7, pc}
471 adr r0, .Lswitch_panic_str
477 .asciz "cpu_switch: sched_qs empty with non-zero sched_whichqs!\n"
480 stmfd sp!, {r4-r7, lr}
484 /* Store all the registers in the process's pcb */
485 add r2, r0, #(PCB_R8)
487 ldmfd sp!, {r4-r7, pc}
489 ENTRY(fork_trampoline)
494 bl _C_LABEL(fork_exit)
497 orr r0, r0, #(I32_bit|F32_bit)
502 movs pc, lr /* Exit */