Alex Bennée | 0dc0772 | 2019-05-14 11:07:15 +0100 | [diff] [blame] | 1 | /* |
Keith Packard | a10b9d9 | 2021-01-08 22:42:52 +0000 | [diff] [blame] | 2 | * ARM Compatible Semihosting Console Support. |
Alex Bennée | 0dc0772 | 2019-05-14 11:07:15 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2019 Linaro Ltd |
| 5 | * |
Keith Packard | a10b9d9 | 2021-01-08 22:42:52 +0000 | [diff] [blame] | 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. |
Alex Bennée | 0dc0772 | 2019-05-14 11:07:15 +0100 | [diff] [blame] | 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | 6b5fe13 | 2021-03-05 13:54:49 +0000 | [diff] [blame] | 14 | #include "semihosting/console.h" |
Alex Bennée | 0dc0772 | 2019-05-14 11:07:15 +0100 | [diff] [blame] | 15 | #include "qemu.h" |
Peter Maydell | 3b249d2 | 2021-09-08 16:44:03 +0100 | [diff] [blame] | 16 | #include "user-internals.h" |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 17 | #include <termios.h> |
Alex Bennée | 0dc0772 | 2019-05-14 11:07:15 +0100 | [diff] [blame] | 18 | |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 19 | /* |
| 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 Henderson | e7fb6f3 | 2022-05-01 12:31:08 -0700 | [diff] [blame] | 26 | int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 27 | { |
Richard Henderson | e7fb6f3 | 2022-05-01 12:31:08 -0700 | [diff] [blame] | 28 | int ret; |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 29 | 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 Henderson | e7fb6f3 | 2022-05-01 12:31:08 -0700 | [diff] [blame] | 35 | new_tio.c_cc[VMIN] = 1; |
| 36 | new_tio.c_cc[VTIME] = 0; |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 37 | tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); |
| 38 | |
Richard Henderson | e7fb6f3 | 2022-05-01 12:31:08 -0700 | [diff] [blame] | 39 | ret = fread(buf, 1, len, stdin); |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 40 | |
| 41 | /* restore config */ |
| 42 | tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); |
| 43 | |
Richard Henderson | e7fb6f3 | 2022-05-01 12:31:08 -0700 | [diff] [blame] | 44 | return ret; |
Keith Packard | 8de702c | 2019-11-04 12:42:30 -0800 | [diff] [blame] | 45 | } |
Richard Henderson | cd66f20 | 2022-05-01 12:42:37 -0700 | [diff] [blame] | 46 | |
| 47 | int qemu_semihosting_console_write(void *buf, int len) |
| 48 | { |
| 49 | return fwrite(buf, 1, len, stderr); |
| 50 | } |