1 /* CRIS exception, interrupt, and trap (EIT) support
2 Copyright (C) 2004, 2005, 2007, 2008 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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "sim-options.h"
23 /* FIXME: get rid of targ-vals.h usage everywhere else. */
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
38 #ifdef HAVE_SYS_STAT_H
41 /* For PATH_MAX, originally. */
46 /* From ld/sysdep.h. */
48 # define SIM_PATHMAX PATH_MAX
51 # define SIM_PATHMAX MAXPATHLEN
53 # define SIM_PATHMAX 1024
57 /* The verbatim values are from asm-cris/unistd.h. */
59 #define TARGET_SYS_exit 1
60 #define TARGET_SYS_read 3
61 #define TARGET_SYS_write 4
62 #define TARGET_SYS_open 5
63 #define TARGET_SYS_close 6
64 #define TARGET_SYS_unlink 10
65 #define TARGET_SYS_time 13
66 #define TARGET_SYS_lseek 19
67 #define TARGET_SYS_getpid 20
68 #define TARGET_SYS_access 33
69 #define TARGET_SYS_kill 37
70 #define TARGET_SYS_rename 38
71 #define TARGET_SYS_pipe 42
72 #define TARGET_SYS_brk 45
73 #define TARGET_SYS_ioctl 54
74 #define TARGET_SYS_fcntl 55
75 #define TARGET_SYS_getppid 64
76 #define TARGET_SYS_setrlimit 75
77 #define TARGET_SYS_gettimeofday 78
78 #define TARGET_SYS_readlink 85
79 #define TARGET_SYS_munmap 91
80 #define TARGET_SYS_truncate 92
81 #define TARGET_SYS_ftruncate 93
82 #define TARGET_SYS_socketcall 102
83 #define TARGET_SYS_stat 106
84 #define TARGET_SYS_fstat 108
85 #define TARGET_SYS_wait4 114
86 #define TARGET_SYS_sigreturn 119
87 #define TARGET_SYS_clone 120
88 #define TARGET_SYS_uname 122
89 #define TARGET_SYS_mprotect 125
90 #define TARGET_SYS_llseek 140
91 #define TARGET_SYS_writev 146
92 #define TARGET_SYS__sysctl 149
93 #define TARGET_SYS_sched_setparam 154
94 #define TARGET_SYS_sched_getparam 155
95 #define TARGET_SYS_sched_setscheduler 156
96 #define TARGET_SYS_sched_getscheduler 157
97 #define TARGET_SYS_sched_yield 158
98 #define TARGET_SYS_sched_get_priority_max 159
99 #define TARGET_SYS_sched_get_priority_min 160
100 #define TARGET_SYS_mremap 163
101 #define TARGET_SYS_poll 168
102 #define TARGET_SYS_rt_sigaction 174
103 #define TARGET_SYS_rt_sigprocmask 175
104 #define TARGET_SYS_rt_sigsuspend 179
105 #define TARGET_SYS_getcwd 183
106 #define TARGET_SYS_ugetrlimit 191
107 #define TARGET_SYS_mmap2 192
108 #define TARGET_SYS_stat64 195
109 #define TARGET_SYS_lstat64 196
110 #define TARGET_SYS_fstat64 197
111 #define TARGET_SYS_geteuid32 201
112 #define TARGET_SYS_getuid32 199
113 #define TARGET_SYS_getegid32 202
114 #define TARGET_SYS_getgid32 200
115 #define TARGET_SYS_fcntl64 221
116 #define TARGET_SYS_exit_group 252
118 #define TARGET_PROT_READ 0x1
119 #define TARGET_PROT_WRITE 0x2
120 #define TARGET_PROT_EXEC 0x4
121 #define TARGET_PROT_NONE 0x0
123 #define TARGET_MAP_SHARED 0x01
124 #define TARGET_MAP_PRIVATE 0x02
125 #define TARGET_MAP_TYPE 0x0f
126 #define TARGET_MAP_FIXED 0x10
127 #define TARGET_MAP_ANONYMOUS 0x20
128 #define TARGET_MAP_DENYWRITE 0x800
130 #define TARGET_CTL_KERN 1
131 #define TARGET_CTL_VM 2
132 #define TARGET_CTL_NET 3
133 #define TARGET_CTL_PROC 4
134 #define TARGET_CTL_FS 5
135 #define TARGET_CTL_DEBUG 6
136 #define TARGET_CTL_DEV 7
137 #define TARGET_CTL_BUS 8
138 #define TARGET_CTL_ABI 9
140 #define TARGET_CTL_KERN_VERSION 4
143 #define TARGET_MREMAP_MAYMOVE 1
144 #define TARGET_MREMAP_FIXED 2
146 #define TARGET_TCGETS 0x5401
148 #define TARGET_UTSNAME "#7 Thu Jan 1 00:00:00 MET 2009"
150 /* Seconds since 1970-01-01 to the above date + 10 minutes;
151 'date -d "Thu Jan 1 00:00:10 MET 2009" +%s'. */
152 #define TARGET_EPOCH 1230764410
154 /* Milliseconds since start of run. We use the number of syscalls to
155 avoid introducing noise in the execution time. */
156 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
158 /* Seconds as in time(2). */
159 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
161 #define TARGET_SCHED_OTHER 0
163 #define TARGET_RLIMIT_STACK 3
164 #define TARGET_RLIMIT_NOFILE 7
166 #define SIM_TARGET_MAX_THREADS 64
167 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
169 /* From linux/sched.h. */
170 #define TARGET_CSIGNAL 0x000000ff
171 #define TARGET_CLONE_VM 0x00000100
172 #define TARGET_CLONE_FS 0x00000200
173 #define TARGET_CLONE_FILES 0x00000400
174 #define TARGET_CLONE_SIGHAND 0x00000800
175 #define TARGET_CLONE_PID 0x00001000
176 #define TARGET_CLONE_PTRACE 0x00002000
177 #define TARGET_CLONE_VFORK 0x00004000
178 #define TARGET_CLONE_PARENT 0x00008000
179 #define TARGET_CLONE_THREAD 0x00010000
180 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
182 /* From asm-cris/poll.h. */
183 #define TARGET_POLLIN 1
185 /* From asm-cris/signal.h. */
186 #define TARGET_SIG_BLOCK 0
187 #define TARGET_SIG_UNBLOCK 1
188 #define TARGET_SIG_SETMASK 2
190 #define TARGET_SIG_DFL 0
191 #define TARGET_SIG_IGN 1
192 #define TARGET_SIG_ERR ((USI)-1)
194 #define TARGET_SIGHUP 1
195 #define TARGET_SIGINT 2
196 #define TARGET_SIGQUIT 3
197 #define TARGET_SIGILL 4
198 #define TARGET_SIGTRAP 5
199 #define TARGET_SIGABRT 6
200 #define TARGET_SIGIOT 6
201 #define TARGET_SIGBUS 7
202 #define TARGET_SIGFPE 8
203 #define TARGET_SIGKILL 9
204 #define TARGET_SIGUSR1 10
205 #define TARGET_SIGSEGV 11
206 #define TARGET_SIGUSR2 12
207 #define TARGET_SIGPIPE 13
208 #define TARGET_SIGALRM 14
209 #define TARGET_SIGTERM 15
210 #define TARGET_SIGSTKFLT 16
211 #define TARGET_SIGCHLD 17
212 #define TARGET_SIGCONT 18
213 #define TARGET_SIGSTOP 19
214 #define TARGET_SIGTSTP 20
215 #define TARGET_SIGTTIN 21
216 #define TARGET_SIGTTOU 22
217 #define TARGET_SIGURG 23
218 #define TARGET_SIGXCPU 24
219 #define TARGET_SIGXFSZ 25
220 #define TARGET_SIGVTALRM 26
221 #define TARGET_SIGPROF 27
222 #define TARGET_SIGWINCH 28
223 #define TARGET_SIGIO 29
224 #define TARGET_SIGPOLL SIGIO
225 /* Actually commented out in the kernel header. */
226 #define TARGET_SIGLOST 29
227 #define TARGET_SIGPWR 30
228 #define TARGET_SIGSYS 31
230 /* From include/asm-cris/signal.h. */
231 #define TARGET_SA_NOCLDSTOP 0x00000001
232 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
233 #define TARGET_SA_SIGINFO 0x00000004
234 #define TARGET_SA_ONSTACK 0x08000000
235 #define TARGET_SA_RESTART 0x10000000
236 #define TARGET_SA_NODEFER 0x40000000
237 #define TARGET_SA_RESETHAND 0x80000000
238 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
239 #define TARGET_SA_RESTORER 0x04000000
241 /* From linux/wait.h. */
242 #define TARGET_WNOHANG 1
243 #define TARGET_WUNTRACED 2
244 #define TARGET___WNOTHREAD 0x20000000
245 #define TARGET___WALL 0x40000000
246 #define TARGET___WCLONE 0x80000000
248 /* From linux/limits.h. */
249 #define TARGET_PIPE_BUF 4096
252 #define TARGET_R_OK 4
253 #define TARGET_W_OK 2
254 #define TARGET_X_OK 1
255 #define TARGET_F_OK 0
257 static const char stat_map[] =
258 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
259 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
260 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
263 static const CB_TARGET_DEFS_MAP syscall_map[] =
265 { CB_SYS_open, TARGET_SYS_open },
266 { CB_SYS_close, TARGET_SYS_close },
267 { CB_SYS_read, TARGET_SYS_read },
268 { CB_SYS_write, TARGET_SYS_write },
269 { CB_SYS_lseek, TARGET_SYS_lseek },
270 { CB_SYS_unlink, TARGET_SYS_unlink },
271 { CB_SYS_getpid, TARGET_SYS_getpid },
272 { CB_SYS_fstat, TARGET_SYS_fstat64 },
273 { CB_SYS_lstat, TARGET_SYS_lstat64 },
274 { CB_SYS_stat, TARGET_SYS_stat64 },
275 { CB_SYS_pipe, TARGET_SYS_pipe },
276 { CB_SYS_rename, TARGET_SYS_rename },
277 { CB_SYS_truncate, TARGET_SYS_truncate },
278 { CB_SYS_ftruncate, TARGET_SYS_ftruncate },
282 /* An older, 32-bit-only stat mapping. */
283 static const char stat32_map[] =
284 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
285 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
286 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
288 /* Map for calls using the 32-bit struct stat. Primarily used by the
289 newlib Linux mapping. */
290 static const CB_TARGET_DEFS_MAP syscall_stat32_map[] =
292 { CB_SYS_fstat, TARGET_SYS_fstat },
293 { CB_SYS_stat, TARGET_SYS_stat },
297 /* Giving the true value for the running sim process will lead to
298 non-time-invariant behavior. */
299 #define TARGET_PID 42
301 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
302 we did, we'd still don't get a register number with the "16" offset. */
303 #define TARGET_SRP_REGNUM (16+11)
305 /* Extracted by applying
306 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
307 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
308 adjusting the synonyms. */
310 static const CB_TARGET_DEFS_MAP errno_map[] =
418 { ENAMETOOLONG, 36 },
577 { EDESTADDRREQ, 89 },
588 #ifdef EPROTONOSUPPORT
589 { EPROTONOSUPPORT, 93 },
591 #ifdef ESOCKTNOSUPPORT
592 { ESOCKTNOSUPPORT, 94 },
598 { EPFNOSUPPORT, 96 },
601 { EAFNOSUPPORT, 97 },
607 { EADDRNOTAVAIL, 99 },
613 { ENETUNREACH, 101 },
619 { ECONNABORTED, 103 },
637 { ETOOMANYREFS, 109 },
643 { ECONNREFUSED, 111 },
649 { EHOSTUNREACH, 113 },
655 { EINPROGRESS, 115 },
682 { EMEDIUMTYPE, 124 },
687 /* Extracted by applying
688 perl -ne 'if ($_ =~ /^#define/) { split;
689 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
690 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
691 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
692 installation and removing synonyms and unnecessary items. Don't
693 forget the end-marker. */
695 /* These we treat specially, as they're used in the fcntl F_GETFL
696 syscall. For consistency, open_map is also manually edited to use
698 #define TARGET_O_ACCMODE 0x3
699 #define TARGET_O_RDONLY 0x0
700 #define TARGET_O_WRONLY 0x1
702 static const CB_TARGET_DEFS_MAP open_map[] = {
704 { O_ACCMODE, TARGET_O_ACCMODE },
707 { O_RDONLY, TARGET_O_RDONLY },
710 { O_WRONLY, TARGET_O_WRONLY },
731 { O_NONBLOCK, 0x800 },
743 { O_DIRECT, 0x4000 },
746 { O_LARGEFILE, 0x8000 },
749 { O_DIRECTORY, 0x10000 },
752 { O_NOFOLLOW, 0x20000 },
757 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
758 static SIM_CPU *current_cpu_for_cb_callback;
760 static int syscall_read_mem (host_callback *, struct cb_syscall *,
761 unsigned long, char *, int);
762 static int syscall_write_mem (host_callback *, struct cb_syscall *,
763 unsigned long, const char *, int);
764 static USI create_map (SIM_DESC, struct cris_sim_mmapped_page **,
766 static USI unmap_pages (SIM_DESC, struct cris_sim_mmapped_page **,
768 static USI is_mapped (SIM_DESC, struct cris_sim_mmapped_page **,
770 static void dump_statistics (SIM_CPU *current_cpu);
771 static void make_first_thread (SIM_CPU *current_cpu);
773 /* Read/write functions for system call interface. */
776 syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED,
777 struct cb_syscall *sc,
778 unsigned long taddr, char *buf, int bytes)
780 SIM_DESC sd = (SIM_DESC) sc->p1;
781 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
783 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
787 syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED,
788 struct cb_syscall *sc,
789 unsigned long taddr, const char *buf, int bytes)
791 SIM_DESC sd = (SIM_DESC) sc->p1;
792 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
794 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
797 /* When we risk running self-modified code (as in trampolines), this is
798 called from special-case insns. The silicon CRIS CPU:s have enough
799 cache snooping implemented making this a simulator-only issue. Tests:
800 gcc.c-torture/execute/931002-1.c execution, -O3 -g
801 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
804 cris_flush_simulator_decode_cache (SIM_CPU *current_cpu,
805 USI pc ATTRIBUTE_UNUSED)
807 SIM_DESC sd = CPU_STATE (current_cpu);
810 if (USING_SCACHE_P (sd))
811 scache_flush_cpu (current_cpu);
815 /* Output statistics at the end of a run. */
817 dump_statistics (SIM_CPU *current_cpu)
819 SIM_DESC sd = CPU_STATE (current_cpu);
820 CRIS_MISC_PROFILE *profp
821 = CPU_CRIS_MISC_PROFILE (current_cpu);
822 unsigned64 total = profp->basic_cycle_count;
823 const char *textmsg = "Basic clock cycles, total @: %llu\n";
825 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
826 what's included in the "total" count only. */
827 switch (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
828 & FLAG_CRIS_MISC_PROFILE_ALL)
830 case FLAG_CRIS_MISC_PROFILE_SIMPLE:
833 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED | FLAG_CRIS_MISC_PROFILE_SIMPLE):
835 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
836 total += profp->unaligned_mem_dword_count;
839 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | FLAG_CRIS_MISC_PROFILE_SIMPLE):
840 textmsg = "Schedulable clock cycles, total @: %llu\n";
842 += (profp->memsrc_stall_count
843 + profp->memraw_stall_count
844 + profp->movemsrc_stall_count
845 + profp->movemdst_stall_count
846 + profp->mulsrc_stall_count
847 + profp->jumpsrc_stall_count
848 + profp->unaligned_mem_dword_count);
851 case FLAG_CRIS_MISC_PROFILE_ALL:
852 textmsg = "All accounted clock cycles, total @: %llu\n";
854 += (profp->memsrc_stall_count
855 + profp->memraw_stall_count
856 + profp->movemsrc_stall_count
857 + profp->movemdst_stall_count
858 + profp->movemaddr_stall_count
859 + profp->mulsrc_stall_count
860 + profp->jumpsrc_stall_count
861 + profp->branch_stall_count
862 + profp->jumptarget_stall_count
863 + profp->unaligned_mem_dword_count);
870 "Internal inconsistency at %s:%d",
872 sim_engine_halt (sd, current_cpu, NULL, 0,
873 sim_stopped, SIM_SIGILL);
876 /* Historically, these messages have gone to stderr, so we'll keep it
877 that way. It's also easier to then tell it from normal program
878 output. FIXME: Add redirect option like "run -e file". */
879 sim_io_eprintf (sd, textmsg, total);
881 /* For v32, unaligned_mem_dword_count should always be 0. For
882 v10, memsrc_stall_count should always be 0. */
883 sim_io_eprintf (sd, "Memory source stall cycles: %llu\n",
884 (unsigned long long) (profp->memsrc_stall_count
885 + profp->unaligned_mem_dword_count));
886 sim_io_eprintf (sd, "Memory read-after-write stall cycles: %llu\n",
887 (unsigned long long) profp->memraw_stall_count);
888 sim_io_eprintf (sd, "Movem source stall cycles: %llu\n",
889 (unsigned long long) profp->movemsrc_stall_count);
890 sim_io_eprintf (sd, "Movem destination stall cycles: %llu\n",
891 (unsigned long long) profp->movemdst_stall_count);
892 sim_io_eprintf (sd, "Movem address stall cycles: %llu\n",
893 (unsigned long long) profp->movemaddr_stall_count);
894 sim_io_eprintf (sd, "Multiplication source stall cycles: %llu\n",
895 (unsigned long long) profp->mulsrc_stall_count);
896 sim_io_eprintf (sd, "Jump source stall cycles: %llu\n",
897 (unsigned long long) profp->jumpsrc_stall_count);
898 sim_io_eprintf (sd, "Branch misprediction stall cycles: %llu\n",
899 (unsigned long long) profp->branch_stall_count);
900 sim_io_eprintf (sd, "Jump target stall cycles: %llu\n",
901 (unsigned long long) profp->jumptarget_stall_count);
904 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
905 Return 1 if a overlap detected, 0 otherwise. */
908 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED,
909 struct cris_sim_mmapped_page **rootp,
912 struct cris_sim_mmapped_page *mapp;
914 if (len == 0 || (len & 8191))
917 /* Iterate over the reverse-address sorted pages until we find a page in
918 or lower than the checked area. */
919 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
920 if (mapp->addr < addr + len && mapp->addr >= addr)
926 /* Check whether any part of [addr .. addr + len - 1] is *un*mapped.
927 Return 1 if the whole area is mapped, 0 otherwise. */
930 is_mapped_only (SIM_DESC sd ATTRIBUTE_UNUSED,
931 struct cris_sim_mmapped_page **rootp,
934 struct cris_sim_mmapped_page *mapp;
936 if (len == 0 || (len & 8191))
939 /* Iterate over the reverse-address sorted pages until we find a page
940 lower than the checked area. */
941 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
942 if (addr == mapp->addr && len == 8192)
944 else if (addr + len > mapp->addr)
950 /* Debug helper; to be run from gdb. */
953 cris_dump_map (SIM_CPU *current_cpu)
955 struct cris_sim_mmapped_page *mapp;
958 for (mapp = current_cpu->highest_mmapped_page,
959 start = mapp == NULL ? 0 : mapp->addr + 8192,
960 end = mapp == NULL ? 0 : mapp->addr + 8191;
964 if (mapp->addr != start - 8192)
966 sim_io_eprintf (CPU_STATE (current_cpu), "0x%x..0x%x\n", start, end);
967 end = mapp->addr + 8191;
973 if (current_cpu->highest_mmapped_page != NULL)
974 sim_io_eprintf (CPU_STATE (current_cpu), "0x%x..0x%x\n", start, end);
977 /* Create mmapped memory. */
980 create_map (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
983 struct cris_sim_mmapped_page *mapp;
984 struct cris_sim_mmapped_page **higher_prevp = rootp;
985 USI new_addr = 0x40000000;
990 new_addr = rootp[0]->addr + 8192;
997 /* Which is better: return an error for this, or just round it up? */
1000 /* Do a recursive call for each page in the request. */
1001 for (page_addr = new_addr; len != 0; page_addr += 8192, len -= 8192)
1002 if (create_map (sd, rootp, page_addr, 8192) >= (USI) -8191)
1009 mapp != NULL && mapp->addr > new_addr;
1011 higher_prevp = &mapp->prev;
1013 /* Allocate the new page, on the next higher page from the last one
1014 allocated, and link in the new descriptor before previous ones. */
1015 mapp = malloc (sizeof (*mapp));
1018 return (USI) -ENOMEM;
1020 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1024 mapp->addr = new_addr;
1025 mapp->prev = *higher_prevp;
1026 *higher_prevp = mapp;
1031 /* Unmap one or more pages. */
1034 unmap_pages (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
1037 struct cris_sim_mmapped_page *mapp;
1038 struct cris_sim_mmapped_page **higher_prevp = rootp;
1045 /* Which is better: return an error for this, or just round it up? */
1048 /* Loop backwards to make each call is O(1) over the number of pages
1049 allocated, if we're unmapping from the high end of the pages. */
1050 for (page_addr = addr + len - 8192;
1053 if (unmap_pages (sd, rootp, page_addr, 8192) != 0)
1059 for (mapp = *rootp; mapp != NULL && mapp->addr > addr; mapp = mapp->prev)
1060 higher_prevp = &mapp->prev;
1062 if (mapp == NULL || mapp->addr != addr)
1065 *higher_prevp = mapp->prev;
1066 sim_core_detach (sd, NULL, 0, 0, addr);
1071 /* The semantic code invokes this for illegal (unrecognized) instructions. */
1074 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
1076 SIM_DESC sd = CPU_STATE (current_cpu);
1078 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
1082 /* Handlers from the CGEN description that should not be called. */
1085 cris_bmod_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1086 UINT srcreg ATTRIBUTE_UNUSED,
1087 USI dstreg ATTRIBUTE_UNUSED)
1093 h_supr_set_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1094 UINT index ATTRIBUTE_UNUSED,
1095 USI page ATTRIBUTE_UNUSED,
1096 USI newval ATTRIBUTE_UNUSED)
1102 h_supr_get_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1103 UINT index ATTRIBUTE_UNUSED,
1104 USI page ATTRIBUTE_UNUSED)
1109 /* Swap one context for another. */
1112 schedule (SIM_CPU *current_cpu, int next)
1114 /* Need to mark context-switches in the trace output. */
1115 if ((CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1116 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE))
1117 cris_trace_printf (CPU_STATE (current_cpu), current_cpu,
1120 /* Copy the current context (if there is one) to its slot. */
1121 if (current_cpu->thread_data[current_cpu->threadno].cpu_context)
1122 memcpy (current_cpu->thread_data[current_cpu->threadno].cpu_context,
1123 ¤t_cpu->cpu_data_placeholder,
1124 current_cpu->thread_cpu_data_size);
1126 /* Copy the new context from its slot. */
1127 memcpy (¤t_cpu->cpu_data_placeholder,
1128 current_cpu->thread_data[next].cpu_context,
1129 current_cpu->thread_cpu_data_size);
1131 /* Update needed stuff to indicate the new context. */
1132 current_cpu->threadno = next;
1134 /* Handle pending signals. */
1135 if (current_cpu->thread_data[next].sigpending
1136 /* We don't run nested signal handlers. This means that pause(2)
1137 and sigsuspend(2) do not work in sighandlers, but that
1138 shouldn't be too hard a restriction. It also greatly
1139 simplifies the code. */
1140 && current_cpu->thread_data[next].cpu_context_atsignal == NULL)
1144 /* See if there's really a pending, non-blocked handler. We don't
1145 queue signals, so just use the first one in ascending order. */
1146 for (sig = 0; sig < 64; sig++)
1147 if (current_cpu->thread_data[next].sigdata[sig].pending
1148 && !current_cpu->thread_data[next].sigdata[sig].blocked)
1154 USI pc = sim_pc_get (current_cpu);
1156 /* It's simpler to save the CPU context inside the simulator
1157 than on the stack. */
1158 current_cpu->thread_data[next].cpu_context_atsignal
1160 ->make_thread_cpu_data) (current_cpu,
1161 current_cpu->thread_data[next]
1164 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1165 sp = bfd_getl32 (regbuf);
1167 /* Make sure we have an aligned stack. */
1170 /* Make room for the signal frame, aligned. FIXME: Check that
1171 the memory exists, map it in if absent. (BTW, should also
1172 implement on-access automatic stack allocation). */
1175 /* This isn't the same signal frame as the kernel uses, because
1176 we don't want to bother getting all registers on and off the
1179 /* First, we store the currently blocked signals. */
1181 for (i = 0; i < 32; i++)
1183 |= current_cpu->thread_data[next].sigdata[i + 1].blocked << i;
1184 sim_core_write_aligned_4 (current_cpu, pc, 0, sp, blocked);
1186 for (i = 0; i < 31; i++)
1188 |= current_cpu->thread_data[next].sigdata[i + 33].blocked << i;
1189 sim_core_write_aligned_4 (current_cpu, pc, 0, sp + 4, blocked);
1191 /* Then, the actual instructions. This is CPU-specific, but we
1192 use instructions from the common subset for v10 and v32 which
1193 should be safe for the time being but could be parametrized
1195 /* MOVU.W [PC+],R9. */
1196 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 8, 0x9c5f);
1197 /* .WORD TARGET_SYS_sigreturn. */
1198 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 10,
1199 TARGET_SYS_sigreturn);
1201 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 12, 0xe93d);
1203 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1204 instruction. Still, it doesn't matter because v10 has no
1205 delay slot for BREAK so it will not be executed). */
1206 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 16, 0x05b0);
1208 /* Modify registers to hold the right values for the sighandler
1209 context: updated stackpointer and return address pointing to
1210 the sigreturn stub. */
1211 bfd_putl32 (sp, regbuf);
1212 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1213 bfd_putl32 (sp + 8, regbuf);
1214 (*CPU_REG_STORE (current_cpu)) (current_cpu, TARGET_SRP_REGNUM,
1217 current_cpu->thread_data[next].sigdata[sig].pending = 0;
1219 /* Block this signal (for the duration of the sighandler). */
1220 current_cpu->thread_data[next].sigdata[sig].blocked = 1;
1222 sim_pc_set (current_cpu, current_cpu->sighandler[sig]);
1223 bfd_putl32 (sig, regbuf);
1224 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10,
1227 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1228 needed all this for, specifies a SA_SIGINFO call but treats it
1229 like an ordinary sighandler; only the signal number argument is
1230 inspected. To make future need to implement SA_SIGINFO
1231 correctly possible, we set the siginfo argument register to a
1232 magic (hopefully non-address) number. (NB: then, you should
1233 just need to pass the siginfo argument; it seems you probably
1234 don't need to implement the specific rt_sigreturn.) */
1235 bfd_putl32 (0xbad5161f, regbuf);
1236 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R11,
1239 /* The third argument is unused and the kernel sets it to 0. */
1240 bfd_putl32 (0, regbuf);
1241 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R12,
1246 /* No, there actually was no pending signal for this thread. Reset
1248 current_cpu->thread_data[next].sigpending = 0;
1252 /* Reschedule the simplest possible way until something else is absolutely
1254 - A. Find the next process (round-robin) that doesn't have at_syscall
1256 - B. If there is none, just run the next process, round-robin.
1257 - Clear at_syscall for the current process. */
1260 reschedule (SIM_CPU *current_cpu)
1264 /* Iterate over all thread slots, because after a few thread creations
1265 and exits, we don't know where the live ones are. */
1266 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1267 i != current_cpu->threadno;
1268 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1269 if (current_cpu->thread_data[i].cpu_context
1270 && current_cpu->thread_data[i].at_syscall == 0)
1272 schedule (current_cpu, i);
1276 /* Pick any next live thread. */
1277 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1278 i != current_cpu->threadno;
1279 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1280 if (current_cpu->thread_data[i].cpu_context)
1282 schedule (current_cpu, i);
1286 /* More than one live thread, but we couldn't find the next one? */
1290 /* Set up everything to receive (or IGN) an incoming signal to the
1294 deliver_signal (SIM_CPU *current_cpu, int sig, unsigned int pid)
1297 USI pc = sim_pc_get (current_cpu);
1299 /* Find the thread index of the pid. */
1300 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1301 /* Apparently it's ok to send signals to zombies (so a check for
1302 current_cpu->thread_data[i].cpu_context != NULL would be
1304 if (current_cpu->thread_data[i].threadid == pid - TARGET_PID)
1307 switch (current_cpu->sighandler[sig])
1309 case TARGET_SIG_DFL:
1312 /* The following according to the glibc
1313 documentation. (The kernel code has non-obvious
1314 execution paths.) */
1317 case TARGET_SIGSEGV:
1319 case TARGET_SIGABRT:
1320 case TARGET_SIGTRAP:
1323 case TARGET_SIGTERM:
1325 case TARGET_SIGQUIT:
1326 case TARGET_SIGKILL:
1329 case TARGET_SIGALRM:
1330 case TARGET_SIGVTALRM:
1331 case TARGET_SIGPROF:
1332 case TARGET_SIGSTOP:
1334 case TARGET_SIGPIPE:
1335 case TARGET_SIGLOST:
1336 case TARGET_SIGXCPU:
1337 case TARGET_SIGXFSZ:
1338 case TARGET_SIGUSR1:
1339 case TARGET_SIGUSR2:
1340 sim_io_eprintf (CPU_STATE (current_cpu),
1341 "Exiting pid %d due to signal %d\n",
1343 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1344 NULL, pc, sim_stopped,
1345 sig == TARGET_SIGABRT
1346 ? SIM_SIGABRT : SIM_SIGILL);
1349 /* The default for all other signals is to be ignored. */
1354 case TARGET_SIG_IGN:
1357 case TARGET_SIGKILL:
1358 case TARGET_SIGSTOP:
1359 /* Can't ignore these signals. */
1360 sim_io_eprintf (CPU_STATE (current_cpu),
1361 "Exiting pid %d due to signal %d\n",
1363 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1364 NULL, pc, sim_stopped, SIM_SIGILL);
1373 /* Mark the signal as pending, making schedule () check
1374 closer. The signal will be handled when the thread is
1375 scheduled and the signal is unblocked. */
1376 current_cpu->thread_data[i].sigdata[sig].pending = 1;
1377 current_cpu->thread_data[i].sigpending = 1;
1382 sim_io_eprintf (CPU_STATE (current_cpu),
1383 "Unimplemented signal: %d\n", sig);
1384 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
1385 sim_stopped, SIM_SIGILL);
1390 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu)),
1394 /* Make the vector and the first item, the main thread. */
1397 make_first_thread (SIM_CPU *current_cpu)
1399 current_cpu->thread_data
1401 SIM_TARGET_MAX_THREADS
1402 * sizeof (current_cpu->thread_data[0]));
1403 current_cpu->thread_data[0].cpu_context
1404 = (*current_cpu->make_thread_cpu_data) (current_cpu,
1406 ->cpu_data_placeholder);
1407 current_cpu->thread_data[0].parent_threadid = -1;
1409 /* For good measure. */
1410 if (TARGET_SIG_DFL != 0)
1414 /* Handle unknown system calls. Returns (if it does) the syscall
1418 cris_unknown_syscall (SIM_CPU *current_cpu, USI pc, char *s, ...)
1420 SIM_DESC sd = CPU_STATE (current_cpu);
1421 host_callback *cb = STATE_CALLBACK (sd);
1423 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP
1424 || cris_unknown_syscall_action == CRIS_USYSC_MSG_ENOSYS)
1429 sim_io_evprintf (sd, s, ap);
1432 if (cris_unknown_syscall_action == CRIS_USYSC_MSG_STOP)
1433 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1436 return -cb_host_to_target_errno (cb, ENOSYS);
1439 /* Main function: the handler of the "break 13" syscall insn. */
1442 cris_break_13_handler (SIM_CPU *current_cpu, USI callnum, USI arg1,
1443 USI arg2, USI arg3, USI arg4, USI arg5, USI arg6,
1447 SIM_DESC sd = CPU_STATE (current_cpu);
1448 host_callback *cb = STATE_CALLBACK (sd);
1450 int threadno = current_cpu->threadno;
1452 current_cpu->syscalls++;
1454 CB_SYSCALL_INIT (&s);
1460 if (callnum == TARGET_SYS_exit_group
1461 || (callnum == TARGET_SYS_exit && current_cpu->m1threads == 0))
1463 if (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1464 & FLAG_CRIS_MISC_PROFILE_ALL)
1465 dump_statistics (current_cpu);
1466 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
1470 s.p2 = (PTR) current_cpu;
1471 s.read_mem = syscall_read_mem;
1472 s.write_mem = syscall_write_mem;
1474 current_cpu_for_cb_callback = current_cpu;
1476 if (cb_syscall (cb, &s) != CB_RC_OK)
1479 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
1481 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1484 retval = s.result == -1 ? -s.errcode : s.result;
1486 if (s.errcode != 0 && s.errcode == cb_host_to_target_errno (cb, ENOSYS))
1488 /* If the generic simulator call said ENOSYS, then let's try the
1489 ones we know ourselves.
1491 The convention is to provide *very limited* functionality on an
1492 as-needed basis, only what's covered by the test-suite, tests
1493 added when functionality changes and abort with a descriptive
1494 message for *everything* else. Where there's no test-case, we
1499 /* It's a pretty safe bet that the "old setup() system call"
1500 number will not be re-used; we can't say the same for higher
1501 numbers. We treat this simulator-generated call as "wait
1502 forever"; we re-run this insn. The wait is ended by a
1503 callback. Sanity check that this is the reason we got
1505 if (current_cpu->thread_data == NULL
1506 || (current_cpu->thread_data[threadno].pipe_write_fd == 0))
1507 goto unimplemented_syscall;
1509 sim_pc_set (current_cpu, pc);
1513 case TARGET_SYS_fcntl64:
1514 case TARGET_SYS_fcntl:
1519 Glibc checks stdin, stdout and stderr fd:s for
1520 close-on-exec security sanity. We just need to provide a
1521 OK return value. If we really need to have a
1522 close-on-exec flag true, we could just do a real fcntl
1528 /* F_SETFD. Just ignore attempts to set the close-on-exec
1534 /* F_GETFL. Check for the special case for open+fdopen. */
1535 if (current_cpu->last_syscall == TARGET_SYS_open
1536 && arg1 == current_cpu->last_open_fd)
1538 retval = current_cpu->last_open_flags & TARGET_O_ACCMODE;
1543 /* Because we can't freopen fd:s 0, 1, 2 to mean
1544 something else than stdin, stdout and stderr
1545 (sim/common/syscall.c:cb_syscall special cases fd
1546 0, 1 and 2), we know what flags that we can
1547 sanely return for these fd:s. */
1548 retval = TARGET_O_RDONLY;
1551 else if (arg1 == 1 || arg1 == 2)
1553 retval = TARGET_O_WRONLY;
1558 /* Nothing else is implemented. */
1560 = cris_unknown_syscall (current_cpu, pc,
1561 "Unimplemented %s syscall "
1562 "(fd: 0x%lx: cmd: 0x%lx arg: "
1564 callnum == TARGET_SYS_fcntl
1565 ? "fcntl" : "fcntl64",
1566 (unsigned long) (USI) arg1,
1567 (unsigned long) (USI) arg2,
1568 (unsigned long) (USI) arg3);
1573 case TARGET_SYS_uname:
1575 /* Fill in a few constants to appease glibc. */
1576 static char sim_utsname[6][65] =
1582 "cris", /* Overwritten below. */
1586 /* Having the hardware type in Linux equal to the bfd
1587 printable name is deliberate: if you make config.guess
1588 work on your Linux-type system the usual way, it
1589 probably will; either the bfd printable_name or the
1590 ambiguous arch_name. */
1591 strcpy (sim_utsname[4], STATE_ARCHITECTURE (sd)->printable_name);
1593 if ((s.write_mem) (cb, &s, arg1, (const char *) sim_utsname,
1594 sizeof (sim_utsname))
1595 != sizeof (sim_utsname))
1596 retval = -cb_host_to_target_errno (cb, EFAULT);
1602 case TARGET_SYS_geteuid32:
1603 /* We tell the truth with these. Maybe we shouldn't, but it
1604 should match the "stat" information. */
1605 retval = geteuid ();
1608 case TARGET_SYS_getuid32:
1612 case TARGET_SYS_getegid32:
1613 retval = getegid ();
1616 case TARGET_SYS_getgid32:
1620 case TARGET_SYS_brk:
1621 /* Most often, we just return the argument, like the Linux
1626 retval = current_cpu->endbrk;
1627 else if (arg1 <= current_cpu->endmem)
1628 current_cpu->endbrk = arg1;
1631 USI new_end = (arg1 + 8191) & ~8191;
1633 /* If the simulator wants to brk more than a certain very
1634 large amount, something is wrong. FIXME: Return an error
1635 or abort? Have command-line selectable? */
1636 if (new_end - current_cpu->endmem > SIM_MAX_ALLOC_CHUNK)
1638 current_cpu->endbrk = current_cpu->endmem;
1639 retval = current_cpu->endmem;
1643 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1644 current_cpu->endmem,
1645 new_end - current_cpu->endmem,
1647 current_cpu->endbrk = arg1;
1648 current_cpu->endmem = new_end;
1652 case TARGET_SYS_getpid:
1653 /* Correct until CLONE_THREAD is implemented. */
1654 retval = current_cpu->thread_data == NULL
1656 : TARGET_PID + current_cpu->thread_data[threadno].threadid;
1659 case TARGET_SYS_getppid:
1660 /* Correct until CLONE_THREAD is implemented. */
1661 retval = current_cpu->thread_data == NULL
1664 + current_cpu->thread_data[threadno].parent_threadid);
1667 case TARGET_SYS_mmap2:
1676 /* At 2.6.27, Linux (many (all?) ports, in the mmap2 syscalls)
1677 still masked away this bit, so let's just ignore
1679 flags &= ~TARGET_MAP_DENYWRITE;
1681 /* If the simulator wants to mmap more than the very large
1682 limit, something is wrong. FIXME: Return an error or
1683 abort? Have command-line selectable? */
1684 if (len > SIM_MAX_ALLOC_CHUNK)
1686 retval = -cb_host_to_target_errno (cb, ENOMEM);
1690 if ((prot != (TARGET_PROT_READ | TARGET_PROT_WRITE)
1692 != (TARGET_PROT_READ
1694 | TARGET_PROT_EXEC))
1695 && (prot != (TARGET_PROT_READ | TARGET_PROT_EXEC))
1696 && prot != TARGET_PROT_READ)
1697 || (flags != (TARGET_MAP_ANONYMOUS | TARGET_MAP_PRIVATE)
1698 && flags != TARGET_MAP_PRIVATE
1699 && flags != (TARGET_MAP_ANONYMOUS
1700 | TARGET_MAP_PRIVATE | TARGET_MAP_FIXED)
1701 && flags != (TARGET_MAP_PRIVATE | TARGET_MAP_FIXED)
1702 && flags != TARGET_MAP_SHARED)
1704 && prot != TARGET_PROT_READ
1705 && prot != (TARGET_PROT_READ | TARGET_PROT_EXEC)
1706 && prot != (TARGET_PROT_READ | TARGET_PROT_WRITE))
1707 || (fd == (USI) -1 && pgoff != 0)
1708 || (fd != (USI) -1 && (flags & TARGET_MAP_ANONYMOUS))
1709 || ((flags & TARGET_MAP_FIXED) == 0
1710 && is_mapped (sd, ¤t_cpu->highest_mmapped_page,
1711 addr, (len + 8191) & ~8191)))
1714 = cris_unknown_syscall (current_cpu, pc,
1715 "Unimplemented mmap2 call "
1716 "(0x%lx, 0x%lx, 0x%lx, "
1717 "0x%lx, 0x%lx, 0x%lx)\n",
1718 (unsigned long) arg1,
1719 (unsigned long) arg2,
1720 (unsigned long) arg3,
1721 (unsigned long) arg4,
1722 (unsigned long) arg5,
1723 (unsigned long) arg6);
1726 else if (fd != (USI) -1)
1733 /* A non-aligned argument is allowed for files. */
1734 USI newlen = (len + 8191) & ~8191;
1736 /* We only support read, read|exec, and read|write,
1737 which we should already have checked. Check again
1739 if (prot != TARGET_PROT_READ
1740 && prot != (TARGET_PROT_READ | TARGET_PROT_EXEC)
1741 && prot != (TARGET_PROT_READ | TARGET_PROT_WRITE))
1744 if ((flags & TARGET_MAP_FIXED)
1745 && unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1750 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1753 if (newaddr >= (USI) -8191)
1756 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1760 /* We were asked for MAP_FIXED, but couldn't. */
1761 if ((flags & TARGET_MAP_FIXED) && newaddr != addr)
1764 unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1766 retval = -cb_host_to_target_errno (cb, EINVAL);
1770 /* Find the current position in the file. */
1771 s.func = TARGET_SYS_lseek;
1775 if (cb_syscall (cb, &s) != CB_RC_OK)
1782 /* Move to the correct offset in the file. */
1783 s.func = TARGET_SYS_lseek;
1785 s.arg2 = pgoff*8192;
1787 if (cb_syscall (cb, &s) != CB_RC_OK)
1793 /* Use the standard read callback to read in "len"
1795 s.func = TARGET_SYS_read;
1799 if (cb_syscall (cb, &s) != CB_RC_OK)
1802 if ((USI) s.result != len)
1805 /* After reading, we need to go back to the previous
1806 position in the file. */
1807 s.func = TARGET_SYS_lseek;
1811 if (cb_syscall (cb, &s) != CB_RC_OK)
1813 if (pos != (USI) s.result)
1820 USI newlen = (len + 8191) & ~8191;
1823 if ((flags & TARGET_MAP_FIXED)
1824 && unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1828 newaddr = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1831 if (newaddr >= (USI) -8191)
1832 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1836 if ((flags & TARGET_MAP_FIXED) && newaddr != addr)
1839 unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1841 retval = -cb_host_to_target_errno (cb, EINVAL);
1848 case TARGET_SYS_mprotect:
1850 /* We only cover the case of linuxthreads mprotecting out
1851 its stack guard page and of dynamic loading mprotecting
1852 away the data (for some reason the whole library, then
1853 mprotects away the data part and mmap-FIX:es it again. */
1858 if (prot != TARGET_PROT_NONE
1859 || !is_mapped_only (sd, ¤t_cpu->highest_mmapped_page,
1860 addr, (len + 8191) & ~8191))
1863 = cris_unknown_syscall (current_cpu, pc,
1864 "Unimplemented mprotect call "
1865 "(0x%lx, 0x%lx, 0x%lx)\n",
1866 (unsigned long) arg1,
1867 (unsigned long) arg2,
1868 (unsigned long) arg3);
1872 /* Just ignore this. We could make this equal to munmap,
1873 but then we'd have to make sure no anon mmaps gets this
1874 address before a subsequent MAP_FIXED mmap intended to
1880 case TARGET_SYS_ioctl:
1882 /* We support only a very limited functionality: checking
1883 stdout with TCGETS to perform the isatty function. The
1884 TCGETS ioctl isn't actually performed or the result used by
1885 an isatty () caller in a "hello, world" program; only the
1886 return value is then used. Maybe we shouldn't care about
1887 the environment of the simulator regarding isatty, but
1888 that's been working before, in the xsim simulator. */
1889 if (arg2 == TARGET_TCGETS && arg1 == 1)
1890 retval = isatty (1) ? 0 : cb_host_to_target_errno (cb, EINVAL);
1892 retval = -cb_host_to_target_errno (cb, EINVAL);
1896 case TARGET_SYS_munmap:
1901 = unmap_pages (sd, ¤t_cpu->highest_mmapped_page, addr,
1903 retval = result != 0 ? -cb_host_to_target_errno (cb, result) : 0;
1907 case TARGET_SYS_wait4:
1915 /* FIXME: We're not properly implementing __WCLONE, and we
1916 don't really need the special casing so we might as well
1917 make this general. */
1918 if ((!(pid == (USI) -1
1919 && options == (TARGET___WCLONE | TARGET_WNOHANG)
1922 && (options == TARGET___WCLONE
1923 || options == TARGET___WALL)))
1925 || current_cpu->thread_data == NULL)
1928 = cris_unknown_syscall (current_cpu, pc,
1929 "Unimplemented wait4 call "
1930 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1931 (unsigned long) arg1,
1932 (unsigned long) arg2,
1933 (unsigned long) arg3,
1934 (unsigned long) arg4);
1938 if (pid == (USI) -1)
1939 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1941 if (current_cpu->thread_data[threadno].threadid
1942 == current_cpu->thread_data[i].parent_threadid
1943 && current_cpu->thread_data[i].threadid != 0
1944 && current_cpu->thread_data[i].cpu_context == NULL)
1946 /* A zombied child. Get the exit value and clear the
1947 zombied entry so it will be reused. */
1948 sim_core_write_unaligned_4 (current_cpu, pc, 0, saddr,
1950 ->thread_data[i].exitval);
1952 = current_cpu->thread_data[i].threadid + TARGET_PID;
1953 memset (¤t_cpu->thread_data[i], 0,
1954 sizeof (current_cpu->thread_data[i]));
1960 /* We're waiting for a specific PID. If we don't find
1961 it zombied on this run, rerun the syscall. */
1962 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1963 if (pid == current_cpu->thread_data[i].threadid + TARGET_PID
1964 && current_cpu->thread_data[i].cpu_context == NULL)
1967 /* Get the exit value if the caller wants it. */
1968 sim_core_write_unaligned_4 (current_cpu, pc, 0,
1975 = current_cpu->thread_data[i].threadid + TARGET_PID;
1976 memset (¤t_cpu->thread_data[i], 0,
1977 sizeof (current_cpu->thread_data[i]));
1982 sim_pc_set (current_cpu, pc);
1985 retval = -cb_host_to_target_errno (cb, ECHILD);
1990 case TARGET_SYS_rt_sigaction:
1998 __sighandler_t sa_handler;
1999 unsigned long sa_flags;
2000 void (*sa_restorer)(void);
2006 sim_core_write_unaligned_4 (current_cpu, pc, 0, old_sa + 0,
2007 current_cpu->sighandler[signum]);
2008 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 4, 0);
2009 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 8, 0);
2011 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
2012 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 12, 0);
2013 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 16, 0);
2017 USI target_sa_handler
2018 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa);
2020 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 4);
2021 USI target_sa_restorer
2022 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 8);
2023 USI target_sa_mask_low
2024 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 12);
2025 USI target_sa_mask_high
2026 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 16);
2028 /* We won't interrupt a syscall so we won't restart it,
2029 but a signal(2) call ends up syscalling rt_sigaction
2030 with this flag, so we have to handle it. The
2031 sa_restorer field contains garbage when not
2032 TARGET_SA_RESTORER, so don't look at it. For the
2033 time being, we don't nest sighandlers, so we
2034 ignore the sa_mask, which simplifies things. */
2035 if ((target_sa_flags != 0
2036 && target_sa_flags != TARGET_SA_RESTART
2037 && target_sa_flags != (TARGET_SA_RESTART|TARGET_SA_SIGINFO))
2038 || target_sa_handler == 0)
2041 = cris_unknown_syscall (current_cpu, pc,
2042 "Unimplemented rt_sigaction "
2045 "[0x%x, 0x%x, 0x%x, "
2046 "{0x%x, 0x%x}], 0x%lx)\n",
2047 (unsigned long) arg1,
2048 (unsigned long) arg2,
2053 target_sa_mask_high,
2054 (unsigned long) arg3);
2058 current_cpu->sighandler[signum] = target_sa_handler;
2060 /* Because we may have unblocked signals, one may now be
2061 pending, if there are threads, that is. */
2062 if (current_cpu->thread_data)
2063 current_cpu->thread_data[threadno].sigpending = 1;
2069 case TARGET_SYS_mremap:
2075 USI new_addr = arg5;
2078 if (new_len == old_len)
2079 /* The program and/or library is possibly confused but
2080 this is a valid call. Happens with ipps-1.40 on file
2083 else if (new_len < old_len)
2085 /* Shrinking is easy. */
2086 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
2087 addr + new_len, old_len - new_len) != 0)
2088 retval = -cb_host_to_target_errno (cb, EINVAL);
2092 else if (! is_mapped (sd, ¤t_cpu->highest_mmapped_page,
2093 addr + old_len, new_len - old_len))
2095 /* If the extension isn't mapped, we can just add it. */
2097 = create_map (sd, ¤t_cpu->highest_mmapped_page,
2098 addr + old_len, new_len - old_len);
2100 if (mapped_addr > (USI) -8192)
2101 retval = -cb_host_to_target_errno (cb, -(SI) mapped_addr);
2105 else if (flags & TARGET_MREMAP_MAYMOVE)
2107 /* Create a whole new map and copy the contents
2108 block-by-block there. We ignore the new_addr argument
2111 USI prev_addr = addr;
2112 USI prev_len = old_len;
2115 = create_map (sd, ¤t_cpu->highest_mmapped_page,
2118 if (mapped_addr > (USI) -8192)
2120 retval = -cb_host_to_target_errno (cb, -(SI) new_addr);
2124 retval = mapped_addr;
2127 old_len -= 8192, mapped_addr += 8192, addr += 8192)
2129 if (sim_core_read_buffer (sd, current_cpu, read_map, buf,
2131 || sim_core_write_buffer (sd, current_cpu, 0, buf,
2132 mapped_addr, 8192) != 8192)
2136 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
2137 prev_addr, prev_len) != 0)
2141 retval = -cb_host_to_target_errno (cb, -ENOMEM);
2145 case TARGET_SYS_poll:
2147 int npollfds = arg2;
2163 /* Check that this is the expected poll call from
2164 linuxthreads/manager.c; we don't support anything else.
2165 Remember, fd == 0 isn't supported. */
2167 || ((fd = sim_core_read_unaligned_4 (current_cpu, pc,
2169 || ((events = sim_core_read_unaligned_2 (current_cpu, pc,
2172 || ((cb->fstat) (cb, fd, &buf) != 0
2173 || (buf.st_mode & S_IFIFO) == 0)
2174 || current_cpu->thread_data == NULL)
2177 = cris_unknown_syscall (current_cpu, pc,
2178 "Unimplemented poll syscall "
2179 "(0x%lx: [0x%x, 0x%x, x], "
2181 (unsigned long) arg1, fd, events,
2182 (unsigned long) arg2,
2183 (unsigned long) arg3);
2189 /* Iterate over threads; find a marker that a writer is
2190 sleeping, waiting for a reader. */
2191 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2192 if (current_cpu->thread_data[i].cpu_context != NULL
2193 && current_cpu->thread_data[i].pipe_read_fd == fd)
2195 revents = TARGET_POLLIN;
2200 /* Timeout decreases with whatever time passed between the
2201 last syscall and this. That's not exactly right for the
2202 first call, but it's close enough that it isn't
2203 worthwhile to complicate matters by making that a special
2206 -= (TARGET_TIME_MS (current_cpu)
2207 - (current_cpu->thread_data[threadno].last_execution));
2209 /* Arrange to repeat this syscall until timeout or event,
2210 decreasing timeout at each iteration. */
2211 if (timeout > 0 && revents == 0)
2213 bfd_byte timeout_buf[4];
2215 bfd_putl32 (timeout, timeout_buf);
2216 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2217 H_GR_R12, timeout_buf, 4);
2218 sim_pc_set (current_cpu, pc);
2223 sim_core_write_unaligned_2 (current_cpu, pc, 0, ufds + 4 + 2,
2228 case TARGET_SYS_time:
2230 retval = (int) (*cb->time) (cb, 0L);
2232 /* At time of this writing, CB_SYSCALL_time doesn't do the
2233 part of setting *arg1 to the return value. */
2235 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, retval);
2239 case TARGET_SYS_gettimeofday:
2242 USI ts = TARGET_TIME (current_cpu);
2243 USI tms = TARGET_TIME_MS (current_cpu);
2245 /* First dword is seconds since TARGET_EPOCH. */
2246 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, ts);
2248 /* Second dword is microseconds. */
2249 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1 + 4,
2250 (tms % 1000) * 1000);
2254 /* Time-zone info is always cleared. */
2255 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, 0);
2256 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, 0);
2261 case TARGET_SYS_llseek:
2263 /* If it fits, tweak parameters to fit the "generic" 32-bit
2264 lseek and use that. */
2272 if (!((offs_hi == 0 && offs_lo >= 0)
2273 || (offs_hi == -1 && offs_lo < 0)))
2276 = cris_unknown_syscall (current_cpu, pc,
2277 "Unimplemented llseek offset,"
2278 " fd %d: 0x%x:0x%x\n",
2279 fd, (unsigned) arg2,
2284 s.func = TARGET_SYS_lseek;
2287 if (cb_syscall (cb, &s) != CB_RC_OK)
2289 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
2291 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2294 retval = -s.errcode;
2297 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp,
2299 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp + 4,
2300 s.result < 0 ? -1 : 0);
2305 /* ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
2308 void *iov_base; Starting address
2309 size_t iov_len; Number of bytes to transfer
2311 case TARGET_SYS_writev:
2319 /* We'll ignore strict error-handling and just do multiple write calls. */
2320 for (i = 0; i < iovcnt; i++)
2324 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2327 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2330 s.func = TARGET_SYS_write;
2335 if (cb_syscall (cb, &s) != CB_RC_OK)
2337 sysret = s.result == -1 ? -s.errcode : s.result;
2339 if (sysret != iov_len)
2354 /* This one does have a generic callback function, but at the time
2355 of this writing, cb_syscall does not have code for it, and we
2356 need target-specific code for the threads implementation
2358 case TARGET_SYS_kill:
2365 /* At kill(2), glibc sets signal masks such that the thread
2366 machinery is initialized. Still, there is and was only
2368 if (current_cpu->max_threadid == 0)
2370 if (pid != TARGET_PID)
2372 retval = -cb_host_to_target_errno (cb, EPERM);
2376 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2377 if (sig == TARGET_SIGABRT)
2378 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2379 the end-point for failing GCC test-cases. */
2380 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2384 sim_io_eprintf (sd, "Unimplemented signal: %d\n", sig);
2385 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2389 /* This will not be reached. */
2393 retval = deliver_signal (current_cpu, sig, pid);
2397 case TARGET_SYS_rt_sigprocmask:
2404 if (how != TARGET_SIG_BLOCK
2405 && how != TARGET_SIG_SETMASK
2406 && how != TARGET_SIG_UNBLOCK)
2409 = cris_unknown_syscall (current_cpu, pc,
2410 "Unimplemented rt_sigprocmask "
2411 "syscall (0x%x, 0x%x, 0x%x)\n",
2419 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2422 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2425 /* The sigmask is kept in the per-thread data, so we may
2426 need to create the first one. */
2427 if (current_cpu->thread_data == NULL)
2428 make_first_thread (current_cpu);
2430 if (how == TARGET_SIG_SETMASK)
2431 for (i = 0; i < 64; i++)
2432 current_cpu->thread_data[threadno].sigdata[i].blocked = 0;
2434 for (i = 0; i < 32; i++)
2435 if ((set_low & (1 << i)))
2436 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2437 = (how != TARGET_SIG_UNBLOCK);
2439 for (i = 0; i < 31; i++)
2440 if ((set_high & (1 << i)))
2441 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2442 = (how != TARGET_SIG_UNBLOCK);
2444 /* The mask changed, so a signal may be unblocked for
2446 current_cpu->thread_data[threadno].sigpending = 1;
2454 for (i = 0; i < 32; i++)
2455 if (current_cpu->thread_data[threadno]
2456 .sigdata[i + 1].blocked)
2458 for (i = 0; i < 31; i++)
2459 if (current_cpu->thread_data[threadno]
2460 .sigdata[i + 33].blocked)
2463 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 0, set_low);
2464 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 4, set_high);
2471 case TARGET_SYS_sigreturn:
2475 int was_sigsuspended;
2477 if (current_cpu->thread_data == NULL
2478 /* The CPU context is saved with the simulator data, not
2479 on the stack as in the real world. */
2480 || (current_cpu->thread_data[threadno].cpu_context_atsignal
2484 = cris_unknown_syscall (current_cpu, pc,
2485 "Invalid sigreturn syscall: "
2486 "no signal handler active "
2487 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, "
2489 (unsigned long) arg1,
2490 (unsigned long) arg2,
2491 (unsigned long) arg3,
2492 (unsigned long) arg4,
2493 (unsigned long) arg5,
2494 (unsigned long) arg6);
2499 = current_cpu->thread_data[threadno].sigsuspended;
2501 /* Restore the sigmask, either from the stack copy made when
2502 the sighandler was called, or from the saved state
2503 specifically for sigsuspend(2). */
2504 if (was_sigsuspended)
2506 current_cpu->thread_data[threadno].sigsuspended = 0;
2507 for (i = 0; i < 64; i++)
2508 current_cpu->thread_data[threadno].sigdata[i].blocked
2509 = current_cpu->thread_data[threadno]
2510 .sigdata[i].blocked_suspendsave;
2518 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2519 H_GR_SP, regbuf, 4);
2520 sp = bfd_getl32 (regbuf);
2522 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp);
2524 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp + 4);
2526 for (i = 0; i < 32; i++)
2527 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2528 = (set_low & (1 << i)) != 0;
2529 for (i = 0; i < 31; i++)
2530 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2531 = (set_high & (1 << i)) != 0;
2534 /* The mask changed, so a signal may be unblocked for
2536 current_cpu->thread_data[threadno].sigpending = 1;
2538 memcpy (¤t_cpu->cpu_data_placeholder,
2539 current_cpu->thread_data[threadno].cpu_context_atsignal,
2540 current_cpu->thread_cpu_data_size);
2541 free (current_cpu->thread_data[threadno].cpu_context_atsignal);
2542 current_cpu->thread_data[threadno].cpu_context_atsignal = NULL;
2544 /* The return value must come from the saved R10. */
2545 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, regbuf, 4);
2546 retval = bfd_getl32 (regbuf);
2548 /* We must also break the "sigsuspension loop". */
2549 if (was_sigsuspended)
2550 sim_pc_set (current_cpu, sim_pc_get (current_cpu) + 2);
2554 case TARGET_SYS_rt_sigsuspend:
2562 = cris_unknown_syscall (current_cpu, pc,
2563 "Unimplemented rt_sigsuspend syscall"
2564 " arguments (0x%lx, 0x%lx)\n",
2565 (unsigned long) arg1,
2566 (unsigned long) arg2);
2570 /* Don't change the signal mask if we're already in
2571 sigsuspend state (i.e. this syscall is a rerun). */
2572 else if (!current_cpu->thread_data[threadno].sigsuspended)
2575 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2578 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2582 /* Save the current sigmask and insert the user-supplied
2584 for (i = 0; i < 32; i++)
2586 current_cpu->thread_data[threadno]
2587 .sigdata[i + 1].blocked_suspendsave
2588 = current_cpu->thread_data[threadno]
2589 .sigdata[i + 1].blocked;
2591 current_cpu->thread_data[threadno]
2592 .sigdata[i + 1].blocked = (set_low & (1 << i)) != 0;
2594 for (i = 0; i < 31; i++)
2596 current_cpu->thread_data[threadno]
2597 .sigdata[i + 33].blocked_suspendsave
2598 = current_cpu->thread_data[threadno]
2599 .sigdata[i + 33].blocked;
2600 current_cpu->thread_data[threadno]
2601 .sigdata[i + 33].blocked = (set_high & (1 << i)) != 0;
2604 current_cpu->thread_data[threadno].sigsuspended = 1;
2606 /* The mask changed, so a signal may be unblocked for
2608 current_cpu->thread_data[threadno].sigpending = 1;
2611 /* Because we don't use arg1 (newsetp) when this syscall is
2612 rerun, it doesn't matter that we overwrite it with the
2613 (constant) return value. */
2614 retval = -cb_host_to_target_errno (cb, EINTR);
2615 sim_pc_set (current_cpu, pc);
2619 /* Add case labels here for other syscalls using the 32-bit
2620 "struct stat", provided they have a corresponding simulator
2621 function of course. */
2622 case TARGET_SYS_stat:
2623 case TARGET_SYS_fstat:
2625 /* As long as the infrastructure doesn't cache anything
2626 related to the stat mapping, this trick gets us a dual
2627 "struct stat"-type mapping in the least error-prone way. */
2628 const char *saved_map = cb->stat_map;
2629 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
2631 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_stat32_map;
2632 cb->stat_map = stat32_map;
2634 if (cb_syscall (cb, &s) != CB_RC_OK)
2637 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2640 retval = s.result == -1 ? -s.errcode : s.result;
2642 cb->stat_map = saved_map;
2643 cb->syscall_map = saved_syscall_map;
2647 case TARGET_SYS_getcwd:
2652 char *cwd = xmalloc (SIM_PATHMAX);
2653 if (cwd != getcwd (cwd, SIM_PATHMAX))
2656 /* FIXME: When and if we support chdir, we need something
2657 a bit more elaborate. */
2658 if (simulator_sysroot[0] != '\0')
2661 retval = -cb_host_to_target_errno (cb, ERANGE);
2662 if (strlen (cwd) + 1 <= size)
2664 retval = strlen (cwd) + 1;
2665 if (sim_core_write_buffer (sd, current_cpu, 0, cwd,
2667 != (unsigned int) retval)
2668 retval = -cb_host_to_target_errno (cb, EFAULT);
2674 case TARGET_SYS_access:
2678 char *pbuf = xmalloc (SIM_PATHMAX);
2683 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2685 strcpy (pbuf, simulator_sysroot);
2686 o += strlen (simulator_sysroot);
2689 for (i = 0; i + o < SIM_PATHMAX; i++)
2692 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2693 if (pbuf[i + o] == 0)
2697 if (i + o == SIM_PATHMAX)
2699 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2703 /* Assert that we don't get calls for files for which we
2704 don't have support. */
2705 if (strncmp (pbuf + strlen (simulator_sysroot),
2708 #define X_AFLAG(x) if (mode & TARGET_ ## x) hmode |= x
2715 if (access (pbuf, hmode) != 0)
2716 retval = -cb_host_to_target_errno (cb, errno);
2724 case TARGET_SYS_readlink:
2729 char *pbuf = xmalloc (SIM_PATHMAX);
2730 char *lbuf = xmalloc (SIM_PATHMAX);
2731 char *lbuf_alloc = lbuf;
2736 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2738 strcpy (pbuf, simulator_sysroot);
2739 o += strlen (simulator_sysroot);
2742 for (i = 0; i + o < SIM_PATHMAX; i++)
2745 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2746 if (pbuf[i + o] == 0)
2750 if (i + o == SIM_PATHMAX)
2752 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2756 /* Intervene calls for certain files expected in the target
2757 proc file system. */
2758 if (strcmp (pbuf + strlen (simulator_sysroot),
2759 "/proc/" XSTRING (TARGET_PID) "/exe") == 0)
2762 = (STATE_PROG_ARGV (sd) != NULL
2763 ? *STATE_PROG_ARGV (sd) : NULL);
2765 if (argv0 == NULL || *argv0 == '.')
2768 = cris_unknown_syscall (current_cpu, pc,
2769 "Unimplemented readlink syscall "
2770 "(0x%lx: [\"%s\"], 0x%lx)\n",
2771 (unsigned long) arg1, pbuf,
2772 (unsigned long) arg2);
2775 else if (*argv0 == '/')
2777 if (strncmp (simulator_sysroot, argv0,
2778 strlen (simulator_sysroot)) == 0)
2779 argv0 += strlen (simulator_sysroot);
2781 strcpy (lbuf, argv0);
2782 nchars = strlen (argv0) + 1;
2786 if (getcwd (lbuf, SIM_PATHMAX) != NULL
2787 && strlen (lbuf) + 2 + strlen (argv0) < SIM_PATHMAX)
2789 if (strncmp (simulator_sysroot, lbuf,
2790 strlen (simulator_sysroot)) == 0)
2791 lbuf += strlen (simulator_sysroot);
2794 strcat (lbuf, argv0);
2795 nchars = strlen (lbuf) + 1;
2802 nchars = readlink (pbuf, lbuf, SIM_PATHMAX);
2804 /* We trust that the readlink result returns a *relative*
2805 link, or one already adjusted for the file-path-prefix.
2806 (We can't generally tell the difference, so we go with
2807 the easiest decision; no adjustment.) */
2811 retval = -cb_host_to_target_errno (cb, errno);
2815 if (bufsiz < nchars)
2818 if (sim_core_write_buffer (sd, current_cpu, write_map, lbuf,
2819 buf, nchars) != (unsigned int) nchars)
2820 retval = -cb_host_to_target_errno (cb, EFAULT);
2829 case TARGET_SYS_sched_getscheduler:
2833 /* FIXME: Search (other) existing threads. */
2834 if (pid != 0 && pid != TARGET_PID)
2835 retval = -cb_host_to_target_errno (cb, ESRCH);
2837 retval = TARGET_SCHED_OTHER;
2841 case TARGET_SYS_sched_getparam:
2847 struct sched_param {
2851 if (pid != 0 && pid != TARGET_PID)
2852 retval = -cb_host_to_target_errno (cb, ESRCH);
2855 /* FIXME: Save scheduler setting before threads are
2857 sim_core_write_unaligned_4 (current_cpu, pc, 0, paramp,
2858 current_cpu->thread_data != NULL
2860 ->thread_data[threadno]
2868 case TARGET_SYS_sched_setparam:
2873 if ((pid != 0 && pid != TARGET_PID)
2874 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2876 retval = -cb_host_to_target_errno (cb, EINVAL);
2882 case TARGET_SYS_sched_setscheduler:
2888 if ((pid != 0 && pid != TARGET_PID)
2889 || policy != TARGET_SCHED_OTHER
2890 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2892 retval = -cb_host_to_target_errno (cb, EINVAL);
2894 /* FIXME: Save scheduler setting to be read in later
2895 sched_getparam calls. */
2900 case TARGET_SYS_sched_yield:
2901 /* We reschedule to the next thread after a syscall anyway, so
2902 we don't have to do anything here than to set the return
2907 case TARGET_SYS_sched_get_priority_min:
2908 case TARGET_SYS_sched_get_priority_max:
2910 retval = -cb_host_to_target_errno (cb, EINVAL);
2915 case TARGET_SYS_ugetrlimit:
2917 unsigned int curlim, maxlim;
2918 if (arg1 != TARGET_RLIMIT_STACK && arg1 != TARGET_RLIMIT_NOFILE)
2920 retval = -cb_host_to_target_errno (cb, EINVAL);
2926 unsigned long rlim_cur;
2927 unsigned long rlim_max;
2929 if (arg1 == TARGET_RLIMIT_NOFILE)
2931 /* Sadly a very low limit. Better not lie, though. */
2932 maxlim = curlim = MAX_CALLBACK_FDS;
2934 else /* arg1 == TARGET_RLIMIT_STACK */
2936 maxlim = 0xffffffff;
2939 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, curlim);
2940 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, maxlim);
2945 case TARGET_SYS_setrlimit:
2946 if (arg1 != TARGET_RLIMIT_STACK)
2948 retval = -cb_host_to_target_errno (cb, EINVAL);
2951 /* FIXME: Save values for future ugetrlimit calls. */
2955 /* Provide a very limited subset of the sysctl functions, and
2956 abort for the rest. */
2957 case TARGET_SYS__sysctl:
2960 struct __sysctl_args {
2967 unsigned long __unused[4];
2969 SI name = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1);
2970 SI name0 = name == 0
2971 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name);
2972 SI name1 = name == 0
2973 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name + 4);
2975 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 4);
2977 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 8);
2979 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 12);
2980 SI oldlen = oldlenp == 0
2981 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, oldlenp);
2983 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 16);
2985 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 20);
2987 if (name0 == TARGET_CTL_KERN && name1 == TARGET_CTL_KERN_VERSION)
2989 SI to_write = oldlen < (SI) sizeof (TARGET_UTSNAME)
2990 ? oldlen : (SI) sizeof (TARGET_UTSNAME);
2992 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldlenp,
2993 sizeof (TARGET_UTSNAME));
2995 if (sim_core_write_buffer (sd, current_cpu, write_map,
2996 TARGET_UTSNAME, oldval,
2998 != (unsigned int) to_write)
2999 retval = -cb_host_to_target_errno (cb, EFAULT);
3006 = cris_unknown_syscall (current_cpu, pc,
3007 "Unimplemented _sysctl syscall "
3008 "(0x%lx: [0x%lx, 0x%lx],"
3009 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
3010 (unsigned long) name,
3011 (unsigned long) name0,
3012 (unsigned long) name1,
3013 (unsigned long) nlen,
3014 (unsigned long) oldval,
3015 (unsigned long) oldlenp,
3016 (unsigned long) newval,
3017 (unsigned long) newlen);
3021 case TARGET_SYS_exit:
3023 /* Here for all but the last thread. */
3026 = current_cpu->thread_data[threadno].threadid + TARGET_PID;
3028 = (current_cpu->thread_data[threadno].parent_threadid
3030 int exitsig = current_cpu->thread_data[threadno].exitsig;
3032 /* Any children are now all orphans. */
3033 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
3034 if (current_cpu->thread_data[i].parent_threadid
3035 == current_cpu->thread_data[threadno].threadid)
3036 /* Make getppid(2) return 1 for them, poor little ones. */
3037 current_cpu->thread_data[i].parent_threadid = -TARGET_PID + 1;
3039 /* Free the cpu context data. When the parent has received
3040 the exit status, we'll clear the entry too. */
3041 free (current_cpu->thread_data[threadno].cpu_context);
3042 current_cpu->thread_data[threadno].cpu_context = NULL;
3043 current_cpu->m1threads--;
3046 sim_io_eprintf (sd, "Thread %d exited with status %d\n",
3048 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
3052 /* Still, we may want to support non-zero exit values. */
3053 current_cpu->thread_data[threadno].exitval = arg1 << 8;
3056 deliver_signal (current_cpu, exitsig, ppid);
3060 case TARGET_SYS_clone:
3062 int nthreads = current_cpu->m1threads + 1;
3063 void *thread_cpu_data;
3064 bfd_byte old_sp_buf[4];
3066 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
3069 /* That's right, the syscall clone arguments are reversed
3070 compared to sys_clone notes in clone(2) and compared to
3071 other Linux ports (i.e. it's the same order as in the
3072 clone(2) libcall). */
3076 if (nthreads == SIM_TARGET_MAX_THREADS)
3078 retval = -cb_host_to_target_errno (cb, EAGAIN);
3082 /* FIXME: Implement the low byte. */
3083 if ((flags & ~TARGET_CSIGNAL) !=
3086 | TARGET_CLONE_FILES
3087 | TARGET_CLONE_SIGHAND)
3091 = cris_unknown_syscall (current_cpu, pc,
3092 "Unimplemented clone syscall "
3094 (unsigned long) arg1,
3095 (unsigned long) arg2);
3099 if (current_cpu->thread_data == NULL)
3100 make_first_thread (current_cpu);
3102 /* The created thread will get the new SP and a cleared R10.
3103 Since it's created out of a copy of the old thread and we
3104 don't have a set-register-function that just take the
3105 cpu_data as a parameter, we set the childs values first,
3106 and write back or overwrite them in the parent after the
3108 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
3109 H_GR_SP, old_sp_buf, 4);
3110 bfd_putl32 (newsp, sp_buf);
3111 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3112 H_GR_SP, sp_buf, 4);
3113 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3114 H_GR_R10, (bfd_byte *) zeros, 4);
3117 ->make_thread_cpu_data) (current_cpu,
3118 ¤t_cpu->cpu_data_placeholder);
3119 (*CPU_REG_STORE (current_cpu)) (current_cpu,
3120 H_GR_SP, old_sp_buf, 4);
3122 retval = ++current_cpu->max_threadid + TARGET_PID;
3124 /* Find an unused slot. After a few threads have been created
3125 and exited, the array is expected to be a bit fragmented.
3126 We don't reuse the first entry, though, that of the
3128 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
3129 if (current_cpu->thread_data[i].cpu_context == NULL
3130 /* Don't reuse a zombied entry. */
3131 && current_cpu->thread_data[i].threadid == 0)
3134 memcpy (¤t_cpu->thread_data[i],
3135 ¤t_cpu->thread_data[threadno],
3136 sizeof (current_cpu->thread_data[i]));
3137 current_cpu->thread_data[i].cpu_context = thread_cpu_data;
3138 current_cpu->thread_data[i].cpu_context_atsignal = NULL;
3139 current_cpu->thread_data[i].threadid = current_cpu->max_threadid;
3140 current_cpu->thread_data[i].parent_threadid
3141 = current_cpu->thread_data[threadno].threadid;
3142 current_cpu->thread_data[i].pipe_read_fd = 0;
3143 current_cpu->thread_data[i].pipe_write_fd = 0;
3144 current_cpu->thread_data[i].at_syscall = 0;
3145 current_cpu->thread_data[i].sigpending = 0;
3146 current_cpu->thread_data[i].sigsuspended = 0;
3147 current_cpu->thread_data[i].exitsig = flags & TARGET_CSIGNAL;
3148 current_cpu->m1threads = nthreads;
3152 /* Better watch these in case they do something necessary. */
3153 case TARGET_SYS_socketcall:
3154 retval = -cb_host_to_target_errno (cb, ENOSYS);
3157 unimplemented_syscall:
3160 = cris_unknown_syscall (current_cpu, pc,
3161 "Unimplemented syscall: %d "
3162 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
3163 callnum, arg1, arg2, arg3, arg4, arg5,
3168 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
3169 if (callnum == TARGET_SYS_open)
3171 current_cpu->last_open_fd = retval;
3172 current_cpu->last_open_flags = arg2;
3175 current_cpu->last_syscall = callnum;
3177 /* A system call is a rescheduling point. For the time being, we don't
3178 reschedule anywhere else. */
3179 if (current_cpu->m1threads != 0
3180 /* We need to schedule off from an exiting thread that is the
3182 || (current_cpu->thread_data != NULL
3183 && current_cpu->thread_data[threadno].cpu_context == NULL))
3185 bfd_byte retval_buf[4];
3187 current_cpu->thread_data[threadno].last_execution
3188 = TARGET_TIME_MS (current_cpu);
3189 bfd_putl32 (retval, retval_buf);
3190 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
3192 current_cpu->thread_data[threadno].at_syscall = 1;
3193 reschedule (current_cpu);
3195 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
3196 retval = bfd_getl32 (retval_buf);
3202 /* Callback from simulator write saying that the pipe at (reader, writer)
3203 is now non-empty (so the writer should wait until the pipe is empty, at
3204 least not write to this or any other pipe). Simplest is to just wait
3205 until the pipe is empty. */
3208 cris_pipe_nonempty (host_callback *cb ATTRIBUTE_UNUSED,
3209 int reader, int writer)
3211 SIM_CPU *cpu = current_cpu_for_cb_callback;
3212 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
3214 /* It's the current thread: we just have to re-run the current
3215 syscall instruction (presumably "break 13") and change the syscall
3216 to the special simulator-wait code. Oh, and set a marker that
3217 we're waiting, so we can disambiguate the special call from a
3220 This function may be called multiple times between cris_pipe_empty,
3221 but we must avoid e.g. decreasing PC every time. Check fd markers
3223 if (cpu->thread_data == NULL)
3225 sim_io_eprintf (CPU_STATE (cpu),
3226 "Terminating simulation due to writing pipe rd:wr %d:%d"
3227 " from one single thread\n", reader, writer);
3228 sim_engine_halt (CPU_STATE (cpu), cpu,
3229 NULL, sim_pc_get (cpu), sim_stopped, SIM_SIGILL);
3231 else if (cpu->thread_data[cpu->threadno].pipe_write_fd == 0)
3233 cpu->thread_data[cpu->threadno].pipe_write_fd = writer;
3234 cpu->thread_data[cpu->threadno].pipe_read_fd = reader;
3235 /* FIXME: We really shouldn't change registers other than R10 in
3236 syscalls (like R9), here or elsewhere. */
3237 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R9, (bfd_byte *) zeros, 4);
3238 sim_pc_set (cpu, sim_pc_get (cpu) - 2);
3242 /* Callback from simulator close or read call saying that the pipe at
3243 (reader, writer) is now empty (so the writer can write again, perhaps
3244 leave a waiting state). If there are bytes remaining, they couldn't be
3245 consumed (perhaps due to the pipe closing). */
3248 cris_pipe_empty (host_callback *cb,
3253 SIM_CPU *cpu = current_cpu_for_cb_callback;
3254 bfd_byte r10_buf[4];
3256 = cb->pipe_buffer[writer].size - cb->pipe_buffer[reader].size;
3258 /* We need to find the thread that waits for this pipe. */
3259 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
3260 if (cpu->thread_data[i].cpu_context
3261 && cpu->thread_data[i].pipe_write_fd == writer)
3265 /* Temporarily switch to this cpu context, so we can change the
3266 PC by ordinary calls. */
3268 memcpy (cpu->thread_data[cpu->threadno].cpu_context,
3269 &cpu->cpu_data_placeholder,
3270 cpu->thread_cpu_data_size);
3271 memcpy (&cpu->cpu_data_placeholder,
3272 cpu->thread_data[i].cpu_context,
3273 cpu->thread_cpu_data_size);
3275 /* The return value is supposed to contain the number of
3276 written bytes, which is the number of bytes requested and
3277 returned at the write call. You might think the right
3278 thing is to adjust the return-value to be only the
3279 *consumed* number of bytes, but it isn't. We're only
3280 called if the pipe buffer is fully consumed or it is being
3281 closed, possibly with remaining bytes. For the latter
3282 case, the writer is still supposed to see success for
3283 PIPE_BUF bytes (a constant which we happen to know and is
3284 unlikely to change). The return value may also be a
3285 negative number; an error value. This case is covered
3286 because "remaining" is always >= 0. */
3287 (*CPU_REG_FETCH (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3288 retval = (int) bfd_getl_signed_32 (r10_buf);
3289 if (retval - remaining > TARGET_PIPE_BUF)
3291 bfd_putl32 (retval - remaining, r10_buf);
3292 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R10, r10_buf, 4);
3294 sim_pc_set (cpu, sim_pc_get (cpu) + 2);
3295 memcpy (cpu->thread_data[i].cpu_context,
3296 &cpu->cpu_data_placeholder,
3297 cpu->thread_cpu_data_size);
3298 memcpy (&cpu->cpu_data_placeholder,
3299 cpu->thread_data[cpu->threadno].cpu_context,
3300 cpu->thread_cpu_data_size);
3301 cpu->thread_data[i].pipe_read_fd = 0;
3302 cpu->thread_data[i].pipe_write_fd = 0;
3309 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3312 cris_time (host_callback *cb ATTRIBUTE_UNUSED, long *t)
3314 long retval = TARGET_TIME (current_cpu_for_cb_callback);
3320 /* Set target-specific callback data. */
3323 cris_set_callbacks (host_callback *cb)
3325 /* Yeargh, have to cast away constness to avoid warnings. */
3326 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
3327 cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
3329 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3330 parameter to cb_store_target_endian will make st_size negative.
3331 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3332 *unsigned*, and/or add syntax for signed-ness. */
3333 cb->stat_map = stat_map;
3334 cb->open_map = (CB_TARGET_DEFS_MAP *) open_map;
3335 cb->pipe_nonempty = cris_pipe_nonempty;
3336 cb->pipe_empty = cris_pipe_empty;
3337 cb->time = cris_time;
3340 /* Process an address exception. */
3343 cris_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
3344 unsigned int map, int nr_bytes, address_word addr,
3345 transfer_type transfer, sim_core_signals sig)
3347 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,