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.


c8f9639093ccfb42b74c97da2c8af9d21ac1f14b
[palacios.git] / palacios / src / devices / ramdisk.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
24 #ifndef V3_CONFIG_DEBUG_RAMDISK
25 #undef PrintDebug
26 #define PrintDebug(fmt, args...)
27 #endif
28
29 struct disk_state {
30     uint8_t * disk_image;
31     uint32_t capacity; // in bytes
32 };
33
34
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;
37
38     PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
39
40     if (lba + num_bytes > disk->capacity) {
41         PrintError("read out of bounds:  lba=%llu (%p), num_bytes=%llu, capacity=%d (%p)\n", 
42                    lba, (void *)(addr_t)lba, num_bytes, disk->capacity, (void *)(addr_t)disk->capacity);
43         return -1;
44     }
45
46     memcpy(buf, (uint8_t *)(disk->disk_image + lba), num_bytes);
47
48     return 0;
49 }
50
51
52 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
53     struct disk_state * disk = (struct disk_state *)private_data;
54
55     PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes,  buf, (uint8_t *)(disk->disk_image + lba));
56
57     if (lba + num_bytes > disk->capacity) {
58         PrintError("write out of bounds: lba=%llu (%p), num_bytes=%llu, capacity=%d (%p)\n", 
59                    lba, (void *)(addr_t)lba, num_bytes, disk->capacity, (void *)(addr_t)disk->capacity);
60         return -1;
61     }
62
63
64     memcpy((uint8_t *)(disk->disk_image + lba), buf, num_bytes);
65
66     return 0;
67 }
68
69
70 static uint64_t get_capacity(void * private_data) {
71     struct disk_state * disk = (struct disk_state *)private_data;
72
73     PrintDebug("Querying RAMDISK capacity %d\n", 
74                (uint32_t)(disk->capacity));
75
76     return disk->capacity;
77 }
78
79 static struct v3_dev_blk_ops blk_ops = {
80     .read = read, 
81     .write = write,
82     .get_capacity = get_capacity,
83 };
84
85
86
87
88 static int disk_free(struct disk_state * state) {
89
90     V3_Free(state);
91     return 0;
92 }
93
94 static struct v3_device_ops dev_ops = {
95     .free = (int (*)(void *))disk_free,
96 };
97
98
99
100
101 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
102     struct disk_state * disk = NULL;
103     struct v3_cfg_file * file = NULL;
104     char * dev_id = v3_cfg_val(cfg, "ID");
105     char * filename = v3_cfg_val(cfg, "file");
106
107     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
108
109     if (!filename) {
110         PrintError("Missing filename (%s) for %s\n", filename, dev_id);
111         return -1;
112     }
113
114     file = v3_cfg_get_file(vm, filename);
115
116     if (!file) {
117         PrintError("Invalid ramdisk file: %s\n", filename);
118         return -1;
119     }
120
121
122     disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
123
124     if (!disk) {
125         PrintError("Cannot allocate in init\n");
126         return -1;
127     }
128
129     memset(disk, 0, sizeof(struct disk_state));
130
131     disk->disk_image = file->data;
132     disk->capacity = file->size;
133     PrintDebug("Registering RAMDISK at %p (size=%d)\n", 
134                (void *)file->data, (uint32_t)file->size);
135
136
137     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
138
139     if (dev == NULL) {
140         PrintError("Could not attach device %s\n", dev_id);
141         V3_Free(disk);
142         return -1;
143     }
144
145
146     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
147                            &blk_ops, frontend_cfg, disk) == -1) {
148         PrintError("Could not connect %s to frontend %s\n", 
149                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
150         v3_remove_device(dev);
151         return -1;
152     }
153     
154
155     return 0;
156 }
157
158
159 device_register("RAMDISK", disk_init)