Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.3.2.14
      1 /*	$NetBSD: cpuswitch.S,v 1.3.2.14 2002/08/12 20:02:00 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994-1998 Mark Brinicombe.
      5  * Copyright (c) 1994 Brini.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software written for Brini by Mark Brinicombe
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Brini.
     21  * 4. The name of the company nor the name of the author may be used to
     22  *    endorse or promote products derived from this software without specific
     23  *    prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  * RiscBSD kernel project
     38  *
     39  * cpuswitch.S
     40  *
     41  * cpu switching functions
     42  *
     43  * Created      : 15/10/94
     44  */
     45 
     46 #include "opt_armfpe.h"
     47 
     48 #include "assym.h"
     49 #include <machine/param.h>
     50 #include <machine/cpu.h>
     51 #include <machine/frame.h>
     52 #include <machine/asm.h>
     53 
     54 #undef IRQdisable
     55 #undef IRQenable
     56 
     57 /*
     58  * New experimental definitions of IRQdisable and IRQenable
     59  * These keep FIQ's enabled since FIQ's are special.
     60  */
     61 
     62 #define IRQdisable \
     63 	mrs	r14, cpsr_all ; \
     64 	orr	r14, r14, #(I32_bit) ; \
     65 	msr	cpsr_all, r14 ; \
     66 
     67 #define IRQenable \
     68 	mrs	r14, cpsr_all ; \
     69 	bic	r14, r14, #(I32_bit) ; \
     70 	msr	cpsr_all, r14 ; \
     71 
     72 /*
     73  * setrunqueue() and remrunqueue()
     74  *
     75  * Functions to add and remove a process for the run queue.
     76  */
     77 
     78 	.text
     79 
     80 Lwhichqs:
     81 	.word	_C_LABEL(sched_whichqs)
     82 
     83 Lqs:
     84 	.word	_C_LABEL(sched_qs)
     85 
     86 /*
     87  * On entry
     88  *	r0 = lwp
     89  */
     90 
     91 ENTRY(setrunqueue)
     92 	/*
     93 	 * Local register usage
     94 	 * 	r0 = process
     95 	 * 	r1 = queue
     96 	 * 	r2 = &qs[queue] and temp
     97 	 * 	r3 = temp
     98 	 *	r12 = whichqs
     99 	 */
    100 #ifdef DIAGNOSTIC
    101 	ldr	r1, [r0, #(L_BACK)]
    102 	teq	r1, #0x00000000
    103 	bne	Lsetrunqueue_erg
    104 
    105 	ldr	r1, [r0, #(L_WCHAN)]
    106 	teq	r1, #0x00000000
    107 	bne	Lsetrunqueue_erg
    108 #endif
    109 
    110 	/* Get the priority of the queue */
    111 	ldrb	r1, [r0, #(L_PRIORITY)]
    112 	mov	r1, r1, lsr #2
    113 
    114 	/* Indicate that there is a process on this queue */
    115 	ldr	r12, Lwhichqs
    116 	ldr	r2, [r12]
    117 	mov	r3, #0x00000001
    118 	mov	r3, r3, lsl r1
    119 	orr	r2, r2, r3
    120 	str	r2, [r12]
    121 
    122 	/* Get the address of the queue */
    123 	ldr	r2, Lqs
    124 	add	r1, r2, r1, lsl # 3
    125 
    126 	/* Hook the process in */
    127 	str	r1, [r0, #(L_FORW)]
    128 	ldr	r2, [r1, #(L_BACK)]
    129 
    130 	str	r0, [r1, #(L_BACK)]
    131 #ifdef DIAGNOSTIC
    132 	teq	r2, #0x00000000
    133 	beq	Lsetrunqueue_erg
    134 #endif
    135 	str	r0, [r2, #(L_FORW)]
    136 	str	r2, [r0, #(L_BACK)]
    137 
    138 	mov	pc, lr
    139 
    140 #ifdef DIAGNOSTIC
    141 Lsetrunqueue_erg:
    142 	mov	r2, r1
    143 	mov	r1, r0
    144 	add	r0, pc, #Ltext1 - . - 8
    145 	bl	_C_LABEL(printf)
    146 
    147 	ldr	r2, Lqs
    148 	ldr	r1, [r2]
    149 	add	r0, pc, #Ltext2 - . - 8
    150 	b	_C_LABEL(panic)
    151 
    152 Ltext1:
    153 	.asciz	"setrunqueue : %08x %08x\n"
    154 Ltext2:
    155 	.asciz	"setrunqueue : [qs]=%08x qs=%08x\n"
    156 	.align	0
    157 #endif
    158 
    159 /*
    160  * On entry
    161  *	r0 = lwp
    162  */
    163 
    164 ENTRY(remrunqueue)
    165 	/*
    166 	 * Local register usage
    167 	 *	r0 = oldproc
    168 	 * 	r1 = queue
    169 	 * 	r2 = &qs[queue] and scratch
    170 	 *	r3 = scratch
    171 	 *	r12 = whichqs
    172 	 */
    173 
    174 	/* Get the priority of the queue */
    175 	ldrb	r1, [r0, #(L_PRIORITY)]
    176 	mov	r1, r1, lsr #2
    177 
    178 	/* Unhook the process */
    179 	ldr	r2, [r0, #(L_FORW)]
    180 	ldr	r3, [r0, #(L_BACK)]
    181 
    182 	str	r3, [r2, #(L_BACK)]
    183 	str	r2, [r3, #(L_FORW)]
    184 
    185 	/* If the queue is now empty clear the queue not empty flag */
    186 	teq	r2, r3
    187 
    188 	/* This could be reworked to avoid the use of r4 */
    189 	ldreq	r12, Lwhichqs
    190 	ldreq	r2, [r12]
    191 	moveq	r3, #0x00000001
    192 	moveq	r3, r3, lsl r1
    193 	biceq	r2, r2, r3
    194 	streq	r2, [r12]
    195 
    196 	/* Remove the back pointer for the process */
    197 	mov	r1, #0x00000000
    198 	str	r1, [r0, #(L_BACK)]
    199 
    200 	mov	pc, lr
    201 
    202 
    203 /*
    204  * cpuswitch()
    205  *
    206  * preforms a process context switch.
    207  * This function has several entry points
    208  */
    209 
    210 Lcurlwp:
    211 	.word	_C_LABEL(curlwp)
    212 
    213 Lcurpcb:
    214 	.word	_C_LABEL(curpcb)
    215 
    216 Lwant_resched:
    217 	.word	_C_LABEL(want_resched)
    218 
    219 Lcpufuncs:
    220 	.word	_C_LABEL(cpufuncs)
    221 
    222 	.data
    223 	.global	_C_LABEL(curpcb)
    224 _C_LABEL(curpcb):
    225 	.word	0x00000000
    226 	.text
    227 
    228 Lblock_userspace_access:
    229 	.word	_C_LABEL(block_userspace_access)
    230 
    231 /*
    232  * Idle loop, exercised while waiting for a process to wake up.
    233  */
    234 /* LINTSTUB: Ignore */
    235 ASENTRY_NP(idle)
    236 	/* Enable interrupts */
    237 	IRQenable
    238 
    239 	/* XXX - r1 needs to be preserved for cpu_switch */
    240 	mov	r7, r1
    241 	ldr	r3, Lcpufuncs
    242 	mov	r0, #0
    243 	add	lr, pc, #Lidle_slept - . - 8
    244 	ldr	pc, [r3, #CF_SLEEP]
    245 
    246 Lidle_slept:
    247 	mov	r1, r7
    248 
    249 	/* Disable interrupts while we check for an active queue */
    250 	IRQdisable
    251 	ldr	r7, Lwhichqs
    252 	ldr	r3, [r7]
    253 	teq	r3, #0x00000000
    254 
    255 	beq	_ASM_LABEL(idle)
    256 	b	Lidle_ret
    257 
    258 /*
    259  * Find a new lwp to run, save the current context and
    260  * load the new context
    261  *
    262  * Arguments:
    263  *	r0	'struct lwp *' of the current LWP
    264  */
    265 
    266 ENTRY(cpu_switch)
    267 /*
    268  * Local register usage. Some of these registers are out of date.
    269  * r1 = oldlwp
    270  * r2 = spl level
    271  * r3 = whichqs
    272  * r4 = queue
    273  * r5 = &qs[queue]
    274  * r6 = newlwp
    275  * r7 = scratch
    276  */
    277 	stmfd	sp!, {r4-r7, lr}
    278 
    279 	/*
    280 	 * Get the current lwp and indicate that there is no longer
    281 	 * a valid process (curlwp = 0)
    282 	 */
    283 	ldr	r7, Lcurlwp
    284 	ldr	r1, [r7]
    285 	mov	r0, #0x00000000
    286 	str	r0, [r7]
    287 
    288 	/* Zero the pcb */
    289 	ldr	r7, Lcurpcb
    290 	str	r0, [r7]
    291 
    292 	/* Lower the spl level to spl0 and get the current spl level. */
    293 	mov	r7, r1
    294 
    295 #ifdef __NEWINTR
    296 	mov	r0, #(IPL_NONE)
    297 	bl	_C_LABEL(_spllower)
    298 #else /* ! __NEWINTR */
    299 #ifdef spl0
    300 	mov	r0, #(_SPL_0)
    301 	bl	_C_LABEL(splx)
    302 #else
    303 	bl	_C_LABEL(spl0)
    304 #endif /* spl0 */
    305 #endif /* __NEWINTR */
    306 
    307 	/* Push the old spl level onto the stack */
    308 	str	r0, [sp, #-0x0004]!
    309 
    310 	mov	r1, r7
    311 
    312 	/* First phase : find a new lwp */
    313 
    314 	/* rem: r1 = old lwp */
    315 
    316 Lswitch_search:
    317 	IRQdisable
    318 
    319 	/* Do we have any active queues  */
    320 	ldr	r7, Lwhichqs
    321 	ldr	r3, [r7]
    322 
    323 	/* If not we must idle until we do. */
    324 	teq	r3, #0x00000000
    325 	beq	_ASM_LABEL(idle)
    326 Lidle_ret:
    327 
    328 	/* rem: r1 = old lwp */
    329 	/* rem: r3 = whichqs */
    330 	/* rem: interrupts are disabled */
    331 
    332 	/*
    333 	 * We have found an active queue. Currently we do not know which queue
    334 	 * is active just that one of them is.
    335 	 */
    336 	/* this is the ffs algorithm devised by d.seal and posted to
    337 	 * comp.sys.arm on 16 Feb 1994.
    338 	 */
    339  	rsb	r5, r3, #0
    340  	ands	r0, r3, r5
    341 
    342 	adr	r5, Lcpu_switch_ffs_table
    343 
    344 				    /* X = R0 */
    345 	orr	r4, r0, r0, lsl #4  /* r4 = X * 0x11 */
    346 	orr	r4, r4, r4, lsl #6  /* r4 = X * 0x451 */
    347 	rsb	r4, r4, r4, lsl #16 /* r4 = X * 0x0450fbaf */
    348 
    349 	/* used further down, saves SA stall */
    350 	ldr	r6, Lqs
    351 
    352 	/* now lookup in table indexed on top 6 bits of a4 */
    353 	ldrb	r4, [ r5, r4, lsr #26 ]
    354 
    355 	/* rem: r0 = bit mask of chosen queue (1 << r4) */
    356 	/* rem: r1 = old lwp */
    357 	/* rem: r3 = whichqs */
    358 	/* rem: r4 = queue number */
    359 	/* rem: interrupts are disabled */
    360 
    361 	/* Get the address of the queue (&qs[queue]) */
    362 	add	r5, r6, r4, lsl #3
    363 
    364 	/*
    365 	 * Get the lwp from the queue and place the next process in
    366 	 * the queue at the head. This basically unlinks the lwp at
    367 	 * the head of the queue.
    368 	 */
    369 	ldr	r6, [r5, #(L_FORW)]
    370 
    371 	/* rem: r6 = new lwp */
    372 	ldr	r7, [r6, #(L_FORW)]
    373 	str	r7, [r5, #(L_FORW)]
    374 
    375 	/*
    376 	 * Test to see if the queue is now empty. If the head of the queue
    377 	 * points to the queue itself then there are no more lwps in
    378 	 * the queue. We can therefore clear the queue not empty flag held
    379 	 * in r3.
    380 	 */
    381 
    382 	teq	r5, r7
    383 	biceq	r3, r3, r0
    384 
    385 	/* rem: r0 = bit mask of chosen queue (1 << r4) - NOT NEEDED AN MORE */
    386 
    387 	/* Fix the back pointer for the lwp now at the head of the queue. */
    388 	ldr	r0, [r6, #(L_BACK)]
    389 	str	r0, [r7, #(L_BACK)]
    390 
    391 	/* Update the RAM copy of the queue not empty flags word. */
    392 	ldr	r7, Lwhichqs
    393 	str	r3, [r7]
    394 
    395 	/* rem: r1 = old lwp */
    396 	/* rem: r3 = whichqs - NOT NEEDED ANY MORE */
    397 	/* rem: r4 = queue number - NOT NEEDED ANY MORE */
    398 	/* rem: r6 = new lwp */
    399 	/* rem: interrupts are disabled */
    400 
    401 	/* Clear the want_resched flag */
    402 	ldr	r7, Lwant_resched
    403 	mov	r0, #0x00000000
    404 	str	r0, [r7]
    405 
    406 	/*
    407 	 * Clear the back pointer of the lwp we have removed from
    408 	 * the head of the queue. The new lwp is isolated now.
    409 	 */
    410 	str	r0, [r6, #(L_BACK)]
    411 
    412 switch_resume:
    413 	/* l->l_cpu initialized in fork1() for single-processor */
    414 
    415 	/* Process is now on a processor. */
    416 	mov	r0, #LSONPROC			/* l->l_stat = LSONPROC */
    417 	str	r0, [r6, #(L_STAT)]
    418 
    419 	/* We have a new curlwp now so make a note it */
    420 	ldr	r7, Lcurlwp
    421 	str	r6, [r7]
    422 
    423 	/* Hook in a new pcb */
    424 	ldr	r7, Lcurpcb
    425 	ldr	r0, [r6, #(L_ADDR)]
    426 	str	r0, [r7]
    427 
    428 	/* At this point we can allow IRQ's again. */
    429 	IRQenable
    430 
    431 	/* rem: r1 = old lwp */
    432 	/* rem: r4 = return value */
    433 	/* rem: r6 = new process */
    434 	/* rem: interrupts are enabled */
    435 
    436 	/*
    437 	 * If the new process is the same as the process that called
    438 	 * cpu_switch() then we do not need to save and restore any
    439 	 * contexts. This means we can make a quick exit.
    440 	 * The test is simple if curlwp on entry (now in r1) is the
    441 	 * same as the proc removed from the queue we can jump to the exit.
    442 	 */
    443 	teq	r1, r6
    444 	moveq	r4, #0x00000000		/* default to "didn't switch" */
    445 	beq	switch_return
    446 
    447 	/*
    448 	 * At this point, we are guaranteed to be switching to
    449 	 * a new lwp.
    450 	 */
    451 	mov	r4, #0x00000001
    452 
    453 	/* Remember the old lwp in r0 */
    454 	mov	r0, r1
    455 
    456 	/*
    457 	 * If the old lwp on entry to cpu_switch was zero then the
    458 	 * process that called it was exiting. This means that we do
    459 	 * not need to save the current context. Instead we can jump
    460 	 * straight to restoring the context for the new process.
    461 	 */
    462 	teq	r0, #0x00000000
    463 	beq	switch_exited
    464 
    465 	/* rem: r0 = old lwp */
    466 	/* rem: r4 = return value */
    467 	/* rem: r6 = new process */
    468 	/* rem: interrupts are enabled */
    469 
    470 	/* Stage two : Save old context */
    471 
    472 	/* Get the user structure for the old lwp. */
    473 	ldr	r1, [r0, #(L_ADDR)]
    474 
    475 	/* Save all the registers in the old lwp's pcb */
    476 	add	r7, r1, #(PCB_R8)
    477 	stmia	r7, {r8-r13}
    478 
    479 	/*
    480 	 * This can be optimised... We know we want to go from SVC32
    481 	 * mode to UND32 mode
    482 	 */
    483         mrs	r3, cpsr_all
    484 	bic	r2, r3, #(PSR_MODE)
    485 	orr	r2, r2, #(PSR_UND32_MODE | I32_bit)
    486         msr	cpsr_all, r2
    487 
    488 	str	sp, [r1, #(PCB_UND_SP)]
    489 
    490         msr	cpsr_all, r3		/* Restore the old mode */
    491 
    492 	/* rem: r0 = old lwp */
    493 	/* rem: r1 = old pcb */
    494 	/* rem: r4 = return value */
    495 	/* rem: r6 = new process */
    496 	/* rem: interrupts are enabled */
    497 
    498 	/* What else needs to be saved  Only FPA stuff when that is supported */
    499 
    500 	/* r1 now free! */
    501 
    502 	/* Third phase : restore saved context */
    503 
    504 	/* rem: r0 = old lwp */
    505 	/* rem: r4 = return value */
    506 	/* rem: r6 = new lwp */
    507 	/* rem: interrupts are enabled */
    508 
    509 	/*
    510 	 * Don't allow user space access between the purge and the switch.
    511 	 */
    512 	ldr	r3, Lblock_userspace_access
    513 	mov	r1, #0x00000001
    514 	mov	r2, #0x00000000
    515 	str	r1, [r3]
    516 
    517 	stmfd	sp!, {r0-r3}
    518 	ldr	r1, Lcpufuncs
    519 	add	lr, pc, #Lcs_cache_purged - . - 8
    520 	ldr	pc, [r1, #CF_IDCACHE_WBINV_ALL]
    521 
    522 Lcs_cache_purged:
    523 	ldmfd	sp!, {r0-r3}
    524 
    525 Lcs_cache_purge_skipped:
    526 	/* At this point we need to kill IRQ's again. */
    527 	IRQdisable
    528 
    529 	/*
    530 	 * Interrupts are disabled so we can allow user space accesses again
    531 	 * as none will occur until interrupts are re-enabled after the
    532 	 * switch.
    533 	 */
    534 	str	r2, [r3]
    535 
    536 	/* Get the user structure for the new process in r1 */
    537 	ldr	r1, [r6, #(L_ADDR)]
    538 
    539 	/* Get the pagedir physical address for the process. */
    540 	ldr	r0, [r1, #(PCB_PAGEDIR)]
    541 
    542 	/* Switch the memory to the new process */
    543 	ldr	r3, Lcpufuncs
    544 	add	lr, pc, #Lcs_context_switched - . - 8
    545 	ldr	pc, [r3, #CF_CONTEXT_SWITCH]
    546 
    547 Lcs_context_switched:
    548 	/*
    549 	 * This can be optimised... We know we want to go from SVC32
    550 	 * mode to UND32 mode
    551 	 */
    552         mrs	r3, cpsr_all
    553 	bic	r2, r3, #(PSR_MODE)
    554 	orr	r2, r2, #(PSR_UND32_MODE)
    555         msr	cpsr_all, r2
    556 
    557 	ldr	sp, [r1, #(PCB_UND_SP)]
    558 
    559         msr	cpsr_all, r3		/* Restore the old mode */
    560 
    561 	/* Restore all the save registers */
    562 	add	r7, r1, #PCB_R8
    563 	ldmia	r7, {r8-r13}
    564 
    565 #ifdef ARMFPE
    566 	add	r0, r1, #(USER_SIZE) & 0x00ff
    567 	add	r0, r0, #(USER_SIZE) & 0xff00
    568 	bl	_C_LABEL(arm_fpe_core_changecontext)
    569 #endif
    570 
    571 	/* We can enable interrupts again */
    572 	IRQenable
    573 
    574 switch_return:
    575 
    576 	/* Get the spl level from the stack and update the current spl level */
    577 	ldr	r0, [sp], #0x0004
    578 	bl	_C_LABEL(splx)
    579 
    580 	/* cpu_switch returns 1 == switched, 0 == didn't switch */
    581 	mov	r0, r4
    582 
    583 	/*
    584 	 * Pull the registers that got pushed when either savectx() or
    585 	 * cpu_switch() was called and return.
    586 	 */
    587 	ldmfd	sp!, {r4-r7, pc}
    588 
    589 switch_exited:
    590 	/*
    591 	 * We skip the cache purge because switch_exit()/switch_lwp_exit()
    592 	 * already did it.  Load up registers the way Lcs_cache_purge_skipped
    593 	 * expects.  Userpsace access already blocked by switch_exit()/
    594 	 * switch_lwp_exit().
    595 	 */
    596 	ldr	r3, Lblock_userspace_access
    597 	mov	r2, #0x00000000
    598 	b	Lcs_cache_purge_skipped
    599 
    600 /*
    601  * cpu_preempt(struct lwp *current, struct lwp *next)
    602  * Switch to the specified next LWP
    603  * Arguments:
    604  *
    605  *	r0	'struct lwp *' of the current LWP
    606  *	r1	'struct lwp *' of the LWP to switch to
    607  */
    608 ENTRY(cpu_preempt)
    609 	stmfd	sp!, {r4-r7, lr}
    610 
    611 	/* Lower the spl level to spl0 and get the current spl level. */
    612 	mov	r6, r0		/* save old lwp */
    613 	mov	r7, r1		/* save new lwp */
    614 
    615 #ifdef __NEWINTR
    616 	mov	r0, #(IPL_NONE)
    617 	bl	_C_LABEL(_spllower)
    618 #else /* ! __NEWINTR */
    619 #ifdef spl0
    620 	mov	r0, #(_SPL_0)
    621 	bl	_C_LABEL(splx)
    622 #else
    623 	bl	_C_LABEL(spl0)
    624 #endif /* spl0 */
    625 #endif /* __NEWINTR */
    626 
    627 	/* Push the old spl level onto the stack */
    628 	str	r0, [sp, #-0x0004]!
    629 
    630 	mov	r0, r6		/* restore old lwp */
    631 	mov	r1, r7		/* restore new lwp */
    632 
    633 	/* rem: r1 = new lwp */
    634 
    635 	IRQdisable
    636 
    637 	/* Do we have any active queues? */
    638 	ldr	r7, Lwhichqs
    639 	ldr	r3, [r7]
    640 
    641 	/* If none, panic! */
    642 	teq	r3, #0x00000000
    643 	beq	preempt_noqueues
    644 
    645 	/* rem: r0 = old lwp */
    646 	/* rem: r1 = new lwp */
    647 	/* rem: r3 = whichqs */
    648 	/* rem: r7 = &whichqs */
    649 	/* rem: interrupts are disabled */
    650 
    651 	/* Compute the queue bit corresponding to the new lwp. */
    652 	ldrb	r4, [r1, #(L_PRIORITY)]
    653 	mov	r2, #0x00000001
    654 	mov	r4, r4, lsr #2		/* queue number */
    655 	mov	r2, r2, lsl r4		/* queue bit */
    656 
    657 	/* rem: r0 = old lwp */
    658 	/* rem: r1 = new lwp */
    659 	/* rem: r2 = queue bit */
    660 	/* rem: r3 = whichqs */
    661 	/* rem: r4 = queue number */
    662 	/* rem: r7 = &whichqs */
    663 
    664 	/*
    665 	 * Unlink the lwp from the queue.
    666 	 */
    667 	ldr	r5, [r1, #(L_BACK)]	/* r5 = l->l_back */
    668 	mov	r6, #0x00000000
    669 	str	r6, [r1, #(L_BACK)]	/* firewall: l->l_back = NULL */
    670 	ldr	r6, [r1, #(L_FORW)]	/* r6 = l->l_forw */
    671 	str	r5, [r6, #(L_BACK)]	/* r6->l_back = r5 */
    672 	str	r6, [r5, #(L_FORW)]	/* r5->l_forw = r6 */
    673 
    674 	teq	r5, r6			/* see if queue is empty */
    675 	biceq	r3, r3, r2		/* clear bit if so */
    676 	streq	r3, [r7]		/* store it back if so */
    677 
    678 	/* rem: r2 (queue bit) now free */
    679 	/* rem: r3 (whichqs) now free */
    680 	/* rem: r7 (&whichqs) now free */
    681 
    682 	/*
    683 	 * Okay, set up registers the way cpu_switch() wants them,
    684 	 * and jump into the middle of it (where we bring up the
    685 	 * new process).
    686 	 */
    687 	mov	r6, r1			/* r6 = new lwp */
    688 	mov	r1, r0			/* r1 = old lwp */
    689 	b	switch_resume
    690 
    691 preempt_noqueues:
    692 	add	r0, pc, #preemptpanic - . - 8
    693 	bl	_C_LABEL(panic)
    694 
    695 preemptpanic:
    696 	.asciz	"cpu_preempt: whichqs empty"
    697 	.align	0
    698 
    699 Llwp0:
    700 	.word	_C_LABEL(lwp0)
    701 
    702 Lkernel_map:
    703 	.word	_C_LABEL(kernel_map)
    704 
    705 /*
    706  * void switch_exit(struct lwp *l, struct lwp *l0);
    707  * Switch to lwp0's saved context and deallocate the address space and kernel
    708  * stack for l.  Then jump into cpu_switch(), as if we were in lwp0 all along.
    709  */
    710 
    711 /* LINTSTUB: Func: void switch_exit(struct lwp *l, struct lwp *l0) */
    712 ENTRY(switch_exit)
    713 	/*
    714 	 * r0 = lwp
    715 	 * r1 = lwp0
    716 	 */
    717 
    718 	mov	r3, r0
    719 
    720 	/* In case we fault */
    721 	ldr	r0, Lcurlwp
    722 	mov	r2, #0x00000000
    723 	str	r2, [r0]
    724 
    725 /*	ldr	r0, Lcurpcb
    726 	str	r2, [r0]*/
    727 
    728 	/*
    729 	 * Don't allow user space access between the purge and the switch.
    730 	 */
    731 	ldr	r0, Lblock_userspace_access
    732 	mov	r2, #0x00000001
    733 	str	r2, [r0]
    734 
    735 	/* Switch to lwp0 context */
    736 
    737 	stmfd	sp!, {r0-r3}
    738 
    739 	ldr	r0, Lcpufuncs
    740 	add	lr, pc, #Lse_cache_purged - . - 8
    741 	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
    742 
    743 Lse_cache_purged:
    744 	ldmfd	sp!, {r0-r3}
    745 
    746 	IRQdisable
    747 
    748 	ldr	r2, [r1, #(L_ADDR)]
    749 	ldr	r0, [r2, #(PCB_PAGEDIR)]
    750 
    751 	/* Switch the memory to the new process */
    752 	ldr	r4, Lcpufuncs
    753 	add	lr, pc, #Lse_context_switched - . - 8
    754 	ldr	pc, [r4, #CF_CONTEXT_SWITCH]
    755 
    756 Lse_context_switched:
    757 	/* Restore all the save registers */
    758 	add	r7, r2, #PCB_R8
    759 	ldmia	r7, {r8-r13}
    760 
    761 	/* This is not really needed ! */
    762 	/* Yes it is for the su and fu routines */
    763 	ldr	r0, Lcurpcb
    764 	str	r2, [r0]
    765 
    766 	IRQenable
    767 
    768 /*	str	r3, [sp, #-0x0004]!*/
    769 
    770 	/*
    771 	 * Schedule the vmspace and stack to be freed.
    772 	 */
    773 	mov	r0, r3			/* exit2(l) */
    774 	bl	_C_LABEL(exit2)
    775 
    776 	/* Paranoia */
    777 	mov	r0, #0x00000000
    778 	ldr	r1, Lcurlwp
    779 	str	r0, [r1]
    780 
    781 	mov	r1, #0x00000000		/* r1 = old lwp = NULL */
    782 	b	Lswitch_search
    783 
    784 /*
    785  * void switch_lwp_exit(struct lwp *l, struct lwp *l0);
    786  * Switch to lwp0's saved context and deallocate the address space and kernel
    787  * stack for l.  Then jump into cpu_switch(), as if we were in lwp0 all along.
    788  */
    789 
    790 /* LINTSTUB: Func: void switch_exit(struct lwp *l, struct lwp *l0) */
    791 ENTRY(switch_lwp_exit)
    792 	/*
    793 	 * r0 = lwp
    794 	 * r1 = lwp0
    795 	 */
    796 
    797 	mov	r3, r0
    798 
    799 	/* In case we fault */
    800 	mov	r2, #0x00000000
    801 	ldr	r0, Lcurlwp
    802 	str	r2, [r0]
    803 
    804 /*	ldr	r0, Lcurpcb
    805 	str	r2, [r0]*/
    806 
    807 	/*
    808 	 * Don't allow user space access between the purge and the switch.
    809 	 */
    810 	ldr	r0, Lblock_userspace_access
    811 	mov	r2, #0x00000001
    812 	str	r2, [r0]
    813 
    814 	/* Switch to lwp0 context */
    815 
    816 	stmfd	sp!, {r0-r3}
    817 
    818 	ldr	r0, Lcpufuncs
    819 	add	lr, pc, #Lsle_cache_purged - . - 8
    820 	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
    821 
    822 Lsle_cache_purged:
    823 	ldmfd	sp!, {r0-r3}
    824 
    825 	IRQdisable
    826 
    827 	ldr	r2, [r1, #(L_ADDR)]
    828 	ldr	r0, [r2, #(PCB_PAGEDIR)]
    829 
    830 	/* Switch the memory to the new process */
    831 	ldr	r4, Lcpufuncs
    832 	add	lr, pc, #Lsle_context_switched - . - 8
    833 	ldr	pc, [r4, #CF_CONTEXT_SWITCH]
    834 
    835 Lsle_context_switched:
    836 	/* Restore all the save registers */
    837 	add	r7, r2, #PCB_R8
    838 	ldmia	r7, {r8-r13}
    839 
    840 	/* This is not really needed ! */
    841 	/* Yes it is for the su and fu routines */
    842 	ldr	r0, Lcurpcb
    843 	str	r2, [r0]
    844 
    845 	IRQenable
    846 
    847 /*	str	r3, [sp, #-0x0004]!*/
    848 
    849 	/*
    850 	 * Schedule the vmspace and stack to be freed.
    851 	 */
    852 	mov	r0, r3			/* lwp_exit2(l) */
    853 	bl	_C_LABEL(lwp_exit2)
    854 
    855 	/* Paranoia */
    856 	ldr	r1, Lcurlwp
    857 	mov	r0, #0x00000000
    858 	str	r0, [r1]
    859 
    860 	mov	r1, #0x00000000		/* r1 = old lwp = NULL */
    861 	b	Lswitch_search
    862 
    863 /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
    864 ENTRY(savectx)
    865 	/*
    866 	 * r0 = pcb
    867 	 */
    868 
    869 	/* Push registers.*/
    870 	stmfd	sp!, {r4-r7, lr}
    871 
    872 	/* Store all the registers in the process's pcb */
    873 	add	r2, r0, #(PCB_R8)
    874 	stmia	r2, {r8-r13}
    875 
    876 	/* Pull the regs of the stack */
    877 	ldmfd	sp!, {r4-r7, pc}
    878 
    879 ENTRY(proc_trampoline)
    880 	add	lr, pc, #(trampoline_return - . - 8)
    881 	mov	r0, r5
    882 	mov	r1, sp
    883 	mov	pc, r4
    884 
    885 trampoline_return:
    886 	/* Kill irq's */
    887         mrs     r0, cpsr_all
    888         orr     r0, r0, #(I32_bit)
    889         msr     cpsr_all, r0
    890 
    891 	PULLFRAME
    892 
    893 	movs	pc, lr			/* Exit */
    894 
    895 	.type Lcpu_switch_ffs_table, _ASM_TYPE_OBJECT;
    896 Lcpu_switch_ffs_table:
    897 /* same as ffs table but all nums are -1 from that */
    898 /*               0   1   2   3   4   5   6   7           */
    899 	.byte	 0,  0,  1, 12,  2,  6,  0, 13  /*  0- 7 */
    900 	.byte	 3,  0,  7,  0,  0,  0,  0, 14  /*  8-15 */
    901 	.byte	10,  4,  0,  0,  8,  0,  0, 25  /* 16-23 */
    902 	.byte	 0,  0,  0,  0,  0, 21, 27, 15  /* 24-31 */
    903 	.byte	31, 11,  5,  0,  0,  0,  0,  0	/* 32-39 */
    904 	.byte	 9,  0,  0, 24,  0,  0, 20, 26  /* 40-47 */
    905 	.byte	30,  0,  0,  0,  0, 23,  0, 19  /* 48-55 */
    906 	.byte   29,  0, 22, 18, 28, 17, 16,  0  /* 56-63 */
    907 
    908 /* End of cpuswitch.S */
    909