Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance .data relocation / .bss zero initialization with MEMC managed memory #68884

Open
talih0 opened this issue Feb 12, 2024 · 5 comments
Open
Assignees
Labels
area: Memory Management Enhancement Changes/Updates/Additions to existing features

Comments

@talih0
Copy link
Contributor

talih0 commented Feb 12, 2024

.bss zero initialization (via z_bss_zero()) and .data relocation (via z_data_copy()) is done early in the boot up via the function z_prep_c().

In addition to initializing RAM, z_bss_zero() also internally calls
bss_zeroing_relocation();
which is the auto-generated code via CMake helper zephyr_code_relocate().

Thus when relocating data to an external RAM (via MEMC driver), the relocation happens before any driver initialization is done.

My setup is for XMC4700 (the MEMC drivers have not been upstreamed yet). But it should be the same issue of the other SoC with MEMC drivers (i.e. Atmel SAM SMC).

Impact
Invalid data in .data section. And a .bss that's not zero-initialized.

@talih0 talih0 added the bug The issue is a bug, or the PR is fixing a bug label Feb 12, 2024
@talih0 talih0 changed the title .data relocation / .bss zero initialization is done before MEMC is initilized .data relocation / .bss zero initialization is done before MEMC is initialized Feb 12, 2024
@MaureenHelm MaureenHelm added the priority: medium Medium impact/importance bug label Feb 13, 2024
@nashif nashif added priority: low Low impact/importance bug Enhancement Changes/Updates/Additions to existing features and removed priority: medium Medium impact/importance bug bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug labels Feb 20, 2024
@nashif nashif changed the title .data relocation / .bss zero initialization is done before MEMC is initialized Enhance .data relocation / .bss zero initialization with MEMC managed memory Feb 20, 2024
@dcpleung dcpleung removed their assignment Feb 27, 2024
@dcpleung
Copy link
Member

Hm... this is not a memory management issue, but rather an issue related to boot and driver initialization. z_prep_c() is an early boot process to prepare an environment for later initialization steps (including driver initialization). So there is no way for this to work as part of the early boot process if it depends on a driver initializing the needed memory. So the solution would involve initializing the external memory during the early boot process, but before the C environment has already been set up... and this will need some specially crafted code.

TBH, I don't think there can be a generic solution as each architecture has their own z_prep_c(), and it will be hard to cater to all different external memory controllers.

@talih0
Copy link
Contributor Author

talih0 commented Feb 27, 2024 via email

@dcpleung
Copy link
Member

Part of me doubt that would work unless you are very careful on where data is placed. The reason is that continue execution of code may require some data having already relocated. So you will need to make sure any code executed between z_prep_c() and relocation being done do not access the relocated region. Relocated BSS may not be as important unless the code relies on it being zero at first.

@talih0
Copy link
Contributor Author

talih0 commented Feb 27, 2024

Yes, you have to be careful not to reallocate some driver data. It's up to the user to
check that the data they relocate is not used before INIT_LEVEL_PRE_KERNEL_1 level.

My use case is to reallocate some application data or library that is used much later in the boot.

@ehughes
Copy link

ehughes commented Jun 26, 2024

I have a similar request. I have a board that uses the MCXN947 with external PSRAM. I need to be able to do some pin muxing and clock init such that the FlexSPI controller is intialized. This needs to happen before the c startup/copydown routines.

Even having a simple hook or weak function that can be called before C system init would be helpful.

I currently have a prototype that will do the minimal number of calls to get things setup, I just need a way to call this function right before the branch to z_prep_c

Having something like this:

`
#ifdef CONFIG_PRE_C_START_HOOK
bl z_pre_c_startup_hook
#endif

/*
 * 'bl' jumps the furthest of the branch instructions that are
 * supported on all platforms. So it is used when jumping to z_prep_c
 * (even though we do not intend to return).
 */
bl z_prep_c 

`

would be good enough for a 1st implementation. This would allow me to add some special code in my board files without needed to modify reset.S

I could prepare a PR with some guidance as to possible alternatives and have the least impact on existing implementations.

The only other option I can think of is to have some sort of dual image setup (like MCU_BOOT) that is a boot stub that will get the PSRAM setup, then jump to the Zephyr application. i.e. simulate something like the bootrom in the i.MXRT with the DCD capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Memory Management Enhancement Changes/Updates/Additions to existing features
Projects
None yet
Development

No branches or pull requests

6 participants