Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


actually add the filedisk implementation
[palacios.git] / palacios / src / devices / filedisk.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22
23 #include <palacios/vmm_file.h>
24
25 #ifndef CONFIG_DEBUG_FILEDISK
26 #undef PrintDebug
27 #define PrintDebug(fmt, args...)
28 #endif
29
30 struct disk_state {
31     uint64_t capacity; // in bytes
32
33     int fd;
34 };
35
36
37 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
38     struct disk_state * disk = (struct disk_state *)private_data;
39
40     PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
41
42     V3_FileRead(disk->fd, lba, buf, num_bytes);
43
44     return 0;
45 }
46
47
48 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
49     struct disk_state * disk = (struct disk_state *)private_data;
50
51     PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes,  buf, (uint8_t *)(disk->disk_image + lba));
52
53     V3_FileWrite(disk->fd, lba, buf, num_bytes);
54
55     return 0;
56 }
57
58
59 static uint64_t get_capacity(void * private_data) {
60     struct disk_state * disk = (struct disk_state *)private_data;
61
62     PrintDebug("Querying RAMDISK capacity %d\n", 
63                (uint32_t)(disk->capacity));
64
65     return disk->capacity;
66 }
67
68 static struct v3_dev_blk_ops blk_ops = {
69     .read = read, 
70     .write = write,
71     .get_capacity = get_capacity,
72 };
73
74
75
76
77 static int disk_free(struct vm_device * dev) {
78     return 0;
79 }
80
81 static struct v3_device_ops dev_ops = {
82     .free = disk_free,
83     .reset = NULL,
84     .start = NULL,
85     .stop = NULL,
86 };
87
88
89
90
91 static int disk_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
92     struct disk_state * disk = NULL;
93     struct v3_cfg_file * file = NULL;
94     char * name = v3_cfg_val(cfg, "filename");
95     char * filename = v3_cfg_val(cfg, "file");
96
97     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
98
99     disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
100     memset(disk, 0, sizeof(struct disk_state));
101
102
103     if (!filename) {
104         PrintError("Missing filename (%s) for %s\n", filename, name);
105         return -1;
106     }
107     
108     disk->fd = V3_FileOpen(filename, 0);
109     disk->capacity = V3_FileSize(disk->fd);
110
111     PrintDebug("Registering RAMDISK at %p (size=%d)\n", 
112                (void *)file->data, (uint32_t)file->size);
113
114     struct vm_device * dev = v3_allocate_device(name, &dev_ops, disk);
115
116     if (v3_attach_device(vm, dev) == -1) {
117         PrintError("Could not attach device %s\n", name);
118         return -1;
119     }
120
121     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
122                            &blk_ops, frontend_cfg, disk) == -1) {
123         PrintError("Could not connect %s to frontend %s\n", 
124                    name, v3_cfg_val(frontend_cfg, "tag"));
125         return -1;
126     }
127     
128
129     return 0;
130 }
131
132
133 device_register("FILEDISK", disk_init)