]>
Commit | Line | Data |
---|---|---|
31e31b8a FB |
1 | /* |
2 | * Linux syscalls | |
5fafdf24 | 3 | * |
31e31b8a FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <stdarg.h> | |
04369ff2 | 23 | #include <string.h> |
31e31b8a FB |
24 | #include <elf.h> |
25 | #include <endian.h> | |
26 | #include <errno.h> | |
27 | #include <unistd.h> | |
28 | #include <fcntl.h> | |
7854b056 | 29 | #include <time.h> |
31e31b8a | 30 | #include <sys/types.h> |
d08d3bb8 TS |
31 | #include <sys/ipc.h> |
32 | #include <sys/msg.h> | |
31e31b8a FB |
33 | #include <sys/wait.h> |
34 | #include <sys/time.h> | |
35 | #include <sys/stat.h> | |
36 | #include <sys/mount.h> | |
39b9aae1 | 37 | #include <sys/prctl.h> |
31e31b8a FB |
38 | #include <sys/resource.h> |
39 | #include <sys/mman.h> | |
40 | #include <sys/swap.h> | |
41 | #include <signal.h> | |
42 | #include <sched.h> | |
43 | #include <sys/socket.h> | |
44 | #include <sys/uio.h> | |
9de5e440 | 45 | #include <sys/poll.h> |
32f36bce | 46 | #include <sys/times.h> |
8853f86e | 47 | #include <sys/shm.h> |
fa294816 | 48 | #include <sys/sem.h> |
56c8f68f | 49 | #include <sys/statfs.h> |
ebc05488 | 50 | #include <utime.h> |
a5448a7d | 51 | #include <sys/sysinfo.h> |
72f03900 | 52 | //#include <sys/user.h> |
8853f86e | 53 | #include <netinet/ip.h> |
7854b056 | 54 | #include <netinet/tcp.h> |
31e31b8a FB |
55 | |
56 | #define termios host_termios | |
57 | #define winsize host_winsize | |
58 | #define termio host_termio | |
04369ff2 FB |
59 | #define sgttyb host_sgttyb /* same as target */ |
60 | #define tchars host_tchars /* same as target */ | |
61 | #define ltchars host_ltchars /* same as target */ | |
31e31b8a FB |
62 | |
63 | #include <linux/termios.h> | |
64 | #include <linux/unistd.h> | |
65 | #include <linux/utsname.h> | |
66 | #include <linux/cdrom.h> | |
67 | #include <linux/hdreg.h> | |
68 | #include <linux/soundcard.h> | |
dab2ed99 | 69 | #include <linux/dirent.h> |
19b84f3c | 70 | #include <linux/kd.h> |
31e31b8a | 71 | |
3ef693a0 | 72 | #include "qemu.h" |
31e31b8a | 73 | |
72f03900 | 74 | //#define DEBUG |
31e31b8a | 75 | |
e6e5906b | 76 | #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \ |
48733d19 | 77 | || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS) |
67867308 FB |
78 | /* 16 bit uid wrappers emulation */ |
79 | #define USE_UID16 | |
80 | #endif | |
81 | ||
1a9353d2 FB |
82 | //#include <linux/msdos_fs.h> |
83 | #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2]) | |
84 | #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2]) | |
85 | ||
70a194b9 | 86 | |
70a194b9 FB |
87 | #undef _syscall0 |
88 | #undef _syscall1 | |
89 | #undef _syscall2 | |
90 | #undef _syscall3 | |
91 | #undef _syscall4 | |
92 | #undef _syscall5 | |
83fcb515 | 93 | #undef _syscall6 |
70a194b9 | 94 | |
83fcb515 FB |
95 | #define _syscall0(type,name) \ |
96 | type name (void) \ | |
97 | { \ | |
98 | return syscall(__NR_##name); \ | |
99 | } | |
70a194b9 | 100 | |
83fcb515 FB |
101 | #define _syscall1(type,name,type1,arg1) \ |
102 | type name (type1 arg1) \ | |
103 | { \ | |
104 | return syscall(__NR_##name, arg1); \ | |
70a194b9 FB |
105 | } |
106 | ||
83fcb515 FB |
107 | #define _syscall2(type,name,type1,arg1,type2,arg2) \ |
108 | type name (type1 arg1,type2 arg2) \ | |
109 | { \ | |
110 | return syscall(__NR_##name, arg1, arg2); \ | |
70a194b9 FB |
111 | } |
112 | ||
83fcb515 FB |
113 | #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ |
114 | type name (type1 arg1,type2 arg2,type3 arg3) \ | |
115 | { \ | |
116 | return syscall(__NR_##name, arg1, arg2, arg3); \ | |
70a194b9 FB |
117 | } |
118 | ||
83fcb515 FB |
119 | #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ |
120 | type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ | |
121 | { \ | |
122 | return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ | |
70a194b9 FB |
123 | } |
124 | ||
83fcb515 FB |
125 | #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
126 | type5,arg5) \ | |
127 | type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ | |
128 | { \ | |
129 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ | |
70a194b9 FB |
130 | } |
131 | ||
83fcb515 FB |
132 | |
133 | #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ | |
134 | type5,arg5,type6,arg6) \ | |
135 | type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \ | |
136 | { \ | |
137 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ | |
70a194b9 | 138 | } |
83fcb515 | 139 | |
70a194b9 | 140 | |
31e31b8a | 141 | #define __NR_sys_uname __NR_uname |
92a34c10 | 142 | #define __NR_sys_faccessat __NR_faccessat |
814d7977 | 143 | #define __NR_sys_fchmodat __NR_fchmodat |
ccfa72b7 | 144 | #define __NR_sys_fchownat __NR_fchownat |
72f03900 | 145 | #define __NR_sys_getcwd1 __NR_getcwd |
72f03900 | 146 | #define __NR_sys_getdents __NR_getdents |
dab2ed99 | 147 | #define __NR_sys_getdents64 __NR_getdents64 |
c6cda17a | 148 | #define __NR_sys_getpriority __NR_getpriority |
64f0ce4c | 149 | #define __NR_sys_linkat __NR_linkat |
4472ad0d | 150 | #define __NR_sys_mkdirat __NR_mkdirat |
75ac37a0 | 151 | #define __NR_sys_mknodat __NR_mknodat |
82424832 | 152 | #define __NR_sys_openat __NR_openat |
5e0ccb18 | 153 | #define __NR_sys_readlinkat __NR_readlinkat |
722183f6 | 154 | #define __NR_sys_renameat __NR_renameat |
66fb9763 | 155 | #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo |
f0b6243d | 156 | #define __NR_sys_symlinkat __NR_symlinkat |
7494b0f9 | 157 | #define __NR_sys_syslog __NR_syslog |
71455574 | 158 | #define __NR_sys_tgkill __NR_tgkill |
4cae1d16 | 159 | #define __NR_sys_tkill __NR_tkill |
8170f56b | 160 | #define __NR_sys_unlinkat __NR_unlinkat |
9007f0ef | 161 | #define __NR_sys_utimensat __NR_utimensat |
31e31b8a | 162 | |
bc51c5c9 | 163 | #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) |
9af9eaaa FB |
164 | #define __NR__llseek __NR_lseek |
165 | #endif | |
166 | ||
72f03900 | 167 | #ifdef __NR_gettid |
31e31b8a | 168 | _syscall0(int, gettid) |
72f03900 | 169 | #else |
0da46a6e TS |
170 | /* This is a replacement for the host gettid() and must return a host |
171 | errno. */ | |
72f03900 FB |
172 | static int gettid(void) { |
173 | return -ENOSYS; | |
174 | } | |
175 | #endif | |
31e31b8a | 176 | _syscall1(int,sys_uname,struct new_utsname *,buf) |
92a34c10 TS |
177 | #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) |
178 | _syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags) | |
179 | #endif | |
814d7977 TS |
180 | #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat) |
181 | _syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname, | |
182 | mode_t,mode,int,flags) | |
183 | #endif | |
ccfa72b7 TS |
184 | #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) |
185 | _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname, | |
186 | uid_t,owner,gid_t,group,int,flags) | |
187 | #endif | |
72f03900 FB |
188 | _syscall2(int,sys_getcwd1,char *,buf,size_t,size) |
189 | _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count); | |
3ae43202 | 190 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
dab2ed99 | 191 | _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count); |
3ae43202 | 192 | #endif |
c6cda17a | 193 | _syscall2(int, sys_getpriority, int, which, int, who); |
31e31b8a FB |
194 | _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, |
195 | loff_t *, res, uint, wh); | |
64f0ce4c TS |
196 | #if defined(TARGET_NR_linkat) && defined(__NR_linkat) |
197 | _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath, | |
198 | int,newdirfd,const char *,newpath,int,flags) | |
199 | #endif | |
4472ad0d TS |
200 | #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat) |
201 | _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode) | |
202 | #endif | |
75ac37a0 TS |
203 | #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat) |
204 | _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname, | |
205 | mode_t,mode,dev_t,dev) | |
206 | #endif | |
82424832 TS |
207 | #if defined(TARGET_NR_openat) && defined(__NR_openat) |
208 | _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode) | |
209 | #endif | |
5e0ccb18 TS |
210 | #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat) |
211 | _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname, | |
212 | char *,buf,size_t,bufsize) | |
213 | #endif | |
722183f6 TS |
214 | #if defined(TARGET_NR_renameat) && defined(__NR_renameat) |
215 | _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath, | |
216 | int,newdirfd,const char *,newpath) | |
217 | #endif | |
66fb9763 | 218 | _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo) |
b51eaa82 | 219 | #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat) |
f0b6243d TS |
220 | _syscall3(int,sys_symlinkat,const char *,oldpath, |
221 | int,newdirfd,const char *,newpath) | |
222 | #endif | |
7494b0f9 | 223 | _syscall3(int,sys_syslog,int,type,char*,bufp,int,len) |
3ae43202 | 224 | #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill) |
71455574 | 225 | _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig) |
4cae1d16 | 226 | #endif |
3ae43202 | 227 | #if defined(TARGET_NR_tkill) && defined(__NR_tkill) |
4cae1d16 TS |
228 | _syscall2(int,sys_tkill,int,tid,int,sig) |
229 | #endif | |
ec86b0fb FB |
230 | #ifdef __NR_exit_group |
231 | _syscall1(int,exit_group,int,error_code) | |
232 | #endif | |
6f5b89a0 TS |
233 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
234 | _syscall1(int,set_tid_address,int *,tidptr) | |
235 | #endif | |
8170f56b TS |
236 | #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat) |
237 | _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags) | |
238 | #endif | |
9007f0ef TS |
239 | #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat) |
240 | _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, | |
241 | const struct timespec *,tsp,int,flags) | |
242 | #endif | |
66fb9763 FB |
243 | |
244 | extern int personality(int); | |
9de5e440 FB |
245 | extern int flock(int, int); |
246 | extern int setfsuid(int); | |
247 | extern int setfsgid(int); | |
5cd4393b FB |
248 | extern int setresuid(uid_t, uid_t, uid_t); |
249 | extern int getresuid(uid_t *, uid_t *, uid_t *); | |
250 | extern int setresgid(gid_t, gid_t, gid_t); | |
251 | extern int getresgid(gid_t *, gid_t *, gid_t *); | |
19b84f3c | 252 | extern int setgroups(int, gid_t *); |
31e31b8a | 253 | |
b92c47c1 TS |
254 | #define ERRNO_TABLE_SIZE 1200 |
255 | ||
256 | /* target_to_host_errno_table[] is initialized from | |
257 | * host_to_target_errno_table[] in syscall_init(). */ | |
258 | static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = { | |
259 | }; | |
260 | ||
637947f1 | 261 | /* |
fe8f096b | 262 | * This list is the union of errno values overridden in asm-<arch>/errno.h |
637947f1 TS |
263 | * minus the errnos that are not actually generic to all archs. |
264 | */ | |
b92c47c1 | 265 | static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = { |
637947f1 TS |
266 | [EIDRM] = TARGET_EIDRM, |
267 | [ECHRNG] = TARGET_ECHRNG, | |
268 | [EL2NSYNC] = TARGET_EL2NSYNC, | |
269 | [EL3HLT] = TARGET_EL3HLT, | |
270 | [EL3RST] = TARGET_EL3RST, | |
271 | [ELNRNG] = TARGET_ELNRNG, | |
272 | [EUNATCH] = TARGET_EUNATCH, | |
273 | [ENOCSI] = TARGET_ENOCSI, | |
274 | [EL2HLT] = TARGET_EL2HLT, | |
275 | [EDEADLK] = TARGET_EDEADLK, | |
276 | [ENOLCK] = TARGET_ENOLCK, | |
277 | [EBADE] = TARGET_EBADE, | |
278 | [EBADR] = TARGET_EBADR, | |
279 | [EXFULL] = TARGET_EXFULL, | |
280 | [ENOANO] = TARGET_ENOANO, | |
281 | [EBADRQC] = TARGET_EBADRQC, | |
282 | [EBADSLT] = TARGET_EBADSLT, | |
283 | [EBFONT] = TARGET_EBFONT, | |
284 | [ENOSTR] = TARGET_ENOSTR, | |
285 | [ENODATA] = TARGET_ENODATA, | |
286 | [ETIME] = TARGET_ETIME, | |
287 | [ENOSR] = TARGET_ENOSR, | |
288 | [ENONET] = TARGET_ENONET, | |
289 | [ENOPKG] = TARGET_ENOPKG, | |
290 | [EREMOTE] = TARGET_EREMOTE, | |
291 | [ENOLINK] = TARGET_ENOLINK, | |
292 | [EADV] = TARGET_EADV, | |
293 | [ESRMNT] = TARGET_ESRMNT, | |
294 | [ECOMM] = TARGET_ECOMM, | |
295 | [EPROTO] = TARGET_EPROTO, | |
296 | [EDOTDOT] = TARGET_EDOTDOT, | |
297 | [EMULTIHOP] = TARGET_EMULTIHOP, | |
298 | [EBADMSG] = TARGET_EBADMSG, | |
299 | [ENAMETOOLONG] = TARGET_ENAMETOOLONG, | |
300 | [EOVERFLOW] = TARGET_EOVERFLOW, | |
301 | [ENOTUNIQ] = TARGET_ENOTUNIQ, | |
302 | [EBADFD] = TARGET_EBADFD, | |
303 | [EREMCHG] = TARGET_EREMCHG, | |
304 | [ELIBACC] = TARGET_ELIBACC, | |
305 | [ELIBBAD] = TARGET_ELIBBAD, | |
306 | [ELIBSCN] = TARGET_ELIBSCN, | |
307 | [ELIBMAX] = TARGET_ELIBMAX, | |
308 | [ELIBEXEC] = TARGET_ELIBEXEC, | |
309 | [EILSEQ] = TARGET_EILSEQ, | |
310 | [ENOSYS] = TARGET_ENOSYS, | |
311 | [ELOOP] = TARGET_ELOOP, | |
312 | [ERESTART] = TARGET_ERESTART, | |
313 | [ESTRPIPE] = TARGET_ESTRPIPE, | |
314 | [ENOTEMPTY] = TARGET_ENOTEMPTY, | |
315 | [EUSERS] = TARGET_EUSERS, | |
316 | [ENOTSOCK] = TARGET_ENOTSOCK, | |
317 | [EDESTADDRREQ] = TARGET_EDESTADDRREQ, | |
318 | [EMSGSIZE] = TARGET_EMSGSIZE, | |
319 | [EPROTOTYPE] = TARGET_EPROTOTYPE, | |
320 | [ENOPROTOOPT] = TARGET_ENOPROTOOPT, | |
321 | [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT, | |
322 | [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT, | |
323 | [EOPNOTSUPP] = TARGET_EOPNOTSUPP, | |
324 | [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT, | |
325 | [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT, | |
326 | [EADDRINUSE] = TARGET_EADDRINUSE, | |
327 | [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL, | |
328 | [ENETDOWN] = TARGET_ENETDOWN, | |
329 | [ENETUNREACH] = TARGET_ENETUNREACH, | |
330 | [ENETRESET] = TARGET_ENETRESET, | |
331 | [ECONNABORTED] = TARGET_ECONNABORTED, | |
332 | [ECONNRESET] = TARGET_ECONNRESET, | |
333 | [ENOBUFS] = TARGET_ENOBUFS, | |
334 | [EISCONN] = TARGET_EISCONN, | |
335 | [ENOTCONN] = TARGET_ENOTCONN, | |
336 | [EUCLEAN] = TARGET_EUCLEAN, | |
337 | [ENOTNAM] = TARGET_ENOTNAM, | |
338 | [ENAVAIL] = TARGET_ENAVAIL, | |
339 | [EISNAM] = TARGET_EISNAM, | |
340 | [EREMOTEIO] = TARGET_EREMOTEIO, | |
341 | [ESHUTDOWN] = TARGET_ESHUTDOWN, | |
342 | [ETOOMANYREFS] = TARGET_ETOOMANYREFS, | |
343 | [ETIMEDOUT] = TARGET_ETIMEDOUT, | |
344 | [ECONNREFUSED] = TARGET_ECONNREFUSED, | |
345 | [EHOSTDOWN] = TARGET_EHOSTDOWN, | |
346 | [EHOSTUNREACH] = TARGET_EHOSTUNREACH, | |
347 | [EALREADY] = TARGET_EALREADY, | |
348 | [EINPROGRESS] = TARGET_EINPROGRESS, | |
349 | [ESTALE] = TARGET_ESTALE, | |
350 | [ECANCELED] = TARGET_ECANCELED, | |
351 | [ENOMEDIUM] = TARGET_ENOMEDIUM, | |
352 | [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE, | |
b7fe5db7 | 353 | #ifdef ENOKEY |
637947f1 | 354 | [ENOKEY] = TARGET_ENOKEY, |
b7fe5db7 TS |
355 | #endif |
356 | #ifdef EKEYEXPIRED | |
637947f1 | 357 | [EKEYEXPIRED] = TARGET_EKEYEXPIRED, |
b7fe5db7 TS |
358 | #endif |
359 | #ifdef EKEYREVOKED | |
637947f1 | 360 | [EKEYREVOKED] = TARGET_EKEYREVOKED, |
b7fe5db7 TS |
361 | #endif |
362 | #ifdef EKEYREJECTED | |
637947f1 | 363 | [EKEYREJECTED] = TARGET_EKEYREJECTED, |
b7fe5db7 TS |
364 | #endif |
365 | #ifdef EOWNERDEAD | |
637947f1 | 366 | [EOWNERDEAD] = TARGET_EOWNERDEAD, |
b7fe5db7 TS |
367 | #endif |
368 | #ifdef ENOTRECOVERABLE | |
637947f1 | 369 | [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE, |
b7fe5db7 | 370 | #endif |
b92c47c1 | 371 | }; |
637947f1 TS |
372 | |
373 | static inline int host_to_target_errno(int err) | |
374 | { | |
375 | if(host_to_target_errno_table[err]) | |
376 | return host_to_target_errno_table[err]; | |
377 | return err; | |
378 | } | |
379 | ||
b92c47c1 TS |
380 | static inline int target_to_host_errno(int err) |
381 | { | |
382 | if (target_to_host_errno_table[err]) | |
383 | return target_to_host_errno_table[err]; | |
384 | return err; | |
385 | } | |
386 | ||
992f48a0 | 387 | static inline abi_long get_errno(abi_long ret) |
31e31b8a FB |
388 | { |
389 | if (ret == -1) | |
637947f1 | 390 | return -host_to_target_errno(errno); |
31e31b8a FB |
391 | else |
392 | return ret; | |
393 | } | |
394 | ||
992f48a0 | 395 | static inline int is_error(abi_long ret) |
31e31b8a | 396 | { |
992f48a0 | 397 | return (abi_ulong)ret >= (abi_ulong)(-4096); |
31e31b8a FB |
398 | } |
399 | ||
b92c47c1 TS |
400 | char *target_strerror(int err) |
401 | { | |
402 | return strerror(target_to_host_errno(err)); | |
403 | } | |
404 | ||
992f48a0 BS |
405 | static abi_ulong target_brk; |
406 | static abi_ulong target_original_brk; | |
31e31b8a | 407 | |
992f48a0 | 408 | void target_set_brk(abi_ulong new_brk) |
31e31b8a | 409 | { |
4c1de73d | 410 | target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); |
31e31b8a FB |
411 | } |
412 | ||
0da46a6e | 413 | /* do_brk() must return target values and target errnos. */ |
992f48a0 | 414 | abi_long do_brk(abi_ulong new_brk) |
31e31b8a | 415 | { |
992f48a0 BS |
416 | abi_ulong brk_page; |
417 | abi_long mapped_addr; | |
31e31b8a FB |
418 | int new_alloc_size; |
419 | ||
420 | if (!new_brk) | |
53a5960a | 421 | return target_brk; |
31e31b8a | 422 | if (new_brk < target_original_brk) |
0da46a6e | 423 | return -TARGET_ENOMEM; |
3b46e624 | 424 | |
53a5960a | 425 | brk_page = HOST_PAGE_ALIGN(target_brk); |
31e31b8a FB |
426 | |
427 | /* If the new brk is less than this, set it and we're done... */ | |
428 | if (new_brk < brk_page) { | |
429 | target_brk = new_brk; | |
53a5960a | 430 | return target_brk; |
31e31b8a FB |
431 | } |
432 | ||
433 | /* We need to allocate more memory after the brk... */ | |
54936004 | 434 | new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1); |
5fafdf24 | 435 | mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, |
54936004 FB |
436 | PROT_READ|PROT_WRITE, |
437 | MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0)); | |
31e31b8a FB |
438 | if (is_error(mapped_addr)) { |
439 | return mapped_addr; | |
440 | } else { | |
441 | target_brk = new_brk; | |
53a5960a | 442 | return target_brk; |
31e31b8a FB |
443 | } |
444 | } | |
445 | ||
5fafdf24 | 446 | static inline fd_set *target_to_host_fds(fd_set *fds, |
992f48a0 | 447 | abi_long *target_fds, int n) |
31e31b8a | 448 | { |
7854b056 | 449 | #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN) |
31e31b8a FB |
450 | return (fd_set *)target_fds; |
451 | #else | |
452 | int i, b; | |
453 | if (target_fds) { | |
454 | FD_ZERO(fds); | |
455 | for(i = 0;i < n; i++) { | |
992f48a0 BS |
456 | b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >> |
457 | (i & (TARGET_ABI_BITS - 1))) & 1; | |
31e31b8a FB |
458 | if (b) |
459 | FD_SET(i, fds); | |
460 | } | |
461 | return fds; | |
462 | } else { | |
463 | return NULL; | |
464 | } | |
465 | #endif | |
466 | } | |
467 | ||
992f48a0 | 468 | static inline void host_to_target_fds(abi_long *target_fds, |
31e31b8a FB |
469 | fd_set *fds, int n) |
470 | { | |
7854b056 | 471 | #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN) |
31e31b8a FB |
472 | /* nothing to do */ |
473 | #else | |
474 | int i, nw, j, k; | |
992f48a0 | 475 | abi_long v; |
31e31b8a FB |
476 | |
477 | if (target_fds) { | |
992f48a0 | 478 | nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS; |
31e31b8a FB |
479 | k = 0; |
480 | for(i = 0;i < nw; i++) { | |
481 | v = 0; | |
992f48a0 | 482 | for(j = 0; j < TARGET_ABI_BITS; j++) { |
31e31b8a FB |
483 | v |= ((FD_ISSET(k, fds) != 0) << j); |
484 | k++; | |
485 | } | |
486 | target_fds[i] = tswapl(v); | |
487 | } | |
488 | } | |
489 | #endif | |
490 | } | |
491 | ||
c596ed17 FB |
492 | #if defined(__alpha__) |
493 | #define HOST_HZ 1024 | |
494 | #else | |
495 | #define HOST_HZ 100 | |
496 | #endif | |
497 | ||
992f48a0 | 498 | static inline abi_long host_to_target_clock_t(long ticks) |
c596ed17 FB |
499 | { |
500 | #if HOST_HZ == TARGET_HZ | |
501 | return ticks; | |
502 | #else | |
503 | return ((int64_t)ticks * TARGET_HZ) / HOST_HZ; | |
504 | #endif | |
505 | } | |
506 | ||
579a97f7 FB |
507 | static inline abi_long host_to_target_rusage(abi_ulong target_addr, |
508 | const struct rusage *rusage) | |
b409186b | 509 | { |
53a5960a PB |
510 | struct target_rusage *target_rusage; |
511 | ||
579a97f7 FB |
512 | if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) |
513 | return -TARGET_EFAULT; | |
b409186b FB |
514 | target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec); |
515 | target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec); | |
516 | target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec); | |
517 | target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec); | |
518 | target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss); | |
519 | target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss); | |
520 | target_rusage->ru_idrss = tswapl(rusage->ru_idrss); | |
521 | target_rusage->ru_isrss = tswapl(rusage->ru_isrss); | |
522 | target_rusage->ru_minflt = tswapl(rusage->ru_minflt); | |
523 | target_rusage->ru_majflt = tswapl(rusage->ru_majflt); | |
524 | target_rusage->ru_nswap = tswapl(rusage->ru_nswap); | |
525 | target_rusage->ru_inblock = tswapl(rusage->ru_inblock); | |
526 | target_rusage->ru_oublock = tswapl(rusage->ru_oublock); | |
527 | target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd); | |
528 | target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv); | |
529 | target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals); | |
530 | target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw); | |
531 | target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw); | |
53a5960a | 532 | unlock_user_struct(target_rusage, target_addr, 1); |
579a97f7 FB |
533 | |
534 | return 0; | |
b409186b FB |
535 | } |
536 | ||
579a97f7 FB |
537 | static inline abi_long target_to_host_timeval(struct timeval *tv, |
538 | abi_ulong target_addr) | |
31e31b8a | 539 | { |
53a5960a PB |
540 | struct target_timeval *target_tv; |
541 | ||
579a97f7 FB |
542 | if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1)) |
543 | return -TARGET_EFAULT; | |
66fb9763 FB |
544 | tv->tv_sec = tswapl(target_tv->tv_sec); |
545 | tv->tv_usec = tswapl(target_tv->tv_usec); | |
53a5960a | 546 | unlock_user_struct(target_tv, target_addr, 0); |
579a97f7 FB |
547 | |
548 | return 0; | |
31e31b8a FB |
549 | } |
550 | ||
579a97f7 FB |
551 | static inline abi_long host_to_target_timeval(abi_ulong target_addr, |
552 | const struct timeval *tv) | |
31e31b8a | 553 | { |
53a5960a PB |
554 | struct target_timeval *target_tv; |
555 | ||
579a97f7 FB |
556 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0)) |
557 | return -TARGET_EFAULT; | |
66fb9763 FB |
558 | target_tv->tv_sec = tswapl(tv->tv_sec); |
559 | target_tv->tv_usec = tswapl(tv->tv_usec); | |
53a5960a | 560 | unlock_user_struct(target_tv, target_addr, 1); |
579a97f7 FB |
561 | |
562 | return 0; | |
31e31b8a FB |
563 | } |
564 | ||
565 | ||
0da46a6e | 566 | /* do_select() must return target values and target errnos. */ |
992f48a0 BS |
567 | static abi_long do_select(int n, |
568 | abi_ulong rfd_p, abi_ulong wfd_p, | |
569 | abi_ulong efd_p, abi_ulong target_tv) | |
31e31b8a FB |
570 | { |
571 | fd_set rfds, wfds, efds; | |
572 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; | |
992f48a0 | 573 | abi_long *target_rfds, *target_wfds, *target_efds; |
31e31b8a | 574 | struct timeval tv, *tv_ptr; |
992f48a0 | 575 | abi_long ret; |
53a5960a | 576 | int ok; |
31e31b8a | 577 | |
53a5960a | 578 | if (rfd_p) { |
579a97f7 FB |
579 | target_rfds = lock_user(VERIFY_WRITE, rfd_p, sizeof(abi_long) * n, 1); |
580 | if (!target_rfds) { | |
581 | ret = -TARGET_EFAULT; | |
582 | goto end; | |
583 | } | |
53a5960a PB |
584 | rfds_ptr = target_to_host_fds(&rfds, target_rfds, n); |
585 | } else { | |
586 | target_rfds = NULL; | |
587 | rfds_ptr = NULL; | |
588 | } | |
589 | if (wfd_p) { | |
579a97f7 FB |
590 | target_wfds = lock_user(VERIFY_WRITE, wfd_p, sizeof(abi_long) * n, 1); |
591 | if (!target_wfds) { | |
592 | ret = -TARGET_EFAULT; | |
593 | goto end; | |
594 | } | |
53a5960a PB |
595 | wfds_ptr = target_to_host_fds(&wfds, target_wfds, n); |
596 | } else { | |
597 | target_wfds = NULL; | |
598 | wfds_ptr = NULL; | |
599 | } | |
600 | if (efd_p) { | |
579a97f7 FB |
601 | target_efds = lock_user(VERIFY_WRITE, efd_p, sizeof(abi_long) * n, 1); |
602 | if (!target_efds) { | |
603 | ret = -TARGET_EFAULT; | |
604 | goto end; | |
605 | } | |
53a5960a PB |
606 | efds_ptr = target_to_host_fds(&efds, target_efds, n); |
607 | } else { | |
608 | target_efds = NULL; | |
609 | efds_ptr = NULL; | |
610 | } | |
3b46e624 | 611 | |
31e31b8a | 612 | if (target_tv) { |
5cd4393b | 613 | target_to_host_timeval(&tv, target_tv); |
31e31b8a FB |
614 | tv_ptr = &tv; |
615 | } else { | |
616 | tv_ptr = NULL; | |
617 | } | |
618 | ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr)); | |
53a5960a PB |
619 | ok = !is_error(ret); |
620 | ||
621 | if (ok) { | |
31e31b8a FB |
622 | host_to_target_fds(target_rfds, rfds_ptr, n); |
623 | host_to_target_fds(target_wfds, wfds_ptr, n); | |
624 | host_to_target_fds(target_efds, efds_ptr, n); | |
625 | ||
626 | if (target_tv) { | |
5cd4393b | 627 | host_to_target_timeval(target_tv, &tv); |
31e31b8a FB |
628 | } |
629 | } | |
579a97f7 FB |
630 | |
631 | end: | |
632 | unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0); | |
633 | unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0); | |
634 | unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0); | |
53a5960a | 635 | |
31e31b8a FB |
636 | return ret; |
637 | } | |
638 | ||
579a97f7 FB |
639 | static inline abi_long target_to_host_sockaddr(struct sockaddr *addr, |
640 | abi_ulong target_addr, | |
641 | socklen_t len) | |
7854b056 | 642 | { |
53a5960a PB |
643 | struct target_sockaddr *target_saddr; |
644 | ||
579a97f7 FB |
645 | target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); |
646 | if (!target_saddr) | |
647 | return -TARGET_EFAULT; | |
53a5960a PB |
648 | memcpy(addr, target_saddr, len); |
649 | addr->sa_family = tswap16(target_saddr->sa_family); | |
650 | unlock_user(target_saddr, target_addr, 0); | |
579a97f7 FB |
651 | |
652 | return 0; | |
7854b056 FB |
653 | } |
654 | ||
579a97f7 FB |
655 | static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, |
656 | struct sockaddr *addr, | |
657 | socklen_t len) | |
7854b056 | 658 | { |
53a5960a PB |
659 | struct target_sockaddr *target_saddr; |
660 | ||
579a97f7 FB |
661 | target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); |
662 | if (!target_saddr) | |
663 | return -TARGET_EFAULT; | |
53a5960a PB |
664 | memcpy(target_saddr, addr, len); |
665 | target_saddr->sa_family = tswap16(addr->sa_family); | |
666 | unlock_user(target_saddr, target_addr, len); | |
579a97f7 FB |
667 | |
668 | return 0; | |
7854b056 FB |
669 | } |
670 | ||
53a5960a | 671 | /* ??? Should this also swap msgh->name? */ |
5a4a898d FB |
672 | static inline abi_long target_to_host_cmsg(struct msghdr *msgh, |
673 | struct target_msghdr *target_msgh) | |
7854b056 FB |
674 | { |
675 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); | |
5a4a898d FB |
676 | abi_long msg_controllen; |
677 | abi_ulong target_cmsg_addr; | |
678 | struct target_cmsghdr *target_cmsg; | |
7854b056 | 679 | socklen_t space = 0; |
5a4a898d FB |
680 | |
681 | msg_controllen = tswapl(target_msgh->msg_controllen); | |
682 | if (msg_controllen < sizeof (struct target_cmsghdr)) | |
683 | goto the_end; | |
684 | target_cmsg_addr = tswapl(target_msgh->msg_control); | |
685 | target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1); | |
686 | if (!target_cmsg) | |
687 | return -TARGET_EFAULT; | |
7854b056 FB |
688 | |
689 | while (cmsg && target_cmsg) { | |
690 | void *data = CMSG_DATA(cmsg); | |
691 | void *target_data = TARGET_CMSG_DATA(target_cmsg); | |
692 | ||
5fafdf24 | 693 | int len = tswapl(target_cmsg->cmsg_len) |
7854b056 FB |
694 | - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr)); |
695 | ||
696 | space += CMSG_SPACE(len); | |
697 | if (space > msgh->msg_controllen) { | |
698 | space -= CMSG_SPACE(len); | |
31febb71 | 699 | gemu_log("Host cmsg overflow\n"); |
7854b056 FB |
700 | break; |
701 | } | |
702 | ||
703 | cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level); | |
704 | cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type); | |
705 | cmsg->cmsg_len = CMSG_LEN(len); | |
706 | ||
3532fa74 | 707 | if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
7854b056 FB |
708 | gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type); |
709 | memcpy(data, target_data, len); | |
710 | } else { | |
711 | int *fd = (int *)data; | |
712 | int *target_fd = (int *)target_data; | |
713 | int i, numfds = len / sizeof(int); | |
714 | ||
715 | for (i = 0; i < numfds; i++) | |
716 | fd[i] = tswap32(target_fd[i]); | |
717 | } | |
718 | ||
719 | cmsg = CMSG_NXTHDR(msgh, cmsg); | |
720 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg); | |
721 | } | |
5a4a898d FB |
722 | unlock_user(target_cmsg, target_cmsg_addr, 0); |
723 | the_end: | |
7854b056 | 724 | msgh->msg_controllen = space; |
5a4a898d | 725 | return 0; |
7854b056 FB |
726 | } |
727 | ||
53a5960a | 728 | /* ??? Should this also swap msgh->name? */ |
5a4a898d FB |
729 | static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh, |
730 | struct msghdr *msgh) | |
7854b056 FB |
731 | { |
732 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); | |
5a4a898d FB |
733 | abi_long msg_controllen; |
734 | abi_ulong target_cmsg_addr; | |
735 | struct target_cmsghdr *target_cmsg; | |
7854b056 FB |
736 | socklen_t space = 0; |
737 | ||
5a4a898d FB |
738 | msg_controllen = tswapl(target_msgh->msg_controllen); |
739 | if (msg_controllen < sizeof (struct target_cmsghdr)) | |
740 | goto the_end; | |
741 | target_cmsg_addr = tswapl(target_msgh->msg_control); | |
742 | target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0); | |
743 | if (!target_cmsg) | |
744 | return -TARGET_EFAULT; | |
745 | ||
7854b056 FB |
746 | while (cmsg && target_cmsg) { |
747 | void *data = CMSG_DATA(cmsg); | |
748 | void *target_data = TARGET_CMSG_DATA(target_cmsg); | |
749 | ||
750 | int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr)); | |
751 | ||
752 | space += TARGET_CMSG_SPACE(len); | |
5a4a898d | 753 | if (space > msg_controllen) { |
7854b056 | 754 | space -= TARGET_CMSG_SPACE(len); |
31febb71 | 755 | gemu_log("Target cmsg overflow\n"); |
7854b056 FB |
756 | break; |
757 | } | |
758 | ||
759 | target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level); | |
760 | target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type); | |
761 | target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len)); | |
762 | ||
3532fa74 | 763 | if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
7854b056 FB |
764 | gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type); |
765 | memcpy(target_data, data, len); | |
766 | } else { | |
767 | int *fd = (int *)data; | |
768 | int *target_fd = (int *)target_data; | |
769 | int i, numfds = len / sizeof(int); | |
770 | ||
771 | for (i = 0; i < numfds; i++) | |
772 | target_fd[i] = tswap32(fd[i]); | |
773 | } | |
774 | ||
775 | cmsg = CMSG_NXTHDR(msgh, cmsg); | |
776 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg); | |
777 | } | |
5a4a898d FB |
778 | unlock_user(target_cmsg, target_cmsg_addr, space); |
779 | the_end: | |
780 | target_msgh->msg_controllen = tswapl(space); | |
781 | return 0; | |
7854b056 FB |
782 | } |
783 | ||
0da46a6e | 784 | /* do_setsockopt() Must return target values and target errnos. */ |
992f48a0 BS |
785 | static abi_long do_setsockopt(int sockfd, int level, int optname, |
786 | abi_ulong optval, socklen_t optlen) | |
7854b056 | 787 | { |
992f48a0 | 788 | abi_long ret; |
32407103 | 789 | int val; |
3b46e624 | 790 | |
8853f86e FB |
791 | switch(level) { |
792 | case SOL_TCP: | |
7854b056 | 793 | /* TCP options all take an 'int' value. */ |
7854b056 | 794 | if (optlen < sizeof(uint32_t)) |
0da46a6e | 795 | return -TARGET_EINVAL; |
3b46e624 | 796 | |
53a5960a | 797 | val = tget32(optval); |
8853f86e FB |
798 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
799 | break; | |
800 | case SOL_IP: | |
801 | switch(optname) { | |
2efbe911 FB |
802 | case IP_TOS: |
803 | case IP_TTL: | |
8853f86e | 804 | case IP_HDRINCL: |
2efbe911 FB |
805 | case IP_ROUTER_ALERT: |
806 | case IP_RECVOPTS: | |
807 | case IP_RETOPTS: | |
808 | case IP_PKTINFO: | |
809 | case IP_MTU_DISCOVER: | |
810 | case IP_RECVERR: | |
811 | case IP_RECVTOS: | |
812 | #ifdef IP_FREEBIND | |
813 | case IP_FREEBIND: | |
814 | #endif | |
815 | case IP_MULTICAST_TTL: | |
816 | case IP_MULTICAST_LOOP: | |
8853f86e FB |
817 | val = 0; |
818 | if (optlen >= sizeof(uint32_t)) { | |
53a5960a | 819 | val = tget32(optval); |
8853f86e | 820 | } else if (optlen >= 1) { |
53a5960a | 821 | val = tget8(optval); |
8853f86e FB |
822 | } |
823 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); | |
824 | break; | |
825 | default: | |
826 | goto unimplemented; | |
827 | } | |
828 | break; | |
3532fa74 | 829 | case TARGET_SOL_SOCKET: |
8853f86e FB |
830 | switch (optname) { |
831 | /* Options with 'int' argument. */ | |
3532fa74 FB |
832 | case TARGET_SO_DEBUG: |
833 | optname = SO_DEBUG; | |
834 | break; | |
835 | case TARGET_SO_REUSEADDR: | |
836 | optname = SO_REUSEADDR; | |
837 | break; | |
838 | case TARGET_SO_TYPE: | |
839 | optname = SO_TYPE; | |
840 | break; | |
841 | case TARGET_SO_ERROR: | |
842 | optname = SO_ERROR; | |
843 | break; | |
844 | case TARGET_SO_DONTROUTE: | |
845 | optname = SO_DONTROUTE; | |
846 | break; | |
847 | case TARGET_SO_BROADCAST: | |
848 | optname = SO_BROADCAST; | |
849 | break; | |
850 | case TARGET_SO_SNDBUF: | |
851 | optname = SO_SNDBUF; | |
852 | break; | |
853 | case TARGET_SO_RCVBUF: | |
854 | optname = SO_RCVBUF; | |
855 | break; | |
856 | case TARGET_SO_KEEPALIVE: | |
857 | optname = SO_KEEPALIVE; | |
858 | break; | |
859 | case TARGET_SO_OOBINLINE: | |
860 | optname = SO_OOBINLINE; | |
861 | break; | |
862 | case TARGET_SO_NO_CHECK: | |
863 | optname = SO_NO_CHECK; | |
864 | break; | |
865 | case TARGET_SO_PRIORITY: | |
866 | optname = SO_PRIORITY; | |
867 | break; | |
5e83e8e3 | 868 | #ifdef SO_BSDCOMPAT |
3532fa74 FB |
869 | case TARGET_SO_BSDCOMPAT: |
870 | optname = SO_BSDCOMPAT; | |
871 | break; | |
5e83e8e3 | 872 | #endif |
3532fa74 FB |
873 | case TARGET_SO_PASSCRED: |
874 | optname = SO_PASSCRED; | |
875 | break; | |
876 | case TARGET_SO_TIMESTAMP: | |
877 | optname = SO_TIMESTAMP; | |
878 | break; | |
879 | case TARGET_SO_RCVLOWAT: | |
880 | optname = SO_RCVLOWAT; | |
881 | break; | |
882 | case TARGET_SO_RCVTIMEO: | |
883 | optname = SO_RCVTIMEO; | |
884 | break; | |
885 | case TARGET_SO_SNDTIMEO: | |
886 | optname = SO_SNDTIMEO; | |
887 | break; | |
8853f86e FB |
888 | break; |
889 | default: | |
890 | goto unimplemented; | |
891 | } | |
3532fa74 | 892 | if (optlen < sizeof(uint32_t)) |
0da46a6e | 893 | return -TARGET_EINVAL; |
3532fa74 FB |
894 | |
895 | val = tget32(optval); | |
896 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val))); | |
8853f86e | 897 | break; |
7854b056 | 898 | default: |
8853f86e FB |
899 | unimplemented: |
900 | gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname); | |
0da46a6e | 901 | ret = -TARGET_ENOSYS; |
7854b056 | 902 | } |
8853f86e | 903 | return ret; |
7854b056 FB |
904 | } |
905 | ||
0da46a6e | 906 | /* do_getsockopt() Must return target values and target errnos. */ |
992f48a0 BS |
907 | static abi_long do_getsockopt(int sockfd, int level, int optname, |
908 | abi_ulong optval, abi_ulong optlen) | |
7854b056 | 909 | { |
992f48a0 | 910 | abi_long ret; |
32407103 | 911 | int len, lv, val; |
8853f86e FB |
912 | |
913 | switch(level) { | |
3532fa74 FB |
914 | case TARGET_SOL_SOCKET: |
915 | level = SOL_SOCKET; | |
8853f86e | 916 | switch (optname) { |
3532fa74 FB |
917 | case TARGET_SO_LINGER: |
918 | case TARGET_SO_RCVTIMEO: | |
919 | case TARGET_SO_SNDTIMEO: | |
920 | case TARGET_SO_PEERCRED: | |
921 | case TARGET_SO_PEERNAME: | |
8853f86e FB |
922 | /* These don't just return a single integer */ |
923 | goto unimplemented; | |
924 | default: | |
2efbe911 FB |
925 | goto int_case; |
926 | } | |
927 | break; | |
928 | case SOL_TCP: | |
929 | /* TCP options all take an 'int' value. */ | |
930 | int_case: | |
53a5960a | 931 | len = tget32(optlen); |
2efbe911 | 932 | if (len < 0) |
0da46a6e | 933 | return -TARGET_EINVAL; |
2efbe911 FB |
934 | lv = sizeof(int); |
935 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); | |
936 | if (ret < 0) | |
937 | return ret; | |
938 | val = tswap32(val); | |
939 | if (len > lv) | |
940 | len = lv; | |
53a5960a PB |
941 | if (len == 4) |
942 | tput32(optval, val); | |
943 | else | |
944 | tput8(optval, val); | |
945 | tput32(optlen, len); | |
2efbe911 FB |
946 | break; |
947 | case SOL_IP: | |
948 | switch(optname) { | |
949 | case IP_TOS: | |
950 | case IP_TTL: | |
951 | case IP_HDRINCL: | |
952 | case IP_ROUTER_ALERT: | |
953 | case IP_RECVOPTS: | |
954 | case IP_RETOPTS: | |
955 | case IP_PKTINFO: | |
956 | case IP_MTU_DISCOVER: | |
957 | case IP_RECVERR: | |
958 | case IP_RECVTOS: | |
959 | #ifdef IP_FREEBIND | |
960 | case IP_FREEBIND: | |
961 | #endif | |
962 | case IP_MULTICAST_TTL: | |
963 | case IP_MULTICAST_LOOP: | |
53a5960a | 964 | len = tget32(optlen); |
8853f86e | 965 | if (len < 0) |
0da46a6e | 966 | return -TARGET_EINVAL; |
8853f86e FB |
967 | lv = sizeof(int); |
968 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); | |
969 | if (ret < 0) | |
970 | return ret; | |
2efbe911 | 971 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
2efbe911 | 972 | len = 1; |
53a5960a PB |
973 | tput32(optlen, len); |
974 | tput8(optval, val); | |
2efbe911 | 975 | } else { |
2efbe911 FB |
976 | if (len > sizeof(int)) |
977 | len = sizeof(int); | |
53a5960a PB |
978 | tput32(optlen, len); |
979 | tput32(optval, val); | |
2efbe911 | 980 | } |
8853f86e | 981 | break; |
2efbe911 FB |
982 | default: |
983 | goto unimplemented; | |
8853f86e FB |
984 | } |
985 | break; | |
986 | default: | |
987 | unimplemented: | |
988 | gemu_log("getsockopt level=%d optname=%d not yet supported\n", | |
989 | level, optname); | |
0da46a6e | 990 | ret = -TARGET_ENOSYS; |
8853f86e FB |
991 | break; |
992 | } | |
993 | return ret; | |
7854b056 FB |
994 | } |
995 | ||
579a97f7 FB |
996 | /* FIXME |
997 | * lock_iovec()/unlock_iovec() have a return code of 0 for success where | |
998 | * other lock functions have a return code of 0 for failure. | |
999 | */ | |
1000 | static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr, | |
1001 | int count, int copy) | |
53a5960a PB |
1002 | { |
1003 | struct target_iovec *target_vec; | |
992f48a0 | 1004 | abi_ulong base; |
579a97f7 | 1005 | int i, j; |
53a5960a | 1006 | |
579a97f7 FB |
1007 | target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1); |
1008 | if (!target_vec) | |
1009 | return -TARGET_EFAULT; | |
53a5960a PB |
1010 | for(i = 0;i < count; i++) { |
1011 | base = tswapl(target_vec[i].iov_base); | |
1012 | vec[i].iov_len = tswapl(target_vec[i].iov_len); | |
579a97f7 FB |
1013 | vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy); |
1014 | if (!vec[i].iov_base) | |
1015 | goto fail; | |
1016 | } | |
1017 | unlock_user (target_vec, target_addr, 0); | |
1018 | return 0; | |
1019 | fail: | |
1020 | /* failure - unwind locks */ | |
1021 | for (j = 0; j < i; j++) { | |
1022 | base = tswapl(target_vec[j].iov_base); | |
1023 | unlock_user(vec[j].iov_base, base, 0); | |
53a5960a PB |
1024 | } |
1025 | unlock_user (target_vec, target_addr, 0); | |
579a97f7 | 1026 | return -TARGET_EFAULT; |
53a5960a PB |
1027 | } |
1028 | ||
579a97f7 FB |
1029 | static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr, |
1030 | int count, int copy) | |
53a5960a PB |
1031 | { |
1032 | struct target_iovec *target_vec; | |
992f48a0 | 1033 | abi_ulong base; |
53a5960a PB |
1034 | int i; |
1035 | ||
579a97f7 FB |
1036 | target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1); |
1037 | if (!target_vec) | |
1038 | return -TARGET_EFAULT; | |
53a5960a PB |
1039 | for(i = 0;i < count; i++) { |
1040 | base = tswapl(target_vec[i].iov_base); | |
1041 | unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); | |
1042 | } | |
1043 | unlock_user (target_vec, target_addr, 0); | |
579a97f7 FB |
1044 | |
1045 | return 0; | |
53a5960a PB |
1046 | } |
1047 | ||
0da46a6e | 1048 | /* do_socket() Must return target values and target errnos. */ |
992f48a0 | 1049 | static abi_long do_socket(int domain, int type, int protocol) |
3532fa74 FB |
1050 | { |
1051 | #if defined(TARGET_MIPS) | |
1052 | switch(type) { | |
1053 | case TARGET_SOCK_DGRAM: | |
1054 | type = SOCK_DGRAM; | |
1055 | break; | |
1056 | case TARGET_SOCK_STREAM: | |
1057 | type = SOCK_STREAM; | |
1058 | break; | |
1059 | case TARGET_SOCK_RAW: | |
1060 | type = SOCK_RAW; | |
1061 | break; | |
1062 | case TARGET_SOCK_RDM: | |
1063 | type = SOCK_RDM; | |
1064 | break; | |
1065 | case TARGET_SOCK_SEQPACKET: | |
1066 | type = SOCK_SEQPACKET; | |
1067 | break; | |
1068 | case TARGET_SOCK_PACKET: | |
1069 | type = SOCK_PACKET; | |
1070 | break; | |
1071 | } | |
1072 | #endif | |
12bc92ab AZ |
1073 | if (domain == PF_NETLINK) |
1074 | return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */ | |
3532fa74 FB |
1075 | return get_errno(socket(domain, type, protocol)); |
1076 | } | |
1077 | ||
0da46a6e | 1078 | /* do_bind() Must return target values and target errnos. */ |
992f48a0 BS |
1079 | static abi_long do_bind(int sockfd, abi_ulong target_addr, |
1080 | socklen_t addrlen) | |
3532fa74 FB |
1081 | { |
1082 | void *addr = alloca(addrlen); | |
3b46e624 | 1083 | |
3532fa74 FB |
1084 | target_to_host_sockaddr(addr, target_addr, addrlen); |
1085 | return get_errno(bind(sockfd, addr, addrlen)); | |
1086 | } | |
1087 | ||
0da46a6e | 1088 | /* do_connect() Must return target values and target errnos. */ |
992f48a0 BS |
1089 | static abi_long do_connect(int sockfd, abi_ulong target_addr, |
1090 | socklen_t addrlen) | |
3532fa74 FB |
1091 | { |
1092 | void *addr = alloca(addrlen); | |
3b46e624 | 1093 | |
3532fa74 FB |
1094 | target_to_host_sockaddr(addr, target_addr, addrlen); |
1095 | return get_errno(connect(sockfd, addr, addrlen)); | |
1096 | } | |
1097 | ||
0da46a6e | 1098 | /* do_sendrecvmsg() Must return target values and target errnos. */ |
992f48a0 BS |
1099 | static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg, |
1100 | int flags, int send) | |
3532fa74 | 1101 | { |
992f48a0 | 1102 | abi_long ret; |
3532fa74 FB |
1103 | struct target_msghdr *msgp; |
1104 | struct msghdr msg; | |
1105 | int count; | |
1106 | struct iovec *vec; | |
992f48a0 | 1107 | abi_ulong target_vec; |
3532fa74 | 1108 | |
579a97f7 FB |
1109 | /* FIXME */ |
1110 | if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE, | |
1111 | msgp, | |
1112 | target_msg, | |
1113 | send ? 1 : 0)) | |
1114 | return -TARGET_EFAULT; | |
3532fa74 FB |
1115 | if (msgp->msg_name) { |
1116 | msg.msg_namelen = tswap32(msgp->msg_namelen); | |
1117 | msg.msg_name = alloca(msg.msg_namelen); | |
1118 | target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name), | |
1119 | msg.msg_namelen); | |
1120 | } else { | |
1121 | msg.msg_name = NULL; | |
1122 | msg.msg_namelen = 0; | |
1123 | } | |
1124 | msg.msg_controllen = 2 * tswapl(msgp->msg_controllen); | |
1125 | msg.msg_control = alloca(msg.msg_controllen); | |
1126 | msg.msg_flags = tswap32(msgp->msg_flags); | |
3b46e624 | 1127 | |
3532fa74 FB |
1128 | count = tswapl(msgp->msg_iovlen); |
1129 | vec = alloca(count * sizeof(struct iovec)); | |
1130 | target_vec = tswapl(msgp->msg_iov); | |
579a97f7 | 1131 | lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send); |
3532fa74 FB |
1132 | msg.msg_iovlen = count; |
1133 | msg.msg_iov = vec; | |
3b46e624 | 1134 | |
3532fa74 | 1135 | if (send) { |
5a4a898d FB |
1136 | ret = target_to_host_cmsg(&msg, msgp); |
1137 | if (ret == 0) | |
1138 | ret = get_errno(sendmsg(fd, &msg, flags)); | |
3532fa74 FB |
1139 | } else { |
1140 | ret = get_errno(recvmsg(fd, &msg, flags)); | |
1141 | if (!is_error(ret)) | |
5a4a898d | 1142 | ret = host_to_target_cmsg(msgp, &msg); |
3532fa74 FB |
1143 | } |
1144 | unlock_iovec(vec, target_vec, count, !send); | |
579a97f7 | 1145 | unlock_user_struct(msgp, target_msg, send ? 0 : 1); |
3532fa74 FB |
1146 | return ret; |
1147 | } | |
1148 | ||
0da46a6e | 1149 | /* do_accept() Must return target values and target errnos. */ |
992f48a0 BS |
1150 | static abi_long do_accept(int fd, abi_ulong target_addr, |
1151 | abi_ulong target_addrlen) | |
1be9e1dc PB |
1152 | { |
1153 | socklen_t addrlen = tget32(target_addrlen); | |
197ea35a | 1154 | void *addr = alloca(addrlen); |
992f48a0 | 1155 | abi_long ret; |
1be9e1dc PB |
1156 | |
1157 | ret = get_errno(accept(fd, addr, &addrlen)); | |
1158 | if (!is_error(ret)) { | |
1159 | host_to_target_sockaddr(target_addr, addr, addrlen); | |
1160 | tput32(target_addrlen, addrlen); | |
1161 | } | |
1162 | return ret; | |
1163 | } | |
1164 | ||
0da46a6e | 1165 | /* do_getpeername() Must return target values and target errnos. */ |
992f48a0 BS |
1166 | static abi_long do_getpeername(int fd, abi_ulong target_addr, |
1167 | abi_ulong target_addrlen) | |
1be9e1dc PB |
1168 | { |
1169 | socklen_t addrlen = tget32(target_addrlen); | |
01c227fb | 1170 | void *addr = alloca(addrlen); |
992f48a0 | 1171 | abi_long ret; |
1be9e1dc PB |
1172 | |
1173 | ret = get_errno(getpeername(fd, addr, &addrlen)); | |
1174 | if (!is_error(ret)) { | |
1175 | host_to_target_sockaddr(target_addr, addr, addrlen); | |
1176 | tput32(target_addrlen, addrlen); | |
1177 | } | |
1178 | return ret; | |
1179 | } | |
1180 | ||
0da46a6e | 1181 | /* do_getsockname() Must return target values and target errnos. */ |
992f48a0 BS |
1182 | static abi_long do_getsockname(int fd, abi_ulong target_addr, |
1183 | abi_ulong target_addrlen) | |
1be9e1dc PB |
1184 | { |
1185 | socklen_t addrlen = tget32(target_addrlen); | |
01c227fb | 1186 | void *addr = alloca(addrlen); |
992f48a0 | 1187 | abi_long ret; |
1be9e1dc PB |
1188 | |
1189 | ret = get_errno(getsockname(fd, addr, &addrlen)); | |
1190 | if (!is_error(ret)) { | |
1191 | host_to_target_sockaddr(target_addr, addr, addrlen); | |
1192 | tput32(target_addrlen, addrlen); | |
1193 | } | |
1194 | return ret; | |
1195 | } | |
1196 | ||
0da46a6e | 1197 | /* do_socketpair() Must return target values and target errnos. */ |
992f48a0 BS |
1198 | static abi_long do_socketpair(int domain, int type, int protocol, |
1199 | abi_ulong target_tab) | |
1be9e1dc PB |
1200 | { |
1201 | int tab[2]; | |
992f48a0 | 1202 | abi_long ret; |
1be9e1dc PB |
1203 | |
1204 | ret = get_errno(socketpair(domain, type, protocol, tab)); | |
1205 | if (!is_error(ret)) { | |
1206 | tput32(target_tab, tab[0]); | |
1207 | tput32(target_tab + 4, tab[1]); | |
1208 | } | |
1209 | return ret; | |
1210 | } | |
1211 | ||
0da46a6e | 1212 | /* do_sendto() Must return target values and target errnos. */ |
992f48a0 BS |
1213 | static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, |
1214 | abi_ulong target_addr, socklen_t addrlen) | |
1be9e1dc PB |
1215 | { |
1216 | void *addr; | |
1217 | void *host_msg; | |
992f48a0 | 1218 | abi_long ret; |
1be9e1dc | 1219 | |
579a97f7 FB |
1220 | host_msg = lock_user(VERIFY_READ, msg, len, 1); |
1221 | if (!host_msg) | |
1222 | return -TARGET_EFAULT; | |
1be9e1dc PB |
1223 | if (target_addr) { |
1224 | addr = alloca(addrlen); | |
1225 | target_to_host_sockaddr(addr, target_addr, addrlen); | |
1226 | ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen)); | |
1227 | } else { | |
1228 | ret = get_errno(send(fd, host_msg, len, flags)); | |
1229 | } | |
1230 | unlock_user(host_msg, msg, 0); | |
1231 | return ret; | |
1232 | } | |
1233 | ||
0da46a6e | 1234 | /* do_recvfrom() Must return target values and target errnos. */ |
992f48a0 BS |
1235 | static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags, |
1236 | abi_ulong target_addr, | |
1237 | abi_ulong target_addrlen) | |
1be9e1dc PB |
1238 | { |
1239 | socklen_t addrlen; | |
1240 | void *addr; | |
1241 | void *host_msg; | |
992f48a0 | 1242 | abi_long ret; |
1be9e1dc | 1243 | |
579a97f7 FB |
1244 | host_msg = lock_user(VERIFY_WRITE, msg, len, 0); |
1245 | if (!host_msg) | |
1246 | return -TARGET_EFAULT; | |
1be9e1dc PB |
1247 | if (target_addr) { |
1248 | addrlen = tget32(target_addrlen); | |
1249 | addr = alloca(addrlen); | |
1250 | ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen)); | |
1251 | } else { | |
1252 | addr = NULL; /* To keep compiler quiet. */ | |
1253 | ret = get_errno(recv(fd, host_msg, len, flags)); | |
1254 | } | |
1255 | if (!is_error(ret)) { | |
1256 | if (target_addr) { | |
1257 | host_to_target_sockaddr(target_addr, addr, addrlen); | |
1258 | tput32(target_addrlen, addrlen); | |
1259 | } | |
1260 | unlock_user(host_msg, msg, len); | |
1261 | } else { | |
1262 | unlock_user(host_msg, msg, 0); | |
1263 | } | |
1264 | return ret; | |
1265 | } | |
1266 | ||
32407103 | 1267 | #ifdef TARGET_NR_socketcall |
0da46a6e | 1268 | /* do_socketcall() Must return target values and target errnos. */ |
992f48a0 | 1269 | static abi_long do_socketcall(int num, abi_ulong vptr) |
31e31b8a | 1270 | { |
992f48a0 BS |
1271 | abi_long ret; |
1272 | const int n = sizeof(abi_ulong); | |
31e31b8a FB |
1273 | |
1274 | switch(num) { | |
1275 | case SOCKOP_socket: | |
7854b056 | 1276 | { |
53a5960a PB |
1277 | int domain = tgetl(vptr); |
1278 | int type = tgetl(vptr + n); | |
1279 | int protocol = tgetl(vptr + 2 * n); | |
3532fa74 | 1280 | ret = do_socket(domain, type, protocol); |
7854b056 | 1281 | } |
31e31b8a FB |
1282 | break; |
1283 | case SOCKOP_bind: | |
7854b056 | 1284 | { |
53a5960a | 1285 | int sockfd = tgetl(vptr); |
992f48a0 | 1286 | abi_ulong target_addr = tgetl(vptr + n); |
53a5960a | 1287 | socklen_t addrlen = tgetl(vptr + 2 * n); |
3532fa74 | 1288 | ret = do_bind(sockfd, target_addr, addrlen); |
7854b056 | 1289 | } |
31e31b8a FB |
1290 | break; |
1291 | case SOCKOP_connect: | |
7854b056 | 1292 | { |
53a5960a | 1293 | int sockfd = tgetl(vptr); |
992f48a0 | 1294 | abi_ulong target_addr = tgetl(vptr + n); |
53a5960a | 1295 | socklen_t addrlen = tgetl(vptr + 2 * n); |
3532fa74 | 1296 | ret = do_connect(sockfd, target_addr, addrlen); |
7854b056 | 1297 | } |
31e31b8a FB |
1298 | break; |
1299 | case SOCKOP_listen: | |
7854b056 | 1300 | { |
53a5960a PB |
1301 | int sockfd = tgetl(vptr); |
1302 | int backlog = tgetl(vptr + n); | |
7854b056 FB |
1303 | ret = get_errno(listen(sockfd, backlog)); |
1304 | } | |
31e31b8a FB |
1305 | break; |
1306 | case SOCKOP_accept: | |
1307 | { | |
53a5960a | 1308 | int sockfd = tgetl(vptr); |
992f48a0 BS |
1309 | abi_ulong target_addr = tgetl(vptr + n); |
1310 | abi_ulong target_addrlen = tgetl(vptr + 2 * n); | |
1be9e1dc | 1311 | ret = do_accept(sockfd, target_addr, target_addrlen); |
31e31b8a FB |
1312 | } |
1313 | break; | |
1314 | case SOCKOP_getsockname: | |
1315 | { | |
53a5960a | 1316 | int sockfd = tgetl(vptr); |
992f48a0 BS |
1317 | abi_ulong target_addr = tgetl(vptr + n); |
1318 | abi_ulong target_addrlen = tgetl(vptr + 2 * n); | |
1be9e1dc | 1319 | ret = do_getsockname(sockfd, target_addr, target_addrlen); |
31e31b8a FB |
1320 | } |
1321 | break; | |
1322 | case SOCKOP_getpeername: | |
1323 | { | |
53a5960a | 1324 | int sockfd = tgetl(vptr); |
992f48a0 BS |
1325 | abi_ulong target_addr = tgetl(vptr + n); |
1326 | abi_ulong target_addrlen = tgetl(vptr + 2 * n); | |
1be9e1dc | 1327 | ret = do_getpeername(sockfd, target_addr, target_addrlen); |
31e31b8a FB |
1328 | } |
1329 | break; | |
1330 | case SOCKOP_socketpair: | |
1331 | { | |
53a5960a PB |
1332 | int domain = tgetl(vptr); |
1333 | int type = tgetl(vptr + n); | |
1334 | int protocol = tgetl(vptr + 2 * n); | |
992f48a0 | 1335 | abi_ulong tab = tgetl(vptr + 3 * n); |
1be9e1dc | 1336 | ret = do_socketpair(domain, type, protocol, tab); |
31e31b8a FB |
1337 | } |
1338 | break; | |
1339 | case SOCKOP_send: | |
7854b056 | 1340 | { |
53a5960a | 1341 | int sockfd = tgetl(vptr); |
992f48a0 | 1342 | abi_ulong msg = tgetl(vptr + n); |
53a5960a PB |
1343 | size_t len = tgetl(vptr + 2 * n); |
1344 | int flags = tgetl(vptr + 3 * n); | |
1be9e1dc | 1345 | ret = do_sendto(sockfd, msg, len, flags, 0, 0); |
7854b056 | 1346 | } |
31e31b8a FB |
1347 | break; |
1348 | case SOCKOP_recv: | |
7854b056 | 1349 | { |
53a5960a | 1350 | int sockfd = tgetl(vptr); |
992f48a0 | 1351 | abi_ulong msg = tgetl(vptr + n); |
53a5960a PB |
1352 | size_t len = tgetl(vptr + 2 * n); |
1353 | int flags = tgetl(vptr + 3 * n); | |
1be9e1dc | 1354 | ret = do_recvfrom(sockfd, msg, len, flags, 0, 0); |
7854b056 | 1355 | } |
31e31b8a FB |
1356 | break; |
1357 | case SOCKOP_sendto: | |
7854b056 | 1358 | { |
53a5960a | 1359 | int sockfd = tgetl(vptr); |
992f48a0 | 1360 | abi_ulong msg = tgetl(vptr + n); |
53a5960a PB |
1361 | size_t len = tgetl(vptr + 2 * n); |
1362 | int flags = tgetl(vptr + 3 * n); | |
992f48a0 | 1363 | abi_ulong addr = tgetl(vptr + 4 * n); |
53a5960a | 1364 | socklen_t addrlen = tgetl(vptr + 5 * n); |
1be9e1dc | 1365 | ret = do_sendto(sockfd, msg, len, flags, addr, addrlen); |
7854b056 | 1366 | } |
31e31b8a FB |
1367 | break; |
1368 | case SOCKOP_recvfrom: | |
1369 | { | |
53a5960a | 1370 | int sockfd = tgetl(vptr); |
992f48a0 | 1371 | abi_ulong msg = tgetl(vptr + n); |
53a5960a PB |
1372 | size_t len = tgetl(vptr + 2 * n); |
1373 | int flags = tgetl(vptr + 3 * n); | |
992f48a0 BS |
1374 | abi_ulong addr = tgetl(vptr + 4 * n); |
1375 | abi_ulong addrlen = tgetl(vptr + 5 * n); | |
1be9e1dc | 1376 | ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen); |
31e31b8a FB |
1377 | } |
1378 | break; | |
1379 | case SOCKOP_shutdown: | |
7854b056 | 1380 | { |
53a5960a PB |
1381 | int sockfd = tgetl(vptr); |
1382 | int how = tgetl(vptr + n); | |
7854b056 FB |
1383 | |
1384 | ret = get_errno(shutdown(sockfd, how)); | |
1385 | } | |
31e31b8a FB |
1386 | break; |
1387 | case SOCKOP_sendmsg: | |
1388 | case SOCKOP_recvmsg: | |
1a9353d2 FB |
1389 | { |
1390 | int fd; | |
992f48a0 | 1391 | abi_ulong target_msg; |
3532fa74 | 1392 | int flags; |
1a9353d2 | 1393 | |
53a5960a | 1394 | fd = tgetl(vptr); |
3532fa74 | 1395 | target_msg = tgetl(vptr + n); |
53a5960a | 1396 | flags = tgetl(vptr + 2 * n); |
3532fa74 | 1397 | |
5fafdf24 | 1398 | ret = do_sendrecvmsg(fd, target_msg, flags, |
3532fa74 | 1399 | (num == SOCKOP_sendmsg)); |
1a9353d2 FB |
1400 | } |
1401 | break; | |
31e31b8a | 1402 | case SOCKOP_setsockopt: |
7854b056 | 1403 | { |
53a5960a PB |
1404 | int sockfd = tgetl(vptr); |
1405 | int level = tgetl(vptr + n); | |
1406 | int optname = tgetl(vptr + 2 * n); | |
992f48a0 | 1407 | abi_ulong optval = tgetl(vptr + 3 * n); |
53a5960a | 1408 | socklen_t optlen = tgetl(vptr + 4 * n); |
7854b056 FB |
1409 | |
1410 | ret = do_setsockopt(sockfd, level, optname, optval, optlen); | |
1411 | } | |
1412 | break; | |
31e31b8a | 1413 | case SOCKOP_getsockopt: |
7854b056 | 1414 | { |
53a5960a PB |
1415 | int sockfd = tgetl(vptr); |
1416 | int level = tgetl(vptr + n); | |
1417 | int optname = tgetl(vptr + 2 * n); | |
992f48a0 BS |
1418 | abi_ulong optval = tgetl(vptr + 3 * n); |
1419 | abi_ulong poptlen = tgetl(vptr + 4 * n); | |
7854b056 | 1420 | |
8853f86e | 1421 | ret = do_getsockopt(sockfd, level, optname, optval, poptlen); |
7854b056 FB |
1422 | } |
1423 | break; | |
31e31b8a FB |
1424 | default: |
1425 | gemu_log("Unsupported socketcall: %d\n", num); | |
0da46a6e | 1426 | ret = -TARGET_ENOSYS; |
31e31b8a FB |
1427 | break; |
1428 | } | |
1429 | return ret; | |
1430 | } | |
32407103 | 1431 | #endif |
31e31b8a | 1432 | |
32407103 | 1433 | #ifdef TARGET_NR_ipc |
8853f86e FB |
1434 | #define N_SHM_REGIONS 32 |
1435 | ||
1436 | static struct shm_region { | |
5a4a898d FB |
1437 | abi_ulong start; |
1438 | abi_ulong size; | |
8853f86e FB |
1439 | } shm_regions[N_SHM_REGIONS]; |
1440 | ||
3eb6b044 TS |
1441 | struct target_ipc_perm |
1442 | { | |
992f48a0 BS |
1443 | abi_long __key; |
1444 | abi_ulong uid; | |
1445 | abi_ulong gid; | |
1446 | abi_ulong cuid; | |
1447 | abi_ulong cgid; | |
3eb6b044 TS |
1448 | unsigned short int mode; |
1449 | unsigned short int __pad1; | |
1450 | unsigned short int __seq; | |
1451 | unsigned short int __pad2; | |
992f48a0 BS |
1452 | abi_ulong __unused1; |
1453 | abi_ulong __unused2; | |
3eb6b044 TS |
1454 | }; |
1455 | ||
1456 | struct target_semid_ds | |
1457 | { | |
1458 | struct target_ipc_perm sem_perm; | |
992f48a0 BS |
1459 | abi_ulong sem_otime; |
1460 | abi_ulong __unused1; | |
1461 | abi_ulong sem_ctime; | |
1462 | abi_ulong __unused2; | |
1463 | abi_ulong sem_nsems; | |
1464 | abi_ulong __unused3; | |
1465 | abi_ulong __unused4; | |
3eb6b044 TS |
1466 | }; |
1467 | ||
579a97f7 FB |
1468 | static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip, |
1469 | abi_ulong target_addr) | |
3eb6b044 TS |
1470 | { |
1471 | struct target_ipc_perm *target_ip; | |
1472 | struct target_semid_ds *target_sd; | |
1473 | ||
579a97f7 FB |
1474 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
1475 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1476 | target_ip=&(target_sd->sem_perm); |
1477 | host_ip->__key = tswapl(target_ip->__key); | |
1478 | host_ip->uid = tswapl(target_ip->uid); | |
1479 | host_ip->gid = tswapl(target_ip->gid); | |
1480 | host_ip->cuid = tswapl(target_ip->cuid); | |
1481 | host_ip->cgid = tswapl(target_ip->cgid); | |
1482 | host_ip->mode = tswapl(target_ip->mode); | |
1483 | unlock_user_struct(target_sd, target_addr, 0); | |
579a97f7 | 1484 | return 0; |
3eb6b044 TS |
1485 | } |
1486 | ||
579a97f7 FB |
1487 | static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr, |
1488 | struct ipc_perm *host_ip) | |
3eb6b044 TS |
1489 | { |
1490 | struct target_ipc_perm *target_ip; | |
1491 | struct target_semid_ds *target_sd; | |
1492 | ||
579a97f7 FB |
1493 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
1494 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1495 | target_ip = &(target_sd->sem_perm); |
1496 | target_ip->__key = tswapl(host_ip->__key); | |
1497 | target_ip->uid = tswapl(host_ip->uid); | |
1498 | target_ip->gid = tswapl(host_ip->gid); | |
1499 | target_ip->cuid = tswapl(host_ip->cuid); | |
1500 | target_ip->cgid = tswapl(host_ip->cgid); | |
1501 | target_ip->mode = tswapl(host_ip->mode); | |
1502 | unlock_user_struct(target_sd, target_addr, 1); | |
579a97f7 | 1503 | return 0; |
3eb6b044 TS |
1504 | } |
1505 | ||
579a97f7 FB |
1506 | static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd, |
1507 | abi_ulong target_addr) | |
3eb6b044 TS |
1508 | { |
1509 | struct target_semid_ds *target_sd; | |
1510 | ||
579a97f7 FB |
1511 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
1512 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1513 | target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr); |
1514 | host_sd->sem_nsems = tswapl(target_sd->sem_nsems); | |
1515 | host_sd->sem_otime = tswapl(target_sd->sem_otime); | |
1516 | host_sd->sem_ctime = tswapl(target_sd->sem_ctime); | |
1517 | unlock_user_struct(target_sd, target_addr, 0); | |
579a97f7 | 1518 | return 0; |
3eb6b044 TS |
1519 | } |
1520 | ||
579a97f7 FB |
1521 | static inline abi_long host_to_target_semid_ds(abi_ulong target_addr, |
1522 | struct semid_ds *host_sd) | |
3eb6b044 TS |
1523 | { |
1524 | struct target_semid_ds *target_sd; | |
1525 | ||
579a97f7 FB |
1526 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
1527 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1528 | host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)); |
1529 | target_sd->sem_nsems = tswapl(host_sd->sem_nsems); | |
1530 | target_sd->sem_otime = tswapl(host_sd->sem_otime); | |
1531 | target_sd->sem_ctime = tswapl(host_sd->sem_ctime); | |
1532 | unlock_user_struct(target_sd, target_addr, 1); | |
579a97f7 | 1533 | return 0; |
3eb6b044 TS |
1534 | } |
1535 | ||
fa294816 TS |
1536 | union semun { |
1537 | int val; | |
3eb6b044 | 1538 | struct semid_ds *buf; |
fa294816 TS |
1539 | unsigned short *array; |
1540 | }; | |
1541 | ||
3eb6b044 TS |
1542 | union target_semun { |
1543 | int val; | |
992f48a0 | 1544 | abi_long buf; |
3eb6b044 TS |
1545 | unsigned short int *array; |
1546 | }; | |
1547 | ||
579a97f7 FB |
1548 | static inline abi_long target_to_host_semun(int cmd, |
1549 | union semun *host_su, | |
1550 | abi_ulong target_addr, | |
1551 | struct semid_ds *ds) | |
3eb6b044 TS |
1552 | { |
1553 | union target_semun *target_su; | |
1554 | ||
1555 | switch( cmd ) { | |
1556 | case IPC_STAT: | |
1557 | case IPC_SET: | |
579a97f7 FB |
1558 | if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1)) |
1559 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1560 | target_to_host_semid_ds(ds,target_su->buf); |
1561 | host_su->buf = ds; | |
1562 | unlock_user_struct(target_su, target_addr, 0); | |
1563 | break; | |
1564 | case GETVAL: | |
1565 | case SETVAL: | |
579a97f7 FB |
1566 | if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1)) |
1567 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1568 | host_su->val = tswapl(target_su->val); |
1569 | unlock_user_struct(target_su, target_addr, 0); | |
1570 | break; | |
1571 | case GETALL: | |
1572 | case SETALL: | |
579a97f7 FB |
1573 | if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1)) |
1574 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1575 | *host_su->array = tswap16(*target_su->array); |
1576 | unlock_user_struct(target_su, target_addr, 0); | |
1577 | break; | |
1578 | default: | |
1579 | gemu_log("semun operation not fully supported: %d\n", (int)cmd); | |
1580 | } | |
579a97f7 | 1581 | return 0; |
3eb6b044 TS |
1582 | } |
1583 | ||
579a97f7 FB |
1584 | static inline abi_long host_to_target_semun(int cmd, |
1585 | abi_ulong target_addr, | |
1586 | union semun *host_su, | |
1587 | struct semid_ds *ds) | |
3eb6b044 TS |
1588 | { |
1589 | union target_semun *target_su; | |
1590 | ||
1591 | switch( cmd ) { | |
1592 | case IPC_STAT: | |
1593 | case IPC_SET: | |
579a97f7 FB |
1594 | if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0)) |
1595 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1596 | host_to_target_semid_ds(target_su->buf,ds); |
1597 | unlock_user_struct(target_su, target_addr, 1); | |
1598 | break; | |
1599 | case GETVAL: | |
1600 | case SETVAL: | |
579a97f7 FB |
1601 | if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0)) |
1602 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1603 | target_su->val = tswapl(host_su->val); |
1604 | unlock_user_struct(target_su, target_addr, 1); | |
1605 | break; | |
1606 | case GETALL: | |
1607 | case SETALL: | |
579a97f7 FB |
1608 | if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0)) |
1609 | return -TARGET_EFAULT; | |
3eb6b044 TS |
1610 | *target_su->array = tswap16(*host_su->array); |
1611 | unlock_user_struct(target_su, target_addr, 1); | |
1612 | break; | |
1613 | default: | |
1614 | gemu_log("semun operation not fully supported: %d\n", (int)cmd); | |
1615 | } | |
579a97f7 | 1616 | return 0; |
3eb6b044 TS |
1617 | } |
1618 | ||
992f48a0 BS |
1619 | static inline abi_long do_semctl(int first, int second, int third, |
1620 | abi_long ptr) | |
3eb6b044 TS |
1621 | { |
1622 | union semun arg; | |
1623 | struct semid_ds dsarg; | |
1624 | int cmd = third&0xff; | |
992f48a0 | 1625 | abi_long ret = 0; |
3eb6b044 TS |
1626 | |
1627 | switch( cmd ) { | |
1628 | case GETVAL: | |
1629 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1630 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1631 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1632 | break; | |
1633 | case SETVAL: | |
1634 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1635 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1636 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1637 | break; | |
1638 | case GETALL: | |
1639 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1640 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1641 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1642 | break; | |
1643 | case SETALL: | |
1644 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1645 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1646 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1647 | break; | |
1648 | case IPC_STAT: | |
1649 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1650 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1651 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1652 | break; | |
1653 | case IPC_SET: | |
1654 | target_to_host_semun(cmd,&arg,ptr,&dsarg); | |
1655 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1656 | host_to_target_semun(cmd,ptr,&arg,&dsarg); | |
1657 | break; | |
1658 | default: | |
1659 | ret = get_errno(semctl(first, second, cmd, arg)); | |
1660 | } | |
1661 | ||
1662 | return ret; | |
1663 | } | |
1664 | ||
1bc012f6 TS |
1665 | struct target_msqid_ds |
1666 | { | |
1667 | struct target_ipc_perm msg_perm; | |
992f48a0 BS |
1668 | abi_ulong msg_stime; |
1669 | abi_ulong __unused1; | |
1670 | abi_ulong msg_rtime; | |
1671 | abi_ulong __unused2; | |
1672 | abi_ulong msg_ctime; | |
1673 | abi_ulong __unused3; | |
1674 | abi_ulong __msg_cbytes; | |
1675 | abi_ulong msg_qnum; | |
1676 | abi_ulong msg_qbytes; | |
1677 | abi_ulong msg_lspid; | |
1678 | abi_ulong msg_lrpid; | |
1679 | abi_ulong __unused4; | |
1680 | abi_ulong __unused5; | |
1bc012f6 TS |
1681 | }; |
1682 | ||
579a97f7 FB |
1683 | static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md, |
1684 | abi_ulong target_addr) | |
1bc012f6 TS |
1685 | { |
1686 | struct target_msqid_ds *target_md; | |
1687 | ||
579a97f7 FB |
1688 | if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1)) |
1689 | return -TARGET_EFAULT; | |
1bc012f6 TS |
1690 | target_to_host_ipc_perm(&(host_md->msg_perm),target_addr); |
1691 | host_md->msg_stime = tswapl(target_md->msg_stime); | |
1692 | host_md->msg_rtime = tswapl(target_md->msg_rtime); | |
1693 | host_md->msg_ctime = tswapl(target_md->msg_ctime); | |
1694 | host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes); | |
1695 | host_md->msg_qnum = tswapl(target_md->msg_qnum); | |
1696 | host_md->msg_qbytes = tswapl(target_md->msg_qbytes); | |
1697 | host_md->msg_lspid = tswapl(target_md->msg_lspid); | |
1698 | host_md->msg_lrpid = tswapl(target_md->msg_lrpid); | |
1699 | unlock_user_struct(target_md, target_addr, 0); | |
579a97f7 | 1700 | return 0; |
1bc012f6 TS |
1701 | } |
1702 | ||
579a97f7 FB |
1703 | static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr, |
1704 | struct msqid_ds *host_md) | |
1bc012f6 TS |
1705 | { |
1706 | struct target_msqid_ds *target_md; | |
1707 | ||
579a97f7 FB |
1708 | if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) |
1709 | return -TARGET_EFAULT; | |
1bc012f6 TS |
1710 | host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)); |
1711 | target_md->msg_stime = tswapl(host_md->msg_stime); | |
1712 | target_md->msg_rtime = tswapl(host_md->msg_rtime); | |
1713 | target_md->msg_ctime = tswapl(host_md->msg_ctime); | |
1714 | target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes); | |
1715 | target_md->msg_qnum = tswapl(host_md->msg_qnum); | |
1716 | target_md->msg_qbytes = tswapl(host_md->msg_qbytes); | |
1717 | target_md->msg_lspid = tswapl(host_md->msg_lspid); | |
1718 | target_md->msg_lrpid = tswapl(host_md->msg_lrpid); | |
1719 | unlock_user_struct(target_md, target_addr, 1); | |
579a97f7 | 1720 | return 0; |
1bc012f6 TS |
1721 | } |
1722 | ||
992f48a0 | 1723 | static inline abi_long do_msgctl(int first, int second, abi_long ptr) |
1bc012f6 TS |
1724 | { |
1725 | struct msqid_ds dsarg; | |
1726 | int cmd = second&0xff; | |
992f48a0 | 1727 | abi_long ret = 0; |
1bc012f6 TS |
1728 | switch( cmd ) { |
1729 | case IPC_STAT: | |
1730 | case IPC_SET: | |
1731 | target_to_host_msqid_ds(&dsarg,ptr); | |
1732 | ret = get_errno(msgctl(first, cmd, &dsarg)); | |
1733 | host_to_target_msqid_ds(ptr,&dsarg); | |
1734 | default: | |
1735 | ret = get_errno(msgctl(first, cmd, &dsarg)); | |
1736 | } | |
1737 | return ret; | |
1738 | } | |
1739 | ||
1740 | struct target_msgbuf { | |
992f48a0 | 1741 | abi_ulong mtype; |
1bc012f6 TS |
1742 | char mtext[1]; |
1743 | }; | |
1744 | ||
992f48a0 BS |
1745 | static inline abi_long do_msgsnd(int msqid, abi_long msgp, |
1746 | unsigned int msgsz, int msgflg) | |
1bc012f6 TS |
1747 | { |
1748 | struct target_msgbuf *target_mb; | |
1749 | struct msgbuf *host_mb; | |
992f48a0 | 1750 | abi_long ret = 0; |
1bc012f6 | 1751 | |
579a97f7 FB |
1752 | if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) |
1753 | return -TARGET_EFAULT; | |
1bc012f6 TS |
1754 | host_mb = malloc(msgsz+sizeof(long)); |
1755 | host_mb->mtype = tswapl(target_mb->mtype); | |
1756 | memcpy(host_mb->mtext,target_mb->mtext,msgsz); | |
1757 | ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg)); | |
1758 | free(host_mb); | |
1759 | unlock_user_struct(target_mb, msgp, 0); | |
1760 | ||
1761 | return ret; | |
1762 | } | |
1763 | ||
992f48a0 BS |
1764 | static inline abi_long do_msgrcv(int msqid, abi_long msgp, |
1765 | unsigned int msgsz, int msgtype, | |
1766 | int msgflg) | |
1bc012f6 TS |
1767 | { |
1768 | struct target_msgbuf *target_mb; | |
579a97f7 | 1769 | char *target_mtext; |
1bc012f6 | 1770 | struct msgbuf *host_mb; |
992f48a0 | 1771 | abi_long ret = 0; |
1bc012f6 | 1772 | |
579a97f7 FB |
1773 | if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0)) |
1774 | return -TARGET_EFAULT; | |
1bc012f6 TS |
1775 | host_mb = malloc(msgsz+sizeof(long)); |
1776 | ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg)); | |
579a97f7 FB |
1777 | if (ret > 0) { |
1778 | abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong); | |
1779 | target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0); | |
1780 | if (!target_mtext) { | |
1781 | ret = -TARGET_EFAULT; | |
1782 | goto end; | |
1783 | } | |
1bc012f6 | 1784 | memcpy(target_mb->mtext, host_mb->mtext, ret); |
579a97f7 FB |
1785 | unlock_user(target_mtext, target_mtext_addr, ret); |
1786 | } | |
1bc012f6 TS |
1787 | target_mb->mtype = tswapl(host_mb->mtype); |
1788 | free(host_mb); | |
1bc012f6 | 1789 | |
579a97f7 FB |
1790 | end: |
1791 | if (target_mb) | |
1792 | unlock_user_struct(target_mb, msgp, 1); | |
1bc012f6 TS |
1793 | return ret; |
1794 | } | |
1795 | ||
53a5960a | 1796 | /* ??? This only works with linear mappings. */ |
0da46a6e | 1797 | /* do_ipc() must return target values and target errnos. */ |
992f48a0 BS |
1798 | static abi_long do_ipc(unsigned int call, int first, |
1799 | int second, int third, | |
1800 | abi_long ptr, abi_long fifth) | |
8853f86e FB |
1801 | { |
1802 | int version; | |
992f48a0 | 1803 | abi_long ret = 0; |
8853f86e FB |
1804 | struct shmid_ds shm_info; |
1805 | int i; | |
1806 | ||
1807 | version = call >> 16; | |
1808 | call &= 0xffff; | |
1809 | ||
1810 | switch (call) { | |
fa294816 | 1811 | case IPCOP_semop: |
579a97f7 | 1812 | ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second)); |
fa294816 TS |
1813 | break; |
1814 | ||
1815 | case IPCOP_semget: | |
1816 | ret = get_errno(semget(first, second, third)); | |
1817 | break; | |
1818 | ||
1819 | case IPCOP_semctl: | |
3eb6b044 | 1820 | ret = do_semctl(first, second, third, ptr); |
fa294816 TS |
1821 | break; |
1822 | ||
1823 | case IPCOP_semtimedop: | |
32407103 | 1824 | gemu_log("Unsupported ipc call: %d (version %d)\n", call, version); |
0da46a6e | 1825 | ret = -TARGET_ENOSYS; |
fa294816 | 1826 | break; |
d96372ef TS |
1827 | |
1828 | case IPCOP_msgget: | |
1829 | ret = get_errno(msgget(first, second)); | |
1830 | break; | |
1831 | ||
1832 | case IPCOP_msgsnd: | |
1bc012f6 | 1833 | ret = do_msgsnd(first, ptr, second, third); |
d96372ef TS |
1834 | break; |
1835 | ||
1836 | case IPCOP_msgctl: | |
1bc012f6 | 1837 | ret = do_msgctl(first, second, ptr); |
d96372ef TS |
1838 | break; |
1839 | ||
1840 | case IPCOP_msgrcv: | |
1bc012f6 | 1841 | { |
579a97f7 | 1842 | /* XXX: this code is not correct */ |
1bc012f6 TS |
1843 | struct ipc_kludge |
1844 | { | |
1845 | void *__unbounded msgp; | |
1846 | long int msgtyp; | |
1847 | }; | |
d96372ef | 1848 | |
579a97f7 | 1849 | struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr); |
1bc012f6 | 1850 | struct msgbuf *msgp = (struct msgbuf *) foo->msgp; |
d96372ef | 1851 | |
1bc012f6 | 1852 | ret = do_msgrcv(first, (long)msgp, second, 0, third); |
d96372ef | 1853 | |
1bc012f6 | 1854 | } |
d96372ef TS |
1855 | break; |
1856 | ||
8853f86e | 1857 | case IPCOP_shmat: |
5a4a898d FB |
1858 | { |
1859 | abi_ulong raddr; | |
1860 | void *host_addr; | |
1861 | /* SHM_* flags are the same on all linux platforms */ | |
1862 | host_addr = shmat(first, (void *)g2h(ptr), second); | |
1863 | if (host_addr == (void *)-1) { | |
1864 | ret = get_errno((long)host_addr); | |
8853f86e | 1865 | break; |
5a4a898d FB |
1866 | } |
1867 | raddr = h2g((unsigned long)host_addr); | |
1868 | /* find out the length of the shared memory segment */ | |
1869 | ||
1870 | ret = get_errno(shmctl(first, IPC_STAT, &shm_info)); | |
1871 | if (is_error(ret)) { | |
1872 | /* can't get length, bail out */ | |
1873 | shmdt(host_addr); | |
1874 | break; | |
1875 | } | |
1876 | page_set_flags(raddr, raddr + shm_info.shm_segsz, | |
1877 | PAGE_VALID | PAGE_READ | | |
1878 | ((second & SHM_RDONLY)? 0: PAGE_WRITE)); | |
1879 | for (i = 0; i < N_SHM_REGIONS; ++i) { | |
1880 | if (shm_regions[i].start == 0) { | |
1881 | shm_regions[i].start = raddr; | |
1882 | shm_regions[i].size = shm_info.shm_segsz; | |
1883 | break; | |
1884 | } | |
1885 | } | |
1886 | if (put_user(raddr, third, abi_ulong)) | |
1887 | return -TARGET_EFAULT; | |
1888 | ret = 0; | |
1889 | } | |
8853f86e FB |
1890 | break; |
1891 | case IPCOP_shmdt: | |
1892 | for (i = 0; i < N_SHM_REGIONS; ++i) { | |
1893 | if (shm_regions[i].start == ptr) { | |
1894 | shm_regions[i].start = 0; | |
1895 | page_set_flags(ptr, shm_regions[i].size, 0); | |
1896 | break; | |
1897 | } | |
1898 | } | |
5a4a898d | 1899 | ret = get_errno(shmdt((void *)g2h(ptr))); |
8853f86e FB |
1900 | break; |
1901 | ||
1902 | case IPCOP_shmget: | |
1903 | /* IPC_* flag values are the same on all linux platforms */ | |
1904 | ret = get_errno(shmget(first, second, third)); | |
1905 | break; | |
1906 | ||
1907 | /* IPC_* and SHM_* command values are the same on all linux platforms */ | |
1908 | case IPCOP_shmctl: | |
1909 | switch(second) { | |
1910 | case IPC_RMID: | |
1911 | case SHM_LOCK: | |
1912 | case SHM_UNLOCK: | |
1913 | ret = get_errno(shmctl(first, second, NULL)); | |
1914 | break; | |
1915 | default: | |
1916 | goto unimplemented; | |
1917 | } | |
1918 | break; | |
1919 | default: | |
1920 | unimplemented: | |
32407103 | 1921 | gemu_log("Unsupported ipc call: %d (version %d)\n", call, version); |
0da46a6e | 1922 | ret = -TARGET_ENOSYS; |
8853f86e FB |
1923 | break; |
1924 | } | |
1925 | return ret; | |
1926 | } | |
32407103 | 1927 | #endif |
8853f86e | 1928 | |
31e31b8a FB |
1929 | /* kernel structure types definitions */ |
1930 | #define IFNAMSIZ 16 | |
1931 | ||
1932 | #define STRUCT(name, list...) STRUCT_ ## name, | |
1933 | #define STRUCT_SPECIAL(name) STRUCT_ ## name, | |
1934 | enum { | |
1935 | #include "syscall_types.h" | |
1936 | }; | |
1937 | #undef STRUCT | |
1938 | #undef STRUCT_SPECIAL | |
1939 | ||
1940 | #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL }; | |
1941 | #define STRUCT_SPECIAL(name) | |
1942 | #include "syscall_types.h" | |
1943 | #undef STRUCT | |
1944 | #undef STRUCT_SPECIAL | |
1945 | ||
1946 | typedef struct IOCTLEntry { | |
2ab83ea7 FB |
1947 | unsigned int target_cmd; |
1948 | unsigned int host_cmd; | |
31e31b8a FB |
1949 | const char *name; |
1950 | int access; | |
1a9353d2 | 1951 | const argtype arg_type[5]; |
31e31b8a FB |
1952 | } IOCTLEntry; |
1953 | ||
1954 | #define IOC_R 0x0001 | |
1955 | #define IOC_W 0x0002 | |
1956 | #define IOC_RW (IOC_R | IOC_W) | |
1957 | ||
1958 | #define MAX_STRUCT_SIZE 4096 | |
1959 | ||
2ab83ea7 | 1960 | IOCTLEntry ioctl_entries[] = { |
31e31b8a FB |
1961 | #define IOCTL(cmd, access, types...) \ |
1962 | { TARGET_ ## cmd, cmd, #cmd, access, { types } }, | |
1963 | #include "ioctls.h" | |
1964 | { 0, 0, }, | |
1965 | }; | |
1966 | ||
53a5960a | 1967 | /* ??? Implement proper locking for ioctls. */ |
0da46a6e | 1968 | /* do_ioctl() Must return target values and target errnos. */ |
992f48a0 | 1969 | static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg) |
31e31b8a FB |
1970 | { |
1971 | const IOCTLEntry *ie; | |
1972 | const argtype *arg_type; | |
992f48a0 | 1973 | abi_long ret; |
31e31b8a | 1974 | uint8_t buf_temp[MAX_STRUCT_SIZE]; |
53a5960a PB |
1975 | int target_size; |
1976 | void *argptr; | |
31e31b8a FB |
1977 | |
1978 | ie = ioctl_entries; | |
1979 | for(;;) { | |
1980 | if (ie->target_cmd == 0) { | |
32407103 | 1981 | gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd); |
0da46a6e | 1982 | return -TARGET_ENOSYS; |
31e31b8a FB |
1983 | } |
1984 | if (ie->target_cmd == cmd) | |
1985 | break; | |
1986 | ie++; | |
1987 | } | |
1988 | arg_type = ie->arg_type; | |
9de5e440 | 1989 | #if defined(DEBUG) |
32407103 | 1990 | gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name); |
72f03900 | 1991 | #endif |
31e31b8a FB |
1992 | switch(arg_type[0]) { |
1993 | case TYPE_NULL: | |
1994 | /* no argument */ | |
1995 | ret = get_errno(ioctl(fd, ie->host_cmd)); | |
1996 | break; | |
1997 | case TYPE_PTRVOID: | |
1998 | case TYPE_INT: | |
1999 | /* int argment */ | |
2000 | ret = get_errno(ioctl(fd, ie->host_cmd, arg)); | |
2001 | break; | |
2002 | case TYPE_PTR: | |
2003 | arg_type++; | |
53a5960a | 2004 | target_size = thunk_type_size(arg_type, 0); |
31e31b8a FB |
2005 | switch(ie->access) { |
2006 | case IOC_R: | |
2007 | ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp)); | |
2008 | if (!is_error(ret)) { | |
579a97f7 FB |
2009 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
2010 | if (!argptr) | |
2011 | return -TARGET_EFAULT; | |
53a5960a PB |
2012 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
2013 | unlock_user(argptr, arg, target_size); | |
31e31b8a FB |
2014 | } |
2015 | break; | |
2016 | case IOC_W: | |
579a97f7 FB |
2017 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
2018 | if (!argptr) | |
2019 | return -TARGET_EFAULT; | |
53a5960a PB |
2020 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
2021 | unlock_user(argptr, arg, 0); | |
31e31b8a FB |
2022 | ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp)); |
2023 | break; | |
2024 | default: | |
2025 | case IOC_RW: | |
579a97f7 FB |
2026 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
2027 | if (!argptr) | |
2028 | return -TARGET_EFAULT; | |
53a5960a PB |
2029 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
2030 | unlock_user(argptr, arg, 0); | |
31e31b8a FB |
2031 | ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp)); |
2032 | if (!is_error(ret)) { | |
579a97f7 FB |
2033 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
2034 | if (!argptr) | |
2035 | return -TARGET_EFAULT; | |
53a5960a PB |
2036 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
2037 | unlock_user(argptr, arg, target_size); | |
31e31b8a FB |
2038 | } |
2039 | break; | |
2040 | } | |
2041 | break; | |
2042 | default: | |
32407103 JM |
2043 | gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", |
2044 | (long)cmd, arg_type[0]); | |
0da46a6e | 2045 | ret = -TARGET_ENOSYS; |
31e31b8a FB |
2046 | break; |
2047 | } | |
2048 | return ret; | |
2049 | } | |
2050 | ||
2051 | bitmask_transtbl iflag_tbl[] = { | |
2052 | { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK }, | |
2053 | { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT }, | |
2054 | { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR }, | |
2055 | { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK }, | |
2056 | { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK }, | |
2057 | { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP }, | |
2058 | { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR }, | |
2059 | { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR }, | |
2060 | { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL }, | |
2061 | { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC }, | |
2062 | { TARGET_IXON, TARGET_IXON, IXON, IXON }, | |
2063 | { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY }, | |
2064 | { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF }, | |
2065 | { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL }, | |
2066 | { 0, 0, 0, 0 } | |
2067 | }; | |
2068 | ||
2069 | bitmask_transtbl oflag_tbl[] = { | |
2070 | { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST }, | |
2071 | { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC }, | |
2072 | { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR }, | |
2073 | { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL }, | |
2074 | { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR }, | |
2075 | { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET }, | |
2076 | { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL }, | |
2077 | { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL }, | |
2078 | { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 }, | |
2079 | { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 }, | |
2080 | { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 }, | |
2081 | { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 }, | |
2082 | { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 }, | |
2083 | { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 }, | |
2084 | { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 }, | |
2085 | { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 }, | |
2086 | { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 }, | |
2087 | { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 }, | |
2088 | { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 }, | |
2089 | { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 }, | |
2090 | { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 }, | |
2091 | { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 }, | |
2092 | { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 }, | |
2093 | { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 }, | |
2094 | { 0, 0, 0, 0 } | |
2095 | }; | |
2096 | ||
2097 | bitmask_transtbl cflag_tbl[] = { | |
2098 | { TARGET_CBAUD, TARGET_B0, CBAUD, B0 }, | |
2099 | { TARGET_CBAUD, TARGET_B50, CBAUD, B50 }, | |
2100 | { TARGET_CBAUD, TARGET_B75, CBAUD, B75 }, | |
2101 | { TARGET_CBAUD, TARGET_B110, CBAUD, B110 }, | |
2102 | { TARGET_CBAUD, TARGET_B134, CBAUD, B134 }, | |
2103 | { TARGET_CBAUD, TARGET_B150, CBAUD, B150 }, | |
2104 | { TARGET_CBAUD, TARGET_B200, CBAUD, B200 }, | |
2105 | { TARGET_CBAUD, TARGET_B300, CBAUD, B300 }, | |
2106 | { TARGET_CBAUD, TARGET_B600, CBAUD, B600 }, | |
2107 | { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 }, | |
2108 | { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 }, | |
2109 | { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 }, | |
2110 | { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 }, | |
2111 | { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 }, | |
2112 | { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 }, | |
2113 | { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 }, | |
2114 | { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 }, | |
2115 | { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 }, | |
2116 | { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 }, | |
2117 | { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 }, | |
2118 | { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 }, | |
2119 | { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 }, | |
2120 | { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 }, | |
2121 | { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 }, | |
2122 | { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB }, | |
2123 | { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD }, | |
2124 | { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB }, | |
2125 | { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD }, | |
2126 | { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL }, | |
2127 | { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL }, | |
2128 | { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS }, | |
2129 | { 0, 0, 0, 0 } | |
2130 | }; | |
2131 | ||
2132 | bitmask_transtbl lflag_tbl[] = { | |
2133 | { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG }, | |
2134 | { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON }, | |
2135 | { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE }, | |
2136 | { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO }, | |
2137 | { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE }, | |
2138 | { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK }, | |
2139 | { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL }, | |
2140 | { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH }, | |
2141 | { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP }, | |
2142 | { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL }, | |
2143 | { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT }, | |
2144 | { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE }, | |
2145 | { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO }, | |
2146 | { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN }, | |
2147 | { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN }, | |
2148 | { 0, 0, 0, 0 } | |
2149 | }; | |
2150 | ||
2151 | static void target_to_host_termios (void *dst, const void *src) | |
2152 | { | |
2153 | struct host_termios *host = dst; | |
2154 | const struct target_termios *target = src; | |
3b46e624 | 2155 | |
5fafdf24 | 2156 | host->c_iflag = |
31e31b8a | 2157 | target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl); |
5fafdf24 | 2158 | host->c_oflag = |
31e31b8a | 2159 | target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl); |
5fafdf24 | 2160 | host->c_cflag = |
31e31b8a | 2161 | target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl); |
5fafdf24 | 2162 | host->c_lflag = |
31e31b8a FB |
2163 | target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl); |
2164 | host->c_line = target->c_line; | |
3b46e624 | 2165 | |
5fafdf24 TS |
2166 | host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; |
2167 | host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; | |
3b46e624 | 2168 | host->c_cc[VERASE] = target->c_cc[TARGET_VERASE]; |
5fafdf24 | 2169 | host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; |
3b46e624 | 2170 | host->c_cc[VEOF] = target->c_cc[TARGET_VEOF]; |
5fafdf24 | 2171 | host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; |
3b46e624 | 2172 | host->c_cc[VMIN] = target->c_cc[TARGET_VMIN]; |
5fafdf24 | 2173 | host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; |
3b46e624 | 2174 | host->c_cc[VSTART] = target->c_cc[TARGET_VSTART]; |
5fafdf24 TS |
2175 | host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; |
2176 | host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; | |
3b46e624 TS |
2177 | host->c_cc[VEOL] = target->c_cc[TARGET_VEOL]; |
2178 | host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT]; | |
2179 | host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD]; | |
2180 | host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE]; | |
2181 | host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT]; | |
5fafdf24 | 2182 | host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; |
31e31b8a | 2183 | } |
3b46e624 | 2184 | |
31e31b8a FB |
2185 | static void host_to_target_termios (void *dst, const void *src) |
2186 | { | |
2187 | struct target_termios *target = dst; | |
2188 | const struct host_termios *host = src; | |
2189 | ||
5fafdf24 | 2190 | target->c_iflag = |
31e31b8a | 2191 | tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl)); |
5fafdf24 | 2192 | target->c_oflag = |
31e31b8a | 2193 | tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl)); |
5fafdf24 | 2194 | target->c_cflag = |
31e31b8a | 2195 | tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl)); |
5fafdf24 | 2196 | target->c_lflag = |
31e31b8a FB |
2197 | tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl)); |
2198 | target->c_line = host->c_line; | |
3b46e624 | 2199 | |
31e31b8a FB |
2200 | target->c_cc[TARGET_VINTR] = host->c_cc[VINTR]; |
2201 | target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT]; | |
2202 | target->c_cc[TARGET_VERASE] = host->c_cc[VERASE]; | |
2203 | target->c_cc[TARGET_VKILL] = host->c_cc[VKILL]; | |
2204 | target->c_cc[TARGET_VEOF] = host->c_cc[VEOF]; | |
2205 | target->c_cc[TARGET_VTIME] = host->c_cc[VTIME]; | |
2206 | target->c_cc[TARGET_VMIN] = host->c_cc[VMIN]; | |
2207 | target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC]; | |
2208 | target->c_cc[TARGET_VSTART] = host->c_cc[VSTART]; | |
2209 | target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP]; | |
2210 | target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP]; | |
2211 | target->c_cc[TARGET_VEOL] = host->c_cc[VEOL]; | |
2212 | target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT]; | |
2213 | target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD]; | |
2214 | target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE]; | |
2215 | target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT]; | |
2216 | target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2]; | |
2217 | } | |
2218 | ||
2219 | StructEntry struct_termios_def = { | |
2220 | .convert = { host_to_target_termios, target_to_host_termios }, | |
2221 | .size = { sizeof(struct target_termios), sizeof(struct host_termios) }, | |
2222 | .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) }, | |
2223 | }; | |
2224 | ||
5286db75 FB |
2225 | static bitmask_transtbl mmap_flags_tbl[] = { |
2226 | { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED }, | |
2227 | { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE }, | |
2228 | { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED }, | |
2229 | { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS }, | |
2230 | { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN }, | |
2231 | { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE }, | |
2232 | { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE }, | |
2233 | { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED }, | |
2234 | { 0, 0, 0, 0 } | |
2235 | }; | |
2236 | ||
ffa65c3b FB |
2237 | static bitmask_transtbl fcntl_flags_tbl[] = { |
2238 | { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, }, | |
2239 | { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, }, | |
2240 | { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, }, | |
2241 | { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, }, | |
2242 | { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, }, | |
2243 | { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, }, | |
2244 | { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, }, | |
2245 | { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, }, | |
2246 | { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, }, | |
2247 | { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, }, | |
2248 | { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, }, | |
2249 | { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, }, | |
2250 | { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, }, | |
121061dc | 2251 | #if defined(O_DIRECT) |
ffa65c3b | 2252 | { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, }, |
121061dc | 2253 | #endif |
ffa65c3b FB |
2254 | { 0, 0, 0, 0 } |
2255 | }; | |
2256 | ||
2ab83ea7 | 2257 | #if defined(TARGET_I386) |
6dbad63e FB |
2258 | |
2259 | /* NOTE: there is really one LDT for all the threads */ | |
2260 | uint8_t *ldt_table; | |
2261 | ||
03acab66 | 2262 | static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount) |
6dbad63e FB |
2263 | { |
2264 | int size; | |
53a5960a | 2265 | void *p; |
6dbad63e FB |
2266 | |
2267 | if (!ldt_table) | |
2268 | return 0; | |
2269 | size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE; | |
2270 | if (size > bytecount) | |
2271 | size = bytecount; | |
579a97f7 FB |
2272 | p = lock_user(VERIFY_WRITE, ptr, size, 0); |
2273 | if (!p) | |
03acab66 | 2274 | return -TARGET_EFAULT; |
579a97f7 | 2275 | /* ??? Should this by byteswapped? */ |
53a5960a PB |
2276 | memcpy(p, ldt_table, size); |
2277 | unlock_user(p, ptr, size); | |
6dbad63e FB |
2278 | return size; |
2279 | } | |
2280 | ||
2281 | /* XXX: add locking support */ | |
03acab66 FB |
2282 | static abi_long write_ldt(CPUX86State *env, |
2283 | abi_ulong ptr, unsigned long bytecount, int oldmode) | |
6dbad63e FB |
2284 | { |
2285 | struct target_modify_ldt_ldt_s ldt_info; | |
53a5960a | 2286 | struct target_modify_ldt_ldt_s *target_ldt_info; |
6dbad63e FB |
2287 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
2288 | int seg_not_present, useable; | |
2289 | uint32_t *lp, entry_1, entry_2; | |
2290 | ||
2291 | if (bytecount != sizeof(ldt_info)) | |
03acab66 | 2292 | return -TARGET_EINVAL; |
579a97f7 | 2293 | if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) |
03acab66 | 2294 | return -TARGET_EFAULT; |
53a5960a PB |
2295 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
2296 | ldt_info.base_addr = tswapl(target_ldt_info->base_addr); | |
2297 | ldt_info.limit = tswap32(target_ldt_info->limit); | |
2298 | ldt_info.flags = tswap32(target_ldt_info->flags); | |
2299 | unlock_user_struct(target_ldt_info, ptr, 0); | |
3b46e624 | 2300 | |
6dbad63e | 2301 | if (ldt_info.entry_number >= TARGET_LDT_ENTRIES) |
03acab66 | 2302 | return -TARGET_EINVAL; |
6dbad63e FB |
2303 | seg_32bit = ldt_info.flags & 1; |
2304 | contents = (ldt_info.flags >> 1) & 3; | |
2305 | read_exec_only = (ldt_info.flags >> 3) & 1; | |
2306 | limit_in_pages = (ldt_info.flags >> 4) & 1; | |
2307 | seg_not_present = (ldt_info.flags >> 5) & 1; | |
2308 | useable = (ldt_info.flags >> 6) & 1; | |
2309 | ||
2310 | if (contents == 3) { | |
2311 | if (oldmode) | |
03acab66 | 2312 | return -TARGET_EINVAL; |
6dbad63e | 2313 | if (seg_not_present == 0) |
03acab66 | 2314 | return -TARGET_EINVAL; |
6dbad63e FB |
2315 | } |
2316 | /* allocate the LDT */ | |
2317 | if (!ldt_table) { | |
2318 | ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); | |
2319 | if (!ldt_table) | |
03acab66 | 2320 | return -TARGET_ENOMEM; |
6dbad63e | 2321 | memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); |
53a5960a | 2322 | env->ldt.base = h2g(ldt_table); |
6dbad63e FB |
2323 | env->ldt.limit = 0xffff; |
2324 | } | |
2325 | ||
2326 | /* NOTE: same code as Linux kernel */ | |
2327 | /* Allow LDTs to be cleared by the user. */ | |
2328 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { | |
2329 | if (oldmode || | |
2330 | (contents == 0 && | |
2331 | read_exec_only == 1 && | |
2332 | seg_32bit == 0 && | |
2333 | limit_in_pages == 0 && | |
2334 | seg_not_present == 1 && | |
2335 | useable == 0 )) { | |
2336 | entry_1 = 0; | |
2337 | entry_2 = 0; | |
2338 | goto install; | |
2339 | } | |
2340 | } | |
3b46e624 | 2341 | |
6dbad63e FB |
2342 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
2343 | (ldt_info.limit & 0x0ffff); | |
2344 | entry_2 = (ldt_info.base_addr & 0xff000000) | | |
2345 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | | |
2346 | (ldt_info.limit & 0xf0000) | | |
2347 | ((read_exec_only ^ 1) << 9) | | |
2348 | (contents << 10) | | |
2349 | ((seg_not_present ^ 1) << 15) | | |
2350 | (seg_32bit << 22) | | |
2351 | (limit_in_pages << 23) | | |
2352 | 0x7000; | |
2353 | if (!oldmode) | |
2354 | entry_2 |= (useable << 20); | |
14ae3ba7 | 2355 | |
6dbad63e FB |
2356 | /* Install the new entry ... */ |
2357 | install: | |
2358 | lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3)); | |
2359 | lp[0] = tswap32(entry_1); | |
2360 | lp[1] = tswap32(entry_2); | |
2361 | return 0; | |
2362 | } | |
2363 | ||
2364 | /* specific and weird i386 syscalls */ | |
03acab66 FB |
2365 | abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, |
2366 | unsigned long bytecount) | |
6dbad63e | 2367 | { |
03acab66 | 2368 | abi_long ret; |
3b46e624 | 2369 | |
6dbad63e FB |
2370 | switch (func) { |
2371 | case 0: | |
2372 | ret = read_ldt(ptr, bytecount); | |
2373 | break; | |
2374 | case 1: | |
2375 | ret = write_ldt(env, ptr, bytecount, 1); | |
2376 | break; | |
2377 | case 0x11: | |
2378 | ret = write_ldt(env, ptr, bytecount, 0); | |
2379 | break; | |
03acab66 FB |
2380 | default: |
2381 | ret = -TARGET_ENOSYS; | |
2382 | break; | |
6dbad63e FB |
2383 | } |
2384 | return ret; | |
2385 | } | |
1b6b029e | 2386 | |
2ab83ea7 FB |
2387 | #endif /* defined(TARGET_I386) */ |
2388 | ||
1b6b029e FB |
2389 | /* this stack is the equivalent of the kernel stack associated with a |
2390 | thread/process */ | |
2391 | #define NEW_STACK_SIZE 8192 | |
2392 | ||
2393 | static int clone_func(void *arg) | |
2394 | { | |
2ab83ea7 | 2395 | CPUState *env = arg; |
1b6b029e FB |
2396 | cpu_loop(env); |
2397 | /* never exits */ | |
2398 | return 0; | |
2399 | } | |
2400 | ||
0da46a6e TS |
2401 | /* do_fork() Must return host values and target errnos (unlike most |
2402 | do_*() functions). */ | |
992f48a0 | 2403 | int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp) |
1b6b029e FB |
2404 | { |
2405 | int ret; | |
5cd4393b | 2406 | TaskState *ts; |
1b6b029e | 2407 | uint8_t *new_stack; |
2ab83ea7 | 2408 | CPUState *new_env; |
3b46e624 | 2409 | |
1b6b029e | 2410 | if (flags & CLONE_VM) { |
5cd4393b FB |
2411 | ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE); |
2412 | memset(ts, 0, sizeof(TaskState)); | |
2413 | new_stack = ts->stack; | |
2414 | ts->used = 1; | |
2415 | /* add in task state list */ | |
2416 | ts->next = first_task_state; | |
2417 | first_task_state = ts; | |
1b6b029e | 2418 | /* we create a new CPU instance. */ |
c5be9f08 | 2419 | new_env = cpu_copy(env); |
2ab83ea7 FB |
2420 | #if defined(TARGET_I386) |
2421 | if (!newsp) | |
2422 | newsp = env->regs[R_ESP]; | |
1b6b029e FB |
2423 | new_env->regs[R_ESP] = newsp; |
2424 | new_env->regs[R_EAX] = 0; | |
2ab83ea7 FB |
2425 | #elif defined(TARGET_ARM) |
2426 | if (!newsp) | |
2427 | newsp = env->regs[13]; | |
2428 | new_env->regs[13] = newsp; | |
2429 | new_env->regs[0] = 0; | |
ebc05488 | 2430 | #elif defined(TARGET_SPARC) |
48dc41eb FB |
2431 | if (!newsp) |
2432 | newsp = env->regwptr[22]; | |
2433 | new_env->regwptr[22] = newsp; | |
2434 | new_env->regwptr[0] = 0; | |
2435 | /* XXXXX */ | |
67867308 | 2436 | printf ("HELPME: %s:%d\n", __FILE__, __LINE__); |
e6e5906b PB |
2437 | #elif defined(TARGET_M68K) |
2438 | if (!newsp) | |
2439 | newsp = env->aregs[7]; | |
2440 | new_env->aregs[7] = newsp; | |
2441 | new_env->dregs[0] = 0; | |
2442 | /* ??? is this sufficient? */ | |
048f6b4d | 2443 | #elif defined(TARGET_MIPS) |
388bb21a | 2444 | if (!newsp) |
ead9360e TS |
2445 | newsp = env->gpr[29][env->current_tc]; |
2446 | new_env->gpr[29][env->current_tc] = newsp; | |
67867308 FB |
2447 | #elif defined(TARGET_PPC) |
2448 | if (!newsp) | |
2449 | newsp = env->gpr[1]; | |
2450 | new_env->gpr[1] = newsp; | |
5fafdf24 | 2451 | { |
67867308 FB |
2452 | int i; |
2453 | for (i = 7; i < 32; i++) | |
2454 | new_env->gpr[i] = 0; | |
2455 | } | |
fdf9b3e8 FB |
2456 | #elif defined(TARGET_SH4) |
2457 | if (!newsp) | |
2458 | newsp = env->gregs[15]; | |
2459 | new_env->gregs[15] = newsp; | |
2460 | /* XXXXX */ | |
7a3148a9 JM |
2461 | #elif defined(TARGET_ALPHA) |
2462 | if (!newsp) | |
2463 | newsp = env->ir[30]; | |
2464 | new_env->ir[30] = newsp; | |
2465 | /* ? */ | |
2466 | { | |
2467 | int i; | |
2468 | for (i = 7; i < 30; i++) | |
2469 | new_env->ir[i] = 0; | |
2470 | } | |
48733d19 TS |
2471 | #elif defined(TARGET_CRIS) |
2472 | if (!newsp) | |
2473 | newsp = env->regs[14]; | |
2474 | new_env->regs[14] = newsp; | |
2ab83ea7 FB |
2475 | #else |
2476 | #error unsupported target CPU | |
2477 | #endif | |
5cd4393b | 2478 | new_env->opaque = ts; |
27725c1d | 2479 | #ifdef __ia64__ |
fd4a43e4 | 2480 | ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env); |
27725c1d FB |
2481 | #else |
2482 | ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env); | |
2483 | #endif | |
1b6b029e FB |
2484 | } else { |
2485 | /* if no CLONE_VM, we consider it is a fork */ | |
2486 | if ((flags & ~CSIGNAL) != 0) | |
2487 | return -EINVAL; | |
2488 | ret = fork(); | |
2489 | } | |
2490 | return ret; | |
2491 | } | |
2492 | ||
992f48a0 | 2493 | static abi_long do_fcntl(int fd, int cmd, abi_ulong arg) |
7775e9ec FB |
2494 | { |
2495 | struct flock fl; | |
53a5960a | 2496 | struct target_flock *target_fl; |
43f238d7 TS |
2497 | struct flock64 fl64; |
2498 | struct target_flock64 *target_fl64; | |
992f48a0 | 2499 | abi_long ret; |
53a5960a | 2500 | |
7775e9ec FB |
2501 | switch(cmd) { |
2502 | case TARGET_F_GETLK: | |
579a97f7 FB |
2503 | if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1)) |
2504 | return -TARGET_EFAULT; | |
5813427b TS |
2505 | fl.l_type = tswap16(target_fl->l_type); |
2506 | fl.l_whence = tswap16(target_fl->l_whence); | |
2507 | fl.l_start = tswapl(target_fl->l_start); | |
2508 | fl.l_len = tswapl(target_fl->l_len); | |
2509 | fl.l_pid = tswapl(target_fl->l_pid); | |
2510 | unlock_user_struct(target_fl, arg, 0); | |
9ee1fa2c | 2511 | ret = get_errno(fcntl(fd, cmd, &fl)); |
7775e9ec | 2512 | if (ret == 0) { |
579a97f7 FB |
2513 | if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0)) |
2514 | return -TARGET_EFAULT; | |
7775e9ec FB |
2515 | target_fl->l_type = tswap16(fl.l_type); |
2516 | target_fl->l_whence = tswap16(fl.l_whence); | |
2517 | target_fl->l_start = tswapl(fl.l_start); | |
2518 | target_fl->l_len = tswapl(fl.l_len); | |
2519 | target_fl->l_pid = tswapl(fl.l_pid); | |
53a5960a | 2520 | unlock_user_struct(target_fl, arg, 1); |
7775e9ec FB |
2521 | } |
2522 | break; | |
3b46e624 | 2523 | |
7775e9ec FB |
2524 | case TARGET_F_SETLK: |
2525 | case TARGET_F_SETLKW: | |
579a97f7 FB |
2526 | if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1)) |
2527 | return -TARGET_EFAULT; | |
7775e9ec FB |
2528 | fl.l_type = tswap16(target_fl->l_type); |
2529 | fl.l_whence = tswap16(target_fl->l_whence); | |
2530 | fl.l_start = tswapl(target_fl->l_start); | |
2531 | fl.l_len = tswapl(target_fl->l_len); | |
2532 | fl.l_pid = tswapl(target_fl->l_pid); | |
53a5960a | 2533 | unlock_user_struct(target_fl, arg, 0); |
9ee1fa2c | 2534 | ret = get_errno(fcntl(fd, cmd, &fl)); |
7775e9ec | 2535 | break; |
3b46e624 | 2536 | |
7775e9ec | 2537 | case TARGET_F_GETLK64: |
579a97f7 FB |
2538 | if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1)) |
2539 | return -TARGET_EFAULT; | |
5813427b TS |
2540 | fl64.l_type = tswap16(target_fl64->l_type) >> 1; |
2541 | fl64.l_whence = tswap16(target_fl64->l_whence); | |
2542 | fl64.l_start = tswapl(target_fl64->l_start); | |
2543 | fl64.l_len = tswapl(target_fl64->l_len); | |
2544 | fl64.l_pid = tswap16(target_fl64->l_pid); | |
2545 | unlock_user_struct(target_fl64, arg, 0); | |
9ee1fa2c | 2546 | ret = get_errno(fcntl(fd, cmd >> 1, &fl64)); |
43f238d7 | 2547 | if (ret == 0) { |
579a97f7 FB |
2548 | if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0)) |
2549 | return -TARGET_EFAULT; | |
43f238d7 TS |
2550 | target_fl64->l_type = tswap16(fl64.l_type) >> 1; |
2551 | target_fl64->l_whence = tswap16(fl64.l_whence); | |
2552 | target_fl64->l_start = tswapl(fl64.l_start); | |
2553 | target_fl64->l_len = tswapl(fl64.l_len); | |
2554 | target_fl64->l_pid = tswapl(fl64.l_pid); | |
2555 | unlock_user_struct(target_fl64, arg, 1); | |
2556 | } | |
9ee1fa2c | 2557 | break; |
7775e9ec FB |
2558 | case TARGET_F_SETLK64: |
2559 | case TARGET_F_SETLKW64: | |
579a97f7 FB |
2560 | if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1)) |
2561 | return -TARGET_EFAULT; | |
43f238d7 TS |
2562 | fl64.l_type = tswap16(target_fl64->l_type) >> 1; |
2563 | fl64.l_whence = tswap16(target_fl64->l_whence); | |
2564 | fl64.l_start = tswapl(target_fl64->l_start); | |
2565 | fl64.l_len = tswapl(target_fl64->l_len); | |
2566 | fl64.l_pid = tswap16(target_fl64->l_pid); | |
2567 | unlock_user_struct(target_fl64, arg, 0); | |
9ee1fa2c | 2568 | ret = get_errno(fcntl(fd, cmd >> 1, &fl64)); |
7775e9ec FB |
2569 | break; |
2570 | ||
ffa65c3b | 2571 | case F_GETFL: |
9ee1fa2c FB |
2572 | ret = get_errno(fcntl(fd, cmd, arg)); |
2573 | if (ret >= 0) { | |
2574 | ret = host_to_target_bitmask(ret, fcntl_flags_tbl); | |
2575 | } | |
ffa65c3b FB |
2576 | break; |
2577 | ||
2578 | case F_SETFL: | |
9ee1fa2c | 2579 | ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl))); |
ffa65c3b FB |
2580 | break; |
2581 | ||
7775e9ec | 2582 | default: |
9ee1fa2c | 2583 | ret = get_errno(fcntl(fd, cmd, arg)); |
7775e9ec FB |
2584 | break; |
2585 | } | |
2586 | return ret; | |
2587 | } | |
2588 | ||
67867308 | 2589 | #ifdef USE_UID16 |
7775e9ec | 2590 | |
67867308 FB |
2591 | static inline int high2lowuid(int uid) |
2592 | { | |
2593 | if (uid > 65535) | |
2594 | return 65534; | |
2595 | else | |
2596 | return uid; | |
2597 | } | |
2598 | ||
2599 | static inline int high2lowgid(int gid) | |
2600 | { | |
2601 | if (gid > 65535) | |
2602 | return 65534; | |
2603 | else | |
2604 | return gid; | |
2605 | } | |
2606 | ||
2607 | static inline int low2highuid(int uid) | |
2608 | { | |
2609 | if ((int16_t)uid == -1) | |
2610 | return -1; | |
2611 | else | |
2612 | return uid; | |
2613 | } | |
2614 | ||
2615 | static inline int low2highgid(int gid) | |
2616 | { | |
2617 | if ((int16_t)gid == -1) | |
2618 | return -1; | |
2619 | else | |
2620 | return gid; | |
2621 | } | |
2622 | ||
2623 | #endif /* USE_UID16 */ | |
1b6b029e | 2624 | |
31e31b8a FB |
2625 | void syscall_init(void) |
2626 | { | |
2ab83ea7 FB |
2627 | IOCTLEntry *ie; |
2628 | const argtype *arg_type; | |
2629 | int size; | |
b92c47c1 | 2630 | int i; |
2ab83ea7 | 2631 | |
5fafdf24 TS |
2632 | #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); |
2633 | #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); | |
31e31b8a FB |
2634 | #include "syscall_types.h" |
2635 | #undef STRUCT | |
2636 | #undef STRUCT_SPECIAL | |
2ab83ea7 FB |
2637 | |
2638 | /* we patch the ioctl size if necessary. We rely on the fact that | |
2639 | no ioctl has all the bits at '1' in the size field */ | |
2640 | ie = ioctl_entries; | |
2641 | while (ie->target_cmd != 0) { | |
2642 | if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) == | |
2643 | TARGET_IOC_SIZEMASK) { | |
2644 | arg_type = ie->arg_type; | |
2645 | if (arg_type[0] != TYPE_PTR) { | |
5fafdf24 | 2646 | fprintf(stderr, "cannot patch size for ioctl 0x%x\n", |
2ab83ea7 FB |
2647 | ie->target_cmd); |
2648 | exit(1); | |
2649 | } | |
2650 | arg_type++; | |
2651 | size = thunk_type_size(arg_type, 0); | |
5fafdf24 | 2652 | ie->target_cmd = (ie->target_cmd & |
2ab83ea7 FB |
2653 | ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) | |
2654 | (size << TARGET_IOC_SIZESHIFT); | |
2655 | } | |
b92c47c1 TS |
2656 | |
2657 | /* Build target_to_host_errno_table[] table from | |
2658 | * host_to_target_errno_table[]. */ | |
2659 | for (i=0; i < ERRNO_TABLE_SIZE; i++) | |
2660 | target_to_host_errno_table[host_to_target_errno_table[i]] = i; | |
2661 | ||
2ab83ea7 FB |
2662 | /* automatic consistency check if same arch */ |
2663 | #if defined(__i386__) && defined(TARGET_I386) | |
2664 | if (ie->target_cmd != ie->host_cmd) { | |
5fafdf24 | 2665 | fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n", |
2ab83ea7 FB |
2666 | ie->target_cmd, ie->host_cmd); |
2667 | } | |
2668 | #endif | |
2669 | ie++; | |
2670 | } | |
31e31b8a | 2671 | } |
c573ff67 | 2672 | |
992f48a0 | 2673 | #if TARGET_ABI_BITS == 32 |
ce4defa0 PB |
2674 | static inline uint64_t target_offset64(uint32_t word0, uint32_t word1) |
2675 | { | |
2676 | #ifdef TARGET_WORDS_BIG_ENDIAN | |
2677 | return ((uint64_t)word0 << 32) | word1; | |
2678 | #else | |
2679 | return ((uint64_t)word1 << 32) | word0; | |
2680 | #endif | |
2681 | } | |
992f48a0 | 2682 | #else /* TARGET_ABI_BITS == 32 */ |
32407103 JM |
2683 | static inline uint64_t target_offset64(uint64_t word0, uint64_t word1) |
2684 | { | |
2685 | return word0; | |
2686 | } | |
992f48a0 | 2687 | #endif /* TARGET_ABI_BITS != 32 */ |
ce4defa0 PB |
2688 | |
2689 | #ifdef TARGET_NR_truncate64 | |
992f48a0 BS |
2690 | static inline abi_long target_truncate64(void *cpu_env, const char *arg1, |
2691 | abi_long arg2, | |
2692 | abi_long arg3, | |
2693 | abi_long arg4) | |
ce4defa0 PB |
2694 | { |
2695 | #ifdef TARGET_ARM | |
2696 | if (((CPUARMState *)cpu_env)->eabi) | |
2697 | { | |
2698 | arg2 = arg3; | |
2699 | arg3 = arg4; | |
2700 | } | |
2701 | #endif | |
2702 | return get_errno(truncate64(arg1, target_offset64(arg2, arg3))); | |
2703 | } | |
2704 | #endif | |
2705 | ||
2706 | #ifdef TARGET_NR_ftruncate64 | |
992f48a0 BS |
2707 | static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1, |
2708 | abi_long arg2, | |
2709 | abi_long arg3, | |
2710 | abi_long arg4) | |
ce4defa0 PB |
2711 | { |
2712 | #ifdef TARGET_ARM | |
2713 | if (((CPUARMState *)cpu_env)->eabi) | |
2714 | { | |
2715 | arg2 = arg3; | |
2716 | arg3 = arg4; | |
2717 | } | |
2718 | #endif | |
2719 | return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3))); | |
2720 | } | |
2721 | #endif | |
2722 | ||
579a97f7 FB |
2723 | static inline abi_long target_to_host_timespec(struct timespec *host_ts, |
2724 | abi_ulong target_addr) | |
53a5960a PB |
2725 | { |
2726 | struct target_timespec *target_ts; | |
2727 | ||
579a97f7 FB |
2728 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) |
2729 | return -TARGET_EFAULT; | |
53a5960a PB |
2730 | host_ts->tv_sec = tswapl(target_ts->tv_sec); |
2731 | host_ts->tv_nsec = tswapl(target_ts->tv_nsec); | |
2732 | unlock_user_struct(target_ts, target_addr, 0); | |
2733 | } | |
2734 | ||
579a97f7 FB |
2735 | static inline abi_long host_to_target_timespec(abi_ulong target_addr, |
2736 | struct timespec *host_ts) | |
53a5960a PB |
2737 | { |
2738 | struct target_timespec *target_ts; | |
2739 | ||
579a97f7 FB |
2740 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) |
2741 | return -TARGET_EFAULT; | |
53a5960a PB |
2742 | target_ts->tv_sec = tswapl(host_ts->tv_sec); |
2743 | target_ts->tv_nsec = tswapl(host_ts->tv_nsec); | |
2744 | unlock_user_struct(target_ts, target_addr, 1); | |
2745 | } | |
2746 | ||
0da46a6e TS |
2747 | /* do_syscall() should always have a single exit point at the end so |
2748 | that actions, such as logging of syscall results, can be performed. | |
2749 | All errnos that do_syscall() returns must be -TARGET_<errcode>. */ | |
992f48a0 BS |
2750 | abi_long do_syscall(void *cpu_env, int num, abi_long arg1, |
2751 | abi_long arg2, abi_long arg3, abi_long arg4, | |
2752 | abi_long arg5, abi_long arg6) | |
31e31b8a | 2753 | { |
992f48a0 | 2754 | abi_long ret; |
31e31b8a | 2755 | struct stat st; |
56c8f68f | 2756 | struct statfs stfs; |
53a5960a | 2757 | void *p; |
3b46e624 | 2758 | |
72f03900 | 2759 | #ifdef DEBUG |
c573ff67 | 2760 | gemu_log("syscall %d", num); |
72f03900 | 2761 | #endif |
b92c47c1 TS |
2762 | if(do_strace) |
2763 | print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); | |
2764 | ||
31e31b8a FB |
2765 | switch(num) { |
2766 | case TARGET_NR_exit: | |
7d13299d FB |
2767 | #ifdef HAVE_GPROF |
2768 | _mcleanup(); | |
2769 | #endif | |
e9009676 | 2770 | gdb_exit(cpu_env, arg1); |
1b6b029e | 2771 | /* XXX: should free thread stack and CPU env */ |
31e31b8a FB |
2772 | _exit(arg1); |
2773 | ret = 0; /* avoid warning */ | |
2774 | break; | |
2775 | case TARGET_NR_read: | |
53a5960a | 2776 | page_unprotect_range(arg2, arg3); |
579a97f7 FB |
2777 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
2778 | goto efault; | |
53a5960a PB |
2779 | ret = get_errno(read(arg1, p, arg3)); |
2780 | unlock_user(p, arg2, ret); | |
31e31b8a FB |
2781 | break; |
2782 | case TARGET_NR_write: | |
579a97f7 FB |
2783 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
2784 | goto efault; | |
53a5960a PB |
2785 | ret = get_errno(write(arg1, p, arg3)); |
2786 | unlock_user(p, arg2, 0); | |
31e31b8a FB |
2787 | break; |
2788 | case TARGET_NR_open: | |
579a97f7 FB |
2789 | if (!(p = lock_user_string(arg1))) { |
2790 | return -TARGET_EFAULT; | |
2791 | goto fail; | |
2792 | } | |
53a5960a | 2793 | ret = get_errno(open(path(p), |
ffa65c3b FB |
2794 | target_to_host_bitmask(arg2, fcntl_flags_tbl), |
2795 | arg3)); | |
53a5960a | 2796 | unlock_user(p, arg1, 0); |
31e31b8a | 2797 | break; |
82424832 TS |
2798 | #if defined(TARGET_NR_openat) && defined(__NR_openat) |
2799 | case TARGET_NR_openat: | |
579a97f7 FB |
2800 | if (!(p = lock_user_string(arg2))) |
2801 | goto efault; | |
2802 | ret = get_errno(sys_openat(arg1, | |
2803 | path(p), | |
2804 | target_to_host_bitmask(arg3, fcntl_flags_tbl), | |
2805 | arg4)); | |
2806 | unlock_user(p, arg2, 0); | |
82424832 TS |
2807 | break; |
2808 | #endif | |
31e31b8a FB |
2809 | case TARGET_NR_close: |
2810 | ret = get_errno(close(arg1)); | |
2811 | break; | |
2812 | case TARGET_NR_brk: | |
53a5960a | 2813 | ret = do_brk(arg1); |
31e31b8a FB |
2814 | break; |
2815 | case TARGET_NR_fork: | |
1b6b029e | 2816 | ret = get_errno(do_fork(cpu_env, SIGCHLD, 0)); |
31e31b8a | 2817 | break; |
e5febef5 | 2818 | #ifdef TARGET_NR_waitpid |
31e31b8a FB |
2819 | case TARGET_NR_waitpid: |
2820 | { | |
53a5960a PB |
2821 | int status; |
2822 | ret = get_errno(waitpid(arg1, &status, arg3)); | |
2823 | if (!is_error(ret) && arg2) | |
2824 | tput32(arg2, status); | |
31e31b8a FB |
2825 | } |
2826 | break; | |
e5febef5 | 2827 | #endif |
7a3148a9 | 2828 | #ifdef TARGET_NR_creat /* not on alpha */ |
31e31b8a | 2829 | case TARGET_NR_creat: |
579a97f7 FB |
2830 | if (!(p = lock_user_string(arg1))) |
2831 | goto efault; | |
53a5960a PB |
2832 | ret = get_errno(creat(p, arg2)); |
2833 | unlock_user(p, arg1, 0); | |
31e31b8a | 2834 | break; |
7a3148a9 | 2835 | #endif |
31e31b8a | 2836 | case TARGET_NR_link: |
53a5960a PB |
2837 | { |
2838 | void * p2; | |
2839 | p = lock_user_string(arg1); | |
2840 | p2 = lock_user_string(arg2); | |
579a97f7 FB |
2841 | if (!p || !p2) |
2842 | ret = -TARGET_EFAULT; | |
2843 | else | |
2844 | ret = get_errno(link(p, p2)); | |
53a5960a PB |
2845 | unlock_user(p2, arg2, 0); |
2846 | unlock_user(p, arg1, 0); | |
2847 | } | |
31e31b8a | 2848 | break; |
64f0ce4c TS |
2849 | #if defined(TARGET_NR_linkat) && defined(__NR_linkat) |
2850 | case TARGET_NR_linkat: | |
64f0ce4c TS |
2851 | { |
2852 | void * p2 = NULL; | |
579a97f7 FB |
2853 | if (!arg2 || !arg4) |
2854 | goto efault; | |
64f0ce4c TS |
2855 | p = lock_user_string(arg2); |
2856 | p2 = lock_user_string(arg4); | |
579a97f7 | 2857 | if (!p || !p2) |
0da46a6e | 2858 | ret = -TARGET_EFAULT; |
64f0ce4c TS |
2859 | else |
2860 | ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5)); | |
579a97f7 FB |
2861 | unlock_user(p, arg2, 0); |
2862 | unlock_user(p2, arg4, 0); | |
64f0ce4c TS |
2863 | } |
2864 | break; | |
2865 | #endif | |
31e31b8a | 2866 | case TARGET_NR_unlink: |
579a97f7 FB |
2867 | if (!(p = lock_user_string(arg1))) |
2868 | goto efault; | |
53a5960a PB |
2869 | ret = get_errno(unlink(p)); |
2870 | unlock_user(p, arg1, 0); | |
31e31b8a | 2871 | break; |
8170f56b TS |
2872 | #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat) |
2873 | case TARGET_NR_unlinkat: | |
579a97f7 FB |
2874 | if (!(p = lock_user_string(arg2))) |
2875 | goto efault; | |
2876 | ret = get_errno(sys_unlinkat(arg1, p, arg3)); | |
2877 | unlock_user(p, arg2, 0); | |
8170f56b | 2878 | #endif |
31e31b8a | 2879 | case TARGET_NR_execve: |
7854b056 FB |
2880 | { |
2881 | char **argp, **envp; | |
f7341ff4 | 2882 | int argc, envc; |
992f48a0 BS |
2883 | abi_ulong gp; |
2884 | abi_ulong guest_argp; | |
2885 | abi_ulong guest_envp; | |
2886 | abi_ulong addr; | |
7854b056 FB |
2887 | char **q; |
2888 | ||
f7341ff4 | 2889 | argc = 0; |
53a5960a PB |
2890 | guest_argp = arg2; |
2891 | for (gp = guest_argp; tgetl(gp); gp++) | |
7854b056 | 2892 | argc++; |
f7341ff4 | 2893 | envc = 0; |
53a5960a PB |
2894 | guest_envp = arg3; |
2895 | for (gp = guest_envp; tgetl(gp); gp++) | |
7854b056 FB |
2896 | envc++; |
2897 | ||
f7341ff4 FB |
2898 | argp = alloca((argc + 1) * sizeof(void *)); |
2899 | envp = alloca((envc + 1) * sizeof(void *)); | |
7854b056 | 2900 | |
53a5960a | 2901 | for (gp = guest_argp, q = argp; ; |
992f48a0 | 2902 | gp += sizeof(abi_ulong), q++) { |
53a5960a PB |
2903 | addr = tgetl(gp); |
2904 | if (!addr) | |
2905 | break; | |
579a97f7 FB |
2906 | if (!(*q = lock_user_string(addr))) { |
2907 | ret = -TARGET_EFAULT; | |
2908 | goto execve_fail; | |
2909 | } | |
53a5960a | 2910 | } |
f7341ff4 FB |
2911 | *q = NULL; |
2912 | ||
53a5960a | 2913 | for (gp = guest_envp, q = envp; ; |
992f48a0 | 2914 | gp += sizeof(abi_ulong), q++) { |
53a5960a PB |
2915 | addr = tgetl(gp); |
2916 | if (!addr) | |
2917 | break; | |
579a97f7 FB |
2918 | if (!(*q = lock_user_string(addr))) { |
2919 | ret = -TARGET_EFAULT; | |
2920 | goto execve_fail; | |
2921 | } | |
53a5960a | 2922 | } |
f7341ff4 | 2923 | *q = NULL; |
7854b056 | 2924 | |
579a97f7 FB |
2925 | if (!(p = lock_user_string(arg1))) { |
2926 | ret = -TARGET_EFAULT; | |
2927 | goto execve_fail; | |
2928 | } | |
53a5960a PB |
2929 | ret = get_errno(execve(p, argp, envp)); |
2930 | unlock_user(p, arg1, 0); | |
2931 | ||
579a97f7 | 2932 | execve_fail: |
53a5960a | 2933 | for (gp = guest_argp, q = argp; *q; |
992f48a0 | 2934 | gp += sizeof(abi_ulong), q++) { |
53a5960a PB |
2935 | addr = tgetl(gp); |
2936 | unlock_user(*q, addr, 0); | |
2937 | } | |
2938 | for (gp = guest_envp, q = envp; *q; | |
992f48a0 | 2939 | gp += sizeof(abi_ulong), q++) { |
53a5960a PB |
2940 | addr = tgetl(gp); |
2941 | unlock_user(*q, addr, 0); | |
2942 | } | |
7854b056 | 2943 | } |
31e31b8a FB |
2944 | break; |
2945 | case TARGET_NR_chdir: | |
579a97f7 FB |
2946 | if (!(p = lock_user_string(arg1))) |
2947 | goto efault; | |
53a5960a PB |
2948 | ret = get_errno(chdir(p)); |
2949 | unlock_user(p, arg1, 0); | |
31e31b8a | 2950 | break; |
a315a145 | 2951 | #ifdef TARGET_NR_time |
31e31b8a FB |
2952 | case TARGET_NR_time: |
2953 | { | |
53a5960a PB |
2954 | time_t host_time; |
2955 | ret = get_errno(time(&host_time)); | |
2956 | if (!is_error(ret) && arg1) | |
2957 | tputl(arg1, host_time); | |
31e31b8a FB |
2958 | } |
2959 | break; | |
a315a145 | 2960 | #endif |
31e31b8a | 2961 | case TARGET_NR_mknod: |
579a97f7 FB |
2962 | if (!(p = lock_user_string(arg1))) |
2963 | goto efault; | |
53a5960a PB |
2964 | ret = get_errno(mknod(p, arg2, arg3)); |
2965 | unlock_user(p, arg1, 0); | |
31e31b8a | 2966 | break; |
75ac37a0 TS |
2967 | #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat) |
2968 | case TARGET_NR_mknodat: | |
579a97f7 FB |
2969 | if (!(p = lock_user_string(arg2))) |
2970 | goto efault; | |
2971 | ret = get_errno(sys_mknodat(arg1, p, arg3, arg4)); | |
2972 | unlock_user(p, arg2, 0); | |
75ac37a0 TS |
2973 | break; |
2974 | #endif | |
31e31b8a | 2975 | case TARGET_NR_chmod: |
579a97f7 FB |
2976 | if (!(p = lock_user_string(arg1))) |
2977 | goto efault; | |
53a5960a PB |
2978 | ret = get_errno(chmod(p, arg2)); |
2979 | unlock_user(p, arg1, 0); | |
31e31b8a | 2980 | break; |
ebc05488 | 2981 | #ifdef TARGET_NR_break |
31e31b8a FB |
2982 | case TARGET_NR_break: |
2983 | goto unimplemented; | |
ebc05488 FB |
2984 | #endif |
2985 | #ifdef TARGET_NR_oldstat | |
31e31b8a FB |
2986 | case TARGET_NR_oldstat: |
2987 | goto unimplemented; | |
ebc05488 | 2988 | #endif |
31e31b8a FB |
2989 | case TARGET_NR_lseek: |
2990 | ret = get_errno(lseek(arg1, arg2, arg3)); | |
2991 | break; | |
7a3148a9 JM |
2992 | #ifdef TARGET_NR_getxpid |
2993 | case TARGET_NR_getxpid: | |
2994 | #else | |
31e31b8a | 2995 | case TARGET_NR_getpid: |
7a3148a9 | 2996 | #endif |
31e31b8a FB |
2997 | ret = get_errno(getpid()); |
2998 | break; | |
2999 | case TARGET_NR_mount: | |
80265918 TS |
3000 | { |
3001 | /* need to look at the data field */ | |
3002 | void *p2, *p3; | |
3003 | p = lock_user_string(arg1); | |
3004 | p2 = lock_user_string(arg2); | |
3005 | p3 = lock_user_string(arg3); | |
579a97f7 FB |
3006 | if (!p || !p2 || !p3) |
3007 | ret = -TARGET_EFAULT; | |
3008 | else | |
3009 | /* FIXME - arg5 should be locked, but it isn't clear how to | |
3010 | * do that since it's not guaranteed to be a NULL-terminated | |
3011 | * string. | |
3012 | */ | |
3013 | ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5))); | |
3014 | unlock_user(p, arg1, 0); | |
3015 | unlock_user(p2, arg2, 0); | |
3016 | unlock_user(p3, arg3, 0); | |
80265918 TS |
3017 | break; |
3018 | } | |
e5febef5 | 3019 | #ifdef TARGET_NR_umount |
31e31b8a | 3020 | case TARGET_NR_umount: |
579a97f7 FB |
3021 | if (!(p = lock_user_string(arg1))) |
3022 | goto efault; | |
53a5960a PB |
3023 | ret = get_errno(umount(p)); |
3024 | unlock_user(p, arg1, 0); | |
31e31b8a | 3025 | break; |
e5febef5 | 3026 | #endif |
7a3148a9 | 3027 | #ifdef TARGET_NR_stime /* not on alpha */ |
31e31b8a FB |
3028 | case TARGET_NR_stime: |
3029 | { | |
53a5960a PB |
3030 | time_t host_time; |
3031 | host_time = tgetl(arg1); | |
3032 | ret = get_errno(stime(&host_time)); | |
31e31b8a FB |
3033 | } |
3034 | break; | |
7a3148a9 | 3035 | #endif |
31e31b8a FB |
3036 | case TARGET_NR_ptrace: |
3037 | goto unimplemented; | |
7a3148a9 | 3038 | #ifdef TARGET_NR_alarm /* not on alpha */ |
31e31b8a FB |
3039 | case TARGET_NR_alarm: |
3040 | ret = alarm(arg1); | |
3041 | break; | |
7a3148a9 | 3042 | #endif |
ebc05488 | 3043 | #ifdef TARGET_NR_oldfstat |
31e31b8a FB |
3044 | case TARGET_NR_oldfstat: |
3045 | goto unimplemented; | |
ebc05488 | 3046 | #endif |
7a3148a9 | 3047 | #ifdef TARGET_NR_pause /* not on alpha */ |
31e31b8a FB |
3048 | case TARGET_NR_pause: |
3049 | ret = get_errno(pause()); | |
3050 | break; | |
7a3148a9 | 3051 | #endif |
e5febef5 | 3052 | #ifdef TARGET_NR_utime |
31e31b8a | 3053 | case TARGET_NR_utime: |
ebc05488 | 3054 | { |
53a5960a PB |
3055 | struct utimbuf tbuf, *host_tbuf; |
3056 | struct target_utimbuf *target_tbuf; | |
3057 | if (arg2) { | |
579a97f7 FB |
3058 | if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1)) |
3059 | goto efault; | |
53a5960a PB |
3060 | tbuf.actime = tswapl(target_tbuf->actime); |
3061 | tbuf.modtime = tswapl(target_tbuf->modtime); | |
3062 | unlock_user_struct(target_tbuf, arg2, 0); | |
3063 | host_tbuf = &tbuf; | |
f72e8ff4 | 3064 | } else { |
53a5960a | 3065 | host_tbuf = NULL; |
f72e8ff4 | 3066 | } |
579a97f7 FB |
3067 | if (!(p = lock_user_string(arg1))) |
3068 | goto efault; | |
53a5960a PB |
3069 | ret = get_errno(utime(p, host_tbuf)); |
3070 | unlock_user(p, arg1, 0); | |
ebc05488 FB |
3071 | } |
3072 | break; | |
e5febef5 | 3073 | #endif |
978a66ff FB |
3074 | case TARGET_NR_utimes: |
3075 | { | |
978a66ff | 3076 | struct timeval *tvp, tv[2]; |
53a5960a PB |
3077 | if (arg2) { |
3078 | target_to_host_timeval(&tv[0], arg2); | |
3079 | target_to_host_timeval(&tv[1], | |
3080 | arg2 + sizeof (struct target_timeval)); | |
978a66ff FB |
3081 | tvp = tv; |
3082 | } else { | |
3083 | tvp = NULL; | |
3084 | } | |
579a97f7 FB |
3085 | if (!(p = lock_user_string(arg1))) |
3086 | goto efault; | |
53a5960a PB |
3087 | ret = get_errno(utimes(p, tvp)); |
3088 | unlock_user(p, arg1, 0); | |
978a66ff FB |
3089 | } |
3090 | break; | |
ebc05488 | 3091 | #ifdef TARGET_NR_stty |
31e31b8a FB |
3092 | case TARGET_NR_stty: |
3093 | goto unimplemented; | |
ebc05488 FB |
3094 | #endif |
3095 | #ifdef TARGET_NR_gtty | |
31e31b8a FB |
3096 | case TARGET_NR_gtty: |
3097 | goto unimplemented; | |
ebc05488 | 3098 | #endif |
31e31b8a | 3099 | case TARGET_NR_access: |
579a97f7 FB |
3100 | if (!(p = lock_user_string(arg1))) |
3101 | goto efault; | |
53a5960a PB |
3102 | ret = get_errno(access(p, arg2)); |
3103 | unlock_user(p, arg1, 0); | |
31e31b8a | 3104 | break; |
92a34c10 TS |
3105 | #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) |
3106 | case TARGET_NR_faccessat: | |
579a97f7 FB |
3107 | if (!(p = lock_user_string(arg2))) |
3108 | goto efault; | |
3109 | ret = get_errno(sys_faccessat(arg1, p, arg3, arg4)); | |
3110 | unlock_user(p, arg2, 0); | |
92a34c10 TS |
3111 | break; |
3112 | #endif | |
7a3148a9 | 3113 | #ifdef TARGET_NR_nice /* not on alpha */ |
31e31b8a FB |
3114 | case TARGET_NR_nice: |
3115 | ret = get_errno(nice(arg1)); | |
3116 | break; | |
7a3148a9 | 3117 | #endif |
ebc05488 | 3118 | #ifdef TARGET_NR_ftime |
31e31b8a FB |
3119 | case TARGET_NR_ftime: |
3120 | goto unimplemented; | |
ebc05488 | 3121 | #endif |
31e31b8a | 3122 | case TARGET_NR_sync: |
04369ff2 FB |
3123 | sync(); |
3124 | ret = 0; | |
31e31b8a FB |
3125 | break; |
3126 | case TARGET_NR_kill: | |
3127 | ret = get_errno(kill(arg1, arg2)); | |
3128 | break; | |
3129 | case TARGET_NR_rename: | |
53a5960a PB |
3130 | { |
3131 | void *p2; | |
3132 | p = lock_user_string(arg1); | |
3133 | p2 = lock_user_string(arg2); | |
579a97f7 FB |
3134 | if (!p || !p2) |
3135 | ret = -TARGET_EFAULT; | |
3136 | else | |
3137 | ret = get_errno(rename(p, p2)); | |
53a5960a PB |
3138 | unlock_user(p2, arg2, 0); |
3139 | unlock_user(p, arg1, 0); | |
3140 | } | |
31e31b8a | 3141 | break; |
722183f6 TS |
3142 | #if defined(TARGET_NR_renameat) && defined(__NR_renameat) |
3143 | case TARGET_NR_renameat: | |
722183f6 | 3144 | { |
579a97f7 | 3145 | void *p2; |
722183f6 TS |
3146 | p = lock_user_string(arg2); |
3147 | p2 = lock_user_string(arg4); | |
579a97f7 | 3148 | if (!p || !p2) |
0da46a6e | 3149 | ret = -TARGET_EFAULT; |
722183f6 TS |
3150 | else |
3151 | ret = get_errno(sys_renameat(arg1, p, arg3, p2)); | |
579a97f7 FB |
3152 | unlock_user(p2, arg4, 0); |
3153 | unlock_user(p, arg2, 0); | |
722183f6 TS |
3154 | } |
3155 | break; | |
3156 | #endif | |
31e31b8a | 3157 | case TARGET_NR_mkdir: |
579a97f7 FB |
3158 | if (!(p = lock_user_string(arg1))) |
3159 | goto efault; | |
53a5960a PB |
3160 | ret = get_errno(mkdir(p, arg2)); |
3161 | unlock_user(p, arg1, 0); | |
31e31b8a | 3162 | break; |
4472ad0d TS |
3163 | #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat) |
3164 | case TARGET_NR_mkdirat: | |
579a97f7 FB |
3165 | if (!(p = lock_user_string(arg2))) |
3166 | goto efault; | |
3167 | ret = get_errno(sys_mkdirat(arg1, p, arg3)); | |
3168 | unlock_user(p, arg2, 0); | |
4472ad0d TS |
3169 | break; |
3170 | #endif | |
31e31b8a | 3171 | case TARGET_NR_rmdir: |
579a97f7 FB |
3172 | if (!(p = lock_user_string(arg1))) |
3173 | goto efault; | |
53a5960a PB |
3174 | ret = get_errno(rmdir(p)); |
3175 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
3176 | break; |
3177 | case TARGET_NR_dup: | |
3178 | ret = get_errno(dup(arg1)); | |
3179 | break; | |
3180 | case TARGET_NR_pipe: | |
3181 | { | |
53a5960a PB |
3182 | int host_pipe[2]; |
3183 | ret = get_errno(pipe(host_pipe)); | |
31e31b8a | 3184 | if (!is_error(ret)) { |
c12ab05c | 3185 | #if defined(TARGET_MIPS) |
ead9360e TS |
3186 | CPUMIPSState *env = (CPUMIPSState*)cpu_env; |
3187 | env->gpr[3][env->current_tc] = host_pipe[1]; | |
c12ab05c TS |
3188 | ret = host_pipe[0]; |
3189 | #else | |
53a5960a PB |
3190 | tput32(arg1, host_pipe[0]); |
3191 | tput32(arg1 + 4, host_pipe[1]); | |
c12ab05c | 3192 | #endif |
31e31b8a FB |
3193 | } |
3194 | } | |
3195 | break; | |
3196 | case TARGET_NR_times: | |
32f36bce | 3197 | { |
53a5960a | 3198 | struct target_tms *tmsp; |
32f36bce FB |
3199 | struct tms tms; |
3200 | ret = get_errno(times(&tms)); | |
53a5960a | 3201 | if (arg1) { |
579a97f7 FB |
3202 | tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0); |
3203 | if (!tmsp) | |
3204 | goto efault; | |
c596ed17 FB |
3205 | tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime)); |
3206 | tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime)); | |
3207 | tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime)); | |
3208 | tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime)); | |
32f36bce | 3209 | } |
c596ed17 FB |
3210 | if (!is_error(ret)) |
3211 | ret = host_to_target_clock_t(ret); | |
32f36bce FB |
3212 | } |
3213 | break; | |
ebc05488 | 3214 | #ifdef TARGET_NR_prof |
31e31b8a FB |
3215 | case TARGET_NR_prof: |
3216 | goto unimplemented; | |
ebc05488 | 3217 | #endif |
e5febef5 | 3218 | #ifdef TARGET_NR_signal |
31e31b8a FB |
3219 | case TARGET_NR_signal: |
3220 | goto unimplemented; | |
e5febef5 | 3221 | #endif |
31e31b8a | 3222 | case TARGET_NR_acct: |
579a97f7 FB |
3223 | if (!(p = lock_user_string(arg1))) |
3224 | goto efault; | |
24836689 PB |
3225 | ret = get_errno(acct(path(p))); |
3226 | unlock_user(p, arg1, 0); | |
3227 | break; | |
7a3148a9 | 3228 | #ifdef TARGET_NR_umount2 /* not on alpha */ |
31e31b8a | 3229 | case TARGET_NR_umount2: |
579a97f7 FB |
3230 | if (!(p = lock_user_string(arg1))) |
3231 | goto efault; | |
53a5960a PB |
3232 | ret = get_errno(umount2(p, arg2)); |
3233 | unlock_user(p, arg1, 0); | |
31e31b8a | 3234 | break; |
7a3148a9 | 3235 | #endif |
ebc05488 | 3236 | #ifdef TARGET_NR_lock |
31e31b8a FB |
3237 | case TARGET_NR_lock: |
3238 | goto unimplemented; | |
ebc05488 | 3239 | #endif |
31e31b8a FB |
3240 | case TARGET_NR_ioctl: |
3241 | ret = do_ioctl(arg1, arg2, arg3); | |
3242 | break; | |
3243 | case TARGET_NR_fcntl: | |
9ee1fa2c | 3244 | ret = do_fcntl(arg1, arg2, arg3); |
31e31b8a | 3245 | break; |
ebc05488 | 3246 | #ifdef TARGET_NR_mpx |
31e31b8a FB |
3247 | case TARGET_NR_mpx: |
3248 | goto unimplemented; | |
ebc05488 | 3249 | #endif |
31e31b8a FB |
3250 | case TARGET_NR_setpgid: |
3251 | ret = get_errno(setpgid(arg1, arg2)); | |
3252 | break; | |
ebc05488 | 3253 | #ifdef TARGET_NR_ulimit |
31e31b8a FB |
3254 | case TARGET_NR_ulimit: |
3255 | goto unimplemented; | |
ebc05488 FB |
3256 | #endif |
3257 | #ifdef TARGET_NR_oldolduname | |
31e31b8a FB |
3258 | case TARGET_NR_oldolduname: |
3259 | goto unimplemented; | |
ebc05488 | 3260 | #endif |
31e31b8a FB |
3261 | case TARGET_NR_umask: |
3262 | ret = get_errno(umask(arg1)); | |
3263 | break; | |
3264 | case TARGET_NR_chroot: | |
579a97f7 FB |
3265 | if (!(p = lock_user_string(arg1))) |
3266 | goto efault; | |
53a5960a PB |
3267 | ret = get_errno(chroot(p)); |
3268 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
3269 | break; |
3270 | case TARGET_NR_ustat: | |
3271 | goto unimplemented; | |
3272 | case TARGET_NR_dup2: | |
3273 | ret = get_errno(dup2(arg1, arg2)); | |
3274 | break; | |
7a3148a9 | 3275 | #ifdef TARGET_NR_getppid /* not on alpha */ |
31e31b8a FB |
3276 | case TARGET_NR_getppid: |
3277 | ret = get_errno(getppid()); | |
3278 | break; | |
7a3148a9 | 3279 | #endif |
31e31b8a FB |
3280 | case TARGET_NR_getpgrp: |
3281 | ret = get_errno(getpgrp()); | |
3282 | break; | |
3283 | case TARGET_NR_setsid: | |
3284 | ret = get_errno(setsid()); | |
3285 | break; | |
e5febef5 | 3286 | #ifdef TARGET_NR_sigaction |
31e31b8a | 3287 | case TARGET_NR_sigaction: |
31e31b8a | 3288 | { |
388bb21a | 3289 | #if !defined(TARGET_MIPS) |
53a5960a | 3290 | struct target_old_sigaction *old_act; |
66fb9763 | 3291 | struct target_sigaction act, oact, *pact; |
53a5960a | 3292 | if (arg2) { |
579a97f7 FB |
3293 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
3294 | goto efault; | |
66fb9763 FB |
3295 | act._sa_handler = old_act->_sa_handler; |
3296 | target_siginitset(&act.sa_mask, old_act->sa_mask); | |
3297 | act.sa_flags = old_act->sa_flags; | |
3298 | act.sa_restorer = old_act->sa_restorer; | |
53a5960a | 3299 | unlock_user_struct(old_act, arg2, 0); |
66fb9763 FB |
3300 | pact = &act; |
3301 | } else { | |
3302 | pact = NULL; | |
3303 | } | |
3304 | ret = get_errno(do_sigaction(arg1, pact, &oact)); | |
53a5960a | 3305 | if (!is_error(ret) && arg3) { |
579a97f7 FB |
3306 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
3307 | goto efault; | |
53a5960a PB |
3308 | old_act->_sa_handler = oact._sa_handler; |
3309 | old_act->sa_mask = oact.sa_mask.sig[0]; | |
3310 | old_act->sa_flags = oact.sa_flags; | |
3311 | old_act->sa_restorer = oact.sa_restorer; | |
3312 | unlock_user_struct(old_act, arg3, 1); | |
66fb9763 | 3313 | } |
388bb21a | 3314 | #else |
106ec879 FB |
3315 | struct target_sigaction act, oact, *pact, *old_act; |
3316 | ||
3317 | if (arg2) { | |
579a97f7 FB |
3318 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
3319 | goto efault; | |
106ec879 FB |
3320 | act._sa_handler = old_act->_sa_handler; |
3321 | target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]); | |
3322 | act.sa_flags = old_act->sa_flags; | |
3323 | unlock_user_struct(old_act, arg2, 0); | |
3324 | pact = &act; | |
3325 | } else { | |
3326 | pact = NULL; | |
3327 | } | |
3328 | ||
3329 | ret = get_errno(do_sigaction(arg1, pact, &oact)); | |
3330 | ||
3331 | if (!is_error(ret) && arg3) { | |
579a97f7 FB |
3332 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
3333 | goto efault; | |
106ec879 FB |
3334 | old_act->_sa_handler = oact._sa_handler; |
3335 | old_act->sa_flags = oact.sa_flags; | |
3336 | old_act->sa_mask.sig[0] = oact.sa_mask.sig[0]; | |
3337 | old_act->sa_mask.sig[1] = 0; | |
3338 | old_act->sa_mask.sig[2] = 0; | |
3339 | old_act->sa_mask.sig[3] = 0; | |
3340 | unlock_user_struct(old_act, arg3, 1); | |
3341 | } | |
388bb21a | 3342 | #endif |
31e31b8a FB |
3343 | } |
3344 | break; | |
e5febef5 | 3345 | #endif |
66fb9763 | 3346 | case TARGET_NR_rt_sigaction: |
53a5960a PB |
3347 | { |
3348 | struct target_sigaction *act; | |
3349 | struct target_sigaction *oact; | |
3350 | ||
579a97f7 FB |
3351 | if (arg2) { |
3352 | if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) | |
3353 | goto efault; | |
3354 | } else | |
53a5960a | 3355 | act = NULL; |
579a97f7 FB |
3356 | if (arg3) { |
3357 | if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) { | |
3358 | ret = -TARGET_EFAULT; | |
3359 | goto rt_sigaction_fail; | |
3360 | } | |
3361 | } else | |
53a5960a PB |
3362 | oact = NULL; |
3363 | ret = get_errno(do_sigaction(arg1, act, oact)); | |
579a97f7 FB |
3364 | rt_sigaction_fail: |
3365 | if (act) | |
53a5960a | 3366 | unlock_user_struct(act, arg2, 0); |
579a97f7 | 3367 | if (oact) |
53a5960a PB |
3368 | unlock_user_struct(oact, arg3, 1); |
3369 | } | |
66fb9763 | 3370 | break; |
7a3148a9 | 3371 | #ifdef TARGET_NR_sgetmask /* not on alpha */ |
31e31b8a | 3372 | case TARGET_NR_sgetmask: |
66fb9763 FB |
3373 | { |
3374 | sigset_t cur_set; | |
992f48a0 | 3375 | abi_ulong target_set; |
66fb9763 FB |
3376 | sigprocmask(0, NULL, &cur_set); |
3377 | host_to_target_old_sigset(&target_set, &cur_set); | |
3378 | ret = target_set; | |
3379 | } | |
3380 | break; | |
7a3148a9 JM |
3381 | #endif |
3382 | #ifdef TARGET_NR_ssetmask /* not on alpha */ | |
31e31b8a | 3383 | case TARGET_NR_ssetmask: |
66fb9763 FB |
3384 | { |
3385 | sigset_t set, oset, cur_set; | |
992f48a0 | 3386 | abi_ulong target_set = arg1; |
66fb9763 FB |
3387 | sigprocmask(0, NULL, &cur_set); |
3388 | target_to_host_old_sigset(&set, &target_set); | |
3389 | sigorset(&set, &set, &cur_set); | |
3390 | sigprocmask(SIG_SETMASK, &set, &oset); | |
3391 | host_to_target_old_sigset(&target_set, &oset); | |
3392 | ret = target_set; | |
3393 | } | |
3394 | break; | |
7a3148a9 | 3395 | #endif |
e5febef5 | 3396 | #ifdef TARGET_NR_sigprocmask |
66fb9763 FB |
3397 | case TARGET_NR_sigprocmask: |
3398 | { | |
3399 | int how = arg1; | |
3400 | sigset_t set, oldset, *set_ptr; | |
3b46e624 | 3401 | |
53a5960a | 3402 | if (arg2) { |
66fb9763 FB |
3403 | switch(how) { |
3404 | case TARGET_SIG_BLOCK: | |
3405 | how = SIG_BLOCK; | |
3406 | break; | |
3407 | case TARGET_SIG_UNBLOCK: | |
3408 | how = SIG_UNBLOCK; | |
3409 | break; | |
3410 | case TARGET_SIG_SETMASK: | |
3411 | how = SIG_SETMASK; | |
3412 | break; | |
3413 | default: | |
0da46a6e | 3414 | ret = -TARGET_EINVAL; |
66fb9763 FB |
3415 | goto fail; |
3416 | } | |
579a97f7 FB |
3417 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
3418 | goto efault; | |
53a5960a PB |
3419 | target_to_host_old_sigset(&set, p); |
3420 | unlock_user(p, arg2, 0); | |
66fb9763 FB |
3421 | set_ptr = &set; |
3422 | } else { | |
3423 | how = 0; | |
3424 | set_ptr = NULL; | |
3425 | } | |
3426 | ret = get_errno(sigprocmask(arg1, set_ptr, &oldset)); | |
53a5960a | 3427 | if (!is_error(ret) && arg3) { |
579a97f7 FB |
3428 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
3429 | goto efault; | |
53a5960a PB |
3430 | host_to_target_old_sigset(p, &oldset); |
3431 | unlock_user(p, arg3, sizeof(target_sigset_t)); | |
66fb9763 FB |
3432 | } |
3433 | } | |
3434 | break; | |
e5febef5 | 3435 | #endif |
66fb9763 FB |
3436 | case TARGET_NR_rt_sigprocmask: |
3437 | { | |
3438 | int how = arg1; | |
3439 | sigset_t set, oldset, *set_ptr; | |
3b46e624 | 3440 | |
53a5960a | 3441 | if (arg2) { |
66fb9763 FB |
3442 | switch(how) { |
3443 | case TARGET_SIG_BLOCK: | |
3444 | how = SIG_BLOCK; | |
3445 | break; | |
3446 | case TARGET_SIG_UNBLOCK: | |
3447 | how = SIG_UNBLOCK; | |
3448 | break; | |
3449 | case TARGET_SIG_SETMASK: | |
3450 | how = SIG_SETMASK; | |
3451 | break; | |
3452 | default: | |
0da46a6e | 3453 | ret = -TARGET_EINVAL; |
66fb9763 FB |
3454 | goto fail; |
3455 | } | |
579a97f7 FB |
3456 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
3457 | goto efault; | |
53a5960a PB |
3458 | target_to_host_sigset(&set, p); |
3459 | unlock_user(p, arg2, 0); | |
66fb9763 FB |
3460 | set_ptr = &set; |
3461 | } else { | |
3462 | how = 0; | |
3463 | set_ptr = NULL; | |
3464 | } | |
3465 | ret = get_errno(sigprocmask(how, set_ptr, &oldset)); | |
53a5960a | 3466 | if (!is_error(ret) && arg3) { |
579a97f7 FB |
3467 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
3468 | goto efault; | |
53a5960a PB |
3469 | host_to_target_sigset(p, &oldset); |
3470 | unlock_user(p, arg3, sizeof(target_sigset_t)); | |
66fb9763 FB |
3471 | } |
3472 | } | |
3473 | break; | |
e5febef5 | 3474 | #ifdef TARGET_NR_sigpending |
66fb9763 FB |
3475 | case TARGET_NR_sigpending: |
3476 | { | |
3477 | sigset_t set; | |
3478 | ret = get_errno(sigpending(&set)); | |
3479 | if (!is_error(ret)) { | |
579a97f7 FB |
3480 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
3481 | goto efault; | |
53a5960a PB |
3482 | host_to_target_old_sigset(p, &set); |
3483 | unlock_user(p, arg1, sizeof(target_sigset_t)); | |
66fb9763 FB |
3484 | } |
3485 | } | |
3486 | break; | |
e5febef5 | 3487 | #endif |
66fb9763 FB |
3488 | case TARGET_NR_rt_sigpending: |
3489 | { | |
3490 | sigset_t set; | |
3491 | ret = get_errno(sigpending(&set)); | |
3492 | if (!is_error(ret)) { | |
579a97f7 FB |
3493 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
3494 | goto efault; | |
53a5960a PB |
3495 | host_to_target_sigset(p, &set); |
3496 | unlock_user(p, arg1, sizeof(target_sigset_t)); | |
66fb9763 FB |
3497 | } |
3498 | } | |
3499 | break; | |
e5febef5 | 3500 | #ifdef TARGET_NR_sigsuspend |
66fb9763 FB |
3501 | case TARGET_NR_sigsuspend: |
3502 | { | |
3503 | sigset_t set; | |
579a97f7 FB |
3504 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
3505 | goto efault; | |
53a5960a PB |
3506 | target_to_host_old_sigset(&set, p); |
3507 | unlock_user(p, arg1, 0); | |
66fb9763 FB |
3508 | ret = get_errno(sigsuspend(&set)); |
3509 | } | |
3510 | break; | |
e5febef5 | 3511 | #endif |
66fb9763 FB |
3512 | case TARGET_NR_rt_sigsuspend: |
3513 | { | |
3514 | sigset_t set; | |
579a97f7 FB |
3515 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
3516 | goto efault; | |
53a5960a PB |
3517 | target_to_host_sigset(&set, p); |
3518 | unlock_user(p, arg1, 0); | |
66fb9763 FB |
3519 | ret = get_errno(sigsuspend(&set)); |
3520 | } | |
3521 | break; | |
3522 | case TARGET_NR_rt_sigtimedwait: | |
3523 | { | |
66fb9763 FB |
3524 | sigset_t set; |
3525 | struct timespec uts, *puts; | |
3526 | siginfo_t uinfo; | |
3b46e624 | 3527 | |
579a97f7 FB |
3528 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
3529 | goto efault; | |
53a5960a PB |
3530 | target_to_host_sigset(&set, p); |
3531 | unlock_user(p, arg1, 0); | |
3532 | if (arg3) { | |
66fb9763 | 3533 | puts = &uts; |
53a5960a | 3534 | target_to_host_timespec(puts, arg3); |
66fb9763 FB |
3535 | } else { |
3536 | puts = NULL; | |
3537 | } | |
3538 | ret = get_errno(sigtimedwait(&set, &uinfo, puts)); | |
53a5960a | 3539 | if (!is_error(ret) && arg2) { |
579a97f7 FB |
3540 | if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0))) |
3541 | goto efault; | |
53a5960a PB |
3542 | host_to_target_siginfo(p, &uinfo); |
3543 | unlock_user(p, arg2, sizeof(target_sigset_t)); | |
66fb9763 FB |
3544 | } |
3545 | } | |
3546 | break; | |
3547 | case TARGET_NR_rt_sigqueueinfo: | |
3548 | { | |
3549 | siginfo_t uinfo; | |
579a97f7 FB |
3550 | if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1))) |
3551 | goto efault; | |
53a5960a PB |
3552 | target_to_host_siginfo(&uinfo, p); |
3553 | unlock_user(p, arg1, 0); | |
66fb9763 FB |
3554 | ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo)); |
3555 | } | |
3556 | break; | |
e5febef5 | 3557 | #ifdef TARGET_NR_sigreturn |
66fb9763 FB |
3558 | case TARGET_NR_sigreturn: |
3559 | /* NOTE: ret is eax, so not transcoding must be done */ | |
3560 | ret = do_sigreturn(cpu_env); | |
3561 | break; | |
e5febef5 | 3562 | #endif |
66fb9763 FB |
3563 | case TARGET_NR_rt_sigreturn: |
3564 | /* NOTE: ret is eax, so not transcoding must be done */ | |
3565 | ret = do_rt_sigreturn(cpu_env); | |
3566 | break; | |
31e31b8a | 3567 | case TARGET_NR_sethostname: |
579a97f7 FB |
3568 | if (!(p = lock_user_string(arg1))) |
3569 | goto efault; | |
53a5960a PB |
3570 | ret = get_errno(sethostname(p, arg2)); |
3571 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
3572 | break; |
3573 | case TARGET_NR_setrlimit: | |
9de5e440 FB |
3574 | { |
3575 | /* XXX: convert resource ? */ | |
3576 | int resource = arg1; | |
53a5960a | 3577 | struct target_rlimit *target_rlim; |
9de5e440 | 3578 | struct rlimit rlim; |
579a97f7 FB |
3579 | if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) |
3580 | goto efault; | |
9de5e440 FB |
3581 | rlim.rlim_cur = tswapl(target_rlim->rlim_cur); |
3582 | rlim.rlim_max = tswapl(target_rlim->rlim_max); | |
53a5960a | 3583 | unlock_user_struct(target_rlim, arg2, 0); |
9de5e440 FB |
3584 | ret = get_errno(setrlimit(resource, &rlim)); |
3585 | } | |
3586 | break; | |
31e31b8a | 3587 | case TARGET_NR_getrlimit: |
9de5e440 FB |
3588 | { |
3589 | /* XXX: convert resource ? */ | |
3590 | int resource = arg1; | |
53a5960a | 3591 | struct target_rlimit *target_rlim; |
9de5e440 | 3592 | struct rlimit rlim; |
3b46e624 | 3593 | |
9de5e440 FB |
3594 | ret = get_errno(getrlimit(resource, &rlim)); |
3595 | if (!is_error(ret)) { | |
579a97f7 FB |
3596 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
3597 | goto efault; | |
53a5960a PB |
3598 | rlim.rlim_cur = tswapl(target_rlim->rlim_cur); |
3599 | rlim.rlim_max = tswapl(target_rlim->rlim_max); | |
3600 | unlock_user_struct(target_rlim, arg2, 1); | |
9de5e440 FB |
3601 | } |
3602 | } | |
3603 | break; | |
31e31b8a | 3604 | case TARGET_NR_getrusage: |
b409186b FB |
3605 | { |
3606 | struct rusage rusage; | |
b409186b FB |
3607 | ret = get_errno(getrusage(arg1, &rusage)); |
3608 | if (!is_error(ret)) { | |
53a5960a | 3609 | host_to_target_rusage(arg2, &rusage); |
b409186b FB |
3610 | } |
3611 | } | |
3612 | break; | |
31e31b8a FB |
3613 | case TARGET_NR_gettimeofday: |
3614 | { | |
31e31b8a FB |
3615 | struct timeval tv; |
3616 | ret = get_errno(gettimeofday(&tv, NULL)); | |
3617 | if (!is_error(ret)) { | |
53a5960a | 3618 | host_to_target_timeval(arg1, &tv); |
31e31b8a FB |
3619 | } |
3620 | } | |
3621 | break; | |
3622 | case TARGET_NR_settimeofday: | |
3623 | { | |
31e31b8a | 3624 | struct timeval tv; |
53a5960a | 3625 | target_to_host_timeval(&tv, arg1); |
31e31b8a FB |
3626 | ret = get_errno(settimeofday(&tv, NULL)); |
3627 | } | |
3628 | break; | |
048f6b4d | 3629 | #ifdef TARGET_NR_select |
31e31b8a | 3630 | case TARGET_NR_select: |
f2674e31 | 3631 | { |
53a5960a | 3632 | struct target_sel_arg_struct *sel; |
992f48a0 | 3633 | abi_ulong inp, outp, exp, tvp; |
53a5960a PB |
3634 | long nsel; |
3635 | ||
579a97f7 FB |
3636 | if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) |
3637 | goto efault; | |
53a5960a PB |
3638 | nsel = tswapl(sel->n); |
3639 | inp = tswapl(sel->inp); | |
3640 | outp = tswapl(sel->outp); | |
3641 | exp = tswapl(sel->exp); | |
3642 | tvp = tswapl(sel->tvp); | |
3643 | unlock_user_struct(sel, arg1, 0); | |
3644 | ret = do_select(nsel, inp, outp, exp, tvp); | |
f2674e31 FB |
3645 | } |
3646 | break; | |
048f6b4d | 3647 | #endif |
31e31b8a | 3648 | case TARGET_NR_symlink: |
53a5960a PB |
3649 | { |
3650 | void *p2; | |
3651 | p = lock_user_string(arg1); | |
3652 | p2 = lock_user_string(arg2); | |
579a97f7 FB |
3653 | if (!p || !p2) |
3654 | ret = -TARGET_EFAULT; | |
3655 | else | |
3656 | ret = get_errno(symlink(p, p2)); | |
53a5960a PB |
3657 | unlock_user(p2, arg2, 0); |
3658 | unlock_user(p, arg1, 0); | |
3659 | } | |
31e31b8a | 3660 | break; |
f0b6243d TS |
3661 | #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat) |
3662 | case TARGET_NR_symlinkat: | |
f0b6243d | 3663 | { |
579a97f7 | 3664 | void *p2; |
f0b6243d TS |
3665 | p = lock_user_string(arg1); |
3666 | p2 = lock_user_string(arg3); | |
579a97f7 | 3667 | if (!p || !p2) |
0da46a6e | 3668 | ret = -TARGET_EFAULT; |
f0b6243d TS |
3669 | else |
3670 | ret = get_errno(sys_symlinkat(p, arg2, p2)); | |
579a97f7 FB |
3671 | unlock_user(p2, arg3, 0); |
3672 | unlock_user(p, arg1, 0); | |
f0b6243d TS |
3673 | } |
3674 | break; | |
3675 | #endif | |
ebc05488 | 3676 | #ifdef TARGET_NR_oldlstat |
31e31b8a FB |
3677 | case TARGET_NR_oldlstat: |
3678 | goto unimplemented; | |
ebc05488 | 3679 | #endif |
31e31b8a | 3680 | case TARGET_NR_readlink: |
53a5960a PB |
3681 | { |
3682 | void *p2; | |
3683 | p = lock_user_string(arg1); | |
579a97f7 FB |
3684 | p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
3685 | if (!p || !p2) | |
3686 | ret = -TARGET_EFAULT; | |
3687 | else | |
3688 | ret = get_errno(readlink(path(p), p2, arg3)); | |
53a5960a PB |
3689 | unlock_user(p2, arg2, ret); |
3690 | unlock_user(p, arg1, 0); | |
3691 | } | |
31e31b8a | 3692 | break; |
5e0ccb18 TS |
3693 | #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat) |
3694 | case TARGET_NR_readlinkat: | |
5e0ccb18 | 3695 | { |
579a97f7 | 3696 | void *p2; |
5e0ccb18 | 3697 | p = lock_user_string(arg2); |
579a97f7 FB |
3698 | p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
3699 | if (!p || !p2) | |
0da46a6e | 3700 | ret = -TARGET_EFAULT; |
5e0ccb18 TS |
3701 | else |
3702 | ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4)); | |
579a97f7 FB |
3703 | unlock_user(p2, arg3, ret); |
3704 | unlock_user(p, arg2, 0); | |
5e0ccb18 TS |
3705 | } |
3706 | break; | |
3707 | #endif | |
e5febef5 | 3708 | #ifdef TARGET_NR_uselib |
31e31b8a FB |
3709 | case TARGET_NR_uselib: |
3710 | goto unimplemented; | |
e5febef5 TS |
3711 | #endif |
3712 | #ifdef TARGET_NR_swapon | |
31e31b8a | 3713 | case TARGET_NR_swapon: |
579a97f7 FB |
3714 | if (!(p = lock_user_string(arg1))) |
3715 | goto efault; | |
53a5960a PB |
3716 | ret = get_errno(swapon(p, arg2)); |
3717 | unlock_user(p, arg1, 0); | |
31e31b8a | 3718 | break; |
e5febef5 | 3719 | #endif |
31e31b8a FB |
3720 | case TARGET_NR_reboot: |
3721 | goto unimplemented; | |
e5febef5 | 3722 | #ifdef TARGET_NR_readdir |
31e31b8a FB |
3723 | case TARGET_NR_readdir: |
3724 | goto unimplemented; | |
e5febef5 TS |
3725 | #endif |
3726 | #ifdef TARGET_NR_mmap | |
31e31b8a | 3727 | case TARGET_NR_mmap: |
48733d19 | 3728 | #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) |
31e31b8a | 3729 | { |
992f48a0 BS |
3730 | abi_ulong *v; |
3731 | abi_ulong v1, v2, v3, v4, v5, v6; | |
579a97f7 FB |
3732 | if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1))) |
3733 | goto efault; | |
53a5960a PB |
3734 | v1 = tswapl(v[0]); |
3735 | v2 = tswapl(v[1]); | |
3736 | v3 = tswapl(v[2]); | |
3737 | v4 = tswapl(v[3]); | |
3738 | v5 = tswapl(v[4]); | |
3739 | v6 = tswapl(v[5]); | |
3740 | unlock_user(v, arg1, 0); | |
5fafdf24 | 3741 | ret = get_errno(target_mmap(v1, v2, v3, |
5286db75 FB |
3742 | target_to_host_bitmask(v4, mmap_flags_tbl), |
3743 | v5, v6)); | |
31e31b8a | 3744 | } |
31e31b8a | 3745 | #else |
5fafdf24 TS |
3746 | ret = get_errno(target_mmap(arg1, arg2, arg3, |
3747 | target_to_host_bitmask(arg4, mmap_flags_tbl), | |
6fb883e8 FB |
3748 | arg5, |
3749 | arg6)); | |
31e31b8a | 3750 | #endif |
6fb883e8 | 3751 | break; |
e5febef5 | 3752 | #endif |
a315a145 | 3753 | #ifdef TARGET_NR_mmap2 |
6fb883e8 | 3754 | case TARGET_NR_mmap2: |
0d3267a7 | 3755 | #if defined(TARGET_SPARC) || defined(TARGET_MIPS) |
c573ff67 FB |
3756 | #define MMAP_SHIFT 12 |
3757 | #else | |
3758 | #define MMAP_SHIFT TARGET_PAGE_BITS | |
3759 | #endif | |
5fafdf24 TS |
3760 | ret = get_errno(target_mmap(arg1, arg2, arg3, |
3761 | target_to_host_bitmask(arg4, mmap_flags_tbl), | |
5286db75 | 3762 | arg5, |
c573ff67 | 3763 | arg6 << MMAP_SHIFT)); |
31e31b8a | 3764 | break; |
a315a145 | 3765 | #endif |
31e31b8a | 3766 | case TARGET_NR_munmap: |
54936004 | 3767 | ret = get_errno(target_munmap(arg1, arg2)); |
31e31b8a | 3768 | break; |
9de5e440 | 3769 | case TARGET_NR_mprotect: |
54936004 | 3770 | ret = get_errno(target_mprotect(arg1, arg2, arg3)); |
9de5e440 | 3771 | break; |
e5febef5 | 3772 | #ifdef TARGET_NR_mremap |
9de5e440 | 3773 | case TARGET_NR_mremap: |
54936004 | 3774 | ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5)); |
9de5e440 | 3775 | break; |
e5febef5 | 3776 | #endif |
53a5960a | 3777 | /* ??? msync/mlock/munlock are broken for softmmu. */ |
e5febef5 | 3778 | #ifdef TARGET_NR_msync |
9de5e440 | 3779 | case TARGET_NR_msync: |
53a5960a | 3780 | ret = get_errno(msync(g2h(arg1), arg2, arg3)); |
9de5e440 | 3781 | break; |
e5febef5 TS |
3782 | #endif |
3783 | #ifdef TARGET_NR_mlock | |
9de5e440 | 3784 | case TARGET_NR_mlock: |
53a5960a | 3785 | ret = get_errno(mlock(g2h(arg1), arg2)); |
9de5e440 | 3786 | break; |
e5febef5 TS |
3787 | #endif |
3788 | #ifdef TARGET_NR_munlock | |
9de5e440 | 3789 | case TARGET_NR_munlock: |
53a5960a | 3790 | ret = get_errno(munlock(g2h(arg1), arg2)); |
9de5e440 | 3791 | break; |
e5febef5 TS |
3792 | #endif |
3793 | #ifdef TARGET_NR_mlockall | |
9de5e440 FB |
3794 | case TARGET_NR_mlockall: |
3795 | ret = get_errno(mlockall(arg1)); | |
3796 | break; | |
e5febef5 TS |
3797 | #endif |
3798 | #ifdef TARGET_NR_munlockall | |
9de5e440 FB |
3799 | case TARGET_NR_munlockall: |
3800 | ret = get_errno(munlockall()); | |
3801 | break; | |
e5febef5 | 3802 | #endif |
31e31b8a | 3803 | case TARGET_NR_truncate: |
579a97f7 FB |
3804 | if (!(p = lock_user_string(arg1))) |
3805 | goto efault; | |
53a5960a PB |
3806 | ret = get_errno(truncate(p, arg2)); |
3807 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
3808 | break; |
3809 | case TARGET_NR_ftruncate: | |
3810 | ret = get_errno(ftruncate(arg1, arg2)); | |
3811 | break; | |
3812 | case TARGET_NR_fchmod: | |
3813 | ret = get_errno(fchmod(arg1, arg2)); | |
3814 | break; | |
814d7977 TS |
3815 | #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat) |
3816 | case TARGET_NR_fchmodat: | |
579a97f7 FB |
3817 | if (!(p = lock_user_string(arg2))) |
3818 | goto efault; | |
3819 | ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4)); | |
3820 | unlock_user(p, arg2, 0); | |
814d7977 TS |
3821 | break; |
3822 | #endif | |
31e31b8a | 3823 | case TARGET_NR_getpriority: |
c6cda17a TS |
3824 | /* libc does special remapping of the return value of |
3825 | * sys_getpriority() so it's just easiest to call | |
3826 | * sys_getpriority() directly rather than through libc. */ | |
3827 | ret = sys_getpriority(arg1, arg2); | |
31e31b8a FB |
3828 | break; |
3829 | case TARGET_NR_setpriority: | |
3830 | ret = get_errno(setpriority(arg1, arg2, arg3)); | |
3831 | break; | |
ebc05488 | 3832 | #ifdef TARGET_NR_profil |
31e31b8a FB |
3833 | case TARGET_NR_profil: |
3834 | goto unimplemented; | |
ebc05488 | 3835 | #endif |
31e31b8a | 3836 | case TARGET_NR_statfs: |
579a97f7 FB |
3837 | if (!(p = lock_user_string(arg1))) |
3838 | goto efault; | |
53a5960a PB |
3839 | ret = get_errno(statfs(path(p), &stfs)); |
3840 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
3841 | convert_statfs: |
3842 | if (!is_error(ret)) { | |
53a5960a | 3843 | struct target_statfs *target_stfs; |
3b46e624 | 3844 | |
579a97f7 FB |
3845 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0)) |
3846 | goto efault; | |
3847 | __put_user(stfs.f_type, &target_stfs->f_type); | |
3848 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); | |
3849 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); | |
3850 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); | |
3851 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); | |
3852 | __put_user(stfs.f_files, &target_stfs->f_files); | |
3853 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); | |
3854 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); | |
3855 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); | |
3856 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); | |
53a5960a | 3857 | unlock_user_struct(target_stfs, arg2, 1); |
31e31b8a FB |
3858 | } |
3859 | break; | |
3860 | case TARGET_NR_fstatfs: | |
56c8f68f | 3861 | ret = get_errno(fstatfs(arg1, &stfs)); |
31e31b8a | 3862 | goto convert_statfs; |
56c8f68f FB |
3863 | #ifdef TARGET_NR_statfs64 |
3864 | case TARGET_NR_statfs64: | |
579a97f7 FB |
3865 | if (!(p = lock_user_string(arg1))) |
3866 | goto efault; | |
53a5960a PB |
3867 | ret = get_errno(statfs(path(p), &stfs)); |
3868 | unlock_user(p, arg1, 0); | |
56c8f68f FB |
3869 | convert_statfs64: |
3870 | if (!is_error(ret)) { | |
53a5960a | 3871 | struct target_statfs64 *target_stfs; |
3b46e624 | 3872 | |
579a97f7 FB |
3873 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0)) |
3874 | goto efault; | |
3875 | __put_user(stfs.f_type, &target_stfs->f_type); | |
3876 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); | |
3877 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); | |
3878 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); | |
3879 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); | |
3880 | __put_user(stfs.f_files, &target_stfs->f_files); | |
3881 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); | |
3882 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); | |
3883 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); | |
3884 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); | |
3885 | unlock_user_struct(target_stfs, arg3, 1); | |
56c8f68f FB |
3886 | } |
3887 | break; | |
3888 | case TARGET_NR_fstatfs64: | |
3889 | ret = get_errno(fstatfs(arg1, &stfs)); | |
3890 | goto convert_statfs64; | |
3891 | #endif | |
ebc05488 | 3892 | #ifdef TARGET_NR_ioperm |
31e31b8a FB |
3893 | case TARGET_NR_ioperm: |
3894 | goto unimplemented; | |
ebc05488 | 3895 | #endif |
e5febef5 | 3896 | #ifdef TARGET_NR_socketcall |
31e31b8a | 3897 | case TARGET_NR_socketcall: |
53a5960a | 3898 | ret = do_socketcall(arg1, arg2); |
31e31b8a | 3899 | break; |
e5febef5 | 3900 | #endif |
3532fa74 FB |
3901 | #ifdef TARGET_NR_accept |
3902 | case TARGET_NR_accept: | |
1be9e1dc | 3903 | ret = do_accept(arg1, arg2, arg3); |
3532fa74 FB |
3904 | break; |
3905 | #endif | |
3906 | #ifdef TARGET_NR_bind | |
3907 | case TARGET_NR_bind: | |
3908 | ret = do_bind(arg1, arg2, arg3); | |
3909 | break; | |
3910 | #endif | |
3911 | #ifdef TARGET_NR_connect | |
3912 | case TARGET_NR_connect: | |
3913 | ret = do_connect(arg1, arg2, arg3); | |
3914 | break; | |
3915 | #endif | |
3916 | #ifdef TARGET_NR_getpeername | |
3917 | case TARGET_NR_getpeername: | |
1be9e1dc | 3918 | ret = do_getpeername(arg1, arg2, arg3); |
3532fa74 FB |
3919 | break; |
3920 | #endif | |
3921 | #ifdef TARGET_NR_getsockname | |
3922 | case TARGET_NR_getsockname: | |
1be9e1dc | 3923 | ret = do_getsockname(arg1, arg2, arg3); |
3532fa74 FB |
3924 | break; |
3925 | #endif | |
3926 | #ifdef TARGET_NR_getsockopt | |
3927 | case TARGET_NR_getsockopt: | |
3928 | ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5); | |
3929 | break; | |
3930 | #endif | |
3931 | #ifdef TARGET_NR_listen | |
3932 | case TARGET_NR_listen: | |
1be9e1dc | 3933 | ret = get_errno(listen(arg1, arg2)); |
3532fa74 FB |
3934 | break; |
3935 | #endif | |
3936 | #ifdef TARGET_NR_recv | |
3937 | case TARGET_NR_recv: | |
214201bd | 3938 | ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0); |
3532fa74 FB |
3939 | break; |
3940 | #endif | |
3941 | #ifdef TARGET_NR_recvfrom | |
3942 | case TARGET_NR_recvfrom: | |
214201bd | 3943 | ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); |
3532fa74 FB |
3944 | break; |
3945 | #endif | |
3946 | #ifdef TARGET_NR_recvmsg | |
3947 | case TARGET_NR_recvmsg: | |
3948 | ret = do_sendrecvmsg(arg1, arg2, arg3, 0); | |
3949 | break; | |
3950 | #endif | |
3951 | #ifdef TARGET_NR_send | |
3952 | case TARGET_NR_send: | |
1be9e1dc | 3953 | ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0); |
3532fa74 FB |
3954 | break; |
3955 | #endif | |
3956 | #ifdef TARGET_NR_sendmsg | |
3957 | case TARGET_NR_sendmsg: | |
3958 | ret = do_sendrecvmsg(arg1, arg2, arg3, 1); | |
3959 | break; | |
3960 | #endif | |
3961 | #ifdef TARGET_NR_sendto | |
3962 | case TARGET_NR_sendto: | |
1be9e1dc | 3963 | ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6); |
3532fa74 FB |
3964 | break; |
3965 | #endif | |
3966 | #ifdef TARGET_NR_shutdown | |
3967 | case TARGET_NR_shutdown: | |
1be9e1dc | 3968 | ret = get_errno(shutdown(arg1, arg2)); |
3532fa74 FB |
3969 | break; |
3970 | #endif | |
3971 | #ifdef TARGET_NR_socket | |
3972 | case TARGET_NR_socket: | |
3973 | ret = do_socket(arg1, arg2, arg3); | |
3974 | break; | |
3975 | #endif | |
3976 | #ifdef TARGET_NR_socketpair | |
3977 | case TARGET_NR_socketpair: | |
1be9e1dc | 3978 | ret = do_socketpair(arg1, arg2, arg3, arg4); |
3532fa74 FB |
3979 | break; |
3980 | #endif | |
3981 | #ifdef TARGET_NR_setsockopt | |
3982 | case TARGET_NR_setsockopt: | |
3983 | ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5); | |
3984 | break; | |
3985 | #endif | |
7494b0f9 | 3986 | |
31e31b8a | 3987 | case TARGET_NR_syslog: |
579a97f7 FB |
3988 | if (!(p = lock_user_string(arg2))) |
3989 | goto efault; | |
e5574487 TS |
3990 | ret = get_errno(sys_syslog((int)arg1, p, (int)arg3)); |
3991 | unlock_user(p, arg2, 0); | |
7494b0f9 TS |
3992 | break; |
3993 | ||
31e31b8a | 3994 | case TARGET_NR_setitimer: |
66fb9763 | 3995 | { |
66fb9763 FB |
3996 | struct itimerval value, ovalue, *pvalue; |
3997 | ||
53a5960a | 3998 | if (arg2) { |
66fb9763 | 3999 | pvalue = &value; |
5fafdf24 | 4000 | target_to_host_timeval(&pvalue->it_interval, |
53a5960a | 4001 | arg2); |
5fafdf24 | 4002 | target_to_host_timeval(&pvalue->it_value, |
53a5960a | 4003 | arg2 + sizeof(struct target_timeval)); |
66fb9763 FB |
4004 | } else { |
4005 | pvalue = NULL; | |
4006 | } | |
4007 | ret = get_errno(setitimer(arg1, pvalue, &ovalue)); | |
53a5960a PB |
4008 | if (!is_error(ret) && arg3) { |
4009 | host_to_target_timeval(arg3, | |
66fb9763 | 4010 | &ovalue.it_interval); |
53a5960a | 4011 | host_to_target_timeval(arg3 + sizeof(struct target_timeval), |
66fb9763 FB |
4012 | &ovalue.it_value); |
4013 | } | |
4014 | } | |
4015 | break; | |
31e31b8a | 4016 | case TARGET_NR_getitimer: |
66fb9763 | 4017 | { |
66fb9763 | 4018 | struct itimerval value; |
3b46e624 | 4019 | |
66fb9763 | 4020 | ret = get_errno(getitimer(arg1, &value)); |
53a5960a PB |
4021 | if (!is_error(ret) && arg2) { |
4022 | host_to_target_timeval(arg2, | |
66fb9763 | 4023 | &value.it_interval); |
53a5960a | 4024 | host_to_target_timeval(arg2 + sizeof(struct target_timeval), |
66fb9763 FB |
4025 | &value.it_value); |
4026 | } | |
4027 | } | |
4028 | break; | |
31e31b8a | 4029 | case TARGET_NR_stat: |
579a97f7 FB |
4030 | if (!(p = lock_user_string(arg1))) |
4031 | goto efault; | |
53a5960a PB |
4032 | ret = get_errno(stat(path(p), &st)); |
4033 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
4034 | goto do_stat; |
4035 | case TARGET_NR_lstat: | |
579a97f7 FB |
4036 | if (!(p = lock_user_string(arg1))) |
4037 | goto efault; | |
53a5960a PB |
4038 | ret = get_errno(lstat(path(p), &st)); |
4039 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
4040 | goto do_stat; |
4041 | case TARGET_NR_fstat: | |
4042 | { | |
4043 | ret = get_errno(fstat(arg1, &st)); | |
4044 | do_stat: | |
4045 | if (!is_error(ret)) { | |
53a5960a | 4046 | struct target_stat *target_st; |
e3584658 | 4047 | |
579a97f7 FB |
4048 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) |
4049 | goto efault; | |
992f48a0 | 4050 | #if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32)) |
e3584658 TS |
4051 | target_st->st_dev = tswap32(st.st_dev); |
4052 | #else | |
31e31b8a | 4053 | target_st->st_dev = tswap16(st.st_dev); |
e3584658 | 4054 | #endif |
31e31b8a | 4055 | target_st->st_ino = tswapl(st.st_ino); |
7d600c80 | 4056 | #if defined(TARGET_PPC) || defined(TARGET_MIPS) |
67867308 FB |
4057 | target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */ |
4058 | target_st->st_uid = tswap32(st.st_uid); | |
4059 | target_st->st_gid = tswap32(st.st_gid); | |
992f48a0 | 4060 | #elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32) |
1b8dd648 BS |
4061 | target_st->st_mode = tswap32(st.st_mode); |
4062 | target_st->st_uid = tswap32(st.st_uid); | |
4063 | target_st->st_gid = tswap32(st.st_gid); | |
67867308 | 4064 | #else |
ec86b0fb | 4065 | target_st->st_mode = tswap16(st.st_mode); |
31e31b8a FB |
4066 | target_st->st_uid = tswap16(st.st_uid); |
4067 | target_st->st_gid = tswap16(st.st_gid); | |
67867308 | 4068 | #endif |
e3584658 TS |
4069 | #if defined(TARGET_MIPS) |
4070 | /* If this is the same on PPC, then just merge w/ the above ifdef */ | |
4071 | target_st->st_nlink = tswapl(st.st_nlink); | |
4072 | target_st->st_rdev = tswapl(st.st_rdev); | |
992f48a0 | 4073 | #elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32) |
1b8dd648 BS |
4074 | target_st->st_nlink = tswap32(st.st_nlink); |
4075 | target_st->st_rdev = tswap32(st.st_rdev); | |
e3584658 | 4076 | #else |
67867308 | 4077 | target_st->st_nlink = tswap16(st.st_nlink); |
31e31b8a | 4078 | target_st->st_rdev = tswap16(st.st_rdev); |
e3584658 | 4079 | #endif |
31e31b8a FB |
4080 | target_st->st_size = tswapl(st.st_size); |
4081 | target_st->st_blksize = tswapl(st.st_blksize); | |
4082 | target_st->st_blocks = tswapl(st.st_blocks); | |
7854b056 FB |
4083 | target_st->target_st_atime = tswapl(st.st_atime); |
4084 | target_st->target_st_mtime = tswapl(st.st_mtime); | |
4085 | target_st->target_st_ctime = tswapl(st.st_ctime); | |
53a5960a | 4086 | unlock_user_struct(target_st, arg2, 1); |
31e31b8a FB |
4087 | } |
4088 | } | |
4089 | break; | |
ebc05488 | 4090 | #ifdef TARGET_NR_olduname |
31e31b8a FB |
4091 | case TARGET_NR_olduname: |
4092 | goto unimplemented; | |
ebc05488 FB |
4093 | #endif |
4094 | #ifdef TARGET_NR_iopl | |
31e31b8a FB |
4095 | case TARGET_NR_iopl: |
4096 | goto unimplemented; | |
ebc05488 | 4097 | #endif |
31e31b8a FB |
4098 | case TARGET_NR_vhangup: |
4099 | ret = get_errno(vhangup()); | |
4100 | break; | |
ebc05488 | 4101 | #ifdef TARGET_NR_idle |
31e31b8a FB |
4102 | case TARGET_NR_idle: |
4103 | goto unimplemented; | |
42ad6ae9 FB |
4104 | #endif |
4105 | #ifdef TARGET_NR_syscall | |
4106 | case TARGET_NR_syscall: | |
4107 | ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0); | |
4108 | break; | |
ebc05488 | 4109 | #endif |
31e31b8a FB |
4110 | case TARGET_NR_wait4: |
4111 | { | |
4112 | int status; | |
992f48a0 | 4113 | abi_long status_ptr = arg2; |
31e31b8a | 4114 | struct rusage rusage, *rusage_ptr; |
992f48a0 | 4115 | abi_ulong target_rusage = arg4; |
31e31b8a FB |
4116 | if (target_rusage) |
4117 | rusage_ptr = &rusage; | |
4118 | else | |
4119 | rusage_ptr = NULL; | |
4120 | ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr)); | |
4121 | if (!is_error(ret)) { | |
4122 | if (status_ptr) | |
53a5960a | 4123 | tputl(status_ptr, status); |
31e31b8a | 4124 | if (target_rusage) { |
b409186b | 4125 | host_to_target_rusage(target_rusage, &rusage); |
31e31b8a FB |
4126 | } |
4127 | } | |
4128 | } | |
4129 | break; | |
e5febef5 | 4130 | #ifdef TARGET_NR_swapoff |
31e31b8a | 4131 | case TARGET_NR_swapoff: |
579a97f7 FB |
4132 | if (!(p = lock_user_string(arg1))) |
4133 | goto efault; | |
53a5960a PB |
4134 | ret = get_errno(swapoff(p)); |
4135 | unlock_user(p, arg1, 0); | |
31e31b8a | 4136 | break; |
e5febef5 | 4137 | #endif |
31e31b8a | 4138 | case TARGET_NR_sysinfo: |
a5448a7d | 4139 | { |
53a5960a | 4140 | struct target_sysinfo *target_value; |
a5448a7d FB |
4141 | struct sysinfo value; |
4142 | ret = get_errno(sysinfo(&value)); | |
53a5960a | 4143 | if (!is_error(ret) && arg1) |
a5448a7d | 4144 | { |
579a97f7 FB |
4145 | if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0)) |
4146 | goto efault; | |
a5448a7d FB |
4147 | __put_user(value.uptime, &target_value->uptime); |
4148 | __put_user(value.loads[0], &target_value->loads[0]); | |
4149 | __put_user(value.loads[1], &target_value->loads[1]); | |
4150 | __put_user(value.loads[2], &target_value->loads[2]); | |
4151 | __put_user(value.totalram, &target_value->totalram); | |
4152 | __put_user(value.freeram, &target_value->freeram); | |
4153 | __put_user(value.sharedram, &target_value->sharedram); | |
4154 | __put_user(value.bufferram, &target_value->bufferram); | |
4155 | __put_user(value.totalswap, &target_value->totalswap); | |
4156 | __put_user(value.freeswap, &target_value->freeswap); | |
4157 | __put_user(value.procs, &target_value->procs); | |
4158 | __put_user(value.totalhigh, &target_value->totalhigh); | |
4159 | __put_user(value.freehigh, &target_value->freehigh); | |
4160 | __put_user(value.mem_unit, &target_value->mem_unit); | |
53a5960a | 4161 | unlock_user_struct(target_value, arg1, 1); |
a5448a7d FB |
4162 | } |
4163 | } | |
4164 | break; | |
e5febef5 | 4165 | #ifdef TARGET_NR_ipc |
31e31b8a | 4166 | case TARGET_NR_ipc: |
8853f86e FB |
4167 | ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6); |
4168 | break; | |
e5febef5 | 4169 | #endif |
31e31b8a FB |
4170 | case TARGET_NR_fsync: |
4171 | ret = get_errno(fsync(arg1)); | |
4172 | break; | |
31e31b8a | 4173 | case TARGET_NR_clone: |
1b6b029e FB |
4174 | ret = get_errno(do_fork(cpu_env, arg1, arg2)); |
4175 | break; | |
ec86b0fb FB |
4176 | #ifdef __NR_exit_group |
4177 | /* new thread calls */ | |
4178 | case TARGET_NR_exit_group: | |
e9009676 | 4179 | gdb_exit(cpu_env, arg1); |
ec86b0fb FB |
4180 | ret = get_errno(exit_group(arg1)); |
4181 | break; | |
4182 | #endif | |
31e31b8a | 4183 | case TARGET_NR_setdomainname: |
579a97f7 FB |
4184 | if (!(p = lock_user_string(arg1))) |
4185 | goto efault; | |
53a5960a PB |
4186 | ret = get_errno(setdomainname(p, arg2)); |
4187 | unlock_user(p, arg1, 0); | |
31e31b8a FB |
4188 | break; |
4189 | case TARGET_NR_uname: | |
4190 | /* no need to transcode because we use the linux syscall */ | |
29e619b1 FB |
4191 | { |
4192 | struct new_utsname * buf; | |
3b46e624 | 4193 | |
579a97f7 FB |
4194 | if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0)) |
4195 | goto efault; | |
29e619b1 FB |
4196 | ret = get_errno(sys_uname(buf)); |
4197 | if (!is_error(ret)) { | |
4198 | /* Overrite the native machine name with whatever is being | |
4199 | emulated. */ | |
4200 | strcpy (buf->machine, UNAME_MACHINE); | |
c5937220 PB |
4201 | /* Allow the user to override the reported release. */ |
4202 | if (qemu_uname_release && *qemu_uname_release) | |
4203 | strcpy (buf->release, qemu_uname_release); | |
29e619b1 | 4204 | } |
53a5960a | 4205 | unlock_user_struct(buf, arg1, 1); |
29e619b1 | 4206 | } |
31e31b8a | 4207 | break; |
6dbad63e | 4208 | #ifdef TARGET_I386 |
31e31b8a | 4209 | case TARGET_NR_modify_ldt: |
03acab66 | 4210 | ret = do_modify_ldt(cpu_env, arg1, arg2, arg3); |
5cd4393b | 4211 | break; |
84409ddb | 4212 | #if !defined(TARGET_X86_64) |
5cd4393b FB |
4213 | case TARGET_NR_vm86old: |
4214 | goto unimplemented; | |
4215 | case TARGET_NR_vm86: | |
53a5960a | 4216 | ret = do_vm86(cpu_env, arg1, arg2); |
6dbad63e | 4217 | break; |
84409ddb | 4218 | #endif |
6dbad63e | 4219 | #endif |
31e31b8a FB |
4220 | case TARGET_NR_adjtimex: |
4221 | goto unimplemented; | |
e5febef5 | 4222 | #ifdef TARGET_NR_create_module |
31e31b8a | 4223 | case TARGET_NR_create_module: |
e5febef5 | 4224 | #endif |
31e31b8a FB |
4225 | case TARGET_NR_init_module: |
4226 | case TARGET_NR_delete_module: | |
e5febef5 | 4227 | #ifdef TARGET_NR_get_kernel_syms |
31e31b8a | 4228 | case TARGET_NR_get_kernel_syms: |
e5febef5 | 4229 | #endif |
31e31b8a FB |
4230 | goto unimplemented; |
4231 | case TARGET_NR_quotactl: | |
4232 | goto unimplemented; | |
4233 | case TARGET_NR_getpgid: | |
4234 | ret = get_errno(getpgid(arg1)); | |
4235 | break; | |
4236 | case TARGET_NR_fchdir: | |
4237 | ret = get_errno(fchdir(arg1)); | |
4238 | break; | |
84409ddb | 4239 | #ifdef TARGET_NR_bdflush /* not on x86_64 */ |
31e31b8a FB |
4240 | case TARGET_NR_bdflush: |
4241 | goto unimplemented; | |
84409ddb | 4242 | #endif |
e5febef5 | 4243 | #ifdef TARGET_NR_sysfs |
31e31b8a FB |
4244 | case TARGET_NR_sysfs: |
4245 | goto unimplemented; | |
e5febef5 | 4246 | #endif |
31e31b8a | 4247 | case TARGET_NR_personality: |
1b6b029e | 4248 | ret = get_errno(personality(arg1)); |
31e31b8a | 4249 | break; |
e5febef5 | 4250 | #ifdef TARGET_NR_afs_syscall |
31e31b8a FB |
4251 | case TARGET_NR_afs_syscall: |
4252 | goto unimplemented; | |
e5febef5 | 4253 | #endif |
7a3148a9 | 4254 | #ifdef TARGET_NR__llseek /* Not on alpha */ |
31e31b8a FB |
4255 | case TARGET_NR__llseek: |
4256 | { | |
4f2ac237 FB |
4257 | #if defined (__x86_64__) |
4258 | ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5)); | |
53a5960a | 4259 | tput64(arg4, ret); |
4f2ac237 | 4260 | #else |
31e31b8a FB |
4261 | int64_t res; |
4262 | ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5)); | |
53a5960a | 4263 | tput64(arg4, res); |
4f2ac237 | 4264 | #endif |
31e31b8a FB |
4265 | } |
4266 | break; | |
7a3148a9 | 4267 | #endif |
31e31b8a | 4268 | case TARGET_NR_getdents: |
992f48a0 | 4269 | #if TARGET_ABI_BITS != 32 |
53a5960a | 4270 | goto unimplemented; |
a315a145 | 4271 | #warning not supported |
992f48a0 | 4272 | #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 |
4add45b4 | 4273 | { |
53a5960a | 4274 | struct target_dirent *target_dirp; |
4add45b4 | 4275 | struct dirent *dirp; |
992f48a0 | 4276 | abi_long count = arg3; |
4add45b4 FB |
4277 | |
4278 | dirp = malloc(count); | |
0da46a6e | 4279 | if (!dirp) { |
579a97f7 | 4280 | ret = -TARGET_ENOMEM; |
0da46a6e TS |
4281 | goto fail; |
4282 | } | |
3b46e624 | 4283 | |
4add45b4 FB |
4284 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
4285 | if (!is_error(ret)) { | |
4286 | struct dirent *de; | |
4287 | struct target_dirent *tde; | |
4288 | int len = ret; | |
4289 | int reclen, treclen; | |
4290 | int count1, tnamelen; | |
4291 | ||
4292 | count1 = 0; | |
4293 | de = dirp; | |
579a97f7 FB |
4294 | if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
4295 | goto efault; | |
4add45b4 FB |
4296 | tde = target_dirp; |
4297 | while (len > 0) { | |
4298 | reclen = de->d_reclen; | |
992f48a0 | 4299 | treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long))); |
4add45b4 FB |
4300 | tde->d_reclen = tswap16(treclen); |
4301 | tde->d_ino = tswapl(de->d_ino); | |
4302 | tde->d_off = tswapl(de->d_off); | |
992f48a0 | 4303 | tnamelen = treclen - (2 * sizeof(abi_long) + 2); |
4add45b4 FB |
4304 | if (tnamelen > 256) |
4305 | tnamelen = 256; | |
80a9d035 | 4306 | /* XXX: may not be correct */ |
4add45b4 FB |
4307 | strncpy(tde->d_name, de->d_name, tnamelen); |
4308 | de = (struct dirent *)((char *)de + reclen); | |
4309 | len -= reclen; | |
1c5bf3bf | 4310 | tde = (struct target_dirent *)((char *)tde + treclen); |
4add45b4 FB |
4311 | count1 += treclen; |
4312 | } | |
4313 | ret = count1; | |
579a97f7 | 4314 | unlock_user(target_dirp, arg2, ret); |
4add45b4 FB |
4315 | } |
4316 | free(dirp); | |
4317 | } | |
4318 | #else | |
31e31b8a | 4319 | { |
53a5960a | 4320 | struct dirent *dirp; |
992f48a0 | 4321 | abi_long count = arg3; |
dab2ed99 | 4322 | |
579a97f7 FB |
4323 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
4324 | goto efault; | |
72f03900 | 4325 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
31e31b8a FB |
4326 | if (!is_error(ret)) { |
4327 | struct dirent *de; | |
4328 | int len = ret; | |
4329 | int reclen; | |
4330 | de = dirp; | |
4331 | while (len > 0) { | |
8083a3e5 | 4332 | reclen = de->d_reclen; |
31e31b8a FB |
4333 | if (reclen > len) |
4334 | break; | |
8083a3e5 | 4335 | de->d_reclen = tswap16(reclen); |
31e31b8a FB |
4336 | tswapls(&de->d_ino); |
4337 | tswapls(&de->d_off); | |
4338 | de = (struct dirent *)((char *)de + reclen); | |
4339 | len -= reclen; | |
4340 | } | |
4341 | } | |
53a5960a | 4342 | unlock_user(dirp, arg2, ret); |
31e31b8a | 4343 | } |
4add45b4 | 4344 | #endif |
31e31b8a | 4345 | break; |
3ae43202 | 4346 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
dab2ed99 FB |
4347 | case TARGET_NR_getdents64: |
4348 | { | |
53a5960a | 4349 | struct dirent64 *dirp; |
992f48a0 | 4350 | abi_long count = arg3; |
579a97f7 FB |
4351 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
4352 | goto efault; | |
dab2ed99 FB |
4353 | ret = get_errno(sys_getdents64(arg1, dirp, count)); |
4354 | if (!is_error(ret)) { | |
4355 | struct dirent64 *de; | |
4356 | int len = ret; | |
4357 | int reclen; | |
4358 | de = dirp; | |
4359 | while (len > 0) { | |
8083a3e5 | 4360 | reclen = de->d_reclen; |
dab2ed99 FB |
4361 | if (reclen > len) |
4362 | break; | |
8083a3e5 | 4363 | de->d_reclen = tswap16(reclen); |
dab2ed99 FB |
4364 | tswap64s(&de->d_ino); |
4365 | tswap64s(&de->d_off); | |
4366 | de = (struct dirent64 *)((char *)de + reclen); | |
4367 | len -= reclen; | |
4368 | } | |
4369 | } | |
53a5960a | 4370 | unlock_user(dirp, arg2, ret); |
dab2ed99 FB |
4371 | } |
4372 | break; | |
a541f297 | 4373 | #endif /* TARGET_NR_getdents64 */ |
e5febef5 | 4374 | #ifdef TARGET_NR__newselect |
31e31b8a | 4375 | case TARGET_NR__newselect: |
53a5960a | 4376 | ret = do_select(arg1, arg2, arg3, arg4, arg5); |
31e31b8a | 4377 | break; |
e5febef5 TS |
4378 | #endif |
4379 | #ifdef TARGET_NR_poll | |
9de5e440 FB |
4380 | case TARGET_NR_poll: |
4381 | { | |
53a5960a | 4382 | struct target_pollfd *target_pfd; |
9de5e440 FB |
4383 | unsigned int nfds = arg2; |
4384 | int timeout = arg3; | |
4385 | struct pollfd *pfd; | |
7854b056 | 4386 | unsigned int i; |
9de5e440 | 4387 | |
579a97f7 FB |
4388 | target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1); |
4389 | if (!target_pfd) | |
4390 | goto efault; | |
9de5e440 FB |
4391 | pfd = alloca(sizeof(struct pollfd) * nfds); |
4392 | for(i = 0; i < nfds; i++) { | |
5cd4393b FB |
4393 | pfd[i].fd = tswap32(target_pfd[i].fd); |
4394 | pfd[i].events = tswap16(target_pfd[i].events); | |
9de5e440 FB |
4395 | } |
4396 | ret = get_errno(poll(pfd, nfds, timeout)); | |
4397 | if (!is_error(ret)) { | |
4398 | for(i = 0; i < nfds; i++) { | |
5cd4393b | 4399 | target_pfd[i].revents = tswap16(pfd[i].revents); |
9de5e440 | 4400 | } |
53a5960a PB |
4401 | ret += nfds * (sizeof(struct target_pollfd) |
4402 | - sizeof(struct pollfd)); | |
9de5e440 | 4403 | } |
53a5960a | 4404 | unlock_user(target_pfd, arg1, ret); |
9de5e440 FB |
4405 | } |
4406 | break; | |
e5febef5 | 4407 | #endif |
31e31b8a | 4408 | case TARGET_NR_flock: |
9de5e440 FB |
4409 | /* NOTE: the flock constant seems to be the same for every |
4410 | Linux platform */ | |
4411 | ret = get_errno(flock(arg1, arg2)); | |
31e31b8a FB |
4412 | break; |
4413 | case TARGET_NR_readv: | |
4414 | { | |
4415 | int count = arg3; | |
31e31b8a | 4416 | struct iovec *vec; |
31e31b8a FB |
4417 | |
4418 | vec = alloca(count * sizeof(struct iovec)); | |
579a97f7 | 4419 | lock_iovec(VERIFY_WRITE, vec, arg2, count, 0); |
31e31b8a | 4420 | ret = get_errno(readv(arg1, vec, count)); |
53a5960a | 4421 | unlock_iovec(vec, arg2, count, 1); |
31e31b8a FB |
4422 | } |
4423 | break; | |
4424 | case TARGET_NR_writev: | |
4425 | { | |
4426 | int count = arg3; | |
31e31b8a | 4427 | struct iovec *vec; |
31e31b8a FB |
4428 | |
4429 | vec = alloca(count * sizeof(struct iovec)); | |
579a97f7 | 4430 | lock_iovec(VERIFY_READ, vec, arg2, count, 1); |
31e31b8a | 4431 | ret = get_errno(writev(arg1, vec, count)); |
53a5960a | 4432 | unlock_iovec(vec, arg2, count, 0); |
31e31b8a FB |
4433 | } |
4434 | break; | |
4435 | case TARGET_NR_getsid: | |
4436 | ret = get_errno(getsid(arg1)); | |
4437 | break; | |
7a3148a9 | 4438 | #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */ |
31e31b8a | 4439 | case TARGET_NR_fdatasync: |
5cd4393b FB |
4440 | ret = get_errno(fdatasync(arg1)); |
4441 | break; | |
7a3148a9 | 4442 | #endif |
31e31b8a | 4443 | case TARGET_NR__sysctl: |
0da46a6e | 4444 | /* We don't implement this, but ENOTDIR is always a safe |
29e619b1 | 4445 | return value. */ |
0da46a6e TS |
4446 | ret = -TARGET_ENOTDIR; |
4447 | break; | |
31e31b8a | 4448 | case TARGET_NR_sched_setparam: |
5cd4393b | 4449 | { |
53a5960a | 4450 | struct sched_param *target_schp; |
5cd4393b | 4451 | struct sched_param schp; |
53a5960a | 4452 | |
579a97f7 FB |
4453 | if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) |
4454 | goto efault; | |
5cd4393b | 4455 | schp.sched_priority = tswap32(target_schp->sched_priority); |
53a5960a | 4456 | unlock_user_struct(target_schp, arg2, 0); |
5cd4393b FB |
4457 | ret = get_errno(sched_setparam(arg1, &schp)); |
4458 | } | |
4459 | break; | |
31e31b8a | 4460 | case TARGET_NR_sched_getparam: |
5cd4393b | 4461 | { |
53a5960a | 4462 | struct sched_param *target_schp; |
5cd4393b FB |
4463 | struct sched_param schp; |
4464 | ret = get_errno(sched_getparam(arg1, &schp)); | |
4465 | if (!is_error(ret)) { | |
579a97f7 FB |
4466 | if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) |
4467 | goto efault; | |
5cd4393b | 4468 | target_schp->sched_priority = tswap32(schp.sched_priority); |
53a5960a | 4469 | unlock_user_struct(target_schp, arg2, 1); |
5cd4393b FB |
4470 | } |
4471 | } | |
4472 | break; | |
31e31b8a | 4473 | case TARGET_NR_sched_setscheduler: |
5cd4393b | 4474 | { |
53a5960a | 4475 | struct sched_param *target_schp; |
5cd4393b | 4476 | struct sched_param schp; |
579a97f7 FB |
4477 | if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) |
4478 | goto efault; | |
5cd4393b | 4479 | schp.sched_priority = tswap32(target_schp->sched_priority); |
53a5960a | 4480 | unlock_user_struct(target_schp, arg3, 0); |
5cd4393b FB |
4481 | ret = get_errno(sched_setscheduler(arg1, arg2, &schp)); |
4482 | } | |
4483 | break; | |
31e31b8a | 4484 | case TARGET_NR_sched_getscheduler: |
5cd4393b FB |
4485 | ret = get_errno(sched_getscheduler(arg1)); |
4486 | break; | |
31e31b8a FB |
4487 | case TARGET_NR_sched_yield: |
4488 | ret = get_errno(sched_yield()); | |
4489 | break; | |
4490 | case TARGET_NR_sched_get_priority_max: | |
5cd4393b FB |
4491 | ret = get_errno(sched_get_priority_max(arg1)); |
4492 | break; | |
31e31b8a | 4493 | case TARGET_NR_sched_get_priority_min: |
5cd4393b FB |
4494 | ret = get_errno(sched_get_priority_min(arg1)); |
4495 | break; | |
31e31b8a | 4496 | case TARGET_NR_sched_rr_get_interval: |
5cd4393b | 4497 | { |
5cd4393b FB |
4498 | struct timespec ts; |
4499 | ret = get_errno(sched_rr_get_interval(arg1, &ts)); | |
4500 | if (!is_error(ret)) { | |
53a5960a | 4501 | host_to_target_timespec(arg2, &ts); |
5cd4393b FB |
4502 | } |
4503 | } | |
4504 | break; | |
31e31b8a | 4505 | case TARGET_NR_nanosleep: |
1b6b029e | 4506 | { |
1b6b029e | 4507 | struct timespec req, rem; |
53a5960a | 4508 | target_to_host_timespec(&req, arg1); |
1b6b029e | 4509 | ret = get_errno(nanosleep(&req, &rem)); |
53a5960a PB |
4510 | if (is_error(ret) && arg2) { |
4511 | host_to_target_timespec(arg2, &rem); | |
1b6b029e FB |
4512 | } |
4513 | } | |
4514 | break; | |
e5febef5 | 4515 | #ifdef TARGET_NR_query_module |
31e31b8a | 4516 | case TARGET_NR_query_module: |
5cd4393b | 4517 | goto unimplemented; |
e5febef5 TS |
4518 | #endif |
4519 | #ifdef TARGET_NR_nfsservctl | |
31e31b8a | 4520 | case TARGET_NR_nfsservctl: |
5cd4393b | 4521 | goto unimplemented; |
e5febef5 | 4522 | #endif |
31e31b8a | 4523 | case TARGET_NR_prctl: |
e5574487 TS |
4524 | switch (arg1) |
4525 | { | |
4526 | case PR_GET_PDEATHSIG: | |
4527 | { | |
4528 | int deathsig; | |
4529 | ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5)); | |
4530 | if (!is_error(ret) && arg2) | |
4531 | tput32(arg2, deathsig); | |
4532 | } | |
4533 | break; | |
4534 | default: | |
4535 | ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5)); | |
4536 | break; | |
4537 | } | |
39b9aae1 | 4538 | break; |
67867308 | 4539 | #ifdef TARGET_NR_pread |
31e31b8a | 4540 | case TARGET_NR_pread: |
53a5960a | 4541 | page_unprotect_range(arg2, arg3); |
579a97f7 FB |
4542 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
4543 | goto efault; | |
53a5960a PB |
4544 | ret = get_errno(pread(arg1, p, arg3, arg4)); |
4545 | unlock_user(p, arg2, ret); | |
206f0fa7 | 4546 | break; |
31e31b8a | 4547 | case TARGET_NR_pwrite: |
579a97f7 FB |
4548 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
4549 | goto efault; | |
53a5960a PB |
4550 | ret = get_errno(pwrite(arg1, p, arg3, arg4)); |
4551 | unlock_user(p, arg2, 0); | |
206f0fa7 | 4552 | break; |
67867308 | 4553 | #endif |
31e31b8a | 4554 | case TARGET_NR_getcwd: |
579a97f7 FB |
4555 | if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0))) |
4556 | goto efault; | |
53a5960a PB |
4557 | ret = get_errno(sys_getcwd1(p, arg2)); |
4558 | unlock_user(p, arg1, ret); | |
31e31b8a FB |
4559 | break; |
4560 | case TARGET_NR_capget: | |
5cd4393b | 4561 | goto unimplemented; |
31e31b8a | 4562 | case TARGET_NR_capset: |
5cd4393b | 4563 | goto unimplemented; |
31e31b8a | 4564 | case TARGET_NR_sigaltstack: |
198a74de TS |
4565 | #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \ |
4566 | defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) | |
579a97f7 | 4567 | ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env)); |
a04e134a TS |
4568 | break; |
4569 | #else | |
5cd4393b | 4570 | goto unimplemented; |
a04e134a | 4571 | #endif |
31e31b8a | 4572 | case TARGET_NR_sendfile: |
5cd4393b | 4573 | goto unimplemented; |
ebc05488 | 4574 | #ifdef TARGET_NR_getpmsg |
31e31b8a | 4575 | case TARGET_NR_getpmsg: |
5cd4393b | 4576 | goto unimplemented; |
ebc05488 FB |
4577 | #endif |
4578 | #ifdef TARGET_NR_putpmsg | |
31e31b8a | 4579 | case TARGET_NR_putpmsg: |
5cd4393b | 4580 | goto unimplemented; |
ebc05488 | 4581 | #endif |
048f6b4d | 4582 | #ifdef TARGET_NR_vfork |
31e31b8a | 4583 | case TARGET_NR_vfork: |
1b6b029e | 4584 | ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0)); |
31e31b8a | 4585 | break; |
048f6b4d | 4586 | #endif |
ebc05488 | 4587 | #ifdef TARGET_NR_ugetrlimit |
31e31b8a | 4588 | case TARGET_NR_ugetrlimit: |
728584be FB |
4589 | { |
4590 | struct rlimit rlim; | |
4591 | ret = get_errno(getrlimit(arg1, &rlim)); | |
4592 | if (!is_error(ret)) { | |
53a5960a | 4593 | struct target_rlimit *target_rlim; |
579a97f7 FB |
4594 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
4595 | goto efault; | |
728584be FB |
4596 | target_rlim->rlim_cur = tswapl(rlim.rlim_cur); |
4597 | target_rlim->rlim_max = tswapl(rlim.rlim_max); | |
53a5960a | 4598 | unlock_user_struct(target_rlim, arg2, 1); |
728584be FB |
4599 | } |
4600 | break; | |
4601 | } | |
ebc05488 | 4602 | #endif |
a315a145 | 4603 | #ifdef TARGET_NR_truncate64 |
31e31b8a | 4604 | case TARGET_NR_truncate64: |
579a97f7 FB |
4605 | if (!(p = lock_user_string(arg1))) |
4606 | goto efault; | |
53a5960a PB |
4607 | ret = target_truncate64(cpu_env, p, arg2, arg3, arg4); |
4608 | unlock_user(p, arg1, 0); | |
667f38b1 | 4609 | break; |
a315a145 FB |
4610 | #endif |
4611 | #ifdef TARGET_NR_ftruncate64 | |
31e31b8a | 4612 | case TARGET_NR_ftruncate64: |
ce4defa0 | 4613 | ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4); |
667f38b1 | 4614 | break; |
a315a145 FB |
4615 | #endif |
4616 | #ifdef TARGET_NR_stat64 | |
31e31b8a | 4617 | case TARGET_NR_stat64: |
579a97f7 FB |
4618 | if (!(p = lock_user_string(arg1))) |
4619 | goto efault; | |
53a5960a PB |
4620 | ret = get_errno(stat(path(p), &st)); |
4621 | unlock_user(p, arg1, 0); | |
60cd49d5 | 4622 | goto do_stat64; |
a315a145 FB |
4623 | #endif |
4624 | #ifdef TARGET_NR_lstat64 | |
31e31b8a | 4625 | case TARGET_NR_lstat64: |
579a97f7 FB |
4626 | if (!(p = lock_user_string(arg1))) |
4627 | goto efault; | |
53a5960a PB |
4628 | ret = get_errno(lstat(path(p), &st)); |
4629 | unlock_user(p, arg1, 0); | |
60cd49d5 | 4630 | goto do_stat64; |
a315a145 FB |
4631 | #endif |
4632 | #ifdef TARGET_NR_fstat64 | |
31e31b8a | 4633 | case TARGET_NR_fstat64: |
60cd49d5 FB |
4634 | { |
4635 | ret = get_errno(fstat(arg1, &st)); | |
4636 | do_stat64: | |
4637 | if (!is_error(ret)) { | |
ce4defa0 PB |
4638 | #ifdef TARGET_ARM |
4639 | if (((CPUARMState *)cpu_env)->eabi) { | |
53a5960a | 4640 | struct target_eabi_stat64 *target_st; |
579a97f7 FB |
4641 | |
4642 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) | |
4643 | goto efault; | |
ce4defa0 | 4644 | memset(target_st, 0, sizeof(struct target_eabi_stat64)); |
579a97f7 FB |
4645 | __put_user(st.st_dev, &target_st->st_dev); |
4646 | __put_user(st.st_ino, &target_st->st_ino); | |
ce4defa0 | 4647 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
579a97f7 FB |
4648 | __put_user(st.st_ino, &target_st->__st_ino); |
4649 | #endif | |
4650 | __put_user(st.st_mode, &target_st->st_mode); | |
4651 | __put_user(st.st_nlink, &target_st->st_nlink); | |
4652 | __put_user(st.st_uid, &target_st->st_uid); | |
4653 | __put_user(st.st_gid, &target_st->st_gid); | |
4654 | __put_user(st.st_rdev, &target_st->st_rdev); | |
4655 | __put_user(st.st_size, &target_st->st_size); | |
4656 | __put_user(st.st_blksize, &target_st->st_blksize); | |
4657 | __put_user(st.st_blocks, &target_st->st_blocks); | |
4658 | __put_user(st.st_atime, &target_st->target_st_atime); | |
4659 | __put_user(st.st_mtime, &target_st->target_st_mtime); | |
4660 | __put_user(st.st_ctime, &target_st->target_st_ctime); | |
4661 | unlock_user_struct(target_st, arg2, 1); | |
ce4defa0 PB |
4662 | } else |
4663 | #endif | |
4664 | { | |
53a5960a | 4665 | struct target_stat64 *target_st; |
579a97f7 FB |
4666 | |
4667 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) | |
4668 | goto efault; | |
ce4defa0 | 4669 | memset(target_st, 0, sizeof(struct target_stat64)); |
579a97f7 FB |
4670 | __put_user(st.st_dev, &target_st->st_dev); |
4671 | __put_user(st.st_ino, &target_st->st_ino); | |
ec86b0fb | 4672 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
579a97f7 | 4673 | __put_user(st.st_ino, &target_st->__st_ino); |
ce4defa0 | 4674 | #endif |
579a97f7 FB |
4675 | __put_user(st.st_mode, &target_st->st_mode); |
4676 | __put_user(st.st_nlink, &target_st->st_nlink); | |
4677 | __put_user(st.st_uid, &target_st->st_uid); | |
4678 | __put_user(st.st_gid, &target_st->st_gid); | |
4679 | __put_user(st.st_rdev, &target_st->st_rdev); | |
ce4defa0 | 4680 | /* XXX: better use of kernel struct */ |
579a97f7 FB |
4681 | __put_user(st.st_size, &target_st->st_size); |
4682 | __put_user(st.st_blksize, &target_st->st_blksize); | |
4683 | __put_user(st.st_blocks, &target_st->st_blocks); | |
4684 | __put_user(st.st_atime, &target_st->target_st_atime); | |
4685 | __put_user(st.st_mtime, &target_st->target_st_mtime); | |
4686 | __put_user(st.st_ctime, &target_st->target_st_ctime); | |
4687 | unlock_user_struct(target_st, arg2, 1); | |
ce4defa0 | 4688 | } |
60cd49d5 FB |
4689 | } |
4690 | } | |
4691 | break; | |
a315a145 | 4692 | #endif |
67867308 FB |
4693 | #ifdef USE_UID16 |
4694 | case TARGET_NR_lchown: | |
579a97f7 FB |
4695 | if (!(p = lock_user_string(arg1))) |
4696 | goto efault; | |
53a5960a PB |
4697 | ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3))); |
4698 | unlock_user(p, arg1, 0); | |
67867308 FB |
4699 | break; |
4700 | case TARGET_NR_getuid: | |
4701 | ret = get_errno(high2lowuid(getuid())); | |
4702 | break; | |
4703 | case TARGET_NR_getgid: | |
4704 | ret = get_errno(high2lowgid(getgid())); | |
4705 | break; | |
4706 | case TARGET_NR_geteuid: | |
4707 | ret = get_errno(high2lowuid(geteuid())); | |
4708 | break; | |
4709 | case TARGET_NR_getegid: | |
4710 | ret = get_errno(high2lowgid(getegid())); | |
4711 | break; | |
4712 | case TARGET_NR_setreuid: | |
4713 | ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2))); | |
4714 | break; | |
4715 | case TARGET_NR_setregid: | |
4716 | ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2))); | |
4717 | break; | |
4718 | case TARGET_NR_getgroups: | |
4719 | { | |
4720 | int gidsetsize = arg1; | |
53a5960a | 4721 | uint16_t *target_grouplist; |
67867308 FB |
4722 | gid_t *grouplist; |
4723 | int i; | |
4724 | ||
4725 | grouplist = alloca(gidsetsize * sizeof(gid_t)); | |
4726 | ret = get_errno(getgroups(gidsetsize, grouplist)); | |
4727 | if (!is_error(ret)) { | |
579a97f7 FB |
4728 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0); |
4729 | if (!target_grouplist) | |
4730 | goto efault; | |
67867308 FB |
4731 | for(i = 0;i < gidsetsize; i++) |
4732 | target_grouplist[i] = tswap16(grouplist[i]); | |
53a5960a | 4733 | unlock_user(target_grouplist, arg2, gidsetsize * 2); |
67867308 FB |
4734 | } |
4735 | } | |
4736 | break; | |
4737 | case TARGET_NR_setgroups: | |
4738 | { | |
4739 | int gidsetsize = arg1; | |
53a5960a | 4740 | uint16_t *target_grouplist; |
67867308 FB |
4741 | gid_t *grouplist; |
4742 | int i; | |
4743 | ||
4744 | grouplist = alloca(gidsetsize * sizeof(gid_t)); | |
579a97f7 FB |
4745 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1); |
4746 | if (!target_grouplist) { | |
4747 | ret = -TARGET_EFAULT; | |
4748 | goto fail; | |
4749 | } | |
67867308 FB |
4750 | for(i = 0;i < gidsetsize; i++) |
4751 | grouplist[i] = tswap16(target_grouplist[i]); | |
53a5960a | 4752 | unlock_user(target_grouplist, arg2, 0); |
67867308 FB |
4753 | ret = get_errno(setgroups(gidsetsize, grouplist)); |
4754 | } | |
4755 | break; | |
4756 | case TARGET_NR_fchown: | |
4757 | ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); | |
4758 | break; | |
ccfa72b7 TS |
4759 | #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) |
4760 | case TARGET_NR_fchownat: | |
579a97f7 FB |
4761 | if (!(p = lock_user_string(arg2))) |
4762 | goto efault; | |
4763 | ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5)); | |
4764 | unlock_user(p, arg2, 0); | |
ccfa72b7 TS |
4765 | break; |
4766 | #endif | |
67867308 FB |
4767 | #ifdef TARGET_NR_setresuid |
4768 | case TARGET_NR_setresuid: | |
5fafdf24 TS |
4769 | ret = get_errno(setresuid(low2highuid(arg1), |
4770 | low2highuid(arg2), | |
67867308 FB |
4771 | low2highuid(arg3))); |
4772 | break; | |
4773 | #endif | |
4774 | #ifdef TARGET_NR_getresuid | |
4775 | case TARGET_NR_getresuid: | |
4776 | { | |
53a5960a | 4777 | uid_t ruid, euid, suid; |
67867308 FB |
4778 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
4779 | if (!is_error(ret)) { | |
53a5960a PB |
4780 | tput16(arg1, tswap16(high2lowuid(ruid))); |
4781 | tput16(arg2, tswap16(high2lowuid(euid))); | |
4782 | tput16(arg3, tswap16(high2lowuid(suid))); | |
67867308 FB |
4783 | } |
4784 | } | |
4785 | break; | |
4786 | #endif | |
4787 | #ifdef TARGET_NR_getresgid | |
4788 | case TARGET_NR_setresgid: | |
5fafdf24 TS |
4789 | ret = get_errno(setresgid(low2highgid(arg1), |
4790 | low2highgid(arg2), | |
67867308 FB |
4791 | low2highgid(arg3))); |
4792 | break; | |
4793 | #endif | |
4794 | #ifdef TARGET_NR_getresgid | |
4795 | case TARGET_NR_getresgid: | |
4796 | { | |
53a5960a | 4797 | gid_t rgid, egid, sgid; |
67867308 FB |
4798 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
4799 | if (!is_error(ret)) { | |
53a5960a PB |
4800 | tput16(arg1, tswap16(high2lowgid(rgid))); |
4801 | tput16(arg2, tswap16(high2lowgid(egid))); | |
4802 | tput16(arg3, tswap16(high2lowgid(sgid))); | |
67867308 FB |
4803 | } |
4804 | } | |
4805 | break; | |
4806 | #endif | |
4807 | case TARGET_NR_chown: | |
579a97f7 FB |
4808 | if (!(p = lock_user_string(arg1))) |
4809 | goto efault; | |
53a5960a PB |
4810 | ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3))); |
4811 | unlock_user(p, arg1, 0); | |
67867308 FB |
4812 | break; |
4813 | case TARGET_NR_setuid: | |
4814 | ret = get_errno(setuid(low2highuid(arg1))); | |
4815 | break; | |
4816 | case TARGET_NR_setgid: | |
4817 | ret = get_errno(setgid(low2highgid(arg1))); | |
4818 | break; | |
4819 | case TARGET_NR_setfsuid: | |
4820 | ret = get_errno(setfsuid(arg1)); | |
4821 | break; | |
4822 | case TARGET_NR_setfsgid: | |
4823 | ret = get_errno(setfsgid(arg1)); | |
4824 | break; | |
4825 | #endif /* USE_UID16 */ | |
4826 | ||
a315a145 | 4827 | #ifdef TARGET_NR_lchown32 |
31e31b8a | 4828 | case TARGET_NR_lchown32: |
579a97f7 FB |
4829 | if (!(p = lock_user_string(arg1))) |
4830 | goto efault; | |
53a5960a PB |
4831 | ret = get_errno(lchown(p, arg2, arg3)); |
4832 | unlock_user(p, arg1, 0); | |
b03c60f3 | 4833 | break; |
a315a145 FB |
4834 | #endif |
4835 | #ifdef TARGET_NR_getuid32 | |
31e31b8a | 4836 | case TARGET_NR_getuid32: |
b03c60f3 FB |
4837 | ret = get_errno(getuid()); |
4838 | break; | |
a315a145 FB |
4839 | #endif |
4840 | #ifdef TARGET_NR_getgid32 | |
31e31b8a | 4841 | case TARGET_NR_getgid32: |
b03c60f3 FB |
4842 | ret = get_errno(getgid()); |
4843 | break; | |
a315a145 FB |
4844 | #endif |
4845 | #ifdef TARGET_NR_geteuid32 | |
31e31b8a | 4846 | case TARGET_NR_geteuid32: |
b03c60f3 FB |
4847 | ret = get_errno(geteuid()); |
4848 | break; | |
a315a145 FB |
4849 | #endif |
4850 | #ifdef TARGET_NR_getegid32 | |
31e31b8a | 4851 | case TARGET_NR_getegid32: |
b03c60f3 FB |
4852 | ret = get_errno(getegid()); |
4853 | break; | |
a315a145 FB |
4854 | #endif |
4855 | #ifdef TARGET_NR_setreuid32 | |
31e31b8a | 4856 | case TARGET_NR_setreuid32: |
b03c60f3 FB |
4857 | ret = get_errno(setreuid(arg1, arg2)); |
4858 | break; | |
a315a145 FB |
4859 | #endif |
4860 | #ifdef TARGET_NR_setregid32 | |
31e31b8a | 4861 | case TARGET_NR_setregid32: |
b03c60f3 FB |
4862 | ret = get_errno(setregid(arg1, arg2)); |
4863 | break; | |
a315a145 FB |
4864 | #endif |
4865 | #ifdef TARGET_NR_getgroups32 | |
31e31b8a | 4866 | case TARGET_NR_getgroups32: |
99c475ab FB |
4867 | { |
4868 | int gidsetsize = arg1; | |
53a5960a | 4869 | uint32_t *target_grouplist; |
99c475ab FB |
4870 | gid_t *grouplist; |
4871 | int i; | |
4872 | ||
4873 | grouplist = alloca(gidsetsize * sizeof(gid_t)); | |
4874 | ret = get_errno(getgroups(gidsetsize, grouplist)); | |
4875 | if (!is_error(ret)) { | |
579a97f7 FB |
4876 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0); |
4877 | if (!target_grouplist) { | |
4878 | ret = -TARGET_EFAULT; | |
4879 | goto fail; | |
4880 | } | |
99c475ab | 4881 | for(i = 0;i < gidsetsize; i++) |
53a5960a PB |
4882 | target_grouplist[i] = tswap32(grouplist[i]); |
4883 | unlock_user(target_grouplist, arg2, gidsetsize * 4); | |
99c475ab FB |
4884 | } |
4885 | } | |
4886 | break; | |
a315a145 FB |
4887 | #endif |
4888 | #ifdef TARGET_NR_setgroups32 | |
31e31b8a | 4889 | case TARGET_NR_setgroups32: |
99c475ab FB |
4890 | { |
4891 | int gidsetsize = arg1; | |
53a5960a | 4892 | uint32_t *target_grouplist; |
99c475ab FB |
4893 | gid_t *grouplist; |
4894 | int i; | |
3b46e624 | 4895 | |
99c475ab | 4896 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
579a97f7 FB |
4897 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1); |
4898 | if (!target_grouplist) { | |
4899 | ret = -TARGET_EFAULT; | |
4900 | goto fail; | |
4901 | } | |
99c475ab | 4902 | for(i = 0;i < gidsetsize; i++) |
53a5960a PB |
4903 | grouplist[i] = tswap32(target_grouplist[i]); |
4904 | unlock_user(target_grouplist, arg2, 0); | |
99c475ab FB |
4905 | ret = get_errno(setgroups(gidsetsize, grouplist)); |
4906 | } | |
4907 | break; | |
a315a145 FB |
4908 | #endif |
4909 | #ifdef TARGET_NR_fchown32 | |
31e31b8a | 4910 | case TARGET_NR_fchown32: |
b03c60f3 FB |
4911 | ret = get_errno(fchown(arg1, arg2, arg3)); |
4912 | break; | |
a315a145 FB |
4913 | #endif |
4914 | #ifdef TARGET_NR_setresuid32 | |
31e31b8a | 4915 | case TARGET_NR_setresuid32: |
b03c60f3 FB |
4916 | ret = get_errno(setresuid(arg1, arg2, arg3)); |
4917 | break; | |
a315a145 FB |
4918 | #endif |
4919 | #ifdef TARGET_NR_getresuid32 | |
31e31b8a | 4920 | case TARGET_NR_getresuid32: |
b03c60f3 | 4921 | { |
53a5960a | 4922 | uid_t ruid, euid, suid; |
b03c60f3 FB |
4923 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
4924 | if (!is_error(ret)) { | |
53a5960a PB |
4925 | tput32(arg1, tswap32(ruid)); |
4926 | tput32(arg2, tswap32(euid)); | |
4927 | tput32(arg3, tswap32(suid)); | |
b03c60f3 FB |
4928 | } |
4929 | } | |
4930 | break; | |
a315a145 FB |
4931 | #endif |
4932 | #ifdef TARGET_NR_setresgid32 | |
31e31b8a | 4933 | case TARGET_NR_setresgid32: |
b03c60f3 FB |
4934 | ret = get_errno(setresgid(arg1, arg2, arg3)); |
4935 | break; | |
a315a145 FB |
4936 | #endif |
4937 | #ifdef TARGET_NR_getresgid32 | |
31e31b8a | 4938 | case TARGET_NR_getresgid32: |
b03c60f3 | 4939 | { |
53a5960a | 4940 | gid_t rgid, egid, sgid; |
b03c60f3 FB |
4941 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
4942 | if (!is_error(ret)) { | |
53a5960a PB |
4943 | tput32(arg1, tswap32(rgid)); |
4944 | tput32(arg2, tswap32(egid)); | |
4945 | tput32(arg3, tswap32(sgid)); | |
b03c60f3 FB |
4946 | } |
4947 | } | |
4948 | break; | |
a315a145 FB |
4949 | #endif |
4950 | #ifdef TARGET_NR_chown32 | |
31e31b8a | 4951 | case TARGET_NR_chown32: |
579a97f7 FB |
4952 | if (!(p = lock_user_string(arg1))) |
4953 | goto efault; | |
53a5960a PB |
4954 | ret = get_errno(chown(p, arg2, arg3)); |
4955 | unlock_user(p, arg1, 0); | |
b03c60f3 | 4956 | break; |
a315a145 FB |
4957 | #endif |
4958 | #ifdef TARGET_NR_setuid32 | |
31e31b8a | 4959 | case TARGET_NR_setuid32: |
b03c60f3 FB |
4960 | ret = get_errno(setuid(arg1)); |
4961 | break; | |
a315a145 FB |
4962 | #endif |
4963 | #ifdef TARGET_NR_setgid32 | |
31e31b8a | 4964 | case TARGET_NR_setgid32: |
b03c60f3 FB |
4965 | ret = get_errno(setgid(arg1)); |
4966 | break; | |
a315a145 FB |
4967 | #endif |
4968 | #ifdef TARGET_NR_setfsuid32 | |
31e31b8a | 4969 | case TARGET_NR_setfsuid32: |
b03c60f3 FB |
4970 | ret = get_errno(setfsuid(arg1)); |
4971 | break; | |
a315a145 FB |
4972 | #endif |
4973 | #ifdef TARGET_NR_setfsgid32 | |
31e31b8a | 4974 | case TARGET_NR_setfsgid32: |
b03c60f3 FB |
4975 | ret = get_errno(setfsgid(arg1)); |
4976 | break; | |
a315a145 | 4977 | #endif |
67867308 | 4978 | |
31e31b8a | 4979 | case TARGET_NR_pivot_root: |
b03c60f3 | 4980 | goto unimplemented; |
ffa65c3b | 4981 | #ifdef TARGET_NR_mincore |
31e31b8a | 4982 | case TARGET_NR_mincore: |
b03c60f3 | 4983 | goto unimplemented; |
ffa65c3b FB |
4984 | #endif |
4985 | #ifdef TARGET_NR_madvise | |
31e31b8a | 4986 | case TARGET_NR_madvise: |
24836689 PB |
4987 | /* A straight passthrough may not be safe because qemu sometimes |
4988 | turns private flie-backed mappings into anonymous mappings. | |
4989 | This will break MADV_DONTNEED. | |
4990 | This is a hint, so ignoring and returning success is ok. */ | |
4991 | ret = get_errno(0); | |
4992 | break; | |
ffa65c3b | 4993 | #endif |
992f48a0 | 4994 | #if TARGET_ABI_BITS == 32 |
31e31b8a | 4995 | case TARGET_NR_fcntl64: |
77e4672d | 4996 | { |
b1e341eb | 4997 | int cmd; |
77e4672d | 4998 | struct flock64 fl; |
53a5960a | 4999 | struct target_flock64 *target_fl; |
ce4defa0 | 5000 | #ifdef TARGET_ARM |
53a5960a | 5001 | struct target_eabi_flock64 *target_efl; |
ce4defa0 | 5002 | #endif |
77e4672d | 5003 | |
b1e341eb TS |
5004 | switch(arg2){ |
5005 | case TARGET_F_GETLK64: | |
5006 | cmd = F_GETLK64; | |
a7222580 | 5007 | break; |
b1e341eb TS |
5008 | case TARGET_F_SETLK64: |
5009 | cmd = F_SETLK64; | |
a7222580 | 5010 | break; |
b1e341eb TS |
5011 | case TARGET_F_SETLKW64: |
5012 | cmd = F_SETLK64; | |
a7222580 | 5013 | break; |
b1e341eb TS |
5014 | default: |
5015 | cmd = arg2; | |
a7222580 | 5016 | break; |
b1e341eb TS |
5017 | } |
5018 | ||
60cd49d5 | 5019 | switch(arg2) { |
b1e341eb | 5020 | case TARGET_F_GETLK64: |
5813427b TS |
5021 | #ifdef TARGET_ARM |
5022 | if (((CPUARMState *)cpu_env)->eabi) { | |
9ee1fa2c FB |
5023 | if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) |
5024 | goto efault; | |
5813427b TS |
5025 | fl.l_type = tswap16(target_efl->l_type); |
5026 | fl.l_whence = tswap16(target_efl->l_whence); | |
5027 | fl.l_start = tswap64(target_efl->l_start); | |
5028 | fl.l_len = tswap64(target_efl->l_len); | |
5029 | fl.l_pid = tswapl(target_efl->l_pid); | |
5030 | unlock_user_struct(target_efl, arg3, 0); | |
5031 | } else | |
5032 | #endif | |
5033 | { | |
9ee1fa2c FB |
5034 | if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) |
5035 | goto efault; | |
5813427b TS |
5036 | fl.l_type = tswap16(target_fl->l_type); |
5037 | fl.l_whence = tswap16(target_fl->l_whence); | |
5038 | fl.l_start = tswap64(target_fl->l_start); | |
5039 | fl.l_len = tswap64(target_fl->l_len); | |
5040 | fl.l_pid = tswapl(target_fl->l_pid); | |
5041 | unlock_user_struct(target_fl, arg3, 0); | |
5042 | } | |
b1e341eb | 5043 | ret = get_errno(fcntl(arg1, cmd, &fl)); |
77e4672d | 5044 | if (ret == 0) { |
ce4defa0 PB |
5045 | #ifdef TARGET_ARM |
5046 | if (((CPUARMState *)cpu_env)->eabi) { | |
9ee1fa2c FB |
5047 | if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0)) |
5048 | goto efault; | |
ce4defa0 PB |
5049 | target_efl->l_type = tswap16(fl.l_type); |
5050 | target_efl->l_whence = tswap16(fl.l_whence); | |
5051 | target_efl->l_start = tswap64(fl.l_start); | |
5052 | target_efl->l_len = tswap64(fl.l_len); | |
5053 | target_efl->l_pid = tswapl(fl.l_pid); | |
53a5960a | 5054 | unlock_user_struct(target_efl, arg3, 1); |
ce4defa0 PB |
5055 | } else |
5056 | #endif | |
5057 | { | |
9ee1fa2c FB |
5058 | if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0)) |
5059 | goto efault; | |
ce4defa0 PB |
5060 | target_fl->l_type = tswap16(fl.l_type); |
5061 | target_fl->l_whence = tswap16(fl.l_whence); | |
5062 | target_fl->l_start = tswap64(fl.l_start); | |
5063 | target_fl->l_len = tswap64(fl.l_len); | |
5064 | target_fl->l_pid = tswapl(fl.l_pid); | |
53a5960a | 5065 | unlock_user_struct(target_fl, arg3, 1); |
ce4defa0 | 5066 | } |
77e4672d FB |
5067 | } |
5068 | break; | |
5069 | ||
b1e341eb TS |
5070 | case TARGET_F_SETLK64: |
5071 | case TARGET_F_SETLKW64: | |
ce4defa0 PB |
5072 | #ifdef TARGET_ARM |
5073 | if (((CPUARMState *)cpu_env)->eabi) { | |
9ee1fa2c FB |
5074 | if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) |
5075 | goto efault; | |
ce4defa0 PB |
5076 | fl.l_type = tswap16(target_efl->l_type); |
5077 | fl.l_whence = tswap16(target_efl->l_whence); | |
5078 | fl.l_start = tswap64(target_efl->l_start); | |
5079 | fl.l_len = tswap64(target_efl->l_len); | |
5080 | fl.l_pid = tswapl(target_efl->l_pid); | |
53a5960a | 5081 | unlock_user_struct(target_efl, arg3, 0); |
ce4defa0 PB |
5082 | } else |
5083 | #endif | |
5084 | { | |
9ee1fa2c FB |
5085 | if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) |
5086 | goto efault; | |
ce4defa0 PB |
5087 | fl.l_type = tswap16(target_fl->l_type); |
5088 | fl.l_whence = tswap16(target_fl->l_whence); | |
5089 | fl.l_start = tswap64(target_fl->l_start); | |
5090 | fl.l_len = tswap64(target_fl->l_len); | |
5091 | fl.l_pid = tswapl(target_fl->l_pid); | |
53a5960a | 5092 | unlock_user_struct(target_fl, arg3, 0); |
ce4defa0 | 5093 | } |
b1e341eb | 5094 | ret = get_errno(fcntl(arg1, cmd, &fl)); |
77e4672d | 5095 | break; |
60cd49d5 | 5096 | default: |
9ee1fa2c | 5097 | ret = do_fcntl(arg1, cmd, arg3); |
60cd49d5 FB |
5098 | break; |
5099 | } | |
77e4672d FB |
5100 | break; |
5101 | } | |
60cd49d5 | 5102 | #endif |
7d600c80 TS |
5103 | #ifdef TARGET_NR_cacheflush |
5104 | case TARGET_NR_cacheflush: | |
5105 | /* self-modifying code is handled automatically, so nothing needed */ | |
5106 | ret = 0; | |
5107 | break; | |
5108 | #endif | |
ebc05488 | 5109 | #ifdef TARGET_NR_security |
31e31b8a FB |
5110 | case TARGET_NR_security: |
5111 | goto unimplemented; | |
c573ff67 FB |
5112 | #endif |
5113 | #ifdef TARGET_NR_getpagesize | |
5114 | case TARGET_NR_getpagesize: | |
5115 | ret = TARGET_PAGE_SIZE; | |
5116 | break; | |
ebc05488 | 5117 | #endif |
31e31b8a FB |
5118 | case TARGET_NR_gettid: |
5119 | ret = get_errno(gettid()); | |
5120 | break; | |
e5febef5 | 5121 | #ifdef TARGET_NR_readahead |
31e31b8a | 5122 | case TARGET_NR_readahead: |
5cd4393b | 5123 | goto unimplemented; |
e5febef5 | 5124 | #endif |
ebc05488 | 5125 | #ifdef TARGET_NR_setxattr |
31e31b8a FB |
5126 | case TARGET_NR_setxattr: |
5127 | case TARGET_NR_lsetxattr: | |
5128 | case TARGET_NR_fsetxattr: | |
5129 | case TARGET_NR_getxattr: | |
5130 | case TARGET_NR_lgetxattr: | |
5131 | case TARGET_NR_fgetxattr: | |
5132 | case TARGET_NR_listxattr: | |
5133 | case TARGET_NR_llistxattr: | |
5134 | case TARGET_NR_flistxattr: | |
5135 | case TARGET_NR_removexattr: | |
5136 | case TARGET_NR_lremovexattr: | |
5137 | case TARGET_NR_fremovexattr: | |
5cd4393b | 5138 | goto unimplemented_nowarn; |
ebc05488 FB |
5139 | #endif |
5140 | #ifdef TARGET_NR_set_thread_area | |
5cd4393b | 5141 | case TARGET_NR_set_thread_area: |
6f5b89a0 TS |
5142 | #ifdef TARGET_MIPS |
5143 | ((CPUMIPSState *) cpu_env)->tls_value = arg1; | |
5144 | ret = 0; | |
5145 | break; | |
5146 | #else | |
5147 | goto unimplemented_nowarn; | |
5148 | #endif | |
5149 | #endif | |
5150 | #ifdef TARGET_NR_get_thread_area | |
5cd4393b FB |
5151 | case TARGET_NR_get_thread_area: |
5152 | goto unimplemented_nowarn; | |
48dc41eb FB |
5153 | #endif |
5154 | #ifdef TARGET_NR_getdomainname | |
5155 | case TARGET_NR_getdomainname: | |
5156 | goto unimplemented_nowarn; | |
ebc05488 | 5157 | #endif |
6f5b89a0 | 5158 | |
b5906f95 TS |
5159 | #ifdef TARGET_NR_clock_gettime |
5160 | case TARGET_NR_clock_gettime: | |
5161 | { | |
5162 | struct timespec ts; | |
5163 | ret = get_errno(clock_gettime(arg1, &ts)); | |
5164 | if (!is_error(ret)) { | |
5165 | host_to_target_timespec(arg2, &ts); | |
5166 | } | |
5167 | break; | |
5168 | } | |
5169 | #endif | |
5170 | #ifdef TARGET_NR_clock_getres | |
5171 | case TARGET_NR_clock_getres: | |
5172 | { | |
5173 | struct timespec ts; | |
5174 | ret = get_errno(clock_getres(arg1, &ts)); | |
5175 | if (!is_error(ret)) { | |
5176 | host_to_target_timespec(arg2, &ts); | |
5177 | } | |
5178 | break; | |
5179 | } | |
5180 | #endif | |
5181 | ||
6f5b89a0 TS |
5182 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
5183 | case TARGET_NR_set_tid_address: | |
579a97f7 FB |
5184 | ret = get_errno(set_tid_address((int *)g2h(arg1))); |
5185 | break; | |
6f5b89a0 TS |
5186 | #endif |
5187 | ||
3ae43202 | 5188 | #if defined(TARGET_NR_tkill) && defined(__NR_tkill) |
4cae1d16 TS |
5189 | case TARGET_NR_tkill: |
5190 | ret = get_errno(sys_tkill((int)arg1, (int)arg2)); | |
5191 | break; | |
5192 | #endif | |
5193 | ||
3ae43202 | 5194 | #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill) |
71455574 TS |
5195 | case TARGET_NR_tgkill: |
5196 | ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3)); | |
5197 | break; | |
5198 | #endif | |
5199 | ||
4f2b1fe8 TS |
5200 | #ifdef TARGET_NR_set_robust_list |
5201 | case TARGET_NR_set_robust_list: | |
5202 | goto unimplemented_nowarn; | |
5203 | #endif | |
5204 | ||
9007f0ef TS |
5205 | #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat) |
5206 | case TARGET_NR_utimensat: | |
5207 | { | |
5208 | struct timespec ts[2]; | |
5209 | target_to_host_timespec(ts, arg3); | |
5210 | target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec)); | |
5211 | if (!arg2) | |
5212 | ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4)); | |
5213 | else { | |
579a97f7 | 5214 | if (!(p = lock_user_string(arg2))) { |
0da46a6e | 5215 | ret = -TARGET_EFAULT; |
579a97f7 FB |
5216 | goto fail; |
5217 | } | |
5218 | ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4)); | |
5219 | unlock_user(p, arg2, 0); | |
9007f0ef TS |
5220 | } |
5221 | } | |
5222 | break; | |
5223 | #endif | |
5224 | ||
31e31b8a FB |
5225 | default: |
5226 | unimplemented: | |
5cd4393b | 5227 | gemu_log("qemu: Unsupported syscall: %d\n", num); |
4f2b1fe8 | 5228 | #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list) |
5cd4393b | 5229 | unimplemented_nowarn: |
80a9d035 | 5230 | #endif |
0da46a6e | 5231 | ret = -TARGET_ENOSYS; |
31e31b8a FB |
5232 | break; |
5233 | } | |
579a97f7 | 5234 | fail: |
c573ff67 FB |
5235 | #ifdef DEBUG |
5236 | gemu_log(" = %ld\n", ret); | |
5237 | #endif | |
b92c47c1 TS |
5238 | if(do_strace) |
5239 | print_syscall_ret(num, ret); | |
31e31b8a | 5240 | return ret; |
579a97f7 FB |
5241 | efault: |
5242 | ret = -TARGET_EFAULT; | |
5243 | goto fail; | |
31e31b8a | 5244 | } |