blob: 3ef03009de5f65fa4807cac71bb9749baaa63ab7 [file] [log] [blame]
Sanjay Lalb680f702012-11-21 18:34:01 -08001/*
David Daney2c07ebb2013-08-01 13:22:33 -07002 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Main entry point for the guest, exception handling.
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 */
Sanjay Lalb680f702012-11-21 18:34:01 -080011
12#include <asm/asm.h>
13#include <asm/asmmacro.h>
14#include <asm/regdef.h>
15#include <asm/mipsregs.h>
16#include <asm/stackframe.h>
17#include <asm/asm-offsets.h>
18
Sanjay Lalb680f702012-11-21 18:34:01 -080019#define _C_LABEL(x) x
20#define MIPSX(name) mips32_ ## name
21#define CALLFRAME_SIZ 32
22
23/*
24 * VECTOR
25 * exception vector entrypoint
26 */
27#define VECTOR(x, regmask) \
28 .ent _C_LABEL(x),0; \
29 EXPORT(x);
30
31#define VECTOR_END(x) \
32 EXPORT(x);
33
34/* Overload, Danger Will Robinson!! */
Sanjay Lalb680f702012-11-21 18:34:01 -080035#define PT_HOST_USERLOCAL PT_EPC
36
37#define CP0_DDATA_LO $28,3
Sanjay Lalb680f702012-11-21 18:34:01 -080038
39/* Resume Flags */
40#define RESUME_FLAG_HOST (1<<1) /* Resume host? */
41
42#define RESUME_GUEST 0
43#define RESUME_HOST RESUME_FLAG_HOST
44
45/*
46 * __kvm_mips_vcpu_run: entry point to the guest
47 * a0: run
48 * a1: vcpu
49 */
David Daney2c07ebb2013-08-01 13:22:33 -070050 .set noreorder
Sanjay Lalb680f702012-11-21 18:34:01 -080051
David Daneybb48c2f2013-08-01 13:22:34 -070052FEXPORT(__kvm_mips_vcpu_run)
David Daney2c07ebb2013-08-01 13:22:33 -070053 /* k0/k1 not being used in host kernel context */
David Daneyea69f282013-08-01 13:22:35 -070054 INT_ADDIU k1, sp, -PT_SIZE
David Daney2c07ebb2013-08-01 13:22:33 -070055 LONG_S $16, PT_R16(k1)
56 LONG_S $17, PT_R17(k1)
David Daney2c07ebb2013-08-01 13:22:33 -070057 LONG_S $18, PT_R18(k1)
58 LONG_S $19, PT_R19(k1)
59 LONG_S $20, PT_R20(k1)
60 LONG_S $21, PT_R21(k1)
61 LONG_S $22, PT_R22(k1)
62 LONG_S $23, PT_R23(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080063
David Daney2c07ebb2013-08-01 13:22:33 -070064 LONG_S $28, PT_R28(k1)
65 LONG_S $29, PT_R29(k1)
66 LONG_S $30, PT_R30(k1)
67 LONG_S $31, PT_R31(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080068
David Daney2c07ebb2013-08-01 13:22:33 -070069 /* Save hi/lo */
70 mflo v0
71 LONG_S v0, PT_LO(k1)
72 mfhi v1
73 LONG_S v1, PT_HI(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080074
75 /* Save host status */
David Daney2c07ebb2013-08-01 13:22:33 -070076 mfc0 v0, CP0_STATUS
77 LONG_S v0, PT_STATUS(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080078
David Daney2c07ebb2013-08-01 13:22:33 -070079 /* Save DDATA_LO, will be used to store pointer to vcpu */
80 mfc0 v1, CP0_DDATA_LO
81 LONG_S v1, PT_HOST_USERLOCAL(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080082
David Daney2c07ebb2013-08-01 13:22:33 -070083 /* DDATA_LO has pointer to vcpu */
84 mtc0 a1, CP0_DDATA_LO
Sanjay Lalb680f702012-11-21 18:34:01 -080085
David Daney2c07ebb2013-08-01 13:22:33 -070086 /* Offset into vcpu->arch */
David Daneyea69f282013-08-01 13:22:35 -070087 INT_ADDIU k1, a1, VCPU_HOST_ARCH
Sanjay Lalb680f702012-11-21 18:34:01 -080088
David Daney2c07ebb2013-08-01 13:22:33 -070089 /*
90 * Save the host stack to VCPU, used for exception processing
91 * when we exit from the Guest
92 */
93 LONG_S sp, VCPU_HOST_STACK(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080094
David Daney2c07ebb2013-08-01 13:22:33 -070095 /* Save the kernel gp as well */
96 LONG_S gp, VCPU_HOST_GP(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -080097
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070098 /*
99 * Setup status register for running the guest in UM, interrupts
100 * are disabled
101 */
David Daney2c07ebb2013-08-01 13:22:33 -0700102 li k0, (ST0_EXL | KSU_USER | ST0_BEV)
103 mtc0 k0, CP0_STATUS
104 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800105
David Daney2c07ebb2013-08-01 13:22:33 -0700106 /* load up the new EBASE */
107 LONG_L k0, VCPU_GUEST_EBASE(k1)
108 mtc0 k0, CP0_EBASE
Sanjay Lalb680f702012-11-21 18:34:01 -0800109
David Daney2c07ebb2013-08-01 13:22:33 -0700110 /*
111 * Now that the new EBASE has been loaded, unset BEV, set
112 * interrupt mask as it was but make sure that timer interrupts
113 * are enabled
114 */
115 li k0, (ST0_EXL | KSU_USER | ST0_IE)
116 andi v0, v0, ST0_IM
117 or k0, k0, v0
118 mtc0 k0, CP0_STATUS
119 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800120
Sanjay Lalb680f702012-11-21 18:34:01 -0800121 /* Set Guest EPC */
David Daney2c07ebb2013-08-01 13:22:33 -0700122 LONG_L t0, VCPU_PC(k1)
123 mtc0 t0, CP0_EPC
Sanjay Lalb680f702012-11-21 18:34:01 -0800124
125FEXPORT(__kvm_mips_load_asid)
David Daney2c07ebb2013-08-01 13:22:33 -0700126 /* Set the ASID for the Guest Kernel */
James Hogan002374f2015-11-11 14:21:18 +0000127 PTR_L t0, VCPU_COP0(k1)
128 LONG_L t0, COP0_STATUS(t0)
129 andi t0, KSU_USER | ST0_ERL | ST0_EXL
130 xori t0, KSU_USER
131 bnez t0, 1f /* If kernel */
David Daneyea69f282013-08-01 13:22:35 -0700132 INT_ADDIU t1, k1, VCPU_GUEST_KERNEL_ASID /* (BD) */
133 INT_ADDIU t1, k1, VCPU_GUEST_USER_ASID /* else user */
Sanjay Lalb680f702012-11-21 18:34:01 -08001341:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700135 /* t1: contains the base of the ASID array, need to get the cpu id */
David Daney2c07ebb2013-08-01 13:22:33 -0700136 LONG_L t2, TI_CPU($28) /* smp_processor_id */
David Daneyea69f282013-08-01 13:22:35 -0700137 INT_SLL t2, t2, 2 /* x4 */
138 REG_ADDU t3, t1, t2
David Daney2c07ebb2013-08-01 13:22:33 -0700139 LONG_L k0, (t3)
Paul Burton2db003a2016-05-06 14:36:24 +0100140#ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
141 li t3, CPUINFO_SIZE/4
142 mul t2, t2, t3 /* x sizeof(struct cpuinfo_mips)/4 */
143 LONG_L t2, (cpu_data + CPUINFO_ASID_MASK)(t2)
144 and k0, k0, t2
145#else
James Hogan9b5c3392016-05-06 14:36:19 +0100146 andi k0, k0, MIPS_ENTRYHI_ASID
Paul Burton2db003a2016-05-06 14:36:24 +0100147#endif
David Daney2c07ebb2013-08-01 13:22:33 -0700148 mtc0 k0, CP0_ENTRYHI
149 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800150
David Daney2c07ebb2013-08-01 13:22:33 -0700151 /* Disable RDHWR access */
152 mtc0 zero, CP0_HWRENA
Sanjay Lalb680f702012-11-21 18:34:01 -0800153
James Hoganf1b711c2016-05-06 14:36:22 +0100154 .set noat
David Daney2c07ebb2013-08-01 13:22:33 -0700155 /* Now load up the Guest Context from VCPU */
156 LONG_L $1, VCPU_R1(k1)
157 LONG_L $2, VCPU_R2(k1)
158 LONG_L $3, VCPU_R3(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800159
David Daney2c07ebb2013-08-01 13:22:33 -0700160 LONG_L $4, VCPU_R4(k1)
161 LONG_L $5, VCPU_R5(k1)
162 LONG_L $6, VCPU_R6(k1)
163 LONG_L $7, VCPU_R7(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800164
David Daney2c07ebb2013-08-01 13:22:33 -0700165 LONG_L $8, VCPU_R8(k1)
166 LONG_L $9, VCPU_R9(k1)
167 LONG_L $10, VCPU_R10(k1)
168 LONG_L $11, VCPU_R11(k1)
169 LONG_L $12, VCPU_R12(k1)
170 LONG_L $13, VCPU_R13(k1)
171 LONG_L $14, VCPU_R14(k1)
172 LONG_L $15, VCPU_R15(k1)
173 LONG_L $16, VCPU_R16(k1)
174 LONG_L $17, VCPU_R17(k1)
175 LONG_L $18, VCPU_R18(k1)
176 LONG_L $19, VCPU_R19(k1)
177 LONG_L $20, VCPU_R20(k1)
178 LONG_L $21, VCPU_R21(k1)
179 LONG_L $22, VCPU_R22(k1)
180 LONG_L $23, VCPU_R23(k1)
181 LONG_L $24, VCPU_R24(k1)
182 LONG_L $25, VCPU_R25(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800183
David Daney2c07ebb2013-08-01 13:22:33 -0700184 /* k0/k1 loaded up later */
Sanjay Lalb680f702012-11-21 18:34:01 -0800185
David Daney2c07ebb2013-08-01 13:22:33 -0700186 LONG_L $28, VCPU_R28(k1)
187 LONG_L $29, VCPU_R29(k1)
188 LONG_L $30, VCPU_R30(k1)
189 LONG_L $31, VCPU_R31(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800190
David Daney2c07ebb2013-08-01 13:22:33 -0700191 /* Restore hi/lo */
192 LONG_L k0, VCPU_LO(k1)
193 mtlo k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800194
David Daney2c07ebb2013-08-01 13:22:33 -0700195 LONG_L k0, VCPU_HI(k1)
196 mthi k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800197
198FEXPORT(__kvm_mips_load_k0k1)
199 /* Restore the guest's k0/k1 registers */
David Daney2c07ebb2013-08-01 13:22:33 -0700200 LONG_L k0, VCPU_R26(k1)
201 LONG_L k1, VCPU_R27(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800202
David Daney2c07ebb2013-08-01 13:22:33 -0700203 /* Jump to guest */
Sanjay Lalb680f702012-11-21 18:34:01 -0800204 eret
Sanjay Lalb680f702012-11-21 18:34:01 -0800205
206VECTOR(MIPSX(exception), unknown)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700207/* Find out what mode we came from and jump to the proper handler. */
David Daney2c07ebb2013-08-01 13:22:33 -0700208 mtc0 k0, CP0_ERROREPC #01: Save guest k0
209 ehb #02:
Sanjay Lalb680f702012-11-21 18:34:01 -0800210
David Daney2c07ebb2013-08-01 13:22:33 -0700211 mfc0 k0, CP0_EBASE #02: Get EBASE
David Daneyea69f282013-08-01 13:22:35 -0700212 INT_SRL k0, k0, 10 #03: Get rid of CPUNum
213 INT_SLL k0, k0, 10 #04
David Daney2c07ebb2013-08-01 13:22:33 -0700214 LONG_S k1, 0x3000(k0) #05: Save k1 @ offset 0x3000
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700215 INT_ADDIU k0, k0, 0x2000 #06: Exception handler is
216 # installed @ offset 0x2000
David Daney2c07ebb2013-08-01 13:22:33 -0700217 j k0 #07: jump to the function
218 nop #08: branch delay slot
Sanjay Lalb680f702012-11-21 18:34:01 -0800219VECTOR_END(MIPSX(exceptionEnd))
220.end MIPSX(exception)
221
222/*
223 * Generic Guest exception handler. We end up here when the guest
224 * does something that causes a trap to kernel mode.
Sanjay Lalb680f702012-11-21 18:34:01 -0800225 */
226NESTED (MIPSX(GuestException), CALLFRAME_SIZ, ra)
David Daney2c07ebb2013-08-01 13:22:33 -0700227 /* Get the VCPU pointer from DDTATA_LO */
228 mfc0 k1, CP0_DDATA_LO
David Daneyea69f282013-08-01 13:22:35 -0700229 INT_ADDIU k1, k1, VCPU_HOST_ARCH
Sanjay Lalb680f702012-11-21 18:34:01 -0800230
David Daney2c07ebb2013-08-01 13:22:33 -0700231 /* Start saving Guest context to VCPU */
232 LONG_S $0, VCPU_R0(k1)
233 LONG_S $1, VCPU_R1(k1)
234 LONG_S $2, VCPU_R2(k1)
235 LONG_S $3, VCPU_R3(k1)
236 LONG_S $4, VCPU_R4(k1)
237 LONG_S $5, VCPU_R5(k1)
238 LONG_S $6, VCPU_R6(k1)
239 LONG_S $7, VCPU_R7(k1)
240 LONG_S $8, VCPU_R8(k1)
241 LONG_S $9, VCPU_R9(k1)
242 LONG_S $10, VCPU_R10(k1)
243 LONG_S $11, VCPU_R11(k1)
244 LONG_S $12, VCPU_R12(k1)
245 LONG_S $13, VCPU_R13(k1)
246 LONG_S $14, VCPU_R14(k1)
247 LONG_S $15, VCPU_R15(k1)
248 LONG_S $16, VCPU_R16(k1)
249 LONG_S $17, VCPU_R17(k1)
250 LONG_S $18, VCPU_R18(k1)
251 LONG_S $19, VCPU_R19(k1)
252 LONG_S $20, VCPU_R20(k1)
253 LONG_S $21, VCPU_R21(k1)
254 LONG_S $22, VCPU_R22(k1)
255 LONG_S $23, VCPU_R23(k1)
256 LONG_S $24, VCPU_R24(k1)
257 LONG_S $25, VCPU_R25(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800258
David Daney2c07ebb2013-08-01 13:22:33 -0700259 /* Guest k0/k1 saved later */
Sanjay Lalb680f702012-11-21 18:34:01 -0800260
David Daney2c07ebb2013-08-01 13:22:33 -0700261 LONG_S $28, VCPU_R28(k1)
262 LONG_S $29, VCPU_R29(k1)
263 LONG_S $30, VCPU_R30(k1)
264 LONG_S $31, VCPU_R31(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800265
James Hoganf1b711c2016-05-06 14:36:22 +0100266 .set at
267
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700268 /* We need to save hi/lo and restore them on the way out */
David Daney2c07ebb2013-08-01 13:22:33 -0700269 mfhi t0
270 LONG_S t0, VCPU_HI(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800271
David Daney2c07ebb2013-08-01 13:22:33 -0700272 mflo t0
273 LONG_S t0, VCPU_LO(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800274
David Daney2c07ebb2013-08-01 13:22:33 -0700275 /* Finally save guest k0/k1 to VCPU */
276 mfc0 t0, CP0_ERROREPC
277 LONG_S t0, VCPU_R26(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800278
David Daney2c07ebb2013-08-01 13:22:33 -0700279 /* Get GUEST k1 and save it in VCPU */
David Daneyea69f282013-08-01 13:22:35 -0700280 PTR_LI t1, ~0x2ff
David Daney2c07ebb2013-08-01 13:22:33 -0700281 mfc0 t0, CP0_EBASE
282 and t0, t0, t1
283 LONG_L t0, 0x3000(t0)
284 LONG_S t0, VCPU_R27(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800285
David Daney2c07ebb2013-08-01 13:22:33 -0700286 /* Now that context has been saved, we can use other registers */
Sanjay Lalb680f702012-11-21 18:34:01 -0800287
David Daney2c07ebb2013-08-01 13:22:33 -0700288 /* Restore vcpu */
289 mfc0 a1, CP0_DDATA_LO
290 move s1, a1
Sanjay Lalb680f702012-11-21 18:34:01 -0800291
David Daney2c07ebb2013-08-01 13:22:33 -0700292 /* Restore run (vcpu->run) */
293 LONG_L a0, VCPU_RUN(a1)
294 /* Save pointer to run in s0, will be saved by the compiler */
295 move s0, a0
296
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700297 /*
298 * Save Host level EPC, BadVaddr and Cause to VCPU, useful to
299 * process the exception
300 */
David Daney2c07ebb2013-08-01 13:22:33 -0700301 mfc0 k0,CP0_EPC
302 LONG_S k0, VCPU_PC(k1)
303
304 mfc0 k0, CP0_BADVADDR
305 LONG_S k0, VCPU_HOST_CP0_BADVADDR(k1)
306
307 mfc0 k0, CP0_CAUSE
308 LONG_S k0, VCPU_HOST_CP0_CAUSE(k1)
309
310 mfc0 k0, CP0_ENTRYHI
311 LONG_S k0, VCPU_HOST_ENTRYHI(k1)
312
313 /* Now restore the host state just enough to run the handlers */
314
James Hogancaa1faa2015-12-16 23:49:26 +0000315 /* Switch EBASE to the one used by Linux */
David Daney2c07ebb2013-08-01 13:22:33 -0700316 /* load up the host EBASE */
317 mfc0 v0, CP0_STATUS
318
David Daney2c07ebb2013-08-01 13:22:33 -0700319 or k0, v0, ST0_BEV
David Daney2c07ebb2013-08-01 13:22:33 -0700320
321 mtc0 k0, CP0_STATUS
322 ehb
323
324 LONG_L k0, VCPU_HOST_EBASE(k1)
325 mtc0 k0,CP0_EBASE
Sanjay Lalb680f702012-11-21 18:34:01 -0800326
James Hogan98e91b82014-11-18 14:09:12 +0000327 /*
328 * If FPU is enabled, save FCR31 and clear it so that later ctc1's don't
329 * trigger FPE for pending exceptions.
330 */
James Hogan98e91b82014-11-18 14:09:12 +0000331 and v1, v0, ST0_CU1
332 beqz v1, 1f
333 nop
334 .set push
335 SET_HARDFLOAT
336 cfc1 t0, fcr31
337 sw t0, VCPU_FCR31(k1)
338 ctc1 zero,fcr31
339 .set pop
James Hogan98e91b82014-11-18 14:09:12 +00003401:
341
James Hogan539cb89fb2015-03-05 11:43:36 +0000342#ifdef CONFIG_CPU_HAS_MSA
343 /*
344 * If MSA is enabled, save MSACSR and clear it so that later
345 * instructions don't trigger MSAFPE for pending exceptions.
346 */
347 mfc0 t0, CP0_CONFIG3
348 ext t0, t0, 28, 1 /* MIPS_CONF3_MSAP */
349 beqz t0, 1f
350 nop
351 mfc0 t0, CP0_CONFIG5
352 ext t0, t0, 27, 1 /* MIPS_CONF5_MSAEN */
353 beqz t0, 1f
354 nop
355 _cfcmsa t0, MSA_CSR
356 sw t0, VCPU_MSA_CSR(k1)
357 _ctcmsa MSA_CSR, zero
3581:
359#endif
360
David Daney2c07ebb2013-08-01 13:22:33 -0700361 /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
David Daney2c07ebb2013-08-01 13:22:33 -0700362 and v0, v0, ~(ST0_EXL | KSU_USER | ST0_IE)
363 or v0, v0, ST0_CU0
David Daney2c07ebb2013-08-01 13:22:33 -0700364 mtc0 v0, CP0_STATUS
365 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800366
David Daney2c07ebb2013-08-01 13:22:33 -0700367 /* Load up host GP */
368 LONG_L gp, VCPU_HOST_GP(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800369
David Daney2c07ebb2013-08-01 13:22:33 -0700370 /* Need a stack before we can jump to "C" */
371 LONG_L sp, VCPU_HOST_STACK(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800372
David Daney2c07ebb2013-08-01 13:22:33 -0700373 /* Saved host state */
David Daneyea69f282013-08-01 13:22:35 -0700374 INT_ADDIU sp, sp, -PT_SIZE
Sanjay Lalb680f702012-11-21 18:34:01 -0800375
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700376 /*
377 * XXXKYMA do we need to load the host ASID, maybe not because the
David Daney2c07ebb2013-08-01 13:22:33 -0700378 * kernel entries are marked GLOBAL, need to verify
379 */
Sanjay Lalb680f702012-11-21 18:34:01 -0800380
David Daney2c07ebb2013-08-01 13:22:33 -0700381 /* Restore host DDATA_LO */
382 LONG_L k0, PT_HOST_USERLOCAL(sp)
383 mtc0 k0, CP0_DDATA_LO
Sanjay Lalb680f702012-11-21 18:34:01 -0800384
David Daney2c07ebb2013-08-01 13:22:33 -0700385 /* Restore RDHWR access */
David Daneyea69f282013-08-01 13:22:35 -0700386 PTR_LI k0, 0x2000000F
David Daney2c07ebb2013-08-01 13:22:33 -0700387 mtc0 k0, CP0_HWRENA
Sanjay Lalb680f702012-11-21 18:34:01 -0800388
David Daney2c07ebb2013-08-01 13:22:33 -0700389 /* Jump to handler */
Sanjay Lalb680f702012-11-21 18:34:01 -0800390FEXPORT(__kvm_mips_jump_to_handler)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700391 /*
392 * XXXKYMA: not sure if this is safe, how large is the stack??
David Daney2c07ebb2013-08-01 13:22:33 -0700393 * Now jump to the kvm_mips_handle_exit() to see if we can deal
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700394 * with this in the kernel
395 */
David Daneyea69f282013-08-01 13:22:35 -0700396 PTR_LA t9, kvm_mips_handle_exit
David Daney2c07ebb2013-08-01 13:22:33 -0700397 jalr.hb t9
David Daneyea69f282013-08-01 13:22:35 -0700398 INT_ADDIU sp, sp, -CALLFRAME_SIZ /* BD Slot */
Sanjay Lalb680f702012-11-21 18:34:01 -0800399
David Daney2c07ebb2013-08-01 13:22:33 -0700400 /* Return from handler Make sure interrupts are disabled */
401 di
402 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800403
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700404 /*
405 * XXXKYMA: k0/k1 could have been blown away if we processed
David Daney2c07ebb2013-08-01 13:22:33 -0700406 * an exception while we were handling the exception from the
407 * guest, reload k1
408 */
Sanjay Lalb680f702012-11-21 18:34:01 -0800409
David Daney2c07ebb2013-08-01 13:22:33 -0700410 move k1, s1
David Daneyea69f282013-08-01 13:22:35 -0700411 INT_ADDIU k1, k1, VCPU_HOST_ARCH
David Daney2c07ebb2013-08-01 13:22:33 -0700412
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700413 /*
414 * Check return value, should tell us if we are returning to the
David Daney2c07ebb2013-08-01 13:22:33 -0700415 * host (handle I/O etc)or resuming the guest
416 */
417 andi t0, v0, RESUME_HOST
418 bnez t0, __kvm_mips_return_to_host
419 nop
Sanjay Lalb680f702012-11-21 18:34:01 -0800420
421__kvm_mips_return_to_guest:
David Daney2c07ebb2013-08-01 13:22:33 -0700422 /* Put the saved pointer to vcpu (s1) back into the DDATA_LO Register */
423 mtc0 s1, CP0_DDATA_LO
Sanjay Lalb680f702012-11-21 18:34:01 -0800424
David Daney2c07ebb2013-08-01 13:22:33 -0700425 /* Load up the Guest EBASE to minimize the window where BEV is set */
426 LONG_L t0, VCPU_GUEST_EBASE(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800427
David Daney2c07ebb2013-08-01 13:22:33 -0700428 /* Switch EBASE back to the one used by KVM */
429 mfc0 v1, CP0_STATUS
David Daney2c07ebb2013-08-01 13:22:33 -0700430 or k0, v1, ST0_BEV
David Daney2c07ebb2013-08-01 13:22:33 -0700431 mtc0 k0, CP0_STATUS
432 ehb
433 mtc0 t0, CP0_EBASE
Sanjay Lalb680f702012-11-21 18:34:01 -0800434
David Daney2c07ebb2013-08-01 13:22:33 -0700435 /* Setup status register for running guest in UM */
David Daney2c07ebb2013-08-01 13:22:33 -0700436 or v1, v1, (ST0_EXL | KSU_USER | ST0_IE)
James Hoganf7982172015-02-04 17:06:37 +0000437 and v1, v1, ~(ST0_CU0 | ST0_MX)
David Daney2c07ebb2013-08-01 13:22:33 -0700438 mtc0 v1, CP0_STATUS
439 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800440
441 /* Set Guest EPC */
David Daney2c07ebb2013-08-01 13:22:33 -0700442 LONG_L t0, VCPU_PC(k1)
443 mtc0 t0, CP0_EPC
Sanjay Lalb680f702012-11-21 18:34:01 -0800444
David Daney2c07ebb2013-08-01 13:22:33 -0700445 /* Set the ASID for the Guest Kernel */
James Hogan002374f2015-11-11 14:21:18 +0000446 PTR_L t0, VCPU_COP0(k1)
447 LONG_L t0, COP0_STATUS(t0)
448 andi t0, KSU_USER | ST0_ERL | ST0_EXL
449 xori t0, KSU_USER
450 bnez t0, 1f /* If kernel */
David Daneyea69f282013-08-01 13:22:35 -0700451 INT_ADDIU t1, k1, VCPU_GUEST_KERNEL_ASID /* (BD) */
452 INT_ADDIU t1, k1, VCPU_GUEST_USER_ASID /* else user */
Sanjay Lalb680f702012-11-21 18:34:01 -08004531:
David Daney2c07ebb2013-08-01 13:22:33 -0700454 /* t1: contains the base of the ASID array, need to get the cpu id */
455 LONG_L t2, TI_CPU($28) /* smp_processor_id */
David Daneyea69f282013-08-01 13:22:35 -0700456 INT_SLL t2, t2, 2 /* x4 */
457 REG_ADDU t3, t1, t2
David Daney2c07ebb2013-08-01 13:22:33 -0700458 LONG_L k0, (t3)
Paul Burton2db003a2016-05-06 14:36:24 +0100459#ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
460 li t3, CPUINFO_SIZE/4
461 mul t2, t2, t3 /* x sizeof(struct cpuinfo_mips)/4 */
462 LONG_L t2, (cpu_data + CPUINFO_ASID_MASK)(t2)
463 and k0, k0, t2
464#else
James Hogan9b5c3392016-05-06 14:36:19 +0100465 andi k0, k0, MIPS_ENTRYHI_ASID
Paul Burton2db003a2016-05-06 14:36:24 +0100466#endif
James Hogancaa1faa2015-12-16 23:49:26 +0000467 mtc0 k0, CP0_ENTRYHI
David Daney2c07ebb2013-08-01 13:22:33 -0700468 ehb
Sanjay Lalb680f702012-11-21 18:34:01 -0800469
David Daney2c07ebb2013-08-01 13:22:33 -0700470 /* Disable RDHWR access */
James Hogancaa1faa2015-12-16 23:49:26 +0000471 mtc0 zero, CP0_HWRENA
Sanjay Lalb680f702012-11-21 18:34:01 -0800472
James Hoganf1b711c2016-05-06 14:36:22 +0100473 .set noat
David Daney2c07ebb2013-08-01 13:22:33 -0700474 /* load the guest context from VCPU and return */
475 LONG_L $0, VCPU_R0(k1)
476 LONG_L $1, VCPU_R1(k1)
477 LONG_L $2, VCPU_R2(k1)
478 LONG_L $3, VCPU_R3(k1)
479 LONG_L $4, VCPU_R4(k1)
480 LONG_L $5, VCPU_R5(k1)
481 LONG_L $6, VCPU_R6(k1)
482 LONG_L $7, VCPU_R7(k1)
483 LONG_L $8, VCPU_R8(k1)
484 LONG_L $9, VCPU_R9(k1)
485 LONG_L $10, VCPU_R10(k1)
486 LONG_L $11, VCPU_R11(k1)
487 LONG_L $12, VCPU_R12(k1)
488 LONG_L $13, VCPU_R13(k1)
489 LONG_L $14, VCPU_R14(k1)
490 LONG_L $15, VCPU_R15(k1)
491 LONG_L $16, VCPU_R16(k1)
492 LONG_L $17, VCPU_R17(k1)
493 LONG_L $18, VCPU_R18(k1)
494 LONG_L $19, VCPU_R19(k1)
495 LONG_L $20, VCPU_R20(k1)
496 LONG_L $21, VCPU_R21(k1)
497 LONG_L $22, VCPU_R22(k1)
498 LONG_L $23, VCPU_R23(k1)
499 LONG_L $24, VCPU_R24(k1)
500 LONG_L $25, VCPU_R25(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800501
David Daney2c07ebb2013-08-01 13:22:33 -0700502 /* $/k1 loaded later */
503 LONG_L $28, VCPU_R28(k1)
504 LONG_L $29, VCPU_R29(k1)
505 LONG_L $30, VCPU_R30(k1)
506 LONG_L $31, VCPU_R31(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800507
508FEXPORT(__kvm_mips_skip_guest_restore)
David Daney2c07ebb2013-08-01 13:22:33 -0700509 LONG_L k0, VCPU_HI(k1)
510 mthi k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800511
David Daney2c07ebb2013-08-01 13:22:33 -0700512 LONG_L k0, VCPU_LO(k1)
513 mtlo k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800514
David Daney2c07ebb2013-08-01 13:22:33 -0700515 LONG_L k0, VCPU_R26(k1)
516 LONG_L k1, VCPU_R27(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800517
David Daney2c07ebb2013-08-01 13:22:33 -0700518 eret
James Hoganf1b711c2016-05-06 14:36:22 +0100519 .set at
Sanjay Lalb680f702012-11-21 18:34:01 -0800520
521__kvm_mips_return_to_host:
David Daney2c07ebb2013-08-01 13:22:33 -0700522 /* EBASE is already pointing to Linux */
523 LONG_L k1, VCPU_HOST_STACK(k1)
David Daneyea69f282013-08-01 13:22:35 -0700524 INT_ADDIU k1,k1, -PT_SIZE
Sanjay Lalb680f702012-11-21 18:34:01 -0800525
David Daney2c07ebb2013-08-01 13:22:33 -0700526 /* Restore host DDATA_LO */
527 LONG_L k0, PT_HOST_USERLOCAL(k1)
528 mtc0 k0, CP0_DDATA_LO
Sanjay Lalb680f702012-11-21 18:34:01 -0800529
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700530 /*
531 * r2/v0 is the return code, shift it down by 2 (arithmetic)
532 * to recover the err code
533 */
David Daneyea69f282013-08-01 13:22:35 -0700534 INT_SRA k0, v0, 2
David Daney2c07ebb2013-08-01 13:22:33 -0700535 move $2, k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800536
James Hogan1300fcd2016-05-06 14:36:21 +0100537 /* Load context saved on the host stack */
David Daney2c07ebb2013-08-01 13:22:33 -0700538 LONG_L $16, PT_R16(k1)
539 LONG_L $17, PT_R17(k1)
540 LONG_L $18, PT_R18(k1)
541 LONG_L $19, PT_R19(k1)
542 LONG_L $20, PT_R20(k1)
543 LONG_L $21, PT_R21(k1)
544 LONG_L $22, PT_R22(k1)
545 LONG_L $23, PT_R23(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800546
David Daney2c07ebb2013-08-01 13:22:33 -0700547 LONG_L $28, PT_R28(k1)
548 LONG_L $29, PT_R29(k1)
549 LONG_L $30, PT_R30(k1)
Sanjay Lalb680f702012-11-21 18:34:01 -0800550
David Daney2c07ebb2013-08-01 13:22:33 -0700551 LONG_L k0, PT_HI(k1)
552 mthi k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800553
David Daney2c07ebb2013-08-01 13:22:33 -0700554 LONG_L k0, PT_LO(k1)
555 mtlo k0
Sanjay Lalb680f702012-11-21 18:34:01 -0800556
David Daney2c07ebb2013-08-01 13:22:33 -0700557 /* Restore RDHWR access */
David Daneyea69f282013-08-01 13:22:35 -0700558 PTR_LI k0, 0x2000000F
James Hogancaa1faa2015-12-16 23:49:26 +0000559 mtc0 k0, CP0_HWRENA
Sanjay Lalb680f702012-11-21 18:34:01 -0800560
David Daney2c07ebb2013-08-01 13:22:33 -0700561 /* Restore RA, which is the address we will return to */
James Hogancaa1faa2015-12-16 23:49:26 +0000562 LONG_L ra, PT_R31(k1)
563 j ra
David Daney2c07ebb2013-08-01 13:22:33 -0700564 nop
Sanjay Lalb680f702012-11-21 18:34:01 -0800565
Sanjay Lalb680f702012-11-21 18:34:01 -0800566VECTOR_END(MIPSX(GuestExceptionEnd))
567.end MIPSX(GuestException)
568
569MIPSX(exceptions):
570 ####
571 ##### The exception handlers.
572 #####
573 .word _C_LABEL(MIPSX(GuestException)) # 0
574 .word _C_LABEL(MIPSX(GuestException)) # 1
575 .word _C_LABEL(MIPSX(GuestException)) # 2
576 .word _C_LABEL(MIPSX(GuestException)) # 3
577 .word _C_LABEL(MIPSX(GuestException)) # 4
578 .word _C_LABEL(MIPSX(GuestException)) # 5
579 .word _C_LABEL(MIPSX(GuestException)) # 6
580 .word _C_LABEL(MIPSX(GuestException)) # 7
581 .word _C_LABEL(MIPSX(GuestException)) # 8
582 .word _C_LABEL(MIPSX(GuestException)) # 9
583 .word _C_LABEL(MIPSX(GuestException)) # 10
584 .word _C_LABEL(MIPSX(GuestException)) # 11
585 .word _C_LABEL(MIPSX(GuestException)) # 12
586 .word _C_LABEL(MIPSX(GuestException)) # 13
587 .word _C_LABEL(MIPSX(GuestException)) # 14
588 .word _C_LABEL(MIPSX(GuestException)) # 15
589 .word _C_LABEL(MIPSX(GuestException)) # 16
590 .word _C_LABEL(MIPSX(GuestException)) # 17
591 .word _C_LABEL(MIPSX(GuestException)) # 18
592 .word _C_LABEL(MIPSX(GuestException)) # 19
593 .word _C_LABEL(MIPSX(GuestException)) # 20
594 .word _C_LABEL(MIPSX(GuestException)) # 21
595 .word _C_LABEL(MIPSX(GuestException)) # 22
596 .word _C_LABEL(MIPSX(GuestException)) # 23
597 .word _C_LABEL(MIPSX(GuestException)) # 24
598 .word _C_LABEL(MIPSX(GuestException)) # 25
599 .word _C_LABEL(MIPSX(GuestException)) # 26
600 .word _C_LABEL(MIPSX(GuestException)) # 27
601 .word _C_LABEL(MIPSX(GuestException)) # 28
602 .word _C_LABEL(MIPSX(GuestException)) # 29
603 .word _C_LABEL(MIPSX(GuestException)) # 30
604 .word _C_LABEL(MIPSX(GuestException)) # 31