Merge remote-tracking branch 'remotes/afaerber/tags/qom-cpu-for-peter' into staging
[qemu/ar7.git] / target-s390x / cpu.c
blob97a92168a862e5a905a4459de2da95da1ce2cf95
1 /*
2 * QEMU S/390 CPU
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2011 Alexander Graf
6 * Copyright (c) 2012 SUSE LINUX Products GmbH
7 * Copyright (c) 2012 IBM Corp.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see
21 * <http://www.gnu.org/licenses/lgpl-2.1.html>
22 * Contributions after 2012-12-11 are licensed under the terms of the
23 * GNU GPL, version 2 or (at your option) any later version.
26 #include "cpu.h"
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
29 #include "hw/hw.h"
30 #ifndef CONFIG_USER_ONLY
31 #include "sysemu/arch_init.h"
32 #endif
34 #define CR0_RESET 0xE0UL
35 #define CR14_RESET 0xC2000000UL;
37 /* generate CPU information for cpu -? */
38 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
40 #ifdef CONFIG_KVM
41 (*cpu_fprintf)(f, "s390 %16s\n", "host");
42 #endif
45 #ifndef CONFIG_USER_ONLY
46 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
48 CpuDefinitionInfoList *entry;
49 CpuDefinitionInfo *info;
51 info = g_malloc0(sizeof(*info));
52 info->name = g_strdup("host");
54 entry = g_malloc0(sizeof(*entry));
55 entry->value = info;
57 return entry;
59 #endif
61 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
63 S390CPU *cpu = S390_CPU(cs);
65 cpu->env.psw.addr = value;
68 static bool s390_cpu_has_work(CPUState *cs)
70 S390CPU *cpu = S390_CPU(cs);
71 CPUS390XState *env = &cpu->env;
73 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
74 (env->psw.mask & PSW_MASK_EXT);
77 #if !defined(CONFIG_USER_ONLY)
78 /* S390CPUClass::load_normal() */
79 static void s390_cpu_load_normal(CPUState *s)
81 S390CPU *cpu = S390_CPU(s);
82 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
83 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
84 s390_add_running_cpu(cpu);
86 #endif
88 /* S390CPUClass::cpu_reset() */
89 static void s390_cpu_reset(CPUState *s)
91 S390CPU *cpu = S390_CPU(s);
92 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
93 CPUS390XState *env = &cpu->env;
95 env->pfault_token = -1UL;
96 s390_del_running_cpu(cpu);
97 scc->parent_reset(s);
98 #if !defined(CONFIG_USER_ONLY)
99 s->halted = 1;
100 #endif
101 tlb_flush(s, 1);
104 /* S390CPUClass::initial_reset() */
105 static void s390_cpu_initial_reset(CPUState *s)
107 S390CPU *cpu = S390_CPU(s);
108 CPUS390XState *env = &cpu->env;
110 s390_cpu_reset(s);
111 /* initial reset does not touch regs,fregs and aregs */
112 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
113 offsetof(CPUS390XState, fpc));
115 /* architectured initial values for CR 0 and 14 */
116 env->cregs[0] = CR0_RESET;
117 env->cregs[14] = CR14_RESET;
119 env->pfault_token = -1UL;
121 #if defined(CONFIG_KVM)
122 /* Reset state inside the kernel that we cannot access yet from QEMU. */
123 if (kvm_enabled()) {
124 if (kvm_vcpu_ioctl(s, KVM_S390_INITIAL_RESET, NULL)) {
125 perror("Initial CPU reset failed");
128 #endif
131 /* CPUClass:reset() */
132 static void s390_cpu_full_reset(CPUState *s)
134 S390CPU *cpu = S390_CPU(s);
135 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
136 CPUS390XState *env = &cpu->env;
138 s390_del_running_cpu(cpu);
140 scc->parent_reset(s);
142 memset(env, 0, offsetof(CPUS390XState, cpu_num));
144 /* architectured initial values for CR 0 and 14 */
145 env->cregs[0] = CR0_RESET;
146 env->cregs[14] = CR14_RESET;
148 env->pfault_token = -1UL;
150 /* set halted to 1 to make sure we can add the cpu in
151 * s390_ipl_cpu code, where CPUState::halted is set back to 0
152 * after incrementing the cpu counter */
153 #if !defined(CONFIG_USER_ONLY)
154 s->halted = 1;
156 if (kvm_enabled()) {
157 kvm_s390_reset_vcpu(cpu);
159 #endif
160 tlb_flush(s, 1);
163 #if !defined(CONFIG_USER_ONLY)
164 static void s390_cpu_machine_reset_cb(void *opaque)
166 S390CPU *cpu = opaque;
168 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
170 #endif
172 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
174 CPUState *cs = CPU(dev);
175 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
177 s390_cpu_gdb_init(cs);
178 qemu_init_vcpu(cs);
179 #if !defined(CONFIG_USER_ONLY)
180 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
181 #else
182 cpu_reset(cs);
183 #endif
185 scc->parent_realize(dev, errp);
188 static void s390_cpu_initfn(Object *obj)
190 CPUState *cs = CPU(obj);
191 S390CPU *cpu = S390_CPU(obj);
192 CPUS390XState *env = &cpu->env;
193 static bool inited;
194 static int cpu_num = 0;
195 #if !defined(CONFIG_USER_ONLY)
196 struct tm tm;
197 #endif
199 cs->env_ptr = env;
200 cpu_exec_init(env);
201 #if !defined(CONFIG_USER_ONLY)
202 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
203 qemu_get_timedate(&tm, 0);
204 env->tod_offset = TOD_UNIX_EPOCH +
205 (time2tod(mktimegm(&tm)) * 1000000000ULL);
206 env->tod_basetime = 0;
207 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
208 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
209 /* set CPUState::halted state to 1 to avoid decrementing the running
210 * cpu counter in s390_cpu_reset to a negative number at
211 * initial ipl */
212 cs->halted = 1;
213 #endif
214 env->cpu_num = cpu_num++;
215 env->ext_index = -1;
217 if (tcg_enabled() && !inited) {
218 inited = true;
219 s390x_translate_init();
223 static void s390_cpu_finalize(Object *obj)
225 #if !defined(CONFIG_USER_ONLY)
226 S390CPU *cpu = S390_CPU(obj);
228 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
229 #endif
232 static const VMStateDescription vmstate_s390_cpu = {
233 .name = "cpu",
234 .unmigratable = 1,
237 static void s390_cpu_class_init(ObjectClass *oc, void *data)
239 S390CPUClass *scc = S390_CPU_CLASS(oc);
240 CPUClass *cc = CPU_CLASS(scc);
241 DeviceClass *dc = DEVICE_CLASS(oc);
243 scc->parent_realize = dc->realize;
244 dc->realize = s390_cpu_realizefn;
246 scc->parent_reset = cc->reset;
247 #if !defined(CONFIG_USER_ONLY)
248 scc->load_normal = s390_cpu_load_normal;
249 #endif
250 scc->cpu_reset = s390_cpu_reset;
251 scc->initial_cpu_reset = s390_cpu_initial_reset;
252 cc->reset = s390_cpu_full_reset;
253 cc->has_work = s390_cpu_has_work;
254 cc->do_interrupt = s390_cpu_do_interrupt;
255 cc->dump_state = s390_cpu_dump_state;
256 cc->set_pc = s390_cpu_set_pc;
257 cc->gdb_read_register = s390_cpu_gdb_read_register;
258 cc->gdb_write_register = s390_cpu_gdb_write_register;
259 #ifdef CONFIG_USER_ONLY
260 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
261 #else
262 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
263 cc->write_elf64_note = s390_cpu_write_elf64_note;
264 cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote;
265 #endif
266 dc->vmsd = &vmstate_s390_cpu;
267 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
268 cc->gdb_core_xml_file = "s390x-core64.xml";
271 static const TypeInfo s390_cpu_type_info = {
272 .name = TYPE_S390_CPU,
273 .parent = TYPE_CPU,
274 .instance_size = sizeof(S390CPU),
275 .instance_init = s390_cpu_initfn,
276 .instance_finalize = s390_cpu_finalize,
277 .abstract = false,
278 .class_size = sizeof(S390CPUClass),
279 .class_init = s390_cpu_class_init,
282 static void s390_cpu_register_types(void)
284 type_register_static(&s390_cpu_type_info);
287 type_init(s390_cpu_register_types)