aboutsummaryrefslogtreecommitdiff
path: root/libc/src/__support/OSUtil/linux/arm/syscall.h
blob: 351a1c05aae1901f7107cb0ee906517a547dc820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===--------- inline implementation of arm syscalls --------------* C++ *-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_ARM_SYSCALL_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_ARM_SYSCALL_H

#include "src/__support/common.h"

#ifdef __thumb__
#error "The arm syscall implementation does not yet support thumb flavor."
#endif // __thumb__

#define REGISTER_DECL_0                                                        \
  register long r7 __asm__("r7") = number;                                     \
  register long r0 __asm__("r0");
#define REGISTER_DECL_1                                                        \
  register long r7 __asm__("r7") = number;                                     \
  register long r0 __asm__("r0") = arg1;
#define REGISTER_DECL_2                                                        \
  REGISTER_DECL_1                                                              \
  register long r1 __asm__("r1") = arg2;
#define REGISTER_DECL_3                                                        \
  REGISTER_DECL_2                                                              \
  register long r2 __asm__("r2") = arg3;
#define REGISTER_DECL_4                                                        \
  REGISTER_DECL_3                                                              \
  register long r3 __asm__("r3") = arg4;
#define REGISTER_DECL_5                                                        \
  REGISTER_DECL_4                                                              \
  register long r4 __asm__("r4") = arg5;
#define REGISTER_DECL_6                                                        \
  REGISTER_DECL_5                                                              \
  register long r5 __asm__("r5") = arg6;

#define REGISTER_CONSTRAINT_0 "r"(r7)
#define REGISTER_CONSTRAINT_1 REGISTER_CONSTRAINT_0, "r"(r0)
#define REGISTER_CONSTRAINT_2 REGISTER_CONSTRAINT_1, "r"(r1)
#define REGISTER_CONSTRAINT_3 REGISTER_CONSTRAINT_2, "r"(r2)
#define REGISTER_CONSTRAINT_4 REGISTER_CONSTRAINT_3, "r"(r3)
#define REGISTER_CONSTRAINT_5 REGISTER_CONSTRAINT_4, "r"(r4)
#define REGISTER_CONSTRAINT_6 REGISTER_CONSTRAINT_5, "r"(r5)

#define SYSCALL_INSTR(input_constraint)                                        \
  LIBC_INLINE_ASM("svc 0" : "=r"(r0) : input_constraint : "memory", "cc")

namespace __llvm_libc {

LIBC_INLINE long syscall_impl(long number) {
  REGISTER_DECL_0;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_0);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1) {
  REGISTER_DECL_1;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_1);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) {
  REGISTER_DECL_2;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_2);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) {
  REGISTER_DECL_3;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_3);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
                              long arg4) {
  REGISTER_DECL_4;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_4);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
                              long arg4, long arg5) {
  REGISTER_DECL_5;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_5);
  return r0;
}

LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
                              long arg4, long arg5, long arg6) {
  REGISTER_DECL_6;
  SYSCALL_INSTR(REGISTER_CONSTRAINT_6);
  return r0;
}

} // namespace __llvm_libc

#undef REGISTER_DECL_0
#undef REGISTER_DECL_1
#undef REGISTER_DECL_2
#undef REGISTER_DECL_3
#undef REGISTER_DECL_4
#undef REGISTER_DECL_5
#undef REGISTER_DECL_6

#undef REGISTER_CONSTRAINT_0
#undef REGISTER_CONSTRAINT_1
#undef REGISTER_CONSTRAINT_2
#undef REGISTER_CONSTRAINT_3
#undef REGISTER_CONSTRAINT_4
#undef REGISTER_CONSTRAINT_5
#undef REGISTER_CONSTRAINT_6

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_ARM_SYSCALL_H