aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86
diff options
context:
space:
mode:
authorMichael S. Tsirkin <mst@redhat.com>2009-06-29 15:24:32 -0400
committerAvi Kivity <avi@redhat.com>2009-09-10 01:33:05 -0400
commitbda9020e2463ec94db9f97e8615f3bae22069838 (patch)
tree48125316d4c0f419a35aefdfbf665d30ad0c55ca /arch/x86
parent6c474694530f377507f9aca438c17206e051e6e7 (diff)
KVM: remove in_range from io devices
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 <mst@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'arch/x86')
-rw-r--r--arch/x86/kvm/i8254.c49
-rw-r--r--arch/x86/kvm/i8259.c20
-rw-r--r--arch/x86/kvm/lapic.c44
-rw-r--r--arch/x86/kvm/x86.c110
4 files changed, 90 insertions, 133 deletions
diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
index 4082cdd468ed..8c3ac30ef9bd 100644
--- a/arch/x86/kvm/i8254.c
+++ b/arch/x86/kvm/i8254.c
@@ -358,8 +358,14 @@ static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
358 return container_of(dev, struct kvm_pit, speaker_dev); 358 return container_of(dev, struct kvm_pit, speaker_dev);
359} 359}
360 360
361static void pit_ioport_write(struct kvm_io_device *this, 361static inline int pit_in_range(gpa_t addr)
362 gpa_t addr, int len, const void *data) 362{
363 return ((addr >= KVM_PIT_BASE_ADDRESS) &&
364 (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
365}
366
367static int pit_ioport_write(struct kvm_io_device *this,
368 gpa_t addr, int len, const void *data)
363{ 369{
364 struct kvm_pit *pit = dev_to_pit(this); 370 struct kvm_pit *pit = dev_to_pit(this);
365 struct kvm_kpit_state *pit_state = &pit->pit_state; 371 struct kvm_kpit_state *pit_state = &pit->pit_state;
@@ -367,6 +373,8 @@ static void pit_ioport_write(struct kvm_io_device *this,
367 int channel, access; 373 int channel, access;
368 struct kvm_kpit_channel_state *s; 374 struct kvm_kpit_channel_state *s;
369 u32 val = *(u32 *) data; 375 u32 val = *(u32 *) data;
376 if (!pit_in_range(addr))
377 return -EOPNOTSUPP;
370 378
371 val &= 0xff; 379 val &= 0xff;
372 addr &= KVM_PIT_CHANNEL_MASK; 380 addr &= KVM_PIT_CHANNEL_MASK;
@@ -429,16 +437,19 @@ static void pit_ioport_write(struct kvm_io_device *this,
429 } 437 }
430 438
431 mutex_unlock(&pit_state->lock); 439 mutex_unlock(&pit_state->lock);
440 return 0;
432} 441}
433 442
434static void pit_ioport_read(struct kvm_io_device *this, 443static int pit_ioport_read(struct kvm_io_device *this,
435 gpa_t addr, int len, void *data) 444 gpa_t addr, int len, void *data)
436{ 445{
437 struct kvm_pit *pit = dev_to_pit(this); 446 struct kvm_pit *pit = dev_to_pit(this);
438 struct kvm_kpit_state *pit_state = &pit->pit_state; 447 struct kvm_kpit_state *pit_state = &pit->pit_state;
439 struct kvm *kvm = pit->kvm; 448 struct kvm *kvm = pit->kvm;
440 int ret, count; 449 int ret, count;
441 struct kvm_kpit_channel_state *s; 450 struct kvm_kpit_channel_state *s;
451 if (!pit_in_range(addr))
452 return -EOPNOTSUPP;
442 453
443 addr &= KVM_PIT_CHANNEL_MASK; 454 addr &= KVM_PIT_CHANNEL_MASK;
444 s = &pit_state->channels[addr]; 455 s = &pit_state->channels[addr];
@@ -493,37 +504,36 @@ static void pit_ioport_read(struct kvm_io_device *this,
493 memcpy(data, (char *)&ret, len); 504 memcpy(data, (char *)&ret, len);
494 505
495 mutex_unlock(&pit_state->lock); 506 mutex_unlock(&pit_state->lock);
507 return 0;
496} 508}
497 509
498static int pit_in_range(struct kvm_io_device *this, gpa_t addr, 510static int speaker_ioport_write(struct kvm_io_device *this,
499 int len, int is_write) 511 gpa_t addr, int len, const void *data)
500{
501 return ((addr >= KVM_PIT_BASE_ADDRESS) &&
502 (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
503}
504
505static void speaker_ioport_write(struct kvm_io_device *this,
506 gpa_t addr, int len, const void *data)
507{ 512{
508 struct kvm_pit *pit = speaker_to_pit(this); 513 struct kvm_pit *pit = speaker_to_pit(this);
509 struct kvm_kpit_state *pit_state = &pit->pit_state; 514 struct kvm_kpit_state *pit_state = &pit->pit_state;
510 struct kvm *kvm = pit->kvm; 515 struct kvm *kvm = pit->kvm;
511 u32 val = *(u32 *) data; 516 u32 val = *(u32 *) data;
517 if (addr != KVM_SPEAKER_BASE_ADDRESS)
518 return -EOPNOTSUPP;
512 519
513 mutex_lock(&pit_state->lock); 520 mutex_lock(&pit_state->lock);
514 pit_state->speaker_data_on = (val >> 1) & 1; 521 pit_state->speaker_data_on = (val >> 1) & 1;
515 pit_set_gate(kvm, 2, val & 1); 522 pit_set_gate(kvm, 2, val & 1);
516 mutex_unlock(&pit_state->lock); 523 mutex_unlock(&pit_state->lock);
524 return 0;
517} 525}
518 526
519static void speaker_ioport_read(struct kvm_io_device *this, 527static int speaker_ioport_read(struct kvm_io_device *this,
520 gpa_t addr, int len, void *data) 528 gpa_t addr, int len, void *data)
521{ 529{
522 struct kvm_pit *pit = speaker_to_pit(this); 530 struct kvm_pit *pit = speaker_to_pit(this);
523 struct kvm_kpit_state *pit_state = &pit->pit_state; 531 struct kvm_kpit_state *pit_state = &pit->pit_state;
524 struct kvm *kvm = pit->kvm; 532 struct kvm *kvm = pit->kvm;
525 unsigned int refresh_clock; 533 unsigned int refresh_clock;
526 int ret; 534 int ret;
535 if (addr != KVM_SPEAKER_BASE_ADDRESS)
536 return -EOPNOTSUPP;
527 537
528 /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */ 538 /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
529 refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1; 539 refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
@@ -535,12 +545,7 @@ static void speaker_ioport_read(struct kvm_io_device *this,
535 len = sizeof(ret); 545 len = sizeof(ret);
536 memcpy(data, (char *)&ret, len); 546 memcpy(data, (char *)&ret, len);
537 mutex_unlock(&pit_state->lock); 547 mutex_unlock(&pit_state->lock);
538} 548 return 0;
539
540static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
541 int len, int is_write)
542{
543 return (addr == KVM_SPEAKER_BASE_ADDRESS);
544} 549}
545 550
546void kvm_pit_reset(struct kvm_pit *pit) 551void kvm_pit_reset(struct kvm_pit *pit)
@@ -574,13 +579,11 @@ static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
574static const struct kvm_io_device_ops pit_dev_ops = { 579static const struct kvm_io_device_ops pit_dev_ops = {
575 .read = pit_ioport_read, 580 .read = pit_ioport_read,
576 .write = pit_ioport_write, 581 .write = pit_ioport_write,
577 .in_range = pit_in_range,
578}; 582};
579 583
580static const struct kvm_io_device_ops speaker_dev_ops = { 584static const struct kvm_io_device_ops speaker_dev_ops = {
581 .read = speaker_ioport_read, 585 .read = speaker_ioport_read,
582 .write = speaker_ioport_write, 586 .write = speaker_ioport_write,
583 .in_range = speaker_in_range,
584}; 587};
585 588
586/* Caller must have writers lock on slots_lock */ 589/* Caller must have writers lock on slots_lock */
diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c
index 1851aec8a7da..1d1bb75dc7bc 100644
--- a/arch/x86/kvm/i8259.c
+++ b/arch/x86/kvm/i8259.c
@@ -430,8 +430,7 @@ static u32 elcr_ioport_read(void *opaque, u32 addr1)
430 return s->elcr; 430 return s->elcr;
431} 431}
432 432
433static int picdev_in_range(struct kvm_io_device *this, gpa_t addr, 433static int picdev_in_range(gpa_t addr)
434 int len, int is_write)
435{ 434{
436 switch (addr) { 435 switch (addr) {
437 case 0x20: 436 case 0x20:
@@ -451,16 +450,18 @@ static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
451 return container_of(dev, struct kvm_pic, dev); 450 return container_of(dev, struct kvm_pic, dev);
452} 451}
453 452
454static void picdev_write(struct kvm_io_device *this, 453static int picdev_write(struct kvm_io_device *this,
455 gpa_t addr, int len, const void *val) 454 gpa_t addr, int len, const void *val)
456{ 455{
457 struct kvm_pic *s = to_pic(this); 456 struct kvm_pic *s = to_pic(this);
458 unsigned char data = *(unsigned char *)val; 457 unsigned char data = *(unsigned char *)val;
458 if (!picdev_in_range(addr))
459 return -EOPNOTSUPP;
459 460
460 if (len != 1) { 461 if (len != 1) {
461 if (printk_ratelimit()) 462 if (printk_ratelimit())
462 printk(KERN_ERR "PIC: non byte write\n"); 463 printk(KERN_ERR "PIC: non byte write\n");
463 return; 464 return 0;
464 } 465 }
465 pic_lock(s); 466 pic_lock(s);
466 switch (addr) { 467 switch (addr) {
@@ -476,18 +477,21 @@ static void picdev_write(struct kvm_io_device *this,
476 break; 477 break;
477 } 478 }
478 pic_unlock(s); 479 pic_unlock(s);
480 return 0;
479} 481}
480 482
481static void picdev_read(struct kvm_io_device *this, 483static int picdev_read(struct kvm_io_device *this,
482 gpa_t addr, int len, void *val) 484 gpa_t addr, int len, void *val)
483{ 485{
484 struct kvm_pic *s = to_pic(this); 486 struct kvm_pic *s = to_pic(this);
485 unsigned char data = 0; 487 unsigned char data = 0;
488 if (!picdev_in_range(addr))
489 return -EOPNOTSUPP;
486 490
487 if (len != 1) { 491 if (len != 1) {
488 if (printk_ratelimit()) 492 if (printk_ratelimit())
489 printk(KERN_ERR "PIC: non byte read\n"); 493 printk(KERN_ERR "PIC: non byte read\n");
490 return; 494 return 0;
491 } 495 }
492 pic_lock(s); 496 pic_lock(s);
493 switch (addr) { 497 switch (addr) {
@@ -504,6 +508,7 @@ static void picdev_read(struct kvm_io_device *this,
504 } 508 }
505 *(unsigned char *)val = data; 509 *(unsigned char *)val = data;
506 pic_unlock(s); 510 pic_unlock(s);
511 return 0;
507} 512}
508 513
509/* 514/*
@@ -526,7 +531,6 @@ static void pic_irq_request(void *opaque, int level)
526static const struct kvm_io_device_ops picdev_ops = { 531static const struct kvm_io_device_ops picdev_ops = {
527 .read = picdev_read, 532 .read = picdev_read,
528 .write = picdev_write, 533 .write = picdev_write,
529 .in_range = picdev_in_range,
530}; 534};
531 535
532struct kvm_pic *kvm_create_pic(struct kvm *kvm) 536struct kvm_pic *kvm_create_pic(struct kvm *kvm)
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 2e0286596387..265a765f038f 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -546,18 +546,27 @@ static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
546 return container_of(dev, struct kvm_lapic, dev); 546 return container_of(dev, struct kvm_lapic, dev);
547} 547}
548 548
549static void apic_mmio_read(struct kvm_io_device *this, 549static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
550 gpa_t address, int len, void *data) 550{
551 return apic_hw_enabled(apic) &&
552 addr >= apic->base_address &&
553 addr < apic->base_address + LAPIC_MMIO_LENGTH;
554}
555
556static int apic_mmio_read(struct kvm_io_device *this,
557 gpa_t address, int len, void *data)
551{ 558{
552 struct kvm_lapic *apic = to_lapic(this); 559 struct kvm_lapic *apic = to_lapic(this);
553 unsigned int offset = address - apic->base_address; 560 unsigned int offset = address - apic->base_address;
554 unsigned char alignment = offset & 0xf; 561 unsigned char alignment = offset & 0xf;
555 u32 result; 562 u32 result;
563 if (!apic_mmio_in_range(apic, address))
564 return -EOPNOTSUPP;
556 565
557 if ((alignment + len) > 4) { 566 if ((alignment + len) > 4) {
558 printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d", 567 printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
559 (unsigned long)address, len); 568 (unsigned long)address, len);
560 return; 569 return 0;
561 } 570 }
562 result = __apic_read(apic, offset & ~0xf); 571 result = __apic_read(apic, offset & ~0xf);
563 572
@@ -574,6 +583,7 @@ static void apic_mmio_read(struct kvm_io_device *this,
574 "should be 1,2, or 4 instead\n", len); 583 "should be 1,2, or 4 instead\n", len);
575 break; 584 break;
576 } 585 }
586 return 0;
577} 587}
578 588
579static void update_divide_count(struct kvm_lapic *apic) 589static void update_divide_count(struct kvm_lapic *apic)
@@ -629,13 +639,15 @@ static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
629 apic->vcpu->kvm->arch.vapics_in_nmi_mode--; 639 apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
630} 640}
631 641
632static void apic_mmio_write(struct kvm_io_device *this, 642static int apic_mmio_write(struct kvm_io_device *this,
633 gpa_t address, int len, const void *data) 643 gpa_t address, int len, const void *data)
634{ 644{
635 struct kvm_lapic *apic = to_lapic(this); 645 struct kvm_lapic *apic = to_lapic(this);
636 unsigned int offset = address - apic->base_address; 646 unsigned int offset = address - apic->base_address;
637 unsigned char alignment = offset & 0xf; 647 unsigned char alignment = offset & 0xf;
638 u32 val; 648 u32 val;
649 if (!apic_mmio_in_range(apic, address))
650 return -EOPNOTSUPP;
639 651
640 /* 652 /*
641 * APIC register must be aligned on 128-bits boundary. 653 * APIC register must be aligned on 128-bits boundary.
@@ -646,7 +658,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
646 /* Don't shout loud, $infamous_os would cause only noise. */ 658 /* Don't shout loud, $infamous_os would cause only noise. */
647 apic_debug("apic write: bad size=%d %lx\n", 659 apic_debug("apic write: bad size=%d %lx\n",
648 len, (long)address); 660 len, (long)address);
649 return; 661 return 0;
650 } 662 }
651 663
652 val = *(u32 *) data; 664 val = *(u32 *) data;
@@ -729,7 +741,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
729 hrtimer_cancel(&apic->lapic_timer.timer); 741 hrtimer_cancel(&apic->lapic_timer.timer);
730 apic_set_reg(apic, APIC_TMICT, val); 742 apic_set_reg(apic, APIC_TMICT, val);
731 start_apic_timer(apic); 743 start_apic_timer(apic);
732 return; 744 return 0;
733 745
734 case APIC_TDCR: 746 case APIC_TDCR:
735 if (val & 4) 747 if (val & 4)
@@ -743,22 +755,7 @@ static void apic_mmio_write(struct kvm_io_device *this,
743 offset); 755 offset);
744 break; 756 break;
745 } 757 }
746 758 return 0;
747}
748
749static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr,
750 int len, int size)
751{
752 struct kvm_lapic *apic = to_lapic(this);
753 int ret = 0;
754
755
756 if (apic_hw_enabled(apic) &&
757 (addr >= apic->base_address) &&
758 (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
759 ret = 1;
760
761 return ret;
762} 759}
763 760
764void kvm_free_lapic(struct kvm_vcpu *vcpu) 761void kvm_free_lapic(struct kvm_vcpu *vcpu)
@@ -938,7 +935,6 @@ static struct kvm_timer_ops lapic_timer_ops = {
938static const struct kvm_io_device_ops apic_mmio_ops = { 935static const struct kvm_io_device_ops apic_mmio_ops = {
939 .read = apic_mmio_read, 936 .read = apic_mmio_read,
940 .write = apic_mmio_write, 937 .write = apic_mmio_write,
941 .in_range = apic_mmio_range,
942}; 938};
943 939
944int kvm_create_lapic(struct kvm_vcpu *vcpu) 940int kvm_create_lapic(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7ce6367c1976..96f0ae7d97b6 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2333,35 +2333,23 @@ static void kvm_init_msr_list(void)
2333 num_msrs_to_save = j; 2333 num_msrs_to_save = j;
2334} 2334}
2335 2335
2336/* 2336static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2337 * Only apic need an MMIO device hook, so shortcut now.. 2337 const void *v)
2338 */
2339static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
2340 gpa_t addr, int len,
2341 int is_write)
2342{ 2338{
2343 struct kvm_io_device *dev; 2339 if (vcpu->arch.apic &&
2340 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2341 return 0;
2344 2342
2345 if (vcpu->arch.apic) { 2343 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
2346 dev = &vcpu->arch.apic->dev;
2347 if (kvm_iodevice_in_range(dev, addr, len, is_write))
2348 return dev;
2349 }
2350 return NULL;
2351} 2344}
2352 2345
2353 2346static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
2354static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
2355 gpa_t addr, int len,
2356 int is_write)
2357{ 2347{
2358 struct kvm_io_device *dev; 2348 if (vcpu->arch.apic &&
2349 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2350 return 0;
2359 2351
2360 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write); 2352 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
2361 if (dev == NULL)
2362 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
2363 is_write);
2364 return dev;
2365} 2353}
2366 2354
2367static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, 2355static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
@@ -2430,7 +2418,6 @@ static int emulator_read_emulated(unsigned long addr,
2430 unsigned int bytes, 2418 unsigned int bytes,
2431 struct kvm_vcpu *vcpu) 2419 struct kvm_vcpu *vcpu)
2432{ 2420{
2433 struct kvm_io_device *mmio_dev;
2434 gpa_t gpa; 2421 gpa_t gpa;
2435 2422
2436 if (vcpu->mmio_read_completed) { 2423 if (vcpu->mmio_read_completed) {
@@ -2455,13 +2442,8 @@ mmio:
2455 /* 2442 /*
2456 * Is this MMIO handled locally? 2443 * Is this MMIO handled locally?
2457 */ 2444 */
2458 mutex_lock(&vcpu->kvm->lock); 2445 if (!vcpu_mmio_read(vcpu, gpa, bytes, val))
2459 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
2460 mutex_unlock(&vcpu->kvm->lock);
2461 if (mmio_dev) {
2462 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
2463 return X86EMUL_CONTINUE; 2446 return X86EMUL_CONTINUE;
2464 }
2465 2447
2466 vcpu->mmio_needed = 1; 2448 vcpu->mmio_needed = 1;
2467 vcpu->mmio_phys_addr = gpa; 2449 vcpu->mmio_phys_addr = gpa;
@@ -2488,7 +2470,6 @@ static int emulator_write_emulated_onepage(unsigned long addr,
2488 unsigned int bytes, 2470 unsigned int bytes,
2489 struct kvm_vcpu *vcpu) 2471 struct kvm_vcpu *vcpu)
2490{ 2472{
2491 struct kvm_io_device *mmio_dev;
2492 gpa_t gpa; 2473 gpa_t gpa;
2493 2474
2494 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); 2475 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
@@ -2509,13 +2490,8 @@ mmio:
2509 /* 2490 /*
2510 * Is this MMIO handled locally? 2491 * Is this MMIO handled locally?
2511 */ 2492 */
2512 mutex_lock(&vcpu->kvm->lock); 2493 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
2513 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
2514 mutex_unlock(&vcpu->kvm->lock);
2515 if (mmio_dev) {
2516 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
2517 return X86EMUL_CONTINUE; 2494 return X86EMUL_CONTINUE;
2518 }
2519 2495
2520 vcpu->mmio_needed = 1; 2496 vcpu->mmio_needed = 1;
2521 vcpu->mmio_phys_addr = gpa; 2497 vcpu->mmio_phys_addr = gpa;
@@ -2850,48 +2826,40 @@ int complete_pio(struct kvm_vcpu *vcpu)
2850 return 0; 2826 return 0;
2851} 2827}
2852 2828
2853static void kernel_pio(struct kvm_io_device *pio_dev, 2829static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
2854 struct kvm_vcpu *vcpu,
2855 void *pd)
2856{ 2830{
2857 /* TODO: String I/O for in kernel device */ 2831 /* TODO: String I/O for in kernel device */
2832 int r;
2858 2833
2859 if (vcpu->arch.pio.in) 2834 if (vcpu->arch.pio.in)
2860 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port, 2835 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
2861 vcpu->arch.pio.size, 2836 vcpu->arch.pio.size, pd);
2862 pd);
2863 else 2837 else
2864 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port, 2838 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
2865 vcpu->arch.pio.size, 2839 vcpu->arch.pio.size, pd);
2866 pd); 2840 return r;
2867} 2841}
2868 2842
2869static void pio_string_write(struct kvm_io_device *pio_dev, 2843static int pio_string_write(struct kvm_vcpu *vcpu)
2870 struct kvm_vcpu *vcpu)
2871{ 2844{
2872 struct kvm_pio_request *io = &vcpu->arch.pio; 2845 struct kvm_pio_request *io = &vcpu->arch.pio;
2873 void *pd = vcpu->arch.pio_data; 2846 void *pd = vcpu->arch.pio_data;
2874 int i; 2847 int i, r = 0;
2875 2848
2876 for (i = 0; i < io->cur_count; i++) { 2849 for (i = 0; i < io->cur_count; i++) {
2877 kvm_iodevice_write(pio_dev, io->port, 2850 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
2878 io->size, 2851 io->port, io->size, pd)) {
2879 pd); 2852 r = -EOPNOTSUPP;
2853 break;
2854 }
2880 pd += io->size; 2855 pd += io->size;
2881 } 2856 }
2882} 2857 return r;
2883
2884static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2885 gpa_t addr, int len,
2886 int is_write)
2887{
2888 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
2889} 2858}
2890 2859
2891int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, 2860int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2892 int size, unsigned port) 2861 int size, unsigned port)
2893{ 2862{
2894 struct kvm_io_device *pio_dev;
2895 unsigned long val; 2863 unsigned long val;
2896 2864
2897 vcpu->run->exit_reason = KVM_EXIT_IO; 2865 vcpu->run->exit_reason = KVM_EXIT_IO;
@@ -2911,11 +2879,7 @@ int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2911 val = kvm_register_read(vcpu, VCPU_REGS_RAX); 2879 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2912 memcpy(vcpu->arch.pio_data, &val, 4); 2880 memcpy(vcpu->arch.pio_data, &val, 4);
2913 2881
2914 mutex_lock(&vcpu->kvm->lock); 2882 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
2915 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
2916 mutex_unlock(&vcpu->kvm->lock);
2917 if (pio_dev) {
2918 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2919 complete_pio(vcpu); 2883 complete_pio(vcpu);
2920 return 1; 2884 return 1;
2921 } 2885 }
@@ -2929,7 +2893,6 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2929{ 2893{
2930 unsigned now, in_page; 2894 unsigned now, in_page;
2931 int ret = 0; 2895 int ret = 0;
2932 struct kvm_io_device *pio_dev;
2933 2896
2934 vcpu->run->exit_reason = KVM_EXIT_IO; 2897 vcpu->run->exit_reason = KVM_EXIT_IO;
2935 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; 2898 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
@@ -2973,12 +2936,6 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2973 2936
2974 vcpu->arch.pio.guest_gva = address; 2937 vcpu->arch.pio.guest_gva = address;
2975 2938
2976 mutex_lock(&vcpu->kvm->lock);
2977 pio_dev = vcpu_find_pio_dev(vcpu, port,
2978 vcpu->arch.pio.cur_count,
2979 !vcpu->arch.pio.in);
2980 mutex_unlock(&vcpu->kvm->lock);
2981
2982 if (!vcpu->arch.pio.in) { 2939 if (!vcpu->arch.pio.in) {
2983 /* string PIO write */ 2940 /* string PIO write */
2984 ret = pio_copy_data(vcpu); 2941 ret = pio_copy_data(vcpu);
@@ -2986,16 +2943,13 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2986 kvm_inject_gp(vcpu, 0); 2943 kvm_inject_gp(vcpu, 0);
2987 return 1; 2944 return 1;
2988 } 2945 }
2989 if (ret == 0 && pio_dev) { 2946 if (ret == 0 && !pio_string_write(vcpu)) {
2990 pio_string_write(pio_dev, vcpu);
2991 complete_pio(vcpu); 2947 complete_pio(vcpu);
2992 if (vcpu->arch.pio.count == 0) 2948 if (vcpu->arch.pio.count == 0)
2993 ret = 1; 2949 ret = 1;
2994 } 2950 }
2995 } else if (pio_dev) 2951 }
2996 pr_unimpl(vcpu, "no string pio read support yet, " 2952 /* no string PIO read support yet */
2997 "port %x size %d count %ld\n",
2998 port, size, count);
2999 2953
3000 return ret; 2954 return ret;
3001} 2955}