Saurabh Sengar
Saurabh Sengar

Reputation: 918

How SVC mode is changed to USER mode?

I understand in latest ARM architecture by 'SVC 0' (system call)instruction USER mode is switched to SVC mode. And return value of system call is stored in r0 register, SPSR_SVC is copied to CPSR . I want to know how which api does this ?

Upvotes: 1

Views: 1797

Answers (1)

Notlikethat
Notlikethat

Reputation: 20924

The architectural operation of the SVC instruction is to save the CPSR into SPSR_SVC, save the address of the following instruction to LR_SVC, switch the CPU into SVC mode, set the PC to the SVC exception vector (0x08) and continue exectuting from there. That's just what the hardware does for that instruction (as a single atomic operation) - there is no "API" involved. The SVC handler code, once it's done whatever it wants to, will usually execute one of the exception return instructions*, which will essentially do all that in reverse - restore the mode and CPSR state from SPSR_SVC, set the PC to LR_SVC, and continue in the restored mode.

Now, any other ABI on top of that is entirely the choice of whatever system is being implemented. If Linux chooses to use r0 to return a value, then that's just how its syscall handler code is written. Other systems might do something entirely different.


*depending on instruction set, the level the exception was taken at, and whether the stack is involved or not, usually one of ERET, SUBS PC, LR, #0, or LDM with PC in the list and the ^ suffix.

Upvotes: 1

Related Questions