blob: 97e7a788bf4a9d17008a9277c0a818e9c92ebdba [file] [log] [blame]
James Hoganc992a4f2017-03-14 10:15:31 +00001/*
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 * KVM/MIPS: Support for hardware virtualization extensions
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Yann Le Du <ledu@kymasys.com>
10 */
11
12#include <linux/errno.h>
13#include <linux/err.h>
14#include <linux/module.h>
15#include <linux/preempt.h>
16#include <linux/vmalloc.h>
17#include <asm/cacheflush.h>
18#include <asm/cacheops.h>
19#include <asm/cmpxchg.h>
20#include <asm/fpu.h>
21#include <asm/hazards.h>
22#include <asm/inst.h>
23#include <asm/mmu_context.h>
24#include <asm/r4kcache.h>
25#include <asm/time.h>
26#include <asm/tlb.h>
27#include <asm/tlbex.h>
28
29#include <linux/kvm_host.h>
30
31#include "interrupt.h"
32
33#include "trace.h"
34
35/* Pointers to last VCPU loaded on each physical CPU */
36static struct kvm_vcpu *last_vcpu[NR_CPUS];
37/* Pointers to last VCPU executed on each physical CPU */
38static struct kvm_vcpu *last_exec_vcpu[NR_CPUS];
39
40/*
41 * Number of guest VTLB entries to use, so we can catch inconsistency between
42 * CPUs.
43 */
44static unsigned int kvm_vz_guest_vtlb_size;
45
46static inline long kvm_vz_read_gc0_ebase(void)
47{
48 if (sizeof(long) == 8 && cpu_has_ebase_wg)
49 return read_gc0_ebase_64();
50 else
51 return read_gc0_ebase();
52}
53
54static inline void kvm_vz_write_gc0_ebase(long v)
55{
56 /*
57 * First write with WG=1 to write upper bits, then write again in case
58 * WG should be left at 0.
59 * write_gc0_ebase_64() is no longer UNDEFINED since R6.
60 */
61 if (sizeof(long) == 8 &&
62 (cpu_has_mips64r6 || cpu_has_ebase_wg)) {
63 write_gc0_ebase_64(v | MIPS_EBASE_WG);
64 write_gc0_ebase_64(v);
65 } else {
66 write_gc0_ebase(v | MIPS_EBASE_WG);
67 write_gc0_ebase(v);
68 }
69}
70
71/*
72 * These Config bits may be writable by the guest:
73 * Config: [K23, KU] (!TLB), K0
74 * Config1: (none)
75 * Config2: [TU, SU] (impl)
76 * Config3: ISAOnExc
77 * Config4: FTLBPageSize
78 * Config5: K, CV, MSAEn, UFE, FRE, SBRI, UFR
79 */
80
81static inline unsigned int kvm_vz_config_guest_wrmask(struct kvm_vcpu *vcpu)
82{
83 return CONF_CM_CMASK;
84}
85
86static inline unsigned int kvm_vz_config1_guest_wrmask(struct kvm_vcpu *vcpu)
87{
88 return 0;
89}
90
91static inline unsigned int kvm_vz_config2_guest_wrmask(struct kvm_vcpu *vcpu)
92{
93 return 0;
94}
95
96static inline unsigned int kvm_vz_config3_guest_wrmask(struct kvm_vcpu *vcpu)
97{
98 return MIPS_CONF3_ISA_OE;
99}
100
101static inline unsigned int kvm_vz_config4_guest_wrmask(struct kvm_vcpu *vcpu)
102{
103 /* no need to be exact */
104 return MIPS_CONF4_VFTLBPAGESIZE;
105}
106
107static inline unsigned int kvm_vz_config5_guest_wrmask(struct kvm_vcpu *vcpu)
108{
109 unsigned int mask = MIPS_CONF5_K | MIPS_CONF5_CV | MIPS_CONF5_SBRI;
110
111 /* Permit MSAEn changes if MSA supported and enabled */
112 if (kvm_mips_guest_has_msa(&vcpu->arch))
113 mask |= MIPS_CONF5_MSAEN;
114
115 /*
116 * Permit guest FPU mode changes if FPU is enabled and the relevant
117 * feature exists according to FIR register.
118 */
119 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
120 if (cpu_has_ufr)
121 mask |= MIPS_CONF5_UFR;
122 if (cpu_has_fre)
123 mask |= MIPS_CONF5_FRE | MIPS_CONF5_UFE;
124 }
125
126 return mask;
127}
128
129/*
130 * VZ optionally allows these additional Config bits to be written by root:
131 * Config: M, [MT]
132 * Config1: M, [MMUSize-1, C2, MD, PC, WR, CA], FP
133 * Config2: M
James Hogandffe0422017-03-14 10:15:34 +0000134 * Config3: M, MSAP, [BPG], ULRI, [DSP2P, DSPP], CTXTC, [ITL, LPA, VEIC,
James Hoganc992a4f2017-03-14 10:15:31 +0000135 * VInt, SP, CDMM, MT, SM, TL]
136 * Config4: M, [VTLBSizeExt, MMUSizeExt]
137 * Config5: [MRP]
138 */
139
140static inline unsigned int kvm_vz_config_user_wrmask(struct kvm_vcpu *vcpu)
141{
142 return kvm_vz_config_guest_wrmask(vcpu) | MIPS_CONF_M;
143}
144
145static inline unsigned int kvm_vz_config1_user_wrmask(struct kvm_vcpu *vcpu)
146{
147 unsigned int mask = kvm_vz_config1_guest_wrmask(vcpu) | MIPS_CONF_M;
148
149 /* Permit FPU to be present if FPU is supported */
150 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
151 mask |= MIPS_CONF1_FP;
152
153 return mask;
154}
155
156static inline unsigned int kvm_vz_config2_user_wrmask(struct kvm_vcpu *vcpu)
157{
158 return kvm_vz_config2_guest_wrmask(vcpu) | MIPS_CONF_M;
159}
160
161static inline unsigned int kvm_vz_config3_user_wrmask(struct kvm_vcpu *vcpu)
162{
163 unsigned int mask = kvm_vz_config3_guest_wrmask(vcpu) | MIPS_CONF_M |
James Hogandffe0422017-03-14 10:15:34 +0000164 MIPS_CONF3_ULRI | MIPS_CONF3_CTXTC;
James Hoganc992a4f2017-03-14 10:15:31 +0000165
166 /* Permit MSA to be present if MSA is supported */
167 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
168 mask |= MIPS_CONF3_MSA;
169
170 return mask;
171}
172
173static inline unsigned int kvm_vz_config4_user_wrmask(struct kvm_vcpu *vcpu)
174{
175 return kvm_vz_config4_guest_wrmask(vcpu) | MIPS_CONF_M;
176}
177
178static inline unsigned int kvm_vz_config5_user_wrmask(struct kvm_vcpu *vcpu)
179{
180 return kvm_vz_config5_guest_wrmask(vcpu);
181}
182
183static gpa_t kvm_vz_gva_to_gpa_cb(gva_t gva)
184{
185 /* VZ guest has already converted gva to gpa */
186 return gva;
187}
188
189static void kvm_vz_queue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
190{
191 set_bit(priority, &vcpu->arch.pending_exceptions);
192 clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
193}
194
195static void kvm_vz_dequeue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
196{
197 clear_bit(priority, &vcpu->arch.pending_exceptions);
198 set_bit(priority, &vcpu->arch.pending_exceptions_clr);
199}
200
201static void kvm_vz_queue_timer_int_cb(struct kvm_vcpu *vcpu)
202{
203 /*
204 * timer expiry is asynchronous to vcpu execution therefore defer guest
205 * cp0 accesses
206 */
207 kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
208}
209
210static void kvm_vz_dequeue_timer_int_cb(struct kvm_vcpu *vcpu)
211{
212 /*
213 * timer expiry is asynchronous to vcpu execution therefore defer guest
214 * cp0 accesses
215 */
216 kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_TIMER);
217}
218
219static void kvm_vz_queue_io_int_cb(struct kvm_vcpu *vcpu,
220 struct kvm_mips_interrupt *irq)
221{
222 int intr = (int)irq->irq;
223
224 /*
225 * interrupts are asynchronous to vcpu execution therefore defer guest
226 * cp0 accesses
227 */
228 switch (intr) {
229 case 2:
230 kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_IO);
231 break;
232
233 case 3:
234 kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_IPI_1);
235 break;
236
237 case 4:
238 kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_IPI_2);
239 break;
240
241 default:
242 break;
243 }
244
245}
246
247static void kvm_vz_dequeue_io_int_cb(struct kvm_vcpu *vcpu,
248 struct kvm_mips_interrupt *irq)
249{
250 int intr = (int)irq->irq;
251
252 /*
253 * interrupts are asynchronous to vcpu execution therefore defer guest
254 * cp0 accesses
255 */
256 switch (intr) {
257 case -2:
258 kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_IO);
259 break;
260
261 case -3:
262 kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_IPI_1);
263 break;
264
265 case -4:
266 kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_IPI_2);
267 break;
268
269 default:
270 break;
271 }
272
273}
274
275static u32 kvm_vz_priority_to_irq[MIPS_EXC_MAX] = {
276 [MIPS_EXC_INT_TIMER] = C_IRQ5,
277 [MIPS_EXC_INT_IO] = C_IRQ0,
278 [MIPS_EXC_INT_IPI_1] = C_IRQ1,
279 [MIPS_EXC_INT_IPI_2] = C_IRQ2,
280};
281
282static int kvm_vz_irq_deliver_cb(struct kvm_vcpu *vcpu, unsigned int priority,
283 u32 cause)
284{
285 u32 irq = (priority < MIPS_EXC_MAX) ?
286 kvm_vz_priority_to_irq[priority] : 0;
287
288 switch (priority) {
289 case MIPS_EXC_INT_TIMER:
290 set_gc0_cause(C_TI);
291 break;
292
293 case MIPS_EXC_INT_IO:
294 case MIPS_EXC_INT_IPI_1:
295 case MIPS_EXC_INT_IPI_2:
296 if (cpu_has_guestctl2)
297 set_c0_guestctl2(irq);
298 else
299 set_gc0_cause(irq);
300 break;
301
302 default:
303 break;
304 }
305
306 clear_bit(priority, &vcpu->arch.pending_exceptions);
307 return 1;
308}
309
310static int kvm_vz_irq_clear_cb(struct kvm_vcpu *vcpu, unsigned int priority,
311 u32 cause)
312{
313 u32 irq = (priority < MIPS_EXC_MAX) ?
314 kvm_vz_priority_to_irq[priority] : 0;
315
316 switch (priority) {
317 case MIPS_EXC_INT_TIMER:
318 /*
319 * Call to kvm_write_c0_guest_compare() clears Cause.TI in
320 * kvm_mips_emulate_CP0(). Explicitly clear irq associated with
321 * Cause.IP[IPTI] if GuestCtl2 virtual interrupt register not
322 * supported or if not using GuestCtl2 Hardware Clear.
323 */
324 if (cpu_has_guestctl2) {
325 if (!(read_c0_guestctl2() & (irq << 14)))
326 clear_c0_guestctl2(irq);
327 } else {
328 clear_gc0_cause(irq);
329 }
330 break;
331
332 case MIPS_EXC_INT_IO:
333 case MIPS_EXC_INT_IPI_1:
334 case MIPS_EXC_INT_IPI_2:
335 /* Clear GuestCtl2.VIP irq if not using Hardware Clear */
336 if (cpu_has_guestctl2) {
337 if (!(read_c0_guestctl2() & (irq << 14)))
338 clear_c0_guestctl2(irq);
339 } else {
340 clear_gc0_cause(irq);
341 }
342 break;
343
344 default:
345 break;
346 }
347
348 clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
349 return 1;
350}
351
352/*
353 * VZ guest timer handling.
354 */
355
356/**
357 * _kvm_vz_restore_stimer() - Restore soft timer state.
358 * @vcpu: Virtual CPU.
359 * @compare: CP0_Compare register value, restored by caller.
360 * @cause: CP0_Cause register to restore.
361 *
362 * Restore VZ state relating to the soft timer.
363 */
364static void _kvm_vz_restore_stimer(struct kvm_vcpu *vcpu, u32 compare,
365 u32 cause)
366{
367 /*
368 * Avoid spurious counter interrupts by setting Guest CP0_Count to just
369 * after Guest CP0_Compare.
370 */
371 write_c0_gtoffset(compare - read_c0_count());
372
373 back_to_back_c0_hazard();
374 write_gc0_cause(cause);
375}
376
377/**
378 * kvm_vz_restore_timer() - Restore guest timer state.
379 * @vcpu: Virtual CPU.
380 *
381 * Restore soft timer state from saved context.
382 */
383static void kvm_vz_restore_timer(struct kvm_vcpu *vcpu)
384{
385 struct mips_coproc *cop0 = vcpu->arch.cop0;
386 u32 cause, compare;
387
388 compare = kvm_read_sw_gc0_compare(cop0);
389 cause = kvm_read_sw_gc0_cause(cop0);
390
391 write_gc0_compare(compare);
392 _kvm_vz_restore_stimer(vcpu, compare, cause);
393}
394
395/**
396 * kvm_vz_save_timer() - Save guest timer state.
397 * @vcpu: Virtual CPU.
398 *
399 * Save VZ guest timer state.
400 */
401static void kvm_vz_save_timer(struct kvm_vcpu *vcpu)
402{
403 struct mips_coproc *cop0 = vcpu->arch.cop0;
404 u32 compare, cause;
405
406 compare = read_gc0_compare();
407 cause = read_gc0_cause();
408
409 /* save timer-related state to VCPU context */
410 kvm_write_sw_gc0_cause(cop0, cause);
411 kvm_write_sw_gc0_compare(cop0, compare);
412}
413
414/**
415 * kvm_vz_gva_to_gpa() - Convert valid GVA to GPA.
416 * @vcpu: KVM VCPU state.
417 * @gva: Guest virtual address to convert.
418 * @gpa: Output guest physical address.
419 *
420 * Convert a guest virtual address (GVA) which is valid according to the guest
421 * context, to a guest physical address (GPA).
422 *
423 * Returns: 0 on success.
424 * -errno on failure.
425 */
426static int kvm_vz_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
427 unsigned long *gpa)
428{
429 u32 gva32 = gva;
430
431 if ((long)gva == (s32)gva32) {
432 /* Handle canonical 32-bit virtual address */
433 if ((s32)gva32 < (s32)0xc0000000) {
434 /* legacy unmapped KSeg0 or KSeg1 */
435 *gpa = gva32 & 0x1fffffff;
436 return 0;
437 }
438#ifdef CONFIG_64BIT
439 } else if ((gva & 0xc000000000000000) == 0x8000000000000000) {
440 /* XKPHYS */
441 /*
442 * Traditionally fully unmapped.
443 * Bits 61:59 specify the CCA, which we can just mask off here.
444 * Bits 58:PABITS should be zero, but we shouldn't have got here
445 * if it wasn't.
446 */
447 *gpa = gva & 0x07ffffffffffffff;
448 return 0;
449#endif
450 }
451
452 return kvm_vz_guest_tlb_lookup(vcpu, gva, gpa);
453}
454
455/**
456 * kvm_vz_badvaddr_to_gpa() - Convert GVA BadVAddr from root exception to GPA.
457 * @vcpu: KVM VCPU state.
458 * @badvaddr: Root BadVAddr.
459 * @gpa: Output guest physical address.
460 *
461 * VZ implementations are permitted to report guest virtual addresses (GVA) in
462 * BadVAddr on a root exception during guest execution, instead of the more
463 * convenient guest physical addresses (GPA). When we get a GVA, this function
464 * converts it to a GPA, taking into account guest segmentation and guest TLB
465 * state.
466 *
467 * Returns: 0 on success.
468 * -errno on failure.
469 */
470static int kvm_vz_badvaddr_to_gpa(struct kvm_vcpu *vcpu, unsigned long badvaddr,
471 unsigned long *gpa)
472{
473 unsigned int gexccode = (vcpu->arch.host_cp0_guestctl0 &
474 MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
475
476 /* If BadVAddr is GPA, then all is well in the world */
477 if (likely(gexccode == MIPS_GCTL0_GEXC_GPA)) {
478 *gpa = badvaddr;
479 return 0;
480 }
481
482 /* Otherwise we'd expect it to be GVA ... */
483 if (WARN(gexccode != MIPS_GCTL0_GEXC_GVA,
484 "Unexpected gexccode %#x\n", gexccode))
485 return -EINVAL;
486
487 /* ... and we need to perform the GVA->GPA translation in software */
488 return kvm_vz_gva_to_gpa(vcpu, badvaddr, gpa);
489}
490
491static int kvm_trap_vz_no_handler(struct kvm_vcpu *vcpu)
492{
493 u32 *opc = (u32 *) vcpu->arch.pc;
494 u32 cause = vcpu->arch.host_cp0_cause;
495 u32 exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
496 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
497 u32 inst = 0;
498
499 /*
500 * Fetch the instruction.
501 */
502 if (cause & CAUSEF_BD)
503 opc += 1;
504 kvm_get_badinstr(opc, vcpu, &inst);
505
506 kvm_err("Exception Code: %d not handled @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n",
507 exccode, opc, inst, badvaddr,
508 read_gc0_status());
509 kvm_arch_vcpu_dump_regs(vcpu);
510 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
511 return RESUME_HOST;
512}
513
514static enum emulation_result kvm_vz_gpsi_cop0(union mips_instruction inst,
515 u32 *opc, u32 cause,
516 struct kvm_run *run,
517 struct kvm_vcpu *vcpu)
518{
519 struct mips_coproc *cop0 = vcpu->arch.cop0;
520 enum emulation_result er = EMULATE_DONE;
521 u32 rt, rd, sel;
522 unsigned long curr_pc;
523 unsigned long val;
524
525 /*
526 * Update PC and hold onto current PC in case there is
527 * an error and we want to rollback the PC
528 */
529 curr_pc = vcpu->arch.pc;
530 er = update_pc(vcpu, cause);
531 if (er == EMULATE_FAIL)
532 return er;
533
534 if (inst.co_format.co) {
535 switch (inst.co_format.func) {
536 case wait_op:
537 er = kvm_mips_emul_wait(vcpu);
538 break;
539 default:
540 er = EMULATE_FAIL;
541 }
542 } else {
543 rt = inst.c0r_format.rt;
544 rd = inst.c0r_format.rd;
545 sel = inst.c0r_format.sel;
546
547 switch (inst.c0r_format.rs) {
548 case dmfc_op:
549 case mfc_op:
550#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
551 cop0->stat[rd][sel]++;
552#endif
553 if (rd == MIPS_CP0_COUNT &&
554 sel == 0) { /* Count */
555 val = kvm_mips_read_count(vcpu);
556 } else if (rd == MIPS_CP0_COMPARE &&
557 sel == 0) { /* Compare */
558 val = read_gc0_compare();
559 } else if ((rd == MIPS_CP0_PRID &&
560 (sel == 0 || /* PRid */
561 sel == 2 || /* CDMMBase */
562 sel == 3)) || /* CMGCRBase */
563 (rd == MIPS_CP0_STATUS &&
564 (sel == 2 || /* SRSCtl */
565 sel == 3)) || /* SRSMap */
566 (rd == MIPS_CP0_CONFIG &&
567 (sel == 7)) || /* Config7 */
568 (rd == MIPS_CP0_ERRCTL &&
569 (sel == 0))) { /* ErrCtl */
570 val = cop0->reg[rd][sel];
571 } else {
572 val = 0;
573 er = EMULATE_FAIL;
574 }
575
576 if (er != EMULATE_FAIL) {
577 /* Sign extend */
578 if (inst.c0r_format.rs == mfc_op)
579 val = (int)val;
580 vcpu->arch.gprs[rt] = val;
581 }
582
583 trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mfc_op) ?
584 KVM_TRACE_MFC0 : KVM_TRACE_DMFC0,
585 KVM_TRACE_COP0(rd, sel), val);
586 break;
587
588 case dmtc_op:
589 case mtc_op:
590#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
591 cop0->stat[rd][sel]++;
592#endif
593 val = vcpu->arch.gprs[rt];
594 trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mtc_op) ?
595 KVM_TRACE_MTC0 : KVM_TRACE_DMTC0,
596 KVM_TRACE_COP0(rd, sel), val);
597
598 if (rd == MIPS_CP0_COUNT &&
599 sel == 0) { /* Count */
600 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
601 } else if (rd == MIPS_CP0_COMPARE &&
602 sel == 0) { /* Compare */
603 kvm_mips_write_compare(vcpu,
604 vcpu->arch.gprs[rt],
605 true);
606 } else if (rd == MIPS_CP0_ERRCTL &&
607 (sel == 0)) { /* ErrCtl */
608 /* ignore the written value */
609 } else {
610 er = EMULATE_FAIL;
611 }
612 break;
613
614 default:
615 er = EMULATE_FAIL;
616 break;
617 }
618 }
619 /* Rollback PC only if emulation was unsuccessful */
620 if (er == EMULATE_FAIL) {
621 kvm_err("[%#lx]%s: unsupported cop0 instruction 0x%08x\n",
622 curr_pc, __func__, inst.word);
623
624 vcpu->arch.pc = curr_pc;
625 }
626
627 return er;
628}
629
630static enum emulation_result kvm_vz_gpsi_cache(union mips_instruction inst,
631 u32 *opc, u32 cause,
632 struct kvm_run *run,
633 struct kvm_vcpu *vcpu)
634{
635 enum emulation_result er = EMULATE_DONE;
636 u32 cache, op_inst, op, base;
637 s16 offset;
638 struct kvm_vcpu_arch *arch = &vcpu->arch;
639 unsigned long va, curr_pc;
640
641 /*
642 * Update PC and hold onto current PC in case there is
643 * an error and we want to rollback the PC
644 */
645 curr_pc = vcpu->arch.pc;
646 er = update_pc(vcpu, cause);
647 if (er == EMULATE_FAIL)
648 return er;
649
650 base = inst.i_format.rs;
651 op_inst = inst.i_format.rt;
652 if (cpu_has_mips_r6)
653 offset = inst.spec3_format.simmediate;
654 else
655 offset = inst.i_format.simmediate;
656 cache = op_inst & CacheOp_Cache;
657 op = op_inst & CacheOp_Op;
658
659 va = arch->gprs[base] + offset;
660
661 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
662 cache, op, base, arch->gprs[base], offset);
663
664 /* Secondary or tirtiary cache ops ignored */
665 if (cache != Cache_I && cache != Cache_D)
666 return EMULATE_DONE;
667
668 switch (op_inst) {
669 case Index_Invalidate_I:
670 flush_icache_line_indexed(va);
671 return EMULATE_DONE;
672 case Index_Writeback_Inv_D:
673 flush_dcache_line_indexed(va);
674 return EMULATE_DONE;
675 default:
676 break;
677 };
678
679 kvm_err("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
680 curr_pc, vcpu->arch.gprs[31], cache, op, base, arch->gprs[base],
681 offset);
682 /* Rollback PC */
683 vcpu->arch.pc = curr_pc;
684
685 return EMULATE_FAIL;
686}
687
688static enum emulation_result kvm_trap_vz_handle_gpsi(u32 cause, u32 *opc,
689 struct kvm_vcpu *vcpu)
690{
691 enum emulation_result er = EMULATE_DONE;
692 struct kvm_vcpu_arch *arch = &vcpu->arch;
693 struct kvm_run *run = vcpu->run;
694 union mips_instruction inst;
695 int rd, rt, sel;
696 int err;
697
698 /*
699 * Fetch the instruction.
700 */
701 if (cause & CAUSEF_BD)
702 opc += 1;
703 err = kvm_get_badinstr(opc, vcpu, &inst.word);
704 if (err)
705 return EMULATE_FAIL;
706
707 switch (inst.r_format.opcode) {
708 case cop0_op:
709 er = kvm_vz_gpsi_cop0(inst, opc, cause, run, vcpu);
710 break;
711#ifndef CONFIG_CPU_MIPSR6
712 case cache_op:
713 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
714 er = kvm_vz_gpsi_cache(inst, opc, cause, run, vcpu);
715 break;
716#endif
717 case spec3_op:
718 switch (inst.spec3_format.func) {
719#ifdef CONFIG_CPU_MIPSR6
720 case cache6_op:
721 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
722 er = kvm_vz_gpsi_cache(inst, opc, cause, run, vcpu);
723 break;
724#endif
725 case rdhwr_op:
726 if (inst.r_format.rs || (inst.r_format.re >> 3))
727 goto unknown;
728
729 rd = inst.r_format.rd;
730 rt = inst.r_format.rt;
731 sel = inst.r_format.re & 0x7;
732
733 switch (rd) {
734 case MIPS_HWR_CC: /* Read count register */
735 arch->gprs[rt] =
736 (long)(int)kvm_mips_read_count(vcpu);
737 break;
738 default:
739 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
740 KVM_TRACE_HWR(rd, sel), 0);
741 goto unknown;
742 };
743
744 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
745 KVM_TRACE_HWR(rd, sel), arch->gprs[rt]);
746
747 er = update_pc(vcpu, cause);
748 break;
749 default:
750 goto unknown;
751 };
752 break;
753unknown:
754
755 default:
756 kvm_err("GPSI exception not supported (%p/%#x)\n",
757 opc, inst.word);
758 kvm_arch_vcpu_dump_regs(vcpu);
759 er = EMULATE_FAIL;
760 break;
761 }
762
763 return er;
764}
765
766static enum emulation_result kvm_trap_vz_handle_gsfc(u32 cause, u32 *opc,
767 struct kvm_vcpu *vcpu)
768{
769 enum emulation_result er = EMULATE_DONE;
770 struct kvm_vcpu_arch *arch = &vcpu->arch;
771 union mips_instruction inst;
772 int err;
773
774 /*
775 * Fetch the instruction.
776 */
777 if (cause & CAUSEF_BD)
778 opc += 1;
779 err = kvm_get_badinstr(opc, vcpu, &inst.word);
780 if (err)
781 return EMULATE_FAIL;
782
783 /* complete MTC0 on behalf of guest and advance EPC */
784 if (inst.c0r_format.opcode == cop0_op &&
785 inst.c0r_format.rs == mtc_op &&
786 inst.c0r_format.z == 0) {
787 int rt = inst.c0r_format.rt;
788 int rd = inst.c0r_format.rd;
789 int sel = inst.c0r_format.sel;
790 unsigned int val = arch->gprs[rt];
791 unsigned int old_val, change;
792
793 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0, KVM_TRACE_COP0(rd, sel),
794 val);
795
796 if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
797 /* FR bit should read as zero if no FPU */
798 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
799 val &= ~(ST0_CU1 | ST0_FR);
800
801 /*
802 * Also don't allow FR to be set if host doesn't support
803 * it.
804 */
805 if (!(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
806 val &= ~ST0_FR;
807
808 old_val = read_gc0_status();
809 change = val ^ old_val;
810
811 if (change & ST0_FR) {
812 /*
813 * FPU and Vector register state is made
814 * UNPREDICTABLE by a change of FR, so don't
815 * even bother saving it.
816 */
817 kvm_drop_fpu(vcpu);
818 }
819
820 /*
821 * If MSA state is already live, it is undefined how it
822 * interacts with FR=0 FPU state, and we don't want to
823 * hit reserved instruction exceptions trying to save
824 * the MSA state later when CU=1 && FR=1, so play it
825 * safe and save it first.
826 */
827 if (change & ST0_CU1 && !(val & ST0_FR) &&
828 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
829 kvm_lose_fpu(vcpu);
830
831 write_gc0_status(val);
832 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
833 u32 old_cause = read_gc0_cause();
834 u32 change = old_cause ^ val;
835
836 /* DC bit enabling/disabling timer? */
837 if (change & CAUSEF_DC) {
838 if (val & CAUSEF_DC)
839 kvm_mips_count_disable_cause(vcpu);
840 else
841 kvm_mips_count_enable_cause(vcpu);
842 }
843
844 /* Only certain bits are RW to the guest */
845 change &= (CAUSEF_DC | CAUSEF_IV | CAUSEF_WP |
846 CAUSEF_IP0 | CAUSEF_IP1);
847
848 /* WP can only be cleared */
849 change &= ~CAUSEF_WP | old_cause;
850
851 write_gc0_cause(old_cause ^ change);
852 } else if ((rd == MIPS_CP0_STATUS) && (sel == 1)) { /* IntCtl */
853 write_gc0_intctl(val);
854 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
855 old_val = read_gc0_config5();
856 change = val ^ old_val;
857 /* Handle changes in FPU/MSA modes */
858 preempt_disable();
859
860 /*
861 * Propagate FRE changes immediately if the FPU
862 * context is already loaded.
863 */
864 if (change & MIPS_CONF5_FRE &&
865 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
866 change_c0_config5(MIPS_CONF5_FRE, val);
867
868 preempt_enable();
869
870 val = old_val ^
871 (change & kvm_vz_config5_guest_wrmask(vcpu));
872 write_gc0_config5(val);
873 } else {
874 kvm_err("Handle GSFC, unsupported field change @ %p: %#x\n",
875 opc, inst.word);
876 er = EMULATE_FAIL;
877 }
878
879 if (er != EMULATE_FAIL)
880 er = update_pc(vcpu, cause);
881 } else {
882 kvm_err("Handle GSFC, unrecognized instruction @ %p: %#x\n",
883 opc, inst.word);
884 er = EMULATE_FAIL;
885 }
886
887 return er;
888}
889
890static enum emulation_result kvm_trap_vz_handle_hc(u32 cause, u32 *opc,
891 struct kvm_vcpu *vcpu)
892{
893 enum emulation_result er;
894 union mips_instruction inst;
895 unsigned long curr_pc;
896 int err;
897
898 if (cause & CAUSEF_BD)
899 opc += 1;
900 err = kvm_get_badinstr(opc, vcpu, &inst.word);
901 if (err)
902 return EMULATE_FAIL;
903
904 /*
905 * Update PC and hold onto current PC in case there is
906 * an error and we want to rollback the PC
907 */
908 curr_pc = vcpu->arch.pc;
909 er = update_pc(vcpu, cause);
910 if (er == EMULATE_FAIL)
911 return er;
912
913 er = kvm_mips_emul_hypcall(vcpu, inst);
914 if (er == EMULATE_FAIL)
915 vcpu->arch.pc = curr_pc;
916
917 return er;
918}
919
920static enum emulation_result kvm_trap_vz_no_handler_guest_exit(u32 gexccode,
921 u32 cause,
922 u32 *opc,
923 struct kvm_vcpu *vcpu)
924{
925 u32 inst;
926
927 /*
928 * Fetch the instruction.
929 */
930 if (cause & CAUSEF_BD)
931 opc += 1;
932 kvm_get_badinstr(opc, vcpu, &inst);
933
934 kvm_err("Guest Exception Code: %d not yet handled @ PC: %p, inst: 0x%08x Status: %#x\n",
935 gexccode, opc, inst, read_gc0_status());
936
937 return EMULATE_FAIL;
938}
939
940static int kvm_trap_vz_handle_guest_exit(struct kvm_vcpu *vcpu)
941{
942 u32 *opc = (u32 *) vcpu->arch.pc;
943 u32 cause = vcpu->arch.host_cp0_cause;
944 enum emulation_result er = EMULATE_DONE;
945 u32 gexccode = (vcpu->arch.host_cp0_guestctl0 &
946 MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
947 int ret = RESUME_GUEST;
948
949 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_GEXCCODE_BASE + gexccode);
950 switch (gexccode) {
951 case MIPS_GCTL0_GEXC_GPSI:
952 ++vcpu->stat.vz_gpsi_exits;
953 er = kvm_trap_vz_handle_gpsi(cause, opc, vcpu);
954 break;
955 case MIPS_GCTL0_GEXC_GSFC:
956 ++vcpu->stat.vz_gsfc_exits;
957 er = kvm_trap_vz_handle_gsfc(cause, opc, vcpu);
958 break;
959 case MIPS_GCTL0_GEXC_HC:
960 ++vcpu->stat.vz_hc_exits;
961 er = kvm_trap_vz_handle_hc(cause, opc, vcpu);
962 break;
963 case MIPS_GCTL0_GEXC_GRR:
964 ++vcpu->stat.vz_grr_exits;
965 er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
966 vcpu);
967 break;
968 case MIPS_GCTL0_GEXC_GVA:
969 ++vcpu->stat.vz_gva_exits;
970 er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
971 vcpu);
972 break;
973 case MIPS_GCTL0_GEXC_GHFC:
974 ++vcpu->stat.vz_ghfc_exits;
975 er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
976 vcpu);
977 break;
978 case MIPS_GCTL0_GEXC_GPA:
979 ++vcpu->stat.vz_gpa_exits;
980 er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
981 vcpu);
982 break;
983 default:
984 ++vcpu->stat.vz_resvd_exits;
985 er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
986 vcpu);
987 break;
988
989 }
990
991 if (er == EMULATE_DONE) {
992 ret = RESUME_GUEST;
993 } else if (er == EMULATE_HYPERCALL) {
994 ret = kvm_mips_handle_hypcall(vcpu);
995 } else {
996 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
997 ret = RESUME_HOST;
998 }
999 return ret;
1000}
1001
1002/**
1003 * kvm_trap_vz_handle_cop_unusuable() - Guest used unusable coprocessor.
1004 * @vcpu: Virtual CPU context.
1005 *
1006 * Handle when the guest attempts to use a coprocessor which hasn't been allowed
1007 * by the root context.
1008 */
1009static int kvm_trap_vz_handle_cop_unusable(struct kvm_vcpu *vcpu)
1010{
1011 struct kvm_run *run = vcpu->run;
1012 u32 cause = vcpu->arch.host_cp0_cause;
1013 enum emulation_result er = EMULATE_FAIL;
1014 int ret = RESUME_GUEST;
1015
1016 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
1017 /*
1018 * If guest FPU not present, the FPU operation should have been
1019 * treated as a reserved instruction!
1020 * If FPU already in use, we shouldn't get this at all.
1021 */
1022 if (WARN_ON(!kvm_mips_guest_has_fpu(&vcpu->arch) ||
1023 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
1024 preempt_enable();
1025 return EMULATE_FAIL;
1026 }
1027
1028 kvm_own_fpu(vcpu);
1029 er = EMULATE_DONE;
1030 }
1031 /* other coprocessors not handled */
1032
1033 switch (er) {
1034 case EMULATE_DONE:
1035 ret = RESUME_GUEST;
1036 break;
1037
1038 case EMULATE_FAIL:
1039 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1040 ret = RESUME_HOST;
1041 break;
1042
1043 default:
1044 BUG();
1045 }
1046 return ret;
1047}
1048
1049/**
1050 * kvm_trap_vz_handle_msa_disabled() - Guest used MSA while disabled in root.
1051 * @vcpu: Virtual CPU context.
1052 *
1053 * Handle when the guest attempts to use MSA when it is disabled in the root
1054 * context.
1055 */
1056static int kvm_trap_vz_handle_msa_disabled(struct kvm_vcpu *vcpu)
1057{
1058 struct kvm_run *run = vcpu->run;
1059
1060 /*
1061 * If MSA not present or not exposed to guest or FR=0, the MSA operation
1062 * should have been treated as a reserved instruction!
1063 * Same if CU1=1, FR=0.
1064 * If MSA already in use, we shouldn't get this at all.
1065 */
1066 if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
1067 (read_gc0_status() & (ST0_CU1 | ST0_FR)) == ST0_CU1 ||
1068 !(read_gc0_config5() & MIPS_CONF5_MSAEN) ||
1069 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1070 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1071 return RESUME_HOST;
1072 }
1073
1074 kvm_own_msa(vcpu);
1075
1076 return RESUME_GUEST;
1077}
1078
1079static int kvm_trap_vz_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
1080{
1081 struct kvm_run *run = vcpu->run;
1082 u32 *opc = (u32 *) vcpu->arch.pc;
1083 u32 cause = vcpu->arch.host_cp0_cause;
1084 ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
1085 union mips_instruction inst;
1086 enum emulation_result er = EMULATE_DONE;
1087 int err, ret = RESUME_GUEST;
1088
1089 if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, false)) {
1090 /* A code fetch fault doesn't count as an MMIO */
1091 if (kvm_is_ifetch_fault(&vcpu->arch)) {
1092 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1093 return RESUME_HOST;
1094 }
1095
1096 /* Fetch the instruction */
1097 if (cause & CAUSEF_BD)
1098 opc += 1;
1099 err = kvm_get_badinstr(opc, vcpu, &inst.word);
1100 if (err) {
1101 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1102 return RESUME_HOST;
1103 }
1104
1105 /* Treat as MMIO */
1106 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1107 if (er == EMULATE_FAIL) {
1108 kvm_err("Guest Emulate Load from MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1109 opc, badvaddr);
1110 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1111 }
1112 }
1113
1114 if (er == EMULATE_DONE) {
1115 ret = RESUME_GUEST;
1116 } else if (er == EMULATE_DO_MMIO) {
1117 run->exit_reason = KVM_EXIT_MMIO;
1118 ret = RESUME_HOST;
1119 } else {
1120 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1121 ret = RESUME_HOST;
1122 }
1123 return ret;
1124}
1125
1126static int kvm_trap_vz_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
1127{
1128 struct kvm_run *run = vcpu->run;
1129 u32 *opc = (u32 *) vcpu->arch.pc;
1130 u32 cause = vcpu->arch.host_cp0_cause;
1131 ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
1132 union mips_instruction inst;
1133 enum emulation_result er = EMULATE_DONE;
1134 int err;
1135 int ret = RESUME_GUEST;
1136
1137 /* Just try the access again if we couldn't do the translation */
1138 if (kvm_vz_badvaddr_to_gpa(vcpu, badvaddr, &badvaddr))
1139 return RESUME_GUEST;
1140 vcpu->arch.host_cp0_badvaddr = badvaddr;
1141
1142 if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, true)) {
1143 /* Fetch the instruction */
1144 if (cause & CAUSEF_BD)
1145 opc += 1;
1146 err = kvm_get_badinstr(opc, vcpu, &inst.word);
1147 if (err) {
1148 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1149 return RESUME_HOST;
1150 }
1151
1152 /* Treat as MMIO */
1153 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1154 if (er == EMULATE_FAIL) {
1155 kvm_err("Guest Emulate Store to MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1156 opc, badvaddr);
1157 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1158 }
1159 }
1160
1161 if (er == EMULATE_DONE) {
1162 ret = RESUME_GUEST;
1163 } else if (er == EMULATE_DO_MMIO) {
1164 run->exit_reason = KVM_EXIT_MMIO;
1165 ret = RESUME_HOST;
1166 } else {
1167 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1168 ret = RESUME_HOST;
1169 }
1170 return ret;
1171}
1172
1173static u64 kvm_vz_get_one_regs[] = {
1174 KVM_REG_MIPS_CP0_INDEX,
1175 KVM_REG_MIPS_CP0_ENTRYLO0,
1176 KVM_REG_MIPS_CP0_ENTRYLO1,
1177 KVM_REG_MIPS_CP0_CONTEXT,
1178 KVM_REG_MIPS_CP0_PAGEMASK,
1179 KVM_REG_MIPS_CP0_PAGEGRAIN,
1180 KVM_REG_MIPS_CP0_WIRED,
1181 KVM_REG_MIPS_CP0_HWRENA,
1182 KVM_REG_MIPS_CP0_BADVADDR,
1183 KVM_REG_MIPS_CP0_COUNT,
1184 KVM_REG_MIPS_CP0_ENTRYHI,
1185 KVM_REG_MIPS_CP0_COMPARE,
1186 KVM_REG_MIPS_CP0_STATUS,
1187 KVM_REG_MIPS_CP0_INTCTL,
1188 KVM_REG_MIPS_CP0_CAUSE,
1189 KVM_REG_MIPS_CP0_EPC,
1190 KVM_REG_MIPS_CP0_PRID,
1191 KVM_REG_MIPS_CP0_EBASE,
1192 KVM_REG_MIPS_CP0_CONFIG,
1193 KVM_REG_MIPS_CP0_CONFIG1,
1194 KVM_REG_MIPS_CP0_CONFIG2,
1195 KVM_REG_MIPS_CP0_CONFIG3,
1196 KVM_REG_MIPS_CP0_CONFIG4,
1197 KVM_REG_MIPS_CP0_CONFIG5,
1198#ifdef CONFIG_64BIT
1199 KVM_REG_MIPS_CP0_XCONTEXT,
1200#endif
1201 KVM_REG_MIPS_CP0_ERROREPC,
1202
1203 KVM_REG_MIPS_COUNT_CTL,
1204 KVM_REG_MIPS_COUNT_RESUME,
1205 KVM_REG_MIPS_COUNT_HZ,
1206};
1207
James Hogandffe0422017-03-14 10:15:34 +00001208static u64 kvm_vz_get_one_regs_contextconfig[] = {
1209 KVM_REG_MIPS_CP0_CONTEXTCONFIG,
1210#ifdef CONFIG_64BIT
1211 KVM_REG_MIPS_CP0_XCONTEXTCONFIG,
1212#endif
1213};
1214
James Hoganc992a4f2017-03-14 10:15:31 +00001215static u64 kvm_vz_get_one_regs_kscratch[] = {
1216 KVM_REG_MIPS_CP0_KSCRATCH1,
1217 KVM_REG_MIPS_CP0_KSCRATCH2,
1218 KVM_REG_MIPS_CP0_KSCRATCH3,
1219 KVM_REG_MIPS_CP0_KSCRATCH4,
1220 KVM_REG_MIPS_CP0_KSCRATCH5,
1221 KVM_REG_MIPS_CP0_KSCRATCH6,
1222};
1223
1224static unsigned long kvm_vz_num_regs(struct kvm_vcpu *vcpu)
1225{
1226 unsigned long ret;
1227
1228 ret = ARRAY_SIZE(kvm_vz_get_one_regs);
1229 if (cpu_guest_has_userlocal)
1230 ++ret;
James Hoganedc89262017-03-14 10:15:33 +00001231 if (cpu_guest_has_badinstr)
1232 ++ret;
1233 if (cpu_guest_has_badinstrp)
1234 ++ret;
James Hogandffe0422017-03-14 10:15:34 +00001235 if (cpu_guest_has_contextconfig)
1236 ret += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
James Hoganc992a4f2017-03-14 10:15:31 +00001237 ret += __arch_hweight8(cpu_data[0].guest.kscratch_mask);
1238
1239 return ret;
1240}
1241
1242static int kvm_vz_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
1243{
1244 u64 index;
1245 unsigned int i;
1246
1247 if (copy_to_user(indices, kvm_vz_get_one_regs,
1248 sizeof(kvm_vz_get_one_regs)))
1249 return -EFAULT;
1250 indices += ARRAY_SIZE(kvm_vz_get_one_regs);
1251
1252 if (cpu_guest_has_userlocal) {
1253 index = KVM_REG_MIPS_CP0_USERLOCAL;
1254 if (copy_to_user(indices, &index, sizeof(index)))
1255 return -EFAULT;
1256 ++indices;
1257 }
James Hoganedc89262017-03-14 10:15:33 +00001258 if (cpu_guest_has_badinstr) {
1259 index = KVM_REG_MIPS_CP0_BADINSTR;
1260 if (copy_to_user(indices, &index, sizeof(index)))
1261 return -EFAULT;
1262 ++indices;
1263 }
1264 if (cpu_guest_has_badinstrp) {
1265 index = KVM_REG_MIPS_CP0_BADINSTRP;
1266 if (copy_to_user(indices, &index, sizeof(index)))
1267 return -EFAULT;
1268 ++indices;
1269 }
James Hogandffe0422017-03-14 10:15:34 +00001270 if (cpu_guest_has_contextconfig) {
1271 if (copy_to_user(indices, kvm_vz_get_one_regs_contextconfig,
1272 sizeof(kvm_vz_get_one_regs_contextconfig)))
1273 return -EFAULT;
1274 indices += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
1275 }
James Hoganc992a4f2017-03-14 10:15:31 +00001276 for (i = 0; i < 6; ++i) {
1277 if (!cpu_guest_has_kscr(i + 2))
1278 continue;
1279
1280 if (copy_to_user(indices, &kvm_vz_get_one_regs_kscratch[i],
1281 sizeof(kvm_vz_get_one_regs_kscratch[i])))
1282 return -EFAULT;
1283 ++indices;
1284 }
1285
1286 return 0;
1287}
1288
1289static inline s64 entrylo_kvm_to_user(unsigned long v)
1290{
1291 s64 mask, ret = v;
1292
1293 if (BITS_PER_LONG == 32) {
1294 /*
1295 * KVM API exposes 64-bit version of the register, so move the
1296 * RI/XI bits up into place.
1297 */
1298 mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
1299 ret &= ~mask;
1300 ret |= ((s64)v & mask) << 32;
1301 }
1302 return ret;
1303}
1304
1305static inline unsigned long entrylo_user_to_kvm(s64 v)
1306{
1307 unsigned long mask, ret = v;
1308
1309 if (BITS_PER_LONG == 32) {
1310 /*
1311 * KVM API exposes 64-bit versiono of the register, so move the
1312 * RI/XI bits down into place.
1313 */
1314 mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
1315 ret &= ~mask;
1316 ret |= (v >> 32) & mask;
1317 }
1318 return ret;
1319}
1320
1321static int kvm_vz_get_one_reg(struct kvm_vcpu *vcpu,
1322 const struct kvm_one_reg *reg,
1323 s64 *v)
1324{
1325 struct mips_coproc *cop0 = vcpu->arch.cop0;
1326 unsigned int idx;
1327
1328 switch (reg->id) {
1329 case KVM_REG_MIPS_CP0_INDEX:
1330 *v = (long)read_gc0_index();
1331 break;
1332 case KVM_REG_MIPS_CP0_ENTRYLO0:
1333 *v = entrylo_kvm_to_user(read_gc0_entrylo0());
1334 break;
1335 case KVM_REG_MIPS_CP0_ENTRYLO1:
1336 *v = entrylo_kvm_to_user(read_gc0_entrylo1());
1337 break;
1338 case KVM_REG_MIPS_CP0_CONTEXT:
1339 *v = (long)read_gc0_context();
1340 break;
James Hogandffe0422017-03-14 10:15:34 +00001341 case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
1342 if (!cpu_guest_has_contextconfig)
1343 return -EINVAL;
1344 *v = read_gc0_contextconfig();
1345 break;
James Hoganc992a4f2017-03-14 10:15:31 +00001346 case KVM_REG_MIPS_CP0_USERLOCAL:
1347 if (!cpu_guest_has_userlocal)
1348 return -EINVAL;
1349 *v = read_gc0_userlocal();
1350 break;
James Hogandffe0422017-03-14 10:15:34 +00001351#ifdef CONFIG_64BIT
1352 case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
1353 if (!cpu_guest_has_contextconfig)
1354 return -EINVAL;
1355 *v = read_gc0_xcontextconfig();
1356 break;
1357#endif
James Hoganc992a4f2017-03-14 10:15:31 +00001358 case KVM_REG_MIPS_CP0_PAGEMASK:
1359 *v = (long)read_gc0_pagemask();
1360 break;
1361 case KVM_REG_MIPS_CP0_PAGEGRAIN:
1362 *v = (long)read_gc0_pagegrain();
1363 break;
1364 case KVM_REG_MIPS_CP0_WIRED:
1365 *v = (long)read_gc0_wired();
1366 break;
1367 case KVM_REG_MIPS_CP0_HWRENA:
1368 *v = (long)read_gc0_hwrena();
1369 break;
1370 case KVM_REG_MIPS_CP0_BADVADDR:
1371 *v = (long)read_gc0_badvaddr();
1372 break;
James Hoganedc89262017-03-14 10:15:33 +00001373 case KVM_REG_MIPS_CP0_BADINSTR:
1374 if (!cpu_guest_has_badinstr)
1375 return -EINVAL;
1376 *v = read_gc0_badinstr();
1377 break;
1378 case KVM_REG_MIPS_CP0_BADINSTRP:
1379 if (!cpu_guest_has_badinstrp)
1380 return -EINVAL;
1381 *v = read_gc0_badinstrp();
1382 break;
James Hoganc992a4f2017-03-14 10:15:31 +00001383 case KVM_REG_MIPS_CP0_COUNT:
1384 *v = kvm_mips_read_count(vcpu);
1385 break;
1386 case KVM_REG_MIPS_CP0_ENTRYHI:
1387 *v = (long)read_gc0_entryhi();
1388 break;
1389 case KVM_REG_MIPS_CP0_COMPARE:
1390 *v = (long)read_gc0_compare();
1391 break;
1392 case KVM_REG_MIPS_CP0_STATUS:
1393 *v = (long)read_gc0_status();
1394 break;
1395 case KVM_REG_MIPS_CP0_INTCTL:
1396 *v = read_gc0_intctl();
1397 break;
1398 case KVM_REG_MIPS_CP0_CAUSE:
1399 *v = (long)read_gc0_cause();
1400 break;
1401 case KVM_REG_MIPS_CP0_EPC:
1402 *v = (long)read_gc0_epc();
1403 break;
1404 case KVM_REG_MIPS_CP0_PRID:
1405 *v = (long)kvm_read_c0_guest_prid(cop0);
1406 break;
1407 case KVM_REG_MIPS_CP0_EBASE:
1408 *v = kvm_vz_read_gc0_ebase();
1409 break;
1410 case KVM_REG_MIPS_CP0_CONFIG:
1411 *v = read_gc0_config();
1412 break;
1413 case KVM_REG_MIPS_CP0_CONFIG1:
1414 if (!cpu_guest_has_conf1)
1415 return -EINVAL;
1416 *v = read_gc0_config1();
1417 break;
1418 case KVM_REG_MIPS_CP0_CONFIG2:
1419 if (!cpu_guest_has_conf2)
1420 return -EINVAL;
1421 *v = read_gc0_config2();
1422 break;
1423 case KVM_REG_MIPS_CP0_CONFIG3:
1424 if (!cpu_guest_has_conf3)
1425 return -EINVAL;
1426 *v = read_gc0_config3();
1427 break;
1428 case KVM_REG_MIPS_CP0_CONFIG4:
1429 if (!cpu_guest_has_conf4)
1430 return -EINVAL;
1431 *v = read_gc0_config4();
1432 break;
1433 case KVM_REG_MIPS_CP0_CONFIG5:
1434 if (!cpu_guest_has_conf5)
1435 return -EINVAL;
1436 *v = read_gc0_config5();
1437 break;
1438#ifdef CONFIG_64BIT
1439 case KVM_REG_MIPS_CP0_XCONTEXT:
1440 *v = read_gc0_xcontext();
1441 break;
1442#endif
1443 case KVM_REG_MIPS_CP0_ERROREPC:
1444 *v = (long)read_gc0_errorepc();
1445 break;
1446 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1447 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1448 if (!cpu_guest_has_kscr(idx))
1449 return -EINVAL;
1450 switch (idx) {
1451 case 2:
1452 *v = (long)read_gc0_kscratch1();
1453 break;
1454 case 3:
1455 *v = (long)read_gc0_kscratch2();
1456 break;
1457 case 4:
1458 *v = (long)read_gc0_kscratch3();
1459 break;
1460 case 5:
1461 *v = (long)read_gc0_kscratch4();
1462 break;
1463 case 6:
1464 *v = (long)read_gc0_kscratch5();
1465 break;
1466 case 7:
1467 *v = (long)read_gc0_kscratch6();
1468 break;
1469 }
1470 break;
1471 case KVM_REG_MIPS_COUNT_CTL:
1472 *v = vcpu->arch.count_ctl;
1473 break;
1474 case KVM_REG_MIPS_COUNT_RESUME:
1475 *v = ktime_to_ns(vcpu->arch.count_resume);
1476 break;
1477 case KVM_REG_MIPS_COUNT_HZ:
1478 *v = vcpu->arch.count_hz;
1479 break;
1480 default:
1481 return -EINVAL;
1482 }
1483 return 0;
1484}
1485
1486static int kvm_vz_set_one_reg(struct kvm_vcpu *vcpu,
1487 const struct kvm_one_reg *reg,
1488 s64 v)
1489{
1490 struct mips_coproc *cop0 = vcpu->arch.cop0;
1491 unsigned int idx;
1492 int ret = 0;
1493 unsigned int cur, change;
1494
1495 switch (reg->id) {
1496 case KVM_REG_MIPS_CP0_INDEX:
1497 write_gc0_index(v);
1498 break;
1499 case KVM_REG_MIPS_CP0_ENTRYLO0:
1500 write_gc0_entrylo0(entrylo_user_to_kvm(v));
1501 break;
1502 case KVM_REG_MIPS_CP0_ENTRYLO1:
1503 write_gc0_entrylo1(entrylo_user_to_kvm(v));
1504 break;
1505 case KVM_REG_MIPS_CP0_CONTEXT:
1506 write_gc0_context(v);
1507 break;
James Hogandffe0422017-03-14 10:15:34 +00001508 case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
1509 if (!cpu_guest_has_contextconfig)
1510 return -EINVAL;
1511 write_gc0_contextconfig(v);
1512 break;
James Hoganc992a4f2017-03-14 10:15:31 +00001513 case KVM_REG_MIPS_CP0_USERLOCAL:
1514 if (!cpu_guest_has_userlocal)
1515 return -EINVAL;
1516 write_gc0_userlocal(v);
1517 break;
James Hogandffe0422017-03-14 10:15:34 +00001518#ifdef CONFIG_64BIT
1519 case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
1520 if (!cpu_guest_has_contextconfig)
1521 return -EINVAL;
1522 write_gc0_xcontextconfig(v);
1523 break;
1524#endif
James Hoganc992a4f2017-03-14 10:15:31 +00001525 case KVM_REG_MIPS_CP0_PAGEMASK:
1526 write_gc0_pagemask(v);
1527 break;
1528 case KVM_REG_MIPS_CP0_PAGEGRAIN:
1529 write_gc0_pagegrain(v);
1530 break;
1531 case KVM_REG_MIPS_CP0_WIRED:
1532 change_gc0_wired(MIPSR6_WIRED_WIRED, v);
1533 break;
1534 case KVM_REG_MIPS_CP0_HWRENA:
1535 write_gc0_hwrena(v);
1536 break;
1537 case KVM_REG_MIPS_CP0_BADVADDR:
1538 write_gc0_badvaddr(v);
1539 break;
James Hoganedc89262017-03-14 10:15:33 +00001540 case KVM_REG_MIPS_CP0_BADINSTR:
1541 if (!cpu_guest_has_badinstr)
1542 return -EINVAL;
1543 write_gc0_badinstr(v);
1544 break;
1545 case KVM_REG_MIPS_CP0_BADINSTRP:
1546 if (!cpu_guest_has_badinstrp)
1547 return -EINVAL;
1548 write_gc0_badinstrp(v);
1549 break;
James Hoganc992a4f2017-03-14 10:15:31 +00001550 case KVM_REG_MIPS_CP0_COUNT:
1551 kvm_mips_write_count(vcpu, v);
1552 break;
1553 case KVM_REG_MIPS_CP0_ENTRYHI:
1554 write_gc0_entryhi(v);
1555 break;
1556 case KVM_REG_MIPS_CP0_COMPARE:
1557 kvm_mips_write_compare(vcpu, v, false);
1558 break;
1559 case KVM_REG_MIPS_CP0_STATUS:
1560 write_gc0_status(v);
1561 break;
1562 case KVM_REG_MIPS_CP0_INTCTL:
1563 write_gc0_intctl(v);
1564 break;
1565 case KVM_REG_MIPS_CP0_CAUSE:
1566 /*
1567 * If the timer is stopped or started (DC bit) it must look
1568 * atomic with changes to the timer interrupt pending bit (TI).
1569 * A timer interrupt should not happen in between.
1570 */
1571 if ((read_gc0_cause() ^ v) & CAUSEF_DC) {
1572 if (v & CAUSEF_DC) {
1573 /* disable timer first */
1574 kvm_mips_count_disable_cause(vcpu);
1575 change_gc0_cause((u32)~CAUSEF_DC, v);
1576 } else {
1577 /* enable timer last */
1578 change_gc0_cause((u32)~CAUSEF_DC, v);
1579 kvm_mips_count_enable_cause(vcpu);
1580 }
1581 } else {
1582 write_gc0_cause(v);
1583 }
1584 break;
1585 case KVM_REG_MIPS_CP0_EPC:
1586 write_gc0_epc(v);
1587 break;
1588 case KVM_REG_MIPS_CP0_PRID:
1589 kvm_write_c0_guest_prid(cop0, v);
1590 break;
1591 case KVM_REG_MIPS_CP0_EBASE:
1592 kvm_vz_write_gc0_ebase(v);
1593 break;
1594 case KVM_REG_MIPS_CP0_CONFIG:
1595 cur = read_gc0_config();
1596 change = (cur ^ v) & kvm_vz_config_user_wrmask(vcpu);
1597 if (change) {
1598 v = cur ^ change;
1599 write_gc0_config(v);
1600 }
1601 break;
1602 case KVM_REG_MIPS_CP0_CONFIG1:
1603 if (!cpu_guest_has_conf1)
1604 break;
1605 cur = read_gc0_config1();
1606 change = (cur ^ v) & kvm_vz_config1_user_wrmask(vcpu);
1607 if (change) {
1608 v = cur ^ change;
1609 write_gc0_config1(v);
1610 }
1611 break;
1612 case KVM_REG_MIPS_CP0_CONFIG2:
1613 if (!cpu_guest_has_conf2)
1614 break;
1615 cur = read_gc0_config2();
1616 change = (cur ^ v) & kvm_vz_config2_user_wrmask(vcpu);
1617 if (change) {
1618 v = cur ^ change;
1619 write_gc0_config2(v);
1620 }
1621 break;
1622 case KVM_REG_MIPS_CP0_CONFIG3:
1623 if (!cpu_guest_has_conf3)
1624 break;
1625 cur = read_gc0_config3();
1626 change = (cur ^ v) & kvm_vz_config3_user_wrmask(vcpu);
1627 if (change) {
1628 v = cur ^ change;
1629 write_gc0_config3(v);
1630 }
1631 break;
1632 case KVM_REG_MIPS_CP0_CONFIG4:
1633 if (!cpu_guest_has_conf4)
1634 break;
1635 cur = read_gc0_config4();
1636 change = (cur ^ v) & kvm_vz_config4_user_wrmask(vcpu);
1637 if (change) {
1638 v = cur ^ change;
1639 write_gc0_config4(v);
1640 }
1641 break;
1642 case KVM_REG_MIPS_CP0_CONFIG5:
1643 if (!cpu_guest_has_conf5)
1644 break;
1645 cur = read_gc0_config5();
1646 change = (cur ^ v) & kvm_vz_config5_user_wrmask(vcpu);
1647 if (change) {
1648 v = cur ^ change;
1649 write_gc0_config5(v);
1650 }
1651 break;
1652#ifdef CONFIG_64BIT
1653 case KVM_REG_MIPS_CP0_XCONTEXT:
1654 write_gc0_xcontext(v);
1655 break;
1656#endif
1657 case KVM_REG_MIPS_CP0_ERROREPC:
1658 write_gc0_errorepc(v);
1659 break;
1660 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1661 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1662 if (!cpu_guest_has_kscr(idx))
1663 return -EINVAL;
1664 switch (idx) {
1665 case 2:
1666 write_gc0_kscratch1(v);
1667 break;
1668 case 3:
1669 write_gc0_kscratch2(v);
1670 break;
1671 case 4:
1672 write_gc0_kscratch3(v);
1673 break;
1674 case 5:
1675 write_gc0_kscratch4(v);
1676 break;
1677 case 6:
1678 write_gc0_kscratch5(v);
1679 break;
1680 case 7:
1681 write_gc0_kscratch6(v);
1682 break;
1683 }
1684 break;
1685 case KVM_REG_MIPS_COUNT_CTL:
1686 ret = kvm_mips_set_count_ctl(vcpu, v);
1687 break;
1688 case KVM_REG_MIPS_COUNT_RESUME:
1689 ret = kvm_mips_set_count_resume(vcpu, v);
1690 break;
1691 case KVM_REG_MIPS_COUNT_HZ:
1692 ret = kvm_mips_set_count_hz(vcpu, v);
1693 break;
1694 default:
1695 return -EINVAL;
1696 }
1697 return ret;
1698}
1699
1700#define guestid_cache(cpu) (cpu_data[cpu].guestid_cache)
1701static void kvm_vz_get_new_guestid(unsigned long cpu, struct kvm_vcpu *vcpu)
1702{
1703 unsigned long guestid = guestid_cache(cpu);
1704
1705 if (!(++guestid & GUESTID_MASK)) {
1706 if (cpu_has_vtag_icache)
1707 flush_icache_all();
1708
1709 if (!guestid) /* fix version if needed */
1710 guestid = GUESTID_FIRST_VERSION;
1711
1712 ++guestid; /* guestid 0 reserved for root */
1713
1714 /* start new guestid cycle */
1715 kvm_vz_local_flush_roottlb_all_guests();
1716 kvm_vz_local_flush_guesttlb_all();
1717 }
1718
1719 guestid_cache(cpu) = guestid;
1720}
1721
1722/* Returns 1 if the guest TLB may be clobbered */
1723static int kvm_vz_check_requests(struct kvm_vcpu *vcpu, int cpu)
1724{
1725 int ret = 0;
1726 int i;
1727
1728 if (!vcpu->requests)
1729 return 0;
1730
1731 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1732 if (cpu_has_guestid) {
1733 /* Drop all GuestIDs for this VCPU */
1734 for_each_possible_cpu(i)
1735 vcpu->arch.vzguestid[i] = 0;
1736 /* This will clobber guest TLB contents too */
1737 ret = 1;
1738 }
1739 /*
1740 * For Root ASID Dealias (RAD) we don't do anything here, but we
1741 * still need the request to ensure we recheck asid_flush_mask.
1742 * We can still return 0 as only the root TLB will be affected
1743 * by a root ASID flush.
1744 */
1745 }
1746
1747 return ret;
1748}
1749
1750static void kvm_vz_vcpu_save_wired(struct kvm_vcpu *vcpu)
1751{
1752 unsigned int wired = read_gc0_wired();
1753 struct kvm_mips_tlb *tlbs;
1754 int i;
1755
1756 /* Expand the wired TLB array if necessary */
1757 wired &= MIPSR6_WIRED_WIRED;
1758 if (wired > vcpu->arch.wired_tlb_limit) {
1759 tlbs = krealloc(vcpu->arch.wired_tlb, wired *
1760 sizeof(*vcpu->arch.wired_tlb), GFP_ATOMIC);
1761 if (WARN_ON(!tlbs)) {
1762 /* Save whatever we can */
1763 wired = vcpu->arch.wired_tlb_limit;
1764 } else {
1765 vcpu->arch.wired_tlb = tlbs;
1766 vcpu->arch.wired_tlb_limit = wired;
1767 }
1768 }
1769
1770 if (wired)
1771 /* Save wired entries from the guest TLB */
1772 kvm_vz_save_guesttlb(vcpu->arch.wired_tlb, 0, wired);
1773 /* Invalidate any dropped entries since last time */
1774 for (i = wired; i < vcpu->arch.wired_tlb_used; ++i) {
1775 vcpu->arch.wired_tlb[i].tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
1776 vcpu->arch.wired_tlb[i].tlb_lo[0] = 0;
1777 vcpu->arch.wired_tlb[i].tlb_lo[1] = 0;
1778 vcpu->arch.wired_tlb[i].tlb_mask = 0;
1779 }
1780 vcpu->arch.wired_tlb_used = wired;
1781}
1782
1783static void kvm_vz_vcpu_load_wired(struct kvm_vcpu *vcpu)
1784{
1785 /* Load wired entries into the guest TLB */
1786 if (vcpu->arch.wired_tlb)
1787 kvm_vz_load_guesttlb(vcpu->arch.wired_tlb, 0,
1788 vcpu->arch.wired_tlb_used);
1789}
1790
1791static void kvm_vz_vcpu_load_tlb(struct kvm_vcpu *vcpu, int cpu)
1792{
1793 struct kvm *kvm = vcpu->kvm;
1794 struct mm_struct *gpa_mm = &kvm->arch.gpa_mm;
1795 bool migrated;
1796
1797 /*
1798 * Are we entering guest context on a different CPU to last time?
1799 * If so, the VCPU's guest TLB state on this CPU may be stale.
1800 */
1801 migrated = (vcpu->arch.last_exec_cpu != cpu);
1802 vcpu->arch.last_exec_cpu = cpu;
1803
1804 /*
1805 * A vcpu's GuestID is set in GuestCtl1.ID when the vcpu is loaded and
1806 * remains set until another vcpu is loaded in. As a rule GuestRID
1807 * remains zeroed when in root context unless the kernel is busy
1808 * manipulating guest tlb entries.
1809 */
1810 if (cpu_has_guestid) {
1811 /*
1812 * Check if our GuestID is of an older version and thus invalid.
1813 *
1814 * We also discard the stored GuestID if we've executed on
1815 * another CPU, as the guest mappings may have changed without
1816 * hypervisor knowledge.
1817 */
1818 if (migrated ||
1819 (vcpu->arch.vzguestid[cpu] ^ guestid_cache(cpu)) &
1820 GUESTID_VERSION_MASK) {
1821 kvm_vz_get_new_guestid(cpu, vcpu);
1822 vcpu->arch.vzguestid[cpu] = guestid_cache(cpu);
1823 trace_kvm_guestid_change(vcpu,
1824 vcpu->arch.vzguestid[cpu]);
1825 }
1826
1827 /* Restore GuestID */
1828 change_c0_guestctl1(GUESTID_MASK, vcpu->arch.vzguestid[cpu]);
1829 } else {
1830 /*
1831 * The Guest TLB only stores a single guest's TLB state, so
1832 * flush it if another VCPU has executed on this CPU.
1833 *
1834 * We also flush if we've executed on another CPU, as the guest
1835 * mappings may have changed without hypervisor knowledge.
1836 */
1837 if (migrated || last_exec_vcpu[cpu] != vcpu)
1838 kvm_vz_local_flush_guesttlb_all();
1839 last_exec_vcpu[cpu] = vcpu;
1840
1841 /*
1842 * Root ASID dealiases guest GPA mappings in the root TLB.
1843 * Allocate new root ASID if needed.
1844 */
1845 if (cpumask_test_and_clear_cpu(cpu, &kvm->arch.asid_flush_mask)
1846 || (cpu_context(cpu, gpa_mm) ^ asid_cache(cpu)) &
1847 asid_version_mask(cpu))
1848 get_new_mmu_context(gpa_mm, cpu);
1849 }
1850}
1851
1852static int kvm_vz_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1853{
1854 struct mips_coproc *cop0 = vcpu->arch.cop0;
1855 bool migrated, all;
1856
1857 /*
1858 * Have we migrated to a different CPU?
1859 * If so, any old guest TLB state may be stale.
1860 */
1861 migrated = (vcpu->arch.last_sched_cpu != cpu);
1862
1863 /*
1864 * Was this the last VCPU to run on this CPU?
1865 * If not, any old guest state from this VCPU will have been clobbered.
1866 */
1867 all = migrated || (last_vcpu[cpu] != vcpu);
1868 last_vcpu[cpu] = vcpu;
1869
1870 /*
1871 * Restore CP0_Wired unconditionally as we clear it after use, and
1872 * restore wired guest TLB entries (while in guest context).
1873 */
1874 kvm_restore_gc0_wired(cop0);
1875 if (current->flags & PF_VCPU) {
1876 tlbw_use_hazard();
1877 kvm_vz_vcpu_load_tlb(vcpu, cpu);
1878 kvm_vz_vcpu_load_wired(vcpu);
1879 }
1880
1881 /*
1882 * Restore timer state regardless, as e.g. Cause.TI can change over time
1883 * if left unmaintained.
1884 */
1885 kvm_vz_restore_timer(vcpu);
1886
1887 /* Don't bother restoring registers multiple times unless necessary */
1888 if (!all)
1889 return 0;
1890
1891 /*
1892 * Restore config registers first, as some implementations restrict
1893 * writes to other registers when the corresponding feature bits aren't
1894 * set. For example Status.CU1 cannot be set unless Config1.FP is set.
1895 */
1896 kvm_restore_gc0_config(cop0);
1897 if (cpu_guest_has_conf1)
1898 kvm_restore_gc0_config1(cop0);
1899 if (cpu_guest_has_conf2)
1900 kvm_restore_gc0_config2(cop0);
1901 if (cpu_guest_has_conf3)
1902 kvm_restore_gc0_config3(cop0);
1903 if (cpu_guest_has_conf4)
1904 kvm_restore_gc0_config4(cop0);
1905 if (cpu_guest_has_conf5)
1906 kvm_restore_gc0_config5(cop0);
1907 if (cpu_guest_has_conf6)
1908 kvm_restore_gc0_config6(cop0);
1909 if (cpu_guest_has_conf7)
1910 kvm_restore_gc0_config7(cop0);
1911
1912 kvm_restore_gc0_index(cop0);
1913 kvm_restore_gc0_entrylo0(cop0);
1914 kvm_restore_gc0_entrylo1(cop0);
1915 kvm_restore_gc0_context(cop0);
James Hogandffe0422017-03-14 10:15:34 +00001916 if (cpu_guest_has_contextconfig)
1917 kvm_restore_gc0_contextconfig(cop0);
James Hoganc992a4f2017-03-14 10:15:31 +00001918#ifdef CONFIG_64BIT
1919 kvm_restore_gc0_xcontext(cop0);
James Hogandffe0422017-03-14 10:15:34 +00001920 if (cpu_guest_has_contextconfig)
1921 kvm_restore_gc0_xcontextconfig(cop0);
James Hoganc992a4f2017-03-14 10:15:31 +00001922#endif
1923 kvm_restore_gc0_pagemask(cop0);
1924 kvm_restore_gc0_pagegrain(cop0);
1925 kvm_restore_gc0_hwrena(cop0);
1926 kvm_restore_gc0_badvaddr(cop0);
1927 kvm_restore_gc0_entryhi(cop0);
1928 kvm_restore_gc0_status(cop0);
1929 kvm_restore_gc0_intctl(cop0);
1930 kvm_restore_gc0_epc(cop0);
1931 kvm_vz_write_gc0_ebase(kvm_read_sw_gc0_ebase(cop0));
1932 if (cpu_guest_has_userlocal)
1933 kvm_restore_gc0_userlocal(cop0);
1934
1935 kvm_restore_gc0_errorepc(cop0);
1936
1937 /* restore KScratch registers if enabled in guest */
1938 if (cpu_guest_has_conf4) {
1939 if (cpu_guest_has_kscr(2))
1940 kvm_restore_gc0_kscratch1(cop0);
1941 if (cpu_guest_has_kscr(3))
1942 kvm_restore_gc0_kscratch2(cop0);
1943 if (cpu_guest_has_kscr(4))
1944 kvm_restore_gc0_kscratch3(cop0);
1945 if (cpu_guest_has_kscr(5))
1946 kvm_restore_gc0_kscratch4(cop0);
1947 if (cpu_guest_has_kscr(6))
1948 kvm_restore_gc0_kscratch5(cop0);
1949 if (cpu_guest_has_kscr(7))
1950 kvm_restore_gc0_kscratch6(cop0);
1951 }
1952
James Hoganedc89262017-03-14 10:15:33 +00001953 if (cpu_guest_has_badinstr)
1954 kvm_restore_gc0_badinstr(cop0);
1955 if (cpu_guest_has_badinstrp)
1956 kvm_restore_gc0_badinstrp(cop0);
1957
James Hoganc992a4f2017-03-14 10:15:31 +00001958 /* restore Root.GuestCtl2 from unused Guest guestctl2 register */
1959 if (cpu_has_guestctl2)
1960 write_c0_guestctl2(
1961 cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL]);
1962
1963 return 0;
1964}
1965
1966static int kvm_vz_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
1967{
1968 struct mips_coproc *cop0 = vcpu->arch.cop0;
1969
1970 if (current->flags & PF_VCPU)
1971 kvm_vz_vcpu_save_wired(vcpu);
1972
1973 kvm_lose_fpu(vcpu);
1974
1975 kvm_save_gc0_index(cop0);
1976 kvm_save_gc0_entrylo0(cop0);
1977 kvm_save_gc0_entrylo1(cop0);
1978 kvm_save_gc0_context(cop0);
James Hogandffe0422017-03-14 10:15:34 +00001979 if (cpu_guest_has_contextconfig)
1980 kvm_save_gc0_contextconfig(cop0);
James Hoganc992a4f2017-03-14 10:15:31 +00001981#ifdef CONFIG_64BIT
1982 kvm_save_gc0_xcontext(cop0);
James Hogandffe0422017-03-14 10:15:34 +00001983 if (cpu_guest_has_contextconfig)
1984 kvm_save_gc0_xcontextconfig(cop0);
James Hoganc992a4f2017-03-14 10:15:31 +00001985#endif
1986 kvm_save_gc0_pagemask(cop0);
1987 kvm_save_gc0_pagegrain(cop0);
1988 kvm_save_gc0_wired(cop0);
1989 /* allow wired TLB entries to be overwritten */
1990 clear_gc0_wired(MIPSR6_WIRED_WIRED);
1991 kvm_save_gc0_hwrena(cop0);
1992 kvm_save_gc0_badvaddr(cop0);
1993 kvm_save_gc0_entryhi(cop0);
1994 kvm_save_gc0_status(cop0);
1995 kvm_save_gc0_intctl(cop0);
1996 kvm_save_gc0_epc(cop0);
1997 kvm_write_sw_gc0_ebase(cop0, kvm_vz_read_gc0_ebase());
1998 if (cpu_guest_has_userlocal)
1999 kvm_save_gc0_userlocal(cop0);
2000
2001 /* only save implemented config registers */
2002 kvm_save_gc0_config(cop0);
2003 if (cpu_guest_has_conf1)
2004 kvm_save_gc0_config1(cop0);
2005 if (cpu_guest_has_conf2)
2006 kvm_save_gc0_config2(cop0);
2007 if (cpu_guest_has_conf3)
2008 kvm_save_gc0_config3(cop0);
2009 if (cpu_guest_has_conf4)
2010 kvm_save_gc0_config4(cop0);
2011 if (cpu_guest_has_conf5)
2012 kvm_save_gc0_config5(cop0);
2013 if (cpu_guest_has_conf6)
2014 kvm_save_gc0_config6(cop0);
2015 if (cpu_guest_has_conf7)
2016 kvm_save_gc0_config7(cop0);
2017
2018 kvm_save_gc0_errorepc(cop0);
2019
2020 /* save KScratch registers if enabled in guest */
2021 if (cpu_guest_has_conf4) {
2022 if (cpu_guest_has_kscr(2))
2023 kvm_save_gc0_kscratch1(cop0);
2024 if (cpu_guest_has_kscr(3))
2025 kvm_save_gc0_kscratch2(cop0);
2026 if (cpu_guest_has_kscr(4))
2027 kvm_save_gc0_kscratch3(cop0);
2028 if (cpu_guest_has_kscr(5))
2029 kvm_save_gc0_kscratch4(cop0);
2030 if (cpu_guest_has_kscr(6))
2031 kvm_save_gc0_kscratch5(cop0);
2032 if (cpu_guest_has_kscr(7))
2033 kvm_save_gc0_kscratch6(cop0);
2034 }
2035
James Hoganedc89262017-03-14 10:15:33 +00002036 if (cpu_guest_has_badinstr)
2037 kvm_save_gc0_badinstr(cop0);
2038 if (cpu_guest_has_badinstrp)
2039 kvm_save_gc0_badinstrp(cop0);
2040
James Hoganc992a4f2017-03-14 10:15:31 +00002041 kvm_vz_save_timer(vcpu);
2042
2043 /* save Root.GuestCtl2 in unused Guest guestctl2 register */
2044 if (cpu_has_guestctl2)
2045 cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] =
2046 read_c0_guestctl2();
2047
2048 return 0;
2049}
2050
2051/**
2052 * kvm_vz_resize_guest_vtlb() - Attempt to resize guest VTLB.
2053 * @size: Number of guest VTLB entries (0 < @size <= root VTLB entries).
2054 *
2055 * Attempt to resize the guest VTLB by writing guest Config registers. This is
2056 * necessary for cores with a shared root/guest TLB to avoid overlap with wired
2057 * entries in the root VTLB.
2058 *
2059 * Returns: The resulting guest VTLB size.
2060 */
2061static unsigned int kvm_vz_resize_guest_vtlb(unsigned int size)
2062{
2063 unsigned int config4 = 0, ret = 0, limit;
2064
2065 /* Write MMUSize - 1 into guest Config registers */
2066 if (cpu_guest_has_conf1)
2067 change_gc0_config1(MIPS_CONF1_TLBS,
2068 (size - 1) << MIPS_CONF1_TLBS_SHIFT);
2069 if (cpu_guest_has_conf4) {
2070 config4 = read_gc0_config4();
2071 if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
2072 MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT) {
2073 config4 &= ~MIPS_CONF4_VTLBSIZEEXT;
2074 config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
2075 MIPS_CONF4_VTLBSIZEEXT_SHIFT;
2076 } else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
2077 MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT) {
2078 config4 &= ~MIPS_CONF4_MMUSIZEEXT;
2079 config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
2080 MIPS_CONF4_MMUSIZEEXT_SHIFT;
2081 }
2082 write_gc0_config4(config4);
2083 }
2084
2085 /*
2086 * Set Guest.Wired.Limit = 0 (no limit up to Guest.MMUSize-1), unless it
2087 * would exceed Root.Wired.Limit (clearing Guest.Wired.Wired so write
2088 * not dropped)
2089 */
2090 if (cpu_has_mips_r6) {
2091 limit = (read_c0_wired() & MIPSR6_WIRED_LIMIT) >>
2092 MIPSR6_WIRED_LIMIT_SHIFT;
2093 if (size - 1 <= limit)
2094 limit = 0;
2095 write_gc0_wired(limit << MIPSR6_WIRED_LIMIT_SHIFT);
2096 }
2097
2098 /* Read back MMUSize - 1 */
2099 back_to_back_c0_hazard();
2100 if (cpu_guest_has_conf1)
2101 ret = (read_gc0_config1() & MIPS_CONF1_TLBS) >>
2102 MIPS_CONF1_TLBS_SHIFT;
2103 if (config4) {
2104 if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
2105 MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT)
2106 ret |= ((config4 & MIPS_CONF4_VTLBSIZEEXT) >>
2107 MIPS_CONF4_VTLBSIZEEXT_SHIFT) <<
2108 MIPS_CONF1_TLBS_SIZE;
2109 else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
2110 MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT)
2111 ret |= ((config4 & MIPS_CONF4_MMUSIZEEXT) >>
2112 MIPS_CONF4_MMUSIZEEXT_SHIFT) <<
2113 MIPS_CONF1_TLBS_SIZE;
2114 }
2115 return ret + 1;
2116}
2117
2118static int kvm_vz_hardware_enable(void)
2119{
2120 unsigned int mmu_size, guest_mmu_size, ftlb_size;
2121
2122 /*
2123 * ImgTec cores tend to use a shared root/guest TLB. To avoid overlap of
2124 * root wired and guest entries, the guest TLB may need resizing.
2125 */
2126 mmu_size = current_cpu_data.tlbsizevtlb;
2127 ftlb_size = current_cpu_data.tlbsize - mmu_size;
2128
2129 /* Try switching to maximum guest VTLB size for flush */
2130 guest_mmu_size = kvm_vz_resize_guest_vtlb(mmu_size);
2131 current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
2132 kvm_vz_local_flush_guesttlb_all();
2133
2134 /*
2135 * Reduce to make space for root wired entries and at least 2 root
2136 * non-wired entries. This does assume that long-term wired entries
2137 * won't be added later.
2138 */
2139 guest_mmu_size = mmu_size - num_wired_entries() - 2;
2140 guest_mmu_size = kvm_vz_resize_guest_vtlb(guest_mmu_size);
2141 current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
2142
2143 /*
2144 * Write the VTLB size, but if another CPU has already written, check it
2145 * matches or we won't provide a consistent view to the guest. If this
2146 * ever happens it suggests an asymmetric number of wired entries.
2147 */
2148 if (cmpxchg(&kvm_vz_guest_vtlb_size, 0, guest_mmu_size) &&
2149 WARN(guest_mmu_size != kvm_vz_guest_vtlb_size,
2150 "Available guest VTLB size mismatch"))
2151 return -EINVAL;
2152
2153 /*
2154 * Enable virtualization features granting guest direct control of
2155 * certain features:
2156 * CP0=1: Guest coprocessor 0 context.
2157 * AT=Guest: Guest MMU.
2158 * CG=1: Hit (virtual address) CACHE operations (optional).
2159 * CF=1: Guest Config registers.
2160 * CGI=1: Indexed flush CACHE operations (optional).
2161 */
2162 write_c0_guestctl0(MIPS_GCTL0_CP0 |
2163 (MIPS_GCTL0_AT_GUEST << MIPS_GCTL0_AT_SHIFT) |
2164 MIPS_GCTL0_CG | MIPS_GCTL0_CF);
2165 if (cpu_has_guestctl0ext)
2166 set_c0_guestctl0ext(MIPS_GCTL0EXT_CGI);
2167
2168 if (cpu_has_guestid) {
2169 write_c0_guestctl1(0);
2170 kvm_vz_local_flush_roottlb_all_guests();
2171
2172 GUESTID_MASK = current_cpu_data.guestid_mask;
2173 GUESTID_FIRST_VERSION = GUESTID_MASK + 1;
2174 GUESTID_VERSION_MASK = ~GUESTID_MASK;
2175
2176 current_cpu_data.guestid_cache = GUESTID_FIRST_VERSION;
2177 }
2178
2179 /* clear any pending injected virtual guest interrupts */
2180 if (cpu_has_guestctl2)
2181 clear_c0_guestctl2(0x3f << 10);
2182
2183 return 0;
2184}
2185
2186static void kvm_vz_hardware_disable(void)
2187{
2188 kvm_vz_local_flush_guesttlb_all();
2189
2190 if (cpu_has_guestid) {
2191 write_c0_guestctl1(0);
2192 kvm_vz_local_flush_roottlb_all_guests();
2193 }
2194}
2195
2196static int kvm_vz_check_extension(struct kvm *kvm, long ext)
2197{
2198 int r;
2199
2200 switch (ext) {
2201 case KVM_CAP_MIPS_VZ:
2202 /* we wouldn't be here unless cpu_has_vz */
2203 r = 1;
2204 break;
2205#ifdef CONFIG_64BIT
2206 case KVM_CAP_MIPS_64BIT:
2207 /* We support 64-bit registers/operations and addresses */
2208 r = 2;
2209 break;
2210#endif
2211 default:
2212 r = 0;
2213 break;
2214 }
2215
2216 return r;
2217}
2218
2219static int kvm_vz_vcpu_init(struct kvm_vcpu *vcpu)
2220{
2221 int i;
2222
2223 for_each_possible_cpu(i)
2224 vcpu->arch.vzguestid[i] = 0;
2225
2226 return 0;
2227}
2228
2229static void kvm_vz_vcpu_uninit(struct kvm_vcpu *vcpu)
2230{
2231 int cpu;
2232
2233 /*
2234 * If the VCPU is freed and reused as another VCPU, we don't want the
2235 * matching pointer wrongly hanging around in last_vcpu[] or
2236 * last_exec_vcpu[].
2237 */
2238 for_each_possible_cpu(cpu) {
2239 if (last_vcpu[cpu] == vcpu)
2240 last_vcpu[cpu] = NULL;
2241 if (last_exec_vcpu[cpu] == vcpu)
2242 last_exec_vcpu[cpu] = NULL;
2243 }
2244}
2245
2246static int kvm_vz_vcpu_setup(struct kvm_vcpu *vcpu)
2247{
2248 struct mips_coproc *cop0 = vcpu->arch.cop0;
2249 unsigned long count_hz = 100*1000*1000; /* default to 100 MHz */
2250
2251 /*
2252 * Start off the timer at the same frequency as the host timer, but the
2253 * soft timer doesn't handle frequencies greater than 1GHz yet.
2254 */
2255 if (mips_hpt_frequency && mips_hpt_frequency <= NSEC_PER_SEC)
2256 count_hz = mips_hpt_frequency;
2257 kvm_mips_init_count(vcpu, count_hz);
2258
2259 /*
2260 * Initialize guest register state to valid architectural reset state.
2261 */
2262
2263 /* PageGrain */
2264 if (cpu_has_mips_r6)
2265 kvm_write_sw_gc0_pagegrain(cop0, PG_RIE | PG_XIE | PG_IEC);
2266 /* Wired */
2267 if (cpu_has_mips_r6)
2268 kvm_write_sw_gc0_wired(cop0,
2269 read_gc0_wired() & MIPSR6_WIRED_LIMIT);
2270 /* Status */
2271 kvm_write_sw_gc0_status(cop0, ST0_BEV | ST0_ERL);
2272 if (cpu_has_mips_r6)
2273 kvm_change_sw_gc0_status(cop0, ST0_FR, read_gc0_status());
2274 /* IntCtl */
2275 kvm_write_sw_gc0_intctl(cop0, read_gc0_intctl() &
2276 (INTCTLF_IPFDC | INTCTLF_IPPCI | INTCTLF_IPTI));
2277 /* PRId */
2278 kvm_write_sw_gc0_prid(cop0, boot_cpu_data.processor_id);
2279 /* EBase */
2280 kvm_write_sw_gc0_ebase(cop0, (s32)0x80000000 | vcpu->vcpu_id);
2281 /* Config */
2282 kvm_save_gc0_config(cop0);
2283 /* architecturally writable (e.g. from guest) */
2284 kvm_change_sw_gc0_config(cop0, CONF_CM_CMASK,
2285 _page_cachable_default >> _CACHE_SHIFT);
2286 /* architecturally read only, but maybe writable from root */
2287 kvm_change_sw_gc0_config(cop0, MIPS_CONF_MT, read_c0_config());
2288 if (cpu_guest_has_conf1) {
2289 kvm_set_sw_gc0_config(cop0, MIPS_CONF_M);
2290 /* Config1 */
2291 kvm_save_gc0_config1(cop0);
2292 /* architecturally read only, but maybe writable from root */
2293 kvm_clear_sw_gc0_config1(cop0, MIPS_CONF1_C2 |
2294 MIPS_CONF1_MD |
2295 MIPS_CONF1_PC |
2296 MIPS_CONF1_WR |
2297 MIPS_CONF1_CA |
2298 MIPS_CONF1_FP);
2299 }
2300 if (cpu_guest_has_conf2) {
2301 kvm_set_sw_gc0_config1(cop0, MIPS_CONF_M);
2302 /* Config2 */
2303 kvm_save_gc0_config2(cop0);
2304 }
2305 if (cpu_guest_has_conf3) {
2306 kvm_set_sw_gc0_config2(cop0, MIPS_CONF_M);
2307 /* Config3 */
2308 kvm_save_gc0_config3(cop0);
2309 /* architecturally writable (e.g. from guest) */
2310 kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_ISA_OE);
2311 /* architecturally read only, but maybe writable from root */
2312 kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_MSA |
2313 MIPS_CONF3_BPG |
2314 MIPS_CONF3_ULRI |
2315 MIPS_CONF3_DSP |
2316 MIPS_CONF3_CTXTC |
2317 MIPS_CONF3_ITL |
2318 MIPS_CONF3_LPA |
2319 MIPS_CONF3_VEIC |
2320 MIPS_CONF3_VINT |
2321 MIPS_CONF3_SP |
2322 MIPS_CONF3_CDMM |
2323 MIPS_CONF3_MT |
2324 MIPS_CONF3_SM |
2325 MIPS_CONF3_TL);
2326 }
2327 if (cpu_guest_has_conf4) {
2328 kvm_set_sw_gc0_config3(cop0, MIPS_CONF_M);
2329 /* Config4 */
2330 kvm_save_gc0_config4(cop0);
2331 }
2332 if (cpu_guest_has_conf5) {
2333 kvm_set_sw_gc0_config4(cop0, MIPS_CONF_M);
2334 /* Config5 */
2335 kvm_save_gc0_config5(cop0);
2336 /* architecturally writable (e.g. from guest) */
2337 kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_K |
2338 MIPS_CONF5_CV |
2339 MIPS_CONF5_MSAEN |
2340 MIPS_CONF5_UFE |
2341 MIPS_CONF5_FRE |
2342 MIPS_CONF5_SBRI |
2343 MIPS_CONF5_UFR);
2344 /* architecturally read only, but maybe writable from root */
2345 kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_MRP);
2346 }
2347
James Hogandffe0422017-03-14 10:15:34 +00002348 if (cpu_guest_has_contextconfig) {
2349 /* ContextConfig */
2350 kvm_write_sw_gc0_contextconfig(cop0, 0x007ffff0);
2351#ifdef CONFIG_64BIT
2352 /* XContextConfig */
2353 /* bits SEGBITS-13+3:4 set */
2354 kvm_write_sw_gc0_xcontextconfig(cop0,
2355 ((1ull << (cpu_vmbits - 13)) - 1) << 4);
2356#endif
2357 }
2358
James Hoganc992a4f2017-03-14 10:15:31 +00002359 /* start with no pending virtual guest interrupts */
2360 if (cpu_has_guestctl2)
2361 cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] = 0;
2362
2363 /* Put PC at reset vector */
2364 vcpu->arch.pc = CKSEG1ADDR(0x1fc00000);
2365
2366 return 0;
2367}
2368
2369static void kvm_vz_flush_shadow_all(struct kvm *kvm)
2370{
2371 if (cpu_has_guestid) {
2372 /* Flush GuestID for each VCPU individually */
2373 kvm_flush_remote_tlbs(kvm);
2374 } else {
2375 /*
2376 * For each CPU there is a single GPA ASID used by all VCPUs in
2377 * the VM, so it doesn't make sense for the VCPUs to handle
2378 * invalidation of these ASIDs individually.
2379 *
2380 * Instead mark all CPUs as needing ASID invalidation in
2381 * asid_flush_mask, and just use kvm_flush_remote_tlbs(kvm) to
2382 * kick any running VCPUs so they check asid_flush_mask.
2383 */
2384 cpumask_setall(&kvm->arch.asid_flush_mask);
2385 kvm_flush_remote_tlbs(kvm);
2386 }
2387}
2388
2389static void kvm_vz_flush_shadow_memslot(struct kvm *kvm,
2390 const struct kvm_memory_slot *slot)
2391{
2392 kvm_vz_flush_shadow_all(kvm);
2393}
2394
2395static void kvm_vz_vcpu_reenter(struct kvm_run *run, struct kvm_vcpu *vcpu)
2396{
2397 int cpu = smp_processor_id();
2398 int preserve_guest_tlb;
2399
2400 preserve_guest_tlb = kvm_vz_check_requests(vcpu, cpu);
2401
2402 if (preserve_guest_tlb)
2403 kvm_vz_vcpu_save_wired(vcpu);
2404
2405 kvm_vz_vcpu_load_tlb(vcpu, cpu);
2406
2407 if (preserve_guest_tlb)
2408 kvm_vz_vcpu_load_wired(vcpu);
2409}
2410
2411static int kvm_vz_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
2412{
2413 int cpu = smp_processor_id();
2414 int r;
2415
2416 /* Check if we have any exceptions/interrupts pending */
2417 kvm_mips_deliver_interrupts(vcpu, read_gc0_cause());
2418
2419 kvm_vz_check_requests(vcpu, cpu);
2420 kvm_vz_vcpu_load_tlb(vcpu, cpu);
2421 kvm_vz_vcpu_load_wired(vcpu);
2422
2423 r = vcpu->arch.vcpu_run(run, vcpu);
2424
2425 kvm_vz_vcpu_save_wired(vcpu);
2426
2427 return r;
2428}
2429
2430static struct kvm_mips_callbacks kvm_vz_callbacks = {
2431 .handle_cop_unusable = kvm_trap_vz_handle_cop_unusable,
2432 .handle_tlb_mod = kvm_trap_vz_handle_tlb_st_miss,
2433 .handle_tlb_ld_miss = kvm_trap_vz_handle_tlb_ld_miss,
2434 .handle_tlb_st_miss = kvm_trap_vz_handle_tlb_st_miss,
2435 .handle_addr_err_st = kvm_trap_vz_no_handler,
2436 .handle_addr_err_ld = kvm_trap_vz_no_handler,
2437 .handle_syscall = kvm_trap_vz_no_handler,
2438 .handle_res_inst = kvm_trap_vz_no_handler,
2439 .handle_break = kvm_trap_vz_no_handler,
2440 .handle_msa_disabled = kvm_trap_vz_handle_msa_disabled,
2441 .handle_guest_exit = kvm_trap_vz_handle_guest_exit,
2442
2443 .hardware_enable = kvm_vz_hardware_enable,
2444 .hardware_disable = kvm_vz_hardware_disable,
2445 .check_extension = kvm_vz_check_extension,
2446 .vcpu_init = kvm_vz_vcpu_init,
2447 .vcpu_uninit = kvm_vz_vcpu_uninit,
2448 .vcpu_setup = kvm_vz_vcpu_setup,
2449 .flush_shadow_all = kvm_vz_flush_shadow_all,
2450 .flush_shadow_memslot = kvm_vz_flush_shadow_memslot,
2451 .gva_to_gpa = kvm_vz_gva_to_gpa_cb,
2452 .queue_timer_int = kvm_vz_queue_timer_int_cb,
2453 .dequeue_timer_int = kvm_vz_dequeue_timer_int_cb,
2454 .queue_io_int = kvm_vz_queue_io_int_cb,
2455 .dequeue_io_int = kvm_vz_dequeue_io_int_cb,
2456 .irq_deliver = kvm_vz_irq_deliver_cb,
2457 .irq_clear = kvm_vz_irq_clear_cb,
2458 .num_regs = kvm_vz_num_regs,
2459 .copy_reg_indices = kvm_vz_copy_reg_indices,
2460 .get_one_reg = kvm_vz_get_one_reg,
2461 .set_one_reg = kvm_vz_set_one_reg,
2462 .vcpu_load = kvm_vz_vcpu_load,
2463 .vcpu_put = kvm_vz_vcpu_put,
2464 .vcpu_run = kvm_vz_vcpu_run,
2465 .vcpu_reenter = kvm_vz_vcpu_reenter,
2466};
2467
2468int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
2469{
2470 if (!cpu_has_vz)
2471 return -ENODEV;
2472
2473 /*
2474 * VZ requires at least 2 KScratch registers, so it should have been
2475 * possible to allocate pgd_reg.
2476 */
2477 if (WARN(pgd_reg == -1,
2478 "pgd_reg not allocated even though cpu_has_vz\n"))
2479 return -ENODEV;
2480
2481 pr_info("Starting KVM with MIPS VZ extensions\n");
2482
2483 *install_callbacks = &kvm_vz_callbacks;
2484 return 0;
2485}