• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "exec/hwaddr.h"
26 #include "qemu/queue.h"
27 #include "qemu/thread.h"
28 #include "qemu/tls.h"
29 #include "qemu/typedefs.h"
30 
31 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
32 
33 /**
34  * vaddr:
35  * Type wide enough to contain any #target_ulong virtual address.
36  */
37 typedef uint64_t vaddr;
38 #define VADDR_PRId PRId64
39 #define VADDR_PRIu PRIu64
40 #define VADDR_PRIo PRIo64
41 #define VADDR_PRIx PRIx64
42 #define VADDR_PRIX PRIX64
43 #define VADDR_MAX UINT64_MAX
44 
45 typedef struct CPUState CPUState;
46 
47 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
48                                     bool is_write, bool is_exec, int opaque,
49                                     unsigned size);
50 
51 struct TranslationBlock;
52 
53 // TODO(digit): Make this a proper QOM object that inherits from
54 // DeviceState/DeviceClass.
55 struct CPUState {
56     int nr_cores;
57     int nr_threads;
58     int numa_node;
59 
60     struct QemuThread *thread;
61 
62     uint32_t host_tid; /* host thread ID */
63     int running; /* Nonzero if cpu is currently running(usermode).  */
64     struct QemuCond *halt_cond;
65     struct qemu_work_item *queued_work_first, *queued_work_last;
66 
67     uint32_t created;
68     uint32_t stop;   /* Stop request */
69     uint32_t stopped; /* Artificially stopped */
70 
71     volatile sig_atomic_t exit_request;
72     uint32_t interrupt_request;
73 
74     void *env_ptr; /* CPUArchState */
75     struct TranslationBlock *current_tb; /* currently executing TB  */
76     int singlestep_enabled;
77     struct GDBRegisterState *gdb_regs;
78     QTAILQ_ENTRY(CPUState) node;   /* next CPU sharing TB cache */
79 
80     const char *cpu_model_str;
81 
82     int kvm_fd;
83     int kvm_vcpu_dirty;
84     struct KVMState *kvm_state;
85     struct kvm_run *kvm_run;
86 
87     struct hax_vcpu_state *hax_vcpu;
88 
89     /* TODO Move common fields from CPUArchState here. */
90     int cpu_index; /* used by alpha TCG */
91     uint32_t halted; /* used by alpha, cris, ppc TCG */
92 };
93 
94 #define CPU(obj)  ((CPUState*)(obj))
95 
96 QTAILQ_HEAD(CPUTailQ, CPUState);
97 extern struct CPUTailQ cpus;
98 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
99 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
100 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
101     QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
102 #define first_cpu QTAILQ_FIRST(&cpus)
103 
104 DECLARE_TLS(CPUState *, current_cpu);
105 #define current_cpu tls_var(current_cpu)
106 
107 // TODO(digit): Remove this.
108 #define cpu_single_env ((CPUArchState*)current_cpu->env_ptr)
109 
110 /**
111  * CPUDumpFlags:
112  * @CPU_DUMP_CODE:
113  * @CPU_DUMP_FPU: dump FPU register state, not just integer
114  * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
115  */
116 enum CPUDumpFlags {
117     CPU_DUMP_CODE = 0x00010000,
118     CPU_DUMP_FPU  = 0x00020000,
119     CPU_DUMP_CCOP = 0x00040000,
120 };
121 
122 /**
123  * cpu_dump_state:
124  * @cpu: The CPU whose state is to be dumped.
125  * @f: File to dump to.
126  * @cpu_fprintf: Function to dump with.
127  * @flags: Flags what to dump.
128  *
129  * Dumps CPU state.
130  */
131 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
132                     int flags);
133 
134 /**
135  * cpu_dump_statistics:
136  * @cpu: The CPU whose state is to be dumped.
137  * @f: File to dump to.
138  * @cpu_fprintf: Function to dump with.
139  * @flags: Flags what to dump.
140  *
141  * Dumps CPU statistics.
142  */
143 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
144                          int flags);
145 
146 /**
147  * cpu_reset:
148  * @cpu: The CPU whose state is to be reset.
149  */
150 void cpu_reset(CPUState *cpu);
151 
152 /**
153  * qemu_cpu_has_work:
154  * @cpu: The vCPU to check.
155  *
156  * Checks whether the CPU has work to do.
157  *
158  * Returns: %true if the CPU has work, %false otherwise.
159  */
160 bool qemu_cpu_has_work(CPUState *cpu);
161 
162 /**
163  * qemu_cpu_is_self:
164  * @cpu: The vCPU to check against.
165  *
166  * Checks whether the caller is executing on the vCPU thread.
167  *
168  * Returns: %true if called from @cpu's thread, %false otherwise.
169  */
170 bool qemu_cpu_is_self(CPUState *cpu);
171 
172 /**
173  * qemu_cpu_kick:
174  * @cpu: The vCPU to kick.
175  *
176  * Kicks @cpu's thread.
177  */
178 void qemu_cpu_kick(CPUState *cpu);
179 
180 /**
181  * cpu_is_stopped:
182  * @cpu: The CPU to check.
183  *
184  * Checks whether the CPU is stopped.
185  *
186  * Returns: %true if run state is not running or if artificially stopped;
187  * %false otherwise.
188  */
189 bool cpu_is_stopped(CPUState *cpu);
190 
191 /**
192  * run_on_cpu:
193  * @cpu: The vCPU to run on.
194  * @func: The function to be executed.
195  * @data: Data to pass to the function.
196  *
197  * Schedules the function @func for execution on the vCPU @cpu.
198  */
199 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
200 
201 /**
202  * qemu_get_cpu:
203  * @index: The CPUState@cpu_index value of the CPU to obtain.
204  *
205  * Gets a CPU matching @index.
206  *
207  * Returns: The CPU or %NULL if there is no matching CPU.
208  */
209 CPUState *qemu_get_cpu(int index);
210 
211 /**
212  * cpu_interrupt:
213  * @cpu: The CPU to set an interrupt on.
214  * @mask: The interupts to set.
215  *
216  * Invokes the interrupt handler.
217  */
218 void cpu_interrupt(CPUState *cpu, int mask);
219 
220 /**
221  * cpu_reset_interrupt:
222  * @cpu: The CPU to clear the interrupt on.
223  * @mask: The interrupt mask to clear.
224  *
225  * Resets interrupts on the vCPU @cpu.
226  */
227 void cpu_reset_interrupt(CPUState *cpu, int mask);
228 
229 /**
230  * cpu_exit:
231  * @cpu: The CPU to exit.
232  *
233  * Requests the CPU @cpu to exit execution.
234  */
235 void cpu_exit(CPUState *cpu);
236 
237 /**
238  * cpu_resume:
239  * @cpu: The CPU to resume.
240  *
241  * Resumes CPU, i.e. puts CPU into runnable state.
242  */
243 void cpu_resume(CPUState *cpu);
244 
245 /**
246  * qemu_init_vcpu:
247  * @cpu: The vCPU to initialize.
248  *
249  * Initializes a vCPU.
250  */
251 void qemu_init_vcpu(CPUState *cpu);
252 
253 #define SSTEP_ENABLE  0x1  /* Enable simulated HW single stepping */
254 #define SSTEP_NOIRQ   0x2  /* Do not use IRQ while single stepping */
255 #define SSTEP_NOTIMER 0x4  /* Do not Timers while single stepping */
256 
257 /**
258  * cpu_single_step:
259  * @cpu: CPU to the flags for.
260  * @enabled: Flags to enable.
261  *
262  * Enables or disables single-stepping for @cpu.
263  */
264 void cpu_single_step(CPUState *cpu, int enabled);
265 
266 #endif  // QEMU_CPU_H
267