Statistics
| Branch: | Revision:

root / darwin-user / syscall.c @ 263466f5

History | View | Annotate | Download (46.7 kB)

1 831b7825 ths
/*
2 831b7825 ths
 *  Darwin syscalls
3 831b7825 ths
 *
4 831b7825 ths
 *  Copyright (c) 2003 Fabrice Bellard
5 831b7825 ths
 *  Copyright (c) 2006 Pierre d'Herbemont
6 831b7825 ths
 *
7 831b7825 ths
 *  This program is free software; you can redistribute it and/or modify
8 831b7825 ths
 *  it under the terms of the GNU General Public License as published by
9 831b7825 ths
 *  the Free Software Foundation; either version 2 of the License, or
10 831b7825 ths
 *  (at your option) any later version.
11 831b7825 ths
 *
12 831b7825 ths
 *  This program is distributed in the hope that it will be useful,
13 831b7825 ths
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 831b7825 ths
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 831b7825 ths
 *  GNU General Public License for more details.
16 831b7825 ths
 *
17 831b7825 ths
 *  You should have received a copy of the GNU General Public License
18 831b7825 ths
 *  along with this program; if not, write to the Free Software
19 831b7825 ths
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 831b7825 ths
 */
21 831b7825 ths
#include <fcntl.h>
22 831b7825 ths
#include <stdio.h>
23 831b7825 ths
#include <stdlib.h>
24 831b7825 ths
#include <errno.h>
25 831b7825 ths
26 263466f5 bellard
#include <mach/host_info.h>
27 831b7825 ths
#include <mach/mach.h>
28 831b7825 ths
#include <mach/mach_time.h>
29 263466f5 bellard
#include <mach/message.h>
30 831b7825 ths
31 831b7825 ths
#include <pthread.h>
32 831b7825 ths
#include <dirent.h>
33 831b7825 ths
34 831b7825 ths
#include <sys/stat.h>
35 831b7825 ths
#include <sys/syscall.h>
36 831b7825 ths
#include <sys/sysctl.h>
37 831b7825 ths
#include <sys/types.h>
38 831b7825 ths
#include <unistd.h>
39 831b7825 ths
#include <sys/ioctl.h>
40 831b7825 ths
#include <sys/mman.h>
41 831b7825 ths
#include <sys/types.h>
42 831b7825 ths
#include <sys/dirent.h>
43 831b7825 ths
#include <sys/uio.h>
44 831b7825 ths
#include <sys/termios.h>
45 831b7825 ths
#include <sys/ptrace.h>
46 831b7825 ths
#include <net/if.h>
47 831b7825 ths
48 831b7825 ths
#include <sys/param.h>
49 831b7825 ths
#include <sys/mount.h>
50 831b7825 ths
51 831b7825 ths
#include <sys/attr.h>
52 831b7825 ths
53 831b7825 ths
#include <mach/ndr.h>
54 831b7825 ths
#include <mach/mig_errors.h>
55 831b7825 ths
56 831b7825 ths
#include "qemu.h"
57 831b7825 ths
58 831b7825 ths
//#define DEBUG_SYSCALL
59 831b7825 ths
60 831b7825 ths
#ifdef DEBUG_SYSCALL
61 831b7825 ths
# define DEBUG_FORCE_ENABLE_LOCAL() int __DEBUG_qemu_user_force_enable = 1
62 831b7825 ths
# define DEBUG_BEGIN_ENABLE  __DEBUG_qemu_user_force_enable = 1;
63 831b7825 ths
# define DEBUG_END_ENABLE  __DEBUG_qemu_user_force_enable = 0;
64 831b7825 ths
65 831b7825 ths
# define DEBUG_DISABLE_ALL() static int __DEBUG_qemu_user_force_enable = 0
66 831b7825 ths
# define DEBUG_ENABLE_ALL()  static int __DEBUG_qemu_user_force_enable = 1
67 831b7825 ths
    DEBUG_ENABLE_ALL();
68 831b7825 ths
69 831b7825 ths
# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); \
70 831b7825 ths
                           if(__DEBUG_qemu_user_force_enable) fprintf(stderr, __VA_ARGS__); \
71 831b7825 ths
                         } while(0)
72 831b7825 ths
#else
73 831b7825 ths
# define DEBUG_FORCE_ENABLE_LOCAL()
74 831b7825 ths
# define DEBUG_BEGIN_ENABLE
75 831b7825 ths
# define DEBUG_END_ENABLE
76 831b7825 ths
77 831b7825 ths
# define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); } while(0)
78 831b7825 ths
#endif
79 831b7825 ths
80 831b7825 ths
enum {
81 831b7825 ths
    bswap_out = 0,
82 831b7825 ths
    bswap_in = 1
83 831b7825 ths
};
84 831b7825 ths
85 831b7825 ths
extern const char *interp_prefix;
86 831b7825 ths
87 831b7825 ths
static inline long get_errno(long ret)
88 831b7825 ths
{
89 831b7825 ths
    if (ret == -1)
90 831b7825 ths
        return -errno;
91 831b7825 ths
    else
92 831b7825 ths
        return ret;
93 831b7825 ths
}
94 831b7825 ths
95 831b7825 ths
static inline int is_error(long ret)
96 831b7825 ths
{
97 831b7825 ths
    return (unsigned long)ret >= (unsigned long)(-4096);
98 831b7825 ths
}
99 831b7825 ths
100 831b7825 ths
/* ------------------------------------------------------------
101 831b7825 ths
   Mach syscall handling
102 831b7825 ths
*/
103 831b7825 ths
104 831b7825 ths
void static inline print_description_msg_header(mach_msg_header_t *hdr)
105 831b7825 ths
{
106 831b7825 ths
    char *name = NULL;
107 831b7825 ths
    int i;
108 831b7825 ths
    struct { int number; char *name; } msg_name[] =
109 831b7825 ths
    {
110 831b7825 ths
        /* see http://fxr.watson.org/fxr/source/compat/mach/mach_namemap.c?v=NETBSD */
111 831b7825 ths
        { 200,      "host_info" },
112 831b7825 ths
        { 202,      "host_page_size" },
113 831b7825 ths
        { 206,      "host_get_clock_service" },
114 831b7825 ths
        { 206,      "host_get_clock_service" },
115 831b7825 ths
        { 206,      "host_get_clock_service" },
116 831b7825 ths
        { 306,      "host_get_clock_service" },
117 831b7825 ths
        { 3204,     "mach_port_allocate" },
118 831b7825 ths
        { 3206,     "mach_port_deallocate" },
119 831b7825 ths
        { 3404,     "mach_ports_lookup" },
120 831b7825 ths
        { 3409,     "mach_task_get_special_port" },
121 831b7825 ths
        { 3414,     "mach_task_get_exception_ports" },
122 831b7825 ths
        { 3418,     "mach_semaphore_create" },
123 831b7825 ths
        { 3504,     "mach_semaphore_create" },
124 831b7825 ths
        { 3509,     "mach_semaphore_create" },
125 831b7825 ths
        { 3518,     "semaphore_create" },
126 831b7825 ths
        { 3616,     "thread_policy" },
127 831b7825 ths
        { 3801,     "vm_allocate" },
128 831b7825 ths
        { 3802,     "vm_deallocate" },
129 831b7825 ths
        { 3802,     "vm_deallocate" },
130 831b7825 ths
        { 3803,     "vm_protect" },
131 831b7825 ths
        { 3812,     "vm_map" },
132 831b7825 ths
        { 4241776,  "lu_message_send_id" },  /* lookupd */
133 831b7825 ths
        { 4241876,  "lu_message_reply_id" }, /* lookupd */
134 831b7825 ths
    };
135 831b7825 ths
136 831b7825 ths
    for(i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
137 831b7825 ths
        if(msg_name[i].number == hdr->msgh_id)
138 831b7825 ths
        {
139 831b7825 ths
            name = msg_name[i].name;
140 831b7825 ths
            break;
141 831b7825 ths
        }
142 831b7825 ths
    }
143 831b7825 ths
    if(!name)
144 831b7825 ths
        DPRINTF("unknown mach msg %d 0x%x\n", hdr->msgh_id, hdr->msgh_id);
145 831b7825 ths
    else
146 831b7825 ths
        DPRINTF("%s\n", name);
147 831b7825 ths
#if 0
148 831b7825 ths
    DPRINTF("Bits: %8x\n", hdr->msgh_bits);
149 831b7825 ths
    DPRINTF("Size: %8x\n", hdr->msgh_size);
150 831b7825 ths
    DPRINTF("Rmte: %8x\n", hdr->msgh_remote_port);
151 831b7825 ths
    DPRINTF("Locl: %8x\n", hdr->msgh_local_port);
152 831b7825 ths
    DPRINTF("Rsrv: %8x\n", hdr->msgh_reserved);
153 831b7825 ths

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

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