2010-01-31 14:54:32 +03:00
|
|
|
/* $NetBSD: subr_kmem.c,v 1.32 2010/01/31 11:54:32 skrll Exp $ */
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c)2006 YAMAMOTO Takashi,
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocator of kernel wired memory.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - worth to have "intrsafe" version? maybe..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-01-31 14:54:32 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.32 2010/01/31 11:54:32 skrll Exp $");
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2006-08-20 13:45:59 +04:00
|
|
|
#include <sys/callback.h>
|
2006-06-25 12:00:01 +04:00
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/vmem.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/debug.h>
|
2007-11-07 03:23:13 +03:00
|
|
|
#include <sys/lockdebug.h>
|
2009-02-01 21:51:07 +03:00
|
|
|
#include <sys/cpu.h>
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2006-08-20 13:45:59 +04:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
#include <uvm/uvm_map.h>
|
2009-03-29 14:51:53 +04:00
|
|
|
#include <uvm/uvm_kmguard.h>
|
2006-08-20 13:45:59 +04:00
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
|
2006-07-03 13:18:35 +04:00
|
|
|
#define KMEM_QUANTUM_SIZE (ALIGNBYTES + 1)
|
2009-02-01 21:51:07 +03:00
|
|
|
#define KMEM_QCACHE_MAX (KMEM_QUANTUM_SIZE * 32)
|
|
|
|
#define KMEM_CACHE_COUNT 16
|
|
|
|
|
|
|
|
typedef struct kmem_cache {
|
|
|
|
pool_cache_t kc_cache;
|
|
|
|
struct pool_allocator kc_pa;
|
|
|
|
char kc_name[12];
|
|
|
|
} kmem_cache_t;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
static vmem_t *kmem_arena;
|
2006-08-20 13:45:59 +04:00
|
|
|
static struct callback_entry kmem_kva_reclaim_entry;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2009-02-01 21:51:07 +03:00
|
|
|
static kmem_cache_t kmem_cache[KMEM_CACHE_COUNT + 1];
|
|
|
|
static size_t kmem_cache_max;
|
|
|
|
static size_t kmem_cache_min;
|
|
|
|
static size_t kmem_cache_mask;
|
|
|
|
static int kmem_cache_shift;
|
|
|
|
|
2006-07-08 10:01:53 +04:00
|
|
|
#if defined(DEBUG)
|
2009-03-29 14:51:53 +04:00
|
|
|
int kmem_guard_depth;
|
|
|
|
size_t kmem_guard_size;
|
|
|
|
static struct uvm_kmguard kmem_guard;
|
2007-02-10 00:55:00 +03:00
|
|
|
static void *kmem_freecheck;
|
2008-02-09 15:56:20 +03:00
|
|
|
#define KMEM_POISON
|
|
|
|
#define KMEM_REDZONE
|
2009-02-01 21:51:07 +03:00
|
|
|
#define KMEM_SIZE
|
2009-03-29 14:51:53 +04:00
|
|
|
#define KMEM_GUARD
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(DEBUG) */
|
|
|
|
|
|
|
|
#if defined(KMEM_POISON)
|
2006-07-08 10:01:53 +04:00
|
|
|
static void kmem_poison_fill(void *, size_t);
|
|
|
|
static void kmem_poison_check(void *, size_t);
|
2008-02-09 15:56:20 +03:00
|
|
|
#else /* defined(KMEM_POISON) */
|
2006-07-08 10:01:53 +04:00
|
|
|
#define kmem_poison_fill(p, sz) /* nothing */
|
|
|
|
#define kmem_poison_check(p, sz) /* nothing */
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(KMEM_POISON) */
|
|
|
|
|
|
|
|
#if defined(KMEM_REDZONE)
|
|
|
|
#define REDZONE_SIZE 1
|
|
|
|
#else /* defined(KMEM_REDZONE) */
|
|
|
|
#define REDZONE_SIZE 0
|
|
|
|
#endif /* defined(KMEM_REDZONE) */
|
2006-07-08 10:01:53 +04:00
|
|
|
|
2009-02-01 21:51:07 +03:00
|
|
|
#if defined(KMEM_SIZE)
|
2009-02-18 00:54:30 +03:00
|
|
|
#define SIZE_SIZE (max(KMEM_QUANTUM_SIZE, sizeof(size_t)))
|
2009-02-01 21:51:07 +03:00
|
|
|
static void kmem_size_set(void *, size_t);
|
2009-10-13 03:36:02 +04:00
|
|
|
static void kmem_size_check(const void *, size_t);
|
2009-02-01 21:51:07 +03:00
|
|
|
#else
|
|
|
|
#define SIZE_SIZE 0
|
|
|
|
#define kmem_size_set(p, sz) /* nothing */
|
|
|
|
#define kmem_size_check(p, sz) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
static vmem_addr_t kmem_backend_alloc(vmem_t *, vmem_size_t, vmem_size_t *,
|
|
|
|
vm_flag_t);
|
|
|
|
static void kmem_backend_free(vmem_t *, vmem_addr_t, vmem_size_t);
|
2006-08-20 13:45:59 +04:00
|
|
|
static int kmem_kva_reclaim_callback(struct callback_entry *, void *, void *);
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2010-01-31 14:54:32 +03:00
|
|
|
CTASSERT(KM_SLEEP == PR_WAITOK);
|
|
|
|
CTASSERT(KM_NOSLEEP == PR_NOWAIT);
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
static inline vm_flag_t
|
|
|
|
kmf_to_vmf(km_flag_t kmflags)
|
|
|
|
{
|
|
|
|
vm_flag_t vmflags;
|
|
|
|
|
|
|
|
KASSERT((kmflags & (KM_SLEEP|KM_NOSLEEP)) != 0);
|
|
|
|
KASSERT((~kmflags & (KM_SLEEP|KM_NOSLEEP)) != 0);
|
|
|
|
|
|
|
|
vmflags = 0;
|
|
|
|
if ((kmflags & KM_SLEEP) != 0) {
|
|
|
|
vmflags |= VM_SLEEP;
|
|
|
|
}
|
|
|
|
if ((kmflags & KM_NOSLEEP) != 0) {
|
|
|
|
vmflags |= VM_NOSLEEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmflags;
|
|
|
|
}
|
|
|
|
|
2009-02-01 21:51:07 +03:00
|
|
|
static void *
|
|
|
|
kmem_poolpage_alloc(struct pool *pool, int prflags)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (void *)vmem_alloc(kmem_arena, pool->pr_alloc->pa_pagesz,
|
|
|
|
kmf_to_vmf(prflags) | VM_INSTANTFIT);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_poolpage_free(struct pool *pool, void *addr)
|
|
|
|
{
|
|
|
|
|
|
|
|
vmem_free(kmem_arena, (vmem_addr_t)addr, pool->pr_alloc->pa_pagesz);
|
|
|
|
}
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
/* ---- kmem API */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kmem_alloc: allocate wired memory.
|
|
|
|
*
|
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_alloc(size_t size, km_flag_t kmflags)
|
|
|
|
{
|
2009-02-01 21:51:07 +03:00
|
|
|
kmem_cache_t *kc;
|
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
KASSERT(!cpu_intr_p());
|
2009-03-29 14:51:53 +04:00
|
|
|
KASSERT(!cpu_softintr_p());
|
|
|
|
KASSERT(size > 0);
|
|
|
|
|
|
|
|
#ifdef KMEM_GUARD
|
|
|
|
if (size <= kmem_guard_size) {
|
|
|
|
return uvm_kmguard_alloc(&kmem_guard, size,
|
|
|
|
(kmflags & KM_SLEEP) != 0);
|
|
|
|
}
|
|
|
|
#endif
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
size += REDZONE_SIZE + SIZE_SIZE;
|
|
|
|
if (size >= kmem_cache_min && size <= kmem_cache_max) {
|
|
|
|
kc = &kmem_cache[(size + kmem_cache_mask) >> kmem_cache_shift];
|
|
|
|
KASSERT(size <= kc->kc_pa.pa_pagesz);
|
|
|
|
kmflags &= (KM_SLEEP | KM_NOSLEEP);
|
|
|
|
p = pool_cache_get(kc->kc_cache, kmflags);
|
|
|
|
} else {
|
|
|
|
p = (void *)vmem_alloc(kmem_arena, size,
|
|
|
|
kmf_to_vmf(kmflags) | VM_INSTANTFIT);
|
|
|
|
}
|
|
|
|
if (__predict_true(p != NULL)) {
|
2008-12-15 14:42:34 +03:00
|
|
|
kmem_poison_check(p, kmem_roundup_size(size));
|
|
|
|
FREECHECK_OUT(&kmem_freecheck, p);
|
2009-02-01 21:51:07 +03:00
|
|
|
kmem_size_set(p, size);
|
|
|
|
p = (uint8_t *)p + SIZE_SIZE;
|
2007-02-05 14:53:46 +03:00
|
|
|
}
|
2008-12-15 14:42:34 +03:00
|
|
|
return p;
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2006-06-25 12:10:04 +04:00
|
|
|
/*
|
|
|
|
* kmem_zalloc: allocate wired memory.
|
|
|
|
*
|
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_zalloc(size_t size, km_flag_t kmflags)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = kmem_alloc(size, kmflags);
|
|
|
|
if (p != NULL) {
|
|
|
|
memset(p, 0, size);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
/*
|
|
|
|
* kmem_free: free wired memory allocated by kmem_alloc.
|
|
|
|
*
|
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
kmem_free(void *p, size_t size)
|
|
|
|
{
|
2009-02-01 21:51:07 +03:00
|
|
|
kmem_cache_t *kc;
|
|
|
|
|
|
|
|
KASSERT(!cpu_intr_p());
|
2009-03-29 14:51:53 +04:00
|
|
|
KASSERT(!cpu_softintr_p());
|
2009-06-04 02:54:51 +04:00
|
|
|
KASSERT(p != NULL);
|
2009-03-29 14:51:53 +04:00
|
|
|
KASSERT(size > 0);
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2009-03-29 14:51:53 +04:00
|
|
|
#ifdef KMEM_GUARD
|
|
|
|
if (size <= kmem_guard_size) {
|
|
|
|
uvm_kmguard_free(&kmem_guard, size, p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-13 03:35:09 +04:00
|
|
|
size += SIZE_SIZE;
|
|
|
|
p = (uint8_t *)p - SIZE_SIZE;
|
|
|
|
kmem_size_check(p, size + REDZONE_SIZE);
|
2007-02-10 00:55:00 +03:00
|
|
|
FREECHECK_IN(&kmem_freecheck, p);
|
2007-11-07 03:23:13 +03:00
|
|
|
LOCKDEBUG_MEM_CHECK(p, size);
|
2008-02-09 15:56:20 +03:00
|
|
|
kmem_poison_check((char *)p + size,
|
|
|
|
kmem_roundup_size(size + REDZONE_SIZE) - size);
|
2006-07-08 10:01:53 +04:00
|
|
|
kmem_poison_fill(p, size);
|
2009-02-07 01:58:49 +03:00
|
|
|
size += REDZONE_SIZE;
|
2009-02-01 21:51:07 +03:00
|
|
|
if (size >= kmem_cache_min && size <= kmem_cache_max) {
|
|
|
|
kc = &kmem_cache[(size + kmem_cache_mask) >> kmem_cache_shift];
|
|
|
|
KASSERT(size <= kc->kc_pa.pa_pagesz);
|
|
|
|
pool_cache_put(kc->kc_cache, p);
|
|
|
|
} else {
|
2009-02-07 01:58:49 +03:00
|
|
|
vmem_free(kmem_arena, (vmem_addr_t)p, size);
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
void
|
|
|
|
kmem_init(void)
|
|
|
|
{
|
2009-02-01 21:51:07 +03:00
|
|
|
kmem_cache_t *kc;
|
|
|
|
size_t sz;
|
|
|
|
int i;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2009-03-29 14:51:53 +04:00
|
|
|
#ifdef KMEM_GUARD
|
|
|
|
uvm_kmguard_init(&kmem_guard, &kmem_guard_depth, &kmem_guard_size,
|
|
|
|
kernel_map);
|
|
|
|
#endif
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
kmem_arena = vmem_create("kmem", 0, 0, KMEM_QUANTUM_SIZE,
|
2009-02-01 21:51:07 +03:00
|
|
|
kmem_backend_alloc, kmem_backend_free, NULL, KMEM_QCACHE_MAX,
|
|
|
|
VM_SLEEP, IPL_NONE);
|
2006-08-20 13:45:59 +04:00
|
|
|
callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,
|
|
|
|
&kmem_kva_reclaim_entry, kmem_arena, kmem_kva_reclaim_callback);
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* kmem caches start at twice the size of the largest vmem qcache
|
|
|
|
* and end at PAGE_SIZE or earlier. assert that KMEM_QCACHE_MAX
|
|
|
|
* is a power of two.
|
|
|
|
*/
|
|
|
|
KASSERT(ffs(KMEM_QCACHE_MAX) != 0);
|
|
|
|
KASSERT(KMEM_QCACHE_MAX - (1 << (ffs(KMEM_QCACHE_MAX) - 1)) == 0);
|
|
|
|
kmem_cache_shift = ffs(KMEM_QCACHE_MAX);
|
|
|
|
kmem_cache_min = 1 << kmem_cache_shift;
|
|
|
|
kmem_cache_mask = kmem_cache_min - 1;
|
|
|
|
for (i = 1; i <= KMEM_CACHE_COUNT; i++) {
|
|
|
|
sz = i << kmem_cache_shift;
|
|
|
|
if (sz > PAGE_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
kmem_cache_max = sz;
|
|
|
|
kc = &kmem_cache[i];
|
|
|
|
kc->kc_pa.pa_pagesz = sz;
|
|
|
|
kc->kc_pa.pa_alloc = kmem_poolpage_alloc;
|
|
|
|
kc->kc_pa.pa_free = kmem_poolpage_free;
|
2009-02-18 16:04:59 +03:00
|
|
|
sprintf(kc->kc_name, "kmem-%zu", sz);
|
2009-02-01 21:51:07 +03:00
|
|
|
kc->kc_cache = pool_cache_init(sz,
|
|
|
|
KMEM_QUANTUM_SIZE, 0, PR_NOALIGN | PR_NOTOUCH,
|
|
|
|
kc->kc_name, &kc->kc_pa, IPL_NONE,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
KASSERT(kc->kc_cache != NULL);
|
|
|
|
}
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
kmem_roundup_size(size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
return vmem_roundup_size(kmem_arena, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---- uvm glue */
|
|
|
|
|
|
|
|
static vmem_addr_t
|
2006-11-01 13:17:58 +03:00
|
|
|
kmem_backend_alloc(vmem_t *dummy, vmem_size_t size, vmem_size_t *resultsize,
|
|
|
|
vm_flag_t vmflags)
|
2006-06-25 12:00:01 +04:00
|
|
|
{
|
|
|
|
uvm_flag_t uflags;
|
2006-07-08 10:01:53 +04:00
|
|
|
vaddr_t va;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
KASSERT(dummy == NULL);
|
|
|
|
KASSERT(size != 0);
|
|
|
|
KASSERT((vmflags & (VM_SLEEP|VM_NOSLEEP)) != 0);
|
|
|
|
KASSERT((~vmflags & (VM_SLEEP|VM_NOSLEEP)) != 0);
|
|
|
|
|
|
|
|
if ((vmflags & VM_NOSLEEP) != 0) {
|
|
|
|
uflags = UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT;
|
|
|
|
} else {
|
|
|
|
uflags = UVM_KMF_WAITVA;
|
|
|
|
}
|
|
|
|
*resultsize = size = round_page(size);
|
2006-07-08 10:01:53 +04:00
|
|
|
va = uvm_km_alloc(kernel_map, size, 0,
|
2006-06-25 12:00:01 +04:00
|
|
|
uflags | UVM_KMF_WIRED | UVM_KMF_CANFAIL);
|
2007-03-02 15:30:53 +03:00
|
|
|
if (va != 0) {
|
|
|
|
kmem_poison_fill((void *)va, size);
|
|
|
|
}
|
2008-12-15 14:42:34 +03:00
|
|
|
return (vmem_addr_t)va;
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-01 13:17:58 +03:00
|
|
|
kmem_backend_free(vmem_t *dummy, vmem_addr_t addr, vmem_size_t size)
|
2006-06-25 12:00:01 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(dummy == NULL);
|
|
|
|
KASSERT(addr != 0);
|
|
|
|
KASSERT(size != 0);
|
|
|
|
KASSERT(size == round_page(size));
|
|
|
|
|
2006-07-08 10:01:53 +04:00
|
|
|
kmem_poison_check((void *)addr, size);
|
2006-06-25 12:00:01 +04:00
|
|
|
uvm_km_free(kernel_map, (vaddr_t)addr, size, UVM_KMF_WIRED);
|
|
|
|
}
|
2006-07-08 10:01:53 +04:00
|
|
|
|
2006-08-20 17:08:11 +04:00
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
kmem_kva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
|
2006-08-20 17:08:11 +04:00
|
|
|
{
|
|
|
|
vmem_t *vm = obj;
|
|
|
|
|
|
|
|
vmem_reap(vm);
|
|
|
|
return CALLBACK_CHAIN_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2006-07-08 10:01:53 +04:00
|
|
|
/* ---- debug */
|
|
|
|
|
2008-02-09 15:56:20 +03:00
|
|
|
#if defined(KMEM_POISON)
|
2006-07-08 10:01:53 +04:00
|
|
|
|
|
|
|
#if defined(_LP64)
|
|
|
|
#define PRIME 0x9e37fffffffc0001UL
|
|
|
|
#else /* defined(_LP64) */
|
|
|
|
#define PRIME 0x9e3779b1
|
|
|
|
#endif /* defined(_LP64) */
|
|
|
|
|
|
|
|
static inline uint8_t
|
|
|
|
kmem_poison_pattern(const void *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (uint8_t)((((uintptr_t)p) * PRIME)
|
|
|
|
>> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_poison_fill(void *p, size_t sz)
|
|
|
|
{
|
|
|
|
uint8_t *cp;
|
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = p;
|
|
|
|
ep = cp + sz;
|
|
|
|
while (cp < ep) {
|
|
|
|
*cp = kmem_poison_pattern(cp);
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_poison_check(void *p, size_t sz)
|
|
|
|
{
|
|
|
|
uint8_t *cp;
|
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = p;
|
|
|
|
ep = cp + sz;
|
|
|
|
while (cp < ep) {
|
|
|
|
const uint8_t expected = kmem_poison_pattern(cp);
|
|
|
|
|
|
|
|
if (*cp != expected) {
|
|
|
|
panic("%s: %p: 0x%02x != 0x%02x\n",
|
|
|
|
__func__, cp, *cp, expected);
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(KMEM_POISON) */
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
#if defined(KMEM_SIZE)
|
|
|
|
static void
|
|
|
|
kmem_size_set(void *p, size_t sz)
|
|
|
|
{
|
|
|
|
|
|
|
|
memcpy(p, &sz, sizeof(sz));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-10-13 03:36:02 +04:00
|
|
|
kmem_size_check(const void *p, size_t sz)
|
2009-02-01 21:51:07 +03:00
|
|
|
{
|
|
|
|
size_t psz;
|
|
|
|
|
|
|
|
memcpy(&psz, p, sizeof(psz));
|
|
|
|
if (psz != sz) {
|
|
|
|
panic("kmem_free(%p, %zu) != allocated size %zu",
|
2009-10-13 03:36:02 +04:00
|
|
|
(const uint8_t *)p + SIZE_SIZE, sz - SIZE_SIZE, psz);
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(KMEM_SIZE) */
|