From nobody Thu Nov 6 01:12:41 2025 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; dkim=fail; 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 1538298762673256.8395266192272; Sun, 30 Sep 2018 02:12:42 -0700 (PDT) Received: from localhost ([::1]:54563 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g6XmL-0007Aw-1R for importer@patchew.org; Sun, 30 Sep 2018 05:12:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41871) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g6WsF-000360-7K for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:14:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g6WsB-0001GX-HJ for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:14:41 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]:51728) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g6WsA-0001E8-P6 for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:14:39 -0400 Received: by mail-wm1-x32e.google.com with SMTP id y25-v6so5779916wmi.1 for ; Sun, 30 Sep 2018 01:14:37 -0700 (PDT) Received: from 640k.lan (94-36-187-248.adsl-ull.clienti.tiscali.it. [94.36.187.248]) by smtp.gmail.com with ESMTPSA id u76-v6sm11369194wmd.10.2018.09.30.01.14.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Sep 2018 01:14:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ju26zi0p9jvi/worec/4ivE3njeuHWEgeQBhob/iuys=; b=bwDY7vNPMXNfSzffs5/gO5veA5r0ky+1nKlq5G4LRfkhAm7Ep0VCnCQAxBt8FUQyFl CIKIdqWYkR8gvNOa42lAqgT+1WZRVuUZkxJVep+YHSHfCjaEU3pbNO7Gfju0PdXRO7wh NWKFpx27ArbvFfqav/OkoluaFMjsPZUfmUXmqbvYISZXWdgyvqjsCh58hq08lhl+8HbK 4Ru1rHY7+tVYUbbSqqoziXNpAa7bX53vbqV7b6qmM9jxEjUDQCA3/xYTSTscSLkvtKhJ okBzMNNXPEoiFLbMiP59hxsGNmJRmYhkgFQf94AS7uG+HB2Squo51l1QiVvnn3g1gq22 8R6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=ju26zi0p9jvi/worec/4ivE3njeuHWEgeQBhob/iuys=; b=LMQegbaEMeCZ/AxIF2H40okqK+DtroyfbABy89QnCVUrL/6f+w+C5e1MjbqWWvea08 rhzlXH5nm/bqgL9YiVPxCfb3Zt89ZYMBZ8rjx4UEuwh8kmwWOu2jrQXzUKNOVZ1Y7nu0 w8DgHe5M8N9Dal3KbX/47OhnTROKz0O4mhgQiTTM54B6EkvqZuFIfMQGPx8oniymZ5ds qXEGW38h7IUETv92KI9BHOIQb9Ilyjex1Dv68rUw7u9P/NUXZ3Cl1T8+HsnZ1O4mAUbn k9ZCxCpDl+61GoZNO//salBHkYzTZngfPPMtXjHsA+O3xPyFCgHP7rzt07FaOWRG5Wit ZSGg== X-Gm-Message-State: ABuFfohrn7lFBwBoThTKNuPmT++Tm5jMFb3wo0KqlXTLZvtLapjPqftc it9nXZ8/CMMroKr3cm9devos5pSK X-Google-Smtp-Source: ACcGV62SofgE/DrRDEj3fiqjm73MwCW8FYo388JIkk2Ruu/AM9QwcUBiUPnBZ5BP+pNrB472F0GXwQ== X-Received: by 2002:a1c:d9cc:: with SMTP id q195-v6mr5628648wmg.23.1538295275835; Sun, 30 Sep 2018 01:14:35 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sun, 30 Sep 2018 10:13:12 +0200 Message-Id: <1538295197-23704-75-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1538295197-23704-1-git-send-email-pbonzini@redhat.com> References: <1538295197-23704-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32e Subject: [Qemu-devel] [PULL 74/79] memory: Fix access_with_adjusted_size(small size) on big-endian memory regions 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: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 From: Philippe Mathieu-Daud=C3=A9 Memory regions configured as DEVICE_BIG_ENDIAN (or DEVICE_NATIVE_ENDIAN on big-endian guest) behave incorrectly when the memory access 'size' is small= er than the implementation 'access_size'. In the following code segment from access_with_adjusted_size(): if (memory_region_big_endian(mr)) { for (i =3D 0; i < size; i +=3D access_size) { r |=3D access_fn(mr, addr + i, value, access_size, (size - access_size - i) * 8, access_mask, attrs); } (size - access_size - i) * 8 is the number of bits that will arithmetic shift the current value. Currently we can only 'left' shift a read() access, and 'right' shift a wri= te(). When the access 'size' is smaller than the implementation, we get a negative number of bits to shift. For the read() case, a negative 'left' shift is a 'right' shift :) However since the 'shift' type is unsigned, there is currently no way to right shift. Fix this by changing the access_fn() prototype to handle signed shift value= s, and modify the memory_region_shift_read|write_access() helpers to correctly arithmetic shift the opposite direction when the 'shift' value is negative. Signed-off-by: Philippe Mathieu-Daud=C3=83=C2=A9 Message-Id: <20180927002416.1781-4-f4bug@amsat.org> Signed-off-by: Paolo Bonzini Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- memory.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/memory.c b/memory.c index a4d3fa7..b96aec7 100644 --- a/memory.c +++ b/memory.c @@ -375,18 +375,30 @@ static void adjust_endianness(MemoryRegion *mr, uint6= 4_t *data, unsigned size) } =20 static inline void memory_region_shift_read_access(uint64_t *value, - unsigned shift, + signed shift, uint64_t mask, uint64_t tmp) { - *value |=3D (tmp & mask) << shift; + if (shift >=3D 0) { + *value |=3D (tmp & mask) << shift; + } else { + *value |=3D (tmp & mask) >> -shift; + } } =20 static inline uint64_t memory_region_shift_write_access(uint64_t *value, - unsigned shift, + signed shift, uint64_t mask) { - return (*value >> shift) & mask; + uint64_t tmp; + + if (shift >=3D 0) { + tmp =3D (*value >> shift) & mask; + } else { + tmp =3D (*value << -shift) & mask; + } + + return tmp; } =20 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offs= et) @@ -415,7 +427,7 @@ static MemTxResult memory_region_oldmmio_read_accessor(= MemoryRegion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs) { @@ -441,7 +453,7 @@ static MemTxResult memory_region_read_accessor(MemoryR= egion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs) { @@ -467,7 +479,7 @@ static MemTxResult memory_region_read_with_attrs_access= or(MemoryRegion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs) { @@ -494,7 +506,7 @@ static MemTxResult memory_region_oldmmio_write_accessor= (MemoryRegion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs) { @@ -519,7 +531,7 @@ static MemTxResult memory_region_write_accessor(MemoryR= egion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs) { @@ -544,7 +556,7 @@ static MemTxResult memory_region_write_with_attrs_acces= sor(MemoryRegion *mr, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attr= s) { @@ -574,7 +586,7 @@ static MemTxResult access_with_adjusted_size(hwaddr add= r, hwaddr addr, uint64_t *value, unsigned size, - unsigned shift, + signed shift, uint64_t mask, MemTxAttrs attrs), MemoryRegion *mr, --=20 1.8.3.1