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>
24 #ifndef CONFIG_DEBUG_RAMDISK
26 #define PrintDebug(fmt, args...)
31 uint32_t capacity; // in bytes
35 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
36 struct disk_state * disk = (struct disk_state *)private_data;
38 PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
40 memcpy(buf, (uint8_t *)(disk->disk_image + lba), num_bytes);
46 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
47 struct disk_state * disk = (struct disk_state *)private_data;
49 PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes, buf, (uint8_t *)(disk->disk_image + lba));
51 memcpy((uint8_t *)(disk->disk_image + lba), buf, num_bytes);
57 static uint64_t get_capacity(void * private_data) {
58 struct disk_state * disk = (struct disk_state *)private_data;
60 PrintDebug("Querying RAMDISK capacity %d\n",
61 (uint32_t)(disk->capacity));
63 return disk->capacity;
66 static struct v3_dev_blk_ops blk_ops = {
69 .get_capacity = get_capacity,
75 static int disk_free(struct vm_device * dev) {
79 static struct v3_device_ops dev_ops = {
89 static int disk_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
90 struct disk_state * disk = NULL;
91 struct v3_cfg_file * file = NULL;
92 char * name = v3_cfg_val(cfg, "name");
93 char * filename = v3_cfg_val(cfg, "file");
95 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
97 disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
98 memset(disk, 0, sizeof(struct disk_state));
102 PrintError("Missing filename (%s) for %s\n", filename, name);
106 file = v3_cfg_get_file(vm, filename);
109 PrintError("Invalid ramdisk file: %s\n", filename);
113 disk->disk_image = file->data;
114 disk->capacity = file->size;
115 PrintDebug("Registering RAMDISK at %p (size=%d)\n",
116 (void *)file->data, (uint32_t)file->size);
118 struct vm_device * dev = v3_allocate_device(name, &dev_ops, disk);
120 if (v3_attach_device(vm, dev) == -1) {
121 PrintError("Could not attach device %s\n", name);
125 if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"),
126 &blk_ops, frontend_cfg, disk) == -1) {
127 PrintError("Could not connect %s to frontend %s\n",
128 name, v3_cfg_val(frontend_cfg, "tag"));
137 device_register("RAMDISK", disk_init)