Skip to content

Commit

Permalink
KVM: remove in_range from io devices
Browse files Browse the repository at this point in the history
This changes bus accesses to use high-level kvm_io_bus_read/kvm_io_bus_write
functions. in_range now becomes unused so it is removed from device ops in
favor of read/write callbacks performing range checks internally.

This allows aliasing (mostly for in-kernel virtio), as well as better error
handling by making it possible to pass errors up to userspace.

Signed-off-by: Michael S. Tsirkin <[email protected]>
Signed-off-by: Avi Kivity <[email protected]>
  • Loading branch information
mstsirkin authored and avikivity committed Sep 10, 2009
1 parent 6c47469 commit bda9020
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 208 deletions.
28 changes: 8 additions & 20 deletions arch/ia64/kvm/kvm-ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,6 @@ int kvm_dev_ioctl_check_extension(long ext)

}

static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
gpa_t addr, int len, int is_write)
{
struct kvm_io_device *dev;

dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);

return dev;
}

static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
{
kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Expand All @@ -231,6 +221,7 @@ static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
{
struct kvm_mmio_req *p;
struct kvm_io_device *mmio_dev;
int r;

p = kvm_get_vcpu_ioreq(vcpu);

Expand All @@ -247,16 +238,13 @@ static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
kvm_run->exit_reason = KVM_EXIT_MMIO;
return 0;
mmio:
mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
if (mmio_dev) {
if (!p->dir)
kvm_iodevice_write(mmio_dev, p->addr, p->size,
&p->data);
else
kvm_iodevice_read(mmio_dev, p->addr, p->size,
&p->data);

} else
if (p->dir)
r = kvm_io_bus_read(&vcpu->kvm->mmio_bus, p->addr,
p->size, &p->data);
else
r = kvm_io_bus_write(&vcpu->kvm->mmio_bus, p->addr,
p->size, &p->data);
if (r)
printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
p->state = STATE_IORESP_READY;

Expand Down
49 changes: 26 additions & 23 deletions arch/x86/kvm/i8254.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,23 @@ static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
return container_of(dev, struct kvm_pit, speaker_dev);
}

static void pit_ioport_write(struct kvm_io_device *this,
gpa_t addr, int len, const void *data)
static inline int pit_in_range(gpa_t addr)
{
return ((addr >= KVM_PIT_BASE_ADDRESS) &&
(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
}

static int pit_ioport_write(struct kvm_io_device *this,
gpa_t addr, int len, const void *data)
{
struct kvm_pit *pit = dev_to_pit(this);
struct kvm_kpit_state *pit_state = &pit->pit_state;
struct kvm *kvm = pit->kvm;
int channel, access;
struct kvm_kpit_channel_state *s;
u32 val = *(u32 *) data;
if (!pit_in_range(addr))
return -EOPNOTSUPP;

val &= 0xff;
addr &= KVM_PIT_CHANNEL_MASK;
Expand Down Expand Up @@ -429,16 +437,19 @@ static void pit_ioport_write(struct kvm_io_device *this,
}

mutex_unlock(&pit_state->lock);
return 0;
}

static void pit_ioport_read(struct kvm_io_device *this,
gpa_t addr, int len, void *data)
static int pit_ioport_read(struct kvm_io_device *this,
gpa_t addr, int len, void *data)
{
struct kvm_pit *pit = dev_to_pit(this);
struct kvm_kpit_state *pit_state = &pit->pit_state;
struct kvm *kvm = pit->kvm;
int ret, count;
struct kvm_kpit_channel_state *s;
if (!pit_in_range(addr))
return -EOPNOTSUPP;

addr &= KVM_PIT_CHANNEL_MASK;
s = &pit_state->channels[addr];
Expand Down Expand Up @@ -493,37 +504,36 @@ static void pit_ioport_read(struct kvm_io_device *this,
memcpy(data, (char *)&ret, len);

mutex_unlock(&pit_state->lock);
return 0;
}

static int pit_in_range(struct kvm_io_device *this, gpa_t addr,
int len, int is_write)
{
return ((addr >= KVM_PIT_BASE_ADDRESS) &&
(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
}

static void speaker_ioport_write(struct kvm_io_device *this,
gpa_t addr, int len, const void *data)
static int speaker_ioport_write(struct kvm_io_device *this,
gpa_t addr, int len, const void *data)
{
struct kvm_pit *pit = speaker_to_pit(this);
struct kvm_kpit_state *pit_state = &pit->pit_state;
struct kvm *kvm = pit->kvm;
u32 val = *(u32 *) data;
if (addr != KVM_SPEAKER_BASE_ADDRESS)
return -EOPNOTSUPP;

mutex_lock(&pit_state->lock);
pit_state->speaker_data_on = (val >> 1) & 1;
pit_set_gate(kvm, 2, val & 1);
mutex_unlock(&pit_state->lock);
return 0;
}

static void speaker_ioport_read(struct kvm_io_device *this,
gpa_t addr, int len, void *data)
static int speaker_ioport_read(struct kvm_io_device *this,
gpa_t addr, int len, void *data)
{
struct kvm_pit *pit = speaker_to_pit(this);
struct kvm_kpit_state *pit_state = &pit->pit_state;
struct kvm *kvm = pit->kvm;
unsigned int refresh_clock;
int ret;
if (addr != KVM_SPEAKER_BASE_ADDRESS)
return -EOPNOTSUPP;

/* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
Expand All @@ -535,12 +545,7 @@ static void speaker_ioport_read(struct kvm_io_device *this,
len = sizeof(ret);
memcpy(data, (char *)&ret, len);
mutex_unlock(&pit_state->lock);
}

static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
int len, int is_write)
{
return (addr == KVM_SPEAKER_BASE_ADDRESS);
return 0;
}

void kvm_pit_reset(struct kvm_pit *pit)
Expand Down Expand Up @@ -574,13 +579,11 @@ static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
static const struct kvm_io_device_ops pit_dev_ops = {
.read = pit_ioport_read,
.write = pit_ioport_write,
.in_range = pit_in_range,
};

static const struct kvm_io_device_ops speaker_dev_ops = {
.read = speaker_ioport_read,
.write = speaker_ioport_write,
.in_range = speaker_in_range,
};

/* Caller must have writers lock on slots_lock */
Expand Down
20 changes: 12 additions & 8 deletions arch/x86/kvm/i8259.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ static u32 elcr_ioport_read(void *opaque, u32 addr1)
return s->elcr;
}

static int picdev_in_range(struct kvm_io_device *this, gpa_t addr,
int len, int is_write)
static int picdev_in_range(gpa_t addr)
{
switch (addr) {
case 0x20:
Expand All @@ -451,16 +450,18 @@ static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
return container_of(dev, struct kvm_pic, dev);
}

static void picdev_write(struct kvm_io_device *this,
static int picdev_write(struct kvm_io_device *this,
gpa_t addr, int len, const void *val)
{
struct kvm_pic *s = to_pic(this);
unsigned char data = *(unsigned char *)val;
if (!picdev_in_range(addr))
return -EOPNOTSUPP;

if (len != 1) {
if (printk_ratelimit())
printk(KERN_ERR "PIC: non byte write\n");
return;
return 0;
}
pic_lock(s);
switch (addr) {
Expand All @@ -476,18 +477,21 @@ static void picdev_write(struct kvm_io_device *this,
break;
}
pic_unlock(s);
return 0;
}

static void picdev_read(struct kvm_io_device *this,
gpa_t addr, int len, void *val)
static int picdev_read(struct kvm_io_device *this,
gpa_t addr, int len, void *val)
{
struct kvm_pic *s = to_pic(this);
unsigned char data = 0;
if (!picdev_in_range(addr))
return -EOPNOTSUPP;

if (len != 1) {
if (printk_ratelimit())
printk(KERN_ERR "PIC: non byte read\n");
return;
return 0;
}
pic_lock(s);
switch (addr) {
Expand All @@ -504,6 +508,7 @@ static void picdev_read(struct kvm_io_device *this,
}
*(unsigned char *)val = data;
pic_unlock(s);
return 0;
}

/*
Expand All @@ -526,7 +531,6 @@ static void pic_irq_request(void *opaque, int level)
static const struct kvm_io_device_ops picdev_ops = {
.read = picdev_read,
.write = picdev_write,
.in_range = picdev_in_range,
};

struct kvm_pic *kvm_create_pic(struct kvm *kvm)
Expand Down
44 changes: 20 additions & 24 deletions arch/x86/kvm/lapic.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,27 @@ static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
return container_of(dev, struct kvm_lapic, dev);
}

static void apic_mmio_read(struct kvm_io_device *this,
gpa_t address, int len, void *data)
static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
{
return apic_hw_enabled(apic) &&
addr >= apic->base_address &&
addr < apic->base_address + LAPIC_MMIO_LENGTH;
}

static int apic_mmio_read(struct kvm_io_device *this,
gpa_t address, int len, void *data)
{
struct kvm_lapic *apic = to_lapic(this);
unsigned int offset = address - apic->base_address;
unsigned char alignment = offset & 0xf;
u32 result;
if (!apic_mmio_in_range(apic, address))
return -EOPNOTSUPP;

if ((alignment + len) > 4) {
printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
(unsigned long)address, len);
return;
return 0;
}
result = __apic_read(apic, offset & ~0xf);

Expand All @@ -574,6 +583,7 @@ static void apic_mmio_read(struct kvm_io_device *this,
"should be 1,2, or 4 instead\n", len);
break;
}
return 0;
}

static void update_divide_count(struct kvm_lapic *apic)
Expand Down Expand Up @@ -629,13 +639,15 @@ static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
}

static void apic_mmio_write(struct kvm_io_device *this,
gpa_t address, int len, const void *data)
static int apic_mmio_write(struct kvm_io_device *this,
gpa_t address, int len, const void *data)
{
struct kvm_lapic *apic = to_lapic(this);
unsigned int offset = address - apic->base_address;
unsigned char alignment = offset & 0xf;
u32 val;
if (!apic_mmio_in_range(apic, address))
return -EOPNOTSUPP;

/*
* APIC register must be aligned on 128-bits boundary.
Expand All @@ -646,7 +658,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
/* Don't shout loud, $infamous_os would cause only noise. */
apic_debug("apic write: bad size=%d %lx\n",
len, (long)address);
return;
return 0;
}

val = *(u32 *) data;
Expand Down Expand Up @@ -729,7 +741,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
hrtimer_cancel(&apic->lapic_timer.timer);
apic_set_reg(apic, APIC_TMICT, val);
start_apic_timer(apic);
return;
return 0;

case APIC_TDCR:
if (val & 4)
Expand All @@ -743,22 +755,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
offset);
break;
}

}

static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr,
int len, int size)
{
struct kvm_lapic *apic = to_lapic(this);
int ret = 0;


if (apic_hw_enabled(apic) &&
(addr >= apic->base_address) &&
(addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
ret = 1;

return ret;
return 0;
}

void kvm_free_lapic(struct kvm_vcpu *vcpu)
Expand Down Expand Up @@ -938,7 +935,6 @@ static struct kvm_timer_ops lapic_timer_ops = {
static const struct kvm_io_device_ops apic_mmio_ops = {
.read = apic_mmio_read,
.write = apic_mmio_write,
.in_range = apic_mmio_range,
};

int kvm_create_lapic(struct kvm_vcpu *vcpu)
Expand Down
Loading

0 comments on commit bda9020

Please sign in to comment.