1 /*
2  * Common syscall type defines
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6 
7 #ifndef _SYSCALLS_COMMON_H
8 #define _SYSCALLS_COMMON_H	1
9 
10 #ifndef _SYSCALL_H
11 # error "Never use <bits/syscalls-common.h> directly; include <sys/syscall.h> instead."
12 #endif
13 
14 #ifndef SYS_ify
15 # define SYS_ify(syscall_name) (__NR_##syscall_name)
16 #endif
17 
18 #ifndef __ASSEMBLER__
19 
20 #include <errno.h>
21 
22 #ifndef INTERNAL_SYSCALL_DECL
23 # define INTERNAL_SYSCALL_DECL(err)         do { } while (0)
24 #endif
25 #ifndef INTERNAL_SYSCALL_ERROR_P
26 # define INTERNAL_SYSCALL_ERROR_P(val, err) ((unsigned long)val >= (unsigned long)(-4095))
27 #endif
28 #ifndef INTERNAL_SYSCALL_ERRNO
29 # define INTERNAL_SYSCALL_ERRNO(val, err)   (-(val))
30 #endif
31 
32 /* Define a macro which expands into the inline wrapper code for a system call */
33 #ifndef INLINE_SYSCALL
34 # define INLINE_SYSCALL(name, nr, args...) INLINE_SYSCALL_NCS(__NR_##name, nr, args)
35 #endif
36 #ifndef INLINE_SYSCALL_NOERR
37 # define INLINE_SYSCALL_NOERR(name, nr, args...) INLINE_SYSCALL_NOERR_NCS(__NR_##name, nr, args)
38 #endif
39 
40 /* Just like INLINE_SYSCALL(), but take a non-constant syscall (NCS) argument */
41 #ifndef INLINE_SYSCALL_NCS
42 # define INLINE_SYSCALL_NCS(name, nr, args...)				\
43 (__extension__								\
44  ({									\
45 	INTERNAL_SYSCALL_DECL(__err);					\
46 	(__extension__							\
47 	 ({								\
48 	   long __res = INTERNAL_SYSCALL_NCS(name, __err, nr, args);	\
49 	   if (unlikely(INTERNAL_SYSCALL_ERROR_P(__res, __err))) {	\
50 		__set_errno(INTERNAL_SYSCALL_ERRNO(__res, __err));	\
51 		__res = -1L;						\
52 	   }								\
53 	   __res;							\
54 	  })								\
55 	);								\
56   })									\
57 )
58 #endif
59 #ifndef INLINE_SYSCALL_NOERR_NCS
60 # define INLINE_SYSCALL_NOERR_NCS(name, nr, args...)			\
61 ({									\
62 	INTERNAL_SYSCALL_DECL(__err);					\
63 	long __res = INTERNAL_SYSCALL_NCS(name, __err, nr, args);	\
64 	__res;								\
65 })
66 #endif
67 
68 /* No point in forcing people to implement both when they only need one */
69 #ifndef INTERNAL_SYSCALL
70 # define INTERNAL_SYSCALL(name, err, nr, args...) INTERNAL_SYSCALL_NCS(__NR_##name, err, nr, args)
71 #endif
72 
73 #ifndef INTERNAL_SYSCALL_NCS
74 # error your port needs to define INTERNAL_SYSCALL_NCS in bits/syscalls.h
75 #endif
76 
77 #ifndef _syscall0
78 
79 #define C_DECL_ARGS_0()			void
80 #define C_DECL_ARGS_1(t, v)		t v
81 #define C_DECL_ARGS_2(t, v, args...)	t v, C_DECL_ARGS_1(args)
82 #define C_DECL_ARGS_3(t, v, args...)	t v, C_DECL_ARGS_2(args)
83 #define C_DECL_ARGS_4(t, v, args...)	t v, C_DECL_ARGS_3(args)
84 #define C_DECL_ARGS_5(t, v, args...)	t v, C_DECL_ARGS_4(args)
85 #define C_DECL_ARGS_6(t, v, args...)	t v, C_DECL_ARGS_5(args)
86 
87 #define C_ARGS_0()
88 #define C_ARGS_1(t, v)			v
89 #define C_ARGS_2(t, v, args...)		v, C_ARGS_1(args)
90 #define C_ARGS_3(t, v, args...)		v, C_ARGS_2(args)
91 #define C_ARGS_4(t, v, args...)		v, C_ARGS_3(args)
92 #define C_ARGS_5(t, v, args...)		v, C_ARGS_4(args)
93 #define C_ARGS_6(t, v, args...)		v, C_ARGS_5(args)
94 
95 #define SYSCALL_FUNC(nargs, type, name, args...)			\
96 type name(C_DECL_ARGS_##nargs(args)) {					\
97 	return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args));	\
98 }
99 
100 #define SYSCALL_NOERR_FUNC(nargs, type, name, args...)			\
101 type name(C_DECL_ARGS_##nargs(args)) {					\
102 	return (type)INLINE_SYSCALL_NOERR(name, nargs, C_ARGS_##nargs(args));	\
103 }
104 
105 #define _syscall0(args...)		SYSCALL_FUNC(0, args)
106 #define _syscall_noerr0(args...)	SYSCALL_NOERR_FUNC(0, args)
107 #define _syscall1(args...)		SYSCALL_FUNC(1, args)
108 #define _syscall_noerr1(args...)	SYSCALL_NOERR_FUNC(1, args)
109 #define _syscall2(args...)		SYSCALL_FUNC(2, args)
110 #define _syscall3(args...)		SYSCALL_FUNC(3, args)
111 #define _syscall4(args...)		SYSCALL_FUNC(4, args)
112 #define _syscall5(args...)		SYSCALL_FUNC(5, args)
113 #define _syscall6(args...)		SYSCALL_FUNC(6, args)
114 
115 #endif /* _syscall0 */
116 
117 #endif /* __ASSEMBLER__ */
118 
119 #endif
120