Statistics
| Branch: | Revision:

root / darwin-user / syscall.c @ 7ed40acf

History | View | Annotate | Download (47 kB)

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 "qemu.h"
57

    
58
//#define DEBUG_SYSCALL
59

    
60
#ifdef DEBUG_SYSCALL
61
# define DEBUG_FORCE_ENABLE_LOCAL() int __DEBUG_qemu_user_force_enable = 1
62
# define DEBUG_BEGIN_ENABLE  __DEBUG_qemu_user_force_enable = 1;
63
# define DEBUG_END_ENABLE  __DEBUG_qemu_user_force_enable = 0;
64

    
65
# define DEBUG_DISABLE_ALL() static int __DEBUG_qemu_user_force_enable = 0
66
# define DEBUG_ENABLE_ALL()  static int __DEBUG_qemu_user_force_enable = 1
67
    DEBUG_ENABLE_ALL();
68

    
69
# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); \
70
                           if(__DEBUG_qemu_user_force_enable) fprintf(stderr, __VA_ARGS__); \
71
                         } while(0)
72
#else
73
# define DEBUG_FORCE_ENABLE_LOCAL()
74
# define DEBUG_BEGIN_ENABLE
75
# define DEBUG_END_ENABLE
76

    
77
# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); } while(0)
78
#endif
79

    
80
enum {
81
    bswap_out = 0,
82
    bswap_in = 1
83
};
84

    
85
extern const char *interp_prefix;
86

    
87
static inline long get_errno(long ret)
88
{
89
    if (ret == -1)
90
        return -errno;
91
    else
92
        return ret;
93
}
94

    
95
static inline int is_error(long ret)
96
{
97
    return (unsigned long)ret >= (unsigned long)(-4096);
98
}
99

    
100
/* ------------------------------------------------------------
101
   Mach syscall handling
102
*/
103

    
104
void static inline print_description_msg_header(mach_msg_header_t *hdr)
105
{
106
    char *name = NULL;
107
    int i;
108
    struct { int number; char *name; } msg_name[] =
109
    {
110
        /* see http://fxr.watson.org/fxr/source/compat/mach/mach_namemap.c?v=NETBSD */
111
        { 200,      "host_info" },
112
        { 202,      "host_page_size" },
113
        { 206,      "host_get_clock_service" },
114
        { 206,      "host_get_clock_service" },
115
        { 206,      "host_get_clock_service" },
116
        { 306,      "host_get_clock_service" },
117
        { 3204,     "mach_port_allocate" },
118
        { 3206,     "mach_port_deallocate" },
119
        { 3404,     "mach_ports_lookup" },
120
        { 3409,     "mach_task_get_special_port" },
121
        { 3414,     "mach_task_get_exception_ports" },
122
        { 3418,     "mach_semaphore_create" },
123
        { 3504,     "mach_semaphore_create" },
124
        { 3509,     "mach_semaphore_create" },
125
        { 3518,     "semaphore_create" },
126
        { 3616,     "thread_policy" },
127
        { 3801,     "vm_allocate" },
128
        { 3802,     "vm_deallocate" },
129
        { 3802,     "vm_deallocate" },
130
        { 3803,     "vm_protect" },
131
        { 3812,     "vm_map" },
132
        { 4241776,  "lu_message_send_id" },  /* lookupd */
133
        { 4241876,  "lu_message_reply_id" }, /* lookupd */
134
    };
135

    
136
    for(i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
137
        if(msg_name[i].number == hdr->msgh_id)
138
        {
139
            name = msg_name[i].name;
140
            break;
141
        }
142
    }
143
    if(!name)
144
        DPRINTF("unknown mach msg %d 0x%x\n", hdr->msgh_id, hdr->msgh_id);
145
    else
146
        DPRINTF("%s\n", name);
147
#if 0
148
    DPRINTF("Bits: %8x\n", hdr->msgh_bits);
149
    DPRINTF("Size: %8x\n", hdr->msgh_size);
150
    DPRINTF("Rmte: %8x\n", hdr->msgh_remote_port);
151
    DPRINTF("Locl: %8x\n", hdr->msgh_local_port);
152
    DPRINTF("Rsrv: %8x\n", hdr->msgh_reserved);
153

154
    DPRINTF("Id  : %8x\n", hdr->msgh_id);
155

156
    NDR_record_t *ndr = (NDR_record_t *)(hdr + 1);
157
    DPRINTF("hdr = %p, sizeof(hdr) = %x, NDR = %p\n", hdr, (unsigned int)sizeof(mach_msg_header_t), ndr);
158
    DPRINTF("%d %d %d %d %d %d %d %d\n",
159
           ndr->mig_vers, ndr->if_vers, ndr->reserved1, ndr->mig_encoding,
160
           ndr->int_rep, ndr->char_rep, ndr->float_rep, ndr->reserved2);
161
#endif
162
}
163

    
164
static inline void print_mach_msg_return(mach_msg_return_t ret)
165
{
166
    int i, found = 0;
167
#define MACH_MSG_RET(msg) { msg, #msg }
168
    struct { int code; char *name; } msg_name[] =
169
    {
170
        /* ref: http://darwinsource.opendarwin.org/10.4.2/xnu-792.2.4/osfmk/man/mach_msg.html */
171
        /* send message */
172
        MACH_MSG_RET(MACH_SEND_MSG_TOO_SMALL),
173
        MACH_MSG_RET(MACH_SEND_NO_BUFFER),
174
        MACH_MSG_RET(MACH_SEND_INVALID_DATA),
175
        MACH_MSG_RET(MACH_SEND_INVALID_HEADER),
176
        MACH_MSG_RET(MACH_SEND_INVALID_DEST),
177
        MACH_MSG_RET(MACH_SEND_INVALID_NOTIFY),
178
        MACH_MSG_RET(MACH_SEND_INVALID_REPLY),
179
        MACH_MSG_RET(MACH_SEND_INVALID_TRAILER),
180
        MACH_MSG_RET(MACH_SEND_INVALID_MEMORY),
181
        MACH_MSG_RET(MACH_SEND_INVALID_RIGHT),
182
        MACH_MSG_RET(MACH_SEND_INVALID_TYPE),
183
        MACH_MSG_RET(MACH_SEND_INTERRUPTED),
184
        MACH_MSG_RET(MACH_SEND_TIMED_OUT),
185

    
186
        MACH_MSG_RET(MACH_RCV_BODY_ERROR),
187
        MACH_MSG_RET(MACH_RCV_HEADER_ERROR),
188

    
189
        MACH_MSG_RET(MACH_RCV_IN_SET),
190
        MACH_MSG_RET(MACH_RCV_INTERRUPTED),
191

    
192
        MACH_MSG_RET(MACH_RCV_INVALID_DATA),
193
        MACH_MSG_RET(MACH_RCV_INVALID_NAME),
194
        MACH_MSG_RET(MACH_RCV_INVALID_NOTIFY),
195
        MACH_MSG_RET(MACH_RCV_INVALID_TRAILER),
196
        MACH_MSG_RET(MACH_RCV_INVALID_TYPE),
197

    
198
        MACH_MSG_RET(MACH_RCV_PORT_CHANGED),
199
        MACH_MSG_RET(MACH_RCV_PORT_DIED),
200

    
201
        MACH_MSG_RET(MACH_RCV_SCATTER_SMALL),
202
        MACH_MSG_RET(MACH_RCV_TIMED_OUT),
203
        MACH_MSG_RET(MACH_RCV_TOO_LARGE)
204
    };
205
#undef MACH_MSG_RET
206

    
207
    if( ret == MACH_MSG_SUCCESS)
208
        DPRINTF("MACH_MSG_SUCCESS\n");
209
    else
210
    {
211
        for( i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
212
            if(msg_name[i].code == ret) {
213
                DPRINTF("%s\n", msg_name[i].name);
214
                found = 1;
215
                break;
216
            }
217
        }
218
        if(!found)
219
            qerror("unknow mach message ret code %d\n", ret);
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

    
243
    if(bswap == bswap_in)
244
        tswap32s(&complex_msg->body.msgh_descriptor_count);
245

    
246
    DPRINTF("body.msgh_descriptor_count %d\n", complex_msg->body.msgh_descriptor_count);
247

    
248
    for(i = 0; i < complex_msg->body.msgh_descriptor_count; i++) {
249
        switch(descr->type)
250
        {
251
            case MACH_MSG_PORT_DESCRIPTOR:
252
                tswap32s(&descr->name);
253
                descr++;
254
                break;
255
            case MACH_MSG_OOL_DESCRIPTOR:
256
            {
257
                mach_msg_ool_descriptor_t *ool = (void *)descr;
258
                tswap32s((uint32_t *)&ool->address);
259
                tswap32s(&ool->size);
260

    
261
                descr = (mach_msg_port_descriptor_t *)(ool+1);
262
                break;
263
            }
264
            case MACH_MSG_OOL_PORTS_DESCRIPTOR:
265
            {
266
                mach_msg_ool_ports_descriptor_t *ool_ports = (void *)descr;
267
                mach_port_name_t * port_names;
268

    
269
                if(bswap == bswap_in)
270
                {
271
                    tswap32s((uint32_t *)&ool_ports->address);
272
                    tswap32s(&ool_ports->count);
273
                }
274

    
275
                port_names = ool_ports->address;
276

    
277
                for(j = 0; j < ool_ports->count; j++)
278
                    tswap32s(&port_names[j]);
279

    
280
                if(bswap == bswap_out)
281
                {
282
                    tswap32s((uint32_t *)&ool_ports->address);
283
                    tswap32s(&ool_ports->count);
284
                }
285

    
286
                descr = (mach_msg_port_descriptor_t *)(ool_ports+1);
287
                break;
288
            }
289
            default: qerror("unknow mach msg descriptor type %x\n", descr->type);
290
        }
291
    }
292
    if(bswap == bswap_out)
293
        tswap32s(&complex_msg->body.msgh_descriptor_count);
294
}
295

    
296
static inline void swap_mach_msg(mach_msg_header_t *hdr, int bswap)
297
{
298
    if (bswap == bswap_out && hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
299
        swap_mach_msg_body((struct complex_msg *)hdr, bswap);
300

    
301
    swap_mach_msg_header(hdr);
302

    
303
    if (bswap == bswap_in && hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
304
        swap_mach_msg_body((struct complex_msg *)hdr, bswap);
305
}
306

    
307
static inline uint32_t target_mach_msg_trap(
308
        mach_msg_header_t *hdr, uint32_t options, uint32_t send_size,
309
        uint32_t rcv_size, uint32_t rcv_name, uint32_t time_out, uint32_t notify)
310
{
311
    extern int mach_msg_trap(mach_msg_header_t *, mach_msg_option_t,
312
          mach_msg_size_t, mach_msg_size_t, mach_port_t,
313
          mach_msg_timeout_t, mach_port_t);
314
    mach_msg_audit_trailer_t *trailer;
315
    mach_msg_id_t msg_id;
316
    uint32_t ret = 0;
317
    int i;
318

    
319
    swap_mach_msg(hdr, bswap_in);
320

    
321
    msg_id = hdr->msgh_id;
322

    
323
    print_description_msg_header(hdr);
324

    
325
    ret = mach_msg_trap(hdr, options, send_size, rcv_size, rcv_name, time_out, notify);
326

    
327
    print_mach_msg_return(ret);
328

    
329
    if( (options & MACH_RCV_MSG) && (REQUESTED_TRAILER_SIZE(options) > 0) )
330
    {
331
        /* XXX: the kernel always return the full trailer with MACH_SEND_MSG, so we should
332
                probably always bswap it  */
333
        /* warning: according to Mac OS X Internals (the book) msg_size might be expressed in
334
                    natural_t units but according to xnu/osfmk/mach/message.h: "The size of
335
                    the message must be specified in bytes" */
336
        trailer = (mach_msg_audit_trailer_t *)((uint8_t *)hdr + hdr->msgh_size);
337
        /* XXX: Should probably do that based on the option asked by the sender, but dealing
338
        with kernel answer seems more sound */
339
        switch(trailer->msgh_trailer_size)
340
        {
341
            case sizeof(mach_msg_audit_trailer_t):
342
                for(i = 0; i < 8; i++)
343
                    tswap32s(&trailer->msgh_audit.val[i]);
344
                /* Fall in mach_msg_security_trailer_t case */
345
            case sizeof(mach_msg_security_trailer_t):
346
                tswap32s(&trailer->msgh_sender.val[0]);
347
                tswap32s(&trailer->msgh_sender.val[1]);
348
                /* Fall in mach_msg_seqno_trailer_t case */
349
            case sizeof(mach_msg_seqno_trailer_t):
350
                tswap32s(&trailer->msgh_seqno);
351
                /* Fall in mach_msg_trailer_t case */
352
            case sizeof(mach_msg_trailer_t):
353
                tswap32s(&trailer->msgh_trailer_type);
354
                tswap32s(&trailer->msgh_trailer_size);
355
                break;
356
            case 0:
357
                /* Safer not to byteswap, but probably wrong */
358
                break;
359
            default:
360
                qerror("unknow trailer type given its size %d\n", trailer->msgh_trailer_size);
361
                break;
362
        }
363
    }
364

    
365
    /* Special message handling */
366
    switch (msg_id) {
367
        case 200: /* host_info */
368
        {
369
            mig_reply_error_t *err = (mig_reply_error_t *)hdr;
370
            struct {
371
                uint32_t unknow1;
372
                uint32_t max_cpus;
373
                uint32_t avail_cpus;
374
                uint32_t memory_size;
375
                uint32_t cpu_type;
376
                uint32_t cpu_subtype;
377
            } *data = (void *)(err+1);
378

    
379
            DPRINTF("maxcpu = 0x%x\n",   data->max_cpus);
380
            DPRINTF("numcpu = 0x%x\n",   data->avail_cpus);
381
            DPRINTF("memsize = 0x%x\n",  data->memory_size);
382

    
383
#if defined(TARGET_I386)
384
            data->cpu_type = CPU_TYPE_I386;
385
            DPRINTF("cpu_type changed to 0x%x(i386)\n", data->cpu_type);
386
            data->cpu_subtype = CPU_SUBTYPE_PENT;
387
            DPRINTF("cpu_subtype changed to 0x%x(i386_pent)\n", data->cpu_subtype);
388
#elif defined(TARGET_PPC)
389
            data->cpu_type = CPU_TYPE_POWERPC;
390
            DPRINTF("cpu_type changed to 0x%x(ppc)\n", data->cpu_type);
391
            data->cpu_subtype = CPU_SUBTYPE_POWERPC_750;
392
            DPRINTF("cpu_subtype changed to 0x%x(ppc_all)\n", data->cpu_subtype);
393
#else
394
# error target not supported
395
#endif
396
            break;
397
        }
398
        case 202: /* host_page_size */
399
        {
400
            mig_reply_error_t *err = (mig_reply_error_t *)hdr;
401
            uint32_t *pagesize = (uint32_t *)(err+1);
402

    
403
            DPRINTF("pagesize = %d\n", *pagesize);
404
            break;
405
        }
406
        default: break;
407
    }
408

    
409
    swap_mach_msg(hdr, bswap_out);
410

    
411
    return ret;
412
}
413

    
414
long do_mach_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
415
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
416
                uint32_t arg8)
417
{
418
    extern uint32_t mach_reply_port();
419

    
420
    long ret = 0;
421

    
422
    arg1 = tswap32(arg1);
423
    arg2 = tswap32(arg2);
424
    arg3 = tswap32(arg3);
425
    arg4 = tswap32(arg4);
426
    arg5 = tswap32(arg5);
427
    arg6 = tswap32(arg6);
428
    arg7 = tswap32(arg7);
429
    arg8 = tswap32(arg8);
430

    
431
    DPRINTF("mach syscall %d : " , num);
432

    
433
    switch(num) {
434
    /* see xnu/osfmk/mach/syscall_sw.h */
435
    case -26:
436
        DPRINTF("mach_reply_port()\n");
437
        ret = mach_reply_port();
438
        break;
439
    case -27:
440
        DPRINTF("mach_thread_self()\n");
441
        ret = mach_thread_self();
442
        break;
443
    case -28:
444
        DPRINTF("mach_task_self()\n");
445
        ret = mach_task_self();
446
        break;
447
    case -29:
448
        DPRINTF("mach_host_self()\n");
449
        ret = mach_host_self();
450
        break;
451
    case -31:
452
        DPRINTF("mach_msg_trap(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
453
                arg1, arg2, arg3, arg4, arg5, arg6, arg7);
454

    
455
        ret = target_mach_msg_trap((mach_msg_header_t *)arg1, arg2, arg3, arg4, arg5, arg6, arg7);
456

    
457
        break;
458
    case -36:
459
        DPRINTF("semaphore_wait_trap(0x%x)\n", arg1);
460
        extern int semaphore_wait_trap(int); // XXX: is there any header for that?
461
        ret = semaphore_wait_trap(arg1);
462
        break;
463
    case -43:
464
        DPRINTF("map_fd(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
465
                arg1, arg2, arg3, arg4, arg5);
466
        ret = map_fd(arg1, arg2, (void*)arg3, arg4, arg5);
467
        tswap32s((uint32_t*)arg3);
468
        break;
469
    case -89:
470
        DPRINTF("mach_timebase_info(0x%x)\n", arg1);
471
        struct mach_timebase_info info;
472
        ret = mach_timebase_info(&info);
473
        if(!is_error(ret))
474
        {
475
            struct mach_timebase_info *outInfo = (void*)arg1;
476
            outInfo->numer = tswap32(info.numer);
477
            outInfo->denom = tswap32(info.denom);
478
        }
479
        break;
480
    case -90:
481
        DPRINTF("mach_wait_until()\n");
482
        extern int mach_wait_until(uint64_t); // XXX: is there any header for that?
483
        ret = mach_wait_until(((uint64_t)arg2<<32) | (uint64_t)arg1);
484
        break;
485
    case -91:
486
        DPRINTF("mk_timer_create()\n");
487
        extern int mk_timer_create(); // XXX: is there any header for that?
488
        ret = mk_timer_create();
489
        break;
490
    case -92:
491
        DPRINTF("mk_timer_destroy()\n");
492
        extern int mk_timer_destroy(int); // XXX: is there any header for that?
493
        ret = mk_timer_destroy(arg1);
494
        break;
495
    case -93:
496
        DPRINTF("mk_timer_create()\n");
497
        extern int mk_timer_arm(int, uint64_t); // XXX: is there any header for that?
498
        ret = mk_timer_arm(arg1, ((uint64_t)arg3<<32) | (uint64_t)arg2);
499
        break;
500
    case -94:
501
        DPRINTF("mk_timer_cancel()\n");
502
        extern int mk_timer_cancel(int, uint64_t *); // XXX: is there any header for that?
503
        ret = mk_timer_cancel(arg1, (uint64_t *)arg2);
504
        if((!is_error(ret)) && arg2)
505
            tswap64s((uint64_t *)arg2);
506
        break;
507
    default:
508
        gemu_log("qemu: Unsupported mach syscall: %d(0x%x)\n", num, num);
509
        gdb_handlesig (cpu_env, SIGTRAP);
510
        exit(0);
511
        break;
512
    }
513
    return ret;
514
}
515

    
516
/* ------------------------------------------------------------
517
   thread type syscall handling
518
*/
519
long do_thread_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
520
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
521
                uint32_t arg8)
522
{
523
    extern uint32_t cthread_set_self(uint32_t);
524
    extern uint32_t processor_facilities_used();
525
    long ret = 0;
526

    
527
    arg1 = tswap32(arg1);
528
    arg2 = tswap32(arg2);
529
    arg3 = tswap32(arg3);
530
    arg4 = tswap32(arg4);
531
    arg5 = tswap32(arg5);
532
    arg6 = tswap32(arg6);
533
    arg7 = tswap32(arg7);
534
    arg8 = tswap32(arg8);
535

    
536
    DPRINTF("thread syscall %d : " , num);
537

    
538
    switch(num) {
539
#ifdef TARGET_I386
540
    case 0x3:
541
#endif
542
    case 0x7FF1: /* cthread_set_self */
543
        DPRINTF("cthread_set_self(0x%x)\n", (unsigned int)arg1);
544
        ret = cthread_set_self(arg1);
545
#ifdef TARGET_I386
546
        /* we need to update the LDT with the address of the thread */
547
        write_dt((void *)(((CPUX86State *) cpu_env)->ldt.base + (4 * sizeof(uint64_t))), arg1, 1,
548
                 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
549
                 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
550
        /* New i386 convention, %gs should be set to our this LDT entry */
551
        cpu_x86_load_seg(cpu_env, R_GS, 0x27);
552
        /* Old i386 convention, the kernel returns the selector for the cthread (pre-10.4.8?)*/
553
        ret = 0x27;
554
#endif
555
        break;
556
    case 0x7FF2: /* Called the super-fast pthread_self handler by the apple guys */
557
        DPRINTF("pthread_self()\n");
558
        ret = (uint32_t)pthread_self();
559
        break;
560
    case 0x7FF3:
561
        DPRINTF("processor_facilities_used()\n");
562
#ifdef __i386__
563
        qerror("processor_facilities_used: not implemented!\n");
564
#else
565
        ret = (uint32_t)processor_facilities_used();
566
#endif
567
        break;
568
    default:
569
        gemu_log("qemu: Unsupported thread syscall: %d(0x%x)\n", num, num);
570
        gdb_handlesig (cpu_env, SIGTRAP);
571
        exit(0);
572
        break;
573
    }
574
    return ret;
575
}
576

    
577
/* ------------------------------------------------------------
578
   ioctl handling
579
*/
580
static inline void byteswap_termios(struct termios *t)
581
{
582
    tswap32s((uint32_t*)&t->c_iflag);
583
    tswap32s((uint32_t*)&t->c_oflag);
584
    tswap32s((uint32_t*)&t->c_cflag);
585
    tswap32s((uint32_t*)&t->c_lflag);
586
    /* 20 (char) bytes then */
587
    tswap32s((uint32_t*)&t->c_ispeed);
588
    tswap32s((uint32_t*)&t->c_ospeed);
589
}
590

    
591
static inline void byteswap_winsize(struct winsize *w)
592
{
593
    tswap16s(&w->ws_row);
594
    tswap16s(&w->ws_col);
595
    tswap16s(&w->ws_xpixel);
596
    tswap16s(&w->ws_ypixel);
597
}
598

    
599
#define STRUCT(name, list...) STRUCT_ ## name,
600
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
601
enum {
602
#include "ioctls_types.h"
603
};
604
#undef STRUCT
605
#undef STRUCT_SPECIAL
606

    
607
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
608
#define STRUCT_SPECIAL(name)
609
#include "ioctls_types.h"
610
#undef STRUCT
611
#undef STRUCT_SPECIAL
612

    
613
typedef struct IOCTLEntry {
614
    unsigned int target_cmd;
615
    unsigned int host_cmd;
616
    const char *name;
617
    int access;
618
    const argtype arg_type[5];
619
} IOCTLEntry;
620

    
621
#define IOC_R 0x0001
622
#define IOC_W 0x0002
623
#define IOC_RW (IOC_R | IOC_W)
624

    
625
#define MAX_STRUCT_SIZE 4096
626

    
627
IOCTLEntry ioctl_entries[] = {
628
#define IOCTL(cmd, access, types...) \
629
    { cmd, cmd, #cmd, access, { types } },
630
#include "ioctls.h"
631
    { 0, 0, },
632
};
633

    
634
/* ??? Implement proper locking for ioctls.  */
635
static long do_ioctl(long fd, long cmd, long arg)
636
{
637
    const IOCTLEntry *ie;
638
    const argtype *arg_type;
639
    int ret;
640
    uint8_t buf_temp[MAX_STRUCT_SIZE];
641
    int target_size;
642
    void *argptr;
643

    
644
    ie = ioctl_entries;
645
    for(;;) {
646
        if (ie->target_cmd == 0) {
647
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
648
            return -ENOSYS;
649
        }
650
        if (ie->target_cmd == cmd)
651
            break;
652
        ie++;
653
    }
654
    arg_type = ie->arg_type;
655
#if defined(DEBUG)
656
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
657
#endif
658
    switch(arg_type[0]) {
659
    case TYPE_NULL:
660
        /* no argument */
661
        ret = get_errno(ioctl(fd, ie->host_cmd));
662
        break;
663
    case TYPE_PTRVOID:
664
    case TYPE_INT:
665
        /* int argment */
666
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
667
        break;
668
    case TYPE_PTR:
669
        arg_type++;
670
        target_size = thunk_type_size(arg_type, 0);
671
        switch(ie->access) {
672
        case IOC_R:
673
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
674
            if (!is_error(ret)) {
675
                argptr = lock_user(arg, target_size, 0);
676
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
677
                unlock_user(argptr, arg, target_size);
678
            }
679
            break;
680
        case IOC_W:
681
            argptr = lock_user(arg, target_size, 1);
682
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
683
            unlock_user(argptr, arg, 0);
684
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
685
            break;
686
        default:
687
        case IOC_RW:
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
            if (!is_error(ret)) {
693
                argptr = lock_user(arg, target_size, 0);
694
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
695
                unlock_user(argptr, arg, target_size);
696
            }
697
            break;
698
        }
699
        break;
700
    default:
701
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
702
        ret = -ENOSYS;
703
        break;
704
    }
705
    return ret;
706
}
707

    
708
/* ------------------------------------------------------------
709
   Unix syscall handling
710
*/
711

    
712
static inline void byteswap_attrlist(struct attrlist *a)
713
{
714
    tswap16s(&a->bitmapcount);
715
    tswap16s(&a->reserved);
716
    tswap32s(&a->commonattr);
717
    tswap32s(&a->volattr);
718
    tswap32s(&a->dirattr);
719
    tswap32s(&a->fileattr);
720
    tswap32s(&a->forkattr);
721
}
722

    
723
struct attrbuf_header {
724
    unsigned long length;
725
};
726

    
727
static inline void byteswap_attrbuf(struct attrbuf_header *attrbuf, struct attrlist *attrlist)
728
{
729
    DPRINTF("attrBuf.lenght %lx\n", attrbuf->length);
730
}
731

    
732
static inline void byteswap_statfs(struct statfs *s)
733
{
734
    tswap16s((uint16_t*)&s->f_otype);
735
    tswap16s((uint16_t*)&s->f_oflags);
736
    tswap32s((uint32_t*)&s->f_bsize);
737
    tswap32s((uint32_t*)&s->f_iosize);
738
    tswap32s((uint32_t*)&s->f_blocks);
739
    tswap32s((uint32_t*)&s->f_bfree);
740
    tswap32s((uint32_t*)&s->f_bavail);
741
    tswap32s((uint32_t*)&s->f_files);
742
    tswap32s((uint32_t*)&s->f_ffree);
743
    tswap32s((uint32_t*)&s->f_fsid.val[0]);
744
    tswap32s((uint32_t*)&s->f_fsid.val[1]);
745
    tswap16s((uint16_t*)&s->f_reserved1);
746
    tswap16s((uint16_t*)&s->f_type);
747
    tswap32s((uint32_t*)&s->f_flags);
748
}
749

    
750
static inline void byteswap_stat(struct stat *s)
751
{
752
    tswap32s((uint32_t*)&s->st_dev);
753
    tswap32s(&s->st_ino);
754
    tswap16s(&s->st_mode);
755
    tswap16s(&s->st_nlink);
756
    tswap32s(&s->st_uid);
757
    tswap32s(&s->st_gid);
758
    tswap32s((uint32_t*)&s->st_rdev);
759
    tswap32s((uint32_t*)&s->st_atimespec.tv_sec);
760
    tswap32s((uint32_t*)&s->st_atimespec.tv_nsec);
761
    tswap32s((uint32_t*)&s->st_mtimespec.tv_sec);
762
    tswap32s((uint32_t*)&s->st_mtimespec.tv_nsec);
763
    tswap32s((uint32_t*)&s->st_ctimespec.tv_sec);
764
    tswap32s((uint32_t*)&s->st_ctimespec.tv_nsec);
765
    tswap64s((uint64_t*)&s->st_size);
766
    tswap64s((uint64_t*)&s->st_blocks);
767
    tswap32s((uint32_t*)&s->st_blksize);
768
    tswap32s(&s->st_flags);
769
    tswap32s(&s->st_gen);
770
}
771

    
772
static inline void byteswap_dirents(struct dirent *d, int bytes)
773
{
774
    char *b;
775
    for( b = (char*)d; (int)b < (int)d+bytes; )
776
    {
777
        unsigned short s = ((struct dirent *)b)->d_reclen;
778
        tswap32s(&((struct dirent *)b)->d_ino);
779
        tswap16s(&((struct dirent *)b)->d_reclen);
780
        if(s<=0)
781
            break;
782
        b += s;
783
    }
784
}
785

    
786
static inline void byteswap_iovec(struct iovec *v, int n)
787
{
788
    int i;
789
    for(i = 0; i < n; i++)
790
    {
791
        tswap32s((uint32_t*)&v[i].iov_base);
792
        tswap32s((uint32_t*)&v[i].iov_len);
793
    }
794
}
795

    
796
static inline void byteswap_timeval(struct timeval *t)
797
{
798
    tswap32s((uint32_t*)&t->tv_sec);
799
    tswap32s((uint32_t*)&t->tv_usec);
800
}
801

    
802
long do_unix_syscall_indirect(void *cpu_env, int num);
803
long do_sync();
804
long do_exit(uint32_t arg1);
805
long do_getlogin(char *out, uint32_t size);
806
long do_open(char * arg1, uint32_t arg2, uint32_t arg3);
807
long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3);
808
long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3);
809
long do_execve(char* arg1, char ** arg2, char ** arg3);
810
long do_getgroups(uint32_t arg1, gid_t * arg2);
811
long do_gettimeofday(struct timeval * arg1, void * arg2);
812
long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
813
long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
814
long do_utimes(char * arg1, struct timeval * arg2);
815
long do_futimes(uint32_t arg1, struct timeval * arg2);
816
long do_statfs(char * arg1, struct statfs * arg2);
817
long do_fstatfs(uint32_t arg1, struct statfs * arg2);
818
long do_stat(char * arg1, struct stat * arg2);
819
long do_fstat(uint32_t arg1, struct stat * arg2);
820
long do_lstat(char * arg1, struct stat * arg2);
821
long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4);
822
long do_lseek(void *cpu_env, int num);
823
long do___sysctl(int * name, uint32_t namelen, void * oldp, size_t * oldlenp, void * newp, size_t newlen  /* ignored */);
824
long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5);
825
long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8);
826
long do_fcntl(int fd, int cmd, int arg);
827

    
828
long no_syscall(void *cpu_env, int num);
829

    
830
long do_pread(uint32_t arg1, void * arg2, size_t arg3, off_t arg4)
831
{
832
    DPRINTF("0x%x, %p, 0x%lx, 0x%llx\n", arg1, arg2, arg3, arg4);
833
    long ret = pread(arg1, arg2, arg3, arg4);
834
    return ret;
835
}
836

    
837
long do_read(int d, void *buf, size_t nbytes)
838
{
839
    DPRINTF("0x%x, %p, 0x%lx\n", d, buf, nbytes);
840
    long ret = get_errno(read(d, buf, nbytes));
841
    if(!is_error(ret))
842
        DPRINTF("%x\n", *(uint32_t*)buf);
843
    return ret;
844
}
845

    
846
long unimpl_unix_syscall(void *cpu_env, int num);
847

    
848
typedef long (*syscall_function_t)(void *cpu_env, int num);
849

    
850

    
851
/* define a table that will handle the syscall number->function association */
852
#define VOID    void
853
#define INT     (uint32_t)get_int_arg(&i, cpu_env)
854
#define INT64   (uint64_t)get_int64_arg(&i, cpu_env)
855
#define UINT    (unsigned int)INT
856
#define PTR     (void*)INT
857

    
858
#define SIZE    INT
859
#define OFFSET  INT64
860

    
861
#define WRAPPER_CALL_DIRECT_0(function, args) long __qemu_##function(void *cpu_env) {  return (long)function(); }
862
#define WRAPPER_CALL_DIRECT_1(function, _arg1) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1;  return (long)function(arg1); }
863
#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); }
864
#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); }
865
#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); }
866
#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); }
867
#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); }
868
#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); }
869
#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); }
870
#define WRAPPER_CALL_DIRECT(function, nargs, args...) WRAPPER_CALL_DIRECT_##nargs(function, args)
871
#define WRAPPER_CALL_NOERRNO(function, nargs, args...)  WRAPPER_CALL_DIRECT(function, nargs, args)
872
#define WRAPPER_CALL_INDIRECT(function, nargs, args...)
873
#define ENTRY(name, number, function, nargs, call_type, args...)  WRAPPER_##call_type(function, nargs, args)
874

    
875
#include "syscalls.h"
876

    
877
#undef ENTRY
878
#undef WRAPPER_CALL_DIRECT
879
#undef WRAPPER_CALL_NOERRNO
880
#undef WRAPPER_CALL_INDIRECT
881
#undef OFFSET
882
#undef SIZE
883
#undef INT
884
#undef PTR
885
#undef INT64
886

    
887
#define _ENTRY(name, number, function, nargs, call_type) [number] = {\
888
        name, \
889
        number, \
890
        (syscall_function_t)function, \
891
        nargs, \
892
        call_type  \
893
        },
894

    
895
#define ENTRY_CALL_DIRECT(name, number, function, nargs, call_type)  _ENTRY(name, number, __qemu_##function, nargs, call_type)
896
#define ENTRY_CALL_NOERRNO(name, number, function, nargs, call_type) ENTRY_CALL_DIRECT(name, number, function, nargs, call_type)
897
#define ENTRY_CALL_INDIRECT(name, number, function, nargs, call_type) _ENTRY(name, number, function, nargs, call_type)
898
#define ENTRY(name, number, function, nargs, call_type, args...) ENTRY_##call_type(name, number, function, nargs, call_type)
899

    
900
#define CALL_DIRECT 1
901
#define CALL_INDIRECT 2
902
#define CALL_NOERRNO  (CALL_DIRECT | 4 /* = 5 */)
903

    
904
struct unix_syscall {
905
    char * name;
906
    int number;
907
    syscall_function_t function;
908
    int nargs;
909
    int call_type;
910
} unix_syscall_table[SYS_MAXSYSCALL] = {
911
#include "syscalls.h"
912
};
913

    
914
#undef ENTRY
915
#undef _ENTRY
916
#undef ENTRY_CALL_DIRECT
917
#undef ENTRY_CALL_INDIRECT
918
#undef ENTRY_CALL_NOERRNO
919

    
920
/* Actual syscalls implementation */
921

    
922
long do_unix_syscall_indirect(void *cpu_env, int num)
923
{
924
    long ret;
925
    int new_num;
926
    int i = 0;
927

    
928
    new_num = get_int_arg(&i, cpu_env);
929
#ifdef TARGET_I386
930
    ((CPUX86State*)cpu_env)->regs[R_ESP] += 4;
931
    /* XXX: not necessary */
932
    ((CPUX86State*)cpu_env)->regs[R_EAX] = new_num;
933
#elif TARGET_PPC
934
    {
935
        int i;
936
        uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
937
        for(i = 3; i < 11; i++)
938
            *regs[i] = *regs[i+1];
939
        /* XXX: not necessary */
940
        *regs[0] = new_num;
941
    }
942
#endif
943
    ret = do_unix_syscall(cpu_env, new_num);
944
#ifdef TARGET_I386
945
    ((CPUX86State*)cpu_env)->regs[R_ESP] -= 4;
946
    /* XXX: not necessary */
947
    ((CPUX86State*)cpu_env)->regs[R_EAX] = num;
948
#elif TARGET_PPC
949
    {
950
        int i;
951
        /* XXX: not really needed those regs are volatile accross calls */
952
        uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
953
        for(i = 11; i > 3; i--)
954
            *regs[i] = *regs[i-1];
955
        regs[3] = new_num;
956
        *regs[0] = num;
957
    }
958
#endif
959
    return ret;
960
}
961

    
962
long do_exit(uint32_t arg1)
963
{
964
    exit(arg1);
965
    /* not reached */
966
    return -1;
967
}
968

    
969
long do_sync()
970
{
971
    sync();
972
    return 0;
973
}
974

    
975
long do_getlogin(char *out, uint32_t size)
976
{
977
    char *login = getlogin();
978
    if(!login)
979
        return -1;
980
    memcpy(out, login, size);
981
    return 0;
982
}
983
long do_open(char * arg1, uint32_t arg2, uint32_t arg3)
984
{
985
    /* XXX: don't let the %s stay in there */
986
    DPRINTF("open(%s, 0x%x, 0x%x)\n", arg1, arg2, arg3);
987
    return get_errno(open(arg1, arg2, arg3));
988
}
989

    
990
long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3)
991
{
992
    long ret;
993
    DPRINTF("getfsstat(%p, 0x%x, 0x%x)\n", arg1, arg2, arg3);
994
    ret = get_errno(getfsstat(arg1, arg2, arg3));
995
    if((!is_error(ret)) && arg1)
996
        byteswap_statfs(arg1);
997
    return ret;
998
}
999

    
1000
long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3)
1001
{
1002
    long ret;
1003
    DPRINTF("sigprocmask(%d, %p, %p)\n", arg1, arg2, arg3);
1004
    gemu_log("XXX: sigprocmask not tested (%d, %p, %p)\n", arg1, arg2, arg3);
1005
    if(arg2)
1006
        tswap32s(arg2);
1007
    ret = get_errno(sigprocmask(arg1, (void *)arg2, (void *)arg3));
1008
    if((!is_error(ret)) && arg3)
1009
        tswap32s(arg3);
1010
    if(arg2)
1011
        tswap32s(arg2);
1012
    return ret;
1013
}
1014

    
1015
long do_execve(char* arg1, char ** arg2, char ** arg3)
1016
{
1017
    long ret;
1018
    char **argv = arg2;
1019
    char **envp = arg3;
1020
    int argc;
1021
    int envc;
1022

    
1023
    /* XXX: don't let the %s stay in here */
1024
    DPRINTF("execve(%s, %p, %p)\n", arg1, arg2, arg3);
1025

    
1026
    for(argc = 0; argv[argc]; argc++);
1027
    for(envc = 0; envp[envc]; envc++);
1028

    
1029
    argv = (char**)malloc(sizeof(char*)*argc);
1030
    envp = (char**)malloc(sizeof(char*)*envc);
1031

    
1032
    for(; argc >= 0; argc--)
1033
        argv[argc] = (char*)tswap32((uint32_t)(arg2)[argc]);
1034

    
1035
    for(; envc >= 0; envc--)
1036
        envp[envc] = (char*)tswap32((uint32_t)(arg3)[envc]);
1037

    
1038
    ret = get_errno(execve(arg1, argv, envp));
1039
    free(argv);
1040
    free(envp);
1041
    return ret;
1042
}
1043

    
1044
long do_getgroups(uint32_t arg1, gid_t * arg2)
1045
{
1046
    long ret;
1047
    int i;
1048
    DPRINTF("getgroups(0x%x, %p)\n", arg1, arg2);
1049
    ret = get_errno(getgroups(arg1, arg2));
1050
    if(ret > 0)
1051
        for(i = 0; i < arg1; i++)
1052
            tswap32s(&arg2[i]);
1053
    return ret;
1054
}
1055

    
1056
long do_gettimeofday(struct timeval * arg1, void * arg2)
1057
{
1058
    long ret;
1059
    DPRINTF("gettimeofday(%p, %p)\n",
1060
            arg1, arg2);
1061
    ret = get_errno(gettimeofday(arg1, arg2));
1062
    if(!is_error(ret))
1063
    {
1064
        /* timezone no longer used according to the manpage, so don't bother with it */
1065
        byteswap_timeval(arg1);
1066
    }
1067
    return ret;
1068
}
1069

    
1070
long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1071
{
1072
    long ret;
1073
    DPRINTF("readv(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1074
    if(arg2)
1075
        byteswap_iovec(arg2, arg3);
1076
    ret = get_errno(readv(arg1, arg2, arg3));
1077
    if((!is_error(ret)) && arg2)
1078
        byteswap_iovec(arg2, arg3);
1079
    return ret;
1080
}
1081

    
1082
long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1083
{
1084
    long ret;
1085
    DPRINTF("writev(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1086
    if(arg2)
1087
        byteswap_iovec(arg2, arg3);
1088
    ret = get_errno(writev(arg1, arg2, arg3));
1089
    if((!is_error(ret)) && arg2)
1090
        byteswap_iovec(arg2, arg3);
1091
    return ret;
1092
}
1093

    
1094
long do_utimes(char * arg1, struct timeval * arg2)
1095
{
1096
    DPRINTF("utimes(%p, %p)\n", arg1, arg2);
1097
    if(arg2)
1098
    {
1099
        byteswap_timeval(arg2);
1100
        byteswap_timeval(arg2+1);
1101
    }
1102
    return get_errno(utimes(arg1, arg2));
1103
}
1104

    
1105
long do_futimes(uint32_t arg1, struct timeval * arg2)
1106
{
1107
    DPRINTF("futimes(0x%x, %p)\n", arg1, arg2);
1108
    if(arg2)
1109
    {
1110
        byteswap_timeval(arg2);
1111
        byteswap_timeval(arg2+1);
1112
    }
1113
    return get_errno(futimes(arg1, arg2));
1114
}
1115

    
1116
long do_statfs(char * arg1, struct statfs * arg2)
1117
{
1118
    long ret;
1119
    DPRINTF("statfs(%p, %p)\n", arg1, arg2);
1120
    ret = get_errno(statfs(arg1, arg2));
1121
    if(!is_error(ret))
1122
        byteswap_statfs(arg2);
1123
    return ret;
1124
}
1125

    
1126
long do_fstatfs(uint32_t arg1, struct statfs* arg2)
1127
{
1128
    long ret;
1129
    DPRINTF("fstatfs(0x%x, %p)\n",
1130
            arg1, arg2);
1131
    ret = get_errno(fstatfs(arg1, arg2));
1132
    if(!is_error(ret))
1133
        byteswap_statfs(arg2);
1134

    
1135
    return ret;
1136
}
1137

    
1138
long do_stat(char * arg1, struct stat * arg2)
1139
{
1140
    long ret;
1141
    /* XXX: don't let the %s stay in there */
1142
    DPRINTF("stat(%s, %p)\n", arg1, arg2);
1143
    ret = get_errno(stat(arg1, arg2));
1144
    if(!is_error(ret))
1145
        byteswap_stat(arg2);
1146
    return ret;
1147
}
1148

    
1149
long do_fstat(uint32_t arg1, struct stat * arg2)
1150
{
1151
    long ret;
1152
    DPRINTF("fstat(0x%x, %p)\n", arg1, arg2);
1153
    ret = get_errno(fstat(arg1, arg2));
1154
    if(!is_error(ret))
1155
        byteswap_stat(arg2);
1156
    return ret;
1157
}
1158

    
1159
long do_lstat(char * arg1, struct stat * arg2)
1160
{
1161
    long ret;
1162
    /* XXX: don't let the %s stay in there */
1163
    DPRINTF("lstat(%s, %p)\n", (const char *)arg1, arg2);
1164
    ret = get_errno(lstat(arg1, arg2));
1165
    if(!is_error(ret))
1166
        byteswap_stat(arg2);
1167
    return ret;
1168
}
1169

    
1170
long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4)
1171
{
1172
    long ret;
1173
    DPRINTF("getdirentries(0x%x, %p, 0x%x, %p)\n", arg1, arg2, arg3, arg4);
1174
    if(arg4)
1175
        tswap32s((uint32_t *)arg4);
1176
    ret = get_errno(getdirentries(arg1, arg2, arg3, arg4));
1177
    if(arg4)
1178
        tswap32s((uint32_t *)arg4);
1179
    if(!is_error(ret))
1180
        byteswap_dirents(arg2, ret);
1181
    return ret;
1182
}
1183

    
1184
long do_lseek(void *cpu_env, int num)
1185
{
1186
    long ret;
1187
    int i = 0;
1188
    uint32_t arg1 = get_int_arg(&i, cpu_env);
1189
    uint64_t offset = get_int64_arg(&i, cpu_env);
1190
    uint32_t arg3 = get_int_arg(&i, cpu_env);
1191
    uint64_t r = lseek(arg1, offset, arg3);
1192
#ifdef TARGET_I386
1193
    /* lowest word in eax, highest in edx */
1194
    ret = r & 0xffffffff; /* will be set to eax after do_unix_syscall exit */
1195
    ((CPUX86State *)cpu_env)->regs[R_EDX] = (uint32_t)((r >> 32) & 0xffffffff) ;
1196
#elif defined TARGET_PPC
1197
    ret = r & 0xffffffff; /* will be set to r3 after do_unix_syscall exit */
1198
    ((CPUPPCState *)cpu_env)->gpr[4] = (uint32_t)((r >> 32) & 0xffffffff) ;
1199
#else
1200
    qerror("64 bit ret value on your arch?");
1201
#endif
1202
    return get_errno(ret);
1203
}
1204

    
1205
void no_swap(void * oldp, int size)
1206
{
1207
}
1208

    
1209
void sysctl_tswap32s(void * oldp, int size)
1210
{
1211
    tswap32s(oldp);
1212
}
1213

    
1214
void bswap_oid(uint32_t * oldp, int size)
1215
{
1216
    int count = size / sizeof(int);
1217
    int i = 0;
1218
    do { tswap32s(oldp + i); } while (++i < count);
1219
}
1220

    
1221
void sysctl_usrstack(uint32_t * oldp, int size)
1222
{
1223
    DPRINTF("sysctl_usrstack: 0x%x\n", *oldp);
1224
    tswap32s(oldp);
1225
}
1226

    
1227
void sysctl_ncpu(uint32_t * ncpu, int size)
1228
{
1229
    *ncpu = 0x1;
1230
    DPRINTF("sysctl_ncpu: 0x%x\n", *ncpu);
1231
    tswap32s(ncpu);
1232
}
1233

    
1234
void sysctl_exec(char * exec, int size)
1235
{
1236
    DPRINTF("sysctl_exec: %s\n", exec);
1237
}
1238

    
1239
void sysctl_translate(char * exec, int size)
1240
{
1241
    DPRINTF("sysctl_translate: %s\n", exec);
1242
}
1243

    
1244
struct sysctl_dir {
1245
    int num;
1246
    const char * name;
1247
    void (*swap_func)(void *, int);
1248
    struct sysctl_dir *childs;
1249
};
1250

    
1251
#define ENTRYD(num, name, childs) { num, name, NULL, childs }
1252
#define ENTRYE(num, name, func)   { num, name, (void (*)(void *, int))func, NULL  }
1253
struct sysctl_dir sysctls_unspec[] = {
1254
    ENTRYE(3,  "oip", bswap_oid),
1255
    { 0, NULL, NULL, NULL }
1256
};
1257

    
1258
struct sysctl_dir sysctls_kern[] = {
1259
    ENTRYE(KERN_TRANSLATE,          "translate",    sysctl_translate), /* 44 */
1260
    ENTRYE(KERN_EXEC,               "exec",         sysctl_exec), /* 45 */
1261
    ENTRYE(KERN_USRSTACK32,          "KERN_USRSTACK32", sysctl_usrstack), /* 35 */
1262
    ENTRYE(KERN_SHREG_PRIVATIZABLE,  "KERN_SHREG_PRIVATIZABLE", sysctl_tswap32s), /* 54 */
1263
    { 0, NULL, NULL, NULL }
1264
};
1265

    
1266
struct sysctl_dir sysctls_hw[] = {
1267
    ENTRYE(HW_NCPU, "ncpud", sysctl_tswap32s),
1268
    ENTRYE(104, "104", no_swap),
1269
    ENTRYE(105, "105", no_swap),
1270
    { 0, NULL, NULL, NULL }
1271
};
1272

    
1273
struct sysctl_dir sysctls[] = {
1274
    ENTRYD(CTL_UNSPEC, "unspec", sysctls_unspec),
1275
    ENTRYD(CTL_KERN, "kern", sysctls_kern),
1276
    ENTRYD(CTL_HW,   "hw",   sysctls_hw ),
1277
    { 0, NULL, NULL, NULL }
1278
};
1279

    
1280
#undef ENTRYE
1281
#undef ENTRYD
1282

    
1283
static inline struct sysctl_dir * get_sysctl_entry_for_mib(int mib, struct sysctl_dir * sysctl_elmt)
1284
{
1285
    if(!sysctl_elmt)
1286
        return NULL;
1287
    for(; sysctl_elmt->name != NULL ; sysctl_elmt++) {
1288
        if(sysctl_elmt->num == mib)
1289
            return sysctl_elmt;
1290
    }
1291
    return NULL;
1292
}
1293

    
1294
static inline long bswap_syctl(int * mib, int count, void *buf, int size)
1295
{
1296
    int i;
1297
    struct sysctl_dir * sysctl = sysctls;
1298
    struct sysctl_dir * ret = NULL;
1299

    
1300
    for(i = 0; i < count; i++) {
1301

    
1302
        if(!(ret = sysctl = get_sysctl_entry_for_mib(mib[i], sysctl))) {
1303
            gemu_log("bswap_syctl: can't find mib %d\n", mib[i]);
1304
            return -ENOTDIR;
1305
        }
1306
        if(!(sysctl = sysctl->childs))
1307
            break;
1308
    }
1309
    
1310
    if(ret->childs)
1311
        qerror("we shouldn't have a directory element\n");
1312

    
1313
    ret->swap_func(buf, size);
1314
    return 0;
1315
}
1316

    
1317
static inline void print_syctl(int * mib, int count)
1318
{
1319
    int i;
1320
    struct sysctl_dir * sysctl = sysctls;
1321
    struct sysctl_dir * ret = NULL;
1322

    
1323
    for(i = 0; i < count; i++) {
1324
        if(!(ret = sysctl = get_sysctl_entry_for_mib(mib[i], sysctl))){
1325
            gemu_log("print_syctl: can't find mib %d\n", mib[i]);
1326
            return;
1327
        }
1328
        DPRINTF("%s.", sysctl->name);
1329
        if(!(sysctl = sysctl->childs))
1330
            break;
1331
    }
1332
    DPRINTF("\n");
1333
}
1334

    
1335
long do___sysctl(int * name, uint32_t namelen, void * oldp, size_t * oldlenp, void * newp, size_t newlen  /* ignored */)
1336
{
1337
    long ret = 0;
1338
    int i;
1339
    DPRINTF("sysctl(%p, 0x%x, %p, %p, %p, 0x%lx)\n",
1340
            name, namelen, oldp, oldlenp, newp, newlen);
1341
    if(name) {
1342
        i = 0;
1343
        do { tswap32s( name + i); } while (++i < namelen);
1344
        print_syctl(name, namelen);
1345
        //bswap_syctl(name, namelen, newp, newlen);
1346
        tswap32s((uint32_t*)oldlenp);
1347
    }
1348
        
1349
    if(name) /* Sometimes sysctl is called with no arg1, ignore */
1350
        ret = get_errno(sysctl(name, namelen, oldp, oldlenp, newp, newlen));
1351

    
1352
#if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1353
    if (!is_error(ret) && bswap_syctl(name, namelen, oldp, *oldlenp) != 0) {
1354
        return -ENOTDIR;
1355
    }
1356
#endif
1357

    
1358
    if(name) {
1359
        //bswap_syctl(name, namelen, newp, newlen);
1360
        tswap32s((uint32_t*)oldlenp);
1361

    
1362
        i = 0;
1363
        do { tswap32s( name + i); } while (++i < namelen);
1364
    }
1365
    return ret;
1366
}
1367

    
1368
long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5)
1369
{
1370
    struct attrlist * attrlist = (void *)arg2;
1371
    long ret;
1372

    
1373
#if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1374
    gemu_log("SYS_getdirentriesattr unimplemented\n");
1375
    return -ENOTSUP;
1376
#endif
1377
    /* XXX: don't let the %s stay in there */
1378
    DPRINTF("getattrlist(%s, %p, %p, 0x%x, 0x%x)\n",
1379
            (char *)arg1, arg2, arg3, arg4, arg5);
1380

    
1381
    if(arg2) /* XXX: We should handle that in a copy especially
1382
        if the structure is not writable */
1383
        byteswap_attrlist(attrlist);
1384

    
1385
    ret = get_errno(getattrlist((const char* )arg1, attrlist, (void *)arg3, arg4, arg5));
1386

    
1387
    if(!is_error(ret))
1388
    {
1389
        byteswap_attrbuf((void *)arg3, attrlist);
1390
        byteswap_attrlist(attrlist);
1391
    }
1392
    return ret;
1393
}
1394

    
1395
long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8)
1396
{
1397
    DPRINTF("getdirentriesattr(0x%x, %p, %p, 0x%lx, %p, %p, %p, 0x%x)\n",
1398
            arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
1399
#if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1400
    qerror("SYS_getdirentriesattr unimplemented\n");
1401
#endif
1402

    
1403
    return get_errno(getdirentriesattr( arg1, (struct attrlist * )arg2, (void *)arg3, arg4,
1404
                                       (unsigned long *)arg5, (unsigned long *)arg6,
1405
                                       (unsigned long *)arg7, arg8));
1406
}
1407

    
1408
static inline void bswap_flock(struct flock *f)
1409
{
1410
    tswap64s(&f->l_start);
1411
    tswap64s(&f->l_len);
1412
    tswap32s(&f->l_pid);
1413
    tswap16s(&f->l_type);
1414
    tswap16s(&f->l_whence);
1415
}
1416

    
1417
static inline void bswap_fstore(struct fstore *f)
1418
{
1419
    tswap32s(&f->fst_flags);
1420
    tswap32s(&f->fst_posmode);
1421
    tswap64s(&f->fst_offset);
1422
    tswap64s(&f->fst_length);
1423
    tswap64s(&f->fst_bytesalloc);
1424
}
1425

    
1426
static inline void bswap_radvisory(struct radvisory *f)
1427
{
1428
    tswap64s(&f->ra_offset);
1429
    tswap32s(&f->ra_count);
1430
}
1431

    
1432
static inline void bswap_fbootstraptransfer(struct fbootstraptransfer *f)
1433
{
1434
    tswap64s(&f->fbt_offset);
1435
    tswap32s((uint32_t*)&f->fbt_length);
1436
    tswap32s((uint32_t*)&f->fbt_buffer); /* XXX: this is a ptr */
1437
}
1438

    
1439
static inline void bswap_log2phys(struct log2phys *f)
1440
{
1441
    tswap32s(&f->l2p_flags);
1442
    tswap64s(&f->l2p_contigbytes);
1443
    tswap64s(&f->l2p_devoffset);
1444
}
1445

    
1446
static inline void bswap_fcntl_arg(int cmd, void * arg)
1447
{
1448
    switch(cmd)
1449
    {
1450
        case F_DUPFD:
1451
        case F_GETFD:
1452
        case F_SETFD:
1453
        case F_GETFL:
1454
        case F_SETFL:
1455
        case F_GETOWN:
1456
        case F_SETOWN:
1457
        case F_SETSIZE:
1458
        case F_RDAHEAD:
1459
        case F_FULLFSYNC:
1460
            break;
1461
        case F_GETLK:
1462
        case F_SETLK:
1463
        case F_SETLKW:
1464
            bswap_flock(arg);
1465
            break;
1466
        case F_PREALLOCATE:
1467
            bswap_fstore(arg);
1468
            break;
1469
        case F_RDADVISE:
1470
            bswap_radvisory(arg);
1471
            break;
1472
        case F_READBOOTSTRAP:
1473
        case F_WRITEBOOTSTRAP:
1474
            bswap_fbootstraptransfer(arg);
1475
            break;
1476
        case F_LOG2PHYS:
1477
            bswap_log2phys(arg);
1478
            break;
1479
        default:
1480
            gemu_log("unknow cmd in fcntl\n");
1481
    }
1482
}
1483

    
1484
long do_fcntl(int fd, int cmd, int arg)
1485
{
1486
    long ret;
1487
    bswap_fcntl_arg(cmd, (void *)arg);
1488
    ret = get_errno(fcntl(fd, cmd, arg));
1489
    if(!is_error(ret))
1490
        bswap_fcntl_arg(cmd, (void *)arg);
1491
    return ret;
1492
}
1493

    
1494
long no_syscall(void *cpu_env, int num)
1495
{
1496
    /* XXX: We should probably fordward it to the host kernel */
1497
    qerror("no unix syscall %d\n", num);
1498
    /* not reached */
1499
    return -1;
1500
}
1501

    
1502
long unimpl_unix_syscall(void *cpu_env, int num)
1503
{
1504
    if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1505
        qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1506

    
1507
    gemu_log("qemu: Unsupported unix syscall %s %d\n", unix_syscall_table[num].name , num);
1508
    gdb_handlesig (cpu_env, SIGTRAP);
1509
    exit(-1);
1510
}
1511

    
1512
long do_unix_syscall(void *cpu_env, int num)
1513
{
1514
    long ret = 0;
1515

    
1516
    DPRINTF("unix syscall %d: " , num);
1517

    
1518
    if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1519
        qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1520

    
1521
    DPRINTF("%s [%s]", unix_syscall_table[num].name, unix_syscall_table[num].call_type & CALL_DIRECT ? "direct" : "indirect" );
1522
    ret = unix_syscall_table[num].function(cpu_env, num);
1523

    
1524
    if(!(unix_syscall_table[num].call_type & CALL_NOERRNO))
1525
        ret = get_errno(ret);
1526

    
1527
    DPRINTF("[returned 0x%x(%d)]\n", (int)ret, (int)ret);
1528
    return ret;
1529
}
1530

    
1531
/* ------------------------------------------------------------
1532
   syscall_init
1533
*/
1534
void syscall_init(void)
1535
{
1536
    /* Nothing yet */
1537
}