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.


Cleanup and sanity-checking of unintentional integer overflow, unsigned/zero comparis...
[palacios.git] / palacios / src / devices / tmpdisk.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 #include <palacios/vm_guest.h>
23
24 struct blk_state {
25     uint64_t capacity;
26     addr_t blk_base_addr;
27     uint8_t * blk_space;
28 };
29
30
31
32 static uint64_t blk_get_capacity(void * private_data) {
33     struct blk_state * blk = (struct blk_state *)private_data;
34
35     //  PrintDebug(info->vm_info, info, "SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
36
37     return blk->capacity;
38 }
39
40
41
42 static int blk_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
43     struct blk_state * blk = (struct blk_state *)private_data;
44
45     //    PrintDebug(info->vm_info, info, "TmpDisk Reading %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
46
47     if (lba + num_bytes > blk->capacity) {
48         PrintError(VM_NONE, VCORE_NONE, "TMPDISK Read past end of disk\n");
49         return -1;
50     }
51
52     memcpy(buf, blk->blk_space + lba, num_bytes);
53
54     return 0;
55 }
56
57
58
59
60 static int blk_write(uint8_t * buf,  uint64_t lba, uint64_t num_bytes, void * private_data) {
61     struct blk_state * blk = (struct blk_state *)private_data;
62
63     //    PrintDebug(VM_NONE, VCORE_NONE, "TmpDisk Writing %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
64
65     if (lba + num_bytes > blk->capacity) {
66         PrintError(VM_NONE, VCORE_NONE, "TMPDISK Write past end of disk\n");
67         return -1;
68     }
69
70     memcpy(blk->blk_space + lba, buf, num_bytes);
71
72     return 0;
73 }
74
75
76 static int blk_free(struct blk_state * blk) {
77     V3_FreePages((void *)blk->blk_base_addr, blk->capacity / 4096);
78
79     V3_Free(blk);
80     return 0;
81 }
82
83
84 static struct v3_dev_blk_ops blk_ops = {
85     .read = blk_read, 
86     .write = blk_write, 
87     .get_capacity = blk_get_capacity,
88 };
89
90
91
92 static struct v3_device_ops dev_ops = {
93     .free = (int (*)(void *))blk_free,
94
95 };
96
97
98
99
100 static int blk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
101     struct blk_state * blk = NULL;
102     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
103     char * dev_id = v3_cfg_val(cfg, "ID");
104     uint64_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024ULL * 1024ULL;
105     
106     if (!frontend_cfg) {
107         PrintError(vm, VCORE_NONE, "Frontend Configuration not present\n");
108         return -1;
109     }
110
111     PrintDebug(vm, VCORE_NONE, "Intializing TMPDISK (capacity=%d)\n", (uint32_t)capacity);
112
113
114     blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state));
115
116     if (!blk) {
117         PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
118         return -1;
119     }
120
121     blk->capacity = capacity;
122     
123     blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096);
124
125     if (!blk->blk_base_addr) { 
126         PrintError(vm, VCORE_NONE, "Cannot allocate block space\n");
127         V3_Free(blk);
128         return -1;
129     }
130
131     blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr));
132     memset(blk->blk_space, 0, capacity);
133
134
135     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, blk);
136
137     if (dev == NULL) {
138         PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
139         V3_Free(blk);
140         return -1;
141     }
142
143     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
144                            &blk_ops, frontend_cfg, blk) == -1) {
145         PrintError(vm, VCORE_NONE, "Could not connect %s to frontend %s\n", 
146                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
147         v3_remove_device(dev);
148         return -1;
149     }
150
151
152     return 0;
153 }
154
155 device_register("TMPDISK", blk_init)