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