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>
22 #include <devices/lnx_virtio_blk.h>
25 #define BLK_CAPACITY (500 * 1024 * 1024)
32 struct vm_device * blk_dev;
41 static uint64_t blk_get_capacity(void * private_data) {
42 struct vm_device * dev = (struct vm_device *)private_data;
43 struct blk_state * blk = (struct blk_state *)(dev->private_data);
45 PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
47 return blk->capacity / HD_SECTOR_SIZE;
52 static int blk_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
53 struct vm_device * dev = (struct vm_device *)private_data;
54 struct blk_state * blk = (struct blk_state *)(dev->private_data);
55 uint32_t offset = lba * HD_SECTOR_SIZE;
56 uint32_t length = sector_count * HD_SECTOR_SIZE;
58 memcpy(buf, blk->blk_space + offset, length);
66 static int blk_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
67 struct vm_device * dev = (struct vm_device *)private_data;
68 struct blk_state * blk = (struct blk_state *)(dev->private_data);
69 uint32_t offset = lba * HD_SECTOR_SIZE;
70 uint32_t length = sector_count * HD_SECTOR_SIZE;
72 memcpy(blk->blk_space + offset, buf, length);
78 static int blk_free(struct vm_device * dev) {
83 static struct v3_hd_ops hd_ops = {
86 .get_capacity = blk_get_capacity,
91 static struct v3_device_ops dev_ops = {
101 static int blk_init(struct guest_info * vm, void * cfg_data) {
102 struct blk_state * blk = NULL;
103 struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data);
106 PrintError("could not find Virtio backend\n");
110 PrintDebug("Creating Blk Device\n");
112 if (virtio_blk == NULL) {
113 PrintError("Blk device requires a virtio block device\n");
117 blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state) + ((BLK_CAPACITY / 4096) / 8));
119 blk->blk_dev = virtio_blk;
120 blk->capacity = BLK_CAPACITY;
122 blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096);
123 blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr));
124 memset(blk->blk_space, 0, BLK_CAPACITY);
127 struct vm_device * dev = v3_allocate_device("TMP_BLK", &dev_ops, blk);
129 if (v3_attach_device(vm, dev) == -1) {
130 PrintError("Could not attach device %s\n", "TMP_BLK");
135 v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev);
141 device_register("TMP_BLK", blk_init)