blob: 91d5b0e370b49dc61ed7419d6bee414c7474c22f [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>
23#include <asm/cpu-info.h>
24#include <asm/mmu_context.h>
25#include <asm/tlbflush.h>
26#include <asm/inst.h>
27
28#undef CONFIG_MIPS_MT
29#include <asm/r4kcache.h>
30#define CONFIG_MIPS_MT
31
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070032#include "opcode.h"
33#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 */
Sanjay Lale685c682012-11-21 18:34:04 -080055 insn.word = kvm_get_inst((uint32_t *) epc, vcpu);
56
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
201enum emulation_result update_pc(struct kvm_vcpu *vcpu, uint32_t cause)
202{
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 */
246static uint32_t kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
247{
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 */
303static uint32_t kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
304{
305 ktime_t expires;
306 int running;
307
308 /* Is the hrtimer pending? */
309 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
310 if (ktime_compare(now, expires) >= 0) {
311 /*
312 * Cancel it while we handle it so there's no chance of
313 * interference with the timeout handler.
314 */
315 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
316
317 /* Nothing should be waiting on the timeout */
318 kvm_mips_callbacks->queue_timer_int(vcpu);
319
320 /*
321 * Restart the timer if it was running based on the expiry time
322 * we read, so that we don't push it back 2 periods.
323 */
324 if (running) {
325 expires = ktime_add_ns(expires,
326 vcpu->arch.count_period);
327 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
328 HRTIMER_MODE_ABS);
329 }
330 }
331
332 /* Return the biased and scaled guest CP0_Count */
333 return vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
334}
335
336/**
337 * kvm_mips_read_count() - Read the current count value.
338 * @vcpu: Virtual CPU.
339 *
340 * Read the current guest CP0_Count value, taking into account whether the timer
341 * is stopped.
342 *
343 * Returns: The current guest CP0_Count value.
344 */
345uint32_t kvm_mips_read_count(struct kvm_vcpu *vcpu)
346{
347 struct mips_coproc *cop0 = vcpu->arch.cop0;
348
349 /* If count disabled just read static copy of count */
350 if (kvm_mips_count_disabled(vcpu))
351 return kvm_read_c0_guest_count(cop0);
352
353 return kvm_mips_read_count_running(vcpu, ktime_get());
354}
355
356/**
357 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
358 * @vcpu: Virtual CPU.
359 * @count: Output pointer for CP0_Count value at point of freeze.
360 *
361 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
362 * at the point it was frozen. It is guaranteed that any pending interrupts at
363 * the point it was frozen are handled, and none after that point.
364 *
365 * This is useful where the time/CP0_Count is needed in the calculation of the
366 * new parameters.
367 *
368 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
369 *
370 * Returns: The ktime at the point of freeze.
371 */
372static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu,
373 uint32_t *count)
374{
375 ktime_t now;
376
377 /* stop hrtimer before finding time */
378 hrtimer_cancel(&vcpu->arch.comparecount_timer);
379 now = ktime_get();
380
381 /* find count at this point and handle pending hrtimer */
382 *count = kvm_mips_read_count_running(vcpu, now);
383
384 return now;
385}
386
James Hogane30492b2014-05-29 10:16:35 +0100387/**
388 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
389 * @vcpu: Virtual CPU.
390 * @now: ktime at point of resume.
391 * @count: CP0_Count at point of resume.
392 *
393 * Resumes the timer and updates the timer expiry based on @now and @count.
394 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
395 * parameters need to be changed.
396 *
397 * It is guaranteed that a timer interrupt immediately after resume will be
398 * handled, but not if CP_Compare is exactly at @count. That case is already
399 * handled by kvm_mips_freeze_timer().
400 *
401 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
402 */
403static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
404 ktime_t now, uint32_t count)
405{
406 struct mips_coproc *cop0 = vcpu->arch.cop0;
407 uint32_t compare;
408 u64 delta;
409 ktime_t expire;
410
411 /* Calculate timeout (wrap 0 to 2^32) */
412 compare = kvm_read_c0_guest_compare(cop0);
413 delta = (u64)(uint32_t)(compare - count - 1) + 1;
414 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
415 expire = ktime_add_ns(now, delta);
416
417 /* Update hrtimer to use new timeout */
418 hrtimer_cancel(&vcpu->arch.comparecount_timer);
419 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
420}
421
422/**
423 * kvm_mips_update_hrtimer() - Update next expiry time of hrtimer.
424 * @vcpu: Virtual CPU.
425 *
426 * Recalculates and updates the expiry time of the hrtimer. This can be used
427 * after timer parameters have been altered which do not depend on the time that
428 * the change occurs (in those cases kvm_mips_freeze_hrtimer() and
429 * kvm_mips_resume_hrtimer() are used directly).
430 *
431 * It is guaranteed that no timer interrupts will be lost in the process.
432 *
433 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
434 */
435static void kvm_mips_update_hrtimer(struct kvm_vcpu *vcpu)
436{
437 ktime_t now;
438 uint32_t count;
439
440 /*
441 * freeze_hrtimer takes care of a timer interrupts <= count, and
442 * resume_hrtimer the hrtimer takes care of a timer interrupts > count.
443 */
444 now = kvm_mips_freeze_hrtimer(vcpu, &count);
445 kvm_mips_resume_hrtimer(vcpu, now, count);
446}
447
448/**
449 * kvm_mips_write_count() - Modify the count and update timer.
450 * @vcpu: Virtual CPU.
451 * @count: Guest CP0_Count value to set.
452 *
453 * Sets the CP0_Count value and updates the timer accordingly.
454 */
455void kvm_mips_write_count(struct kvm_vcpu *vcpu, uint32_t count)
456{
457 struct mips_coproc *cop0 = vcpu->arch.cop0;
458 ktime_t now;
459
460 /* Calculate bias */
James Hoganf8239342014-05-29 10:16:37 +0100461 now = kvm_mips_count_time(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100462 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
463
464 if (kvm_mips_count_disabled(vcpu))
465 /* The timer's disabled, adjust the static count */
466 kvm_write_c0_guest_count(cop0, count);
467 else
468 /* Update timeout */
469 kvm_mips_resume_hrtimer(vcpu, now, count);
470}
471
472/**
473 * kvm_mips_init_count() - Initialise timer.
474 * @vcpu: Virtual CPU.
475 *
476 * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
477 * it going if it's enabled.
478 */
479void kvm_mips_init_count(struct kvm_vcpu *vcpu)
480{
481 /* 100 MHz */
482 vcpu->arch.count_hz = 100*1000*1000;
483 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
484 vcpu->arch.count_hz);
485 vcpu->arch.count_dyn_bias = 0;
486
487 /* Starting at 0 */
488 kvm_mips_write_count(vcpu, 0);
489}
490
491/**
James Hoganf74a8e22014-05-29 10:16:38 +0100492 * kvm_mips_set_count_hz() - Update the frequency of the timer.
493 * @vcpu: Virtual CPU.
494 * @count_hz: Frequency of CP0_Count timer in Hz.
495 *
496 * Change the frequency of the CP0_Count timer. This is done atomically so that
497 * CP0_Count is continuous and no timer interrupt is lost.
498 *
499 * Returns: -EINVAL if @count_hz is out of range.
500 * 0 on success.
501 */
502int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
503{
504 struct mips_coproc *cop0 = vcpu->arch.cop0;
505 int dc;
506 ktime_t now;
507 u32 count;
508
509 /* ensure the frequency is in a sensible range... */
510 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
511 return -EINVAL;
512 /* ... and has actually changed */
513 if (vcpu->arch.count_hz == count_hz)
514 return 0;
515
516 /* Safely freeze timer so we can keep it continuous */
517 dc = kvm_mips_count_disabled(vcpu);
518 if (dc) {
519 now = kvm_mips_count_time(vcpu);
520 count = kvm_read_c0_guest_count(cop0);
521 } else {
522 now = kvm_mips_freeze_hrtimer(vcpu, &count);
523 }
524
525 /* Update the frequency */
526 vcpu->arch.count_hz = count_hz;
527 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
528 vcpu->arch.count_dyn_bias = 0;
529
530 /* Calculate adjusted bias so dynamic count is unchanged */
531 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
532
533 /* Update and resume hrtimer */
534 if (!dc)
535 kvm_mips_resume_hrtimer(vcpu, now, count);
536 return 0;
537}
538
539/**
James Hogane30492b2014-05-29 10:16:35 +0100540 * kvm_mips_write_compare() - Modify compare and update timer.
541 * @vcpu: Virtual CPU.
542 * @compare: New CP0_Compare value.
543 *
544 * Update CP0_Compare to a new value and update the timeout.
545 */
546void kvm_mips_write_compare(struct kvm_vcpu *vcpu, uint32_t compare)
547{
548 struct mips_coproc *cop0 = vcpu->arch.cop0;
549
550 /* if unchanged, must just be an ack */
551 if (kvm_read_c0_guest_compare(cop0) == compare)
552 return;
553
554 /* Update compare */
555 kvm_write_c0_guest_compare(cop0, compare);
556
557 /* Update timeout if count enabled */
558 if (!kvm_mips_count_disabled(vcpu))
559 kvm_mips_update_hrtimer(vcpu);
560}
561
562/**
563 * kvm_mips_count_disable() - Disable count.
564 * @vcpu: Virtual CPU.
565 *
566 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
567 * time will be handled but not after.
568 *
James Hoganf8239342014-05-29 10:16:37 +0100569 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
570 * count_ctl.DC has been set (count disabled).
James Hogane30492b2014-05-29 10:16:35 +0100571 *
572 * Returns: The time that the timer was stopped.
573 */
574static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
575{
576 struct mips_coproc *cop0 = vcpu->arch.cop0;
577 uint32_t count;
578 ktime_t now;
579
580 /* Stop hrtimer */
581 hrtimer_cancel(&vcpu->arch.comparecount_timer);
582
583 /* Set the static count from the dynamic count, handling pending TI */
584 now = ktime_get();
585 count = kvm_mips_read_count_running(vcpu, now);
586 kvm_write_c0_guest_count(cop0, count);
587
588 return now;
589}
590
591/**
592 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
593 * @vcpu: Virtual CPU.
594 *
595 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
James Hoganf8239342014-05-29 10:16:37 +0100596 * before the final stop time will be handled if the timer isn't disabled by
597 * count_ctl.DC, but not after.
James Hogane30492b2014-05-29 10:16:35 +0100598 *
599 * Assumes CP0_Cause.DC is clear (count enabled).
600 */
601void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
602{
603 struct mips_coproc *cop0 = vcpu->arch.cop0;
604
605 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
James Hoganf8239342014-05-29 10:16:37 +0100606 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
607 kvm_mips_count_disable(vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100608}
609
610/**
611 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
612 * @vcpu: Virtual CPU.
613 *
614 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
James Hoganf8239342014-05-29 10:16:37 +0100615 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
616 * potentially before even returning, so the caller should be careful with
617 * ordering of CP0_Cause modifications so as not to lose it.
James Hogane30492b2014-05-29 10:16:35 +0100618 *
619 * Assumes CP0_Cause.DC is set (count disabled).
620 */
621void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
622{
623 struct mips_coproc *cop0 = vcpu->arch.cop0;
624 uint32_t count;
625
626 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
627
628 /*
629 * Set the dynamic count to match the static count.
James Hoganf8239342014-05-29 10:16:37 +0100630 * This starts the hrtimer if count_ctl.DC allows it.
631 * Otherwise it conveniently updates the biases.
James Hogane30492b2014-05-29 10:16:35 +0100632 */
633 count = kvm_read_c0_guest_count(cop0);
634 kvm_mips_write_count(vcpu, count);
635}
636
637/**
James Hoganf8239342014-05-29 10:16:37 +0100638 * kvm_mips_set_count_ctl() - Update the count control KVM register.
639 * @vcpu: Virtual CPU.
640 * @count_ctl: Count control register new value.
641 *
642 * Set the count control KVM register. The timer is updated accordingly.
643 *
644 * Returns: -EINVAL if reserved bits are set.
645 * 0 on success.
646 */
647int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
648{
649 struct mips_coproc *cop0 = vcpu->arch.cop0;
650 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
651 s64 delta;
652 ktime_t expire, now;
653 uint32_t count, compare;
654
655 /* Only allow defined bits to be changed */
656 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
657 return -EINVAL;
658
659 /* Apply new value */
660 vcpu->arch.count_ctl = count_ctl;
661
662 /* Master CP0_Count disable */
663 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
664 /* Is CP0_Cause.DC already disabling CP0_Count? */
665 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
666 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
667 /* Just record the current time */
668 vcpu->arch.count_resume = ktime_get();
669 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
670 /* disable timer and record current time */
671 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
672 } else {
673 /*
674 * Calculate timeout relative to static count at resume
675 * time (wrap 0 to 2^32).
676 */
677 count = kvm_read_c0_guest_count(cop0);
678 compare = kvm_read_c0_guest_compare(cop0);
679 delta = (u64)(uint32_t)(compare - count - 1) + 1;
680 delta = div_u64(delta * NSEC_PER_SEC,
681 vcpu->arch.count_hz);
682 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
683
684 /* Handle pending interrupt */
685 now = ktime_get();
686 if (ktime_compare(now, expire) >= 0)
687 /* Nothing should be waiting on the timeout */
688 kvm_mips_callbacks->queue_timer_int(vcpu);
689
690 /* Resume hrtimer without changing bias */
691 count = kvm_mips_read_count_running(vcpu, now);
692 kvm_mips_resume_hrtimer(vcpu, now, count);
693 }
694 }
695
696 return 0;
697}
698
699/**
700 * kvm_mips_set_count_resume() - Update the count resume KVM register.
701 * @vcpu: Virtual CPU.
702 * @count_resume: Count resume register new value.
703 *
704 * Set the count resume KVM register.
705 *
706 * Returns: -EINVAL if out of valid range (0..now).
707 * 0 on success.
708 */
709int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
710{
711 /*
712 * It doesn't make sense for the resume time to be in the future, as it
713 * would be possible for the next interrupt to be more than a full
714 * period in the future.
715 */
716 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
717 return -EINVAL;
718
719 vcpu->arch.count_resume = ns_to_ktime(count_resume);
720 return 0;
721}
722
723/**
James Hogane30492b2014-05-29 10:16:35 +0100724 * kvm_mips_count_timeout() - Push timer forward on timeout.
725 * @vcpu: Virtual CPU.
726 *
727 * Handle an hrtimer event by push the hrtimer forward a period.
728 *
729 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
730 */
731enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
732{
733 /* Add the Count period to the current expiry time */
734 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
735 vcpu->arch.count_period);
736 return HRTIMER_RESTART;
Sanjay Lale685c682012-11-21 18:34:04 -0800737}
738
739enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
740{
741 struct mips_coproc *cop0 = vcpu->arch.cop0;
742 enum emulation_result er = EMULATE_DONE;
743
744 if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
745 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
746 kvm_read_c0_guest_epc(cop0));
747 kvm_clear_c0_guest_status(cop0, ST0_EXL);
748 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
749
750 } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
751 kvm_clear_c0_guest_status(cop0, ST0_ERL);
752 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
753 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700754 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
755 vcpu->arch.pc);
Sanjay Lale685c682012-11-21 18:34:04 -0800756 er = EMULATE_FAIL;
757 }
758
759 return er;
760}
761
762enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
763{
Sanjay Lale685c682012-11-21 18:34:04 -0800764 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
765 vcpu->arch.pending_exceptions);
766
767 ++vcpu->stat.wait_exits;
768 trace_kvm_exit(vcpu, WAIT_EXITS);
769 if (!vcpu->arch.pending_exceptions) {
770 vcpu->arch.wait = 1;
771 kvm_vcpu_block(vcpu);
772
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700773 /*
774 * We we are runnable, then definitely go off to user space to
775 * check if any I/O interrupts are pending.
Sanjay Lale685c682012-11-21 18:34:04 -0800776 */
777 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
778 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
779 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
780 }
781 }
782
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700783 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800784}
785
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700786/*
787 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
788 * we can catch this, if things ever change
Sanjay Lale685c682012-11-21 18:34:04 -0800789 */
790enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
791{
792 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -0800793 uint32_t pc = vcpu->arch.pc;
794
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700795 kvm_err("[%#x] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700796 return EMULATE_FAIL;
Sanjay Lale685c682012-11-21 18:34:04 -0800797}
798
799/* Write Guest TLB Entry @ Index */
800enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
801{
802 struct mips_coproc *cop0 = vcpu->arch.cop0;
803 int index = kvm_read_c0_guest_index(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800804 struct kvm_mips_tlb *tlb = NULL;
805 uint32_t pc = vcpu->arch.pc;
806
807 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700808 kvm_debug("%s: illegal index: %d\n", __func__, index);
809 kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
810 pc, index, kvm_read_c0_guest_entryhi(cop0),
811 kvm_read_c0_guest_entrylo0(cop0),
812 kvm_read_c0_guest_entrylo1(cop0),
813 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800814 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
815 }
816
817 tlb = &vcpu->arch.guest_tlb[index];
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700818 /*
819 * Probe the shadow host TLB for the entry being overwritten, if one
820 * matches, invalidate it
821 */
Sanjay Lale685c682012-11-21 18:34:04 -0800822 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
Sanjay Lale685c682012-11-21 18:34:04 -0800823
824 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
825 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
826 tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
827 tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
828
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700829 kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
830 pc, index, kvm_read_c0_guest_entryhi(cop0),
831 kvm_read_c0_guest_entrylo0(cop0),
832 kvm_read_c0_guest_entrylo1(cop0),
833 kvm_read_c0_guest_pagemask(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800834
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700835 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800836}
837
838/* Write Guest TLB Entry @ Random Index */
839enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
840{
841 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -0800842 struct kvm_mips_tlb *tlb = NULL;
843 uint32_t pc = vcpu->arch.pc;
844 int index;
845
Sanjay Lale685c682012-11-21 18:34:04 -0800846 get_random_bytes(&index, sizeof(index));
847 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
Sanjay Lale685c682012-11-21 18:34:04 -0800848
Sanjay Lale685c682012-11-21 18:34:04 -0800849 tlb = &vcpu->arch.guest_tlb[index];
850
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700851 /*
852 * Probe the shadow host TLB for the entry being overwritten, if one
853 * matches, invalidate it
854 */
Sanjay Lale685c682012-11-21 18:34:04 -0800855 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
Sanjay Lale685c682012-11-21 18:34:04 -0800856
857 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
858 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
859 tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
860 tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
861
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700862 kvm_debug("[%#x] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
863 pc, index, kvm_read_c0_guest_entryhi(cop0),
864 kvm_read_c0_guest_entrylo0(cop0),
865 kvm_read_c0_guest_entrylo1(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -0800866
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700867 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800868}
869
870enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
871{
872 struct mips_coproc *cop0 = vcpu->arch.cop0;
873 long entryhi = kvm_read_c0_guest_entryhi(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -0800874 uint32_t pc = vcpu->arch.pc;
875 int index = -1;
876
877 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
878
879 kvm_write_c0_guest_index(cop0, index);
880
881 kvm_debug("[%#x] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
882 index);
883
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700884 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -0800885}
886
James Hoganc7716072014-06-26 15:11:29 +0100887/**
888 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
889 * @vcpu: Virtual CPU.
890 *
891 * Finds the mask of bits which are writable in the guest's Config1 CP0
892 * register, by userland (currently read-only to the guest).
893 */
894unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
895{
896 /* Read-only */
897 return 0;
898}
899
900/**
901 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
902 * @vcpu: Virtual CPU.
903 *
904 * Finds the mask of bits which are writable in the guest's Config3 CP0
905 * register, by userland (currently read-only to the guest).
906 */
907unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
908{
909 /* Config4 is optional */
910 return MIPS_CONF_M;
911}
912
913/**
914 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
915 * @vcpu: Virtual CPU.
916 *
917 * Finds the mask of bits which are writable in the guest's Config4 CP0
918 * register, by userland (currently read-only to the guest).
919 */
920unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
921{
922 /* Config5 is optional */
923 return MIPS_CONF_M;
924}
925
926/**
927 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
928 * @vcpu: Virtual CPU.
929 *
930 * Finds the mask of bits which are writable in the guest's Config5 CP0
931 * register, by the guest itself.
932 */
933unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
934{
935 /* Read-only */
936 return 0;
937}
938
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700939enum emulation_result kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc,
940 uint32_t cause, struct kvm_run *run,
941 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -0800942{
943 struct mips_coproc *cop0 = vcpu->arch.cop0;
944 enum emulation_result er = EMULATE_DONE;
945 int32_t rt, rd, copz, sel, co_bit, op;
946 uint32_t pc = vcpu->arch.pc;
947 unsigned long curr_pc;
948
949 /*
950 * Update PC and hold onto current PC in case there is
951 * an error and we want to rollback the PC
952 */
953 curr_pc = vcpu->arch.pc;
954 er = update_pc(vcpu, cause);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700955 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -0800956 return er;
Sanjay Lale685c682012-11-21 18:34:04 -0800957
958 copz = (inst >> 21) & 0x1f;
959 rt = (inst >> 16) & 0x1f;
960 rd = (inst >> 11) & 0x1f;
961 sel = inst & 0x7;
962 co_bit = (inst >> 25) & 1;
963
Sanjay Lale685c682012-11-21 18:34:04 -0800964 if (co_bit) {
965 op = (inst) & 0xff;
966
967 switch (op) {
968 case tlbr_op: /* Read indexed TLB entry */
969 er = kvm_mips_emul_tlbr(vcpu);
970 break;
971 case tlbwi_op: /* Write indexed */
972 er = kvm_mips_emul_tlbwi(vcpu);
973 break;
974 case tlbwr_op: /* Write random */
975 er = kvm_mips_emul_tlbwr(vcpu);
976 break;
977 case tlbp_op: /* TLB Probe */
978 er = kvm_mips_emul_tlbp(vcpu);
979 break;
980 case rfe_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700981 kvm_err("!!!COP0_RFE!!!\n");
Sanjay Lale685c682012-11-21 18:34:04 -0800982 break;
983 case eret_op:
984 er = kvm_mips_emul_eret(vcpu);
985 goto dont_update_pc;
986 break;
987 case wait_op:
988 er = kvm_mips_emul_wait(vcpu);
989 break;
990 }
991 } else {
992 switch (copz) {
993 case mfc_op:
994#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
995 cop0->stat[rd][sel]++;
996#endif
997 /* Get reg */
998 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +0100999 vcpu->arch.gprs[rt] = kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001000 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1001 vcpu->arch.gprs[rt] = 0x0;
1002#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1003 kvm_mips_trans_mfc0(inst, opc, vcpu);
1004#endif
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001005 } else {
Sanjay Lale685c682012-11-21 18:34:04 -08001006 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1007
1008#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1009 kvm_mips_trans_mfc0(inst, opc, vcpu);
1010#endif
1011 }
1012
1013 kvm_debug
1014 ("[%#x] MFCz[%d][%d], vcpu->arch.gprs[%d]: %#lx\n",
1015 pc, rd, sel, rt, vcpu->arch.gprs[rt]);
1016
1017 break;
1018
1019 case dmfc_op:
1020 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1021 break;
1022
1023 case mtc_op:
1024#ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1025 cop0->stat[rd][sel]++;
1026#endif
1027 if ((rd == MIPS_CP0_TLB_INDEX)
1028 && (vcpu->arch.gprs[rt] >=
1029 KVM_MIPS_GUEST_TLB_SIZE)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001030 kvm_err("Invalid TLB Index: %ld",
1031 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001032 er = EMULATE_FAIL;
1033 break;
1034 }
1035#define C0_EBASE_CORE_MASK 0xff
1036 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
1037 /* Preserve CORE number */
1038 kvm_change_c0_guest_ebase(cop0,
1039 ~(C0_EBASE_CORE_MASK),
1040 vcpu->arch.gprs[rt]);
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001041 kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
1042 kvm_read_c0_guest_ebase(cop0));
Sanjay Lale685c682012-11-21 18:34:04 -08001043 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
David Daney48c4ac92013-05-13 13:56:44 -07001044 uint32_t nasid =
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001045 vcpu->arch.gprs[rt] & ASID_MASK;
1046 if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
David Daney48c4ac92013-05-13 13:56:44 -07001047 ((kvm_read_c0_guest_entryhi(cop0) &
1048 ASID_MASK) != nasid)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001049 kvm_debug("MTCz, change ASID from %#lx to %#lx\n",
1050 kvm_read_c0_guest_entryhi(cop0)
1051 & ASID_MASK,
1052 vcpu->arch.gprs[rt]
1053 & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001054
1055 /* Blow away the shadow host TLBs */
1056 kvm_mips_flush_host_tlb(1);
1057 }
1058 kvm_write_c0_guest_entryhi(cop0,
1059 vcpu->arch.gprs[rt]);
1060 }
1061 /* Are we writing to COUNT */
1062 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
James Hogane30492b2014-05-29 10:16:35 +01001063 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001064 goto done;
1065 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
1066 kvm_debug("[%#x] MTCz, COMPARE %#lx <- %#lx\n",
1067 pc, kvm_read_c0_guest_compare(cop0),
1068 vcpu->arch.gprs[rt]);
1069
1070 /* If we are writing to COMPARE */
1071 /* Clear pending timer interrupt, if any */
1072 kvm_mips_callbacks->dequeue_timer_int(vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001073 kvm_mips_write_compare(vcpu,
1074 vcpu->arch.gprs[rt]);
Sanjay Lale685c682012-11-21 18:34:04 -08001075 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
1076 kvm_write_c0_guest_status(cop0,
1077 vcpu->arch.gprs[rt]);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001078 /*
1079 * Make sure that CU1 and NMI bits are
1080 * never set
1081 */
Sanjay Lale685c682012-11-21 18:34:04 -08001082 kvm_clear_c0_guest_status(cop0,
1083 (ST0_CU1 | ST0_NMI));
1084
1085#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1086 kvm_mips_trans_mtc0(inst, opc, vcpu);
1087#endif
James Hogane30492b2014-05-29 10:16:35 +01001088 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
1089 uint32_t old_cause, new_cause;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001090
James Hogane30492b2014-05-29 10:16:35 +01001091 old_cause = kvm_read_c0_guest_cause(cop0);
1092 new_cause = vcpu->arch.gprs[rt];
1093 /* Update R/W bits */
1094 kvm_change_c0_guest_cause(cop0, 0x08800300,
1095 new_cause);
1096 /* DC bit enabling/disabling timer? */
1097 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1098 if (new_cause & CAUSEF_DC)
1099 kvm_mips_count_disable_cause(vcpu);
1100 else
1101 kvm_mips_count_enable_cause(vcpu);
1102 }
Sanjay Lale685c682012-11-21 18:34:04 -08001103 } else {
1104 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1105#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1106 kvm_mips_trans_mtc0(inst, opc, vcpu);
1107#endif
1108 }
1109
1110 kvm_debug("[%#x] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
1111 rd, sel, cop0->reg[rd][sel]);
1112 break;
1113
1114 case dmtc_op:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001115 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1116 vcpu->arch.pc, rt, rd, sel);
Sanjay Lale685c682012-11-21 18:34:04 -08001117 er = EMULATE_FAIL;
1118 break;
1119
1120 case mfmcz_op:
1121#ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1122 cop0->stat[MIPS_CP0_STATUS][0]++;
1123#endif
1124 if (rt != 0) {
1125 vcpu->arch.gprs[rt] =
1126 kvm_read_c0_guest_status(cop0);
1127 }
1128 /* EI */
1129 if (inst & 0x20) {
1130 kvm_debug("[%#lx] mfmcz_op: EI\n",
1131 vcpu->arch.pc);
1132 kvm_set_c0_guest_status(cop0, ST0_IE);
1133 } else {
1134 kvm_debug("[%#lx] mfmcz_op: DI\n",
1135 vcpu->arch.pc);
1136 kvm_clear_c0_guest_status(cop0, ST0_IE);
1137 }
1138
1139 break;
1140
1141 case wrpgpr_op:
1142 {
1143 uint32_t css =
1144 cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1145 uint32_t pss =
1146 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001147 /*
1148 * We don't support any shadow register sets, so
1149 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1150 */
Sanjay Lale685c682012-11-21 18:34:04 -08001151 if (css || pss) {
1152 er = EMULATE_FAIL;
1153 break;
1154 }
1155 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1156 vcpu->arch.gprs[rt]);
1157 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1158 }
1159 break;
1160 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001161 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
1162 vcpu->arch.pc, copz);
Sanjay Lale685c682012-11-21 18:34:04 -08001163 er = EMULATE_FAIL;
1164 break;
1165 }
1166 }
1167
1168done:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001169 /* Rollback PC only if emulation was unsuccessful */
1170 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001171 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001172
1173dont_update_pc:
1174 /*
1175 * This is for special instructions whose emulation
1176 * updates the PC, so do not overwrite the PC under
1177 * any circumstances
1178 */
1179
1180 return er;
1181}
1182
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001183enum emulation_result kvm_mips_emulate_store(uint32_t inst, uint32_t cause,
1184 struct kvm_run *run,
1185 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001186{
1187 enum emulation_result er = EMULATE_DO_MMIO;
1188 int32_t op, base, rt, offset;
1189 uint32_t bytes;
1190 void *data = run->mmio.data;
1191 unsigned long curr_pc;
1192
1193 /*
1194 * Update PC and hold onto current PC in case there is
1195 * an error and we want to rollback the PC
1196 */
1197 curr_pc = vcpu->arch.pc;
1198 er = update_pc(vcpu, cause);
1199 if (er == EMULATE_FAIL)
1200 return er;
1201
1202 rt = (inst >> 16) & 0x1f;
1203 base = (inst >> 21) & 0x1f;
1204 offset = inst & 0xffff;
1205 op = (inst >> 26) & 0x3f;
1206
1207 switch (op) {
1208 case sb_op:
1209 bytes = 1;
1210 if (bytes > sizeof(run->mmio.data)) {
1211 kvm_err("%s: bad MMIO length: %d\n", __func__,
1212 run->mmio.len);
1213 }
1214 run->mmio.phys_addr =
1215 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1216 host_cp0_badvaddr);
1217 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1218 er = EMULATE_FAIL;
1219 break;
1220 }
1221 run->mmio.len = bytes;
1222 run->mmio.is_write = 1;
1223 vcpu->mmio_needed = 1;
1224 vcpu->mmio_is_write = 1;
1225 *(u8 *) data = vcpu->arch.gprs[rt];
1226 kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1227 vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
1228 *(uint8_t *) data);
1229
1230 break;
1231
1232 case sw_op:
1233 bytes = 4;
1234 if (bytes > sizeof(run->mmio.data)) {
1235 kvm_err("%s: bad MMIO length: %d\n", __func__,
1236 run->mmio.len);
1237 }
1238 run->mmio.phys_addr =
1239 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1240 host_cp0_badvaddr);
1241 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1242 er = EMULATE_FAIL;
1243 break;
1244 }
1245
1246 run->mmio.len = bytes;
1247 run->mmio.is_write = 1;
1248 vcpu->mmio_needed = 1;
1249 vcpu->mmio_is_write = 1;
1250 *(uint32_t *) data = vcpu->arch.gprs[rt];
1251
1252 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1253 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1254 vcpu->arch.gprs[rt], *(uint32_t *) data);
1255 break;
1256
1257 case sh_op:
1258 bytes = 2;
1259 if (bytes > sizeof(run->mmio.data)) {
1260 kvm_err("%s: bad MMIO length: %d\n", __func__,
1261 run->mmio.len);
1262 }
1263 run->mmio.phys_addr =
1264 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1265 host_cp0_badvaddr);
1266 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1267 er = EMULATE_FAIL;
1268 break;
1269 }
1270
1271 run->mmio.len = bytes;
1272 run->mmio.is_write = 1;
1273 vcpu->mmio_needed = 1;
1274 vcpu->mmio_is_write = 1;
1275 *(uint16_t *) data = vcpu->arch.gprs[rt];
1276
1277 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1278 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1279 vcpu->arch.gprs[rt], *(uint32_t *) data);
1280 break;
1281
1282 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001283 kvm_err("Store not yet supported");
Sanjay Lale685c682012-11-21 18:34:04 -08001284 er = EMULATE_FAIL;
1285 break;
1286 }
1287
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001288 /* Rollback PC if emulation was unsuccessful */
1289 if (er == EMULATE_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08001290 vcpu->arch.pc = curr_pc;
Sanjay Lale685c682012-11-21 18:34:04 -08001291
1292 return er;
1293}
1294
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001295enum emulation_result kvm_mips_emulate_load(uint32_t inst, uint32_t cause,
1296 struct kvm_run *run,
1297 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001298{
1299 enum emulation_result er = EMULATE_DO_MMIO;
1300 int32_t op, base, rt, offset;
1301 uint32_t bytes;
1302
1303 rt = (inst >> 16) & 0x1f;
1304 base = (inst >> 21) & 0x1f;
1305 offset = inst & 0xffff;
1306 op = (inst >> 26) & 0x3f;
1307
1308 vcpu->arch.pending_load_cause = cause;
1309 vcpu->arch.io_gpr = rt;
1310
1311 switch (op) {
1312 case lw_op:
1313 bytes = 4;
1314 if (bytes > sizeof(run->mmio.data)) {
1315 kvm_err("%s: bad MMIO length: %d\n", __func__,
1316 run->mmio.len);
1317 er = EMULATE_FAIL;
1318 break;
1319 }
1320 run->mmio.phys_addr =
1321 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1322 host_cp0_badvaddr);
1323 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1324 er = EMULATE_FAIL;
1325 break;
1326 }
1327
1328 run->mmio.len = bytes;
1329 run->mmio.is_write = 0;
1330 vcpu->mmio_needed = 1;
1331 vcpu->mmio_is_write = 0;
1332 break;
1333
1334 case lh_op:
1335 case lhu_op:
1336 bytes = 2;
1337 if (bytes > sizeof(run->mmio.data)) {
1338 kvm_err("%s: bad MMIO length: %d\n", __func__,
1339 run->mmio.len);
1340 er = EMULATE_FAIL;
1341 break;
1342 }
1343 run->mmio.phys_addr =
1344 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1345 host_cp0_badvaddr);
1346 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1347 er = EMULATE_FAIL;
1348 break;
1349 }
1350
1351 run->mmio.len = bytes;
1352 run->mmio.is_write = 0;
1353 vcpu->mmio_needed = 1;
1354 vcpu->mmio_is_write = 0;
1355
1356 if (op == lh_op)
1357 vcpu->mmio_needed = 2;
1358 else
1359 vcpu->mmio_needed = 1;
1360
1361 break;
1362
1363 case lbu_op:
1364 case lb_op:
1365 bytes = 1;
1366 if (bytes > sizeof(run->mmio.data)) {
1367 kvm_err("%s: bad MMIO length: %d\n", __func__,
1368 run->mmio.len);
1369 er = EMULATE_FAIL;
1370 break;
1371 }
1372 run->mmio.phys_addr =
1373 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1374 host_cp0_badvaddr);
1375 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1376 er = EMULATE_FAIL;
1377 break;
1378 }
1379
1380 run->mmio.len = bytes;
1381 run->mmio.is_write = 0;
1382 vcpu->mmio_is_write = 0;
1383
1384 if (op == lb_op)
1385 vcpu->mmio_needed = 2;
1386 else
1387 vcpu->mmio_needed = 1;
1388
1389 break;
1390
1391 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001392 kvm_err("Load not yet supported");
Sanjay Lale685c682012-11-21 18:34:04 -08001393 er = EMULATE_FAIL;
1394 break;
1395 }
1396
1397 return er;
1398}
1399
1400int kvm_mips_sync_icache(unsigned long va, struct kvm_vcpu *vcpu)
1401{
1402 unsigned long offset = (va & ~PAGE_MASK);
1403 struct kvm *kvm = vcpu->kvm;
1404 unsigned long pa;
1405 gfn_t gfn;
1406 pfn_t pfn;
1407
1408 gfn = va >> PAGE_SHIFT;
1409
1410 if (gfn >= kvm->arch.guest_pmap_npages) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001411 kvm_err("%s: Invalid gfn: %#llx\n", __func__, gfn);
Sanjay Lale685c682012-11-21 18:34:04 -08001412 kvm_mips_dump_host_tlbs();
1413 kvm_arch_vcpu_dump_regs(vcpu);
1414 return -1;
1415 }
1416 pfn = kvm->arch.guest_pmap[gfn];
1417 pa = (pfn << PAGE_SHIFT) | offset;
1418
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001419 kvm_debug("%s: va: %#lx, unmapped: %#x\n", __func__, va,
1420 CKSEG0ADDR(pa));
Sanjay Lale685c682012-11-21 18:34:04 -08001421
James Hoganfacaaec2014-05-29 10:16:25 +01001422 local_flush_icache_range(CKSEG0ADDR(pa), 32);
Sanjay Lale685c682012-11-21 18:34:04 -08001423 return 0;
1424}
1425
1426#define MIPS_CACHE_OP_INDEX_INV 0x0
1427#define MIPS_CACHE_OP_INDEX_LD_TAG 0x1
1428#define MIPS_CACHE_OP_INDEX_ST_TAG 0x2
1429#define MIPS_CACHE_OP_IMP 0x3
1430#define MIPS_CACHE_OP_HIT_INV 0x4
1431#define MIPS_CACHE_OP_FILL_WB_INV 0x5
1432#define MIPS_CACHE_OP_HIT_HB 0x6
1433#define MIPS_CACHE_OP_FETCH_LOCK 0x7
1434
1435#define MIPS_CACHE_ICACHE 0x0
1436#define MIPS_CACHE_DCACHE 0x1
1437#define MIPS_CACHE_SEC 0x3
1438
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001439enum emulation_result kvm_mips_emulate_cache(uint32_t inst, uint32_t *opc,
1440 uint32_t cause,
1441 struct kvm_run *run,
1442 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001443{
1444 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lale685c682012-11-21 18:34:04 -08001445 enum emulation_result er = EMULATE_DONE;
1446 int32_t offset, cache, op_inst, op, base;
1447 struct kvm_vcpu_arch *arch = &vcpu->arch;
1448 unsigned long va;
1449 unsigned long curr_pc;
1450
1451 /*
1452 * Update PC and hold onto current PC in case there is
1453 * an error and we want to rollback the PC
1454 */
1455 curr_pc = vcpu->arch.pc;
1456 er = update_pc(vcpu, cause);
1457 if (er == EMULATE_FAIL)
1458 return er;
1459
1460 base = (inst >> 21) & 0x1f;
1461 op_inst = (inst >> 16) & 0x1f;
1462 offset = inst & 0xffff;
1463 cache = (inst >> 16) & 0x3;
1464 op = (inst >> 18) & 0x7;
1465
1466 va = arch->gprs[base] + offset;
1467
1468 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1469 cache, op, base, arch->gprs[base], offset);
1470
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001471 /*
1472 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1473 * invalidate the caches entirely by stepping through all the
1474 * ways/indexes
Sanjay Lale685c682012-11-21 18:34:04 -08001475 */
1476 if (op == MIPS_CACHE_OP_INDEX_INV) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001477 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1478 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1479 arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001480
1481 if (cache == MIPS_CACHE_DCACHE)
1482 r4k_blast_dcache();
1483 else if (cache == MIPS_CACHE_ICACHE)
1484 r4k_blast_icache();
1485 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001486 kvm_err("%s: unsupported CACHE INDEX operation\n",
1487 __func__);
Sanjay Lale685c682012-11-21 18:34:04 -08001488 return EMULATE_FAIL;
1489 }
1490
1491#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1492 kvm_mips_trans_cache_index(inst, opc, vcpu);
1493#endif
1494 goto done;
1495 }
1496
1497 preempt_disable();
1498 if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001499 if (kvm_mips_host_tlb_lookup(vcpu, va) < 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001500 kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08001501 } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
1502 KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
1503 int index;
1504
1505 /* If an entry already exists then skip */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001506 if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
Sanjay Lale685c682012-11-21 18:34:04 -08001507 goto skip_fault;
Sanjay Lale685c682012-11-21 18:34:04 -08001508
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001509 /*
1510 * If address not in the guest TLB, then give the guest a fault,
1511 * the resulting handler will do the right thing
Sanjay Lale685c682012-11-21 18:34:04 -08001512 */
1513 index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001514 (kvm_read_c0_guest_entryhi
1515 (cop0) & ASID_MASK));
Sanjay Lale685c682012-11-21 18:34:04 -08001516
1517 if (index < 0) {
1518 vcpu->arch.host_cp0_entryhi = (va & VPN2_MASK);
1519 vcpu->arch.host_cp0_badvaddr = va;
1520 er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
1521 vcpu);
1522 preempt_enable();
1523 goto dont_update_pc;
1524 } else {
1525 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001526 /*
1527 * Check if the entry is valid, if not then setup a TLB
1528 * invalid exception to the guest
1529 */
Sanjay Lale685c682012-11-21 18:34:04 -08001530 if (!TLB_IS_VALID(*tlb, va)) {
1531 er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
1532 run, vcpu);
1533 preempt_enable();
1534 goto dont_update_pc;
1535 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001536 /*
1537 * We fault an entry from the guest tlb to the
1538 * shadow host TLB
1539 */
Sanjay Lale685c682012-11-21 18:34:04 -08001540 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb,
1541 NULL,
1542 NULL);
1543 }
1544 }
1545 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001546 kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1547 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001548 er = EMULATE_FAIL;
1549 preempt_enable();
1550 goto dont_update_pc;
1551
1552 }
1553
1554skip_fault:
1555 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
1556 if (cache == MIPS_CACHE_DCACHE
1557 && (op == MIPS_CACHE_OP_FILL_WB_INV
1558 || op == MIPS_CACHE_OP_HIT_INV)) {
1559 flush_dcache_line(va);
1560
1561#ifdef CONFIG_KVM_MIPS_DYN_TRANS
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001562 /*
1563 * Replace the CACHE instruction, with a SYNCI, not the same,
1564 * but avoids a trap
1565 */
Sanjay Lale685c682012-11-21 18:34:04 -08001566 kvm_mips_trans_cache_va(inst, opc, vcpu);
1567#endif
1568 } else if (op == MIPS_CACHE_OP_HIT_INV && cache == MIPS_CACHE_ICACHE) {
1569 flush_dcache_line(va);
1570 flush_icache_line(va);
1571
1572#ifdef CONFIG_KVM_MIPS_DYN_TRANS
1573 /* Replace the CACHE instruction, with a SYNCI */
1574 kvm_mips_trans_cache_va(inst, opc, vcpu);
1575#endif
1576 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001577 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1578 cache, op, base, arch->gprs[base], offset);
Sanjay Lale685c682012-11-21 18:34:04 -08001579 er = EMULATE_FAIL;
1580 preempt_enable();
1581 goto dont_update_pc;
1582 }
1583
1584 preempt_enable();
1585
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001586dont_update_pc:
1587 /* Rollback PC */
Sanjay Lale685c682012-11-21 18:34:04 -08001588 vcpu->arch.pc = curr_pc;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001589done:
Sanjay Lale685c682012-11-21 18:34:04 -08001590 return er;
1591}
1592
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001593enum emulation_result kvm_mips_emulate_inst(unsigned long cause, uint32_t *opc,
1594 struct kvm_run *run,
1595 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001596{
1597 enum emulation_result er = EMULATE_DONE;
1598 uint32_t inst;
1599
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001600 /* Fetch the instruction. */
1601 if (cause & CAUSEF_BD)
Sanjay Lale685c682012-11-21 18:34:04 -08001602 opc += 1;
Sanjay Lale685c682012-11-21 18:34:04 -08001603
1604 inst = kvm_get_inst(opc, vcpu);
1605
1606 switch (((union mips_instruction)inst).r_format.opcode) {
1607 case cop0_op:
1608 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1609 break;
1610 case sb_op:
1611 case sh_op:
1612 case sw_op:
1613 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1614 break;
1615 case lb_op:
1616 case lbu_op:
1617 case lhu_op:
1618 case lh_op:
1619 case lw_op:
1620 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1621 break;
1622
1623 case cache_op:
1624 ++vcpu->stat.cache_exits;
1625 trace_kvm_exit(vcpu, CACHE_EXITS);
1626 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1627 break;
1628
1629 default:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001630 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
1631 inst);
Sanjay Lale685c682012-11-21 18:34:04 -08001632 kvm_arch_vcpu_dump_regs(vcpu);
1633 er = EMULATE_FAIL;
1634 break;
1635 }
1636
1637 return er;
1638}
1639
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001640enum emulation_result kvm_mips_emulate_syscall(unsigned long cause,
1641 uint32_t *opc,
1642 struct kvm_run *run,
1643 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001644{
1645 struct mips_coproc *cop0 = vcpu->arch.cop0;
1646 struct kvm_vcpu_arch *arch = &vcpu->arch;
1647 enum emulation_result er = EMULATE_DONE;
1648
1649 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1650 /* save old pc */
1651 kvm_write_c0_guest_epc(cop0, arch->pc);
1652 kvm_set_c0_guest_status(cop0, ST0_EXL);
1653
1654 if (cause & CAUSEF_BD)
1655 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1656 else
1657 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1658
1659 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1660
1661 kvm_change_c0_guest_cause(cop0, (0xff),
1662 (T_SYSCALL << CAUSEB_EXCCODE));
1663
1664 /* Set PC to the exception entry point */
1665 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1666
1667 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001668 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08001669 er = EMULATE_FAIL;
1670 }
1671
1672 return er;
1673}
1674
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001675enum emulation_result kvm_mips_emulate_tlbmiss_ld(unsigned long cause,
1676 uint32_t *opc,
1677 struct kvm_run *run,
1678 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001679{
1680 struct mips_coproc *cop0 = vcpu->arch.cop0;
1681 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001682 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001683 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001684
1685 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1686 /* save old pc */
1687 kvm_write_c0_guest_epc(cop0, arch->pc);
1688 kvm_set_c0_guest_status(cop0, ST0_EXL);
1689
1690 if (cause & CAUSEF_BD)
1691 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1692 else
1693 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1694
1695 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1696 arch->pc);
1697
1698 /* set pc to the exception entry point */
1699 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1700
1701 } else {
1702 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1703 arch->pc);
1704
1705 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1706 }
1707
1708 kvm_change_c0_guest_cause(cop0, (0xff),
1709 (T_TLB_LD_MISS << CAUSEB_EXCCODE));
1710
1711 /* setup badvaddr, context and entryhi registers for the guest */
1712 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1713 /* XXXKYMA: is the context register used by linux??? */
1714 kvm_write_c0_guest_entryhi(cop0, entryhi);
1715 /* Blow away the shadow host TLBs */
1716 kvm_mips_flush_host_tlb(1);
1717
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001718 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001719}
1720
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001721enum emulation_result kvm_mips_emulate_tlbinv_ld(unsigned long cause,
1722 uint32_t *opc,
1723 struct kvm_run *run,
1724 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001725{
1726 struct mips_coproc *cop0 = vcpu->arch.cop0;
1727 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001728 unsigned long entryhi =
1729 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001730 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001731
1732 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1733 /* save old pc */
1734 kvm_write_c0_guest_epc(cop0, arch->pc);
1735 kvm_set_c0_guest_status(cop0, ST0_EXL);
1736
1737 if (cause & CAUSEF_BD)
1738 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1739 else
1740 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1741
1742 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
1743 arch->pc);
1744
1745 /* set pc to the exception entry point */
1746 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1747
1748 } else {
1749 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1750 arch->pc);
1751 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1752 }
1753
1754 kvm_change_c0_guest_cause(cop0, (0xff),
1755 (T_TLB_LD_MISS << CAUSEB_EXCCODE));
1756
1757 /* setup badvaddr, context and entryhi registers for the guest */
1758 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1759 /* XXXKYMA: is the context register used by linux??? */
1760 kvm_write_c0_guest_entryhi(cop0, entryhi);
1761 /* Blow away the shadow host TLBs */
1762 kvm_mips_flush_host_tlb(1);
1763
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001764 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001765}
1766
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001767enum emulation_result kvm_mips_emulate_tlbmiss_st(unsigned long cause,
1768 uint32_t *opc,
1769 struct kvm_run *run,
1770 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001771{
1772 struct mips_coproc *cop0 = vcpu->arch.cop0;
1773 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001774 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001775 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001776
1777 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1778 /* save old pc */
1779 kvm_write_c0_guest_epc(cop0, arch->pc);
1780 kvm_set_c0_guest_status(cop0, ST0_EXL);
1781
1782 if (cause & CAUSEF_BD)
1783 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1784 else
1785 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1786
1787 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1788 arch->pc);
1789
1790 /* Set PC to the exception entry point */
1791 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1792 } else {
1793 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1794 arch->pc);
1795 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1796 }
1797
1798 kvm_change_c0_guest_cause(cop0, (0xff),
1799 (T_TLB_ST_MISS << CAUSEB_EXCCODE));
1800
1801 /* setup badvaddr, context and entryhi registers for the guest */
1802 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1803 /* XXXKYMA: is the context register used by linux??? */
1804 kvm_write_c0_guest_entryhi(cop0, entryhi);
1805 /* Blow away the shadow host TLBs */
1806 kvm_mips_flush_host_tlb(1);
1807
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001808 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001809}
1810
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001811enum emulation_result kvm_mips_emulate_tlbinv_st(unsigned long cause,
1812 uint32_t *opc,
1813 struct kvm_run *run,
1814 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001815{
1816 struct mips_coproc *cop0 = vcpu->arch.cop0;
1817 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001818 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001819 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001820
1821 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1822 /* save old pc */
1823 kvm_write_c0_guest_epc(cop0, arch->pc);
1824 kvm_set_c0_guest_status(cop0, ST0_EXL);
1825
1826 if (cause & CAUSEF_BD)
1827 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1828 else
1829 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1830
1831 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1832 arch->pc);
1833
1834 /* Set PC to the exception entry point */
1835 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1836 } else {
1837 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1838 arch->pc);
1839 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1840 }
1841
1842 kvm_change_c0_guest_cause(cop0, (0xff),
1843 (T_TLB_ST_MISS << CAUSEB_EXCCODE));
1844
1845 /* setup badvaddr, context and entryhi registers for the guest */
1846 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1847 /* XXXKYMA: is the context register used by linux??? */
1848 kvm_write_c0_guest_entryhi(cop0, entryhi);
1849 /* Blow away the shadow host TLBs */
1850 kvm_mips_flush_host_tlb(1);
1851
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001852 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001853}
1854
1855/* TLBMOD: store into address matching TLB with Dirty bit off */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001856enum emulation_result kvm_mips_handle_tlbmod(unsigned long cause, uint32_t *opc,
1857 struct kvm_run *run,
1858 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001859{
1860 enum emulation_result er = EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001861#ifdef DEBUG
James Hogan3d654832014-05-29 10:16:41 +01001862 struct mips_coproc *cop0 = vcpu->arch.cop0;
1863 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
1864 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
1865 int index;
1866
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001867 /* If address not in the guest TLB, then we are in trouble */
Sanjay Lale685c682012-11-21 18:34:04 -08001868 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
1869 if (index < 0) {
1870 /* XXXKYMA Invalidate and retry */
1871 kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
1872 kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
1873 __func__, entryhi);
1874 kvm_mips_dump_guest_tlbs(vcpu);
1875 kvm_mips_dump_host_tlbs();
1876 return EMULATE_FAIL;
1877 }
1878#endif
1879
1880 er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
1881 return er;
1882}
1883
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001884enum emulation_result kvm_mips_emulate_tlbmod(unsigned long cause,
1885 uint32_t *opc,
1886 struct kvm_run *run,
1887 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001888{
1889 struct mips_coproc *cop0 = vcpu->arch.cop0;
1890 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07001891 (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
Sanjay Lale685c682012-11-21 18:34:04 -08001892 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001893
1894 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1895 /* save old pc */
1896 kvm_write_c0_guest_epc(cop0, arch->pc);
1897 kvm_set_c0_guest_status(cop0, ST0_EXL);
1898
1899 if (cause & CAUSEF_BD)
1900 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1901 else
1902 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1903
1904 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
1905 arch->pc);
1906
1907 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1908 } else {
1909 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
1910 arch->pc);
1911 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1912 }
1913
1914 kvm_change_c0_guest_cause(cop0, (0xff), (T_TLB_MOD << CAUSEB_EXCCODE));
1915
1916 /* setup badvaddr, context and entryhi registers for the guest */
1917 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1918 /* XXXKYMA: is the context register used by linux??? */
1919 kvm_write_c0_guest_entryhi(cop0, entryhi);
1920 /* Blow away the shadow host TLBs */
1921 kvm_mips_flush_host_tlb(1);
1922
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001923 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001924}
1925
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001926enum emulation_result kvm_mips_emulate_fpu_exc(unsigned long cause,
1927 uint32_t *opc,
1928 struct kvm_run *run,
1929 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001930{
1931 struct mips_coproc *cop0 = vcpu->arch.cop0;
1932 struct kvm_vcpu_arch *arch = &vcpu->arch;
Sanjay Lale685c682012-11-21 18:34:04 -08001933
1934 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1935 /* save old pc */
1936 kvm_write_c0_guest_epc(cop0, arch->pc);
1937 kvm_set_c0_guest_status(cop0, ST0_EXL);
1938
1939 if (cause & CAUSEF_BD)
1940 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1941 else
1942 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1943
1944 }
1945
1946 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1947
1948 kvm_change_c0_guest_cause(cop0, (0xff),
1949 (T_COP_UNUSABLE << CAUSEB_EXCCODE));
1950 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
1951
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001952 return EMULATE_DONE;
Sanjay Lale685c682012-11-21 18:34:04 -08001953}
1954
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001955enum emulation_result kvm_mips_emulate_ri_exc(unsigned long cause,
1956 uint32_t *opc,
1957 struct kvm_run *run,
1958 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001959{
1960 struct mips_coproc *cop0 = vcpu->arch.cop0;
1961 struct kvm_vcpu_arch *arch = &vcpu->arch;
1962 enum emulation_result er = EMULATE_DONE;
1963
1964 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1965 /* save old pc */
1966 kvm_write_c0_guest_epc(cop0, arch->pc);
1967 kvm_set_c0_guest_status(cop0, ST0_EXL);
1968
1969 if (cause & CAUSEF_BD)
1970 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1971 else
1972 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1973
1974 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
1975
1976 kvm_change_c0_guest_cause(cop0, (0xff),
1977 (T_RES_INST << CAUSEB_EXCCODE));
1978
1979 /* Set PC to the exception entry point */
1980 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1981
1982 } else {
1983 kvm_err("Trying to deliver RI when EXL is already set\n");
1984 er = EMULATE_FAIL;
1985 }
1986
1987 return er;
1988}
1989
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001990enum emulation_result kvm_mips_emulate_bp_exc(unsigned long cause,
1991 uint32_t *opc,
1992 struct kvm_run *run,
1993 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08001994{
1995 struct mips_coproc *cop0 = vcpu->arch.cop0;
1996 struct kvm_vcpu_arch *arch = &vcpu->arch;
1997 enum emulation_result er = EMULATE_DONE;
1998
1999 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2000 /* save old pc */
2001 kvm_write_c0_guest_epc(cop0, arch->pc);
2002 kvm_set_c0_guest_status(cop0, ST0_EXL);
2003
2004 if (cause & CAUSEF_BD)
2005 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2006 else
2007 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2008
2009 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2010
2011 kvm_change_c0_guest_cause(cop0, (0xff),
2012 (T_BREAK << CAUSEB_EXCCODE));
2013
2014 /* Set PC to the exception entry point */
2015 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2016
2017 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002018 kvm_err("Trying to deliver BP when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002019 er = EMULATE_FAIL;
2020 }
2021
2022 return er;
2023}
2024
James Hogan0a560422015-02-06 16:03:57 +00002025enum emulation_result kvm_mips_emulate_trap_exc(unsigned long cause,
2026 uint32_t *opc,
2027 struct kvm_run *run,
2028 struct kvm_vcpu *vcpu)
2029{
2030 struct mips_coproc *cop0 = vcpu->arch.cop0;
2031 struct kvm_vcpu_arch *arch = &vcpu->arch;
2032 enum emulation_result er = EMULATE_DONE;
2033
2034 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2035 /* save old pc */
2036 kvm_write_c0_guest_epc(cop0, arch->pc);
2037 kvm_set_c0_guest_status(cop0, ST0_EXL);
2038
2039 if (cause & CAUSEF_BD)
2040 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2041 else
2042 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2043
2044 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2045
2046 kvm_change_c0_guest_cause(cop0, (0xff),
2047 (T_TRAP << CAUSEB_EXCCODE));
2048
2049 /* Set PC to the exception entry point */
2050 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2051
2052 } else {
2053 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2054 er = EMULATE_FAIL;
2055 }
2056
2057 return er;
2058}
2059
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002060/* ll/sc, rdhwr, sync emulation */
Sanjay Lale685c682012-11-21 18:34:04 -08002061
2062#define OPCODE 0xfc000000
2063#define BASE 0x03e00000
2064#define RT 0x001f0000
2065#define OFFSET 0x0000ffff
2066#define LL 0xc0000000
2067#define SC 0xe0000000
2068#define SPEC0 0x00000000
2069#define SPEC3 0x7c000000
2070#define RD 0x0000f800
2071#define FUNC 0x0000003f
2072#define SYNC 0x0000000f
2073#define RDHWR 0x0000003b
2074
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002075enum emulation_result kvm_mips_handle_ri(unsigned long cause, uint32_t *opc,
2076 struct kvm_run *run,
2077 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002078{
2079 struct mips_coproc *cop0 = vcpu->arch.cop0;
2080 struct kvm_vcpu_arch *arch = &vcpu->arch;
2081 enum emulation_result er = EMULATE_DONE;
2082 unsigned long curr_pc;
2083 uint32_t inst;
2084
2085 /*
2086 * Update PC and hold onto current PC in case there is
2087 * an error and we want to rollback the PC
2088 */
2089 curr_pc = vcpu->arch.pc;
2090 er = update_pc(vcpu, cause);
2091 if (er == EMULATE_FAIL)
2092 return er;
2093
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002094 /* Fetch the instruction. */
Sanjay Lale685c682012-11-21 18:34:04 -08002095 if (cause & CAUSEF_BD)
2096 opc += 1;
2097
2098 inst = kvm_get_inst(opc, vcpu);
2099
2100 if (inst == KVM_INVALID_INST) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002101 kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
Sanjay Lale685c682012-11-21 18:34:04 -08002102 return EMULATE_FAIL;
2103 }
2104
2105 if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
James Hogan26f4f3b2014-03-14 13:06:09 +00002106 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002107 int rd = (inst & RD) >> 11;
2108 int rt = (inst & RT) >> 16;
James Hogan26f4f3b2014-03-14 13:06:09 +00002109 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2110 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2111 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2112 rd, opc);
2113 goto emulate_ri;
2114 }
Sanjay Lale685c682012-11-21 18:34:04 -08002115 switch (rd) {
2116 case 0: /* CPU number */
2117 arch->gprs[rt] = 0;
2118 break;
2119 case 1: /* SYNCI length */
2120 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2121 current_cpu_data.icache.linesz);
2122 break;
2123 case 2: /* Read count register */
James Hogane30492b2014-05-29 10:16:35 +01002124 arch->gprs[rt] = kvm_mips_read_count(vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002125 break;
2126 case 3: /* Count register resolution */
2127 switch (current_cpu_data.cputype) {
2128 case CPU_20KC:
2129 case CPU_25KF:
2130 arch->gprs[rt] = 1;
2131 break;
2132 default:
2133 arch->gprs[rt] = 2;
2134 }
2135 break;
2136 case 29:
Sanjay Lale685c682012-11-21 18:34:04 -08002137 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
Sanjay Lale685c682012-11-21 18:34:04 -08002138 break;
2139
2140 default:
James Hogan15505672014-03-14 13:06:07 +00002141 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
James Hogan26f4f3b2014-03-14 13:06:09 +00002142 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002143 }
2144 } else {
James Hogan15505672014-03-14 13:06:07 +00002145 kvm_debug("Emulate RI not supported @ %p: %#x\n", opc, inst);
James Hogan26f4f3b2014-03-14 13:06:09 +00002146 goto emulate_ri;
Sanjay Lale685c682012-11-21 18:34:04 -08002147 }
2148
James Hogan26f4f3b2014-03-14 13:06:09 +00002149 return EMULATE_DONE;
2150
2151emulate_ri:
Sanjay Lale685c682012-11-21 18:34:04 -08002152 /*
James Hogan26f4f3b2014-03-14 13:06:09 +00002153 * Rollback PC (if in branch delay slot then the PC already points to
2154 * branch target), and pass the RI exception to the guest OS.
Sanjay Lale685c682012-11-21 18:34:04 -08002155 */
James Hogan26f4f3b2014-03-14 13:06:09 +00002156 vcpu->arch.pc = curr_pc;
2157 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
Sanjay Lale685c682012-11-21 18:34:04 -08002158}
2159
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002160enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2161 struct kvm_run *run)
Sanjay Lale685c682012-11-21 18:34:04 -08002162{
2163 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2164 enum emulation_result er = EMULATE_DONE;
2165 unsigned long curr_pc;
2166
2167 if (run->mmio.len > sizeof(*gpr)) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002168 kvm_err("Bad MMIO length: %d", run->mmio.len);
Sanjay Lale685c682012-11-21 18:34:04 -08002169 er = EMULATE_FAIL;
2170 goto done;
2171 }
2172
2173 /*
2174 * Update PC and hold onto current PC in case there is
2175 * an error and we want to rollback the PC
2176 */
2177 curr_pc = vcpu->arch.pc;
2178 er = update_pc(vcpu, vcpu->arch.pending_load_cause);
2179 if (er == EMULATE_FAIL)
2180 return er;
2181
2182 switch (run->mmio.len) {
2183 case 4:
2184 *gpr = *(int32_t *) run->mmio.data;
2185 break;
2186
2187 case 2:
2188 if (vcpu->mmio_needed == 2)
2189 *gpr = *(int16_t *) run->mmio.data;
2190 else
2191 *gpr = *(int16_t *) run->mmio.data;
2192
2193 break;
2194 case 1:
2195 if (vcpu->mmio_needed == 2)
2196 *gpr = *(int8_t *) run->mmio.data;
2197 else
2198 *gpr = *(u8 *) run->mmio.data;
2199 break;
2200 }
2201
2202 if (vcpu->arch.pending_load_cause & CAUSEF_BD)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002203 kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
2204 vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
2205 vcpu->mmio_needed);
Sanjay Lale685c682012-11-21 18:34:04 -08002206
2207done:
2208 return er;
2209}
2210
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002211static enum emulation_result kvm_mips_emulate_exc(unsigned long cause,
2212 uint32_t *opc,
2213 struct kvm_run *run,
2214 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002215{
2216 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2217 struct mips_coproc *cop0 = vcpu->arch.cop0;
2218 struct kvm_vcpu_arch *arch = &vcpu->arch;
2219 enum emulation_result er = EMULATE_DONE;
2220
2221 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2222 /* save old pc */
2223 kvm_write_c0_guest_epc(cop0, arch->pc);
2224 kvm_set_c0_guest_status(cop0, ST0_EXL);
2225
2226 if (cause & CAUSEF_BD)
2227 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2228 else
2229 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2230
2231 kvm_change_c0_guest_cause(cop0, (0xff),
2232 (exccode << CAUSEB_EXCCODE));
2233
2234 /* Set PC to the exception entry point */
2235 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2236 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2237
2238 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2239 exccode, kvm_read_c0_guest_epc(cop0),
2240 kvm_read_c0_guest_badvaddr(cop0));
2241 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002242 kvm_err("Trying to deliver EXC when EXL is already set\n");
Sanjay Lale685c682012-11-21 18:34:04 -08002243 er = EMULATE_FAIL;
2244 }
2245
2246 return er;
2247}
2248
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002249enum emulation_result kvm_mips_check_privilege(unsigned long cause,
2250 uint32_t *opc,
2251 struct kvm_run *run,
2252 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002253{
2254 enum emulation_result er = EMULATE_DONE;
2255 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2256 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2257
2258 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2259
2260 if (usermode) {
2261 switch (exccode) {
2262 case T_INT:
2263 case T_SYSCALL:
2264 case T_BREAK:
2265 case T_RES_INST:
James Hogan0a560422015-02-06 16:03:57 +00002266 case T_TRAP:
James Hogan98119ad2015-02-06 11:11:56 +00002267 case T_MSADIS:
Sanjay Lale685c682012-11-21 18:34:04 -08002268 break;
2269
2270 case T_COP_UNUSABLE:
2271 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2272 er = EMULATE_PRIV_FAIL;
2273 break;
2274
2275 case T_TLB_MOD:
2276 break;
2277
2278 case T_TLB_LD_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002279 /*
2280 * We we are accessing Guest kernel space, then send an
2281 * address error exception to the guest
2282 */
Sanjay Lale685c682012-11-21 18:34:04 -08002283 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002284 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2285 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002286 cause &= ~0xff;
2287 cause |= (T_ADDR_ERR_LD << CAUSEB_EXCCODE);
2288 er = EMULATE_PRIV_FAIL;
2289 }
2290 break;
2291
2292 case T_TLB_ST_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002293 /*
2294 * We we are accessing Guest kernel space, then send an
2295 * address error exception to the guest
2296 */
Sanjay Lale685c682012-11-21 18:34:04 -08002297 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002298 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2299 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002300 cause &= ~0xff;
2301 cause |= (T_ADDR_ERR_ST << CAUSEB_EXCCODE);
2302 er = EMULATE_PRIV_FAIL;
2303 }
2304 break;
2305
2306 case T_ADDR_ERR_ST:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002307 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2308 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002309 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2310 cause &= ~0xff;
2311 cause |= (T_TLB_ST_MISS << CAUSEB_EXCCODE);
2312 }
2313 er = EMULATE_PRIV_FAIL;
2314 break;
2315 case T_ADDR_ERR_LD:
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002316 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2317 badvaddr);
Sanjay Lale685c682012-11-21 18:34:04 -08002318 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2319 cause &= ~0xff;
2320 cause |= (T_TLB_LD_MISS << CAUSEB_EXCCODE);
2321 }
2322 er = EMULATE_PRIV_FAIL;
2323 break;
2324 default:
2325 er = EMULATE_PRIV_FAIL;
2326 break;
2327 }
2328 }
2329
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002330 if (er == EMULATE_PRIV_FAIL)
Sanjay Lale685c682012-11-21 18:34:04 -08002331 kvm_mips_emulate_exc(cause, opc, run, vcpu);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002332
Sanjay Lale685c682012-11-21 18:34:04 -08002333 return er;
2334}
2335
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002336/*
2337 * User Address (UA) fault, this could happen if
Sanjay Lale685c682012-11-21 18:34:04 -08002338 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2339 * case we pass on the fault to the guest kernel and let it handle it.
2340 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2341 * case we inject the TLB from the Guest TLB into the shadow host TLB
2342 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002343enum emulation_result kvm_mips_handle_tlbmiss(unsigned long cause,
2344 uint32_t *opc,
2345 struct kvm_run *run,
2346 struct kvm_vcpu *vcpu)
Sanjay Lale685c682012-11-21 18:34:04 -08002347{
2348 enum emulation_result er = EMULATE_DONE;
2349 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2350 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2351 int index;
2352
2353 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
2354 vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
2355
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002356 /*
2357 * KVM would not have got the exception if this entry was valid in the
2358 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2359 * send the guest an exception. The guest exc handler should then inject
2360 * an entry into the guest TLB.
Sanjay Lale685c682012-11-21 18:34:04 -08002361 */
2362 index = kvm_mips_guest_tlb_lookup(vcpu,
2363 (va & VPN2_MASK) |
David Daney48c4ac92013-05-13 13:56:44 -07002364 (kvm_read_c0_guest_entryhi
2365 (vcpu->arch.cop0) & ASID_MASK));
Sanjay Lale685c682012-11-21 18:34:04 -08002366 if (index < 0) {
2367 if (exccode == T_TLB_LD_MISS) {
2368 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
2369 } else if (exccode == T_TLB_ST_MISS) {
2370 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2371 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002372 kvm_err("%s: invalid exc code: %d\n", __func__,
2373 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002374 er = EMULATE_FAIL;
2375 }
2376 } else {
2377 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2378
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002379 /*
2380 * Check if the entry is valid, if not then setup a TLB invalid
2381 * exception to the guest
2382 */
Sanjay Lale685c682012-11-21 18:34:04 -08002383 if (!TLB_IS_VALID(*tlb, va)) {
2384 if (exccode == T_TLB_LD_MISS) {
2385 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2386 vcpu);
2387 } else if (exccode == T_TLB_ST_MISS) {
2388 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2389 vcpu);
2390 } else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07002391 kvm_err("%s: invalid exc code: %d\n", __func__,
2392 exccode);
Sanjay Lale685c682012-11-21 18:34:04 -08002393 er = EMULATE_FAIL;
2394 }
2395 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002396 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
2397 tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
2398 /*
2399 * OK we have a Guest TLB entry, now inject it into the
2400 * shadow host TLB
2401 */
Sanjay Lale685c682012-11-21 18:34:04 -08002402 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
2403 NULL);
2404 }
2405 }
2406
2407 return er;
2408}