Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.3.2.2
      1 /*	$NetBSD: cpuswitch.S,v 1.3.2.2 2001/11/15 06:39:21 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 Lcurproc:
    211 	.word	_C_LABEL(curproc)
    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 
    235 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 	bne	sw1
    255 
    256 	/* All processes are still asleep so idle a while longer */
    257 	b	idle
    258 
    259 
    260 /*
    261  * Find a new lwp to run, save the current context and
    262  * load the new context
    263  *
    264  * Arguments:
    265  *	r0	'struct lwp *' of the current LWP
    266  */
    267 
    268 ENTRY(cpu_switch)
    269 /*
    270  * Local register usage. Some of these registers are out of date.
    271  * r1 = oldlwp
    272  * r2 = spl level
    273  * r3 = whichqs
    274  * r4 = queue
    275  * r5 = &qs[queue]
    276  * r6 = newlwp
    277  * r7 = scratch
    278  */
    279 	stmfd	sp!, {r4-r7, lr}
    280 
    281 	/*
    282 	 * Get the current lwp and indicate that there is no longer
    283 	 * a valid process (curproc = 0)
    284 	 */
    285 	ldr	r7, Lcurproc
    286 	ldr	r1, [r7]
    287 	mov	r0, #0x00000000
    288 	str	r0, [r7]
    289 
    290 	/* Zero the pcb */
    291 	ldr	r7, Lcurpcb
    292 	str	r0, [r7]
    293 
    294 	/* Lower the spl level to spl0 and get the current spl level. */
    295 	mov	r7, r1
    296 
    297 #ifdef spl0
    298 	mov	r0, #(_SPL_0)
    299 	bl	_C_LABEL(splx)
    300 #else
    301 	bl	_C_LABEL(spl0)
    302 #endif
    303 
    304 	/* Push the old spl level onto the stack */
    305 	str	r0, [sp, #-0x0004]!
    306 
    307 	mov	r1, r7
    308 
    309 	/* First phase : find a new lwp */
    310 
    311 	/* rem: r1 = old proc */
    312 
    313 switch_search:
    314 	IRQdisable
    315 
    316 	/* Do we have any active queues  */
    317 	ldr	r7, Lwhichqs
    318 	ldr	r3, [r7]
    319 
    320 	/* If not we must idle until we do. */
    321 	teq	r3, #0x00000000
    322 	beq	idle
    323 
    324 sw1:
    325 	/* rem: r1 = old lwp */
    326 	/* rem: r3 = whichqs */
    327 	/* rem: interrupts are disabled */
    328 
    329 	/*
    330 	 * Paranoid debug time ....
    331 	 * Is this overkill ? If we are not in SVC mode then things are
    332 	 * very sick and will probably have already died.
    333 	 */
    334 #ifdef DIAGNOSTIC
    335 	mrs	r4, cpsr_all
    336 	and	r4, r4, #(PSR_MODE)
    337 	teq	r4, #(PSR_SVC32_MODE)
    338 	beq	switchmodeok
    339 
    340 	add	r0, pc, #switchpanic - . - 8
    341 	mrs	r1, cpsr_all
    342 	bl	_C_LABEL(panic)
    343 
    344 switchpanic:
    345 	.asciz	"Yikes! In cpu_switch() but not in SVC mode (%08x)\n"
    346 	.align	0
    347 
    348 switchmodeok:
    349 #endif
    350 
    351 	/*
    352 	 * We have found an active queue. Currently we do not know which queue
    353 	 * is active just that one of them is.
    354 	 */
    355 	/* this is the ffs algorithm devised by d.seal and posted to
    356 	 * comp.sys.arm on 16 Feb 1994.
    357 	 */
    358  	rsb	r5, r3, #0
    359  	ands	r0, r3, r5
    360 
    361 	adr	r5, Lcpu_switch_ffs_table
    362 
    363 				    /* X = R0 */
    364 	orr	r4, r0, r0, lsl #4  /* r4 = X * 0x11 */
    365 	orr	r4, r4, r4, lsl #6  /* r4 = X * 0x451 */
    366 	rsb	r4, r4, r4, lsl #16 /* r4 = X * 0x0450fbaf */
    367 
    368 	/* used further down, saves SA stall */
    369 	ldr	r6, Lqs
    370 
    371 	/* now lookup in table indexed on top 6 bits of a4 */
    372 	ldrb	r4, [ r5, r4, lsr #26 ]
    373 
    374 	/* rem: r0 = bit mask of chosen queue (1 << r4) */
    375 	/* rem: r1 = old lwp */
    376 	/* rem: r3 = whichqs */
    377 	/* rem: r4 = queue number */
    378 	/* rem: interrupts are disabled */
    379 
    380 	/* Get the address of the queue (&qs[queue]) */
    381 	add	r5, r6, r4, lsl #3
    382 
    383 	/*
    384 	 * Get the lwp from the queue and place the next process in
    385 	 * the queue at the head. This basically unlinks the lwp at
    386 	 * the head of the queue.
    387 	 */
    388 	ldr	r6, [r5, #(L_FORW)]
    389 
    390 	/* rem: r6 = new lwp */
    391 	ldr	r7, [r6, #(L_FORW)]
    392 	str	r7, [r5, #(L_FORW)]
    393 
    394 	/*
    395 	 * Test to see if the queue is now empty. If the head of the queue
    396 	 * points to the queue itself then there are no more lwps in
    397 	 * the queue. We can therefore clear the queue not empty flag held
    398 	 * in r3.
    399 	 */
    400 
    401 	teq	r5, r7
    402 	biceq	r3, r3, r0
    403 
    404 	/* rem: r0 = bit mask of chosen queue (1 << r4) - NOT NEEDED AN MORE */
    405 
    406 	/* Fix the back pointer for the lwp now at the head of the queue. */
    407 	ldr	r0, [r6, #(L_BACK)]
    408 	str	r0, [r7, #(L_BACK)]
    409 
    410 	/* Update the RAM copy of the queue not empty flags word. */
    411 	ldr	r7, Lwhichqs
    412 	str	r3, [r7]
    413 
    414 	/* rem: r1 = old lwp */
    415 	/* rem: r3 = whichqs - NOT NEEDED ANY MORE */
    416 	/* rem: r4 = queue number - NOT NEEDED ANY MORE */
    417 	/* rem: r6 = new lwp */
    418 	/* rem: interrupts are disabled */
    419 
    420 	/* Clear the want_resched flag */
    421 	mov	r0, #0x00000000
    422 	ldr	r7, Lwant_resched
    423 	str	r0, [r7]
    424 
    425 	/*
    426 	 * Clear the back pointer of the lwp we have removed from
    427 	 * the head of the queue. The new lwp is isolated now.
    428 	 */
    429 	mov	r0, #0x00000000
    430 	str	r0, [r6, #(L_BACK)]
    431 
    432 switch_resume:
    433 	/* l->l_cpu initialized in fork1() for single-processor */
    434 
    435 	/* Process is now on a processor. */
    436 	mov	r0, #LSONPROC			/* l->l_stat = LSONPROC */
    437 	strb	r0, [r6, #(L_STAT)]
    438 
    439 	/* We have a new curproc now so make a note it */
    440 	ldr	r7, Lcurproc
    441 	str	r6, [r7]
    442 
    443 	/* Hook in a new pcb */
    444 	ldr	r7, Lcurpcb
    445 	ldr	r0, [r6, #(L_ADDR)]
    446 	str	r0, [r7]
    447 
    448 	/* At this point we can allow IRQ's again. */
    449 	IRQenable
    450 
    451 	/* rem: r1 = old lwp */
    452 	/* rem: r4 = return value */
    453 	/* rem: r6 = new process */
    454 	/* rem: interrupts are disabled */
    455 
    456 	/*
    457 	 * If the new process is the same as the process that called
    458 	 * cpu_switch() then we do not need to save and restore any
    459 	 * contexts. This means we can make a quick exit.
    460 	 * The test is simple if curproc on entry (now in r1) is the
    461 	 * same as the proc removed from the queue we can jump to the exit.
    462 	 */
    463 	teq	r1, r6
    464 	moveq	r4, #0x00000000		/* default to "didn't switch" */
    465 	beq	switch_return
    466 
    467 	/*
    468 	 * At this point, we are guaranteed to be switching to
    469 	 * a new lwp.
    470 	 */
    471 	mov	r4, #0x00000001
    472 
    473 	/*
    474 	 * If the curproc on entry to cpu_switch was zero then the
    475 	 * process that called it was exiting. This means that we do
    476 	 * not need to save the current context. Instead we can jump
    477 	 * straight to restoring the context for the new process.
    478 	 */
    479 	teq	r1, #0x00000000
    480 	beq	switch_exited
    481 
    482 	/* rem: r1 = old proc */
    483 	/* rem: r4 = return value */
    484 	/* rem: r6 = new process */
    485 	/* rem: interrupts are disabled */
    486 
    487 	/* Stage two : Save old context */
    488 
    489 	/* Remember the old process in r0 */
    490 	mov	r0, r1
    491 
    492 	/* Get the user structure for the old process. */
    493 	ldr	r1, [r1, #(L_ADDR)]
    494 
    495 	/* Save all the registers in the old process's pcb */
    496 	add	r7, r1, #(PCB_R8)
    497 	stmia	r7, {r8-r13}
    498 
    499 	/*
    500 	 * This can be optimised... We know we want to go from SVC32
    501 	 * mode to UND32 mode
    502 	 */
    503         mrs	r3, cpsr_all
    504 	bic	r2, r3, #(PSR_MODE)
    505 	orr	r2, r2, #(PSR_UND32_MODE | I32_bit)
    506         msr	cpsr_all, r2
    507 
    508 	str	sp, [r1, #(PCB_UND_SP)]
    509 
    510         msr	cpsr_all, r3		/* Restore the old mode */
    511 
    512 	/* rem: r0 = old proc */
    513 	/* rem: r0 = old pcb */
    514 	/* rem: r4 = return value */
    515 	/* rem: r6 = new process */
    516 	/* rem: interrupts are disabled */
    517 
    518 	/* What else needs to be saved  Only FPA stuff when that is supported */
    519 
    520 	/* Third phase : restore saved context */
    521 
    522 switch_exited:
    523 	/* Don't allow user space access beween the purge and the switch */
    524 	ldr	r3, Lblock_userspace_access
    525 	ldr	r2, [r3]
    526 	orr	r0, r2, #1
    527 	str	r0, [r3]
    528 
    529 	stmfd	sp!, {r0-r3}
    530 	ldr	r0, Lcpufuncs
    531 	add	lr, pc, #Lcs_cache_purged - . - 8
    532 	ldr	pc, [r0, #CF_CACHE_PURGE_ID]
    533 
    534 Lcs_cache_purged:
    535 	ldmfd	sp!, {r0-r3}
    536 
    537 	/* At this point we need to kill IRQ's again. */
    538 	IRQdisable
    539 
    540 	/* Interrupts are disabled so we can allow user space accesses again
    541 	 * as none will occur until interrupts are re-enabled after the
    542 	 * switch.
    543 	 */
    544 	str	r2, [r3]
    545 
    546 	/* Get the user structure for the new process in r1 */
    547 	ldr	r1, [r6, #(L_ADDR)]
    548 
    549 	/* Get the pagedir physical address for the process. */
    550 	ldr	r0, [r1, #(PCB_PAGEDIR)]
    551 
    552 	/* Switch the memory to the new process */
    553 	ldr	r3, Lcpufuncs
    554 	add	lr, pc, #Lcs_context_switched - . - 8
    555 	ldr	pc, [r3, #CF_CONTEXT_SWITCH]
    556 
    557 Lcs_context_switched:
    558 	/*
    559 	 * This can be optimised... We know we want to go from SVC32
    560 	 * mode to UND32 mode
    561 	 */
    562         mrs	r3, cpsr_all
    563 	bic	r2, r3, #(PSR_MODE)
    564 	orr	r2, r2, #(PSR_UND32_MODE)
    565         msr	cpsr_all, r2
    566 
    567 	ldr	sp, [r1, #(PCB_UND_SP)]
    568 
    569         msr	cpsr_all, r3		/* Restore the old mode */
    570 
    571 	/* Restore all the save registers */
    572 	add	r7, r1, #PCB_R8
    573 	ldmia	r7, {r8-r13}
    574 
    575 	/* Remember the pcb currently in use */
    576 	ldr	r7, Lcurpcb
    577 	str	r1, [r7]
    578 
    579 #ifdef ARMFPE
    580 	add	r0, r1, #(USER_SIZE) & 0x00ff
    581 	add	r0, r0, #(USER_SIZE) & 0xff00
    582 	bl	_C_LABEL(arm_fpe_core_changecontext)
    583 #endif
    584 
    585 	/* We can enable interrupts again */
    586 	IRQenable
    587 
    588 switch_return:
    589 	/* We have a new curproc now so make a note it */
    590 /*
    591 	ldr	r7, Lcurproc
    592 	str	r6, [r7]
    593 */
    594 
    595 	/* Get the spl level from the stack and update the current spl level */
    596 	ldr	r0, [sp], #0x0004
    597 	bl	_C_LABEL(splx)
    598 
    599 	/* cpu_switch returns 1 == switched, 0 == didn't switch */
    600 	mov	r0, r4
    601 
    602 	/*
    603 	 * Pull the registers that got pushed when either savectx() or
    604 	 * cpu_switch() was called and return.
    605 	 */
    606 	ldmfd	sp!, {r4-r7, pc}
    607 
    608 /*
    609  * cpu_preempt(struct lwp *current, struct lwp *next)
    610  * Switch to the specified next LWP
    611  * Arguments:
    612  *
    613  *	r0	'struct lwp *' of the current LWP
    614  *	r1	'struct lwp *' of the LWP to switch to
    615  */
    616 ENTRY(cpu_preempt)
    617 	stmfd	sp!, {r4-r7, lr}
    618 
    619 	/* Lower the spl level to spl0 and get the current spl level. */
    620 	mov	r6, r0		/* save old lwp */
    621 	mov	r7, r1		/* save new lwp */
    622 
    623 #ifdef spl0
    624 	mov	r0, #(_SPL_0)
    625 	bl	_C_LABEL(splx)
    626 #else
    627 	bl	_C_LABEL(spl0)
    628 #endif
    629 
    630 	/* Push the old spl level onto the stack */
    631 	str	r0, [sp, #-0x0004]!
    632 
    633 	mov	r0, r6		/* restore old lwp */
    634 	mov	r1, r7		/* restore new lwp */
    635 
    636 	/* rem: r1 = new lwp */
    637 
    638 	IRQdisable
    639 
    640 	/* Do we have any active queues? */
    641 	ldr	r7, Lwhichqs
    642 	ldr	r3, [r7]
    643 
    644 	/* If none, panic! */
    645 	teq	r3, #0x00000000
    646 	beq	preempt_noqueues
    647 
    648 	/* rem: r0 = old lwp */
    649 	/* rem: r1 = new lwp */
    650 	/* rem: r3 = whichqs */
    651 	/* rem: r7 = &whichqs */
    652 	/* rem: interrupts are disabled */
    653 
    654 	/* Compute the queue bit corresponding to the new lwp. */
    655 	ldrb	r4, [r1, #(L_PRIORITY)]
    656 	mov	r2, #0x00000001
    657 	mov	r4, r4, lsr #2		/* queue number */
    658 	mov	r2, r2, lsl r4		/* queue bit */
    659 
    660 	/* rem: r0 = old lwp */
    661 	/* rem: r1 = new lwp */
    662 	/* rem: r2 = queue bit */
    663 	/* rem: r3 = whichqs */
    664 	/* rem: r4 = queue number */
    665 	/* rem: r7 = &whichqs */
    666 
    667 	/*
    668 	 * Unlink the lwp from the queue.
    669 	 */
    670 	ldr	r5, [r1, #(L_BACK)]	/* r5 = l->l_back */
    671 	mov	r6, #0x00000000
    672 	str	r6, [r1, #(L_BACK)]	/* firewall: l->l_back = NULL */
    673 	ldr	r6, [r1, #(L_FORW)]	/* r6 = l->l_forw */
    674 	str	r5, [r6, #(L_BACK)]	/* r6->l_back = r5 */
    675 	str	r6, [r5, #(L_FORW)]	/* r5->l_forw = r6 */
    676 
    677 	teq	r5, r6			/* see if queue is empty */
    678 	biceq	r3, r3, r2		/* clear bit if so */
    679 	streq	r3, [r7]		/* store it back if so */
    680 
    681 	/* rem: r2 (queue bit) now free */
    682 	/* rem: r3 (whichqs) now free */
    683 	/* rem: r7 (&whichqs) now free */
    684 
    685 	/*
    686 	 * Okay, set up registers the way cpu_switch() wants them,
    687 	 * and jump into the middle of it (where we bring up the
    688 	 * new process).
    689 	 */
    690 	mov	r6, r1			/* r6 = new lwp */
    691 	mov	r1, r0			/* r1 = old lwp */
    692 	b	switch_resume
    693 
    694 preempt_noqueues:
    695 	add	r0, pc, #preemptpanic - . - 8
    696 	bl	_C_LABEL(panic)
    697 
    698 preemptpanic:
    699 	.asciz	"cpu_preempt: whichqs empty"
    700 	.align	0
    701 
    702 Llwp0:
    703 	.word	_C_LABEL(lwp0)
    704 
    705 Lkernel_map:
    706 	.word	_C_LABEL(kernel_map)
    707 
    708 
    709 ENTRY(switch_exit)
    710 	/*
    711 	 * r0 = lwp
    712 	 * r1 = lwp0
    713 	 */
    714 
    715 	mov	r3, r0
    716  	ldr	r1, Llwp0
    717 
    718 	/* In case we fault */
    719 	mov	r2, #0x00000000
    720 	ldr	r0, Lcurproc
    721 	str	r2, [r0]
    722 
    723 /*	ldr	r0, Lcurpcb
    724 	str	r2, [r0]*/
    725 
    726 	/* Switch to lwp0 context */
    727 
    728 	stmfd	sp!, {r0-r3}
    729 
    730 	ldr	r0, Lcpufuncs
    731 	add	lr, pc, #Lse_cache_purged - . - 8
    732 	ldr	pc, [r0, #CF_CACHE_PURGE_ID]
    733 
    734 Lse_cache_purged:
    735 	ldmfd	sp!, {r0-r3}
    736 
    737 	IRQdisable
    738 
    739 	ldr	r2, [r1, #(L_ADDR)]
    740 	ldr	r0, [r2, #(PCB_PAGEDIR)]
    741 
    742 	/* Switch the memory to the new process */
    743 	ldr	r4, Lcpufuncs
    744 	add	lr, pc, #Lse_context_switched - . - 8
    745 	ldr	pc, [r4, #CF_CONTEXT_SWITCH]
    746 
    747 Lse_context_switched:
    748 	/* Restore all the save registers */
    749 	add	r7, r2, #PCB_R8
    750 	ldmia	r7, {r8-r13}
    751 
    752 	/* This is not really needed ! */
    753 	/* Yes it is for the su and fu routines */
    754 	ldr	r0, Lcurpcb
    755 	str	r2, [r0]
    756 
    757 	IRQenable
    758 
    759 /*	str	r3, [sp, #-0x0004]!*/
    760 
    761 	/*
    762 	 * Schedule the vmspace and stack to be freed.
    763 	 */
    764 	mov	r0, r3			/* exit2(l) */
    765 	bl	_C_LABEL(exit2)
    766 
    767 	/* Paranoia */
    768 	mov	r0, #0x00000000
    769 	ldr	r1, Lcurproc
    770 	str	r0, [r1]
    771 
    772         ldr     r1, Llwp0
    773 	b	switch_search
    774 
    775 ENTRY(switch_lwp_exit)
    776 	/*
    777 	 * r0 = lwp
    778 	 * r1 = lwp0
    779 	 */
    780 
    781 	mov	r3, r0
    782  	ldr	r1, Llwp0
    783 
    784 	/* In case we fault */
    785 	mov	r2, #0x00000000
    786 	ldr	r0, Lcurproc
    787 	str	r2, [r0]
    788 
    789 /*	ldr	r0, Lcurpcb
    790 	str	r2, [r0]*/
    791 
    792 	/* Switch to lwp0 context */
    793 
    794 	stmfd	sp!, {r0-r3}
    795 
    796 	ldr	r0, Lcpufuncs
    797 	add	lr, pc, #Lsle_cache_purged - . - 8
    798 	ldr	pc, [r0, #CF_CACHE_PURGE_ID]
    799 
    800 Lsle_cache_purged:
    801 	ldmfd	sp!, {r0-r3}
    802 
    803 	IRQdisable
    804 
    805 	ldr	r2, [r1, #(L_ADDR)]
    806 	ldr	r0, [r2, #(PCB_PAGEDIR)]
    807 
    808 	/* Switch the memory to the new process */
    809 	ldr	r4, Lcpufuncs
    810 	add	lr, pc, #Lsle_context_switched - . - 8
    811 	ldr	pc, [r4, #CF_CONTEXT_SWITCH]
    812 
    813 Lsle_context_switched:
    814 	/* Restore all the save registers */
    815 	add	r7, r2, #PCB_R8
    816 	ldmia	r7, {r8-r13}
    817 
    818 	/* This is not really needed ! */
    819 	/* Yes it is for the su and fu routines */
    820 	ldr	r0, Lcurpcb
    821 	str	r2, [r0]
    822 
    823 	IRQenable
    824 
    825 /*	str	r3, [sp, #-0x0004]!*/
    826 
    827 	/*
    828 	 * Schedule the vmspace and stack to be freed.
    829 	 */
    830 	mov	r0, r3			/* lwp_exit2(l) */
    831 	bl	_C_LABEL(lwp_exit2)
    832 
    833 	/* Paranoia */
    834 	mov	r0, #0x00000000
    835 	ldr	r1, Lcurproc
    836 	str	r0, [r1]
    837 
    838         ldr     r1, Llwp0
    839 	b	switch_search
    840 
    841 Lcurrent_spl_level:
    842 	.word	_C_LABEL(current_spl_level)
    843 
    844 ENTRY(savectx)
    845 	/*
    846 	 * r0 = pcb
    847 	 */
    848 
    849 	/* Push registers.*/
    850 	stmfd	sp!, {r4-r7, lr}
    851 
    852 	/* Store all the registers in the process's pcb */
    853 	add	r2, r0, #(PCB_R8)
    854 	stmia	r2, {r8-r13}
    855 
    856 	/* Pull the regs of the stack */
    857 	ldmfd	sp!, {r4-r7, pc}
    858 
    859 ENTRY(proc_trampoline)
    860 	add	lr, pc, #(trampoline_return - . - 8)
    861 	mov	r0, r5
    862 	mov	r1, sp
    863 	mov	pc, r4
    864 
    865 trampoline_return:
    866 	/* Kill irq's */
    867         mrs     r0, cpsr_all
    868         orr     r0, r0, #(I32_bit)
    869         msr     cpsr_all, r0
    870 
    871 	PULLFRAME
    872 
    873 	movs	pc, lr			/* Exit */
    874 
    875 	.type Lcpu_switch_ffs_table, _ASM_TYPE_OBJECT;
    876 Lcpu_switch_ffs_table:
    877 /* same as ffs table but all nums are -1 from that */
    878 /*               0   1   2   3   4   5   6   7           */
    879 	.byte	 0,  0,  1, 12,  2,  6,  0, 13  /*  0- 7 */
    880 	.byte	 3,  0,  7,  0,  0,  0,  0, 14  /*  8-15 */
    881 	.byte	10,  4,  0,  0,  8,  0,  0, 25  /* 16-23 */
    882 	.byte	 0,  0,  0,  0,  0, 21, 27, 15  /* 24-31 */
    883 	.byte	31, 11,  5,  0,  0,  0,  0,  0	/* 32-39 */
    884 	.byte	 9,  0,  0, 24,  0,  0, 20, 26  /* 40-47 */
    885 	.byte	30,  0,  0,  0,  0, 23,  0, 19  /* 48-55 */
    886 	.byte   29,  0, 22, 18, 28, 17, 16,  0  /* 56-63 */
    887 
    888 /* End of cpuswitch.S */
    889