2 * I/O instructions for S/390
4 * Copyright 2012, 2015 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
12 #include "qemu/osdep.h"
16 #include "hw/s390x/ioinst.h"
18 #include "hw/s390x/s390-pci-bus.h"
20 int ioinst_disassemble_sch_ident(uint32_t value
, int *m
, int *cssid
, int *ssid
,
23 if (!IOINST_SCHID_ONE(value
)) {
26 if (!IOINST_SCHID_M(value
)) {
27 if (IOINST_SCHID_CSSID(value
)) {
33 *cssid
= IOINST_SCHID_CSSID(value
);
36 *ssid
= IOINST_SCHID_SSID(value
);
37 *schid
= IOINST_SCHID_NR(value
);
41 void ioinst_handle_xsch(S390CPU
*cpu
, uint64_t reg1
)
43 int cssid
, ssid
, schid
, m
;
46 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
47 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
50 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
51 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
52 if (!sch
|| !css_subch_visible(sch
)) {
56 setcc(cpu
, css_do_xsch(sch
));
59 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
61 int cssid
, ssid
, schid
, m
;
66 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
67 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
70 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
71 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
72 if (sch
&& css_subch_visible(sch
)) {
73 ret
= css_do_csch(sch
);
83 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
85 int cssid
, ssid
, schid
, m
;
90 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
91 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
94 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
95 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
96 if (sch
&& css_subch_visible(sch
)) {
97 ret
= css_do_hsch(sch
);
116 static int ioinst_schib_valid(SCHIB
*schib
)
118 if ((be16_to_cpu(schib
->pmcw
.flags
) & PMCW_FLAGS_MASK_INVALID
) ||
119 (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_INVALID
)) {
122 /* Disallow extended measurements for now. */
123 if (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_XMWME
) {
129 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
131 int cssid
, ssid
, schid
, m
;
137 CPUS390XState
*env
= &cpu
->env
;
140 addr
= decode_basedisp_s(env
, ipb
, &ar
);
142 program_interrupt(env
, PGM_SPECIFICATION
, 4);
145 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &schib
, sizeof(schib
))) {
148 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
149 !ioinst_schib_valid(&schib
)) {
150 program_interrupt(env
, PGM_OPERAND
, 4);
153 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
154 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
155 if (sch
&& css_subch_visible(sch
)) {
156 ret
= css_do_msch(sch
, &schib
);
175 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
177 dest
->intparm
= be32_to_cpu(src
->intparm
);
178 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
179 dest
->lpm
= src
->lpm
;
180 dest
->ctrl1
= src
->ctrl1
;
181 dest
->cpa
= be32_to_cpu(src
->cpa
);
184 static int ioinst_orb_valid(ORB
*orb
)
186 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
187 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
190 /* We don't support MIDA. */
191 if (orb
->ctrl1
& ORB_CTRL1_MASK_MIDAW
) {
194 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
200 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
202 int cssid
, ssid
, schid
, m
;
206 CPUS390XState
*env
= &cpu
->env
;
209 addr
= decode_basedisp_s(env
, ipb
, &ar
);
211 program_interrupt(env
, PGM_SPECIFICATION
, 4);
214 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &orig_orb
, sizeof(orb
))) {
217 copy_orb_from_guest(&orb
, &orig_orb
);
218 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
219 !ioinst_orb_valid(&orb
)) {
220 program_interrupt(env
, PGM_OPERAND
, 4);
223 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
224 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
225 if (!sch
|| !css_subch_visible(sch
)) {
229 setcc(cpu
, css_do_ssch(sch
, &orb
));
232 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
237 CPUS390XState
*env
= &cpu
->env
;
240 addr
= decode_basedisp_s(env
, ipb
, &ar
);
242 program_interrupt(env
, PGM_SPECIFICATION
, 4);
246 cc
= css_do_stcrw(&crw
);
247 /* 0 - crw stored, 1 - zeroes stored */
249 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &crw
, sizeof(crw
)) == 0) {
251 } else if (cc
== 0) {
252 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
253 css_undo_stcrw(&crw
);
257 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
259 int cssid
, ssid
, schid
, m
;
264 CPUS390XState
*env
= &cpu
->env
;
267 addr
= decode_basedisp_s(env
, ipb
, &ar
);
269 program_interrupt(env
, PGM_SPECIFICATION
, 4);
273 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
275 * As operand exceptions have a lower priority than access exceptions,
276 * we check whether the memory area is writeable (injecting the
277 * access execption if it is not) first.
279 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
))) {
280 program_interrupt(env
, PGM_OPERAND
, 4);
284 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
285 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
287 if (css_subch_visible(sch
)) {
288 css_do_stsch(sch
, &schib
);
291 /* Indicate no more subchannels in this css/ss */
295 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
296 cc
= 3; /* No more subchannels in this css/ss */
298 /* Store an empty schib. */
299 memset(&schib
, 0, sizeof(schib
));
304 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &schib
,
305 sizeof(schib
)) != 0) {
309 /* Access exceptions have a higher priority than cc3 */
310 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
)) != 0) {
317 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
319 CPUS390XState
*env
= &cpu
->env
;
320 int cssid
, ssid
, schid
, m
;
327 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
328 program_interrupt(env
, PGM_OPERAND
, 4);
331 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
332 addr
= decode_basedisp_s(env
, ipb
, &ar
);
334 program_interrupt(env
, PGM_SPECIFICATION
, 4);
338 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
339 if (sch
&& css_subch_visible(sch
)) {
340 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
344 /* 0 - status pending, 1 - not status pending, 3 - not operational */
346 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &irb
, irb_len
) != 0) {
349 css_do_tsch_update_subch(sch
);
351 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
352 /* Access exceptions have a higher priority than cc3 */
353 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, irb_len
) != 0) {
362 typedef struct ChscReq
{
368 } QEMU_PACKED ChscReq
;
370 typedef struct ChscResp
{
375 } QEMU_PACKED ChscResp
;
377 #define CHSC_MIN_RESP_LEN 0x0008
379 #define CHSC_SCPD 0x0002
380 #define CHSC_SCSC 0x0010
381 #define CHSC_SDA 0x0031
382 #define CHSC_SEI 0x000e
384 #define CHSC_SCPD_0_M 0x20000000
385 #define CHSC_SCPD_0_C 0x10000000
386 #define CHSC_SCPD_0_FMT 0x0f000000
387 #define CHSC_SCPD_0_CSSID 0x00ff0000
388 #define CHSC_SCPD_0_RFMT 0x00000f00
389 #define CHSC_SCPD_0_RES 0xc000f000
390 #define CHSC_SCPD_1_RES 0xffffff00
391 #define CHSC_SCPD_01_CHPID 0x000000ff
392 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
394 uint16_t len
= be16_to_cpu(req
->len
);
395 uint32_t param0
= be32_to_cpu(req
->param0
);
396 uint32_t param1
= be32_to_cpu(req
->param1
);
400 uint8_t f_chpid
, l_chpid
;
404 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
405 if ((rfmt
== 0) || (rfmt
== 1)) {
406 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
408 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
409 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
413 if (param0
& CHSC_SCPD_0_FMT
) {
417 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
418 m
= param0
& CHSC_SCPD_0_M
;
420 if (!m
|| !css_present(cssid
)) {
425 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
426 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
427 if (l_chpid
< f_chpid
) {
431 /* css_collect_chp_desc() is endian-aware */
432 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
434 res
->code
= cpu_to_be16(0x0001);
435 res
->len
= cpu_to_be16(8 + desc_size
);
436 res
->param
= cpu_to_be32(rfmt
);
440 res
->code
= cpu_to_be16(resp_code
);
441 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
442 res
->param
= cpu_to_be32(rfmt
);
445 #define CHSC_SCSC_0_M 0x20000000
446 #define CHSC_SCSC_0_FMT 0x000f0000
447 #define CHSC_SCSC_0_CSSID 0x0000ff00
448 #define CHSC_SCSC_0_RES 0xdff000ff
449 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
451 uint16_t len
= be16_to_cpu(req
->len
);
452 uint32_t param0
= be32_to_cpu(req
->param0
);
455 uint32_t general_chars
[510];
456 uint32_t chsc_chars
[508];
463 if (param0
& CHSC_SCSC_0_FMT
) {
467 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
469 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
474 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
478 res
->code
= cpu_to_be16(0x0001);
479 res
->len
= cpu_to_be16(4080);
482 memset(general_chars
, 0, sizeof(general_chars
));
483 memset(chsc_chars
, 0, sizeof(chsc_chars
));
485 general_chars
[0] = cpu_to_be32(0x03000000);
486 general_chars
[1] = cpu_to_be32(0x00079000);
487 general_chars
[3] = cpu_to_be32(0x00080000);
489 chsc_chars
[0] = cpu_to_be32(0x40000000);
490 chsc_chars
[3] = cpu_to_be32(0x00040000);
492 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
493 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
497 res
->code
= cpu_to_be16(resp_code
);
498 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
502 #define CHSC_SDA_0_FMT 0x0f000000
503 #define CHSC_SDA_0_OC 0x0000ffff
504 #define CHSC_SDA_0_RES 0xf0ff0000
505 #define CHSC_SDA_OC_MCSSE 0x0
506 #define CHSC_SDA_OC_MSS 0x2
507 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
509 uint16_t resp_code
= 0x0001;
510 uint16_t len
= be16_to_cpu(req
->len
);
511 uint32_t param0
= be32_to_cpu(req
->param0
);
515 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
520 if (param0
& CHSC_SDA_0_FMT
) {
525 oc
= param0
& CHSC_SDA_0_OC
;
527 case CHSC_SDA_OC_MCSSE
:
528 ret
= css_enable_mcsse();
529 if (ret
== -EINVAL
) {
534 case CHSC_SDA_OC_MSS
:
535 ret
= css_enable_mss();
536 if (ret
== -EINVAL
) {
547 res
->code
= cpu_to_be16(resp_code
);
548 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
552 static int chsc_sei_nt0_get_event(void *res
)
558 static int chsc_sei_nt0_have_event(void)
564 static int chsc_sei_nt2_get_event(void *res
)
566 if (s390_has_feat(S390_FEAT_ZPCI
)) {
567 return pci_chsc_sei_nt2_get_event(res
);
572 static int chsc_sei_nt2_have_event(void)
574 if (s390_has_feat(S390_FEAT_ZPCI
)) {
575 return pci_chsc_sei_nt2_have_event();
580 #define CHSC_SEI_NT0 (1ULL << 63)
581 #define CHSC_SEI_NT2 (1ULL << 61)
582 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
584 uint64_t selection_mask
= ldq_p(&req
->param1
);
585 uint8_t *res_flags
= (uint8_t *)res
->data
;
589 /* regarding architecture nt0 can not be masked */
590 have_event
= !chsc_sei_nt0_get_event(res
);
591 have_more
= chsc_sei_nt0_have_event();
593 if (selection_mask
& CHSC_SEI_NT2
) {
595 have_event
= !chsc_sei_nt2_get_event(res
);
599 have_more
= chsc_sei_nt2_have_event();
604 res
->code
= cpu_to_be16(0x0001);
606 (*res_flags
) |= 0x80;
608 (*res_flags
) &= ~0x80;
609 css_clear_sei_pending();
612 res
->code
= cpu_to_be16(0x0005);
613 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
617 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
619 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
620 res
->code
= cpu_to_be16(0x0004);
624 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
632 CPUS390XState
*env
= &cpu
->env
;
633 uint8_t buf
[TARGET_PAGE_SIZE
];
635 trace_ioinst("chsc");
636 reg
= (ipb
>> 20) & 0x00f;
637 addr
= env
->regs
[reg
];
640 program_interrupt(env
, PGM_SPECIFICATION
, 4);
644 * Reading sizeof(ChscReq) bytes is currently enough for all of our
645 * present CHSC sub-handlers ... if we ever need more, we should take
646 * care of req->len here first.
648 if (s390_cpu_virt_mem_read(cpu
, addr
, reg
, buf
, sizeof(ChscReq
))) {
651 req
= (ChscReq
*)buf
;
652 len
= be16_to_cpu(req
->len
);
653 /* Length field valid? */
654 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
655 program_interrupt(env
, PGM_OPERAND
, 4);
658 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
659 res
= (void *)((char *)req
+ len
);
660 command
= be16_to_cpu(req
->command
);
661 trace_ioinst_chsc_cmd(command
, len
);
664 ioinst_handle_chsc_scsc(req
, res
);
667 ioinst_handle_chsc_scpd(req
, res
);
670 ioinst_handle_chsc_sda(req
, res
);
673 ioinst_handle_chsc_sei(req
, res
);
676 ioinst_handle_chsc_unimplemented(res
);
680 if (!s390_cpu_virt_mem_write(cpu
, addr
+ len
, reg
, res
,
681 be16_to_cpu(res
->len
))) {
682 setcc(cpu
, 0); /* Command execution complete */
686 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
688 CPUS390XState
*env
= &cpu
->env
;
697 addr
= decode_basedisp_s(env
, ipb
, &ar
);
699 program_interrupt(env
, PGM_SPECIFICATION
, 4);
703 lowcore
= addr
? 0 : 1;
704 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
705 ret
= css_do_tpi(&int_code
, lowcore
);
707 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, ar
, &int_code
, len
);
712 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
713 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
714 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
715 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
717 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
723 CPUS390XState
*env
= &cpu
->env
;
725 trace_ioinst("schm");
727 if (SCHM_REG1_RES(reg1
)) {
728 program_interrupt(env
, PGM_OPERAND
, 4);
732 mbk
= SCHM_REG1_MBK(reg1
);
733 update
= SCHM_REG1_UPD(reg1
);
734 dct
= SCHM_REG1_DCT(reg1
);
736 if (update
&& (reg2
& 0x000000000000001f)) {
737 program_interrupt(env
, PGM_OPERAND
, 4);
741 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
744 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
746 int cssid
, ssid
, schid
, m
;
749 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
750 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
753 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
754 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
755 if (!sch
|| !css_subch_visible(sch
)) {
759 setcc(cpu
, css_do_rsch(sch
));
762 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
763 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
764 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
765 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
771 CPUS390XState
*env
= &cpu
->env
;
773 if (RCHP_REG1_RES(reg1
)) {
774 program_interrupt(env
, PGM_OPERAND
, 4);
778 cssid
= RCHP_REG1_CSSID(reg1
);
779 chpid
= RCHP_REG1_CHPID(reg1
);
781 trace_ioinst_chp_id("rchp", cssid
, chpid
);
783 ret
= css_do_rchp(cssid
, chpid
);
796 /* Invalid channel subsystem. */
797 program_interrupt(env
, PGM_OPERAND
, 4);
803 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
804 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
806 /* We do not provide address limit checking, so let's suppress it. */
807 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
808 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);