MIPS: KVM: Add CP0_Count/Compare KVM register access

Implement KVM_{GET,SET}_ONE_REG ioctl based access to the guest CP0
Count and Compare registers. These registers are special in that writing
to them has side effects (adjusting the time until the next timer
interrupt) and reading of Count depends on the time. Therefore add a
couple of callbacks so that different implementations (trap & emulate or
VZ) can implement them differently depending on what the hardware
provides.

The trap & emulate versions mostly duplicate what happens when a T&E
guest reads or writes these registers, so it inherits the same
limitations which can be fixed in later patches.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Gleb Natapov <gleb@kernel.org>
Cc: kvm@vger.kernel.org
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: David Daney <david.daney@cavium.com>
Cc: Sanjay Lal <sanjayl@kymasys.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
James Hogan 2014-05-29 10:16:29 +01:00 committed by Paolo Bonzini
parent 48a3c4e4cd
commit f8be02daca
3 changed files with 56 additions and 0 deletions

View File

@ -523,6 +523,10 @@ struct kvm_mips_callbacks {
uint32_t cause); uint32_t cause);
int (*irq_clear) (struct kvm_vcpu *vcpu, unsigned int priority, int (*irq_clear) (struct kvm_vcpu *vcpu, unsigned int priority,
uint32_t cause); uint32_t cause);
int (*get_one_reg)(struct kvm_vcpu *vcpu,
const struct kvm_one_reg *reg, s64 *v);
int (*set_one_reg)(struct kvm_vcpu *vcpu,
const struct kvm_one_reg *reg, s64 v);
}; };
extern struct kvm_mips_callbacks *kvm_mips_callbacks; extern struct kvm_mips_callbacks *kvm_mips_callbacks;
int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks); int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks);

View File

@ -529,7 +529,9 @@ static u64 kvm_mips_get_one_regs[] = {
KVM_REG_MIPS_CP0_PAGEMASK, KVM_REG_MIPS_CP0_PAGEMASK,
KVM_REG_MIPS_CP0_WIRED, KVM_REG_MIPS_CP0_WIRED,
KVM_REG_MIPS_CP0_BADVADDR, KVM_REG_MIPS_CP0_BADVADDR,
KVM_REG_MIPS_CP0_COUNT,
KVM_REG_MIPS_CP0_ENTRYHI, KVM_REG_MIPS_CP0_ENTRYHI,
KVM_REG_MIPS_CP0_COMPARE,
KVM_REG_MIPS_CP0_STATUS, KVM_REG_MIPS_CP0_STATUS,
KVM_REG_MIPS_CP0_CAUSE, KVM_REG_MIPS_CP0_CAUSE,
KVM_REG_MIPS_CP0_EPC, KVM_REG_MIPS_CP0_EPC,
@ -545,6 +547,7 @@ static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
const struct kvm_one_reg *reg) const struct kvm_one_reg *reg)
{ {
struct mips_coproc *cop0 = vcpu->arch.cop0; struct mips_coproc *cop0 = vcpu->arch.cop0;
int ret;
s64 v; s64 v;
switch (reg->id) { switch (reg->id) {
@ -579,6 +582,9 @@ static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
case KVM_REG_MIPS_CP0_ENTRYHI: case KVM_REG_MIPS_CP0_ENTRYHI:
v = (long)kvm_read_c0_guest_entryhi(cop0); v = (long)kvm_read_c0_guest_entryhi(cop0);
break; break;
case KVM_REG_MIPS_CP0_COMPARE:
v = (long)kvm_read_c0_guest_compare(cop0);
break;
case KVM_REG_MIPS_CP0_STATUS: case KVM_REG_MIPS_CP0_STATUS:
v = (long)kvm_read_c0_guest_status(cop0); v = (long)kvm_read_c0_guest_status(cop0);
break; break;
@ -606,6 +612,12 @@ static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
case KVM_REG_MIPS_CP0_CONFIG7: case KVM_REG_MIPS_CP0_CONFIG7:
v = (long)kvm_read_c0_guest_config7(cop0); v = (long)kvm_read_c0_guest_config7(cop0);
break; break;
/* registers to be handled specially */
case KVM_REG_MIPS_CP0_COUNT:
ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
if (ret)
return ret;
break;
default: default:
return -EINVAL; return -EINVAL;
} }
@ -690,6 +702,10 @@ static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
case KVM_REG_MIPS_CP0_ERROREPC: case KVM_REG_MIPS_CP0_ERROREPC:
kvm_write_c0_guest_errorepc(cop0, v); kvm_write_c0_guest_errorepc(cop0, v);
break; break;
/* registers to be handled specially */
case KVM_REG_MIPS_CP0_COUNT:
case KVM_REG_MIPS_CP0_COMPARE:
return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
default: default:
return -EINVAL; return -EINVAL;
} }

View File

@ -401,6 +401,40 @@ static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
return 0; return 0;
} }
static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
const struct kvm_one_reg *reg,
s64 *v)
{
switch (reg->id) {
case KVM_REG_MIPS_CP0_COUNT:
/* XXXKYMA: Run the Guest count register @ 1/4 the rate of the host */
*v = (read_c0_count() >> 2);
break;
default:
return -EINVAL;
}
return 0;
}
static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
const struct kvm_one_reg *reg,
s64 v)
{
struct mips_coproc *cop0 = vcpu->arch.cop0;
switch (reg->id) {
case KVM_REG_MIPS_CP0_COUNT:
/* Not supported yet */
break;
case KVM_REG_MIPS_CP0_COMPARE:
kvm_write_c0_guest_compare(cop0, v);
break;
default:
return -EINVAL;
}
return 0;
}
static struct kvm_mips_callbacks kvm_trap_emul_callbacks = { static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
/* exit handlers */ /* exit handlers */
.handle_cop_unusable = kvm_trap_emul_handle_cop_unusable, .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
@ -423,6 +457,8 @@ static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
.dequeue_io_int = kvm_mips_dequeue_io_int_cb, .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
.irq_deliver = kvm_mips_irq_deliver_cb, .irq_deliver = kvm_mips_irq_deliver_cb,
.irq_clear = kvm_mips_irq_clear_cb, .irq_clear = kvm_mips_irq_clear_cb,
.get_one_reg = kvm_trap_emul_get_one_reg,
.set_one_reg = kvm_trap_emul_set_one_reg,
}; };
int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks) int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)