blob: 4833ebad89d9b875971c19e47256fb599f605a99 [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 Hoganf4474d52017-03-14 10:15:39 +0000311int 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 Hoganf4474d52017-03-14 10:15:39 +0000470ktime_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 Hoganf4474d52017-03-14 10:15:39 +0000520 * kvm_mips_restore_hrtimer() - Restore hrtimer after a gap, updating expiry.
521 * @vcpu: Virtual CPU.
522 * @before: Time before Count was saved, lower bound of drift calculation.
523 * @count: CP0_Count at point of restore.
524 * @min_drift: Minimum amount of drift permitted before correction.
525 * Must be <= 0.
526 *
527 * Restores the timer from a particular @count, accounting for drift. This can
528 * be used in conjunction with kvm_mips_freeze_timer() when a hardware timer is
529 * to be used for a period of time, but the exact ktime corresponding to the
530 * final Count that must be restored is not known.
531 *
532 * It is gauranteed that a timer interrupt immediately after restore will be
533 * handled, but not if CP0_Compare is exactly at @count. That case should
534 * already be handled when the hardware timer state is saved.
535 *
536 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is not
537 * stopped).
538 *
539 * Returns: Amount of correction to count_bias due to drift.
540 */
541int kvm_mips_restore_hrtimer(struct kvm_vcpu *vcpu, ktime_t before,
542 u32 count, int min_drift)
543{
544 ktime_t now, count_time;
545 u32 now_count, before_count;
546 u64 delta;
547 int drift, ret = 0;
548
549 /* Calculate expected count at before */
550 before_count = vcpu->arch.count_bias +
551 kvm_mips_ktime_to_count(vcpu, before);
552
553 /*
554 * Detect significantly negative drift, where count is lower than
555 * expected. Some negative drift is expected when hardware counter is
556 * set after kvm_mips_freeze_timer(), and it is harmless to allow the
557 * time to jump forwards a little, within reason. If the drift is too
558 * significant, adjust the bias to avoid a big Guest.CP0_Count jump.
559 */
560 drift = count - before_count;
561 if (drift < min_drift) {
562 count_time = before;
563 vcpu->arch.count_bias += drift;
564 ret = drift;
565 goto resume;
566 }
567
568 /* Calculate expected count right now */
569 now = ktime_get();
570 now_count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
571
572 /*
573 * Detect positive drift, where count is higher than expected, and
574 * adjust the bias to avoid guest time going backwards.
575 */
576 drift = count - now_count;
577 if (drift > 0) {
578 count_time = now;
579 vcpu->arch.count_bias += drift;
580 ret = drift;
581 goto resume;
582 }
583
584 /* Subtract nanosecond delta to find ktime when count was read */
585 delta = (u64)(u32)(now_count - count);
586 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
587 count_time = ktime_sub_ns(now, delta);
588
589resume:
590 /* Resume using the calculated ktime */
591 kvm_mips_resume_hrtimer(vcpu, count_time, count);
592 return ret;
593}
594
595/**
James Hogane30492b2014-05-29 10:16:35 +0100596 * kvm_mips_write_count() - Modify the count and update timer.
597 * @vcpu: Virtual CPU.
598 * @count: Guest CP0_Count value to set.
599 *
600 * Sets the CP0_Count value and updates the timer accordingly.
601 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100602void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100603{
604 struct mips_coproc *cop0 = vcpu->arch.cop0;
605 ktime_t now;
606
607 /* Calculate bias */
James Hoganf8239342014-05-29 10:16:37 +0100608 now = kvm_mips_count_time(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100609 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
610
611 if (kvm_mips_count_disabled(vcpu))
612 /* The timer's disabled, adjust the static count */
613 kvm_write_c0_guest_count(cop0, count);
614 else
615 /* Update timeout */
616 kvm_mips_resume_hrtimer(vcpu, now, count);
617}
618
619/**
620 * kvm_mips_init_count() - Initialise timer.
621 * @vcpu: Virtual CPU.
James Hogana517c1a2017-03-14 10:15:21 +0000622 * @count_hz: Frequency of timer.
James Hogane30492b2014-05-29 10:16:35 +0100623 *
James Hogana517c1a2017-03-14 10:15:21 +0000624 * Initialise the timer to the specified frequency, zero it, and set it going if
625 * it's enabled.
James Hogane30492b2014-05-29 10:16:35 +0100626 */
James Hogana517c1a2017-03-14 10:15:21 +0000627void kvm_mips_init_count(struct kvm_vcpu *vcpu, unsigned long count_hz)
James Hogane30492b2014-05-29 10:16:35 +0100628{
James Hogana517c1a2017-03-14 10:15:21 +0000629 vcpu->arch.count_hz = count_hz;
630 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
James Hogane30492b2014-05-29 10:16:35 +0100631 vcpu->arch.count_dyn_bias = 0;
632
633 /* Starting at 0 */
634 kvm_mips_write_count(vcpu, 0);
635}
636
637/**
James Hoganf74a8e22014-05-29 10:16:38 +0100638 * kvm_mips_set_count_hz() - Update the frequency of the timer.
639 * @vcpu: Virtual CPU.
640 * @count_hz: Frequency of CP0_Count timer in Hz.
641 *
642 * Change the frequency of the CP0_Count timer. This is done atomically so that
643 * CP0_Count is continuous and no timer interrupt is lost.
644 *
645 * Returns: -EINVAL if @count_hz is out of range.
646 * 0 on success.
647 */
648int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
649{
650 struct mips_coproc *cop0 = vcpu->arch.cop0;
651 int dc;
652 ktime_t now;
653 u32 count;
654
655 /* ensure the frequency is in a sensible range... */
656 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
657 return -EINVAL;
658 /* ... and has actually changed */
659 if (vcpu->arch.count_hz == count_hz)
660 return 0;
661
662 /* Safely freeze timer so we can keep it continuous */
663 dc = kvm_mips_count_disabled(vcpu);
664 if (dc) {
665 now = kvm_mips_count_time(vcpu);
666 count = kvm_read_c0_guest_count(cop0);
667 } else {
668 now = kvm_mips_freeze_hrtimer(vcpu, &count);
669 }
670
671 /* Update the frequency */
672 vcpu->arch.count_hz = count_hz;
673 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
674 vcpu->arch.count_dyn_bias = 0;
675
676 /* Calculate adjusted bias so dynamic count is unchanged */
677 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
678
679 /* Update and resume hrtimer */
680 if (!dc)
681 kvm_mips_resume_hrtimer(vcpu, now, count);
682 return 0;
683}
684
685/**
James Hogane30492b2014-05-29 10:16:35 +0100686 * kvm_mips_write_compare() - Modify compare and update timer.
687 * @vcpu: Virtual CPU.
688 * @compare: New CP0_Compare value.
James Hoganb45bacd2016-04-22 10:38:46 +0100689 * @ack: Whether to acknowledge timer interrupt.
James Hogane30492b2014-05-29 10:16:35 +0100690 *
691 * Update CP0_Compare to a new value and update the timeout.
James Hoganb45bacd2016-04-22 10:38:46 +0100692 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
693 * any pending timer interrupt is preserved.
James Hogane30492b2014-05-29 10:16:35 +0100694 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100695void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
James Hogane30492b2014-05-29 10:16:35 +0100696{
697 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganb45bacd2016-04-22 10:38:46 +0100698 int dc;
699 u32 old_compare = kvm_read_c0_guest_compare(cop0);
James Hogan5dee99b2017-03-14 10:15:28 +0000700 s32 delta = compare - old_compare;
701 u32 cause;
702 ktime_t now = ktime_set(0, 0); /* silence bogus GCC warning */
James Hogan8cffd192016-06-09 14:19:08 +0100703 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100704
705 /* if unchanged, must just be an ack */
James Hoganb45bacd2016-04-22 10:38:46 +0100706 if (old_compare == compare) {
707 if (!ack)
708 return;
709 kvm_mips_callbacks->dequeue_timer_int(vcpu);
710 kvm_write_c0_guest_compare(cop0, compare);
James Hogane30492b2014-05-29 10:16:35 +0100711 return;
James Hoganb45bacd2016-04-22 10:38:46 +0100712 }
James Hogane30492b2014-05-29 10:16:35 +0100713
James Hogan5dee99b2017-03-14 10:15:28 +0000714 /*
715 * If guest CP0_Compare moves forward, CP0_GTOffset should be adjusted
716 * too to prevent guest CP0_Count hitting guest CP0_Compare.
717 *
718 * The new GTOffset corresponds to the new value of CP0_Compare, and is
719 * set prior to it being written into the guest context. We disable
720 * preemption until the new value is written to prevent restore of a
721 * GTOffset corresponding to the old CP0_Compare value.
722 */
723 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta > 0) {
724 preempt_disable();
725 write_c0_gtoffset(compare - read_c0_count());
726 back_to_back_c0_hazard();
727 }
728
James Hoganb45bacd2016-04-22 10:38:46 +0100729 /* freeze_hrtimer() takes care of timer interrupts <= count */
730 dc = kvm_mips_count_disabled(vcpu);
731 if (!dc)
732 now = kvm_mips_freeze_hrtimer(vcpu, &count);
733
734 if (ack)
735 kvm_mips_callbacks->dequeue_timer_int(vcpu);
James Hogan5dee99b2017-03-14 10:15:28 +0000736 else if (IS_ENABLED(CONFIG_KVM_MIPS_VZ))
737 /*
738 * With VZ, writing CP0_Compare acks (clears) CP0_Cause.TI, so
739 * preserve guest CP0_Cause.TI if we don't want to ack it.
740 */
741 cause = kvm_read_c0_guest_cause(cop0);
James Hoganb45bacd2016-04-22 10:38:46 +0100742
James Hogane30492b2014-05-29 10:16:35 +0100743 kvm_write_c0_guest_compare(cop0, compare);
744
James Hogan5dee99b2017-03-14 10:15:28 +0000745 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
746 if (delta > 0)
747 preempt_enable();
748
749 back_to_back_c0_hazard();
750
751 if (!ack && cause & CAUSEF_TI)
752 kvm_write_c0_guest_cause(cop0, cause);
753 }
754
James Hoganb45bacd2016-04-22 10:38:46 +0100755 /* resume_hrtimer() takes care of timer interrupts > count */
756 if (!dc)
757 kvm_mips_resume_hrtimer(vcpu, now, count);
James Hogan5dee99b2017-03-14 10:15:28 +0000758
759 /*
760 * If guest CP0_Compare is moving backward, we delay CP0_GTOffset change
761 * until after the new CP0_Compare is written, otherwise new guest
762 * CP0_Count could hit new guest CP0_Compare.
763 */
764 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta <= 0)
765 write_c0_gtoffset(compare - read_c0_count());
James Hogane30492b2014-05-29 10:16:35 +0100766}
767
768/**
769 * kvm_mips_count_disable() - Disable count.
770 * @vcpu: Virtual CPU.
771 *
772 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
773 * time will be handled but not after.
774 *
James Hoganf8239342014-05-29 10:16:37 +0100775 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
776 * count_ctl.DC has been set (count disabled).
James Hogane30492b2014-05-29 10:16:35 +0100777 *
778 * Returns: The time that the timer was stopped.
779 */
780static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
781{
782 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100783 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100784 ktime_t now;
785
786 /* Stop hrtimer */
787 hrtimer_cancel(&vcpu->arch.comparecount_timer);
788
789 /* Set the static count from the dynamic count, handling pending TI */
790 now = ktime_get();
791 count = kvm_mips_read_count_running(vcpu, now);
792 kvm_write_c0_guest_count(cop0, count);
793
794 return now;
795}
796
797/**
798 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
799 * @vcpu: Virtual CPU.
800 *
801 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
James Hoganf8239342014-05-29 10:16:37 +0100802 * before the final stop time will be handled if the timer isn't disabled by
803 * count_ctl.DC, but not after.
James Hogane30492b2014-05-29 10:16:35 +0100804 *
805 * Assumes CP0_Cause.DC is clear (count enabled).
806 */
807void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
808{
809 struct mips_coproc *cop0 = vcpu->arch.cop0;
810
811 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
James Hoganf8239342014-05-29 10:16:37 +0100812 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
813 kvm_mips_count_disable(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100814}
815
816/**
817 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
818 * @vcpu: Virtual CPU.
819 *
820 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
James Hoganf8239342014-05-29 10:16:37 +0100821 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
822 * potentially before even returning, so the caller should be careful with
823 * ordering of CP0_Cause modifications so as not to lose it.
James Hogane30492b2014-05-29 10:16:35 +0100824 *
825 * Assumes CP0_Cause.DC is set (count disabled).
826 */
827void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
828{
829 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100830 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100831
832 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
833
834 /*
835 * Set the dynamic count to match the static count.
James Hoganf8239342014-05-29 10:16:37 +0100836 * This starts the hrtimer if count_ctl.DC allows it.
837 * Otherwise it conveniently updates the biases.
James Hogane30492b2014-05-29 10:16:35 +0100838 */
839 count = kvm_read_c0_guest_count(cop0);
840 kvm_mips_write_count(vcpu, count);
841}
842
843/**
James Hoganf8239342014-05-29 10:16:37 +0100844 * kvm_mips_set_count_ctl() - Update the count control KVM register.
845 * @vcpu: Virtual CPU.
846 * @count_ctl: Count control register new value.
847 *
848 * Set the count control KVM register. The timer is updated accordingly.
849 *
850 * Returns: -EINVAL if reserved bits are set.
851 * 0 on success.
852 */
853int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
854{
855 struct mips_coproc *cop0 = vcpu->arch.cop0;
856 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
857 s64 delta;
858 ktime_t expire, now;
James Hogan8cffd192016-06-09 14:19:08 +0100859 u32 count, compare;
James Hoganf8239342014-05-29 10:16:37 +0100860
861 /* Only allow defined bits to be changed */
862 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
863 return -EINVAL;
864
865 /* Apply new value */
866 vcpu->arch.count_ctl = count_ctl;
867
868 /* Master CP0_Count disable */
869 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
870 /* Is CP0_Cause.DC already disabling CP0_Count? */
871 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
872 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
873 /* Just record the current time */
874 vcpu->arch.count_resume = ktime_get();
875 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
876 /* disable timer and record current time */
877 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
878 } else {
879 /*
880 * Calculate timeout relative to static count at resume
881 * time (wrap 0 to 2^32).
882 */
883 count = kvm_read_c0_guest_count(cop0);
884 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100885 delta = (u64)(u32)(compare - count - 1) + 1;
James Hoganf8239342014-05-29 10:16:37 +0100886 delta = div_u64(delta * NSEC_PER_SEC,
887 vcpu->arch.count_hz);
888 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
889
890 /* Handle pending interrupt */
891 now = ktime_get();
892 if (ktime_compare(now, expire) >= 0)
893 /* Nothing should be waiting on the timeout */
894 kvm_mips_callbacks->queue_timer_int(vcpu);
895
896 /* Resume hrtimer without changing bias */
897 count = kvm_mips_read_count_running(vcpu, now);
898 kvm_mips_resume_hrtimer(vcpu, now, count);
899 }
900 }
901
902 return 0;
903}
904
905/**
906 * kvm_mips_set_count_resume() - Update the count resume KVM register.
907 * @vcpu: Virtual CPU.
908 * @count_resume: Count resume register new value.
909 *
910 * Set the count resume KVM register.
911 *
912 * Returns: -EINVAL if out of valid range (0..now).
913 * 0 on success.
914 */
915int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
916{
917 /*
918 * It doesn't make sense for the resume time to be in the future, as it
919 * would be possible for the next interrupt to be more than a full
920 * period in the future.
921 */
922 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
923 return -EINVAL;
924
925 vcpu->arch.count_resume = ns_to_ktime(count_resume);
926 return 0;
927}
928
929/**
James Hogane30492b2014-05-29 10:16:35 +0100930 * kvm_mips_count_timeout() - Push timer forward on timeout.
931 * @vcpu: Virtual CPU.
932 *
933 * Handle an hrtimer event by push the hrtimer forward a period.
934 *
935 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
936 */
937enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
938{
939 /* Add the Count period to the current expiry time */
940 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
941 vcpu->arch.count_period);
942 return HRTIMER_RESTART;
Sanjay Lale685c682012-11-21 18:34:04 -0800943}
944
945enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
946{
947 struct mips_coproc *cop0 = vcpu->arch.cop0;
948 enum emulation_result er = EMULATE_DONE;
949
James Hoganede5f3e2016-10-25 16:11:11 +0100950 if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
951 kvm_clear_c0_guest_status(cop0, ST0_ERL);
952 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
953 } else if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
Sanjay Lale685c682012-11-21 18:34:04 -0800954 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
955 kvm_read_c0_guest_epc(cop0));
956 kvm_clear_c0_guest_status(cop0, ST0_EXL);
957 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
958
Sanjay Lale685c682012-11-21 18:34:04 -0800959 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700960 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
961 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800962 er = EMULATE_FAIL;
963 }
964
965 return er;
966}
967
968enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
969{
Sanjay Lale685c682012-11-21 18:34:04 -0800970 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
971 vcpu->arch.pending_exceptions);
972
973 ++vcpu->stat.wait_exits;
James Hogan1e09e862016-06-14 09:40:12 +0100974 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
Sanjay Lale685c682012-11-21 18:34:04 -0800975 if (!vcpu->arch.pending_exceptions) {
James Hoganf4474d52017-03-14 10:15:39 +0000976 kvm_vz_lose_htimer(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -0800977 vcpu->arch.wait = 1;
978 kvm_vcpu_block(vcpu);
979
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700980 /*
981 * We we are runnable, then definitely go off to user space to
982 * check if any I/O interrupts are pending.
Sanjay Lale685c682012-11-21 18:34:04 -0800983 */
984 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
985 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
986 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
987 }
988 }
989
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700990 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800991}
992
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700993/*
994 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
995 * we can catch this, if things ever change
Sanjay Lale685c682012-11-21 18:34:04 -0800996 */
997enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
998{
999 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +01001000 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001001
James Hogana27660f2017-03-14 10:15:25 +00001002 kvm_err("[%#lx] COP0_TLBR [%d]\n", pc, kvm_read_c0_guest_index(cop0));
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001003 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001004}
1005
James Hogan91e4f1b2016-09-15 17:20:06 +01001006/**
1007 * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map.
1008 * @vcpu: VCPU with changed mappings.
1009 * @tlb: TLB entry being removed.
1010 *
1011 * This is called to indicate a single change in guest MMU mappings, so that we
1012 * can arrange TLB flushes on this and other CPUs.
1013 */
1014static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu,
1015 struct kvm_mips_tlb *tlb)
1016{
James Hoganc550d532016-10-11 23:14:39 +01001017 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
1018 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
James Hogan91e4f1b2016-09-15 17:20:06 +01001019 int cpu, i;
1020 bool user;
1021
1022 /* No need to flush for entries which are already invalid */
1023 if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V))
1024 return;
James Hoganaba85922016-12-16 15:57:00 +00001025 /* Don't touch host kernel page tables or TLB mappings */
1026 if ((unsigned long)tlb->tlb_hi > 0x7fffffff)
1027 return;
James Hogan91e4f1b2016-09-15 17:20:06 +01001028 /* User address space doesn't need flushing for KSeg2/3 changes */
1029 user = tlb->tlb_hi < KVM_GUEST_KSEG0;
1030
1031 preempt_disable();
1032
James Hoganaba85922016-12-16 15:57:00 +00001033 /* Invalidate page table entries */
1034 kvm_trap_emul_invalidate_gva(vcpu, tlb->tlb_hi & VPN2_MASK, user);
1035
James Hogan91e4f1b2016-09-15 17:20:06 +01001036 /*
1037 * Probe the shadow host TLB for the entry being overwritten, if one
1038 * matches, invalidate it
1039 */
James Hogan57e38692016-10-08 00:15:52 +01001040 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi, user, true);
James Hogan91e4f1b2016-09-15 17:20:06 +01001041
1042 /* Invalidate the whole ASID on other CPUs */
1043 cpu = smp_processor_id();
1044 for_each_possible_cpu(i) {
1045 if (i == cpu)
1046 continue;
1047 if (user)
James Hoganc550d532016-10-11 23:14:39 +01001048 cpu_context(i, user_mm) = 0;
1049 cpu_context(i, kern_mm) = 0;
James Hogan91e4f1b2016-09-15 17:20:06 +01001050 }
1051
1052 preempt_enable();
1053}
1054
Sanjay Lale685c682012-11-21 18:34:04 -08001055/* Write Guest TLB Entry @ Index */
1056enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
1057{
1058 struct mips_coproc *cop0 = vcpu->arch.cop0;
1059 int index = kvm_read_c0_guest_index(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001060 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +01001061 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001062
1063 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001064 kvm_debug("%s: illegal index: %d\n", __func__, index);
James Hogan8cffd192016-06-09 14:19:08 +01001065 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001066 pc, index, kvm_read_c0_guest_entryhi(cop0),
1067 kvm_read_c0_guest_entrylo0(cop0),
1068 kvm_read_c0_guest_entrylo1(cop0),
1069 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001070 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
1071 }
1072
1073 tlb = &vcpu->arch.guest_tlb[index];
James Hogan91e4f1b2016-09-15 17:20:06 +01001074
1075 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -08001076
1077 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
1078 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +01001079 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
1080 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001081
James Hogan8cffd192016-06-09 14:19:08 +01001082 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001083 pc, index, kvm_read_c0_guest_entryhi(cop0),
1084 kvm_read_c0_guest_entrylo0(cop0),
1085 kvm_read_c0_guest_entrylo1(cop0),
1086 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001087
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001088 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001089}
1090
1091/* Write Guest TLB Entry @ Random Index */
1092enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
1093{
1094 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -08001095 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +01001096 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001097 int index;
1098
Sanjay Lale685c682012-11-21 18:34:04 -08001099 get_random_bytes(&index, sizeof(index));
1100 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
Sanjay Lale685c682012-11-21 18:34:04 -08001101
Sanjay Lale685c682012-11-21 18:34:04 -08001102 tlb = &vcpu->arch.guest_tlb[index];
1103
James Hogan91e4f1b2016-09-15 17:20:06 +01001104 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -08001105
1106 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
1107 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +01001108 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
1109 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001110
James Hogan8cffd192016-06-09 14:19:08 +01001111 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001112 pc, index, kvm_read_c0_guest_entryhi(cop0),
1113 kvm_read_c0_guest_entrylo0(cop0),
1114 kvm_read_c0_guest_entrylo1(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001115
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001116 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001117}
1118
1119enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
1120{
1121 struct mips_coproc *cop0 = vcpu->arch.cop0;
1122 long entryhi = kvm_read_c0_guest_entryhi(cop0);
James Hogan8cffd192016-06-09 14:19:08 +01001123 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001124 int index = -1;
1125
1126 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
1127
1128 kvm_write_c0_guest_index(cop0, index);
1129
James Hogan8cffd192016-06-09 14:19:08 +01001130 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
Sanjay Lale685c682012-11-21 18:34:04 -08001131 index);
1132
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001133 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001134}
1135
James Hoganc7716072014-06-26 15:11:29 +01001136/**
1137 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
1138 * @vcpu: Virtual CPU.
1139 *
1140 * Finds the mask of bits which are writable in the guest's Config1 CP0
1141 * register, by userland (currently read-only to the guest).
1142 */
1143unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
1144{
James Hogan6cdc65e2015-02-03 13:59:38 +00001145 unsigned int mask = 0;
1146
1147 /* Permit FPU to be present if FPU is supported */
1148 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
1149 mask |= MIPS_CONF1_FP;
1150
1151 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001152}
1153
1154/**
1155 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
1156 * @vcpu: Virtual CPU.
1157 *
1158 * Finds the mask of bits which are writable in the guest's Config3 CP0
1159 * register, by userland (currently read-only to the guest).
1160 */
1161unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
1162{
James Hogancef061d02016-06-15 19:29:54 +01001163 /* Config4 and ULRI are optional */
1164 unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
James Hogan2b6009d2015-02-06 23:01:00 +00001165
1166 /* Permit MSA to be present if MSA is supported */
1167 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
1168 mask |= MIPS_CONF3_MSA;
1169
1170 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001171}
1172
1173/**
1174 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
1175 * @vcpu: Virtual CPU.
1176 *
1177 * Finds the mask of bits which are writable in the guest's Config4 CP0
1178 * register, by userland (currently read-only to the guest).
1179 */
1180unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
1181{
1182 /* Config5 is optional */
James Hogan05108702016-06-15 19:29:56 +01001183 unsigned int mask = MIPS_CONF_M;
1184
1185 /* KScrExist */
James Hogan654229a2016-12-08 22:46:41 +00001186 mask |= 0xfc << MIPS_CONF4_KSCREXIST_SHIFT;
James Hogan05108702016-06-15 19:29:56 +01001187
1188 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001189}
1190
1191/**
1192 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
1193 * @vcpu: Virtual CPU.
1194 *
1195 * Finds the mask of bits which are writable in the guest's Config5 CP0
1196 * register, by the guest itself.
1197 */
1198unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
1199{
James Hogan6cdc65e2015-02-03 13:59:38 +00001200 unsigned int mask = 0;
1201
James Hogan2b6009d2015-02-06 23:01:00 +00001202 /* Permit MSAEn changes if MSA supported and enabled */
1203 if (kvm_mips_guest_has_msa(&vcpu->arch))
1204 mask |= MIPS_CONF5_MSAEN;
1205
James Hogan6cdc65e2015-02-03 13:59:38 +00001206 /*
1207 * Permit guest FPU mode changes if FPU is enabled and the relevant
1208 * feature exists according to FIR register.
1209 */
1210 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1211 if (cpu_has_fre)
1212 mask |= MIPS_CONF5_FRE;
1213 /* We don't support UFR or UFE */
1214 }
1215
1216 return mask;
James Hoganc7716072014-06-26 15:11:29 +01001217}
1218
James Hogan258f3a22016-06-15 19:29:47 +01001219enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
1220 u32 *opc, u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001221 struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001222 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001223{
1224 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganc550d532016-10-11 23:14:39 +01001225 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
Sanjay Lale685c682012-11-21 18:34:04 -08001226 enum emulation_result er = EMULATE_DONE;
James Hogan258f3a22016-06-15 19:29:47 +01001227 u32 rt, rd, sel;
Sanjay Lale685c682012-11-21 18:34:04 -08001228 unsigned long curr_pc;
James Hogan91e4f1b2016-09-15 17:20:06 +01001229 int cpu, i;
Sanjay Lale685c682012-11-21 18:34:04 -08001230
1231 /*
1232 * Update PC and hold onto current PC in case there is
1233 * an error and we want to rollback the PC
1234 */
1235 curr_pc = vcpu->arch.pc;
1236 er = update_pc(vcpu, cause);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001237 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001238 return er;
Sanjay Lale685c682012-11-21 18:34:04 -08001239
James Hogan258f3a22016-06-15 19:29:47 +01001240 if (inst.co_format.co) {
1241 switch (inst.co_format.func) {
Sanjay Lale685c682012-11-21 18:34:04 -08001242 case tlbr_op: /* Read indexed TLB entry */
1243 er = kvm_mips_emul_tlbr(vcpu);
1244 break;
1245 case tlbwi_op: /* Write indexed */
1246 er = kvm_mips_emul_tlbwi(vcpu);
1247 break;
1248 case tlbwr_op: /* Write random */
1249 er = kvm_mips_emul_tlbwr(vcpu);
1250 break;
1251 case tlbp_op: /* TLB Probe */
1252 er = kvm_mips_emul_tlbp(vcpu);
1253 break;
1254 case rfe_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001255 kvm_err("!!!COP0_RFE!!!\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001256 break;
1257 case eret_op:
1258 er = kvm_mips_emul_eret(vcpu);
1259 goto dont_update_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001260 case wait_op:
1261 er = kvm_mips_emul_wait(vcpu);
1262 break;
James Hogan955d8dc2017-03-14 10:15:14 +00001263 case hypcall_op:
1264 er = kvm_mips_emul_hypcall(vcpu, inst);
1265 break;
Sanjay Lale685c682012-11-21 18:34:04 -08001266 }
1267 } else {
James Hogan258f3a22016-06-15 19:29:47 +01001268 rt = inst.c0r_format.rt;
1269 rd = inst.c0r_format.rd;
1270 sel = inst.c0r_format.sel;
1271
1272 switch (inst.c0r_format.rs) {
Sanjay Lale685c682012-11-21 18:34:04 -08001273 case mfc_op:
1274#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1275 cop0->stat[rd][sel]++;
1276#endif
1277 /* Get reg */
1278 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogan172e02d2016-07-08 11:53:28 +01001279 vcpu->arch.gprs[rt] =
1280 (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001281 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1282 vcpu->arch.gprs[rt] = 0x0;
1283#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1284 kvm_mips_trans_mfc0(inst, opc, vcpu);
1285#endif
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001286 } else {
James Hogan172e02d2016-07-08 11:53:28 +01001287 vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
Sanjay Lale685c682012-11-21 18:34:04 -08001288
1289#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1290 kvm_mips_trans_mfc0(inst, opc, vcpu);
1291#endif
1292 }
1293
James Hogan6398da12016-06-14 09:40:15 +01001294 trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
1295 KVM_TRACE_COP0(rd, sel),
1296 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001297 break;
1298
1299 case dmfc_op:
1300 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
James Hogan6398da12016-06-14 09:40:15 +01001301
1302 trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
1303 KVM_TRACE_COP0(rd, sel),
1304 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001305 break;
1306
1307 case mtc_op:
1308#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1309 cop0->stat[rd][sel]++;
1310#endif
James Hogan6398da12016-06-14 09:40:15 +01001311 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
1312 KVM_TRACE_COP0(rd, sel),
1313 vcpu->arch.gprs[rt]);
1314
Sanjay Lale685c682012-11-21 18:34:04 -08001315 if ((rd == MIPS_CP0_TLB_INDEX)
1316 && (vcpu->arch.gprs[rt] >=
1317 KVM_MIPS_GUEST_TLB_SIZE)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001318 kvm_err("Invalid TLB Index: %ld",
1319 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001320 er = EMULATE_FAIL;
1321 break;
1322 }
Sanjay Lale685c682012-11-21 18:34:04 -08001323 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
James Hogan7801bbe2016-11-14 23:59:27 +00001324 /*
1325 * Preserve core number, and keep the exception
1326 * base in guest KSeg0.
1327 */
1328 kvm_change_c0_guest_ebase(cop0, 0x1ffff000,
Sanjay Lale685c682012-11-21 18:34:04 -08001329 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001330 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
James Hogan8cffd192016-06-09 14:19:08 +01001331 u32 nasid =
Paul Burtonca64c2b2016-05-06 14:36:20 +01001332 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
James Hoganbf18db42016-09-16 13:14:09 +01001333 if (((kvm_read_c0_guest_entryhi(cop0) &
Paul Burtonca64c2b2016-05-06 14:36:20 +01001334 KVM_ENTRYHI_ASID) != nasid)) {
James Hogan9887d1c2016-06-14 09:40:13 +01001335 trace_kvm_asid_change(vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001336 kvm_read_c0_guest_entryhi(cop0)
James Hogan9887d1c2016-06-14 09:40:13 +01001337 & KVM_ENTRYHI_ASID,
1338 nasid);
Sanjay Lale685c682012-11-21 18:34:04 -08001339
James Hogan25b08c72016-09-16 00:06:43 +01001340 /*
James Hogana31b50d2016-12-16 15:57:00 +00001341 * Flush entries from the GVA page
1342 * tables.
1343 * Guest user page table will get
1344 * flushed lazily on re-entry to guest
1345 * user if the guest ASID actually
1346 * changes.
1347 */
1348 kvm_mips_flush_gva_pt(kern_mm->pgd,
1349 KMF_KERN);
1350
1351 /*
James Hogan25b08c72016-09-16 00:06:43 +01001352 * Regenerate/invalidate kernel MMU
1353 * context.
1354 * The user MMU context will be
1355 * regenerated lazily on re-entry to
1356 * guest user if the guest ASID actually
1357 * changes.
1358 */
James Hogan91e4f1b2016-09-15 17:20:06 +01001359 preempt_disable();
James Hogan91e4f1b2016-09-15 17:20:06 +01001360 cpu = smp_processor_id();
James Hogana98dd742016-10-07 22:39:41 +01001361 get_new_mmu_context(kern_mm, cpu);
James Hogan91e4f1b2016-09-15 17:20:06 +01001362 for_each_possible_cpu(i)
James Hogan25b08c72016-09-16 00:06:43 +01001363 if (i != cpu)
James Hoganc550d532016-10-11 23:14:39 +01001364 cpu_context(i, kern_mm) = 0;
James Hogan91e4f1b2016-09-15 17:20:06 +01001365 preempt_enable();
Sanjay Lale685c682012-11-21 18:34:04 -08001366 }
1367 kvm_write_c0_guest_entryhi(cop0,
1368 vcpu->arch.gprs[rt]);
1369 }
1370 /* Are we writing to COUNT */
1371 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001372 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001373 goto done;
1374 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
Sanjay Lale685c682012-11-21 18:34:04 -08001375 /* If we are writing to COMPARE */
1376 /* Clear pending timer interrupt, if any */
James Hogane30492b2014-05-29 10:16:35 +01001377 kvm_mips_write_compare(vcpu,
James Hoganb45bacd2016-04-22 10:38:46 +01001378 vcpu->arch.gprs[rt],
1379 true);
Sanjay Lale685c682012-11-21 18:34:04 -08001380 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
James Hogan6cdc65e2015-02-03 13:59:38 +00001381 unsigned int old_val, val, change;
1382
1383 old_val = kvm_read_c0_guest_status(cop0);
1384 val = vcpu->arch.gprs[rt];
1385 change = val ^ old_val;
1386
1387 /* Make sure that the NMI bit is never set */
1388 val &= ~ST0_NMI;
1389
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001390 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001391 * Don't allow CU1 or FR to be set unless FPU
1392 * capability enabled and exists in guest
1393 * configuration.
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001394 */
James Hogan6cdc65e2015-02-03 13:59:38 +00001395 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1396 val &= ~(ST0_CU1 | ST0_FR);
1397
1398 /*
1399 * Also don't allow FR to be set if host doesn't
1400 * support it.
1401 */
1402 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1403 val &= ~ST0_FR;
1404
1405
1406 /* Handle changes in FPU mode */
1407 preempt_disable();
1408
1409 /*
1410 * FPU and Vector register state is made
1411 * UNPREDICTABLE by a change of FR, so don't
1412 * even bother saving it.
1413 */
1414 if (change & ST0_FR)
1415 kvm_drop_fpu(vcpu);
1416
1417 /*
James Hogan2b6009d2015-02-06 23:01:00 +00001418 * If MSA state is already live, it is undefined
1419 * how it interacts with FR=0 FPU state, and we
1420 * don't want to hit reserved instruction
1421 * exceptions trying to save the MSA state later
1422 * when CU=1 && FR=1, so play it safe and save
1423 * it first.
1424 */
1425 if (change & ST0_CU1 && !(val & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001426 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001427 kvm_lose_fpu(vcpu);
1428
1429 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001430 * Propagate CU1 (FPU enable) changes
1431 * immediately if the FPU context is already
1432 * loaded. When disabling we leave the context
1433 * loaded so it can be quickly enabled again in
1434 * the near future.
1435 */
1436 if (change & ST0_CU1 &&
James Hoganf9431762016-06-14 09:40:10 +01001437 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001438 change_c0_status(ST0_CU1, val);
1439
1440 preempt_enable();
1441
1442 kvm_write_c0_guest_status(cop0, val);
Sanjay Lale685c682012-11-21 18:34:04 -08001443
1444#ifdef CONFIG_KVM_MIPS_DYN_TRANS
James Hogan6cdc65e2015-02-03 13:59:38 +00001445 /*
1446 * If FPU present, we need CU1/FR bits to take
1447 * effect fairly soon.
1448 */
1449 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1450 kvm_mips_trans_mtc0(inst, opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001451#endif
James Hogan6cdc65e2015-02-03 13:59:38 +00001452 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1453 unsigned int old_val, val, change, wrmask;
1454
1455 old_val = kvm_read_c0_guest_config5(cop0);
1456 val = vcpu->arch.gprs[rt];
1457
1458 /* Only a few bits are writable in Config5 */
1459 wrmask = kvm_mips_config5_wrmask(vcpu);
1460 change = (val ^ old_val) & wrmask;
1461 val = old_val ^ change;
1462
1463
James Hogan2b6009d2015-02-06 23:01:00 +00001464 /* Handle changes in FPU/MSA modes */
James Hogan6cdc65e2015-02-03 13:59:38 +00001465 preempt_disable();
1466
1467 /*
1468 * Propagate FRE changes immediately if the FPU
1469 * context is already loaded.
1470 */
1471 if (change & MIPS_CONF5_FRE &&
James Hoganf9431762016-06-14 09:40:10 +01001472 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001473 change_c0_config5(MIPS_CONF5_FRE, val);
1474
James Hogan2b6009d2015-02-06 23:01:00 +00001475 /*
1476 * Propagate MSAEn changes immediately if the
1477 * MSA context is already loaded. When disabling
1478 * we leave the context loaded so it can be
1479 * quickly enabled again in the near future.
1480 */
1481 if (change & MIPS_CONF5_MSAEN &&
James Hoganf9431762016-06-14 09:40:10 +01001482 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001483 change_c0_config5(MIPS_CONF5_MSAEN,
1484 val);
1485
James Hogan6cdc65e2015-02-03 13:59:38 +00001486 preempt_enable();
1487
1488 kvm_write_c0_guest_config5(cop0, val);
James Hogane30492b2014-05-29 10:16:35 +01001489 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
James Hogan8cffd192016-06-09 14:19:08 +01001490 u32 old_cause, new_cause;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001491
James Hogane30492b2014-05-29 10:16:35 +01001492 old_cause = kvm_read_c0_guest_cause(cop0);
1493 new_cause = vcpu->arch.gprs[rt];
1494 /* Update R/W bits */
1495 kvm_change_c0_guest_cause(cop0, 0x08800300,
1496 new_cause);
1497 /* DC bit enabling/disabling timer? */
1498 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1499 if (new_cause & CAUSEF_DC)
1500 kvm_mips_count_disable_cause(vcpu);
1501 else
1502 kvm_mips_count_enable_cause(vcpu);
1503 }
James Hogancef061d02016-06-15 19:29:54 +01001504 } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
1505 u32 mask = MIPS_HWRENA_CPUNUM |
1506 MIPS_HWRENA_SYNCISTEP |
1507 MIPS_HWRENA_CC |
1508 MIPS_HWRENA_CCRES;
1509
1510 if (kvm_read_c0_guest_config3(cop0) &
1511 MIPS_CONF3_ULRI)
1512 mask |= MIPS_HWRENA_ULR;
1513 cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
Sanjay Lale685c682012-11-21 18:34:04 -08001514 } else {
1515 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1516#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1517 kvm_mips_trans_mtc0(inst, opc, vcpu);
1518#endif
1519 }
Sanjay Lale685c682012-11-21 18:34:04 -08001520 break;
1521
1522 case dmtc_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001523 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1524 vcpu->arch.pc, rt, rd, sel);
James Hogan6398da12016-06-14 09:40:15 +01001525 trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
1526 KVM_TRACE_COP0(rd, sel),
1527 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001528 er = EMULATE_FAIL;
1529 break;
1530
James Hoganb2c59632015-12-16 23:49:38 +00001531 case mfmc0_op:
Sanjay Lale685c682012-11-21 18:34:04 -08001532#ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1533 cop0->stat[MIPS_CP0_STATUS][0]++;
1534#endif
James Hogancaa1faa2015-12-16 23:49:26 +00001535 if (rt != 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001536 vcpu->arch.gprs[rt] =
1537 kvm_read_c0_guest_status(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001538 /* EI */
James Hogan258f3a22016-06-15 19:29:47 +01001539 if (inst.mfmc0_format.sc) {
James Hoganb2c59632015-12-16 23:49:38 +00001540 kvm_debug("[%#lx] mfmc0_op: EI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001541 vcpu->arch.pc);
1542 kvm_set_c0_guest_status(cop0, ST0_IE);
1543 } else {
James Hoganb2c59632015-12-16 23:49:38 +00001544 kvm_debug("[%#lx] mfmc0_op: DI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001545 vcpu->arch.pc);
1546 kvm_clear_c0_guest_status(cop0, ST0_IE);
1547 }
1548
1549 break;
1550
1551 case wrpgpr_op:
1552 {
James Hogan8cffd192016-06-09 14:19:08 +01001553 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1554 u32 pss =
Sanjay Lale685c682012-11-21 18:34:04 -08001555 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001556 /*
1557 * We don't support any shadow register sets, so
1558 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1559 */
Sanjay Lale685c682012-11-21 18:34:04 -08001560 if (css || pss) {
1561 er = EMULATE_FAIL;
1562 break;
1563 }
1564 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1565 vcpu->arch.gprs[rt]);
1566 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1567 }
1568 break;
1569 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001570 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
James Hogan258f3a22016-06-15 19:29:47 +01001571 vcpu->arch.pc, inst.c0r_format.rs);
Sanjay Lale685c682012-11-21 18:34:04 -08001572 er = EMULATE_FAIL;
1573 break;
1574 }
1575 }
1576
1577done:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001578 /* Rollback PC only if emulation was unsuccessful */
1579 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001580 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001581
1582dont_update_pc:
1583 /*
1584 * This is for special instructions whose emulation
1585 * updates the PC, so do not overwrite the PC under
1586 * any circumstances
1587 */
1588
1589 return er;
1590}
1591
James Hogan258f3a22016-06-15 19:29:47 +01001592enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
1593 u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001594 struct kvm_run *run,
1595 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001596{
James Hogan8b48d5b2017-03-14 10:15:15 +00001597 enum emulation_result er;
James Hogan258f3a22016-06-15 19:29:47 +01001598 u32 rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001599 void *data = run->mmio.data;
1600 unsigned long curr_pc;
1601
1602 /*
1603 * Update PC and hold onto current PC in case there is
1604 * an error and we want to rollback the PC
1605 */
1606 curr_pc = vcpu->arch.pc;
1607 er = update_pc(vcpu, cause);
1608 if (er == EMULATE_FAIL)
1609 return er;
1610
James Hogan258f3a22016-06-15 19:29:47 +01001611 rt = inst.i_format.rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001612
James Hogan8b48d5b2017-03-14 10:15:15 +00001613 run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
1614 vcpu->arch.host_cp0_badvaddr);
1615 if (run->mmio.phys_addr == KVM_INVALID_ADDR)
1616 goto out_fail;
1617
James Hogan258f3a22016-06-15 19:29:47 +01001618 switch (inst.i_format.opcode) {
James Hogan59d78142017-03-14 10:15:16 +00001619#if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
1620 case sd_op:
1621 run->mmio.len = 8;
1622 *(u64 *)data = vcpu->arch.gprs[rt];
1623
1624 kvm_debug("[%#lx] OP_SD: eaddr: %#lx, gpr: %#lx, data: %#llx\n",
1625 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1626 vcpu->arch.gprs[rt], *(u64 *)data);
1627 break;
1628#endif
1629
Sanjay Lale685c682012-11-21 18:34:04 -08001630 case sw_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001631 run->mmio.len = 4;
1632 *(u32 *)data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001633
1634 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1635 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8b48d5b2017-03-14 10:15:15 +00001636 vcpu->arch.gprs[rt], *(u32 *)data);
Sanjay Lale685c682012-11-21 18:34:04 -08001637 break;
1638
1639 case sh_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001640 run->mmio.len = 2;
1641 *(u16 *)data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001642
1643 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1644 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8b48d5b2017-03-14 10:15:15 +00001645 vcpu->arch.gprs[rt], *(u16 *)data);
1646 break;
1647
1648 case sb_op:
1649 run->mmio.len = 1;
1650 *(u8 *)data = vcpu->arch.gprs[rt];
1651
1652 kvm_debug("[%#lx] OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1653 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1654 vcpu->arch.gprs[rt], *(u8 *)data);
Sanjay Lale685c682012-11-21 18:34:04 -08001655 break;
1656
1657 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001658 kvm_err("Store not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001659 inst.word);
James Hogan8b48d5b2017-03-14 10:15:15 +00001660 goto out_fail;
Sanjay Lale685c682012-11-21 18:34:04 -08001661 }
1662
James Hogan8b48d5b2017-03-14 10:15:15 +00001663 run->mmio.is_write = 1;
1664 vcpu->mmio_needed = 1;
1665 vcpu->mmio_is_write = 1;
1666 return EMULATE_DO_MMIO;
Sanjay Lale685c682012-11-21 18:34:04 -08001667
James Hogan8b48d5b2017-03-14 10:15:15 +00001668out_fail:
1669 /* Rollback PC if emulation was unsuccessful */
1670 vcpu->arch.pc = curr_pc;
1671 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001672}
1673
James Hogan258f3a22016-06-15 19:29:47 +01001674enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
1675 u32 cause, struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001676 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001677{
James Hogan8b48d5b2017-03-14 10:15:15 +00001678 enum emulation_result er;
James Hogane1e575f62016-10-25 16:11:12 +01001679 unsigned long curr_pc;
James Hogan258f3a22016-06-15 19:29:47 +01001680 u32 op, rt;
Sanjay Lale685c682012-11-21 18:34:04 -08001681
James Hogan258f3a22016-06-15 19:29:47 +01001682 rt = inst.i_format.rt;
1683 op = inst.i_format.opcode;
Sanjay Lale685c682012-11-21 18:34:04 -08001684
James Hogane1e575f62016-10-25 16:11:12 +01001685 /*
1686 * Find the resume PC now while we have safe and easy access to the
1687 * prior branch instruction, and save it for
1688 * kvm_mips_complete_mmio_load() to restore later.
1689 */
1690 curr_pc = vcpu->arch.pc;
1691 er = update_pc(vcpu, cause);
1692 if (er == EMULATE_FAIL)
1693 return er;
1694 vcpu->arch.io_pc = vcpu->arch.pc;
1695 vcpu->arch.pc = curr_pc;
1696
Sanjay Lale685c682012-11-21 18:34:04 -08001697 vcpu->arch.io_gpr = rt;
1698
James Hogan8b48d5b2017-03-14 10:15:15 +00001699 run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
1700 vcpu->arch.host_cp0_badvaddr);
1701 if (run->mmio.phys_addr == KVM_INVALID_ADDR)
1702 return EMULATE_FAIL;
1703
1704 vcpu->mmio_needed = 2; /* signed */
Sanjay Lale685c682012-11-21 18:34:04 -08001705 switch (op) {
James Hogan59d78142017-03-14 10:15:16 +00001706#if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
1707 case ld_op:
1708 run->mmio.len = 8;
1709 break;
1710
1711 case lwu_op:
1712 vcpu->mmio_needed = 1; /* unsigned */
1713 /* fall through */
1714#endif
Sanjay Lale685c682012-11-21 18:34:04 -08001715 case lw_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001716 run->mmio.len = 4;
Sanjay Lale685c682012-11-21 18:34:04 -08001717 break;
1718
Sanjay Lale685c682012-11-21 18:34:04 -08001719 case lhu_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001720 vcpu->mmio_needed = 1; /* unsigned */
1721 /* fall through */
1722 case lh_op:
1723 run->mmio.len = 2;
Sanjay Lale685c682012-11-21 18:34:04 -08001724 break;
1725
1726 case lbu_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001727 vcpu->mmio_needed = 1; /* unsigned */
1728 /* fall through */
Sanjay Lale685c682012-11-21 18:34:04 -08001729 case lb_op:
James Hogan8b48d5b2017-03-14 10:15:15 +00001730 run->mmio.len = 1;
Sanjay Lale685c682012-11-21 18:34:04 -08001731 break;
1732
1733 default:
James Hogand86c1eb2016-06-14 09:40:17 +01001734 kvm_err("Load not yet supported (inst=0x%08x)\n",
James Hogan258f3a22016-06-15 19:29:47 +01001735 inst.word);
James Hogan8b48d5b2017-03-14 10:15:15 +00001736 vcpu->mmio_needed = 0;
1737 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001738 }
1739
James Hogan8b48d5b2017-03-14 10:15:15 +00001740 run->mmio.is_write = 0;
1741 vcpu->mmio_is_write = 0;
1742 return EMULATE_DO_MMIO;
Sanjay Lale685c682012-11-21 18:34:04 -08001743}
1744
James Hogan60c7aa32017-03-14 10:15:29 +00001745#ifndef CONFIG_KVM_MIPS_VZ
James Hogan4cf74c92016-11-26 00:37:28 +00001746static enum emulation_result kvm_mips_guest_cache_op(int (*fn)(unsigned long),
1747 unsigned long curr_pc,
1748 unsigned long addr,
1749 struct kvm_run *run,
1750 struct kvm_vcpu *vcpu,
1751 u32 cause)
1752{
1753 int err;
1754
1755 for (;;) {
1756 /* Carefully attempt the cache operation */
1757 kvm_trap_emul_gva_lockless_begin(vcpu);
1758 err = fn(addr);
1759 kvm_trap_emul_gva_lockless_end(vcpu);
1760
1761 if (likely(!err))
1762 return EMULATE_DONE;
1763
1764 /*
1765 * Try to handle the fault and retry, maybe we just raced with a
1766 * GVA invalidation.
1767 */
1768 switch (kvm_trap_emul_gva_fault(vcpu, addr, false)) {
1769 case KVM_MIPS_GVA:
1770 case KVM_MIPS_GPA:
1771 /* bad virtual or physical address */
1772 return EMULATE_FAIL;
1773 case KVM_MIPS_TLB:
1774 /* no matching guest TLB */
1775 vcpu->arch.host_cp0_badvaddr = addr;
1776 vcpu->arch.pc = curr_pc;
1777 kvm_mips_emulate_tlbmiss_ld(cause, NULL, run, vcpu);
1778 return EMULATE_EXCEPT;
1779 case KVM_MIPS_TLBINV:
1780 /* invalid matching guest TLB */
1781 vcpu->arch.host_cp0_badvaddr = addr;
1782 vcpu->arch.pc = curr_pc;
1783 kvm_mips_emulate_tlbinv_ld(cause, NULL, run, vcpu);
1784 return EMULATE_EXCEPT;
1785 default:
1786 break;
1787 };
1788 }
1789}
1790
James Hogan258f3a22016-06-15 19:29:47 +01001791enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
1792 u32 *opc, u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001793 struct kvm_run *run,
1794 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001795{
Sanjay Lale685c682012-11-21 18:34:04 -08001796 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01001797 u32 cache, op_inst, op, base;
1798 s16 offset;
Sanjay Lale685c682012-11-21 18:34:04 -08001799 struct kvm_vcpu_arch *arch = &vcpu->arch;
1800 unsigned long va;
1801 unsigned long curr_pc;
1802
1803 /*
1804 * Update PC and hold onto current PC in case there is
1805 * an error and we want to rollback the PC
1806 */
1807 curr_pc = vcpu->arch.pc;
1808 er = update_pc(vcpu, cause);
1809 if (er == EMULATE_FAIL)
1810 return er;
1811
James Hogan258f3a22016-06-15 19:29:47 +01001812 base = inst.i_format.rs;
1813 op_inst = inst.i_format.rt;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001814 if (cpu_has_mips_r6)
1815 offset = inst.spec3_format.simmediate;
1816 else
1817 offset = inst.i_format.simmediate;
James Hoganf4956f62015-12-16 23:49:37 +00001818 cache = op_inst & CacheOp_Cache;
1819 op = op_inst & CacheOp_Op;
Sanjay Lale685c682012-11-21 18:34:04 -08001820
1821 va = arch->gprs[base] + offset;
1822
1823 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1824 cache, op, base, arch->gprs[base], offset);
1825
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001826 /*
1827 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1828 * invalidate the caches entirely by stepping through all the
1829 * ways/indexes
Sanjay Lale685c682012-11-21 18:34:04 -08001830 */
James Hoganf4956f62015-12-16 23:49:37 +00001831 if (op == Index_Writeback_Inv) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001832 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1833 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1834 arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001835
James Hogan4fa9de52017-03-14 10:25:45 +00001836 if (cache == Cache_D) {
1837#ifdef CONFIG_CPU_R4K_CACHE_TLB
Sanjay Lale685c682012-11-21 18:34:04 -08001838 r4k_blast_dcache();
James Hogan4fa9de52017-03-14 10:25:45 +00001839#else
1840 switch (boot_cpu_type()) {
1841 case CPU_CAVIUM_OCTEON3:
1842 /* locally flush icache */
1843 local_flush_icache_range(0, 0);
1844 break;
1845 default:
1846 __flush_cache_all();
1847 break;
1848 }
1849#endif
1850 } else if (cache == Cache_I) {
1851#ifdef CONFIG_CPU_R4K_CACHE_TLB
Sanjay Lale685c682012-11-21 18:34:04 -08001852 r4k_blast_icache();
James Hogan4fa9de52017-03-14 10:25:45 +00001853#else
1854 switch (boot_cpu_type()) {
1855 case CPU_CAVIUM_OCTEON3:
1856 /* locally flush icache */
1857 local_flush_icache_range(0, 0);
1858 break;
1859 default:
1860 flush_icache_all();
1861 break;
1862 }
1863#endif
1864 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001865 kvm_err("%s: unsupported CACHE INDEX operation\n",
1866 __func__);
Sanjay Lale685c682012-11-21 18:34:04 -08001867 return EMULATE_FAIL;
1868 }
1869
1870#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1871 kvm_mips_trans_cache_index(inst, opc, vcpu);
1872#endif
1873 goto done;
1874 }
1875
Sanjay Lale685c682012-11-21 18:34:04 -08001876 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
James Hoganf4956f62015-12-16 23:49:37 +00001877 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
James Hogan4cf74c92016-11-26 00:37:28 +00001878 /*
1879 * Perform the dcache part of icache synchronisation on the
1880 * guest's behalf.
1881 */
1882 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1883 curr_pc, va, run, vcpu, cause);
1884 if (er != EMULATE_DONE)
1885 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001886#ifdef CONFIG_KVM_MIPS_DYN_TRANS
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001887 /*
1888 * Replace the CACHE instruction, with a SYNCI, not the same,
1889 * but avoids a trap
1890 */
Sanjay Lale685c682012-11-21 18:34:04 -08001891 kvm_mips_trans_cache_va(inst, opc, vcpu);
1892#endif
James Hoganf4956f62015-12-16 23:49:37 +00001893 } else if (op_inst == Hit_Invalidate_I) {
James Hogan4cf74c92016-11-26 00:37:28 +00001894 /* Perform the icache synchronisation on the guest's behalf */
1895 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1896 curr_pc, va, run, vcpu, cause);
1897 if (er != EMULATE_DONE)
1898 goto done;
1899 er = kvm_mips_guest_cache_op(protected_flush_icache_line,
1900 curr_pc, va, run, vcpu, cause);
1901 if (er != EMULATE_DONE)
1902 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001903
1904#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1905 /* Replace the CACHE instruction, with a SYNCI */
1906 kvm_mips_trans_cache_va(inst, opc, vcpu);
1907#endif
1908 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001909 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1910 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001911 er = EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001912 }
1913
James Hogancc81e942016-06-09 10:50:45 +01001914done:
1915 /* Rollback PC only if emulation was unsuccessful */
1916 if (er == EMULATE_FAIL)
1917 vcpu->arch.pc = curr_pc;
James Hogan4cf74c92016-11-26 00:37:28 +00001918 /* Guest exception needs guest to resume */
1919 if (er == EMULATE_EXCEPT)
1920 er = EMULATE_DONE;
James Hogancc81e942016-06-09 10:50:45 +01001921
Sanjay Lale685c682012-11-21 18:34:04 -08001922 return er;
1923}
1924
James Hogan31cf7492016-06-09 14:19:09 +01001925enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001926 struct kvm_run *run,
1927 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001928{
James Hogan258f3a22016-06-15 19:29:47 +01001929 union mips_instruction inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001930 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001931 int err;
Sanjay Lale685c682012-11-21 18:34:04 -08001932
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001933 /* Fetch the instruction. */
1934 if (cause & CAUSEF_BD)
Sanjay Lale685c682012-11-21 18:34:04 -08001935 opc += 1;
James Hogan6a97c772015-04-23 16:54:35 +01001936 err = kvm_get_badinstr(opc, vcpu, &inst.word);
James Hogan122e51d2016-11-28 17:23:14 +00001937 if (err)
1938 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001939
James Hogan258f3a22016-06-15 19:29:47 +01001940 switch (inst.r_format.opcode) {
Sanjay Lale685c682012-11-21 18:34:04 -08001941 case cop0_op:
1942 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1943 break;
Sanjay Lale685c682012-11-21 18:34:04 -08001944
James Hogan5cc4aaf2016-07-04 19:35:13 +01001945#ifndef CONFIG_CPU_MIPSR6
Sanjay Lale685c682012-11-21 18:34:04 -08001946 case cache_op:
1947 ++vcpu->stat.cache_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001948 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
Sanjay Lale685c682012-11-21 18:34:04 -08001949 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1950 break;
James Hogan5cc4aaf2016-07-04 19:35:13 +01001951#else
1952 case spec3_op:
1953 switch (inst.spec3_format.func) {
1954 case cache6_op:
1955 ++vcpu->stat.cache_exits;
1956 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1957 er = kvm_mips_emulate_cache(inst, opc, cause, run,
1958 vcpu);
1959 break;
1960 default:
1961 goto unknown;
1962 };
1963 break;
1964unknown:
1965#endif
Sanjay Lale685c682012-11-21 18:34:04 -08001966
1967 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001968 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
James Hogan258f3a22016-06-15 19:29:47 +01001969 inst.word);
Sanjay Lale685c682012-11-21 18:34:04 -08001970 kvm_arch_vcpu_dump_regs(vcpu);
1971 er = EMULATE_FAIL;
1972 break;
1973 }
1974
1975 return er;
1976}
James Hogan60c7aa32017-03-14 10:15:29 +00001977#endif /* CONFIG_KVM_MIPS_VZ */
Sanjay Lale685c682012-11-21 18:34:04 -08001978
James Hogan7801bbe2016-11-14 23:59:27 +00001979/**
1980 * kvm_mips_guest_exception_base() - Find guest exception vector base address.
1981 *
1982 * Returns: The base address of the current guest exception vector, taking
1983 * both Guest.CP0_Status.BEV and Guest.CP0_EBase into account.
1984 */
1985long kvm_mips_guest_exception_base(struct kvm_vcpu *vcpu)
1986{
1987 struct mips_coproc *cop0 = vcpu->arch.cop0;
1988
1989 if (kvm_read_c0_guest_status(cop0) & ST0_BEV)
1990 return KVM_GUEST_CKSEG1ADDR(0x1fc00200);
1991 else
1992 return kvm_read_c0_guest_ebase(cop0) & MIPS_EBASE_BASE;
1993}
1994
James Hogan31cf7492016-06-09 14:19:09 +01001995enum emulation_result kvm_mips_emulate_syscall(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001996 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001997 struct kvm_run *run,
1998 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001999{
2000 struct mips_coproc *cop0 = vcpu->arch.cop0;
2001 struct kvm_vcpu_arch *arch = &vcpu->arch;
2002 enum emulation_result er = EMULATE_DONE;
2003
2004 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2005 /* save old pc */
2006 kvm_write_c0_guest_epc(cop0, arch->pc);
2007 kvm_set_c0_guest_status(cop0, ST0_EXL);
2008
2009 if (cause & CAUSEF_BD)
2010 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2011 else
2012 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2013
2014 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
2015
2016 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002017 (EXCCODE_SYS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002018
2019 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002020 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002021
2022 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002023 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002024 er = EMULATE_FAIL;
2025 }
2026
2027 return er;
2028}
2029
James Hogan31cf7492016-06-09 14:19:09 +01002030enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002031 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002032 struct kvm_run *run,
2033 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002034{
2035 struct mips_coproc *cop0 = vcpu->arch.cop0;
2036 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002037 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002038 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002039
2040 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2041 /* save old pc */
2042 kvm_write_c0_guest_epc(cop0, arch->pc);
2043 kvm_set_c0_guest_status(cop0, ST0_EXL);
2044
2045 if (cause & CAUSEF_BD)
2046 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2047 else
2048 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2049
2050 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
2051 arch->pc);
2052
2053 /* set pc to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002054 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
Sanjay Lale685c682012-11-21 18:34:04 -08002055
2056 } else {
2057 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
2058 arch->pc);
2059
James Hogan7801bbe2016-11-14 23:59:27 +00002060 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002061 }
2062
2063 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002064 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002065
2066 /* setup badvaddr, context and entryhi registers for the guest */
2067 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2068 /* XXXKYMA: is the context register used by linux??? */
2069 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002070
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002071 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002072}
2073
James Hogan31cf7492016-06-09 14:19:09 +01002074enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002075 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002076 struct kvm_run *run,
2077 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002078{
2079 struct mips_coproc *cop0 = vcpu->arch.cop0;
2080 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002081 unsigned long entryhi =
2082 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002083 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002084
2085 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2086 /* save old pc */
2087 kvm_write_c0_guest_epc(cop0, arch->pc);
2088 kvm_set_c0_guest_status(cop0, ST0_EXL);
2089
2090 if (cause & CAUSEF_BD)
2091 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2092 else
2093 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2094
2095 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
2096 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002097 } else {
2098 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
2099 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002100 }
2101
James Hogan7801bbe2016-11-14 23:59:27 +00002102 /* set pc to the exception entry point */
2103 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2104
Sanjay Lale685c682012-11-21 18:34:04 -08002105 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002106 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002107
2108 /* setup badvaddr, context and entryhi registers for the guest */
2109 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2110 /* XXXKYMA: is the context register used by linux??? */
2111 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002112
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002113 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002114}
2115
James Hogan31cf7492016-06-09 14:19:09 +01002116enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002117 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002118 struct kvm_run *run,
2119 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002120{
2121 struct mips_coproc *cop0 = vcpu->arch.cop0;
2122 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002123 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002124 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002125
2126 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2127 /* save old pc */
2128 kvm_write_c0_guest_epc(cop0, arch->pc);
2129 kvm_set_c0_guest_status(cop0, ST0_EXL);
2130
2131 if (cause & CAUSEF_BD)
2132 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2133 else
2134 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2135
2136 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2137 arch->pc);
2138
2139 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002140 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
Sanjay Lale685c682012-11-21 18:34:04 -08002141 } else {
2142 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2143 arch->pc);
James Hogan7801bbe2016-11-14 23:59:27 +00002144 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002145 }
2146
2147 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002148 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002149
2150 /* setup badvaddr, context and entryhi registers for the guest */
2151 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2152 /* XXXKYMA: is the context register used by linux??? */
2153 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002154
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002155 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002156}
2157
James Hogan31cf7492016-06-09 14:19:09 +01002158enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002159 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002160 struct kvm_run *run,
2161 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002162{
2163 struct mips_coproc *cop0 = vcpu->arch.cop0;
2164 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002165 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002166 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002167
2168 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2169 /* save old pc */
2170 kvm_write_c0_guest_epc(cop0, arch->pc);
2171 kvm_set_c0_guest_status(cop0, ST0_EXL);
2172
2173 if (cause & CAUSEF_BD)
2174 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2175 else
2176 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2177
2178 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2179 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002180 } else {
2181 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2182 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002183 }
2184
James Hogan7801bbe2016-11-14 23:59:27 +00002185 /* Set PC to the exception entry point */
2186 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2187
Sanjay Lale685c682012-11-21 18:34:04 -08002188 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002189 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002190
2191 /* setup badvaddr, context and entryhi registers for the guest */
2192 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2193 /* XXXKYMA: is the context register used by linux??? */
2194 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002195
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002196 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002197}
2198
James Hogan31cf7492016-06-09 14:19:09 +01002199enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002200 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002201 struct kvm_run *run,
2202 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002203{
2204 struct mips_coproc *cop0 = vcpu->arch.cop0;
2205 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002206 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08002207 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002208
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("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2220 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002221 } else {
2222 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2223 arch->pc);
Sanjay Lale685c682012-11-21 18:34:04 -08002224 }
2225
James Hogan7801bbe2016-11-14 23:59:27 +00002226 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2227
James Hogan16d100db2015-12-16 23:49:33 +00002228 kvm_change_c0_guest_cause(cop0, (0xff),
2229 (EXCCODE_MOD << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002230
2231 /* setup badvaddr, context and entryhi registers for the guest */
2232 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2233 /* XXXKYMA: is the context register used by linux??? */
2234 kvm_write_c0_guest_entryhi(cop0, entryhi);
Sanjay Lale685c682012-11-21 18:34:04 -08002235
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002236 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002237}
2238
James Hogan31cf7492016-06-09 14:19:09 +01002239enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002240 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002241 struct kvm_run *run,
2242 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002243{
2244 struct mips_coproc *cop0 = vcpu->arch.cop0;
2245 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002246
2247 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2248 /* save old pc */
2249 kvm_write_c0_guest_epc(cop0, arch->pc);
2250 kvm_set_c0_guest_status(cop0, ST0_EXL);
2251
2252 if (cause & CAUSEF_BD)
2253 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2254 else
2255 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2256
2257 }
2258
James Hogan7801bbe2016-11-14 23:59:27 +00002259 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002260
2261 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002262 (EXCCODE_CPU << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002263 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2264
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002265 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002266}
2267
James Hogan31cf7492016-06-09 14:19:09 +01002268enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002269 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002270 struct kvm_run *run,
2271 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002272{
2273 struct mips_coproc *cop0 = vcpu->arch.cop0;
2274 struct kvm_vcpu_arch *arch = &vcpu->arch;
2275 enum emulation_result er = EMULATE_DONE;
2276
2277 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2278 /* save old pc */
2279 kvm_write_c0_guest_epc(cop0, arch->pc);
2280 kvm_set_c0_guest_status(cop0, ST0_EXL);
2281
2282 if (cause & CAUSEF_BD)
2283 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2284 else
2285 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2286
2287 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2288
2289 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002290 (EXCCODE_RI << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002291
2292 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002293 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002294
2295 } else {
2296 kvm_err("Trying to deliver RI when EXL is already set\n");
2297 er = EMULATE_FAIL;
2298 }
2299
2300 return er;
2301}
2302
James Hogan31cf7492016-06-09 14:19:09 +01002303enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002304 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002305 struct kvm_run *run,
2306 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002307{
2308 struct mips_coproc *cop0 = vcpu->arch.cop0;
2309 struct kvm_vcpu_arch *arch = &vcpu->arch;
2310 enum emulation_result er = EMULATE_DONE;
2311
2312 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2313 /* save old pc */
2314 kvm_write_c0_guest_epc(cop0, arch->pc);
2315 kvm_set_c0_guest_status(cop0, ST0_EXL);
2316
2317 if (cause & CAUSEF_BD)
2318 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2319 else
2320 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2321
2322 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2323
2324 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002325 (EXCCODE_BP << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002326
2327 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002328 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002329
2330 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002331 kvm_err("Trying to deliver BP when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002332 er = EMULATE_FAIL;
2333 }
2334
2335 return er;
2336}
2337
James Hogan31cf7492016-06-09 14:19:09 +01002338enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002339 u32 *opc,
James Hogan0a560422015-02-06 16:03:57 +00002340 struct kvm_run *run,
2341 struct kvm_vcpu *vcpu)
2342{
2343 struct mips_coproc *cop0 = vcpu->arch.cop0;
2344 struct kvm_vcpu_arch *arch = &vcpu->arch;
2345 enum emulation_result er = EMULATE_DONE;
2346
2347 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2348 /* save old pc */
2349 kvm_write_c0_guest_epc(cop0, arch->pc);
2350 kvm_set_c0_guest_status(cop0, ST0_EXL);
2351
2352 if (cause & CAUSEF_BD)
2353 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2354 else
2355 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2356
2357 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2358
2359 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002360 (EXCCODE_TR << CAUSEB_EXCCODE));
James Hogan0a560422015-02-06 16:03:57 +00002361
2362 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002363 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hogan0a560422015-02-06 16:03:57 +00002364
2365 } else {
2366 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2367 er = EMULATE_FAIL;
2368 }
2369
2370 return er;
2371}
2372
James Hogan31cf7492016-06-09 14:19:09 +01002373enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002374 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002375 struct kvm_run *run,
2376 struct kvm_vcpu *vcpu)
2377{
2378 struct mips_coproc *cop0 = vcpu->arch.cop0;
2379 struct kvm_vcpu_arch *arch = &vcpu->arch;
2380 enum emulation_result er = EMULATE_DONE;
2381
2382 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2383 /* save old pc */
2384 kvm_write_c0_guest_epc(cop0, arch->pc);
2385 kvm_set_c0_guest_status(cop0, ST0_EXL);
2386
2387 if (cause & CAUSEF_BD)
2388 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2389 else
2390 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2391
2392 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2393
2394 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002395 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002396
2397 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002398 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hoganc2537ed2015-02-06 10:56:27 +00002399
2400 } else {
2401 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2402 er = EMULATE_FAIL;
2403 }
2404
2405 return er;
2406}
2407
James Hogan31cf7492016-06-09 14:19:09 +01002408enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002409 u32 *opc,
James Hogan1c0cd662015-02-06 10:56:27 +00002410 struct kvm_run *run,
2411 struct kvm_vcpu *vcpu)
2412{
2413 struct mips_coproc *cop0 = vcpu->arch.cop0;
2414 struct kvm_vcpu_arch *arch = &vcpu->arch;
2415 enum emulation_result er = EMULATE_DONE;
2416
2417 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2418 /* save old pc */
2419 kvm_write_c0_guest_epc(cop0, arch->pc);
2420 kvm_set_c0_guest_status(cop0, ST0_EXL);
2421
2422 if (cause & CAUSEF_BD)
2423 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2424 else
2425 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2426
2427 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2428
2429 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002430 (EXCCODE_FPE << CAUSEB_EXCCODE));
James Hogan1c0cd662015-02-06 10:56:27 +00002431
2432 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002433 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hogan1c0cd662015-02-06 10:56:27 +00002434
2435 } else {
2436 kvm_err("Trying to deliver FPE when EXL is already set\n");
2437 er = EMULATE_FAIL;
2438 }
2439
2440 return er;
2441}
2442
James Hogan31cf7492016-06-09 14:19:09 +01002443enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002444 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002445 struct kvm_run *run,
2446 struct kvm_vcpu *vcpu)
2447{
2448 struct mips_coproc *cop0 = vcpu->arch.cop0;
2449 struct kvm_vcpu_arch *arch = &vcpu->arch;
2450 enum emulation_result er = EMULATE_DONE;
2451
2452 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2453 /* save old pc */
2454 kvm_write_c0_guest_epc(cop0, arch->pc);
2455 kvm_set_c0_guest_status(cop0, ST0_EXL);
2456
2457 if (cause & CAUSEF_BD)
2458 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2459 else
2460 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2461
2462 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2463
2464 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002465 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002466
2467 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002468 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
James Hoganc2537ed2015-02-06 10:56:27 +00002469
2470 } else {
2471 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2472 er = EMULATE_FAIL;
2473 }
2474
2475 return er;
2476}
2477
James Hogan31cf7492016-06-09 14:19:09 +01002478enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002479 struct kvm_run *run,
2480 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002481{
2482 struct mips_coproc *cop0 = vcpu->arch.cop0;
2483 struct kvm_vcpu_arch *arch = &vcpu->arch;
2484 enum emulation_result er = EMULATE_DONE;
2485 unsigned long curr_pc;
James Hogan258f3a22016-06-15 19:29:47 +01002486 union mips_instruction inst;
James Hogan122e51d2016-11-28 17:23:14 +00002487 int err;
Sanjay Lale685c682012-11-21 18:34:04 -08002488
2489 /*
2490 * Update PC and hold onto current PC in case there is
2491 * an error and we want to rollback the PC
2492 */
2493 curr_pc = vcpu->arch.pc;
2494 er = update_pc(vcpu, cause);
2495 if (er == EMULATE_FAIL)
2496 return er;
2497
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002498 /* Fetch the instruction. */
Sanjay Lale685c682012-11-21 18:34:04 -08002499 if (cause & CAUSEF_BD)
2500 opc += 1;
James Hogan6a97c772015-04-23 16:54:35 +01002501 err = kvm_get_badinstr(opc, vcpu, &inst.word);
James Hogan122e51d2016-11-28 17:23:14 +00002502 if (err) {
2503 kvm_err("%s: Cannot get inst @ %p (%d)\n", __func__, opc, err);
Sanjay Lale685c682012-11-21 18:34:04 -08002504 return EMULATE_FAIL;
2505 }
2506
James Hogan258f3a22016-06-15 19:29:47 +01002507 if (inst.r_format.opcode == spec3_op &&
James Hogan8eeab812016-07-04 19:35:14 +01002508 inst.r_format.func == rdhwr_op &&
2509 inst.r_format.rs == 0 &&
2510 (inst.r_format.re >> 3) == 0) {
James Hogan26f4f3b2014-03-14 13:06:09 +00002511 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
James Hogan258f3a22016-06-15 19:29:47 +01002512 int rd = inst.r_format.rd;
2513 int rt = inst.r_format.rt;
2514 int sel = inst.r_format.re & 0x7;
James Hogan6398da12016-06-14 09:40:15 +01002515
James Hogan26f4f3b2014-03-14 13:06:09 +00002516 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2517 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2518 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2519 rd, opc);
2520 goto emulate_ri;
2521 }
Sanjay Lale685c682012-11-21 18:34:04 -08002522 switch (rd) {
James Hoganaff565a2016-06-15 19:29:52 +01002523 case MIPS_HWR_CPUNUM: /* CPU number */
James Hogancf1fb0f2016-06-15 19:29:55 +01002524 arch->gprs[rt] = vcpu->vcpu_id;
Sanjay Lale685c682012-11-21 18:34:04 -08002525 break;
James Hoganaff565a2016-06-15 19:29:52 +01002526 case MIPS_HWR_SYNCISTEP: /* SYNCI length */
Sanjay Lale685c682012-11-21 18:34:04 -08002527 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2528 current_cpu_data.icache.linesz);
2529 break;
James Hoganaff565a2016-06-15 19:29:52 +01002530 case MIPS_HWR_CC: /* Read count register */
James Hogan172e02d2016-07-08 11:53:28 +01002531 arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002532 break;
James Hoganaff565a2016-06-15 19:29:52 +01002533 case MIPS_HWR_CCRES: /* Count register resolution */
Sanjay Lale685c682012-11-21 18:34:04 -08002534 switch (current_cpu_data.cputype) {
2535 case CPU_20KC:
2536 case CPU_25KF:
2537 arch->gprs[rt] = 1;
2538 break;
2539 default:
2540 arch->gprs[rt] = 2;
2541 }
2542 break;
James Hoganaff565a2016-06-15 19:29:52 +01002543 case MIPS_HWR_ULR: /* Read UserLocal register */
Sanjay Lale685c682012-11-21 18:34:04 -08002544 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08002545 break;
2546
2547 default:
James Hogan15505672014-03-14 13:06:07 +00002548 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
James Hogan26f4f3b2014-03-14 13:06:09 +00002549 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002550 }
James Hogan6398da12016-06-14 09:40:15 +01002551
2552 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
2553 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08002554 } else {
James Hogan258f3a22016-06-15 19:29:47 +01002555 kvm_debug("Emulate RI not supported @ %p: %#x\n",
2556 opc, inst.word);
James Hogan26f4f3b2014-03-14 13:06:09 +00002557 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002558 }
2559
James Hogan26f4f3b2014-03-14 13:06:09 +00002560 return EMULATE_DONE;
2561
2562emulate_ri:
Sanjay Lale685c682012-11-21 18:34:04 -08002563 /*
James Hogan26f4f3b2014-03-14 13:06:09 +00002564 * Rollback PC (if in branch delay slot then the PC already points to
2565 * branch target), and pass the RI exception to the guest OS.
Sanjay Lale685c682012-11-21 18:34:04 -08002566 */
James Hogan26f4f3b2014-03-14 13:06:09 +00002567 vcpu->arch.pc = curr_pc;
2568 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002569}
2570
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002571enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2572 struct kvm_run *run)
Sanjay Lale685c682012-11-21 18:34:04 -08002573{
2574 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2575 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002576
2577 if (run->mmio.len > sizeof(*gpr)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002578 kvm_err("Bad MMIO length: %d", run->mmio.len);
Sanjay Lale685c682012-11-21 18:34:04 -08002579 er = EMULATE_FAIL;
2580 goto done;
2581 }
2582
James Hogane1e575f62016-10-25 16:11:12 +01002583 /* Restore saved resume PC */
2584 vcpu->arch.pc = vcpu->arch.io_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08002585
2586 switch (run->mmio.len) {
James Hogan59d78142017-03-14 10:15:16 +00002587 case 8:
2588 *gpr = *(s64 *)run->mmio.data;
2589 break;
2590
Sanjay Lale685c682012-11-21 18:34:04 -08002591 case 4:
James Hogan59d78142017-03-14 10:15:16 +00002592 if (vcpu->mmio_needed == 2)
2593 *gpr = *(s32 *)run->mmio.data;
2594 else
2595 *gpr = *(u32 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002596 break;
2597
2598 case 2:
2599 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002600 *gpr = *(s16 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002601 else
James Hogan8cffd192016-06-09 14:19:08 +01002602 *gpr = *(u16 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002603
2604 break;
2605 case 1:
2606 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002607 *gpr = *(s8 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002608 else
2609 *gpr = *(u8 *) run->mmio.data;
2610 break;
2611 }
2612
Sanjay Lale685c682012-11-21 18:34:04 -08002613done:
2614 return er;
2615}
2616
James Hogan31cf7492016-06-09 14:19:09 +01002617static enum emulation_result kvm_mips_emulate_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002618 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002619 struct kvm_run *run,
2620 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002621{
James Hogan8cffd192016-06-09 14:19:08 +01002622 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002623 struct mips_coproc *cop0 = vcpu->arch.cop0;
2624 struct kvm_vcpu_arch *arch = &vcpu->arch;
2625 enum emulation_result er = EMULATE_DONE;
2626
2627 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2628 /* save old pc */
2629 kvm_write_c0_guest_epc(cop0, arch->pc);
2630 kvm_set_c0_guest_status(cop0, ST0_EXL);
2631
2632 if (cause & CAUSEF_BD)
2633 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2634 else
2635 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2636
2637 kvm_change_c0_guest_cause(cop0, (0xff),
2638 (exccode << CAUSEB_EXCCODE));
2639
2640 /* Set PC to the exception entry point */
James Hogan7801bbe2016-11-14 23:59:27 +00002641 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
Sanjay Lale685c682012-11-21 18:34:04 -08002642 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2643
2644 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2645 exccode, kvm_read_c0_guest_epc(cop0),
2646 kvm_read_c0_guest_badvaddr(cop0));
2647 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002648 kvm_err("Trying to deliver EXC when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002649 er = EMULATE_FAIL;
2650 }
2651
2652 return er;
2653}
2654
James Hogan31cf7492016-06-09 14:19:09 +01002655enum emulation_result kvm_mips_check_privilege(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002656 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002657 struct kvm_run *run,
2658 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002659{
2660 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002661 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002662 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2663
2664 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2665
2666 if (usermode) {
2667 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00002668 case EXCCODE_INT:
2669 case EXCCODE_SYS:
2670 case EXCCODE_BP:
2671 case EXCCODE_RI:
2672 case EXCCODE_TR:
2673 case EXCCODE_MSAFPE:
2674 case EXCCODE_FPE:
2675 case EXCCODE_MSADIS:
Sanjay Lale685c682012-11-21 18:34:04 -08002676 break;
2677
James Hogan16d100db2015-12-16 23:49:33 +00002678 case EXCCODE_CPU:
Sanjay Lale685c682012-11-21 18:34:04 -08002679 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2680 er = EMULATE_PRIV_FAIL;
2681 break;
2682
James Hogan16d100db2015-12-16 23:49:33 +00002683 case EXCCODE_MOD:
Sanjay Lale685c682012-11-21 18:34:04 -08002684 break;
2685
James Hogan16d100db2015-12-16 23:49:33 +00002686 case EXCCODE_TLBL:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002687 /*
2688 * We we are accessing Guest kernel space, then send an
2689 * address error exception to the guest
2690 */
Sanjay Lale685c682012-11-21 18:34:04 -08002691 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002692 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2693 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002694 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002695 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002696 er = EMULATE_PRIV_FAIL;
2697 }
2698 break;
2699
James Hogan16d100db2015-12-16 23:49:33 +00002700 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002701 /*
2702 * We we are accessing Guest kernel space, then send an
2703 * address error exception to the guest
2704 */
Sanjay Lale685c682012-11-21 18:34:04 -08002705 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002706 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2707 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002708 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002709 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002710 er = EMULATE_PRIV_FAIL;
2711 }
2712 break;
2713
James Hogan16d100db2015-12-16 23:49:33 +00002714 case EXCCODE_ADES:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002715 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2716 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002717 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2718 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002719 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002720 }
2721 er = EMULATE_PRIV_FAIL;
2722 break;
James Hogan16d100db2015-12-16 23:49:33 +00002723 case EXCCODE_ADEL:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002724 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2725 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002726 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2727 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002728 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002729 }
2730 er = EMULATE_PRIV_FAIL;
2731 break;
2732 default:
2733 er = EMULATE_PRIV_FAIL;
2734 break;
2735 }
2736 }
2737
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002738 if (er == EMULATE_PRIV_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08002739 kvm_mips_emulate_exc(cause, opc, run, vcpu);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002740
Sanjay Lale685c682012-11-21 18:34:04 -08002741 return er;
2742}
2743
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002744/*
2745 * User Address (UA) fault, this could happen if
Sanjay Lale685c682012-11-21 18:34:04 -08002746 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2747 * case we pass on the fault to the guest kernel and let it handle it.
2748 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2749 * case we inject the TLB from the Guest TLB into the shadow host TLB
2750 */
James Hogan31cf7492016-06-09 14:19:09 +01002751enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002752 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002753 struct kvm_run *run,
James Hogan577ed7f2015-05-01 14:56:31 +01002754 struct kvm_vcpu *vcpu,
2755 bool write_fault)
Sanjay Lale685c682012-11-21 18:34:04 -08002756{
2757 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002758 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002759 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2760 int index;
2761
James Hogane4e94c02016-06-09 14:19:05 +01002762 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2763 vcpu->arch.host_cp0_badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002764
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002765 /*
2766 * KVM would not have got the exception if this entry was valid in the
2767 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2768 * send the guest an exception. The guest exc handler should then inject
2769 * an entry into the guest TLB.
Sanjay Lale685c682012-11-21 18:34:04 -08002770 */
2771 index = kvm_mips_guest_tlb_lookup(vcpu,
James Hogancaa1faa2015-12-16 23:49:26 +00002772 (va & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002773 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2774 KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08002775 if (index < 0) {
James Hogan16d100db2015-12-16 23:49:33 +00002776 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002777 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002778 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002779 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2780 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002781 kvm_err("%s: invalid exc code: %d\n", __func__,
2782 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002783 er = EMULATE_FAIL;
2784 }
2785 } else {
2786 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2787
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002788 /*
2789 * Check if the entry is valid, if not then setup a TLB invalid
2790 * exception to the guest
2791 */
Sanjay Lale685c682012-11-21 18:34:04 -08002792 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan16d100db2015-12-16 23:49:33 +00002793 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002794 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2795 vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002796 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002797 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2798 vcpu);
2799 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002800 kvm_err("%s: invalid exc code: %d\n", __func__,
2801 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002802 er = EMULATE_FAIL;
2803 }
2804 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002805 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
James Hogan9fbfb062016-06-09 14:19:17 +01002806 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002807 /*
2808 * OK we have a Guest TLB entry, now inject it into the
2809 * shadow host TLB
2810 */
James Hogan577ed7f2015-05-01 14:56:31 +01002811 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, va,
2812 write_fault)) {
James Hogan9b731bc2016-08-11 11:58:15 +01002813 kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
2814 __func__, va, index, vcpu,
2815 read_c0_entryhi());
2816 er = EMULATE_FAIL;
2817 }
Sanjay Lale685c682012-11-21 18:34:04 -08002818 }
2819 }
2820
2821 return er;
2822}