Ipc.c 13 KB

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