1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * RISCV (32 and 64) specific definitions for NOLIBC
4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_ARCH_RISCV_H
8 #define _NOLIBC_ARCH_RISCV_H
13 /* Syscalls for RISCV :
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 ecall
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 my_syscall0(num) \
26 register long _num __asm__ ("a7") = (num); \
27 register long _arg1 __asm__ ("a0"); \
38 #define my_syscall1(num, arg1) \
40 register long _num __asm__ ("a7") = (num); \
41 register long _arg1 __asm__ ("a0") = (long)(arg1); \
52 #define my_syscall2(num, arg1, arg2) \
54 register long _num __asm__ ("a7") = (num); \
55 register long _arg1 __asm__ ("a0") = (long)(arg1); \
56 register long _arg2 __asm__ ("a1") = (long)(arg2); \
68 #define my_syscall3(num, arg1, arg2, arg3) \
70 register long _num __asm__ ("a7") = (num); \
71 register long _arg1 __asm__ ("a0") = (long)(arg1); \
72 register long _arg2 __asm__ ("a1") = (long)(arg2); \
73 register long _arg3 __asm__ ("a2") = (long)(arg3); \
78 : "r"(_arg2), "r"(_arg3), \
85 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
87 register long _num __asm__ ("a7") = (num); \
88 register long _arg1 __asm__ ("a0") = (long)(arg1); \
89 register long _arg2 __asm__ ("a1") = (long)(arg2); \
90 register long _arg3 __asm__ ("a2") = (long)(arg3); \
91 register long _arg4 __asm__ ("a3") = (long)(arg4); \
96 : "r"(_arg2), "r"(_arg3), "r"(_arg4), \
103 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
105 register long _num __asm__ ("a7") = (num); \
106 register long _arg1 __asm__ ("a0") = (long)(arg1); \
107 register long _arg2 __asm__ ("a1") = (long)(arg2); \
108 register long _arg3 __asm__ ("a2") = (long)(arg3); \
109 register long _arg4 __asm__ ("a3") = (long)(arg4); \
110 register long _arg5 __asm__ ("a4") = (long)(arg5); \
115 : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
122 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
124 register long _num __asm__ ("a7") = (num); \
125 register long _arg1 __asm__ ("a0") = (long)(arg1); \
126 register long _arg2 __asm__ ("a1") = (long)(arg2); \
127 register long _arg3 __asm__ ("a2") = (long)(arg3); \
128 register long _arg4 __asm__ ("a3") = (long)(arg4); \
129 register long _arg5 __asm__ ("a4") = (long)(arg5); \
130 register long _arg6 __asm__ ("a5") = (long)(arg6); \
135 : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \
143 void __attribute__((weak
, noreturn
)) __nolibc_entrypoint __no_stack_protector
_start(void)
148 "lla gp, __global_pointer$\n"
150 "mv a0, sp\n" /* save stack pointer to a0, as arg1 of _start_c */
151 "andi sp, a0, -16\n" /* sp must be 16-byte aligned */
152 "call _start_c\n" /* transfer to c runtime */
154 __nolibc_entrypoint_epilogue();
157 #endif /* _NOLIBC_ARCH_RISCV_H */