2002-07-09 12:24:59 +00:00
|
|
|
/*
|
|
|
|
|
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
|
*/
|
2002-10-29 23:07:06 +00:00
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <kernel.h>
|
|
|
|
|
#include <vm.h>
|
|
|
|
|
#include <vm_priv.h>
|
|
|
|
|
#include <vm_cache.h>
|
|
|
|
|
#include <vm_page.h>
|
2002-10-29 23:07:06 +00:00
|
|
|
#include <malloc.h>
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <int.h>
|
|
|
|
|
#include <khash.h>
|
|
|
|
|
#include <lock.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
#include <lock.h>
|
|
|
|
|
#include <smp.h>
|
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
|
#include <Errors.h>
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
//#define TRACE_VM_CACHE
|
|
|
|
|
#ifdef TRACE_VM_CACHE
|
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
|
#else
|
|
|
|
|
# define TRACE(x) ;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
/* hash table of pages keyed by cache they're in and offset */
|
|
|
|
|
#define PAGE_TABLE_SIZE 1024 /* make this dynamic */
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
static void *page_cache_table;
|
2002-10-26 16:13:36 +00:00
|
|
|
static spinlock page_cache_table_lock;
|
2002-07-09 12:24:59 +00:00
|
|
|
|
|
|
|
|
struct page_lookup_key {
|
|
|
|
|
off_t offset;
|
|
|
|
|
vm_cache_ref *ref;
|
|
|
|
|
};
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
page_compare_func(void *_p, const void *_key)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_page *p = _p;
|
|
|
|
|
const struct page_lookup_key *key = _key;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
TRACE(("page_compare_func: p 0x%x, key 0x%x\n", p, key));
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (p->cache_ref == key->ref && p->offset == key->offset)
|
2002-07-09 12:24:59 +00:00
|
|
|
return 0;
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
return -1;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-11-29 08:38:52 +00:00
|
|
|
static uint32
|
|
|
|
|
page_hash_func(void *_p, const void *_key, uint32 range)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_page *p = _p;
|
|
|
|
|
const struct page_lookup_key *key = _key;
|
|
|
|
|
#if 0
|
|
|
|
|
if(p)
|
|
|
|
|
dprintf("page_hash_func: p 0x%x, key 0x%x, HASH = 0x%x\n", p, key, HASH(p->offset, p->cache_ref) % range);
|
|
|
|
|
else
|
|
|
|
|
dprintf("page_hash_func: p 0x%x, key 0x%x, HASH = 0x%x\n", p, key, HASH(key->offset, key->ref) % range);
|
|
|
|
|
#endif
|
2004-09-10 23:43:15 +00:00
|
|
|
#define HASH(offset, ref) ((unsigned int)(offset >> 12) ^ ((unsigned int)(ref)>>4))
|
|
|
|
|
|
|
|
|
|
if (p)
|
2002-07-09 12:24:59 +00:00
|
|
|
return HASH(p->offset, p->cache_ref) % range;
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
return HASH(key->offset, key->ref) % range;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2004-10-08 15:10:50 +00:00
|
|
|
status_t
|
2004-09-10 23:43:15 +00:00
|
|
|
vm_cache_init(kernel_args *ka)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_page p;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
page_cache_table = hash_init(PAGE_TABLE_SIZE, (int)&p.hash_next - (int)&p,
|
|
|
|
|
&page_compare_func, &page_hash_func);
|
|
|
|
|
if (!page_cache_table)
|
2002-07-09 12:24:59 +00:00
|
|
|
panic("vm_cache_init: cannot allocate memory for page cache hash table\n");
|
|
|
|
|
page_cache_table_lock = 0;
|
|
|
|
|
|
2004-10-08 15:10:50 +00:00
|
|
|
return B_OK;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
vm_cache *
|
|
|
|
|
vm_cache_create(vm_store *store)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_cache *cache;
|
|
|
|
|
|
2002-10-29 23:07:06 +00:00
|
|
|
cache = malloc(sizeof(vm_cache));
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache == NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
cache->page_list = NULL;
|
|
|
|
|
cache->ref = NULL;
|
|
|
|
|
cache->source = NULL;
|
|
|
|
|
cache->store = store;
|
2004-09-10 23:43:15 +00:00
|
|
|
if (store != NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
store->cache = cache;
|
|
|
|
|
cache->virtual_size = 0;
|
|
|
|
|
cache->temporary = 0;
|
|
|
|
|
cache->scan_skip = 0;
|
|
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
vm_cache_ref *
|
|
|
|
|
vm_cache_ref_create(vm_cache *cache)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_cache_ref *ref;
|
|
|
|
|
|
2002-10-29 23:07:06 +00:00
|
|
|
ref = malloc(sizeof(vm_cache_ref));
|
2004-09-10 23:43:15 +00:00
|
|
|
if (ref == NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
ref->cache = cache;
|
|
|
|
|
mutex_init(&ref->lock, "cache_ref_mutex");
|
|
|
|
|
ref->region_list = NULL;
|
|
|
|
|
ref->ref_count = 0;
|
|
|
|
|
cache->ref = ref;
|
|
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vm_cache_acquire_ref(vm_cache_ref *cache_ref, bool acquire_store_ref)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
// dprintf("vm_cache_acquire_ref: cache_ref 0x%x, ref will be %d\n", cache_ref, cache_ref->ref_count+1);
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache_ref == NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
panic("vm_cache_acquire_ref: passed NULL\n");
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
if (acquire_store_ref && cache_ref->cache->store->ops->acquire_ref)
|
2002-07-09 12:24:59 +00:00
|
|
|
cache_ref->cache->store->ops->acquire_ref(cache_ref->cache->store);
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
atomic_add(&cache_ref->ref_count, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vm_cache_release_ref(vm_cache_ref *cache_ref)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
2004-09-10 23:43:15 +00:00
|
|
|
off_t store_committed_size = 0;
|
2002-07-09 12:24:59 +00:00
|
|
|
vm_page *page;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
TRACE(("vm_cache_release_ref: cache_ref 0x%x, ref will be %d\n", cache_ref, cache_ref->ref_count - 1));
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache_ref == NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
panic("vm_cache_release_ref: passed NULL\n");
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (atomic_add(&cache_ref->ref_count, -1) != 1) {
|
|
|
|
|
if (cache_ref->cache->store->ops->release_ref)
|
|
|
|
|
cache_ref->cache->store->ops->release_ref(cache_ref->cache->store);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
// delete this cache
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
// delete the cache's backing store, if it has one
|
|
|
|
|
if (cache_ref->cache->store) {
|
|
|
|
|
store_committed_size = cache_ref->cache->store->committed_size;
|
|
|
|
|
(*cache_ref->cache->store->ops->destroy)(cache_ref->cache->store);
|
|
|
|
|
}
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
// free all of the pages in the cache
|
|
|
|
|
page = cache_ref->cache->page_list;
|
|
|
|
|
while (page) {
|
|
|
|
|
vm_page *oldPage = page;
|
|
|
|
|
int state;
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
page = page->cache_next;
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
// remove it from the hash table
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
|
acquire_spinlock(&page_cache_table_lock);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
hash_remove(page_cache_table, oldPage);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
release_spinlock(&page_cache_table_lock);
|
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
|
|
TRACE(("vm_cache_release_ref: freeing page 0x%x\n", oldPage->ppn));
|
|
|
|
|
vm_page_set_state(oldPage, PAGE_STATE_FREE);
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
2004-09-10 23:43:15 +00:00
|
|
|
vm_increase_max_commit(cache_ref->cache->virtual_size - store_committed_size);
|
|
|
|
|
|
|
|
|
|
// remove the ref to the source
|
|
|
|
|
if (cache_ref->cache->source)
|
|
|
|
|
vm_cache_release_ref(cache_ref->cache->source->ref);
|
|
|
|
|
|
|
|
|
|
mutex_destroy(&cache_ref->lock);
|
|
|
|
|
free(cache_ref->cache);
|
|
|
|
|
free(cache_ref);
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
vm_page *
|
|
|
|
|
vm_cache_lookup_page(vm_cache_ref *cache_ref, off_t offset)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
vm_page *page;
|
|
|
|
|
int state;
|
|
|
|
|
struct page_lookup_key key;
|
|
|
|
|
|
|
|
|
|
key.offset = offset;
|
|
|
|
|
key.ref = cache_ref;
|
|
|
|
|
|
2002-07-25 01:05:51 +00:00
|
|
|
state = disable_interrupts();
|
2002-07-09 12:24:59 +00:00
|
|
|
acquire_spinlock(&page_cache_table_lock);
|
|
|
|
|
|
|
|
|
|
page = hash_lookup(page_cache_table, &key);
|
|
|
|
|
|
|
|
|
|
release_spinlock(&page_cache_table_lock);
|
2002-07-25 01:05:51 +00:00
|
|
|
restore_interrupts(state);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
|
|
|
|
return page;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vm_cache_insert_page(vm_cache_ref *cache_ref, vm_page *page, off_t offset)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
int state;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
TRACE(("vm_cache_insert_page: cache 0x%x, page 0x%x, offset 0x%x 0x%x\n", cache_ref, page, offset));
|
2002-07-09 12:24:59 +00:00
|
|
|
|
|
|
|
|
page->offset = offset;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache_ref->cache->page_list != NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
cache_ref->cache->page_list->cache_prev = page;
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
page->cache_next = cache_ref->cache->page_list;
|
|
|
|
|
page->cache_prev = NULL;
|
|
|
|
|
cache_ref->cache->page_list = page;
|
|
|
|
|
|
|
|
|
|
page->cache_ref = cache_ref;
|
|
|
|
|
|
2002-07-25 01:05:51 +00:00
|
|
|
state = disable_interrupts();
|
2002-07-09 12:24:59 +00:00
|
|
|
acquire_spinlock(&page_cache_table_lock);
|
|
|
|
|
|
|
|
|
|
hash_insert(page_cache_table, page);
|
|
|
|
|
|
|
|
|
|
release_spinlock(&page_cache_table_lock);
|
2002-07-25 01:05:51 +00:00
|
|
|
restore_interrupts(state);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2004-10-08 15:10:50 +00:00
|
|
|
/** Removes the vm_page from this cache. Of course, the page must
|
|
|
|
|
* really be in this cache or evil things will happen.
|
|
|
|
|
* The vm_cache_ref lock must be held.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
void
|
|
|
|
|
vm_cache_remove_page(vm_cache_ref *cache_ref, vm_page *page)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
int state;
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
TRACE(("vm_cache_remove_page: cache 0x%x, page 0x%x\n", cache_ref, page));
|
2004-10-08 15:10:50 +00:00
|
|
|
ASSERT_LOCKED_MUTEX(&cache_ref->lock);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2002-07-25 01:05:51 +00:00
|
|
|
state = disable_interrupts();
|
2002-07-09 12:24:59 +00:00
|
|
|
acquire_spinlock(&page_cache_table_lock);
|
|
|
|
|
|
|
|
|
|
hash_remove(page_cache_table, page);
|
|
|
|
|
|
|
|
|
|
release_spinlock(&page_cache_table_lock);
|
2002-07-25 01:05:51 +00:00
|
|
|
restore_interrupts(state);
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache_ref->cache->page_list == page) {
|
|
|
|
|
if (page->cache_next != NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
page->cache_next->cache_prev = NULL;
|
|
|
|
|
cache_ref->cache->page_list = page->cache_next;
|
|
|
|
|
} else {
|
2004-09-10 23:43:15 +00:00
|
|
|
if (page->cache_prev != NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
page->cache_prev->cache_next = page->cache_next;
|
2004-09-10 23:43:15 +00:00
|
|
|
if (page->cache_next != NULL)
|
2002-07-09 12:24:59 +00:00
|
|
|
page->cache_next->cache_prev = page->cache_prev;
|
|
|
|
|
}
|
|
|
|
|
page->cache_ref = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2004-10-08 15:10:50 +00:00
|
|
|
status_t
|
|
|
|
|
vm_cache_resize(vm_cache_ref *cacheRef, size_t newSize)
|
|
|
|
|
{
|
|
|
|
|
vm_cache *cache = cacheRef->cache;
|
|
|
|
|
size_t oldSize;
|
|
|
|
|
|
|
|
|
|
// ToDo: only cache's with an anonymous memory store should be resizable!
|
|
|
|
|
if (!cache->temporary)
|
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
|
|
mutex_lock(&cacheRef->lock);
|
|
|
|
|
|
|
|
|
|
oldSize = cache->virtual_size;
|
|
|
|
|
if (newSize < oldSize) {
|
|
|
|
|
// we need to remove all pages in the cache outside of the new virtual size
|
|
|
|
|
vm_page *page, *next;
|
|
|
|
|
|
|
|
|
|
for (page = cache->page_list; page; page = next) {
|
|
|
|
|
next = page->cache_next;
|
|
|
|
|
|
|
|
|
|
if (page->offset >= newSize) {
|
|
|
|
|
// remove the page and put it into the free queue
|
|
|
|
|
vm_cache_remove_page(cacheRef, page);
|
|
|
|
|
vm_page_set_state(page, PAGE_STATE_FREE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache->virtual_size = newSize;
|
|
|
|
|
mutex_unlock(&cacheRef->lock);
|
|
|
|
|
|
|
|
|
|
vm_increase_max_commit(oldSize - newSize);
|
|
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-09-10 23:43:15 +00:00
|
|
|
vm_cache_insert_region(vm_cache_ref *cache_ref, vm_region *region)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
mutex_lock(&cache_ref->lock);
|
|
|
|
|
|
|
|
|
|
region->cache_next = cache_ref->region_list;
|
2004-09-10 23:43:15 +00:00
|
|
|
if (region->cache_next)
|
2002-07-09 12:24:59 +00:00
|
|
|
region->cache_next->cache_prev = region;
|
|
|
|
|
region->cache_prev = NULL;
|
|
|
|
|
cache_ref->region_list = region;
|
|
|
|
|
|
|
|
|
|
mutex_unlock(&cache_ref->lock);
|
2004-10-08 15:10:50 +00:00
|
|
|
return B_OK;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
|
2004-10-08 15:10:50 +00:00
|
|
|
status_t
|
2004-09-10 23:43:15 +00:00
|
|
|
vm_cache_remove_region(vm_cache_ref *cache_ref, vm_region *region)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
|
|
|
|
mutex_lock(&cache_ref->lock);
|
|
|
|
|
|
2004-09-10 23:43:15 +00:00
|
|
|
if (region->cache_prev)
|
2002-07-09 12:24:59 +00:00
|
|
|
region->cache_prev->cache_next = region->cache_next;
|
2004-09-10 23:43:15 +00:00
|
|
|
if (region->cache_next)
|
2002-07-09 12:24:59 +00:00
|
|
|
region->cache_next->cache_prev = region->cache_prev;
|
2004-09-10 23:43:15 +00:00
|
|
|
if (cache_ref->region_list == region)
|
2002-07-09 12:24:59 +00:00
|
|
|
cache_ref->region_list = region->cache_next;
|
|
|
|
|
|
|
|
|
|
mutex_unlock(&cache_ref->lock);
|
2004-10-08 15:10:50 +00:00
|
|
|
return B_OK;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|