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.


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