blob: 8dc9e64346e61d6a28d9dff91d4fc5351e4de35c [file] [log] [blame]
Sanjay Lale685c682012-11-21 18:34:04 -08001/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * KVM/MIPS: Instruction/Exception emulation
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 */
Sanjay Lale685c682012-11-21 18:34:04 -080011
12#include <linux/errno.h>
13#include <linux/err.h>
James Hogane30492b2014-05-29 10:16:35 +010014#include <linux/ktime.h>
Sanjay Lale685c682012-11-21 18:34:04 -080015#include <linux/kvm_host.h>
16#include <linux/module.h>
17#include <linux/vmalloc.h>
18#include <linux/fs.h>
19#include <linux/bootmem.h>
20#include <linux/random.h>
21#include <asm/page.h>
22#include <asm/cacheflush.h>
James Hoganf4956f62015-12-16 23:49:37 +000023#include <asm/cacheops.h>
Sanjay Lale685c682012-11-21 18:34:04 -080024#include <asm/cpu-info.h>
25#include <asm/mmu_context.h>
26#include <asm/tlbflush.h>
27#include <asm/inst.h>
28
29#undef CONFIG_MIPS_MT
30#include <asm/r4kcache.h>
31#define CONFIG_MIPS_MT
32
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070033#include "interrupt.h"
34#include "commpage.h"
Sanjay Lale685c682012-11-21 18:34:04 -080035
36#include "trace.h"
37
38/*
39 * Compute the return address and do emulate branch simulation, if required.
40 * This function should be called only in branch delay slot active.
41 */
42unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
43 unsigned long instpc)
44{
45 unsigned int dspcontrol;
46 union mips_instruction insn;
47 struct kvm_vcpu_arch *arch = &vcpu->arch;
48 long epc = instpc;
49 long nextpc = KVM_INVALID_INST;
50
51 if (epc & 3)
52 goto unaligned;
53
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070054 /* Read the instruction */
James Hogan8cffd192016-06-09 14:19:08 +010055 insn.word = kvm_get_inst((u32 *) epc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -080056
57 if (insn.word == KVM_INVALID_INST)
58 return KVM_INVALID_INST;
59
60 switch (insn.i_format.opcode) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070061 /* jr and jalr are in r_format format. */
Sanjay Lale685c682012-11-21 18:34:04 -080062 case spec_op:
63 switch (insn.r_format.func) {
64 case jalr_op:
65 arch->gprs[insn.r_format.rd] = epc + 8;
66 /* Fall through */
67 case jr_op:
68 nextpc = arch->gprs[insn.r_format.rs];
69 break;
70 }
71 break;
72
73 /*
74 * This group contains:
75 * bltz_op, bgez_op, bltzl_op, bgezl_op,
76 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
77 */
78 case bcond_op:
79 switch (insn.i_format.rt) {
80 case bltz_op:
81 case bltzl_op:
82 if ((long)arch->gprs[insn.i_format.rs] < 0)
83 epc = epc + 4 + (insn.i_format.simmediate << 2);
84 else
85 epc += 8;
86 nextpc = epc;
87 break;
88
89 case bgez_op:
90 case bgezl_op:
91 if ((long)arch->gprs[insn.i_format.rs] >= 0)
92 epc = epc + 4 + (insn.i_format.simmediate << 2);
93 else
94 epc += 8;
95 nextpc = epc;
96 break;
97
98 case bltzal_op:
99 case bltzall_op:
100 arch->gprs[31] = epc + 8;
101 if ((long)arch->gprs[insn.i_format.rs] < 0)
102 epc = epc + 4 + (insn.i_format.simmediate << 2);
103 else
104 epc += 8;
105 nextpc = epc;
106 break;
107
108 case bgezal_op:
109 case bgezall_op:
110 arch->gprs[31] = epc + 8;
111 if ((long)arch->gprs[insn.i_format.rs] >= 0)
112 epc = epc + 4 + (insn.i_format.simmediate << 2);
113 else
114 epc += 8;
115 nextpc = epc;
116 break;
117 case bposge32_op:
118 if (!cpu_has_dsp)
119 goto sigill;
120
121 dspcontrol = rddsp(0x01);
122
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700123 if (dspcontrol >= 32)
Sanjay Lale685c682012-11-21 18:34:04 -0800124 epc = epc + 4 + (insn.i_format.simmediate << 2);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700125 else
Sanjay Lale685c682012-11-21 18:34:04 -0800126 epc += 8;
127 nextpc = epc;
128 break;
129 }
130 break;
131
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700132 /* These are unconditional and in j_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800133 case jal_op:
134 arch->gprs[31] = instpc + 8;
135 case j_op:
136 epc += 4;
137 epc >>= 28;
138 epc <<= 28;
139 epc |= (insn.j_format.target << 2);
140 nextpc = epc;
141 break;
142
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700143 /* These are conditional and in i_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800144 case beq_op:
145 case beql_op:
146 if (arch->gprs[insn.i_format.rs] ==
147 arch->gprs[insn.i_format.rt])
148 epc = epc + 4 + (insn.i_format.simmediate << 2);
149 else
150 epc += 8;
151 nextpc = epc;
152 break;
153
154 case bne_op:
155 case bnel_op:
156 if (arch->gprs[insn.i_format.rs] !=
157 arch->gprs[insn.i_format.rt])
158 epc = epc + 4 + (insn.i_format.simmediate << 2);
159 else
160 epc += 8;
161 nextpc = epc;
162 break;
163
James Hogan2e0badf2016-07-04 19:35:12 +0100164 case blez_op: /* POP06 */
165#ifndef CONFIG_CPU_MIPSR6
166 case blezl_op: /* removed in R6 */
167#endif
168 if (insn.i_format.rt != 0)
169 goto compact_branch;
Sanjay Lale685c682012-11-21 18:34:04 -0800170 if ((long)arch->gprs[insn.i_format.rs] <= 0)
171 epc = epc + 4 + (insn.i_format.simmediate << 2);
172 else
173 epc += 8;
174 nextpc = epc;
175 break;
176
James Hogan2e0badf2016-07-04 19:35:12 +0100177 case bgtz_op: /* POP07 */
178#ifndef CONFIG_CPU_MIPSR6
179 case bgtzl_op: /* removed in R6 */
180#endif
181 if (insn.i_format.rt != 0)
182 goto compact_branch;
Sanjay Lale685c682012-11-21 18:34:04 -0800183 if ((long)arch->gprs[insn.i_format.rs] > 0)
184 epc = epc + 4 + (insn.i_format.simmediate << 2);
185 else
186 epc += 8;
187 nextpc = epc;
188 break;
189
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700190 /* And now the FPA/cp1 branch instructions. */
Sanjay Lale685c682012-11-21 18:34:04 -0800191 case cop1_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700192 kvm_err("%s: unsupported cop1_op\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800193 break;
James Hogan2e0badf2016-07-04 19:35:12 +0100194
195#ifdef CONFIG_CPU_MIPSR6
196 /* R6 added the following compact branches with forbidden slots */
197 case blezl_op: /* POP26 */
198 case bgtzl_op: /* POP27 */
199 /* only rt == 0 isn't compact branch */
200 if (insn.i_format.rt != 0)
201 goto compact_branch;
202 break;
203 case pop10_op:
204 case pop30_op:
205 /* only rs == rt == 0 is reserved, rest are compact branches */
206 if (insn.i_format.rs != 0 || insn.i_format.rt != 0)
207 goto compact_branch;
208 break;
209 case pop66_op:
210 case pop76_op:
211 /* only rs == 0 isn't compact branch */
212 if (insn.i_format.rs != 0)
213 goto compact_branch;
214 break;
215compact_branch:
216 /*
217 * If we've hit an exception on the forbidden slot, then
218 * the branch must not have been taken.
219 */
220 epc += 8;
221 nextpc = epc;
222 break;
223#else
224compact_branch:
225 /* Compact branches not supported before R6 */
226 break;
227#endif
Sanjay Lale685c682012-11-21 18:34:04 -0800228 }
229
230 return nextpc;
231
232unaligned:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700233 kvm_err("%s: unaligned epc\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800234 return nextpc;
235
236sigill:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700237 kvm_err("%s: DSP branch but not DSP ASE\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800238 return nextpc;
239}
240
James Hoganbdb7ed82016-06-09 14:19:07 +0100241enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
Sanjay Lale685c682012-11-21 18:34:04 -0800242{
243 unsigned long branch_pc;
244 enum emulation_result er = EMULATE_DONE;
245
246 if (cause & CAUSEF_BD) {
247 branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
248 if (branch_pc == KVM_INVALID_INST) {
249 er = EMULATE_FAIL;
250 } else {
251 vcpu->arch.pc = branch_pc;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700252 kvm_debug("BD update_pc(): New PC: %#lx\n",
253 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800254 }
255 } else
256 vcpu->arch.pc += 4;
257
258 kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
259
260 return er;
261}
262
James Hogane30492b2014-05-29 10:16:35 +0100263/**
264 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
265 * @vcpu: Virtual CPU.
Sanjay Lale685c682012-11-21 18:34:04 -0800266 *
James Hoganf8239342014-05-29 10:16:37 +0100267 * Returns: 1 if the CP0_Count timer is disabled by either the guest
268 * CP0_Cause.DC bit or the count_ctl.DC bit.
James Hogane30492b2014-05-29 10:16:35 +0100269 * 0 otherwise (in which case CP0_Count timer is running).
Sanjay Lale685c682012-11-21 18:34:04 -0800270 */
James Hogane30492b2014-05-29 10:16:35 +0100271static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -0800272{
273 struct mips_coproc *cop0 = vcpu->arch.cop0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700274
James Hoganf8239342014-05-29 10:16:37 +0100275 return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
276 (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
James Hogane30492b2014-05-29 10:16:35 +0100277}
Sanjay Lale685c682012-11-21 18:34:04 -0800278
James Hogane30492b2014-05-29 10:16:35 +0100279/**
280 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
281 *
282 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
283 *
284 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
285 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100286static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100287{
288 s64 now_ns, periods;
289 u64 delta;
290
291 now_ns = ktime_to_ns(now);
292 delta = now_ns + vcpu->arch.count_dyn_bias;
293
294 if (delta >= vcpu->arch.count_period) {
295 /* If delta is out of safe range the bias needs adjusting */
296 periods = div64_s64(now_ns, vcpu->arch.count_period);
297 vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
298 /* Recalculate delta with new bias */
299 delta = now_ns + vcpu->arch.count_dyn_bias;
Sanjay Lale685c682012-11-21 18:34:04 -0800300 }
301
James Hogane30492b2014-05-29 10:16:35 +0100302 /*
303 * We've ensured that:
304 * delta < count_period
305 *
306 * Therefore the intermediate delta*count_hz will never overflow since
307 * at the boundary condition:
308 * delta = count_period
309 * delta = NSEC_PER_SEC * 2^32 / count_hz
310 * delta * count_hz = NSEC_PER_SEC * 2^32
311 */
312 return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
313}
314
315/**
James Hoganf8239342014-05-29 10:16:37 +0100316 * kvm_mips_count_time() - Get effective current time.
317 * @vcpu: Virtual CPU.
318 *
319 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
320 * except when the master disable bit is set in count_ctl, in which case it is
321 * count_resume, i.e. the time that the count was disabled.
322 *
323 * Returns: Effective monotonic ktime for CP0_Count.
324 */
325static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
326{
327 if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
328 return vcpu->arch.count_resume;
329
330 return ktime_get();
331}
332
333/**
James Hogane30492b2014-05-29 10:16:35 +0100334 * kvm_mips_read_count_running() - Read the current count value as if running.
335 * @vcpu: Virtual CPU.
336 * @now: Kernel time to read CP0_Count at.
337 *
338 * Returns the current guest CP0_Count register at time @now and handles if the
339 * timer interrupt is pending and hasn't been handled yet.
340 *
341 * Returns: The current value of the guest CP0_Count register.
342 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100343static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100344{
James Hogan4355c442016-04-22 10:38:45 +0100345 struct mips_coproc *cop0 = vcpu->arch.cop0;
346 ktime_t expires, threshold;
James Hogan8cffd192016-06-09 14:19:08 +0100347 u32 count, compare;
James Hogane30492b2014-05-29 10:16:35 +0100348 int running;
349
James Hogan4355c442016-04-22 10:38:45 +0100350 /* Calculate the biased and scaled guest CP0_Count */
351 count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
352 compare = kvm_read_c0_guest_compare(cop0);
353
354 /*
355 * Find whether CP0_Count has reached the closest timer interrupt. If
356 * not, we shouldn't inject it.
357 */
James Hogan8cffd192016-06-09 14:19:08 +0100358 if ((s32)(count - compare) < 0)
James Hogan4355c442016-04-22 10:38:45 +0100359 return count;
360
361 /*
362 * The CP0_Count we're going to return has already reached the closest
363 * timer interrupt. Quickly check if it really is a new interrupt by
364 * looking at whether the interval until the hrtimer expiry time is
365 * less than 1/4 of the timer period.
366 */
James Hogane30492b2014-05-29 10:16:35 +0100367 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
James Hogan4355c442016-04-22 10:38:45 +0100368 threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
369 if (ktime_before(expires, threshold)) {
James Hogane30492b2014-05-29 10:16:35 +0100370 /*
371 * Cancel it while we handle it so there's no chance of
372 * interference with the timeout handler.
373 */
374 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
375
376 /* Nothing should be waiting on the timeout */
377 kvm_mips_callbacks->queue_timer_int(vcpu);
378
379 /*
380 * Restart the timer if it was running based on the expiry time
381 * we read, so that we don't push it back 2 periods.
382 */
383 if (running) {
384 expires = ktime_add_ns(expires,
385 vcpu->arch.count_period);
386 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
387 HRTIMER_MODE_ABS);
388 }
389 }
390
James Hogan4355c442016-04-22 10:38:45 +0100391 return count;
James Hogane30492b2014-05-29 10:16:35 +0100392}
393
394/**
395 * kvm_mips_read_count() - Read the current count value.
396 * @vcpu: Virtual CPU.
397 *
398 * Read the current guest CP0_Count value, taking into account whether the timer
399 * is stopped.
400 *
401 * Returns: The current guest CP0_Count value.
402 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100403u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
James Hogane30492b2014-05-29 10:16:35 +0100404{
405 struct mips_coproc *cop0 = vcpu->arch.cop0;
406
407 /* If count disabled just read static copy of count */
408 if (kvm_mips_count_disabled(vcpu))
409 return kvm_read_c0_guest_count(cop0);
410
411 return kvm_mips_read_count_running(vcpu, ktime_get());
412}
413
414/**
415 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
416 * @vcpu: Virtual CPU.
417 * @count: Output pointer for CP0_Count value at point of freeze.
418 *
419 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
420 * at the point it was frozen. It is guaranteed that any pending interrupts at
421 * the point it was frozen are handled, and none after that point.
422 *
423 * This is useful where the time/CP0_Count is needed in the calculation of the
424 * new parameters.
425 *
426 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
427 *
428 * Returns: The ktime at the point of freeze.
429 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100430static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
James Hogane30492b2014-05-29 10:16:35 +0100431{
432 ktime_t now;
433
434 /* stop hrtimer before finding time */
435 hrtimer_cancel(&vcpu->arch.comparecount_timer);
436 now = ktime_get();
437
438 /* find count at this point and handle pending hrtimer */
439 *count = kvm_mips_read_count_running(vcpu, now);
440
441 return now;
442}
443
James Hogane30492b2014-05-29 10:16:35 +0100444/**
445 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
446 * @vcpu: Virtual CPU.
447 * @now: ktime at point of resume.
448 * @count: CP0_Count at point of resume.
449 *
450 * Resumes the timer and updates the timer expiry based on @now and @count.
451 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
452 * parameters need to be changed.
453 *
454 * It is guaranteed that a timer interrupt immediately after resume will be
455 * handled, but not if CP_Compare is exactly at @count. That case is already
456 * handled by kvm_mips_freeze_timer().
457 *
458 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
459 */
460static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
James Hoganbdb7ed82016-06-09 14:19:07 +0100461 ktime_t now, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100462{
463 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100464 u32 compare;
James Hogane30492b2014-05-29 10:16:35 +0100465 u64 delta;
466 ktime_t expire;
467
468 /* Calculate timeout (wrap 0 to 2^32) */
469 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100470 delta = (u64)(u32)(compare - count - 1) + 1;
James Hogane30492b2014-05-29 10:16:35 +0100471 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
472 expire = ktime_add_ns(now, delta);
473
474 /* Update hrtimer to use new timeout */
475 hrtimer_cancel(&vcpu->arch.comparecount_timer);
476 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
477}
478
479/**
James Hogane30492b2014-05-29 10:16:35 +0100480 * kvm_mips_write_count() - Modify the count and update timer.
481 * @vcpu: Virtual CPU.
482 * @count: Guest CP0_Count value to set.
483 *
484 * Sets the CP0_Count value and updates the timer accordingly.
485 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100486void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100487{
488 struct mips_coproc *cop0 = vcpu->arch.cop0;
489 ktime_t now;
490
491 /* Calculate bias */
James Hoganf8239342014-05-29 10:16:37 +0100492 now = kvm_mips_count_time(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100493 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
494
495 if (kvm_mips_count_disabled(vcpu))
496 /* The timer's disabled, adjust the static count */
497 kvm_write_c0_guest_count(cop0, count);
498 else
499 /* Update timeout */
500 kvm_mips_resume_hrtimer(vcpu, now, count);
501}
502
503/**
504 * kvm_mips_init_count() - Initialise timer.
505 * @vcpu: Virtual CPU.
506 *
507 * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
508 * it going if it's enabled.
509 */
510void kvm_mips_init_count(struct kvm_vcpu *vcpu)
511{
512 /* 100 MHz */
513 vcpu->arch.count_hz = 100*1000*1000;
514 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
515 vcpu->arch.count_hz);
516 vcpu->arch.count_dyn_bias = 0;
517
518 /* Starting at 0 */
519 kvm_mips_write_count(vcpu, 0);
520}
521
522/**
James Hoganf74a8e22014-05-29 10:16:38 +0100523 * kvm_mips_set_count_hz() - Update the frequency of the timer.
524 * @vcpu: Virtual CPU.
525 * @count_hz: Frequency of CP0_Count timer in Hz.
526 *
527 * Change the frequency of the CP0_Count timer. This is done atomically so that
528 * CP0_Count is continuous and no timer interrupt is lost.
529 *
530 * Returns: -EINVAL if @count_hz is out of range.
531 * 0 on success.
532 */
533int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
534{
535 struct mips_coproc *cop0 = vcpu->arch.cop0;
536 int dc;
537 ktime_t now;
538 u32 count;
539
540 /* ensure the frequency is in a sensible range... */
541 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
542 return -EINVAL;
543 /* ... and has actually changed */
544 if (vcpu->arch.count_hz == count_hz)
545 return 0;
546
547 /* Safely freeze timer so we can keep it continuous */
548 dc = kvm_mips_count_disabled(vcpu);
549 if (dc) {
550 now = kvm_mips_count_time(vcpu);
551 count = kvm_read_c0_guest_count(cop0);
552 } else {
553 now = kvm_mips_freeze_hrtimer(vcpu, &count);
554 }
555
556 /* Update the frequency */
557 vcpu->arch.count_hz = count_hz;
558 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
559 vcpu->arch.count_dyn_bias = 0;
560
561 /* Calculate adjusted bias so dynamic count is unchanged */
562 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
563
564 /* Update and resume hrtimer */
565 if (!dc)
566 kvm_mips_resume_hrtimer(vcpu, now, count);
567 return 0;
568}
569
570/**
James Hogane30492b2014-05-29 10:16:35 +0100571 * kvm_mips_write_compare() - Modify compare and update timer.
572 * @vcpu: Virtual CPU.
573 * @compare: New CP0_Compare value.
James Hoganb45bacd2016-04-22 10:38:46 +0100574 * @ack: Whether to acknowledge timer interrupt.
James Hogane30492b2014-05-29 10:16:35 +0100575 *
576 * Update CP0_Compare to a new value and update the timeout.
James Hoganb45bacd2016-04-22 10:38:46 +0100577 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
578 * any pending timer interrupt is preserved.
James Hogane30492b2014-05-29 10:16:35 +0100579 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100580void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
James Hogane30492b2014-05-29 10:16:35 +0100581{
582 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganb45bacd2016-04-22 10:38:46 +0100583 int dc;
584 u32 old_compare = kvm_read_c0_guest_compare(cop0);
585 ktime_t now;
James Hogan8cffd192016-06-09 14:19:08 +0100586 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100587
588 /* if unchanged, must just be an ack */
James Hoganb45bacd2016-04-22 10:38:46 +0100589 if (old_compare == compare) {
590 if (!ack)
591 return;
592 kvm_mips_callbacks->dequeue_timer_int(vcpu);
593 kvm_write_c0_guest_compare(cop0, compare);
James Hogane30492b2014-05-29 10:16:35 +0100594 return;
James Hoganb45bacd2016-04-22 10:38:46 +0100595 }
James Hogane30492b2014-05-29 10:16:35 +0100596
James Hoganb45bacd2016-04-22 10:38:46 +0100597 /* freeze_hrtimer() takes care of timer interrupts <= count */
598 dc = kvm_mips_count_disabled(vcpu);
599 if (!dc)
600 now = kvm_mips_freeze_hrtimer(vcpu, &count);
601
602 if (ack)
603 kvm_mips_callbacks->dequeue_timer_int(vcpu);
604
James Hogane30492b2014-05-29 10:16:35 +0100605 kvm_write_c0_guest_compare(cop0, compare);
606
James Hoganb45bacd2016-04-22 10:38:46 +0100607 /* resume_hrtimer() takes care of timer interrupts > count */
608 if (!dc)
609 kvm_mips_resume_hrtimer(vcpu, now, count);
James Hogane30492b2014-05-29 10:16:35 +0100610}
611
612/**
613 * kvm_mips_count_disable() - Disable count.
614 * @vcpu: Virtual CPU.
615 *
616 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
617 * time will be handled but not after.
618 *
James Hoganf8239342014-05-29 10:16:37 +0100619 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
620 * count_ctl.DC has been set (count disabled).
James Hogane30492b2014-05-29 10:16:35 +0100621 *
622 * Returns: The time that the timer was stopped.
623 */
624static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
625{
626 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100627 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100628 ktime_t now;
629
630 /* Stop hrtimer */
631 hrtimer_cancel(&vcpu->arch.comparecount_timer);
632
633 /* Set the static count from the dynamic count, handling pending TI */
634 now = ktime_get();
635 count = kvm_mips_read_count_running(vcpu, now);
636 kvm_write_c0_guest_count(cop0, count);
637
638 return now;
639}
640
641/**
642 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
643 * @vcpu: Virtual CPU.
644 *
645 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
James Hoganf8239342014-05-29 10:16:37 +0100646 * before the final stop time will be handled if the timer isn't disabled by
647 * count_ctl.DC, but not after.
James Hogane30492b2014-05-29 10:16:35 +0100648 *
649 * Assumes CP0_Cause.DC is clear (count enabled).
650 */
651void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
652{
653 struct mips_coproc *cop0 = vcpu->arch.cop0;
654
655 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
James Hoganf8239342014-05-29 10:16:37 +0100656 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
657 kvm_mips_count_disable(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100658}
659
660/**
661 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
662 * @vcpu: Virtual CPU.
663 *
664 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
James Hoganf8239342014-05-29 10:16:37 +0100665 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
666 * potentially before even returning, so the caller should be careful with
667 * ordering of CP0_Cause modifications so as not to lose it.
James Hogane30492b2014-05-29 10:16:35 +0100668 *
669 * Assumes CP0_Cause.DC is set (count disabled).
670 */
671void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
672{
673 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100674 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100675
676 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
677
678 /*
679 * Set the dynamic count to match the static count.
James Hoganf8239342014-05-29 10:16:37 +0100680 * This starts the hrtimer if count_ctl.DC allows it.
681 * Otherwise it conveniently updates the biases.
James Hogane30492b2014-05-29 10:16:35 +0100682 */
683 count = kvm_read_c0_guest_count(cop0);
684 kvm_mips_write_count(vcpu, count);
685}
686
687/**
James Hoganf8239342014-05-29 10:16:37 +0100688 * kvm_mips_set_count_ctl() - Update the count control KVM register.
689 * @vcpu: Virtual CPU.
690 * @count_ctl: Count control register new value.
691 *
692 * Set the count control KVM register. The timer is updated accordingly.
693 *
694 * Returns: -EINVAL if reserved bits are set.
695 * 0 on success.
696 */
697int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
698{
699 struct mips_coproc *cop0 = vcpu->arch.cop0;
700 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
701 s64 delta;
702 ktime_t expire, now;
James Hogan8cffd192016-06-09 14:19:08 +0100703 u32 count, compare;
James Hoganf8239342014-05-29 10:16:37 +0100704
705 /* Only allow defined bits to be changed */
706 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
707 return -EINVAL;
708
709 /* Apply new value */
710 vcpu->arch.count_ctl = count_ctl;
711
712 /* Master CP0_Count disable */
713 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
714 /* Is CP0_Cause.DC already disabling CP0_Count? */
715 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
716 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
717 /* Just record the current time */
718 vcpu->arch.count_resume = ktime_get();
719 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
720 /* disable timer and record current time */
721 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
722 } else {
723 /*
724 * Calculate timeout relative to static count at resume
725 * time (wrap 0 to 2^32).
726 */
727 count = kvm_read_c0_guest_count(cop0);
728 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100729 delta = (u64)(u32)(compare - count - 1) + 1;
James Hoganf8239342014-05-29 10:16:37 +0100730 delta = div_u64(delta * NSEC_PER_SEC,
731 vcpu->arch.count_hz);
732 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
733
734 /* Handle pending interrupt */
735 now = ktime_get();
736 if (ktime_compare(now, expire) >= 0)
737 /* Nothing should be waiting on the timeout */
738 kvm_mips_callbacks->queue_timer_int(vcpu);
739
740 /* Resume hrtimer without changing bias */
741 count = kvm_mips_read_count_running(vcpu, now);
742 kvm_mips_resume_hrtimer(vcpu, now, count);
743 }
744 }
745
746 return 0;
747}
748
749/**
750 * kvm_mips_set_count_resume() - Update the count resume KVM register.
751 * @vcpu: Virtual CPU.
752 * @count_resume: Count resume register new value.
753 *
754 * Set the count resume KVM register.
755 *
756 * Returns: -EINVAL if out of valid range (0..now).
757 * 0 on success.
758 */
759int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
760{
761 /*
762 * It doesn't make sense for the resume time to be in the future, as it
763 * would be possible for the next interrupt to be more than a full
764 * period in the future.
765 */
766 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
767 return -EINVAL;
768
769 vcpu->arch.count_resume = ns_to_ktime(count_resume);
770 return 0;
771}
772
773/**
James Hogane30492b2014-05-29 10:16:35 +0100774 * kvm_mips_count_timeout() - Push timer forward on timeout.
775 * @vcpu: Virtual CPU.
776 *
777 * Handle an hrtimer event by push the hrtimer forward a period.
778 *
779 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
780 */
781enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
782{
783 /* Add the Count period to the current expiry time */
784 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
785 vcpu->arch.count_period);
786 return HRTIMER_RESTART;
Sanjay Lale685c682012-11-21 18:34:04 -0800787}
788
789enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
790{
791 struct mips_coproc *cop0 = vcpu->arch.cop0;
792 enum emulation_result er = EMULATE_DONE;
793
794 if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
795 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
796 kvm_read_c0_guest_epc(cop0));
797 kvm_clear_c0_guest_status(cop0, ST0_EXL);
798 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
799
800 } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
801 kvm_clear_c0_guest_status(cop0, ST0_ERL);
802 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
803 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700804 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
805 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800806 er = EMULATE_FAIL;
807 }
808
809 return er;
810}
811
812enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
813{
Sanjay Lale685c682012-11-21 18:34:04 -0800814 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
815 vcpu->arch.pending_exceptions);
816
817 ++vcpu->stat.wait_exits;
James Hogan1e09e862016-06-14 09:40:12 +0100818 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
Sanjay Lale685c682012-11-21 18:34:04 -0800819 if (!vcpu->arch.pending_exceptions) {
820 vcpu->arch.wait = 1;
821 kvm_vcpu_block(vcpu);
822
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700823 /*
824 * We we are runnable, then definitely go off to user space to
825 * check if any I/O interrupts are pending.
Sanjay Lale685c682012-11-21 18:34:04 -0800826 */
827 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
828 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
829 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
830 }
831 }
832
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700833 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800834}
835
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700836/*
837 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
838 * we can catch this, if things ever change
Sanjay Lale685c682012-11-21 18:34:04 -0800839 */
840enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
841{
842 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100843 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800844
James Hogan8cffd192016-06-09 14:19:08 +0100845 kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700846 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -0800847}
848
James Hogan91e4f1b2016-09-15 17:20:06 +0100849/**
850 * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map.
851 * @vcpu: VCPU with changed mappings.
852 * @tlb: TLB entry being removed.
853 *
854 * This is called to indicate a single change in guest MMU mappings, so that we
855 * can arrange TLB flushes on this and other CPUs.
856 */
857static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu,
858 struct kvm_mips_tlb *tlb)
859{
860 int cpu, i;
861 bool user;
862
863 /* No need to flush for entries which are already invalid */
864 if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V))
865 return;
866 /* User address space doesn't need flushing for KSeg2/3 changes */
867 user = tlb->tlb_hi < KVM_GUEST_KSEG0;
868
869 preempt_disable();
870
871 /*
872 * Probe the shadow host TLB for the entry being overwritten, if one
873 * matches, invalidate it
874 */
875 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
876
877 /* Invalidate the whole ASID on other CPUs */
878 cpu = smp_processor_id();
879 for_each_possible_cpu(i) {
880 if (i == cpu)
881 continue;
882 if (user)
883 vcpu->arch.guest_user_asid[i] = 0;
884 vcpu->arch.guest_kernel_asid[i] = 0;
885 }
886
887 preempt_enable();
888}
889
Sanjay Lale685c682012-11-21 18:34:04 -0800890/* Write Guest TLB Entry @ Index */
891enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
892{
893 struct mips_coproc *cop0 = vcpu->arch.cop0;
894 int index = kvm_read_c0_guest_index(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800895 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +0100896 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800897
898 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700899 kvm_debug("%s: illegal index: %d\n", __func__, index);
James Hogan8cffd192016-06-09 14:19:08 +0100900 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700901 pc, index, kvm_read_c0_guest_entryhi(cop0),
902 kvm_read_c0_guest_entrylo0(cop0),
903 kvm_read_c0_guest_entrylo1(cop0),
904 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800905 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
906 }
907
908 tlb = &vcpu->arch.guest_tlb[index];
James Hogan91e4f1b2016-09-15 17:20:06 +0100909
910 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -0800911
912 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
913 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +0100914 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
915 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800916
James Hogan8cffd192016-06-09 14:19:08 +0100917 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700918 pc, index, kvm_read_c0_guest_entryhi(cop0),
919 kvm_read_c0_guest_entrylo0(cop0),
920 kvm_read_c0_guest_entrylo1(cop0),
921 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800922
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700923 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800924}
925
926/* Write Guest TLB Entry @ Random Index */
927enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
928{
929 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -0800930 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +0100931 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800932 int index;
933
Sanjay Lale685c682012-11-21 18:34:04 -0800934 get_random_bytes(&index, sizeof(index));
935 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
Sanjay Lale685c682012-11-21 18:34:04 -0800936
Sanjay Lale685c682012-11-21 18:34:04 -0800937 tlb = &vcpu->arch.guest_tlb[index];
938
James Hogan91e4f1b2016-09-15 17:20:06 +0100939 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -0800940
941 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
942 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +0100943 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
944 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800945
James Hogan8cffd192016-06-09 14:19:08 +0100946 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700947 pc, index, kvm_read_c0_guest_entryhi(cop0),
948 kvm_read_c0_guest_entrylo0(cop0),
949 kvm_read_c0_guest_entrylo1(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800950
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700951 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800952}
953
954enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
955{
956 struct mips_coproc *cop0 = vcpu->arch.cop0;
957 long entryhi = kvm_read_c0_guest_entryhi(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100958 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800959 int index = -1;
960
961 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
962
963 kvm_write_c0_guest_index(cop0, index);
964
James Hogan8cffd192016-06-09 14:19:08 +0100965 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
Sanjay Lale685c682012-11-21 18:34:04 -0800966 index);
967
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700968 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800969}
970
James Hoganc7716072014-06-26 15:11:29 +0100971/**
972 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
973 * @vcpu: Virtual CPU.
974 *
975 * Finds the mask of bits which are writable in the guest's Config1 CP0
976 * register, by userland (currently read-only to the guest).
977 */
978unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
979{
James Hogan6cdc65e2015-02-03 13:59:38 +0000980 unsigned int mask = 0;
981
982 /* Permit FPU to be present if FPU is supported */
983 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
984 mask |= MIPS_CONF1_FP;
985
986 return mask;
James Hoganc7716072014-06-26 15:11:29 +0100987}
988
989/**
990 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
991 * @vcpu: Virtual CPU.
992 *
993 * Finds the mask of bits which are writable in the guest's Config3 CP0
994 * register, by userland (currently read-only to the guest).
995 */
996unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
997{
James Hogancef061d02016-06-15 19:29:54 +0100998 /* Config4 and ULRI are optional */
999 unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
James Hogan2b6009d2015-02-06 23:01:00 +00001000
1001 /* Permit MSA to be present if MSA is supported */
1002 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
1003 mask |= MIPS_CONF3_MSA;
1004
1005 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001006}
1007
1008/**
1009 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
1010 * @vcpu: Virtual CPU.
1011 *
1012 * Finds the mask of bits which are writable in the guest's Config4 CP0
1013 * register, by userland (currently read-only to the guest).
1014 */
1015unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
1016{
1017 /* Config5 is optional */
James Hogan05108702016-06-15 19:29:56 +01001018 unsigned int mask = MIPS_CONF_M;
1019
1020 /* KScrExist */
1021 mask |= (unsigned int)vcpu->arch.kscratch_enabled << 16;
1022
1023 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001024}
1025
1026/**
1027 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
1028 * @vcpu: Virtual CPU.
1029 *
1030 * Finds the mask of bits which are writable in the guest's Config5 CP0
1031 * register, by the guest itself.
1032 */
1033unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
1034{
James Hogan6cdc65e2015-02-03 13:59:38 +00001035 unsigned int mask = 0;
1036
James Hogan2b6009d2015-02-06 23:01:00 +00001037 /* Permit MSAEn changes if MSA supported and enabled */
1038 if (kvm_mips_guest_has_msa(&vcpu->arch))
1039 mask |= MIPS_CONF5_MSAEN;
1040
James Hogan6cdc65e2015-02-03 13:59:38 +00001041 /*
1042 * Permit guest FPU mode changes if FPU is enabled and the relevant
1043 * feature exists according to FIR register.
1044 */
1045 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1046 if (cpu_has_fre)
1047 mask |= MIPS_CONF5_FRE;
1048 /* We don't support UFR or UFE */
1049 }
1050
1051 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001052}
1053
James Hogan258f3a22016-06-15 19:29:47 +01001054enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
1055 u32 *opc, u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001056 struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001057 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001058{
1059 struct mips_coproc *cop0 = vcpu->arch.cop0;
1060 enum emulation_result er = EMULATE_DONE;
James Hogan258f3a22016-06-15 19:29:47 +01001061 u32 rt, rd, sel;
Sanjay Lale685c682012-11-21 18:34:04 -08001062 unsigned long curr_pc;
James Hogan91e4f1b2016-09-15 17:20:06 +01001063 int cpu, i;
Sanjay Lale685c682012-11-21 18:34:04 -08001064
1065 /*
1066 * Update PC and hold onto current PC in case there is
1067 * an error and we want to rollback the PC
1068 */
1069 curr_pc = vcpu->arch.pc;
1070 er = update_pc(vcpu, cause);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001071 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001072 return er;
Sanjay Lale685c682012-11-21 18:34:04 -08001073
James Hogan258f3a22016-06-15 19:29:47 +01001074 if (inst.co_format.co) {
1075 switch (inst.co_format.func) {
Sanjay Lale685c682012-11-21 18:34:04 -08001076 case tlbr_op: /* Read indexed TLB entry */
1077 er = kvm_mips_emul_tlbr(vcpu);
1078 break;
1079 case tlbwi_op: /* Write indexed */
1080 er = kvm_mips_emul_tlbwi(vcpu);
1081 break;
1082 case tlbwr_op: /* Write random */
1083 er = kvm_mips_emul_tlbwr(vcpu);
1084 break;
1085 case tlbp_op: /* TLB Probe */
1086 er = kvm_mips_emul_tlbp(vcpu);
1087 break;
1088 case rfe_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001089 kvm_err("!!!COP0_RFE!!!\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001090 break;
1091 case eret_op:
1092 er = kvm_mips_emul_eret(vcpu);
1093 goto dont_update_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001094 case wait_op:
1095 er = kvm_mips_emul_wait(vcpu);
1096 break;
1097 }
1098 } else {
James Hogan258f3a22016-06-15 19:29:47 +01001099 rt = inst.c0r_format.rt;
1100 rd = inst.c0r_format.rd;
1101 sel = inst.c0r_format.sel;
1102
1103 switch (inst.c0r_format.rs) {
Sanjay Lale685c682012-11-21 18:34:04 -08001104 case mfc_op:
1105#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1106 cop0->stat[rd][sel]++;
1107#endif
1108 /* Get reg */
1109 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogan172e02d2016-07-08 11:53:28 +01001110 vcpu->arch.gprs[rt] =
1111 (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001112 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1113 vcpu->arch.gprs[rt] = 0x0;
1114#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1115 kvm_mips_trans_mfc0(inst, opc, vcpu);
1116#endif
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001117 } else {
James Hogan172e02d2016-07-08 11:53:28 +01001118 vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
Sanjay Lale685c682012-11-21 18:34:04 -08001119
1120#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1121 kvm_mips_trans_mfc0(inst, opc, vcpu);
1122#endif
1123 }
1124
James Hogan6398da12016-06-14 09:40:15 +01001125 trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
1126 KVM_TRACE_COP0(rd, sel),
1127 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001128 break;
1129
1130 case dmfc_op:
1131 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
James Hogan6398da12016-06-14 09:40:15 +01001132
1133 trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
1134 KVM_TRACE_COP0(rd, sel),
1135 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001136 break;
1137
1138 case mtc_op:
1139#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1140 cop0->stat[rd][sel]++;
1141#endif
James Hogan6398da12016-06-14 09:40:15 +01001142 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
1143 KVM_TRACE_COP0(rd, sel),
1144 vcpu->arch.gprs[rt]);
1145
Sanjay Lale685c682012-11-21 18:34:04 -08001146 if ((rd == MIPS_CP0_TLB_INDEX)
1147 && (vcpu->arch.gprs[rt] >=
1148 KVM_MIPS_GUEST_TLB_SIZE)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001149 kvm_err("Invalid TLB Index: %ld",
1150 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001151 er = EMULATE_FAIL;
1152 break;
1153 }
1154#define C0_EBASE_CORE_MASK 0xff
1155 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
1156 /* Preserve CORE number */
1157 kvm_change_c0_guest_ebase(cop0,
1158 ~(C0_EBASE_CORE_MASK),
1159 vcpu->arch.gprs[rt]);
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001160 kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
1161 kvm_read_c0_guest_ebase(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001162 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
James Hogan8cffd192016-06-09 14:19:08 +01001163 u32 nasid =
Paul Burtonca64c2b2016-05-06 14:36:20 +01001164 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001165 if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
David Daney48c4ac92013-05-13 13:56:44 -07001166 ((kvm_read_c0_guest_entryhi(cop0) &
Paul Burtonca64c2b2016-05-06 14:36:20 +01001167 KVM_ENTRYHI_ASID) != nasid)) {
James Hogan9887d1c2016-06-14 09:40:13 +01001168 trace_kvm_asid_change(vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001169 kvm_read_c0_guest_entryhi(cop0)
James Hogan9887d1c2016-06-14 09:40:13 +01001170 & KVM_ENTRYHI_ASID,
1171 nasid);
Sanjay Lale685c682012-11-21 18:34:04 -08001172
James Hogan25b08c72016-09-16 00:06:43 +01001173 /*
1174 * Regenerate/invalidate kernel MMU
1175 * context.
1176 * The user MMU context will be
1177 * regenerated lazily on re-entry to
1178 * guest user if the guest ASID actually
1179 * changes.
1180 */
James Hogan91e4f1b2016-09-15 17:20:06 +01001181 preempt_disable();
James Hogan91e4f1b2016-09-15 17:20:06 +01001182 cpu = smp_processor_id();
James Hogan25b08c72016-09-16 00:06:43 +01001183 kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm,
1184 cpu, vcpu);
1185 vcpu->arch.guest_kernel_asid[cpu] =
1186 vcpu->arch.guest_kernel_mm.context.asid[cpu];
James Hogan91e4f1b2016-09-15 17:20:06 +01001187 for_each_possible_cpu(i)
James Hogan25b08c72016-09-16 00:06:43 +01001188 if (i != cpu)
James Hogan91e4f1b2016-09-15 17:20:06 +01001189 vcpu->arch.guest_kernel_asid[i] = 0;
James Hogan91e4f1b2016-09-15 17:20:06 +01001190 preempt_enable();
Sanjay Lale685c682012-11-21 18:34:04 -08001191 }
1192 kvm_write_c0_guest_entryhi(cop0,
1193 vcpu->arch.gprs[rt]);
1194 }
1195 /* Are we writing to COUNT */
1196 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001197 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001198 goto done;
1199 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
Sanjay Lale685c682012-11-21 18:34:04 -08001200 /* If we are writing to COMPARE */
1201 /* Clear pending timer interrupt, if any */
James Hogane30492b2014-05-29 10:16:35 +01001202 kvm_mips_write_compare(vcpu,
James Hoganb45bacd2016-04-22 10:38:46 +01001203 vcpu->arch.gprs[rt],
1204 true);
Sanjay Lale685c682012-11-21 18:34:04 -08001205 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
James Hogan6cdc65e2015-02-03 13:59:38 +00001206 unsigned int old_val, val, change;
1207
1208 old_val = kvm_read_c0_guest_status(cop0);
1209 val = vcpu->arch.gprs[rt];
1210 change = val ^ old_val;
1211
1212 /* Make sure that the NMI bit is never set */
1213 val &= ~ST0_NMI;
1214
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001215 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001216 * Don't allow CU1 or FR to be set unless FPU
1217 * capability enabled and exists in guest
1218 * configuration.
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001219 */
James Hogan6cdc65e2015-02-03 13:59:38 +00001220 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1221 val &= ~(ST0_CU1 | ST0_FR);
1222
1223 /*
1224 * Also don't allow FR to be set if host doesn't
1225 * support it.
1226 */
1227 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1228 val &= ~ST0_FR;
1229
1230
1231 /* Handle changes in FPU mode */
1232 preempt_disable();
1233
1234 /*
1235 * FPU and Vector register state is made
1236 * UNPREDICTABLE by a change of FR, so don't
1237 * even bother saving it.
1238 */
1239 if (change & ST0_FR)
1240 kvm_drop_fpu(vcpu);
1241
1242 /*
James Hogan2b6009d2015-02-06 23:01:00 +00001243 * If MSA state is already live, it is undefined
1244 * how it interacts with FR=0 FPU state, and we
1245 * don't want to hit reserved instruction
1246 * exceptions trying to save the MSA state later
1247 * when CU=1 && FR=1, so play it safe and save
1248 * it first.
1249 */
1250 if (change & ST0_CU1 && !(val & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001251 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001252 kvm_lose_fpu(vcpu);
1253
1254 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001255 * Propagate CU1 (FPU enable) changes
1256 * immediately if the FPU context is already
1257 * loaded. When disabling we leave the context
1258 * loaded so it can be quickly enabled again in
1259 * the near future.
1260 */
1261 if (change & ST0_CU1 &&
James Hoganf9431762016-06-14 09:40:10 +01001262 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001263 change_c0_status(ST0_CU1, val);
1264
1265 preempt_enable();
1266
1267 kvm_write_c0_guest_status(cop0, val);
Sanjay Lale685c682012-11-21 18:34:04 -08001268
1269#ifdef CONFIG_KVM_MIPS_DYN_TRANS
James Hogan6cdc65e2015-02-03 13:59:38 +00001270 /*
1271 * If FPU present, we need CU1/FR bits to take
1272 * effect fairly soon.
1273 */
1274 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1275 kvm_mips_trans_mtc0(inst, opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001276#endif
James Hogan6cdc65e2015-02-03 13:59:38 +00001277 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1278 unsigned int old_val, val, change, wrmask;
1279
1280 old_val = kvm_read_c0_guest_config5(cop0);
1281 val = vcpu->arch.gprs[rt];
1282
1283 /* Only a few bits are writable in Config5 */
1284 wrmask = kvm_mips_config5_wrmask(vcpu);
1285 change = (val ^ old_val) & wrmask;
1286 val = old_val ^ change;
1287
1288
James Hogan2b6009d2015-02-06 23:01:00 +00001289 /* Handle changes in FPU/MSA modes */
James Hogan6cdc65e2015-02-03 13:59:38 +00001290 preempt_disable();
1291
1292 /*
1293 * Propagate FRE changes immediately if the FPU
1294 * context is already loaded.
1295 */
1296 if (change & MIPS_CONF5_FRE &&
James Hoganf9431762016-06-14 09:40:10 +01001297 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001298 change_c0_config5(MIPS_CONF5_FRE, val);
1299
James Hogan2b6009d2015-02-06 23:01:00 +00001300 /*
1301 * Propagate MSAEn changes immediately if the
1302 * MSA context is already loaded. When disabling
1303 * we leave the context loaded so it can be
1304 * quickly enabled again in the near future.
1305 */
1306 if (change & MIPS_CONF5_MSAEN &&
James Hoganf9431762016-06-14 09:40:10 +01001307 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001308 change_c0_config5(MIPS_CONF5_MSAEN,
1309 val);
1310
James Hogan6cdc65e2015-02-03 13:59:38 +00001311 preempt_enable();
1312
1313 kvm_write_c0_guest_config5(cop0, val);
James Hogane30492b2014-05-29 10:16:35 +01001314 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
James Hogan8cffd192016-06-09 14:19:08 +01001315 u32 old_cause, new_cause;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001316
James Hogane30492b2014-05-29 10:16:35 +01001317 old_cause = kvm_read_c0_guest_cause(cop0);
1318 new_cause = vcpu->arch.gprs[rt];
1319 /* Update R/W bits */
1320 kvm_change_c0_guest_cause(cop0, 0x08800300,
1321 new_cause);
1322 /* DC bit enabling/disabling timer? */
1323 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1324 if (new_cause & CAUSEF_DC)
1325 kvm_mips_count_disable_cause(vcpu);
1326 else
1327 kvm_mips_count_enable_cause(vcpu);
1328 }
James Hogancef061d02016-06-15 19:29:54 +01001329 } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
1330 u32 mask = MIPS_HWRENA_CPUNUM |
1331 MIPS_HWRENA_SYNCISTEP |
1332 MIPS_HWRENA_CC |
1333 MIPS_HWRENA_CCRES;
1334
1335 if (kvm_read_c0_guest_config3(cop0) &
1336 MIPS_CONF3_ULRI)
1337 mask |= MIPS_HWRENA_ULR;
1338 cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
Sanjay Lale685c682012-11-21 18:34:04 -08001339 } else {
1340 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1341#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1342 kvm_mips_trans_mtc0(inst, opc, vcpu);
1343#endif
1344 }
Sanjay Lale685c682012-11-21 18:34:04 -08001345 break;
1346
1347 case dmtc_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001348 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1349 vcpu->arch.pc, rt, rd, sel);
James Hogan6398da12016-06-14 09:40:15 +01001350 trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
1351 KVM_TRACE_COP0(rd, sel),
1352 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001353 er = EMULATE_FAIL;
1354 break;
1355
James Hoganb2c59632015-12-16 23:49:38 +00001356 case mfmc0_op:
Sanjay Lale685c682012-11-21 18:34:04 -08001357#ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1358 cop0->stat[MIPS_CP0_STATUS][0]++;
1359#endif
James Hogancaa1faa2015-12-16 23:49:26 +00001360 if (rt != 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001361 vcpu->arch.gprs[rt] =
1362 kvm_read_c0_guest_status(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001363 /* EI */
James Hogan258f3a22016-06-15 19:29:47 +01001364 if (inst.mfmc0_format.sc) {
James Hoganb2c59632015-12-16 23:49:38 +00001365 kvm_debug("[%#lx] mfmc0_op: EI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001366 vcpu->arch.pc);
1367 kvm_set_c0_guest_status(cop0, ST0_IE);
1368 } else {
James Hoganb2c59632015-12-16 23:49:38 +00001369 kvm_debug("[%#lx] mfmc0_op: DI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001370 vcpu->arch.pc);
1371 kvm_clear_c0_guest_status(cop0, ST0_IE);
1372 }
1373
1374 break;
1375
1376 case wrpgpr_op:
1377 {
James Hogan8cffd192016-06-09 14:19:08 +01001378 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1379 u32 pss =
Sanjay Lale685c682012-11-21 18:34:04 -08001380 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001381 /*
1382 * We don't support any shadow register sets, so
1383 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1384 */
Sanjay Lale685c682012-11-21 18:34:04 -08001385 if (css || pss) {
1386 er = EMULATE_FAIL;
1387 break;
1388 }
1389 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1390 vcpu->arch.gprs[rt]);
1391 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1392 }
1393 break;
1394 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001395 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
James Hogan258f3a22016-06-15 19:29:47 +01001396 vcpu->arch.pc, inst.c0r_format.rs);
Sanjay Lale685c682012-11-21 18:34:04 -08001397 er = EMULATE_FAIL;
1398 break;
1399 }
1400 }
1401
1402done:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001403 /* Rollback PC only if emulation was unsuccessful */
1404 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001405 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001406
1407dont_update_pc:
1408 /*
1409 * This is for special instructions whose emulation
1410 * updates the PC, so do not overwrite the PC under
1411 * any circumstances
1412 */
1413
1414 return er;
1415}
1416
James Hogan258f3a22016-06-15 19:29:47 +01001417enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
1418 u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001419 struct kvm_run *run,
1420 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001421{
1422 enum emulation_result er = EMULATE_DO_MMIO;
James Hogan258f3a22016-06-15 19:29:47 +01001423 u32 rt;
James Hogan8cffd192016-06-09 14:19:08 +01001424 u32 bytes;
Sanjay Lale685c682012-11-21 18:34:04 -08001425 void *data = run->mmio.data;
1426 unsigned long curr_pc;
1427
1428 /*
1429 * Update PC and hold onto current PC in case there is
1430 * an error and we want to rollback the PC
1431 */
1432 curr_pc = vcpu->arch.pc;
1433 er = update_pc(vcpu, cause);
1434 if (er == EMULATE_FAIL)
1435 return er;
1436
James Hogan258f3a22016-06-15 19:29:47 +01001437 rt = inst.i_format.rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001438
James Hogan258f3a22016-06-15 19:29:47 +01001439 switch (inst.i_format.opcode) {
Sanjay Lale685c682012-11-21 18:34:04 -08001440 case sb_op:
1441 bytes = 1;
1442 if (bytes > sizeof(run->mmio.data)) {
1443 kvm_err("%s: bad MMIO length: %d\n", __func__,
1444 run->mmio.len);
1445 }
1446 run->mmio.phys_addr =
1447 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1448 host_cp0_badvaddr);
1449 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1450 er = EMULATE_FAIL;
1451 break;
1452 }
1453 run->mmio.len = bytes;
1454 run->mmio.is_write = 1;
1455 vcpu->mmio_needed = 1;
1456 vcpu->mmio_is_write = 1;
1457 *(u8 *) data = vcpu->arch.gprs[rt];
1458 kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1459 vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
James Hogan8cffd192016-06-09 14:19:08 +01001460 *(u8 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001461
1462 break;
1463
1464 case sw_op:
1465 bytes = 4;
1466 if (bytes > sizeof(run->mmio.data)) {
1467 kvm_err("%s: bad MMIO length: %d\n", __func__,
1468 run->mmio.len);
1469 }
1470 run->mmio.phys_addr =
1471 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1472 host_cp0_badvaddr);
1473 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1474 er = EMULATE_FAIL;
1475 break;
1476 }
1477
1478 run->mmio.len = bytes;
1479 run->mmio.is_write = 1;
1480 vcpu->mmio_needed = 1;
1481 vcpu->mmio_is_write = 1;
James Hogan8cffd192016-06-09 14:19:08 +01001482 *(u32 *) data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001483
1484 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1485 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8cffd192016-06-09 14:19:08 +01001486 vcpu->arch.gprs[rt], *(u32 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001487 break;
1488
1489 case sh_op:
1490 bytes = 2;
1491 if (bytes > sizeof(run->mmio.data)) {
1492 kvm_err("%s: bad MMIO length: %d\n", __func__,
1493 run->mmio.len);
1494 }
1495 run->mmio.phys_addr =
1496 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1497 host_cp0_badvaddr);
1498 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1499 er = EMULATE_FAIL;
1500 break;
1501 }
1502
1503 run->mmio.len = bytes;
1504 run->mmio.is_write = 1;
1505 vcpu->mmio_needed = 1;
1506 vcpu->mmio_is_write = 1;
James Hogan8cffd192016-06-09 14:19:08 +01001507 *(u16 *) data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001508
1509 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1510 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8cffd192016-06-09 14:19:08 +01001511 vcpu->arch.gprs[rt], *(u32 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001512 break;
1513
1514 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001515 kvm_err("Store not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001516 inst.word);
Sanjay Lale685c682012-11-21 18:34:04 -08001517 er = EMULATE_FAIL;
1518 break;
1519 }
1520
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001521 /* Rollback PC if emulation was unsuccessful */
1522 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001523 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001524
1525 return er;
1526}
1527
James Hogan258f3a22016-06-15 19:29:47 +01001528enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
1529 u32 cause, struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001530 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001531{
1532 enum emulation_result er = EMULATE_DO_MMIO;
James Hogan258f3a22016-06-15 19:29:47 +01001533 u32 op, rt;
James Hogan8cffd192016-06-09 14:19:08 +01001534 u32 bytes;
Sanjay Lale685c682012-11-21 18:34:04 -08001535
James Hogan258f3a22016-06-15 19:29:47 +01001536 rt = inst.i_format.rt;
1537 op = inst.i_format.opcode;
Sanjay Lale685c682012-11-21 18:34:04 -08001538
1539 vcpu->arch.pending_load_cause = cause;
1540 vcpu->arch.io_gpr = rt;
1541
1542 switch (op) {
1543 case lw_op:
1544 bytes = 4;
1545 if (bytes > sizeof(run->mmio.data)) {
1546 kvm_err("%s: bad MMIO length: %d\n", __func__,
1547 run->mmio.len);
1548 er = EMULATE_FAIL;
1549 break;
1550 }
1551 run->mmio.phys_addr =
1552 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1553 host_cp0_badvaddr);
1554 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1555 er = EMULATE_FAIL;
1556 break;
1557 }
1558
1559 run->mmio.len = bytes;
1560 run->mmio.is_write = 0;
1561 vcpu->mmio_needed = 1;
1562 vcpu->mmio_is_write = 0;
1563 break;
1564
1565 case lh_op:
1566 case lhu_op:
1567 bytes = 2;
1568 if (bytes > sizeof(run->mmio.data)) {
1569 kvm_err("%s: bad MMIO length: %d\n", __func__,
1570 run->mmio.len);
1571 er = EMULATE_FAIL;
1572 break;
1573 }
1574 run->mmio.phys_addr =
1575 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1576 host_cp0_badvaddr);
1577 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1578 er = EMULATE_FAIL;
1579 break;
1580 }
1581
1582 run->mmio.len = bytes;
1583 run->mmio.is_write = 0;
1584 vcpu->mmio_needed = 1;
1585 vcpu->mmio_is_write = 0;
1586
1587 if (op == lh_op)
1588 vcpu->mmio_needed = 2;
1589 else
1590 vcpu->mmio_needed = 1;
1591
1592 break;
1593
1594 case lbu_op:
1595 case lb_op:
1596 bytes = 1;
1597 if (bytes > sizeof(run->mmio.data)) {
1598 kvm_err("%s: bad MMIO length: %d\n", __func__,
1599 run->mmio.len);
1600 er = EMULATE_FAIL;
1601 break;
1602 }
1603 run->mmio.phys_addr =
1604 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1605 host_cp0_badvaddr);
1606 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1607 er = EMULATE_FAIL;
1608 break;
1609 }
1610
1611 run->mmio.len = bytes;
1612 run->mmio.is_write = 0;
1613 vcpu->mmio_is_write = 0;
1614
1615 if (op == lb_op)
1616 vcpu->mmio_needed = 2;
1617 else
1618 vcpu->mmio_needed = 1;
1619
1620 break;
1621
1622 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001623 kvm_err("Load not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001624 inst.word);
Sanjay Lale685c682012-11-21 18:34:04 -08001625 er = EMULATE_FAIL;
1626 break;
1627 }
1628
1629 return er;
1630}
1631
James Hogan258f3a22016-06-15 19:29:47 +01001632enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
1633 u32 *opc, u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001634 struct kvm_run *run,
1635 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001636{
1637 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -08001638 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01001639 u32 cache, op_inst, op, base;
1640 s16 offset;
Sanjay Lale685c682012-11-21 18:34:04 -08001641 struct kvm_vcpu_arch *arch = &vcpu->arch;
1642 unsigned long va;
1643 unsigned long curr_pc;
1644
1645 /*
1646 * Update PC and hold onto current PC in case there is
1647 * an error and we want to rollback the PC
1648 */
1649 curr_pc = vcpu->arch.pc;
1650 er = update_pc(vcpu, cause);
1651 if (er == EMULATE_FAIL)
1652 return er;
1653
James Hogan258f3a22016-06-15 19:29:47 +01001654 base = inst.i_format.rs;
1655 op_inst = inst.i_format.rt;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001656 if (cpu_has_mips_r6)
1657 offset = inst.spec3_format.simmediate;
1658 else
1659 offset = inst.i_format.simmediate;
James Hoganf4956f62015-12-16 23:49:37 +00001660 cache = op_inst & CacheOp_Cache;
1661 op = op_inst & CacheOp_Op;
Sanjay Lale685c682012-11-21 18:34:04 -08001662
1663 va = arch->gprs[base] + offset;
1664
1665 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1666 cache, op, base, arch->gprs[base], offset);
1667
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001668 /*
1669 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1670 * invalidate the caches entirely by stepping through all the
1671 * ways/indexes
Sanjay Lale685c682012-11-21 18:34:04 -08001672 */
James Hoganf4956f62015-12-16 23:49:37 +00001673 if (op == Index_Writeback_Inv) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001674 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1675 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1676 arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001677
James Hoganf4956f62015-12-16 23:49:37 +00001678 if (cache == Cache_D)
Sanjay Lale685c682012-11-21 18:34:04 -08001679 r4k_blast_dcache();
James Hoganf4956f62015-12-16 23:49:37 +00001680 else if (cache == Cache_I)
Sanjay Lale685c682012-11-21 18:34:04 -08001681 r4k_blast_icache();
1682 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001683 kvm_err("%s: unsupported CACHE INDEX operation\n",
1684 __func__);
Sanjay Lale685c682012-11-21 18:34:04 -08001685 return EMULATE_FAIL;
1686 }
1687
1688#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1689 kvm_mips_trans_cache_index(inst, opc, vcpu);
1690#endif
1691 goto done;
1692 }
1693
1694 preempt_disable();
1695 if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
James Hogan9b731bc2016-08-11 11:58:15 +01001696 if (kvm_mips_host_tlb_lookup(vcpu, va) < 0 &&
1697 kvm_mips_handle_kseg0_tlb_fault(va, vcpu)) {
1698 kvm_err("%s: handling mapped kseg0 tlb fault for %lx, vcpu: %p, ASID: %#lx\n",
1699 __func__, va, vcpu, read_c0_entryhi());
1700 er = EMULATE_FAIL;
1701 preempt_enable();
1702 goto done;
1703 }
Sanjay Lale685c682012-11-21 18:34:04 -08001704 } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
1705 KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
1706 int index;
1707
1708 /* If an entry already exists then skip */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001709 if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001710 goto skip_fault;
Sanjay Lale685c682012-11-21 18:34:04 -08001711
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001712 /*
1713 * If address not in the guest TLB, then give the guest a fault,
1714 * the resulting handler will do the right thing
Sanjay Lale685c682012-11-21 18:34:04 -08001715 */
1716 index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001717 (kvm_read_c0_guest_entryhi
Paul Burtonca64c2b2016-05-06 14:36:20 +01001718 (cop0) & KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08001719
1720 if (index < 0) {
Sanjay Lale685c682012-11-21 18:34:04 -08001721 vcpu->arch.host_cp0_badvaddr = va;
James Hogan6df82a72016-06-09 10:50:46 +01001722 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001723 er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
1724 vcpu);
1725 preempt_enable();
1726 goto dont_update_pc;
1727 } else {
1728 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001729 /*
1730 * Check if the entry is valid, if not then setup a TLB
1731 * invalid exception to the guest
1732 */
Sanjay Lale685c682012-11-21 18:34:04 -08001733 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan6df82a72016-06-09 10:50:46 +01001734 vcpu->arch.host_cp0_badvaddr = va;
1735 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001736 er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
1737 run, vcpu);
1738 preempt_enable();
1739 goto dont_update_pc;
James Hogan9b731bc2016-08-11 11:58:15 +01001740 }
1741 /*
1742 * We fault an entry from the guest tlb to the
1743 * shadow host TLB
1744 */
1745 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) {
1746 kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
1747 __func__, va, index, vcpu,
1748 read_c0_entryhi());
1749 er = EMULATE_FAIL;
1750 preempt_enable();
1751 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001752 }
1753 }
1754 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001755 kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1756 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001757 er = EMULATE_FAIL;
1758 preempt_enable();
James Hogancc81e942016-06-09 10:50:45 +01001759 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001760
1761 }
1762
1763skip_fault:
1764 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
James Hoganf4956f62015-12-16 23:49:37 +00001765 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
Sanjay Lale685c682012-11-21 18:34:04 -08001766 flush_dcache_line(va);
1767
1768#ifdef CONFIG_KVM_MIPS_DYN_TRANS
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001769 /*
1770 * Replace the CACHE instruction, with a SYNCI, not the same,
1771 * but avoids a trap
1772 */
Sanjay Lale685c682012-11-21 18:34:04 -08001773 kvm_mips_trans_cache_va(inst, opc, vcpu);
1774#endif
James Hoganf4956f62015-12-16 23:49:37 +00001775 } else if (op_inst == Hit_Invalidate_I) {
Sanjay Lale685c682012-11-21 18:34:04 -08001776 flush_dcache_line(va);
1777 flush_icache_line(va);
1778
1779#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1780 /* Replace the CACHE instruction, with a SYNCI */
1781 kvm_mips_trans_cache_va(inst, opc, vcpu);
1782#endif
1783 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001784 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1785 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001786 er = EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001787 }
1788
1789 preempt_enable();
James Hogancc81e942016-06-09 10:50:45 +01001790done:
1791 /* Rollback PC only if emulation was unsuccessful */
1792 if (er == EMULATE_FAIL)
1793 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001794
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001795dont_update_pc:
James Hogancc81e942016-06-09 10:50:45 +01001796 /*
1797 * This is for exceptions whose emulation updates the PC, so do not
1798 * overwrite the PC under any circumstances
1799 */
1800
Sanjay Lale685c682012-11-21 18:34:04 -08001801 return er;
1802}
1803
James Hogan31cf7492016-06-09 14:19:09 +01001804enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001805 struct kvm_run *run,
1806 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001807{
James Hogan258f3a22016-06-15 19:29:47 +01001808 union mips_instruction inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001809 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001810
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001811 /* Fetch the instruction. */
1812 if (cause & CAUSEF_BD)
Sanjay Lale685c682012-11-21 18:34:04 -08001813 opc += 1;
Sanjay Lale685c682012-11-21 18:34:04 -08001814
James Hogan258f3a22016-06-15 19:29:47 +01001815 inst.word = kvm_get_inst(opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001816
James Hogan258f3a22016-06-15 19:29:47 +01001817 switch (inst.r_format.opcode) {
Sanjay Lale685c682012-11-21 18:34:04 -08001818 case cop0_op:
1819 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1820 break;
1821 case sb_op:
1822 case sh_op:
1823 case sw_op:
1824 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1825 break;
1826 case lb_op:
1827 case lbu_op:
1828 case lhu_op:
1829 case lh_op:
1830 case lw_op:
1831 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1832 break;
1833
James Hogan5cc4aaf2016-07-04 19:35:13 +01001834#ifndef CONFIG_CPU_MIPSR6
Sanjay Lale685c682012-11-21 18:34:04 -08001835 case cache_op:
1836 ++vcpu->stat.cache_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001837 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
Sanjay Lale685c682012-11-21 18:34:04 -08001838 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1839 break;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001840#else
1841 case spec3_op:
1842 switch (inst.spec3_format.func) {
1843 case cache6_op:
1844 ++vcpu->stat.cache_exits;
1845 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1846 er = kvm_mips_emulate_cache(inst, opc, cause, run,
1847 vcpu);
1848 break;
1849 default:
1850 goto unknown;
1851 };
1852 break;
1853unknown:
1854#endif
Sanjay Lale685c682012-11-21 18:34:04 -08001855
1856 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001857 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
James Hogan258f3a22016-06-15 19:29:47 +01001858 inst.word);
Sanjay Lale685c682012-11-21 18:34:04 -08001859 kvm_arch_vcpu_dump_regs(vcpu);
1860 er = EMULATE_FAIL;
1861 break;
1862 }
1863
1864 return er;
1865}
1866
James Hogan31cf7492016-06-09 14:19:09 +01001867enum emulation_result kvm_mips_emulate_syscall(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001868 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001869 struct kvm_run *run,
1870 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001871{
1872 struct mips_coproc *cop0 = vcpu->arch.cop0;
1873 struct kvm_vcpu_arch *arch = &vcpu->arch;
1874 enum emulation_result er = EMULATE_DONE;
1875
1876 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1877 /* save old pc */
1878 kvm_write_c0_guest_epc(cop0, arch->pc);
1879 kvm_set_c0_guest_status(cop0, ST0_EXL);
1880
1881 if (cause & CAUSEF_BD)
1882 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1883 else
1884 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1885
1886 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1887
1888 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001889 (EXCCODE_SYS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001890
1891 /* Set PC to the exception entry point */
1892 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1893
1894 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001895 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001896 er = EMULATE_FAIL;
1897 }
1898
1899 return er;
1900}
1901
James Hogan31cf7492016-06-09 14:19:09 +01001902enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001903 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001904 struct kvm_run *run,
1905 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001906{
1907 struct mips_coproc *cop0 = vcpu->arch.cop0;
1908 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001909 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001910 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001911
1912 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1913 /* save old pc */
1914 kvm_write_c0_guest_epc(cop0, arch->pc);
1915 kvm_set_c0_guest_status(cop0, ST0_EXL);
1916
1917 if (cause & CAUSEF_BD)
1918 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1919 else
1920 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1921
1922 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1923 arch->pc);
1924
1925 /* set pc to the exception entry point */
1926 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1927
1928 } else {
1929 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1930 arch->pc);
1931
1932 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1933 }
1934
1935 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001936 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001937
1938 /* setup badvaddr, context and entryhi registers for the guest */
1939 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1940 /* XXXKYMA: is the context register used by linux??? */
1941 kvm_write_c0_guest_entryhi(cop0, entryhi);
1942 /* Blow away the shadow host TLBs */
1943 kvm_mips_flush_host_tlb(1);
1944
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001945 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001946}
1947
James Hogan31cf7492016-06-09 14:19:09 +01001948enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001949 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001950 struct kvm_run *run,
1951 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001952{
1953 struct mips_coproc *cop0 = vcpu->arch.cop0;
1954 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001955 unsigned long entryhi =
1956 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001957 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001958
1959 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1960 /* save old pc */
1961 kvm_write_c0_guest_epc(cop0, arch->pc);
1962 kvm_set_c0_guest_status(cop0, ST0_EXL);
1963
1964 if (cause & CAUSEF_BD)
1965 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1966 else
1967 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1968
1969 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
1970 arch->pc);
1971
1972 /* set pc to the exception entry point */
1973 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1974
1975 } else {
1976 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1977 arch->pc);
1978 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1979 }
1980
1981 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001982 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001983
1984 /* setup badvaddr, context and entryhi registers for the guest */
1985 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1986 /* XXXKYMA: is the context register used by linux??? */
1987 kvm_write_c0_guest_entryhi(cop0, entryhi);
1988 /* Blow away the shadow host TLBs */
1989 kvm_mips_flush_host_tlb(1);
1990
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001991 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001992}
1993
James Hogan31cf7492016-06-09 14:19:09 +01001994enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001995 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001996 struct kvm_run *run,
1997 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001998{
1999 struct mips_coproc *cop0 = vcpu->arch.cop0;
2000 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002001 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002002 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002003
2004 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2005 /* save old pc */
2006 kvm_write_c0_guest_epc(cop0, arch->pc);
2007 kvm_set_c0_guest_status(cop0, ST0_EXL);
2008
2009 if (cause & CAUSEF_BD)
2010 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2011 else
2012 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2013
2014 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2015 arch->pc);
2016
2017 /* Set PC to the exception entry point */
2018 arch->pc = KVM_GUEST_KSEG0 + 0x0;
2019 } else {
2020 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2021 arch->pc);
2022 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2023 }
2024
2025 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002026 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002027
2028 /* setup badvaddr, context and entryhi registers for the guest */
2029 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2030 /* XXXKYMA: is the context register used by linux??? */
2031 kvm_write_c0_guest_entryhi(cop0, entryhi);
2032 /* Blow away the shadow host TLBs */
2033 kvm_mips_flush_host_tlb(1);
2034
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002035 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002036}
2037
James Hogan31cf7492016-06-09 14:19:09 +01002038enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002039 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002040 struct kvm_run *run,
2041 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002042{
2043 struct mips_coproc *cop0 = vcpu->arch.cop0;
2044 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002045 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002046 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002047
2048 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2049 /* save old pc */
2050 kvm_write_c0_guest_epc(cop0, arch->pc);
2051 kvm_set_c0_guest_status(cop0, ST0_EXL);
2052
2053 if (cause & CAUSEF_BD)
2054 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2055 else
2056 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2057
2058 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2059 arch->pc);
2060
2061 /* Set PC to the exception entry point */
2062 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2063 } else {
2064 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2065 arch->pc);
2066 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2067 }
2068
2069 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002070 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002071
2072 /* setup badvaddr, context and entryhi registers for the guest */
2073 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2074 /* XXXKYMA: is the context register used by linux??? */
2075 kvm_write_c0_guest_entryhi(cop0, entryhi);
2076 /* Blow away the shadow host TLBs */
2077 kvm_mips_flush_host_tlb(1);
2078
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002079 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002080}
2081
2082/* TLBMOD: store into address matching TLB with Dirty bit off */
James Hogan31cf7492016-06-09 14:19:09 +01002083enum emulation_result kvm_mips_handle_tlbmod(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002084 struct kvm_run *run,
2085 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002086{
2087 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002088#ifdef DEBUG
James Hogan3d654832014-05-29 10:16:41 +01002089 struct mips_coproc *cop0 = vcpu->arch.cop0;
2090 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002091 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
James Hogan3d654832014-05-29 10:16:41 +01002092 int index;
2093
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002094 /* If address not in the guest TLB, then we are in trouble */
Sanjay Lale685c682012-11-21 18:34:04 -08002095 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
2096 if (index < 0) {
2097 /* XXXKYMA Invalidate and retry */
2098 kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
2099 kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
2100 __func__, entryhi);
2101 kvm_mips_dump_guest_tlbs(vcpu);
2102 kvm_mips_dump_host_tlbs();
2103 return EMULATE_FAIL;
2104 }
2105#endif
2106
2107 er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
2108 return er;
2109}
2110
James Hogan31cf7492016-06-09 14:19:09 +01002111enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002112 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002113 struct kvm_run *run,
2114 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002115{
2116 struct mips_coproc *cop0 = vcpu->arch.cop0;
2117 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002118 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002119 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002120
2121 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2122 /* save old pc */
2123 kvm_write_c0_guest_epc(cop0, arch->pc);
2124 kvm_set_c0_guest_status(cop0, ST0_EXL);
2125
2126 if (cause & CAUSEF_BD)
2127 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2128 else
2129 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2130
2131 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2132 arch->pc);
2133
2134 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2135 } else {
2136 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2137 arch->pc);
2138 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2139 }
2140
James Hogan16d100db2015-12-16 23:49:33 +00002141 kvm_change_c0_guest_cause(cop0, (0xff),
2142 (EXCCODE_MOD << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002143
2144 /* setup badvaddr, context and entryhi registers for the guest */
2145 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2146 /* XXXKYMA: is the context register used by linux??? */
2147 kvm_write_c0_guest_entryhi(cop0, entryhi);
2148 /* Blow away the shadow host TLBs */
2149 kvm_mips_flush_host_tlb(1);
2150
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002151 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002152}
2153
James Hogan31cf7492016-06-09 14:19:09 +01002154enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002155 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002156 struct kvm_run *run,
2157 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002158{
2159 struct mips_coproc *cop0 = vcpu->arch.cop0;
2160 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002161
2162 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2163 /* save old pc */
2164 kvm_write_c0_guest_epc(cop0, arch->pc);
2165 kvm_set_c0_guest_status(cop0, ST0_EXL);
2166
2167 if (cause & CAUSEF_BD)
2168 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2169 else
2170 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2171
2172 }
2173
2174 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2175
2176 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002177 (EXCCODE_CPU << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002178 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2179
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002180 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002181}
2182
James Hogan31cf7492016-06-09 14:19:09 +01002183enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002184 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002185 struct kvm_run *run,
2186 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002187{
2188 struct mips_coproc *cop0 = vcpu->arch.cop0;
2189 struct kvm_vcpu_arch *arch = &vcpu->arch;
2190 enum emulation_result er = EMULATE_DONE;
2191
2192 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2193 /* save old pc */
2194 kvm_write_c0_guest_epc(cop0, arch->pc);
2195 kvm_set_c0_guest_status(cop0, ST0_EXL);
2196
2197 if (cause & CAUSEF_BD)
2198 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2199 else
2200 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2201
2202 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2203
2204 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002205 (EXCCODE_RI << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002206
2207 /* Set PC to the exception entry point */
2208 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2209
2210 } else {
2211 kvm_err("Trying to deliver RI when EXL is already set\n");
2212 er = EMULATE_FAIL;
2213 }
2214
2215 return er;
2216}
2217
James Hogan31cf7492016-06-09 14:19:09 +01002218enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002219 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002220 struct kvm_run *run,
2221 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002222{
2223 struct mips_coproc *cop0 = vcpu->arch.cop0;
2224 struct kvm_vcpu_arch *arch = &vcpu->arch;
2225 enum emulation_result er = EMULATE_DONE;
2226
2227 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2228 /* save old pc */
2229 kvm_write_c0_guest_epc(cop0, arch->pc);
2230 kvm_set_c0_guest_status(cop0, ST0_EXL);
2231
2232 if (cause & CAUSEF_BD)
2233 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2234 else
2235 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2236
2237 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2238
2239 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002240 (EXCCODE_BP << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002241
2242 /* Set PC to the exception entry point */
2243 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2244
2245 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002246 kvm_err("Trying to deliver BP when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002247 er = EMULATE_FAIL;
2248 }
2249
2250 return er;
2251}
2252
James Hogan31cf7492016-06-09 14:19:09 +01002253enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002254 u32 *opc,
James Hogan0a560422015-02-06 16:03:57 +00002255 struct kvm_run *run,
2256 struct kvm_vcpu *vcpu)
2257{
2258 struct mips_coproc *cop0 = vcpu->arch.cop0;
2259 struct kvm_vcpu_arch *arch = &vcpu->arch;
2260 enum emulation_result er = EMULATE_DONE;
2261
2262 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2263 /* save old pc */
2264 kvm_write_c0_guest_epc(cop0, arch->pc);
2265 kvm_set_c0_guest_status(cop0, ST0_EXL);
2266
2267 if (cause & CAUSEF_BD)
2268 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2269 else
2270 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2271
2272 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2273
2274 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002275 (EXCCODE_TR << CAUSEB_EXCCODE));
James Hogan0a560422015-02-06 16:03:57 +00002276
2277 /* Set PC to the exception entry point */
2278 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2279
2280 } else {
2281 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2282 er = EMULATE_FAIL;
2283 }
2284
2285 return er;
2286}
2287
James Hogan31cf7492016-06-09 14:19:09 +01002288enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002289 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002290 struct kvm_run *run,
2291 struct kvm_vcpu *vcpu)
2292{
2293 struct mips_coproc *cop0 = vcpu->arch.cop0;
2294 struct kvm_vcpu_arch *arch = &vcpu->arch;
2295 enum emulation_result er = EMULATE_DONE;
2296
2297 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2298 /* save old pc */
2299 kvm_write_c0_guest_epc(cop0, arch->pc);
2300 kvm_set_c0_guest_status(cop0, ST0_EXL);
2301
2302 if (cause & CAUSEF_BD)
2303 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2304 else
2305 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2306
2307 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2308
2309 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002310 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002311
2312 /* Set PC to the exception entry point */
2313 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2314
2315 } else {
2316 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2317 er = EMULATE_FAIL;
2318 }
2319
2320 return er;
2321}
2322
James Hogan31cf7492016-06-09 14:19:09 +01002323enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002324 u32 *opc,
James Hogan1c0cd662015-02-06 10:56:27 +00002325 struct kvm_run *run,
2326 struct kvm_vcpu *vcpu)
2327{
2328 struct mips_coproc *cop0 = vcpu->arch.cop0;
2329 struct kvm_vcpu_arch *arch = &vcpu->arch;
2330 enum emulation_result er = EMULATE_DONE;
2331
2332 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2333 /* save old pc */
2334 kvm_write_c0_guest_epc(cop0, arch->pc);
2335 kvm_set_c0_guest_status(cop0, ST0_EXL);
2336
2337 if (cause & CAUSEF_BD)
2338 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2339 else
2340 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2341
2342 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2343
2344 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002345 (EXCCODE_FPE << CAUSEB_EXCCODE));
James Hogan1c0cd662015-02-06 10:56:27 +00002346
2347 /* Set PC to the exception entry point */
2348 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2349
2350 } else {
2351 kvm_err("Trying to deliver FPE when EXL is already set\n");
2352 er = EMULATE_FAIL;
2353 }
2354
2355 return er;
2356}
2357
James Hogan31cf7492016-06-09 14:19:09 +01002358enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002359 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002360 struct kvm_run *run,
2361 struct kvm_vcpu *vcpu)
2362{
2363 struct mips_coproc *cop0 = vcpu->arch.cop0;
2364 struct kvm_vcpu_arch *arch = &vcpu->arch;
2365 enum emulation_result er = EMULATE_DONE;
2366
2367 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2368 /* save old pc */
2369 kvm_write_c0_guest_epc(cop0, arch->pc);
2370 kvm_set_c0_guest_status(cop0, ST0_EXL);
2371
2372 if (cause & CAUSEF_BD)
2373 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2374 else
2375 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2376
2377 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2378
2379 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002380 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002381
2382 /* Set PC to the exception entry point */
2383 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2384
2385 } else {
2386 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2387 er = EMULATE_FAIL;
2388 }
2389
2390 return er;
2391}
2392
James Hogan31cf7492016-06-09 14:19:09 +01002393enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002394 struct kvm_run *run,
2395 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002396{
2397 struct mips_coproc *cop0 = vcpu->arch.cop0;
2398 struct kvm_vcpu_arch *arch = &vcpu->arch;
2399 enum emulation_result er = EMULATE_DONE;
2400 unsigned long curr_pc;
James Hogan258f3a22016-06-15 19:29:47 +01002401 union mips_instruction inst;
Sanjay Lale685c682012-11-21 18:34:04 -08002402
2403 /*
2404 * Update PC and hold onto current PC in case there is
2405 * an error and we want to rollback the PC
2406 */
2407 curr_pc = vcpu->arch.pc;
2408 er = update_pc(vcpu, cause);
2409 if (er == EMULATE_FAIL)
2410 return er;
2411
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002412 /* Fetch the instruction. */
Sanjay Lale685c682012-11-21 18:34:04 -08002413 if (cause & CAUSEF_BD)
2414 opc += 1;
2415
James Hogan258f3a22016-06-15 19:29:47 +01002416 inst.word = kvm_get_inst(opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002417
James Hogan258f3a22016-06-15 19:29:47 +01002418 if (inst.word == KVM_INVALID_INST) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002419 kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
Sanjay Lale685c682012-11-21 18:34:04 -08002420 return EMULATE_FAIL;
2421 }
2422
James Hogan258f3a22016-06-15 19:29:47 +01002423 if (inst.r_format.opcode == spec3_op &&
James Hogan8eeab812016-07-04 19:35:14 +01002424 inst.r_format.func == rdhwr_op &&
2425 inst.r_format.rs == 0 &&
2426 (inst.r_format.re >> 3) == 0) {
James Hogan26f4f3b2014-03-14 13:06:09 +00002427 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
James Hogan258f3a22016-06-15 19:29:47 +01002428 int rd = inst.r_format.rd;
2429 int rt = inst.r_format.rt;
2430 int sel = inst.r_format.re & 0x7;
James Hogan6398da12016-06-14 09:40:15 +01002431
James Hogan26f4f3b2014-03-14 13:06:09 +00002432 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2433 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2434 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2435 rd, opc);
2436 goto emulate_ri;
2437 }
Sanjay Lale685c682012-11-21 18:34:04 -08002438 switch (rd) {
James Hoganaff565a2016-06-15 19:29:52 +01002439 case MIPS_HWR_CPUNUM: /* CPU number */
James Hogancf1fb0f2016-06-15 19:29:55 +01002440 arch->gprs[rt] = vcpu->vcpu_id;
Sanjay Lale685c682012-11-21 18:34:04 -08002441 break;
James Hoganaff565a2016-06-15 19:29:52 +01002442 case MIPS_HWR_SYNCISTEP: /* SYNCI length */
Sanjay Lale685c682012-11-21 18:34:04 -08002443 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2444 current_cpu_data.icache.linesz);
2445 break;
James Hoganaff565a2016-06-15 19:29:52 +01002446 case MIPS_HWR_CC: /* Read count register */
James Hogan172e02d2016-07-08 11:53:28 +01002447 arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002448 break;
James Hoganaff565a2016-06-15 19:29:52 +01002449 case MIPS_HWR_CCRES: /* Count register resolution */
Sanjay Lale685c682012-11-21 18:34:04 -08002450 switch (current_cpu_data.cputype) {
2451 case CPU_20KC:
2452 case CPU_25KF:
2453 arch->gprs[rt] = 1;
2454 break;
2455 default:
2456 arch->gprs[rt] = 2;
2457 }
2458 break;
James Hoganaff565a2016-06-15 19:29:52 +01002459 case MIPS_HWR_ULR: /* Read UserLocal register */
Sanjay Lale685c682012-11-21 18:34:04 -08002460 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08002461 break;
2462
2463 default:
James Hogan15505672014-03-14 13:06:07 +00002464 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
James Hogan26f4f3b2014-03-14 13:06:09 +00002465 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002466 }
James Hogan6398da12016-06-14 09:40:15 +01002467
2468 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
2469 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08002470 } else {
James Hogan258f3a22016-06-15 19:29:47 +01002471 kvm_debug("Emulate RI not supported @ %p: %#x\n",
2472 opc, inst.word);
James Hogan26f4f3b2014-03-14 13:06:09 +00002473 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002474 }
2475
James Hogan26f4f3b2014-03-14 13:06:09 +00002476 return EMULATE_DONE;
2477
2478emulate_ri:
Sanjay Lale685c682012-11-21 18:34:04 -08002479 /*
James Hogan26f4f3b2014-03-14 13:06:09 +00002480 * Rollback PC (if in branch delay slot then the PC already points to
2481 * branch target), and pass the RI exception to the guest OS.
Sanjay Lale685c682012-11-21 18:34:04 -08002482 */
James Hogan26f4f3b2014-03-14 13:06:09 +00002483 vcpu->arch.pc = curr_pc;
2484 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002485}
2486
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002487enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2488 struct kvm_run *run)
Sanjay Lale685c682012-11-21 18:34:04 -08002489{
2490 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2491 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002492
2493 if (run->mmio.len > sizeof(*gpr)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002494 kvm_err("Bad MMIO length: %d", run->mmio.len);
Sanjay Lale685c682012-11-21 18:34:04 -08002495 er = EMULATE_FAIL;
2496 goto done;
2497 }
2498
Sanjay Lale685c682012-11-21 18:34:04 -08002499 er = update_pc(vcpu, vcpu->arch.pending_load_cause);
2500 if (er == EMULATE_FAIL)
2501 return er;
2502
2503 switch (run->mmio.len) {
2504 case 4:
James Hogan8cffd192016-06-09 14:19:08 +01002505 *gpr = *(s32 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002506 break;
2507
2508 case 2:
2509 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002510 *gpr = *(s16 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002511 else
James Hogan8cffd192016-06-09 14:19:08 +01002512 *gpr = *(u16 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002513
2514 break;
2515 case 1:
2516 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002517 *gpr = *(s8 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002518 else
2519 *gpr = *(u8 *) run->mmio.data;
2520 break;
2521 }
2522
2523 if (vcpu->arch.pending_load_cause & CAUSEF_BD)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002524 kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
2525 vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
2526 vcpu->mmio_needed);
Sanjay Lale685c682012-11-21 18:34:04 -08002527
2528done:
2529 return er;
2530}
2531
James Hogan31cf7492016-06-09 14:19:09 +01002532static enum emulation_result kvm_mips_emulate_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002533 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002534 struct kvm_run *run,
2535 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002536{
James Hogan8cffd192016-06-09 14:19:08 +01002537 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002538 struct mips_coproc *cop0 = vcpu->arch.cop0;
2539 struct kvm_vcpu_arch *arch = &vcpu->arch;
2540 enum emulation_result er = EMULATE_DONE;
2541
2542 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2543 /* save old pc */
2544 kvm_write_c0_guest_epc(cop0, arch->pc);
2545 kvm_set_c0_guest_status(cop0, ST0_EXL);
2546
2547 if (cause & CAUSEF_BD)
2548 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2549 else
2550 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2551
2552 kvm_change_c0_guest_cause(cop0, (0xff),
2553 (exccode << CAUSEB_EXCCODE));
2554
2555 /* Set PC to the exception entry point */
2556 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2557 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2558
2559 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2560 exccode, kvm_read_c0_guest_epc(cop0),
2561 kvm_read_c0_guest_badvaddr(cop0));
2562 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002563 kvm_err("Trying to deliver EXC when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002564 er = EMULATE_FAIL;
2565 }
2566
2567 return er;
2568}
2569
James Hogan31cf7492016-06-09 14:19:09 +01002570enum emulation_result kvm_mips_check_privilege(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002571 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002572 struct kvm_run *run,
2573 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002574{
2575 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002576 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002577 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2578
2579 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2580
2581 if (usermode) {
2582 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00002583 case EXCCODE_INT:
2584 case EXCCODE_SYS:
2585 case EXCCODE_BP:
2586 case EXCCODE_RI:
2587 case EXCCODE_TR:
2588 case EXCCODE_MSAFPE:
2589 case EXCCODE_FPE:
2590 case EXCCODE_MSADIS:
Sanjay Lale685c682012-11-21 18:34:04 -08002591 break;
2592
James Hogan16d100db2015-12-16 23:49:33 +00002593 case EXCCODE_CPU:
Sanjay Lale685c682012-11-21 18:34:04 -08002594 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2595 er = EMULATE_PRIV_FAIL;
2596 break;
2597
James Hogan16d100db2015-12-16 23:49:33 +00002598 case EXCCODE_MOD:
Sanjay Lale685c682012-11-21 18:34:04 -08002599 break;
2600
James Hogan16d100db2015-12-16 23:49:33 +00002601 case EXCCODE_TLBL:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002602 /*
2603 * We we are accessing Guest kernel space, then send an
2604 * address error exception to the guest
2605 */
Sanjay Lale685c682012-11-21 18:34:04 -08002606 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002607 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2608 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002609 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002610 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002611 er = EMULATE_PRIV_FAIL;
2612 }
2613 break;
2614
James Hogan16d100db2015-12-16 23:49:33 +00002615 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002616 /*
2617 * We we are accessing Guest kernel space, then send an
2618 * address error exception to the guest
2619 */
Sanjay Lale685c682012-11-21 18:34:04 -08002620 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002621 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2622 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002623 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002624 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002625 er = EMULATE_PRIV_FAIL;
2626 }
2627 break;
2628
James Hogan16d100db2015-12-16 23:49:33 +00002629 case EXCCODE_ADES:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002630 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2631 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002632 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2633 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002634 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002635 }
2636 er = EMULATE_PRIV_FAIL;
2637 break;
James Hogan16d100db2015-12-16 23:49:33 +00002638 case EXCCODE_ADEL:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002639 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2640 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002641 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2642 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002643 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002644 }
2645 er = EMULATE_PRIV_FAIL;
2646 break;
2647 default:
2648 er = EMULATE_PRIV_FAIL;
2649 break;
2650 }
2651 }
2652
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002653 if (er == EMULATE_PRIV_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08002654 kvm_mips_emulate_exc(cause, opc, run, vcpu);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002655
Sanjay Lale685c682012-11-21 18:34:04 -08002656 return er;
2657}
2658
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002659/*
2660 * User Address (UA) fault, this could happen if
Sanjay Lale685c682012-11-21 18:34:04 -08002661 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2662 * case we pass on the fault to the guest kernel and let it handle it.
2663 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2664 * case we inject the TLB from the Guest TLB into the shadow host TLB
2665 */
James Hogan31cf7492016-06-09 14:19:09 +01002666enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002667 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002668 struct kvm_run *run,
2669 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002670{
2671 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002672 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002673 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2674 int index;
2675
James Hogane4e94c02016-06-09 14:19:05 +01002676 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2677 vcpu->arch.host_cp0_badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002678
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002679 /*
2680 * KVM would not have got the exception if this entry was valid in the
2681 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2682 * send the guest an exception. The guest exc handler should then inject
2683 * an entry into the guest TLB.
Sanjay Lale685c682012-11-21 18:34:04 -08002684 */
2685 index = kvm_mips_guest_tlb_lookup(vcpu,
James Hogancaa1faa2015-12-16 23:49:26 +00002686 (va & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002687 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2688 KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08002689 if (index < 0) {
James Hogan16d100db2015-12-16 23:49:33 +00002690 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002691 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002692 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002693 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2694 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002695 kvm_err("%s: invalid exc code: %d\n", __func__,
2696 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002697 er = EMULATE_FAIL;
2698 }
2699 } else {
2700 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2701
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002702 /*
2703 * Check if the entry is valid, if not then setup a TLB invalid
2704 * exception to the guest
2705 */
Sanjay Lale685c682012-11-21 18:34:04 -08002706 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan16d100db2015-12-16 23:49:33 +00002707 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002708 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2709 vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002710 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002711 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2712 vcpu);
2713 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002714 kvm_err("%s: invalid exc code: %d\n", __func__,
2715 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002716 er = EMULATE_FAIL;
2717 }
2718 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002719 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
James Hogan9fbfb062016-06-09 14:19:17 +01002720 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002721 /*
2722 * OK we have a Guest TLB entry, now inject it into the
2723 * shadow host TLB
2724 */
James Hogan9b731bc2016-08-11 11:58:15 +01002725 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) {
2726 kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
2727 __func__, va, index, vcpu,
2728 read_c0_entryhi());
2729 er = EMULATE_FAIL;
2730 }
Sanjay Lale685c682012-11-21 18:34:04 -08002731 }
2732 }
2733
2734 return er;
2735}