Skip to content

Conversation

deepikabhavnani
Copy link

@deepikabhavnani deepikabhavnani commented Sep 22, 2017

Cortex v8 architecture based devices support secure/non-secure builds. Secure build should generate the object file/library from elf during link process which is used by non-secure binary during linking.
TZ_context functions are part of secure state binary and are used to handle secure stack for thread execution.

NS flag in the core name in Target.json indicated Non Secure build for the core. It is supported for v8M devices only.
Secure library will be built as part of linking process for GCC compiler. Non-Secure project should add this secure object file as part of the linking process.

Secure library is named as cmse_lib.o.

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

I think we can improve the python modifications. Suggestions for how below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please use

secure_file = join(dirname(output), "cmse_lib.o")

instead.

Copy link
Author

Choose a reason for hiding this comment

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

Team: cmse_lib.o : Is it ok to name secure library as generic secure lib, or should it be core specific like CortexM23-S-lib.o?

Copy link
Contributor

Choose a reason for hiding this comment

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

For reference, if you are going to name it that way, the python should look like:

secure_file = join(dirname(output), "%s-S-lib.o" % self.target.core)

Copy link
Contributor

Choose a reason for hiding this comment

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

Please use

self.progress("Secure Library Object", secure_file)

instead.

Copy link
Author

Choose a reason for hiding this comment

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

@theotherjimmy - Build for non-secure will fail if secure library file is missing during linking. To make it more user friendly:

  1. Should be scan and report for missing file in advance?
  2. Add it as requirement in app,json and expect it to be at same location?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmmm... We could do that, but what if someone compiles for a pure non-secure CORTEX-M23-NS? Would you be locking them into having an empty object file to trick the build system?

Copy link
Author

@deepikabhavnani deepikabhavnani Sep 22, 2017

Choose a reason for hiding this comment

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

Empty object file won;t help because it will again fail during linking. Core will boot in secure mode always and to switch to non-secure mode, stubs function and switch to non-secure reset handler will be required. One cannot boot with just non-secure project.

Copy link
Contributor

Choose a reason for hiding this comment

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

You misunderstood my question. Let me rephrase. Can a user build a non-secure Cortex-M23-NS application without any secure component?

Copy link
Author

@deepikabhavnani deepikabhavnani Sep 22, 2017

Choose a reason for hiding this comment

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

Sorry do u mean no secure API's just basic dummy functions. Yes build will be successful and it will be same as current mbed-os without any advantage of security

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. If I understand correctly, A user would need to have an object file compiled for secure Cortex-M23 containing these functions, even if they don't care about security and just want to compile there entire application for Cortex-M23-NS.

If that's the case, then I think it's fine to insist that they have an object file matching the naming convention created by the linker.

Copy link
Author

Choose a reason for hiding this comment

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

@theotherjimmy

self.progress("Secure Library Object", secure_file)

Does truncates file path basename(event['file']) and prints just filename. I need path of file to be printed. Didn't saw any args which can be helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does truncates file path basename(event['file']) and prints just filename.

I'm having trouble understanding that this sentence is supposed to mean. Could you rephrase it? Is this a question? Is this an assertion?

I need path of file to be printed.

Hmmmmmm.... That's not supported by it. Try:

self.info("Secure Library object file %s" % secure_file)

@theotherjimmy
Copy link
Contributor

theotherjimmy commented Sep 22, 2017

@deepikabhavnani I beat you too it 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always defined (shouldn't this check first if its defined then its value) ?

Copy link
Author

Choose a reason for hiding this comment

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

Yes it is always defined in

#ifndef __DOMAIN_NS

Copy link
Contributor

Choose a reason for hiding this comment

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

4 spaces alignment in this file?

Copy link
Contributor

Choose a reason for hiding this comment

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

are these two something that should be in the config ?

Copy link
Author

Choose a reason for hiding this comment

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

Yes we can have them in config

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 27, 2017

Another round of reviews opened!

@deepikabhavnani
Copy link
Author

Rebased to pick up latest tools fixes

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 29, 2017

Bump for review !

@deepikabhavnani
Copy link
Author

deepikabhavnani commented Sep 29, 2017

Application in Secure mode, calls assembly functions which are also toolchain specific. I need to move them to mbed_boot.c after verification.


/* Number of process slots (threads may call secure library code) */
#ifndef MBED_CONF_APP_TZ_PROCESS_STACK_SLOTS
#define MBED_CONF_APP_TZ_PROCESS_STACK_SLOTS 8U
Copy link
Member

Choose a reason for hiding this comment

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

why 8? Can we document that

Copy link
Author

Choose a reason for hiding this comment

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

Ideally it should be number of threads we allow in mbed-os. (Considering all of them have access to secure library). Or do we want to restrict threads from using secure library?

_main_thread_attr.name = "main_thread";

/* Allow main thread to call secure functions */
#if (__DOMAIN_NS == 1U)
Copy link
Member

Choose a reason for hiding this comment

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

Don't worry about it for this PR, but this file is becoming unreadable with more ifdefs than anything else, we should find better way of doing this.

#endif

/* Stack size of the secure library code */
#ifndef MBED_CONF_APP_TZ_PROCESS_STACK_SIZE
Copy link
Member

Choose a reason for hiding this comment

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

What is this stack for exactly?

Copy link
Author

Choose a reason for hiding this comment

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

It is stack for NSC region. i.e Secure library code executed from non-secure process.

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth extending the comment ? Are we using code defaults instead of config - (this could be in the config file set, always present if not overriden) ? All in one place, this can however be fixed, as we already have this mbed_rtx_conf.h file.

Copy link
Author

Choose a reason for hiding this comment

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

@0xc0170 - In case of secure build, we ignore rtos (mbed_rtx_conf.h) hence I am exploring other correct options to update this flag.



/// Number of process slots (threads may call secure library code)
#ifndef TZ_PROCESS_STACK_SLOTS
Copy link
Member

Choose a reason for hiding this comment

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

We do the same in config file. Why do we duplicate that it's confusing

Copy link
Author

Choose a reason for hiding this comment

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

I thought we do not update the flags/code picked up from CMSIS repo, hence i duplicated it. Can remove if required in this case.

@@ -0,0 +1,200 @@
/* mbed Microcontroller Library
Copy link
Member

Choose a reason for hiding this comment

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

Where does this file coming from?

Copy link
Author

Choose a reason for hiding this comment

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

@theotherjimmy
Copy link
Contributor

@bulislaw Have your comments been addressed?

@mbed-ci
Copy link

mbed-ci commented Oct 9, 2017

Build : SUCCESS

Build number : 29
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5180/

Triggering tests

/test mbed-os

@mbed-ci
Copy link

mbed-ci commented Oct 9, 2017

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 10, 2017

@tommikas jenkins CI restart here?

@mbed-ci
Copy link

mbed-ci commented Oct 10, 2017

Build : SUCCESS

Build number : 69
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5180/

Triggering tests

/test mbed-os

@mbed-ci
Copy link

mbed-ci commented Oct 10, 2017

Build : SUCCESS

Build number : 74
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5180/

Triggering tests

/test mbed-os

@mbed-ci
Copy link

mbed-ci commented Oct 10, 2017

@mbed-ci
Copy link

mbed-ci commented Oct 11, 2017

Build : SUCCESS

Build number : 97
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5180/

Triggering tests

/test mbed-os

@mbed-ci
Copy link

mbed-ci commented Oct 11, 2017

@mbed-ci
Copy link

mbed-ci commented Oct 11, 2017

Build : SUCCESS

Build number : 132
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5180/

Triggering tests

/test mbed-os

Copy link
Contributor

Choose a reason for hiding this comment

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

why is this in the tools and not behind a TARGET_M23 folder?

Copy link
Author

Choose a reason for hiding this comment

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

Creation of secure library shouldn;t be part of tools?

Copy link
Contributor

Choose a reason for hiding this comment

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

I dont know the workflow or modifications needed to that file

Copy link
Contributor

@0xc0170 0xc0170 Oct 12, 2017

Choose a reason for hiding this comment

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

@deepikabhavnani As I read this ld flag: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0803h/pge1444636329852.html - this is the output thus located into build dir as cmse_lib.o. Threfore it is part of tools, or is there any other option?

cc @theotherjimmy

Copy link
Author

@deepikabhavnani deepikabhavnani Oct 12, 2017

Choose a reason for hiding this comment

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

@0xc0170 - Generation of Secure library has to be part of tools, ARMC6 compiler allows creation from elf file as well, but GCC/ARM support generation of this object file during linking.

We can rename lib to anything we want (like target specific) and can place in any required build folder (Current: .\BUILD<Target Device><Toolchain>\cmse_lib.o

deepikabhavnani added 7 commits October 13, 2017 10:20
Cortex v8 architecture based devices support secure/non-secure builds.
Secure build should generate the object file/library from elf during link
process which is used by non-secure binary during linking.

--out-implib=file specifies output library in which symbols are exported
--cmse-implib requests libraries mentioned above are secure gateway
libraries

Creation of secure library is done as part of linking process in GCC/ARMC6/IAR
Non-Secure project should add this secure object file as part of the
linking process.
Secure library is named as cmse_lib.o.
osPriorityIdle,
0U, 0U
#if (__DOMAIN_NS == 1U)
1U,
Copy link
Author

Choose a reason for hiding this comment

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

Actual fix should come in CMSIS, raised an issue ARM-software/CMSIS_5#252.
Need to update when we have proper fix from CMSIS

{

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
call_non_secure_rtos();
Copy link
Author

Choose a reason for hiding this comment

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

With this change main of secure thread will never be called, any initialization required needs to be done in mbed_sdk_init.

BMI Sys_ContextSave1 ; Branch if secure
MOV R0,LR ; Get EXC_RETURN
LSLS R0,R0,#25 ; Check domain of interrupted thread
BPL Sys_ContextSave1 ; Branch if not secure
Copy link
Author

Choose a reason for hiding this comment

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

Raised issue + added PR in CMSIS
ARM-software/CMSIS_5#260

Deepika added 4 commits October 13, 2017 12:07
TZ_context functions are part of secure state binary and are used to handle
secure stack for thread execution.Template implementation from CMSIS is used,
can be enhanced in future.

TZ_PROCESS defines are added to config.
Stack size is increased to 512 in source file as well, since config file is not
picked when compiling mbed-os in secure mode without RTOS.
Non-Secure threads can access secure calls only when tz_module attribute is set
as 1(OS_SECURE_CALLABLE_THREAD), while thread creation.
Hence adding tz_module as an argument to ctor.
Issues addressed:
1. Switch to secure/nonsecure context save/restore is based on 6th bit in
LR register, correct the bug (R7 instead of LR was used for decision)
2. Retain value of R7
3. Branch if non-secure instead of secure
@deepikabhavnani deepikabhavnani force-pushed the secure_support branch 2 times, most recently from 10e2d09 to c52d375 Compare October 16, 2017 19:29
@deepikabhavnani
Copy link
Author

deepikabhavnani commented Oct 31, 2017

@theotherjimmy @adbridge @0xc0170 - This PR is on hold till ARMC6 compiler issue is resolved.
https://jira.arm.com/browse/SDCOMP-49132

@deepikabhavnani
Copy link
Author

Closing this, will reopen when work on this feature is resumed

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.

6 participants