pal_internal.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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_internal.h
  17. *
  18. * This file contains definition of functions, variables and data structures
  19. * for internal uses.
  20. */
  21. #ifndef PAL_INTERNAL_H
  22. #define PAL_INTERNAL_H
  23. #include "pal_defs.h"
  24. #include "pal.h"
  25. #include "atomic.h"
  26. #include "api.h"
  27. #ifndef IN_PAL
  28. # error "pal_internal.h can only be included in PAL"
  29. #endif
  30. /* handle_ops is the operators provided for each handler type. They are
  31. mostly used by Stream-related PAL calls, but can also be used by
  32. some others in special ways. */
  33. struct handle_ops {
  34. /* 'getrealpath' return the real path that represent the handle */
  35. const char * (*getrealpath) (PAL_HANDLE handle);
  36. /* 'getname' is used by DkStreamGetName. It's different from
  37. 'getrealpath' */
  38. int (*getname) (PAL_HANDLE handle, char * buffer, int count);
  39. /* 'open' is used by DkStreamOpen. 'handle' is a preallocated handle,
  40. 'type' will be a normalized prefix, 'uri' is the remaining string
  41. of uri */
  42. int (*open) (PAL_HANDLE * handle, const char * type, const char * uri,
  43. int access, int share, int create, int options);
  44. /* 'read' and 'write' is used by DkStreamRead and DkStreamWrite, so
  45. they have exactly same prototype as them. */
  46. int (*read) (PAL_HANDLE handle, int offset, int count, void * buffer);
  47. int (*write) (PAL_HANDLE handle, int offset, int count,
  48. const void * buffer);
  49. /* 'readbyaddr' and 'writebyaddr' are the same as read and write,
  50. but with extra field to specify address */
  51. int (*readbyaddr) (PAL_HANDLE handle, int offset, int count, void * buffer,
  52. char * addr, int addrlen);
  53. int (*writebyaddr) (PAL_HANDLE handle, int offset, int count,
  54. const void * buffer, const char * addr, int addrlen);
  55. /* 'close' and 'delete' is used by DkObjectClose and DkStreamDelete,
  56. 'close' will close the stream, while 'delete' actually destroy
  57. the stream, such as deleting a file or shutting down a socket */
  58. int (*close) (PAL_HANDLE handle);
  59. int (*delete) (PAL_HANDLE handle, int access);
  60. /* 'map' and 'unmap' will map or unmap the handle into memory space,
  61. it's not necessary mapped by mmap, so unmap also needs 'handle'
  62. to deal with special cases */
  63. int (*map) (PAL_HANDLE handle, void ** address, int prot, int offset,
  64. int size);
  65. /* 'setlength' is used by DkStreamFlush. It truncate the stream
  66. to certain size. */
  67. int (*setlength) (PAL_HANDLE handle, int length);
  68. /* 'flush' is used by DkStreamFlush. It syncs the stream to the device */
  69. int (*flush) (PAL_HANDLE handle);
  70. /* 'waitforclient' is used by DkStreamWaitforClient. It accepts an
  71. connection */
  72. int (*waitforclient) (PAL_HANDLE server, PAL_HANDLE *client);
  73. /* 'attrquery' is used by DkStreamAttributesQuery. It queries the
  74. attributes of a stream */
  75. int (*attrquery) (const char * type, const char * uri,
  76. PAL_STREAM_ATTR * attr);
  77. /* 'attrquerybyhdl' is used by DkStreamAttributesQuerybyHandle. It queries
  78. the attributes of a stream handle */
  79. int (*attrquerybyhdl) (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  80. /* 'attrsetbyhdl' is used by DkStreamAttributesSetbyHandle. It queries
  81. the attributes of a stream handle */
  82. int (*attrsetbyhdl) (PAL_HANDLE handle, PAL_STREAM_ATTR * attr);
  83. /* 'wait' is used for synchronous wait */
  84. int (*wait) (PAL_HANDLE handle, int time);
  85. /* 'rename' is used to change name of a stream, or reset its share
  86. option */
  87. int (*rename) (PAL_HANDLE handle, const char * type, const char * uri);
  88. };
  89. extern const struct handle_ops * pal_handle_ops [];
  90. #define HANDLE_OPS(handle) \
  91. ({ int _type = PAL_GET_TYPE(handle); \
  92. (_type <= 0 || _type >= PAL_HANDLE_TYPE_BOUND) ? \
  93. NULL : pal_handle_ops[_type]; \
  94. })
  95. /* interger hash functions defined inline. The algorithm we used here
  96. is based on Robert Jenkins developed in 96', the algorithm has two
  97. version, 32-bit one and 64-bit one. */
  98. static inline uint32_t hash32 (uint32_t key)
  99. {
  100. key = ~key + (key << 15);
  101. key = key ^ (key >> 12);
  102. key = key + (key << 2);
  103. key = key ^ (key >> 4);
  104. key = (key + (key << 3)) + (key << 11);
  105. key = key ^ (key >> 16);
  106. return key;
  107. }
  108. static inline uint64_t hash64 (uint64_t key)
  109. {
  110. key = (~key) + (key << 21);
  111. key = key ^ (key >> 24);
  112. key = (key + (key << 3)) + (key << 8);
  113. key = key ^ (key >> 14);
  114. key = (key + (key << 2)) + (key << 4);
  115. key = key ^ (key >> 28);
  116. key = key + (key << 31);
  117. return key;
  118. }
  119. /* We allow dynamic size handle allocation. Here is some macro to help
  120. deciding the actual size of the handle */
  121. #define HANDLE_SIZE(type) ({ PAL_HANDLE _h; sizeof(_h->type); })
  122. #define UNKNOWN_HANDLE(handle) \
  123. (PAL_GET_TYPE(handle) == 0 || PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND)
  124. static inline int handle_size (PAL_HANDLE handle)
  125. {
  126. static int handle_sizes[PAL_HANDLE_TYPE_BOUND]
  127. = { 0,
  128. [pal_type_file] = sizeof(handle->file),
  129. [pal_type_pipe] = sizeof(handle->pipe),
  130. [pal_type_pipesrv] = sizeof(handle->pipe),
  131. [pal_type_pipecli] = sizeof(handle->pipe),
  132. [pal_type_pipeprv] = sizeof(handle->pipeprv),
  133. [pal_type_dev] = sizeof(handle->dev),
  134. [pal_type_dir] = sizeof(handle->dir),
  135. [pal_type_tcp] = sizeof(handle->sock),
  136. [pal_type_tcpsrv] = sizeof(handle->sock),
  137. [pal_type_udp] = sizeof(handle->sock),
  138. [pal_type_udpsrv] = sizeof(handle->sock),
  139. [pal_type_process] = sizeof(handle->process),
  140. [pal_type_mcast] = sizeof(handle->mcast),
  141. [pal_type_thread] = sizeof(handle->thread),
  142. [pal_type_semaphore] = sizeof(handle->semaphore),
  143. [pal_type_event] = sizeof(handle->event),
  144. [pal_type_gipc] = sizeof(handle->gipc),
  145. };
  146. if (UNKNOWN_HANDLE(handle))
  147. return 0;
  148. else
  149. return handle_sizes[PAL_GET_TYPE(handle)];
  150. }
  151. #ifdef __x86_64__
  152. # ifdef OMIT_FRAME_POINTER
  153. # define store_stack(rsp) \
  154. void * rsp; \
  155. do { \
  156. asm volatile ("movq %%rsp, %0\r\n" \
  157. : "=g"(rsp) :: "memory"); \
  158. } while(0)
  159. # else
  160. # define store_stack(rsp, rbp) \
  161. void * rsp, * rbp; \
  162. do { \
  163. asm volatile ("movq %%rsp, %0\r\n" \
  164. "movq %%rbp, %1\r\n" \
  165. : "=g"(rsp), "=g"(rbp) :: "memory"); \
  166. } while(0)
  167. # endif
  168. struct arch_frame {
  169. unsigned long rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
  170. };
  171. # define store_register(reg, var) \
  172. asm volatile ("movq %%" #reg ", %0" : "=g" (var) :: "memory");
  173. # define store_register_in_frame(reg, f) store_register(reg, (f)->reg)
  174. # define arch_store_frame(f) \
  175. store_register_in_frame(rsp, f) \
  176. store_register_in_frame(rbp, f) \
  177. store_register_in_frame(rbx, f) \
  178. store_register_in_frame(rsi, f) \
  179. store_register_in_frame(rdi, f) \
  180. store_register_in_frame(r12, f) \
  181. store_register_in_frame(r13, f) \
  182. store_register_in_frame(r14, f) \
  183. store_register_in_frame(r15, f)
  184. # define restore_register(reg, var, clobber...) \
  185. asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
  186. # define restore_register_in_frame(reg, f) \
  187. restore_register(reg, (f)->reg, \
  188. "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
  189. # define arch_restore_frame(f) \
  190. restore_register_in_frame(r15, f) \
  191. restore_register_in_frame(r14, f) \
  192. restore_register_in_frame(r13, f) \
  193. restore_register_in_frame(r12, f) \
  194. restore_register_in_frame(rdi, f) \
  195. restore_register_in_frame(rsi, f) \
  196. restore_register_in_frame(rbx, f) \
  197. restore_register_in_frame(rbp, f) \
  198. restore_register_in_frame(rsp, f)
  199. #endif /* __x86_64__ */
  200. struct pal_frame {
  201. volatile struct pal_frame * self;
  202. void * func;
  203. const char * funcname;
  204. struct arch_frame arch;
  205. };
  206. static inline
  207. void __store_frame (struct pal_frame * frame,
  208. void * func, const char * funcname)
  209. {
  210. *(volatile void **) &frame->self = frame;
  211. frame->func = func;
  212. frame->funcname = funcname;
  213. arch_store_frame(&frame->arch)
  214. }
  215. static inline
  216. void __clear_frame (struct pal_frame * frame)
  217. {
  218. if (*(volatile void **) &frame->self == frame) {
  219. asm volatile ("nop" ::: "memory");
  220. *(volatile void **) &frame->self = NULL;
  221. asm volatile ("nop" ::: "memory");
  222. }
  223. }
  224. #define store_frame(f) \
  225. struct pal_frame frame; \
  226. __store_frame(&frame, &Dk##f, "Dk" #f)
  227. #define leave_frame(ret, err) \
  228. do { \
  229. __clear_frame(&frame); \
  230. if (err) _DkRaiseFailure(err); \
  231. return ret; \
  232. } while (0)
  233. /* failure notify. The rountine is called whenever a PAL call return
  234. error code. As the current design of PAL does not return error
  235. code directly, we rely on DkAsynchronousEventUpcall to handle
  236. PAL call error. If the user does not set up a upcall, the error
  237. code will be ignored. Ignoring PAL error code can be a possible
  238. optimization for SHIM. */
  239. void notify_failure (unsigned long error);
  240. /* all pal config value */
  241. extern struct pal_internal_state {
  242. PAL_NUM pal_token;
  243. void * pal_addr;
  244. PAL_HANDLE parent_handle;
  245. const char * manifest;
  246. PAL_HANDLE manifest_handle;
  247. const char * exec;
  248. PAL_HANDLE exec_handle;
  249. PAL_HANDLE log_stream;
  250. enum {
  251. LOG_FILE = 0x01,
  252. LOG_PIPE = 0x02,
  253. LOG_SOCKET = 0x04,
  254. LOG_GENERIC_TYPES = 0x08,
  255. } log_types;
  256. struct config_store * root_config;
  257. unsigned long pagesize;
  258. unsigned long alloc_align, alloc_shift, alloc_mask;
  259. PAL_HANDLE console;
  260. const char * syscall_sym_name;
  261. void * syscall_sym_addr;
  262. unsigned long start_time;
  263. #if PROFILING == 1
  264. unsigned long relocation_time;
  265. unsigned long linking_time;
  266. unsigned long manifest_loading_time;
  267. unsigned long slab_time;
  268. unsigned long tail_startup_time;
  269. unsigned long process_create_time;
  270. #endif
  271. } pal_state;
  272. #define BREAK() \
  273. do { \
  274. asm volatile ("int $3"); \
  275. } while (0)
  276. extern PAL_CONTROL __pal_control;
  277. #define ALLOC_ALIGNDOWN(addr) \
  278. ({ \
  279. assert(pal_state.alloc_mask); \
  280. ((unsigned long) (addr)) & pal_state.alloc_mask; \
  281. })
  282. #define ALLOC_ALIGNUP(addr) \
  283. ({ \
  284. assert(pal_state.alloc_shift && pal_state.alloc_mask); \
  285. (((unsigned long) (addr)) + pal_state.alloc_shift) & pal_state.alloc_mask; \
  286. })
  287. #define ALLOC_ALIGNED(addr) \
  288. ({ \
  289. assert(pal_state.alloc_mask); \
  290. (unsigned long) (addr) == \
  291. (((unsigned long) (addr)) & pal_state.alloc_mask); \
  292. })
  293. /* For initialization */
  294. unsigned long _DkGetPagesize (void);
  295. unsigned long _DkGetAllocationAlignment (void);
  296. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end);
  297. bool _DkCheckMemoryMappable (const void * addr, int size);
  298. PAL_NUM _DkGetProcessId (void);
  299. PAL_NUM _DkGetHostId (void);
  300. unsigned long _DkMemoryQuota (void);
  301. unsigned long _DkMemoryAvailableQuota (void);
  302. typedef typeof(__pal_control.cpu_info) PAL_CPU_INFO;
  303. void _DkGetCPUInfo (PAL_CPU_INFO * info);
  304. /* Main function called by host-dependent bootstrapper */
  305. void pal_main (PAL_NUM pal_token, void * pal_addr, const char * pal_name,
  306. int argc, const char ** argv, const char ** envp,
  307. PAL_HANDLE parent_handle,
  308. PAL_HANDLE thread_handle,
  309. PAL_HANDLE exec_handle,
  310. PAL_HANDLE manifest_handle);
  311. void start_execution (const char * first_argv, int argc, const char ** argv,
  312. const char ** envp);
  313. #include <atomic.h>
  314. int _DkInternalLock (PAL_LOCK * mut);
  315. int _DkInternalUnlock (PAL_LOCK * mut);
  316. /* Internal DK calls, in case any of the internal routines needs to use them */
  317. /* DkStream calls */
  318. int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
  319. int access, int share, int create, int options);
  320. int _DkStreamDelete (PAL_HANDLE handle, int access);
  321. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  322. char * addr, int addrlen);
  323. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count,
  324. const void * buf, const char * addr, int addrlen);
  325. int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr);
  326. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr);
  327. int _DkStreamMap (PAL_HANDLE handle, void ** addr, int prot, int offset,
  328. int size);
  329. int _DkStreamUnmap (void * addr, int size);
  330. int _DkStreamSetLength (PAL_HANDLE handle, int length);
  331. int _DkStreamFlush (PAL_HANDLE handle);
  332. int _DkStreamGetName (PAL_HANDLE handle, char * buf, int size);
  333. const char * _DkStreamRealpath (PAL_HANDLE hdl);
  334. int _DkStreamFile (PAL_HANDLE hdl, PAL_HANDLE * file);
  335. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo);
  336. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo);
  337. PAL_HANDLE _DkBroadcastStreamOpen (void);
  338. /* DkProcess and DkThread calls */
  339. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  340. const void * parem, int flags);
  341. void _DkThreadExit (void);
  342. int _DkThreadDelayExecution (unsigned long * duration);
  343. void _DkThreadYieldExecution (void);
  344. int _DkThreadResume (PAL_HANDLE threadHandle);
  345. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,
  346. int flags, const char ** args);
  347. void _DkProcessExit (int exitCode);
  348. int _DkProcessSandboxCreate (const char * manifest, int flags);
  349. /* DkSemaphore calls */
  350. int _DkSemaphoreCreate (PAL_HANDLE handle, int initialCount, int maxCount);
  351. void _DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle);
  352. int _DkSemaphoreAcquire (PAL_HANDLE sem, int count);
  353. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, int timeout);
  354. void _DkSemaphoreRelease (PAL_HANDLE sem, int count);
  355. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem);
  356. /* DkEvent calls */
  357. int _DkEventCreate (PAL_HANDLE * event, bool initialState,
  358. bool isnotification);
  359. void _DkEventDestroy (PAL_HANDLE handle);
  360. int _DkEventSet (PAL_HANDLE event, int wakeup);
  361. int _DkEventWaitTimeout (PAL_HANDLE event, int timeout);
  362. int _DkEventWait (PAL_HANDLE event);
  363. int _DkEventClear (PAL_HANDLE event);
  364. /* DkVirtualMemory calls */
  365. int _DkVirtualMemoryAlloc (void ** paddr, int size, int alloc_type, int prot);
  366. int _DkVirtualMemoryFree (void * addr, int size);
  367. int _DkVirtualMemoryProtect (void * addr, int size, int prot);
  368. /* DkObject calls */
  369. int _DkObjectReference (PAL_HANDLE objectHandle);
  370. int _DkObjectClose (PAL_HANDLE objectHandle);
  371. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, int timeout,
  372. PAL_HANDLE * polled);
  373. /* DkException calls & structures */
  374. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  375. int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND]) (int, PAL_UPCALL, int);
  376. void _DkRaiseFailure (int error);
  377. void _DkExceptionReturn (const void * event);
  378. /* other DK calls */
  379. unsigned long _DkSystemTimeQuery (void);
  380. int _DkFastRandomBitsRead (void * buffer, int size);
  381. int _DkRandomBitsRead (void * buffer, int size);
  382. int _DkSegmentRegisterSet (int reg, const void * addr);
  383. int _DkSegmentRegisterGet (int reg, void ** addr);
  384. int _DkInstructionCacheFlush (const void * addr, int size);
  385. int _DkCreatePhysicalMemoryChannel (PAL_HANDLE * handle, unsigned long * key);
  386. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
  387. PAL_PTR * addrs, PAL_NUM * sizes, int flags);
  388. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
  389. PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots);
  390. int _DkCpuIdRetrieve (unsigned int leaf, unsigned int values[4]);
  391. unsigned long _DkHandleCompatibilityException (unsigned long syscallno,
  392. unsigned long args[6]);
  393. #define init_fail(exitcode, reason) \
  394. do { \
  395. printf("PAL failed at " __FILE__ ":%s:%u (exitcode = %u, reason=%s)\n", \
  396. __FUNCTION__, (unsigned int)__LINE__, \
  397. (unsigned int) (exitcode), (const char *) (reason)); \
  398. _DkProcessExit(exitcode); \
  399. } while (0)
  400. /* function and definition for loading binaries */
  401. enum object_type { OBJECT_RTLD, OBJECT_EXEC, OBJECT_PRELOAD, OBJECT_EXTERNAL };
  402. int check_elf_object (PAL_HANDLE handle);
  403. int load_elf_object (const char * uri, enum object_type type);
  404. int load_elf_object_by_handle (PAL_HANDLE handle, enum object_type type);
  405. void init_slab_mgr (int alignment);
  406. void * malloc (int size);
  407. void * remalloc (const void * mem, int size);
  408. void * calloc (int nmem, int size);
  409. void free (void * mem);
  410. #define attribute_hidden __attribute__ ((visibility ("hidden")))
  411. #define alias_str(name) #name
  412. #define extern_alias(name) \
  413. extern __typeof(name) pal_##name __attribute ((alias (alias_str(name))))
  414. void _DkPrintConsole (const void * buf, int size);
  415. int printf (const char *fmt, ...);
  416. void write_log (int nstrs, ...);
  417. static inline void log_stream (const char * uri)
  418. {
  419. if (!uri || !pal_state.log_stream)
  420. return;
  421. bool logging = false;
  422. if ((pal_state.log_types & LOG_FILE) &&
  423. uri[0] == 'f' && uri[1] == 'i' && uri[2] == 'l' && uri[3] == 'e')
  424. logging = true;
  425. if ((pal_state.log_types & LOG_PIPE) &&
  426. uri[0] == 'p' && uri[1] == 'i' && uri[2] == 'p' && uri[3] == 'e')
  427. logging = true;
  428. if ((pal_state.log_types & LOG_SOCKET) &&
  429. uri[0] == 't' && uri[1] == 'c' && uri[2] == 'p')
  430. logging = true;
  431. if ((pal_state.log_types & LOG_SOCKET) &&
  432. uri[0] == 'u' && uri[1] == 'd' && uri[2] == 'p')
  433. logging = true;
  434. if (logging)
  435. write_log(2, uri, "\n");
  436. }
  437. #endif