qemu/include/qom/cpu.h
<<
>>
Prefs
   1/*
   2 * QEMU CPU model
   3 *
   4 * Copyright (c) 2012 SUSE LINUX Products GmbH
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version 2
   9 * of the License, or (at your option) any later version.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * along with this program; if not, see
  18 * <http://www.gnu.org/licenses/gpl-2.0.html>
  19 */
  20#ifndef QEMU_CPU_H
  21#define QEMU_CPU_H
  22
  23#include <signal.h>
  24#include "hw/qdev-core.h"
  25#include "qemu/thread.h"
  26
  27typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
  28
  29/**
  30 * SECTION:cpu
  31 * @section_id: QEMU-cpu
  32 * @title: CPU Class
  33 * @short_description: Base class for all CPUs
  34 */
  35
  36#define TYPE_CPU "cpu"
  37
  38#define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
  39#define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
  40#define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
  41
  42typedef struct CPUState CPUState;
  43
  44/**
  45 * CPUClass:
  46 * @class_by_name: Callback to map -cpu command line model name to an
  47 * instantiatable CPU type.
  48 * @reset: Callback to reset the #CPUState to its initial state.
  49 * @do_interrupt: Callback for interrupt handling.
  50 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
  51 * @vmsd: State description for migration.
  52 *
  53 * Represents a CPU family or model.
  54 */
  55typedef struct CPUClass {
  56    /*< private >*/
  57    DeviceClass parent_class;
  58    /*< public >*/
  59
  60    ObjectClass *(*class_by_name)(const char *cpu_model);
  61
  62    void (*reset)(CPUState *cpu);
  63    void (*do_interrupt)(CPUState *cpu);
  64    int64_t (*get_arch_id)(CPUState *cpu);
  65
  66    const struct VMStateDescription *vmsd;
  67    int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
  68                            int cpuid, void *opaque);
  69    int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
  70                                void *opaque);
  71    int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
  72                            int cpuid, void *opaque);
  73    int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
  74                                void *opaque);
  75} CPUClass;
  76
  77struct KVMState;
  78struct kvm_run;
  79
  80/**
  81 * CPUState:
  82 * @cpu_index: CPU index (informative).
  83 * @nr_cores: Number of cores within this CPU package.
  84 * @nr_threads: Number of threads within this CPU.
  85 * @numa_node: NUMA node this CPU is belonging to.
  86 * @host_tid: Host thread ID.
  87 * @running: #true if CPU is currently running (usermode).
  88 * @created: Indicates whether the CPU thread has been successfully created.
  89 * @interrupt_request: Indicates a pending interrupt request.
  90 * @halted: Nonzero if the CPU is in suspended state.
  91 * @stop: Indicates a pending stop request.
  92 * @stopped: Indicates the CPU has been artificially stopped.
  93 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
  94 *           CPU and return to its top level loop.
  95 * @env_ptr: Pointer to subclass-specific CPUArchState field.
  96 * @current_tb: Currently executing TB.
  97 * @kvm_fd: vCPU file descriptor for KVM.
  98 *
  99 * State of one CPU core or thread.
 100 */
 101struct CPUState {
 102    /*< private >*/
 103    DeviceState parent_obj;
 104    /*< public >*/
 105
 106    int nr_cores;
 107    int nr_threads;
 108    int numa_node;
 109
 110    struct QemuThread *thread;
 111#ifdef _WIN32
 112    HANDLE hThread;
 113#endif
 114    int thread_id;
 115    uint32_t host_tid;
 116    bool running;
 117    struct QemuCond *halt_cond;
 118    struct qemu_work_item *queued_work_first, *queued_work_last;
 119    bool thread_kicked;
 120    bool created;
 121    bool stop;
 122    bool stopped;
 123    volatile sig_atomic_t exit_request;
 124    volatile sig_atomic_t tcg_exit_req;
 125    uint32_t interrupt_request;
 126
 127    void *env_ptr; /* CPUArchState */
 128    struct TranslationBlock *current_tb;
 129
 130    int kvm_fd;
 131    bool kvm_vcpu_dirty;
 132    struct KVMState *kvm_state;
 133    struct kvm_run *kvm_run;
 134
 135    /* TODO Move common fields from CPUArchState here. */
 136    int cpu_index; /* used by alpha TCG */
 137    uint32_t halted; /* used by alpha, cris, ppc TCG */
 138};
 139
 140/**
 141 * cpu_write_elf64_note:
 142 * @f: pointer to a function that writes memory to a file
 143 * @cpu: The CPU whose memory is to be dumped
 144 * @cpuid: ID number of the CPU
 145 * @opaque: pointer to the CPUState struct
 146 */
 147int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
 148                         int cpuid, void *opaque);
 149
 150/**
 151 * cpu_write_elf64_qemunote:
 152 * @f: pointer to a function that writes memory to a file
 153 * @cpu: The CPU whose memory is to be dumped
 154 * @cpuid: ID number of the CPU
 155 * @opaque: pointer to the CPUState struct
 156 */
 157int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
 158                             void *opaque);
 159
 160/**
 161 * cpu_write_elf32_note:
 162 * @f: pointer to a function that writes memory to a file
 163 * @cpu: The CPU whose memory is to be dumped
 164 * @cpuid: ID number of the CPU
 165 * @opaque: pointer to the CPUState struct
 166 */
 167int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
 168                         int cpuid, void *opaque);
 169
 170/**
 171 * cpu_write_elf32_qemunote:
 172 * @f: pointer to a function that writes memory to a file
 173 * @cpu: The CPU whose memory is to be dumped
 174 * @cpuid: ID number of the CPU
 175 * @opaque: pointer to the CPUState struct
 176 */
 177int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
 178                             void *opaque);
 179
 180/**
 181 * cpu_reset:
 182 * @cpu: The CPU whose state is to be reset.
 183 */
 184void cpu_reset(CPUState *cpu);
 185
 186/**
 187 * cpu_class_by_name:
 188 * @typename: The CPU base type.
 189 * @cpu_model: The model string without any parameters.
 190 *
 191 * Looks up a CPU #ObjectClass matching name @cpu_model.
 192 *
 193 * Returns: A #CPUClass or %NULL if not matching class is found.
 194 */
 195ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 196
 197/**
 198 * cpu_class_set_vmsd:
 199 * @cc: CPU class
 200 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
 201 *
 202 * Sets #VMStateDescription for @cc.
 203 *
 204 * The @value argument is intentionally discarded for the non-softmmu targets
 205 * to avoid linker errors or excessive preprocessor usage. If this behavior
 206 * is undesired, you should assign #CPUState.vmsd directly instead.
 207 */
 208#ifndef CONFIG_USER_ONLY
 209static inline void cpu_class_set_vmsd(CPUClass *cc,
 210                                      const struct VMStateDescription *value)
 211{
 212    cc->vmsd = value;
 213}
 214#else
 215#define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
 216#endif
 217
 218/**
 219 * qemu_cpu_has_work:
 220 * @cpu: The vCPU to check.
 221 *
 222 * Checks whether the CPU has work to do.
 223 *
 224 * Returns: %true if the CPU has work, %false otherwise.
 225 */
 226bool qemu_cpu_has_work(CPUState *cpu);
 227
 228/**
 229 * qemu_cpu_is_self:
 230 * @cpu: The vCPU to check against.
 231 *
 232 * Checks whether the caller is executing on the vCPU thread.
 233 *
 234 * Returns: %true if called from @cpu's thread, %false otherwise.
 235 */
 236bool qemu_cpu_is_self(CPUState *cpu);
 237
 238/**
 239 * qemu_cpu_kick:
 240 * @cpu: The vCPU to kick.
 241 *
 242 * Kicks @cpu's thread.
 243 */
 244void qemu_cpu_kick(CPUState *cpu);
 245
 246/**
 247 * cpu_is_stopped:
 248 * @cpu: The CPU to check.
 249 *
 250 * Checks whether the CPU is stopped.
 251 *
 252 * Returns: %true if run state is not running or if artificially stopped;
 253 * %false otherwise.
 254 */
 255bool cpu_is_stopped(CPUState *cpu);
 256
 257/**
 258 * run_on_cpu:
 259 * @cpu: The vCPU to run on.
 260 * @func: The function to be executed.
 261 * @data: Data to pass to the function.
 262 *
 263 * Schedules the function @func for execution on the vCPU @cpu.
 264 */
 265void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
 266
 267/**
 268 * qemu_for_each_cpu:
 269 * @func: The function to be executed.
 270 * @data: Data to pass to the function.
 271 *
 272 * Executes @func for each CPU.
 273 */
 274void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
 275
 276/**
 277 * qemu_get_cpu:
 278 * @index: The CPUState@cpu_index value of the CPU to obtain.
 279 *
 280 * Gets a CPU matching @index.
 281 *
 282 * Returns: The CPU or %NULL if there is no matching CPU.
 283 */
 284CPUState *qemu_get_cpu(int index);
 285
 286/**
 287 * cpu_exists:
 288 * @id: Guest-exposed CPU ID to lookup.
 289 *
 290 * Search for CPU with specified ID.
 291 *
 292 * Returns: %true - CPU is found, %false - CPU isn't found.
 293 */
 294bool cpu_exists(int64_t id);
 295
 296#ifndef CONFIG_USER_ONLY
 297
 298typedef void (*CPUInterruptHandler)(CPUState *, int);
 299
 300extern CPUInterruptHandler cpu_interrupt_handler;
 301
 302/**
 303 * cpu_interrupt:
 304 * @cpu: The CPU to set an interrupt on.
 305 * @mask: The interupts to set.
 306 *
 307 * Invokes the interrupt handler.
 308 */
 309static inline void cpu_interrupt(CPUState *cpu, int mask)
 310{
 311    cpu_interrupt_handler(cpu, mask);
 312}
 313
 314#else /* USER_ONLY */
 315
 316void cpu_interrupt(CPUState *cpu, int mask);
 317
 318#endif /* USER_ONLY */
 319
 320/**
 321 * cpu_reset_interrupt:
 322 * @cpu: The CPU to clear the interrupt on.
 323 * @mask: The interrupt mask to clear.
 324 *
 325 * Resets interrupts on the vCPU @cpu.
 326 */
 327void cpu_reset_interrupt(CPUState *cpu, int mask);
 328
 329/**
 330 * cpu_resume:
 331 * @cpu: The CPU to resume.
 332 *
 333 * Resumes CPU, i.e. puts CPU into runnable state.
 334 */
 335void cpu_resume(CPUState *cpu);
 336
 337#endif
 338