2 * Copyright 2008 IBM Corporation
4 * Copyright 2011 Intel Corporation
5 * Copyright 2016 Veertu, Inc.
6 * Copyright 2017 The Android Open Source Project
8 * QEMU Hypervisor.framework support
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.
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.
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/>.
22 * This file contain code under public domain from the hvdos project:
23 * https://github.com/mist64/hvdos
25 * Parts Copyright (c) 2011 NetApp, Inc.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
50 #include "qemu/osdep.h"
51 #include "qemu/error-report.h"
52 #include "qemu/main-loop.h"
53 #include "sysemu/hvf.h"
54 #include "sysemu/runstate.h"
55 #include "target/i386/cpu.h"
56 #include "qemu/guest-random.h"
58 #include "hvf-accel-ops.h"
61 * The HVF-specific vCPU thread function. This one should only run when the host
62 * CPU supports the VMX "unrestricted guest" feature.
64 static void *hvf_cpu_thread_fn(void *arg
)
70 assert(hvf_enabled());
72 rcu_register_thread();
74 qemu_mutex_lock_iothread();
75 qemu_thread_get_self(cpu
->thread
);
77 cpu
->thread_id
= qemu_get_thread_id();
83 /* signal CPU creation */
84 cpu_thread_signal_created(cpu
);
85 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
88 if (cpu_can_run(cpu
)) {
89 r
= hvf_vcpu_exec(cpu
);
90 if (r
== EXCP_DEBUG
) {
91 cpu_handle_guest_debug(cpu
);
94 qemu_wait_io_event(cpu
);
95 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
97 hvf_vcpu_destroy(cpu
);
98 cpu_thread_signal_destroyed(cpu
);
99 qemu_mutex_unlock_iothread();
100 rcu_unregister_thread();
104 static void hvf_start_vcpu_thread(CPUState
*cpu
)
106 char thread_name
[VCPU_THREAD_NAME_SIZE
];
109 * HVF currently does not support TCG, and only runs in
110 * unrestricted-guest mode.
112 assert(hvf_enabled());
114 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
115 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
116 qemu_cond_init(cpu
->halt_cond
);
118 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/HVF",
120 qemu_thread_create(cpu
->thread
, thread_name
, hvf_cpu_thread_fn
,
121 cpu
, QEMU_THREAD_JOINABLE
);
124 static void hvf_accel_ops_class_init(ObjectClass
*oc
, void *data
)
126 AccelOpsClass
*ops
= ACCEL_OPS_CLASS(oc
);
128 ops
->create_vcpu_thread
= hvf_start_vcpu_thread
;
130 ops
->synchronize_post_reset
= hvf_cpu_synchronize_post_reset
;
131 ops
->synchronize_post_init
= hvf_cpu_synchronize_post_init
;
132 ops
->synchronize_state
= hvf_cpu_synchronize_state
;
133 ops
->synchronize_pre_loadvm
= hvf_cpu_synchronize_pre_loadvm
;
135 static const TypeInfo hvf_accel_ops_type
= {
136 .name
= ACCEL_OPS_NAME("hvf"),
138 .parent
= TYPE_ACCEL_OPS
,
139 .class_init
= hvf_accel_ops_class_init
,
142 static void hvf_accel_ops_register_types(void)
144 type_register_static(&hvf_accel_ops_type
);
146 type_init(hvf_accel_ops_register_types
);