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

MPU API #8335

Closed
wants to merge 22 commits into from
Closed

MPU API #8335

wants to merge 22 commits into from

Conversation

c1728p9
Copy link
Contributor

@c1728p9 c1728p9 commented Oct 6, 2018

Description

Add a minimalistic HAL MPU API with the ability to prevent execution in ram. Enable this by default.

This is just a preliminary patchset intended for testing and feedback.

Pull request type

[ ] Fix
[ ] Refactor
[ ] Target update
[x] Functionality change
[ ] Breaking change

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 6, 2018

TESTS/mbed_hal/mpu/main.cpp Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
hal/mbed_mpu_api.c Outdated Show resolved Hide resolved
core_util_critical_section_enter();
if (mem_xn_lock == USHRT_MAX) {
core_util_critical_section_exit();
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), "Memory execute never lock overflow (> USHRT_MAX)", mem_xn_lock);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the module be MBED_MODULE_PLATFORM or MBED_MODULE_HAL? Since the code is under platform should that be MBED_MODULE_PLATFORM.

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 changed this to MBED_MODULE_PLATFORM since it is under the platform directory.

@c1728p9 c1728p9 requested a review from flit October 10, 2018 20:43
@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 10, 2018

Made the following changes:

  • Rebased to master to pull in fixes
  • Added cache settings which match the default for all MPU regions
  • Hardcoded hardfault IRQn since this differs on some targets
  • Turned off the MPU for devices not part of the ARMv6m and ARMv7m architecture

@c1728p9 c1728p9 changed the title [RFC] MPU API MPU API and design doc Oct 18, 2018
@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 18, 2018

Added a design doc and title of this PR. I also removed Need work since this can be merged once everyone is happy with it.

@kjbracey
Copy link
Contributor

At the minute, because mbed OS doesn't touch the MPU, targets can and do use it to configure particular requirements about, for example, marking SRAM regions for Ethernet or similar buffers Shareable/Non-cacheable.

I think you may need some sort of hook to allow that, or maybe just reserve some region numbers for target use - eg don't touch regions 4 and up.

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 19, 2018

@kjbracey-arm the default implementation here is for the ARMv7m MPU which I don't see being used in any of the targets. I know some Kinetis devices turn off their MPU for certain operations but Kinetis devices have a custom MPU, so this change won't effect them. Which targets did you see changing MPU settings? Are any targets with an Arm MPU using it?

@cmonr
Copy link
Contributor

cmonr commented Oct 19, 2018

@SenRamakri @ARMmbed/mbed-os-hal @bulislaw @flit @dreemkiller @donatieng @deepikabhavnani @TacoGrandeTX
A reminder that this still in need of a review

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 20, 2018

I rebased this to master and added a commit to disable the MPU when doing flash programming.

Copy link
Member

@bulislaw bulislaw left a comment

Choose a reason for hiding this comment

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

+1 for the design docs. Please make sure docs are in place both for the platform and porting guide sides.

- System integration so the execution from RAM is disabled by default
- Initial porting for the Arm MPU

The goal of this is to increase security for a large number of devices by disabling execution from ram it is not required.
Copy link
Member

Choose a reason for hiding this comment

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

disabling execution from ram it is not required. what's not required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, this should be disabling execution from ram when it is not required

}

/*
* ARMv6m and ARMv7m memory map:
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to allow platforms override default memory map on hal level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may be required if there is a target with non-standard memory, but for now there isn't a good reason to.

Copy link

@deepikabhavnani deepikabhavnani left a comment

Choose a reason for hiding this comment

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

We discussed one fix https://github.com/ARMmbed/mbed-os/pull/8335/files#diff-891ef2631c17d62d627a2598292b2ebeR81, which is needed, Apart from that looks good. Thanks 👍

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 29, 2018

Added fixes, an RAII XN API and rebased on master.

Copy link
Contributor

@donatieng donatieng left a comment

Choose a reason for hiding this comment

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

Great stuff @c1728p9
@ARMmbed/mbed-os-maintainers please hold CI for now, v8M support is coming
@dreemkiller would you be able to check this?

@cmonr
Copy link
Contributor

cmonr commented Nov 26, 2018

Restarting CI

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 26, 2018

I restarted travis (already second time, having networking issues, not related)

@donatieng
Copy link
Contributor

Third time's the charm 🤔 - restarted again

@donatieng
Copy link
Contributor

😭

@cmonr
Copy link
Contributor

cmonr commented Nov 26, 2018

@donatieng Somehow, it looks like Travis is searching for the head branch in ARMmbed's repo instead of @c1728p9' fork. Looking into what we can do for now.

Might end up either reopening the PR or cloning the contents of this PR into ARMmbed:mpu to allow the PR to progress.

@donatieng
Copy link
Contributor

@cmonr Yes just noticed the same:

git clone --depth=50 --branch=mpu https://github.com/ARMmbed/mbed-os.git ARMmbed/mbed-os

@cmonr
Copy link
Contributor

cmonr commented Nov 26, 2018

Trying a thing. Standby.

@cmonr cmonr closed this Nov 26, 2018
@cmonr cmonr removed the needs: CI label Nov 26, 2018
@cmonr cmonr reopened this Nov 26, 2018
@cmonr
Copy link
Contributor

cmonr commented Nov 26, 2018

Trying another thing.

@cmonr cmonr closed this Nov 26, 2018
@cmonr cmonr removed the needs: CI label Nov 26, 2018
@cmonr cmonr mentioned this pull request Nov 26, 2018
@mbed-ci
Copy link

mbed-ci commented Nov 26, 2018

Test run: SUCCESS

Summary: 4 of 4 test jobs passed
Build number : 21
Build artifacts
Build logs

@mattbrown015
Copy link
Contributor

Hi,

I've just tried to update to the latest master and my code size has increased considerably because of this PR. I was somewhat surprised!

| Module                                                 |        .text |    .data |      .bss |
|--------------------------------------------------------|--------------|----------|-----------|
...
| mbed-os\hal\mpu\mbed_mpu_v7m.o                         |    315(+315) |    0(+0) |     0(+0) |
...
| mbed-os\platform\mbed_mpu_mgmt.o                       |    446(+446) |    0(+0) |     4(+4) |
| Subtotals                                              | 215926(+824) | 3296(+0) | 28488(+0) |
...
Total Static RAM memory (data + bss): 31784(+0) bytes
Total Flash memory (text + data): 219222(+824) bytes

An increase of 824 bytes feels like a lot, to me, for something I didn't ask for! :-;

I can mitigate this slightly by defining MBED_MPU_CUSTOM and removing the MPU device from my custom target:

| Module                                                 |        .text |    .data |      .bss |
|--------------------------------------------------------|--------------|----------|-----------|
...
| mbed-os\hal\mpu\mbed_mpu_v7m.o                         |      0(-315) |    0(+0) |     0(+0) |    ...
...
| mbed-os\platform\mbed_mpu_mgmt.o                       |     406(-40) |    0(+0) |     4(+0) |
...
| Subtotals                                              | 215590(-336) | 3296(+0) | 28488(+0) |
Total Static RAM memory (data + bss): 31784(+0) bytes
Total Flash memory (text + data): 218886(-336) bytes

But now it feels like I've got 406 byte of completely pointless code in mbed_mpu_mgmt.c.

I'm using the develop profile and errors are enabled. Some space could/would be saved by removing the error messages.

My target is a STM32L442 and luckily I've got some spare code flash at the moment. I've got more code to write though.

Obviously security is a really important subject but shouldn't it be possible to disable this feature so that it has zero impact??

Our device has propriety LoRaWAN and NFC interfaces (there's no network or Internet connection). I'm wondering how exposed we are to exploits such as buffer overrun attacks. (If we'd finished our software on time I never would have noticed this PR or realised that the ARM M4 had an MPU.) Perhaps security professionals would suggest this is exactly the attitude that has caused all the great security breaches! :-}

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 6, 2018

Thanks @mattbrown015 for the feedback, we will review

@cmonr
Copy link
Contributor

cmonr commented Dec 6, 2018

@mattbrown015 Would you mind migrating this to an issue, so that we can track and assign it properly?

@bulislaw
Copy link
Member

@c1728p9 this memory increase is unacceptable, I'm considering actually switching this feature off by default for now. Please can you look at it ASAP.

@kjbracey
Copy link
Contributor

That mbed_mpu_mgmt code should only be activated if someone trying to deactivate protection at runtime. The only in-tree user is FlashIAP, so I hope @mattbrown015's application is using nvstore or FlashIAPBlockDevice or something else depending on that. If not, something weird is happening.

I can knock up a quick PR addressing #9007 and also cutting down the memory use when it's enabled.

@mattbrown015
Copy link
Contributor

I hope @mattbrown015's application is using nvstore or FlashIAPBlockDevice or something else depending on that

We are using FlashIAP directly.

Up until #8335 I didn't even realise the M4 had a MPU so we're not explicitly doing anything about memory protection. Obviously that implies some issues on our part but we're trying to get this project finished so I think it is a bit late for us to make many changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet