target/mips: Fix TCG temporary leak in gen_cache_operation()
[qemu/ar7.git] / accel / tcg / tcg-accel-ops-mttcg.c
blob847d2079d21f1eedcb3e214ccd1ffec845ce39f6
1 /*
2 * QEMU TCG Multi Threaded vCPUs implementation
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 Red Hat Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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"
35 #include "tcg-accel-ops.h"
36 #include "tcg-accel-ops-mttcg.h"
39 * In the multi-threaded case each vCPU has its own thread. The TLS
40 * variable current_cpu can be used deep in the code to find the
41 * current CPUState for a given thread.
44 static void *mttcg_cpu_thread_fn(void *arg)
46 CPUState *cpu = arg;
48 assert(tcg_enabled());
49 g_assert(!icount_enabled());
51 rcu_register_thread();
52 tcg_register_thread();
54 qemu_mutex_lock_iothread();
55 qemu_thread_get_self(cpu->thread);
57 cpu->thread_id = qemu_get_thread_id();
58 cpu->can_do_io = 1;
59 current_cpu = cpu;
60 cpu_thread_signal_created(cpu);
61 qemu_guest_random_seed_thread_part2(cpu->random_seed);
63 /* process any pending work */
64 cpu->exit_request = 1;
66 do {
67 if (cpu_can_run(cpu)) {
68 int r;
69 qemu_mutex_unlock_iothread();
70 r = tcg_cpus_exec(cpu);
71 qemu_mutex_lock_iothread();
72 switch (r) {
73 case EXCP_DEBUG:
74 cpu_handle_guest_debug(cpu);
75 break;
76 case EXCP_HALTED:
78 * during start-up the vCPU is reset and the thread is
79 * kicked several times. If we don't ensure we go back
80 * to sleep in the halted state we won't cleanly
81 * start-up when the vCPU is enabled.
83 * cpu->halted should ensure we sleep in wait_io_event
85 g_assert(cpu->halted);
86 break;
87 case EXCP_ATOMIC:
88 qemu_mutex_unlock_iothread();
89 cpu_exec_step_atomic(cpu);
90 qemu_mutex_lock_iothread();
91 default:
92 /* Ignore everything else? */
93 break;
97 qatomic_mb_set(&cpu->exit_request, 0);
98 qemu_wait_io_event(cpu);
99 } while (!cpu->unplug || cpu_can_run(cpu));
101 tcg_cpus_destroy(cpu);
102 qemu_mutex_unlock_iothread();
103 rcu_unregister_thread();
104 return NULL;
107 void mttcg_kick_vcpu_thread(CPUState *cpu)
109 cpu_exit(cpu);
112 void mttcg_start_vcpu_thread(CPUState *cpu)
114 char thread_name[VCPU_THREAD_NAME_SIZE];
116 g_assert(tcg_enabled());
117 tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
119 cpu->thread = g_malloc0(sizeof(QemuThread));
120 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
121 qemu_cond_init(cpu->halt_cond);
123 /* create a thread per vCPU with TCG (MTTCG) */
124 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
125 cpu->cpu_index);
127 qemu_thread_create(cpu->thread, thread_name, mttcg_cpu_thread_fn,
128 cpu, QEMU_THREAD_JOINABLE);
130 #ifdef _WIN32
131 cpu->hThread = qemu_thread_get_handle(cpu->thread);
132 #endif