* ppc-linux-nat.c (have_ptrace_getsetregs): New variable.
[gdb/SamB.git] / sim / m32r / traps-linux.c
blobec95a6854d4a5e2f40a2c866f8f2aeddd3e0738f
1 /* m32r exception, interrupt, and trap (EIT) support
2 Copyright (C) 1998, 2003, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Renesas.
5 This file is part of GDB, the GNU debugger.
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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "sim-main.h"
21 #include "syscall.h"
22 #include "targ-vals.h"
23 #include <dirent.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <utime.h>
29 #include <sys/mman.h>
30 #include <sys/poll.h>
31 #include <sys/resource.h>
32 #include <sys/sysinfo.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/timeb.h>
36 #include <sys/timex.h>
37 #include <sys/types.h>
38 #include <sys/uio.h>
39 #include <sys/utsname.h>
40 #include <sys/vfs.h>
41 #include <linux/sysctl.h>
42 #include <linux/types.h>
43 #include <linux/unistd.h>
45 #define TRAP_ELF_SYSCALL 0
46 #define TRAP_LINUX_SYSCALL 2
47 #define TRAP_FLUSH_CACHE 12
49 /* The semantic code invokes this for invalid (unrecognized) instructions. */
51 SEM_PC
52 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
54 SIM_DESC sd = CPU_STATE (current_cpu);
56 #if 0
57 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
59 h_bsm_set (current_cpu, h_sm_get (current_cpu));
60 h_bie_set (current_cpu, h_ie_get (current_cpu));
61 h_bcond_set (current_cpu, h_cond_get (current_cpu));
62 /* sm not changed */
63 h_ie_set (current_cpu, 0);
64 h_cond_set (current_cpu, 0);
66 h_bpc_set (current_cpu, cia);
68 sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
69 EIT_RSVD_INSN_ADDR);
71 else
72 #endif
73 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
74 return vpc;
77 /* Process an address exception. */
79 void
80 m32r_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
81 unsigned int map, int nr_bytes, address_word addr,
82 transfer_type transfer, sim_core_signals sig)
84 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
86 m32rbf_h_cr_set (current_cpu, H_CR_BBPC,
87 m32rbf_h_cr_get (current_cpu, H_CR_BPC));
88 if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32R)
90 m32rbf_h_bpsw_set (current_cpu, m32rbf_h_psw_get (current_cpu));
91 /* sm not changed */
92 m32rbf_h_psw_set (current_cpu, m32rbf_h_psw_get (current_cpu) & 0x80);
94 else if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32RX)
96 m32rxf_h_bpsw_set (current_cpu, m32rxf_h_psw_get (current_cpu));
97 /* sm not changed */
98 m32rxf_h_psw_set (current_cpu, m32rxf_h_psw_get (current_cpu) & 0x80);
100 else
102 m32r2f_h_bpsw_set (current_cpu, m32r2f_h_psw_get (current_cpu));
103 /* sm not changed */
104 m32r2f_h_psw_set (current_cpu, m32r2f_h_psw_get (current_cpu) & 0x80);
106 m32rbf_h_cr_set (current_cpu, H_CR_BPC, cia);
108 sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
109 EIT_ADDR_EXCP_ADDR);
111 else
112 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
113 transfer, sig);
116 /* Read/write functions for system call interface. */
118 static int
119 syscall_read_mem (host_callback *cb, struct cb_syscall *sc,
120 unsigned long taddr, char *buf, int bytes)
122 SIM_DESC sd = (SIM_DESC) sc->p1;
123 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
125 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
128 static int
129 syscall_write_mem (host_callback *cb, struct cb_syscall *sc,
130 unsigned long taddr, const char *buf, int bytes)
132 SIM_DESC sd = (SIM_DESC) sc->p1;
133 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
135 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
138 /* Translate target's address to host's address. */
140 static void *
141 t2h_addr (host_callback *cb, struct cb_syscall *sc,
142 unsigned long taddr)
144 extern sim_core_trans_addr (SIM_DESC, sim_cpu *, unsigned, address_word);
145 void *addr;
146 SIM_DESC sd = (SIM_DESC) sc->p1;
147 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
149 if (taddr == 0)
150 return NULL;
152 return sim_core_trans_addr (sd, cpu, read_map, taddr);
155 static unsigned int
156 conv_endian (unsigned int tvalue)
158 unsigned int hvalue;
159 unsigned int t1, t2, t3, t4;
161 if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN)
163 t1 = tvalue & 0xff000000;
164 t2 = tvalue & 0x00ff0000;
165 t3 = tvalue & 0x0000ff00;
166 t4 = tvalue & 0x000000ff;
168 hvalue = t1 >> 24;
169 hvalue += t2 >> 8;
170 hvalue += t3 << 8;
171 hvalue += t4 << 24;
173 else
174 hvalue = tvalue;
176 return hvalue;
179 static unsigned short
180 conv_endian16 (unsigned short tvalue)
182 unsigned short hvalue;
183 unsigned short t1, t2;
185 if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN)
187 t1 = tvalue & 0xff00;
188 t2 = tvalue & 0x00ff;
190 hvalue = t1 >> 8;
191 hvalue += t2 << 8;
193 else
194 hvalue = tvalue;
196 return hvalue;
199 static void
200 translate_endian(void *addr, size_t size)
202 unsigned int *p = (unsigned int *) addr;
203 int i;
205 for (i = 0; i <= size - 4; i += 4,p++)
206 *p = conv_endian(*p);
208 if (i <= size - 2)
209 *((unsigned short *) p) = conv_endian16(*((unsigned short *) p));
212 /* Trap support.
213 The result is the pc address to continue at.
214 Preprocessing like saving the various registers has already been done. */
217 m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
219 SIM_DESC sd = CPU_STATE (current_cpu);
220 host_callback *cb = STATE_CALLBACK (sd);
222 #ifdef SIM_HAVE_BREAKPOINTS
223 /* Check for breakpoints "owned" by the simulator first, regardless
224 of --environment. */
225 if (num == TRAP_BREAKPOINT)
227 /* First try sim-break.c. If it's a breakpoint the simulator "owns"
228 it doesn't return. Otherwise it returns and let's us try. */
229 sim_handle_breakpoint (sd, current_cpu, pc);
230 /* Fall through. */
232 #endif
234 switch (num)
236 case TRAP_ELF_SYSCALL :
238 CB_SYSCALL s;
240 CB_SYSCALL_INIT (&s);
241 s.func = m32rbf_h_gr_get (current_cpu, 0);
242 s.arg1 = m32rbf_h_gr_get (current_cpu, 1);
243 s.arg2 = m32rbf_h_gr_get (current_cpu, 2);
244 s.arg3 = m32rbf_h_gr_get (current_cpu, 3);
246 if (s.func == TARGET_SYS_exit)
248 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
251 s.p1 = (PTR) sd;
252 s.p2 = (PTR) current_cpu;
253 s.read_mem = syscall_read_mem;
254 s.write_mem = syscall_write_mem;
255 cb_syscall (cb, &s);
256 m32rbf_h_gr_set (current_cpu, 2, s.errcode);
257 m32rbf_h_gr_set (current_cpu, 0, s.result);
258 m32rbf_h_gr_set (current_cpu, 1, s.result2);
259 break;
262 case TRAP_LINUX_SYSCALL :
264 CB_SYSCALL s;
265 unsigned int func, arg1, arg2, arg3, arg4, arg5, arg6, arg7;
266 int result, result2, errcode;
268 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
270 /* The new pc is the trap vector entry.
271 We assume there's a branch there to some handler.
272 Use cr5 as EVB (EIT Vector Base) register. */
273 USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
274 return new_pc;
277 func = m32rbf_h_gr_get (current_cpu, 7);
278 arg1 = m32rbf_h_gr_get (current_cpu, 0);
279 arg2 = m32rbf_h_gr_get (current_cpu, 1);
280 arg3 = m32rbf_h_gr_get (current_cpu, 2);
281 arg4 = m32rbf_h_gr_get (current_cpu, 3);
282 arg5 = m32rbf_h_gr_get (current_cpu, 4);
283 arg6 = m32rbf_h_gr_get (current_cpu, 5);
284 arg7 = m32rbf_h_gr_get (current_cpu, 6);
286 CB_SYSCALL_INIT (&s);
287 s.func = func;
288 s.arg1 = arg1;
289 s.arg2 = arg2;
290 s.arg3 = arg3;
292 s.p1 = (PTR) sd;
293 s.p2 = (PTR) current_cpu;
294 s.read_mem = syscall_read_mem;
295 s.write_mem = syscall_write_mem;
297 result = 0;
298 result2 = 0;
299 errcode = 0;
301 switch (func)
303 case __NR_exit:
304 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
305 break;
307 case __NR_read:
308 result = read(arg1, t2h_addr(cb, &s, arg2), arg3);
309 errcode = errno;
310 break;
312 case __NR_write:
313 result = write(arg1, t2h_addr(cb, &s, arg2), arg3);
314 errcode = errno;
315 break;
317 case __NR_open:
318 result = open((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
319 errcode = errno;
320 break;
322 case __NR_close:
323 result = close(arg1);
324 errcode = errno;
325 break;
327 case __NR_creat:
328 result = creat((char *) t2h_addr(cb, &s, arg1), arg2);
329 errcode = errno;
330 break;
332 case __NR_link:
333 result = link((char *) t2h_addr(cb, &s, arg1),
334 (char *) t2h_addr(cb, &s, arg2));
335 errcode = errno;
336 break;
338 case __NR_unlink:
339 result = unlink((char *) t2h_addr(cb, &s, arg1));
340 errcode = errno;
341 break;
343 case __NR_chdir:
344 result = chdir((char *) t2h_addr(cb, &s, arg1));
345 errcode = errno;
346 break;
348 case __NR_time:
350 time_t t;
352 if (arg1 == 0)
354 result = (int) time(NULL);
355 errcode = errno;
357 else
359 result = (int) time(&t);
360 errcode = errno;
362 if (result != 0)
363 break;
365 translate_endian((void *) &t, sizeof(t));
366 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t)) != sizeof(t))
368 result = -1;
369 errcode = EINVAL;
373 break;
375 case __NR_mknod:
376 result = mknod((char *) t2h_addr(cb, &s, arg1),
377 (mode_t) arg2, (dev_t) arg3);
378 errcode = errno;
379 break;
381 case __NR_chmod:
382 result = chmod((char *) t2h_addr(cb, &s, arg1), (mode_t) arg2);
383 errcode = errno;
384 break;
386 case __NR_lchown32:
387 case __NR_lchown:
388 result = lchown((char *) t2h_addr(cb, &s, arg1),
389 (uid_t) arg2, (gid_t) arg3);
390 errcode = errno;
391 break;
393 case __NR_lseek:
394 result = (int) lseek(arg1, (off_t) arg2, arg3);
395 errcode = errno;
396 break;
398 case __NR_getpid:
399 result = getpid();
400 errcode = errno;
401 break;
403 case __NR_getuid32:
404 case __NR_getuid:
405 result = getuid();
406 errcode = errno;
407 break;
409 case __NR_utime:
411 struct utimbuf buf;
413 if (arg2 == 0)
415 result = utime((char *) t2h_addr(cb, &s, arg1), NULL);
416 errcode = errno;
418 else
420 buf = *((struct utimbuf *) t2h_addr(cb, &s, arg2));
421 translate_endian((void *) &buf, sizeof(buf));
422 result = utime((char *) t2h_addr(cb, &s, arg1), &buf);
423 errcode = errno;
426 break;
428 case __NR_access:
429 result = access((char *) t2h_addr(cb, &s, arg1), arg2);
430 errcode = errno;
431 break;
433 case __NR_ftime:
435 struct timeb t;
437 result = ftime(&t);
438 errcode = errno;
440 if (result != 0)
441 break;
443 t.time = conv_endian(t.time);
444 t.millitm = conv_endian16(t.millitm);
445 t.timezone = conv_endian16(t.timezone);
446 t.dstflag = conv_endian16(t.dstflag);
447 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t))
448 != sizeof(t))
450 result = -1;
451 errcode = EINVAL;
455 case __NR_sync:
456 sync();
457 result = 0;
458 break;
460 case __NR_rename:
461 result = rename((char *) t2h_addr(cb, &s, arg1),
462 (char *) t2h_addr(cb, &s, arg2));
463 errcode = errno;
464 break;
466 case __NR_mkdir:
467 result = mkdir((char *) t2h_addr(cb, &s, arg1), arg2);
468 errcode = errno;
469 break;
471 case __NR_rmdir:
472 result = rmdir((char *) t2h_addr(cb, &s, arg1));
473 errcode = errno;
474 break;
476 case __NR_dup:
477 result = dup(arg1);
478 errcode = errno;
479 break;
481 case __NR_brk:
482 result = brk((void *) arg1);
483 errcode = errno;
484 //result = arg1;
485 break;
487 case __NR_getgid32:
488 case __NR_getgid:
489 result = getgid();
490 errcode = errno;
491 break;
493 case __NR_geteuid32:
494 case __NR_geteuid:
495 result = geteuid();
496 errcode = errno;
497 break;
499 case __NR_getegid32:
500 case __NR_getegid:
501 result = getegid();
502 errcode = errno;
503 break;
505 case __NR_ioctl:
506 result = ioctl(arg1, arg2, arg3);
507 errcode = errno;
508 break;
510 case __NR_fcntl:
511 result = fcntl(arg1, arg2, arg3);
512 errcode = errno;
513 break;
515 case __NR_dup2:
516 result = dup2(arg1, arg2);
517 errcode = errno;
518 break;
520 case __NR_getppid:
521 result = getppid();
522 errcode = errno;
523 break;
525 case __NR_getpgrp:
526 result = getpgrp();
527 errcode = errno;
528 break;
530 case __NR_getrlimit:
532 struct rlimit rlim;
534 result = getrlimit(arg1, &rlim);
535 errcode = errno;
537 if (result != 0)
538 break;
540 translate_endian((void *) &rlim, sizeof(rlim));
541 if ((s.write_mem) (cb, &s, arg2, (char *) &rlim, sizeof(rlim))
542 != sizeof(rlim))
544 result = -1;
545 errcode = EINVAL;
548 break;
550 case __NR_getrusage:
552 struct rusage usage;
554 result = getrusage(arg1, &usage);
555 errcode = errno;
557 if (result != 0)
558 break;
560 translate_endian((void *) &usage, sizeof(usage));
561 if ((s.write_mem) (cb, &s, arg2, (char *) &usage, sizeof(usage))
562 != sizeof(usage))
564 result = -1;
565 errcode = EINVAL;
568 break;
570 case __NR_gettimeofday:
572 struct timeval tv;
573 struct timezone tz;
575 result = gettimeofday(&tv, &tz);
576 errcode = errno;
578 if (result != 0)
579 break;
581 translate_endian((void *) &tv, sizeof(tv));
582 if ((s.write_mem) (cb, &s, arg1, (char *) &tv, sizeof(tv))
583 != sizeof(tv))
585 result = -1;
586 errcode = EINVAL;
589 translate_endian((void *) &tz, sizeof(tz));
590 if ((s.write_mem) (cb, &s, arg2, (char *) &tz, sizeof(tz))
591 != sizeof(tz))
593 result = -1;
594 errcode = EINVAL;
597 break;
599 case __NR_getgroups32:
600 case __NR_getgroups:
602 gid_t *list;
604 if (arg1 > 0)
605 list = (gid_t *) malloc(arg1 * sizeof(gid_t));
607 result = getgroups(arg1, list);
608 errcode = errno;
610 if (result != 0)
611 break;
613 translate_endian((void *) list, arg1 * sizeof(gid_t));
614 if (arg1 > 0)
615 if ((s.write_mem) (cb, &s, arg2, (char *) list, arg1 * sizeof(gid_t))
616 != arg1 * sizeof(gid_t))
618 result = -1;
619 errcode = EINVAL;
622 break;
624 case __NR_select:
626 int n;
627 fd_set readfds;
628 fd_set *treadfdsp;
629 fd_set *hreadfdsp;
630 fd_set writefds;
631 fd_set *twritefdsp;
632 fd_set *hwritefdsp;
633 fd_set exceptfds;
634 fd_set *texceptfdsp;
635 fd_set *hexceptfdsp;
636 struct timeval *ttimeoutp;
637 struct timeval timeout;
639 n = arg1;
641 treadfdsp = (fd_set *) arg2;
642 if (treadfdsp != NULL)
644 readfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) treadfdsp));
645 translate_endian((void *) &readfds, sizeof(readfds));
646 hreadfdsp = &readfds;
648 else
649 hreadfdsp = NULL;
651 twritefdsp = (fd_set *) arg3;
652 if (twritefdsp != NULL)
654 writefds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) twritefdsp));
655 translate_endian((void *) &writefds, sizeof(writefds));
656 hwritefdsp = &writefds;
658 else
659 hwritefdsp = NULL;
661 texceptfdsp = (fd_set *) arg4;
662 if (texceptfdsp != NULL)
664 exceptfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) texceptfdsp));
665 translate_endian((void *) &exceptfds, sizeof(exceptfds));
666 hexceptfdsp = &exceptfds;
668 else
669 hexceptfdsp = NULL;
671 ttimeoutp = (struct timeval *) arg5;
672 timeout = *((struct timeval *) t2h_addr(cb, &s, (unsigned int) ttimeoutp));
673 translate_endian((void *) &timeout, sizeof(timeout));
675 result = select(n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
676 errcode = errno;
678 if (result != 0)
679 break;
681 if (treadfdsp != NULL)
683 translate_endian((void *) &readfds, sizeof(readfds));
684 if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
685 (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
687 result = -1;
688 errcode = EINVAL;
692 if (twritefdsp != NULL)
694 translate_endian((void *) &writefds, sizeof(writefds));
695 if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
696 (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
698 result = -1;
699 errcode = EINVAL;
703 if (texceptfdsp != NULL)
705 translate_endian((void *) &exceptfds, sizeof(exceptfds));
706 if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
707 (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
709 result = -1;
710 errcode = EINVAL;
714 translate_endian((void *) &timeout, sizeof(timeout));
715 if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
716 (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
718 result = -1;
719 errcode = EINVAL;
722 break;
724 case __NR_symlink:
725 result = symlink((char *) t2h_addr(cb, &s, arg1),
726 (char *) t2h_addr(cb, &s, arg2));
727 errcode = errno;
728 break;
730 case __NR_readlink:
731 result = readlink((char *) t2h_addr(cb, &s, arg1),
732 (char *) t2h_addr(cb, &s, arg2),
733 arg3);
734 errcode = errno;
735 break;
737 case __NR_readdir:
738 result = (int) readdir((DIR *) t2h_addr(cb, &s, arg1));
739 errcode = errno;
740 break;
742 #if 0
743 case __NR_mmap:
745 result = (int) mmap((void *) t2h_addr(cb, &s, arg1),
746 arg2, arg3, arg4, arg5, arg6);
747 errcode = errno;
749 if (errno == 0)
751 sim_core_attach (sd, NULL,
752 0, access_read_write_exec, 0,
753 result, arg2, 0, NULL, NULL);
756 break;
757 #endif
758 case __NR_mmap2:
760 void *addr;
761 size_t len;
762 int prot, flags, fildes;
763 off_t off;
765 addr = (void *) t2h_addr(cb, &s, arg1);
766 len = arg2;
767 prot = arg3;
768 flags = arg4;
769 fildes = arg5;
770 off = arg6 << 12;
772 result = (int) mmap(addr, len, prot, flags, fildes, off);
773 errcode = errno;
774 if (result != -1)
776 char c;
777 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
778 sim_core_attach (sd, NULL,
779 0, access_read_write_exec, 0,
780 result, len, 0, NULL, NULL);
783 break;
785 case __NR_mmap:
787 void *addr;
788 size_t len;
789 int prot, flags, fildes;
790 off_t off;
792 addr = *((void **) t2h_addr(cb, &s, arg1));
793 len = *((size_t *) t2h_addr(cb, &s, arg1 + 4));
794 prot = *((int *) t2h_addr(cb, &s, arg1 + 8));
795 flags = *((int *) t2h_addr(cb, &s, arg1 + 12));
796 fildes = *((int *) t2h_addr(cb, &s, arg1 + 16));
797 off = *((off_t *) t2h_addr(cb, &s, arg1 + 20));
799 addr = (void *) conv_endian((unsigned int) addr);
800 len = conv_endian(len);
801 prot = conv_endian(prot);
802 flags = conv_endian(flags);
803 fildes = conv_endian(fildes);
804 off = conv_endian(off);
806 //addr = (void *) t2h_addr(cb, &s, (unsigned int) addr);
807 result = (int) mmap(addr, len, prot, flags, fildes, off);
808 errcode = errno;
810 //if (errno == 0)
811 if (result != -1)
813 char c;
814 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
815 sim_core_attach (sd, NULL,
816 0, access_read_write_exec, 0,
817 result, len, 0, NULL, NULL);
820 break;
822 case __NR_munmap:
824 result = munmap((void *)arg1, arg2);
825 errcode = errno;
826 if (result != -1)
828 sim_core_detach (sd, NULL, 0, arg2, result);
831 break;
833 case __NR_truncate:
834 result = truncate((char *) t2h_addr(cb, &s, arg1), arg2);
835 errcode = errno;
836 break;
838 case __NR_ftruncate:
839 result = ftruncate(arg1, arg2);
840 errcode = errno;
841 break;
843 case __NR_fchmod:
844 result = fchmod(arg1, arg2);
845 errcode = errno;
846 break;
848 case __NR_fchown32:
849 case __NR_fchown:
850 result = fchown(arg1, arg2, arg3);
851 errcode = errno;
852 break;
854 case __NR_statfs:
856 struct statfs statbuf;
858 result = statfs((char *) t2h_addr(cb, &s, arg1), &statbuf);
859 errcode = errno;
861 if (result != 0)
862 break;
864 translate_endian((void *) &statbuf, sizeof(statbuf));
865 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
866 != sizeof(statbuf))
868 result = -1;
869 errcode = EINVAL;
872 break;
874 case __NR_fstatfs:
876 struct statfs statbuf;
878 result = fstatfs(arg1, &statbuf);
879 errcode = errno;
881 if (result != 0)
882 break;
884 translate_endian((void *) &statbuf, sizeof(statbuf));
885 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
886 != sizeof(statbuf))
888 result = -1;
889 errcode = EINVAL;
892 break;
894 case __NR_syslog:
895 result = syslog(arg1, (char *) t2h_addr(cb, &s, arg2));
896 errcode = errno;
897 break;
899 case __NR_setitimer:
901 struct itimerval value, ovalue;
903 value = *((struct itimerval *) t2h_addr(cb, &s, arg2));
904 translate_endian((void *) &value, sizeof(value));
906 if (arg2 == 0)
908 result = setitimer(arg1, &value, NULL);
909 errcode = errno;
911 else
913 result = setitimer(arg1, &value, &ovalue);
914 errcode = errno;
916 if (result != 0)
917 break;
919 translate_endian((void *) &ovalue, sizeof(ovalue));
920 if ((s.write_mem) (cb, &s, arg3, (char *) &ovalue, sizeof(ovalue))
921 != sizeof(ovalue))
923 result = -1;
924 errcode = EINVAL;
928 break;
930 case __NR_getitimer:
932 struct itimerval value;
934 result = getitimer(arg1, &value);
935 errcode = errno;
937 if (result != 0)
938 break;
940 translate_endian((void *) &value, sizeof(value));
941 if ((s.write_mem) (cb, &s, arg2, (char *) &value, sizeof(value))
942 != sizeof(value))
944 result = -1;
945 errcode = EINVAL;
948 break;
950 case __NR_stat:
952 char *buf;
953 int buflen;
954 struct stat statbuf;
956 result = stat((char *) t2h_addr(cb, &s, arg1), &statbuf);
957 errcode = errno;
958 if (result < 0)
959 break;
961 buflen = cb_host_to_target_stat (cb, NULL, NULL);
962 buf = xmalloc (buflen);
963 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
965 /* The translation failed. This is due to an internal
966 host program error, not the target's fault. */
967 free (buf);
968 result = -1;
969 errcode = ENOSYS;
970 break;
972 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
974 free (buf);
975 result = -1;
976 errcode = EINVAL;
977 break;
979 free (buf);
981 break;
983 case __NR_lstat:
985 char *buf;
986 int buflen;
987 struct stat statbuf;
989 result = lstat((char *) t2h_addr(cb, &s, arg1), &statbuf);
990 errcode = errno;
991 if (result < 0)
992 break;
994 buflen = cb_host_to_target_stat (cb, NULL, NULL);
995 buf = xmalloc (buflen);
996 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
998 /* The translation failed. This is due to an internal
999 host program error, not the target's fault. */
1000 free (buf);
1001 result = -1;
1002 errcode = ENOSYS;
1003 break;
1005 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
1007 free (buf);
1008 result = -1;
1009 errcode = EINVAL;
1010 break;
1012 free (buf);
1014 break;
1016 case __NR_fstat:
1018 char *buf;
1019 int buflen;
1020 struct stat statbuf;
1022 result = fstat(arg1, &statbuf);
1023 errcode = errno;
1024 if (result < 0)
1025 break;
1027 buflen = cb_host_to_target_stat (cb, NULL, NULL);
1028 buf = xmalloc (buflen);
1029 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
1031 /* The translation failed. This is due to an internal
1032 host program error, not the target's fault. */
1033 free (buf);
1034 result = -1;
1035 errcode = ENOSYS;
1036 break;
1038 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
1040 free (buf);
1041 result = -1;
1042 errcode = EINVAL;
1043 break;
1045 free (buf);
1047 break;
1049 case __NR_sysinfo:
1051 struct sysinfo info;
1053 result = sysinfo(&info);
1054 errcode = errno;
1056 if (result != 0)
1057 break;
1059 info.uptime = conv_endian(info.uptime);
1060 info.loads[0] = conv_endian(info.loads[0]);
1061 info.loads[1] = conv_endian(info.loads[1]);
1062 info.loads[2] = conv_endian(info.loads[2]);
1063 info.totalram = conv_endian(info.totalram);
1064 info.freeram = conv_endian(info.freeram);
1065 info.sharedram = conv_endian(info.sharedram);
1066 info.bufferram = conv_endian(info.bufferram);
1067 info.totalswap = conv_endian(info.totalswap);
1068 info.freeswap = conv_endian(info.freeswap);
1069 info.procs = conv_endian16(info.procs);
1070 #if LINUX_VERSION_CODE >= 0x20400
1071 info.totalhigh = conv_endian(info.totalhigh);
1072 info.freehigh = conv_endian(info.freehigh);
1073 info.mem_unit = conv_endian(info.mem_unit);
1074 #endif
1075 if ((s.write_mem) (cb, &s, arg1, (char *) &info, sizeof(info))
1076 != sizeof(info))
1078 result = -1;
1079 errcode = EINVAL;
1082 break;
1084 #if 0
1085 case __NR_ipc:
1087 result = ipc(arg1, arg2, arg3, arg4,
1088 (void *) t2h_addr(cb, &s, arg5), arg6);
1089 errcode = errno;
1091 break;
1092 #endif
1094 case __NR_fsync:
1095 result = fsync(arg1);
1096 errcode = errno;
1097 break;
1099 case __NR_uname:
1100 /* utsname contains only arrays of char, so it is not necessary
1101 to translate endian. */
1102 result = uname((struct utsname *) t2h_addr(cb, &s, arg1));
1103 errcode = errno;
1104 break;
1106 case __NR_adjtimex:
1108 struct timex buf;
1110 result = adjtimex(&buf);
1111 errcode = errno;
1113 if (result != 0)
1114 break;
1116 translate_endian((void *) &buf, sizeof(buf));
1117 if ((s.write_mem) (cb, &s, arg1, (char *) &buf, sizeof(buf))
1118 != sizeof(buf))
1120 result = -1;
1121 errcode = EINVAL;
1124 break;
1126 case __NR_mprotect:
1127 result = mprotect((void *) arg1, arg2, arg3);
1128 errcode = errno;
1129 break;
1131 case __NR_fchdir:
1132 result = fchdir(arg1);
1133 errcode = errno;
1134 break;
1136 case __NR_setfsuid32:
1137 case __NR_setfsuid:
1138 result = setfsuid(arg1);
1139 errcode = errno;
1140 break;
1142 case __NR_setfsgid32:
1143 case __NR_setfsgid:
1144 result = setfsgid(arg1);
1145 errcode = errno;
1146 break;
1148 #if 0
1149 case __NR__llseek:
1151 loff_t buf;
1153 result = _llseek(arg1, arg2, arg3, &buf, arg5);
1154 errcode = errno;
1156 if (result != 0)
1157 break;
1159 translate_endian((void *) &buf, sizeof(buf));
1160 if ((s.write_mem) (cb, &s, t2h_addr(cb, &s, arg4),
1161 (char *) &buf, sizeof(buf)) != sizeof(buf))
1163 result = -1;
1164 errcode = EINVAL;
1167 break;
1169 case __NR_getdents:
1171 struct dirent dir;
1173 result = getdents(arg1, &dir, arg3);
1174 errcode = errno;
1176 if (result != 0)
1177 break;
1179 dir.d_ino = conv_endian(dir.d_ino);
1180 dir.d_off = conv_endian(dir.d_off);
1181 dir.d_reclen = conv_endian16(dir.d_reclen);
1182 if ((s.write_mem) (cb, &s, arg2, (char *) &dir, sizeof(dir))
1183 != sizeof(dir))
1185 result = -1;
1186 errcode = EINVAL;
1189 break;
1190 #endif
1192 case __NR_flock:
1193 result = flock(arg1, arg2);
1194 errcode = errno;
1195 break;
1197 case __NR_msync:
1198 result = msync((void *) arg1, arg2, arg3);
1199 errcode = errno;
1200 break;
1202 case __NR_readv:
1204 struct iovec vector;
1206 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1207 translate_endian((void *) &vector, sizeof(vector));
1209 result = readv(arg1, &vector, arg3);
1210 errcode = errno;
1212 break;
1214 case __NR_writev:
1216 struct iovec vector;
1218 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1219 translate_endian((void *) &vector, sizeof(vector));
1221 result = writev(arg1, &vector, arg3);
1222 errcode = errno;
1224 break;
1226 case __NR_fdatasync:
1227 result = fdatasync(arg1);
1228 errcode = errno;
1229 break;
1231 case __NR_mlock:
1232 result = mlock((void *) t2h_addr(cb, &s, arg1), arg2);
1233 errcode = errno;
1234 break;
1236 case __NR_munlock:
1237 result = munlock((void *) t2h_addr(cb, &s, arg1), arg2);
1238 errcode = errno;
1239 break;
1241 case __NR_nanosleep:
1243 struct timespec req, rem;
1245 req = *((struct timespec *) t2h_addr(cb, &s, arg2));
1246 translate_endian((void *) &req, sizeof(req));
1248 result = nanosleep(&req, &rem);
1249 errcode = errno;
1251 if (result != 0)
1252 break;
1254 translate_endian((void *) &rem, sizeof(rem));
1255 if ((s.write_mem) (cb, &s, arg2, (char *) &rem, sizeof(rem))
1256 != sizeof(rem))
1258 result = -1;
1259 errcode = EINVAL;
1262 break;
1264 case __NR_mremap: /* FIXME */
1265 result = (int) mremap((void *) t2h_addr(cb, &s, arg1), arg2, arg3, arg4);
1266 errcode = errno;
1267 break;
1269 case __NR_getresuid32:
1270 case __NR_getresuid:
1272 uid_t ruid, euid, suid;
1274 result = getresuid(&ruid, &euid, &suid);
1275 errcode = errno;
1277 if (result != 0)
1278 break;
1280 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(ruid);
1281 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(euid);
1282 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(suid);
1284 break;
1286 case __NR_poll:
1288 struct pollfd ufds;
1290 ufds = *((struct pollfd *) t2h_addr(cb, &s, arg1));
1291 ufds.fd = conv_endian(ufds.fd);
1292 ufds.events = conv_endian16(ufds.events);
1293 ufds.revents = conv_endian16(ufds.revents);
1295 result = poll(&ufds, arg2, arg3);
1296 errcode = errno;
1298 break;
1300 case __NR_getresgid32:
1301 case __NR_getresgid:
1303 uid_t rgid, egid, sgid;
1305 result = getresgid(&rgid, &egid, &sgid);
1306 errcode = errno;
1308 if (result != 0)
1309 break;
1311 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(rgid);
1312 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(egid);
1313 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(sgid);
1315 break;
1317 case __NR_pread:
1318 result = pread(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1319 errcode = errno;
1320 break;
1322 case __NR_pwrite:
1323 result = pwrite(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1324 errcode = errno;
1325 break;
1327 case __NR_chown32:
1328 case __NR_chown:
1329 result = chown((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
1330 errcode = errno;
1331 break;
1333 case __NR_getcwd:
1334 result = (int) getcwd((char *) t2h_addr(cb, &s, arg1), arg2);
1335 errcode = errno;
1336 break;
1338 case __NR_sendfile:
1340 off_t offset;
1342 offset = *((off_t *) t2h_addr(cb, &s, arg3));
1343 offset = conv_endian(offset);
1345 result = sendfile(arg1, arg2, &offset, arg3);
1346 errcode = errno;
1348 if (result != 0)
1349 break;
1351 *((off_t *) t2h_addr(cb, &s, arg3)) = conv_endian(offset);
1353 break;
1355 default:
1356 result = -1;
1357 errcode = ENOSYS;
1358 break;
1361 if (result == -1)
1362 m32rbf_h_gr_set (current_cpu, 0, -errcode);
1363 else
1364 m32rbf_h_gr_set (current_cpu, 0, result);
1365 break;
1368 case TRAP_BREAKPOINT:
1369 sim_engine_halt (sd, current_cpu, NULL, pc,
1370 sim_stopped, SIM_SIGTRAP);
1371 break;
1373 case TRAP_FLUSH_CACHE:
1374 /* Do nothing. */
1375 break;
1377 default :
1379 /* Use cr5 as EVB (EIT Vector Base) register. */
1380 USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
1381 return new_pc;
1385 /* Fake an "rte" insn. */
1386 /* FIXME: Should duplicate all of rte processing. */
1387 return (pc & -4) + 4;