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 / ram_hd.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 <devices/ram_hd.h>
22 #include <devices/ide.h>
23
24
25
26 #ifndef CONFIG_DEBUG_IDE
27 #undef PrintDebug
28 #define PrintDebug(fmt, args...)
29 #endif
30
31 struct hd_state {
32     addr_t disk_image;
33     uint32_t capacity; // in bytes
34
35     struct vm_device * ide;
36
37     uint_t bus;
38     uint_t drive;
39 };
40
41
42 // HDs always read 512 byte blocks... ?
43 static int hd_read(uint8_t * buf, int sector_count, uint64_t lba,  void * private_data) {
44     struct vm_device * hd_dev = (struct vm_device *)private_data;
45     struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
46     int offset = lba * HD_SECTOR_SIZE;
47     int length = sector_count * HD_SECTOR_SIZE;
48
49     //    PrintDebug("Reading RAM HD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length);
50
51     memcpy(buf, (uint8_t *)(hd->disk_image + offset), length);
52
53     return 0;
54 }
55
56
57 static int hd_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
58     struct vm_device * hd_dev = (struct vm_device *)private_data;
59     struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
60     int offset = lba * HD_SECTOR_SIZE;
61     int length = sector_count * HD_SECTOR_SIZE;
62
63     memcpy((uint8_t *)(hd->disk_image + offset), buf, length);
64
65     return 0;
66 }
67
68
69 static uint64_t hd_get_capacity(void * private_data) {
70     struct vm_device * hd_dev = (struct vm_device *)private_data;
71     struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
72     PrintDebug("Querying RAM HD capacity (bytes=%d) (ret = %d)\n", 
73                hd->capacity, hd->capacity  / HD_SECTOR_SIZE);
74     return hd->capacity / HD_SECTOR_SIZE;
75 }
76
77 static struct v3_hd_ops hd_ops = {
78     .read = hd_read, 
79     .write = hd_write,
80     .get_capacity = hd_get_capacity,
81 };
82
83
84
85
86 static int hd_free(struct vm_device * dev) {
87     return 0;
88 }
89
90 static struct v3_device_ops dev_ops = {
91     .free = hd_free,
92     .reset = NULL,
93     .start = NULL,
94     .stop = NULL,
95 };
96
97
98
99
100 static int hd_init(struct guest_info * vm, void * cfg_data) {
101     struct hd_state * hd = NULL;
102     struct ram_hd_cfg * cfg = (struct ram_hd_cfg *)cfg_data;
103
104     if (cfg->size % HD_SECTOR_SIZE) {
105         PrintError("HD image must be an integral of sector size (HD_SECTOR_SIZE=%d)\n", HD_SECTOR_SIZE);
106         return -1;
107     }
108
109     hd = (struct hd_state *)V3_Malloc(sizeof(struct hd_state));
110
111     PrintDebug("Registering Ram HDD at %p (size=%d)\n", (void *)cfg->ramdisk, cfg->size);
112
113     hd->disk_image = cfg->ramdisk;
114     hd->capacity = cfg->size;
115
116     hd->ide = v3_find_dev(vm, cfg->ide);
117
118     if (hd->ide == 0) {
119         PrintError("Could not find backend %s\n", cfg->ide);
120         return -1;
121     }
122
123     hd->bus = cfg->bus;
124     hd->drive = cfg->drive;
125         
126     struct vm_device * dev = v3_allocate_device("RAM-HD", &dev_ops, hd);
127
128     if (v3_attach_device(vm, dev) == -1) {
129         PrintError("Could not attach device %s\n", "RAM-HD");
130         return -1;
131     }
132
133
134     if (v3_ide_register_harddisk(hd->ide, hd->bus, hd->drive, "RAM-HD", &hd_ops, dev) == -1) {
135         return -1;
136     }
137     
138     return 0;
139 }
140
141
142 device_register("RAM-HD", hd_init)