From nobody Tue Feb 10 01:30:46 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538008001634153.5427244145967; Wed, 26 Sep 2018 17:26:41 -0700 (PDT) Received: from localhost ([::1]:33139 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5K8U-0005uT-N5 for importer@patchew.org; Wed, 26 Sep 2018 20:26:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57139) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5K6g-0004xF-Ur for qemu-devel@nongnu.org; Wed, 26 Sep 2018 20:24:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5K6f-00011B-Q6 for qemu-devel@nongnu.org; Wed, 26 Sep 2018 20:24:38 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:38453) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g5K6f-00010t-GM for qemu-devel@nongnu.org; Wed, 26 Sep 2018 20:24:37 -0400 Received: by mail-wr1-x444.google.com with SMTP id j8-v6so677728wrw.5 for ; Wed, 26 Sep 2018 17:24:37 -0700 (PDT) Received: from x1.local (106.red-88-7-90.staticip.rima-tde.net. [88.7.90.106]) by smtp.gmail.com with ESMTPSA id x14-v6sm559668wmc.36.2018.09.26.17.24.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Sep 2018 17:24: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=JQlWVQRgXbBmK4B504HDYOvr3PiUiayxnNsOP5+/zWo=; b=kI7pPbC0ccUVIwjGSs+5b+E17kQENLTh9ltn3LZ2Ha3y72xmk6RSZHIMbFvO+mJk/4 SlnczZ9WsiMEwat0+FGrsueieaqujUwFmSbkm/vS1tHcR/KfZmRLjbo/f4Lk2XlgfAX8 mO+bhcLXwUS0NSyvXW6X/aIYB5G2dqKxCo/3WO/eIbhvt4v8ZJPz5ozh+aI3umi6Vnci oGCezh5xqleXEXkkBy7MtUtK+3QP8IE5VDDZmIpvOHQI3gXNeSik5YdKcX2zI2mzHlUb CAEtgigQQKnsgb48CtfrQbAQ4UcS9jm8gCXcGyg+khkUULIDEidzndSyIcSYfIY15ZmT 3dTw== 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=JQlWVQRgXbBmK4B504HDYOvr3PiUiayxnNsOP5+/zWo=; b=LDrmbQ6GIchEUkAvNVmOhEVSvrjMhfwR87c2mUgXuqQPge0Dy2bEuEWAvWgj9DM6SM QKzJtniNcPaip8Ku3VFtGA79ivVWyZIFu2OAF6/ha9enPITEltdQWxCA5phnPWFwEY4G BW2EMEB73MSIgvA4utzzVJM+XvI6wa6+4MZFytNI/O2wyC7IWA+YXLEHrEDqc13J29rH PjKcVTenJ15CA2SEnPy+Qmf85apn5XHOepzt0tCXIyem2sNP6r333dUFJpI/2gMX+kRK yMQ0mzOQbXidOY0qB92szNIs5d7b4kDvqcSd11zNcgcE7Lo5KL+szSuY0WkzWcJ9sfsG klvQ== X-Gm-Message-State: ABuFfojc3pteHOHERAdnYrdCAjNIoxScjdJ+vZ4c1J8BHf2PrvjxE+8l GfXmMkCzEOjggoqXpm5Im7o= X-Google-Smtp-Source: ACcGV639409mStFr0CQDWuHj6vhLpXYvAH5NWQ3yhZo8v+s/Q0L+xE1ETYlMUFBwm9vTfZvfg+tmyg== X-Received: by 2002:adf:8206:: with SMTP id 6-v6mr6749302wrb.160.1538007876486; Wed, 26 Sep 2018 17:24:36 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Paolo Bonzini , Peter Maydell Date: Thu, 27 Sep 2018 02:24:16 +0200 Message-Id: <20180927002416.1781-4-f4bug@amsat.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180927002416.1781-1-f4bug@amsat.org> References: <20180927002416.1781-1-f4bug@amsat.org> 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::444 Subject: [Qemu-devel] [PATCH 3/3] 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: Andrew Jeffery , KONRAD Frederic , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 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=A9 --- memory.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/memory.c b/memory.c index 48edf7dc23..a9f0fdc440 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 2.19.0