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"
23 #include "targ-vals.h"
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
37 #ifdef HAVE_SYS_STAT_H
41 /* The verbatim values are from asm-cris/unistd.h. */
43 #define TARGET_SYS_exit 1
44 #define TARGET_SYS_read 3
45 #define TARGET_SYS_write 4
46 #define TARGET_SYS_open 5
47 #define TARGET_SYS_close 6
48 #define TARGET_SYS_unlink 10
49 #define TARGET_SYS_time 13
50 #define TARGET_SYS_lseek 19
51 #define TARGET_SYS_getpid 20
52 #define TARGET_SYS_kill 37
53 #define TARGET_SYS_rename 38
54 #define TARGET_SYS_pipe 42
55 #define TARGET_SYS_brk 45
56 #define TARGET_SYS_ioctl 54
57 #define TARGET_SYS_fcntl 55
58 #define TARGET_SYS_getppid 64
59 #define TARGET_SYS_setrlimit 75
60 #define TARGET_SYS_gettimeofday 78
61 #define TARGET_SYS_readlink 85
62 #define TARGET_SYS_munmap 91
63 #define TARGET_SYS_truncate 92
64 #define TARGET_SYS_ftruncate 93
65 #define TARGET_SYS_socketcall 102
66 #define TARGET_SYS_stat 106
67 #define TARGET_SYS_fstat 108
68 #define TARGET_SYS_wait4 114
69 #define TARGET_SYS_sigreturn 119
70 #define TARGET_SYS_clone 120
71 #define TARGET_SYS_uname 122
72 #define TARGET_SYS_mprotect 125
73 #define TARGET_SYS_llseek 140
74 #define TARGET_SYS__sysctl 149
75 #define TARGET_SYS_sched_setparam 154
76 #define TARGET_SYS_sched_getparam 155
77 #define TARGET_SYS_sched_setscheduler 156
78 #define TARGET_SYS_sched_getscheduler 157
79 #define TARGET_SYS_sched_yield 158
80 #define TARGET_SYS_sched_get_priority_max 159
81 #define TARGET_SYS_sched_get_priority_min 160
82 #define TARGET_SYS_mremap 163
83 #define TARGET_SYS_poll 168
84 #define TARGET_SYS_rt_sigaction 174
85 #define TARGET_SYS_rt_sigprocmask 175
86 #define TARGET_SYS_rt_sigsuspend 179
87 #define TARGET_SYS_getcwd 183
88 #define TARGET_SYS_ugetrlimit 191
89 #define TARGET_SYS_mmap2 192
90 #define TARGET_SYS_stat64 195
91 #define TARGET_SYS_lstat64 196
92 #define TARGET_SYS_fstat64 197
93 #define TARGET_SYS_geteuid32 201
94 #define TARGET_SYS_getuid32 199
95 #define TARGET_SYS_getegid32 202
96 #define TARGET_SYS_getgid32 200
97 #define TARGET_SYS_fcntl64 221
99 #define TARGET_PROT_READ 0x1
100 #define TARGET_PROT_WRITE 0x2
101 #define TARGET_PROT_EXEC 0x4
102 #define TARGET_PROT_NONE 0x0
104 #define TARGET_MAP_SHARED 0x01
105 #define TARGET_MAP_PRIVATE 0x02
106 #define TARGET_MAP_TYPE 0x0f
107 #define TARGET_MAP_FIXED 0x10
108 #define TARGET_MAP_ANONYMOUS 0x20
110 #define TARGET_CTL_KERN 1
111 #define TARGET_CTL_VM 2
112 #define TARGET_CTL_NET 3
113 #define TARGET_CTL_PROC 4
114 #define TARGET_CTL_FS 5
115 #define TARGET_CTL_DEBUG 6
116 #define TARGET_CTL_DEV 7
117 #define TARGET_CTL_BUS 8
118 #define TARGET_CTL_ABI 9
120 #define TARGET_CTL_KERN_VERSION 4
123 #define TARGET_MREMAP_MAYMOVE 1
124 #define TARGET_MREMAP_FIXED 2
126 #define TARGET_TCGETS 0x5401
128 #define TARGET_UTSNAME "#38 Sun Apr 1 00:00:00 MET 2001"
130 /* Seconds since the above date + 10 minutes. */
131 #define TARGET_EPOCH 986080200
133 /* Milliseconds since start of run. We use the number of syscalls to
134 avoid introducing noise in the execution time. */
135 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
137 /* Seconds as in time(2). */
138 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
140 #define TARGET_SCHED_OTHER 0
142 #define TARGET_RLIMIT_STACK 3
143 #define TARGET_RLIMIT_NOFILE 7
145 #define SIM_TARGET_MAX_THREADS 64
146 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
148 /* From linux/sched.h. */
149 #define TARGET_CSIGNAL 0x000000ff
150 #define TARGET_CLONE_VM 0x00000100
151 #define TARGET_CLONE_FS 0x00000200
152 #define TARGET_CLONE_FILES 0x00000400
153 #define TARGET_CLONE_SIGHAND 0x00000800
154 #define TARGET_CLONE_PID 0x00001000
155 #define TARGET_CLONE_PTRACE 0x00002000
156 #define TARGET_CLONE_VFORK 0x00004000
157 #define TARGET_CLONE_PARENT 0x00008000
158 #define TARGET_CLONE_THREAD 0x00010000
159 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
161 /* From asm-cris/poll.h. */
162 #define TARGET_POLLIN 1
164 /* From asm-cris/signal.h. */
165 #define TARGET_SIG_BLOCK 0
166 #define TARGET_SIG_UNBLOCK 1
167 #define TARGET_SIG_SETMASK 2
169 #define TARGET_SIG_DFL 0
170 #define TARGET_SIG_IGN 1
171 #define TARGET_SIG_ERR ((USI)-1)
173 #define TARGET_SIGHUP 1
174 #define TARGET_SIGINT 2
175 #define TARGET_SIGQUIT 3
176 #define TARGET_SIGILL 4
177 #define TARGET_SIGTRAP 5
178 #define TARGET_SIGABRT 6
179 #define TARGET_SIGIOT 6
180 #define TARGET_SIGBUS 7
181 #define TARGET_SIGFPE 8
182 #define TARGET_SIGKILL 9
183 #define TARGET_SIGUSR1 10
184 #define TARGET_SIGSEGV 11
185 #define TARGET_SIGUSR2 12
186 #define TARGET_SIGPIPE 13
187 #define TARGET_SIGALRM 14
188 #define TARGET_SIGTERM 15
189 #define TARGET_SIGSTKFLT 16
190 #define TARGET_SIGCHLD 17
191 #define TARGET_SIGCONT 18
192 #define TARGET_SIGSTOP 19
193 #define TARGET_SIGTSTP 20
194 #define TARGET_SIGTTIN 21
195 #define TARGET_SIGTTOU 22
196 #define TARGET_SIGURG 23
197 #define TARGET_SIGXCPU 24
198 #define TARGET_SIGXFSZ 25
199 #define TARGET_SIGVTALRM 26
200 #define TARGET_SIGPROF 27
201 #define TARGET_SIGWINCH 28
202 #define TARGET_SIGIO 29
203 #define TARGET_SIGPOLL SIGIO
204 /* Actually commented out in the kernel header. */
205 #define TARGET_SIGLOST 29
206 #define TARGET_SIGPWR 30
207 #define TARGET_SIGSYS 31
209 /* From include/asm-cris/signal.h. */
210 #define TARGET_SA_NOCLDSTOP 0x00000001
211 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
212 #define TARGET_SA_SIGINFO 0x00000004
213 #define TARGET_SA_ONSTACK 0x08000000
214 #define TARGET_SA_RESTART 0x10000000
215 #define TARGET_SA_NODEFER 0x40000000
216 #define TARGET_SA_RESETHAND 0x80000000
217 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
218 #define TARGET_SA_RESTORER 0x04000000
220 /* From linux/wait.h. */
221 #define TARGET_WNOHANG 1
222 #define TARGET_WUNTRACED 2
223 #define TARGET___WNOTHREAD 0x20000000
224 #define TARGET___WALL 0x40000000
225 #define TARGET___WCLONE 0x80000000
227 static const char stat_map[] =
228 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
229 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
230 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
233 static const CB_TARGET_DEFS_MAP syscall_map[] =
235 { CB_SYS_open, TARGET_SYS_open },
236 { CB_SYS_close, TARGET_SYS_close },
237 { CB_SYS_read, TARGET_SYS_read },
238 { CB_SYS_write, TARGET_SYS_write },
239 { CB_SYS_lseek, TARGET_SYS_lseek },
240 { CB_SYS_unlink, TARGET_SYS_unlink },
241 { CB_SYS_getpid, TARGET_SYS_getpid },
242 { CB_SYS_fstat, TARGET_SYS_fstat64 },
243 { CB_SYS_lstat, TARGET_SYS_lstat64 },
244 { CB_SYS_stat, TARGET_SYS_stat64 },
245 { CB_SYS_pipe, TARGET_SYS_pipe },
246 { CB_SYS_time, TARGET_SYS_time },
247 { CB_SYS_rename, TARGET_SYS_rename },
248 { CB_SYS_truncate, TARGET_SYS_truncate },
249 { CB_SYS_ftruncate, TARGET_SYS_ftruncate },
253 /* An older, 32-bit-only stat mapping. */
254 static const char stat32_map[] =
255 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
256 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
257 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
259 /* Map for calls using the 32-bit struct stat. Primarily used by the
260 newlib Linux mapping. */
261 static const CB_TARGET_DEFS_MAP syscall_stat32_map[] =
263 { CB_SYS_fstat, TARGET_SYS_fstat },
264 { CB_SYS_stat, TARGET_SYS_stat },
268 /* Giving the true value for the running sim process will lead to
269 non-time-invariant behavior. */
270 #define TARGET_PID 42
272 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
273 we did, we'd still don't get a register number with the "16" offset. */
274 #define TARGET_SRP_REGNUM (16+11)
276 /* Extracted by applying
277 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
278 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
279 adjusting the synonyms. */
281 static const CB_TARGET_DEFS_MAP errno_map[] =
389 { ENAMETOOLONG, 36 },
548 { EDESTADDRREQ, 89 },
559 #ifdef EPROTONOSUPPORT
560 { EPROTONOSUPPORT, 93 },
562 #ifdef ESOCKTNOSUPPORT
563 { ESOCKTNOSUPPORT, 94 },
569 { EPFNOSUPPORT, 96 },
572 { EAFNOSUPPORT, 97 },
578 { EADDRNOTAVAIL, 99 },
584 { ENETUNREACH, 101 },
590 { ECONNABORTED, 103 },
608 { ETOOMANYREFS, 109 },
614 { ECONNREFUSED, 111 },
620 { EHOSTUNREACH, 113 },
626 { EINPROGRESS, 115 },
653 { EMEDIUMTYPE, 124 },
658 /* Extracted by applying
659 perl -ne 'if ($_ =~ /^#define/) { split;
660 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
661 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
662 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
663 installation and removing synonyms and unnecessary items. Don't
664 forget the end-marker. */
666 static const CB_TARGET_DEFS_MAP open_map[] = {
695 { O_NONBLOCK, 0x800 },
707 { O_DIRECT, 0x4000 },
710 { O_LARGEFILE, 0x8000 },
713 { O_DIRECTORY, 0x10000 },
716 { O_NOFOLLOW, 0x20000 },
721 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
722 static SIM_CPU *current_cpu_for_cb_callback;
724 static int syscall_read_mem (host_callback *, struct cb_syscall *,
725 unsigned long, char *, int);
726 static int syscall_write_mem (host_callback *, struct cb_syscall *,
727 unsigned long, const char *, int);
728 static USI create_map (SIM_DESC, struct cris_sim_mmapped_page **,
730 static USI unmap_pages (SIM_DESC, struct cris_sim_mmapped_page **,
732 static USI is_mapped (SIM_DESC, struct cris_sim_mmapped_page **,
734 static void dump_statistics (SIM_CPU *current_cpu);
735 static void make_first_thread (SIM_CPU *current_cpu);
737 /* Read/write functions for system call interface. */
740 syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED,
741 struct cb_syscall *sc,
742 unsigned long taddr, char *buf, int bytes)
744 SIM_DESC sd = (SIM_DESC) sc->p1;
745 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
747 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
751 syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED,
752 struct cb_syscall *sc,
753 unsigned long taddr, const char *buf, int bytes)
755 SIM_DESC sd = (SIM_DESC) sc->p1;
756 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
758 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
761 /* When we risk running self-modified code (as in trampolines), this is
762 called from special-case insns. The silicon CRIS CPU:s have enough
763 cache snooping implemented making this a simulator-only issue. Tests:
764 gcc.c-torture/execute/931002-1.c execution, -O3 -g
765 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
768 cris_flush_simulator_decode_cache (SIM_CPU *current_cpu,
769 USI pc ATTRIBUTE_UNUSED)
771 SIM_DESC sd = CPU_STATE (current_cpu);
774 if (USING_SCACHE_P (sd))
775 scache_flush_cpu (current_cpu);
779 /* Output statistics at the end of a run. */
781 dump_statistics (SIM_CPU *current_cpu)
783 SIM_DESC sd = CPU_STATE (current_cpu);
784 CRIS_MISC_PROFILE *profp
785 = CPU_CRIS_MISC_PROFILE (current_cpu);
786 unsigned64 total = profp->basic_cycle_count;
787 const char *textmsg = "Basic clock cycles, total @: %llu\n";
789 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
790 what's included in the "total" count only. */
791 switch (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
792 & FLAG_CRIS_MISC_PROFILE_ALL)
794 case FLAG_CRIS_MISC_PROFILE_SIMPLE:
797 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED | FLAG_CRIS_MISC_PROFILE_SIMPLE):
799 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
800 total += profp->unaligned_mem_dword_count;
803 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | FLAG_CRIS_MISC_PROFILE_SIMPLE):
804 textmsg = "Schedulable clock cycles, total @: %llu\n";
806 += (profp->memsrc_stall_count
807 + profp->memraw_stall_count
808 + profp->movemsrc_stall_count
809 + profp->movemdst_stall_count
810 + profp->mulsrc_stall_count
811 + profp->jumpsrc_stall_count
812 + profp->unaligned_mem_dword_count);
815 case FLAG_CRIS_MISC_PROFILE_ALL:
816 textmsg = "All accounted clock cycles, total @: %llu\n";
818 += (profp->memsrc_stall_count
819 + profp->memraw_stall_count
820 + profp->movemsrc_stall_count
821 + profp->movemdst_stall_count
822 + profp->movemaddr_stall_count
823 + profp->mulsrc_stall_count
824 + profp->jumpsrc_stall_count
825 + profp->branch_stall_count
826 + profp->jumptarget_stall_count
827 + profp->unaligned_mem_dword_count);
834 "Internal inconsistency at %s:%d",
836 sim_engine_halt (sd, current_cpu, NULL, 0,
837 sim_stopped, SIM_SIGILL);
840 /* Historically, these messages have gone to stderr, so we'll keep it
841 that way. It's also easier to then tell it from normal program
842 output. FIXME: Add redirect option like "run -e file". */
843 sim_io_eprintf (sd, textmsg, total);
845 /* For v32, unaligned_mem_dword_count should always be 0. For
846 v10, memsrc_stall_count should always be 0. */
847 sim_io_eprintf (sd, "Memory source stall cycles: %lld\n",
848 profp->memsrc_stall_count
849 + profp->unaligned_mem_dword_count);
850 sim_io_eprintf (sd, "Memory read-after-write stall cycles: %lld\n",
851 profp->memraw_stall_count);
852 sim_io_eprintf (sd, "Movem source stall cycles: %lld\n",
853 profp->movemsrc_stall_count);
854 sim_io_eprintf (sd, "Movem destination stall cycles: %lld\n",
855 profp->movemdst_stall_count);
856 sim_io_eprintf (sd, "Movem address stall cycles: %lld\n",
857 profp->movemaddr_stall_count);
858 sim_io_eprintf (sd, "Multiplication source stall cycles: %lld\n",
859 profp->mulsrc_stall_count);
860 sim_io_eprintf (sd, "Jump source stall cycles: %lld\n",
861 profp->jumpsrc_stall_count);
862 sim_io_eprintf (sd, "Branch misprediction stall cycles: %lld\n",
863 profp->branch_stall_count);
864 sim_io_eprintf (sd, "Jump target stall cycles: %lld\n",
865 profp->jumptarget_stall_count);
868 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
869 Return 1 if a overlap detected, 0 otherwise. */
872 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED,
873 struct cris_sim_mmapped_page **rootp,
876 struct cris_sim_mmapped_page *mapp;
878 if (len == 0 || (len & 8191))
881 /* Iterate over the reverse-address sorted pages until we find a page in
882 or lower than the checked area. */
883 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
884 if (mapp->addr < addr + len && mapp->addr >= addr)
890 /* Create mmapped memory. */
893 create_map (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
896 struct cris_sim_mmapped_page *mapp;
897 struct cris_sim_mmapped_page **higher_prevp = rootp;
898 USI new_addr = 0x40000000;
903 new_addr = rootp[0]->addr + 8192;
910 /* Which is better: return an error for this, or just round it up? */
913 /* Do a recursive call for each page in the request. */
914 for (page_addr = new_addr; len != 0; page_addr += 8192, len -= 8192)
915 if (create_map (sd, rootp, page_addr, 8192) >= (USI) -8191)
922 mapp != NULL && mapp->addr > new_addr;
924 higher_prevp = &mapp->prev;
926 /* Allocate the new page, on the next higher page from the last one
927 allocated, and link in the new descriptor before previous ones. */
928 mapp = malloc (sizeof (*mapp));
931 return (USI) -ENOMEM;
933 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
937 mapp->addr = new_addr;
938 mapp->prev = *higher_prevp;
939 *higher_prevp = mapp;
944 /* Unmap one or more pages. */
947 unmap_pages (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
950 struct cris_sim_mmapped_page *mapp;
951 struct cris_sim_mmapped_page **higher_prevp = rootp;
958 /* Which is better: return an error for this, or just round it up? */
961 /* Loop backwards to make each call is O(1) over the number of pages
962 allocated, if we're unmapping from the high end of the pages. */
963 for (page_addr = addr + len - 8192;
966 if (unmap_pages (sd, rootp, page_addr, 8192) != 0)
972 for (mapp = *rootp; mapp != NULL && mapp->addr > addr; mapp = mapp->prev)
973 higher_prevp = &mapp->prev;
975 if (mapp == NULL || mapp->addr != addr)
978 *higher_prevp = mapp->prev;
979 sim_core_detach (sd, NULL, 0, 0, addr);
984 /* The semantic code invokes this for illegal (unrecognized) instructions. */
987 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
989 SIM_DESC sd = CPU_STATE (current_cpu);
991 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
995 /* Handlers from the CGEN description that should not be called. */
998 cris_bmod_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
999 UINT srcreg ATTRIBUTE_UNUSED,
1000 USI dstreg ATTRIBUTE_UNUSED)
1006 h_supr_set_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1007 UINT index ATTRIBUTE_UNUSED,
1008 USI page ATTRIBUTE_UNUSED,
1009 USI newval ATTRIBUTE_UNUSED)
1015 h_supr_get_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1016 UINT index ATTRIBUTE_UNUSED,
1017 USI page ATTRIBUTE_UNUSED)
1022 /* Swap one context for another. */
1025 schedule (SIM_CPU *current_cpu, int next)
1027 /* Need to mark context-switches in the trace output. */
1028 if ((CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1029 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE))
1030 cris_trace_printf (CPU_STATE (current_cpu), current_cpu,
1033 /* Copy the current context (if there is one) to its slot. */
1034 if (current_cpu->thread_data[current_cpu->threadno].cpu_context)
1035 memcpy (current_cpu->thread_data[current_cpu->threadno].cpu_context,
1036 ¤t_cpu->cpu_data_placeholder,
1037 current_cpu->thread_cpu_data_size);
1039 /* Copy the new context from its slot. */
1040 memcpy (¤t_cpu->cpu_data_placeholder,
1041 current_cpu->thread_data[next].cpu_context,
1042 current_cpu->thread_cpu_data_size);
1044 /* Update needed stuff to indicate the new context. */
1045 current_cpu->threadno = next;
1047 /* Handle pending signals. */
1048 if (current_cpu->thread_data[next].sigpending
1049 /* We don't run nested signal handlers. This means that pause(2)
1050 and sigsuspend(2) do not work in sighandlers, but that
1051 shouldn't be too hard a restriction. It also greatly
1052 simplifies the code. */
1053 && current_cpu->thread_data[next].cpu_context_atsignal == NULL)
1057 /* See if there's really a pending, non-blocked handler. We don't
1058 queue signals, so just use the first one in ascending order. */
1059 for (sig = 0; sig < 64; sig++)
1060 if (current_cpu->thread_data[next].sigdata[sig].pending
1061 && !current_cpu->thread_data[next].sigdata[sig].blocked)
1067 USI pc = sim_pc_get (current_cpu);
1069 /* It's simpler to save the CPU context inside the simulator
1070 than on the stack. */
1071 current_cpu->thread_data[next].cpu_context_atsignal
1073 ->make_thread_cpu_data) (current_cpu,
1074 current_cpu->thread_data[next]
1077 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1078 sp = bfd_getl32 (regbuf);
1080 /* Make sure we have an aligned stack. */
1083 /* Make room for the signal frame, aligned. FIXME: Check that
1084 the memory exists, map it in if absent. (BTW, should also
1085 implement on-access automatic stack allocation). */
1088 /* This isn't the same signal frame as the kernel uses, because
1089 we don't want to bother getting all registers on and off the
1092 /* First, we store the currently blocked signals. */
1094 for (i = 0; i < 32; i++)
1096 |= current_cpu->thread_data[next].sigdata[i + 1].blocked << i;
1097 sim_core_write_aligned_4 (current_cpu, pc, 0, sp, blocked);
1099 for (i = 0; i < 31; i++)
1101 |= current_cpu->thread_data[next].sigdata[i + 33].blocked << i;
1102 sim_core_write_aligned_4 (current_cpu, pc, 0, sp + 4, blocked);
1104 /* Then, the actual instructions. This is CPU-specific, but we
1105 use instructions from the common subset for v10 and v32 which
1106 should be safe for the time being but could be parametrized
1108 /* MOVU.W [PC+],R9. */
1109 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 8, 0x9c5f);
1110 /* .WORD TARGET_SYS_sigreturn. */
1111 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 10,
1112 TARGET_SYS_sigreturn);
1114 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 12, 0xe93d);
1116 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1117 instruction. Still, it doesn't matter because v10 has no
1118 delay slot for BREAK so it will not be executed). */
1119 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 16, 0x05b0);
1121 /* Modify registers to hold the right values for the sighandler
1122 context: updated stackpointer and return address pointing to
1123 the sigreturn stub. */
1124 bfd_putl32 (sp, regbuf);
1125 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1126 bfd_putl32 (sp + 8, regbuf);
1127 (*CPU_REG_STORE (current_cpu)) (current_cpu, TARGET_SRP_REGNUM,
1130 current_cpu->thread_data[next].sigdata[sig].pending = 0;
1132 /* Block this signal (for the duration of the sighandler). */
1133 current_cpu->thread_data[next].sigdata[sig].blocked = 1;
1135 sim_pc_set (current_cpu, current_cpu->sighandler[sig]);
1136 bfd_putl32 (sig, regbuf);
1137 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10,
1140 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1141 needed all this for, specifies a SA_SIGINFO call but treats it
1142 like an ordinary sighandler; only the signal number argument is
1143 inspected. To make future need to implement SA_SIGINFO
1144 correctly possible, we set the siginfo argument register to a
1145 magic (hopefully non-address) number. (NB: then, you should
1146 just need to pass the siginfo argument; it seems you probably
1147 don't need to implement the specific rt_sigreturn.) */
1148 bfd_putl32 (0xbad5161f, regbuf);
1149 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R11,
1152 /* The third argument is unused and the kernel sets it to 0. */
1153 bfd_putl32 (0, regbuf);
1154 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R12,
1159 /* No, there actually was no pending signal for this thread. Reset
1161 current_cpu->thread_data[next].sigpending = 0;
1165 /* Reschedule the simplest possible way until something else is absolutely
1167 - A. Find the next process (round-robin) that doesn't have at_syscall
1169 - B. If there is none, just run the next process, round-robin.
1170 - Clear at_syscall for the current process. */
1173 reschedule (SIM_CPU *current_cpu)
1177 /* Iterate over all thread slots, because after a few thread creations
1178 and exits, we don't know where the live ones are. */
1179 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1180 i != current_cpu->threadno;
1181 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1182 if (current_cpu->thread_data[i].cpu_context
1183 && current_cpu->thread_data[i].at_syscall == 0)
1185 schedule (current_cpu, i);
1189 /* Pick any next live thread. */
1190 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1191 i != current_cpu->threadno;
1192 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1193 if (current_cpu->thread_data[i].cpu_context)
1195 schedule (current_cpu, i);
1199 /* More than one live thread, but we couldn't find the next one? */
1203 /* Set up everything to receive (or IGN) an incoming signal to the
1207 deliver_signal (SIM_CPU *current_cpu, int sig, unsigned int pid)
1210 USI pc = sim_pc_get (current_cpu);
1212 /* Find the thread index of the pid. */
1213 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1214 /* Apparently it's ok to send signals to zombies (so a check for
1215 current_cpu->thread_data[i].cpu_context != NULL would be
1217 if (current_cpu->thread_data[i].threadid == pid - TARGET_PID)
1220 switch (current_cpu->sighandler[sig])
1222 case TARGET_SIG_DFL:
1225 /* The following according to the glibc
1226 documentation. (The kernel code has non-obvious
1227 execution paths.) */
1230 case TARGET_SIGSEGV:
1232 case TARGET_SIGABRT:
1233 case TARGET_SIGTRAP:
1236 case TARGET_SIGTERM:
1238 case TARGET_SIGQUIT:
1239 case TARGET_SIGKILL:
1242 case TARGET_SIGALRM:
1243 case TARGET_SIGVTALRM:
1244 case TARGET_SIGPROF:
1245 case TARGET_SIGSTOP:
1247 case TARGET_SIGPIPE:
1248 case TARGET_SIGLOST:
1249 case TARGET_SIGXCPU:
1250 case TARGET_SIGXFSZ:
1251 case TARGET_SIGUSR1:
1252 case TARGET_SIGUSR2:
1253 sim_io_eprintf (CPU_STATE (current_cpu),
1254 "Exiting pid %d due to signal %d\n",
1256 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1257 NULL, pc, sim_stopped,
1258 sig == TARGET_SIGABRT
1259 ? SIM_SIGABRT : SIM_SIGILL);
1262 /* The default for all other signals is to be ignored. */
1267 case TARGET_SIG_IGN:
1270 case TARGET_SIGKILL:
1271 case TARGET_SIGSTOP:
1272 /* Can't ignore these signals. */
1273 sim_io_eprintf (CPU_STATE (current_cpu),
1274 "Exiting pid %d due to signal %d\n",
1276 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1277 NULL, pc, sim_stopped, SIM_SIGILL);
1286 /* Mark the signal as pending, making schedule () check
1287 closer. The signal will be handled when the thread is
1288 scheduled and the signal is unblocked. */
1289 current_cpu->thread_data[i].sigdata[sig].pending = 1;
1290 current_cpu->thread_data[i].sigpending = 1;
1295 sim_io_eprintf (CPU_STATE (current_cpu),
1296 "Unimplemented signal: %d\n", sig);
1297 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
1298 sim_stopped, SIM_SIGILL);
1303 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu)),
1307 /* Make the vector and the first item, the main thread. */
1310 make_first_thread (SIM_CPU *current_cpu)
1312 current_cpu->thread_data
1314 SIM_TARGET_MAX_THREADS
1315 * sizeof (current_cpu->thread_data[0]));
1316 current_cpu->thread_data[0].cpu_context
1317 = (*current_cpu->make_thread_cpu_data) (current_cpu,
1319 ->cpu_data_placeholder);
1320 current_cpu->thread_data[0].parent_threadid = -1;
1322 /* For good measure. */
1323 if (TARGET_SIG_DFL != 0)
1327 /* Main function: the handler of the "break 13" syscall insn. */
1330 cris_break_13_handler (SIM_CPU *current_cpu, USI callnum, USI arg1,
1331 USI arg2, USI arg3, USI arg4, USI arg5, USI arg6,
1335 SIM_DESC sd = CPU_STATE (current_cpu);
1336 host_callback *cb = STATE_CALLBACK (sd);
1338 int threadno = current_cpu->threadno;
1340 current_cpu->syscalls++;
1342 CB_SYSCALL_INIT (&s);
1348 if (callnum == TARGET_SYS_exit && current_cpu->m1threads == 0)
1350 if (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1351 & FLAG_CRIS_MISC_PROFILE_ALL)
1352 dump_statistics (current_cpu);
1353 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
1357 s.p2 = (PTR) current_cpu;
1358 s.read_mem = syscall_read_mem;
1359 s.write_mem = syscall_write_mem;
1361 current_cpu_for_cb_callback = current_cpu;
1363 if (cb_syscall (cb, &s) != CB_RC_OK)
1366 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
1368 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1371 retval = s.result == -1 ? -s.errcode : s.result;
1373 if (s.errcode != 0 && s.errcode == cb_host_to_target_errno (cb, ENOSYS))
1375 /* If the generic simulator call said ENOSYS, then let's try the
1376 ones we know ourselves.
1378 The convention is to provide *very limited* functionality on an
1379 as-needed basis, only what's covered by the test-suite, tests
1380 added when functionality changes and abort with a descriptive
1381 message for *everything* else. Where there's no test-case, we
1386 /* It's a pretty safe bet that the "old setup() system call"
1387 number will not be re-used; we can't say the same for higher
1388 numbers. We treat this simulator-generated call as "wait
1389 forever"; we re-run this insn. The wait is ended by a
1390 callback. Sanity check that this is the reason we got
1392 if (current_cpu->thread_data == NULL
1393 || (current_cpu->thread_data[threadno].pipe_write_fd == 0))
1394 goto unimplemented_syscall;
1396 sim_pc_set (current_cpu, pc);
1400 case TARGET_SYS_fcntl64:
1401 case TARGET_SYS_fcntl:
1405 Glibc checks stdin, stdout and stderr fd:s for
1406 close-on-exec security sanity. We just need to provide a
1407 OK return value. If we really need to have a
1408 close-on-exec flag true, we could just do a real fcntl
1414 /* F_SETFD. Just ignore attempts to set the close-on-exec
1420 case TARGET_SYS_uname:
1422 /* Fill in a few constants to appease glibc. */
1423 static const char sim_utsname[6][65] =
1433 if ((s.write_mem) (cb, &s, arg1, (const char *) sim_utsname,
1434 sizeof (sim_utsname))
1435 != sizeof (sim_utsname))
1436 retval = -cb_host_to_target_errno (cb, EFAULT);
1442 case TARGET_SYS_geteuid32:
1443 /* We tell the truth with these. Maybe we shouldn't, but it
1444 should match the "stat" information. */
1445 retval = geteuid ();
1448 case TARGET_SYS_getuid32:
1452 case TARGET_SYS_getegid32:
1453 retval = getegid ();
1456 case TARGET_SYS_getgid32:
1460 case TARGET_SYS_brk:
1461 /* Most often, we just return the argument, like the Linux
1466 retval = current_cpu->endbrk;
1467 else if (arg1 <= current_cpu->endmem)
1468 current_cpu->endbrk = arg1;
1471 USI new_end = (arg1 + 8191) & ~8191;
1473 /* If the simulator wants to brk more than a certain very
1474 large amount, something is wrong. FIXME: Return an error
1475 or abort? Have command-line selectable? */
1476 if (new_end - current_cpu->endmem > SIM_MAX_ALLOC_CHUNK)
1478 current_cpu->endbrk = current_cpu->endmem;
1479 retval = current_cpu->endmem;
1483 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1484 current_cpu->endmem,
1485 new_end - current_cpu->endmem,
1487 current_cpu->endbrk = arg1;
1488 current_cpu->endmem = new_end;
1492 case TARGET_SYS_getpid:
1493 /* Correct until CLONE_THREAD is implemented. */
1494 retval = current_cpu->thread_data == NULL
1496 : TARGET_PID + current_cpu->thread_data[threadno].threadid;
1499 case TARGET_SYS_getppid:
1500 /* Correct until CLONE_THREAD is implemented. */
1501 retval = current_cpu->thread_data == NULL
1504 + current_cpu->thread_data[threadno].parent_threadid);
1507 case TARGET_SYS_mmap2:
1516 /* If the simulator wants to mmap more than the very large
1517 limit, something is wrong. FIXME: Return an error or
1518 abort? Have command-line selectable? */
1519 if (len > SIM_MAX_ALLOC_CHUNK)
1521 retval = -cb_host_to_target_errno (cb, ENOMEM);
1525 if ((prot != (TARGET_PROT_READ | TARGET_PROT_WRITE)
1527 != (TARGET_PROT_READ
1529 | TARGET_PROT_EXEC))
1530 && prot != TARGET_PROT_READ)
1531 || (flags != (TARGET_MAP_ANONYMOUS | TARGET_MAP_PRIVATE)
1532 && flags != TARGET_MAP_PRIVATE
1533 && flags != TARGET_MAP_SHARED)
1534 || (fd != (USI) -1 && prot != TARGET_PROT_READ)
1537 sim_io_eprintf (sd, "Unimplemented mmap2 call "
1538 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1539 (unsigned long) arg1,
1540 (unsigned long) arg2,
1541 (unsigned long) arg3,
1542 (unsigned long) arg4,
1543 (unsigned long) arg5,
1544 (unsigned long) arg6);
1545 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1549 else if (fd != (USI) -1)
1556 /* A non-aligned argument is allowed for files. */
1557 USI newlen = (len + 8191) & ~8191;
1559 /* We only support read, which we should already have
1560 checked. Check again anyway. */
1561 if (prot != TARGET_PROT_READ)
1565 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1568 if (newaddr >= (USI) -8191)
1571 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1575 /* Find the current position in the file. */
1576 s.func = TARGET_SYS_lseek;
1580 if (cb_syscall (cb, &s) != CB_RC_OK)
1587 /* Use the standard read callback to read in "len"
1589 s.func = TARGET_SYS_read;
1593 if (cb_syscall (cb, &s) != CB_RC_OK)
1596 if ((USI) s.result != len)
1599 /* After reading, we need to go back to the previous
1600 position in the file. */
1601 s.func = TARGET_SYS_lseek;
1605 if (cb_syscall (cb, &s) != CB_RC_OK)
1607 if (pos != (USI) s.result)
1615 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1616 (len + 8191) & ~8191);
1618 if (newaddr >= (USI) -8191)
1619 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1626 case TARGET_SYS_mprotect:
1628 /* We only cover the case of linuxthreads mprotecting out its
1629 stack guard page. */
1634 if ((addr & 8191) != 0
1636 || prot != TARGET_PROT_NONE
1637 || !is_mapped (sd, ¤t_cpu->highest_mmapped_page, addr,
1640 sim_io_eprintf (sd, "Unimplemented mprotect call "
1641 "(0x%lx, 0x%lx, 0x%lx)\n",
1642 (unsigned long) arg1,
1643 (unsigned long) arg2,
1644 (unsigned long) arg3);
1645 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1650 /* FIXME: We should account for pages like this that are
1651 "mprotected out". For now, we just tell the simulator
1652 core to remove that page from its map. */
1653 sim_core_detach (sd, NULL, 0, 0, addr);
1658 case TARGET_SYS_ioctl:
1660 /* We support only a very limited functionality: checking
1661 stdout with TCGETS to perform the isatty function. The
1662 TCGETS ioctl isn't actually performed or the result used by
1663 an isatty () caller in a "hello, world" program; only the
1664 return value is then used. Maybe we shouldn't care about
1665 the environment of the simulator regarding isatty, but
1666 that's been working before, in the xsim simulator. */
1667 if (arg2 == TARGET_TCGETS && arg1 == 1)
1668 retval = isatty (1) ? 0 : cb_host_to_target_errno (cb, EINVAL);
1670 retval = -cb_host_to_target_errno (cb, EINVAL);
1674 case TARGET_SYS_munmap:
1679 = unmap_pages (sd, ¤t_cpu->highest_mmapped_page, addr,
1681 retval = result != 0 ? -cb_host_to_target_errno (cb, result) : 0;
1685 case TARGET_SYS_wait4:
1693 /* FIXME: We're not properly implementing __WCLONE, and we
1694 don't really need the special casing so we might as well
1695 make this general. */
1696 if ((!(pid == (USI) -1
1697 && options == (TARGET___WCLONE | TARGET_WNOHANG)
1700 && (options == TARGET___WCLONE
1701 || options == TARGET___WALL)))
1703 || current_cpu->thread_data == NULL)
1705 sim_io_eprintf (sd, "Unimplemented wait4 call "
1706 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1707 (unsigned long) arg1,
1708 (unsigned long) arg2,
1709 (unsigned long) arg3,
1710 (unsigned long) arg4);
1711 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1716 if (pid == (USI) -1)
1717 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1719 if (current_cpu->thread_data[threadno].threadid
1720 == current_cpu->thread_data[i].parent_threadid
1721 && current_cpu->thread_data[i].threadid != 0
1722 && current_cpu->thread_data[i].cpu_context == NULL)
1724 /* A zombied child. Get the exit value and clear the
1725 zombied entry so it will be reused. */
1726 sim_core_write_unaligned_4 (current_cpu, pc, 0, saddr,
1728 ->thread_data[i].exitval);
1730 = current_cpu->thread_data[i].threadid + TARGET_PID;
1731 memset (¤t_cpu->thread_data[i], 0,
1732 sizeof (current_cpu->thread_data[i]));
1738 /* We're waiting for a specific PID. If we don't find
1739 it zombied on this run, rerun the syscall. */
1740 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1741 if (pid == current_cpu->thread_data[i].threadid + TARGET_PID
1742 && current_cpu->thread_data[i].cpu_context == NULL)
1745 /* Get the exit value if the caller wants it. */
1746 sim_core_write_unaligned_4 (current_cpu, pc, 0,
1753 = current_cpu->thread_data[i].threadid + TARGET_PID;
1754 memset (¤t_cpu->thread_data[i], 0,
1755 sizeof (current_cpu->thread_data[i]));
1760 sim_pc_set (current_cpu, pc);
1763 retval = -cb_host_to_target_errno (cb, ECHILD);
1768 case TARGET_SYS_rt_sigaction:
1776 __sighandler_t sa_handler;
1777 unsigned long sa_flags;
1778 void (*sa_restorer)(void);
1784 sim_core_write_unaligned_4 (current_cpu, pc, 0, old_sa + 0,
1785 current_cpu->sighandler[signum]);
1786 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 4, 0);
1787 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 8, 0);
1789 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
1790 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 12, 0);
1791 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 16, 0);
1795 USI target_sa_handler
1796 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa);
1798 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 4);
1799 USI target_sa_restorer
1800 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 8);
1801 USI target_sa_mask_low
1802 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 12);
1803 USI target_sa_mask_high
1804 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 16);
1806 /* We won't interrupt a syscall so we won't restart it,
1807 but a signal(2) call ends up syscalling rt_sigaction
1808 with this flag, so we have to handle it. The
1809 sa_restorer field contains garbage when not
1810 TARGET_SA_RESTORER, so don't look at it. For the
1811 time being, we don't nest sighandlers, so we
1812 ignore the sa_mask, which simplifies things. */
1813 if ((target_sa_flags != 0
1814 && target_sa_flags != TARGET_SA_RESTART
1815 && target_sa_flags != (TARGET_SA_RESTART|TARGET_SA_SIGINFO))
1816 || target_sa_handler == 0)
1818 sim_io_eprintf (sd, "Unimplemented rt_sigaction "
1820 "0x%lx: [0x%x, 0x%x, 0x%x, "
1823 (unsigned long) arg1,
1824 (unsigned long) arg2,
1825 target_sa_handler, target_sa_flags,
1827 target_sa_mask_low, target_sa_mask_high,
1828 (unsigned long) arg3);
1829 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1833 current_cpu->sighandler[signum] = target_sa_handler;
1835 /* Because we may have unblocked signals, one may now be
1836 pending, if there are threads, that is. */
1837 if (current_cpu->thread_data)
1838 current_cpu->thread_data[threadno].sigpending = 1;
1844 case TARGET_SYS_mremap:
1850 USI new_addr = arg5;
1853 if (new_len == old_len)
1854 /* The program and/or library is possibly confused but
1855 this is a valid call. Happens with ipps-1.40 on file
1858 else if (new_len < old_len)
1860 /* Shrinking is easy. */
1861 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1862 addr + new_len, old_len - new_len) != 0)
1863 retval = -cb_host_to_target_errno (cb, EINVAL);
1867 else if (! is_mapped (sd, ¤t_cpu->highest_mmapped_page,
1868 addr + old_len, new_len - old_len))
1870 /* If the extension isn't mapped, we can just add it. */
1872 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1873 addr + old_len, new_len - old_len);
1875 if (mapped_addr > (USI) -8192)
1876 retval = -cb_host_to_target_errno (cb, -(SI) mapped_addr);
1880 else if (flags & TARGET_MREMAP_MAYMOVE)
1882 /* Create a whole new map and copy the contents
1883 block-by-block there. We ignore the new_addr argument
1886 USI prev_addr = addr;
1887 USI prev_len = old_len;
1890 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1893 if (mapped_addr > (USI) -8192)
1895 retval = -cb_host_to_target_errno (cb, -(SI) new_addr);
1899 retval = mapped_addr;
1902 old_len -= 8192, mapped_addr += 8192, addr += 8192)
1904 if (sim_core_read_buffer (sd, current_cpu, read_map, buf,
1906 || sim_core_write_buffer (sd, current_cpu, 0, buf,
1907 mapped_addr, 8192) != 8192)
1911 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1912 prev_addr, prev_len) != 0)
1916 retval = -cb_host_to_target_errno (cb, -ENOMEM);
1920 case TARGET_SYS_poll:
1922 int npollfds = arg2;
1938 /* Check that this is the expected poll call from
1939 linuxthreads/manager.c; we don't support anything else.
1940 Remember, fd == 0 isn't supported. */
1942 || ((fd = sim_core_read_unaligned_4 (current_cpu, pc,
1944 || ((events = sim_core_read_unaligned_2 (current_cpu, pc,
1947 || ((cb->fstat) (cb, fd, &buf) != 0
1948 || (buf.st_mode & S_IFIFO) == 0)
1949 || current_cpu->thread_data == NULL)
1951 sim_io_eprintf (sd, "Unimplemented poll syscall "
1952 "(0x%lx: [0x%x, 0x%x, x], 0x%lx, 0x%lx)\n",
1953 (unsigned long) arg1, fd, events,
1954 (unsigned long) arg2, (unsigned long) arg3);
1955 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1961 /* Iterate over threads; find a marker that a writer is
1962 sleeping, waiting for a reader. */
1963 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1964 if (current_cpu->thread_data[i].cpu_context != NULL
1965 && current_cpu->thread_data[i].pipe_read_fd == fd)
1967 revents = TARGET_POLLIN;
1972 /* Timeout decreases with whatever time passed between the
1973 last syscall and this. That's not exactly right for the
1974 first call, but it's close enough that it isn't
1975 worthwhile to complicate matters by making that a special
1978 -= (TARGET_TIME_MS (current_cpu)
1979 - (current_cpu->thread_data[threadno].last_execution));
1981 /* Arrange to repeat this syscall until timeout or event,
1982 decreasing timeout at each iteration. */
1983 if (timeout > 0 && revents == 0)
1985 bfd_byte timeout_buf[4];
1987 bfd_putl32 (timeout, timeout_buf);
1988 (*CPU_REG_STORE (current_cpu)) (current_cpu,
1989 H_GR_R12, timeout_buf, 4);
1990 sim_pc_set (current_cpu, pc);
1995 sim_core_write_unaligned_2 (current_cpu, pc, 0, ufds + 4 + 2,
2000 case TARGET_SYS_gettimeofday:
2003 USI ts = TARGET_TIME (current_cpu);
2004 USI tms = TARGET_TIME_MS (current_cpu);
2006 /* First dword is seconds since TARGET_EPOCH. */
2007 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, ts);
2009 /* Second dword is microseconds. */
2010 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1 + 4,
2011 (tms % 1000) * 1000);
2015 /* Time-zone info is always cleared. */
2016 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, 0);
2017 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, 0);
2022 case TARGET_SYS_llseek:
2024 /* If it fits, tweak parameters to fit the "generic" 32-bit
2025 lseek and use that. */
2033 if (!((offs_hi == 0 && offs_lo >= 0)
2034 || (offs_hi == -1 && offs_lo < 0)))
2037 "Unimplemented llseek offset,"
2038 " fd %d: 0x%x:0x%x\n",
2039 fd, (unsigned) arg2, (unsigned) arg3);
2040 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2044 s.func = TARGET_SYS_lseek;
2047 if (cb_syscall (cb, &s) != CB_RC_OK)
2049 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
2051 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2054 retval = -s.errcode;
2057 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp,
2059 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp + 4,
2060 s.result < 0 ? -1 : 0);
2065 /* This one does have a generic callback function, but at the time
2066 of this writing, cb_syscall does not have code for it, and we
2067 need target-specific code for the threads implementation
2069 case TARGET_SYS_kill:
2076 /* At kill(2), glibc sets signal masks such that the thread
2077 machinery is initialized. Still, there is and was only
2079 if (current_cpu->max_threadid == 0)
2081 if (pid != TARGET_PID)
2083 retval = -cb_host_to_target_errno (cb, EPERM);
2087 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2088 if (sig == TARGET_SIGABRT)
2089 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2090 the end-point for failing GCC test-cases. */
2091 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2095 sim_io_eprintf (sd, "Unimplemented signal: %d\n", sig);
2096 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2100 /* This will not be reached. */
2104 retval = deliver_signal (current_cpu, sig, pid);
2108 case TARGET_SYS_rt_sigprocmask:
2115 if (how != TARGET_SIG_BLOCK
2116 && how != TARGET_SIG_SETMASK
2117 && how != TARGET_SIG_UNBLOCK)
2119 sim_io_eprintf (sd, "Unimplemented rt_sigprocmask syscall "
2120 "(0x%x, 0x%x, 0x%x)\n", arg1, arg2, arg3);
2121 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2130 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2133 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2136 /* The sigmask is kept in the per-thread data, so we may
2137 need to create the first one. */
2138 if (current_cpu->thread_data == NULL)
2139 make_first_thread (current_cpu);
2141 if (how == TARGET_SIG_SETMASK)
2142 for (i = 0; i < 64; i++)
2143 current_cpu->thread_data[threadno].sigdata[i].blocked = 0;
2145 for (i = 0; i < 32; i++)
2146 if ((set_low & (1 << i)))
2147 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2148 = (how != TARGET_SIG_UNBLOCK);
2150 for (i = 0; i < 31; i++)
2151 if ((set_high & (1 << i)))
2152 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2153 = (how != TARGET_SIG_UNBLOCK);
2155 /* The mask changed, so a signal may be unblocked for
2157 current_cpu->thread_data[threadno].sigpending = 1;
2165 for (i = 0; i < 32; i++)
2166 if (current_cpu->thread_data[threadno]
2167 .sigdata[i + 1].blocked)
2169 for (i = 0; i < 31; i++)
2170 if (current_cpu->thread_data[threadno]
2171 .sigdata[i + 33].blocked)
2174 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 0, set_low);
2175 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 4, set_high);
2182 case TARGET_SYS_sigreturn:
2186 int was_sigsuspended;
2188 if (current_cpu->thread_data == NULL
2189 /* The CPU context is saved with the simulator data, not
2190 on the stack as in the real world. */
2191 || (current_cpu->thread_data[threadno].cpu_context_atsignal
2194 sim_io_eprintf (sd, "Invalid sigreturn syscall: no signal"
2196 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2197 (unsigned long) arg1,
2198 (unsigned long) arg2,
2199 (unsigned long) arg3,
2200 (unsigned long) arg4,
2201 (unsigned long) arg5,
2202 (unsigned long) arg6);
2203 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2208 = current_cpu->thread_data[threadno].sigsuspended;
2210 /* Restore the sigmask, either from the stack copy made when
2211 the sighandler was called, or from the saved state
2212 specifically for sigsuspend(2). */
2213 if (was_sigsuspended)
2215 current_cpu->thread_data[threadno].sigsuspended = 0;
2216 for (i = 0; i < 64; i++)
2217 current_cpu->thread_data[threadno].sigdata[i].blocked
2218 = current_cpu->thread_data[threadno]
2219 .sigdata[i].blocked_suspendsave;
2227 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2228 H_GR_SP, regbuf, 4);
2229 sp = bfd_getl32 (regbuf);
2231 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp);
2233 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp + 4);
2235 for (i = 0; i < 32; i++)
2236 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2237 = (set_low & (1 << i)) != 0;
2238 for (i = 0; i < 31; i++)
2239 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2240 = (set_high & (1 << i)) != 0;
2243 /* The mask changed, so a signal may be unblocked for
2245 current_cpu->thread_data[threadno].sigpending = 1;
2247 memcpy (¤t_cpu->cpu_data_placeholder,
2248 current_cpu->thread_data[threadno].cpu_context_atsignal,
2249 current_cpu->thread_cpu_data_size);
2250 free (current_cpu->thread_data[threadno].cpu_context_atsignal);
2251 current_cpu->thread_data[threadno].cpu_context_atsignal = NULL;
2253 /* The return value must come from the saved R10. */
2254 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, regbuf, 4);
2255 retval = bfd_getl32 (regbuf);
2257 /* We must also break the "sigsuspension loop". */
2258 if (was_sigsuspended)
2259 sim_pc_set (current_cpu, sim_pc_get (current_cpu) + 2);
2263 case TARGET_SYS_rt_sigsuspend:
2270 sim_io_eprintf (sd, "Unimplemented rt_sigsuspend syscall"
2271 " arguments (0x%lx, 0x%lx)\n",
2272 (unsigned long) arg1, (unsigned long) arg2);
2273 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2277 /* Don't change the signal mask if we're already in
2278 sigsuspend state (i.e. this syscall is a rerun). */
2279 else if (!current_cpu->thread_data[threadno].sigsuspended)
2282 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2285 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2289 /* Save the current sigmask and insert the user-supplied
2291 for (i = 0; i < 32; i++)
2293 current_cpu->thread_data[threadno]
2294 .sigdata[i + 1].blocked_suspendsave
2295 = current_cpu->thread_data[threadno]
2296 .sigdata[i + 1].blocked;
2298 current_cpu->thread_data[threadno]
2299 .sigdata[i + 1].blocked = (set_low & (1 << i)) != 0;
2301 for (i = 0; i < 31; i++)
2303 current_cpu->thread_data[threadno]
2304 .sigdata[i + 33].blocked_suspendsave
2305 = current_cpu->thread_data[threadno]
2306 .sigdata[i + 33].blocked;
2307 current_cpu->thread_data[threadno]
2308 .sigdata[i + 33].blocked = (set_high & (1 << i)) != 0;
2311 current_cpu->thread_data[threadno].sigsuspended = 1;
2313 /* The mask changed, so a signal may be unblocked for
2315 current_cpu->thread_data[threadno].sigpending = 1;
2318 /* Because we don't use arg1 (newsetp) when this syscall is
2319 rerun, it doesn't matter that we overwrite it with the
2320 (constant) return value. */
2321 retval = -cb_host_to_target_errno (cb, EINTR);
2322 sim_pc_set (current_cpu, pc);
2326 /* Add case labels here for other syscalls using the 32-bit
2327 "struct stat", provided they have a corresponding simulator
2328 function of course. */
2329 case TARGET_SYS_stat:
2330 case TARGET_SYS_fstat:
2332 /* As long as the infrastructure doesn't cache anything
2333 related to the stat mapping, this trick gets us a dual
2334 "struct stat"-type mapping in the least error-prone way. */
2335 const char *saved_map = cb->stat_map;
2336 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
2338 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_stat32_map;
2339 cb->stat_map = stat32_map;
2341 if (cb_syscall (cb, &s) != CB_RC_OK)
2344 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2347 retval = s.result == -1 ? -s.errcode : s.result;
2349 cb->stat_map = saved_map;
2350 cb->syscall_map = saved_syscall_map;
2354 case TARGET_SYS_getcwd:
2359 char *cwd = xmalloc (MAXPATHLEN);
2360 if (cwd != getcwd (cwd, MAXPATHLEN))
2363 /* FIXME: When and if we support chdir, we need something
2364 a bit more elaborate. */
2365 if (simulator_sysroot[0] != '\0')
2368 retval = -cb_host_to_target_errno (cb, ERANGE);
2369 if (strlen (cwd) + 1 <= size)
2371 retval = strlen (cwd) + 1;
2372 if (sim_core_write_buffer (sd, current_cpu, 0, cwd,
2374 != (unsigned int) retval)
2375 retval = -cb_host_to_target_errno (cb, EFAULT);
2381 case TARGET_SYS_readlink:
2386 char *pbuf = xmalloc (MAXPATHLEN);
2387 char *lbuf = xmalloc (MAXPATHLEN);
2388 char *lbuf_alloc = lbuf;
2393 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2395 strcpy (pbuf, simulator_sysroot);
2396 o += strlen (simulator_sysroot);
2399 for (i = 0; i + o < MAXPATHLEN; i++)
2402 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2403 if (pbuf[i + o] == 0)
2407 if (i + o == MAXPATHLEN)
2409 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2413 /* Intervene calls for certain files expected in the target
2414 proc file system. */
2415 if (strcmp (pbuf + strlen (simulator_sysroot),
2416 "/proc/" XSTRING (TARGET_PID) "/exe") == 0)
2419 = (STATE_PROG_ARGV (sd) != NULL
2420 ? *STATE_PROG_ARGV (sd) : NULL);
2422 if (argv0 == NULL || *argv0 == '.')
2424 sim_io_eprintf (sd, "Unimplemented readlink syscall "
2425 "(0x%lx: [\"%s\"], 0x%lx)\n",
2426 (unsigned long) arg1, pbuf,
2427 (unsigned long) arg2);
2428 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2432 else if (*argv0 == '/')
2434 if (strncmp (simulator_sysroot, argv0,
2435 strlen (simulator_sysroot)) == 0)
2436 argv0 += strlen (simulator_sysroot);
2438 strcpy (lbuf, argv0);
2439 nchars = strlen (argv0) + 1;
2443 if (getcwd (lbuf, MAXPATHLEN) != NULL
2444 && strlen (lbuf) + 2 + strlen (argv0) < MAXPATHLEN)
2446 if (strncmp (simulator_sysroot, lbuf,
2447 strlen (simulator_sysroot)) == 0)
2448 lbuf += strlen (simulator_sysroot);
2451 strcat (lbuf, argv0);
2452 nchars = strlen (lbuf) + 1;
2459 nchars = readlink (pbuf, lbuf, MAXPATHLEN);
2461 /* We trust that the readlink result returns a *relative*
2462 link, or one already adjusted for the file-path-prefix.
2463 (We can't generally tell the difference, so we go with
2464 the easiest decision; no adjustment.) */
2468 retval = -cb_host_to_target_errno (cb, errno);
2472 if (bufsiz < nchars)
2475 if (sim_core_write_buffer (sd, current_cpu, write_map, lbuf,
2476 buf, nchars) != (unsigned int) nchars)
2477 retval = -cb_host_to_target_errno (cb, EFAULT);
2486 case TARGET_SYS_sched_getscheduler:
2490 /* FIXME: Search (other) existing threads. */
2491 if (pid != 0 && pid != TARGET_PID)
2492 retval = -cb_host_to_target_errno (cb, ESRCH);
2494 retval = TARGET_SCHED_OTHER;
2498 case TARGET_SYS_sched_getparam:
2504 struct sched_param {
2508 if (pid != 0 && pid != TARGET_PID)
2509 retval = -cb_host_to_target_errno (cb, ESRCH);
2512 /* FIXME: Save scheduler setting before threads are
2514 sim_core_write_unaligned_4 (current_cpu, pc, 0, paramp,
2515 current_cpu->thread_data != NULL
2517 ->thread_data[threadno]
2525 case TARGET_SYS_sched_setparam:
2530 if ((pid != 0 && pid != TARGET_PID)
2531 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2533 retval = -cb_host_to_target_errno (cb, EINVAL);
2539 case TARGET_SYS_sched_setscheduler:
2545 if ((pid != 0 && pid != TARGET_PID)
2546 || policy != TARGET_SCHED_OTHER
2547 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2549 retval = -cb_host_to_target_errno (cb, EINVAL);
2551 /* FIXME: Save scheduler setting to be read in later
2552 sched_getparam calls. */
2557 case TARGET_SYS_sched_yield:
2558 /* We reschedule to the next thread after a syscall anyway, so
2559 we don't have to do anything here than to set the return
2564 case TARGET_SYS_sched_get_priority_min:
2565 case TARGET_SYS_sched_get_priority_max:
2567 retval = -cb_host_to_target_errno (cb, EINVAL);
2572 case TARGET_SYS_ugetrlimit:
2574 unsigned int curlim, maxlim;
2575 if (arg1 != TARGET_RLIMIT_STACK && arg1 != TARGET_RLIMIT_NOFILE)
2577 retval = -cb_host_to_target_errno (cb, EINVAL);
2583 unsigned long rlim_cur;
2584 unsigned long rlim_max;
2586 if (arg1 == TARGET_RLIMIT_NOFILE)
2588 /* Sadly a very low limit. Better not lie, though. */
2589 maxlim = curlim = MAX_CALLBACK_FDS;
2591 else /* arg1 == TARGET_RLIMIT_STACK */
2593 maxlim = 0xffffffff;
2596 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, curlim);
2597 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, maxlim);
2602 case TARGET_SYS_setrlimit:
2603 if (arg1 != TARGET_RLIMIT_STACK)
2605 retval = -cb_host_to_target_errno (cb, EINVAL);
2608 /* FIXME: Save values for future ugetrlimit calls. */
2612 /* Provide a very limited subset of the sysctl functions, and
2613 abort for the rest. */
2614 case TARGET_SYS__sysctl:
2617 struct __sysctl_args {
2624 unsigned long __unused[4];
2626 SI name = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1);
2627 SI name0 = name == 0
2628 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name);
2629 SI name1 = name == 0
2630 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name + 4);
2632 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 4);
2634 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 8);
2636 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 12);
2637 SI oldlen = oldlenp == 0
2638 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, oldlenp);
2640 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 16);
2642 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 20);
2644 if (name0 == TARGET_CTL_KERN && name1 == TARGET_CTL_KERN_VERSION)
2646 SI to_write = oldlen < (SI) sizeof (TARGET_UTSNAME)
2647 ? oldlen : (SI) sizeof (TARGET_UTSNAME);
2649 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldlenp,
2650 sizeof (TARGET_UTSNAME));
2652 if (sim_core_write_buffer (sd, current_cpu, write_map,
2653 TARGET_UTSNAME, oldval,
2655 != (unsigned int) to_write)
2656 retval = -cb_host_to_target_errno (cb, EFAULT);
2662 sim_io_eprintf (sd, "Unimplemented _sysctl syscall "
2663 "(0x%lx: [0x%lx, 0x%lx],"
2664 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2665 (unsigned long) name,
2666 (unsigned long) name0,
2667 (unsigned long) name1,
2668 (unsigned long) nlen,
2669 (unsigned long) oldval,
2670 (unsigned long) oldlenp,
2671 (unsigned long) newval,
2672 (unsigned long) newlen);
2673 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2678 case TARGET_SYS_exit:
2680 /* Here for all but the last thread. */
2683 = current_cpu->thread_data[threadno].threadid + TARGET_PID;
2685 = (current_cpu->thread_data[threadno].parent_threadid
2687 int exitsig = current_cpu->thread_data[threadno].exitsig;
2689 /* Any children are now all orphans. */
2690 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2691 if (current_cpu->thread_data[i].parent_threadid
2692 == current_cpu->thread_data[threadno].threadid)
2693 /* Make getppid(2) return 1 for them, poor little ones. */
2694 current_cpu->thread_data[i].parent_threadid = -TARGET_PID + 1;
2696 /* Free the cpu context data. When the parent has received
2697 the exit status, we'll clear the entry too. */
2698 free (current_cpu->thread_data[threadno].cpu_context);
2699 current_cpu->thread_data[threadno].cpu_context = NULL;
2700 current_cpu->m1threads--;
2703 sim_io_eprintf (sd, "Thread %d exited with status %d\n",
2705 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2709 /* Still, we may want to support non-zero exit values. */
2710 current_cpu->thread_data[threadno].exitval = arg1 << 8;
2713 deliver_signal (current_cpu, exitsig, ppid);
2717 case TARGET_SYS_clone:
2719 int nthreads = current_cpu->m1threads + 1;
2720 void *thread_cpu_data;
2721 bfd_byte old_sp_buf[4];
2723 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2726 /* That's right, the syscall clone arguments are reversed
2727 compared to sys_clone notes in clone(2) and compared to
2728 other Linux ports (i.e. it's the same order as in the
2729 clone(2) libcall). */
2733 if (nthreads == SIM_TARGET_MAX_THREADS)
2735 retval = -cb_host_to_target_errno (cb, EAGAIN);
2739 /* FIXME: Implement the low byte. */
2740 if ((flags & ~TARGET_CSIGNAL) !=
2743 | TARGET_CLONE_FILES
2744 | TARGET_CLONE_SIGHAND)
2748 "Unimplemented clone syscall (0x%lx, 0x%lx)\n",
2749 (unsigned long) arg1, (unsigned long) arg2);
2750 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2754 if (current_cpu->thread_data == NULL)
2755 make_first_thread (current_cpu);
2757 /* The created thread will get the new SP and a cleared R10.
2758 Since it's created out of a copy of the old thread and we
2759 don't have a set-register-function that just take the
2760 cpu_data as a parameter, we set the childs values first,
2761 and write back or overwrite them in the parent after the
2763 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2764 H_GR_SP, old_sp_buf, 4);
2765 bfd_putl32 (newsp, sp_buf);
2766 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2767 H_GR_SP, sp_buf, 4);
2768 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2769 H_GR_R10, (bfd_byte *) zeros, 4);
2772 ->make_thread_cpu_data) (current_cpu,
2773 ¤t_cpu->cpu_data_placeholder);
2774 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2775 H_GR_SP, old_sp_buf, 4);
2777 retval = ++current_cpu->max_threadid + TARGET_PID;
2779 /* Find an unused slot. After a few threads have been created
2780 and exited, the array is expected to be a bit fragmented.
2781 We don't reuse the first entry, though, that of the
2783 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
2784 if (current_cpu->thread_data[i].cpu_context == NULL
2785 /* Don't reuse a zombied entry. */
2786 && current_cpu->thread_data[i].threadid == 0)
2789 memcpy (¤t_cpu->thread_data[i],
2790 ¤t_cpu->thread_data[threadno],
2791 sizeof (current_cpu->thread_data[i]));
2792 current_cpu->thread_data[i].cpu_context = thread_cpu_data;
2793 current_cpu->thread_data[i].cpu_context_atsignal = NULL;
2794 current_cpu->thread_data[i].threadid = current_cpu->max_threadid;
2795 current_cpu->thread_data[i].parent_threadid
2796 = current_cpu->thread_data[threadno].threadid;
2797 current_cpu->thread_data[i].pipe_read_fd = 0;
2798 current_cpu->thread_data[i].pipe_write_fd = 0;
2799 current_cpu->thread_data[i].at_syscall = 0;
2800 current_cpu->thread_data[i].sigpending = 0;
2801 current_cpu->thread_data[i].sigsuspended = 0;
2802 current_cpu->thread_data[i].exitsig = flags & TARGET_CSIGNAL;
2803 current_cpu->m1threads = nthreads;
2807 /* Better watch these in case they do something necessary. */
2808 case TARGET_SYS_socketcall:
2809 retval = -cb_host_to_target_errno (cb, ENOSYS);
2812 unimplemented_syscall:
2814 sim_io_eprintf (sd, "Unimplemented syscall: %d "
2815 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n", callnum,
2816 arg1, arg2, arg3, arg4, arg5, arg6);
2817 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2822 /* A system call is a rescheduling point. For the time being, we don't
2823 reschedule anywhere else. */
2824 if (current_cpu->m1threads != 0
2825 /* We need to schedule off from an exiting thread that is the
2827 || (current_cpu->thread_data != NULL
2828 && current_cpu->thread_data[threadno].cpu_context == NULL))
2830 bfd_byte retval_buf[4];
2832 current_cpu->thread_data[threadno].last_execution
2833 = TARGET_TIME_MS (current_cpu);
2834 bfd_putl32 (retval, retval_buf);
2835 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2837 current_cpu->thread_data[threadno].at_syscall = 1;
2838 reschedule (current_cpu);
2840 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2841 retval = bfd_getl32 (retval_buf);
2847 /* Callback from simulator write saying that the pipe at (reader, writer)
2848 is now non-empty (so the writer should wait until the pipe is empty, at
2849 least not write to this or any other pipe). Simplest is to just wait
2850 until the pipe is empty. */
2853 cris_pipe_nonempty (host_callback *cb ATTRIBUTE_UNUSED,
2854 int reader, int writer)
2856 SIM_CPU *cpu = current_cpu_for_cb_callback;
2857 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2859 /* It's the current thread: we just have to re-run the current
2860 syscall instruction (presumably "break 13") and change the syscall
2861 to the special simulator-wait code. Oh, and set a marker that
2862 we're waiting, so we can disambiguate the special call from a
2865 This function may be called multiple times between cris_pipe_empty,
2866 but we must avoid e.g. decreasing PC every time. Check fd markers
2868 if (cpu->thread_data == NULL)
2870 sim_io_eprintf (CPU_STATE (cpu),
2871 "Terminating simulation due to writing pipe rd:wr %d:%d"
2872 " from one single thread\n", reader, writer);
2873 sim_engine_halt (CPU_STATE (cpu), cpu,
2874 NULL, sim_pc_get (cpu), sim_stopped, SIM_SIGILL);
2876 else if (cpu->thread_data[cpu->threadno].pipe_write_fd == 0)
2878 cpu->thread_data[cpu->threadno].pipe_write_fd = writer;
2879 cpu->thread_data[cpu->threadno].pipe_read_fd = reader;
2880 /* FIXME: We really shouldn't change registers other than R10 in
2881 syscalls (like R9), here or elsewhere. */
2882 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R9, (bfd_byte *) zeros, 4);
2883 sim_pc_set (cpu, sim_pc_get (cpu) - 2);
2887 /* Callback from simulator close or read call saying that the pipe at
2888 (reader, writer) is now empty (so the writer can write again, perhaps
2889 leave a waiting state). If there are bytes remaining, they couldn't be
2890 consumed (perhaps due to the pipe closing). */
2893 cris_pipe_empty (host_callback *cb,
2894 int reader ATTRIBUTE_UNUSED,
2898 SIM_CPU *cpu = current_cpu_for_cb_callback;
2899 bfd_byte r10_buf[4];
2900 int remaining = cb->pipe_buffer[writer].size;
2902 /* We need to find the thread that waits for this pipe. */
2903 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2904 if (cpu->thread_data[i].cpu_context
2905 && cpu->thread_data[i].pipe_write_fd == writer)
2908 /* Temporarily switch to this cpu context, so we can change the
2909 PC by ordinary calls. */
2911 memcpy (cpu->thread_data[cpu->threadno].cpu_context,
2912 &cpu->cpu_data_placeholder,
2913 cpu->thread_cpu_data_size);
2914 memcpy (&cpu->cpu_data_placeholder,
2915 cpu->thread_data[i].cpu_context,
2916 cpu->thread_cpu_data_size);
2918 /* The return value is supposed to contain the number of written
2919 bytes, which is the number of bytes requested and returned at
2920 the write call. We subtract the remaining bytes from that,
2921 but making sure we still get a positive number.
2922 The return value may also be a negative number; an error
2923 value. We cover this case by comparing against remaining,
2924 which is always >= 0. */
2925 (*CPU_REG_FETCH (cpu)) (cpu, H_GR_R10, r10_buf, 4);
2926 retval = (int) bfd_getl_signed_32 (r10_buf);
2927 if (retval >= remaining)
2928 bfd_putl32 (retval - remaining, r10_buf);
2929 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R10, r10_buf, 4);
2931 sim_pc_set (cpu, sim_pc_get (cpu) + 2);
2932 memcpy (cpu->thread_data[i].cpu_context,
2933 &cpu->cpu_data_placeholder,
2934 cpu->thread_cpu_data_size);
2935 memcpy (&cpu->cpu_data_placeholder,
2936 cpu->thread_data[cpu->threadno].cpu_context,
2937 cpu->thread_cpu_data_size);
2938 cpu->thread_data[i].pipe_read_fd = 0;
2939 cpu->thread_data[i].pipe_write_fd = 0;
2946 /* We have a simulator-specific notion of time. See TARGET_TIME. */
2949 cris_time (host_callback *cb ATTRIBUTE_UNUSED, long *t)
2951 long retval = TARGET_TIME (current_cpu_for_cb_callback);
2957 /* Set target-specific callback data. */
2960 cris_set_callbacks (host_callback *cb)
2962 /* Yeargh, have to cast away constness to avoid warnings. */
2963 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
2964 cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
2966 /* The kernel stat64 layout. If we see a file > 2G, the "long"
2967 parameter to cb_store_target_endian will make st_size negative.
2968 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
2969 *unsigned*, and/or add syntax for signed-ness. */
2970 cb->stat_map = stat_map;
2971 cb->open_map = (CB_TARGET_DEFS_MAP *) open_map;
2972 cb->pipe_nonempty = cris_pipe_nonempty;
2973 cb->pipe_empty = cris_pipe_empty;
2974 cb->time = cris_time;
2977 /* Process an address exception. */
2980 cris_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
2981 unsigned int map, int nr_bytes, address_word addr,
2982 transfer_type transfer, sim_core_signals sig)
2984 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,