sim: callback: extend syscall interface to handle 7 args
[binutils-gdb.git] / sim / m32r / traps-linux.c
1 /* m32r exception, interrupt, and trap (EIT) support
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 Contributed by Renesas.
4
5 This file is part of GDB, the GNU debugger.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* This must come before any other includes. */
21 #include "defs.h"
22
23 #include "portability.h"
24 #include "sim-main.h"
25 #include "sim-signal.h"
26 #include "sim-syscall.h"
27 #include "syscall.h"
28 #include "targ-vals.h"
29 #include <dirent.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <utime.h>
35 #include <sys/mman.h>
36 #include <sys/poll.h>
37 #include <sys/resource.h>
38 #include <sys/sysinfo.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/timeb.h>
42 #include <sys/timex.h>
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 #include <sys/utsname.h>
46 #include <sys/vfs.h>
47 #include <linux/sysctl.h>
48 #include <linux/types.h>
49 #include <linux/unistd.h>
50
51 #define TRAP_ELF_SYSCALL 0
52 #define TRAP_LINUX_SYSCALL 2
53 #define TRAP_FLUSH_CACHE 12
54
55 /* The semantic code invokes this for invalid (unrecognized) instructions. */
56
57 SEM_PC
58 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
59 {
60 SIM_DESC sd = CPU_STATE (current_cpu);
61
62 #if 0
63 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
64 {
65 h_bsm_set (current_cpu, h_sm_get (current_cpu));
66 h_bie_set (current_cpu, h_ie_get (current_cpu));
67 h_bcond_set (current_cpu, h_cond_get (current_cpu));
68 /* sm not changed */
69 h_ie_set (current_cpu, 0);
70 h_cond_set (current_cpu, 0);
71
72 h_bpc_set (current_cpu, cia);
73
74 sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
75 EIT_RSVD_INSN_ADDR);
76 }
77 else
78 #endif
79 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
80 return vpc;
81 }
82
83 /* Process an address exception. */
84
85 void
86 m32r_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
87 unsigned int map, int nr_bytes, address_word addr,
88 transfer_type transfer, sim_core_signals sig)
89 {
90 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
91 {
92 m32rbf_h_cr_set (current_cpu, H_CR_BBPC,
93 m32rbf_h_cr_get (current_cpu, H_CR_BPC));
94 if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32R)
95 {
96 m32rbf_h_bpsw_set (current_cpu, m32rbf_h_psw_get (current_cpu));
97 /* sm not changed */
98 m32rbf_h_psw_set (current_cpu, m32rbf_h_psw_get (current_cpu) & 0x80);
99 }
100 else if (MACH_NUM (CPU_MACH (current_cpu)) == MACH_M32RX)
101 {
102 m32rxf_h_bpsw_set (current_cpu, m32rxf_h_psw_get (current_cpu));
103 /* sm not changed */
104 m32rxf_h_psw_set (current_cpu, m32rxf_h_psw_get (current_cpu) & 0x80);
105 }
106 else
107 {
108 m32r2f_h_bpsw_set (current_cpu, m32r2f_h_psw_get (current_cpu));
109 /* sm not changed */
110 m32r2f_h_psw_set (current_cpu, m32r2f_h_psw_get (current_cpu) & 0x80);
111 }
112 m32rbf_h_cr_set (current_cpu, H_CR_BPC, cia);
113
114 sim_engine_restart (CPU_STATE (current_cpu), current_cpu, NULL,
115 EIT_ADDR_EXCP_ADDR);
116 }
117 else
118 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
119 transfer, sig);
120 }
121 \f
122 /* Translate target's address to host's address. */
123
124 static void *
125 t2h_addr (host_callback *cb, struct cb_syscall *sc,
126 unsigned long taddr)
127 {
128 void *addr;
129 SIM_DESC sd = (SIM_DESC) sc->p1;
130 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
131
132 if (taddr == 0)
133 return NULL;
134
135 return sim_core_trans_addr (sd, cpu, read_map, taddr);
136 }
137
138 static unsigned int
139 conv_endian (unsigned int tvalue)
140 {
141 unsigned int hvalue;
142 unsigned int t1, t2, t3, t4;
143
144 if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
145 {
146 t1 = tvalue & 0xff000000;
147 t2 = tvalue & 0x00ff0000;
148 t3 = tvalue & 0x0000ff00;
149 t4 = tvalue & 0x000000ff;
150
151 hvalue = t1 >> 24;
152 hvalue += t2 >> 8;
153 hvalue += t3 << 8;
154 hvalue += t4 << 24;
155 }
156 else
157 hvalue = tvalue;
158
159 return hvalue;
160 }
161
162 static unsigned short
163 conv_endian16 (unsigned short tvalue)
164 {
165 unsigned short hvalue;
166 unsigned short t1, t2;
167
168 if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
169 {
170 t1 = tvalue & 0xff00;
171 t2 = tvalue & 0x00ff;
172
173 hvalue = t1 >> 8;
174 hvalue += t2 << 8;
175 }
176 else
177 hvalue = tvalue;
178
179 return hvalue;
180 }
181
182 static void
183 translate_endian(void *addr, size_t size)
184 {
185 unsigned int *p = (unsigned int *) addr;
186 int i;
187
188 for (i = 0; i <= size - 4; i += 4,p++)
189 *p = conv_endian(*p);
190
191 if (i <= size - 2)
192 *((unsigned short *) p) = conv_endian16(*((unsigned short *) p));
193 }
194
195 /* Trap support.
196 The result is the pc address to continue at.
197 Preprocessing like saving the various registers has already been done. */
198
199 USI
200 m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
201 {
202 SIM_DESC sd = CPU_STATE (current_cpu);
203 host_callback *cb = STATE_CALLBACK (sd);
204
205 switch (num)
206 {
207 case TRAP_ELF_SYSCALL :
208 {
209 long result, result2;
210 int errcode;
211
212 sim_syscall_multi (current_cpu,
213 m32rbf_h_gr_get (current_cpu, 0),
214 m32rbf_h_gr_get (current_cpu, 1),
215 m32rbf_h_gr_get (current_cpu, 2),
216 m32rbf_h_gr_get (current_cpu, 3),
217 m32rbf_h_gr_get (current_cpu, 4),
218 &result, &result2, &errcode);
219
220 m32rbf_h_gr_set (current_cpu, 2, errcode);
221 m32rbf_h_gr_set (current_cpu, 0, result);
222 m32rbf_h_gr_set (current_cpu, 1, result2);
223 break;
224 }
225
226 case TRAP_LINUX_SYSCALL :
227 {
228 CB_SYSCALL s;
229 unsigned int func, arg1, arg2, arg3, arg4, arg5, arg6, arg7;
230 int result, result2, errcode;
231
232 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
233 {
234 /* The new pc is the trap vector entry.
235 We assume there's a branch there to some handler.
236 Use cr5 as EVB (EIT Vector Base) register. */
237 USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
238 return new_pc;
239 }
240
241 func = m32rbf_h_gr_get (current_cpu, 7);
242 arg1 = m32rbf_h_gr_get (current_cpu, 0);
243 arg2 = m32rbf_h_gr_get (current_cpu, 1);
244 arg3 = m32rbf_h_gr_get (current_cpu, 2);
245 arg4 = m32rbf_h_gr_get (current_cpu, 3);
246 arg5 = m32rbf_h_gr_get (current_cpu, 4);
247 arg6 = m32rbf_h_gr_get (current_cpu, 5);
248 arg7 = m32rbf_h_gr_get (current_cpu, 6);
249
250 CB_SYSCALL_INIT (&s);
251 s.func = func;
252 s.arg1 = arg1;
253 s.arg2 = arg2;
254 s.arg3 = arg3;
255 s.arg4 = arg4;
256 s.arg5 = arg5;
257 s.arg6 = arg6;
258 s.arg7 = arg7;
259
260 s.p1 = (PTR) sd;
261 s.p2 = (PTR) current_cpu;
262 s.read_mem = sim_syscall_read_mem;
263 s.write_mem = sim_syscall_write_mem;
264
265 result = 0;
266 result2 = 0;
267 errcode = 0;
268
269 switch (func)
270 {
271 case __NR_exit:
272 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
273 break;
274
275 case __NR_read:
276 result = read(arg1, t2h_addr(cb, &s, arg2), arg3);
277 errcode = errno;
278 break;
279
280 case __NR_write:
281 result = write(arg1, t2h_addr(cb, &s, arg2), arg3);
282 errcode = errno;
283 break;
284
285 case __NR_open:
286 result = open((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
287 errcode = errno;
288 break;
289
290 case __NR_close:
291 result = close(arg1);
292 errcode = errno;
293 break;
294
295 case __NR_creat:
296 result = creat((char *) t2h_addr(cb, &s, arg1), arg2);
297 errcode = errno;
298 break;
299
300 case __NR_link:
301 result = link((char *) t2h_addr(cb, &s, arg1),
302 (char *) t2h_addr(cb, &s, arg2));
303 errcode = errno;
304 break;
305
306 case __NR_unlink:
307 result = unlink((char *) t2h_addr(cb, &s, arg1));
308 errcode = errno;
309 break;
310
311 case __NR_chdir:
312 result = chdir((char *) t2h_addr(cb, &s, arg1));
313 errcode = errno;
314 break;
315
316 case __NR_time:
317 {
318 time_t t;
319
320 if (arg1 == 0)
321 {
322 result = (int) time(NULL);
323 errcode = errno;
324 }
325 else
326 {
327 result = (int) time(&t);
328 errcode = errno;
329
330 if (result != 0)
331 break;
332
333 translate_endian((void *) &t, sizeof(t));
334 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t)) != sizeof(t))
335 {
336 result = -1;
337 errcode = EINVAL;
338 }
339 }
340 }
341 break;
342
343 case __NR_mknod:
344 result = mknod((char *) t2h_addr(cb, &s, arg1),
345 (mode_t) arg2, (dev_t) arg3);
346 errcode = errno;
347 break;
348
349 case __NR_chmod:
350 result = chmod((char *) t2h_addr(cb, &s, arg1), (mode_t) arg2);
351 errcode = errno;
352 break;
353
354 case __NR_lchown32:
355 case __NR_lchown:
356 result = lchown((char *) t2h_addr(cb, &s, arg1),
357 (uid_t) arg2, (gid_t) arg3);
358 errcode = errno;
359 break;
360
361 case __NR_lseek:
362 result = (int) lseek(arg1, (off_t) arg2, arg3);
363 errcode = errno;
364 break;
365
366 case __NR_getpid:
367 result = getpid();
368 errcode = errno;
369 break;
370
371 case __NR_getuid32:
372 case __NR_getuid:
373 result = getuid();
374 errcode = errno;
375 break;
376
377 case __NR_utime:
378 {
379 struct utimbuf buf;
380
381 if (arg2 == 0)
382 {
383 result = utime((char *) t2h_addr(cb, &s, arg1), NULL);
384 errcode = errno;
385 }
386 else
387 {
388 buf = *((struct utimbuf *) t2h_addr(cb, &s, arg2));
389 translate_endian((void *) &buf, sizeof(buf));
390 result = utime((char *) t2h_addr(cb, &s, arg1), &buf);
391 errcode = errno;
392 }
393 }
394 break;
395
396 case __NR_access:
397 result = access((char *) t2h_addr(cb, &s, arg1), arg2);
398 errcode = errno;
399 break;
400
401 case __NR_ftime:
402 {
403 struct timeb t;
404
405 result = ftime(&t);
406 errcode = errno;
407
408 if (result != 0)
409 break;
410
411 t.time = conv_endian(t.time);
412 t.millitm = conv_endian16(t.millitm);
413 t.timezone = conv_endian16(t.timezone);
414 t.dstflag = conv_endian16(t.dstflag);
415 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t))
416 != sizeof(t))
417 {
418 result = -1;
419 errcode = EINVAL;
420 }
421 }
422
423 case __NR_sync:
424 sync();
425 result = 0;
426 break;
427
428 case __NR_rename:
429 result = rename((char *) t2h_addr(cb, &s, arg1),
430 (char *) t2h_addr(cb, &s, arg2));
431 errcode = errno;
432 break;
433
434 case __NR_mkdir:
435 result = mkdir((char *) t2h_addr(cb, &s, arg1), arg2);
436 errcode = errno;
437 break;
438
439 case __NR_rmdir:
440 result = rmdir((char *) t2h_addr(cb, &s, arg1));
441 errcode = errno;
442 break;
443
444 case __NR_dup:
445 result = dup(arg1);
446 errcode = errno;
447 break;
448
449 case __NR_brk:
450 result = brk((void *) arg1);
451 errcode = errno;
452 //result = arg1;
453 break;
454
455 case __NR_getgid32:
456 case __NR_getgid:
457 result = getgid();
458 errcode = errno;
459 break;
460
461 case __NR_geteuid32:
462 case __NR_geteuid:
463 result = geteuid();
464 errcode = errno;
465 break;
466
467 case __NR_getegid32:
468 case __NR_getegid:
469 result = getegid();
470 errcode = errno;
471 break;
472
473 case __NR_ioctl:
474 result = ioctl(arg1, arg2, arg3);
475 errcode = errno;
476 break;
477
478 case __NR_fcntl:
479 result = fcntl(arg1, arg2, arg3);
480 errcode = errno;
481 break;
482
483 case __NR_dup2:
484 result = dup2(arg1, arg2);
485 errcode = errno;
486 break;
487
488 case __NR_getppid:
489 result = getppid();
490 errcode = errno;
491 break;
492
493 case __NR_getpgrp:
494 result = getpgrp();
495 errcode = errno;
496 break;
497
498 case __NR_getrlimit:
499 {
500 struct rlimit rlim;
501
502 result = getrlimit(arg1, &rlim);
503 errcode = errno;
504
505 if (result != 0)
506 break;
507
508 translate_endian((void *) &rlim, sizeof(rlim));
509 if ((s.write_mem) (cb, &s, arg2, (char *) &rlim, sizeof(rlim))
510 != sizeof(rlim))
511 {
512 result = -1;
513 errcode = EINVAL;
514 }
515 }
516 break;
517
518 case __NR_getrusage:
519 {
520 struct rusage usage;
521
522 result = getrusage(arg1, &usage);
523 errcode = errno;
524
525 if (result != 0)
526 break;
527
528 translate_endian((void *) &usage, sizeof(usage));
529 if ((s.write_mem) (cb, &s, arg2, (char *) &usage, sizeof(usage))
530 != sizeof(usage))
531 {
532 result = -1;
533 errcode = EINVAL;
534 }
535 }
536 break;
537
538 case __NR_gettimeofday:
539 {
540 struct timeval tv;
541 struct timezone tz;
542
543 result = gettimeofday(&tv, &tz);
544 errcode = errno;
545
546 if (result != 0)
547 break;
548
549 translate_endian((void *) &tv, sizeof(tv));
550 if ((s.write_mem) (cb, &s, arg1, (char *) &tv, sizeof(tv))
551 != sizeof(tv))
552 {
553 result = -1;
554 errcode = EINVAL;
555 }
556
557 translate_endian((void *) &tz, sizeof(tz));
558 if ((s.write_mem) (cb, &s, arg2, (char *) &tz, sizeof(tz))
559 != sizeof(tz))
560 {
561 result = -1;
562 errcode = EINVAL;
563 }
564 }
565 break;
566
567 case __NR_getgroups32:
568 case __NR_getgroups:
569 {
570 gid_t *list;
571
572 if (arg1 > 0)
573 list = (gid_t *) malloc(arg1 * sizeof(gid_t));
574
575 result = getgroups(arg1, list);
576 errcode = errno;
577
578 if (result != 0)
579 break;
580
581 translate_endian((void *) list, arg1 * sizeof(gid_t));
582 if (arg1 > 0)
583 if ((s.write_mem) (cb, &s, arg2, (char *) list, arg1 * sizeof(gid_t))
584 != arg1 * sizeof(gid_t))
585 {
586 result = -1;
587 errcode = EINVAL;
588 }
589 }
590 break;
591
592 case __NR_select:
593 {
594 int n;
595 fd_set readfds;
596 fd_set *treadfdsp;
597 fd_set *hreadfdsp;
598 fd_set writefds;
599 fd_set *twritefdsp;
600 fd_set *hwritefdsp;
601 fd_set exceptfds;
602 fd_set *texceptfdsp;
603 fd_set *hexceptfdsp;
604 struct timeval *ttimeoutp;
605 struct timeval timeout;
606
607 n = arg1;
608
609 treadfdsp = (fd_set *) arg2;
610 if (treadfdsp != NULL)
611 {
612 readfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) treadfdsp));
613 translate_endian((void *) &readfds, sizeof(readfds));
614 hreadfdsp = &readfds;
615 }
616 else
617 hreadfdsp = NULL;
618
619 twritefdsp = (fd_set *) arg3;
620 if (twritefdsp != NULL)
621 {
622 writefds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) twritefdsp));
623 translate_endian((void *) &writefds, sizeof(writefds));
624 hwritefdsp = &writefds;
625 }
626 else
627 hwritefdsp = NULL;
628
629 texceptfdsp = (fd_set *) arg4;
630 if (texceptfdsp != NULL)
631 {
632 exceptfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) texceptfdsp));
633 translate_endian((void *) &exceptfds, sizeof(exceptfds));
634 hexceptfdsp = &exceptfds;
635 }
636 else
637 hexceptfdsp = NULL;
638
639 ttimeoutp = (struct timeval *) arg5;
640 timeout = *((struct timeval *) t2h_addr(cb, &s, (unsigned int) ttimeoutp));
641 translate_endian((void *) &timeout, sizeof(timeout));
642
643 result = select(n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
644 errcode = errno;
645
646 if (result != 0)
647 break;
648
649 if (treadfdsp != NULL)
650 {
651 translate_endian((void *) &readfds, sizeof(readfds));
652 if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
653 (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
654 {
655 result = -1;
656 errcode = EINVAL;
657 }
658 }
659
660 if (twritefdsp != NULL)
661 {
662 translate_endian((void *) &writefds, sizeof(writefds));
663 if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
664 (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
665 {
666 result = -1;
667 errcode = EINVAL;
668 }
669 }
670
671 if (texceptfdsp != NULL)
672 {
673 translate_endian((void *) &exceptfds, sizeof(exceptfds));
674 if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
675 (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
676 {
677 result = -1;
678 errcode = EINVAL;
679 }
680 }
681
682 translate_endian((void *) &timeout, sizeof(timeout));
683 if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
684 (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
685 {
686 result = -1;
687 errcode = EINVAL;
688 }
689 }
690 break;
691
692 case __NR_symlink:
693 result = symlink((char *) t2h_addr(cb, &s, arg1),
694 (char *) t2h_addr(cb, &s, arg2));
695 errcode = errno;
696 break;
697
698 case __NR_readlink:
699 result = readlink((char *) t2h_addr(cb, &s, arg1),
700 (char *) t2h_addr(cb, &s, arg2),
701 arg3);
702 errcode = errno;
703 break;
704
705 case __NR_readdir:
706 result = (int) readdir((DIR *) t2h_addr(cb, &s, arg1));
707 errcode = errno;
708 break;
709
710 #if 0
711 case __NR_mmap:
712 {
713 result = (int) mmap((void *) t2h_addr(cb, &s, arg1),
714 arg2, arg3, arg4, arg5, arg6);
715 errcode = errno;
716
717 if (errno == 0)
718 {
719 sim_core_attach (sd, NULL,
720 0, access_read_write_exec, 0,
721 result, arg2, 0, NULL, NULL);
722 }
723 }
724 break;
725 #endif
726 case __NR_mmap2:
727 {
728 void *addr;
729 size_t len;
730 int prot, flags, fildes;
731 off_t off;
732
733 addr = (void *) t2h_addr(cb, &s, arg1);
734 len = arg2;
735 prot = arg3;
736 flags = arg4;
737 fildes = arg5;
738 off = arg6 << 12;
739
740 result = (int) mmap(addr, len, prot, flags, fildes, off);
741 errcode = errno;
742 if (result != -1)
743 {
744 char c;
745 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
746 sim_core_attach (sd, NULL,
747 0, access_read_write_exec, 0,
748 result, len, 0, NULL, NULL);
749 }
750 }
751 break;
752
753 case __NR_mmap:
754 {
755 void *addr;
756 size_t len;
757 int prot, flags, fildes;
758 off_t off;
759
760 addr = *((void **) t2h_addr(cb, &s, arg1));
761 len = *((size_t *) t2h_addr(cb, &s, arg1 + 4));
762 prot = *((int *) t2h_addr(cb, &s, arg1 + 8));
763 flags = *((int *) t2h_addr(cb, &s, arg1 + 12));
764 fildes = *((int *) t2h_addr(cb, &s, arg1 + 16));
765 off = *((off_t *) t2h_addr(cb, &s, arg1 + 20));
766
767 addr = (void *) conv_endian((unsigned int) addr);
768 len = conv_endian(len);
769 prot = conv_endian(prot);
770 flags = conv_endian(flags);
771 fildes = conv_endian(fildes);
772 off = conv_endian(off);
773
774 //addr = (void *) t2h_addr(cb, &s, (unsigned int) addr);
775 result = (int) mmap(addr, len, prot, flags, fildes, off);
776 errcode = errno;
777
778 //if (errno == 0)
779 if (result != -1)
780 {
781 char c;
782 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
783 sim_core_attach (sd, NULL,
784 0, access_read_write_exec, 0,
785 result, len, 0, NULL, NULL);
786 }
787 }
788 break;
789
790 case __NR_munmap:
791 {
792 result = munmap((void *)arg1, arg2);
793 errcode = errno;
794 if (result != -1)
795 {
796 sim_core_detach (sd, NULL, 0, arg2, result);
797 }
798 }
799 break;
800
801 case __NR_truncate:
802 result = truncate((char *) t2h_addr(cb, &s, arg1), arg2);
803 errcode = errno;
804 break;
805
806 case __NR_ftruncate:
807 result = ftruncate(arg1, arg2);
808 errcode = errno;
809 break;
810
811 case __NR_fchmod:
812 result = fchmod(arg1, arg2);
813 errcode = errno;
814 break;
815
816 case __NR_fchown32:
817 case __NR_fchown:
818 result = fchown(arg1, arg2, arg3);
819 errcode = errno;
820 break;
821
822 case __NR_statfs:
823 {
824 struct statfs statbuf;
825
826 result = statfs((char *) t2h_addr(cb, &s, arg1), &statbuf);
827 errcode = errno;
828
829 if (result != 0)
830 break;
831
832 translate_endian((void *) &statbuf, sizeof(statbuf));
833 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
834 != sizeof(statbuf))
835 {
836 result = -1;
837 errcode = EINVAL;
838 }
839 }
840 break;
841
842 case __NR_fstatfs:
843 {
844 struct statfs statbuf;
845
846 result = fstatfs(arg1, &statbuf);
847 errcode = errno;
848
849 if (result != 0)
850 break;
851
852 translate_endian((void *) &statbuf, sizeof(statbuf));
853 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
854 != sizeof(statbuf))
855 {
856 result = -1;
857 errcode = EINVAL;
858 }
859 }
860 break;
861
862 case __NR_syslog:
863 result = syslog(arg1, (char *) t2h_addr(cb, &s, arg2));
864 errcode = errno;
865 break;
866
867 case __NR_setitimer:
868 {
869 struct itimerval value, ovalue;
870
871 value = *((struct itimerval *) t2h_addr(cb, &s, arg2));
872 translate_endian((void *) &value, sizeof(value));
873
874 if (arg2 == 0)
875 {
876 result = setitimer(arg1, &value, NULL);
877 errcode = errno;
878 }
879 else
880 {
881 result = setitimer(arg1, &value, &ovalue);
882 errcode = errno;
883
884 if (result != 0)
885 break;
886
887 translate_endian((void *) &ovalue, sizeof(ovalue));
888 if ((s.write_mem) (cb, &s, arg3, (char *) &ovalue, sizeof(ovalue))
889 != sizeof(ovalue))
890 {
891 result = -1;
892 errcode = EINVAL;
893 }
894 }
895 }
896 break;
897
898 case __NR_getitimer:
899 {
900 struct itimerval value;
901
902 result = getitimer(arg1, &value);
903 errcode = errno;
904
905 if (result != 0)
906 break;
907
908 translate_endian((void *) &value, sizeof(value));
909 if ((s.write_mem) (cb, &s, arg2, (char *) &value, sizeof(value))
910 != sizeof(value))
911 {
912 result = -1;
913 errcode = EINVAL;
914 }
915 }
916 break;
917
918 case __NR_stat:
919 {
920 char *buf;
921 int buflen;
922 struct stat statbuf;
923
924 result = stat((char *) t2h_addr(cb, &s, arg1), &statbuf);
925 errcode = errno;
926 if (result < 0)
927 break;
928
929 buflen = cb_host_to_target_stat (cb, NULL, NULL);
930 buf = xmalloc (buflen);
931 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
932 {
933 /* The translation failed. This is due to an internal
934 host program error, not the target's fault. */
935 free (buf);
936 result = -1;
937 errcode = ENOSYS;
938 break;
939 }
940 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
941 {
942 free (buf);
943 result = -1;
944 errcode = EINVAL;
945 break;
946 }
947 free (buf);
948 }
949 break;
950
951 case __NR_lstat:
952 {
953 char *buf;
954 int buflen;
955 struct stat statbuf;
956
957 result = lstat((char *) t2h_addr(cb, &s, arg1), &statbuf);
958 errcode = errno;
959 if (result < 0)
960 break;
961
962 buflen = cb_host_to_target_stat (cb, NULL, NULL);
963 buf = xmalloc (buflen);
964 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
965 {
966 /* The translation failed. This is due to an internal
967 host program error, not the target's fault. */
968 free (buf);
969 result = -1;
970 errcode = ENOSYS;
971 break;
972 }
973 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
974 {
975 free (buf);
976 result = -1;
977 errcode = EINVAL;
978 break;
979 }
980 free (buf);
981 }
982 break;
983
984 case __NR_fstat:
985 {
986 char *buf;
987 int buflen;
988 struct stat statbuf;
989
990 result = fstat(arg1, &statbuf);
991 errcode = errno;
992 if (result < 0)
993 break;
994
995 buflen = cb_host_to_target_stat (cb, NULL, NULL);
996 buf = xmalloc (buflen);
997 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
998 {
999 /* The translation failed. This is due to an internal
1000 host program error, not the target's fault. */
1001 free (buf);
1002 result = -1;
1003 errcode = ENOSYS;
1004 break;
1005 }
1006 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
1007 {
1008 free (buf);
1009 result = -1;
1010 errcode = EINVAL;
1011 break;
1012 }
1013 free (buf);
1014 }
1015 break;
1016
1017 case __NR_sysinfo:
1018 {
1019 struct sysinfo info;
1020
1021 result = sysinfo(&info);
1022 errcode = errno;
1023
1024 if (result != 0)
1025 break;
1026
1027 info.uptime = conv_endian(info.uptime);
1028 info.loads[0] = conv_endian(info.loads[0]);
1029 info.loads[1] = conv_endian(info.loads[1]);
1030 info.loads[2] = conv_endian(info.loads[2]);
1031 info.totalram = conv_endian(info.totalram);
1032 info.freeram = conv_endian(info.freeram);
1033 info.sharedram = conv_endian(info.sharedram);
1034 info.bufferram = conv_endian(info.bufferram);
1035 info.totalswap = conv_endian(info.totalswap);
1036 info.freeswap = conv_endian(info.freeswap);
1037 info.procs = conv_endian16(info.procs);
1038 #if LINUX_VERSION_CODE >= 0x20400
1039 info.totalhigh = conv_endian(info.totalhigh);
1040 info.freehigh = conv_endian(info.freehigh);
1041 info.mem_unit = conv_endian(info.mem_unit);
1042 #endif
1043 if ((s.write_mem) (cb, &s, arg1, (char *) &info, sizeof(info))
1044 != sizeof(info))
1045 {
1046 result = -1;
1047 errcode = EINVAL;
1048 }
1049 }
1050 break;
1051
1052 #if 0
1053 case __NR_ipc:
1054 {
1055 result = ipc(arg1, arg2, arg3, arg4,
1056 (void *) t2h_addr(cb, &s, arg5), arg6);
1057 errcode = errno;
1058 }
1059 break;
1060 #endif
1061
1062 case __NR_fsync:
1063 result = fsync(arg1);
1064 errcode = errno;
1065 break;
1066
1067 case __NR_uname:
1068 /* utsname contains only arrays of char, so it is not necessary
1069 to translate endian. */
1070 result = uname((struct utsname *) t2h_addr(cb, &s, arg1));
1071 errcode = errno;
1072 break;
1073
1074 case __NR_adjtimex:
1075 {
1076 struct timex buf;
1077
1078 result = adjtimex(&buf);
1079 errcode = errno;
1080
1081 if (result != 0)
1082 break;
1083
1084 translate_endian((void *) &buf, sizeof(buf));
1085 if ((s.write_mem) (cb, &s, arg1, (char *) &buf, sizeof(buf))
1086 != sizeof(buf))
1087 {
1088 result = -1;
1089 errcode = EINVAL;
1090 }
1091 }
1092 break;
1093
1094 case __NR_mprotect:
1095 result = mprotect((void *) arg1, arg2, arg3);
1096 errcode = errno;
1097 break;
1098
1099 case __NR_fchdir:
1100 result = fchdir(arg1);
1101 errcode = errno;
1102 break;
1103
1104 case __NR_setfsuid32:
1105 case __NR_setfsuid:
1106 result = setfsuid(arg1);
1107 errcode = errno;
1108 break;
1109
1110 case __NR_setfsgid32:
1111 case __NR_setfsgid:
1112 result = setfsgid(arg1);
1113 errcode = errno;
1114 break;
1115
1116 #if 0
1117 case __NR__llseek:
1118 {
1119 loff_t buf;
1120
1121 result = _llseek(arg1, arg2, arg3, &buf, arg5);
1122 errcode = errno;
1123
1124 if (result != 0)
1125 break;
1126
1127 translate_endian((void *) &buf, sizeof(buf));
1128 if ((s.write_mem) (cb, &s, t2h_addr(cb, &s, arg4),
1129 (char *) &buf, sizeof(buf)) != sizeof(buf))
1130 {
1131 result = -1;
1132 errcode = EINVAL;
1133 }
1134 }
1135 break;
1136
1137 case __NR_getdents:
1138 {
1139 struct dirent dir;
1140
1141 result = getdents(arg1, &dir, arg3);
1142 errcode = errno;
1143
1144 if (result != 0)
1145 break;
1146
1147 dir.d_ino = conv_endian(dir.d_ino);
1148 dir.d_off = conv_endian(dir.d_off);
1149 dir.d_reclen = conv_endian16(dir.d_reclen);
1150 if ((s.write_mem) (cb, &s, arg2, (char *) &dir, sizeof(dir))
1151 != sizeof(dir))
1152 {
1153 result = -1;
1154 errcode = EINVAL;
1155 }
1156 }
1157 break;
1158 #endif
1159
1160 case __NR_flock:
1161 result = flock(arg1, arg2);
1162 errcode = errno;
1163 break;
1164
1165 case __NR_msync:
1166 result = msync((void *) arg1, arg2, arg3);
1167 errcode = errno;
1168 break;
1169
1170 case __NR_readv:
1171 {
1172 struct iovec vector;
1173
1174 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1175 translate_endian((void *) &vector, sizeof(vector));
1176
1177 result = readv(arg1, &vector, arg3);
1178 errcode = errno;
1179 }
1180 break;
1181
1182 case __NR_writev:
1183 {
1184 struct iovec vector;
1185
1186 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1187 translate_endian((void *) &vector, sizeof(vector));
1188
1189 result = writev(arg1, &vector, arg3);
1190 errcode = errno;
1191 }
1192 break;
1193
1194 case __NR_fdatasync:
1195 result = fdatasync(arg1);
1196 errcode = errno;
1197 break;
1198
1199 case __NR_mlock:
1200 result = mlock((void *) t2h_addr(cb, &s, arg1), arg2);
1201 errcode = errno;
1202 break;
1203
1204 case __NR_munlock:
1205 result = munlock((void *) t2h_addr(cb, &s, arg1), arg2);
1206 errcode = errno;
1207 break;
1208
1209 case __NR_nanosleep:
1210 {
1211 struct timespec req, rem;
1212
1213 req = *((struct timespec *) t2h_addr(cb, &s, arg2));
1214 translate_endian((void *) &req, sizeof(req));
1215
1216 result = nanosleep(&req, &rem);
1217 errcode = errno;
1218
1219 if (result != 0)
1220 break;
1221
1222 translate_endian((void *) &rem, sizeof(rem));
1223 if ((s.write_mem) (cb, &s, arg2, (char *) &rem, sizeof(rem))
1224 != sizeof(rem))
1225 {
1226 result = -1;
1227 errcode = EINVAL;
1228 }
1229 }
1230 break;
1231
1232 case __NR_mremap: /* FIXME */
1233 result = (int) mremap((void *) t2h_addr(cb, &s, arg1), arg2, arg3, arg4);
1234 errcode = errno;
1235 break;
1236
1237 case __NR_getresuid32:
1238 case __NR_getresuid:
1239 {
1240 uid_t ruid, euid, suid;
1241
1242 result = getresuid(&ruid, &euid, &suid);
1243 errcode = errno;
1244
1245 if (result != 0)
1246 break;
1247
1248 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(ruid);
1249 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(euid);
1250 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(suid);
1251 }
1252 break;
1253
1254 case __NR_poll:
1255 {
1256 struct pollfd ufds;
1257
1258 ufds = *((struct pollfd *) t2h_addr(cb, &s, arg1));
1259 ufds.fd = conv_endian(ufds.fd);
1260 ufds.events = conv_endian16(ufds.events);
1261 ufds.revents = conv_endian16(ufds.revents);
1262
1263 result = poll(&ufds, arg2, arg3);
1264 errcode = errno;
1265 }
1266 break;
1267
1268 case __NR_getresgid32:
1269 case __NR_getresgid:
1270 {
1271 uid_t rgid, egid, sgid;
1272
1273 result = getresgid(&rgid, &egid, &sgid);
1274 errcode = errno;
1275
1276 if (result != 0)
1277 break;
1278
1279 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(rgid);
1280 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(egid);
1281 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(sgid);
1282 }
1283 break;
1284
1285 case __NR_pread:
1286 result = pread(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1287 errcode = errno;
1288 break;
1289
1290 case __NR_pwrite:
1291 result = pwrite(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1292 errcode = errno;
1293 break;
1294
1295 case __NR_chown32:
1296 case __NR_chown:
1297 result = chown((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
1298 errcode = errno;
1299 break;
1300
1301 case __NR_getcwd:
1302 result = (int) getcwd((char *) t2h_addr(cb, &s, arg1), arg2);
1303 errcode = errno;
1304 break;
1305
1306 case __NR_sendfile:
1307 {
1308 off_t offset;
1309
1310 offset = *((off_t *) t2h_addr(cb, &s, arg3));
1311 offset = conv_endian(offset);
1312
1313 result = sendfile(arg1, arg2, &offset, arg3);
1314 errcode = errno;
1315
1316 if (result != 0)
1317 break;
1318
1319 *((off_t *) t2h_addr(cb, &s, arg3)) = conv_endian(offset);
1320 }
1321 break;
1322
1323 default:
1324 result = -1;
1325 errcode = ENOSYS;
1326 break;
1327 }
1328
1329 if (result == -1)
1330 m32rbf_h_gr_set (current_cpu, 0, -errcode);
1331 else
1332 m32rbf_h_gr_set (current_cpu, 0, result);
1333 break;
1334 }
1335
1336 case TRAP_BREAKPOINT:
1337 sim_engine_halt (sd, current_cpu, NULL, pc,
1338 sim_stopped, SIM_SIGTRAP);
1339 break;
1340
1341 case TRAP_FLUSH_CACHE:
1342 /* Do nothing. */
1343 break;
1344
1345 default :
1346 {
1347 /* Use cr5 as EVB (EIT Vector Base) register. */
1348 USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
1349 return new_pc;
1350 }
1351 }
1352
1353 /* Fake an "rte" insn. */
1354 /* FIXME: Should duplicate all of rte processing. */
1355 return (pc & -4) + 4;
1356 }