db_ipc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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_debug.h"
  27. #include "pal_security.h"
  28. #include "graphene-ipc.h"
  29. #include "api.h"
  30. int gipc_open (PAL_HANDLE * handle, const char * type, const char * uri,
  31. int access, int share, int create, int options)
  32. {
  33. int64_t token;
  34. int rv;
  35. int fd = INLINE_SYSCALL(open, 3, GIPC_FILE, O_RDONLY|O_CLOEXEC, 0);
  36. if (IS_ERR(fd))
  37. return -PAL_ERROR_DENIED;
  38. token = atoi(uri);
  39. rv = INLINE_SYSCALL(ioctl, 3, fd, GIPC_JOIN, token);
  40. if (rv < 0) {
  41. INLINE_SYSCALL(close, 1, fd);
  42. return -PAL_ERROR_DENIED;
  43. }
  44. PAL_HANDLE hdl = malloc(HANDLE_SIZE(gipc));
  45. SET_HANDLE_TYPE(hdl, gipc);
  46. hdl->gipc.fd = fd;
  47. hdl->gipc.token = token;
  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. return (IS_ERR(ret)) ? -PAL_ERROR_BADHANDLE : 0;
  55. }
  56. const char * gipc_getrealpath (PAL_HANDLE handle)
  57. {
  58. return GIPC_FILE;
  59. }
  60. struct handle_ops gipc_ops = {
  61. .getrealpath = &gipc_getrealpath,
  62. .open = &gipc_open,
  63. .close = &gipc_close,
  64. };
  65. int _DkCreatePhysicalMemoryChannel (PAL_HANDLE * handle, unsigned long * key)
  66. {
  67. unsigned long token = 0;
  68. int fd = INLINE_SYSCALL(open, 3, GIPC_FILE, O_RDONLY|O_CLOEXEC, 0);
  69. if (IS_ERR(fd))
  70. goto err;
  71. PAL_HANDLE hdl = malloc(HANDLE_SIZE(gipc));
  72. SET_HANDLE_TYPE(hdl, gipc);
  73. hdl->gipc.fd = fd;
  74. // ioctl to create a new queue
  75. token = INLINE_SYSCALL(ioctl, 3, fd, GIPC_CREATE, 0);
  76. if (token < 0)
  77. goto err_fd;
  78. *handle = hdl;
  79. *key = token;
  80. return 0;
  81. err_fd:
  82. INLINE_SYSCALL(close, 1, fd);
  83. err:
  84. return -PAL_ERROR_DENIED;
  85. }
  86. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
  87. PAL_PTR * addrs, PAL_NUM * sizes, int flags)
  88. {
  89. int fd = channel->gipc.fd;
  90. struct gipc_send gs;
  91. gs.addr = __alloca(sizeof(unsigned long) * entries);
  92. gs.len = __alloca(sizeof(unsigned long) * entries);
  93. for (int i = 0 ; i < entries ; i++) {
  94. if (!addrs[i] || !sizes[i] || !ALLOC_ALIGNED(addrs[i]) ||
  95. !ALLOC_ALIGNED(sizes[i]))
  96. return -PAL_ERROR_INVAL;
  97. gs.addr[i] = (unsigned long) addrs[i];
  98. gs.len[i] = sizes[i];
  99. }
  100. gs.entries = entries;
  101. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_SEND, &gs);
  102. if (IS_ERR(ret))
  103. return -PAL_ERROR_DENIED;
  104. return ret;
  105. }
  106. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
  107. PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots)
  108. {
  109. int fd = channel->gipc.fd;
  110. struct gipc_recv gr;
  111. gr.addr = __alloca(sizeof(unsigned long) * entries);
  112. gr.len = __alloca(sizeof(unsigned long) * entries);
  113. gr.prot = __alloca(sizeof(unsigned long) * entries);
  114. for (int i = 0 ; i < entries ; i++) {
  115. if (!sizes[i] || !ALLOC_ALIGNED(addrs[i]) || !ALLOC_ALIGNED(sizes[i]))
  116. return -PAL_ERROR_INVAL;
  117. gr.addr[i] = (unsigned long) addrs[i];
  118. gr.len[i] = sizes[i];
  119. gr.prot[i] = HOST_PROT(prots[i]);
  120. }
  121. gr.entries = entries;
  122. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_RECV, &gr);
  123. if (IS_ERR(ret))
  124. return -PAL_ERROR_DENIED;
  125. for (int i = 0 ; i < entries ; i++)
  126. addrs[i] = (PAL_PTR) gr.addr[i];
  127. return ret;
  128. }