2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
32 static uint64_t blk_get_capacity(void * private_data) {
33 struct blk_state * blk = (struct blk_state *)private_data;
35 // PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
42 static int blk_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
43 struct blk_state * blk = (struct blk_state *)private_data;
45 // PrintDebug("TmpDisk Reading %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
47 if (lba + num_bytes > blk->capacity) {
48 PrintError("TMPDISK Read past end of disk\n");
52 memcpy(buf, blk->blk_space + lba, num_bytes);
60 static int blk_write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
61 struct blk_state * blk = (struct blk_state *)private_data;
63 // PrintDebug("TmpDisk Writing %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
65 if (lba + num_bytes > blk->capacity) {
66 PrintError("TMPDISK Write past end of disk\n");
70 memcpy(blk->blk_space + lba, buf, num_bytes);
76 static int blk_free(struct blk_state * blk) {
77 V3_FreePages((void *)blk->blk_base_addr, blk->capacity / 4096);
84 static struct v3_dev_blk_ops blk_ops = {
87 .get_capacity = blk_get_capacity,
92 static struct v3_device_ops dev_ops = {
93 .free = (int (*)(void *))blk_free,
100 static int blk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
101 struct blk_state * blk = NULL;
102 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
103 char * dev_id = v3_cfg_val(cfg, "ID");
104 uint64_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024 * 1024;
107 PrintError("Frontend Configuration not present\n");
111 PrintDebug("Intializing TMPDISK (capacity=%d)\n", (uint32_t)capacity);
114 blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state));
116 blk->capacity = capacity;
118 blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096);
119 blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr));
120 memset(blk->blk_space, 0, capacity);
123 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, blk);
126 PrintError("Could not attach device %s\n", dev_id);
131 if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"),
132 &blk_ops, frontend_cfg, blk) == -1) {
133 PrintError("Could not connect %s to frontend %s\n",
134 dev_id, v3_cfg_val(frontend_cfg, "tag"));
135 v3_remove_device(dev);
143 device_register("TMPDISK", blk_init)