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"
23 #include "exec/exec-all.h"
25 #include "chardev/char.h"
26 #include "chardev/char-fe.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/main-loop.h"
29 #include "qapi/error.h"
30 #include "qemu/fifo8.h"
32 int qemu_semihosting_log_out(const char *s
, int len
)
34 Chardev
*chardev
= semihosting_get_chardev();
36 return qemu_chr_write_all(chardev
, (uint8_t *) s
, len
);
38 return write(STDERR_FILENO
, s
, len
);
43 * A re-implementation of lock_user_string that we can use locally
44 * instead of relying on softmmu-semi. Hopefully we can deprecate that
45 * in time. Copy string until we find a 0 or address error.
47 static GString
*copy_user_string(CPUArchState
*env
, target_ulong addr
)
49 CPUState
*cpu
= env_cpu(env
);
50 GString
*s
= g_string_sized_new(128);
54 if (cpu_memory_rw_debug(cpu
, addr
++, &c
, 1, 0) == 0) {
55 s
= g_string_append_c(s
, c
);
57 qemu_log_mask(LOG_GUEST_ERROR
,
58 "%s: passed inaccessible address " TARGET_FMT_lx
,
67 static void semihosting_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
69 if (ret
== (target_ulong
) -1) {
70 qemu_log("%s: gdb console output failed ("TARGET_FMT_ld
")",
75 int qemu_semihosting_console_outs(CPUArchState
*env
, target_ulong addr
)
77 GString
*s
= copy_user_string(env
, addr
);
80 if (use_gdb_syscalls()) {
81 gdb_do_syscall(semihosting_cb
, "write,2,%x,%x", addr
, s
->len
);
83 out
= qemu_semihosting_log_out(s
->str
, s
->len
);
86 g_string_free(s
, true);
90 void qemu_semihosting_console_outc(CPUArchState
*env
, target_ulong addr
)
92 CPUState
*cpu
= env_cpu(env
);
95 if (cpu_memory_rw_debug(cpu
, addr
, &c
, 1, 0) == 0) {
96 if (use_gdb_syscalls()) {
97 gdb_do_syscall(semihosting_cb
, "write,2,%x,%x", addr
, 1);
99 qemu_semihosting_log_out((const char *) &c
, 1);
102 qemu_log_mask(LOG_GUEST_ERROR
,
103 "%s: passed inaccessible address " TARGET_FMT_lx
,
108 #define FIFO_SIZE 1024
110 /* Access to this structure is protected by the BQL */
111 typedef struct SemihostingConsole
{
113 GSList
*sleeping_cpus
;
116 } SemihostingConsole
;
118 static SemihostingConsole console
;
120 static int console_can_read(void *opaque
)
122 SemihostingConsole
*c
= opaque
;
124 g_assert(qemu_mutex_iothread_locked());
125 ret
= (int) fifo8_num_free(&c
->fifo
);
129 static void console_wake_up(gpointer data
, gpointer user_data
)
131 CPUState
*cs
= (CPUState
*) data
;
132 /* cpu_handle_halt won't know we have work so just unbung here */
137 static void console_read(void *opaque
, const uint8_t *buf
, int size
)
139 SemihostingConsole
*c
= opaque
;
140 g_assert(qemu_mutex_iothread_locked());
141 while (size
-- && !fifo8_is_full(&c
->fifo
)) {
142 fifo8_push(&c
->fifo
, *buf
++);
144 g_slist_foreach(c
->sleeping_cpus
, console_wake_up
, NULL
);
145 c
->sleeping_cpus
= NULL
;
148 target_ulong
qemu_semihosting_console_inc(CPUArchState
*env
)
151 SemihostingConsole
*c
= &console
;
152 g_assert(qemu_mutex_iothread_locked());
153 g_assert(current_cpu
);
154 if (fifo8_is_empty(&c
->fifo
)) {
155 c
->sleeping_cpus
= g_slist_prepend(c
->sleeping_cpus
, current_cpu
);
156 current_cpu
->halted
= 1;
157 current_cpu
->exception_index
= EXCP_HALTED
;
158 cpu_loop_exit(current_cpu
);
161 ch
= fifo8_pop(&c
->fifo
);
162 return (target_ulong
) ch
;
165 void qemu_semihosting_console_init(void)
167 Chardev
*chr
= semihosting_get_chardev();
170 fifo8_create(&console
.fifo
, FIFO_SIZE
);
171 qemu_chr_fe_init(&console
.backend
, chr
, &error_abort
);
172 qemu_chr_fe_set_handlers(&console
.backend
,
175 NULL
, NULL
, &console
,