/src/qemu/accel/tcg/tcg-accel-ops-mttcg.c
Line | Count | Source |
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 "exec/cpu-common.h" |
28 | | #include "system/tcg.h" |
29 | | #include "system/replay.h" |
30 | | #include "exec/icount.h" |
31 | | #include "qemu/main-loop.h" |
32 | | #include "qemu/notify.h" |
33 | | #include "qemu/guest-random.h" |
34 | | #include "hw/core/boards.h" |
35 | | #include "accel/tcg/cpu-loop.h" |
36 | | #include "tcg/startup.h" |
37 | | #include "tcg-accel-ops.h" |
38 | | #include "tcg-accel-ops-mttcg.h" |
39 | | |
40 | | typedef struct MttcgForceRcuNotifier { |
41 | | Notifier notifier; |
42 | | CPUState *cpu; |
43 | | } MttcgForceRcuNotifier; |
44 | | |
45 | | static void do_nothing(CPUState *cpu, run_on_cpu_data d) |
46 | 0 | { |
47 | 0 | } |
48 | | |
49 | | static void mttcg_force_rcu(Notifier *notify, void *data) |
50 | 0 | { |
51 | 0 | CPUState *cpu = container_of(notify, MttcgForceRcuNotifier, notifier)->cpu; |
52 | | |
53 | | /* |
54 | | * Called with rcu_registry_lock held, using async_run_on_cpu() ensures |
55 | | * that there are no deadlocks. |
56 | | */ |
57 | 0 | async_run_on_cpu(cpu, do_nothing, RUN_ON_CPU_NULL); |
58 | 0 | } |
59 | | |
60 | | /* |
61 | | * In the multi-threaded case each vCPU has its own thread. The TLS |
62 | | * variable current_cpu can be used deep in the code to find the |
63 | | * current CPUState for a given thread. |
64 | | */ |
65 | | |
66 | | static void *mttcg_cpu_thread_fn(void *arg) |
67 | 0 | { |
68 | 0 | MttcgForceRcuNotifier force_rcu; |
69 | 0 | CPUState *cpu = arg; |
70 | |
|
71 | 0 | assert(tcg_enabled()); |
72 | 0 | g_assert(!icount_enabled()); |
73 | | |
74 | 0 | rcu_register_thread(); |
75 | 0 | force_rcu.notifier.notify = mttcg_force_rcu; |
76 | 0 | force_rcu.cpu = cpu; |
77 | 0 | rcu_add_force_rcu_notifier(&force_rcu.notifier); |
78 | 0 | tcg_register_thread(); |
79 | |
|
80 | 0 | bql_lock(); |
81 | 0 | qemu_thread_get_self(cpu->thread); |
82 | |
|
83 | 0 | cpu->thread_id = qemu_get_thread_id(); |
84 | 0 | cpu->neg.can_do_io = true; |
85 | 0 | current_cpu = cpu; |
86 | 0 | cpu_thread_signal_created(cpu); |
87 | 0 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
88 | |
|
89 | 0 | do { |
90 | 0 | qemu_process_cpu_events(cpu); |
91 | |
|
92 | 0 | if (cpu_can_run(cpu)) { |
93 | 0 | int excp; |
94 | |
|
95 | 0 | bql_unlock(); |
96 | 0 | excp = tcg_cpu_exec(cpu); |
97 | 0 | bql_lock(); |
98 | 0 | switch (excp) { |
99 | 0 | case EXCP_DEBUG: |
100 | 0 | cpu_handle_guest_debug(cpu); |
101 | 0 | break; |
102 | 0 | case EXCP_HALTED: |
103 | | /* |
104 | | * Usually cpu->halted is set, but may have already been |
105 | | * reset by another thread by the time we arrive here. |
106 | | */ |
107 | 0 | break; |
108 | 0 | case EXCP_ATOMIC: |
109 | 0 | bql_unlock(); |
110 | 0 | cpu_exec_step_atomic(cpu); |
111 | 0 | bql_lock(); |
112 | 0 | default: |
113 | | /* Ignore everything else? */ |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } while (!cpu->unplug || cpu_can_run(cpu)); |
118 | | |
119 | 0 | tcg_cpu_destroy(cpu); |
120 | 0 | bql_unlock(); |
121 | 0 | rcu_remove_force_rcu_notifier(&force_rcu.notifier); |
122 | 0 | rcu_unregister_thread(); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | | |
126 | | void mttcg_start_vcpu_thread(CPUState *cpu) |
127 | 0 | { |
128 | 0 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
129 | |
|
130 | 0 | g_assert(tcg_enabled()); |
131 | 0 | tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1); |
132 | | |
133 | | /* create a thread per vCPU with TCG (MTTCG) */ |
134 | 0 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG", |
135 | 0 | cpu->cpu_index); |
136 | |
|
137 | 0 | qemu_thread_create(cpu->thread, thread_name, mttcg_cpu_thread_fn, |
138 | 0 | cpu, QEMU_THREAD_JOINABLE); |
139 | 0 | } |