156b5170cSKeith Packard /*
2a10b9d93SKeith Packard * ARM Compatible Semihosting Console Support.
356b5170cSKeith Packard *
456b5170cSKeith Packard * Copyright (c) 2019 Linaro Ltd
556b5170cSKeith Packard *
6a10b9d93SKeith Packard * Currently ARM and RISC-V are unique in having support for
7a10b9d93SKeith Packard * semihosting support in linux-user. So for now we implement the
8a10b9d93SKeith Packard * common console API but just for arm and risc-v linux-user.
956b5170cSKeith Packard *
1056b5170cSKeith Packard * SPDX-License-Identifier: GPL-2.0-or-later
1156b5170cSKeith Packard */
1256b5170cSKeith Packard
1356b5170cSKeith Packard #include "qemu/osdep.h"
146b5fe137SPhilippe Mathieu-Daudé #include "semihosting/console.h"
1556b5170cSKeith Packard #include "qemu.h"
163b249d26SPeter Maydell #include "user-internals.h"
1756b5170cSKeith Packard #include <termios.h>
1856b5170cSKeith Packard
1956b5170cSKeith Packard /*
2056b5170cSKeith Packard * For linux-user we can safely block. However as we want to return as
2156b5170cSKeith Packard * soon as a character is read we need to tweak the termio to disable
2256b5170cSKeith Packard * line buffering. We restore the old mode afterwards in case the
2356b5170cSKeith Packard * program is expecting more normal behaviour. This is slow but
2456b5170cSKeith Packard * nothing using semihosting console reading is expecting to be fast.
2556b5170cSKeith Packard */
qemu_semihosting_console_read(CPUState * cs,void * buf,int len)26e7fb6f32SRichard Henderson int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
2756b5170cSKeith Packard {
28e7fb6f32SRichard Henderson int ret;
2956b5170cSKeith Packard struct termios old_tio, new_tio;
3056b5170cSKeith Packard
3156b5170cSKeith Packard /* Disable line-buffering and echo */
3256b5170cSKeith Packard tcgetattr(STDIN_FILENO, &old_tio);
3356b5170cSKeith Packard new_tio = old_tio;
3456b5170cSKeith Packard new_tio.c_lflag &= (~ICANON & ~ECHO);
35e7fb6f32SRichard Henderson new_tio.c_cc[VMIN] = 1;
36e7fb6f32SRichard Henderson new_tio.c_cc[VTIME] = 0;
3756b5170cSKeith Packard tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
3856b5170cSKeith Packard
39e7fb6f32SRichard Henderson ret = fread(buf, 1, len, stdin);
4056b5170cSKeith Packard
4156b5170cSKeith Packard /* restore config */
4256b5170cSKeith Packard tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
4356b5170cSKeith Packard
44e7fb6f32SRichard Henderson return ret;
4556b5170cSKeith Packard }
46*cd66f20fSRichard Henderson
qemu_semihosting_console_write(void * buf,int len)47*cd66f20fSRichard Henderson int qemu_semihosting_console_write(void *buf, int len)
48*cd66f20fSRichard Henderson {
49*cd66f20fSRichard Henderson return fwrite(buf, 1, len, stderr);
50*cd66f20fSRichard Henderson }
51