1 /* CRIS exception, interrupt, and trap (EIT) support
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Axis Communications.
5 This file is part of the GNU simulators.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sim-options.h"
24 /* FIXME: get rid of targ-vals.h usage everywhere else. */
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
38 #ifdef HAVE_SYS_STAT_H
42 /* The verbatim values are from asm-cris/unistd.h. */
44 #define TARGET_SYS_exit 1
45 #define TARGET_SYS_read 3
46 #define TARGET_SYS_write 4
47 #define TARGET_SYS_open 5
48 #define TARGET_SYS_close 6
49 #define TARGET_SYS_unlink 10
50 #define TARGET_SYS_time 13
51 #define TARGET_SYS_lseek 19
52 #define TARGET_SYS_getpid 20
53 #define TARGET_SYS_kill 37
54 #define TARGET_SYS_rename 38
55 #define TARGET_SYS_pipe 42
56 #define TARGET_SYS_brk 45
57 #define TARGET_SYS_ioctl 54
58 #define TARGET_SYS_fcntl 55
59 #define TARGET_SYS_getppid 64
60 #define TARGET_SYS_setrlimit 75
61 #define TARGET_SYS_gettimeofday 78
62 #define TARGET_SYS_readlink 85
63 #define TARGET_SYS_munmap 91
64 #define TARGET_SYS_truncate 92
65 #define TARGET_SYS_ftruncate 93
66 #define TARGET_SYS_socketcall 102
67 #define TARGET_SYS_stat 106
68 #define TARGET_SYS_fstat 108
69 #define TARGET_SYS_wait4 114
70 #define TARGET_SYS_sigreturn 119
71 #define TARGET_SYS_clone 120
72 #define TARGET_SYS_uname 122
73 #define TARGET_SYS_mprotect 125
74 #define TARGET_SYS_llseek 140
75 #define TARGET_SYS__sysctl 149
76 #define TARGET_SYS_sched_setparam 154
77 #define TARGET_SYS_sched_getparam 155
78 #define TARGET_SYS_sched_setscheduler 156
79 #define TARGET_SYS_sched_getscheduler 157
80 #define TARGET_SYS_sched_yield 158
81 #define TARGET_SYS_sched_get_priority_max 159
82 #define TARGET_SYS_sched_get_priority_min 160
83 #define TARGET_SYS_mremap 163
84 #define TARGET_SYS_poll 168
85 #define TARGET_SYS_rt_sigaction 174
86 #define TARGET_SYS_rt_sigprocmask 175
87 #define TARGET_SYS_rt_sigsuspend 179
88 #define TARGET_SYS_getcwd 183
89 #define TARGET_SYS_ugetrlimit 191
90 #define TARGET_SYS_mmap2 192
91 #define TARGET_SYS_stat64 195
92 #define TARGET_SYS_lstat64 196
93 #define TARGET_SYS_fstat64 197
94 #define TARGET_SYS_geteuid32 201
95 #define TARGET_SYS_getuid32 199
96 #define TARGET_SYS_getegid32 202
97 #define TARGET_SYS_getgid32 200
98 #define TARGET_SYS_fcntl64 221
100 #define TARGET_PROT_READ 0x1
101 #define TARGET_PROT_WRITE 0x2
102 #define TARGET_PROT_EXEC 0x4
103 #define TARGET_PROT_NONE 0x0
105 #define TARGET_MAP_SHARED 0x01
106 #define TARGET_MAP_PRIVATE 0x02
107 #define TARGET_MAP_TYPE 0x0f
108 #define TARGET_MAP_FIXED 0x10
109 #define TARGET_MAP_ANONYMOUS 0x20
111 #define TARGET_CTL_KERN 1
112 #define TARGET_CTL_VM 2
113 #define TARGET_CTL_NET 3
114 #define TARGET_CTL_PROC 4
115 #define TARGET_CTL_FS 5
116 #define TARGET_CTL_DEBUG 6
117 #define TARGET_CTL_DEV 7
118 #define TARGET_CTL_BUS 8
119 #define TARGET_CTL_ABI 9
121 #define TARGET_CTL_KERN_VERSION 4
124 #define TARGET_MREMAP_MAYMOVE 1
125 #define TARGET_MREMAP_FIXED 2
127 #define TARGET_TCGETS 0x5401
129 #define TARGET_UTSNAME "#38 Sun Apr 1 00:00:00 MET 2001"
131 /* Seconds since the above date + 10 minutes. */
132 #define TARGET_EPOCH 986080200
134 /* Milliseconds since start of run. We use the number of syscalls to
135 avoid introducing noise in the execution time. */
136 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
138 /* Seconds as in time(2). */
139 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
141 #define TARGET_SCHED_OTHER 0
143 #define TARGET_RLIMIT_STACK 3
144 #define TARGET_RLIMIT_NOFILE 7
146 #define SIM_TARGET_MAX_THREADS 64
147 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
149 /* From linux/sched.h. */
150 #define TARGET_CSIGNAL 0x000000ff
151 #define TARGET_CLONE_VM 0x00000100
152 #define TARGET_CLONE_FS 0x00000200
153 #define TARGET_CLONE_FILES 0x00000400
154 #define TARGET_CLONE_SIGHAND 0x00000800
155 #define TARGET_CLONE_PID 0x00001000
156 #define TARGET_CLONE_PTRACE 0x00002000
157 #define TARGET_CLONE_VFORK 0x00004000
158 #define TARGET_CLONE_PARENT 0x00008000
159 #define TARGET_CLONE_THREAD 0x00010000
160 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
162 /* From asm-cris/poll.h. */
163 #define TARGET_POLLIN 1
165 /* From asm-cris/signal.h. */
166 #define TARGET_SIG_BLOCK 0
167 #define TARGET_SIG_UNBLOCK 1
168 #define TARGET_SIG_SETMASK 2
170 #define TARGET_SIG_DFL 0
171 #define TARGET_SIG_IGN 1
172 #define TARGET_SIG_ERR ((USI)-1)
174 #define TARGET_SIGHUP 1
175 #define TARGET_SIGINT 2
176 #define TARGET_SIGQUIT 3
177 #define TARGET_SIGILL 4
178 #define TARGET_SIGTRAP 5
179 #define TARGET_SIGABRT 6
180 #define TARGET_SIGIOT 6
181 #define TARGET_SIGBUS 7
182 #define TARGET_SIGFPE 8
183 #define TARGET_SIGKILL 9
184 #define TARGET_SIGUSR1 10
185 #define TARGET_SIGSEGV 11
186 #define TARGET_SIGUSR2 12
187 #define TARGET_SIGPIPE 13
188 #define TARGET_SIGALRM 14
189 #define TARGET_SIGTERM 15
190 #define TARGET_SIGSTKFLT 16
191 #define TARGET_SIGCHLD 17
192 #define TARGET_SIGCONT 18
193 #define TARGET_SIGSTOP 19
194 #define TARGET_SIGTSTP 20
195 #define TARGET_SIGTTIN 21
196 #define TARGET_SIGTTOU 22
197 #define TARGET_SIGURG 23
198 #define TARGET_SIGXCPU 24
199 #define TARGET_SIGXFSZ 25
200 #define TARGET_SIGVTALRM 26
201 #define TARGET_SIGPROF 27
202 #define TARGET_SIGWINCH 28
203 #define TARGET_SIGIO 29
204 #define TARGET_SIGPOLL SIGIO
205 /* Actually commented out in the kernel header. */
206 #define TARGET_SIGLOST 29
207 #define TARGET_SIGPWR 30
208 #define TARGET_SIGSYS 31
210 /* From include/asm-cris/signal.h. */
211 #define TARGET_SA_NOCLDSTOP 0x00000001
212 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
213 #define TARGET_SA_SIGINFO 0x00000004
214 #define TARGET_SA_ONSTACK 0x08000000
215 #define TARGET_SA_RESTART 0x10000000
216 #define TARGET_SA_NODEFER 0x40000000
217 #define TARGET_SA_RESETHAND 0x80000000
218 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
219 #define TARGET_SA_RESTORER 0x04000000
221 /* From linux/wait.h. */
222 #define TARGET_WNOHANG 1
223 #define TARGET_WUNTRACED 2
224 #define TARGET___WNOTHREAD 0x20000000
225 #define TARGET___WALL 0x40000000
226 #define TARGET___WCLONE 0x80000000
228 static const char stat_map[] =
229 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
230 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
231 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
234 static const CB_TARGET_DEFS_MAP syscall_map[] =
236 { CB_SYS_open, TARGET_SYS_open },
237 { CB_SYS_close, TARGET_SYS_close },
238 { CB_SYS_read, TARGET_SYS_read },
239 { CB_SYS_write, TARGET_SYS_write },
240 { CB_SYS_lseek, TARGET_SYS_lseek },
241 { CB_SYS_unlink, TARGET_SYS_unlink },
242 { CB_SYS_getpid, TARGET_SYS_getpid },
243 { CB_SYS_fstat, TARGET_SYS_fstat64 },
244 { CB_SYS_lstat, TARGET_SYS_lstat64 },
245 { CB_SYS_stat, TARGET_SYS_stat64 },
246 { CB_SYS_pipe, TARGET_SYS_pipe },
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 /* These we treat specially, as they're used in the fcntl F_GETFL
667 syscall. For consistency, open_map is also manually edited to use
669 #define TARGET_O_ACCMODE 0x3
670 #define TARGET_O_RDONLY 0x0
671 #define TARGET_O_WRONLY 0x1
673 static const CB_TARGET_DEFS_MAP open_map[] = {
675 { O_ACCMODE, TARGET_O_ACCMODE },
678 { O_RDONLY, TARGET_O_RDONLY },
681 { O_WRONLY, TARGET_O_WRONLY },
702 { O_NONBLOCK, 0x800 },
714 { O_DIRECT, 0x4000 },
717 { O_LARGEFILE, 0x8000 },
720 { O_DIRECTORY, 0x10000 },
723 { O_NOFOLLOW, 0x20000 },
728 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
729 static SIM_CPU *current_cpu_for_cb_callback;
731 static int syscall_read_mem (host_callback *, struct cb_syscall *,
732 unsigned long, char *, int);
733 static int syscall_write_mem (host_callback *, struct cb_syscall *,
734 unsigned long, const char *, int);
735 static USI create_map (SIM_DESC, struct cris_sim_mmapped_page **,
737 static USI unmap_pages (SIM_DESC, struct cris_sim_mmapped_page **,
739 static USI is_mapped (SIM_DESC, struct cris_sim_mmapped_page **,
741 static void dump_statistics (SIM_CPU *current_cpu);
742 static void make_first_thread (SIM_CPU *current_cpu);
744 /* Read/write functions for system call interface. */
747 syscall_read_mem (host_callback *cb ATTRIBUTE_UNUSED,
748 struct cb_syscall *sc,
749 unsigned long taddr, char *buf, int bytes)
751 SIM_DESC sd = (SIM_DESC) sc->p1;
752 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
754 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
758 syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED,
759 struct cb_syscall *sc,
760 unsigned long taddr, const char *buf, int bytes)
762 SIM_DESC sd = (SIM_DESC) sc->p1;
763 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
765 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
768 /* When we risk running self-modified code (as in trampolines), this is
769 called from special-case insns. The silicon CRIS CPU:s have enough
770 cache snooping implemented making this a simulator-only issue. Tests:
771 gcc.c-torture/execute/931002-1.c execution, -O3 -g
772 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
775 cris_flush_simulator_decode_cache (SIM_CPU *current_cpu,
776 USI pc ATTRIBUTE_UNUSED)
778 SIM_DESC sd = CPU_STATE (current_cpu);
781 if (USING_SCACHE_P (sd))
782 scache_flush_cpu (current_cpu);
786 /* Output statistics at the end of a run. */
788 dump_statistics (SIM_CPU *current_cpu)
790 SIM_DESC sd = CPU_STATE (current_cpu);
791 CRIS_MISC_PROFILE *profp
792 = CPU_CRIS_MISC_PROFILE (current_cpu);
793 unsigned64 total = profp->basic_cycle_count;
794 const char *textmsg = "Basic clock cycles, total @: %llu\n";
796 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
797 what's included in the "total" count only. */
798 switch (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
799 & FLAG_CRIS_MISC_PROFILE_ALL)
801 case FLAG_CRIS_MISC_PROFILE_SIMPLE:
804 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED | FLAG_CRIS_MISC_PROFILE_SIMPLE):
806 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
807 total += profp->unaligned_mem_dword_count;
810 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE | FLAG_CRIS_MISC_PROFILE_SIMPLE):
811 textmsg = "Schedulable clock cycles, total @: %llu\n";
813 += (profp->memsrc_stall_count
814 + profp->memraw_stall_count
815 + profp->movemsrc_stall_count
816 + profp->movemdst_stall_count
817 + profp->mulsrc_stall_count
818 + profp->jumpsrc_stall_count
819 + profp->unaligned_mem_dword_count);
822 case FLAG_CRIS_MISC_PROFILE_ALL:
823 textmsg = "All accounted clock cycles, total @: %llu\n";
825 += (profp->memsrc_stall_count
826 + profp->memraw_stall_count
827 + profp->movemsrc_stall_count
828 + profp->movemdst_stall_count
829 + profp->movemaddr_stall_count
830 + profp->mulsrc_stall_count
831 + profp->jumpsrc_stall_count
832 + profp->branch_stall_count
833 + profp->jumptarget_stall_count
834 + profp->unaligned_mem_dword_count);
841 "Internal inconsistency at %s:%d",
843 sim_engine_halt (sd, current_cpu, NULL, 0,
844 sim_stopped, SIM_SIGILL);
847 /* Historically, these messages have gone to stderr, so we'll keep it
848 that way. It's also easier to then tell it from normal program
849 output. FIXME: Add redirect option like "run -e file". */
850 sim_io_eprintf (sd, textmsg, total);
852 /* For v32, unaligned_mem_dword_count should always be 0. For
853 v10, memsrc_stall_count should always be 0. */
854 sim_io_eprintf (sd, "Memory source stall cycles: %lld\n",
855 profp->memsrc_stall_count
856 + profp->unaligned_mem_dword_count);
857 sim_io_eprintf (sd, "Memory read-after-write stall cycles: %lld\n",
858 profp->memraw_stall_count);
859 sim_io_eprintf (sd, "Movem source stall cycles: %lld\n",
860 profp->movemsrc_stall_count);
861 sim_io_eprintf (sd, "Movem destination stall cycles: %lld\n",
862 profp->movemdst_stall_count);
863 sim_io_eprintf (sd, "Movem address stall cycles: %lld\n",
864 profp->movemaddr_stall_count);
865 sim_io_eprintf (sd, "Multiplication source stall cycles: %lld\n",
866 profp->mulsrc_stall_count);
867 sim_io_eprintf (sd, "Jump source stall cycles: %lld\n",
868 profp->jumpsrc_stall_count);
869 sim_io_eprintf (sd, "Branch misprediction stall cycles: %lld\n",
870 profp->branch_stall_count);
871 sim_io_eprintf (sd, "Jump target stall cycles: %lld\n",
872 profp->jumptarget_stall_count);
875 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
876 Return 1 if a overlap detected, 0 otherwise. */
879 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED,
880 struct cris_sim_mmapped_page **rootp,
883 struct cris_sim_mmapped_page *mapp;
885 if (len == 0 || (len & 8191))
888 /* Iterate over the reverse-address sorted pages until we find a page in
889 or lower than the checked area. */
890 for (mapp = *rootp; mapp != NULL && mapp->addr >= addr; mapp = mapp->prev)
891 if (mapp->addr < addr + len && mapp->addr >= addr)
897 /* Create mmapped memory. */
900 create_map (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
903 struct cris_sim_mmapped_page *mapp;
904 struct cris_sim_mmapped_page **higher_prevp = rootp;
905 USI new_addr = 0x40000000;
910 new_addr = rootp[0]->addr + 8192;
917 /* Which is better: return an error for this, or just round it up? */
920 /* Do a recursive call for each page in the request. */
921 for (page_addr = new_addr; len != 0; page_addr += 8192, len -= 8192)
922 if (create_map (sd, rootp, page_addr, 8192) >= (USI) -8191)
929 mapp != NULL && mapp->addr > new_addr;
931 higher_prevp = &mapp->prev;
933 /* Allocate the new page, on the next higher page from the last one
934 allocated, and link in the new descriptor before previous ones. */
935 mapp = malloc (sizeof (*mapp));
938 return (USI) -ENOMEM;
940 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
944 mapp->addr = new_addr;
945 mapp->prev = *higher_prevp;
946 *higher_prevp = mapp;
951 /* Unmap one or more pages. */
954 unmap_pages (SIM_DESC sd, struct cris_sim_mmapped_page **rootp, USI addr,
957 struct cris_sim_mmapped_page *mapp;
958 struct cris_sim_mmapped_page **higher_prevp = rootp;
965 /* Which is better: return an error for this, or just round it up? */
968 /* Loop backwards to make each call is O(1) over the number of pages
969 allocated, if we're unmapping from the high end of the pages. */
970 for (page_addr = addr + len - 8192;
973 if (unmap_pages (sd, rootp, page_addr, 8192) != 0)
979 for (mapp = *rootp; mapp != NULL && mapp->addr > addr; mapp = mapp->prev)
980 higher_prevp = &mapp->prev;
982 if (mapp == NULL || mapp->addr != addr)
985 *higher_prevp = mapp->prev;
986 sim_core_detach (sd, NULL, 0, 0, addr);
991 /* The semantic code invokes this for illegal (unrecognized) instructions. */
994 sim_engine_invalid_insn (SIM_CPU *current_cpu, IADDR cia, SEM_PC vpc)
996 SIM_DESC sd = CPU_STATE (current_cpu);
998 sim_engine_halt (sd, current_cpu, NULL, cia, sim_stopped, SIM_SIGILL);
1002 /* Handlers from the CGEN description that should not be called. */
1005 cris_bmod_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1006 UINT srcreg ATTRIBUTE_UNUSED,
1007 USI dstreg ATTRIBUTE_UNUSED)
1013 h_supr_set_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1014 UINT index ATTRIBUTE_UNUSED,
1015 USI page ATTRIBUTE_UNUSED,
1016 USI newval ATTRIBUTE_UNUSED)
1022 h_supr_get_handler (SIM_CPU *current_cpu ATTRIBUTE_UNUSED,
1023 UINT index ATTRIBUTE_UNUSED,
1024 USI page ATTRIBUTE_UNUSED)
1029 /* Swap one context for another. */
1032 schedule (SIM_CPU *current_cpu, int next)
1034 /* Need to mark context-switches in the trace output. */
1035 if ((CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1036 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE))
1037 cris_trace_printf (CPU_STATE (current_cpu), current_cpu,
1040 /* Copy the current context (if there is one) to its slot. */
1041 if (current_cpu->thread_data[current_cpu->threadno].cpu_context)
1042 memcpy (current_cpu->thread_data[current_cpu->threadno].cpu_context,
1043 ¤t_cpu->cpu_data_placeholder,
1044 current_cpu->thread_cpu_data_size);
1046 /* Copy the new context from its slot. */
1047 memcpy (¤t_cpu->cpu_data_placeholder,
1048 current_cpu->thread_data[next].cpu_context,
1049 current_cpu->thread_cpu_data_size);
1051 /* Update needed stuff to indicate the new context. */
1052 current_cpu->threadno = next;
1054 /* Handle pending signals. */
1055 if (current_cpu->thread_data[next].sigpending
1056 /* We don't run nested signal handlers. This means that pause(2)
1057 and sigsuspend(2) do not work in sighandlers, but that
1058 shouldn't be too hard a restriction. It also greatly
1059 simplifies the code. */
1060 && current_cpu->thread_data[next].cpu_context_atsignal == NULL)
1064 /* See if there's really a pending, non-blocked handler. We don't
1065 queue signals, so just use the first one in ascending order. */
1066 for (sig = 0; sig < 64; sig++)
1067 if (current_cpu->thread_data[next].sigdata[sig].pending
1068 && !current_cpu->thread_data[next].sigdata[sig].blocked)
1074 USI pc = sim_pc_get (current_cpu);
1076 /* It's simpler to save the CPU context inside the simulator
1077 than on the stack. */
1078 current_cpu->thread_data[next].cpu_context_atsignal
1080 ->make_thread_cpu_data) (current_cpu,
1081 current_cpu->thread_data[next]
1084 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1085 sp = bfd_getl32 (regbuf);
1087 /* Make sure we have an aligned stack. */
1090 /* Make room for the signal frame, aligned. FIXME: Check that
1091 the memory exists, map it in if absent. (BTW, should also
1092 implement on-access automatic stack allocation). */
1095 /* This isn't the same signal frame as the kernel uses, because
1096 we don't want to bother getting all registers on and off the
1099 /* First, we store the currently blocked signals. */
1101 for (i = 0; i < 32; i++)
1103 |= current_cpu->thread_data[next].sigdata[i + 1].blocked << i;
1104 sim_core_write_aligned_4 (current_cpu, pc, 0, sp, blocked);
1106 for (i = 0; i < 31; i++)
1108 |= current_cpu->thread_data[next].sigdata[i + 33].blocked << i;
1109 sim_core_write_aligned_4 (current_cpu, pc, 0, sp + 4, blocked);
1111 /* Then, the actual instructions. This is CPU-specific, but we
1112 use instructions from the common subset for v10 and v32 which
1113 should be safe for the time being but could be parametrized
1115 /* MOVU.W [PC+],R9. */
1116 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 8, 0x9c5f);
1117 /* .WORD TARGET_SYS_sigreturn. */
1118 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 10,
1119 TARGET_SYS_sigreturn);
1121 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 12, 0xe93d);
1123 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1124 instruction. Still, it doesn't matter because v10 has no
1125 delay slot for BREAK so it will not be executed). */
1126 sim_core_write_aligned_2 (current_cpu, pc, 0, sp + 16, 0x05b0);
1128 /* Modify registers to hold the right values for the sighandler
1129 context: updated stackpointer and return address pointing to
1130 the sigreturn stub. */
1131 bfd_putl32 (sp, regbuf);
1132 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_SP, regbuf, 4);
1133 bfd_putl32 (sp + 8, regbuf);
1134 (*CPU_REG_STORE (current_cpu)) (current_cpu, TARGET_SRP_REGNUM,
1137 current_cpu->thread_data[next].sigdata[sig].pending = 0;
1139 /* Block this signal (for the duration of the sighandler). */
1140 current_cpu->thread_data[next].sigdata[sig].blocked = 1;
1142 sim_pc_set (current_cpu, current_cpu->sighandler[sig]);
1143 bfd_putl32 (sig, regbuf);
1144 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10,
1147 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1148 needed all this for, specifies a SA_SIGINFO call but treats it
1149 like an ordinary sighandler; only the signal number argument is
1150 inspected. To make future need to implement SA_SIGINFO
1151 correctly possible, we set the siginfo argument register to a
1152 magic (hopefully non-address) number. (NB: then, you should
1153 just need to pass the siginfo argument; it seems you probably
1154 don't need to implement the specific rt_sigreturn.) */
1155 bfd_putl32 (0xbad5161f, regbuf);
1156 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R11,
1159 /* The third argument is unused and the kernel sets it to 0. */
1160 bfd_putl32 (0, regbuf);
1161 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R12,
1166 /* No, there actually was no pending signal for this thread. Reset
1168 current_cpu->thread_data[next].sigpending = 0;
1172 /* Reschedule the simplest possible way until something else is absolutely
1174 - A. Find the next process (round-robin) that doesn't have at_syscall
1176 - B. If there is none, just run the next process, round-robin.
1177 - Clear at_syscall for the current process. */
1180 reschedule (SIM_CPU *current_cpu)
1184 /* Iterate over all thread slots, because after a few thread creations
1185 and exits, we don't know where the live ones are. */
1186 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1187 i != current_cpu->threadno;
1188 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1189 if (current_cpu->thread_data[i].cpu_context
1190 && current_cpu->thread_data[i].at_syscall == 0)
1192 schedule (current_cpu, i);
1196 /* Pick any next live thread. */
1197 for (i = (current_cpu->threadno + 1) % SIM_TARGET_MAX_THREADS;
1198 i != current_cpu->threadno;
1199 i = (i + 1) % SIM_TARGET_MAX_THREADS)
1200 if (current_cpu->thread_data[i].cpu_context)
1202 schedule (current_cpu, i);
1206 /* More than one live thread, but we couldn't find the next one? */
1210 /* Set up everything to receive (or IGN) an incoming signal to the
1214 deliver_signal (SIM_CPU *current_cpu, int sig, unsigned int pid)
1217 USI pc = sim_pc_get (current_cpu);
1219 /* Find the thread index of the pid. */
1220 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
1221 /* Apparently it's ok to send signals to zombies (so a check for
1222 current_cpu->thread_data[i].cpu_context != NULL would be
1224 if (current_cpu->thread_data[i].threadid == pid - TARGET_PID)
1227 switch (current_cpu->sighandler[sig])
1229 case TARGET_SIG_DFL:
1232 /* The following according to the glibc
1233 documentation. (The kernel code has non-obvious
1234 execution paths.) */
1237 case TARGET_SIGSEGV:
1239 case TARGET_SIGABRT:
1240 case TARGET_SIGTRAP:
1243 case TARGET_SIGTERM:
1245 case TARGET_SIGQUIT:
1246 case TARGET_SIGKILL:
1249 case TARGET_SIGALRM:
1250 case TARGET_SIGVTALRM:
1251 case TARGET_SIGPROF:
1252 case TARGET_SIGSTOP:
1254 case TARGET_SIGPIPE:
1255 case TARGET_SIGLOST:
1256 case TARGET_SIGXCPU:
1257 case TARGET_SIGXFSZ:
1258 case TARGET_SIGUSR1:
1259 case TARGET_SIGUSR2:
1260 sim_io_eprintf (CPU_STATE (current_cpu),
1261 "Exiting pid %d due to signal %d\n",
1263 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1264 NULL, pc, sim_stopped,
1265 sig == TARGET_SIGABRT
1266 ? SIM_SIGABRT : SIM_SIGILL);
1269 /* The default for all other signals is to be ignored. */
1274 case TARGET_SIG_IGN:
1277 case TARGET_SIGKILL:
1278 case TARGET_SIGSTOP:
1279 /* Can't ignore these signals. */
1280 sim_io_eprintf (CPU_STATE (current_cpu),
1281 "Exiting pid %d due to signal %d\n",
1283 sim_engine_halt (CPU_STATE (current_cpu), current_cpu,
1284 NULL, pc, sim_stopped, SIM_SIGILL);
1293 /* Mark the signal as pending, making schedule () check
1294 closer. The signal will be handled when the thread is
1295 scheduled and the signal is unblocked. */
1296 current_cpu->thread_data[i].sigdata[sig].pending = 1;
1297 current_cpu->thread_data[i].sigpending = 1;
1302 sim_io_eprintf (CPU_STATE (current_cpu),
1303 "Unimplemented signal: %d\n", sig);
1304 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
1305 sim_stopped, SIM_SIGILL);
1310 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu)),
1314 /* Make the vector and the first item, the main thread. */
1317 make_first_thread (SIM_CPU *current_cpu)
1319 current_cpu->thread_data
1321 SIM_TARGET_MAX_THREADS
1322 * sizeof (current_cpu->thread_data[0]));
1323 current_cpu->thread_data[0].cpu_context
1324 = (*current_cpu->make_thread_cpu_data) (current_cpu,
1326 ->cpu_data_placeholder);
1327 current_cpu->thread_data[0].parent_threadid = -1;
1329 /* For good measure. */
1330 if (TARGET_SIG_DFL != 0)
1334 /* Main function: the handler of the "break 13" syscall insn. */
1337 cris_break_13_handler (SIM_CPU *current_cpu, USI callnum, USI arg1,
1338 USI arg2, USI arg3, USI arg4, USI arg5, USI arg6,
1342 SIM_DESC sd = CPU_STATE (current_cpu);
1343 host_callback *cb = STATE_CALLBACK (sd);
1345 int threadno = current_cpu->threadno;
1347 current_cpu->syscalls++;
1349 CB_SYSCALL_INIT (&s);
1355 if (callnum == TARGET_SYS_exit && current_cpu->m1threads == 0)
1357 if (CPU_CRIS_MISC_PROFILE (current_cpu)->flags
1358 & FLAG_CRIS_MISC_PROFILE_ALL)
1359 dump_statistics (current_cpu);
1360 sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, arg1);
1364 s.p2 = (PTR) current_cpu;
1365 s.read_mem = syscall_read_mem;
1366 s.write_mem = syscall_write_mem;
1368 current_cpu_for_cb_callback = current_cpu;
1370 if (cb_syscall (cb, &s) != CB_RC_OK)
1373 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
1375 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
1378 retval = s.result == -1 ? -s.errcode : s.result;
1380 if (s.errcode != 0 && s.errcode == cb_host_to_target_errno (cb, ENOSYS))
1382 /* If the generic simulator call said ENOSYS, then let's try the
1383 ones we know ourselves.
1385 The convention is to provide *very limited* functionality on an
1386 as-needed basis, only what's covered by the test-suite, tests
1387 added when functionality changes and abort with a descriptive
1388 message for *everything* else. Where there's no test-case, we
1393 /* It's a pretty safe bet that the "old setup() system call"
1394 number will not be re-used; we can't say the same for higher
1395 numbers. We treat this simulator-generated call as "wait
1396 forever"; we re-run this insn. The wait is ended by a
1397 callback. Sanity check that this is the reason we got
1399 if (current_cpu->thread_data == NULL
1400 || (current_cpu->thread_data[threadno].pipe_write_fd == 0))
1401 goto unimplemented_syscall;
1403 sim_pc_set (current_cpu, pc);
1407 case TARGET_SYS_fcntl64:
1408 case TARGET_SYS_fcntl:
1413 Glibc checks stdin, stdout and stderr fd:s for
1414 close-on-exec security sanity. We just need to provide a
1415 OK return value. If we really need to have a
1416 close-on-exec flag true, we could just do a real fcntl
1422 /* F_SETFD. Just ignore attempts to set the close-on-exec
1428 /* F_GETFL. Check for the special case for open+fdopen. */
1429 if (current_cpu->last_syscall == TARGET_SYS_open
1430 && arg1 == current_cpu->last_open_fd)
1432 retval = current_cpu->last_open_flags & TARGET_O_ACCMODE;
1437 /* Because we can't freopen fd:s 0, 1, 2 to mean
1438 something else than stdin, stdout and stderr
1439 (sim/common/syscall.c:cb_syscall special cases fd
1440 0, 1 and 2), we know what flags that we can
1441 sanely return for these fd:s. */
1442 retval = TARGET_O_RDONLY;
1445 else if (arg1 == 1 || arg1 == 2)
1447 retval = TARGET_O_WRONLY;
1452 /* Abort for all other cases. */
1453 sim_io_eprintf (sd, "Unimplemented %s syscall "
1454 "(fd: 0x%lx: cmd: 0x%lx arg: 0x%lx)\n",
1455 callnum == TARGET_SYS_fcntl
1456 ? "fcntl" : "fcntl64",
1457 (unsigned long) (USI) arg1,
1458 (unsigned long) (USI) arg2,
1459 (unsigned long) (USI) arg3);
1460 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1466 case TARGET_SYS_uname:
1468 /* Fill in a few constants to appease glibc. */
1469 static const char sim_utsname[6][65] =
1479 if ((s.write_mem) (cb, &s, arg1, (const char *) sim_utsname,
1480 sizeof (sim_utsname))
1481 != sizeof (sim_utsname))
1482 retval = -cb_host_to_target_errno (cb, EFAULT);
1488 case TARGET_SYS_geteuid32:
1489 /* We tell the truth with these. Maybe we shouldn't, but it
1490 should match the "stat" information. */
1491 retval = geteuid ();
1494 case TARGET_SYS_getuid32:
1498 case TARGET_SYS_getegid32:
1499 retval = getegid ();
1502 case TARGET_SYS_getgid32:
1506 case TARGET_SYS_brk:
1507 /* Most often, we just return the argument, like the Linux
1512 retval = current_cpu->endbrk;
1513 else if (arg1 <= current_cpu->endmem)
1514 current_cpu->endbrk = arg1;
1517 USI new_end = (arg1 + 8191) & ~8191;
1519 /* If the simulator wants to brk more than a certain very
1520 large amount, something is wrong. FIXME: Return an error
1521 or abort? Have command-line selectable? */
1522 if (new_end - current_cpu->endmem > SIM_MAX_ALLOC_CHUNK)
1524 current_cpu->endbrk = current_cpu->endmem;
1525 retval = current_cpu->endmem;
1529 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
1530 current_cpu->endmem,
1531 new_end - current_cpu->endmem,
1533 current_cpu->endbrk = arg1;
1534 current_cpu->endmem = new_end;
1538 case TARGET_SYS_getpid:
1539 /* Correct until CLONE_THREAD is implemented. */
1540 retval = current_cpu->thread_data == NULL
1542 : TARGET_PID + current_cpu->thread_data[threadno].threadid;
1545 case TARGET_SYS_getppid:
1546 /* Correct until CLONE_THREAD is implemented. */
1547 retval = current_cpu->thread_data == NULL
1550 + current_cpu->thread_data[threadno].parent_threadid);
1553 case TARGET_SYS_mmap2:
1562 /* If the simulator wants to mmap more than the very large
1563 limit, something is wrong. FIXME: Return an error or
1564 abort? Have command-line selectable? */
1565 if (len > SIM_MAX_ALLOC_CHUNK)
1567 retval = -cb_host_to_target_errno (cb, ENOMEM);
1571 if ((prot != (TARGET_PROT_READ | TARGET_PROT_WRITE)
1573 != (TARGET_PROT_READ
1575 | TARGET_PROT_EXEC))
1576 && prot != TARGET_PROT_READ)
1577 || (flags != (TARGET_MAP_ANONYMOUS | TARGET_MAP_PRIVATE)
1578 && flags != TARGET_MAP_PRIVATE
1579 && flags != TARGET_MAP_SHARED)
1580 || (fd != (USI) -1 && prot != TARGET_PROT_READ)
1583 sim_io_eprintf (sd, "Unimplemented mmap2 call "
1584 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1585 (unsigned long) arg1,
1586 (unsigned long) arg2,
1587 (unsigned long) arg3,
1588 (unsigned long) arg4,
1589 (unsigned long) arg5,
1590 (unsigned long) arg6);
1591 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1595 else if (fd != (USI) -1)
1602 /* A non-aligned argument is allowed for files. */
1603 USI newlen = (len + 8191) & ~8191;
1605 /* We only support read, which we should already have
1606 checked. Check again anyway. */
1607 if (prot != TARGET_PROT_READ)
1611 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1614 if (newaddr >= (USI) -8191)
1617 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1621 /* Find the current position in the file. */
1622 s.func = TARGET_SYS_lseek;
1626 if (cb_syscall (cb, &s) != CB_RC_OK)
1633 /* Use the standard read callback to read in "len"
1635 s.func = TARGET_SYS_read;
1639 if (cb_syscall (cb, &s) != CB_RC_OK)
1642 if ((USI) s.result != len)
1645 /* After reading, we need to go back to the previous
1646 position in the file. */
1647 s.func = TARGET_SYS_lseek;
1651 if (cb_syscall (cb, &s) != CB_RC_OK)
1653 if (pos != (USI) s.result)
1661 = create_map (sd, ¤t_cpu->highest_mmapped_page, addr,
1662 (len + 8191) & ~8191);
1664 if (newaddr >= (USI) -8191)
1665 retval = -cb_host_to_target_errno (cb, -(SI) newaddr);
1672 case TARGET_SYS_mprotect:
1674 /* We only cover the case of linuxthreads mprotecting out its
1675 stack guard page. */
1680 if ((addr & 8191) != 0
1682 || prot != TARGET_PROT_NONE
1683 || !is_mapped (sd, ¤t_cpu->highest_mmapped_page, addr,
1686 sim_io_eprintf (sd, "Unimplemented mprotect call "
1687 "(0x%lx, 0x%lx, 0x%lx)\n",
1688 (unsigned long) arg1,
1689 (unsigned long) arg2,
1690 (unsigned long) arg3);
1691 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1696 /* FIXME: We should account for pages like this that are
1697 "mprotected out". For now, we just tell the simulator
1698 core to remove that page from its map. */
1699 sim_core_detach (sd, NULL, 0, 0, addr);
1704 case TARGET_SYS_ioctl:
1706 /* We support only a very limited functionality: checking
1707 stdout with TCGETS to perform the isatty function. The
1708 TCGETS ioctl isn't actually performed or the result used by
1709 an isatty () caller in a "hello, world" program; only the
1710 return value is then used. Maybe we shouldn't care about
1711 the environment of the simulator regarding isatty, but
1712 that's been working before, in the xsim simulator. */
1713 if (arg2 == TARGET_TCGETS && arg1 == 1)
1714 retval = isatty (1) ? 0 : cb_host_to_target_errno (cb, EINVAL);
1716 retval = -cb_host_to_target_errno (cb, EINVAL);
1720 case TARGET_SYS_munmap:
1725 = unmap_pages (sd, ¤t_cpu->highest_mmapped_page, addr,
1727 retval = result != 0 ? -cb_host_to_target_errno (cb, result) : 0;
1731 case TARGET_SYS_wait4:
1739 /* FIXME: We're not properly implementing __WCLONE, and we
1740 don't really need the special casing so we might as well
1741 make this general. */
1742 if ((!(pid == (USI) -1
1743 && options == (TARGET___WCLONE | TARGET_WNOHANG)
1746 && (options == TARGET___WCLONE
1747 || options == TARGET___WALL)))
1749 || current_cpu->thread_data == NULL)
1751 sim_io_eprintf (sd, "Unimplemented wait4 call "
1752 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1753 (unsigned long) arg1,
1754 (unsigned long) arg2,
1755 (unsigned long) arg3,
1756 (unsigned long) arg4);
1757 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1762 if (pid == (USI) -1)
1763 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1765 if (current_cpu->thread_data[threadno].threadid
1766 == current_cpu->thread_data[i].parent_threadid
1767 && current_cpu->thread_data[i].threadid != 0
1768 && current_cpu->thread_data[i].cpu_context == NULL)
1770 /* A zombied child. Get the exit value and clear the
1771 zombied entry so it will be reused. */
1772 sim_core_write_unaligned_4 (current_cpu, pc, 0, saddr,
1774 ->thread_data[i].exitval);
1776 = current_cpu->thread_data[i].threadid + TARGET_PID;
1777 memset (¤t_cpu->thread_data[i], 0,
1778 sizeof (current_cpu->thread_data[i]));
1784 /* We're waiting for a specific PID. If we don't find
1785 it zombied on this run, rerun the syscall. */
1786 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
1787 if (pid == current_cpu->thread_data[i].threadid + TARGET_PID
1788 && current_cpu->thread_data[i].cpu_context == NULL)
1791 /* Get the exit value if the caller wants it. */
1792 sim_core_write_unaligned_4 (current_cpu, pc, 0,
1799 = current_cpu->thread_data[i].threadid + TARGET_PID;
1800 memset (¤t_cpu->thread_data[i], 0,
1801 sizeof (current_cpu->thread_data[i]));
1806 sim_pc_set (current_cpu, pc);
1809 retval = -cb_host_to_target_errno (cb, ECHILD);
1814 case TARGET_SYS_rt_sigaction:
1822 __sighandler_t sa_handler;
1823 unsigned long sa_flags;
1824 void (*sa_restorer)(void);
1830 sim_core_write_unaligned_4 (current_cpu, pc, 0, old_sa + 0,
1831 current_cpu->sighandler[signum]);
1832 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 4, 0);
1833 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 8, 0);
1835 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
1836 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 12, 0);
1837 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg3 + 16, 0);
1841 USI target_sa_handler
1842 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa);
1844 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 4);
1845 USI target_sa_restorer
1846 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 8);
1847 USI target_sa_mask_low
1848 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 12);
1849 USI target_sa_mask_high
1850 = sim_core_read_unaligned_4 (current_cpu, pc, 0, new_sa + 16);
1852 /* We won't interrupt a syscall so we won't restart it,
1853 but a signal(2) call ends up syscalling rt_sigaction
1854 with this flag, so we have to handle it. The
1855 sa_restorer field contains garbage when not
1856 TARGET_SA_RESTORER, so don't look at it. For the
1857 time being, we don't nest sighandlers, so we
1858 ignore the sa_mask, which simplifies things. */
1859 if ((target_sa_flags != 0
1860 && target_sa_flags != TARGET_SA_RESTART
1861 && target_sa_flags != (TARGET_SA_RESTART|TARGET_SA_SIGINFO))
1862 || target_sa_handler == 0)
1864 sim_io_eprintf (sd, "Unimplemented rt_sigaction "
1866 "0x%lx: [0x%x, 0x%x, 0x%x, "
1869 (unsigned long) arg1,
1870 (unsigned long) arg2,
1871 target_sa_handler, target_sa_flags,
1873 target_sa_mask_low, target_sa_mask_high,
1874 (unsigned long) arg3);
1875 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
1879 current_cpu->sighandler[signum] = target_sa_handler;
1881 /* Because we may have unblocked signals, one may now be
1882 pending, if there are threads, that is. */
1883 if (current_cpu->thread_data)
1884 current_cpu->thread_data[threadno].sigpending = 1;
1890 case TARGET_SYS_mremap:
1896 USI new_addr = arg5;
1899 if (new_len == old_len)
1900 /* The program and/or library is possibly confused but
1901 this is a valid call. Happens with ipps-1.40 on file
1904 else if (new_len < old_len)
1906 /* Shrinking is easy. */
1907 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1908 addr + new_len, old_len - new_len) != 0)
1909 retval = -cb_host_to_target_errno (cb, EINVAL);
1913 else if (! is_mapped (sd, ¤t_cpu->highest_mmapped_page,
1914 addr + old_len, new_len - old_len))
1916 /* If the extension isn't mapped, we can just add it. */
1918 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1919 addr + old_len, new_len - old_len);
1921 if (mapped_addr > (USI) -8192)
1922 retval = -cb_host_to_target_errno (cb, -(SI) mapped_addr);
1926 else if (flags & TARGET_MREMAP_MAYMOVE)
1928 /* Create a whole new map and copy the contents
1929 block-by-block there. We ignore the new_addr argument
1932 USI prev_addr = addr;
1933 USI prev_len = old_len;
1936 = create_map (sd, ¤t_cpu->highest_mmapped_page,
1939 if (mapped_addr > (USI) -8192)
1941 retval = -cb_host_to_target_errno (cb, -(SI) new_addr);
1945 retval = mapped_addr;
1948 old_len -= 8192, mapped_addr += 8192, addr += 8192)
1950 if (sim_core_read_buffer (sd, current_cpu, read_map, buf,
1952 || sim_core_write_buffer (sd, current_cpu, 0, buf,
1953 mapped_addr, 8192) != 8192)
1957 if (unmap_pages (sd, ¤t_cpu->highest_mmapped_page,
1958 prev_addr, prev_len) != 0)
1962 retval = -cb_host_to_target_errno (cb, -ENOMEM);
1966 case TARGET_SYS_poll:
1968 int npollfds = arg2;
1984 /* Check that this is the expected poll call from
1985 linuxthreads/manager.c; we don't support anything else.
1986 Remember, fd == 0 isn't supported. */
1988 || ((fd = sim_core_read_unaligned_4 (current_cpu, pc,
1990 || ((events = sim_core_read_unaligned_2 (current_cpu, pc,
1993 || ((cb->fstat) (cb, fd, &buf) != 0
1994 || (buf.st_mode & S_IFIFO) == 0)
1995 || current_cpu->thread_data == NULL)
1997 sim_io_eprintf (sd, "Unimplemented poll syscall "
1998 "(0x%lx: [0x%x, 0x%x, x], 0x%lx, 0x%lx)\n",
1999 (unsigned long) arg1, fd, events,
2000 (unsigned long) arg2, (unsigned long) arg3);
2001 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2007 /* Iterate over threads; find a marker that a writer is
2008 sleeping, waiting for a reader. */
2009 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2010 if (current_cpu->thread_data[i].cpu_context != NULL
2011 && current_cpu->thread_data[i].pipe_read_fd == fd)
2013 revents = TARGET_POLLIN;
2018 /* Timeout decreases with whatever time passed between the
2019 last syscall and this. That's not exactly right for the
2020 first call, but it's close enough that it isn't
2021 worthwhile to complicate matters by making that a special
2024 -= (TARGET_TIME_MS (current_cpu)
2025 - (current_cpu->thread_data[threadno].last_execution));
2027 /* Arrange to repeat this syscall until timeout or event,
2028 decreasing timeout at each iteration. */
2029 if (timeout > 0 && revents == 0)
2031 bfd_byte timeout_buf[4];
2033 bfd_putl32 (timeout, timeout_buf);
2034 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2035 H_GR_R12, timeout_buf, 4);
2036 sim_pc_set (current_cpu, pc);
2041 sim_core_write_unaligned_2 (current_cpu, pc, 0, ufds + 4 + 2,
2046 case TARGET_SYS_time:
2048 retval = (int) (*cb->time) (cb, 0L);
2050 /* At time of this writing, CB_SYSCALL_time doesn't do the
2051 part of setting *arg1 to the return value. */
2053 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, retval);
2057 case TARGET_SYS_gettimeofday:
2060 USI ts = TARGET_TIME (current_cpu);
2061 USI tms = TARGET_TIME_MS (current_cpu);
2063 /* First dword is seconds since TARGET_EPOCH. */
2064 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1, ts);
2066 /* Second dword is microseconds. */
2067 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg1 + 4,
2068 (tms % 1000) * 1000);
2072 /* Time-zone info is always cleared. */
2073 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, 0);
2074 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, 0);
2079 case TARGET_SYS_llseek:
2081 /* If it fits, tweak parameters to fit the "generic" 32-bit
2082 lseek and use that. */
2090 if (!((offs_hi == 0 && offs_lo >= 0)
2091 || (offs_hi == -1 && offs_lo < 0)))
2094 "Unimplemented llseek offset,"
2095 " fd %d: 0x%x:0x%x\n",
2096 fd, (unsigned) arg2, (unsigned) arg3);
2097 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2101 s.func = TARGET_SYS_lseek;
2104 if (cb_syscall (cb, &s) != CB_RC_OK)
2106 sim_io_eprintf (sd, "Break 13: invalid %d? Returned %ld\n", callnum,
2108 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGILL);
2111 retval = -s.errcode;
2114 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp,
2116 sim_core_write_unaligned_4 (current_cpu, pc, 0, resultp + 4,
2117 s.result < 0 ? -1 : 0);
2122 /* This one does have a generic callback function, but at the time
2123 of this writing, cb_syscall does not have code for it, and we
2124 need target-specific code for the threads implementation
2126 case TARGET_SYS_kill:
2133 /* At kill(2), glibc sets signal masks such that the thread
2134 machinery is initialized. Still, there is and was only
2136 if (current_cpu->max_threadid == 0)
2138 if (pid != TARGET_PID)
2140 retval = -cb_host_to_target_errno (cb, EPERM);
2144 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2145 if (sig == TARGET_SIGABRT)
2146 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2147 the end-point for failing GCC test-cases. */
2148 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2152 sim_io_eprintf (sd, "Unimplemented signal: %d\n", sig);
2153 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2157 /* This will not be reached. */
2161 retval = deliver_signal (current_cpu, sig, pid);
2165 case TARGET_SYS_rt_sigprocmask:
2172 if (how != TARGET_SIG_BLOCK
2173 && how != TARGET_SIG_SETMASK
2174 && how != TARGET_SIG_UNBLOCK)
2176 sim_io_eprintf (sd, "Unimplemented rt_sigprocmask syscall "
2177 "(0x%x, 0x%x, 0x%x)\n", arg1, arg2, arg3);
2178 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2187 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2190 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2193 /* The sigmask is kept in the per-thread data, so we may
2194 need to create the first one. */
2195 if (current_cpu->thread_data == NULL)
2196 make_first_thread (current_cpu);
2198 if (how == TARGET_SIG_SETMASK)
2199 for (i = 0; i < 64; i++)
2200 current_cpu->thread_data[threadno].sigdata[i].blocked = 0;
2202 for (i = 0; i < 32; i++)
2203 if ((set_low & (1 << i)))
2204 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2205 = (how != TARGET_SIG_UNBLOCK);
2207 for (i = 0; i < 31; i++)
2208 if ((set_high & (1 << i)))
2209 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2210 = (how != TARGET_SIG_UNBLOCK);
2212 /* The mask changed, so a signal may be unblocked for
2214 current_cpu->thread_data[threadno].sigpending = 1;
2222 for (i = 0; i < 32; i++)
2223 if (current_cpu->thread_data[threadno]
2224 .sigdata[i + 1].blocked)
2226 for (i = 0; i < 31; i++)
2227 if (current_cpu->thread_data[threadno]
2228 .sigdata[i + 33].blocked)
2231 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 0, set_low);
2232 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldsetp + 4, set_high);
2239 case TARGET_SYS_sigreturn:
2243 int was_sigsuspended;
2245 if (current_cpu->thread_data == NULL
2246 /* The CPU context is saved with the simulator data, not
2247 on the stack as in the real world. */
2248 || (current_cpu->thread_data[threadno].cpu_context_atsignal
2251 sim_io_eprintf (sd, "Invalid sigreturn syscall: no signal"
2253 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2254 (unsigned long) arg1,
2255 (unsigned long) arg2,
2256 (unsigned long) arg3,
2257 (unsigned long) arg4,
2258 (unsigned long) arg5,
2259 (unsigned long) arg6);
2260 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2265 = current_cpu->thread_data[threadno].sigsuspended;
2267 /* Restore the sigmask, either from the stack copy made when
2268 the sighandler was called, or from the saved state
2269 specifically for sigsuspend(2). */
2270 if (was_sigsuspended)
2272 current_cpu->thread_data[threadno].sigsuspended = 0;
2273 for (i = 0; i < 64; i++)
2274 current_cpu->thread_data[threadno].sigdata[i].blocked
2275 = current_cpu->thread_data[threadno]
2276 .sigdata[i].blocked_suspendsave;
2284 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2285 H_GR_SP, regbuf, 4);
2286 sp = bfd_getl32 (regbuf);
2288 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp);
2290 = sim_core_read_unaligned_4 (current_cpu, pc, 0, sp + 4);
2292 for (i = 0; i < 32; i++)
2293 current_cpu->thread_data[threadno].sigdata[i + 1].blocked
2294 = (set_low & (1 << i)) != 0;
2295 for (i = 0; i < 31; i++)
2296 current_cpu->thread_data[threadno].sigdata[i + 33].blocked
2297 = (set_high & (1 << i)) != 0;
2300 /* The mask changed, so a signal may be unblocked for
2302 current_cpu->thread_data[threadno].sigpending = 1;
2304 memcpy (¤t_cpu->cpu_data_placeholder,
2305 current_cpu->thread_data[threadno].cpu_context_atsignal,
2306 current_cpu->thread_cpu_data_size);
2307 free (current_cpu->thread_data[threadno].cpu_context_atsignal);
2308 current_cpu->thread_data[threadno].cpu_context_atsignal = NULL;
2310 /* The return value must come from the saved R10. */
2311 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, regbuf, 4);
2312 retval = bfd_getl32 (regbuf);
2314 /* We must also break the "sigsuspension loop". */
2315 if (was_sigsuspended)
2316 sim_pc_set (current_cpu, sim_pc_get (current_cpu) + 2);
2320 case TARGET_SYS_rt_sigsuspend:
2327 sim_io_eprintf (sd, "Unimplemented rt_sigsuspend syscall"
2328 " arguments (0x%lx, 0x%lx)\n",
2329 (unsigned long) arg1, (unsigned long) arg2);
2330 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2334 /* Don't change the signal mask if we're already in
2335 sigsuspend state (i.e. this syscall is a rerun). */
2336 else if (!current_cpu->thread_data[threadno].sigsuspended)
2339 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2342 = sim_core_read_unaligned_4 (current_cpu, pc, 0,
2346 /* Save the current sigmask and insert the user-supplied
2348 for (i = 0; i < 32; i++)
2350 current_cpu->thread_data[threadno]
2351 .sigdata[i + 1].blocked_suspendsave
2352 = current_cpu->thread_data[threadno]
2353 .sigdata[i + 1].blocked;
2355 current_cpu->thread_data[threadno]
2356 .sigdata[i + 1].blocked = (set_low & (1 << i)) != 0;
2358 for (i = 0; i < 31; i++)
2360 current_cpu->thread_data[threadno]
2361 .sigdata[i + 33].blocked_suspendsave
2362 = current_cpu->thread_data[threadno]
2363 .sigdata[i + 33].blocked;
2364 current_cpu->thread_data[threadno]
2365 .sigdata[i + 33].blocked = (set_high & (1 << i)) != 0;
2368 current_cpu->thread_data[threadno].sigsuspended = 1;
2370 /* The mask changed, so a signal may be unblocked for
2372 current_cpu->thread_data[threadno].sigpending = 1;
2375 /* Because we don't use arg1 (newsetp) when this syscall is
2376 rerun, it doesn't matter that we overwrite it with the
2377 (constant) return value. */
2378 retval = -cb_host_to_target_errno (cb, EINTR);
2379 sim_pc_set (current_cpu, pc);
2383 /* Add case labels here for other syscalls using the 32-bit
2384 "struct stat", provided they have a corresponding simulator
2385 function of course. */
2386 case TARGET_SYS_stat:
2387 case TARGET_SYS_fstat:
2389 /* As long as the infrastructure doesn't cache anything
2390 related to the stat mapping, this trick gets us a dual
2391 "struct stat"-type mapping in the least error-prone way. */
2392 const char *saved_map = cb->stat_map;
2393 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
2395 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_stat32_map;
2396 cb->stat_map = stat32_map;
2398 if (cb_syscall (cb, &s) != CB_RC_OK)
2401 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2404 retval = s.result == -1 ? -s.errcode : s.result;
2406 cb->stat_map = saved_map;
2407 cb->syscall_map = saved_syscall_map;
2411 case TARGET_SYS_getcwd:
2416 char *cwd = xmalloc (MAXPATHLEN);
2417 if (cwd != getcwd (cwd, MAXPATHLEN))
2420 /* FIXME: When and if we support chdir, we need something
2421 a bit more elaborate. */
2422 if (simulator_sysroot[0] != '\0')
2425 retval = -cb_host_to_target_errno (cb, ERANGE);
2426 if (strlen (cwd) + 1 <= size)
2428 retval = strlen (cwd) + 1;
2429 if (sim_core_write_buffer (sd, current_cpu, 0, cwd,
2431 != (unsigned int) retval)
2432 retval = -cb_host_to_target_errno (cb, EFAULT);
2438 case TARGET_SYS_readlink:
2443 char *pbuf = xmalloc (MAXPATHLEN);
2444 char *lbuf = xmalloc (MAXPATHLEN);
2445 char *lbuf_alloc = lbuf;
2450 if (sim_core_read_unaligned_1 (current_cpu, pc, 0, path) == '/')
2452 strcpy (pbuf, simulator_sysroot);
2453 o += strlen (simulator_sysroot);
2456 for (i = 0; i + o < MAXPATHLEN; i++)
2459 = sim_core_read_unaligned_1 (current_cpu, pc, 0, path + i);
2460 if (pbuf[i + o] == 0)
2464 if (i + o == MAXPATHLEN)
2466 retval = -cb_host_to_target_errno (cb, ENAMETOOLONG);
2470 /* Intervene calls for certain files expected in the target
2471 proc file system. */
2472 if (strcmp (pbuf + strlen (simulator_sysroot),
2473 "/proc/" XSTRING (TARGET_PID) "/exe") == 0)
2476 = (STATE_PROG_ARGV (sd) != NULL
2477 ? *STATE_PROG_ARGV (sd) : NULL);
2479 if (argv0 == NULL || *argv0 == '.')
2481 sim_io_eprintf (sd, "Unimplemented readlink syscall "
2482 "(0x%lx: [\"%s\"], 0x%lx)\n",
2483 (unsigned long) arg1, pbuf,
2484 (unsigned long) arg2);
2485 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2489 else if (*argv0 == '/')
2491 if (strncmp (simulator_sysroot, argv0,
2492 strlen (simulator_sysroot)) == 0)
2493 argv0 += strlen (simulator_sysroot);
2495 strcpy (lbuf, argv0);
2496 nchars = strlen (argv0) + 1;
2500 if (getcwd (lbuf, MAXPATHLEN) != NULL
2501 && strlen (lbuf) + 2 + strlen (argv0) < MAXPATHLEN)
2503 if (strncmp (simulator_sysroot, lbuf,
2504 strlen (simulator_sysroot)) == 0)
2505 lbuf += strlen (simulator_sysroot);
2508 strcat (lbuf, argv0);
2509 nchars = strlen (lbuf) + 1;
2516 nchars = readlink (pbuf, lbuf, MAXPATHLEN);
2518 /* We trust that the readlink result returns a *relative*
2519 link, or one already adjusted for the file-path-prefix.
2520 (We can't generally tell the difference, so we go with
2521 the easiest decision; no adjustment.) */
2525 retval = -cb_host_to_target_errno (cb, errno);
2529 if (bufsiz < nchars)
2532 if (sim_core_write_buffer (sd, current_cpu, write_map, lbuf,
2533 buf, nchars) != (unsigned int) nchars)
2534 retval = -cb_host_to_target_errno (cb, EFAULT);
2543 case TARGET_SYS_sched_getscheduler:
2547 /* FIXME: Search (other) existing threads. */
2548 if (pid != 0 && pid != TARGET_PID)
2549 retval = -cb_host_to_target_errno (cb, ESRCH);
2551 retval = TARGET_SCHED_OTHER;
2555 case TARGET_SYS_sched_getparam:
2561 struct sched_param {
2565 if (pid != 0 && pid != TARGET_PID)
2566 retval = -cb_host_to_target_errno (cb, ESRCH);
2569 /* FIXME: Save scheduler setting before threads are
2571 sim_core_write_unaligned_4 (current_cpu, pc, 0, paramp,
2572 current_cpu->thread_data != NULL
2574 ->thread_data[threadno]
2582 case TARGET_SYS_sched_setparam:
2587 if ((pid != 0 && pid != TARGET_PID)
2588 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2590 retval = -cb_host_to_target_errno (cb, EINVAL);
2596 case TARGET_SYS_sched_setscheduler:
2602 if ((pid != 0 && pid != TARGET_PID)
2603 || policy != TARGET_SCHED_OTHER
2604 || sim_core_read_unaligned_4 (current_cpu, pc, 0,
2606 retval = -cb_host_to_target_errno (cb, EINVAL);
2608 /* FIXME: Save scheduler setting to be read in later
2609 sched_getparam calls. */
2614 case TARGET_SYS_sched_yield:
2615 /* We reschedule to the next thread after a syscall anyway, so
2616 we don't have to do anything here than to set the return
2621 case TARGET_SYS_sched_get_priority_min:
2622 case TARGET_SYS_sched_get_priority_max:
2624 retval = -cb_host_to_target_errno (cb, EINVAL);
2629 case TARGET_SYS_ugetrlimit:
2631 unsigned int curlim, maxlim;
2632 if (arg1 != TARGET_RLIMIT_STACK && arg1 != TARGET_RLIMIT_NOFILE)
2634 retval = -cb_host_to_target_errno (cb, EINVAL);
2640 unsigned long rlim_cur;
2641 unsigned long rlim_max;
2643 if (arg1 == TARGET_RLIMIT_NOFILE)
2645 /* Sadly a very low limit. Better not lie, though. */
2646 maxlim = curlim = MAX_CALLBACK_FDS;
2648 else /* arg1 == TARGET_RLIMIT_STACK */
2650 maxlim = 0xffffffff;
2653 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2, curlim);
2654 sim_core_write_unaligned_4 (current_cpu, pc, 0, arg2 + 4, maxlim);
2659 case TARGET_SYS_setrlimit:
2660 if (arg1 != TARGET_RLIMIT_STACK)
2662 retval = -cb_host_to_target_errno (cb, EINVAL);
2665 /* FIXME: Save values for future ugetrlimit calls. */
2669 /* Provide a very limited subset of the sysctl functions, and
2670 abort for the rest. */
2671 case TARGET_SYS__sysctl:
2674 struct __sysctl_args {
2681 unsigned long __unused[4];
2683 SI name = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1);
2684 SI name0 = name == 0
2685 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name);
2686 SI name1 = name == 0
2687 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, name + 4);
2689 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 4);
2691 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 8);
2693 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 12);
2694 SI oldlen = oldlenp == 0
2695 ? 0 : sim_core_read_unaligned_4 (current_cpu, pc, 0, oldlenp);
2697 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 16);
2699 = sim_core_read_unaligned_4 (current_cpu, pc, 0, arg1 + 20);
2701 if (name0 == TARGET_CTL_KERN && name1 == TARGET_CTL_KERN_VERSION)
2703 SI to_write = oldlen < (SI) sizeof (TARGET_UTSNAME)
2704 ? oldlen : (SI) sizeof (TARGET_UTSNAME);
2706 sim_core_write_unaligned_4 (current_cpu, pc, 0, oldlenp,
2707 sizeof (TARGET_UTSNAME));
2709 if (sim_core_write_buffer (sd, current_cpu, write_map,
2710 TARGET_UTSNAME, oldval,
2712 != (unsigned int) to_write)
2713 retval = -cb_host_to_target_errno (cb, EFAULT);
2719 sim_io_eprintf (sd, "Unimplemented _sysctl syscall "
2720 "(0x%lx: [0x%lx, 0x%lx],"
2721 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2722 (unsigned long) name,
2723 (unsigned long) name0,
2724 (unsigned long) name1,
2725 (unsigned long) nlen,
2726 (unsigned long) oldval,
2727 (unsigned long) oldlenp,
2728 (unsigned long) newval,
2729 (unsigned long) newlen);
2730 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2735 case TARGET_SYS_exit:
2737 /* Here for all but the last thread. */
2740 = current_cpu->thread_data[threadno].threadid + TARGET_PID;
2742 = (current_cpu->thread_data[threadno].parent_threadid
2744 int exitsig = current_cpu->thread_data[threadno].exitsig;
2746 /* Any children are now all orphans. */
2747 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2748 if (current_cpu->thread_data[i].parent_threadid
2749 == current_cpu->thread_data[threadno].threadid)
2750 /* Make getppid(2) return 1 for them, poor little ones. */
2751 current_cpu->thread_data[i].parent_threadid = -TARGET_PID + 1;
2753 /* Free the cpu context data. When the parent has received
2754 the exit status, we'll clear the entry too. */
2755 free (current_cpu->thread_data[threadno].cpu_context);
2756 current_cpu->thread_data[threadno].cpu_context = NULL;
2757 current_cpu->m1threads--;
2760 sim_io_eprintf (sd, "Thread %d exited with status %d\n",
2762 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2766 /* Still, we may want to support non-zero exit values. */
2767 current_cpu->thread_data[threadno].exitval = arg1 << 8;
2770 deliver_signal (current_cpu, exitsig, ppid);
2774 case TARGET_SYS_clone:
2776 int nthreads = current_cpu->m1threads + 1;
2777 void *thread_cpu_data;
2778 bfd_byte old_sp_buf[4];
2780 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2783 /* That's right, the syscall clone arguments are reversed
2784 compared to sys_clone notes in clone(2) and compared to
2785 other Linux ports (i.e. it's the same order as in the
2786 clone(2) libcall). */
2790 if (nthreads == SIM_TARGET_MAX_THREADS)
2792 retval = -cb_host_to_target_errno (cb, EAGAIN);
2796 /* FIXME: Implement the low byte. */
2797 if ((flags & ~TARGET_CSIGNAL) !=
2800 | TARGET_CLONE_FILES
2801 | TARGET_CLONE_SIGHAND)
2805 "Unimplemented clone syscall (0x%lx, 0x%lx)\n",
2806 (unsigned long) arg1, (unsigned long) arg2);
2807 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2811 if (current_cpu->thread_data == NULL)
2812 make_first_thread (current_cpu);
2814 /* The created thread will get the new SP and a cleared R10.
2815 Since it's created out of a copy of the old thread and we
2816 don't have a set-register-function that just take the
2817 cpu_data as a parameter, we set the childs values first,
2818 and write back or overwrite them in the parent after the
2820 (*CPU_REG_FETCH (current_cpu)) (current_cpu,
2821 H_GR_SP, old_sp_buf, 4);
2822 bfd_putl32 (newsp, sp_buf);
2823 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2824 H_GR_SP, sp_buf, 4);
2825 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2826 H_GR_R10, (bfd_byte *) zeros, 4);
2829 ->make_thread_cpu_data) (current_cpu,
2830 ¤t_cpu->cpu_data_placeholder);
2831 (*CPU_REG_STORE (current_cpu)) (current_cpu,
2832 H_GR_SP, old_sp_buf, 4);
2834 retval = ++current_cpu->max_threadid + TARGET_PID;
2836 /* Find an unused slot. After a few threads have been created
2837 and exited, the array is expected to be a bit fragmented.
2838 We don't reuse the first entry, though, that of the
2840 for (i = 1; i < SIM_TARGET_MAX_THREADS; i++)
2841 if (current_cpu->thread_data[i].cpu_context == NULL
2842 /* Don't reuse a zombied entry. */
2843 && current_cpu->thread_data[i].threadid == 0)
2846 memcpy (¤t_cpu->thread_data[i],
2847 ¤t_cpu->thread_data[threadno],
2848 sizeof (current_cpu->thread_data[i]));
2849 current_cpu->thread_data[i].cpu_context = thread_cpu_data;
2850 current_cpu->thread_data[i].cpu_context_atsignal = NULL;
2851 current_cpu->thread_data[i].threadid = current_cpu->max_threadid;
2852 current_cpu->thread_data[i].parent_threadid
2853 = current_cpu->thread_data[threadno].threadid;
2854 current_cpu->thread_data[i].pipe_read_fd = 0;
2855 current_cpu->thread_data[i].pipe_write_fd = 0;
2856 current_cpu->thread_data[i].at_syscall = 0;
2857 current_cpu->thread_data[i].sigpending = 0;
2858 current_cpu->thread_data[i].sigsuspended = 0;
2859 current_cpu->thread_data[i].exitsig = flags & TARGET_CSIGNAL;
2860 current_cpu->m1threads = nthreads;
2864 /* Better watch these in case they do something necessary. */
2865 case TARGET_SYS_socketcall:
2866 retval = -cb_host_to_target_errno (cb, ENOSYS);
2869 unimplemented_syscall:
2871 sim_io_eprintf (sd, "Unimplemented syscall: %d "
2872 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n", callnum,
2873 arg1, arg2, arg3, arg4, arg5, arg6);
2874 sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped,
2879 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
2880 if (callnum == TARGET_SYS_open)
2882 current_cpu->last_open_fd = retval;
2883 current_cpu->last_open_flags = arg2;
2885 current_cpu->last_syscall = callnum;
2887 /* A system call is a rescheduling point. For the time being, we don't
2888 reschedule anywhere else. */
2889 if (current_cpu->m1threads != 0
2890 /* We need to schedule off from an exiting thread that is the
2892 || (current_cpu->thread_data != NULL
2893 && current_cpu->thread_data[threadno].cpu_context == NULL))
2895 bfd_byte retval_buf[4];
2897 current_cpu->thread_data[threadno].last_execution
2898 = TARGET_TIME_MS (current_cpu);
2899 bfd_putl32 (retval, retval_buf);
2900 (*CPU_REG_STORE (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2902 current_cpu->thread_data[threadno].at_syscall = 1;
2903 reschedule (current_cpu);
2905 (*CPU_REG_FETCH (current_cpu)) (current_cpu, H_GR_R10, retval_buf, 4);
2906 retval = bfd_getl32 (retval_buf);
2912 /* Callback from simulator write saying that the pipe at (reader, writer)
2913 is now non-empty (so the writer should wait until the pipe is empty, at
2914 least not write to this or any other pipe). Simplest is to just wait
2915 until the pipe is empty. */
2918 cris_pipe_nonempty (host_callback *cb ATTRIBUTE_UNUSED,
2919 int reader, int writer)
2921 SIM_CPU *cpu = current_cpu_for_cb_callback;
2922 const bfd_byte zeros[4] = { 0, 0, 0, 0 };
2924 /* It's the current thread: we just have to re-run the current
2925 syscall instruction (presumably "break 13") and change the syscall
2926 to the special simulator-wait code. Oh, and set a marker that
2927 we're waiting, so we can disambiguate the special call from a
2930 This function may be called multiple times between cris_pipe_empty,
2931 but we must avoid e.g. decreasing PC every time. Check fd markers
2933 if (cpu->thread_data == NULL)
2935 sim_io_eprintf (CPU_STATE (cpu),
2936 "Terminating simulation due to writing pipe rd:wr %d:%d"
2937 " from one single thread\n", reader, writer);
2938 sim_engine_halt (CPU_STATE (cpu), cpu,
2939 NULL, sim_pc_get (cpu), sim_stopped, SIM_SIGILL);
2941 else if (cpu->thread_data[cpu->threadno].pipe_write_fd == 0)
2943 cpu->thread_data[cpu->threadno].pipe_write_fd = writer;
2944 cpu->thread_data[cpu->threadno].pipe_read_fd = reader;
2945 /* FIXME: We really shouldn't change registers other than R10 in
2946 syscalls (like R9), here or elsewhere. */
2947 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R9, (bfd_byte *) zeros, 4);
2948 sim_pc_set (cpu, sim_pc_get (cpu) - 2);
2952 /* Callback from simulator close or read call saying that the pipe at
2953 (reader, writer) is now empty (so the writer can write again, perhaps
2954 leave a waiting state). If there are bytes remaining, they couldn't be
2955 consumed (perhaps due to the pipe closing). */
2958 cris_pipe_empty (host_callback *cb,
2959 int reader ATTRIBUTE_UNUSED,
2963 SIM_CPU *cpu = current_cpu_for_cb_callback;
2964 bfd_byte r10_buf[4];
2965 int remaining = cb->pipe_buffer[writer].size;
2967 /* We need to find the thread that waits for this pipe. */
2968 for (i = 0; i < SIM_TARGET_MAX_THREADS; i++)
2969 if (cpu->thread_data[i].cpu_context
2970 && cpu->thread_data[i].pipe_write_fd == writer)
2973 /* Temporarily switch to this cpu context, so we can change the
2974 PC by ordinary calls. */
2976 memcpy (cpu->thread_data[cpu->threadno].cpu_context,
2977 &cpu->cpu_data_placeholder,
2978 cpu->thread_cpu_data_size);
2979 memcpy (&cpu->cpu_data_placeholder,
2980 cpu->thread_data[i].cpu_context,
2981 cpu->thread_cpu_data_size);
2983 /* The return value is supposed to contain the number of written
2984 bytes, which is the number of bytes requested and returned at
2985 the write call. We subtract the remaining bytes from that,
2986 but making sure we still get a positive number.
2987 The return value may also be a negative number; an error
2988 value. We cover this case by comparing against remaining,
2989 which is always >= 0. */
2990 (*CPU_REG_FETCH (cpu)) (cpu, H_GR_R10, r10_buf, 4);
2991 retval = (int) bfd_getl_signed_32 (r10_buf);
2992 if (retval >= remaining)
2993 bfd_putl32 (retval - remaining, r10_buf);
2994 (*CPU_REG_STORE (cpu)) (cpu, H_GR_R10, r10_buf, 4);
2996 sim_pc_set (cpu, sim_pc_get (cpu) + 2);
2997 memcpy (cpu->thread_data[i].cpu_context,
2998 &cpu->cpu_data_placeholder,
2999 cpu->thread_cpu_data_size);
3000 memcpy (&cpu->cpu_data_placeholder,
3001 cpu->thread_data[cpu->threadno].cpu_context,
3002 cpu->thread_cpu_data_size);
3003 cpu->thread_data[i].pipe_read_fd = 0;
3004 cpu->thread_data[i].pipe_write_fd = 0;
3011 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3014 cris_time (host_callback *cb ATTRIBUTE_UNUSED, long *t)
3016 long retval = TARGET_TIME (current_cpu_for_cb_callback);
3022 /* Set target-specific callback data. */
3025 cris_set_callbacks (host_callback *cb)
3027 /* Yeargh, have to cast away constness to avoid warnings. */
3028 cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
3029 cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
3031 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3032 parameter to cb_store_target_endian will make st_size negative.
3033 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3034 *unsigned*, and/or add syntax for signed-ness. */
3035 cb->stat_map = stat_map;
3036 cb->open_map = (CB_TARGET_DEFS_MAP *) open_map;
3037 cb->pipe_nonempty = cris_pipe_nonempty;
3038 cb->pipe_empty = cris_pipe_empty;
3039 cb->time = cris_time;
3042 /* Process an address exception. */
3045 cris_core_signal (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia,
3046 unsigned int map, int nr_bytes, address_word addr,
3047 transfer_type transfer, sim_core_signals sig)
3049 sim_core_signal (sd, current_cpu, cia, map, nr_bytes, addr,