db_ipc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * db_ipc.c
  17. *
  18. * This file contains APIs for physical memory bulk copy across processes.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_security.h"
  27. #include "graphene-ipc.h"
  28. #include "api.h"
  29. #include <fcntl.h>
  30. #include <sched.h>
  31. int gipc_open (PAL_HANDLE * handle, const char * type, const char * uri,
  32. int access, int share, int create, int options)
  33. {
  34. int64_t token;
  35. int rv;
  36. int fd = INLINE_SYSCALL(open, 3, GIPC_FILE, O_RDONLY|O_CLOEXEC, 0);
  37. if (IS_ERR(fd))
  38. return -PAL_ERROR_DENIED;
  39. token = atoi(uri);
  40. rv = INLINE_SYSCALL(ioctl, 3, fd, GIPC_JOIN, token);
  41. if (rv < 0) {
  42. INLINE_SYSCALL(close, 1, fd);
  43. return -PAL_ERROR_DENIED;
  44. }
  45. PAL_HANDLE hdl = malloc(HANDLE_SIZE(gipc));
  46. SET_HANDLE_TYPE(hdl, gipc);
  47. hdl->gipc.fd = fd;
  48. *handle = hdl;
  49. return 0;
  50. }
  51. int gipc_close (PAL_HANDLE handle)
  52. {
  53. int ret = INLINE_SYSCALL(close, 1, handle->gipc.fd);
  54. if (IS_ERR(ret))
  55. return -PAL_ERROR_BADHANDLE;
  56. return 0;
  57. }
  58. const char * gipc_getrealpath (PAL_HANDLE handle)
  59. {
  60. return GIPC_FILE;
  61. }
  62. struct handle_ops gipc_ops = {
  63. .getrealpath = &gipc_getrealpath,
  64. .open = &gipc_open,
  65. .close = &gipc_close,
  66. };
  67. int _DkCreatePhysicalMemoryChannel (PAL_HANDLE * handle, unsigned long * key)
  68. {
  69. store_frame(CreatePhysicalMemoryChannel);
  70. unsigned long token = 0;
  71. int fd = INLINE_SYSCALL(open, 3, GIPC_FILE, O_RDONLY|O_CLOEXEC, 0);
  72. if (IS_ERR(fd))
  73. goto err;
  74. PAL_HANDLE hdl = malloc(HANDLE_SIZE(gipc));
  75. SET_HANDLE_TYPE(hdl, gipc);
  76. hdl->gipc.fd = fd;
  77. // ioctl to create a new queue
  78. token = INLINE_SYSCALL(ioctl, 3, fd, GIPC_CREATE, 0);
  79. if (token < 0)
  80. goto err_fd;
  81. *handle = hdl;
  82. *key = token;
  83. return 0;
  84. err_fd:
  85. INLINE_SYSCALL(close, 1, fd);
  86. err:
  87. return -PAL_ERROR_DENIED;
  88. }
  89. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries, void ** addrs,
  90. unsigned long * sizes, int flags)
  91. {
  92. int fd = channel->gipc.fd;
  93. struct gipc_send gs;
  94. int nsent = 0, n;
  95. unsigned long npagesent = 0;
  96. while (nsent < entries) {
  97. n = entries - nsent;
  98. if (n > ADDR_ENTS)
  99. n = ADDR_ENTS;
  100. gs.entries = n;
  101. gs.addr = (unsigned long *) (addrs + nsent);
  102. gs.len = (unsigned long *) (sizes + nsent);
  103. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_SEND, &gs);
  104. if (IS_ERR(ret)) {
  105. if (!npagesent)
  106. return -PAL_ERROR_DENIED;
  107. return npagesent;
  108. }
  109. npagesent += ret;
  110. nsent += n;
  111. }
  112. return npagesent;
  113. }
  114. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries, void ** addrs,
  115. unsigned long * sizes, unsigned int * prots)
  116. {
  117. int fd = channel->gipc.fd;
  118. struct gipc_recv gr;
  119. int nrecv = 0, n;
  120. unsigned long npagerecv = 0;
  121. while (nrecv < entries) {
  122. n = entries - nrecv;
  123. if (n > ADDR_ENTS)
  124. n = ADDR_ENTS;
  125. gr.entries = n;
  126. gr.addr = (unsigned long *) (addrs + nrecv);
  127. gr.len = (unsigned long *) (sizes + nrecv);
  128. gr.prot = (int *) (prots + nrecv);
  129. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_RECV, &gr);
  130. if (IS_ERR(ret)) {
  131. if (!npagerecv)
  132. return -PAL_ERROR_DENIED;
  133. return npagerecv;
  134. }
  135. npagerecv += ret;
  136. nrecv += n;
  137. }
  138. return npagerecv;
  139. }