2 * Semihosting Console Support
4 * Copyright (c) 2015 Imagination Technologies
5 * Copyright (c) 2019 Linaro Ltd
7 * This provides support for outputting to a semihosting console.
9 * While most semihosting implementations support reading and writing
10 * to arbitrary file descriptors we treat the console as something
11 * specifically for debugging interaction. This means messages can be
12 * re-directed to gdb (if currently being used to debug) or even
13 * re-directed elsewhere.
15 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include "qemu/osdep.h"
20 #include "hw/semihosting/semihost.h"
21 #include "hw/semihosting/console.h"
22 #include "exec/gdbstub.h"
24 #include "chardev/char.h"
26 int qemu_semihosting_log_out(const char *s
, int len
)
28 Chardev
*chardev
= semihosting_get_chardev();
30 return qemu_chr_write_all(chardev
, (uint8_t *) s
, len
);
32 return write(STDERR_FILENO
, s
, len
);
37 * A re-implementation of lock_user_string that we can use locally
38 * instead of relying on softmmu-semi. Hopefully we can deprecate that
39 * in time. Copy string until we find a 0 or address error.
41 static GString
*copy_user_string(CPUArchState
*env
, target_ulong addr
)
43 CPUState
*cpu
= env_cpu(env
);
44 GString
*s
= g_string_sized_new(128);
48 if (cpu_memory_rw_debug(cpu
, addr
++, &c
, 1, 0) == 0) {
49 s
= g_string_append_c(s
, c
);
51 qemu_log_mask(LOG_GUEST_ERROR
,
52 "%s: passed inaccessible address " TARGET_FMT_lx
,
61 static void semihosting_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
63 if (ret
== (target_ulong
) -1) {
64 qemu_log("%s: gdb console output failed ("TARGET_FMT_ld
")",
69 int qemu_semihosting_console_outs(CPUArchState
*env
, target_ulong addr
)
71 GString
*s
= copy_user_string(env
, addr
);
74 if (use_gdb_syscalls()) {
75 gdb_do_syscall(semihosting_cb
, "write,2,%x,%x", addr
, s
->len
);
77 out
= qemu_semihosting_log_out(s
->str
, s
->len
);
80 g_string_free(s
, true);
84 void qemu_semihosting_console_outc(CPUArchState
*env
, target_ulong addr
)
86 CPUState
*cpu
= env_cpu(env
);
89 if (cpu_memory_rw_debug(cpu
, addr
, &c
, 1, 0) == 0) {
90 if (use_gdb_syscalls()) {
91 gdb_do_syscall(semihosting_cb
, "write,2,%x,%x", addr
, 1);
93 qemu_semihosting_log_out((const char *) &c
, 1);
96 qemu_log_mask(LOG_GUEST_ERROR
,
97 "%s: passed inaccessible address " TARGET_FMT_lx
,