From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55762C4338F for ; Sat, 31 Jul 2021 08:46:43 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9561E603E9 for ; Sat, 31 Jul 2021 08:46:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9561E603E9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7380083281; Sat, 31 Jul 2021 10:46:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NdEk6S6y"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2A02D8326E; Sat, 31 Jul 2021 10:45:54 +0200 (CEST) Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id ECF1582DDA for ; Sat, 31 Jul 2021 10:45:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=bmeng.cn@gmail.com Received: by mail-pj1-x102f.google.com with SMTP id b6so18590549pji.4 for ; Sat, 31 Jul 2021 01:45:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Eq8+saNIK6NYPUklgGkYIrYeETsTnnVPlvxzqmJZGoM=; b=NdEk6S6yAnCh5izw+Kedhws2kb8SQfD/w93dYkOOQ44ZrlkRwtDcadfJ08BYoHyO0/ Gm02pISLz1gMO1INIk+MxcUhByC8768ghcUt7ATBCPdqcfx5BindI4NHNNLiB3PjT9XF rheND+arjMEkimLRPjDlY5aX8vbWqo+JwjreembMpzeNwPJkWdN457udLIoxCbAe+Ks6 KSOgKhhoYZRk3Vl9IU7n2+B0pziWnJaNZ3F1Mw8CqR6o2QVgwVZ8qoJ0/n8PMOWlOyLE FRelJgfW2Yefm7ICQQTqEwp4Y10io0rWyxzVVy9EsLrfx/K258q9od/ip0Gjc0qYE7Na S3Cw== 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=Eq8+saNIK6NYPUklgGkYIrYeETsTnnVPlvxzqmJZGoM=; b=jrMbsCyqLEIfTuTd9p0KiSQUhrDcWtc/Fw7GFNiNab9/ARwjrONCnsLGm9uS21SEvV tDaDLW5Rs6M9EoHUl4RkmMLthNeaKMioBFyosZsVyN6PlgVBY0cbmgURqI18US7n+BFI o+/wQazZ4vQziwximYVpMcQI93xPSXWy2df+6lMP9tVFxc+7NWAtazyZrc2YdJp7MoG2 ipdT6Lv9MM7z/WDUtkJEchWyA3NIVOV/VkB5aOt3HwznwYiqFy4GHhvCnaHjsfX2nk0h s9JuKfVVma2hOVugHqtgmCk1CBGTMX+11PkgzDA05gpId1YeuOf98nxficlAnsvvoo3H dgGg== X-Gm-Message-State: AOAM532Mgbof0tONjx8Q+Mt1mrPqqVYQ03T1a+qSW2zBCM7MFfoHwXJT AE9K8ubTZM3wP+F1TL5+b/ptTPMYTI0= X-Google-Smtp-Source: ABdhPJxsN3x0gkG8yNgWmo04OGsEAKa4Pbsua8LCXMsZV0L9RDXWcnKBi4fU3vxvPNPr3FmNzFo8RA== X-Received: by 2002:a17:90a:e56:: with SMTP id p22mr7253227pja.73.1627721146311; Sat, 31 Jul 2021 01:45:46 -0700 (PDT) Received: from i9-aorus-gtx1080.localdomain (144.168.56.201.16clouds.com. [144.168.56.201]) by smtp.gmail.com with ESMTPSA id h6sm688355pfv.155.2021.07.31.01.45.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Jul 2021 01:45:46 -0700 (PDT) From: Bin Meng To: Simon Glass , u-boot@lists.denx.de Cc: Bin Meng Subject: [PATCH 4/7] x86: mtrr: Abort if requested size is not power of 2 Date: Sat, 31 Jul 2021 16:45:26 +0800 Message-Id: <20210731084529.7524-5-bmeng.cn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210731084529.7524-1-bmeng.cn@gmail.com> References: <20210731084529.7524-1-bmeng.cn@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean The size parameter of mtrr_add_request() and mtrr_set_next_var() shall be power of 2, otherwise the logic creates a mask that does not meet the requirement of IA32_MTRR_PHYSMASK register. Programming such a mask value to IA32_MTRR_PHYSMASK generates #GP. Signed-off-by: Bin Meng --- arch/x86/cpu/mtrr.c | 7 +++++++ arch/x86/include/asm/mtrr.h | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/x86/cpu/mtrr.c b/arch/x86/cpu/mtrr.c index 14c644eb56..260a008093 100644 --- a/arch/x86/cpu/mtrr.c +++ b/arch/x86/cpu/mtrr.c @@ -26,6 +26,7 @@ #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -179,6 +180,9 @@ int mtrr_add_request(int type, uint64_t start, uint64_t size) if (!gd->arch.has_mtrr) return -ENOSYS; + if (!is_power_of_2(size)) + return -EINVAL; + if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS) return -ENOSPC; req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++]; @@ -223,6 +227,9 @@ int mtrr_set_next_var(uint type, uint64_t start, uint64_t size) { int mtrr; + if (!is_power_of_2(size)) + return -EINVAL; + mtrr = get_free_var_mtrr(); if (mtrr < 0) return mtrr; diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h index 384672e93f..d1aa86bf1d 100644 --- a/arch/x86/include/asm/mtrr.h +++ b/arch/x86/include/asm/mtrr.h @@ -119,7 +119,7 @@ void mtrr_close(struct mtrr_state *state, bool do_caches); * * @type: Requested type (MTRR_TYPE_) * @start: Start address - * @size: Size + * @size: Size, must be power of 2 * * @return: 0 on success, non-zero on failure */ @@ -144,8 +144,9 @@ int mtrr_commit(bool do_caches); * * @type: Requested type (MTRR_TYPE_) * @start: Start address - * @size: Size - * @return 0 on success, -ENOSPC if there are no more MTRRs + * @size: Size, must be power of 2 + * @return 0 on success, -EINVAL if size is not power of 2, + * -ENOSPC if there are no more MTRRs */ int mtrr_set_next_var(uint type, uint64_t base, uint64_t size); -- 2.25.1