]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lxfuxlibc/lib/src/lxfuxlc.c
89ffd3745f0944e0a32f3a4a1b463874acd49180
[l4.git] / l4 / pkg / lxfuxlibc / lib / src / lxfuxlc.c
1 /*
2  * (c) 2004-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
4  *               Frank Mehnert <fm3@os.inf.tu-dresden.de>
5  *     economic rights: Technische Universität Dresden (Germany)
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU Lesser General Public License 2.1.
8  * Please see the COPYING-LGPL-2.1 file for details.
9  */
10
11 /*
12  * Some very minor system calls for Linux.
13  */
14
15 #include <l4/lxfuxlibc/lxfuxlc.h>
16
17 /* unistd.h stuff */
18 #define __NR_exit               1
19 #define __NR_fork               2
20 #define __NR_read               3
21 #define __NR_write              4
22 #define __NR_open               5
23 #define __NR_close              6
24 #define __NR_waitpid            7
25 #define __NR_unlink             10
26 #define __NR_chdir              12
27 #define __NR_lseek              19
28 #define __NR_getpid             20
29 #define __NR_kill               37
30 #define __NR_rename             38
31 #define __NR_mkdir              39
32 #define __NR_rmdir              40
33 #define __NR_pipe               42
34 #define __NR_ioctl              54
35 #define __NR_gettimeofday       78
36 #define __NR_ftruncate          93
37 #define __NR_socketcall         102
38 #define __NR_stat               106
39 #define __NR_lstat              107
40 #define __NR_fstat              108
41 #define __NR_ipc                117
42 #define __NR_select             142 /* new select */
43 #define __NR_readv              145
44 #define __NR_writev             146
45 #define __NR_poll               168
46 #define __NR___lx_priv_ftruncate64 194
47 #define __NR_ftruncate64        194
48 #define __NR_stat64             195
49 #define __NR_fstat64            197
50 #define __NR_getdents64         220
51 #define __NR_fcntl64            221
52 #define __NR_openat             295
53 #define __NR_mkdirat            296
54 #define __NR_unlinkat           301
55 #define __NR_renameat           302
56 #define __NR_faccessat          307
57
58
59 /* Some stuff pilfered from linux/include/asm-i386/unistd.h */
60
61 #define __lx_syscall_return(type, res)                                  \
62   return (type) (res)
63
64 /* This one isn't used anymore and just left for the education of the reader
65  */
66 /* user-visible error numbers are in the range -1 to -124, see asm/errno.h */
67 #define __lx_syscall_return_with_errno(type, res)                                       \
68 do {                                                                    \
69   if ((unsigned long)(res) >= (unsigned long)(-125)) {                  \
70     lx_errno = -(res);                                                  \
71     res = -1;                                                           \
72   }                                                                     \
73   return (type) (res);                                                  \
74 } while (0)
75
76 #define __lx_syscall0(type,name)                                        \
77 type lx_##name(void)                                                    \
78 {                                                                       \
79   long __res;                                                           \
80   __asm__ __volatile__ (                                                \
81       "int      $0x80\n"                                                \
82       : "=a" (__res)                                                    \
83       : "0" (__NR_##name)                                               \
84       : "memory");                                                      \
85   __lx_syscall_return(type, __res);                                     \
86 }
87
88 #define __lx_syscall1(type,name,type1,arg1)                             \
89 type lx_##name(type1 arg1)                                              \
90 {                                                                       \
91   long __res;                                                           \
92   __asm__ __volatile__ (                                                \
93       "int      $0x80\n"                                                \
94       : "=a" (__res)                                                    \
95       : "0" (__NR_##name),                                              \
96         "b" ((long)(arg1))                                              \
97       : "memory");                                                      \
98   __lx_syscall_return(type, __res);                                     \
99 }
100
101 #define __lx_syscall1_e(type,name,type1,arg1)                           \
102 type lx_##name(type1 arg1)                                              \
103 {                                                                       \
104 loop:                                                                   \
105   __asm__ __volatile__ (                                                \
106       "int      $0x80\n"                                                \
107       :                                                                 \
108       : "a" (__NR_##name),                                              \
109         "b" ((long)(arg1))                                              \
110       : "memory");                                                      \
111   goto loop;                                                            \
112 }
113
114 #define __lx_syscall2(type,name,type1,arg1,type2,arg2)                  \
115 type lx_##name(type1 arg1, type2 arg2)                                  \
116 {                                                                       \
117   long __res;                                                           \
118   __asm__ __volatile__ (                                                \
119       "int      $0x80\n"                                                \
120       : "=a" (__res)                                                    \
121       : "0" (__NR_##name),                                              \
122         "b" ((long)(arg1)), "c" ((long)(arg2))                          \
123       : "memory");                                                      \
124   __lx_syscall_return(type, __res);                                     \
125 }
126
127 #define __lx_syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)       \
128 type lx_##name(type1 arg1, type2 arg2, type3 arg3)                      \
129 {                                                                       \
130   long __res;                                                           \
131   __asm__ __volatile__ (                                                \
132       "int      $0x80\n"                                                \
133       : "=a" (__res)                                                    \
134       : "0" (__NR_##name),                                              \
135         "b" ((long)(arg1)), "c" ((long)(arg2)), "d" ((long)(arg3))      \
136       : "memory");                                                      \
137   __lx_syscall_return(type, __res);                                     \
138 }
139
140 #define __lx_syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)    \
141 type lx_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4   )       \
142 {                                                                       \
143   long __res;                                                           \
144   __asm__ __volatile__ (                                                \
145       "int      $0x80\n"                                                \
146       : "=a" (__res)                                                    \
147       : "0" (__NR_##name),                                              \
148         "b" ((long)(arg1)), "c" ((long)(arg2)), "d" ((long)(arg3)),     \
149         "S" ((long)(arg4))                                              \
150       : "memory");                                                      \
151   __lx_syscall_return(type, __res);                                     \
152 }
153
154 #define __lx_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
155 type lx_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)      \
156 {                                                                       \
157   long __res;                                                           \
158   __asm__ __volatile__ (                                                \
159       "int      $0x80\n"                                                \
160       : "=a" (__res)                                                    \
161       : "0" (__NR_##name),                                              \
162         "b" ((long)(arg1)), "c" ((long)(arg2)), "d" ((long)(arg3)),     \
163         "S" ((long)(arg4)), "D" ((long)(arg5))                          \
164       : "memory");                                                      \
165   __lx_syscall_return(type, __res);                                     \
166 }
167
168 #define __lx_syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6)      \
169 type lx_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6)  \
170 {                                                                       \
171   long __res;                                                           \
172   __asm__ __volatile__ (                                                \
173       "push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp"   \
174       : "=a" (__res)                                                    \
175       : "i" (__NR_##name),                                              \
176         "b" ((long)(arg1)), "c" ((long)(arg2)), "d" ((long)(arg3)),     \
177         "S" ((long)(arg4)), "D" ((long)(arg5)), "0" ((long)(arg6))      \
178       : "memory");                                                      \
179   __lx_syscall_return(type, __res);                                     \
180 }
181
182 __lx_syscall1_e(void, exit, int, status)
183 __lx_syscall0(lx_pid_t, fork)
184 __lx_syscall3(long, read,  unsigned int, fd, void *, buf, unsigned int, count)
185 __lx_syscall3(long, write, unsigned int, fd, const void *, buf, unsigned int, count)
186 __lx_syscall3(long, open, const char *, filename, int, flags, int, mode)
187 __lx_syscall1(long, close, unsigned int, fd)
188 __lx_syscall3(lx_pid_t, waitpid, lx_pid_t, pid, int *, wait_stat, int, options)
189 __lx_syscall1(int, unlink, const char *, filename)
190 __lx_syscall1(int, chdir, const char *, filename)
191 __lx_syscall3(long, lseek, unsigned int, fd, unsigned long, offset, unsigned int, origin)
192 __lx_syscall0(long, getpid)
193 __lx_syscall2(int, kill, lx_pid_t, pid, int, sig)
194 __lx_syscall2(int, rename, const char *, oldpath, const char *, newpath)
195 __lx_syscall2(int, mkdir, const char *, filename, int, mode)
196 __lx_syscall1(int, rmdir, const char *, filename)
197 __lx_syscall1(int, pipe, int *, filedes)
198 __lx_syscall3(long, ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
199 __lx_syscall2(long, gettimeofday, struct lx_timeval *, tv, struct lx_timezone *, tz)
200 __lx_syscall5(int, select, int, n, lx_fd_set *, readfds, lx_fd_set *, writefds, lx_fd_set *, exceptfds, struct lx_timeval *, timeout)
201 __lx_syscall2(int, ftruncate, int, fd, unsigned long, ofs)
202 __lx_syscall2(int, socketcall, int, call, unsigned long *, args);
203 __lx_syscall2(int, stat, const char *, filename, struct lx_stat *, buf)
204 __lx_syscall2(int, lstat, const char *, filename, struct lx_stat *, buf)
205 __lx_syscall2(int, fstat, int, filedes, struct lx_stat *, buf)
206 __lx_syscall6(int, ipc, unsigned int, call, int, first, int, second, int, third, const void *, ptr, long, fifth)
207 __lx_syscall3(lx_ssize_t, readv, int, fd, const struct lx_iovec*, iov, int, cnt);
208 __lx_syscall3(lx_ssize_t, writev, int, fd, const struct lx_iovec*, iov, int, cnt);
209 __lx_syscall3(int, poll, struct lx_pollfd *, fds, lx_nfds_t, nfds, int, timeout)
210 #ifdef __i386__
211 int lx___lx_priv_ftruncate64(int, unsigned long, unsigned long);
212 __lx_syscall3(int, __lx_priv_ftruncate64, int, fd, unsigned long, high_length, unsigned long, low_length);
213 #else
214 __lx_syscall2(int, ftruncate64, int, fd, unsigned long, length);
215 #endif
216 __lx_syscall2(int, stat64,  const char *, pathname, struct lx_stat64 *, buf)
217 __lx_syscall2(int, fstat64, int, fd, struct lx_stat64 *, buf)
218 __lx_syscall3(long, getdents64, int, fd, char *, buf, unsigned int, nbytes)
219 __lx_syscall3(int, fcntl64, int, fd, unsigned int, cmd, unsigned long, arg);
220 __lx_syscall4(int, openat,  int, fd, const char *, path, int, flags, int, mode);
221 __lx_syscall3(int, mkdirat, int, fd, const char *, name, int, mode);
222 __lx_syscall3(int, unlinkat, int, fd, const char *, name, int, flags);
223 __lx_syscall4(int, renameat, int, ofd, const char *, oname, int, nfd, const char *, nname);
224 __lx_syscall3(int, faccessat, int, dfd, const char *, filename, int, mode);
225
226 #ifdef __i386__
227 int lx_ftruncate64(int, unsigned long long);
228 int lx_ftruncate64(int fd, unsigned long long length)
229 {
230   return lx___lx_priv_ftruncate64(fd, length << 32, length);
231 }
232 #endif
233
234 /* ========================================================================
235  *                   pure wrapper stuff w/o syscalls
236  * ========================================================================
237  */
238
239 /* time could also be done through a syscall */
240 long lx_time(int *tloc)
241 {
242   struct lx_timeval tv;
243   if (lx_gettimeofday(&tv, NULL) == 0) {
244     if (tloc)
245       *tloc = tv.tv_sec;
246     return tv.tv_sec;
247   }
248   return -1;
249 }
250
251 unsigned int lx_sleep(unsigned int seconds)
252 {
253   struct lx_timeval tv;
254
255   tv.tv_sec  = seconds;
256   tv.tv_usec = 0;
257   if (lx_select(1, NULL, NULL, NULL, &tv) < 0)
258     return 0;
259
260   return tv.tv_sec;
261 }
262
263 unsigned int lx_msleep(unsigned int mseconds)
264 {
265   struct lx_timeval tv;
266
267   tv.tv_sec  = 0;
268   tv.tv_usec = mseconds * 1000;
269   if (lx_select(1, NULL, NULL, NULL, &tv) < 0)
270     return 0;
271
272   return tv.tv_sec;
273 }
274
275 lx_pid_t lx_wait(int *wait_stat)
276 {
277   return lx_waitpid(-1, wait_stat, 0);
278 }
279
280 void *lx_shmat(int shmid, const void *shmaddr, int shmflg)
281 {
282   void *raddr;
283   void *result = (void *)lx_ipc(SHMAT, shmid, shmflg, (int)&raddr, shmaddr, 0);
284   if ((unsigned long)result <= -(unsigned long)8196)
285     result = raddr;
286   return result;
287
288 }
289
290 enum { SYS_SOCKET = 1, SYS_CONNECT = 3 };
291
292 int lx_socket(int family, int type, int protocol)
293 {
294   unsigned long a[3] = { family, type, protocol };
295   return lx_socketcall(SYS_SOCKET, a);
296 }
297
298 int lx_connect(int sockfd, const struct lx_sockaddr *saddr, unsigned long addrlen)
299 {
300   unsigned long a[3] = { sockfd, (unsigned long)saddr, addrlen };
301   return lx_socketcall(SYS_CONNECT, a);
302 }
303
304 /* ------------------------------------------------------------------ */
305
306 void lx_outchar(unsigned char c)
307 {
308   lx_write(1, (char*)&c, 1);
309 }
310
311 void lx_outdec32(unsigned int i)
312 {
313   char xx[11]; /* int is 32bit -> up to 10 figues... */
314   int count = 10;
315
316   xx[10] = 0;
317   do {
318     count--;
319     xx[count] = '0' + i % 10;
320     i /= 10;
321   } while (i);
322   lx_write(1, &xx[count], 10 - count); 
323 }