pc-testdev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * QEMU x86 ISA testdev
  3. *
  4. * Copyright (c) 2012 Avi Kivity, Gerd Hoffmann, Marcelo Tosatti
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /*
  25. * This device is used to test KVM features specific to the x86 port, such
  26. * as emulation, power management, interrupt routing, among others. It's meant
  27. * to be used like:
  28. *
  29. * qemu-system-x86_64 -device pc-testdev -serial stdio \
  30. * -device isa-debug-exit,iobase=0xf4,iosize=0x4 \
  31. * -kernel /home/lmr/Code/virt-test.git/kvm/unittests/msr.flat
  32. *
  33. * Where msr.flat is one of the KVM unittests, present on a separate repo,
  34. * git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
  35. */
  36. #include "config-host.h"
  37. #if defined(CONFIG_POSIX)
  38. #include <sys/mman.h>
  39. #endif
  40. #include "hw.h"
  41. #include "qdev.h"
  42. #include "isa.h"
  43. #define IOMEM_LEN 0x10000
  44. typedef struct PCTestdev {
  45. ISADevice parent_obj;
  46. MemoryRegion ioport;
  47. MemoryRegion flush;
  48. MemoryRegion irq;
  49. MemoryRegion iomem;
  50. uint32_t ioport_data;
  51. char iomem_buf[IOMEM_LEN];
  52. } PCTestdev;
  53. #define TYPE_TESTDEV "pc-testdev"
  54. #define TESTDEV(obj) \
  55. OBJECT_CHECK(PCTestdev, (obj), TYPE_TESTDEV)
  56. static void test_irq_line(void *opaque, hwaddr addr, uint64_t data,
  57. unsigned len)
  58. {
  59. PCTestdev *dev = opaque;
  60. ISADevice *isa = ISA_DEVICE(dev);
  61. qemu_set_irq(isa_get_irq(isa, addr), !!data);
  62. }
  63. static const MemoryRegionOps test_irq_ops = {
  64. .write = test_irq_line,
  65. .valid.min_access_size = 1,
  66. .valid.max_access_size = 1,
  67. .endianness = DEVICE_LITTLE_ENDIAN,
  68. };
  69. static void test_ioport_write(void *opaque, hwaddr addr, uint64_t data,
  70. unsigned len)
  71. {
  72. PCTestdev *dev = opaque;
  73. dev->ioport_data = data;
  74. }
  75. static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len)
  76. {
  77. PCTestdev *dev = opaque;
  78. return dev->ioport_data;
  79. }
  80. static const MemoryRegionOps test_ioport_ops = {
  81. .read = test_ioport_read,
  82. .write = test_ioport_write,
  83. .endianness = DEVICE_LITTLE_ENDIAN,
  84. };
  85. static void test_flush_page(void *opaque, hwaddr addr, uint64_t data,
  86. unsigned len)
  87. {
  88. hwaddr page = 4096;
  89. void *a = cpu_physical_memory_map(data & ~0xffful, &page, 0);
  90. /* We might not be able to get the full page, only mprotect what we actually
  91. have mapped */
  92. #if defined(CONFIG_POSIX)
  93. mprotect(a, page, PROT_NONE);
  94. mprotect(a, page, PROT_READ|PROT_WRITE);
  95. #endif
  96. cpu_physical_memory_unmap(a, page, 0, 0);
  97. }
  98. static const MemoryRegionOps test_flush_ops = {
  99. .write = test_flush_page,
  100. .valid.min_access_size = 4,
  101. .valid.max_access_size = 4,
  102. .endianness = DEVICE_LITTLE_ENDIAN,
  103. };
  104. static uint64_t test_iomem_read(void *opaque, hwaddr addr, unsigned len)
  105. {
  106. PCTestdev *dev = opaque;
  107. uint64_t ret = 0;
  108. memcpy(&ret, &dev->iomem_buf[addr], len);
  109. ret = le64_to_cpu(ret);
  110. return ret;
  111. }
  112. static void test_iomem_write(void *opaque, hwaddr addr, uint64_t val,
  113. unsigned len)
  114. {
  115. PCTestdev *dev = opaque;
  116. val = cpu_to_le64(val);
  117. memcpy(&dev->iomem_buf[addr], &val, len);
  118. dev->iomem_buf[addr] = val;
  119. }
  120. static const MemoryRegionOps test_iomem_ops = {
  121. .read = test_iomem_read,
  122. .write = test_iomem_write,
  123. .endianness = DEVICE_LITTLE_ENDIAN,
  124. };
  125. static int init_test_device(ISADevice *isa)
  126. {
  127. PCTestdev *dev = TESTDEV(isa);
  128. MemoryRegion *mem = isa_address_space(isa);
  129. MemoryRegion *io = isa_address_space_io(isa);
  130. memory_region_init_io(&dev->ioport, &test_ioport_ops, dev,
  131. "pc-testdev-ioport", 4);
  132. memory_region_init_io(&dev->flush, &test_flush_ops, dev,
  133. "pc-testdev-flush-page", 4);
  134. memory_region_init_io(&dev->irq, &test_irq_ops, dev,
  135. "pc-testdev-irq-line", 24);
  136. memory_region_init_io(&dev->iomem, &test_iomem_ops, dev,
  137. "pc-testdev-iomem", IOMEM_LEN);
  138. memory_region_add_subregion(io, 0xe0, &dev->ioport);
  139. memory_region_add_subregion(io, 0xe4, &dev->flush);
  140. memory_region_add_subregion(io, 0x2000, &dev->irq);
  141. memory_region_add_subregion(mem, 0xff000000, &dev->iomem);
  142. return 0;
  143. }
  144. static void testdev_class_init(ObjectClass *klass, void *data)
  145. {
  146. ISADeviceClass *k = ISA_DEVICE_CLASS(klass);
  147. k->init = init_test_device;
  148. }
  149. static const TypeInfo testdev_info = {
  150. .name = TYPE_TESTDEV,
  151. .parent = TYPE_ISA_DEVICE,
  152. .instance_size = sizeof(PCTestdev),
  153. .class_init = testdev_class_init,
  154. };
  155. static void testdev_register_types(void)
  156. {
  157. type_register_static(&testdev_info);
  158. }
  159. type_init(testdev_register_types)