db_eventfd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2019 Intel Corporation
  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. * db_eventfd.c
  15. *
  16. * This file contains operations to handle streams with URIs that have "eventfd:".
  17. */
  18. #include <asm/fcntl.h>
  19. #include <asm/poll.h>
  20. #include <linux/types.h>
  21. #include <linux/un.h>
  22. #include <sys/eventfd.h>
  23. #include "api.h"
  24. #include "pal.h"
  25. #include "pal_debug.h"
  26. #include "pal_defs.h"
  27. #include "pal_error.h"
  28. #include "pal_internal.h"
  29. #include "pal_linux.h"
  30. #include "pal_linux_defs.h"
  31. #include "pal_linux_error.h"
  32. #include "pal_security.h"
  33. static inline int eventfd_type(int options) {
  34. int type = 0;
  35. if (options & PAL_OPTION_NONBLOCK)
  36. type |= EFD_NONBLOCK;
  37. if (options & PAL_OPTION_CLOEXEC)
  38. type |= EFD_CLOEXEC;
  39. if (options & PAL_OPTION_EFD_SEMAPHORE)
  40. type |= EFD_SEMAPHORE;
  41. return type;
  42. }
  43. /* `type` must be eventfd, `uri` & `access` & `share` are unused, `create` holds eventfd's initval,
  44. * `options` holds eventfd's flags */
  45. static int eventfd_pal_open(PAL_HANDLE* handle, const char* type, const char* uri, int access,
  46. int share, int create, int options) {
  47. int ret;
  48. __UNUSED(access);
  49. __UNUSED(share);
  50. if ((strcmp_static(type, URI_TYPE_EVENTFD) != 0) || (*uri != '\0')) {
  51. return -PAL_ERROR_INVAL;
  52. }
  53. /* Using create arg as a work-around (note: initval is uint32 but create is int32).*/
  54. ret = ocall_eventfd(create, eventfd_type(options));
  55. if (IS_ERR(ret))
  56. return unix_to_pal_error(ERRNO(ret));
  57. PAL_HANDLE hdl = malloc(HANDLE_SIZE(eventfd));
  58. SET_HANDLE_TYPE(hdl, eventfd);
  59. /* Note: using index 0, given that there is only 1 eventfd FD per pal-handle. */
  60. HANDLE_HDR(hdl)->flags = RFD(0) | WFD(0) | WRITABLE(0);
  61. hdl->eventfd.fd = ret;
  62. hdl->eventfd.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  63. *handle = hdl;
  64. return 0;
  65. }
  66. static int64_t eventfd_pal_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  67. if (offset)
  68. return -PAL_ERROR_INVAL;
  69. if (!IS_HANDLE_TYPE(handle, eventfd))
  70. return -PAL_ERROR_NOTCONNECTION;
  71. if (len < sizeof(uint64_t))
  72. return -PAL_ERROR_INVAL;
  73. /* TODO: verify that the value returned in buffer is somehow meaningful (to prevent Iago
  74. * attacks) */
  75. int bytes = ocall_read(handle->eventfd.fd, buffer, len);
  76. if (IS_ERR(bytes))
  77. return unix_to_pal_error(ERRNO(bytes));
  78. if (!bytes)
  79. return -PAL_ERROR_ENDOFSTREAM;
  80. return bytes;
  81. }
  82. static int64_t eventfd_pal_write(PAL_HANDLE handle, uint64_t offset, uint64_t len,
  83. const void* buffer) {
  84. if (offset)
  85. return -PAL_ERROR_INVAL;
  86. if (!IS_HANDLE_TYPE(handle, eventfd))
  87. return -PAL_ERROR_NOTCONNECTION;
  88. if (len < sizeof(uint64_t))
  89. return -PAL_ERROR_INVAL;
  90. int bytes = ocall_write(handle->eventfd.fd, buffer, len);
  91. PAL_FLG writable = WRITABLE(0);
  92. if (IS_ERR(bytes)) {
  93. if (ERRNO(bytes) == EAGAIN)
  94. HANDLE_HDR(handle)->flags &= ~writable;
  95. return unix_to_pal_error(ERRNO(bytes));
  96. }
  97. if ((uint64_t)bytes == sizeof(uint64_t))
  98. HANDLE_HDR(handle)->flags |= writable;
  99. else
  100. HANDLE_HDR(handle)->flags &= ~writable;
  101. return bytes;
  102. }
  103. /* invoked during poll operation on eventfd from LibOS. */
  104. static int eventfd_pal_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  105. if (handle->generic.fds[0] == PAL_IDX_POISON)
  106. return -PAL_ERROR_BADHANDLE;
  107. attr->handle_type = PAL_GET_TYPE(handle);
  108. int efd = handle->eventfd.fd;
  109. int flags = HANDLE_HDR(handle)->flags;
  110. struct pollfd pfd = {.fd = efd, .events = POLLIN, .revents = 0};
  111. int ret = ocall_poll(&pfd, 1, 0);
  112. if (IS_ERR(ret))
  113. return unix_to_pal_error(ERRNO(ret));
  114. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  115. attr->disconnected = flags & ERROR(0);
  116. attr->nonblocking = handle->eventfd.nonblocking;
  117. /* For future use, so that Linux host kernel can send notifications to user-space apps. App
  118. * receives virtual FD from LibOS, but the Linux-host eventfd is memorized here, such that this
  119. * Linux-host eventfd can be retrieved (by LibOS) during app's ioctl(). */
  120. attr->no_of_fds = 1;
  121. attr->fds[0] = efd;
  122. return 0;
  123. }
  124. static int eventfd_pal_close(PAL_HANDLE handle) {
  125. if (IS_HANDLE_TYPE(handle, eventfd)) {
  126. if (handle->eventfd.fd != PAL_IDX_POISON) {
  127. ocall_close(handle->eventfd.fd);
  128. handle->eventfd.fd = PAL_IDX_POISON;
  129. }
  130. return 0;
  131. }
  132. return 0;
  133. }
  134. struct handle_ops eventfd_ops = {
  135. .open = &eventfd_pal_open,
  136. .read = &eventfd_pal_read,
  137. .write = &eventfd_pal_write,
  138. .close = &eventfd_pal_close,
  139. .attrquerybyhdl = &eventfd_pal_attrquerybyhdl,
  140. };