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