]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/swtch-v6.S
Include machine/acle-compat.h in cdefs.h on arm if the compiler doesn't
[FreeBSD/FreeBSD.git] / sys / arm / arm / swtch-v6.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 #include <machine/sysreg.h>
88 #include <machine/vfp.h>
89
90 __FBSDID("$FreeBSD$");
91
92 #if defined(SMP)
93 #define GET_PCPU(tmp, tmp2) \
94         mrc     CP15_MPIDR(tmp);        \
95         and     tmp, tmp, #0xf;         \
96         ldr     tmp2, .Lcurpcpu+4;      \
97         mul     tmp, tmp, tmp2;         \
98         ldr     tmp2, .Lcurpcpu;        \
99         add     tmp, tmp, tmp2;
100 #else
101
102 #define GET_PCPU(tmp, tmp2) \
103         ldr     tmp, .Lcurpcpu
104 #endif
105
106 #ifdef VFP
107         .fpu vfp        /* allow VFP instructions */
108 #endif
109
110 .Lcurpcpu:
111         .word   _C_LABEL(__pcpu)
112         .word   PCPU_SIZE
113 .Lblocked_lock:
114         .word   _C_LABEL(blocked_lock)
115
116 ENTRY(cpu_context_switch)
117         DSB
118         /*
119         * We can directly switch between translation tables only when the
120         * size of the mapping for any given virtual address is the same
121         * in the old and new translation tables.
122         * Thus, we must switch to kernel pmap translation table as
123         * intermediate mapping because all sizes of these mappings are same
124         * (or unmapped). The same is true for switch from kernel pmap
125         * translation table to new pmap one.
126         */
127         mov     r2, #(CPU_ASID_KERNEL)
128         ldr     r1, =(_C_LABEL(pmap_kern_ttb))
129         ldr     r1, [r1]
130         mcr     CP15_TTBR0(r1)          /* switch to kernel TTB */
131         ISB
132         mcr     CP15_TLBIASID(r2)       /* flush not global TLBs */
133         DSB
134         mcr     CP15_TTBR0(r0)          /* switch to new TTB */
135         ISB
136         /*
137         * We must flush not global TLBs again because PT2MAP mapping
138         * is different.
139         */
140         mcr     CP15_TLBIASID(r2)       /* flush not global TLBs */
141         /*
142         * Flush entire Branch Target Cache because of the branch predictor
143         * is not architecturally invisible. See ARM Architecture Reference
144         * Manual ARMv7-A and ARMv7-R edition, page B2-1264(65), Branch
145         * predictors and Requirements for branch predictor maintenance
146         * operations sections.
147         */
148         mcr     CP15_BPIALL             /* flush entire Branch Target Cache */
149         DSB
150         mov     pc, lr
151 END(cpu_context_switch)
152
153 /*
154  * cpu_throw(oldtd, newtd)
155  *
156  * Remove current thread state, then select the next thread to run
157  * and load its state.
158  * r0 = oldtd
159  * r1 = newtd
160  */
161 ENTRY(cpu_throw)
162         mov     r10, r0                 /* r10 = oldtd */
163         mov     r11, r1                 /* r11 = newtd */
164
165 #ifdef VFP                              /* This thread is dying, disable */
166         bl      _C_LABEL(vfp_discard)   /* VFP without preserving state. */
167 #endif
168         GET_PCPU(r8, r9)                /* r8 = current pcpu */
169         ldr     r4, [r8, #PC_CPUID]     /* r4 = current cpu id */
170
171         cmp     r10, #0                 /* old thread? */
172         beq     2f                      /* no, skip */
173
174         /* Remove this CPU from the active list. */
175         ldr     r5, [r8, #PC_CURPMAP]
176         mov     r0, #(PM_ACTIVE)
177         add     r5, r0                  /* r5 = old pm_active */
178
179         /* Compute position and mask. */
180 #if _NCPUWORDS > 1
181         lsr     r0, r4, #3
182         bic     r0, #3
183         add     r5, r0                  /* r5 = position in old pm_active */
184         mov     r2, #1
185         and     r0, r4, #31
186         lsl     r2, r0                  /* r2 = mask */
187 #else
188         mov     r2, #1
189         lsl     r2, r4                  /* r2 = mask */
190 #endif
191         /* Clear cpu from old active list. */
192 #ifdef SMP
193 1:      ldrex   r0, [r5]
194         bic     r0, r2
195         strex   r1, r0, [r5]
196         teq     r1, #0
197         bne     1b
198 #else
199         ldr     r0, [r5]
200         bic     r0, r2
201         str     r0, [r5]
202 #endif
203
204 2:
205 #ifdef INVARIANTS
206         cmp     r11, #0                 /* new thread? */
207         beq     badsw1                  /* no, panic */
208 #endif
209         ldr     r7, [r11, #(TD_PCB)]    /* r7 = new PCB */
210
211         /*
212          * Registers at this point
213          *   r4  = current cpu id
214          *   r7  = new PCB
215          *   r8  = current pcpu
216          *   r11 = newtd
217          */
218
219         /* MMU switch to new thread. */
220         ldr     r0, [r7, #(PCB_PAGEDIR)]
221 #ifdef INVARIANTS
222         cmp     r0, #0                  /* new thread? */
223         beq     badsw4                  /* no, panic */
224 #endif
225         bl      _C_LABEL(cpu_context_switch)
226
227         /*
228          * Set new PMAP as current one.
229          * Insert cpu to new active list.
230          */
231
232         ldr     r6, [r11, #(TD_PROC)]   /* newtd->proc */
233         ldr     r6, [r6, #(P_VMSPACE)]  /* newtd->proc->vmspace */
234         add     r6, #VM_PMAP            /* newtd->proc->vmspace->pmap */
235         str     r6, [r8, #PC_CURPMAP]   /* store to curpmap */
236
237         mov     r0, #PM_ACTIVE
238         add     r6, r0                  /* r6 = new pm_active */
239
240         /* compute position and mask */
241 #if _NCPUWORDS > 1
242         lsr     r0, r4, #3
243         bic     r0, #3
244         add     r6, r0                  /* r6 = position in new pm_active */
245         mov     r2, #1
246         and     r0, r4, #31
247         lsl     r2, r0                  /* r2 = mask */
248 #else
249         mov     r2, #1
250         lsl     r2, r4                  /* r2 = mask */
251 #endif
252         /* Set cpu to new active list. */
253 #ifdef SMP
254 1:      ldrex   r0, [r6]
255         orr     r0, r2
256         strex   r1, r0, [r6]
257         teq     r1, #0
258         bne     1b
259 #else
260         ldr     r0, [r6]
261         orr     r0, r2
262         str     r0, [r6]
263 #endif
264         /*
265          * Registers at this point.
266          *   r7  = new PCB
267          *   r8  = current pcpu
268          *   r11 = newtd
269          * They must match the ones in sw1 position !!!
270          */
271         DMB
272         b       sw1     /* share new thread init with cpu_switch() */
273 END(cpu_throw)
274
275 /*
276  * cpu_switch(oldtd, newtd, lock)
277  *
278  * Save the current thread state, then select the next thread to run
279  * and load its state.
280  * r0 = oldtd
281  * r1 = newtd
282  * r2 = lock (new lock for old thread)
283  */
284 ENTRY(cpu_switch)
285         /* Interrupts are disabled. */
286 #ifdef INVARIANTS
287         cmp     r0, #0                  /* old thread? */
288         beq     badsw2                  /* no, panic */
289 #endif
290         /* Save all the registers in the old thread's pcb. */
291         ldr     r3, [r0, #(TD_PCB)]
292         add     r3, #(PCB_R4)
293         stmia   r3, {r4-r12, sp, lr, pc}
294
295 #ifdef INVARIANTS
296         cmp     r1, #0                  /* new thread? */
297         beq     badsw3                  /* no, panic */
298 #endif
299         /*
300          * Save arguments. Note that we can now use r0-r14 until
301          * it is time to restore them for the new thread. However,
302          * some registers are not safe over function call.
303          */
304         mov     r9, r2                  /* r9 = lock */
305         mov     r10, r0                 /* r10 = oldtd */
306         mov     r11, r1                 /* r11 = newtd */
307
308         GET_PCPU(r8, r3)                /* r8 = current PCPU */
309         ldr     r7, [r11, #(TD_PCB)]    /* r7 = newtd->td_pcb */
310
311
312
313 #ifdef VFP
314         ldr     r3, [r10, #(TD_PCB)]
315         fmrx    r0, fpexc               /* If the VFP is enabled */
316         tst     r0, #(VFPEXC_EN)        /* the current thread has */
317         movne   r1, #1                  /* used it, so go save */
318         addne   r0, r3, #(PCB_VFPSTATE) /* the state into the PCB */
319         blne    _C_LABEL(vfp_store)     /* and disable the VFP. */
320 #endif
321
322         /*
323          * MMU switch. If we're switching to a thread with the same
324          * address space as the outgoing one, we can skip the MMU switch.
325          */
326         mrc     CP15_TTBR0(r1)          /* r1 = old TTB */
327         ldr     r0, [r7, #(PCB_PAGEDIR)] /* r0 = new TTB */
328         cmp     r0, r1                  /* Switching to the TTB? */
329         beq     sw0                     /* same TTB, skip */
330
331 #ifdef INVARIANTS
332         cmp     r0, #0                  /* new thread? */
333         beq     badsw4                  /* no, panic */
334 #endif
335
336         bl      cpu_context_switch      /* new TTB as argument */
337
338         /*
339          * Registers at this point
340          *   r7  = new PCB
341          *   r8  = current pcpu
342          *   r9  = lock
343          *   r10 = oldtd
344          *   r11 = newtd
345          */
346
347         /*
348          * Set new PMAP as current one.
349          * Update active list on PMAPs.
350          */
351         ldr     r6, [r11, #TD_PROC]     /* newtd->proc */
352         ldr     r6, [r6, #P_VMSPACE]    /* newtd->proc->vmspace */
353         add     r6, #VM_PMAP            /* newtd->proc->vmspace->pmap */
354
355         ldr     r5, [r8, #PC_CURPMAP]   /* get old curpmap */
356         str     r6, [r8, #PC_CURPMAP]   /* and save new one */
357
358         mov     r0, #PM_ACTIVE
359         add     r5, r0                  /* r5 = old pm_active */
360         add     r6, r0                  /* r6 = new pm_active */
361
362         /* Compute position and mask. */
363         ldr     r4, [r8, #PC_CPUID]
364 #if _NCPUWORDS > 1
365         lsr     r0, r4, #3
366         bic     r0, #3
367         add     r5, r0                  /* r5 = position in old pm_active */
368         add     r6, r0                  /* r6 = position in new pm_active */
369         mov     r2, #1
370         and     r0, r4, #31
371         lsl     r2, r0                  /* r2 = mask */
372 #else
373         mov     r2, #1
374         lsl     r2, r4                  /* r2 = mask */
375 #endif
376         /* Clear cpu from old active list. */
377 #ifdef SMP
378 1:      ldrex   r0, [r5]
379         bic     r0, r2
380         strex   r1, r0, [r5]
381         teq     r1, #0
382         bne     1b
383 #else
384         ldr     r0, [r5]
385         bic     r0, r2
386         str     r0, [r5]
387 #endif
388         /* Set cpu to new active list. */
389 #ifdef SMP
390 1:      ldrex   r0, [r6]
391         orr     r0, r2
392         strex   r1, r0, [r6]
393         teq     r1, #0
394         bne     1b
395 #else
396         ldr     r0, [r6]
397         orr     r0, r2
398         str     r0, [r6]
399 #endif
400
401 sw0:
402         /*
403          * Registers at this point
404          *   r7  = new PCB
405          *   r8  = current pcpu
406          *   r9  = lock
407          *   r10 = oldtd
408          *   r11 = newtd
409          */
410
411         /* Change the old thread lock. */
412         add     r5, r10, #TD_LOCK
413         DMB
414 1:      ldrex   r0, [r5]
415         strex   r1, r9, [r5]
416         teq     r1, #0
417         bne     1b
418         DMB
419
420 sw1:
421         clrex
422         /*
423          * Registers at this point
424          *   r7  = new PCB
425          *   r8  = current pcpu
426          *   r11 = newtd
427          */
428
429 #if defined(SMP) && defined(SCHED_ULE)
430         /*
431          * 386 and amd64 do the blocked lock test only for SMP and SCHED_ULE
432          * QQQ: What does it mean in reality and why is it done?
433          */
434         ldr     r6, =blocked_lock
435 1:
436         ldr     r3, [r11, #TD_LOCK]     /* atomic write regular read */
437         cmp     r3, r6
438         beq     1b
439 #endif
440         /* Set the new tls */
441         ldr     r0, [r11, #(TD_MD + MD_TP)]
442         mcr     CP15_TPIDRURO(r0)       /* write tls thread reg 2 */
443
444         /* We have a new curthread now so make a note it */
445         str     r11, [r8, #PC_CURTHREAD]
446         mcr     CP15_TPIDRPRW(r11)
447
448         /* store pcb in per cpu structure */
449         str     r7, [r8, #PC_CURPCB]
450
451         /*
452          * Restore all saved registers and return. Note that some saved
453          * registers can be changed when either cpu_fork(), cpu_set_upcall(),
454          * cpu_set_fork_handler(), or makectx() was called.
455          */
456         add     r3, r7, #PCB_R4
457         ldmia   r3, {r4-r12, sp, pc}
458
459 #ifdef INVARIANTS
460 badsw1:
461         ldr     r0, =sw1_panic_str
462         bl      _C_LABEL(panic)
463 1:      nop
464         b       1b
465
466 badsw2:
467         ldr     r0, =sw2_panic_str
468         bl      _C_LABEL(panic)
469 1:      nop
470         b       1b
471
472 badsw3:
473         ldr     r0, =sw3_panic_str
474         bl      _C_LABEL(panic)
475 1:      nop
476         b       1b
477
478 badsw4:
479         ldr     r0, =sw4_panic_str
480         bl      _C_LABEL(panic)
481 1:      nop
482         b       1b
483
484 sw1_panic_str:
485         .asciz  "cpu_throw: no newthread supplied.\n"
486 sw2_panic_str:
487         .asciz  "cpu_switch: no curthread supplied.\n"
488 sw3_panic_str:
489         .asciz  "cpu_switch: no newthread supplied.\n"
490 sw4_panic_str:
491         .asciz  "cpu_switch: new pagedir is NULL.\n"
492 #endif
493 END(cpu_switch)