hab_pipe.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "hab.h"
  7. #include "hab_pipe.h"
  8. size_t hab_pipe_calc_required_bytes(const uint32_t shared_buf_size)
  9. {
  10. return sizeof(struct hab_pipe)
  11. + (2 * (sizeof(struct hab_shared_buf) + shared_buf_size));
  12. }
  13. /*
  14. * Must store the tx and rx ring buf pointers in non-shared/local area and
  15. * always use such pointers(inaccessible from the remote untrusted side) to
  16. * read/write the shared ring buffer region. Following reasons to keep it
  17. * in local:
  18. * 1. Such kind of local ring buf pointers are of no use for the remote side.
  19. * 2. There is a info disclosure risk if they are stored and used in share buffer.
  20. * 3. Furthermore, the untrusted peer can modify it deliberately. It will cause
  21. * arbitrary/OOB access on local side.
  22. */
  23. struct hab_pipe_endpoint *hab_pipe_init(struct hab_pipe *pipe,
  24. struct hab_shared_buf **tx_buf_p,
  25. struct hab_shared_buf **rx_buf_p,
  26. struct dbg_items **itms,
  27. const uint32_t shared_buf_size, int top)
  28. {
  29. struct hab_pipe_endpoint *ep = NULL;
  30. struct hab_shared_buf *buf_a = NULL;
  31. struct hab_shared_buf *buf_b = NULL;
  32. struct dbg_items *its = NULL;
  33. if (!pipe || !tx_buf_p || !rx_buf_p)
  34. return NULL;
  35. /* debug only */
  36. its = kzalloc(sizeof(struct dbg_items), GFP_KERNEL);
  37. buf_a = (struct hab_shared_buf *) pipe->buf_base;
  38. buf_b = (struct hab_shared_buf *) (pipe->buf_base
  39. + sizeof(struct hab_shared_buf) + shared_buf_size);
  40. if (top) {
  41. ep = &pipe->top;
  42. memset(ep, 0, sizeof(*ep));
  43. *tx_buf_p = buf_a;
  44. *rx_buf_p = buf_b;
  45. pipe->legacy_buf_a = NULL;
  46. } else {
  47. ep = &pipe->bottom;
  48. memset(ep, 0, sizeof(*ep));
  49. *tx_buf_p = buf_b;
  50. *rx_buf_p = buf_a;
  51. memset(buf_b, 0, sizeof(struct hab_shared_buf));
  52. memset(buf_a, 0, sizeof(struct hab_shared_buf));
  53. buf_a->size = shared_buf_size;
  54. buf_b->size = shared_buf_size;
  55. pipe->legacy_buf_b = NULL;
  56. pipe->legacy_total_size = 0;
  57. }
  58. *itms = its;
  59. return ep;
  60. }
  61. uint32_t hab_pipe_write(struct hab_pipe_endpoint *ep,
  62. struct hab_shared_buf *sh_buf,
  63. const uint32_t buf_size,
  64. unsigned char *p, uint32_t num_bytes)
  65. {
  66. /* Save a copy for index and count to avoid ToC-ToU issue */
  67. uint32_t ep_tx_index = ep->tx_info.index;
  68. uint32_t ep_tx_wr_count = ep->tx_info.wr_count;
  69. uint32_t sh_buf_rd_count = sh_buf->rd_count;
  70. uint32_t space = 0U;
  71. uint32_t count1, count2;
  72. if (buf_size < (ep_tx_wr_count - sh_buf_rd_count)) {
  73. pr_err("rd/wr counter error wr:%u rd:%u\n",
  74. ep_tx_wr_count, sh_buf_rd_count);
  75. return 0;
  76. }
  77. space = buf_size - (ep_tx_wr_count - sh_buf_rd_count);
  78. if (!p || num_bytes > space || num_bytes == 0) {
  79. pr_err("****can not write to pipe p %pK to-write %d space available %d\n",
  80. p, num_bytes, space);
  81. return 0;
  82. }
  83. asm volatile("dmb ish" ::: "memory");
  84. if ((buf_size < ep_tx_index) || (buf_size < num_bytes)) {
  85. pr_err("index in tx ep is out of boundary or number of bytes is larger than the ring buffer size\n");
  86. return 0;
  87. }
  88. count1 = (num_bytes <= (buf_size - ep_tx_index))
  89. ? num_bytes : (buf_size - ep_tx_index);
  90. count2 = num_bytes - count1;
  91. if (count1 > 0) {
  92. memcpy((void *)&sh_buf->data[ep_tx_index], p, count1);
  93. ep_tx_wr_count += count1;
  94. ep_tx_index += count1;
  95. if (ep_tx_index >= buf_size)
  96. ep_tx_index = 0;
  97. }
  98. if (count2 > 0) {/* handle buffer wrapping */
  99. memcpy((void *)&sh_buf->data[ep_tx_index],
  100. p + count1, count2);
  101. ep_tx_wr_count += count2;
  102. ep_tx_index += count2;
  103. if (ep_tx_index >= buf_size)
  104. ep_tx_index = 0;
  105. }
  106. ep->tx_info.wr_count = ep_tx_wr_count;
  107. ep->tx_info.index = ep_tx_index;
  108. return num_bytes;
  109. }
  110. /* Updates the write index which is shared with the other VM */
  111. void hab_pipe_write_commit(struct hab_pipe_endpoint *ep,
  112. struct hab_shared_buf *sh_buf)
  113. {
  114. /* Must commit data before incrementing count */
  115. asm volatile("dmb ishst" ::: "memory");
  116. sh_buf->wr_count = ep->tx_info.wr_count;
  117. }
  118. #define HAB_HEAD_CLEAR 0xCC
  119. uint32_t hab_pipe_read(struct hab_pipe_endpoint *ep,
  120. struct hab_shared_buf *sh_buf,
  121. const uint32_t buf_size,
  122. unsigned char *p, uint32_t size, uint32_t clear)
  123. {
  124. /* Save a copy for index to avoid ToC-ToU issue */
  125. uint32_t ep_rx_index = ep->rx_info.index;
  126. /* mb to guarantee wr_count is updated after contents are written */
  127. uint32_t avail = sh_buf->wr_count - sh_buf->rd_count;
  128. uint32_t count1, count2, to_read;
  129. uint32_t index_saved = ep_rx_index; /* store original for retry */
  130. static uint8_t signature_mismatch;
  131. if (!p || avail == 0 || size == 0 || ep_rx_index > buf_size)
  132. return 0;
  133. asm volatile("dmb ishld" ::: "memory");
  134. /* error if available is less than size and available is not zero */
  135. to_read = (avail < size) ? avail : size;
  136. /*
  137. * Generally, the available size should be equal to the expected read size.
  138. * But when calling hab_msg_drop() during message recv, available size may
  139. * less than expected size.
  140. */
  141. if (to_read < size)
  142. pr_info("less data available %d than requested %d\n",
  143. avail, size);
  144. count1 = (to_read <= (buf_size - ep_rx_index)) ? to_read :
  145. (buf_size - ep_rx_index);
  146. count2 = to_read - count1;
  147. if (count1 > 0) {
  148. memcpy(p, (void *)&sh_buf->data[ep_rx_index], count1);
  149. ep_rx_index += count1;
  150. if (ep_rx_index >= buf_size)
  151. ep_rx_index = 0;
  152. }
  153. if (count2 > 0) { /* handle buffer wrapping */
  154. memcpy(p + count1, (void *)&sh_buf->data[ep_rx_index],
  155. count2);
  156. ep_rx_index += count2;
  157. }
  158. ep->rx_info.index = ep_rx_index;
  159. if (count1 + count2) {
  160. struct hab_header *head = (struct hab_header *)p;
  161. int retry_cnt = 0;
  162. if (clear && (size == sizeof(*head))) {
  163. retry:
  164. if (unlikely(head->signature != 0xBEE1BEE1)) {
  165. pr_debug("hab head corruption detected at %pK buf %pK %08X %08X %08X %08X %08X rd %d wr %d index %X saved %X retry %d\n",
  166. head, &sh_buf->data[0],
  167. head->id_type,
  168. head->payload_size,
  169. head->session_id,
  170. head->signature, head->sequence,
  171. sh_buf->rd_count, sh_buf->wr_count,
  172. ep->rx_info.index, index_saved,
  173. retry_cnt);
  174. if (retry_cnt++ <= 1000) {
  175. memcpy(p, &sh_buf->data[index_saved],
  176. count1);
  177. if (count2)
  178. memcpy(&p[count1],
  179. &sh_buf->data[ep_rx_index - count2],
  180. count2);
  181. if (!signature_mismatch)
  182. goto retry;
  183. } else
  184. pr_err("quit retry after %d time may fail %X %X %X %X %X rd %d wr %d index %X\n",
  185. retry_cnt, head->id_type,
  186. head->payload_size,
  187. head->session_id,
  188. head->signature,
  189. head->sequence,
  190. sh_buf->rd_count,
  191. sh_buf->wr_count,
  192. ep->rx_info.index);
  193. signature_mismatch = 1;
  194. } else
  195. signature_mismatch = 0;
  196. }
  197. /* If the signature has mismatched,
  198. * don't increment the shared buffer index.
  199. */
  200. if (signature_mismatch) {
  201. ep->rx_info.index = index_saved + 1;
  202. if (ep->rx_info.index >= sh_buf->size)
  203. ep->rx_info.index = 0;
  204. to_read = (retry_cnt < 1000) ? 0xFFFFFFFE : 0xFFFFFFFF;
  205. }
  206. /*Must commit data before incremeting count*/
  207. asm volatile("dmb ish" ::: "memory");
  208. sh_buf->rd_count += (signature_mismatch) ? 1 : count1 + count2;
  209. }
  210. return to_read;
  211. }
  212. void hab_pipe_rxinfo(struct hab_pipe_endpoint *ep,
  213. struct hab_shared_buf *sh_buf,
  214. uint32_t *rd_cnt,
  215. uint32_t *wr_cnt, uint32_t *idx)
  216. {
  217. *idx = ep->rx_info.index;
  218. *rd_cnt = sh_buf->rd_count;
  219. *wr_cnt = sh_buf->wr_count;
  220. }