X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=linux_usr%2Fv3_mem.c;h=183b9665117ec98c16284504cc29d756494ae51a;hb=700ce14388ea852b7599b73b0d5a9f94fd873045;hp=b02cdeb6b3ace2174705e3755ab47ca7c85284d0;hpb=d9bf51688652d1c40ff0e33c8a4a0c625688ce29;p=palacios.git diff --git a/linux_usr/v3_mem.c b/linux_usr/v3_mem.c index b02cdeb..183b966 100644 --- a/linux_usr/v3_mem.c +++ b/linux_usr/v3_mem.c @@ -30,9 +30,7 @@ int dir_filter(const struct dirent * dir) { -int dir_cmp(const void * d1, const void * d2) { - const struct dirent ** dir1 = (const struct dirent **)d1; - const struct dirent ** dir2 = (const struct dirent **)d2; +int dir_cmp(const struct dirent **dir1, const struct dirent ** dir2) { int num1 = atoi((*dir1)->d_name + 6); int num2 = atoi((*dir2)->d_name + 6); @@ -43,6 +41,7 @@ int dir_cmp(const void * d1, const void * d2) { int main(int argc, char * argv[]) { unsigned long long mem_size_bytes = 0; + unsigned long long mem_min_start = 0; unsigned int block_size_bytes = 0; int bitmap_entries = 0; unsigned char * bitmap = NULL; @@ -50,15 +49,22 @@ int main(int argc, char * argv[]) { int reg_start = 0; int mem_ready = 0; - if (argc != 2) { - printf("Usage: v3_mem \n"); - return -1; + if (argc != 2 && argc != 3) { + printf("usage: v3_mem [min_start (MB)]\n\n" + "Offline memory for use in Palacios.\n" + "min_start is the minimum allowable starting address.\n" + "this is zero by default\n\n"); + return -1; } mem_size_bytes = atoll(argv[1]) * (1024 * 1024); - printf("Trying to find %dMB (%d bytes) of memory\n", atoll(argv[1]), mem_size_bytes); + if (argc==3) { + mem_min_start = atoll(argv[1]) * (1024 * 1024); + } + + printf("Trying to find %dMB (%d bytes) of memory above %llu\n", atoll(argv[1]), mem_size_bytes, mem_min_start); /* Figure out the block size */ { @@ -71,24 +77,28 @@ int main(int argc, char * argv[]) { perror("Could not open block size file: " SYS_PATH "block_size_bytes"); return -1; } - + if (read(tmp_fd, tmp_buf, BUF_SIZE) <= 0) { perror("Could not read block size file: " SYS_PATH "block_size_bytes"); return -1; } - + close(tmp_fd); block_size_bytes = strtoll(tmp_buf, NULL, 16); printf("Memory block size is %dMB (%d bytes)\n", block_size_bytes / (1024 * 1024), block_size_bytes); + } num_blocks = mem_size_bytes / block_size_bytes; - if (block_size_bytes % mem_size_bytes) num_blocks++; + if (mem_size_bytes % block_size_bytes) num_blocks++; - printf("Looking for %d blocks of memory\n", num_blocks); + mem_min_start = block_size_bytes * + ((mem_min_start / block_size_bytes) + (!!(mem_min_start % block_size_bytes))); + + printf("Looking for %d blocks of memory starting at %p (block %llu)\n", num_blocks, (void*)mem_min_start, mem_min_start/block_size_bytes); // We now need to find consecutive offlinable memory blocks @@ -98,19 +108,26 @@ int main(int argc, char * argv[]) { struct dirent ** namelist = NULL; int size = 0; int i = 0; + int j = 0; + int last_block = 0; + int first_block = mem_min_start/block_size_bytes; - bitmap_entries = scandir(SYS_PATH, &namelist, dir_filter, dir_cmp); + last_block = scandir(SYS_PATH, &namelist, dir_filter, dir_cmp); + bitmap_entries = atoi(namelist[last_block - 1]->d_name + 6) + 1; size = bitmap_entries / 8; if (bitmap_entries % 8) size++; bitmap = malloc(size); + if (!bitmap) { + printf("ERROR: could not allocate space for bitmap\n"); + return -1; + } + memset(bitmap, 0, size); - for (i = 0; i < bitmap_entries; i++) { + for (i = 0 ; j < bitmap_entries - 1; i++) { struct dirent * tmp_dir = namelist[i]; - int major = i / 8; - int minor = i % 8; int block_fd = 0; char status_str[BUF_SIZE]; char fname[BUF_SIZE]; @@ -120,10 +137,20 @@ int main(int argc, char * argv[]) { snprintf(fname, BUF_SIZE, "%s%s/removable", SYS_PATH, tmp_dir->d_name); + j = atoi(tmp_dir->d_name + 6); + int major = j / 8; + int minor = j % 8; + + + if (i= num_blocks) { @@ -177,7 +203,7 @@ int main(int argc, char * argv[]) { } } - + if (run_len < num_blocks) { fprintf(stderr, "Could not find enough consecutive memory blocks... (found %d)\n", run_len); return -1; @@ -196,7 +222,7 @@ int main(int argc, char * argv[]) { memset(fname, 0, 256); snprintf(fname, 256, "%smemory%d/state", SYS_PATH, i + reg_start); - + block_file = fopen(fname, "r+"); if (block_file == NULL) { @@ -224,7 +250,7 @@ int main(int argc, char * argv[]) { for (i = 0; i < num_blocks; i++) { - int block_fd = NULL; + int block_fd = 0; char fname[BUF_SIZE]; char status_buf[BUF_SIZE]; @@ -234,14 +260,14 @@ int main(int argc, char * argv[]) { snprintf(fname, BUF_SIZE, "%smemory%d/state", SYS_PATH, i + reg_start); - + block_fd = open(fname, O_RDONLY); if (block_fd == -1) { perror("Could not open block file"); return -1; } - + if (read(block_fd, status_buf, BUF_SIZE) <= 0) { perror("Could not read block status"); return -1; @@ -281,7 +307,6 @@ int main(int argc, char * argv[]) { fclose(block_file); } - break; } @@ -301,10 +326,10 @@ int main(int argc, char * argv[]) { { int v3_fd = 0; struct v3_mem_region mem; - unsigned long long num_bytes = num_blocks * block_size_bytes; - unsigned long long base_addr = reg_start * block_size_bytes; + unsigned long long num_bytes = (unsigned long long)(num_blocks) * (unsigned long long)(block_size_bytes); + unsigned long long base_addr = (unsigned long long)(reg_start) * (unsigned long long)(block_size_bytes); - printf("Giving Palacios %dMB of memory at (%p) \n", + printf("Giving Palacios %lluMB of memory at (%p) \n", num_bytes / (1024 * 1024), base_addr); mem.base_addr = base_addr;