Ipc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. #define UNIT (pal_control.alloc_align)
  5. static const char* volatile message = NULL;
  6. void handler(PAL_PTR event, PAL_NUM arg, PAL_CONTEXT* context) {
  7. if (message)
  8. pal_printf("%s", message);
  9. while (*(unsigned char*)context->rip != 0x90) {
  10. context->rip++;
  11. }
  12. DkExceptionReturn(event);
  13. }
  14. int main(int argc, char** argv, char** envp) {
  15. char gipc_uri[20];
  16. int ret;
  17. DkSetExceptionHandler(handler, PAL_EVENT_MEMFAULT);
  18. if (argc > 1 && !memcmp(argv[1], "Child", 6)) {
  19. /* private memory */
  20. ret = DkStreamRead(pal_control.parent_process, 0, 20, gipc_uri, NULL, 0);
  21. if (ret > 0) {
  22. PAL_HANDLE ipc1 = DkStreamOpen(gipc_uri, 0, 0, 0, 0);
  23. if (ipc1) {
  24. pal_printf("Join Physical Memory Store OK\n");
  25. PAL_PTR mem_addr = 0;
  26. PAL_NUM mem_size = UNIT;
  27. PAL_FLG mem_prot = PAL_PROT_READ | PAL_PROT_WRITE;
  28. ret = DkPhysicalMemoryMap(ipc1, 1, &mem_addr, &mem_size, &mem_prot);
  29. if (ret > 0) {
  30. pal_printf("[Test 1] Physical Memory Map : %s\n", (char*)mem_addr);
  31. memcpy((void*)mem_addr, "Hello World, Bob", 20);
  32. pal_printf("[Test 1] Receiver After Map : %s\n", (char*)mem_addr);
  33. }
  34. ret = 0;
  35. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &ret, NULL);
  36. DkObjectClose(ipc1);
  37. }
  38. }
  39. /* private untouched memory */
  40. ret = DkStreamRead(pal_control.parent_process, 0, 20, gipc_uri, NULL, 0);
  41. if (ret > 0) {
  42. PAL_HANDLE ipc2 = DkStreamOpen(gipc_uri, 0, 0, 0, 0);
  43. if (ipc2) {
  44. pal_printf("Join Physical Memory Store OK\n");
  45. PAL_PTR mem_addr = 0;
  46. PAL_NUM mem_size = UNIT;
  47. PAL_FLG mem_prot = PAL_PROT_READ | PAL_PROT_WRITE;
  48. ret = DkPhysicalMemoryMap(ipc2, 1, &mem_addr, &mem_size, &mem_prot);
  49. if (ret > 0) {
  50. pal_printf("[Test 2] Physical Memory Map : %s\n", (char*)mem_addr);
  51. memcpy((void*)mem_addr, "Hello World, Bob", 20);
  52. pal_printf("[Test 2] Receiver After Map : %s\n", (char*)mem_addr);
  53. }
  54. ret = 0;
  55. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &ret, NULL);
  56. DkStreamDelete(ipc2, 0);
  57. DkObjectClose(ipc2);
  58. }
  59. }
  60. /* file-backed memory */
  61. ret = DkStreamRead(pal_control.parent_process, 0, 20, gipc_uri, NULL, 0);
  62. if (ret > 0) {
  63. PAL_HANDLE ipc3 = DkStreamOpen(gipc_uri, 0, 0, 0, 0);
  64. if (ipc3) {
  65. pal_printf("Join Physical Memory Store OK\n");
  66. PAL_PTR mem_addr = 0;
  67. PAL_NUM mem_size = UNIT;
  68. PAL_FLG mem_prot = PAL_PROT_READ | PAL_PROT_WRITE;
  69. ret = DkPhysicalMemoryMap(ipc3, 1, &mem_addr, &mem_size, &mem_prot);
  70. if (ret > 0) {
  71. pal_printf("[Test 3] Physical Memory Map : %s\n", (char*)mem_addr);
  72. memcpy((void*)mem_addr, "Hello World, Bob", 20);
  73. pal_printf("[Test 3] Receiver After Map : %s\n", (char*)mem_addr);
  74. }
  75. ret = 0;
  76. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &ret, NULL);
  77. DkObjectClose(ipc3);
  78. }
  79. }
  80. /* file-backed memory beyond file size */
  81. ret = DkStreamRead(pal_control.parent_process, 0, 20, gipc_uri, NULL, 0);
  82. if (ret > 0) {
  83. PAL_HANDLE ipc4 = DkStreamOpen(gipc_uri, 0, 0, 0, 0);
  84. if (ipc4) {
  85. pal_printf("Join Physical Memory Store OK\n");
  86. PAL_PTR mem_addr = 0;
  87. PAL_NUM mem_size = UNIT;
  88. PAL_FLG mem_prot = PAL_PROT_READ | PAL_PROT_WRITE;
  89. ret = DkPhysicalMemoryMap(ipc4, 1, &mem_addr, &mem_size, &mem_prot);
  90. if (ret > 0) {
  91. message = "[Test 4] Physical Memory Map : Memory Fault\n";
  92. *(volatile int*)mem_addr;
  93. __asm__ volatile("nop");
  94. message = NULL;
  95. }
  96. ret = 0;
  97. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &ret, NULL);
  98. DkObjectClose(ipc4);
  99. }
  100. }
  101. /* large memory */
  102. ret = DkStreamRead(pal_control.parent_process, 0, 20, gipc_uri, NULL, 0);
  103. if (ret > 0) {
  104. PAL_HANDLE ipc5 = DkStreamOpen(gipc_uri, 0, 0, 0, 0);
  105. if (ipc5) {
  106. pal_printf("Join Physical Memory Store OK\n");
  107. PAL_PTR mem_addr = 0;
  108. PAL_NUM mem_size = UNIT * 1024 * 64;
  109. PAL_FLG mem_prot = PAL_PROT_READ | PAL_PROT_WRITE;
  110. ret = DkPhysicalMemoryMap(ipc5, 1, &mem_addr, &mem_size, &mem_prot);
  111. if (ret > 0) {
  112. pal_printf("[Test 5] Physical Memory Map : %s\n",
  113. (char*)mem_addr + UNIT * 1024);
  114. }
  115. ret = 0;
  116. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &ret, NULL);
  117. DkObjectClose(ipc5);
  118. }
  119. }
  120. } else {
  121. PAL_STR args[3] = {"Ipc", "Child", 0};
  122. PAL_HANDLE proc = DkProcessCreate("file:Ipc", args);
  123. if (!proc)
  124. return 0;
  125. /* private memory */
  126. PAL_NUM key1;
  127. PAL_HANDLE ipc1 = DkCreatePhysicalMemoryChannel(&key1);
  128. if (ipc1) {
  129. snprintf(gipc_uri, 20, "gipc:%ld", key1);
  130. pal_printf("Create Physical Memory Store OK\n");
  131. void* mem1 = (void*)DkVirtualMemoryAlloc(NULL, UNIT, 0, PAL_PROT_READ | PAL_PROT_WRITE);
  132. if (mem1) {
  133. memcpy(mem1, "Hello World", 20);
  134. PAL_PTR mem_addr = mem1;
  135. PAL_NUM mem_size = UNIT;
  136. if (DkPhysicalMemoryCommit(ipc1, 1, &mem_addr, &mem_size)) {
  137. pal_printf("[Test 1] Physical Memory Commit OK\n");
  138. memcpy(mem1, "Hello World, Alice", 20);
  139. pal_printf("[Test 1] Sender After Commit: %s\n", (char*)mem1);
  140. DkStreamWrite(proc, 0, 20, gipc_uri, NULL);
  141. memcpy(mem1, "Alice, Hello World", 20);
  142. pal_printf("[Test 1] Sender Before Map : %s\n", (char*)mem1);
  143. DkStreamRead(proc, 0, sizeof(int), &ret, NULL, 0);
  144. pal_printf("[Test 1] Sender After Map : %s\n", (char*)mem1);
  145. }
  146. }
  147. DkObjectClose(ipc1);
  148. }
  149. /* private untouched memory */
  150. PAL_NUM key2;
  151. PAL_HANDLE ipc2 = DkCreatePhysicalMemoryChannel(&key2);
  152. if (ipc2) {
  153. snprintf(gipc_uri, 20, "gipc:%ld", key2);
  154. pal_printf("Create Physical Memory Store OK\n");
  155. void* mem2 = (void*)DkVirtualMemoryAlloc(NULL, UNIT, 0, PAL_PROT_READ | PAL_PROT_WRITE);
  156. if (mem2) {
  157. PAL_PTR mem_addr = mem2;
  158. PAL_NUM mem_size = UNIT;
  159. if (DkPhysicalMemoryCommit(ipc2, 1, &mem_addr, &mem_size)) {
  160. pal_printf("[Test 2] Physical Memory Commit OK\n");
  161. memcpy(mem2, "Hello World, Alice", 20);
  162. pal_printf("[Test 2] Sender After Commit: %s\n", (char*)mem2);
  163. DkStreamWrite(proc, 0, 20, gipc_uri, NULL);
  164. memcpy(mem2, "Alice, Hello World", 20);
  165. pal_printf("[Test 2] Sender Before Map : %s\n", (char*)mem2);
  166. DkStreamRead(proc, 0, sizeof(int), &ret, NULL, 0);
  167. pal_printf("[Test 2] Sender After Map : %s\n", (char*)mem2);
  168. }
  169. }
  170. DkObjectClose(ipc2);
  171. }
  172. /* file-backed memory */
  173. PAL_NUM key3;
  174. PAL_HANDLE ipc3 = DkCreatePhysicalMemoryChannel(&key3);
  175. if (ipc3) {
  176. snprintf(gipc_uri, 20, "gipc:%ld", key3);
  177. pal_printf("Create Physical Memory Store OK\n");
  178. void* mem3 = NULL;
  179. PAL_HANDLE file1 = DkStreamOpen("file:ipc_mapping.tmp", PAL_ACCESS_RDWR, 0, 0, 0);
  180. if (file1) {
  181. mem3 = (void*)DkStreamMap(file1, NULL, PAL_PROT_READ | PAL_PROT_WRITE, 0, UNIT);
  182. DkObjectClose(file1);
  183. }
  184. if (mem3) {
  185. PAL_PTR mem_addr = mem3;
  186. PAL_NUM mem_size = UNIT;
  187. if (DkPhysicalMemoryCommit(ipc3, 1, &mem_addr, &mem_size)) {
  188. pal_printf("[Test 3] Physical Memory Commit OK\n");
  189. DkStreamWrite(proc, 0, 20, gipc_uri, NULL);
  190. pal_printf("[Test 3] Sender After Commit: %s\n", (char*)mem3);
  191. DkStreamRead(proc, 0, sizeof(int), &ret, NULL, 0);
  192. pal_printf("[Test 3] Sender After Map : %s\n", (char*)mem3);
  193. }
  194. }
  195. DkObjectClose(ipc3);
  196. }
  197. /* file-backed memory beyond file size */
  198. PAL_NUM key4;
  199. PAL_HANDLE ipc4 = DkCreatePhysicalMemoryChannel(&key4);
  200. if (ipc4) {
  201. snprintf(gipc_uri, 20, "gipc:%ld", key4);
  202. pal_printf("Create Physical Memory Store OK\n");
  203. void* mem4 = NULL;
  204. PAL_HANDLE file2 = DkStreamOpen("file:ipc_mapping.tmp", PAL_ACCESS_RDWR, 0, 0, 0);
  205. if (file2) {
  206. mem4 = (void*)DkStreamMap(file2, NULL, PAL_PROT_READ | PAL_PROT_WRITE, UNIT, UNIT);
  207. DkObjectClose(file2);
  208. }
  209. if (mem4) {
  210. PAL_PTR mem_addr = mem4;
  211. PAL_NUM mem_size = UNIT;
  212. if (DkPhysicalMemoryCommit(ipc4, 1, &mem_addr, &mem_size)) {
  213. pal_printf("[Test 4] Physical Memory Commit OK\n");
  214. DkStreamWrite(proc, 0, 20, gipc_uri, NULL);
  215. DkStreamRead(proc, 0, sizeof(int), &ret, NULL, 0);
  216. }
  217. }
  218. DkObjectClose(ipc4);
  219. }
  220. /* large memory */
  221. PAL_NUM key5;
  222. PAL_HANDLE ipc5 = DkCreatePhysicalMemoryChannel(&key5);
  223. if (ipc5) {
  224. snprintf(gipc_uri, 20, "gipc:%ld", key5);
  225. pal_printf("Create Physical Memory Store OK\n");
  226. void* mem5 = (void*)DkVirtualMemoryAlloc(NULL, UNIT * 1024 * 64, 0,
  227. PAL_PROT_READ | PAL_PROT_WRITE);
  228. if (mem5) {
  229. pal_printf("Touch Memory at %p\n", mem5 + UNIT * 1024);
  230. memcpy(mem5 + UNIT * 1024, "Hello World", 20);
  231. PAL_PTR mem_addr = mem5;
  232. PAL_NUM mem_size = UNIT * 1024 * 64;
  233. DkStreamWrite(proc, 0, 20, gipc_uri, NULL);
  234. if (DkPhysicalMemoryCommit(ipc5, 1, &mem_addr, &mem_size)) {
  235. pal_printf("[Test 5] Physical Memory Commit OK\n");
  236. DkStreamRead(proc, 0, sizeof(int), &ret, NULL, 0);
  237. }
  238. }
  239. DkObjectClose(ipc1);
  240. }
  241. }
  242. return 0;
  243. }