This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Best way run driver code in privilege mode?

I want to write some drivers that run in privilege mode.  For example if I have a UART write function I want to make it easy to wrap the write function such that it runs in privilege mode.  My thought was it would be nice to just have a macro/attribute I could wrap code in that would cause it to run in privilege mode but could not figure out a way to do it. 

size_t write(uint8_t *ptrData, size_t len)

{

    PRIVILEGE {

      //run code to access peripheral in privilege mode

    }

}

The best I could come up with is a wrapper such that I use the SVC interrupt to call a function. 

size_t _write(uint8_t *ptrData, size_t len)

{

    //run code to access peripheral in privilege mode

}

size_t write(uint8_t *ptrData, size_t len)

{

     return SVC_CALL(_write);

}

Basically the SVC_CALL would push the function address on the stack and then call SVC #0 (or any number) and then the SVC interrupt would pop the function address and the stack for the function arguments, then call the _write() function in privilege mode.  The issue here is that it is not as clean as the first example, and you have to be careful as the ABI and return values.   For example knowing that the _write() function has two arguments passed in verse one, or three. 

https://blog.stratifylabs.co/device/2013-10-12-Effective-Use-of-ARM-Cortex-M3-SVCall/

Has someone has figured out a good way to write drivers that run in privilege mode and abstract the call to drivers, if not how do people currently do it?  

Parents
  • You have access to the "SVC" opcode via L14, therefore you can use e.g. SVC #0 for write and SVC #1 for read.

    Or you use r12 to store the "function code".

    But sure, as soon as you have a function with more parameter than fitting into R0..R3 it is getting more complicated.

Reply
  • You have access to the "SVC" opcode via L14, therefore you can use e.g. SVC #0 for write and SVC #1 for read.

    Or you use r12 to store the "function code".

    But sure, as soon as you have a function with more parameter than fitting into R0..R3 it is getting more complicated.

Children