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 <devices/lnx_virtio_blk.h>
23 #include <palacios/vmm_sym_swap.h>
25 #define SWAP_CAPACITY (150 * 1024 * 1024)
29 /* This is the first page that linux writes to the swap area */
30 /* Taken from Linux */
33 char reserved[PAGE_SIZE - 10];
34 char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
37 char bootbits[1024]; /* Space for disklabel etc. */
41 unsigned char sws_uuid[16];
42 unsigned char sws_volume[16];
43 uint32_t type; // The index into the swap_map
44 uint32_t padding[116];
45 // uint32_t padding[117];
55 struct vm_device * blk_dev;
58 uint_t unswapped_pages;
63 addr_t swap_base_addr;
65 uint8_t usage_map[0]; // This must be the last structure member
71 static inline void set_index_usage(struct swap_state * swap, uint32_t index, int used) {
72 int major = index / 8;
73 int minor = index % 8;
76 swap->usage_map[major] |= (1 << minor);
78 swap->usage_map[major] &= ~(1 << minor);
82 static inline int get_index_usage(struct swap_state * swap, uint32_t index) {
83 int major = index / 8;
84 int minor = index % 8;
85 return swap->usage_map[major] & (1 << minor);
89 static inline uint32_t get_swap_index_from_offset(uint32_t offset) {
90 // CAREFUL: The index might be offset by 1, because the first 4K is the header
91 return (offset / 4096);
96 static inline uint32_t get_swap_index(uint32_t offset) {
97 // CAREFUL: The index might be offset by 1, because the first 4K is the header
98 return (swap_addr - swap->swap_space) / 4096;
103 static inline void * get_swap_entry(uint32_t pg_index, void * private_data) {
104 struct swap_state * swap = (struct swap_state *)private_data;
105 void * pg_addr = NULL;
107 if (get_index_usage(swap, pg_index)) {
108 // CAREFUL: The index might be offset by 1, because the first 4K is the header
109 pg_addr = (void *)(swap->swap_space + (pg_index * 4096));
117 static uint64_t swap_get_capacity(void * private_data) {
118 struct vm_device * dev = (struct vm_device *)private_data;
119 struct swap_state * swap = (struct swap_state *)(dev->private_data);
121 PrintDebug("SymSwap: Getting Capacity %d\n", (uint32_t)(swap->capacity));
123 return swap->capacity / HD_SECTOR_SIZE;
127 static struct v3_swap_ops swap_ops = {
128 .get_swap_entry = get_swap_entry,
133 static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
134 struct vm_device * dev = (struct vm_device *)private_data;
135 struct swap_state * swap = (struct swap_state *)(dev->private_data);
136 uint32_t offset = lba * HD_SECTOR_SIZE;
137 uint32_t length = sector_count * HD_SECTOR_SIZE;
140 PrintDebug("SymSwap: Reading %d bytes to %p from %p\n", length,
141 buf, (void *)(swap->swap_space + offset));
144 PrintError("Swapping in length that is not a page multiple\n");
147 memcpy(buf, swap->swap_space + offset, length);
149 swap->unswapped_pages += (length / 4096);
151 set_index_usage(swap, get_swap_index_from_offset(offset), 0);
154 // Notify the shadow paging layer
156 PrintDebug("Swapped in %d pages\n", length / 4096);
164 static int swap_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
165 struct vm_device * dev = (struct vm_device *)private_data;
166 struct swap_state * swap = (struct swap_state *)(dev->private_data);
167 uint32_t offset = lba * HD_SECTOR_SIZE;
168 uint32_t length = sector_count * HD_SECTOR_SIZE;
171 PrintDebug("SymSwap: Writing %d bytes to %p from %p\n", length,
172 (void *)(swap->swap_space + offset), buf);
176 PrintError("Swapping out length that is not a page multiple\n");
179 if ((swap->active == 0) && (offset == 0)) {
180 // This is the swap header page
181 union swap_header * hdr = (union swap_header *)buf;
183 if (length != 4096) {
184 PrintError("Initializing Swap space by not writing page multiples. This sucks...\n");
190 PrintDebug("Swap Type=%d (magic=%s)\n", hdr->info.type, hdr->magic.magic);
192 if (v3_register_swap_disk(dev->vm, hdr->info.type, &swap_ops, dev) == -1) {
193 PrintError("Error registering symbiotic swap disk\n");
199 memcpy(swap->swap_space + offset, buf, length);
201 swap->swapped_pages += (length / 4096);
203 set_index_usage(swap, get_swap_index_from_offset(offset), 1);
205 PrintDebug("Swapped out %d pages\n", length / 4096);
211 static int swap_free(struct vm_device * dev) {
216 static struct v3_hd_ops hd_ops = {
219 .get_capacity = swap_get_capacity,
224 static struct v3_device_ops dev_ops = {
236 static int swap_init(struct guest_info * vm, void * cfg_data) {
237 struct swap_state * swap = NULL;
238 struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data);
241 PrintError("could not find Virtio backend\n");
245 PrintDebug("Creating Swap Device\n");
247 if (virtio_blk == NULL) {
248 PrintError("Swap device requires a virtio block device\n");
252 swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state) + ((SWAP_CAPACITY / 4096) / 8));
254 swap->blk_dev = virtio_blk;
255 swap->capacity = SWAP_CAPACITY;
257 swap->swapped_pages = 0;
258 swap->unswapped_pages = 0;
262 swap->swap_base_addr = (addr_t)V3_AllocPages(swap->capacity / 4096);
263 swap->swap_space = (uint8_t *)V3_VAddr((void *)(swap->swap_base_addr));
264 memset(swap->swap_space, 0, SWAP_CAPACITY);
266 memset(swap->usage_map, 0, ((SWAP_CAPACITY / 4096) / 8));
268 struct vm_device * dev = v3_allocate_device("SYM_SWAP", &dev_ops, swap);
270 if (v3_attach_device(vm, dev) == -1) {
271 PrintError("Could not attach device %s\n", "SYM_SWAP");
276 v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev);
283 device_register("SYM_SWAP", swap_init)