X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fsym_swap.c;h=a3cfd77243d528a0efec3e6596919081d3129570;hb=942df9bb1e2570764d24c74f797247536639502e;hp=706d1c5eb6e8d6d0fd8c5341b4bb6dbb08881812;hpb=2e8b584de26a975655d20d5e94a047a4b286b339;p=palacios.git diff --git a/palacios/src/devices/sym_swap.c b/palacios/src/devices/sym_swap.c index 706d1c5..a3cfd77 100644 --- a/palacios/src/devices/sym_swap.c +++ b/palacios/src/devices/sym_swap.c @@ -18,33 +18,72 @@ */ #include +#include +#include -#include -#include - - +#define SWAP_CAPACITY (4096 * HD_SECTOR_SIZE) struct swap_state { struct vm_device * blk_dev; + uint64_t capacity; + uint8_t * swap_space; + addr_t swap_base_addr; + }; -static int swap_init(struct vm_device * dev) { - return -1; + +static uint64_t swap_get_capacity(void * private_data) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + + return swap->capacity / HD_SECTOR_SIZE; +} + +static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + int offset = lba * HD_SECTOR_SIZE; + int length = sector_count * HD_SECTOR_SIZE; + + PrintDebug("SymSwap: Reading %d bytes\n", length); + + memcpy(buf, swap->swap_space + offset, length); + + return 0; +} + +static int swap_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + int offset = lba * HD_SECTOR_SIZE; + int length = sector_count * HD_SECTOR_SIZE; + + PrintDebug("SymSwap: Writing %d bytes\n", length); + + memcpy(swap->swap_space + offset, buf, length); + + return 0; } -static int swap_deinit(struct vm_device * dev) { +static int swap_free(struct vm_device * dev) { return -1; } +static struct v3_hd_ops hd_ops = { + .read = swap_read, + .write = swap_write, + .get_capacity = swap_get_capacity, +}; + -static struct vm_device_ops dev_ops = { - .init = swap_init, - .deinit = swap_deinit, + +static struct v3_device_ops dev_ops = { + .free = swap_free, .reset = NULL, .start = NULL, .stop = NULL, @@ -52,19 +91,44 @@ static struct vm_device_ops dev_ops = { -struct vm_device * v3_create_swap(struct vm_device * virtio_blk) { + +static int swap_init(struct guest_info * vm, void * cfg_data) { struct swap_state * swap = NULL; + struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data); + + if (!virtio_blk) { + PrintError("could not find Virtio backend\n"); + return -1; + } PrintDebug("Creating Swap Device\n"); if (virtio_blk == NULL) { PrintError("Swap device requires a virtio block device\n"); - return NULL; + return -1; } swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state)); swap->blk_dev = virtio_blk; + swap->capacity = SWAP_CAPACITY; + + swap->swap_base_addr = (addr_t)V3_AllocPages(swap->capacity / 4096); + swap->swap_space = (uint8_t *)V3_VAddr((void *)(swap->swap_base_addr)); + + struct vm_device * dev = v3_allocate_device("SYM_SWAP", &dev_ops, swap); - return v3_create_device("SYM_SWAP", &dev_ops, swap); + if (v3_attach_device(vm, dev) == -1) { + PrintError("Could not attach device %s\n", "SYM_SWAP"); + return -1; + } + + + v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev); + + return 0; } + + + +device_register("SYM_SWAP", swap_init)