guestfd.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Hosted file support for semihosting syscalls.
  3. *
  4. * Copyright (c) 2005, 2007 CodeSourcery.
  5. * Copyright (c) 2019 Linaro
  6. * Copyright © 2020 by Keith Packard <keithp@keithp.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #ifndef SEMIHOSTING_GUESTFD_H
  11. #define SEMIHOSTING_GUESTFD_H
  12. typedef enum GuestFDType {
  13. GuestFDUnused = 0,
  14. GuestFDHost,
  15. GuestFDGDB,
  16. GuestFDStatic,
  17. GuestFDConsole,
  18. } GuestFDType;
  19. /*
  20. * Guest file descriptors are integer indexes into an array of
  21. * these structures (we will dynamically resize as necessary).
  22. */
  23. typedef struct GuestFD {
  24. GuestFDType type;
  25. union {
  26. int hostfd;
  27. struct {
  28. const uint8_t *data;
  29. size_t len;
  30. size_t off;
  31. } staticfile;
  32. };
  33. } GuestFD;
  34. /*
  35. * For ARM semihosting, we have a separate structure for routing
  36. * data for the console which is outside the guest fd address space.
  37. */
  38. extern GuestFD console_in_gf;
  39. extern GuestFD console_out_gf;
  40. /**
  41. * alloc_guestfd:
  42. *
  43. * Allocate an unused GuestFD index. The associated guestfd index
  44. * will still be GuestFDUnused until it is initialized.
  45. */
  46. int alloc_guestfd(void);
  47. /**
  48. * dealloc_guestfd:
  49. * @guestfd: GuestFD index
  50. *
  51. * Deallocate a GuestFD index. The associated GuestFD structure
  52. * will be recycled for a subsequent allocation.
  53. */
  54. void dealloc_guestfd(int guestfd);
  55. /**
  56. * get_guestfd:
  57. * @guestfd: GuestFD index
  58. *
  59. * Return the GuestFD structure associated with an initialized @guestfd,
  60. * or NULL if it has not been allocated, or hasn't been initialized.
  61. */
  62. GuestFD *get_guestfd(int guestfd);
  63. /**
  64. * associate_guestfd:
  65. * @guestfd: GuestFD index
  66. * @hostfd: host file descriptor
  67. *
  68. * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd.
  69. */
  70. void associate_guestfd(int guestfd, int hostfd);
  71. /**
  72. * staticfile_guestfd:
  73. * @guestfd: GuestFD index
  74. * @data: data to be read
  75. * @len: length of @data
  76. *
  77. * Initialize the GuestFD for @guestfd to GuestFDStatic.
  78. * The @len bytes at @data will be returned to the guest on reads.
  79. */
  80. void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);
  81. #endif /* SEMIHOSTING_GUESTFD_H */