From 7fe21e06c16e54a8f12cb122f4ad8f12ec5492b1 Mon Sep 17 00:00:00 2001 From: Brett Gordon Date: Tue, 28 Feb 2017 15:03:06 -0500 Subject: [PATCH] bank16k: add swapping --- Kernel/bank16k.c | 51 +++++++++++++++++++++++++++++++++++++++-- Kernel/include/kernel.h | 4 ++++ Kernel/swap.c | 24 +++++++++++-------- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/Kernel/bank16k.c b/Kernel/bank16k.c index 799ae8f0..2642b476 100644 --- a/Kernel/bank16k.c +++ b/Kernel/bank16k.c @@ -79,12 +79,14 @@ static int maps_needed(uint16_t top) * * Use p-> not udata. The udata is not yet swapped in! */ -int pagemap_alloc(ptptr p) +static int pagemap_alloc2(ptptr p, uint8_t c) { uint8_t *ptr = (uint8_t *) & p->p_page; int needed = maps_needed(p->p_top); int i; + if (c) + needed--; #ifdef SWAPDEV /* Throw our toys out of our pram until we have enough room */ while (needed > pfptr) @@ -99,13 +101,19 @@ int pagemap_alloc(ptptr p) for (i = 0; i < needed; i++) ptr[i] = pfree[--pfptr]; + if (!c) + c = ptr[i - 1]; while (i < 4) { - ptr[i] = ptr[i - 1]; + ptr[i] = c; i++; } return 0; } +int pagemap_alloc( ptptr p ){ + return pagemap_alloc2(p, 0); +} + /* * Reallocate the maps for a process */ @@ -168,6 +176,45 @@ usize_t pagemap_mem_used(void) #ifdef SWAPDEV +/* Returns a page with common copied to it + used by swapping 16k platforms to procure a common page. + platforms will have to provide a void copy_common(uint8_t page) + that copies the current common page to the specified page. +*/ + +uint8_t get_common() +{ + ptptr p = NULL; + /* if current context is dead, then reuse it's common */ + if (udata.u_ptab->p_status == P_ZOMBIE || + udata.u_ptab->p_status == P_EMPTY){ + return pfree[--pfptr]; + } + /* otherwise get alloc a page and copy common to it */ + if (pfptr){ + int ret = pfree[--pfptr]; + copy_common(ret); + return ret; + } + /* No free pages so swap something out */ + /* and return it's common */ + swapneeded(p, 0); + return pfree[--pfptr]; +} + +/* Finish swapping in rest of task p, using page as it's common + - called from swapping 16k platforms to finish the swapping + processes, after starting it with get_common() + +*/ +void swap_finish(uint8_t page, ptptr p) +{ + uint16_t map = p->p_page2; + pagemap_alloc2(p, page); + swapper2(p, map); +} + + /* * Swap out the memory of a process to make room * for something else. For bank16k do this as four operations diff --git a/Kernel/include/kernel.h b/Kernel/include/kernel.h index 5cc48273..e0002a5c 100644 --- a/Kernel/include/kernel.h +++ b/Kernel/include/kernel.h @@ -873,6 +873,9 @@ extern void swapmap_add(uint8_t swap); extern int swapmap_alloc(void); extern ptptr swapneeded(ptptr p, int selfok); extern void swapper(ptptr p); +extern void swapper2(ptptr p, uint16_t map); +extern uint8_t get_common(); +extern void swap_finish(uint8_t page, ptptr p); /* These two are provided by the bank code selected for the port */ extern int swapout(ptptr p); extern void swapin(ptptr p, uint16_t map); @@ -894,6 +897,7 @@ extern void inittod(void); /* provided by architecture or helpers */ extern void device_init(void); /* provided by platform */ extern void pagemap_init(void); +extern void copy_common(uint8_t page); extern void pagemap_add(uint8_t page); /* FIXME: may need a page type for big boxes */ extern void pagemap_free(ptptr p); extern int pagemap_alloc(ptptr p); diff --git a/Kernel/swap.c b/Kernel/swap.c index c8339b89..ef619358 100644 --- a/Kernel/swap.c +++ b/Kernel/swap.c @@ -133,16 +133,8 @@ ptptr swapneeded(ptptr p, int notself) return n; } -/* - * Called from switchin when we discover that we want to run - * a swapped process. We let pagemap_alloc cause any needed swap - * out of idle processes. - */ -void swapper(ptptr p) +void swapper2(ptptr p, uint16_t map) { - uint16_t map = p->p_page2; - pagemap_alloc(p); /* May cause a swapout. May also destroy - the old value of p->page2 */ #ifdef DEBUG kprintf("Swapping in %p (page %d), utab.ptab %p\n", p, p->p_page, udata.u_ptab); @@ -154,4 +146,18 @@ void swapper(ptptr p) p, p->p_page, udata.u_ptab); #endif } + +/* + * Called from switchin when we discover that we want to run + * a swapped process. We let pagemap_alloc cause any needed swap + * out of idle processes. + */ +void swapper(ptptr p) +{ + uint16_t map = p->p_page2; + pagemap_alloc(p); /* May cause a swapout. May also destroy + the old value of p->page2 */ + return swapper2(p, map); +} + #endif -- 2.34.1