blob: ee0e61d2b6fb2612a5c1555f2970a9fdaf2a1e0e [file] [log] [blame]
Sanjay Lale685c682012-11-21 18:34:04 -08001/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * KVM/MIPS: Instruction/Exception emulation
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 */
Sanjay Lale685c682012-11-21 18:34:04 -080011
12#include <linux/errno.h>
13#include <linux/err.h>
James Hogane30492b2014-05-29 10:16:35 +010014#include <linux/ktime.h>
Sanjay Lale685c682012-11-21 18:34:04 -080015#include <linux/kvm_host.h>
16#include <linux/module.h>
17#include <linux/vmalloc.h>
18#include <linux/fs.h>
19#include <linux/bootmem.h>
20#include <linux/random.h>
21#include <asm/page.h>
22#include <asm/cacheflush.h>
James Hoganf4956f62015-12-16 23:49:37 +000023#include <asm/cacheops.h>
Sanjay Lale685c682012-11-21 18:34:04 -080024#include <asm/cpu-info.h>
25#include <asm/mmu_context.h>
26#include <asm/tlbflush.h>
27#include <asm/inst.h>
28
29#undef CONFIG_MIPS_MT
30#include <asm/r4kcache.h>
31#define CONFIG_MIPS_MT
32
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070033#include "interrupt.h"
34#include "commpage.h"
Sanjay Lale685c682012-11-21 18:34:04 -080035
36#include "trace.h"
37
38/*
39 * Compute the return address and do emulate branch simulation, if required.
40 * This function should be called only in branch delay slot active.
41 */
42unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
43 unsigned long instpc)
44{
45 unsigned int dspcontrol;
46 union mips_instruction insn;
47 struct kvm_vcpu_arch *arch = &vcpu->arch;
48 long epc = instpc;
49 long nextpc = KVM_INVALID_INST;
50
51 if (epc & 3)
52 goto unaligned;
53
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070054 /* Read the instruction */
James Hogan8cffd192016-06-09 14:19:08 +010055 insn.word = kvm_get_inst((u32 *) epc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -080056
57 if (insn.word == KVM_INVALID_INST)
58 return KVM_INVALID_INST;
59
60 switch (insn.i_format.opcode) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070061 /* jr and jalr are in r_format format. */
Sanjay Lale685c682012-11-21 18:34:04 -080062 case spec_op:
63 switch (insn.r_format.func) {
64 case jalr_op:
65 arch->gprs[insn.r_format.rd] = epc + 8;
66 /* Fall through */
67 case jr_op:
68 nextpc = arch->gprs[insn.r_format.rs];
69 break;
70 }
71 break;
72
73 /*
74 * This group contains:
75 * bltz_op, bgez_op, bltzl_op, bgezl_op,
76 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
77 */
78 case bcond_op:
79 switch (insn.i_format.rt) {
80 case bltz_op:
81 case bltzl_op:
82 if ((long)arch->gprs[insn.i_format.rs] < 0)
83 epc = epc + 4 + (insn.i_format.simmediate << 2);
84 else
85 epc += 8;
86 nextpc = epc;
87 break;
88
89 case bgez_op:
90 case bgezl_op:
91 if ((long)arch->gprs[insn.i_format.rs] >= 0)
92 epc = epc + 4 + (insn.i_format.simmediate << 2);
93 else
94 epc += 8;
95 nextpc = epc;
96 break;
97
98 case bltzal_op:
99 case bltzall_op:
100 arch->gprs[31] = epc + 8;
101 if ((long)arch->gprs[insn.i_format.rs] < 0)
102 epc = epc + 4 + (insn.i_format.simmediate << 2);
103 else
104 epc += 8;
105 nextpc = epc;
106 break;
107
108 case bgezal_op:
109 case bgezall_op:
110 arch->gprs[31] = epc + 8;
111 if ((long)arch->gprs[insn.i_format.rs] >= 0)
112 epc = epc + 4 + (insn.i_format.simmediate << 2);
113 else
114 epc += 8;
115 nextpc = epc;
116 break;
117 case bposge32_op:
118 if (!cpu_has_dsp)
119 goto sigill;
120
121 dspcontrol = rddsp(0x01);
122
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700123 if (dspcontrol >= 32)
Sanjay Lale685c682012-11-21 18:34:04 -0800124 epc = epc + 4 + (insn.i_format.simmediate << 2);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700125 else
Sanjay Lale685c682012-11-21 18:34:04 -0800126 epc += 8;
127 nextpc = epc;
128 break;
129 }
130 break;
131
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700132 /* These are unconditional and in j_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800133 case jal_op:
134 arch->gprs[31] = instpc + 8;
135 case j_op:
136 epc += 4;
137 epc >>= 28;
138 epc <<= 28;
139 epc |= (insn.j_format.target << 2);
140 nextpc = epc;
141 break;
142
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700143 /* These are conditional and in i_format. */
Sanjay Lale685c682012-11-21 18:34:04 -0800144 case beq_op:
145 case beql_op:
146 if (arch->gprs[insn.i_format.rs] ==
147 arch->gprs[insn.i_format.rt])
148 epc = epc + 4 + (insn.i_format.simmediate << 2);
149 else
150 epc += 8;
151 nextpc = epc;
152 break;
153
154 case bne_op:
155 case bnel_op:
156 if (arch->gprs[insn.i_format.rs] !=
157 arch->gprs[insn.i_format.rt])
158 epc = epc + 4 + (insn.i_format.simmediate << 2);
159 else
160 epc += 8;
161 nextpc = epc;
162 break;
163
164 case blez_op: /* not really i_format */
165 case blezl_op:
166 /* rt field assumed to be zero */
167 if ((long)arch->gprs[insn.i_format.rs] <= 0)
168 epc = epc + 4 + (insn.i_format.simmediate << 2);
169 else
170 epc += 8;
171 nextpc = epc;
172 break;
173
174 case bgtz_op:
175 case bgtzl_op:
176 /* rt field assumed to be zero */
177 if ((long)arch->gprs[insn.i_format.rs] > 0)
178 epc = epc + 4 + (insn.i_format.simmediate << 2);
179 else
180 epc += 8;
181 nextpc = epc;
182 break;
183
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700184 /* And now the FPA/cp1 branch instructions. */
Sanjay Lale685c682012-11-21 18:34:04 -0800185 case cop1_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700186 kvm_err("%s: unsupported cop1_op\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800187 break;
188 }
189
190 return nextpc;
191
192unaligned:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700193 kvm_err("%s: unaligned epc\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800194 return nextpc;
195
196sigill:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700197 kvm_err("%s: DSP branch but not DSP ASE\n", __func__);
Sanjay Lale685c682012-11-21 18:34:04 -0800198 return nextpc;
199}
200
James Hoganbdb7ed82016-06-09 14:19:07 +0100201enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
Sanjay Lale685c682012-11-21 18:34:04 -0800202{
203 unsigned long branch_pc;
204 enum emulation_result er = EMULATE_DONE;
205
206 if (cause & CAUSEF_BD) {
207 branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
208 if (branch_pc == KVM_INVALID_INST) {
209 er = EMULATE_FAIL;
210 } else {
211 vcpu->arch.pc = branch_pc;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700212 kvm_debug("BD update_pc(): New PC: %#lx\n",
213 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800214 }
215 } else
216 vcpu->arch.pc += 4;
217
218 kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
219
220 return er;
221}
222
James Hogane30492b2014-05-29 10:16:35 +0100223/**
224 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
225 * @vcpu: Virtual CPU.
Sanjay Lale685c682012-11-21 18:34:04 -0800226 *
James Hoganf8239342014-05-29 10:16:37 +0100227 * Returns: 1 if the CP0_Count timer is disabled by either the guest
228 * CP0_Cause.DC bit or the count_ctl.DC bit.
James Hogane30492b2014-05-29 10:16:35 +0100229 * 0 otherwise (in which case CP0_Count timer is running).
Sanjay Lale685c682012-11-21 18:34:04 -0800230 */
James Hogane30492b2014-05-29 10:16:35 +0100231static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -0800232{
233 struct mips_coproc *cop0 = vcpu->arch.cop0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700234
James Hoganf8239342014-05-29 10:16:37 +0100235 return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
236 (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
James Hogane30492b2014-05-29 10:16:35 +0100237}
Sanjay Lale685c682012-11-21 18:34:04 -0800238
James Hogane30492b2014-05-29 10:16:35 +0100239/**
240 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
241 *
242 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
243 *
244 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
245 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100246static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100247{
248 s64 now_ns, periods;
249 u64 delta;
250
251 now_ns = ktime_to_ns(now);
252 delta = now_ns + vcpu->arch.count_dyn_bias;
253
254 if (delta >= vcpu->arch.count_period) {
255 /* If delta is out of safe range the bias needs adjusting */
256 periods = div64_s64(now_ns, vcpu->arch.count_period);
257 vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
258 /* Recalculate delta with new bias */
259 delta = now_ns + vcpu->arch.count_dyn_bias;
Sanjay Lale685c682012-11-21 18:34:04 -0800260 }
261
James Hogane30492b2014-05-29 10:16:35 +0100262 /*
263 * We've ensured that:
264 * delta < count_period
265 *
266 * Therefore the intermediate delta*count_hz will never overflow since
267 * at the boundary condition:
268 * delta = count_period
269 * delta = NSEC_PER_SEC * 2^32 / count_hz
270 * delta * count_hz = NSEC_PER_SEC * 2^32
271 */
272 return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
273}
274
275/**
James Hoganf8239342014-05-29 10:16:37 +0100276 * kvm_mips_count_time() - Get effective current time.
277 * @vcpu: Virtual CPU.
278 *
279 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
280 * except when the master disable bit is set in count_ctl, in which case it is
281 * count_resume, i.e. the time that the count was disabled.
282 *
283 * Returns: Effective monotonic ktime for CP0_Count.
284 */
285static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
286{
287 if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
288 return vcpu->arch.count_resume;
289
290 return ktime_get();
291}
292
293/**
James Hogane30492b2014-05-29 10:16:35 +0100294 * kvm_mips_read_count_running() - Read the current count value as if running.
295 * @vcpu: Virtual CPU.
296 * @now: Kernel time to read CP0_Count at.
297 *
298 * Returns the current guest CP0_Count register at time @now and handles if the
299 * timer interrupt is pending and hasn't been handled yet.
300 *
301 * Returns: The current value of the guest CP0_Count register.
302 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100303static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
James Hogane30492b2014-05-29 10:16:35 +0100304{
James Hogan4355c442016-04-22 10:38:45 +0100305 struct mips_coproc *cop0 = vcpu->arch.cop0;
306 ktime_t expires, threshold;
James Hogan8cffd192016-06-09 14:19:08 +0100307 u32 count, compare;
James Hogane30492b2014-05-29 10:16:35 +0100308 int running;
309
James Hogan4355c442016-04-22 10:38:45 +0100310 /* Calculate the biased and scaled guest CP0_Count */
311 count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
312 compare = kvm_read_c0_guest_compare(cop0);
313
314 /*
315 * Find whether CP0_Count has reached the closest timer interrupt. If
316 * not, we shouldn't inject it.
317 */
James Hogan8cffd192016-06-09 14:19:08 +0100318 if ((s32)(count - compare) < 0)
James Hogan4355c442016-04-22 10:38:45 +0100319 return count;
320
321 /*
322 * The CP0_Count we're going to return has already reached the closest
323 * timer interrupt. Quickly check if it really is a new interrupt by
324 * looking at whether the interval until the hrtimer expiry time is
325 * less than 1/4 of the timer period.
326 */
James Hogane30492b2014-05-29 10:16:35 +0100327 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
James Hogan4355c442016-04-22 10:38:45 +0100328 threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
329 if (ktime_before(expires, threshold)) {
James Hogane30492b2014-05-29 10:16:35 +0100330 /*
331 * Cancel it while we handle it so there's no chance of
332 * interference with the timeout handler.
333 */
334 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
335
336 /* Nothing should be waiting on the timeout */
337 kvm_mips_callbacks->queue_timer_int(vcpu);
338
339 /*
340 * Restart the timer if it was running based on the expiry time
341 * we read, so that we don't push it back 2 periods.
342 */
343 if (running) {
344 expires = ktime_add_ns(expires,
345 vcpu->arch.count_period);
346 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
347 HRTIMER_MODE_ABS);
348 }
349 }
350
James Hogan4355c442016-04-22 10:38:45 +0100351 return count;
James Hogane30492b2014-05-29 10:16:35 +0100352}
353
354/**
355 * kvm_mips_read_count() - Read the current count value.
356 * @vcpu: Virtual CPU.
357 *
358 * Read the current guest CP0_Count value, taking into account whether the timer
359 * is stopped.
360 *
361 * Returns: The current guest CP0_Count value.
362 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100363u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
James Hogane30492b2014-05-29 10:16:35 +0100364{
365 struct mips_coproc *cop0 = vcpu->arch.cop0;
366
367 /* If count disabled just read static copy of count */
368 if (kvm_mips_count_disabled(vcpu))
369 return kvm_read_c0_guest_count(cop0);
370
371 return kvm_mips_read_count_running(vcpu, ktime_get());
372}
373
374/**
375 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
376 * @vcpu: Virtual CPU.
377 * @count: Output pointer for CP0_Count value at point of freeze.
378 *
379 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
380 * at the point it was frozen. It is guaranteed that any pending interrupts at
381 * the point it was frozen are handled, and none after that point.
382 *
383 * This is useful where the time/CP0_Count is needed in the calculation of the
384 * new parameters.
385 *
386 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
387 *
388 * Returns: The ktime at the point of freeze.
389 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100390static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
James Hogane30492b2014-05-29 10:16:35 +0100391{
392 ktime_t now;
393
394 /* stop hrtimer before finding time */
395 hrtimer_cancel(&vcpu->arch.comparecount_timer);
396 now = ktime_get();
397
398 /* find count at this point and handle pending hrtimer */
399 *count = kvm_mips_read_count_running(vcpu, now);
400
401 return now;
402}
403
James Hogane30492b2014-05-29 10:16:35 +0100404/**
405 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
406 * @vcpu: Virtual CPU.
407 * @now: ktime at point of resume.
408 * @count: CP0_Count at point of resume.
409 *
410 * Resumes the timer and updates the timer expiry based on @now and @count.
411 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
412 * parameters need to be changed.
413 *
414 * It is guaranteed that a timer interrupt immediately after resume will be
415 * handled, but not if CP_Compare is exactly at @count. That case is already
416 * handled by kvm_mips_freeze_timer().
417 *
418 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
419 */
420static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
James Hoganbdb7ed82016-06-09 14:19:07 +0100421 ktime_t now, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100422{
423 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100424 u32 compare;
James Hogane30492b2014-05-29 10:16:35 +0100425 u64 delta;
426 ktime_t expire;
427
428 /* Calculate timeout (wrap 0 to 2^32) */
429 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100430 delta = (u64)(u32)(compare - count - 1) + 1;
James Hogane30492b2014-05-29 10:16:35 +0100431 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
432 expire = ktime_add_ns(now, delta);
433
434 /* Update hrtimer to use new timeout */
435 hrtimer_cancel(&vcpu->arch.comparecount_timer);
436 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
437}
438
439/**
James Hogane30492b2014-05-29 10:16:35 +0100440 * kvm_mips_write_count() - Modify the count and update timer.
441 * @vcpu: Virtual CPU.
442 * @count: Guest CP0_Count value to set.
443 *
444 * Sets the CP0_Count value and updates the timer accordingly.
445 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100446void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
James Hogane30492b2014-05-29 10:16:35 +0100447{
448 struct mips_coproc *cop0 = vcpu->arch.cop0;
449 ktime_t now;
450
451 /* Calculate bias */
James Hoganf8239342014-05-29 10:16:37 +0100452 now = kvm_mips_count_time(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100453 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
454
455 if (kvm_mips_count_disabled(vcpu))
456 /* The timer's disabled, adjust the static count */
457 kvm_write_c0_guest_count(cop0, count);
458 else
459 /* Update timeout */
460 kvm_mips_resume_hrtimer(vcpu, now, count);
461}
462
463/**
464 * kvm_mips_init_count() - Initialise timer.
465 * @vcpu: Virtual CPU.
466 *
467 * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
468 * it going if it's enabled.
469 */
470void kvm_mips_init_count(struct kvm_vcpu *vcpu)
471{
472 /* 100 MHz */
473 vcpu->arch.count_hz = 100*1000*1000;
474 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
475 vcpu->arch.count_hz);
476 vcpu->arch.count_dyn_bias = 0;
477
478 /* Starting at 0 */
479 kvm_mips_write_count(vcpu, 0);
480}
481
482/**
James Hoganf74a8e22014-05-29 10:16:38 +0100483 * kvm_mips_set_count_hz() - Update the frequency of the timer.
484 * @vcpu: Virtual CPU.
485 * @count_hz: Frequency of CP0_Count timer in Hz.
486 *
487 * Change the frequency of the CP0_Count timer. This is done atomically so that
488 * CP0_Count is continuous and no timer interrupt is lost.
489 *
490 * Returns: -EINVAL if @count_hz is out of range.
491 * 0 on success.
492 */
493int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
494{
495 struct mips_coproc *cop0 = vcpu->arch.cop0;
496 int dc;
497 ktime_t now;
498 u32 count;
499
500 /* ensure the frequency is in a sensible range... */
501 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
502 return -EINVAL;
503 /* ... and has actually changed */
504 if (vcpu->arch.count_hz == count_hz)
505 return 0;
506
507 /* Safely freeze timer so we can keep it continuous */
508 dc = kvm_mips_count_disabled(vcpu);
509 if (dc) {
510 now = kvm_mips_count_time(vcpu);
511 count = kvm_read_c0_guest_count(cop0);
512 } else {
513 now = kvm_mips_freeze_hrtimer(vcpu, &count);
514 }
515
516 /* Update the frequency */
517 vcpu->arch.count_hz = count_hz;
518 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
519 vcpu->arch.count_dyn_bias = 0;
520
521 /* Calculate adjusted bias so dynamic count is unchanged */
522 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
523
524 /* Update and resume hrtimer */
525 if (!dc)
526 kvm_mips_resume_hrtimer(vcpu, now, count);
527 return 0;
528}
529
530/**
James Hogane30492b2014-05-29 10:16:35 +0100531 * kvm_mips_write_compare() - Modify compare and update timer.
532 * @vcpu: Virtual CPU.
533 * @compare: New CP0_Compare value.
James Hoganb45bacd2016-04-22 10:38:46 +0100534 * @ack: Whether to acknowledge timer interrupt.
James Hogane30492b2014-05-29 10:16:35 +0100535 *
536 * Update CP0_Compare to a new value and update the timeout.
James Hoganb45bacd2016-04-22 10:38:46 +0100537 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
538 * any pending timer interrupt is preserved.
James Hogane30492b2014-05-29 10:16:35 +0100539 */
James Hoganbdb7ed82016-06-09 14:19:07 +0100540void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
James Hogane30492b2014-05-29 10:16:35 +0100541{
542 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganb45bacd2016-04-22 10:38:46 +0100543 int dc;
544 u32 old_compare = kvm_read_c0_guest_compare(cop0);
545 ktime_t now;
James Hogan8cffd192016-06-09 14:19:08 +0100546 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100547
548 /* if unchanged, must just be an ack */
James Hoganb45bacd2016-04-22 10:38:46 +0100549 if (old_compare == compare) {
550 if (!ack)
551 return;
552 kvm_mips_callbacks->dequeue_timer_int(vcpu);
553 kvm_write_c0_guest_compare(cop0, compare);
James Hogane30492b2014-05-29 10:16:35 +0100554 return;
James Hoganb45bacd2016-04-22 10:38:46 +0100555 }
James Hogane30492b2014-05-29 10:16:35 +0100556
James Hoganb45bacd2016-04-22 10:38:46 +0100557 /* freeze_hrtimer() takes care of timer interrupts <= count */
558 dc = kvm_mips_count_disabled(vcpu);
559 if (!dc)
560 now = kvm_mips_freeze_hrtimer(vcpu, &count);
561
562 if (ack)
563 kvm_mips_callbacks->dequeue_timer_int(vcpu);
564
James Hogane30492b2014-05-29 10:16:35 +0100565 kvm_write_c0_guest_compare(cop0, compare);
566
James Hoganb45bacd2016-04-22 10:38:46 +0100567 /* resume_hrtimer() takes care of timer interrupts > count */
568 if (!dc)
569 kvm_mips_resume_hrtimer(vcpu, now, count);
James Hogane30492b2014-05-29 10:16:35 +0100570}
571
572/**
573 * kvm_mips_count_disable() - Disable count.
574 * @vcpu: Virtual CPU.
575 *
576 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
577 * time will be handled but not after.
578 *
James Hoganf8239342014-05-29 10:16:37 +0100579 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
580 * count_ctl.DC has been set (count disabled).
James Hogane30492b2014-05-29 10:16:35 +0100581 *
582 * Returns: The time that the timer was stopped.
583 */
584static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
585{
586 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100587 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100588 ktime_t now;
589
590 /* Stop hrtimer */
591 hrtimer_cancel(&vcpu->arch.comparecount_timer);
592
593 /* Set the static count from the dynamic count, handling pending TI */
594 now = ktime_get();
595 count = kvm_mips_read_count_running(vcpu, now);
596 kvm_write_c0_guest_count(cop0, count);
597
598 return now;
599}
600
601/**
602 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
603 * @vcpu: Virtual CPU.
604 *
605 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
James Hoganf8239342014-05-29 10:16:37 +0100606 * before the final stop time will be handled if the timer isn't disabled by
607 * count_ctl.DC, but not after.
James Hogane30492b2014-05-29 10:16:35 +0100608 *
609 * Assumes CP0_Cause.DC is clear (count enabled).
610 */
611void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
612{
613 struct mips_coproc *cop0 = vcpu->arch.cop0;
614
615 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
James Hoganf8239342014-05-29 10:16:37 +0100616 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
617 kvm_mips_count_disable(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100618}
619
620/**
621 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
622 * @vcpu: Virtual CPU.
623 *
624 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
James Hoganf8239342014-05-29 10:16:37 +0100625 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
626 * potentially before even returning, so the caller should be careful with
627 * ordering of CP0_Cause modifications so as not to lose it.
James Hogane30492b2014-05-29 10:16:35 +0100628 *
629 * Assumes CP0_Cause.DC is set (count disabled).
630 */
631void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
632{
633 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100634 u32 count;
James Hogane30492b2014-05-29 10:16:35 +0100635
636 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
637
638 /*
639 * Set the dynamic count to match the static count.
James Hoganf8239342014-05-29 10:16:37 +0100640 * This starts the hrtimer if count_ctl.DC allows it.
641 * Otherwise it conveniently updates the biases.
James Hogane30492b2014-05-29 10:16:35 +0100642 */
643 count = kvm_read_c0_guest_count(cop0);
644 kvm_mips_write_count(vcpu, count);
645}
646
647/**
James Hoganf8239342014-05-29 10:16:37 +0100648 * kvm_mips_set_count_ctl() - Update the count control KVM register.
649 * @vcpu: Virtual CPU.
650 * @count_ctl: Count control register new value.
651 *
652 * Set the count control KVM register. The timer is updated accordingly.
653 *
654 * Returns: -EINVAL if reserved bits are set.
655 * 0 on success.
656 */
657int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
658{
659 struct mips_coproc *cop0 = vcpu->arch.cop0;
660 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
661 s64 delta;
662 ktime_t expire, now;
James Hogan8cffd192016-06-09 14:19:08 +0100663 u32 count, compare;
James Hoganf8239342014-05-29 10:16:37 +0100664
665 /* Only allow defined bits to be changed */
666 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
667 return -EINVAL;
668
669 /* Apply new value */
670 vcpu->arch.count_ctl = count_ctl;
671
672 /* Master CP0_Count disable */
673 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
674 /* Is CP0_Cause.DC already disabling CP0_Count? */
675 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
676 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
677 /* Just record the current time */
678 vcpu->arch.count_resume = ktime_get();
679 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
680 /* disable timer and record current time */
681 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
682 } else {
683 /*
684 * Calculate timeout relative to static count at resume
685 * time (wrap 0 to 2^32).
686 */
687 count = kvm_read_c0_guest_count(cop0);
688 compare = kvm_read_c0_guest_compare(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100689 delta = (u64)(u32)(compare - count - 1) + 1;
James Hoganf8239342014-05-29 10:16:37 +0100690 delta = div_u64(delta * NSEC_PER_SEC,
691 vcpu->arch.count_hz);
692 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
693
694 /* Handle pending interrupt */
695 now = ktime_get();
696 if (ktime_compare(now, expire) >= 0)
697 /* Nothing should be waiting on the timeout */
698 kvm_mips_callbacks->queue_timer_int(vcpu);
699
700 /* Resume hrtimer without changing bias */
701 count = kvm_mips_read_count_running(vcpu, now);
702 kvm_mips_resume_hrtimer(vcpu, now, count);
703 }
704 }
705
706 return 0;
707}
708
709/**
710 * kvm_mips_set_count_resume() - Update the count resume KVM register.
711 * @vcpu: Virtual CPU.
712 * @count_resume: Count resume register new value.
713 *
714 * Set the count resume KVM register.
715 *
716 * Returns: -EINVAL if out of valid range (0..now).
717 * 0 on success.
718 */
719int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
720{
721 /*
722 * It doesn't make sense for the resume time to be in the future, as it
723 * would be possible for the next interrupt to be more than a full
724 * period in the future.
725 */
726 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
727 return -EINVAL;
728
729 vcpu->arch.count_resume = ns_to_ktime(count_resume);
730 return 0;
731}
732
733/**
James Hogane30492b2014-05-29 10:16:35 +0100734 * kvm_mips_count_timeout() - Push timer forward on timeout.
735 * @vcpu: Virtual CPU.
736 *
737 * Handle an hrtimer event by push the hrtimer forward a period.
738 *
739 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
740 */
741enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
742{
743 /* Add the Count period to the current expiry time */
744 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
745 vcpu->arch.count_period);
746 return HRTIMER_RESTART;
Sanjay Lale685c682012-11-21 18:34:04 -0800747}
748
749enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
750{
751 struct mips_coproc *cop0 = vcpu->arch.cop0;
752 enum emulation_result er = EMULATE_DONE;
753
754 if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
755 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
756 kvm_read_c0_guest_epc(cop0));
757 kvm_clear_c0_guest_status(cop0, ST0_EXL);
758 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
759
760 } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
761 kvm_clear_c0_guest_status(cop0, ST0_ERL);
762 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
763 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700764 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
765 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800766 er = EMULATE_FAIL;
767 }
768
769 return er;
770}
771
772enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
773{
Sanjay Lale685c682012-11-21 18:34:04 -0800774 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
775 vcpu->arch.pending_exceptions);
776
777 ++vcpu->stat.wait_exits;
James Hogan1e09e862016-06-14 09:40:12 +0100778 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
Sanjay Lale685c682012-11-21 18:34:04 -0800779 if (!vcpu->arch.pending_exceptions) {
780 vcpu->arch.wait = 1;
781 kvm_vcpu_block(vcpu);
782
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700783 /*
784 * We we are runnable, then definitely go off to user space to
785 * check if any I/O interrupts are pending.
Sanjay Lale685c682012-11-21 18:34:04 -0800786 */
787 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
788 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
789 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
790 }
791 }
792
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700793 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800794}
795
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700796/*
797 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
798 * we can catch this, if things ever change
Sanjay Lale685c682012-11-21 18:34:04 -0800799 */
800enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
801{
802 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan8cffd192016-06-09 14:19:08 +0100803 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800804
James Hogan8cffd192016-06-09 14:19:08 +0100805 kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700806 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -0800807}
808
809/* Write Guest TLB Entry @ Index */
810enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
811{
812 struct mips_coproc *cop0 = vcpu->arch.cop0;
813 int index = kvm_read_c0_guest_index(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800814 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +0100815 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800816
817 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700818 kvm_debug("%s: illegal index: %d\n", __func__, index);
James Hogan8cffd192016-06-09 14:19:08 +0100819 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700820 pc, index, kvm_read_c0_guest_entryhi(cop0),
821 kvm_read_c0_guest_entrylo0(cop0),
822 kvm_read_c0_guest_entrylo1(cop0),
823 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800824 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
825 }
826
827 tlb = &vcpu->arch.guest_tlb[index];
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700828 /*
829 * Probe the shadow host TLB for the entry being overwritten, if one
830 * matches, invalidate it
831 */
Sanjay Lale685c682012-11-21 18:34:04 -0800832 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
Sanjay Lale685c682012-11-21 18:34:04 -0800833
834 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
835 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +0100836 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
837 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800838
James Hogan8cffd192016-06-09 14:19:08 +0100839 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700840 pc, index, kvm_read_c0_guest_entryhi(cop0),
841 kvm_read_c0_guest_entrylo0(cop0),
842 kvm_read_c0_guest_entrylo1(cop0),
843 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800844
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700845 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800846}
847
848/* Write Guest TLB Entry @ Random Index */
849enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
850{
851 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -0800852 struct kvm_mips_tlb *tlb = NULL;
James Hogan8cffd192016-06-09 14:19:08 +0100853 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800854 int index;
855
Sanjay Lale685c682012-11-21 18:34:04 -0800856 get_random_bytes(&index, sizeof(index));
857 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
Sanjay Lale685c682012-11-21 18:34:04 -0800858
Sanjay Lale685c682012-11-21 18:34:04 -0800859 tlb = &vcpu->arch.guest_tlb[index];
860
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700861 /*
862 * Probe the shadow host TLB for the entry being overwritten, if one
863 * matches, invalidate it
864 */
Sanjay Lale685c682012-11-21 18:34:04 -0800865 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
Sanjay Lale685c682012-11-21 18:34:04 -0800866
867 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
868 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
James Hogan9fbfb062016-06-09 14:19:17 +0100869 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
870 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800871
James Hogan8cffd192016-06-09 14:19:08 +0100872 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700873 pc, index, kvm_read_c0_guest_entryhi(cop0),
874 kvm_read_c0_guest_entrylo0(cop0),
875 kvm_read_c0_guest_entrylo1(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800876
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700877 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800878}
879
880enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
881{
882 struct mips_coproc *cop0 = vcpu->arch.cop0;
883 long entryhi = kvm_read_c0_guest_entryhi(cop0);
James Hogan8cffd192016-06-09 14:19:08 +0100884 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800885 int index = -1;
886
887 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
888
889 kvm_write_c0_guest_index(cop0, index);
890
James Hogan8cffd192016-06-09 14:19:08 +0100891 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
Sanjay Lale685c682012-11-21 18:34:04 -0800892 index);
893
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700894 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800895}
896
James Hoganc7716072014-06-26 15:11:29 +0100897/**
898 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
899 * @vcpu: Virtual CPU.
900 *
901 * Finds the mask of bits which are writable in the guest's Config1 CP0
902 * register, by userland (currently read-only to the guest).
903 */
904unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
905{
James Hogan6cdc65e2015-02-03 13:59:38 +0000906 unsigned int mask = 0;
907
908 /* Permit FPU to be present if FPU is supported */
909 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
910 mask |= MIPS_CONF1_FP;
911
912 return mask;
James Hoganc7716072014-06-26 15:11:29 +0100913}
914
915/**
916 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
917 * @vcpu: Virtual CPU.
918 *
919 * Finds the mask of bits which are writable in the guest's Config3 CP0
920 * register, by userland (currently read-only to the guest).
921 */
922unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
923{
924 /* Config4 is optional */
James Hogan2b6009d2015-02-06 23:01:00 +0000925 unsigned int mask = MIPS_CONF_M;
926
927 /* Permit MSA to be present if MSA is supported */
928 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
929 mask |= MIPS_CONF3_MSA;
930
931 return mask;
James Hoganc7716072014-06-26 15:11:29 +0100932}
933
934/**
935 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
936 * @vcpu: Virtual CPU.
937 *
938 * Finds the mask of bits which are writable in the guest's Config4 CP0
939 * register, by userland (currently read-only to the guest).
940 */
941unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
942{
943 /* Config5 is optional */
944 return MIPS_CONF_M;
945}
946
947/**
948 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
949 * @vcpu: Virtual CPU.
950 *
951 * Finds the mask of bits which are writable in the guest's Config5 CP0
952 * register, by the guest itself.
953 */
954unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
955{
James Hogan6cdc65e2015-02-03 13:59:38 +0000956 unsigned int mask = 0;
957
James Hogan2b6009d2015-02-06 23:01:00 +0000958 /* Permit MSAEn changes if MSA supported and enabled */
959 if (kvm_mips_guest_has_msa(&vcpu->arch))
960 mask |= MIPS_CONF5_MSAEN;
961
James Hogan6cdc65e2015-02-03 13:59:38 +0000962 /*
963 * Permit guest FPU mode changes if FPU is enabled and the relevant
964 * feature exists according to FIR register.
965 */
966 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
967 if (cpu_has_fre)
968 mask |= MIPS_CONF5_FRE;
969 /* We don't support UFR or UFE */
970 }
971
972 return mask;
James Hoganc7716072014-06-26 15:11:29 +0100973}
974
James Hoganbdb7ed82016-06-09 14:19:07 +0100975enum emulation_result kvm_mips_emulate_CP0(u32 inst, u32 *opc, u32 cause,
976 struct kvm_run *run,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700977 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -0800978{
979 struct mips_coproc *cop0 = vcpu->arch.cop0;
980 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +0100981 u32 rt, rd, copz, sel, co_bit, op;
982 unsigned long pc = vcpu->arch.pc;
Sanjay Lale685c682012-11-21 18:34:04 -0800983 unsigned long curr_pc;
984
985 /*
986 * Update PC and hold onto current PC in case there is
987 * an error and we want to rollback the PC
988 */
989 curr_pc = vcpu->arch.pc;
990 er = update_pc(vcpu, cause);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700991 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -0800992 return er;
Sanjay Lale685c682012-11-21 18:34:04 -0800993
994 copz = (inst >> 21) & 0x1f;
995 rt = (inst >> 16) & 0x1f;
996 rd = (inst >> 11) & 0x1f;
997 sel = inst & 0x7;
998 co_bit = (inst >> 25) & 1;
999
Sanjay Lale685c682012-11-21 18:34:04 -08001000 if (co_bit) {
1001 op = (inst) & 0xff;
1002
1003 switch (op) {
1004 case tlbr_op: /* Read indexed TLB entry */
1005 er = kvm_mips_emul_tlbr(vcpu);
1006 break;
1007 case tlbwi_op: /* Write indexed */
1008 er = kvm_mips_emul_tlbwi(vcpu);
1009 break;
1010 case tlbwr_op: /* Write random */
1011 er = kvm_mips_emul_tlbwr(vcpu);
1012 break;
1013 case tlbp_op: /* TLB Probe */
1014 er = kvm_mips_emul_tlbp(vcpu);
1015 break;
1016 case rfe_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001017 kvm_err("!!!COP0_RFE!!!\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001018 break;
1019 case eret_op:
1020 er = kvm_mips_emul_eret(vcpu);
1021 goto dont_update_pc;
1022 break;
1023 case wait_op:
1024 er = kvm_mips_emul_wait(vcpu);
1025 break;
1026 }
1027 } else {
1028 switch (copz) {
1029 case mfc_op:
1030#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1031 cop0->stat[rd][sel]++;
1032#endif
1033 /* Get reg */
1034 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001035 vcpu->arch.gprs[rt] = kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001036 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1037 vcpu->arch.gprs[rt] = 0x0;
1038#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1039 kvm_mips_trans_mfc0(inst, opc, vcpu);
1040#endif
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001041 } else {
Sanjay Lale685c682012-11-21 18:34:04 -08001042 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1043
1044#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1045 kvm_mips_trans_mfc0(inst, opc, vcpu);
1046#endif
1047 }
1048
1049 kvm_debug
James Hogan8cffd192016-06-09 14:19:08 +01001050 ("[%#lx] MFCz[%d][%d], vcpu->arch.gprs[%d]: %#lx\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001051 pc, rd, sel, rt, vcpu->arch.gprs[rt]);
1052
1053 break;
1054
1055 case dmfc_op:
1056 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1057 break;
1058
1059 case mtc_op:
1060#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1061 cop0->stat[rd][sel]++;
1062#endif
1063 if ((rd == MIPS_CP0_TLB_INDEX)
1064 && (vcpu->arch.gprs[rt] >=
1065 KVM_MIPS_GUEST_TLB_SIZE)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001066 kvm_err("Invalid TLB Index: %ld",
1067 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001068 er = EMULATE_FAIL;
1069 break;
1070 }
1071#define C0_EBASE_CORE_MASK 0xff
1072 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
1073 /* Preserve CORE number */
1074 kvm_change_c0_guest_ebase(cop0,
1075 ~(C0_EBASE_CORE_MASK),
1076 vcpu->arch.gprs[rt]);
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001077 kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
1078 kvm_read_c0_guest_ebase(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001079 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
James Hogan8cffd192016-06-09 14:19:08 +01001080 u32 nasid =
Paul Burtonca64c2b2016-05-06 14:36:20 +01001081 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001082 if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
David Daney48c4ac92013-05-13 13:56:44 -07001083 ((kvm_read_c0_guest_entryhi(cop0) &
Paul Burtonca64c2b2016-05-06 14:36:20 +01001084 KVM_ENTRYHI_ASID) != nasid)) {
James Hogan9887d1c2016-06-14 09:40:13 +01001085 trace_kvm_asid_change(vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001086 kvm_read_c0_guest_entryhi(cop0)
James Hogan9887d1c2016-06-14 09:40:13 +01001087 & KVM_ENTRYHI_ASID,
1088 nasid);
Sanjay Lale685c682012-11-21 18:34:04 -08001089
1090 /* Blow away the shadow host TLBs */
1091 kvm_mips_flush_host_tlb(1);
1092 }
1093 kvm_write_c0_guest_entryhi(cop0,
1094 vcpu->arch.gprs[rt]);
1095 }
1096 /* Are we writing to COUNT */
1097 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001098 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001099 goto done;
1100 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
James Hogan8cffd192016-06-09 14:19:08 +01001101 kvm_debug("[%#lx] MTCz, COMPARE %#lx <- %#lx\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001102 pc, kvm_read_c0_guest_compare(cop0),
1103 vcpu->arch.gprs[rt]);
1104
1105 /* If we are writing to COMPARE */
1106 /* Clear pending timer interrupt, if any */
James Hogane30492b2014-05-29 10:16:35 +01001107 kvm_mips_write_compare(vcpu,
James Hoganb45bacd2016-04-22 10:38:46 +01001108 vcpu->arch.gprs[rt],
1109 true);
Sanjay Lale685c682012-11-21 18:34:04 -08001110 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
James Hogan6cdc65e2015-02-03 13:59:38 +00001111 unsigned int old_val, val, change;
1112
1113 old_val = kvm_read_c0_guest_status(cop0);
1114 val = vcpu->arch.gprs[rt];
1115 change = val ^ old_val;
1116
1117 /* Make sure that the NMI bit is never set */
1118 val &= ~ST0_NMI;
1119
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001120 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001121 * Don't allow CU1 or FR to be set unless FPU
1122 * capability enabled and exists in guest
1123 * configuration.
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001124 */
James Hogan6cdc65e2015-02-03 13:59:38 +00001125 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1126 val &= ~(ST0_CU1 | ST0_FR);
1127
1128 /*
1129 * Also don't allow FR to be set if host doesn't
1130 * support it.
1131 */
1132 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1133 val &= ~ST0_FR;
1134
1135
1136 /* Handle changes in FPU mode */
1137 preempt_disable();
1138
1139 /*
1140 * FPU and Vector register state is made
1141 * UNPREDICTABLE by a change of FR, so don't
1142 * even bother saving it.
1143 */
1144 if (change & ST0_FR)
1145 kvm_drop_fpu(vcpu);
1146
1147 /*
James Hogan2b6009d2015-02-06 23:01:00 +00001148 * If MSA state is already live, it is undefined
1149 * how it interacts with FR=0 FPU state, and we
1150 * don't want to hit reserved instruction
1151 * exceptions trying to save the MSA state later
1152 * when CU=1 && FR=1, so play it safe and save
1153 * it first.
1154 */
1155 if (change & ST0_CU1 && !(val & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001156 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001157 kvm_lose_fpu(vcpu);
1158
1159 /*
James Hogan6cdc65e2015-02-03 13:59:38 +00001160 * Propagate CU1 (FPU enable) changes
1161 * immediately if the FPU context is already
1162 * loaded. When disabling we leave the context
1163 * loaded so it can be quickly enabled again in
1164 * the near future.
1165 */
1166 if (change & ST0_CU1 &&
James Hoganf9431762016-06-14 09:40:10 +01001167 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001168 change_c0_status(ST0_CU1, val);
1169
1170 preempt_enable();
1171
1172 kvm_write_c0_guest_status(cop0, val);
Sanjay Lale685c682012-11-21 18:34:04 -08001173
1174#ifdef CONFIG_KVM_MIPS_DYN_TRANS
James Hogan6cdc65e2015-02-03 13:59:38 +00001175 /*
1176 * If FPU present, we need CU1/FR bits to take
1177 * effect fairly soon.
1178 */
1179 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1180 kvm_mips_trans_mtc0(inst, opc, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001181#endif
James Hogan6cdc65e2015-02-03 13:59:38 +00001182 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1183 unsigned int old_val, val, change, wrmask;
1184
1185 old_val = kvm_read_c0_guest_config5(cop0);
1186 val = vcpu->arch.gprs[rt];
1187
1188 /* Only a few bits are writable in Config5 */
1189 wrmask = kvm_mips_config5_wrmask(vcpu);
1190 change = (val ^ old_val) & wrmask;
1191 val = old_val ^ change;
1192
1193
James Hogan2b6009d2015-02-06 23:01:00 +00001194 /* Handle changes in FPU/MSA modes */
James Hogan6cdc65e2015-02-03 13:59:38 +00001195 preempt_disable();
1196
1197 /*
1198 * Propagate FRE changes immediately if the FPU
1199 * context is already loaded.
1200 */
1201 if (change & MIPS_CONF5_FRE &&
James Hoganf9431762016-06-14 09:40:10 +01001202 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
James Hogan6cdc65e2015-02-03 13:59:38 +00001203 change_c0_config5(MIPS_CONF5_FRE, val);
1204
James Hogan2b6009d2015-02-06 23:01:00 +00001205 /*
1206 * Propagate MSAEn changes immediately if the
1207 * MSA context is already loaded. When disabling
1208 * we leave the context loaded so it can be
1209 * quickly enabled again in the near future.
1210 */
1211 if (change & MIPS_CONF5_MSAEN &&
James Hoganf9431762016-06-14 09:40:10 +01001212 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan2b6009d2015-02-06 23:01:00 +00001213 change_c0_config5(MIPS_CONF5_MSAEN,
1214 val);
1215
James Hogan6cdc65e2015-02-03 13:59:38 +00001216 preempt_enable();
1217
1218 kvm_write_c0_guest_config5(cop0, val);
James Hogane30492b2014-05-29 10:16:35 +01001219 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
James Hogan8cffd192016-06-09 14:19:08 +01001220 u32 old_cause, new_cause;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001221
James Hogane30492b2014-05-29 10:16:35 +01001222 old_cause = kvm_read_c0_guest_cause(cop0);
1223 new_cause = vcpu->arch.gprs[rt];
1224 /* Update R/W bits */
1225 kvm_change_c0_guest_cause(cop0, 0x08800300,
1226 new_cause);
1227 /* DC bit enabling/disabling timer? */
1228 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1229 if (new_cause & CAUSEF_DC)
1230 kvm_mips_count_disable_cause(vcpu);
1231 else
1232 kvm_mips_count_enable_cause(vcpu);
1233 }
Sanjay Lale685c682012-11-21 18:34:04 -08001234 } else {
1235 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1236#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1237 kvm_mips_trans_mtc0(inst, opc, vcpu);
1238#endif
1239 }
1240
James Hogan8cffd192016-06-09 14:19:08 +01001241 kvm_debug("[%#lx] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
Sanjay Lale685c682012-11-21 18:34:04 -08001242 rd, sel, cop0->reg[rd][sel]);
1243 break;
1244
1245 case dmtc_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001246 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1247 vcpu->arch.pc, rt, rd, sel);
Sanjay Lale685c682012-11-21 18:34:04 -08001248 er = EMULATE_FAIL;
1249 break;
1250
James Hoganb2c59632015-12-16 23:49:38 +00001251 case mfmc0_op:
Sanjay Lale685c682012-11-21 18:34:04 -08001252#ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1253 cop0->stat[MIPS_CP0_STATUS][0]++;
1254#endif
James Hogancaa1faa2015-12-16 23:49:26 +00001255 if (rt != 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001256 vcpu->arch.gprs[rt] =
1257 kvm_read_c0_guest_status(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08001258 /* EI */
1259 if (inst & 0x20) {
James Hoganb2c59632015-12-16 23:49:38 +00001260 kvm_debug("[%#lx] mfmc0_op: EI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001261 vcpu->arch.pc);
1262 kvm_set_c0_guest_status(cop0, ST0_IE);
1263 } else {
James Hoganb2c59632015-12-16 23:49:38 +00001264 kvm_debug("[%#lx] mfmc0_op: DI\n",
Sanjay Lale685c682012-11-21 18:34:04 -08001265 vcpu->arch.pc);
1266 kvm_clear_c0_guest_status(cop0, ST0_IE);
1267 }
1268
1269 break;
1270
1271 case wrpgpr_op:
1272 {
James Hogan8cffd192016-06-09 14:19:08 +01001273 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1274 u32 pss =
Sanjay Lale685c682012-11-21 18:34:04 -08001275 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001276 /*
1277 * We don't support any shadow register sets, so
1278 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1279 */
Sanjay Lale685c682012-11-21 18:34:04 -08001280 if (css || pss) {
1281 er = EMULATE_FAIL;
1282 break;
1283 }
1284 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1285 vcpu->arch.gprs[rt]);
1286 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1287 }
1288 break;
1289 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001290 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
1291 vcpu->arch.pc, copz);
Sanjay Lale685c682012-11-21 18:34:04 -08001292 er = EMULATE_FAIL;
1293 break;
1294 }
1295 }
1296
1297done:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001298 /* Rollback PC only if emulation was unsuccessful */
1299 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001300 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001301
1302dont_update_pc:
1303 /*
1304 * This is for special instructions whose emulation
1305 * updates the PC, so do not overwrite the PC under
1306 * any circumstances
1307 */
1308
1309 return er;
1310}
1311
James Hoganbdb7ed82016-06-09 14:19:07 +01001312enum emulation_result kvm_mips_emulate_store(u32 inst, u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001313 struct kvm_run *run,
1314 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001315{
1316 enum emulation_result er = EMULATE_DO_MMIO;
James Hogan8cffd192016-06-09 14:19:08 +01001317 u32 op, base, rt;
1318 s16 offset;
1319 u32 bytes;
Sanjay Lale685c682012-11-21 18:34:04 -08001320 void *data = run->mmio.data;
1321 unsigned long curr_pc;
1322
1323 /*
1324 * Update PC and hold onto current PC in case there is
1325 * an error and we want to rollback the PC
1326 */
1327 curr_pc = vcpu->arch.pc;
1328 er = update_pc(vcpu, cause);
1329 if (er == EMULATE_FAIL)
1330 return er;
1331
1332 rt = (inst >> 16) & 0x1f;
1333 base = (inst >> 21) & 0x1f;
James Hogan8cffd192016-06-09 14:19:08 +01001334 offset = (s16)inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001335 op = (inst >> 26) & 0x3f;
1336
1337 switch (op) {
1338 case sb_op:
1339 bytes = 1;
1340 if (bytes > sizeof(run->mmio.data)) {
1341 kvm_err("%s: bad MMIO length: %d\n", __func__,
1342 run->mmio.len);
1343 }
1344 run->mmio.phys_addr =
1345 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1346 host_cp0_badvaddr);
1347 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1348 er = EMULATE_FAIL;
1349 break;
1350 }
1351 run->mmio.len = bytes;
1352 run->mmio.is_write = 1;
1353 vcpu->mmio_needed = 1;
1354 vcpu->mmio_is_write = 1;
1355 *(u8 *) data = vcpu->arch.gprs[rt];
1356 kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1357 vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
James Hogan8cffd192016-06-09 14:19:08 +01001358 *(u8 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001359
1360 break;
1361
1362 case sw_op:
1363 bytes = 4;
1364 if (bytes > sizeof(run->mmio.data)) {
1365 kvm_err("%s: bad MMIO length: %d\n", __func__,
1366 run->mmio.len);
1367 }
1368 run->mmio.phys_addr =
1369 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1370 host_cp0_badvaddr);
1371 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1372 er = EMULATE_FAIL;
1373 break;
1374 }
1375
1376 run->mmio.len = bytes;
1377 run->mmio.is_write = 1;
1378 vcpu->mmio_needed = 1;
1379 vcpu->mmio_is_write = 1;
James Hogan8cffd192016-06-09 14:19:08 +01001380 *(u32 *) data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001381
1382 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1383 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8cffd192016-06-09 14:19:08 +01001384 vcpu->arch.gprs[rt], *(u32 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001385 break;
1386
1387 case sh_op:
1388 bytes = 2;
1389 if (bytes > sizeof(run->mmio.data)) {
1390 kvm_err("%s: bad MMIO length: %d\n", __func__,
1391 run->mmio.len);
1392 }
1393 run->mmio.phys_addr =
1394 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1395 host_cp0_badvaddr);
1396 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1397 er = EMULATE_FAIL;
1398 break;
1399 }
1400
1401 run->mmio.len = bytes;
1402 run->mmio.is_write = 1;
1403 vcpu->mmio_needed = 1;
1404 vcpu->mmio_is_write = 1;
James Hogan8cffd192016-06-09 14:19:08 +01001405 *(u16 *) data = vcpu->arch.gprs[rt];
Sanjay Lale685c682012-11-21 18:34:04 -08001406
1407 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1408 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
James Hogan8cffd192016-06-09 14:19:08 +01001409 vcpu->arch.gprs[rt], *(u32 *) data);
Sanjay Lale685c682012-11-21 18:34:04 -08001410 break;
1411
1412 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001413 kvm_err("Store not yet supported");
Sanjay Lale685c682012-11-21 18:34:04 -08001414 er = EMULATE_FAIL;
1415 break;
1416 }
1417
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001418 /* Rollback PC if emulation was unsuccessful */
1419 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001420 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001421
1422 return er;
1423}
1424
James Hoganbdb7ed82016-06-09 14:19:07 +01001425enum emulation_result kvm_mips_emulate_load(u32 inst, u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001426 struct kvm_run *run,
1427 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001428{
1429 enum emulation_result er = EMULATE_DO_MMIO;
James Hogan8cffd192016-06-09 14:19:08 +01001430 u32 op, base, rt;
1431 s16 offset;
1432 u32 bytes;
Sanjay Lale685c682012-11-21 18:34:04 -08001433
1434 rt = (inst >> 16) & 0x1f;
1435 base = (inst >> 21) & 0x1f;
James Hogan8cffd192016-06-09 14:19:08 +01001436 offset = (s16)inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001437 op = (inst >> 26) & 0x3f;
1438
1439 vcpu->arch.pending_load_cause = cause;
1440 vcpu->arch.io_gpr = rt;
1441
1442 switch (op) {
1443 case lw_op:
1444 bytes = 4;
1445 if (bytes > sizeof(run->mmio.data)) {
1446 kvm_err("%s: bad MMIO length: %d\n", __func__,
1447 run->mmio.len);
1448 er = EMULATE_FAIL;
1449 break;
1450 }
1451 run->mmio.phys_addr =
1452 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1453 host_cp0_badvaddr);
1454 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1455 er = EMULATE_FAIL;
1456 break;
1457 }
1458
1459 run->mmio.len = bytes;
1460 run->mmio.is_write = 0;
1461 vcpu->mmio_needed = 1;
1462 vcpu->mmio_is_write = 0;
1463 break;
1464
1465 case lh_op:
1466 case lhu_op:
1467 bytes = 2;
1468 if (bytes > sizeof(run->mmio.data)) {
1469 kvm_err("%s: bad MMIO length: %d\n", __func__,
1470 run->mmio.len);
1471 er = EMULATE_FAIL;
1472 break;
1473 }
1474 run->mmio.phys_addr =
1475 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1476 host_cp0_badvaddr);
1477 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1478 er = EMULATE_FAIL;
1479 break;
1480 }
1481
1482 run->mmio.len = bytes;
1483 run->mmio.is_write = 0;
1484 vcpu->mmio_needed = 1;
1485 vcpu->mmio_is_write = 0;
1486
1487 if (op == lh_op)
1488 vcpu->mmio_needed = 2;
1489 else
1490 vcpu->mmio_needed = 1;
1491
1492 break;
1493
1494 case lbu_op:
1495 case lb_op:
1496 bytes = 1;
1497 if (bytes > sizeof(run->mmio.data)) {
1498 kvm_err("%s: bad MMIO length: %d\n", __func__,
1499 run->mmio.len);
1500 er = EMULATE_FAIL;
1501 break;
1502 }
1503 run->mmio.phys_addr =
1504 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1505 host_cp0_badvaddr);
1506 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1507 er = EMULATE_FAIL;
1508 break;
1509 }
1510
1511 run->mmio.len = bytes;
1512 run->mmio.is_write = 0;
1513 vcpu->mmio_is_write = 0;
1514
1515 if (op == lb_op)
1516 vcpu->mmio_needed = 2;
1517 else
1518 vcpu->mmio_needed = 1;
1519
1520 break;
1521
1522 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001523 kvm_err("Load not yet supported");
Sanjay Lale685c682012-11-21 18:34:04 -08001524 er = EMULATE_FAIL;
1525 break;
1526 }
1527
1528 return er;
1529}
1530
James Hoganbdb7ed82016-06-09 14:19:07 +01001531enum emulation_result kvm_mips_emulate_cache(u32 inst, u32 *opc,
1532 u32 cause,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001533 struct kvm_run *run,
1534 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001535{
1536 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -08001537 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01001538 u32 cache, op_inst, op, base;
1539 s16 offset;
Sanjay Lale685c682012-11-21 18:34:04 -08001540 struct kvm_vcpu_arch *arch = &vcpu->arch;
1541 unsigned long va;
1542 unsigned long curr_pc;
1543
1544 /*
1545 * Update PC and hold onto current PC in case there is
1546 * an error and we want to rollback the PC
1547 */
1548 curr_pc = vcpu->arch.pc;
1549 er = update_pc(vcpu, cause);
1550 if (er == EMULATE_FAIL)
1551 return er;
1552
1553 base = (inst >> 21) & 0x1f;
1554 op_inst = (inst >> 16) & 0x1f;
James Hogan8cffd192016-06-09 14:19:08 +01001555 offset = (s16)inst;
James Hoganf4956f62015-12-16 23:49:37 +00001556 cache = op_inst & CacheOp_Cache;
1557 op = op_inst & CacheOp_Op;
Sanjay Lale685c682012-11-21 18:34:04 -08001558
1559 va = arch->gprs[base] + offset;
1560
1561 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1562 cache, op, base, arch->gprs[base], offset);
1563
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001564 /*
1565 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1566 * invalidate the caches entirely by stepping through all the
1567 * ways/indexes
Sanjay Lale685c682012-11-21 18:34:04 -08001568 */
James Hoganf4956f62015-12-16 23:49:37 +00001569 if (op == Index_Writeback_Inv) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001570 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1571 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1572 arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001573
James Hoganf4956f62015-12-16 23:49:37 +00001574 if (cache == Cache_D)
Sanjay Lale685c682012-11-21 18:34:04 -08001575 r4k_blast_dcache();
James Hoganf4956f62015-12-16 23:49:37 +00001576 else if (cache == Cache_I)
Sanjay Lale685c682012-11-21 18:34:04 -08001577 r4k_blast_icache();
1578 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001579 kvm_err("%s: unsupported CACHE INDEX operation\n",
1580 __func__);
Sanjay Lale685c682012-11-21 18:34:04 -08001581 return EMULATE_FAIL;
1582 }
1583
1584#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1585 kvm_mips_trans_cache_index(inst, opc, vcpu);
1586#endif
1587 goto done;
1588 }
1589
1590 preempt_disable();
1591 if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001592 if (kvm_mips_host_tlb_lookup(vcpu, va) < 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001593 kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001594 } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
1595 KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
1596 int index;
1597
1598 /* If an entry already exists then skip */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001599 if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001600 goto skip_fault;
Sanjay Lale685c682012-11-21 18:34:04 -08001601
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001602 /*
1603 * If address not in the guest TLB, then give the guest a fault,
1604 * the resulting handler will do the right thing
Sanjay Lale685c682012-11-21 18:34:04 -08001605 */
1606 index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001607 (kvm_read_c0_guest_entryhi
Paul Burtonca64c2b2016-05-06 14:36:20 +01001608 (cop0) & KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08001609
1610 if (index < 0) {
Sanjay Lale685c682012-11-21 18:34:04 -08001611 vcpu->arch.host_cp0_badvaddr = va;
James Hogan6df82a72016-06-09 10:50:46 +01001612 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001613 er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
1614 vcpu);
1615 preempt_enable();
1616 goto dont_update_pc;
1617 } else {
1618 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001619 /*
1620 * Check if the entry is valid, if not then setup a TLB
1621 * invalid exception to the guest
1622 */
Sanjay Lale685c682012-11-21 18:34:04 -08001623 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan6df82a72016-06-09 10:50:46 +01001624 vcpu->arch.host_cp0_badvaddr = va;
1625 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001626 er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
1627 run, vcpu);
1628 preempt_enable();
1629 goto dont_update_pc;
1630 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001631 /*
1632 * We fault an entry from the guest tlb to the
1633 * shadow host TLB
1634 */
James Hogan26ee17f2016-06-09 14:19:13 +01001635 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -08001636 }
1637 }
1638 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001639 kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1640 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001641 er = EMULATE_FAIL;
1642 preempt_enable();
James Hogancc81e942016-06-09 10:50:45 +01001643 goto done;
Sanjay Lale685c682012-11-21 18:34:04 -08001644
1645 }
1646
1647skip_fault:
1648 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
James Hoganf4956f62015-12-16 23:49:37 +00001649 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
Sanjay Lale685c682012-11-21 18:34:04 -08001650 flush_dcache_line(va);
1651
1652#ifdef CONFIG_KVM_MIPS_DYN_TRANS
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001653 /*
1654 * Replace the CACHE instruction, with a SYNCI, not the same,
1655 * but avoids a trap
1656 */
Sanjay Lale685c682012-11-21 18:34:04 -08001657 kvm_mips_trans_cache_va(inst, opc, vcpu);
1658#endif
James Hoganf4956f62015-12-16 23:49:37 +00001659 } else if (op_inst == Hit_Invalidate_I) {
Sanjay Lale685c682012-11-21 18:34:04 -08001660 flush_dcache_line(va);
1661 flush_icache_line(va);
1662
1663#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1664 /* Replace the CACHE instruction, with a SYNCI */
1665 kvm_mips_trans_cache_va(inst, opc, vcpu);
1666#endif
1667 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001668 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1669 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001670 er = EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -08001671 }
1672
1673 preempt_enable();
James Hogancc81e942016-06-09 10:50:45 +01001674done:
1675 /* Rollback PC only if emulation was unsuccessful */
1676 if (er == EMULATE_FAIL)
1677 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001678
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001679dont_update_pc:
James Hogancc81e942016-06-09 10:50:45 +01001680 /*
1681 * This is for exceptions whose emulation updates the PC, so do not
1682 * overwrite the PC under any circumstances
1683 */
1684
Sanjay Lale685c682012-11-21 18:34:04 -08001685 return er;
1686}
1687
James Hogan31cf7492016-06-09 14:19:09 +01001688enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001689 struct kvm_run *run,
1690 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001691{
1692 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01001693 u32 inst;
Sanjay Lale685c682012-11-21 18:34:04 -08001694
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001695 /* Fetch the instruction. */
1696 if (cause & CAUSEF_BD)
Sanjay Lale685c682012-11-21 18:34:04 -08001697 opc += 1;
Sanjay Lale685c682012-11-21 18:34:04 -08001698
1699 inst = kvm_get_inst(opc, vcpu);
1700
1701 switch (((union mips_instruction)inst).r_format.opcode) {
1702 case cop0_op:
1703 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1704 break;
1705 case sb_op:
1706 case sh_op:
1707 case sw_op:
1708 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1709 break;
1710 case lb_op:
1711 case lbu_op:
1712 case lhu_op:
1713 case lh_op:
1714 case lw_op:
1715 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1716 break;
1717
1718 case cache_op:
1719 ++vcpu->stat.cache_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001720 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
Sanjay Lale685c682012-11-21 18:34:04 -08001721 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1722 break;
1723
1724 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001725 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
1726 inst);
Sanjay Lale685c682012-11-21 18:34:04 -08001727 kvm_arch_vcpu_dump_regs(vcpu);
1728 er = EMULATE_FAIL;
1729 break;
1730 }
1731
1732 return er;
1733}
1734
James Hogan31cf7492016-06-09 14:19:09 +01001735enum emulation_result kvm_mips_emulate_syscall(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001736 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001737 struct kvm_run *run,
1738 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001739{
1740 struct mips_coproc *cop0 = vcpu->arch.cop0;
1741 struct kvm_vcpu_arch *arch = &vcpu->arch;
1742 enum emulation_result er = EMULATE_DONE;
1743
1744 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1745 /* save old pc */
1746 kvm_write_c0_guest_epc(cop0, arch->pc);
1747 kvm_set_c0_guest_status(cop0, ST0_EXL);
1748
1749 if (cause & CAUSEF_BD)
1750 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1751 else
1752 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1753
1754 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1755
1756 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001757 (EXCCODE_SYS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001758
1759 /* Set PC to the exception entry point */
1760 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1761
1762 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001763 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001764 er = EMULATE_FAIL;
1765 }
1766
1767 return er;
1768}
1769
James Hogan31cf7492016-06-09 14:19:09 +01001770enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001771 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001772 struct kvm_run *run,
1773 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001774{
1775 struct mips_coproc *cop0 = vcpu->arch.cop0;
1776 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001777 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001778 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001779
1780 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1781 /* save old pc */
1782 kvm_write_c0_guest_epc(cop0, arch->pc);
1783 kvm_set_c0_guest_status(cop0, ST0_EXL);
1784
1785 if (cause & CAUSEF_BD)
1786 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1787 else
1788 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1789
1790 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1791 arch->pc);
1792
1793 /* set pc to the exception entry point */
1794 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1795
1796 } else {
1797 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1798 arch->pc);
1799
1800 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1801 }
1802
1803 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001804 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001805
1806 /* setup badvaddr, context and entryhi registers for the guest */
1807 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1808 /* XXXKYMA: is the context register used by linux??? */
1809 kvm_write_c0_guest_entryhi(cop0, entryhi);
1810 /* Blow away the shadow host TLBs */
1811 kvm_mips_flush_host_tlb(1);
1812
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001813 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001814}
1815
James Hogan31cf7492016-06-09 14:19:09 +01001816enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001817 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001818 struct kvm_run *run,
1819 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001820{
1821 struct mips_coproc *cop0 = vcpu->arch.cop0;
1822 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001823 unsigned long entryhi =
1824 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001825 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001826
1827 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1828 /* save old pc */
1829 kvm_write_c0_guest_epc(cop0, arch->pc);
1830 kvm_set_c0_guest_status(cop0, ST0_EXL);
1831
1832 if (cause & CAUSEF_BD)
1833 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1834 else
1835 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1836
1837 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
1838 arch->pc);
1839
1840 /* set pc to the exception entry point */
1841 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1842
1843 } else {
1844 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1845 arch->pc);
1846 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1847 }
1848
1849 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001850 (EXCCODE_TLBL << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001851
1852 /* setup badvaddr, context and entryhi registers for the guest */
1853 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1854 /* XXXKYMA: is the context register used by linux??? */
1855 kvm_write_c0_guest_entryhi(cop0, entryhi);
1856 /* Blow away the shadow host TLBs */
1857 kvm_mips_flush_host_tlb(1);
1858
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001859 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001860}
1861
James Hogan31cf7492016-06-09 14:19:09 +01001862enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001863 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001864 struct kvm_run *run,
1865 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001866{
1867 struct mips_coproc *cop0 = vcpu->arch.cop0;
1868 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001869 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001870 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001871
1872 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1873 /* save old pc */
1874 kvm_write_c0_guest_epc(cop0, arch->pc);
1875 kvm_set_c0_guest_status(cop0, ST0_EXL);
1876
1877 if (cause & CAUSEF_BD)
1878 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1879 else
1880 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1881
1882 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1883 arch->pc);
1884
1885 /* Set PC to the exception entry point */
1886 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1887 } else {
1888 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1889 arch->pc);
1890 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1891 }
1892
1893 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001894 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001895
1896 /* setup badvaddr, context and entryhi registers for the guest */
1897 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1898 /* XXXKYMA: is the context register used by linux??? */
1899 kvm_write_c0_guest_entryhi(cop0, entryhi);
1900 /* Blow away the shadow host TLBs */
1901 kvm_mips_flush_host_tlb(1);
1902
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001903 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001904}
1905
James Hogan31cf7492016-06-09 14:19:09 +01001906enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001907 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001908 struct kvm_run *run,
1909 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001910{
1911 struct mips_coproc *cop0 = vcpu->arch.cop0;
1912 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001913 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001914 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001915
1916 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1917 /* save old pc */
1918 kvm_write_c0_guest_epc(cop0, arch->pc);
1919 kvm_set_c0_guest_status(cop0, ST0_EXL);
1920
1921 if (cause & CAUSEF_BD)
1922 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1923 else
1924 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1925
1926 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1927 arch->pc);
1928
1929 /* Set PC to the exception entry point */
1930 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1931 } else {
1932 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1933 arch->pc);
1934 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1935 }
1936
1937 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00001938 (EXCCODE_TLBS << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08001939
1940 /* setup badvaddr, context and entryhi registers for the guest */
1941 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1942 /* XXXKYMA: is the context register used by linux??? */
1943 kvm_write_c0_guest_entryhi(cop0, entryhi);
1944 /* Blow away the shadow host TLBs */
1945 kvm_mips_flush_host_tlb(1);
1946
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001947 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001948}
1949
1950/* TLBMOD: store into address matching TLB with Dirty bit off */
James Hogan31cf7492016-06-09 14:19:09 +01001951enum emulation_result kvm_mips_handle_tlbmod(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001952 struct kvm_run *run,
1953 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001954{
1955 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001956#ifdef DEBUG
James Hogan3d654832014-05-29 10:16:41 +01001957 struct mips_coproc *cop0 = vcpu->arch.cop0;
1958 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001959 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
James Hogan3d654832014-05-29 10:16:41 +01001960 int index;
1961
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001962 /* If address not in the guest TLB, then we are in trouble */
Sanjay Lale685c682012-11-21 18:34:04 -08001963 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
1964 if (index < 0) {
1965 /* XXXKYMA Invalidate and retry */
1966 kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
1967 kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
1968 __func__, entryhi);
1969 kvm_mips_dump_guest_tlbs(vcpu);
1970 kvm_mips_dump_host_tlbs();
1971 return EMULATE_FAIL;
1972 }
1973#endif
1974
1975 er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
1976 return er;
1977}
1978
James Hogan31cf7492016-06-09 14:19:09 +01001979enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01001980 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001981 struct kvm_run *run,
1982 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001983{
1984 struct mips_coproc *cop0 = vcpu->arch.cop0;
1985 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01001986 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
Sanjay Lale685c682012-11-21 18:34:04 -08001987 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001988
1989 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1990 /* save old pc */
1991 kvm_write_c0_guest_epc(cop0, arch->pc);
1992 kvm_set_c0_guest_status(cop0, ST0_EXL);
1993
1994 if (cause & CAUSEF_BD)
1995 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1996 else
1997 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1998
1999 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2000 arch->pc);
2001
2002 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2003 } else {
2004 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2005 arch->pc);
2006 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2007 }
2008
James Hogan16d100db2015-12-16 23:49:33 +00002009 kvm_change_c0_guest_cause(cop0, (0xff),
2010 (EXCCODE_MOD << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002011
2012 /* setup badvaddr, context and entryhi registers for the guest */
2013 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2014 /* XXXKYMA: is the context register used by linux??? */
2015 kvm_write_c0_guest_entryhi(cop0, entryhi);
2016 /* Blow away the shadow host TLBs */
2017 kvm_mips_flush_host_tlb(1);
2018
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002019 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002020}
2021
James Hogan31cf7492016-06-09 14:19:09 +01002022enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002023 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002024 struct kvm_run *run,
2025 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002026{
2027 struct mips_coproc *cop0 = vcpu->arch.cop0;
2028 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08002029
2030 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2031 /* save old pc */
2032 kvm_write_c0_guest_epc(cop0, arch->pc);
2033 kvm_set_c0_guest_status(cop0, ST0_EXL);
2034
2035 if (cause & CAUSEF_BD)
2036 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2037 else
2038 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2039
2040 }
2041
2042 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2043
2044 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002045 (EXCCODE_CPU << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002046 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2047
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07002048 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002049}
2050
James Hogan31cf7492016-06-09 14:19:09 +01002051enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002052 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002053 struct kvm_run *run,
2054 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002055{
2056 struct mips_coproc *cop0 = vcpu->arch.cop0;
2057 struct kvm_vcpu_arch *arch = &vcpu->arch;
2058 enum emulation_result er = EMULATE_DONE;
2059
2060 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2061 /* save old pc */
2062 kvm_write_c0_guest_epc(cop0, arch->pc);
2063 kvm_set_c0_guest_status(cop0, ST0_EXL);
2064
2065 if (cause & CAUSEF_BD)
2066 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2067 else
2068 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2069
2070 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2071
2072 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002073 (EXCCODE_RI << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002074
2075 /* Set PC to the exception entry point */
2076 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2077
2078 } else {
2079 kvm_err("Trying to deliver RI when EXL is already set\n");
2080 er = EMULATE_FAIL;
2081 }
2082
2083 return er;
2084}
2085
James Hogan31cf7492016-06-09 14:19:09 +01002086enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002087 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002088 struct kvm_run *run,
2089 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002090{
2091 struct mips_coproc *cop0 = vcpu->arch.cop0;
2092 struct kvm_vcpu_arch *arch = &vcpu->arch;
2093 enum emulation_result er = EMULATE_DONE;
2094
2095 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2096 /* save old pc */
2097 kvm_write_c0_guest_epc(cop0, arch->pc);
2098 kvm_set_c0_guest_status(cop0, ST0_EXL);
2099
2100 if (cause & CAUSEF_BD)
2101 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2102 else
2103 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2104
2105 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2106
2107 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002108 (EXCCODE_BP << CAUSEB_EXCCODE));
Sanjay Lale685c682012-11-21 18:34:04 -08002109
2110 /* Set PC to the exception entry point */
2111 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2112
2113 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002114 kvm_err("Trying to deliver BP when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002115 er = EMULATE_FAIL;
2116 }
2117
2118 return er;
2119}
2120
James Hogan31cf7492016-06-09 14:19:09 +01002121enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002122 u32 *opc,
James Hogan0a560422015-02-06 16:03:57 +00002123 struct kvm_run *run,
2124 struct kvm_vcpu *vcpu)
2125{
2126 struct mips_coproc *cop0 = vcpu->arch.cop0;
2127 struct kvm_vcpu_arch *arch = &vcpu->arch;
2128 enum emulation_result er = EMULATE_DONE;
2129
2130 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2131 /* save old pc */
2132 kvm_write_c0_guest_epc(cop0, arch->pc);
2133 kvm_set_c0_guest_status(cop0, ST0_EXL);
2134
2135 if (cause & CAUSEF_BD)
2136 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2137 else
2138 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2139
2140 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2141
2142 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002143 (EXCCODE_TR << CAUSEB_EXCCODE));
James Hogan0a560422015-02-06 16:03:57 +00002144
2145 /* Set PC to the exception entry point */
2146 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2147
2148 } else {
2149 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2150 er = EMULATE_FAIL;
2151 }
2152
2153 return er;
2154}
2155
James Hogan31cf7492016-06-09 14:19:09 +01002156enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002157 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002158 struct kvm_run *run,
2159 struct kvm_vcpu *vcpu)
2160{
2161 struct mips_coproc *cop0 = vcpu->arch.cop0;
2162 struct kvm_vcpu_arch *arch = &vcpu->arch;
2163 enum emulation_result er = EMULATE_DONE;
2164
2165 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2166 /* save old pc */
2167 kvm_write_c0_guest_epc(cop0, arch->pc);
2168 kvm_set_c0_guest_status(cop0, ST0_EXL);
2169
2170 if (cause & CAUSEF_BD)
2171 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2172 else
2173 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2174
2175 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2176
2177 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002178 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002179
2180 /* Set PC to the exception entry point */
2181 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2182
2183 } else {
2184 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2185 er = EMULATE_FAIL;
2186 }
2187
2188 return er;
2189}
2190
James Hogan31cf7492016-06-09 14:19:09 +01002191enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002192 u32 *opc,
James Hogan1c0cd662015-02-06 10:56:27 +00002193 struct kvm_run *run,
2194 struct kvm_vcpu *vcpu)
2195{
2196 struct mips_coproc *cop0 = vcpu->arch.cop0;
2197 struct kvm_vcpu_arch *arch = &vcpu->arch;
2198 enum emulation_result er = EMULATE_DONE;
2199
2200 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2201 /* save old pc */
2202 kvm_write_c0_guest_epc(cop0, arch->pc);
2203 kvm_set_c0_guest_status(cop0, ST0_EXL);
2204
2205 if (cause & CAUSEF_BD)
2206 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2207 else
2208 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2209
2210 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2211
2212 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002213 (EXCCODE_FPE << CAUSEB_EXCCODE));
James Hogan1c0cd662015-02-06 10:56:27 +00002214
2215 /* Set PC to the exception entry point */
2216 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2217
2218 } else {
2219 kvm_err("Trying to deliver FPE when EXL is already set\n");
2220 er = EMULATE_FAIL;
2221 }
2222
2223 return er;
2224}
2225
James Hogan31cf7492016-06-09 14:19:09 +01002226enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002227 u32 *opc,
James Hoganc2537ed2015-02-06 10:56:27 +00002228 struct kvm_run *run,
2229 struct kvm_vcpu *vcpu)
2230{
2231 struct mips_coproc *cop0 = vcpu->arch.cop0;
2232 struct kvm_vcpu_arch *arch = &vcpu->arch;
2233 enum emulation_result er = EMULATE_DONE;
2234
2235 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2236 /* save old pc */
2237 kvm_write_c0_guest_epc(cop0, arch->pc);
2238 kvm_set_c0_guest_status(cop0, ST0_EXL);
2239
2240 if (cause & CAUSEF_BD)
2241 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2242 else
2243 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2244
2245 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2246
2247 kvm_change_c0_guest_cause(cop0, (0xff),
James Hogan16d100db2015-12-16 23:49:33 +00002248 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
James Hoganc2537ed2015-02-06 10:56:27 +00002249
2250 /* Set PC to the exception entry point */
2251 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2252
2253 } else {
2254 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2255 er = EMULATE_FAIL;
2256 }
2257
2258 return er;
2259}
2260
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002261/* ll/sc, rdhwr, sync emulation */
Sanjay Lale685c682012-11-21 18:34:04 -08002262
2263#define OPCODE 0xfc000000
2264#define BASE 0x03e00000
2265#define RT 0x001f0000
2266#define OFFSET 0x0000ffff
2267#define LL 0xc0000000
2268#define SC 0xe0000000
2269#define SPEC0 0x00000000
2270#define SPEC3 0x7c000000
2271#define RD 0x0000f800
2272#define FUNC 0x0000003f
2273#define SYNC 0x0000000f
2274#define RDHWR 0x0000003b
2275
James Hogan31cf7492016-06-09 14:19:09 +01002276enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002277 struct kvm_run *run,
2278 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002279{
2280 struct mips_coproc *cop0 = vcpu->arch.cop0;
2281 struct kvm_vcpu_arch *arch = &vcpu->arch;
2282 enum emulation_result er = EMULATE_DONE;
2283 unsigned long curr_pc;
James Hogan8cffd192016-06-09 14:19:08 +01002284 u32 inst;
Sanjay Lale685c682012-11-21 18:34:04 -08002285
2286 /*
2287 * Update PC and hold onto current PC in case there is
2288 * an error and we want to rollback the PC
2289 */
2290 curr_pc = vcpu->arch.pc;
2291 er = update_pc(vcpu, cause);
2292 if (er == EMULATE_FAIL)
2293 return er;
2294
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002295 /* Fetch the instruction. */
Sanjay Lale685c682012-11-21 18:34:04 -08002296 if (cause & CAUSEF_BD)
2297 opc += 1;
2298
2299 inst = kvm_get_inst(opc, vcpu);
2300
2301 if (inst == KVM_INVALID_INST) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002302 kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
Sanjay Lale685c682012-11-21 18:34:04 -08002303 return EMULATE_FAIL;
2304 }
2305
2306 if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
James Hogan26f4f3b2014-03-14 13:06:09 +00002307 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002308 int rd = (inst & RD) >> 11;
2309 int rt = (inst & RT) >> 16;
James Hogan26f4f3b2014-03-14 13:06:09 +00002310 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2311 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2312 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2313 rd, opc);
2314 goto emulate_ri;
2315 }
Sanjay Lale685c682012-11-21 18:34:04 -08002316 switch (rd) {
2317 case 0: /* CPU number */
2318 arch->gprs[rt] = 0;
2319 break;
2320 case 1: /* SYNCI length */
2321 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2322 current_cpu_data.icache.linesz);
2323 break;
2324 case 2: /* Read count register */
James Hogane30492b2014-05-29 10:16:35 +01002325 arch->gprs[rt] = kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002326 break;
2327 case 3: /* Count register resolution */
2328 switch (current_cpu_data.cputype) {
2329 case CPU_20KC:
2330 case CPU_25KF:
2331 arch->gprs[rt] = 1;
2332 break;
2333 default:
2334 arch->gprs[rt] = 2;
2335 }
2336 break;
2337 case 29:
Sanjay Lale685c682012-11-21 18:34:04 -08002338 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08002339 break;
2340
2341 default:
James Hogan15505672014-03-14 13:06:07 +00002342 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
James Hogan26f4f3b2014-03-14 13:06:09 +00002343 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002344 }
2345 } else {
James Hogan15505672014-03-14 13:06:07 +00002346 kvm_debug("Emulate RI not supported @ %p: %#x\n", opc, inst);
James Hogan26f4f3b2014-03-14 13:06:09 +00002347 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002348 }
2349
James Hogan26f4f3b2014-03-14 13:06:09 +00002350 return EMULATE_DONE;
2351
2352emulate_ri:
Sanjay Lale685c682012-11-21 18:34:04 -08002353 /*
James Hogan26f4f3b2014-03-14 13:06:09 +00002354 * Rollback PC (if in branch delay slot then the PC already points to
2355 * branch target), and pass the RI exception to the guest OS.
Sanjay Lale685c682012-11-21 18:34:04 -08002356 */
James Hogan26f4f3b2014-03-14 13:06:09 +00002357 vcpu->arch.pc = curr_pc;
2358 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002359}
2360
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002361enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2362 struct kvm_run *run)
Sanjay Lale685c682012-11-21 18:34:04 -08002363{
2364 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2365 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08002366
2367 if (run->mmio.len > sizeof(*gpr)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002368 kvm_err("Bad MMIO length: %d", run->mmio.len);
Sanjay Lale685c682012-11-21 18:34:04 -08002369 er = EMULATE_FAIL;
2370 goto done;
2371 }
2372
Sanjay Lale685c682012-11-21 18:34:04 -08002373 er = update_pc(vcpu, vcpu->arch.pending_load_cause);
2374 if (er == EMULATE_FAIL)
2375 return er;
2376
2377 switch (run->mmio.len) {
2378 case 4:
James Hogan8cffd192016-06-09 14:19:08 +01002379 *gpr = *(s32 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002380 break;
2381
2382 case 2:
2383 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002384 *gpr = *(s16 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002385 else
James Hogan8cffd192016-06-09 14:19:08 +01002386 *gpr = *(u16 *)run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002387
2388 break;
2389 case 1:
2390 if (vcpu->mmio_needed == 2)
James Hogan8cffd192016-06-09 14:19:08 +01002391 *gpr = *(s8 *) run->mmio.data;
Sanjay Lale685c682012-11-21 18:34:04 -08002392 else
2393 *gpr = *(u8 *) run->mmio.data;
2394 break;
2395 }
2396
2397 if (vcpu->arch.pending_load_cause & CAUSEF_BD)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002398 kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
2399 vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
2400 vcpu->mmio_needed);
Sanjay Lale685c682012-11-21 18:34:04 -08002401
2402done:
2403 return er;
2404}
2405
James Hogan31cf7492016-06-09 14:19:09 +01002406static enum emulation_result kvm_mips_emulate_exc(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002407 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002408 struct kvm_run *run,
2409 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002410{
James Hogan8cffd192016-06-09 14:19:08 +01002411 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002412 struct mips_coproc *cop0 = vcpu->arch.cop0;
2413 struct kvm_vcpu_arch *arch = &vcpu->arch;
2414 enum emulation_result er = EMULATE_DONE;
2415
2416 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2417 /* save old pc */
2418 kvm_write_c0_guest_epc(cop0, arch->pc);
2419 kvm_set_c0_guest_status(cop0, ST0_EXL);
2420
2421 if (cause & CAUSEF_BD)
2422 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2423 else
2424 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2425
2426 kvm_change_c0_guest_cause(cop0, (0xff),
2427 (exccode << CAUSEB_EXCCODE));
2428
2429 /* Set PC to the exception entry point */
2430 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2431 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2432
2433 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2434 exccode, kvm_read_c0_guest_epc(cop0),
2435 kvm_read_c0_guest_badvaddr(cop0));
2436 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002437 kvm_err("Trying to deliver EXC when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002438 er = EMULATE_FAIL;
2439 }
2440
2441 return er;
2442}
2443
James Hogan31cf7492016-06-09 14:19:09 +01002444enum emulation_result kvm_mips_check_privilege(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002445 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002446 struct kvm_run *run,
2447 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002448{
2449 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002450 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002451 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2452
2453 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2454
2455 if (usermode) {
2456 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00002457 case EXCCODE_INT:
2458 case EXCCODE_SYS:
2459 case EXCCODE_BP:
2460 case EXCCODE_RI:
2461 case EXCCODE_TR:
2462 case EXCCODE_MSAFPE:
2463 case EXCCODE_FPE:
2464 case EXCCODE_MSADIS:
Sanjay Lale685c682012-11-21 18:34:04 -08002465 break;
2466
James Hogan16d100db2015-12-16 23:49:33 +00002467 case EXCCODE_CPU:
Sanjay Lale685c682012-11-21 18:34:04 -08002468 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2469 er = EMULATE_PRIV_FAIL;
2470 break;
2471
James Hogan16d100db2015-12-16 23:49:33 +00002472 case EXCCODE_MOD:
Sanjay Lale685c682012-11-21 18:34:04 -08002473 break;
2474
James Hogan16d100db2015-12-16 23:49:33 +00002475 case EXCCODE_TLBL:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002476 /*
2477 * We we are accessing Guest kernel space, then send an
2478 * address error exception to the guest
2479 */
Sanjay Lale685c682012-11-21 18:34:04 -08002480 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002481 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2482 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002483 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002484 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002485 er = EMULATE_PRIV_FAIL;
2486 }
2487 break;
2488
James Hogan16d100db2015-12-16 23:49:33 +00002489 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002490 /*
2491 * We we are accessing Guest kernel space, then send an
2492 * address error exception to the guest
2493 */
Sanjay Lale685c682012-11-21 18:34:04 -08002494 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002495 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2496 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002497 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002498 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002499 er = EMULATE_PRIV_FAIL;
2500 }
2501 break;
2502
James Hogan16d100db2015-12-16 23:49:33 +00002503 case EXCCODE_ADES:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002504 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2505 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002506 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2507 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002508 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002509 }
2510 er = EMULATE_PRIV_FAIL;
2511 break;
James Hogan16d100db2015-12-16 23:49:33 +00002512 case EXCCODE_ADEL:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002513 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2514 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002515 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2516 cause &= ~0xff;
James Hogan16d100db2015-12-16 23:49:33 +00002517 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
Sanjay Lale685c682012-11-21 18:34:04 -08002518 }
2519 er = EMULATE_PRIV_FAIL;
2520 break;
2521 default:
2522 er = EMULATE_PRIV_FAIL;
2523 break;
2524 }
2525 }
2526
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002527 if (er == EMULATE_PRIV_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08002528 kvm_mips_emulate_exc(cause, opc, run, vcpu);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002529
Sanjay Lale685c682012-11-21 18:34:04 -08002530 return er;
2531}
2532
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002533/*
2534 * User Address (UA) fault, this could happen if
Sanjay Lale685c682012-11-21 18:34:04 -08002535 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2536 * case we pass on the fault to the guest kernel and let it handle it.
2537 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2538 * case we inject the TLB from the Guest TLB into the shadow host TLB
2539 */
James Hogan31cf7492016-06-09 14:19:09 +01002540enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
James Hoganbdb7ed82016-06-09 14:19:07 +01002541 u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002542 struct kvm_run *run,
2543 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002544{
2545 enum emulation_result er = EMULATE_DONE;
James Hogan8cffd192016-06-09 14:19:08 +01002546 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
Sanjay Lale685c682012-11-21 18:34:04 -08002547 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2548 int index;
2549
James Hogane4e94c02016-06-09 14:19:05 +01002550 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2551 vcpu->arch.host_cp0_badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002552
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002553 /*
2554 * KVM would not have got the exception if this entry was valid in the
2555 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2556 * send the guest an exception. The guest exc handler should then inject
2557 * an entry into the guest TLB.
Sanjay Lale685c682012-11-21 18:34:04 -08002558 */
2559 index = kvm_mips_guest_tlb_lookup(vcpu,
James Hogancaa1faa2015-12-16 23:49:26 +00002560 (va & VPN2_MASK) |
Paul Burtonca64c2b2016-05-06 14:36:20 +01002561 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2562 KVM_ENTRYHI_ASID));
Sanjay Lale685c682012-11-21 18:34:04 -08002563 if (index < 0) {
James Hogan16d100db2015-12-16 23:49:33 +00002564 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002565 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002566 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002567 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2568 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002569 kvm_err("%s: invalid exc code: %d\n", __func__,
2570 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002571 er = EMULATE_FAIL;
2572 }
2573 } else {
2574 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2575
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002576 /*
2577 * Check if the entry is valid, if not then setup a TLB invalid
2578 * exception to the guest
2579 */
Sanjay Lale685c682012-11-21 18:34:04 -08002580 if (!TLB_IS_VALID(*tlb, va)) {
James Hogan16d100db2015-12-16 23:49:33 +00002581 if (exccode == EXCCODE_TLBL) {
Sanjay Lale685c682012-11-21 18:34:04 -08002582 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2583 vcpu);
James Hogan16d100db2015-12-16 23:49:33 +00002584 } else if (exccode == EXCCODE_TLBS) {
Sanjay Lale685c682012-11-21 18:34:04 -08002585 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2586 vcpu);
2587 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002588 kvm_err("%s: invalid exc code: %d\n", __func__,
2589 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002590 er = EMULATE_FAIL;
2591 }
2592 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002593 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
James Hogan9fbfb062016-06-09 14:19:17 +01002594 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002595 /*
2596 * OK we have a Guest TLB entry, now inject it into the
2597 * shadow host TLB
2598 */
James Hogan26ee17f2016-06-09 14:19:13 +01002599 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb);
Sanjay Lale685c682012-11-21 18:34:04 -08002600 }
2601 }
2602
2603 return er;
2604}