ARM System Calls: Difference between revisions

no edit summary
[unchecked revision][unchecked revision]
No edit summary
No edit summary
Line 2:
==System Calls==
 
<pre>swi 420x420000</pre>
This is how you call the system on ARM. The instruction `<span style="font-family:monospace">swi`</span> jumps to a predefined address, which in turn jumps to the system call handler. The system call handler executes the specific function and return to the user code with:
<pre>subsmov pc, lr, #4</pre>
Most of the time you won't need to worry about returning from the interrupt, as GCC, set up to cross compile for ARM, lets you code the interrupt handlers in C:
<source lang="c">void swi_handler () __attribute__((interrupt));</source>
Line 19:
b . @ FIQ
</pre>
This is the ARM equivalent to the IDT, on the x86, and it is stored at address 0. The only entry, we need to worry about is the SWI Handler. To install our own SWI handler, we replace the <span style="font-family:monospace">b ."</span> instruction with a branch to our handler:
<pre>
interrupt_vector_table:
Line 38:
void __attribute__ ((interrupt ("SWI"))) swi_handler (int r0, int r1, int r2, int r3) {}
</source>
You have probably noticed from the first example (<span style="font-family:monospace">swi 42"0x420000</span>), that the "swi" instruction takes an integer as an argument. For it to work in both ARM and THUMB modes of the processor, the integer must be left shifted by 16. To get this argumentinteger fromin the C code, you do this:
<source lang="c">
uint32_tuint8_t int_vector = 0;
asm volatile ("ldrldrb %0, [lr, #-82]" : "=r" (int_vector));
int_vector &= 0xFFFFFF;
</source>
We only need the first 24-bits, because the last 8-bits are those of the "swi" instruction, 0xEF.
[[Category:ARM]]
[[Category:In Progress]]
Anonymous user