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