]> git.proxmox.com Git - mirror_qemu.git/blame - semihosting/console.c
accel/tcg: Un-inline icount_exit_request() for clarity
[mirror_qemu.git] / semihosting / console.c
CommitLineData
a331c6d7
AB
1/*
2 * Semihosting Console Support
3 *
4 * Copyright (c) 2015 Imagination Technologies
5 * Copyright (c) 2019 Linaro Ltd
6 *
7 * This provides support for outputting to a semihosting console.
8 *
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.
14 *
15 * SPDX-License-Identifier: GPL-2.0-or-later
16 */
17
18#include "qemu/osdep.h"
6b5fe137
PMD
19#include "semihosting/semihost.h"
20#include "semihosting/console.h"
a331c6d7 21#include "exec/gdbstub.h"
8de702cb 22#include "exec/exec-all.h"
a331c6d7 23#include "qemu/log.h"
4e7f9032 24#include "chardev/char.h"
8de702cb 25#include "chardev/char-fe.h"
8de702cb
KP
26#include "qemu/main-loop.h"
27#include "qapi/error.h"
28#include "qemu/fifo8.h"
a331c6d7 29
fb08790b
RH
30/* Access to this structure is protected by the BQL */
31typedef struct SemihostingConsole {
32 CharBackend backend;
33 Chardev *chr;
34 GSList *sleeping_cpus;
35 bool got;
36 Fifo8 fifo;
37} SemihostingConsole;
38
39static SemihostingConsole console;
40
8de702cb
KP
41#define FIFO_SIZE 1024
42
8de702cb
KP
43static int console_can_read(void *opaque)
44{
45 SemihostingConsole *c = opaque;
195801d7 46 g_assert(bql_locked());
66997c42 47 return (int)fifo8_num_free(&c->fifo);
8de702cb
KP
48}
49
50static void console_wake_up(gpointer data, gpointer user_data)
51{
52 CPUState *cs = (CPUState *) data;
53 /* cpu_handle_halt won't know we have work so just unbung here */
54 cs->halted = 0;
55 qemu_cpu_kick(cs);
56}
57
58static void console_read(void *opaque, const uint8_t *buf, int size)
59{
60 SemihostingConsole *c = opaque;
195801d7 61 g_assert(bql_locked());
8de702cb
KP
62 while (size-- && !fifo8_is_full(&c->fifo)) {
63 fifo8_push(&c->fifo, *buf++);
64 }
65 g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
66 c->sleeping_cpus = NULL;
67}
68
1b9177f7
RH
69bool qemu_semihosting_console_ready(void)
70{
71 SemihostingConsole *c = &console;
72
195801d7 73 g_assert(bql_locked());
1b9177f7
RH
74 return !fifo8_is_empty(&c->fifo);
75}
76
77void qemu_semihosting_console_block_until_ready(CPUState *cs)
8de702cb 78{
8de702cb 79 SemihostingConsole *c = &console;
3367d452 80
195801d7 81 g_assert(bql_locked());
e7fb6f32
RH
82
83 /* Block if the fifo is completely empty. */
8de702cb 84 if (fifo8_is_empty(&c->fifo)) {
3367d452
RH
85 c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
86 cs->halted = 1;
87 cs->exception_index = EXCP_HALTED;
88 cpu_loop_exit(cs);
8de702cb
KP
89 /* never returns */
90 }
1b9177f7
RH
91}
92
93int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
94{
95 SemihostingConsole *c = &console;
96 int ret = 0;
97
98 qemu_semihosting_console_block_until_ready(cs);
e7fb6f32
RH
99
100 /* Read until buffer full or fifo exhausted. */
101 do {
102 *(char *)(buf + ret) = fifo8_pop(&c->fifo);
103 ret++;
104 } while (ret < len && !fifo8_is_empty(&c->fifo));
105
106 return ret;
8de702cb
KP
107}
108
cd66f20f
RH
109int qemu_semihosting_console_write(void *buf, int len)
110{
111 if (console.chr) {
aed04e63
PM
112 int r = qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
113 return r < 0 ? 0 : r;
cd66f20f
RH
114 } else {
115 return fwrite(buf, 1, len, stderr);
116 }
117}
118
fb08790b 119void qemu_semihosting_console_init(Chardev *chr)
8de702cb 120{
fb08790b 121 console.chr = chr;
8de702cb
KP
122 if (chr) {
123 fifo8_create(&console.fifo, FIFO_SIZE);
124 qemu_chr_fe_init(&console.backend, chr, &error_abort);
125 qemu_chr_fe_set_handlers(&console.backend,
126 console_can_read,
127 console_read,
128 NULL, NULL, &console,
129 NULL, true);
130 }
e4a4aaa5
RH
131
132 qemu_semihosting_guestfd_init();
8de702cb 133}