pc-testdev.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/hw.h"
  41. #include "hw/qdev.h"
  42. #include "hw/isa/isa.h"
  43. #define IOMEM_LEN 0x10000
  44. typedef struct PCTestdev {
  45. ISADevice parent_obj;
  46. MemoryRegion ioport;
  47. MemoryRegion ioport_byte;
  48. MemoryRegion flush;
  49. MemoryRegion irq;
  50. MemoryRegion iomem;
  51. uint32_t ioport_data;
  52. char iomem_buf[IOMEM_LEN];
  53. } PCTestdev;
  54. #define TYPE_TESTDEV "pc-testdev"
  55. #define TESTDEV(obj) \
  56. OBJECT_CHECK(PCTestdev, (obj), TYPE_TESTDEV)
  57. static void test_irq_line(void *opaque, hwaddr addr, uint64_t data,
  58. unsigned len)
  59. {
  60. PCTestdev *dev = opaque;
  61. ISADevice *isa = ISA_DEVICE(dev);
  62. qemu_set_irq(isa_get_irq(isa, addr), !!data);
  63. }
  64. static const MemoryRegionOps test_irq_ops = {
  65. .write = test_irq_line,
  66. .valid.min_access_size = 1,
  67. .valid.max_access_size = 1,
  68. .endianness = DEVICE_LITTLE_ENDIAN,
  69. };
  70. static void test_ioport_write(void *opaque, hwaddr addr, uint64_t data,
  71. unsigned len)
  72. {
  73. PCTestdev *dev = opaque;
  74. int bits = len * 8;
  75. int start_bit = (addr & 3) * 8;
  76. uint32_t mask = ((uint32_t)-1 >> (32 - bits)) << start_bit;
  77. dev->ioport_data &= ~mask;
  78. dev->ioport_data |= data << start_bit;
  79. }
  80. static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len)
  81. {
  82. PCTestdev *dev = opaque;
  83. int bits = len * 8;
  84. int start_bit = (addr & 3) * 8;
  85. uint32_t mask = ((uint32_t)-1 >> (32 - bits)) << start_bit;
  86. return (dev->ioport_data & mask) >> start_bit;
  87. }
  88. static const MemoryRegionOps test_ioport_ops = {
  89. .read = test_ioport_read,
  90. .write = test_ioport_write,
  91. .endianness = DEVICE_LITTLE_ENDIAN,
  92. };
  93. static const MemoryRegionOps test_ioport_byte_ops = {
  94. .read = test_ioport_read,
  95. .write = test_ioport_write,
  96. .valid.min_access_size = 1,
  97. .valid.max_access_size = 4,
  98. .impl.min_access_size = 1,
  99. .impl.max_access_size = 1,
  100. .endianness = DEVICE_LITTLE_ENDIAN,
  101. };
  102. static void test_flush_page(void *opaque, hwaddr addr, uint64_t data,
  103. unsigned len)
  104. {
  105. hwaddr page = 4096;
  106. void *a = cpu_physical_memory_map(data & ~0xffful, &page, 0);
  107. /* We might not be able to get the full page, only mprotect what we actually
  108. have mapped */
  109. #if defined(CONFIG_POSIX)
  110. mprotect(a, page, PROT_NONE);
  111. mprotect(a, page, PROT_READ|PROT_WRITE);
  112. #endif
  113. cpu_physical_memory_unmap(a, page, 0, 0);
  114. }
  115. static const MemoryRegionOps test_flush_ops = {
  116. .write = test_flush_page,
  117. .valid.min_access_size = 4,
  118. .valid.max_access_size = 4,
  119. .endianness = DEVICE_LITTLE_ENDIAN,
  120. };
  121. static uint64_t test_iomem_read(void *opaque, hwaddr addr, unsigned len)
  122. {
  123. PCTestdev *dev = opaque;
  124. uint64_t ret = 0;
  125. memcpy(&ret, &dev->iomem_buf[addr], len);
  126. return ret;
  127. }
  128. static void test_iomem_write(void *opaque, hwaddr addr, uint64_t val,
  129. unsigned len)
  130. {
  131. PCTestdev *dev = opaque;
  132. memcpy(&dev->iomem_buf[addr], &val, len);
  133. dev->iomem_buf[addr] = val;
  134. }
  135. static const MemoryRegionOps test_iomem_ops = {
  136. .read = test_iomem_read,
  137. .write = test_iomem_write,
  138. .endianness = DEVICE_LITTLE_ENDIAN,
  139. };
  140. static void testdev_realizefn(DeviceState *d, Error **errp)
  141. {
  142. ISADevice *isa = ISA_DEVICE(d);
  143. PCTestdev *dev = TESTDEV(d);
  144. MemoryRegion *mem = isa_address_space(isa);
  145. MemoryRegion *io = isa_address_space_io(isa);
  146. memory_region_init_io(&dev->ioport, OBJECT(dev), &test_ioport_ops, dev,
  147. "pc-testdev-ioport", 4);
  148. memory_region_init_io(&dev->ioport_byte, OBJECT(dev),
  149. &test_ioport_byte_ops, dev,
  150. "pc-testdev-ioport-byte", 4);
  151. memory_region_init_io(&dev->flush, OBJECT(dev), &test_flush_ops, dev,
  152. "pc-testdev-flush-page", 4);
  153. memory_region_init_io(&dev->irq, OBJECT(dev), &test_irq_ops, dev,
  154. "pc-testdev-irq-line", 24);
  155. memory_region_init_io(&dev->iomem, OBJECT(dev), &test_iomem_ops, dev,
  156. "pc-testdev-iomem", IOMEM_LEN);
  157. memory_region_add_subregion(io, 0xe0, &dev->ioport);
  158. memory_region_add_subregion(io, 0xe4, &dev->flush);
  159. memory_region_add_subregion(io, 0xe8, &dev->ioport_byte);
  160. memory_region_add_subregion(io, 0x2000, &dev->irq);
  161. memory_region_add_subregion(mem, 0xff000000, &dev->iomem);
  162. }
  163. static void testdev_class_init(ObjectClass *klass, void *data)
  164. {
  165. DeviceClass *dc = DEVICE_CLASS(klass);
  166. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  167. dc->realize = testdev_realizefn;
  168. }
  169. static const TypeInfo testdev_info = {
  170. .name = TYPE_TESTDEV,
  171. .parent = TYPE_ISA_DEVICE,
  172. .instance_size = sizeof(PCTestdev),
  173. .class_init = testdev_class_init,
  174. };
  175. static void testdev_register_types(void)
  176. {
  177. type_register_static(&testdev_info);
  178. }
  179. type_init(testdev_register_types)