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