]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/m32r/traps-linux.c
sim: split sim-signal.h include out
[thirdparty/binutils-gdb.git] / sim / m32r / traps-linux.c
CommitLineData
6edf0760 1/* m32r exception, interrupt, and trap (EIT) support
3666a048 2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
6edf0760
NC
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
4744ac1b
JB
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
6edf0760
NC
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
4744ac1b
JB
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/>. */
6edf0760 19
6df01ab8
MF
20/* This must come before any other includes. */
21#include "defs.h"
22
ad9cc209 23#include "portability.h"
6edf0760 24#include "sim-main.h"
1fef66b0 25#include "sim-signal.h"
61a0c964 26#include "sim-syscall.h"
6edf0760
NC
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>
6edf0760
NC
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
57SEM_PC
58sim_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
85void
86m32r_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
6edf0760
NC
122/* Translate target's address to host's address. */
123
124static void *
125t2h_addr (host_callback *cb, struct cb_syscall *sc,
126 unsigned long taddr)
127{
6edf0760
NC
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
138static unsigned int
139conv_endian (unsigned int tvalue)
140{
141 unsigned int hvalue;
142 unsigned int t1, t2, t3, t4;
143
0cb8d851 144 if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
6edf0760
NC
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
162static unsigned short
163conv_endian16 (unsigned short tvalue)
164{
165 unsigned short hvalue;
166 unsigned short t1, t2;
167
0cb8d851 168 if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
6edf0760
NC
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
182static void
183translate_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
199USI
200m32r_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
6edf0760
NC
205 switch (num)
206 {
207 case TRAP_ELF_SYSCALL :
208 {
7d5c6c43
MF
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;
6edf0760
NC
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
256 s.p1 = (PTR) sd;
257 s.p2 = (PTR) current_cpu;
61a0c964
MF
258 s.read_mem = sim_syscall_read_mem;
259 s.write_mem = sim_syscall_write_mem;
6edf0760
NC
260
261 result = 0;
262 result2 = 0;
263 errcode = 0;
264
265 switch (func)
266 {
267 case __NR_exit:
268 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
269 break;
270
271 case __NR_read:
272 result = read(arg1, t2h_addr(cb, &s, arg2), arg3);
273 errcode = errno;
274 break;
275
276 case __NR_write:
277 result = write(arg1, t2h_addr(cb, &s, arg2), arg3);
278 errcode = errno;
279 break;
280
281 case __NR_open:
282 result = open((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
283 errcode = errno;
284 break;
285
286 case __NR_close:
287 result = close(arg1);
288 errcode = errno;
289 break;
290
291 case __NR_creat:
292 result = creat((char *) t2h_addr(cb, &s, arg1), arg2);
293 errcode = errno;
294 break;
295
296 case __NR_link:
297 result = link((char *) t2h_addr(cb, &s, arg1),
298 (char *) t2h_addr(cb, &s, arg2));
299 errcode = errno;
300 break;
301
302 case __NR_unlink:
303 result = unlink((char *) t2h_addr(cb, &s, arg1));
304 errcode = errno;
305 break;
306
307 case __NR_chdir:
308 result = chdir((char *) t2h_addr(cb, &s, arg1));
309 errcode = errno;
310 break;
311
312 case __NR_time:
313 {
314 time_t t;
315
316 if (arg1 == 0)
317 {
318 result = (int) time(NULL);
319 errcode = errno;
320 }
321 else
322 {
323 result = (int) time(&t);
324 errcode = errno;
325
326 if (result != 0)
327 break;
328
329 translate_endian((void *) &t, sizeof(t));
330 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t)) != sizeof(t))
331 {
332 result = -1;
333 errcode = EINVAL;
334 }
335 }
336 }
337 break;
338
339 case __NR_mknod:
340 result = mknod((char *) t2h_addr(cb, &s, arg1),
341 (mode_t) arg2, (dev_t) arg3);
342 errcode = errno;
343 break;
344
345 case __NR_chmod:
346 result = chmod((char *) t2h_addr(cb, &s, arg1), (mode_t) arg2);
347 errcode = errno;
348 break;
349
19e64aa9 350 case __NR_lchown32:
6edf0760
NC
351 case __NR_lchown:
352 result = lchown((char *) t2h_addr(cb, &s, arg1),
353 (uid_t) arg2, (gid_t) arg3);
354 errcode = errno;
355 break;
356
357 case __NR_lseek:
358 result = (int) lseek(arg1, (off_t) arg2, arg3);
359 errcode = errno;
360 break;
361
362 case __NR_getpid:
363 result = getpid();
364 errcode = errno;
365 break;
366
19e64aa9 367 case __NR_getuid32:
6edf0760
NC
368 case __NR_getuid:
369 result = getuid();
370 errcode = errno;
371 break;
372
373 case __NR_utime:
374 {
375 struct utimbuf buf;
376
377 if (arg2 == 0)
378 {
379 result = utime((char *) t2h_addr(cb, &s, arg1), NULL);
380 errcode = errno;
381 }
382 else
383 {
384 buf = *((struct utimbuf *) t2h_addr(cb, &s, arg2));
385 translate_endian((void *) &buf, sizeof(buf));
386 result = utime((char *) t2h_addr(cb, &s, arg1), &buf);
387 errcode = errno;
388 }
389 }
390 break;
391
392 case __NR_access:
393 result = access((char *) t2h_addr(cb, &s, arg1), arg2);
394 errcode = errno;
395 break;
396
397 case __NR_ftime:
398 {
399 struct timeb t;
400
401 result = ftime(&t);
402 errcode = errno;
403
404 if (result != 0)
405 break;
406
407 t.time = conv_endian(t.time);
408 t.millitm = conv_endian16(t.millitm);
409 t.timezone = conv_endian16(t.timezone);
410 t.dstflag = conv_endian16(t.dstflag);
411 if ((s.write_mem) (cb, &s, arg1, (char *) &t, sizeof(t))
412 != sizeof(t))
413 {
414 result = -1;
415 errcode = EINVAL;
416 }
417 }
418
419 case __NR_sync:
420 sync();
421 result = 0;
422 break;
423
424 case __NR_rename:
425 result = rename((char *) t2h_addr(cb, &s, arg1),
426 (char *) t2h_addr(cb, &s, arg2));
427 errcode = errno;
428 break;
429
430 case __NR_mkdir:
431 result = mkdir((char *) t2h_addr(cb, &s, arg1), arg2);
432 errcode = errno;
433 break;
434
435 case __NR_rmdir:
436 result = rmdir((char *) t2h_addr(cb, &s, arg1));
437 errcode = errno;
438 break;
439
440 case __NR_dup:
441 result = dup(arg1);
442 errcode = errno;
443 break;
444
445 case __NR_brk:
446 result = brk((void *) arg1);
447 errcode = errno;
448 //result = arg1;
449 break;
450
19e64aa9 451 case __NR_getgid32:
6edf0760
NC
452 case __NR_getgid:
453 result = getgid();
454 errcode = errno;
455 break;
456
19e64aa9 457 case __NR_geteuid32:
6edf0760
NC
458 case __NR_geteuid:
459 result = geteuid();
460 errcode = errno;
461 break;
462
19e64aa9 463 case __NR_getegid32:
6edf0760
NC
464 case __NR_getegid:
465 result = getegid();
466 errcode = errno;
467 break;
468
469 case __NR_ioctl:
470 result = ioctl(arg1, arg2, arg3);
471 errcode = errno;
472 break;
473
474 case __NR_fcntl:
475 result = fcntl(arg1, arg2, arg3);
476 errcode = errno;
477 break;
478
6edf0760
NC
479 case __NR_dup2:
480 result = dup2(arg1, arg2);
481 errcode = errno;
482 break;
483
484 case __NR_getppid:
485 result = getppid();
486 errcode = errno;
487 break;
488
489 case __NR_getpgrp:
490 result = getpgrp();
491 errcode = errno;
492 break;
493
494 case __NR_getrlimit:
495 {
496 struct rlimit rlim;
497
498 result = getrlimit(arg1, &rlim);
499 errcode = errno;
500
501 if (result != 0)
502 break;
503
504 translate_endian((void *) &rlim, sizeof(rlim));
505 if ((s.write_mem) (cb, &s, arg2, (char *) &rlim, sizeof(rlim))
506 != sizeof(rlim))
507 {
508 result = -1;
509 errcode = EINVAL;
510 }
511 }
512 break;
513
514 case __NR_getrusage:
515 {
516 struct rusage usage;
517
518 result = getrusage(arg1, &usage);
519 errcode = errno;
520
521 if (result != 0)
522 break;
523
524 translate_endian((void *) &usage, sizeof(usage));
525 if ((s.write_mem) (cb, &s, arg2, (char *) &usage, sizeof(usage))
526 != sizeof(usage))
527 {
528 result = -1;
529 errcode = EINVAL;
530 }
531 }
532 break;
533
534 case __NR_gettimeofday:
535 {
536 struct timeval tv;
537 struct timezone tz;
538
539 result = gettimeofday(&tv, &tz);
540 errcode = errno;
541
542 if (result != 0)
543 break;
544
545 translate_endian((void *) &tv, sizeof(tv));
546 if ((s.write_mem) (cb, &s, arg1, (char *) &tv, sizeof(tv))
547 != sizeof(tv))
548 {
549 result = -1;
550 errcode = EINVAL;
551 }
552
553 translate_endian((void *) &tz, sizeof(tz));
554 if ((s.write_mem) (cb, &s, arg2, (char *) &tz, sizeof(tz))
555 != sizeof(tz))
556 {
557 result = -1;
558 errcode = EINVAL;
559 }
560 }
561 break;
562
19e64aa9 563 case __NR_getgroups32:
6edf0760
NC
564 case __NR_getgroups:
565 {
566 gid_t *list;
567
568 if (arg1 > 0)
569 list = (gid_t *) malloc(arg1 * sizeof(gid_t));
570
571 result = getgroups(arg1, list);
572 errcode = errno;
573
574 if (result != 0)
575 break;
576
577 translate_endian((void *) list, arg1 * sizeof(gid_t));
578 if (arg1 > 0)
579 if ((s.write_mem) (cb, &s, arg2, (char *) list, arg1 * sizeof(gid_t))
580 != arg1 * sizeof(gid_t))
581 {
582 result = -1;
583 errcode = EINVAL;
584 }
585 }
586 break;
587
588 case __NR_select:
589 {
590 int n;
591 fd_set readfds;
592 fd_set *treadfdsp;
593 fd_set *hreadfdsp;
594 fd_set writefds;
595 fd_set *twritefdsp;
596 fd_set *hwritefdsp;
597 fd_set exceptfds;
598 fd_set *texceptfdsp;
599 fd_set *hexceptfdsp;
600 struct timeval *ttimeoutp;
601 struct timeval timeout;
602
603 n = arg1;
604
605 treadfdsp = (fd_set *) arg2;
606 if (treadfdsp != NULL)
607 {
608 readfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) treadfdsp));
609 translate_endian((void *) &readfds, sizeof(readfds));
610 hreadfdsp = &readfds;
611 }
612 else
613 hreadfdsp = NULL;
614
615 twritefdsp = (fd_set *) arg3;
616 if (twritefdsp != NULL)
617 {
618 writefds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) twritefdsp));
619 translate_endian((void *) &writefds, sizeof(writefds));
620 hwritefdsp = &writefds;
621 }
622 else
623 hwritefdsp = NULL;
624
625 texceptfdsp = (fd_set *) arg4;
626 if (texceptfdsp != NULL)
627 {
628 exceptfds = *((fd_set *) t2h_addr(cb, &s, (unsigned int) texceptfdsp));
629 translate_endian((void *) &exceptfds, sizeof(exceptfds));
630 hexceptfdsp = &exceptfds;
631 }
632 else
633 hexceptfdsp = NULL;
634
635 ttimeoutp = (struct timeval *) arg5;
636 timeout = *((struct timeval *) t2h_addr(cb, &s, (unsigned int) ttimeoutp));
637 translate_endian((void *) &timeout, sizeof(timeout));
638
639 result = select(n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
640 errcode = errno;
641
642 if (result != 0)
643 break;
644
645 if (treadfdsp != NULL)
646 {
647 translate_endian((void *) &readfds, sizeof(readfds));
648 if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
649 (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
650 {
651 result = -1;
652 errcode = EINVAL;
653 }
654 }
655
656 if (twritefdsp != NULL)
657 {
658 translate_endian((void *) &writefds, sizeof(writefds));
659 if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
660 (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
661 {
662 result = -1;
663 errcode = EINVAL;
664 }
665 }
666
667 if (texceptfdsp != NULL)
668 {
669 translate_endian((void *) &exceptfds, sizeof(exceptfds));
670 if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
671 (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
672 {
673 result = -1;
674 errcode = EINVAL;
675 }
676 }
677
678 translate_endian((void *) &timeout, sizeof(timeout));
679 if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
680 (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
681 {
682 result = -1;
683 errcode = EINVAL;
684 }
685 }
686 break;
687
688 case __NR_symlink:
689 result = symlink((char *) t2h_addr(cb, &s, arg1),
690 (char *) t2h_addr(cb, &s, arg2));
691 errcode = errno;
692 break;
693
694 case __NR_readlink:
695 result = readlink((char *) t2h_addr(cb, &s, arg1),
696 (char *) t2h_addr(cb, &s, arg2),
697 arg3);
698 errcode = errno;
699 break;
700
701 case __NR_readdir:
702 result = (int) readdir((DIR *) t2h_addr(cb, &s, arg1));
703 errcode = errno;
704 break;
705
706#if 0
707 case __NR_mmap:
708 {
709 result = (int) mmap((void *) t2h_addr(cb, &s, arg1),
710 arg2, arg3, arg4, arg5, arg6);
711 errcode = errno;
712
713 if (errno == 0)
714 {
715 sim_core_attach (sd, NULL,
716 0, access_read_write_exec, 0,
717 result, arg2, 0, NULL, NULL);
718 }
719 }
720 break;
721#endif
19e64aa9
KI
722 case __NR_mmap2:
723 {
724 void *addr;
725 size_t len;
726 int prot, flags, fildes;
727 off_t off;
728
729 addr = (void *) t2h_addr(cb, &s, arg1);
730 len = arg2;
731 prot = arg3;
732 flags = arg4;
733 fildes = arg5;
734 off = arg6 << 12;
735
736 result = (int) mmap(addr, len, prot, flags, fildes, off);
737 errcode = errno;
738 if (result != -1)
739 {
740 char c;
741 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
742 sim_core_attach (sd, NULL,
743 0, access_read_write_exec, 0,
744 result, len, 0, NULL, NULL);
745 }
746 }
747 break;
748
6edf0760
NC
749 case __NR_mmap:
750 {
751 void *addr;
752 size_t len;
753 int prot, flags, fildes;
754 off_t off;
755
756 addr = *((void **) t2h_addr(cb, &s, arg1));
757 len = *((size_t *) t2h_addr(cb, &s, arg1 + 4));
758 prot = *((int *) t2h_addr(cb, &s, arg1 + 8));
759 flags = *((int *) t2h_addr(cb, &s, arg1 + 12));
760 fildes = *((int *) t2h_addr(cb, &s, arg1 + 16));
761 off = *((off_t *) t2h_addr(cb, &s, arg1 + 20));
762
763 addr = (void *) conv_endian((unsigned int) addr);
764 len = conv_endian(len);
765 prot = conv_endian(prot);
766 flags = conv_endian(flags);
767 fildes = conv_endian(fildes);
768 off = conv_endian(off);
769
770 //addr = (void *) t2h_addr(cb, &s, (unsigned int) addr);
771 result = (int) mmap(addr, len, prot, flags, fildes, off);
772 errcode = errno;
773
774 //if (errno == 0)
775 if (result != -1)
776 {
777 char c;
778 if (sim_core_read_buffer (sd, NULL, read_map, &c, result, 1) == 0)
779 sim_core_attach (sd, NULL,
780 0, access_read_write_exec, 0,
781 result, len, 0, NULL, NULL);
782 }
783 }
784 break;
785
786 case __NR_munmap:
787 {
788 result = munmap((void *)arg1, arg2);
789 errcode = errno;
790 if (result != -1)
791 {
792 sim_core_detach (sd, NULL, 0, arg2, result);
793 }
794 }
795 break;
796
797 case __NR_truncate:
798 result = truncate((char *) t2h_addr(cb, &s, arg1), arg2);
799 errcode = errno;
800 break;
801
802 case __NR_ftruncate:
803 result = ftruncate(arg1, arg2);
804 errcode = errno;
805 break;
806
807 case __NR_fchmod:
808 result = fchmod(arg1, arg2);
809 errcode = errno;
810 break;
811
19e64aa9 812 case __NR_fchown32:
6edf0760
NC
813 case __NR_fchown:
814 result = fchown(arg1, arg2, arg3);
815 errcode = errno;
816 break;
817
818 case __NR_statfs:
819 {
820 struct statfs statbuf;
821
822 result = statfs((char *) t2h_addr(cb, &s, arg1), &statbuf);
823 errcode = errno;
824
825 if (result != 0)
826 break;
827
828 translate_endian((void *) &statbuf, sizeof(statbuf));
829 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
830 != sizeof(statbuf))
831 {
832 result = -1;
833 errcode = EINVAL;
834 }
835 }
836 break;
837
838 case __NR_fstatfs:
839 {
840 struct statfs statbuf;
841
842 result = fstatfs(arg1, &statbuf);
843 errcode = errno;
844
845 if (result != 0)
846 break;
847
848 translate_endian((void *) &statbuf, sizeof(statbuf));
849 if ((s.write_mem) (cb, &s, arg2, (char *) &statbuf, sizeof(statbuf))
850 != sizeof(statbuf))
851 {
852 result = -1;
853 errcode = EINVAL;
854 }
855 }
856 break;
857
858 case __NR_syslog:
859 result = syslog(arg1, (char *) t2h_addr(cb, &s, arg2));
860 errcode = errno;
861 break;
862
863 case __NR_setitimer:
864 {
865 struct itimerval value, ovalue;
866
867 value = *((struct itimerval *) t2h_addr(cb, &s, arg2));
868 translate_endian((void *) &value, sizeof(value));
869
870 if (arg2 == 0)
871 {
872 result = setitimer(arg1, &value, NULL);
873 errcode = errno;
874 }
875 else
876 {
877 result = setitimer(arg1, &value, &ovalue);
878 errcode = errno;
879
880 if (result != 0)
881 break;
882
883 translate_endian((void *) &ovalue, sizeof(ovalue));
884 if ((s.write_mem) (cb, &s, arg3, (char *) &ovalue, sizeof(ovalue))
885 != sizeof(ovalue))
886 {
887 result = -1;
888 errcode = EINVAL;
889 }
890 }
891 }
892 break;
893
894 case __NR_getitimer:
895 {
896 struct itimerval value;
897
898 result = getitimer(arg1, &value);
899 errcode = errno;
900
901 if (result != 0)
902 break;
903
904 translate_endian((void *) &value, sizeof(value));
905 if ((s.write_mem) (cb, &s, arg2, (char *) &value, sizeof(value))
906 != sizeof(value))
907 {
908 result = -1;
909 errcode = EINVAL;
910 }
911 }
912 break;
913
914 case __NR_stat:
915 {
916 char *buf;
917 int buflen;
918 struct stat statbuf;
919
920 result = stat((char *) t2h_addr(cb, &s, arg1), &statbuf);
921 errcode = errno;
922 if (result < 0)
923 break;
924
925 buflen = cb_host_to_target_stat (cb, NULL, NULL);
926 buf = xmalloc (buflen);
927 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
928 {
929 /* The translation failed. This is due to an internal
930 host program error, not the target's fault. */
931 free (buf);
932 result = -1;
933 errcode = ENOSYS;
934 break;
935 }
936 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
937 {
938 free (buf);
939 result = -1;
940 errcode = EINVAL;
941 break;
942 }
943 free (buf);
944 }
945 break;
946
947 case __NR_lstat:
948 {
949 char *buf;
950 int buflen;
951 struct stat statbuf;
952
953 result = lstat((char *) t2h_addr(cb, &s, arg1), &statbuf);
954 errcode = errno;
955 if (result < 0)
956 break;
957
958 buflen = cb_host_to_target_stat (cb, NULL, NULL);
959 buf = xmalloc (buflen);
960 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
961 {
962 /* The translation failed. This is due to an internal
963 host program error, not the target's fault. */
964 free (buf);
965 result = -1;
966 errcode = ENOSYS;
967 break;
968 }
969 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
970 {
971 free (buf);
972 result = -1;
973 errcode = EINVAL;
974 break;
975 }
976 free (buf);
977 }
978 break;
979
980 case __NR_fstat:
981 {
982 char *buf;
983 int buflen;
984 struct stat statbuf;
985
986 result = fstat(arg1, &statbuf);
987 errcode = errno;
988 if (result < 0)
989 break;
990
991 buflen = cb_host_to_target_stat (cb, NULL, NULL);
992 buf = xmalloc (buflen);
993 if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
994 {
995 /* The translation failed. This is due to an internal
996 host program error, not the target's fault. */
997 free (buf);
998 result = -1;
999 errcode = ENOSYS;
1000 break;
1001 }
1002 if ((s.write_mem) (cb, &s, arg2, buf, buflen) != buflen)
1003 {
1004 free (buf);
1005 result = -1;
1006 errcode = EINVAL;
1007 break;
1008 }
1009 free (buf);
1010 }
1011 break;
1012
1013 case __NR_sysinfo:
1014 {
1015 struct sysinfo info;
1016
1017 result = sysinfo(&info);
1018 errcode = errno;
1019
1020 if (result != 0)
1021 break;
1022
1023 info.uptime = conv_endian(info.uptime);
1024 info.loads[0] = conv_endian(info.loads[0]);
1025 info.loads[1] = conv_endian(info.loads[1]);
1026 info.loads[2] = conv_endian(info.loads[2]);
1027 info.totalram = conv_endian(info.totalram);
1028 info.freeram = conv_endian(info.freeram);
1029 info.sharedram = conv_endian(info.sharedram);
1030 info.bufferram = conv_endian(info.bufferram);
1031 info.totalswap = conv_endian(info.totalswap);
1032 info.freeswap = conv_endian(info.freeswap);
1033 info.procs = conv_endian16(info.procs);
1034#if LINUX_VERSION_CODE >= 0x20400
1035 info.totalhigh = conv_endian(info.totalhigh);
1036 info.freehigh = conv_endian(info.freehigh);
1037 info.mem_unit = conv_endian(info.mem_unit);
1038#endif
1039 if ((s.write_mem) (cb, &s, arg1, (char *) &info, sizeof(info))
1040 != sizeof(info))
1041 {
1042 result = -1;
1043 errcode = EINVAL;
1044 }
1045 }
1046 break;
1047
1048#if 0
1049 case __NR_ipc:
1050 {
1051 result = ipc(arg1, arg2, arg3, arg4,
1052 (void *) t2h_addr(cb, &s, arg5), arg6);
1053 errcode = errno;
1054 }
1055 break;
1056#endif
1057
1058 case __NR_fsync:
1059 result = fsync(arg1);
1060 errcode = errno;
1061 break;
1062
1063 case __NR_uname:
1064 /* utsname contains only arrays of char, so it is not necessary
1065 to translate endian. */
1066 result = uname((struct utsname *) t2h_addr(cb, &s, arg1));
1067 errcode = errno;
1068 break;
1069
1070 case __NR_adjtimex:
1071 {
1072 struct timex buf;
1073
1074 result = adjtimex(&buf);
1075 errcode = errno;
1076
1077 if (result != 0)
1078 break;
1079
1080 translate_endian((void *) &buf, sizeof(buf));
1081 if ((s.write_mem) (cb, &s, arg1, (char *) &buf, sizeof(buf))
1082 != sizeof(buf))
1083 {
1084 result = -1;
1085 errcode = EINVAL;
1086 }
1087 }
1088 break;
1089
1090 case __NR_mprotect:
1091 result = mprotect((void *) arg1, arg2, arg3);
1092 errcode = errno;
1093 break;
1094
6edf0760
NC
1095 case __NR_fchdir:
1096 result = fchdir(arg1);
1097 errcode = errno;
1098 break;
1099
19e64aa9 1100 case __NR_setfsuid32:
6edf0760
NC
1101 case __NR_setfsuid:
1102 result = setfsuid(arg1);
1103 errcode = errno;
1104 break;
1105
19e64aa9 1106 case __NR_setfsgid32:
6edf0760
NC
1107 case __NR_setfsgid:
1108 result = setfsgid(arg1);
1109 errcode = errno;
1110 break;
1111
1112#if 0
1113 case __NR__llseek:
1114 {
1115 loff_t buf;
1116
1117 result = _llseek(arg1, arg2, arg3, &buf, arg5);
1118 errcode = errno;
1119
1120 if (result != 0)
1121 break;
1122
1123 translate_endian((void *) &buf, sizeof(buf));
1124 if ((s.write_mem) (cb, &s, t2h_addr(cb, &s, arg4),
1125 (char *) &buf, sizeof(buf)) != sizeof(buf))
1126 {
1127 result = -1;
1128 errcode = EINVAL;
1129 }
1130 }
1131 break;
1132
1133 case __NR_getdents:
1134 {
1135 struct dirent dir;
1136
1137 result = getdents(arg1, &dir, arg3);
1138 errcode = errno;
1139
1140 if (result != 0)
1141 break;
1142
1143 dir.d_ino = conv_endian(dir.d_ino);
1144 dir.d_off = conv_endian(dir.d_off);
1145 dir.d_reclen = conv_endian16(dir.d_reclen);
1146 if ((s.write_mem) (cb, &s, arg2, (char *) &dir, sizeof(dir))
1147 != sizeof(dir))
1148 {
1149 result = -1;
1150 errcode = EINVAL;
1151 }
1152 }
1153 break;
1154#endif
1155
1156 case __NR_flock:
1157 result = flock(arg1, arg2);
1158 errcode = errno;
1159 break;
1160
1161 case __NR_msync:
1162 result = msync((void *) arg1, arg2, arg3);
1163 errcode = errno;
1164 break;
1165
1166 case __NR_readv:
1167 {
1168 struct iovec vector;
1169
1170 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1171 translate_endian((void *) &vector, sizeof(vector));
1172
1173 result = readv(arg1, &vector, arg3);
1174 errcode = errno;
1175 }
1176 break;
1177
1178 case __NR_writev:
1179 {
1180 struct iovec vector;
1181
1182 vector = *((struct iovec *) t2h_addr(cb, &s, arg2));
1183 translate_endian((void *) &vector, sizeof(vector));
1184
1185 result = writev(arg1, &vector, arg3);
1186 errcode = errno;
1187 }
1188 break;
1189
1190 case __NR_fdatasync:
1191 result = fdatasync(arg1);
1192 errcode = errno;
1193 break;
1194
1195 case __NR_mlock:
1196 result = mlock((void *) t2h_addr(cb, &s, arg1), arg2);
1197 errcode = errno;
1198 break;
1199
1200 case __NR_munlock:
1201 result = munlock((void *) t2h_addr(cb, &s, arg1), arg2);
1202 errcode = errno;
1203 break;
1204
1205 case __NR_nanosleep:
1206 {
1207 struct timespec req, rem;
1208
1209 req = *((struct timespec *) t2h_addr(cb, &s, arg2));
1210 translate_endian((void *) &req, sizeof(req));
1211
1212 result = nanosleep(&req, &rem);
1213 errcode = errno;
1214
1215 if (result != 0)
1216 break;
1217
1218 translate_endian((void *) &rem, sizeof(rem));
1219 if ((s.write_mem) (cb, &s, arg2, (char *) &rem, sizeof(rem))
1220 != sizeof(rem))
1221 {
1222 result = -1;
1223 errcode = EINVAL;
1224 }
1225 }
1226 break;
1227
1228 case __NR_mremap: /* FIXME */
1229 result = (int) mremap((void *) t2h_addr(cb, &s, arg1), arg2, arg3, arg4);
1230 errcode = errno;
1231 break;
1232
19e64aa9 1233 case __NR_getresuid32:
6edf0760
NC
1234 case __NR_getresuid:
1235 {
1236 uid_t ruid, euid, suid;
1237
1238 result = getresuid(&ruid, &euid, &suid);
1239 errcode = errno;
1240
1241 if (result != 0)
1242 break;
1243
1244 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(ruid);
1245 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(euid);
1246 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(suid);
1247 }
1248 break;
1249
1250 case __NR_poll:
1251 {
1252 struct pollfd ufds;
1253
1254 ufds = *((struct pollfd *) t2h_addr(cb, &s, arg1));
1255 ufds.fd = conv_endian(ufds.fd);
1256 ufds.events = conv_endian16(ufds.events);
1257 ufds.revents = conv_endian16(ufds.revents);
1258
1259 result = poll(&ufds, arg2, arg3);
1260 errcode = errno;
1261 }
1262 break;
1263
19e64aa9 1264 case __NR_getresgid32:
6edf0760
NC
1265 case __NR_getresgid:
1266 {
1267 uid_t rgid, egid, sgid;
1268
1269 result = getresgid(&rgid, &egid, &sgid);
1270 errcode = errno;
1271
1272 if (result != 0)
1273 break;
1274
1275 *((uid_t *) t2h_addr(cb, &s, arg1)) = conv_endian(rgid);
1276 *((uid_t *) t2h_addr(cb, &s, arg2)) = conv_endian(egid);
1277 *((uid_t *) t2h_addr(cb, &s, arg3)) = conv_endian(sgid);
1278 }
1279 break;
1280
1281 case __NR_pread:
1282 result = pread(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1283 errcode = errno;
1284 break;
1285
1286 case __NR_pwrite:
1287 result = pwrite(arg1, (void *) t2h_addr(cb, &s, arg2), arg3, arg4);
1288 errcode = errno;
1289 break;
1290
19e64aa9 1291 case __NR_chown32:
6edf0760
NC
1292 case __NR_chown:
1293 result = chown((char *) t2h_addr(cb, &s, arg1), arg2, arg3);
1294 errcode = errno;
1295 break;
1296
1297 case __NR_getcwd:
1298 result = (int) getcwd((char *) t2h_addr(cb, &s, arg1), arg2);
1299 errcode = errno;
1300 break;
1301
1302 case __NR_sendfile:
1303 {
1304 off_t offset;
1305
1306 offset = *((off_t *) t2h_addr(cb, &s, arg3));
1307 offset = conv_endian(offset);
1308
1309 result = sendfile(arg1, arg2, &offset, arg3);
1310 errcode = errno;
1311
1312 if (result != 0)
1313 break;
1314
1315 *((off_t *) t2h_addr(cb, &s, arg3)) = conv_endian(offset);
1316 }
1317 break;
1318
1319 default:
1320 result = -1;
1321 errcode = ENOSYS;
1322 break;
1323 }
1324
1325 if (result == -1)
1326 m32rbf_h_gr_set (current_cpu, 0, -errcode);
1327 else
1328 m32rbf_h_gr_set (current_cpu, 0, result);
1329 break;
1330 }
1331
1332 case TRAP_BREAKPOINT:
1333 sim_engine_halt (sd, current_cpu, NULL, pc,
1334 sim_stopped, SIM_SIGTRAP);
1335 break;
1336
1337 case TRAP_FLUSH_CACHE:
1338 /* Do nothing. */
1339 break;
1340
1341 default :
1342 {
1343 /* Use cr5 as EVB (EIT Vector Base) register. */
1344 USI new_pc = m32rbf_h_cr_get (current_cpu, 5) + 0x40 + num * 4;
1345 return new_pc;
1346 }
1347 }
1348
1349 /* Fake an "rte" insn. */
1350 /* FIXME: Should duplicate all of rte processing. */
1351 return (pc & -4) + 4;
1352}