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>
23 #include <interfaces/vmm_file.h>
24 #include <palacios/vm_guest.h>
26 #ifndef V3_CONFIG_DEBUG_FILEDISK
28 #define PrintDebug(fmt, args...)
32 uint64_t capacity; // in bytes
39 static int write_all(v3_file_t fd, char * buf, uint64_t offset, uint64_t length) {
40 uint64_t bytes_written = 0;
42 PrintDebug(VM_NONE, VCORE_NONE, "Writing %llu bytes\n", length - bytes_written);
43 while (bytes_written < length) {
44 int tmp_bytes = v3_file_write(fd, buf + bytes_written, length - bytes_written, offset + bytes_written);
45 PrintDebug(VM_NONE, VCORE_NONE, "Wrote %d bytes\n", tmp_bytes);
47 if (tmp_bytes <= 0 ) {
48 PrintError(VM_NONE, VCORE_NONE, "Write failed\n");
52 bytes_written += tmp_bytes;
59 static int read_all(v3_file_t fd, char * buf, uint64_t offset, uint64_t length) {
60 uint64_t bytes_read = 0;
62 PrintDebug(VM_NONE, VCORE_NONE, "Reading %llu bytes\n", length - bytes_read);
63 while (bytes_read < length) {
64 int tmp_bytes = v3_file_read(fd, buf + bytes_read, length - bytes_read, offset + bytes_read);
65 PrintDebug(VM_NONE, VCORE_NONE, "Read %d bytes\n", tmp_bytes);
68 PrintError(VM_NONE, VCORE_NONE, "Read failed\n");
72 bytes_read += tmp_bytes;
78 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
79 struct disk_state * disk = (struct disk_state *)private_data;
81 PrintDebug(VM_NONE, VCORE_NONE, "Reading %llu bytes from %llu to 0x%p\n", num_bytes, lba, buf);
83 if (lba + num_bytes > disk->capacity) {
84 PrintError(VM_NONE, VCORE_NONE, "Out of bounds read: lba=%llu, num_bytes=%llu, capacity=%llu\n",
85 lba, num_bytes, disk->capacity);
89 return read_all(disk->fd, buf, lba, num_bytes);
93 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
94 struct disk_state * disk = (struct disk_state *)private_data;
96 PrintDebug(VM_NONE, VCORE_NONE, "Writing %llu bytes from 0x%p to %llu\n", num_bytes, buf, lba);
98 if (lba + num_bytes > disk->capacity) {
99 PrintError(VM_NONE, VCORE_NONE, "Out of bounds read: lba=%llu, num_bytes=%llu, capacity=%llu\n",
100 lba, num_bytes, disk->capacity);
105 return write_all(disk->fd, buf, lba, num_bytes);
109 static uint64_t get_capacity(void * private_data) {
110 struct disk_state * disk = (struct disk_state *)private_data;
112 PrintDebug(VM_NONE, VCORE_NONE, "Querying FILEDISK capacity %llu\n", disk->capacity);
114 return disk->capacity;
117 static struct v3_dev_blk_ops blk_ops = {
120 .get_capacity = get_capacity,
126 static int disk_free(struct disk_state * disk) {
127 v3_file_close(disk->fd);
133 static struct v3_device_ops dev_ops = {
134 .free = (int (*)(void *))disk_free,
140 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
141 struct disk_state * disk = NULL;
142 char * path = v3_cfg_val(cfg, "path");
143 char * dev_id = v3_cfg_val(cfg, "ID");
144 char * writable = v3_cfg_val(cfg, "writable");
145 char * writeable = v3_cfg_val(cfg, "writeable");
147 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
148 int flags = FILE_OPEN_MODE_READ;
150 if ( ((writable) && (writable[0] == '1')) ||
151 ((writeable) && (writeable[0] == '1')) ) {
152 flags |= FILE_OPEN_MODE_WRITE;
156 PrintError(vm, VCORE_NONE, "Missing path (%s) for %s\n", path, dev_id);
160 disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
163 PrintError(vm, VCORE_NONE, "Could not allocate disk\n");
167 memset(disk, 0, sizeof(struct disk_state));
169 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
172 PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
178 disk->fd = v3_file_open(vm, path, flags);
180 if (disk->fd == NULL) {
181 PrintError(vm, VCORE_NONE, "Could not open file disk:%s\n", path);
182 v3_remove_device(dev);
186 disk->capacity = v3_file_size(disk->fd);
188 V3_Print(vm, VCORE_NONE, "Registering FILEDISK %s (path=%s, fd=%lu, size=%llu, writeable=%d)\n",
189 dev_id, path, (addr_t)disk->fd, disk->capacity,
190 flags & FILE_OPEN_MODE_WRITE);
193 if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"),
194 &blk_ops, frontend_cfg, disk) == -1) {
195 PrintError(vm, VCORE_NONE, "Could not connect %s to frontend %s\n",
196 dev_id, v3_cfg_val(frontend_cfg, "tag"));
197 v3_remove_device(dev);
206 device_register("FILEDISK", disk_init)