xref: /openbmc/qemu/accel/dummy-cpus.c (revision af531756)
1 /*
2  * Dummy cpu thread code
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/rcu.h"
16 #include "sysemu/cpus.h"
17 #include "qemu/guest-random.h"
18 #include "qemu/main-loop.h"
19 #include "hw/core/cpu.h"
20 
21 static void *dummy_cpu_thread_fn(void *arg)
22 {
23     CPUState *cpu = arg;
24     sigset_t waitset;
25     int r;
26 
27     rcu_register_thread();
28 
29     qemu_mutex_lock_iothread();
30     qemu_thread_get_self(cpu->thread);
31     cpu->thread_id = qemu_get_thread_id();
32     cpu->can_do_io = 1;
33     current_cpu = cpu;
34 
35     sigemptyset(&waitset);
36     sigaddset(&waitset, SIG_IPI);
37 
38     /* signal CPU creation */
39     cpu_thread_signal_created(cpu);
40     qemu_guest_random_seed_thread_part2(cpu->random_seed);
41 
42     do {
43         qemu_mutex_unlock_iothread();
44         do {
45             int sig;
46             r = sigwait(&waitset, &sig);
47         } while (r == -1 && (errno == EAGAIN || errno == EINTR));
48         if (r == -1) {
49             perror("sigwait");
50             exit(1);
51         }
52         qemu_mutex_lock_iothread();
53         qemu_wait_io_event(cpu);
54     } while (!cpu->unplug);
55 
56     qemu_mutex_unlock_iothread();
57     rcu_unregister_thread();
58     return NULL;
59 }
60 
61 void dummy_start_vcpu_thread(CPUState *cpu)
62 {
63     char thread_name[VCPU_THREAD_NAME_SIZE];
64 
65     cpu->thread = g_malloc0(sizeof(QemuThread));
66     cpu->halt_cond = g_malloc0(sizeof(QemuCond));
67     qemu_cond_init(cpu->halt_cond);
68     snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
69              cpu->cpu_index);
70     qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
71                        QEMU_THREAD_JOINABLE);
72 }
73