From nobody Sat May 18 20:15:25 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1615574461; cv=none; d=zohomail.com; s=zohoarc; b=Kww4rVr1sHWwuV9I1A70YY212s4iu7347jA2B/4fskayPoyGByBU7+sfHNSOqmbFmQeFlPvB9nBMgn0cdV2/dgqV1gUoE7ojNQlqw0ZiBpKZ9thpUjMEzLcl0g7lWEG+MwdUQ+vR9h3A4S2iwAUPKveH9FSavSQy10NaY9jmv14= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615574461; h=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=FdVDED8kKnbt0t9rcpUoiyt06STJfiA/Zd4Bwc57Y8s=; b=Nv+JAXNU42QLahtetLVha+A8K/+rFyi5xJLzjbSUw+5H+n+7RNvy3A9FdY2ocJGICg3y98BvFS/AlRmOMhszuHxKunjg0s95OGA6yKqAyj1xOSYyQ1yCrIhHCx38ZsJj7tNBcxtN8T4tF5QHlnSfjDIvmkIxS8sUtCBuBB+PmrU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass 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 1615574461298153.71404496259743; Fri, 12 Mar 2021 10:41:01 -0800 (PST) Received: from localhost ([::1]:47050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKmiZ-0002km-VR for importer@patchew.org; Fri, 12 Mar 2021 13:40:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKlbg-0005TG-Dj for qemu-devel@nongnu.org; Fri, 12 Mar 2021 12:29:48 -0500 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]:44708) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lKlbb-0002g0-GV for qemu-devel@nongnu.org; Fri, 12 Mar 2021 12:29:48 -0500 Received: by mail-wm1-x331.google.com with SMTP id j4-20020a05600c4104b029010c62bc1e20so15636902wmi.3 for ; Fri, 12 Mar 2021 09:29:43 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id m132sm2679001wmf.45.2021.03.12.09.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Mar 2021 09:29:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FdVDED8kKnbt0t9rcpUoiyt06STJfiA/Zd4Bwc57Y8s=; b=hFY6aiFV596TJ8mRkE54kxP7Hq8Bne2Gl5B/yE5QkwiYbMPtyTSHGDBSWRDdMSDhk/ IK0YleMIff7FjwcHCa5bVHX97jK7kIUcUtTQ8sbdoUA+1tGNc91wYqcmTfFSFcQCq4e3 +ir0U4uqctisXEKMA8YjOzVMmegfbLY5rXmERkvVS+qtfS+qE/oIWMpaCiI790t5vseI rliQsMmSWthDenmoaEpMIYEH16PYnw52enyAdoEuhCkVoI+8MCgq8khHKZc7LaN403lz kQyOMJwNm2rXJWUiuP8QAT0W0Ob2uuc52k1rgjEdGthezZEmIy4KcQp52QmErur7yuD/ r75A== 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=FdVDED8kKnbt0t9rcpUoiyt06STJfiA/Zd4Bwc57Y8s=; b=fXwn/bLtawIswQ1lMaRBZZ0VNu5pY1cBH7E1wJmLbh8flBt1GUY2J0UWUhtqdH0dXy 5h615/id72ZDlZc3nKSJr+VtvPwNarvX5n3b3UTtJuvuu4pgwY5gFCrWO5VpOZxLcR8K jlHvkcLm6PfZwvlQX8AXCdJpNYpw8RefkmG0aYxzAkVin/nwrDjAD+64Vc2GQMk9PLyT rDLJAXK/qicvtOaNHRXkk9Pws2T57/HyijOAorSudt01idH6+gENodhmdiXuBo9QKFKU ine/GZlkSbd/+AWFkrKZu7GmVR1Fxh1UnYg62neyVoUliRmxiVQUbjDLRoPCADZLBk+O nhgw== X-Gm-Message-State: AOAM533bzoe7Tj9orWVa0x+1FtU1e146tlqIca6SLHqjXB+faeiEC4lX C6hhuqgLlHnhuQ2sGdz2Ksp//Q== X-Google-Smtp-Source: ABdhPJzQ89JN2fTIh/ppkmnbRidq7/Xv7MHtTF7ee2D+MnttGNNUzrgtMOEP60iy2c7Ws/R5taSxmA== X-Received: by 2002:a1c:3c02:: with SMTP id j2mr14101898wma.92.1615570182187; Fri, 12 Mar 2021 09:29:42 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH for-6.0 1/2] memory: Add offset_in_region to flatview_cb arguments Date: Fri, 12 Mar 2021 17:29:38 +0000 Message-Id: <20210312172939.695-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210312172939.695-1-peter.maydell@linaro.org> References: <20210312172939.695-1-peter.maydell@linaro.org> MIME-Version: 1.0 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::331; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x331.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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: Paolo Bonzini , Kumar Gala Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) Content-Type: text/plain; charset="utf-8" The function flatview_for_each_range() calls a callback for each range in a FlatView. Currently the callback gets the start and length of the range and the MemoryRegion involved, but not the offset within the MemoryRegion. Add this to the callback's arguments; we're going to want it for a new use in the next commit. While we're editing the flatview_cb typedef, add names for the arguments that were missing names. Signed-off-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- include/exec/memory.h | 4 +++- softmmu/memory.c | 3 ++- tests/qtest/fuzz/generic_fuzz.c | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 54ccf1a5f09..0c3ddf2bb9a 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -778,7 +778,9 @@ static inline FlatView *address_space_to_flatview(Addre= ssSpace *as) =20 typedef int (*flatview_cb)(Int128 start, Int128 len, - const MemoryRegion*, void*); + const MemoryRegion *mr, + hwaddr offset_in_region, + void *opaque); =20 void flatview_for_each_range(FlatView *fv, flatview_cb cb , void *opaque); =20 diff --git a/softmmu/memory.c b/softmmu/memory.c index 9db47b7db6b..3cc78862c79 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -671,7 +671,8 @@ void flatview_for_each_range(FlatView *fv, flatview_cb = cb , void *opaque) assert(cb); =20 FOR_EACH_FLAT_RANGE(fr, fv) { - if (cb(fr->addr.start, fr->addr.size, fr->mr, opaque)) + if (cb(fr->addr.start, fr->addr.size, fr->mr, + fr->offset_in_region, opaque)) break; } } diff --git a/tests/qtest/fuzz/generic_fuzz.c b/tests/qtest/fuzz/generic_fuz= z.c index ee8c17a04c4..c10a19316f5 100644 --- a/tests/qtest/fuzz/generic_fuzz.c +++ b/tests/qtest/fuzz/generic_fuzz.c @@ -96,7 +96,9 @@ struct get_io_cb_info { }; =20 static int get_io_address_cb(Int128 start, Int128 size, - const MemoryRegion *mr, void *opaque) { + const MemoryRegion *mr, + hwaddr offset_in_region, + void *opaque) { struct get_io_cb_info *info =3D opaque; if (g_hash_table_lookup(fuzzable_memoryregions, mr)) { if (info->index =3D=3D 0) { --=20 2.20.1 From nobody Sat May 18 20:15:26 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1615574783; cv=none; d=zohomail.com; s=zohoarc; b=Wtb/3RDgXToJ+FqKon12xG4rFVA/WeCd2DK9n368xKcjVU4AvWIL+iQOqoBfxKoLbpM8NiXK4JZ57ViX5vUDDIBj75E/pDjUwOpmpiScjohF87yHQIRTP6iUAW9GjNlMYZ/Z9llF4I09LLzNwLJEpb9U9BOE486GeELHxwuII+k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615574783; h=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=8OpPe7RRzgUsca/HiL56Quy2lcOKFBmE9qvI9fkuUVI=; b=Pem80HisY0wNmFsVqIW6J9RLnSG06R0EfamHb+eU52HPnSLbY4m8PpuY+mpj28V+I/Jm1Ouzcp/aWvFz2E1TkEOoCbAXV+n72k9FxNeROPZ7XIAuB7uk4VpSBHn0EEJEQBWiR49lK+Zf4p3AX+XbTKWj9zXdUoUQka6ocMSdaOo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass 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 1615574783025493.02577899590824; Fri, 12 Mar 2021 10:46:23 -0800 (PST) Received: from localhost ([::1]:57876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKmnl-0000Q7-VK for importer@patchew.org; Fri, 12 Mar 2021 13:46:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKlbi-0005XA-7i for qemu-devel@nongnu.org; Fri, 12 Mar 2021 12:29:50 -0500 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:45621) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lKlbe-0002gE-W1 for qemu-devel@nongnu.org; Fri, 12 Mar 2021 12:29:49 -0500 Received: by mail-wm1-x343.google.com with SMTP id r10-20020a05600c35cab029010c946c95easo15613780wmq.4 for ; Fri, 12 Mar 2021 09:29:44 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id m132sm2679001wmf.45.2021.03.12.09.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Mar 2021 09:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8OpPe7RRzgUsca/HiL56Quy2lcOKFBmE9qvI9fkuUVI=; b=E9VewL2vDmzNAz01er9cmXvHW1gGrkxCzHNjdJEI4MiGISq5Ju9qJ7mNxWBWUHsdmD CQB7yUbdwHgbPsBxp1cZpBSf2PC8YGW4n5z49WlvtmppYrwGWWU1eQtf4h3xZCypdVO+ B9QFyPnO7UvkUWCHvr5P4mj6GHWUnFN8PCFj5Gpu6+2ZBz66Ev6C17tlh4FikVAoYlUt WeFhllOuRgITejI7umnErc9X06neafOI5S6LjKmVvkxSEu3Y0AAT6G6QeU7oWS36BSmh Yj9/TeWR1HnwPyCYi2D2XGFQSpW13aOZ/MyRmUjmJ2fmYWY6rRRtU5omNWzS61Y+zL/t z8sA== 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=8OpPe7RRzgUsca/HiL56Quy2lcOKFBmE9qvI9fkuUVI=; b=ZX9JrlVAa8M4WakEzIN/VfxPJojUNCbccProETwO7xNT1xrY8XxUHuA/jZ1/YwjtkE 1+kcw7IbZ1XCny/4+OnV3lf2t62oUaZ2vVCwSHWOzPexMo5kJHmD6kzygXixsjp2lv8j LTVdO9K67xXfq9o4KKa7CccyIQoYoCPsnZzLJvhoPrapPknpXO9TtwUPWh/QL0QZnRd9 XjAJ8akL+s20SE/MV6VY8ExwS3XTYROiXqI/jFHfDHKRWQiad8b7UHYPlnd26zPTkiwJ IXGCXTV2Ja4OdMdXPep1W3CY+sDmlk7+OGx3rEGcOPcfqdSNhajj9IrVjvCfxS3lgFuC RsoQ== X-Gm-Message-State: AOAM531+60hMszyiV5SzdhOfmKSINIh2LD9Lh/GBmZ23uC9iOzonMKoX m1MZGM3qT/QcqndbwCqfC3PuUpgpO8SKLtvZ X-Google-Smtp-Source: ABdhPJzSVqOfl1D6d4JZNpS14oyKgJOFDG/jQZLtUpH78riNEPZGDj0neOWs3RHCX36ta+oezDlQug== X-Received: by 2002:a05:600c:4f03:: with SMTP id l3mr14122951wmq.149.1615570183083; Fri, 12 Mar 2021 09:29:43 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH for-6.0 2/2] target/arm: Make M-profile VTOR loads on reset handle memory aliasing Date: Fri, 12 Mar 2021 17:29:39 +0000 Message-Id: <20210312172939.695-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210312172939.695-1-peter.maydell@linaro.org> References: <20210312172939.695-1-peter.maydell@linaro.org> MIME-Version: 1.0 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::343; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x343.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: Paolo Bonzini , Kumar Gala Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) Content-Type: text/plain; charset="utf-8" For Arm M-profile CPUs, on reset the CPU must load its initial PC and SP from a vector table in guest memory. Because we can't guarantee reset ordering, we have to handle the possibility that the ROM blob loader's reset function has not yet run when the CPU resets, in which case the data in an ELF file specified by the user won't be in guest memory to be read yet. We work around the reset ordering problem by checking whether the ROM blob loader has any data for the address where the vector table is, using rom_ptr(). Unfortunately this does not handle the possibility of memory aliasing. For many M-profile boards, memory can be accessed via multiple possible physical addresses; if the board has the vector table at address X but the user's ELF file loads data via a different address Y which is an alias to the same underlying guest RAM then rom_ptr() will not find it. Handle the possibility of aliasing by iterating through the whole FlatView of the CPU's address space checking for other mappings of the MemoryRegion corresponding to the location of the vector table. If we find any aliases we use rom_ptr() to see if the ROM blob loader has any data there. Signed-off-by: Peter Maydell --- target/arm/cpu.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/target/arm/cpu.c b/target/arm/cpu.c index ae04884408c..aac78ae6623 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -161,6 +161,72 @@ static void cp_reg_check_reset(gpointer key, gpointer = value, gpointer opaque) assert(oldvalue =3D=3D newvalue); } =20 +#ifndef CONFIG_USER_ONLY +typedef struct FindRomCBData { + size_t size; /* Amount of data we want from ROM, in bytes */ + MemoryRegion *mr; /* MR at the unaliased guest addr */ + hwaddr xlat; /* Offset of addr within mr */ + uint8_t *rom; /* Output: rom data pointer, if found */ +} FindRomCBData; + +static int find_rom_cb(Int128 start, Int128 len, const MemoryRegion *mr, + hwaddr offset_in_region, void *opaque) +{ + FindRomCBData *cbdata =3D opaque; + hwaddr alias_addr; + + if (mr !=3D cbdata->mr) { + return 0; + } + + alias_addr =3D int128_get64(start) + cbdata->xlat - offset_in_region; + cbdata->rom =3D rom_ptr(alias_addr, cbdata->size); + if (!cbdata->rom) { + return 0; + } + /* Found a match, stop iterating */ + return 1; +} + +static uint8_t *find_rom_for_addr(AddressSpace *as, hwaddr addr, size_t si= ze) +{ + /* + * Find any ROM data for the given guest address range. If there + * is a ROM blob then return a pointer to the host memory + * corresponding to 'addr'; otherwise return NULL. + * + * This is like rom_ptr(), except that it handles possible aliases + * within the CPU's address space, so that we still find a ROM + * blob even if it was loaded to an address that aliases addr + * rather than to addr itself. + */ + FlatView *fv; + uint8_t *rom; + hwaddr len_unused; + FindRomCBData cbdata =3D {}; + + /* Easy case: there's data at the actual address */ + rom =3D rom_ptr(addr, size); + if (rom) { + return rom; + } + + RCU_READ_LOCK_GUARD(); + + fv =3D address_space_to_flatview(as); + cbdata.mr =3D flatview_translate(fv, addr, &cbdata.xlat, &len_unused, + false, MEMTXATTRS_UNSPECIFIED); + if (!cbdata.mr) { + /* Nothing at this address, so there can't be any aliasing */ + return NULL; + } + + cbdata.size =3D size; + flatview_for_each_range(fv, find_rom_cb, &cbdata); + return cbdata.rom; +} +#endif + static void arm_cpu_reset(DeviceState *dev) { CPUState *s =3D CPU(dev); @@ -331,7 +397,7 @@ static void arm_cpu_reset(DeviceState *dev) =20 /* Load the initial SP and PC from offset 0 and 4 in the vector ta= ble */ vecbase =3D env->v7m.vecbase[env->v7m.secure]; - rom =3D rom_ptr(vecbase, 8); + rom =3D find_rom_for_addr(s->as, vecbase, 8); if (rom) { /* Address zero is covered by ROM which hasn't yet been * copied into physical memory. --=20 2.20.1