2 * m68k/ColdFire Semihosting syscall interface
4 * Copyright (c) 2005-2007 CodeSourcery.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
31 #if defined(CONFIG_USER_ONLY)
33 #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
35 #include "qemu-common.h"
36 #include "exec/gdbstub.h"
37 #include "exec/softmmu-semi.h"
39 #include "sysemu/sysemu.h"
42 #define HOSTED_INIT_SIM 1
44 #define HOSTED_CLOSE 3
46 #define HOSTED_WRITE 5
47 #define HOSTED_LSEEK 6
48 #define HOSTED_RENAME 7
49 #define HOSTED_UNLINK 8
51 #define HOSTED_FSTAT 10
52 #define HOSTED_GETTIMEOFDAY 11
53 #define HOSTED_ISATTY 12
54 #define HOSTED_SYSTEM 13
56 typedef uint32_t gdb_mode_t
;
57 typedef uint32_t gdb_time_t
;
59 struct m68k_gdb_stat
{
60 uint32_t gdb_st_dev
; /* device */
61 uint32_t gdb_st_ino
; /* inode */
62 gdb_mode_t gdb_st_mode
; /* protection */
63 uint32_t gdb_st_nlink
; /* number of hard links */
64 uint32_t gdb_st_uid
; /* user ID of owner */
65 uint32_t gdb_st_gid
; /* group ID of owner */
66 uint32_t gdb_st_rdev
; /* device type (if inode device) */
67 uint64_t gdb_st_size
; /* total size, in bytes */
68 uint64_t gdb_st_blksize
; /* blocksize for filesystem I/O */
69 uint64_t gdb_st_blocks
; /* number of blocks allocated */
70 gdb_time_t gdb_st_atime
; /* time of last access */
71 gdb_time_t gdb_st_mtime
; /* time of last modification */
72 gdb_time_t gdb_st_ctime
; /* time of last change */
76 gdb_time_t tv_sec
; /* second */
77 uint64_t tv_usec
; /* microsecond */
80 #define GDB_O_RDONLY 0x0
81 #define GDB_O_WRONLY 0x1
82 #define GDB_O_RDWR 0x2
83 #define GDB_O_APPEND 0x8
84 #define GDB_O_CREAT 0x200
85 #define GDB_O_TRUNC 0x400
86 #define GDB_O_EXCL 0x800
88 static int translate_openflags(int flags
)
92 if (flags
& GDB_O_WRONLY
)
94 else if (flags
& GDB_O_RDWR
)
99 if (flags
& GDB_O_APPEND
) hf
|= O_APPEND
;
100 if (flags
& GDB_O_CREAT
) hf
|= O_CREAT
;
101 if (flags
& GDB_O_TRUNC
) hf
|= O_TRUNC
;
102 if (flags
& GDB_O_EXCL
) hf
|= O_EXCL
;
107 static void translate_stat(CPUM68KState
*env
, target_ulong addr
, struct stat
*s
)
109 struct m68k_gdb_stat
*p
;
111 if (!(p
= lock_user(VERIFY_WRITE
, addr
, sizeof(struct m68k_gdb_stat
), 0)))
112 /* FIXME - should this return an error code? */
114 p
->gdb_st_dev
= cpu_to_be32(s
->st_dev
);
115 p
->gdb_st_ino
= cpu_to_be32(s
->st_ino
);
116 p
->gdb_st_mode
= cpu_to_be32(s
->st_mode
);
117 p
->gdb_st_nlink
= cpu_to_be32(s
->st_nlink
);
118 p
->gdb_st_uid
= cpu_to_be32(s
->st_uid
);
119 p
->gdb_st_gid
= cpu_to_be32(s
->st_gid
);
120 p
->gdb_st_rdev
= cpu_to_be32(s
->st_rdev
);
121 p
->gdb_st_size
= cpu_to_be64(s
->st_size
);
123 /* Windows stat is missing some fields. */
124 p
->gdb_st_blksize
= 0;
125 p
->gdb_st_blocks
= 0;
127 p
->gdb_st_blksize
= cpu_to_be64(s
->st_blksize
);
128 p
->gdb_st_blocks
= cpu_to_be64(s
->st_blocks
);
130 p
->gdb_st_atime
= cpu_to_be32(s
->st_atime
);
131 p
->gdb_st_mtime
= cpu_to_be32(s
->st_mtime
);
132 p
->gdb_st_ctime
= cpu_to_be32(s
->st_ctime
);
133 unlock_user(p
, addr
, sizeof(struct m68k_gdb_stat
));
136 static void m68k_semi_return_u32(CPUM68KState
*env
, uint32_t ret
, uint32_t err
)
138 target_ulong args
= env
->dregs
[1];
139 if (put_user_u32(ret
, args
) ||
140 put_user_u32(err
, args
+ 4)) {
141 /* The m68k semihosting ABI does not provide any way to report this
142 * error to the guest, so the best we can do is log it in qemu.
143 * It is always a guest error not to pass us a valid argument block.
145 qemu_log_mask(LOG_GUEST_ERROR
, "m68k-semihosting: return value "
146 "discarded because argument block not writable\n");
150 static void m68k_semi_return_u64(CPUM68KState
*env
, uint64_t ret
, uint32_t err
)
152 target_ulong args
= env
->dregs
[1];
153 if (put_user_u32(ret
>> 32, args
) ||
154 put_user_u32(ret
, args
+ 4) ||
155 put_user_u32(err
, args
+ 8)) {
156 /* No way to report this via m68k semihosting ABI; just log it */
157 qemu_log_mask(LOG_GUEST_ERROR
, "m68k-semihosting: return value "
158 "discarded because argument block not writable\n");
162 static int m68k_semi_is_fseek
;
164 static void m68k_semi_cb(CPUM68KState
*env
, target_ulong ret
, target_ulong err
)
166 if (m68k_semi_is_fseek
) {
167 /* FIXME: We've already lost the high bits of the fseek
169 m68k_semi_return_u64(env
, ret
, err
);
170 m68k_semi_is_fseek
= 0;
172 m68k_semi_return_u32(env
, ret
, err
);
176 /* Read the input value from the argument block; fail the semihosting
177 * call if the memory read fails.
179 #define GET_ARG(n) do { \
180 if (get_user_ual(arg ## n, args + (n) * 4)) { \
187 void do_m68k_semihosting(CPUM68KState
*env
, int nr
)
190 target_ulong arg0
, arg1
, arg2
, arg3
;
196 args
= env
->dregs
[1];
199 gdb_exit(env
, env
->dregs
[0]);
206 if (use_gdb_syscalls()) {
207 gdb_do_syscall(m68k_semi_cb
, "open,%s,%x,%x", arg0
, (int)arg1
,
211 p
= lock_user_string(arg0
);
213 /* FIXME - check error code? */
216 result
= open(p
, translate_openflags(arg2
), arg3
);
217 unlock_user(p
, arg0
, 0);
223 /* Ignore attempts to close stdin/out/err. */
227 if (use_gdb_syscalls()) {
228 gdb_do_syscall(m68k_semi_cb
, "close,%x", arg0
);
243 if (use_gdb_syscalls()) {
244 gdb_do_syscall(m68k_semi_cb
, "read,%x,%x,%x",
248 p
= lock_user(VERIFY_WRITE
, arg1
, len
, 0);
250 /* FIXME - check error code? */
253 result
= read(arg0
, p
, len
);
254 unlock_user(p
, arg1
, len
);
263 if (use_gdb_syscalls()) {
264 gdb_do_syscall(m68k_semi_cb
, "write,%x,%x,%x",
268 p
= lock_user(VERIFY_READ
, arg1
, len
, 1);
270 /* FIXME - check error code? */
273 result
= write(arg0
, p
, len
);
274 unlock_user(p
, arg0
, 0);
285 off
= (uint32_t)arg2
| ((uint64_t)arg1
<< 32);
286 if (use_gdb_syscalls()) {
287 m68k_semi_is_fseek
= 1;
288 gdb_do_syscall(m68k_semi_cb
, "fseek,%x,%lx,%x",
291 off
= lseek(arg0
, off
, arg3
);
292 m68k_semi_return_u64(env
, off
, errno
);
301 if (use_gdb_syscalls()) {
302 gdb_do_syscall(m68k_semi_cb
, "rename,%s,%s",
303 arg0
, (int)arg1
, arg2
, (int)arg3
);
306 p
= lock_user_string(arg0
);
307 q
= lock_user_string(arg2
);
309 /* FIXME - check error code? */
312 result
= rename(p
, q
);
314 unlock_user(p
, arg0
, 0);
315 unlock_user(q
, arg2
, 0);
321 if (use_gdb_syscalls()) {
322 gdb_do_syscall(m68k_semi_cb
, "unlink,%s",
326 p
= lock_user_string(arg0
);
328 /* FIXME - check error code? */
332 unlock_user(p
, arg0
, 0);
340 if (use_gdb_syscalls()) {
341 gdb_do_syscall(m68k_semi_cb
, "stat,%s,%x",
342 arg0
, (int)arg1
, arg2
);
346 p
= lock_user_string(arg0
);
348 /* FIXME - check error code? */
351 result
= stat(p
, &s
);
352 unlock_user(p
, arg0
, 0);
355 translate_stat(env
, arg2
, &s
);
362 if (use_gdb_syscalls()) {
363 gdb_do_syscall(m68k_semi_cb
, "fstat,%x,%x",
368 result
= fstat(arg0
, &s
);
370 translate_stat(env
, arg1
, &s
);
374 case HOSTED_GETTIMEOFDAY
:
377 if (use_gdb_syscalls()) {
378 gdb_do_syscall(m68k_semi_cb
, "gettimeofday,%x,%x",
383 struct gdb_timeval
*p
;
384 result
= qemu_gettimeofday(&tv
);
386 if (!(p
= lock_user(VERIFY_WRITE
,
387 arg0
, sizeof(struct gdb_timeval
), 0))) {
388 /* FIXME - check error code? */
391 p
->tv_sec
= cpu_to_be32(tv
.tv_sec
);
392 p
->tv_usec
= cpu_to_be64(tv
.tv_usec
);
393 unlock_user(p
, arg0
, sizeof(struct gdb_timeval
));
400 if (use_gdb_syscalls()) {
401 gdb_do_syscall(m68k_semi_cb
, "isatty,%x", arg0
);
404 result
= isatty(arg0
);
410 if (use_gdb_syscalls()) {
411 gdb_do_syscall(m68k_semi_cb
, "system,%s",
415 p
= lock_user_string(arg0
);
417 /* FIXME - check error code? */
421 unlock_user(p
, arg0
, 0);
425 case HOSTED_INIT_SIM
:
426 #if defined(CONFIG_USER_ONLY)
428 TaskState
*ts
= env
->opaque
;
429 /* Allocate the heap using sbrk. */
430 if (!ts
->heap_limit
) {
436 size
= SEMIHOSTING_HEAP_SIZE
;
437 /* Try a big heap, and reduce the size if that fails. */
439 ret
= do_brk(base
+ size
);
440 if (ret
>= (base
+ size
)) {
445 ts
->heap_limit
= base
+ size
;
447 /* This call may happen before we have writable memory, so return
448 values directly in registers. */
449 env
->dregs
[1] = ts
->heap_limit
;
450 env
->aregs
[7] = ts
->stack_base
;
453 /* FIXME: This is wrong for boards where RAM does not start at
455 env
->dregs
[1] = ram_size
;
456 env
->aregs
[7] = ram_size
;
460 cpu_abort(env
, "Unsupported semihosting syscall %d\n", nr
);
464 m68k_semi_return_u32(env
, result
, errno
);