X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Ftmpdisk.c;h=9a4371c590912777d2377f082dc5fdd8b495ca19;hb=198151f1d58834ff7889389007232a3d250f51f1;hp=450de5304b4b65e00c9ea851f30685a4fb762477;hpb=e2964f7234daa3429275c039769a313f0870c212;p=palacios.git diff --git a/palacios/src/devices/tmpdisk.c b/palacios/src/devices/tmpdisk.c index 450de53..9a4371c 100644 --- a/palacios/src/devices/tmpdisk.c +++ b/palacios/src/devices/tmpdisk.c @@ -19,7 +19,7 @@ #include #include - +#include struct blk_state { uint64_t capacity; @@ -32,7 +32,7 @@ struct blk_state { static uint64_t blk_get_capacity(void * private_data) { struct blk_state * blk = (struct blk_state *)private_data; - // PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity)); + // PrintDebug(info->vm_info, info, "SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity)); return blk->capacity; } @@ -42,10 +42,10 @@ static uint64_t blk_get_capacity(void * private_data) { static int blk_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) { struct blk_state * blk = (struct blk_state *)private_data; - // PrintDebug("TmpDisk Reading %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba); + // PrintDebug(info->vm_info, info, "TmpDisk Reading %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba); if (lba + num_bytes > blk->capacity) { - PrintError("TMPDISK Read past end of disk\n"); + PrintError(VM_NONE, VCORE_NONE, "TMPDISK Read past end of disk\n"); return -1; } @@ -60,10 +60,10 @@ static int blk_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * priv static int blk_write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) { struct blk_state * blk = (struct blk_state *)private_data; - // PrintDebug("TmpDisk Writing %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba); + // PrintDebug(VM_NONE, VCORE_NONE, "TmpDisk Writing %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba); if (lba + num_bytes > blk->capacity) { - PrintError("TMPDISK Write past end of disk\n"); + PrintError(VM_NONE, VCORE_NONE, "TMPDISK Write past end of disk\n"); return -1; } @@ -73,8 +73,11 @@ static int blk_write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * pr } -static int blk_free(struct vm_device * dev) { - return -1; +static int blk_free(struct blk_state * blk) { + V3_FreePages((void *)blk->blk_base_addr, blk->capacity / 4096); + + V3_Free(blk); + return 0; } @@ -87,7 +90,7 @@ static struct v3_dev_blk_ops blk_ops = { static struct v3_device_ops dev_ops = { - .free = blk_free, + .free = (int (*)(void *))blk_free, }; @@ -98,36 +101,50 @@ static int blk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { struct blk_state * blk = NULL; v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend"); char * dev_id = v3_cfg_val(cfg, "ID"); - uint64_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024 * 1024; + uint64_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024ULL * 1024ULL; if (!frontend_cfg) { - PrintError("Frontend Configuration not present\n"); + PrintError(vm, VCORE_NONE, "Frontend Configuration not present\n"); return -1; } - PrintDebug("Intializing TMPDISK (capacity=%d)\n", (uint32_t)capacity); + PrintDebug(vm, VCORE_NONE, "Intializing TMPDISK (capacity=%d)\n", (uint32_t)capacity); blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state)); + if (!blk) { + PrintError(vm, VCORE_NONE, "Cannot allocate in init\n"); + return -1; + } + blk->capacity = capacity; blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096); + + if (!blk->blk_base_addr) { + PrintError(vm, VCORE_NONE, "Cannot allocate block space\n"); + V3_Free(blk); + return -1; + } + blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr)); memset(blk->blk_space, 0, capacity); - struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, blk); + struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, blk); - if (v3_attach_device(vm, dev) == -1) { - PrintError("Could not attach device %s\n", dev_id); + if (dev == NULL) { + PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id); + V3_Free(blk); return -1; } if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), &blk_ops, frontend_cfg, blk) == -1) { - PrintError("Could not connect %s to frontend %s\n", + PrintError(vm, VCORE_NONE, "Could not connect %s to frontend %s\n", dev_id, v3_cfg_val(frontend_cfg, "tag")); + v3_remove_device(dev); return -1; }