Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.21
      1  1.21    bjh21 /*	$NetBSD: cpuswitch.S,v 1.21 2002/10/09 22:28:03 bjh21 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.19    bjh21 #include "opt_multiprocessor.h"
     48   1.1    chris 
     49   1.1    chris #include "assym.h"
     50   1.1    chris #include <machine/param.h>
     51   1.1    chris #include <machine/cpu.h>
     52   1.1    chris #include <machine/frame.h>
     53   1.1    chris #include <machine/asm.h>
     54   1.1    chris 
     55   1.1    chris #undef IRQdisable
     56   1.1    chris #undef IRQenable
     57   1.1    chris 
     58   1.1    chris /*
     59   1.1    chris  * New experimental definitions of IRQdisable and IRQenable
     60   1.1    chris  * These keep FIQ's enabled since FIQ's are special.
     61   1.1    chris  */
     62   1.1    chris 
     63   1.1    chris #define IRQdisable \
     64  1.13  thorpej 	mrs	r14, cpsr ; \
     65   1.1    chris 	orr	r14, r14, #(I32_bit) ; \
     66  1.13  thorpej 	msr	cpsr_c, r14 ; \
     67   1.1    chris 
     68   1.1    chris #define IRQenable \
     69  1.13  thorpej 	mrs	r14, cpsr ; \
     70   1.1    chris 	bic	r14, r14, #(I32_bit) ; \
     71  1.13  thorpej 	msr	cpsr_c, r14 ; \
     72   1.1    chris 
     73   1.1    chris /*
     74   1.1    chris  * setrunqueue() and remrunqueue()
     75   1.1    chris  *
     76   1.1    chris  * Functions to add and remove a process for the run queue.
     77   1.1    chris  */
     78   1.1    chris 
     79   1.1    chris 	.text
     80   1.1    chris 
     81  1.17  thorpej .Lwhichqs:
     82   1.1    chris 	.word	_C_LABEL(sched_whichqs)
     83   1.1    chris 
     84  1.17  thorpej .Lqs:
     85   1.1    chris 	.word	_C_LABEL(sched_qs)
     86   1.1    chris 
     87   1.1    chris /*
     88   1.1    chris  * On entry
     89   1.1    chris  *	r0 = process
     90   1.1    chris  */
     91   1.1    chris 
     92   1.1    chris ENTRY(setrunqueue)
     93   1.1    chris 	/*
     94   1.1    chris 	 * Local register usage
     95   1.1    chris 	 * 	r0 = process
     96   1.1    chris 	 * 	r1 = queue
     97   1.1    chris 	 * 	r2 = &qs[queue] and temp
     98   1.1    chris 	 * 	r3 = temp
     99   1.1    chris 	 *	r12 = whichqs
    100   1.1    chris 	 */
    101   1.1    chris #ifdef DIAGNOSTIC
    102   1.1    chris 	ldr	r1, [r0, #(P_BACK)]
    103   1.1    chris 	teq	r1, #0x00000000
    104  1.14   briggs 	bne	.Lsetrunqueue_erg
    105   1.1    chris 
    106   1.1    chris 	ldr	r1, [r0, #(P_WCHAN)]
    107   1.1    chris 	teq	r1, #0x00000000
    108  1.14   briggs 	bne	.Lsetrunqueue_erg
    109   1.1    chris #endif
    110   1.1    chris 
    111   1.1    chris 	/* Get the priority of the queue */
    112   1.1    chris 	ldrb	r1, [r0, #(P_PRIORITY)]
    113   1.1    chris 
    114   1.1    chris 	/* Indicate that there is a process on this queue */
    115  1.17  thorpej 	ldr	r12, .Lwhichqs
    116  1.12    chris 	mov	r1, r1, lsr #2
    117   1.1    chris 	ldr	r2, [r12]
    118   1.1    chris 	mov	r3, #0x00000001
    119   1.1    chris 	mov	r3, r3, lsl r1
    120   1.1    chris 	orr	r2, r2, r3
    121   1.1    chris 	str	r2, [r12]
    122   1.1    chris 
    123   1.1    chris 	/* Get the address of the queue */
    124  1.17  thorpej 	ldr	r2, .Lqs
    125   1.1    chris 	add	r1, r2, r1, lsl # 3
    126   1.1    chris 
    127   1.1    chris 	/* Hook the process in */
    128   1.1    chris 	str	r1, [r0, #(P_FORW)]
    129   1.1    chris 	ldr	r2, [r1, #(P_BACK)]
    130   1.1    chris 
    131   1.1    chris 	str	r0, [r1, #(P_BACK)]
    132   1.1    chris #ifdef DIAGNOSTIC
    133   1.1    chris 	teq	r2, #0x00000000
    134  1.14   briggs 	beq	.Lsetrunqueue_erg
    135   1.1    chris #endif
    136   1.1    chris 	str	r0, [r2, #(P_FORW)]
    137   1.1    chris 	str	r2, [r0, #(P_BACK)]
    138   1.1    chris 
    139   1.1    chris 	mov	pc, lr
    140   1.1    chris 
    141   1.1    chris #ifdef DIAGNOSTIC
    142  1.14   briggs .Lsetrunqueue_erg:
    143   1.1    chris 	mov	r2, r1
    144   1.1    chris 	mov	r1, r0
    145  1.17  thorpej 	add	r0, pc, #.Ltext1 - . - 8
    146   1.1    chris 	bl	_C_LABEL(printf)
    147   1.1    chris 
    148  1.17  thorpej 	ldr	r2, .Lqs
    149   1.1    chris 	ldr	r1, [r2]
    150  1.17  thorpej 	add	r0, pc, #.Ltext2 - . - 8
    151   1.1    chris 	b	_C_LABEL(panic)
    152   1.1    chris 
    153  1.17  thorpej .Ltext1:
    154   1.1    chris 	.asciz	"setrunqueue : %08x %08x\n"
    155  1.17  thorpej .Ltext2:
    156   1.1    chris 	.asciz	"setrunqueue : [qs]=%08x qs=%08x\n"
    157   1.1    chris 	.align	0
    158   1.1    chris #endif
    159   1.1    chris 
    160   1.1    chris /*
    161   1.1    chris  * On entry
    162   1.1    chris  *	r0 = process
    163   1.1    chris  */
    164   1.1    chris 
    165   1.1    chris ENTRY(remrunqueue)
    166   1.1    chris 	/*
    167   1.1    chris 	 * Local register usage
    168   1.1    chris 	 *	r0 = oldproc
    169   1.1    chris 	 * 	r1 = queue
    170   1.1    chris 	 * 	r2 = &qs[queue] and scratch
    171   1.1    chris 	 *	r3 = scratch
    172   1.1    chris 	 *	r12 = whichqs
    173   1.1    chris 	 */
    174   1.1    chris 
    175   1.1    chris 	/* Get the priority of the queue */
    176   1.1    chris 	ldrb	r1, [r0, #(P_PRIORITY)]
    177   1.1    chris 	mov	r1, r1, lsr #2
    178   1.1    chris 
    179   1.1    chris 	/* Unhook the process */
    180   1.1    chris 	ldr	r2, [r0, #(P_FORW)]
    181   1.1    chris 	ldr	r3, [r0, #(P_BACK)]
    182   1.1    chris 
    183   1.1    chris 	str	r3, [r2, #(P_BACK)]
    184   1.1    chris 	str	r2, [r3, #(P_FORW)]
    185   1.1    chris 
    186   1.1    chris 	/* If the queue is now empty clear the queue not empty flag */
    187   1.1    chris 	teq	r2, r3
    188   1.1    chris 
    189   1.1    chris 	/* This could be reworked to avoid the use of r4 */
    190  1.17  thorpej 	ldreq	r12, .Lwhichqs
    191  1.12    chris 	moveq	r3, #0x00000001
    192   1.1    chris 	ldreq	r2, [r12]
    193   1.1    chris 	moveq	r3, r3, lsl r1
    194   1.1    chris 	biceq	r2, r2, r3
    195   1.1    chris 	streq	r2, [r12]
    196   1.1    chris 
    197   1.1    chris 	/* Remove the back pointer for the process */
    198   1.1    chris 	mov	r1, #0x00000000
    199   1.1    chris 	str	r1, [r0, #(P_BACK)]
    200   1.1    chris 
    201   1.1    chris 	mov	pc, lr
    202   1.1    chris 
    203   1.1    chris 
    204   1.1    chris /*
    205   1.1    chris  * cpuswitch()
    206   1.1    chris  *
    207   1.1    chris  * preforms a process context switch.
    208   1.1    chris  * This function has several entry points
    209   1.1    chris  */
    210   1.1    chris 
    211  1.19    bjh21 #ifdef MULTIPROCESSOR
    212  1.19    bjh21 .Lcpu_info_store:
    213  1.19    bjh21 	.word	_C_LABEL(cpu_info_store)
    214  1.19    bjh21 .Lcurproc:
    215  1.19    bjh21 	/* FIXME: This is bogus in the general case. */
    216  1.19    bjh21 	.word	_C_LABEL(cpu_info_store) + CI_CURPROC
    217  1.19    bjh21 #else
    218  1.17  thorpej .Lcurproc:
    219   1.1    chris 	.word	_C_LABEL(curproc)
    220  1.19    bjh21 #endif
    221   1.1    chris 
    222  1.17  thorpej .Lcurpcb:
    223   1.1    chris 	.word	_C_LABEL(curpcb)
    224   1.1    chris 
    225  1.17  thorpej .Lwant_resched:
    226   1.1    chris 	.word	_C_LABEL(want_resched)
    227   1.1    chris 
    228  1.17  thorpej .Lcpufuncs:
    229   1.1    chris 	.word	_C_LABEL(cpufuncs)
    230   1.1    chris 
    231   1.1    chris 	.data
    232   1.1    chris 	.global	_C_LABEL(curpcb)
    233   1.1    chris _C_LABEL(curpcb):
    234   1.1    chris 	.word	0x00000000
    235   1.1    chris 	.text
    236   1.1    chris 
    237  1.17  thorpej .Lblock_userspace_access:
    238   1.1    chris 	.word	_C_LABEL(block_userspace_access)
    239   1.1    chris 
    240  1.15  thorpej .Lcpu_do_powersave:
    241  1.15  thorpej 	.word	_C_LABEL(cpu_do_powersave)
    242  1.15  thorpej 
    243   1.1    chris /*
    244   1.1    chris  * Idle loop, exercised while waiting for a process to wake up.
    245  1.16  thorpej  *
    246  1.16  thorpej  * NOTE: When we jump back to .Lswitch_search, we must have a
    247  1.16  thorpej  * pointer to whichqs in r7, which is what it is when we arrive
    248  1.16  thorpej  * here.
    249   1.1    chris  */
    250   1.7    chris /* LINTSTUB: Ignore */
    251   1.4    chris ASENTRY_NP(idle)
    252  1.19    bjh21 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    253   1.7    chris 	bl	_C_LABEL(sched_unlock_idle)
    254   1.7    chris #endif
    255  1.16  thorpej 	ldr	r3, .Lcpu_do_powersave
    256  1.16  thorpej 
    257   1.1    chris 	/* Enable interrupts */
    258   1.1    chris 	IRQenable
    259   1.1    chris 
    260  1.15  thorpej 	/* If we don't want to sleep, use a simpler loop. */
    261  1.16  thorpej 	ldr	r3, [r3]		/* r3 = cpu_do_powersave */
    262  1.15  thorpej 	teq	r3, #0
    263  1.16  thorpej 	bne	2f
    264  1.16  thorpej 
    265  1.16  thorpej 	/* Non-powersave idle. */
    266  1.16  thorpej 1:	/* should maybe do uvm pageidlezero stuff here */
    267  1.16  thorpej 	ldr	r3, [r7]		/* r3 = whichqs */
    268  1.16  thorpej 	teq	r3, #0x00000000
    269  1.16  thorpej 	bne	.Lswitch_search
    270  1.16  thorpej 	b	1b
    271  1.15  thorpej 
    272  1.16  thorpej 2:	/* Powersave idle. */
    273  1.17  thorpej 	ldr	r4, .Lcpufuncs
    274  1.16  thorpej 3:	ldr	r3, [r7]		/* r3 = whichqs */
    275  1.15  thorpej 	teq	r3, #0x00000000
    276  1.15  thorpej 	bne	.Lswitch_search
    277  1.15  thorpej 
    278  1.15  thorpej 	/* if saving power, don't want to pageidlezero */
    279   1.1    chris 	mov	r0, #0
    280  1.21    bjh21 	adr	lr, 3b
    281  1.15  thorpej 	ldr	pc, [r4, #(CF_SLEEP)]
    282  1.15  thorpej 	/* loops back around */
    283  1.15  thorpej 
    284   1.1    chris 
    285   1.1    chris /*
    286   1.1    chris  * Find a new process to run, save the current context and
    287   1.1    chris  * load the new context
    288   1.1    chris  */
    289   1.1    chris 
    290   1.1    chris ENTRY(cpu_switch)
    291   1.1    chris /*
    292   1.1    chris  * Local register usage. Some of these registers are out of date.
    293   1.1    chris  * r1 = oldproc
    294   1.1    chris  * r3 = whichqs
    295   1.1    chris  * r4 = queue
    296   1.1    chris  * r5 = &qs[queue]
    297   1.1    chris  * r6 = newproc
    298   1.1    chris  * r7 = scratch
    299   1.1    chris  */
    300   1.1    chris 	stmfd	sp!, {r4-r7, lr}
    301   1.1    chris 
    302   1.1    chris 	/*
    303   1.1    chris 	 * Get the current process and indicate that there is no longer
    304  1.11  thorpej 	 * a valid process (curproc = 0).  Zero the current PCB pointer
    305  1.11  thorpej 	 * while we're at it.
    306   1.1    chris 	 */
    307  1.17  thorpej 	ldr	r7, .Lcurproc
    308  1.17  thorpej 	ldr	r6, .Lcurpcb
    309   1.1    chris 	mov	r0, #0x00000000
    310  1.11  thorpej 	ldr	r1, [r7]		/* r1 = curproc */
    311  1.11  thorpej 	str	r0, [r7]		/* curproc = NULL */
    312  1.11  thorpej 	str	r0, [r6]		/* curpcb = NULL */
    313   1.1    chris 
    314  1.10  thorpej 	/* stash the old proc while we call functions */
    315  1.10  thorpej 	mov	r5, r1
    316   1.1    chris 
    317  1.19    bjh21 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    318   1.7    chris 	/* release the sched_lock before handling interrupts */
    319   1.7    chris 	bl	_C_LABEL(sched_unlock_idle)
    320   1.7    chris #endif
    321   1.7    chris 
    322   1.7    chris 	/* Lower the spl level to spl0 and get the current spl level. */
    323   1.5  thorpej #ifdef __NEWINTR
    324   1.5  thorpej 	mov	r0, #(IPL_NONE)
    325   1.5  thorpej 	bl	_C_LABEL(_spllower)
    326   1.5  thorpej #else /* ! __NEWINTR */
    327   1.1    chris #ifdef spl0
    328   1.1    chris 	mov	r0, #(_SPL_0)
    329   1.1    chris 	bl	_C_LABEL(splx)
    330   1.1    chris #else
    331   1.1    chris 	bl	_C_LABEL(spl0)
    332   1.5  thorpej #endif /* spl0 */
    333   1.5  thorpej #endif /* __NEWINTR */
    334   1.1    chris 
    335   1.1    chris 	/* Push the old spl level onto the stack */
    336   1.1    chris 	str	r0, [sp, #-0x0004]!
    337   1.1    chris 
    338   1.1    chris 	/* First phase : find a new process */
    339   1.1    chris 
    340  1.17  thorpej 	ldr	r7, .Lwhichqs
    341  1.16  thorpej 
    342   1.7    chris 	/* rem: r5 = old proc */
    343  1.16  thorpej 	/* rem: r7 = &whichqs */
    344   1.7    chris 
    345  1.14   briggs .Lswitch_search:
    346   1.1    chris 	IRQdisable
    347  1.19    bjh21 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    348   1.7    chris 	bl	_C_LABEL(sched_lock_idle)
    349   1.7    chris #endif
    350   1.7    chris 
    351   1.1    chris 	/* Do we have any active queues  */
    352   1.1    chris 	ldr	r3, [r7]
    353   1.1    chris 
    354   1.1    chris 	/* If not we must idle until we do. */
    355   1.1    chris 	teq	r3, #0x00000000
    356   1.4    chris 	beq	_ASM_LABEL(idle)
    357   1.7    chris 
    358  1.10  thorpej 	/* put old proc back in r1 */
    359   1.7    chris 	mov	r1, r5
    360   1.1    chris 
    361   1.1    chris 	/* rem: r1 = old proc */
    362   1.1    chris 	/* rem: r3 = whichqs */
    363   1.1    chris 	/* rem: interrupts are disabled */
    364   1.1    chris 
    365   1.1    chris 	/*
    366   1.1    chris 	 * We have found an active queue. Currently we do not know which queue
    367   1.1    chris 	 * is active just that one of them is.
    368   1.1    chris 	 */
    369   1.1    chris 	/* this is the ffs algorithm devised by d.seal and posted to
    370   1.1    chris 	 * comp.sys.arm on 16 Feb 1994.
    371   1.1    chris 	 */
    372   1.1    chris  	rsb	r5, r3, #0
    373   1.1    chris  	ands	r0, r3, r5
    374   1.1    chris 
    375  1.17  thorpej 	adr	r5, .Lcpu_switch_ffs_table
    376   1.1    chris 
    377   1.3    chris 				    /* X = R0 */
    378   1.3    chris 	orr	r4, r0, r0, lsl #4  /* r4 = X * 0x11 */
    379   1.3    chris 	orr	r4, r4, r4, lsl #6  /* r4 = X * 0x451 */
    380   1.3    chris 	rsb	r4, r4, r4, lsl #16 /* r4 = X * 0x0450fbaf */
    381   1.1    chris 
    382   1.1    chris 	/* used further down, saves SA stall */
    383  1.17  thorpej 	ldr	r6, .Lqs
    384   1.1    chris 
    385   1.3    chris 	/* now lookup in table indexed on top 6 bits of a4 */
    386   1.1    chris 	ldrb	r4, [ r5, r4, lsr #26 ]
    387   1.1    chris 
    388   1.1    chris 	/* rem: r0 = bit mask of chosen queue (1 << r4) */
    389   1.1    chris 	/* rem: r1 = old proc */
    390   1.1    chris 	/* rem: r3 = whichqs */
    391   1.1    chris 	/* rem: r4 = queue number */
    392   1.1    chris 	/* rem: interrupts are disabled */
    393   1.1    chris 
    394   1.1    chris 	/* Get the address of the queue (&qs[queue]) */
    395   1.1    chris 	add	r5, r6, r4, lsl #3
    396   1.1    chris 
    397   1.1    chris 	/*
    398   1.1    chris 	 * Get the process from the queue and place the next process in
    399   1.1    chris 	 * the queue at the head. This basically unlinks the process at
    400   1.1    chris 	 * the head of the queue.
    401   1.1    chris 	 */
    402   1.1    chris 	ldr	r6, [r5, #(P_FORW)]
    403   1.1    chris 
    404   1.1    chris 	/* rem: r6 = new process */
    405   1.1    chris 	ldr	r7, [r6, #(P_FORW)]
    406   1.1    chris 	str	r7, [r5, #(P_FORW)]
    407   1.1    chris 
    408   1.1    chris 	/*
    409   1.1    chris 	 * Test to see if the queue is now empty. If the head of the queue
    410   1.1    chris 	 * points to the queue itself then there are no more processes in
    411   1.1    chris 	 * the queue. We can therefore clear the queue not empty flag held
    412   1.1    chris 	 * in r3.
    413   1.1    chris 	 */
    414   1.1    chris 
    415   1.1    chris 	teq	r5, r7
    416   1.1    chris 	biceq	r3, r3, r0
    417   1.1    chris 
    418   1.1    chris 	/* rem: r0 = bit mask of chosen queue (1 << r4) - NOT NEEDED AN MORE */
    419   1.1    chris 
    420   1.1    chris 	/* Fix the back pointer for the process now at the head of the queue. */
    421   1.1    chris 	ldr	r0, [r6, #(P_BACK)]
    422   1.1    chris 	str	r0, [r7, #(P_BACK)]
    423   1.1    chris 
    424   1.1    chris 	/* Update the RAM copy of the queue not empty flags word. */
    425  1.17  thorpej 	ldr	r7, .Lwhichqs
    426   1.1    chris 	str	r3, [r7]
    427   1.1    chris 
    428   1.1    chris 	/* rem: r1 = old proc */
    429   1.1    chris 	/* rem: r3 = whichqs - NOT NEEDED ANY MORE */
    430   1.1    chris 	/* rem: r4 = queue number - NOT NEEDED ANY MORE */
    431   1.1    chris 	/* rem: r6 = new process */
    432   1.1    chris 	/* rem: interrupts are disabled */
    433   1.1    chris 
    434   1.1    chris 	/* Clear the want_resched flag */
    435  1.17  thorpej 	ldr	r7, .Lwant_resched
    436   1.1    chris 	mov	r0, #0x00000000
    437   1.1    chris 	str	r0, [r7]
    438   1.1    chris 
    439   1.1    chris 	/*
    440   1.1    chris 	 * Clear the back pointer of the process we have removed from
    441   1.1    chris 	 * the head of the queue. The new process is isolated now.
    442   1.1    chris 	 */
    443   1.1    chris 	str	r0, [r6, #(P_BACK)]
    444   1.1    chris 
    445  1.19    bjh21 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    446   1.7    chris 	/*
    447   1.7    chris 	 * unlock the sched_lock, but leave interrupts off, for now.
    448   1.7    chris 	 */
    449   1.7    chris 	mov	r7, r1
    450   1.7    chris 	bl	_C_LABEL(sched_unlock_idle)
    451   1.7    chris 	mov	r1, r7
    452   1.7    chris #endif
    453   1.7    chris 
    454  1.19    bjh21 #ifdef MULTIPROCESSOR
    455  1.19    bjh21 	/* XXX use curcpu() */
    456  1.19    bjh21 	ldr	r0, .Lcpu_info_store
    457  1.19    bjh21 	str	r0, [r6, #(P_CPU)]
    458  1.19    bjh21 #else
    459   1.1    chris 	/* p->p_cpu initialized in fork1() for single-processor */
    460  1.19    bjh21 #endif
    461   1.1    chris 
    462   1.1    chris 	/* Process is now on a processor. */
    463   1.1    chris 	mov	r0, #SONPROC			/* p->p_stat = SONPROC */
    464   1.1    chris 	strb	r0, [r6, #(P_STAT)]
    465   1.1    chris 
    466   1.1    chris 	/* We have a new curproc now so make a note it */
    467  1.17  thorpej 	ldr	r7, .Lcurproc
    468   1.1    chris 	str	r6, [r7]
    469   1.1    chris 
    470   1.1    chris 	/* Hook in a new pcb */
    471  1.17  thorpej 	ldr	r7, .Lcurpcb
    472   1.1    chris 	ldr	r0, [r6, #(P_ADDR)]
    473   1.1    chris 	str	r0, [r7]
    474   1.1    chris 
    475   1.1    chris 	/* At this point we can allow IRQ's again. */
    476   1.1    chris 	IRQenable
    477   1.1    chris 
    478   1.1    chris 	/* rem: r1 = old proc */
    479   1.1    chris 	/* rem: r6 = new process */
    480   1.4    chris 	/* rem: interrupts are enabled */
    481   1.1    chris 
    482   1.1    chris 	/*
    483   1.1    chris 	 * If the new process is the same as the process that called
    484   1.1    chris 	 * cpu_switch() then we do not need to save and restore any
    485   1.1    chris 	 * contexts. This means we can make a quick exit.
    486   1.1    chris 	 * The test is simple if curproc on entry (now in r1) is the
    487   1.1    chris 	 * same as the proc removed from the queue we can jump to the exit.
    488   1.1    chris 	 */
    489   1.1    chris 	teq	r1, r6
    490  1.14   briggs 	beq	.Lswitch_return
    491   1.1    chris 
    492   1.9  thorpej 	/* Remember the old process in r0 */
    493   1.9  thorpej 	mov	r0, r1
    494   1.9  thorpej 
    495   1.1    chris 	/*
    496   1.1    chris 	 * If the curproc on entry to cpu_switch was zero then the
    497   1.1    chris 	 * process that called it was exiting. This means that we do
    498   1.1    chris 	 * not need to save the current context. Instead we can jump
    499   1.1    chris 	 * straight to restoring the context for the new process.
    500   1.1    chris 	 */
    501   1.9  thorpej 	teq	r0, #0x00000000
    502  1.14   briggs 	beq	.Lswitch_exited
    503   1.1    chris 
    504   1.9  thorpej 	/* rem: r0 = old proc */
    505   1.1    chris 	/* rem: r6 = new process */
    506   1.4    chris 	/* rem: interrupts are enabled */
    507   1.1    chris 
    508   1.1    chris 	/* Stage two : Save old context */
    509   1.1    chris 
    510   1.1    chris 	/* Get the user structure for the old process. */
    511   1.9  thorpej 	ldr	r1, [r0, #(P_ADDR)]
    512   1.1    chris 
    513   1.1    chris 	/* Save all the registers in the old process's pcb */
    514   1.1    chris 	add	r7, r1, #(PCB_R8)
    515   1.1    chris 	stmia	r7, {r8-r13}
    516   1.1    chris 
    517   1.1    chris 	/*
    518   1.1    chris 	 * This can be optimised... We know we want to go from SVC32
    519   1.1    chris 	 * mode to UND32 mode
    520   1.1    chris 	 */
    521  1.13  thorpej         mrs	r3, cpsr
    522   1.1    chris 	bic	r2, r3, #(PSR_MODE)
    523   1.1    chris 	orr	r2, r2, #(PSR_UND32_MODE | I32_bit)
    524  1.13  thorpej         msr	cpsr_c, r2
    525   1.1    chris 
    526   1.1    chris 	str	sp, [r1, #(PCB_UND_SP)]
    527   1.1    chris 
    528  1.13  thorpej         msr	cpsr_c, r3		/* Restore the old mode */
    529   1.1    chris 
    530   1.1    chris 	/* rem: r0 = old proc */
    531   1.4    chris 	/* rem: r1 = old pcb */
    532   1.1    chris 	/* rem: r6 = new process */
    533   1.4    chris 	/* rem: interrupts are enabled */
    534   1.1    chris 
    535   1.1    chris 	/* What else needs to be saved  Only FPA stuff when that is supported */
    536   1.1    chris 
    537   1.9  thorpej 	/* r1 now free! */
    538   1.9  thorpej 
    539   1.1    chris 	/* Third phase : restore saved context */
    540   1.1    chris 
    541   1.9  thorpej 	/* rem: r0 = old proc */
    542   1.9  thorpej 	/* rem: r6 = new process */
    543   1.9  thorpej 	/* rem: interrupts are enabled */
    544   1.9  thorpej 
    545   1.9  thorpej 	/*
    546   1.9  thorpej 	 * Don't allow user space access between the purge and the switch.
    547   1.9  thorpej 	 */
    548  1.17  thorpej 	ldr	r3, .Lblock_userspace_access
    549   1.9  thorpej 	mov	r1, #0x00000001
    550   1.9  thorpej 	mov	r2, #0x00000000
    551   1.9  thorpej 	str	r1, [r3]
    552   1.1    chris 
    553   1.1    chris 	stmfd	sp!, {r0-r3}
    554  1.17  thorpej 	ldr	r1, .Lcpufuncs
    555  1.14   briggs 	add	lr, pc, #.Lcs_cache_purged - . - 8
    556   1.9  thorpej 	ldr	pc, [r1, #CF_IDCACHE_WBINV_ALL]
    557   1.1    chris 
    558  1.14   briggs .Lcs_cache_purged:
    559   1.1    chris 	ldmfd	sp!, {r0-r3}
    560   1.1    chris 
    561  1.14   briggs .Lcs_cache_purge_skipped:
    562   1.1    chris 	/* At this point we need to kill IRQ's again. */
    563   1.1    chris 	IRQdisable
    564   1.1    chris 
    565   1.9  thorpej 	/*
    566   1.9  thorpej 	 * Interrupts are disabled so we can allow user space accesses again
    567   1.1    chris 	 * as none will occur until interrupts are re-enabled after the
    568   1.1    chris 	 * switch.
    569   1.1    chris 	 */
    570   1.1    chris 	str	r2, [r3]
    571   1.1    chris 
    572   1.1    chris 	/* Get the user structure for the new process in r1 */
    573   1.1    chris 	ldr	r1, [r6, #(P_ADDR)]
    574   1.1    chris 
    575   1.1    chris 	/* Get the pagedir physical address for the process. */
    576   1.1    chris 	ldr	r0, [r1, #(PCB_PAGEDIR)]
    577   1.1    chris 
    578   1.1    chris 	/* Switch the memory to the new process */
    579  1.17  thorpej 	ldr	r3, .Lcpufuncs
    580  1.14   briggs 	add	lr, pc, #.Lcs_context_switched - . - 8
    581   1.1    chris 	ldr	pc, [r3, #CF_CONTEXT_SWITCH]
    582   1.1    chris 
    583  1.14   briggs .Lcs_context_switched:
    584   1.1    chris 	/*
    585   1.1    chris 	 * This can be optimised... We know we want to go from SVC32
    586   1.1    chris 	 * mode to UND32 mode
    587   1.1    chris 	 */
    588  1.13  thorpej         mrs	r3, cpsr
    589   1.1    chris 	bic	r2, r3, #(PSR_MODE)
    590   1.1    chris 	orr	r2, r2, #(PSR_UND32_MODE)
    591  1.13  thorpej         msr	cpsr_c, r2
    592   1.1    chris 
    593   1.1    chris 	ldr	sp, [r1, #(PCB_UND_SP)]
    594   1.1    chris 
    595  1.13  thorpej         msr	cpsr_c, r3		/* Restore the old mode */
    596   1.1    chris 
    597   1.1    chris 	/* Restore all the save registers */
    598   1.1    chris 	add	r7, r1, #PCB_R8
    599   1.1    chris 	ldmia	r7, {r8-r13}
    600   1.1    chris 
    601  1.18  thorpej 	mov	r7, r1			/* preserve PCB pointer */
    602  1.18  thorpej 
    603   1.1    chris #ifdef ARMFPE
    604   1.1    chris 	add	r0, r1, #(USER_SIZE) & 0x00ff
    605   1.1    chris 	add	r0, r0, #(USER_SIZE) & 0xff00
    606   1.1    chris 	bl	_C_LABEL(arm_fpe_core_changecontext)
    607   1.1    chris #endif
    608   1.1    chris 
    609   1.1    chris 	/* We can enable interrupts again */
    610   1.1    chris 	IRQenable
    611   1.1    chris 
    612  1.18  thorpej 	/* rem: r6 = new proc */
    613  1.18  thorpej 	/* rem: r7 = new PCB */
    614  1.18  thorpej 
    615  1.18  thorpej 	/*
    616  1.18  thorpej 	 * Check for restartable atomic sequences (RAS).
    617  1.18  thorpej 	 */
    618  1.18  thorpej 
    619  1.18  thorpej 	ldr	r2, [r6, #(P_NRAS)]
    620  1.18  thorpej 	ldr	r4, [r7, #(PCB_TF)]	/* r4 = trapframe (used below) */
    621  1.18  thorpej 	teq	r2, #0			/* p->p_nras == 0? */
    622  1.18  thorpej 	bne	.Lswitch_do_ras		/* no, check for one */
    623  1.18  thorpej 
    624  1.14   briggs .Lswitch_return:
    625   1.1    chris 
    626   1.1    chris 	/* Get the spl level from the stack and update the current spl level */
    627   1.1    chris 	ldr	r0, [sp], #0x0004
    628   1.1    chris 	bl	_C_LABEL(splx)
    629   1.1    chris 
    630   1.1    chris 	/* cpu_switch returns the proc it switched to. */
    631   1.1    chris 	mov	r0, r6
    632   1.1    chris 
    633   1.1    chris 	/*
    634   1.1    chris 	 * Pull the registers that got pushed when either savectx() or
    635   1.1    chris 	 * cpu_switch() was called and return.
    636   1.1    chris 	 */
    637   1.1    chris 	ldmfd	sp!, {r4-r7, pc}
    638  1.18  thorpej 
    639  1.18  thorpej .Lswitch_do_ras:
    640  1.18  thorpej 	ldr	r1, [r4, #(TF_PC)]	/* second ras_lookup() arg */
    641  1.18  thorpej 	mov	r0, r6			/* first ras_lookup() arg */
    642  1.18  thorpej 	bl	_C_LABEL(ras_lookup)
    643  1.18  thorpej 	cmn	r0, #1			/* -1 means "not in a RAS" */
    644  1.18  thorpej 	strne	r0, [r4, #(TF_PC)]
    645  1.18  thorpej 	b	.Lswitch_return
    646   1.1    chris 
    647  1.14   briggs .Lswitch_exited:
    648   1.9  thorpej 	/*
    649   1.9  thorpej 	 * We skip the cache purge because switch_exit() already did
    650   1.9  thorpej 	 * it.  Load up registers the way Lcs_cache_purge_skipped
    651   1.9  thorpej 	 * expects.  Userspace access already blocked in switch_exit().
    652   1.9  thorpej 	 */
    653  1.17  thorpej 	ldr	r3, .Lblock_userspace_access
    654   1.9  thorpej 	mov	r2, #0x00000000
    655  1.14   briggs 	b	.Lcs_cache_purge_skipped
    656   1.9  thorpej 
    657   1.7    chris /*
    658   1.8  thorpej  * void switch_exit(struct proc *p, struct proc *p0);
    659   1.7    chris  * Switch to proc0's saved context and deallocate the address space and kernel
    660   1.7    chris  * stack for p.  Then jump into cpu_switch(), as if we were in proc0 all along.
    661   1.7    chris  */
    662   1.1    chris 
    663   1.8  thorpej /* LINTSTUB: Func: void switch_exit(struct proc *p, struct proc *p0) */
    664   1.1    chris ENTRY(switch_exit)
    665   1.1    chris 	/*
    666   1.1    chris 	 * r0 = proc
    667   1.1    chris 	 * r1 = proc0
    668   1.1    chris 	 */
    669   1.1    chris 
    670   1.1    chris 	mov	r3, r0
    671   1.1    chris 
    672   1.1    chris 	/* In case we fault */
    673  1.17  thorpej 	ldr	r0, .Lcurproc
    674   1.1    chris 	mov	r2, #0x00000000
    675   1.1    chris 	str	r2, [r0]
    676   1.1    chris 
    677  1.17  thorpej /*	ldr	r0, .Lcurpcb
    678   1.1    chris 	str	r2, [r0]*/
    679   1.9  thorpej 
    680   1.9  thorpej 	/*
    681   1.9  thorpej 	 * Don't allow user space access between the purge and the switch.
    682   1.9  thorpej 	 */
    683  1.17  thorpej 	ldr	r0, .Lblock_userspace_access
    684   1.9  thorpej 	mov	r2, #0x00000001
    685   1.9  thorpej 	str	r2, [r0]
    686   1.1    chris 
    687   1.1    chris 	/* Switch to proc0 context */
    688   1.1    chris 
    689   1.1    chris 	stmfd	sp!, {r0-r3}
    690   1.1    chris 
    691  1.17  thorpej 	ldr	r0, .Lcpufuncs
    692  1.14   briggs 	add	lr, pc, #.Lse_cache_purged - . - 8
    693   1.6  thorpej 	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
    694   1.1    chris 
    695  1.14   briggs .Lse_cache_purged:
    696   1.1    chris 	ldmfd	sp!, {r0-r3}
    697   1.1    chris 
    698   1.1    chris 	IRQdisable
    699   1.1    chris 
    700   1.1    chris 	ldr	r2, [r1, #(P_ADDR)]
    701   1.1    chris 	ldr	r0, [r2, #(PCB_PAGEDIR)]
    702   1.1    chris 
    703   1.1    chris 	/* Switch the memory to the new process */
    704  1.17  thorpej 	ldr	r4, .Lcpufuncs
    705  1.14   briggs 	add	lr, pc, #.Lse_context_switched - . - 8
    706   1.1    chris 	ldr	pc, [r4, #CF_CONTEXT_SWITCH]
    707   1.1    chris 
    708  1.14   briggs .Lse_context_switched:
    709   1.1    chris 	/* Restore all the save registers */
    710   1.1    chris 	add	r7, r2, #PCB_R8
    711   1.1    chris 	ldmia	r7, {r8-r13}
    712   1.1    chris 
    713   1.1    chris 	/* This is not really needed ! */
    714   1.1    chris 	/* Yes it is for the su and fu routines */
    715  1.17  thorpej 	ldr	r0, .Lcurpcb
    716   1.1    chris 	str	r2, [r0]
    717   1.1    chris 
    718   1.1    chris 	IRQenable
    719   1.1    chris 
    720   1.1    chris /*	str	r3, [sp, #-0x0004]!*/
    721   1.1    chris 
    722   1.1    chris 	/*
    723   1.1    chris 	 * Schedule the vmspace and stack to be freed.
    724   1.1    chris 	 */
    725   1.1    chris 	mov	r0, r3			/* exit2(p) */
    726   1.1    chris 	bl	_C_LABEL(exit2)
    727   1.1    chris 
    728   1.1    chris 	/* Paranoia */
    729  1.17  thorpej 	ldr	r1, .Lcurproc
    730   1.1    chris 	mov	r0, #0x00000000
    731   1.1    chris 	str	r0, [r1]
    732   1.1    chris 
    733  1.17  thorpej 	ldr	r7, .Lwhichqs		/* r7 = &whichqs */
    734   1.8  thorpej 	mov	r5, #0x00000000		/* r5 = old proc = NULL */
    735  1.14   briggs 	b	.Lswitch_search
    736   1.1    chris 
    737   1.7    chris /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
    738   1.1    chris ENTRY(savectx)
    739   1.1    chris 	/*
    740   1.1    chris 	 * r0 = pcb
    741   1.1    chris 	 */
    742   1.1    chris 
    743   1.1    chris 	/* Push registers.*/
    744   1.1    chris 	stmfd	sp!, {r4-r7, lr}
    745   1.1    chris 
    746   1.1    chris 	/* Store all the registers in the process's pcb */
    747   1.1    chris 	add	r2, r0, #(PCB_R8)
    748   1.1    chris 	stmia	r2, {r8-r13}
    749   1.1    chris 
    750   1.1    chris 	/* Pull the regs of the stack */
    751   1.1    chris 	ldmfd	sp!, {r4-r7, pc}
    752   1.1    chris 
    753   1.1    chris ENTRY(proc_trampoline)
    754  1.19    bjh21 #ifdef MULTIPROCESSOR
    755  1.19    bjh21 	bl	_C_LABEL(proc_trampoline_mp)
    756  1.19    bjh21 #endif
    757  1.14   briggs 	add	lr, pc, #(.Ltrampoline_return - . - 8)
    758   1.1    chris 	mov	r0, r5
    759   1.1    chris 	mov	r1, sp
    760   1.1    chris 	mov	pc, r4
    761   1.1    chris 
    762  1.14   briggs .Ltrampoline_return:
    763   1.1    chris 	/* Kill irq's */
    764  1.13  thorpej         mrs     r0, cpsr
    765   1.1    chris         orr     r0, r0, #(I32_bit)
    766  1.13  thorpej         msr     cpsr_c, r0
    767   1.1    chris 
    768   1.1    chris 	PULLFRAME
    769   1.1    chris 
    770   1.1    chris 	movs	pc, lr			/* Exit */
    771   1.1    chris 
    772  1.17  thorpej 	.type .Lcpu_switch_ffs_table, _ASM_TYPE_OBJECT;
    773  1.17  thorpej .Lcpu_switch_ffs_table:
    774   1.1    chris /* same as ffs table but all nums are -1 from that */
    775   1.1    chris /*               0   1   2   3   4   5   6   7           */
    776   1.1    chris 	.byte	 0,  0,  1, 12,  2,  6,  0, 13  /*  0- 7 */
    777   1.1    chris 	.byte	 3,  0,  7,  0,  0,  0,  0, 14  /*  8-15 */
    778   1.1    chris 	.byte	10,  4,  0,  0,  8,  0,  0, 25  /* 16-23 */
    779   1.1    chris 	.byte	 0,  0,  0,  0,  0, 21, 27, 15  /* 24-31 */
    780   1.1    chris 	.byte	31, 11,  5,  0,  0,  0,  0,  0	/* 32-39 */
    781   1.1    chris 	.byte	 9,  0,  0, 24,  0,  0, 20, 26  /* 40-47 */
    782   1.1    chris 	.byte	30,  0,  0,  0,  0, 23,  0, 19  /* 48-55 */
    783   1.1    chris 	.byte   29,  0, 22, 18, 28, 17, 16,  0  /* 56-63 */
    784   1.1    chris 
    785   1.1    chris /* End of cpuswitch.S */
    786