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"
15 #include "hw/s390x/ioinst.h"
17 #include "hw/s390x/s390-pci-bus.h"
19 int ioinst_disassemble_sch_ident(uint32_t value
, int *m
, int *cssid
, int *ssid
,
22 if (!IOINST_SCHID_ONE(value
)) {
25 if (!IOINST_SCHID_M(value
)) {
26 if (IOINST_SCHID_CSSID(value
)) {
32 *cssid
= IOINST_SCHID_CSSID(value
);
35 *ssid
= IOINST_SCHID_SSID(value
);
36 *schid
= IOINST_SCHID_NR(value
);
40 void ioinst_handle_xsch(S390CPU
*cpu
, uint64_t reg1
)
42 int cssid
, ssid
, schid
, m
;
47 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
48 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
51 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
52 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
53 if (sch
&& css_subch_visible(sch
)) {
54 ret
= css_do_xsch(sch
);
73 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
75 int cssid
, ssid
, schid
, m
;
80 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
81 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
84 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
85 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
86 if (sch
&& css_subch_visible(sch
)) {
87 ret
= css_do_csch(sch
);
97 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
99 int cssid
, ssid
, schid
, m
;
104 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
105 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
108 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
109 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
110 if (sch
&& css_subch_visible(sch
)) {
111 ret
= css_do_hsch(sch
);
130 static int ioinst_schib_valid(SCHIB
*schib
)
132 if ((be16_to_cpu(schib
->pmcw
.flags
) & PMCW_FLAGS_MASK_INVALID
) ||
133 (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_INVALID
)) {
136 /* Disallow extended measurements for now. */
137 if (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_XMWME
) {
143 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
145 int cssid
, ssid
, schid
, m
;
151 CPUS390XState
*env
= &cpu
->env
;
154 addr
= decode_basedisp_s(env
, ipb
, &ar
);
156 program_interrupt(env
, PGM_SPECIFICATION
, 2);
159 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &schib
, sizeof(schib
))) {
162 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
163 !ioinst_schib_valid(&schib
)) {
164 program_interrupt(env
, PGM_OPERAND
, 2);
167 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
168 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
169 if (sch
&& css_subch_visible(sch
)) {
170 ret
= css_do_msch(sch
, &schib
);
189 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
191 dest
->intparm
= be32_to_cpu(src
->intparm
);
192 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
193 dest
->lpm
= src
->lpm
;
194 dest
->ctrl1
= src
->ctrl1
;
195 dest
->cpa
= be32_to_cpu(src
->cpa
);
198 static int ioinst_orb_valid(ORB
*orb
)
200 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
201 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
204 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
210 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
212 int cssid
, ssid
, schid
, m
;
218 CPUS390XState
*env
= &cpu
->env
;
221 addr
= decode_basedisp_s(env
, ipb
, &ar
);
223 program_interrupt(env
, PGM_SPECIFICATION
, 2);
226 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &orig_orb
, sizeof(orb
))) {
229 copy_orb_from_guest(&orb
, &orig_orb
);
230 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
231 !ioinst_orb_valid(&orb
)) {
232 program_interrupt(env
, PGM_OPERAND
, 2);
235 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
236 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
237 if (sch
&& css_subch_visible(sch
)) {
238 ret
= css_do_ssch(sch
, &orb
);
250 * I'm wondering whether there is something better
251 * to do for us here (like setting some device or
252 * subchannel status).
254 program_interrupt(env
, PGM_ADDRESSING
, 4);
266 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
271 CPUS390XState
*env
= &cpu
->env
;
274 addr
= decode_basedisp_s(env
, ipb
, &ar
);
276 program_interrupt(env
, PGM_SPECIFICATION
, 2);
280 cc
= css_do_stcrw(&crw
);
281 /* 0 - crw stored, 1 - zeroes stored */
283 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &crw
, sizeof(crw
)) == 0) {
285 } else if (cc
== 0) {
286 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
287 css_undo_stcrw(&crw
);
291 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
293 int cssid
, ssid
, schid
, m
;
298 CPUS390XState
*env
= &cpu
->env
;
301 addr
= decode_basedisp_s(env
, ipb
, &ar
);
303 program_interrupt(env
, PGM_SPECIFICATION
, 2);
307 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
309 * As operand exceptions have a lower priority than access exceptions,
310 * we check whether the memory area is writeable (injecting the
311 * access execption if it is not) first.
313 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
))) {
314 program_interrupt(env
, PGM_OPERAND
, 2);
318 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
319 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
321 if (css_subch_visible(sch
)) {
322 css_do_stsch(sch
, &schib
);
325 /* Indicate no more subchannels in this css/ss */
329 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
330 cc
= 3; /* No more subchannels in this css/ss */
332 /* Store an empty schib. */
333 memset(&schib
, 0, sizeof(schib
));
338 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &schib
,
339 sizeof(schib
)) != 0) {
343 /* Access exceptions have a higher priority than cc3 */
344 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
)) != 0) {
351 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
353 CPUS390XState
*env
= &cpu
->env
;
354 int cssid
, ssid
, schid
, m
;
361 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
362 program_interrupt(env
, PGM_OPERAND
, 2);
365 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
366 addr
= decode_basedisp_s(env
, ipb
, &ar
);
368 program_interrupt(env
, PGM_SPECIFICATION
, 2);
372 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
373 if (sch
&& css_subch_visible(sch
)) {
374 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
378 /* 0 - status pending, 1 - not status pending, 3 - not operational */
380 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &irb
, irb_len
) != 0) {
383 css_do_tsch_update_subch(sch
);
385 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
386 /* Access exceptions have a higher priority than cc3 */
387 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, irb_len
) != 0) {
396 typedef struct ChscReq
{
402 } QEMU_PACKED ChscReq
;
404 typedef struct ChscResp
{
409 } QEMU_PACKED ChscResp
;
411 #define CHSC_MIN_RESP_LEN 0x0008
413 #define CHSC_SCPD 0x0002
414 #define CHSC_SCSC 0x0010
415 #define CHSC_SDA 0x0031
416 #define CHSC_SEI 0x000e
418 #define CHSC_SCPD_0_M 0x20000000
419 #define CHSC_SCPD_0_C 0x10000000
420 #define CHSC_SCPD_0_FMT 0x0f000000
421 #define CHSC_SCPD_0_CSSID 0x00ff0000
422 #define CHSC_SCPD_0_RFMT 0x00000f00
423 #define CHSC_SCPD_0_RES 0xc000f000
424 #define CHSC_SCPD_1_RES 0xffffff00
425 #define CHSC_SCPD_01_CHPID 0x000000ff
426 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
428 uint16_t len
= be16_to_cpu(req
->len
);
429 uint32_t param0
= be32_to_cpu(req
->param0
);
430 uint32_t param1
= be32_to_cpu(req
->param1
);
434 uint8_t f_chpid
, l_chpid
;
438 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
439 if ((rfmt
== 0) || (rfmt
== 1)) {
440 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
442 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
443 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
447 if (param0
& CHSC_SCPD_0_FMT
) {
451 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
452 m
= param0
& CHSC_SCPD_0_M
;
454 if (!m
|| !css_present(cssid
)) {
459 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
460 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
461 if (l_chpid
< f_chpid
) {
465 /* css_collect_chp_desc() is endian-aware */
466 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
468 res
->code
= cpu_to_be16(0x0001);
469 res
->len
= cpu_to_be16(8 + desc_size
);
470 res
->param
= cpu_to_be32(rfmt
);
474 res
->code
= cpu_to_be16(resp_code
);
475 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
476 res
->param
= cpu_to_be32(rfmt
);
479 #define CHSC_SCSC_0_M 0x20000000
480 #define CHSC_SCSC_0_FMT 0x000f0000
481 #define CHSC_SCSC_0_CSSID 0x0000ff00
482 #define CHSC_SCSC_0_RES 0xdff000ff
483 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
485 uint16_t len
= be16_to_cpu(req
->len
);
486 uint32_t param0
= be32_to_cpu(req
->param0
);
489 uint32_t general_chars
[510];
490 uint32_t chsc_chars
[508];
497 if (param0
& CHSC_SCSC_0_FMT
) {
501 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
503 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
508 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
512 res
->code
= cpu_to_be16(0x0001);
513 res
->len
= cpu_to_be16(4080);
516 memset(general_chars
, 0, sizeof(general_chars
));
517 memset(chsc_chars
, 0, sizeof(chsc_chars
));
519 general_chars
[0] = cpu_to_be32(0x03000000);
520 general_chars
[1] = cpu_to_be32(0x00079000);
521 general_chars
[3] = cpu_to_be32(0x00080000);
523 chsc_chars
[0] = cpu_to_be32(0x40000000);
524 chsc_chars
[3] = cpu_to_be32(0x00040000);
526 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
527 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
531 res
->code
= cpu_to_be16(resp_code
);
532 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
536 #define CHSC_SDA_0_FMT 0x0f000000
537 #define CHSC_SDA_0_OC 0x0000ffff
538 #define CHSC_SDA_0_RES 0xf0ff0000
539 #define CHSC_SDA_OC_MCSSE 0x0
540 #define CHSC_SDA_OC_MSS 0x2
541 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
543 uint16_t resp_code
= 0x0001;
544 uint16_t len
= be16_to_cpu(req
->len
);
545 uint32_t param0
= be32_to_cpu(req
->param0
);
549 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
554 if (param0
& CHSC_SDA_0_FMT
) {
559 oc
= param0
& CHSC_SDA_0_OC
;
561 case CHSC_SDA_OC_MCSSE
:
562 ret
= css_enable_mcsse();
563 if (ret
== -EINVAL
) {
568 case CHSC_SDA_OC_MSS
:
569 ret
= css_enable_mss();
570 if (ret
== -EINVAL
) {
581 res
->code
= cpu_to_be16(resp_code
);
582 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
586 static int chsc_sei_nt0_get_event(void *res
)
592 static int chsc_sei_nt0_have_event(void)
598 #define CHSC_SEI_NT0 (1ULL << 63)
599 #define CHSC_SEI_NT2 (1ULL << 61)
600 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
602 uint64_t selection_mask
= ldq_p(&req
->param1
);
603 uint8_t *res_flags
= (uint8_t *)res
->data
;
607 /* regarding architecture nt0 can not be masked */
608 have_event
= !chsc_sei_nt0_get_event(res
);
609 have_more
= chsc_sei_nt0_have_event();
611 if (selection_mask
& CHSC_SEI_NT2
) {
613 have_event
= !chsc_sei_nt2_get_event(res
);
617 have_more
= chsc_sei_nt2_have_event();
622 res
->code
= cpu_to_be16(0x0001);
624 (*res_flags
) |= 0x80;
626 (*res_flags
) &= ~0x80;
627 css_clear_sei_pending();
630 res
->code
= cpu_to_be16(0x0005);
631 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
635 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
637 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
638 res
->code
= cpu_to_be16(0x0004);
642 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
650 CPUS390XState
*env
= &cpu
->env
;
651 uint8_t buf
[TARGET_PAGE_SIZE
];
653 trace_ioinst("chsc");
654 reg
= (ipb
>> 20) & 0x00f;
655 addr
= env
->regs
[reg
];
658 program_interrupt(env
, PGM_SPECIFICATION
, 2);
662 * Reading sizeof(ChscReq) bytes is currently enough for all of our
663 * present CHSC sub-handlers ... if we ever need more, we should take
664 * care of req->len here first.
666 if (s390_cpu_virt_mem_read(cpu
, addr
, reg
, buf
, sizeof(ChscReq
))) {
669 req
= (ChscReq
*)buf
;
670 len
= be16_to_cpu(req
->len
);
671 /* Length field valid? */
672 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
673 program_interrupt(env
, PGM_OPERAND
, 2);
676 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
677 res
= (void *)((char *)req
+ len
);
678 command
= be16_to_cpu(req
->command
);
679 trace_ioinst_chsc_cmd(command
, len
);
682 ioinst_handle_chsc_scsc(req
, res
);
685 ioinst_handle_chsc_scpd(req
, res
);
688 ioinst_handle_chsc_sda(req
, res
);
691 ioinst_handle_chsc_sei(req
, res
);
694 ioinst_handle_chsc_unimplemented(res
);
698 if (!s390_cpu_virt_mem_write(cpu
, addr
+ len
, reg
, res
,
699 be16_to_cpu(res
->len
))) {
700 setcc(cpu
, 0); /* Command execution complete */
704 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
706 CPUS390XState
*env
= &cpu
->env
;
715 addr
= decode_basedisp_s(env
, ipb
, &ar
);
717 program_interrupt(env
, PGM_SPECIFICATION
, 2);
721 lowcore
= addr
? 0 : 1;
722 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
723 ret
= css_do_tpi(&int_code
, lowcore
);
725 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, ar
, &int_code
, len
);
730 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
731 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
732 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
733 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
735 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
741 CPUS390XState
*env
= &cpu
->env
;
743 trace_ioinst("schm");
745 if (SCHM_REG1_RES(reg1
)) {
746 program_interrupt(env
, PGM_OPERAND
, 2);
750 mbk
= SCHM_REG1_MBK(reg1
);
751 update
= SCHM_REG1_UPD(reg1
);
752 dct
= SCHM_REG1_DCT(reg1
);
754 if (update
&& (reg2
& 0x000000000000001f)) {
755 program_interrupt(env
, PGM_OPERAND
, 2);
759 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
762 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
764 int cssid
, ssid
, schid
, m
;
769 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
770 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
773 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
774 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
775 if (sch
&& css_subch_visible(sch
)) {
776 ret
= css_do_rsch(sch
);
795 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
796 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
797 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
798 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
804 CPUS390XState
*env
= &cpu
->env
;
806 if (RCHP_REG1_RES(reg1
)) {
807 program_interrupt(env
, PGM_OPERAND
, 2);
811 cssid
= RCHP_REG1_CSSID(reg1
);
812 chpid
= RCHP_REG1_CHPID(reg1
);
814 trace_ioinst_chp_id("rchp", cssid
, chpid
);
816 ret
= css_do_rchp(cssid
, chpid
);
829 /* Invalid channel subsystem. */
830 program_interrupt(env
, PGM_OPERAND
, 2);
836 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
837 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
839 /* We do not provide address limit checking, so let's suppress it. */
840 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
841 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);