From 8eb0243fe65c82ce5c680766a1ffef12e7807aaf Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Thu, 19 Feb 2015 22:49:47 +0000 Subject: [PATCH] swap, bankfixed: move the swapin/out methods to the memory manager --- Kernel/bankfixed.c | 69 ++++++++++++++++++++++++++++++++++++++++++ Kernel/swap.c | 75 +++++----------------------------------------- 2 files changed, 77 insertions(+), 67 deletions(-) diff --git a/Kernel/bankfixed.c b/Kernel/bankfixed.c index 758dfd45..a38e909e 100644 --- a/Kernel/bankfixed.c +++ b/Kernel/bankfixed.c @@ -33,6 +33,10 @@ #ifdef CONFIG_BANK_FIXED +#ifndef CONFIG_COMMON_COPY +#define flush_cache(p) do {} while(0) +#endif + /* Kernel is 0, apps 1,2,3 etc */ static unsigned char pfree[MAX_MAPS]; static unsigned char pfptr = 0; @@ -77,4 +81,69 @@ uint16_t pagemap_mem_used(void) return (pfmax - pfptr) * (MAP_SIZE >> 10); } +/* + * Swap out the memory of a process to make room + * for something else + */ +int swapout(ptptr p) +{ + uint16_t page = p->p_page; + uint16_t blk; + uint16_t map; + + swapproc = p; + + if (page) { +#ifdef DEBUG + kprintf("Swapping out %x (%d)\n", p, p->p_page); +#endif + /* Are we out of swap ? */ + map = swapmap_alloc(); + if (map == 0) + return ENOMEM; + flush_cache(p); + blk = map * SWAP_SIZE; + /* Write the app (and possibly the uarea etc..) to disk */ + swapwrite(SWAPDEV, blk, SWAPTOP - SWAPBASE, + SWAPBASE); + pagemap_free(p); + p->p_page = 0; + p->p_page2 = map; +#ifdef DEBUG + kprintf("%x: swapout done %d\n", p, p->p_page); +#endif + } +#ifdef DEBUG + else + kprintf("%x: process already swapped!\n", p); +#endif + return 0; +} + +/* + * Swap ourself in: must be on the swap stack when we do this + */ +void swapin(ptptr p) +{ + uint16_t blk = p->p_page2 * SWAP_SIZE; + +#ifdef DEBUG + kprintf("Swapin %x, %d\n", p, p->p_page); +#endif + if (!p->p_page) { + kprintf("%x: nopage!\n", p); + return; + } + + /* Return our swap */ + swapmap_add(p->p_page2); + + swapproc = p; /* always ourself */ + swapread(SWAPDEV, blk, SWAPTOP - SWAPBASE, + SWAPBASE); +#ifdef DEBUG + kprintf("%x: swapin done %d\n", p, p->p_page); +#endif +} + #endif diff --git a/Kernel/swap.c b/Kernel/swap.c index 9b6eaef7..bac72ed4 100644 --- a/Kernel/swap.c +++ b/Kernel/swap.c @@ -11,9 +11,6 @@ #ifdef SWAPDEV -#ifndef CONFIG_COMMON_COPY -#define flush_cache(p) do {} while(0) -#endif uint8_t *swapbase; unsigned int swapcnt; @@ -31,6 +28,14 @@ void swapmap_add(uint8_t swap) swapmap[swapptr++] = swap; } +int swapmap_alloc(void) +{ + if (swapptr) + return swapmap[--swapptr]; + else + return 0; +} + int swapread(uint16_t dev, blkno_t blkno, unsigned int nbytes, uint8_t *buf) { @@ -50,70 +55,6 @@ int swapwrite(uint16_t dev, blkno_t blkno, unsigned int nbytes, return ((*dev_tab[major(dev)].dev_write) (minor(dev), 2, 0)); } -/* - * Swap out the memory of a process to make room - * for something else - */ -int swapout(ptptr p) -{ - uint16_t page = p->p_page; - uint16_t blk; - uint16_t map; - - swapproc = p; - - if (page) { -#ifdef DEBUG - kprintf("Swapping out %x (%d)\n", p, p->p_page); -#endif - /* Are we out of swap ? */ - if (swapptr == 0) - return ENOMEM; - flush_cache(p); - map = swapmap[--swapptr]; - blk = map * SWAP_SIZE; - /* Write the app (and possibly the uarea etc..) to disk */ - swapwrite(SWAPDEV, blk, SWAPTOP - SWAPBASE, - SWAPBASE); - pagemap_free(p); - p->p_page = 0; - p->p_page2 = map; -#ifdef DEBUG - kprintf("%x: swapout done %d\n", p, p->p_page); -#endif - } -#ifdef DEBUG - else - kprintf("%x: process already swapped!\n", p); -#endif - return 0; -} - -/* - * Swap ourself in: must be on the swap stack when we do this - */ -void swapin(ptptr p) -{ - uint16_t blk = p->p_page2 * SWAP_SIZE; - -#ifdef DEBUG - kprintf("Swapin %x, %d\n", p, p->p_page); -#endif - if (!p->p_page) { - kprintf("%x: nopage!\n", p); - return; - } - - /* Return our swap */ - swapmap[swapptr++] = p->p_page2; - - swapproc = p; /* always ourself */ - swapread(SWAPDEV, blk, SWAPTOP - SWAPBASE, - SWAPBASE); -#ifdef DEBUG - kprintf("%x: swapin done %d\n", p, p->p_page); -#endif -} /* * Swap out process. As we have them all the same size we ignore -- 2.34.1