]> git.proxmox.com Git - mirror_qemu.git/blame - accel/tcg/tcg-cpus-mttcg.c
accel/tcg: split tcg_start_vcpu_thread
[mirror_qemu.git] / accel / tcg / tcg-cpus-mttcg.c
CommitLineData
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"
27#include "qemu-common.h"
28#include "sysemu/tcg.h"
29#include "sysemu/replay.h"
30#include "qemu/main-loop.h"
31#include "qemu/guest-random.h"
32#include "exec/exec-all.h"
33#include "hw/boards.h"
34
35#include "tcg-cpus.h"
45e077d7
CF
36
37/*
38 * In the multi-threaded case each vCPU has its own thread. The TLS
39 * variable current_cpu can be used deep in the code to find the
40 * current CPUState for a given thread.
41 */
42
37c2f9a7 43static void *tcg_cpu_thread_fn(void *arg)
45e077d7
CF
44{
45 CPUState *cpu = arg;
46
47 assert(tcg_enabled());
48 g_assert(!icount_enabled());
49
50 rcu_register_thread();
51 tcg_register_thread();
52
53 qemu_mutex_lock_iothread();
54 qemu_thread_get_self(cpu->thread);
55
56 cpu->thread_id = qemu_get_thread_id();
57 cpu->can_do_io = 1;
58 current_cpu = cpu;
59 cpu_thread_signal_created(cpu);
60 qemu_guest_random_seed_thread_part2(cpu->random_seed);
61
62 /* process any pending work */
63 cpu->exit_request = 1;
64
65 do {
66 if (cpu_can_run(cpu)) {
67 int r;
68 qemu_mutex_unlock_iothread();
69 r = tcg_cpu_exec(cpu);
70 qemu_mutex_lock_iothread();
71 switch (r) {
72 case EXCP_DEBUG:
73 cpu_handle_guest_debug(cpu);
74 break;
75 case EXCP_HALTED:
76 /*
77 * during start-up the vCPU is reset and the thread is
78 * kicked several times. If we don't ensure we go back
79 * to sleep in the halted state we won't cleanly
80 * start-up when the vCPU is enabled.
81 *
82 * cpu->halted should ensure we sleep in wait_io_event
83 */
84 g_assert(cpu->halted);
85 break;
86 case EXCP_ATOMIC:
87 qemu_mutex_unlock_iothread();
88 cpu_exec_step_atomic(cpu);
89 qemu_mutex_lock_iothread();
90 default:
91 /* Ignore everything else? */
92 break;
93 }
94 }
95
96 qatomic_mb_set(&cpu->exit_request, 0);
97 qemu_wait_io_event(cpu);
98 } while (!cpu->unplug || cpu_can_run(cpu));
99
100 qemu_tcg_destroy_vcpu(cpu);
101 qemu_mutex_unlock_iothread();
102 rcu_unregister_thread();
103 return NULL;
104}
105
106static void mttcg_kick_vcpu_thread(CPUState *cpu)
107{
108 cpu_exit(cpu);
109}
110
37c2f9a7
CF
111static void mttcg_start_vcpu_thread(CPUState *cpu)
112{
113 char thread_name[VCPU_THREAD_NAME_SIZE];
114
115 g_assert(tcg_enabled());
116
117 parallel_cpus = (current_machine->smp.max_cpus > 1);
118
119 cpu->thread = g_malloc0(sizeof(QemuThread));
120 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
121 qemu_cond_init(cpu->halt_cond);
122
123 /* create a thread per vCPU with TCG (MTTCG) */
124 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
125 cpu->cpu_index);
126
127 qemu_thread_create(cpu->thread, thread_name, tcg_cpu_thread_fn,
128 cpu, QEMU_THREAD_JOINABLE);
129
130#ifdef _WIN32
131 cpu->hThread = qemu_thread_get_handle(cpu->thread);
132#endif
133}
134
45e077d7 135const CpusAccel tcg_cpus_mttcg = {
37c2f9a7 136 .create_vcpu_thread = mttcg_start_vcpu_thread,
45e077d7
CF
137 .kick_vcpu_thread = mttcg_kick_vcpu_thread,
138
139 .handle_interrupt = tcg_handle_interrupt,
140};