]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/arm/semihost.c
Merge remote-tracking branch 'remotes/stsquad/tags/pull-testing-and-misc-271020-1...
[mirror_qemu.git] / linux-user / arm / semihost.c
CommitLineData
0dc07721
AB
1/*
2 * ARM Semihosting Console Support
3 *
4 * Copyright (c) 2019 Linaro Ltd
5 *
6 * Currently ARM is unique in having support for semihosting support
7 * in linux-user. So for now we implement the common console API but
8 * just for arm linux-user.
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13#include "qemu/osdep.h"
14#include "cpu.h"
15#include "hw/semihosting/console.h"
16#include "qemu.h"
8de702cb 17#include <termios.h>
0dc07721 18
78e24848 19int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
0dc07721 20{
78e24848
AB
21 int len = target_strlen(addr);
22 void *s;
23 if (len < 0){
24 qemu_log_mask(LOG_GUEST_ERROR,
25 "%s: passed inaccessible address " TARGET_FMT_lx,
26 __func__, addr);
27 return 0;
28 }
29 s = lock_user(VERIFY_READ, addr, (long)(len + 1), 1);
30 g_assert(s); /* target_strlen has already verified this will work */
31 len = write(STDERR_FILENO, s, len);
0dc07721
AB
32 unlock_user(s, addr, 0);
33 return len;
34}
78e24848
AB
35
36void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
37{
38 char c;
39
40 if (get_user_u8(c, addr)) {
41 qemu_log_mask(LOG_GUEST_ERROR,
42 "%s: passed inaccessible address " TARGET_FMT_lx,
43 __func__, addr);
44 } else {
45 if (write(STDERR_FILENO, &c, 1) != 1) {
46 qemu_log_mask(LOG_UNIMP, "%s: unexpected write to stdout failure",
47 __func__);
48 }
49 }
50}
8de702cb
KP
51
52/*
53 * For linux-user we can safely block. However as we want to return as
54 * soon as a character is read we need to tweak the termio to disable
55 * line buffering. We restore the old mode afterwards in case the
56 * program is expecting more normal behaviour. This is slow but
57 * nothing using semihosting console reading is expecting to be fast.
58 */
59target_ulong qemu_semihosting_console_inc(CPUArchState *env)
60{
61 uint8_t c;
62 struct termios old_tio, new_tio;
63
64 /* Disable line-buffering and echo */
65 tcgetattr(STDIN_FILENO, &old_tio);
66 new_tio = old_tio;
67 new_tio.c_lflag &= (~ICANON & ~ECHO);
68 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
69
70 c = getchar();
71
72 /* restore config */
73 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
74
75 return (target_ulong) c;
76}