pal_linux.h 5.6 KB

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