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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 #include <sys/types.h>
33 #if defined(CONFIG_USER_ONLY)
35 #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
37 #include "qemu-common.h"
40 #include "softmmu-semi.h"
44 #define HOSTED_INIT_SIM 1
46 #define HOSTED_CLOSE 3
48 #define HOSTED_WRITE 5
49 #define HOSTED_LSEEK 6
50 #define HOSTED_RENAME 7
51 #define HOSTED_UNLINK 8
53 #define HOSTED_FSTAT 10
54 #define HOSTED_GETTIMEOFDAY 11
55 #define HOSTED_ISATTY 12
56 #define HOSTED_SYSTEM 13
58 typedef uint32_t gdb_mode_t
;
59 typedef uint32_t gdb_time_t
;
61 struct m68k_gdb_stat
{
62 uint32_t gdb_st_dev
; /* device */
63 uint32_t gdb_st_ino
; /* inode */
64 gdb_mode_t gdb_st_mode
; /* protection */
65 uint32_t gdb_st_nlink
; /* number of hard links */
66 uint32_t gdb_st_uid
; /* user ID of owner */
67 uint32_t gdb_st_gid
; /* group ID of owner */
68 uint32_t gdb_st_rdev
; /* device type (if inode device) */
69 uint64_t gdb_st_size
; /* total size, in bytes */
70 uint64_t gdb_st_blksize
; /* blocksize for filesystem I/O */
71 uint64_t gdb_st_blocks
; /* number of blocks allocated */
72 gdb_time_t gdb_st_atime
; /* time of last access */
73 gdb_time_t gdb_st_mtime
; /* time of last modification */
74 gdb_time_t gdb_st_ctime
; /* time of last change */
75 } __attribute__((packed
));
78 gdb_time_t tv_sec
; /* second */
79 uint64_t tv_usec
; /* microsecond */
80 } __attribute__((packed
));
82 #define GDB_O_RDONLY 0x0
83 #define GDB_O_WRONLY 0x1
84 #define GDB_O_RDWR 0x2
85 #define GDB_O_APPEND 0x8
86 #define GDB_O_CREAT 0x200
87 #define GDB_O_TRUNC 0x400
88 #define GDB_O_EXCL 0x800
90 static int translate_openflags(int flags
)
94 if (flags
& GDB_O_WRONLY
)
96 else if (flags
& GDB_O_RDWR
)
101 if (flags
& GDB_O_APPEND
) hf
|= O_APPEND
;
102 if (flags
& GDB_O_CREAT
) hf
|= O_CREAT
;
103 if (flags
& GDB_O_TRUNC
) hf
|= O_TRUNC
;
104 if (flags
& GDB_O_EXCL
) hf
|= O_EXCL
;
109 static void translate_stat(CPUState
*env
, target_ulong addr
, struct stat
*s
)
111 struct m68k_gdb_stat
*p
;
113 if (!(p
= lock_user(VERIFY_WRITE
, addr
, sizeof(struct m68k_gdb_stat
), 0)))
114 /* FIXME - should this return an error code? */
116 p
->gdb_st_dev
= cpu_to_be32(s
->st_dev
);
117 p
->gdb_st_ino
= cpu_to_be32(s
->st_ino
);
118 p
->gdb_st_mode
= cpu_to_be32(s
->st_mode
);
119 p
->gdb_st_nlink
= cpu_to_be32(s
->st_nlink
);
120 p
->gdb_st_uid
= cpu_to_be32(s
->st_uid
);
121 p
->gdb_st_gid
= cpu_to_be32(s
->st_gid
);
122 p
->gdb_st_rdev
= cpu_to_be32(s
->st_rdev
);
123 p
->gdb_st_size
= cpu_to_be64(s
->st_size
);
125 /* Windows stat is missing some fields. */
126 p
->gdb_st_blksize
= 0;
127 p
->gdb_st_blocks
= 0;
129 p
->gdb_st_blksize
= cpu_to_be64(s
->st_blksize
);
130 p
->gdb_st_blocks
= cpu_to_be64(s
->st_blocks
);
132 p
->gdb_st_atime
= cpu_to_be32(s
->st_atime
);
133 p
->gdb_st_mtime
= cpu_to_be32(s
->st_mtime
);
134 p
->gdb_st_ctime
= cpu_to_be32(s
->st_ctime
);
135 unlock_user(p
, addr
, sizeof(struct m68k_gdb_stat
));
138 static int m68k_semi_is_fseek
;
140 static void m68k_semi_cb(CPUState
*env
, target_ulong ret
, target_ulong err
)
144 args
= env
->dregs
[1];
145 if (m68k_semi_is_fseek
) {
146 /* FIXME: We've already lost the high bits of the fseek
148 /* FIXME - handle put_user() failure */
149 put_user_u32(0, args
);
151 m68k_semi_is_fseek
= 0;
153 /* FIXME - handle put_user() failure */
154 put_user_u32(ret
, args
);
155 put_user_u32(errno
, args
+ 4);
160 target_ulong __arg; \
161 /* FIXME - handle get_user() failure */ \
162 get_user_ual(__arg, args + (n) * 4); \
165 #define PARG(x) ((unsigned long)ARG(x))
166 void do_m68k_semihosting(CPUM68KState
*env
, int nr
)
174 args
= env
->dregs
[1];
179 if (use_gdb_syscalls()) {
180 gdb_do_syscall(m68k_semi_cb
, "open,%s,%x,%x", ARG(0), (int)ARG(1),
184 if (!(p
= lock_user_string(ARG(0)))) {
185 /* FIXME - check error code? */
188 result
= open(p
, translate_openflags(ARG(2)), ARG(3));
189 unlock_user(p
, ARG(0), 0);
195 /* Ignore attempts to close stdin/out/err. */
198 if (use_gdb_syscalls()) {
199 gdb_do_syscall(m68k_semi_cb
, "close,%x", ARG(0));
211 if (use_gdb_syscalls()) {
212 gdb_do_syscall(m68k_semi_cb
, "read,%x,%x,%x",
213 ARG(0), ARG(1), len
);
216 if (!(p
= lock_user(VERIFY_WRITE
, ARG(1), len
, 0))) {
217 /* FIXME - check error code? */
220 result
= read(ARG(0), p
, len
);
221 unlock_user(p
, ARG(1), len
);
227 if (use_gdb_syscalls()) {
228 gdb_do_syscall(m68k_semi_cb
, "write,%x,%x,%x",
229 ARG(0), ARG(1), len
);
232 if (!(p
= lock_user(VERIFY_READ
, ARG(1), len
, 1))) {
233 /* FIXME - check error code? */
236 result
= write(ARG(0), p
, len
);
237 unlock_user(p
, ARG(0), 0);
244 off
= (uint32_t)ARG(2) | ((uint64_t)ARG(1) << 32);
245 if (use_gdb_syscalls()) {
246 m68k_semi_is_fseek
= 1;
247 gdb_do_syscall(m68k_semi_cb
, "fseek,%x,%lx,%x",
248 ARG(0), off
, ARG(3));
250 off
= lseek(ARG(0), off
, ARG(3));
251 /* FIXME - handle put_user() failure */
252 put_user_u32(off
>> 32, args
);
253 put_user_u32(off
, args
+ 4);
254 put_user_u32(errno
, args
+ 8);
259 if (use_gdb_syscalls()) {
260 gdb_do_syscall(m68k_semi_cb
, "rename,%s,%s",
261 ARG(0), (int)ARG(1), ARG(2), (int)ARG(3));
264 p
= lock_user_string(ARG(0));
265 q
= lock_user_string(ARG(2));
267 /* FIXME - check error code? */
270 result
= rename(p
, q
);
272 unlock_user(p
, ARG(0), 0);
273 unlock_user(q
, ARG(2), 0);
277 if (use_gdb_syscalls()) {
278 gdb_do_syscall(m68k_semi_cb
, "unlink,%s",
279 ARG(0), (int)ARG(1));
282 if (!(p
= lock_user_string(ARG(0)))) {
283 /* FIXME - check error code? */
287 unlock_user(p
, ARG(0), 0);
292 if (use_gdb_syscalls()) {
293 gdb_do_syscall(m68k_semi_cb
, "stat,%s,%x",
294 ARG(0), (int)ARG(1), ARG(2));
298 if (!(p
= lock_user_string(ARG(0)))) {
299 /* FIXME - check error code? */
302 result
= stat(p
, &s
);
303 unlock_user(p
, ARG(0), 0);
306 translate_stat(env
, ARG(2), &s
);
311 if (use_gdb_syscalls()) {
312 gdb_do_syscall(m68k_semi_cb
, "fstat,%x,%x",
317 result
= fstat(ARG(0), &s
);
319 translate_stat(env
, ARG(1), &s
);
323 case HOSTED_GETTIMEOFDAY
:
324 if (use_gdb_syscalls()) {
325 gdb_do_syscall(m68k_semi_cb
, "gettimeofday,%x,%x",
330 struct gdb_timeval
*p
;
331 result
= qemu_gettimeofday(&tv
);
333 if (!(p
= lock_user(VERIFY_WRITE
,
334 ARG(0), sizeof(struct gdb_timeval
), 0))) {
335 /* FIXME - check error code? */
338 p
->tv_sec
= cpu_to_be32(tv
.tv_sec
);
339 p
->tv_usec
= cpu_to_be64(tv
.tv_usec
);
340 unlock_user(p
, ARG(0), sizeof(struct gdb_timeval
));
346 if (use_gdb_syscalls()) {
347 gdb_do_syscall(m68k_semi_cb
, "isatty,%x", ARG(0));
350 result
= isatty(ARG(0));
354 if (use_gdb_syscalls()) {
355 gdb_do_syscall(m68k_semi_cb
, "system,%s",
356 ARG(0), (int)ARG(1));
359 if (!(p
= lock_user_string(ARG(0)))) {
360 /* FIXME - check error code? */
364 unlock_user(p
, ARG(0), 0);
368 case HOSTED_INIT_SIM
:
369 #if defined(CONFIG_USER_ONLY)
371 TaskState
*ts
= env
->opaque
;
372 /* Allocate the heap using sbrk. */
373 if (!ts
->heap_limit
) {
379 size
= SEMIHOSTING_HEAP_SIZE
;
380 /* Try a big heap, and reduce the size if that fails. */
382 ret
= do_brk(base
+ size
);
387 ts
->heap_limit
= base
+ size
;
389 /* This call may happen before we have writable memory, so return
390 values directly in registers. */
391 env
->dregs
[1] = ts
->heap_limit
;
392 env
->aregs
[7] = ts
->stack_base
;
395 /* FIXME: This is wrong for boards where RAM does not start at
397 env
->dregs
[1] = ram_size
;
398 env
->aregs
[7] = ram_size
;
402 cpu_abort(env
, "Unsupported semihosting syscall %d\n", nr
);
405 /* FIXME - handle put_user() failure */
406 put_user_u32(result
, args
);
407 put_user_u32(errno
, args
+ 4);