1 /* CRIS exception, interrupt, and trap (EIT) support
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Axis Communications.
5 This file is part of the GNU simulators.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sim-options.h"
24 /* FIXME: get rid of targ-vals.h usage everywhere else. */
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
39 #ifdef HAVE_SYS_STAT_H
42 /* For PATH_MAX, originally. */
47 /* From ld/sysdep.h. */
49 # define SIM_PATHMAX PATH_MAX
52 # define SIM_PATHMAX MAXPATHLEN
54 # define SIM_PATHMAX 1024
58 /* The verbatim values are from asm-cris/unistd.h. */
60 #define TARGET_SYS_exit 1
61 #define TARGET_SYS_read 3
62 #define TARGET_SYS_write 4
63 #define TARGET_SYS_open 5
64 #define TARGET_SYS_close 6
65 #define TARGET_SYS_unlink 10
66 #define TARGET_SYS_time 13
67 #define TARGET_SYS_lseek 19
68 #define TARGET_SYS_getpid 20
69 #define TARGET_SYS_kill 37
70 #define TARGET_SYS_rename 38
71 #define TARGET_SYS_pipe 42
72 #define TARGET_SYS_brk 45
73 #define TARGET_SYS_ioctl 54
74 #define TARGET_SYS_fcntl 55
75 #define TARGET_SYS_getppid 64
76 #define TARGET_SYS_setrlimit 75
77 #define TARGET_SYS_gettimeofday 78
78 #define TARGET_SYS_readlink 85
79 #define TARGET_SYS_munmap 91
80 #define TARGET_SYS_truncate 92
81 #define TARGET_SYS_ftruncate 93
82 #define TARGET_SYS_socketcall 102
83 #define TARGET_SYS_stat 106
84 #define TARGET_SYS_fstat 108
85 #define TARGET_SYS_wait4 114
86 #define TARGET_SYS_sigreturn 119
87 #define TARGET_SYS_clone 120
88 #define TARGET_SYS_uname 122
89 #define TARGET_SYS_mprotect 125
90 #define TARGET_SYS_llseek 140
91 #define TARGET_SYS__sysctl 149
92 #define TARGET_SYS_sched_setparam 154
93 #define TARGET_SYS_sched_getparam 155
94 #define TARGET_SYS_sched_setscheduler 156
95 #define TARGET_SYS_sched_getscheduler 157
96 #define TARGET_SYS_sched_yield 158
97 #define TARGET_SYS_sched_get_priority_max 159
98 #define TARGET_SYS_sched_get_priority_min 160
99 #define TARGET_SYS_mremap 163
100 #define TARGET_SYS_poll 168
101 #define TARGET_SYS_rt_sigaction 174
102 #define TARGET_SYS_rt_sigprocmask 175
103 #define TARGET_SYS_rt_sigsuspend 179
104 #define TARGET_SYS_getcwd 183
105 #define TARGET_SYS_ugetrlimit 191
106 #define TARGET_SYS_mmap2 192
107 #define TARGET_SYS_stat64 195
108 #define TARGET_SYS_lstat64 196
109 #define TARGET_SYS_fstat64 197
110 #define TARGET_SYS_geteuid32 201
111 #define TARGET_SYS_getuid32 199
112 #define TARGET_SYS_getegid32 202
113 #define TARGET_SYS_getgid32 200
114 #define TARGET_SYS_fcntl64 221
116 #define TARGET_PROT_READ 0x1
117 #define TARGET_PROT_WRITE 0x2
118 #define TARGET_PROT_EXEC 0x4
119 #define TARGET_PROT_NONE 0x0
121 #define TARGET_MAP_SHARED 0x01
122 #define TARGET_MAP_PRIVATE 0x02
123 #define TARGET_MAP_TYPE 0x0f
124 #define TARGET_MAP_FIXED 0x10
125 #define TARGET_MAP_ANONYMOUS 0x20
127 #define TARGET_CTL_KERN 1
128 #define TARGET_CTL_VM 2
129 #define TARGET_CTL_NET 3
130 #define TARGET_CTL_PROC 4
131 #define TARGET_CTL_FS 5
132 #define TARGET_CTL_DEBUG 6
133 #define TARGET_CTL_DEV 7
134 #define TARGET_CTL_BUS 8
135 #define TARGET_CTL_ABI 9
137 #define TARGET_CTL_KERN_VERSION 4
140 #define TARGET_MREMAP_MAYMOVE 1
141 #define TARGET_MREMAP_FIXED 2
143 #define TARGET_TCGETS 0x5401
145 #define TARGET_UTSNAME "#38 Sun Apr 1 00:00:00 MET 2001"
147 /* Seconds since the above date + 10 minutes. */
148 #define TARGET_EPOCH 986080200
150 /* Milliseconds since start of run. We use the number of syscalls to
151 avoid introducing noise in the execution time. */
152 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
154 /* Seconds as in time(2). */
155 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
157 #define TARGET_SCHED_OTHER 0
159 #define TARGET_RLIMIT_STACK 3
160 #define TARGET_RLIMIT_NOFILE 7
162 #define SIM_TARGET_MAX_THREADS 64
163 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
165 /* From linux/sched.h. */
166 #define TARGET_CSIGNAL 0x000000ff
167 #define TARGET_CLONE_VM 0x00000100
168 #define TARGET_CLONE_FS 0x00000200
169 #define TARGET_CLONE_FILES 0x00000400
170 #define TARGET_CLONE_SIGHAND 0x00000800
171 #define TARGET_CLONE_PID 0x00001000
172 #define TARGET_CLONE_PTRACE 0x00002000
173 #define TARGET_CLONE_VFORK 0x00004000
174 #define TARGET_CLONE_PARENT 0x00008000
175 #define TARGET_CLONE_THREAD 0x00010000
176 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
178 /* From asm-cris/poll.h. */
179 #define TARGET_POLLIN 1
181 /* From asm-cris/signal.h. */
182 #define TARGET_SIG_BLOCK 0
183 #define TARGET_SIG_UNBLOCK 1
184 #define TARGET_SIG_SETMASK 2
186 #define TARGET_SIG_DFL 0
187 #define TARGET_SIG_IGN 1
188 #define TARGET_SIG_ERR ((USI)-1)
190 #define TARGET_SIGHUP 1
191 #define TARGET_SIGINT 2
192 #define TARGET_SIGQUIT 3
193 #define TARGET_SIGILL 4
194 #define TARGET_SIGTRAP 5
195 #define TARGET_SIGABRT 6
196 #define TARGET_SIGIOT 6
197 #define TARGET_SIGBUS 7
198 #define TARGET_SIGFPE 8
199 #define TARGET_SIGKILL 9
200 #define TARGET_SIGUSR1 10
201 #define TARGET_SIGSEGV 11
202 #define TARGET_SIGUSR2 12
203 #define TARGET_SIGPIPE 13
204 #define TARGET_SIGALRM 14
205 #define TARGET_SIGTERM 15
206 #define TARGET_SIGSTKFLT 16
207 #define TARGET_SIGCHLD 17
208 #define TARGET_SIGCONT 18
209 #define TARGET_SIGSTOP 19
210 #define TARGET_SIGTSTP 20
211 #define TARGET_SIGTTIN 21
212 #define TARGET_SIGTTOU 22
213 #define TARGET_SIGURG 23
214 #define TARGET_SIGXCPU 24
215 #define TARGET_SIGXFSZ 25
216 #define TARGET_SIGVTALRM 26
217 #define TARGET_SIGPROF 27
218 #define TARGET_SIGWINCH 28
219 #define TARGET_SIGIO 29
220 #define TARGET_SIGPOLL SIGIO
221 /* Actually commented out in the kernel header. */
222 #define TARGET_SIGLOST 29
223 #define TARGET_SIGPWR 30
224 #define TARGET_SIGSYS 31
226 /* From include/asm-cris/signal.h. */
227 #define TARGET_SA_NOCLDSTOP 0x00000001
228 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
229 #define TARGET_SA_SIGINFO 0x00000004
230 #define TARGET_SA_ONSTACK 0x08000000
231 #define TARGET_SA_RESTART 0x10000000
232 #define TARGET_SA_NODEFER 0x40000000
233 #define TARGET_SA_RESETHAND 0x80000000
234 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
235 #define TARGET_SA_RESTORER 0x04000000
237 /* From linux/wait.h. */
238 #define TARGET_WNOHANG 1
239 #define TARGET_WUNTRACED 2
240 #define TARGET___WNOTHREAD 0x20000000
241 #define TARGET___WALL 0x40000000
242 #define TARGET___WCLONE 0x80000000
244 /* From linux/limits.h. */
245 #define TARGET_PIPE_BUF 4096
247 static const char stat_map[] =
248 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
249 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
250 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
253 static const CB_TARGET_DEFS_MAP syscall_map[] =
255 { CB_SYS_open, TARGET_SYS_open },
256 { CB_SYS_close, TARGET_SYS_close },
257 { CB_SYS_read, TARGET_SYS_read },
258 { CB_SYS_write, TARGET_SYS_write },
259 { CB_SYS_lseek, TARGET_SYS_lseek },
260 { CB_SYS_unlink, TARGET_SYS_unlink },
261 { CB_SYS_getpid, TARGET_SYS_getpid },
262 { CB_SYS_fstat, TARGET_SYS_fstat64 },
263 { CB_SYS_lstat, TARGET_SYS_lstat64 },
264 { CB_SYS_stat, TARGET_SYS_stat64 },
265 { CB_SYS_pipe, TARGET_SYS_pipe },
266 { CB_SYS_rename, TARGET_SYS_rename },
267 { CB_SYS_truncate, TARGET_SYS_truncate },
268 { CB_SYS_ftruncate, TARGET_SYS_ftruncate },
272 /* An older, 32-bit-only stat mapping. */
273 static const char stat32_map[] =
274 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
275 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
276 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
278 /* Map for calls using the 32-bit struct stat. Primarily used by the
279 newlib Linux mapping. */
280 static const CB_TARGET_DEFS_MAP syscall_stat32_map[] =
282 { CB_SYS_fstat, TARGET_SYS_fstat },
283 { CB_SYS_stat, TARGET_SYS_stat },
287 /* Giving the true value for the running sim process will lead to
288 non-time-invariant behavior. */
289 #define TARGET_PID 42
291 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
292 we did, we'd still don't get a register number with the "16" offset. */
293 #define TARGET_SRP_REGNUM (16+11)
295 /* Extracted by applying
296 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
297 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
298 adjusting the synonyms. */
300 static const CB_TARGET_DEFS_MAP errno_map[] =
408 { ENAMETOOLONG, 36 },
567 { EDESTADDRREQ, 89 },
578 #ifdef EPROTONOSUPPORT
579 { EPROTONOSUPPORT, 93 },
581 #ifdef ESOCKTNOSUPPORT
582 { ESOCKTNOSUPPORT, 94 },
588 { EPFNOSUPPORT, 96 },
591 { EAFNOSUPPORT, 97 },
597 { EADDRNOTAVAIL, 99 },
603 { ENETUNREACH, 101 },
609 { ECONNABORTED, 103 },
627 { ETOOMANYREFS, 109 },
633 { ECONNREFUSED, 111 },
639 { EHOSTUNREACH, 113 },
645 { EINPROGRESS, 115 },
672 { EMEDIUMTYPE, 124 },
677 /* Extracted by applying
678 perl -ne 'if ($_ =~ /^#define/) { split;
679 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
680 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
681 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
682 installation and removing synonyms and unnecessary items. Don't
683 forget the end-marker. */
685 /* These we treat specially, as they're used in the fcntl F_GETFL
686 syscall. For consistency, open_map is also manually edited to use
688 #define TARGET_O_ACCMODE 0x3
689 #define TARGET_O_RDONLY 0x0
690 #define TARGET_O_WRONLY 0x1
692 static const CB_TARGET_DEFS_MAP open_map[] = {
694 { O_ACCMODE, TARGET_O_ACCMODE },
697 { O_RDONLY, TARGET_O_RDONLY },
700 { O_WRONLY, TARGET_O_WRONLY },
721 { O_NONBLOCK, 0x800 },
733 { O_DIRECT, 0x4000 },
736 { O_LARGEFILE, 0x8000 },
739 { O_DIRECTORY, 0x10000 },
742 { O_NOFOLLOW, 0x20000 },
747 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
748 static SIM_CPU *current_cpu_for_cb_callback;
750 static int syscall_read_mem (host_callback *, struct cb_syscall *,
751 unsigned long, char *, int);
752 static int syscall_write_mem (host_callback *, struct cb_syscall *,
753 unsigned long, const char *, int);
754 static USI create_map (SIM_DESC, struct cris_sim_mmapped_page **,
756 static USI unmap_pages (SIM_DESC, struct cris_sim_mmapped_page **,
758 static USI is_mapped (SIM_DESC, struct cris_sim_mmapped_page **,
760 static void dump_statistics (SIM_CPU *current_cpu);
761 static void make_first_thread (SIM_CPU *current_cpu);
763 /* Read/write functions for system call interface. */
766 syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED,
767 struct cb_syscall *sc,
768 unsigned long taddr, char *buf, int bytes)
770 SIM_DESC sd = (SIM_DESC) sc->p1;
771 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
773 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
777 syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED,
778 struct cb_syscall *sc,
779 unsigned long taddr, const char *buf, int bytes)
781 SIM_DESC sd = (SIM_DESC) sc->p1;
782 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
784 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
787 /* When we risk running self-modified code (as in trampolines), this is
788 called from special-case insns. The silicon CRIS CPU:s have enough
789 cache snooping implemented making this a simulator-only issue. Tests:
790 gcc.c-torture/execute/931002-1.c execution, -O3 -g
791 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
794 cris_flush_simulator_decode_cache (SIM_CPU *current_cpu,
795 USI pc ATTRIBUTE_UNUSED)
797 SIM_DESC sd = CPU_STATE (current_cpu);
800 if (USING_SCACHE_P (sd))
801 scache_flush_cpu (current_cpu);
805 /* Output statistics at the end of a run. */
807 dump_statistics (SIM_CPU *current_cpu)
809 SIM_DESC sd = CPU_STATE (current_cpu);
810 CRIS_MISC_PROFILE *profp
811 = CPU_CRIS_MISC_PROFILE (current_cpu);
812 unsigned64 total = profp->basic_cycle_count;
813 const char *textmsg = "Basic clock cycles, total @: %llu\n";
815 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
816 what's included in the "total" count only. */
817 switch (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
818 & FLAG_CRIS_MISC_PROFILE_ALL)
820 case FLAG_CRIS_MISC_PROFILE_SIMPLE:
823 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED | FLAG_CRIS_MISC_PROFILE_SIMPLE):
825 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
826 total += profp->unaligned_mem_dword_count;
829 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | FLAG_CRIS_MISC_PROFILE_SIMPLE):
830 textmsg = "Schedulable clock cycles, total @: %llu\n";
832 += (profp->memsrc_stall_count
833 + profp->memraw_stall_count
834 + profp->movemsrc_stall_count
835 + profp->movemdst_stall_count
836 + profp->mulsrc_stall_count
837 + profp->jumpsrc_stall_count
838 + profp->unaligned_mem_dword_count);
841 case FLAG_CRIS_MISC_PROFILE_ALL:
842 textmsg = "All accounted clock cycles, total @: %llu\n";
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);
860 "Internal inconsistency at %s:%d",
862 sim_engine_halt (sd, current_cpu, NULL, 0,
863 sim_stopped, SIM_SIGILL);
866 /* Historically, these messages have gone to stderr, so we'll keep it
867 that way. It's also easier to then tell it from normal program
868 output. FIXME: Add redirect option like "run -e file". */
869 sim_io_eprintf (sd, textmsg, total);
871 /* For v32, unaligned_mem_dword_count should always be 0. For
872 v10, memsrc_stall_count should always be 0. */
873 sim_io_eprintf (sd, "Memory source stall cycles: %lld\n",
874 profp->memsrc_stall_count
875 + profp->unaligned_mem_dword_count);
876 sim_io_eprintf (sd, "Memory read-after-write stall cycles: %lld\n",
877 profp->memraw_stall_count);
878 sim_io_eprintf (sd, "Movem source stall cycles: %lld\n",
879 profp->movemsrc_stall_count);
880 sim_io_eprintf (sd, "Movem destination stall cycles: %lld\n",
881 profp->movemdst_stall_count);
882 sim_io_eprintf (sd, "Movem address stall cycles: %lld\n",
883 profp->movemaddr_stall_count);
884 sim_io_eprintf (sd, "Multiplication source stall cycles: %lld\n",
885 profp->mulsrc_stall_count);
886 sim_io_eprintf (sd, "Jump source stall cycles: %lld\n",
887 profp->jumpsrc_stall_count);
888 sim_io_eprintf (sd, "Branch misprediction stall cycles: %lld\n",
889 profp->branch_stall_count);
890 sim_io_eprintf (sd, "Jump target stall cycles: %lld\n",
891 profp->jumptarget_stall_count);
894 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
895 Return 1 if a overlap detected, 0 otherwise. */
898 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED,
899 struct cris_sim_mmapped_page **rootp,
902 struct cris_sim_mmapped_page *mapp;
904 if (len == 0 || (len & 8191))
907 /* Iterate over the reverse-address sorted pages until we find a page in
908 or lower than the checked area. */
909 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
910 if (mapp->addr < addr + len && mapp->addr >= addr)
916 /* Create mmapped memory. */
919 create_map (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
922 struct cris_sim_mmapped_page *mapp;
923 struct cris_sim_mmapped_page **higher_prevp = rootp;
924 USI new_addr = 0x40000000;
929 new_addr = rootp[0]->addr + 8192;
936 /* Which is better: return an error for this, or just round it up? */
939 /* Do a recursive call for each page in the request. */
940 for (page_addr = new_addr; len != 0; page_addr += 8192, len -= 8192)
941 if (create_map (sd, rootp, page_addr, 8192) >= (USI) -8191)
948 mapp != NULL && mapp->addr > new_addr;
950 higher_prevp = &mapp->prev;
952 /* Allocate the new page, on the next higher page from the last one
953 allocated, and link in the new descriptor before previous ones. */
954 mapp = malloc (sizeof (*mapp));
957 return (USI) -ENOMEM;
959 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
963 mapp->addr = new_addr;
964 mapp->prev = *higher_prevp;
965 *higher_prevp = mapp;
970 /* Unmap one or more pages. */
973 unmap_pages (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
976 struct cris_sim_mmapped_page *mapp;
977 struct cris_sim_mmapped_page **higher_prevp = rootp;
984 /* Which is better: return an error for this, or just round it up? */
987 /* Loop backwards to make each call is O(1) over the number of pages
988 allocated, if we're unmapping from the high end of the pages. */
989 for (page_addr = addr + len - 8192;
992 if (unmap_pages (sd, rootp, page_addr, 8192) != 0)
998 for (mapp = *rootp; mapp != NULL && mapp->addr > addr; mapp = mapp->prev)
999 higher_prevp = &mapp->prev;
1001 if (mapp == NULL || mapp->addr != addr)
1004 *higher_prevp = mapp->prev;
1005 sim_core_detach (sd, NULL, 0, 0, addr);
1010 /* The semantic code invokes this for illegal (unrecognized) instructions. */
1013 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
1015 SIM_DESC sd = CPU_STATE (current_cpu);
1017 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
1021 /* Handlers from the CGEN description that should not be called. */
1024 cris_bmod_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1025 UINT srcreg ATTRIBUTE_UNUSED,
1026 USI dstreg ATTRIBUTE_UNUSED)
1032 h_supr_set_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1033 UINT index ATTRIBUTE_UNUSED,
1034 USI page ATTRIBUTE_UNUSED,
1035 USI newval ATTRIBUTE_UNUSED)
1041 h_supr_get_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1042 UINT index ATTRIBUTE_UNUSED,
1043 USI page ATTRIBUTE_UNUSED)
1048 /* Swap one context for another. */
1051 schedule (SIM_CPU *current_cpu, int next)
1053 /* Need to mark context-switches in the trace output. */
1054 if ((CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1055 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE))
1056 cris_trace_printf (CPU_STATE (current_cpu), current_cpu,
1059 /* Copy the current context (if there is one) to its slot. */
1060 if (current_cpu->thread_data[current_cpu->threadno].cpu_context)
1061 memcpy (current_cpu->thread_data[current_cpu->threadno].cpu_context,
1062 ¤t_cpu->cpu_data_placeholder,
1063 current_cpu->thread_cpu_data_size);
1065 /* Copy the new context from its slot. */
1066 memcpy (¤t_cpu->cpu_data_placeholder,
1067 current_cpu->thread_data[next].cpu_context,
1068 current_cpu->thread_cpu_data_size);
1070 /* Update needed stuff to indicate the new context. */
1071 current_cpu->threadno = next;
1073 /* Handle pending signals. */
1074 if (current_cpu->thread_data[next].sigpending
1075 /* We don't run nested signal handlers. This means that pause(2)
1076 and sigsuspend(2) do not work in sighandlers, but that
1077 shouldn't be too hard a restriction. It also greatly
1078 simplifies the code. */
1079 && current_cpu->thread_data[next].cpu_context_atsignal == NULL)
1083 /* See if there's really a pending, non-blocked handler. We don't
1084 queue signals, so just use the first one in ascending order. */
1085 for (sig = 0; sig < 64; sig++)
1086 if (current_cpu->thread_data[next].sigdata[sig].pending
1087 && !current_cpu->thread_data[next].sigdata[sig].blocked)
1093 USI pc = sim_pc_get (current_cpu);
1095 /* It's simpler to save the CPU context inside the simulator
1096 than on the stack. */
1097 current_cpu->thread_data[next].cpu_context_atsignal
1099 ->make_thread_cpu_data) (current_cpu,
1100 current_cpu->thread_data[next]
1103 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1104 sp = bfd_getl32 (regbuf);
1106 /* Make sure we have an aligned stack. */
1109 /* Make room for the signal frame, aligned. FIXME: Check that
1110 the memory exists, map it in if absent. (BTW, should also
1111 implement on-access automatic stack allocation). */
1114 /* This isn't the same signal frame as the kernel uses, because
1115 we don't want to bother getting all registers on and off the
1118 /* First, we store the currently blocked signals. */
1120 for (i = 0; i < 32; i++)
1122 |= current_cpu->thread_data[next].sigdata[i + 1].blocked << i;
1123 sim_core_write_aligned_4 (current_cpu, pc, 0, sp, blocked);
1125 for (i = 0; i < 31; i++)
1127 |= current_cpu->thread_data[next].sigdata[i + 33].blocked << i;
1128 sim_core_write_aligned_4 (current_cpu, pc, 0, sp + 4, blocked);
1130 /* Then, the actual instructions. This is CPU-specific, but we
1131 use instructions from the common subset for v10 and v32 which
1132 should be safe for the time being but could be parametrized
1134 /* MOVU.W [PC+],R9. */
1135 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 8, 0x9c5f);
1136 /* .WORD TARGET_SYS_sigreturn. */
1137 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 10,
1138 TARGET_SYS_sigreturn);
1140 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 12, 0xe93d);
1142 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1143 instruction. Still, it doesn't matter because v10 has no
1144 delay slot for BREAK so it will not be executed). */
1145 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 16, 0x05b0);
1147 /* Modify registers to hold the right values for the sighandler
1148 context: updated stackpointer and return address pointing to
1149 the sigreturn stub. */
1150 bfd_putl32 (sp, regbuf);
1151 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1152 bfd_putl32 (sp + 8, regbuf);
1153 (*CPU_REG_STORE (current_cpu)) (current_cpu, TARGET_SRP_REGNUM,
1156 current_cpu->thread_data[next].sigdata[sig].pending = 0;
1158 /* Block this signal (for the duration of the sighandler). */
1159 current_cpu->thread_data[next].sigdata[sig].blocked = 1;
1161 sim_pc_set (current_cpu, current_cpu->sighandler[sig]);
1162 bfd_putl32 (sig, regbuf);
1163 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10,
1166 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1167 needed all this for, specifies a SA_SIGINFO call but treats it
1168 like an ordinary sighandler; only the signal number argument is
1169 inspected. To make future need to implement SA_SIGINFO
1170 correctly possible, we set the siginfo argument register to a
1171 magic (hopefully non-address) number. (NB: then, you should
1172 just need to pass the siginfo argument; it seems you probably
1173 don't need to implement the specific rt_sigreturn.) */
1174 bfd_putl32 (0xbad5161f, regbuf);
1175 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R11,
1178 /* The third argument is unused and the kernel sets it to 0. */
1179 bfd_putl32 (0, regbuf);
1180 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R12,
1185 /* No, there actually was no pending signal for this thread. Reset
1187 current_cpu->thread_data[next].sigpending = 0;
1191 /* Reschedule the simplest possible way until something else is absolutely
1193 - A. Find the next process (round-robin) that doesn't have at_syscall
1195 - B. If there is none, just run the next process, round-robin.
1196 - Clear at_syscall for the current process. */
1199 reschedule (SIM_CPU *current_cpu)
1203 /* Iterate over all thread slots, because after a few thread creations
1204 and exits, we don't know where the live ones are. */
1205 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1206 i != current_cpu->threadno;
1207 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1208 if (current_cpu->thread_data[i].cpu_context
1209 && current_cpu->thread_data[i].at_syscall == 0)
1211 schedule (current_cpu, i);
1215 /* Pick any next live thread. */
1216 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1217 i != current_cpu->threadno;
1218 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1219 if (current_cpu->thread_data[i].cpu_context)
1221 schedule (current_cpu, i);
1225 /* More than one live thread, but we couldn't find the next one? */
1229 /* Set up everything to receive (or IGN) an incoming signal to the
1233 deliver_signal (SIM_CPU *current_cpu, int sig, unsigned int pid)
1236 USI pc = sim_pc_get (current_cpu);
1238 /* Find the thread index of the pid. */
1239 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1240 /* Apparently it's ok to send signals to zombies (so a check for
1241 current_cpu->thread_data[i].cpu_context != NULL would be
1243 if (current_cpu->thread_data[i].threadid == pid - TARGET_PID)
1246 switch (current_cpu->sighandler[sig])
1248 case TARGET_SIG_DFL:
1251 /* The following according to the glibc
1252 documentation. (The kernel code has non-obvious
1253 execution paths.) */
1256 case TARGET_SIGSEGV:
1258 case TARGET_SIGABRT:
1259 case TARGET_SIGTRAP:
1262 case TARGET_SIGTERM:
1264 case TARGET_SIGQUIT:
1265 case TARGET_SIGKILL:
1268 case TARGET_SIGALRM:
1269 case TARGET_SIGVTALRM:
1270 case TARGET_SIGPROF:
1271 case TARGET_SIGSTOP:
1273 case TARGET_SIGPIPE:
1274 case TARGET_SIGLOST:
1275 case TARGET_SIGXCPU:
1276 case TARGET_SIGXFSZ:
1277 case TARGET_SIGUSR1:
1278 case TARGET_SIGUSR2:
1279 sim_io_eprintf (CPU_STATE (current_cpu),
1280 "Exiting pid %d due to signal %d\n",
1282 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1283 NULL, pc, sim_stopped,
1284 sig == TARGET_SIGABRT
1285 ? SIM_SIGABRT : SIM_SIGILL);
1288 /* The default for all other signals is to be ignored. */
1293 case TARGET_SIG_IGN:
1296 case TARGET_SIGKILL:
1297 case TARGET_SIGSTOP:
1298 /* Can't ignore these signals. */
1299 sim_io_eprintf (CPU_STATE (current_cpu),
1300 "Exiting pid %d due to signal %d\n",
1302 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1303 NULL, pc, sim_stopped, SIM_SIGILL);
1312 /* Mark the signal as pending, making schedule () check
1313 closer. The signal will be handled when the thread is
1314 scheduled and the signal is unblocked. */
1315 current_cpu->thread_data[i].sigdata[sig].pending = 1;
1316 current_cpu->thread_data[i].sigpending = 1;
1321 sim_io_eprintf (CPU_STATE (current_cpu),
1322 "Unimplemented signal: %d\n", sig);
1323 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
1324 sim_stopped, SIM_SIGILL);
1329 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu)),
1333 /* Make the vector and the first item, the main thread. */
1336 make_first_thread (SIM_CPU *current_cpu)
1338 current_cpu->thread_data
1340 SIM_TARGET_MAX_THREADS
1341 * sizeof (current_cpu->thread_data[0]));
1342 current_cpu->thread_data[0].cpu_context
1343 = (*current_cpu->make_thread_cpu_data) (current_cpu,
1345 ->cpu_data_placeholder);
1346 current_cpu->thread_data[0].parent_threadid = -1;
1348 /* For good measure. */
1349 if (TARGET_SIG_DFL != 0)
1353 /* Handle unknown system calls. Returns (if it does) the syscall
1357 cris_unknown_syscall (SIM_CPU *current_cpu, USI pc, char *s, ...)
1359 SIM_DESC sd = CPU_STATE (current_cpu);
1360 host_callback *cb = STATE_CALLBACK (sd);
1362 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP
1363 || cris_unknown_syscall_action == CRIS_USYSC_MSG_ENOSYS)
1368 sim_io_evprintf (sd, s, ap);
1371 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP)
1372 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1375 return -cb_host_to_target_errno (cb, ENOSYS);
1378 /* Main function: the handler of the "break 13" syscall insn. */
1381 cris_break_13_handler (SIM_CPU *current_cpu, USI callnum, USI arg1,
1382 USI arg2, USI arg3, USI arg4, USI arg5, USI arg6,
1386 SIM_DESC sd = CPU_STATE (current_cpu);
1387 host_callback *cb = STATE_CALLBACK (sd);
1389 int threadno = current_cpu->threadno;
1391 current_cpu->syscalls++;
1393 CB_SYSCALL_INIT (&s);
1399 if (callnum == TARGET_SYS_exit && current_cpu->m1threads == 0)
1401 if (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1402 & FLAG_CRIS_MISC_PROFILE_ALL)
1403 dump_statistics (current_cpu);
1404 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
1408 s.p2 = (PTR) current_cpu;
1409 s.read_mem = syscall_read_mem;
1410 s.write_mem = syscall_write_mem;
1412 current_cpu_for_cb_callback = current_cpu;
1414 if (cb_syscall (cb, &s) != CB_RC_OK)
1417 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
1419 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1422 retval = s.result == -1 ? -s.errcode : s.result;
1424 if (s.errcode != 0 && s.errcode == cb_host_to_target_errno (cb, ENOSYS))
1426 /* If the generic simulator call said ENOSYS, then let's try the
1427 ones we know ourselves.
1429 The convention is to provide *very limited* functionality on an
1430 as-needed basis, only what's covered by the test-suite, tests
1431 added when functionality changes and abort with a descriptive
1432 message for *everything* else. Where there's no test-case, we
1437 /* It's a pretty safe bet that the "old setup() system call"
1438 number will not be re-used; we can't say the same for higher
1439 numbers. We treat this simulator-generated call as "wait
1440 forever"; we re-run this insn. The wait is ended by a
1441 callback. Sanity check that this is the reason we got
1443 if (current_cpu->thread_data == NULL
1444 || (current_cpu->thread_data[threadno].pipe_write_fd == 0))
1445 goto unimplemented_syscall;
1447 sim_pc_set (current_cpu, pc);
1451 case TARGET_SYS_fcntl64:
1452 case TARGET_SYS_fcntl:
1457 Glibc checks stdin, stdout and stderr fd:s for
1458 close-on-exec security sanity. We just need to provide a
1459 OK return value. If we really need to have a
1460 close-on-exec flag true, we could just do a real fcntl
1466 /* F_SETFD. Just ignore attempts to set the close-on-exec
1472 /* F_GETFL. Check for the special case for open+fdopen. */
1473 if (current_cpu->last_syscall == TARGET_SYS_open
1474 && arg1 == current_cpu->last_open_fd)
1476 retval = current_cpu->last_open_flags & TARGET_O_ACCMODE;
1481 /* Because we can't freopen fd:s 0, 1, 2 to mean
1482 something else than stdin, stdout and stderr
1483 (sim/common/syscall.c:cb_syscall special cases fd
1484 0, 1 and 2), we know what flags that we can
1485 sanely return for these fd:s. */
1486 retval = TARGET_O_RDONLY;
1489 else if (arg1 == 1 || arg1 == 2)
1491 retval = TARGET_O_WRONLY;
1496 /* Nothing else is implemented. */
1498 = cris_unknown_syscall (current_cpu, pc,
1499 "Unimplemented %s syscall "
1500 "(fd: 0x%lx: cmd: 0x%lx arg: "
1502 callnum == TARGET_SYS_fcntl
1503 ? "fcntl" : "fcntl64",
1504 (unsigned long) (USI) arg1,
1505 (unsigned long) (USI) arg2,
1506 (unsigned long) (USI) arg3);
1511 case TARGET_SYS_uname:
1513 /* Fill in a few constants to appease glibc. */
1514 static const char sim_utsname[6][65] =
1524 if ((s.write_mem) (cb, &s, arg1, (const char *) sim_utsname,
1525 sizeof (sim_utsname))
1526 != sizeof (sim_utsname))
1527 retval = -cb_host_to_target_errno (cb, EFAULT);
1533 case TARGET_SYS_geteuid32:
1534 /* We tell the truth with these. Maybe we shouldn't, but it
1535 should match the "stat" information. */
1536 retval = geteuid ();
1539 case TARGET_SYS_getuid32:
1543 case TARGET_SYS_getegid32:
1544 retval = getegid ();
1547 case TARGET_SYS_getgid32:
1551 case TARGET_SYS_brk:
1552 /* Most often, we just return the argument, like the Linux
1557 retval = current_cpu->endbrk;
1558 else if (arg1 <= current_cpu->endmem)
1559 current_cpu->endbrk = arg1;
1562 USI new_end = (arg1 + 8191) & ~8191;
1564 /* If the simulator wants to brk more than a certain very
1565 large amount, something is wrong. FIXME: Return an error
1566 or abort? Have command-line selectable? */
1567 if (new_end - current_cpu->endmem > SIM_MAX_ALLOC_CHUNK)
1569 current_cpu->endbrk = current_cpu->endmem;
1570 retval = current_cpu->endmem;
1574 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1575 current_cpu->endmem,
1576 new_end - current_cpu->endmem,
1578 current_cpu->endbrk = arg1;
1579 current_cpu->endmem = new_end;
1583 case TARGET_SYS_getpid:
1584 /* Correct until CLONE_THREAD is implemented. */
1585 retval = current_cpu->thread_data == NULL
1587 : TARGET_PID + current_cpu->thread_data[threadno].threadid;
1590 case TARGET_SYS_getppid:
1591 /* Correct until CLONE_THREAD is implemented. */
1592 retval = current_cpu->thread_data == NULL
1595 + current_cpu->thread_data[threadno].parent_threadid);
1598 case TARGET_SYS_mmap2:
1607 /* If the simulator wants to mmap more than the very large
1608 limit, something is wrong. FIXME: Return an error or
1609 abort? Have command-line selectable? */
1610 if (len > SIM_MAX_ALLOC_CHUNK)
1612 retval = -cb_host_to_target_errno (cb, ENOMEM);
1616 if ((prot != (TARGET_PROT_READ | TARGET_PROT_WRITE)
1618 != (TARGET_PROT_READ
1620 | TARGET_PROT_EXEC))
1621 && prot != TARGET_PROT_READ)
1622 || (flags != (TARGET_MAP_ANONYMOUS | TARGET_MAP_PRIVATE)
1623 && flags != TARGET_MAP_PRIVATE
1624 && flags != TARGET_MAP_SHARED)
1625 || (fd != (USI) -1 && prot != TARGET_PROT_READ)
1629 = cris_unknown_syscall (current_cpu, pc,
1630 "Unimplemented mmap2 call "
1631 "(0x%lx, 0x%lx, 0x%lx, "
1632 "0x%lx, 0x%lx, 0x%lx)\n",
1633 (unsigned long) arg1,
1634 (unsigned long) arg2,
1635 (unsigned long) arg3,
1636 (unsigned long) arg4,
1637 (unsigned long) arg5,
1638 (unsigned long) arg6);
1641 else if (fd != (USI) -1)
1648 /* A non-aligned argument is allowed for files. */
1649 USI newlen = (len + 8191) & ~8191;
1651 /* We only support read, which we should already have
1652 checked. Check again anyway. */
1653 if (prot != TARGET_PROT_READ)
1657 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1660 if (newaddr >= (USI) -8191)
1663 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1667 /* Find the current position in the file. */
1668 s.func = TARGET_SYS_lseek;
1672 if (cb_syscall (cb, &s) != CB_RC_OK)
1679 /* Use the standard read callback to read in "len"
1681 s.func = TARGET_SYS_read;
1685 if (cb_syscall (cb, &s) != CB_RC_OK)
1688 if ((USI) s.result != len)
1691 /* After reading, we need to go back to the previous
1692 position in the file. */
1693 s.func = TARGET_SYS_lseek;
1697 if (cb_syscall (cb, &s) != CB_RC_OK)
1699 if (pos != (USI) s.result)
1707 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1708 (len + 8191) & ~8191);
1710 if (newaddr >= (USI) -8191)
1711 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1718 case TARGET_SYS_mprotect:
1720 /* We only cover the case of linuxthreads mprotecting out its
1721 stack guard page. */
1726 if ((addr & 8191) != 0
1728 || prot != TARGET_PROT_NONE
1729 || !is_mapped (sd, ¤t_cpu->highest_mmapped_page, addr,
1733 = cris_unknown_syscall (current_cpu, pc,
1734 "Unimplemented mprotect call "
1735 "(0x%lx, 0x%lx, 0x%lx)\n",
1736 (unsigned long) arg1,
1737 (unsigned long) arg2,
1738 (unsigned long) arg3);
1742 /* FIXME: We should account for pages like this that are
1743 "mprotected out". For now, we just tell the simulator
1744 core to remove that page from its map. */
1745 sim_core_detach (sd, NULL, 0, 0, addr);
1750 case TARGET_SYS_ioctl:
1752 /* We support only a very limited functionality: checking
1753 stdout with TCGETS to perform the isatty function. The
1754 TCGETS ioctl isn't actually performed or the result used by
1755 an isatty () caller in a "hello, world" program; only the
1756 return value is then used. Maybe we shouldn't care about
1757 the environment of the simulator regarding isatty, but
1758 that's been working before, in the xsim simulator. */
1759 if (arg2 == TARGET_TCGETS && arg1 == 1)
1760 retval = isatty (1) ? 0 : cb_host_to_target_errno (cb, EINVAL);
1762 retval = -cb_host_to_target_errno (cb, EINVAL);
1766 case TARGET_SYS_munmap:
1771 = unmap_pages (sd, ¤t_cpu->highest_mmapped_page, addr,
1773 retval = result != 0 ? -cb_host_to_target_errno (cb, result) : 0;
1777 case TARGET_SYS_wait4:
1785 /* FIXME: We're not properly implementing __WCLONE, and we
1786 don't really need the special casing so we might as well
1787 make this general. */
1788 if ((!(pid == (USI) -1
1789 && options == (TARGET___WCLONE | TARGET_WNOHANG)
1792 && (options == TARGET___WCLONE
1793 || options == TARGET___WALL)))
1795 || current_cpu->thread_data == NULL)
1798 = cris_unknown_syscall (current_cpu, pc,
1799 "Unimplemented wait4 call "
1800 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1801 (unsigned long) arg1,
1802 (unsigned long) arg2,
1803 (unsigned long) arg3,
1804 (unsigned long) arg4);
1808 if (pid == (USI) -1)
1809 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1811 if (current_cpu->thread_data[threadno].threadid
1812 == current_cpu->thread_data[i].parent_threadid
1813 && current_cpu->thread_data[i].threadid != 0
1814 && current_cpu->thread_data[i].cpu_context == NULL)
1816 /* A zombied child. Get the exit value and clear the
1817 zombied entry so it will be reused. */
1818 sim_core_write_unaligned_4 (current_cpu, pc, 0, saddr,
1820 ->thread_data[i].exitval);
1822 = current_cpu->thread_data[i].threadid + TARGET_PID;
1823 memset (¤t_cpu->thread_data[i], 0,
1824 sizeof (current_cpu->thread_data[i]));
1830 /* We're waiting for a specific PID. If we don't find
1831 it zombied on this run, rerun the syscall. */
1832 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1833 if (pid == current_cpu->thread_data[i].threadid + TARGET_PID
1834 && current_cpu->thread_data[i].cpu_context == NULL)
1837 /* Get the exit value if the caller wants it. */
1838 sim_core_write_unaligned_4 (current_cpu, pc, 0,
1845 = current_cpu->thread_data[i].threadid + TARGET_PID;
1846 memset (¤t_cpu->thread_data[i], 0,
1847 sizeof (current_cpu->thread_data[i]));
1852 sim_pc_set (current_cpu, pc);
1855 retval = -cb_host_to_target_errno (cb, ECHILD);
1860 case TARGET_SYS_rt_sigaction:
1868 __sighandler_t sa_handler;
1869 unsigned long sa_flags;
1870 void (*sa_restorer)(void);
1876 sim_core_write_unaligned_4 (current_cpu, pc, 0, old_sa + 0,
1877 current_cpu->sighandler[signum]);
1878 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 4, 0);
1879 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 8, 0);
1881 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
1882 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 12, 0);
1883 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 16, 0);
1887 USI target_sa_handler
1888 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa);
1890 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 4);
1891 USI target_sa_restorer
1892 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 8);
1893 USI target_sa_mask_low
1894 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 12);
1895 USI target_sa_mask_high
1896 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 16);
1898 /* We won't interrupt a syscall so we won't restart it,
1899 but a signal(2) call ends up syscalling rt_sigaction
1900 with this flag, so we have to handle it. The
1901 sa_restorer field contains garbage when not
1902 TARGET_SA_RESTORER, so don't look at it. For the
1903 time being, we don't nest sighandlers, so we
1904 ignore the sa_mask, which simplifies things. */
1905 if ((target_sa_flags != 0
1906 && target_sa_flags != TARGET_SA_RESTART
1907 && target_sa_flags != (TARGET_SA_RESTART|TARGET_SA_SIGINFO))
1908 || target_sa_handler == 0)
1911 = cris_unknown_syscall (current_cpu, pc,
1912 "Unimplemented rt_sigaction "
1915 "[0x%x, 0x%x, 0x%x, "
1916 "{0x%x, 0x%x}], 0x%lx)\n",
1917 (unsigned long) arg1,
1918 (unsigned long) arg2,
1923 target_sa_mask_high,
1924 (unsigned long) arg3);
1928 current_cpu->sighandler[signum] = target_sa_handler;
1930 /* Because we may have unblocked signals, one may now be
1931 pending, if there are threads, that is. */
1932 if (current_cpu->thread_data)
1933 current_cpu->thread_data[threadno].sigpending = 1;
1939 case TARGET_SYS_mremap:
1945 USI new_addr = arg5;
1948 if (new_len == old_len)
1949 /* The program and/or library is possibly confused but
1950 this is a valid call. Happens with ipps-1.40 on file
1953 else if (new_len < old_len)
1955 /* Shrinking is easy. */
1956 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1957 addr + new_len, old_len - new_len) != 0)
1958 retval = -cb_host_to_target_errno (cb, EINVAL);
1962 else if (! is_mapped (sd, ¤t_cpu->highest_mmapped_page,
1963 addr + old_len, new_len - old_len))
1965 /* If the extension isn't mapped, we can just add it. */
1967 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1968 addr + old_len, new_len - old_len);
1970 if (mapped_addr > (USI) -8192)
1971 retval = -cb_host_to_target_errno (cb, -(SI) mapped_addr);
1975 else if (flags & TARGET_MREMAP_MAYMOVE)
1977 /* Create a whole new map and copy the contents
1978 block-by-block there. We ignore the new_addr argument
1981 USI prev_addr = addr;
1982 USI prev_len = old_len;
1985 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1988 if (mapped_addr > (USI) -8192)
1990 retval = -cb_host_to_target_errno (cb, -(SI) new_addr);
1994 retval = mapped_addr;
1997 old_len -= 8192, mapped_addr += 8192, addr += 8192)
1999 if (sim_core_read_buffer (sd, current_cpu, read_map, buf,
2001 || sim_core_write_buffer (sd, current_cpu, 0, buf,
2002 mapped_addr, 8192) != 8192)
2006 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
2007 prev_addr, prev_len) != 0)
2011 retval = -cb_host_to_target_errno (cb, -ENOMEM);
2015 case TARGET_SYS_poll:
2017 int npollfds = arg2;
2033 /* Check that this is the expected poll call from
2034 linuxthreads/manager.c; we don't support anything else.
2035 Remember, fd == 0 isn't supported. */
2037 || ((fd = sim_core_read_unaligned_4 (current_cpu, pc,
2039 || ((events = sim_core_read_unaligned_2 (current_cpu, pc,
2042 || ((cb->fstat) (cb, fd, &buf) != 0
2043 || (buf.st_mode & S_IFIFO) == 0)
2044 || current_cpu->thread_data == NULL)
2047 = cris_unknown_syscall (current_cpu, pc,
2048 "Unimplemented poll syscall "
2049 "(0x%lx: [0x%x, 0x%x, x], "
2051 (unsigned long) arg1, fd, events,
2052 (unsigned long) arg2,
2053 (unsigned long) arg3);
2059 /* Iterate over threads; find a marker that a writer is
2060 sleeping, waiting for a reader. */
2061 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2062 if (current_cpu->thread_data[i].cpu_context != NULL
2063 && current_cpu->thread_data[i].pipe_read_fd == fd)
2065 revents = TARGET_POLLIN;
2070 /* Timeout decreases with whatever time passed between the
2071 last syscall and this. That's not exactly right for the
2072 first call, but it's close enough that it isn't
2073 worthwhile to complicate matters by making that a special
2076 -= (TARGET_TIME_MS (current_cpu)
2077 - (current_cpu->thread_data[threadno].last_execution));
2079 /* Arrange to repeat this syscall until timeout or event,
2080 decreasing timeout at each iteration. */
2081 if (timeout > 0 && revents == 0)
2083 bfd_byte timeout_buf[4];
2085 bfd_putl32 (timeout, timeout_buf);
2086 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2087 H_GR_R12, timeout_buf, 4);
2088 sim_pc_set (current_cpu, pc);
2093 sim_core_write_unaligned_2 (current_cpu, pc, 0, ufds + 4 + 2,
2098 case TARGET_SYS_time:
2100 retval = (int) (*cb->time) (cb, 0L);
2102 /* At time of this writing, CB_SYSCALL_time doesn't do the
2103 part of setting *arg1 to the return value. */
2105 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, retval);
2109 case TARGET_SYS_gettimeofday:
2112 USI ts = TARGET_TIME (current_cpu);
2113 USI tms = TARGET_TIME_MS (current_cpu);
2115 /* First dword is seconds since TARGET_EPOCH. */
2116 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, ts);
2118 /* Second dword is microseconds. */
2119 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1 + 4,
2120 (tms % 1000) * 1000);
2124 /* Time-zone info is always cleared. */
2125 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, 0);
2126 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, 0);
2131 case TARGET_SYS_llseek:
2133 /* If it fits, tweak parameters to fit the "generic" 32-bit
2134 lseek and use that. */
2142 if (!((offs_hi == 0 && offs_lo >= 0)
2143 || (offs_hi == -1 && offs_lo < 0)))
2146 = cris_unknown_syscall (current_cpu, pc,
2147 "Unimplemented llseek offset,"
2148 " fd %d: 0x%x:0x%x\n",
2149 fd, (unsigned) arg2,
2154 s.func = TARGET_SYS_lseek;
2157 if (cb_syscall (cb, &s) != CB_RC_OK)
2159 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
2161 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2164 retval = -s.errcode;
2167 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp,
2169 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp + 4,
2170 s.result < 0 ? -1 : 0);
2175 /* This one does have a generic callback function, but at the time
2176 of this writing, cb_syscall does not have code for it, and we
2177 need target-specific code for the threads implementation
2179 case TARGET_SYS_kill:
2186 /* At kill(2), glibc sets signal masks such that the thread
2187 machinery is initialized. Still, there is and was only
2189 if (current_cpu->max_threadid == 0)
2191 if (pid != TARGET_PID)
2193 retval = -cb_host_to_target_errno (cb, EPERM);
2197 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2198 if (sig == TARGET_SIGABRT)
2199 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2200 the end-point for failing GCC test-cases. */
2201 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2205 sim_io_eprintf (sd, "Unimplemented signal: %d\n", sig);
2206 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2210 /* This will not be reached. */
2214 retval = deliver_signal (current_cpu, sig, pid);
2218 case TARGET_SYS_rt_sigprocmask:
2225 if (how != TARGET_SIG_BLOCK
2226 && how != TARGET_SIG_SETMASK
2227 && how != TARGET_SIG_UNBLOCK)
2230 = cris_unknown_syscall (current_cpu, pc,
2231 "Unimplemented rt_sigprocmask "
2232 "syscall (0x%x, 0x%x, 0x%x)\n",
2240 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2243 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2246 /* The sigmask is kept in the per-thread data, so we may
2247 need to create the first one. */
2248 if (current_cpu->thread_data == NULL)
2249 make_first_thread (current_cpu);
2251 if (how == TARGET_SIG_SETMASK)
2252 for (i = 0; i < 64; i++)
2253 current_cpu->thread_data[threadno].sigdata[i].blocked = 0;
2255 for (i = 0; i < 32; i++)
2256 if ((set_low & (1 << i)))
2257 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2258 = (how != TARGET_SIG_UNBLOCK);
2260 for (i = 0; i < 31; i++)
2261 if ((set_high & (1 << i)))
2262 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2263 = (how != TARGET_SIG_UNBLOCK);
2265 /* The mask changed, so a signal may be unblocked for
2267 current_cpu->thread_data[threadno].sigpending = 1;
2275 for (i = 0; i < 32; i++)
2276 if (current_cpu->thread_data[threadno]
2277 .sigdata[i + 1].blocked)
2279 for (i = 0; i < 31; i++)
2280 if (current_cpu->thread_data[threadno]
2281 .sigdata[i + 33].blocked)
2284 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 0, set_low);
2285 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 4, set_high);
2292 case TARGET_SYS_sigreturn:
2296 int was_sigsuspended;
2298 if (current_cpu->thread_data == NULL
2299 /* The CPU context is saved with the simulator data, not
2300 on the stack as in the real world. */
2301 || (current_cpu->thread_data[threadno].cpu_context_atsignal
2305 = cris_unknown_syscall (current_cpu, pc,
2306 "Invalid sigreturn syscall: "
2307 "no signal handler active "
2308 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, "
2310 (unsigned long) arg1,
2311 (unsigned long) arg2,
2312 (unsigned long) arg3,
2313 (unsigned long) arg4,
2314 (unsigned long) arg5,
2315 (unsigned long) arg6);
2320 = current_cpu->thread_data[threadno].sigsuspended;
2322 /* Restore the sigmask, either from the stack copy made when
2323 the sighandler was called, or from the saved state
2324 specifically for sigsuspend(2). */
2325 if (was_sigsuspended)
2327 current_cpu->thread_data[threadno].sigsuspended = 0;
2328 for (i = 0; i < 64; i++)
2329 current_cpu->thread_data[threadno].sigdata[i].blocked
2330 = current_cpu->thread_data[threadno]
2331 .sigdata[i].blocked_suspendsave;
2339 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2340 H_GR_SP, regbuf, 4);
2341 sp = bfd_getl32 (regbuf);
2343 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp);
2345 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp + 4);
2347 for (i = 0; i < 32; i++)
2348 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2349 = (set_low & (1 << i)) != 0;
2350 for (i = 0; i < 31; i++)
2351 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2352 = (set_high & (1 << i)) != 0;
2355 /* The mask changed, so a signal may be unblocked for
2357 current_cpu->thread_data[threadno].sigpending = 1;
2359 memcpy (¤t_cpu->cpu_data_placeholder,
2360 current_cpu->thread_data[threadno].cpu_context_atsignal,
2361 current_cpu->thread_cpu_data_size);
2362 free (current_cpu->thread_data[threadno].cpu_context_atsignal);
2363 current_cpu->thread_data[threadno].cpu_context_atsignal = NULL;
2365 /* The return value must come from the saved R10. */
2366 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, regbuf, 4);
2367 retval = bfd_getl32 (regbuf);
2369 /* We must also break the "sigsuspension loop". */
2370 if (was_sigsuspended)
2371 sim_pc_set (current_cpu, sim_pc_get (current_cpu) + 2);
2375 case TARGET_SYS_rt_sigsuspend:
2383 = cris_unknown_syscall (current_cpu, pc,
2384 "Unimplemented rt_sigsuspend syscall"
2385 " arguments (0x%lx, 0x%lx)\n",
2386 (unsigned long) arg1,
2387 (unsigned long) arg2);
2391 /* Don't change the signal mask if we're already in
2392 sigsuspend state (i.e. this syscall is a rerun). */
2393 else if (!current_cpu->thread_data[threadno].sigsuspended)
2396 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2399 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2403 /* Save the current sigmask and insert the user-supplied
2405 for (i = 0; i < 32; i++)
2407 current_cpu->thread_data[threadno]
2408 .sigdata[i + 1].blocked_suspendsave
2409 = current_cpu->thread_data[threadno]
2410 .sigdata[i + 1].blocked;
2412 current_cpu->thread_data[threadno]
2413 .sigdata[i + 1].blocked = (set_low & (1 << i)) != 0;
2415 for (i = 0; i < 31; i++)
2417 current_cpu->thread_data[threadno]
2418 .sigdata[i + 33].blocked_suspendsave
2419 = current_cpu->thread_data[threadno]
2420 .sigdata[i + 33].blocked;
2421 current_cpu->thread_data[threadno]
2422 .sigdata[i + 33].blocked = (set_high & (1 << i)) != 0;
2425 current_cpu->thread_data[threadno].sigsuspended = 1;
2427 /* The mask changed, so a signal may be unblocked for
2429 current_cpu->thread_data[threadno].sigpending = 1;
2432 /* Because we don't use arg1 (newsetp) when this syscall is
2433 rerun, it doesn't matter that we overwrite it with the
2434 (constant) return value. */
2435 retval = -cb_host_to_target_errno (cb, EINTR);
2436 sim_pc_set (current_cpu, pc);
2440 /* Add case labels here for other syscalls using the 32-bit
2441 "struct stat", provided they have a corresponding simulator
2442 function of course. */
2443 case TARGET_SYS_stat:
2444 case TARGET_SYS_fstat:
2446 /* As long as the infrastructure doesn't cache anything
2447 related to the stat mapping, this trick gets us a dual
2448 "struct stat"-type mapping in the least error-prone way. */
2449 const char *saved_map = cb->stat_map;
2450 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
2452 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_stat32_map;
2453 cb->stat_map = stat32_map;
2455 if (cb_syscall (cb, &s) != CB_RC_OK)
2458 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2461 retval = s.result == -1 ? -s.errcode : s.result;
2463 cb->stat_map = saved_map;
2464 cb->syscall_map = saved_syscall_map;
2468 case TARGET_SYS_getcwd:
2473 char *cwd = xmalloc (SIM_PATHMAX);
2474 if (cwd != getcwd (cwd, SIM_PATHMAX))
2477 /* FIXME: When and if we support chdir, we need something
2478 a bit more elaborate. */
2479 if (simulator_sysroot[0] != '\0')
2482 retval = -cb_host_to_target_errno (cb, ERANGE);
2483 if (strlen (cwd) + 1 <= size)
2485 retval = strlen (cwd) + 1;
2486 if (sim_core_write_buffer (sd, current_cpu, 0, cwd,
2488 != (unsigned int) retval)
2489 retval = -cb_host_to_target_errno (cb, EFAULT);
2495 case TARGET_SYS_readlink:
2500 char *pbuf = xmalloc (SIM_PATHMAX);
2501 char *lbuf = xmalloc (SIM_PATHMAX);
2502 char *lbuf_alloc = lbuf;
2507 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2509 strcpy (pbuf, simulator_sysroot);
2510 o += strlen (simulator_sysroot);
2513 for (i = 0; i + o < SIM_PATHMAX; i++)
2516 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2517 if (pbuf[i + o] == 0)
2521 if (i + o == SIM_PATHMAX)
2523 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2527 /* Intervene calls for certain files expected in the target
2528 proc file system. */
2529 if (strcmp (pbuf + strlen (simulator_sysroot),
2530 "/proc/" XSTRING (TARGET_PID) "/exe") == 0)
2533 = (STATE_PROG_ARGV (sd) != NULL
2534 ? *STATE_PROG_ARGV (sd) : NULL);
2536 if (argv0 == NULL || *argv0 == '.')
2539 = cris_unknown_syscall (current_cpu, pc,
2540 "Unimplemented readlink syscall "
2541 "(0x%lx: [\"%s\"], 0x%lx)\n",
2542 (unsigned long) arg1, pbuf,
2543 (unsigned long) arg2);
2546 else if (*argv0 == '/')
2548 if (strncmp (simulator_sysroot, argv0,
2549 strlen (simulator_sysroot)) == 0)
2550 argv0 += strlen (simulator_sysroot);
2552 strcpy (lbuf, argv0);
2553 nchars = strlen (argv0) + 1;
2557 if (getcwd (lbuf, SIM_PATHMAX) != NULL
2558 && strlen (lbuf) + 2 + strlen (argv0) < SIM_PATHMAX)
2560 if (strncmp (simulator_sysroot, lbuf,
2561 strlen (simulator_sysroot)) == 0)
2562 lbuf += strlen (simulator_sysroot);
2565 strcat (lbuf, argv0);
2566 nchars = strlen (lbuf) + 1;
2573 nchars = readlink (pbuf, lbuf, SIM_PATHMAX);
2575 /* We trust that the readlink result returns a *relative*
2576 link, or one already adjusted for the file-path-prefix.
2577 (We can't generally tell the difference, so we go with
2578 the easiest decision; no adjustment.) */
2582 retval = -cb_host_to_target_errno (cb, errno);
2586 if (bufsiz < nchars)
2589 if (sim_core_write_buffer (sd, current_cpu, write_map, lbuf,
2590 buf, nchars) != (unsigned int) nchars)
2591 retval = -cb_host_to_target_errno (cb, EFAULT);
2600 case TARGET_SYS_sched_getscheduler:
2604 /* FIXME: Search (other) existing threads. */
2605 if (pid != 0 && pid != TARGET_PID)
2606 retval = -cb_host_to_target_errno (cb, ESRCH);
2608 retval = TARGET_SCHED_OTHER;
2612 case TARGET_SYS_sched_getparam:
2618 struct sched_param {
2622 if (pid != 0 && pid != TARGET_PID)
2623 retval = -cb_host_to_target_errno (cb, ESRCH);
2626 /* FIXME: Save scheduler setting before threads are
2628 sim_core_write_unaligned_4 (current_cpu, pc, 0, paramp,
2629 current_cpu->thread_data != NULL
2631 ->thread_data[threadno]
2639 case TARGET_SYS_sched_setparam:
2644 if ((pid != 0 && pid != TARGET_PID)
2645 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2647 retval = -cb_host_to_target_errno (cb, EINVAL);
2653 case TARGET_SYS_sched_setscheduler:
2659 if ((pid != 0 && pid != TARGET_PID)
2660 || policy != TARGET_SCHED_OTHER
2661 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2663 retval = -cb_host_to_target_errno (cb, EINVAL);
2665 /* FIXME: Save scheduler setting to be read in later
2666 sched_getparam calls. */
2671 case TARGET_SYS_sched_yield:
2672 /* We reschedule to the next thread after a syscall anyway, so
2673 we don't have to do anything here than to set the return
2678 case TARGET_SYS_sched_get_priority_min:
2679 case TARGET_SYS_sched_get_priority_max:
2681 retval = -cb_host_to_target_errno (cb, EINVAL);
2686 case TARGET_SYS_ugetrlimit:
2688 unsigned int curlim, maxlim;
2689 if (arg1 != TARGET_RLIMIT_STACK && arg1 != TARGET_RLIMIT_NOFILE)
2691 retval = -cb_host_to_target_errno (cb, EINVAL);
2697 unsigned long rlim_cur;
2698 unsigned long rlim_max;
2700 if (arg1 == TARGET_RLIMIT_NOFILE)
2702 /* Sadly a very low limit. Better not lie, though. */
2703 maxlim = curlim = MAX_CALLBACK_FDS;
2705 else /* arg1 == TARGET_RLIMIT_STACK */
2707 maxlim = 0xffffffff;
2710 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, curlim);
2711 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, maxlim);
2716 case TARGET_SYS_setrlimit:
2717 if (arg1 != TARGET_RLIMIT_STACK)
2719 retval = -cb_host_to_target_errno (cb, EINVAL);
2722 /* FIXME: Save values for future ugetrlimit calls. */
2726 /* Provide a very limited subset of the sysctl functions, and
2727 abort for the rest. */
2728 case TARGET_SYS__sysctl:
2731 struct __sysctl_args {
2738 unsigned long __unused[4];
2740 SI name = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1);
2741 SI name0 = name == 0
2742 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name);
2743 SI name1 = name == 0
2744 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name + 4);
2746 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 4);
2748 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 8);
2750 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 12);
2751 SI oldlen = oldlenp == 0
2752 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, oldlenp);
2754 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 16);
2756 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 20);
2758 if (name0 == TARGET_CTL_KERN && name1 == TARGET_CTL_KERN_VERSION)
2760 SI to_write = oldlen < (SI) sizeof (TARGET_UTSNAME)
2761 ? oldlen : (SI) sizeof (TARGET_UTSNAME);
2763 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldlenp,
2764 sizeof (TARGET_UTSNAME));
2766 if (sim_core_write_buffer (sd, current_cpu, write_map,
2767 TARGET_UTSNAME, oldval,
2769 != (unsigned int) to_write)
2770 retval = -cb_host_to_target_errno (cb, EFAULT);
2777 = cris_unknown_syscall (current_cpu, pc,
2778 "Unimplemented _sysctl syscall "
2779 "(0x%lx: [0x%lx, 0x%lx],"
2780 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2781 (unsigned long) name,
2782 (unsigned long) name0,
2783 (unsigned long) name1,
2784 (unsigned long) nlen,
2785 (unsigned long) oldval,
2786 (unsigned long) oldlenp,
2787 (unsigned long) newval,
2788 (unsigned long) newlen);
2792 case TARGET_SYS_exit:
2794 /* Here for all but the last thread. */
2797 = current_cpu->thread_data[threadno].threadid + TARGET_PID;
2799 = (current_cpu->thread_data[threadno].parent_threadid
2801 int exitsig = current_cpu->thread_data[threadno].exitsig;
2803 /* Any children are now all orphans. */
2804 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2805 if (current_cpu->thread_data[i].parent_threadid
2806 == current_cpu->thread_data[threadno].threadid)
2807 /* Make getppid(2) return 1 for them, poor little ones. */
2808 current_cpu->thread_data[i].parent_threadid = -TARGET_PID + 1;
2810 /* Free the cpu context data. When the parent has received
2811 the exit status, we'll clear the entry too. */
2812 free (current_cpu->thread_data[threadno].cpu_context);
2813 current_cpu->thread_data[threadno].cpu_context = NULL;
2814 current_cpu->m1threads--;
2817 sim_io_eprintf (sd, "Thread %d exited with status %d\n",
2819 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2823 /* Still, we may want to support non-zero exit values. */
2824 current_cpu->thread_data[threadno].exitval = arg1 << 8;
2827 deliver_signal (current_cpu, exitsig, ppid);
2831 case TARGET_SYS_clone:
2833 int nthreads = current_cpu->m1threads + 1;
2834 void *thread_cpu_data;
2835 bfd_byte old_sp_buf[4];
2837 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2840 /* That's right, the syscall clone arguments are reversed
2841 compared to sys_clone notes in clone(2) and compared to
2842 other Linux ports (i.e. it's the same order as in the
2843 clone(2) libcall). */
2847 if (nthreads == SIM_TARGET_MAX_THREADS)
2849 retval = -cb_host_to_target_errno (cb, EAGAIN);
2853 /* FIXME: Implement the low byte. */
2854 if ((flags & ~TARGET_CSIGNAL) !=
2857 | TARGET_CLONE_FILES
2858 | TARGET_CLONE_SIGHAND)
2862 = cris_unknown_syscall (current_cpu, pc,
2863 "Unimplemented clone syscall "
2865 (unsigned long) arg1,
2866 (unsigned long) arg2);
2870 if (current_cpu->thread_data == NULL)
2871 make_first_thread (current_cpu);
2873 /* The created thread will get the new SP and a cleared R10.
2874 Since it's created out of a copy of the old thread and we
2875 don't have a set-register-function that just take the
2876 cpu_data as a parameter, we set the childs values first,
2877 and write back or overwrite them in the parent after the
2879 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2880 H_GR_SP, old_sp_buf, 4);
2881 bfd_putl32 (newsp, sp_buf);
2882 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2883 H_GR_SP, sp_buf, 4);
2884 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2885 H_GR_R10, (bfd_byte *) zeros, 4);
2888 ->make_thread_cpu_data) (current_cpu,
2889 ¤t_cpu->cpu_data_placeholder);
2890 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2891 H_GR_SP, old_sp_buf, 4);
2893 retval = ++current_cpu->max_threadid + TARGET_PID;
2895 /* Find an unused slot. After a few threads have been created
2896 and exited, the array is expected to be a bit fragmented.
2897 We don't reuse the first entry, though, that of the
2899 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
2900 if (current_cpu->thread_data[i].cpu_context == NULL
2901 /* Don't reuse a zombied entry. */
2902 && current_cpu->thread_data[i].threadid == 0)
2905 memcpy (¤t_cpu->thread_data[i],
2906 ¤t_cpu->thread_data[threadno],
2907 sizeof (current_cpu->thread_data[i]));
2908 current_cpu->thread_data[i].cpu_context = thread_cpu_data;
2909 current_cpu->thread_data[i].cpu_context_atsignal = NULL;
2910 current_cpu->thread_data[i].threadid = current_cpu->max_threadid;
2911 current_cpu->thread_data[i].parent_threadid
2912 = current_cpu->thread_data[threadno].threadid;
2913 current_cpu->thread_data[i].pipe_read_fd = 0;
2914 current_cpu->thread_data[i].pipe_write_fd = 0;
2915 current_cpu->thread_data[i].at_syscall = 0;
2916 current_cpu->thread_data[i].sigpending = 0;
2917 current_cpu->thread_data[i].sigsuspended = 0;
2918 current_cpu->thread_data[i].exitsig = flags & TARGET_CSIGNAL;
2919 current_cpu->m1threads = nthreads;
2923 /* Better watch these in case they do something necessary. */
2924 case TARGET_SYS_socketcall:
2925 retval = -cb_host_to_target_errno (cb, ENOSYS);
2928 unimplemented_syscall:
2931 = cris_unknown_syscall (current_cpu, pc,
2932 "Unimplemented syscall: %d "
2933 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
2934 callnum, arg1, arg2, arg3, arg4, arg5,
2939 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
2940 if (callnum == TARGET_SYS_open)
2942 current_cpu->last_open_fd = retval;
2943 current_cpu->last_open_flags = arg2;
2946 current_cpu->last_syscall = callnum;
2948 /* A system call is a rescheduling point. For the time being, we don't
2949 reschedule anywhere else. */
2950 if (current_cpu->m1threads != 0
2951 /* We need to schedule off from an exiting thread that is the
2953 || (current_cpu->thread_data != NULL
2954 && current_cpu->thread_data[threadno].cpu_context == NULL))
2956 bfd_byte retval_buf[4];
2958 current_cpu->thread_data[threadno].last_execution
2959 = TARGET_TIME_MS (current_cpu);
2960 bfd_putl32 (retval, retval_buf);
2961 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2963 current_cpu->thread_data[threadno].at_syscall = 1;
2964 reschedule (current_cpu);
2966 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2967 retval = bfd_getl32 (retval_buf);
2973 /* Callback from simulator write saying that the pipe at (reader, writer)
2974 is now non-empty (so the writer should wait until the pipe is empty, at
2975 least not write to this or any other pipe). Simplest is to just wait
2976 until the pipe is empty. */
2979 cris_pipe_nonempty (host_callback *cb ATTRIBUTE_UNUSED,
2980 int reader, int writer)
2982 SIM_CPU *cpu = current_cpu_for_cb_callback;
2983 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2985 /* It's the current thread: we just have to re-run the current
2986 syscall instruction (presumably "break 13") and change the syscall
2987 to the special simulator-wait code. Oh, and set a marker that
2988 we're waiting, so we can disambiguate the special call from a
2991 This function may be called multiple times between cris_pipe_empty,
2992 but we must avoid e.g. decreasing PC every time. Check fd markers
2994 if (cpu->thread_data == NULL)
2996 sim_io_eprintf (CPU_STATE (cpu),
2997 "Terminating simulation due to writing pipe rd:wr %d:%d"
2998 " from one single thread\n", reader, writer);
2999 sim_engine_halt (CPU_STATE (cpu), cpu,
3000 NULL, sim_pc_get (cpu), sim_stopped, SIM_SIGILL);
3002 else if (cpu->thread_data[cpu->threadno].pipe_write_fd == 0)
3004 cpu->thread_data[cpu->threadno].pipe_write_fd = writer;
3005 cpu->thread_data[cpu->threadno].pipe_read_fd = reader;
3006 /* FIXME: We really shouldn't change registers other than R10 in
3007 syscalls (like R9), here or elsewhere. */
3008 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R9, (bfd_byte *) zeros, 4);
3009 sim_pc_set (cpu, sim_pc_get (cpu) - 2);
3013 /* Callback from simulator close or read call saying that the pipe at
3014 (reader, writer) is now empty (so the writer can write again, perhaps
3015 leave a waiting state). If there are bytes remaining, they couldn't be
3016 consumed (perhaps due to the pipe closing). */
3019 cris_pipe_empty (host_callback *cb,
3024 SIM_CPU *cpu = current_cpu_for_cb_callback;
3025 bfd_byte r10_buf[4];
3027 = cb->pipe_buffer[writer].size - cb->pipe_buffer[reader].size;
3029 /* We need to find the thread that waits for this pipe. */
3030 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
3031 if (cpu->thread_data[i].cpu_context
3032 && cpu->thread_data[i].pipe_write_fd == writer)
3036 /* Temporarily switch to this cpu context, so we can change the
3037 PC by ordinary calls. */
3039 memcpy (cpu->thread_data[cpu->threadno].cpu_context,
3040 &cpu->cpu_data_placeholder,
3041 cpu->thread_cpu_data_size);
3042 memcpy (&cpu->cpu_data_placeholder,
3043 cpu->thread_data[i].cpu_context,
3044 cpu->thread_cpu_data_size);
3046 /* The return value is supposed to contain the number of
3047 written bytes, which is the number of bytes requested and
3048 returned at the write call. You might think the right
3049 thing is to adjust the return-value to be only the
3050 *consumed* number of bytes, but it isn't. We're only
3051 called if the pipe buffer is fully consumed or it is being
3052 closed, possibly with remaining bytes. For the latter
3053 case, the writer is still supposed to see success for
3054 PIPE_BUF bytes (a constant which we happen to know and is
3055 unlikely to change). The return value may also be a
3056 negative number; an error value. This case is covered
3057 because "remaining" is always >= 0. */
3058 (*CPU_REG_FETCH (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3059 retval = (int) bfd_getl_signed_32 (r10_buf);
3060 if (retval - remaining > TARGET_PIPE_BUF)
3062 bfd_putl32 (retval - remaining, r10_buf);
3063 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3065 sim_pc_set (cpu, sim_pc_get (cpu) + 2);
3066 memcpy (cpu->thread_data[i].cpu_context,
3067 &cpu->cpu_data_placeholder,
3068 cpu->thread_cpu_data_size);
3069 memcpy (&cpu->cpu_data_placeholder,
3070 cpu->thread_data[cpu->threadno].cpu_context,
3071 cpu->thread_cpu_data_size);
3072 cpu->thread_data[i].pipe_read_fd = 0;
3073 cpu->thread_data[i].pipe_write_fd = 0;
3080 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3083 cris_time (host_callback *cb ATTRIBUTE_UNUSED, long *t)
3085 long retval = TARGET_TIME (current_cpu_for_cb_callback);
3091 /* Set target-specific callback data. */
3094 cris_set_callbacks (host_callback *cb)
3096 /* Yeargh, have to cast away constness to avoid warnings. */
3097 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
3098 cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
3100 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3101 parameter to cb_store_target_endian will make st_size negative.
3102 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3103 *unsigned*, and/or add syntax for signed-ness. */
3104 cb->stat_map = stat_map;
3105 cb->open_map = (CB_TARGET_DEFS_MAP *) open_map;
3106 cb->pipe_nonempty = cris_pipe_nonempty;
3107 cb->pipe_empty = cris_pipe_empty;
3108 cb->time = cris_time;
3111 /* Process an address exception. */
3114 cris_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
3115 unsigned int map, int nr_bytes, address_word addr,
3116 transfer_type transfer, sim_core_signals sig)
3118 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,