vmport.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * QEMU VMPort emulation
  3. *
  4. * Copyright (C) 2007 Hervé Poussineau
  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. * Guest code that interacts with this virtual device can be found
  26. * in VMware open-vm-tools open-source project:
  27. * https://github.com/vmware/open-vm-tools
  28. */
  29. #include "qemu/osdep.h"
  30. #include "hw/isa/isa.h"
  31. #include "hw/i386/vmport.h"
  32. #include "hw/qdev-properties.h"
  33. #include "hw/boards.h"
  34. #include "system/system.h"
  35. #include "system/hw_accel.h"
  36. #include "system/qtest.h"
  37. #include "qemu/log.h"
  38. #include "trace.h"
  39. #include "qom/object.h"
  40. #define VMPORT_MAGIC 0x564D5868
  41. /* Compatibility flags for migration */
  42. #define VMPORT_COMPAT_READ_SET_EAX_BIT 0
  43. #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1
  44. #define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2
  45. #define VMPORT_COMPAT_CMDS_V2_BIT 3
  46. #define VMPORT_COMPAT_READ_SET_EAX \
  47. (1 << VMPORT_COMPAT_READ_SET_EAX_BIT)
  48. #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \
  49. (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT)
  50. #define VMPORT_COMPAT_REPORT_VMX_TYPE \
  51. (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT)
  52. #define VMPORT_COMPAT_CMDS_V2 \
  53. (1 << VMPORT_COMPAT_CMDS_V2_BIT)
  54. /* vCPU features reported by CMD_GET_VCPU_INFO */
  55. #define VCPU_INFO_SLC64_BIT 0
  56. #define VCPU_INFO_SYNC_VTSCS_BIT 1
  57. #define VCPU_INFO_HV_REPLAY_OK_BIT 2
  58. #define VCPU_INFO_LEGACY_X2APIC_BIT 3
  59. #define VCPU_INFO_RESERVED_BIT 31
  60. OBJECT_DECLARE_SIMPLE_TYPE(VMPortState, VMPORT)
  61. struct VMPortState {
  62. ISADevice parent_obj;
  63. MemoryRegion io;
  64. VMPortReadFunc *func[VMPORT_ENTRIES];
  65. void *opaque[VMPORT_ENTRIES];
  66. uint32_t vmware_vmx_version;
  67. uint8_t vmware_vmx_type;
  68. uint32_t compat_flags;
  69. };
  70. static VMPortState *port_state;
  71. void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque)
  72. {
  73. assert(command < VMPORT_ENTRIES);
  74. assert(port_state);
  75. trace_vmport_register(command, func, opaque);
  76. port_state->func[command] = func;
  77. port_state->opaque[command] = opaque;
  78. }
  79. static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
  80. unsigned size)
  81. {
  82. VMPortState *s = opaque;
  83. CPUState *cs = current_cpu;
  84. X86CPU *cpu = X86_CPU(cs);
  85. CPUX86State *env;
  86. unsigned char command;
  87. uint32_t eax;
  88. if (qtest_enabled()) {
  89. return -1;
  90. }
  91. env = &cpu->env;
  92. cpu_synchronize_state(cs);
  93. eax = env->regs[R_EAX];
  94. if (eax != VMPORT_MAGIC) {
  95. goto err;
  96. }
  97. command = env->regs[R_ECX];
  98. trace_vmport_command(command);
  99. if (command >= VMPORT_ENTRIES || !s->func[command]) {
  100. qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
  101. goto err;
  102. }
  103. eax = s->func[command](s->opaque[command], addr);
  104. goto out;
  105. err:
  106. if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) {
  107. eax = UINT32_MAX;
  108. }
  109. out:
  110. /*
  111. * The call above to cpu_synchronize_state() gets vCPU registers values
  112. * to QEMU but also cause QEMU to write QEMU vCPU registers values to
  113. * vCPU implementation (e.g. Accelerator such as KVM) just before
  114. * resuming guest.
  115. *
  116. * Therefore, in order to make IOPort return value propagate to
  117. * guest EAX, we need to explicitly update QEMU EAX register value.
  118. */
  119. if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) {
  120. cpu->env.regs[R_EAX] = eax;
  121. }
  122. return eax;
  123. }
  124. static void vmport_ioport_write(void *opaque, hwaddr addr,
  125. uint64_t val, unsigned size)
  126. {
  127. X86CPU *cpu = X86_CPU(current_cpu);
  128. if (qtest_enabled()) {
  129. return;
  130. }
  131. cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
  132. }
  133. static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
  134. {
  135. X86CPU *cpu = X86_CPU(current_cpu);
  136. if (qtest_enabled()) {
  137. return -1;
  138. }
  139. cpu->env.regs[R_EBX] = VMPORT_MAGIC;
  140. if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) {
  141. cpu->env.regs[R_ECX] = port_state->vmware_vmx_type;
  142. }
  143. return port_state->vmware_vmx_version;
  144. }
  145. static uint32_t vmport_cmd_get_bios_uuid(void *opaque, uint32_t addr)
  146. {
  147. X86CPU *cpu = X86_CPU(current_cpu);
  148. uint32_t *uuid_parts = (uint32_t *)(qemu_uuid.data);
  149. cpu->env.regs[R_EAX] = le32_to_cpu(uuid_parts[0]);
  150. cpu->env.regs[R_EBX] = le32_to_cpu(uuid_parts[1]);
  151. cpu->env.regs[R_ECX] = le32_to_cpu(uuid_parts[2]);
  152. cpu->env.regs[R_EDX] = le32_to_cpu(uuid_parts[3]);
  153. return cpu->env.regs[R_EAX];
  154. }
  155. static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
  156. {
  157. X86CPU *cpu = X86_CPU(current_cpu);
  158. if (qtest_enabled()) {
  159. return -1;
  160. }
  161. cpu->env.regs[R_EBX] = 0x1177;
  162. return current_machine->ram_size;
  163. }
  164. static uint32_t vmport_cmd_get_hz(void *opaque, uint32_t addr)
  165. {
  166. X86CPU *cpu = X86_CPU(current_cpu);
  167. if (cpu->env.tsc_khz && cpu->env.apic_bus_freq) {
  168. uint64_t tsc_freq = (uint64_t)cpu->env.tsc_khz * 1000;
  169. cpu->env.regs[R_ECX] = cpu->env.apic_bus_freq;
  170. cpu->env.regs[R_EBX] = (uint32_t)(tsc_freq >> 32);
  171. cpu->env.regs[R_EAX] = (uint32_t)tsc_freq;
  172. } else {
  173. /* Signal cmd as not supported */
  174. cpu->env.regs[R_EBX] = UINT32_MAX;
  175. }
  176. return cpu->env.regs[R_EAX];
  177. }
  178. static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr)
  179. {
  180. X86CPU *cpu = X86_CPU(current_cpu);
  181. uint32_t ret = 0;
  182. if (cpu->env.features[FEAT_1_ECX] & CPUID_EXT_X2APIC) {
  183. ret |= 1 << VCPU_INFO_LEGACY_X2APIC_BIT;
  184. }
  185. return ret;
  186. }
  187. static const MemoryRegionOps vmport_ops = {
  188. .read = vmport_ioport_read,
  189. .write = vmport_ioport_write,
  190. .impl = {
  191. .min_access_size = 4,
  192. .max_access_size = 4,
  193. },
  194. .endianness = DEVICE_LITTLE_ENDIAN,
  195. };
  196. static void vmport_realizefn(DeviceState *dev, Error **errp)
  197. {
  198. ISADevice *isadev = ISA_DEVICE(dev);
  199. VMPortState *s = VMPORT(dev);
  200. memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
  201. isa_register_ioport(isadev, &s->io, 0x5658);
  202. port_state = s;
  203. /* Register some generic port commands */
  204. vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
  205. vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
  206. if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) {
  207. vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL);
  208. vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL);
  209. vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info,
  210. NULL);
  211. }
  212. }
  213. static const Property vmport_properties[] = {
  214. /* Used to enforce compatibility for migration */
  215. DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags,
  216. VMPORT_COMPAT_READ_SET_EAX_BIT, true),
  217. DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags,
  218. VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true),
  219. DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags,
  220. VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true),
  221. DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags,
  222. VMPORT_COMPAT_CMDS_V2_BIT, true),
  223. /* Default value taken from open-vm-tools code VERSION_MAGIC definition */
  224. DEFINE_PROP_UINT32("vmware-vmx-version", VMPortState,
  225. vmware_vmx_version, 6),
  226. /*
  227. * Value determines which VMware product type host report itself to guest.
  228. *
  229. * Most guests are fine with exposing host as VMware ESX server.
  230. * Some legacy/proprietary guests hard-code a given type.
  231. *
  232. * For a complete list of values, refer to enum VMXType at open-vm-tools
  233. * project (Defined at lib/include/vm_vmx_type.h).
  234. *
  235. * Reasonable options:
  236. * 0 - Unset
  237. * 1 - VMware Express (deprecated)
  238. * 2 - VMware ESX Server
  239. * 3 - VMware Server (Deprecated)
  240. * 4 - VMware Workstation
  241. * 5 - ACE 1.x (Deprecated)
  242. */
  243. DEFINE_PROP_UINT8("vmware-vmx-type", VMPortState, vmware_vmx_type, 2),
  244. };
  245. static void vmport_class_initfn(ObjectClass *klass, void *data)
  246. {
  247. DeviceClass *dc = DEVICE_CLASS(klass);
  248. dc->realize = vmport_realizefn;
  249. /* Reason: realize sets global port_state */
  250. dc->user_creatable = false;
  251. device_class_set_props(dc, vmport_properties);
  252. }
  253. static const TypeInfo vmport_info = {
  254. .name = TYPE_VMPORT,
  255. .parent = TYPE_ISA_DEVICE,
  256. .instance_size = sizeof(VMPortState),
  257. .class_init = vmport_class_initfn,
  258. };
  259. static void vmport_register_types(void)
  260. {
  261. type_register_static(&vmport_info);
  262. }
  263. type_init(vmport_register_types)