blob: cee62a365c4f7e54e9e69ae9d288fdfad643fe14 [file] [log] [blame]
Alex Bennée0dc07722019-05-14 11:07:15 +01001/*
Keith Packarda10b9d92021-01-08 22:42:52 +00002 * ARM Compatible Semihosting Console Support.
Alex Bennée0dc07722019-05-14 11:07:15 +01003 *
4 * Copyright (c) 2019 Linaro Ltd
5 *
Keith Packarda10b9d92021-01-08 22:42:52 +00006 * 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.
Alex Bennée0dc07722019-05-14 11:07:15 +01009 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13#include "qemu/osdep.h"
Philippe Mathieu-Daudé6b5fe132021-03-05 13:54:49 +000014#include "semihosting/console.h"
Alex Bennée0dc07722019-05-14 11:07:15 +010015#include "qemu.h"
Peter Maydell3b249d22021-09-08 16:44:03 +010016#include "user-internals.h"
Keith Packard8de702c2019-11-04 12:42:30 -080017#include <termios.h>
Alex Bennée0dc07722019-05-14 11:07:15 +010018
Keith Packard8de702c2019-11-04 12:42:30 -080019/*
20 * For linux-user we can safely block. However as we want to return as
21 * soon as a character is read we need to tweak the termio to disable
22 * line buffering. We restore the old mode afterwards in case the
23 * program is expecting more normal behaviour. This is slow but
24 * nothing using semihosting console reading is expecting to be fast.
25 */
Richard Hendersone7fb6f32022-05-01 12:31:08 -070026int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
Keith Packard8de702c2019-11-04 12:42:30 -080027{
Richard Hendersone7fb6f32022-05-01 12:31:08 -070028 int ret;
Keith Packard8de702c2019-11-04 12:42:30 -080029 struct termios old_tio, new_tio;
30
31 /* Disable line-buffering and echo */
32 tcgetattr(STDIN_FILENO, &old_tio);
33 new_tio = old_tio;
34 new_tio.c_lflag &= (~ICANON & ~ECHO);
Richard Hendersone7fb6f32022-05-01 12:31:08 -070035 new_tio.c_cc[VMIN] = 1;
36 new_tio.c_cc[VTIME] = 0;
Keith Packard8de702c2019-11-04 12:42:30 -080037 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
38
Richard Hendersone7fb6f32022-05-01 12:31:08 -070039 ret = fread(buf, 1, len, stdin);
Keith Packard8de702c2019-11-04 12:42:30 -080040
41 /* restore config */
42 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
43
Richard Hendersone7fb6f32022-05-01 12:31:08 -070044 return ret;
Keith Packard8de702c2019-11-04 12:42:30 -080045}
Richard Hendersoncd66f202022-05-01 12:42:37 -070046
47int qemu_semihosting_console_write(void *buf, int len)
48{
49 return fwrite(buf, 1, len, stderr);
50}