From nobody Sun May 5 17:32:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538036763828102.0909706234147; Thu, 27 Sep 2018 01:26:03 -0700 (PDT) Received: from localhost ([::1]:34412 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5RcP-0002Og-LM for importer@patchew.org; Thu, 27 Sep 2018 04:25:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43654) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5RaP-0001Lq-9N for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5RaO-0003Wt-H6 for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5RaO-0003Wi-Af; Thu, 27 Sep 2018 04:23:48 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 990963082128; Thu, 27 Sep 2018 08:23:47 +0000 (UTC) Received: from thuth.com (ovpn-116-44.ams2.redhat.com [10.36.116.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1FA1E600C7; Thu, 27 Sep 2018 08:23:44 +0000 (UTC) From: Thomas Huth To: qemu-s390x@nongnu.org, Cornelia Huck Date: Thu, 27 Sep 2018 10:23:33 +0200 Message-Id: <1538036615-32542-2-git-send-email-thuth@redhat.com> In-Reply-To: <1538036615-32542-1-git-send-email-thuth@redhat.com> References: <1538036615-32542-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 27 Sep 2018 08:23:47 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/3] hw/s390x/ipl: Fix alignment problems of S390IPLState members X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christian Borntraeger , qemu-devel@nongnu.org, "Dr. David Alan Gilbert" , David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The IplParameterBlock and QemuIplParameters structures are declared with QEMU_PACKED, so the compiler assumes that the structures do not need to be aligned in memory. Since the are listed after a "bool" within the S390IPLState, the IplParameterBlock and QemuIplParameters are also indeed mis-aligned in memory. This causes problems on Sparc during migration, since we use VMSTATE_UINT16 in vmstate_iplb to access the devno member for exampl= e, and the corresponding migration functions (like qemu_get_be16s) then try to access a 16-bit value from a misaligned memory address. The easiest solution to fix this problem is to move the packed structures to the beginning of the S390IPLState, right after the DeviceState of course which has to stay first for QOM reasons. But since DeviceState is a non-pac= ked struct, we can be sure that it will be padded to the correct alignment at t= he end. If not, the QEMU_BUILD_BUG_MSG in this patch will tell us. Signed-off-by: Thomas Huth Reviewed-by: David Hildenbrand --- hw/s390x/ipl.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hw/s390x/ipl.h b/hw/s390x/ipl.h index 4e87b89..b3a07a1 100644 --- a/hw/s390x/ipl.h +++ b/hw/s390x/ipl.h @@ -132,15 +132,15 @@ typedef struct QemuIplParameters QemuIplParameters; struct S390IPLState { /*< private >*/ DeviceState parent_obj; + IplParameterBlock iplb; + QemuIplParameters qipl; uint64_t start_addr; uint64_t compat_start_addr; uint64_t bios_start_addr; uint64_t compat_bios_start_addr; bool enforce_bios; - IplParameterBlock iplb; bool iplb_valid; bool netboot; - QemuIplParameters qipl; /* reset related properties don't have to be migrated or reset */ enum s390_reset reset_type; int reset_cpu_index; @@ -157,6 +157,7 @@ struct S390IPLState { bool iplbext_migration; }; typedef struct S390IPLState S390IPLState; +QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wr= ong"); =20 #define S390_IPL_TYPE_FCP 0x00 #define S390_IPL_TYPE_CCW 0x02 --=20 1.8.3.1 From nobody Sun May 5 17:32:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538036763121177.215372663381; Thu, 27 Sep 2018 01:26:03 -0700 (PDT) Received: from localhost ([::1]:34417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5RcU-0002Wh-03 for importer@patchew.org; Thu, 27 Sep 2018 04:25:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43693) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5RaS-0001N2-IE for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5RaR-0003Xt-Lo for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59852) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5RaR-0003Xe-BY; Thu, 27 Sep 2018 04:23:51 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AD49B308AA0E; Thu, 27 Sep 2018 08:23:50 +0000 (UTC) Received: from thuth.com (ovpn-116-44.ams2.redhat.com [10.36.116.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3044D600C7; Thu, 27 Sep 2018 08:23:47 +0000 (UTC) From: Thomas Huth To: qemu-s390x@nongnu.org, Cornelia Huck Date: Thu, 27 Sep 2018 10:23:34 +0200 Message-Id: <1538036615-32542-3-git-send-email-thuth@redhat.com> In-Reply-To: <1538036615-32542-1-git-send-email-thuth@redhat.com> References: <1538036615-32542-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Thu, 27 Sep 2018 08:23:50 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/3] hw/s390x/css: Remove QEMU_PACKED from struct SenseId X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christian Borntraeger , qemu-devel@nongnu.org, "Dr. David Alan Gilbert" , David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The uint16_t member cu_type of struct SenseId is not naturally aligned, and since the struct is marked with QEMU_PACKED, this can lead to unaligned memory accesses - which does not work on architectures like Sparc. Thus remove the QEMU_PACKED here and rather copy the struct byte by byte when we do copy_sense_id_to_guest(). Signed-off-by: Thomas Huth Reviewed-by: David Hildenbrand --- hw/s390x/css.c | 38 ++++++++++++++++++++++---------------- include/hw/s390x/css.h | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/hw/s390x/css.c b/hw/s390x/css.c index 5a9fe45..04ec5cc 100644 --- a/hw/s390x/css.c +++ b/hw/s390x/css.c @@ -750,20 +750,25 @@ static void sch_handle_halt_func(SubchDev *sch) =20 } =20 -static void copy_sense_id_to_guest(SenseId *dest, SenseId *src) +/* + * As the SenseId struct cannot be packed (would cause unaligned accesses)= , we + * have to copy the individual fields to an unstructured area using the co= rrect + * layout (see SA22-7204-01 "Common I/O-Device Commands"). + */ +static void copy_sense_id_to_guest(uint8_t *dest, SenseId *src) { int i; =20 - dest->reserved =3D src->reserved; - dest->cu_type =3D cpu_to_be16(src->cu_type); - dest->cu_model =3D src->cu_model; - dest->dev_type =3D cpu_to_be16(src->dev_type); - dest->dev_model =3D src->dev_model; - dest->unused =3D src->unused; - for (i =3D 0; i < ARRAY_SIZE(dest->ciw); i++) { - dest->ciw[i].type =3D src->ciw[i].type; - dest->ciw[i].command =3D src->ciw[i].command; - dest->ciw[i].count =3D cpu_to_be16(src->ciw[i].count); + dest[0] =3D src->reserved; + stw_be_p(dest + 1, src->cu_type); + dest[3] =3D src->cu_model; + stw_be_p(dest + 4, src->dev_type); + dest[6] =3D src->dev_model; + dest[7] =3D src->unused; + for (i =3D 0; i < ARRAY_SIZE(src->ciw); i++) { + dest[8 + i * 4] =3D src->ciw[i].type; + dest[9 + i * 4] =3D src->ciw[i].command; + stw_be_p(dest + 10 + i * 4, src->ciw[i].count); } } =20 @@ -1044,9 +1049,10 @@ static int css_interpret_ccw(SubchDev *sch, hwaddr c= cw_addr, break; case CCW_CMD_SENSE_ID: { - SenseId sense_id; + /* According to SA22-7204-01, Sense-ID can store up to 256 bytes */ + uint8_t sense_id[256]; =20 - copy_sense_id_to_guest(&sense_id, &sch->id); + copy_sense_id_to_guest(sense_id, &sch->id); /* Sense ID information is device specific. */ if (check_len) { if (ccw.count !=3D sizeof(sense_id)) { @@ -1060,11 +1066,11 @@ static int css_interpret_ccw(SubchDev *sch, hwaddr = ccw_addr, * have enough place to store at least bytes 0-3. */ if (len >=3D 4) { - sense_id.reserved =3D 0xff; + sense_id[0] =3D 0xff; } else { - sense_id.reserved =3D 0; + sense_id[0] =3D 0; } - ccw_dstream_write_buf(&sch->cds, &sense_id, len); + ccw_dstream_write_buf(&sch->cds, sense_id, len); sch->curr_status.scsw.count =3D ccw_dstream_residual_count(&sch->c= ds); ret =3D 0; break; diff --git a/include/hw/s390x/css.h b/include/hw/s390x/css.h index 9da5912..bec82d0 100644 --- a/include/hw/s390x/css.h +++ b/include/hw/s390x/css.h @@ -48,7 +48,7 @@ typedef struct SenseId { uint8_t unused; /* padding byte */ /* extended part */ CIW ciw[MAX_CIWS]; /* variable # of CIWs */ -} QEMU_PACKED SenseId; +} SenseId; /* Note: No QEMU_PACKED due to unaligned memb= ers */ =20 /* Channel measurements, from linux/drivers/s390/cio/cmf.c. */ typedef struct CMB { --=20 1.8.3.1 From nobody Sun May 5 17:32:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538036772608697.1606022971592; Thu, 27 Sep 2018 01:26:12 -0700 (PDT) Received: from localhost ([::1]:34418 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5Rce-0002gI-EU for importer@patchew.org; Thu, 27 Sep 2018 04:26:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43735) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5RaX-0001S7-Qc for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5RaV-0003Yb-7i for qemu-devel@nongnu.org; Thu, 27 Sep 2018 04:23:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38640) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5RaU-0003YL-Vz; Thu, 27 Sep 2018 04:23:55 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 44BB5C0467C0; Thu, 27 Sep 2018 08:23:54 +0000 (UTC) Received: from thuth.com (ovpn-116-44.ams2.redhat.com [10.36.116.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 41C9F600CC; Thu, 27 Sep 2018 08:23:50 +0000 (UTC) From: Thomas Huth To: qemu-s390x@nongnu.org, Cornelia Huck Date: Thu, 27 Sep 2018 10:23:35 +0200 Message-Id: <1538036615-32542-4-git-send-email-thuth@redhat.com> In-Reply-To: <1538036615-32542-1-git-send-email-thuth@redhat.com> References: <1538036615-32542-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 27 Sep 2018 08:23:54 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 3/3] hw/s390x/ioinst: Fix alignment problem in struct SubchDev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christian Borntraeger , qemu-devel@nongnu.org, "Dr. David Alan Gilbert" , David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" struct SubchDev embeds several other structures which are marked with QEMU_PACKED. This causes the compiler to not care for proper alignment of these structures. When we later pass around pointers to the unaligned struct members during migration, this causes problems on host architectures like Sparc that can not do unaligned memory access. Most of the structs in ioinst.h are naturally aligned, so we can fix most of the problem by removing the QEMU_PACKED statements (and use QEMU_BUILD_BUG_MSG() statements instead to make sure that there is no padding). However, for the struct SCHIB, we have to keep the QEMU_PACKED since the compiler adds some padding here otherwise. Move this struct to the beginning of struct SubchDev instead to fix the alignment problem here, too. Signed-off-by: Thomas Huth Reviewed-by: David Hildenbrand --- include/hw/s390x/css.h | 4 ++-- include/hw/s390x/ioinst.h | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/hw/s390x/css.h b/include/hw/s390x/css.h index bec82d0..aae19c4 100644 --- a/include/hw/s390x/css.h +++ b/include/hw/s390x/css.h @@ -118,11 +118,12 @@ typedef enum IOInstEnding { typedef struct SubchDev SubchDev; struct SubchDev { /* channel-subsystem related things: */ + SCHIB curr_status; /* Needs alignment and thus must come fir= st */ + ORB orb; uint8_t cssid; uint8_t ssid; uint16_t schid; uint16_t devno; - SCHIB curr_status; uint8_t sense_data[32]; hwaddr channel_prog; CCW1 last_cmd; @@ -131,7 +132,6 @@ struct SubchDev { bool thinint_active; uint8_t ccw_no_data_cnt; uint16_t migrated_schid; /* used for missmatch detection */ - ORB orb; CcwDataStream cds; /* transport-provided data: */ int (*ccw_cb) (SubchDev *, CCW1); diff --git a/include/hw/s390x/ioinst.h b/include/hw/s390x/ioinst.h index 5f2db69..c6737a3 100644 --- a/include/hw/s390x/ioinst.h +++ b/include/hw/s390x/ioinst.h @@ -25,7 +25,8 @@ typedef struct SCSW { uint8_t dstat; uint8_t cstat; uint16_t count; -} QEMU_PACKED SCSW; +} SCSW; +QEMU_BUILD_BUG_MSG(sizeof(SCSW) !=3D 12, "size of SCSW is wrong"); =20 #define SCSW_FLAGS_MASK_KEY 0xf000 #define SCSW_FLAGS_MASK_SCTL 0x0800 @@ -94,7 +95,8 @@ typedef struct PMCW { uint8_t pam; uint8_t chpid[8]; uint32_t chars; -} QEMU_PACKED PMCW; +} PMCW; +QEMU_BUILD_BUG_MSG(sizeof(PMCW) !=3D 28, "size of PMCW is wrong"); =20 #define PMCW_FLAGS_MASK_QF 0x8000 #define PMCW_FLAGS_MASK_W 0x4000 @@ -127,7 +129,8 @@ typedef struct IRB { uint32_t esw[5]; uint32_t ecw[8]; uint32_t emw[8]; -} QEMU_PACKED IRB; +} IRB; +QEMU_BUILD_BUG_MSG(sizeof(IRB) !=3D 96, "size of IRB is wrong"); =20 /* operation request block */ typedef struct ORB { @@ -136,7 +139,8 @@ typedef struct ORB { uint8_t lpm; uint8_t ctrl1; uint32_t cpa; -} QEMU_PACKED ORB; +} ORB; +QEMU_BUILD_BUG_MSG(sizeof(ORB) !=3D 12, "size of ORB is wrong"); =20 #define ORB_CTRL0_MASK_KEY 0xf000 #define ORB_CTRL0_MASK_SPND 0x0800 @@ -165,7 +169,8 @@ typedef struct CCW0 { uint8_t flags; uint8_t reserved; uint16_t count; -} QEMU_PACKED CCW0; +} CCW0; +QEMU_BUILD_BUG_MSG(sizeof(CCW0) !=3D 8, "size of CCW0 is wrong"); =20 /* channel command word (type 1) */ typedef struct CCW1 { @@ -173,7 +178,8 @@ typedef struct CCW1 { uint8_t flags; uint16_t count; uint32_t cda; -} QEMU_PACKED CCW1; +} CCW1; +QEMU_BUILD_BUG_MSG(sizeof(CCW1) !=3D 8, "size of CCW1 is wrong"); =20 #define CCW_FLAG_DC 0x80 #define CCW_FLAG_CC 0x40 @@ -192,7 +198,8 @@ typedef struct CCW1 { typedef struct CRW { uint16_t flags; uint16_t rsid; -} QEMU_PACKED CRW; +} CRW; +QEMU_BUILD_BUG_MSG(sizeof(CRW) !=3D 4, "size of CRW is wrong"); =20 #define CRW_FLAGS_MASK_S 0x4000 #define CRW_FLAGS_MASK_R 0x2000 --=20 1.8.3.1