All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bin Meng <bmeng.cn@gmail.com>
To: Simon Glass <sjg@chromium.org>, u-boot@lists.denx.de
Cc: Bin Meng <bmeng.cn@gmail.com>
Subject: [PATCH 4/7] x86: mtrr: Abort if requested size is not power of 2
Date: Sat, 31 Jul 2021 16:45:26 +0800	[thread overview]
Message-ID: <20210731084529.7524-5-bmeng.cn@gmail.com> (raw)
In-Reply-To: <20210731084529.7524-1-bmeng.cn@gmail.com>

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 <bmeng.cn@gmail.com>
---

 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 <asm/mp.h>
 #include <asm/msr.h>
 #include <asm/mtrr.h>
+#include <linux/log2.h>
 
 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


  parent reply	other threads:[~2021-07-31  8:46 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-31  8:45 [PATCH 0/7] x86: Various fixes to MTRR and FSP codes Bin Meng
2021-07-31  8:45 ` [PATCH 1/7] x86: fsp: Don't program MTRR for DRAM Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:53     ` Bin Meng
2021-07-31  8:45 ` [PATCH 2/7] x86: mtrr: Do not clear the unused ones in mtrr_commit() Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:50     ` Bin Meng
2021-07-31  8:45 ` [PATCH 3/7] x86: mtrr: Skip MSRs that were already programmed " Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:50     ` Bin Meng
2021-07-31  8:45 ` Bin Meng [this message]
2021-08-01 19:19   ` [PATCH 4/7] x86: mtrr: Abort if requested size is not power of 2 Simon Glass
2021-08-02  2:50     ` Bin Meng
2021-07-31  8:45 ` [PATCH 5/7] x86: cmd: hob: Fix the command usage and help messages Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:50     ` Bin Meng
2021-07-31  8:45 ` [PATCH 6/7] x86: cmd: hob: Fix display of resource type for system memory Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:50     ` Bin Meng
2021-07-31  8:45 ` [PATCH 7/7] x86: fsp: Only FSP2 has INIT_PHASE_END_FIRMWARE Bin Meng
2021-08-01 19:19   ` Simon Glass
2021-08-02  2:53     ` Bin Meng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210731084529.7524-5-bmeng.cn@gmail.com \
    --to=bmeng.cn@gmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.