From nobody Fri Dec 19 07:15:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621509000; cv=none; d=zohomail.com; s=zohoarc; b=mGLAei7YJUW/aIHrtTslYdbUvKU3nSAjB8Prf3QezDiTnikrdjESAoZkmC7WCZqOrG95TzvZ0P0HHehZSDiUzf0vzVQkQwsIg3hfKuoY3F6s38LmzabhmevUQCxsOJOk9u2RXwA2HaXbRZyAgoc+tGeogoS9ksDzD7OqsDhrBjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621509000; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=QEjpdg/IZ00DpLUx/mVY0KVvckbtUYRfwfr8N0WJ7zo=; b=BkMuSY8X+bg0/FmGiMpzIZAftI9COD+wpxBebwzTNQ83aL+RvyW6nwj5FrRgEFCgsHS7vz/N09y6UAIcxTJwS/5FI94xj8UaF4OpNkca6TWUu5Hcb5AYwXHWmM6hzrX4HOELN1MjnSscxrT6N/vvCZSWeaA13QirkEB+AiU/tKs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1621509000852869.1324845597298; Thu, 20 May 2021 04:10:00 -0700 (PDT) Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-206-eucBqCreOuC1Gfj9y55Qvg-1; Thu, 20 May 2021 07:09:58 -0400 Received: by mail-wr1-f72.google.com with SMTP id p11-20020adfc38b0000b0290111f48b8adfso5026390wrf.7 for ; Thu, 20 May 2021 04:09:58 -0700 (PDT) Return-Path: Return-Path: 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=1621508999; 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=IQD6WoO0dyRZp8zYT4lRYt9sZ54KwhwB6Lln0BBWdj7u8Q3Rn2A3PkpRp/UEk8uHKXNfOR Yqtnn3PcnyuO3rxtCIFhG6x3PPgK9oaWnD7zqIg009h3B62lRJf6ySVQOfz8zh2evM2QaY fSfqxwybfrfYio2kA8KD6jPPpgkGbU4= X-MC-Unique: eucBqCreOuC1Gfj9y55Qvg-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=TRD7ZcxR21OmLHzWWVdGRV382BgF8QzVPivQiA1Awqb/bRi9T25R/Wj2VW34ENCE/8 umHEBxVBpP6EcCEX7l/0vlLceGgIvEw0UVHKReZwu82XEe8WlHMOY+YqwDBwnQ6iPV2h f0CUkXbsnh/WsKcMQzLlXslI9xN9xtsxiz6tObN4QeehXIVknPLG/dZaomsH8fpKj0eR 03H9k/bjgc1Uto9J986kW+mcKLxHE6u96cvpQ72OeDDdWDo7C0A6hdrH5VNpLkl4sFyl 5MV70VIbdQK20sOv++SWA+Zw6n0Hl21AyiziQV8s/M4EcYb+1c3kvcPSU09ImxvUWk3f hDCg== X-Gm-Message-State: AOAM5317uw2kblZo2Lpf0fcce4YHRYsBGXcdO1PRXI9nm9rk1RWLx4Sn 2MddDVBSfFVhJixaQJaILa1V3hrAm1NH4KTC3pQ56JmUqOFsMI5l9Wt6GsCMh6L1EImuwi9+Tnp VNTPJAqODQFXXwA== X-Received: by 2002:adf:d4d1:: with SMTP id w17mr3820159wrk.413.1621508997219; 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 Cc: "Michael S. Tsirkin" , Stefan Hajnoczi , Peter Maydell , Peter Xu , Paolo Bonzini , Richard Henderson , Bibo Mao , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= 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 X-ZohoMail-DKIM: pass (identity @redhat.com) 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