]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU S/390 CPU | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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. | |
24 | */ | |
25 | ||
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 | |
33 | ||
34 | #define CR0_RESET 0xE0UL | |
35 | #define CR14_RESET 0xC2000000UL; | |
36 | ||
37 | /* generate CPU information for cpu -? */ | |
38 | void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf) | |
39 | { | |
40 | #ifdef CONFIG_KVM | |
41 | (*cpu_fprintf)(f, "s390 %16s\n", "host"); | |
42 | #endif | |
43 | } | |
44 | ||
45 | #ifndef CONFIG_USER_ONLY | |
46 | CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) | |
47 | { | |
48 | CpuDefinitionInfoList *entry; | |
49 | CpuDefinitionInfo *info; | |
50 | ||
51 | info = g_malloc0(sizeof(*info)); | |
52 | info->name = g_strdup("host"); | |
53 | ||
54 | entry = g_malloc0(sizeof(*entry)); | |
55 | entry->value = info; | |
56 | ||
57 | return entry; | |
58 | } | |
59 | #endif | |
60 | ||
61 | static void s390_cpu_set_pc(CPUState *cs, vaddr value) | |
62 | { | |
63 | S390CPU *cpu = S390_CPU(cs); | |
64 | ||
65 | cpu->env.psw.addr = value; | |
66 | } | |
67 | ||
68 | #if !defined(CONFIG_USER_ONLY) | |
69 | /* S390CPUClass::load_normal() */ | |
70 | static void s390_cpu_load_normal(CPUState *s) | |
71 | { | |
72 | S390CPU *cpu = S390_CPU(s); | |
73 | cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR; | |
74 | cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64; | |
75 | s390_add_running_cpu(cpu); | |
76 | } | |
77 | #endif | |
78 | ||
79 | /* S390CPUClass::cpu_reset() */ | |
80 | static void s390_cpu_reset(CPUState *s) | |
81 | { | |
82 | S390CPU *cpu = S390_CPU(s); | |
83 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
84 | CPUS390XState *env = &cpu->env; | |
85 | ||
86 | env->pfault_token = -1UL; | |
87 | s390_del_running_cpu(cpu); | |
88 | scc->parent_reset(s); | |
89 | #if !defined(CONFIG_USER_ONLY) | |
90 | s->halted = 1; | |
91 | #endif | |
92 | tlb_flush(env, 1); | |
93 | } | |
94 | ||
95 | /* S390CPUClass::initial_reset() */ | |
96 | static void s390_cpu_initial_reset(CPUState *s) | |
97 | { | |
98 | S390CPU *cpu = S390_CPU(s); | |
99 | CPUS390XState *env = &cpu->env; | |
100 | ||
101 | s390_cpu_reset(s); | |
102 | /* initial reset does not touch regs,fregs and aregs */ | |
103 | memset(&env->fpc, 0, offsetof(CPUS390XState, breakpoints) - | |
104 | offsetof(CPUS390XState, fpc)); | |
105 | ||
106 | /* architectured initial values for CR 0 and 14 */ | |
107 | env->cregs[0] = CR0_RESET; | |
108 | env->cregs[14] = CR14_RESET; | |
109 | ||
110 | env->pfault_token = -1UL; | |
111 | ||
112 | #if defined(CONFIG_KVM) | |
113 | /* Reset state inside the kernel that we cannot access yet from QEMU. */ | |
114 | if (kvm_enabled()) { | |
115 | if (kvm_vcpu_ioctl(s, KVM_S390_INITIAL_RESET, NULL)) { | |
116 | perror("Initial CPU reset failed"); | |
117 | } | |
118 | } | |
119 | #endif | |
120 | } | |
121 | ||
122 | /* CPUClass:reset() */ | |
123 | static void s390_cpu_full_reset(CPUState *s) | |
124 | { | |
125 | S390CPU *cpu = S390_CPU(s); | |
126 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
127 | CPUS390XState *env = &cpu->env; | |
128 | ||
129 | s390_del_running_cpu(cpu); | |
130 | ||
131 | scc->parent_reset(s); | |
132 | ||
133 | memset(env, 0, offsetof(CPUS390XState, breakpoints)); | |
134 | ||
135 | /* architectured initial values for CR 0 and 14 */ | |
136 | env->cregs[0] = CR0_RESET; | |
137 | env->cregs[14] = CR14_RESET; | |
138 | ||
139 | env->pfault_token = -1UL; | |
140 | ||
141 | /* set halted to 1 to make sure we can add the cpu in | |
142 | * s390_ipl_cpu code, where CPUState::halted is set back to 0 | |
143 | * after incrementing the cpu counter */ | |
144 | #if !defined(CONFIG_USER_ONLY) | |
145 | s->halted = 1; | |
146 | #endif | |
147 | tlb_flush(env, 1); | |
148 | } | |
149 | ||
150 | #if !defined(CONFIG_USER_ONLY) | |
151 | static void s390_cpu_machine_reset_cb(void *opaque) | |
152 | { | |
153 | S390CPU *cpu = opaque; | |
154 | ||
155 | cpu_reset(CPU(cpu)); | |
156 | } | |
157 | #endif | |
158 | ||
159 | static void s390_cpu_realizefn(DeviceState *dev, Error **errp) | |
160 | { | |
161 | CPUState *cs = CPU(dev); | |
162 | S390CPUClass *scc = S390_CPU_GET_CLASS(dev); | |
163 | ||
164 | qemu_init_vcpu(cs); | |
165 | cpu_reset(cs); | |
166 | ||
167 | scc->parent_realize(dev, errp); | |
168 | } | |
169 | ||
170 | static void s390_cpu_initfn(Object *obj) | |
171 | { | |
172 | CPUState *cs = CPU(obj); | |
173 | S390CPU *cpu = S390_CPU(obj); | |
174 | CPUS390XState *env = &cpu->env; | |
175 | static bool inited; | |
176 | static int cpu_num = 0; | |
177 | #if !defined(CONFIG_USER_ONLY) | |
178 | struct tm tm; | |
179 | #endif | |
180 | ||
181 | cs->env_ptr = env; | |
182 | cpu_exec_init(env); | |
183 | #if !defined(CONFIG_USER_ONLY) | |
184 | qemu_register_reset(s390_cpu_machine_reset_cb, cpu); | |
185 | qemu_get_timedate(&tm, 0); | |
186 | env->tod_offset = TOD_UNIX_EPOCH + | |
187 | (time2tod(mktimegm(&tm)) * 1000000000ULL); | |
188 | env->tod_basetime = 0; | |
189 | env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu); | |
190 | env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu); | |
191 | /* set CPUState::halted state to 1 to avoid decrementing the running | |
192 | * cpu counter in s390_cpu_reset to a negative number at | |
193 | * initial ipl */ | |
194 | cs->halted = 1; | |
195 | #endif | |
196 | env->cpu_num = cpu_num++; | |
197 | env->ext_index = -1; | |
198 | ||
199 | if (tcg_enabled() && !inited) { | |
200 | inited = true; | |
201 | s390x_translate_init(); | |
202 | } | |
203 | } | |
204 | ||
205 | static void s390_cpu_finalize(Object *obj) | |
206 | { | |
207 | #if !defined(CONFIG_USER_ONLY) | |
208 | S390CPU *cpu = S390_CPU(obj); | |
209 | ||
210 | qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu); | |
211 | #endif | |
212 | } | |
213 | ||
214 | static const VMStateDescription vmstate_s390_cpu = { | |
215 | .name = "cpu", | |
216 | .unmigratable = 1, | |
217 | }; | |
218 | ||
219 | static void s390_cpu_class_init(ObjectClass *oc, void *data) | |
220 | { | |
221 | S390CPUClass *scc = S390_CPU_CLASS(oc); | |
222 | CPUClass *cc = CPU_CLASS(scc); | |
223 | DeviceClass *dc = DEVICE_CLASS(oc); | |
224 | ||
225 | scc->parent_realize = dc->realize; | |
226 | dc->realize = s390_cpu_realizefn; | |
227 | ||
228 | scc->parent_reset = cc->reset; | |
229 | #if !defined(CONFIG_USER_ONLY) | |
230 | scc->load_normal = s390_cpu_load_normal; | |
231 | #endif | |
232 | scc->cpu_reset = s390_cpu_reset; | |
233 | scc->initial_cpu_reset = s390_cpu_initial_reset; | |
234 | cc->reset = s390_cpu_full_reset; | |
235 | cc->do_interrupt = s390_cpu_do_interrupt; | |
236 | cc->dump_state = s390_cpu_dump_state; | |
237 | cc->set_pc = s390_cpu_set_pc; | |
238 | cc->gdb_read_register = s390_cpu_gdb_read_register; | |
239 | cc->gdb_write_register = s390_cpu_gdb_write_register; | |
240 | #ifndef CONFIG_USER_ONLY | |
241 | cc->get_phys_page_debug = s390_cpu_get_phys_page_debug; | |
242 | cc->write_elf64_note = s390_cpu_write_elf64_note; | |
243 | cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote; | |
244 | #endif | |
245 | dc->vmsd = &vmstate_s390_cpu; | |
246 | cc->gdb_num_core_regs = S390_NUM_REGS; | |
247 | } | |
248 | ||
249 | static const TypeInfo s390_cpu_type_info = { | |
250 | .name = TYPE_S390_CPU, | |
251 | .parent = TYPE_CPU, | |
252 | .instance_size = sizeof(S390CPU), | |
253 | .instance_init = s390_cpu_initfn, | |
254 | .instance_finalize = s390_cpu_finalize, | |
255 | .abstract = false, | |
256 | .class_size = sizeof(S390CPUClass), | |
257 | .class_init = s390_cpu_class_init, | |
258 | }; | |
259 | ||
260 | static void s390_cpu_register_types(void) | |
261 | { | |
262 | type_register_static(&s390_cpu_type_info); | |
263 | } | |
264 | ||
265 | type_init(s390_cpu_register_types) |