]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/mips/kvm/entry.c
c0d9f551c1c18f589a5207dae21f928a066a4a53
[karo-tx-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 <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
58 #define C0_CONFIG3      16, 3
59 #define C0_CONFIG5      16, 5
60 #define C0_DDATA_LO     28, 3
61 #define C0_ERROREPC     30, 0
62
63 #define CALLFRAME_SIZ   32
64
65 enum label_id {
66         label_fpu_1 = 1,
67         label_msa_1,
68         label_return_to_host,
69         label_kernel_asid,
70 };
71
72 UASM_L_LA(_fpu_1)
73 UASM_L_LA(_msa_1)
74 UASM_L_LA(_return_to_host)
75 UASM_L_LA(_kernel_asid)
76
77 static void *kvm_mips_build_enter_guest(void *addr);
78 static void *kvm_mips_build_ret_from_exit(void *addr);
79 static void *kvm_mips_build_ret_to_guest(void *addr);
80 static void *kvm_mips_build_ret_to_host(void *addr);
81
82 /**
83  * kvm_mips_build_vcpu_run() - Assemble function to start running a guest VCPU.
84  * @addr:       Address to start writing code.
85  *
86  * Assemble the start of the vcpu_run function to run a guest VCPU. The function
87  * conforms to the following prototype:
88  *
89  * int vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu);
90  *
91  * The exit from the guest and return to the caller is handled by the code
92  * generated by kvm_mips_build_ret_to_host().
93  *
94  * Returns:     Next address after end of written function.
95  */
96 void *kvm_mips_build_vcpu_run(void *addr)
97 {
98         u32 *p = addr;
99         unsigned int i;
100
101         /*
102          * A0: run
103          * A1: vcpu
104          */
105
106         /* k0/k1 not being used in host kernel context */
107         uasm_i_addiu(&p, K1, SP, -(int)sizeof(struct pt_regs));
108         for (i = 16; i < 32; ++i) {
109                 if (i == 24)
110                         i = 28;
111                 UASM_i_SW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
112         }
113
114         /* Save hi/lo */
115         uasm_i_mflo(&p, V0);
116         UASM_i_SW(&p, V0, offsetof(struct pt_regs, lo), K1);
117         uasm_i_mfhi(&p, V1);
118         UASM_i_SW(&p, V1, offsetof(struct pt_regs, hi), K1);
119
120         /* Save host status */
121         uasm_i_mfc0(&p, V0, C0_STATUS);
122         UASM_i_SW(&p, V0, offsetof(struct pt_regs, cp0_status), K1);
123
124         /* Save DDATA_LO, will be used to store pointer to vcpu */
125         uasm_i_mfc0(&p, V1, C0_DDATA_LO);
126         UASM_i_SW(&p, V1, offsetof(struct pt_regs, cp0_epc), K1);
127
128         /* DDATA_LO has pointer to vcpu */
129         uasm_i_mtc0(&p, A1, C0_DDATA_LO);
130
131         /* Offset into vcpu->arch */
132         uasm_i_addiu(&p, K1, A1, offsetof(struct kvm_vcpu, arch));
133
134         /*
135          * Save the host stack to VCPU, used for exception processing
136          * when we exit from the Guest
137          */
138         UASM_i_SW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
139
140         /* Save the kernel gp as well */
141         UASM_i_SW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
142
143         /*
144          * Setup status register for running the guest in UM, interrupts
145          * are disabled
146          */
147         UASM_i_LA(&p, K0, ST0_EXL | KSU_USER | ST0_BEV);
148         uasm_i_mtc0(&p, K0, C0_STATUS);
149         uasm_i_ehb(&p);
150
151         /* load up the new EBASE */
152         UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
153         uasm_i_mtc0(&p, K0, C0_EBASE);
154
155         /*
156          * Now that the new EBASE has been loaded, unset BEV, set
157          * interrupt mask as it was but make sure that timer interrupts
158          * are enabled
159          */
160         uasm_i_addiu(&p, K0, ZERO, ST0_EXL | KSU_USER | ST0_IE);
161         uasm_i_andi(&p, V0, V0, ST0_IM);
162         uasm_i_or(&p, K0, K0, V0);
163         uasm_i_mtc0(&p, K0, C0_STATUS);
164         uasm_i_ehb(&p);
165
166         p = kvm_mips_build_enter_guest(p);
167
168         return p;
169 }
170
171 /**
172  * kvm_mips_build_enter_guest() - Assemble code to resume guest execution.
173  * @addr:       Address to start writing code.
174  *
175  * Assemble the code to resume guest execution. This code is common between the
176  * initial entry into the guest from the host, and returning from the exit
177  * handler back to the guest.
178  *
179  * Returns:     Next address after end of written function.
180  */
181 static void *kvm_mips_build_enter_guest(void *addr)
182 {
183         u32 *p = addr;
184         unsigned int i;
185         struct uasm_label labels[2];
186         struct uasm_reloc relocs[2];
187         struct uasm_label *l = labels;
188         struct uasm_reloc *r = relocs;
189
190         memset(labels, 0, sizeof(labels));
191         memset(relocs, 0, sizeof(relocs));
192
193         /* Set Guest EPC */
194         UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, pc), K1);
195         uasm_i_mtc0(&p, T0, C0_EPC);
196
197         /* Set the ASID for the Guest Kernel */
198         UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, cop0), K1);
199         UASM_i_LW(&p, T0, offsetof(struct mips_coproc, reg[MIPS_CP0_STATUS][0]),
200                   T0);
201         uasm_i_andi(&p, T0, T0, KSU_USER | ST0_ERL | ST0_EXL);
202         uasm_i_xori(&p, T0, T0, KSU_USER);
203         uasm_il_bnez(&p, &r, T0, label_kernel_asid);
204          uasm_i_addiu(&p, T1, K1,
205                       offsetof(struct kvm_vcpu_arch, guest_kernel_asid));
206         /* else user */
207         uasm_i_addiu(&p, T1, K1,
208                      offsetof(struct kvm_vcpu_arch, guest_user_asid));
209         uasm_l_kernel_asid(&l, p);
210
211         /* t1: contains the base of the ASID array, need to get the cpu id  */
212         /* smp_processor_id */
213         UASM_i_LW(&p, T2, offsetof(struct thread_info, cpu), GP);
214         /* x4 */
215         uasm_i_sll(&p, T2, T2, 2);
216         UASM_i_ADDU(&p, T3, T1, T2);
217         UASM_i_LW(&p, K0, 0, T3);
218 #ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
219         /* x sizeof(struct cpuinfo_mips)/4 */
220         uasm_i_addiu(&p, T3, ZERO, sizeof(struct cpuinfo_mips)/4);
221         uasm_i_mul(&p, T2, T2, T3);
222
223         UASM_i_LA_mostly(&p, AT, (long)&cpu_data[0].asid_mask);
224         UASM_i_ADDU(&p, AT, AT, T2);
225         UASM_i_LW(&p, T2, uasm_rel_lo((long)&cpu_data[0].asid_mask), AT);
226         uasm_i_and(&p, K0, K0, T2);
227 #else
228         uasm_i_andi(&p, K0, K0, MIPS_ENTRYHI_ASID);
229 #endif
230         uasm_i_mtc0(&p, K0, C0_ENTRYHI);
231         uasm_i_ehb(&p);
232
233         /* Disable RDHWR access */
234         uasm_i_mtc0(&p, ZERO, C0_HWRENA);
235
236         /* load the guest context from VCPU and return */
237         for (i = 1; i < 32; ++i) {
238                 /* Guest k0/k1 loaded later */
239                 if (i == K0 || i == K1)
240                         continue;
241                 UASM_i_LW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
242         }
243
244         /* Restore hi/lo */
245         UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, hi), K1);
246         uasm_i_mthi(&p, K0);
247
248         UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, lo), K1);
249         uasm_i_mtlo(&p, K0);
250
251         /* Restore the guest's k0/k1 registers */
252         UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
253         UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
254
255         /* Jump to guest */
256         uasm_i_eret(&p);
257
258         uasm_resolve_relocs(relocs, labels);
259
260         return p;
261 }
262
263 /**
264  * kvm_mips_build_exception() - Assemble first level guest exception handler.
265  * @addr:       Address to start writing code.
266  *
267  * Assemble exception vector code for guest execution. The generated vector will
268  * jump to the common exception handler generated by kvm_mips_build_exit().
269  *
270  * Returns:     Next address after end of written function.
271  */
272 void *kvm_mips_build_exception(void *addr)
273 {
274         u32 *p = addr;
275
276         /* Save guest k0 */
277         uasm_i_mtc0(&p, K0, C0_ERROREPC);
278         uasm_i_ehb(&p);
279
280         /* Get EBASE */
281         uasm_i_mfc0(&p, K0, C0_EBASE);
282         /* Get rid of CPUNum */
283         uasm_i_srl(&p, K0, K0, 10);
284         uasm_i_sll(&p, K0, K0, 10);
285         /* Save k1 @ offset 0x3000 */
286         UASM_i_SW(&p, K1, 0x3000, K0);
287
288         /* Exception handler is installed @ offset 0x2000 */
289         uasm_i_addiu(&p, K0, K0, 0x2000);
290         /* Jump to the function */
291         uasm_i_jr(&p, K0);
292          uasm_i_nop(&p);
293
294         return p;
295 }
296
297 /**
298  * kvm_mips_build_exit() - Assemble common guest exit handler.
299  * @addr:       Address to start writing code.
300  *
301  * Assemble the generic guest exit handling code. This is called by the
302  * exception vectors (generated by kvm_mips_build_exception()), and calls
303  * kvm_mips_handle_exit(), then either resumes the guest or returns to the host
304  * depending on the return value.
305  *
306  * Returns:     Next address after end of written function.
307  */
308 void *kvm_mips_build_exit(void *addr)
309 {
310         u32 *p = addr;
311         unsigned int i;
312         struct uasm_label labels[3];
313         struct uasm_reloc relocs[3];
314         struct uasm_label *l = labels;
315         struct uasm_reloc *r = relocs;
316
317         memset(labels, 0, sizeof(labels));
318         memset(relocs, 0, sizeof(relocs));
319
320         /*
321          * Generic Guest exception handler. We end up here when the guest
322          * does something that causes a trap to kernel mode.
323          */
324
325         /* Get the VCPU pointer from DDATA_LO */
326         uasm_i_mfc0(&p, K1, C0_DDATA_LO);
327         uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
328
329         /* Start saving Guest context to VCPU */
330         for (i = 0; i < 32; ++i) {
331                 /* Guest k0/k1 saved later */
332                 if (i == K0 || i == K1)
333                         continue;
334                 UASM_i_SW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
335         }
336
337         /* We need to save hi/lo and restore them on the way out */
338         uasm_i_mfhi(&p, T0);
339         UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, hi), K1);
340
341         uasm_i_mflo(&p, T0);
342         UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, lo), K1);
343
344         /* Finally save guest k0/k1 to VCPU */
345         uasm_i_mfc0(&p, T0, C0_ERROREPC);
346         UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
347
348         /* Get GUEST k1 and save it in VCPU */
349         uasm_i_addiu(&p, T1, ZERO, ~0x2ff);
350         uasm_i_mfc0(&p, T0, C0_EBASE);
351         uasm_i_and(&p, T0, T0, T1);
352         UASM_i_LW(&p, T0, 0x3000, T0);
353         UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
354
355         /* Now that context has been saved, we can use other registers */
356
357         /* Restore vcpu */
358         uasm_i_mfc0(&p, A1, C0_DDATA_LO);
359         uasm_i_move(&p, S1, A1);
360
361         /* Restore run (vcpu->run) */
362         UASM_i_LW(&p, A0, offsetof(struct kvm_vcpu, run), A1);
363         /* Save pointer to run in s0, will be saved by the compiler */
364         uasm_i_move(&p, S0, A0);
365
366         /*
367          * Save Host level EPC, BadVaddr and Cause to VCPU, useful to process
368          * the exception
369          */
370         uasm_i_mfc0(&p, K0, C0_EPC);
371         UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, pc), K1);
372
373         uasm_i_mfc0(&p, K0, C0_BADVADDR);
374         UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_badvaddr),
375                   K1);
376
377         uasm_i_mfc0(&p, K0, C0_CAUSE);
378         uasm_i_sw(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_cause), K1);
379
380         /* Now restore the host state just enough to run the handlers */
381
382         /* Switch EBASE to the one used by Linux */
383         /* load up the host EBASE */
384         uasm_i_mfc0(&p, V0, C0_STATUS);
385
386         uasm_i_lui(&p, AT, ST0_BEV >> 16);
387         uasm_i_or(&p, K0, V0, AT);
388
389         uasm_i_mtc0(&p, K0, C0_STATUS);
390         uasm_i_ehb(&p);
391
392         UASM_i_LA_mostly(&p, K0, (long)&ebase);
393         UASM_i_LW(&p, K0, uasm_rel_lo((long)&ebase), K0);
394         uasm_i_mtc0(&p, K0, C0_EBASE);
395
396         if (raw_cpu_has_fpu) {
397                 /*
398                  * If FPU is enabled, save FCR31 and clear it so that later
399                  * ctc1's don't trigger FPE for pending exceptions.
400                  */
401                 uasm_i_lui(&p, AT, ST0_CU1 >> 16);
402                 uasm_i_and(&p, V1, V0, AT);
403                 uasm_il_beqz(&p, &r, V1, label_fpu_1);
404                  uasm_i_nop(&p);
405                 uasm_i_cfc1(&p, T0, 31);
406                 uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.fcr31),
407                           K1);
408                 uasm_i_ctc1(&p, ZERO, 31);
409                 uasm_l_fpu_1(&l, p);
410         }
411
412 #ifdef CONFIG_CPU_HAS_MSA
413         /*
414          * If MSA is enabled, save MSACSR and clear it so that later
415          * instructions don't trigger MSAFPE for pending exceptions.
416          */
417         uasm_i_mfc0(&p, T0, C0_CONFIG3);
418         uasm_i_ext(&p, T0, T0, 28, 1); /* MIPS_CONF3_MSAP */
419         uasm_il_beqz(&p, &r, T0, label_msa_1);
420          uasm_i_nop(&p);
421         uasm_i_mfc0(&p, T0, C0_CONFIG5);
422         uasm_i_ext(&p, T0, T0, 27, 1); /* MIPS_CONF5_MSAEN */
423         uasm_il_beqz(&p, &r, T0, label_msa_1);
424          uasm_i_nop(&p);
425         uasm_i_cfcmsa(&p, T0, MSA_CSR);
426         uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.msacsr),
427                   K1);
428         uasm_i_ctcmsa(&p, MSA_CSR, ZERO);
429         uasm_l_msa_1(&l, p);
430 #endif
431
432         /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
433         uasm_i_addiu(&p, AT, ZERO, ~(ST0_EXL | KSU_USER | ST0_IE));
434         uasm_i_and(&p, V0, V0, AT);
435         uasm_i_lui(&p, AT, ST0_CU0 >> 16);
436         uasm_i_or(&p, V0, V0, AT);
437         uasm_i_mtc0(&p, V0, C0_STATUS);
438         uasm_i_ehb(&p);
439
440         /* Load up host GP */
441         UASM_i_LW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
442
443         /* Need a stack before we can jump to "C" */
444         UASM_i_LW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
445
446         /* Saved host state */
447         uasm_i_addiu(&p, SP, SP, -(int)sizeof(struct pt_regs));
448
449         /*
450          * XXXKYMA do we need to load the host ASID, maybe not because the
451          * kernel entries are marked GLOBAL, need to verify
452          */
453
454         /* Restore host DDATA_LO */
455         UASM_i_LW(&p, K0, offsetof(struct pt_regs, cp0_epc), SP);
456         uasm_i_mtc0(&p, K0, C0_DDATA_LO);
457
458         /* Restore RDHWR access */
459         UASM_i_LA_mostly(&p, K0, (long)&hwrena);
460         uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
461         uasm_i_mtc0(&p, K0, C0_HWRENA);
462
463         /* Jump to handler */
464         /*
465          * XXXKYMA: not sure if this is safe, how large is the stack??
466          * Now jump to the kvm_mips_handle_exit() to see if we can deal
467          * with this in the kernel
468          */
469         UASM_i_LA(&p, T9, (unsigned long)kvm_mips_handle_exit);
470         uasm_i_jalr(&p, RA, T9);
471          uasm_i_addiu(&p, SP, SP, -CALLFRAME_SIZ);
472
473         uasm_resolve_relocs(relocs, labels);
474
475         p = kvm_mips_build_ret_from_exit(p);
476
477         return p;
478 }
479
480 /**
481  * kvm_mips_build_ret_from_exit() - Assemble guest exit return handler.
482  * @addr:       Address to start writing code.
483  *
484  * Assemble the code to handle the return from kvm_mips_handle_exit(), either
485  * resuming the guest or returning to the host depending on the return value.
486  *
487  * Returns:     Next address after end of written function.
488  */
489 static void *kvm_mips_build_ret_from_exit(void *addr)
490 {
491         u32 *p = addr;
492         struct uasm_label labels[2];
493         struct uasm_reloc relocs[2];
494         struct uasm_label *l = labels;
495         struct uasm_reloc *r = relocs;
496
497         memset(labels, 0, sizeof(labels));
498         memset(relocs, 0, sizeof(relocs));
499
500         /* Return from handler Make sure interrupts are disabled */
501         uasm_i_di(&p, ZERO);
502         uasm_i_ehb(&p);
503
504         /*
505          * XXXKYMA: k0/k1 could have been blown away if we processed
506          * an exception while we were handling the exception from the
507          * guest, reload k1
508          */
509
510         uasm_i_move(&p, K1, S1);
511         uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
512
513         /*
514          * Check return value, should tell us if we are returning to the
515          * host (handle I/O etc)or resuming the guest
516          */
517         uasm_i_andi(&p, T0, V0, RESUME_HOST);
518         uasm_il_bnez(&p, &r, T0, label_return_to_host);
519          uasm_i_nop(&p);
520
521         p = kvm_mips_build_ret_to_guest(p);
522
523         uasm_l_return_to_host(&l, p);
524         p = kvm_mips_build_ret_to_host(p);
525
526         uasm_resolve_relocs(relocs, labels);
527
528         return p;
529 }
530
531 /**
532  * kvm_mips_build_ret_to_guest() - Assemble code to return to the guest.
533  * @addr:       Address to start writing code.
534  *
535  * Assemble the code to handle return from the guest exit handler
536  * (kvm_mips_handle_exit()) back to the guest.
537  *
538  * Returns:     Next address after end of written function.
539  */
540 static void *kvm_mips_build_ret_to_guest(void *addr)
541 {
542         u32 *p = addr;
543
544         /* Put the saved pointer to vcpu (s1) back into the DDATA_LO Register */
545         uasm_i_mtc0(&p, S1, C0_DDATA_LO);
546
547         /* Load up the Guest EBASE to minimize the window where BEV is set */
548         UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
549
550         /* Switch EBASE back to the one used by KVM */
551         uasm_i_mfc0(&p, V1, C0_STATUS);
552         uasm_i_lui(&p, AT, ST0_BEV >> 16);
553         uasm_i_or(&p, K0, V1, AT);
554         uasm_i_mtc0(&p, K0, C0_STATUS);
555         uasm_i_ehb(&p);
556         uasm_i_mtc0(&p, T0, C0_EBASE);
557
558         /* Setup status register for running guest in UM */
559         uasm_i_ori(&p, V1, V1, ST0_EXL | KSU_USER | ST0_IE);
560         UASM_i_LA(&p, AT, ~(ST0_CU0 | ST0_MX));
561         uasm_i_and(&p, V1, V1, AT);
562         uasm_i_mtc0(&p, V1, C0_STATUS);
563         uasm_i_ehb(&p);
564
565         p = kvm_mips_build_enter_guest(p);
566
567         return p;
568 }
569
570 /**
571  * kvm_mips_build_ret_to_host() - Assemble code to return to the host.
572  * @addr:       Address to start writing code.
573  *
574  * Assemble the code to handle return from the guest exit handler
575  * (kvm_mips_handle_exit()) back to the host, i.e. to the caller of the vcpu_run
576  * function generated by kvm_mips_build_vcpu_run().
577  *
578  * Returns:     Next address after end of written function.
579  */
580 static void *kvm_mips_build_ret_to_host(void *addr)
581 {
582         u32 *p = addr;
583         unsigned int i;
584
585         /* EBASE is already pointing to Linux */
586         UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, host_stack), K1);
587         uasm_i_addiu(&p, K1, K1, -(int)sizeof(struct pt_regs));
588
589         /* Restore host DDATA_LO */
590         UASM_i_LW(&p, K0, offsetof(struct pt_regs, cp0_epc), K1);
591         uasm_i_mtc0(&p, K0, C0_DDATA_LO);
592
593         /*
594          * r2/v0 is the return code, shift it down by 2 (arithmetic)
595          * to recover the err code
596          */
597         uasm_i_sra(&p, K0, V0, 2);
598         uasm_i_move(&p, V0, K0);
599
600         /* Load context saved on the host stack */
601         for (i = 16; i < 31; ++i) {
602                 if (i == 24)
603                         i = 28;
604                 UASM_i_LW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
605         }
606
607         UASM_i_LW(&p, K0, offsetof(struct pt_regs, hi), K1);
608         uasm_i_mthi(&p, K0);
609
610         UASM_i_LW(&p, K0, offsetof(struct pt_regs, lo), K1);
611         uasm_i_mtlo(&p, K0);
612
613         /* Restore RDHWR access */
614         UASM_i_LA_mostly(&p, K0, (long)&hwrena);
615         uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
616         uasm_i_mtc0(&p, K0, C0_HWRENA);
617
618         /* Restore RA, which is the address we will return to */
619         UASM_i_LW(&p, RA, offsetof(struct pt_regs, regs[RA]), K1);
620         uasm_i_jr(&p, RA);
621          uasm_i_nop(&p);
622
623         return p;
624 }
625