2 * S/390 misc helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "exec/memory.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
26 #include "sysemu/kvm.h"
27 #include "qemu/timer.h"
29 #include <linux/kvm.h>
31 #include "exec/cpu_ldst.h"
33 #if !defined(CONFIG_USER_ONLY)
34 #include "sysemu/cpus.h"
35 #include "sysemu/sysemu.h"
36 #include "hw/s390x/ebcdic.h"
39 /* #define DEBUG_HELPER */
41 #define HELPER_LOG(x...) qemu_log(x)
43 #define HELPER_LOG(x...)
46 /* Raise an exception dynamically from a helper function. */
47 void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
50 CPUState *cs = CPU(s390_env_get_cpu(env));
53 cs->exception_index = EXCP_PGM;
54 env->int_pgm_code = excp;
56 /* Use the (ultimate) callers address to find the insn that trapped. */
57 cpu_restore_state(cs, retaddr);
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;
67 /* Raise an exception statically from a TB. */
68 void HELPER(exception)(CPUS390XState *env, uint32_t excp)
70 CPUState *cs = CPU(s390_env_get_cpu(env));
72 HELPER_LOG("%s: exception %d\n", __func__, excp);
73 cs->exception_index = excp;
77 #ifndef CONFIG_USER_ONLY
79 void program_interrupt(CPUS390XState *env, uint32_t code, int ilen)
81 S390CPU *cpu = s390_env_get_cpu(env);
83 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
88 struct kvm_s390_irq irq = {
89 .type = KVM_S390_PROGRAM_INT,
93 kvm_s390_vcpu_interrupt(cpu, &irq);
96 CPUState *cs = CPU(cpu);
98 env->int_pgm_code = code;
99 env->int_pgm_ilen = ilen;
100 cs->exception_index = EXCP_PGM;
105 /* SCLP service call */
106 uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
108 int r = sclp_service_call(env, r1, r2);
110 program_interrupt(env, -r, 4);
116 #ifndef CONFIG_USER_ONLY
117 static int modified_clear_reset(S390CPU *cpu)
119 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
123 cpu_synchronize_all_states();
125 run_on_cpu(t, s390_do_cpu_full_reset, t);
128 io_subsystem_reset();
129 scc->load_normal(CPU(cpu));
130 cpu_synchronize_all_post_reset();
135 static int load_normal_reset(S390CPU *cpu)
137 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
141 cpu_synchronize_all_states();
143 run_on_cpu(t, s390_do_cpu_reset, t);
146 io_subsystem_reset();
147 scc->initial_cpu_reset(CPU(cpu));
148 scc->load_normal(CPU(cpu));
149 cpu_synchronize_all_post_reset();
154 #define DIAG_308_RC_NO_CONF 0x0102
155 #define DIAG_308_RC_INVALID 0x0402
156 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3)
158 uint64_t addr = env->regs[r1];
159 uint64_t subcode = env->regs[r3];
161 if (env->psw.mask & PSW_MASK_PSTATE) {
162 program_interrupt(env, PGM_PRIVILEGED, ILEN_LATER_INC);
166 if ((subcode & ~0x0ffffULL) || (subcode > 6)) {
167 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
173 modified_clear_reset(s390_env_get_cpu(env));
176 load_normal_reset(s390_env_get_cpu(env));
179 if ((r1 & 1) || (addr & 0x0fffULL)) {
180 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
183 env->regs[r1+1] = DIAG_308_RC_INVALID;
186 if ((r1 & 1) || (addr & 0x0fffULL)) {
187 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
190 env->regs[r1+1] = DIAG_308_RC_NO_CONF;
193 hw_error("Unhandled diag308 subcode %" PRIx64, subcode);
200 uint64_t HELPER(diag)(CPUS390XState *env, uint32_t num, uint64_t mem,
208 r = s390_virtio_hypercall(env);
224 program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC);
231 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
233 CPUState *cs = CPU(s390_env_get_cpu(env));
234 uint32_t prefix = a1 & 0x7fffe000;
237 qemu_log("prefix: %#x\n", prefix);
238 tlb_flush_page(cs, 0);
239 tlb_flush_page(cs, TARGET_PAGE_SIZE);
242 static inline uint64_t clock_value(CPUS390XState *env)
246 time = env->tod_offset +
247 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
253 uint64_t HELPER(stck)(CPUS390XState *env)
255 return clock_value(env);
258 /* Set Clock Comparator */
259 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
265 /* difference between now and then */
266 time -= clock_value(env);
268 time = (time * 125) >> 9;
270 timer_mod(env->tod_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
273 /* Store Clock Comparator */
274 uint64_t HELPER(stckc)(CPUS390XState *env)
281 void HELPER(spt)(CPUS390XState *env, uint64_t time)
288 time = (time * 125) >> 9;
290 timer_mod(env->cpu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
293 /* Store CPU Timer */
294 uint64_t HELPER(stpt)(CPUS390XState *env)
300 /* Store System Information */
301 uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0,
302 uint64_t r0, uint64_t r1)
307 if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
308 ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
309 /* valid function code, invalid reserved bits */
310 program_interrupt(env, PGM_SPECIFICATION, 2);
313 sel1 = r0 & STSI_R0_SEL1_MASK;
314 sel2 = r1 & STSI_R1_SEL2_MASK;
316 /* XXX: spec exception if sysib is not 4k-aligned */
318 switch (r0 & STSI_LEVEL_MASK) {
320 if ((sel1 == 1) && (sel2 == 1)) {
321 /* Basic Machine Configuration */
322 struct sysib_111 sysib;
324 memset(&sysib, 0, sizeof(sysib));
325 ebcdic_put(sysib.manuf, "QEMU ", 16);
326 /* same as machine type number in STORE CPU ID */
327 ebcdic_put(sysib.type, "QEMU", 4);
328 /* same as model number in STORE CPU ID */
329 ebcdic_put(sysib.model, "QEMU ", 16);
330 ebcdic_put(sysib.sequence, "QEMU ", 16);
331 ebcdic_put(sysib.plant, "QEMU", 4);
332 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
333 } else if ((sel1 == 2) && (sel2 == 1)) {
334 /* Basic Machine CPU */
335 struct sysib_121 sysib;
337 memset(&sysib, 0, sizeof(sysib));
338 /* XXX make different for different CPUs? */
339 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
340 ebcdic_put(sysib.plant, "QEMU", 4);
341 stw_p(&sysib.cpu_addr, env->cpu_num);
342 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
343 } else if ((sel1 == 2) && (sel2 == 2)) {
344 /* Basic Machine CPUs */
345 struct sysib_122 sysib;
347 memset(&sysib, 0, sizeof(sysib));
348 stl_p(&sysib.capability, 0x443afc29);
349 /* XXX change when SMP comes */
350 stw_p(&sysib.total_cpus, 1);
351 stw_p(&sysib.active_cpus, 1);
352 stw_p(&sysib.standby_cpus, 0);
353 stw_p(&sysib.reserved_cpus, 0);
354 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
361 if ((sel1 == 2) && (sel2 == 1)) {
363 struct sysib_221 sysib;
365 memset(&sysib, 0, sizeof(sysib));
366 /* XXX make different for different CPUs? */
367 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
368 ebcdic_put(sysib.plant, "QEMU", 4);
369 stw_p(&sysib.cpu_addr, env->cpu_num);
370 stw_p(&sysib.cpu_id, 0);
371 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
372 } else if ((sel1 == 2) && (sel2 == 2)) {
374 struct sysib_222 sysib;
376 memset(&sysib, 0, sizeof(sysib));
377 stw_p(&sysib.lpar_num, 0);
379 /* XXX change when SMP comes */
380 stw_p(&sysib.total_cpus, 1);
381 stw_p(&sysib.conf_cpus, 1);
382 stw_p(&sysib.standby_cpus, 0);
383 stw_p(&sysib.reserved_cpus, 0);
384 ebcdic_put(sysib.name, "QEMU ", 8);
385 stl_p(&sysib.caf, 1000);
386 stw_p(&sysib.dedicated_cpus, 0);
387 stw_p(&sysib.shared_cpus, 0);
388 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
396 if ((sel1 == 2) && (sel2 == 2)) {
398 struct sysib_322 sysib;
400 memset(&sysib, 0, sizeof(sysib));
402 /* XXX change when SMP comes */
403 stw_p(&sysib.vm[0].total_cpus, 1);
404 stw_p(&sysib.vm[0].conf_cpus, 1);
405 stw_p(&sysib.vm[0].standby_cpus, 0);
406 stw_p(&sysib.vm[0].reserved_cpus, 0);
407 ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
408 stl_p(&sysib.vm[0].caf, 1000);
409 ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16);
410 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
416 case STSI_LEVEL_CURRENT:
417 env->regs[0] = STSI_LEVEL_3;
427 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
432 HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
433 __func__, order_code, r1, cpu_addr);
435 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
436 as parameter (input). Status (output) is always R1. */
438 switch (order_code) {
443 /* enumerate CPU status */
445 /* XXX implement when SMP comes */
448 env->regs[r1] &= 0xffffffff00000000ULL;
451 #if !defined(CONFIG_USER_ONLY)
453 qemu_system_reset_request();
454 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
457 qemu_system_shutdown_request();
458 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
463 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);