2
0

pc-testdev.c 6.7 KB

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