]> Git Repo - qemu.git/blob - hw/semihosting/console.c
fw_cfg: Allow reboot-timeout=-1 again
[qemu.git] / hw / semihosting / console.c
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"
19 #include "cpu.h"
20 #include "hw/semihosting/semihost.h"
21 #include "hw/semihosting/console.h"
22 #include "exec/gdbstub.h"
23 #include "qemu/log.h"
24 #include "chardev/char.h"
25
26 int qemu_semihosting_log_out(const char *s, int len)
27 {
28     Chardev *chardev = semihosting_get_chardev();
29     if (chardev) {
30         return qemu_chr_write_all(chardev, (uint8_t *) s, len);
31     } else {
32         return write(STDERR_FILENO, s, len);
33     }
34 }
35
36 /*
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.
40  */
41 static GString *copy_user_string(CPUArchState *env, target_ulong addr)
42 {
43     CPUState *cpu = env_cpu(env);
44     GString *s = g_string_sized_new(128);
45     uint8_t c;
46
47     do {
48         if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
49             s = g_string_append_c(s, c);
50         } else {
51             qemu_log_mask(LOG_GUEST_ERROR,
52                           "%s: passed inaccessible address " TARGET_FMT_lx,
53                           __func__, addr);
54             break;
55         }
56     } while (c!=0);
57
58     return s;
59 }
60
61 static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err)
62 {
63     if (ret == (target_ulong) -1) {
64         qemu_log("%s: gdb console output failed ("TARGET_FMT_ld")",
65                  __func__, err);
66     }
67 }
68
69 int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
70 {
71     GString *s = copy_user_string(env, addr);
72     int out = s->len;
73
74     if (use_gdb_syscalls()) {
75         gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, s->len);
76     } else {
77         out = qemu_semihosting_log_out(s->str, s->len);
78     }
79
80     g_string_free(s, true);
81     return out;
82 }
83
84 void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
85 {
86     CPUState *cpu = env_cpu(env);
87     uint8_t c;
88
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);
92         } else {
93             qemu_semihosting_log_out((const char *) &c, 1);
94         }
95     } else {
96         qemu_log_mask(LOG_GUEST_ERROR,
97                       "%s: passed inaccessible address " TARGET_FMT_lx,
98                       __func__, addr);
99     }
100 }
This page took 0.029896 seconds and 4 git commands to generate.