pal_internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. #ifndef IN_PAL
  26. # error "pal_internal.h can only be included in PAL"
  27. #endif
  28. #define PAL_FILE(name) XSTRINGIFY(PAL_DIR) "/" name
  29. #define RUNTIME_FILE(name) XSTRINGIFY(RUNTIME_DIR) "/" name
  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, uint64_t offset,
  64. uint64_t size);
  65. /* 'setlength' is used by DkStreamFlush. It truncate the stream
  66. to certain size. */
  67. int64_t (*setlength) (PAL_HANDLE handle, uint64_t 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. * Time is in microseconds, NO_TIMEOUT means no timeout.
  85. * Returns 0 on success, a negative value on failure.
  86. * Timeout: -PAL_ERROR_TRYAGAIN
  87. * Positive return values are undefined.
  88. */
  89. int (*wait) (PAL_HANDLE handle, uint64_t time);
  90. /* 'rename' is used to change name of a stream, or reset its share
  91. option */
  92. int (*rename) (PAL_HANDLE handle, const char * type, const char * uri);
  93. };
  94. extern const struct handle_ops * pal_handle_ops [];
  95. static inline const struct handle_ops * HANDLE_OPS (PAL_HANDLE handle)
  96. {
  97. int _type = PAL_GET_TYPE(handle);
  98. if (_type <= 0 || _type >= PAL_HANDLE_TYPE_BOUND)
  99. return NULL;
  100. return pal_handle_ops[_type];
  101. }
  102. /* interger hash functions defined inline. The algorithm we used here
  103. is based on Robert Jenkins developed in 96', the algorithm has two
  104. version, 32-bit one and 64-bit one. */
  105. static inline unsigned int hash32 (unsigned int key)
  106. {
  107. key = ~key + (key << 15);
  108. key = key ^ (key >> 12);
  109. key = key + (key << 2);
  110. key = key ^ (key >> 4);
  111. key = (key + (key << 3)) + (key << 11);
  112. key = key ^ (key >> 16);
  113. return key;
  114. }
  115. static inline unsigned int hash64 (unsigned long key)
  116. {
  117. key = (~key) + (key << 21);
  118. key = key ^ (key >> 24);
  119. key = (key + (key << 3)) + (key << 8);
  120. key = key ^ (key >> 14);
  121. key = (key + (key << 2)) + (key << 4);
  122. key = key ^ (key >> 28);
  123. key = key + (key << 31);
  124. return key;
  125. }
  126. /* We allow dynamic size handle allocation. Here is some macro to help
  127. deciding the actual size of the handle */
  128. extern PAL_HANDLE _h;
  129. #define HANDLE_SIZE(type) (sizeof(*_h))
  130. #define UNKNOWN_HANDLE(handle) \
  131. (PAL_GET_TYPE(handle) == 0 || PAL_GET_TYPE(handle) >= PAL_HANDLE_TYPE_BOUND)
  132. static inline int handle_size (PAL_HANDLE handle)
  133. {
  134. return sizeof(*handle);
  135. }
  136. #ifndef ENTER_PAL_CALL
  137. # define ENTER_PAL_CALL(name)
  138. #endif
  139. #ifndef LEAVE_PAL_CALL
  140. # define LEAVE_PAL_CALL()
  141. #endif
  142. #ifndef LEAVE_PAL_CALL_RETURN
  143. # define LEAVE_PAL_CALL_RETURN(retval) do { return (retval); } while (0)
  144. #endif
  145. /* failure notify. The rountine is called whenever a PAL call return
  146. error code. As the current design of PAL does not return error
  147. code directly, we rely on DkAsynchronousEventUpcall to handle
  148. PAL call error. If the user does not set up a upcall, the error
  149. code will be ignored. Ignoring PAL error code can be a possible
  150. optimization for SHIM. */
  151. void notify_failure (unsigned long error);
  152. /* all pal config value */
  153. extern struct pal_internal_state {
  154. PAL_NUM instance_id;
  155. PAL_HANDLE parent_process;
  156. const char * manifest;
  157. PAL_HANDLE manifest_handle;
  158. const char * exec;
  159. PAL_HANDLE exec_handle;
  160. PAL_HANDLE log_stream;
  161. enum {
  162. LOG_FILE = 0x01,
  163. LOG_PIPE = 0x02,
  164. LOG_SOCKET = 0x04,
  165. LOG_GENERIC_TYPES = 0x08,
  166. } log_types;
  167. struct config_store * root_config;
  168. unsigned long pagesize;
  169. unsigned long alloc_align, alloc_shift, alloc_mask;
  170. PAL_HANDLE console;
  171. unsigned long start_time;
  172. #if PROFILING == 1
  173. unsigned long relocation_time;
  174. unsigned long linking_time;
  175. unsigned long manifest_loading_time;
  176. unsigned long slab_time;
  177. unsigned long tail_startup_time;
  178. unsigned long process_create_time;
  179. #endif
  180. } pal_state;
  181. #ifdef __GNUC__
  182. #define BREAK() \
  183. do { \
  184. asm volatile ("int $3"); \
  185. } while (0)
  186. #else
  187. #define BREAK()
  188. #endif
  189. extern PAL_CONTROL __pal_control;
  190. #define ALLOC_ALIGNDOWN(addr) \
  191. (((unsigned long) (addr)) & pal_state.alloc_mask)
  192. #define ALLOC_ALIGNUP(addr) \
  193. ((((unsigned long) (addr)) + pal_state.alloc_shift) & pal_state.alloc_mask)
  194. #define ALLOC_ALIGNED(addr) \
  195. ((unsigned long) (addr) == \
  196. (((unsigned long) (addr)) & pal_state.alloc_mask))
  197. /* Main initialization function */
  198. void pal_main (
  199. PAL_NUM instance_id, /* current instance id */
  200. PAL_HANDLE manifest_handle, /* manifest handle if opened */
  201. PAL_HANDLE exec_handle, /* executable handle if opened */
  202. PAL_PTR exec_loaded_addr, /* executable addr if loaded */
  203. PAL_HANDLE parent_process, /* parent process if it's a child */
  204. PAL_HANDLE first_thread, /* first thread handle */
  205. PAL_STR * arguments, /* application arguments */
  206. PAL_STR * environments /* environment variables */
  207. );
  208. /* For initialization */
  209. unsigned long _DkGetPagesize (void);
  210. unsigned long _DkGetAllocationAlignment (void);
  211. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end);
  212. bool _DkCheckMemoryMappable (const void * addr, int size);
  213. PAL_NUM _DkGetProcessId (void);
  214. PAL_NUM _DkGetHostId (void);
  215. unsigned long _DkMemoryQuota (void);
  216. unsigned long _DkMemoryAvailableQuota (void);
  217. void _DkGetCPUInfo (PAL_CPU_INFO * info);
  218. /* Internal DK calls, in case any of the internal routines needs to use them */
  219. /* DkStream calls */
  220. int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
  221. int access, int share, int create, int options);
  222. int _DkStreamDelete (PAL_HANDLE handle, int access);
  223. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  224. char * addr, int addrlen);
  225. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count,
  226. const void * buf, const char * addr, int addrlen);
  227. int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr);
  228. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr);
  229. int _DkStreamMap (PAL_HANDLE handle, void ** addr, int prot, uint64_t offset,
  230. uint64_t size);
  231. int _DkStreamUnmap (void * addr, uint64_t size);
  232. int64_t _DkStreamSetLength (PAL_HANDLE handle, uint64_t length);
  233. int _DkStreamFlush (PAL_HANDLE handle);
  234. int _DkStreamGetName (PAL_HANDLE handle, char * buf, int size);
  235. const char * _DkStreamRealpath (PAL_HANDLE hdl);
  236. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo);
  237. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo);
  238. PAL_HANDLE _DkBroadcastStreamOpen (void);
  239. /* DkProcess and DkThread calls */
  240. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  241. const void * parem, int flags);
  242. void _DkThreadExit (void);
  243. int _DkThreadDelayExecution (unsigned long * duration);
  244. void _DkThreadYieldExecution (void);
  245. int _DkThreadResume (PAL_HANDLE threadHandle);
  246. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,
  247. int flags, const char ** args);
  248. void _DkProcessExit (int exitCode);
  249. int _DkProcessSandboxCreate (const char * manifest, int flags);
  250. /* DkMutex calls */
  251. int _DkMutexCreate (PAL_HANDLE * handle, int initialCount);
  252. int _DkMutexAcquire (PAL_HANDLE sem);
  253. int _DkMutexAcquireTimeout (PAL_HANDLE sem, int timeout);
  254. void _DkMutexRelease (PAL_HANDLE sem);
  255. int _DkMutexGetCurrentCount (PAL_HANDLE sem);
  256. /* DkEvent calls */
  257. int _DkEventCreate (PAL_HANDLE * event, bool initialState,
  258. bool isnotification);
  259. int _DkEventSet (PAL_HANDLE event, int wakeup);
  260. int _DkEventWaitTimeout (PAL_HANDLE event, uint64_t timeout);
  261. int _DkEventWait (PAL_HANDLE event);
  262. int _DkEventClear (PAL_HANDLE event);
  263. /* DkVirtualMemory calls */
  264. int _DkVirtualMemoryAlloc (void ** paddr, uint64_t size, int alloc_type, int prot);
  265. int _DkVirtualMemoryFree (void * addr, uint64_t size);
  266. int _DkVirtualMemoryProtect (void * addr, uint64_t size, int prot);
  267. /* DkObject calls */
  268. int _DkObjectReference (PAL_HANDLE objectHandle);
  269. int _DkObjectClose (PAL_HANDLE objectHandle);
  270. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
  271. PAL_HANDLE * polled);
  272. /* DkException calls & structures */
  273. PAL_EVENT_HANDLER _DkGetExceptionHandler (PAL_NUM event_num);
  274. void _DkRaiseFailure (int error);
  275. void _DkExceptionReturn (void * event);
  276. /* other DK calls */
  277. int _DkInternalLock (PAL_LOCK * mut);
  278. int _DkInternalUnlock (PAL_LOCK * mut);
  279. unsigned long _DkSystemTimeQuery (void);
  280. int _DkFastRandomBitsRead (void * buffer, int size);
  281. int _DkRandomBitsRead (void * buffer, int size);
  282. int _DkSegmentRegisterSet (int reg, const void * addr);
  283. int _DkSegmentRegisterGet (int reg, void ** addr);
  284. int _DkInstructionCacheFlush (const void * addr, int size);
  285. int _DkCreatePhysicalMemoryChannel (PAL_HANDLE * handle, unsigned long * key);
  286. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
  287. PAL_PTR * addrs, PAL_NUM * sizes, int flags);
  288. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
  289. PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots);
  290. int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf, unsigned int values[4]);
  291. #define init_fail(exitcode, reason) \
  292. do { \
  293. printf("PAL failed at " __FILE__ ":%s:%u (exitcode = %u, reason=%s)\n", \
  294. __FUNCTION__, (unsigned int)__LINE__, \
  295. (unsigned int) (exitcode), (const char *) (reason)); \
  296. _DkProcessExit(exitcode); \
  297. } while (0)
  298. /* function and definition for loading binaries */
  299. enum object_type { OBJECT_RTLD, OBJECT_EXEC, OBJECT_PRELOAD, OBJECT_EXTERNAL };
  300. int check_elf_object (PAL_HANDLE handle);
  301. int load_elf_object (const char * uri, enum object_type type);
  302. int load_elf_object_by_handle (PAL_HANDLE handle, enum object_type type);
  303. int add_elf_object(void * addr, PAL_HANDLE handle, int type);
  304. #ifndef NO_INTERNAL_ALLOC
  305. void init_slab_mgr (int alignment);
  306. void * malloc (size_t size);
  307. void * remalloc (const void * mem, size_t size);
  308. void * calloc (size_t nmem, size_t size);
  309. char * strdup(const char *source);
  310. void free (void * mem);
  311. #endif
  312. #ifdef __GNUC__
  313. # define __attribute_hidden __attribute__ ((visibility ("hidden")))
  314. # define __attribute_always_inline __attribute__((always_inline))
  315. # define __attribute_unused __attribute__((unused))
  316. # define __attribute_noinline __attribute__((noinline))
  317. #else
  318. # define __attribute_hidden
  319. # define __attribute_always_inline
  320. # define __attribute_unused
  321. # define __attribute_noinline
  322. #endif
  323. #define alias_str(name) #name
  324. #ifdef __GNUC__
  325. # define extern_alias(name) \
  326. extern __typeof(name) pal_##name __attribute ((alias (alias_str(name))))
  327. #else
  328. # define extern_alias(name)
  329. #endif
  330. void _DkPrintConsole (const void * buf, int size);
  331. int printf (const char *fmt, ...);
  332. #include <stdarg.h>
  333. int vprintf(const char * fmt, va_list *ap);
  334. void write_log (int nstrs, ...);
  335. static inline void log_stream (const char * uri)
  336. {
  337. if (!uri || !pal_state.log_stream)
  338. return;
  339. bool logging = false;
  340. if ((pal_state.log_types & LOG_FILE) &&
  341. uri[0] == 'f' && uri[1] == 'i' && uri[2] == 'l' && uri[3] == 'e')
  342. logging = true;
  343. if ((pal_state.log_types & LOG_PIPE) &&
  344. uri[0] == 'p' && uri[1] == 'i' && uri[2] == 'p' && uri[3] == 'e')
  345. logging = true;
  346. if ((pal_state.log_types & LOG_SOCKET) &&
  347. uri[0] == 't' && uri[1] == 'c' && uri[2] == 'p')
  348. logging = true;
  349. if ((pal_state.log_types & LOG_SOCKET) &&
  350. uri[0] == 'u' && uri[1] == 'd' && uri[2] == 'p')
  351. logging = true;
  352. if (logging)
  353. write_log(2, uri, "\n");
  354. }
  355. #endif