void swap_in(uint32_t index, void *frame) { bitmap_flip(swap_bitmap, index); int i; for(i=0;i<SECTOR_PER_PAGE;i++) disk_read(swap_disk, (index * SECTOR_PER_PAGE) + i, frame + (i * DISK_SECTOR_SIZE)); }
/* Swap a page back to frame. */ void swap_back_to_frame (int slot_index, uint8_t *kpage) { if (!lock_held_by_current_thread (&swap_table_lock)) lock_acquire (&swap_table_lock); ASSERT (bitmap_test (swap_table, slot_index) == true); bitmap_flip (swap_table, slot_index); int i = 0; for (i = 0; i < 8; i++) block_read (swap_block, slot_index * 8 + i, kpage + i * BLOCK_SECTOR_SIZE); lock_release (&swap_table_lock); }
//Loads from the swap to main memory void swap_in (size_t used_index, void* frame){ if (!swap_block || !swap_map) return; //Returns if there is no swap block or frame lock_acquire(&swap_lock); //Acquires the lock status for swap if (bitmap_test(swap_map, used_index) == SWAP_FREE) PANIC ("Trying to swap in a free block! Kernel panicking."); //Goes into panic mode if the swap block is free and swap in is in process bitmap_flip(swap_map, used_index); //Flips the bitmap size_t i; for (i = 0; i < SECTORS_PER_PAGE; i++){ //Block reader block_read(swap_block, used_index * SECTORS_PER_PAGE + i, (uint8_t *) frame + i * BLOCK_SECTOR_SIZE); } lock_release(&swap_lock); //Releases the swap lock }
/* move data from swap slot to frame */ void vm_swap_in(size_t idx, void *page){ lock_acquire(&swap_lock); if (!bitmap_test(swap_partition, idx)){ /* Swap slot is already free */ lock_release(&swap_lock); PANIC("Swap slot is already free\n"); } bitmap_flip(swap_partition, idx); size_t i; for (i = 0; i < SECTORS_PER_PAGE; ++i){ block_read(swap_slot, (idx * SECTORS_PER_PAGE) + i, page + (BLOCK_SECTOR_SIZE * i)); } lock_release(&swap_lock); }
void swap_in(size_t used_index,void *kaddr) { if(!swap_block||!swap_map)return ; lock_acquire(&swap_lock); if(bitmap_test(swap_map,used_index) == SWAP_FREE); bitmap_flip(swap_map,used_index); size_t i; for(i = 0;i< SECTORS_PER_PAGE;i++) { block_read(swap_block,used_index*SECTORS_PER_PAGE +i, (uint8_t *)kaddr + i*BLOCK_SECTOR_SIZE); } lock_release(&swap_lock); }
size_t swap_out(void *kaddr) { lock_acquire(&swap_lock); //size_t free_index = bitmap_scan_and_flip(swap_map,0,1,SWAP_FREE); size_t free_index = bitmap_scan(swap_map,0,1,SWAP_FREE); bitmap_flip(swap_map,free_index); size_t i; for(i = 0;i <SECTORS_PER_PAGE;i++) { block_write(swap_block,free_index*SECTORS_PER_PAGE +i, (uint8_t*)kaddr + i*BLOCK_SECTOR_SIZE); } lock_release(&swap_lock); return free_index; }
void free_slot (void *page, size_t index) { /* Mark the slot as free again */ ASSERT (bitmap_test (swap_slot_map, index)); bitmap_flip (swap_slot_map, index); /* This is almost identical to the loop in pick_slot_and_swap, we're just going the other way */ int i = 0; for (; i < SECTORS_PER_PAGE; ++i) { block_read (block_device, (index * SECTORS_PER_PAGE) + i, page + (BLOCK_SECTOR_SIZE * i)); } }
/* Swaps in page P, which must have a locked frame (and be swapped out). */ void swap_in(void *f, size_t index) { if (!swap_device || !swap_bitmap) { PANIC("No swap partition available!"); } lock_acquire(&swap_lock); if (bitmap_test(swap_bitmap, index) == SWAP_FREE) { lock_release(&swap_lock); return; } bitmap_flip(swap_bitmap, index); lock_release(&swap_lock); size_t i; for (i = 0; i < SECTORS_PER_PAGE; i++) { block_read(swap_device, index * SECTORS_PER_PAGE + i, (uint8_t *) f + i * BLOCK_SECTOR_SIZE); } }
void swap_in (size_t used_index, void* frame) { if (!swap_block || !swap_map) { return; } lock_acquire(&swap_lock); if (bitmap_test(swap_map, used_index) == SWAP_FREE) { PANIC ("Trying to swap in a free block! Kernel panicking."); } bitmap_flip(swap_map, used_index); size_t i; for (i = 0; i < SECTORS_PER_PAGE; i++) { block_read(swap_block, used_index * SECTORS_PER_PAGE + i, (uint8_t *) frame + i * BLOCK_SECTOR_SIZE); } lock_release(&swap_lock); }