xref: /openbmc/qemu/accel/tcg/tcg-accel-ops.c (revision 6f03770d)
1 /*
2  * QEMU TCG vCPU common functionality
3  *
4  * Functionality common to all TCG vCPU variants: mttcg, rr and icount.
5  *
6  * Copyright (c) 2003-2008 Fabrice Bellard
7  * Copyright (c) 2014 Red Hat Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qemu-common.h"
30 #include "sysemu/tcg.h"
31 #include "sysemu/replay.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/guest-random.h"
34 #include "exec/exec-all.h"
35 #include "hw/boards.h"
36 
37 #include "tcg-accel-ops.h"
38 #include "tcg-accel-ops-mttcg.h"
39 #include "tcg-accel-ops-rr.h"
40 #include "tcg-accel-ops-icount.h"
41 
42 /* common functionality among all TCG variants */
43 
44 void tcg_cpus_destroy(CPUState *cpu)
45 {
46     cpu_thread_signal_destroyed(cpu);
47 }
48 
49 int tcg_cpus_exec(CPUState *cpu)
50 {
51     int ret;
52 #ifdef CONFIG_PROFILER
53     int64_t ti;
54 #endif
55     assert(tcg_enabled());
56 #ifdef CONFIG_PROFILER
57     ti = profile_getclock();
58 #endif
59     cpu_exec_start(cpu);
60     ret = cpu_exec(cpu);
61     cpu_exec_end(cpu);
62 #ifdef CONFIG_PROFILER
63     qatomic_set(&tcg_ctx->prof.cpu_exec_time,
64                 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
65 #endif
66     return ret;
67 }
68 
69 /* mask must never be zero, except for A20 change call */
70 void tcg_handle_interrupt(CPUState *cpu, int mask)
71 {
72     g_assert(qemu_mutex_iothread_locked());
73 
74     cpu->interrupt_request |= mask;
75 
76     /*
77      * If called from iothread context, wake the target cpu in
78      * case its halted.
79      */
80     if (!qemu_cpu_is_self(cpu)) {
81         qemu_cpu_kick(cpu);
82     } else {
83         qatomic_set(&cpu_neg(cpu)->icount_decr.u16.high, -1);
84     }
85 }
86 
87 static void tcg_accel_ops_init(AccelOpsClass *ops)
88 {
89     if (qemu_tcg_mttcg_enabled()) {
90         ops->create_vcpu_thread = mttcg_start_vcpu_thread;
91         ops->kick_vcpu_thread = mttcg_kick_vcpu_thread;
92         ops->handle_interrupt = tcg_handle_interrupt;
93     } else if (icount_enabled()) {
94         ops->create_vcpu_thread = rr_start_vcpu_thread;
95         ops->kick_vcpu_thread = rr_kick_vcpu_thread;
96         ops->handle_interrupt = icount_handle_interrupt;
97         ops->get_virtual_clock = icount_get;
98         ops->get_elapsed_ticks = icount_get;
99     } else {
100         ops->create_vcpu_thread = rr_start_vcpu_thread;
101         ops->kick_vcpu_thread = rr_kick_vcpu_thread;
102         ops->handle_interrupt = tcg_handle_interrupt;
103     }
104 }
105 
106 static void tcg_accel_ops_class_init(ObjectClass *oc, void *data)
107 {
108     AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
109 
110     ops->ops_init = tcg_accel_ops_init;
111 }
112 
113 static const TypeInfo tcg_accel_ops_type = {
114     .name = ACCEL_OPS_NAME("tcg"),
115 
116     .parent = TYPE_ACCEL_OPS,
117     .class_init = tcg_accel_ops_class_init,
118     .abstract = true,
119 };
120 
121 static void tcg_accel_ops_register_types(void)
122 {
123     type_register_static(&tcg_accel_ops_type);
124 }
125 type_init(tcg_accel_ops_register_types);
126