shim_sysv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /*
  14. * shim_sysv.h
  15. *
  16. * This file includes functions and types for implementing System V IPC
  17. * functionality.
  18. */
  19. #ifndef __SHIM_SYSV_H__
  20. #define __SHIM_SYSV_H__
  21. #include <shim_types.h>
  22. #include <shim_handle.h>
  23. enum sysv_type { SYSV_NONE, SYSV_MSGQ, SYSV_SEM, SYSV_SHM };
  24. #define SYSV_TYPE_STR(type) \
  25. ((type) == SYSV_MSGQ ? "MSGQ" : \
  26. ((type) == SYSV_SEM ? "SEM" : \
  27. ((type) == SYSV_SHM ? "SHM" : \
  28. "")))
  29. #define VALID_SYSV_TYPE(type) \
  30. ((type) == SYSV_MSGQ || (type) == SYSV_SEM || (type) == SYSV_SHM)
  31. struct sysv_score {
  32. IDTYPE vmid;
  33. unsigned long score;
  34. };
  35. struct sysv_client {
  36. struct shim_ipc_port * port;
  37. IDTYPE vmid;
  38. unsigned seq;
  39. };
  40. struct shim_handle;
  41. struct sysv_balance_policy {
  42. unsigned int score_decay;
  43. unsigned int score_max;
  44. unsigned int balance_threshold;
  45. int (*migrate) (struct shim_handle * hdl, struct sysv_client * client);
  46. };
  47. int __balance_sysv_score (struct sysv_balance_policy * policy,
  48. struct shim_handle * hdl,
  49. struct sysv_score * scores, int nscores,
  50. struct sysv_client * src, long score);
  51. #define MSG_NOERROR 010000
  52. #include <list.h>
  53. struct __kernel_msgbuf {
  54. long mtype; /* type of message */
  55. char mtext[]; /* message text */
  56. };
  57. #define MSG_QOBJ_SIZE 64
  58. struct msg_qobj {
  59. void * next;
  60. char data[MSG_QOBJ_SIZE - sizeof(void *)];
  61. } __attribute__((packed));
  62. struct msg_item {
  63. void * next;
  64. unsigned short size;
  65. char data[];
  66. } __attribute__((packed));
  67. #define MSG_ITEM_DATA_SIZE(size) \
  68. ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_item) ? (size) : \
  69. MSG_QOBJ_SIZE - sizeof(struct msg_item))
  70. struct msg_ext_item {
  71. void * next;
  72. char data[];
  73. } __attribute__((packed));
  74. #define MSG_EXT_ITEM_DATA_SIZE(size) \
  75. ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_ext_item) ? (size) : \
  76. MSG_QOBJ_SIZE - sizeof(struct msg_ext_item))
  77. struct msg_req {
  78. struct msg_req * next;
  79. unsigned short size;
  80. int flags;
  81. struct sysv_client dest;
  82. } __attribute__((packed));
  83. #define INIT_MSG_TYPE_SIZE 32
  84. struct msg_type {
  85. long type; /* type of the messages */
  86. struct msg_item * msgs, * msg_tail;
  87. struct msg_req * reqs, * req_tail;
  88. };
  89. #define DEFAULT_MSG_QUEUE_SIZE 2048
  90. #define MSG_SND_SCORE 1
  91. #define MSG_RCV_SCORE 20
  92. #define MSG_SCORE_DECAY 10
  93. #define MSG_SCORE_MAX 200
  94. #define MSG_BALANCE_THRESHOLD 100
  95. struct msg_handle_backup {
  96. int perm; /* access permissions */
  97. int nmsgs; /* number of msgs */
  98. int currentsize; /* current size in bytes */
  99. };
  100. struct msg_backup {
  101. long type;
  102. int size;
  103. char data[];
  104. };
  105. struct shim_msg_handle;
  106. int add_msg_handle (unsigned long key, IDTYPE id, bool owned);
  107. int del_msg_handle (struct shim_msg_handle * msgq);
  108. struct shim_msg_handle * get_msg_handle_by_key (unsigned long key);
  109. struct shim_msg_handle * get_msg_handle_by_id (IDTYPE id);
  110. void put_msg_handle (struct shim_msg_handle * msgq);
  111. int recover_msg_ownership (struct shim_msg_handle * msgq);
  112. int add_sysv_msg (struct shim_msg_handle * msgq,
  113. long type, size_t size, const void * data,
  114. struct sysv_client * src);
  115. int get_sysv_msg (struct shim_msg_handle * msgq,
  116. long type, size_t size, void * data, int flags,
  117. struct sysv_client * src);
  118. int store_all_msg_persist (void);
  119. #define HOST_SEM_NUM 65535
  120. DEFINE_LIST(sem_ops);
  121. struct sem_ops {
  122. LIST_TYPE(sem_ops) progress;
  123. struct sem_stat {
  124. bool completed;
  125. bool failed;
  126. int nops;
  127. int current;
  128. unsigned long timeout;
  129. } stat;
  130. struct sysv_client client;
  131. struct sembuf ops[];
  132. };
  133. DEFINE_LISTP(sem_ops);
  134. struct sem_obj {
  135. unsigned short num;
  136. unsigned short val;
  137. unsigned short zcnt;
  138. unsigned short ncnt;
  139. IDTYPE pid;
  140. PAL_NUM host_sem_id;
  141. PAL_HANDLE host_sem;
  142. LISTP_TYPE(sem_ops) ops;
  143. LISTP_TYPE(sem_ops) next_ops;
  144. };
  145. #define SEM_POSITIVE_SCORE(num) ((num) < 5 ? 5 - (num) : 1)
  146. #define SEM_ZERO_SCORE 20
  147. #define SEM_NEGATIVE_SCORE(num) (20 * (num))
  148. #define SEM_SCORE_DECAY 10
  149. #define SEM_SCORE_MAX 200
  150. #define SEM_BALANCE_THRESHOLD 100
  151. struct sem_backup {
  152. unsigned short val;
  153. unsigned short zcnt;
  154. unsigned short ncnt;
  155. IDTYPE pid;
  156. };
  157. struct sem_client_backup {
  158. IDTYPE vmid;
  159. unsigned long seq;
  160. int current;
  161. int nops;
  162. };
  163. int add_sem_handle (unsigned long key, IDTYPE id, int nsems, bool owned);
  164. struct shim_sem_handle * get_sem_handle_by_key (unsigned long key);
  165. struct shim_sem_handle * get_sem_handle_by_id (IDTYPE semid);
  166. void put_sem_handle (struct shim_sem_handle * sem);
  167. int del_sem_handle (struct shim_sem_handle * sem);
  168. int recover_sem_ownership (struct shim_sem_handle * sem,
  169. struct sem_backup * backups, int nbackups,
  170. struct sem_client_backup * clients, int nclients);
  171. int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
  172. int nsops, unsigned long timeout,
  173. struct sysv_client * client);
  174. #ifdef USE_SHARED_SEMAPHORE
  175. int send_sem_host_ids (struct shim_sem_handle * sem,
  176. struct shim_ipc_port * port, IDTYPE dest,
  177. unsigned long seq);
  178. #endif
  179. #endif /* __SHIM_SYSV_H__ */