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.


Context-based output infrastructure (V3_Print, etc) and modifications to use it
[palacios.git] / palacios / src / devices / disk_model.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
25
26 void __udelay(unsigned long usecs);
27
28
29 struct disk_state {
30     struct v3_dev_blk_ops * ops;
31     uint32_t seek_usecs;
32
33     void * private_data;
34 };
35
36
37 static int model_write(uint8_t * buf,  uint64_t lba, uint64_t num_bytes, void * private_data) {
38     struct disk_state * model = (struct disk_state *)private_data;
39     
40     __udelay(model->seek_usecs);
41
42     return model->ops->write(buf, lba, num_bytes, model->private_data);
43
44 }
45
46 static int model_read(uint8_t * buf,  uint64_t lba, uint64_t num_bytes, void * private_data) {
47     struct disk_state * model = (struct disk_state *)private_data;
48     
49     __udelay(model->seek_usecs);
50
51     return model->ops->read(buf, lba, num_bytes, model->private_data);
52
53 }
54
55 static uint64_t model_get_capacity(void * private_data) {
56     struct disk_state * model = (struct disk_state *)private_data;
57
58     return model->ops->get_capacity(model->private_data);
59 }
60
61 static int model_free(struct disk_state * model) {
62
63     // unhook from frontend
64
65     V3_Free(model);
66     return 0;
67 }
68
69
70
71 static struct v3_dev_blk_ops blk_ops = {
72     .read = model_read, 
73     .write = model_write, 
74     .get_capacity = model_get_capacity,
75 };
76
77
78
79 static struct v3_device_ops dev_ops = {
80     .free = (int (*)(void *))model_free,
81 };
82
83
84 static int connect_fn(struct v3_vm_info * vm, 
85                       void * frontend_data, 
86                       struct v3_dev_blk_ops * ops, 
87                       v3_cfg_tree_t * cfg, 
88                       void * private_data) {
89
90   v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
91   uint32_t seek_time = atoi(v3_cfg_val(cfg, "seek_us"));
92   struct disk_state * model = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
93
94   if (!model) {
95       PrintError(vm, VCORE_NONE, "Cannot allocate\n");
96       return -1;
97   }
98
99   model->ops = ops;
100   model->seek_usecs = seek_time;
101   model->private_data = private_data;
102
103   if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
104                          &blk_ops, frontend_cfg, model) == -1) {
105       PrintError(vm, VCORE_NONE, "Could not connect  to frontend %s\n", 
106                   v3_cfg_val(frontend_cfg, "tag"));
107       return -1;
108   }
109
110   return 0;
111 }
112
113 static int model_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
114
115     char * dev_id = v3_cfg_val(cfg, "ID");
116
117     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, NULL);
118
119     if (dev == NULL) {
120         PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
121         return -1;
122     }
123
124     if (v3_dev_add_blk_frontend(vm, dev_id, connect_fn, NULL) == -1) {
125         PrintError(vm, VCORE_NONE, "Could not register %s as block frontend\n", dev_id);
126         v3_remove_device(dev);
127         return -1;
128     }
129
130
131     return 0;
132 }
133
134
135
136 device_register("DISK_MODEL", model_init)