pal_host.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <spinlock.h>
  25. typedef spinlock_t PAL_LOCK;
  26. #define LOCK_INIT INIT_SPINLOCK_UNLOCKED
  27. #define _DkInternalLock spinlock_lock
  28. #define _DkInternalUnlock spinlock_unlock
  29. #define _DkInternalIsLocked spinlock_is_locked
  30. void * malloc_untrusted (int size);
  31. void free_untrusted (void * mem);
  32. #include <list.h>
  33. /* Simpler mutex design: a single variable that tracks whether the mutex
  34. * is locked (just waste a 64 bit word for now). State is 1 (locked) or
  35. * 0 (unlocked).
  36. *
  37. * Keep a count of how many threads are waiting on the mutex.
  38. *
  39. * If DEBUG_MUTEX is defined, mutex_handle will record the owner of
  40. * mutex locking. */
  41. struct mutex_handle {
  42. volatile int64_t * locked;
  43. struct atomic_int nwaiters;
  44. #ifdef DEBUG_MUTEX
  45. int owner;
  46. #endif
  47. };
  48. /* Initializer of Mutexes */
  49. #define MUTEX_HANDLE_INIT { .u = 0 }
  50. #define INIT_MUTEX_HANDLE(m) do { (m)->u = 0; } while (0)
  51. DEFINE_LIST(pal_handle_thread);
  52. struct pal_handle_thread {
  53. PAL_HDR reserved;
  54. PAL_IDX tid;
  55. PAL_PTR tcs;
  56. LIST_TYPE(pal_handle_thread) list;
  57. void * param;
  58. };
  59. /* RPC streams are encrypted with 256-bit AES keys */
  60. typedef uint8_t PAL_SESSION_KEY[32];
  61. typedef struct pal_handle
  62. {
  63. /*
  64. * Here we define the internal structure of PAL_HANDLE.
  65. * user has no access to the content inside these handles.
  66. */
  67. PAL_HDR hdr;
  68. union {
  69. struct {
  70. PAL_IDX fds[MAX_FDS];
  71. } generic;
  72. struct {
  73. PAL_IDX fd;
  74. PAL_STR realpath;
  75. PAL_NUM total;
  76. /* below fields are used only for trusted files */
  77. PAL_PTR stubs; /* contains hashes of file chunks */
  78. PAL_PTR umem; /* valid only when stubs != NULL */
  79. } file;
  80. struct {
  81. PAL_IDX fd;
  82. PAL_NUM pipeid;
  83. PAL_BOL nonblocking;
  84. } pipe;
  85. struct {
  86. PAL_IDX fds[MAX_FDS];
  87. PAL_BOL nonblocking;
  88. } pipeprv;
  89. struct {
  90. PAL_IDX fd;
  91. /* TODO: add other flags in future, if needed (e.g., semaphore) */
  92. PAL_BOL nonblocking;
  93. } eventfd;
  94. struct {
  95. PAL_IDX fd_in, fd_out;
  96. PAL_IDX dev_type;
  97. PAL_BOL destroy;
  98. PAL_STR realpath;
  99. } dev;
  100. struct {
  101. PAL_IDX fd;
  102. PAL_STR realpath;
  103. PAL_PTR buf;
  104. PAL_PTR ptr;
  105. PAL_PTR end;
  106. PAL_BOL endofstream;
  107. } dir;
  108. struct {
  109. PAL_IDX fd;
  110. PAL_PTR bind;
  111. PAL_PTR conn;
  112. PAL_BOL nonblocking;
  113. PAL_NUM linger;
  114. PAL_NUM receivebuf;
  115. PAL_NUM sendbuf;
  116. PAL_NUM receivetimeout;
  117. PAL_NUM sendtimeout;
  118. PAL_BOL tcp_cork;
  119. PAL_BOL tcp_keepalive;
  120. PAL_BOL tcp_nodelay;
  121. } sock;
  122. struct {
  123. PAL_IDX stream;
  124. PAL_IDX cargo;
  125. PAL_IDX pid;
  126. PAL_BOL nonblocking;
  127. PAL_SESSION_KEY session_key;
  128. void* ssl_ctx;
  129. } process;
  130. struct pal_handle_thread thread;
  131. struct {
  132. struct atomic_int nwaiters;
  133. PAL_NUM max_value;
  134. union {
  135. struct mutex_handle mut;
  136. } mutex;
  137. struct {
  138. struct atomic_int * signaled;
  139. struct atomic_int nwaiters;
  140. PAL_BOL isnotification;
  141. } event;
  142. };
  143. };
  144. } * PAL_HANDLE;
  145. #define RFD(n) (1 << (MAX_FDS*0 + (n)))
  146. #define WFD(n) (1 << (MAX_FDS*1 + (n)))
  147. #define ERROR(n) (1 << (MAX_FDS*2 + (n)))
  148. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  149. #endif /* PAL_HOST_H */