public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: u-boot@lists.denx.de, "Marek Behún" <kabel@kernel.org>,
	"Peng Fan" <peng.fan@oss.nxp.com>,
	"Tom Rini" <trini@konsulko.com>
Subject: [PATCH 2/9] common/memsize.c: Fix get_effective_memsize() to check for overflow
Date: Fri,  9 Sep 2022 17:32:39 +0200	[thread overview]
Message-ID: <20220909153246.8455-3-pali@kernel.org> (raw)
In-Reply-To: <20220909153246.8455-1-pali@kernel.org>

Ensure that top of RAM can be represented by phys_size_t type. If RAM is
too large or RAM base address is too upper then limit RAM size to prevent
address space overflow.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 common/memsize.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/common/memsize.c b/common/memsize.c
index 31884acca0df..d11ed67632fb 100644
--- a/common/memsize.c
+++ b/common/memsize.c
@@ -94,11 +94,23 @@ long get_ram_size(long *base, long maxsize)
 
 phys_size_t __weak get_effective_memsize(void)
 {
+	phys_size_t ram_size = gd->ram_size;
+
+	/*
+	 * Check for overflow and limit ram size to some representable value.
+	 * It is required that ram_base + ram_size must be representable by
+	 * phys_size_t type and must be aligned by direct access, therefore
+	 * calculate it from last 4kB sector which should work as alignment
+	 * on any platform.
+	 */
+	if (gd->ram_base + ram_size < gd->ram_base)
+		ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base;
+
 #ifndef CONFIG_MAX_MEM_MAPPED
-	return gd->ram_size;
+	return ram_size;
 #else
 	/* limit stack to what we can reasonable map */
-	return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
-		CONFIG_MAX_MEM_MAPPED : gd->ram_size);
+	return ((ram_size > CONFIG_MAX_MEM_MAPPED) ?
+		CONFIG_MAX_MEM_MAPPED : ram_size);
 #endif
 }
-- 
2.20.1


  parent reply	other threads:[~2022-09-09 15:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-09 15:32 [PATCH 0/9] Support for 4 GB DDR modules for 32-bit U-Boot builds Pali Rohár
2022-09-09 15:32 ` [PATCH 1/9] common/memsize.c: Fix get_effective_memsize() to always check for CONFIG_MAX_MEM_MAPPED Pali Rohár
2022-09-23 22:48   ` Tom Rini
2022-09-09 15:32 ` Pali Rohár [this message]
2022-09-09 15:32 ` [PATCH 3/9] board_f: Fix types for board_get_usable_ram_top() Pali Rohár
2022-09-09 18:21   ` Simon Glass
2022-09-09 15:32 ` [PATCH 4/9] board_f: Fix printing gd->ram_size and gd->ram_top Pali Rohár
2022-09-09 18:20   ` Simon Glass
2022-09-09 21:46     ` Pali Rohár
2022-09-12 13:33       ` Simon Glass
2022-09-09 15:32 ` [PATCH 5/9] ddr: fsl: Fix checking for maximal mappable memory Pali Rohár
2022-09-09 15:32 ` [PATCH 6/9] ddr: fsl: Fix fsl_ddr_sdram_size() for 4GB modules with 32-bit phys_size_t Pali Rohár
2022-09-09 15:32 ` [PATCH 7/9] ddr: fsl: Allow to detect 4 GB DDR modules in 32-bit mode Pali Rohár
2022-09-09 15:32 ` [PATCH 8/9] ddr: fsl: Fix re-align of verbose DRAM information for non-SPL builds Pali Rohár
2022-09-09 18:21   ` Simon Glass
2022-09-09 19:08     ` Tom Rini
2022-09-09 19:39       ` Simon Glass
2022-09-09 21:05         ` Tom Rini
2022-09-09 15:32 ` [PATCH 9/9] powerpc/mpc85xx: Explain TLB unmapped memory message Pali Rohár
2022-09-11  9:29 ` [PATCH 10/9] powerpc/mpc85xx: Fix re-align of unmapped DDR memory message for non-SPL builds Pali Rohár

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=20220909153246.8455-3-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=kabel@kernel.org \
    --cc=peng.fan@oss.nxp.com \
    --cc=trini@konsulko.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox