shim_sysv.h 6.7 KB

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