Intercept SVC call from user space con ARMv7M

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
mribelotta
Posts: 5
Joined: Sun Jul 14, 2013 11:36 pm

Intercept SVC call from user space con ARMv7M

Post 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!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Intercept SVC call from user space con ARMv7M

Post 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
mribelotta
Posts: 5
Joined: Sun Jul 14, 2013 11:36 pm

Re: Intercept SVC call from user space con ARMv7M

Post 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.
Post Reply