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
;
48 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
49 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
52 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
53 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
54 if (sch
&& css_subch_visible(sch
)) {
55 ret
= css_do_xsch(sch
);
74 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
76 int cssid
, ssid
, schid
, m
;
81 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
82 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
85 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
86 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
87 if (sch
&& css_subch_visible(sch
)) {
88 ret
= css_do_csch(sch
);
98 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
100 int cssid
, ssid
, schid
, m
;
105 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
106 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
109 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
110 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
111 if (sch
&& css_subch_visible(sch
)) {
112 ret
= css_do_hsch(sch
);
131 static int ioinst_schib_valid(SCHIB
*schib
)
133 if ((be16_to_cpu(schib
->pmcw
.flags
) & PMCW_FLAGS_MASK_INVALID
) ||
134 (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_INVALID
)) {
137 /* Disallow extended measurements for now. */
138 if (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_XMWME
) {
144 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
146 int cssid
, ssid
, schid
, m
;
152 CPUS390XState
*env
= &cpu
->env
;
155 addr
= decode_basedisp_s(env
, ipb
, &ar
);
157 program_interrupt(env
, PGM_SPECIFICATION
, 4);
160 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &schib
, sizeof(schib
))) {
163 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
164 !ioinst_schib_valid(&schib
)) {
165 program_interrupt(env
, PGM_OPERAND
, 4);
168 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
169 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
170 if (sch
&& css_subch_visible(sch
)) {
171 ret
= css_do_msch(sch
, &schib
);
190 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
192 dest
->intparm
= be32_to_cpu(src
->intparm
);
193 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
194 dest
->lpm
= src
->lpm
;
195 dest
->ctrl1
= src
->ctrl1
;
196 dest
->cpa
= be32_to_cpu(src
->cpa
);
199 static int ioinst_orb_valid(ORB
*orb
)
201 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
202 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
205 /* We don't support MIDA. */
206 if (orb
->ctrl1
& ORB_CTRL1_MASK_MIDAW
) {
209 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
215 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
217 int cssid
, ssid
, schid
, m
;
223 CPUS390XState
*env
= &cpu
->env
;
226 addr
= decode_basedisp_s(env
, ipb
, &ar
);
228 program_interrupt(env
, PGM_SPECIFICATION
, 4);
231 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &orig_orb
, sizeof(orb
))) {
234 copy_orb_from_guest(&orb
, &orig_orb
);
235 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
236 !ioinst_orb_valid(&orb
)) {
237 program_interrupt(env
, PGM_OPERAND
, 4);
240 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
241 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
242 if (sch
&& css_subch_visible(sch
)) {
243 ret
= css_do_ssch(sch
, &orb
);
255 * I'm wondering whether there is something better
256 * to do for us here (like setting some device or
257 * subchannel status).
259 program_interrupt(env
, PGM_ADDRESSING
, 4);
271 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
276 CPUS390XState
*env
= &cpu
->env
;
279 addr
= decode_basedisp_s(env
, ipb
, &ar
);
281 program_interrupt(env
, PGM_SPECIFICATION
, 4);
285 cc
= css_do_stcrw(&crw
);
286 /* 0 - crw stored, 1 - zeroes stored */
288 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &crw
, sizeof(crw
)) == 0) {
290 } else if (cc
== 0) {
291 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
292 css_undo_stcrw(&crw
);
296 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
298 int cssid
, ssid
, schid
, m
;
303 CPUS390XState
*env
= &cpu
->env
;
306 addr
= decode_basedisp_s(env
, ipb
, &ar
);
308 program_interrupt(env
, PGM_SPECIFICATION
, 4);
312 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
314 * As operand exceptions have a lower priority than access exceptions,
315 * we check whether the memory area is writeable (injecting the
316 * access execption if it is not) first.
318 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
))) {
319 program_interrupt(env
, PGM_OPERAND
, 4);
323 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
324 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
326 if (css_subch_visible(sch
)) {
327 css_do_stsch(sch
, &schib
);
330 /* Indicate no more subchannels in this css/ss */
334 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
335 cc
= 3; /* No more subchannels in this css/ss */
337 /* Store an empty schib. */
338 memset(&schib
, 0, sizeof(schib
));
343 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &schib
,
344 sizeof(schib
)) != 0) {
348 /* Access exceptions have a higher priority than cc3 */
349 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
)) != 0) {
356 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
358 CPUS390XState
*env
= &cpu
->env
;
359 int cssid
, ssid
, schid
, m
;
366 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
367 program_interrupt(env
, PGM_OPERAND
, 4);
370 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
371 addr
= decode_basedisp_s(env
, ipb
, &ar
);
373 program_interrupt(env
, PGM_SPECIFICATION
, 4);
377 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
378 if (sch
&& css_subch_visible(sch
)) {
379 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
383 /* 0 - status pending, 1 - not status pending, 3 - not operational */
385 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &irb
, irb_len
) != 0) {
388 css_do_tsch_update_subch(sch
);
390 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
391 /* Access exceptions have a higher priority than cc3 */
392 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, irb_len
) != 0) {
401 typedef struct ChscReq
{
407 } QEMU_PACKED ChscReq
;
409 typedef struct ChscResp
{
414 } QEMU_PACKED ChscResp
;
416 #define CHSC_MIN_RESP_LEN 0x0008
418 #define CHSC_SCPD 0x0002
419 #define CHSC_SCSC 0x0010
420 #define CHSC_SDA 0x0031
421 #define CHSC_SEI 0x000e
423 #define CHSC_SCPD_0_M 0x20000000
424 #define CHSC_SCPD_0_C 0x10000000
425 #define CHSC_SCPD_0_FMT 0x0f000000
426 #define CHSC_SCPD_0_CSSID 0x00ff0000
427 #define CHSC_SCPD_0_RFMT 0x00000f00
428 #define CHSC_SCPD_0_RES 0xc000f000
429 #define CHSC_SCPD_1_RES 0xffffff00
430 #define CHSC_SCPD_01_CHPID 0x000000ff
431 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
433 uint16_t len
= be16_to_cpu(req
->len
);
434 uint32_t param0
= be32_to_cpu(req
->param0
);
435 uint32_t param1
= be32_to_cpu(req
->param1
);
439 uint8_t f_chpid
, l_chpid
;
443 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
444 if ((rfmt
== 0) || (rfmt
== 1)) {
445 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
447 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
448 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
452 if (param0
& CHSC_SCPD_0_FMT
) {
456 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
457 m
= param0
& CHSC_SCPD_0_M
;
459 if (!m
|| !css_present(cssid
)) {
464 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
465 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
466 if (l_chpid
< f_chpid
) {
470 /* css_collect_chp_desc() is endian-aware */
471 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
473 res
->code
= cpu_to_be16(0x0001);
474 res
->len
= cpu_to_be16(8 + desc_size
);
475 res
->param
= cpu_to_be32(rfmt
);
479 res
->code
= cpu_to_be16(resp_code
);
480 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
481 res
->param
= cpu_to_be32(rfmt
);
484 #define CHSC_SCSC_0_M 0x20000000
485 #define CHSC_SCSC_0_FMT 0x000f0000
486 #define CHSC_SCSC_0_CSSID 0x0000ff00
487 #define CHSC_SCSC_0_RES 0xdff000ff
488 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
490 uint16_t len
= be16_to_cpu(req
->len
);
491 uint32_t param0
= be32_to_cpu(req
->param0
);
494 uint32_t general_chars
[510];
495 uint32_t chsc_chars
[508];
502 if (param0
& CHSC_SCSC_0_FMT
) {
506 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
508 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
513 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
517 res
->code
= cpu_to_be16(0x0001);
518 res
->len
= cpu_to_be16(4080);
521 memset(general_chars
, 0, sizeof(general_chars
));
522 memset(chsc_chars
, 0, sizeof(chsc_chars
));
524 general_chars
[0] = cpu_to_be32(0x03000000);
525 general_chars
[1] = cpu_to_be32(0x00079000);
526 general_chars
[3] = cpu_to_be32(0x00080000);
528 chsc_chars
[0] = cpu_to_be32(0x40000000);
529 chsc_chars
[3] = cpu_to_be32(0x00040000);
531 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
532 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
536 res
->code
= cpu_to_be16(resp_code
);
537 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
541 #define CHSC_SDA_0_FMT 0x0f000000
542 #define CHSC_SDA_0_OC 0x0000ffff
543 #define CHSC_SDA_0_RES 0xf0ff0000
544 #define CHSC_SDA_OC_MCSSE 0x0
545 #define CHSC_SDA_OC_MSS 0x2
546 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
548 uint16_t resp_code
= 0x0001;
549 uint16_t len
= be16_to_cpu(req
->len
);
550 uint32_t param0
= be32_to_cpu(req
->param0
);
554 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
559 if (param0
& CHSC_SDA_0_FMT
) {
564 oc
= param0
& CHSC_SDA_0_OC
;
566 case CHSC_SDA_OC_MCSSE
:
567 ret
= css_enable_mcsse();
568 if (ret
== -EINVAL
) {
573 case CHSC_SDA_OC_MSS
:
574 ret
= css_enable_mss();
575 if (ret
== -EINVAL
) {
586 res
->code
= cpu_to_be16(resp_code
);
587 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
591 static int chsc_sei_nt0_get_event(void *res
)
597 static int chsc_sei_nt0_have_event(void)
603 static int chsc_sei_nt2_get_event(void *res
)
605 if (s390_has_feat(S390_FEAT_ZPCI
)) {
606 return pci_chsc_sei_nt2_get_event(res
);
611 static int chsc_sei_nt2_have_event(void)
613 if (s390_has_feat(S390_FEAT_ZPCI
)) {
614 return pci_chsc_sei_nt2_have_event();
619 #define CHSC_SEI_NT0 (1ULL << 63)
620 #define CHSC_SEI_NT2 (1ULL << 61)
621 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
623 uint64_t selection_mask
= ldq_p(&req
->param1
);
624 uint8_t *res_flags
= (uint8_t *)res
->data
;
628 /* regarding architecture nt0 can not be masked */
629 have_event
= !chsc_sei_nt0_get_event(res
);
630 have_more
= chsc_sei_nt0_have_event();
632 if (selection_mask
& CHSC_SEI_NT2
) {
634 have_event
= !chsc_sei_nt2_get_event(res
);
638 have_more
= chsc_sei_nt2_have_event();
643 res
->code
= cpu_to_be16(0x0001);
645 (*res_flags
) |= 0x80;
647 (*res_flags
) &= ~0x80;
648 css_clear_sei_pending();
651 res
->code
= cpu_to_be16(0x0005);
652 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
656 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
658 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
659 res
->code
= cpu_to_be16(0x0004);
663 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
671 CPUS390XState
*env
= &cpu
->env
;
672 uint8_t buf
[TARGET_PAGE_SIZE
];
674 trace_ioinst("chsc");
675 reg
= (ipb
>> 20) & 0x00f;
676 addr
= env
->regs
[reg
];
679 program_interrupt(env
, PGM_SPECIFICATION
, 4);
683 * Reading sizeof(ChscReq) bytes is currently enough for all of our
684 * present CHSC sub-handlers ... if we ever need more, we should take
685 * care of req->len here first.
687 if (s390_cpu_virt_mem_read(cpu
, addr
, reg
, buf
, sizeof(ChscReq
))) {
690 req
= (ChscReq
*)buf
;
691 len
= be16_to_cpu(req
->len
);
692 /* Length field valid? */
693 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
694 program_interrupt(env
, PGM_OPERAND
, 4);
697 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
698 res
= (void *)((char *)req
+ len
);
699 command
= be16_to_cpu(req
->command
);
700 trace_ioinst_chsc_cmd(command
, len
);
703 ioinst_handle_chsc_scsc(req
, res
);
706 ioinst_handle_chsc_scpd(req
, res
);
709 ioinst_handle_chsc_sda(req
, res
);
712 ioinst_handle_chsc_sei(req
, res
);
715 ioinst_handle_chsc_unimplemented(res
);
719 if (!s390_cpu_virt_mem_write(cpu
, addr
+ len
, reg
, res
,
720 be16_to_cpu(res
->len
))) {
721 setcc(cpu
, 0); /* Command execution complete */
725 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
727 CPUS390XState
*env
= &cpu
->env
;
736 addr
= decode_basedisp_s(env
, ipb
, &ar
);
738 program_interrupt(env
, PGM_SPECIFICATION
, 4);
742 lowcore
= addr
? 0 : 1;
743 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
744 ret
= css_do_tpi(&int_code
, lowcore
);
746 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, ar
, &int_code
, len
);
751 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
752 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
753 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
754 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
756 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
762 CPUS390XState
*env
= &cpu
->env
;
764 trace_ioinst("schm");
766 if (SCHM_REG1_RES(reg1
)) {
767 program_interrupt(env
, PGM_OPERAND
, 4);
771 mbk
= SCHM_REG1_MBK(reg1
);
772 update
= SCHM_REG1_UPD(reg1
);
773 dct
= SCHM_REG1_DCT(reg1
);
775 if (update
&& (reg2
& 0x000000000000001f)) {
776 program_interrupt(env
, PGM_OPERAND
, 4);
780 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
783 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
785 int cssid
, ssid
, schid
, m
;
790 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
791 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
794 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
795 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
796 if (sch
&& css_subch_visible(sch
)) {
797 ret
= css_do_rsch(sch
);
816 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
817 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
818 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
819 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
825 CPUS390XState
*env
= &cpu
->env
;
827 if (RCHP_REG1_RES(reg1
)) {
828 program_interrupt(env
, PGM_OPERAND
, 4);
832 cssid
= RCHP_REG1_CSSID(reg1
);
833 chpid
= RCHP_REG1_CHPID(reg1
);
835 trace_ioinst_chp_id("rchp", cssid
, chpid
);
837 ret
= css_do_rchp(cssid
, chpid
);
850 /* Invalid channel subsystem. */
851 program_interrupt(env
, PGM_OPERAND
, 4);
857 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
858 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
860 /* We do not provide address limit checking, so let's suppress it. */
861 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
862 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);