Analysis of Arm64 Linux System Call

Arm64 system has two type of traps,

Synchronous
Asynchronous

and four exceptions which start with el (stands for exception level.)

el0 (userspace)
el1 (kernel)
el2 (hypervisor)
el3 (secure mode)

Synchronous is a well-known term of system-call, …


This content originally appeared on DEV Community and was authored by Leesoo Ahn

Arm64 system has two type of traps,

  • Synchronous
  • Asynchronous

and four exceptions which start with el (stands for exception level.)

  • el0 (userspace)
  • el1 (kernel)
  • el2 (hypervisor)
  • el3 (secure mode)

Synchronous is a well-known term of system-call, while Asynchronous is as hardware interrupt in Arm whitepaper. But the latter is off-topic in this article.

One process is working in el0 and it would raise its hand by itself if it needs any system resource at a time. This is system-call and switches the exception level of CPUs from el0 to el1. Now, Kernel takes the CPU and does something for the leftovers instead of the process. Once it's done, it hands out the CPU to the process again.

The following code is about one of (real) system-call APIs from musl, a well-known libc library.

#define __asm_syscall(...) do { \
    __asm__ __volatile__ ( "svc 0" \
    : "=r"(x0) : __VA_ARGS__ : "memory", "cc"); \
    return x0; \
} while (0)

static inline long __syscall0(long n)
{
    register long x8 __asm__("x8") = n;
    register long x0 __asm__("x0");
    __asm_syscall("r"(x8));
}

Imagine that one process mentioned above is about to call fork() very soon. The API doesn't take any arguments and therefore, it maps to __syscall0(..).

What you need to keep in mind regarding to the code is svc instruction (stands for supervisor-call), to switch from el0 to el1 with x8 register holding digits represents the system-call number.

el0_sync handler would be called in el1 by the exception vector table describing what to do if svc raised.

SYM_CODE_START_LOCAL_NOALIGN(el0_sync)
    kernel_entry 0
    mov x0, sp
    bl  el0_sync_handler
    b   ret_to_user
SYM_CODE_END(el0_sync)


This content originally appeared on DEV Community and was authored by Leesoo Ahn


Print Share Comment Cite Upload Translate Updates
APA

Leesoo Ahn | Sciencx (2024-08-12T17:35:12+00:00) Analysis of Arm64 Linux System Call. Retrieved from https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/

MLA
" » Analysis of Arm64 Linux System Call." Leesoo Ahn | Sciencx - Monday August 12, 2024, https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/
HARVARD
Leesoo Ahn | Sciencx Monday August 12, 2024 » Analysis of Arm64 Linux System Call., viewed ,<https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/>
VANCOUVER
Leesoo Ahn | Sciencx - » Analysis of Arm64 Linux System Call. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/
CHICAGO
" » Analysis of Arm64 Linux System Call." Leesoo Ahn | Sciencx - Accessed . https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/
IEEE
" » Analysis of Arm64 Linux System Call." Leesoo Ahn | Sciencx [Online]. Available: https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/. [Accessed: ]
rf:citation
» Analysis of Arm64 Linux System Call | Leesoo Ahn | Sciencx | https://www.scien.cx/2024/08/12/analysis-of-arm64-linux-system-call/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.