shim_sysv.h 6.0 KB

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