Skip to contentSkip to conversation

add PCI support for the s390 platform

PATCH In review Frank Blaschka 5 participants 18 messages Jan 9, 2015 – Jan 21, 2015

lore
Share & tools
lore
#0 lore raw

v3 cover add PCI support for the s390 platform

This set of patches implemets PCI support for the s390 platform. Now it is possible to run virtio-net-pci and potentially all virtual pci devices conforming to s390 platform constrains.

V1 added lot of feedback from Alex Graf
fixed tons of endian issues

V2 added couple of small improvments and code cleanup
fixed build on 32-bit and non Linux
added pci to maintainer file

V3 fix css_generate_css_crws to pass cssid to css_queue_crw
fix and compile test w32 build
review #defines for ULL usage

patches apply to latest qemu master
please consider for integration into 2.3

Thanks,

Frank

Frank Blaschka (3):
s390: Add PCI bus support
s390: implement pci instructions
kvm: extend kvm_irqchip_add_msi_route to work on s390

 MAINTAINERS                       |   2 +
 default-configs/s390x-softmmu.mak |   1 +
 hw/s390x/Makefile.objs            |   1 +
 hw/s390x/css.c                    |   5 +
 hw/s390x/css.h                    |   1 +
 hw/s390x/s390-pci-bus.c           | 591 +++++++++++++++++++++++++++
 hw/s390x/s390-pci-bus.h           | 251 ++++++++++++
 hw/s390x/s390-pci-inst.c          | 811 ++++++++++++++++++++++++++++++++++++++
 hw/s390x/s390-pci-inst.h          | 288 ++++++++++++++
 hw/s390x/s390-virtio-ccw.c        |   7 +
 hw/s390x/sclp.c                   |  10 +-
 include/hw/s390x/sclp.h           |   8 +
 include/sysemu/kvm.h              |   4 +
 kvm-all.c                         |   7 +
 target-arm/kvm.c                  |   6 +
 target-i386/kvm.c                 |   6 +
 target-mips/kvm.c                 |   6 +
 target-ppc/kvm.c                  |   6 +
 target-s390x/ioinst.c             |  52 +++
 target-s390x/ioinst.h             |   1 +
 target-s390x/kvm.c                | 179 +++++++++
 21 files changed, 2242 insertions(+), 1 deletion(-)
 create mode 100644 hw/s390x/s390-pci-bus.c
 create mode 100644 hw/s390x/s390-pci-bus.h
 create mode 100644 hw/s390x/s390-pci-inst.c
 create mode 100644 hw/s390x/s390-pci-inst.h
Signature · 1 line
2.1.4
#1 lore raw

PATCH v3 1/3 s390: Add PCI bus support

From: Frank Blaschka <[email protected]>

This patch implements a pci bus for s390x together with infrastructure to generate and handle hotplug events, to configure/unconfigure via sclp instruction, to do iommu translations and provide s390 support for MSI/MSI-X notification processing.

---
 MAINTAINERS                       |   2 +
 default-configs/s390x-softmmu.mak |   1 +
 hw/s390x/Makefile.objs            |   1 +
 hw/s390x/css.c                    |   5 +
 hw/s390x/css.h                    |   1 +
 hw/s390x/s390-pci-bus.c           | 591 ++++++++++++++++++++++++++++++++++++++
 hw/s390x/s390-pci-bus.h           | 251 ++++++++++++++++
 hw/s390x/s390-virtio-ccw.c        |   7 +
 hw/s390x/sclp.c                   |  10 +-
 include/hw/s390x/sclp.h           |   8 +
 target-s390x/ioinst.c             |  52 ++++
 target-s390x/ioinst.h             |   1 +
 12 files changed, 929 insertions(+), 1 deletion(-)
 create mode 100644 hw/s390x/s390-pci-bus.c
 create mode 100644 hw/s390x/s390-pci-bus.h
MAINTAINERS +2 -0
index 01cfb05..19c274b 100644
@@ -534,6 +534,7 @@ S390 Virtio
 M: Alexander Graf <[email protected]>
 S: Maintained
 F: hw/s390x/s390-*.c
+X: hw/s390x/*pci*.[hc]
 
 S390 Virtio-ccw
 M: Cornelia Huck <[email protected]>
@@ -544,6 +545,7 @@ F: hw/s390x/s390-virtio-ccw.c
 F: hw/s390x/css.[hc]
 F: hw/s390x/sclp*.[hc]
 F: hw/s390x/ipl*.[hc]
+F: hw/s390x/*pci*.[hc]
 F: include/hw/s390x/
 F: pc-bios/s390-ccw/
 T: git git://github.com/cohuck/qemu virtio-ccw-upstr
default-configs/s390x-softmmu.mak +1 -0
index 126d88d..6ee2ff8 100644
@@ -1,3 +1,4 @@
+include pci.mak
 CONFIG_VIRTIO=y
 CONFIG_SCLPCONSOLE=y
 CONFIG_S390_FLIC=y
hw/s390x/Makefile.objs +1 -0
index 1ba6c3a..428d957 100644
@@ -8,3 +8,4 @@ obj-y += ipl.o
 obj-y += css.o
 obj-y += s390-virtio-ccw.o
 obj-y += virtio-ccw.o
+obj-y += s390-pci-bus.o
hw/s390x/css.c +5 -0
index b67c039..d0c5dde 100644
@@ -1299,6 +1299,11 @@ void css_generate_chp_crws(uint8_t cssid, uint8_t chpid)
     /* TODO */
 }
 
+void css_generate_css_crws(uint8_t cssid)
+{
+    css_queue_crw(CRW_RSC_CSS, 0, 0, cssid);
+}
+
 int css_enable_mcsse(void)
 {
     trace_css_enable_facility("mcsse");
hw/s390x/css.h +1 -0
index 33104ac..7e53148 100644
@@ -101,6 +101,7 @@ void css_queue_crw(uint8_t rsc, uint8_t erc, int chain, uint16_t rsid);
 void css_generate_sch_crws(uint8_t cssid, uint8_t ssid, uint16_t schid,
                            int hotplugged, int add);
 void css_generate_chp_crws(uint8_t cssid, uint8_t chpid);
+void css_generate_css_crws(uint8_t cssid);
 void css_adapter_interrupt(uint8_t isc);
 
 #define CSS_IO_ADAPTER_VIRTIO 1
hw/s390x/s390-pci-bus.c new file +591 -0

591 lines not on this page · raw message

hw/s390x/s390-pci-bus.h new file +251 -0

251 lines not on this page · raw message

hw/s390x/s390-virtio-ccw.c +7 -0
index bc4dc2a..0b8b42b 100644
@@ -18,6 +18,7 @@
 #include "css.h"
 #include "virtio-ccw.h"
 #include "qemu/config-file.h"
+#include "s390-pci-bus.h"
 
 #define TYPE_S390_CCW_MACHINE               "s390-ccw-machine"
 
@@ -91,6 +92,7 @@ static void ccw_init(MachineState *machine)
     uint8_t *storage_keys;
     int ret;
     VirtualCssBus *css_bus;
+    DeviceState *dev;
     QemuOpts *opts = qemu_opts_find(qemu_find_opts("memory"), NULL);
     ram_addr_t pad_size = 0;
     ram_addr_t maxmem = qemu_opt_get_size(opts, "maxmem", my_ram_size);
@@ -127,6 +129,11 @@ static void ccw_init(MachineState *machine)
                       machine->initrd_filename, "s390-ccw.img");
     s390_flic_init();
 
+    dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
+    object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
+                              OBJECT(dev), NULL);
+    qdev_init_nofail(dev);
+
     /* register hypercalls */
     virtio_ccw_register_hcalls();
 
hw/s390x/sclp.c +9 -1
index a759da7..a969975 100644
@@ -20,6 +20,7 @@
 #include "qemu/config-file.h"
 #include "hw/s390x/sclp.h"
 #include "hw/s390x/event-facility.h"
+#include "hw/s390x/s390-pci-bus.h"
 
 static inline SCLPEventFacility *get_event_facility(void)
 {
@@ -62,7 +63,8 @@ static void read_SCP_info(SCCB *sccb)
         read_info->entries[i].type = 0;
     }
 
-    read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO);
+    read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
+                                        SCLP_HAS_PCI_RECONFIG);
 
     /*
      * The storage increment size is a multiple of 1M and is a power of 2.
@@ -350,6 +352,12 @@ static void sclp_execute(SCCB *sccb, uint32_t code)
     case SCLP_UNASSIGN_STORAGE:
         unassign_storage(sccb);
         break;
+    case SCLP_CMDW_CONFIGURE_PCI:
+        s390_pci_sclp_configure(1, sccb);
+        break;
+    case SCLP_CMDW_DECONFIGURE_PCI:
+        s390_pci_sclp_configure(0, sccb);
+        break;
     default:
         efc->command_handler(ef, sccb, code);
         break;
include/hw/s390x/sclp.h +8 -0
index ec07a11..e8a64e2 100644
@@ -43,14 +43,22 @@
 #define SCLP_CMDW_CONFIGURE_CPU                 0x00110001
 #define SCLP_CMDW_DECONFIGURE_CPU               0x00100001
 
+/* SCLP PCI codes */
+#define SCLP_HAS_PCI_RECONFIG                   0x0000000040000000ULL
+#define SCLP_CMDW_CONFIGURE_PCI                 0x001a0001
+#define SCLP_CMDW_DECONFIGURE_PCI               0x001b0001
+#define SCLP_RECONFIG_PCI_ATPYE                 2
+
 /* SCLP response codes */
 #define SCLP_RC_NORMAL_READ_COMPLETION          0x0010
 #define SCLP_RC_NORMAL_COMPLETION               0x0020
 #define SCLP_RC_SCCB_BOUNDARY_VIOLATION         0x0100
+#define SCLP_RC_NO_ACTION_REQUIRED              0x0120
 #define SCLP_RC_INVALID_SCLP_COMMAND            0x01f0
 #define SCLP_RC_CONTAINED_EQUIPMENT_CHECK       0x0340
 #define SCLP_RC_INSUFFICIENT_SCCB_LENGTH        0x0300
 #define SCLP_RC_STANDBY_READ_COMPLETION         0x0410
+#define SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED       0x09f0
 #define SCLP_RC_INVALID_FUNCTION                0x40f0
 #define SCLP_RC_NO_EVENT_BUFFERS_STORED         0x60f0
 #define SCLP_RC_INVALID_SELECTION_MASK          0x70f0
target-s390x/ioinst.c +52 -0

76 lines not on this page · raw message

target-s390x/ioinst.h +1 -0
index 29f6423..1efe16c 100644
@@ -204,6 +204,7 @@ typedef struct CRW {
 
 #define CRW_RSC_SUBCH 0x3
 #define CRW_RSC_CHP   0x4
+#define CRW_RSC_CSS   0xb
 
 /* I/O interruption code */
 typedef struct IOIntCode {
Signature · 1 line
2.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3 V3] s390: Add PCI bus support

Quoted 7 lines · On Fri, 9 Jan 2015 09:04:38 +0100 Frank Blaschka…
hw/s390x/s390-pci-bus.c new file +7 -0
+static IOMMUTLBEntry s390_translate_iommu(MemoryRegion *iommu, hwaddr addr,
+                                          bool is_write)
+{
+    uint64_t pte;
+    uint32_t flags;
+    S390PCIBusDevice *pbdev = container_of(iommu, S390PCIBusDevice, mr);
+    S390pciState *s = S390_PCI_HOST_BRIDGE(pci_device_root_bus(pbdev->pdev)

hw/s390x/s390-pci-bus.c line 317

Quoted 10 lines
hw/s390x/s390-pci-bus.c new file +10 -0
+                                           ->qbus.parent);
+    IOMMUTLBEntry ret = {
+        .target_as = &address_space_memory,
+        .iova = 0,
+        .translated_addr = 0,
+        .addr_mask = ~(hwaddr)0,
+        .perm = IOMMU_NONE,
+    };
+
+    DPRINTF("iommu trans addr 0x%lx\n", addr);

This won't compile on 32 bit with debugging enabled (needs to be PRIx64).

No need to resend the series, though; I've fixed up this and the other occurence in this file (and the two in the inst code) since it is a trivial change.

hw/s390x/s390-pci-bus.c line 319

Quoted 2 lines
hw/s390x/s390-pci-bus.c new file +2 -0
+
+    /* s390 does not have an APIC maped to main storage so we use
                                       ^

And while at it, I also fixed up this typo :)

Quoted 2 lines
hw/s390x/s390-pci-bus.c new file +2 -0
+     * a separate AddressSpace only for msix notifications
+     */
#3 lore raw

PATCH v3 2/3 s390: implement pci instructions

In reply to Frank Blaschka

From: Frank Blaschka <[email protected]>

This patch implements the s390 pci instructions in qemu. It allows to access and drive pci devices attached to the s390 pci bus. Because of platform constrains devices using IO BARs are not supported. Also a device has to support MSI/MSI-X to run on s390.

---
 hw/s390x/Makefile.objs   |   2 +-
 hw/s390x/s390-pci-inst.c | 811 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/s390x/s390-pci-inst.h | 288 +++++++++++++++++
 target-s390x/kvm.c       | 153 +++++++++
 4 files changed, 1253 insertions(+), 1 deletion(-)
 create mode 100644 hw/s390x/s390-pci-inst.c
 create mode 100644 hw/s390x/s390-pci-inst.h
hw/s390x/Makefile.objs +1 -1
index 428d957..27cd75a 100644
@@ -8,4 +8,4 @@ obj-y += ipl.o
 obj-y += css.o
 obj-y += s390-virtio-ccw.o
 obj-y += virtio-ccw.o
-obj-y += s390-pci-bus.o
+obj-y += s390-pci-bus.o s390-pci-inst.o
hw/s390x/s390-pci-inst.c new file +811 -0

811 lines not on this page · raw message

hw/s390x/s390-pci-inst.h new file +288 -0

288 lines not on this page · raw message

target-s390x/kvm.c +153 -0

202 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

________________________________________________________________________________________________________
*** CID 1264324: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264324: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->noi" with type
"unsigned short" (16 bits, unsigned) is promoted in "(pbdev->isc << 28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Does this code work as intended?

#5 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

#6 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)

     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;

- stw_p(&fib.data, data);
+ stl_p(&fib.data, data);

     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;

fixes the implicit sign extension within the assignment preceding it. Let me explain it again real slow:

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

Regarding the leak, I prefer my patch, because it avoids the free on error. But you're the maintainer.

#7 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 61 lines · Markus Armbruster
Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

(quoted diff) +1 -1
@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;
-    stw_p(&fib.data, data);
+    stl_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;

fixes the implicit sign extension within the assignment preceding it. Let me explain it again real slow:

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

4. The two shift results stay int and get ored.

Quoted 7 lines

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

Quoted 2 lines

Regarding the leak, I prefer my patch, because it avoids the free on error. But you're the maintainer.

#8 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 53 lines · Markus Armbruster
Quoted 51 lines · Markus Armbruster
Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

(quoted diff) +1 -1
@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;
-    stw_p(&fib.data, data);
+    stl_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;

fixes the implicit sign extension within the assignment preceding it. Let me explain it again real slow:

Quoted 7 lines
Quoted 7 lines

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

uint16_t to int

Quoted 6 lines
Quoted 6 lines

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

Quoted 9 lines
Quoted 4 lines

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

Hi Markus,

thx for your explanation. I did not see a problem since ISC is not bigger than 0x7 so MSB is never set. But the time I wrote the code I was not aware of ind_offset is uint64_t since zpci defines only a 6 bit field for this value.

How can I avoid the sign extension and make Coverity happy?

Quoted 2 lines
Quoted 2 lines

Regarding the leak, I prefer my patch, because it avoids the free on error. But you're the maintainer.

This is fine for me as well ...

Thx,

Frank

Quoted 1 line
#9 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 54 lines · Frank Blaschka
Quoted 53 lines · Markus Armbruster
Quoted 51 lines · Markus Armbruster
Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

(quoted diff) +1 -1
@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;
-    stw_p(&fib.data, data);
+    stl_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;

fixes the implicit sign extension within the assignment preceding it. Let me explain it again real slow:

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

uint16_t to int

Yes, that's what I meant :)

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

Quoted 9 lines
Quoted 4 lines

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

Quoted 5 lines

Hi Markus,

thx for your explanation. I did not see a problem since ISC is not bigger than 0x7 so MSB is never set. But the time I wrote the code I was not aware of ind_offset is uint64_t since zpci defines only a 6 bit field for this value.

Okay.

Quoted 1 line

How can I avoid the sign extension and make Coverity happy?

Casting pbdev->routes.adapter.ind_offset to uint32_t should do. Then, all operands of | are either int (promoted from narrower unsigned type) or uint32_t (type cast). Conversion from int to uint32_t won't sign-extend as long as int is at least 32 bits. Surely the case for anything that can run QEMU.

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

Regarding the leak, I prefer my patch, because it avoids the free on error. But you're the maintainer.

This is fine for me as well ...

Thx,

Frank

Quoted 1 line
#10 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 91 lines · Markus Armbruster
Quoted 54 lines · Frank Blaschka
Quoted 53 lines · Markus Armbruster
Quoted 51 lines · Markus Armbruster
Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

(quoted diff) +1 -1
@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;
-    stw_p(&fib.data, data);
+    stl_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;

fixes the implicit sign extension within the assignment preceding it. Let me explain it again real slow:

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

uint16_t to int

Yes, that's what I meant :)

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

Quoted 9 lines
Quoted 4 lines

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

Quoted 5 lines

Hi Markus,

thx for your explanation. I did not see a problem since ISC is not bigger than 0x7 so MSB is never set. But the time I wrote the code I was not aware of ind_offset is uint64_t since zpci defines only a 6 bit field for this value.

Okay.

Quoted 7 lines
Quoted 1 line

How can I avoid the sign extension and make Coverity happy?

Casting pbdev->routes.adapter.ind_offset to uint32_t should do. Then, all operands of | are either int (promoted from narrower unsigned type) or uint32_t (type cast). Conversion from int to uint32_t won't sign-extend as long as int is at least 32 bits. Surely the case for anything that can run QEMU.

Yup, it hushes up Coverity (I checked).

hw/s390x/s390-pci-inst.c +2 -2
index 5ea13e5..2bed3f5 100644
@@ -785,8 +785,8 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     stq_p(&fib.fmb_addr, pbdev->fmb_addr);
 
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
-           (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
-           pbdev->routes.adapter.summary_offset;
+           ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |
+           (pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;
     stw_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
#11 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 39 lines · Markus Armbruster
Quoted 30 lines · Markus Armbruster
Quoted 10 lines · Frank Blaschka
Quoted 8 lines · Markus Armbruster
Quoted 7 lines · Markus Armbruster

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

uint16_t to int

Yes, that's what I meant :)

Quoted 15 lines
Quoted 15 lines
Quoted 10 lines

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

hw/s390x/s390-pci-inst.c +0 -0
index 5ea13e5..2bed3f5 100644

hw/s390x/s390-pci-inst.c int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba) · line 792

Quoted 10 lines
hw/s390x/s390-pci-inst.c +2 -2
@@ -785,8 +785,8 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     stq_p(&fib.fmb_addr, pbdev->fmb_addr);
 
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
-           (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
-           pbdev->routes.adapter.summary_offset;
+           ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |
+           (pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;
     stw_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {

This doesn't make sense to me as a fix for the problem you describe above. Either
(1) pbdev->isc may have bit 3 set: in this case shifting it left by 28 is undefined behaviour in C, and we must not do it (and adding a cast to ind_offset doesn't help us at all)
(2) pbdev->isc is guaranteed never to have bit 3 set: in this case the sign extension to uint64_t in step 6 above will have no effect, because the sign bit in the int result will be clear

So you can either:
(1) cast pbdev->isc to uint32_t before shifting, thus ensuring that we do all our | operations on unsigned types and that we won't shift into the sign bit regardless of pbdev->isc's value
(2) state that we know pbdev->isc is always less than 8 and so this is a coverity false positive to be suppressed via the web UI

But the patch you have doesn't seem like the right thing to me.

thanks
-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 49 lines · Peter Maydell
Quoted 48 lines · Markus Armbruster
Quoted 30 lines · Markus Armbruster
Quoted 10 lines · Frank Blaschka
Quoted 8 lines · Markus Armbruster
Quoted 7 lines · Markus Armbruster

1. pbdev->isc gets promoted from uint8_t to int as operand of binary << (usual arithmetic conversions ISO/IEC 9899:1999 6.3.1.8)

2. The int result is shifted left 28 bits. This can set the MSB.

3. Likewise: pbdev->noi gets promoted from uint64_t to int, and shifted left 16 bits.

uint16_t to int

Yes, that's what I meant :)

Quoted 15 lines
Quoted 15 lines
Quoted 10 lines

4. The two shift results stay int and get ored.

5. pbdev->routes.adapter.ind_offset stays uint64_t, and is shifted left 8 bits.

6. The next or's left operand is the int result of 4 and the right operant is the uint64_t result of 5. Therefore, the left operand is *sign-extended* from int to uint64_t. This copies bit#7 of pbdev->isc to bits#31..63. Whoops.

I neglected to say: we don't currently use the upper 32 bits, and as long as we do that, the sign extension is harmless. I'd recommend to avoid it all the same, for robustness, and to hush up Coverity.

hw/s390x/s390-pci-inst.c +2 -2
index 5ea13e5..2bed3f5 100644
@@ -785,8 +785,8 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t
 r1, uint64_t fiba)
     stq_p(&fib.fmb_addr, pbdev->fmb_addr);
 
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
-           (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
-           pbdev->routes.adapter.summary_offset;
+           ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |
+           (pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;
     stw_p(&fib.data, data);
 
Quoted 7 lines
Quoted 2 lines
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {

This doesn't make sense to me as a fix for the problem you describe above. Either
(1) pbdev->isc may have bit 3 set: in this case shifting it left by 28 is undefined behaviour in C,

Correct.

Quoted 1 line

and we must not do it

I suspect we shift signed values all over the place, without regard for signed overflow. Machines are fine with that, but some day some compiler wiseguy may find a way to save a femtosecond or two for some program that never does that, breaking programs that do it, and then we'll be in trouble.

We should follow the kernel's lead and compile with -fno-strict-overflow.

Quoted 1 line

(and adding a cast to ind_offset doesn't help us at all)

Correct, it doesn't help with the signed left shift of pbdev->isc.

Quoted 5 lines

(2) pbdev->isc is guaranteed never to have bit 3 set: in this case the sign extension to uint64_t in step 6 above will have no effect, because the sign bit in the int result will be clear

Quoted 8 lines

So you can either:
(1) cast pbdev->isc to uint32_t before shifting, thus ensuring that we do all our | operations on unsigned types and that we won't shift into the sign bit regardless of pbdev->isc's value
(2) state that we know pbdev->isc is always less than 8 and so this is a coverity false positive to be suppressed via the web UI

But the patch you have doesn't seem like the right thing to me.

Frank's code, Frank's choice :)

#13 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

Quoted 5 lines · Markus Armbruster

I suspect we shift signed values all over the place, without regard for signed overflow. Machines are fine with that, but some day some compiler wiseguy may find a way to save a femtosecond or two for some program that never does that, breaking programs that do it, and then we'll be in trouble.

clang with its undefined behaviour sanitizers will warn at runtime when we do this. I've sent out some patches to fix instances of this in the past. Coverity will also warn in some cases I think.

Quoted 2 lines

We should follow the kernel's lead and compile with -fno-strict-overflow.

I don't believe that option affects signed shifts, only signed addition, subtraction and multiplication.

-- PMM

#14 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

I suspect we shift signed values all over the place, without regard for signed overflow. Machines are fine with that, but some day some compiler wiseguy may find a way to save a femtosecond or two for some program that never does that, breaking programs that do it, and then we'll be in trouble.

As I said before, if there was a way to save those femtoseconds, they would have already tried.

More likely, the compiler people know they would become the main attractions at pitch and feather spectacles, so they're not going to treat that as undefined behavior.

Paolo

#15 lore raw

Re: [Qemu-devel] [PATCH 2/3 V3] s390: implement pci instructions

In reply to Markus Armbruster
Quoted 48 lines · On Tue, 20 Jan 2015 13:33:27 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 27 lines · Cornelia Huck
Quoted 17 lines · On Tue, 20 Jan 2015 10:45:41 +0100 Markus Armbruster <[email protected]> wrote:

This patch makes Coverity unhappy:

*** CID 1264326: Unintended sign extension (SIGN_EXTENSION) /hw/s390x/s390-pci-inst.c: 787 in stpcifc_service_call()

781         stq_p(&fib.pal, pbdev->pal);
782         stq_p(&fib.iota, pbdev->g_iota);
783         stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
784         stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
785         stq_p(&fib.fmb_addr, pbdev->fmb_addr);

786

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

CID 1264326: Unintended sign extension (SIGN_EXTENSION) Suspicious implicit sign extension: "pbdev->isc" with type
"unsigned char" (8 bits, unsigned) is promoted in "(pbdev->isc <<
28) | (pbdev->noi << 16)" to type "int" (32 bits, signed), then sign-extended to type "unsigned long" (64 bits, unsigned). If "(pbdev->isc << 28) | (pbdev->noi << 16)" is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

Quoted 6 lines

787 data = (pbdev->isc << 28) | (pbdev->noi << 16) |
788 (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
789 pbdev->routes.adapter.summary_offset;
790 stw_p(&fib.data, data);
791
792 if (pbdev->fh >> ENABLE_BIT_OFFSET) {

Quoted 6 lines

There's a fix for this (and the memory leak):

http://marc.info/?l=qemu-devel&m=142124886620078&w=2

The patch is sitting in my queue, will send with the next pile of s390x updates.

I can't see how

(quoted diff) +1 -1
@@ -787,7 +787,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba)
     data = (pbdev->isc << 28) | (pbdev->noi << 16) |
            (pbdev->routes.adapter.ind_offset << 8) | (pbdev->sum << 7) |
            pbdev->routes.adapter.summary_offset;
-    stw_p(&fib.data, data);
+    stl_p(&fib.data, data);
 
     if (pbdev->fh >> ENABLE_BIT_OFFSET) {
         fib.fc |= 0x80;
Quoted 1 line

fixes the implicit sign extension within the assignment preceding it.

What, I am expected to actually read the explanations? :)

Quoted 2 lines

Regarding the leak, I prefer my patch, because it avoids the free on error. But you're the maintainer.

Indeed, that's a good point.

I'll drop Frank's original patch and instead take your memory leak fix. Will take a patch from Frank for the sign extension stuff (and the stw/stl fix) as well once it has been posted.

#16 lore raw

PATCH v3 3/3 kvm: extend kvm_irqchip_add_msi_route to work on s390

In reply to Frank Blaschka

From: Frank Blaschka <[email protected]>

on s390 MSI-X irqs are presented as thin or adapter interrupts for this we have to reorganize the routing entry to contain valid information for the adapter interrupt code on s390. To minimize impact on existing code we introduce an architecture function to fixup the routing entry.

---
 include/sysemu/kvm.h |  4 ++++
 kvm-all.c            |  7 +++++++
 target-arm/kvm.c     |  6 ++++++
 target-i386/kvm.c    |  6 ++++++
 target-mips/kvm.c    |  6 ++++++
 target-ppc/kvm.c     |  6 ++++++
 target-s390x/kvm.c   | 26 ++++++++++++++++++++++++++
 7 files changed, 61 insertions(+)
include/sysemu/kvm.h +4 -0
index 104cf35..30cb84d 100644
@@ -158,6 +158,7 @@ extern bool kvm_readonly_mem_allowed;
 
 struct kvm_run;
 struct kvm_lapic_state;
+struct kvm_irq_routing_entry;
 
 typedef struct KVMCapabilityInfo {
     const char *name;
@@ -270,6 +271,9 @@ int kvm_arch_on_sigbus(int code, void *addr);
 
 void kvm_arch_init_irq_routing(KVMState *s);
 
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                             uint64_t address, uint32_t data);
+
 int kvm_set_irq(KVMState *s, int irq, int level);
 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
 
kvm-all.c +7 -0
index 18cc6b4..2f21a4e 100644
@@ -1225,6 +1225,10 @@ int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
     kroute.u.msi.address_lo = (uint32_t)msg.address;
     kroute.u.msi.address_hi = msg.address >> 32;
     kroute.u.msi.data = le32_to_cpu(msg.data);
+    if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data)) {
+        kvm_irqchip_release_virq(s, virq);
+        return -EINVAL;
+    }
 
     kvm_add_routing_entry(s, &kroute);
     kvm_irqchip_commit_routes(s);
@@ -1250,6 +1254,9 @@ int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg)
     kroute.u.msi.address_lo = (uint32_t)msg.address;
     kroute.u.msi.address_hi = msg.address >> 32;
     kroute.u.msi.data = le32_to_cpu(msg.data);
+    if (kvm_arch_fixup_msi_route(&kroute, msg.address, msg.data)) {
+        return -EINVAL;
+    }
 
     return kvm_update_routing_entry(s, &kroute);
 }
target-arm/kvm.c +6 -0
index 4d81f3d..23cefe9 100644
@@ -548,3 +548,9 @@ int kvm_arch_irqchip_create(KVMState *s)
 
     return 0;
 }
+
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                             uint64_t address, uint32_t data)
+{
+    return 0;
+}
target-i386/kvm.c +6 -0
index f92edfe..54ccb89 100644
@@ -2733,3 +2733,9 @@ int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
     return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX |
                                                 KVM_DEV_IRQ_HOST_MSIX);
 }
+
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                             uint64_t address, uint32_t data)
+{
+    return 0;
+}
target-mips/kvm.c +6 -0
index a761ea5..b68191c 100644
@@ -688,3 +688,9 @@ int kvm_arch_get_registers(CPUState *cs)
 
     return ret;
 }
+
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                             uint64_t address, uint32_t data)
+{
+    return 0;
+}
target-ppc/kvm.c +6 -0
index 6843fa0..04c83cd 100644
@@ -2388,3 +2388,9 @@ out_close:
 error_out:
     return;
 }
+
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                             uint64_t address, uint32_t data)
+{
+    return 0;
+}
target-s390x/kvm.c +26 -0
index a6849ab..89cc218 100644
@@ -41,6 +41,7 @@
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"
+#include "hw/s390x/s390-pci-bus.h"
 
 /* #define DEBUG_KVM */
 
@@ -1514,3 +1515,28 @@ int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
 
     return ret;
 }
+
+int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
+                              uint64_t address, uint32_t data)
+{
+    S390PCIBusDevice *pbdev;
+    uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
+    uint32_t vec = data & ZPCI_MSI_VEC_MASK;
+
+    pbdev = s390_pci_find_dev_by_fid(fid);
+    if (!pbdev) {
+        DPRINTF("add_msi_route no dev\n");
+        return -ENODEV;
+    }
+
+    pbdev->routes.adapter.ind_offset = vec;
+
+    route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
+    route->flags = 0;
+    route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
+    route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
+    route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
+    route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
+    route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
+    return 0;
+}
Signature · 1 line
2.1.4
#17 lore raw
In reply to Frank Blaschka
Quoted 11 lines · On Fri, 9 Jan 2015 09:04:37 +0100 Frank Blaschka…

This set of patches implemets PCI support for the s390 platform. Now it is possible to run virtio-net-pci and potentially all virtual pci devices conforming to s390 platform constrains.

V1 added lot of feedback from Alex Graf
fixed tons of endian issues

V2 added couple of small improvments and code cleanup
fixed build on 32-bit and non Linux
added pci to maintainer file

Quoted 6 lines

V3 fix css_generate_css_crws to pass cssid to css_queue_crw
fix and compile test w32 build
review #defines for ULL usage

patches apply to latest qemu master
please consider for integration into 2.3

Seems to look OK now. I've compiled Linux 64 bit and 32 bit and also verified that mingw builds now seem to be fine.

Outlook for a pull request next week is good :)