X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fsym_swap.c;h=4b7270d1b866d2a981c24209c0ecac3989d56bbf;hb=1786dcbd1b5a05d2591556db17bff62465713ee1;hp=f2057f40b40ca757aeda9540b9c338ad7a7bd638;hpb=1a24d4e8fb3918a42b124777db5b41940150023e;p=palacios.git diff --git a/palacios/src/devices/sym_swap.c b/palacios/src/devices/sym_swap.c index f2057f4..4b7270d 100644 --- a/palacios/src/devices/sym_swap.c +++ b/palacios/src/devices/sym_swap.c @@ -20,9 +20,14 @@ #include #include #include +#include #define SWAP_CAPACITY (150 * 1024 * 1024) +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY +#include +#endif + /* This is the first page that linux writes to the swap area */ /* Taken from Linux */ @@ -45,20 +50,84 @@ union swap_header { } info; }; + + struct swap_state { - + int active; + struct vm_device * blk_dev; uint_t swapped_pages; uint_t unswapped_pages; + + union swap_header * hdr; + +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + uint32_t pages_in; + uint32_t pages_out; +#endif + + uint64_t capacity; uint8_t * swap_space; addr_t swap_base_addr; + + uint8_t usage_map[0]; // This must be the last structure member }; + +static inline void set_index_usage(struct swap_state * swap, uint32_t index, int used) { + int major = index / 8; + int minor = index % 8; + + if (used) { + swap->usage_map[major] |= (1 << minor); + } else { + swap->usage_map[major] &= ~(1 << minor); + } +} + +static inline int get_index_usage(struct swap_state * swap, uint32_t index) { + int major = index / 8; + int minor = index % 8; + + return (swap->usage_map[major] & (1 << minor)); +} + + +static inline uint32_t get_swap_index_from_offset(uint32_t offset) { + // CAREFUL: The index might be offset by 1, because the first 4K is the header + return (offset / 4096); +} + + +/* + static inline uint32_t get_swap_index(uint32_t offset) { + // CAREFUL: The index might be offset by 1, because the first 4K is the header + return (swap_addr - swap->swap_space) / 4096; + } +*/ + + +static inline void * get_swap_entry(uint32_t pg_index, void * private_data) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + void * pg_addr = NULL; + int ret = 0; + + if ((ret = get_index_usage(swap, pg_index))) { + // CAREFUL: The index might be offset by 1, because the first 4K is the header + pg_addr = (void *)(swap->swap_space + (pg_index * 4096)); + } + + return pg_addr; +} + + + 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); @@ -68,16 +137,25 @@ static uint64_t swap_get_capacity(void * private_data) { return swap->capacity / HD_SECTOR_SIZE; } + +static struct v3_swap_ops swap_ops = { + .get_swap_entry = get_swap_entry, +}; + + + 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; + uint32_t offset = lba * HD_SECTOR_SIZE; + uint32_t length = sector_count * HD_SECTOR_SIZE; + + + /* + PrintDebug("SymSwap: Reading %d bytes to %p from %p\n", length, + buf, (void *)(swap->swap_space + offset)); + */ - - PrintDebug("SymSwap: Reading %d bytes to %p from %p\n", length, - buf, (void *)(swap->swap_space + offset)); - if (length % 4096) { PrintError("Swapping in length that is not a page multiple\n"); } @@ -86,43 +164,74 @@ static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * priv swap->unswapped_pages += (length / 4096); - PrintDebug("Swapped in %d pages\n", length / 4096); + if ((swap->active == 1) && (offset != 0)) { + int i = 0; + // Notify the shadow paging layer + +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + swap->pages_in += length / 4096; +#endif + + for (i = 0; i < length; i += 4096) { + set_index_usage(swap, get_swap_index_from_offset(offset + i), 0); + v3_swap_in_notify(dev->vm, get_swap_index_from_offset(offset + i), swap->hdr->info.type); + } + } 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; + uint32_t offset = lba * HD_SECTOR_SIZE; + uint32_t length = sector_count * HD_SECTOR_SIZE; + /* PrintDebug("SymSwap: Writing %d bytes to %p from %p\n", length, (void *)(swap->swap_space + offset), buf); */ + if (length % 4096) { PrintError("Swapping out length that is not a page multiple\n"); } - if (offset == 0) { - // This is the swap header page - union swap_header * hdr; + if ((swap->active == 0) && (offset == 0)) { + // This is the swap header page + if (length != 4096) { PrintError("Initializing Swap space by not writing page multiples. This sucks...\n"); return -1; } - hdr = (union swap_header *)buf; - + swap->active = 1; - PrintDebug("Swap Type=%d (magic=%s)\n", hdr->info.type, hdr->magic.magic); + PrintDebug("Swap Type=%d (magic=%s)\n", swap->hdr->info.type, swap->hdr->magic.magic); + + if (v3_register_swap_disk(dev->vm, swap->hdr->info.type, &swap_ops, dev) == -1) { + PrintError("Error registering symbiotic swap disk\n"); + return -1; + } } memcpy(swap->swap_space + offset, buf, length); swap->swapped_pages += (length / 4096); - PrintDebug("Swapped out %d pages\n", length / 4096); + if ((swap->active == 1) && (offset != 0)) { + int i = 0; + +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + swap->pages_out += length / 4096; +#endif + + for (i = 0; i < length; i += 4096) { + set_index_usage(swap, get_swap_index_from_offset(offset + i), 1); + } + } return 0; } @@ -149,6 +258,20 @@ static struct v3_device_ops dev_ops = { }; +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY +static void telemetry_cb(struct guest_info * info, void * private_data, char * hdr) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + + V3_Print("%sSwap Device:\n", hdr); + V3_Print("%s\tPages Swapped in=%d\n", hdr, swap->pages_in); + V3_Print("%s\tPages Swapped out=%d\n", hdr, swap->pages_out); + +} +#endif + + + static int swap_init(struct guest_info * vm, void * cfg_data) { @@ -167,7 +290,7 @@ static int swap_init(struct guest_info * vm, void * cfg_data) { return -1; } - swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state)); + swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state) + ((SWAP_CAPACITY / 4096) / 8)); swap->blk_dev = virtio_blk; swap->capacity = SWAP_CAPACITY; @@ -175,10 +298,15 @@ static int swap_init(struct guest_info * vm, void * cfg_data) { swap->swapped_pages = 0; swap->unswapped_pages = 0; + swap->active = 0; + swap->hdr = (union swap_header *)swap; + + swap->swap_base_addr = (addr_t)V3_AllocPages(swap->capacity / 4096); swap->swap_space = (uint8_t *)V3_VAddr((void *)(swap->swap_base_addr)); memset(swap->swap_space, 0, SWAP_CAPACITY); + memset(swap->usage_map, 0, ((SWAP_CAPACITY / 4096) / 8)); struct vm_device * dev = v3_allocate_device("SYM_SWAP", &dev_ops, swap); @@ -190,9 +318,13 @@ static int swap_init(struct guest_info * vm, void * cfg_data) { v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev); +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + if (vm->enable_telemetry) { + v3_add_telemetry_cb(vm, telemetry_cb, dev); + } +#endif + return 0; } - - device_register("SYM_SWAP", swap_init)