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
82 #include "opt_sched.h"
84 #include <machine/asm.h>
85 #include <machine/asmacros.h>
86 #include <machine/armreg.h>
87 #include <machine/vfp.h>
89 __FBSDID("$FreeBSD$");
91 #define DOMAIN_CLIENT 0x01
93 #if defined(_ARM_ARCH_6) && defined(SMP)
94 #define GET_PCPU(tmp, tmp2) \
95 mrc p15, 0, tmp, c0, c0, 5; \
97 ldr tmp2, .Lcurpcpu+4; \
99 ldr tmp2, .Lcurpcpu; \
103 #define GET_PCPU(tmp, tmp2) \
108 .fpu vfp /* allow VFP instructions */
112 .word _C_LABEL(__pcpu)
115 .word _C_LABEL(cpufuncs)
117 .word _C_LABEL(blocked_lock)
120 * cpu_throw(oldtd, newtd)
122 * Remove current thread state, then select the next thread to run
123 * and load its state.
135 #ifdef VFP /* This thread is dying, disable */
136 bl _C_LABEL(vfp_discard) /* VFP without preserving state. */
140 ldr r7, [r5, #(TD_PCB)] /* r7 = new thread's PCB */
142 /* Switch to lwp0 context */
145 #if !defined(CPU_ARM11) && !defined(CPU_CORTEXA) && !defined(CPU_MV_PJ4B) && !defined(CPU_KRAIT)
147 ldr pc, [r9, #CF_IDCACHE_WBINV_ALL]
149 ldr r0, [r7, #(PCB_PL1VEC)]
150 ldr r1, [r7, #(PCB_DACR)]
152 * r0 = Pointer to L1 slot for vector_page (or NULL)
160 * Ensure the vector table is accessible by fixing up lwp0's L1
162 cmp r0, #0 /* No need to fixup vector table? */
163 ldrne r3, [r0] /* But if yes, fetch current value */
164 ldrne r2, [r7, #(PCB_L1VEC)] /* Fetch new vector_page value */
165 mcr p15, 0, r1, c3, c0, 0 /* Update DACR for lwp0's context */
166 cmpne r3, r2 /* Stuffing the same value? */
167 strne r2, [r0] /* Store if not. */
169 #ifdef PMAP_INCLUDE_PTE_SYNC
171 * Need to sync the cache to make sure that last store is
172 * visible to the MMU.
176 ldrne pc, [r9, #CF_DCACHE_WB_RANGE]
177 #endif /* PMAP_INCLUDE_PTE_SYNC */
180 * Note: We don't do the same optimisation as cpu_switch() with
181 * respect to avoiding flushing the TLB if we're switching to
182 * the same L1 since this process' VM space may be about to go
183 * away, so we don't want *any* turds left in the TLB.
186 /* Switch the memory to the new process */
187 ldr r0, [r7, #(PCB_PAGEDIR)]
189 ldr pc, [r9, #CF_CONTEXT_SWITCH]
192 /* Hook in a new pcb */
193 str r7, [r6, #PC_CURPCB]
194 /* We have a new curthread now so make a note it */
195 str r5, [r6, #PC_CURTHREAD]
196 #ifndef ARM_TP_ADDRESS
197 mcr p15, 0, r5, c13, c0, 4
200 ldr r6, [r5, #(TD_MD + MD_TP)]
201 #ifdef ARM_TP_ADDRESS
202 ldr r4, =ARM_TP_ADDRESS
204 ldr r6, [r5, #(TD_MD + MD_RAS_START)]
205 str r6, [r4, #4] /* ARM_RAS_START */
206 ldr r6, [r5, #(TD_MD + MD_RAS_END)]
207 str r6, [r4, #8] /* ARM_RAS_END */
209 mcr p15, 0, r6, c13, c0, 3
211 /* Restore all the saved registers and exit */
213 ldmia r3, {r4-r12, sp, pc}
217 * cpu_switch(oldtd, newtd, lock)
219 * Save the current thread state, then select the next thread to run
220 * and load its state.
223 * r2 = lock (new lock for old thread)
226 /* Interrupts are disabled. */
227 /* Save all the registers in the old thread's pcb. */
228 ldr r3, [r0, #(TD_PCB)]
230 /* Restore all the saved registers and exit */
232 stmia r3, {r4-r12, sp, lr, pc}
234 mov r6, r2 /* Save the mutex */
236 /* rem: r0 = old lwp */
237 /* rem: interrupts are disabled */
239 /* Process is now on a processor. */
240 /* We have a new curthread now so make a note it */
242 str r1, [r7, #PC_CURTHREAD]
243 #ifndef ARM_TP_ADDRESS
244 mcr p15, 0, r1, c13, c0, 4
247 /* Hook in a new pcb */
248 ldr r2, [r1, #TD_PCB]
249 str r2, [r7, #PC_CURPCB]
251 /* Stage two : Save old context */
253 /* Get the user structure for the old thread. */
254 ldr r2, [r0, #(TD_PCB)]
255 mov r4, r0 /* Save the old thread. */
257 #ifdef ARM_TP_ADDRESS
258 /* Store the old tp */
259 ldr r3, =ARM_TP_ADDRESS
261 str r9, [r0, #(TD_MD + MD_TP)]
263 str r9, [r0, #(TD_MD + MD_RAS_START)]
265 str r9, [r0, #(TD_MD + MD_RAS_END)]
268 ldr r9, [r1, #(TD_MD + MD_TP)]
270 ldr r9, [r1, #(TD_MD + MD_RAS_START)]
272 ldr r9, [r1, #(TD_MD + MD_RAS_END)]
275 /* Store the old tp */
276 mrc p15, 0, r9, c13, c0, 3
277 str r9, [r0, #(TD_MD + MD_TP)]
280 ldr r9, [r1, #(TD_MD + MD_TP)]
281 mcr p15, 0, r9, c13, c0, 3
284 /* Get the user structure for the new process in r9 */
285 ldr r9, [r1, #(TD_PCB)]
287 /* rem: r2 = old PCB */
288 /* rem: r9 = new PCB */
289 /* rem: interrupts are enabled */
292 fmrx r0, fpexc /* If the VFP is enabled */
293 tst r0, #(VFPEXC_EN) /* the current thread has */
294 movne r1, #1 /* used it, so go save */
295 addne r0, r2, #(PCB_VFPSTATE) /* the state into the PCB */
296 blne _C_LABEL(vfp_store) /* and disable the VFP. */
299 /* r0-r3 now free! */
301 /* Third phase : restore saved context */
303 /* rem: r2 = old PCB */
304 /* rem: r9 = new PCB */
306 ldr r5, [r9, #(PCB_DACR)] /* r5 = new DACR */
307 mov r2, #DOMAIN_CLIENT
308 cmp r5, r2, lsl #(PMAP_DOMAIN_KERNEL * 2) /* Sw to kernel thread? */
309 beq .Lcs_context_switched /* Yup. Don't flush cache */
310 mrc p15, 0, r0, c3, c0, 0 /* r0 = old DACR */
312 * Get the new L1 table pointer into r11. If we're switching to
313 * an LWP with the same address space as the outgoing one, we can
314 * skip the cache purge and the TTB load.
316 * To avoid data dep stalls that would happen anyway, we try
317 * and get some useful work done in the mean time.
319 mrc p15, 0, r10, c2, c0, 0 /* r10 = old L1 */
320 ldr r11, [r9, #(PCB_PAGEDIR)] /* r11 = new L1 */
322 teq r10, r11 /* Same L1? */
323 cmpeq r0, r5 /* Same DACR? */
324 beq .Lcs_context_switched /* yes! */
326 #if !defined(CPU_ARM11) && !defined(CPU_CORTEXA) && !defined(CPU_MV_PJ4B) && !defined(CPU_KRAIT)
328 * Definately need to flush the cache.
333 ldr pc, [r1, #CF_IDCACHE_WBINV_ALL]
335 .Lcs_cache_purge_skipped:
337 /* rem: r9 = new PCB */
338 /* rem: r10 = old L1 */
339 /* rem: r11 = new L1 */
342 ldr r7, [r9, #(PCB_PL1VEC)]
345 * Ensure the vector table is accessible by fixing up the L1
347 cmp r7, #0 /* No need to fixup vector table? */
348 ldrne r2, [r7] /* But if yes, fetch current value */
349 ldrne r0, [r9, #(PCB_L1VEC)] /* Fetch new vector_page value */
350 mcr p15, 0, r5, c3, c0, 0 /* Update DACR for new context */
351 cmpne r2, r0 /* Stuffing the same value? */
352 #ifndef PMAP_INCLUDE_PTE_SYNC
353 strne r0, [r7] /* Nope, update it */
356 str r0, [r7] /* Otherwise, update it */
359 * Need to sync the cache to make sure that last store is
360 * visible to the MMU.
366 ldr pc, [r2, #CF_DCACHE_WB_RANGE]
369 #endif /* PMAP_INCLUDE_PTE_SYNC */
371 cmp r10, r11 /* Switching to the same L1? */
373 beq .Lcs_same_l1 /* Yup. */
375 * Do a full context switch, including full TLB flush.
379 ldr pc, [r10, #CF_CONTEXT_SWITCH]
381 b .Lcs_context_switched
384 * We're switching to a different process in the same L1.
385 * In this situation, we only need to flush the TLB for the
386 * vector_page mapping, and even then only if r7 is non-NULL.
390 movne r0, #0 /* We *know* vector_page's VA is 0x0 */
392 ldrne pc, [r10, #CF_TLB_FLUSHID_SE]
394 .Lcs_context_switched:
396 /* Release the old thread */
397 str r6, [r4, #TD_LOCK]
398 #if defined(SCHED_ULE) && defined(SMP)
399 ldr r6, .Lblocked_lock
400 GET_CURTHREAD_PTR(r3)
402 ldr r4, [r3, #TD_LOCK]
407 /* XXXSCW: Safe to re-enable FIQs here */
409 /* rem: r9 = new PCB */
411 /* Restore all the saved registers and exit */
413 ldmia r3, {r4-r12, sp, pc}
420 /* Store all the registers in the thread's pcb */
421 add r3, r0, #(PCB_R4)
422 stmia r3, {r4-r12, sp, lr, pc}
424 fmrx r2, fpexc /* If the VFP is enabled */
425 tst r2, #(VFPEXC_EN) /* the current thread has */
426 movne r1, #1 /* used it, so go save */
427 addne r0, r0, #(PCB_VFPSTATE) /* the state into the PCB */
428 blne _C_LABEL(vfp_store) /* and disable the VFP. */
434 ENTRY(fork_trampoline)
435 STOP_UNWINDING /* EABI: Don't unwind beyond the thread enty point. */
436 mov fp, #0 /* OABI: Stack traceback via fp stops here. */
440 ldr lr, =swi_exit /* Go finish forking, then return */
441 b _C_LABEL(fork_exit) /* to userland via swi_exit code. */