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/>.
21 #include "qemu/osdep.h"
23 #include "exec/memory.h"
24 #include "qemu/host-utils.h"
25 #include "exec/helper-proto.h"
26 #include "sysemu/kvm.h"
27 #include "qemu/timer.h"
28 #include "exec/address-spaces.h"
30 #include <linux/kvm.h>
32 #include "exec/cpu_ldst.h"
33 #include "hw/watchdog/wdt_diag288.h"
35 #if !defined(CONFIG_USER_ONLY)
36 #include "sysemu/cpus.h"
37 #include "sysemu/sysemu.h"
38 #include "hw/s390x/ebcdic.h"
39 #include "hw/s390x/ipl.h"
42 /* #define DEBUG_HELPER */
44 #define HELPER_LOG(x...) qemu_log(x)
46 #define HELPER_LOG(x...)
49 /* Raise an exception dynamically from a helper function. */
50 void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
53 CPUState *cs = CPU(s390_env_get_cpu(env));
56 cs->exception_index = EXCP_PGM;
57 env->int_pgm_code = excp;
59 /* Use the (ultimate) callers address to find the insn that trapped. */
60 cpu_restore_state(cs, retaddr);
62 /* Advance past the insn. */
63 t = cpu_ldub_code(env, env->psw.addr);
64 env->int_pgm_ilen = t = get_ilen(t);
70 /* Raise an exception statically from a TB. */
71 void HELPER(exception)(CPUS390XState *env, uint32_t excp)
73 CPUState *cs = CPU(s390_env_get_cpu(env));
75 HELPER_LOG("%s: exception %d\n", __func__, excp);
76 cs->exception_index = excp;
80 #ifndef CONFIG_USER_ONLY
82 void program_interrupt(CPUS390XState *env, uint32_t code, int ilen)
84 S390CPU *cpu = s390_env_get_cpu(env);
86 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
91 struct kvm_s390_irq irq = {
92 .type = KVM_S390_PROGRAM_INT,
96 kvm_s390_vcpu_interrupt(cpu, &irq);
99 CPUState *cs = CPU(cpu);
101 env->int_pgm_code = code;
102 env->int_pgm_ilen = ilen;
103 cs->exception_index = EXCP_PGM;
108 /* SCLP service call */
109 uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
111 int r = sclp_service_call(env, r1, r2);
113 program_interrupt(env, -r, 4);
119 #ifndef CONFIG_USER_ONLY
120 static int modified_clear_reset(S390CPU *cpu)
122 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
126 cpu_synchronize_all_states();
128 run_on_cpu(t, s390_do_cpu_full_reset, t);
133 scc->load_normal(CPU(cpu));
134 cpu_synchronize_all_post_reset();
139 static int load_normal_reset(S390CPU *cpu)
141 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
145 cpu_synchronize_all_states();
147 run_on_cpu(t, s390_do_cpu_reset, t);
151 scc->initial_cpu_reset(CPU(cpu));
152 scc->load_normal(CPU(cpu));
153 cpu_synchronize_all_post_reset();
158 int handle_diag_288(CPUS390XState *env, uint64_t r1, uint64_t r3)
160 uint64_t func = env->regs[r1];
161 uint64_t timeout = env->regs[r1 + 1];
162 uint64_t action = env->regs[r3];
164 DIAG288State *diag288;
165 DIAG288Class *diag288_class;
167 if (r1 % 2 || action != 0) {
171 /* Timeout must be more than 15 seconds except for timer deletion */
172 if (func != WDT_DIAG288_CANCEL && timeout < 15) {
176 obj = object_resolve_path_type("", TYPE_WDT_DIAG288, NULL);
181 diag288 = DIAG288(obj);
182 diag288_class = DIAG288_GET_CLASS(diag288);
183 return diag288_class->handle_timer(diag288, func, timeout);
186 #define DIAG_308_RC_OK 0x0001
187 #define DIAG_308_RC_NO_CONF 0x0102
188 #define DIAG_308_RC_INVALID 0x0402
190 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3)
192 uint64_t addr = env->regs[r1];
193 uint64_t subcode = env->regs[r3];
194 IplParameterBlock *iplb;
196 if (env->psw.mask & PSW_MASK_PSTATE) {
197 program_interrupt(env, PGM_PRIVILEGED, ILEN_LATER_INC);
201 if ((subcode & ~0x0ffffULL) || (subcode > 6)) {
202 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
208 modified_clear_reset(s390_env_get_cpu(env));
210 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
214 load_normal_reset(s390_env_get_cpu(env));
216 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
220 s390_reipl_request();
222 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
226 if ((r1 & 1) || (addr & 0x0fffULL)) {
227 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
230 if (!address_space_access_valid(&address_space_memory, addr,
231 sizeof(IplParameterBlock), false)) {
232 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
235 iplb = g_malloc0(sizeof(struct IplParameterBlock));
236 cpu_physical_memory_read(addr, iplb, sizeof(struct IplParameterBlock));
237 s390_ipl_update_diag308(iplb);
238 env->regs[r1 + 1] = DIAG_308_RC_OK;
242 if ((r1 & 1) || (addr & 0x0fffULL)) {
243 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
246 if (!address_space_access_valid(&address_space_memory, addr,
247 sizeof(IplParameterBlock), true)) {
248 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
251 iplb = s390_ipl_get_iplb();
253 cpu_physical_memory_write(addr, iplb,
254 sizeof(struct IplParameterBlock));
255 env->regs[r1 + 1] = DIAG_308_RC_OK;
257 env->regs[r1 + 1] = DIAG_308_RC_NO_CONF;
261 hw_error("Unhandled diag308 subcode %" PRIx64, subcode);
267 void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
274 r = s390_virtio_hypercall(env);
282 handle_diag_308(env, r1, r3);
291 program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC);
296 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
298 CPUState *cs = CPU(s390_env_get_cpu(env));
299 uint32_t prefix = a1 & 0x7fffe000;
302 HELPER_LOG("prefix: %#x\n", prefix);
303 tlb_flush_page(cs, 0);
304 tlb_flush_page(cs, TARGET_PAGE_SIZE);
308 uint64_t HELPER(stck)(CPUS390XState *env)
312 time = env->tod_offset +
313 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
318 /* Set Clock Comparator */
319 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
327 /* difference between origins */
328 time -= env->tod_offset;
331 time = tod2time(time);
333 timer_mod(env->tod_timer, env->tod_basetime + time);
336 /* Store Clock Comparator */
337 uint64_t HELPER(stckc)(CPUS390XState *env)
343 void HELPER(spt)(CPUS390XState *env, uint64_t time)
350 time = tod2time(time);
352 env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
354 timer_mod(env->cpu_timer, env->cputm);
357 /* Store CPU Timer */
358 uint64_t HELPER(stpt)(CPUS390XState *env)
360 return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
363 /* Store System Information */
364 uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0,
365 uint64_t r0, uint64_t r1)
370 if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
371 ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
372 /* valid function code, invalid reserved bits */
373 program_interrupt(env, PGM_SPECIFICATION, 2);
376 sel1 = r0 & STSI_R0_SEL1_MASK;
377 sel2 = r1 & STSI_R1_SEL2_MASK;
379 /* XXX: spec exception if sysib is not 4k-aligned */
381 switch (r0 & STSI_LEVEL_MASK) {
383 if ((sel1 == 1) && (sel2 == 1)) {
384 /* Basic Machine Configuration */
385 struct sysib_111 sysib;
387 memset(&sysib, 0, sizeof(sysib));
388 ebcdic_put(sysib.manuf, "QEMU ", 16);
389 /* same as machine type number in STORE CPU ID */
390 ebcdic_put(sysib.type, "QEMU", 4);
391 /* same as model number in STORE CPU ID */
392 ebcdic_put(sysib.model, "QEMU ", 16);
393 ebcdic_put(sysib.sequence, "QEMU ", 16);
394 ebcdic_put(sysib.plant, "QEMU", 4);
395 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
396 } else if ((sel1 == 2) && (sel2 == 1)) {
397 /* Basic Machine CPU */
398 struct sysib_121 sysib;
400 memset(&sysib, 0, sizeof(sysib));
401 /* XXX make different for different CPUs? */
402 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
403 ebcdic_put(sysib.plant, "QEMU", 4);
404 stw_p(&sysib.cpu_addr, env->cpu_num);
405 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
406 } else if ((sel1 == 2) && (sel2 == 2)) {
407 /* Basic Machine CPUs */
408 struct sysib_122 sysib;
410 memset(&sysib, 0, sizeof(sysib));
411 stl_p(&sysib.capability, 0x443afc29);
412 /* XXX change when SMP comes */
413 stw_p(&sysib.total_cpus, 1);
414 stw_p(&sysib.active_cpus, 1);
415 stw_p(&sysib.standby_cpus, 0);
416 stw_p(&sysib.reserved_cpus, 0);
417 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
424 if ((sel1 == 2) && (sel2 == 1)) {
426 struct sysib_221 sysib;
428 memset(&sysib, 0, sizeof(sysib));
429 /* XXX make different for different CPUs? */
430 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
431 ebcdic_put(sysib.plant, "QEMU", 4);
432 stw_p(&sysib.cpu_addr, env->cpu_num);
433 stw_p(&sysib.cpu_id, 0);
434 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
435 } else if ((sel1 == 2) && (sel2 == 2)) {
437 struct sysib_222 sysib;
439 memset(&sysib, 0, sizeof(sysib));
440 stw_p(&sysib.lpar_num, 0);
442 /* XXX change when SMP comes */
443 stw_p(&sysib.total_cpus, 1);
444 stw_p(&sysib.conf_cpus, 1);
445 stw_p(&sysib.standby_cpus, 0);
446 stw_p(&sysib.reserved_cpus, 0);
447 ebcdic_put(sysib.name, "QEMU ", 8);
448 stl_p(&sysib.caf, 1000);
449 stw_p(&sysib.dedicated_cpus, 0);
450 stw_p(&sysib.shared_cpus, 0);
451 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
459 if ((sel1 == 2) && (sel2 == 2)) {
461 struct sysib_322 sysib;
463 memset(&sysib, 0, sizeof(sysib));
465 /* XXX change when SMP comes */
466 stw_p(&sysib.vm[0].total_cpus, 1);
467 stw_p(&sysib.vm[0].conf_cpus, 1);
468 stw_p(&sysib.vm[0].standby_cpus, 0);
469 stw_p(&sysib.vm[0].reserved_cpus, 0);
470 ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
471 stl_p(&sysib.vm[0].caf, 1000);
472 ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16);
473 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
479 case STSI_LEVEL_CURRENT:
480 env->regs[0] = STSI_LEVEL_3;
490 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
493 int cc = SIGP_CC_ORDER_CODE_ACCEPTED;
495 HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
496 __func__, order_code, r1, cpu_addr);
498 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
499 as parameter (input). Status (output) is always R1. */
501 switch (order_code) {
506 /* enumerate CPU status */
508 /* XXX implement when SMP comes */
511 env->regs[r1] &= 0xffffffff00000000ULL;
514 #if !defined(CONFIG_USER_ONLY)
516 qemu_system_reset_request();
517 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
520 qemu_system_shutdown_request();
521 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
526 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
527 cc = SIGP_CC_NOT_OPERATIONAL;
534 #ifndef CONFIG_USER_ONLY
535 void HELPER(xsch)(CPUS390XState *env, uint64_t r1)
537 S390CPU *cpu = s390_env_get_cpu(env);
538 ioinst_handle_xsch(cpu, r1);
541 void HELPER(csch)(CPUS390XState *env, uint64_t r1)
543 S390CPU *cpu = s390_env_get_cpu(env);
544 ioinst_handle_csch(cpu, r1);
547 void HELPER(hsch)(CPUS390XState *env, uint64_t r1)
549 S390CPU *cpu = s390_env_get_cpu(env);
550 ioinst_handle_hsch(cpu, r1);
553 void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
555 S390CPU *cpu = s390_env_get_cpu(env);
556 ioinst_handle_msch(cpu, r1, inst >> 16);
559 void HELPER(rchp)(CPUS390XState *env, uint64_t r1)
561 S390CPU *cpu = s390_env_get_cpu(env);
562 ioinst_handle_rchp(cpu, r1);
565 void HELPER(rsch)(CPUS390XState *env, uint64_t r1)
567 S390CPU *cpu = s390_env_get_cpu(env);
568 ioinst_handle_rsch(cpu, r1);
571 void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
573 S390CPU *cpu = s390_env_get_cpu(env);
574 ioinst_handle_ssch(cpu, r1, inst >> 16);
577 void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
579 S390CPU *cpu = s390_env_get_cpu(env);
580 ioinst_handle_stsch(cpu, r1, inst >> 16);
583 void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
585 S390CPU *cpu = s390_env_get_cpu(env);
586 ioinst_handle_tsch(cpu, r1, inst >> 16);
589 void HELPER(chsc)(CPUS390XState *env, uint64_t inst)
591 S390CPU *cpu = s390_env_get_cpu(env);
592 ioinst_handle_chsc(cpu, inst >> 16);
596 #ifndef CONFIG_USER_ONLY
597 void HELPER(per_check_exception)(CPUS390XState *env)
599 CPUState *cs = CPU(s390_env_get_cpu(env));
601 if (env->per_perc_atmid) {
602 env->int_pgm_code = PGM_PER;
603 env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, env->per_address));
605 cs->exception_index = EXCP_PGM;
610 void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to)
612 if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) {
613 if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS)
614 || get_per_in_range(env, to)) {
615 env->per_address = from;
616 env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env);
621 void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr)
623 if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) {
624 env->per_address = addr;
625 env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env);
627 /* If the instruction has to be nullified, trigger the
628 exception immediately. */
629 if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) {
630 CPUState *cs = CPU(s390_env_get_cpu(env));
632 env->int_pgm_code = PGM_PER;
633 env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr));
635 cs->exception_index = EXCP_PGM;