offload_common.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #if defined(LINUX) || defined(FREEBSD)
  27. #include <mm_malloc.h>
  28. #endif
  29. #include "offload_common.h"
  30. // The debug routines
  31. #if OFFLOAD_DEBUG > 0
  32. void __dump_bytes(
  33. int trace_level,
  34. const void *data,
  35. int len
  36. )
  37. {
  38. if (console_enabled > trace_level) {
  39. const uint8_t *arr = (const uint8_t*) data;
  40. char buffer[4096];
  41. char *bufferp;
  42. int count = 0;
  43. bufferp = buffer;
  44. while (len--) {
  45. sprintf(bufferp, "%02x", *arr++);
  46. bufferp += 2;
  47. count++;
  48. if ((count&3) == 0) {
  49. sprintf(bufferp, " ");
  50. bufferp++;
  51. }
  52. if ((count&63) == 0) {
  53. OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer);
  54. bufferp = buffer;
  55. count = 0;
  56. }
  57. }
  58. if (count) {
  59. OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer);
  60. }
  61. }
  62. }
  63. #endif // OFFLOAD_DEBUG
  64. // The Marshaller and associated routines
  65. void Marshaller::send_data(
  66. const void *data,
  67. int64_t length
  68. )
  69. {
  70. OFFLOAD_DEBUG_TRACE(2, "send_data(%p, %lld)\n",
  71. data, length);
  72. memcpy(buffer_ptr, data, (size_t)length);
  73. buffer_ptr += length;
  74. tfr_size += length;
  75. }
  76. void Marshaller::receive_data(
  77. void *data,
  78. int64_t length
  79. )
  80. {
  81. OFFLOAD_DEBUG_TRACE(2, "receive_data(%p, %lld)\n",
  82. data, length);
  83. memcpy(data, buffer_ptr, (size_t)length);
  84. buffer_ptr += length;
  85. tfr_size += length;
  86. }
  87. // Send function pointer
  88. void Marshaller::send_func_ptr(
  89. const void* data
  90. )
  91. {
  92. const char* name;
  93. size_t length;
  94. if (data != 0) {
  95. name = __offload_funcs.find_name(data);
  96. if (name == 0) {
  97. #if OFFLOAD_DEBUG > 0
  98. if (console_enabled > 2) {
  99. __offload_funcs.dump();
  100. }
  101. #endif // OFFLOAD_DEBUG > 0
  102. LIBOFFLOAD_ERROR(c_send_func_ptr, data);
  103. exit(1);
  104. }
  105. length = strlen(name) + 1;
  106. }
  107. else {
  108. name = "";
  109. length = 1;
  110. }
  111. memcpy(buffer_ptr, name, length);
  112. buffer_ptr += length;
  113. tfr_size += length;
  114. }
  115. // Receive function pointer
  116. void Marshaller::receive_func_ptr(
  117. const void** data
  118. )
  119. {
  120. const char* name;
  121. size_t length;
  122. name = (const char*) buffer_ptr;
  123. if (name[0] != '\0') {
  124. *data = __offload_funcs.find_addr(name);
  125. if (*data == 0) {
  126. #if OFFLOAD_DEBUG > 0
  127. if (console_enabled > 2) {
  128. __offload_funcs.dump();
  129. }
  130. #endif // OFFLOAD_DEBUG > 0
  131. LIBOFFLOAD_ERROR(c_receive_func_ptr, name);
  132. exit(1);
  133. }
  134. length = strlen(name) + 1;
  135. }
  136. else {
  137. *data = 0;
  138. length = 1;
  139. }
  140. buffer_ptr += length;
  141. tfr_size += length;
  142. }
  143. // End of the Marshaller and associated routines
  144. extern void *OFFLOAD_MALLOC(
  145. size_t size,
  146. size_t align
  147. )
  148. {
  149. void *ptr;
  150. int err;
  151. OFFLOAD_DEBUG_TRACE(2, "%s(%lld, %lld)\n", __func__, size, align);
  152. if (align < sizeof(void*)) {
  153. align = sizeof(void*);
  154. }
  155. ptr = _mm_malloc(size, align);
  156. if (ptr == NULL) {
  157. LIBOFFLOAD_ERROR(c_offload_malloc, size, align);
  158. exit(1);
  159. }
  160. OFFLOAD_DEBUG_TRACE(2, "%s returned %p\n", __func__, ptr);
  161. return ptr;
  162. }