]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/swtch-v6.S
libfdt: Update to 1.4.6, switch to using libfdt for overlay support
[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         mrc     CP15_TPIDRURW(r4)
295         str     r4, [r3, #(PCB_TPIDRURW - PCB_R4)]
296
297 #ifdef INVARIANTS
298         cmp     r1, #0                  /* new thread? */
299         beq     badsw3                  /* no, panic */
300 #endif
301         /*
302          * Save arguments. Note that we can now use r0-r14 until
303          * it is time to restore them for the new thread. However,
304          * some registers are not safe over function call.
305          */
306         mov     r9, r2                  /* r9 = lock */
307         mov     r10, r0                 /* r10 = oldtd */
308         mov     r11, r1                 /* r11 = newtd */
309
310         GET_PCPU(r8, r3)                /* r8 = current PCPU */
311         ldr     r7, [r11, #(TD_PCB)]    /* r7 = newtd->td_pcb */
312
313
314
315 #ifdef VFP
316         ldr     r3, [r10, #(TD_PCB)]
317         fmrx    r0, fpexc               /* If the VFP is enabled */
318         tst     r0, #(VFPEXC_EN)        /* the current thread has */
319         movne   r1, #1                  /* used it, so go save */
320         addne   r0, r3, #(PCB_VFPSTATE) /* the state into the PCB */
321         blne    _C_LABEL(vfp_store)     /* and disable the VFP. */
322 #endif
323
324         /*
325          * MMU switch. If we're switching to a thread with the same
326          * address space as the outgoing one, we can skip the MMU switch.
327          */
328         mrc     CP15_TTBR0(r1)          /* r1 = old TTB */
329         ldr     r0, [r7, #(PCB_PAGEDIR)] /* r0 = new TTB */
330         cmp     r0, r1                  /* Switching to the TTB? */
331         beq     sw0                     /* same TTB, skip */
332
333 #ifdef INVARIANTS
334         cmp     r0, #0                  /* new thread? */
335         beq     badsw4                  /* no, panic */
336 #endif
337
338         bl      cpu_context_switch      /* new TTB as argument */
339
340         /*
341          * Registers at this point
342          *   r7  = new PCB
343          *   r8  = current pcpu
344          *   r9  = lock
345          *   r10 = oldtd
346          *   r11 = newtd
347          */
348
349         /*
350          * Set new PMAP as current one.
351          * Update active list on PMAPs.
352          */
353         ldr     r6, [r11, #TD_PROC]     /* newtd->proc */
354         ldr     r6, [r6, #P_VMSPACE]    /* newtd->proc->vmspace */
355         add     r6, #VM_PMAP            /* newtd->proc->vmspace->pmap */
356
357         ldr     r5, [r8, #PC_CURPMAP]   /* get old curpmap */
358         str     r6, [r8, #PC_CURPMAP]   /* and save new one */
359
360         mov     r0, #PM_ACTIVE
361         add     r5, r0                  /* r5 = old pm_active */
362         add     r6, r0                  /* r6 = new pm_active */
363
364         /* Compute position and mask. */
365         ldr     r4, [r8, #PC_CPUID]
366 #if _NCPUWORDS > 1
367         lsr     r0, r4, #3
368         bic     r0, #3
369         add     r5, r0                  /* r5 = position in old pm_active */
370         add     r6, r0                  /* r6 = position in new pm_active */
371         mov     r2, #1
372         and     r0, r4, #31
373         lsl     r2, r0                  /* r2 = mask */
374 #else
375         mov     r2, #1
376         lsl     r2, r4                  /* r2 = mask */
377 #endif
378         /* Clear cpu from old active list. */
379 #ifdef SMP
380 1:      ldrex   r0, [r5]
381         bic     r0, r2
382         strex   r1, r0, [r5]
383         teq     r1, #0
384         bne     1b
385 #else
386         ldr     r0, [r5]
387         bic     r0, r2
388         str     r0, [r5]
389 #endif
390         /* Set cpu to new active list. */
391 #ifdef SMP
392 1:      ldrex   r0, [r6]
393         orr     r0, r2
394         strex   r1, r0, [r6]
395         teq     r1, #0
396         bne     1b
397 #else
398         ldr     r0, [r6]
399         orr     r0, r2
400         str     r0, [r6]
401 #endif
402
403 sw0:
404         /*
405          * Registers at this point
406          *   r7  = new PCB
407          *   r8  = current pcpu
408          *   r9  = lock
409          *   r10 = oldtd
410          *   r11 = newtd
411          */
412
413         /* Change the old thread lock. */
414         add     r5, r10, #TD_LOCK
415         DMB
416 1:      ldrex   r0, [r5]
417         strex   r1, r9, [r5]
418         teq     r1, #0
419         bne     1b
420         DMB
421
422 sw1:
423         clrex
424         /*
425          * Registers at this point
426          *   r7  = new PCB
427          *   r8  = current pcpu
428          *   r11 = newtd
429          */
430
431 #if defined(SMP) && defined(SCHED_ULE)
432         /*
433          * 386 and amd64 do the blocked lock test only for SMP and SCHED_ULE
434          * QQQ: What does it mean in reality and why is it done?
435          */
436         ldr     r6, =blocked_lock
437 1:
438         ldr     r3, [r11, #TD_LOCK]     /* atomic write regular read */
439         cmp     r3, r6
440         beq     1b
441 #endif
442
443         /* We have a new curthread now so make a note it */
444         str     r11, [r8, #PC_CURTHREAD]
445         mcr     CP15_TPIDRPRW(r11)
446
447         /* store pcb in per cpu structure */
448         str     r7, [r8, #PC_CURPCB]
449
450         /*
451          * Restore all saved registers and return. Note that some saved
452          * registers can be changed when either cpu_fork(), cpu_copy_thread(),
453          * cpu_fork_kthread_handler(), or makectx() was called.
454          *
455          * The value of TPIDRURW is also written into TPIDRURO, as
456          * userspace still uses TPIDRURO, modifying it through
457          * sysarch(ARM_SET_TP, addr).
458          */
459         ldr     r3, [r7, #PCB_TPIDRURW]
460         mcr     CP15_TPIDRURW(r3)       /* write tls thread reg 2 */
461         mcr     CP15_TPIDRURO(r3)       /* write tls thread reg 3 */
462         add     r3, r7, #PCB_R4
463         ldmia   r3, {r4-r12, sp, pc}
464
465 #ifdef INVARIANTS
466 badsw1:
467         ldr     r0, =sw1_panic_str
468         bl      _C_LABEL(panic)
469 1:      nop
470         b       1b
471
472 badsw2:
473         ldr     r0, =sw2_panic_str
474         bl      _C_LABEL(panic)
475 1:      nop
476         b       1b
477
478 badsw3:
479         ldr     r0, =sw3_panic_str
480         bl      _C_LABEL(panic)
481 1:      nop
482         b       1b
483
484 badsw4:
485         ldr     r0, =sw4_panic_str
486         bl      _C_LABEL(panic)
487 1:      nop
488         b       1b
489
490 sw1_panic_str:
491         .asciz  "cpu_throw: no newthread supplied.\n"
492 sw2_panic_str:
493         .asciz  "cpu_switch: no curthread supplied.\n"
494 sw3_panic_str:
495         .asciz  "cpu_switch: no newthread supplied.\n"
496 sw4_panic_str:
497         .asciz  "cpu_switch: new pagedir is NULL.\n"
498 #endif
499 END(cpu_switch)