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

armv8-m: making full SRAM run-time partitioning (MPU gap filling) optional #20152

Conversation

ioannisg
Copy link
Member

@ioannisg ioannisg commented Oct 25, 2019

Addresses #19067 for ARM.

It should be working atm.
I would like a review on the Kconfig symbol name I've used, and the default behavior.
I am also going to add a test of the feature,

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch 2 times, most recently from ca2702c to e945d37 Compare October 25, 2019 13:54
@ioannisg ioannisg changed the title Arch arm mpu optional non overal config armv8-m: making full SRAM run-time partitioning (MPU gap filling) optional Oct 25, 2019
@zephyrbot zephyrbot added the area: ARM ARM (32-bit) Architecture label Oct 25, 2019
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from e945d37 to efcf04f Compare October 28, 2019 05:57
@zephyrbot zephyrbot added area: Tests Issues related to a particular existing or missing test area: Kernel labels Oct 28, 2019
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from efcf04f to cf25a00 Compare October 28, 2019 16:08
@ioannisg ioannisg added this to the v2.1.0 milestone Oct 29, 2019
@andrewboie
Copy link
Contributor

I think it should be off by default -- an extra feature that can be enabled if users want it. It's not required for user mode at any rate.

As far as the naming of the Kconfig, MPU_FULL_BACKGROUND_SRAM_PARTITIONING is rather long winded, but sure, no complaints if you want to call it that.

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch 2 times, most recently from ad9a7a3 to e903d14 Compare October 29, 2019 15:34
@ioannisg ioannisg requested a review from ruuddw October 29, 2019 15:37
@zephyrbot
Copy link
Collaborator

zephyrbot commented Oct 29, 2019

All checks are passing now.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from e903d14 to 05671f0 Compare October 29, 2019 15:47
@wentongwu wentongwu self-requested a review October 30, 2019 08:35
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from 05671f0 to 29b5260 Compare November 1, 2019 09:58
@ioannisg
Copy link
Member Author

ioannisg commented Nov 1, 2019

I want to have this in for Zephyr v2.1 release; is it possible to have additional reviewing (besides @andrewboie ) on the introduced Kconfig option at least?

@ioannisg ioannisg closed this Nov 4, 2019
@ulfalizer
Copy link
Collaborator

ulfalizer commented Nov 4, 2019

Note that the direct (depends on) dependencies of symbols that are defined in multiple locations get ORed together, so there's no way to add extra dependencies by defining a symbol more than once.

The dependencies of MPU_SRAM_GAP_FILLING end up as this (copied from the menuconfig):

Direct dependencies (=n):
     USERSPACE(=n) && ARM_MPU(=n) && CPU_HAS_MPU(=y) && ARM(=y)  (=n)
  || MPU_REQUIRES_NON_OVERLAPPING_REGIONS(=n)

If MPU_REQUIRES_NON_OVERLAPPING_REGIONS is y, it will always be possible to enable MPU_SRAM_GAP_FILLING. Is that okay here?

A tip is to go into the menuconfig and jump to each of the definition locations of the symbol. That might help with figuring out how it works. Basically, the definition in arch/Kconfig will always be visible as long as MPU_REQUIRES_NON_OVERLAPPING_REGIONS is y, allowing the symbol to be toggled there.

@ioannisg
Copy link
Member Author

ioannisg commented Nov 4, 2019

Note that the direct (depends on) dependencies of symbols that are defined in multiple locations get ORed together, so there's no way to add extra dependencies by defining a symbol more than once.

The dependencies of MPU_SRAM_GAP_FILLING end up as this (copied frrom the menuconfig):

Direct dependencies (=n):
     USERSPACE(=n) && ARM_MPU(=n) && CPU_HAS_MPU(=y) && ARM(=y)  (=n)
  || MPU_REQUIRES_NON_OVERLAPPING_REGIONS(=n)

If MPU_REQUIRES_NON_OVERLAPPING_REGIONS is y, it will always be possible to enable MPU_SRAM_GAP_FILLING. Is that okay here?

A tip is to go into the menuconfig and jumping to each of the definition locations of the symbol. That might help with figuring out how it works. Basically, the definition in arch/Kconfig will always be visible as long as MPU_REQUIRES_NON_OVERLAPPING_REGIONS is y, allowing the symbol to be toggled there.

Thanks so much @ulfalizer for this feedback.

If MPU_REQUIRES_NON_OVERLAPPING_REGIONS is y, it will always be possible to enable MPU_SRAM_GAP_FILLING. Is that okay here?

Not exactly. I need MPU_SRAM_GAP_FILLING to have a direct dependency on MPU_REQUIRES_NON_OVERLAPPING_REGIONS, but I want it to be defined only if USERSPACE is also defined. I had the impression that dependencies are ANDed instead of ORed. So what's your proposal?

@@ -62,6 +62,31 @@ config MPU_ALLOW_FLASH_WRITE
help
Enable this to allow MPU RWX access to flash memory

config MPU_SRAM_GAP_FILLING
Copy link
Member Author

Choose a reason for hiding this comment

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

@ulfalizer can we put the definition into if USERSPACE block, to make it defined only if USERSPACE is also defined? Then, what will happen with the dependency on MPU_REQUIRES_NON_OVERLAPPING_REGIONS?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Note that if FOO is just a shorthand for adding depends on FOO to the symbol(s) within it. if and depends on are exactly the same thing. I think this is something that confuses people re. Kconfig, where people assume if does more than that. :)

If a symbol is defined in multiple locations and should depend on USERSPACE, then all definition locations need to have depends on USERSPACE (or be within an if USERSPACE -- same thing), since the dependencies get ORed together.

Personally, I'd try to refactor it so that there's just a single definition in arch/Kconfig, maybe along with some helper symbols.

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, I'd try to refactor it so that there's just a single definition in arch/Kconfig, maybe along with some helper symbols.

That's hard; I raised the definition up in arch/Kconfig, as it is essentially a cross-arch option, but I want to keep re-defining it in arch/arm/cortex-m/mpu/Kconfig , so I can have the additional help text that is ARMv8-M specific. But it is becoming clear from your comments that dependencies are always ORed so I cannot add additional dependencies here (so I need to move depends on USERSPACE up in the top-level definition, correct?)

So how can I do it in a way it satisfies you @ulfalizer ? :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

These two are 100% identical:

if FOO
if BAR

config FOO
	bool "foo"
	depends on BAZ

endif
endif
config FOO
	bool "foo"
	depends on FOO && BAR && BAZ

if does not "cancel" the definition, but only modified the dependencies, like depends on does. When a symbol is defined more than once, the dependencies get ||'d together.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A tip is to play around with this in menuconfig btw, because it'll make it clear how multiple definitions work:

config DEP1
	bool "dep1"

config DEP2
	bool "dep2"

config MULTIDEF
	bool "first multidef prompt"
	depends on DEP1

config MULTIDEF
	bool "second multidef prompt"
	depends on DEP2

Copy link
Member Author

Choose a reason for hiding this comment

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

The point is that : only the ARCH-specific option will be used in the code. the top-arch option will only be used in Kconfig, to enable the ARM-specific option.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Was thinking from an interface design perspective. There'll be a button you toggle that says "MPU SRAM gap filling" or the like, but it won't do anything unless you also toggle "User mode threads".

Feels like it gets lost sometimes that Kconfig defines a configuration interface. :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

As long as that wouldn't be weird to users, it'll work at least.

Copy link
Collaborator

@ulfalizer ulfalizer Nov 4, 2019

Choose a reason for hiding this comment

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

My preferred solution would be either

  1. A single definition in arch/Kconfig, with some arch-specific notes in the help text, and maybe some helper symbols to adjust it for different arch capabilities

  2. Have multiple arch-specific definitions (wouldn't necessarily need to have the same name either), and skip the definition in arch/Kconfig

I won't impose anything though. Just keep the multiple-definition dependency gotchas in mind.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ulfalizer could you come back to this, briefy?
FYI, I integrated your suggestion of _not_duplicating the definition of the Kconfig option - it is really cleaner. So, I introduced an internal hidden ARM-only option that takes the configuration enforced by the user. The internal option is used in source code.

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from 29b5260 to 40942a0 Compare November 5, 2019 14:49
@ioannisg ioannisg requested a review from vonhust November 5, 2019 15:07
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch 4 times, most recently from 584e31c to 99247b1 Compare November 6, 2019 08:22
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from 99247b1 to 31ecc5d Compare November 6, 2019 11:05
@zephyrbot zephyrbot added the area: Sanitycheck Sanitycheck has been renamed to Twister label Nov 6, 2019
@ulfalizer
Copy link
Collaborator

@ulfalizer could you come back to this, briefy?
FYI, I integrated your suggestion of _not_duplicating the definition of the Kconfig option - it is really cleaner. So, I introduced an internal hidden ARM-only option that takes the configuration enforced by the user. The internal option is used in source code.

Personally, I think I'd either have a single shared symbol in arch/Kconfig with some platform-specific notes in the help text, or separate symbols per arch/<platform>/mpu/Kconfig (maybe along with factoring out common ones into some Kconfig.*_mpu files or the like).

The main motivation is to avoid complicated multiple definition stuff where not many people know how it works (and that's easy to get wrong re. dependencies).

I won't block anything here though, so go with whatever you prefer. :)

Please also check how things look in the menuconfig. With the current approach, I suspect the help text for ARM_MPU_SRAM_GAP_FILLING might be hard to notice, because it's not what you get when you press ? on the configurable symbol in the menuconfig.

Copy link
Member

@aescolar aescolar left a comment

Choose a reason for hiding this comment

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

@ioannisg note that you still have in this branch the sanitycheck hotfix which is already in master (I do not understand why github is not complaining about the conflict and let's one press the button..) Please disregard this review on your own when you fix it (no need to ping me back)

@ioannisg
Copy link
Member Author

ioannisg commented Nov 7, 2019

@ioannisg note that you still have in this branch the sanitycheck hotfix which is already in master (I do not understand why github is not complaining about the conflict and let's one press the button..) Please disregard this review on your own when you fix it (no need to ping me back)

The commit is present because I did not rebase.
There is no conflict because the commit in this PR is identical to what was merged (If I understand GH properly)

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from 31ecc5d to 4c70ad7 Compare November 7, 2019 15:29
@ioannisg ioannisg dismissed aescolar’s stale review November 7, 2019 15:30

not applicable :)

@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch 2 times, most recently from a81becb to 833dfeb Compare November 7, 2019 15:38
@ioannisg
Copy link
Member Author

ioannisg commented Nov 7, 2019

@ulfalizer could you come back to this, briefy?
FYI, I integrated your suggestion of _not_duplicating the definition of the Kconfig option - it is really cleaner. So, I introduced an internal hidden ARM-only option that takes the configuration enforced by the user. The internal option is used in source code.

Personally, I think I'd either have a single shared symbol in arch/Kconfig with some platform-specific notes in the help text, or separate symbols per arch/<platform>/mpu/Kconfig (maybe along with factoring out common ones into some Kconfig.*_mpu files or the like).

The main motivation is to avoid complicated multiple definition stuff where not many people know how it works (and that's easy to get wrong re. dependencies).

I won't block anything here though, so go with whatever you prefer. :)

Please also check how things look in the menuconfig. With the current approach, I suspect the help text for ARM_MPU_SRAM_GAP_FILLING might be hard to notice, because it's not what you get when you press ? on the configurable symbol in the menuconfig.

@ulfalizer I finally went along with your preference here, i.e. removed the internal option, and added the ARMv8-M specific explanatory text in the config ARM_MPU option.

We introduce MPU_GAP_FILLING Kconfig option that instructs
the MPU driver to enforce a full SRAM partitioning, when it
programs the dynamic MPU regions (user thread stack, PRIV stack
guard and application memory domains) at context-switch. We
allow this to be configurable, in order to increase the number
of MPU regions available for application memory domain programming.

This option is introduced in arch/Kconfig, as it is expected
to serve as a cross-ARCH symbol. The option can be set by the
user during build configuration.

By not enforcing full partition, we may leave part of kernel
SRAM area covered only by the default ARM memory map. This
is fine for User Mode, since the background ARM map does not
allow nPRIV access at all. The difference is that kernel code
will be able to attempt fetching instructions from kernel SRAM
area without this leading directly to a MemManage exception.

Since this does not compromize User Mode, we make the skipping
of full partitioning the default behavior for the ARMv8-M MPU
driver. The application developer may be able to overwrite this.

In the wake of this change we update the macro definitions in
arm_core_mpu_dev.h that derive the maximum number of MPU regions
for application memory domains.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
This commit moves the function mpu_configure_regions(.) from
arm_mpu_v7_internal.h to arm_mpu.c. The function is to be used
by the both ARMv7-M MPU driver, as well as the ARMv8-M MPU
driver (when it behaves like the ARMv7-M driver).

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
We allow the run-time, full paritioning of the SRAM space by the
ARMv8-M MPU driver to be an optional feature.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
We add a new test-case for the mem_protect and userspace tests,
to test the ARMv8-M MPU driver without the skipping of full SRAM
partitioning (i.e. gap filling).

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
@ioannisg ioannisg force-pushed the arch_arm_mpu_optional_non_overal_config branch from 833dfeb to 17e251f Compare November 7, 2019 16:14
@andrewboie andrewboie merged commit 40fbff6 into zephyrproject-rtos:master Nov 7, 2019
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: Kernel area: Memory Protection area: Sanitycheck Sanitycheck has been renamed to Twister area: Tests Issues related to a particular existing or missing test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants