cpuswitch.S revision 1.51 1 /* $NetBSD: cpuswitch.S,v 1.51 2008/01/12 20:50:23 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_arm32_pmap.h"
82 #include "opt_multiprocessor.h"
83 #include "opt_lockdebug.h"
84
85 #include "assym.h"
86 #include <arm/arm32/pte.h>
87 #include <machine/param.h>
88 #include <machine/cpu.h>
89 #include <machine/frame.h>
90 #include <machine/asm.h>
91
92 /* LINTSTUB: include <sys/param.h> */
93
94 #undef IRQdisable
95 #undef IRQenable
96
97 /*
98 * New experimental definitions of IRQdisable and IRQenable
99 * These keep FIQ's enabled since FIQ's are special.
100 */
101
102 #define IRQdisable \
103 mrs r14, cpsr ; \
104 orr r14, r14, #(I32_bit) ; \
105 msr cpsr_c, r14 ; \
106
107 #define IRQenable \
108 mrs r14, cpsr ; \
109 bic r14, r14, #(I32_bit) ; \
110 msr cpsr_c, r14 ; \
111
112 /*
113 * These are used for switching the translation table/DACR.
114 * Since the vector page can be invalid for a short time, we must
115 * disable both regular IRQs *and* FIQs.
116 *
117 * XXX: This is not necessary if the vector table is relocated.
118 */
119 #define IRQdisableALL \
120 mrs r14, cpsr ; \
121 orr r14, r14, #(I32_bit | F32_bit) ; \
122 msr cpsr_c, r14
123
124 #define IRQenableALL \
125 mrs r14, cpsr ; \
126 bic r14, r14, #(I32_bit | F32_bit) ; \
127 msr cpsr_c, r14
128
129 .text
130
131 #ifdef MULTIPROCESSOR
132 .Lcpu_info_store:
133 .word _C_LABEL(cpu_info_store)
134 .Lcurlwp:
135 /* FIXME: This is bogus in the general case. */
136 .word _C_LABEL(cpu_info_store) + CI_CURLWP
137
138 .Lcurpcb:
139 .word _C_LABEL(cpu_info_store) + CI_CURPCB
140 #else
141 .Lcurlwp:
142 .word _C_LABEL(curlwp)
143
144 .Lcurpcb:
145 .word _C_LABEL(curpcb)
146 #endif
147
148 .Lcpufuncs:
149 .word _C_LABEL(cpufuncs)
150
151 #ifndef MULTIPROCESSOR
152 .data
153 .global _C_LABEL(curpcb)
154 _C_LABEL(curpcb):
155 .word 0x00000000
156 .text
157 #endif
158
159 .Lblock_userspace_access:
160 .word _C_LABEL(block_userspace_access)
161
162
163 /*
164 * struct lwp *
165 * cpu_switchto(struct lwp *current, struct lwp *next)
166 *
167 * Switch to the specified next LWP
168 * Arguments:
169 *
170 * r0 'struct lwp *' of the current LWP
171 * r1 'struct lwp *' of the LWP to switch to
172 */
173 ENTRY(cpu_switchto)
174 mov ip, sp
175 stmfd sp!, {r4-r7, ip, lr}
176
177 mov r6, r1 /* save new lwp */
178 mov r4, r0 /* save old lwp, it's the return value */
179
180 IRQdisable
181
182 #ifdef MULTIPROCESSOR
183 /* XXX use curcpu() */
184 ldr r0, .Lcpu_info_store
185 str r0, [r6, #(L_CPU)]
186 #else
187 /* l->l_cpu initialized in fork1() for single-processor */
188 #endif
189
190 /* We have a new curlwp now so make a note it */
191 ldr r7, .Lcurlwp
192 str r6, [r7]
193
194 /* Hook in a new pcb */
195 ldr r7, .Lcurpcb
196 ldr r0, [r6, #(L_ADDR)]
197 str r0, [r7]
198
199 /* At this point we can allow IRQ's again. */
200 IRQenable
201
202 /* rem: r4 = old lwp */
203 /* rem: r6 = new lwp */
204 /* rem: interrupts are enabled */
205
206 /*
207 * If the old lwp on entry to cpu_switchto was zero then the
208 * process that called it was exiting. This means that we do
209 * not need to save the current context. Instead we can jump
210 * straight to restoring the context for the new process.
211 */
212 teq r4, #0x00000000
213 ldreq r9, [r6, #(L_ADDR)]
214 beq .Ldo_switch
215
216 /* rem: r4 = old lwp */
217 /* rem: r6 = new lwp */
218 /* rem: interrupts are enabled */
219
220 /* Save old context */
221
222 /* Get the user structure for the old lwp. */
223 ldr r1, [r4, #(L_ADDR)]
224
225 /* Save all the registers in the old lwp's pcb */
226 #ifndef __XSCALE__
227 add r7, r1, #(PCB_R8)
228 stmia r7, {r8-r13}
229 #else
230 strd r8, [r1, #(PCB_R8)]
231 strd r10, [r1, #(PCB_R10)]
232 strd r12, [r1, #(PCB_R12)]
233 #endif
234
235 /*
236 * NOTE: We can now use r8-r13 until it is time to restore
237 * them for the new process.
238 */
239
240 /* rem: r1 = old lwp PCB */
241 /* rem: r4 = old lwp */
242 /* rem: r6 = new lwp */
243 /* rem: interrupts are enabled */
244
245 /* Remember the old PCB. */
246 mov r8, r1
247
248 /* r1 now free! */
249
250 /* Get the user structure for the new process in r9 */
251 ldr r9, [r6, #(L_ADDR)]
252
253 /*
254 * This can be optimised... We know we want to go from SVC32
255 * mode to UND32 mode
256 */
257 mrs r3, cpsr
258 bic r2, r3, #(PSR_MODE)
259 orr r2, r2, #(PSR_UND32_MODE | I32_bit)
260 msr cpsr_c, r2
261
262 str sp, [r8, #(PCB_UND_SP)]
263
264 msr cpsr_c, r3 /* Restore the old mode */
265
266 /* What else needs to be saved? Only FPA stuff when that is supported */
267
268 /* Restore saved context */
269
270 /* rem: r4 = old lwp */
271 /* rem: r6 = new lwp */
272 /* rem: r9 = new PCB */
273 /* rem: interrupts are enabled */
274
275 .Ldo_switch:
276 mov r0, r4
277 mov r1, r6
278 bl _C_LABEL(pmap_switch)
279
280
281 /* rem: r4 = old lwp */
282 /* rem: r6 = new lwp */
283 /* rem: r9 = new PCB */
284
285 /*
286 * This can be optimised... We know we want to go from SVC32
287 * mode to UND32 mode
288 */
289 mrs r3, cpsr
290 bic r2, r3, #(PSR_MODE)
291 orr r2, r2, #(PSR_UND32_MODE)
292 msr cpsr_c, r2
293
294 ldr sp, [r9, #(PCB_UND_SP)]
295
296 msr cpsr_c, r3 /* Restore the old mode */
297
298 /* Restore all the save registers */
299 #ifndef __XSCALE__
300 add r7, r9, #PCB_R8
301 ldmia r7, {r8-r13}
302
303 sub r7, r7, #PCB_R8 /* restore PCB pointer */
304 #else
305 mov r7, r9
306 ldr r8, [r7, #(PCB_R8)]
307 ldr r9, [r7, #(PCB_R9)]
308 ldr r10, [r7, #(PCB_R10)]
309 ldr r11, [r7, #(PCB_R11)]
310 ldr r12, [r7, #(PCB_R12)]
311 ldr r13, [r7, #(PCB_SP)]
312 #endif
313
314 ldr r5, [r6, #(L_PROC)] /* fetch the proc for below */
315
316 /* rem: r4 = old lwp */
317 /* rem: r5 = new lwp's proc */
318 /* rem: r6 = new lwp */
319 /* rem: r7 = new pcb */
320
321 #ifdef ARMFPE
322 add r0, r7, #(USER_SIZE) & 0x00ff
323 add r0, r0, #(USER_SIZE) & 0xff00
324 bl _C_LABEL(arm_fpe_core_changecontext)
325 #endif
326
327 /* We can enable interrupts again */
328 IRQenableALL
329
330 /* rem: r4 = old lwp */
331 /* rem: r5 = new lwp's proc */
332 /* rem: r6 = new lwp */
333 /* rem: r7 = new PCB */
334
335 /*
336 * Check for restartable atomic sequences (RAS).
337 */
338
339 ldr r2, [r5, #(P_RASLIST)]
340 ldr r1, [r7, #(PCB_TF)] /* r1 = trapframe (used below) */
341 teq r2, #0 /* p->p_nras == 0? */
342 bne .Lswitch_do_ras /* no, check for one */
343
344 .Lswitch_return:
345 /* cpu_switchto returns the old lwp */
346 mov r0, r4
347 /* lwp_trampoline expects new lwp as it's second argument */
348 mov r1, r6
349
350 /*
351 * Pull the registers that got pushed when cpu_switchto() was called,
352 * and return.
353 */
354 ldmfd sp, {r4-r7, sp, pc}
355
356 .Lswitch_do_ras:
357 ldr r1, [r1, #(TF_PC)] /* second ras_lookup() arg */
358 mov r0, r5 /* first ras_lookup() arg */
359 bl _C_LABEL(ras_lookup)
360 cmn r0, #1 /* -1 means "not in a RAS" */
361 ldrne r1, [r7, #(PCB_TF)]
362 strne r0, [r1, #(TF_PC)]
363 b .Lswitch_return
364
365 ENTRY(lwp_trampoline)
366 bl _C_LABEL(lwp_startup)
367
368 mov r0, r5
369 mov r1, sp
370 mov lr, pc
371 mov pc, r4
372
373 /* Kill irq's */
374 mrs r0, cpsr
375 orr r0, r0, #(I32_bit)
376 msr cpsr_c, r0
377
378 PULLFRAME
379
380 movs pc, lr /* Exit */
381