Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.33
      1 /*	$NetBSD: cpuswitch.S,v 1.33 2003/05/21 18:04:42 thorpej 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 #include "opt_armfpe.h"
     81 #include "opt_arm32_pmap.h"
     82 #include "opt_multiprocessor.h"
     83 
     84 #include "assym.h"
     85 #include <machine/param.h>
     86 #include <machine/cpu.h>
     87 #include <machine/frame.h>
     88 #include <machine/asm.h>
     89 
     90 #undef IRQdisable
     91 #undef IRQenable
     92 
     93 /*
     94  * New experimental definitions of IRQdisable and IRQenable
     95  * These keep FIQ's enabled since FIQ's are special.
     96  */
     97 
     98 #define IRQdisable \
     99 	mrs	r14, cpsr ; \
    100 	orr	r14, r14, #(I32_bit) ; \
    101 	msr	cpsr_c, r14 ; \
    102 
    103 #define IRQenable \
    104 	mrs	r14, cpsr ; \
    105 	bic	r14, r14, #(I32_bit) ; \
    106 	msr	cpsr_c, r14 ; \
    107 
    108 /*
    109  * These are used for switching the translation table/DACR.
    110  * Since the vector page can be invalid for a short time, we must
    111  * disable both regular IRQs *and* FIQs.
    112  *
    113  * XXX: This is not necessary if the vector table is relocated.
    114  */
    115 #define IRQdisableALL \
    116 	mrs	r14, cpsr ; \
    117 	orr	r14, r14, #(I32_bit | F32_bit) ; \
    118 	msr	cpsr_c, r14
    119 
    120 #define IRQenableALL \
    121 	mrs	r14, cpsr ; \
    122 	bic	r14, r14, #(I32_bit | F32_bit) ; \
    123 	msr	cpsr_c, r14
    124 
    125 	.text
    126 
    127 .Lwhichqs:
    128 	.word	_C_LABEL(sched_whichqs)
    129 
    130 .Lqs:
    131 	.word	_C_LABEL(sched_qs)
    132 
    133 /*
    134  * cpuswitch()
    135  *
    136  * preforms a process context switch.
    137  * This function has several entry points
    138  */
    139 
    140 #ifdef MULTIPROCESSOR
    141 .Lcpu_info_store:
    142 	.word	_C_LABEL(cpu_info_store)
    143 .Lcurlwp:
    144 	/* FIXME: This is bogus in the general case. */
    145 	.word	_C_LABEL(cpu_info_store) + CI_CURLWP
    146 
    147 .Lcurpcb:
    148 	.word	_C_LABEL(cpu_info_store) + CI_CURPCB
    149 #else
    150 .Lcurlwp:
    151 	.word	_C_LABEL(curlwp)
    152 
    153 .Lcurpcb:
    154 	.word	_C_LABEL(curpcb)
    155 #endif
    156 
    157 .Lwant_resched:
    158 	.word	_C_LABEL(want_resched)
    159 
    160 .Lcpufuncs:
    161 	.word	_C_LABEL(cpufuncs)
    162 
    163 #ifndef MULTIPROCESSOR
    164 	.data
    165 	.global	_C_LABEL(curpcb)
    166 _C_LABEL(curpcb):
    167 	.word	0x00000000
    168 	.text
    169 #endif
    170 
    171 .Lblock_userspace_access:
    172 	.word	_C_LABEL(block_userspace_access)
    173 
    174 .Lcpu_do_powersave:
    175 	.word	_C_LABEL(cpu_do_powersave)
    176 
    177 .Lpmap_kernel_cstate:
    178 	.word	(kernel_pmap_store + PMAP_CSTATE)
    179 
    180 .Llast_cache_state_ptr:
    181 	.word	_C_LABEL(pmap_cache_state)
    182 
    183 /*
    184  * Idle loop, exercised while waiting for a process to wake up.
    185  *
    186  * NOTE: When we jump back to .Lswitch_search, we must have a
    187  * pointer to whichqs in r7, which is what it is when we arrive
    188  * here.
    189  */
    190 /* LINTSTUB: Ignore */
    191 ASENTRY_NP(idle)
    192 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    193 	bl	_C_LABEL(sched_unlock_idle)
    194 #endif
    195 	ldr	r3, .Lcpu_do_powersave
    196 
    197 	/* Enable interrupts */
    198 	IRQenable
    199 
    200 	/* If we don't want to sleep, use a simpler loop. */
    201 	ldr	r3, [r3]		/* r3 = cpu_do_powersave */
    202 	teq	r3, #0
    203 	bne	2f
    204 
    205 	/* Non-powersave idle. */
    206 1:	/* should maybe do uvm pageidlezero stuff here */
    207 	ldr	r3, [r7]		/* r3 = whichqs */
    208 	teq	r3, #0x00000000
    209 	bne	.Lswitch_search
    210 	b	1b
    211 
    212 2:	/* Powersave idle. */
    213 	ldr	r4, .Lcpufuncs
    214 3:	ldr	r3, [r7]		/* r3 = whichqs */
    215 	teq	r3, #0x00000000
    216 	bne	.Lswitch_search
    217 
    218 	/* if saving power, don't want to pageidlezero */
    219 	mov	r0, #0
    220 	adr	lr, 3b
    221 	ldr	pc, [r4, #(CF_SLEEP)]
    222 	/* loops back around */
    223 
    224 
    225 /*
    226  * Find a new lwp to run, save the current context and
    227  * load the new context
    228  *
    229  * Arguments:
    230  *	r0	'struct lwp *' of the current LWP
    231  */
    232 
    233 ENTRY(cpu_switch)
    234 /*
    235  * Local register usage. Some of these registers are out of date.
    236  * r1 = oldlwp
    237  * r2 = spl level
    238  * r3 = whichqs
    239  * r4 = queue
    240  * r5 = &qs[queue]
    241  * r6 = newlwp
    242  * r7 = scratch
    243  */
    244 	stmfd	sp!, {r4-r7, lr}
    245 
    246 	/*
    247 	 * Indicate that there is no longer a valid process (curlwp = 0).
    248 	 * Zero the current PCB pointer while we're at it.
    249 	 */
    250 	ldr	r7, .Lcurlwp
    251 	ldr	r6, .Lcurpcb
    252 	mov	r2, #0x00000000
    253 	str	r2, [r7]		/* curproc = NULL */
    254 	str	r2, [r6]		/* curpcb = NULL */
    255 
    256 	/* stash the old proc while we call functions */
    257 	mov	r5, r0
    258 
    259 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    260 	/* release the sched_lock before handling interrupts */
    261 	bl	_C_LABEL(sched_unlock_idle)
    262 #endif
    263 
    264 	/* Lower the spl level to spl0 and get the current spl level. */
    265 #ifdef __NEWINTR
    266 	mov	r0, #(IPL_NONE)
    267 	bl	_C_LABEL(_spllower)
    268 #else /* ! __NEWINTR */
    269 #ifdef spl0
    270 	mov	r0, #(_SPL_0)
    271 	bl	_C_LABEL(splx)
    272 #else
    273 	bl	_C_LABEL(spl0)
    274 #endif /* spl0 */
    275 #endif /* __NEWINTR */
    276 
    277 	/* Push the old spl level onto the stack */
    278 	str	r0, [sp, #-0x0004]!
    279 
    280 	/* First phase : find a new lwp */
    281 
    282 	ldr	r7, .Lwhichqs
    283 
    284 	/* rem: r5 = old lwp */
    285 	/* rem: r7 = &whichqs */
    286 
    287 .Lswitch_search:
    288 	IRQdisable
    289 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    290 	bl	_C_LABEL(sched_lock_idle)
    291 #endif
    292 
    293 	/* Do we have any active queues  */
    294 	ldr	r3, [r7]
    295 
    296 	/* If not we must idle until we do. */
    297 	teq	r3, #0x00000000
    298 	beq	_ASM_LABEL(idle)
    299 
    300 	/* put old proc back in r1 */
    301 	mov	r1, r5
    302 
    303 	/* rem: r1 = old lwp */
    304 	/* rem: r3 = whichqs */
    305 	/* rem: interrupts are disabled */
    306 
    307 	/*
    308 	 * We have found an active queue. Currently we do not know which queue
    309 	 * is active just that one of them is.
    310 	 */
    311 	/* this is the ffs algorithm devised by d.seal and posted to
    312 	 * comp.sys.arm on 16 Feb 1994.
    313 	 */
    314  	rsb	r5, r3, #0
    315  	ands	r0, r3, r5
    316 
    317 	adr	r5, .Lcpu_switch_ffs_table
    318 
    319 				    /* X = R0 */
    320 	orr	r4, r0, r0, lsl #4  /* r4 = X * 0x11 */
    321 	orr	r4, r4, r4, lsl #6  /* r4 = X * 0x451 */
    322 	rsb	r4, r4, r4, lsl #16 /* r4 = X * 0x0450fbaf */
    323 
    324 	/* used further down, saves SA stall */
    325 	ldr	r6, .Lqs
    326 
    327 	/* now lookup in table indexed on top 6 bits of a4 */
    328 	ldrb	r4, [ r5, r4, lsr #26 ]
    329 
    330 	/* rem: r0 = bit mask of chosen queue (1 << r4) */
    331 	/* rem: r1 = old lwp */
    332 	/* rem: r3 = whichqs */
    333 	/* rem: r4 = queue number */
    334 	/* rem: interrupts are disabled */
    335 
    336 	/* Get the address of the queue (&qs[queue]) */
    337 	add	r5, r6, r4, lsl #3
    338 
    339 	/*
    340 	 * Get the lwp from the queue and place the next process in
    341 	 * the queue at the head. This basically unlinks the lwp at
    342 	 * the head of the queue.
    343 	 */
    344 	ldr	r6, [r5, #(L_FORW)]
    345 
    346 	/* rem: r6 = new lwp */
    347 	ldr	r7, [r6, #(L_FORW)]
    348 	str	r7, [r5, #(L_FORW)]
    349 
    350 	/*
    351 	 * Test to see if the queue is now empty. If the head of the queue
    352 	 * points to the queue itself then there are no more lwps in
    353 	 * the queue. We can therefore clear the queue not empty flag held
    354 	 * in r3.
    355 	 */
    356 
    357 	teq	r5, r7
    358 	biceq	r3, r3, r0
    359 
    360 	/* rem: r0 = bit mask of chosen queue (1 << r4) - NOT NEEDED AN MORE */
    361 
    362 	/* Fix the back pointer for the lwp now at the head of the queue. */
    363 	ldr	r0, [r6, #(L_BACK)]
    364 	str	r0, [r7, #(L_BACK)]
    365 
    366 	/* Update the RAM copy of the queue not empty flags word. */
    367 	ldr	r7, .Lwhichqs
    368 	str	r3, [r7]
    369 
    370 	/* rem: r1 = old lwp */
    371 	/* rem: r3 = whichqs - NOT NEEDED ANY MORE */
    372 	/* rem: r4 = queue number - NOT NEEDED ANY MORE */
    373 	/* rem: r6 = new lwp */
    374 	/* rem: interrupts are disabled */
    375 
    376 	/* Clear the want_resched flag */
    377 	ldr	r7, .Lwant_resched
    378 	mov	r0, #0x00000000
    379 	str	r0, [r7]
    380 
    381 	/*
    382 	 * Clear the back pointer of the lwp we have removed from
    383 	 * the head of the queue. The new lwp is isolated now.
    384 	 */
    385 	str	r0, [r6, #(L_BACK)]
    386 
    387 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    388 	/*
    389 	 * unlock the sched_lock, but leave interrupts off, for now.
    390 	 */
    391 	mov	r7, r1
    392 	bl	_C_LABEL(sched_unlock_idle)
    393 	mov	r1, r7
    394 #endif
    395 
    396 .Lswitch_resume:
    397 #ifdef MULTIPROCESSOR
    398 	/* XXX use curcpu() */
    399 	ldr	r0, .Lcpu_info_store
    400 	str	r0, [r6, #(L_CPU)]
    401 #else
    402 	/* l->l_cpu initialized in fork1() for single-processor */
    403 #endif
    404 
    405 	/* Process is now on a processor. */
    406 	mov	r0, #LSONPROC			/* l->l_stat = LSONPROC */
    407 	str	r0, [r6, #(L_STAT)]
    408 
    409 	/* We have a new curlwp now so make a note it */
    410 	ldr	r7, .Lcurlwp
    411 	str	r6, [r7]
    412 
    413 	/* Hook in a new pcb */
    414 	ldr	r7, .Lcurpcb
    415 	ldr	r0, [r6, #(L_ADDR)]
    416 	str	r0, [r7]
    417 
    418 	/* At this point we can allow IRQ's again. */
    419 	IRQenable
    420 
    421 	/* rem: r1 = old lwp */
    422 	/* rem: r4 = return value */
    423 	/* rem: r6 = new process */
    424 	/* rem: interrupts are enabled */
    425 
    426 	/*
    427 	 * If the new process is the same as the process that called
    428 	 * cpu_switch() then we do not need to save and restore any
    429 	 * contexts. This means we can make a quick exit.
    430 	 * The test is simple if curlwp on entry (now in r1) is the
    431 	 * same as the proc removed from the queue we can jump to the exit.
    432 	 */
    433 	teq	r1, r6
    434 	moveq	r4, #0x00000000		/* default to "didn't switch" */
    435 	beq	.Lswitch_return
    436 
    437 	/*
    438 	 * At this point, we are guaranteed to be switching to
    439 	 * a new lwp.
    440 	 */
    441 	mov	r4, #0x00000001
    442 
    443 	/* Remember the old lwp in r0 */
    444 	mov	r0, r1
    445 
    446 	/*
    447 	 * If the old lwp on entry to cpu_switch was zero then the
    448 	 * process that called it was exiting. This means that we do
    449 	 * not need to save the current context. Instead we can jump
    450 	 * straight to restoring the context for the new process.
    451 	 */
    452 	teq	r0, #0x00000000
    453 	beq	.Lswitch_exited
    454 
    455 	/* rem: r0 = old lwp */
    456 	/* rem: r4 = return value */
    457 	/* rem: r6 = new process */
    458 	/* rem: interrupts are enabled */
    459 
    460 	/* Stage two : Save old context */
    461 
    462 	/* Get the user structure for the old lwp. */
    463 	ldr	r1, [r0, #(L_ADDR)]
    464 
    465 	/* Save all the registers in the old lwp's pcb */
    466 	add	r7, r1, #(PCB_R8)
    467 	stmia	r7, {r8-r13}
    468 
    469 	/*
    470 	 * NOTE: We can now use r8-r13 until it is time to restore
    471 	 * them for the new process.
    472 	 */
    473 
    474 	/* Remember the old PCB. */
    475 	mov	r8, r1
    476 
    477 	/* r1 now free! */
    478 
    479 	/* Get the user structure for the new process in r9 */
    480 	ldr	r9, [r6, #(L_ADDR)]
    481 
    482 	/*
    483 	 * This can be optimised... We know we want to go from SVC32
    484 	 * mode to UND32 mode
    485 	 */
    486         mrs	r3, cpsr
    487 	bic	r2, r3, #(PSR_MODE)
    488 	orr	r2, r2, #(PSR_UND32_MODE | I32_bit)
    489         msr	cpsr_c, r2
    490 
    491 	str	sp, [r8, #(PCB_UND_SP)]
    492 
    493         msr	cpsr_c, r3		/* Restore the old mode */
    494 
    495 	/* rem: r0 = old lwp */
    496 	/* rem: r4 = return value */
    497 	/* rem: r6 = new process */
    498 	/* rem: r8 = old PCB */
    499 	/* rem: r9 = new PCB */
    500 	/* rem: interrupts are enabled */
    501 
    502 	/* What else needs to be saved  Only FPA stuff when that is supported */
    503 
    504 	/* Third phase : restore saved context */
    505 
    506 	/* rem: r0 = old lwp */
    507 	/* rem: r4 = return value */
    508 	/* rem: r6 = new lwp */
    509 	/* rem: r8 = old PCB */
    510 	/* rem: r9 = new PCB */
    511 	/* rem: interrupts are enabled */
    512 
    513 	/*
    514 	 * Get the new L1 table pointer into r11.  If we're switching to
    515 	 * an LWP with the same address space as the outgoing one, we can
    516 	 * skip the cache purge and the TTB load.
    517 	 *
    518 	 * To avoid data dep stalls that would happen anyway, we try
    519 	 * and get some useful work done in the mean time.
    520 	 */
    521 	ldr	r10, [r8, #(PCB_PAGEDIR)]	/* r10 = old L1 */
    522 	ldr	r11, [r9, #(PCB_PAGEDIR)]	/* r11 = new L1 */
    523 
    524 	ldr	r0, [r8, #(PCB_DACR)]		/* r0 = old DACR */
    525 	ldr	r1, [r9, #(PCB_DACR)]		/* r1 = new DACR */
    526 	ldr	r8, [r9, #(PCB_CSTATE)]		/* r8 = &new_pmap->pm_cstate */
    527 	ldr	r5, .Llast_cache_state_ptr	/* Previous thread's cstate */
    528 
    529 	teq	r10, r11			/* Same L1? */
    530 	ldr	r5, [r5]
    531 	cmpeq	r0, r1				/* Same DACR? */
    532 	beq	.Lcs_context_switched		/* yes! */
    533 
    534 	ldr	r3, .Lblock_userspace_access
    535 	mov	r12, #0
    536 	cmp	r5, #0				/* No last vm? (switch_exit) */
    537 	beq	.Lcs_cache_purge_skipped	/* No, we can skip cache flsh */
    538 
    539 	mov	r2, #DOMAIN_CLIENT
    540 	cmp	r1, r2, lsl #(PMAP_DOMAIN_KERNEL * 2) /* Sw to kernel thread? */
    541 	beq	.Lcs_cache_purge_skipped	/* Yup. Don't flush cache */
    542 
    543 	cmp	r5, r8				/* Same userland VM space? */
    544 	ldrneb	r12, [r5, #(CS_CACHE_ID)]	/* Last VM space cache state */
    545 
    546 	/*
    547 	 * We're definately switching to a new userland VM space,
    548 	 * and the previous userland VM space has yet to be flushed
    549 	 * from the cache/tlb.
    550 	 *
    551 	 * r12 holds the previous VM space's cs_cache_id state
    552 	 */
    553 	tst	r12, #0xff			/* Test cs_cache_id */
    554 	beq	.Lcs_cache_purge_skipped	/* VM space is not in cache */
    555 
    556 	/*
    557 	 * Definately need to flush the cache.
    558 	 * Mark the old VM space as NOT being resident in the cache.
    559 	 */
    560 	mov	r2, #0x00000000
    561 	strb	r2, [r5, #(CS_CACHE_ID)]
    562 	strb	r2, [r5, #(CS_CACHE_D)]
    563 
    564 	/*
    565 	 * Don't allow user space access between the purge and the switch.
    566 	 */
    567 	mov	r2, #0x00000001
    568 	str	r2, [r3]
    569 
    570 	stmfd	sp!, {r0-r3}
    571 	ldr	r1, .Lcpufuncs
    572 	mov	lr, pc
    573 	ldr	pc, [r1, #CF_IDCACHE_WBINV_ALL]
    574 	ldmfd	sp!, {r0-r3}
    575 
    576 .Lcs_cache_purge_skipped:
    577 	/* rem: r1 = new DACR */
    578 	/* rem: r3 = &block_userspace_access */
    579 	/* rem: r4 = return value */
    580 	/* rem: r5 = &old_pmap->pm_cstate (or NULL) */
    581 	/* rem: r6 = new lwp */
    582 	/* rem: r8 = &new_pmap->pm_cstate */
    583 	/* rem: r9 = new PCB */
    584 	/* rem: r10 = old L1 */
    585 	/* rem: r11 = new L1 */
    586 
    587 	mov	r2, #0x00000000
    588 	ldr	r7, [r9, #(PCB_PL1VEC)]
    589 
    590 	/*
    591 	 * At this point we need to kill IRQ's again.
    592 	 *
    593 	 * XXXSCW: Don't need to block FIQs if vectors have been relocated
    594 	 */
    595 	IRQdisableALL
    596 
    597 	/*
    598 	 * Interrupts are disabled so we can allow user space accesses again
    599 	 * as none will occur until interrupts are re-enabled after the
    600 	 * switch.
    601 	 */
    602 	str	r2, [r3]
    603 
    604 	/*
    605 	 * Ensure the vector table is accessible by fixing up the L1
    606 	 */
    607 	cmp	r7, #0			/* No need to fixup vector table? */
    608 	ldrne	r2, [r7]		/* But if yes, fetch current value */
    609 	ldrne	r0, [r9, #(PCB_L1VEC)]	/* Fetch new vector_page value */
    610 	mcr	p15, 0, r1, c3, c0, 0	/* Update DACR for new context */
    611 	cmpne	r2, r0			/* Stuffing the same value? */
    612 #ifndef PMAP_INCLUDE_PTE_SYNC
    613 	strne	r0, [r7]		/* Nope, update it */
    614 #else
    615 	beq	.Lcs_same_vector
    616 	str	r0, [r7]		/* Otherwise, update it */
    617 
    618 	/*
    619 	 * Need to sync the cache to make sure that last store is
    620 	 * visible to the MMU.
    621 	 */
    622 	ldr	r2, .Lcpufuncs
    623 	mov	r0, r7
    624 	mov	r1, #4
    625 	mov	lr, pc
    626 	ldr	pc, [r2, #CF_DCACHE_WB_RANGE]
    627 
    628 .Lcs_same_vector:
    629 #endif /* PMAP_INCLUDE_PTE_SYNC */
    630 
    631 	cmp	r10, r11		/* Switching to the same L1? */
    632 	ldr	r10, .Lcpufuncs
    633 	beq	.Lcs_same_l1		/* Yup. */
    634 
    635 	/*
    636 	 * Do a full context switch, including full TLB flush.
    637 	 */
    638 	mov	r0, r11
    639 	mov	lr, pc
    640 	ldr	pc, [r10, #CF_CONTEXT_SWITCH]
    641 
    642 	/*
    643 	 * Mark the old VM space as NOT being resident in the TLB
    644 	 */
    645 	mov	r2, #0x00000000
    646 	cmp	r5, #0
    647 	strneh	r2, [r5, #(CS_TLB_ID)]
    648 	b	.Lcs_context_switched
    649 
    650 	/*
    651 	 * We're switching to a different process in the same L1.
    652 	 * In this situation, we only need to flush the TLB for the
    653 	 * vector_page mapping, and even then only if r7 is non-NULL.
    654 	 */
    655 .Lcs_same_l1:
    656 	cmp	r7, #0
    657 	movne	r0, #0			/* We *know* vector_page's VA is 0x0 */
    658 	movne	lr, pc
    659 	ldrne	pc, [r10, #CF_TLB_FLUSHID_SE]
    660 
    661 .Lcs_context_switched:
    662 	/* rem: r8 = &new_pmap->pm_cstate */
    663 
    664 	/* XXXSCW: Safe to re-enable FIQs here */
    665 
    666 	/*
    667 	 * The new VM space is live in the cache and TLB.
    668 	 * Update its cache/tlb state, and if it's not the kernel
    669 	 * pmap, update the 'last cache state' pointer.
    670 	 */
    671 	mov	r2, #-1
    672 	ldr	r5, .Lpmap_kernel_cstate
    673 	ldr	r0, .Llast_cache_state_ptr
    674 	str	r2, [r8, #(CS_ALL)]
    675 	cmp	r5, r8
    676 	strne	r8, [r0]
    677 
    678 	/* rem: r4 = return value */
    679 	/* rem: r6 = new lwp */
    680 	/* rem: r9 = new PCB */
    681 
    682 	/*
    683 	 * This can be optimised... We know we want to go from SVC32
    684 	 * mode to UND32 mode
    685 	 */
    686         mrs	r3, cpsr
    687 	bic	r2, r3, #(PSR_MODE)
    688 	orr	r2, r2, #(PSR_UND32_MODE)
    689         msr	cpsr_c, r2
    690 
    691 	ldr	sp, [r9, #(PCB_UND_SP)]
    692 
    693         msr	cpsr_c, r3		/* Restore the old mode */
    694 
    695 	/* Restore all the save registers */
    696 	add	r7, r9, #PCB_R8
    697 	ldmia	r7, {r8-r13}
    698 
    699 	sub	r7, r7, #PCB_R8		/* restore PCB pointer */
    700 
    701 	ldr	r5, [r6, #(L_PROC)]	/* fetch the proc for below */
    702 
    703 	/* rem: r4 = return value */
    704 	/* rem: r5 = new lwp's proc */
    705 	/* rem: r6 = new lwp */
    706 	/* rem: r7 = new pcb */
    707 
    708 #ifdef ARMFPE
    709 	add	r0, r7, #(USER_SIZE) & 0x00ff
    710 	add	r0, r0, #(USER_SIZE) & 0xff00
    711 	bl	_C_LABEL(arm_fpe_core_changecontext)
    712 #endif
    713 
    714 	/* We can enable interrupts again */
    715 	IRQenableALL
    716 
    717 	/* rem: r4 = return value */
    718 	/* rem: r5 = new lwp's proc */
    719 	/* rem: r6 = new lwp */
    720 	/* rem: r7 = new PCB */
    721 
    722 	/*
    723 	 * Check for restartable atomic sequences (RAS).
    724 	 */
    725 
    726 	ldr	r2, [r5, #(P_NRAS)]
    727 	ldr	r4, [r7, #(PCB_TF)]	/* r4 = trapframe (used below) */
    728 	teq	r2, #0			/* p->p_nras == 0? */
    729 	bne	.Lswitch_do_ras		/* no, check for one */
    730 
    731 .Lswitch_return:
    732 
    733 	/* Get the spl level from the stack and update the current spl level */
    734 	ldr	r0, [sp], #0x0004
    735 	bl	_C_LABEL(splx)
    736 
    737 	/* cpu_switch returns 1 == switched, 0 == didn't switch */
    738 	mov	r0, r4
    739 
    740 	/*
    741 	 * Pull the registers that got pushed when either savectx() or
    742 	 * cpu_switch() was called and return.
    743 	 */
    744 	ldmfd	sp!, {r4-r7, pc}
    745 
    746 .Lswitch_do_ras:
    747 	ldr	r1, [r4, #(TF_PC)]	/* second ras_lookup() arg */
    748 	mov	r0, r5			/* first ras_lookup() arg */
    749 	bl	_C_LABEL(ras_lookup)
    750 	cmn	r0, #1			/* -1 means "not in a RAS" */
    751 	strne	r0, [r4, #(TF_PC)]
    752 	b	.Lswitch_return
    753 
    754 .Lswitch_exited:
    755 	/*
    756 	 * We skip the cache purge because switch_exit() already did it.
    757 	 * Load up registers the way .Lcs_cache_purge_skipped expects.
    758 	 * Userpsace access already blocked by switch_exit().
    759 	 */
    760 	ldr	r9, [r6, #(L_ADDR)]		/* r9 = new PCB */
    761 	ldr	r3, .Lblock_userspace_access
    762 	mrc	p15, 0, r10, c2, c0, 0		/* r10 = old L1 */
    763 	mov	r5, #0				/* No previous cache state */
    764 	ldr	r1, [r9, #(PCB_DACR)]		/* r1 = new DACR */
    765 	ldr	r8, [r9, #(PCB_CSTATE)]		/* r8 = new cache state */
    766 	ldr	r11, [r9, #(PCB_PAGEDIR)]	/* r11 = new L1 */
    767 	b	.Lcs_cache_purge_skipped
    768 
    769 /*
    770  * cpu_switchto(struct lwp *current, struct lwp *next)
    771  * Switch to the specified next LWP
    772  * Arguments:
    773  *
    774  *	r0	'struct lwp *' of the current LWP
    775  *	r1	'struct lwp *' of the LWP to switch to
    776  */
    777 ENTRY(cpu_switchto)
    778 	stmfd	sp!, {r4-r7, lr}
    779 
    780 	/* Lower the spl level to spl0 and get the current spl level. */
    781 	mov	r6, r0		/* save old lwp */
    782 	mov	r5, r1		/* save new lwp */
    783 
    784 #if defined(LOCKDEBUG)
    785 	/* release the sched_lock before handling interrupts */
    786 	bl	_C_LABEL(sched_unlock_idle)
    787 #endif
    788 
    789 #ifdef __NEWINTR
    790 	mov	r0, #(IPL_NONE)
    791 	bl	_C_LABEL(_spllower)
    792 #else /* ! __NEWINTR */
    793 #ifdef spl0
    794 	mov	r0, #(_SPL_0)
    795 	bl	_C_LABEL(splx)
    796 #else
    797 	bl	_C_LABEL(spl0)
    798 #endif /* spl0 */
    799 #endif /* __NEWINTR */
    800 
    801 	/* Push the old spl level onto the stack */
    802 	str	r0, [sp, #-0x0004]!
    803 
    804 	IRQdisable
    805 #if defined(LOCKDEBUG)
    806 	bl	_C_LABEL(sched_lock_idle)
    807 #endif
    808 
    809 	mov	r0, r6		/* restore old lwp */
    810 	mov	r1, r5		/* restore new lwp */
    811 
    812 	/* rem: r0 = old lwp */
    813 	/* rem: r1 = new lwp */
    814 	/* rem: interrupts are disabled */
    815 
    816 	/*
    817 	 * Okay, set up registers the way cpu_switch() wants them,
    818 	 * and jump into the middle of it (where we bring up the
    819 	 * new process).
    820 	 */
    821 	mov	r6, r1			/* r6 = new lwp */
    822 #if defined(LOCKDEBUG)
    823 	mov	r5, r0			/* preserve old lwp */
    824 	bl	_C_LABEL(sched_unlock_idle)
    825 	mov	r1, r5			/* r1 = old lwp */
    826 #else
    827 	mov	r1, r0			/* r1 = old lwp */
    828 #endif
    829 	b	.Lswitch_resume
    830 
    831 /*
    832  * void switch_exit(struct lwp *l, struct lwp *l0, void (*exit)(struct lwp *));
    833  * Switch to lwp0's saved context and deallocate the address space and kernel
    834  * stack for l.  Then jump into cpu_switch(), as if we were in lwp0 all along.
    835  */
    836 
    837 /* LINTSTUB: Func: void switch_exit(struct lwp *l, struct lwp *l0, void (*)(struct lwp *)) */
    838 ENTRY(switch_exit)
    839 	/*
    840 	 * The process is going away, so we can use callee-saved
    841 	 * registers here without having to save them.
    842 	 */
    843 
    844 	mov	r4, r0
    845 	ldr	r0, .Lcurlwp
    846 
    847 	mov	r5, r1
    848 	ldr	r1, .Lblock_userspace_access
    849 
    850 	mov	r6, r2
    851 
    852 	/*
    853 	 * r4 = lwp
    854 	 * r5 = lwp0
    855 	 * r6 = exit func
    856 	 */
    857 
    858 	mov	r2, #0x00000000		/* curlwp = NULL */
    859 	str	r2, [r0]
    860 
    861 	/*
    862 	 * We're about to clear both the cache and the TLB.
    863 	 * Make sure to zap the 'last cache state' pointer since the
    864 	 * pmap might be about to go away. Also ensure the outgoing
    865 	 * VM space's cache state is marked as NOT resident in the
    866 	 * cache, and that lwp0's cache state IS resident.
    867 	 */
    868 	ldr	r7, [r4, #(L_ADDR)]		/* r7 = old lwp's PCB */
    869 	ldr	r0, .Llast_cache_state_ptr	/* Last userland cache state */
    870 	ldr	r9, [r7, #(PCB_CSTATE)]		/* Fetch cache state pointer */
    871 	ldr	r3, [r5, #(L_ADDR)]		/* r3 = lwp0's PCB */
    872 	str	r2, [r0]			/* No previous cache state */
    873 	str	r2, [r9, #(CS_ALL)]		/* Zap old lwp's cache state */
    874 	ldr	r3, [r3, #(PCB_CSTATE)]		/* lwp0's cache state */
    875 	mov	r2, #-1
    876 	str	r2, [r3, #(CS_ALL)]		/* lwp0 is in da cache! */
    877 
    878 	/*
    879 	 * Don't allow user space access between the purge and the switch.
    880 	 */
    881 	mov	r2, #0x00000001
    882 	str	r2, [r1]
    883 
    884 	/* Switch to lwp0 context */
    885 
    886 	ldr	r9, .Lcpufuncs
    887 	mov	lr, pc
    888 	ldr	pc, [r9, #CF_IDCACHE_WBINV_ALL]
    889 
    890 	ldr	r0, [r7, #(PCB_PL1VEC)]
    891 	ldr	r1, [r7, #(PCB_DACR)]
    892 
    893 	/*
    894 	 * r0 = Pointer to L1 slot for vector_page (or NULL)
    895 	 * r1 = lwp0's DACR
    896 	 * r4 = lwp we're switching from
    897 	 * r5 = lwp0
    898 	 * r6 = exit func
    899 	 * r7 = lwp0's PCB
    900 	 * r9 = cpufuncs
    901 	 */
    902 
    903 	IRQdisableALL
    904 
    905 	/*
    906 	 * Ensure the vector table is accessible by fixing up lwp0's L1
    907 	 */
    908 	cmp	r0, #0			/* No need to fixup vector table? */
    909 	ldrne	r3, [r0]		/* But if yes, fetch current value */
    910 	ldrne	r2, [r7, #(PCB_L1VEC)]	/* Fetch new vector_page value */
    911 	mcr	p15, 0, r1, c3, c0, 0	/* Update DACR for lwp0's context */
    912 	cmpne	r3, r2			/* Stuffing the same value? */
    913 	strne	r2, [r0]		/* Store if not. */
    914 
    915 #ifdef PMAP_INCLUDE_PTE_SYNC
    916 	/*
    917 	 * Need to sync the cache to make sure that last store is
    918 	 * visible to the MMU.
    919 	 */
    920 	movne	r1, #4
    921 	movne	lr, pc
    922 	ldrne	pc, [r9, #CF_DCACHE_WB_RANGE]
    923 #endif /* PMAP_INCLUDE_PTE_SYNC */
    924 
    925 	/*
    926 	 * Note: We don't do the same optimisation as cpu_switch() with
    927 	 * respect to avoiding flushing the TLB if we're switching to
    928 	 * the same L1 since this process' VM space may be about to go
    929 	 * away, so we don't want *any* turds left in the TLB.
    930 	 */
    931 
    932 	/* Switch the memory to the new process */
    933 	ldr	r0, [r7, #(PCB_PAGEDIR)]
    934 	mov	lr, pc
    935 	ldr	pc, [r9, #CF_CONTEXT_SWITCH]
    936 
    937 	ldr	r0, .Lcurpcb
    938 
    939 	/* Restore all the save registers */
    940 	add	r1, r7, #PCB_R8
    941 	ldmia	r1, {r8-r13}
    942 
    943 	str	r7, [r0]	/* curpcb = lwp0's PCB */
    944 
    945 	IRQenableALL
    946 
    947 	/*
    948 	 * Schedule the vmspace and stack to be freed.
    949 	 */
    950 	mov	r0, r4			/* {lwp_}exit2(l) */
    951 	mov	lr, pc
    952 	mov	pc, r6
    953 
    954 	ldr	r7, .Lwhichqs		/* r7 = &whichqs */
    955 	mov	r5, #0x00000000		/* r5 = old lwp = NULL */
    956 	b	.Lswitch_search
    957 
    958 /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
    959 ENTRY(savectx)
    960 	/*
    961 	 * r0 = pcb
    962 	 */
    963 
    964 	/* Push registers.*/
    965 	stmfd	sp!, {r4-r7, lr}
    966 
    967 	/* Store all the registers in the process's pcb */
    968 	add	r2, r0, #(PCB_R8)
    969 	stmia	r2, {r8-r13}
    970 
    971 	/* Pull the regs of the stack */
    972 	ldmfd	sp!, {r4-r7, pc}
    973 
    974 ENTRY(proc_trampoline)
    975 #ifdef MULTIPROCESSOR
    976 	bl	_C_LABEL(proc_trampoline_mp)
    977 #endif
    978 	mov	r0, r5
    979 	mov	r1, sp
    980 	mov	lr, pc
    981 	mov	pc, r4
    982 
    983 	/* Kill irq's */
    984         mrs     r0, cpsr
    985         orr     r0, r0, #(I32_bit)
    986         msr     cpsr_c, r0
    987 
    988 	PULLFRAME
    989 
    990 	movs	pc, lr			/* Exit */
    991 
    992 	.type .Lcpu_switch_ffs_table, _ASM_TYPE_OBJECT;
    993 .Lcpu_switch_ffs_table:
    994 /* same as ffs table but all nums are -1 from that */
    995 /*               0   1   2   3   4   5   6   7           */
    996 	.byte	 0,  0,  1, 12,  2,  6,  0, 13  /*  0- 7 */
    997 	.byte	 3,  0,  7,  0,  0,  0,  0, 14  /*  8-15 */
    998 	.byte	10,  4,  0,  0,  8,  0,  0, 25  /* 16-23 */
    999 	.byte	 0,  0,  0,  0,  0, 21, 27, 15  /* 24-31 */
   1000 	.byte	31, 11,  5,  0,  0,  0,  0,  0	/* 32-39 */
   1001 	.byte	 9,  0,  0, 24,  0,  0, 20, 26  /* 40-47 */
   1002 	.byte	30,  0,  0,  0,  0, 23,  0, 19  /* 48-55 */
   1003 	.byte   29,  0, 22, 18, 28, 17, 16,  0  /* 56-63 */
   1004