pal_host.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * pal_host.h
  15. *
  16. * This file contains definition of PAL host ABI.
  17. */
  18. #ifndef PAL_HOST_H
  19. #define PAL_HOST_H
  20. #ifndef IN_PAL
  21. # error "cannot be included outside PAL"
  22. #endif
  23. #include <atomic.h>
  24. /* Simpler mutex design: a single variable that tracks whether the
  25. * mutex is locked (just waste a 64 bit word for now). State is 1 (locked) or
  26. * 0 (unlocked).
  27. * Keep a count of how many threads are waiting on the mutex.
  28. * If DEBUG_MUTEX is defined,
  29. * mutex_handle will record the owner of mutex locking. */
  30. typedef struct mutex_handle {
  31. volatile int64_t locked;
  32. struct atomic_int nwaiters;
  33. #ifdef DEBUG_MUTEX
  34. int owner;
  35. #endif
  36. } PAL_LOCK;
  37. /* Initializer of Mutexes */
  38. #define MUTEX_HANDLE_INIT { .locked = 0, .nwaiters.counter = 0 }
  39. #define INIT_MUTEX_HANDLE(m) do { (m)->locked = 0; atomic_set(&(m)->nwaiters, 0); } while (0)
  40. #define LOCK_INIT MUTEX_HANDLE_INIT
  41. #define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock)
  42. /* Locking and unlocking of Mutexes */
  43. int _DkMutexLock(struct mutex_handle* mut);
  44. int _DkMutexLockTimeout(struct mutex_handle* mut, int64_t timeout_us);
  45. int _DkMutexUnlock(struct mutex_handle* mut);
  46. typedef struct {
  47. PAL_HDR hdr;
  48. } PAL_RESERVED_HDR;
  49. typedef struct pal_handle
  50. {
  51. /* TSAI: Here we define the internal types of PAL_HANDLE
  52. * in PAL design, user has not to access the content inside the
  53. * handle, also there is no need to allocate the internal
  54. * handles, so we hide the type name of these handles on purpose.
  55. */
  56. PAL_HDR hdr;
  57. union {
  58. struct {
  59. PAL_IDX fds[MAX_FDS];
  60. } generic;
  61. struct {
  62. PAL_IDX fd;
  63. PAL_NUM offset;
  64. PAL_STR realpath;
  65. /*
  66. * map_start is to request this file should be mapped to this
  67. * address. When fork is emulated, the address is already
  68. * determined by parent process.
  69. */
  70. PAL_PTR map_start;
  71. } file;
  72. struct {
  73. PAL_IDX fd;
  74. PAL_NUM pipeid;
  75. PAL_BOL nonblocking;
  76. } pipe;
  77. struct {
  78. PAL_IDX fds[MAX_FDS];
  79. PAL_BOL nonblocking;
  80. } pipeprv;
  81. struct {
  82. PAL_IDX fd;
  83. /* TODO: add other flags in future, if needed (e.g., semaphore) */
  84. PAL_BOL nonblocking;
  85. } eventfd;
  86. struct {
  87. PAL_IDX fd_in, fd_out;
  88. PAL_IDX dev_type;
  89. PAL_BOL destroy;
  90. PAL_STR realpath;
  91. } dev;
  92. struct {
  93. PAL_IDX fd;
  94. PAL_STR realpath;
  95. PAL_PTR buf;
  96. PAL_PTR ptr;
  97. PAL_PTR end;
  98. PAL_BOL endofstream;
  99. } dir;
  100. struct {
  101. PAL_IDX fd;
  102. PAL_PTR bind;
  103. PAL_PTR conn;
  104. PAL_BOL nonblocking;
  105. PAL_BOL reuseaddr;
  106. PAL_NUM linger;
  107. PAL_NUM receivebuf;
  108. PAL_NUM sendbuf;
  109. PAL_NUM receivetimeout;
  110. PAL_NUM sendtimeout;
  111. PAL_BOL tcp_cork;
  112. PAL_BOL tcp_keepalive;
  113. PAL_BOL tcp_nodelay;
  114. } sock;
  115. struct {
  116. PAL_IDX stream_in, stream_out;
  117. PAL_IDX cargo;
  118. PAL_IDX pid;
  119. PAL_BOL nonblocking;
  120. } process;
  121. struct {
  122. PAL_IDX cli;
  123. PAL_IDX srv;
  124. PAL_IDX port;
  125. PAL_BOL nonblocking;
  126. PAL_PTR addr;
  127. } mcast;
  128. struct {
  129. PAL_IDX tid;
  130. PAL_PTR stack;
  131. } thread;
  132. struct {
  133. struct mutex_handle mut;
  134. } mutex;
  135. struct {
  136. struct atomic_int signaled;
  137. struct atomic_int nwaiters;
  138. PAL_BOL isnotification;
  139. } event;
  140. };
  141. } * PAL_HANDLE;
  142. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  143. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  144. #define WRITABLE(n) (1 << (MAX_FDS*2 + (n)))
  145. #define ERROR(n) (1 << (MAX_FDS*3 + (n)))
  146. #define HAS_FDS ((1 << MAX_FDS*2) - 1)
  147. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  148. extern void __check_pending_event (void);
  149. #define LEAVE_PAL_CALL() do { __check_pending_event(); } while (0)
  150. #define LEAVE_PAL_CALL_RETURN(retval) \
  151. do { __check_pending_event(); return (retval); } while (0)
  152. #endif /* PAL_HOST_H */