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