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