pal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.h
  15. *
  16. * This file contains definition of PAL host ABI.
  17. */
  18. #ifndef PAL_H
  19. #define PAL_H
  20. #include <stdbool.h>
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <stdnoreturn.h>
  24. typedef uint64_t PAL_NUM;
  25. typedef const char * PAL_STR;
  26. typedef void * PAL_PTR;
  27. typedef uint32_t PAL_FLG;
  28. typedef uint32_t PAL_IDX;
  29. typedef bool PAL_BOL;
  30. /* Moved MAX_FDS from <host_kernel>/pal_host.h to here,
  31. * since it is 3, across all host kernels. */
  32. #define MAX_FDS 3
  33. #ifdef IN_PAL
  34. #include <atomic.h>
  35. typedef struct atomic_int PAL_REF;
  36. typedef struct {
  37. PAL_IDX type;
  38. PAL_FLG flags;
  39. } PAL_HDR;
  40. # include "pal_host.h"
  41. # ifndef HANDLE_HDR
  42. # define HANDLE_HDR(handle) (&((handle)->hdr))
  43. # endif
  44. static inline void init_handle_hdr(PAL_HDR *hdr, int pal_type) {
  45. hdr->type = pal_type;
  46. hdr->flags = 0;
  47. }
  48. # define SET_HANDLE_TYPE(handle, t) init_handle_hdr(HANDLE_HDR(handle), pal_type_##t)
  49. # define IS_HANDLE_TYPE(handle, t) (HANDLE_HDR(handle)->type == pal_type_##t)
  50. #else
  51. typedef union pal_handle
  52. {
  53. struct {
  54. PAL_IDX type;
  55. /* the PAL-level reference counting is deprecated */
  56. } hdr;
  57. } * PAL_HANDLE;
  58. # ifndef HANDLE_HDR
  59. # define HANDLE_HDR(handle) (&((handle)->hdr))
  60. # endif
  61. #endif /* !IN_PAL */
  62. #define PAL_LIBOS_TCB_SIZE 256
  63. typedef struct pal_tcb {
  64. struct pal_tcb * self;
  65. /* uint64_t for alignment */
  66. uint64_t libos_tcb[(PAL_LIBOS_TCB_SIZE + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
  67. /* data private to PAL implementation follows this struct. */
  68. } PAL_TCB;
  69. static inline PAL_TCB * pal_get_tcb (void)
  70. {
  71. PAL_TCB * tcb;
  72. __asm__ ("movq %%gs:%c1,%q0"
  73. : "=r" (tcb)
  74. : "i" (offsetof(struct pal_tcb, self)));
  75. return tcb;
  76. }
  77. typedef struct {
  78. #ifdef __x86_64__
  79. PAL_NUM r8, r9, r10, r11, r12, r13, r14, r15;
  80. PAL_NUM rdi, rsi, rbp, rbx, rdx, rax, rcx;
  81. PAL_NUM rsp, rip;
  82. PAL_NUM efl, csgsfs, err, trapno, oldmask, cr2;
  83. #else
  84. # error "Unsupported architecture"
  85. #endif
  86. } PAL_CONTEXT;
  87. #define PAL_TRUE true
  88. #define PAL_FALSE false
  89. /********** PAL TYPE DEFINITIONS **********/
  90. enum {
  91. pal_type_file,
  92. pal_type_pipe,
  93. pal_type_pipesrv,
  94. pal_type_pipecli,
  95. pal_type_pipeprv,
  96. pal_type_dev,
  97. pal_type_dir,
  98. pal_type_tcp,
  99. pal_type_tcpsrv,
  100. pal_type_udp,
  101. pal_type_udpsrv,
  102. pal_type_process,
  103. pal_type_mcast,
  104. pal_type_thread,
  105. pal_type_mutex,
  106. pal_type_event,
  107. pal_type_eventfd,
  108. PAL_HANDLE_TYPE_BOUND,
  109. };
  110. #define PAL_IDX_POISON ((PAL_IDX)-1) /* PAL identifier poison value */
  111. #define PAL_GET_TYPE(h) (HANDLE_HDR(h)->type)
  112. #define PAL_CHECK_TYPE(h, t) (PAL_GET_TYPE(h) == pal_type_##t)
  113. #define UNKNOWN_HANDLE(handle) (PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND)
  114. typedef struct { PAL_PTR start, end; } PAL_PTR_RANGE;
  115. typedef struct {
  116. PAL_NUM cpu_num;
  117. PAL_STR cpu_vendor;
  118. PAL_STR cpu_brand;
  119. PAL_NUM cpu_family;
  120. PAL_NUM cpu_model;
  121. PAL_NUM cpu_stepping;
  122. PAL_STR cpu_flags;
  123. } PAL_CPU_INFO;
  124. typedef struct {
  125. PAL_NUM mem_total;
  126. } PAL_MEM_INFO;
  127. /********** PAL APIs **********/
  128. typedef struct {
  129. PAL_STR host_type;
  130. /* An identifier of current picoprocess */
  131. PAL_NUM process_id;
  132. PAL_NUM host_id;
  133. /***** Handles and executables *****/
  134. /* program manifest */
  135. PAL_HANDLE manifest_handle;
  136. /* executable name */
  137. PAL_STR executable;
  138. /* handle of parent process */
  139. PAL_HANDLE parent_process;
  140. /* handle of first thread */
  141. PAL_HANDLE first_thread;
  142. /* debug stream */
  143. PAL_HANDLE debug_stream;
  144. /* broadcast RPC stream */
  145. PAL_HANDLE broadcast_stream;
  146. /***** Memory layout ******/
  147. /* The range of user address */
  148. PAL_PTR_RANGE user_address;
  149. /* Reserved memory range inside of user address.
  150. * Used for example by SGX for exec area (including memory gap) in the
  151. * middle of the heap. If unused set start == end. */
  152. PAL_PTR_RANGE user_address_hole;
  153. /* address where executable is loaded */
  154. PAL_PTR_RANGE executable_range;
  155. /* manifest preloaded here */
  156. PAL_PTR_RANGE manifest_preload;
  157. /***** Host information *****/
  158. /* Host allocation alignment.
  159. * This currently is (and most likely will always be) indistinguishable from the page size,
  160. * looking from the LibOS perspective. The two values can be different on the PAL level though,
  161. * see e.g. SYSTEM_INFO::dwAllocationGranularity on Windows.
  162. */
  163. PAL_NUM alloc_align;
  164. /* CPU information (only required ones) */
  165. PAL_CPU_INFO cpu_info;
  166. /* Memory information (only required ones) */
  167. PAL_MEM_INFO mem_info;
  168. /* Attestation information */
  169. PAL_STR attestation_status;
  170. PAL_STR attestation_timestamp;
  171. /* Purely for profiling */
  172. PAL_NUM startup_time;
  173. PAL_NUM host_specific_startup_time;
  174. PAL_NUM relocation_time;
  175. PAL_NUM linking_time;
  176. PAL_NUM manifest_loading_time;
  177. PAL_NUM allocation_time;
  178. PAL_NUM tail_startup_time;
  179. PAL_NUM child_creation_time;
  180. } PAL_CONTROL;
  181. #define pal_control (*pal_control_addr())
  182. PAL_CONTROL * pal_control_addr (void);
  183. /* The ABI includes three calls to allocate, free, and modify the
  184. * permission bits on page-base virtual memory. Permissions in-
  185. * clude read, write, execute, and guard. Memory regions can be
  186. * unallocated, reserved, or backed by committed memory
  187. */
  188. /* Memory Allocation Flags */
  189. #define PAL_ALLOC_RESERVE 0x0001 /* Only reserve the memory */
  190. #ifdef IN_PAL
  191. #define PAL_ALLOC_INTERNAL 0x8000
  192. #endif
  193. /* Memory Protection Flags */
  194. #define PAL_PROT_NONE 0x0 /* 0x0 Page can not be accessed. */
  195. #define PAL_PROT_READ 0x1 /* 0x1 Page can be read. */
  196. #define PAL_PROT_WRITE 0x2 /* 0x2 Page can be written. */
  197. #define PAL_PROT_EXEC 0x4 /* 0x4 Page can be executed. */
  198. #define PAL_PROT_WRITECOPY 0x8 /* 0x8 Copy on write */
  199. #define PAL_PROT_MASK 0xF
  200. // If addr != NULL, then the returned region is always exactly at addr.
  201. PAL_PTR
  202. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  203. PAL_FLG prot);
  204. void
  205. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size);
  206. PAL_BOL
  207. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot);
  208. /* The ABI includes one call to create a child process and one call to
  209. * terminate the running process. A child process does not inherit
  210. * any objects or memory from its parent process and the parent
  211. * process may not modify the execution of its children. A parent can
  212. * wait for a child to exit using its handle. Parent and child may
  213. * communicate through I/O streams provided by the parent to the
  214. * child at creation
  215. */
  216. #define PAL_PROCESS_MASK 0x0
  217. PAL_HANDLE
  218. DkProcessCreate (PAL_STR uri, PAL_STR * args);
  219. noreturn void
  220. DkProcessExit (PAL_NUM exitCode);
  221. /* The stream ABI includes nine calls to open, read, write, map, unmap,
  222. * truncate, flush, delete and wait for I/O streams and three calls to
  223. * access metadata about an I/O stream. The ABI purposefully does not
  224. * provide an ioctl call. Supported URI schemes include file:, pipe:,
  225. * http:, https:, tcp:, udp:, pipe.srv:, http.srv, tcp.srv:, and udp.srv:.
  226. * The latter four schemes are used to open inbound I/O streams for
  227. * server applications.
  228. */
  229. /* DkStreamOpen
  230. * access_mode: WRONLY or RDONLY or RDWR
  231. * share_flags: permission for the created file
  232. * create_flags: the creation options for the file
  233. * options: other options
  234. */
  235. /* Stream Access Flags */
  236. #define PAL_ACCESS_RDONLY 00
  237. #define PAL_ACCESS_WRONLY 01
  238. #define PAL_ACCESS_RDWR 02
  239. #define PAL_ACCESS_APPEND 04
  240. #define PAL_ACCESS_MASK 07
  241. /* Stream Sharing Flags */
  242. #define PAL_SHARE_GLOBAL_X 01
  243. #define PAL_SHARE_GLOBAL_W 02
  244. #define PAL_SHARE_GLOBAL_R 04
  245. #define PAL_SHARE_GROUP_X 010
  246. #define PAL_SHARE_GROUP_W 020
  247. #define PAL_SHARE_GROUP_R 040
  248. #define PAL_SHARE_OWNER_X 0100
  249. #define PAL_SHARE_OWNER_W 0200
  250. #define PAL_SHARE_OWNER_R 0400
  251. #define PAL_SHARE_MASK 0777
  252. /* Stream Create Flags */
  253. #define PAL_CREATE_TRY 0100 /* 0100 Create file if file not
  254. exist (O_CREAT) */
  255. #define PAL_CREATE_ALWAYS 0200 /* 0300 Create file and fail if file
  256. already exist (O_CREAT|O_EXCL) */
  257. #define PAL_CREATE_MASK 0300
  258. /* Stream Option Flags */
  259. #define PAL_OPTION_NONBLOCK 04000
  260. #define PAL_OPTION_MASK 04000
  261. /* CLOEXEC is generic for any stream.
  262. * SEMAPHORE is specific to eventfd syscall. */
  263. #define PAL_OPTION_CLOEXEC 01000
  264. #define PAL_OPTION_EFD_SEMAPHORE 02000
  265. /* error value of read/write */
  266. #define PAL_STREAM_ERROR ((PAL_NUM)-1L)
  267. #define WITHIN_MASK(val, mask) (((val)|(mask)) == (mask))
  268. PAL_HANDLE
  269. DkStreamOpen (PAL_STR uri, PAL_FLG access, PAL_FLG share_flags,
  270. PAL_FLG create, PAL_FLG options);
  271. PAL_HANDLE
  272. DkStreamWaitForClient (PAL_HANDLE handle);
  273. PAL_NUM
  274. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  275. PAL_PTR buffer, PAL_PTR source, PAL_NUM size);
  276. PAL_NUM
  277. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  278. PAL_PTR buffer, PAL_STR dest);
  279. #define PAL_DELETE_RD 01
  280. #define PAL_DELETE_WR 02
  281. void
  282. DkStreamDelete (PAL_HANDLE handle, PAL_FLG access);
  283. PAL_PTR
  284. DkStreamMap (PAL_HANDLE handle, PAL_PTR address, PAL_FLG prot,
  285. PAL_NUM offset, PAL_NUM size);
  286. void
  287. DkStreamUnmap (PAL_PTR addr, PAL_NUM size);
  288. /* Sets the length of the file referenced by handle to length. Returns the 0
  289. * on success, a _positive_ errno on failure.
  290. */
  291. PAL_NUM
  292. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length);
  293. PAL_BOL
  294. DkStreamFlush (PAL_HANDLE handle);
  295. PAL_BOL
  296. DkSendHandle (PAL_HANDLE handle, PAL_HANDLE cargo);
  297. PAL_HANDLE
  298. DkReceiveHandle (PAL_HANDLE handle);
  299. /* stream attribute structure */
  300. typedef struct {
  301. PAL_IDX handle_type;
  302. PAL_BOL disconnected;
  303. PAL_BOL nonblocking;
  304. PAL_BOL readable, writable, runnable;
  305. PAL_FLG share_flags;
  306. PAL_NUM pending_size;
  307. PAL_IDX no_of_fds;
  308. PAL_IDX fds[MAX_FDS];
  309. union {
  310. struct {
  311. PAL_NUM linger;
  312. PAL_NUM receivebuf, sendbuf;
  313. PAL_NUM receivetimeout, sendtimeout;
  314. PAL_BOL tcp_cork;
  315. PAL_BOL tcp_keepalive;
  316. PAL_BOL tcp_nodelay;
  317. } socket;
  318. };
  319. } PAL_STREAM_ATTR;
  320. PAL_BOL
  321. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr);
  322. PAL_BOL
  323. DkStreamAttributesQueryByHandle (PAL_HANDLE handle,
  324. PAL_STREAM_ATTR * attr);
  325. PAL_BOL
  326. DkStreamAttributesSetByHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  327. PAL_NUM
  328. DkStreamGetName (PAL_HANDLE handle, PAL_PTR buffer, PAL_NUM size);
  329. PAL_BOL
  330. DkStreamChangeName (PAL_HANDLE handle, PAL_STR uri);
  331. /* The ABI supports multithreading through five calls to create,
  332. * sleep, yield the scheduler quantum for, resume execution of, and
  333. * terminate threads, as well as seven calls to create, signal, and
  334. * block on synchronization objects
  335. */
  336. #define PAL_THREAD_MASK 0
  337. PAL_HANDLE
  338. DkThreadCreate (PAL_PTR addr, PAL_PTR param);
  339. // assuming duration to be in microseconds
  340. PAL_NUM
  341. DkThreadDelayExecution (PAL_NUM duration);
  342. void
  343. DkThreadYieldExecution (void);
  344. noreturn void DkThreadExit(PAL_PTR clear_child_tid);
  345. PAL_BOL
  346. DkThreadResume (PAL_HANDLE thread);
  347. /* Exception Handling */
  348. /* arithmetic error (div-by-zero, floating point exception, etc.) */
  349. #define PAL_EVENT_ARITHMETIC_ERROR 1
  350. /* segmentation fault, protection fault, bus fault */
  351. #define PAL_EVENT_MEMFAULT 2
  352. /* illegal instructions */
  353. #define PAL_EVENT_ILLEGAL 3
  354. /* terminated by external program */
  355. #define PAL_EVENT_QUIT 4
  356. /* suspended by external program */
  357. #define PAL_EVENT_SUSPEND 5
  358. /* continued by external program */
  359. #define PAL_EVENT_RESUME 6
  360. /* failure within PAL calls */
  361. #define PAL_EVENT_FAILURE 7
  362. #define PAL_EVENT_NUM_BOUND 8
  363. #define PAL_EVENT_PRIVATE 0x0001 /* upcall specific to thread */
  364. #define PAL_EVENT_RESET 0x0002 /* reset the event upcall */
  365. typedef void (*PAL_EVENT_HANDLER) (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT *);
  366. PAL_BOL
  367. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event);
  368. void DkExceptionReturn (PAL_PTR event);
  369. /* parameter: keeping int threadHandle for now (to be in sync with the paper).
  370. * We may want to replace it with a PAL_HANDLE. Ideally, either use PAL_HANDLE
  371. * or threadHandle.
  372. */
  373. /* Create a Mutex.
  374. * initialCount of 0 is totally unlocked; an initialCount of 1
  375. * is initialized to locked. */
  376. PAL_HANDLE
  377. DkMutexCreate (PAL_NUM initialCount);
  378. /* Destroy a mutex using DkObjectClose */
  379. void
  380. DkMutexRelease (PAL_HANDLE mutexHandle);
  381. PAL_HANDLE
  382. DkNotificationEventCreate (PAL_BOL initialState);
  383. PAL_HANDLE
  384. DkSynchronizationEventCreate (PAL_BOL initialState);
  385. /* DkEventDestroy deprecated, replaced by DkObjectClose */
  386. void
  387. DkEventSet (PAL_HANDLE eventHandle);
  388. /* DkEventWait deprecated, replaced by DkObjectsWaitAny */
  389. void
  390. DkEventClear (PAL_HANDLE eventHandle);
  391. #define NO_TIMEOUT ((PAL_NUM)-1)
  392. /* Returns: NULL if the call times out, the ready handle on success */
  393. PAL_HANDLE
  394. DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE* handle_array, PAL_NUM timeout_us);
  395. #define PAL_WAIT_SIGNAL 1 /* ignored in events */
  396. #define PAL_WAIT_READ 2
  397. #define PAL_WAIT_WRITE 4
  398. #define PAL_WAIT_ERROR 8 /* ignored in events */
  399. PAL_BOL DkObjectsWaitEvents(PAL_NUM count, PAL_HANDLE* handle_array, PAL_FLG* events,
  400. PAL_FLG* ret_events, PAL_NUM timeout_us);
  401. /* Deprecate DkObjectReference */
  402. void DkObjectClose (PAL_HANDLE objectHandle);
  403. /* the ABI includes seven assorted calls to get wall clock
  404. * time, generate cryptographically-strong random bits, flush por-
  405. * tions of instruction caches, increment and decrement the reference
  406. * counts on objects shared between threads, and to coordinate
  407. * threads with the security monitor during process serialization
  408. */
  409. /* assuming the time to be in microseconds */
  410. PAL_NUM
  411. DkSystemTimeQuery (void);
  412. /*
  413. * Cryptographically secure random.
  414. * 0 on success, negative on failure.
  415. */
  416. PAL_NUM
  417. DkRandomBitsRead (PAL_PTR buffer, PAL_NUM size);
  418. PAL_BOL
  419. DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size);
  420. #define PAL_SEGMENT_FS 0x1
  421. #define PAL_SEGMENT_GS 0x2
  422. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr);
  423. PAL_NUM DkMemoryAvailableQuota (void);
  424. #define PAL_CPUID_WORD_EAX 0
  425. #define PAL_CPUID_WORD_EBX 1
  426. #define PAL_CPUID_WORD_ECX 2
  427. #define PAL_CPUID_WORD_EDX 3
  428. #define PAL_CPUID_WORD_NUM 4
  429. PAL_BOL
  430. DkCpuIdRetrieve (PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4]);
  431. #ifdef __GNUC__
  432. # define symbol_version_default(real, name, version) \
  433. __asm__ (".symver " #real "," #name "@@" #version "\n")
  434. #else
  435. # define symbol_version_default(real, name, version)
  436. #endif
  437. #endif /* PAL_H */