pal_linux.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef PAL_LINUX_H
  14. #define PAL_LINUX_H
  15. #include "pal_defs.h"
  16. #include "pal_linux_defs.h"
  17. #include "pal.h"
  18. #include "pal_internal.h"
  19. #include "pal_linux_error.h"
  20. #include "list.h"
  21. #define PAL_LOADER RUNTIME_FILE("pal-Linux")
  22. #include <linux/mman.h>
  23. #include <sys/syscall.h>
  24. #include <sigset.h>
  25. #include <asm/fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #ifdef __x86_64__
  30. # include "sysdep-x86_64.h"
  31. #endif
  32. #define IS_ERR INTERNAL_SYSCALL_ERROR
  33. #define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
  34. #define ERRNO INTERNAL_SYSCALL_ERRNO
  35. #define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
  36. #define GRAPHENE_UNIX_PREFIX_FMT "/graphene/%016lx"
  37. struct timespec;
  38. struct timeval;
  39. extern struct pal_linux_state {
  40. PAL_NUM parent_process_id;
  41. PAL_NUM process_id;
  42. #ifdef DEBUG
  43. bool in_gdb;
  44. #endif
  45. const char ** environ;
  46. /* credentails */
  47. unsigned int pid;
  48. unsigned int uid, gid;
  49. /* currently enabled signals */
  50. __sigset_t set_signals;
  51. __sigset_t blocked_signals;
  52. unsigned long memory_quota;
  53. #if USE_VDSO_GETTIME == 1
  54. # if USE_CLOCK_GETTIME == 1
  55. long int (*vdso_clock_gettime) (long int clk, struct timespec * tp);
  56. # else
  57. long int (*vdso_gettimeofday) (struct timeval *, void *);
  58. # endif
  59. #endif
  60. } linux_state;
  61. #include <asm/mman.h>
  62. #ifdef INLINE_SYSCALL
  63. # ifdef __i386__
  64. # define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
  65. ({ \
  66. struct mmap_arg_struct { \
  67. unsigned long addr; \
  68. unsigned long len; \
  69. unsigned long prot; \
  70. unsigned long flags; \
  71. unsigned long fd; \
  72. unsigned long offset; \
  73. } args = { .addr = (unsigned long)(addr), \
  74. .len = (unsigned long)(len), \
  75. .prot = (unsigned long)(prot), \
  76. .flags = (unsigned long)(flags), \
  77. .fd = (unsigned long)(fd), \
  78. .offset = (unsigned long)(offset), }; \
  79. INLINE_SYSCALL(mmap, 1, &args); \
  80. })
  81. # else
  82. # define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
  83. INLINE_SYSCALL(mmap, 6, addr, len, prot, flags, fd, offset)
  84. # endif
  85. #else
  86. # error "INLINE_SYSCALL not supported"
  87. #endif
  88. #ifndef SIGCHLD
  89. # define SIGCHLD 17
  90. #endif
  91. #ifdef DEBUG
  92. # define ARCH_VFORK() \
  93. (linux_state.in_gdb ? \
  94. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK|SIGCHLD, 0, NULL, NULL) :\
  95. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  96. #else
  97. # define ARCH_VFORK() \
  98. (INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  99. #endif
  100. #define PRESET_PAGESIZE (1 << 12)
  101. #define DEFAULT_BACKLOG 2048
  102. static inline int HOST_FLAGS (int alloc_type, int prot)
  103. {
  104. return ((alloc_type & PAL_ALLOC_RESERVE) ? MAP_NORESERVE|MAP_UNINITIALIZED : 0) |
  105. ((prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
  106. }
  107. static inline int HOST_PROT (int prot)
  108. {
  109. return prot & (PAL_PROT_READ|PAL_PROT_WRITE|PAL_PROT_EXEC);
  110. }
  111. static inline int HOST_ACCESS (int access)
  112. {
  113. return (access & (PAL_ACCESS_RDONLY|PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) |
  114. ((access & PAL_ACCESS_APPEND) ? O_APPEND|O_WRONLY : 0);
  115. }
  116. int clone (int (*__fn) (void * __arg), void * __child_stack,
  117. int __flags, const void * __arg, ...);
  118. /* set/unset CLOEXEC flags of all fds in a handle */
  119. int handle_set_cloexec (PAL_HANDLE handle, bool enable);
  120. /* serialize/deserialize a handle into/from a malloc'ed buffer */
  121. int handle_serialize (PAL_HANDLE handle, void ** data);
  122. int handle_deserialize (PAL_HANDLE * handle, const void * data, int size);
  123. #define ACCESS_R 4
  124. #define ACCESS_W 2
  125. #define ACCESS_X 1
  126. bool stataccess (struct stat * stats, int acc);
  127. void init_child_process (PAL_HANDLE * parent, PAL_HANDLE * exec,
  128. PAL_HANDLE * manifest);
  129. void cpuid (unsigned int leaf, unsigned int subleaf,
  130. unsigned int words[]);
  131. int block_signals (bool block, const int * sigs, int nsig);
  132. int block_async_signals (bool block);
  133. void signal_setup (void);
  134. unsigned long _DkSystemTimeQueryEarly (void);
  135. extern char __text_start, __text_end, __data_start, __data_end;
  136. #define TEXT_START ((void*)(&__text_start))
  137. #define TEXT_END ((void*)(&__text_end))
  138. #define DATA_START ((void*)(&__text_start))
  139. #define DATA_END ((void*)(&__text_end))
  140. #define ADDR_IN_PAL(addr) \
  141. ((void*)(addr) > TEXT_START && (void*)(addr) < TEXT_END)
  142. DEFINE_LIST(event_queue);
  143. struct event_queue {
  144. LIST_TYPE(event_queue) list;
  145. int event_num;
  146. };
  147. DEFINE_LISTP(event_queue);
  148. typedef struct pal_tcb_linux {
  149. PAL_TCB common;
  150. struct {
  151. /* private to Linux PAL */
  152. int pending_event;
  153. LISTP_TYPE(event_queue) pending_queue;
  154. PAL_HANDLE handle;
  155. void * alt_stack;
  156. int (*callback) (void *);
  157. void * param;
  158. };
  159. } PAL_TCB_LINUX;
  160. noreturn void pal_linux_main (void * args);
  161. int pal_thread_init (void * tcbptr);
  162. static inline PAL_TCB_LINUX * get_tcb_linux (void)
  163. {
  164. return (PAL_TCB_LINUX*)pal_get_tcb();
  165. }
  166. #endif /* PAL_LINUX_H */