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