From patchwork Wed May 8 17:55:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 512BBC04FFE for ; Wed, 8 May 2024 17:56:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVr-0001Bl-On; Wed, 08 May 2024 13:55:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVn-0001Am-OM for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:28 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVh-00027i-10 for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:26 -0400 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a59c5c9c6aeso306366b.2 for ; Wed, 08 May 2024 10:55:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190917; x=1715795717; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EU56KqxsaM/0HEsRCm8u8nVsVnVRC1FhJWxFNu5K1Wg=; b=ThrVwvUDsNMFMihZG35qCNfLsvH0/XDzYelLJ2oaWT1Xfxst4OSjpvuqaQy8QkT7Iz yyf07m82XExEBe55Z0pVOJ44Z26IC5glTCyXMMDdp2tGyN6Cde4SAYtWsAPHGWfkH3ah RkmWEMErZQVntyBphaISsayyNCiTpuu7nJ8VLavln9s+zhzcy0TVcGLg7kUGv7VqXcpU 394eaz2M9fLUQqqx3M/3kf/dnxypZrgxlR05VfdTzMFMDv/ojOnpSnm9hOX6/u591E89 E7dnYVyzgudG4t5aOPJXaeNCi4aUK+sqHyEsujW7p6S1Cq+KZiRh2ulBTYPYthuE9B+H Q2/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190917; x=1715795717; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EU56KqxsaM/0HEsRCm8u8nVsVnVRC1FhJWxFNu5K1Wg=; b=Zltf5TdezuitE49zf/fcR8/TqeB70sfbO+a6jfFtkhPKKM/yX0ni984BEn60tqkpy+ oIZ8cIOzmCbsWBOosoVcxVqqTWLwz1hQ4puiDfhkA6NdFZE/IgCML0GVDt1seZkY0/ki enG2/JRfMQ8uDYtv6ta9eOovCDyv1QoMVz7yft1DSWLf4wvHjPWbvc71jYaXK3QdS+nx hIBglSi8vhrOjGIiNtNJMNG5WAcEyueAopLX7VYFoKeplTy0G/mU5JwX1+6B//FJW8GE aichgTOvf7CzbuY/8K9E2FYETrkUF2VZ+OG3OUHAiKThlOeyg4edKl7rt9sg+fAsOuuE JmKg== X-Gm-Message-State: AOJu0Ywc/eDnbAynS9FWTphyM3mg5ph7dRHpNOm9ZYHEYlGFn+9OxgAo fuTXLEElccgAmneeKRMhG5C+09Un9Q5sPNdkZgENR8vM00+hUQJd1MbNDw== X-Google-Smtp-Source: AGHT+IFcLY8yA/vCzjycEdYGohWoZCib+pffxSmvqqoekHDpezhZ0FmuLQ+UDi5WlaeK0cUBkl9Djw== X-Received: by 2002:a17:906:4956:b0:a59:d39a:8d65 with SMTP id a640c23a62f3a-a59fb95556dmr222797466b.21.1715190916911; Wed, 08 May 2024 10:55:16 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:16 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 1/6] hw/i386/x86: Eliminate two if statements in x86_bios_rom_init() Date: Wed, 8 May 2024 19:55:02 +0200 Message-ID: <20240508175507.22270-2-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=shentey@gmail.com; helo=mail-ej1-x62f.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Given that memory_region_set_readonly() is a no-op when the readonlyness is already as requested it is possible to simplify the pattern if (condition) { foo(true); } to foo(condition); which is shorter and allows to see the invariant of the code more easily. Signed-off-by: Bernhard Beschow Reviewed-by: Philippe Mathieu-Daudé --- hw/i386/x86.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 3d5b51e92d..2a4f3ee285 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -1163,9 +1163,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, load_image_size(filename, ptr, bios_size); x86_firmware_configure(ptr, bios_size); } else { - if (!isapc_ram_fw) { - memory_region_set_readonly(bios, true); - } + memory_region_set_readonly(bios, !isapc_ram_fw); ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); if (ret != 0) { goto bios_error; @@ -1182,9 +1180,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, 0x100000 - isa_bios_size, isa_bios, 1); - if (!isapc_ram_fw) { - memory_region_set_readonly(isa_bios, true); - } + memory_region_set_readonly(isa_bios, !isapc_ram_fw); /* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory, From patchwork Wed May 8 17:55:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6805FC04FFE for ; Wed, 8 May 2024 17:56:11 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVr-0001Bk-Lg; Wed, 08 May 2024 13:55:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVl-0001AS-9Y for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:25 -0400 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVh-00027w-Km for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:23 -0400 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-a599a298990so407166b.2 for ; Wed, 08 May 2024 10:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190919; x=1715795719; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hJDIVyxjPNuLrERcUUBAFqkvrcsC+w94NadcVcwbfE8=; b=fjgCW4K1u/rWLhqB32PJ1ahsIZHBFWERA7gHJ+vtzqD9j+ULLwLuSBHuidqu95rfyj +pzkpEjljXpSCR6JC2UySVcj393COPu+4tthEHysERgPtrtEYrI4zBY4525MmMFVjxt4 6mmgYmVaSsbLVczQA2oiA3hRoO059W/DkSYtoRH5/cz+4BshGXEZvcMwZFW8nIopm8Qo RYziIh+snS6Md7JGUyZUkbQwrVhVf2l7OOtYeLKhKWzLHi+VBwXhzZRqTDHP7QOBlB89 msBeyaOxNybVnVzDMUW7l1d6WM2oK+kE4SwO+txfZZ5IMcjj8axl4F9FX886okiaysfj n7aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190919; x=1715795719; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hJDIVyxjPNuLrERcUUBAFqkvrcsC+w94NadcVcwbfE8=; b=obVFbQdCVpQfi5R+fgcWWvQ01g5q2UD2tDs/Nfc5YiWaI7ADm5h8ENWGeDVsn3yy83 xjrf5YwwrjVY88sfFUNjKFot/R5eQTLTXts6L1UDYMysRsecJhRMDAQs+zXxqthDF182 2bw3gyxZP8Zt7TY459RcjQ+9KI+S22rRJDqofSuGiTiqSjBwOpn9Xzqg+8h4xpeOPWOx Ii8LW+2ruUNJG1gNQKaIvj1gL5I9sXX0YWTukGCaPdsyGmmBvt32/tlgiLHa+QfSWwrM EuOmOBdUrdI+FmUy960S9Ju2+bujqufaYPnoLDIwE0v275/TzWHBQ97dToydyMwBYQvY aFjg== X-Gm-Message-State: AOJu0YyE0i0guwtXKc/FVVmTdW7G7zlQXz9rYEHPQ5f0CPT1ke7S5UVc sPfrjRHGAN7D1vKvNPTR+5GrrYYdivlZbVHh2JbU7FdJJCpx0B9zQFoeoA== X-Google-Smtp-Source: AGHT+IFwJm8yLM/cJw31QY5OGB2pf+OKfGhDNpWVW2zFV7wdn2Dz4pbG1gIiPhJ/EvklV3Lik0bvTQ== X-Received: by 2002:a17:906:3544:b0:a59:c319:f1de with SMTP id a640c23a62f3a-a59fb92142cmr205465366b.15.1715190918964; Wed, 08 May 2024 10:55:18 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:17 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 2/6] hw/i386: Have x86_bios_rom_init() take X86MachineState rather than MachineState Date: Wed, 8 May 2024 19:55:03 +0200 Message-ID: <20240508175507.22270-3-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::629; envelope-from=shentey@gmail.com; helo=mail-ej1-x629.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The function creates and leaks two MemoryRegion objects regarding the BIOS which will be moved into X86MachineState in the next steps to avoid the leakage. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Bernhard Beschow --- include/hw/i386/x86.h | 2 +- hw/i386/microvm.c | 2 +- hw/i386/pc_sysfw.c | 4 ++-- hw/i386/x86.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index 4dc30dcb4d..cb07618d19 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -116,7 +116,7 @@ void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev, void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp); -void x86_bios_rom_init(MachineState *ms, const char *default_firmware, +void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, MemoryRegion *rom_memory, bool isapc_ram_fw); void x86_load_linux(X86MachineState *x86ms, diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c index 61a772dfe6..fec63cacfa 100644 --- a/hw/i386/microvm.c +++ b/hw/i386/microvm.c @@ -278,7 +278,7 @@ static void microvm_devices_init(MicrovmMachineState *mms) default_firmware = x86_machine_is_acpi_enabled(x86ms) ? MICROVM_BIOS_FILENAME : MICROVM_QBOOT_FILENAME; - x86_bios_rom_init(MACHINE(mms), default_firmware, get_system_memory(), true); + x86_bios_rom_init(x86ms, default_firmware, get_system_memory(), true); } static void microvm_memory_init(MicrovmMachineState *mms) diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index 87b5bf59d6..59c7a81692 100644 --- a/hw/i386/pc_sysfw.c +++ b/hw/i386/pc_sysfw.c @@ -205,7 +205,7 @@ void pc_system_firmware_init(PCMachineState *pcms, BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; if (!pcmc->pci_enabled) { - x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true); + x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, true); return; } @@ -226,7 +226,7 @@ void pc_system_firmware_init(PCMachineState *pcms, if (!pflash_blk[0]) { /* Machine property pflash0 not set, use ROM mode */ - x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false); + x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, false); } else { if (kvm_enabled() && !kvm_readonly_mem_enabled()) { /* diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 2a4f3ee285..6d3c72f124 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -1128,7 +1128,7 @@ void x86_load_linux(X86MachineState *x86ms, nb_option_roms++; } -void x86_bios_rom_init(MachineState *ms, const char *default_firmware, +void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, MemoryRegion *rom_memory, bool isapc_ram_fw) { const char *bios_name; @@ -1138,7 +1138,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, ssize_t ret; /* BIOS load */ - bios_name = ms->firmware ?: default_firmware; + bios_name = MACHINE(x86ms)->firmware ?: default_firmware; filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); if (filename) { bios_size = get_image_size(filename); From patchwork Wed May 8 17:55:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659018 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 59AD4C04FFE for ; Wed, 8 May 2024 17:55:43 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVr-0001BR-4U; Wed, 08 May 2024 13:55:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVl-0001AR-8l for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:25 -0400 Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVi-00028I-PW for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:24 -0400 Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-51f71e4970bso5512693e87.2 for ; Wed, 08 May 2024 10:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190921; x=1715795721; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qfdYiD64f+4cUowLihzuANf2MWyPtIwpg/YL0XZS8FI=; b=EJvy0TyWO2YpeOIj5NVBl28w5L+VzHrof9qPZXuqCD6Cas4Jro3nvXE+cCQ9kJFAjh lSdNacoHyA/Tq06BVvmniczU6Hmks0KNpmbGcqjzhl5kaNAcifv8VzL/d3I1gmKnr05v sjImuQx6hs5N/7foVRMSyiZ6BLSDwLdu8P2s5jzkThvNr+U7PDM1XRfUPTCT8TJEEouw eM50pNnNq6G8RoFxXLZW5PgTUG1qosC5nTtuGA1w5IqBh7s2p3mU7KzqNDMs7+12dxHY 0E/8H7jRXDneS/VmEMAIkD57myx3m4i4G/ITXkLkkrJM2wRYW4ntqDVYOoU05o6ha9YS fe9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190921; x=1715795721; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qfdYiD64f+4cUowLihzuANf2MWyPtIwpg/YL0XZS8FI=; b=Uw+1NK/ZQzADXYKS5fHZZ/aP5Uejm3PFcXYmdGnYl6zwBswbcAzfN+QOHq/5b4I6vG ahnDYMwGvqefBkyXUd3d9GBQtqeulgx4LEODsi11yg7/iYDN2DPwF9WPpeNuZwSd/HK9 sFw/XX9MzbmV9syvt6oZ2d9d0ILe6b696dCxBjJE6Qlo1G2Bhcu4oUAf8DYomazk+8fa ITd6N+Xir3to02At+vw85BdGDCkfylaaHkKb7Tri38iB8dVYf94hemvFo6WFjk+qra9z M0JvnAvSWy17fLKQtyBq1cClDU/vKcxmLogJ5cfgaiad7XjNoFoTD16bGTKN3UekYKr0 PVow== X-Gm-Message-State: AOJu0Yy8mHOJUseQ2Ze29WD43oR8BNeHw70QrpjWr0IyMwkgaiKgvBvo xNBe4o51AEPXM2OZqfDIh4JTc7CI2FCwQMuvnZRWYEk3yvd97FlqYrpoQA== X-Google-Smtp-Source: AGHT+IFzBcCtERbNX/I0rpM/6M46ZdylCG8BoivHzOAXzt2EU0yseZtqA9sYN8P5Vv3jMN01gX96XQ== X-Received: by 2002:ac2:5e9b:0:b0:51b:1e76:4ea9 with SMTP id 2adb3069b0e04-5217c372edfmr2795595e87.4.1715190920400; Wed, 08 May 2024 10:55:20 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:19 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 3/6] hw/i386/x86: Don't leak "isa-bios" memory regions Date: Wed, 8 May 2024 19:55:04 +0200 Message-ID: <20240508175507.22270-4-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::12f; envelope-from=shentey@gmail.com; helo=mail-lf1-x12f.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Fix the leaking in x86_bios_rom_init() and pc_isa_bios_init() by adding an "isa_bios" attribute to X86MachineState. Suggested-by: Philippe Mathieu-Daudé Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Bernhard Beschow --- include/hw/i386/x86.h | 7 +++++++ hw/i386/pc_sysfw.c | 7 +++---- hw/i386/x86.c | 9 ++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index cb07618d19..a07de79167 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -18,6 +18,7 @@ #define HW_I386_X86_H #include "exec/hwaddr.h" +#include "exec/memory.h" #include "hw/boards.h" #include "hw/intc/ioapic.h" @@ -52,6 +53,12 @@ struct X86MachineState { GMappedFile *initrd_mapped_file; HotplugHandler *acpi_dev; + /* + * Map the upper 128 KiB of the BIOS just underneath the 1 MiB address + * boundary. + */ + MemoryRegion isa_bios; + /* RAM information (sizes, addresses, configuration): */ ram_addr_t below_4g_mem_size, above_4g_mem_size; diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index 59c7a81692..82d37cb376 100644 --- a/hw/i386/pc_sysfw.c +++ b/hw/i386/pc_sysfw.c @@ -40,11 +40,10 @@ #define FLASH_SECTOR_SIZE 4096 -static void pc_isa_bios_init(MemoryRegion *rom_memory, +static void pc_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *rom_memory, MemoryRegion *flash_mem) { int isa_bios_size; - MemoryRegion *isa_bios; uint64_t flash_size; void *flash_ptr, *isa_bios_ptr; @@ -52,7 +51,6 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory, /* map the last 128KB of the BIOS in ISA space */ isa_bios_size = MIN(flash_size, 128 * KiB); - isa_bios = g_malloc(sizeof(*isa_bios)); memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, &error_fatal); memory_region_add_subregion_overlap(rom_memory, @@ -136,6 +134,7 @@ void pc_system_flash_cleanup_unused(PCMachineState *pcms) static void pc_system_flash_map(PCMachineState *pcms, MemoryRegion *rom_memory) { + X86MachineState *x86ms = X86_MACHINE(pcms); hwaddr total_size = 0; int i; BlockBackend *blk; @@ -185,7 +184,7 @@ static void pc_system_flash_map(PCMachineState *pcms, if (i == 0) { flash_mem = pflash_cfi01_get_memory(system_flash); - pc_isa_bios_init(rom_memory, flash_mem); + pc_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem); /* Encrypt the pflash boot ROM */ if (sev_enabled()) { diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 6d3c72f124..457e8a34a5 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -1133,7 +1133,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, { const char *bios_name; char *filename; - MemoryRegion *bios, *isa_bios; + MemoryRegion *bios; int bios_size, isa_bios_size; ssize_t ret; @@ -1173,14 +1173,13 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, /* map the last 128KB of the BIOS in ISA space */ isa_bios_size = MIN(bios_size, 128 * KiB); - isa_bios = g_malloc(sizeof(*isa_bios)); - memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, + memory_region_init_alias(&x86ms->isa_bios, NULL, "isa-bios", bios, bios_size - isa_bios_size, isa_bios_size); memory_region_add_subregion_overlap(rom_memory, 0x100000 - isa_bios_size, - isa_bios, + &x86ms->isa_bios, 1); - memory_region_set_readonly(isa_bios, !isapc_ram_fw); + memory_region_set_readonly(&x86ms->isa_bios, !isapc_ram_fw); /* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory, From patchwork Wed May 8 17:55:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ED39CC04FFE for ; Wed, 8 May 2024 17:56:54 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVv-0001Dc-7q; Wed, 08 May 2024 13:55:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVn-0001Al-OO for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:28 -0400 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVl-00028U-2W for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:26 -0400 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-a59c5c9c6aeso320566b.2 for ; Wed, 08 May 2024 10:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190922; x=1715795722; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6+TSGbSG4esBOIIsXyBX5ILJEmV3/MNWQb4SUxib0NM=; b=Ak0juWaMLcRcf8BLU1gG25TAdJt7S78V2NcgJTJv7ZnPXEGrJHJ8dB/vHA4P53K1DT 10yx2nbvqlo/sJzatDWYQUuT6vyZ24UJJqTXz6VjhgFGoKm5+uVL1jJ3upIcFXgObKQ6 0YMq4nFf0Wv0nhwkZHPbzrwAu1FmpOnG2E4ijoSXwDZ7CzuUD/3/6Q650Jk2uH023O6i bgEcDFuBg/UrbeQiOYBLveWclUqjjEVpjijue3zcivDVSnmnROaGDQUvSn+IoT4sBetI 9BtKw+c39qcaRdXcrEwMs7mA+q9DV1Nz3/uhkHVx+gKoVZVGxd2EEtNBJUqP8ZTrjfHp 1vNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190922; x=1715795722; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6+TSGbSG4esBOIIsXyBX5ILJEmV3/MNWQb4SUxib0NM=; b=WcJrARsJBTvgv1/Ztsr4+H67BMDimZMQB/zMktyMOKV/w7CoZBG9At/SKixFbs2x+H PTu2R5Pfiz7CAKiooyUna2SQhAgIA3PbD/+YBIEmKMvhf8NyhLYdecDdUjwgbin+zmNG qHcEv7eCmcdX7zxAjI65QNesHX1V1OXHzIPp4Kt1W0sGsarGfp0mKVLfzR4tG3jg2d7k ZOQaKgh+5CWjSf+qYTZ1bhWD7e3RJnRuP93+kINbH4um7doeyobGMRws/kPc8hddWqR7 O0oun33MCQmDyoliGVvIdPngrq3ds0JqJdmUBNPh6ulMH+TSjd5zdNfbNj5AuJRbmFkP RpwA== X-Gm-Message-State: AOJu0Yzzx/HZ7Gleib24vr5f+RJ6C0aR13Bdm7RKKgBKeDqF9ogQ5dQ1 c5xZRx8re+TDbVqrDVJoJnVadsfRyxI9SMFLicIH6kB0btfmTZSiuVMGXg== X-Google-Smtp-Source: AGHT+IGZfpYtOLXRaFAwyDghnNGZWQBwNxYloXRd5i/jpiUp2XTtVz8zlkcP1PUmzDE9XDPrLjELzQ== X-Received: by 2002:a17:906:b80d:b0:a59:cdf4:f940 with SMTP id a640c23a62f3a-a59fb955e80mr206570866b.28.1715190921807; Wed, 08 May 2024 10:55:21 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:21 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 4/6] hw/i386/x86: Don't leak "pc.bios" memory region Date: Wed, 8 May 2024 19:55:05 +0200 Message-ID: <20240508175507.22270-5-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::629; envelope-from=shentey@gmail.com; helo=mail-ej1-x629.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Fix the leaking in x86_bios_rom_init() by adding a "bios" attribute to X86MachineState. Note that it is only used in the -bios case. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Bernhard Beschow --- include/hw/i386/x86.h | 6 ++++++ hw/i386/x86.c | 13 ++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index a07de79167..55c6809ae0 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -53,6 +53,12 @@ struct X86MachineState { GMappedFile *initrd_mapped_file; HotplugHandler *acpi_dev; + /* + * Map the whole BIOS just underneath the 4 GiB address boundary. Only used + * in the ROM (-bios) case. + */ + MemoryRegion bios; + /* * Map the upper 128 KiB of the BIOS just underneath the 1 MiB address * boundary. diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 457e8a34a5..29167de97d 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -1133,7 +1133,6 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, { const char *bios_name; char *filename; - MemoryRegion *bios; int bios_size, isa_bios_size; ssize_t ret; @@ -1149,8 +1148,8 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, (bios_size % 65536) != 0) { goto bios_error; } - bios = g_malloc(sizeof(*bios)); - memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal); + memory_region_init_ram(&x86ms->bios, NULL, "pc.bios", bios_size, + &error_fatal); if (sev_enabled()) { /* * The concept of a "reset" simply doesn't exist for @@ -1159,11 +1158,11 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, * the firmware as rom to properly re-initialize on reset. * Just go for a straight file load instead. */ - void *ptr = memory_region_get_ram_ptr(bios); + void *ptr = memory_region_get_ram_ptr(&x86ms->bios); load_image_size(filename, ptr, bios_size); x86_firmware_configure(ptr, bios_size); } else { - memory_region_set_readonly(bios, !isapc_ram_fw); + memory_region_set_readonly(&x86ms->bios, !isapc_ram_fw); ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); if (ret != 0) { goto bios_error; @@ -1173,7 +1172,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, /* map the last 128KB of the BIOS in ISA space */ isa_bios_size = MIN(bios_size, 128 * KiB); - memory_region_init_alias(&x86ms->isa_bios, NULL, "isa-bios", bios, + memory_region_init_alias(&x86ms->isa_bios, NULL, "isa-bios", &x86ms->bios, bios_size - isa_bios_size, isa_bios_size); memory_region_add_subregion_overlap(rom_memory, 0x100000 - isa_bios_size, @@ -1184,7 +1183,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, /* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory, (uint32_t)(-bios_size), - bios); + &x86ms->bios); return; bios_error: From patchwork Wed May 8 17:55:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 987FDC04FFE for ; Wed, 8 May 2024 17:56:27 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVr-0001Bj-Jg; Wed, 08 May 2024 13:55:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVn-0001An-OL for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:28 -0400 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVl-00028b-6J for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:26 -0400 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a599eedc8eeso2561466b.1 for ; Wed, 08 May 2024 10:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190923; x=1715795723; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xf3jKnS45LzINVXGJFDGMTra3RQ9wnYhNqwEpjOfSQw=; b=U/caoGQc0R+whRnIdOAbAGoUUyVCJyvbzzC1sK1BAIe96OPTy5CR4snNyZo7/Q19Kl Cu6dgXsbp8WTztS2wyAGNEsbhZdBO07AlJwAGRkotbIvSVyh2ifRjDn04R0w1/VO9Ndl scbTBL2cdJEhANfGNckUCzJMVg8zdazU1+SnJso+gyELyhTcjn9cARPmBRmOaKjuXzN3 wR1hG80UDHPUURL7COLT+iDeS+IZjEOZqpeumTHaQZjAF8FL686AR4jsKAhnuZvLTV9l tYnMD2ogFadGxvha+r1U4tN7f/qUswPUaNrNgr9l80Jgk/79LgWNQ/CpbiWK+rnx1Ae9 bgsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190923; x=1715795723; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xf3jKnS45LzINVXGJFDGMTra3RQ9wnYhNqwEpjOfSQw=; b=VAavroGZ1iV8P7IulAZ+Tckh240nZWrA3I3i5WUimAg9HQkSRq8BzNm+k3XnimNDmR fIvg3HqvAcs15R2EU35c9L+avH9D5vW4kYoRQoxhGT3C3OHORv7kit0zye7Yia8iqj1C lZGglsgIwFl+0YRCZRECiiH8isOEZOtBjLkjYDeRkuf47k/hoIFpHQvFs4wDmcSnPbBu B6nf8R5vILarysyp6hsKDhM5BG5LBXI8ipYK3nTw9V+LohITsCPheo7A5iJyP3Xek9RE +QZks56wtTCLTFfolXWV3SoumOSNi5wGNzvEWfNhn37A0V+vSn2+ywVvx7X0Cve4y4Ne sQFg== X-Gm-Message-State: AOJu0YwvdDmDLqRJQ0xmO8pFWXiIV7uaqGEOHdu2Dnsp7HrAfbE90cNm SdmiTQQjCm3XoiFuYsFpxfk9FKbkUCCi6xIdKX5wpf/ENy8Gm9sJxhE5uQ== X-Google-Smtp-Source: AGHT+IE6Q5x3GxRyUV0pwEaQzA9Cp7ogOewGCA5jqk2rYBF7tkPPFViQInZeNi555vFN/70sl3EnOg== X-Received: by 2002:a17:906:2352:b0:a55:a895:46ae with SMTP id a640c23a62f3a-a59fb9d061bmr199009766b.63.1715190923065; Wed, 08 May 2024 10:55:23 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:22 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 5/6] hw/i386/x86: Extract x86_isa_bios_init() from x86_bios_rom_init() Date: Wed, 8 May 2024 19:55:06 +0200 Message-ID: <20240508175507.22270-6-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::636; envelope-from=shentey@gmail.com; helo=mail-ej1-x636.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The function is inspired by pc_isa_bios_init() and should eventually replace it. Using x86_isa_bios_init() rather than pc_isa_bios_init() fixes pflash commands to work in the isa-bios region. While at it convert the magic number 0x100000 (== 1MiB) to increase readability. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Bernhard Beschow --- include/hw/i386/x86.h | 2 ++ hw/i386/x86.c | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index 55c6809ae0..d7b7d3f3ce 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -129,6 +129,8 @@ void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev, void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp); +void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory, + MemoryRegion *bios, bool read_only); void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, MemoryRegion *rom_memory, bool isapc_ram_fw); diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 29167de97d..c61f4ebfa6 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -1128,12 +1128,25 @@ void x86_load_linux(X86MachineState *x86ms, nb_option_roms++; } +void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory, + MemoryRegion *bios, bool read_only) +{ + uint64_t bios_size = memory_region_size(bios); + uint64_t isa_bios_size = MIN(bios_size, 128 * KiB); + + memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, + bios_size - isa_bios_size, isa_bios_size); + memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size, + isa_bios, 1); + memory_region_set_readonly(isa_bios, read_only); +} + void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, MemoryRegion *rom_memory, bool isapc_ram_fw) { const char *bios_name; char *filename; - int bios_size, isa_bios_size; + int bios_size; ssize_t ret; /* BIOS load */ @@ -1171,14 +1184,8 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, g_free(filename); /* map the last 128KB of the BIOS in ISA space */ - isa_bios_size = MIN(bios_size, 128 * KiB); - memory_region_init_alias(&x86ms->isa_bios, NULL, "isa-bios", &x86ms->bios, - bios_size - isa_bios_size, isa_bios_size); - memory_region_add_subregion_overlap(rom_memory, - 0x100000 - isa_bios_size, - &x86ms->isa_bios, - 1); - memory_region_set_readonly(&x86ms->isa_bios, !isapc_ram_fw); + x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios, + !isapc_ram_fw); /* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory, From patchwork Wed May 8 17:55:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernhard Beschow X-Patchwork-Id: 13659021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A8064C25B74 for ; Wed, 8 May 2024 17:56:11 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4lVt-0001DJ-UX; Wed, 08 May 2024 13:55:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4lVp-0001BQ-8Y for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:31 -0400 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4lVn-00028v-9M for qemu-devel@nongnu.org; Wed, 08 May 2024 13:55:29 -0400 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-51fea3031c3so6120429e87.0 for ; Wed, 08 May 2024 10:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715190924; x=1715795724; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q3ZfC5WZy7A+MxhECoq43BsVJE3/UqvvUQ3KRfQIO0U=; b=jP+QEOhXUorBfUa22ZkXPQ3cSlowtwI8OrxAumTwgOJlgIdTYG4+sP2G8bVi4xISJz 7GGMgTs9wR9IYGMk8cfRu/tuqQtCIDPMYL4ddbRcKktcqpBhJNfQbYP7LPxhR2Z4S026 RlOgvqwZ0nPyL5CEBGiil3GQur+F4UcnB/VA7Ec3W7lrq+kkJLvfiVkohV6CJVbK4t9c GwaN5TukU0GzCApcEpIl9QssoQDmXHLlFHQlpOiq/EXlTOdjI4rGvrltkgcMSl6H/5sA YZsMSsbrGyB5EFId9N4tZ76PUWM41nXF3st2q6q0plXPNOCQ7QpyRGs2bD0VbVE0kyQK HFXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715190924; x=1715795724; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q3ZfC5WZy7A+MxhECoq43BsVJE3/UqvvUQ3KRfQIO0U=; b=w9eFDhxnH04TwqIQe/bxXtYuEfEuTIwHHMxssYMPbEWuW2gFQBgj77ed90TWky9HHK q8wD+fdKkX+yClkNtkVOYc9VDw2ZK9N9c8tYyvndp4QIWn/zoI93F12yv3w5GC01tCbC yhjCLraotV0ftEy6MBLdGcQLkCuk75rveOJW9bL/KRy9AvjmLpCuvcBxDjv7TmtR1+rv AyvrkQ6Sz+cMFw0TMAfVhTHMrd0MQae0zoJxsvaLHWv3tM1/3DG0Tg7QcxHTo8SH5zCS lSJ2ryvGx8CIxzRvDYmwBZKBfz6JjwPLxYT3OJKLxNn5Ldr11LTkdzcbZnllNENAEqBr aQKw== X-Gm-Message-State: AOJu0YzIW5jz6orZgxH7F0J03Kt3humtJloK1w7O8bdEDSBV3NjRb0tj 8eTb8cNuvKP9ISQODo6MhDtROWdmHt1pRGxuQO3cqOJbiG5YQgqmNoCKWA== X-Google-Smtp-Source: AGHT+IFfGYmch1gqNZb9XcfQUWivGCG3nbTaTZpgOfBmwqQa5UlGcX4pLYtghVITjLau2YUuq7+zqw== X-Received: by 2002:a19:5f03:0:b0:521:e065:c6c6 with SMTP id 2adb3069b0e04-521e065c97dmr286934e87.53.1715190924291; Wed, 08 May 2024 10:55:24 -0700 (PDT) Received: from archlinux.. (dynamic-077-013-167-103.77.13.pool.telefonica.de. [77.13.167.103]) by smtp.gmail.com with ESMTPSA id a24-20020a170906671800b00a59c5a129basm4483821ejp.80.2024.05.08.10.55.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:55:23 -0700 (PDT) From: Bernhard Beschow To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Sergio Lopez , Richard Henderson , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Marcel Apfelbaum , Paolo Bonzini , Michael Roth , Bernhard Beschow Subject: [PATCH v3 6/6] hw/i386/pc_sysfw: Alias rather than copy isa-bios region Date: Wed, 8 May 2024 19:55:07 +0200 Message-ID: <20240508175507.22270-7-shentey@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508175507.22270-1-shentey@gmail.com> References: <20240508175507.22270-1-shentey@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::135; envelope-from=shentey@gmail.com; helo=mail-lf1-x135.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, FREEMAIL_FROM=0.001, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org In the -bios case the "isa-bios" memory region is an alias to the BIOS mapped to the top of the 4G memory boundary. Do the same in the -pflash case, but only for new machine versions for migration compatibility. This establishes common behavior and makes pflash commands work in the "isa-bios" region which some real-world legacy bioses rely on. Note that in the sev_enabled() case, the "isa-bios" memory region in the -pflash case will now also point to encrypted memory, just like it already does in the -bios case. When running `info mtree` before and after this commit with `qemu-system-x86_64 -S -drive \ if=pflash,format=raw,readonly=on,file=/usr/share/qemu/bios-256k.bin` and running `diff -u before.mtree after.mtree` results in the following changes in the memory tree: --- before.mtree +++ after.mtree @@ -71,7 +71,7 @@ 0000000000000000-ffffffffffffffff (prio -1, i/o): pci 00000000000a0000-00000000000bffff (prio 1, i/o): vga-lowmem 00000000000c0000-00000000000dffff (prio 1, rom): pc.rom - 00000000000e0000-00000000000fffff (prio 1, rom): isa-bios + 00000000000e0000-00000000000fffff (prio 1, romd): alias isa-bios @system.flash0 0000000000020000-000000000003ffff 00000000000a0000-00000000000bffff (prio 1, i/o): alias smram-region @pci 00000000000a0000-00000000000bffff 00000000000c0000-00000000000c3fff (prio 1, i/o): alias pam-pci @pci 00000000000c0000-00000000000c3fff 00000000000c4000-00000000000c7fff (prio 1, i/o): alias pam-pci @pci 00000000000c4000-00000000000c7fff @@ -108,7 +108,7 @@ 0000000000000000-ffffffffffffffff (prio -1, i/o): pci 00000000000a0000-00000000000bffff (prio 1, i/o): vga-lowmem 00000000000c0000-00000000000dffff (prio 1, rom): pc.rom - 00000000000e0000-00000000000fffff (prio 1, rom): isa-bios + 00000000000e0000-00000000000fffff (prio 1, romd): alias isa-bios @system.flash0 0000000000020000-000000000003ffff 00000000000a0000-00000000000bffff (prio 1, i/o): alias smram-region @pci 00000000000a0000-00000000000bffff 00000000000c0000-00000000000c3fff (prio 1, i/o): alias pam-pci @pci 00000000000c0000-00000000000c3fff 00000000000c4000-00000000000c7fff (prio 1, i/o): alias pam-pci @pci 00000000000c4000-00000000000c7fff @@ -131,11 +131,14 @@ memory-region: pc.ram 0000000000000000-0000000007ffffff (prio 0, ram): pc.ram +memory-region: system.flash0 + 00000000fffc0000-00000000ffffffff (prio 0, romd): system.flash0 + memory-region: pci 0000000000000000-ffffffffffffffff (prio -1, i/o): pci 00000000000a0000-00000000000bffff (prio 1, i/o): vga-lowmem 00000000000c0000-00000000000dffff (prio 1, rom): pc.rom - 00000000000e0000-00000000000fffff (prio 1, rom): isa-bios + 00000000000e0000-00000000000fffff (prio 1, romd): alias isa-bios @system.flash0 0000000000020000-000000000003ffff memory-region: smram 00000000000a0000-00000000000bffff (prio 0, ram): alias smram-low @pc.ram 00000000000a0000-00000000000bffff Note that in both cases the "system" memory region contains the entry 00000000fffc0000-00000000ffffffff (prio 0, romd): system.flash0 but the "system.flash0" memory region only appears standalone when "isa-bios" is an alias. Signed-off-by: Bernhard Beschow Acked-by: Michael S. Tsirkin --- include/hw/i386/pc.h | 1 + hw/i386/pc.c | 1 + hw/i386/pc_piix.c | 3 +++ hw/i386/pc_q35.c | 2 ++ hw/i386/pc_sysfw.c | 8 +++++++- 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h index e52290916c..ad9c3d9ba8 100644 --- a/include/hw/i386/pc.h +++ b/include/hw/i386/pc.h @@ -119,6 +119,7 @@ struct PCMachineClass { bool enforce_aligned_dimm; bool broken_reserved_end; bool enforce_amd_1tb_hole; + bool isa_bios_alias; /* generate legacy CPU hotplug AML */ bool legacy_cpu_hotplug; diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 46235466d7..4878705af7 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1812,6 +1812,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data) pcmc->has_reserved_memory = true; pcmc->enforce_aligned_dimm = true; pcmc->enforce_amd_1tb_hole = true; + pcmc->isa_bios_alias = true; /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported * to be used at the moment, 32K should be enough for a while. */ pcmc->acpi_data_size = 0x20000 + 0x8000; diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c index 8850c49c66..d4e9deb509 100644 --- a/hw/i386/pc_piix.c +++ b/hw/i386/pc_piix.c @@ -525,12 +525,15 @@ DEFINE_I440FX_MACHINE(v9_1, "pc-i440fx-9.1", NULL, static void pc_i440fx_9_0_machine_options(MachineClass *m) { + PCMachineClass *pcmc = PC_MACHINE_CLASS(m); + pc_i440fx_9_1_machine_options(m); m->alias = NULL; m->is_default = false; compat_props_add(m->compat_props, hw_compat_9_0, hw_compat_9_0_len); compat_props_add(m->compat_props, pc_compat_9_0, pc_compat_9_0_len); + pcmc->isa_bios_alias = false; } DEFINE_I440FX_MACHINE(v9_0, "pc-i440fx-9.0", NULL, diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c index bb53a51ac1..bd7db4abac 100644 --- a/hw/i386/pc_q35.c +++ b/hw/i386/pc_q35.c @@ -378,10 +378,12 @@ DEFINE_Q35_MACHINE(v9_1, "pc-q35-9.1", NULL, static void pc_q35_9_0_machine_options(MachineClass *m) { + PCMachineClass *pcmc = PC_MACHINE_CLASS(m); pc_q35_9_1_machine_options(m); m->alias = NULL; compat_props_add(m->compat_props, hw_compat_9_0, hw_compat_9_0_len); compat_props_add(m->compat_props, pc_compat_9_0, pc_compat_9_0_len); + pcmc->isa_bios_alias = false; } DEFINE_Q35_MACHINE(v9_0, "pc-q35-9.0", NULL, diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index 82d37cb376..ac88ad4eb9 100644 --- a/hw/i386/pc_sysfw.c +++ b/hw/i386/pc_sysfw.c @@ -135,6 +135,7 @@ static void pc_system_flash_map(PCMachineState *pcms, MemoryRegion *rom_memory) { X86MachineState *x86ms = X86_MACHINE(pcms); + PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); hwaddr total_size = 0; int i; BlockBackend *blk; @@ -184,7 +185,12 @@ static void pc_system_flash_map(PCMachineState *pcms, if (i == 0) { flash_mem = pflash_cfi01_get_memory(system_flash); - pc_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem); + if (pcmc->isa_bios_alias) { + x86_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem, + true); + } else { + pc_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem); + } /* Encrypt the pflash boot ROM */ if (sev_enabled()) {