From nobody Thu May 16 06:18:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1558018190; cv=none; d=zoho.com; s=zohoarc; b=eHpwy1ttneFG9QJ4ARmG0a7IUIHMHMcnF4cq7qBma9qrMX5UoRo6y9x1KODrQwVUr52vrAOCvdaCvjenWJuzfBceZ9cxhjM3uGp4raDK2jMsviskTN4X+MkJJsdJHptT+rhby+tjRepfITTaMfBvAlK97tsbKZREwMw2LQLe3ts= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558018190; 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:ARC-Authentication-Results; bh=ZzmBM+wcCb9270VyvJ51TIpyGicjAazonG2iWSjoQgc=; b=WX29oQn0JD6PIF9cJ89+uE1x1yaSEOX+GgTJV49cA9FKdP0+y3bsouje0lHk1R5OPhhH9Qmin0QXNFfmrmHMyIOqWxCknDigwW0IedXveFc0QNC2KKPrpv4YzmMKemf6ExaPVTn7onPeEs5X/WibS19yBSz3OJ5p9pokYOcgFGA= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=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 155801819059359.92160313896443; Thu, 16 May 2019 07:49:50 -0700 (PDT) Received: from localhost ([127.0.0.1]:59417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHhU-0003XY-Dd for importer@patchew.org; Thu, 16 May 2019 10:49:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58851) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHfX-0002Za-6W for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hRHfW-0008E8-0G for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:38 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:36333) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hRHfV-0008D3-Pm for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:37 -0400 Received: by mail-wr1-x444.google.com with SMTP id s17so3759008wru.3 for ; Thu, 16 May 2019 07:47:37 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id o8sm7629018wra.4.2019.05.16.07.47.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 07:47:36 -0700 (PDT) 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=ZzmBM+wcCb9270VyvJ51TIpyGicjAazonG2iWSjoQgc=; b=dFH3g7apCsFiSnQ2IZ2+IxFvBA512Pz9PeYAWM4u3CIt80+JQzjym49JE2DGBwhMtN BoI4Zt7gWyn2jKFh2AapYyYPsqIU+HRUR8PpHaZf+wIwbb357YNddL1MZ3cMTmZDDrsy dgFdwil6BkvO6u/ksdDsqY7A+IN+97kYRrajt88UTMolwNa05NEZiiO1W3+DYp6fxcT6 381b7K6K6XIi2g1chgiONKrCU3zzO4Ux47rmSubsnh6JIkC8tQm41idgbTz27KONAq3x zyI/HSzB/oZxWlwq6KKRdNcwCBZbuTBhEAzgDtSFKAGsVZhctaqneKCnnLGya+ojIjgJ kM7A== 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=ZzmBM+wcCb9270VyvJ51TIpyGicjAazonG2iWSjoQgc=; b=sDa2noDmsRZwHFmIJRd3EF2Q6jsSXU35IUjVMJUX66e8oGnN/7tm55IetVaaDcLyQU 3X8MPD5GRXhf0YJBwO45MsVDMPrya+ESfAZLKTywXeIDJ+4WJGLOYFwOv9phKbYc8DUW JUW4ok42jbXzu6qPqTyd/cxCg1SRUBQR5DnBzNYr99wOQCwUcPHSHxJy+qSqqJYoP1MZ C19xqNEhGzXkzBFefOwVfeZcPKjPz7SlLGXMM2fmIKtMsV8lvmgdoSpjZXfzSmGFl3i0 PcMTPtyayiF9LOzEh6bGlIhFMPVg4J8njW8+s94Ci3beE132vBr9vTTsBNwQbrN9gXFZ FViw== X-Gm-Message-State: APjAAAVqGaZVznndXIlkACuzJMQtIQEaXpv/AvKHddzEbXLcyXVr8rkF nmrr29Z/YDo3RKWI/viZW27AHA== X-Google-Smtp-Source: APXvYqyno3abO5WDcjRoEUpjErfabNhvEQ3prajEnTi2vpSofbX3VL+6PmCuiJvISI6uXiX5DPLabA== X-Received: by 2002:adf:c188:: with SMTP id x8mr20036758wre.256.1558018056982; Thu, 16 May 2019 07:47:36 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Thu, 16 May 2019 15:47:30 +0100 Message-Id: <20190516144733.32399-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190516144733.32399-1-peter.maydell@linaro.org> References: <20190516144733.32399-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::444 Subject: [Qemu-devel] [PATCH v2 1/4] hw/arm/boot: Don't assume RAM starts at address zero X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" In the Arm kernel/initrd loading code, in some places we make the incorrect assumption that info->ram_size can be treated as the address of the end of RAM, as for instance when we calculate the available space for the initrd using "info->ram_size - info->initrd_start". This is wrong, because many Arm boards (including "virt") specify a non-zero info->loader_start to indicate that their RAM area starts at a non-zero physical address. Correct the places which make this incorrect assumption. Signed-off-by: Peter Maydell Reviewed-by: Alex Benn=C3=A9e Tested-by: Mark Rutland --- hw/arm/boot.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hw/arm/boot.c b/hw/arm/boot.c index a830655e1af..0bb9a7d5b5c 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -976,6 +976,7 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, int elf_machine; hwaddr entry; static const ARMInsnFixup *primary_loader; + uint64_t ram_end =3D info->loader_start + info->ram_size; =20 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { primary_loader =3D bootloader_aarch64; @@ -1047,8 +1048,7 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, /* 32-bit ARM */ entry =3D info->loader_start + KERNEL_LOAD_ADDR; kernel_size =3D load_image_targphys_as(info->kernel_filename, entr= y, - info->ram_size - KERNEL_LOAD_= ADDR, - as); + ram_end - KERNEL_LOAD_ADDR, a= s); is_linux =3D 1; } if (kernel_size < 0) { @@ -1062,12 +1062,11 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cp= u, if (info->initrd_filename) { initrd_size =3D load_ramdisk_as(info->initrd_filename, info->initrd_start, - info->ram_size - info->initrd_st= art, - as); + ram_end - info->initrd_start, as= ); if (initrd_size < 0) { initrd_size =3D load_image_targphys_as(info->initrd_filena= me, info->initrd_start, - info->ram_size - + ram_end - info->initrd_start, as); } --=20 2.20.1 From nobody Thu May 16 06:18:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1558018215; cv=none; d=zoho.com; s=zohoarc; b=HeOrWASaZnLTQtD6Rpb4npzEEKYFPQ/tBEcOb1zucq5S9PZLKNZLbx0gxn+Q2dpmrW/Jwqzf6OvqeE0i0xoNAoXhBWCh4qoMmmn8SyRQnBaqULt9UernrrxUw7K8fy/F6b8Sti4M8yPIRmFHMl6BeDdikVnxuECXfk6U5EadwCI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558018215; 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:ARC-Authentication-Results; bh=DgMhkLiABo0DJwU8oJ8YkfjP+Hk6v1roeqysC4eg468=; b=HK+CiPxlUFRbZVYK+XqAB8iXk+1Pd+J5pBQgo/ZXa1/Mn3Yl1Hn8p522cPcEUnyq+igpY2hG0ZC6uAVJrgO+pBk7pV6na+4JAHrWr+0X1bOVTt2VdvVGYzMfr1WDjVSBR9p21Pvo6nC77coIj+PfKJxX1cucmJuBx3P09J3GwXE= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=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 1558018215975758.5045204241134; Thu, 16 May 2019 07:50:15 -0700 (PDT) Received: from localhost ([127.0.0.1]:59435 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHhq-0003oK-Qj for importer@patchew.org; Thu, 16 May 2019 10:50:02 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58903) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHfZ-0002au-7a for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hRHfY-0008H6-5g for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:41 -0400 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:39957) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hRHfX-0008Eh-86 for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:39 -0400 Received: by mail-wm1-x343.google.com with SMTP id h11so3752880wmb.5 for ; Thu, 16 May 2019 07:47:39 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id o8sm7629018wra.4.2019.05.16.07.47.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 07:47:37 -0700 (PDT) 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=DgMhkLiABo0DJwU8oJ8YkfjP+Hk6v1roeqysC4eg468=; b=SE+aDuN2LJ0lQpMoo3ZRFMPWP9p0Weq1dhOnfcYXLJ79rrxDtptgaDDry/xFsWUZqh eGBHZgrufeHGBGg8ZUruLfPOXSDTXPrDhA/iVRTm8gxN0vi+xvpPSnGtb+IdR3vvaOrg kxpGfZ3OvKEGOtcR352oxR76OHjU0B7Dpa1G/3CoLDW7Hbt4gxTXQbJfA0hivygq8P8s V0wEpSfveD1o3Z0yFyXJMoN6mK5UMIXdThhvM6YMXaKIJMAwk0uymPQ2KBL/H0kYXwyW +vGrU604TknDsnj9O+9Az3DG7WIxZ2meQfG/feuGmmQdTF+FOr7LSMyDpQm6YQCkRFP+ bn9A== 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=DgMhkLiABo0DJwU8oJ8YkfjP+Hk6v1roeqysC4eg468=; b=Kl6B5BcOlSIcjmipkYClOiAF/Wbl21VOWoJJI8F0nzSdrqMeLIZ9unVA2ZboSqshSb T5XnYv39+xjas5kNxKsvNfFZR5WEwuRhqrkGG504V9BZjKwl+1I6T7C4UXfjlz1ruqwn 937soYjLW+qebIw1YgyNXPezwD6I38Z7H42qEBNRAc7qVDd60uZlrICjjHR+1m7qYOrK eMopwC0fg7VkvvdFVGPQOVFxHXNStF25lYvn/cVWjaz3gJonToqjSyI64TzP5tBmMuXZ Oh/yKhLNCj7Mfkt4NxzqF04D2QSg13FYCONufzxKY76QYPfBLgA0L9vGb87NME/D8BzD qoqA== X-Gm-Message-State: APjAAAVh3Os9AX6Eype/jXKaT4bEawoHO33p5LZIycnye3OnAS2t/h9z ZTRVBHREFmpgTgYSrN5/ljrDgg== X-Google-Smtp-Source: APXvYqwJboeSmaXVCkOQA0uw97JcB1A7k6HHKmAOMOaI4Qh9zlskErpjyUIWYYIVZ9RjZjJjIf3Ogg== X-Received: by 2002:a1c:63d5:: with SMTP id x204mr11618190wmb.3.1558018058135; Thu, 16 May 2019 07:47:38 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Thu, 16 May 2019 15:47:31 +0100 Message-Id: <20190516144733.32399-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190516144733.32399-1-peter.maydell@linaro.org> References: <20190516144733.32399-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::343 Subject: [Qemu-devel] [PATCH v2 2/4] hw/arm/boot: Diagnose layouts that put initrd or DTB off the end of RAM X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We calculate the locations in memory where we want to put the initrd and the DTB based on the size of the kernel, since they come after it. Add some explicit checks that these aren't off the end of RAM entirely. (At the moment the way we calculate the initrd_start means that it can't ever be off the end of RAM, but that will change with the next commit.) Signed-off-by: Peter Maydell Reviewed-by: Alex Benn=C3=A9e Tested-by: Mark Rutland --- hw/arm/boot.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hw/arm/boot.c b/hw/arm/boot.c index 0bb9a7d5b5c..935be3b92a5 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -1055,11 +1055,25 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cp= u, error_report("could not load kernel '%s'", info->kernel_filename); exit(1); } + + if (kernel_size > info->ram_size) { + error_report("kernel '%s' is too large to fit in RAM " + "(kernel size %d, RAM size %" PRId64 ")", + info->kernel_filename, kernel_size, info->ram_size); + exit(1); + } + info->entry =3D entry; if (is_linux) { uint32_t fixupcontext[FIXUP_MAX]; =20 if (info->initrd_filename) { + + if (info->initrd_start >=3D ram_end) { + error_report("not enough space after kernel to load initrd= "); + exit(1); + } + initrd_size =3D load_ramdisk_as(info->initrd_filename, info->initrd_start, ram_end - info->initrd_start, as= ); @@ -1075,6 +1089,11 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, info->initrd_filename); exit(1); } + if (info->initrd_start + initrd_size > info->ram_size) { + error_report("could not load initrd '%s': " + "too big to fit into RAM after the kernel", + info->initrd_filename); + } } else { initrd_size =3D 0; } @@ -1110,6 +1129,10 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, /* Place the DTB after the initrd in memory with alignment. */ info->dtb_start =3D QEMU_ALIGN_UP(info->initrd_start + initrd_= size, align); + if (info->dtb_start >=3D ram_end) { + error_report("Not enough space for DTB after kernel/initrd= "); + exit(1); + } fixupcontext[FIXUP_ARGPTR_LO] =3D info->dtb_start; fixupcontext[FIXUP_ARGPTR_HI] =3D info->dtb_start >> 32; } else { --=20 2.20.1 From nobody Thu May 16 06:18:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1558018403; cv=none; d=zoho.com; s=zohoarc; b=M78dOgAT0b4RRDZGU5ziyJ4FdcxvE1GhCYFp2uH8EpQmPrrjCWdhwPB+DTVbJQ1N3vp1AI8qHpBD2HIFajlPxNznoY62t8c2jljboouVvMJduJO5yfrC+jfh0h+cPgo72jqpCUAca/P/WzcJgQMPbNS1USzokdtip+BVqau/Pq4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558018403; 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:ARC-Authentication-Results; bh=l9q7KvmjaLy6huMCLkNzLHAGghAhe5aW2pE9iFxRei4=; b=YPbgYxW56sK7Oa1713CgToggqwxjE/3s34X9ICIspLG9rMGslOSEN6AhJdoRpJGFzS0r1t6M2EHT30xpn7BpisPlVZTuTfXf1iIfmR9oGeyS8b9QeP75AMeF8f8M4saLH0hp8W2Au303AYg7caDtk9b2lZdCVhFzgCybNadSDBg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=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 1558018403434316.9127182280446; Thu, 16 May 2019 07:53:23 -0700 (PDT) Received: from localhost ([127.0.0.1]:59500 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHkx-0006Sa-B6 for importer@patchew.org; Thu, 16 May 2019 10:53:15 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58920) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHfZ-0002bP-Rp for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hRHfY-0008Hx-KV for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:41 -0400 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:45700) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hRHfY-0008G7-D0 for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:40 -0400 Received: by mail-wr1-x442.google.com with SMTP id b18so3683910wrq.12 for ; Thu, 16 May 2019 07:47:40 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id o8sm7629018wra.4.2019.05.16.07.47.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 07:47:38 -0700 (PDT) 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=l9q7KvmjaLy6huMCLkNzLHAGghAhe5aW2pE9iFxRei4=; b=siYCmilXdCQxs0pTnh8rf295KB88n2tg3pdX3QvoPyXQqmhksf1Kxuw127Sy048LOb Pj7IWwFOatj83QodTxOm2oRAE5okdEmtYzdFlB2a0zIowtMOhmgSHlW16JMK/dF6LWN+ pezYWy7mS2OLXXYlevM3XFwFZTu9ptLUUkbXzUS8n2NSSdqtHhr0y8G0/OyNnfZLTw99 eUuKHFTLxIiTR43JE+LVlWfrkj0Y26zcSWK7FfsUabYMHTeqqWgfrKVrY0dXPXlPP0fI 3FvQyMznOc8CM0mZkPfr2H0j8bTzQOlXhjRA9k+R1V2E4PmvbICdROlfbZZzOl5gO+xx fHXQ== 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=l9q7KvmjaLy6huMCLkNzLHAGghAhe5aW2pE9iFxRei4=; b=Dp4s3Lup7VKWBuz1s8HDkmRii0+kkbkmzjvTfALJ4Vt/Hp7g3q3mVzsx5ZBVUfrsWQ F8ECbZHCUZIciFMpKr5q4rAIxv5Wc5lASyC1LNc2yok7YQao/jdEpgT2TRhREHjdmBlM BkH02m35XMTdcuhT1TaAf0g6gOxtqgumbC1kLP+zLZW5rodBJliOaWSPROUg0zPi1Amd hYOm31pIy1C7ikDiovru1Ysl1Srw+ihkOUs3Mui3+sWBjVNRFmpOuPruljNQPRHkH5fE 7TG+liyl4cxtodIIc0tl05KpkEIOsqofYY7flwazz/ZmYIhqKd91yDox3fP9q/m2VSq3 avDg== X-Gm-Message-State: APjAAAUcRYx2POBW3R7ZEmKkxw8GuJHxLn0rJ2yBhhj2OTmUniW5ocIX TWkvSVm+5B07cVQBSH1TGYRNdg== X-Google-Smtp-Source: APXvYqx0E3d7kqMINU4Wxx4KBJs2TUmPTAyeuT4VhjY/bozFY/2i4qlC23tsg0unTnm19siU1Qspdg== X-Received: by 2002:adf:dcc4:: with SMTP id x4mr5709889wrm.107.1558018059213; Thu, 16 May 2019 07:47:39 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Thu, 16 May 2019 15:47:32 +0100 Message-Id: <20190516144733.32399-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190516144733.32399-1-peter.maydell@linaro.org> References: <20190516144733.32399-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::442 Subject: [Qemu-devel] [PATCH v2 3/4] hw/arm/boot: Avoid placing the initrd on top of the kernel X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We currently put the initrd at the smaller of: * 128MB into RAM * halfway into the RAM (with the dtb following it). However for large kernels this might mean that the kernel overlaps the initrd. For some kinds of kernel (self-decompressing 32-bit kernels, and ELF images with a BSS section at the end) we don't know the exact size, but even there we have a minimum size. Put the initrd at least further into RAM than that. For image formats that can give us an exact kernel size, this will mean that we definitely avoid overlaying kernel and initrd. Signed-off-by: Peter Maydell Reviewed-by: Alex Benn=C3=A9e Tested-by: Mark Rutland --- hw/arm/boot.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/hw/arm/boot.c b/hw/arm/boot.c index 935be3b92a5..e441393fdf5 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -999,20 +999,6 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, if (info->nb_cpus =3D=3D 0) info->nb_cpus =3D 1; =20 - /* - * We want to put the initrd far enough into RAM that when the - * kernel is uncompressed it will not clobber the initrd. However - * on boards without much RAM we must ensure that we still leave - * enough room for a decent sized initrd, and on boards with large - * amounts of RAM we must avoid the initrd being so far up in RAM - * that it is outside lowmem and inaccessible to the kernel. - * So for boards with less than 256MB of RAM we put the initrd - * halfway into RAM, and for boards with 256MB of RAM or more we put - * the initrd at 128MB. - */ - info->initrd_start =3D info->loader_start + - MIN(info->ram_size / 2, 128 * 1024 * 1024); - /* Assume that raw images are linux kernels, and ELF images are not. = */ kernel_size =3D arm_load_elf(info, &elf_entry, &elf_low_addr, &elf_high_addr, elf_machine, as); @@ -1064,6 +1050,26 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, } =20 info->entry =3D entry; + + /* + * We want to put the initrd far enough into RAM that when the + * kernel is uncompressed it will not clobber the initrd. However + * on boards without much RAM we must ensure that we still leave + * enough room for a decent sized initrd, and on boards with large + * amounts of RAM we must avoid the initrd being so far up in RAM + * that it is outside lowmem and inaccessible to the kernel. + * So for boards with less than 256MB of RAM we put the initrd + * halfway into RAM, and for boards with 256MB of RAM or more we put + * the initrd at 128MB. + * We also refuse to put the initrd somewhere that will definitely + * overlay the kernel we just loaded, though for kernel formats which + * don't tell us their exact size (eg self-decompressing 32-bit kernel= s) + * we might still make a bad choice here. + */ + info->initrd_start =3D info->loader_start + + MAX(MIN(info->ram_size / 2, 128 * 1024 * 1024), kernel_size); + info->initrd_start =3D TARGET_PAGE_ALIGN(info->initrd_start); + if (is_linux) { uint32_t fixupcontext[FIXUP_MAX]; =20 --=20 2.20.1 From nobody Thu May 16 06:18:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1558018385; cv=none; d=zoho.com; s=zohoarc; b=VfdQqyL9EmNC7Gvgz3xaVsSH+jTYhOsa69iVCWrduZPaJhCW0U5N4AAp0bvGfNr2bEn3/1IBgiHC7Cg4LNAOBj6K0WVo4iBquZi52EzZzxnVWZmNrOWFpJ2aXX+Gr7Y8G0lUQD4+3D8MfLCXfh0FmQfBoWiCPkIlNUhLAi3ECa8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558018385; 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:ARC-Authentication-Results; bh=GibGcyTIcznIDyrFlRM3SJZWG0A8Mzt1f5zkapp7jhE=; b=okIlV/6559ehSlzVFXWObVF1M5gjZMwlWVQpuM/YoiQ4jUH4L9psfFQFFBpzYA5KHu4jbXQUgWw6Zo8rDjJ8jty/pfB1vFXiyI5POKnp9gULpM/UcVFQ7pXZWBsrfzPTE6fP8kKYKfsALqZ3FUWg8xrdKbuDmL586ukiIp+inbs= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=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 1558018385054910.528454786925; Thu, 16 May 2019 07:53:05 -0700 (PDT) Received: from localhost ([127.0.0.1]:59498 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHkj-0006FQ-2U for importer@patchew.org; Thu, 16 May 2019 10:53:01 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58944) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hRHfb-0002cl-0J for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hRHfZ-0008JW-MS for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:42 -0400 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:50458) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hRHfZ-0008Hp-94 for qemu-devel@nongnu.org; Thu, 16 May 2019 10:47:41 -0400 Received: by mail-wm1-x341.google.com with SMTP id f204so3965740wme.0 for ; Thu, 16 May 2019 07:47:41 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id o8sm7629018wra.4.2019.05.16.07.47.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 07:47:39 -0700 (PDT) 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=GibGcyTIcznIDyrFlRM3SJZWG0A8Mzt1f5zkapp7jhE=; b=H8Vw5e9C8/9W/6OsKd81L4/LvbBc2gh2ltRh2oswyCqbSq88kWs6lpAxp3orCMc/jX LCRa8pEa+gCuox1nRe5+hcUgLe71W5es9StoMYW3KMGiNcEkhxCxuBWYFFap1+qnxidU k10jihy6GbiOWzv3rojFlAddeW8nDmQEr69j7y8zdyAOWSJH6yeatDF2EEnqvoSddlBh 8sNdUl67gTOYxS9p92PwAX7iW3fqI75WB3tJghGZ1qR7CuQAefC/NUQnRuUDotGMfppT znBGqXo8hvBXnrb4gNRgs9Yo85PsoIZpostZBcXPCkCI6BKAKhXiAbyDBFub22lvpDT5 EFDg== 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=GibGcyTIcznIDyrFlRM3SJZWG0A8Mzt1f5zkapp7jhE=; b=mRZVcRksXlONYLh7nQe86EQ3KVwchCfsd4q1+khbgZyk39E7O896OubeHBbPEuswXP hgbY+9TaWIlb5AbYW/8MZUOCZirK7AAaz6aZ1+3q8hcf5Ghi1a3DS34U63rG/jEn526A ub2hIxMK9rnOAHPLPXKoL+oValZ3rmZCFKqVU0hFxLXVDpAlmYk1d4vaDOEtR9lK/oiZ ALczErWfF65/C6ZTxQpCUxqngh/oNtqePao+l9/2F14+1IHSSdzA2f4wny6P4K7vJ+Du vXTcI2NiQUyCSUOa1ZOizCwKGfOJlD0TiOpCfzg0P3JDuLnC6O7AaWKSzSHfTpM2AtEa UBhg== X-Gm-Message-State: APjAAAWksxsxQ6+LwKX38KnVAT6STCvjZ8oaM1ddVLoF8WdxdMZQTfFZ mYu+ejeBG2MLblijP8d8pfXGRQ== X-Google-Smtp-Source: APXvYqzzvrGLTAMifVaW+w/j/ewdzNHniyUyJF3W9lTDfCcAYL2wH6By/+QRagfzPayS/ZrD1XUQ7w== X-Received: by 2002:a1c:e916:: with SMTP id q22mr27842504wmc.148.1558018060254; Thu, 16 May 2019 07:47:40 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Thu, 16 May 2019 15:47:33 +0100 Message-Id: <20190516144733.32399-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190516144733.32399-1-peter.maydell@linaro.org> References: <20190516144733.32399-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::341 Subject: [Qemu-devel] [PATCH v2 4/4] hw/arm/boot: Honour image size field in AArch64 Image format kernels X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Since Linux v3.17, the kernel's Image header includes a field image_size, which gives the total size of the kernel including unpopulated data sections such as the BSS). If this is present, then return it from load_aarch64_image() as the true size of the kernel rather than just using the size of the Image file itself. This allows the code which calculates where to put the initrd to avoid putting it in the kernel's BSS area. This means that we should be able to reliably load kernel images which are larger than 128MB without accidentally putting the initrd or dtb in locations that clash with the kernel itself. Fixes: https://bugs.launchpad.net/qemu/+bug/1823998 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Alex Benn=C3=A9e Tested-by: Mark Rutland --- hw/arm/boot.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/hw/arm/boot.c b/hw/arm/boot.c index e441393fdf5..fc6f37ba6cf 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -910,6 +910,7 @@ static uint64_t load_aarch64_image(const char *filename= , hwaddr mem_base, hwaddr *entry, AddressSpace *as) { hwaddr kernel_load_offset =3D KERNEL64_LOAD_ADDR; + uint64_t kernel_size =3D 0; uint8_t *buffer; int size; =20 @@ -937,7 +938,10 @@ static uint64_t load_aarch64_image(const char *filenam= e, hwaddr mem_base, * is only valid if the image_size is non-zero. */ memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals= )); - if (hdrvals[1] !=3D 0) { + + kernel_size =3D le64_to_cpu(hdrvals[1]); + + if (kernel_size !=3D 0) { kernel_load_offset =3D le64_to_cpu(hdrvals[0]); =20 /* @@ -955,12 +959,21 @@ static uint64_t load_aarch64_image(const char *filena= me, hwaddr mem_base, } } =20 + /* + * Kernels before v3.17 don't populate the image_size field, and + * raw images have no header. For those our best guess at the size + * is the size of the Image file itself. + */ + if (kernel_size =3D=3D 0) { + kernel_size =3D size; + } + *entry =3D mem_base + kernel_load_offset; rom_add_blob_fixed_as(filename, buffer, size, *entry, as); =20 g_free(buffer); =20 - return size; + return kernel_size; } =20 static void arm_setup_direct_kernel_boot(ARMCPU *cpu, --=20 2.20.1