hvf-accel-ops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright 2008 IBM Corporation
  3. * 2008 Red Hat, Inc.
  4. * Copyright 2011 Intel Corporation
  5. * Copyright 2016 Veertu, Inc.
  6. * Copyright 2017 The Android Open Source Project
  7. *
  8. * QEMU Hypervisor.framework support
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of version 2 of the GNU General Public
  12. * License as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * This file contain code under public domain from the hvdos project:
  23. * https://github.com/mist64/hvdos
  24. *
  25. * Parts Copyright (c) 2011 NetApp, Inc.
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
  38. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. */
  49. #include "qemu/osdep.h"
  50. #include "qemu/error-report.h"
  51. #include "qemu/main-loop.h"
  52. #include "exec/address-spaces.h"
  53. #include "exec/exec-all.h"
  54. #include "gdbstub/enums.h"
  55. #include "hw/boards.h"
  56. #include "system/cpus.h"
  57. #include "system/hvf.h"
  58. #include "system/hvf_int.h"
  59. #include "system/runstate.h"
  60. #include "qemu/guest-random.h"
  61. HVFState *hvf_state;
  62. /* Memory slots */
  63. hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size)
  64. {
  65. hvf_slot *slot;
  66. int x;
  67. for (x = 0; x < hvf_state->num_slots; ++x) {
  68. slot = &hvf_state->slots[x];
  69. if (slot->size && start < (slot->start + slot->size) &&
  70. (start + size) > slot->start) {
  71. return slot;
  72. }
  73. }
  74. return NULL;
  75. }
  76. struct mac_slot {
  77. int present;
  78. uint64_t size;
  79. uint64_t gpa_start;
  80. uint64_t gva;
  81. };
  82. struct mac_slot mac_slots[32];
  83. static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
  84. {
  85. struct mac_slot *macslot;
  86. hv_return_t ret;
  87. macslot = &mac_slots[slot->slot_id];
  88. if (macslot->present) {
  89. if (macslot->size != slot->size) {
  90. macslot->present = 0;
  91. ret = hv_vm_unmap(macslot->gpa_start, macslot->size);
  92. assert_hvf_ok(ret);
  93. }
  94. }
  95. if (!slot->size) {
  96. return 0;
  97. }
  98. macslot->present = 1;
  99. macslot->gpa_start = slot->start;
  100. macslot->size = slot->size;
  101. ret = hv_vm_map(slot->mem, slot->start, slot->size, flags);
  102. assert_hvf_ok(ret);
  103. return 0;
  104. }
  105. static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
  106. {
  107. hvf_slot *mem;
  108. MemoryRegion *area = section->mr;
  109. bool writable = !area->readonly && !area->rom_device;
  110. hv_memory_flags_t flags;
  111. uint64_t page_size = qemu_real_host_page_size();
  112. if (!memory_region_is_ram(area)) {
  113. if (writable) {
  114. return;
  115. } else if (!memory_region_is_romd(area)) {
  116. /*
  117. * If the memory device is not in romd_mode, then we actually want
  118. * to remove the hvf memory slot so all accesses will trap.
  119. */
  120. add = false;
  121. }
  122. }
  123. if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) ||
  124. !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size)) {
  125. /* Not page aligned, so we can not map as RAM */
  126. add = false;
  127. }
  128. mem = hvf_find_overlap_slot(
  129. section->offset_within_address_space,
  130. int128_get64(section->size));
  131. if (mem && add) {
  132. if (mem->size == int128_get64(section->size) &&
  133. mem->start == section->offset_within_address_space &&
  134. mem->mem == (memory_region_get_ram_ptr(area) +
  135. section->offset_within_region)) {
  136. return; /* Same region was attempted to register, go away. */
  137. }
  138. }
  139. /* Region needs to be reset. set the size to 0 and remap it. */
  140. if (mem) {
  141. mem->size = 0;
  142. if (do_hvf_set_memory(mem, 0)) {
  143. error_report("Failed to reset overlapping slot");
  144. abort();
  145. }
  146. }
  147. if (!add) {
  148. return;
  149. }
  150. if (area->readonly ||
  151. (!memory_region_is_ram(area) && memory_region_is_romd(area))) {
  152. flags = HV_MEMORY_READ | HV_MEMORY_EXEC;
  153. } else {
  154. flags = HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC;
  155. }
  156. /* Now make a new slot. */
  157. int x;
  158. for (x = 0; x < hvf_state->num_slots; ++x) {
  159. mem = &hvf_state->slots[x];
  160. if (!mem->size) {
  161. break;
  162. }
  163. }
  164. if (x == hvf_state->num_slots) {
  165. error_report("No free slots");
  166. abort();
  167. }
  168. mem->size = int128_get64(section->size);
  169. mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region;
  170. mem->start = section->offset_within_address_space;
  171. mem->region = area;
  172. if (do_hvf_set_memory(mem, flags)) {
  173. error_report("Error registering new memory slot");
  174. abort();
  175. }
  176. }
  177. static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
  178. {
  179. if (!cpu->accel->dirty) {
  180. hvf_get_registers(cpu);
  181. cpu->accel->dirty = true;
  182. }
  183. }
  184. static void hvf_cpu_synchronize_state(CPUState *cpu)
  185. {
  186. if (!cpu->accel->dirty) {
  187. run_on_cpu(cpu, do_hvf_cpu_synchronize_state, RUN_ON_CPU_NULL);
  188. }
  189. }
  190. static void do_hvf_cpu_synchronize_set_dirty(CPUState *cpu,
  191. run_on_cpu_data arg)
  192. {
  193. /* QEMU state is the reference, push it to HVF now and on next entry */
  194. cpu->accel->dirty = true;
  195. }
  196. static void hvf_cpu_synchronize_post_reset(CPUState *cpu)
  197. {
  198. run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
  199. }
  200. static void hvf_cpu_synchronize_post_init(CPUState *cpu)
  201. {
  202. run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
  203. }
  204. static void hvf_cpu_synchronize_pre_loadvm(CPUState *cpu)
  205. {
  206. run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
  207. }
  208. static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)
  209. {
  210. hvf_slot *slot;
  211. slot = hvf_find_overlap_slot(
  212. section->offset_within_address_space,
  213. int128_get64(section->size));
  214. /* protect region against writes; begin tracking it */
  215. if (on) {
  216. slot->flags |= HVF_SLOT_LOG;
  217. hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size,
  218. HV_MEMORY_READ | HV_MEMORY_EXEC);
  219. /* stop tracking region*/
  220. } else {
  221. slot->flags &= ~HVF_SLOT_LOG;
  222. hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size,
  223. HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC);
  224. }
  225. }
  226. static void hvf_log_start(MemoryListener *listener,
  227. MemoryRegionSection *section, int old, int new)
  228. {
  229. if (old != 0) {
  230. return;
  231. }
  232. hvf_set_dirty_tracking(section, 1);
  233. }
  234. static void hvf_log_stop(MemoryListener *listener,
  235. MemoryRegionSection *section, int old, int new)
  236. {
  237. if (new != 0) {
  238. return;
  239. }
  240. hvf_set_dirty_tracking(section, 0);
  241. }
  242. static void hvf_log_sync(MemoryListener *listener,
  243. MemoryRegionSection *section)
  244. {
  245. /*
  246. * sync of dirty pages is handled elsewhere; just make sure we keep
  247. * tracking the region.
  248. */
  249. hvf_set_dirty_tracking(section, 1);
  250. }
  251. static void hvf_region_add(MemoryListener *listener,
  252. MemoryRegionSection *section)
  253. {
  254. hvf_set_phys_mem(section, true);
  255. }
  256. static void hvf_region_del(MemoryListener *listener,
  257. MemoryRegionSection *section)
  258. {
  259. hvf_set_phys_mem(section, false);
  260. }
  261. static MemoryListener hvf_memory_listener = {
  262. .name = "hvf",
  263. .priority = MEMORY_LISTENER_PRIORITY_ACCEL,
  264. .region_add = hvf_region_add,
  265. .region_del = hvf_region_del,
  266. .log_start = hvf_log_start,
  267. .log_stop = hvf_log_stop,
  268. .log_sync = hvf_log_sync,
  269. };
  270. static void dummy_signal(int sig)
  271. {
  272. }
  273. bool hvf_allowed;
  274. static int hvf_accel_init(MachineState *ms)
  275. {
  276. int x;
  277. hv_return_t ret;
  278. HVFState *s;
  279. int pa_range = 36;
  280. MachineClass *mc = MACHINE_GET_CLASS(ms);
  281. if (mc->hvf_get_physical_address_range) {
  282. pa_range = mc->hvf_get_physical_address_range(ms);
  283. if (pa_range < 0) {
  284. return -EINVAL;
  285. }
  286. }
  287. ret = hvf_arch_vm_create(ms, (uint32_t)pa_range);
  288. assert_hvf_ok(ret);
  289. s = g_new0(HVFState, 1);
  290. s->num_slots = ARRAY_SIZE(s->slots);
  291. for (x = 0; x < s->num_slots; ++x) {
  292. s->slots[x].size = 0;
  293. s->slots[x].slot_id = x;
  294. }
  295. QTAILQ_INIT(&s->hvf_sw_breakpoints);
  296. hvf_state = s;
  297. memory_listener_register(&hvf_memory_listener, &address_space_memory);
  298. return hvf_arch_init();
  299. }
  300. static inline int hvf_gdbstub_sstep_flags(void)
  301. {
  302. return SSTEP_ENABLE | SSTEP_NOIRQ;
  303. }
  304. static void hvf_accel_class_init(ObjectClass *oc, void *data)
  305. {
  306. AccelClass *ac = ACCEL_CLASS(oc);
  307. ac->name = "HVF";
  308. ac->init_machine = hvf_accel_init;
  309. ac->allowed = &hvf_allowed;
  310. ac->gdbstub_supported_sstep_flags = hvf_gdbstub_sstep_flags;
  311. }
  312. static const TypeInfo hvf_accel_type = {
  313. .name = TYPE_HVF_ACCEL,
  314. .parent = TYPE_ACCEL,
  315. .class_init = hvf_accel_class_init,
  316. };
  317. static void hvf_type_init(void)
  318. {
  319. type_register_static(&hvf_accel_type);
  320. }
  321. type_init(hvf_type_init);
  322. static void hvf_vcpu_destroy(CPUState *cpu)
  323. {
  324. hv_return_t ret = hv_vcpu_destroy(cpu->accel->fd);
  325. assert_hvf_ok(ret);
  326. hvf_arch_vcpu_destroy(cpu);
  327. g_free(cpu->accel);
  328. cpu->accel = NULL;
  329. }
  330. static int hvf_init_vcpu(CPUState *cpu)
  331. {
  332. int r;
  333. cpu->accel = g_new0(AccelCPUState, 1);
  334. /* init cpu signals */
  335. struct sigaction sigact;
  336. memset(&sigact, 0, sizeof(sigact));
  337. sigact.sa_handler = dummy_signal;
  338. sigaction(SIG_IPI, &sigact, NULL);
  339. pthread_sigmask(SIG_BLOCK, NULL, &cpu->accel->unblock_ipi_mask);
  340. sigdelset(&cpu->accel->unblock_ipi_mask, SIG_IPI);
  341. #ifdef __aarch64__
  342. r = hv_vcpu_create(&cpu->accel->fd,
  343. (hv_vcpu_exit_t **)&cpu->accel->exit, NULL);
  344. #else
  345. r = hv_vcpu_create(&cpu->accel->fd, HV_VCPU_DEFAULT);
  346. #endif
  347. cpu->accel->dirty = true;
  348. assert_hvf_ok(r);
  349. cpu->accel->guest_debug_enabled = false;
  350. return hvf_arch_init_vcpu(cpu);
  351. }
  352. /*
  353. * The HVF-specific vCPU thread function. This one should only run when the host
  354. * CPU supports the VMX "unrestricted guest" feature.
  355. */
  356. static void *hvf_cpu_thread_fn(void *arg)
  357. {
  358. CPUState *cpu = arg;
  359. int r;
  360. assert(hvf_enabled());
  361. rcu_register_thread();
  362. bql_lock();
  363. qemu_thread_get_self(cpu->thread);
  364. cpu->thread_id = qemu_get_thread_id();
  365. current_cpu = cpu;
  366. hvf_init_vcpu(cpu);
  367. /* signal CPU creation */
  368. cpu_thread_signal_created(cpu);
  369. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  370. do {
  371. if (cpu_can_run(cpu)) {
  372. r = hvf_vcpu_exec(cpu);
  373. if (r == EXCP_DEBUG) {
  374. cpu_handle_guest_debug(cpu);
  375. }
  376. }
  377. qemu_wait_io_event(cpu);
  378. } while (!cpu->unplug || cpu_can_run(cpu));
  379. hvf_vcpu_destroy(cpu);
  380. cpu_thread_signal_destroyed(cpu);
  381. bql_unlock();
  382. rcu_unregister_thread();
  383. return NULL;
  384. }
  385. static void hvf_start_vcpu_thread(CPUState *cpu)
  386. {
  387. char thread_name[VCPU_THREAD_NAME_SIZE];
  388. /*
  389. * HVF currently does not support TCG, and only runs in
  390. * unrestricted-guest mode.
  391. */
  392. assert(hvf_enabled());
  393. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
  394. cpu->cpu_index);
  395. qemu_thread_create(cpu->thread, thread_name, hvf_cpu_thread_fn,
  396. cpu, QEMU_THREAD_JOINABLE);
  397. }
  398. static int hvf_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len)
  399. {
  400. struct hvf_sw_breakpoint *bp;
  401. int err;
  402. if (type == GDB_BREAKPOINT_SW) {
  403. bp = hvf_find_sw_breakpoint(cpu, addr);
  404. if (bp) {
  405. bp->use_count++;
  406. return 0;
  407. }
  408. bp = g_new(struct hvf_sw_breakpoint, 1);
  409. bp->pc = addr;
  410. bp->use_count = 1;
  411. err = hvf_arch_insert_sw_breakpoint(cpu, bp);
  412. if (err) {
  413. g_free(bp);
  414. return err;
  415. }
  416. QTAILQ_INSERT_HEAD(&hvf_state->hvf_sw_breakpoints, bp, entry);
  417. } else {
  418. err = hvf_arch_insert_hw_breakpoint(addr, len, type);
  419. if (err) {
  420. return err;
  421. }
  422. }
  423. CPU_FOREACH(cpu) {
  424. err = hvf_update_guest_debug(cpu);
  425. if (err) {
  426. return err;
  427. }
  428. }
  429. return 0;
  430. }
  431. static int hvf_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len)
  432. {
  433. struct hvf_sw_breakpoint *bp;
  434. int err;
  435. if (type == GDB_BREAKPOINT_SW) {
  436. bp = hvf_find_sw_breakpoint(cpu, addr);
  437. if (!bp) {
  438. return -ENOENT;
  439. }
  440. if (bp->use_count > 1) {
  441. bp->use_count--;
  442. return 0;
  443. }
  444. err = hvf_arch_remove_sw_breakpoint(cpu, bp);
  445. if (err) {
  446. return err;
  447. }
  448. QTAILQ_REMOVE(&hvf_state->hvf_sw_breakpoints, bp, entry);
  449. g_free(bp);
  450. } else {
  451. err = hvf_arch_remove_hw_breakpoint(addr, len, type);
  452. if (err) {
  453. return err;
  454. }
  455. }
  456. CPU_FOREACH(cpu) {
  457. err = hvf_update_guest_debug(cpu);
  458. if (err) {
  459. return err;
  460. }
  461. }
  462. return 0;
  463. }
  464. static void hvf_remove_all_breakpoints(CPUState *cpu)
  465. {
  466. struct hvf_sw_breakpoint *bp, *next;
  467. CPUState *tmpcpu;
  468. QTAILQ_FOREACH_SAFE(bp, &hvf_state->hvf_sw_breakpoints, entry, next) {
  469. if (hvf_arch_remove_sw_breakpoint(cpu, bp) != 0) {
  470. /* Try harder to find a CPU that currently sees the breakpoint. */
  471. CPU_FOREACH(tmpcpu)
  472. {
  473. if (hvf_arch_remove_sw_breakpoint(tmpcpu, bp) == 0) {
  474. break;
  475. }
  476. }
  477. }
  478. QTAILQ_REMOVE(&hvf_state->hvf_sw_breakpoints, bp, entry);
  479. g_free(bp);
  480. }
  481. hvf_arch_remove_all_hw_breakpoints();
  482. CPU_FOREACH(cpu) {
  483. hvf_update_guest_debug(cpu);
  484. }
  485. }
  486. static void hvf_accel_ops_class_init(ObjectClass *oc, void *data)
  487. {
  488. AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
  489. ops->create_vcpu_thread = hvf_start_vcpu_thread;
  490. ops->kick_vcpu_thread = hvf_kick_vcpu_thread;
  491. ops->synchronize_post_reset = hvf_cpu_synchronize_post_reset;
  492. ops->synchronize_post_init = hvf_cpu_synchronize_post_init;
  493. ops->synchronize_state = hvf_cpu_synchronize_state;
  494. ops->synchronize_pre_loadvm = hvf_cpu_synchronize_pre_loadvm;
  495. ops->insert_breakpoint = hvf_insert_breakpoint;
  496. ops->remove_breakpoint = hvf_remove_breakpoint;
  497. ops->remove_all_breakpoints = hvf_remove_all_breakpoints;
  498. ops->update_guest_debug = hvf_update_guest_debug;
  499. ops->supports_guest_debug = hvf_arch_supports_guest_debug;
  500. };
  501. static const TypeInfo hvf_accel_ops_type = {
  502. .name = ACCEL_OPS_NAME("hvf"),
  503. .parent = TYPE_ACCEL_OPS,
  504. .class_init = hvf_accel_ops_class_init,
  505. .abstract = true,
  506. };
  507. static void hvf_accel_ops_register_types(void)
  508. {
  509. type_register_static(&hvf_accel_ops_type);
  510. }
  511. type_init(hvf_accel_ops_register_types);