qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

semihosting: implement a semihosting console

This provides two functions for handling console output that handle
the common backend behaviour for semihosting.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

+116
+1
hw/semihosting/Makefile.objs
··· 1 1 obj-$(CONFIG_SEMIHOSTING) += config.o 2 + obj-$(CONFIG_SEMIHOSTING) += console.o
+77
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/console.h" 21 + #include "exec/gdbstub.h" 22 + #include "qemu/log.h" 23 + 24 + int qemu_semihosting_log_out(const char *s, int len) 25 + { 26 + return write(STDERR_FILENO, s, len); 27 + } 28 + 29 + /* 30 + * A re-implementation of lock_user_string that we can use locally 31 + * instead of relying on softmmu-semi. Hopefully we can deprecate that 32 + * in time. We either copy len bytes if specified or until we find a NULL. 33 + */ 34 + static GString *copy_user_string(CPUArchState *env, target_ulong addr, int len) 35 + { 36 + CPUState *cpu = ENV_GET_CPU(env); 37 + GString *s = g_string_sized_new(len ? len : 128); 38 + uint8_t c; 39 + bool done; 40 + 41 + do { 42 + if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) { 43 + s = g_string_append_c(s, c); 44 + done = len ? s->len == len : c == 0; 45 + } else { 46 + qemu_log_mask(LOG_GUEST_ERROR, 47 + "%s: passed inaccessible address " TARGET_FMT_lx, 48 + __func__, addr); 49 + done = true; 50 + } 51 + } while (!done); 52 + 53 + return s; 54 + } 55 + 56 + static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err) 57 + { 58 + if (ret == (target_ulong) -1) { 59 + qemu_log("%s: gdb console output failed ("TARGET_FMT_ld")", 60 + __func__, err); 61 + } 62 + } 63 + 64 + int qemu_semihosting_console_out(CPUArchState *env, target_ulong addr, int len) 65 + { 66 + GString *s = copy_user_string(env, addr, len); 67 + int out = s->len; 68 + 69 + if (use_gdb_syscalls()) { 70 + gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, s->len); 71 + } else { 72 + out = qemu_semihosting_log_out(s->str, s->len); 73 + } 74 + 75 + g_string_free(s, true); 76 + return out; 77 + }
+38
include/hw/semihosting/console.h
··· 1 + /* 2 + * Semihosting Console 3 + * 4 + * Copyright (c) 2019 Linaro Ltd 5 + * 6 + * SPDX-License-Identifier: GPL-2.0-or-later 7 + */ 8 + 9 + #ifndef _SEMIHOST_CONSOLE_H_ 10 + #define _SEMIHOST_CONSOLE_H_ 11 + 12 + /** 13 + * qemu_semihosting_console_out: 14 + * @env: CPUArchState 15 + * @s: host address of guest string 16 + * @len: length of string or 0 (string is null terminated) 17 + * 18 + * Send a guest string to the debug console. This may be the remote 19 + * gdb session if a softmmu guest is currently being debugged. 20 + * 21 + * Returns: number of bytes written. 22 + */ 23 + int qemu_semihosting_console_out(CPUArchState *env, target_ulong s, int len); 24 + 25 + /** 26 + * qemu_semihosting_log_out: 27 + * @s: pointer to string 28 + * @len: length of string 29 + * 30 + * Send a string to the debug output. Unlike console_out these strings 31 + * can't be sent to a remote gdb instance as they don't exist in guest 32 + * memory. 33 + * 34 + * Returns: number of bytes written 35 + */ 36 + int qemu_semihosting_log_out(const char *s, int len); 37 + 38 + #endif /* _SEMIHOST_CONSOLE_H_ */