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
;
46 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
47 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
50 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
51 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
52 if (!sch
|| !css_subch_visible(sch
)) {
56 setcc(cpu
, css_do_xsch(sch
));
59 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
61 int cssid
, ssid
, schid
, m
;
64 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
65 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
68 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
69 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
70 if (!sch
|| !css_subch_visible(sch
)) {
74 setcc(cpu
, css_do_csch(sch
));
77 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
79 int cssid
, ssid
, schid
, m
;
84 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
85 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
88 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
89 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
90 if (sch
&& css_subch_visible(sch
)) {
91 ret
= css_do_hsch(sch
);
110 static int ioinst_schib_valid(SCHIB
*schib
)
112 if ((be16_to_cpu(schib
->pmcw
.flags
) & PMCW_FLAGS_MASK_INVALID
) ||
113 (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_INVALID
)) {
116 /* Disallow extended measurements for now. */
117 if (be32_to_cpu(schib
->pmcw
.chars
) & PMCW_CHARS_MASK_XMWME
) {
123 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
125 int cssid
, ssid
, schid
, m
;
131 CPUS390XState
*env
= &cpu
->env
;
134 addr
= decode_basedisp_s(env
, ipb
, &ar
);
136 program_interrupt(env
, PGM_SPECIFICATION
, 4);
139 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &schib
, sizeof(schib
))) {
142 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
143 !ioinst_schib_valid(&schib
)) {
144 program_interrupt(env
, PGM_OPERAND
, 4);
147 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
148 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
149 if (sch
&& css_subch_visible(sch
)) {
150 ret
= css_do_msch(sch
, &schib
);
169 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
171 dest
->intparm
= be32_to_cpu(src
->intparm
);
172 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
173 dest
->lpm
= src
->lpm
;
174 dest
->ctrl1
= src
->ctrl1
;
175 dest
->cpa
= be32_to_cpu(src
->cpa
);
178 static int ioinst_orb_valid(ORB
*orb
)
180 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
181 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
184 /* We don't support MIDA. */
185 if (orb
->ctrl1
& ORB_CTRL1_MASK_MIDAW
) {
188 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
194 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
196 int cssid
, ssid
, schid
, m
;
200 CPUS390XState
*env
= &cpu
->env
;
203 addr
= decode_basedisp_s(env
, ipb
, &ar
);
205 program_interrupt(env
, PGM_SPECIFICATION
, 4);
208 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &orig_orb
, sizeof(orb
))) {
211 copy_orb_from_guest(&orb
, &orig_orb
);
212 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
213 !ioinst_orb_valid(&orb
)) {
214 program_interrupt(env
, PGM_OPERAND
, 4);
217 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
218 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
219 if (!sch
|| !css_subch_visible(sch
)) {
223 setcc(cpu
, css_do_ssch(sch
, &orb
));
226 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
231 CPUS390XState
*env
= &cpu
->env
;
234 addr
= decode_basedisp_s(env
, ipb
, &ar
);
236 program_interrupt(env
, PGM_SPECIFICATION
, 4);
240 cc
= css_do_stcrw(&crw
);
241 /* 0 - crw stored, 1 - zeroes stored */
243 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &crw
, sizeof(crw
)) == 0) {
245 } else if (cc
== 0) {
246 /* Write failed: requeue CRW since STCRW is a suppressing instruction */
247 css_undo_stcrw(&crw
);
251 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
253 int cssid
, ssid
, schid
, m
;
258 CPUS390XState
*env
= &cpu
->env
;
261 addr
= decode_basedisp_s(env
, ipb
, &ar
);
263 program_interrupt(env
, PGM_SPECIFICATION
, 4);
267 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
269 * As operand exceptions have a lower priority than access exceptions,
270 * we check whether the memory area is writeable (injecting the
271 * access execption if it is not) first.
273 if (!s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
))) {
274 program_interrupt(env
, PGM_OPERAND
, 4);
278 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
279 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
281 if (css_subch_visible(sch
)) {
282 css_do_stsch(sch
, &schib
);
285 /* Indicate no more subchannels in this css/ss */
289 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
290 cc
= 3; /* No more subchannels in this css/ss */
292 /* Store an empty schib. */
293 memset(&schib
, 0, sizeof(schib
));
298 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &schib
,
299 sizeof(schib
)) != 0) {
303 /* Access exceptions have a higher priority than cc3 */
304 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, sizeof(schib
)) != 0) {
311 int ioinst_handle_tsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
313 CPUS390XState
*env
= &cpu
->env
;
314 int cssid
, ssid
, schid
, m
;
321 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
322 program_interrupt(env
, PGM_OPERAND
, 4);
325 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
326 addr
= decode_basedisp_s(env
, ipb
, &ar
);
328 program_interrupt(env
, PGM_SPECIFICATION
, 4);
332 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
333 if (sch
&& css_subch_visible(sch
)) {
334 cc
= css_do_tsch_get_irb(sch
, &irb
, &irb_len
);
338 /* 0 - status pending, 1 - not status pending, 3 - not operational */
340 if (s390_cpu_virt_mem_write(cpu
, addr
, ar
, &irb
, irb_len
) != 0) {
343 css_do_tsch_update_subch(sch
);
345 irb_len
= sizeof(irb
) - sizeof(irb
.emw
);
346 /* Access exceptions have a higher priority than cc3 */
347 if (s390_cpu_virt_mem_check_write(cpu
, addr
, ar
, irb_len
) != 0) {
356 typedef struct ChscReq
{
362 } QEMU_PACKED ChscReq
;
364 typedef struct ChscResp
{
369 } QEMU_PACKED ChscResp
;
371 #define CHSC_MIN_RESP_LEN 0x0008
373 #define CHSC_SCPD 0x0002
374 #define CHSC_SCSC 0x0010
375 #define CHSC_SDA 0x0031
376 #define CHSC_SEI 0x000e
378 #define CHSC_SCPD_0_M 0x20000000
379 #define CHSC_SCPD_0_C 0x10000000
380 #define CHSC_SCPD_0_FMT 0x0f000000
381 #define CHSC_SCPD_0_CSSID 0x00ff0000
382 #define CHSC_SCPD_0_RFMT 0x00000f00
383 #define CHSC_SCPD_0_RES 0xc000f000
384 #define CHSC_SCPD_1_RES 0xffffff00
385 #define CHSC_SCPD_01_CHPID 0x000000ff
386 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
388 uint16_t len
= be16_to_cpu(req
->len
);
389 uint32_t param0
= be32_to_cpu(req
->param0
);
390 uint32_t param1
= be32_to_cpu(req
->param1
);
394 uint8_t f_chpid
, l_chpid
;
398 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
399 if ((rfmt
== 0) || (rfmt
== 1)) {
400 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
402 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
403 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
407 if (param0
& CHSC_SCPD_0_FMT
) {
411 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
412 m
= param0
& CHSC_SCPD_0_M
;
414 if (!m
|| !css_present(cssid
)) {
419 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
420 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
421 if (l_chpid
< f_chpid
) {
425 /* css_collect_chp_desc() is endian-aware */
426 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
428 res
->code
= cpu_to_be16(0x0001);
429 res
->len
= cpu_to_be16(8 + desc_size
);
430 res
->param
= cpu_to_be32(rfmt
);
434 res
->code
= cpu_to_be16(resp_code
);
435 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
436 res
->param
= cpu_to_be32(rfmt
);
439 #define CHSC_SCSC_0_M 0x20000000
440 #define CHSC_SCSC_0_FMT 0x000f0000
441 #define CHSC_SCSC_0_CSSID 0x0000ff00
442 #define CHSC_SCSC_0_RES 0xdff000ff
443 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
445 uint16_t len
= be16_to_cpu(req
->len
);
446 uint32_t param0
= be32_to_cpu(req
->param0
);
449 uint32_t general_chars
[510];
450 uint32_t chsc_chars
[508];
457 if (param0
& CHSC_SCSC_0_FMT
) {
461 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
463 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
468 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
472 res
->code
= cpu_to_be16(0x0001);
473 res
->len
= cpu_to_be16(4080);
476 memset(general_chars
, 0, sizeof(general_chars
));
477 memset(chsc_chars
, 0, sizeof(chsc_chars
));
479 general_chars
[0] = cpu_to_be32(0x03000000);
480 general_chars
[1] = cpu_to_be32(0x00079000);
481 general_chars
[3] = cpu_to_be32(0x00080000);
483 chsc_chars
[0] = cpu_to_be32(0x40000000);
484 chsc_chars
[3] = cpu_to_be32(0x00040000);
486 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
487 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
491 res
->code
= cpu_to_be16(resp_code
);
492 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
496 #define CHSC_SDA_0_FMT 0x0f000000
497 #define CHSC_SDA_0_OC 0x0000ffff
498 #define CHSC_SDA_0_RES 0xf0ff0000
499 #define CHSC_SDA_OC_MCSSE 0x0
500 #define CHSC_SDA_OC_MSS 0x2
501 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
503 uint16_t resp_code
= 0x0001;
504 uint16_t len
= be16_to_cpu(req
->len
);
505 uint32_t param0
= be32_to_cpu(req
->param0
);
509 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
514 if (param0
& CHSC_SDA_0_FMT
) {
519 oc
= param0
& CHSC_SDA_0_OC
;
521 case CHSC_SDA_OC_MCSSE
:
522 ret
= css_enable_mcsse();
523 if (ret
== -EINVAL
) {
528 case CHSC_SDA_OC_MSS
:
529 ret
= css_enable_mss();
530 if (ret
== -EINVAL
) {
541 res
->code
= cpu_to_be16(resp_code
);
542 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
546 static int chsc_sei_nt0_get_event(void *res
)
552 static int chsc_sei_nt0_have_event(void)
558 static int chsc_sei_nt2_get_event(void *res
)
560 if (s390_has_feat(S390_FEAT_ZPCI
)) {
561 return pci_chsc_sei_nt2_get_event(res
);
566 static int chsc_sei_nt2_have_event(void)
568 if (s390_has_feat(S390_FEAT_ZPCI
)) {
569 return pci_chsc_sei_nt2_have_event();
574 #define CHSC_SEI_NT0 (1ULL << 63)
575 #define CHSC_SEI_NT2 (1ULL << 61)
576 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
578 uint64_t selection_mask
= ldq_p(&req
->param1
);
579 uint8_t *res_flags
= (uint8_t *)res
->data
;
583 /* regarding architecture nt0 can not be masked */
584 have_event
= !chsc_sei_nt0_get_event(res
);
585 have_more
= chsc_sei_nt0_have_event();
587 if (selection_mask
& CHSC_SEI_NT2
) {
589 have_event
= !chsc_sei_nt2_get_event(res
);
593 have_more
= chsc_sei_nt2_have_event();
598 res
->code
= cpu_to_be16(0x0001);
600 (*res_flags
) |= 0x80;
602 (*res_flags
) &= ~0x80;
603 css_clear_sei_pending();
606 res
->code
= cpu_to_be16(0x0005);
607 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
611 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
613 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
614 res
->code
= cpu_to_be16(0x0004);
618 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
626 CPUS390XState
*env
= &cpu
->env
;
627 uint8_t buf
[TARGET_PAGE_SIZE
];
629 trace_ioinst("chsc");
630 reg
= (ipb
>> 20) & 0x00f;
631 addr
= env
->regs
[reg
];
634 program_interrupt(env
, PGM_SPECIFICATION
, 4);
638 * Reading sizeof(ChscReq) bytes is currently enough for all of our
639 * present CHSC sub-handlers ... if we ever need more, we should take
640 * care of req->len here first.
642 if (s390_cpu_virt_mem_read(cpu
, addr
, reg
, buf
, sizeof(ChscReq
))) {
645 req
= (ChscReq
*)buf
;
646 len
= be16_to_cpu(req
->len
);
647 /* Length field valid? */
648 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
649 program_interrupt(env
, PGM_OPERAND
, 4);
652 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
653 res
= (void *)((char *)req
+ len
);
654 command
= be16_to_cpu(req
->command
);
655 trace_ioinst_chsc_cmd(command
, len
);
658 ioinst_handle_chsc_scsc(req
, res
);
661 ioinst_handle_chsc_scpd(req
, res
);
664 ioinst_handle_chsc_sda(req
, res
);
667 ioinst_handle_chsc_sei(req
, res
);
670 ioinst_handle_chsc_unimplemented(res
);
674 if (!s390_cpu_virt_mem_write(cpu
, addr
+ len
, reg
, res
,
675 be16_to_cpu(res
->len
))) {
676 setcc(cpu
, 0); /* Command execution complete */
680 int ioinst_handle_tpi(S390CPU
*cpu
, uint32_t ipb
)
682 CPUS390XState
*env
= &cpu
->env
;
691 addr
= decode_basedisp_s(env
, ipb
, &ar
);
693 program_interrupt(env
, PGM_SPECIFICATION
, 4);
697 lowcore
= addr
? 0 : 1;
698 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
699 ret
= css_do_tpi(&int_code
, lowcore
);
701 s390_cpu_virt_mem_write(cpu
, lowcore
? 184 : addr
, ar
, &int_code
, len
);
706 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
707 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
708 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
709 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
711 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
717 CPUS390XState
*env
= &cpu
->env
;
719 trace_ioinst("schm");
721 if (SCHM_REG1_RES(reg1
)) {
722 program_interrupt(env
, PGM_OPERAND
, 4);
726 mbk
= SCHM_REG1_MBK(reg1
);
727 update
= SCHM_REG1_UPD(reg1
);
728 dct
= SCHM_REG1_DCT(reg1
);
730 if (update
&& (reg2
& 0x000000000000001f)) {
731 program_interrupt(env
, PGM_OPERAND
, 4);
735 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
738 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
740 int cssid
, ssid
, schid
, m
;
743 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
744 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);
747 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
748 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
749 if (!sch
|| !css_subch_visible(sch
)) {
753 setcc(cpu
, css_do_rsch(sch
));
756 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
757 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
758 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
759 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
765 CPUS390XState
*env
= &cpu
->env
;
767 if (RCHP_REG1_RES(reg1
)) {
768 program_interrupt(env
, PGM_OPERAND
, 4);
772 cssid
= RCHP_REG1_CSSID(reg1
);
773 chpid
= RCHP_REG1_CHPID(reg1
);
775 trace_ioinst_chp_id("rchp", cssid
, chpid
);
777 ret
= css_do_rchp(cssid
, chpid
);
790 /* Invalid channel subsystem. */
791 program_interrupt(env
, PGM_OPERAND
, 4);
797 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
798 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
800 /* We do not provide address limit checking, so let's suppress it. */
801 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
802 program_interrupt(&cpu
->env
, PGM_OPERAND
, 4);