4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Pierre d'Herbemont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <mach/message.h>
27 #include <mach/mach.h>
28 #include <mach/mach_time.h>
34 #include <sys/syscall.h>
35 #include <sys/sysctl.h>
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/dirent.h>
43 #include <sys/termios.h>
44 #include <sys/ptrace.h>
47 #include <sys/param.h>
48 #include <sys/mount.h>
53 #include <mach/mig_errors.h>
57 //#define DEBUG_SYSCALL
60 # define DEBUG_FORCE_ENABLE_LOCAL() int __DEBUG_qemu_user_force_enable = 1
61 # define DEBUG_BEGIN_ENABLE __DEBUG_qemu_user_force_enable = 1;
62 # define DEBUG_END_ENABLE __DEBUG_qemu_user_force_enable = 0;
64 # define DEBUG_DISABLE_ALL() static int __DEBUG_qemu_user_force_enable = 0
65 # define DEBUG_ENABLE_ALL() static int __DEBUG_qemu_user_force_enable = 1
68 # define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); \
69 if(__DEBUG_qemu_user_force_enable) fprintf(stderr, __VA_ARGS__); \
72 # define DEBUG_FORCE_ENABLE_LOCAL()
73 # define DEBUG_BEGIN_ENABLE
74 # define DEBUG_END_ENABLE
76 # define DPRINTF(...) do { if(loglevel) fprintf(logfile, __VA_ARGS__); } while(0)
84 extern const char *interp_prefix;
86 static inline long get_errno(long ret)
94 static inline int is_error(long ret)
96 return (unsigned long)ret >= (unsigned long)(-4096);
99 /* ------------------------------------------------------------
100 Mach syscall handling
103 void static inline print_description_msg_header(mach_msg_header_t *hdr)
107 struct { int number; char *name; } msg_name[] =
109 /* see http://fxr.watson.org/fxr/source/compat/mach/mach_namemap.c?v=NETBSD */
110 { 200, "host_info" },
111 { 202, "host_page_size" },
112 { 206, "host_get_clock_service" },
113 { 206, "host_get_clock_service" },
114 { 206, "host_get_clock_service" },
115 { 306, "host_get_clock_service" },
116 { 3204, "mach_port_allocate" },
117 { 3206, "mach_port_deallocate" },
118 { 3404, "mach_ports_lookup" },
119 { 3409, "mach_task_get_special_port" },
120 { 3414, "mach_task_get_exception_ports" },
121 { 3418, "mach_semaphore_create" },
122 { 3504, "mach_semaphore_create" },
123 { 3509, "mach_semaphore_create" },
124 { 3518, "semaphore_create" },
125 { 3616, "thread_policy" },
126 { 3801, "vm_allocate" },
127 { 3802, "vm_deallocate" },
128 { 3802, "vm_deallocate" },
129 { 3803, "vm_protect" },
131 { 4241776, "lu_message_send_id" }, /* lookupd */
132 { 4241876, "lu_message_reply_id" }, /* lookupd */
135 for(i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
136 if(msg_name[i].number == hdr->msgh_id)
138 name = msg_name[i].name;
143 DPRINTF("unknown mach msg %d 0x%x\n", hdr->msgh_id, hdr->msgh_id);
145 DPRINTF("%s\n", name);
147 DPRINTF("Bits: %8x\n", hdr->msgh_bits);
148 DPRINTF("Size: %8x\n", hdr->msgh_size);
149 DPRINTF("Rmte: %8x\n", hdr->msgh_remote_port);
150 DPRINTF("Locl: %8x\n", hdr->msgh_local_port);
151 DPRINTF("Rsrv: %8x\n", hdr->msgh_reserved);
153 DPRINTF("Id : %8x\n", hdr->msgh_id);
155 NDR_record_t *ndr = (NDR_record_t *)(hdr + 1);
156 DPRINTF("hdr = %p, sizeof(hdr) = %x, NDR = %p\n", hdr, (unsigned int)sizeof(mach_msg_header_t), ndr);
157 DPRINTF("%d %d %d %d %d %d %d %d\n",
158 ndr->mig_vers, ndr->if_vers, ndr->reserved1, ndr->mig_encoding,
159 ndr->int_rep, ndr->char_rep, ndr->float_rep, ndr->reserved2);
163 static inline void print_mach_msg_return(mach_msg_return_t ret)
166 #define MACH_MSG_RET(msg) { msg, #msg }
167 struct { int code; char *name; } msg_name[] =
169 /* ref: http://darwinsource.opendarwin.org/10.4.2/xnu-792.2.4/osfmk/man/mach_msg.html */
171 MACH_MSG_RET(MACH_SEND_MSG_TOO_SMALL),
172 MACH_MSG_RET(MACH_SEND_NO_BUFFER),
173 MACH_MSG_RET(MACH_SEND_INVALID_DATA),
174 MACH_MSG_RET(MACH_SEND_INVALID_HEADER),
175 MACH_MSG_RET(MACH_SEND_INVALID_DEST),
176 MACH_MSG_RET(MACH_SEND_INVALID_NOTIFY),
177 MACH_MSG_RET(MACH_SEND_INVALID_REPLY),
178 MACH_MSG_RET(MACH_SEND_INVALID_TRAILER),
179 MACH_MSG_RET(MACH_SEND_INVALID_MEMORY),
180 MACH_MSG_RET(MACH_SEND_INVALID_RIGHT),
181 MACH_MSG_RET(MACH_SEND_INVALID_TYPE),
182 MACH_MSG_RET(MACH_SEND_INTERRUPTED),
183 MACH_MSG_RET(MACH_SEND_TIMED_OUT),
185 MACH_MSG_RET(MACH_RCV_BODY_ERROR),
186 MACH_MSG_RET(MACH_RCV_HEADER_ERROR),
188 MACH_MSG_RET(MACH_RCV_IN_SET),
189 MACH_MSG_RET(MACH_RCV_INTERRUPTED),
191 MACH_MSG_RET(MACH_RCV_INVALID_DATA),
192 MACH_MSG_RET(MACH_RCV_INVALID_NAME),
193 MACH_MSG_RET(MACH_RCV_INVALID_NOTIFY),
194 MACH_MSG_RET(MACH_RCV_INVALID_TRAILER),
195 MACH_MSG_RET(MACH_RCV_INVALID_TYPE),
197 MACH_MSG_RET(MACH_RCV_PORT_CHANGED),
198 MACH_MSG_RET(MACH_RCV_PORT_DIED),
200 MACH_MSG_RET(MACH_RCV_SCATTER_SMALL),
201 MACH_MSG_RET(MACH_RCV_TIMED_OUT),
202 MACH_MSG_RET(MACH_RCV_TOO_LARGE)
206 if( ret == MACH_MSG_SUCCESS)
207 DPRINTF("MACH_MSG_SUCCESS\n");
210 for( i = 0; i < sizeof(msg_name)/sizeof(msg_name[0]); i++) {
211 if(msg_name[0].code & ret) {
212 DPRINTF("%s ", msg_name[0].name);
217 qerror("unknow mach message ret code %d\n", ret);
223 static inline void swap_mach_msg_header(mach_msg_header_t *hdr)
225 hdr->msgh_bits = tswap32(hdr->msgh_bits);
226 hdr->msgh_size = tswap32(hdr->msgh_size);
227 hdr->msgh_remote_port = tswap32(hdr->msgh_remote_port);
228 hdr->msgh_local_port = tswap32(hdr->msgh_local_port);
229 hdr->msgh_reserved = tswap32(hdr->msgh_reserved);
230 hdr->msgh_id = tswap32(hdr->msgh_id);
234 mach_msg_header_t hdr;
235 mach_msg_body_t body;
238 static inline void * swap_mach_msg_body(struct complex_msg *complex_msg, int bswap)
240 mach_msg_port_descriptor_t *descr = (mach_msg_port_descriptor_t *)(complex_msg+1);
242 void *additional_data;
244 if(bswap == bswap_in)
245 tswap32s(&complex_msg->body.msgh_descriptor_count);
247 DPRINTF("body.msgh_descriptor_count %d\n", complex_msg->body.msgh_descriptor_count);
249 for(i = 0; i < complex_msg->body.msgh_descriptor_count; i++) {
252 case MACH_MSG_PORT_DESCRIPTOR:
253 tswap32s(&descr->name);
256 case MACH_MSG_OOL_DESCRIPTOR:
258 mach_msg_ool_descriptor_t *ool = (void *)descr;
259 tswap32s((uint32_t *)&ool->address);
260 tswap32s(&ool->size);
262 descr = (mach_msg_port_descriptor_t *)(ool+1);
265 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
267 mach_msg_ool_ports_descriptor_t *ool_ports = (void *)descr;
268 mach_port_name_t * port_names;
270 if(bswap == bswap_in)
272 tswap32s((uint32_t *)&ool_ports->address);
273 tswap32s(&ool_ports->count);
276 port_names = ool_ports->address;
278 for(j = 0; j < ool_ports->count; j++)
279 tswap32s(&port_names[j]);
281 if(bswap == bswap_out)
283 tswap32s((uint32_t *)&ool_ports->address);
284 tswap32s(&ool_ports->count);
287 descr = (mach_msg_port_descriptor_t *)(ool_ports+1);
290 default: qerror("unknow mach msg descriptor type %x\n", descr->type);
293 if(bswap == bswap_out)
294 tswap32s(&complex_msg->body.msgh_descriptor_count);
295 additional_data = descr;
296 return additional_data;
299 static inline uint32_t target_mach_msg_trap(
300 mach_msg_header_t *hdr, uint32_t options, uint32_t send_size,
301 uint32_t rcv_size, uint32_t rcv_name, uint32_t time_out, uint32_t notify )
303 extern int mach_msg_trap(mach_msg_header_t *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
304 mach_msg_audit_trailer_t *trailer;
305 mach_msg_id_t msg_id;
307 char *additional_data;
310 swap_mach_msg_header(hdr);
312 print_description_msg_header(hdr);
314 msg_id = hdr->msgh_id;
316 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
317 additional_data = swap_mach_msg_body((struct complex_msg *)hdr, bswap_in);
319 additional_data = (void*)(hdr+1);
321 ret = mach_msg_trap(hdr, options, send_size, rcv_size, rcv_name, time_out, notify);
323 print_mach_msg_return(ret);
325 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
326 additional_data = swap_mach_msg_body((struct complex_msg *)hdr, bswap_out);
328 additional_data = (void*)(hdr+1);
330 if( (options & MACH_RCV_MSG) && (REQUESTED_TRAILER_SIZE(options) > 0) )
332 /* XXX: the kernel always return the full trailer with MACH_SEND_MSG, so we should
333 probably always bswap it */
334 /* warning: according to Mac OS X Internals (the book) msg_size might be expressed in
335 natural_t units but according to xnu/osfmk/mach/message.h: "The size of
336 the message must be specified in bytes" */
337 trailer = (mach_msg_audit_trailer_t *)((uint8_t *)hdr + hdr->msgh_size);
338 /* XXX: Should probably do that based on the option asked by the sender, but dealing
339 with kernel answer seems more sound */
340 switch(trailer->msgh_trailer_size)
342 case sizeof(mach_msg_audit_trailer_t):
343 for(i = 0; i < 8; i++)
344 tswap32s(&trailer->msgh_audit.val[i]);
345 /* Fall in mach_msg_security_trailer_t case */
346 case sizeof(mach_msg_security_trailer_t):
347 tswap32s(&trailer->msgh_sender.val[0]);
348 tswap32s(&trailer->msgh_sender.val[1]);
349 /* Fall in mach_msg_seqno_trailer_t case */
350 case sizeof(mach_msg_seqno_trailer_t):
351 tswap32s(&trailer->msgh_seqno);
352 /* Fall in mach_msg_trailer_t case */
353 case sizeof(mach_msg_trailer_t):
354 tswap32s(&trailer->msgh_trailer_type);
355 tswap32s(&trailer->msgh_trailer_size);
358 /* Safer not to byteswap, but probably wrong */
361 qerror("unknow trailer type given its size %d\n", trailer->msgh_trailer_size);
366 /* Special message handling */
368 case 200: /* host_info */
370 mig_reply_error_t *err = (mig_reply_error_t *)hdr;
377 uint32_t cpu_subtype;
378 } *data = (void *)(err+1);
380 DPRINTF("maxcpu = 0x%x\n", data->maxcpu);
381 DPRINTF("numcpu = 0x%x\n", data->maxcpu);
382 DPRINTF("memsize = 0x%x\n", data->memsize);
384 #if defined(TARGET_I386)
385 data->cpu_type = CPU_TYPE_I386;
386 DPRINTF("cpu_type changed to 0x%x(i386)\n", data->cpu_type);
387 #elif defined(TARGET_PPC)
388 data->cpu_type = CPU_TYPE_POWERPC;
389 DPRINTF("cpu_type changed to 0x%x(ppc)\n", data->cpu_type);
391 # error target not supported
394 #if defined(TARGET_I386)
395 data->cpu_subtype = CPU_SUBTYPE_PENT;
396 DPRINTF("cpu_subtype changed to 0x%x(i386_pent)\n", data->cpu_subtype);
397 #elif defined(TARGET_PPC)
398 data->cpu_subtype = CPU_SUBTYPE_POWERPC_750;
399 DPRINTF("cpu_subtype changed to 0x%x(ppc_all)\n", data->cpu_subtype);
401 # error target not supported
405 case 202: /* host_page_size */
407 mig_reply_error_t *err = (mig_reply_error_t *)hdr;
408 uint32_t *pagesize = (uint32_t *)(err+1);
410 DPRINTF("pagesize = %d\n", *pagesize);
416 swap_mach_msg_header(hdr);
421 long do_mach_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
422 uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
425 extern uint32_t mach_reply_port();
429 arg1 = tswap32(arg1);
430 arg2 = tswap32(arg2);
431 arg3 = tswap32(arg3);
432 arg4 = tswap32(arg4);
433 arg5 = tswap32(arg5);
434 arg6 = tswap32(arg6);
435 arg7 = tswap32(arg7);
436 arg8 = tswap32(arg8);
438 DPRINTF("mach syscall %d : " , num);
441 /* see xnu/osfmk/mach/syscall_sw.h */
443 DPRINTF("mach_reply_port()\n");
444 ret = mach_reply_port();
447 DPRINTF("mach_thread_self()\n");
448 ret = mach_thread_self();
451 DPRINTF("mach_task_self()\n");
452 ret = mach_task_self();
455 DPRINTF("mach_host_self()\n");
456 ret = mach_host_self();
459 DPRINTF("mach_msg_trap(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
460 arg1, arg2, arg3, arg4, arg5, arg6, arg7);
462 ret = target_mach_msg_trap((mach_msg_header_t *)arg1, arg2, arg3, arg4, arg5, arg6, arg7);
466 DPRINTF("semaphore_wait_trap(0x%x)\n", arg1);
467 extern int semaphore_wait_trap(int); // XXX: is there any header for that?
468 ret = semaphore_wait_trap(arg1);
471 DPRINTF("map_fd(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
472 arg1, arg2, arg3, arg4, arg5);
473 ret = map_fd(arg1, arg2, (void*)arg3, arg4, arg5);
474 tswap32s((uint32_t*)arg3);
477 DPRINTF("mach_timebase_info(0x%x)\n", arg1);
478 struct mach_timebase_info info;
479 ret = mach_timebase_info(&info);
482 struct mach_timebase_info *outInfo = (void*)arg1;
483 outInfo->numer = tswap32(info.numer);
484 outInfo->denom = tswap32(info.denom);
488 DPRINTF("mach_wait_until()\n");
489 extern int mach_wait_until(uint64_t); // XXX: is there any header for that?
490 ret = mach_wait_until(((uint64_t)arg2<<32) | (uint64_t)arg1);
493 DPRINTF("mk_timer_create()\n");
494 extern int mk_timer_create(); // XXX: is there any header for that?
495 ret = mk_timer_create();
498 DPRINTF("mk_timer_destroy()\n");
499 extern int mk_timer_destroy(int); // XXX: is there any header for that?
500 ret = mk_timer_destroy(arg1);
503 DPRINTF("mk_timer_create()\n");
504 extern int mk_timer_arm(int, uint64_t); // XXX: is there any header for that?
505 ret = mk_timer_arm(arg1, ((uint64_t)arg3<<32) | (uint64_t)arg2);
508 DPRINTF("mk_timer_cancel()\n");
509 extern int mk_timer_cancel(int, uint64_t *); // XXX: is there any header for that?
510 ret = mk_timer_cancel(arg1, (uint64_t *)arg2);
511 if((!is_error(ret)) && arg2)
512 tswap64s((uint64_t *)arg2);
515 gemu_log("qemu: Unsupported mach syscall: %d(0x%x)\n", num, num);
516 gdb_handlesig (cpu_env, SIGTRAP);
523 /* ------------------------------------------------------------
524 thread type syscall handling
526 long do_thread_syscall(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
527 uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
530 extern uint32_t cthread_set_self(uint32_t);
531 extern uint32_t processor_facilities_used();
534 arg1 = tswap32(arg1);
535 arg2 = tswap32(arg2);
536 arg3 = tswap32(arg3);
537 arg4 = tswap32(arg4);
538 arg5 = tswap32(arg5);
539 arg6 = tswap32(arg6);
540 arg7 = tswap32(arg7);
541 arg8 = tswap32(arg8);
543 DPRINTF("thread syscall %d : " , num);
549 case 0x7FF1: /* cthread_set_self */
550 DPRINTF("cthread_set_self(0x%x)\n", (unsigned int)arg1);
551 ret = cthread_set_self(arg1);
553 /* we need to update the LDT with the address of the thread */
554 write_dt((void *)(((CPUX86State *) cpu_env)->ldt.base + (4 * sizeof(uint64_t))), arg1, 1,
555 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
556 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
557 /* New i386 convention, %gs should be set to our this LDT entry */
558 cpu_x86_load_seg(cpu_env, R_GS, 0x27);
559 /* Old i386 convention, the kernel returns the selector for the cthread (pre-10.4.8?)*/
563 case 0x7FF2: /* Called the super-fast pthread_self handler by the apple guys */
564 DPRINTF("pthread_self()\n");
565 ret = (uint32_t)pthread_self();
568 DPRINTF("processor_facilities_used()\n");
570 qerror("processor_facilities_used: not implemented!\n");
572 ret = (uint32_t)processor_facilities_used();
576 gemu_log("qemu: Unsupported thread syscall: %d(0x%x)\n", num, num);
577 gdb_handlesig (cpu_env, SIGTRAP);
584 /* ------------------------------------------------------------
587 static inline void byteswap_termios(struct termios *t)
589 tswap32s((uint32_t*)&t->c_iflag);
590 tswap32s((uint32_t*)&t->c_oflag);
591 tswap32s((uint32_t*)&t->c_cflag);
592 tswap32s((uint32_t*)&t->c_lflag);
593 /* 20 (char) bytes then */
594 tswap32s((uint32_t*)&t->c_ispeed);
595 tswap32s((uint32_t*)&t->c_ospeed);
598 static inline void byteswap_winsize(struct winsize *w)
600 tswap16s(&w->ws_row);
601 tswap16s(&w->ws_col);
602 tswap16s(&w->ws_xpixel);
603 tswap16s(&w->ws_ypixel);
606 #define STRUCT(name, list...) STRUCT_ ## name,
607 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
609 #include "ioctls_types.h"
612 #undef STRUCT_SPECIAL
614 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
615 #define STRUCT_SPECIAL(name)
616 #include "ioctls_types.h"
618 #undef STRUCT_SPECIAL
620 typedef struct IOCTLEntry {
621 unsigned int target_cmd;
622 unsigned int host_cmd;
625 const argtype arg_type[5];
630 #define IOC_RW (IOC_R | IOC_W)
632 #define MAX_STRUCT_SIZE 4096
634 IOCTLEntry ioctl_entries[] = {
635 #define IOCTL(cmd, access, types...) \
636 { cmd, cmd, #cmd, access, { types } },
641 /* ??? Implement proper locking for ioctls. */
642 static long do_ioctl(long fd, long cmd, long arg)
644 const IOCTLEntry *ie;
645 const argtype *arg_type;
647 uint8_t buf_temp[MAX_STRUCT_SIZE];
653 if (ie->target_cmd == 0) {
654 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
657 if (ie->target_cmd == cmd)
661 arg_type = ie->arg_type;
663 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
665 switch(arg_type[0]) {
668 ret = get_errno(ioctl(fd, ie->host_cmd));
673 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
677 target_size = thunk_type_size(arg_type, 0);
680 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
681 if (!is_error(ret)) {
682 argptr = lock_user(arg, target_size, 0);
683 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
684 unlock_user(argptr, arg, target_size);
688 argptr = lock_user(arg, target_size, 1);
689 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
690 unlock_user(argptr, arg, 0);
691 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
695 argptr = lock_user(arg, target_size, 1);
696 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
697 unlock_user(argptr, arg, 0);
698 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
699 if (!is_error(ret)) {
700 argptr = lock_user(arg, target_size, 0);
701 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
702 unlock_user(argptr, arg, target_size);
708 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
715 /* ------------------------------------------------------------
716 Unix syscall handling
719 static inline void byteswap_attrlist(struct attrlist *a)
721 tswap16s(&a->bitmapcount);
722 tswap16s(&a->reserved);
723 tswap32s(&a->commonattr);
724 tswap32s(&a->volattr);
725 tswap32s(&a->dirattr);
726 tswap32s(&a->fileattr);
727 tswap32s(&a->forkattr);
730 struct attrbuf_header {
731 unsigned long length;
734 static inline void byteswap_attrbuf(struct attrbuf_header *attrbuf, struct attrlist *attrlist)
736 DPRINTF("attrBuf.lenght %lx\n", attrbuf->length);
739 static inline void byteswap_statfs(struct statfs *s)
741 tswap16s((uint16_t*)&s->f_otype);
742 tswap16s((uint16_t*)&s->f_oflags);
743 tswap32s((uint32_t*)&s->f_bsize);
744 tswap32s((uint32_t*)&s->f_iosize);
745 tswap32s((uint32_t*)&s->f_blocks);
746 tswap32s((uint32_t*)&s->f_bfree);
747 tswap32s((uint32_t*)&s->f_bavail);
748 tswap32s((uint32_t*)&s->f_files);
749 tswap32s((uint32_t*)&s->f_ffree);
750 tswap32s((uint32_t*)&s->f_fsid.val[0]);
751 tswap32s((uint32_t*)&s->f_fsid.val[1]);
752 tswap16s((uint16_t*)&s->f_reserved1);
753 tswap16s((uint16_t*)&s->f_type);
754 tswap32s((uint32_t*)&s->f_flags);
757 static inline void byteswap_stat(struct stat *s)
759 tswap32s((uint32_t*)&s->st_dev);
760 tswap32s(&s->st_ino);
761 tswap16s(&s->st_mode);
762 tswap16s(&s->st_nlink);
763 tswap32s(&s->st_uid);
764 tswap32s(&s->st_gid);
765 tswap32s((uint32_t*)&s->st_rdev);
766 tswap32s((uint32_t*)&s->st_atimespec.tv_sec);
767 tswap32s((uint32_t*)&s->st_atimespec.tv_nsec);
768 tswap32s((uint32_t*)&s->st_mtimespec.tv_sec);
769 tswap32s((uint32_t*)&s->st_mtimespec.tv_nsec);
770 tswap32s((uint32_t*)&s->st_ctimespec.tv_sec);
771 tswap32s((uint32_t*)&s->st_ctimespec.tv_nsec);
772 tswap64s((uint64_t*)&s->st_size);
773 tswap64s((uint64_t*)&s->st_blocks);
774 tswap32s((uint32_t*)&s->st_blksize);
775 tswap32s(&s->st_flags);
776 tswap32s(&s->st_gen);
779 static inline void byteswap_dirents(struct dirent *d, int bytes)
782 for( b = (char*)d; (int)b < (int)d+bytes; )
784 unsigned short s = ((struct dirent *)b)->d_reclen;
785 tswap32s(&((struct dirent *)b)->d_ino);
786 tswap16s(&((struct dirent *)b)->d_reclen);
793 static inline void byteswap_iovec(struct iovec *v, int n)
796 for(i = 0; i < n; i++)
798 tswap32s((uint32_t*)&v[i].iov_base);
799 tswap32s((uint32_t*)&v[i].iov_len);
803 static inline void byteswap_timeval(struct timeval *t)
805 tswap32s((uint32_t*)&t->tv_sec);
806 tswap32s((uint32_t*)&t->tv_usec);
809 long do_unix_syscall_indirect(void *cpu_env, int num);
811 long do_exit(uint32_t arg1);
812 long do_getlogin(char *out, uint32_t size);
813 long do_open(char * arg1, uint32_t arg2, uint32_t arg3);
814 long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3);
815 long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3);
816 long do_execve(char* arg1, char ** arg2, char ** arg3);
817 long do_getgroups(uint32_t arg1, gid_t * arg2);
818 long do_gettimeofday(struct timeval * arg1, void * arg2);
819 long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
820 long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3);
821 long do_utimes(char * arg1, struct timeval * arg2);
822 long do_futimes(uint32_t arg1, struct timeval * arg2);
823 long do_statfs(char * arg1, struct statfs * arg2);
824 long do_fstatfs(uint32_t arg1, struct statfs * arg2);
825 long do_stat(char * arg1, struct stat * arg2);
826 long do_fstat(uint32_t arg1, struct stat * arg2);
827 long do_lstat(char * arg1, struct stat * arg2);
828 long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4);
829 long do_lseek(void *cpu_env, int num);
830 long do___sysctl(void * arg1, uint32_t arg2, void * arg3, void * arg4, void * arg5, size_t arg6);
831 long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5);
832 long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8);
834 long no_syscall(void *cpu_env, int num);
836 long do_pread(uint32_t arg1, void * arg2, size_t arg3, off_t arg4)
838 //DPRINTF("0x%x, 0x%x, 0x%x, 0x%llx\n", arg1, arg2, arg3, arg4);
839 long ret = (pread(arg1, arg2, arg3, arg4));
840 DPRINTF("0x%x\n", *(int*)arg2);
843 long unimpl_unix_syscall(void *cpu_env, int num);
845 typedef long (*syscall_function_t)(void *cpu_env, int num);
848 /* define a table that will handle the syscall number->function association */
850 #define INT (uint32_t)get_int_arg(&i, cpu_env)
851 #define INT64 (uint64_t)get_int64_arg(&i, cpu_env)
852 #define UINT (unsigned int)INT
853 #define PTR (void*)INT
858 #define WRAPPER_CALL_DIRECT_0(function, args) long __qemu_##function(void *cpu_env) { return (long)function(); }
859 #define WRAPPER_CALL_DIRECT_1(function, _arg1) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; return (long)function(arg1); }
860 #define WRAPPER_CALL_DIRECT_2(function, _arg1, _arg2) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; return (long)function(arg1, arg2); }
861 #define WRAPPER_CALL_DIRECT_3(function, _arg1, _arg2, _arg3) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; return (long)function(arg1, arg2, arg3); }
862 #define WRAPPER_CALL_DIRECT_4(function, _arg1, _arg2, _arg3, _arg4) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; return (long)function(arg1, arg2, arg3, arg4); }
863 #define WRAPPER_CALL_DIRECT_5(function, _arg1, _arg2, _arg3, _arg4, _arg5) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; return (long)function(arg1, arg2, arg3, arg4, arg5); }
864 #define WRAPPER_CALL_DIRECT_6(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6; return (long)function(arg1, arg2, arg3, arg4, arg5, arg6); }
865 #define WRAPPER_CALL_DIRECT_7(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6; typeof(_arg7) arg7 = _arg7; return (long)function(arg1, arg2, arg3, arg4, arg5, arg6, arg7); }
866 #define WRAPPER_CALL_DIRECT_8(function, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) long __qemu_##function(void *cpu_env) { int i = 0; typeof(_arg1) arg1 = _arg1; typeof(_arg2) arg2 = _arg2; typeof(_arg3) arg3 = _arg3; typeof(_arg4) arg4 = _arg4; typeof(_arg5) arg5 = _arg5; typeof(_arg6) arg6 = _arg6; typeof(_arg7) arg7 = _arg7; typeof(_arg8) arg8 = _arg8; return (long)function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); }
867 #define WRAPPER_CALL_DIRECT(function, nargs, args...) WRAPPER_CALL_DIRECT_##nargs(function, args)
868 #define WRAPPER_CALL_NOERRNO(function, nargs, args...) WRAPPER_CALL_DIRECT(function, nargs, args)
869 #define WRAPPER_CALL_INDIRECT(function, nargs, args...)
870 #define ENTRY(name, number, function, nargs, call_type, args...) WRAPPER_##call_type(function, nargs, args)
872 #include "syscalls.h"
875 #undef WRAPPER_CALL_DIRECT
876 #undef WRAPPER_CALL_NOERRNO
877 #undef WRAPPER_CALL_INDIRECT
884 #define _ENTRY(name, number, function, nargs, call_type) [number] = {\
887 (syscall_function_t)function, \
892 #define ENTRY_CALL_DIRECT(name, number, function, nargs, call_type) _ENTRY(name, number, __qemu_##function, nargs, call_type)
893 #define ENTRY_CALL_NOERRNO(name, number, function, nargs, call_type) ENTRY_CALL_DIRECT(name, number, function, nargs, call_type)
894 #define ENTRY_CALL_INDIRECT(name, number, function, nargs, call_type) _ENTRY(name, number, function, nargs, call_type)
895 #define ENTRY(name, number, function, nargs, call_type, args...) ENTRY_##call_type(name, number, function, nargs, call_type)
897 #define CALL_DIRECT 1
898 #define CALL_INDIRECT 2
899 #define CALL_NOERRNO (CALL_DIRECT | 4 /* = 5 */)
901 struct unix_syscall {
904 syscall_function_t function;
907 } unix_syscall_table[SYS_MAXSYSCALL] = {
908 #include "syscalls.h"
913 #undef ENTRY_CALL_DIRECT
914 #undef ENTRY_CALL_INDIRECT
915 #undef ENTRY_CALL_NOERRNO
917 /* Actual syscalls implementation */
919 long do_unix_syscall_indirect(void *cpu_env, int num)
925 new_num = get_int_arg(&i, cpu_env);
927 ((CPUX86State*)cpu_env)->regs[R_ESP] += 4;
928 /* XXX: not necessary */
929 ((CPUX86State*)cpu_env)->regs[R_EAX] = new_num;
933 uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
934 for(i = 3; i < 11; i++)
935 *regs[i] = *regs[i+1];
936 /* XXX: not necessary */
940 ret = do_unix_syscall(cpu_env, new_num);
942 ((CPUX86State*)cpu_env)->regs[R_ESP] -= 4;
943 /* XXX: not necessary */
944 ((CPUX86State*)cpu_env)->regs[R_EAX] = num;
948 /* XXX: not really needed those regs are volatile accross calls */
949 uint32_t **regs = ((CPUPPCState*)cpu_env)->gpr;
950 for(i = 11; i > 3; i--)
951 *regs[i] = *regs[i-1];
959 long do_exit(uint32_t arg1)
972 long do_getlogin(char *out, uint32_t size)
974 char *login = getlogin();
977 memcpy(out, login, size);
980 long do_open(char * arg1, uint32_t arg2, uint32_t arg3)
982 /* XXX: don't let the %s stay in there */
983 DPRINTF("open(%s, 0x%x, 0x%x)\n", arg1, arg2, arg3);
984 return get_errno(open(arg1, arg2, arg3));
987 long do_getfsstat(struct statfs * arg1, uint32_t arg2, uint32_t arg3)
990 DPRINTF("getfsstat(%p, 0x%x, 0x%x)\n", arg1, arg2, arg3);
991 ret = get_errno(getfsstat(arg1, arg2, arg3));
992 if((!is_error(ret)) && arg1)
993 byteswap_statfs(arg1);
997 long do_sigprocmask(uint32_t arg1, uint32_t * arg2, uint32_t * arg3)
1000 DPRINTF("sigprocmask(%d, %p, %p)\n", arg1, arg2, arg3);
1001 gemu_log("XXX: sigprocmask not tested (%d, %p, %p)\n", arg1, arg2, arg3);
1004 ret = get_errno(sigprocmask(arg1, (void *)arg2, (void *)arg3));
1005 if((!is_error(ret)) && arg3)
1012 long do_execve(char* arg1, char ** arg2, char ** arg3)
1020 /* XXX: don't let the %s stay in here */
1021 DPRINTF("execve(%s, %p, %p)\n", arg1, arg2, arg3);
1023 for(argc = 0; argv[argc]; argc++);
1024 for(envc = 0; envp[envc]; envc++);
1026 argv = (char**)malloc(sizeof(char*)*argc);
1027 envp = (char**)malloc(sizeof(char*)*envc);
1029 for(; argc >= 0; argc--)
1030 argv[argc] = (char*)tswap32((uint32_t)(arg2)[argc]);
1032 for(; envc >= 0; envc--)
1033 envp[envc] = (char*)tswap32((uint32_t)(arg3)[envc]);
1035 ret = get_errno(execve(arg1, argv, envp));
1041 long do_getgroups(uint32_t arg1, gid_t * arg2)
1045 DPRINTF("getgroups(0x%x, %p)\n", arg1, arg2);
1046 ret = get_errno(getgroups(arg1, arg2));
1048 for(i = 0; i < arg1; i++)
1053 long do_gettimeofday(struct timeval * arg1, void * arg2)
1056 DPRINTF("gettimeofday(%p, %p)\n",
1058 ret = get_errno(gettimeofday(arg1, arg2));
1061 /* timezone no longer used according to the manpage, so don't bother with it */
1062 byteswap_timeval(arg1);
1067 long do_readv(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1070 DPRINTF("readv(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1072 byteswap_iovec(arg2, arg3);
1073 ret = get_errno(readv(arg1, arg2, arg3));
1074 if((!is_error(ret)) && arg2)
1075 byteswap_iovec(arg2, arg3);
1079 long do_writev(uint32_t arg1, struct iovec * arg2, uint32_t arg3)
1082 DPRINTF("writev(0x%x, %p, 0x%x)\n", arg1, arg2, arg3);
1084 byteswap_iovec(arg2, arg3);
1085 ret = get_errno(writev(arg1, arg2, arg3));
1086 if((!is_error(ret)) && arg2)
1087 byteswap_iovec(arg2, arg3);
1091 long do_utimes(char * arg1, struct timeval * arg2)
1093 DPRINTF("utimes(%p, %p)\n", arg1, arg2);
1096 byteswap_timeval(arg2);
1097 byteswap_timeval(arg2+1);
1099 return get_errno(utimes(arg1, arg2));
1102 long do_futimes(uint32_t arg1, struct timeval * arg2)
1104 DPRINTF("futimes(0x%x, %p)\n", arg1, arg2);
1107 byteswap_timeval(arg2);
1108 byteswap_timeval(arg2+1);
1110 return get_errno(futimes(arg1, arg2));
1113 long do_statfs(char * arg1, struct statfs * arg2)
1116 DPRINTF("statfs(%p, %p)\n", arg1, arg2);
1117 ret = get_errno(statfs(arg1, arg2));
1119 byteswap_statfs(arg2);
1123 long do_fstatfs(uint32_t arg1, struct statfs* arg2)
1126 DPRINTF("fstatfs(0x%x, %p)\n",
1128 ret = get_errno(fstatfs(arg1, arg2));
1130 byteswap_statfs(arg2);
1135 long do_stat(char * arg1, struct stat * arg2)
1138 /* XXX: don't let the %s stay in there */
1139 DPRINTF("stat(%s, %p)\n", arg1, arg2);
1140 ret = get_errno(stat(arg1, arg2));
1142 byteswap_stat(arg2);
1146 long do_fstat(uint32_t arg1, struct stat * arg2)
1149 DPRINTF("fstat(0x%x, %p)\n", arg1, arg2);
1150 ret = get_errno(fstat(arg1, arg2));
1152 byteswap_stat(arg2);
1156 long do_lstat(char * arg1, struct stat * arg2)
1159 /* XXX: don't let the %s stay in there */
1160 DPRINTF("lstat(%s, %p)\n", (const char *)arg1, arg2);
1161 ret = get_errno(lstat(arg1, arg2));
1163 byteswap_stat(arg2);
1167 long do_getdirentries(uint32_t arg1, void* arg2, uint32_t arg3, void* arg4)
1170 DPRINTF("getdirentries(0x%x, %p, 0x%x, %p)\n", arg1, arg2, arg3, arg4);
1172 tswap32s((uint32_t *)arg4);
1173 ret = get_errno(getdirentries(arg1, arg2, arg3, arg4));
1175 tswap32s((uint32_t *)arg4);
1177 byteswap_dirents(arg2, ret);
1181 long do_lseek(void *cpu_env, int num)
1185 uint32_t arg1 = get_int_arg(&i, cpu_env);
1186 uint64_t offset = get_int64_arg(&i, cpu_env);
1187 uint32_t arg3 = get_int_arg(&i, cpu_env);
1188 uint64_t r = lseek(arg1, offset, arg3);
1190 /* lowest word in eax, highest in edx */
1191 ret = r & 0xffffffff; /* will be set to eax after do_unix_syscall exit */
1192 ((CPUX86State *)cpu_env)->regs[R_EDX] = (uint32_t)((r >> 32) & 0xffffffff) ;
1193 #elif defined TARGET_PPC
1194 ret = r & 0xffffffff; /* will be set to r3 after do_unix_syscall exit */
1195 ((CPUPPCState *)cpu_env)->gpr[4] = (uint32_t)((r >> 32) & 0xffffffff) ;
1197 qerror("64 bit ret value on your arch?");
1199 return get_errno(ret);
1202 long do___sysctl(void * arg1, uint32_t arg2, void * arg3, void * arg4, void * arg5, size_t arg6)
1206 DPRINTF("sysctl(%p, 0x%x, %p, %p, %p, 0x%lx)\n",
1207 arg1, arg2, arg3, arg4, arg5, arg6);
1210 do { *((int *) arg1 + i) = tswap32(*((int *) arg1 + i)); } while (++i < arg2);
1214 *(int *) arg4 = tswap32(*(int *) arg4);
1216 ret = get_errno(sysctl((void *)arg1, arg2, (void *)arg3, (void *)arg4, (void *)arg5, arg6));
1218 if ((ret == 0) && (arg2 == 2) && (*((int *) arg1) == 0) && (*((int *) arg1 + 1) == 3)) {
1219 /* The output here is the new id - we need to swap it so it can be passed
1220 back in (and then unswapped) */
1221 int count = (*(int *) arg4) / sizeof(int);
1224 *((int *) arg3 + i) = tswap32(*((int *) arg3 + i));
1225 } while (++i < count);
1227 *(int *) arg4 = tswap32(*(int *) arg4);
1232 long do_getattrlist(void * arg1, void * arg2, void * arg3, uint32_t arg4, uint32_t arg5)
1234 struct attrlist * attrlist = (void *)arg2;
1237 #if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1238 qerror("SYS_getdirentriesattr unimplemented\n");
1240 /* XXX: don't let the %s stay in there */
1241 DPRINTF("getattrlist(%s, %p, %p, 0x%x, 0x%x)\n",
1242 (char *)arg1, arg2, arg3, arg4, arg5);
1244 if(arg2) /* XXX: We should handle that in a copy especially
1245 if the structure is not writable */
1246 byteswap_attrlist(attrlist);
1248 ret = get_errno(getattrlist((const char* )arg1, attrlist, (void *)arg3, arg4, arg5));
1252 byteswap_attrbuf((void *)arg3, attrlist);
1253 byteswap_attrlist(attrlist);
1258 long do_getdirentriesattr(uint32_t arg1, void * arg2, void * arg3, size_t arg4, void * arg5, void * arg6, void* arg7, uint32_t arg8)
1260 DPRINTF("getdirentriesattr(0x%x, %p, %p, 0x%lx, %p, %p, %p, 0x%x)\n",
1261 arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
1262 #if defined(TARGET_I386) ^ defined(__i386__) || defined(TARGET_PPC) ^ defined(__ppc__)
1263 qerror("SYS_getdirentriesattr unimplemented\n");
1266 return get_errno(getdirentriesattr( arg1, (struct attrlist * )arg2, (void *)arg3, arg4,
1267 (unsigned long *)arg5, (unsigned long *)arg6,
1268 (unsigned long *)arg7, arg8));
1272 long no_syscall(void *cpu_env, int num)
1274 /* XXX: We should probably fordward it to the host kernel */
1275 qerror("no unix syscall %d\n", num);
1280 long unimpl_unix_syscall(void *cpu_env, int num)
1282 if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1283 qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1285 gemu_log("qemu: Unsupported unix syscall %s %d\n", unix_syscall_table[num].name , num);
1286 gdb_handlesig (cpu_env, SIGTRAP);
1290 long do_unix_syscall(void *cpu_env, int num)
1294 DPRINTF("unix syscall %d: " , num);
1296 if( (num < 0) || (num > SYS_MAXSYSCALL-1) )
1297 qerror("unix syscall %d is out of unix syscall bounds (0-%d) " , num, SYS_MAXSYSCALL-1);
1299 DPRINTF("%s [%s]", unix_syscall_table[num].name, unix_syscall_table[num].call_type & CALL_DIRECT ? "direct" : "indirect" );
1300 ret = unix_syscall_table[num].function(cpu_env, num);
1302 if(!(unix_syscall_table[num].call_type & CALL_NOERRNO))
1303 ret = get_errno(ret);
1305 DPRINTF("[returned 0x%x(%d)]\n", (int)ret, (int)ret);
1309 /* ------------------------------------------------------------
1312 void syscall_init(void)