Ipc.c 12 KB

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