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