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