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