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 hwaddr len
= sizeof(*crw
);
261 CPUS390XState
*env
= &cpu
->env
;
263 addr
= decode_basedisp_s(env
, ipb
);
265 program_interrupt(env
, PGM_SPECIFICATION
, 2);
268 crw
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
269 if (!crw
|| len
!= sizeof(*crw
)) {
270 program_interrupt(env
, PGM_ADDRESSING
, 2);
273 cc
= css_do_stcrw(crw
);
274 /* 0 - crw stored, 1 - zeroes stored */
278 s390_cpu_physical_memory_unmap(env
, crw
, len
, 1);
281 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
283 int cssid
, ssid
, schid
, m
;
288 CPUS390XState
*env
= &cpu
->env
;
290 addr
= decode_basedisp_s(env
, ipb
);
292 program_interrupt(env
, PGM_SPECIFICATION
, 2);
296 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
298 * As operand exceptions have a lower priority than access exceptions,
299 * we check whether the memory area is writeable (injecting the
300 * access execption if it is not) first.
302 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, sizeof(schib
))) {
303 program_interrupt(env
, PGM_OPERAND
, 2);
307 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
308 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
310 if (css_subch_visible(sch
)) {
311 css_do_stsch(sch
, &schib
);
314 /* Indicate no more subchannels in this css/ss */
318 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
319 cc
= 3; /* No more subchannels in this css/ss */
321 /* Store an empty schib. */
322 memset(&schib
, 0, sizeof(schib
));
327 if (s390_cpu_virt_mem_write(cpu
, addr
, &schib
, sizeof(schib
)) != 0) {
331 /* Access exceptions have a higher priority than cc3 */
332 if (s390_cpu_virt_mem_check_write(cpu
, addr
, sizeof(schib
)) != 0) {
339 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
341 CPUS390XState
*env
= &cpu
->env
;
342 int cssid
, ssid
, schid
, m
;
348 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
349 program_interrupt(env
, PGM_OPERAND
, 2);
352 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
353 addr
= decode_basedisp_s(env
, ipb
);
355 program_interrupt(env
, PGM_SPECIFICATION
, 2);
359 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
360 if (sch
&& css_subch_visible(sch
)) {
361 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
365 /* 0 - status pending, 1 - not status pending, 3 - not operational */
367 if (s390_cpu_virt_mem_write(cpu
, addr
, &irb
, irb_len
) != 0) {
370 css_do_tsch_update_subch(sch
);
372 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
373 /* Access exceptions have a higher priority than cc3 */
374 if (s390_cpu_virt_mem_check_write(cpu
, addr
, irb_len
) != 0) {
383 typedef struct ChscReq
{
389 } QEMU_PACKED ChscReq
;
391 typedef struct ChscResp
{
396 } QEMU_PACKED ChscResp
;
398 #define CHSC_MIN_RESP_LEN 0x0008
400 #define CHSC_SCPD 0x0002
401 #define CHSC_SCSC 0x0010
402 #define CHSC_SDA 0x0031
403 #define CHSC_SEI 0x000e
405 #define CHSC_SCPD_0_M 0x20000000
406 #define CHSC_SCPD_0_C 0x10000000
407 #define CHSC_SCPD_0_FMT 0x0f000000
408 #define CHSC_SCPD_0_CSSID 0x00ff0000
409 #define CHSC_SCPD_0_RFMT 0x00000f00
410 #define CHSC_SCPD_0_RES 0xc000f000
411 #define CHSC_SCPD_1_RES 0xffffff00
412 #define CHSC_SCPD_01_CHPID 0x000000ff
413 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
415 uint16_t len
= be16_to_cpu(req
->len
);
416 uint32_t param0
= be32_to_cpu(req
->param0
);
417 uint32_t param1
= be32_to_cpu(req
->param1
);
421 uint8_t f_chpid
, l_chpid
;
425 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
426 if ((rfmt
== 0) || (rfmt
== 1)) {
427 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
429 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
430 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
434 if (param0
& CHSC_SCPD_0_FMT
) {
438 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
439 m
= param0
& CHSC_SCPD_0_M
;
441 if (!m
|| !css_present(cssid
)) {
446 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
447 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
448 if (l_chpid
< f_chpid
) {
452 /* css_collect_chp_desc() is endian-aware */
453 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
455 res
->code
= cpu_to_be16(0x0001);
456 res
->len
= cpu_to_be16(8 + desc_size
);
457 res
->param
= cpu_to_be32(rfmt
);
461 res
->code
= cpu_to_be16(resp_code
);
462 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
463 res
->param
= cpu_to_be32(rfmt
);
466 #define CHSC_SCSC_0_M 0x20000000
467 #define CHSC_SCSC_0_FMT 0x000f0000
468 #define CHSC_SCSC_0_CSSID 0x0000ff00
469 #define CHSC_SCSC_0_RES 0xdff000ff
470 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
472 uint16_t len
= be16_to_cpu(req
->len
);
473 uint32_t param0
= be32_to_cpu(req
->param0
);
476 uint32_t general_chars
[510];
477 uint32_t chsc_chars
[508];
484 if (param0
& CHSC_SCSC_0_FMT
) {
488 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
490 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
495 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
499 res
->code
= cpu_to_be16(0x0001);
500 res
->len
= cpu_to_be16(4080);
503 memset(general_chars
, 0, sizeof(general_chars
));
504 memset(chsc_chars
, 0, sizeof(chsc_chars
));
506 general_chars
[0] = cpu_to_be32(0x03000000);
507 general_chars
[1] = cpu_to_be32(0x00059000);
509 chsc_chars
[0] = cpu_to_be32(0x40000000);
510 chsc_chars
[3] = cpu_to_be32(0x00040000);
512 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
513 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
517 res
->code
= cpu_to_be16(resp_code
);
518 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
522 #define CHSC_SDA_0_FMT 0x0f000000
523 #define CHSC_SDA_0_OC 0x0000ffff
524 #define CHSC_SDA_0_RES 0xf0ff0000
525 #define CHSC_SDA_OC_MCSSE 0x0
526 #define CHSC_SDA_OC_MSS 0x2
527 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
529 uint16_t resp_code
= 0x0001;
530 uint16_t len
= be16_to_cpu(req
->len
);
531 uint32_t param0
= be32_to_cpu(req
->param0
);
535 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
540 if (param0
& CHSC_SDA_0_FMT
) {
545 oc
= param0
& CHSC_SDA_0_OC
;
547 case CHSC_SDA_OC_MCSSE
:
548 ret
= css_enable_mcsse();
549 if (ret
== -EINVAL
) {
554 case CHSC_SDA_OC_MSS
:
555 ret
= css_enable_mss();
556 if (ret
== -EINVAL
) {
567 res
->code
= cpu_to_be16(resp_code
);
568 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
572 static int chsc_sei_nt0_get_event(void *res
)
578 static int chsc_sei_nt0_have_event(void)
584 #define CHSC_SEI_NT0 (1ULL << 63)
585 #define CHSC_SEI_NT2 (1ULL << 61)
586 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
588 uint64_t selection_mask
= ldq_p(&req
->param1
);
589 uint8_t *res_flags
= (uint8_t *)res
->data
;
593 /* regarding architecture nt0 can not be masked */
594 have_event
= !chsc_sei_nt0_get_event(res
);
595 have_more
= chsc_sei_nt0_have_event();
597 if (selection_mask
& CHSC_SEI_NT2
) {
599 have_event
= !chsc_sei_nt2_get_event(res
);
603 have_more
= chsc_sei_nt2_have_event();
608 res
->code
= cpu_to_be16(0x0001);
610 (*res_flags
) |= 0x80;
612 (*res_flags
) &= ~0x80;
615 res
->code
= cpu_to_be16(0x0004);
619 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
621 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
622 res
->code
= cpu_to_be16(0x0004);
626 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
634 hwaddr map_size
= TARGET_PAGE_SIZE
;
635 CPUS390XState
*env
= &cpu
->env
;
637 trace_ioinst("chsc");
638 reg
= (ipb
>> 20) & 0x00f;
639 addr
= env
->regs
[reg
];
642 program_interrupt(env
, PGM_SPECIFICATION
, 2);
645 req
= s390_cpu_physical_memory_map(env
, addr
, &map_size
, 1);
646 if (!req
|| map_size
!= TARGET_PAGE_SIZE
) {
647 program_interrupt(env
, PGM_ADDRESSING
, 2);
650 len
= be16_to_cpu(req
->len
);
651 /* Length field valid? */
652 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
653 program_interrupt(env
, PGM_OPERAND
, 2);
656 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
657 res
= (void *)((char *)req
+ len
);
658 command
= be16_to_cpu(req
->command
);
659 trace_ioinst_chsc_cmd(command
, len
);
662 ioinst_handle_chsc_scsc(req
, res
);
665 ioinst_handle_chsc_scpd(req
, res
);
668 ioinst_handle_chsc_sda(req
, res
);
671 ioinst_handle_chsc_sei(req
, res
);
674 ioinst_handle_chsc_unimplemented(res
);
678 setcc(cpu
, 0); /* Command execution complete */
680 s390_cpu_physical_memory_unmap(env
, req
, map_size
, 1);
683 int ioinst_handle_tpi(CPUS390XState
*env
, uint32_t ipb
)
688 hwaddr len
, orig_len
;
692 addr
= decode_basedisp_s(env
, ipb
);
694 program_interrupt(env
, PGM_SPECIFICATION
, 2);
698 lowcore
= addr
? 0 : 1;
699 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
701 int_code
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
702 if (!int_code
|| (len
!= orig_len
)) {
703 program_interrupt(env
, PGM_ADDRESSING
, 2);
707 ret
= css_do_tpi(int_code
, lowcore
);
709 s390_cpu_physical_memory_unmap(env
, int_code
, len
, 1);
713 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
714 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
715 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
716 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
718 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
724 CPUS390XState
*env
= &cpu
->env
;
726 trace_ioinst("schm");
728 if (SCHM_REG1_RES(reg1
)) {
729 program_interrupt(env
, PGM_OPERAND
, 2);
733 mbk
= SCHM_REG1_MBK(reg1
);
734 update
= SCHM_REG1_UPD(reg1
);
735 dct
= SCHM_REG1_DCT(reg1
);
737 if (update
&& (reg2
& 0x000000000000001f)) {
738 program_interrupt(env
, PGM_OPERAND
, 2);
742 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
745 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
747 int cssid
, ssid
, schid
, m
;
752 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
753 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
756 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
757 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
758 if (sch
&& css_subch_visible(sch
)) {
759 ret
= css_do_rsch(sch
);
778 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
779 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
780 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
781 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
787 CPUS390XState
*env
= &cpu
->env
;
789 if (RCHP_REG1_RES(reg1
)) {
790 program_interrupt(env
, PGM_OPERAND
, 2);
794 cssid
= RCHP_REG1_CSSID(reg1
);
795 chpid
= RCHP_REG1_CHPID(reg1
);
797 trace_ioinst_chp_id("rchp", cssid
, chpid
);
799 ret
= css_do_rchp(cssid
, chpid
);
812 /* Invalid channel subsystem. */
813 program_interrupt(env
, PGM_OPERAND
, 2);
819 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
820 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
822 /* We do not provide address limit checking, so let's suppress it. */
823 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
824 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);