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
41 /* For PATH_MAX, originally. */
46 /* From ld/sysdep.h. */
48 # define SIM_PATHMAX PATH_MAX
51 # define SIM_PATHMAX MAXPATHLEN
53 # define SIM_PATHMAX 1024
57 /* The verbatim values are from asm-cris/unistd.h. */
59 #define TARGET_SYS_exit 1
60 #define TARGET_SYS_read 3
61 #define TARGET_SYS_write 4
62 #define TARGET_SYS_open 5
63 #define TARGET_SYS_close 6
64 #define TARGET_SYS_unlink 10
65 #define TARGET_SYS_time 13
66 #define TARGET_SYS_lseek 19
67 #define TARGET_SYS_getpid 20
68 #define TARGET_SYS_kill 37
69 #define TARGET_SYS_rename 38
70 #define TARGET_SYS_pipe 42
71 #define TARGET_SYS_brk 45
72 #define TARGET_SYS_ioctl 54
73 #define TARGET_SYS_fcntl 55
74 #define TARGET_SYS_getppid 64
75 #define TARGET_SYS_setrlimit 75
76 #define TARGET_SYS_gettimeofday 78
77 #define TARGET_SYS_readlink 85
78 #define TARGET_SYS_munmap 91
79 #define TARGET_SYS_truncate 92
80 #define TARGET_SYS_ftruncate 93
81 #define TARGET_SYS_socketcall 102
82 #define TARGET_SYS_stat 106
83 #define TARGET_SYS_fstat 108
84 #define TARGET_SYS_wait4 114
85 #define TARGET_SYS_sigreturn 119
86 #define TARGET_SYS_clone 120
87 #define TARGET_SYS_uname 122
88 #define TARGET_SYS_mprotect 125
89 #define TARGET_SYS_llseek 140
90 #define TARGET_SYS__sysctl 149
91 #define TARGET_SYS_sched_setparam 154
92 #define TARGET_SYS_sched_getparam 155
93 #define TARGET_SYS_sched_setscheduler 156
94 #define TARGET_SYS_sched_getscheduler 157
95 #define TARGET_SYS_sched_yield 158
96 #define TARGET_SYS_sched_get_priority_max 159
97 #define TARGET_SYS_sched_get_priority_min 160
98 #define TARGET_SYS_mremap 163
99 #define TARGET_SYS_poll 168
100 #define TARGET_SYS_rt_sigaction 174
101 #define TARGET_SYS_rt_sigprocmask 175
102 #define TARGET_SYS_rt_sigsuspend 179
103 #define TARGET_SYS_getcwd 183
104 #define TARGET_SYS_ugetrlimit 191
105 #define TARGET_SYS_mmap2 192
106 #define TARGET_SYS_stat64 195
107 #define TARGET_SYS_lstat64 196
108 #define TARGET_SYS_fstat64 197
109 #define TARGET_SYS_geteuid32 201
110 #define TARGET_SYS_getuid32 199
111 #define TARGET_SYS_getegid32 202
112 #define TARGET_SYS_getgid32 200
113 #define TARGET_SYS_fcntl64 221
115 #define TARGET_PROT_READ 0x1
116 #define TARGET_PROT_WRITE 0x2
117 #define TARGET_PROT_EXEC 0x4
118 #define TARGET_PROT_NONE 0x0
120 #define TARGET_MAP_SHARED 0x01
121 #define TARGET_MAP_PRIVATE 0x02
122 #define TARGET_MAP_TYPE 0x0f
123 #define TARGET_MAP_FIXED 0x10
124 #define TARGET_MAP_ANONYMOUS 0x20
126 #define TARGET_CTL_KERN 1
127 #define TARGET_CTL_VM 2
128 #define TARGET_CTL_NET 3
129 #define TARGET_CTL_PROC 4
130 #define TARGET_CTL_FS 5
131 #define TARGET_CTL_DEBUG 6
132 #define TARGET_CTL_DEV 7
133 #define TARGET_CTL_BUS 8
134 #define TARGET_CTL_ABI 9
136 #define TARGET_CTL_KERN_VERSION 4
139 #define TARGET_MREMAP_MAYMOVE 1
140 #define TARGET_MREMAP_FIXED 2
142 #define TARGET_TCGETS 0x5401
144 #define TARGET_UTSNAME "#38 Sun Apr 1 00:00:00 MET 2001"
146 /* Seconds since the above date + 10 minutes. */
147 #define TARGET_EPOCH 986080200
149 /* Milliseconds since start of run. We use the number of syscalls to
150 avoid introducing noise in the execution time. */
151 #define TARGET_TIME_MS(cpu) ((cpu)->syscalls)
153 /* Seconds as in time(2). */
154 #define TARGET_TIME(cpu) (TARGET_EPOCH + TARGET_TIME_MS (cpu) / 1000)
156 #define TARGET_SCHED_OTHER 0
158 #define TARGET_RLIMIT_STACK 3
159 #define TARGET_RLIMIT_NOFILE 7
161 #define SIM_TARGET_MAX_THREADS 64
162 #define SIM_MAX_ALLOC_CHUNK (512*1024*1024)
164 /* From linux/sched.h. */
165 #define TARGET_CSIGNAL 0x000000ff
166 #define TARGET_CLONE_VM 0x00000100
167 #define TARGET_CLONE_FS 0x00000200
168 #define TARGET_CLONE_FILES 0x00000400
169 #define TARGET_CLONE_SIGHAND 0x00000800
170 #define TARGET_CLONE_PID 0x00001000
171 #define TARGET_CLONE_PTRACE 0x00002000
172 #define TARGET_CLONE_VFORK 0x00004000
173 #define TARGET_CLONE_PARENT 0x00008000
174 #define TARGET_CLONE_THREAD 0x00010000
175 #define TARGET_CLONE_SIGNAL (TARGET_CLONE_SIGHAND | TARGET_CLONE_THREAD)
177 /* From asm-cris/poll.h. */
178 #define TARGET_POLLIN 1
180 /* From asm-cris/signal.h. */
181 #define TARGET_SIG_BLOCK 0
182 #define TARGET_SIG_UNBLOCK 1
183 #define TARGET_SIG_SETMASK 2
185 #define TARGET_SIG_DFL 0
186 #define TARGET_SIG_IGN 1
187 #define TARGET_SIG_ERR ((USI)-1)
189 #define TARGET_SIGHUP 1
190 #define TARGET_SIGINT 2
191 #define TARGET_SIGQUIT 3
192 #define TARGET_SIGILL 4
193 #define TARGET_SIGTRAP 5
194 #define TARGET_SIGABRT 6
195 #define TARGET_SIGIOT 6
196 #define TARGET_SIGBUS 7
197 #define TARGET_SIGFPE 8
198 #define TARGET_SIGKILL 9
199 #define TARGET_SIGUSR1 10
200 #define TARGET_SIGSEGV 11
201 #define TARGET_SIGUSR2 12
202 #define TARGET_SIGPIPE 13
203 #define TARGET_SIGALRM 14
204 #define TARGET_SIGTERM 15
205 #define TARGET_SIGSTKFLT 16
206 #define TARGET_SIGCHLD 17
207 #define TARGET_SIGCONT 18
208 #define TARGET_SIGSTOP 19
209 #define TARGET_SIGTSTP 20
210 #define TARGET_SIGTTIN 21
211 #define TARGET_SIGTTOU 22
212 #define TARGET_SIGURG 23
213 #define TARGET_SIGXCPU 24
214 #define TARGET_SIGXFSZ 25
215 #define TARGET_SIGVTALRM 26
216 #define TARGET_SIGPROF 27
217 #define TARGET_SIGWINCH 28
218 #define TARGET_SIGIO 29
219 #define TARGET_SIGPOLL SIGIO
220 /* Actually commented out in the kernel header. */
221 #define TARGET_SIGLOST 29
222 #define TARGET_SIGPWR 30
223 #define TARGET_SIGSYS 31
225 /* From include/asm-cris/signal.h. */
226 #define TARGET_SA_NOCLDSTOP 0x00000001
227 #define TARGET_SA_NOCLDWAIT 0x00000002 /* not supported yet */
228 #define TARGET_SA_SIGINFO 0x00000004
229 #define TARGET_SA_ONSTACK 0x08000000
230 #define TARGET_SA_RESTART 0x10000000
231 #define TARGET_SA_NODEFER 0x40000000
232 #define TARGET_SA_RESETHAND 0x80000000
233 #define TARGET_SA_INTERRUPT 0x20000000 /* dummy -- ignored */
234 #define TARGET_SA_RESTORER 0x04000000
236 /* From linux/wait.h. */
237 #define TARGET_WNOHANG 1
238 #define TARGET_WUNTRACED 2
239 #define TARGET___WNOTHREAD 0x20000000
240 #define TARGET___WALL 0x40000000
241 #define TARGET___WCLONE 0x80000000
243 static const char stat_map
[] =
244 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
245 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
246 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
249 static const CB_TARGET_DEFS_MAP syscall_map
[] =
251 { CB_SYS_open
, TARGET_SYS_open
},
252 { CB_SYS_close
, TARGET_SYS_close
},
253 { CB_SYS_read
, TARGET_SYS_read
},
254 { CB_SYS_write
, TARGET_SYS_write
},
255 { CB_SYS_lseek
, TARGET_SYS_lseek
},
256 { CB_SYS_unlink
, TARGET_SYS_unlink
},
257 { CB_SYS_getpid
, TARGET_SYS_getpid
},
258 { CB_SYS_fstat
, TARGET_SYS_fstat64
},
259 { CB_SYS_lstat
, TARGET_SYS_lstat64
},
260 { CB_SYS_stat
, TARGET_SYS_stat64
},
261 { CB_SYS_pipe
, TARGET_SYS_pipe
},
262 { CB_SYS_rename
, TARGET_SYS_rename
},
263 { CB_SYS_truncate
, TARGET_SYS_truncate
},
264 { CB_SYS_ftruncate
, TARGET_SYS_ftruncate
},
268 /* An older, 32-bit-only stat mapping. */
269 static const char stat32_map
[] =
270 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
271 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
272 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
274 /* Map for calls using the 32-bit struct stat. Primarily used by the
275 newlib Linux mapping. */
276 static const CB_TARGET_DEFS_MAP syscall_stat32_map
[] =
278 { CB_SYS_fstat
, TARGET_SYS_fstat
},
279 { CB_SYS_stat
, TARGET_SYS_stat
},
283 /* Giving the true value for the running sim process will lead to
284 non-time-invariant behavior. */
285 #define TARGET_PID 42
287 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
288 we did, we'd still don't get a register number with the "16" offset. */
289 #define TARGET_SRP_REGNUM (16+11)
291 /* Extracted by applying
292 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
293 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
294 adjusting the synonyms. */
296 static const CB_TARGET_DEFS_MAP errno_map
[] =
404 { ENAMETOOLONG
, 36 },
563 { EDESTADDRREQ
, 89 },
574 #ifdef EPROTONOSUPPORT
575 { EPROTONOSUPPORT
, 93 },
577 #ifdef ESOCKTNOSUPPORT
578 { ESOCKTNOSUPPORT
, 94 },
584 { EPFNOSUPPORT
, 96 },
587 { EAFNOSUPPORT
, 97 },
593 { EADDRNOTAVAIL
, 99 },
599 { ENETUNREACH
, 101 },
605 { ECONNABORTED
, 103 },
623 { ETOOMANYREFS
, 109 },
629 { ECONNREFUSED
, 111 },
635 { EHOSTUNREACH
, 113 },
641 { EINPROGRESS
, 115 },
668 { EMEDIUMTYPE
, 124 },
673 /* Extracted by applying
674 perl -ne 'if ($_ =~ /^#define/) { split;
675 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
676 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
677 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
678 installation and removing synonyms and unnecessary items. Don't
679 forget the end-marker. */
681 /* These we treat specially, as they're used in the fcntl F_GETFL
682 syscall. For consistency, open_map is also manually edited to use
684 #define TARGET_O_ACCMODE 0x3
685 #define TARGET_O_RDONLY 0x0
686 #define TARGET_O_WRONLY 0x1
688 static const CB_TARGET_DEFS_MAP open_map
[] = {
690 { O_ACCMODE
, TARGET_O_ACCMODE
},
693 { O_RDONLY
, TARGET_O_RDONLY
},
696 { O_WRONLY
, TARGET_O_WRONLY
},
717 { O_NONBLOCK
, 0x800 },
729 { O_DIRECT
, 0x4000 },
732 { O_LARGEFILE
, 0x8000 },
735 { O_DIRECTORY
, 0x10000 },
738 { O_NOFOLLOW
, 0x20000 },
743 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
744 static SIM_CPU
*current_cpu_for_cb_callback
;
746 static int syscall_read_mem (host_callback
*, struct cb_syscall
*,
747 unsigned long, char *, int);
748 static int syscall_write_mem (host_callback
*, struct cb_syscall
*,
749 unsigned long, const char *, int);
750 static USI
create_map (SIM_DESC
, struct cris_sim_mmapped_page
**,
752 static USI
unmap_pages (SIM_DESC
, struct cris_sim_mmapped_page
**,
754 static USI
is_mapped (SIM_DESC
, struct cris_sim_mmapped_page
**,
756 static void dump_statistics (SIM_CPU
*current_cpu
);
757 static void make_first_thread (SIM_CPU
*current_cpu
);
759 /* Read/write functions for system call interface. */
762 syscall_read_mem (host_callback
*cb ATTRIBUTE_UNUSED
,
763 struct cb_syscall
*sc
,
764 unsigned long taddr
, char *buf
, int bytes
)
766 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
767 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
769 return sim_core_read_buffer (sd
, cpu
, read_map
, buf
, taddr
, bytes
);
773 syscall_write_mem (host_callback
*cb ATTRIBUTE_UNUSED
,
774 struct cb_syscall
*sc
,
775 unsigned long taddr
, const char *buf
, int bytes
)
777 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
778 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
780 return sim_core_write_buffer (sd
, cpu
, write_map
, buf
, taddr
, bytes
);
783 /* When we risk running self-modified code (as in trampolines), this is
784 called from special-case insns. The silicon CRIS CPU:s have enough
785 cache snooping implemented making this a simulator-only issue. Tests:
786 gcc.c-torture/execute/931002-1.c execution, -O3 -g
787 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
790 cris_flush_simulator_decode_cache (SIM_CPU
*current_cpu
,
791 USI pc ATTRIBUTE_UNUSED
)
793 SIM_DESC sd
= CPU_STATE (current_cpu
);
796 if (USING_SCACHE_P (sd
))
797 scache_flush_cpu (current_cpu
);
801 /* Output statistics at the end of a run. */
803 dump_statistics (SIM_CPU
*current_cpu
)
805 SIM_DESC sd
= CPU_STATE (current_cpu
);
806 CRIS_MISC_PROFILE
*profp
807 = CPU_CRIS_MISC_PROFILE (current_cpu
);
808 unsigned64 total
= profp
->basic_cycle_count
;
809 const char *textmsg
= "Basic clock cycles, total @: %llu\n";
811 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
812 what's included in the "total" count only. */
813 switch (CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
814 & FLAG_CRIS_MISC_PROFILE_ALL
)
816 case FLAG_CRIS_MISC_PROFILE_SIMPLE
:
819 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED
| FLAG_CRIS_MISC_PROFILE_SIMPLE
):
821 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
822 total
+= profp
->unaligned_mem_dword_count
;
825 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
| FLAG_CRIS_MISC_PROFILE_SIMPLE
):
826 textmsg
= "Schedulable clock cycles, total @: %llu\n";
828 += (profp
->memsrc_stall_count
829 + profp
->memraw_stall_count
830 + profp
->movemsrc_stall_count
831 + profp
->movemdst_stall_count
832 + profp
->mulsrc_stall_count
833 + profp
->jumpsrc_stall_count
834 + profp
->unaligned_mem_dword_count
);
837 case FLAG_CRIS_MISC_PROFILE_ALL
:
838 textmsg
= "All accounted clock cycles, total @: %llu\n";
840 += (profp
->memsrc_stall_count
841 + profp
->memraw_stall_count
842 + profp
->movemsrc_stall_count
843 + profp
->movemdst_stall_count
844 + profp
->movemaddr_stall_count
845 + profp
->mulsrc_stall_count
846 + profp
->jumpsrc_stall_count
847 + profp
->branch_stall_count
848 + profp
->jumptarget_stall_count
849 + profp
->unaligned_mem_dword_count
);
856 "Internal inconsistency at %s:%d",
858 sim_engine_halt (sd
, current_cpu
, NULL
, 0,
859 sim_stopped
, SIM_SIGILL
);
862 /* Historically, these messages have gone to stderr, so we'll keep it
863 that way. It's also easier to then tell it from normal program
864 output. FIXME: Add redirect option like "run -e file". */
865 sim_io_eprintf (sd
, textmsg
, total
);
867 /* For v32, unaligned_mem_dword_count should always be 0. For
868 v10, memsrc_stall_count should always be 0. */
869 sim_io_eprintf (sd
, "Memory source stall cycles: %lld\n",
870 profp
->memsrc_stall_count
871 + profp
->unaligned_mem_dword_count
);
872 sim_io_eprintf (sd
, "Memory read-after-write stall cycles: %lld\n",
873 profp
->memraw_stall_count
);
874 sim_io_eprintf (sd
, "Movem source stall cycles: %lld\n",
875 profp
->movemsrc_stall_count
);
876 sim_io_eprintf (sd
, "Movem destination stall cycles: %lld\n",
877 profp
->movemdst_stall_count
);
878 sim_io_eprintf (sd
, "Movem address stall cycles: %lld\n",
879 profp
->movemaddr_stall_count
);
880 sim_io_eprintf (sd
, "Multiplication source stall cycles: %lld\n",
881 profp
->mulsrc_stall_count
);
882 sim_io_eprintf (sd
, "Jump source stall cycles: %lld\n",
883 profp
->jumpsrc_stall_count
);
884 sim_io_eprintf (sd
, "Branch misprediction stall cycles: %lld\n",
885 profp
->branch_stall_count
);
886 sim_io_eprintf (sd
, "Jump target stall cycles: %lld\n",
887 profp
->jumptarget_stall_count
);
890 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
891 Return 1 if a overlap detected, 0 otherwise. */
894 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED
,
895 struct cris_sim_mmapped_page
**rootp
,
898 struct cris_sim_mmapped_page
*mapp
;
900 if (len
== 0 || (len
& 8191))
903 /* Iterate over the reverse-address sorted pages until we find a page in
904 or lower than the checked area. */
905 for (mapp
= *rootp
; mapp
!= NULL
&& mapp
->addr
>= addr
; mapp
= mapp
->prev
)
906 if (mapp
->addr
< addr
+ len
&& mapp
->addr
>= addr
)
912 /* Create mmapped memory. */
915 create_map (SIM_DESC sd
, struct cris_sim_mmapped_page
**rootp
, USI addr
,
918 struct cris_sim_mmapped_page
*mapp
;
919 struct cris_sim_mmapped_page
**higher_prevp
= rootp
;
920 USI new_addr
= 0x40000000;
925 new_addr
= rootp
[0]->addr
+ 8192;
932 /* Which is better: return an error for this, or just round it up? */
935 /* Do a recursive call for each page in the request. */
936 for (page_addr
= new_addr
; len
!= 0; page_addr
+= 8192, len
-= 8192)
937 if (create_map (sd
, rootp
, page_addr
, 8192) >= (USI
) -8191)
944 mapp
!= NULL
&& mapp
->addr
> new_addr
;
946 higher_prevp
= &mapp
->prev
;
948 /* Allocate the new page, on the next higher page from the last one
949 allocated, and link in the new descriptor before previous ones. */
950 mapp
= malloc (sizeof (*mapp
));
953 return (USI
) -ENOMEM
;
955 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
959 mapp
->addr
= new_addr
;
960 mapp
->prev
= *higher_prevp
;
961 *higher_prevp
= mapp
;
966 /* Unmap one or more pages. */
969 unmap_pages (SIM_DESC sd
, struct cris_sim_mmapped_page
**rootp
, USI addr
,
972 struct cris_sim_mmapped_page
*mapp
;
973 struct cris_sim_mmapped_page
**higher_prevp
= rootp
;
980 /* Which is better: return an error for this, or just round it up? */
983 /* Loop backwards to make each call is O(1) over the number of pages
984 allocated, if we're unmapping from the high end of the pages. */
985 for (page_addr
= addr
+ len
- 8192;
988 if (unmap_pages (sd
, rootp
, page_addr
, 8192) != 0)
994 for (mapp
= *rootp
; mapp
!= NULL
&& mapp
->addr
> addr
; mapp
= mapp
->prev
)
995 higher_prevp
= &mapp
->prev
;
997 if (mapp
== NULL
|| mapp
->addr
!= addr
)
1000 *higher_prevp
= mapp
->prev
;
1001 sim_core_detach (sd
, NULL
, 0, 0, addr
);
1006 /* The semantic code invokes this for illegal (unrecognized) instructions. */
1009 sim_engine_invalid_insn (SIM_CPU
*current_cpu
, IADDR cia
, SEM_PC vpc
)
1011 SIM_DESC sd
= CPU_STATE (current_cpu
);
1013 sim_engine_halt (sd
, current_cpu
, NULL
, cia
, sim_stopped
, SIM_SIGILL
);
1017 /* Handlers from the CGEN description that should not be called. */
1020 cris_bmod_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1021 UINT srcreg ATTRIBUTE_UNUSED
,
1022 USI dstreg ATTRIBUTE_UNUSED
)
1028 h_supr_set_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1029 UINT index ATTRIBUTE_UNUSED
,
1030 USI page ATTRIBUTE_UNUSED
,
1031 USI newval ATTRIBUTE_UNUSED
)
1037 h_supr_get_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1038 UINT index ATTRIBUTE_UNUSED
,
1039 USI page ATTRIBUTE_UNUSED
)
1044 /* Swap one context for another. */
1047 schedule (SIM_CPU
*current_cpu
, int next
)
1049 /* Need to mark context-switches in the trace output. */
1050 if ((CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
1051 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE
))
1052 cris_trace_printf (CPU_STATE (current_cpu
), current_cpu
,
1055 /* Copy the current context (if there is one) to its slot. */
1056 if (current_cpu
->thread_data
[current_cpu
->threadno
].cpu_context
)
1057 memcpy (current_cpu
->thread_data
[current_cpu
->threadno
].cpu_context
,
1058 ¤t_cpu
->cpu_data_placeholder
,
1059 current_cpu
->thread_cpu_data_size
);
1061 /* Copy the new context from its slot. */
1062 memcpy (¤t_cpu
->cpu_data_placeholder
,
1063 current_cpu
->thread_data
[next
].cpu_context
,
1064 current_cpu
->thread_cpu_data_size
);
1066 /* Update needed stuff to indicate the new context. */
1067 current_cpu
->threadno
= next
;
1069 /* Handle pending signals. */
1070 if (current_cpu
->thread_data
[next
].sigpending
1071 /* We don't run nested signal handlers. This means that pause(2)
1072 and sigsuspend(2) do not work in sighandlers, but that
1073 shouldn't be too hard a restriction. It also greatly
1074 simplifies the code. */
1075 && current_cpu
->thread_data
[next
].cpu_context_atsignal
== NULL
)
1079 /* See if there's really a pending, non-blocked handler. We don't
1080 queue signals, so just use the first one in ascending order. */
1081 for (sig
= 0; sig
< 64; sig
++)
1082 if (current_cpu
->thread_data
[next
].sigdata
[sig
].pending
1083 && !current_cpu
->thread_data
[next
].sigdata
[sig
].blocked
)
1089 USI pc
= sim_pc_get (current_cpu
);
1091 /* It's simpler to save the CPU context inside the simulator
1092 than on the stack. */
1093 current_cpu
->thread_data
[next
].cpu_context_atsignal
1095 ->make_thread_cpu_data
) (current_cpu
,
1096 current_cpu
->thread_data
[next
]
1099 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_SP
, regbuf
, 4);
1100 sp
= bfd_getl32 (regbuf
);
1102 /* Make sure we have an aligned stack. */
1105 /* Make room for the signal frame, aligned. FIXME: Check that
1106 the memory exists, map it in if absent. (BTW, should also
1107 implement on-access automatic stack allocation). */
1110 /* This isn't the same signal frame as the kernel uses, because
1111 we don't want to bother getting all registers on and off the
1114 /* First, we store the currently blocked signals. */
1116 for (i
= 0; i
< 32; i
++)
1118 |= current_cpu
->thread_data
[next
].sigdata
[i
+ 1].blocked
<< i
;
1119 sim_core_write_aligned_4 (current_cpu
, pc
, 0, sp
, blocked
);
1121 for (i
= 0; i
< 31; i
++)
1123 |= current_cpu
->thread_data
[next
].sigdata
[i
+ 33].blocked
<< i
;
1124 sim_core_write_aligned_4 (current_cpu
, pc
, 0, sp
+ 4, blocked
);
1126 /* Then, the actual instructions. This is CPU-specific, but we
1127 use instructions from the common subset for v10 and v32 which
1128 should be safe for the time being but could be parametrized
1130 /* MOVU.W [PC+],R9. */
1131 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 8, 0x9c5f);
1132 /* .WORD TARGET_SYS_sigreturn. */
1133 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 10,
1134 TARGET_SYS_sigreturn
);
1136 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 12, 0xe93d);
1138 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1139 instruction. Still, it doesn't matter because v10 has no
1140 delay slot for BREAK so it will not be executed). */
1141 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 16, 0x05b0);
1143 /* Modify registers to hold the right values for the sighandler
1144 context: updated stackpointer and return address pointing to
1145 the sigreturn stub. */
1146 bfd_putl32 (sp
, regbuf
);
1147 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_SP
, regbuf
, 4);
1148 bfd_putl32 (sp
+ 8, regbuf
);
1149 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, TARGET_SRP_REGNUM
,
1152 current_cpu
->thread_data
[next
].sigdata
[sig
].pending
= 0;
1154 /* Block this signal (for the duration of the sighandler). */
1155 current_cpu
->thread_data
[next
].sigdata
[sig
].blocked
= 1;
1157 sim_pc_set (current_cpu
, current_cpu
->sighandler
[sig
]);
1158 bfd_putl32 (sig
, regbuf
);
1159 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R10
,
1162 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1163 needed all this for, specifies a SA_SIGINFO call but treats it
1164 like an ordinary sighandler; only the signal number argument is
1165 inspected. To make future need to implement SA_SIGINFO
1166 correctly possible, we set the siginfo argument register to a
1167 magic (hopefully non-address) number. (NB: then, you should
1168 just need to pass the siginfo argument; it seems you probably
1169 don't need to implement the specific rt_sigreturn.) */
1170 bfd_putl32 (0xbad5161f, regbuf
);
1171 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R11
,
1174 /* The third argument is unused and the kernel sets it to 0. */
1175 bfd_putl32 (0, regbuf
);
1176 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R12
,
1181 /* No, there actually was no pending signal for this thread. Reset
1183 current_cpu
->thread_data
[next
].sigpending
= 0;
1187 /* Reschedule the simplest possible way until something else is absolutely
1189 - A. Find the next process (round-robin) that doesn't have at_syscall
1191 - B. If there is none, just run the next process, round-robin.
1192 - Clear at_syscall for the current process. */
1195 reschedule (SIM_CPU
*current_cpu
)
1199 /* Iterate over all thread slots, because after a few thread creations
1200 and exits, we don't know where the live ones are. */
1201 for (i
= (current_cpu
->threadno
+ 1) % SIM_TARGET_MAX_THREADS
;
1202 i
!= current_cpu
->threadno
;
1203 i
= (i
+ 1) % SIM_TARGET_MAX_THREADS
)
1204 if (current_cpu
->thread_data
[i
].cpu_context
1205 && current_cpu
->thread_data
[i
].at_syscall
== 0)
1207 schedule (current_cpu
, i
);
1211 /* Pick any next live thread. */
1212 for (i
= (current_cpu
->threadno
+ 1) % SIM_TARGET_MAX_THREADS
;
1213 i
!= current_cpu
->threadno
;
1214 i
= (i
+ 1) % SIM_TARGET_MAX_THREADS
)
1215 if (current_cpu
->thread_data
[i
].cpu_context
)
1217 schedule (current_cpu
, i
);
1221 /* More than one live thread, but we couldn't find the next one? */
1225 /* Set up everything to receive (or IGN) an incoming signal to the
1229 deliver_signal (SIM_CPU
*current_cpu
, int sig
, unsigned int pid
)
1232 USI pc
= sim_pc_get (current_cpu
);
1234 /* Find the thread index of the pid. */
1235 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
1236 /* Apparently it's ok to send signals to zombies (so a check for
1237 current_cpu->thread_data[i].cpu_context != NULL would be
1239 if (current_cpu
->thread_data
[i
].threadid
== pid
- TARGET_PID
)
1242 switch (current_cpu
->sighandler
[sig
])
1244 case TARGET_SIG_DFL
:
1247 /* The following according to the glibc
1248 documentation. (The kernel code has non-obvious
1249 execution paths.) */
1252 case TARGET_SIGSEGV
:
1254 case TARGET_SIGABRT
:
1255 case TARGET_SIGTRAP
:
1258 case TARGET_SIGTERM
:
1260 case TARGET_SIGQUIT
:
1261 case TARGET_SIGKILL
:
1264 case TARGET_SIGALRM
:
1265 case TARGET_SIGVTALRM
:
1266 case TARGET_SIGPROF
:
1267 case TARGET_SIGSTOP
:
1269 case TARGET_SIGPIPE
:
1270 case TARGET_SIGLOST
:
1271 case TARGET_SIGXCPU
:
1272 case TARGET_SIGXFSZ
:
1273 case TARGET_SIGUSR1
:
1274 case TARGET_SIGUSR2
:
1275 sim_io_eprintf (CPU_STATE (current_cpu
),
1276 "Exiting pid %d due to signal %d\n",
1278 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
,
1279 NULL
, pc
, sim_stopped
,
1280 sig
== TARGET_SIGABRT
1281 ? SIM_SIGABRT
: SIM_SIGILL
);
1284 /* The default for all other signals is to be ignored. */
1289 case TARGET_SIG_IGN
:
1292 case TARGET_SIGKILL
:
1293 case TARGET_SIGSTOP
:
1294 /* Can't ignore these signals. */
1295 sim_io_eprintf (CPU_STATE (current_cpu
),
1296 "Exiting pid %d due to signal %d\n",
1298 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
,
1299 NULL
, pc
, sim_stopped
, SIM_SIGILL
);
1308 /* Mark the signal as pending, making schedule () check
1309 closer. The signal will be handled when the thread is
1310 scheduled and the signal is unblocked. */
1311 current_cpu
->thread_data
[i
].sigdata
[sig
].pending
= 1;
1312 current_cpu
->thread_data
[i
].sigpending
= 1;
1317 sim_io_eprintf (CPU_STATE (current_cpu
),
1318 "Unimplemented signal: %d\n", sig
);
1319 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
, NULL
, pc
,
1320 sim_stopped
, SIM_SIGILL
);
1325 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu
)),
1329 /* Make the vector and the first item, the main thread. */
1332 make_first_thread (SIM_CPU
*current_cpu
)
1334 current_cpu
->thread_data
1336 SIM_TARGET_MAX_THREADS
1337 * sizeof (current_cpu
->thread_data
[0]));
1338 current_cpu
->thread_data
[0].cpu_context
1339 = (*current_cpu
->make_thread_cpu_data
) (current_cpu
,
1341 ->cpu_data_placeholder
);
1342 current_cpu
->thread_data
[0].parent_threadid
= -1;
1344 /* For good measure. */
1345 if (TARGET_SIG_DFL
!= 0)
1349 /* Main function: the handler of the "break 13" syscall insn. */
1352 cris_break_13_handler (SIM_CPU
*current_cpu
, USI callnum
, USI arg1
,
1353 USI arg2
, USI arg3
, USI arg4
, USI arg5
, USI arg6
,
1357 SIM_DESC sd
= CPU_STATE (current_cpu
);
1358 host_callback
*cb
= STATE_CALLBACK (sd
);
1360 int threadno
= current_cpu
->threadno
;
1362 current_cpu
->syscalls
++;
1364 CB_SYSCALL_INIT (&s
);
1370 if (callnum
== TARGET_SYS_exit
&& current_cpu
->m1threads
== 0)
1372 if (CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
1373 & FLAG_CRIS_MISC_PROFILE_ALL
)
1374 dump_statistics (current_cpu
);
1375 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_exited
, arg1
);
1379 s
.p2
= (PTR
) current_cpu
;
1380 s
.read_mem
= syscall_read_mem
;
1381 s
.write_mem
= syscall_write_mem
;
1383 current_cpu_for_cb_callback
= current_cpu
;
1385 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1388 sim_io_eprintf (sd
, "Break 13: invalid %d? Returned %ld\n", callnum
,
1390 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
1393 retval
= s
.result
== -1 ? -s
.errcode
: s
.result
;
1395 if (s
.errcode
!= 0 && s
.errcode
== cb_host_to_target_errno (cb
, ENOSYS
))
1397 /* If the generic simulator call said ENOSYS, then let's try the
1398 ones we know ourselves.
1400 The convention is to provide *very limited* functionality on an
1401 as-needed basis, only what's covered by the test-suite, tests
1402 added when functionality changes and abort with a descriptive
1403 message for *everything* else. Where there's no test-case, we
1408 /* It's a pretty safe bet that the "old setup() system call"
1409 number will not be re-used; we can't say the same for higher
1410 numbers. We treat this simulator-generated call as "wait
1411 forever"; we re-run this insn. The wait is ended by a
1412 callback. Sanity check that this is the reason we got
1414 if (current_cpu
->thread_data
== NULL
1415 || (current_cpu
->thread_data
[threadno
].pipe_write_fd
== 0))
1416 goto unimplemented_syscall
;
1418 sim_pc_set (current_cpu
, pc
);
1422 case TARGET_SYS_fcntl64
:
1423 case TARGET_SYS_fcntl
:
1428 Glibc checks stdin, stdout and stderr fd:s for
1429 close-on-exec security sanity. We just need to provide a
1430 OK return value. If we really need to have a
1431 close-on-exec flag true, we could just do a real fcntl
1437 /* F_SETFD. Just ignore attempts to set the close-on-exec
1443 /* F_GETFL. Check for the special case for open+fdopen. */
1444 if (current_cpu
->last_syscall
== TARGET_SYS_open
1445 && arg1
== current_cpu
->last_open_fd
)
1447 retval
= current_cpu
->last_open_flags
& TARGET_O_ACCMODE
;
1452 /* Because we can't freopen fd:s 0, 1, 2 to mean
1453 something else than stdin, stdout and stderr
1454 (sim/common/syscall.c:cb_syscall special cases fd
1455 0, 1 and 2), we know what flags that we can
1456 sanely return for these fd:s. */
1457 retval
= TARGET_O_RDONLY
;
1460 else if (arg1
== 1 || arg1
== 2)
1462 retval
= TARGET_O_WRONLY
;
1467 /* Abort for all other cases. */
1468 sim_io_eprintf (sd
, "Unimplemented %s syscall "
1469 "(fd: 0x%lx: cmd: 0x%lx arg: 0x%lx)\n",
1470 callnum
== TARGET_SYS_fcntl
1471 ? "fcntl" : "fcntl64",
1472 (unsigned long) (USI
) arg1
,
1473 (unsigned long) (USI
) arg2
,
1474 (unsigned long) (USI
) arg3
);
1475 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
1481 case TARGET_SYS_uname
:
1483 /* Fill in a few constants to appease glibc. */
1484 static const char sim_utsname
[6][65] =
1494 if ((s
.write_mem
) (cb
, &s
, arg1
, (const char *) sim_utsname
,
1495 sizeof (sim_utsname
))
1496 != sizeof (sim_utsname
))
1497 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
1503 case TARGET_SYS_geteuid32
:
1504 /* We tell the truth with these. Maybe we shouldn't, but it
1505 should match the "stat" information. */
1506 retval
= geteuid ();
1509 case TARGET_SYS_getuid32
:
1513 case TARGET_SYS_getegid32
:
1514 retval
= getegid ();
1517 case TARGET_SYS_getgid32
:
1521 case TARGET_SYS_brk
:
1522 /* Most often, we just return the argument, like the Linux
1527 retval
= current_cpu
->endbrk
;
1528 else if (arg1
<= current_cpu
->endmem
)
1529 current_cpu
->endbrk
= arg1
;
1532 USI new_end
= (arg1
+ 8191) & ~8191;
1534 /* If the simulator wants to brk more than a certain very
1535 large amount, something is wrong. FIXME: Return an error
1536 or abort? Have command-line selectable? */
1537 if (new_end
- current_cpu
->endmem
> SIM_MAX_ALLOC_CHUNK
)
1539 current_cpu
->endbrk
= current_cpu
->endmem
;
1540 retval
= current_cpu
->endmem
;
1544 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
1545 current_cpu
->endmem
,
1546 new_end
- current_cpu
->endmem
,
1548 current_cpu
->endbrk
= arg1
;
1549 current_cpu
->endmem
= new_end
;
1553 case TARGET_SYS_getpid
:
1554 /* Correct until CLONE_THREAD is implemented. */
1555 retval
= current_cpu
->thread_data
== NULL
1557 : TARGET_PID
+ current_cpu
->thread_data
[threadno
].threadid
;
1560 case TARGET_SYS_getppid
:
1561 /* Correct until CLONE_THREAD is implemented. */
1562 retval
= current_cpu
->thread_data
== NULL
1565 + current_cpu
->thread_data
[threadno
].parent_threadid
);
1568 case TARGET_SYS_mmap2
:
1577 /* If the simulator wants to mmap more than the very large
1578 limit, something is wrong. FIXME: Return an error or
1579 abort? Have command-line selectable? */
1580 if (len
> SIM_MAX_ALLOC_CHUNK
)
1582 retval
= -cb_host_to_target_errno (cb
, ENOMEM
);
1586 if ((prot
!= (TARGET_PROT_READ
| TARGET_PROT_WRITE
)
1588 != (TARGET_PROT_READ
1590 | TARGET_PROT_EXEC
))
1591 && prot
!= TARGET_PROT_READ
)
1592 || (flags
!= (TARGET_MAP_ANONYMOUS
| TARGET_MAP_PRIVATE
)
1593 && flags
!= TARGET_MAP_PRIVATE
1594 && flags
!= TARGET_MAP_SHARED
)
1595 || (fd
!= (USI
) -1 && prot
!= TARGET_PROT_READ
)
1598 sim_io_eprintf (sd
, "Unimplemented mmap2 call "
1599 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1600 (unsigned long) arg1
,
1601 (unsigned long) arg2
,
1602 (unsigned long) arg3
,
1603 (unsigned long) arg4
,
1604 (unsigned long) arg5
,
1605 (unsigned long) arg6
);
1606 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
1610 else if (fd
!= (USI
) -1)
1617 /* A non-aligned argument is allowed for files. */
1618 USI newlen
= (len
+ 8191) & ~8191;
1620 /* We only support read, which we should already have
1621 checked. Check again anyway. */
1622 if (prot
!= TARGET_PROT_READ
)
1626 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1629 if (newaddr
>= (USI
) -8191)
1632 retval
= -cb_host_to_target_errno (cb
, -(SI
) newaddr
);
1636 /* Find the current position in the file. */
1637 s
.func
= TARGET_SYS_lseek
;
1641 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1648 /* Use the standard read callback to read in "len"
1650 s
.func
= TARGET_SYS_read
;
1654 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1657 if ((USI
) s
.result
!= len
)
1660 /* After reading, we need to go back to the previous
1661 position in the file. */
1662 s
.func
= TARGET_SYS_lseek
;
1666 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1668 if (pos
!= (USI
) s
.result
)
1676 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1677 (len
+ 8191) & ~8191);
1679 if (newaddr
>= (USI
) -8191)
1680 retval
= -cb_host_to_target_errno (cb
, -(SI
) newaddr
);
1687 case TARGET_SYS_mprotect
:
1689 /* We only cover the case of linuxthreads mprotecting out its
1690 stack guard page. */
1695 if ((addr
& 8191) != 0
1697 || prot
!= TARGET_PROT_NONE
1698 || !is_mapped (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1701 sim_io_eprintf (sd
, "Unimplemented mprotect call "
1702 "(0x%lx, 0x%lx, 0x%lx)\n",
1703 (unsigned long) arg1
,
1704 (unsigned long) arg2
,
1705 (unsigned long) arg3
);
1706 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
1711 /* FIXME: We should account for pages like this that are
1712 "mprotected out". For now, we just tell the simulator
1713 core to remove that page from its map. */
1714 sim_core_detach (sd
, NULL
, 0, 0, addr
);
1719 case TARGET_SYS_ioctl
:
1721 /* We support only a very limited functionality: checking
1722 stdout with TCGETS to perform the isatty function. The
1723 TCGETS ioctl isn't actually performed or the result used by
1724 an isatty () caller in a "hello, world" program; only the
1725 return value is then used. Maybe we shouldn't care about
1726 the environment of the simulator regarding isatty, but
1727 that's been working before, in the xsim simulator. */
1728 if (arg2
== TARGET_TCGETS
&& arg1
== 1)
1729 retval
= isatty (1) ? 0 : cb_host_to_target_errno (cb
, EINVAL
);
1731 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
1735 case TARGET_SYS_munmap
:
1740 = unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1742 retval
= result
!= 0 ? -cb_host_to_target_errno (cb
, result
) : 0;
1746 case TARGET_SYS_wait4
:
1754 /* FIXME: We're not properly implementing __WCLONE, and we
1755 don't really need the special casing so we might as well
1756 make this general. */
1757 if ((!(pid
== (USI
) -1
1758 && options
== (TARGET___WCLONE
| TARGET_WNOHANG
)
1761 && (options
== TARGET___WCLONE
1762 || options
== TARGET___WALL
)))
1764 || current_cpu
->thread_data
== NULL
)
1766 sim_io_eprintf (sd
, "Unimplemented wait4 call "
1767 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1768 (unsigned long) arg1
,
1769 (unsigned long) arg2
,
1770 (unsigned long) arg3
,
1771 (unsigned long) arg4
);
1772 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
1777 if (pid
== (USI
) -1)
1778 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
1780 if (current_cpu
->thread_data
[threadno
].threadid
1781 == current_cpu
->thread_data
[i
].parent_threadid
1782 && current_cpu
->thread_data
[i
].threadid
!= 0
1783 && current_cpu
->thread_data
[i
].cpu_context
== NULL
)
1785 /* A zombied child. Get the exit value and clear the
1786 zombied entry so it will be reused. */
1787 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, saddr
,
1789 ->thread_data
[i
].exitval
);
1791 = current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
;
1792 memset (¤t_cpu
->thread_data
[i
], 0,
1793 sizeof (current_cpu
->thread_data
[i
]));
1799 /* We're waiting for a specific PID. If we don't find
1800 it zombied on this run, rerun the syscall. */
1801 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
1802 if (pid
== current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
1803 && current_cpu
->thread_data
[i
].cpu_context
== NULL
)
1806 /* Get the exit value if the caller wants it. */
1807 sim_core_write_unaligned_4 (current_cpu
, pc
, 0,
1814 = current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
;
1815 memset (¤t_cpu
->thread_data
[i
], 0,
1816 sizeof (current_cpu
->thread_data
[i
]));
1821 sim_pc_set (current_cpu
, pc
);
1824 retval
= -cb_host_to_target_errno (cb
, ECHILD
);
1829 case TARGET_SYS_rt_sigaction
:
1837 __sighandler_t sa_handler;
1838 unsigned long sa_flags;
1839 void (*sa_restorer)(void);
1845 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, old_sa
+ 0,
1846 current_cpu
->sighandler
[signum
]);
1847 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 4, 0);
1848 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 8, 0);
1850 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
1851 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 12, 0);
1852 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 16, 0);
1856 USI target_sa_handler
1857 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
);
1859 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 4);
1860 USI target_sa_restorer
1861 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 8);
1862 USI target_sa_mask_low
1863 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 12);
1864 USI target_sa_mask_high
1865 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 16);
1867 /* We won't interrupt a syscall so we won't restart it,
1868 but a signal(2) call ends up syscalling rt_sigaction
1869 with this flag, so we have to handle it. The
1870 sa_restorer field contains garbage when not
1871 TARGET_SA_RESTORER, so don't look at it. For the
1872 time being, we don't nest sighandlers, so we
1873 ignore the sa_mask, which simplifies things. */
1874 if ((target_sa_flags
!= 0
1875 && target_sa_flags
!= TARGET_SA_RESTART
1876 && target_sa_flags
!= (TARGET_SA_RESTART
|TARGET_SA_SIGINFO
))
1877 || target_sa_handler
== 0)
1879 sim_io_eprintf (sd
, "Unimplemented rt_sigaction "
1881 "0x%lx: [0x%x, 0x%x, 0x%x, "
1884 (unsigned long) arg1
,
1885 (unsigned long) arg2
,
1886 target_sa_handler
, target_sa_flags
,
1888 target_sa_mask_low
, target_sa_mask_high
,
1889 (unsigned long) arg3
);
1890 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
1894 current_cpu
->sighandler
[signum
] = target_sa_handler
;
1896 /* Because we may have unblocked signals, one may now be
1897 pending, if there are threads, that is. */
1898 if (current_cpu
->thread_data
)
1899 current_cpu
->thread_data
[threadno
].sigpending
= 1;
1905 case TARGET_SYS_mremap
:
1911 USI new_addr
= arg5
;
1914 if (new_len
== old_len
)
1915 /* The program and/or library is possibly confused but
1916 this is a valid call. Happens with ipps-1.40 on file
1919 else if (new_len
< old_len
)
1921 /* Shrinking is easy. */
1922 if (unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
,
1923 addr
+ new_len
, old_len
- new_len
) != 0)
1924 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
1928 else if (! is_mapped (sd
, ¤t_cpu
->highest_mmapped_page
,
1929 addr
+ old_len
, new_len
- old_len
))
1931 /* If the extension isn't mapped, we can just add it. */
1933 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
,
1934 addr
+ old_len
, new_len
- old_len
);
1936 if (mapped_addr
> (USI
) -8192)
1937 retval
= -cb_host_to_target_errno (cb
, -(SI
) mapped_addr
);
1941 else if (flags
& TARGET_MREMAP_MAYMOVE
)
1943 /* Create a whole new map and copy the contents
1944 block-by-block there. We ignore the new_addr argument
1947 USI prev_addr
= addr
;
1948 USI prev_len
= old_len
;
1951 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
,
1954 if (mapped_addr
> (USI
) -8192)
1956 retval
= -cb_host_to_target_errno (cb
, -(SI
) new_addr
);
1960 retval
= mapped_addr
;
1963 old_len
-= 8192, mapped_addr
+= 8192, addr
+= 8192)
1965 if (sim_core_read_buffer (sd
, current_cpu
, read_map
, buf
,
1967 || sim_core_write_buffer (sd
, current_cpu
, 0, buf
,
1968 mapped_addr
, 8192) != 8192)
1972 if (unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
,
1973 prev_addr
, prev_len
) != 0)
1977 retval
= -cb_host_to_target_errno (cb
, -ENOMEM
);
1981 case TARGET_SYS_poll
:
1983 int npollfds
= arg2
;
1999 /* Check that this is the expected poll call from
2000 linuxthreads/manager.c; we don't support anything else.
2001 Remember, fd == 0 isn't supported. */
2003 || ((fd
= sim_core_read_unaligned_4 (current_cpu
, pc
,
2005 || ((events
= sim_core_read_unaligned_2 (current_cpu
, pc
,
2008 || ((cb
->fstat
) (cb
, fd
, &buf
) != 0
2009 || (buf
.st_mode
& S_IFIFO
) == 0)
2010 || current_cpu
->thread_data
== NULL
)
2012 sim_io_eprintf (sd
, "Unimplemented poll syscall "
2013 "(0x%lx: [0x%x, 0x%x, x], 0x%lx, 0x%lx)\n",
2014 (unsigned long) arg1
, fd
, events
,
2015 (unsigned long) arg2
, (unsigned long) arg3
);
2016 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
2022 /* Iterate over threads; find a marker that a writer is
2023 sleeping, waiting for a reader. */
2024 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
2025 if (current_cpu
->thread_data
[i
].cpu_context
!= NULL
2026 && current_cpu
->thread_data
[i
].pipe_read_fd
== fd
)
2028 revents
= TARGET_POLLIN
;
2033 /* Timeout decreases with whatever time passed between the
2034 last syscall and this. That's not exactly right for the
2035 first call, but it's close enough that it isn't
2036 worthwhile to complicate matters by making that a special
2039 -= (TARGET_TIME_MS (current_cpu
)
2040 - (current_cpu
->thread_data
[threadno
].last_execution
));
2042 /* Arrange to repeat this syscall until timeout or event,
2043 decreasing timeout at each iteration. */
2044 if (timeout
> 0 && revents
== 0)
2046 bfd_byte timeout_buf
[4];
2048 bfd_putl32 (timeout
, timeout_buf
);
2049 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2050 H_GR_R12
, timeout_buf
, 4);
2051 sim_pc_set (current_cpu
, pc
);
2056 sim_core_write_unaligned_2 (current_cpu
, pc
, 0, ufds
+ 4 + 2,
2061 case TARGET_SYS_time
:
2063 retval
= (int) (*cb
->time
) (cb
, 0L);
2065 /* At time of this writing, CB_SYSCALL_time doesn't do the
2066 part of setting *arg1 to the return value. */
2068 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
, retval
);
2072 case TARGET_SYS_gettimeofday
:
2075 USI ts
= TARGET_TIME (current_cpu
);
2076 USI tms
= TARGET_TIME_MS (current_cpu
);
2078 /* First dword is seconds since TARGET_EPOCH. */
2079 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
, ts
);
2081 /* Second dword is microseconds. */
2082 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 4,
2083 (tms
% 1000) * 1000);
2087 /* Time-zone info is always cleared. */
2088 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
, 0);
2089 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
+ 4, 0);
2094 case TARGET_SYS_llseek
:
2096 /* If it fits, tweak parameters to fit the "generic" 32-bit
2097 lseek and use that. */
2105 if (!((offs_hi
== 0 && offs_lo
>= 0)
2106 || (offs_hi
== -1 && offs_lo
< 0)))
2109 "Unimplemented llseek offset,"
2110 " fd %d: 0x%x:0x%x\n",
2111 fd
, (unsigned) arg2
, (unsigned) arg3
);
2112 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2116 s
.func
= TARGET_SYS_lseek
;
2119 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
2121 sim_io_eprintf (sd
, "Break 13: invalid %d? Returned %ld\n", callnum
,
2123 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
2126 retval
= -s
.errcode
;
2129 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, resultp
,
2131 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, resultp
+ 4,
2132 s
.result
< 0 ? -1 : 0);
2137 /* This one does have a generic callback function, but at the time
2138 of this writing, cb_syscall does not have code for it, and we
2139 need target-specific code for the threads implementation
2141 case TARGET_SYS_kill
:
2148 /* At kill(2), glibc sets signal masks such that the thread
2149 machinery is initialized. Still, there is and was only
2151 if (current_cpu
->max_threadid
== 0)
2153 if (pid
!= TARGET_PID
)
2155 retval
= -cb_host_to_target_errno (cb
, EPERM
);
2159 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2160 if (sig
== TARGET_SIGABRT
)
2161 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2162 the end-point for failing GCC test-cases. */
2163 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2167 sim_io_eprintf (sd
, "Unimplemented signal: %d\n", sig
);
2168 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2172 /* This will not be reached. */
2176 retval
= deliver_signal (current_cpu
, sig
, pid
);
2180 case TARGET_SYS_rt_sigprocmask
:
2187 if (how
!= TARGET_SIG_BLOCK
2188 && how
!= TARGET_SIG_SETMASK
2189 && how
!= TARGET_SIG_UNBLOCK
)
2191 sim_io_eprintf (sd
, "Unimplemented rt_sigprocmask syscall "
2192 "(0x%x, 0x%x, 0x%x)\n", arg1
, arg2
, arg3
);
2193 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2202 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2205 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2208 /* The sigmask is kept in the per-thread data, so we may
2209 need to create the first one. */
2210 if (current_cpu
->thread_data
== NULL
)
2211 make_first_thread (current_cpu
);
2213 if (how
== TARGET_SIG_SETMASK
)
2214 for (i
= 0; i
< 64; i
++)
2215 current_cpu
->thread_data
[threadno
].sigdata
[i
].blocked
= 0;
2217 for (i
= 0; i
< 32; i
++)
2218 if ((set_low
& (1 << i
)))
2219 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 1].blocked
2220 = (how
!= TARGET_SIG_UNBLOCK
);
2222 for (i
= 0; i
< 31; i
++)
2223 if ((set_high
& (1 << i
)))
2224 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 33].blocked
2225 = (how
!= TARGET_SIG_UNBLOCK
);
2227 /* The mask changed, so a signal may be unblocked for
2229 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2237 for (i
= 0; i
< 32; i
++)
2238 if (current_cpu
->thread_data
[threadno
]
2239 .sigdata
[i
+ 1].blocked
)
2241 for (i
= 0; i
< 31; i
++)
2242 if (current_cpu
->thread_data
[threadno
]
2243 .sigdata
[i
+ 33].blocked
)
2246 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldsetp
+ 0, set_low
);
2247 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldsetp
+ 4, set_high
);
2254 case TARGET_SYS_sigreturn
:
2258 int was_sigsuspended
;
2260 if (current_cpu
->thread_data
== NULL
2261 /* The CPU context is saved with the simulator data, not
2262 on the stack as in the real world. */
2263 || (current_cpu
->thread_data
[threadno
].cpu_context_atsignal
2266 sim_io_eprintf (sd
, "Invalid sigreturn syscall: no signal"
2268 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2269 (unsigned long) arg1
,
2270 (unsigned long) arg2
,
2271 (unsigned long) arg3
,
2272 (unsigned long) arg4
,
2273 (unsigned long) arg5
,
2274 (unsigned long) arg6
);
2275 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2280 = current_cpu
->thread_data
[threadno
].sigsuspended
;
2282 /* Restore the sigmask, either from the stack copy made when
2283 the sighandler was called, or from the saved state
2284 specifically for sigsuspend(2). */
2285 if (was_sigsuspended
)
2287 current_cpu
->thread_data
[threadno
].sigsuspended
= 0;
2288 for (i
= 0; i
< 64; i
++)
2289 current_cpu
->thread_data
[threadno
].sigdata
[i
].blocked
2290 = current_cpu
->thread_data
[threadno
]
2291 .sigdata
[i
].blocked_suspendsave
;
2299 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
,
2300 H_GR_SP
, regbuf
, 4);
2301 sp
= bfd_getl32 (regbuf
);
2303 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, sp
);
2305 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, sp
+ 4);
2307 for (i
= 0; i
< 32; i
++)
2308 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 1].blocked
2309 = (set_low
& (1 << i
)) != 0;
2310 for (i
= 0; i
< 31; i
++)
2311 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 33].blocked
2312 = (set_high
& (1 << i
)) != 0;
2315 /* The mask changed, so a signal may be unblocked for
2317 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2319 memcpy (¤t_cpu
->cpu_data_placeholder
,
2320 current_cpu
->thread_data
[threadno
].cpu_context_atsignal
,
2321 current_cpu
->thread_cpu_data_size
);
2322 free (current_cpu
->thread_data
[threadno
].cpu_context_atsignal
);
2323 current_cpu
->thread_data
[threadno
].cpu_context_atsignal
= NULL
;
2325 /* The return value must come from the saved R10. */
2326 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_R10
, regbuf
, 4);
2327 retval
= bfd_getl32 (regbuf
);
2329 /* We must also break the "sigsuspension loop". */
2330 if (was_sigsuspended
)
2331 sim_pc_set (current_cpu
, sim_pc_get (current_cpu
) + 2);
2335 case TARGET_SYS_rt_sigsuspend
:
2342 sim_io_eprintf (sd
, "Unimplemented rt_sigsuspend syscall"
2343 " arguments (0x%lx, 0x%lx)\n",
2344 (unsigned long) arg1
, (unsigned long) arg2
);
2345 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2349 /* Don't change the signal mask if we're already in
2350 sigsuspend state (i.e. this syscall is a rerun). */
2351 else if (!current_cpu
->thread_data
[threadno
].sigsuspended
)
2354 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2357 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2361 /* Save the current sigmask and insert the user-supplied
2363 for (i
= 0; i
< 32; i
++)
2365 current_cpu
->thread_data
[threadno
]
2366 .sigdata
[i
+ 1].blocked_suspendsave
2367 = current_cpu
->thread_data
[threadno
]
2368 .sigdata
[i
+ 1].blocked
;
2370 current_cpu
->thread_data
[threadno
]
2371 .sigdata
[i
+ 1].blocked
= (set_low
& (1 << i
)) != 0;
2373 for (i
= 0; i
< 31; i
++)
2375 current_cpu
->thread_data
[threadno
]
2376 .sigdata
[i
+ 33].blocked_suspendsave
2377 = current_cpu
->thread_data
[threadno
]
2378 .sigdata
[i
+ 33].blocked
;
2379 current_cpu
->thread_data
[threadno
]
2380 .sigdata
[i
+ 33].blocked
= (set_high
& (1 << i
)) != 0;
2383 current_cpu
->thread_data
[threadno
].sigsuspended
= 1;
2385 /* The mask changed, so a signal may be unblocked for
2387 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2390 /* Because we don't use arg1 (newsetp) when this syscall is
2391 rerun, it doesn't matter that we overwrite it with the
2392 (constant) return value. */
2393 retval
= -cb_host_to_target_errno (cb
, EINTR
);
2394 sim_pc_set (current_cpu
, pc
);
2398 /* Add case labels here for other syscalls using the 32-bit
2399 "struct stat", provided they have a corresponding simulator
2400 function of course. */
2401 case TARGET_SYS_stat
:
2402 case TARGET_SYS_fstat
:
2404 /* As long as the infrastructure doesn't cache anything
2405 related to the stat mapping, this trick gets us a dual
2406 "struct stat"-type mapping in the least error-prone way. */
2407 const char *saved_map
= cb
->stat_map
;
2408 CB_TARGET_DEFS_MAP
*saved_syscall_map
= cb
->syscall_map
;
2410 cb
->syscall_map
= (CB_TARGET_DEFS_MAP
*) syscall_stat32_map
;
2411 cb
->stat_map
= stat32_map
;
2413 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
2416 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2419 retval
= s
.result
== -1 ? -s
.errcode
: s
.result
;
2421 cb
->stat_map
= saved_map
;
2422 cb
->syscall_map
= saved_syscall_map
;
2426 case TARGET_SYS_getcwd
:
2431 char *cwd
= xmalloc (SIM_PATHMAX
);
2432 if (cwd
!= getcwd (cwd
, SIM_PATHMAX
))
2435 /* FIXME: When and if we support chdir, we need something
2436 a bit more elaborate. */
2437 if (simulator_sysroot
[0] != '\0')
2440 retval
= -cb_host_to_target_errno (cb
, ERANGE
);
2441 if (strlen (cwd
) + 1 <= size
)
2443 retval
= strlen (cwd
) + 1;
2444 if (sim_core_write_buffer (sd
, current_cpu
, 0, cwd
,
2446 != (unsigned int) retval
)
2447 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2453 case TARGET_SYS_readlink
:
2458 char *pbuf
= xmalloc (SIM_PATHMAX
);
2459 char *lbuf
= xmalloc (SIM_PATHMAX
);
2460 char *lbuf_alloc
= lbuf
;
2465 if (sim_core_read_unaligned_1 (current_cpu
, pc
, 0, path
) == '/')
2467 strcpy (pbuf
, simulator_sysroot
);
2468 o
+= strlen (simulator_sysroot
);
2471 for (i
= 0; i
+ o
< SIM_PATHMAX
; i
++)
2474 = sim_core_read_unaligned_1 (current_cpu
, pc
, 0, path
+ i
);
2475 if (pbuf
[i
+ o
] == 0)
2479 if (i
+ o
== SIM_PATHMAX
)
2481 retval
= -cb_host_to_target_errno (cb
, ENAMETOOLONG
);
2485 /* Intervene calls for certain files expected in the target
2486 proc file system. */
2487 if (strcmp (pbuf
+ strlen (simulator_sysroot
),
2488 "/proc/" XSTRING (TARGET_PID
) "/exe") == 0)
2491 = (STATE_PROG_ARGV (sd
) != NULL
2492 ? *STATE_PROG_ARGV (sd
) : NULL
);
2494 if (argv0
== NULL
|| *argv0
== '.')
2496 sim_io_eprintf (sd
, "Unimplemented readlink syscall "
2497 "(0x%lx: [\"%s\"], 0x%lx)\n",
2498 (unsigned long) arg1
, pbuf
,
2499 (unsigned long) arg2
);
2500 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2504 else if (*argv0
== '/')
2506 if (strncmp (simulator_sysroot
, argv0
,
2507 strlen (simulator_sysroot
)) == 0)
2508 argv0
+= strlen (simulator_sysroot
);
2510 strcpy (lbuf
, argv0
);
2511 nchars
= strlen (argv0
) + 1;
2515 if (getcwd (lbuf
, SIM_PATHMAX
) != NULL
2516 && strlen (lbuf
) + 2 + strlen (argv0
) < SIM_PATHMAX
)
2518 if (strncmp (simulator_sysroot
, lbuf
,
2519 strlen (simulator_sysroot
)) == 0)
2520 lbuf
+= strlen (simulator_sysroot
);
2523 strcat (lbuf
, argv0
);
2524 nchars
= strlen (lbuf
) + 1;
2531 nchars
= readlink (pbuf
, lbuf
, SIM_PATHMAX
);
2533 /* We trust that the readlink result returns a *relative*
2534 link, or one already adjusted for the file-path-prefix.
2535 (We can't generally tell the difference, so we go with
2536 the easiest decision; no adjustment.) */
2540 retval
= -cb_host_to_target_errno (cb
, errno
);
2544 if (bufsiz
< nchars
)
2547 if (sim_core_write_buffer (sd
, current_cpu
, write_map
, lbuf
,
2548 buf
, nchars
) != (unsigned int) nchars
)
2549 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2558 case TARGET_SYS_sched_getscheduler
:
2562 /* FIXME: Search (other) existing threads. */
2563 if (pid
!= 0 && pid
!= TARGET_PID
)
2564 retval
= -cb_host_to_target_errno (cb
, ESRCH
);
2566 retval
= TARGET_SCHED_OTHER
;
2570 case TARGET_SYS_sched_getparam
:
2576 struct sched_param {
2580 if (pid
!= 0 && pid
!= TARGET_PID
)
2581 retval
= -cb_host_to_target_errno (cb
, ESRCH
);
2584 /* FIXME: Save scheduler setting before threads are
2586 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, paramp
,
2587 current_cpu
->thread_data
!= NULL
2589 ->thread_data
[threadno
]
2597 case TARGET_SYS_sched_setparam
:
2602 if ((pid
!= 0 && pid
!= TARGET_PID
)
2603 || sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2605 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2611 case TARGET_SYS_sched_setscheduler
:
2617 if ((pid
!= 0 && pid
!= TARGET_PID
)
2618 || policy
!= TARGET_SCHED_OTHER
2619 || sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2621 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2623 /* FIXME: Save scheduler setting to be read in later
2624 sched_getparam calls. */
2629 case TARGET_SYS_sched_yield
:
2630 /* We reschedule to the next thread after a syscall anyway, so
2631 we don't have to do anything here than to set the return
2636 case TARGET_SYS_sched_get_priority_min
:
2637 case TARGET_SYS_sched_get_priority_max
:
2639 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2644 case TARGET_SYS_ugetrlimit
:
2646 unsigned int curlim
, maxlim
;
2647 if (arg1
!= TARGET_RLIMIT_STACK
&& arg1
!= TARGET_RLIMIT_NOFILE
)
2649 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2655 unsigned long rlim_cur;
2656 unsigned long rlim_max;
2658 if (arg1
== TARGET_RLIMIT_NOFILE
)
2660 /* Sadly a very low limit. Better not lie, though. */
2661 maxlim
= curlim
= MAX_CALLBACK_FDS
;
2663 else /* arg1 == TARGET_RLIMIT_STACK */
2665 maxlim
= 0xffffffff;
2668 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
, curlim
);
2669 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
+ 4, maxlim
);
2674 case TARGET_SYS_setrlimit
:
2675 if (arg1
!= TARGET_RLIMIT_STACK
)
2677 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2680 /* FIXME: Save values for future ugetrlimit calls. */
2684 /* Provide a very limited subset of the sysctl functions, and
2685 abort for the rest. */
2686 case TARGET_SYS__sysctl
:
2689 struct __sysctl_args {
2696 unsigned long __unused[4];
2698 SI name
= sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
);
2699 SI name0
= name
== 0
2700 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, name
);
2701 SI name1
= name
== 0
2702 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, name
+ 4);
2704 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 4);
2706 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 8);
2708 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 12);
2709 SI oldlen
= oldlenp
== 0
2710 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, oldlenp
);
2712 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 16);
2714 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 20);
2716 if (name0
== TARGET_CTL_KERN
&& name1
== TARGET_CTL_KERN_VERSION
)
2718 SI to_write
= oldlen
< (SI
) sizeof (TARGET_UTSNAME
)
2719 ? oldlen
: (SI
) sizeof (TARGET_UTSNAME
);
2721 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldlenp
,
2722 sizeof (TARGET_UTSNAME
));
2724 if (sim_core_write_buffer (sd
, current_cpu
, write_map
,
2725 TARGET_UTSNAME
, oldval
,
2727 != (unsigned int) to_write
)
2728 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2734 sim_io_eprintf (sd
, "Unimplemented _sysctl syscall "
2735 "(0x%lx: [0x%lx, 0x%lx],"
2736 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2737 (unsigned long) name
,
2738 (unsigned long) name0
,
2739 (unsigned long) name1
,
2740 (unsigned long) nlen
,
2741 (unsigned long) oldval
,
2742 (unsigned long) oldlenp
,
2743 (unsigned long) newval
,
2744 (unsigned long) newlen
);
2745 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2750 case TARGET_SYS_exit
:
2752 /* Here for all but the last thread. */
2755 = current_cpu
->thread_data
[threadno
].threadid
+ TARGET_PID
;
2757 = (current_cpu
->thread_data
[threadno
].parent_threadid
2759 int exitsig
= current_cpu
->thread_data
[threadno
].exitsig
;
2761 /* Any children are now all orphans. */
2762 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
2763 if (current_cpu
->thread_data
[i
].parent_threadid
2764 == current_cpu
->thread_data
[threadno
].threadid
)
2765 /* Make getppid(2) return 1 for them, poor little ones. */
2766 current_cpu
->thread_data
[i
].parent_threadid
= -TARGET_PID
+ 1;
2768 /* Free the cpu context data. When the parent has received
2769 the exit status, we'll clear the entry too. */
2770 free (current_cpu
->thread_data
[threadno
].cpu_context
);
2771 current_cpu
->thread_data
[threadno
].cpu_context
= NULL
;
2772 current_cpu
->m1threads
--;
2775 sim_io_eprintf (sd
, "Thread %d exited with status %d\n",
2777 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2781 /* Still, we may want to support non-zero exit values. */
2782 current_cpu
->thread_data
[threadno
].exitval
= arg1
<< 8;
2785 deliver_signal (current_cpu
, exitsig
, ppid
);
2789 case TARGET_SYS_clone
:
2791 int nthreads
= current_cpu
->m1threads
+ 1;
2792 void *thread_cpu_data
;
2793 bfd_byte old_sp_buf
[4];
2795 const bfd_byte zeros
[4] = { 0, 0, 0, 0 };
2798 /* That's right, the syscall clone arguments are reversed
2799 compared to sys_clone notes in clone(2) and compared to
2800 other Linux ports (i.e. it's the same order as in the
2801 clone(2) libcall). */
2805 if (nthreads
== SIM_TARGET_MAX_THREADS
)
2807 retval
= -cb_host_to_target_errno (cb
, EAGAIN
);
2811 /* FIXME: Implement the low byte. */
2812 if ((flags
& ~TARGET_CSIGNAL
) !=
2815 | TARGET_CLONE_FILES
2816 | TARGET_CLONE_SIGHAND
)
2820 "Unimplemented clone syscall (0x%lx, 0x%lx)\n",
2821 (unsigned long) arg1
, (unsigned long) arg2
);
2822 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2826 if (current_cpu
->thread_data
== NULL
)
2827 make_first_thread (current_cpu
);
2829 /* The created thread will get the new SP and a cleared R10.
2830 Since it's created out of a copy of the old thread and we
2831 don't have a set-register-function that just take the
2832 cpu_data as a parameter, we set the childs values first,
2833 and write back or overwrite them in the parent after the
2835 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
,
2836 H_GR_SP
, old_sp_buf
, 4);
2837 bfd_putl32 (newsp
, sp_buf
);
2838 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2839 H_GR_SP
, sp_buf
, 4);
2840 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2841 H_GR_R10
, (bfd_byte
*) zeros
, 4);
2844 ->make_thread_cpu_data
) (current_cpu
,
2845 ¤t_cpu
->cpu_data_placeholder
);
2846 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2847 H_GR_SP
, old_sp_buf
, 4);
2849 retval
= ++current_cpu
->max_threadid
+ TARGET_PID
;
2851 /* Find an unused slot. After a few threads have been created
2852 and exited, the array is expected to be a bit fragmented.
2853 We don't reuse the first entry, though, that of the
2855 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
2856 if (current_cpu
->thread_data
[i
].cpu_context
== NULL
2857 /* Don't reuse a zombied entry. */
2858 && current_cpu
->thread_data
[i
].threadid
== 0)
2861 memcpy (¤t_cpu
->thread_data
[i
],
2862 ¤t_cpu
->thread_data
[threadno
],
2863 sizeof (current_cpu
->thread_data
[i
]));
2864 current_cpu
->thread_data
[i
].cpu_context
= thread_cpu_data
;
2865 current_cpu
->thread_data
[i
].cpu_context_atsignal
= NULL
;
2866 current_cpu
->thread_data
[i
].threadid
= current_cpu
->max_threadid
;
2867 current_cpu
->thread_data
[i
].parent_threadid
2868 = current_cpu
->thread_data
[threadno
].threadid
;
2869 current_cpu
->thread_data
[i
].pipe_read_fd
= 0;
2870 current_cpu
->thread_data
[i
].pipe_write_fd
= 0;
2871 current_cpu
->thread_data
[i
].at_syscall
= 0;
2872 current_cpu
->thread_data
[i
].sigpending
= 0;
2873 current_cpu
->thread_data
[i
].sigsuspended
= 0;
2874 current_cpu
->thread_data
[i
].exitsig
= flags
& TARGET_CSIGNAL
;
2875 current_cpu
->m1threads
= nthreads
;
2879 /* Better watch these in case they do something necessary. */
2880 case TARGET_SYS_socketcall
:
2881 retval
= -cb_host_to_target_errno (cb
, ENOSYS
);
2884 unimplemented_syscall
:
2886 sim_io_eprintf (sd
, "Unimplemented syscall: %d "
2887 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n", callnum
,
2888 arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
2889 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2894 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
2895 if (callnum
== TARGET_SYS_open
)
2897 current_cpu
->last_open_fd
= retval
;
2898 current_cpu
->last_open_flags
= arg2
;
2900 current_cpu
->last_syscall
= callnum
;
2902 /* A system call is a rescheduling point. For the time being, we don't
2903 reschedule anywhere else. */
2904 if (current_cpu
->m1threads
!= 0
2905 /* We need to schedule off from an exiting thread that is the
2907 || (current_cpu
->thread_data
!= NULL
2908 && current_cpu
->thread_data
[threadno
].cpu_context
== NULL
))
2910 bfd_byte retval_buf
[4];
2912 current_cpu
->thread_data
[threadno
].last_execution
2913 = TARGET_TIME_MS (current_cpu
);
2914 bfd_putl32 (retval
, retval_buf
);
2915 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R10
, retval_buf
, 4);
2917 current_cpu
->thread_data
[threadno
].at_syscall
= 1;
2918 reschedule (current_cpu
);
2920 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_R10
, retval_buf
, 4);
2921 retval
= bfd_getl32 (retval_buf
);
2927 /* Callback from simulator write saying that the pipe at (reader, writer)
2928 is now non-empty (so the writer should wait until the pipe is empty, at
2929 least not write to this or any other pipe). Simplest is to just wait
2930 until the pipe is empty. */
2933 cris_pipe_nonempty (host_callback
*cb ATTRIBUTE_UNUSED
,
2934 int reader
, int writer
)
2936 SIM_CPU
*cpu
= current_cpu_for_cb_callback
;
2937 const bfd_byte zeros
[4] = { 0, 0, 0, 0 };
2939 /* It's the current thread: we just have to re-run the current
2940 syscall instruction (presumably "break 13") and change the syscall
2941 to the special simulator-wait code. Oh, and set a marker that
2942 we're waiting, so we can disambiguate the special call from a
2945 This function may be called multiple times between cris_pipe_empty,
2946 but we must avoid e.g. decreasing PC every time. Check fd markers
2948 if (cpu
->thread_data
== NULL
)
2950 sim_io_eprintf (CPU_STATE (cpu
),
2951 "Terminating simulation due to writing pipe rd:wr %d:%d"
2952 " from one single thread\n", reader
, writer
);
2953 sim_engine_halt (CPU_STATE (cpu
), cpu
,
2954 NULL
, sim_pc_get (cpu
), sim_stopped
, SIM_SIGILL
);
2956 else if (cpu
->thread_data
[cpu
->threadno
].pipe_write_fd
== 0)
2958 cpu
->thread_data
[cpu
->threadno
].pipe_write_fd
= writer
;
2959 cpu
->thread_data
[cpu
->threadno
].pipe_read_fd
= reader
;
2960 /* FIXME: We really shouldn't change registers other than R10 in
2961 syscalls (like R9), here or elsewhere. */
2962 (*CPU_REG_STORE (cpu
)) (cpu
, H_GR_R9
, (bfd_byte
*) zeros
, 4);
2963 sim_pc_set (cpu
, sim_pc_get (cpu
) - 2);
2967 /* Callback from simulator close or read call saying that the pipe at
2968 (reader, writer) is now empty (so the writer can write again, perhaps
2969 leave a waiting state). If there are bytes remaining, they couldn't be
2970 consumed (perhaps due to the pipe closing). */
2973 cris_pipe_empty (host_callback
*cb
,
2974 int reader ATTRIBUTE_UNUSED
,
2978 SIM_CPU
*cpu
= current_cpu_for_cb_callback
;
2979 bfd_byte r10_buf
[4];
2980 int remaining
= cb
->pipe_buffer
[writer
].size
;
2982 /* We need to find the thread that waits for this pipe. */
2983 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
2984 if (cpu
->thread_data
[i
].cpu_context
2985 && cpu
->thread_data
[i
].pipe_write_fd
== writer
)
2988 /* Temporarily switch to this cpu context, so we can change the
2989 PC by ordinary calls. */
2991 memcpy (cpu
->thread_data
[cpu
->threadno
].cpu_context
,
2992 &cpu
->cpu_data_placeholder
,
2993 cpu
->thread_cpu_data_size
);
2994 memcpy (&cpu
->cpu_data_placeholder
,
2995 cpu
->thread_data
[i
].cpu_context
,
2996 cpu
->thread_cpu_data_size
);
2998 /* The return value is supposed to contain the number of written
2999 bytes, which is the number of bytes requested and returned at
3000 the write call. We subtract the remaining bytes from that,
3001 but making sure we still get a positive number.
3002 The return value may also be a negative number; an error
3003 value. We cover this case by comparing against remaining,
3004 which is always >= 0. */
3005 (*CPU_REG_FETCH (cpu
)) (cpu
, H_GR_R10
, r10_buf
, 4);
3006 retval
= (int) bfd_getl_signed_32 (r10_buf
);
3007 if (retval
>= remaining
)
3008 bfd_putl32 (retval
- remaining
, r10_buf
);
3009 (*CPU_REG_STORE (cpu
)) (cpu
, H_GR_R10
, r10_buf
, 4);
3011 sim_pc_set (cpu
, sim_pc_get (cpu
) + 2);
3012 memcpy (cpu
->thread_data
[i
].cpu_context
,
3013 &cpu
->cpu_data_placeholder
,
3014 cpu
->thread_cpu_data_size
);
3015 memcpy (&cpu
->cpu_data_placeholder
,
3016 cpu
->thread_data
[cpu
->threadno
].cpu_context
,
3017 cpu
->thread_cpu_data_size
);
3018 cpu
->thread_data
[i
].pipe_read_fd
= 0;
3019 cpu
->thread_data
[i
].pipe_write_fd
= 0;
3026 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3029 cris_time (host_callback
*cb ATTRIBUTE_UNUSED
, long *t
)
3031 long retval
= TARGET_TIME (current_cpu_for_cb_callback
);
3037 /* Set target-specific callback data. */
3040 cris_set_callbacks (host_callback
*cb
)
3042 /* Yeargh, have to cast away constness to avoid warnings. */
3043 cb
->syscall_map
= (CB_TARGET_DEFS_MAP
*) syscall_map
;
3044 cb
->errno_map
= (CB_TARGET_DEFS_MAP
*) errno_map
;
3046 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3047 parameter to cb_store_target_endian will make st_size negative.
3048 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3049 *unsigned*, and/or add syntax for signed-ness. */
3050 cb
->stat_map
= stat_map
;
3051 cb
->open_map
= (CB_TARGET_DEFS_MAP
*) open_map
;
3052 cb
->pipe_nonempty
= cris_pipe_nonempty
;
3053 cb
->pipe_empty
= cris_pipe_empty
;
3054 cb
->time
= cris_time
;
3057 /* Process an address exception. */
3060 cris_core_signal (SIM_DESC sd
, SIM_CPU
*current_cpu
, sim_cia cia
,
3061 unsigned int map
, int nr_bytes
, address_word addr
,
3062 transfer_type transfer
, sim_core_signals sig
)
3064 sim_core_signal (sd
, current_cpu
, cia
, map
, nr_bytes
, addr
,