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