xref: /qemu/accel/tcg/tcg-accel-ops-mttcg.c (revision 01499add2ae6529589002860e1880ff193a6578a)
1 /*
2  * QEMU TCG Multi Threaded vCPUs implementation
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2014 Red Hat Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "system/tcg.h"
28 #include "system/replay.h"
29 #include "exec/icount.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/notify.h"
32 #include "qemu/guest-random.h"
33 #include "hw/boards.h"
34 #include "tcg/startup.h"
35 #include "tcg-accel-ops.h"
36 #include "tcg-accel-ops-mttcg.h"
37 
38 typedef struct MttcgForceRcuNotifier {
39     Notifier notifier;
40     CPUState *cpu;
41 } MttcgForceRcuNotifier;
42 
43 static void do_nothing(CPUState *cpu, run_on_cpu_data d)
44 {
45 }
46 
47 static void mttcg_force_rcu(Notifier *notify, void *data)
48 {
49     CPUState *cpu = container_of(notify, MttcgForceRcuNotifier, notifier)->cpu;
50 
51     /*
52      * Called with rcu_registry_lock held, using async_run_on_cpu() ensures
53      * that there are no deadlocks.
54      */
55     async_run_on_cpu(cpu, do_nothing, RUN_ON_CPU_NULL);
56 }
57 
58 /*
59  * In the multi-threaded case each vCPU has its own thread. The TLS
60  * variable current_cpu can be used deep in the code to find the
61  * current CPUState for a given thread.
62  */
63 
64 static void *mttcg_cpu_thread_fn(void *arg)
65 {
66     MttcgForceRcuNotifier force_rcu;
67     CPUState *cpu = arg;
68 
69     assert(tcg_enabled());
70     g_assert(!icount_enabled());
71 
72     rcu_register_thread();
73     force_rcu.notifier.notify = mttcg_force_rcu;
74     force_rcu.cpu = cpu;
75     rcu_add_force_rcu_notifier(&force_rcu.notifier);
76     tcg_register_thread();
77 
78     bql_lock();
79     qemu_thread_get_self(cpu->thread);
80 
81     cpu->thread_id = qemu_get_thread_id();
82     cpu->neg.can_do_io = true;
83     current_cpu = cpu;
84     cpu_thread_signal_created(cpu);
85     qemu_guest_random_seed_thread_part2(cpu->random_seed);
86 
87     /* process any pending work */
88     cpu->exit_request = 1;
89 
90     do {
91         if (cpu_can_run(cpu)) {
92             int r;
93             bql_unlock();
94             r = tcg_cpu_exec(cpu);
95             bql_lock();
96             switch (r) {
97             case EXCP_DEBUG:
98                 cpu_handle_guest_debug(cpu);
99                 break;
100             case EXCP_HALTED:
101                 /*
102                  * Usually cpu->halted is set, but may have already been
103                  * reset by another thread by the time we arrive here.
104                  */
105                 break;
106             case EXCP_ATOMIC:
107                 bql_unlock();
108                 cpu_exec_step_atomic(cpu);
109                 bql_lock();
110             default:
111                 /* Ignore everything else? */
112                 break;
113             }
114         }
115 
116         qatomic_set_mb(&cpu->exit_request, 0);
117         qemu_wait_io_event(cpu);
118     } while (!cpu->unplug || cpu_can_run(cpu));
119 
120     tcg_cpu_destroy(cpu);
121     bql_unlock();
122     rcu_remove_force_rcu_notifier(&force_rcu.notifier);
123     rcu_unregister_thread();
124     return NULL;
125 }
126 
127 void mttcg_kick_vcpu_thread(CPUState *cpu)
128 {
129     cpu_exit(cpu);
130 }
131 
132 void mttcg_start_vcpu_thread(CPUState *cpu)
133 {
134     char thread_name[VCPU_THREAD_NAME_SIZE];
135 
136     g_assert(tcg_enabled());
137     tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
138 
139     /* create a thread per vCPU with TCG (MTTCG) */
140     snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
141              cpu->cpu_index);
142 
143     qemu_thread_create(cpu->thread, thread_name, mttcg_cpu_thread_fn,
144                        cpu, QEMU_THREAD_JOINABLE);
145 }
146