* cris/traps.c (TARGET_MAP_DENYWRITE): Define.
[binutils-gdb.git] / sim / cris / traps.c
1 /* CRIS exception, interrupt, and trap (EIT) support
2 Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Axis Communications.
4
5 This file is part of the GNU simulators.
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 #include "sim-main.h"
21 #include "sim-options.h"
22 #include "bfd.h"
23 /* FIXME: get rid of targ-vals.h usage everywhere else. */
24
25 #include <stdarg.h>
26 #ifdef HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 /* For PATH_MAX, originally. */
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45
46 /* From ld/sysdep.h. */
47 #ifdef PATH_MAX
48 # define SIM_PATHMAX PATH_MAX
49 #else
50 # ifdef MAXPATHLEN
51 # define SIM_PATHMAX MAXPATHLEN
52 # else
53 # define SIM_PATHMAX 1024
54 # endif
55 #endif
56
57 /* The verbatim values are from asm-cris/unistd.h. */
58
59 #define TARGET_SYS_exit 1
60 #define TARGET_SYS_read 3
61 #define TARGET_SYS_write 4
62 #define TARGET_SYS_open 5
63 #define TARGET_SYS_close 6
64 #define TARGET_SYS_unlink 10
65 #define TARGET_SYS_time 13
66 #define TARGET_SYS_lseek 19
67 #define TARGET_SYS_getpid 20
68 #define TARGET_SYS_access 33
69 #define TARGET_SYS_kill 37
70 #define TARGET_SYS_rename 38
71 #define TARGET_SYS_pipe 42
72 #define TARGET_SYS_brk 45
73 #define TARGET_SYS_ioctl 54
74 #define TARGET_SYS_fcntl 55
75 #define TARGET_SYS_getppid 64
76 #define TARGET_SYS_setrlimit 75
77 #define TARGET_SYS_gettimeofday 78
78 #define TARGET_SYS_readlink 85
79 #define TARGET_SYS_munmap 91
80 #define TARGET_SYS_truncate 92
81 #define TARGET_SYS_ftruncate 93
82 #define TARGET_SYS_socketcall 102
83 #define TARGET_SYS_stat 106
84 #define TARGET_SYS_fstat 108
85 #define TARGET_SYS_wait4 114
86 #define TARGET_SYS_sigreturn 119
87 #define TARGET_SYS_clone 120
88 #define TARGET_SYS_uname 122
89 #define TARGET_SYS_mprotect 125
90 #define TARGET_SYS_llseek 140
91 #define TARGET_SYS_writev 146
92 #define TARGET_SYS__sysctl 149
93 #define TARGET_SYS_sched_setparam 154
94 #define TARGET_SYS_sched_getparam 155
95 #define TARGET_SYS_sched_setscheduler 156
96 #define TARGET_SYS_sched_getscheduler 157
97 #define TARGET_SYS_sched_yield 158
98 #define TARGET_SYS_sched_get_priority_max 159
99 #define TARGET_SYS_sched_get_priority_min 160
100 #define TARGET_SYS_mremap 163
101 #define TARGET_SYS_poll 168
102 #define TARGET_SYS_rt_sigaction 174
103 #define TARGET_SYS_rt_sigprocmask 175
104 #define TARGET_SYS_rt_sigsuspend 179
105 #define TARGET_SYS_getcwd 183
106 #define TARGET_SYS_ugetrlimit 191
107 #define TARGET_SYS_mmap2 192
108 #define TARGET_SYS_stat64 195
109 #define TARGET_SYS_lstat64 196
110 #define TARGET_SYS_fstat64 197
111 #define TARGET_SYS_geteuid32 201
112 #define TARGET_SYS_getuid32 199
113 #define TARGET_SYS_getegid32 202
114 #define TARGET_SYS_getgid32 200
115 #define TARGET_SYS_fcntl64 221
116
117 #define TARGET_PROT_READ 0x1
118 #define TARGET_PROT_WRITE 0x2
119 #define TARGET_PROT_EXEC 0x4
120 #define TARGET_PROT_NONE 0x0
121
122 #define TARGET_MAP_SHARED 0x01
123 #define TARGET_MAP_PRIVATE 0x02
124 #define TARGET_MAP_TYPE 0x0f
125 #define TARGET_MAP_FIXED 0x10
126 #define TARGET_MAP_ANONYMOUS 0x20
127 #define TARGET_MAP_DENYWRITE 0x800
128
129 #define TARGET_CTL_KERN 1
130 #define TARGET_CTL_VM 2
131 #define TARGET_CTL_NET 3
132 #define TARGET_CTL_PROC 4
133 #define TARGET_CTL_FS 5
134 #define TARGET_CTL_DEBUG 6
135 #define TARGET_CTL_DEV 7
136 #define TARGET_CTL_BUS 8
137 #define TARGET_CTL_ABI 9
138
139 #define TARGET_CTL_KERN_VERSION 4
140
141 /* linux/mman.h */
142 #define TARGET_MREMAP_MAYMOVE 1
143 #define TARGET_MREMAP_FIXED 2
144
145 #define TARGET_TCGETS 0x5401
146
147 #define TARGET_UTSNAME "#38 Sun Apr 1 00:00:00 MET 2001"
148
149 /* Seconds since the above date + 10 minutes. */
150 #define TARGET_EPOCH 986080200
151
152 /* Milliseconds since start of run. We use the number of syscalls to
153 avoid introducing noise in the execution time. */
154 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
155
156 /* Seconds as in time(2). */
157 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
158
159 #define TARGET_SCHED_OTHER 0
160
161 #define TARGET_RLIMIT_STACK 3
162 #define TARGET_RLIMIT_NOFILE 7
163
164 #define SIM_TARGET_MAX_THREADS 64
165 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
166
167 /* From linux/sched.h. */
168 #define TARGET_CSIGNAL 0x000000ff
169 #define TARGET_CLONE_VM 0x00000100
170 #define TARGET_CLONE_FS 0x00000200
171 #define TARGET_CLONE_FILES 0x00000400
172 #define TARGET_CLONE_SIGHAND 0x00000800
173 #define TARGET_CLONE_PID 0x00001000
174 #define TARGET_CLONE_PTRACE 0x00002000
175 #define TARGET_CLONE_VFORK 0x00004000
176 #define TARGET_CLONE_PARENT 0x00008000
177 #define TARGET_CLONE_THREAD 0x00010000
178 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
179
180 /* From asm-cris/poll.h. */
181 #define TARGET_POLLIN 1
182
183 /* From asm-cris/signal.h. */
184 #define TARGET_SIG_BLOCK 0
185 #define TARGET_SIG_UNBLOCK 1
186 #define TARGET_SIG_SETMASK 2
187
188 #define TARGET_SIG_DFL 0
189 #define TARGET_SIG_IGN 1
190 #define TARGET_SIG_ERR ((USI)-1)
191
192 #define TARGET_SIGHUP 1
193 #define TARGET_SIGINT 2
194 #define TARGET_SIGQUIT 3
195 #define TARGET_SIGILL 4
196 #define TARGET_SIGTRAP 5
197 #define TARGET_SIGABRT 6
198 #define TARGET_SIGIOT 6
199 #define TARGET_SIGBUS 7
200 #define TARGET_SIGFPE 8
201 #define TARGET_SIGKILL 9
202 #define TARGET_SIGUSR1 10
203 #define TARGET_SIGSEGV 11
204 #define TARGET_SIGUSR2 12
205 #define TARGET_SIGPIPE 13
206 #define TARGET_SIGALRM 14
207 #define TARGET_SIGTERM 15
208 #define TARGET_SIGSTKFLT 16
209 #define TARGET_SIGCHLD 17
210 #define TARGET_SIGCONT 18
211 #define TARGET_SIGSTOP 19
212 #define TARGET_SIGTSTP 20
213 #define TARGET_SIGTTIN 21
214 #define TARGET_SIGTTOU 22
215 #define TARGET_SIGURG 23
216 #define TARGET_SIGXCPU 24
217 #define TARGET_SIGXFSZ 25
218 #define TARGET_SIGVTALRM 26
219 #define TARGET_SIGPROF 27
220 #define TARGET_SIGWINCH 28
221 #define TARGET_SIGIO 29
222 #define TARGET_SIGPOLL SIGIO
223 /* Actually commented out in the kernel header. */
224 #define TARGET_SIGLOST 29
225 #define TARGET_SIGPWR 30
226 #define TARGET_SIGSYS 31
227
228 /* From include/asm-cris/signal.h. */
229 #define TARGET_SA_NOCLDSTOP 0x00000001
230 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
231 #define TARGET_SA_SIGINFO 0x00000004
232 #define TARGET_SA_ONSTACK 0x08000000
233 #define TARGET_SA_RESTART 0x10000000
234 #define TARGET_SA_NODEFER 0x40000000
235 #define TARGET_SA_RESETHAND 0x80000000
236 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
237 #define TARGET_SA_RESTORER 0x04000000
238
239 /* From linux/wait.h. */
240 #define TARGET_WNOHANG 1
241 #define TARGET_WUNTRACED 2
242 #define TARGET___WNOTHREAD 0x20000000
243 #define TARGET___WALL 0x40000000
244 #define TARGET___WCLONE 0x80000000
245
246 /* From linux/limits.h. */
247 #define TARGET_PIPE_BUF 4096
248
249 /* From unistd.h. */
250 #define TARGET_R_OK 4
251 #define TARGET_W_OK 2
252 #define TARGET_X_OK 1
253 #define TARGET_F_OK 0
254
255 static const char stat_map[] =
256 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
257 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
258 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
259 ":st_ino,8";
260
261 static const CB_TARGET_DEFS_MAP syscall_map[] =
262 {
263 { CB_SYS_open, TARGET_SYS_open },
264 { CB_SYS_close, TARGET_SYS_close },
265 { CB_SYS_read, TARGET_SYS_read },
266 { CB_SYS_write, TARGET_SYS_write },
267 { CB_SYS_lseek, TARGET_SYS_lseek },
268 { CB_SYS_unlink, TARGET_SYS_unlink },
269 { CB_SYS_getpid, TARGET_SYS_getpid },
270 { CB_SYS_fstat, TARGET_SYS_fstat64 },
271 { CB_SYS_lstat, TARGET_SYS_lstat64 },
272 { CB_SYS_stat, TARGET_SYS_stat64 },
273 { CB_SYS_pipe, TARGET_SYS_pipe },
274 { CB_SYS_rename, TARGET_SYS_rename },
275 { CB_SYS_truncate, TARGET_SYS_truncate },
276 { CB_SYS_ftruncate, TARGET_SYS_ftruncate },
277 { 0, -1 }
278 };
279
280 /* An older, 32-bit-only stat mapping. */
281 static const char stat32_map[] =
282 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
283 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
284 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
285
286 /* Map for calls using the 32-bit struct stat. Primarily used by the
287 newlib Linux mapping. */
288 static const CB_TARGET_DEFS_MAP syscall_stat32_map[] =
289 {
290 { CB_SYS_fstat, TARGET_SYS_fstat },
291 { CB_SYS_stat, TARGET_SYS_stat },
292 { 0, -1 }
293 };
294
295 /* Giving the true value for the running sim process will lead to
296 non-time-invariant behavior. */
297 #define TARGET_PID 42
298
299 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
300 we did, we'd still don't get a register number with the "16" offset. */
301 #define TARGET_SRP_REGNUM (16+11)
302
303 /* Extracted by applying
304 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
305 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
306 adjusting the synonyms. */
307
308 static const CB_TARGET_DEFS_MAP errno_map[] =
309 {
310 #ifdef EPERM
311 { EPERM, 1 },
312 #endif
313 #ifdef ENOENT
314 { ENOENT, 2 },
315 #endif
316 #ifdef ESRCH
317 { ESRCH, 3 },
318 #endif
319 #ifdef EINTR
320 { EINTR, 4 },
321 #endif
322 #ifdef EIO
323 { EIO, 5 },
324 #endif
325 #ifdef ENXIO
326 { ENXIO, 6 },
327 #endif
328 #ifdef E2BIG
329 { E2BIG, 7 },
330 #endif
331 #ifdef ENOEXEC
332 { ENOEXEC, 8 },
333 #endif
334 #ifdef EBADF
335 { EBADF, 9 },
336 #endif
337 #ifdef ECHILD
338 { ECHILD, 10 },
339 #endif
340 #ifdef EAGAIN
341 { EAGAIN, 11 },
342 #endif
343 #ifdef ENOMEM
344 { ENOMEM, 12 },
345 #endif
346 #ifdef EACCES
347 { EACCES, 13 },
348 #endif
349 #ifdef EFAULT
350 { EFAULT, 14 },
351 #endif
352 #ifdef ENOTBLK
353 { ENOTBLK, 15 },
354 #endif
355 #ifdef EBUSY
356 { EBUSY, 16 },
357 #endif
358 #ifdef EEXIST
359 { EEXIST, 17 },
360 #endif
361 #ifdef EXDEV
362 { EXDEV, 18 },
363 #endif
364 #ifdef ENODEV
365 { ENODEV, 19 },
366 #endif
367 #ifdef ENOTDIR
368 { ENOTDIR, 20 },
369 #endif
370 #ifdef EISDIR
371 { EISDIR, 21 },
372 #endif
373 #ifdef EINVAL
374 { EINVAL, 22 },
375 #endif
376 #ifdef ENFILE
377 { ENFILE, 23 },
378 #endif
379 #ifdef EMFILE
380 { EMFILE, 24 },
381 #endif
382 #ifdef ENOTTY
383 { ENOTTY, 25 },
384 #endif
385 #ifdef ETXTBSY
386 { ETXTBSY, 26 },
387 #endif
388 #ifdef EFBIG
389 { EFBIG, 27 },
390 #endif
391 #ifdef ENOSPC
392 { ENOSPC, 28 },
393 #endif
394 #ifdef ESPIPE
395 { ESPIPE, 29 },
396 #endif
397 #ifdef EROFS
398 { EROFS, 30 },
399 #endif
400 #ifdef EMLINK
401 { EMLINK, 31 },
402 #endif
403 #ifdef EPIPE
404 { EPIPE, 32 },
405 #endif
406 #ifdef EDOM
407 { EDOM, 33 },
408 #endif
409 #ifdef ERANGE
410 { ERANGE, 34 },
411 #endif
412 #ifdef EDEADLK
413 { EDEADLK, 35 },
414 #endif
415 #ifdef ENAMETOOLONG
416 { ENAMETOOLONG, 36 },
417 #endif
418 #ifdef ENOLCK
419 { ENOLCK, 37 },
420 #endif
421 #ifdef ENOSYS
422 { ENOSYS, 38 },
423 #endif
424 #ifdef ENOTEMPTY
425 { ENOTEMPTY, 39 },
426 #endif
427 #ifdef ELOOP
428 { ELOOP, 40 },
429 #endif
430 #ifdef EWOULDBLOCK
431 { EWOULDBLOCK, 11 },
432 #endif
433 #ifdef ENOMSG
434 { ENOMSG, 42 },
435 #endif
436 #ifdef EIDRM
437 { EIDRM, 43 },
438 #endif
439 #ifdef ECHRNG
440 { ECHRNG, 44 },
441 #endif
442 #ifdef EL2NSYNC
443 { EL2NSYNC, 45 },
444 #endif
445 #ifdef EL3HLT
446 { EL3HLT, 46 },
447 #endif
448 #ifdef EL3RST
449 { EL3RST, 47 },
450 #endif
451 #ifdef ELNRNG
452 { ELNRNG, 48 },
453 #endif
454 #ifdef EUNATCH
455 { EUNATCH, 49 },
456 #endif
457 #ifdef ENOCSI
458 { ENOCSI, 50 },
459 #endif
460 #ifdef EL2HLT
461 { EL2HLT, 51 },
462 #endif
463 #ifdef EBADE
464 { EBADE, 52 },
465 #endif
466 #ifdef EBADR
467 { EBADR, 53 },
468 #endif
469 #ifdef EXFULL
470 { EXFULL, 54 },
471 #endif
472 #ifdef ENOANO
473 { ENOANO, 55 },
474 #endif
475 #ifdef EBADRQC
476 { EBADRQC, 56 },
477 #endif
478 #ifdef EBADSLT
479 { EBADSLT, 57 },
480 #endif
481 #ifdef EDEADLOCK
482 { EDEADLOCK, 35 },
483 #endif
484 #ifdef EBFONT
485 { EBFONT, 59 },
486 #endif
487 #ifdef ENOSTR
488 { ENOSTR, 60 },
489 #endif
490 #ifdef ENODATA
491 { ENODATA, 61 },
492 #endif
493 #ifdef ETIME
494 { ETIME, 62 },
495 #endif
496 #ifdef ENOSR
497 { ENOSR, 63 },
498 #endif
499 #ifdef ENONET
500 { ENONET, 64 },
501 #endif
502 #ifdef ENOPKG
503 { ENOPKG, 65 },
504 #endif
505 #ifdef EREMOTE
506 { EREMOTE, 66 },
507 #endif
508 #ifdef ENOLINK
509 { ENOLINK, 67 },
510 #endif
511 #ifdef EADV
512 { EADV, 68 },
513 #endif
514 #ifdef ESRMNT
515 { ESRMNT, 69 },
516 #endif
517 #ifdef ECOMM
518 { ECOMM, 70 },
519 #endif
520 #ifdef EPROTO
521 { EPROTO, 71 },
522 #endif
523 #ifdef EMULTIHOP
524 { EMULTIHOP, 72 },
525 #endif
526 #ifdef EDOTDOT
527 { EDOTDOT, 73 },
528 #endif
529 #ifdef EBADMSG
530 { EBADMSG, 74 },
531 #endif
532 #ifdef EOVERFLOW
533 { EOVERFLOW, 75 },
534 #endif
535 #ifdef ENOTUNIQ
536 { ENOTUNIQ, 76 },
537 #endif
538 #ifdef EBADFD
539 { EBADFD, 77 },
540 #endif
541 #ifdef EREMCHG
542 { EREMCHG, 78 },
543 #endif
544 #ifdef ELIBACC
545 { ELIBACC, 79 },
546 #endif
547 #ifdef ELIBBAD
548 { ELIBBAD, 80 },
549 #endif
550 #ifdef ELIBSCN
551 { ELIBSCN, 81 },
552 #endif
553 #ifdef ELIBMAX
554 { ELIBMAX, 82 },
555 #endif
556 #ifdef ELIBEXEC
557 { ELIBEXEC, 83 },
558 #endif
559 #ifdef EILSEQ
560 { EILSEQ, 84 },
561 #endif
562 #ifdef ERESTART
563 { ERESTART, 85 },
564 #endif
565 #ifdef ESTRPIPE
566 { ESTRPIPE, 86 },
567 #endif
568 #ifdef EUSERS
569 { EUSERS, 87 },
570 #endif
571 #ifdef ENOTSOCK
572 { ENOTSOCK, 88 },
573 #endif
574 #ifdef EDESTADDRREQ
575 { EDESTADDRREQ, 89 },
576 #endif
577 #ifdef EMSGSIZE
578 { EMSGSIZE, 90 },
579 #endif
580 #ifdef EPROTOTYPE
581 { EPROTOTYPE, 91 },
582 #endif
583 #ifdef ENOPROTOOPT
584 { ENOPROTOOPT, 92 },
585 #endif
586 #ifdef EPROTONOSUPPORT
587 { EPROTONOSUPPORT, 93 },
588 #endif
589 #ifdef ESOCKTNOSUPPORT
590 { ESOCKTNOSUPPORT, 94 },
591 #endif
592 #ifdef EOPNOTSUPP
593 { EOPNOTSUPP, 95 },
594 #endif
595 #ifdef EPFNOSUPPORT
596 { EPFNOSUPPORT, 96 },
597 #endif
598 #ifdef EAFNOSUPPORT
599 { EAFNOSUPPORT, 97 },
600 #endif
601 #ifdef EADDRINUSE
602 { EADDRINUSE, 98 },
603 #endif
604 #ifdef EADDRNOTAVAIL
605 { EADDRNOTAVAIL, 99 },
606 #endif
607 #ifdef ENETDOWN
608 { ENETDOWN, 100 },
609 #endif
610 #ifdef ENETUNREACH
611 { ENETUNREACH, 101 },
612 #endif
613 #ifdef ENETRESET
614 { ENETRESET, 102 },
615 #endif
616 #ifdef ECONNABORTED
617 { ECONNABORTED, 103 },
618 #endif
619 #ifdef ECONNRESET
620 { ECONNRESET, 104 },
621 #endif
622 #ifdef ENOBUFS
623 { ENOBUFS, 105 },
624 #endif
625 #ifdef EISCONN
626 { EISCONN, 106 },
627 #endif
628 #ifdef ENOTCONN
629 { ENOTCONN, 107 },
630 #endif
631 #ifdef ESHUTDOWN
632 { ESHUTDOWN, 108 },
633 #endif
634 #ifdef ETOOMANYREFS
635 { ETOOMANYREFS, 109 },
636 #endif
637 #ifdef ETIMEDOUT
638 { ETIMEDOUT, 110 },
639 #endif
640 #ifdef ECONNREFUSED
641 { ECONNREFUSED, 111 },
642 #endif
643 #ifdef EHOSTDOWN
644 { EHOSTDOWN, 112 },
645 #endif
646 #ifdef EHOSTUNREACH
647 { EHOSTUNREACH, 113 },
648 #endif
649 #ifdef EALREADY
650 { EALREADY, 114 },
651 #endif
652 #ifdef EINPROGRESS
653 { EINPROGRESS, 115 },
654 #endif
655 #ifdef ESTALE
656 { ESTALE, 116 },
657 #endif
658 #ifdef EUCLEAN
659 { EUCLEAN, 117 },
660 #endif
661 #ifdef ENOTNAM
662 { ENOTNAM, 118 },
663 #endif
664 #ifdef ENAVAIL
665 { ENAVAIL, 119 },
666 #endif
667 #ifdef EISNAM
668 { EISNAM, 120 },
669 #endif
670 #ifdef EREMOTEIO
671 { EREMOTEIO, 121 },
672 #endif
673 #ifdef EDQUOT
674 { EDQUOT, 122 },
675 #endif
676 #ifdef ENOMEDIUM
677 { ENOMEDIUM, 123 },
678 #endif
679 #ifdef EMEDIUMTYPE
680 { EMEDIUMTYPE, 124 },
681 #endif
682 { 0, -1 }
683 };
684
685 /* Extracted by applying
686 perl -ne 'if ($_ =~ /^#define/) { split;
687 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
688 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
689 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
690 installation and removing synonyms and unnecessary items. Don't
691 forget the end-marker. */
692
693 /* These we treat specially, as they're used in the fcntl F_GETFL
694 syscall. For consistency, open_map is also manually edited to use
695 these macros. */
696 #define TARGET_O_ACCMODE 0x3
697 #define TARGET_O_RDONLY 0x0
698 #define TARGET_O_WRONLY 0x1
699
700 static const CB_TARGET_DEFS_MAP open_map[] = {
701 #ifdef O_ACCMODE
702 { O_ACCMODE, TARGET_O_ACCMODE },
703 #endif
704 #ifdef O_RDONLY
705 { O_RDONLY, TARGET_O_RDONLY },
706 #endif
707 #ifdef O_WRONLY
708 { O_WRONLY, TARGET_O_WRONLY },
709 #endif
710 #ifdef O_RDWR
711 { O_RDWR, 0x2 },
712 #endif
713 #ifdef O_CREAT
714 { O_CREAT, 0x40 },
715 #endif
716 #ifdef O_EXCL
717 { O_EXCL, 0x80 },
718 #endif
719 #ifdef O_NOCTTY
720 { O_NOCTTY, 0x100 },
721 #endif
722 #ifdef O_TRUNC
723 { O_TRUNC, 0x200 },
724 #endif
725 #ifdef O_APPEND
726 { O_APPEND, 0x400 },
727 #endif
728 #ifdef O_NONBLOCK
729 { O_NONBLOCK, 0x800 },
730 #endif
731 #ifdef O_NDELAY
732 { O_NDELAY, 0x0 },
733 #endif
734 #ifdef O_SYNC
735 { O_SYNC, 0x1000 },
736 #endif
737 #ifdef FASYNC
738 { FASYNC, 0x2000 },
739 #endif
740 #ifdef O_DIRECT
741 { O_DIRECT, 0x4000 },
742 #endif
743 #ifdef O_LARGEFILE
744 { O_LARGEFILE, 0x8000 },
745 #endif
746 #ifdef O_DIRECTORY
747 { O_DIRECTORY, 0x10000 },
748 #endif
749 #ifdef O_NOFOLLOW
750 { O_NOFOLLOW, 0x20000 },
751 #endif
752 { -1, -1 }
753 };
754
755 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
756 static SIM_CPU *current_cpu_for_cb_callback;
757
758 static int syscall_read_mem (host_callback *, struct cb_syscall *,
759 unsigned long, char *, int);
760 static int syscall_write_mem (host_callback *, struct cb_syscall *,
761 unsigned long, const char *, int);
762 static USI create_map (SIM_DESC, struct cris_sim_mmapped_page **,
763 USI addr, USI len);
764 static USI unmap_pages (SIM_DESC, struct cris_sim_mmapped_page **,
765 USI addr, USI len);
766 static USI is_mapped (SIM_DESC, struct cris_sim_mmapped_page **,
767 USI addr, USI len);
768 static void dump_statistics (SIM_CPU *current_cpu);
769 static void make_first_thread (SIM_CPU *current_cpu);
770
771 /* Read/write functions for system call interface. */
772
773 static int
774 syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED,
775 struct cb_syscall *sc,
776 unsigned long taddr, char *buf, int bytes)
777 {
778 SIM_DESC sd = (SIM_DESC) sc->p1;
779 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
780
781 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
782 }
783
784 static int
785 syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED,
786 struct cb_syscall *sc,
787 unsigned long taddr, const char *buf, int bytes)
788 {
789 SIM_DESC sd = (SIM_DESC) sc->p1;
790 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
791
792 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
793 }
794
795 /* When we risk running self-modified code (as in trampolines), this is
796 called from special-case insns. The silicon CRIS CPU:s have enough
797 cache snooping implemented making this a simulator-only issue. Tests:
798 gcc.c-torture/execute/931002-1.c execution, -O3 -g
799 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
800
801 void
802 cris_flush_simulator_decode_cache (SIM_CPU *current_cpu,
803 USI pc ATTRIBUTE_UNUSED)
804 {
805 SIM_DESC sd = CPU_STATE (current_cpu);
806
807 #if WITH_SCACHE
808 if (USING_SCACHE_P (sd))
809 scache_flush_cpu (current_cpu);
810 #endif
811 }
812
813 /* Output statistics at the end of a run. */
814 static void
815 dump_statistics (SIM_CPU *current_cpu)
816 {
817 SIM_DESC sd = CPU_STATE (current_cpu);
818 CRIS_MISC_PROFILE *profp
819 = CPU_CRIS_MISC_PROFILE (current_cpu);
820 unsigned64 total = profp->basic_cycle_count;
821 const char *textmsg = "Basic clock cycles, total @: %llu\n";
822
823 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
824 what's included in the "total" count only. */
825 switch (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
826 & FLAG_CRIS_MISC_PROFILE_ALL)
827 {
828 case FLAG_CRIS_MISC_PROFILE_SIMPLE:
829 break;
830
831 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED | FLAG_CRIS_MISC_PROFILE_SIMPLE):
832 textmsg
833 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
834 total += profp->unaligned_mem_dword_count;
835 break;
836
837 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | FLAG_CRIS_MISC_PROFILE_SIMPLE):
838 textmsg = "Schedulable clock cycles, total @: %llu\n";
839 total
840 += (profp->memsrc_stall_count
841 + profp->memraw_stall_count
842 + profp->movemsrc_stall_count
843 + profp->movemdst_stall_count
844 + profp->mulsrc_stall_count
845 + profp->jumpsrc_stall_count
846 + profp->unaligned_mem_dword_count);
847 break;
848
849 case FLAG_CRIS_MISC_PROFILE_ALL:
850 textmsg = "All accounted clock cycles, total @: %llu\n";
851 total
852 += (profp->memsrc_stall_count
853 + profp->memraw_stall_count
854 + profp->movemsrc_stall_count
855 + profp->movemdst_stall_count
856 + profp->movemaddr_stall_count
857 + profp->mulsrc_stall_count
858 + profp->jumpsrc_stall_count
859 + profp->branch_stall_count
860 + profp->jumptarget_stall_count
861 + profp->unaligned_mem_dword_count);
862 break;
863
864 default:
865 abort ();
866
867 sim_io_eprintf (sd,
868 "Internal inconsistency at %s:%d",
869 __FILE__, __LINE__);
870 sim_engine_halt (sd, current_cpu, NULL, 0,
871 sim_stopped, SIM_SIGILL);
872 }
873
874 /* Historically, these messages have gone to stderr, so we'll keep it
875 that way. It's also easier to then tell it from normal program
876 output. FIXME: Add redirect option like "run -e file". */
877 sim_io_eprintf (sd, textmsg, total);
878
879 /* For v32, unaligned_mem_dword_count should always be 0. For
880 v10, memsrc_stall_count should always be 0. */
881 sim_io_eprintf (sd, "Memory source stall cycles: %llu\n",
882 (unsigned long long) (profp->memsrc_stall_count
883 + profp->unaligned_mem_dword_count));
884 sim_io_eprintf (sd, "Memory read-after-write stall cycles: %llu\n",
885 (unsigned long long) profp->memraw_stall_count);
886 sim_io_eprintf (sd, "Movem source stall cycles: %llu\n",
887 (unsigned long long) profp->movemsrc_stall_count);
888 sim_io_eprintf (sd, "Movem destination stall cycles: %llu\n",
889 (unsigned long long) profp->movemdst_stall_count);
890 sim_io_eprintf (sd, "Movem address stall cycles: %llu\n",
891 (unsigned long long) profp->movemaddr_stall_count);
892 sim_io_eprintf (sd, "Multiplication source stall cycles: %llu\n",
893 (unsigned long long) profp->mulsrc_stall_count);
894 sim_io_eprintf (sd, "Jump source stall cycles: %llu\n",
895 (unsigned long long) profp->jumpsrc_stall_count);
896 sim_io_eprintf (sd, "Branch misprediction stall cycles: %llu\n",
897 (unsigned long long) profp->branch_stall_count);
898 sim_io_eprintf (sd, "Jump target stall cycles: %llu\n",
899 (unsigned long long) profp->jumptarget_stall_count);
900 }
901
902 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
903 Return 1 if a overlap detected, 0 otherwise. */
904
905 static USI
906 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED,
907 struct cris_sim_mmapped_page **rootp,
908 USI addr, USI len)
909 {
910 struct cris_sim_mmapped_page *mapp;
911
912 if (len == 0 || (len & 8191))
913 abort ();
914
915 /* Iterate over the reverse-address sorted pages until we find a page in
916 or lower than the checked area. */
917 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
918 if (mapp->addr < addr + len && mapp->addr >= addr)
919 return 1;
920
921 return 0;
922 }
923
924 /* Check whether any part of [addr .. addr + len - 1] is *un*mapped.
925 Return 1 if the whole area is mapped, 0 otherwise. */
926
927 static USI
928 is_mapped_only (SIM_DESC sd ATTRIBUTE_UNUSED,
929 struct cris_sim_mmapped_page **rootp,
930 USI addr, USI len)
931 {
932 struct cris_sim_mmapped_page *mapp;
933
934 if (len == 0 || (len & 8191))
935 abort ();
936
937 /* Iterate over the reverse-address sorted pages until we find a page
938 lower than the checked area. */
939 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
940 if (addr == mapp->addr && len == 8192)
941 return 1;
942 else if (addr + len > mapp->addr)
943 len -= 8192;
944
945 return 0;
946 }
947
948 /* Debug helper; to be run from gdb. */
949
950 void
951 cris_dump_map (SIM_CPU *current_cpu)
952 {
953 struct cris_sim_mmapped_page *mapp;
954 USI start, end;
955
956 for (mapp = current_cpu->highest_mmapped_page,
957 start = mapp == NULL ? 0 : mapp->addr + 8192,
958 end = mapp == NULL ? 0 : mapp->addr + 8191;
959 mapp != NULL;
960 mapp = mapp->prev)
961 {
962 if (mapp->addr != start - 8192)
963 {
964 sim_io_eprintf (CPU_STATE (current_cpu), "0x%x..0x%x\n", start, end);
965 end = mapp->addr + 8191;
966 }
967
968 start = mapp->addr;
969 }
970
971 if (current_cpu->highest_mmapped_page != NULL)
972 sim_io_eprintf (CPU_STATE (current_cpu), "0x%x..0x%x\n", start, end);
973 }
974
975 /* Create mmapped memory. */
976
977 static USI
978 create_map (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
979 USI len)
980 {
981 struct cris_sim_mmapped_page *mapp;
982 struct cris_sim_mmapped_page **higher_prevp = rootp;
983 USI new_addr = 0x40000000;
984
985 if (addr != 0)
986 new_addr = addr;
987 else if (*rootp)
988 new_addr = rootp[0]->addr + 8192;
989
990 if (len != 8192)
991 {
992 USI page_addr;
993
994 if (len & 8191)
995 /* Which is better: return an error for this, or just round it up? */
996 abort ();
997
998 /* Do a recursive call for each page in the request. */
999 for (page_addr = new_addr; len != 0; page_addr += 8192, len -= 8192)
1000 if (create_map (sd, rootp, page_addr, 8192) >= (USI) -8191)
1001 abort ();
1002
1003 return new_addr;
1004 }
1005
1006 for (mapp = *rootp;
1007 mapp != NULL && mapp->addr > new_addr;
1008 mapp = mapp->prev)
1009 higher_prevp = &mapp->prev;
1010
1011 /* Allocate the new page, on the next higher page from the last one
1012 allocated, and link in the new descriptor before previous ones. */
1013 mapp = malloc (sizeof (*mapp));
1014
1015 if (mapp == NULL)
1016 return (USI) -ENOMEM;
1017
1018 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1019 new_addr, len,
1020 0, NULL, NULL);
1021
1022 mapp->addr = new_addr;
1023 mapp->prev = *higher_prevp;
1024 *higher_prevp = mapp;
1025
1026 return new_addr;
1027 }
1028
1029 /* Unmap one or more pages. */
1030
1031 static USI
1032 unmap_pages (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
1033 USI len)
1034 {
1035 struct cris_sim_mmapped_page *mapp;
1036 struct cris_sim_mmapped_page **higher_prevp = rootp;
1037
1038 if (len != 8192)
1039 {
1040 USI page_addr;
1041
1042 if (len & 8191)
1043 /* Which is better: return an error for this, or just round it up? */
1044 abort ();
1045
1046 /* Loop backwards to make each call is O(1) over the number of pages
1047 allocated, if we're unmapping from the high end of the pages. */
1048 for (page_addr = addr + len - 8192;
1049 page_addr >= addr;
1050 page_addr -= 8192)
1051 if (unmap_pages (sd, rootp, page_addr, 8192) != 0)
1052 abort ();
1053
1054 return 0;
1055 }
1056
1057 for (mapp = *rootp; mapp != NULL && mapp->addr > addr; mapp = mapp->prev)
1058 higher_prevp = &mapp->prev;
1059
1060 if (mapp == NULL || mapp->addr != addr)
1061 return EINVAL;
1062
1063 *higher_prevp = mapp->prev;
1064 sim_core_detach (sd, NULL, 0, 0, addr);
1065 free (mapp);
1066 return 0;
1067 }
1068
1069 /* The semantic code invokes this for illegal (unrecognized) instructions. */
1070
1071 SEM_PC
1072 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
1073 {
1074 SIM_DESC sd = CPU_STATE (current_cpu);
1075
1076 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
1077 return vpc;
1078 }
1079
1080 /* Handlers from the CGEN description that should not be called. */
1081
1082 USI
1083 cris_bmod_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1084 UINT srcreg ATTRIBUTE_UNUSED,
1085 USI dstreg ATTRIBUTE_UNUSED)
1086 {
1087 abort ();
1088 }
1089
1090 void
1091 h_supr_set_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1092 UINT index ATTRIBUTE_UNUSED,
1093 USI page ATTRIBUTE_UNUSED,
1094 USI newval ATTRIBUTE_UNUSED)
1095 {
1096 abort ();
1097 }
1098
1099 USI
1100 h_supr_get_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1101 UINT index ATTRIBUTE_UNUSED,
1102 USI page ATTRIBUTE_UNUSED)
1103 {
1104 abort ();
1105 }
1106
1107 /* Swap one context for another. */
1108
1109 static void
1110 schedule (SIM_CPU *current_cpu, int next)
1111 {
1112 /* Need to mark context-switches in the trace output. */
1113 if ((CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1114 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE))
1115 cris_trace_printf (CPU_STATE (current_cpu), current_cpu,
1116 "\t#:%d\n", next);
1117
1118 /* Copy the current context (if there is one) to its slot. */
1119 if (current_cpu->thread_data[current_cpu->threadno].cpu_context)
1120 memcpy (current_cpu->thread_data[current_cpu->threadno].cpu_context,
1121 &current_cpu->cpu_data_placeholder,
1122 current_cpu->thread_cpu_data_size);
1123
1124 /* Copy the new context from its slot. */
1125 memcpy (&current_cpu->cpu_data_placeholder,
1126 current_cpu->thread_data[next].cpu_context,
1127 current_cpu->thread_cpu_data_size);
1128
1129 /* Update needed stuff to indicate the new context. */
1130 current_cpu->threadno = next;
1131
1132 /* Handle pending signals. */
1133 if (current_cpu->thread_data[next].sigpending
1134 /* We don't run nested signal handlers. This means that pause(2)
1135 and sigsuspend(2) do not work in sighandlers, but that
1136 shouldn't be too hard a restriction. It also greatly
1137 simplifies the code. */
1138 && current_cpu->thread_data[next].cpu_context_atsignal == NULL)
1139 {
1140 int sig;
1141
1142 /* See if there's really a pending, non-blocked handler. We don't
1143 queue signals, so just use the first one in ascending order. */
1144 for (sig = 0; sig < 64; sig++)
1145 if (current_cpu->thread_data[next].sigdata[sig].pending
1146 && !current_cpu->thread_data[next].sigdata[sig].blocked)
1147 {
1148 bfd_byte regbuf[4];
1149 USI sp;
1150 int i;
1151 USI blocked;
1152 USI pc = sim_pc_get (current_cpu);
1153
1154 /* It's simpler to save the CPU context inside the simulator
1155 than on the stack. */
1156 current_cpu->thread_data[next].cpu_context_atsignal
1157 = (*current_cpu
1158 ->make_thread_cpu_data) (current_cpu,
1159 current_cpu->thread_data[next]
1160 .cpu_context);
1161
1162 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1163 sp = bfd_getl32 (regbuf);
1164
1165 /* Make sure we have an aligned stack. */
1166 sp &= ~3;
1167
1168 /* Make room for the signal frame, aligned. FIXME: Check that
1169 the memory exists, map it in if absent. (BTW, should also
1170 implement on-access automatic stack allocation). */
1171 sp -= 20;
1172
1173 /* This isn't the same signal frame as the kernel uses, because
1174 we don't want to bother getting all registers on and off the
1175 stack. */
1176
1177 /* First, we store the currently blocked signals. */
1178 blocked = 0;
1179 for (i = 0; i < 32; i++)
1180 blocked
1181 |= current_cpu->thread_data[next].sigdata[i + 1].blocked << i;
1182 sim_core_write_aligned_4 (current_cpu, pc, 0, sp, blocked);
1183 blocked = 0;
1184 for (i = 0; i < 31; i++)
1185 blocked
1186 |= current_cpu->thread_data[next].sigdata[i + 33].blocked << i;
1187 sim_core_write_aligned_4 (current_cpu, pc, 0, sp + 4, blocked);
1188
1189 /* Then, the actual instructions. This is CPU-specific, but we
1190 use instructions from the common subset for v10 and v32 which
1191 should be safe for the time being but could be parametrized
1192 if need be. */
1193 /* MOVU.W [PC+],R9. */
1194 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 8, 0x9c5f);
1195 /* .WORD TARGET_SYS_sigreturn. */
1196 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 10,
1197 TARGET_SYS_sigreturn);
1198 /* BREAK 13. */
1199 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 12, 0xe93d);
1200
1201 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1202 instruction. Still, it doesn't matter because v10 has no
1203 delay slot for BREAK so it will not be executed). */
1204 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 16, 0x05b0);
1205
1206 /* Modify registers to hold the right values for the sighandler
1207 context: updated stackpointer and return address pointing to
1208 the sigreturn stub. */
1209 bfd_putl32 (sp, regbuf);
1210 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1211 bfd_putl32 (sp + 8, regbuf);
1212 (*CPU_REG_STORE (current_cpu)) (current_cpu, TARGET_SRP_REGNUM,
1213 regbuf, 4);
1214
1215 current_cpu->thread_data[next].sigdata[sig].pending = 0;
1216
1217 /* Block this signal (for the duration of the sighandler). */
1218 current_cpu->thread_data[next].sigdata[sig].blocked = 1;
1219
1220 sim_pc_set (current_cpu, current_cpu->sighandler[sig]);
1221 bfd_putl32 (sig, regbuf);
1222 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10,
1223 regbuf, 4);
1224
1225 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1226 needed all this for, specifies a SA_SIGINFO call but treats it
1227 like an ordinary sighandler; only the signal number argument is
1228 inspected. To make future need to implement SA_SIGINFO
1229 correctly possible, we set the siginfo argument register to a
1230 magic (hopefully non-address) number. (NB: then, you should
1231 just need to pass the siginfo argument; it seems you probably
1232 don't need to implement the specific rt_sigreturn.) */
1233 bfd_putl32 (0xbad5161f, regbuf);
1234 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R11,
1235 regbuf, 4);
1236
1237 /* The third argument is unused and the kernel sets it to 0. */
1238 bfd_putl32 (0, regbuf);
1239 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R12,
1240 regbuf, 4);
1241 return;
1242 }
1243
1244 /* No, there actually was no pending signal for this thread. Reset
1245 this flag. */
1246 current_cpu->thread_data[next].sigpending = 0;
1247 }
1248 }
1249
1250 /* Reschedule the simplest possible way until something else is absolutely
1251 necessary:
1252 - A. Find the next process (round-robin) that doesn't have at_syscall
1253 set, schedule it.
1254 - B. If there is none, just run the next process, round-robin.
1255 - Clear at_syscall for the current process. */
1256
1257 static void
1258 reschedule (SIM_CPU *current_cpu)
1259 {
1260 int i;
1261
1262 /* Iterate over all thread slots, because after a few thread creations
1263 and exits, we don't know where the live ones are. */
1264 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1265 i != current_cpu->threadno;
1266 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1267 if (current_cpu->thread_data[i].cpu_context
1268 && current_cpu->thread_data[i].at_syscall == 0)
1269 {
1270 schedule (current_cpu, i);
1271 return;
1272 }
1273
1274 /* Pick any next live thread. */
1275 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1276 i != current_cpu->threadno;
1277 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1278 if (current_cpu->thread_data[i].cpu_context)
1279 {
1280 schedule (current_cpu, i);
1281 return;
1282 }
1283
1284 /* More than one live thread, but we couldn't find the next one? */
1285 abort ();
1286 }
1287
1288 /* Set up everything to receive (or IGN) an incoming signal to the
1289 current context. */
1290
1291 static int
1292 deliver_signal (SIM_CPU *current_cpu, int sig, unsigned int pid)
1293 {
1294 int i;
1295 USI pc = sim_pc_get (current_cpu);
1296
1297 /* Find the thread index of the pid. */
1298 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1299 /* Apparently it's ok to send signals to zombies (so a check for
1300 current_cpu->thread_data[i].cpu_context != NULL would be
1301 wrong). */
1302 if (current_cpu->thread_data[i].threadid == pid - TARGET_PID)
1303 {
1304 if (sig < 64)
1305 switch (current_cpu->sighandler[sig])
1306 {
1307 case TARGET_SIG_DFL:
1308 switch (sig)
1309 {
1310 /* The following according to the glibc
1311 documentation. (The kernel code has non-obvious
1312 execution paths.) */
1313 case TARGET_SIGFPE:
1314 case TARGET_SIGILL:
1315 case TARGET_SIGSEGV:
1316 case TARGET_SIGBUS:
1317 case TARGET_SIGABRT:
1318 case TARGET_SIGTRAP:
1319 case TARGET_SIGSYS:
1320
1321 case TARGET_SIGTERM:
1322 case TARGET_SIGINT:
1323 case TARGET_SIGQUIT:
1324 case TARGET_SIGKILL:
1325 case TARGET_SIGHUP:
1326
1327 case TARGET_SIGALRM:
1328 case TARGET_SIGVTALRM:
1329 case TARGET_SIGPROF:
1330 case TARGET_SIGSTOP:
1331
1332 case TARGET_SIGPIPE:
1333 case TARGET_SIGLOST:
1334 case TARGET_SIGXCPU:
1335 case TARGET_SIGXFSZ:
1336 case TARGET_SIGUSR1:
1337 case TARGET_SIGUSR2:
1338 sim_io_eprintf (CPU_STATE (current_cpu),
1339 "Exiting pid %d due to signal %d\n",
1340 pid, sig);
1341 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1342 NULL, pc, sim_stopped,
1343 sig == TARGET_SIGABRT
1344 ? SIM_SIGABRT : SIM_SIGILL);
1345 return 0;
1346
1347 /* The default for all other signals is to be ignored. */
1348 default:
1349 return 0;
1350 }
1351
1352 case TARGET_SIG_IGN:
1353 switch (sig)
1354 {
1355 case TARGET_SIGKILL:
1356 case TARGET_SIGSTOP:
1357 /* Can't ignore these signals. */
1358 sim_io_eprintf (CPU_STATE (current_cpu),
1359 "Exiting pid %d due to signal %d\n",
1360 pid, sig);
1361 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1362 NULL, pc, sim_stopped, SIM_SIGILL);
1363 return 0;
1364
1365 default:
1366 return 0;
1367 }
1368 break;
1369
1370 default:
1371 /* Mark the signal as pending, making schedule () check
1372 closer. The signal will be handled when the thread is
1373 scheduled and the signal is unblocked. */
1374 current_cpu->thread_data[i].sigdata[sig].pending = 1;
1375 current_cpu->thread_data[i].sigpending = 1;
1376 return 0;
1377 }
1378 else
1379 {
1380 sim_io_eprintf (CPU_STATE (current_cpu),
1381 "Unimplemented signal: %d\n", sig);
1382 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
1383 sim_stopped, SIM_SIGILL);
1384 }
1385 }
1386
1387 return
1388 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu)),
1389 ESRCH);
1390 }
1391
1392 /* Make the vector and the first item, the main thread. */
1393
1394 static void
1395 make_first_thread (SIM_CPU *current_cpu)
1396 {
1397 current_cpu->thread_data
1398 = xcalloc (1,
1399 SIM_TARGET_MAX_THREADS
1400 * sizeof (current_cpu->thread_data[0]));
1401 current_cpu->thread_data[0].cpu_context
1402 = (*current_cpu->make_thread_cpu_data) (current_cpu,
1403 &current_cpu
1404 ->cpu_data_placeholder);
1405 current_cpu->thread_data[0].parent_threadid = -1;
1406
1407 /* For good measure. */
1408 if (TARGET_SIG_DFL != 0)
1409 abort ();
1410 }
1411
1412 /* Handle unknown system calls. Returns (if it does) the syscall
1413 return value. */
1414
1415 static USI
1416 cris_unknown_syscall (SIM_CPU *current_cpu, USI pc, char *s, ...)
1417 {
1418 SIM_DESC sd = CPU_STATE (current_cpu);
1419 host_callback *cb = STATE_CALLBACK (sd);
1420
1421 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP
1422 || cris_unknown_syscall_action == CRIS_USYSC_MSG_ENOSYS)
1423 {
1424 va_list ap;
1425
1426 va_start (ap, s);
1427 sim_io_evprintf (sd, s, ap);
1428 va_end (ap);
1429
1430 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP)
1431 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1432 }
1433
1434 return -cb_host_to_target_errno (cb, ENOSYS);
1435 }
1436
1437 /* Main function: the handler of the "break 13" syscall insn. */
1438
1439 USI
1440 cris_break_13_handler (SIM_CPU *current_cpu, USI callnum, USI arg1,
1441 USI arg2, USI arg3, USI arg4, USI arg5, USI arg6,
1442 USI pc)
1443 {
1444 CB_SYSCALL s;
1445 SIM_DESC sd = CPU_STATE (current_cpu);
1446 host_callback *cb = STATE_CALLBACK (sd);
1447 int retval;
1448 int threadno = current_cpu->threadno;
1449
1450 current_cpu->syscalls++;
1451
1452 CB_SYSCALL_INIT (&s);
1453 s.func = callnum;
1454 s.arg1 = arg1;
1455 s.arg2 = arg2;
1456 s.arg3 = arg3;
1457
1458 if (callnum == TARGET_SYS_exit && current_cpu->m1threads == 0)
1459 {
1460 if (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1461 & FLAG_CRIS_MISC_PROFILE_ALL)
1462 dump_statistics (current_cpu);
1463 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
1464 }
1465
1466 s.p1 = (PTR) sd;
1467 s.p2 = (PTR) current_cpu;
1468 s.read_mem = syscall_read_mem;
1469 s.write_mem = syscall_write_mem;
1470
1471 current_cpu_for_cb_callback = current_cpu;
1472
1473 if (cb_syscall (cb, &s) != CB_RC_OK)
1474 {
1475 abort ();
1476 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
1477 s.result);
1478 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1479 }
1480
1481 retval = s.result == -1 ? -s.errcode : s.result;
1482
1483 if (s.errcode != 0 && s.errcode == cb_host_to_target_errno (cb, ENOSYS))
1484 {
1485 /* If the generic simulator call said ENOSYS, then let's try the
1486 ones we know ourselves.
1487
1488 The convention is to provide *very limited* functionality on an
1489 as-needed basis, only what's covered by the test-suite, tests
1490 added when functionality changes and abort with a descriptive
1491 message for *everything* else. Where there's no test-case, we
1492 just abort. */
1493 switch (callnum)
1494 {
1495 case 0:
1496 /* It's a pretty safe bet that the "old setup() system call"
1497 number will not be re-used; we can't say the same for higher
1498 numbers. We treat this simulator-generated call as "wait
1499 forever"; we re-run this insn. The wait is ended by a
1500 callback. Sanity check that this is the reason we got
1501 here. */
1502 if (current_cpu->thread_data == NULL
1503 || (current_cpu->thread_data[threadno].pipe_write_fd == 0))
1504 goto unimplemented_syscall;
1505
1506 sim_pc_set (current_cpu, pc);
1507 retval = arg1;
1508 break;
1509
1510 case TARGET_SYS_fcntl64:
1511 case TARGET_SYS_fcntl:
1512 switch (arg2)
1513 {
1514 case 1:
1515 /* F_GETFD.
1516 Glibc checks stdin, stdout and stderr fd:s for
1517 close-on-exec security sanity. We just need to provide a
1518 OK return value. If we really need to have a
1519 close-on-exec flag true, we could just do a real fcntl
1520 here. */
1521 retval = 0;
1522 break;
1523
1524 case 2:
1525 /* F_SETFD. Just ignore attempts to set the close-on-exec
1526 flag. */
1527 retval = 0;
1528 break;
1529
1530 case 3:
1531 /* F_GETFL. Check for the special case for open+fdopen. */
1532 if (current_cpu->last_syscall == TARGET_SYS_open
1533 && arg1 == current_cpu->last_open_fd)
1534 {
1535 retval = current_cpu->last_open_flags & TARGET_O_ACCMODE;
1536 break;
1537 }
1538 else if (arg1 == 0)
1539 {
1540 /* Because we can't freopen fd:s 0, 1, 2 to mean
1541 something else than stdin, stdout and stderr
1542 (sim/common/syscall.c:cb_syscall special cases fd
1543 0, 1 and 2), we know what flags that we can
1544 sanely return for these fd:s. */
1545 retval = TARGET_O_RDONLY;
1546 break;
1547 }
1548 else if (arg1 == 1 || arg1 == 2)
1549 {
1550 retval = TARGET_O_WRONLY;
1551 break;
1552 }
1553 /* FALLTHROUGH */
1554 default:
1555 /* Nothing else is implemented. */
1556 retval
1557 = cris_unknown_syscall (current_cpu, pc,
1558 "Unimplemented %s syscall "
1559 "(fd: 0x%lx: cmd: 0x%lx arg: "
1560 "0x%lx)\n",
1561 callnum == TARGET_SYS_fcntl
1562 ? "fcntl" : "fcntl64",
1563 (unsigned long) (USI) arg1,
1564 (unsigned long) (USI) arg2,
1565 (unsigned long) (USI) arg3);
1566 break;
1567 }
1568 break;
1569
1570 case TARGET_SYS_uname:
1571 {
1572 /* Fill in a few constants to appease glibc. */
1573 static const char sim_utsname[6][65] =
1574 {
1575 "Linux",
1576 "sim-target",
1577 "2.4.5",
1578 TARGET_UTSNAME,
1579 "cris",
1580 "localdomain"
1581 };
1582
1583 if ((s.write_mem) (cb, &s, arg1, (const char *) sim_utsname,
1584 sizeof (sim_utsname))
1585 != sizeof (sim_utsname))
1586 retval = -cb_host_to_target_errno (cb, EFAULT);
1587 else
1588 retval = 0;
1589 break;
1590 }
1591
1592 case TARGET_SYS_geteuid32:
1593 /* We tell the truth with these. Maybe we shouldn't, but it
1594 should match the "stat" information. */
1595 retval = geteuid ();
1596 break;
1597
1598 case TARGET_SYS_getuid32:
1599 retval = getuid ();
1600 break;
1601
1602 case TARGET_SYS_getegid32:
1603 retval = getegid ();
1604 break;
1605
1606 case TARGET_SYS_getgid32:
1607 retval = getgid ();
1608 break;
1609
1610 case TARGET_SYS_brk:
1611 /* Most often, we just return the argument, like the Linux
1612 kernel. */
1613 retval = arg1;
1614
1615 if (arg1 == 0)
1616 retval = current_cpu->endbrk;
1617 else if (arg1 <= current_cpu->endmem)
1618 current_cpu->endbrk = arg1;
1619 else
1620 {
1621 USI new_end = (arg1 + 8191) & ~8191;
1622
1623 /* If the simulator wants to brk more than a certain very
1624 large amount, something is wrong. FIXME: Return an error
1625 or abort? Have command-line selectable? */
1626 if (new_end - current_cpu->endmem > SIM_MAX_ALLOC_CHUNK)
1627 {
1628 current_cpu->endbrk = current_cpu->endmem;
1629 retval = current_cpu->endmem;
1630 break;
1631 }
1632
1633 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1634 current_cpu->endmem,
1635 new_end - current_cpu->endmem,
1636 0, NULL, NULL);
1637 current_cpu->endbrk = arg1;
1638 current_cpu->endmem = new_end;
1639 }
1640 break;
1641
1642 case TARGET_SYS_getpid:
1643 /* Correct until CLONE_THREAD is implemented. */
1644 retval = current_cpu->thread_data == NULL
1645 ? TARGET_PID
1646 : TARGET_PID + current_cpu->thread_data[threadno].threadid;
1647 break;
1648
1649 case TARGET_SYS_getppid:
1650 /* Correct until CLONE_THREAD is implemented. */
1651 retval = current_cpu->thread_data == NULL
1652 ? TARGET_PID - 1
1653 : (TARGET_PID
1654 + current_cpu->thread_data[threadno].parent_threadid);
1655 break;
1656
1657 case TARGET_SYS_mmap2:
1658 {
1659 USI addr = arg1;
1660 USI len = arg2;
1661 USI prot = arg3;
1662 USI flags = arg4;
1663 USI fd = arg5;
1664 USI pgoff = arg6;
1665
1666 /* At 2.6.27, Linux (many (all?) ports, in the mmap2 syscalls)
1667 still masked away this bit, so let's just ignore
1668 it. */
1669 flags &= ~TARGET_MAP_DENYWRITE;
1670
1671 /* If the simulator wants to mmap more than the very large
1672 limit, something is wrong. FIXME: Return an error or
1673 abort? Have command-line selectable? */
1674 if (len > SIM_MAX_ALLOC_CHUNK)
1675 {
1676 retval = -cb_host_to_target_errno (cb, ENOMEM);
1677 break;
1678 }
1679
1680 if ((prot != (TARGET_PROT_READ | TARGET_PROT_WRITE)
1681 && (prot
1682 != (TARGET_PROT_READ
1683 | TARGET_PROT_WRITE
1684 | TARGET_PROT_EXEC))
1685 && (prot != (TARGET_PROT_READ | TARGET_PROT_EXEC))
1686 && prot != TARGET_PROT_READ)
1687 || (flags != (TARGET_MAP_ANONYMOUS | TARGET_MAP_PRIVATE)
1688 && flags != TARGET_MAP_PRIVATE
1689 && flags != (TARGET_MAP_ANONYMOUS
1690 | TARGET_MAP_PRIVATE | TARGET_MAP_FIXED)
1691 && flags != (TARGET_MAP_PRIVATE | TARGET_MAP_FIXED)
1692 && flags != TARGET_MAP_SHARED)
1693 || (fd != (USI) -1
1694 && prot != TARGET_PROT_READ
1695 && prot != (TARGET_PROT_READ | TARGET_PROT_EXEC)
1696 && prot != (TARGET_PROT_READ | TARGET_PROT_WRITE))
1697 || (fd == (USI) -1 && pgoff != 0)
1698 || (fd != (USI) -1 && (flags & TARGET_MAP_ANONYMOUS))
1699 || ((flags & TARGET_MAP_FIXED) == 0
1700 && is_mapped (sd, &current_cpu->highest_mmapped_page,
1701 addr, (len + 8191) & ~8191)))
1702 {
1703 retval
1704 = cris_unknown_syscall (current_cpu, pc,
1705 "Unimplemented mmap2 call "
1706 "(0x%lx, 0x%lx, 0x%lx, "
1707 "0x%lx, 0x%lx, 0x%lx)\n",
1708 (unsigned long) arg1,
1709 (unsigned long) arg2,
1710 (unsigned long) arg3,
1711 (unsigned long) arg4,
1712 (unsigned long) arg5,
1713 (unsigned long) arg6);
1714 break;
1715 }
1716 else if (fd != (USI) -1)
1717 {
1718 /* Map a file. */
1719
1720 USI newaddr;
1721 USI pos;
1722
1723 /* A non-aligned argument is allowed for files. */
1724 USI newlen = (len + 8191) & ~8191;
1725
1726 /* We only support read, read|exec, and read|write,
1727 which we should already have checked. Check again
1728 anyway. */
1729 if (prot != TARGET_PROT_READ
1730 && prot != (TARGET_PROT_READ | TARGET_PROT_EXEC)
1731 && prot != (TARGET_PROT_READ | TARGET_PROT_WRITE))
1732 abort ();
1733
1734 if ((flags & TARGET_MAP_FIXED)
1735 && unmap_pages (sd, &current_cpu->highest_mmapped_page,
1736 addr, newlen) != 0)
1737 abort ();
1738
1739 newaddr
1740 = create_map (sd, &current_cpu->highest_mmapped_page, addr,
1741 newlen);
1742
1743 if (newaddr >= (USI) -8191)
1744 {
1745 abort ();
1746 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1747 break;
1748 }
1749
1750 /* We were asked for MAP_FIXED, but couldn't. */
1751 if ((flags & TARGET_MAP_FIXED) && newaddr != addr)
1752 {
1753 abort ();
1754 unmap_pages (sd, &current_cpu->highest_mmapped_page,
1755 newaddr, newlen);
1756 retval = -cb_host_to_target_errno (cb, EINVAL);
1757 break;
1758 }
1759
1760 /* Find the current position in the file. */
1761 s.func = TARGET_SYS_lseek;
1762 s.arg1 = fd;
1763 s.arg2 = 0;
1764 s.arg3 = SEEK_CUR;
1765 if (cb_syscall (cb, &s) != CB_RC_OK)
1766 abort ();
1767 pos = s.result;
1768
1769 if (s.result < 0)
1770 abort ();
1771
1772 /* Move to the correct offset in the file. */
1773 s.func = TARGET_SYS_lseek;
1774 s.arg1 = fd;
1775 s.arg2 = pgoff*8192;
1776 s.arg3 = SEEK_SET;
1777 if (cb_syscall (cb, &s) != CB_RC_OK)
1778 abort ();
1779
1780 if (s.result < 0)
1781 abort ();
1782
1783 /* Use the standard read callback to read in "len"
1784 bytes. */
1785 s.func = TARGET_SYS_read;
1786 s.arg1 = fd;
1787 s.arg2 = newaddr;
1788 s.arg3 = len;
1789 if (cb_syscall (cb, &s) != CB_RC_OK)
1790 abort ();
1791
1792 if ((USI) s.result != len)
1793 abort ();
1794
1795 /* After reading, we need to go back to the previous
1796 position in the file. */
1797 s.func = TARGET_SYS_lseek;
1798 s.arg1 = fd;
1799 s.arg2 = pos;
1800 s.arg3 = SEEK_SET;
1801 if (cb_syscall (cb, &s) != CB_RC_OK)
1802 abort ();
1803 if (pos != (USI) s.result)
1804 abort ();
1805
1806 retval = newaddr;
1807 }
1808 else
1809 {
1810 USI newlen = (len + 8191) & ~8191;
1811 USI newaddr;
1812
1813 if ((flags & TARGET_MAP_FIXED)
1814 && unmap_pages (sd, &current_cpu->highest_mmapped_page,
1815 addr, newlen) != 0)
1816 abort ();
1817
1818 newaddr = create_map (sd, &current_cpu->highest_mmapped_page, addr,
1819 newlen);
1820
1821 if (newaddr >= (USI) -8191)
1822 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1823 else
1824 retval = newaddr;
1825
1826 if ((flags & TARGET_MAP_FIXED) && newaddr != addr)
1827 {
1828 abort ();
1829 unmap_pages (sd, &current_cpu->highest_mmapped_page,
1830 newaddr, newlen);
1831 retval = -cb_host_to_target_errno (cb, EINVAL);
1832 break;
1833 }
1834 }
1835 break;
1836 }
1837
1838 case TARGET_SYS_mprotect:
1839 {
1840 /* We only cover the case of linuxthreads mprotecting out
1841 its stack guard page and of dynamic loading mprotecting
1842 away the data (for some reason the whole library, then
1843 mprotects away the data part and mmap-FIX:es it again. */
1844 USI addr = arg1;
1845 USI len = arg2;
1846 USI prot = arg3;
1847
1848 if (prot != TARGET_PROT_NONE
1849 || !is_mapped_only (sd, &current_cpu->highest_mmapped_page,
1850 addr, (len + 8191) & ~8191))
1851 {
1852 retval
1853 = cris_unknown_syscall (current_cpu, pc,
1854 "Unimplemented mprotect call "
1855 "(0x%lx, 0x%lx, 0x%lx)\n",
1856 (unsigned long) arg1,
1857 (unsigned long) arg2,
1858 (unsigned long) arg3);
1859 break;
1860 }
1861
1862 /* Just ignore this. We could make this equal to munmap,
1863 but then we'd have to make sure no anon mmaps gets this
1864 address before a subsequent MAP_FIXED mmap intended to
1865 override it. */
1866 retval = 0;
1867 break;
1868 }
1869
1870 case TARGET_SYS_ioctl:
1871 {
1872 /* We support only a very limited functionality: checking
1873 stdout with TCGETS to perform the isatty function. The
1874 TCGETS ioctl isn't actually performed or the result used by
1875 an isatty () caller in a "hello, world" program; only the
1876 return value is then used. Maybe we shouldn't care about
1877 the environment of the simulator regarding isatty, but
1878 that's been working before, in the xsim simulator. */
1879 if (arg2 == TARGET_TCGETS && arg1 == 1)
1880 retval = isatty (1) ? 0 : cb_host_to_target_errno (cb, EINVAL);
1881 else
1882 retval = -cb_host_to_target_errno (cb, EINVAL);
1883 break;
1884 }
1885
1886 case TARGET_SYS_munmap:
1887 {
1888 USI addr = arg1;
1889 USI len = arg2;
1890 USI result
1891 = unmap_pages (sd, &current_cpu->highest_mmapped_page, addr,
1892 len);
1893 retval = result != 0 ? -cb_host_to_target_errno (cb, result) : 0;
1894 break;
1895 }
1896
1897 case TARGET_SYS_wait4:
1898 {
1899 int i;
1900 USI pid = arg1;
1901 USI saddr = arg2;
1902 USI options = arg3;
1903 USI rusagep = arg4;
1904
1905 /* FIXME: We're not properly implementing __WCLONE, and we
1906 don't really need the special casing so we might as well
1907 make this general. */
1908 if ((!(pid == (USI) -1
1909 && options == (TARGET___WCLONE | TARGET_WNOHANG)
1910 && saddr != 0)
1911 && !(pid > 0
1912 && (options == TARGET___WCLONE
1913 || options == TARGET___WALL)))
1914 || rusagep != 0
1915 || current_cpu->thread_data == NULL)
1916 {
1917 retval
1918 = cris_unknown_syscall (current_cpu, pc,
1919 "Unimplemented wait4 call "
1920 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1921 (unsigned long) arg1,
1922 (unsigned long) arg2,
1923 (unsigned long) arg3,
1924 (unsigned long) arg4);
1925 break;
1926 }
1927
1928 if (pid == (USI) -1)
1929 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1930 {
1931 if (current_cpu->thread_data[threadno].threadid
1932 == current_cpu->thread_data[i].parent_threadid
1933 && current_cpu->thread_data[i].threadid != 0
1934 && current_cpu->thread_data[i].cpu_context == NULL)
1935 {
1936 /* A zombied child. Get the exit value and clear the
1937 zombied entry so it will be reused. */
1938 sim_core_write_unaligned_4 (current_cpu, pc, 0, saddr,
1939 current_cpu
1940 ->thread_data[i].exitval);
1941 retval
1942 = current_cpu->thread_data[i].threadid + TARGET_PID;
1943 memset (&current_cpu->thread_data[i], 0,
1944 sizeof (current_cpu->thread_data[i]));
1945 goto outer_break;
1946 }
1947 }
1948 else
1949 {
1950 /* We're waiting for a specific PID. If we don't find
1951 it zombied on this run, rerun the syscall. */
1952 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1953 if (pid == current_cpu->thread_data[i].threadid + TARGET_PID
1954 && current_cpu->thread_data[i].cpu_context == NULL)
1955 {
1956 if (saddr != 0)
1957 /* Get the exit value if the caller wants it. */
1958 sim_core_write_unaligned_4 (current_cpu, pc, 0,
1959 saddr,
1960 current_cpu
1961 ->thread_data[i]
1962 .exitval);
1963
1964 retval
1965 = current_cpu->thread_data[i].threadid + TARGET_PID;
1966 memset (&current_cpu->thread_data[i], 0,
1967 sizeof (current_cpu->thread_data[i]));
1968
1969 goto outer_break;
1970 }
1971
1972 sim_pc_set (current_cpu, pc);
1973 }
1974
1975 retval = -cb_host_to_target_errno (cb, ECHILD);
1976 outer_break:
1977 break;
1978 }
1979
1980 case TARGET_SYS_rt_sigaction:
1981 {
1982 USI signum = arg1;
1983 USI old_sa = arg3;
1984 USI new_sa = arg2;
1985
1986 /* The kernel says:
1987 struct sigaction {
1988 __sighandler_t sa_handler;
1989 unsigned long sa_flags;
1990 void (*sa_restorer)(void);
1991 sigset_t sa_mask;
1992 }; */
1993
1994 if (old_sa != 0)
1995 {
1996 sim_core_write_unaligned_4 (current_cpu, pc, 0, old_sa + 0,
1997 current_cpu->sighandler[signum]);
1998 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 4, 0);
1999 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 8, 0);
2000
2001 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
2002 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 12, 0);
2003 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 16, 0);
2004 }
2005 if (new_sa != 0)
2006 {
2007 USI target_sa_handler
2008 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa);
2009 USI target_sa_flags
2010 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 4);
2011 USI target_sa_restorer
2012 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 8);
2013 USI target_sa_mask_low
2014 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 12);
2015 USI target_sa_mask_high
2016 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 16);
2017
2018 /* We won't interrupt a syscall so we won't restart it,
2019 but a signal(2) call ends up syscalling rt_sigaction
2020 with this flag, so we have to handle it. The
2021 sa_restorer field contains garbage when not
2022 TARGET_SA_RESTORER, so don't look at it. For the
2023 time being, we don't nest sighandlers, so we
2024 ignore the sa_mask, which simplifies things. */
2025 if ((target_sa_flags != 0
2026 && target_sa_flags != TARGET_SA_RESTART
2027 && target_sa_flags != (TARGET_SA_RESTART|TARGET_SA_SIGINFO))
2028 || target_sa_handler == 0)
2029 {
2030 retval
2031 = cris_unknown_syscall (current_cpu, pc,
2032 "Unimplemented rt_sigaction "
2033 "syscall "
2034 "(0x%lx, 0x%lx: "
2035 "[0x%x, 0x%x, 0x%x, "
2036 "{0x%x, 0x%x}], 0x%lx)\n",
2037 (unsigned long) arg1,
2038 (unsigned long) arg2,
2039 target_sa_handler,
2040 target_sa_flags,
2041 target_sa_restorer,
2042 target_sa_mask_low,
2043 target_sa_mask_high,
2044 (unsigned long) arg3);
2045 break;
2046 }
2047
2048 current_cpu->sighandler[signum] = target_sa_handler;
2049
2050 /* Because we may have unblocked signals, one may now be
2051 pending, if there are threads, that is. */
2052 if (current_cpu->thread_data)
2053 current_cpu->thread_data[threadno].sigpending = 1;
2054 }
2055 retval = 0;
2056 break;
2057 }
2058
2059 case TARGET_SYS_mremap:
2060 {
2061 USI addr = arg1;
2062 USI old_len = arg2;
2063 USI new_len = arg3;
2064 USI flags = arg4;
2065 USI new_addr = arg5;
2066 USI mapped_addr;
2067
2068 if (new_len == old_len)
2069 /* The program and/or library is possibly confused but
2070 this is a valid call. Happens with ipps-1.40 on file
2071 svs_all. */
2072 retval = addr;
2073 else if (new_len < old_len)
2074 {
2075 /* Shrinking is easy. */
2076 if (unmap_pages (sd, &current_cpu->highest_mmapped_page,
2077 addr + new_len, old_len - new_len) != 0)
2078 retval = -cb_host_to_target_errno (cb, EINVAL);
2079 else
2080 retval = addr;
2081 }
2082 else if (! is_mapped (sd, &current_cpu->highest_mmapped_page,
2083 addr + old_len, new_len - old_len))
2084 {
2085 /* If the extension isn't mapped, we can just add it. */
2086 mapped_addr
2087 = create_map (sd, &current_cpu->highest_mmapped_page,
2088 addr + old_len, new_len - old_len);
2089
2090 if (mapped_addr > (USI) -8192)
2091 retval = -cb_host_to_target_errno (cb, -(SI) mapped_addr);
2092 else
2093 retval = addr;
2094 }
2095 else if (flags & TARGET_MREMAP_MAYMOVE)
2096 {
2097 /* Create a whole new map and copy the contents
2098 block-by-block there. We ignore the new_addr argument
2099 for now. */
2100 char buf[8192];
2101 USI prev_addr = addr;
2102 USI prev_len = old_len;
2103
2104 mapped_addr
2105 = create_map (sd, &current_cpu->highest_mmapped_page,
2106 0, new_len);
2107
2108 if (mapped_addr > (USI) -8192)
2109 {
2110 retval = -cb_host_to_target_errno (cb, -(SI) new_addr);
2111 break;
2112 }
2113
2114 retval = mapped_addr;
2115
2116 for (; old_len > 0;
2117 old_len -= 8192, mapped_addr += 8192, addr += 8192)
2118 {
2119 if (sim_core_read_buffer (sd, current_cpu, read_map, buf,
2120 addr, 8192) != 8192
2121 || sim_core_write_buffer (sd, current_cpu, 0, buf,
2122 mapped_addr, 8192) != 8192)
2123 abort ();
2124 }
2125
2126 if (unmap_pages (sd, &current_cpu->highest_mmapped_page,
2127 prev_addr, prev_len) != 0)
2128 abort ();
2129 }
2130 else
2131 retval = -cb_host_to_target_errno (cb, -ENOMEM);
2132 break;
2133 }
2134
2135 case TARGET_SYS_poll:
2136 {
2137 int npollfds = arg2;
2138 int timeout = arg3;
2139 SI ufds = arg1;
2140 SI fd = -1;
2141 HI events = -1;
2142 HI revents = 0;
2143 struct stat buf;
2144 int i;
2145
2146 /* The kernel says:
2147 struct pollfd {
2148 int fd;
2149 short events;
2150 short revents;
2151 }; */
2152
2153 /* Check that this is the expected poll call from
2154 linuxthreads/manager.c; we don't support anything else.
2155 Remember, fd == 0 isn't supported. */
2156 if (npollfds != 1
2157 || ((fd = sim_core_read_unaligned_4 (current_cpu, pc,
2158 0, ufds)) <= 0)
2159 || ((events = sim_core_read_unaligned_2 (current_cpu, pc,
2160 0, ufds + 4))
2161 != TARGET_POLLIN)
2162 || ((cb->fstat) (cb, fd, &buf) != 0
2163 || (buf.st_mode & S_IFIFO) == 0)
2164 || current_cpu->thread_data == NULL)
2165 {
2166 retval
2167 = cris_unknown_syscall (current_cpu, pc,
2168 "Unimplemented poll syscall "
2169 "(0x%lx: [0x%x, 0x%x, x], "
2170 "0x%lx, 0x%lx)\n",
2171 (unsigned long) arg1, fd, events,
2172 (unsigned long) arg2,
2173 (unsigned long) arg3);
2174 break;
2175 }
2176
2177 retval = 0;
2178
2179 /* Iterate over threads; find a marker that a writer is
2180 sleeping, waiting for a reader. */
2181 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2182 if (current_cpu->thread_data[i].cpu_context != NULL
2183 && current_cpu->thread_data[i].pipe_read_fd == fd)
2184 {
2185 revents = TARGET_POLLIN;
2186 retval = 1;
2187 break;
2188 }
2189
2190 /* Timeout decreases with whatever time passed between the
2191 last syscall and this. That's not exactly right for the
2192 first call, but it's close enough that it isn't
2193 worthwhile to complicate matters by making that a special
2194 case. */
2195 timeout
2196 -= (TARGET_TIME_MS (current_cpu)
2197 - (current_cpu->thread_data[threadno].last_execution));
2198
2199 /* Arrange to repeat this syscall until timeout or event,
2200 decreasing timeout at each iteration. */
2201 if (timeout > 0 && revents == 0)
2202 {
2203 bfd_byte timeout_buf[4];
2204
2205 bfd_putl32 (timeout, timeout_buf);
2206 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2207 H_GR_R12, timeout_buf, 4);
2208 sim_pc_set (current_cpu, pc);
2209 retval = arg1;
2210 break;
2211 }
2212
2213 sim_core_write_unaligned_2 (current_cpu, pc, 0, ufds + 4 + 2,
2214 revents);
2215 break;
2216 }
2217
2218 case TARGET_SYS_time:
2219 {
2220 retval = (int) (*cb->time) (cb, 0L);
2221
2222 /* At time of this writing, CB_SYSCALL_time doesn't do the
2223 part of setting *arg1 to the return value. */
2224 if (arg1)
2225 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, retval);
2226 break;
2227 }
2228
2229 case TARGET_SYS_gettimeofday:
2230 if (arg1 != 0)
2231 {
2232 USI ts = TARGET_TIME (current_cpu);
2233 USI tms = TARGET_TIME_MS (current_cpu);
2234
2235 /* First dword is seconds since TARGET_EPOCH. */
2236 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, ts);
2237
2238 /* Second dword is microseconds. */
2239 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1 + 4,
2240 (tms % 1000) * 1000);
2241 }
2242 if (arg2 != 0)
2243 {
2244 /* Time-zone info is always cleared. */
2245 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, 0);
2246 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, 0);
2247 }
2248 retval = 0;
2249 break;
2250
2251 case TARGET_SYS_llseek:
2252 {
2253 /* If it fits, tweak parameters to fit the "generic" 32-bit
2254 lseek and use that. */
2255 SI fd = arg1;
2256 SI offs_hi = arg2;
2257 SI offs_lo = arg3;
2258 SI resultp = arg4;
2259 SI whence = arg5;
2260 retval = 0;
2261
2262 if (!((offs_hi == 0 && offs_lo >= 0)
2263 || (offs_hi == -1 && offs_lo < 0)))
2264 {
2265 retval
2266 = cris_unknown_syscall (current_cpu, pc,
2267 "Unimplemented llseek offset,"
2268 " fd %d: 0x%x:0x%x\n",
2269 fd, (unsigned) arg2,
2270 (unsigned) arg3);
2271 break;
2272 }
2273
2274 s.func = TARGET_SYS_lseek;
2275 s.arg2 = offs_lo;
2276 s.arg3 = whence;
2277 if (cb_syscall (cb, &s) != CB_RC_OK)
2278 {
2279 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
2280 s.result);
2281 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2282 }
2283 if (s.result < 0)
2284 retval = -s.errcode;
2285 else
2286 {
2287 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp,
2288 s.result);
2289 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp + 4,
2290 s.result < 0 ? -1 : 0);
2291 }
2292 break;
2293 }
2294
2295 /* ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
2296 where:
2297 struct iovec {
2298 void *iov_base; Starting address
2299 size_t iov_len; Number of bytes to transfer
2300 }; */
2301 case TARGET_SYS_writev:
2302 {
2303 SI fd = arg1;
2304 SI iov = arg2;
2305 SI iovcnt = arg3;
2306 SI retcnt = 0;
2307 int i;
2308
2309 /* We'll ignore strict error-handling and just do multiple write calls. */
2310 for (i = 0; i < iovcnt; i++)
2311 {
2312 int sysret;
2313 USI iov_base
2314 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2315 iov + 8*i);
2316 USI iov_len
2317 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2318 iov + 8*i + 4);
2319
2320 s.func = TARGET_SYS_write;
2321 s.arg1 = fd;
2322 s.arg2 = iov_base;
2323 s.arg3 = iov_len;
2324
2325 if (cb_syscall (cb, &s) != CB_RC_OK)
2326 abort ();
2327 sysret = s.result == -1 ? -s.errcode : s.result;
2328
2329 if (sysret != iov_len)
2330 {
2331 if (i != 0)
2332 abort ();
2333 retcnt = sysret;
2334 break;
2335 }
2336
2337 retcnt += iov_len;
2338 }
2339
2340 retval = retcnt;
2341 }
2342 break;
2343
2344 /* This one does have a generic callback function, but at the time
2345 of this writing, cb_syscall does not have code for it, and we
2346 need target-specific code for the threads implementation
2347 anyway. */
2348 case TARGET_SYS_kill:
2349 {
2350 USI pid = arg1;
2351 USI sig = arg2;
2352
2353 retval = 0;
2354
2355 /* At kill(2), glibc sets signal masks such that the thread
2356 machinery is initialized. Still, there is and was only
2357 one thread. */
2358 if (current_cpu->max_threadid == 0)
2359 {
2360 if (pid != TARGET_PID)
2361 {
2362 retval = -cb_host_to_target_errno (cb, EPERM);
2363 break;
2364 }
2365
2366 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2367 if (sig == TARGET_SIGABRT)
2368 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2369 the end-point for failing GCC test-cases. */
2370 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2371 SIM_SIGABRT);
2372 else
2373 {
2374 sim_io_eprintf (sd, "Unimplemented signal: %d\n", sig);
2375 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2376 SIM_SIGILL);
2377 }
2378
2379 /* This will not be reached. */
2380 abort ();
2381 }
2382 else
2383 retval = deliver_signal (current_cpu, sig, pid);
2384 break;
2385 }
2386
2387 case TARGET_SYS_rt_sigprocmask:
2388 {
2389 int i;
2390 USI how = arg1;
2391 USI newsetp = arg2;
2392 USI oldsetp = arg3;
2393
2394 if (how != TARGET_SIG_BLOCK
2395 && how != TARGET_SIG_SETMASK
2396 && how != TARGET_SIG_UNBLOCK)
2397 {
2398 retval
2399 = cris_unknown_syscall (current_cpu, pc,
2400 "Unimplemented rt_sigprocmask "
2401 "syscall (0x%x, 0x%x, 0x%x)\n",
2402 arg1, arg2, arg3);
2403 break;
2404 }
2405
2406 if (newsetp)
2407 {
2408 USI set_low
2409 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2410 newsetp);
2411 USI set_high
2412 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2413 newsetp + 4);
2414
2415 /* The sigmask is kept in the per-thread data, so we may
2416 need to create the first one. */
2417 if (current_cpu->thread_data == NULL)
2418 make_first_thread (current_cpu);
2419
2420 if (how == TARGET_SIG_SETMASK)
2421 for (i = 0; i < 64; i++)
2422 current_cpu->thread_data[threadno].sigdata[i].blocked = 0;
2423
2424 for (i = 0; i < 32; i++)
2425 if ((set_low & (1 << i)))
2426 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2427 = (how != TARGET_SIG_UNBLOCK);
2428
2429 for (i = 0; i < 31; i++)
2430 if ((set_high & (1 << i)))
2431 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2432 = (how != TARGET_SIG_UNBLOCK);
2433
2434 /* The mask changed, so a signal may be unblocked for
2435 execution. */
2436 current_cpu->thread_data[threadno].sigpending = 1;
2437 }
2438
2439 if (oldsetp != 0)
2440 {
2441 USI set_low = 0;
2442 USI set_high = 0;
2443
2444 for (i = 0; i < 32; i++)
2445 if (current_cpu->thread_data[threadno]
2446 .sigdata[i + 1].blocked)
2447 set_low |= 1 << i;
2448 for (i = 0; i < 31; i++)
2449 if (current_cpu->thread_data[threadno]
2450 .sigdata[i + 33].blocked)
2451 set_high |= 1 << i;
2452
2453 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 0, set_low);
2454 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 4, set_high);
2455 }
2456
2457 retval = 0;
2458 break;
2459 }
2460
2461 case TARGET_SYS_sigreturn:
2462 {
2463 int i;
2464 bfd_byte regbuf[4];
2465 int was_sigsuspended;
2466
2467 if (current_cpu->thread_data == NULL
2468 /* The CPU context is saved with the simulator data, not
2469 on the stack as in the real world. */
2470 || (current_cpu->thread_data[threadno].cpu_context_atsignal
2471 == NULL))
2472 {
2473 retval
2474 = cris_unknown_syscall (current_cpu, pc,
2475 "Invalid sigreturn syscall: "
2476 "no signal handler active "
2477 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, "
2478 "0x%lx, 0x%lx)\n",
2479 (unsigned long) arg1,
2480 (unsigned long) arg2,
2481 (unsigned long) arg3,
2482 (unsigned long) arg4,
2483 (unsigned long) arg5,
2484 (unsigned long) arg6);
2485 break;
2486 }
2487
2488 was_sigsuspended
2489 = current_cpu->thread_data[threadno].sigsuspended;
2490
2491 /* Restore the sigmask, either from the stack copy made when
2492 the sighandler was called, or from the saved state
2493 specifically for sigsuspend(2). */
2494 if (was_sigsuspended)
2495 {
2496 current_cpu->thread_data[threadno].sigsuspended = 0;
2497 for (i = 0; i < 64; i++)
2498 current_cpu->thread_data[threadno].sigdata[i].blocked
2499 = current_cpu->thread_data[threadno]
2500 .sigdata[i].blocked_suspendsave;
2501 }
2502 else
2503 {
2504 USI sp;
2505 USI set_low;
2506 USI set_high;
2507
2508 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2509 H_GR_SP, regbuf, 4);
2510 sp = bfd_getl32 (regbuf);
2511 set_low
2512 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp);
2513 set_high
2514 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp + 4);
2515
2516 for (i = 0; i < 32; i++)
2517 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2518 = (set_low & (1 << i)) != 0;
2519 for (i = 0; i < 31; i++)
2520 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2521 = (set_high & (1 << i)) != 0;
2522 }
2523
2524 /* The mask changed, so a signal may be unblocked for
2525 execution. */
2526 current_cpu->thread_data[threadno].sigpending = 1;
2527
2528 memcpy (&current_cpu->cpu_data_placeholder,
2529 current_cpu->thread_data[threadno].cpu_context_atsignal,
2530 current_cpu->thread_cpu_data_size);
2531 free (current_cpu->thread_data[threadno].cpu_context_atsignal);
2532 current_cpu->thread_data[threadno].cpu_context_atsignal = NULL;
2533
2534 /* The return value must come from the saved R10. */
2535 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, regbuf, 4);
2536 retval = bfd_getl32 (regbuf);
2537
2538 /* We must also break the "sigsuspension loop". */
2539 if (was_sigsuspended)
2540 sim_pc_set (current_cpu, sim_pc_get (current_cpu) + 2);
2541 break;
2542 }
2543
2544 case TARGET_SYS_rt_sigsuspend:
2545 {
2546 USI newsetp = arg1;
2547 USI setsize = arg2;
2548
2549 if (setsize != 8)
2550 {
2551 retval
2552 = cris_unknown_syscall (current_cpu, pc,
2553 "Unimplemented rt_sigsuspend syscall"
2554 " arguments (0x%lx, 0x%lx)\n",
2555 (unsigned long) arg1,
2556 (unsigned long) arg2);
2557 break;
2558 }
2559
2560 /* Don't change the signal mask if we're already in
2561 sigsuspend state (i.e. this syscall is a rerun). */
2562 else if (!current_cpu->thread_data[threadno].sigsuspended)
2563 {
2564 USI set_low
2565 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2566 newsetp);
2567 USI set_high
2568 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2569 newsetp + 4);
2570 int i;
2571
2572 /* Save the current sigmask and insert the user-supplied
2573 one. */
2574 for (i = 0; i < 32; i++)
2575 {
2576 current_cpu->thread_data[threadno]
2577 .sigdata[i + 1].blocked_suspendsave
2578 = current_cpu->thread_data[threadno]
2579 .sigdata[i + 1].blocked;
2580
2581 current_cpu->thread_data[threadno]
2582 .sigdata[i + 1].blocked = (set_low & (1 << i)) != 0;
2583 }
2584 for (i = 0; i < 31; i++)
2585 {
2586 current_cpu->thread_data[threadno]
2587 .sigdata[i + 33].blocked_suspendsave
2588 = current_cpu->thread_data[threadno]
2589 .sigdata[i + 33].blocked;
2590 current_cpu->thread_data[threadno]
2591 .sigdata[i + 33].blocked = (set_high & (1 << i)) != 0;
2592 }
2593
2594 current_cpu->thread_data[threadno].sigsuspended = 1;
2595
2596 /* The mask changed, so a signal may be unblocked for
2597 execution. */
2598 current_cpu->thread_data[threadno].sigpending = 1;
2599 }
2600
2601 /* Because we don't use arg1 (newsetp) when this syscall is
2602 rerun, it doesn't matter that we overwrite it with the
2603 (constant) return value. */
2604 retval = -cb_host_to_target_errno (cb, EINTR);
2605 sim_pc_set (current_cpu, pc);
2606 break;
2607 }
2608
2609 /* Add case labels here for other syscalls using the 32-bit
2610 "struct stat", provided they have a corresponding simulator
2611 function of course. */
2612 case TARGET_SYS_stat:
2613 case TARGET_SYS_fstat:
2614 {
2615 /* As long as the infrastructure doesn't cache anything
2616 related to the stat mapping, this trick gets us a dual
2617 "struct stat"-type mapping in the least error-prone way. */
2618 const char *saved_map = cb->stat_map;
2619 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
2620
2621 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_stat32_map;
2622 cb->stat_map = stat32_map;
2623
2624 if (cb_syscall (cb, &s) != CB_RC_OK)
2625 {
2626 abort ();
2627 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2628 SIM_SIGILL);
2629 }
2630 retval = s.result == -1 ? -s.errcode : s.result;
2631
2632 cb->stat_map = saved_map;
2633 cb->syscall_map = saved_syscall_map;
2634 break;
2635 }
2636
2637 case TARGET_SYS_getcwd:
2638 {
2639 USI buf = arg1;
2640 USI size = arg2;
2641
2642 char *cwd = xmalloc (SIM_PATHMAX);
2643 if (cwd != getcwd (cwd, SIM_PATHMAX))
2644 abort ();
2645
2646 /* FIXME: When and if we support chdir, we need something
2647 a bit more elaborate. */
2648 if (simulator_sysroot[0] != '\0')
2649 strcpy (cwd, "/");
2650
2651 retval = -cb_host_to_target_errno (cb, ERANGE);
2652 if (strlen (cwd) + 1 <= size)
2653 {
2654 retval = strlen (cwd) + 1;
2655 if (sim_core_write_buffer (sd, current_cpu, 0, cwd,
2656 buf, retval)
2657 != (unsigned int) retval)
2658 retval = -cb_host_to_target_errno (cb, EFAULT);
2659 }
2660 free (cwd);
2661 break;
2662 }
2663
2664 case TARGET_SYS_access:
2665 {
2666 SI path = arg1;
2667 SI mode = arg2;
2668 char *pbuf = xmalloc (SIM_PATHMAX);
2669 int i;
2670 int o = 0;
2671 int hmode = 0;
2672
2673 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2674 {
2675 strcpy (pbuf, simulator_sysroot);
2676 o += strlen (simulator_sysroot);
2677 }
2678
2679 for (i = 0; i + o < SIM_PATHMAX; i++)
2680 {
2681 pbuf[i + o]
2682 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2683 if (pbuf[i + o] == 0)
2684 break;
2685 }
2686
2687 if (i + o == SIM_PATHMAX)
2688 {
2689 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2690 break;
2691 }
2692
2693 /* Assert that we don't get calls for files for which we
2694 don't have support. */
2695 if (strncmp (pbuf + strlen (simulator_sysroot),
2696 "/proc/", 6) == 0)
2697 abort ();
2698 #define X_AFLAG(x) if (mode & TARGET_ ## x) hmode |= x
2699 X_AFLAG (R_OK);
2700 X_AFLAG (W_OK);
2701 X_AFLAG (X_OK);
2702 X_AFLAG (F_OK);
2703 #undef X_AFLAG
2704
2705 if (access (pbuf, hmode) != 0)
2706 retval = -cb_host_to_target_errno (cb, errno);
2707 else
2708 retval = 0;
2709
2710 free (pbuf);
2711 break;
2712 }
2713
2714 case TARGET_SYS_readlink:
2715 {
2716 SI path = arg1;
2717 SI buf = arg2;
2718 SI bufsiz = arg3;
2719 char *pbuf = xmalloc (SIM_PATHMAX);
2720 char *lbuf = xmalloc (SIM_PATHMAX);
2721 char *lbuf_alloc = lbuf;
2722 int nchars = -1;
2723 int i;
2724 int o = 0;
2725
2726 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2727 {
2728 strcpy (pbuf, simulator_sysroot);
2729 o += strlen (simulator_sysroot);
2730 }
2731
2732 for (i = 0; i + o < SIM_PATHMAX; i++)
2733 {
2734 pbuf[i + o]
2735 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2736 if (pbuf[i + o] == 0)
2737 break;
2738 }
2739
2740 if (i + o == SIM_PATHMAX)
2741 {
2742 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2743 break;
2744 }
2745
2746 /* Intervene calls for certain files expected in the target
2747 proc file system. */
2748 if (strcmp (pbuf + strlen (simulator_sysroot),
2749 "/proc/" XSTRING (TARGET_PID) "/exe") == 0)
2750 {
2751 char *argv0
2752 = (STATE_PROG_ARGV (sd) != NULL
2753 ? *STATE_PROG_ARGV (sd) : NULL);
2754
2755 if (argv0 == NULL || *argv0 == '.')
2756 {
2757 retval
2758 = cris_unknown_syscall (current_cpu, pc,
2759 "Unimplemented readlink syscall "
2760 "(0x%lx: [\"%s\"], 0x%lx)\n",
2761 (unsigned long) arg1, pbuf,
2762 (unsigned long) arg2);
2763 break;
2764 }
2765 else if (*argv0 == '/')
2766 {
2767 if (strncmp (simulator_sysroot, argv0,
2768 strlen (simulator_sysroot)) == 0)
2769 argv0 += strlen (simulator_sysroot);
2770
2771 strcpy (lbuf, argv0);
2772 nchars = strlen (argv0) + 1;
2773 }
2774 else
2775 {
2776 if (getcwd (lbuf, SIM_PATHMAX) != NULL
2777 && strlen (lbuf) + 2 + strlen (argv0) < SIM_PATHMAX)
2778 {
2779 if (strncmp (simulator_sysroot, lbuf,
2780 strlen (simulator_sysroot)) == 0)
2781 lbuf += strlen (simulator_sysroot);
2782
2783 strcat (lbuf, "/");
2784 strcat (lbuf, argv0);
2785 nchars = strlen (lbuf) + 1;
2786 }
2787 else
2788 abort ();
2789 }
2790 }
2791 else
2792 nchars = readlink (pbuf, lbuf, SIM_PATHMAX);
2793
2794 /* We trust that the readlink result returns a *relative*
2795 link, or one already adjusted for the file-path-prefix.
2796 (We can't generally tell the difference, so we go with
2797 the easiest decision; no adjustment.) */
2798
2799 if (nchars == -1)
2800 {
2801 retval = -cb_host_to_target_errno (cb, errno);
2802 break;
2803 }
2804
2805 if (bufsiz < nchars)
2806 nchars = bufsiz;
2807
2808 if (sim_core_write_buffer (sd, current_cpu, write_map, lbuf,
2809 buf, nchars) != (unsigned int) nchars)
2810 retval = -cb_host_to_target_errno (cb, EFAULT);
2811 else
2812 retval = nchars;
2813
2814 free (pbuf);
2815 free (lbuf_alloc);
2816 break;
2817 }
2818
2819 case TARGET_SYS_sched_getscheduler:
2820 {
2821 USI pid = arg1;
2822
2823 /* FIXME: Search (other) existing threads. */
2824 if (pid != 0 && pid != TARGET_PID)
2825 retval = -cb_host_to_target_errno (cb, ESRCH);
2826 else
2827 retval = TARGET_SCHED_OTHER;
2828 break;
2829 }
2830
2831 case TARGET_SYS_sched_getparam:
2832 {
2833 USI pid = arg1;
2834 USI paramp = arg2;
2835
2836 /* The kernel says:
2837 struct sched_param {
2838 int sched_priority;
2839 }; */
2840
2841 if (pid != 0 && pid != TARGET_PID)
2842 retval = -cb_host_to_target_errno (cb, ESRCH);
2843 else
2844 {
2845 /* FIXME: Save scheduler setting before threads are
2846 created too. */
2847 sim_core_write_unaligned_4 (current_cpu, pc, 0, paramp,
2848 current_cpu->thread_data != NULL
2849 ? (current_cpu
2850 ->thread_data[threadno]
2851 .priority)
2852 : 0);
2853 retval = 0;
2854 }
2855 break;
2856 }
2857
2858 case TARGET_SYS_sched_setparam:
2859 {
2860 USI pid = arg1;
2861 USI paramp = arg2;
2862
2863 if ((pid != 0 && pid != TARGET_PID)
2864 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2865 paramp) != 0)
2866 retval = -cb_host_to_target_errno (cb, EINVAL);
2867 else
2868 retval = 0;
2869 break;
2870 }
2871
2872 case TARGET_SYS_sched_setscheduler:
2873 {
2874 USI pid = arg1;
2875 USI policy = arg2;
2876 USI paramp = arg3;
2877
2878 if ((pid != 0 && pid != TARGET_PID)
2879 || policy != TARGET_SCHED_OTHER
2880 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2881 paramp) != 0)
2882 retval = -cb_host_to_target_errno (cb, EINVAL);
2883 else
2884 /* FIXME: Save scheduler setting to be read in later
2885 sched_getparam calls. */
2886 retval = 0;
2887 break;
2888 }
2889
2890 case TARGET_SYS_sched_yield:
2891 /* We reschedule to the next thread after a syscall anyway, so
2892 we don't have to do anything here than to set the return
2893 value. */
2894 retval = 0;
2895 break;
2896
2897 case TARGET_SYS_sched_get_priority_min:
2898 case TARGET_SYS_sched_get_priority_max:
2899 if (arg1 != 0)
2900 retval = -cb_host_to_target_errno (cb, EINVAL);
2901 else
2902 retval = 0;
2903 break;
2904
2905 case TARGET_SYS_ugetrlimit:
2906 {
2907 unsigned int curlim, maxlim;
2908 if (arg1 != TARGET_RLIMIT_STACK && arg1 != TARGET_RLIMIT_NOFILE)
2909 {
2910 retval = -cb_host_to_target_errno (cb, EINVAL);
2911 break;
2912 }
2913
2914 /* The kernel says:
2915 struct rlimit {
2916 unsigned long rlim_cur;
2917 unsigned long rlim_max;
2918 }; */
2919 if (arg1 == TARGET_RLIMIT_NOFILE)
2920 {
2921 /* Sadly a very low limit. Better not lie, though. */
2922 maxlim = curlim = MAX_CALLBACK_FDS;
2923 }
2924 else /* arg1 == TARGET_RLIMIT_STACK */
2925 {
2926 maxlim = 0xffffffff;
2927 curlim = 0x800000;
2928 }
2929 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, curlim);
2930 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, maxlim);
2931 retval = 0;
2932 break;
2933 }
2934
2935 case TARGET_SYS_setrlimit:
2936 if (arg1 != TARGET_RLIMIT_STACK)
2937 {
2938 retval = -cb_host_to_target_errno (cb, EINVAL);
2939 break;
2940 }
2941 /* FIXME: Save values for future ugetrlimit calls. */
2942 retval = 0;
2943 break;
2944
2945 /* Provide a very limited subset of the sysctl functions, and
2946 abort for the rest. */
2947 case TARGET_SYS__sysctl:
2948 {
2949 /* The kernel says:
2950 struct __sysctl_args {
2951 int *name;
2952 int nlen;
2953 void *oldval;
2954 size_t *oldlenp;
2955 void *newval;
2956 size_t newlen;
2957 unsigned long __unused[4];
2958 }; */
2959 SI name = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1);
2960 SI name0 = name == 0
2961 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name);
2962 SI name1 = name == 0
2963 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name + 4);
2964 SI nlen
2965 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 4);
2966 SI oldval
2967 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 8);
2968 SI oldlenp
2969 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 12);
2970 SI oldlen = oldlenp == 0
2971 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, oldlenp);
2972 SI newval
2973 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 16);
2974 SI newlen
2975 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 20);
2976
2977 if (name0 == TARGET_CTL_KERN && name1 == TARGET_CTL_KERN_VERSION)
2978 {
2979 SI to_write = oldlen < (SI) sizeof (TARGET_UTSNAME)
2980 ? oldlen : (SI) sizeof (TARGET_UTSNAME);
2981
2982 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldlenp,
2983 sizeof (TARGET_UTSNAME));
2984
2985 if (sim_core_write_buffer (sd, current_cpu, write_map,
2986 TARGET_UTSNAME, oldval,
2987 to_write)
2988 != (unsigned int) to_write)
2989 retval = -cb_host_to_target_errno (cb, EFAULT);
2990 else
2991 retval = 0;
2992 break;
2993 }
2994
2995 retval
2996 = cris_unknown_syscall (current_cpu, pc,
2997 "Unimplemented _sysctl syscall "
2998 "(0x%lx: [0x%lx, 0x%lx],"
2999 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
3000 (unsigned long) name,
3001 (unsigned long) name0,
3002 (unsigned long) name1,
3003 (unsigned long) nlen,
3004 (unsigned long) oldval,
3005 (unsigned long) oldlenp,
3006 (unsigned long) newval,
3007 (unsigned long) newlen);
3008 break;
3009 }
3010
3011 case TARGET_SYS_exit:
3012 {
3013 /* Here for all but the last thread. */
3014 int i;
3015 int pid
3016 = current_cpu->thread_data[threadno].threadid + TARGET_PID;
3017 int ppid
3018 = (current_cpu->thread_data[threadno].parent_threadid
3019 + TARGET_PID);
3020 int exitsig = current_cpu->thread_data[threadno].exitsig;
3021
3022 /* Any children are now all orphans. */
3023 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
3024 if (current_cpu->thread_data[i].parent_threadid
3025 == current_cpu->thread_data[threadno].threadid)
3026 /* Make getppid(2) return 1 for them, poor little ones. */
3027 current_cpu->thread_data[i].parent_threadid = -TARGET_PID + 1;
3028
3029 /* Free the cpu context data. When the parent has received
3030 the exit status, we'll clear the entry too. */
3031 free (current_cpu->thread_data[threadno].cpu_context);
3032 current_cpu->thread_data[threadno].cpu_context = NULL;
3033 current_cpu->m1threads--;
3034 if (arg1 != 0)
3035 {
3036 sim_io_eprintf (sd, "Thread %d exited with status %d\n",
3037 pid, arg1);
3038 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
3039 SIM_SIGILL);
3040 }
3041
3042 /* Still, we may want to support non-zero exit values. */
3043 current_cpu->thread_data[threadno].exitval = arg1 << 8;
3044
3045 if (exitsig)
3046 deliver_signal (current_cpu, exitsig, ppid);
3047 break;
3048 }
3049
3050 case TARGET_SYS_clone:
3051 {
3052 int nthreads = current_cpu->m1threads + 1;
3053 void *thread_cpu_data;
3054 bfd_byte old_sp_buf[4];
3055 bfd_byte sp_buf[4];
3056 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
3057 int i;
3058
3059 /* That's right, the syscall clone arguments are reversed
3060 compared to sys_clone notes in clone(2) and compared to
3061 other Linux ports (i.e. it's the same order as in the
3062 clone(2) libcall). */
3063 USI flags = arg2;
3064 USI newsp = arg1;
3065
3066 if (nthreads == SIM_TARGET_MAX_THREADS)
3067 {
3068 retval = -cb_host_to_target_errno (cb, EAGAIN);
3069 break;
3070 }
3071
3072 /* FIXME: Implement the low byte. */
3073 if ((flags & ~TARGET_CSIGNAL) !=
3074 (TARGET_CLONE_VM
3075 | TARGET_CLONE_FS
3076 | TARGET_CLONE_FILES
3077 | TARGET_CLONE_SIGHAND)
3078 || newsp == 0)
3079 {
3080 retval
3081 = cris_unknown_syscall (current_cpu, pc,
3082 "Unimplemented clone syscall "
3083 "(0x%lx, 0x%lx)\n",
3084 (unsigned long) arg1,
3085 (unsigned long) arg2);
3086 break;
3087 }
3088
3089 if (current_cpu->thread_data == NULL)
3090 make_first_thread (current_cpu);
3091
3092 /* The created thread will get the new SP and a cleared R10.
3093 Since it's created out of a copy of the old thread and we
3094 don't have a set-register-function that just take the
3095 cpu_data as a parameter, we set the childs values first,
3096 and write back or overwrite them in the parent after the
3097 copy. */
3098 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
3099 H_GR_SP, old_sp_buf, 4);
3100 bfd_putl32 (newsp, sp_buf);
3101 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3102 H_GR_SP, sp_buf, 4);
3103 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3104 H_GR_R10, (bfd_byte *) zeros, 4);
3105 thread_cpu_data
3106 = (*current_cpu
3107 ->make_thread_cpu_data) (current_cpu,
3108 &current_cpu->cpu_data_placeholder);
3109 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3110 H_GR_SP, old_sp_buf, 4);
3111
3112 retval = ++current_cpu->max_threadid + TARGET_PID;
3113
3114 /* Find an unused slot. After a few threads have been created
3115 and exited, the array is expected to be a bit fragmented.
3116 We don't reuse the first entry, though, that of the
3117 original thread. */
3118 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
3119 if (current_cpu->thread_data[i].cpu_context == NULL
3120 /* Don't reuse a zombied entry. */
3121 && current_cpu->thread_data[i].threadid == 0)
3122 break;
3123
3124 memcpy (&current_cpu->thread_data[i],
3125 &current_cpu->thread_data[threadno],
3126 sizeof (current_cpu->thread_data[i]));
3127 current_cpu->thread_data[i].cpu_context = thread_cpu_data;
3128 current_cpu->thread_data[i].cpu_context_atsignal = NULL;
3129 current_cpu->thread_data[i].threadid = current_cpu->max_threadid;
3130 current_cpu->thread_data[i].parent_threadid
3131 = current_cpu->thread_data[threadno].threadid;
3132 current_cpu->thread_data[i].pipe_read_fd = 0;
3133 current_cpu->thread_data[i].pipe_write_fd = 0;
3134 current_cpu->thread_data[i].at_syscall = 0;
3135 current_cpu->thread_data[i].sigpending = 0;
3136 current_cpu->thread_data[i].sigsuspended = 0;
3137 current_cpu->thread_data[i].exitsig = flags & TARGET_CSIGNAL;
3138 current_cpu->m1threads = nthreads;
3139 break;
3140 }
3141
3142 /* Better watch these in case they do something necessary. */
3143 case TARGET_SYS_socketcall:
3144 retval = -cb_host_to_target_errno (cb, ENOSYS);
3145 break;
3146
3147 unimplemented_syscall:
3148 default:
3149 retval
3150 = cris_unknown_syscall (current_cpu, pc,
3151 "Unimplemented syscall: %d "
3152 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
3153 callnum, arg1, arg2, arg3, arg4, arg5,
3154 arg6);
3155 }
3156 }
3157
3158 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
3159 if (callnum == TARGET_SYS_open)
3160 {
3161 current_cpu->last_open_fd = retval;
3162 current_cpu->last_open_flags = arg2;
3163 }
3164
3165 current_cpu->last_syscall = callnum;
3166
3167 /* A system call is a rescheduling point. For the time being, we don't
3168 reschedule anywhere else. */
3169 if (current_cpu->m1threads != 0
3170 /* We need to schedule off from an exiting thread that is the
3171 second-last one. */
3172 || (current_cpu->thread_data != NULL
3173 && current_cpu->thread_data[threadno].cpu_context == NULL))
3174 {
3175 bfd_byte retval_buf[4];
3176
3177 current_cpu->thread_data[threadno].last_execution
3178 = TARGET_TIME_MS (current_cpu);
3179 bfd_putl32 (retval, retval_buf);
3180 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
3181
3182 current_cpu->thread_data[threadno].at_syscall = 1;
3183 reschedule (current_cpu);
3184
3185 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
3186 retval = bfd_getl32 (retval_buf);
3187 }
3188
3189 return retval;
3190 }
3191
3192 /* Callback from simulator write saying that the pipe at (reader, writer)
3193 is now non-empty (so the writer should wait until the pipe is empty, at
3194 least not write to this or any other pipe). Simplest is to just wait
3195 until the pipe is empty. */
3196
3197 static void
3198 cris_pipe_nonempty (host_callback *cb ATTRIBUTE_UNUSED,
3199 int reader, int writer)
3200 {
3201 SIM_CPU *cpu = current_cpu_for_cb_callback;
3202 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
3203
3204 /* It's the current thread: we just have to re-run the current
3205 syscall instruction (presumably "break 13") and change the syscall
3206 to the special simulator-wait code. Oh, and set a marker that
3207 we're waiting, so we can disambiguate the special call from a
3208 program error.
3209
3210 This function may be called multiple times between cris_pipe_empty,
3211 but we must avoid e.g. decreasing PC every time. Check fd markers
3212 to tell. */
3213 if (cpu->thread_data == NULL)
3214 {
3215 sim_io_eprintf (CPU_STATE (cpu),
3216 "Terminating simulation due to writing pipe rd:wr %d:%d"
3217 " from one single thread\n", reader, writer);
3218 sim_engine_halt (CPU_STATE (cpu), cpu,
3219 NULL, sim_pc_get (cpu), sim_stopped, SIM_SIGILL);
3220 }
3221 else if (cpu->thread_data[cpu->threadno].pipe_write_fd == 0)
3222 {
3223 cpu->thread_data[cpu->threadno].pipe_write_fd = writer;
3224 cpu->thread_data[cpu->threadno].pipe_read_fd = reader;
3225 /* FIXME: We really shouldn't change registers other than R10 in
3226 syscalls (like R9), here or elsewhere. */
3227 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R9, (bfd_byte *) zeros, 4);
3228 sim_pc_set (cpu, sim_pc_get (cpu) - 2);
3229 }
3230 }
3231
3232 /* Callback from simulator close or read call saying that the pipe at
3233 (reader, writer) is now empty (so the writer can write again, perhaps
3234 leave a waiting state). If there are bytes remaining, they couldn't be
3235 consumed (perhaps due to the pipe closing). */
3236
3237 static void
3238 cris_pipe_empty (host_callback *cb,
3239 int reader,
3240 int writer)
3241 {
3242 int i;
3243 SIM_CPU *cpu = current_cpu_for_cb_callback;
3244 bfd_byte r10_buf[4];
3245 int remaining
3246 = cb->pipe_buffer[writer].size - cb->pipe_buffer[reader].size;
3247
3248 /* We need to find the thread that waits for this pipe. */
3249 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
3250 if (cpu->thread_data[i].cpu_context
3251 && cpu->thread_data[i].pipe_write_fd == writer)
3252 {
3253 int retval;
3254
3255 /* Temporarily switch to this cpu context, so we can change the
3256 PC by ordinary calls. */
3257
3258 memcpy (cpu->thread_data[cpu->threadno].cpu_context,
3259 &cpu->cpu_data_placeholder,
3260 cpu->thread_cpu_data_size);
3261 memcpy (&cpu->cpu_data_placeholder,
3262 cpu->thread_data[i].cpu_context,
3263 cpu->thread_cpu_data_size);
3264
3265 /* The return value is supposed to contain the number of
3266 written bytes, which is the number of bytes requested and
3267 returned at the write call. You might think the right
3268 thing is to adjust the return-value to be only the
3269 *consumed* number of bytes, but it isn't. We're only
3270 called if the pipe buffer is fully consumed or it is being
3271 closed, possibly with remaining bytes. For the latter
3272 case, the writer is still supposed to see success for
3273 PIPE_BUF bytes (a constant which we happen to know and is
3274 unlikely to change). The return value may also be a
3275 negative number; an error value. This case is covered
3276 because "remaining" is always >= 0. */
3277 (*CPU_REG_FETCH (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3278 retval = (int) bfd_getl_signed_32 (r10_buf);
3279 if (retval - remaining > TARGET_PIPE_BUF)
3280 {
3281 bfd_putl32 (retval - remaining, r10_buf);
3282 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3283 }
3284 sim_pc_set (cpu, sim_pc_get (cpu) + 2);
3285 memcpy (cpu->thread_data[i].cpu_context,
3286 &cpu->cpu_data_placeholder,
3287 cpu->thread_cpu_data_size);
3288 memcpy (&cpu->cpu_data_placeholder,
3289 cpu->thread_data[cpu->threadno].cpu_context,
3290 cpu->thread_cpu_data_size);
3291 cpu->thread_data[i].pipe_read_fd = 0;
3292 cpu->thread_data[i].pipe_write_fd = 0;
3293 return;
3294 }
3295
3296 abort ();
3297 }
3298
3299 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3300
3301 static long
3302 cris_time (host_callback *cb ATTRIBUTE_UNUSED, long *t)
3303 {
3304 long retval = TARGET_TIME (current_cpu_for_cb_callback);
3305 if (t)
3306 *t = retval;
3307 return retval;
3308 }
3309
3310 /* Set target-specific callback data. */
3311
3312 void
3313 cris_set_callbacks (host_callback *cb)
3314 {
3315 /* Yeargh, have to cast away constness to avoid warnings. */
3316 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
3317 cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
3318
3319 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3320 parameter to cb_store_target_endian will make st_size negative.
3321 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3322 *unsigned*, and/or add syntax for signed-ness. */
3323 cb->stat_map = stat_map;
3324 cb->open_map = (CB_TARGET_DEFS_MAP *) open_map;
3325 cb->pipe_nonempty = cris_pipe_nonempty;
3326 cb->pipe_empty = cris_pipe_empty;
3327 cb->time = cris_time;
3328 }
3329
3330 /* Process an address exception. */
3331
3332 void
3333 cris_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
3334 unsigned int map, int nr_bytes, address_word addr,
3335 transfer_type transfer, sim_core_signals sig)
3336 {
3337 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,
3338 transfer, sig);
3339 }