Commit | Line | Data |
---|---|---|
ef016f83 MF |
1 | /* Simulator for Analog Devices Blackfin processors. |
2 | ||
3666a048 | 3 | Copyright (C) 2005-2021 Free Software Foundation, Inc. |
ef016f83 MF |
4 | Contributed by Analog Devices, Inc. |
5 | ||
6 | This file is part of simulators. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
6df01ab8 MF |
21 | /* This must come before any other includes. */ |
22 | #include "defs.h" | |
ef016f83 MF |
23 | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <signal.h> | |
28 | #include <errno.h> | |
29 | #include <fcntl.h> | |
30 | #include <unistd.h> | |
31 | #include <sys/time.h> | |
32 | ||
ad9cc209 | 33 | #include "portability.h" |
df68e12b | 34 | #include "sim/callback.h" |
ef016f83 MF |
35 | #include "gdb/signals.h" |
36 | #include "sim-main.h" | |
61a0c964 | 37 | #include "sim-syscall.h" |
ef016f83 MF |
38 | #include "sim-hw.h" |
39 | ||
40 | #include "targ-vals.h" | |
41 | ||
aaa9dc01 MF |
42 | /* The numbers here do not matter. They just need to be unique. They also |
43 | need not be static across releases -- they're used internally only. The | |
44 | mapping from the Linux ABI to the CB values is in linux-targ-map.h. */ | |
ef016f83 MF |
45 | #define CB_SYS_ioctl 201 |
46 | #define CB_SYS_mmap2 202 | |
47 | #define CB_SYS_munmap 203 | |
48 | #define CB_SYS_dup2 204 | |
49 | #define CB_SYS_getuid 205 | |
50 | #define CB_SYS_getuid32 206 | |
51 | #define CB_SYS_getgid 207 | |
52 | #define CB_SYS_getgid32 208 | |
53 | #define CB_SYS_setuid 209 | |
54 | #define CB_SYS_setuid32 210 | |
55 | #define CB_SYS_setgid 211 | |
56 | #define CB_SYS_setgid32 212 | |
57 | #define CB_SYS_pread 213 | |
58 | #define CB_SYS__llseek 214 | |
59 | #define CB_SYS_getcwd 215 | |
60 | #define CB_SYS_stat64 216 | |
61 | #define CB_SYS_lstat64 217 | |
62 | #define CB_SYS_fstat64 218 | |
63 | #define CB_SYS_ftruncate64 219 | |
64 | #define CB_SYS_gettimeofday 220 | |
65 | #define CB_SYS_access 221 | |
66 | #include "linux-targ-map.h" | |
67 | #include "linux-fixed-code.h" | |
68 | ||
69 | #include "elf/common.h" | |
70 | #include "elf/external.h" | |
71 | #include "elf/internal.h" | |
72 | #include "elf/bfin.h" | |
73 | #include "elf-bfd.h" | |
74 | ||
75 | #include "dv-bfin_cec.h" | |
76 | #include "dv-bfin_mmu.h" | |
77 | ||
a931db6a | 78 | static const char cb_linux_stat_map_32[] = |
ef016f83 MF |
79 | /* Linux kernel 32bit layout: */ |
80 | "st_dev,2:space,2:st_ino,4:st_mode,2:st_nlink,2:st_uid,2:st_gid,2:st_rdev,2:" | |
81 | "space,2:st_size,4:st_blksize,4:st_blocks,4:st_atime,4:st_atimensec,4:" | |
82 | "st_mtime,4:st_mtimensec,4:st_ctime,4:st_ctimensec,4:space,4:space,4"; | |
83 | /* uClibc public ABI 32bit layout: | |
84 | "st_dev,8:space,2:space,2:st_ino,4:st_mode,4:st_nlink,4:st_uid,4:st_gid,4:" | |
85 | "st_rdev,8:space,2:space,2:st_size,4:st_blksiez,4:st_blocks,4:st_atime,4:" | |
86 | "st_atimensec,4:st_mtime,4:st_mtimensec,4:st_ctime,4:st_ctimensec,4:space,4:" | |
87 | "space,4"; */ | |
a931db6a | 88 | static const char cb_linux_stat_map_64[] = |
ef016f83 MF |
89 | "st_dev,8:space,4:space,4:st_mode,4:st_nlink,4:st_uid,4:st_gid,4:st_rdev,8:" |
90 | "space,4:st_size,8:st_blksize,4:st_blocks,8:st_atime,4:st_atimensec,4:" | |
91 | "st_mtime,4:st_mtimensec,4:st_ctime,4:st_ctimensec,4:st_ino,8"; | |
a931db6a MF |
92 | static const char cb_libgloss_stat_map_32[] = |
93 | "st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2:st_rdev,2:" | |
94 | "st_size,4:st_atime,4:space,4:st_mtime,4:space,4:st_ctime,4:" | |
95 | "space,4:st_blksize,4:st_blocks,4:space,8"; | |
96 | static const char *stat_map_32, *stat_map_64; | |
ef016f83 | 97 | |
ef016f83 MF |
98 | /* Simulate a monitor trap, put the result into r0 and errno into r1 |
99 | return offset by which to adjust pc. */ | |
100 | ||
101 | void | |
102 | bfin_syscall (SIM_CPU *cpu) | |
103 | { | |
104 | SIM_DESC sd = CPU_STATE (cpu); | |
2e3d4f4d | 105 | char * const *argv = (void *)STATE_PROG_ARGV (sd); |
ef016f83 MF |
106 | host_callback *cb = STATE_CALLBACK (sd); |
107 | bu32 args[6]; | |
108 | CB_SYSCALL sc; | |
109 | char *p; | |
cc5add8c | 110 | char _tbuf[1024 * 3], *tbuf = _tbuf, tstr[1024]; |
ef016f83 MF |
111 | int fmt_ret_hex = 0; |
112 | ||
113 | CB_SYSCALL_INIT (&sc); | |
114 | ||
115 | if (STATE_ENVIRONMENT (sd) == USER_ENVIRONMENT) | |
116 | { | |
117 | /* Linux syscall. */ | |
118 | sc.func = PREG (0); | |
119 | sc.arg1 = args[0] = DREG (0); | |
120 | sc.arg2 = args[1] = DREG (1); | |
121 | sc.arg3 = args[2] = DREG (2); | |
122 | sc.arg4 = args[3] = DREG (3); | |
bdedb2d2 MF |
123 | sc.arg5 = args[4] = DREG (4); |
124 | sc.arg6 = args[5] = DREG (5); | |
ef016f83 MF |
125 | } |
126 | else | |
127 | { | |
128 | /* libgloss syscall. */ | |
129 | sc.func = PREG (0); | |
130 | sc.arg1 = args[0] = GET_LONG (DREG (0)); | |
131 | sc.arg2 = args[1] = GET_LONG (DREG (0) + 4); | |
132 | sc.arg3 = args[2] = GET_LONG (DREG (0) + 8); | |
133 | sc.arg4 = args[3] = GET_LONG (DREG (0) + 12); | |
bdedb2d2 MF |
134 | sc.arg5 = args[4] = GET_LONG (DREG (0) + 16); |
135 | sc.arg6 = args[5] = GET_LONG (DREG (0) + 20); | |
ef016f83 MF |
136 | } |
137 | sc.p1 = (PTR) sd; | |
138 | sc.p2 = (PTR) cpu; | |
61a0c964 MF |
139 | sc.read_mem = sim_syscall_read_mem; |
140 | sc.write_mem = sim_syscall_write_mem; | |
ef016f83 MF |
141 | |
142 | /* Common cb_syscall() handles most functions. */ | |
143 | switch (cb_target_to_host_syscall (cb, sc.func)) | |
144 | { | |
145 | case CB_SYS_exit: | |
146 | tbuf += sprintf (tbuf, "exit(%i)", args[0]); | |
147 | sim_engine_halt (sd, cpu, NULL, PCREG, sim_exited, sc.arg1); | |
148 | ||
149 | #ifdef CB_SYS_argc | |
150 | case CB_SYS_argc: | |
151 | tbuf += sprintf (tbuf, "argc()"); | |
34fed699 | 152 | sc.result = countargv ((char **)argv); |
ef016f83 MF |
153 | break; |
154 | case CB_SYS_argnlen: | |
155 | { | |
156 | tbuf += sprintf (tbuf, "argnlen(%u)", args[0]); | |
34fed699 | 157 | if (sc.arg1 < countargv ((char **)argv)) |
ef016f83 MF |
158 | sc.result = strlen (argv[sc.arg1]); |
159 | else | |
160 | sc.result = -1; | |
161 | } | |
162 | break; | |
163 | case CB_SYS_argn: | |
164 | { | |
165 | tbuf += sprintf (tbuf, "argn(%u)", args[0]); | |
34fed699 | 166 | if (sc.arg1 < countargv ((char **)argv)) |
ef016f83 MF |
167 | { |
168 | const char *argn = argv[sc.arg1]; | |
169 | int len = strlen (argn); | |
170 | int written = sc.write_mem (cb, &sc, sc.arg2, argn, len + 1); | |
171 | if (written == len + 1) | |
172 | sc.result = sc.arg2; | |
173 | else | |
174 | sc.result = -1; | |
175 | } | |
176 | else | |
177 | sc.result = -1; | |
178 | } | |
179 | break; | |
180 | #endif | |
181 | ||
182 | case CB_SYS_gettimeofday: | |
183 | { | |
184 | struct timeval _tv, *tv = &_tv; | |
185 | struct timezone _tz, *tz = &_tz; | |
186 | ||
187 | tbuf += sprintf (tbuf, "gettimeofday(%#x, %#x)", args[0], args[1]); | |
188 | ||
189 | if (sc.arg1 == 0) | |
190 | tv = NULL; | |
191 | if (sc.arg2 == 0) | |
192 | tz = NULL; | |
193 | sc.result = gettimeofday (tv, tz); | |
194 | ||
195 | if (sc.result == 0) | |
196 | { | |
197 | bu32 t; | |
198 | ||
199 | if (tv) | |
200 | { | |
201 | t = tv->tv_sec; | |
202 | sc.write_mem (cb, &sc, sc.arg1, (void *)&t, 4); | |
203 | t = tv->tv_usec; | |
204 | sc.write_mem (cb, &sc, sc.arg1 + 4, (void *)&t, 4); | |
205 | } | |
206 | ||
207 | if (sc.arg2) | |
208 | { | |
209 | t = tz->tz_minuteswest; | |
210 | sc.write_mem (cb, &sc, sc.arg1, (void *)&t, 4); | |
211 | t = tz->tz_dsttime; | |
212 | sc.write_mem (cb, &sc, sc.arg1 + 4, (void *)&t, 4); | |
213 | } | |
214 | } | |
215 | else | |
216 | goto sys_finish; | |
217 | } | |
218 | break; | |
219 | ||
220 | case CB_SYS_ioctl: | |
221 | /* XXX: hack just enough to get basic stdio w/uClibc ... */ | |
222 | tbuf += sprintf (tbuf, "ioctl(%i, %#x, %u)", args[0], args[1], args[2]); | |
223 | if (sc.arg2 == 0x5401) | |
224 | { | |
225 | sc.result = !isatty (sc.arg1); | |
226 | sc.errcode = 0; | |
227 | } | |
228 | else | |
229 | { | |
230 | sc.result = -1; | |
231 | sc.errcode = TARGET_EINVAL; | |
232 | } | |
233 | break; | |
234 | ||
235 | case CB_SYS_mmap2: | |
236 | { | |
237 | static bu32 heap = BFIN_DEFAULT_MEM_SIZE / 2; | |
238 | ||
239 | fmt_ret_hex = 1; | |
240 | tbuf += sprintf (tbuf, "mmap2(%#x, %u, %#x, %#x, %i, %u)", | |
241 | args[0], args[1], args[2], args[3], args[4], args[5]); | |
242 | ||
243 | sc.errcode = 0; | |
244 | ||
245 | if (sc.arg4 & 0x20 /*MAP_ANONYMOUS*/) | |
246 | /* XXX: We don't handle zeroing, but default is all zeros. */; | |
247 | else if (args[4] >= MAX_CALLBACK_FDS) | |
248 | sc.errcode = TARGET_ENOSYS; | |
249 | else | |
250 | { | |
9e6584c9 | 251 | #ifdef HAVE_PREAD |
ef016f83 MF |
252 | char *data = xmalloc (sc.arg2); |
253 | ||
254 | /* XXX: Should add a cb->pread. */ | |
255 | if (pread (cb->fdmap[args[4]], data, sc.arg2, args[5] << 12) == sc.arg2) | |
256 | sc.write_mem (cb, &sc, heap, data, sc.arg2); | |
257 | else | |
258 | sc.errcode = TARGET_EINVAL; | |
259 | ||
260 | free (data); | |
9e6584c9 MF |
261 | #else |
262 | sc.errcode = TARGET_ENOSYS; | |
263 | #endif | |
ef016f83 MF |
264 | } |
265 | ||
266 | if (sc.errcode) | |
267 | { | |
268 | sc.result = -1; | |
269 | break; | |
270 | } | |
271 | ||
272 | sc.result = heap; | |
273 | heap += sc.arg2; | |
274 | /* Keep it page aligned. */ | |
0ede24f2 | 275 | heap = align_up (heap, 4096); |
ef016f83 MF |
276 | |
277 | break; | |
278 | } | |
279 | ||
280 | case CB_SYS_munmap: | |
281 | /* XXX: meh, just lie for mmap(). */ | |
282 | tbuf += sprintf (tbuf, "munmap(%#x, %u)", args[0], args[1]); | |
283 | sc.result = 0; | |
284 | break; | |
285 | ||
286 | case CB_SYS_dup2: | |
287 | tbuf += sprintf (tbuf, "dup2(%i, %i)", args[0], args[1]); | |
288 | if (sc.arg1 >= MAX_CALLBACK_FDS || sc.arg2 >= MAX_CALLBACK_FDS) | |
289 | { | |
290 | sc.result = -1; | |
291 | sc.errcode = TARGET_EINVAL; | |
292 | } | |
293 | else | |
294 | { | |
295 | sc.result = dup2 (cb->fdmap[sc.arg1], cb->fdmap[sc.arg2]); | |
296 | goto sys_finish; | |
297 | } | |
298 | break; | |
299 | ||
300 | case CB_SYS__llseek: | |
301 | tbuf += sprintf (tbuf, "llseek(%i, %u, %u, %#x, %u)", | |
302 | args[0], args[1], args[2], args[3], args[4]); | |
303 | sc.func = TARGET_LINUX_SYS_lseek; | |
304 | if (sc.arg2) | |
305 | { | |
306 | sc.result = -1; | |
307 | sc.errcode = TARGET_EINVAL; | |
308 | } | |
309 | else | |
310 | { | |
311 | sc.arg2 = sc.arg3; | |
312 | sc.arg3 = args[4]; | |
313 | cb_syscall (cb, &sc); | |
314 | if (sc.result != -1) | |
315 | { | |
316 | bu32 z = 0; | |
317 | sc.write_mem (cb, &sc, args[3], (void *)&sc.result, 4); | |
318 | sc.write_mem (cb, &sc, args[3] + 4, (void *)&z, 4); | |
319 | } | |
320 | } | |
321 | break; | |
322 | ||
323 | /* XXX: Should add a cb->pread. */ | |
324 | case CB_SYS_pread: | |
325 | tbuf += sprintf (tbuf, "pread(%i, %#x, %u, %i)", | |
326 | args[0], args[1], args[2], args[3]); | |
327 | if (sc.arg1 >= MAX_CALLBACK_FDS) | |
328 | { | |
329 | sc.result = -1; | |
330 | sc.errcode = TARGET_EINVAL; | |
331 | } | |
332 | else | |
333 | { | |
334 | long old_pos, read_result, read_errcode; | |
335 | ||
336 | /* Get current filepos. */ | |
337 | sc.func = TARGET_LINUX_SYS_lseek; | |
338 | sc.arg2 = 0; | |
339 | sc.arg3 = SEEK_CUR; | |
340 | cb_syscall (cb, &sc); | |
341 | if (sc.result == -1) | |
342 | break; | |
343 | old_pos = sc.result; | |
344 | ||
345 | /* Move to the new pos. */ | |
346 | sc.func = TARGET_LINUX_SYS_lseek; | |
347 | sc.arg2 = args[3]; | |
348 | sc.arg3 = SEEK_SET; | |
349 | cb_syscall (cb, &sc); | |
350 | if (sc.result == -1) | |
351 | break; | |
352 | ||
353 | /* Read the data. */ | |
354 | sc.func = TARGET_LINUX_SYS_read; | |
355 | sc.arg2 = args[1]; | |
356 | sc.arg3 = args[2]; | |
357 | cb_syscall (cb, &sc); | |
358 | read_result = sc.result; | |
359 | read_errcode = sc.errcode; | |
360 | ||
361 | /* Move back to the old pos. */ | |
362 | sc.func = TARGET_LINUX_SYS_lseek; | |
363 | sc.arg2 = old_pos; | |
364 | sc.arg3 = SEEK_SET; | |
365 | cb_syscall (cb, &sc); | |
366 | ||
367 | sc.result = read_result; | |
368 | sc.errcode = read_errcode; | |
369 | } | |
370 | break; | |
371 | ||
372 | case CB_SYS_getcwd: | |
373 | tbuf += sprintf (tbuf, "getcwd(%#x, %u)", args[0], args[1]); | |
374 | ||
375 | p = alloca (sc.arg2); | |
376 | if (getcwd (p, sc.arg2) == NULL) | |
377 | { | |
378 | sc.result = -1; | |
379 | sc.errcode = TARGET_EINVAL; | |
380 | } | |
381 | else | |
382 | { | |
383 | sc.write_mem (cb, &sc, sc.arg1, p, sc.arg2); | |
384 | sc.result = sc.arg1; | |
385 | } | |
386 | break; | |
387 | ||
388 | case CB_SYS_stat64: | |
cc5add8c MF |
389 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
390 | strcpy (tstr, "???"); | |
391 | tbuf += sprintf (tbuf, "stat64(%#x:\"%s\", %u)", args[0], tstr, args[1]); | |
ef016f83 MF |
392 | cb->stat_map = stat_map_64; |
393 | sc.func = TARGET_LINUX_SYS_stat; | |
394 | cb_syscall (cb, &sc); | |
395 | cb->stat_map = stat_map_32; | |
396 | break; | |
397 | case CB_SYS_lstat64: | |
cc5add8c MF |
398 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
399 | strcpy (tstr, "???"); | |
400 | tbuf += sprintf (tbuf, "lstat64(%#x:\"%s\", %u)", args[0], tstr, args[1]); | |
ef016f83 MF |
401 | cb->stat_map = stat_map_64; |
402 | sc.func = TARGET_LINUX_SYS_lstat; | |
403 | cb_syscall (cb, &sc); | |
404 | cb->stat_map = stat_map_32; | |
405 | break; | |
406 | case CB_SYS_fstat64: | |
407 | tbuf += sprintf (tbuf, "fstat64(%#x, %u)", args[0], args[1]); | |
408 | cb->stat_map = stat_map_64; | |
409 | sc.func = TARGET_LINUX_SYS_fstat; | |
410 | cb_syscall (cb, &sc); | |
411 | cb->stat_map = stat_map_32; | |
412 | break; | |
413 | ||
414 | case CB_SYS_ftruncate64: | |
415 | tbuf += sprintf (tbuf, "ftruncate64(%u, %u)", args[0], args[1]); | |
416 | sc.func = TARGET_LINUX_SYS_ftruncate; | |
417 | cb_syscall (cb, &sc); | |
418 | break; | |
419 | ||
420 | case CB_SYS_getuid: | |
421 | case CB_SYS_getuid32: | |
422 | tbuf += sprintf (tbuf, "getuid()"); | |
423 | sc.result = getuid (); | |
424 | goto sys_finish; | |
425 | case CB_SYS_getgid: | |
426 | case CB_SYS_getgid32: | |
427 | tbuf += sprintf (tbuf, "getgid()"); | |
428 | sc.result = getgid (); | |
429 | goto sys_finish; | |
430 | case CB_SYS_setuid: | |
431 | sc.arg1 &= 0xffff; | |
432 | case CB_SYS_setuid32: | |
433 | tbuf += sprintf (tbuf, "setuid(%u)", args[0]); | |
434 | sc.result = setuid (sc.arg1); | |
435 | goto sys_finish; | |
436 | case CB_SYS_setgid: | |
437 | sc.arg1 &= 0xffff; | |
438 | case CB_SYS_setgid32: | |
439 | tbuf += sprintf (tbuf, "setgid(%u)", args[0]); | |
440 | sc.result = setgid (sc.arg1); | |
441 | goto sys_finish; | |
442 | ||
ef016f83 MF |
443 | case CB_SYS_kill: |
444 | tbuf += sprintf (tbuf, "kill(%u, %i)", args[0], args[1]); | |
445 | /* Only let the app kill itself. */ | |
446 | if (sc.arg1 != getpid ()) | |
447 | { | |
448 | sc.result = -1; | |
449 | sc.errcode = TARGET_EPERM; | |
450 | } | |
451 | else | |
452 | { | |
9e6584c9 | 453 | #ifdef HAVE_KILL |
ef016f83 MF |
454 | sc.result = kill (sc.arg1, sc.arg2); |
455 | goto sys_finish; | |
9e6584c9 MF |
456 | #else |
457 | sc.result = -1; | |
458 | sc.errcode = TARGET_ENOSYS; | |
459 | #endif | |
ef016f83 MF |
460 | } |
461 | break; | |
462 | ||
463 | case CB_SYS_open: | |
cc5add8c MF |
464 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
465 | strcpy (tstr, "???"); | |
466 | tbuf += sprintf (tbuf, "open(%#x:\"%s\", %#x, %o)", | |
467 | args[0], tstr, args[1], args[2]); | |
ef016f83 MF |
468 | goto case_default; |
469 | case CB_SYS_close: | |
470 | tbuf += sprintf (tbuf, "close(%i)", args[0]); | |
471 | goto case_default; | |
472 | case CB_SYS_read: | |
473 | tbuf += sprintf (tbuf, "read(%i, %#x, %u)", args[0], args[1], args[2]); | |
474 | goto case_default; | |
475 | case CB_SYS_write: | |
cc5add8c MF |
476 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[1])) |
477 | strcpy (tstr, "???"); | |
478 | tbuf += sprintf (tbuf, "write(%i, %#x:\"%s\", %u)", | |
479 | args[0], args[1], tstr, args[2]); | |
ef016f83 MF |
480 | goto case_default; |
481 | case CB_SYS_lseek: | |
482 | tbuf += sprintf (tbuf, "lseek(%i, %i, %i)", args[0], args[1], args[2]); | |
483 | goto case_default; | |
484 | case CB_SYS_unlink: | |
cc5add8c MF |
485 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
486 | strcpy (tstr, "???"); | |
487 | tbuf += sprintf (tbuf, "unlink(%#x:\"%s\")", args[0], tstr); | |
ef016f83 MF |
488 | goto case_default; |
489 | case CB_SYS_truncate: | |
cc5add8c MF |
490 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
491 | strcpy (tstr, "???"); | |
492 | tbuf += sprintf (tbuf, "truncate(%#x:\"%s\", %i)", args[0], tstr, args[1]); | |
ef016f83 MF |
493 | goto case_default; |
494 | case CB_SYS_ftruncate: | |
495 | tbuf += sprintf (tbuf, "ftruncate(%i, %i)", args[0], args[1]); | |
496 | goto case_default; | |
497 | case CB_SYS_rename: | |
cc5add8c MF |
498 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
499 | strcpy (tstr, "???"); | |
500 | tbuf += sprintf (tbuf, "rename(%#x:\"%s\", ", args[0], tstr); | |
501 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[1])) | |
502 | strcpy (tstr, "???"); | |
503 | tbuf += sprintf (tbuf, "%#x:\"%s\")", args[1], tstr); | |
ef016f83 MF |
504 | goto case_default; |
505 | case CB_SYS_stat: | |
cc5add8c MF |
506 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
507 | strcpy (tstr, "???"); | |
508 | tbuf += sprintf (tbuf, "stat(%#x:\"%s\", %#x)", args[0], tstr, args[1]); | |
ef016f83 MF |
509 | goto case_default; |
510 | case CB_SYS_fstat: | |
511 | tbuf += sprintf (tbuf, "fstat(%i, %#x)", args[0], args[1]); | |
512 | goto case_default; | |
513 | case CB_SYS_lstat: | |
cc5add8c MF |
514 | if (cb_get_string (cb, &sc, tstr, sizeof (tstr), args[0])) |
515 | strcpy (tstr, "???"); | |
516 | tbuf += sprintf (tbuf, "lstat(%#x:\"%s\", %#x)", args[0], tstr, args[1]); | |
ef016f83 MF |
517 | goto case_default; |
518 | case CB_SYS_pipe: | |
519 | tbuf += sprintf (tbuf, "pipe(%#x, %#x)", args[0], args[1]); | |
520 | goto case_default; | |
521 | ||
522 | default: | |
523 | tbuf += sprintf (tbuf, "???_%i(%#x, %#x, %#x, %#x, %#x, %#x)", sc.func, | |
524 | args[0], args[1], args[2], args[3], args[4], args[5]); | |
525 | case_default: | |
526 | cb_syscall (cb, &sc); | |
527 | break; | |
528 | ||
529 | sys_finish: | |
530 | if (sc.result == -1) | |
531 | { | |
532 | cb->last_errno = errno; | |
533 | sc.errcode = cb->get_errno (cb); | |
534 | } | |
535 | } | |
536 | ||
537 | TRACE_EVENTS (cpu, "syscall_%i(%#x, %#x, %#x, %#x, %#x, %#x) = %li (error = %i)", | |
538 | sc.func, args[0], args[1], args[2], args[3], args[4], args[5], | |
539 | sc.result, sc.errcode); | |
540 | ||
541 | tbuf += sprintf (tbuf, " = "); | |
542 | if (STATE_ENVIRONMENT (sd) == USER_ENVIRONMENT) | |
543 | { | |
544 | if (sc.result == -1) | |
545 | { | |
546 | tbuf += sprintf (tbuf, "-1 (error = %i)", sc.errcode); | |
547 | if (sc.errcode == cb_host_to_target_errno (cb, ENOSYS)) | |
548 | { | |
549 | sim_io_eprintf (sd, "bfin-sim: %#x: unimplemented syscall %i\n", | |
550 | PCREG, sc.func); | |
551 | } | |
552 | SET_DREG (0, -sc.errcode); | |
553 | } | |
554 | else | |
555 | { | |
556 | if (fmt_ret_hex) | |
557 | tbuf += sprintf (tbuf, "%#lx", sc.result); | |
558 | else | |
559 | tbuf += sprintf (tbuf, "%lu", sc.result); | |
560 | SET_DREG (0, sc.result); | |
561 | } | |
562 | } | |
563 | else | |
564 | { | |
565 | tbuf += sprintf (tbuf, "%lu (error = %i)", sc.result, sc.errcode); | |
566 | SET_DREG (0, sc.result); | |
ce2486ab MF |
567 | SET_DREG (1, sc.result2); |
568 | SET_DREG (2, sc.errcode); | |
ef016f83 MF |
569 | } |
570 | ||
571 | TRACE_SYSCALL (cpu, "%s", _tbuf); | |
572 | } | |
573 | ||
ef016f83 MF |
574 | /* Execute a single instruction. */ |
575 | ||
576 | static sim_cia | |
577 | step_once (SIM_CPU *cpu) | |
578 | { | |
579 | SIM_DESC sd = CPU_STATE (cpu); | |
580 | bu32 insn_len, oldpc = PCREG; | |
581 | int i; | |
582 | bool ssstep; | |
583 | ||
584 | if (TRACE_ANY_P (cpu)) | |
585 | trace_prefix (sd, cpu, NULL_CIA, oldpc, TRACE_LINENUM_P (cpu), | |
586 | NULL, 0, " "); /* Use a space for gcc warnings. */ | |
587 | ||
824c8628 MF |
588 | TRACE_DISASM (cpu, oldpc); |
589 | ||
ef016f83 MF |
590 | /* Handle hardware single stepping when lower than EVT3, and when SYSCFG |
591 | has already had the SSSTEP bit enabled. */ | |
592 | ssstep = false; | |
593 | if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT | |
594 | && (SYSCFGREG & SYSCFG_SSSTEP)) | |
595 | { | |
596 | int ivg = cec_get_ivg (cpu); | |
597 | if (ivg == -1 || ivg > 3) | |
598 | ssstep = true; | |
599 | } | |
600 | ||
601 | #if 0 | |
602 | /* XXX: Is this what happens on the hardware ? */ | |
603 | if (cec_get_ivg (cpu) == EVT_EMU) | |
604 | cec_return (cpu, EVT_EMU); | |
605 | #endif | |
606 | ||
607 | BFIN_CPU_STATE.did_jump = false; | |
608 | ||
609 | insn_len = interp_insn_bfin (cpu, oldpc); | |
610 | ||
611 | /* If we executed this insn successfully, then we always decrement | |
612 | the loop counter. We don't want to update the PC though if the | |
613 | last insn happened to be a change in code flow (jump/etc...). */ | |
614 | if (!BFIN_CPU_STATE.did_jump) | |
615 | SET_PCREG (hwloop_get_next_pc (cpu, oldpc, insn_len)); | |
616 | for (i = 1; i >= 0; --i) | |
617 | if (LCREG (i) && oldpc == LBREG (i)) | |
618 | { | |
619 | SET_LCREG (i, LCREG (i) - 1); | |
620 | if (LCREG (i)) | |
621 | break; | |
622 | } | |
623 | ||
624 | ++ PROFILE_TOTAL_INSN_COUNT (CPU_PROFILE_DATA (cpu)); | |
625 | ||
626 | /* Handle hardware single stepping only if we're still lower than EVT3. | |
627 | XXX: May not be entirely correct wrt EXCPT insns. */ | |
628 | if (ssstep) | |
629 | { | |
630 | int ivg = cec_get_ivg (cpu); | |
631 | if (ivg == -1 || ivg > 3) | |
632 | { | |
633 | INSN_LEN = 0; | |
634 | cec_exception (cpu, VEC_STEP); | |
635 | } | |
636 | } | |
637 | ||
638 | return oldpc; | |
639 | } | |
640 | ||
641 | void | |
642 | sim_engine_run (SIM_DESC sd, | |
643 | int next_cpu_nr, /* ignore */ | |
644 | int nr_cpus, /* ignore */ | |
645 | int siggnal) /* ignore */ | |
646 | { | |
647 | bu32 ticks; | |
648 | SIM_CPU *cpu; | |
649 | ||
650 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
651 | ||
652 | cpu = STATE_CPU (sd, 0); | |
653 | ||
654 | while (1) | |
655 | { | |
656 | step_once (cpu); | |
657 | /* Process any events -- can't use tickn because it may | |
658 | advance right over the next event. */ | |
659 | for (ticks = 0; ticks < CYCLE_DELAY; ++ticks) | |
660 | if (sim_events_tick (sd)) | |
661 | sim_events_process (sd); | |
662 | } | |
663 | } | |
664 | ||
665 | /* Cover function of sim_state_free to free the cpu buffers as well. */ | |
666 | ||
667 | static void | |
668 | free_state (SIM_DESC sd) | |
669 | { | |
670 | if (STATE_MODULES (sd) != NULL) | |
671 | sim_module_uninstall (sd); | |
672 | sim_cpu_free_all (sd); | |
673 | sim_state_free (sd); | |
674 | } | |
675 | ||
676 | /* Create an instance of the simulator. */ | |
677 | ||
678 | static void | |
679 | bfin_initialize_cpu (SIM_DESC sd, SIM_CPU *cpu) | |
680 | { | |
681 | memset (&cpu->state, 0, sizeof (cpu->state)); | |
682 | ||
683 | PROFILE_TOTAL_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = 0; | |
684 | ||
685 | bfin_model_cpu_init (sd, cpu); | |
686 | ||
687 | /* Set default stack to top of scratch pad. */ | |
688 | SET_SPREG (BFIN_DEFAULT_MEM_SIZE); | |
689 | SET_KSPREG (BFIN_DEFAULT_MEM_SIZE); | |
690 | SET_USPREG (BFIN_DEFAULT_MEM_SIZE); | |
691 | ||
692 | /* This is what the hardware likes. */ | |
693 | SET_SYSCFGREG (0x30); | |
694 | } | |
695 | ||
696 | SIM_DESC | |
697 | sim_open (SIM_OPEN_KIND kind, host_callback *callback, | |
2e3d4f4d | 698 | struct bfd *abfd, char * const *argv) |
ef016f83 MF |
699 | { |
700 | char c; | |
701 | int i; | |
85d93de3 MF |
702 | SIM_DESC sd = sim_state_alloc_extra (kind, callback, |
703 | sizeof (struct bfin_board_data)); | |
ef016f83 | 704 | |
ba307cdd MF |
705 | /* Set default options before parsing user options. */ |
706 | current_alignment = STRICT_ALIGNMENT; | |
f9a4d543 | 707 | current_target_byte_order = BFD_ENDIAN_LITTLE; |
ba307cdd | 708 | |
ef016f83 | 709 | /* The cpu data is kept in a separately allocated chunk of memory. */ |
d5a71b11 | 710 | if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK) |
ef016f83 MF |
711 | { |
712 | free_state (sd); | |
713 | return 0; | |
714 | } | |
715 | ||
ef016f83 MF |
716 | if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) |
717 | { | |
718 | free_state (sd); | |
719 | return 0; | |
720 | } | |
721 | ||
722 | /* XXX: Default to the Virtual environment. */ | |
723 | if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT) | |
724 | STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; | |
725 | ||
77cf2ef5 | 726 | /* The parser will print an error message for us, so we silently return. */ |
ef016f83 MF |
727 | if (sim_parse_args (sd, argv) != SIM_RC_OK) |
728 | { | |
729 | free_state (sd); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | /* Allocate external memory if none specified by user. | |
734 | Use address 4 here in case the user wanted address 0 unmapped. */ | |
735 | if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0) | |
736 | { | |
737 | bu16 emuexcpt = 0x25; | |
f08708cb | 738 | sim_do_commandf (sd, "memory-size 0x%x", BFIN_DEFAULT_MEM_SIZE); |
ef016f83 MF |
739 | sim_write (sd, 0, (void *)&emuexcpt, 2); |
740 | } | |
741 | ||
742 | /* Check for/establish the a reference program image. */ | |
743 | if (sim_analyze_program (sd, | |
744 | (STATE_PROG_ARGV (sd) != NULL | |
745 | ? *STATE_PROG_ARGV (sd) | |
746 | : NULL), abfd) != SIM_RC_OK) | |
747 | { | |
748 | free_state (sd); | |
749 | return 0; | |
750 | } | |
751 | ||
752 | /* Establish any remaining configuration options. */ | |
753 | if (sim_config (sd) != SIM_RC_OK) | |
754 | { | |
755 | free_state (sd); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | if (sim_post_argv_init (sd) != SIM_RC_OK) | |
760 | { | |
761 | free_state (sd); | |
762 | return 0; | |
763 | } | |
764 | ||
765 | /* CPU specific initialization. */ | |
766 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
767 | { | |
768 | SIM_CPU *cpu = STATE_CPU (sd, i); | |
769 | bfin_initialize_cpu (sd, cpu); | |
770 | } | |
771 | ||
772 | return sd; | |
773 | } | |
774 | ||
ef016f83 | 775 | /* Some utils don't like having a NULL environ. */ |
2e3d4f4d | 776 | static char * const simple_env[] = { "HOME=/", "PATH=/bin", NULL }; |
ef016f83 MF |
777 | |
778 | static bu32 fdpic_load_offset; | |
779 | ||
780 | static bool | |
781 | bfin_fdpic_load (SIM_DESC sd, SIM_CPU *cpu, struct bfd *abfd, bu32 *sp, | |
782 | bu32 *elf_addrs, char **ldso_path) | |
783 | { | |
784 | bool ret; | |
785 | int i; | |
786 | ||
787 | Elf_Internal_Ehdr *iehdr; | |
788 | Elf32_External_Ehdr ehdr; | |
789 | Elf_Internal_Phdr *phdrs; | |
790 | unsigned char *data; | |
791 | long phdr_size; | |
792 | int phdrc; | |
793 | bu32 nsegs; | |
794 | ||
795 | bu32 max_load_addr; | |
796 | ||
797 | unsigned char null[4] = { 0, 0, 0, 0 }; | |
798 | ||
799 | ret = false; | |
800 | *ldso_path = NULL; | |
801 | ||
802 | /* See if this an FDPIC ELF. */ | |
803 | phdrs = NULL; | |
804 | if (!abfd) | |
805 | goto skip_fdpic_init; | |
806 | if (bfd_seek (abfd, 0, SEEK_SET) != 0) | |
807 | goto skip_fdpic_init; | |
808 | if (bfd_bread (&ehdr, sizeof (ehdr), abfd) != sizeof (ehdr)) | |
809 | goto skip_fdpic_init; | |
810 | iehdr = elf_elfheader (abfd); | |
811 | if (!(iehdr->e_flags & EF_BFIN_FDPIC)) | |
812 | goto skip_fdpic_init; | |
813 | ||
814 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
815 | sim_io_printf (sd, "Loading FDPIC ELF %s\n Load base: %#x\n ELF entry: %#x\n", | |
816 | bfd_get_filename (abfd), fdpic_load_offset, elf_addrs[0]); | |
817 | ||
818 | /* Grab the Program Headers to set up the loadsegs on the stack. */ | |
819 | phdr_size = bfd_get_elf_phdr_upper_bound (abfd); | |
820 | if (phdr_size == -1) | |
821 | goto skip_fdpic_init; | |
822 | phdrs = xmalloc (phdr_size); | |
823 | phdrc = bfd_get_elf_phdrs (abfd, phdrs); | |
824 | if (phdrc == -1) | |
825 | goto skip_fdpic_init; | |
826 | ||
827 | /* Push the Ehdr onto the stack. */ | |
828 | *sp -= sizeof (ehdr); | |
829 | elf_addrs[3] = *sp; | |
830 | sim_write (sd, *sp, (void *)&ehdr, sizeof (ehdr)); | |
831 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
832 | sim_io_printf (sd, " Elf_Ehdr: %#x\n", *sp); | |
833 | ||
834 | /* Since we're relocating things ourselves, we need to relocate | |
835 | the start address as well. */ | |
836 | elf_addrs[0] = bfd_get_start_address (abfd) + fdpic_load_offset; | |
837 | ||
838 | /* And the Exec's Phdrs onto the stack. */ | |
839 | if (STATE_PROG_BFD (sd) == abfd) | |
840 | { | |
841 | elf_addrs[4] = elf_addrs[0]; | |
842 | ||
843 | phdr_size = iehdr->e_phentsize * iehdr->e_phnum; | |
844 | if (bfd_seek (abfd, iehdr->e_phoff, SEEK_SET) != 0) | |
845 | goto skip_fdpic_init; | |
846 | data = xmalloc (phdr_size); | |
847 | if (bfd_bread (data, phdr_size, abfd) != phdr_size) | |
848 | goto skip_fdpic_init; | |
849 | *sp -= phdr_size; | |
850 | elf_addrs[1] = *sp; | |
851 | elf_addrs[2] = phdrc; | |
852 | sim_write (sd, *sp, data, phdr_size); | |
853 | free (data); | |
854 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
855 | sim_io_printf (sd, " Elf_Phdrs: %#x\n", *sp); | |
856 | } | |
857 | ||
858 | /* Now push all the loadsegs. */ | |
859 | nsegs = 0; | |
860 | max_load_addr = 0; | |
861 | for (i = phdrc; i >= 0; --i) | |
862 | if (phdrs[i].p_type == PT_LOAD) | |
863 | { | |
864 | Elf_Internal_Phdr *p = &phdrs[i]; | |
865 | bu32 paddr, vaddr, memsz, filesz; | |
866 | ||
867 | paddr = p->p_paddr + fdpic_load_offset; | |
868 | vaddr = p->p_vaddr; | |
869 | memsz = p->p_memsz; | |
870 | filesz = p->p_filesz; | |
871 | ||
872 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
873 | sim_io_printf (sd, " PHDR %i: vma %#x lma %#x filesz %#x memsz %#x\n", | |
874 | i, vaddr, paddr, filesz, memsz); | |
875 | ||
876 | data = xmalloc (memsz); | |
877 | if (memsz != filesz) | |
878 | memset (data + filesz, 0, memsz - filesz); | |
879 | ||
880 | if (bfd_seek (abfd, p->p_offset, SEEK_SET) == 0 | |
881 | && bfd_bread (data, filesz, abfd) == filesz) | |
882 | sim_write (sd, paddr, data, memsz); | |
883 | ||
884 | free (data); | |
885 | ||
bc273e17 | 886 | max_load_addr = max (paddr + memsz, max_load_addr); |
ef016f83 MF |
887 | |
888 | *sp -= 12; | |
889 | sim_write (sd, *sp+0, (void *)&paddr, 4); /* loadseg.addr */ | |
890 | sim_write (sd, *sp+4, (void *)&vaddr, 4); /* loadseg.p_vaddr */ | |
891 | sim_write (sd, *sp+8, (void *)&memsz, 4); /* loadseg.p_memsz */ | |
892 | ++nsegs; | |
893 | } | |
894 | else if (phdrs[i].p_type == PT_DYNAMIC) | |
895 | { | |
896 | elf_addrs[5] = phdrs[i].p_paddr + fdpic_load_offset; | |
897 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
898 | sim_io_printf (sd, " PT_DYNAMIC: %#x\n", elf_addrs[5]); | |
899 | } | |
900 | else if (phdrs[i].p_type == PT_INTERP) | |
901 | { | |
902 | uint32_t off = phdrs[i].p_offset; | |
903 | uint32_t len = phdrs[i].p_filesz; | |
904 | ||
905 | *ldso_path = xmalloc (len); | |
906 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
907 | || bfd_bread (*ldso_path, len, abfd) != len) | |
908 | { | |
909 | free (*ldso_path); | |
910 | *ldso_path = NULL; | |
911 | } | |
912 | else if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG) | |
913 | sim_io_printf (sd, " PT_INTERP: %s\n", *ldso_path); | |
914 | } | |
915 | ||
916 | /* Update the load offset with a few extra pages. */ | |
0ede24f2 MF |
917 | fdpic_load_offset = align_up (max (max_load_addr, fdpic_load_offset), |
918 | 0x10000); | |
ef016f83 MF |
919 | fdpic_load_offset += 0x10000; |
920 | ||
921 | /* Push the summary loadmap info onto the stack last. */ | |
922 | *sp -= 4; | |
923 | sim_write (sd, *sp+0, null, 2); /* loadmap.version */ | |
924 | sim_write (sd, *sp+2, (void *)&nsegs, 2); /* loadmap.nsegs */ | |
925 | ||
926 | ret = true; | |
927 | skip_fdpic_init: | |
928 | free (phdrs); | |
929 | ||
930 | return ret; | |
931 | } | |
932 | ||
933 | static void | |
934 | bfin_user_init (SIM_DESC sd, SIM_CPU *cpu, struct bfd *abfd, | |
2e3d4f4d | 935 | char * const *argv, char * const *env) |
ef016f83 MF |
936 | { |
937 | /* XXX: Missing host -> target endian ... */ | |
938 | /* Linux starts the user app with the stack: | |
939 | argc | |
940 | argv[0] -- pointers to the actual strings | |
941 | argv[1..N] | |
942 | NULL | |
943 | env[0] | |
944 | env[1..N] | |
945 | NULL | |
946 | auxvt[0].type -- ELF Auxiliary Vector Table | |
947 | auxvt[0].value | |
948 | auxvt[1..N] | |
949 | AT_NULL | |
950 | 0 | |
951 | argv[0..N][0..M] -- actual argv/env strings | |
952 | env[0..N][0..M] | |
953 | FDPIC loadmaps -- for FDPIC apps | |
954 | So set things up the same way. */ | |
955 | int i, argc, envc; | |
956 | bu32 argv_flat, env_flat; | |
957 | ||
958 | bu32 sp, sp_flat; | |
959 | ||
960 | /* start, at_phdr, at_phnum, at_base, at_entry, pt_dynamic */ | |
961 | bu32 elf_addrs[6]; | |
e4967d72 | 962 | bu32 auxvt; |
ef016f83 MF |
963 | bu32 exec_loadmap, ldso_loadmap; |
964 | char *ldso_path; | |
965 | ||
966 | unsigned char null[4] = { 0, 0, 0, 0 }; | |
967 | ||
968 | host_callback *cb = STATE_CALLBACK (sd); | |
969 | ||
970 | elf_addrs[0] = elf_addrs[4] = bfd_get_start_address (abfd); | |
971 | elf_addrs[1] = elf_addrs[2] = elf_addrs[3] = elf_addrs[5] = 0; | |
972 | ||
973 | /* Keep the load addresses consistent between runs. Also make sure we make | |
974 | space for the fixed code region (part of the Blackfin Linux ABI). */ | |
975 | fdpic_load_offset = 0x1000; | |
976 | ||
977 | /* First try to load this as an FDPIC executable. */ | |
978 | sp = SPREG; | |
979 | if (!bfin_fdpic_load (sd, cpu, STATE_PROG_BFD (sd), &sp, elf_addrs, &ldso_path)) | |
980 | goto skip_fdpic_init; | |
981 | exec_loadmap = sp; | |
982 | ||
983 | /* If that worked, then load the fixed code region. We only do this for | |
984 | FDPIC ELFs atm because they are PIEs and let us relocate them without | |
985 | manual fixups. FLAT files however require location processing which | |
986 | we do not do ourselves, and they link with a VMA of 0. */ | |
987 | sim_write (sd, 0x400, bfin_linux_fixed_code, sizeof (bfin_linux_fixed_code)); | |
988 | ||
989 | /* If the FDPIC needs an interpreter, then load it up too. */ | |
990 | if (ldso_path) | |
991 | { | |
992 | const char *ldso_full_path = concat (simulator_sysroot, ldso_path, NULL); | |
993 | struct bfd *ldso_bfd; | |
994 | ||
995 | ldso_bfd = bfd_openr (ldso_full_path, STATE_TARGET (sd)); | |
996 | if (!ldso_bfd) | |
997 | { | |
998 | sim_io_eprintf (sd, "bfin-sim: bfd open failed: %s\n", ldso_full_path); | |
999 | goto static_fdpic; | |
1000 | } | |
1001 | if (!bfd_check_format (ldso_bfd, bfd_object)) | |
1002 | sim_io_eprintf (sd, "bfin-sim: bfd format not valid: %s\n", ldso_full_path); | |
1003 | bfd_set_arch_info (ldso_bfd, STATE_ARCHITECTURE (sd)); | |
1004 | ||
1005 | if (!bfin_fdpic_load (sd, cpu, ldso_bfd, &sp, elf_addrs, &ldso_path)) | |
1006 | sim_io_eprintf (sd, "bfin-sim: FDPIC ldso failed to load: %s\n", ldso_full_path); | |
1007 | if (ldso_path) | |
1008 | sim_io_eprintf (sd, "bfin-sim: FDPIC ldso (%s) needs an interpreter (%s) !?\n", | |
1009 | ldso_full_path, ldso_path); | |
1010 | ||
1011 | ldso_loadmap = sp; | |
1012 | } | |
1013 | else | |
1014 | static_fdpic: | |
1015 | ldso_loadmap = 0; | |
1016 | ||
1017 | /* Finally setup the registers required by the FDPIC ABI. */ | |
1018 | SET_DREG (7, 0); /* Zero out FINI funcptr -- ldso will set this up. */ | |
1019 | SET_PREG (0, exec_loadmap); /* Exec loadmap addr. */ | |
1020 | SET_PREG (1, ldso_loadmap); /* Interp loadmap addr. */ | |
1021 | SET_PREG (2, elf_addrs[5]); /* PT_DYNAMIC map addr. */ | |
1022 | ||
1023 | auxvt = 1; | |
1024 | SET_SPREG (sp); | |
1025 | skip_fdpic_init: | |
1026 | sim_pc_set (cpu, elf_addrs[0]); | |
1027 | ||
1028 | /* Figure out how much storage the argv/env strings need. */ | |
34fed699 | 1029 | argc = countargv ((char **)argv); |
ef016f83 MF |
1030 | if (argc == -1) |
1031 | argc = 0; | |
1032 | argv_flat = argc; /* NUL bytes */ | |
1033 | for (i = 0; i < argc; ++i) | |
1034 | argv_flat += strlen (argv[i]); | |
1035 | ||
1036 | if (!env) | |
1037 | env = simple_env; | |
34fed699 | 1038 | envc = countargv ((char **)env); |
ef016f83 MF |
1039 | env_flat = envc; /* NUL bytes */ |
1040 | for (i = 0; i < envc; ++i) | |
1041 | env_flat += strlen (env[i]); | |
1042 | ||
1043 | /* Push the Auxiliary Vector Table between argv/env and actual strings. */ | |
0ede24f2 | 1044 | sp_flat = sp = align_up (SPREG - argv_flat - env_flat - 4, 4); |
ef016f83 MF |
1045 | if (auxvt) |
1046 | { | |
1047 | # define AT_PUSH(at, val) \ | |
1048 | auxvt_size += 8; \ | |
1049 | sp -= 4; \ | |
1050 | auxvt = (val); \ | |
1051 | sim_write (sd, sp, (void *)&auxvt, 4); \ | |
1052 | sp -= 4; \ | |
1053 | auxvt = (at); \ | |
1054 | sim_write (sd, sp, (void *)&auxvt, 4) | |
ef016f83 MF |
1055 | unsigned int egid = getegid (), gid = getgid (); |
1056 | unsigned int euid = geteuid (), uid = getuid (); | |
e4967d72 | 1057 | bu32 auxvt_size = 0; |
ef016f83 MF |
1058 | AT_PUSH (AT_NULL, 0); |
1059 | AT_PUSH (AT_SECURE, egid != gid || euid != uid); | |
1060 | AT_PUSH (AT_EGID, egid); | |
1061 | AT_PUSH (AT_GID, gid); | |
1062 | AT_PUSH (AT_EUID, euid); | |
1063 | AT_PUSH (AT_UID, uid); | |
1064 | AT_PUSH (AT_ENTRY, elf_addrs[4]); | |
1065 | AT_PUSH (AT_FLAGS, 0); | |
1066 | AT_PUSH (AT_BASE, elf_addrs[3]); | |
1067 | AT_PUSH (AT_PHNUM, elf_addrs[2]); | |
1068 | AT_PUSH (AT_PHENT, sizeof (Elf32_External_Phdr)); | |
1069 | AT_PUSH (AT_PHDR, elf_addrs[1]); | |
1070 | AT_PUSH (AT_CLKTCK, 100); /* XXX: This ever not 100 ? */ | |
1071 | AT_PUSH (AT_PAGESZ, 4096); | |
1072 | AT_PUSH (AT_HWCAP, 0); | |
1073 | #undef AT_PUSH | |
1074 | } | |
1075 | SET_SPREG (sp); | |
1076 | ||
1077 | /* Push the argc/argv/env after the auxvt. */ | |
1078 | sp -= ((1 + argc + 1 + envc + 1) * 4); | |
1079 | SET_SPREG (sp); | |
1080 | ||
1081 | /* First push the argc value. */ | |
1082 | sim_write (sd, sp, (void *)&argc, 4); | |
1083 | sp += 4; | |
1084 | ||
1085 | /* Then the actual argv strings so we know where to point argv[]. */ | |
1086 | for (i = 0; i < argc; ++i) | |
1087 | { | |
1088 | unsigned len = strlen (argv[i]) + 1; | |
1089 | sim_write (sd, sp_flat, (void *)argv[i], len); | |
1090 | sim_write (sd, sp, (void *)&sp_flat, 4); | |
1091 | sp_flat += len; | |
1092 | sp += 4; | |
1093 | } | |
1094 | sim_write (sd, sp, null, 4); | |
1095 | sp += 4; | |
1096 | ||
1097 | /* Then the actual env strings so we know where to point env[]. */ | |
1098 | for (i = 0; i < envc; ++i) | |
1099 | { | |
1100 | unsigned len = strlen (env[i]) + 1; | |
1101 | sim_write (sd, sp_flat, (void *)env[i], len); | |
1102 | sim_write (sd, sp, (void *)&sp_flat, 4); | |
1103 | sp_flat += len; | |
1104 | sp += 4; | |
1105 | } | |
1106 | ||
1107 | /* Set some callbacks. */ | |
1108 | cb->syscall_map = cb_linux_syscall_map; | |
1109 | cb->errno_map = cb_linux_errno_map; | |
1110 | cb->open_map = cb_linux_open_map; | |
1111 | cb->signal_map = cb_linux_signal_map; | |
a931db6a MF |
1112 | cb->stat_map = stat_map_32 = cb_linux_stat_map_32; |
1113 | stat_map_64 = cb_linux_stat_map_64; | |
ef016f83 MF |
1114 | } |
1115 | ||
1116 | static void | |
2e3d4f4d | 1117 | bfin_os_init (SIM_DESC sd, SIM_CPU *cpu, char * const *argv) |
ef016f83 MF |
1118 | { |
1119 | /* Pass the command line via a string in R0 like Linux expects. */ | |
1120 | int i; | |
1121 | bu8 byte; | |
1122 | bu32 cmdline = BFIN_L1_SRAM_SCRATCH; | |
1123 | ||
1124 | SET_DREG (0, cmdline); | |
1125 | if (argv && argv[0]) | |
1126 | { | |
1127 | i = 1; | |
1128 | byte = ' '; | |
1129 | while (argv[i]) | |
1130 | { | |
1131 | bu32 len = strlen (argv[i]); | |
1132 | sim_write (sd, cmdline, (void *)argv[i], len); | |
1133 | cmdline += len; | |
1134 | sim_write (sd, cmdline, &byte, 1); | |
1135 | ++cmdline; | |
1136 | ++i; | |
1137 | } | |
1138 | } | |
1139 | byte = 0; | |
1140 | sim_write (sd, cmdline, &byte, 1); | |
1141 | } | |
1142 | ||
a931db6a MF |
1143 | static void |
1144 | bfin_virtual_init (SIM_DESC sd, SIM_CPU *cpu) | |
1145 | { | |
1146 | host_callback *cb = STATE_CALLBACK (sd); | |
1147 | ||
1148 | cb->stat_map = stat_map_32 = cb_libgloss_stat_map_32; | |
1149 | stat_map_64 = NULL; | |
1150 | } | |
1151 | ||
ef016f83 MF |
1152 | SIM_RC |
1153 | sim_create_inferior (SIM_DESC sd, struct bfd *abfd, | |
2e3d4f4d | 1154 | char * const *argv, char * const *env) |
ef016f83 MF |
1155 | { |
1156 | SIM_CPU *cpu = STATE_CPU (sd, 0); | |
1157 | SIM_ADDR addr; | |
1158 | ||
1159 | /* Set the PC. */ | |
1160 | if (abfd != NULL) | |
1161 | addr = bfd_get_start_address (abfd); | |
1162 | else | |
1163 | addr = 0; | |
1164 | sim_pc_set (cpu, addr); | |
1165 | ||
0e967299 MF |
1166 | /* Standalone mode (i.e. `run`) will take care of the argv for us in |
1167 | sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' | |
1168 | with `gdb`), we need to handle it because the user can change the | |
1169 | argv on the fly via gdb's 'run'. */ | |
1170 | if (STATE_PROG_ARGV (sd) != argv) | |
ef016f83 | 1171 | { |
886ea33b | 1172 | freeargv (STATE_PROG_ARGV (sd)); |
ef016f83 MF |
1173 | STATE_PROG_ARGV (sd) = dupargv (argv); |
1174 | } | |
1175 | ||
1176 | switch (STATE_ENVIRONMENT (sd)) | |
1177 | { | |
1178 | case USER_ENVIRONMENT: | |
2e3d4f4d | 1179 | bfin_user_init (sd, cpu, abfd, argv, env); |
ef016f83 MF |
1180 | break; |
1181 | case OPERATING_ENVIRONMENT: | |
2e3d4f4d | 1182 | bfin_os_init (sd, cpu, argv); |
ef016f83 MF |
1183 | break; |
1184 | default: | |
a931db6a | 1185 | bfin_virtual_init (sd, cpu); |
ef016f83 MF |
1186 | break; |
1187 | } | |
1188 | ||
1189 | return SIM_RC_OK; | |
1190 | } |