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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vm_guest.h>
26 void __udelay(unsigned long usecs);
30 struct v3_dev_blk_ops * ops;
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;
40 __udelay(model->seek_usecs);
42 return model->ops->write(buf, lba, num_bytes, model->private_data);
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;
49 __udelay(model->seek_usecs);
51 return model->ops->read(buf, lba, num_bytes, model->private_data);
55 static uint64_t model_get_capacity(void * private_data) {
56 struct disk_state * model = (struct disk_state *)private_data;
58 return model->ops->get_capacity(model->private_data);
61 static int model_free(struct disk_state * model) {
63 // unhook from frontend
71 static struct v3_dev_blk_ops blk_ops = {
74 .get_capacity = model_get_capacity,
79 static struct v3_device_ops dev_ops = {
80 .free = (int (*)(void *))model_free,
84 static int connect_fn(struct v3_vm_info * vm,
86 struct v3_dev_blk_ops * ops,
88 void * private_data) {
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));
95 PrintError(vm, VCORE_NONE, "Cannot allocate\n");
100 model->seek_usecs = seek_time;
101 model->private_data = private_data;
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"));
113 static int model_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
115 char * dev_id = v3_cfg_val(cfg, "ID");
117 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, NULL);
120 PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
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);
136 device_register("DISK_MODEL", model_init)