pal_linux.h 6.4 KB

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