int main (int argc, char ** argv, char **envp) { void * p1 = (void *) DkVirtualMemoryAlloc (NULL, pal_control.alloc_align * 4, 0, PAL_PROT_READ|PAL_PROT_WRITE); void * p2 = (void *) DkVirtualMemoryAlloc (NULL, pal_control.alloc_align * 4, 0, PAL_PROT_READ|PAL_PROT_WRITE); void * p3 = (void *) DkVirtualMemoryAlloc (NULL, pal_control.alloc_align * 2, 0, PAL_PROT_READ|PAL_PROT_WRITE); DkVirtualMemoryAlloc ((void *) (((uint64_t) p1 + (uint64_t) p2) / 2), pal_control.alloc_align * 4, 0, PAL_PROT_READ|PAL_PROT_WRITE); DkVirtualMemoryAlloc (p3, pal_control.alloc_align * 2, 0, PAL_PROT_READ|PAL_PROT_WRITE); DkVirtualMemoryFree (p3, pal_control.alloc_align); return 0; }
static void * __malloc (size_t size) { int flags = MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL; size = ALIGN_UP(size); void * addr = get_unmapped_vma(size, flags); addr = DkVirtualMemoryAlloc(addr, size, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (addr) bkeep_mmap(addr, size, PROT_READ|PROT_WRITE, flags, NULL, 0, "checkpoint"); return addr; }
int main (int argc, char ** argv) { if (argc < 2) { pal_printf("specify the port to open\n"); return 0; } char uri[60]; snprintf(uri, 60, "tcp.srv:127.0.0.1:%s", argv[1]); PAL_HANDLE srv = DkStreamOpen(uri, PAL_ACCESS_RDWR, 0, PAL_CREAT_TRY, 0); if (srv == NULL) { pal_printf("DkStreamOpen failed\n"); return -1; } void * buffer = (void *) DkVirtualMemoryAlloc(NULL, 4096, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (!buffer) { pal_printf("DkVirtualMemoryAlloc failed\n"); return -1; } PAL_HANDLE hdls[8]; int nhdls = 1, i; hdls[0] = srv; while(1) { PAL_HANDLE hdl = DkObjectsWaitAny(nhdls, hdls, NO_TIMEOUT); if (!hdl) continue; if (hdl == srv) { hdl = DkStreamWaitForClient(srv); if (!hdl) continue; if (nhdls >= 8) { pal_printf("[ ] connection rejected\n"); DkObjectClose(hdl); continue; } pal_printf("[%d] receive new connection\n", nhdls); hdls[nhdls++] = hdl; continue; } int cnt = 0; for (i = 0 ; i < nhdls ; i++) if (hdls[i] == hdl) cnt = i; int bytes = DkStreamRead(hdl, 0, 4096, buffer, NULL, 0); if (bytes == 0) { DkObjectClose(hdls[cnt]); if (cnt != nhdls - 1) hdls[cnt] = hdls[nhdls - 1]; nhdls--; continue; } ((char *) buffer)[bytes] = 0; pal_printf("[%d] %s", cnt, (char *) buffer); } return 0; }
int main (int argc, char ** argv, char ** envp) { volatile int c; DkSetExceptionHandler(handler, PAL_EVENT_MEMFAULT, 0); void * mem1 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 4, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (mem1) pal_printf("Memory Allocation OK\n"); void * mem2 = (void *) DkVirtualMemoryAlloc(NULL, UNIT, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (mem2) { c = count; *(volatile int *) mem2 = 0; pal_printf("(int *) %p = %d\n", mem2, *(volatile int *) mem2); if (c == count) pal_printf("Memory Allocation Protection (RW) OK\n"); DkVirtualMemoryProtect(mem2, UNIT, PAL_PROT_READ); c = count; *(volatile int *) mem2 = 0; asm volatile("nop"); if (c == count - 1) pal_printf("Memory Protection (R) OK\n"); DkVirtualMemoryFree(mem2, UNIT); c = count; *(volatile int *) mem2 = 0; asm volatile("nop"); if (c == count - 1) pal_printf("Memory Deallocation OK\n"); } void * mem3 = (void *) pal_control.user_address.start; void * mem4 = (void *) pal_control.user_address.end - UNIT; if (mem3 >= pal_control.executable_range.start && mem3 < pal_control.executable_range.end) mem3 = (void *) (((PAL_NUM) pal_control.executable_range.end + UNIT - 1) & ~(UNIT - 1)); mem3 = (void *) DkVirtualMemoryAlloc(mem3, UNIT, 0, PAL_PROT_READ|PAL_PROT_WRITE); mem4 = (void *) DkVirtualMemoryAlloc(mem4, UNIT, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (mem3 && mem4) pal_printf("Memory Allocation with Address OK\n"); /* total memory */ pal_printf("Total Memory: %llu\n", pal_control.mem_info.mem_total); unsigned long before = DkMemoryAvailableQuota(); void * mem5 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 1000, 0, PAL_PROT_READ|PAL_PROT_WRITE); if (mem5) { unsigned long after = before; for (int i = 0 ; i < 10000 ; i++) { for (void * ptr = mem5 ; ptr < mem5 + UNIT * 1000 ; ptr += UNIT) *(volatile int *) ptr = 0; unsigned long quota = DkMemoryAvailableQuota(); if (quota < after) after = quota; } pal_printf("Memory Qouta Before Allocation: %ld\n", before); pal_printf("Memory Qouta After Allocation: %ld\n", after); /* chance are some pages are evicted, so at least 80% accuracy */ if (before >= after + UNIT * 800) pal_printf("Get Memory Available Quota OK\n"); } return 0; }
int main(int argc, char ** argv) { char * name = "parent"; if (argc == 1) { const char * args[3]; char uri[20], uri2[20]; snprintf(uri2, 20, "file:%s", argv[0]); args[0] = "Ipc"; args[1] = uri; args[2] = NULL; void * mem = (void *) DkVirtualMemoryAlloc(NULL, pal_control.alloc_align, 0, PAL_PROT_READ|PAL_PROT_WRITE); pal_printf("mem = %p\n", mem); snprintf((char *) mem, 4096, "Hello World"); PAL_NUM key = 0; PAL_HANDLE chdl = DkCreatePhysicalMemoryChannel(&key); if (chdl == NULL) { pal_printf ("(parent) DkCreatePhysicalMemoryChannel Failed," " Make sure gipc module is loaded\n"); return 0; } snprintf(uri, 20, "gipc:%lld", key); PAL_HANDLE phdl = DkProcessCreate(uri2, 0, args); if (phdl == NULL) pal_printf ("ProcessCreate Failed\n"); PAL_PTR addr = (PAL_PTR) mem; PAL_NUM size = pal_control.alloc_align; DkPhysicalMemoryCommit(chdl, 1, &addr, &size, 0); DkObjectClose(chdl); char x; int rv = DkStreamRead(phdl, 0, 1, &x, NULL, 0); if (rv != 1) { pal_printf("Failed to get exit signal from child, %d\n", rv); return -1; } } else { name = argv[1]; PAL_HANDLE chdl = DkStreamOpen(name, 0, 0, 0, 0); if (chdl == NULL) { pal_printf("(child) StreamOpen Failed\n"); return 0; } PAL_PTR addr = NULL; PAL_NUM size = pal_control.alloc_align; PAL_FLG prot = PAL_PROT_READ|PAL_PROT_WRITE; int len = DkPhysicalMemoryMap (chdl, 1, &addr, &size, &prot); if (!len) { pal_printf("PhysicalMemoryMap Failed\n"); return 0; } pal_printf("(child) mem = %p\n", addr); pal_printf("(child) receive string: %s\n", (char *) addr); DkStreamDelete(chdl, 0); DkObjectClose(chdl); // Write a byte to the parent int rv = DkStreamWrite(pal_control.parent_process, 0, 1, "z", NULL); if (rv < 0) { pal_printf("Failed to write an exit byte\n"); return -1; } } pal_printf("Enter Main Thread (%s)\n", name); DkThreadDelayExecution (3000); pal_printf("Leave Main Thread\n"); return 0; }