blob: fb2cbf653474eb22733621b8d059c42adbaede40 [file] [log] [blame]
James Hogan90e93112016-06-23 17:34:39 +01001/*
2 * 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 * Generation of main entry point for the guest, exception handling.
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 *
11 * Copyright (C) 2016 Imagination Technologies Ltd.
12 */
13
14#include <linux/kvm_host.h>
15#include <asm/msa.h>
16#include <asm/setup.h>
17#include <asm/uasm.h>
18
19/* Register names */
20#define ZERO 0
21#define AT 1
22#define V0 2
23#define V1 3
24#define A0 4
25#define A1 5
26
27#if _MIPS_SIM == _MIPS_SIM_ABI32
28#define T0 8
29#define T1 9
30#define T2 10
31#define T3 11
32#endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */
33
34#if _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32
35#define T0 12
36#define T1 13
37#define T2 14
38#define T3 15
39#endif /* _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32 */
40
41#define S0 16
42#define S1 17
43#define T9 25
44#define K0 26
45#define K1 27
46#define GP 28
47#define SP 29
48#define RA 31
49
50/* Some CP0 registers */
51#define C0_HWRENA 7, 0
52#define C0_BADVADDR 8, 0
53#define C0_ENTRYHI 10, 0
54#define C0_STATUS 12, 0
55#define C0_CAUSE 13, 0
56#define C0_EPC 14, 0
57#define C0_EBASE 15, 1
James Hogan90e93112016-06-23 17:34:39 +010058#define C0_CONFIG5 16, 5
59#define C0_DDATA_LO 28, 3
60#define C0_ERROREPC 30, 0
61
62#define CALLFRAME_SIZ 32
63
James Hogan1e5217f52016-06-23 17:34:45 +010064static unsigned int scratch_vcpu[2] = { C0_DDATA_LO };
65static unsigned int scratch_tmp[2] = { C0_ERROREPC };
66
James Hogan90e93112016-06-23 17:34:39 +010067enum label_id {
68 label_fpu_1 = 1,
69 label_msa_1,
70 label_return_to_host,
71 label_kernel_asid,
James Hogan1f9ca622016-06-23 17:34:46 +010072 label_exit_common,
James Hogan90e93112016-06-23 17:34:39 +010073};
74
75UASM_L_LA(_fpu_1)
76UASM_L_LA(_msa_1)
77UASM_L_LA(_return_to_host)
78UASM_L_LA(_kernel_asid)
James Hogan1f9ca622016-06-23 17:34:46 +010079UASM_L_LA(_exit_common)
James Hogan90e93112016-06-23 17:34:39 +010080
81static void *kvm_mips_build_enter_guest(void *addr);
82static void *kvm_mips_build_ret_from_exit(void *addr);
83static void *kvm_mips_build_ret_to_guest(void *addr);
84static void *kvm_mips_build_ret_to_host(void *addr);
85
86/**
James Hogan1e5217f52016-06-23 17:34:45 +010087 * kvm_mips_entry_setup() - Perform global setup for entry code.
88 *
89 * Perform global setup for entry code, such as choosing a scratch register.
90 *
91 * Returns: 0 on success.
92 * -errno on failure.
93 */
94int kvm_mips_entry_setup(void)
95{
96 /*
97 * We prefer to use KScratchN registers if they are available over the
98 * defaults above, which may not work on all cores.
99 */
100 unsigned int kscratch_mask = cpu_data[0].kscratch_mask & 0xfc;
101
102 /* Pick a scratch register for storing VCPU */
103 if (kscratch_mask) {
104 scratch_vcpu[0] = 31;
105 scratch_vcpu[1] = ffs(kscratch_mask) - 1;
106 kscratch_mask &= ~BIT(scratch_vcpu[1]);
107 }
108
109 /* Pick a scratch register to use as a temp for saving state */
110 if (kscratch_mask) {
111 scratch_tmp[0] = 31;
112 scratch_tmp[1] = ffs(kscratch_mask) - 1;
113 kscratch_mask &= ~BIT(scratch_tmp[1]);
114 }
115
116 return 0;
117}
118
119static void kvm_mips_build_save_scratch(u32 **p, unsigned int tmp,
120 unsigned int frame)
121{
122 /* Save the VCPU scratch register value in cp0_epc of the stack frame */
123 uasm_i_mfc0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
124 UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
125
126 /* Save the temp scratch register value in cp0_cause of stack frame */
127 if (scratch_tmp[0] == 31) {
128 uasm_i_mfc0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
129 UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
130 }
131}
132
133static void kvm_mips_build_restore_scratch(u32 **p, unsigned int tmp,
134 unsigned int frame)
135{
136 /*
137 * Restore host scratch register values saved by
138 * kvm_mips_build_save_scratch().
139 */
140 UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
141 uasm_i_mtc0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
142
143 if (scratch_tmp[0] == 31) {
144 UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
145 uasm_i_mtc0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
146 }
147}
148
149/**
James Hogan90e93112016-06-23 17:34:39 +0100150 * kvm_mips_build_vcpu_run() - Assemble function to start running a guest VCPU.
151 * @addr: Address to start writing code.
152 *
153 * Assemble the start of the vcpu_run function to run a guest VCPU. The function
154 * conforms to the following prototype:
155 *
156 * int vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu);
157 *
158 * The exit from the guest and return to the caller is handled by the code
159 * generated by kvm_mips_build_ret_to_host().
160 *
161 * Returns: Next address after end of written function.
162 */
163void *kvm_mips_build_vcpu_run(void *addr)
164{
165 u32 *p = addr;
166 unsigned int i;
167
168 /*
169 * A0: run
170 * A1: vcpu
171 */
172
173 /* k0/k1 not being used in host kernel context */
174 uasm_i_addiu(&p, K1, SP, -(int)sizeof(struct pt_regs));
175 for (i = 16; i < 32; ++i) {
176 if (i == 24)
177 i = 28;
178 UASM_i_SW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
179 }
180
181 /* Save hi/lo */
182 uasm_i_mflo(&p, V0);
183 UASM_i_SW(&p, V0, offsetof(struct pt_regs, lo), K1);
184 uasm_i_mfhi(&p, V1);
185 UASM_i_SW(&p, V1, offsetof(struct pt_regs, hi), K1);
186
187 /* Save host status */
188 uasm_i_mfc0(&p, V0, C0_STATUS);
189 UASM_i_SW(&p, V0, offsetof(struct pt_regs, cp0_status), K1);
190
James Hogan1e5217f52016-06-23 17:34:45 +0100191 /* Save scratch registers, will be used to store pointer to vcpu etc */
192 kvm_mips_build_save_scratch(&p, V1, K1);
James Hogan90e93112016-06-23 17:34:39 +0100193
James Hogan1e5217f52016-06-23 17:34:45 +0100194 /* VCPU scratch register has pointer to vcpu */
195 uasm_i_mtc0(&p, A1, scratch_vcpu[0], scratch_vcpu[1]);
James Hogan90e93112016-06-23 17:34:39 +0100196
197 /* Offset into vcpu->arch */
198 uasm_i_addiu(&p, K1, A1, offsetof(struct kvm_vcpu, arch));
199
200 /*
201 * Save the host stack to VCPU, used for exception processing
202 * when we exit from the Guest
203 */
204 UASM_i_SW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
205
206 /* Save the kernel gp as well */
207 UASM_i_SW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
208
209 /*
210 * Setup status register for running the guest in UM, interrupts
211 * are disabled
212 */
213 UASM_i_LA(&p, K0, ST0_EXL | KSU_USER | ST0_BEV);
214 uasm_i_mtc0(&p, K0, C0_STATUS);
215 uasm_i_ehb(&p);
216
217 /* load up the new EBASE */
218 UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
219 uasm_i_mtc0(&p, K0, C0_EBASE);
220
221 /*
222 * Now that the new EBASE has been loaded, unset BEV, set
223 * interrupt mask as it was but make sure that timer interrupts
224 * are enabled
225 */
226 uasm_i_addiu(&p, K0, ZERO, ST0_EXL | KSU_USER | ST0_IE);
227 uasm_i_andi(&p, V0, V0, ST0_IM);
228 uasm_i_or(&p, K0, K0, V0);
229 uasm_i_mtc0(&p, K0, C0_STATUS);
230 uasm_i_ehb(&p);
231
232 p = kvm_mips_build_enter_guest(p);
233
234 return p;
235}
236
237/**
238 * kvm_mips_build_enter_guest() - Assemble code to resume guest execution.
239 * @addr: Address to start writing code.
240 *
241 * Assemble the code to resume guest execution. This code is common between the
242 * initial entry into the guest from the host, and returning from the exit
243 * handler back to the guest.
244 *
245 * Returns: Next address after end of written function.
246 */
247static void *kvm_mips_build_enter_guest(void *addr)
248{
249 u32 *p = addr;
250 unsigned int i;
251 struct uasm_label labels[2];
252 struct uasm_reloc relocs[2];
253 struct uasm_label *l = labels;
254 struct uasm_reloc *r = relocs;
255
256 memset(labels, 0, sizeof(labels));
257 memset(relocs, 0, sizeof(relocs));
258
259 /* Set Guest EPC */
260 UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, pc), K1);
261 uasm_i_mtc0(&p, T0, C0_EPC);
262
263 /* Set the ASID for the Guest Kernel */
264 UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, cop0), K1);
265 UASM_i_LW(&p, T0, offsetof(struct mips_coproc, reg[MIPS_CP0_STATUS][0]),
266 T0);
267 uasm_i_andi(&p, T0, T0, KSU_USER | ST0_ERL | ST0_EXL);
268 uasm_i_xori(&p, T0, T0, KSU_USER);
269 uasm_il_bnez(&p, &r, T0, label_kernel_asid);
270 uasm_i_addiu(&p, T1, K1,
271 offsetof(struct kvm_vcpu_arch, guest_kernel_asid));
272 /* else user */
273 uasm_i_addiu(&p, T1, K1,
274 offsetof(struct kvm_vcpu_arch, guest_user_asid));
275 uasm_l_kernel_asid(&l, p);
276
277 /* t1: contains the base of the ASID array, need to get the cpu id */
278 /* smp_processor_id */
279 UASM_i_LW(&p, T2, offsetof(struct thread_info, cpu), GP);
280 /* x4 */
281 uasm_i_sll(&p, T2, T2, 2);
282 UASM_i_ADDU(&p, T3, T1, T2);
283 UASM_i_LW(&p, K0, 0, T3);
284#ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
285 /* x sizeof(struct cpuinfo_mips)/4 */
286 uasm_i_addiu(&p, T3, ZERO, sizeof(struct cpuinfo_mips)/4);
287 uasm_i_mul(&p, T2, T2, T3);
288
289 UASM_i_LA_mostly(&p, AT, (long)&cpu_data[0].asid_mask);
290 UASM_i_ADDU(&p, AT, AT, T2);
291 UASM_i_LW(&p, T2, uasm_rel_lo((long)&cpu_data[0].asid_mask), AT);
292 uasm_i_and(&p, K0, K0, T2);
293#else
294 uasm_i_andi(&p, K0, K0, MIPS_ENTRYHI_ASID);
295#endif
296 uasm_i_mtc0(&p, K0, C0_ENTRYHI);
297 uasm_i_ehb(&p);
298
299 /* Disable RDHWR access */
300 uasm_i_mtc0(&p, ZERO, C0_HWRENA);
301
302 /* load the guest context from VCPU and return */
303 for (i = 1; i < 32; ++i) {
304 /* Guest k0/k1 loaded later */
305 if (i == K0 || i == K1)
306 continue;
307 UASM_i_LW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
308 }
309
310 /* Restore hi/lo */
311 UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, hi), K1);
312 uasm_i_mthi(&p, K0);
313
314 UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, lo), K1);
315 uasm_i_mtlo(&p, K0);
316
317 /* Restore the guest's k0/k1 registers */
318 UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
319 UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
320
321 /* Jump to guest */
322 uasm_i_eret(&p);
323
324 uasm_resolve_relocs(relocs, labels);
325
326 return p;
327}
328
329/**
330 * kvm_mips_build_exception() - Assemble first level guest exception handler.
331 * @addr: Address to start writing code.
James Hogan1f9ca622016-06-23 17:34:46 +0100332 * @handler: Address of common handler (within range of @addr).
James Hogan90e93112016-06-23 17:34:39 +0100333 *
334 * Assemble exception vector code for guest execution. The generated vector will
James Hogan1f9ca622016-06-23 17:34:46 +0100335 * branch to the common exception handler generated by kvm_mips_build_exit().
James Hogan90e93112016-06-23 17:34:39 +0100336 *
337 * Returns: Next address after end of written function.
338 */
James Hogan1f9ca622016-06-23 17:34:46 +0100339void *kvm_mips_build_exception(void *addr, void *handler)
James Hogan90e93112016-06-23 17:34:39 +0100340{
341 u32 *p = addr;
James Hogan1f9ca622016-06-23 17:34:46 +0100342 struct uasm_label labels[2];
343 struct uasm_reloc relocs[2];
344 struct uasm_label *l = labels;
345 struct uasm_reloc *r = relocs;
346
347 memset(labels, 0, sizeof(labels));
348 memset(relocs, 0, sizeof(relocs));
James Hogan90e93112016-06-23 17:34:39 +0100349
350 /* Save guest k0 */
James Hogan1e5217f52016-06-23 17:34:45 +0100351 uasm_i_mtc0(&p, K0, scratch_tmp[0], scratch_tmp[1]);
James Hogan90e93112016-06-23 17:34:39 +0100352 uasm_i_ehb(&p);
353
354 /* Get EBASE */
355 uasm_i_mfc0(&p, K0, C0_EBASE);
356 /* Get rid of CPUNum */
357 uasm_i_srl(&p, K0, K0, 10);
358 uasm_i_sll(&p, K0, K0, 10);
359 /* Save k1 @ offset 0x3000 */
360 UASM_i_SW(&p, K1, 0x3000, K0);
361
James Hogan1f9ca622016-06-23 17:34:46 +0100362 /* Branch to the common handler */
363 uasm_il_b(&p, &r, label_exit_common);
James Hogan90e93112016-06-23 17:34:39 +0100364 uasm_i_nop(&p);
365
James Hogan1f9ca622016-06-23 17:34:46 +0100366 uasm_l_exit_common(&l, handler);
367 uasm_resolve_relocs(relocs, labels);
368
James Hogan90e93112016-06-23 17:34:39 +0100369 return p;
370}
371
372/**
373 * kvm_mips_build_exit() - Assemble common guest exit handler.
374 * @addr: Address to start writing code.
375 *
376 * Assemble the generic guest exit handling code. This is called by the
377 * exception vectors (generated by kvm_mips_build_exception()), and calls
378 * kvm_mips_handle_exit(), then either resumes the guest or returns to the host
379 * depending on the return value.
380 *
381 * Returns: Next address after end of written function.
382 */
383void *kvm_mips_build_exit(void *addr)
384{
385 u32 *p = addr;
386 unsigned int i;
387 struct uasm_label labels[3];
388 struct uasm_reloc relocs[3];
389 struct uasm_label *l = labels;
390 struct uasm_reloc *r = relocs;
391
392 memset(labels, 0, sizeof(labels));
393 memset(relocs, 0, sizeof(relocs));
394
395 /*
396 * Generic Guest exception handler. We end up here when the guest
397 * does something that causes a trap to kernel mode.
398 */
399
James Hogan1e5217f52016-06-23 17:34:45 +0100400 /* Get the VCPU pointer from the scratch register */
401 uasm_i_mfc0(&p, K1, scratch_vcpu[0], scratch_vcpu[1]);
James Hogan90e93112016-06-23 17:34:39 +0100402 uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
403
404 /* Start saving Guest context to VCPU */
405 for (i = 0; i < 32; ++i) {
406 /* Guest k0/k1 saved later */
407 if (i == K0 || i == K1)
408 continue;
409 UASM_i_SW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
410 }
411
412 /* We need to save hi/lo and restore them on the way out */
413 uasm_i_mfhi(&p, T0);
414 UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, hi), K1);
415
416 uasm_i_mflo(&p, T0);
417 UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, lo), K1);
418
419 /* Finally save guest k0/k1 to VCPU */
James Hogan1e5217f52016-06-23 17:34:45 +0100420 uasm_i_mfc0(&p, T0, scratch_tmp[0], scratch_tmp[1]);
James Hogan90e93112016-06-23 17:34:39 +0100421 UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
422
423 /* Get GUEST k1 and save it in VCPU */
424 uasm_i_addiu(&p, T1, ZERO, ~0x2ff);
425 uasm_i_mfc0(&p, T0, C0_EBASE);
426 uasm_i_and(&p, T0, T0, T1);
427 UASM_i_LW(&p, T0, 0x3000, T0);
428 UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
429
430 /* Now that context has been saved, we can use other registers */
431
432 /* Restore vcpu */
James Hogan1e5217f52016-06-23 17:34:45 +0100433 uasm_i_mfc0(&p, A1, scratch_vcpu[0], scratch_vcpu[1]);
James Hogan90e93112016-06-23 17:34:39 +0100434 uasm_i_move(&p, S1, A1);
435
436 /* Restore run (vcpu->run) */
437 UASM_i_LW(&p, A0, offsetof(struct kvm_vcpu, run), A1);
438 /* Save pointer to run in s0, will be saved by the compiler */
439 uasm_i_move(&p, S0, A0);
440
441 /*
442 * Save Host level EPC, BadVaddr and Cause to VCPU, useful to process
443 * the exception
444 */
445 uasm_i_mfc0(&p, K0, C0_EPC);
446 UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, pc), K1);
447
448 uasm_i_mfc0(&p, K0, C0_BADVADDR);
449 UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_badvaddr),
450 K1);
451
452 uasm_i_mfc0(&p, K0, C0_CAUSE);
453 uasm_i_sw(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_cause), K1);
454
455 /* Now restore the host state just enough to run the handlers */
456
457 /* Switch EBASE to the one used by Linux */
458 /* load up the host EBASE */
459 uasm_i_mfc0(&p, V0, C0_STATUS);
460
461 uasm_i_lui(&p, AT, ST0_BEV >> 16);
462 uasm_i_or(&p, K0, V0, AT);
463
464 uasm_i_mtc0(&p, K0, C0_STATUS);
465 uasm_i_ehb(&p);
466
467 UASM_i_LA_mostly(&p, K0, (long)&ebase);
468 UASM_i_LW(&p, K0, uasm_rel_lo((long)&ebase), K0);
469 uasm_i_mtc0(&p, K0, C0_EBASE);
470
James Hogand37f4032016-06-23 17:34:42 +0100471 if (raw_cpu_has_fpu) {
472 /*
473 * If FPU is enabled, save FCR31 and clear it so that later
474 * ctc1's don't trigger FPE for pending exceptions.
475 */
476 uasm_i_lui(&p, AT, ST0_CU1 >> 16);
477 uasm_i_and(&p, V1, V0, AT);
478 uasm_il_beqz(&p, &r, V1, label_fpu_1);
479 uasm_i_nop(&p);
480 uasm_i_cfc1(&p, T0, 31);
481 uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.fcr31),
482 K1);
483 uasm_i_ctc1(&p, ZERO, 31);
484 uasm_l_fpu_1(&l, p);
485 }
James Hogan90e93112016-06-23 17:34:39 +0100486
James Hogan38ea7a72016-06-23 17:34:43 +0100487 if (cpu_has_msa) {
488 /*
489 * If MSA is enabled, save MSACSR and clear it so that later
490 * instructions don't trigger MSAFPE for pending exceptions.
491 */
492 uasm_i_mfc0(&p, T0, C0_CONFIG5);
493 uasm_i_ext(&p, T0, T0, 27, 1); /* MIPS_CONF5_MSAEN */
494 uasm_il_beqz(&p, &r, T0, label_msa_1);
495 uasm_i_nop(&p);
496 uasm_i_cfcmsa(&p, T0, MSA_CSR);
497 uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.msacsr),
498 K1);
499 uasm_i_ctcmsa(&p, MSA_CSR, ZERO);
500 uasm_l_msa_1(&l, p);
501 }
James Hogan90e93112016-06-23 17:34:39 +0100502
503 /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
504 uasm_i_addiu(&p, AT, ZERO, ~(ST0_EXL | KSU_USER | ST0_IE));
505 uasm_i_and(&p, V0, V0, AT);
506 uasm_i_lui(&p, AT, ST0_CU0 >> 16);
507 uasm_i_or(&p, V0, V0, AT);
508 uasm_i_mtc0(&p, V0, C0_STATUS);
509 uasm_i_ehb(&p);
510
511 /* Load up host GP */
512 UASM_i_LW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
513
514 /* Need a stack before we can jump to "C" */
515 UASM_i_LW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
516
517 /* Saved host state */
518 uasm_i_addiu(&p, SP, SP, -(int)sizeof(struct pt_regs));
519
520 /*
521 * XXXKYMA do we need to load the host ASID, maybe not because the
522 * kernel entries are marked GLOBAL, need to verify
523 */
524
James Hogan1e5217f52016-06-23 17:34:45 +0100525 /* Restore host scratch registers, as we'll have clobbered them */
526 kvm_mips_build_restore_scratch(&p, K0, SP);
James Hogan90e93112016-06-23 17:34:39 +0100527
528 /* Restore RDHWR access */
529 UASM_i_LA_mostly(&p, K0, (long)&hwrena);
530 uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
531 uasm_i_mtc0(&p, K0, C0_HWRENA);
532
533 /* Jump to handler */
534 /*
535 * XXXKYMA: not sure if this is safe, how large is the stack??
536 * Now jump to the kvm_mips_handle_exit() to see if we can deal
537 * with this in the kernel
538 */
539 UASM_i_LA(&p, T9, (unsigned long)kvm_mips_handle_exit);
540 uasm_i_jalr(&p, RA, T9);
541 uasm_i_addiu(&p, SP, SP, -CALLFRAME_SIZ);
542
543 uasm_resolve_relocs(relocs, labels);
544
545 p = kvm_mips_build_ret_from_exit(p);
546
547 return p;
548}
549
550/**
551 * kvm_mips_build_ret_from_exit() - Assemble guest exit return handler.
552 * @addr: Address to start writing code.
553 *
554 * Assemble the code to handle the return from kvm_mips_handle_exit(), either
555 * resuming the guest or returning to the host depending on the return value.
556 *
557 * Returns: Next address after end of written function.
558 */
559static void *kvm_mips_build_ret_from_exit(void *addr)
560{
561 u32 *p = addr;
562 struct uasm_label labels[2];
563 struct uasm_reloc relocs[2];
564 struct uasm_label *l = labels;
565 struct uasm_reloc *r = relocs;
566
567 memset(labels, 0, sizeof(labels));
568 memset(relocs, 0, sizeof(relocs));
569
570 /* Return from handler Make sure interrupts are disabled */
571 uasm_i_di(&p, ZERO);
572 uasm_i_ehb(&p);
573
574 /*
575 * XXXKYMA: k0/k1 could have been blown away if we processed
576 * an exception while we were handling the exception from the
577 * guest, reload k1
578 */
579
580 uasm_i_move(&p, K1, S1);
581 uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
582
583 /*
584 * Check return value, should tell us if we are returning to the
585 * host (handle I/O etc)or resuming the guest
586 */
587 uasm_i_andi(&p, T0, V0, RESUME_HOST);
588 uasm_il_bnez(&p, &r, T0, label_return_to_host);
589 uasm_i_nop(&p);
590
591 p = kvm_mips_build_ret_to_guest(p);
592
593 uasm_l_return_to_host(&l, p);
594 p = kvm_mips_build_ret_to_host(p);
595
596 uasm_resolve_relocs(relocs, labels);
597
598 return p;
599}
600
601/**
602 * kvm_mips_build_ret_to_guest() - Assemble code to return to the guest.
603 * @addr: Address to start writing code.
604 *
605 * Assemble the code to handle return from the guest exit handler
606 * (kvm_mips_handle_exit()) back to the guest.
607 *
608 * Returns: Next address after end of written function.
609 */
610static void *kvm_mips_build_ret_to_guest(void *addr)
611{
612 u32 *p = addr;
613
James Hogan1e5217f52016-06-23 17:34:45 +0100614 /* Put the saved pointer to vcpu (s1) back into the scratch register */
615 uasm_i_mtc0(&p, S1, scratch_vcpu[0], scratch_vcpu[1]);
James Hogan90e93112016-06-23 17:34:39 +0100616
617 /* Load up the Guest EBASE to minimize the window where BEV is set */
618 UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
619
620 /* Switch EBASE back to the one used by KVM */
621 uasm_i_mfc0(&p, V1, C0_STATUS);
622 uasm_i_lui(&p, AT, ST0_BEV >> 16);
623 uasm_i_or(&p, K0, V1, AT);
624 uasm_i_mtc0(&p, K0, C0_STATUS);
625 uasm_i_ehb(&p);
626 uasm_i_mtc0(&p, T0, C0_EBASE);
627
628 /* Setup status register for running guest in UM */
629 uasm_i_ori(&p, V1, V1, ST0_EXL | KSU_USER | ST0_IE);
630 UASM_i_LA(&p, AT, ~(ST0_CU0 | ST0_MX));
631 uasm_i_and(&p, V1, V1, AT);
632 uasm_i_mtc0(&p, V1, C0_STATUS);
633 uasm_i_ehb(&p);
634
635 p = kvm_mips_build_enter_guest(p);
636
637 return p;
638}
639
640/**
641 * kvm_mips_build_ret_to_host() - Assemble code to return to the host.
642 * @addr: Address to start writing code.
643 *
644 * Assemble the code to handle return from the guest exit handler
645 * (kvm_mips_handle_exit()) back to the host, i.e. to the caller of the vcpu_run
646 * function generated by kvm_mips_build_vcpu_run().
647 *
648 * Returns: Next address after end of written function.
649 */
650static void *kvm_mips_build_ret_to_host(void *addr)
651{
652 u32 *p = addr;
653 unsigned int i;
654
655 /* EBASE is already pointing to Linux */
656 UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, host_stack), K1);
657 uasm_i_addiu(&p, K1, K1, -(int)sizeof(struct pt_regs));
658
James Hogan90e93112016-06-23 17:34:39 +0100659 /*
660 * r2/v0 is the return code, shift it down by 2 (arithmetic)
661 * to recover the err code
662 */
663 uasm_i_sra(&p, K0, V0, 2);
664 uasm_i_move(&p, V0, K0);
665
666 /* Load context saved on the host stack */
667 for (i = 16; i < 31; ++i) {
668 if (i == 24)
669 i = 28;
670 UASM_i_LW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
671 }
672
673 UASM_i_LW(&p, K0, offsetof(struct pt_regs, hi), K1);
674 uasm_i_mthi(&p, K0);
675
676 UASM_i_LW(&p, K0, offsetof(struct pt_regs, lo), K1);
677 uasm_i_mtlo(&p, K0);
678
679 /* Restore RDHWR access */
680 UASM_i_LA_mostly(&p, K0, (long)&hwrena);
681 uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
682 uasm_i_mtc0(&p, K0, C0_HWRENA);
683
684 /* Restore RA, which is the address we will return to */
685 UASM_i_LW(&p, RA, offsetof(struct pt_regs, regs[RA]), K1);
686 uasm_i_jr(&p, RA);
687 uasm_i_nop(&p);
688
689 return p;
690}
691