f17e5910126f13b6cd8c19e05c55db3dd5148e71
[qemu] / darwin-user / syscall.c
1 /*
2  *  Darwin syscalls
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *  Copyright (c) 2006 Pierre d'Herbemont
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include <mach/host_info.h>
27 #include <mach/mach.h>
28 #include <mach/mach_time.h>
29 #include <mach/message.h>
30
31 #include <pthread.h>
32 #include <dirent.h>
33
34 #include <sys/stat.h>
35 #include <sys/syscall.h>
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41 #include <sys/types.h>
42 #include <sys/dirent.h>
43 #include <sys/uio.h>
44 #include <sys/termios.h>
45 #include <sys/ptrace.h>
46 #include <net/if.h>
47
48 #include <sys/param.h>
49 #include <sys/mount.h>
50
51 #include <sys/attr.h>
52
53 #include <mach/ndr.h>
54 #include <mach/mig_errors.h>
55
56 #include <sys/xattr.h>
57
58 #include "qemu.h"
59
60 //#define DEBUG_SYSCALL
61
62 #ifdef DEBUG_SYSCALL
63 # define DEBUG_FORCE_ENABLE_LOCAL() int __DEBUG_qemu_user_force_enable = 1
64 # define DEBUG_BEGIN_ENABLE  __DEBUG_qemu_user_force_enable = 1;
65 # define DEBUG_END_ENABLE  __DEBUG_qemu_user_force_enable = 0;
66
67 # define DEBUG_DISABLE_ALL() static int __DEBUG_qemu_user_force_enable = 0
68 # define DEBUG_ENABLE_ALL()  static int __DEBUG_qemu_user_force_enable = 1
69     DEBUG_ENABLE_ALL();
70
71 # define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); \
72                            if(__DEBUG_qemu_user_force_enable) fprintf(stderr, __VA_ARGS__); \
73                          } while(0)
74 #else
75 # define DEBUG_FORCE_ENABLE_LOCAL()
76 # define DEBUG_BEGIN_ENABLE
77 # define DEBUG_END_ENABLE
78
79 # define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); } while(0)
80 #endif
81
82 enum {
83     bswap_out = 0,
84     bswap_in = 1
85 };
86
87 extern const char *interp_prefix;
88
89 static inline long get_errno(long ret)
90 {
91     if (ret == -1)
92         return -errno;
93     else
94         return ret;
95 }
96
97 static inline int is_error(long ret)
98 {
99     return (unsigned long)ret >= (unsigned long)(-4096);
100 }
101
102 /* ------------------------------------------------------------
103    Mach syscall handling
104 */
105
106 void static inline print_description_msg_header(mach_msg_header_t *hdr)
107 {
108     char *name = NULL;
109     int i;
110     struct { int number; char *name; } msg_name[] =
111     {
112         /* see http://fxr.watson.org/fxr/source/compat/mach/mach_namemap.c?v=NETBSD */
113         { 200,      "host_info" },
114         { 202,      "host_page_size" },
115         { 206,      "host_get_clock_service" },
116         { 206,      "host_get_clock_service" },
117         { 206,      "host_get_clock_service" },
118         { 306,      "host_get_clock_service" },
119         { 3204,     "mach_port_allocate" },
120         { 3206,     "mach_port_deallocate" },
121         { 3404,     "mach_ports_lookup" },
122         { 3409,     "mach_task_get_special_port" },
123         { 3414,     "mach_task_get_exception_ports" },
124         { 3418,     "mach_semaphore_create" },
125         { 3504,     "mach_semaphore_create" },
126         { 3509,     "mach_semaphore_create" },
127         { 3518,     "semaphore_create" },
128         { 3616,     "thread_policy" },
129         { 3801,     "vm_allocate" },
130         { 3802,     "vm_deallocate" },
131         { 3802,     "vm_deallocate" },
132         { 3803,     "vm_protect" },
133         { 3812,     "vm_map" },
134         { 4241776,  "lu_message_send_id" },  /* lookupd */
135         { 4241876,  "lu_message_reply_id" }, /* lookupd */
136     };
137
138     for(i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
139         if(msg_name[i].number == hdr->msgh_id)
140         {
141             name = msg_name[i].name;
142             break;
143         }
144     }
145     if(!name)
146         DPRINTF("unknown mach msg %d 0x%x\n", hdr->msgh_id, hdr->msgh_id);
147     else
148         DPRINTF("%s\n", name);
149 #if 0
150     DPRINTF("Bits: %8x\n", hdr->msgh_bits);
151     DPRINTF("Size: %8x\n", hdr->msgh_size);
152     DPRINTF("Rmte: %8x\n", hdr->msgh_remote_port);
153     DPRINTF("Locl: %8x\n", hdr->msgh_local_port);
154     DPRINTF("Rsrv: %8x\n", hdr->msgh_reserved);
155
156     DPRINTF("Id  : %8x\n", hdr->msgh_id);
157
158     NDR_record_t *ndr = (NDR_record_t *)(hdr + 1);
159     DPRINTF("hdr = %p, sizeof(hdr) = %x, NDR = %p\n", hdr, (unsigned int)sizeof(mach_msg_header_t), ndr);
160     DPRINTF("%d %d %d %d %d %d %d %d\n",
161            ndr->mig_vers, ndr->if_vers, ndr->reserved1, ndr->mig_encoding,
162            ndr->int_rep, ndr->char_rep, ndr->float_rep, ndr->reserved2);
163 #endif
164 }
165
166 static inline void print_mach_msg_return(mach_msg_return_t ret)
167 {
168     int i, found = 0;
169 #define MACH_MSG_RET(msg) { msg, #msg }
170     struct { int code; char *name; } msg_name[] =
171     {
172         /* ref: http://darwinsource.opendarwin.org/10.4.2/xnu-792.2.4/osfmk/man/mach_msg.html */
173         /* send message */
174         MACH_MSG_RET(MACH_SEND_MSG_TOO_SMALL),
175         MACH_MSG_RET(MACH_SEND_NO_BUFFER),
176         MACH_MSG_RET(MACH_SEND_INVALID_DATA),
177         MACH_MSG_RET(MACH_SEND_INVALID_HEADER),
178         MACH_MSG_RET(MACH_SEND_INVALID_DEST),
179         MACH_MSG_RET(MACH_SEND_INVALID_NOTIFY),
180         MACH_MSG_RET(MACH_SEND_INVALID_REPLY),
181         MACH_MSG_RET(MACH_SEND_INVALID_TRAILER),
182         MACH_MSG_RET(MACH_SEND_INVALID_MEMORY),
183         MACH_MSG_RET(MACH_SEND_INVALID_RIGHT),
184         MACH_MSG_RET(MACH_SEND_INVALID_TYPE),
185         MACH_MSG_RET(MACH_SEND_INTERRUPTED),
186         MACH_MSG_RET(MACH_SEND_TIMED_OUT),
187
188         MACH_MSG_RET(MACH_RCV_BODY_ERROR),
189         MACH_MSG_RET(MACH_RCV_HEADER_ERROR),
190
191         MACH_MSG_RET(MACH_RCV_IN_SET),
192         MACH_MSG_RET(MACH_RCV_INTERRUPTED),
193
194         MACH_MSG_RET(MACH_RCV_INVALID_DATA),
195         MACH_MSG_RET(MACH_RCV_INVALID_NAME),
196         MACH_MSG_RET(MACH_RCV_INVALID_NOTIFY),
197         MACH_MSG_RET(MACH_RCV_INVALID_TRAILER),
198         MACH_MSG_RET(MACH_RCV_INVALID_TYPE),
199
200         MACH_MSG_RET(MACH_RCV_PORT_CHANGED),
201         MACH_MSG_RET(MACH_RCV_PORT_DIED),
202
203         MACH_MSG_RET(MACH_RCV_SCATTER_SMALL),
204         MACH_MSG_RET(MACH_RCV_TIMED_OUT),
205         MACH_MSG_RET(MACH_RCV_TOO_LARGE)
206     };
207 #undef MACH_MSG_RET
208
209     if( ret == MACH_MSG_SUCCESS)
210         DPRINTF("MACH_MSG_SUCCESS\n");
211     else
212     {
213         for( i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
214             if(msg_name[i].code == ret) {
215                 DPRINTF("%s\n", msg_name[i].name);
216                 found = 1;
217                 break;
218             }
219         }
220         if(!found)
221             qerror("unknow mach message ret code %d\n", ret);
222     }
223 }
224
225 static inline void swap_mach_msg_header(mach_msg_header_t *hdr)
226 {
227     hdr->msgh_bits = tswap32(hdr->msgh_bits);
228     hdr->msgh_size = tswap32(hdr->msgh_size);
229     hdr->msgh_remote_port = tswap32(hdr->msgh_remote_port);
230     hdr->msgh_local_port = tswap32(hdr->msgh_local_port);
231     hdr->msgh_reserved = tswap32(hdr->msgh_reserved);
232     hdr->msgh_id = tswap32(hdr->msgh_id);
233 }
234
235 struct complex_msg {
236             mach_msg_header_t hdr;
237             mach_msg_body_t body;
238 };
239
240 static inline void swap_mach_msg_body(struct complex_msg *complex_msg, int bswap)
241 {
242     mach_msg_port_descriptor_t *descr = (mach_msg_port_descriptor_t *)(complex_msg+1);
243     int i,j;
244
245     if(bswap == bswap_in)
246         tswap32s(&complex_msg->body.msgh_descriptor_count);
247
248     DPRINTF("body.msgh_descriptor_count %d\n", complex_msg->body.msgh_descriptor_count);
249
250     for(i = 0; i < complex_msg->body.msgh_descriptor_count; i++) {
251         switch(descr->type)
252         {
253             case MACH_MSG_PORT_DESCRIPTOR:
254                 tswap32s(&descr->name);
255                 descr++;
256                 break;
257             case MACH_MSG_OOL_DESCRIPTOR:
258             {
259                 mach_msg_ool_descriptor_t *ool = (void *)descr;
260                 tswap32s((uint32_t *)&ool->address);
261                 tswap32s(&ool->size);
262
263                 descr = (mach_msg_port_descriptor_t *)(ool+1);
264                 break;
265             }
266             case MACH_MSG_OOL_PORTS_DESCRIPTOR:
267             {
268                 mach_msg_ool_ports_descriptor_t *ool_ports = (void *)descr;
269                 mach_port_name_t * port_names;
270
271                 if(bswap == bswap_in)
272                 {
273                     tswap32s((uint32_t *)&ool_ports->address);
274                     tswap32s(&ool_ports->count);
275                 }
276
277                 port_names = ool_ports->address;
278
279                 for(j = 0; j < ool_ports->count; j++)
280                     tswap32s(&port_names[j]);
281
282                 if(bswap == bswap_out)
283                 {
284                     tswap32s((uint32_t *)&ool_ports->address);
285                     tswap32s(&ool_ports->count);
286                 }
287
288                 descr = (mach_msg_port_descriptor_t *)(ool_ports+1);
289                 break;
290             }
291             default: qerror("unknow mach msg descriptor type %x\n", descr->type);
292         }
293     }
294     if(bswap == bswap_out)
295         tswap32s(&complex_msg->body.msgh_descriptor_count);
296 }
297
298 static inline void swap_mach_msg(mach_msg_header_t *hdr, int bswap)
299 {
300     if (bswap == bswap_out && hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
301         swap_mach_msg_body((struct complex_msg *)hdr, bswap);
302
303     swap_mach_msg_header(hdr);
304
305     if (bswap == bswap_in && hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
306         swap_mach_msg_body((struct complex_msg *)hdr, bswap);
307 }
308
309 static inline uint32_t target_mach_msg_trap(
310         mach_msg_header_t *hdr, uint32_t options, uint32_t send_size,
311         uint32_t rcv_size, uint32_t rcv_name, uint32_t time_out, uint32_t notify)
312 {
313     extern int mach_msg_trap(mach_msg_header_t *, mach_msg_option_t,
314           mach_msg_size_t, mach_msg_size_t, mach_port_t,
315           mach_msg_timeout_t, mach_port_t);
316     mach_msg_audit_trailer_t *trailer;
317     mach_msg_id_t msg_id;
318     uint32_t ret = 0;
319     int i;
320
321     swap_mach_msg(hdr, bswap_in);
322
323     msg_id = hdr->msgh_id;
324
325     print_description_msg_header(hdr);
326
327     ret = mach_msg_trap(hdr, options, send_size, rcv_size, rcv_name, time_out, notify);
328
329     print_mach_msg_return(ret);
330
331     if( (options & MACH_RCV_MSG) && (REQUESTED_TRAILER_SIZE(options) > 0) )
332     {
333         /* XXX: the kernel always return the full trailer with MACH_SEND_MSG, so we should
334                 probably always bswap it  */
335         /* warning: according to Mac OS X Internals (the book) msg_size might be expressed in
336                     natural_t units but according to xnu/osfmk/mach/message.h: "The size of
337                     the message must be specified in bytes" */
338         trailer = (mach_msg_audit_trailer_t *)((uint8_t *)hdr + hdr->msgh_size);
339         /* XXX: Should probably do that based on the option asked by the sender, but dealing
340         with kernel answer seems more sound */
341         switch(trailer->msgh_trailer_size)
342         {
343             case sizeof(mach_msg_audit_trailer_t):
344                 for(i = 0; i < 8; i++)
345                     tswap32s(&trailer->msgh_audit.val[i]);
346                 /* Fall in mach_msg_security_trailer_t case */
347             case sizeof(mach_msg_security_trailer_t):
348                 tswap32s(&trailer->msgh_sender.val[0]);
349                 tswap32s(&trailer->msgh_sender.val[1]);
350                 /* Fall in mach_msg_seqno_trailer_t case */
351             case sizeof(mach_msg_seqno_trailer_t):
352                 tswap32s(&trailer->msgh_seqno);
353                 /* Fall in mach_msg_trailer_t case */
354             case sizeof(mach_msg_trailer_t):
355                 tswap32s(&trailer->msgh_trailer_type);
356                 tswap32s(&trailer->msgh_trailer_size);
357                 break;
358             case 0:
359                 /* Safer not to byteswap, but probably wrong */
360                 break;
361             default:
362                 qerror("unknow trailer type given its size %d\n", trailer->msgh_trailer_size);
363                 break;
364         }
365     }
366
367     /* Special message handling */
368     switch (msg_id) {
369         case 200: /* host_info */
370         {
371             mig_reply_error_t *err = (mig_reply_error_t *)hdr;
372             struct {
373                 uint32_t unknow1;
374                 uint32_t max_cpus;
375                 uint32_t avail_cpus;
376                 uint32_t memory_size;
377                 uint32_t cpu_type;
378                 uint32_t cpu_subtype;
379             } *data = (void *)(err+1);
380
381             DPRINTF("maxcpu = 0x%x\n",   data->max_cpus);
382             DPRINTF("numcpu = 0x%x\n",   data->avail_cpus);
383             DPRINTF("memsize = 0x%x\n",  data->memory_size);
384
385 #if defined(TARGET_I386)
386             data->cpu_type = CPU_TYPE_I386;
387             DPRINTF("cpu_type changed to 0x%x(i386)\n", data->cpu_type);
388             data->cpu_subtype = CPU_SUBTYPE_PENT;
389             DPRINTF("cpu_subtype changed to 0x%x(i386_pent)\n", data->cpu_subtype);
390 #elif defined(TARGET_PPC)
391             data->cpu_type = CPU_TYPE_POWERPC;
392             DPRINTF("cpu_type changed to 0x%x(ppc)\n", data->cpu_type);
393             data->cpu_subtype = CPU_SUBTYPE_POWERPC_750;
394             DPRINTF("cpu_subtype changed to 0x%x(ppc_all)\n", data->cpu_subtype);
395 #else
396 # error target not supported
397 #endif
398             break;
399         }
400         case 202: /* host_page_size */
401         {
402             mig_reply_error_t *err = (mig_reply_error_t *)hdr;
403             uint32_t *pagesize = (uint32_t *)(err+1);
404
405             DPRINTF("pagesize = %d\n", *pagesize);
406             break;
407         }
408         default: break;
409     }
410
411     swap_mach_msg(hdr, bswap_out);
412
413     return ret;
414 }
415
416 long do_mach_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
417                 uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
418                 uint32_t arg8)
419 {
420     extern uint32_t mach_reply_port();
421
422     long ret = 0;
423
424     arg1 = tswap32(arg1);
425     arg2 = tswap32(arg2);
426     arg3 = tswap32(arg3);
427     arg4 = tswap32(arg4);
428     arg5 = tswap32(arg5);
429     arg6 = tswap32(arg6);
430     arg7 = tswap32(arg7);
431     arg8 = tswap32(arg8);
432
433     DPRINTF("mach syscall %d : " , num);
434
435     switch(num) {
436     /* see xnu/osfmk/mach/syscall_sw.h */
437     case -26:
438         DPRINTF("mach_reply_port()\n");
439         ret = mach_reply_port();
440         break;
441     case -27:
442         DPRINTF("mach_thread_self()\n");
443         ret = mach_thread_self();
444         break;
445     case -28:
446         DPRINTF("mach_task_self()\n");
447         ret = mach_task_self();
448         break;
449     case -29:
450         DPRINTF("mach_host_self()\n");
451         ret = mach_host_self();
452         break;
453     case -31:
454         DPRINTF("mach_msg_trap(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
455                 arg1, arg2, arg3, arg4, arg5, arg6, arg7);
456         ret = target_mach_msg_trap((mach_msg_header_t *)arg1, arg2, arg3, arg4, arg5, arg6, arg7);
457         break;
458 /* may need more translation if target arch is different from host */
459 #if (defined(TARGET_I386) && defined(__i386__)) || (defined(TARGET_PPC) && defined(__ppc__))
460     case -33:
461         DPRINTF("semaphore_signal_trap(0x%x)\n", arg1);
462         ret = semaphore_signal_trap(arg1);
463         break;
464     case -34:
465         DPRINTF("semaphore_signal_all_trap(0x%x)\n", arg1);
466         ret = semaphore_signal_all_trap(arg1);
467         break;
468     case -35:
469         DPRINTF("semaphore_signal_thread_trap(0x%x)\n", arg1, arg2);
470         ret = semaphore_signal_thread_trap(arg1,arg2);
471         break;
472 #endif
473     case -36:
474         DPRINTF("semaphore_wait_trap(0x%x)\n", arg1);
475         extern int semaphore_wait_trap(int); // XXX: is there any header for that?
476         ret = semaphore_wait_trap(arg1);
477         break;
478 /* may need more translation if target arch is different from host */
479 #if (defined(TARGET_I386) && defined(__i386__)) || (defined(TARGET_PPC) && defined(__ppc__))
480     case -37:
481         DPRINTF("semaphore_wait_signal_trap(0x%x, 0x%x)\n", arg1, arg2);
482         ret = semaphore_wait_signal_trap(arg1,arg2);
483         break;
484 #endif
485     case -43:
486         DPRINTF("map_fd(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
487                 arg1, arg2, arg3, arg4, arg5);
488         ret = map_fd(arg1, arg2, (void*)arg3, arg4, arg5);
489         tswap32s((uint32_t*)arg3);
490         break;
491 /* may need more translation if target arch is different from host */
492 #if (defined(TARGET_I386) && defined(__i386__)) || (defined(TARGET_PPC) && defined(__ppc__))
493     case -61:
494         DPRINTF("syscall_thread_switch(0x%x, 0x%x, 0x%x)\n",
495                 arg1, arg2, arg3);
496         ret = syscall_thread_switch(arg1, arg2, arg3);  // just a hint to the scheduler; can drop?
497         break;
498 #endif
499     case -89:
500         DPRINTF("mach_timebase_info(0x%x)\n", arg1);
501         struct mach_timebase_info info;
502         ret = mach_timebase_info(&info);
503         if(!is_error(ret))
504         {
505             struct mach_timebase_info *outInfo = (void*)arg1;
506             outInfo->numer = tswap32(info.numer);
507             outInfo->denom = tswap32(info.denom);
508         }
509         break;
510     case -90:
511         DPRINTF("mach_wait_until()\n");
512         extern int mach_wait_until(uint64_t); // XXX: is there any header for that?
513         ret = mach_wait_until(((uint64_t)arg2<<32) | (uint64_t)arg1);
514         break;
515     case -91:
516         DPRINTF("mk_timer_create()\n");
517         extern int mk_timer_create(); // XXX: is there any header for that?
518         ret = mk_timer_create();
519         break;
520     case -92:
521         DPRINTF("mk_timer_destroy()\n");
522         extern int mk_timer_destroy(int); // XXX: is there any header for that?
523         ret = mk_timer_destroy(arg1);
524         break;
525     case -93:
526         DPRINTF("mk_timer_create()\n");
527         extern int mk_timer_arm(int, uint64_t); // XXX: is there any header for that?
528         ret = mk_timer_arm(arg1, ((uint64_t)arg3<<32) | (uint64_t)arg2);
529         break;
530     case -94:
531         DPRINTF("mk_timer_cancel()\n");
532         extern int mk_timer_cancel(int, uint64_t *); // XXX: is there any header for that?
533         ret = mk_timer_cancel(arg1, (uint64_t *)arg2);
534         if((!is_error(ret)) && arg2)
535             tswap64s((uint64_t *)arg2);
536         break;
537     default:
538         gemu_log("qemu: Unsupported mach syscall: %d(0x%x)\n", num, num);
539         gdb_handlesig (cpu_env, SIGTRAP);
540         exit(0);
541         break;
542     }
543     return ret;
544 }
545
546 /* ------------------------------------------------------------
547    thread type syscall handling
548 */
549 long do_thread_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
550                 uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
551                 uint32_t arg8)
552 {
553     extern uint32_t cthread_set_self(uint32_t);
554     extern uint32_t processor_facilities_used();
555     long ret = 0;
556
557     arg1 = tswap32(arg1);
558     arg2 = tswap32(arg2);
559     arg3 = tswap32(arg3);
560     arg4 = tswap32(arg4);
561     arg5 = tswap32(arg5);
562     arg6 = tswap32(arg6);
563     arg7 = tswap32(arg7);
564     arg8 = tswap32(arg8);
565
566     DPRINTF("thread syscall %d : " , num);
567
568     switch(num) {
569 #ifdef TARGET_I386
570     case 0x3:
571 #endif
572     case 0x7FF1: /* cthread_set_self */
573         DPRINTF("cthread_set_self(0x%x)\n", (unsigned int)arg1);
574         ret = cthread_set_self(arg1);
575 #ifdef TARGET_I386
576         /* we need to update the LDT with the address of the thread */
577         write_dt((void *)(((CPUX86State *) cpu_env)->ldt.base + (4 * sizeof(uint64_t))), arg1, 1,
578                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
579                  (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
580         /* New i386 convention, %gs should be set to our this LDT entry */
581         cpu_x86_load_seg(cpu_env, R_GS, 0x27);
582         /* Old i386 convention, the kernel returns the selector for the cthread (pre-10.4.8?)*/
583         ret = 0x27;
584 #endif
585         break;
586     case 0x7FF2: /* Called the super-fast pthread_self handler by the apple guys */
587         DPRINTF("pthread_self()\n");
588         ret = (uint32_t)pthread_self();
589         break;
590     case 0x7FF3:
591         DPRINTF("processor_facilities_used()\n");
592 #ifdef __i386__
593         qerror("processor_facilities_used: not implemented!\n");
594 #else
595         ret = (uint32_t)processor_facilities_used();
596 #endif
597         break;
598     default:
599         gemu_log("qemu: Unsupported thread syscall: %d(0x%x)\n", num, num);
600         gdb_handlesig (cpu_env, SIGTRAP);
601         exit(0);
602         break;
603     }
604     return ret;
605 }
606
607 /* ------------------------------------------------------------
608    ioctl handling
609 */
610 static inline void byteswap_termios(struct termios *t)
611 {
612     tswap32s((uint32_t*)&t->c_iflag);
613     tswap32s((uint32_t*)&t->c_oflag);
614     tswap32s((uint32_t*)&t->c_cflag);
615     tswap32s((uint32_t*)&t->c_lflag);
616     /* 20 (char) bytes then */
617     tswap32s((uint32_t*)&t->c_ispeed);
618     tswap32s((uint32_t*)&t->c_ospeed);
619 }
620
621 static inline void byteswap_winsize(struct winsize *w)
622 {
623     tswap16s(&w->ws_row);
624     tswap16s(&w->ws_col);
625     tswap16s(&w->ws_xpixel);
626     tswap16s(&w->ws_ypixel);
627 }
628
629 #define STRUCT(name, list...) STRUCT_ ## name,
630 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
631 enum {
632 #include "ioctls_types.h"
633 };
634 #undef STRUCT
635 #undef STRUCT_SPECIAL
636
637 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
638 #define STRUCT_SPECIAL(name)
639 #include "ioctls_types.h"
640 #undef STRUCT
641 #undef STRUCT_SPECIAL
642
643 typedef struct IOCTLEntry {
644     unsigned int target_cmd;
645     unsigned int host_cmd;
646     const char *name;
647     int access;
648     const argtype arg_type[5];
649 } IOCTLEntry;
650
651 #define IOC_R 0x0001
652 #define IOC_W 0x0002
653 #define IOC_RW (IOC_R | IOC_W)
654
655 #define MAX_STRUCT_SIZE 4096
656
657 IOCTLEntry ioctl_entries[] = {
658 #define IOCTL(cmd, access, types...) \
659     { cmd, cmd, #cmd, access, { types } },
660 #include "ioctls.h"
661     { 0, 0, },
662 };
663
664 /* ??? Implement proper locking for ioctls.  */
665 static long do_ioctl(long fd, long cmd, long arg)
666 {
667     const IOCTLEntry *ie;
668     const argtype *arg_type;
669     int ret;
670     uint8_t buf_temp[MAX_STRUCT_SIZE];
671     int target_size;
672     void *argptr;
673
674     ie = ioctl_entries;
675     for(;;) {
676         if (ie->target_cmd == 0) {
677             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
678             return -ENOSYS;
679         }
680         if (ie->target_cmd == cmd)
681             break;
682         ie++;
683     }
684     arg_type = ie->arg_type;
685 #if defined(DEBUG)
686     gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
687 #endif
688     switch(arg_type[0]) {
689     case TYPE_NULL:
690         /* no argument */
691         ret = get_errno(ioctl(fd, ie->host_cmd));
692         break;
693     case TYPE_PTRVOID:
694     case TYPE_INT:
695         /* int argment */
696         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
697         break;
698     case TYPE_PTR:
699         arg_type++;
700         target_size = thunk_type_size(arg_type, 0);
701         switch(ie->access) {
702         case IOC_R:
703             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
704             if (!is_error(ret)) {
705                 argptr = lock_user(arg, target_size, 0);
706                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
707                 unlock_user(argptr, arg, target_size);
708             }
709             break;
710         case IOC_W:
711             argptr = lock_user(arg, target_size, 1);
712             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
713             unlock_user(argptr, arg, 0);
714             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
715             break;
716         default:
717         case IOC_RW:
718             argptr = lock_user(arg, target_size, 1);
719             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
720             unlock_user(argptr, arg, 0);
721             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
722             if (!is_error(ret)) {
723                 argptr = lock_user(arg, target_size, 0);
724                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
725                 unlock_user(argptr, arg, target_size);
726             }
727             break;
728         }
729         break;
730     default:
731         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
732         ret = -ENOSYS;
733         break;
734     }
735     return ret;
736 }
737
738 /* ------------------------------------------------------------
739    Unix syscall handling
740 */
741
742 static inline void byteswap_attrlist(struct attrlist *a)
743 {
744     tswap16s(&a->bitmapcount);
745     tswap16s(&a->reserved);
746     tswap32s(&a->commonattr);
747     tswap32s(&a->volattr);
748     tswap32s(&a->dirattr);
749     tswap32s(&a->fileattr);
750     tswap32s(&a->forkattr);
751 }
752
753 struct attrbuf_header {
754     unsigned long length;
755 };
756
757 static inline void byteswap_attrbuf(struct attrbuf_header *attrbuf, struct attrlist *attrlist)
758 {
759     DPRINTF("attrBuf.lenght %lx\n", attrbuf->length);
760 }
761
762 static inline void byteswap_statfs(struct statfs *s)
763 {
764     tswap16s((uint16_t*)&s->f_otype);
765     tswap16s((uint16_t*)&s->f_oflags);
766     tswap32s((uint32_t*)&s->f_bsize);
767     tswap32s((uint32_t*)&s->f_iosize);
768     tswap32s((uint32_t*)&s->f_blocks);
769     tswap32s((uint32_t*)&s->f_bfree);
770     tswap32s((uint32_t*)&s->f_bavail);
771     tswap32s((uint32_t*)&s->f_files);
772     tswap32s((uint32_t*)&s->f_ffree);
773     tswap32s((uint32_t*)&s->f_fsid.val[0]);
774     tswap32s((uint32_t*)&s->f_fsid.val[1]);
775     tswap16s((uint16_t*)&s->f_reserved1);
776     tswap16s((uint16_t*)&s->f_type);
777     tswap32s((uint32_t*)&s->f_flags);
778 }
779
780 static inline void byteswap_stat(struct stat *s)
781 {
782     tswap32s((uint32_t*)&s->st_dev);
783     tswap32s(&s->st_ino);
784     tswap16s(&s->st_mode);
785     tswap16s(&s->st_nlink);
786     tswap32s(&s->st_uid);
787     tswap32s(&s->st_gid);
788     tswap32s((uint32_t*)&s->st_rdev);
789     tswap32s((uint32_t*)&s->st_atimespec.tv_sec);
790     tswap32s((uint32_t*)&s->st_atimespec.tv_nsec);
791     tswap32s((uint32_t*)&s->st_mtimespec.tv_sec);
792     tswap32s((uint32_t*)&s->st_mtimespec.tv_nsec);
793     tswap32s((uint32_t*)&s->st_ctimespec.tv_sec);
794     tswap32s((uint32_t*)&s->st_ctimespec.tv_nsec);
795     tswap64s((uint64_t*)&s->st_size);
796     tswap64s((uint64_t*)&s->st_blocks);
797     tswap32s((uint32_t*)&s->st_blksize);
798     tswap32s(&s->st_flags);
799     tswap32s(&s->st_gen);
800 }
801
802 static inline void byteswap_dirents(struct dirent *d, int bytes)
803 {
804     char *b;
805     for( b = (char*)d; (int)b < (int)d+bytes; )
806     {
807         unsigned short s = ((struct dirent *)b)->d_reclen;
808         tswap32s(&((struct dirent *)b)->d_ino);
809         tswap16s(&((struct dirent *)b)->d_reclen);
810         if(s<=0)
811             break;
812         b += s;
813     }
814 }
815
816 static inline void byteswap_iovec(struct iovec *v, int n)
817 {
818     int i;
819     for(i = 0; i < n; i++)
820     {
821         tswap32s((uint32_t*)&v[i].iov_base);
822         tswap32s((uint32_t*)&v[i].iov_len);
823     }
824 }
825
826 static inline void byteswap_timeval(struct timeval *t)
827 {
828     tswap32s((uint32_t*)&t->tv_sec);
829     tswap32s((uint32_t*)&t->tv_usec);
830 }
831
832 long do_unix_syscall_indirect(void *cpu_env, int num);
833 long do_sync();
834 long do_exit(uint32_t arg1);
835 long do_getlogin(char *out, uint32_t size);
836 long do_open(char * arg1, uint32_t arg2, uint32_t arg3);
837 long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3);
838 long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3);
839 long do_execve(char* arg1, char ** arg2, char ** arg3);
840 long do_getgroups(uint32_t arg1, gid_t * arg2);
841 long do_gettimeofday(struct timeval * arg1, void * arg2);
842 long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
843 long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
844 long do_utimes(char * arg1, struct timeval * arg2);
845 long do_futimes(uint32_t arg1, struct timeval * arg2);
846 long do_statfs(char * arg1, struct statfs * arg2);
847 long do_fstatfs(uint32_t arg1, struct statfs * arg2);
848 long do_stat(char * arg1, struct stat * arg2);
849 long do_fstat(uint32_t arg1, struct stat * arg2);
850 long do_lstat(char * arg1, struct stat * arg2);
851 long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4);
852 long do_lseek(void *cpu_env, int num);
853 long do___sysctl(int * name, uint32_t namelen, void * oldp, size_t * oldlenp, void * newp, size_t newlen  /* ignored */);
854 long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5);
855 long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8);
856 long do_fcntl(int fd, int cmd, int arg);
857
858 long no_syscall(void *cpu_env, int num);
859
860 long do_pread(uint32_t arg1, void * arg2, size_t arg3, off_t arg4)
861 {
862     DPRINTF("0x%x, %p, 0x%lx, 0x%llx\n", arg1, arg2, arg3, arg4);
863     long ret = pread(arg1, arg2, arg3, arg4);
864     return ret;
865 }
866
867 long do_read(int d, void *buf, size_t nbytes)
868 {
869     DPRINTF("0x%x, %p, 0x%lx\n", d, buf, nbytes);
870     long ret = get_errno(read(d, buf, nbytes));
871     if(!is_error(ret))
872         DPRINTF("%x\n", *(uint32_t*)buf);
873     return ret;
874 }
875
876 long unimpl_unix_syscall(void *cpu_env, int num);
877
878 typedef long (*syscall_function_t)(void *cpu_env, int num);
879
880
881 /* define a table that will handle the syscall number->function association */
882 #define VOID    void
883 #define INT     (uint32_t)get_int_arg(&i, cpu_env)
884 #define INT64   (uint64_t)get_int64_arg(&i, cpu_env)
885 #define UINT    (unsigned int)INT
886 #define PTR     (void*)INT
887
888 #define SIZE    INT
889 #define OFFSET  INT64
890
891 #define WRAPPER_CALL_DIRECT_0(function, args) long __qemu_##function(void *cpu_env) {  return (long)function(); }
892 #define WRAPPER_CALL_DIRECT_1(function, _arg1) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1;  return (long)function(arg1); }
893 #define WRAPPER_CALL_DIRECT_2(function, _arg1, _arg2) long __qemu_##function(void *cpu_env) { int i = 0;  typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; return (long)function(arg1, arg2); }
894 #define WRAPPER_CALL_DIRECT_3(function, _arg1, _arg2, _arg3) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; return (long)function(arg1, arg2, arg3); }
895 #define WRAPPER_CALL_DIRECT_4(function, _arg1, _arg2, _arg3, _arg4) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; return (long)function(arg1, arg2, arg3, arg4); }
896 #define WRAPPER_CALL_DIRECT_5(function, _arg1, _arg2, _arg3, _arg4, _arg5) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5;  return (long)function(arg1, arg2, arg3, arg4, arg5); }
897 #define WRAPPER_CALL_DIRECT_6(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6;  return (long)function(arg1, arg2, arg3, arg4, arg5, arg6); }
898 #define WRAPPER_CALL_DIRECT_7(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6; typeof(_arg7) arg7 = _arg7; return (long)function(arg1, arg2, arg3, arg4, arg5, arg6, arg7); }
899 #define WRAPPER_CALL_DIRECT_8(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) long __qemu_##function(void *cpu_env) { int i = 0;   typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6; typeof(_arg7) arg7 = _arg7; typeof(_arg8) arg8 = _arg8;  return (long)function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); }
900 #define WRAPPER_CALL_DIRECT(function, nargs, args...) WRAPPER_CALL_DIRECT_##nargs(function, args)
901 #define WRAPPER_CALL_NOERRNO(function, nargs, args...)  WRAPPER_CALL_DIRECT(function, nargs, args)
902 #define WRAPPER_CALL_INDIRECT(function, nargs, args...)
903 #define ENTRY(name, number, function, nargs, call_type, args...)  WRAPPER_##call_type(function, nargs, args)
904
905 #include "syscalls.h"
906
907 #undef ENTRY
908 #undef WRAPPER_CALL_DIRECT
909 #undef WRAPPER_CALL_NOERRNO
910 #undef WRAPPER_CALL_INDIRECT
911 #undef OFFSET
912 #undef SIZE
913 #undef INT
914 #undef PTR
915 #undef INT64
916
917 #define _ENTRY(name, number, function, nargs, call_type) [number] = {\
918         name, \
919         number, \
920         (syscall_function_t)function, \
921         nargs, \
922         call_type  \
923         },
924
925 #define ENTRY_CALL_DIRECT(name, number, function, nargs, call_type)  _ENTRY(name, number, __qemu_##function, nargs, call_type)
926 #define ENTRY_CALL_NOERRNO(name, number, function, nargs, call_type) ENTRY_CALL_DIRECT(name, number, function, nargs, call_type)
927 #define ENTRY_CALL_INDIRECT(name, number, function, nargs, call_type) _ENTRY(name, number, function, nargs, call_type)
928 #define ENTRY(name, number, function, nargs, call_type, args...) ENTRY_##call_type(name, number, function, nargs, call_type)
929
930 #define CALL_DIRECT 1
931 #define CALL_INDIRECT 2
932 #define CALL_NOERRNO  (CALL_DIRECT | 4 /* = 5 */)
933
934 struct unix_syscall {
935     char * name;
936     int number;
937     syscall_function_t function;
938     int nargs;
939     int call_type;
940 } unix_syscall_table[SYS_MAXSYSCALL] = {
941 #include "syscalls.h"
942 };
943
944 #undef ENTRY
945 #undef _ENTRY
946 #undef ENTRY_CALL_DIRECT
947 #undef ENTRY_CALL_INDIRECT
948 #undef ENTRY_CALL_NOERRNO
949
950 /* Actual syscalls implementation */
951
952 long do_unix_syscall_indirect(void *cpu_env, int num)
953 {
954     long ret;
955     int new_num;
956     int i = 0;
957
958     new_num = get_int_arg(&i, cpu_env);
959 #ifdef TARGET_I386
960     ((CPUX86State*)cpu_env)->regs[R_ESP] += 4;
961     /* XXX: not necessary */
962     ((CPUX86State*)cpu_env)->regs[R_EAX] = new_num;
963 #elif TARGET_PPC
964     {
965         int i;
966         uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
967         for(i = 3; i < 11; i++)
968             *regs[i] = *regs[i+1];
969         /* XXX: not necessary */
970         *regs[0] = new_num;
971     }
972 #endif
973     ret = do_unix_syscall(cpu_env, new_num);
974 #ifdef TARGET_I386
975     ((CPUX86State*)cpu_env)->regs[R_ESP] -= 4;
976     /* XXX: not necessary */
977     ((CPUX86State*)cpu_env)->regs[R_EAX] = num;
978 #elif TARGET_PPC
979     {
980         int i;
981         /* XXX: not really needed those regs are volatile accross calls */
982         uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
983         for(i = 11; i > 3; i--)
984             *regs[i] = *regs[i-1];
985         regs[3] = new_num;
986         *regs[0] = num;
987     }
988 #endif
989     return ret;
990 }
991
992 long do_exit(uint32_t arg1)
993 {
994     exit(arg1);
995     /* not reached */
996     return -1;
997 }
998
999 long do_sync()
1000 {
1001     sync();
1002     return 0;
1003 }
1004
1005 long do_getlogin(char *out, uint32_t size)
1006 {
1007     char *login = getlogin();
1008     if(!login)
1009         return -1;
1010     memcpy(out, login, size);
1011     return 0;
1012 }
1013 long do_open(char * arg1, uint32_t arg2, uint32_t arg3)
1014 {
1015     /* XXX: don't let the %s stay in there */
1016     DPRINTF("open(%s, 0x%x, 0x%x)\n", arg1, arg2, arg3);
1017     return get_errno(open(arg1, arg2, arg3));
1018 }
1019
1020 long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3)
1021 {
1022     long ret;
1023     DPRINTF("getfsstat(%p, 0x%x, 0x%x)\n", arg1, arg2, arg3);
1024     ret = get_errno(getfsstat(arg1, arg2, arg3));
1025     if((!is_error(ret)) && arg1)
1026         byteswap_statfs(arg1);
1027     return ret;
1028 }
1029
1030 long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3)
1031 {
1032     long ret;
1033     DPRINTF("sigprocmask(%d, %p, %p)\n", arg1, arg2, arg3);
1034     gemu_log("XXX: sigprocmask not tested (%d, %p, %p)\n", arg1, arg2, arg3);
1035     if(arg2)
1036         tswap32s(arg2);
1037     ret = get_errno(sigprocmask(arg1, (void *)arg2, (void *)arg3));
1038     if((!is_error(ret)) && arg3)
1039         tswap32s(arg3);
1040     if(arg2)
1041         tswap32s(arg2);
1042     return ret;
1043 }
1044
1045 long do_execve(char* arg1, char ** arg2, char ** arg3)
1046 {
1047     long ret;
1048     char **argv = arg2;
1049     char **envp = arg3;
1050     int argc;
1051     int envc;
1052
1053     /* XXX: don't let the %s stay in here */
1054     DPRINTF("execve(%s, %p, %p)\n", arg1, arg2, arg3);
1055
1056     for(argc = 0; argv[argc]; argc++);
1057     for(envc = 0; envp[envc]; envc++);
1058
1059     argv = (char**)malloc(sizeof(char*)*argc);
1060     envp = (char**)malloc(sizeof(char*)*envc);
1061
1062     for(; argc >= 0; argc--)
1063         argv[argc] = (char*)tswap32((uint32_t)(arg2)[argc]);
1064
1065     for(; envc >= 0; envc--)
1066         envp[envc] = (char*)tswap32((uint32_t)(arg3)[envc]);
1067
1068     ret = get_errno(execve(arg1, argv, envp));
1069     free(argv);
1070     free(envp);
1071     return ret;
1072 }
1073
1074 long do_getgroups(uint32_t arg1, gid_t * arg2)
1075 {
1076     long ret;
1077     int i;
1078     DPRINTF("getgroups(0x%x, %p)\n", arg1, arg2);
1079     ret = get_errno(getgroups(arg1, arg2));
1080     if(ret > 0)
1081         for(i = 0; i < arg1; i++)
1082             tswap32s(&arg2[i]);
1083     return ret;
1084 }
1085
1086 long do_gettimeofday(struct timeval * arg1, void * arg2)
1087 {
1088     long ret;
1089     DPRINTF("gettimeofday(%p, %p)\n",
1090             arg1, arg2);
1091     ret = get_errno(gettimeofday(arg1, arg2));
1092     if(!is_error(ret))
1093     {
1094         /* timezone no longer used according to the manpage, so don't bother with it */
1095         byteswap_timeval(arg1);
1096     }
1097     return ret;
1098 }
1099
1100 long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1101 {
1102     long ret;
1103     DPRINTF("readv(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1104     if(arg2)
1105         byteswap_iovec(arg2, arg3);
1106     ret = get_errno(readv(arg1, arg2, arg3));
1107     if((!is_error(ret)) && arg2)
1108         byteswap_iovec(arg2, arg3);
1109     return ret;
1110 }
1111
1112 long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1113 {
1114     long ret;
1115     DPRINTF("writev(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1116     if(arg2)
1117         byteswap_iovec(arg2, arg3);
1118     ret = get_errno(writev(arg1, arg2, arg3));
1119     if((!is_error(ret)) && arg2)
1120         byteswap_iovec(arg2, arg3);
1121     return ret;
1122 }
1123
1124 long do_utimes(char * arg1, struct timeval * arg2)
1125 {
1126     DPRINTF("utimes(%p, %p)\n", arg1, arg2);
1127     if(arg2)
1128     {
1129         byteswap_timeval(arg2);
1130         byteswap_timeval(arg2+1);
1131     }
1132     return get_errno(utimes(arg1, arg2));
1133 }
1134
1135 long do_futimes(uint32_t arg1, struct timeval * arg2)
1136 {
1137     DPRINTF("futimes(0x%x, %p)\n", arg1, arg2);
1138     if(arg2)
1139     {
1140         byteswap_timeval(arg2);
1141         byteswap_timeval(arg2+1);
1142     }
1143     return get_errno(futimes(arg1, arg2));
1144 }
1145
1146 long do_statfs(char * arg1, struct statfs * arg2)
1147 {
1148     long ret;
1149     DPRINTF("statfs(%p, %p)\n", arg1, arg2);
1150     ret = get_errno(statfs(arg1, arg2));
1151     if(!is_error(ret))
1152         byteswap_statfs(arg2);
1153     return ret;
1154 }
1155
1156 long do_fstatfs(uint32_t arg1, struct statfs* arg2)
1157 {
1158     long ret;
1159     DPRINTF("fstatfs(0x%x, %p)\n",
1160             arg1, arg2);
1161     ret = get_errno(fstatfs(arg1, arg2));
1162     if(!is_error(ret))
1163         byteswap_statfs(arg2);
1164
1165     return ret;
1166 }
1167
1168 long do_stat(char * arg1, struct stat * arg2)
1169 {
1170     long ret;
1171     /* XXX: don't let the %s stay in there */
1172     DPRINTF("stat(%s, %p)\n", arg1, arg2);
1173     ret = get_errno(stat(arg1, arg2));
1174     if(!is_error(ret))
1175         byteswap_stat(arg2);
1176     return ret;
1177 }
1178
1179 long do_fstat(uint32_t arg1, struct stat * arg2)
1180 {
1181     long ret;
1182     DPRINTF("fstat(0x%x, %p)\n", arg1, arg2);
1183     ret = get_errno(fstat(arg1, arg2));
1184     if(!is_error(ret))
1185         byteswap_stat(arg2);
1186     return ret;
1187 }
1188
1189 long do_lstat(char * arg1, struct stat * arg2)
1190 {
1191     long ret;
1192     /* XXX: don't let the %s stay in there */
1193     DPRINTF("lstat(%s, %p)\n", (const char *)arg1, arg2);
1194     ret = get_errno(lstat(arg1, arg2));
1195     if(!is_error(ret))
1196         byteswap_stat(arg2);
1197     return ret;
1198 }
1199
1200 long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4)
1201 {
1202     long ret;
1203     DPRINTF("getdirentries(0x%x, %p, 0x%x, %p)\n", arg1, arg2, arg3, arg4);
1204     if(arg4)
1205         tswap32s((uint32_t *)arg4);
1206     ret = get_errno(getdirentries(arg1, arg2, arg3, arg4));
1207     if(arg4)
1208         tswap32s((uint32_t *)arg4);
1209     if(!is_error(ret))
1210         byteswap_dirents(arg2, ret);
1211     return ret;
1212 }
1213
1214 long do_lseek(void *cpu_env, int num)
1215 {
1216     long ret;
1217     int i = 0;
1218     uint32_t arg1 = get_int_arg(&i, cpu_env);
1219     uint64_t offset = get_int64_arg(&i, cpu_env);
1220     uint32_t arg3 = get_int_arg(&i, cpu_env);
1221     uint64_t r = lseek(arg1, offset, arg3);
1222 #ifdef TARGET_I386
1223     /* lowest word in eax, highest in edx */
1224     ret = r & 0xffffffff; /* will be set to eax after do_unix_syscall exit */
1225     ((CPUX86State *)cpu_env)->regs[R_EDX] = (uint32_t)((r >> 32) & 0xffffffff) ;
1226 #elif defined TARGET_PPC
1227     ret = r & 0xffffffff; /* will be set to r3 after do_unix_syscall exit */
1228     ((CPUPPCState *)cpu_env)->gpr[4] = (uint32_t)((r >> 32) & 0xffffffff) ;
1229 #else
1230     qerror("64 bit ret value on your arch?");
1231 #endif
1232     return get_errno(ret);
1233 }
1234
1235 void no_swap(void * oldp, int size)
1236 {
1237 }
1238
1239 void sysctl_tswap32s(void * oldp, int size)
1240 {
1241     tswap32s(oldp);
1242 }
1243
1244 void bswap_oid(uint32_t * oldp, int size)
1245 {
1246     int count = size / sizeof(int);
1247     int i = 0;
1248     do { tswap32s(oldp + i); } while (++i < count);
1249 }
1250
1251 void sysctl_usrstack(uint32_t * oldp, int size)
1252 {
1253     DPRINTF("sysctl_usrstack: 0x%x\n", *oldp);
1254     tswap32s(oldp);
1255 }
1256
1257 void sysctl_ncpu(uint32_t * ncpu, int size)
1258 {
1259     *ncpu = 0x1;
1260     DPRINTF("sysctl_ncpu: 0x%x\n", *ncpu);
1261     tswap32s(ncpu);
1262 }
1263
1264 void sysctl_exec(char * exec, int size)
1265 {
1266     DPRINTF("sysctl_exec: %s\n", exec);
1267 }
1268
1269 void sysctl_translate(char * exec, int size)
1270 {
1271     DPRINTF("sysctl_translate: %s\n", exec);
1272 }
1273
1274 struct sysctl_dir {
1275     int num;
1276     const char * name;
1277     void (*swap_func)(void *, int);
1278     struct sysctl_dir *childs;
1279 };
1280
1281 #define ENTRYD(num, name, childs) { num, name, NULL, childs }
1282 #define ENTRYE(num, name, func)   { num, name, (void (*)(void *, int))func, NULL  }
1283 struct sysctl_dir sysctls_unspec[] = {
1284     ENTRYE(3,  "oip", bswap_oid),
1285     { 0, NULL, NULL, NULL }
1286 };
1287
1288 struct sysctl_dir sysctls_kern[] = {
1289     ENTRYE(KERN_TRANSLATE,          "translate",    sysctl_translate), /* 44 */
1290     ENTRYE(KERN_EXEC,               "exec",         sysctl_exec), /* 45 */
1291     ENTRYE(KERN_USRSTACK32,          "KERN_USRSTACK32", sysctl_usrstack), /* 35 */
1292     ENTRYE(KERN_SHREG_PRIVATIZABLE,  "KERN_SHREG_PRIVATIZABLE", sysctl_tswap32s), /* 54 */
1293     { 0, NULL, NULL, NULL }
1294 };
1295
1296 struct sysctl_dir sysctls_hw[] = {
1297     ENTRYE(HW_NCPU, "ncpud", sysctl_tswap32s),
1298     ENTRYE(104, "104", no_swap),
1299     ENTRYE(105, "105", no_swap),
1300     { 0, NULL, NULL, NULL }
1301 };
1302
1303 struct sysctl_dir sysctls[] = {
1304     ENTRYD(CTL_UNSPEC, "unspec", sysctls_unspec),
1305     ENTRYD(CTL_KERN, "kern", sysctls_kern),
1306     ENTRYD(CTL_HW,   "hw",   sysctls_hw ),
1307     { 0, NULL, NULL, NULL }
1308 };
1309
1310 #undef ENTRYE
1311 #undef ENTRYD
1312
1313 static inline struct sysctl_dir * get_sysctl_entry_for_mib(int mib, struct sysctl_dir * sysctl_elmt)
1314 {
1315     if(!sysctl_elmt)
1316         return NULL;
1317     for(; sysctl_elmt->name != NULL ; sysctl_elmt++) {
1318         if(sysctl_elmt->num == mib)
1319             return sysctl_elmt;
1320     }
1321     return NULL;
1322 }
1323
1324 static inline long bswap_syctl(int * mib, int count, void *buf, int size)
1325 {
1326     int i;
1327     struct sysctl_dir * sysctl = sysctls;
1328     struct sysctl_dir * ret = NULL;
1329
1330     for(i = 0; i < count; i++) {
1331
1332         if(!(ret = sysctl = get_sysctl_entry_for_mib(mib[i], sysctl))) {
1333             gemu_log("bswap_syctl: can't find mib %d\n", mib[i]);
1334             return -ENOTDIR;
1335         }
1336         if(!(sysctl = sysctl->childs))
1337             break;
1338     }
1339
1340     if(ret->childs)
1341         qerror("we shouldn't have a directory element\n");
1342
1343     ret->swap_func(buf, size);
1344     return 0;
1345 }
1346
1347 static inline void print_syctl(int * mib, int count)
1348 {
1349     int i;
1350     struct sysctl_dir * sysctl = sysctls;
1351     struct sysctl_dir * ret = NULL;
1352
1353     for(i = 0; i < count; i++) {
1354         if(!(ret = sysctl = get_sysctl_entry_for_mib(mib[i], sysctl))){
1355             gemu_log("print_syctl: can't find mib %d\n", mib[i]);
1356             return;
1357         }
1358         DPRINTF("%s.", sysctl->name);
1359         if(!(sysctl = sysctl->childs))
1360             break;
1361     }
1362     DPRINTF("\n");
1363 }
1364
1365 long do___sysctl(int * name, uint32_t namelen, void * oldp, size_t * oldlenp, void * newp, size_t newlen  /* ignored */)
1366 {
1367     long ret = 0;
1368     int i;
1369     DPRINTF("sysctl(%p, 0x%x, %p, %p, %p, 0x%lx)\n",
1370             name, namelen, oldp, oldlenp, newp, newlen);
1371     if(name) {
1372         i = 0;
1373         do { tswap32s( name + i); } while (++i < namelen);
1374         print_syctl(name, namelen);
1375         //bswap_syctl(name, namelen, newp, newlen);
1376         tswap32s((uint32_t*)oldlenp);
1377     }
1378
1379     if(name) /* Sometimes sysctl is called with no arg1, ignore */
1380         ret = get_errno(sysctl(name, namelen, oldp, oldlenp, newp, newlen));
1381
1382 #if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1383     if (!is_error(ret) && bswap_syctl(name, namelen, oldp, *oldlenp) != 0) {
1384         return -ENOTDIR;
1385     }
1386 #endif
1387
1388     if(name) {
1389         //bswap_syctl(name, namelen, newp, newlen);
1390         tswap32s((uint32_t*)oldlenp);
1391
1392         i = 0;
1393         do { tswap32s( name + i); } while (++i < namelen);
1394     }
1395     return ret;
1396 }
1397
1398 long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5)
1399 {
1400     struct attrlist * attrlist = (void *)arg2;
1401     long ret;
1402
1403 #if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1404     gemu_log("SYS_getdirentriesattr unimplemented\n");
1405     return -ENOTSUP;
1406 #endif
1407     /* XXX: don't let the %s stay in there */
1408     DPRINTF("getattrlist(%s, %p, %p, 0x%x, 0x%x)\n",
1409             (char *)arg1, arg2, arg3, arg4, arg5);
1410
1411     if(arg2) /* XXX: We should handle that in a copy especially
1412         if the structure is not writable */
1413         byteswap_attrlist(attrlist);
1414
1415     ret = get_errno(getattrlist((const char* )arg1, attrlist, (void *)arg3, arg4, arg5));
1416
1417     if(!is_error(ret))
1418     {
1419         byteswap_attrbuf((void *)arg3, attrlist);
1420         byteswap_attrlist(attrlist);
1421     }
1422     return ret;
1423 }
1424
1425 long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8)
1426 {
1427     DPRINTF("getdirentriesattr(0x%x, %p, %p, 0x%lx, %p, %p, %p, 0x%x)\n",
1428             arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
1429 #if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1430     qerror("SYS_getdirentriesattr unimplemented\n");
1431 #endif
1432
1433     return get_errno(getdirentriesattr( arg1, (struct attrlist * )arg2, (void *)arg3, arg4,
1434                                        (unsigned long *)arg5, (unsigned long *)arg6,
1435                                        (unsigned long *)arg7, arg8));
1436 }
1437
1438 static inline void bswap_flock(struct flock *f)
1439 {
1440     tswap64s(&f->l_start);
1441     tswap64s(&f->l_len);
1442     tswap32s(&f->l_pid);
1443     tswap16s(&f->l_type);
1444     tswap16s(&f->l_whence);
1445 }
1446
1447 static inline void bswap_fstore(struct fstore *f)
1448 {
1449     tswap32s(&f->fst_flags);
1450     tswap32s(&f->fst_posmode);
1451     tswap64s(&f->fst_offset);
1452     tswap64s(&f->fst_length);
1453     tswap64s(&f->fst_bytesalloc);
1454 }
1455
1456 static inline void bswap_radvisory(struct radvisory *f)
1457 {
1458     tswap64s(&f->ra_offset);
1459     tswap32s(&f->ra_count);
1460 }
1461
1462 static inline void bswap_fbootstraptransfer(struct fbootstraptransfer *f)
1463 {
1464     tswap64s(&f->fbt_offset);
1465     tswap32s((uint32_t*)&f->fbt_length);
1466     tswap32s((uint32_t*)&f->fbt_buffer); /* XXX: this is a ptr */
1467 }
1468
1469 static inline void bswap_log2phys(struct log2phys *f)
1470 {
1471     tswap32s(&f->l2p_flags);
1472     tswap64s(&f->l2p_contigbytes);
1473     tswap64s(&f->l2p_devoffset);
1474 }
1475
1476 static inline void bswap_fcntl_arg(int cmd, void * arg)
1477 {
1478     switch(cmd)
1479     {
1480         case F_DUPFD:
1481         case F_GETFD:
1482         case F_SETFD:
1483         case F_GETFL:
1484         case F_SETFL:
1485         case F_GETOWN:
1486         case F_SETOWN:
1487         case F_SETSIZE:
1488         case F_RDAHEAD:
1489         case F_FULLFSYNC:
1490             break;
1491         case F_GETLK:
1492         case F_SETLK:
1493         case F_SETLKW:
1494             bswap_flock(arg);
1495             break;
1496         case F_PREALLOCATE:
1497             bswap_fstore(arg);
1498             break;
1499         case F_RDADVISE:
1500             bswap_radvisory(arg);
1501             break;
1502         case F_READBOOTSTRAP:
1503         case F_WRITEBOOTSTRAP:
1504             bswap_fbootstraptransfer(arg);
1505             break;
1506         case F_LOG2PHYS:
1507             bswap_log2phys(arg);
1508             break;
1509         default:
1510             gemu_log("unknow cmd in fcntl\n");
1511     }
1512 }
1513
1514 long do_fcntl(int fd, int cmd, int arg)
1515 {
1516     long ret;
1517     bswap_fcntl_arg(cmd, (void *)arg);
1518     ret = get_errno(fcntl(fd, cmd, arg));
1519     if(!is_error(ret))
1520         bswap_fcntl_arg(cmd, (void *)arg);
1521     return ret;
1522 }
1523
1524 long no_syscall(void *cpu_env, int num)
1525 {
1526     /* XXX: We should probably fordward it to the host kernel */
1527     qerror("no unix syscall %d\n", num);
1528     /* not reached */
1529     return -1;
1530 }
1531
1532 long unimpl_unix_syscall(void *cpu_env, int num)
1533 {
1534     if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1535         qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1536
1537     gemu_log("qemu: Unsupported unix syscall %s %d\n", unix_syscall_table[num].name , num);
1538     gdb_handlesig (cpu_env, SIGTRAP);
1539     exit(-1);
1540 }
1541
1542 long do_unix_syscall(void *cpu_env, int num)
1543 {
1544     long ret = 0;
1545
1546     DPRINTF("unix syscall %d: " , num);
1547
1548     if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1549         qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1550
1551     DPRINTF("%s [%s]", unix_syscall_table[num].name, unix_syscall_table[num].call_type & CALL_DIRECT ? "direct" : "indirect" );
1552     ret = unix_syscall_table[num].function(cpu_env, num);
1553
1554     if(!(unix_syscall_table[num].call_type & CALL_NOERRNO))
1555         ret = get_errno(ret);
1556
1557     DPRINTF("[returned 0x%x(%d)]\n", (int)ret, (int)ret);
1558     return ret;
1559 }
1560
1561 /* ------------------------------------------------------------
1562    syscall_init
1563 */
1564 void syscall_init(void)
1565 {
1566     /* Nothing yet */
1567 }