• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--------- inline implementation of riscv syscalls ------------* C++ *-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_RISCV_SYSCALL_H
10 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_RISCV_SYSCALL_H
11 
12 #include "src/__support/common.h"
13 
14 #define REGISTER_DECL_0                                                        \
15   register long a7 __asm__("a7") = number;                                     \
16   register long a0 __asm__("a0");
17 #define REGISTER_DECL_1                                                        \
18   register long a7 __asm__("a7") = number;                                     \
19   register long a0 __asm__("a0") = arg1;
20 #define REGISTER_DECL_2 REGISTER_DECL_1 register long a1 __asm__("a1") = arg2;
21 #define REGISTER_DECL_3                                                        \
22   REGISTER_DECL_2                                                              \
23   register long a2 __asm__("a2") = arg3;
24 #define REGISTER_DECL_4                                                        \
25   REGISTER_DECL_3                                                              \
26   register long a3 __asm__("a3") = arg4;
27 #define REGISTER_DECL_5                                                        \
28   REGISTER_DECL_4                                                              \
29   register long a4 __asm__("a4") = arg5;
30 #define REGISTER_DECL_6                                                        \
31   REGISTER_DECL_5                                                              \
32   register long a5 __asm__("a5") = arg6;
33 
34 #define REGISTER_CONSTRAINT_0 "r"(a7)
35 #define REGISTER_CONSTRAINT_1 REGISTER_CONSTRAINT_0, "r"(a0)
36 #define REGISTER_CONSTRAINT_2 REGISTER_CONSTRAINT_1, "r"(a1)
37 #define REGISTER_CONSTRAINT_3 REGISTER_CONSTRAINT_2, "r"(a2)
38 #define REGISTER_CONSTRAINT_4 REGISTER_CONSTRAINT_3, "r"(a3)
39 #define REGISTER_CONSTRAINT_5 REGISTER_CONSTRAINT_4, "r"(a4)
40 #define REGISTER_CONSTRAINT_6 REGISTER_CONSTRAINT_5, "r"(a5)
41 
42 #define SYSCALL_INSTR(input_constraint)                                        \
43   LIBC_INLINE_ASM("ecall\n\t" : "=r"(a0) : input_constraint : "memory")
44 
45 namespace LIBC_NAMESPACE {
46 
syscall_impl(long number)47 LIBC_INLINE long syscall_impl(long number) {
48   REGISTER_DECL_0;
49   SYSCALL_INSTR(REGISTER_CONSTRAINT_0);
50   return a0;
51 }
52 
syscall_impl(long number,long arg1)53 LIBC_INLINE long syscall_impl(long number, long arg1) {
54   REGISTER_DECL_1;
55   SYSCALL_INSTR(REGISTER_CONSTRAINT_1);
56   return a0;
57 }
58 
syscall_impl(long number,long arg1,long arg2)59 LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) {
60   REGISTER_DECL_2;
61   SYSCALL_INSTR(REGISTER_CONSTRAINT_2);
62   return a0;
63 }
64 
syscall_impl(long number,long arg1,long arg2,long arg3)65 LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) {
66   REGISTER_DECL_3;
67   SYSCALL_INSTR(REGISTER_CONSTRAINT_3);
68   return a0;
69 }
70 
syscall_impl(long number,long arg1,long arg2,long arg3,long arg4)71 LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
72                               long arg4) {
73   REGISTER_DECL_4;
74   SYSCALL_INSTR(REGISTER_CONSTRAINT_4);
75   return a0;
76 }
77 
syscall_impl(long number,long arg1,long arg2,long arg3,long arg4,long arg5)78 LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
79                               long arg4, long arg5) {
80   REGISTER_DECL_5;
81   SYSCALL_INSTR(REGISTER_CONSTRAINT_5);
82   return a0;
83 }
84 
syscall_impl(long number,long arg1,long arg2,long arg3,long arg4,long arg5,long arg6)85 LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
86                               long arg4, long arg5, long arg6) {
87   REGISTER_DECL_6;
88   SYSCALL_INSTR(REGISTER_CONSTRAINT_6);
89   return a0;
90 }
91 
92 } // namespace LIBC_NAMESPACE
93 
94 #undef REGISTER_DECL_0
95 #undef REGISTER_DECL_1
96 #undef REGISTER_DECL_2
97 #undef REGISTER_DECL_3
98 #undef REGISTER_DECL_4
99 #undef REGISTER_DECL_5
100 #undef REGISTER_DECL_6
101 
102 #undef REGISTER_CONSTRAINT_0
103 #undef REGISTER_CONSTRAINT_1
104 #undef REGISTER_CONSTRAINT_2
105 #undef REGISTER_CONSTRAINT_3
106 #undef REGISTER_CONSTRAINT_4
107 #undef REGISTER_CONSTRAINT_5
108 #undef REGISTER_CONSTRAINT_6
109 
110 #endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_RISCV_SYSCALL_H
111