db_ipc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_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. store_frame(CreatePhysicalMemoryChannel);
  68. unsigned long token = 0;
  69. int fd = INLINE_SYSCALL(open, 3, GIPC_FILE, O_RDONLY|O_CLOEXEC, 0);
  70. if (IS_ERR(fd))
  71. goto err;
  72. PAL_HANDLE hdl = malloc(HANDLE_SIZE(gipc));
  73. SET_HANDLE_TYPE(hdl, gipc);
  74. hdl->gipc.fd = fd;
  75. // ioctl to create a new queue
  76. token = INLINE_SYSCALL(ioctl, 3, fd, GIPC_CREATE, 0);
  77. if (token < 0)
  78. goto err_fd;
  79. *handle = hdl;
  80. *key = token;
  81. return 0;
  82. err_fd:
  83. INLINE_SYSCALL(close, 1, fd);
  84. err:
  85. return -PAL_ERROR_DENIED;
  86. }
  87. int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
  88. PAL_PTR * addrs, PAL_NUM * sizes, int flags)
  89. {
  90. int fd = channel->gipc.fd;
  91. struct gipc_send gs;
  92. gs.addr = __alloca(sizeof(unsigned long) * entries);
  93. gs.len = __alloca(sizeof(unsigned long) * entries);
  94. for (int i = 0 ; i < entries ; i++) {
  95. if (!addrs[i] || !sizes[i] || !ALLOC_ALIGNED(addrs[i]) ||
  96. !ALLOC_ALIGNED(sizes[i]))
  97. return -PAL_ERROR_INVAL;
  98. gs.addr[i] = (unsigned long) addrs[i];
  99. gs.len[i] = sizes[i];
  100. }
  101. gs.entries = entries;
  102. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_SEND, &gs);
  103. if (IS_ERR(ret))
  104. return -PAL_ERROR_DENIED;
  105. return ret;
  106. }
  107. int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
  108. PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots)
  109. {
  110. int fd = channel->gipc.fd;
  111. struct gipc_recv gr;
  112. gr.addr = __alloca(sizeof(unsigned long) * entries);
  113. gr.len = __alloca(sizeof(unsigned long) * entries);
  114. gr.prot = __alloca(sizeof(unsigned long) * entries);
  115. for (int i = 0 ; i < entries ; i++) {
  116. if (!sizes[i] || !ALLOC_ALIGNED(addrs[i]) || !ALLOC_ALIGNED(sizes[i]))
  117. return -PAL_ERROR_INVAL;
  118. gr.addr[i] = (unsigned long) addrs[i];
  119. gr.len[i] = sizes[i];
  120. gr.prot[i] = HOST_PROT(prots[i]);
  121. }
  122. gr.entries = entries;
  123. int ret = INLINE_SYSCALL(ioctl, 3, fd, GIPC_RECV, &gr);
  124. if (IS_ERR(ret))
  125. return -PAL_ERROR_DENIED;
  126. for (int i = 0 ; i < entries ; i++)
  127. addrs[i] = (PAL_PTR) gr.addr[i];
  128. return ret;
  129. }