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
;
221 CPUS390XState
*env
= &cpu
->env
;
224 addr
= decode_basedisp_s(env
, ipb
, &ar
);
226 program_interrupt(env
, PGM_SPECIFICATION
, 4);
229 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &orig_orb
, sizeof(orb
))) {
232 copy_orb_from_guest(&orb
, &orig_orb
);
233 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
234 !ioinst_orb_valid(&orb
)) {
235 program_interrupt(env
, PGM_OPERAND
, 4);
238 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
239 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
240 if (!sch
|| !css_subch_visible(sch
)) {
244 setcc(cpu
, css_do_ssch(sch
, &orb
));
247 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
252 CPUS390XState
*env
= &cpu
->env
;
255 addr
= decode_basedisp_s(env
, ipb
, &ar
);
257 program_interrupt(env
, PGM_SPECIFICATION
, 4);
261 cc
= css_do_stcrw(&crw
);
262 /* 0 - crw stored, 1 - zeroes stored */
264 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &crw
, sizeof(crw
)) == 0) {
266 } else if (cc
== 0) {
267 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
268 css_undo_stcrw(&crw
);
272 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
274 int cssid
, ssid
, schid
, m
;
279 CPUS390XState
*env
= &cpu
->env
;
282 addr
= decode_basedisp_s(env
, ipb
, &ar
);
284 program_interrupt(env
, PGM_SPECIFICATION
, 4);
288 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
290 * As operand exceptions have a lower priority than access exceptions,
291 * we check whether the memory area is writeable (injecting the
292 * access execption if it is not) first.
294 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
))) {
295 program_interrupt(env
, PGM_OPERAND
, 4);
299 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
300 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
302 if (css_subch_visible(sch
)) {
303 css_do_stsch(sch
, &schib
);
306 /* Indicate no more subchannels in this css/ss */
310 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
311 cc
= 3; /* No more subchannels in this css/ss */
313 /* Store an empty schib. */
314 memset(&schib
, 0, sizeof(schib
));
319 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &schib
,
320 sizeof(schib
)) != 0) {
324 /* Access exceptions have a higher priority than cc3 */
325 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
)) != 0) {
332 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
334 CPUS390XState
*env
= &cpu
->env
;
335 int cssid
, ssid
, schid
, m
;
342 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
343 program_interrupt(env
, PGM_OPERAND
, 4);
346 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
347 addr
= decode_basedisp_s(env
, ipb
, &ar
);
349 program_interrupt(env
, PGM_SPECIFICATION
, 4);
353 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
354 if (sch
&& css_subch_visible(sch
)) {
355 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
359 /* 0 - status pending, 1 - not status pending, 3 - not operational */
361 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &irb
, irb_len
) != 0) {
364 css_do_tsch_update_subch(sch
);
366 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
367 /* Access exceptions have a higher priority than cc3 */
368 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, irb_len
) != 0) {
377 typedef struct ChscReq
{
383 } QEMU_PACKED ChscReq
;
385 typedef struct ChscResp
{
390 } QEMU_PACKED ChscResp
;
392 #define CHSC_MIN_RESP_LEN 0x0008
394 #define CHSC_SCPD 0x0002
395 #define CHSC_SCSC 0x0010
396 #define CHSC_SDA 0x0031
397 #define CHSC_SEI 0x000e
399 #define CHSC_SCPD_0_M 0x20000000
400 #define CHSC_SCPD_0_C 0x10000000
401 #define CHSC_SCPD_0_FMT 0x0f000000
402 #define CHSC_SCPD_0_CSSID 0x00ff0000
403 #define CHSC_SCPD_0_RFMT 0x00000f00
404 #define CHSC_SCPD_0_RES 0xc000f000
405 #define CHSC_SCPD_1_RES 0xffffff00
406 #define CHSC_SCPD_01_CHPID 0x000000ff
407 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
409 uint16_t len
= be16_to_cpu(req
->len
);
410 uint32_t param0
= be32_to_cpu(req
->param0
);
411 uint32_t param1
= be32_to_cpu(req
->param1
);
415 uint8_t f_chpid
, l_chpid
;
419 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
420 if ((rfmt
== 0) || (rfmt
== 1)) {
421 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
423 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
424 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
428 if (param0
& CHSC_SCPD_0_FMT
) {
432 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
433 m
= param0
& CHSC_SCPD_0_M
;
435 if (!m
|| !css_present(cssid
)) {
440 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
441 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
442 if (l_chpid
< f_chpid
) {
446 /* css_collect_chp_desc() is endian-aware */
447 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
449 res
->code
= cpu_to_be16(0x0001);
450 res
->len
= cpu_to_be16(8 + desc_size
);
451 res
->param
= cpu_to_be32(rfmt
);
455 res
->code
= cpu_to_be16(resp_code
);
456 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
457 res
->param
= cpu_to_be32(rfmt
);
460 #define CHSC_SCSC_0_M 0x20000000
461 #define CHSC_SCSC_0_FMT 0x000f0000
462 #define CHSC_SCSC_0_CSSID 0x0000ff00
463 #define CHSC_SCSC_0_RES 0xdff000ff
464 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
466 uint16_t len
= be16_to_cpu(req
->len
);
467 uint32_t param0
= be32_to_cpu(req
->param0
);
470 uint32_t general_chars
[510];
471 uint32_t chsc_chars
[508];
478 if (param0
& CHSC_SCSC_0_FMT
) {
482 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
484 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
489 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
493 res
->code
= cpu_to_be16(0x0001);
494 res
->len
= cpu_to_be16(4080);
497 memset(general_chars
, 0, sizeof(general_chars
));
498 memset(chsc_chars
, 0, sizeof(chsc_chars
));
500 general_chars
[0] = cpu_to_be32(0x03000000);
501 general_chars
[1] = cpu_to_be32(0x00079000);
502 general_chars
[3] = cpu_to_be32(0x00080000);
504 chsc_chars
[0] = cpu_to_be32(0x40000000);
505 chsc_chars
[3] = cpu_to_be32(0x00040000);
507 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
508 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
512 res
->code
= cpu_to_be16(resp_code
);
513 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
517 #define CHSC_SDA_0_FMT 0x0f000000
518 #define CHSC_SDA_0_OC 0x0000ffff
519 #define CHSC_SDA_0_RES 0xf0ff0000
520 #define CHSC_SDA_OC_MCSSE 0x0
521 #define CHSC_SDA_OC_MSS 0x2
522 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
524 uint16_t resp_code
= 0x0001;
525 uint16_t len
= be16_to_cpu(req
->len
);
526 uint32_t param0
= be32_to_cpu(req
->param0
);
530 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
535 if (param0
& CHSC_SDA_0_FMT
) {
540 oc
= param0
& CHSC_SDA_0_OC
;
542 case CHSC_SDA_OC_MCSSE
:
543 ret
= css_enable_mcsse();
544 if (ret
== -EINVAL
) {
549 case CHSC_SDA_OC_MSS
:
550 ret
= css_enable_mss();
551 if (ret
== -EINVAL
) {
562 res
->code
= cpu_to_be16(resp_code
);
563 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
567 static int chsc_sei_nt0_get_event(void *res
)
573 static int chsc_sei_nt0_have_event(void)
579 static int chsc_sei_nt2_get_event(void *res
)
581 if (s390_has_feat(S390_FEAT_ZPCI
)) {
582 return pci_chsc_sei_nt2_get_event(res
);
587 static int chsc_sei_nt2_have_event(void)
589 if (s390_has_feat(S390_FEAT_ZPCI
)) {
590 return pci_chsc_sei_nt2_have_event();
595 #define CHSC_SEI_NT0 (1ULL << 63)
596 #define CHSC_SEI_NT2 (1ULL << 61)
597 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
599 uint64_t selection_mask
= ldq_p(&req
->param1
);
600 uint8_t *res_flags
= (uint8_t *)res
->data
;
604 /* regarding architecture nt0 can not be masked */
605 have_event
= !chsc_sei_nt0_get_event(res
);
606 have_more
= chsc_sei_nt0_have_event();
608 if (selection_mask
& CHSC_SEI_NT2
) {
610 have_event
= !chsc_sei_nt2_get_event(res
);
614 have_more
= chsc_sei_nt2_have_event();
619 res
->code
= cpu_to_be16(0x0001);
621 (*res_flags
) |= 0x80;
623 (*res_flags
) &= ~0x80;
624 css_clear_sei_pending();
627 res
->code
= cpu_to_be16(0x0005);
628 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
632 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
634 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
635 res
->code
= cpu_to_be16(0x0004);
639 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
647 CPUS390XState
*env
= &cpu
->env
;
648 uint8_t buf
[TARGET_PAGE_SIZE
];
650 trace_ioinst("chsc");
651 reg
= (ipb
>> 20) & 0x00f;
652 addr
= env
->regs
[reg
];
655 program_interrupt(env
, PGM_SPECIFICATION
, 4);
659 * Reading sizeof(ChscReq) bytes is currently enough for all of our
660 * present CHSC sub-handlers ... if we ever need more, we should take
661 * care of req->len here first.
663 if (s390_cpu_virt_mem_read(cpu
, addr
, reg
, buf
, sizeof(ChscReq
))) {
666 req
= (ChscReq
*)buf
;
667 len
= be16_to_cpu(req
->len
);
668 /* Length field valid? */
669 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
670 program_interrupt(env
, PGM_OPERAND
, 4);
673 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
674 res
= (void *)((char *)req
+ len
);
675 command
= be16_to_cpu(req
->command
);
676 trace_ioinst_chsc_cmd(command
, len
);
679 ioinst_handle_chsc_scsc(req
, res
);
682 ioinst_handle_chsc_scpd(req
, res
);
685 ioinst_handle_chsc_sda(req
, res
);
688 ioinst_handle_chsc_sei(req
, res
);
691 ioinst_handle_chsc_unimplemented(res
);
695 if (!s390_cpu_virt_mem_write(cpu
, addr
+ len
, reg
, res
,
696 be16_to_cpu(res
->len
))) {
697 setcc(cpu
, 0); /* Command execution complete */
701 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
703 CPUS390XState
*env
= &cpu
->env
;
712 addr
= decode_basedisp_s(env
, ipb
, &ar
);
714 program_interrupt(env
, PGM_SPECIFICATION
, 4);
718 lowcore
= addr
? 0 : 1;
719 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
720 ret
= css_do_tpi(&int_code
, lowcore
);
722 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, ar
, &int_code
, len
);
727 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
728 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
729 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
730 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
732 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
738 CPUS390XState
*env
= &cpu
->env
;
740 trace_ioinst("schm");
742 if (SCHM_REG1_RES(reg1
)) {
743 program_interrupt(env
, PGM_OPERAND
, 4);
747 mbk
= SCHM_REG1_MBK(reg1
);
748 update
= SCHM_REG1_UPD(reg1
);
749 dct
= SCHM_REG1_DCT(reg1
);
751 if (update
&& (reg2
& 0x000000000000001f)) {
752 program_interrupt(env
, PGM_OPERAND
, 4);
756 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
759 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
761 int cssid
, ssid
, schid
, m
;
764 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
765 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
768 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
769 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
770 if (!sch
|| !css_subch_visible(sch
)) {
774 setcc(cpu
, css_do_rsch(sch
));
777 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
778 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
779 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
780 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
786 CPUS390XState
*env
= &cpu
->env
;
788 if (RCHP_REG1_RES(reg1
)) {
789 program_interrupt(env
, PGM_OPERAND
, 4);
793 cssid
= RCHP_REG1_CSSID(reg1
);
794 chpid
= RCHP_REG1_CHPID(reg1
);
796 trace_ioinst_chp_id("rchp", cssid
, chpid
);
798 ret
= css_do_rchp(cssid
, chpid
);
811 /* Invalid channel subsystem. */
812 program_interrupt(env
, PGM_OPERAND
, 4);
818 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
819 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
821 /* We do not provide address limit checking, so let's suppress it. */
822 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
823 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);