From nobody Tue Jul 1 09:23:58 2025 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621509281; cv=none; d=zohomail.com; s=zohoarc; b=chRYuEcxUJKL/wBNirb9RDazohSZvys25MG4p3i6n8MyuL+J3hpmj2+4/mBQc5P+26NbKyjGHDZz1/bb8pPWLD1h+90xQdhJw/ago7W+qRvC7D0fEG5gtknIN5LPf+ja3q8B3bx3Fiw6aytNhdeTGy57BWdrMONIde3/eszaeMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621509281; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QEjpdg/IZ00DpLUx/mVY0KVvckbtUYRfwfr8N0WJ7zo=; b=DhpDJLQWuKEbHNzfDaxzS3eEGG51dsVP43MzjgIyViBZOeIQrqwL5J53zgv5DCWkf3of+djgItL4IWLMYqZ1rFg7GVVLNVKyducHl4PBMRSe37I6WnWyprK0lB8+rel45FoQmh/Rsh211B69+qHU0FPPZ8TyudlQ0mUNfB6RT2s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621509281830116.50623741523225; Thu, 20 May 2021 04:14:41 -0700 (PDT) Received: from localhost ([::1]:46428 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ljgdU-0007jG-SK for importer2@patchew.org; Thu, 20 May 2021 07:14:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44682) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljgZ0-0000RJ-Fc for qemu-devel@nongnu.org; Thu, 20 May 2021 07:10:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:42150) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljgYy-0004eG-Kr for qemu-devel@nongnu.org; Thu, 20 May 2021 07:10:02 -0400 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-208-NCqmkrCSO-qGZLZh29ykFQ-1; Thu, 20 May 2021 07:09:58 -0400 Received: by mail-wm1-f69.google.com with SMTP id 12-20020a1c010c0000b0290176491efde9so2188819wmb.4 for ; Thu, 20 May 2021 04:09:58 -0700 (PDT) Received: from x1w.redhat.com (31.red-83-51-215.dynamicip.rima-tde.net. [83.51.215.31]) by smtp.gmail.com with ESMTPSA id a19sm2290569wmb.40.2021.05.20.04.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 04:09:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621509000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QEjpdg/IZ00DpLUx/mVY0KVvckbtUYRfwfr8N0WJ7zo=; b=WLQ1INpCdihdrOX0tKNGdN6l+nYTx84B8X81I3JP+ZHGEMllCIOLGyJLUFz/bC3sQgPN6u LBdA804sVWcTcuFPCX+a0aOMc4c7UrxeBmsJvKGou7gTzhgiLRO7HEl2FMjC5vN5cl2tR/ wvmF9hSh+fJ50v4tSduQgwhpIW1qmEE= X-MC-Unique: NCqmkrCSO-qGZLZh29ykFQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QEjpdg/IZ00DpLUx/mVY0KVvckbtUYRfwfr8N0WJ7zo=; b=h2NsXl8RBJZzUKAFkItQRFYQXRj8EEwtcjk/5PWVvHDmm9NhOwpSEmgoa++XHb87+N magB/iPb+D1/n3OuB/9reoRw77DwUVymwcGwWihs0Aak49mmeyuJz0HkqRFMQ9eWPOzn qD6WTowTCgS/cEI4yH2U1ZZmGbShYAIXWFYhGRWIRKfawgBrj89JWO3L0BxA1avdGU0+ OQlO55tP0lGIS76xzMUDh3JISUOFD34VnyU1e2UCBzQSo0Hj1RaoW5XdXHovqYFUcPqR zz4WKxIOOr0VOyjlcHsOkglzPO17TpiaUct0siCl4kAZ+zRWTx6RCUOjHet4iOyQC4i2 HiFw== X-Gm-Message-State: AOAM531+e9nsmkCrvM4aG3Nma8/6WSU2dAJ0+ysxtDPXrD2vz8zN+5of 7q2zaNaGr6PKwsU0i+1ArVI1VyMSWefMX4MyjauxnHPV+n71PhMM6FipfcWI2kZOhMnZrIuPRx4 ine3+54mJVDeM4iS1FtLV9SPgAcadH9D0c7m86LwOwnHo6zTKJvsHnJC16bA2D2TL X-Received: by 2002:adf:d4d1:: with SMTP id w17mr3820168wrk.413.1621508997263; Thu, 20 May 2021 04:09:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMKWz7tO4wpoHu+uid8luC73vTDixiYStFcHSGB6n9Cm0IESN0cTtD5YhN/4V8wQe7Onypow== X-Received: by 2002:adf:d4d1:: with SMTP id w17mr3820143wrk.413.1621508997051; Thu, 20 May 2021 04:09:57 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [RFC PATCH 08/12] exec/memory_ldst_cached: Check address alignment if requested Date: Thu, 20 May 2021 13:09:15 +0200 Message-Id: <20210520110919.2483190-9-philmd@redhat.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20210520110919.2483190-1-philmd@redhat.com> References: <20210520110919.2483190-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.39, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Michael S. Tsirkin" , Richard Henderson , Peter Xu , Bibo Mao , Stefan Hajnoczi , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) If the caller requires strict alignment, check the address satisfies it before doing the transaction. Otherwise return a MEMTX_UNALIGNED_ERROR. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- include/exec/memory_ldst_cached.h.inc | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/exec/memory_ldst_cached.h.inc b/include/exec/memory_ld= st_cached.h.inc index 515beb48f47..311a9759a22 100644 --- a/include/exec/memory_ldst_cached.h.inc +++ b/include/exec/memory_ldst_cached.h.inc @@ -31,6 +31,13 @@ static inline uint16_t ADDRESS_SPACE_LD_CACHED(uw)(Memor= yRegionCache *cache, assert(addr < cache->len && 2 <=3D cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 2, cache->mrs.mr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint16_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return (uint16_t)-1; /* XXX */ + } if (result) { *result =3D MEMTX_OK; } @@ -47,6 +54,13 @@ static inline uint32_t ADDRESS_SPACE_LD_CACHED(l)(Memory= RegionCache *cache, assert(addr < cache->len && 4 <=3D cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 4, cache->mrs.mr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint32_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return (uint32_t)-1; /* XXX */ + } if (result) { *result =3D MEMTX_OK; } @@ -63,6 +77,13 @@ static inline uint64_t ADDRESS_SPACE_LD_CACHED(q)(Memory= RegionCache *cache, assert(addr < cache->len && 8 <=3D cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 8, cache->mrs.mr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint64_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return (uint64_t)-1; /* XXX */ + } if (result) { *result =3D MEMTX_OK; } @@ -89,6 +110,13 @@ static inline void ADDRESS_SPACE_ST_CACHED(w)(MemoryReg= ionCache *cache, { assert(addr < cache->len && 2 <=3D cache->len - addr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint16_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return; + } if (result) { *result =3D MEMTX_OK; } @@ -104,6 +132,13 @@ static inline void ADDRESS_SPACE_ST_CACHED(l)(MemoryRe= gionCache *cache, { assert(addr < cache->len && 4 <=3D cache->len - addr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint32_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return; + } if (result) { *result =3D MEMTX_OK; } @@ -119,6 +154,13 @@ static inline void ADDRESS_SPACE_ST_CACHED(q)(MemoryRe= gionCache *cache, { assert(addr < cache->len && 8 <=3D cache->len - addr); if (likely(cache->ptr)) { + if (attrs.aligned && unlikely(!QEMU_PTR_IS_ALIGNED(cache->ptr, + sizeof(uint64_t= )))) { + if (result) { + *result =3D MEMTX_UNALIGNED_ERROR; + } + return; + } if (result) { *result =3D MEMTX_OK; } --=20 2.26.3