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>
24 #define SWAP_CAPACITY (150 * 1024 * 1024)
28 struct vm_device * blk_dev;
31 uint_t unswapped_pages;
35 addr_t swap_base_addr;
40 static uint64_t swap_get_capacity(void * private_data) {
41 struct vm_device * dev = (struct vm_device *)private_data;
42 struct swap_state * swap = (struct swap_state *)(dev->private_data);
44 PrintDebug("SymSwap: Getting Capacity %d\n", (uint32_t)(swap->capacity));
46 return swap->capacity / HD_SECTOR_SIZE;
49 static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
50 struct vm_device * dev = (struct vm_device *)private_data;
51 struct swap_state * swap = (struct swap_state *)(dev->private_data);
52 int offset = lba * HD_SECTOR_SIZE;
53 int length = sector_count * HD_SECTOR_SIZE;
56 PrintDebug("SymSwap: Reading %d bytes to %p from %p\n", length,
57 buf, (void *)(swap->swap_space + offset));
60 PrintError("Swapping in length that is not a page multiple\n");
63 memcpy(buf, swap->swap_space + offset, length);
65 swap->unswapped_pages += (length / 4096);
67 PrintDebug("Swapped in %d pages\n", length / 4096);
72 static int swap_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
73 struct vm_device * dev = (struct vm_device *)private_data;
74 struct swap_state * swap = (struct swap_state *)(dev->private_data);
75 int offset = lba * HD_SECTOR_SIZE;
76 int length = sector_count * HD_SECTOR_SIZE;
78 PrintDebug("SymSwap: Writing %d bytes to %p from %p\n", length,
79 (void *)(swap->swap_space + offset), buf);
82 PrintError("Swapping out length that is not a page multiple\n");
85 memcpy(swap->swap_space + offset, buf, length);
87 swap->swapped_pages += (length / 4096);
89 PrintDebug("Swapped out %d pages\n", length / 4096);
95 static int swap_free(struct vm_device * dev) {
100 static struct v3_hd_ops hd_ops = {
103 .get_capacity = swap_get_capacity,
108 static struct v3_device_ops dev_ops = {
118 static int swap_init(struct guest_info * vm, void * cfg_data) {
119 struct swap_state * swap = NULL;
120 struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data);
123 PrintError("could not find Virtio backend\n");
127 PrintDebug("Creating Swap Device\n");
129 if (virtio_blk == NULL) {
130 PrintError("Swap device requires a virtio block device\n");
134 swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state));
136 swap->blk_dev = virtio_blk;
137 swap->capacity = SWAP_CAPACITY;
139 swap->swapped_pages = 0;
140 swap->unswapped_pages = 0;
142 swap->swap_base_addr = (addr_t)V3_AllocPages(swap->capacity / 4096);
143 swap->swap_space = (uint8_t *)V3_VAddr((void *)(swap->swap_base_addr));
144 memset(swap->swap_space, 0, SWAP_CAPACITY);
147 struct vm_device * dev = v3_allocate_device("SYM_SWAP", &dev_ops, swap);
149 if (v3_attach_device(vm, dev) == -1) {
150 PrintError("Could not attach device %s\n", "SYM_SWAP");
155 v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev);
162 device_register("SYM_SWAP", swap_init)