]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/swtch.S
Change the way pcpu and curthread are stored per-core:
[FreeBSD/FreeBSD.git] / sys / arm / arm / swtch.S
1 /*      $NetBSD: cpuswitch.S,v 1.41 2003/11/15 08:44:18 scw 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
81 #include "assym.s"
82 #include "opt_sched.h"
83
84 #include <machine/asm.h>
85 #include <machine/asmacros.h>
86 #include <machine/armreg.h>
87 __FBSDID("$FreeBSD$");
88
89 #define DOMAIN_CLIENT   0x01
90
91 #ifdef _ARM_ARCH_6
92 #define GET_PCPU(tmp, tmp2) \
93         mrc     p15, 0, tmp, c0, c0, 5; \
94         and     tmp, tmp, #0xf;         \
95         ldr     tmp2, .Lcurpcpu+4;      \
96         mul     tmp, tmp, tmp2;         \
97         ldr     tmp2, .Lcurpcpu;        \
98         add     tmp, tmp, tmp2;
99 #else
100
101 #define GET_PCPU(tmp, tmp2) \
102         ldr     tmp, .Lcurpcpu
103 #endif
104
105 .Lcurpcpu:
106         .word   _C_LABEL(__pcpu)
107         .word   PCPU_SIZE
108 .Lcpufuncs:     
109         .word   _C_LABEL(cpufuncs)
110 .Lblocked_lock:
111         .word   _C_LABEL(blocked_lock)
112
113 ENTRY(cpu_throw)
114         mov     r5, r1
115
116         /*
117          * r0 = oldtd
118          * r5 = newtd
119          */
120
121         GET_PCPU(r7, r9)
122
123 #ifdef VFP
124         /*
125          * vfp_discard will clear pcpu->pc_vfpcthread, and modify
126          * and modify the control as needed.
127          */
128         ldr     r4, [r7, #(PC_VFPCTHREAD)]      /* this thread using vfp? */
129         cmp     r0, r4
130         bne     3f
131         bl      _C_LABEL(vfp_discard)           /* yes, shut down vfp */
132 3:
133 #endif          /* VFP */
134
135         ldr     r7, [r5, #(TD_PCB)]             /* r7 = new thread's PCB */
136   
137         /* Switch to lwp0 context */
138
139         ldr     r9, .Lcpufuncs
140 #if !defined(CPU_ARM11) && !defined(CPU_CORTEXA) && !defined(CPU_MV_PJ4B) && !defined(CPU_KRAIT)
141         mov     lr, pc
142         ldr     pc, [r9, #CF_IDCACHE_WBINV_ALL]
143 #endif
144         ldr     r0, [r7, #(PCB_PL1VEC)]
145         ldr     r1, [r7, #(PCB_DACR)]
146         /*
147          * r0 = Pointer to L1 slot for vector_page (or NULL)
148          * r1 = lwp0's DACR
149          * r5 = lwp0
150          * r6 = exit func
151          * r7 = lwp0's PCB
152          * r9 = cpufuncs
153          */
154
155         /*
156          * Ensure the vector table is accessible by fixing up lwp0's L1
157          */
158         cmp     r0, #0                  /* No need to fixup vector table? */
159         ldrne   r3, [r0]                /* But if yes, fetch current value */
160         ldrne   r2, [r7, #(PCB_L1VEC)]  /* Fetch new vector_page value */
161         mcr     p15, 0, r1, c3, c0, 0   /* Update DACR for lwp0's context */
162         cmpne   r3, r2                  /* Stuffing the same value? */
163         strne   r2, [r0]                /* Store if not. */
164
165 #ifdef PMAP_INCLUDE_PTE_SYNC
166         /*
167          * Need to sync the cache to make sure that last store is
168          * visible to the MMU.
169          */
170         movne   r1, #4
171         movne   lr, pc
172         ldrne   pc, [r9, #CF_DCACHE_WB_RANGE]
173 #endif /* PMAP_INCLUDE_PTE_SYNC */
174
175         /*
176          * Note: We don't do the same optimisation as cpu_switch() with
177          * respect to avoiding flushing the TLB if we're switching to
178          * the same L1 since this process' VM space may be about to go
179          * away, so we don't want *any* turds left in the TLB.
180          */
181
182         /* Switch the memory to the new process */
183         ldr     r0, [r7, #(PCB_PAGEDIR)]
184         mov     lr, pc
185         ldr     pc, [r9, #CF_CONTEXT_SWITCH]
186
187         /* Restore all the save registers */
188 #ifndef _ARM_ARCH_5E
189         add     r1, r7, #PCB_R8
190         ldmia   r1, {r8-r13}
191 #else
192         ldr     r8, [r7, #(PCB_R8)]
193         ldr     r9, [r7, #(PCB_R9)]
194         ldr     r10, [r7, #(PCB_R10)]
195         ldr     r11, [r7, #(PCB_R11)]
196         ldr     r12, [r7, #(PCB_R12)]
197         ldr     r13, [r7, #(PCB_SP)]
198 #endif
199
200         GET_PCPU(r6, r4)
201         /* Hook in a new pcb */
202         str     r7, [r6, #PC_CURPCB]
203         /* We have a new curthread now so make a note it */
204         add     r6, r6, #PC_CURTHREAD
205         str     r5, [r6]
206 #ifndef ARM_TP_ADDRESS
207         mcr     p15, 0, r5, c13, c0, 4
208 #endif
209         /* Set the new tp */
210         ldr     r6, [r5, #(TD_MD + MD_TP)]
211 #ifdef ARM_TP_ADDRESS
212         ldr     r4, =ARM_TP_ADDRESS
213         str     r6, [r4]
214         ldr     r6, [r5, #(TD_MD + MD_RAS_START)]
215         str     r6, [r4, #4] /* ARM_RAS_START */
216         ldr     r6, [r5, #(TD_MD + MD_RAS_END)]
217         str     r6, [r4, #8] /* ARM_RAS_END */
218 #else
219         mcr p15, 0, r6, c13, c0, 3
220 #endif
221
222         add     sp, sp, #4;
223         ldmfd   sp!, {r4-r7, pc}
224 END(cpu_throw)
225
226 ENTRY(cpu_switch)
227         stmfd   sp!, {r4-r7, lr}
228         sub     sp, sp, #4;
229 #ifdef __ARM_EABI__
230         .save   {r4-r7, lr}
231         .pad    #4
232 #endif
233
234         mov     r6, r2 /* Save the mutex */
235
236 .Lswitch_resume:
237         /* rem: r0 = old lwp */
238         /* rem: interrupts are disabled */
239
240         /* Process is now on a processor. */
241         /* We have a new curthread now so make a note it */
242         GET_PCPU(r7, r2)
243         add     r7, r7, #PC_CURTHREAD
244         str     r1, [r7]
245 #ifndef ARM_TP_ADDRESS
246         mcr     p15, 0, r1, c13, c0, 4
247 #endif
248
249         /* Hook in a new pcb */
250         ldr     r2, [r1, #TD_PCB]
251         str     r2, [r7, #PC_CURPCB]
252
253         /* rem: r1 = new process */
254         /* rem: interrupts are enabled */
255
256         /* Stage two : Save old context */
257
258         /* Get the user structure for the old thread. */
259         ldr     r2, [r0, #(TD_PCB)]
260         mov     r4, r0 /* Save the old thread. */
261
262         /* Save all the registers in the old thread's pcb */
263 #ifndef _ARM_ARCH_5E
264         add     r7, r2, #(PCB_R8)
265         stmia   r7, {r8-r13}
266 #else
267         strd    r8, [r2, #(PCB_R8)]
268         strd    r10, [r2, #(PCB_R10)]
269         strd    r12, [r2, #(PCB_R12)]
270 #endif
271         str     pc, [r2, #(PCB_PC)]
272
273         /*
274          * NOTE: We can now use r8-r13 until it is time to restore
275          * them for the new process.
276          */
277 #ifdef ARM_TP_ADDRESS
278         /* Store the old tp */
279         ldr     r3, =ARM_TP_ADDRESS
280         ldr     r9, [r3]
281         str     r9, [r0, #(TD_MD + MD_TP)]
282         ldr     r9, [r3, #4]
283         str     r9, [r0, #(TD_MD + MD_RAS_START)]
284         ldr     r9, [r3, #8]
285         str     r9, [r0, #(TD_MD + MD_RAS_END)]
286
287         /* Set the new tp */
288         ldr     r9, [r1, #(TD_MD + MD_TP)]
289         str     r9, [r3]
290         ldr     r9, [r1, #(TD_MD + MD_RAS_START)]
291         str     r9, [r3, #4]
292         ldr     r9, [r1, #(TD_MD + MD_RAS_END)]
293         str     r9, [r3, #8]
294 #else
295         /* Store the old tp */
296         mrc p15, 0, r9, c13, c0, 3
297         str     r9, [r0, #(TD_MD + MD_TP)]
298
299         /* Set the new tp */
300         ldr     r9, [r1, #(TD_MD + MD_TP)]
301         mcr p15, 0, r9, c13, c0, 3
302 #endif
303         
304         /* Get the user structure for the new process in r9 */
305         ldr     r9, [r1, #(TD_PCB)]
306
307         mrs     r3, cpsr
308         /*
309          * We can do that, since
310          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
311          */
312         orr     r8, r3, #(PSR_UND32_MODE)
313         msr     cpsr_c, r8
314
315         str     sp, [r2, #(PCB_UND_SP)]
316
317         msr     cpsr_c, r3              /* Restore the old mode */
318         /* rem: r2 = old PCB */
319         /* rem: r9 = new PCB */
320         /* rem: interrupts are enabled */
321
322 #ifdef VFP
323         /*
324          * vfp_store will clear pcpu->pc_vfpcthread, save 
325          * registers and state, and modify the control as needed.
326          * a future exception will bounce the backup settings in the fp unit.
327          * XXX vfp_store can't change r4
328          */
329         GET_PCPU(r7, r8)
330         ldr     r8, [r7, #(PC_VFPCTHREAD)]
331         cmp     r4, r8                          /* old thread used vfp? */
332         bne     1f                              /* no, don't save */
333         cmp     r1, r4                          /* same thread ? */
334         beq     1f                              /* yes, skip vfp store */
335 #ifdef SMP
336         ldr     r8, [r7, #(PC_CPU)]             /* last used on this cpu? */
337         ldr     r3, [r2, #(PCB_VFPCPU)]
338         cmp     r8, r3          /* last cpu to use these registers? */
339         bne     1f              /* no. these values are stale */
340 #endif
341         add     r0, r2, #(PCB_VFPSTATE)
342         bl      _C_LABEL(vfp_store)
343 1:
344 #endif          /* VFP */
345
346         /* r1 now free! */
347
348         /* Third phase : restore saved context */
349
350         /* rem: r2 = old PCB */
351         /* rem: r9 = new PCB */
352         /* rem: interrupts are enabled */
353
354         ldr     r5, [r9, #(PCB_DACR)]           /* r5 = new DACR */
355         mov     r2, #DOMAIN_CLIENT
356         cmp     r5, r2, lsl #(PMAP_DOMAIN_KERNEL * 2) /* Sw to kernel thread? */
357         beq     .Lcs_context_switched        /* Yup. Don't flush cache */
358         mrc     p15, 0, r0, c3, c0, 0           /* r0 = old DACR */
359         /*
360          * Get the new L1 table pointer into r11.  If we're switching to
361          * an LWP with the same address space as the outgoing one, we can
362          * skip the cache purge and the TTB load.
363          *
364          * To avoid data dep stalls that would happen anyway, we try
365          * and get some useful work done in the mean time.
366          */
367         mrc     p15, 0, r10, c2, c0, 0          /* r10 = old L1 */
368         ldr     r11, [r9, #(PCB_PAGEDIR)]       /* r11 = new L1 */
369
370
371         teq     r10, r11                        /* Same L1? */
372         cmpeq   r0, r5                          /* Same DACR? */
373         beq     .Lcs_context_switched           /* yes! */
374
375 #if !defined(CPU_ARM11) && !defined(CPU_CORTEXA) && !defined(CPU_MV_PJ4B) && !defined(CPU_KRAIT)
376         /*
377          * Definately need to flush the cache.
378          */
379
380         ldr     r1, .Lcpufuncs
381         mov     lr, pc
382         ldr     pc, [r1, #CF_IDCACHE_WBINV_ALL]
383 #endif
384 .Lcs_cache_purge_skipped:
385         /* rem: r6 = lock */
386         /* rem: r9 = new PCB */
387         /* rem: r10 = old L1 */
388         /* rem: r11 = new L1 */
389
390         mov     r2, #0x00000000
391         ldr     r7, [r9, #(PCB_PL1VEC)]
392
393         /*
394          * Ensure the vector table is accessible by fixing up the L1
395          */
396         cmp     r7, #0                  /* No need to fixup vector table? */
397         ldrne   r2, [r7]                /* But if yes, fetch current value */
398         ldrne   r0, [r9, #(PCB_L1VEC)]  /* Fetch new vector_page value */
399         mcr     p15, 0, r5, c3, c0, 0   /* Update DACR for new context */
400         cmpne   r2, r0                  /* Stuffing the same value? */
401 #ifndef PMAP_INCLUDE_PTE_SYNC
402         strne   r0, [r7]                /* Nope, update it */
403 #else
404         beq     .Lcs_same_vector
405         str     r0, [r7]                /* Otherwise, update it */
406
407         /*
408          * Need to sync the cache to make sure that last store is
409          * visible to the MMU.
410          */
411         ldr     r2, .Lcpufuncs
412         mov     r0, r7
413         mov     r1, #4
414         mov     lr, pc
415         ldr     pc, [r2, #CF_DCACHE_WB_RANGE]
416
417 .Lcs_same_vector:
418 #endif /* PMAP_INCLUDE_PTE_SYNC */
419
420         cmp     r10, r11                /* Switching to the same L1? */
421         ldr     r10, .Lcpufuncs
422         beq     .Lcs_same_l1            /* Yup. */
423         /*
424          * Do a full context switch, including full TLB flush.
425          */
426         mov     r0, r11
427         mov     lr, pc
428         ldr     pc, [r10, #CF_CONTEXT_SWITCH]
429
430         b       .Lcs_context_switched
431
432         /*
433          * We're switching to a different process in the same L1.
434          * In this situation, we only need to flush the TLB for the
435          * vector_page mapping, and even then only if r7 is non-NULL.
436          */
437 .Lcs_same_l1:
438         cmp     r7, #0
439         movne   r0, #0                  /* We *know* vector_page's VA is 0x0 */
440         movne   lr, pc
441         ldrne   pc, [r10, #CF_TLB_FLUSHID_SE]
442         /*
443          * We can do that, since
444          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
445          */
446
447 .Lcs_context_switched:
448
449         /* Release the old thread */
450         str     r6, [r4, #TD_LOCK]
451 #if defined(SCHED_ULE) && defined(SMP)
452         ldr     r6, .Lblocked_lock
453         GET_CURTHREAD_PTR(r3)
454 1:
455         ldr     r4, [r3, #TD_LOCK]
456         cmp     r4, r6
457         beq     1b
458 #endif
459         
460         /* XXXSCW: Safe to re-enable FIQs here */
461
462         /* rem: r9 = new PCB */
463
464         mrs     r3, cpsr
465         /*
466          * We can do that, since
467          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
468          */
469         orr     r2, r3, #(PSR_UND32_MODE)
470         msr     cpsr_c, r2
471
472         ldr     sp, [r9, #(PCB_UND_SP)]
473
474         msr     cpsr_c, r3              /* Restore the old mode */
475         /* Restore all the save registers */
476 #ifndef _ARM_ARCH_5E
477         add     r7, r9, #PCB_R8
478         ldmia   r7, {r8-r13}
479         sub     r7, r7, #PCB_R8         /* restore PCB pointer */
480 #else
481         mov     r7, r9
482         ldr     r8, [r7, #(PCB_R8)]
483         ldr     r9, [r7, #(PCB_R9)]
484         ldr     r10, [r7, #(PCB_R10)]
485         ldr     r11, [r7, #(PCB_R11)]
486         ldr     r12, [r7, #(PCB_R12)]
487         ldr     r13, [r7, #(PCB_SP)]
488 #endif
489
490         /* rem: r5 = new lwp's proc */
491         /* rem: r6 = lock */
492         /* rem: r7 = new PCB */
493
494 .Lswitch_return:
495
496         /*
497          * Pull the registers that got pushed when either savectx() or
498          * cpu_switch() was called and return.
499          */
500         add     sp, sp, #4;
501         ldmfd   sp!, {r4-r7, pc}
502 #ifdef DIAGNOSTIC
503 .Lswitch_bogons:
504         adr     r0, .Lswitch_panic_str
505         bl      _C_LABEL(panic)
506 1:      nop
507         b       1b
508
509 .Lswitch_panic_str:
510         .asciz  "cpu_switch: sched_qs empty with non-zero sched_whichqs!\n"
511 #endif
512 END(cpu_switch)
513
514 ENTRY(savectx)
515         stmfd   sp!, {r4-r7, lr}
516         sub     sp, sp, #4
517         /*
518          * r0 = pcb
519          */
520         /* Store all the registers in the process's pcb */
521         add     r2, r0, #(PCB_R8)
522         stmia   r2, {r8-r13}
523 #ifdef VFP
524         /*
525          * vfp_store will clear pcpu->pc_vfpcthread, save 
526          * registers and state, and modify the control as needed.
527          * a future exception will bounce the backup settings in the fp unit.
528          */
529         GET_PCPU(r7, r4)
530         ldr     r4, [r7, #(PC_VFPCTHREAD)]      /* vfp thread */
531         ldr     r2, [r7, #(PC_CURTHREAD)]       /* current thread */
532         cmp     r4, r2
533         bne     1f
534 #ifdef SMP
535         ldr     r2, [r7, #(PC_CPU)]     /* last used on this cpu? */
536         ldr     r3, [r0, #(PCB_VFPCPU)]
537         cmp     r2, r3
538         bne     1f              /* no. these values are stale */
539 #endif
540         add     r0, r0, #(PCB_VFPSTATE)
541         bl      _C_LABEL(vfp_store)
542 1:
543 #endif          /* VFP */
544         add     sp, sp, #4;
545         ldmfd   sp!, {r4-r7, pc}
546 END(savectx)
547
548 ENTRY(fork_trampoline)
549         STOP_UNWINDING  /* Can't unwind beyond the thread enty point */
550         mov     r1, r5
551         mov     r2, sp
552         mov     r0, r4
553         mov     fp, #0
554         bl      _C_LABEL(fork_exit)
555         /* Kill irq"s */
556         mrs     r0, cpsr
557         orr     r0, r0, #(I32_bit|F32_bit)
558         msr     cpsr_c, r0
559         DO_AST
560         PULLFRAME
561
562         movs    pc, lr                  /* Exit */
563
564 AST_LOCALS
565 END(fork_trampoline)
566