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