2 * Syscall implementations for semihosting.
4 * Copyright (c) 2022 Linaro
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "qemu/osdep.h"
11 #include "gdbstub/syscalls.h"
12 #include "semihosting/guestfd.h"
13 #include "semihosting/syscalls.h"
14 #include "semihosting/console.h"
15 #ifdef CONFIG_USER_ONLY
18 #include "semihosting/softmmu-uaccess.h"
23 * Validate or compute the length of the string (including terminator).
25 static int validate_strlen(CPUState
*cs
, target_ulong str
, target_ulong tlen
)
27 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
31 ssize_t slen
= target_strlen(str
);
36 if (slen
>= INT32_MAX
) {
41 if (tlen
> INT32_MAX
) {
44 if (get_user_u8(c
, str
+ tlen
- 1)) {
53 static int validate_lock_user_string(char **pstr
, CPUState
*cs
,
54 target_ulong tstr
, target_ulong tlen
)
56 int ret
= validate_strlen(cs
, tstr
, tlen
);
57 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
61 str
= lock_user(VERIFY_READ
, tstr
, ret
, true);
62 ret
= str
? 0 : -EFAULT
;
69 * TODO: Note that gdb always stores the stat structure big-endian.
70 * So far, that's ok, as the only two targets using this are also
71 * big-endian. Until we do something with gdb, also produce the
72 * same big-endian result from the host.
74 static int copy_stat_to_user(CPUState
*cs
, target_ulong addr
,
77 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
80 if (s
->st_dev
!= (uint32_t)s
->st_dev
||
81 s
->st_ino
!= (uint32_t)s
->st_ino
) {
85 p
= lock_user(VERIFY_WRITE
, addr
, sizeof(struct gdb_stat
), 0);
90 p
->gdb_st_dev
= cpu_to_be32(s
->st_dev
);
91 p
->gdb_st_ino
= cpu_to_be32(s
->st_ino
);
92 p
->gdb_st_mode
= cpu_to_be32(s
->st_mode
);
93 p
->gdb_st_nlink
= cpu_to_be32(s
->st_nlink
);
94 p
->gdb_st_uid
= cpu_to_be32(s
->st_uid
);
95 p
->gdb_st_gid
= cpu_to_be32(s
->st_gid
);
96 p
->gdb_st_rdev
= cpu_to_be32(s
->st_rdev
);
97 p
->gdb_st_size
= cpu_to_be64(s
->st_size
);
99 /* Windows stat is missing some fields. */
100 p
->gdb_st_blksize
= 0;
101 p
->gdb_st_blocks
= 0;
103 p
->gdb_st_blksize
= cpu_to_be64(s
->st_blksize
);
104 p
->gdb_st_blocks
= cpu_to_be64(s
->st_blocks
);
106 p
->gdb_st_atime
= cpu_to_be32(s
->st_atime
);
107 p
->gdb_st_mtime
= cpu_to_be32(s
->st_mtime
);
108 p
->gdb_st_ctime
= cpu_to_be32(s
->st_ctime
);
110 unlock_user(p
, addr
, sizeof(struct gdb_stat
));
115 * GDB semihosting syscall implementations.
118 static gdb_syscall_complete_cb gdb_open_complete
;
120 static void gdb_open_cb(CPUState
*cs
, uint64_t ret
, int err
)
123 int guestfd
= alloc_guestfd();
124 associate_guestfd(guestfd
, ret
);
127 gdb_open_complete(cs
, ret
, err
);
130 static void gdb_open(CPUState
*cs
, gdb_syscall_complete_cb complete
,
131 target_ulong fname
, target_ulong fname_len
,
132 int gdb_flags
, int mode
)
134 int len
= validate_strlen(cs
, fname
, fname_len
);
136 complete(cs
, -1, -len
);
140 gdb_open_complete
= complete
;
141 gdb_do_syscall(gdb_open_cb
, "open,%s,%x,%x",
142 (uint64_t)fname
, (uint32_t)len
,
143 (uint32_t)gdb_flags
, (uint32_t)mode
);
146 static void gdb_close(CPUState
*cs
, gdb_syscall_complete_cb complete
,
149 gdb_do_syscall(complete
, "close,%x", (uint32_t)gf
->hostfd
);
152 static void gdb_read(CPUState
*cs
, gdb_syscall_complete_cb complete
,
153 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
155 gdb_do_syscall(complete
, "read,%x,%lx,%lx",
156 (uint32_t)gf
->hostfd
, (uint64_t)buf
, (uint64_t)len
);
159 static void gdb_write(CPUState
*cs
, gdb_syscall_complete_cb complete
,
160 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
162 gdb_do_syscall(complete
, "write,%x,%lx,%lx",
163 (uint32_t)gf
->hostfd
, (uint64_t)buf
, (uint64_t)len
);
166 static void gdb_lseek(CPUState
*cs
, gdb_syscall_complete_cb complete
,
167 GuestFD
*gf
, int64_t off
, int gdb_whence
)
169 gdb_do_syscall(complete
, "lseek,%x,%lx,%x",
170 (uint32_t)gf
->hostfd
, off
, (uint32_t)gdb_whence
);
173 static void gdb_isatty(CPUState
*cs
, gdb_syscall_complete_cb complete
,
176 gdb_do_syscall(complete
, "isatty,%x", (uint32_t)gf
->hostfd
);
179 static void gdb_fstat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
180 GuestFD
*gf
, target_ulong addr
)
182 gdb_do_syscall(complete
, "fstat,%x,%lx",
183 (uint32_t)gf
->hostfd
, (uint64_t)addr
);
186 static void gdb_stat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
187 target_ulong fname
, target_ulong fname_len
,
190 int len
= validate_strlen(cs
, fname
, fname_len
);
192 complete(cs
, -1, -len
);
196 gdb_do_syscall(complete
, "stat,%s,%lx",
197 (uint64_t)fname
, (uint32_t)len
, (uint64_t)addr
);
200 static void gdb_remove(CPUState
*cs
, gdb_syscall_complete_cb complete
,
201 target_ulong fname
, target_ulong fname_len
)
203 int len
= validate_strlen(cs
, fname
, fname_len
);
205 complete(cs
, -1, -len
);
209 gdb_do_syscall(complete
, "unlink,%s", (uint64_t)fname
, (uint32_t)len
);
212 static void gdb_rename(CPUState
*cs
, gdb_syscall_complete_cb complete
,
213 target_ulong oname
, target_ulong oname_len
,
214 target_ulong nname
, target_ulong nname_len
)
218 olen
= validate_strlen(cs
, oname
, oname_len
);
220 complete(cs
, -1, -olen
);
223 nlen
= validate_strlen(cs
, nname
, nname_len
);
225 complete(cs
, -1, -nlen
);
229 gdb_do_syscall(complete
, "rename,%s,%s",
230 (uint64_t)oname
, (uint32_t)olen
,
231 (uint64_t)nname
, (uint32_t)nlen
);
234 static void gdb_system(CPUState
*cs
, gdb_syscall_complete_cb complete
,
235 target_ulong cmd
, target_ulong cmd_len
)
237 int len
= validate_strlen(cs
, cmd
, cmd_len
);
239 complete(cs
, -1, -len
);
243 gdb_do_syscall(complete
, "system,%s", (uint64_t)cmd
, (uint32_t)len
);
246 static void gdb_gettimeofday(CPUState
*cs
, gdb_syscall_complete_cb complete
,
247 target_ulong tv_addr
, target_ulong tz_addr
)
249 gdb_do_syscall(complete
, "gettimeofday,%lx,%lx",
250 (uint64_t)tv_addr
, (uint64_t)tz_addr
);
254 * Host semihosting syscall implementations.
257 static void host_open(CPUState
*cs
, gdb_syscall_complete_cb complete
,
258 target_ulong fname
, target_ulong fname_len
,
259 int gdb_flags
, int mode
)
261 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
263 int ret
, host_flags
= O_BINARY
;
265 ret
= validate_lock_user_string(&p
, cs
, fname
, fname_len
);
267 complete(cs
, -1, -ret
);
271 if (gdb_flags
& GDB_O_WRONLY
) {
272 host_flags
|= O_WRONLY
;
273 } else if (gdb_flags
& GDB_O_RDWR
) {
274 host_flags
|= O_RDWR
;
276 host_flags
|= O_RDONLY
;
278 if (gdb_flags
& GDB_O_CREAT
) {
279 host_flags
|= O_CREAT
;
281 if (gdb_flags
& GDB_O_TRUNC
) {
282 host_flags
|= O_TRUNC
;
284 if (gdb_flags
& GDB_O_EXCL
) {
285 host_flags
|= O_EXCL
;
288 ret
= open(p
, host_flags
, mode
);
290 complete(cs
, -1, errno
);
292 int guestfd
= alloc_guestfd();
293 associate_guestfd(guestfd
, ret
);
294 complete(cs
, guestfd
, 0);
296 unlock_user(p
, fname
, 0);
299 static void host_close(CPUState
*cs
, gdb_syscall_complete_cb complete
,
303 * Only close the underlying host fd if it's one we opened on behalf
304 * of the guest in SYS_OPEN.
306 if (gf
->hostfd
!= STDIN_FILENO
&&
307 gf
->hostfd
!= STDOUT_FILENO
&&
308 gf
->hostfd
!= STDERR_FILENO
&&
309 close(gf
->hostfd
) < 0) {
310 complete(cs
, -1, errno
);
316 static void host_read(CPUState
*cs
, gdb_syscall_complete_cb complete
,
317 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
319 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
320 void *ptr
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
324 complete(cs
, -1, EFAULT
);
327 ret
= RETRY_ON_EINTR(read(gf
->hostfd
, ptr
, len
));
329 unlock_user(ptr
, buf
, 0);
330 complete(cs
, -1, errno
);
332 unlock_user(ptr
, buf
, ret
);
333 complete(cs
, ret
, 0);
337 static void host_write(CPUState
*cs
, gdb_syscall_complete_cb complete
,
338 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
340 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
341 void *ptr
= lock_user(VERIFY_READ
, buf
, len
, 1);
345 complete(cs
, -1, EFAULT
);
348 ret
= write(gf
->hostfd
, ptr
, len
);
349 unlock_user(ptr
, buf
, 0);
350 complete(cs
, ret
, ret
== -1 ? errno
: 0);
353 static void host_lseek(CPUState
*cs
, gdb_syscall_complete_cb complete
,
354 GuestFD
*gf
, int64_t off
, int whence
)
356 /* So far, all hosts use the same values. */
357 QEMU_BUILD_BUG_ON(GDB_SEEK_SET
!= SEEK_SET
);
358 QEMU_BUILD_BUG_ON(GDB_SEEK_CUR
!= SEEK_CUR
);
359 QEMU_BUILD_BUG_ON(GDB_SEEK_END
!= SEEK_END
);
365 ret
= lseek(gf
->hostfd
, ret
, whence
);
373 complete(cs
, ret
, err
);
376 static void host_isatty(CPUState
*cs
, gdb_syscall_complete_cb complete
,
379 int ret
= isatty(gf
->hostfd
);
380 complete(cs
, ret
, ret
? 0 : errno
);
383 static void host_flen(CPUState
*cs
, gdb_syscall_complete_cb complete
,
388 if (fstat(gf
->hostfd
, &buf
) < 0) {
389 complete(cs
, -1, errno
);
391 complete(cs
, buf
.st_size
, 0);
395 static void host_fstat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
396 GuestFD
*gf
, target_ulong addr
)
401 ret
= fstat(gf
->hostfd
, &buf
);
403 complete(cs
, -1, errno
);
406 ret
= copy_stat_to_user(cs
, addr
, &buf
);
407 complete(cs
, ret
? -1 : 0, ret
? -ret
: 0);
410 static void host_stat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
411 target_ulong fname
, target_ulong fname_len
,
414 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
419 ret
= validate_lock_user_string(&name
, cs
, fname
, fname_len
);
421 complete(cs
, -1, -ret
);
425 ret
= stat(name
, &buf
);
429 ret
= copy_stat_to_user(cs
, addr
, &buf
);
436 unlock_user(name
, fname
, 0);
437 complete(cs
, ret
, err
);
440 static void host_remove(CPUState
*cs
, gdb_syscall_complete_cb complete
,
441 target_ulong fname
, target_ulong fname_len
)
443 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
447 ret
= validate_lock_user_string(&p
, cs
, fname
, fname_len
);
449 complete(cs
, -1, -ret
);
454 unlock_user(p
, fname
, 0);
455 complete(cs
, ret
, ret
? errno
: 0);
458 static void host_rename(CPUState
*cs
, gdb_syscall_complete_cb complete
,
459 target_ulong oname
, target_ulong oname_len
,
460 target_ulong nname
, target_ulong nname_len
)
462 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
466 ret
= validate_lock_user_string(&ostr
, cs
, oname
, oname_len
);
468 complete(cs
, -1, -ret
);
471 ret
= validate_lock_user_string(&nstr
, cs
, nname
, nname_len
);
473 unlock_user(ostr
, oname
, 0);
474 complete(cs
, -1, -ret
);
478 ret
= rename(ostr
, nstr
);
479 unlock_user(ostr
, oname
, 0);
480 unlock_user(nstr
, nname
, 0);
481 complete(cs
, ret
, ret
? errno
: 0);
484 static void host_system(CPUState
*cs
, gdb_syscall_complete_cb complete
,
485 target_ulong cmd
, target_ulong cmd_len
)
487 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
491 ret
= validate_lock_user_string(&p
, cs
, cmd
, cmd_len
);
493 complete(cs
, -1, -ret
);
498 unlock_user(p
, cmd
, 0);
499 complete(cs
, ret
, ret
== -1 ? errno
: 0);
502 static void host_gettimeofday(CPUState
*cs
, gdb_syscall_complete_cb complete
,
503 target_ulong tv_addr
, target_ulong tz_addr
)
505 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
506 struct gdb_timeval
*p
;
509 /* GDB fails on non-null TZ, so be consistent. */
511 complete(cs
, -1, EINVAL
);
515 p
= lock_user(VERIFY_WRITE
, tv_addr
, sizeof(struct gdb_timeval
), 0);
517 complete(cs
, -1, EFAULT
);
521 /* TODO: Like stat, gdb always produces big-endian results; match it. */
522 rt
= g_get_real_time();
523 p
->tv_sec
= cpu_to_be32(rt
/ G_USEC_PER_SEC
);
524 p
->tv_usec
= cpu_to_be64(rt
% G_USEC_PER_SEC
);
525 unlock_user(p
, tv_addr
, sizeof(struct gdb_timeval
));
528 #ifndef CONFIG_USER_ONLY
529 static void host_poll_one(CPUState
*cs
, gdb_syscall_complete_cb complete
,
530 GuestFD
*gf
, GIOCondition cond
, int timeout
)
533 * Since this is only used by xtensa in system mode, and stdio is
534 * handled through GuestFDConsole, and there are no semihosting
535 * system calls for sockets and the like, that means this descriptor
536 * must be a normal file. Normal files never block and are thus
539 complete(cs
, cond
& (G_IO_IN
| G_IO_OUT
), 0);
544 * Static file semihosting syscall implementations.
547 static void staticfile_read(CPUState
*cs
, gdb_syscall_complete_cb complete
,
548 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
550 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
551 target_ulong rest
= gf
->staticfile
.len
- gf
->staticfile
.off
;
557 ptr
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
559 complete(cs
, -1, EFAULT
);
562 memcpy(ptr
, gf
->staticfile
.data
+ gf
->staticfile
.off
, len
);
563 gf
->staticfile
.off
+= len
;
564 unlock_user(ptr
, buf
, len
);
565 complete(cs
, len
, 0);
568 static void staticfile_lseek(CPUState
*cs
, gdb_syscall_complete_cb complete
,
569 GuestFD
*gf
, int64_t off
, int gdb_whence
)
573 switch (gdb_whence
) {
578 ret
= gf
->staticfile
.off
+ off
;
581 ret
= gf
->staticfile
.len
+ off
;
587 if (ret
>= 0 && ret
<= gf
->staticfile
.len
) {
588 gf
->staticfile
.off
= ret
;
589 complete(cs
, ret
, 0);
591 complete(cs
, -1, EINVAL
);
595 static void staticfile_flen(CPUState
*cs
, gdb_syscall_complete_cb complete
,
598 complete(cs
, gf
->staticfile
.len
, 0);
602 * Console semihosting syscall implementations.
605 static void console_read(CPUState
*cs
, gdb_syscall_complete_cb complete
,
606 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
608 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
612 ptr
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
614 complete(cs
, -1, EFAULT
);
617 ret
= qemu_semihosting_console_read(cs
, ptr
, len
);
618 unlock_user(ptr
, buf
, ret
);
619 complete(cs
, ret
, 0);
622 static void console_write(CPUState
*cs
, gdb_syscall_complete_cb complete
,
623 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
625 CPUArchState
*env G_GNUC_UNUSED
= cs
->env_ptr
;
626 char *ptr
= lock_user(VERIFY_READ
, buf
, len
, 1);
630 complete(cs
, -1, EFAULT
);
633 ret
= qemu_semihosting_console_write(ptr
, len
);
634 unlock_user(ptr
, buf
, 0);
635 complete(cs
, ret
? ret
: -1, ret
? 0 : EIO
);
638 static void console_fstat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
639 GuestFD
*gf
, target_ulong addr
)
641 static const struct stat tty_buf
= {
642 .st_mode
= 020666, /* S_IFCHR, ugo+rw */
643 .st_rdev
= 5, /* makedev(5, 0) -- linux /dev/tty */
647 ret
= copy_stat_to_user(cs
, addr
, &tty_buf
);
648 complete(cs
, ret
? -1 : 0, ret
? -ret
: 0);
651 #ifndef CONFIG_USER_ONLY
652 static void console_poll_one(CPUState
*cs
, gdb_syscall_complete_cb complete
,
653 GuestFD
*gf
, GIOCondition cond
, int timeout
)
655 /* The semihosting console does not support urgent data or errors. */
656 cond
&= G_IO_IN
| G_IO_OUT
;
659 * Since qemu_semihosting_console_write never blocks, we can
660 * consider output always ready -- leave G_IO_OUT alone.
661 * All that remains is to conditionally signal input ready.
662 * Since output ready causes an immediate return, only block
665 * TODO: Implement proper timeout. For now, only support
666 * indefinite wait or immediate poll.
668 if (cond
== G_IO_IN
&& timeout
< 0) {
669 qemu_semihosting_console_block_until_ready(cs
);
670 /* We returned -- input must be ready. */
671 } else if ((cond
& G_IO_IN
) && !qemu_semihosting_console_ready()) {
675 complete(cs
, cond
, 0);
680 * Syscall entry points.
683 void semihost_sys_open(CPUState
*cs
, gdb_syscall_complete_cb complete
,
684 target_ulong fname
, target_ulong fname_len
,
685 int gdb_flags
, int mode
)
687 if (use_gdb_syscalls()) {
688 gdb_open(cs
, complete
, fname
, fname_len
, gdb_flags
, mode
);
690 host_open(cs
, complete
, fname
, fname_len
, gdb_flags
, mode
);
694 void semihost_sys_close(CPUState
*cs
, gdb_syscall_complete_cb complete
, int fd
)
696 GuestFD
*gf
= get_guestfd(fd
);
699 complete(cs
, -1, EBADF
);
704 gdb_close(cs
, complete
, gf
);
707 host_close(cs
, complete
, gf
);
714 g_assert_not_reached();
719 void semihost_sys_read_gf(CPUState
*cs
, gdb_syscall_complete_cb complete
,
720 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
723 * Bound length for 64-bit guests on 32-bit hosts, not overflowing ssize_t.
724 * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
725 * idea to do this unconditionally.
727 if (len
> INT32_MAX
) {
732 gdb_read(cs
, complete
, gf
, buf
, len
);
735 host_read(cs
, complete
, gf
, buf
, len
);
738 staticfile_read(cs
, complete
, gf
, buf
, len
);
741 console_read(cs
, complete
, gf
, buf
, len
);
744 g_assert_not_reached();
748 void semihost_sys_read(CPUState
*cs
, gdb_syscall_complete_cb complete
,
749 int fd
, target_ulong buf
, target_ulong len
)
751 GuestFD
*gf
= get_guestfd(fd
);
754 semihost_sys_read_gf(cs
, complete
, gf
, buf
, len
);
756 complete(cs
, -1, EBADF
);
760 void semihost_sys_write_gf(CPUState
*cs
, gdb_syscall_complete_cb complete
,
761 GuestFD
*gf
, target_ulong buf
, target_ulong len
)
764 * Bound length for 64-bit guests on 32-bit hosts, not overflowing ssize_t.
765 * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
766 * idea to do this unconditionally.
768 if (len
> INT32_MAX
) {
773 gdb_write(cs
, complete
, gf
, buf
, len
);
776 host_write(cs
, complete
, gf
, buf
, len
);
779 console_write(cs
, complete
, gf
, buf
, len
);
782 /* Static files are never open for writing: EBADF. */
783 complete(cs
, -1, EBADF
);
786 g_assert_not_reached();
790 void semihost_sys_write(CPUState
*cs
, gdb_syscall_complete_cb complete
,
791 int fd
, target_ulong buf
, target_ulong len
)
793 GuestFD
*gf
= get_guestfd(fd
);
796 semihost_sys_write_gf(cs
, complete
, gf
, buf
, len
);
798 complete(cs
, -1, EBADF
);
802 void semihost_sys_lseek(CPUState
*cs
, gdb_syscall_complete_cb complete
,
803 int fd
, int64_t off
, int gdb_whence
)
805 GuestFD
*gf
= get_guestfd(fd
);
808 complete(cs
, -1, EBADF
);
813 gdb_lseek(cs
, complete
, gf
, off
, gdb_whence
);
816 host_lseek(cs
, complete
, gf
, off
, gdb_whence
);
819 staticfile_lseek(cs
, complete
, gf
, off
, gdb_whence
);
822 complete(cs
, -1, ESPIPE
);
825 g_assert_not_reached();
829 void semihost_sys_isatty(CPUState
*cs
, gdb_syscall_complete_cb complete
, int fd
)
831 GuestFD
*gf
= get_guestfd(fd
);
834 complete(cs
, 0, EBADF
);
839 gdb_isatty(cs
, complete
, gf
);
842 host_isatty(cs
, complete
, gf
);
845 complete(cs
, 0, ENOTTY
);
851 g_assert_not_reached();
855 void semihost_sys_flen(CPUState
*cs
, gdb_syscall_complete_cb fstat_cb
,
856 gdb_syscall_complete_cb flen_cb
, int fd
,
857 target_ulong fstat_addr
)
859 GuestFD
*gf
= get_guestfd(fd
);
862 flen_cb(cs
, -1, EBADF
);
867 gdb_fstat(cs
, fstat_cb
, gf
, fstat_addr
);
870 host_flen(cs
, flen_cb
, gf
);
873 staticfile_flen(cs
, flen_cb
, gf
);
877 g_assert_not_reached();
881 void semihost_sys_fstat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
882 int fd
, target_ulong addr
)
884 GuestFD
*gf
= get_guestfd(fd
);
887 complete(cs
, -1, EBADF
);
892 gdb_fstat(cs
, complete
, gf
, addr
);
895 host_fstat(cs
, complete
, gf
, addr
);
898 console_fstat(cs
, complete
, gf
, addr
);
902 g_assert_not_reached();
906 void semihost_sys_stat(CPUState
*cs
, gdb_syscall_complete_cb complete
,
907 target_ulong fname
, target_ulong fname_len
,
910 if (use_gdb_syscalls()) {
911 gdb_stat(cs
, complete
, fname
, fname_len
, addr
);
913 host_stat(cs
, complete
, fname
, fname_len
, addr
);
917 void semihost_sys_remove(CPUState
*cs
, gdb_syscall_complete_cb complete
,
918 target_ulong fname
, target_ulong fname_len
)
920 if (use_gdb_syscalls()) {
921 gdb_remove(cs
, complete
, fname
, fname_len
);
923 host_remove(cs
, complete
, fname
, fname_len
);
927 void semihost_sys_rename(CPUState
*cs
, gdb_syscall_complete_cb complete
,
928 target_ulong oname
, target_ulong oname_len
,
929 target_ulong nname
, target_ulong nname_len
)
931 if (use_gdb_syscalls()) {
932 gdb_rename(cs
, complete
, oname
, oname_len
, nname
, nname_len
);
934 host_rename(cs
, complete
, oname
, oname_len
, nname
, nname_len
);
938 void semihost_sys_system(CPUState
*cs
, gdb_syscall_complete_cb complete
,
939 target_ulong cmd
, target_ulong cmd_len
)
941 if (use_gdb_syscalls()) {
942 gdb_system(cs
, complete
, cmd
, cmd_len
);
944 host_system(cs
, complete
, cmd
, cmd_len
);
948 void semihost_sys_gettimeofday(CPUState
*cs
, gdb_syscall_complete_cb complete
,
949 target_ulong tv_addr
, target_ulong tz_addr
)
951 if (use_gdb_syscalls()) {
952 gdb_gettimeofday(cs
, complete
, tv_addr
, tz_addr
);
954 host_gettimeofday(cs
, complete
, tv_addr
, tz_addr
);
958 #ifndef CONFIG_USER_ONLY
959 void semihost_sys_poll_one(CPUState
*cs
, gdb_syscall_complete_cb complete
,
960 int fd
, GIOCondition cond
, int timeout
)
962 GuestFD
*gf
= get_guestfd(fd
);
965 complete(cs
, G_IO_NVAL
, 1);
970 complete(cs
, G_IO_NVAL
, 1);
973 host_poll_one(cs
, complete
, gf
, cond
, timeout
);
976 console_poll_one(cs
, complete
, gf
, cond
, timeout
);
980 g_assert_not_reached();