1 /* CRIS exception, interrupt, and trap (EIT) support
2 Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Axis Communications.
5 This file is part of the GNU simulators.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "sim-options.h"
23 /* FIXME: get rid of targ-vals.h usage everywhere else. */
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
38 #ifdef HAVE_SYS_STAT_H
41 /* For PATH_MAX, originally. */
46 /* From ld/sysdep.h. */
48 # define SIM_PATHMAX PATH_MAX
51 # define SIM_PATHMAX MAXPATHLEN
53 # define SIM_PATHMAX 1024
57 /* The verbatim values are from asm-cris/unistd.h. */
59 #define TARGET_SYS_exit 1
60 #define TARGET_SYS_read 3
61 #define TARGET_SYS_write 4
62 #define TARGET_SYS_open 5
63 #define TARGET_SYS_close 6
64 #define TARGET_SYS_unlink 10
65 #define TARGET_SYS_time 13
66 #define TARGET_SYS_lseek 19
67 #define TARGET_SYS_getpid 20
68 #define TARGET_SYS_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 /* From linux/limits.h. */
244 #define TARGET_PIPE_BUF 4096
246 static const char stat_map
[] =
247 "st_dev,2:space,10:space,4:st_mode,4:st_nlink,4:st_uid,4"
248 ":st_gid,4:st_rdev,2:space,10:st_size,8:st_blksize,4:st_blocks,4"
249 ":space,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,4"
252 static const CB_TARGET_DEFS_MAP syscall_map
[] =
254 { CB_SYS_open
, TARGET_SYS_open
},
255 { CB_SYS_close
, TARGET_SYS_close
},
256 { CB_SYS_read
, TARGET_SYS_read
},
257 { CB_SYS_write
, TARGET_SYS_write
},
258 { CB_SYS_lseek
, TARGET_SYS_lseek
},
259 { CB_SYS_unlink
, TARGET_SYS_unlink
},
260 { CB_SYS_getpid
, TARGET_SYS_getpid
},
261 { CB_SYS_fstat
, TARGET_SYS_fstat64
},
262 { CB_SYS_lstat
, TARGET_SYS_lstat64
},
263 { CB_SYS_stat
, TARGET_SYS_stat64
},
264 { CB_SYS_pipe
, TARGET_SYS_pipe
},
265 { CB_SYS_rename
, TARGET_SYS_rename
},
266 { CB_SYS_truncate
, TARGET_SYS_truncate
},
267 { CB_SYS_ftruncate
, TARGET_SYS_ftruncate
},
271 /* An older, 32-bit-only stat mapping. */
272 static const char stat32_map
[] =
273 "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2"
274 ":st_gid,2:st_rdev,2:space,2:st_size,4:st_blksize,4:st_blocks,4"
275 ":st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:space,12";
277 /* Map for calls using the 32-bit struct stat. Primarily used by the
278 newlib Linux mapping. */
279 static const CB_TARGET_DEFS_MAP syscall_stat32_map
[] =
281 { CB_SYS_fstat
, TARGET_SYS_fstat
},
282 { CB_SYS_stat
, TARGET_SYS_stat
},
286 /* Giving the true value for the running sim process will lead to
287 non-time-invariant behavior. */
288 #define TARGET_PID 42
290 /* Unfortunately, we don't get this from cris.cpu at the moment, and if
291 we did, we'd still don't get a register number with the "16" offset. */
292 #define TARGET_SRP_REGNUM (16+11)
294 /* Extracted by applying
295 awk '/^#define/ { printf "#ifdef %s\n { %s, %s },\n#endif\n", $2, $2, $3;}'
296 on .../include/asm/errno.h in a GNU/Linux/CRIS installation and
297 adjusting the synonyms. */
299 static const CB_TARGET_DEFS_MAP errno_map
[] =
407 { ENAMETOOLONG
, 36 },
566 { EDESTADDRREQ
, 89 },
577 #ifdef EPROTONOSUPPORT
578 { EPROTONOSUPPORT
, 93 },
580 #ifdef ESOCKTNOSUPPORT
581 { ESOCKTNOSUPPORT
, 94 },
587 { EPFNOSUPPORT
, 96 },
590 { EAFNOSUPPORT
, 97 },
596 { EADDRNOTAVAIL
, 99 },
602 { ENETUNREACH
, 101 },
608 { ECONNABORTED
, 103 },
626 { ETOOMANYREFS
, 109 },
632 { ECONNREFUSED
, 111 },
638 { EHOSTUNREACH
, 113 },
644 { EINPROGRESS
, 115 },
671 { EMEDIUMTYPE
, 124 },
676 /* Extracted by applying
677 perl -ne 'if ($_ =~ /^#define/) { split;
678 printf "#ifdef $_[1]\n { %s, 0x%x },\n#endif\n",
679 $_[1], $_[2] =~ /^0/ ? oct($_[2]) : $_[2];}'
680 on pertinent parts of .../include/asm/fcntl.h in a GNU/Linux/CRIS
681 installation and removing synonyms and unnecessary items. Don't
682 forget the end-marker. */
684 /* These we treat specially, as they're used in the fcntl F_GETFL
685 syscall. For consistency, open_map is also manually edited to use
687 #define TARGET_O_ACCMODE 0x3
688 #define TARGET_O_RDONLY 0x0
689 #define TARGET_O_WRONLY 0x1
691 static const CB_TARGET_DEFS_MAP open_map
[] = {
693 { O_ACCMODE
, TARGET_O_ACCMODE
},
696 { O_RDONLY
, TARGET_O_RDONLY
},
699 { O_WRONLY
, TARGET_O_WRONLY
},
720 { O_NONBLOCK
, 0x800 },
732 { O_DIRECT
, 0x4000 },
735 { O_LARGEFILE
, 0x8000 },
738 { O_DIRECTORY
, 0x10000 },
741 { O_NOFOLLOW
, 0x20000 },
746 /* Needed for the cris_pipe_nonempty and cris_pipe_empty syscalls. */
747 static SIM_CPU
*current_cpu_for_cb_callback
;
749 static int syscall_read_mem (host_callback
*, struct cb_syscall
*,
750 unsigned long, char *, int);
751 static int syscall_write_mem (host_callback
*, struct cb_syscall
*,
752 unsigned long, const char *, int);
753 static USI
create_map (SIM_DESC
, struct cris_sim_mmapped_page
**,
755 static USI
unmap_pages (SIM_DESC
, struct cris_sim_mmapped_page
**,
757 static USI
is_mapped (SIM_DESC
, struct cris_sim_mmapped_page
**,
759 static void dump_statistics (SIM_CPU
*current_cpu
);
760 static void make_first_thread (SIM_CPU
*current_cpu
);
762 /* Read/write functions for system call interface. */
765 syscall_read_mem (host_callback
*cb ATTRIBUTE_UNUSED
,
766 struct cb_syscall
*sc
,
767 unsigned long taddr
, char *buf
, int bytes
)
769 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
770 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
772 return sim_core_read_buffer (sd
, cpu
, read_map
, buf
, taddr
, bytes
);
776 syscall_write_mem (host_callback
*cb ATTRIBUTE_UNUSED
,
777 struct cb_syscall
*sc
,
778 unsigned long taddr
, const char *buf
, int bytes
)
780 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
781 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
783 return sim_core_write_buffer (sd
, cpu
, write_map
, buf
, taddr
, bytes
);
786 /* When we risk running self-modified code (as in trampolines), this is
787 called from special-case insns. The silicon CRIS CPU:s have enough
788 cache snooping implemented making this a simulator-only issue. Tests:
789 gcc.c-torture/execute/931002-1.c execution, -O3 -g
790 gcc.c-torture/execute/931002-1.c execution, -O3 -fomit-frame-pointer. */
793 cris_flush_simulator_decode_cache (SIM_CPU
*current_cpu
,
794 USI pc ATTRIBUTE_UNUSED
)
796 SIM_DESC sd
= CPU_STATE (current_cpu
);
799 if (USING_SCACHE_P (sd
))
800 scache_flush_cpu (current_cpu
);
804 /* Output statistics at the end of a run. */
806 dump_statistics (SIM_CPU
*current_cpu
)
808 SIM_DESC sd
= CPU_STATE (current_cpu
);
809 CRIS_MISC_PROFILE
*profp
810 = CPU_CRIS_MISC_PROFILE (current_cpu
);
811 unsigned64 total
= profp
->basic_cycle_count
;
812 const char *textmsg
= "Basic clock cycles, total @: %llu\n";
814 /* The --cris-stats={basic|unaligned|schedulable|all} counts affect
815 what's included in the "total" count only. */
816 switch (CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
817 & FLAG_CRIS_MISC_PROFILE_ALL
)
819 case FLAG_CRIS_MISC_PROFILE_SIMPLE
:
822 case (FLAG_CRIS_MISC_PROFILE_UNALIGNED
| FLAG_CRIS_MISC_PROFILE_SIMPLE
):
824 = "Clock cycles including stall cycles for unaligned accesses @: %llu\n";
825 total
+= profp
->unaligned_mem_dword_count
;
828 case (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
| FLAG_CRIS_MISC_PROFILE_SIMPLE
):
829 textmsg
= "Schedulable clock cycles, total @: %llu\n";
831 += (profp
->memsrc_stall_count
832 + profp
->memraw_stall_count
833 + profp
->movemsrc_stall_count
834 + profp
->movemdst_stall_count
835 + profp
->mulsrc_stall_count
836 + profp
->jumpsrc_stall_count
837 + profp
->unaligned_mem_dword_count
);
840 case FLAG_CRIS_MISC_PROFILE_ALL
:
841 textmsg
= "All accounted clock cycles, total @: %llu\n";
843 += (profp
->memsrc_stall_count
844 + profp
->memraw_stall_count
845 + profp
->movemsrc_stall_count
846 + profp
->movemdst_stall_count
847 + profp
->movemaddr_stall_count
848 + profp
->mulsrc_stall_count
849 + profp
->jumpsrc_stall_count
850 + profp
->branch_stall_count
851 + profp
->jumptarget_stall_count
852 + profp
->unaligned_mem_dword_count
);
859 "Internal inconsistency at %s:%d",
861 sim_engine_halt (sd
, current_cpu
, NULL
, 0,
862 sim_stopped
, SIM_SIGILL
);
865 /* Historically, these messages have gone to stderr, so we'll keep it
866 that way. It's also easier to then tell it from normal program
867 output. FIXME: Add redirect option like "run -e file". */
868 sim_io_eprintf (sd
, textmsg
, total
);
870 /* For v32, unaligned_mem_dword_count should always be 0. For
871 v10, memsrc_stall_count should always be 0. */
872 sim_io_eprintf (sd
, "Memory source stall cycles: %llu\n",
873 (unsigned long long) (profp
->memsrc_stall_count
874 + profp
->unaligned_mem_dword_count
));
875 sim_io_eprintf (sd
, "Memory read-after-write stall cycles: %llu\n",
876 (unsigned long long) profp
->memraw_stall_count
);
877 sim_io_eprintf (sd
, "Movem source stall cycles: %llu\n",
878 (unsigned long long) profp
->movemsrc_stall_count
);
879 sim_io_eprintf (sd
, "Movem destination stall cycles: %llu\n",
880 (unsigned long long) profp
->movemdst_stall_count
);
881 sim_io_eprintf (sd
, "Movem address stall cycles: %llu\n",
882 (unsigned long long) profp
->movemaddr_stall_count
);
883 sim_io_eprintf (sd
, "Multiplication source stall cycles: %llu\n",
884 (unsigned long long) profp
->mulsrc_stall_count
);
885 sim_io_eprintf (sd
, "Jump source stall cycles: %llu\n",
886 (unsigned long long) profp
->jumpsrc_stall_count
);
887 sim_io_eprintf (sd
, "Branch misprediction stall cycles: %llu\n",
888 (unsigned long long) profp
->branch_stall_count
);
889 sim_io_eprintf (sd
, "Jump target stall cycles: %llu\n",
890 (unsigned long long) profp
->jumptarget_stall_count
);
893 /* Check whether any part of [addr .. addr + len - 1] is already mapped.
894 Return 1 if a overlap detected, 0 otherwise. */
897 is_mapped (SIM_DESC sd ATTRIBUTE_UNUSED
,
898 struct cris_sim_mmapped_page
**rootp
,
901 struct cris_sim_mmapped_page
*mapp
;
903 if (len
== 0 || (len
& 8191))
906 /* Iterate over the reverse-address sorted pages until we find a page in
907 or lower than the checked area. */
908 for (mapp
= *rootp
; mapp
!= NULL
&& mapp
->addr
>= addr
; mapp
= mapp
->prev
)
909 if (mapp
->addr
< addr
+ len
&& mapp
->addr
>= addr
)
915 /* Create mmapped memory. */
918 create_map (SIM_DESC sd
, struct cris_sim_mmapped_page
**rootp
, USI addr
,
921 struct cris_sim_mmapped_page
*mapp
;
922 struct cris_sim_mmapped_page
**higher_prevp
= rootp
;
923 USI new_addr
= 0x40000000;
928 new_addr
= rootp
[0]->addr
+ 8192;
935 /* Which is better: return an error for this, or just round it up? */
938 /* Do a recursive call for each page in the request. */
939 for (page_addr
= new_addr
; len
!= 0; page_addr
+= 8192, len
-= 8192)
940 if (create_map (sd
, rootp
, page_addr
, 8192) >= (USI
) -8191)
947 mapp
!= NULL
&& mapp
->addr
> new_addr
;
949 higher_prevp
= &mapp
->prev
;
951 /* Allocate the new page, on the next higher page from the last one
952 allocated, and link in the new descriptor before previous ones. */
953 mapp
= malloc (sizeof (*mapp
));
956 return (USI
) -ENOMEM
;
958 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
962 mapp
->addr
= new_addr
;
963 mapp
->prev
= *higher_prevp
;
964 *higher_prevp
= mapp
;
969 /* Unmap one or more pages. */
972 unmap_pages (SIM_DESC sd
, struct cris_sim_mmapped_page
**rootp
, USI addr
,
975 struct cris_sim_mmapped_page
*mapp
;
976 struct cris_sim_mmapped_page
**higher_prevp
= rootp
;
983 /* Which is better: return an error for this, or just round it up? */
986 /* Loop backwards to make each call is O(1) over the number of pages
987 allocated, if we're unmapping from the high end of the pages. */
988 for (page_addr
= addr
+ len
- 8192;
991 if (unmap_pages (sd
, rootp
, page_addr
, 8192) != 0)
997 for (mapp
= *rootp
; mapp
!= NULL
&& mapp
->addr
> addr
; mapp
= mapp
->prev
)
998 higher_prevp
= &mapp
->prev
;
1000 if (mapp
== NULL
|| mapp
->addr
!= addr
)
1003 *higher_prevp
= mapp
->prev
;
1004 sim_core_detach (sd
, NULL
, 0, 0, addr
);
1009 /* The semantic code invokes this for illegal (unrecognized) instructions. */
1012 sim_engine_invalid_insn (SIM_CPU
*current_cpu
, IADDR cia
, SEM_PC vpc
)
1014 SIM_DESC sd
= CPU_STATE (current_cpu
);
1016 sim_engine_halt (sd
, current_cpu
, NULL
, cia
, sim_stopped
, SIM_SIGILL
);
1020 /* Handlers from the CGEN description that should not be called. */
1023 cris_bmod_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1024 UINT srcreg ATTRIBUTE_UNUSED
,
1025 USI dstreg ATTRIBUTE_UNUSED
)
1031 h_supr_set_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1032 UINT index ATTRIBUTE_UNUSED
,
1033 USI page ATTRIBUTE_UNUSED
,
1034 USI newval ATTRIBUTE_UNUSED
)
1040 h_supr_get_handler (SIM_CPU
*current_cpu ATTRIBUTE_UNUSED
,
1041 UINT index ATTRIBUTE_UNUSED
,
1042 USI page ATTRIBUTE_UNUSED
)
1047 /* Swap one context for another. */
1050 schedule (SIM_CPU
*current_cpu
, int next
)
1052 /* Need to mark context-switches in the trace output. */
1053 if ((CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
1054 & FLAG_CRIS_MISC_PROFILE_XSIM_TRACE
))
1055 cris_trace_printf (CPU_STATE (current_cpu
), current_cpu
,
1058 /* Copy the current context (if there is one) to its slot. */
1059 if (current_cpu
->thread_data
[current_cpu
->threadno
].cpu_context
)
1060 memcpy (current_cpu
->thread_data
[current_cpu
->threadno
].cpu_context
,
1061 ¤t_cpu
->cpu_data_placeholder
,
1062 current_cpu
->thread_cpu_data_size
);
1064 /* Copy the new context from its slot. */
1065 memcpy (¤t_cpu
->cpu_data_placeholder
,
1066 current_cpu
->thread_data
[next
].cpu_context
,
1067 current_cpu
->thread_cpu_data_size
);
1069 /* Update needed stuff to indicate the new context. */
1070 current_cpu
->threadno
= next
;
1072 /* Handle pending signals. */
1073 if (current_cpu
->thread_data
[next
].sigpending
1074 /* We don't run nested signal handlers. This means that pause(2)
1075 and sigsuspend(2) do not work in sighandlers, but that
1076 shouldn't be too hard a restriction. It also greatly
1077 simplifies the code. */
1078 && current_cpu
->thread_data
[next
].cpu_context_atsignal
== NULL
)
1082 /* See if there's really a pending, non-blocked handler. We don't
1083 queue signals, so just use the first one in ascending order. */
1084 for (sig
= 0; sig
< 64; sig
++)
1085 if (current_cpu
->thread_data
[next
].sigdata
[sig
].pending
1086 && !current_cpu
->thread_data
[next
].sigdata
[sig
].blocked
)
1092 USI pc
= sim_pc_get (current_cpu
);
1094 /* It's simpler to save the CPU context inside the simulator
1095 than on the stack. */
1096 current_cpu
->thread_data
[next
].cpu_context_atsignal
1098 ->make_thread_cpu_data
) (current_cpu
,
1099 current_cpu
->thread_data
[next
]
1102 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_SP
, regbuf
, 4);
1103 sp
= bfd_getl32 (regbuf
);
1105 /* Make sure we have an aligned stack. */
1108 /* Make room for the signal frame, aligned. FIXME: Check that
1109 the memory exists, map it in if absent. (BTW, should also
1110 implement on-access automatic stack allocation). */
1113 /* This isn't the same signal frame as the kernel uses, because
1114 we don't want to bother getting all registers on and off the
1117 /* First, we store the currently blocked signals. */
1119 for (i
= 0; i
< 32; i
++)
1121 |= current_cpu
->thread_data
[next
].sigdata
[i
+ 1].blocked
<< i
;
1122 sim_core_write_aligned_4 (current_cpu
, pc
, 0, sp
, blocked
);
1124 for (i
= 0; i
< 31; i
++)
1126 |= current_cpu
->thread_data
[next
].sigdata
[i
+ 33].blocked
<< i
;
1127 sim_core_write_aligned_4 (current_cpu
, pc
, 0, sp
+ 4, blocked
);
1129 /* Then, the actual instructions. This is CPU-specific, but we
1130 use instructions from the common subset for v10 and v32 which
1131 should be safe for the time being but could be parametrized
1133 /* MOVU.W [PC+],R9. */
1134 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 8, 0x9c5f);
1135 /* .WORD TARGET_SYS_sigreturn. */
1136 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 10,
1137 TARGET_SYS_sigreturn
);
1139 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 12, 0xe93d);
1141 /* NOP (on v32; it's SETF on v10, but is the correct compatible
1142 instruction. Still, it doesn't matter because v10 has no
1143 delay slot for BREAK so it will not be executed). */
1144 sim_core_write_aligned_2 (current_cpu
, pc
, 0, sp
+ 16, 0x05b0);
1146 /* Modify registers to hold the right values for the sighandler
1147 context: updated stackpointer and return address pointing to
1148 the sigreturn stub. */
1149 bfd_putl32 (sp
, regbuf
);
1150 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_SP
, regbuf
, 4);
1151 bfd_putl32 (sp
+ 8, regbuf
);
1152 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, TARGET_SRP_REGNUM
,
1155 current_cpu
->thread_data
[next
].sigdata
[sig
].pending
= 0;
1157 /* Block this signal (for the duration of the sighandler). */
1158 current_cpu
->thread_data
[next
].sigdata
[sig
].blocked
= 1;
1160 sim_pc_set (current_cpu
, current_cpu
->sighandler
[sig
]);
1161 bfd_putl32 (sig
, regbuf
);
1162 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R10
,
1165 /* We ignore a SA_SIGINFO flag in the sigaction call; the code I
1166 needed all this for, specifies a SA_SIGINFO call but treats it
1167 like an ordinary sighandler; only the signal number argument is
1168 inspected. To make future need to implement SA_SIGINFO
1169 correctly possible, we set the siginfo argument register to a
1170 magic (hopefully non-address) number. (NB: then, you should
1171 just need to pass the siginfo argument; it seems you probably
1172 don't need to implement the specific rt_sigreturn.) */
1173 bfd_putl32 (0xbad5161f, regbuf
);
1174 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R11
,
1177 /* The third argument is unused and the kernel sets it to 0. */
1178 bfd_putl32 (0, regbuf
);
1179 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R12
,
1184 /* No, there actually was no pending signal for this thread. Reset
1186 current_cpu
->thread_data
[next
].sigpending
= 0;
1190 /* Reschedule the simplest possible way until something else is absolutely
1192 - A. Find the next process (round-robin) that doesn't have at_syscall
1194 - B. If there is none, just run the next process, round-robin.
1195 - Clear at_syscall for the current process. */
1198 reschedule (SIM_CPU
*current_cpu
)
1202 /* Iterate over all thread slots, because after a few thread creations
1203 and exits, we don't know where the live ones are. */
1204 for (i
= (current_cpu
->threadno
+ 1) % SIM_TARGET_MAX_THREADS
;
1205 i
!= current_cpu
->threadno
;
1206 i
= (i
+ 1) % SIM_TARGET_MAX_THREADS
)
1207 if (current_cpu
->thread_data
[i
].cpu_context
1208 && current_cpu
->thread_data
[i
].at_syscall
== 0)
1210 schedule (current_cpu
, i
);
1214 /* Pick any next live thread. */
1215 for (i
= (current_cpu
->threadno
+ 1) % SIM_TARGET_MAX_THREADS
;
1216 i
!= current_cpu
->threadno
;
1217 i
= (i
+ 1) % SIM_TARGET_MAX_THREADS
)
1218 if (current_cpu
->thread_data
[i
].cpu_context
)
1220 schedule (current_cpu
, i
);
1224 /* More than one live thread, but we couldn't find the next one? */
1228 /* Set up everything to receive (or IGN) an incoming signal to the
1232 deliver_signal (SIM_CPU
*current_cpu
, int sig
, unsigned int pid
)
1235 USI pc
= sim_pc_get (current_cpu
);
1237 /* Find the thread index of the pid. */
1238 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
1239 /* Apparently it's ok to send signals to zombies (so a check for
1240 current_cpu->thread_data[i].cpu_context != NULL would be
1242 if (current_cpu
->thread_data
[i
].threadid
== pid
- TARGET_PID
)
1245 switch (current_cpu
->sighandler
[sig
])
1247 case TARGET_SIG_DFL
:
1250 /* The following according to the glibc
1251 documentation. (The kernel code has non-obvious
1252 execution paths.) */
1255 case TARGET_SIGSEGV
:
1257 case TARGET_SIGABRT
:
1258 case TARGET_SIGTRAP
:
1261 case TARGET_SIGTERM
:
1263 case TARGET_SIGQUIT
:
1264 case TARGET_SIGKILL
:
1267 case TARGET_SIGALRM
:
1268 case TARGET_SIGVTALRM
:
1269 case TARGET_SIGPROF
:
1270 case TARGET_SIGSTOP
:
1272 case TARGET_SIGPIPE
:
1273 case TARGET_SIGLOST
:
1274 case TARGET_SIGXCPU
:
1275 case TARGET_SIGXFSZ
:
1276 case TARGET_SIGUSR1
:
1277 case TARGET_SIGUSR2
:
1278 sim_io_eprintf (CPU_STATE (current_cpu
),
1279 "Exiting pid %d due to signal %d\n",
1281 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
,
1282 NULL
, pc
, sim_stopped
,
1283 sig
== TARGET_SIGABRT
1284 ? SIM_SIGABRT
: SIM_SIGILL
);
1287 /* The default for all other signals is to be ignored. */
1292 case TARGET_SIG_IGN
:
1295 case TARGET_SIGKILL
:
1296 case TARGET_SIGSTOP
:
1297 /* Can't ignore these signals. */
1298 sim_io_eprintf (CPU_STATE (current_cpu
),
1299 "Exiting pid %d due to signal %d\n",
1301 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
,
1302 NULL
, pc
, sim_stopped
, SIM_SIGILL
);
1311 /* Mark the signal as pending, making schedule () check
1312 closer. The signal will be handled when the thread is
1313 scheduled and the signal is unblocked. */
1314 current_cpu
->thread_data
[i
].sigdata
[sig
].pending
= 1;
1315 current_cpu
->thread_data
[i
].sigpending
= 1;
1320 sim_io_eprintf (CPU_STATE (current_cpu
),
1321 "Unimplemented signal: %d\n", sig
);
1322 sim_engine_halt (CPU_STATE (current_cpu
), current_cpu
, NULL
, pc
,
1323 sim_stopped
, SIM_SIGILL
);
1328 -cb_host_to_target_errno (STATE_CALLBACK (CPU_STATE (current_cpu
)),
1332 /* Make the vector and the first item, the main thread. */
1335 make_first_thread (SIM_CPU
*current_cpu
)
1337 current_cpu
->thread_data
1339 SIM_TARGET_MAX_THREADS
1340 * sizeof (current_cpu
->thread_data
[0]));
1341 current_cpu
->thread_data
[0].cpu_context
1342 = (*current_cpu
->make_thread_cpu_data
) (current_cpu
,
1344 ->cpu_data_placeholder
);
1345 current_cpu
->thread_data
[0].parent_threadid
= -1;
1347 /* For good measure. */
1348 if (TARGET_SIG_DFL
!= 0)
1352 /* Handle unknown system calls. Returns (if it does) the syscall
1356 cris_unknown_syscall (SIM_CPU
*current_cpu
, USI pc
, char *s
, ...)
1358 SIM_DESC sd
= CPU_STATE (current_cpu
);
1359 host_callback
*cb
= STATE_CALLBACK (sd
);
1361 if (cris_unknown_syscall_action
== CRIS_USYSC_MSG_STOP
1362 || cris_unknown_syscall_action
== CRIS_USYSC_MSG_ENOSYS
)
1367 sim_io_evprintf (sd
, s
, ap
);
1370 if (cris_unknown_syscall_action
== CRIS_USYSC_MSG_STOP
)
1371 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
1374 return -cb_host_to_target_errno (cb
, ENOSYS
);
1377 /* Main function: the handler of the "break 13" syscall insn. */
1380 cris_break_13_handler (SIM_CPU
*current_cpu
, USI callnum
, USI arg1
,
1381 USI arg2
, USI arg3
, USI arg4
, USI arg5
, USI arg6
,
1385 SIM_DESC sd
= CPU_STATE (current_cpu
);
1386 host_callback
*cb
= STATE_CALLBACK (sd
);
1388 int threadno
= current_cpu
->threadno
;
1390 current_cpu
->syscalls
++;
1392 CB_SYSCALL_INIT (&s
);
1398 if (callnum
== TARGET_SYS_exit
&& current_cpu
->m1threads
== 0)
1400 if (CPU_CRIS_MISC_PROFILE (current_cpu
)->flags
1401 & FLAG_CRIS_MISC_PROFILE_ALL
)
1402 dump_statistics (current_cpu
);
1403 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_exited
, arg1
);
1407 s
.p2
= (PTR
) current_cpu
;
1408 s
.read_mem
= syscall_read_mem
;
1409 s
.write_mem
= syscall_write_mem
;
1411 current_cpu_for_cb_callback
= current_cpu
;
1413 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1416 sim_io_eprintf (sd
, "Break 13: invalid %d? Returned %ld\n", callnum
,
1418 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
1421 retval
= s
.result
== -1 ? -s
.errcode
: s
.result
;
1423 if (s
.errcode
!= 0 && s
.errcode
== cb_host_to_target_errno (cb
, ENOSYS
))
1425 /* If the generic simulator call said ENOSYS, then let's try the
1426 ones we know ourselves.
1428 The convention is to provide *very limited* functionality on an
1429 as-needed basis, only what's covered by the test-suite, tests
1430 added when functionality changes and abort with a descriptive
1431 message for *everything* else. Where there's no test-case, we
1436 /* It's a pretty safe bet that the "old setup() system call"
1437 number will not be re-used; we can't say the same for higher
1438 numbers. We treat this simulator-generated call as "wait
1439 forever"; we re-run this insn. The wait is ended by a
1440 callback. Sanity check that this is the reason we got
1442 if (current_cpu
->thread_data
== NULL
1443 || (current_cpu
->thread_data
[threadno
].pipe_write_fd
== 0))
1444 goto unimplemented_syscall
;
1446 sim_pc_set (current_cpu
, pc
);
1450 case TARGET_SYS_fcntl64
:
1451 case TARGET_SYS_fcntl
:
1456 Glibc checks stdin, stdout and stderr fd:s for
1457 close-on-exec security sanity. We just need to provide a
1458 OK return value. If we really need to have a
1459 close-on-exec flag true, we could just do a real fcntl
1465 /* F_SETFD. Just ignore attempts to set the close-on-exec
1471 /* F_GETFL. Check for the special case for open+fdopen. */
1472 if (current_cpu
->last_syscall
== TARGET_SYS_open
1473 && arg1
== current_cpu
->last_open_fd
)
1475 retval
= current_cpu
->last_open_flags
& TARGET_O_ACCMODE
;
1480 /* Because we can't freopen fd:s 0, 1, 2 to mean
1481 something else than stdin, stdout and stderr
1482 (sim/common/syscall.c:cb_syscall special cases fd
1483 0, 1 and 2), we know what flags that we can
1484 sanely return for these fd:s. */
1485 retval
= TARGET_O_RDONLY
;
1488 else if (arg1
== 1 || arg1
== 2)
1490 retval
= TARGET_O_WRONLY
;
1495 /* Nothing else is implemented. */
1497 = cris_unknown_syscall (current_cpu
, pc
,
1498 "Unimplemented %s syscall "
1499 "(fd: 0x%lx: cmd: 0x%lx arg: "
1501 callnum
== TARGET_SYS_fcntl
1502 ? "fcntl" : "fcntl64",
1503 (unsigned long) (USI
) arg1
,
1504 (unsigned long) (USI
) arg2
,
1505 (unsigned long) (USI
) arg3
);
1510 case TARGET_SYS_uname
:
1512 /* Fill in a few constants to appease glibc. */
1513 static const char sim_utsname
[6][65] =
1523 if ((s
.write_mem
) (cb
, &s
, arg1
, (const char *) sim_utsname
,
1524 sizeof (sim_utsname
))
1525 != sizeof (sim_utsname
))
1526 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
1532 case TARGET_SYS_geteuid32
:
1533 /* We tell the truth with these. Maybe we shouldn't, but it
1534 should match the "stat" information. */
1535 retval
= geteuid ();
1538 case TARGET_SYS_getuid32
:
1542 case TARGET_SYS_getegid32
:
1543 retval
= getegid ();
1546 case TARGET_SYS_getgid32
:
1550 case TARGET_SYS_brk
:
1551 /* Most often, we just return the argument, like the Linux
1556 retval
= current_cpu
->endbrk
;
1557 else if (arg1
<= current_cpu
->endmem
)
1558 current_cpu
->endbrk
= arg1
;
1561 USI new_end
= (arg1
+ 8191) & ~8191;
1563 /* If the simulator wants to brk more than a certain very
1564 large amount, something is wrong. FIXME: Return an error
1565 or abort? Have command-line selectable? */
1566 if (new_end
- current_cpu
->endmem
> SIM_MAX_ALLOC_CHUNK
)
1568 current_cpu
->endbrk
= current_cpu
->endmem
;
1569 retval
= current_cpu
->endmem
;
1573 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
1574 current_cpu
->endmem
,
1575 new_end
- current_cpu
->endmem
,
1577 current_cpu
->endbrk
= arg1
;
1578 current_cpu
->endmem
= new_end
;
1582 case TARGET_SYS_getpid
:
1583 /* Correct until CLONE_THREAD is implemented. */
1584 retval
= current_cpu
->thread_data
== NULL
1586 : TARGET_PID
+ current_cpu
->thread_data
[threadno
].threadid
;
1589 case TARGET_SYS_getppid
:
1590 /* Correct until CLONE_THREAD is implemented. */
1591 retval
= current_cpu
->thread_data
== NULL
1594 + current_cpu
->thread_data
[threadno
].parent_threadid
);
1597 case TARGET_SYS_mmap2
:
1606 /* If the simulator wants to mmap more than the very large
1607 limit, something is wrong. FIXME: Return an error or
1608 abort? Have command-line selectable? */
1609 if (len
> SIM_MAX_ALLOC_CHUNK
)
1611 retval
= -cb_host_to_target_errno (cb
, ENOMEM
);
1615 if ((prot
!= (TARGET_PROT_READ
| TARGET_PROT_WRITE
)
1617 != (TARGET_PROT_READ
1619 | TARGET_PROT_EXEC
))
1620 && prot
!= TARGET_PROT_READ
)
1621 || (flags
!= (TARGET_MAP_ANONYMOUS
| TARGET_MAP_PRIVATE
)
1622 && flags
!= TARGET_MAP_PRIVATE
1623 && flags
!= TARGET_MAP_SHARED
)
1624 || (fd
!= (USI
) -1 && prot
!= TARGET_PROT_READ
)
1628 = cris_unknown_syscall (current_cpu
, pc
,
1629 "Unimplemented mmap2 call "
1630 "(0x%lx, 0x%lx, 0x%lx, "
1631 "0x%lx, 0x%lx, 0x%lx)\n",
1632 (unsigned long) arg1
,
1633 (unsigned long) arg2
,
1634 (unsigned long) arg3
,
1635 (unsigned long) arg4
,
1636 (unsigned long) arg5
,
1637 (unsigned long) arg6
);
1640 else if (fd
!= (USI
) -1)
1647 /* A non-aligned argument is allowed for files. */
1648 USI newlen
= (len
+ 8191) & ~8191;
1650 /* We only support read, which we should already have
1651 checked. Check again anyway. */
1652 if (prot
!= TARGET_PROT_READ
)
1656 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1659 if (newaddr
>= (USI
) -8191)
1662 retval
= -cb_host_to_target_errno (cb
, -(SI
) newaddr
);
1666 /* Find the current position in the file. */
1667 s
.func
= TARGET_SYS_lseek
;
1671 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1678 /* Use the standard read callback to read in "len"
1680 s
.func
= TARGET_SYS_read
;
1684 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1687 if ((USI
) s
.result
!= len
)
1690 /* After reading, we need to go back to the previous
1691 position in the file. */
1692 s
.func
= TARGET_SYS_lseek
;
1696 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
1698 if (pos
!= (USI
) s
.result
)
1706 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1707 (len
+ 8191) & ~8191);
1709 if (newaddr
>= (USI
) -8191)
1710 retval
= -cb_host_to_target_errno (cb
, -(SI
) newaddr
);
1717 case TARGET_SYS_mprotect
:
1719 /* We only cover the case of linuxthreads mprotecting out its
1720 stack guard page. */
1725 if ((addr
& 8191) != 0
1727 || prot
!= TARGET_PROT_NONE
1728 || !is_mapped (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1732 = cris_unknown_syscall (current_cpu
, pc
,
1733 "Unimplemented mprotect call "
1734 "(0x%lx, 0x%lx, 0x%lx)\n",
1735 (unsigned long) arg1
,
1736 (unsigned long) arg2
,
1737 (unsigned long) arg3
);
1741 /* FIXME: We should account for pages like this that are
1742 "mprotected out". For now, we just tell the simulator
1743 core to remove that page from its map. */
1744 sim_core_detach (sd
, NULL
, 0, 0, addr
);
1749 case TARGET_SYS_ioctl
:
1751 /* We support only a very limited functionality: checking
1752 stdout with TCGETS to perform the isatty function. The
1753 TCGETS ioctl isn't actually performed or the result used by
1754 an isatty () caller in a "hello, world" program; only the
1755 return value is then used. Maybe we shouldn't care about
1756 the environment of the simulator regarding isatty, but
1757 that's been working before, in the xsim simulator. */
1758 if (arg2
== TARGET_TCGETS
&& arg1
== 1)
1759 retval
= isatty (1) ? 0 : cb_host_to_target_errno (cb
, EINVAL
);
1761 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
1765 case TARGET_SYS_munmap
:
1770 = unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
, addr
,
1772 retval
= result
!= 0 ? -cb_host_to_target_errno (cb
, result
) : 0;
1776 case TARGET_SYS_wait4
:
1784 /* FIXME: We're not properly implementing __WCLONE, and we
1785 don't really need the special casing so we might as well
1786 make this general. */
1787 if ((!(pid
== (USI
) -1
1788 && options
== (TARGET___WCLONE
| TARGET_WNOHANG
)
1791 && (options
== TARGET___WCLONE
1792 || options
== TARGET___WALL
)))
1794 || current_cpu
->thread_data
== NULL
)
1797 = cris_unknown_syscall (current_cpu
, pc
,
1798 "Unimplemented wait4 call "
1799 "(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
1800 (unsigned long) arg1
,
1801 (unsigned long) arg2
,
1802 (unsigned long) arg3
,
1803 (unsigned long) arg4
);
1807 if (pid
== (USI
) -1)
1808 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
1810 if (current_cpu
->thread_data
[threadno
].threadid
1811 == current_cpu
->thread_data
[i
].parent_threadid
1812 && current_cpu
->thread_data
[i
].threadid
!= 0
1813 && current_cpu
->thread_data
[i
].cpu_context
== NULL
)
1815 /* A zombied child. Get the exit value and clear the
1816 zombied entry so it will be reused. */
1817 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, saddr
,
1819 ->thread_data
[i
].exitval
);
1821 = current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
;
1822 memset (¤t_cpu
->thread_data
[i
], 0,
1823 sizeof (current_cpu
->thread_data
[i
]));
1829 /* We're waiting for a specific PID. If we don't find
1830 it zombied on this run, rerun the syscall. */
1831 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
1832 if (pid
== current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
1833 && current_cpu
->thread_data
[i
].cpu_context
== NULL
)
1836 /* Get the exit value if the caller wants it. */
1837 sim_core_write_unaligned_4 (current_cpu
, pc
, 0,
1844 = current_cpu
->thread_data
[i
].threadid
+ TARGET_PID
;
1845 memset (¤t_cpu
->thread_data
[i
], 0,
1846 sizeof (current_cpu
->thread_data
[i
]));
1851 sim_pc_set (current_cpu
, pc
);
1854 retval
= -cb_host_to_target_errno (cb
, ECHILD
);
1859 case TARGET_SYS_rt_sigaction
:
1867 __sighandler_t sa_handler;
1868 unsigned long sa_flags;
1869 void (*sa_restorer)(void);
1875 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, old_sa
+ 0,
1876 current_cpu
->sighandler
[signum
]);
1877 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 4, 0);
1878 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 8, 0);
1880 /* We'll assume _NSIG_WORDS is 2 for the kernel. */
1881 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 12, 0);
1882 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg3
+ 16, 0);
1886 USI target_sa_handler
1887 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
);
1889 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 4);
1890 USI target_sa_restorer
1891 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 8);
1892 USI target_sa_mask_low
1893 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 12);
1894 USI target_sa_mask_high
1895 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, new_sa
+ 16);
1897 /* We won't interrupt a syscall so we won't restart it,
1898 but a signal(2) call ends up syscalling rt_sigaction
1899 with this flag, so we have to handle it. The
1900 sa_restorer field contains garbage when not
1901 TARGET_SA_RESTORER, so don't look at it. For the
1902 time being, we don't nest sighandlers, so we
1903 ignore the sa_mask, which simplifies things. */
1904 if ((target_sa_flags
!= 0
1905 && target_sa_flags
!= TARGET_SA_RESTART
1906 && target_sa_flags
!= (TARGET_SA_RESTART
|TARGET_SA_SIGINFO
))
1907 || target_sa_handler
== 0)
1910 = cris_unknown_syscall (current_cpu
, pc
,
1911 "Unimplemented rt_sigaction "
1914 "[0x%x, 0x%x, 0x%x, "
1915 "{0x%x, 0x%x}], 0x%lx)\n",
1916 (unsigned long) arg1
,
1917 (unsigned long) arg2
,
1922 target_sa_mask_high
,
1923 (unsigned long) arg3
);
1927 current_cpu
->sighandler
[signum
] = target_sa_handler
;
1929 /* Because we may have unblocked signals, one may now be
1930 pending, if there are threads, that is. */
1931 if (current_cpu
->thread_data
)
1932 current_cpu
->thread_data
[threadno
].sigpending
= 1;
1938 case TARGET_SYS_mremap
:
1944 USI new_addr
= arg5
;
1947 if (new_len
== old_len
)
1948 /* The program and/or library is possibly confused but
1949 this is a valid call. Happens with ipps-1.40 on file
1952 else if (new_len
< old_len
)
1954 /* Shrinking is easy. */
1955 if (unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
,
1956 addr
+ new_len
, old_len
- new_len
) != 0)
1957 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
1961 else if (! is_mapped (sd
, ¤t_cpu
->highest_mmapped_page
,
1962 addr
+ old_len
, new_len
- old_len
))
1964 /* If the extension isn't mapped, we can just add it. */
1966 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
,
1967 addr
+ old_len
, new_len
- old_len
);
1969 if (mapped_addr
> (USI
) -8192)
1970 retval
= -cb_host_to_target_errno (cb
, -(SI
) mapped_addr
);
1974 else if (flags
& TARGET_MREMAP_MAYMOVE
)
1976 /* Create a whole new map and copy the contents
1977 block-by-block there. We ignore the new_addr argument
1980 USI prev_addr
= addr
;
1981 USI prev_len
= old_len
;
1984 = create_map (sd
, ¤t_cpu
->highest_mmapped_page
,
1987 if (mapped_addr
> (USI
) -8192)
1989 retval
= -cb_host_to_target_errno (cb
, -(SI
) new_addr
);
1993 retval
= mapped_addr
;
1996 old_len
-= 8192, mapped_addr
+= 8192, addr
+= 8192)
1998 if (sim_core_read_buffer (sd
, current_cpu
, read_map
, buf
,
2000 || sim_core_write_buffer (sd
, current_cpu
, 0, buf
,
2001 mapped_addr
, 8192) != 8192)
2005 if (unmap_pages (sd
, ¤t_cpu
->highest_mmapped_page
,
2006 prev_addr
, prev_len
) != 0)
2010 retval
= -cb_host_to_target_errno (cb
, -ENOMEM
);
2014 case TARGET_SYS_poll
:
2016 int npollfds
= arg2
;
2032 /* Check that this is the expected poll call from
2033 linuxthreads/manager.c; we don't support anything else.
2034 Remember, fd == 0 isn't supported. */
2036 || ((fd
= sim_core_read_unaligned_4 (current_cpu
, pc
,
2038 || ((events
= sim_core_read_unaligned_2 (current_cpu
, pc
,
2041 || ((cb
->fstat
) (cb
, fd
, &buf
) != 0
2042 || (buf
.st_mode
& S_IFIFO
) == 0)
2043 || current_cpu
->thread_data
== NULL
)
2046 = cris_unknown_syscall (current_cpu
, pc
,
2047 "Unimplemented poll syscall "
2048 "(0x%lx: [0x%x, 0x%x, x], "
2050 (unsigned long) arg1
, fd
, events
,
2051 (unsigned long) arg2
,
2052 (unsigned long) arg3
);
2058 /* Iterate over threads; find a marker that a writer is
2059 sleeping, waiting for a reader. */
2060 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
2061 if (current_cpu
->thread_data
[i
].cpu_context
!= NULL
2062 && current_cpu
->thread_data
[i
].pipe_read_fd
== fd
)
2064 revents
= TARGET_POLLIN
;
2069 /* Timeout decreases with whatever time passed between the
2070 last syscall and this. That's not exactly right for the
2071 first call, but it's close enough that it isn't
2072 worthwhile to complicate matters by making that a special
2075 -= (TARGET_TIME_MS (current_cpu
)
2076 - (current_cpu
->thread_data
[threadno
].last_execution
));
2078 /* Arrange to repeat this syscall until timeout or event,
2079 decreasing timeout at each iteration. */
2080 if (timeout
> 0 && revents
== 0)
2082 bfd_byte timeout_buf
[4];
2084 bfd_putl32 (timeout
, timeout_buf
);
2085 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2086 H_GR_R12
, timeout_buf
, 4);
2087 sim_pc_set (current_cpu
, pc
);
2092 sim_core_write_unaligned_2 (current_cpu
, pc
, 0, ufds
+ 4 + 2,
2097 case TARGET_SYS_time
:
2099 retval
= (int) (*cb
->time
) (cb
, 0L);
2101 /* At time of this writing, CB_SYSCALL_time doesn't do the
2102 part of setting *arg1 to the return value. */
2104 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
, retval
);
2108 case TARGET_SYS_gettimeofday
:
2111 USI ts
= TARGET_TIME (current_cpu
);
2112 USI tms
= TARGET_TIME_MS (current_cpu
);
2114 /* First dword is seconds since TARGET_EPOCH. */
2115 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
, ts
);
2117 /* Second dword is microseconds. */
2118 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 4,
2119 (tms
% 1000) * 1000);
2123 /* Time-zone info is always cleared. */
2124 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
, 0);
2125 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
+ 4, 0);
2130 case TARGET_SYS_llseek
:
2132 /* If it fits, tweak parameters to fit the "generic" 32-bit
2133 lseek and use that. */
2141 if (!((offs_hi
== 0 && offs_lo
>= 0)
2142 || (offs_hi
== -1 && offs_lo
< 0)))
2145 = cris_unknown_syscall (current_cpu
, pc
,
2146 "Unimplemented llseek offset,"
2147 " fd %d: 0x%x:0x%x\n",
2148 fd
, (unsigned) arg2
,
2153 s
.func
= TARGET_SYS_lseek
;
2156 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
2158 sim_io_eprintf (sd
, "Break 13: invalid %d? Returned %ld\n", callnum
,
2160 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
2163 retval
= -s
.errcode
;
2166 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, resultp
,
2168 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, resultp
+ 4,
2169 s
.result
< 0 ? -1 : 0);
2174 /* This one does have a generic callback function, but at the time
2175 of this writing, cb_syscall does not have code for it, and we
2176 need target-specific code for the threads implementation
2178 case TARGET_SYS_kill
:
2185 /* At kill(2), glibc sets signal masks such that the thread
2186 machinery is initialized. Still, there is and was only
2188 if (current_cpu
->max_threadid
== 0)
2190 if (pid
!= TARGET_PID
)
2192 retval
= -cb_host_to_target_errno (cb
, EPERM
);
2196 /* FIXME: Signal infrastructure (target-to-sim mapping). */
2197 if (sig
== TARGET_SIGABRT
)
2198 /* A call "abort ()", i.e. "kill (getpid(), SIGABRT)" is
2199 the end-point for failing GCC test-cases. */
2200 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2204 sim_io_eprintf (sd
, "Unimplemented signal: %d\n", sig
);
2205 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2209 /* This will not be reached. */
2213 retval
= deliver_signal (current_cpu
, sig
, pid
);
2217 case TARGET_SYS_rt_sigprocmask
:
2224 if (how
!= TARGET_SIG_BLOCK
2225 && how
!= TARGET_SIG_SETMASK
2226 && how
!= TARGET_SIG_UNBLOCK
)
2229 = cris_unknown_syscall (current_cpu
, pc
,
2230 "Unimplemented rt_sigprocmask "
2231 "syscall (0x%x, 0x%x, 0x%x)\n",
2239 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2242 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2245 /* The sigmask is kept in the per-thread data, so we may
2246 need to create the first one. */
2247 if (current_cpu
->thread_data
== NULL
)
2248 make_first_thread (current_cpu
);
2250 if (how
== TARGET_SIG_SETMASK
)
2251 for (i
= 0; i
< 64; i
++)
2252 current_cpu
->thread_data
[threadno
].sigdata
[i
].blocked
= 0;
2254 for (i
= 0; i
< 32; i
++)
2255 if ((set_low
& (1 << i
)))
2256 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 1].blocked
2257 = (how
!= TARGET_SIG_UNBLOCK
);
2259 for (i
= 0; i
< 31; i
++)
2260 if ((set_high
& (1 << i
)))
2261 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 33].blocked
2262 = (how
!= TARGET_SIG_UNBLOCK
);
2264 /* The mask changed, so a signal may be unblocked for
2266 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2274 for (i
= 0; i
< 32; i
++)
2275 if (current_cpu
->thread_data
[threadno
]
2276 .sigdata
[i
+ 1].blocked
)
2278 for (i
= 0; i
< 31; i
++)
2279 if (current_cpu
->thread_data
[threadno
]
2280 .sigdata
[i
+ 33].blocked
)
2283 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldsetp
+ 0, set_low
);
2284 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldsetp
+ 4, set_high
);
2291 case TARGET_SYS_sigreturn
:
2295 int was_sigsuspended
;
2297 if (current_cpu
->thread_data
== NULL
2298 /* The CPU context is saved with the simulator data, not
2299 on the stack as in the real world. */
2300 || (current_cpu
->thread_data
[threadno
].cpu_context_atsignal
2304 = cris_unknown_syscall (current_cpu
, pc
,
2305 "Invalid sigreturn syscall: "
2306 "no signal handler active "
2307 "(0x%lx, 0x%lx, 0x%lx, 0x%lx, "
2309 (unsigned long) arg1
,
2310 (unsigned long) arg2
,
2311 (unsigned long) arg3
,
2312 (unsigned long) arg4
,
2313 (unsigned long) arg5
,
2314 (unsigned long) arg6
);
2319 = current_cpu
->thread_data
[threadno
].sigsuspended
;
2321 /* Restore the sigmask, either from the stack copy made when
2322 the sighandler was called, or from the saved state
2323 specifically for sigsuspend(2). */
2324 if (was_sigsuspended
)
2326 current_cpu
->thread_data
[threadno
].sigsuspended
= 0;
2327 for (i
= 0; i
< 64; i
++)
2328 current_cpu
->thread_data
[threadno
].sigdata
[i
].blocked
2329 = current_cpu
->thread_data
[threadno
]
2330 .sigdata
[i
].blocked_suspendsave
;
2338 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
,
2339 H_GR_SP
, regbuf
, 4);
2340 sp
= bfd_getl32 (regbuf
);
2342 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, sp
);
2344 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, sp
+ 4);
2346 for (i
= 0; i
< 32; i
++)
2347 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 1].blocked
2348 = (set_low
& (1 << i
)) != 0;
2349 for (i
= 0; i
< 31; i
++)
2350 current_cpu
->thread_data
[threadno
].sigdata
[i
+ 33].blocked
2351 = (set_high
& (1 << i
)) != 0;
2354 /* The mask changed, so a signal may be unblocked for
2356 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2358 memcpy (¤t_cpu
->cpu_data_placeholder
,
2359 current_cpu
->thread_data
[threadno
].cpu_context_atsignal
,
2360 current_cpu
->thread_cpu_data_size
);
2361 free (current_cpu
->thread_data
[threadno
].cpu_context_atsignal
);
2362 current_cpu
->thread_data
[threadno
].cpu_context_atsignal
= NULL
;
2364 /* The return value must come from the saved R10. */
2365 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_R10
, regbuf
, 4);
2366 retval
= bfd_getl32 (regbuf
);
2368 /* We must also break the "sigsuspension loop". */
2369 if (was_sigsuspended
)
2370 sim_pc_set (current_cpu
, sim_pc_get (current_cpu
) + 2);
2374 case TARGET_SYS_rt_sigsuspend
:
2382 = cris_unknown_syscall (current_cpu
, pc
,
2383 "Unimplemented rt_sigsuspend syscall"
2384 " arguments (0x%lx, 0x%lx)\n",
2385 (unsigned long) arg1
,
2386 (unsigned long) arg2
);
2390 /* Don't change the signal mask if we're already in
2391 sigsuspend state (i.e. this syscall is a rerun). */
2392 else if (!current_cpu
->thread_data
[threadno
].sigsuspended
)
2395 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2398 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2402 /* Save the current sigmask and insert the user-supplied
2404 for (i
= 0; i
< 32; i
++)
2406 current_cpu
->thread_data
[threadno
]
2407 .sigdata
[i
+ 1].blocked_suspendsave
2408 = current_cpu
->thread_data
[threadno
]
2409 .sigdata
[i
+ 1].blocked
;
2411 current_cpu
->thread_data
[threadno
]
2412 .sigdata
[i
+ 1].blocked
= (set_low
& (1 << i
)) != 0;
2414 for (i
= 0; i
< 31; i
++)
2416 current_cpu
->thread_data
[threadno
]
2417 .sigdata
[i
+ 33].blocked_suspendsave
2418 = current_cpu
->thread_data
[threadno
]
2419 .sigdata
[i
+ 33].blocked
;
2420 current_cpu
->thread_data
[threadno
]
2421 .sigdata
[i
+ 33].blocked
= (set_high
& (1 << i
)) != 0;
2424 current_cpu
->thread_data
[threadno
].sigsuspended
= 1;
2426 /* The mask changed, so a signal may be unblocked for
2428 current_cpu
->thread_data
[threadno
].sigpending
= 1;
2431 /* Because we don't use arg1 (newsetp) when this syscall is
2432 rerun, it doesn't matter that we overwrite it with the
2433 (constant) return value. */
2434 retval
= -cb_host_to_target_errno (cb
, EINTR
);
2435 sim_pc_set (current_cpu
, pc
);
2439 /* Add case labels here for other syscalls using the 32-bit
2440 "struct stat", provided they have a corresponding simulator
2441 function of course. */
2442 case TARGET_SYS_stat
:
2443 case TARGET_SYS_fstat
:
2445 /* As long as the infrastructure doesn't cache anything
2446 related to the stat mapping, this trick gets us a dual
2447 "struct stat"-type mapping in the least error-prone way. */
2448 const char *saved_map
= cb
->stat_map
;
2449 CB_TARGET_DEFS_MAP
*saved_syscall_map
= cb
->syscall_map
;
2451 cb
->syscall_map
= (CB_TARGET_DEFS_MAP
*) syscall_stat32_map
;
2452 cb
->stat_map
= stat32_map
;
2454 if (cb_syscall (cb
, &s
) != CB_RC_OK
)
2457 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2460 retval
= s
.result
== -1 ? -s
.errcode
: s
.result
;
2462 cb
->stat_map
= saved_map
;
2463 cb
->syscall_map
= saved_syscall_map
;
2467 case TARGET_SYS_getcwd
:
2472 char *cwd
= xmalloc (SIM_PATHMAX
);
2473 if (cwd
!= getcwd (cwd
, SIM_PATHMAX
))
2476 /* FIXME: When and if we support chdir, we need something
2477 a bit more elaborate. */
2478 if (simulator_sysroot
[0] != '\0')
2481 retval
= -cb_host_to_target_errno (cb
, ERANGE
);
2482 if (strlen (cwd
) + 1 <= size
)
2484 retval
= strlen (cwd
) + 1;
2485 if (sim_core_write_buffer (sd
, current_cpu
, 0, cwd
,
2487 != (unsigned int) retval
)
2488 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2494 case TARGET_SYS_readlink
:
2499 char *pbuf
= xmalloc (SIM_PATHMAX
);
2500 char *lbuf
= xmalloc (SIM_PATHMAX
);
2501 char *lbuf_alloc
= lbuf
;
2506 if (sim_core_read_unaligned_1 (current_cpu
, pc
, 0, path
) == '/')
2508 strcpy (pbuf
, simulator_sysroot
);
2509 o
+= strlen (simulator_sysroot
);
2512 for (i
= 0; i
+ o
< SIM_PATHMAX
; i
++)
2515 = sim_core_read_unaligned_1 (current_cpu
, pc
, 0, path
+ i
);
2516 if (pbuf
[i
+ o
] == 0)
2520 if (i
+ o
== SIM_PATHMAX
)
2522 retval
= -cb_host_to_target_errno (cb
, ENAMETOOLONG
);
2526 /* Intervene calls for certain files expected in the target
2527 proc file system. */
2528 if (strcmp (pbuf
+ strlen (simulator_sysroot
),
2529 "/proc/" XSTRING (TARGET_PID
) "/exe") == 0)
2532 = (STATE_PROG_ARGV (sd
) != NULL
2533 ? *STATE_PROG_ARGV (sd
) : NULL
);
2535 if (argv0
== NULL
|| *argv0
== '.')
2538 = cris_unknown_syscall (current_cpu
, pc
,
2539 "Unimplemented readlink syscall "
2540 "(0x%lx: [\"%s\"], 0x%lx)\n",
2541 (unsigned long) arg1
, pbuf
,
2542 (unsigned long) arg2
);
2545 else if (*argv0
== '/')
2547 if (strncmp (simulator_sysroot
, argv0
,
2548 strlen (simulator_sysroot
)) == 0)
2549 argv0
+= strlen (simulator_sysroot
);
2551 strcpy (lbuf
, argv0
);
2552 nchars
= strlen (argv0
) + 1;
2556 if (getcwd (lbuf
, SIM_PATHMAX
) != NULL
2557 && strlen (lbuf
) + 2 + strlen (argv0
) < SIM_PATHMAX
)
2559 if (strncmp (simulator_sysroot
, lbuf
,
2560 strlen (simulator_sysroot
)) == 0)
2561 lbuf
+= strlen (simulator_sysroot
);
2564 strcat (lbuf
, argv0
);
2565 nchars
= strlen (lbuf
) + 1;
2572 nchars
= readlink (pbuf
, lbuf
, SIM_PATHMAX
);
2574 /* We trust that the readlink result returns a *relative*
2575 link, or one already adjusted for the file-path-prefix.
2576 (We can't generally tell the difference, so we go with
2577 the easiest decision; no adjustment.) */
2581 retval
= -cb_host_to_target_errno (cb
, errno
);
2585 if (bufsiz
< nchars
)
2588 if (sim_core_write_buffer (sd
, current_cpu
, write_map
, lbuf
,
2589 buf
, nchars
) != (unsigned int) nchars
)
2590 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2599 case TARGET_SYS_sched_getscheduler
:
2603 /* FIXME: Search (other) existing threads. */
2604 if (pid
!= 0 && pid
!= TARGET_PID
)
2605 retval
= -cb_host_to_target_errno (cb
, ESRCH
);
2607 retval
= TARGET_SCHED_OTHER
;
2611 case TARGET_SYS_sched_getparam
:
2617 struct sched_param {
2621 if (pid
!= 0 && pid
!= TARGET_PID
)
2622 retval
= -cb_host_to_target_errno (cb
, ESRCH
);
2625 /* FIXME: Save scheduler setting before threads are
2627 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, paramp
,
2628 current_cpu
->thread_data
!= NULL
2630 ->thread_data
[threadno
]
2638 case TARGET_SYS_sched_setparam
:
2643 if ((pid
!= 0 && pid
!= TARGET_PID
)
2644 || sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2646 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2652 case TARGET_SYS_sched_setscheduler
:
2658 if ((pid
!= 0 && pid
!= TARGET_PID
)
2659 || policy
!= TARGET_SCHED_OTHER
2660 || sim_core_read_unaligned_4 (current_cpu
, pc
, 0,
2662 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2664 /* FIXME: Save scheduler setting to be read in later
2665 sched_getparam calls. */
2670 case TARGET_SYS_sched_yield
:
2671 /* We reschedule to the next thread after a syscall anyway, so
2672 we don't have to do anything here than to set the return
2677 case TARGET_SYS_sched_get_priority_min
:
2678 case TARGET_SYS_sched_get_priority_max
:
2680 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2685 case TARGET_SYS_ugetrlimit
:
2687 unsigned int curlim
, maxlim
;
2688 if (arg1
!= TARGET_RLIMIT_STACK
&& arg1
!= TARGET_RLIMIT_NOFILE
)
2690 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2696 unsigned long rlim_cur;
2697 unsigned long rlim_max;
2699 if (arg1
== TARGET_RLIMIT_NOFILE
)
2701 /* Sadly a very low limit. Better not lie, though. */
2702 maxlim
= curlim
= MAX_CALLBACK_FDS
;
2704 else /* arg1 == TARGET_RLIMIT_STACK */
2706 maxlim
= 0xffffffff;
2709 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
, curlim
);
2710 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, arg2
+ 4, maxlim
);
2715 case TARGET_SYS_setrlimit
:
2716 if (arg1
!= TARGET_RLIMIT_STACK
)
2718 retval
= -cb_host_to_target_errno (cb
, EINVAL
);
2721 /* FIXME: Save values for future ugetrlimit calls. */
2725 /* Provide a very limited subset of the sysctl functions, and
2726 abort for the rest. */
2727 case TARGET_SYS__sysctl
:
2730 struct __sysctl_args {
2737 unsigned long __unused[4];
2739 SI name
= sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
);
2740 SI name0
= name
== 0
2741 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, name
);
2742 SI name1
= name
== 0
2743 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, name
+ 4);
2745 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 4);
2747 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 8);
2749 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 12);
2750 SI oldlen
= oldlenp
== 0
2751 ? 0 : sim_core_read_unaligned_4 (current_cpu
, pc
, 0, oldlenp
);
2753 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 16);
2755 = sim_core_read_unaligned_4 (current_cpu
, pc
, 0, arg1
+ 20);
2757 if (name0
== TARGET_CTL_KERN
&& name1
== TARGET_CTL_KERN_VERSION
)
2759 SI to_write
= oldlen
< (SI
) sizeof (TARGET_UTSNAME
)
2760 ? oldlen
: (SI
) sizeof (TARGET_UTSNAME
);
2762 sim_core_write_unaligned_4 (current_cpu
, pc
, 0, oldlenp
,
2763 sizeof (TARGET_UTSNAME
));
2765 if (sim_core_write_buffer (sd
, current_cpu
, write_map
,
2766 TARGET_UTSNAME
, oldval
,
2768 != (unsigned int) to_write
)
2769 retval
= -cb_host_to_target_errno (cb
, EFAULT
);
2776 = cris_unknown_syscall (current_cpu
, pc
,
2777 "Unimplemented _sysctl syscall "
2778 "(0x%lx: [0x%lx, 0x%lx],"
2779 " 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
2780 (unsigned long) name
,
2781 (unsigned long) name0
,
2782 (unsigned long) name1
,
2783 (unsigned long) nlen
,
2784 (unsigned long) oldval
,
2785 (unsigned long) oldlenp
,
2786 (unsigned long) newval
,
2787 (unsigned long) newlen
);
2791 case TARGET_SYS_exit
:
2793 /* Here for all but the last thread. */
2796 = current_cpu
->thread_data
[threadno
].threadid
+ TARGET_PID
;
2798 = (current_cpu
->thread_data
[threadno
].parent_threadid
2800 int exitsig
= current_cpu
->thread_data
[threadno
].exitsig
;
2802 /* Any children are now all orphans. */
2803 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
2804 if (current_cpu
->thread_data
[i
].parent_threadid
2805 == current_cpu
->thread_data
[threadno
].threadid
)
2806 /* Make getppid(2) return 1 for them, poor little ones. */
2807 current_cpu
->thread_data
[i
].parent_threadid
= -TARGET_PID
+ 1;
2809 /* Free the cpu context data. When the parent has received
2810 the exit status, we'll clear the entry too. */
2811 free (current_cpu
->thread_data
[threadno
].cpu_context
);
2812 current_cpu
->thread_data
[threadno
].cpu_context
= NULL
;
2813 current_cpu
->m1threads
--;
2816 sim_io_eprintf (sd
, "Thread %d exited with status %d\n",
2818 sim_engine_halt (sd
, current_cpu
, NULL
, pc
, sim_stopped
,
2822 /* Still, we may want to support non-zero exit values. */
2823 current_cpu
->thread_data
[threadno
].exitval
= arg1
<< 8;
2826 deliver_signal (current_cpu
, exitsig
, ppid
);
2830 case TARGET_SYS_clone
:
2832 int nthreads
= current_cpu
->m1threads
+ 1;
2833 void *thread_cpu_data
;
2834 bfd_byte old_sp_buf
[4];
2836 const bfd_byte zeros
[4] = { 0, 0, 0, 0 };
2839 /* That's right, the syscall clone arguments are reversed
2840 compared to sys_clone notes in clone(2) and compared to
2841 other Linux ports (i.e. it's the same order as in the
2842 clone(2) libcall). */
2846 if (nthreads
== SIM_TARGET_MAX_THREADS
)
2848 retval
= -cb_host_to_target_errno (cb
, EAGAIN
);
2852 /* FIXME: Implement the low byte. */
2853 if ((flags
& ~TARGET_CSIGNAL
) !=
2856 | TARGET_CLONE_FILES
2857 | TARGET_CLONE_SIGHAND
)
2861 = cris_unknown_syscall (current_cpu
, pc
,
2862 "Unimplemented clone syscall "
2864 (unsigned long) arg1
,
2865 (unsigned long) arg2
);
2869 if (current_cpu
->thread_data
== NULL
)
2870 make_first_thread (current_cpu
);
2872 /* The created thread will get the new SP and a cleared R10.
2873 Since it's created out of a copy of the old thread and we
2874 don't have a set-register-function that just take the
2875 cpu_data as a parameter, we set the childs values first,
2876 and write back or overwrite them in the parent after the
2878 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
,
2879 H_GR_SP
, old_sp_buf
, 4);
2880 bfd_putl32 (newsp
, sp_buf
);
2881 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2882 H_GR_SP
, sp_buf
, 4);
2883 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2884 H_GR_R10
, (bfd_byte
*) zeros
, 4);
2887 ->make_thread_cpu_data
) (current_cpu
,
2888 ¤t_cpu
->cpu_data_placeholder
);
2889 (*CPU_REG_STORE (current_cpu
)) (current_cpu
,
2890 H_GR_SP
, old_sp_buf
, 4);
2892 retval
= ++current_cpu
->max_threadid
+ TARGET_PID
;
2894 /* Find an unused slot. After a few threads have been created
2895 and exited, the array is expected to be a bit fragmented.
2896 We don't reuse the first entry, though, that of the
2898 for (i
= 1; i
< SIM_TARGET_MAX_THREADS
; i
++)
2899 if (current_cpu
->thread_data
[i
].cpu_context
== NULL
2900 /* Don't reuse a zombied entry. */
2901 && current_cpu
->thread_data
[i
].threadid
== 0)
2904 memcpy (¤t_cpu
->thread_data
[i
],
2905 ¤t_cpu
->thread_data
[threadno
],
2906 sizeof (current_cpu
->thread_data
[i
]));
2907 current_cpu
->thread_data
[i
].cpu_context
= thread_cpu_data
;
2908 current_cpu
->thread_data
[i
].cpu_context_atsignal
= NULL
;
2909 current_cpu
->thread_data
[i
].threadid
= current_cpu
->max_threadid
;
2910 current_cpu
->thread_data
[i
].parent_threadid
2911 = current_cpu
->thread_data
[threadno
].threadid
;
2912 current_cpu
->thread_data
[i
].pipe_read_fd
= 0;
2913 current_cpu
->thread_data
[i
].pipe_write_fd
= 0;
2914 current_cpu
->thread_data
[i
].at_syscall
= 0;
2915 current_cpu
->thread_data
[i
].sigpending
= 0;
2916 current_cpu
->thread_data
[i
].sigsuspended
= 0;
2917 current_cpu
->thread_data
[i
].exitsig
= flags
& TARGET_CSIGNAL
;
2918 current_cpu
->m1threads
= nthreads
;
2922 /* Better watch these in case they do something necessary. */
2923 case TARGET_SYS_socketcall
:
2924 retval
= -cb_host_to_target_errno (cb
, ENOSYS
);
2927 unimplemented_syscall
:
2930 = cris_unknown_syscall (current_cpu
, pc
,
2931 "Unimplemented syscall: %d "
2932 "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
2933 callnum
, arg1
, arg2
, arg3
, arg4
, arg5
,
2938 /* Minimal support for fcntl F_GETFL as used in open+fdopen. */
2939 if (callnum
== TARGET_SYS_open
)
2941 current_cpu
->last_open_fd
= retval
;
2942 current_cpu
->last_open_flags
= arg2
;
2945 current_cpu
->last_syscall
= callnum
;
2947 /* A system call is a rescheduling point. For the time being, we don't
2948 reschedule anywhere else. */
2949 if (current_cpu
->m1threads
!= 0
2950 /* We need to schedule off from an exiting thread that is the
2952 || (current_cpu
->thread_data
!= NULL
2953 && current_cpu
->thread_data
[threadno
].cpu_context
== NULL
))
2955 bfd_byte retval_buf
[4];
2957 current_cpu
->thread_data
[threadno
].last_execution
2958 = TARGET_TIME_MS (current_cpu
);
2959 bfd_putl32 (retval
, retval_buf
);
2960 (*CPU_REG_STORE (current_cpu
)) (current_cpu
, H_GR_R10
, retval_buf
, 4);
2962 current_cpu
->thread_data
[threadno
].at_syscall
= 1;
2963 reschedule (current_cpu
);
2965 (*CPU_REG_FETCH (current_cpu
)) (current_cpu
, H_GR_R10
, retval_buf
, 4);
2966 retval
= bfd_getl32 (retval_buf
);
2972 /* Callback from simulator write saying that the pipe at (reader, writer)
2973 is now non-empty (so the writer should wait until the pipe is empty, at
2974 least not write to this or any other pipe). Simplest is to just wait
2975 until the pipe is empty. */
2978 cris_pipe_nonempty (host_callback
*cb ATTRIBUTE_UNUSED
,
2979 int reader
, int writer
)
2981 SIM_CPU
*cpu
= current_cpu_for_cb_callback
;
2982 const bfd_byte zeros
[4] = { 0, 0, 0, 0 };
2984 /* It's the current thread: we just have to re-run the current
2985 syscall instruction (presumably "break 13") and change the syscall
2986 to the special simulator-wait code. Oh, and set a marker that
2987 we're waiting, so we can disambiguate the special call from a
2990 This function may be called multiple times between cris_pipe_empty,
2991 but we must avoid e.g. decreasing PC every time. Check fd markers
2993 if (cpu
->thread_data
== NULL
)
2995 sim_io_eprintf (CPU_STATE (cpu
),
2996 "Terminating simulation due to writing pipe rd:wr %d:%d"
2997 " from one single thread\n", reader
, writer
);
2998 sim_engine_halt (CPU_STATE (cpu
), cpu
,
2999 NULL
, sim_pc_get (cpu
), sim_stopped
, SIM_SIGILL
);
3001 else if (cpu
->thread_data
[cpu
->threadno
].pipe_write_fd
== 0)
3003 cpu
->thread_data
[cpu
->threadno
].pipe_write_fd
= writer
;
3004 cpu
->thread_data
[cpu
->threadno
].pipe_read_fd
= reader
;
3005 /* FIXME: We really shouldn't change registers other than R10 in
3006 syscalls (like R9), here or elsewhere. */
3007 (*CPU_REG_STORE (cpu
)) (cpu
, H_GR_R9
, (bfd_byte
*) zeros
, 4);
3008 sim_pc_set (cpu
, sim_pc_get (cpu
) - 2);
3012 /* Callback from simulator close or read call saying that the pipe at
3013 (reader, writer) is now empty (so the writer can write again, perhaps
3014 leave a waiting state). If there are bytes remaining, they couldn't be
3015 consumed (perhaps due to the pipe closing). */
3018 cris_pipe_empty (host_callback
*cb
,
3023 SIM_CPU
*cpu
= current_cpu_for_cb_callback
;
3024 bfd_byte r10_buf
[4];
3026 = cb
->pipe_buffer
[writer
].size
- cb
->pipe_buffer
[reader
].size
;
3028 /* We need to find the thread that waits for this pipe. */
3029 for (i
= 0; i
< SIM_TARGET_MAX_THREADS
; i
++)
3030 if (cpu
->thread_data
[i
].cpu_context
3031 && cpu
->thread_data
[i
].pipe_write_fd
== writer
)
3035 /* Temporarily switch to this cpu context, so we can change the
3036 PC by ordinary calls. */
3038 memcpy (cpu
->thread_data
[cpu
->threadno
].cpu_context
,
3039 &cpu
->cpu_data_placeholder
,
3040 cpu
->thread_cpu_data_size
);
3041 memcpy (&cpu
->cpu_data_placeholder
,
3042 cpu
->thread_data
[i
].cpu_context
,
3043 cpu
->thread_cpu_data_size
);
3045 /* The return value is supposed to contain the number of
3046 written bytes, which is the number of bytes requested and
3047 returned at the write call. You might think the right
3048 thing is to adjust the return-value to be only the
3049 *consumed* number of bytes, but it isn't. We're only
3050 called if the pipe buffer is fully consumed or it is being
3051 closed, possibly with remaining bytes. For the latter
3052 case, the writer is still supposed to see success for
3053 PIPE_BUF bytes (a constant which we happen to know and is
3054 unlikely to change). The return value may also be a
3055 negative number; an error value. This case is covered
3056 because "remaining" is always >= 0. */
3057 (*CPU_REG_FETCH (cpu
)) (cpu
, H_GR_R10
, r10_buf
, 4);
3058 retval
= (int) bfd_getl_signed_32 (r10_buf
);
3059 if (retval
- remaining
> TARGET_PIPE_BUF
)
3061 bfd_putl32 (retval
- remaining
, r10_buf
);
3062 (*CPU_REG_STORE (cpu
)) (cpu
, H_GR_R10
, r10_buf
, 4);
3064 sim_pc_set (cpu
, sim_pc_get (cpu
) + 2);
3065 memcpy (cpu
->thread_data
[i
].cpu_context
,
3066 &cpu
->cpu_data_placeholder
,
3067 cpu
->thread_cpu_data_size
);
3068 memcpy (&cpu
->cpu_data_placeholder
,
3069 cpu
->thread_data
[cpu
->threadno
].cpu_context
,
3070 cpu
->thread_cpu_data_size
);
3071 cpu
->thread_data
[i
].pipe_read_fd
= 0;
3072 cpu
->thread_data
[i
].pipe_write_fd
= 0;
3079 /* We have a simulator-specific notion of time. See TARGET_TIME. */
3082 cris_time (host_callback
*cb ATTRIBUTE_UNUSED
, long *t
)
3084 long retval
= TARGET_TIME (current_cpu_for_cb_callback
);
3090 /* Set target-specific callback data. */
3093 cris_set_callbacks (host_callback
*cb
)
3095 /* Yeargh, have to cast away constness to avoid warnings. */
3096 cb
->syscall_map
= (CB_TARGET_DEFS_MAP
*) syscall_map
;
3097 cb
->errno_map
= (CB_TARGET_DEFS_MAP
*) errno_map
;
3099 /* The kernel stat64 layout. If we see a file > 2G, the "long"
3100 parameter to cb_store_target_endian will make st_size negative.
3101 Similarly for st_ino. FIXME: Find a 64-bit type, and use it
3102 *unsigned*, and/or add syntax for signed-ness. */
3103 cb
->stat_map
= stat_map
;
3104 cb
->open_map
= (CB_TARGET_DEFS_MAP
*) open_map
;
3105 cb
->pipe_nonempty
= cris_pipe_nonempty
;
3106 cb
->pipe_empty
= cris_pipe_empty
;
3107 cb
->time
= cris_time
;
3110 /* Process an address exception. */
3113 cris_core_signal (SIM_DESC sd
, SIM_CPU
*current_cpu
, sim_cia cia
,
3114 unsigned int map
, int nr_bytes
, address_word addr
,
3115 transfer_type transfer
, sim_core_signals sig
)
3117 sim_core_signal (sd
, current_cpu
, cia
, map
, nr_bytes
, addr
,