pal_host.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * pal_host.h
  17. *
  18. * This file contains definition of PAL host ABI.
  19. */
  20. #ifndef PAL_HOST_H
  21. #define PAL_HOST_H
  22. #ifndef IN_PAL
  23. # error "cannot be included outside PAL"
  24. #endif
  25. #include <atomic.h>
  26. /* Spinlocking */
  27. typedef struct spinlock {
  28. struct atomic_int value;
  29. } PAL_LOCK;
  30. int _DkSpinLock (struct spinlock * lock);
  31. int _DkSpinUnlock (struct spinlock * lock);
  32. #define LOCK_INIT { .value = { 0 } }
  33. #define _DkInternalLock _DkSpinLock
  34. #define _DkInternalUnlock _DkSpinUnlock
  35. void * malloc_untrusted (int size);
  36. void free_untrusted (void * mem);
  37. #include <linux_list.h>
  38. /* internal Mutex design, the structure has to align at integer boundary
  39. because it is required by futex call. If DEBUG_MUTEX is defined,
  40. mutex_handle will record the owner of mutex locking. */
  41. struct mutex_handle {
  42. struct atomic_int value;
  43. };
  44. typedef union pal_handle
  45. {
  46. /* TSAI: Here we define the internal types of PAL_HANDLE
  47. * in PAL design, user has not to access the content inside the
  48. * handle, also there is no need to allocate the internal
  49. * handles, so we hide the type name of these handles on purpose.
  50. */
  51. struct {
  52. PAL_IDX type;
  53. PAL_REF ref;
  54. PAL_FLG flags;
  55. PAL_IDX fds[];
  56. } hdr;
  57. struct {
  58. PAL_HDR reserved;
  59. PAL_IDX fd;
  60. PAL_BOL append;
  61. PAL_BOL pass;
  62. PAL_STR realpath;
  63. PAL_NUM total;
  64. PAL_PTR stubs;
  65. } file;
  66. struct {
  67. PAL_HDR reserved;
  68. PAL_IDX fd;
  69. PAL_NUM pipeid;
  70. PAL_BOL nonblocking;
  71. } pipe;
  72. struct {
  73. PAL_HDR reserved;
  74. PAL_IDX fds[2];
  75. PAL_BOL nonblocking;
  76. } pipeprv;
  77. struct {
  78. PAL_HDR reserved;
  79. PAL_IDX fd_in, fd_out;
  80. PAL_IDX dev_type;
  81. PAL_BOL destroy;
  82. PAL_STR realpath;
  83. } dev;
  84. struct {
  85. PAL_HDR reserved;
  86. PAL_IDX fd;
  87. PAL_STR realpath;
  88. PAL_PTR buf;
  89. PAL_PTR ptr;
  90. PAL_PTR end;
  91. PAL_BOL endofstream;
  92. } dir;
  93. struct {
  94. PAL_HDR reserved;
  95. PAL_IDX fd;
  96. } gipc;
  97. struct {
  98. PAL_HDR reserved;
  99. PAL_IDX fd;
  100. PAL_PTR bind;
  101. PAL_PTR conn;
  102. PAL_BOL nonblocking;
  103. PAL_NUM linger;
  104. PAL_NUM receivebuf;
  105. PAL_NUM sendbuf;
  106. PAL_NUM receivetimeout;
  107. PAL_NUM sendtimeout;
  108. PAL_BOL tcp_cork;
  109. PAL_BOL tcp_keepalive;
  110. PAL_BOL tcp_nodelay;
  111. } sock;
  112. struct {
  113. PAL_HDR reserved;
  114. PAL_IDX stream_in, stream_out;
  115. PAL_IDX cargo;
  116. PAL_IDX pid;
  117. PAL_BOL nonblocking;
  118. } process;
  119. struct {
  120. PAL_HDR reserved;
  121. PAL_IDX cli;
  122. PAL_IDX srv;
  123. PAL_IDX port;
  124. PAL_BOL nonblocking;
  125. } mcast;
  126. struct {
  127. PAL_HDR reserved;
  128. PAL_IDX tid;
  129. struct hlist_node hlist;
  130. } thread;
  131. struct {
  132. PAL_HDR reserved;
  133. struct atomic_int nwaiters;
  134. PAL_NUM max_value;
  135. union {
  136. struct mutex_handle mut;
  137. struct atomic_int i;
  138. } value;
  139. } semaphore;
  140. struct {
  141. PAL_HDR reserved;
  142. struct atomic_int signaled;
  143. struct atomic_int nwaiters;
  144. PAL_BOL isnotification;
  145. } event;
  146. } * PAL_HANDLE;
  147. #define RFD(n) (00001 << (n))
  148. #define WFD(n) (00010 << (n))
  149. #define WRITEABLE(n) (00100 << (n))
  150. #define ERROR(n) (01000 << (n))
  151. #define MAX_FDS (3)
  152. #define HAS_FDS (00077)
  153. #define HANDLE_TYPE(handle) ((handle)->hdr.type)
  154. struct arch_frame {
  155. #ifdef __x86_64__
  156. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  157. #else
  158. # error "unsupported architecture"
  159. #endif
  160. };
  161. #ifdef __x86_64__
  162. # define store_register(reg, var) \
  163. asm volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  164. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  165. # define arch_store_frame(f) \
  166. store_register_in_frame(rsp, f) \
  167. store_register_in_frame(rbp, f) \
  168. store_register_in_frame(rbx, f) \
  169. store_register_in_frame(rsi, f) \
  170. store_register_in_frame(rdi, f) \
  171. store_register_in_frame(r12, f) \
  172. store_register_in_frame(r13, f) \
  173. store_register_in_frame(r14, f) \
  174. store_register_in_frame(r15, f)
  175. # define restore_register(reg, var, clobber...) \
  176. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  177. # define restore_register_in_frame(reg, f) \
  178. restore_register(reg, (f)->reg, \
  179. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  180. # define arch_restore_frame(f) \
  181. restore_register_in_frame(r15, f) \
  182. restore_register_in_frame(r14, f) \
  183. restore_register_in_frame(r13, f) \
  184. restore_register_in_frame(r12, f) \
  185. restore_register_in_frame(rdi, f) \
  186. restore_register_in_frame(rsi, f) \
  187. restore_register_in_frame(rbx, f) \
  188. restore_register_in_frame(rbp, f) \
  189. restore_register_in_frame(rsp, f)
  190. #else /* __x86_64__ */
  191. # error "unsupported architecture"
  192. #endif
  193. struct pal_frame {
  194. volatile struct pal_frame * self;
  195. void * func;
  196. const char * funcname;
  197. struct arch_frame arch;
  198. };
  199. static inline
  200. void __store_frame (struct pal_frame * frame,
  201. void * func, const char * funcname)
  202. {
  203. *(volatile void **) &frame->self = frame;
  204. frame->func = func;
  205. frame->funcname = funcname;
  206. arch_store_frame(&frame->arch)
  207. }
  208. #define ENTER_PAL_CALL(name) \
  209. struct pal_frame frame; \
  210. __store_frame(&frame, &(name), #name)
  211. static inline
  212. void __clear_frame (struct pal_frame * frame)
  213. {
  214. if (*(volatile void **) &frame->self == frame) {
  215. asm volatile ("nop" ::: "memory");
  216. *(volatile void **) &frame->self = NULL;
  217. }
  218. }
  219. #define LEAVE_PAL_CALL() \
  220. do { \
  221. __clear_frame(&frame); \
  222. } while (0)
  223. #define LEAVE_PAL_CALL_RETURN(retval) \
  224. do { \
  225. __clear_frame(&frame); \
  226. return (retval); \
  227. } while (0)
  228. #endif /* PAL_HOST_H */