Statistics
| Branch: | Revision:

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

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