/*===========================================================================* * test_map * *===========================================================================*/ void test_map(void) { char buf_buf[CLICK_SIZE * 2]; char buf_buf2[CLICK_SIZE * 2]; char *buf, *buf2; char get_buf[CLICK_SIZE]; int *p; volatile int *p2; int *get_p; size_t get_len; int is; int r; buf = (char*) CLICK_CEIL(buf_buf); buf2 = (char*) CLICK_CEIL(buf_buf2); p = (int *)buf; p2 = (int *)buf2; get_p = (int *)get_buf; *p = 1; r = ds_publish_map(key_map, buf, CLICK_SIZE, 0); assert(r == OK); r = ds_snapshot_map(key_map, &is); assert(r == OK); /* Copy the mapped memory range. * Set *p=2, then the mapped memory range should change too * and *get_p should be 2. */ *p = 2; get_len = CLICK_SIZE; r = ds_retrieve_map(key_map, get_buf, &get_len, 0, DSMF_COPY_MAPPED); assert(r == OK && get_len == CLICK_SIZE && *get_p == 2); /* Copy snapshot, where *get_p should still be 1. */ get_len = CLICK_SIZE; r = ds_retrieve_map(key_map, get_buf, &get_len, is, DSMF_COPY_SNAPSHOT); assert(r == OK && get_len == CLICK_SIZE && *get_p == 1); /* Map the mapped memory range to @buf2, then set *p=3, which * in turn should let *p2=3. */ get_len = CLICK_SIZE; r = ds_retrieve_map(key_map, buf2, &get_len, 0, DSMF_MAP_MAPPED); assert(r == OK && get_len == CLICK_SIZE); *p = 3; assert(*p2 == 3); r = ds_delete_map(key_map); assert(r == OK); printf("DSTEST: MAP test successful!\n"); }
/*===========================================================================* * rs_memctl_map_prealloc * *===========================================================================*/ static int rs_memctl_map_prealloc(struct vmproc *vmp, vir_bytes *addr, size_t *len) { struct vir_region *vr; vir_bytes base, top; int is_vm; if(*len <= 0) { return EINVAL; } *len = CLICK_CEIL(*len); is_vm = (vmp->vm_endpoint == VM_PROC_NR); base = is_vm ? VM_OWN_MMAPBASE : VM_MMAPBASE; top = is_vm ? VM_OWN_MMAPTOP : VM_MMAPTOP; if (!(vr = map_page_region(vmp, base, top, *len, VR_ANON|VR_WRITABLE|VR_UNINITIALIZED, MF_PREALLOC, &mem_type_anon))) { return ENOMEM; } vr->flags |= VR_PREALLOC_MAP; *addr = vr->vaddr; return OK; }
/*===========================================================================* * main * *===========================================================================*/ int main(int argc, char **argv) { endpoint_t ep_self, ep_requestor, ep_child; cp_grant_id_t gid; int i, r, pid; char *buf; int status; /* SEF local startup. */ env_setargs(argc, argv); sef_local_startup(); /* Prepare work. */ buf = (char*) CLICK_CEIL(buf_buf); fid_send = open(FIFO_GRANTOR, O_WRONLY); fid_get = open(FIFO_REQUESTOR, O_RDONLY); if(fid_get < 0 || fid_send < 0) { printf("GRANTOR: can't open fifo files.\n"); return 1; } /* Get the requestor's endpoint. */ read(fid_get, &ep_requestor, sizeof(ep_requestor)); dprint("GRANTOR: getting requestor's endpoint: %d\n", ep_requestor); /* Grant. */ gid = cpf_grant_direct(ep_requestor, (long)buf, BUF_SIZE, CPF_READ | CPF_WRITE | CPF_MAP); ep_self = getprocnr(); dprint("GRANTOR: sending my endpoint %d and gid %d\n", ep_self, gid); write(fid_send, &ep_self, sizeof(ep_self)); write(fid_send, &gid, sizeof(gid)); /* Test safemap. */ buf[0] = 0; FIFO_NOTIFY(fid_send); FIFO_WAIT(fid_get); return 0; }
/*===========================================================================* * get_mem_chunks * *===========================================================================*/ void get_mem_chunks( struct memory *mem_chunks) /* store mem chunks here */ { /* Initialize the free memory list from the kernel-provided memory map. Translate * the byte offsets and sizes in this list to clicks, properly truncated. */ phys_bytes base, size, limit; int i; struct memory *memp; /* Initialize everything to zero. */ memset(mem_chunks, 0, NR_MEMS*sizeof(*mem_chunks)); /* Obtain and parse memory from kernel environment. */ /* XXX Any memory chunk in excess of NR_MEMS is silently ignored. */ for(i = 0; i < MIN(MAXMEMMAP, NR_MEMS); i++) { mem_chunks[i].base = kernel_boot_info.memmap[i].mm_base_addr; mem_chunks[i].size = kernel_boot_info.memmap[i].mm_length; } /* Round physical memory to clicks. Round start up, round end down. */ for (i = 0; i < NR_MEMS; i++) { memp = &mem_chunks[i]; /* next mem chunk is stored here */ base = mem_chunks[i].base; size = mem_chunks[i].size; limit = base + size; base = (phys_bytes) (CLICK_CEIL(base)); limit = (phys_bytes) (CLICK_FLOOR(limit)); if (limit <= base) { memp->base = memp->size = 0; } else { memp->base = base >> CLICK_SHIFT; memp->size = (limit - base) >> CLICK_SHIFT; } } }
/*===========================================================================* * main * *===========================================================================*/ int main(int argc, char **argv) { endpoint_t ep_self, ep_child; size_t size = BUF_SIZE; int i, r, pid; int status; u64_t start, end, diff; double micros; char nr_pages_str[10], is_map_str[2], is_write_str[2]; int nr_pages, is_map, is_write; /* SEF local startup. */ env_setargs(argc, argv); sef_local_startup(); /* Parse the command line. */ r = env_get_param("pages", nr_pages_str, sizeof(nr_pages_str)); errno = 0; nr_pages = atoi(nr_pages_str); if (r != OK || errno || nr_pages <=0) { exit_usage(); } if(nr_pages > TEST_PAGE_NUM) { printf("REQUESTOR: too many pages. Max allowed: %d\n", TEST_PAGE_NUM); exit_usage(); } r = env_get_param("map", is_map_str, sizeof(is_map_str)); errno = 0; is_map = atoi(is_map_str); if (r != OK || errno || (is_map!=0 && is_map!=1)) { exit_usage(); } r = env_get_param("write", is_write_str, sizeof(is_write_str)); errno = 0; is_write = atoi(is_write_str); if (r != OK || errno || (is_write!=0 && is_write!=1)) { exit_usage(); } printf("REQUESTOR: Running tests with pages=%d map=%d write=%d...\n", nr_pages, is_map, is_write); /* Prepare work. */ buf = (char*) CLICK_CEIL(buf_buf); fid_get = open(FIFO_GRANTOR, O_RDONLY); fid_send = open(FIFO_REQUESTOR, O_WRONLY); if(fid_get < 0 || fid_send < 0) { printf("REQUESTOR: can't open fifo files.\n"); return 1; } /* Send the endpoint to the granter, in order to let him to * create the grant. */ ep_self = getprocnr(); write(fid_send, &ep_self, sizeof(ep_self)); dprint("REQUESTOR: sending my endpoint: %d\n", ep_self); /* Get the granter's endpoint and gid. */ read(fid_get, &ep_granter, sizeof(ep_granter)); read(fid_get, &gid, sizeof(gid)); dprint("REQUESTOR: getting granter's endpoint %d and gid %d\n", ep_granter, gid); FIFO_WAIT(fid_get); diff = make64(0, 0); if(is_map) { /* Test safemap. */ for(i=0;i<NR_TEST_ITERATIONS;i++) { read_tsc_64(&start); r = sys_safemap(ep_granter, gid, 0, (long)buf, nr_pages*CLICK_SIZE, D, 1); if(r != OK) { printf("REQUESTOR: safemap error: %d\n", r); return 1; } read_write_buff(buf, nr_pages*CLICK_SIZE, is_write); read_tsc_64(&end); diff = add64(diff, (sub64(end, start))); r = sys_safeunmap(D, (long)buf); if(r != OK) { printf("REQUESTOR: safeunmap error: %d\n", r); return 1; } } micros = ((double)tsc_64_to_micros(diff)) / (NR_TEST_ITERATIONS*nr_pages); REPORT_TEST("REQUESTOR", "SAFEMAP", micros); } else { /* Test safecopy. */ for(i=0;i<NR_TEST_ITERATIONS;i++) { read_tsc_64(&start); r = sys_safecopyfrom(ep_granter, gid, 0, (long)buf, nr_pages*CLICK_SIZE, D); if(r != OK) { printf("REQUESTOR: safecopy error: %d\n", r); return 1; } read_write_buff(buf, nr_pages*CLICK_SIZE, is_write); read_tsc_64(&end); diff = add64(diff, (sub64(end, start))); } micros = ((double)tsc_64_to_micros(diff)) / (NR_TEST_ITERATIONS*nr_pages); REPORT_TEST("REQUESTOR", "SAFECOPY", micros); } FIFO_NOTIFY(fid_send); return 0; }
/*===========================================================================* * main * *===========================================================================*/ int main(int argc, char **argv) { endpoint_t ep_self, ep_child; size_t size = BUF_SIZE; int i, r, pid; int status; /* SEF local startup. */ env_setargs(argc, argv); sef_local_startup(); /* Prepare work. */ buf = (char*) CLICK_CEIL(buf_buf); fid_get = open(FIFO_GRANTOR, O_RDONLY); fid_send = open(FIFO_REQUESTOR, O_WRONLY); if(fid_get < 0 || fid_send < 0) { printf("REQUESTOR: can't open fifo files.\n"); return 1; } /* Send the endpoint to the granter, in order to let him to * create the grant. */ ep_self = getprocnr(); write(fid_send, &ep_self, sizeof(ep_self)); dprint("REQUESTOR: sending my endpoint: %d\n", ep_self); /* Get the granter's endpoint and gid. */ read(fid_get, &ep_granter, sizeof(ep_granter)); read(fid_get, &gid, sizeof(gid)); dprint("REQUESTOR: getting granter's endpoint %d and gid %d\n", ep_granter, gid); /* Test MAP. */ FIFO_WAIT(fid_get); r = sys_safemap(ep_granter, gid, 0, (long)buf, size, 1); if(r != OK) { printf("REQUESTOR: error in sys_safemap: %d\n", r); return 1; } CHECK_TEST("REQUESTOR", buf[0], BUF_START_GRANTOR, "MAP"); buf[0] = BUF_START_REQUESTOR; r = sys_safeunmap((long)buf); if(r != OK) { printf("REQUESTOR: error in sys_safeunmap: %d\n", r); return 1; } FIFO_NOTIFY(fid_send); /* Test UNMAP. */ FIFO_WAIT(fid_get); CHECK_TEST("REQUESTOR", buf[0], BUF_START_REQUESTOR, "UNMAP"); r = sys_safemap(ep_granter, gid, 0, (long)buf, size, 1); if(r != 0) { printf("REQUESTOR: error in sys_safemap: %d\n", r); return 1; } FIFO_NOTIFY(fid_send); /* Test REVOKE. */ FIFO_WAIT(fid_get); CHECK_TEST("REQUESTOR", buf[0], BUF_START_GRANTOR, "REVOKE"); buf[0] = BUF_START_REQUESTOR; FIFO_NOTIFY(fid_send); /* Test SMAP_COW. */ FIFO_WAIT(fid_get); r = sys_safemap(ep_granter, gid, 0, (long)buf, size, 1); if(r != OK) { printf("REQUESTOR: error in sys_safemap: %d\n", r); return 1; } buf[0] = BUF_START_REQUESTOR; pid = fork(); if(pid < 0) { printf("REQUESTOR: error in fork\n"); return 1; } if(pid == 0) { exit(buf[0] != BUF_START_REQUESTOR); } FIFO_NOTIFY(fid_send); FIFO_WAIT(fid_get); ep_child = getnprocnr(pid); if ((r = sys_privctl(ep_child, SYS_PRIV_SET_USER, NULL)) != OK) { printf("REQUESTOR: unable to set privileges: %d\n", r); return 1; } if ((r = sys_privctl(ep_child, SYS_PRIV_ALLOW, NULL)) != OK) { printf("REQUESTOR: child process can't run: %d\n", r); return 1; } wait(&status); FIFO_NOTIFY(fid_send); CHECK_TEST("REQUESTOR", buf[0], BUF_START_GRANTOR, "SMAP_COW"); CHECK_TEST("REQUESTOR", 1, WIFEXITED(status) && (WEXITSTATUS(status) == 0), "SMAP_COW child"); /* Test COW_SMAP. */ FIFO_WAIT(fid_get); buf[0] = BUF_START_REQUESTOR; r = sys_safemap(ep_granter, gid, 0, (long)buf, size, 1); if(r != OK) { printf("REQUESTOR: error in sys_safemap: %d\n", r); return 1; } FIFO_NOTIFY(fid_send); FIFO_WAIT(fid_get); CHECK_TEST("REQUESTOR", buf[0], BUF_START_GRANTOR+1, "COW_SMAP"); /* Test COW_SMAP2 (with COW safecopy). */ FIFO_WAIT(fid_get); buf[0] = BUF_START_REQUESTOR; r = sys_safecopyto(ep_granter, gid, 0, (long)buf, size); if(r != OK) { printf("REQUESTOR: error in sys_safecopyto: %d\n", r); return 1; } r = sys_safemap(ep_granter, gid, 0, (long)buf, size, 1); if(r != OK) { printf("REQUESTOR: error in sys_safemap: %d\n", r); return 1; } FIFO_NOTIFY(fid_send); CHECK_TEST("REQUESTOR", buf[0], BUF_START_REQUESTOR, "COW_SMAP2"); return 0; }