semihost.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * ARM Compatible Semihosting Console Support.
  3. *
  4. * Copyright (c) 2019 Linaro Ltd
  5. *
  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.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0-or-later
  11. */
  12. #include "qemu/osdep.h"
  13. #include "semihosting/console.h"
  14. #include "qemu.h"
  15. #include "user-internals.h"
  16. #include <termios.h>
  17. /*
  18. * For linux-user we can safely block. However as we want to return as
  19. * soon as a character is read we need to tweak the termio to disable
  20. * line buffering. We restore the old mode afterwards in case the
  21. * program is expecting more normal behaviour. This is slow but
  22. * nothing using semihosting console reading is expecting to be fast.
  23. */
  24. int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
  25. {
  26. int ret;
  27. struct termios old_tio, new_tio;
  28. /* Disable line-buffering and echo */
  29. tcgetattr(STDIN_FILENO, &old_tio);
  30. new_tio = old_tio;
  31. new_tio.c_lflag &= (~ICANON & ~ECHO);
  32. new_tio.c_cc[VMIN] = 1;
  33. new_tio.c_cc[VTIME] = 0;
  34. tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
  35. ret = fread(buf, 1, len, stdin);
  36. /* restore config */
  37. tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
  38. return ret;
  39. }
  40. int qemu_semihosting_console_write(void *buf, int len)
  41. {
  42. return fwrite(buf, 1, len, stderr);
  43. }