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 <sys/types.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 ((schib
->pmcw
.flags
& PMCW_FLAGS_MASK_INVALID
) ||
133 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_INVALID
)) {
136 /* Disallow extended measurements for now. */
137 if (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
;
153 addr
= decode_basedisp_s(env
, ipb
);
155 program_interrupt(env
, PGM_SPECIFICATION
, 2);
158 if (s390_cpu_virt_mem_read(cpu
, addr
, &schib
, sizeof(schib
))) {
161 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
162 !ioinst_schib_valid(&schib
)) {
163 program_interrupt(env
, PGM_OPERAND
, 2);
166 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
167 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
168 if (sch
&& css_subch_visible(sch
)) {
169 ret
= css_do_msch(sch
, &schib
);
188 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
190 dest
->intparm
= be32_to_cpu(src
->intparm
);
191 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
192 dest
->lpm
= src
->lpm
;
193 dest
->ctrl1
= src
->ctrl1
;
194 dest
->cpa
= be32_to_cpu(src
->cpa
);
197 static int ioinst_orb_valid(ORB
*orb
)
199 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
200 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
203 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
209 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
211 int cssid
, ssid
, schid
, m
;
217 CPUS390XState
*env
= &cpu
->env
;
219 addr
= decode_basedisp_s(env
, ipb
);
221 program_interrupt(env
, PGM_SPECIFICATION
, 2);
224 if (s390_cpu_virt_mem_read(cpu
, addr
, &orig_orb
, sizeof(orb
))) {
227 copy_orb_from_guest(&orb
, &orig_orb
);
228 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
229 !ioinst_orb_valid(&orb
)) {
230 program_interrupt(env
, PGM_OPERAND
, 2);
233 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
234 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
235 if (sch
&& css_subch_visible(sch
)) {
236 ret
= css_do_ssch(sch
, &orb
);
255 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
260 CPUS390XState
*env
= &cpu
->env
;
262 addr
= decode_basedisp_s(env
, ipb
);
264 program_interrupt(env
, PGM_SPECIFICATION
, 2);
268 cc
= css_do_stcrw(&crw
);
269 /* 0 - crw stored, 1 - zeroes stored */
271 if (s390_cpu_virt_mem_write(cpu
, addr
, &crw
, sizeof(crw
)) == 0) {
273 } else if (cc
== 0) {
274 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
275 css_undo_stcrw(&crw
);
279 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
281 int cssid
, ssid
, schid
, m
;
286 CPUS390XState
*env
= &cpu
->env
;
288 addr
= decode_basedisp_s(env
, ipb
);
290 program_interrupt(env
, PGM_SPECIFICATION
, 2);
294 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
296 * As operand exceptions have a lower priority than access exceptions,
297 * we check whether the memory area is writeable (injecting the
298 * access execption if it is not) first.
300 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, sizeof(schib
))) {
301 program_interrupt(env
, PGM_OPERAND
, 2);
305 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
306 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
308 if (css_subch_visible(sch
)) {
309 css_do_stsch(sch
, &schib
);
312 /* Indicate no more subchannels in this css/ss */
316 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
317 cc
= 3; /* No more subchannels in this css/ss */
319 /* Store an empty schib. */
320 memset(&schib
, 0, sizeof(schib
));
325 if (s390_cpu_virt_mem_write(cpu
, addr
, &schib
, sizeof(schib
)) != 0) {
329 /* Access exceptions have a higher priority than cc3 */
330 if (s390_cpu_virt_mem_check_write(cpu
, addr
, sizeof(schib
)) != 0) {
337 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
339 CPUS390XState
*env
= &cpu
->env
;
340 int cssid
, ssid
, schid
, m
;
346 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
347 program_interrupt(env
, PGM_OPERAND
, 2);
350 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
351 addr
= decode_basedisp_s(env
, ipb
);
353 program_interrupt(env
, PGM_SPECIFICATION
, 2);
357 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
358 if (sch
&& css_subch_visible(sch
)) {
359 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
363 /* 0 - status pending, 1 - not status pending, 3 - not operational */
365 if (s390_cpu_virt_mem_write(cpu
, addr
, &irb
, irb_len
) != 0) {
368 css_do_tsch_update_subch(sch
);
370 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
371 /* Access exceptions have a higher priority than cc3 */
372 if (s390_cpu_virt_mem_check_write(cpu
, addr
, irb_len
) != 0) {
381 typedef struct ChscReq
{
387 } QEMU_PACKED ChscReq
;
389 typedef struct ChscResp
{
394 } QEMU_PACKED ChscResp
;
396 #define CHSC_MIN_RESP_LEN 0x0008
398 #define CHSC_SCPD 0x0002
399 #define CHSC_SCSC 0x0010
400 #define CHSC_SDA 0x0031
401 #define CHSC_SEI 0x000e
403 #define CHSC_SCPD_0_M 0x20000000
404 #define CHSC_SCPD_0_C 0x10000000
405 #define CHSC_SCPD_0_FMT 0x0f000000
406 #define CHSC_SCPD_0_CSSID 0x00ff0000
407 #define CHSC_SCPD_0_RFMT 0x00000f00
408 #define CHSC_SCPD_0_RES 0xc000f000
409 #define CHSC_SCPD_1_RES 0xffffff00
410 #define CHSC_SCPD_01_CHPID 0x000000ff
411 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
413 uint16_t len
= be16_to_cpu(req
->len
);
414 uint32_t param0
= be32_to_cpu(req
->param0
);
415 uint32_t param1
= be32_to_cpu(req
->param1
);
419 uint8_t f_chpid
, l_chpid
;
423 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
424 if ((rfmt
== 0) || (rfmt
== 1)) {
425 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
427 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
428 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
432 if (param0
& CHSC_SCPD_0_FMT
) {
436 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
437 m
= param0
& CHSC_SCPD_0_M
;
439 if (!m
|| !css_present(cssid
)) {
444 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
445 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
446 if (l_chpid
< f_chpid
) {
450 /* css_collect_chp_desc() is endian-aware */
451 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
453 res
->code
= cpu_to_be16(0x0001);
454 res
->len
= cpu_to_be16(8 + desc_size
);
455 res
->param
= cpu_to_be32(rfmt
);
459 res
->code
= cpu_to_be16(resp_code
);
460 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
461 res
->param
= cpu_to_be32(rfmt
);
464 #define CHSC_SCSC_0_M 0x20000000
465 #define CHSC_SCSC_0_FMT 0x000f0000
466 #define CHSC_SCSC_0_CSSID 0x0000ff00
467 #define CHSC_SCSC_0_RES 0xdff000ff
468 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
470 uint16_t len
= be16_to_cpu(req
->len
);
471 uint32_t param0
= be32_to_cpu(req
->param0
);
474 uint32_t general_chars
[510];
475 uint32_t chsc_chars
[508];
482 if (param0
& CHSC_SCSC_0_FMT
) {
486 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
488 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
493 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
497 res
->code
= cpu_to_be16(0x0001);
498 res
->len
= cpu_to_be16(4080);
501 memset(general_chars
, 0, sizeof(general_chars
));
502 memset(chsc_chars
, 0, sizeof(chsc_chars
));
504 general_chars
[0] = cpu_to_be32(0x03000000);
505 general_chars
[1] = cpu_to_be32(0x00059000);
507 chsc_chars
[0] = cpu_to_be32(0x40000000);
508 chsc_chars
[3] = cpu_to_be32(0x00040000);
510 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
511 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
515 res
->code
= cpu_to_be16(resp_code
);
516 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
520 #define CHSC_SDA_0_FMT 0x0f000000
521 #define CHSC_SDA_0_OC 0x0000ffff
522 #define CHSC_SDA_0_RES 0xf0ff0000
523 #define CHSC_SDA_OC_MCSSE 0x0
524 #define CHSC_SDA_OC_MSS 0x2
525 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
527 uint16_t resp_code
= 0x0001;
528 uint16_t len
= be16_to_cpu(req
->len
);
529 uint32_t param0
= be32_to_cpu(req
->param0
);
533 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
538 if (param0
& CHSC_SDA_0_FMT
) {
543 oc
= param0
& CHSC_SDA_0_OC
;
545 case CHSC_SDA_OC_MCSSE
:
546 ret
= css_enable_mcsse();
547 if (ret
== -EINVAL
) {
552 case CHSC_SDA_OC_MSS
:
553 ret
= css_enable_mss();
554 if (ret
== -EINVAL
) {
565 res
->code
= cpu_to_be16(resp_code
);
566 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
570 static int chsc_sei_nt0_get_event(void *res
)
576 static int chsc_sei_nt0_have_event(void)
582 #define CHSC_SEI_NT0 (1ULL << 63)
583 #define CHSC_SEI_NT2 (1ULL << 61)
584 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
586 uint64_t selection_mask
= ldq_p(&req
->param1
);
587 uint8_t *res_flags
= (uint8_t *)res
->data
;
591 /* regarding architecture nt0 can not be masked */
592 have_event
= !chsc_sei_nt0_get_event(res
);
593 have_more
= chsc_sei_nt0_have_event();
595 if (selection_mask
& CHSC_SEI_NT2
) {
597 have_event
= !chsc_sei_nt2_get_event(res
);
601 have_more
= chsc_sei_nt2_have_event();
606 res
->code
= cpu_to_be16(0x0001);
608 (*res_flags
) |= 0x80;
610 (*res_flags
) &= ~0x80;
613 res
->code
= cpu_to_be16(0x0004);
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
, 2);
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
, 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
, 2);
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
, res
, be16_to_cpu(res
->len
))) {
681 setcc(cpu
, 0); /* Command execution complete */
685 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
687 CPUS390XState
*env
= &cpu
->env
;
695 addr
= decode_basedisp_s(env
, ipb
);
697 program_interrupt(env
, PGM_SPECIFICATION
, 2);
701 lowcore
= addr
? 0 : 1;
702 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
703 ret
= css_do_tpi(&int_code
, lowcore
);
705 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, &int_code
, len
);
710 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
711 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
712 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
713 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
715 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
721 CPUS390XState
*env
= &cpu
->env
;
723 trace_ioinst("schm");
725 if (SCHM_REG1_RES(reg1
)) {
726 program_interrupt(env
, PGM_OPERAND
, 2);
730 mbk
= SCHM_REG1_MBK(reg1
);
731 update
= SCHM_REG1_UPD(reg1
);
732 dct
= SCHM_REG1_DCT(reg1
);
734 if (update
&& (reg2
& 0x000000000000001f)) {
735 program_interrupt(env
, PGM_OPERAND
, 2);
739 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
742 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
744 int cssid
, ssid
, schid
, m
;
749 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
750 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
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
)) {
756 ret
= css_do_rsch(sch
);
775 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
776 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
777 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
778 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
784 CPUS390XState
*env
= &cpu
->env
;
786 if (RCHP_REG1_RES(reg1
)) {
787 program_interrupt(env
, PGM_OPERAND
, 2);
791 cssid
= RCHP_REG1_CSSID(reg1
);
792 chpid
= RCHP_REG1_CHPID(reg1
);
794 trace_ioinst_chp_id("rchp", cssid
, chpid
);
796 ret
= css_do_rchp(cssid
, chpid
);
809 /* Invalid channel subsystem. */
810 program_interrupt(env
, PGM_OPERAND
, 2);
816 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
817 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
819 /* We do not provide address limit checking, so let's suppress it. */
820 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
821 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);