]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/swtch.S
4478 dtrace_dof_maxsize is far too small
[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 #if defined(_ARM_ARCH_6) && defined(SMP)
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         str     r1, [r7, #PC_CURTHREAD]
244 #ifndef ARM_TP_ADDRESS
245         mcr     p15, 0, r1, c13, c0, 4
246 #endif
247
248         /* Hook in a new pcb */
249         ldr     r2, [r1, #TD_PCB]
250         str     r2, [r7, #PC_CURPCB]
251
252         /* rem: r1 = new process */
253         /* rem: interrupts are enabled */
254
255         /* Stage two : Save old context */
256
257         /* Get the user structure for the old thread. */
258         ldr     r2, [r0, #(TD_PCB)]
259         mov     r4, r0 /* Save the old thread. */
260
261         /* Save all the registers in the old thread's pcb */
262 #ifndef _ARM_ARCH_5E
263         add     r7, r2, #(PCB_R8)
264         stmia   r7, {r8-r13}
265 #else
266         strd    r8, [r2, #(PCB_R8)]
267         strd    r10, [r2, #(PCB_R10)]
268         strd    r12, [r2, #(PCB_R12)]
269 #endif
270         str     pc, [r2, #(PCB_PC)]
271
272         /*
273          * NOTE: We can now use r8-r13 until it is time to restore
274          * them for the new process.
275          */
276 #ifdef ARM_TP_ADDRESS
277         /* Store the old tp */
278         ldr     r3, =ARM_TP_ADDRESS
279         ldr     r9, [r3]
280         str     r9, [r0, #(TD_MD + MD_TP)]
281         ldr     r9, [r3, #4]
282         str     r9, [r0, #(TD_MD + MD_RAS_START)]
283         ldr     r9, [r3, #8]
284         str     r9, [r0, #(TD_MD + MD_RAS_END)]
285
286         /* Set the new tp */
287         ldr     r9, [r1, #(TD_MD + MD_TP)]
288         str     r9, [r3]
289         ldr     r9, [r1, #(TD_MD + MD_RAS_START)]
290         str     r9, [r3, #4]
291         ldr     r9, [r1, #(TD_MD + MD_RAS_END)]
292         str     r9, [r3, #8]
293 #else
294         /* Store the old tp */
295         mrc p15, 0, r9, c13, c0, 3
296         str     r9, [r0, #(TD_MD + MD_TP)]
297
298         /* Set the new tp */
299         ldr     r9, [r1, #(TD_MD + MD_TP)]
300         mcr p15, 0, r9, c13, c0, 3
301 #endif
302         
303         /* Get the user structure for the new process in r9 */
304         ldr     r9, [r1, #(TD_PCB)]
305
306         mrs     r3, cpsr
307         /*
308          * We can do that, since
309          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
310          */
311         orr     r8, r3, #(PSR_UND32_MODE)
312         msr     cpsr_c, r8
313
314         str     sp, [r2, #(PCB_UND_SP)]
315
316         msr     cpsr_c, r3              /* Restore the old mode */
317         /* rem: r2 = old PCB */
318         /* rem: r9 = new PCB */
319         /* rem: interrupts are enabled */
320
321 #ifdef VFP
322         /*
323          * vfp_store will clear pcpu->pc_vfpcthread, save 
324          * registers and state, and modify the control as needed.
325          * a future exception will bounce the backup settings in the fp unit.
326          * XXX vfp_store can't change r4
327          */
328         GET_PCPU(r7, r8)
329         ldr     r8, [r7, #(PC_VFPCTHREAD)]
330         cmp     r4, r8                          /* old thread used vfp? */
331         bne     1f                              /* no, don't save */
332         cmp     r1, r4                          /* same thread ? */
333         beq     1f                              /* yes, skip vfp store */
334 #ifdef SMP
335         ldr     r8, [r7, #(PC_CPU)]             /* last used on this cpu? */
336         ldr     r3, [r2, #(PCB_VFPCPU)]
337         cmp     r8, r3          /* last cpu to use these registers? */
338         bne     1f              /* no. these values are stale */
339 #endif
340         add     r0, r2, #(PCB_VFPSTATE)
341         bl      _C_LABEL(vfp_store)
342 1:
343 #endif          /* VFP */
344
345         /* r1 now free! */
346
347         /* Third phase : restore saved context */
348
349         /* rem: r2 = old PCB */
350         /* rem: r9 = new PCB */
351         /* rem: interrupts are enabled */
352
353         ldr     r5, [r9, #(PCB_DACR)]           /* r5 = new DACR */
354         mov     r2, #DOMAIN_CLIENT
355         cmp     r5, r2, lsl #(PMAP_DOMAIN_KERNEL * 2) /* Sw to kernel thread? */
356         beq     .Lcs_context_switched        /* Yup. Don't flush cache */
357         mrc     p15, 0, r0, c3, c0, 0           /* r0 = old DACR */
358         /*
359          * Get the new L1 table pointer into r11.  If we're switching to
360          * an LWP with the same address space as the outgoing one, we can
361          * skip the cache purge and the TTB load.
362          *
363          * To avoid data dep stalls that would happen anyway, we try
364          * and get some useful work done in the mean time.
365          */
366         mrc     p15, 0, r10, c2, c0, 0          /* r10 = old L1 */
367         ldr     r11, [r9, #(PCB_PAGEDIR)]       /* r11 = new L1 */
368
369
370         teq     r10, r11                        /* Same L1? */
371         cmpeq   r0, r5                          /* Same DACR? */
372         beq     .Lcs_context_switched           /* yes! */
373
374 #if !defined(CPU_ARM11) && !defined(CPU_CORTEXA) && !defined(CPU_MV_PJ4B) && !defined(CPU_KRAIT)
375         /*
376          * Definately need to flush the cache.
377          */
378
379         ldr     r1, .Lcpufuncs
380         mov     lr, pc
381         ldr     pc, [r1, #CF_IDCACHE_WBINV_ALL]
382 #endif
383 .Lcs_cache_purge_skipped:
384         /* rem: r6 = lock */
385         /* rem: r9 = new PCB */
386         /* rem: r10 = old L1 */
387         /* rem: r11 = new L1 */
388
389         mov     r2, #0x00000000
390         ldr     r7, [r9, #(PCB_PL1VEC)]
391
392         /*
393          * Ensure the vector table is accessible by fixing up the L1
394          */
395         cmp     r7, #0                  /* No need to fixup vector table? */
396         ldrne   r2, [r7]                /* But if yes, fetch current value */
397         ldrne   r0, [r9, #(PCB_L1VEC)]  /* Fetch new vector_page value */
398         mcr     p15, 0, r5, c3, c0, 0   /* Update DACR for new context */
399         cmpne   r2, r0                  /* Stuffing the same value? */
400 #ifndef PMAP_INCLUDE_PTE_SYNC
401         strne   r0, [r7]                /* Nope, update it */
402 #else
403         beq     .Lcs_same_vector
404         str     r0, [r7]                /* Otherwise, update it */
405
406         /*
407          * Need to sync the cache to make sure that last store is
408          * visible to the MMU.
409          */
410         ldr     r2, .Lcpufuncs
411         mov     r0, r7
412         mov     r1, #4
413         mov     lr, pc
414         ldr     pc, [r2, #CF_DCACHE_WB_RANGE]
415
416 .Lcs_same_vector:
417 #endif /* PMAP_INCLUDE_PTE_SYNC */
418
419         cmp     r10, r11                /* Switching to the same L1? */
420         ldr     r10, .Lcpufuncs
421         beq     .Lcs_same_l1            /* Yup. */
422         /*
423          * Do a full context switch, including full TLB flush.
424          */
425         mov     r0, r11
426         mov     lr, pc
427         ldr     pc, [r10, #CF_CONTEXT_SWITCH]
428
429         b       .Lcs_context_switched
430
431         /*
432          * We're switching to a different process in the same L1.
433          * In this situation, we only need to flush the TLB for the
434          * vector_page mapping, and even then only if r7 is non-NULL.
435          */
436 .Lcs_same_l1:
437         cmp     r7, #0
438         movne   r0, #0                  /* We *know* vector_page's VA is 0x0 */
439         movne   lr, pc
440         ldrne   pc, [r10, #CF_TLB_FLUSHID_SE]
441         /*
442          * We can do that, since
443          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
444          */
445
446 .Lcs_context_switched:
447
448         /* Release the old thread */
449         str     r6, [r4, #TD_LOCK]
450 #if defined(SCHED_ULE) && defined(SMP)
451         ldr     r6, .Lblocked_lock
452         GET_CURTHREAD_PTR(r3)
453 1:
454         ldr     r4, [r3, #TD_LOCK]
455         cmp     r4, r6
456         beq     1b
457 #endif
458         
459         /* XXXSCW: Safe to re-enable FIQs here */
460
461         /* rem: r9 = new PCB */
462
463         mrs     r3, cpsr
464         /*
465          * We can do that, since
466          * PSR_SVC32_MODE|PSR_UND32_MODE == MSR_UND32_MODE
467          */
468         orr     r2, r3, #(PSR_UND32_MODE)
469         msr     cpsr_c, r2
470
471         ldr     sp, [r9, #(PCB_UND_SP)]
472
473         msr     cpsr_c, r3              /* Restore the old mode */
474         /* Restore all the save registers */
475 #ifndef _ARM_ARCH_5E
476         add     r7, r9, #PCB_R8
477         ldmia   r7, {r8-r13}
478         sub     r7, r7, #PCB_R8         /* restore PCB pointer */
479 #else
480         mov     r7, r9
481         ldr     r8, [r7, #(PCB_R8)]
482         ldr     r9, [r7, #(PCB_R9)]
483         ldr     r10, [r7, #(PCB_R10)]
484         ldr     r11, [r7, #(PCB_R11)]
485         ldr     r12, [r7, #(PCB_R12)]
486         ldr     r13, [r7, #(PCB_SP)]
487 #endif
488
489         /* rem: r5 = new lwp's proc */
490         /* rem: r6 = lock */
491         /* rem: r7 = new PCB */
492
493 .Lswitch_return:
494
495         /*
496          * Pull the registers that got pushed when either savectx() or
497          * cpu_switch() was called and return.
498          */
499         add     sp, sp, #4;
500         ldmfd   sp!, {r4-r7, pc}
501 #ifdef DIAGNOSTIC
502 .Lswitch_bogons:
503         adr     r0, .Lswitch_panic_str
504         bl      _C_LABEL(panic)
505 1:      nop
506         b       1b
507
508 .Lswitch_panic_str:
509         .asciz  "cpu_switch: sched_qs empty with non-zero sched_whichqs!\n"
510 #endif
511 END(cpu_switch)
512
513 ENTRY(savectx)
514         stmfd   sp!, {r4-r7, lr}
515         sub     sp, sp, #4
516         /*
517          * r0 = pcb
518          */
519         /* Store all the registers in the process's pcb */
520         add     r2, r0, #(PCB_R8)
521         stmia   r2, {r8-r13}
522 #ifdef VFP
523         /*
524          * vfp_store will clear pcpu->pc_vfpcthread, save 
525          * registers and state, and modify the control as needed.
526          * a future exception will bounce the backup settings in the fp unit.
527          */
528         GET_PCPU(r7, r4)
529         ldr     r4, [r7, #(PC_VFPCTHREAD)]      /* vfp thread */
530         ldr     r2, [r7, #(PC_CURTHREAD)]       /* current thread */
531         cmp     r4, r2
532         bne     1f
533 #ifdef SMP
534         ldr     r2, [r7, #(PC_CPU)]     /* last used on this cpu? */
535         ldr     r3, [r0, #(PCB_VFPCPU)]
536         cmp     r2, r3
537         bne     1f              /* no. these values are stale */
538 #endif
539         add     r0, r0, #(PCB_VFPSTATE)
540         bl      _C_LABEL(vfp_store)
541 1:
542 #endif          /* VFP */
543         add     sp, sp, #4;
544         ldmfd   sp!, {r4-r7, pc}
545 END(savectx)
546
547 ENTRY(fork_trampoline)
548         STOP_UNWINDING  /* Can't unwind beyond the thread enty point */
549         mov     r1, r5
550         mov     r2, sp
551         mov     r0, r4
552         mov     fp, #0
553         bl      _C_LABEL(fork_exit)
554         /* Kill irq"s */
555         mrs     r0, cpsr
556         orr     r0, r0, #(I32_bit|F32_bit)
557         msr     cpsr_c, r0
558         DO_AST
559         PULLFRAME
560
561         movs    pc, lr                  /* Exit */
562
563 AST_LOCALS
564 END(fork_trampoline)
565