xref: /openbmc/qemu/accel/dummy-cpus.c (revision daff9f7f)
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 
25     rcu_register_thread();
26 
27     bql_lock();
28     qemu_thread_get_self(cpu->thread);
29     cpu->thread_id = qemu_get_thread_id();
30     current_cpu = cpu;
31 
32 #ifndef _WIN32
33     sigset_t waitset;
34     int r;
35 
36     sigemptyset(&waitset);
37     sigaddset(&waitset, SIG_IPI);
38 #endif
39 
40     /* signal CPU creation */
41     cpu_thread_signal_created(cpu);
42     qemu_guest_random_seed_thread_part2(cpu->random_seed);
43 
44     do {
45         bql_unlock();
46 #ifndef _WIN32
47         do {
48             int sig;
49             r = sigwait(&waitset, &sig);
50         } while (r == -1 && (errno == EAGAIN || errno == EINTR));
51         if (r == -1) {
52             perror("sigwait");
53             exit(1);
54         }
55 #else
56         qemu_sem_wait(&cpu->sem);
57 #endif
58         bql_lock();
59         qemu_wait_io_event(cpu);
60     } while (!cpu->unplug);
61 
62     bql_unlock();
63     rcu_unregister_thread();
64     return NULL;
65 }
66 
67 void dummy_start_vcpu_thread(CPUState *cpu)
68 {
69     char thread_name[VCPU_THREAD_NAME_SIZE];
70 
71     snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
72              cpu->cpu_index);
73     qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
74                        QEMU_THREAD_JOINABLE);
75 #ifdef _WIN32
76     qemu_sem_init(&cpu->sem, 0);
77 #endif
78 }
79