1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * LoongArch specific definitions for NOLIBC
4 * Copyright (C) 2023 Loongson Technology Corporation Limited
7 #ifndef _NOLIBC_ARCH_LOONGARCH_H
8 #define _NOLIBC_ARCH_LOONGARCH_H
13 /* Syscalls for LoongArch :
14 * - stack is 16-byte aligned
15 * - syscall number is passed in a7
16 * - arguments are in a0, a1, a2, a3, a4, a5
17 * - the system call is performed by calling "syscall 0"
18 * - syscall return comes in a0
19 * - the arguments are cast to long and assigned into the target
20 * registers which are then simply passed as registers to the asm code,
21 * so that we don't have to experience issues with register constraints.
24 #define _NOLIBC_SYSCALL_CLOBBERLIST \
25 "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8"
27 #define my_syscall0(num) \
29 register long _num __asm__ ("a7") = (num); \
30 register long _arg1 __asm__ ("a0"); \
36 : _NOLIBC_SYSCALL_CLOBBERLIST \
41 #define my_syscall1(num, arg1) \
43 register long _num __asm__ ("a7") = (num); \
44 register long _arg1 __asm__ ("a0") = (long)(arg1); \
50 : _NOLIBC_SYSCALL_CLOBBERLIST \
55 #define my_syscall2(num, arg1, arg2) \
57 register long _num __asm__ ("a7") = (num); \
58 register long _arg1 __asm__ ("a0") = (long)(arg1); \
59 register long _arg2 __asm__ ("a1") = (long)(arg2); \
66 : _NOLIBC_SYSCALL_CLOBBERLIST \
71 #define my_syscall3(num, arg1, arg2, arg3) \
73 register long _num __asm__ ("a7") = (num); \
74 register long _arg1 __asm__ ("a0") = (long)(arg1); \
75 register long _arg2 __asm__ ("a1") = (long)(arg2); \
76 register long _arg3 __asm__ ("a2") = (long)(arg3); \
81 : "r"(_arg2), "r"(_arg3), \
83 : _NOLIBC_SYSCALL_CLOBBERLIST \
88 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
90 register long _num __asm__ ("a7") = (num); \
91 register long _arg1 __asm__ ("a0") = (long)(arg1); \
92 register long _arg2 __asm__ ("a1") = (long)(arg2); \
93 register long _arg3 __asm__ ("a2") = (long)(arg3); \
94 register long _arg4 __asm__ ("a3") = (long)(arg4); \
99 : "r"(_arg2), "r"(_arg3), "r"(_arg4), \
101 : _NOLIBC_SYSCALL_CLOBBERLIST \
106 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
108 register long _num __asm__ ("a7") = (num); \
109 register long _arg1 __asm__ ("a0") = (long)(arg1); \
110 register long _arg2 __asm__ ("a1") = (long)(arg2); \
111 register long _arg3 __asm__ ("a2") = (long)(arg3); \
112 register long _arg4 __asm__ ("a3") = (long)(arg4); \
113 register long _arg5 __asm__ ("a4") = (long)(arg5); \
118 : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
120 : _NOLIBC_SYSCALL_CLOBBERLIST \
125 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
127 register long _num __asm__ ("a7") = (num); \
128 register long _arg1 __asm__ ("a0") = (long)(arg1); \
129 register long _arg2 __asm__ ("a1") = (long)(arg2); \
130 register long _arg3 __asm__ ("a2") = (long)(arg3); \
131 register long _arg4 __asm__ ("a3") = (long)(arg4); \
132 register long _arg5 __asm__ ("a4") = (long)(arg5); \
133 register long _arg6 __asm__ ("a5") = (long)(arg6); \
138 : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \
140 : _NOLIBC_SYSCALL_CLOBBERLIST \
145 #if __loongarch_grlen == 32
146 #define LONG_BSTRINS "bstrins.w"
147 #else /* __loongarch_grlen == 64 */
148 #define LONG_BSTRINS "bstrins.d"
152 void __attribute__((weak
, noreturn
)) __nolibc_entrypoint __no_stack_protector
_start(void)
155 "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */
156 LONG_BSTRINS
" $sp, $zero, 3, 0\n" /* $sp must be 16-byte aligned */
157 "bl _start_c\n" /* transfer to c runtime */
159 __nolibc_entrypoint_epilogue();
162 #endif /* _NOLIBC_ARCH_LOONGARCH_H */