mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Rouven Czerwinski <r.czerwinski@pengutronix.de>,
	barebox@lists.infradead.org
Subject: Re: [PATCH 3/8] ARM: mmu: use reserve mem entries to modify maps
Date: Tue, 20 Apr 2021 11:09:35 +0200	[thread overview]
Message-ID: <ade2db02-9ced-a831-81f6-9b2d391973d6@pengutronix.de> (raw)
In-Reply-To: <20210420075700.246124-3-r.czerwinski@pengutronix.de>

On 20.04.21 09:56, Rouven Czerwinski wrote:
> Use the information from the reserved memory entries to modify the
> mapping of memory regions to mark them as uncachable and not-executable.
> This also prevents the processor from speculating into these regions,
> preventing hard to debug scenarios where boots fail for unknown reasons.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/mmu.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 58 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
> index 6af228505d..71d6cad1ef 100644
> --- a/arch/arm/cpu/mmu.c
> +++ b/arch/arm/cpu/mmu.c
> @@ -17,6 +17,7 @@
>  #include <memory.h>
>  #include <asm/system_info.h>
>  #include <asm/sections.h>
> +#include <of.h>
>  
>  #include "mmu.h"
>  
> @@ -387,6 +388,57 @@ static void vectors_init(void)
>  	create_vector_table(ARM_LOW_VECTORS);
>  }
>  
> +static void create_sections_with_intersect(struct memory_bank *bank)
> +{
> +	int i;
> +	unsigned flag;

u32 pmd_flags fits the intent better.

> +	struct of_reserve_map *res_map = of_get_reserve_map();

Drop this call. It's overwritten later.

> +	unsigned long j_end;
> +	unsigned long end;
> +	unsigned long j;
> +
> +	res_map = of_get_reserve_map();
> +	if (!res_map)

Check unneeded, you never enter here if !of_get_reserve_map() anyway.

> +		return;
> +
> +	end = bank->start + bank->size - 1;
> +	j = bank->start;
> +
> +	while(j < end) {

Missing space after while. Same for if() a few lines down.
IMO a for loop would be clearer here.

> +		flag = PMD_SECT_DEF_CACHED;
> +		j_end = j + PGDIR_SIZE - 1;
> +
> +		for (i = 0; i < res_map->num_entries; i++) {
> +			if(BIT(i) & res_map->xn && (j_end >= res_map->start[i] &&

Always wrap bitwise comparisons in parenthesis. Doesn't matter for code
correctness here however. The parens for the && stuff can be dropped.

> +						    j_end <= res_map->end[i]))

What about a section that starts in a reserved region, but doesn't end in one?
This wouldn't be considered here, would it?

> +				flag = PMD_SECT_DEF_UNCACHED | PMD_SECT_XN;
> +		}
> +
> +		create_sections(ttb, j, j_end, flag);
> +		j += PGDIR_SIZE;
> +	}
> +}
> +
> +static bool intersects_reserved_map_xn(struct memory_bank *bank)
> +{
> +	struct of_reserve_map *res_map;
> +	unsigned long bank_end;
> +	int i;
> +
> +	res_map = of_get_reserve_map();
> +
> +	if (!res_map)

Also early-exit if !res_map->xn.

> +		return false;
> +
> +	bank_end = bank->start + bank->size;

band_end is one past the end here...

> +	for (i = 0; i < res_map->num_entries; i++) {
> +		if(res_map->start[i] >= bank->start || res_map->end[i] <= bank_end)

So this should read < bank_end.

> +			return true;
> +	}
> +	return false;
> +}
> +
> +
>  /*
>   * Prepare MMU for usage enable it.
>   */
> @@ -448,8 +500,12 @@ void __mmu_init(bool mmu_on)
>  	vectors_init();
>  
>  	for_each_memory_bank(bank) {
> -		create_sections(ttb, bank->start, bank->start + bank->size - 1,
> -				PMD_SECT_DEF_CACHED);
> +		if (intersects_reserved_map_xn(bank)) {

I think it would be cleaner to remove this check here and just have one section
creation function. You can check whether there are any XN sections and if any
XN sections are intersecting at the start and then just have a flag you check
in the loop to skip the section search


> +			create_sections_with_intersect(bank);
> +		} else {
> +			create_sections(ttb, bank->start, bank->start + bank->size - 1,
> +					PMD_SECT_DEF_CACHED);
> +		}
>  		__mmu_cache_flush();
>  	}
>  
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  reply	other threads:[~2021-04-20  9:12 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-20  7:56 [PATCH 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
2021-04-20  7:56 ` [PATCH 2/8] of: export of_get_reserve_map Rouven Czerwinski
2021-04-20  8:47   ` Ahmad Fatoum
2021-04-20  7:56 ` [PATCH 3/8] ARM: mmu: use reserve mem entries to modify maps Rouven Czerwinski
2021-04-20  9:09   ` Ahmad Fatoum [this message]
2021-04-20  7:56 ` [PATCH 4/8] of: add flag to not create resmem DT entries Rouven Czerwinski
2021-04-20  9:22   ` Ahmad Fatoum
2021-04-20  7:56 ` [PATCH 5/8] of: add reserved_mem_read initcall Rouven Czerwinski
2021-04-20  9:23   ` Ahmad Fatoum
2021-04-20 10:36   ` Ahmad Fatoum
2021-04-20  7:56 ` [PATCH 6/8] pbl: fdt: add support to parse reserved mem Rouven Czerwinski
2021-04-20 10:29   ` Ahmad Fatoum
2021-04-20  7:57 ` [PATCH 7/8] ARM: mmu-early: map no-map entries XN & uncached Rouven Czerwinski
2021-04-20  9:00   ` Lucas Stach
2021-04-20  7:57 ` [PATCH 8/8] PBL: enable LIBFDT for OP-TEE early loading Rouven Czerwinski
2021-04-20 10:33   ` Ahmad Fatoum
2021-04-20  8:45 ` [PATCH 1/8] of: reserve: add xn flag mem entries Ahmad Fatoum

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=ade2db02-9ced-a831-81f6-9b2d391973d6@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=r.czerwinski@pengutronix.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