shim_sysv.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 <linux_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. struct sem_obj {
  123. unsigned short num;
  124. unsigned short val;
  125. unsigned short zcnt;
  126. unsigned short ncnt;
  127. IDTYPE pid;
  128. PAL_NUM host_sem_id;
  129. PAL_HANDLE host_sem;
  130. struct list_head ops;
  131. struct list_head next_ops;
  132. };
  133. struct sem_ops {
  134. struct list_head progress;
  135. struct sem_stat {
  136. bool completed;
  137. bool failed;
  138. int nops;
  139. int current;
  140. unsigned long timeout;
  141. } stat;
  142. struct sysv_client client;
  143. struct sembuf 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__ */