2 * 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
6 * KVM/MIPS: Instruction/Exception emulation
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/ktime.h>
15 #include <linux/kvm_host.h>
16 #include <linux/vmalloc.h>
18 #include <linux/memblock.h>
19 #include <linux/random.h>
21 #include <asm/cacheflush.h>
22 #include <asm/cacheops.h>
23 #include <asm/cpu-info.h>
24 #include <asm/mmu_context.h>
25 #include <asm/tlbflush.h>
29 #include <asm/r4kcache.h>
30 #define CONFIG_MIPS_MT
32 #include "interrupt.h"
37 * Compute the return address and do emulate branch simulation, if required.
38 * This function should be called only in branch delay slot active.
40 static int kvm_compute_return_epc(struct kvm_vcpu
*vcpu
, unsigned long instpc
,
43 unsigned int dspcontrol
;
44 union mips_instruction insn
;
45 struct kvm_vcpu_arch
*arch
= &vcpu
->arch
;
51 kvm_err("%s: unaligned epc\n", __func__
);
55 /* Read the instruction */
56 err
= kvm_get_badinstrp((u32
*)epc
, vcpu
, &insn
.word
);
60 switch (insn
.i_format
.opcode
) {
61 /* jr and jalr are in r_format format. */
63 switch (insn
.r_format
.func
) {
65 arch
->gprs
[insn
.r_format
.rd
] = epc
+ 8;
68 nextpc
= arch
->gprs
[insn
.r_format
.rs
];
76 * This group contains:
77 * bltz_op, bgez_op, bltzl_op, bgezl_op,
78 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
81 switch (insn
.i_format
.rt
) {
84 if ((long)arch
->gprs
[insn
.i_format
.rs
] < 0)
85 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
93 if ((long)arch
->gprs
[insn
.i_format
.rs
] >= 0)
94 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
102 arch
->gprs
[31] = epc
+ 8;
103 if ((long)arch
->gprs
[insn
.i_format
.rs
] < 0)
104 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
112 arch
->gprs
[31] = epc
+ 8;
113 if ((long)arch
->gprs
[insn
.i_format
.rs
] >= 0)
114 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
121 kvm_err("%s: DSP branch but not DSP ASE\n",
126 dspcontrol
= rddsp(0x01);
128 if (dspcontrol
>= 32)
129 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
139 /* These are unconditional and in j_format. */
141 arch
->gprs
[31] = instpc
+ 8;
147 epc
|= (insn
.j_format
.target
<< 2);
151 /* These are conditional and in i_format. */
154 if (arch
->gprs
[insn
.i_format
.rs
] ==
155 arch
->gprs
[insn
.i_format
.rt
])
156 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
164 if (arch
->gprs
[insn
.i_format
.rs
] !=
165 arch
->gprs
[insn
.i_format
.rt
])
166 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
172 case blez_op
: /* POP06 */
173 #ifndef CONFIG_CPU_MIPSR6
174 case blezl_op
: /* removed in R6 */
176 if (insn
.i_format
.rt
!= 0)
178 if ((long)arch
->gprs
[insn
.i_format
.rs
] <= 0)
179 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
185 case bgtz_op
: /* POP07 */
186 #ifndef CONFIG_CPU_MIPSR6
187 case bgtzl_op
: /* removed in R6 */
189 if (insn
.i_format
.rt
!= 0)
191 if ((long)arch
->gprs
[insn
.i_format
.rs
] > 0)
192 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
198 /* And now the FPA/cp1 branch instructions. */
200 kvm_err("%s: unsupported cop1_op\n", __func__
);
203 #ifdef CONFIG_CPU_MIPSR6
204 /* R6 added the following compact branches with forbidden slots */
205 case blezl_op
: /* POP26 */
206 case bgtzl_op
: /* POP27 */
207 /* only rt == 0 isn't compact branch */
208 if (insn
.i_format
.rt
!= 0)
213 /* only rs == rt == 0 is reserved, rest are compact branches */
214 if (insn
.i_format
.rs
!= 0 || insn
.i_format
.rt
!= 0)
219 /* only rs == 0 isn't compact branch */
220 if (insn
.i_format
.rs
!= 0)
225 * If we've hit an exception on the forbidden slot, then
226 * the branch must not have been taken.
233 /* Fall through - Compact branches not supported before R6 */
243 enum emulation_result
update_pc(struct kvm_vcpu
*vcpu
, u32 cause
)
247 if (cause
& CAUSEF_BD
) {
248 err
= kvm_compute_return_epc(vcpu
, vcpu
->arch
.pc
,
256 kvm_debug("update_pc(): New PC: %#lx\n", vcpu
->arch
.pc
);
262 * kvm_get_badinstr() - Get bad instruction encoding.
263 * @opc: Guest pointer to faulting instruction.
264 * @vcpu: KVM VCPU information.
266 * Gets the instruction encoding of the faulting instruction, using the saved
267 * BadInstr register value if it exists, otherwise falling back to reading guest
270 * Returns: The instruction encoding of the faulting instruction.
272 int kvm_get_badinstr(u32
*opc
, struct kvm_vcpu
*vcpu
, u32
*out
)
274 if (cpu_has_badinstr
) {
275 *out
= vcpu
->arch
.host_cp0_badinstr
;
278 WARN_ONCE(1, "CPU doesn't have BadInstr register\n");
284 * kvm_get_badinstrp() - Get bad prior instruction encoding.
285 * @opc: Guest pointer to prior faulting instruction.
286 * @vcpu: KVM VCPU information.
288 * Gets the instruction encoding of the prior faulting instruction (the branch
289 * containing the delay slot which faulted), using the saved BadInstrP register
290 * value if it exists, otherwise falling back to reading guest memory at @opc.
292 * Returns: The instruction encoding of the prior faulting instruction.
294 int kvm_get_badinstrp(u32
*opc
, struct kvm_vcpu
*vcpu
, u32
*out
)
296 if (cpu_has_badinstrp
) {
297 *out
= vcpu
->arch
.host_cp0_badinstrp
;
300 WARN_ONCE(1, "CPU doesn't have BadInstrp register\n");
306 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
307 * @vcpu: Virtual CPU.
309 * Returns: 1 if the CP0_Count timer is disabled by either the guest
310 * CP0_Cause.DC bit or the count_ctl.DC bit.
311 * 0 otherwise (in which case CP0_Count timer is running).
313 int kvm_mips_count_disabled(struct kvm_vcpu
*vcpu
)
315 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
317 return (vcpu
->arch
.count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
) ||
318 (kvm_read_c0_guest_cause(cop0
) & CAUSEF_DC
);
322 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
324 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
326 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
328 static u32
kvm_mips_ktime_to_count(struct kvm_vcpu
*vcpu
, ktime_t now
)
333 now_ns
= ktime_to_ns(now
);
334 delta
= now_ns
+ vcpu
->arch
.count_dyn_bias
;
336 if (delta
>= vcpu
->arch
.count_period
) {
337 /* If delta is out of safe range the bias needs adjusting */
338 periods
= div64_s64(now_ns
, vcpu
->arch
.count_period
);
339 vcpu
->arch
.count_dyn_bias
= -periods
* vcpu
->arch
.count_period
;
340 /* Recalculate delta with new bias */
341 delta
= now_ns
+ vcpu
->arch
.count_dyn_bias
;
345 * We've ensured that:
346 * delta < count_period
348 * Therefore the intermediate delta*count_hz will never overflow since
349 * at the boundary condition:
350 * delta = count_period
351 * delta = NSEC_PER_SEC * 2^32 / count_hz
352 * delta * count_hz = NSEC_PER_SEC * 2^32
354 return div_u64(delta
* vcpu
->arch
.count_hz
, NSEC_PER_SEC
);
358 * kvm_mips_count_time() - Get effective current time.
359 * @vcpu: Virtual CPU.
361 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
362 * except when the master disable bit is set in count_ctl, in which case it is
363 * count_resume, i.e. the time that the count was disabled.
365 * Returns: Effective monotonic ktime for CP0_Count.
367 static inline ktime_t
kvm_mips_count_time(struct kvm_vcpu
*vcpu
)
369 if (unlikely(vcpu
->arch
.count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
))
370 return vcpu
->arch
.count_resume
;
376 * kvm_mips_read_count_running() - Read the current count value as if running.
377 * @vcpu: Virtual CPU.
378 * @now: Kernel time to read CP0_Count at.
380 * Returns the current guest CP0_Count register at time @now and handles if the
381 * timer interrupt is pending and hasn't been handled yet.
383 * Returns: The current value of the guest CP0_Count register.
385 static u32
kvm_mips_read_count_running(struct kvm_vcpu
*vcpu
, ktime_t now
)
387 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
388 ktime_t expires
, threshold
;
392 /* Calculate the biased and scaled guest CP0_Count */
393 count
= vcpu
->arch
.count_bias
+ kvm_mips_ktime_to_count(vcpu
, now
);
394 compare
= kvm_read_c0_guest_compare(cop0
);
397 * Find whether CP0_Count has reached the closest timer interrupt. If
398 * not, we shouldn't inject it.
400 if ((s32
)(count
- compare
) < 0)
404 * The CP0_Count we're going to return has already reached the closest
405 * timer interrupt. Quickly check if it really is a new interrupt by
406 * looking at whether the interval until the hrtimer expiry time is
407 * less than 1/4 of the timer period.
409 expires
= hrtimer_get_expires(&vcpu
->arch
.comparecount_timer
);
410 threshold
= ktime_add_ns(now
, vcpu
->arch
.count_period
/ 4);
411 if (ktime_before(expires
, threshold
)) {
413 * Cancel it while we handle it so there's no chance of
414 * interference with the timeout handler.
416 running
= hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
418 /* Nothing should be waiting on the timeout */
419 kvm_mips_callbacks
->queue_timer_int(vcpu
);
422 * Restart the timer if it was running based on the expiry time
423 * we read, so that we don't push it back 2 periods.
426 expires
= ktime_add_ns(expires
,
427 vcpu
->arch
.count_period
);
428 hrtimer_start(&vcpu
->arch
.comparecount_timer
, expires
,
437 * kvm_mips_read_count() - Read the current count value.
438 * @vcpu: Virtual CPU.
440 * Read the current guest CP0_Count value, taking into account whether the timer
443 * Returns: The current guest CP0_Count value.
445 u32
kvm_mips_read_count(struct kvm_vcpu
*vcpu
)
447 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
449 /* If count disabled just read static copy of count */
450 if (kvm_mips_count_disabled(vcpu
))
451 return kvm_read_c0_guest_count(cop0
);
453 return kvm_mips_read_count_running(vcpu
, ktime_get());
457 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
458 * @vcpu: Virtual CPU.
459 * @count: Output pointer for CP0_Count value at point of freeze.
461 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
462 * at the point it was frozen. It is guaranteed that any pending interrupts at
463 * the point it was frozen are handled, and none after that point.
465 * This is useful where the time/CP0_Count is needed in the calculation of the
468 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
470 * Returns: The ktime at the point of freeze.
472 ktime_t
kvm_mips_freeze_hrtimer(struct kvm_vcpu
*vcpu
, u32
*count
)
476 /* stop hrtimer before finding time */
477 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
480 /* find count at this point and handle pending hrtimer */
481 *count
= kvm_mips_read_count_running(vcpu
, now
);
487 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
488 * @vcpu: Virtual CPU.
489 * @now: ktime at point of resume.
490 * @count: CP0_Count at point of resume.
492 * Resumes the timer and updates the timer expiry based on @now and @count.
493 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
494 * parameters need to be changed.
496 * It is guaranteed that a timer interrupt immediately after resume will be
497 * handled, but not if CP_Compare is exactly at @count. That case is already
498 * handled by kvm_mips_freeze_timer().
500 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
502 static void kvm_mips_resume_hrtimer(struct kvm_vcpu
*vcpu
,
503 ktime_t now
, u32 count
)
505 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
510 /* Calculate timeout (wrap 0 to 2^32) */
511 compare
= kvm_read_c0_guest_compare(cop0
);
512 delta
= (u64
)(u32
)(compare
- count
- 1) + 1;
513 delta
= div_u64(delta
* NSEC_PER_SEC
, vcpu
->arch
.count_hz
);
514 expire
= ktime_add_ns(now
, delta
);
516 /* Update hrtimer to use new timeout */
517 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
518 hrtimer_start(&vcpu
->arch
.comparecount_timer
, expire
, HRTIMER_MODE_ABS
);
522 * kvm_mips_restore_hrtimer() - Restore hrtimer after a gap, updating expiry.
523 * @vcpu: Virtual CPU.
524 * @before: Time before Count was saved, lower bound of drift calculation.
525 * @count: CP0_Count at point of restore.
526 * @min_drift: Minimum amount of drift permitted before correction.
529 * Restores the timer from a particular @count, accounting for drift. This can
530 * be used in conjunction with kvm_mips_freeze_timer() when a hardware timer is
531 * to be used for a period of time, but the exact ktime corresponding to the
532 * final Count that must be restored is not known.
534 * It is guaranteed that a timer interrupt immediately after restore will be
535 * handled, but not if CP0_Compare is exactly at @count. That case should
536 * already be handled when the hardware timer state is saved.
538 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is not
541 * Returns: Amount of correction to count_bias due to drift.
543 int kvm_mips_restore_hrtimer(struct kvm_vcpu
*vcpu
, ktime_t before
,
544 u32 count
, int min_drift
)
546 ktime_t now
, count_time
;
547 u32 now_count
, before_count
;
551 /* Calculate expected count at before */
552 before_count
= vcpu
->arch
.count_bias
+
553 kvm_mips_ktime_to_count(vcpu
, before
);
556 * Detect significantly negative drift, where count is lower than
557 * expected. Some negative drift is expected when hardware counter is
558 * set after kvm_mips_freeze_timer(), and it is harmless to allow the
559 * time to jump forwards a little, within reason. If the drift is too
560 * significant, adjust the bias to avoid a big Guest.CP0_Count jump.
562 drift
= count
- before_count
;
563 if (drift
< min_drift
) {
565 vcpu
->arch
.count_bias
+= drift
;
570 /* Calculate expected count right now */
572 now_count
= vcpu
->arch
.count_bias
+ kvm_mips_ktime_to_count(vcpu
, now
);
575 * Detect positive drift, where count is higher than expected, and
576 * adjust the bias to avoid guest time going backwards.
578 drift
= count
- now_count
;
581 vcpu
->arch
.count_bias
+= drift
;
586 /* Subtract nanosecond delta to find ktime when count was read */
587 delta
= (u64
)(u32
)(now_count
- count
);
588 delta
= div_u64(delta
* NSEC_PER_SEC
, vcpu
->arch
.count_hz
);
589 count_time
= ktime_sub_ns(now
, delta
);
592 /* Resume using the calculated ktime */
593 kvm_mips_resume_hrtimer(vcpu
, count_time
, count
);
598 * kvm_mips_write_count() - Modify the count and update timer.
599 * @vcpu: Virtual CPU.
600 * @count: Guest CP0_Count value to set.
602 * Sets the CP0_Count value and updates the timer accordingly.
604 void kvm_mips_write_count(struct kvm_vcpu
*vcpu
, u32 count
)
606 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
610 now
= kvm_mips_count_time(vcpu
);
611 vcpu
->arch
.count_bias
= count
- kvm_mips_ktime_to_count(vcpu
, now
);
613 if (kvm_mips_count_disabled(vcpu
))
614 /* The timer's disabled, adjust the static count */
615 kvm_write_c0_guest_count(cop0
, count
);
618 kvm_mips_resume_hrtimer(vcpu
, now
, count
);
622 * kvm_mips_init_count() - Initialise timer.
623 * @vcpu: Virtual CPU.
624 * @count_hz: Frequency of timer.
626 * Initialise the timer to the specified frequency, zero it, and set it going if
629 void kvm_mips_init_count(struct kvm_vcpu
*vcpu
, unsigned long count_hz
)
631 vcpu
->arch
.count_hz
= count_hz
;
632 vcpu
->arch
.count_period
= div_u64((u64
)NSEC_PER_SEC
<< 32, count_hz
);
633 vcpu
->arch
.count_dyn_bias
= 0;
636 kvm_mips_write_count(vcpu
, 0);
640 * kvm_mips_set_count_hz() - Update the frequency of the timer.
641 * @vcpu: Virtual CPU.
642 * @count_hz: Frequency of CP0_Count timer in Hz.
644 * Change the frequency of the CP0_Count timer. This is done atomically so that
645 * CP0_Count is continuous and no timer interrupt is lost.
647 * Returns: -EINVAL if @count_hz is out of range.
650 int kvm_mips_set_count_hz(struct kvm_vcpu
*vcpu
, s64 count_hz
)
652 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
657 /* ensure the frequency is in a sensible range... */
658 if (count_hz
<= 0 || count_hz
> NSEC_PER_SEC
)
660 /* ... and has actually changed */
661 if (vcpu
->arch
.count_hz
== count_hz
)
664 /* Safely freeze timer so we can keep it continuous */
665 dc
= kvm_mips_count_disabled(vcpu
);
667 now
= kvm_mips_count_time(vcpu
);
668 count
= kvm_read_c0_guest_count(cop0
);
670 now
= kvm_mips_freeze_hrtimer(vcpu
, &count
);
673 /* Update the frequency */
674 vcpu
->arch
.count_hz
= count_hz
;
675 vcpu
->arch
.count_period
= div_u64((u64
)NSEC_PER_SEC
<< 32, count_hz
);
676 vcpu
->arch
.count_dyn_bias
= 0;
678 /* Calculate adjusted bias so dynamic count is unchanged */
679 vcpu
->arch
.count_bias
= count
- kvm_mips_ktime_to_count(vcpu
, now
);
681 /* Update and resume hrtimer */
683 kvm_mips_resume_hrtimer(vcpu
, now
, count
);
688 * kvm_mips_write_compare() - Modify compare and update timer.
689 * @vcpu: Virtual CPU.
690 * @compare: New CP0_Compare value.
691 * @ack: Whether to acknowledge timer interrupt.
693 * Update CP0_Compare to a new value and update the timeout.
694 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
695 * any pending timer interrupt is preserved.
697 void kvm_mips_write_compare(struct kvm_vcpu
*vcpu
, u32 compare
, bool ack
)
699 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
701 u32 old_compare
= kvm_read_c0_guest_compare(cop0
);
702 s32 delta
= compare
- old_compare
;
704 ktime_t now
= ktime_set(0, 0); /* silence bogus GCC warning */
707 /* if unchanged, must just be an ack */
708 if (old_compare
== compare
) {
711 kvm_mips_callbacks
->dequeue_timer_int(vcpu
);
712 kvm_write_c0_guest_compare(cop0
, compare
);
717 * If guest CP0_Compare moves forward, CP0_GTOffset should be adjusted
718 * too to prevent guest CP0_Count hitting guest CP0_Compare.
720 * The new GTOffset corresponds to the new value of CP0_Compare, and is
721 * set prior to it being written into the guest context. We disable
722 * preemption until the new value is written to prevent restore of a
723 * GTOffset corresponding to the old CP0_Compare value.
727 write_c0_gtoffset(compare
- read_c0_count());
728 back_to_back_c0_hazard();
731 /* freeze_hrtimer() takes care of timer interrupts <= count */
732 dc
= kvm_mips_count_disabled(vcpu
);
734 now
= kvm_mips_freeze_hrtimer(vcpu
, &count
);
737 kvm_mips_callbacks
->dequeue_timer_int(vcpu
);
740 * With VZ, writing CP0_Compare acks (clears) CP0_Cause.TI, so
741 * preserve guest CP0_Cause.TI if we don't want to ack it.
743 cause
= kvm_read_c0_guest_cause(cop0
);
745 kvm_write_c0_guest_compare(cop0
, compare
);
750 back_to_back_c0_hazard();
752 if (!ack
&& cause
& CAUSEF_TI
)
753 kvm_write_c0_guest_cause(cop0
, cause
);
755 /* resume_hrtimer() takes care of timer interrupts > count */
757 kvm_mips_resume_hrtimer(vcpu
, now
, count
);
760 * If guest CP0_Compare is moving backward, we delay CP0_GTOffset change
761 * until after the new CP0_Compare is written, otherwise new guest
762 * CP0_Count could hit new guest CP0_Compare.
765 write_c0_gtoffset(compare
- read_c0_count());
769 * kvm_mips_count_disable() - Disable count.
770 * @vcpu: Virtual CPU.
772 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
773 * time will be handled but not after.
775 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
776 * count_ctl.DC has been set (count disabled).
778 * Returns: The time that the timer was stopped.
780 static ktime_t
kvm_mips_count_disable(struct kvm_vcpu
*vcpu
)
782 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
787 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
789 /* Set the static count from the dynamic count, handling pending TI */
791 count
= kvm_mips_read_count_running(vcpu
, now
);
792 kvm_write_c0_guest_count(cop0
, count
);
798 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
799 * @vcpu: Virtual CPU.
801 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
802 * before the final stop time will be handled if the timer isn't disabled by
803 * count_ctl.DC, but not after.
805 * Assumes CP0_Cause.DC is clear (count enabled).
807 void kvm_mips_count_disable_cause(struct kvm_vcpu
*vcpu
)
809 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
811 kvm_set_c0_guest_cause(cop0
, CAUSEF_DC
);
812 if (!(vcpu
->arch
.count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
))
813 kvm_mips_count_disable(vcpu
);
817 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
818 * @vcpu: Virtual CPU.
820 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
821 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
822 * potentially before even returning, so the caller should be careful with
823 * ordering of CP0_Cause modifications so as not to lose it.
825 * Assumes CP0_Cause.DC is set (count disabled).
827 void kvm_mips_count_enable_cause(struct kvm_vcpu
*vcpu
)
829 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
832 kvm_clear_c0_guest_cause(cop0
, CAUSEF_DC
);
835 * Set the dynamic count to match the static count.
836 * This starts the hrtimer if count_ctl.DC allows it.
837 * Otherwise it conveniently updates the biases.
839 count
= kvm_read_c0_guest_count(cop0
);
840 kvm_mips_write_count(vcpu
, count
);
844 * kvm_mips_set_count_ctl() - Update the count control KVM register.
845 * @vcpu: Virtual CPU.
846 * @count_ctl: Count control register new value.
848 * Set the count control KVM register. The timer is updated accordingly.
850 * Returns: -EINVAL if reserved bits are set.
853 int kvm_mips_set_count_ctl(struct kvm_vcpu
*vcpu
, s64 count_ctl
)
855 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
856 s64 changed
= count_ctl
^ vcpu
->arch
.count_ctl
;
861 /* Only allow defined bits to be changed */
862 if (changed
& ~(s64
)(KVM_REG_MIPS_COUNT_CTL_DC
))
865 /* Apply new value */
866 vcpu
->arch
.count_ctl
= count_ctl
;
868 /* Master CP0_Count disable */
869 if (changed
& KVM_REG_MIPS_COUNT_CTL_DC
) {
870 /* Is CP0_Cause.DC already disabling CP0_Count? */
871 if (kvm_read_c0_guest_cause(cop0
) & CAUSEF_DC
) {
872 if (count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)
873 /* Just record the current time */
874 vcpu
->arch
.count_resume
= ktime_get();
875 } else if (count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
) {
876 /* disable timer and record current time */
877 vcpu
->arch
.count_resume
= kvm_mips_count_disable(vcpu
);
880 * Calculate timeout relative to static count at resume
881 * time (wrap 0 to 2^32).
883 count
= kvm_read_c0_guest_count(cop0
);
884 compare
= kvm_read_c0_guest_compare(cop0
);
885 delta
= (u64
)(u32
)(compare
- count
- 1) + 1;
886 delta
= div_u64(delta
* NSEC_PER_SEC
,
887 vcpu
->arch
.count_hz
);
888 expire
= ktime_add_ns(vcpu
->arch
.count_resume
, delta
);
890 /* Handle pending interrupt */
892 if (ktime_compare(now
, expire
) >= 0)
893 /* Nothing should be waiting on the timeout */
894 kvm_mips_callbacks
->queue_timer_int(vcpu
);
896 /* Resume hrtimer without changing bias */
897 count
= kvm_mips_read_count_running(vcpu
, now
);
898 kvm_mips_resume_hrtimer(vcpu
, now
, count
);
906 * kvm_mips_set_count_resume() - Update the count resume KVM register.
907 * @vcpu: Virtual CPU.
908 * @count_resume: Count resume register new value.
910 * Set the count resume KVM register.
912 * Returns: -EINVAL if out of valid range (0..now).
915 int kvm_mips_set_count_resume(struct kvm_vcpu
*vcpu
, s64 count_resume
)
918 * It doesn't make sense for the resume time to be in the future, as it
919 * would be possible for the next interrupt to be more than a full
920 * period in the future.
922 if (count_resume
< 0 || count_resume
> ktime_to_ns(ktime_get()))
925 vcpu
->arch
.count_resume
= ns_to_ktime(count_resume
);
930 * kvm_mips_count_timeout() - Push timer forward on timeout.
931 * @vcpu: Virtual CPU.
933 * Handle an hrtimer event by push the hrtimer forward a period.
935 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
937 enum hrtimer_restart
kvm_mips_count_timeout(struct kvm_vcpu
*vcpu
)
939 /* Add the Count period to the current expiry time */
940 hrtimer_add_expires_ns(&vcpu
->arch
.comparecount_timer
,
941 vcpu
->arch
.count_period
);
942 return HRTIMER_RESTART
;
945 enum emulation_result
kvm_mips_emul_wait(struct kvm_vcpu
*vcpu
)
947 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu
->arch
.pc
,
948 vcpu
->arch
.pending_exceptions
);
950 ++vcpu
->stat
.wait_exits
;
951 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_WAIT
);
952 if (!vcpu
->arch
.pending_exceptions
) {
953 kvm_vz_lose_htimer(vcpu
);
958 * We are runnable, then definitely go off to user space to
959 * check if any I/O interrupts are pending.
961 if (kvm_arch_vcpu_runnable(vcpu
))
962 vcpu
->run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
968 enum emulation_result
kvm_mips_emulate_store(union mips_instruction inst
,
970 struct kvm_vcpu
*vcpu
)
973 enum emulation_result er
;
975 struct kvm_run
*run
= vcpu
->run
;
976 void *data
= run
->mmio
.data
;
978 unsigned long curr_pc
;
981 * Update PC and hold onto current PC in case there is
982 * an error and we want to rollback the PC
984 curr_pc
= vcpu
->arch
.pc
;
985 er
= update_pc(vcpu
, cause
);
986 if (er
== EMULATE_FAIL
)
989 rt
= inst
.i_format
.rt
;
991 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
992 vcpu
->arch
.host_cp0_badvaddr
);
993 if (run
->mmio
.phys_addr
== KVM_INVALID_ADDR
)
996 switch (inst
.i_format
.opcode
) {
997 #if defined(CONFIG_64BIT)
1000 *(u64
*)data
= vcpu
->arch
.gprs
[rt
];
1002 kvm_debug("[%#lx] OP_SD: eaddr: %#lx, gpr: %#lx, data: %#llx\n",
1003 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1004 vcpu
->arch
.gprs
[rt
], *(u64
*)data
);
1010 *(u32
*)data
= vcpu
->arch
.gprs
[rt
];
1012 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1013 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1014 vcpu
->arch
.gprs
[rt
], *(u32
*)data
);
1019 *(u16
*)data
= vcpu
->arch
.gprs
[rt
];
1021 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1022 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1023 vcpu
->arch
.gprs
[rt
], *(u16
*)data
);
1028 *(u8
*)data
= vcpu
->arch
.gprs
[rt
];
1030 kvm_debug("[%#lx] OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1031 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1032 vcpu
->arch
.gprs
[rt
], *(u8
*)data
);
1036 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1037 vcpu
->arch
.host_cp0_badvaddr
) & (~0x3);
1039 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x3;
1042 *(u32
*)data
= ((*(u32
*)data
) & 0xffffff00) |
1043 (vcpu
->arch
.gprs
[rt
] >> 24);
1046 *(u32
*)data
= ((*(u32
*)data
) & 0xffff0000) |
1047 (vcpu
->arch
.gprs
[rt
] >> 16);
1050 *(u32
*)data
= ((*(u32
*)data
) & 0xff000000) |
1051 (vcpu
->arch
.gprs
[rt
] >> 8);
1054 *(u32
*)data
= vcpu
->arch
.gprs
[rt
];
1060 kvm_debug("[%#lx] OP_SWL: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1061 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1062 vcpu
->arch
.gprs
[rt
], *(u32
*)data
);
1066 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1067 vcpu
->arch
.host_cp0_badvaddr
) & (~0x3);
1069 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x3;
1072 *(u32
*)data
= vcpu
->arch
.gprs
[rt
];
1075 *(u32
*)data
= ((*(u32
*)data
) & 0xff) |
1076 (vcpu
->arch
.gprs
[rt
] << 8);
1079 *(u32
*)data
= ((*(u32
*)data
) & 0xffff) |
1080 (vcpu
->arch
.gprs
[rt
] << 16);
1083 *(u32
*)data
= ((*(u32
*)data
) & 0xffffff) |
1084 (vcpu
->arch
.gprs
[rt
] << 24);
1090 kvm_debug("[%#lx] OP_SWR: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1091 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1092 vcpu
->arch
.gprs
[rt
], *(u32
*)data
);
1095 #if defined(CONFIG_64BIT)
1097 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1098 vcpu
->arch
.host_cp0_badvaddr
) & (~0x7);
1101 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x7;
1104 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffffffff00) |
1105 ((vcpu
->arch
.gprs
[rt
] >> 56) & 0xff);
1108 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffffff0000) |
1109 ((vcpu
->arch
.gprs
[rt
] >> 48) & 0xffff);
1112 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffff000000) |
1113 ((vcpu
->arch
.gprs
[rt
] >> 40) & 0xffffff);
1116 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffff00000000) |
1117 ((vcpu
->arch
.gprs
[rt
] >> 32) & 0xffffffff);
1120 *(u64
*)data
= ((*(u64
*)data
) & 0xffffff0000000000) |
1121 ((vcpu
->arch
.gprs
[rt
] >> 24) & 0xffffffffff);
1124 *(u64
*)data
= ((*(u64
*)data
) & 0xffff000000000000) |
1125 ((vcpu
->arch
.gprs
[rt
] >> 16) & 0xffffffffffff);
1128 *(u64
*)data
= ((*(u64
*)data
) & 0xff00000000000000) |
1129 ((vcpu
->arch
.gprs
[rt
] >> 8) & 0xffffffffffffff);
1132 *(u64
*)data
= vcpu
->arch
.gprs
[rt
];
1138 kvm_debug("[%#lx] OP_SDL: eaddr: %#lx, gpr: %#lx, data: %llx\n",
1139 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1140 vcpu
->arch
.gprs
[rt
], *(u64
*)data
);
1144 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1145 vcpu
->arch
.host_cp0_badvaddr
) & (~0x7);
1148 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x7;
1151 *(u64
*)data
= vcpu
->arch
.gprs
[rt
];
1154 *(u64
*)data
= ((*(u64
*)data
) & 0xff) |
1155 (vcpu
->arch
.gprs
[rt
] << 8);
1158 *(u64
*)data
= ((*(u64
*)data
) & 0xffff) |
1159 (vcpu
->arch
.gprs
[rt
] << 16);
1162 *(u64
*)data
= ((*(u64
*)data
) & 0xffffff) |
1163 (vcpu
->arch
.gprs
[rt
] << 24);
1166 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffff) |
1167 (vcpu
->arch
.gprs
[rt
] << 32);
1170 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffff) |
1171 (vcpu
->arch
.gprs
[rt
] << 40);
1174 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffffff) |
1175 (vcpu
->arch
.gprs
[rt
] << 48);
1178 *(u64
*)data
= ((*(u64
*)data
) & 0xffffffffffffff) |
1179 (vcpu
->arch
.gprs
[rt
] << 56);
1185 kvm_debug("[%#lx] OP_SDR: eaddr: %#lx, gpr: %#lx, data: %llx\n",
1186 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1187 vcpu
->arch
.gprs
[rt
], *(u64
*)data
);
1191 #ifdef CONFIG_CPU_LOONGSON64
1193 rt
= inst
.loongson3_lsdc2_format
.rt
;
1194 switch (inst
.loongson3_lsdc2_format
.opcode1
) {
1196 * Loongson-3 overridden sdc2 instructions.
1197 * opcode1 instruction
1198 * 0x0 gssbx: store 1 bytes from GPR
1199 * 0x1 gsshx: store 2 bytes from GPR
1200 * 0x2 gsswx: store 4 bytes from GPR
1201 * 0x3 gssdx: store 8 bytes from GPR
1205 *(u8
*)data
= vcpu
->arch
.gprs
[rt
];
1207 kvm_debug("[%#lx] OP_GSSBX: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1208 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1209 vcpu
->arch
.gprs
[rt
], *(u8
*)data
);
1213 *(u16
*)data
= vcpu
->arch
.gprs
[rt
];
1215 kvm_debug("[%#lx] OP_GSSSHX: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1216 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1217 vcpu
->arch
.gprs
[rt
], *(u16
*)data
);
1221 *(u32
*)data
= vcpu
->arch
.gprs
[rt
];
1223 kvm_debug("[%#lx] OP_GSSWX: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1224 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1225 vcpu
->arch
.gprs
[rt
], *(u32
*)data
);
1229 *(u64
*)data
= vcpu
->arch
.gprs
[rt
];
1231 kvm_debug("[%#lx] OP_GSSDX: eaddr: %#lx, gpr: %#lx, data: %#llx\n",
1232 vcpu
->arch
.pc
, vcpu
->arch
.host_cp0_badvaddr
,
1233 vcpu
->arch
.gprs
[rt
], *(u64
*)data
);
1236 kvm_err("Godson Extended GS-Store not yet supported (inst=0x%08x)\n",
1243 kvm_err("Store not yet supported (inst=0x%08x)\n",
1248 vcpu
->mmio_needed
= 1;
1249 run
->mmio
.is_write
= 1;
1250 vcpu
->mmio_is_write
= 1;
1252 r
= kvm_io_bus_write(vcpu
, KVM_MMIO_BUS
,
1253 run
->mmio
.phys_addr
, run
->mmio
.len
, data
);
1256 vcpu
->mmio_needed
= 0;
1257 return EMULATE_DONE
;
1260 return EMULATE_DO_MMIO
;
1263 /* Rollback PC if emulation was unsuccessful */
1264 vcpu
->arch
.pc
= curr_pc
;
1265 return EMULATE_FAIL
;
1268 enum emulation_result
kvm_mips_emulate_load(union mips_instruction inst
,
1269 u32 cause
, struct kvm_vcpu
*vcpu
)
1271 struct kvm_run
*run
= vcpu
->run
;
1273 enum emulation_result er
;
1274 unsigned long curr_pc
;
1278 rt
= inst
.i_format
.rt
;
1279 op
= inst
.i_format
.opcode
;
1282 * Find the resume PC now while we have safe and easy access to the
1283 * prior branch instruction, and save it for
1284 * kvm_mips_complete_mmio_load() to restore later.
1286 curr_pc
= vcpu
->arch
.pc
;
1287 er
= update_pc(vcpu
, cause
);
1288 if (er
== EMULATE_FAIL
)
1290 vcpu
->arch
.io_pc
= vcpu
->arch
.pc
;
1291 vcpu
->arch
.pc
= curr_pc
;
1293 vcpu
->arch
.io_gpr
= rt
;
1295 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1296 vcpu
->arch
.host_cp0_badvaddr
);
1297 if (run
->mmio
.phys_addr
== KVM_INVALID_ADDR
)
1298 return EMULATE_FAIL
;
1300 vcpu
->mmio_needed
= 2; /* signed */
1302 #if defined(CONFIG_64BIT)
1308 vcpu
->mmio_needed
= 1; /* unsigned */
1316 vcpu
->mmio_needed
= 1; /* unsigned */
1323 vcpu
->mmio_needed
= 1; /* unsigned */
1330 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1331 vcpu
->arch
.host_cp0_badvaddr
) & (~0x3);
1334 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x3;
1337 vcpu
->mmio_needed
= 3; /* 1 byte */
1340 vcpu
->mmio_needed
= 4; /* 2 bytes */
1343 vcpu
->mmio_needed
= 5; /* 3 bytes */
1346 vcpu
->mmio_needed
= 6; /* 4 bytes */
1354 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1355 vcpu
->arch
.host_cp0_badvaddr
) & (~0x3);
1358 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x3;
1361 vcpu
->mmio_needed
= 7; /* 4 bytes */
1364 vcpu
->mmio_needed
= 8; /* 3 bytes */
1367 vcpu
->mmio_needed
= 9; /* 2 bytes */
1370 vcpu
->mmio_needed
= 10; /* 1 byte */
1377 #if defined(CONFIG_64BIT)
1379 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1380 vcpu
->arch
.host_cp0_badvaddr
) & (~0x7);
1383 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x7;
1386 vcpu
->mmio_needed
= 11; /* 1 byte */
1389 vcpu
->mmio_needed
= 12; /* 2 bytes */
1392 vcpu
->mmio_needed
= 13; /* 3 bytes */
1395 vcpu
->mmio_needed
= 14; /* 4 bytes */
1398 vcpu
->mmio_needed
= 15; /* 5 bytes */
1401 vcpu
->mmio_needed
= 16; /* 6 bytes */
1404 vcpu
->mmio_needed
= 17; /* 7 bytes */
1407 vcpu
->mmio_needed
= 18; /* 8 bytes */
1415 run
->mmio
.phys_addr
= kvm_mips_callbacks
->gva_to_gpa(
1416 vcpu
->arch
.host_cp0_badvaddr
) & (~0x7);
1419 imme
= vcpu
->arch
.host_cp0_badvaddr
& 0x7;
1422 vcpu
->mmio_needed
= 19; /* 8 bytes */
1425 vcpu
->mmio_needed
= 20; /* 7 bytes */
1428 vcpu
->mmio_needed
= 21; /* 6 bytes */
1431 vcpu
->mmio_needed
= 22; /* 5 bytes */
1434 vcpu
->mmio_needed
= 23; /* 4 bytes */
1437 vcpu
->mmio_needed
= 24; /* 3 bytes */
1440 vcpu
->mmio_needed
= 25; /* 2 bytes */
1443 vcpu
->mmio_needed
= 26; /* 1 byte */
1451 #ifdef CONFIG_CPU_LOONGSON64
1453 rt
= inst
.loongson3_lsdc2_format
.rt
;
1454 switch (inst
.loongson3_lsdc2_format
.opcode1
) {
1456 * Loongson-3 overridden ldc2 instructions.
1457 * opcode1 instruction
1458 * 0x0 gslbx: store 1 bytes from GPR
1459 * 0x1 gslhx: store 2 bytes from GPR
1460 * 0x2 gslwx: store 4 bytes from GPR
1461 * 0x3 gsldx: store 8 bytes from GPR
1465 vcpu
->mmio_needed
= 27; /* signed */
1469 vcpu
->mmio_needed
= 28; /* signed */
1473 vcpu
->mmio_needed
= 29; /* signed */
1477 vcpu
->mmio_needed
= 30; /* signed */
1480 kvm_err("Godson Extended GS-Load for float not yet supported (inst=0x%08x)\n",
1488 kvm_err("Load not yet supported (inst=0x%08x)\n",
1490 vcpu
->mmio_needed
= 0;
1491 return EMULATE_FAIL
;
1494 run
->mmio
.is_write
= 0;
1495 vcpu
->mmio_is_write
= 0;
1497 r
= kvm_io_bus_read(vcpu
, KVM_MMIO_BUS
,
1498 run
->mmio
.phys_addr
, run
->mmio
.len
, run
->mmio
.data
);
1501 kvm_mips_complete_mmio_load(vcpu
);
1502 vcpu
->mmio_needed
= 0;
1503 return EMULATE_DONE
;
1506 return EMULATE_DO_MMIO
;
1509 enum emulation_result
kvm_mips_complete_mmio_load(struct kvm_vcpu
*vcpu
)
1511 struct kvm_run
*run
= vcpu
->run
;
1512 unsigned long *gpr
= &vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
];
1513 enum emulation_result er
= EMULATE_DONE
;
1515 if (run
->mmio
.len
> sizeof(*gpr
)) {
1516 kvm_err("Bad MMIO length: %d", run
->mmio
.len
);
1521 /* Restore saved resume PC */
1522 vcpu
->arch
.pc
= vcpu
->arch
.io_pc
;
1524 switch (run
->mmio
.len
) {
1526 switch (vcpu
->mmio_needed
) {
1528 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffffffff) |
1529 (((*(s64
*)run
->mmio
.data
) & 0xff) << 56);
1532 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffffff) |
1533 (((*(s64
*)run
->mmio
.data
) & 0xffff) << 48);
1536 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffff) |
1537 (((*(s64
*)run
->mmio
.data
) & 0xffffff) << 40);
1540 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffff) |
1541 (((*(s64
*)run
->mmio
.data
) & 0xffffffff) << 32);
1544 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffff) |
1545 (((*(s64
*)run
->mmio
.data
) & 0xffffffffff) << 24);
1548 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffff) |
1549 (((*(s64
*)run
->mmio
.data
) & 0xffffffffffff) << 16);
1552 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xff) |
1553 (((*(s64
*)run
->mmio
.data
) & 0xffffffffffffff) << 8);
1557 *gpr
= *(s64
*)run
->mmio
.data
;
1560 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xff00000000000000) |
1561 ((((*(s64
*)run
->mmio
.data
)) >> 8) & 0xffffffffffffff);
1564 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffff000000000000) |
1565 ((((*(s64
*)run
->mmio
.data
)) >> 16) & 0xffffffffffff);
1568 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffff0000000000) |
1569 ((((*(s64
*)run
->mmio
.data
)) >> 24) & 0xffffffffff);
1572 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffff00000000) |
1573 ((((*(s64
*)run
->mmio
.data
)) >> 32) & 0xffffffff);
1576 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffff000000) |
1577 ((((*(s64
*)run
->mmio
.data
)) >> 40) & 0xffffff);
1580 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffffff0000) |
1581 ((((*(s64
*)run
->mmio
.data
)) >> 48) & 0xffff);
1584 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffffffffffff00) |
1585 ((((*(s64
*)run
->mmio
.data
)) >> 56) & 0xff);
1588 *gpr
= *(s64
*)run
->mmio
.data
;
1593 switch (vcpu
->mmio_needed
) {
1595 *gpr
= *(u32
*)run
->mmio
.data
;
1598 *gpr
= *(s32
*)run
->mmio
.data
;
1601 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffff) |
1602 (((*(s32
*)run
->mmio
.data
) & 0xff) << 24);
1605 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffff) |
1606 (((*(s32
*)run
->mmio
.data
) & 0xffff) << 16);
1609 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xff) |
1610 (((*(s32
*)run
->mmio
.data
) & 0xffffff) << 8);
1614 *gpr
= *(s32
*)run
->mmio
.data
;
1617 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xff000000) |
1618 ((((*(s32
*)run
->mmio
.data
)) >> 8) & 0xffffff);
1621 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffff0000) |
1622 ((((*(s32
*)run
->mmio
.data
)) >> 16) & 0xffff);
1625 *gpr
= (vcpu
->arch
.gprs
[vcpu
->arch
.io_gpr
] & 0xffffff00) |
1626 ((((*(s32
*)run
->mmio
.data
)) >> 24) & 0xff);
1629 *gpr
= *(s32
*)run
->mmio
.data
;
1634 if (vcpu
->mmio_needed
== 1)
1635 *gpr
= *(u16
*)run
->mmio
.data
;
1637 *gpr
= *(s16
*)run
->mmio
.data
;
1641 if (vcpu
->mmio_needed
== 1)
1642 *gpr
= *(u8
*)run
->mmio
.data
;
1644 *gpr
= *(s8
*)run
->mmio
.data
;