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

scripts: align sram .text section when userspace and code reloation enabled #17042

Closed

Conversation

wentongwu
Copy link
Contributor

@wentongwu wentongwu commented Jun 25, 2019

1) Code relocation causes comipler randomly generates _stub that will
cause different memory layout in the magic building process for user
mode. And the different memory layout will generate wrong hash table
in priv_stacks_hash.c and it will cause user mode doesn't work.
Align sram .text section when userspace and code relocation enabled
to generate same .rodata, .data and .bss sections in sram region.
User can adjust Kconfig RELOCATION_SRAM_TEXT_ALIGN_SIZE to make less
wasting memory.

2) make mpu align inside sections instead of outside to
avoid overlap.

3) fix spell typo in target_relocation.cmake.

Fixes: #17038.

@zephyrbot
Copy link
Collaborator

zephyrbot commented Jun 25, 2019

All checks are passing now.

Review history of this comment for details about previous failed status.
Note that some checks might have not completed yet.

@wentongwu
Copy link
Contributor Author

@andrewboie @ioannisg @nashif could you please take a look this change?Thanks

@andrewboie
Copy link
Contributor

I think I understand this patch, can you describe in a bit more detail how the memory map is shifting before this fix was applied?

@wentongwu
Copy link
Contributor Author

I think I understand this patch, can you describe in a bit more detail how the memory map is shifting before this fix was applied?

@andrewboie please see issue #17038 .

@wentongwu
Copy link
Contributor Author

push again to solve conflict with master.

@andrewboie
Copy link
Contributor

OK, thanks for the further details.
If I understand correctly, the issue is that the ".text.sedi_pm_pmu2nvic_isr.__stub" gets added to the text section in zephyr.elf, but not zephyr_prebuilt.elf. Something is causing this to be pulled into only the final binary. At this time it is not known what is causing this to happen. This patch is a workaround to reserve some room in the prebuilt binary for text area expansion, so that the memory addresses don't change in between the two builds.

@wentongwu
Copy link
Contributor Author

shippable fail not related to my change

  1. up_squared/samples/boards/up_squared/gpio_counter/sample.board.up_squared.gpio_counter failure: build_error
  2. qemu_xtensa/tests/kernel/sched/preempt/kernel.sched.preempt failure: timeout
  3. up_squared/samples/synchronization/sample.synchronization failure: build_error

Re-submit.

@wentongwu
Copy link
Contributor Author

wentongwu commented Jun 27, 2019

OK, thanks for the further details.
If I understand correctly, the issue is that the ".text.sedi_pm_pmu2nvic_isr.__stub" gets added to the text section in zephyr.elf, but not zephyr_prebuilt.elf. Something is causing this to be pulled into only the final binary. At this time it is not known what is causing this to happen. This patch is a workaround to reserve some room in the prebuilt binary for text area expansion, so that the memory addresses don't change in between the two builds.

@andrewboie yes, that's workaround for it so far.
could you please take a look this PR again, there is no code change for the re-submit, just to pass the shippable. Thanks a lot.

@andrewboie
Copy link
Contributor

andrewboie commented Jun 27, 2019

What steps have you taken to isolate why the extra data in the text section is being generated for zephyr.elf and not zephyr_prebuilt.elf? Where are these symbols coming from? Are they somehow being pulled in due to the gperf code we add in this phase?

@andrewboie andrewboie requested a review from dcpleung June 27, 2019 22:46
default 80000
depends on ARCH_HAS_USERSPACE && CODE_DATA_RELOCATION
help
Align sram text section when userspace and code relocation
Copy link
Contributor

Choose a reason for hiding this comment

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

This help text really doesn't explain what is going on. You need to expand on this.

help
Align sram text section when userspace and code relocation
enabled to generate same .rodata, .data and .bss sections
in sram region for the magic building process.
Copy link
Contributor

Choose a reason for hiding this comment

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

"magic building process" is NOT a technical term

@@ -82,6 +82,15 @@ config CODE_DATA_RELOCATION
files should be specified in the CMakeList.txt file with
a cmake API zephyr_code_relocation().

config RELOCATION_SRAM_TEXT_ALIGN_SIZE
int "code relocation sram text section align size"
default 80000
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this mean? We are by default reserving 80 Kilobytes of space? Why so much, given that the offset in the binary was only 8 bytes? The help text needs to clearly explain the semantics on the value we set here.

@@ -3,28 +3,31 @@
# See root CMakeLists.txt for description and expectations of these macros

macro(toolchain_ld_relocation)
set(MEM_RELOCATAION_LD "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld")
set(MEM_RELOCATAION_SRAM_DATA_LD
set(MEM_RELOCATION_LD "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld")
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you make some whitespace changes or something? I can't tell the difference between the before and after, it looks like the code is unchanged.

Copy link
Contributor

Choose a reason for hiding this comment

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

Disregard, I see it now

@wentongwu
Copy link
Contributor Author

What steps have you taken to isolate why the extra data in the text section is being generated for zephyr.elf and not zephyr_prebuilt.elf? Where are these symbols coming from? Are they somehow being pulled in due to the gperf code we add in this phase?

@andrewboie @jocelyn-li @nashif One method I used is to put *stub to one separate section and do small align, but it doesn't work. Another idea is trying to put the .text section behind .data section, so the extra code will not impact .data section, I didn't do this one in afraid of broken other boards. And finally it's the workaround method, it works well, but now I know it doesn't fit the opensource spirit, I'm sorry for that.

Look at it again today, I find the extra stubs are comprised of veneer functions, that's because the distance between calling code and the called function is too far(because of relocation, also there are other reasons to cause the far distance), the linker will automatically generate and insert veneer functions. Now I'm trying to figure out the solution. And now I want to close this PR.

@wentongwu wentongwu closed this Jun 28, 2019
Code relocation causes comipler randomly generates _stub that will
cause different memory layout in the magic building process for user
mode. And the different memory layout will generate wrong hash table
in priv_stacks_hash.c and it will cause user mode doesn't work.

Align sram .text section when userspace and code relocation enabled
to generate same .rodata, .data and .bss sections in sram region.
User can adjust Kconfig RELOCATION_SRAM_TEXT_ALIGN_SIZE to make less
wasting memory.

Fixes: zephyrproject-rtos#17038.

Signed-off-by: Wentong Wu <[email protected]>
make mpu align inside sections instead of outside to
avoid overlap.

Signed-off-by: Wentong Wu <[email protected]>
fix spell typo in target_relocation.cmake.

Signed-off-by: Wentong Wu <[email protected]>
@wentongwu wentongwu deleted the non_xip_relocate branch September 20, 2019 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

code relocation generating different memory layout cause user mode not working
3 participants