Skip to content

Conversation

@mmahadevan108
Copy link
Contributor

  1. Create a static MPU map for code relocated to SRAM
  2. Update the python script to provide proper alignment for the SRAM region
  3. Setup the static MPU regions before PRE_KERNEL_1 and PRE_KERNEL_2 functions are invoked. This will setup the MPU for SRAM regions in case code relocated to SRAM and invoked from any of these functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no different from the RAMFUNC support, at least in the sense of memory attributes. Would it be possible to group these things together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of a clean way to do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make sense if the SRAM region was always the relocation destination, but it's not. ITCM is another possibility for some platforms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood, thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these symbols defined anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They get generated at build time and are placed in build/zephyr/include/generated/linker_relocate.ld

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes we always relocate to the SRAM region, which isn't necessarily true. It's possible to relocate to other regions.

@ioannisg
Copy link
Member

ioannisg commented Apr 7, 2021

I need to have a closer look into this. For now, I could only comment that the refactor you're doing seems to break the support in NXP MPU to start the MPU stuff at PRE KERNEL 2.

Also, for the whole thing to work efficiently, you probably need to have the RAM area for relocation inside the RAM_SECTIONS - is it the case?

@mmahadevan108
Copy link
Contributor Author

RAM_SECTIONS

Relocation is done using zephyr_code_relocate(src/*.c SRAM) function which places the files in SRAM region. scripts/gen_relocate_app.py will generate the linker_relocate.ld and code_relocation.c files at build time.

@mmahadevan108
Copy link
Contributor Author

Rebased

@mmahadevan108
Copy link
Contributor Author

Any comments?

@mmahadevan108
Copy link
Contributor Author

I need to have a closer look into this. For now, I could only comment that the refactor you're doing seems to break the support in NXP MPU to start the MPU stuff at PRE KERNEL 2.

I have ran userspaces tests on the FRDM_K64F board and the tests pass. The tests I ran were

tests/kernel/mem_protect/userspace
samples/userspace/shared_mem
samples/userspace/hello_world_user
samples/userspace/prod_consumer/

@ioannisg
Copy link
Member

I need to have a closer look into this. For now, I could only comment that the refactor you're doing seems to break the support in NXP MPU to start the MPU stuff at PRE KERNEL 2.

I have ran userspaces tests on the FRDM_K64F board and the tests pass. The tests I ran were

tests/kernel/mem_protect/userspace
samples/userspace/shared_mem
samples/userspace/hello_world_user
samples/userspace/prod_consumer/

I am not saying that the tests are breaking. I only commented that there was no option any more for POST KERNEL initialization of the module. :)

@mmahadevan108
Copy link
Contributor Author

I need to have a closer look into this. For now, I could only comment that the refactor you're doing seems to break the support in NXP MPU to start the MPU stuff at PRE KERNEL 2.

I have ran userspaces tests on the FRDM_K64F board and the tests pass. The tests I ran were

tests/kernel/mem_protect/userspace
samples/userspace/shared_mem
samples/userspace/hello_world_user
samples/userspace/prod_consumer/

I am not saying that the tests are breaking. I only commented that there was no option any more for POST KERNEL initialization of the module. :)

The NXP MPU driver was delaying the init to PRE_KERNEL2 for logging purposes. I think this was meant to help debugging during MPU driver init.

@mmahadevan108
Copy link
Contributor Author

Any questions on this PR?

@mmahadevan108 mmahadevan108 added the dev-review To be discussed in dev-review meeting label Apr 20, 2021
@mmahadevan108
Copy link
Contributor Author

Any questions on this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason that this generally works today is that all the "static" regions are located (forced by the linker, actually) in the beginning of SRAM, before the kernel ram (which includes the application memory). This placement is a requirement, currently, for Cortex-M33, because the static memory regions are actually partitioning the background SRAM, because overlapping regions are not allowed. (btw, you cannot currently call configure_static_regions for Cortex-M33 for a region that is not in RAM, unless i am missing something).

In ARMv7-M this is not a requirement, as the MPU static regions do not need to partition the SRAM, and you can configure static regions basically everywhere.

So I am not sure if this solution is generic enough to be accepted.

@MaureenHelm @mmahadevan108 did you try to test this PR on a Cortex-M33 with MPU? Especially if the relocation is not on SRAM. If so, I 'd like to hear how this works.

Besides this, I have no more objections for this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am running this code on a Cortex-M33. The code is relocated to SRAM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but how about relocating to outside SRAM as @MaureenHelm was suggesting? I am not sure this would work right now for CM33, because of the way the mpu driver is expecting the static regions be in SRAM.

So, should we accept this solution, we must add a constraint that this won't work with relocation outside SRAM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We relocate to ITCM instead of SRAM on i.MX RT1064, which is a Cortex-M7. Not an M33. We talked about it a bit in #31132 (comment) when I tried to add an MPU region for ITCM, but we then decided it wasn't necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall it now, thanks.
In my opinion, we should make sure the solution that is presented here works in a generic way. If not, then we need to restrict it on the ARCHes that support it.

Copy link
Contributor Author

@mmahadevan108 mmahadevan108 Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address this issue I have created a new Kconfig called CODE_DATA_RELOCATION_SRAM. The changes made to arm_code_mpu.c file are now using #if defined(CONFIG_CODE_DATA_RELOCATION_SRAM) which allows the MPU entry to be added only when code is relocated to SRAM.

@MaureenHelm MaureenHelm added this to the v2.6.0 milestone Apr 29, 2021
1. This will help us identify if the relocation is to
SRAM which is used when setting up the MPU entry
for the SRAM region where code is relocated
2. Move CODE_DATA_RELOCATION configs to ARM specific
folder

Signed-off-by: Mahesh Mahadevan <[email protected]>
Use the CODE_DATA_RELOCATION_SRAM config to indentify code relocated
to SRAM so we can setup the MPU for the SRAM region used for code
relocation.

Signed-off-by: Mahesh Mahadevan <[email protected]>
Code relocated to SRAM need MPU setup.

Signed-off-by: Mahesh Mahadevan <[email protected]>
Code relocated using CONFIG_CODE_DATA_RELOCATION_SRAM should
be allowed to execute from SRAM

Signed-off-by: Mahesh Mahadevan <[email protected]>
Setup the static MPU regions before PRE_KERNEL_1 and
PRE_KERNEL_2 functions are invoked. This will setup
the MPU for SRAM regions in case code relocated to SRAM
is invoked from any of these functions.

Signed-off-by: Mahesh Mahadevan <[email protected]>
@ioannisg ioannisg self-assigned this May 3, 2021
@mmahadevan108
Copy link
Contributor Author

Apologies to keep asking about this PR, I would like to get this in before the merge window closes. Also a few other open PR's are dependent on this.

/* RAM area for relocated text */
.start = (uint32_t)&__sram_text_start,
.size = (uint32_t)&__sram_text_size,
.attr = K_MEM_PARTITION_P_RX_U_RX,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This partition has exactly the same attribution as the ramfunc section. I wonder if that one could be re-used. Not against, for now, to accept adding one more region. But i really do not see why we need to have 2 static regions with the same attribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ARM ARM (32-bit) Architecture area: Kconfig area: Kernel dev-review To be discussed in dev-review meeting platform: NXP NXP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants