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.


Added intial cut at IDE disk support
[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 struct hd_state {
26     addr_t disk_image;
27     uint32_t capacity; // in bytes
28
29     struct vm_device * ide;
30
31     uint_t bus;
32     uint_t drive;
33 };
34
35
36 // HDs always read 2048 byte blocks... ?
37 static int hd_read(uint8_t * buf, int count, int lba,  void * private_data) {
38     struct vm_device * hd_dev = (struct vm_device *)private_data;
39     struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
40     int offset = lba * IDE_SECTOR_SIZE;
41     int length = ((offset + count) > hd->capacity) ? (hd->capacity - offset) : count;
42
43     PrintDebug("Reading RAM HD at (LBA=%d) offset %d (length=%d)\n", lba, offset, length);
44
45     memcpy(buf, (uint8_t *)(hd->disk_image + offset), length);
46
47     // Pad out the rest of the buffer with 0's
48     //    memset(buf + length, 0, IDE_SECTOR_SIZE - length);
49
50     return 0;
51 }
52
53
54 static uint32_t hd_get_capacity(void * private_data) {
55     struct vm_device * hd_dev = (struct vm_device *)private_data;
56     struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
57     PrintDebug("Querying RAM HD capacity (bytes=%d) (ret = %d)\n", 
58                hd->capacity, (hd->capacity + IDE_SECTOR_SIZE - 1) / IDE_SECTOR_SIZE);
59     return (hd->capacity + IDE_SECTOR_SIZE - 1) / IDE_SECTOR_SIZE;
60 }
61
62 static struct v3_ide_hd_ops hd_ops = {
63     .read = hd_read, 
64     .get_capacity = hd_get_capacity,
65 };
66
67
68 static int hd_init(struct vm_device * dev) {
69     struct hd_state * hd = (struct hd_state *)(dev->private_data);
70
71     if (v3_ide_register_harddisk(hd->ide, hd->bus, hd->drive, "V3-RAM-HD", &hd_ops, dev) == -1) {
72         return -1;
73     }
74     
75     return 0;
76 }
77
78
79 static int hd_deinit(struct vm_device * dev) {
80     return 0;
81 }
82
83 static struct vm_device_ops dev_ops = {
84     .init = hd_init, 
85     .deinit = hd_deinit,
86     .reset = NULL,
87     .start = NULL,
88     .stop = NULL,
89 };
90
91 struct vm_device * v3_create_ram_hd(struct vm_device * ide, 
92                                     uint_t bus, uint_t drive, 
93                                     addr_t ramdisk, uint32_t size) {
94     struct hd_state * hd = (struct hd_state *)V3_Malloc(sizeof(struct hd_state));
95
96     PrintDebug("Registering Ram HDD at %p (size=%d)\n", (void *)ramdisk, size);
97
98     hd->disk_image = ramdisk;
99     hd->capacity = size;
100
101     hd->ide = ide;
102     hd->bus = bus;
103     hd->drive = drive;
104         
105     struct vm_device * hd_dev = v3_create_device("RAM-HD", &dev_ops, hd);
106
107     return hd_dev;
108 }