pal_freebsd.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_FREEBSD_H
  14. #define PAL_FREEBSD_H
  15. #include "pal.h"
  16. #include "pal_defs.h"
  17. #include "pal_freebsd_defs.h"
  18. #include "pal_freebsd_error.h"
  19. typedef int __kernel_pid_t;
  20. #include <sigset.h>
  21. #include <sys/syscall.h>
  22. #include <unistd.h>
  23. #undef __htonl
  24. #undef __ntohl
  25. #undef __htons
  26. #undef __ntohs
  27. #ifdef __x86_64__
  28. #include "sysdep-x86_64.h"
  29. #endif
  30. #define PAL_LOADER XSTRINGIFY(PAL_LOADER_PATH)
  31. #define IS_ERR INTERNAL_SYSCALL_ERROR
  32. #define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
  33. #define ERRNO INTERNAL_SYSCALL_ERRNO
  34. #define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
  35. struct timespec;
  36. struct timeval;
  37. extern struct pal_bsd_state {
  38. /* state */
  39. unsigned long start_time;
  40. /* credentails */
  41. unsigned int pid;
  42. unsigned int uid, gid;
  43. unsigned int parent_pid;
  44. /* currently enabled signals */
  45. _sigset_t sigset;
  46. unsigned long memory_quota;
  47. } bsd_state;
  48. #include <sys/mman.h>
  49. #ifdef INLINE_SYSCALL
  50. #ifdef __i386__
  51. #define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
  52. ({ \
  53. struct mmap_arg_struct { \
  54. unsigned long addr; \
  55. unsigned long len; \
  56. unsigned long prot; \
  57. unsigned long flags; \
  58. unsigned long fd; \
  59. unsigned long offset; \
  60. } args = { \
  61. .addr = (unsigned long)(addr), \
  62. .len = (unsigned long)(len), \
  63. .prot = (unsigned long)(prot), \
  64. .flags = (unsigned long)(flags), \
  65. .fd = (unsigned long)(fd), \
  66. .offset = (unsigned long)(offset), \
  67. }; \
  68. INLINE_SYSCALL(mmap, 1, &args); \
  69. })
  70. #else
  71. #define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
  72. INLINE_SYSCALL(mmap, 6, (addr), (len), (prot), (flags), (fd), (offset))
  73. #endif
  74. #else
  75. #error "INLINE_SYSCALL not supported"
  76. #endif
  77. #define PRESET_PAGESIZE (1 << 12)
  78. #define DEFAULT_BACKLOG 2048
  79. static inline int HOST_FLAGS(int alloc_type, int prot) {
  80. return (prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED;
  81. }
  82. static inline int HOST_PROT(int prot) {
  83. return prot & (PAL_PROT_READ | PAL_PROT_WRITE | PAL_PROT_EXEC);
  84. }
  85. int __clone(int (*__fn)(void* __arg), void* __child_stack, int __flags, const void* __arg, ...);
  86. /* set/unset CLOEXEC flags of all fds in a handle */
  87. int handle_set_cloexec(PAL_HANDLE handle, bool enable);
  88. /* serialize/deserialize a handle into/from a malloc'ed buffer */
  89. int handle_serialize(PAL_HANDLE handle, void** data);
  90. int handle_deserialize(PAL_HANDLE* handle, const void* data, int size);
  91. #define ACCESS_R 4
  92. #define ACCESS_W 2
  93. #define ACCESS_X 1
  94. struct stat;
  95. bool stataccess(struct stat* stats, int acc);
  96. #include <sys/fcntl.h>
  97. static inline int HOST_FILE_OPEN(int access_type, int create_type, int options) {
  98. return ((access_type) | (create_type & PAL_CREATE_TRY ? O_CREAT : 0) |
  99. (create_type & PAL_CREATE_ALWAYS ? O_EXCL : 0) | (options));
  100. }
  101. #include <sys/stat.h>
  102. static inline int HOST_PERM(int share_type) {
  103. return (share_type & PAL_SHARE_GLOBAL_X ? S_IXUSR | S_IXGRP | S_IXOTH : 0) |
  104. (share_type & PAL_SHARE_GLOBAL_W ? S_IWUSR | S_IWGRP | S_IWOTH : 0) |
  105. (share_type & PAL_SHARE_GLOBAL_R ? S_IRUSR | S_IRGRP | S_IROTH : 0) |
  106. (share_type & PAL_SHARE_GROUP_X ? S_IXGRP : 0) |
  107. (share_type & PAL_SHARE_GROUP_W ? S_IWGRP : 0) |
  108. (share_type & PAL_SHARE_GROUP_R ? S_IRGRP : 0) |
  109. (share_type & PAL_SHARE_OWNER_X ? S_IXUSR : 0) |
  110. (share_type & PAL_SHARE_OWNER_W ? S_IWUSR : 0) |
  111. (share_type & PAL_SHARE_OWNER_R ? S_IRUSR : 0);
  112. }
  113. static inline int HOST_OPTIONS(int options) {
  114. return (options & PAL_OPTION_NONBLOCK ? O_NONBLOCK : 0);
  115. }
  116. #include <sys/socket.h>
  117. static inline int HOST_SOCKET_OPTIONS(int options) {
  118. return (options & PAL_OPTION_NONBLOCK ? SOCK_NONBLOCK : 0);
  119. }
  120. /* Locking and unlocking of Mutexes */
  121. int _DkMutexLock(struct mutex_handle* mut);
  122. int _DkMutexLockTimeout(struct mutex_handle* mut, int timeout);
  123. int _DkMutexUnlock(struct mutex_handle* mut);
  124. /*UMTX constants*/
  125. #define UMTX_OP_WAIT 2
  126. #define UMTX_OP_WAKE 3
  127. #define UMTX_OP_WAIT_UINT 11
  128. void init_child_process(PAL_HANDLE* parent, PAL_HANDLE* exec, PAL_HANDLE* manifest);
  129. void signal_setup(void);
  130. extern char __text_start, __text_end, __data_start, __data_end;
  131. #define TEXT_START ((void*)(&__text_start))
  132. #define TEXT_END ((void*)(&__text_end))
  133. #define DATA_START ((void*)(&__text_start))
  134. #define DATA_END ((void*)(&__text_end))
  135. #endif /* PAL_FREEBSD_H */