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