]>
Commit | Line | Data |
---|---|---|
10ec5117 | 1 | /* |
aea1e885 | 2 | * S/390 misc helper routines |
10ec5117 | 3 | * |
defb0e31 | 4 | * Copyright (c) 2009 Ulrich Hecht |
10ec5117 AG |
5 | * Copyright (c) 2009 Alexander Graf |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
70539e18 | 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
10ec5117 AG |
19 | */ |
20 | ||
3e457172 | 21 | #include "cpu.h" |
022c62cb | 22 | #include "exec/memory.h" |
1de7afc9 | 23 | #include "qemu/host-utils.h" |
2ef6175a | 24 | #include "exec/helper-proto.h" |
defb0e31 | 25 | #include <string.h> |
9c17d615 | 26 | #include "sysemu/kvm.h" |
1de7afc9 | 27 | #include "qemu/timer.h" |
af2be207 JK |
28 | #ifdef CONFIG_KVM |
29 | #include <linux/kvm.h> | |
30 | #endif | |
f08b6170 | 31 | #include "exec/cpu_ldst.h" |
10ec5117 | 32 | |
71e47088 | 33 | #if !defined(CONFIG_USER_ONLY) |
f0778475 | 34 | #include "sysemu/cpus.h" |
9c17d615 | 35 | #include "sysemu/sysemu.h" |
40fa5264 | 36 | #include "hw/s390x/ebcdic.h" |
10ec5117 | 37 | #endif |
d5a43964 | 38 | |
defb0e31 AG |
39 | /* #define DEBUG_HELPER */ |
40 | #ifdef DEBUG_HELPER | |
41 | #define HELPER_LOG(x...) qemu_log(x) | |
42 | #else | |
43 | #define HELPER_LOG(x...) | |
44 | #endif | |
45 | ||
b4e2bd35 RH |
46 | /* Raise an exception dynamically from a helper function. */ |
47 | void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp, | |
48 | uintptr_t retaddr) | |
49 | { | |
27103424 | 50 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
b4e2bd35 RH |
51 | int t; |
52 | ||
27103424 | 53 | cs->exception_index = EXCP_PGM; |
b4e2bd35 RH |
54 | env->int_pgm_code = excp; |
55 | ||
56 | /* Use the (ultimate) callers address to find the insn that trapped. */ | |
3f38f309 | 57 | cpu_restore_state(cs, retaddr); |
b4e2bd35 RH |
58 | |
59 | /* Advance past the insn. */ | |
60 | t = cpu_ldub_code(env, env->psw.addr); | |
61 | env->int_pgm_ilen = t = get_ilen(t); | |
62 | env->psw.addr += 2 * t; | |
63 | ||
5638d180 | 64 | cpu_loop_exit(cs); |
b4e2bd35 RH |
65 | } |
66 | ||
d5a103cd | 67 | /* Raise an exception statically from a TB. */ |
089f5c06 | 68 | void HELPER(exception)(CPUS390XState *env, uint32_t excp) |
defb0e31 | 69 | { |
27103424 AF |
70 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
71 | ||
71e47088 | 72 | HELPER_LOG("%s: exception %d\n", __func__, excp); |
27103424 | 73 | cs->exception_index = excp; |
5638d180 | 74 | cpu_loop_exit(cs); |
defb0e31 AG |
75 | } |
76 | ||
defb0e31 | 77 | #ifndef CONFIG_USER_ONLY |
a158986d | 78 | |
d5a103cd | 79 | void program_interrupt(CPUS390XState *env, uint32_t code, int ilen) |
defb0e31 | 80 | { |
27103424 AF |
81 | S390CPU *cpu = s390_env_get_cpu(env); |
82 | ||
0d404541 RH |
83 | qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n", |
84 | env->psw.addr); | |
defb0e31 AG |
85 | |
86 | if (kvm_enabled()) { | |
af2be207 | 87 | #ifdef CONFIG_KVM |
27103424 | 88 | kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code); |
af2be207 | 89 | #endif |
defb0e31 | 90 | } else { |
27103424 AF |
91 | CPUState *cs = CPU(cpu); |
92 | ||
defb0e31 | 93 | env->int_pgm_code = code; |
d5a103cd | 94 | env->int_pgm_ilen = ilen; |
27103424 | 95 | cs->exception_index = EXCP_PGM; |
5638d180 | 96 | cpu_loop_exit(cs); |
defb0e31 AG |
97 | } |
98 | } | |
99 | ||
defb0e31 | 100 | /* SCLP service call */ |
dc458df9 | 101 | uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2) |
defb0e31 | 102 | { |
6e252802 | 103 | int r = sclp_service_call(env, r1, r2); |
9abf567d CB |
104 | if (r < 0) { |
105 | program_interrupt(env, -r, 4); | |
106 | return 0; | |
107 | } | |
108 | return r; | |
defb0e31 AG |
109 | } |
110 | ||
268846ba | 111 | #ifndef CONFIG_USER_ONLY |
f0778475 CB |
112 | static void cpu_reset_all(void) |
113 | { | |
bdc44640 | 114 | CPUState *cs; |
f0778475 CB |
115 | S390CPUClass *scc; |
116 | ||
bdc44640 AF |
117 | CPU_FOREACH(cs) { |
118 | scc = S390_CPU_GET_CLASS(cs); | |
119 | scc->cpu_reset(cs); | |
f0778475 CB |
120 | } |
121 | } | |
122 | ||
d8b30c83 CB |
123 | static void cpu_full_reset_all(void) |
124 | { | |
125 | CPUState *cpu; | |
126 | ||
127 | CPU_FOREACH(cpu) { | |
128 | cpu_reset(cpu); | |
129 | } | |
130 | } | |
131 | ||
132 | static int modified_clear_reset(S390CPU *cpu) | |
133 | { | |
134 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
135 | ||
136 | pause_all_vcpus(); | |
137 | cpu_synchronize_all_states(); | |
138 | cpu_full_reset_all(); | |
139 | io_subsystem_reset(); | |
140 | scc->load_normal(CPU(cpu)); | |
141 | cpu_synchronize_all_post_reset(); | |
142 | resume_all_vcpus(); | |
143 | return 0; | |
144 | } | |
145 | ||
f0778475 CB |
146 | static int load_normal_reset(S390CPU *cpu) |
147 | { | |
148 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
149 | ||
150 | pause_all_vcpus(); | |
151 | cpu_synchronize_all_states(); | |
152 | cpu_reset_all(); | |
153 | io_subsystem_reset(); | |
154 | scc->initial_cpu_reset(CPU(cpu)); | |
155 | scc->load_normal(CPU(cpu)); | |
156 | cpu_synchronize_all_post_reset(); | |
157 | resume_all_vcpus(); | |
158 | return 0; | |
159 | } | |
160 | ||
268846ba ED |
161 | #define DIAG_308_RC_NO_CONF 0x0102 |
162 | #define DIAG_308_RC_INVALID 0x0402 | |
163 | void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3) | |
164 | { | |
165 | uint64_t addr = env->regs[r1]; | |
166 | uint64_t subcode = env->regs[r3]; | |
167 | ||
168 | if (env->psw.mask & PSW_MASK_PSTATE) { | |
169 | program_interrupt(env, PGM_PRIVILEGED, ILEN_LATER_INC); | |
170 | return; | |
171 | } | |
172 | ||
173 | if ((subcode & ~0x0ffffULL) || (subcode > 6)) { | |
174 | program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC); | |
175 | return; | |
176 | } | |
177 | ||
178 | switch (subcode) { | |
d8b30c83 CB |
179 | case 0: |
180 | modified_clear_reset(s390_env_get_cpu(env)); | |
181 | break; | |
f0778475 CB |
182 | case 1: |
183 | load_normal_reset(s390_env_get_cpu(env)); | |
184 | break; | |
268846ba ED |
185 | case 5: |
186 | if ((r1 & 1) || (addr & 0x0fffULL)) { | |
187 | program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC); | |
188 | return; | |
189 | } | |
190 | env->regs[r1+1] = DIAG_308_RC_INVALID; | |
191 | return; | |
192 | case 6: | |
193 | if ((r1 & 1) || (addr & 0x0fffULL)) { | |
194 | program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC); | |
195 | return; | |
196 | } | |
197 | env->regs[r1+1] = DIAG_308_RC_NO_CONF; | |
198 | return; | |
199 | default: | |
200 | hw_error("Unhandled diag308 subcode %" PRIx64, subcode); | |
201 | break; | |
202 | } | |
203 | } | |
204 | #endif | |
205 | ||
defb0e31 | 206 | /* DIAG */ |
089f5c06 BS |
207 | uint64_t HELPER(diag)(CPUS390XState *env, uint32_t num, uint64_t mem, |
208 | uint64_t code) | |
defb0e31 AG |
209 | { |
210 | uint64_t r; | |
211 | ||
212 | switch (num) { | |
213 | case 0x500: | |
214 | /* KVM hypercall */ | |
28e942f8 | 215 | r = s390_virtio_hypercall(env); |
defb0e31 AG |
216 | break; |
217 | case 0x44: | |
218 | /* yield */ | |
219 | r = 0; | |
220 | break; | |
221 | case 0x308: | |
222 | /* ipl */ | |
223 | r = 0; | |
224 | break; | |
225 | default: | |
226 | r = -1; | |
227 | break; | |
228 | } | |
229 | ||
230 | if (r) { | |
d5a103cd | 231 | program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC); |
defb0e31 AG |
232 | } |
233 | ||
234 | return r; | |
235 | } | |
236 | ||
defb0e31 | 237 | /* Set Prefix */ |
089f5c06 | 238 | void HELPER(spx)(CPUS390XState *env, uint64_t a1) |
defb0e31 | 239 | { |
31b030d4 | 240 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
e805a0d3 | 241 | uint32_t prefix = a1 & 0x7fffe000; |
31b030d4 | 242 | |
e805a0d3 | 243 | env->psa = prefix; |
defb0e31 | 244 | qemu_log("prefix: %#x\n", prefix); |
31b030d4 AF |
245 | tlb_flush_page(cs, 0); |
246 | tlb_flush_page(cs, TARGET_PAGE_SIZE); | |
defb0e31 AG |
247 | } |
248 | ||
a4e3ad19 | 249 | static inline uint64_t clock_value(CPUS390XState *env) |
defb0e31 AG |
250 | { |
251 | uint64_t time; | |
252 | ||
253 | time = env->tod_offset + | |
bc72ad67 | 254 | time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime); |
defb0e31 AG |
255 | |
256 | return time; | |
257 | } | |
258 | ||
259 | /* Store Clock */ | |
434c91a5 | 260 | uint64_t HELPER(stck)(CPUS390XState *env) |
defb0e31 | 261 | { |
434c91a5 | 262 | return clock_value(env); |
defb0e31 AG |
263 | } |
264 | ||
defb0e31 | 265 | /* Set Clock Comparator */ |
dd3eb7b5 | 266 | void HELPER(sckc)(CPUS390XState *env, uint64_t time) |
defb0e31 | 267 | { |
defb0e31 AG |
268 | if (time == -1ULL) { |
269 | return; | |
270 | } | |
271 | ||
272 | /* difference between now and then */ | |
273 | time -= clock_value(env); | |
274 | /* nanoseconds */ | |
275 | time = (time * 125) >> 9; | |
276 | ||
bc72ad67 | 277 | timer_mod(env->tod_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time); |
defb0e31 AG |
278 | } |
279 | ||
280 | /* Store Clock Comparator */ | |
dd3eb7b5 | 281 | uint64_t HELPER(stckc)(CPUS390XState *env) |
defb0e31 AG |
282 | { |
283 | /* XXX implement */ | |
dd3eb7b5 | 284 | return 0; |
defb0e31 AG |
285 | } |
286 | ||
287 | /* Set CPU Timer */ | |
c4f0a863 | 288 | void HELPER(spt)(CPUS390XState *env, uint64_t time) |
defb0e31 | 289 | { |
defb0e31 AG |
290 | if (time == -1ULL) { |
291 | return; | |
292 | } | |
293 | ||
294 | /* nanoseconds */ | |
295 | time = (time * 125) >> 9; | |
296 | ||
bc72ad67 | 297 | timer_mod(env->cpu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time); |
defb0e31 AG |
298 | } |
299 | ||
300 | /* Store CPU Timer */ | |
c4f0a863 | 301 | uint64_t HELPER(stpt)(CPUS390XState *env) |
defb0e31 AG |
302 | { |
303 | /* XXX implement */ | |
c4f0a863 | 304 | return 0; |
defb0e31 AG |
305 | } |
306 | ||
307 | /* Store System Information */ | |
d14b3e09 RH |
308 | uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, |
309 | uint64_t r0, uint64_t r1) | |
defb0e31 AG |
310 | { |
311 | int cc = 0; | |
312 | int sel1, sel2; | |
313 | ||
314 | if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 && | |
315 | ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) { | |
316 | /* valid function code, invalid reserved bits */ | |
317 | program_interrupt(env, PGM_SPECIFICATION, 2); | |
318 | } | |
319 | ||
320 | sel1 = r0 & STSI_R0_SEL1_MASK; | |
321 | sel2 = r1 & STSI_R1_SEL2_MASK; | |
322 | ||
323 | /* XXX: spec exception if sysib is not 4k-aligned */ | |
324 | ||
325 | switch (r0 & STSI_LEVEL_MASK) { | |
326 | case STSI_LEVEL_1: | |
327 | if ((sel1 == 1) && (sel2 == 1)) { | |
328 | /* Basic Machine Configuration */ | |
329 | struct sysib_111 sysib; | |
330 | ||
331 | memset(&sysib, 0, sizeof(sysib)); | |
332 | ebcdic_put(sysib.manuf, "QEMU ", 16); | |
333 | /* same as machine type number in STORE CPU ID */ | |
334 | ebcdic_put(sysib.type, "QEMU", 4); | |
335 | /* same as model number in STORE CPU ID */ | |
336 | ebcdic_put(sysib.model, "QEMU ", 16); | |
337 | ebcdic_put(sysib.sequence, "QEMU ", 16); | |
338 | ebcdic_put(sysib.plant, "QEMU", 4); | |
eb6282f2 | 339 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
340 | } else if ((sel1 == 2) && (sel2 == 1)) { |
341 | /* Basic Machine CPU */ | |
342 | struct sysib_121 sysib; | |
343 | ||
344 | memset(&sysib, 0, sizeof(sysib)); | |
345 | /* XXX make different for different CPUs? */ | |
346 | ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16); | |
347 | ebcdic_put(sysib.plant, "QEMU", 4); | |
348 | stw_p(&sysib.cpu_addr, env->cpu_num); | |
eb6282f2 | 349 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
350 | } else if ((sel1 == 2) && (sel2 == 2)) { |
351 | /* Basic Machine CPUs */ | |
352 | struct sysib_122 sysib; | |
353 | ||
354 | memset(&sysib, 0, sizeof(sysib)); | |
355 | stl_p(&sysib.capability, 0x443afc29); | |
356 | /* XXX change when SMP comes */ | |
357 | stw_p(&sysib.total_cpus, 1); | |
358 | stw_p(&sysib.active_cpus, 1); | |
359 | stw_p(&sysib.standby_cpus, 0); | |
360 | stw_p(&sysib.reserved_cpus, 0); | |
eb6282f2 | 361 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
362 | } else { |
363 | cc = 3; | |
364 | } | |
365 | break; | |
366 | case STSI_LEVEL_2: | |
71e47088 BS |
367 | { |
368 | if ((sel1 == 2) && (sel2 == 1)) { | |
369 | /* LPAR CPU */ | |
370 | struct sysib_221 sysib; | |
371 | ||
372 | memset(&sysib, 0, sizeof(sysib)); | |
373 | /* XXX make different for different CPUs? */ | |
374 | ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16); | |
375 | ebcdic_put(sysib.plant, "QEMU", 4); | |
376 | stw_p(&sysib.cpu_addr, env->cpu_num); | |
377 | stw_p(&sysib.cpu_id, 0); | |
eb6282f2 | 378 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
379 | } else if ((sel1 == 2) && (sel2 == 2)) { |
380 | /* LPAR CPUs */ | |
381 | struct sysib_222 sysib; | |
382 | ||
383 | memset(&sysib, 0, sizeof(sysib)); | |
384 | stw_p(&sysib.lpar_num, 0); | |
385 | sysib.lcpuc = 0; | |
386 | /* XXX change when SMP comes */ | |
387 | stw_p(&sysib.total_cpus, 1); | |
388 | stw_p(&sysib.conf_cpus, 1); | |
389 | stw_p(&sysib.standby_cpus, 0); | |
390 | stw_p(&sysib.reserved_cpus, 0); | |
391 | ebcdic_put(sysib.name, "QEMU ", 8); | |
392 | stl_p(&sysib.caf, 1000); | |
393 | stw_p(&sysib.dedicated_cpus, 0); | |
394 | stw_p(&sysib.shared_cpus, 0); | |
eb6282f2 | 395 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
396 | } else { |
397 | cc = 3; | |
398 | } | |
399 | break; | |
defb0e31 | 400 | } |
defb0e31 | 401 | case STSI_LEVEL_3: |
71e47088 BS |
402 | { |
403 | if ((sel1 == 2) && (sel2 == 2)) { | |
404 | /* VM CPUs */ | |
405 | struct sysib_322 sysib; | |
406 | ||
407 | memset(&sysib, 0, sizeof(sysib)); | |
408 | sysib.count = 1; | |
409 | /* XXX change when SMP comes */ | |
410 | stw_p(&sysib.vm[0].total_cpus, 1); | |
411 | stw_p(&sysib.vm[0].conf_cpus, 1); | |
412 | stw_p(&sysib.vm[0].standby_cpus, 0); | |
413 | stw_p(&sysib.vm[0].reserved_cpus, 0); | |
414 | ebcdic_put(sysib.vm[0].name, "KVMguest", 8); | |
415 | stl_p(&sysib.vm[0].caf, 1000); | |
416 | ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16); | |
eb6282f2 | 417 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
418 | } else { |
419 | cc = 3; | |
420 | } | |
421 | break; | |
defb0e31 | 422 | } |
defb0e31 AG |
423 | case STSI_LEVEL_CURRENT: |
424 | env->regs[0] = STSI_LEVEL_3; | |
425 | break; | |
426 | default: | |
427 | cc = 3; | |
428 | break; | |
429 | } | |
430 | ||
431 | return cc; | |
432 | } | |
433 | ||
089f5c06 BS |
434 | uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1, |
435 | uint64_t cpu_addr) | |
defb0e31 AG |
436 | { |
437 | int cc = 0; | |
438 | ||
439 | HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n", | |
71e47088 | 440 | __func__, order_code, r1, cpu_addr); |
defb0e31 | 441 | |
71e47088 | 442 | /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register" |
defb0e31 AG |
443 | as parameter (input). Status (output) is always R1. */ |
444 | ||
445 | switch (order_code) { | |
446 | case SIGP_SET_ARCH: | |
447 | /* switch arch */ | |
448 | break; | |
449 | case SIGP_SENSE: | |
450 | /* enumerate CPU status */ | |
451 | if (cpu_addr) { | |
452 | /* XXX implement when SMP comes */ | |
453 | return 3; | |
454 | } | |
455 | env->regs[r1] &= 0xffffffff00000000ULL; | |
456 | cc = 1; | |
457 | break; | |
71e47088 | 458 | #if !defined(CONFIG_USER_ONLY) |
1864b94a AG |
459 | case SIGP_RESTART: |
460 | qemu_system_reset_request(); | |
5638d180 | 461 | cpu_loop_exit(CPU(s390_env_get_cpu(env))); |
1864b94a AG |
462 | break; |
463 | case SIGP_STOP: | |
464 | qemu_system_shutdown_request(); | |
5638d180 | 465 | cpu_loop_exit(CPU(s390_env_get_cpu(env))); |
1864b94a AG |
466 | break; |
467 | #endif | |
defb0e31 AG |
468 | default: |
469 | /* unknown sigp */ | |
470 | fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code); | |
471 | cc = 3; | |
472 | } | |
473 | ||
474 | return cc; | |
475 | } | |
defb0e31 | 476 | #endif |