Page 1 of 1

Intercept SVC call from user space con ARMv7M

Posted: Tue Jan 28, 2014 8:49 pm
by mribelotta
I try to build code on unprivileged ARMv7m land (user mode) and comunicate with OS using "SVC #n" instrucction.

In https://github.com/mabl/ChibiOS/blob/ma ... _v7m.c#L58 the code shows that the SVC instruction ignores the value of #n.

This is the correct way to intercept SVC call and make "!CORTEX_SIMPLIFIED_PRIORITY" mode continuing work?:

Code: Select all

void SVCallVector(void) {
  switch(getSVCn()) {
  case 0: doSVCallVector(); break; /* return???? */
  case 1: doStuff(); break;
  case 2: doOherStuff(); break;
  ....anithing...
  }
  /* port_unlock_from_isr(); ??????  */
}

/* This code posibly make a new stack context. It is rigth? */
void doSVCallVector(void) {
  struct extctx *ctxp;

  /* Current PSP value.*/
  asm volatile ("mrs     %0, PSP" : "=r" (ctxp) : : "memory");

  /* Discarding the current exception context and positioning the stack to
     point to the real one.*/
  ctxp++;

#if CORTEX_USE_FPU
  /* Restoring the special register SCB_FPCCR.*/
  SCB_FPCCR = (uint32_t)ctxp->fpccr;
  SCB_FPCAR = SCB_FPCAR + sizeof (struct extctx);
#endif
  asm volatile ("msr     PSP, %0" : : "r" (ctxp) : "memory");
  port_unlock_from_isr(); /* Where is this going?!!!?*/
}


Wath is the diference with CORTEX_SIMPLIFIED_PRIORITY? Who lost in this mode?

The MSP is used only in kernel handler mode? The code only work with the PSP registrer

Many thanks!

Re: Intercept SVC call from user space con ARMv7M

Posted: Tue Jan 28, 2014 10:08 pm
by Giovanni
Hi,

CORTEX_SIMPLIFIED_PRIORITY means that interrupts are globally disabled during critical zones (normally interrupts are only partially disabled up to a certain priority level), the advantage is a more compact kernel.

When CORTEX_SIMPLIFIED_PRIORITY is set to FALSE (the default) the SVC instruction is used by the kernel to re-enter the exception mode so you can't use it unless you heavily modify the existing handler.

When CORTEX_SIMPLIFIED_PRIORITY is set to TRUE the SVC vector is not used and you may use it for that purpose.

Note that execution in user mode is not supported, execution of any kernel primitive from user mode would generate an exception because the use of privileged instructions, preemption of user mode is not supported by the port layer, there is an assumption that the system is running in privileged mode, returning from ISRs would restore the privileged mode.

Giovanni

Re: Intercept SVC call from user space con ARMv7M

Posted: Wed Jan 29, 2014 12:03 am
by mribelotta
Thanks Giovanni! The solution for my problem is use CORTEX_SIMPLIFIED_PRIORITY defined to TRUE.

The code that I want to run in user mode, is loaded from an external ELF using this library:
https://github.com/martinribelotta/elfloader
And I need to protect the OS execution of faulty or malicious code.