pal.h 15 KB

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