| 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 "system/cpus.h" |
| 17 | #include "qemu/guest-random.h" |
| 18 | #include "qemu/main-loop.h" |
| 19 | #include "hw/core/cpu.h" |
| 20 | #include "accel/dummy-cpus.h" |
| 21 | |
| 22 | static void *dummy_cpu_thread_fn(void *arg) |
| 23 | { |
| 24 | CPUState *cpu = arg; |
| 25 | |
| 26 | rcu_register_thread(); |
| 27 | |
| 28 | bql_lock(); |
| 29 | qemu_thread_get_self(cpu->thread); |
| 30 | cpu->thread_id = qemu_get_thread_id(); |
| 31 | current_cpu = cpu; |
| 32 | |
| 33 | #ifndef _WIN32 |
| 34 | sigset_t waitset; |
| 35 | int r; |
| 36 | |
| 37 | sigemptyset(&waitset); |
| 38 | sigaddset(&waitset, SIG_IPI); |
| 39 | #endif |
| 40 | |
| 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_process_cpu_events(cpu); |
| 47 | bql_unlock(); |
| 48 | #ifndef _WIN32 |
| 49 | do { |
| 50 | int sig; |
| 51 | r = sigwait(&waitset, &sig); |
| 52 | } while (r == -1 && (errno == EAGAIN || errno == EINTR)); |
| 53 | if (r == -1) { |
| 54 | perror("sigwait"); |
| 55 | exit(1); |
| 56 | } |
| 57 | #else |
| 58 | qemu_sem_wait(&cpu->sem); |
| 59 | #endif |
| 60 | bql_lock(); |
| 61 | } while (!cpu->unplug); |
| 62 | |
| 63 | bql_unlock(); |
| 64 | rcu_unregister_thread(); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | void dummy_start_vcpu_thread(CPUState *cpu) |
| 69 | { |
| 70 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 71 | |
| 72 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY", |
| 73 | cpu->cpu_index); |
| 74 | qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu, |
| 75 | QEMU_THREAD_JOINABLE); |
| 76 | #ifdef _WIN32 |
| 77 | qemu_sem_init(&cpu->sem, 0); |
| 78 | #endif |
| 79 | } |