blob: 42424822898c45b10a18ed06a9d4f5ece4f624c3 [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>
Sanjay Lale685c682012-11-21 18:34:04 -080016#include <linux/vmalloc.h>
17#include <linux/fs.h>
18#include <linux/bootmem.h>
19#include <linux/random.h>
20#include <asm/page.h>
21#include <asm/cacheflush.h>
James Hoganf4956f62015-12-16 23:49:37 +000022#include <asm/cacheops.h>
Sanjay Lale685c682012-11-21 18:34:04 -080023#include <asm/cpu-info.h>
24#include <asm/mmu_context.h>
25#include <asm/tlbflush.h>
26#include <asm/inst.h>
27
28#undef CONFIG_MIPS_MT
29#include <asm/r4kcache.h>
30#define CONFIG_MIPS_MT
31
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070032#include "interrupt.h"
33#include "commpage.h"
Sanjay Lale685c682012-11-21 18:34:04 -080034
35#include "trace.h"
36
37/*
38 * Compute the return address and do emulate branch simulation, if required.
39 * This function should be called only in branch delay slot active.
40 */
James Hogan122e51d2016-11-28 17:23:14 +000041static int kvm_compute_return_epc(struct kvm_vcpu *vcpu, unsigned long instpc,
42 unsigned long *out)
Sanjay Lale685c682012-11-21 18:34:04 -080043{
44 unsigned int dspcontrol;
45 union mips_instruction insn;
46 struct kvm_vcpu_arch *arch = &vcpu->arch;
47 long epc = instpc;
James Hogan122e51d2016-11-28 17:23:14 +000048 long nextpc;
49 int err;
Sanjay Lale685c682012-11-21 18:34:04 -080050
James Hogan122e51d2016-11-28 17:23:14 +000051 if (epc & 3) {
52 kvm_err("%s: unaligned epc\n", __func__);
53 return -EINVAL;
54 }
Sanjay Lale685c682012-11-21 18:34:04 -080055
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070056 /* Read the instruction */
James Hogan6a97c772015-04-23 16:54:35 +010057 err = kvm_get_badinstrp((u32 *)epc, vcpu, &insn.word);
James Hogan122e51d2016-11-28 17:23:14 +000058 if (err)
59 return err;
Sanjay Lale685c682012-11-21 18:34:04 -080060
61 switch (insn.i_format.opcode) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070062 /* jr and jalr are in r_format format. */
Sanjay Lale685c682012-11-21 18:34:04 -080063 case spec_op:
64 switch (insn.r_format.func) {
65 case jalr_op:
66 arch->gprs[insn.r_format.rd] = epc + 8;
67 /* Fall through */
68 case jr_op:
69 nextpc = arch->gprs[insn.r_format.rs];
70 break;
James Hogan122e51d2016-11-28 17:23:14 +000071 default:
72 return -EINVAL;
Sanjay Lale685c682012-11-21 18:34:04 -080073 }
74 break;
75
76 /*
77 * This group contains:
78 * bltz_op, bgez_op, bltzl_op, bgezl_op,
79 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
80 */
81 case bcond_op:
82 switch (insn.i_format.rt) {
83 case bltz_op:
84 case bltzl_op:
85 if ((long)arch->gprs[insn.i_format.rs] < 0)
86 epc = epc + 4 + (insn.i_format.simmediate << 2);
87 else
88 epc += 8;
89 nextpc = epc;
90 break;
91
92 case bgez_op:
93 case bgezl_op:
94 if ((long)arch->gprs[insn.i_format.rs] >= 0)
95 epc = epc + 4 + (insn.i_format.simmediate << 2);
96 else
97 epc += 8;
98 nextpc = epc;
99 break;
100
101 case bltzal_op:
102 case bltzall_op:
103 arch->gprs[31] = epc + 8;
104 if ((long)arch->gprs[insn.i_format.rs] < 0)
105 epc = epc + 4 + (insn.i_format.simmediate << 2);
106 else
107 epc += 8;
108 nextpc = epc;
109 break;
110
111 case bgezal_op:
112 case bgezall_op:
113 arch->gprs[31] = epc + 8;
114 if ((long)arch->gprs[insn.i_format.rs] >= 0)
115 epc = epc + 4 + (insn.i_format.simmediate << 2);
116 else
117 epc += 8;
118 nextpc = epc;
119 break;
120 case bposge32_op:
James Hogan122e51d2016-11-28 17:23:14 +0000121 if (!cpu_has_dsp) {
122 kvm_err("%s: DSP branch but not DSP ASE\n",
123 __func__);
124 return -EINVAL;
125 }
Sanjay Lale685c682012-11-21 18:34:04 -0800126
127 dspcontrol = rddsp(0x01);
128
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700129 if (dspcontrol >= 32)
Sanjay Lale685c682012-11-21 18:34:04 -0800130 epc = epc + 4 + (insn.i_format.simmediate << 2);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700131 else
Sanjay Lale685c682012-11-21 18:34:04 -0800132 epc += 8;
133 nextpc = epc;
134 break;
James Hogan122e51d2016-11-28 17:23:14 +0000135 default:
136 return -EINVAL;
Sanjay Lale685c682012-11-21 18:34:04 -0800137 }
138 break;
139
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700140 /* These are unconditional and in j_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800141 case jal_op:
142 arch->gprs[31] = instpc + 8;
143 case j_op:
144 epc += 4;
145 epc >>= 28;
146 epc <<= 28;
147 epc |= (insn.j_format.target << 2);
148 nextpc = epc;
149 break;
150
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700151 /* These are conditional and in i_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800152 case beq_op:
153 case beql_op:
154 if (arch->gprs[insn.i_format.rs] ==
155 arch->gprs[insn.i_format.rt])
156 epc = epc + 4 + (insn.i_format.simmediate << 2);
157 else
158 epc += 8;
159 nextpc = epc;
160 break;
161
162 case bne_op:
163 case bnel_op:
164 if (arch->gprs[insn.i_format.rs] !=
165 arch->gprs[insn.i_format.rt])
166 epc = epc + 4 + (insn.i_format.simmediate << 2);
167 else
168 epc += 8;
169 nextpc = epc;
170 break;
171
James Hogan2e0badf2016-07-04 19:35:12 +0100172 case blez_op: /* POP06 */
173#ifndef CONFIG_CPU_MIPSR6
174 case blezl_op: /* removed in R6 */
175#endif
176 if (insn.i_format.rt != 0)
177 goto compact_branch;
Sanjay Lale685c682012-11-21 18:34:04 -0800178 if ((long)arch->gprs[insn.i_format.rs] <= 0)
179 epc = epc + 4 + (insn.i_format.simmediate << 2);
180 else
181 epc += 8;
182 nextpc = epc;
183 break;
184
James Hogan2e0badf2016-07-04 19:35:12 +0100185 case bgtz_op: /* POP07 */
186#ifndef CONFIG_CPU_MIPSR6
187 case bgtzl_op: /* removed in R6 */
188#endif
189 if (insn.i_format.rt != 0)
190 goto compact_branch;
Sanjay Lale685c682012-11-21 18:34:04 -0800191 if ((long)arch->gprs[insn.i_format.rs] > 0)
192 epc = epc + 4 + (insn.i_format.simmediate << 2);
193 else
194 epc += 8;
195 nextpc = epc;
196 break;
197
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700198 /* And now the FPA/cp1 branch instructions. */
Sanjay Lale685c682012-11-21 18:34:04 -0800199 case cop1_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700200 kvm_err("%s: unsupported cop1_op\n", __func__);
James Hogan122e51d2016-11-28 17:23:14 +0000201 return -EINVAL;
James Hogan2e0badf2016-07-04 19:35:12 +0100202
203#ifdef CONFIG_CPU_MIPSR6
204 /* R6 added the following compact branches with forbidden slots */
205 case blezl_op: /* POP26 */
206 case bgtzl_op: /* POP27 */
207 /* only rt == 0 isn't compact branch */
208 if (insn.i_format.rt != 0)
209 goto compact_branch;
James Hogan122e51d2016-11-28 17:23:14 +0000210 return -EINVAL;
James Hogan2e0badf2016-07-04 19:35:12 +0100211 case pop10_op:
212 case pop30_op:
213 /* only rs == rt == 0 is reserved, rest are compact branches */
214 if (insn.i_format.rs != 0 || insn.i_format.rt != 0)
215 goto compact_branch;
James Hogan122e51d2016-11-28 17:23:14 +0000216 return -EINVAL;
James Hogan2e0badf2016-07-04 19:35:12 +0100217 case pop66_op:
218 case pop76_op:
219 /* only rs == 0 isn't compact branch */
220 if (insn.i_format.rs != 0)
221 goto compact_branch;
James Hogan122e51d2016-11-28 17:23:14 +0000222 return -EINVAL;
James Hogan2e0badf2016-07-04 19:35:12 +0100223compact_branch:
224 /*
225 * If we've hit an exception on the forbidden slot, then
226 * the branch must not have been taken.
227 */
228 epc += 8;
229 nextpc = epc;
230 break;
231#else
232compact_branch:
James Hogan122e51d2016-11-28 17:23:14 +0000233 /* Fall through - Compact branches not supported before R6 */
James Hogan2e0badf2016-07-04 19:35:12 +0100234#endif
James Hogan122e51d2016-11-28 17:23:14 +0000235 default:
236 return -EINVAL;
Sanjay Lale685c682012-11-21 18:34:04 -0800237 }
238
James Hogan122e51d2016-11-28 17:23:14 +0000239 *out = nextpc;
240 return 0;
Sanjay Lale685c682012-11-21 18:34:04 -0800241}
242
James Hoganbdb7ed82016-06-09 14:19:07 +0100243enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
Sanjay Lale685c682012-11-21 18:34:04 -0800244{
James Hogan122e51d2016-11-28 17:23:14 +0000245 int err;
Sanjay Lale685c682012-11-21 18:34:04 -0800246
247 if (cause & CAUSEF_BD) {
James Hogan122e51d2016-11-28 17:23:14 +0000248 err = kvm_compute_return_epc(vcpu, vcpu->arch.pc,
249 &vcpu->arch.pc);
250 if (err)
251 return EMULATE_FAIL;
252 } else {
Sanjay Lale685c682012-11-21 18:34:04 -0800253 vcpu->arch.pc += 4;
James Hogan122e51d2016-11-28 17:23:14 +0000254 }
Sanjay Lale685c682012-11-21 18:34:04 -0800255
256 kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
257
James Hogan122e51d2016-11-28 17:23:14 +0000258 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800259}
260
James Hogane30492b2014-05-29 10:16:35 +0100261/**
James Hogan6a97c772015-04-23 16:54:35 +0100262 * kvm_get_badinstr() - Get bad instruction encoding.
263 * @opc: Guest pointer to faulting instruction.
264 * @vcpu: KVM VCPU information.
265 *
266 * Gets the instruction encoding of the faulting instruction, using the saved
267 * BadInstr register value if it exists, otherwise falling back to reading guest
268 * memory at @opc.
269 *
270 * Returns: The instruction encoding of the faulting instruction.
271 */
272int kvm_get_badinstr(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
273{
274 if (cpu_has_badinstr) {
275 *out = vcpu->arch.host_cp0_badinstr;
276 return 0;
277 } else {
278 return kvm_get_inst(opc, vcpu, out);
279 }
280}
281
282/**
283 * kvm_get_badinstrp() - Get bad prior instruction encoding.
284 * @opc: Guest pointer to prior faulting instruction.
285 * @vcpu: KVM VCPU information.
286 *
287 * Gets the instruction encoding of the prior faulting instruction (the branch
288 * containing the delay slot which faulted), using the saved BadInstrP register
289 * value if it exists, otherwise falling back to reading guest memory at @opc.
290 *
291 * Returns: The instruction encoding of the prior faulting instruction.
292 */
293int kvm_get_badinstrp(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
294{
295 if (cpu_has_badinstrp) {
296 *out = vcpu->arch.host_cp0_badinstrp;
297 return 0;
298 } else {
299 return kvm_get_inst(opc, vcpu, out);
300 }
301}
302
303/**
James Hogane30492b2014-05-29 10:16:35 +0100304 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
305 * @vcpu: Virtual CPU.
Sanjay Lale685c682012-11-21 18:34:04 -0800306 *
James Hoganf8239342014-05-29 10:16:37 +0100307 * Returns: 1 if the CP0_Count timer is disabled by either the guest
308 * CP0_Cause.DC bit or the count_ctl.DC bit.
James Hogane30492b2014-05-29 10:16:35 +0100309 * 0 otherwise (in which case CP0_Count timer is running).
Sanjay Lale685c682012-11-21 18:34:04 -0800310 */
James Hogane30492b2014-05-29 10:16:35 +0100311static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -0800312{
313 struct mips_coproc *cop0 = vcpu->arch.cop0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700314
James Hoganf8239342014-05-29 10:16:37 +0100315 return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
316 (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
James Hogane30492b2014-05-29 10:16:35 +0100317}
Sanjay Lale685c682012-11-21 18:34:04 -0800318
James Hogane30492b2014-05-29 10:16:35 +0100319/**
320 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
321 *
322 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
323 *
324 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
325 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100326static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100327{
328 s64 now_ns, periods;
329 u64 delta;
330
331 now_ns = ktime_to_ns(now);
332 delta = now_ns + vcpu->arch.count_dyn_bias;
333
334 if (delta >= vcpu->arch.count_period) {
335 /* If delta is out of safe range the bias needs adjusting */
336 periods = div64_s64(now_ns, vcpu->arch.count_period);
337 vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
338 /* Recalculate delta with new bias */
339 delta = now_ns + vcpu->arch.count_dyn_bias;
Sanjay Lale685c682012-11-21 18:34:04 -0800340 }
341
James Hogane30492b2014-05-29 10:16:35 +0100342 /*
343 * We've ensured that:
344 * delta < count_period
345 *
346 * Therefore the intermediate delta*count_hz will never overflow since
347 * at the boundary condition:
348 * delta = count_period
349 * delta = NSEC_PER_SEC * 2^32 / count_hz
350 * delta * count_hz = NSEC_PER_SEC * 2^32
351 */
352 return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
353}
354
355/**
James Hoganf8239342014-05-29 10:16:37 +0100356 * kvm_mips_count_time() - Get effective current time.
357 * @vcpu: Virtual CPU.
358 *
359 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
360 * except when the master disable bit is set in count_ctl, in which case it is
361 * count_resume, i.e. the time that the count was disabled.
362 *
363 * Returns: Effective monotonic ktime for CP0_Count.
364 */
365static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
366{
367 if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
368 return vcpu->arch.count_resume;
369
370 return ktime_get();
371}
372
373/**
James Hogane30492b2014-05-29 10:16:35 +0100374 * kvm_mips_read_count_running() - Read the current count value as if running.
375 * @vcpu: Virtual CPU.
376 * @now: Kernel time to read CP0_Count at.
377 *
378 * Returns the current guest CP0_Count register at time @now and handles if the
379 * timer interrupt is pending and hasn't been handled yet.
380 *
381 * Returns: The current value of the guest CP0_Count register.
382 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100383static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100384{
James Hogan4355c442016-04-22 10:38:45 +0100385 struct mips_coproc *cop0 = vcpu->arch.cop0;
386 ktime_t expires, threshold;
James Hogan8cffd192016-06-09 14:19:08 +0100387 u32 count, compare;
James Hogane30492b2014-05-29 10:16:35 +0100388 int running;
389
James Hogan4355c442016-04-22 10:38:45 +0100390 /* Calculate the biased and scaled guest CP0_Count */
391 count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
392 compare = kvm_read_c0_guest_compare(cop0);
393
394 /*
395 * Find whether CP0_Count has reached the closest timer interrupt. If
396 * not, we shouldn't inject it.
397 */
James Hogan8cffd192016-06-09 14:19:08 +0100398 if ((s32)(count - compare) < 0)
James Hogan4355c442016-04-22 10:38:45 +0100399 return count;
400
401 /*
402 * The CP0_Count we're going to return has already reached the closest
403 * timer interrupt. Quickly check if it really is a new interrupt by
404 * looking at whether the interval until the hrtimer expiry time is
405 * less than 1/4 of the timer period.
406 */
James Hogane30492b2014-05-29 10:16:35 +0100407 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
James Hogan4355c442016-04-22 10:38:45 +0100408 threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
409 if (ktime_before(expires, threshold)) {
James Hogane30492b2014-05-29 10:16:35 +0100410 /*
411 * Cancel it while we handle it so there's no chance of
412 * interference with the timeout handler.
413 */
414 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
415
416 /* Nothing should be waiting on the timeout */
417 kvm_mips_callbacks->queue_timer_int(vcpu);
418
419 /*
420 * Restart the timer if it was running based on the expiry time
421 * we read, so that we don't push it back 2 periods.
422 */
423 if (running) {
424 expires = ktime_add_ns(expires,
425 vcpu->arch.count_period);
426 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
427 HRTIMER_MODE_ABS);
428 }
429 }
430
James Hogan4355c442016-04-22 10:38:45 +0100431 return count;
James Hogane30492b2014-05-29 10:16:35 +0100432}
433
434/**
435 * kvm_mips_read_count() - Read the current count value.
436 * @vcpu: Virtual CPU.
437 *
438 * Read the current guest CP0_Count value, taking into account whether the timer
439 * is stopped.
440 *
441 * Returns: The current guest CP0_Count value.
442 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100443u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
James Hogane30492b2014-05-29 10:16:35 +0100444{
445 struct mips_coproc *cop0 = vcpu->arch.cop0;
446
447 /* If count disabled just read static copy of count */
448 if (kvm_mips_count_disabled(vcpu))
449 return kvm_read_c0_guest_count(cop0);
450
451 return kvm_mips_read_count_running(vcpu, ktime_get());
452}
453
454/**
455 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
456 * @vcpu: Virtual CPU.
457 * @count: Output pointer for CP0_Count value at point of freeze.
458 *
459 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
460 * at the point it was frozen. It is guaranteed that any pending interrupts at
461 * the point it was frozen are handled, and none after that point.
462 *
463 * This is useful where the time/CP0_Count is needed in the calculation of the
464 * new parameters.
465 *
466 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
467 *
468 * Returns: The ktime at the point of freeze.
469 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100470static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
James Hogane30492b2014-05-29 10:16:35 +0100471{
472 ktime_t now;
473
474 /* stop hrtimer before finding time */
475 hrtimer_cancel(&vcpu->arch.comparecount_timer);
476 now = ktime_get();
477
478 /* find count at this point and handle pending hrtimer */
479 *count = kvm_mips_read_count_running(vcpu, now);
480
481 return now;
482}
483
James Hogane30492b2014-05-29 10:16:35 +0100484/**
485 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
486 * @vcpu: Virtual CPU.
487 * @now: ktime at point of resume.
488 * @count: CP0_Count at point of resume.
489 *
490 * Resumes the timer and updates the timer expiry based on @now and @count.
491 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
492 * parameters need to be changed.
493 *
494 * It is guaranteed that a timer interrupt immediately after resume will be
495 * handled, but not if CP_Compare is exactly at @count. That case is already
496 * handled by kvm_mips_freeze_timer().
497 *
498 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
499 */
500static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
James Hoganbdb7ed82016-06-09 14:19:07 +0100501 ktime_t now, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100502{
503 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100504 u32 compare;
James Hogane30492b2014-05-29 10:16:35 +0100505 u64 delta;
506 ktime_t expire;
507
508 /* Calculate timeout (wrap 0 to 2^32) */
509 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100510 delta = (u64)(u32)(compare - count - 1) + 1;
James Hogane30492b2014-05-29 10:16:35 +0100511 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
512 expire = ktime_add_ns(now, delta);
513
514 /* Update hrtimer to use new timeout */
515 hrtimer_cancel(&vcpu->arch.comparecount_timer);
516 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
517}
518
519/**
James Hogane30492b2014-05-29 10:16:35 +0100520 * kvm_mips_write_count() - Modify the count and update timer.
521 * @vcpu: Virtual CPU.
522 * @count: Guest CP0_Count value to set.
523 *
524 * Sets the CP0_Count value and updates the timer accordingly.
525 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100526void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100527{
528 struct mips_coproc *cop0 = vcpu->arch.cop0;
529 ktime_t now;
530
531 /* Calculate bias */
James Hoganf8239342014-05-29 10:16:37 +0100532 now = kvm_mips_count_time(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100533 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
534
535 if (kvm_mips_count_disabled(vcpu))
536 /* The timer's disabled, adjust the static count */
537 kvm_write_c0_guest_count(cop0, count);
538 else
539 /* Update timeout */
540 kvm_mips_resume_hrtimer(vcpu, now, count);
541}
542
543/**
544 * kvm_mips_init_count() - Initialise timer.
545 * @vcpu: Virtual CPU.
James Hogana517c1a2017-03-14 10:15:21 +0000546 * @count_hz: Frequency of timer.
James Hogane30492b2014-05-29 10:16:35 +0100547 *
James Hogana517c1a2017-03-14 10:15:21 +0000548 * Initialise the timer to the specified frequency, zero it, and set it going if
549 * it's enabled.
James Hogane30492b2014-05-29 10:16:35 +0100550 */
James Hogana517c1a2017-03-14 10:15:21 +0000551void kvm_mips_init_count(struct kvm_vcpu *vcpu, unsigned long count_hz)
James Hogane30492b2014-05-29 10:16:35 +0100552{
James Hogana517c1a2017-03-14 10:15:21 +0000553 vcpu->arch.count_hz = count_hz;
554 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
James Hogane30492b2014-05-29 10:16:35 +0100555 vcpu->arch.count_dyn_bias = 0;
556
557 /* Starting at 0 */
558 kvm_mips_write_count(vcpu, 0);
559}
560
561/**
James Hoganf74a8e22014-05-29 10:16:38 +0100562 * kvm_mips_set_count_hz() - Update the frequency of the timer.
563 * @vcpu: Virtual CPU.
564 * @count_hz: Frequency of CP0_Count timer in Hz.
565 *
566 * Change the frequency of the CP0_Count timer. This is done atomically so that
567 * CP0_Count is continuous and no timer interrupt is lost.
568 *
569 * Returns: -EINVAL if @count_hz is out of range.
570 * 0 on success.
571 */
572int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
573{
574 struct mips_coproc *cop0 = vcpu->arch.cop0;
575 int dc;
576 ktime_t now;
577 u32 count;
578
579 /* ensure the frequency is in a sensible range... */
580 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
581 return -EINVAL;
582 /* ... and has actually changed */
583 if (vcpu->arch.count_hz == count_hz)
584 return 0;
585
586 /* Safely freeze timer so we can keep it continuous */
587 dc = kvm_mips_count_disabled(vcpu);
588 if (dc) {
589 now = kvm_mips_count_time(vcpu);
590 count = kvm_read_c0_guest_count(cop0);
591 } else {
592 now = kvm_mips_freeze_hrtimer(vcpu, &count);
593 }
594
595 /* Update the frequency */
596 vcpu->arch.count_hz = count_hz;
597 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
598 vcpu->arch.count_dyn_bias = 0;
599
600 /* Calculate adjusted bias so dynamic count is unchanged */
601 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
602
603 /* Update and resume hrtimer */
604 if (!dc)
605 kvm_mips_resume_hrtimer(vcpu, now, count);
606 return 0;
607}
608
609/**
James Hogane30492b2014-05-29 10:16:35 +0100610 * kvm_mips_write_compare() - Modify compare and update timer.
611 * @vcpu: Virtual CPU.
612 * @compare: New CP0_Compare value.
James Hoganb45bacd2016-04-22 10:38:46 +0100613 * @ack: Whether to acknowledge timer interrupt.
James Hogane30492b2014-05-29 10:16:35 +0100614 *
615 * Update CP0_Compare to a new value and update the timeout.
James Hoganb45bacd2016-04-22 10:38:46 +0100616 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
617 * any pending timer interrupt is preserved.
James Hogane30492b2014-05-29 10:16:35 +0100618 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100619void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
James Hogane30492b2014-05-29 10:16:35 +0100620{
621 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganb45bacd2016-04-22 10:38:46 +0100622 int dc;
623 u32 old_compare = kvm_read_c0_guest_compare(cop0);
James Hogan5dee99b2017-03-14 10:15:28 +0000624 s32 delta = compare - old_compare;
625 u32 cause;
626 ktime_t now = ktime_set(0, 0); /* silence bogus GCC warning */
James Hogan8cffd192016-06-09 14:19:08 +0100627 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100628
629 /* if unchanged, must just be an ack */
James Hoganb45bacd2016-04-22 10:38:46 +0100630 if (old_compare == compare) {
631 if (!ack)
632 return;
633 kvm_mips_callbacks->dequeue_timer_int(vcpu);
634 kvm_write_c0_guest_compare(cop0, compare);
James Hogane30492b2014-05-29 10:16:35 +0100635 return;
James Hoganb45bacd2016-04-22 10:38:46 +0100636 }
James Hogane30492b2014-05-29 10:16:35 +0100637
James Hogan5dee99b2017-03-14 10:15:28 +0000638 /*
639 * If guest CP0_Compare moves forward, CP0_GTOffset should be adjusted
640 * too to prevent guest CP0_Count hitting guest CP0_Compare.
641 *
642 * The new GTOffset corresponds to the new value of CP0_Compare, and is
643 * set prior to it being written into the guest context. We disable
644 * preemption until the new value is written to prevent restore of a
645 * GTOffset corresponding to the old CP0_Compare value.
646 */
647 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta > 0) {
648 preempt_disable();
649 write_c0_gtoffset(compare - read_c0_count());
650 back_to_back_c0_hazard();
651 }
652
James Hoganb45bacd2016-04-22 10:38:46 +0100653 /* freeze_hrtimer() takes care of timer interrupts <= count */
654 dc = kvm_mips_count_disabled(vcpu);
655 if (!dc)
656 now = kvm_mips_freeze_hrtimer(vcpu, &count);
657
658 if (ack)
659 kvm_mips_callbacks->dequeue_timer_int(vcpu);
James Hogan5dee99b2017-03-14 10:15:28 +0000660 else if (IS_ENABLED(CONFIG_KVM_MIPS_VZ))
661 /*
662 * With VZ, writing CP0_Compare acks (clears) CP0_Cause.TI, so
663 * preserve guest CP0_Cause.TI if we don't want to ack it.
664 */
665 cause = kvm_read_c0_guest_cause(cop0);
James Hoganb45bacd2016-04-22 10:38:46 +0100666
James Hogane30492b2014-05-29 10:16:35 +0100667 kvm_write_c0_guest_compare(cop0, compare);
668
James Hogan5dee99b2017-03-14 10:15:28 +0000669 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
670 if (delta > 0)
671 preempt_enable();
672
673 back_to_back_c0_hazard();
674
675 if (!ack && cause & CAUSEF_TI)
676 kvm_write_c0_guest_cause(cop0, cause);
677 }
678
James Hoganb45bacd2016-04-22 10:38:46 +0100679 /* resume_hrtimer() takes care of timer interrupts > count */
680 if (!dc)
681 kvm_mips_resume_hrtimer(vcpu, now, count);
James Hogan5dee99b2017-03-14 10:15:28 +0000682
683 /*
684 * If guest CP0_Compare is moving backward, we delay CP0_GTOffset change
685 * until after the new CP0_Compare is written, otherwise new guest
686 * CP0_Count could hit new guest CP0_Compare.
687 */
688 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta <= 0)
689 write_c0_gtoffset(compare - read_c0_count());
James Hogane30492b2014-05-29 10:16:35 +0100690}
691
692/**
693 * kvm_mips_count_disable() - Disable count.
694 * @vcpu: Virtual CPU.
695 *
696 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
697 * time will be handled but not after.
698 *
James Hoganf8239342014-05-29 10:16:37 +0100699 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
700 * count_ctl.DC has been set (count disabled).
James Hogane30492b2014-05-29 10:16:35 +0100701 *
702 * Returns: The time that the timer was stopped.
703 */
704static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
705{
706 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100707 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100708 ktime_t now;
709
710 /* Stop hrtimer */
711 hrtimer_cancel(&vcpu->arch.comparecount_timer);
712
713 /* Set the static count from the dynamic count, handling pending TI */
714 now = ktime_get();
715 count = kvm_mips_read_count_running(vcpu, now);
716 kvm_write_c0_guest_count(cop0, count);
717
718 return now;
719}
720
721/**
722 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
723 * @vcpu: Virtual CPU.
724 *
725 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
James Hoganf8239342014-05-29 10:16:37 +0100726 * before the final stop time will be handled if the timer isn't disabled by
727 * count_ctl.DC, but not after.
James Hogane30492b2014-05-29 10:16:35 +0100728 *
729 * Assumes CP0_Cause.DC is clear (count enabled).
730 */
731void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
732{
733 struct mips_coproc *cop0 = vcpu->arch.cop0;
734
735 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
James Hoganf8239342014-05-29 10:16:37 +0100736 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
737 kvm_mips_count_disable(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100738}
739
740/**
741 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
742 * @vcpu: Virtual CPU.
743 *
744 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
James Hoganf8239342014-05-29 10:16:37 +0100745 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
746 * potentially before even returning, so the caller should be careful with
747 * ordering of CP0_Cause modifications so as not to lose it.
James Hogane30492b2014-05-29 10:16:35 +0100748 *
749 * Assumes CP0_Cause.DC is set (count disabled).
750 */
751void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
752{
753 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100754 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100755
756 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
757
758 /*
759 * Set the dynamic count to match the static count.
James Hoganf8239342014-05-29 10:16:37 +0100760 * This starts the hrtimer if count_ctl.DC allows it.
761 * Otherwise it conveniently updates the biases.
James Hogane30492b2014-05-29 10:16:35 +0100762 */
763 count = kvm_read_c0_guest_count(cop0);
764 kvm_mips_write_count(vcpu, count);
765}
766
767/**
James Hoganf8239342014-05-29 10:16:37 +0100768 * kvm_mips_set_count_ctl() - Update the count control KVM register.
769 * @vcpu: Virtual CPU.
770 * @count_ctl: Count control register new value.
771 *
772 * Set the count control KVM register. The timer is updated accordingly.
773 *
774 * Returns: -EINVAL if reserved bits are set.
775 * 0 on success.
776 */
777int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
778{
779 struct mips_coproc *cop0 = vcpu->arch.cop0;
780 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
781 s64 delta;
782 ktime_t expire, now;
James Hogan8cffd192016-06-09 14:19:08 +0100783 u32 count, compare;
James Hoganf8239342014-05-29 10:16:37 +0100784
785 /* Only allow defined bits to be changed */
786 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
787 return -EINVAL;
788
789 /* Apply new value */
790 vcpu->arch.count_ctl = count_ctl;
791
792 /* Master CP0_Count disable */
793 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
794 /* Is CP0_Cause.DC already disabling CP0_Count? */
795 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
796 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
797 /* Just record the current time */
798 vcpu->arch.count_resume = ktime_get();
799 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
800 /* disable timer and record current time */
801 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
802 } else {
803 /*
804 * Calculate timeout relative to static count at resume
805 * time (wrap 0 to 2^32).
806 */
807 count = kvm_read_c0_guest_count(cop0);
808 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100809 delta = (u64)(u32)(compare - count - 1) + 1;
James Hoganf8239342014-05-29 10:16:37 +0100810 delta = div_u64(delta * NSEC_PER_SEC,
811 vcpu->arch.count_hz);
812 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
813
814 /* Handle pending interrupt */
815 now = ktime_get();
816 if (ktime_compare(now, expire) >= 0)
817 /* Nothing should be waiting on the timeout */
818 kvm_mips_callbacks->queue_timer_int(vcpu);
819
820 /* Resume hrtimer without changing bias */
821 count = kvm_mips_read_count_running(vcpu, now);
822 kvm_mips_resume_hrtimer(vcpu, now, count);
823 }
824 }
825
826 return 0;
827}
828
829/**
830 * kvm_mips_set_count_resume() - Update the count resume KVM register.
831 * @vcpu: Virtual CPU.
832 * @count_resume: Count resume register new value.
833 *
834 * Set the count resume KVM register.
835 *
836 * Returns: -EINVAL if out of valid range (0..now).
837 * 0 on success.
838 */
839int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
840{
841 /*
842 * It doesn't make sense for the resume time to be in the future, as it
843 * would be possible for the next interrupt to be more than a full
844 * period in the future.
845 */
846 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
847 return -EINVAL;
848
849 vcpu->arch.count_resume = ns_to_ktime(count_resume);
850 return 0;
851}
852
853/**
James Hogane30492b2014-05-29 10:16:35 +0100854 * kvm_mips_count_timeout() - Push timer forward on timeout.
855 * @vcpu: Virtual CPU.
856 *
857 * Handle an hrtimer event by push the hrtimer forward a period.
858 *
859 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
860 */
861enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
862{
863 /* Add the Count period to the current expiry time */
864 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
865 vcpu->arch.count_period);
866 return HRTIMER_RESTART;
Sanjay Lale685c682012-11-21 18:34:04 -0800867}
868
869enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
870{
871 struct mips_coproc *cop0 = vcpu->arch.cop0;
872 enum emulation_result er = EMULATE_DONE;
873
James Hoganede5f3e2016-10-25 16:11:11 +0100874 if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
875 kvm_clear_c0_guest_status(cop0, ST0_ERL);
876 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
877 } else if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
Sanjay Lale685c682012-11-21 18:34:04 -0800878 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
879 kvm_read_c0_guest_epc(cop0));
880 kvm_clear_c0_guest_status(cop0, ST0_EXL);
881 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
882
Sanjay Lale685c682012-11-21 18:34:04 -0800883 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700884 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
885 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800886 er = EMULATE_FAIL;
887 }
888
889 return er;
890}
891
892enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
893{
Sanjay Lale685c682012-11-21 18:34:04 -0800894 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
895 vcpu->arch.pending_exceptions);
896
897 ++vcpu->stat.wait_exits;
James Hogan1e09e862016-06-14 09:40:12 +0100898 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
Sanjay Lale685c682012-11-21 18:34:04 -0800899 if (!vcpu->arch.pending_exceptions) {
900 vcpu->arch.wait = 1;
901 kvm_vcpu_block(vcpu);
902
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700903 /*
904 * We we are runnable, then definitely go off to user space to
905 * check if any I/O interrupts are pending.
Sanjay Lale685c682012-11-21 18:34:04 -0800906 */
907 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
908 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
909 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
910 }
911 }
912
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700913 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800914}
915
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700916/*
917 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
918 * we can catch this, if things ever change
Sanjay Lale685c682012-11-21 18:34:04 -0800919 */
920enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
921{
922 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100923 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800924
James Hogana27660f2017-03-14 10:15:25 +0000925 kvm_err("[%#lx] COP0_TLBR [%d]\n", pc, kvm_read_c0_guest_index(cop0));
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700926 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -0800927}
928
James Hogan91e4f1b2016-09-15 17:20:06 +0100929/**
930 * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map.
931 * @vcpu: VCPU with changed mappings.
932 * @tlb: TLB entry being removed.
933 *
934 * This is called to indicate a single change in guest MMU mappings, so that we
935 * can arrange TLB flushes on this and other CPUs.
936 */
937static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu,
938 struct kvm_mips_tlb *tlb)
939{
James Hoganc550d532016-10-11 23:14:39 +0100940 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
941 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
James Hogan91e4f1b2016-09-15 17:20:06 +0100942 int cpu, i;
943 bool user;
944
945 /* No need to flush for entries which are already invalid */
946 if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V))
947 return;
James Hoganaba85922016-12-16 15:57:00 +0000948 /* Don't touch host kernel page tables or TLB mappings */
949 if ((unsigned long)tlb->tlb_hi > 0x7fffffff)
950 return;
James Hogan91e4f1b2016-09-15 17:20:06 +0100951 /* User address space doesn't need flushing for KSeg2/3 changes */
952 user = tlb->tlb_hi < KVM_GUEST_KSEG0;
953
954 preempt_disable();
955
James Hoganaba85922016-12-16 15:57:00 +0000956 /* Invalidate page table entries */
957 kvm_trap_emul_invalidate_gva(vcpu, tlb->tlb_hi & VPN2_MASK, user);
958
James Hogan91e4f1b2016-09-15 17:20:06 +0100959 /*
960 * Probe the shadow host TLB for the entry being overwritten, if one
961 * matches, invalidate it
962 */
James Hogan57e38692016-10-08 00:15:52 +0100963 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi, user, true);
James Hogan91e4f1b2016-09-15 17:20:06 +0100964
965 /* Invalidate the whole ASID on other CPUs */
966 cpu = smp_processor_id();
967 for_each_possible_cpu(i) {
968 if (i == cpu)
969 continue;
970 if (user)
James Hoganc550d532016-10-11 23:14:39 +0100971 cpu_context(i, user_mm) = 0;
972 cpu_context(i, kern_mm) = 0;
James Hogan91e4f1b2016-09-15 17:20:06 +0100973 }
974
975 preempt_enable();
976}
977
Sanjay Lale685c682012-11-21 18:34:04 -0800978/* Write Guest TLB Entry @ Index */
979enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
980{
981 struct mips_coproc *cop0 = vcpu->arch.cop0;
982 int index = kvm_read_c0_guest_index(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800983 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +0100984 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800985
986 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700987 kvm_debug("%s: illegal index: %d\n", __func__, index);
James Hogan8cffd192016-06-09 14:19:08 +0100988 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700989 pc, index, kvm_read_c0_guest_entryhi(cop0),
990 kvm_read_c0_guest_entrylo0(cop0),
991 kvm_read_c0_guest_entrylo1(cop0),
992 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800993 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
994 }
995
996 tlb = &vcpu->arch.guest_tlb[index];
James Hogan91e4f1b2016-09-15 17:20:06 +0100997
998 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -0800999
1000 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
1001 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +01001002 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
1003 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001004
James Hogan8cffd192016-06-09 14:19:08 +01001005 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001006 pc, index, kvm_read_c0_guest_entryhi(cop0),
1007 kvm_read_c0_guest_entrylo0(cop0),
1008 kvm_read_c0_guest_entrylo1(cop0),
1009 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001010
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001011 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001012}
1013
1014/* Write Guest TLB Entry @ Random Index */
1015enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
1016{
1017 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -08001018 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +01001019 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001020 int index;
1021
Sanjay Lale685c682012-11-21 18:34:04 -08001022 get_random_bytes(&index, sizeof(index));
1023 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
Sanjay Lale685c682012-11-21 18:34:04 -08001024
Sanjay Lale685c682012-11-21 18:34:04 -08001025 tlb = &vcpu->arch.guest_tlb[index];
1026
James Hogan91e4f1b2016-09-15 17:20:06 +01001027 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -08001028
1029 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
1030 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +01001031 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
1032 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001033
James Hogan8cffd192016-06-09 14:19:08 +01001034 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001035 pc, index, kvm_read_c0_guest_entryhi(cop0),
1036 kvm_read_c0_guest_entrylo0(cop0),
1037 kvm_read_c0_guest_entrylo1(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001038
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001039 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001040}
1041
1042enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
1043{
1044 struct mips_coproc *cop0 = vcpu->arch.cop0;
1045 long entryhi = kvm_read_c0_guest_entryhi(cop0);
James Hogan8cffd192016-06-09 14:19:08 +01001046 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001047 int index = -1;
1048
1049 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
1050
1051 kvm_write_c0_guest_index(cop0, index);
1052
James Hogan8cffd192016-06-09 14:19:08 +01001053 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
Sanjay Lale685c682012-11-21 18:34:04 -08001054 index);
1055
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001056 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001057}
1058
James Hoganc7716072014-06-26 15:11:29 +01001059/**
1060 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
1061 * @vcpu: Virtual CPU.
1062 *
1063 * Finds the mask of bits which are writable in the guest's Config1 CP0
1064 * register, by userland (currently read-only to the guest).
1065 */
1066unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
1067{
James Hogan6cdc65e2015-02-03 13:59:38 +00001068 unsigned int mask = 0;
1069
1070 /* Permit FPU to be present if FPU is supported */
1071 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
1072 mask |= MIPS_CONF1_FP;
1073
1074 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001075}
1076
1077/**
1078 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
1079 * @vcpu: Virtual CPU.
1080 *
1081 * Finds the mask of bits which are writable in the guest's Config3 CP0
1082 * register, by userland (currently read-only to the guest).
1083 */
1084unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
1085{
James Hogancef061d02016-06-15 19:29:54 +01001086 /* Config4 and ULRI are optional */
1087 unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
James Hogan2b6009d2015-02-06 23:01:00 +00001088
1089 /* Permit MSA to be present if MSA is supported */
1090 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
1091 mask |= MIPS_CONF3_MSA;
1092
1093 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001094}
1095
1096/**
1097 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
1098 * @vcpu: Virtual CPU.
1099 *
1100 * Finds the mask of bits which are writable in the guest's Config4 CP0
1101 * register, by userland (currently read-only to the guest).
1102 */
1103unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
1104{
1105 /* Config5 is optional */
James Hogan05108702016-06-15 19:29:56 +01001106 unsigned int mask = MIPS_CONF_M;
1107
1108 /* KScrExist */
James Hogan654229a2016-12-08 22:46:41 +00001109 mask |= 0xfc << MIPS_CONF4_KSCREXIST_SHIFT;
James Hogan05108702016-06-15 19:29:56 +01001110
1111 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001112}
1113
1114/**
1115 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
1116 * @vcpu: Virtual CPU.
1117 *
1118 * Finds the mask of bits which are writable in the guest's Config5 CP0
1119 * register, by the guest itself.
1120 */
1121unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
1122{
James Hogan6cdc65e2015-02-03 13:59:38 +00001123 unsigned int mask = 0;
1124
James Hogan2b6009d2015-02-06 23:01:00 +00001125 /* Permit MSAEn changes if MSA supported and enabled */
1126 if (kvm_mips_guest_has_msa(&vcpu->arch))
1127 mask |= MIPS_CONF5_MSAEN;
1128
James Hogan6cdc65e2015-02-03 13:59:38 +00001129 /*
1130 * Permit guest FPU mode changes if FPU is enabled and the relevant
1131 * feature exists according to FIR register.
1132 */
1133 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1134 if (cpu_has_fre)
1135 mask |= MIPS_CONF5_FRE;
1136 /* We don't support UFR or UFE */
1137 }
1138
1139 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001140}
1141
James Hogan258f3a22016-06-15 19:29:47 +01001142enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
1143 u32 *opc, u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001144 struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001145 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001146{
1147 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganc550d532016-10-11 23:14:39 +01001148 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
Sanjay Lale685c682012-11-21 18:34:04 -08001149 enum emulation_result er = EMULATE_DONE;
James Hogan258f3a22016-06-15 19:29:47 +01001150 u32 rt, rd, sel;
Sanjay Lale685c682012-11-21 18:34:04 -08001151 unsigned long curr_pc;
James Hogan91e4f1b2016-09-15 17:20:06 +01001152 int cpu, i;
Sanjay Lale685c682012-11-21 18:34:04 -08001153
1154 /*
1155 * Update PC and hold onto current PC in case there is
1156 * an error and we want to rollback the PC
1157 */
1158 curr_pc = vcpu->arch.pc;
1159 er = update_pc(vcpu, cause);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001160 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001161 return er;
Sanjay Lale685c682012-11-21 18:34:04 -08001162
James Hogan258f3a22016-06-15 19:29:47 +01001163 if (inst.co_format.co) {
1164 switch (inst.co_format.func) {
Sanjay Lale685c682012-11-21 18:34:04 -08001165 case tlbr_op: /* Read indexed TLB entry */
1166 er = kvm_mips_emul_tlbr(vcpu);
1167 break;
1168 case tlbwi_op: /* Write indexed */
1169 er = kvm_mips_emul_tlbwi(vcpu);
1170 break;
1171 case tlbwr_op: /* Write random */
1172 er = kvm_mips_emul_tlbwr(vcpu);
1173 break;
1174 case tlbp_op: /* TLB Probe */
1175 er = kvm_mips_emul_tlbp(vcpu);
1176 break;
1177 case rfe_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001178 kvm_err("!!!COP0_RFE!!!\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001179 break;
1180 case eret_op:
1181 er = kvm_mips_emul_eret(vcpu);
1182 goto dont_update_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001183 case wait_op:
1184 er = kvm_mips_emul_wait(vcpu);
1185 break;
James Hogan955d8dc2017-03-14 10:15:14 +00001186 case hypcall_op:
1187 er = kvm_mips_emul_hypcall(vcpu, inst);
1188 break;
Sanjay Lale685c682012-11-21 18:34:04 -08001189 }
1190 } else {
James Hogan258f3a22016-06-15 19:29:47 +01001191 rt = inst.c0r_format.rt;
1192 rd = inst.c0r_format.rd;
1193 sel = inst.c0r_format.sel;
1194
1195 switch (inst.c0r_format.rs) {
Sanjay Lale685c682012-11-21 18:34:04 -08001196 case mfc_op:
1197#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1198 cop0->stat[rd][sel]++;
1199#endif
1200 /* Get reg */
1201 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogan172e02d2016-07-08 11:53:28 +01001202 vcpu->arch.gprs[rt] =
1203 (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001204 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1205 vcpu->arch.gprs[rt] = 0x0;
1206#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1207 kvm_mips_trans_mfc0(inst, opc, vcpu);
1208#endif
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001209 } else {
James Hogan172e02d2016-07-08 11:53:28 +01001210 vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
Sanjay Lale685c682012-11-21 18:34:04 -08001211
1212#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1213 kvm_mips_trans_mfc0(inst, opc, vcpu);
1214#endif
1215 }
1216
James Hogan6398da12016-06-14 09:40:15 +01001217 trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
1218 KVM_TRACE_COP0(rd, sel),
1219 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001220 break;
1221
1222 case dmfc_op:
1223 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
James Hogan6398da12016-06-14 09:40:15 +01001224
1225 trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
1226 KVM_TRACE_COP0(rd, sel),
1227 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001228 break;
1229
1230 case mtc_op:
1231#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1232 cop0->stat[rd][sel]++;
1233#endif
James Hogan6398da12016-06-14 09:40:15 +01001234 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
1235 KVM_TRACE_COP0(rd, sel),
1236 vcpu->arch.gprs[rt]);
1237
Sanjay Lale685c682012-11-21 18:34:04 -08001238 if ((rd == MIPS_CP0_TLB_INDEX)
1239 && (vcpu->arch.gprs[rt] >=
1240 KVM_MIPS_GUEST_TLB_SIZE)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001241 kvm_err("Invalid TLB Index: %ld",
1242 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001243 er = EMULATE_FAIL;
1244 break;
1245 }
Sanjay Lale685c682012-11-21 18:34:04 -08001246 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
James Hogan7801bbe2016-11-14 23:59:27 +00001247 /*
1248 * Preserve core number, and keep the exception
1249 * base in guest KSeg0.
1250 */
1251 kvm_change_c0_guest_ebase(cop0, 0x1ffff000,
Sanjay Lale685c682012-11-21 18:34:04 -08001252 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001253 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
James Hogan8cffd192016-06-09 14:19:08 +01001254 u32 nasid =
Paul Burtonca64c2b2016-05-06 14:36:20 +01001255 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
James Hoganbf18db42016-09-16 13:14:09 +01001256 if (((kvm_read_c0_guest_entryhi(cop0) &
Paul Burtonca64c2b2016-05-06 14:36:20 +01001257 KVM_ENTRYHI_ASID) != nasid)) {
James Hogan9887d1c2016-06-14 09:40:13 +01001258 trace_kvm_asid_change(vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001259 kvm_read_c0_guest_entryhi(cop0)
James Hogan9887d1c2016-06-14 09:40:13 +01001260 & KVM_ENTRYHI_ASID,
1261 nasid);
Sanjay Lale685c682012-11-21 18:34:04 -08001262
James Hogan25b08c72016-09-16 00:06:43 +01001263 /*
James Hogana31b50d2016-12-16 15:57:00 +00001264 * Flush entries from the GVA page
1265 * tables.
1266 * Guest user page table will get
1267 * flushed lazily on re-entry to guest
1268 * user if the guest ASID actually
1269 * changes.
1270 */
1271 kvm_mips_flush_gva_pt(kern_mm->pgd,
1272 KMF_KERN);
1273
1274 /*
James Hogan25b08c72016-09-16 00:06:43 +01001275 * Regenerate/invalidate kernel MMU
1276 * context.
1277 * The user MMU context will be
1278 * regenerated lazily on re-entry to
1279 * guest user if the guest ASID actually
1280 * changes.
1281 */
James Hogan91e4f1b2016-09-15 17:20:06 +01001282 preempt_disable();
James Hogan91e4f1b2016-09-15 17:20:06 +01001283 cpu = smp_processor_id();
James Hogana98dd742016-10-07 22:39:41 +01001284 get_new_mmu_context(kern_mm, cpu);
James Hogan91e4f1b2016-09-15 17:20:06 +01001285 for_each_possible_cpu(i)
James Hogan25b08c72016-09-16 00:06:43 +01001286 if (i != cpu)
James Hoganc550d532016-10-11 23:14:39 +01001287 cpu_context(i, kern_mm) = 0;
James Hogan91e4f1b2016-09-15 17:20:06 +01001288 preempt_enable();
Sanjay Lale685c682012-11-21 18:34:04 -08001289 }
1290 kvm_write_c0_guest_entryhi(cop0,
1291 vcpu->arch.gprs[rt]);
1292 }
1293 /* Are we writing to COUNT */
1294 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001295 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001296 goto done;
1297 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
Sanjay Lale685c682012-11-21 18:34:04 -08001298 /* If we are writing to COMPARE */
1299 /* Clear pending timer interrupt, if any */
James Hogane30492b2014-05-29 10:16:35 +01001300 kvm_mips_write_compare(vcpu,
James Hoganb45bacd2016-04-22 10:38:46 +01001301 vcpu->arch.gprs[rt],
1302 true);
Sanjay Lale685c682012-11-21 18:34:04 -08001303 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
James Hogan6cdc65e2015-02-03 13:59:38 +00001304 unsigned int old_val, val, change;
1305
1306 old_val = kvm_read_c0_guest_status(cop0);
1307 val = vcpu->arch.gprs[rt];
1308 change = val ^ old_val;
1309
1310 /* Make sure that the NMI bit is never set */
1311 val &= ~ST0_NMI;
1312
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001313 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001314 * Don't allow CU1 or FR to be set unless FPU
1315 * capability enabled and exists in guest
1316 * configuration.
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001317 */
James Hogan6cdc65e2015-02-03 13:59:38 +00001318 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1319 val &= ~(ST0_CU1 | ST0_FR);
1320
1321 /*
1322 * Also don't allow FR to be set if host doesn't
1323 * support it.
1324 */
1325 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1326 val &= ~ST0_FR;
1327
1328
1329 /* Handle changes in FPU mode */
1330 preempt_disable();
1331
1332 /*
1333 * FPU and Vector register state is made
1334 * UNPREDICTABLE by a change of FR, so don't
1335 * even bother saving it.
1336 */
1337 if (change & ST0_FR)
1338 kvm_drop_fpu(vcpu);
1339
1340 /*
James Hogan2b6009d2015-02-06 23:01:00 +00001341 * If MSA state is already live, it is undefined
1342 * how it interacts with FR=0 FPU state, and we
1343 * don't want to hit reserved instruction
1344 * exceptions trying to save the MSA state later
1345 * when CU=1 && FR=1, so play it safe and save
1346 * it first.
1347 */
1348 if (change & ST0_CU1 && !(val & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001349 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001350 kvm_lose_fpu(vcpu);
1351
1352 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001353 * Propagate CU1 (FPU enable) changes
1354 * immediately if the FPU context is already
1355 * loaded. When disabling we leave the context
1356 * loaded so it can be quickly enabled again in
1357 * the near future.
1358 */
1359 if (change & ST0_CU1 &&
James Hoganf9431762016-06-14 09:40:10 +01001360 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001361 change_c0_status(ST0_CU1, val);
1362
1363 preempt_enable();
1364
1365 kvm_write_c0_guest_status(cop0, val);
Sanjay Lale685c682012-11-21 18:34:04 -08001366
1367#ifdef CONFIG_KVM_MIPS_DYN_TRANS
James Hogan6cdc65e2015-02-03 13:59:38 +00001368 /*
1369 * If FPU present, we need CU1/FR bits to take
1370 * effect fairly soon.
1371 */
1372 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1373 kvm_mips_trans_mtc0(inst, opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001374#endif
James Hogan6cdc65e2015-02-03 13:59:38 +00001375 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1376 unsigned int old_val, val, change, wrmask;
1377
1378 old_val = kvm_read_c0_guest_config5(cop0);
1379 val = vcpu->arch.gprs[rt];
1380
1381 /* Only a few bits are writable in Config5 */
1382 wrmask = kvm_mips_config5_wrmask(vcpu);
1383 change = (val ^ old_val) & wrmask;
1384 val = old_val ^ change;
1385
1386
James Hogan2b6009d2015-02-06 23:01:00 +00001387 /* Handle changes in FPU/MSA modes */
James Hogan6cdc65e2015-02-03 13:59:38 +00001388 preempt_disable();
1389
1390 /*
1391 * Propagate FRE changes immediately if the FPU
1392 * context is already loaded.
1393 */
1394 if (change & MIPS_CONF5_FRE &&
James Hoganf9431762016-06-14 09:40:10 +01001395 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001396 change_c0_config5(MIPS_CONF5_FRE, val);
1397
James Hogan2b6009d2015-02-06 23:01:00 +00001398 /*
1399 * Propagate MSAEn changes immediately if the
1400 * MSA context is already loaded. When disabling
1401 * we leave the context loaded so it can be
1402 * quickly enabled again in the near future.
1403 */
1404 if (change & MIPS_CONF5_MSAEN &&
James Hoganf9431762016-06-14 09:40:10 +01001405 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001406 change_c0_config5(MIPS_CONF5_MSAEN,
1407 val);
1408
James Hogan6cdc65e2015-02-03 13:59:38 +00001409 preempt_enable();
1410
1411 kvm_write_c0_guest_config5(cop0, val);
James Hogane30492b2014-05-29 10:16:35 +01001412 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
James Hogan8cffd192016-06-09 14:19:08 +01001413 u32 old_cause, new_cause;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001414
James Hogane30492b2014-05-29 10:16:35 +01001415 old_cause = kvm_read_c0_guest_cause(cop0);
1416 new_cause = vcpu->arch.gprs[rt];
1417 /* Update R/W bits */
1418 kvm_change_c0_guest_cause(cop0, 0x08800300,
1419 new_cause);
1420 /* DC bit enabling/disabling timer? */
1421 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1422 if (new_cause & CAUSEF_DC)
1423 kvm_mips_count_disable_cause(vcpu);
1424 else
1425 kvm_mips_count_enable_cause(vcpu);
1426 }
James Hogancef061d02016-06-15 19:29:54 +01001427 } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
1428 u32 mask = MIPS_HWRENA_CPUNUM |
1429 MIPS_HWRENA_SYNCISTEP |
1430 MIPS_HWRENA_CC |
1431 MIPS_HWRENA_CCRES;
1432
1433 if (kvm_read_c0_guest_config3(cop0) &
1434 MIPS_CONF3_ULRI)
1435 mask |= MIPS_HWRENA_ULR;
1436 cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
Sanjay Lale685c682012-11-21 18:34:04 -08001437 } else {
1438 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1439#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1440 kvm_mips_trans_mtc0(inst, opc, vcpu);
1441#endif
1442 }
Sanjay Lale685c682012-11-21 18:34:04 -08001443 break;
1444
1445 case dmtc_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001446 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1447 vcpu->arch.pc, rt, rd, sel);
James Hogan6398da12016-06-14 09:40:15 +01001448 trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
1449 KVM_TRACE_COP0(rd, sel),
1450 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001451 er = EMULATE_FAIL;
1452 break;
1453
James Hoganb2c59632015-12-16 23:49:38 +00001454 case mfmc0_op:
Sanjay Lale685c682012-11-21 18:34:04 -08001455#ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1456 cop0->stat[MIPS_CP0_STATUS][0]++;
1457#endif
James Hogancaa1faa2015-12-16 23:49:26 +00001458 if (rt != 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001459 vcpu->arch.gprs[rt] =
1460 kvm_read_c0_guest_status(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001461 /* EI */
James Hogan258f3a22016-06-15 19:29:47 +01001462 if (inst.mfmc0_format.sc) {
James Hoganb2c59632015-12-16 23:49:38 +00001463 kvm_debug("[%#lx] mfmc0_op: EI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001464 vcpu->arch.pc);
1465 kvm_set_c0_guest_status(cop0, ST0_IE);
1466 } else {
James Hoganb2c59632015-12-16 23:49:38 +00001467 kvm_debug("[%#lx] mfmc0_op: DI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001468 vcpu->arch.pc);
1469 kvm_clear_c0_guest_status(cop0, ST0_IE);
1470 }
1471
1472 break;
1473
1474 case wrpgpr_op:
1475 {
James Hogan8cffd192016-06-09 14:19:08 +01001476 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1477 u32 pss =
Sanjay Lale685c682012-11-21 18:34:04 -08001478 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001479 /*
1480 * We don't support any shadow register sets, so
1481 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1482 */
Sanjay Lale685c682012-11-21 18:34:04 -08001483 if (css || pss) {
1484 er = EMULATE_FAIL;
1485 break;
1486 }
1487 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1488 vcpu->arch.gprs[rt]);
1489 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1490 }
1491 break;
1492 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001493 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
James Hogan258f3a22016-06-15 19:29:47 +01001494 vcpu->arch.pc, inst.c0r_format.rs);
Sanjay Lale685c682012-11-21 18:34:04 -08001495 er = EMULATE_FAIL;
1496 break;
1497 }
1498 }
1499
1500done:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001501 /* Rollback PC only if emulation was unsuccessful */
1502 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001503 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001504
1505dont_update_pc:
1506 /*
1507 * This is for special instructions whose emulation
1508 * updates the PC, so do not overwrite the PC under
1509 * any circumstances
1510 */
1511
1512 return er;
1513}
1514
James Hogan258f3a22016-06-15 19:29:47 +01001515enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
1516 u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001517 struct kvm_run *run,
1518 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001519{
James Hogan8b48d5b2017-03-14 10:15:15 +00001520 enum emulation_result er;
James Hogan258f3a22016-06-15 19:29:47 +01001521 u32 rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001522 void *data = run->mmio.data;
1523 unsigned long curr_pc;
1524
1525 /*
1526 * Update PC and hold onto current PC in case there is
1527 * an error and we want to rollback the PC
1528 */
1529 curr_pc = vcpu->arch.pc;
1530 er = update_pc(vcpu, cause);
1531 if (er == EMULATE_FAIL)
1532 return er;
1533
James Hogan258f3a22016-06-15 19:29:47 +01001534 rt = inst.i_format.rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001535
James Hogan8b48d5b2017-03-14 10:15:15 +00001536 run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
1537 vcpu->arch.host_cp0_badvaddr);
1538 if (run->mmio.phys_addr == KVM_INVALID_ADDR)
1539 goto out_fail;
1540
James Hogan258f3a22016-06-15 19:29:47 +01001541 switch (inst.i_format.opcode) {
James Hogan59d78142017-03-14 10:15:16 +00001542#if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
1543 case sd_op:
1544 run->mmio.len = 8;
1545 *(u64 *)data = vcpu->arch.gprs[rt];
1546
1547 kvm_debug("[%#lx] OP_SD: eaddr: %#lx, gpr: %#lx, data: %#llx\n",
1548 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1549 vcpu->arch.gprs[rt], *(u64 *)data);
1550 break;
1551#endif
1552
Sanjay Lale685c682012-11-21 18:34:04 -08001553 case sw_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001554 run->mmio.len = 4;
1555 *(u32 *)data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001556
1557 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1558 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8b48d5b2017-03-14 10:15:15 +00001559 vcpu->arch.gprs[rt], *(u32 *)data);
Sanjay Lale685c682012-11-21 18:34:04 -08001560 break;
1561
1562 case sh_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001563 run->mmio.len = 2;
1564 *(u16 *)data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001565
1566 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1567 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8b48d5b2017-03-14 10:15:15 +00001568 vcpu->arch.gprs[rt], *(u16 *)data);
1569 break;
1570
1571 case sb_op:
1572 run->mmio.len = 1;
1573 *(u8 *)data = vcpu->arch.gprs[rt];
1574
1575 kvm_debug("[%#lx] OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1576 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1577 vcpu->arch.gprs[rt], *(u8 *)data);
Sanjay Lale685c682012-11-21 18:34:04 -08001578 break;
1579
1580 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001581 kvm_err("Store not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001582 inst.word);
James Hogan8b48d5b2017-03-14 10:15:15 +00001583 goto out_fail;
Sanjay Lale685c682012-11-21 18:34:04 -08001584 }
1585
James Hogan8b48d5b2017-03-14 10:15:15 +00001586 run->mmio.is_write = 1;
1587 vcpu->mmio_needed = 1;
1588 vcpu->mmio_is_write = 1;
1589 return EMULATE_DO_MMIO;
Sanjay Lale685c682012-11-21 18:34:04 -08001590
James Hogan8b48d5b2017-03-14 10:15:15 +00001591out_fail:
1592 /* Rollback PC if emulation was unsuccessful */
1593 vcpu->arch.pc = curr_pc;
1594 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001595}
1596
James Hogan258f3a22016-06-15 19:29:47 +01001597enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
1598 u32 cause, struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001599 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001600{
James Hogan8b48d5b2017-03-14 10:15:15 +00001601 enum emulation_result er;
James Hogane1e575f62016-10-25 16:11:12 +01001602 unsigned long curr_pc;
James Hogan258f3a22016-06-15 19:29:47 +01001603 u32 op, rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001604
James Hogan258f3a22016-06-15 19:29:47 +01001605 rt = inst.i_format.rt;
1606 op = inst.i_format.opcode;
Sanjay Lale685c682012-11-21 18:34:04 -08001607
James Hogane1e575f62016-10-25 16:11:12 +01001608 /*
1609 * Find the resume PC now while we have safe and easy access to the
1610 * prior branch instruction, and save it for
1611 * kvm_mips_complete_mmio_load() to restore later.
1612 */
1613 curr_pc = vcpu->arch.pc;
1614 er = update_pc(vcpu, cause);
1615 if (er == EMULATE_FAIL)
1616 return er;
1617 vcpu->arch.io_pc = vcpu->arch.pc;
1618 vcpu->arch.pc = curr_pc;
1619
Sanjay Lale685c682012-11-21 18:34:04 -08001620 vcpu->arch.io_gpr = rt;
1621
James Hogan8b48d5b2017-03-14 10:15:15 +00001622 run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
1623 vcpu->arch.host_cp0_badvaddr);
1624 if (run->mmio.phys_addr == KVM_INVALID_ADDR)
1625 return EMULATE_FAIL;
1626
1627 vcpu->mmio_needed = 2; /* signed */
Sanjay Lale685c682012-11-21 18:34:04 -08001628 switch (op) {
James Hogan59d78142017-03-14 10:15:16 +00001629#if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
1630 case ld_op:
1631 run->mmio.len = 8;
1632 break;
1633
1634 case lwu_op:
1635 vcpu->mmio_needed = 1; /* unsigned */
1636 /* fall through */
1637#endif
Sanjay Lale685c682012-11-21 18:34:04 -08001638 case lw_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001639 run->mmio.len = 4;
Sanjay Lale685c682012-11-21 18:34:04 -08001640 break;
1641
Sanjay Lale685c682012-11-21 18:34:04 -08001642 case lhu_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001643 vcpu->mmio_needed = 1; /* unsigned */
1644 /* fall through */
1645 case lh_op:
1646 run->mmio.len = 2;
Sanjay Lale685c682012-11-21 18:34:04 -08001647 break;
1648
1649 case lbu_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001650 vcpu->mmio_needed = 1; /* unsigned */
1651 /* fall through */
Sanjay Lale685c682012-11-21 18:34:04 -08001652 case lb_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001653 run->mmio.len = 1;
Sanjay Lale685c682012-11-21 18:34:04 -08001654 break;
1655
1656 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001657 kvm_err("Load not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001658 inst.word);
James Hogan8b48d5b2017-03-14 10:15:15 +00001659 vcpu->mmio_needed = 0;
1660 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001661 }
1662
James Hogan8b48d5b2017-03-14 10:15:15 +00001663 run->mmio.is_write = 0;
1664 vcpu->mmio_is_write = 0;
1665 return EMULATE_DO_MMIO;
Sanjay Lale685c682012-11-21 18:34:04 -08001666}
1667
James Hogan4cf74c92016-11-26 00:37:28 +00001668static enum emulation_result kvm_mips_guest_cache_op(int (*fn)(unsigned long),
1669 unsigned long curr_pc,
1670 unsigned long addr,
1671 struct kvm_run *run,
1672 struct kvm_vcpu *vcpu,
1673 u32 cause)
1674{
1675 int err;
1676
1677 for (;;) {
1678 /* Carefully attempt the cache operation */
1679 kvm_trap_emul_gva_lockless_begin(vcpu);
1680 err = fn(addr);
1681 kvm_trap_emul_gva_lockless_end(vcpu);
1682
1683 if (likely(!err))
1684 return EMULATE_DONE;
1685
1686 /*
1687 * Try to handle the fault and retry, maybe we just raced with a
1688 * GVA invalidation.
1689 */
1690 switch (kvm_trap_emul_gva_fault(vcpu, addr, false)) {
1691 case KVM_MIPS_GVA:
1692 case KVM_MIPS_GPA:
1693 /* bad virtual or physical address */
1694 return EMULATE_FAIL;
1695 case KVM_MIPS_TLB:
1696 /* no matching guest TLB */
1697 vcpu->arch.host_cp0_badvaddr = addr;
1698 vcpu->arch.pc = curr_pc;
1699 kvm_mips_emulate_tlbmiss_ld(cause, NULL, run, vcpu);
1700 return EMULATE_EXCEPT;
1701 case KVM_MIPS_TLBINV:
1702 /* invalid matching guest TLB */
1703 vcpu->arch.host_cp0_badvaddr = addr;
1704 vcpu->arch.pc = curr_pc;
1705 kvm_mips_emulate_tlbinv_ld(cause, NULL, run, vcpu);
1706 return EMULATE_EXCEPT;
1707 default:
1708 break;
1709 };
1710 }
1711}
1712
James Hogan258f3a22016-06-15 19:29:47 +01001713enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
1714 u32 *opc, u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001715 struct kvm_run *run,
1716 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001717{
Sanjay Lale685c682012-11-21 18:34:04 -08001718 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01001719 u32 cache, op_inst, op, base;
1720 s16 offset;
Sanjay Lale685c682012-11-21 18:34:04 -08001721 struct kvm_vcpu_arch *arch = &vcpu->arch;
1722 unsigned long va;
1723 unsigned long curr_pc;
1724
1725 /*
1726 * Update PC and hold onto current PC in case there is
1727 * an error and we want to rollback the PC
1728 */
1729 curr_pc = vcpu->arch.pc;
1730 er = update_pc(vcpu, cause);
1731 if (er == EMULATE_FAIL)
1732 return er;
1733
James Hogan258f3a22016-06-15 19:29:47 +01001734 base = inst.i_format.rs;
1735 op_inst = inst.i_format.rt;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001736 if (cpu_has_mips_r6)
1737 offset = inst.spec3_format.simmediate;
1738 else
1739 offset = inst.i_format.simmediate;
James Hoganf4956f62015-12-16 23:49:37 +00001740 cache = op_inst & CacheOp_Cache;
1741 op = op_inst & CacheOp_Op;
Sanjay Lale685c682012-11-21 18:34:04 -08001742
1743 va = arch->gprs[base] + offset;
1744
1745 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1746 cache, op, base, arch->gprs[base], offset);
1747
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001748 /*
1749 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1750 * invalidate the caches entirely by stepping through all the
1751 * ways/indexes
Sanjay Lale685c682012-11-21 18:34:04 -08001752 */
James Hoganf4956f62015-12-16 23:49:37 +00001753 if (op == Index_Writeback_Inv) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001754 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1755 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1756 arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001757
James Hoganf4956f62015-12-16 23:49:37 +00001758 if (cache == Cache_D)
Sanjay Lale685c682012-11-21 18:34:04 -08001759 r4k_blast_dcache();
James Hoganf4956f62015-12-16 23:49:37 +00001760 else if (cache == Cache_I)
Sanjay Lale685c682012-11-21 18:34:04 -08001761 r4k_blast_icache();
1762 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001763 kvm_err("%s: unsupported CACHE INDEX operation\n",
1764 __func__);
Sanjay Lale685c682012-11-21 18:34:04 -08001765 return EMULATE_FAIL;
1766 }
1767
1768#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1769 kvm_mips_trans_cache_index(inst, opc, vcpu);
1770#endif
1771 goto done;
1772 }
1773
Sanjay Lale685c682012-11-21 18:34:04 -08001774 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
James Hoganf4956f62015-12-16 23:49:37 +00001775 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
James Hogan4cf74c92016-11-26 00:37:28 +00001776 /*
1777 * Perform the dcache part of icache synchronisation on the
1778 * guest's behalf.
1779 */
1780 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1781 curr_pc, va, run, vcpu, cause);
1782 if (er != EMULATE_DONE)
1783 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001784#ifdef CONFIG_KVM_MIPS_DYN_TRANS
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001785 /*
1786 * Replace the CACHE instruction, with a SYNCI, not the same,
1787 * but avoids a trap
1788 */
Sanjay Lale685c682012-11-21 18:34:04 -08001789 kvm_mips_trans_cache_va(inst, opc, vcpu);
1790#endif
James Hoganf4956f62015-12-16 23:49:37 +00001791 } else if (op_inst == Hit_Invalidate_I) {
James Hogan4cf74c92016-11-26 00:37:28 +00001792 /* Perform the icache synchronisation on the guest's behalf */
1793 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1794 curr_pc, va, run, vcpu, cause);
1795 if (er != EMULATE_DONE)
1796 goto done;
1797 er = kvm_mips_guest_cache_op(protected_flush_icache_line,
1798 curr_pc, va, run, vcpu, cause);
1799 if (er != EMULATE_DONE)
1800 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001801
1802#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1803 /* Replace the CACHE instruction, with a SYNCI */
1804 kvm_mips_trans_cache_va(inst, opc, vcpu);
1805#endif
1806 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001807 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1808 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001809 er = EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001810 }
1811
James Hogancc81e942016-06-09 10:50:45 +01001812done:
1813 /* Rollback PC only if emulation was unsuccessful */
1814 if (er == EMULATE_FAIL)
1815 vcpu->arch.pc = curr_pc;
James Hogan4cf74c92016-11-26 00:37:28 +00001816 /* Guest exception needs guest to resume */
1817 if (er == EMULATE_EXCEPT)
1818 er = EMULATE_DONE;
James Hogancc81e942016-06-09 10:50:45 +01001819
Sanjay Lale685c682012-11-21 18:34:04 -08001820 return er;
1821}
1822
James Hogan31cf7492016-06-09 14:19:09 +01001823enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001824 struct kvm_run *run,
1825 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001826{
James Hogan258f3a22016-06-15 19:29:47 +01001827 union mips_instruction inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001828 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001829 int err;
Sanjay Lale685c682012-11-21 18:34:04 -08001830
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001831 /* Fetch the instruction. */
1832 if (cause & CAUSEF_BD)
Sanjay Lale685c682012-11-21 18:34:04 -08001833 opc += 1;
James Hogan6a97c772015-04-23 16:54:35 +01001834 err = kvm_get_badinstr(opc, vcpu, &inst.word);
James Hogan122e51d2016-11-28 17:23:14 +00001835 if (err)
1836 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001837
James Hogan258f3a22016-06-15 19:29:47 +01001838 switch (inst.r_format.opcode) {
Sanjay Lale685c682012-11-21 18:34:04 -08001839 case cop0_op:
1840 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1841 break;
Sanjay Lale685c682012-11-21 18:34:04 -08001842
James Hogan5cc4aaf2016-07-04 19:35:13 +01001843#ifndef CONFIG_CPU_MIPSR6
Sanjay Lale685c682012-11-21 18:34:04 -08001844 case cache_op:
1845 ++vcpu->stat.cache_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001846 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
Sanjay Lale685c682012-11-21 18:34:04 -08001847 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1848 break;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001849#else
1850 case spec3_op:
1851 switch (inst.spec3_format.func) {
1852 case cache6_op:
1853 ++vcpu->stat.cache_exits;
1854 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1855 er = kvm_mips_emulate_cache(inst, opc, cause, run,
1856 vcpu);
1857 break;
1858 default:
1859 goto unknown;
1860 };
1861 break;
1862unknown:
1863#endif
Sanjay Lale685c682012-11-21 18:34:04 -08001864
1865 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001866 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
James Hogan258f3a22016-06-15 19:29:47 +01001867 inst.word);
Sanjay Lale685c682012-11-21 18:34:04 -08001868 kvm_arch_vcpu_dump_regs(vcpu);
1869 er = EMULATE_FAIL;
1870 break;
1871 }
1872
1873 return er;
1874}
1875
James Hogan7801bbe2016-11-14 23:59:27 +00001876/**
1877 * kvm_mips_guest_exception_base() - Find guest exception vector base address.
1878 *
1879 * Returns: The base address of the current guest exception vector, taking
1880 * both Guest.CP0_Status.BEV and Guest.CP0_EBase into account.
1881 */
1882long kvm_mips_guest_exception_base(struct kvm_vcpu *vcpu)
1883{
1884 struct mips_coproc *cop0 = vcpu->arch.cop0;
1885
1886 if (kvm_read_c0_guest_status(cop0) & ST0_BEV)
1887 return KVM_GUEST_CKSEG1ADDR(0x1fc00200);
1888 else
1889 return kvm_read_c0_guest_ebase(cop0) & MIPS_EBASE_BASE;
1890}
1891
James Hogan31cf7492016-06-09 14:19:09 +01001892enum emulation_result kvm_mips_emulate_syscall(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001893 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001894 struct kvm_run *run,
1895 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001896{
1897 struct mips_coproc *cop0 = vcpu->arch.cop0;
1898 struct kvm_vcpu_arch *arch = &vcpu->arch;
1899 enum emulation_result er = EMULATE_DONE;
1900
1901 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1902 /* save old pc */
1903 kvm_write_c0_guest_epc(cop0, arch->pc);
1904 kvm_set_c0_guest_status(cop0, ST0_EXL);
1905
1906 if (cause & CAUSEF_BD)
1907 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1908 else
1909 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1910
1911 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1912
1913 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001914 (EXCCODE_SYS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001915
1916 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00001917 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08001918
1919 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001920 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001921 er = EMULATE_FAIL;
1922 }
1923
1924 return er;
1925}
1926
James Hogan31cf7492016-06-09 14:19:09 +01001927enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001928 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001929 struct kvm_run *run,
1930 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001931{
1932 struct mips_coproc *cop0 = vcpu->arch.cop0;
1933 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001934 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001935 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001936
1937 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1938 /* save old pc */
1939 kvm_write_c0_guest_epc(cop0, arch->pc);
1940 kvm_set_c0_guest_status(cop0, ST0_EXL);
1941
1942 if (cause & CAUSEF_BD)
1943 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1944 else
1945 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1946
1947 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1948 arch->pc);
1949
1950 /* set pc to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00001951 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
Sanjay Lale685c682012-11-21 18:34:04 -08001952
1953 } else {
1954 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1955 arch->pc);
1956
James Hogan7801bbe2016-11-14 23:59:27 +00001957 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08001958 }
1959
1960 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001961 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001962
1963 /* setup badvaddr, context and entryhi registers for the guest */
1964 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1965 /* XXXKYMA: is the context register used by linux??? */
1966 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08001967
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001968 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001969}
1970
James Hogan31cf7492016-06-09 14:19:09 +01001971enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001972 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001973 struct kvm_run *run,
1974 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001975{
1976 struct mips_coproc *cop0 = vcpu->arch.cop0;
1977 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001978 unsigned long entryhi =
1979 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001980 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001981
1982 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1983 /* save old pc */
1984 kvm_write_c0_guest_epc(cop0, arch->pc);
1985 kvm_set_c0_guest_status(cop0, ST0_EXL);
1986
1987 if (cause & CAUSEF_BD)
1988 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1989 else
1990 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1991
1992 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
1993 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08001994 } else {
1995 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1996 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08001997 }
1998
James Hogan7801bbe2016-11-14 23:59:27 +00001999 /* set pc to the exception entry point */
2000 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2001
Sanjay Lale685c682012-11-21 18:34:04 -08002002 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002003 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002004
2005 /* setup badvaddr, context and entryhi registers for the guest */
2006 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2007 /* XXXKYMA: is the context register used by linux??? */
2008 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002009
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002010 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002011}
2012
James Hogan31cf7492016-06-09 14:19:09 +01002013enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002014 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002015 struct kvm_run *run,
2016 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002017{
2018 struct mips_coproc *cop0 = vcpu->arch.cop0;
2019 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002020 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002021 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002022
2023 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2024 /* save old pc */
2025 kvm_write_c0_guest_epc(cop0, arch->pc);
2026 kvm_set_c0_guest_status(cop0, ST0_EXL);
2027
2028 if (cause & CAUSEF_BD)
2029 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2030 else
2031 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2032
2033 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2034 arch->pc);
2035
2036 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002037 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
Sanjay Lale685c682012-11-21 18:34:04 -08002038 } else {
2039 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2040 arch->pc);
James Hogan7801bbe2016-11-14 23:59:27 +00002041 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002042 }
2043
2044 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002045 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002046
2047 /* setup badvaddr, context and entryhi registers for the guest */
2048 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2049 /* XXXKYMA: is the context register used by linux??? */
2050 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002051
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002052 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002053}
2054
James Hogan31cf7492016-06-09 14:19:09 +01002055enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002056 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002057 struct kvm_run *run,
2058 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002059{
2060 struct mips_coproc *cop0 = vcpu->arch.cop0;
2061 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002062 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002063 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002064
2065 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2066 /* save old pc */
2067 kvm_write_c0_guest_epc(cop0, arch->pc);
2068 kvm_set_c0_guest_status(cop0, ST0_EXL);
2069
2070 if (cause & CAUSEF_BD)
2071 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2072 else
2073 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2074
2075 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2076 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002077 } else {
2078 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2079 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002080 }
2081
James Hogan7801bbe2016-11-14 23:59:27 +00002082 /* Set PC to the exception entry point */
2083 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2084
Sanjay Lale685c682012-11-21 18:34:04 -08002085 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002086 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002087
2088 /* setup badvaddr, context and entryhi registers for the guest */
2089 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2090 /* XXXKYMA: is the context register used by linux??? */
2091 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002092
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002093 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002094}
2095
James Hogan31cf7492016-06-09 14:19:09 +01002096enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002097 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002098 struct kvm_run *run,
2099 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002100{
2101 struct mips_coproc *cop0 = vcpu->arch.cop0;
2102 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002103 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002104 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002105
2106 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2107 /* save old pc */
2108 kvm_write_c0_guest_epc(cop0, arch->pc);
2109 kvm_set_c0_guest_status(cop0, ST0_EXL);
2110
2111 if (cause & CAUSEF_BD)
2112 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2113 else
2114 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2115
2116 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2117 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002118 } else {
2119 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2120 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002121 }
2122
James Hogan7801bbe2016-11-14 23:59:27 +00002123 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2124
James Hogan16d100db2015-12-16 23:49:33 +00002125 kvm_change_c0_guest_cause(cop0, (0xff),
2126 (EXCCODE_MOD << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002127
2128 /* setup badvaddr, context and entryhi registers for the guest */
2129 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2130 /* XXXKYMA: is the context register used by linux??? */
2131 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002132
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002133 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002134}
2135
James Hogan31cf7492016-06-09 14:19:09 +01002136enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002137 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002138 struct kvm_run *run,
2139 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002140{
2141 struct mips_coproc *cop0 = vcpu->arch.cop0;
2142 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002143
2144 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2145 /* save old pc */
2146 kvm_write_c0_guest_epc(cop0, arch->pc);
2147 kvm_set_c0_guest_status(cop0, ST0_EXL);
2148
2149 if (cause & CAUSEF_BD)
2150 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2151 else
2152 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2153
2154 }
2155
James Hogan7801bbe2016-11-14 23:59:27 +00002156 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002157
2158 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002159 (EXCCODE_CPU << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002160 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2161
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002162 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002163}
2164
James Hogan31cf7492016-06-09 14:19:09 +01002165enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002166 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002167 struct kvm_run *run,
2168 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002169{
2170 struct mips_coproc *cop0 = vcpu->arch.cop0;
2171 struct kvm_vcpu_arch *arch = &vcpu->arch;
2172 enum emulation_result er = EMULATE_DONE;
2173
2174 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2175 /* save old pc */
2176 kvm_write_c0_guest_epc(cop0, arch->pc);
2177 kvm_set_c0_guest_status(cop0, ST0_EXL);
2178
2179 if (cause & CAUSEF_BD)
2180 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2181 else
2182 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2183
2184 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2185
2186 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002187 (EXCCODE_RI << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002188
2189 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002190 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002191
2192 } else {
2193 kvm_err("Trying to deliver RI when EXL is already set\n");
2194 er = EMULATE_FAIL;
2195 }
2196
2197 return er;
2198}
2199
James Hogan31cf7492016-06-09 14:19:09 +01002200enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002201 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002202 struct kvm_run *run,
2203 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002204{
2205 struct mips_coproc *cop0 = vcpu->arch.cop0;
2206 struct kvm_vcpu_arch *arch = &vcpu->arch;
2207 enum emulation_result er = EMULATE_DONE;
2208
2209 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2210 /* save old pc */
2211 kvm_write_c0_guest_epc(cop0, arch->pc);
2212 kvm_set_c0_guest_status(cop0, ST0_EXL);
2213
2214 if (cause & CAUSEF_BD)
2215 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2216 else
2217 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2218
2219 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2220
2221 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002222 (EXCCODE_BP << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002223
2224 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002225 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002226
2227 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002228 kvm_err("Trying to deliver BP when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002229 er = EMULATE_FAIL;
2230 }
2231
2232 return er;
2233}
2234
James Hogan31cf7492016-06-09 14:19:09 +01002235enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002236 u32 *opc,
James Hogan0a560422015-02-06 16:03:57 +00002237 struct kvm_run *run,
2238 struct kvm_vcpu *vcpu)
2239{
2240 struct mips_coproc *cop0 = vcpu->arch.cop0;
2241 struct kvm_vcpu_arch *arch = &vcpu->arch;
2242 enum emulation_result er = EMULATE_DONE;
2243
2244 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2245 /* save old pc */
2246 kvm_write_c0_guest_epc(cop0, arch->pc);
2247 kvm_set_c0_guest_status(cop0, ST0_EXL);
2248
2249 if (cause & CAUSEF_BD)
2250 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2251 else
2252 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2253
2254 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2255
2256 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002257 (EXCCODE_TR << CAUSEB_EXCCODE));
James Hogan0a560422015-02-06 16:03:57 +00002258
2259 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002260 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hogan0a560422015-02-06 16:03:57 +00002261
2262 } else {
2263 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2264 er = EMULATE_FAIL;
2265 }
2266
2267 return er;
2268}
2269
James Hogan31cf7492016-06-09 14:19:09 +01002270enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002271 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002272 struct kvm_run *run,
2273 struct kvm_vcpu *vcpu)
2274{
2275 struct mips_coproc *cop0 = vcpu->arch.cop0;
2276 struct kvm_vcpu_arch *arch = &vcpu->arch;
2277 enum emulation_result er = EMULATE_DONE;
2278
2279 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2280 /* save old pc */
2281 kvm_write_c0_guest_epc(cop0, arch->pc);
2282 kvm_set_c0_guest_status(cop0, ST0_EXL);
2283
2284 if (cause & CAUSEF_BD)
2285 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2286 else
2287 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2288
2289 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2290
2291 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002292 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002293
2294 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002295 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hoganc2537ed2015-02-06 10:56:27 +00002296
2297 } else {
2298 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2299 er = EMULATE_FAIL;
2300 }
2301
2302 return er;
2303}
2304
James Hogan31cf7492016-06-09 14:19:09 +01002305enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002306 u32 *opc,
James Hogan1c0cd662015-02-06 10:56:27 +00002307 struct kvm_run *run,
2308 struct kvm_vcpu *vcpu)
2309{
2310 struct mips_coproc *cop0 = vcpu->arch.cop0;
2311 struct kvm_vcpu_arch *arch = &vcpu->arch;
2312 enum emulation_result er = EMULATE_DONE;
2313
2314 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2315 /* save old pc */
2316 kvm_write_c0_guest_epc(cop0, arch->pc);
2317 kvm_set_c0_guest_status(cop0, ST0_EXL);
2318
2319 if (cause & CAUSEF_BD)
2320 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2321 else
2322 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2323
2324 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2325
2326 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002327 (EXCCODE_FPE << CAUSEB_EXCCODE));
James Hogan1c0cd662015-02-06 10:56:27 +00002328
2329 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002330 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hogan1c0cd662015-02-06 10:56:27 +00002331
2332 } else {
2333 kvm_err("Trying to deliver FPE when EXL is already set\n");
2334 er = EMULATE_FAIL;
2335 }
2336
2337 return er;
2338}
2339
James Hogan31cf7492016-06-09 14:19:09 +01002340enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002341 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002342 struct kvm_run *run,
2343 struct kvm_vcpu *vcpu)
2344{
2345 struct mips_coproc *cop0 = vcpu->arch.cop0;
2346 struct kvm_vcpu_arch *arch = &vcpu->arch;
2347 enum emulation_result er = EMULATE_DONE;
2348
2349 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2350 /* save old pc */
2351 kvm_write_c0_guest_epc(cop0, arch->pc);
2352 kvm_set_c0_guest_status(cop0, ST0_EXL);
2353
2354 if (cause & CAUSEF_BD)
2355 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2356 else
2357 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2358
2359 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2360
2361 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002362 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002363
2364 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002365 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hoganc2537ed2015-02-06 10:56:27 +00002366
2367 } else {
2368 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2369 er = EMULATE_FAIL;
2370 }
2371
2372 return er;
2373}
2374
James Hogan31cf7492016-06-09 14:19:09 +01002375enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002376 struct kvm_run *run,
2377 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002378{
2379 struct mips_coproc *cop0 = vcpu->arch.cop0;
2380 struct kvm_vcpu_arch *arch = &vcpu->arch;
2381 enum emulation_result er = EMULATE_DONE;
2382 unsigned long curr_pc;
James Hogan258f3a22016-06-15 19:29:47 +01002383 union mips_instruction inst;
James Hogan122e51d2016-11-28 17:23:14 +00002384 int err;
Sanjay Lale685c682012-11-21 18:34:04 -08002385
2386 /*
2387 * Update PC and hold onto current PC in case there is
2388 * an error and we want to rollback the PC
2389 */
2390 curr_pc = vcpu->arch.pc;
2391 er = update_pc(vcpu, cause);
2392 if (er == EMULATE_FAIL)
2393 return er;
2394
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002395 /* Fetch the instruction. */
Sanjay Lale685c682012-11-21 18:34:04 -08002396 if (cause & CAUSEF_BD)
2397 opc += 1;
James Hogan6a97c772015-04-23 16:54:35 +01002398 err = kvm_get_badinstr(opc, vcpu, &inst.word);
James Hogan122e51d2016-11-28 17:23:14 +00002399 if (err) {
2400 kvm_err("%s: Cannot get inst @ %p (%d)\n", __func__, opc, err);
Sanjay Lale685c682012-11-21 18:34:04 -08002401 return EMULATE_FAIL;
2402 }
2403
James Hogan258f3a22016-06-15 19:29:47 +01002404 if (inst.r_format.opcode == spec3_op &&
James Hogan8eeab812016-07-04 19:35:14 +01002405 inst.r_format.func == rdhwr_op &&
2406 inst.r_format.rs == 0 &&
2407 (inst.r_format.re >> 3) == 0) {
James Hogan26f4f3b2014-03-14 13:06:09 +00002408 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
James Hogan258f3a22016-06-15 19:29:47 +01002409 int rd = inst.r_format.rd;
2410 int rt = inst.r_format.rt;
2411 int sel = inst.r_format.re & 0x7;
James Hogan6398da12016-06-14 09:40:15 +01002412
James Hogan26f4f3b2014-03-14 13:06:09 +00002413 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2414 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2415 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2416 rd, opc);
2417 goto emulate_ri;
2418 }
Sanjay Lale685c682012-11-21 18:34:04 -08002419 switch (rd) {
James Hoganaff565a2016-06-15 19:29:52 +01002420 case MIPS_HWR_CPUNUM: /* CPU number */
James Hogancf1fb0f2016-06-15 19:29:55 +01002421 arch->gprs[rt] = vcpu->vcpu_id;
Sanjay Lale685c682012-11-21 18:34:04 -08002422 break;
James Hoganaff565a2016-06-15 19:29:52 +01002423 case MIPS_HWR_SYNCISTEP: /* SYNCI length */
Sanjay Lale685c682012-11-21 18:34:04 -08002424 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2425 current_cpu_data.icache.linesz);
2426 break;
James Hoganaff565a2016-06-15 19:29:52 +01002427 case MIPS_HWR_CC: /* Read count register */
James Hogan172e02d2016-07-08 11:53:28 +01002428 arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002429 break;
James Hoganaff565a2016-06-15 19:29:52 +01002430 case MIPS_HWR_CCRES: /* Count register resolution */
Sanjay Lale685c682012-11-21 18:34:04 -08002431 switch (current_cpu_data.cputype) {
2432 case CPU_20KC:
2433 case CPU_25KF:
2434 arch->gprs[rt] = 1;
2435 break;
2436 default:
2437 arch->gprs[rt] = 2;
2438 }
2439 break;
James Hoganaff565a2016-06-15 19:29:52 +01002440 case MIPS_HWR_ULR: /* Read UserLocal register */
Sanjay Lale685c682012-11-21 18:34:04 -08002441 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08002442 break;
2443
2444 default:
James Hogan15505672014-03-14 13:06:07 +00002445 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
James Hogan26f4f3b2014-03-14 13:06:09 +00002446 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002447 }
James Hogan6398da12016-06-14 09:40:15 +01002448
2449 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
2450 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08002451 } else {
James Hogan258f3a22016-06-15 19:29:47 +01002452 kvm_debug("Emulate RI not supported @ %p: %#x\n",
2453 opc, inst.word);
James Hogan26f4f3b2014-03-14 13:06:09 +00002454 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002455 }
2456
James Hogan26f4f3b2014-03-14 13:06:09 +00002457 return EMULATE_DONE;
2458
2459emulate_ri:
Sanjay Lale685c682012-11-21 18:34:04 -08002460 /*
James Hogan26f4f3b2014-03-14 13:06:09 +00002461 * Rollback PC (if in branch delay slot then the PC already points to
2462 * branch target), and pass the RI exception to the guest OS.
Sanjay Lale685c682012-11-21 18:34:04 -08002463 */
James Hogan26f4f3b2014-03-14 13:06:09 +00002464 vcpu->arch.pc = curr_pc;
2465 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002466}
2467
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002468enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2469 struct kvm_run *run)
Sanjay Lale685c682012-11-21 18:34:04 -08002470{
2471 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2472 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002473
2474 if (run->mmio.len > sizeof(*gpr)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002475 kvm_err("Bad MMIO length: %d", run->mmio.len);
Sanjay Lale685c682012-11-21 18:34:04 -08002476 er = EMULATE_FAIL;
2477 goto done;
2478 }
2479
James Hogane1e575f62016-10-25 16:11:12 +01002480 /* Restore saved resume PC */
2481 vcpu->arch.pc = vcpu->arch.io_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08002482
2483 switch (run->mmio.len) {
James Hogan59d78142017-03-14 10:15:16 +00002484 case 8:
2485 *gpr = *(s64 *)run->mmio.data;
2486 break;
2487
Sanjay Lale685c682012-11-21 18:34:04 -08002488 case 4:
James Hogan59d78142017-03-14 10:15:16 +00002489 if (vcpu->mmio_needed == 2)
2490 *gpr = *(s32 *)run->mmio.data;
2491 else
2492 *gpr = *(u32 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002493 break;
2494
2495 case 2:
2496 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002497 *gpr = *(s16 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002498 else
James Hogan8cffd192016-06-09 14:19:08 +01002499 *gpr = *(u16 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002500
2501 break;
2502 case 1:
2503 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002504 *gpr = *(s8 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002505 else
2506 *gpr = *(u8 *) run->mmio.data;
2507 break;
2508 }
2509
Sanjay Lale685c682012-11-21 18:34:04 -08002510done:
2511 return er;
2512}
2513
James Hogan31cf7492016-06-09 14:19:09 +01002514static enum emulation_result kvm_mips_emulate_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002515 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002516 struct kvm_run *run,
2517 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002518{
James Hogan8cffd192016-06-09 14:19:08 +01002519 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002520 struct mips_coproc *cop0 = vcpu->arch.cop0;
2521 struct kvm_vcpu_arch *arch = &vcpu->arch;
2522 enum emulation_result er = EMULATE_DONE;
2523
2524 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2525 /* save old pc */
2526 kvm_write_c0_guest_epc(cop0, arch->pc);
2527 kvm_set_c0_guest_status(cop0, ST0_EXL);
2528
2529 if (cause & CAUSEF_BD)
2530 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2531 else
2532 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2533
2534 kvm_change_c0_guest_cause(cop0, (0xff),
2535 (exccode << CAUSEB_EXCCODE));
2536
2537 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002538 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002539 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2540
2541 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2542 exccode, kvm_read_c0_guest_epc(cop0),
2543 kvm_read_c0_guest_badvaddr(cop0));
2544 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002545 kvm_err("Trying to deliver EXC when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002546 er = EMULATE_FAIL;
2547 }
2548
2549 return er;
2550}
2551
James Hogan31cf7492016-06-09 14:19:09 +01002552enum emulation_result kvm_mips_check_privilege(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002553 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002554 struct kvm_run *run,
2555 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002556{
2557 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002558 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002559 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2560
2561 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2562
2563 if (usermode) {
2564 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00002565 case EXCCODE_INT:
2566 case EXCCODE_SYS:
2567 case EXCCODE_BP:
2568 case EXCCODE_RI:
2569 case EXCCODE_TR:
2570 case EXCCODE_MSAFPE:
2571 case EXCCODE_FPE:
2572 case EXCCODE_MSADIS:
Sanjay Lale685c682012-11-21 18:34:04 -08002573 break;
2574
James Hogan16d100db2015-12-16 23:49:33 +00002575 case EXCCODE_CPU:
Sanjay Lale685c682012-11-21 18:34:04 -08002576 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2577 er = EMULATE_PRIV_FAIL;
2578 break;
2579
James Hogan16d100db2015-12-16 23:49:33 +00002580 case EXCCODE_MOD:
Sanjay Lale685c682012-11-21 18:34:04 -08002581 break;
2582
James Hogan16d100db2015-12-16 23:49:33 +00002583 case EXCCODE_TLBL:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002584 /*
2585 * We we are accessing Guest kernel space, then send an
2586 * address error exception to the guest
2587 */
Sanjay Lale685c682012-11-21 18:34:04 -08002588 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002589 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2590 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002591 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002592 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002593 er = EMULATE_PRIV_FAIL;
2594 }
2595 break;
2596
James Hogan16d100db2015-12-16 23:49:33 +00002597 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002598 /*
2599 * We we are accessing Guest kernel space, then send an
2600 * address error exception to the guest
2601 */
Sanjay Lale685c682012-11-21 18:34:04 -08002602 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002603 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2604 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002605 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002606 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002607 er = EMULATE_PRIV_FAIL;
2608 }
2609 break;
2610
James Hogan16d100db2015-12-16 23:49:33 +00002611 case EXCCODE_ADES:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002612 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2613 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002614 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2615 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002616 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002617 }
2618 er = EMULATE_PRIV_FAIL;
2619 break;
James Hogan16d100db2015-12-16 23:49:33 +00002620 case EXCCODE_ADEL:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002621 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2622 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002623 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2624 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002625 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002626 }
2627 er = EMULATE_PRIV_FAIL;
2628 break;
2629 default:
2630 er = EMULATE_PRIV_FAIL;
2631 break;
2632 }
2633 }
2634
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002635 if (er == EMULATE_PRIV_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08002636 kvm_mips_emulate_exc(cause, opc, run, vcpu);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002637
Sanjay Lale685c682012-11-21 18:34:04 -08002638 return er;
2639}
2640
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002641/*
2642 * User Address (UA) fault, this could happen if
Sanjay Lale685c682012-11-21 18:34:04 -08002643 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2644 * case we pass on the fault to the guest kernel and let it handle it.
2645 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2646 * case we inject the TLB from the Guest TLB into the shadow host TLB
2647 */
James Hogan31cf7492016-06-09 14:19:09 +01002648enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002649 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002650 struct kvm_run *run,
James Hogan577ed7f2015-05-01 14:56:31 +01002651 struct kvm_vcpu *vcpu,
2652 bool write_fault)
Sanjay Lale685c682012-11-21 18:34:04 -08002653{
2654 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002655 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002656 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2657 int index;
2658
James Hogane4e94c02016-06-09 14:19:05 +01002659 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2660 vcpu->arch.host_cp0_badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002661
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002662 /*
2663 * KVM would not have got the exception if this entry was valid in the
2664 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2665 * send the guest an exception. The guest exc handler should then inject
2666 * an entry into the guest TLB.
Sanjay Lale685c682012-11-21 18:34:04 -08002667 */
2668 index = kvm_mips_guest_tlb_lookup(vcpu,
James Hogancaa1faa2015-12-16 23:49:26 +00002669 (va & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002670 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2671 KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08002672 if (index < 0) {
James Hogan16d100db2015-12-16 23:49:33 +00002673 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002674 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002675 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002676 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2677 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002678 kvm_err("%s: invalid exc code: %d\n", __func__,
2679 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002680 er = EMULATE_FAIL;
2681 }
2682 } else {
2683 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2684
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002685 /*
2686 * Check if the entry is valid, if not then setup a TLB invalid
2687 * exception to the guest
2688 */
Sanjay Lale685c682012-11-21 18:34:04 -08002689 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan16d100db2015-12-16 23:49:33 +00002690 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002691 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2692 vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002693 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002694 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2695 vcpu);
2696 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002697 kvm_err("%s: invalid exc code: %d\n", __func__,
2698 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002699 er = EMULATE_FAIL;
2700 }
2701 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002702 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
James Hogan9fbfb062016-06-09 14:19:17 +01002703 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002704 /*
2705 * OK we have a Guest TLB entry, now inject it into the
2706 * shadow host TLB
2707 */
James Hogan577ed7f2015-05-01 14:56:31 +01002708 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, va,
2709 write_fault)) {
James Hogan9b731bc2016-08-11 11:58:15 +01002710 kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
2711 __func__, va, index, vcpu,
2712 read_c0_entryhi());
2713 er = EMULATE_FAIL;
2714 }
Sanjay Lale685c682012-11-21 18:34:04 -08002715 }
2716 }
2717
2718 return er;
2719}