2 * ARM Compatible Semihosting Console Support.
4 * Copyright (c) 2019 Linaro Ltd
6 * Currently ARM and RISC-V are unique in having support for
7 * semihosting support in linux-user. So for now we implement the
8 * common console API but just for arm and risc-v linux-user.
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "qemu/osdep.h"
14 #include "semihosting/console.h"
18 int qemu_semihosting_console_outs(CPUArchState
*env
, target_ulong addr
)
20 int len
= target_strlen(addr
);
23 qemu_log_mask(LOG_GUEST_ERROR
,
24 "%s: passed inaccessible address " TARGET_FMT_lx
,
28 s
= lock_user(VERIFY_READ
, addr
, (long)(len
+ 1), 1);
29 g_assert(s
); /* target_strlen has already verified this will work */
30 len
= write(STDERR_FILENO
, s
, len
);
31 unlock_user(s
, addr
, 0);
35 void qemu_semihosting_console_outc(CPUArchState
*env
, target_ulong addr
)
39 if (get_user_u8(c
, addr
)) {
40 qemu_log_mask(LOG_GUEST_ERROR
,
41 "%s: passed inaccessible address " TARGET_FMT_lx
,
44 if (write(STDERR_FILENO
, &c
, 1) != 1) {
45 qemu_log_mask(LOG_UNIMP
, "%s: unexpected write to stdout failure",
52 * For linux-user we can safely block. However as we want to return as
53 * soon as a character is read we need to tweak the termio to disable
54 * line buffering. We restore the old mode afterwards in case the
55 * program is expecting more normal behaviour. This is slow but
56 * nothing using semihosting console reading is expecting to be fast.
58 target_ulong
qemu_semihosting_console_inc(CPUArchState
*env
)
61 struct termios old_tio
, new_tio
;
63 /* Disable line-buffering and echo */
64 tcgetattr(STDIN_FILENO
, &old_tio
);
66 new_tio
.c_lflag
&= (~ICANON
& ~ECHO
);
67 tcsetattr(STDIN_FILENO
, TCSANOW
, &new_tio
);
72 tcsetattr(STDIN_FILENO
, TCSANOW
, &old_tio
);
74 return (target_ulong
) c
;