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