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.


05e9bda5df07e66f03208aa1c02c3c58666f5e09
[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     memcpy(buf, (uint8_t *)(disk->disk_image + lba), num_bytes);
41
42     return 0;
43 }
44
45
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;
48
49     PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes,  buf, (uint8_t *)(disk->disk_image + lba));
50
51     memcpy((uint8_t *)(disk->disk_image + lba), buf, num_bytes);
52
53     return 0;
54 }
55
56
57 static uint64_t get_capacity(void * private_data) {
58     struct disk_state * disk = (struct disk_state *)private_data;
59
60     PrintDebug("Querying RAMDISK capacity %d\n", 
61                (uint32_t)(disk->capacity));
62
63     return disk->capacity;
64 }
65
66 static struct v3_dev_blk_ops blk_ops = {
67     .read = read, 
68     .write = write,
69     .get_capacity = get_capacity,
70 };
71
72
73
74
75 static int disk_free(struct disk_state * state) {
76
77     V3_Free(state);
78     return 0;
79 }
80
81 static struct v3_device_ops dev_ops = {
82     .free = (int (*)(void *))disk_free,
83 };
84
85
86
87
88 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
89     struct disk_state * disk = NULL;
90     struct v3_cfg_file * file = NULL;
91     char * dev_id = v3_cfg_val(cfg, "ID");
92     char * filename = v3_cfg_val(cfg, "file");
93
94     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
95
96     if (!filename) {
97         PrintError("Missing filename (%s) for %s\n", filename, dev_id);
98         return -1;
99     }
100
101     file = v3_cfg_get_file(vm, filename);
102
103     if (!file) {
104         PrintError("Invalid ramdisk file: %s\n", filename);
105         return -1;
106     }
107
108
109     disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
110     memset(disk, 0, sizeof(struct disk_state));
111
112     disk->disk_image = file->data;
113     disk->capacity = file->size;
114     PrintDebug("Registering RAMDISK at %p (size=%d)\n", 
115                (void *)file->data, (uint32_t)file->size);
116
117
118     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
119
120     if (dev == NULL) {
121         PrintError("Could not attach device %s\n", dev_id);
122         V3_Free(disk);
123         return -1;
124     }
125
126
127     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
128                            &blk_ops, frontend_cfg, disk) == -1) {
129         PrintError("Could not connect %s to frontend %s\n", 
130                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
131         v3_remove_device(dev);
132         return -1;
133     }
134     
135
136     return 0;
137 }
138
139
140 device_register("RAMDISK", disk_init)