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/exec-all.h"
33 #include "exec/cpu_ldst.h"
35 #if !defined(CONFIG_USER_ONLY)
36 #include "hw/watchdog/wdt_diag288.h"
37 #include "sysemu/cpus.h"
38 #include "sysemu/sysemu.h"
39 #include "hw/s390x/ebcdic.h"
40 #include "hw/s390x/ipl.h"
43 /* #define DEBUG_HELPER */
45 #define HELPER_LOG(x...) qemu_log(x)
47 #define HELPER_LOG(x...)
50 /* Raise an exception dynamically from a helper function. */
51 void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
54 CPUState *cs = CPU(s390_env_get_cpu(env));
57 cs->exception_index = EXCP_PGM;
58 env->int_pgm_code = excp;
60 /* Use the (ultimate) callers address to find the insn that trapped. */
61 cpu_restore_state(cs, retaddr);
63 /* Advance past the insn. */
64 t = cpu_ldub_code(env, env->psw.addr);
65 env->int_pgm_ilen = t = get_ilen(t);
71 /* Raise an exception statically from a TB. */
72 void HELPER(exception)(CPUS390XState *env, uint32_t excp)
74 CPUState *cs = CPU(s390_env_get_cpu(env));
76 HELPER_LOG("%s: exception %d\n", __func__, excp);
77 cs->exception_index = excp;
81 #ifndef CONFIG_USER_ONLY
83 void program_interrupt(CPUS390XState *env, uint32_t code, int ilen)
85 S390CPU *cpu = s390_env_get_cpu(env);
87 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
92 struct kvm_s390_irq irq = {
93 .type = KVM_S390_PROGRAM_INT,
97 kvm_s390_vcpu_interrupt(cpu, &irq);
100 CPUState *cs = CPU(cpu);
102 env->int_pgm_code = code;
103 env->int_pgm_ilen = ilen;
104 cs->exception_index = EXCP_PGM;
109 /* SCLP service call */
110 uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
112 int r = sclp_service_call(env, r1, r2);
114 program_interrupt(env, -r, 4);
120 #ifndef CONFIG_USER_ONLY
121 static int modified_clear_reset(S390CPU *cpu)
123 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
127 cpu_synchronize_all_states();
129 run_on_cpu(t, s390_do_cpu_full_reset, t);
134 scc->load_normal(CPU(cpu));
135 cpu_synchronize_all_post_reset();
140 static int load_normal_reset(S390CPU *cpu)
142 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
146 cpu_synchronize_all_states();
148 run_on_cpu(t, s390_do_cpu_reset, t);
152 scc->initial_cpu_reset(CPU(cpu));
153 scc->load_normal(CPU(cpu));
154 cpu_synchronize_all_post_reset();
159 int handle_diag_288(CPUS390XState *env, uint64_t r1, uint64_t r3)
161 uint64_t func = env->regs[r1];
162 uint64_t timeout = env->regs[r1 + 1];
163 uint64_t action = env->regs[r3];
165 DIAG288State *diag288;
166 DIAG288Class *diag288_class;
168 if (r1 % 2 || action != 0) {
172 /* Timeout must be more than 15 seconds except for timer deletion */
173 if (func != WDT_DIAG288_CANCEL && timeout < 15) {
177 obj = object_resolve_path_type("", TYPE_WDT_DIAG288, NULL);
182 diag288 = DIAG288(obj);
183 diag288_class = DIAG288_GET_CLASS(diag288);
184 return diag288_class->handle_timer(diag288, func, timeout);
187 #define DIAG_308_RC_OK 0x0001
188 #define DIAG_308_RC_NO_CONF 0x0102
189 #define DIAG_308_RC_INVALID 0x0402
191 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3)
193 uint64_t addr = env->regs[r1];
194 uint64_t subcode = env->regs[r3];
195 IplParameterBlock *iplb;
197 if (env->psw.mask & PSW_MASK_PSTATE) {
198 program_interrupt(env, PGM_PRIVILEGED, ILEN_LATER_INC);
202 if ((subcode & ~0x0ffffULL) || (subcode > 6)) {
203 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
209 modified_clear_reset(s390_env_get_cpu(env));
211 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
215 load_normal_reset(s390_env_get_cpu(env));
217 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
221 s390_reipl_request();
223 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
227 if ((r1 & 1) || (addr & 0x0fffULL)) {
228 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
231 if (!address_space_access_valid(&address_space_memory, addr,
232 sizeof(IplParameterBlock), false)) {
233 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
236 iplb = g_malloc0(sizeof(IplParameterBlock));
237 cpu_physical_memory_read(addr, iplb, sizeof(iplb->len));
238 if (!iplb_valid_len(iplb)) {
239 env->regs[r1 + 1] = DIAG_308_RC_INVALID;
243 cpu_physical_memory_read(addr, iplb, be32_to_cpu(iplb->len));
245 if (!iplb_valid_ccw(iplb) && !iplb_valid_fcp(iplb)) {
246 env->regs[r1 + 1] = DIAG_308_RC_INVALID;
250 s390_ipl_update_diag308(iplb);
251 env->regs[r1 + 1] = DIAG_308_RC_OK;
256 if ((r1 & 1) || (addr & 0x0fffULL)) {
257 program_interrupt(env, PGM_SPECIFICATION, ILEN_LATER_INC);
260 if (!address_space_access_valid(&address_space_memory, addr,
261 sizeof(IplParameterBlock), true)) {
262 program_interrupt(env, PGM_ADDRESSING, ILEN_LATER_INC);
265 iplb = s390_ipl_get_iplb();
267 cpu_physical_memory_write(addr, iplb, be32_to_cpu(iplb->len));
268 env->regs[r1 + 1] = DIAG_308_RC_OK;
270 env->regs[r1 + 1] = DIAG_308_RC_NO_CONF;
274 hw_error("Unhandled diag308 subcode %" PRIx64, subcode);
280 void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
287 r = s390_virtio_hypercall(env);
295 handle_diag_308(env, r1, r3);
304 program_interrupt(env, PGM_OPERATION, ILEN_LATER_INC);
309 void HELPER(spx)(CPUS390XState *env, uint64_t a1)
311 CPUState *cs = CPU(s390_env_get_cpu(env));
312 uint32_t prefix = a1 & 0x7fffe000;
315 HELPER_LOG("prefix: %#x\n", prefix);
316 tlb_flush_page(cs, 0);
317 tlb_flush_page(cs, TARGET_PAGE_SIZE);
321 uint64_t HELPER(stck)(CPUS390XState *env)
325 time = env->tod_offset +
326 time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
331 /* Set Clock Comparator */
332 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
340 /* difference between origins */
341 time -= env->tod_offset;
344 time = tod2time(time);
346 timer_mod(env->tod_timer, env->tod_basetime + time);
349 /* Store Clock Comparator */
350 uint64_t HELPER(stckc)(CPUS390XState *env)
356 void HELPER(spt)(CPUS390XState *env, uint64_t time)
363 time = tod2time(time);
365 env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
367 timer_mod(env->cpu_timer, env->cputm);
370 /* Store CPU Timer */
371 uint64_t HELPER(stpt)(CPUS390XState *env)
373 return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
376 /* Store System Information */
377 uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0,
378 uint64_t r0, uint64_t r1)
383 if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
384 ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
385 /* valid function code, invalid reserved bits */
386 program_interrupt(env, PGM_SPECIFICATION, 2);
389 sel1 = r0 & STSI_R0_SEL1_MASK;
390 sel2 = r1 & STSI_R1_SEL2_MASK;
392 /* XXX: spec exception if sysib is not 4k-aligned */
394 switch (r0 & STSI_LEVEL_MASK) {
396 if ((sel1 == 1) && (sel2 == 1)) {
397 /* Basic Machine Configuration */
398 struct sysib_111 sysib;
400 memset(&sysib, 0, sizeof(sysib));
401 ebcdic_put(sysib.manuf, "QEMU ", 16);
402 /* same as machine type number in STORE CPU ID */
403 ebcdic_put(sysib.type, "QEMU", 4);
404 /* same as model number in STORE CPU ID */
405 ebcdic_put(sysib.model, "QEMU ", 16);
406 ebcdic_put(sysib.sequence, "QEMU ", 16);
407 ebcdic_put(sysib.plant, "QEMU", 4);
408 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
409 } else if ((sel1 == 2) && (sel2 == 1)) {
410 /* Basic Machine CPU */
411 struct sysib_121 sysib;
413 memset(&sysib, 0, sizeof(sysib));
414 /* XXX make different for different CPUs? */
415 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
416 ebcdic_put(sysib.plant, "QEMU", 4);
417 stw_p(&sysib.cpu_addr, env->cpu_num);
418 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
419 } else if ((sel1 == 2) && (sel2 == 2)) {
420 /* Basic Machine CPUs */
421 struct sysib_122 sysib;
423 memset(&sysib, 0, sizeof(sysib));
424 stl_p(&sysib.capability, 0x443afc29);
425 /* XXX change when SMP comes */
426 stw_p(&sysib.total_cpus, 1);
427 stw_p(&sysib.active_cpus, 1);
428 stw_p(&sysib.standby_cpus, 0);
429 stw_p(&sysib.reserved_cpus, 0);
430 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
437 if ((sel1 == 2) && (sel2 == 1)) {
439 struct sysib_221 sysib;
441 memset(&sysib, 0, sizeof(sysib));
442 /* XXX make different for different CPUs? */
443 ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
444 ebcdic_put(sysib.plant, "QEMU", 4);
445 stw_p(&sysib.cpu_addr, env->cpu_num);
446 stw_p(&sysib.cpu_id, 0);
447 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
448 } else if ((sel1 == 2) && (sel2 == 2)) {
450 struct sysib_222 sysib;
452 memset(&sysib, 0, sizeof(sysib));
453 stw_p(&sysib.lpar_num, 0);
455 /* XXX change when SMP comes */
456 stw_p(&sysib.total_cpus, 1);
457 stw_p(&sysib.conf_cpus, 1);
458 stw_p(&sysib.standby_cpus, 0);
459 stw_p(&sysib.reserved_cpus, 0);
460 ebcdic_put(sysib.name, "QEMU ", 8);
461 stl_p(&sysib.caf, 1000);
462 stw_p(&sysib.dedicated_cpus, 0);
463 stw_p(&sysib.shared_cpus, 0);
464 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
472 if ((sel1 == 2) && (sel2 == 2)) {
474 struct sysib_322 sysib;
476 memset(&sysib, 0, sizeof(sysib));
478 /* XXX change when SMP comes */
479 stw_p(&sysib.vm[0].total_cpus, 1);
480 stw_p(&sysib.vm[0].conf_cpus, 1);
481 stw_p(&sysib.vm[0].standby_cpus, 0);
482 stw_p(&sysib.vm[0].reserved_cpus, 0);
483 ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
484 stl_p(&sysib.vm[0].caf, 1000);
485 ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16);
486 cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
492 case STSI_LEVEL_CURRENT:
493 env->regs[0] = STSI_LEVEL_3;
503 uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
506 int cc = SIGP_CC_ORDER_CODE_ACCEPTED;
508 HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
509 __func__, order_code, r1, cpu_addr);
511 /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
512 as parameter (input). Status (output) is always R1. */
514 switch (order_code) {
519 /* enumerate CPU status */
521 /* XXX implement when SMP comes */
524 env->regs[r1] &= 0xffffffff00000000ULL;
527 #if !defined(CONFIG_USER_ONLY)
529 qemu_system_reset_request();
530 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
533 qemu_system_shutdown_request();
534 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
539 fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
540 cc = SIGP_CC_NOT_OPERATIONAL;
547 #ifndef CONFIG_USER_ONLY
548 void HELPER(xsch)(CPUS390XState *env, uint64_t r1)
550 S390CPU *cpu = s390_env_get_cpu(env);
551 ioinst_handle_xsch(cpu, r1);
554 void HELPER(csch)(CPUS390XState *env, uint64_t r1)
556 S390CPU *cpu = s390_env_get_cpu(env);
557 ioinst_handle_csch(cpu, r1);
560 void HELPER(hsch)(CPUS390XState *env, uint64_t r1)
562 S390CPU *cpu = s390_env_get_cpu(env);
563 ioinst_handle_hsch(cpu, r1);
566 void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
568 S390CPU *cpu = s390_env_get_cpu(env);
569 ioinst_handle_msch(cpu, r1, inst >> 16);
572 void HELPER(rchp)(CPUS390XState *env, uint64_t r1)
574 S390CPU *cpu = s390_env_get_cpu(env);
575 ioinst_handle_rchp(cpu, r1);
578 void HELPER(rsch)(CPUS390XState *env, uint64_t r1)
580 S390CPU *cpu = s390_env_get_cpu(env);
581 ioinst_handle_rsch(cpu, r1);
584 void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
586 S390CPU *cpu = s390_env_get_cpu(env);
587 ioinst_handle_ssch(cpu, r1, inst >> 16);
590 void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
592 S390CPU *cpu = s390_env_get_cpu(env);
593 ioinst_handle_stsch(cpu, r1, inst >> 16);
596 void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
598 S390CPU *cpu = s390_env_get_cpu(env);
599 ioinst_handle_tsch(cpu, r1, inst >> 16);
602 void HELPER(chsc)(CPUS390XState *env, uint64_t inst)
604 S390CPU *cpu = s390_env_get_cpu(env);
605 ioinst_handle_chsc(cpu, inst >> 16);
609 #ifndef CONFIG_USER_ONLY
610 void HELPER(per_check_exception)(CPUS390XState *env)
612 CPUState *cs = CPU(s390_env_get_cpu(env));
614 if (env->per_perc_atmid) {
615 env->int_pgm_code = PGM_PER;
616 env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, env->per_address));
618 cs->exception_index = EXCP_PGM;
623 void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to)
625 if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) {
626 if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS)
627 || get_per_in_range(env, to)) {
628 env->per_address = from;
629 env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env);
634 void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr)
636 if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) {
637 env->per_address = addr;
638 env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env);
640 /* If the instruction has to be nullified, trigger the
641 exception immediately. */
642 if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) {
643 CPUState *cs = CPU(s390_env_get_cpu(env));
645 env->int_pgm_code = PGM_PER;
646 env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr));
648 cs->exception_index = EXCP_PGM;