Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.99
      1 /*	$NetBSD: cpuswitch.S,v 1.99 2020/07/03 06:35:05 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 /*
     38  * Copyright (c) 1994-1998 Mark Brinicombe.
     39  * Copyright (c) 1994 Brini.
     40  * All rights reserved.
     41  *
     42  * This code is derived from software written for Brini by Mark Brinicombe
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Brini.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * RiscBSD kernel project
     72  *
     73  * cpuswitch.S
     74  *
     75  * cpu switching functions
     76  *
     77  * Created      : 15/10/94
     78  */
     79 
     80 #include "opt_armfpe.h"
     81 #include "opt_cpuoptions.h"
     82 #include "opt_lockdebug.h"
     83 #include "opt_multiprocessor.h"
     84 
     85 #include "assym.h"
     86 #include <arm/asm.h>
     87 #include <arm/locore.h>
     88 
     89 	RCSID("$NetBSD: cpuswitch.S,v 1.99 2020/07/03 06:35:05 skrll Exp $")
     90 
     91 /* LINTSTUB: include <sys/param.h> */
     92 
     93 #ifdef FPU_VFP
     94 	.fpu vfpv2
     95 #endif
     96 
     97 #undef IRQdisable
     98 #undef IRQenable
     99 
    100 /*
    101  * New experimental definitions of IRQdisable and IRQenable
    102  * These keep FIQ's enabled since FIQ's are special.
    103  */
    104 
    105 #ifdef _ARM_ARCH_6
    106 #define	IRQdisable	cpsid	i
    107 #define	IRQenable	cpsie	i
    108 #else
    109 #define IRQdisable \
    110 	mrs	r14, cpsr ; \
    111 	orr	r14, r14, #(I32_bit) ; \
    112 	msr	cpsr_c, r14
    113 
    114 #define IRQenable \
    115 	mrs	r14, cpsr ; \
    116 	bic	r14, r14, #(I32_bit) ; \
    117 	msr	cpsr_c, r14
    118 
    119 #endif
    120 
    121 	.text
    122 
    123 /*
    124  * struct lwp *
    125  * cpu_switchto(struct lwp *current, struct lwp *next)
    126  *
    127  * Switch to the specified next LWP
    128  * Arguments:
    129  *
    130  *	r0	'struct lwp *' of the current LWP
    131  *	r1	'struct lwp *' of the LWP to switch to
    132  *	r2	returning
    133  */
    134 ENTRY(cpu_switchto)
    135 	mov	ip, sp
    136 	push	{r4-r7, ip, lr}
    137 
    138 	/* move lwps into caller saved registers */
    139 	mov	r6, r1
    140 	mov	r4, r0
    141 
    142 #ifdef TPIDRPRW_IS_CURCPU
    143 	GET_CURCPU(r5)
    144 #else
    145 	ldr	r5, [r6, #L_CPU]		/* get cpu from new lwp */
    146 #endif
    147 
    148 	/* rem: r4 = old lwp */
    149 	/* rem: r5 = curcpu() */
    150 	/* rem: r6 = new lwp */
    151 	/* rem: interrupts are enabled */
    152 
    153 	/* Save old context */
    154 
    155 	/* Get the user structure for the old lwp. */
    156 	ldr	r7, [r4, #(L_PCB)]
    157 
    158 	/* Save all the registers in the old lwp's pcb */
    159 #if defined(_ARM_ARCH_DWORD_OK)
    160 	strd	r8, r9, [r7, #(PCB_R8)]
    161 	strd	r10, r11, [r7, #(PCB_R10)]
    162 	strd	r12, r13, [r7, #(PCB_R12)]
    163 #else
    164 	add	r0, r7, #(PCB_R8)
    165 	stmia	r0, {r8-r13}
    166 #endif
    167 
    168 #ifdef _ARM_ARCH_6
    169 	/*
    170 	 * Save user read/write thread/process id register
    171 	 */
    172 	mrc	p15, 0, r0, c13, c0, 2
    173 	str	r0, [r7, #(PCB_USER_PID_RW)]
    174 #endif
    175 	/*
    176 	 * NOTE: We can now use r8-r13 until it is time to restore
    177 	 * them for the new process.
    178 	 */
    179 
    180 	/* Restore saved context */
    181 
    182 	/* rem: r4 = old lwp */
    183 	/* rem: r5 = curcpu() */
    184 	/* rem: r6 = new lwp */
    185 
    186 	IRQdisable
    187 #if defined(TPIDRPRW_IS_CURLWP)
    188 	mcr	p15, 0, r6, c13, c0, 4		/* set current lwp */
    189 #endif
    190 
    191 	/* We have a new curlwp now so make a note of it */
    192 	str	r6, [r5, #(CI_CURLWP)]
    193 	/* Get the new pcb */
    194 	ldr	r7, [r6, #(L_PCB)]
    195 
    196 	/* make sure we are using the new lwp's stack */
    197 	ldr	sp, [r7, #(PCB_KSP)]
    198 
    199 	/* At this point we can allow IRQ's again. */
    200 	IRQenable
    201 
    202 	/* rem: r4 = old lwp */
    203 	/* rem: r5 = curcpu() */
    204 	/* rem: r6 = new lwp */
    205 	/* rem: r7 = new pcb */
    206 	/* rem: interrupts are enabled */
    207 
    208 	/*
    209 	 * If we are switching to a system lwp, don't bother restoring
    210 	 * thread or vfp registers and skip the ras check.
    211 	 */
    212 	ldr	r0, [r6, #(L_FLAG)]
    213 	tst	r0, #(LW_SYSTEM)
    214 	bne	.Lswitch_do_restore
    215 
    216 #ifdef _ARM_ARCH_6
    217 	/*
    218 	 * Restore user thread/process id registers
    219 	 */
    220 	ldr	r0, [r7, #(PCB_USER_PID_RW)]
    221 	mcr	p15, 0, r0, c13, c0, 2
    222 	ldr	r0, [r6, #(L_PRIVATE)]
    223 	mcr	p15, 0, r0, c13, c0, 3
    224 #endif
    225 
    226 #ifdef FPU_VFP
    227 	/*
    228 	 * If we have a VFP, we need to load FPEXC.
    229 	 */
    230 	ldr	r0, [r5, #(CI_VFP_ID)]
    231 	cmp	r0, #0
    232 	ldrne	r0, [r7, #(PCB_VFP_FPEXC)]
    233 	vmsrne	fpexc, r0
    234 #endif
    235 
    236 	/*
    237 	 * Check for restartable atomic sequences (RAS).
    238 	 */
    239 
    240 	ldr	r0, [r6, #(L_PROC)]	/* fetch the proc for ras_lookup */
    241 	ldr	r2, [r0, #(P_RASLIST)]
    242 	cmp	r2, #0			/* p->p_nras == 0? */
    243 	beq	.Lswitch_do_restore
    244 
    245 	/* we can use r8 since we haven't restored saved registers yet. */
    246 	ldr	r8, [r6, #(L_MD_TF)]	/* r1 = trapframe (used below) */
    247 	ldr	r1, [r8, #(TF_PC)]	/* second ras_lookup() arg */
    248 	bl	_C_LABEL(ras_lookup)
    249 	cmn	r0, #1			/* -1 means "not in a RAS" */
    250 	strne	r0, [r8, #(TF_PC)]
    251 
    252 	/* rem: r4 = old lwp */
    253 	/* rem: r5 = curcpu() */
    254 	/* rem: r6 = new lwp */
    255 	/* rem: r7 = new pcb */
    256 
    257 .Lswitch_do_restore:
    258 	/* Restore all the saved registers */
    259 #ifdef __XSCALE__
    260 	ldr	r8, [r7, #(PCB_R8)]
    261 	ldr	r9, [r7, #(PCB_R9)]
    262 	ldr	r10, [r7, #(PCB_R10)]
    263 	ldr	r11, [r7, #(PCB_R11)]
    264 	ldr	r12, [r7, #(PCB_R12)]
    265 #elif defined(_ARM_ARCH_DWORD_OK)
    266 	ldrd	r8, r9, [r7, #(PCB_R8)]
    267 	ldrd	r10, r11, [r7, #(PCB_R10)]
    268 	ldr	r12, [r7, #(PCB_R12)]
    269 #else
    270 	add	r0, r7, #PCB_R8
    271 	ldmia	r0, {r8-r12}
    272 #endif
    273 
    274 	/* Record the old lwp for pmap_activate()'s benefit */
    275 #ifndef ARM_MMU_EXTENDED
    276 	str	r4, [r5, #CI_LASTLWP]
    277 #endif
    278 
    279 	/* cpu_switchto returns the old lwp */
    280 	mov	r0, r4
    281 	/* lwp_trampoline expects new lwp as its second argument */
    282 	mov	r1, r6
    283 
    284 #ifdef _ARM_ARCH_7
    285 	clrex				/* cause any subsequent STREX* to fail */
    286 #endif
    287 
    288 	/*
    289 	 * Pull the registers that got pushed when cpu_switchto() was called,
    290 	 * and return.
    291 	 */
    292 	pop	{r4-r7, ip, pc}
    293 
    294 END(cpu_switchto)
    295 
    296 ENTRY_NP(lwp_trampoline)
    297 	/*
    298 	 * cpu_switchto gives us:
    299 	 *	arg0(r0) = old lwp
    300 	 *	arg1(r1) = new lwp
    301 	 * setup by cpu_lwp_fork:
    302 	 *	r4 = func to call
    303 	 *	r5 = arg to func
    304 	 *	r6 = <unused>
    305 	 *	r7 = spsr mode
    306 	 */
    307 	bl	_C_LABEL(lwp_startup)
    308 
    309 	mov	fp, #0			/* top stack frame */
    310 	mov	r0, r5
    311 	mov	r1, sp
    312 #ifdef _ARM_ARCH_5
    313 	blx	r4
    314 #else
    315 	mov	lr, pc
    316 	mov	pc, r4
    317 #endif
    318 
    319 	GET_CPSR(r0)
    320 	CPSID_I(r0, r0)			/* Kill irq's */
    321 
    322 	GET_CURCPU(r4)			/* for DO_AST */
    323 	DO_AST_AND_RESTORE_ALIGNMENT_FAULTS
    324 	PULLFRAME
    325 
    326 	movs	pc, lr			/* Exit */
    327 END(lwp_trampoline)
    328 
    329 AST_ALIGNMENT_FAULT_LOCALS
    330 
    331 #ifdef __HAVE_FAST_SOFTINTS
    332 /*
    333  *	Called at IPL_HIGH
    334  *	r0 = new lwp
    335  *	r1 = ipl for softint_dispatch
    336  */
    337 ENTRY_NP(softint_switch)
    338 	push	{r4, r6, r7, lr}
    339 
    340 	ldr	r7, [r0, #L_CPU]	/* get curcpu */
    341 #if defined(TPIDRPRW_IS_CURLWP)
    342 	mrc	p15, 0, r4, c13, c0, 4	/* get old lwp */
    343 #else
    344 	ldr	r4, [r7, #(CI_CURLWP)]	/* get old lwp */
    345 #endif
    346 	mrs	r6, cpsr		/* we need to save this */
    347 
    348 	/*
    349 	 * If the soft lwp blocks, it needs to return to softint_tramp
    350 	 */
    351 	mov	r2, sp			/* think ip */
    352 	adr	r3, softint_tramp	/* think lr */
    353 	push	{r2-r3}
    354 	push	{r4-r7}
    355 
    356 	mov	r5, r0			/* save new lwp */
    357 
    358 	ldr	r2, [r4, #(L_PCB)]	/* get old lwp's pcb */
    359 
    360 	/* Save all the registers into the old lwp's pcb */
    361 #if defined(__XSCALE__) || defined(_ARM_ARCH_6)
    362 	strd	r8, r9, [r2, #(PCB_R8)]
    363 	strd	r10, r11, [r2, #(PCB_R10)]
    364 	strd	r12, r13, [r2, #(PCB_R12)]
    365 #else
    366 	add	r3, r2, #(PCB_R8)
    367 	stmia	r3, {r8-r13}
    368 #endif
    369 
    370 #ifdef _ARM_ARCH_6
    371 	/*
    372 	 * Save user read/write thread/process id register in case it was
    373 	 * set in userland.
    374 	 */
    375 	mrc	p15, 0, r0, c13, c0, 2
    376 	str	r0, [r2, #(PCB_USER_PID_RW)]
    377 #endif
    378 
    379 	/* this is an invariant so load before disabling intrs */
    380 	ldr	r2, [r5, #(L_PCB)]	/* get new lwp's pcb */
    381 
    382 	IRQdisable
    383 	/*
    384 	 * We're switching to a bound LWP so its l_cpu is already correct.
    385 	 */
    386 #if defined(TPIDRPRW_IS_CURLWP)
    387 	mcr	p15, 0, r5, c13, c0, 4	/* save new lwp */
    388 #endif
    389 	str	r5, [r7, #(CI_CURLWP)]	/* save new lwp */
    390 
    391 	/*
    392 	 * Normally, we'd get {r8-r13} but since this is a softint lwp
    393 	 * its existing state doesn't matter.  We start the stack just
    394 	 * below the trapframe.
    395 	 */
    396 	ldr	sp, [r5, #(L_MD_TF)]	/* get new lwp's stack ptr */
    397 
    398 	/* At this point we can allow IRQ's again. */
    399 	IRQenable
    400 					/* r1 still has ipl */
    401 	mov	r0, r4			/* r0 has pinned (old) lwp */
    402 	bl	_C_LABEL(softint_dispatch)
    403 	/*
    404 	 * If we've returned, we need to change everything back and return.
    405 	 */
    406 	ldr	r2, [r4, #(L_PCB)]	/* get pinned lwp's pcb */
    407 
    408 	/*
    409 	 * We don't need to restore all the registers since another lwp was
    410 	 * never executed.  But we do need the SP from the formerly pinned lwp.
    411 	 */
    412 
    413 	IRQdisable
    414 #if defined(TPIDRPRW_IS_CURLWP)
    415 	mcr	p15, 0, r4, c13, c0, 4	/* restore pinned lwp */
    416 #endif
    417 	str	r4, [r7, #(CI_CURLWP)]	/* restore pinned lwp */
    418 	ldr	sp, [r2, #(PCB_KSP)]	/* now running on the old stack. */
    419 
    420 	/* At this point we can allow IRQ's again. */
    421 	msr	cpsr_c, r6
    422 
    423 	/*
    424 	 * Grab the registers that got pushed at the start and return.
    425 	 */
    426 	pop	{r4-r7, ip, lr}		/* eat switch frame */
    427 	pop	{r4, r6, r7, pc}	/* pop stack and return */
    428 
    429 END(softint_switch)
    430 
    431 /*
    432  * r0 = previous LWP (the soft lwp)
    433  * r4 = original LWP (the current lwp)
    434  * r6 = original CPSR
    435  * r7 = curcpu()
    436  */
    437 ENTRY_NP(softint_tramp)
    438 	ldr	r3, [r7, #(CI_MTX_COUNT)]	/* readjust after mi_switch */
    439 	add	r3, r3, #1
    440 	str	r3, [r7, #(CI_MTX_COUNT)]
    441 
    442 	msr	cpsr_c, r6			/* restore interrupts */
    443 	pop	{r4, r6, r7, pc}		/* pop stack and return */
    444 END(softint_tramp)
    445 #endif /* __HAVE_FAST_SOFTINTS */
    446