pal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #ifdef __x86_64__
  78. union pal_csgsfs {
  79. struct {
  80. uint16_t cs;
  81. uint16_t gs;
  82. uint16_t fs;
  83. uint16_t ss;
  84. };
  85. uint64_t csgsfs;
  86. };
  87. /* adopt Linux style fp layout, _libc_fpstate of glibc:
  88. * Because self-contained definition is needed for Pal definition,
  89. * same layout is defined with PAL prefix.
  90. */
  91. #define PAL_FP_XSTATE_MAGIC1 0x46505853U
  92. #define PAL_FP_XSTATE_MAGIC2 0x46505845U
  93. #define PAL_FP_XSTATE_MAGIC2_SIZE (sizeof(PAL_FP_XSTATE_MAGIC2))
  94. enum PAL_XFEATURE {
  95. PAL_XFEATURE_FP,
  96. PAL_XFEATURE_SSE,
  97. PAL_XFEATURE_YMM,
  98. PAL_XFEATURE_BNDREGS,
  99. PAL_XFEATURE_BNDCSR,
  100. PAL_XFEATURE_OPMASK,
  101. PAL_XFEATURE_ZMM_Hi256,
  102. PAL_XFEATURE_Hi16_ZMM,
  103. PAL_XFEATURE_PT,
  104. PAL_XFEATURE_PKRU,
  105. PAL_XFEATURE_MAX,
  106. };
  107. #define PAL_XFEATURE_MASK_FP (1UL << PAL_XFEATURE_FP)
  108. #define PAL_XFEATURE_MASK_SSE (1UL << PAL_XFEATURE_SSE)
  109. #define PAL_XFEATURE_MASK_YMM (1UL << PAL_XFEATURE_YMM)
  110. #define PAL_XFEATURE_MASK_BNDREGS (1UL << PAL_XFEATURE_BNDREGS)
  111. #define PAL_XFEATURE_MASK_BNDCSR (1UL << PAL_XFEATURE_BNDCSR)
  112. #define PAL_XFEATURE_MASK_OPMASK (1UL << PAL_XFEATURE_OPMASK)
  113. #define PAL_XFEATURE_MASK_ZMM_Hi256 (1UL << PAL_XFEATURE_ZMM_Hi256)
  114. #define PAL_XFEATURE_MASK_Hi16_ZMM (1UL << PAL_XFEATURE_Hi16_ZMM)
  115. #define PAL_XFEATURE_MASK_PT (1UL << PAL_XFEATURE_PT)
  116. #define PAL_XFEATURE_MASK_PKRU (1UL << PAL_XFEATURE_PKRU)
  117. #define PAL_XFEATURE_MASK_FPSSE (PAL_XFEATURE_MASK_FP \
  118. | PAL_XFEATURE_MASK_SSE)
  119. #define PAL_XFEATURE_MASK_AVX512 (PAL_XFEATURE_MASK_OPMASK \
  120. | PAL_XFEATURE_MASK_ZMM_Hi256 \
  121. | PAL_XFEATURE_MASK_Hi16_ZMM)
  122. typedef struct {
  123. uint32_t magic1; /* PAL_FP_XSTATE_MAGIC1 */
  124. uint32_t extended_size; /* xsave_size */
  125. uint64_t xfeatures; /* XSAVE feature */
  126. uint32_t xstate_size; /* xsave_size + PAL_FP_STATE_MAGIC2_SIZE */
  127. uint32_t padding[7];
  128. } PAL_FPX_SW_BYTES;
  129. typedef struct {
  130. uint32_t cwd;
  131. uint32_t swd;
  132. uint32_t twd;
  133. uint32_t fip;
  134. uint32_t fcs;
  135. uint32_t foo;
  136. uint32_t fos;
  137. uint32_t st_space[20];
  138. uint8_t ftop;
  139. uint8_t changed;
  140. uint8_t lookahead;
  141. uint8_t no_update;
  142. uint8_t rm;
  143. uint8_t alimit;
  144. void* info; /* struct math_emu_info */
  145. uint32_t entry_eip;
  146. } PAL_SWREGS_STATE;
  147. typedef struct {
  148. uint16_t significand[4];
  149. uint16_t exponent;
  150. uint16_t padding[3];
  151. } PAL_FPXREG;
  152. typedef struct {
  153. uint32_t element[4];
  154. } PAL_XMMREG;
  155. typedef struct {
  156. /* 64-bit FXSAVE format. */
  157. uint16_t cwd;
  158. uint16_t swd;
  159. uint16_t ftw;
  160. uint16_t fop;
  161. uint64_t rip;
  162. uint64_t rdp;
  163. uint32_t mxcsr;
  164. uint32_t mxcr_mask;
  165. PAL_FPXREG st[8];
  166. PAL_XMMREG xmm[16];
  167. union {
  168. uint32_t padding[24];
  169. struct {
  170. uint32_t padding2[12];
  171. PAL_FPX_SW_BYTES sw_reserved;
  172. };
  173. };
  174. } PAL_FPSTATE;
  175. typedef struct {
  176. uint64_t xfeatures;
  177. uint64_t xcomp_bv;
  178. uint64_t reserved[6];
  179. } __attribute__((packed)) PAL_XSTATE_HEADER;
  180. #define PAL_XSTATE_ALIGN 64
  181. typedef struct {
  182. PAL_FPSTATE fpstate;
  183. PAL_XSTATE_HEADER header;
  184. } __attribute__((packed, aligned(PAL_XSTATE_ALIGN))) PAL_XREGS_STATE;
  185. #else
  186. # error "Unsupported architecture"
  187. #endif
  188. typedef struct {
  189. #ifdef __x86_64__
  190. PAL_NUM r8, r9, r10, r11, r12, r13, r14, r15;
  191. PAL_NUM rdi, rsi, rbp, rbx, rdx, rax, rcx;
  192. PAL_NUM rsp, rip;
  193. PAL_NUM efl, csgsfs, err, trapno, oldmask, cr2;
  194. PAL_XREGS_STATE* fpregs;
  195. #else
  196. # error "Unsupported architecture"
  197. #endif
  198. } PAL_CONTEXT;
  199. #define PAL_TRUE true
  200. #define PAL_FALSE false
  201. /********** PAL TYPE DEFINITIONS **********/
  202. enum {
  203. pal_type_file,
  204. pal_type_pipe,
  205. pal_type_pipesrv,
  206. pal_type_pipecli,
  207. pal_type_pipeprv,
  208. pal_type_dev,
  209. pal_type_dir,
  210. pal_type_tcp,
  211. pal_type_tcpsrv,
  212. pal_type_udp,
  213. pal_type_udpsrv,
  214. pal_type_process,
  215. pal_type_thread,
  216. pal_type_mutex,
  217. pal_type_event,
  218. pal_type_eventfd,
  219. PAL_HANDLE_TYPE_BOUND,
  220. };
  221. #define PAL_IDX_POISON ((PAL_IDX)-1) /* PAL identifier poison value */
  222. #define PAL_GET_TYPE(h) (HANDLE_HDR(h)->type)
  223. #define PAL_CHECK_TYPE(h, t) (PAL_GET_TYPE(h) == pal_type_##t)
  224. #define UNKNOWN_HANDLE(handle) (PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND)
  225. typedef struct { PAL_PTR start, end; } PAL_PTR_RANGE;
  226. typedef struct {
  227. PAL_NUM cpu_num;
  228. PAL_STR cpu_vendor;
  229. PAL_STR cpu_brand;
  230. PAL_NUM cpu_family;
  231. PAL_NUM cpu_model;
  232. PAL_NUM cpu_stepping;
  233. double cpu_bogomips;
  234. PAL_STR cpu_flags;
  235. } PAL_CPU_INFO;
  236. typedef struct {
  237. PAL_NUM mem_total;
  238. } PAL_MEM_INFO;
  239. /********** PAL APIs **********/
  240. typedef struct {
  241. PAL_STR host_type;
  242. /* An identifier of current picoprocess */
  243. PAL_NUM process_id;
  244. PAL_NUM host_id;
  245. /***** Handles and executables *****/
  246. /* program manifest */
  247. PAL_HANDLE manifest_handle;
  248. /* executable name */
  249. PAL_STR executable;
  250. /* handle of parent process */
  251. PAL_HANDLE parent_process;
  252. /* handle of first thread */
  253. PAL_HANDLE first_thread;
  254. /* debug stream */
  255. PAL_HANDLE debug_stream;
  256. /***** Memory layout ******/
  257. /* The range of user address */
  258. PAL_PTR_RANGE user_address;
  259. /* Reserved memory range inside of user address.
  260. * Used for example by SGX for exec area (including memory gap) in the
  261. * middle of the heap. If unused set start == end. */
  262. PAL_PTR_RANGE user_address_hole;
  263. /* address where executable is loaded */
  264. PAL_PTR_RANGE executable_range;
  265. /* manifest preloaded here */
  266. PAL_PTR_RANGE manifest_preload;
  267. /***** Host information *****/
  268. /* Host allocation alignment.
  269. * This currently is (and most likely will always be) indistinguishable from the page size,
  270. * looking from the LibOS perspective. The two values can be different on the PAL level though,
  271. * see e.g. SYSTEM_INFO::dwAllocationGranularity on Windows.
  272. */
  273. PAL_NUM alloc_align;
  274. /* CPU information (only required ones) */
  275. PAL_CPU_INFO cpu_info;
  276. /* Memory information (only required ones) */
  277. PAL_MEM_INFO mem_info;
  278. /* Attestation information */
  279. PAL_STR attestation_status;
  280. PAL_STR attestation_timestamp;
  281. /* Purely for profiling */
  282. PAL_NUM startup_time;
  283. PAL_NUM host_specific_startup_time;
  284. PAL_NUM relocation_time;
  285. PAL_NUM linking_time;
  286. PAL_NUM manifest_loading_time;
  287. PAL_NUM allocation_time;
  288. PAL_NUM tail_startup_time;
  289. PAL_NUM child_creation_time;
  290. } PAL_CONTROL;
  291. #define pal_control (*pal_control_addr())
  292. PAL_CONTROL * pal_control_addr (void);
  293. /* The ABI includes three calls to allocate, free, and modify the
  294. * permission bits on page-base virtual memory. Permissions in-
  295. * clude read, write, execute, and guard. Memory regions can be
  296. * unallocated, reserved, or backed by committed memory
  297. */
  298. /* Memory Allocation Flags */
  299. #define PAL_ALLOC_RESERVE 0x0001 /* Only reserve the memory */
  300. #ifdef IN_PAL
  301. #define PAL_ALLOC_INTERNAL 0x8000
  302. #endif
  303. /* Memory Protection Flags */
  304. #define PAL_PROT_NONE 0x0 /* 0x0 Page can not be accessed. */
  305. #define PAL_PROT_READ 0x1 /* 0x1 Page can be read. */
  306. #define PAL_PROT_WRITE 0x2 /* 0x2 Page can be written. */
  307. #define PAL_PROT_EXEC 0x4 /* 0x4 Page can be executed. */
  308. #define PAL_PROT_WRITECOPY 0x8 /* 0x8 Copy on write */
  309. #define PAL_PROT_MASK 0xF
  310. // If addr != NULL, then the returned region is always exactly at addr.
  311. PAL_PTR
  312. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  313. PAL_FLG prot);
  314. void
  315. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size);
  316. PAL_BOL
  317. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot);
  318. /* The ABI includes one call to create a child process and one call to
  319. * terminate the running process. A child process does not inherit
  320. * any objects or memory from its parent process and the parent
  321. * process may not modify the execution of its children. A parent can
  322. * wait for a child to exit using its handle. Parent and child may
  323. * communicate through I/O streams provided by the parent to the
  324. * child at creation
  325. */
  326. #define PAL_PROCESS_MASK 0x0
  327. PAL_HANDLE
  328. DkProcessCreate (PAL_STR uri, PAL_STR * args);
  329. /*!
  330. * \brief Magic exit code that instructs the exiting process to wait for its children
  331. *
  332. * Required for a corner case when the parent exec's the child in a new Graphene process: for
  333. * correctness, the parent cannot immediately exit since it may have a parent that waits on it.
  334. * If an application by coincidence picks this magic number as its exit code, it is changed to
  335. * another exit code so as to not confuse the PAL code.
  336. */
  337. #define PAL_WAIT_FOR_CHILDREN_EXIT (1024 * 1024)
  338. noreturn void
  339. DkProcessExit (PAL_NUM exitCode);
  340. /* The stream ABI includes nine calls to open, read, write, map, unmap,
  341. * truncate, flush, delete and wait for I/O streams and three calls to
  342. * access metadata about an I/O stream. The ABI purposefully does not
  343. * provide an ioctl call. Supported URI schemes include file:, pipe:,
  344. * http:, https:, tcp:, udp:, pipe.srv:, http.srv, tcp.srv:, and udp.srv:.
  345. * The latter four schemes are used to open inbound I/O streams for
  346. * server applications.
  347. */
  348. /* DkStreamOpen
  349. * access_mode: WRONLY or RDONLY or RDWR
  350. * share_flags: permission for the created file
  351. * create_flags: the creation options for the file
  352. * options: other options
  353. */
  354. /* Stream Access Flags */
  355. #define PAL_ACCESS_RDONLY 00
  356. #define PAL_ACCESS_WRONLY 01
  357. #define PAL_ACCESS_RDWR 02
  358. #define PAL_ACCESS_APPEND 04
  359. #define PAL_ACCESS_MASK 07
  360. /* Stream Sharing Flags */
  361. #define PAL_SHARE_GLOBAL_X 01
  362. #define PAL_SHARE_GLOBAL_W 02
  363. #define PAL_SHARE_GLOBAL_R 04
  364. #define PAL_SHARE_GROUP_X 010
  365. #define PAL_SHARE_GROUP_W 020
  366. #define PAL_SHARE_GROUP_R 040
  367. #define PAL_SHARE_OWNER_X 0100
  368. #define PAL_SHARE_OWNER_W 0200
  369. #define PAL_SHARE_OWNER_R 0400
  370. #define PAL_SHARE_MASK 0777
  371. /* Stream Create Flags */
  372. #define PAL_CREATE_TRY 0100 /* Create file if file not exist (O_CREAT) */
  373. #define PAL_CREATE_ALWAYS 0200 /* Create file and fail if file already exist (O_CREAT|O_EXCL) */
  374. #define PAL_CREATE_DUALSTACK 0400 /* Create dual-stack socket (opposite of IPV6_V6ONLY) */
  375. #define PAL_CREATE_MASK 0700
  376. /* Stream Option Flags */
  377. #define PAL_OPTION_NONBLOCK 04000
  378. #define PAL_OPTION_MASK 04000
  379. /* CLOEXEC is generic for any stream.
  380. * SEMAPHORE is specific to eventfd syscall. */
  381. #define PAL_OPTION_CLOEXEC 01000
  382. #define PAL_OPTION_EFD_SEMAPHORE 02000
  383. /* error value of read/write */
  384. #define PAL_STREAM_ERROR ((PAL_NUM)-1L)
  385. #define WITHIN_MASK(val, mask) (((val)|(mask)) == (mask))
  386. PAL_HANDLE
  387. DkStreamOpen (PAL_STR uri, PAL_FLG access, PAL_FLG share_flags,
  388. PAL_FLG create, PAL_FLG options);
  389. PAL_HANDLE
  390. DkStreamWaitForClient (PAL_HANDLE handle);
  391. PAL_NUM
  392. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  393. PAL_PTR buffer, PAL_PTR source, PAL_NUM size);
  394. PAL_NUM
  395. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  396. PAL_PTR buffer, PAL_STR dest);
  397. #define PAL_DELETE_RD 01
  398. #define PAL_DELETE_WR 02
  399. void
  400. DkStreamDelete (PAL_HANDLE handle, PAL_FLG access);
  401. PAL_PTR
  402. DkStreamMap (PAL_HANDLE handle, PAL_PTR address, PAL_FLG prot,
  403. PAL_NUM offset, PAL_NUM size);
  404. void
  405. DkStreamUnmap (PAL_PTR addr, PAL_NUM size);
  406. /* Sets the length of the file referenced by handle to length. Returns the 0
  407. * on success, a _positive_ errno on failure.
  408. */
  409. PAL_NUM
  410. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length);
  411. PAL_BOL
  412. DkStreamFlush (PAL_HANDLE handle);
  413. PAL_BOL
  414. DkSendHandle (PAL_HANDLE handle, PAL_HANDLE cargo);
  415. PAL_HANDLE
  416. DkReceiveHandle (PAL_HANDLE handle);
  417. /* stream attribute structure */
  418. typedef struct {
  419. PAL_IDX handle_type;
  420. PAL_BOL disconnected;
  421. PAL_BOL nonblocking;
  422. PAL_BOL readable, writable, runnable;
  423. PAL_BOL secure;
  424. PAL_FLG share_flags;
  425. PAL_NUM pending_size;
  426. PAL_IDX no_of_fds;
  427. PAL_IDX fds[MAX_FDS];
  428. union {
  429. struct {
  430. PAL_NUM linger;
  431. PAL_NUM receivebuf, sendbuf;
  432. PAL_NUM receivetimeout, sendtimeout;
  433. PAL_BOL tcp_cork;
  434. PAL_BOL tcp_keepalive;
  435. PAL_BOL tcp_nodelay;
  436. } socket;
  437. };
  438. } PAL_STREAM_ATTR;
  439. PAL_BOL
  440. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr);
  441. PAL_BOL
  442. DkStreamAttributesQueryByHandle (PAL_HANDLE handle,
  443. PAL_STREAM_ATTR * attr);
  444. PAL_BOL
  445. DkStreamAttributesSetByHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  446. PAL_NUM
  447. DkStreamGetName (PAL_HANDLE handle, PAL_PTR buffer, PAL_NUM size);
  448. PAL_BOL
  449. DkStreamChangeName (PAL_HANDLE handle, PAL_STR uri);
  450. /* The ABI supports multithreading through five calls to create,
  451. * sleep, yield the scheduler quantum for, resume execution of, and
  452. * terminate threads, as well as seven calls to create, signal, and
  453. * block on synchronization objects
  454. */
  455. #define PAL_THREAD_MASK 0
  456. PAL_HANDLE
  457. DkThreadCreate (PAL_PTR addr, PAL_PTR param);
  458. // assuming duration to be in microseconds
  459. PAL_NUM
  460. DkThreadDelayExecution (PAL_NUM duration);
  461. void
  462. DkThreadYieldExecution (void);
  463. noreturn void DkThreadExit(PAL_PTR clear_child_tid);
  464. PAL_BOL
  465. DkThreadResume (PAL_HANDLE thread);
  466. /* Exception Handling */
  467. /* arithmetic error (div-by-zero, floating point exception, etc.) */
  468. #define PAL_EVENT_ARITHMETIC_ERROR 1
  469. /* segmentation fault, protection fault, bus fault */
  470. #define PAL_EVENT_MEMFAULT 2
  471. /* illegal instructions */
  472. #define PAL_EVENT_ILLEGAL 3
  473. /* terminated by external program */
  474. #define PAL_EVENT_QUIT 4
  475. /* suspended by external program */
  476. #define PAL_EVENT_SUSPEND 5
  477. /* continued by external program */
  478. #define PAL_EVENT_RESUME 6
  479. /* failure within PAL calls */
  480. #define PAL_EVENT_FAILURE 7
  481. #define PAL_EVENT_NUM_BOUND 8
  482. #define PAL_EVENT_PRIVATE 0x0001 /* upcall specific to thread */
  483. #define PAL_EVENT_RESET 0x0002 /* reset the event upcall */
  484. typedef void (*PAL_EVENT_HANDLER) (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT *);
  485. PAL_BOL
  486. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event);
  487. void DkExceptionReturn (PAL_PTR event);
  488. /* parameter: keeping int threadHandle for now (to be in sync with the paper).
  489. * We may want to replace it with a PAL_HANDLE. Ideally, either use PAL_HANDLE
  490. * or threadHandle.
  491. */
  492. /* Create a Mutex.
  493. * initialCount of 0 is totally unlocked; an initialCount of 1
  494. * is initialized to locked. */
  495. PAL_HANDLE
  496. DkMutexCreate (PAL_NUM initialCount);
  497. /* Destroy a mutex using DkObjectClose */
  498. void
  499. DkMutexRelease (PAL_HANDLE mutexHandle);
  500. PAL_HANDLE
  501. DkNotificationEventCreate (PAL_BOL initialState);
  502. PAL_HANDLE
  503. DkSynchronizationEventCreate (PAL_BOL initialState);
  504. void
  505. DkEventSet (PAL_HANDLE eventHandle);
  506. void
  507. DkEventClear (PAL_HANDLE eventHandle);
  508. #define NO_TIMEOUT ((PAL_NUM)-1)
  509. PAL_BOL DkSynchronizationObjectWait(PAL_HANDLE handle, PAL_NUM timeout_us);
  510. #define PAL_WAIT_SIGNAL 1 /* ignored in events */
  511. #define PAL_WAIT_READ 2
  512. #define PAL_WAIT_WRITE 4
  513. #define PAL_WAIT_ERROR 8 /* ignored in events */
  514. PAL_BOL DkStreamsWaitEvents(PAL_NUM count, PAL_HANDLE* handle_array, PAL_FLG* events,
  515. PAL_FLG* ret_events, PAL_NUM timeout_us);
  516. /* Deprecate DkObjectReference */
  517. void DkObjectClose (PAL_HANDLE objectHandle);
  518. /* the ABI includes seven assorted calls to get wall clock
  519. * time, generate cryptographically-strong random bits, flush por-
  520. * tions of instruction caches, increment and decrement the reference
  521. * counts on objects shared between threads, and to coordinate
  522. * threads with the security monitor during process serialization
  523. */
  524. /* assuming the time to be in microseconds */
  525. PAL_NUM
  526. DkSystemTimeQuery (void);
  527. /*
  528. * Cryptographically secure random.
  529. * 0 on success, negative on failure.
  530. */
  531. PAL_NUM
  532. DkRandomBitsRead (PAL_PTR buffer, PAL_NUM size);
  533. PAL_BOL
  534. DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size);
  535. #define PAL_SEGMENT_FS 0x1
  536. #define PAL_SEGMENT_GS 0x2
  537. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr);
  538. PAL_NUM DkMemoryAvailableQuota (void);
  539. #define PAL_CPUID_WORD_EAX 0
  540. #define PAL_CPUID_WORD_EBX 1
  541. #define PAL_CPUID_WORD_ECX 2
  542. #define PAL_CPUID_WORD_EDX 3
  543. #define PAL_CPUID_WORD_NUM 4
  544. PAL_BOL
  545. DkCpuIdRetrieve (PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4]);
  546. #ifdef __GNUC__
  547. # define symbol_version_default(real, name, version) \
  548. __asm__ (".symver " #real "," #name "@@" #version "\n")
  549. #else
  550. # define symbol_version_default(real, name, version)
  551. #endif
  552. #endif /* PAL_H */