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.


various changes...
[palacios.git] / palacios / src / devices / tmp_blk.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 <devices/lnx_virtio_blk.h>
23
24
25 #define BLK_CAPACITY (500 * 1024 * 1024)
26
27
28
29
30 struct blk_state {
31
32     struct vm_device * blk_dev;
33
34     uint64_t capacity;
35     uint8_t * blk_space;
36     addr_t blk_base_addr;
37 };
38
39
40
41 static uint64_t blk_get_capacity(void * private_data) {
42     struct vm_device * dev = (struct vm_device *)private_data;
43     struct blk_state * blk = (struct blk_state *)(dev->private_data);
44
45     PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
46
47     return blk->capacity / HD_SECTOR_SIZE;
48 }
49
50
51
52 static int blk_read(uint8_t * buf, int sector_count, uint64_t lba,  void * private_data) {
53     struct vm_device * dev = (struct vm_device *)private_data;
54     struct blk_state * blk = (struct blk_state *)(dev->private_data);
55     uint32_t offset = lba * HD_SECTOR_SIZE;
56     uint32_t length = sector_count * HD_SECTOR_SIZE;
57
58     memcpy(buf, blk->blk_space + offset, length);
59
60     return 0;
61 }
62
63
64
65
66 static int blk_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
67     struct vm_device * dev = (struct vm_device *)private_data;
68     struct blk_state * blk = (struct blk_state *)(dev->private_data);
69     uint32_t offset = lba * HD_SECTOR_SIZE;
70     uint32_t length = sector_count * HD_SECTOR_SIZE;
71
72     memcpy(blk->blk_space + offset, buf, length);
73
74     return 0;
75 }
76
77
78 static int blk_free(struct vm_device * dev) {
79     return -1;
80 }
81
82
83 static struct v3_hd_ops hd_ops = {
84     .read = blk_read, 
85     .write = blk_write, 
86     .get_capacity = blk_get_capacity,
87 };
88
89
90
91 static struct v3_device_ops dev_ops = {
92     .free = blk_free,
93     .reset = NULL,
94     .start = NULL,
95     .stop = NULL,
96 };
97
98
99
100
101 static int blk_init(struct guest_info * vm, void * cfg_data) {
102     struct blk_state * blk = NULL;
103     struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data);
104
105     if (!virtio_blk) {
106         PrintError("could not find Virtio backend\n");
107         return -1;
108     }
109
110     PrintDebug("Creating Blk Device\n");
111
112     if (virtio_blk == NULL) {
113         PrintError("Blk device requires a virtio block device\n");
114         return -1;
115     }
116
117     blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state) + ((BLK_CAPACITY / 4096) / 8));
118
119     blk->blk_dev = virtio_blk;
120     blk->capacity = BLK_CAPACITY;
121
122     blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096);
123     blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr));
124     memset(blk->blk_space, 0, BLK_CAPACITY);
125
126
127     struct vm_device * dev = v3_allocate_device("TMP_BLK", &dev_ops, blk);
128
129     if (v3_attach_device(vm, dev) == -1) {
130         PrintError("Could not attach device %s\n", "TMP_BLK");
131         return -1;
132     }
133
134
135     v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev);
136
137
138     return 0;
139 }
140
141 device_register("TMP_BLK", blk_init)