Home | History | Annotate | Line # | Download | only in arm32
cpuswitch.S revision 1.95.2.1
      1 /*	$NetBSD: cpuswitch.S,v 1.95.2.1 2020/01/17 21:47:23 ad 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_arm32_pmap.h"
     82 #include "opt_multiprocessor.h"
     83 #include "opt_cpuoptions.h"
     84 #include "opt_lockdebug.h"
     85 
     86 #include "assym.h"
     87 #include <arm/asm.h>
     88 #include <arm/locore.h>
     89 
     90 	RCSID("$NetBSD: cpuswitch.S,v 1.95.2.1 2020/01/17 21:47:23 ad Exp $")
     91 
     92 /* LINTSTUB: include <sys/param.h> */
     93 
     94 #ifdef FPU_VFP
     95 	.fpu vfpv2
     96 #endif
     97 
     98 #undef IRQdisable
     99 #undef IRQenable
    100 
    101 /*
    102  * New experimental definitions of IRQdisable and IRQenable
    103  * These keep FIQ's enabled since FIQ's are special.
    104  */
    105 
    106 #ifdef _ARM_ARCH_6
    107 #define	IRQdisable	cpsid	i
    108 #define	IRQenable	cpsie	i
    109 #else
    110 #define IRQdisable \
    111 	mrs	r14, cpsr ; \
    112 	orr	r14, r14, #(I32_bit) ; \
    113 	msr	cpsr_c, r14
    114 
    115 #define IRQenable \
    116 	mrs	r14, cpsr ; \
    117 	bic	r14, r14, #(I32_bit) ; \
    118 	msr	cpsr_c, r14
    119 
    120 #endif
    121 
    122 	.text
    123 
    124 /*
    125  * struct lwp *
    126  * cpu_switchto(struct lwp *current, struct lwp *next)
    127  *
    128  * Switch to the specified next LWP
    129  * Arguments:
    130  *
    131  *	r0	'struct lwp *' of the current LWP
    132  *	r1	'struct lwp *' of the LWP to switch to
    133  *	r2	returning
    134  */
    135 ENTRY(cpu_switchto)
    136 	mov	ip, sp
    137 	push	{r4-r7, ip, lr}
    138 
    139 	/* move lwps into caller saved registers */
    140 	mov	r6, r1
    141 	mov	r4, r0
    142 
    143 #ifdef TPIDRPRW_IS_CURCPU
    144 	GET_CURCPU(r5)
    145 #else
    146 	ldr	r5, [r6, #L_CPU]		/* get cpu from new lwp */
    147 #endif
    148 
    149 	/* rem: r4 = old lwp */
    150 	/* rem: r5 = curcpu() */
    151 	/* rem: r6 = new lwp */
    152 	/* rem: interrupts are enabled */
    153 
    154 	/* Save old context */
    155 
    156 	/* Get the user structure for the old lwp. */
    157 	ldr	r7, [r4, #(L_PCB)]
    158 
    159 	/* Save all the registers in the old lwp's pcb */
    160 #if defined(_ARM_ARCH_DWORD_OK)
    161 	strd	r8, r9, [r7, #(PCB_R8)]
    162 	strd	r10, r11, [r7, #(PCB_R10)]
    163 	strd	r12, r13, [r7, #(PCB_R12)]
    164 #else
    165 	add	r0, r7, #(PCB_R8)
    166 	stmia	r0, {r8-r13}
    167 #endif
    168 
    169 #ifdef _ARM_ARCH_6
    170 	/*
    171 	 * Save user read/write thread/process id register
    172 	 */
    173 	mrc	p15, 0, r0, c13, c0, 2
    174 	str	r0, [r7, #(PCB_USER_PID_RW)]
    175 #endif
    176 	/*
    177 	 * NOTE: We can now use r8-r13 until it is time to restore
    178 	 * them for the new process.
    179 	 */
    180 
    181 	/* Restore saved context */
    182 
    183 	/* rem: r4 = old lwp */
    184 	/* rem: r5 = curcpu() */
    185 	/* rem: r6 = new lwp */
    186 
    187 	IRQdisable
    188 #if defined(TPIDRPRW_IS_CURLWP)
    189 	mcr	p15, 0, r6, c13, c0, 4		/* set current lwp */
    190 #endif
    191 
    192 	/* We have a new curlwp now so make a note of it */
    193 	str	r6, [r5, #(CI_CURLWP)]
    194 	/* Get the new pcb */
    195 	ldr	r7, [r6, #(L_PCB)]
    196 
    197 	/* make sure we are using the new lwp's stack */
    198 	ldr	sp, [r7, #(PCB_KSP)]
    199 
    200 	/* At this point we can allow IRQ's again. */
    201 	IRQenable
    202 
    203 	/* rem: r4 = old lwp */
    204 	/* rem: r5 = curcpu() */
    205 	/* rem: r6 = new lwp */
    206 	/* rem: r7 = new pcb */
    207 	/* rem: interrupts are enabled */
    208 
    209 	/*
    210 	 * If we are switching to a system lwp, don't bother restoring
    211 	 * thread or vfp registers and skip the ras check.
    212 	 */
    213 	ldr	r0, [r6, #(L_FLAG)]
    214 	tst	r0, #(LW_SYSTEM)
    215 	bne	.Lswitch_do_restore
    216 
    217 #ifdef _ARM_ARCH_6
    218 	/*
    219 	 * Restore user thread/process id registers
    220 	 */
    221 	ldr	r0, [r7, #(PCB_USER_PID_RW)]
    222 	mcr	p15, 0, r0, c13, c0, 2
    223 	ldr	r0, [r6, #(L_PRIVATE)]
    224 	mcr	p15, 0, r0, c13, c0, 3
    225 #endif
    226 
    227 #ifdef FPU_VFP
    228 	/*
    229 	 * If we have a VFP, we need to load FPEXC.
    230 	 */
    231 	ldr	r0, [r5, #(CI_VFP_ID)]
    232 	cmp	r0, #0
    233 	ldrne	r0, [r7, #(PCB_VFP_FPEXC)]
    234 	vmsrne	fpexc, r0
    235 #endif
    236 
    237 	/*
    238 	 * Check for restartable atomic sequences (RAS).
    239 	 */
    240 
    241 	ldr	r0, [r6, #(L_PROC)]	/* fetch the proc for ras_lookup */
    242 	ldr	r2, [r0, #(P_RASLIST)]
    243 	cmp	r2, #0			/* p->p_nras == 0? */
    244 	beq	.Lswitch_do_restore
    245 
    246 	/* we can use r8 since we haven't restored saved registers yet. */
    247 	ldr	r8, [r6, #(L_MD_TF)]	/* r1 = trapframe (used below) */
    248 	ldr	r1, [r8, #(TF_PC)]	/* second ras_lookup() arg */
    249 	bl	_C_LABEL(ras_lookup)
    250 	cmn	r0, #1			/* -1 means "not in a RAS" */
    251 	strne	r0, [r8, #(TF_PC)]
    252 
    253 	/* rem: r4 = old lwp */
    254 	/* rem: r5 = curcpu() */
    255 	/* rem: r6 = new lwp */
    256 	/* rem: r7 = new pcb */
    257 
    258 .Lswitch_do_restore:
    259 	/* Restore all the saved registers */
    260 #ifdef __XSCALE__
    261 	ldr	r8, [r7, #(PCB_R8)]
    262 	ldr	r9, [r7, #(PCB_R9)]
    263 	ldr	r10, [r7, #(PCB_R10)]
    264 	ldr	r11, [r7, #(PCB_R11)]
    265 	ldr	r12, [r7, #(PCB_R12)]
    266 #elif defined(_ARM_ARCH_DWORD_OK)
    267 	ldrd	r8, r9, [r7, #(PCB_R8)]
    268 	ldrd	r10, r11, [r7, #(PCB_R10)]
    269 	ldr	r12, [r7, #(PCB_R12)]
    270 #else
    271 	add	r0, r7, #PCB_R8
    272 	ldmia	r0, {r8-r12}
    273 #endif
    274 
    275 	/* Record the old lwp for pmap_activate()'s benefit */
    276 #ifndef ARM_MMU_EXTENDED
    277 	str	r4, [r5, #CI_LASTLWP]
    278 #endif
    279 
    280 	/* cpu_switchto returns the old lwp */
    281 	mov	r0, r4
    282 	/* lwp_trampoline expects new lwp as its second argument */
    283 	mov	r1, r6
    284 
    285 #ifdef _ARM_ARCH_7
    286 	clrex				/* cause any subsequent STREX* to fail */
    287 #endif
    288 
    289 	/*
    290 	 * Pull the registers that got pushed when cpu_switchto() was called,
    291 	 * and return.
    292 	 */
    293 	pop	{r4-r7, ip, pc}
    294 
    295 END(cpu_switchto)
    296 
    297 ENTRY_NP(lwp_trampoline)
    298 	/*
    299 	 * cpu_switchto gives us:
    300 	 *	arg0(r0) = old lwp
    301 	 *	arg1(r1) = new lwp
    302 	 * setup by cpu_lwp_fork:
    303 	 *	r4 = func to call
    304 	 *	r5 = arg to func
    305 	 *	r6 = <unused>
    306 	 *	r7 = spsr mode
    307 	 */
    308 	bl	_C_LABEL(lwp_startup)
    309 
    310 	mov	fp, #0			/* top stack frame */
    311 	mov	r0, r5
    312 	mov	r1, sp
    313 #ifdef _ARM_ARCH_5
    314 	blx	r4
    315 #else
    316 	mov	lr, pc
    317 	mov	pc, r4
    318 #endif
    319 
    320 	GET_CPSR(r0)
    321 	CPSID_I(r0, r0)			/* Kill irq's */
    322 
    323 	GET_CURCPU(r4)			/* for DO_AST */
    324 	DO_AST_AND_RESTORE_ALIGNMENT_FAULTS
    325 	PULLFRAME
    326 
    327 	movs	pc, lr			/* Exit */
    328 END(lwp_trampoline)
    329 
    330 AST_ALIGNMENT_FAULT_LOCALS
    331 
    332 #ifdef __HAVE_FAST_SOFTINTS
    333 /*
    334  *	Called at IPL_HIGH
    335  *	r0 = new lwp
    336  *	r1 = ipl for softint_dispatch
    337  */
    338 ENTRY_NP(softint_switch)
    339 	push	{r4, r6, r7, lr}
    340 
    341 	ldr	r7, [r0, #L_CPU]	/* get curcpu */
    342 #if defined(TPIDRPRW_IS_CURLWP)
    343 	mrc	p15, 0, r4, c13, c0, 4	/* get old lwp */
    344 #else
    345 	ldr	r4, [r7, #(CI_CURLWP)]	/* get old lwp */
    346 #endif
    347 	mrs	r6, cpsr		/* we need to save this */
    348 
    349 	/*
    350 	 * If the soft lwp blocks, it needs to return to softint_tramp
    351 	 */
    352 	mov	r2, sp			/* think ip */
    353 	adr	r3, softint_tramp	/* think lr */
    354 	push	{r2-r3}
    355 	push	{r4-r7}
    356 
    357 	mov	r5, r0			/* save new lwp */
    358 
    359 	ldr	r2, [r4, #(L_PCB)]	/* get old lwp's pcb */
    360 
    361 	/* Save all the registers into the old lwp's pcb */
    362 #if defined(__XSCALE__) || defined(_ARM_ARCH_6)
    363 	strd	r8, r9, [r2, #(PCB_R8)]
    364 	strd	r10, r11, [r2, #(PCB_R10)]
    365 	strd	r12, r13, [r2, #(PCB_R12)]
    366 #else
    367 	add	r3, r2, #(PCB_R8)
    368 	stmia	r3, {r8-r13}
    369 #endif
    370 
    371 #ifdef _ARM_ARCH_6
    372 	/*
    373 	 * Save user read/write thread/process id register in case it was
    374 	 * set in userland.
    375 	 */
    376 	mrc	p15, 0, r0, c13, c0, 2
    377 	str	r0, [r2, #(PCB_USER_PID_RW)]
    378 #endif
    379 
    380 	/* this is an invariant so load before disabling intrs */
    381 	ldr	r2, [r5, #(L_PCB)]	/* get new lwp's pcb */
    382 
    383 	IRQdisable
    384 	/*
    385 	 * We're switching to a bound LWP so its l_cpu is already correct.
    386 	 */
    387 #if defined(TPIDRPRW_IS_CURLWP)
    388 	mcr	p15, 0, r5, c13, c0, 4	/* save new lwp */
    389 #endif
    390 	str	r5, [r7, #(CI_CURLWP)]	/* save new lwp */
    391 
    392 	/*
    393 	 * Normally, we'd get {r8-r13} but since this is a softint lwp
    394 	 * its existing state doesn't matter.  We start the stack just
    395 	 * below the trapframe.
    396 	 */
    397 	ldr	sp, [r5, #(L_MD_TF)]	/* get new lwp's stack ptr */
    398 
    399 	/* At this point we can allow IRQ's again. */
    400 	IRQenable
    401 					/* r1 still has ipl */
    402 	mov	r0, r4			/* r0 has pinned (old) lwp */
    403 	bl	_C_LABEL(softint_dispatch)
    404 	/*
    405 	 * If we've returned, we need to change everything back and return.
    406 	 */
    407 	ldr	r2, [r4, #(L_PCB)]	/* get pinned lwp's pcb */
    408 
    409 	/*
    410 	 * We don't need to restore all the registers since another lwp was
    411 	 * never executed.  But we do need the SP from the formerly pinned lwp.
    412 	 */
    413 
    414 	IRQdisable
    415 #if defined(TPIDRPRW_IS_CURLWP)
    416 	mcr	p15, 0, r4, c13, c0, 4	/* restore pinned lwp */
    417 #endif
    418 	str	r4, [r7, #(CI_CURLWP)]	/* restore pinned lwp */
    419 	ldr	sp, [r2, #(PCB_KSP)]	/* now running on the old stack. */
    420 
    421 	/* At this point we can allow IRQ's again. */
    422 	msr	cpsr_c, r6
    423 
    424 	/*
    425 	 * Grab the registers that got pushed at the start and return.
    426 	 */
    427 	pop	{r4-r7, ip, lr}		/* eat switch frame */
    428 	pop	{r4, r6, r7, pc}	/* pop stack and return */
    429 
    430 END(softint_switch)
    431 
    432 /*
    433  * r0 = previous LWP (the soft lwp)
    434  * r4 = original LWP (the current lwp)
    435  * r6 = original CPSR
    436  * r7 = curcpu()
    437  */
    438 ENTRY_NP(softint_tramp)
    439 	ldr	r3, [r7, #(CI_MTX_COUNT)]	/* readjust after mi_switch */
    440 	add	r3, r3, #1
    441 	str	r3, [r7, #(CI_MTX_COUNT)]
    442 
    443 	msr	cpsr_c, r6			/* restore interrupts */
    444 	pop	{r4, r6, r7, pc}		/* pop stack and return */
    445 END(softint_tramp)
    446 #endif /* __HAVE_FAST_SOFTINTS */
    447