blob: b75c919ac358677919598506ce55bacad35601f4 [file] [log] [blame]
Claudio Fontana1583a382020-07-07 10:21:10 +02001/*
Jason Andryuk9ce84a02020-10-13 10:05:10 -04002 * Dummy cpu thread code
Claudio Fontana1583a382020-07-07 10:21:10 +02003 *
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"
Claudio Fontana1583a382020-07-07 10:21:10 +020016#include "sysemu/cpus.h"
Claudio Fontana1583a382020-07-07 10:21:10 +020017#include "qemu/guest-random.h"
18#include "qemu/main-loop.h"
19#include "hw/core/cpu.h"
20
Jason Andryuk9ce84a02020-10-13 10:05:10 -040021static void *dummy_cpu_thread_fn(void *arg)
Claudio Fontana1583a382020-07-07 10:21:10 +020022{
Claudio Fontana1583a382020-07-07 10:21:10 +020023 CPUState *cpu = arg;
Claudio Fontana1583a382020-07-07 10:21:10 +020024
25 rcu_register_thread();
26
27 qemu_mutex_lock_iothread();
28 qemu_thread_get_self(cpu->thread);
29 cpu->thread_id = qemu_get_thread_id();
Richard Henderson464dacf2023-09-15 15:41:39 -070030 cpu->neg.can_do_io = true;
Claudio Fontana1583a382020-07-07 10:21:10 +020031 current_cpu = cpu;
32
Xuzhou Chengc9923552022-10-28 12:57:26 +080033#ifndef _WIN32
34 sigset_t waitset;
35 int r;
36
Claudio Fontana1583a382020-07-07 10:21:10 +020037 sigemptyset(&waitset);
38 sigaddset(&waitset, SIG_IPI);
Xuzhou Chengc9923552022-10-28 12:57:26 +080039#endif
Claudio Fontana1583a382020-07-07 10:21:10 +020040
41 /* signal CPU creation */
42 cpu_thread_signal_created(cpu);
43 qemu_guest_random_seed_thread_part2(cpu->random_seed);
44
45 do {
46 qemu_mutex_unlock_iothread();
Xuzhou Chengc9923552022-10-28 12:57:26 +080047#ifndef _WIN32
Claudio Fontana1583a382020-07-07 10:21:10 +020048 do {
49 int sig;
50 r = sigwait(&waitset, &sig);
51 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
52 if (r == -1) {
53 perror("sigwait");
54 exit(1);
55 }
Xuzhou Chengc9923552022-10-28 12:57:26 +080056#else
57 qemu_sem_wait(&cpu->sem);
58#endif
Claudio Fontana1583a382020-07-07 10:21:10 +020059 qemu_mutex_lock_iothread();
60 qemu_wait_io_event(cpu);
61 } while (!cpu->unplug);
62
63 qemu_mutex_unlock_iothread();
64 rcu_unregister_thread();
65 return NULL;
Claudio Fontana1583a382020-07-07 10:21:10 +020066}
67
Jason Andryuk9ce84a02020-10-13 10:05:10 -040068void dummy_start_vcpu_thread(CPUState *cpu)
Claudio Fontana1583a382020-07-07 10:21:10 +020069{
70 char thread_name[VCPU_THREAD_NAME_SIZE];
71
72 cpu->thread = g_malloc0(sizeof(QemuThread));
73 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
74 qemu_cond_init(cpu->halt_cond);
75 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
76 cpu->cpu_index);
Jason Andryuk9ce84a02020-10-13 10:05:10 -040077 qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
Claudio Fontana1583a382020-07-07 10:21:10 +020078 QEMU_THREAD_JOINABLE);
Xuzhou Chengc9923552022-10-28 12:57:26 +080079#ifdef _WIN32
80 qemu_sem_init(&cpu->sem, 0);
81#endif
Claudio Fontana1583a382020-07-07 10:21:10 +020082}