From nobody Fri May 10 21:11:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.221.43 as permitted sender) client-ip=209.85.221.43; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wr1-f43.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1618725433; cv=none; d=zohomail.com; s=zohoarc; b=eRxNGmSxWEE8i4MbsBoTho+yZG4Zmc29G6CBUYMa6s3Zgxm0m9AB76sopLtD79UjEP4MgeK3r1H9ZsvdfqeRAwRjqT6zfjgFX6PiIbRNLWZQRLWq2BrBWM3vR9c0OWOrQ+UMYIg34g+SG6MaJVWcrfJuohZcEEqWhIbpaVpRZDY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618725433; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Sender:Subject:To; bh=EQnokyzPBUlpkcCtUGe9E3DY+cuW/2ENBpzEnBucWls=; b=V84T3gzgGLogz12+XuZePCfGL56MVAMisEcvHq+OX5HWBMEn4URQqgXEF6xg4ROD3944QiCu+NJrgcNfe3VsVLQZbQMueLzMDLfrlXGfuzOLu84LT+kdL95JLRVdfjh541EKrjrQHNqezIpXF8Qrp15O1CfPRo3FS4gzELrM2+A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by mx.zohomail.com with SMTPS id 1618725432974114.63773946328286; Sat, 17 Apr 2021 22:57:12 -0700 (PDT) Received: by mail-wr1-f43.google.com with SMTP id x7so30601678wrw.10 for ; Sat, 17 Apr 2021 22:57:12 -0700 (PDT) Return-Path: Return-Path: Received: from localhost.localdomain (39.red-81-40-121.staticip.rima-tde.net. [81.40.121.39]) by smtp.gmail.com with ESMTPSA id u2sm15058744wmc.22.2021.04.17.22.57.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Apr 2021 22:57:10 -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:mime-version :content-transfer-encoding; bh=EQnokyzPBUlpkcCtUGe9E3DY+cuW/2ENBpzEnBucWls=; b=CyiBziCLsWmWzdYC3QkD+h5za0C7dtBR501NODUruEZSZP2nmDqydKftLY5LGg272w vdyiNWMuUi91UFzEccUusE3Oz5cD0PX3kiUVjZyd4sdkG392H8MUTiCQXHhBSeco5f5X 2+p0slpcsk2DLuxODrrUytdGm5PVCUC9nJv4bbuCzZvbMe/KoTTw1KzEhGDIP226rHmQ hOmqPHf2UNn7r4eL6VuW6026LosoLIryeSSKouO8u/8N1vcSJMLm2+dU/TcfxP0f6m6Q isqus1kuo2tlkA7vt3WA1kJLOlCLX3tv24u9KwN0JLmdPc38wTNqJ/MHvAp7w/CG9YXq EaRQ== 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 :mime-version:content-transfer-encoding; bh=EQnokyzPBUlpkcCtUGe9E3DY+cuW/2ENBpzEnBucWls=; b=loECjjI/Odqkfp0EJoChr3M3gapW9BEgAFVD9i+2zel2UFiHstq0SfjXKL20+72EhW XqfiYsWRraVYGzXBKqSwUjdspA7RyCsrnTty8O6WMSydaATFQ9P3JzpMRrXppktu6kxn ptCsJ696i+IQJ8M7iOQswJRd4m2GNXzvXilw7H8Wy3j/5qKny2buPbDU07v4KcUtir3a znrSOcFOq7mOrGruBZR0djcDkNINW80PLpIMI+IQFS+fc1/BsDn/7byUjki4/VH9K/hZ RJPjBcLsWgR59QC0qJ2bPTzMd6yQs3IAJdYYCbTd3GP0my2HaDYvVKOGE+g0zICxHdDw kN/Q== X-Gm-Message-State: AOAM5314/+MxLAI3/feRXNHHbbcT60h7i4WHAMsMGY+AvzMBizbTsAhc gy5ouFk/vQRBQ9tYxooJe70= X-Google-Smtp-Source: ABdhPJwaOF+9AWnNtyUXab99V0KwNOmwNxUZezjlrOIgpHyFEJ9HkpDFpVR++2T9efyyfSC+fLXR0Q== X-Received: by 2002:adf:ea09:: with SMTP id q9mr7610564wrm.235.1618725431164; Sat, 17 Apr 2021 22:57:11 -0700 (PDT) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Richard Henderson , Peter Xu , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v4] memory: Directly dispatch alias accesses on origin memory region Date: Sun, 18 Apr 2021 07:57:08 +0200 Message-Id: <20210418055708.820980-1-f4bug@amsat.org> X-Mailer: git-send-email 2.26.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) Since commit 2cdfcf272d ("memory: assign MemoryRegionOps to all regions"), all newly created regions are assigned with unassigned_mem_ops (which might be then overwritten). When using aliased container regions, and there is no region mapped at address 0 in the container, the memory_region_dispatch_read() and memory_region_dispatch_write() calls incorrectly return the container unassigned_mem_ops, because the alias offset is not used. Consider the following setup: +--------------------+ < - - - - - - - - - - - + | Container | mr | (unassigned_mem) | | | | | | | | | alias_offset + + <- - - - - - +----------+---------+ | +----------------+ | | | | | MemoryRegion0 | | | | | +----------------+ | | Alias | addr1 | | MemoryRegion1 | | <~ ~ ~ ~ ~ | | <~~~~~~ | +----------------+ | | | | | +--------------------+ | | | | | | | | | +----------------+ | | | MemoryRegionX | | | +----------------+ | | | MemoryRegionY | | | +----------------+ | | | MemoryRegionZ | | | +----------------+ | +--------------------+ The memory_region_init_alias() flow is: memory_region_init_alias() -> memory_region_init() -> object_initialize(TYPE_MEMORY_REGION) -> memory_region_initfn() -> mr->ops =3D &unassigned_mem_ops; Later when accessing offset=3Daddr1 via the alias, we expect to hit MemoryRegion1. The memory_region_dispatch_read() flow is: memory_region_dispatch_read(addr1) -> memory_region_access_valid(mr) <- addr1 offset is ignored -> mr->ops->valid.accepts() -> unassigned_mem_accepts() <- false <- false <- MEMTX_DECODE_ERROR The caller gets a MEMTX_DECODE_ERROR while the access is OK. Fix by dispatching aliases recursively, accessing its origin region after adding the alias offset. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Peter Xu --- v4: - added ASCII schema v3: - reworded, mentioning the "alias to container" case - use recursive call instead of while(), because easier when debugging therefore reset Richard R-b tag. v2: - use while() --- softmmu/memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/softmmu/memory.c b/softmmu/memory.c index d4493ef9e43..b899ca6a6b7 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1442,6 +1442,11 @@ MemTxResult memory_region_dispatch_read(MemoryRegion= *mr, unsigned size =3D memop_size(op); MemTxResult r; =20 + if (mr->alias) { + return memory_region_dispatch_read(mr->alias, + mr->alias_offset + addr, + pval, op, attrs); + } if (!memory_region_access_valid(mr, addr, size, false, attrs)) { *pval =3D unassigned_mem_read(mr, addr, size); return MEMTX_DECODE_ERROR; @@ -1486,6 +1491,11 @@ MemTxResult memory_region_dispatch_write(MemoryRegio= n *mr, { unsigned size =3D memop_size(op); =20 + if (mr->alias) { + return memory_region_dispatch_write(mr->alias, + mr->alias_offset + addr, + data, op, attrs); + } if (!memory_region_access_valid(mr, addr, size, true, attrs)) { unassigned_mem_write(mr, addr, data, size); return MEMTX_DECODE_ERROR; --=20 2.26.3