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