Home | History | Annotate | Line # | Download | only in kern
subr_kmem.c revision 1.4.2.2
      1  1.4.2.2  gdamore /*	$NetBSD: subr_kmem.c,v 1.4.2.2 2006/07/13 17:49:51 gdamore Exp $	*/
      2  1.4.2.2  gdamore 
      3  1.4.2.2  gdamore /*-
      4  1.4.2.2  gdamore  * Copyright (c)2006 YAMAMOTO Takashi,
      5  1.4.2.2  gdamore  * All rights reserved.
      6  1.4.2.2  gdamore  *
      7  1.4.2.2  gdamore  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  gdamore  * modification, are permitted provided that the following conditions
      9  1.4.2.2  gdamore  * are met:
     10  1.4.2.2  gdamore  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  gdamore  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  gdamore  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  gdamore  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  gdamore  *
     16  1.4.2.2  gdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.4.2.2  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.4.2.2  gdamore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.4.2.2  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.4.2.2  gdamore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.4.2.2  gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.4.2.2  gdamore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.4.2.2  gdamore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.4.2.2  gdamore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.4.2.2  gdamore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.4.2.2  gdamore  * SUCH DAMAGE.
     27  1.4.2.2  gdamore  */
     28  1.4.2.2  gdamore 
     29  1.4.2.2  gdamore /*
     30  1.4.2.2  gdamore  * allocator of kernel wired memory.
     31  1.4.2.2  gdamore  *
     32  1.4.2.2  gdamore  * TODO:
     33  1.4.2.2  gdamore  * -	worth to have "intrsafe" version?  maybe..
     34  1.4.2.2  gdamore  */
     35  1.4.2.2  gdamore 
     36  1.4.2.2  gdamore #include <sys/cdefs.h>
     37  1.4.2.2  gdamore __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.4.2.2 2006/07/13 17:49:51 gdamore Exp $");
     38  1.4.2.2  gdamore 
     39  1.4.2.2  gdamore #include <sys/param.h>
     40  1.4.2.2  gdamore #include <sys/kmem.h>
     41  1.4.2.2  gdamore #include <sys/vmem.h>
     42  1.4.2.2  gdamore 
     43  1.4.2.2  gdamore #include <lib/libkern/libkern.h>
     44  1.4.2.2  gdamore 
     45  1.4.2.2  gdamore #define	KMEM_QUANTUM_SIZE	(ALIGNBYTES + 1)
     46  1.4.2.2  gdamore 
     47  1.4.2.2  gdamore static vmem_t *kmem_arena;
     48  1.4.2.2  gdamore 
     49  1.4.2.2  gdamore #if defined(DEBUG)
     50  1.4.2.2  gdamore static void kmem_poison_fill(void *, size_t);
     51  1.4.2.2  gdamore static void kmem_poison_check(void *, size_t);
     52  1.4.2.2  gdamore #else /* defined(DEBUG) */
     53  1.4.2.2  gdamore #define	kmem_poison_fill(p, sz)		/* nothing */
     54  1.4.2.2  gdamore #define	kmem_poison_check(p, sz)	/* nothing */
     55  1.4.2.2  gdamore #endif /* defined(DEBUG) */
     56  1.4.2.2  gdamore 
     57  1.4.2.2  gdamore static vmem_addr_t kmem_backend_alloc(vmem_t *, vmem_size_t, vmem_size_t *,
     58  1.4.2.2  gdamore     vm_flag_t);
     59  1.4.2.2  gdamore static void kmem_backend_free(vmem_t *, vmem_addr_t, vmem_size_t);
     60  1.4.2.2  gdamore 
     61  1.4.2.2  gdamore static inline vm_flag_t
     62  1.4.2.2  gdamore kmf_to_vmf(km_flag_t kmflags)
     63  1.4.2.2  gdamore {
     64  1.4.2.2  gdamore 	vm_flag_t vmflags;
     65  1.4.2.2  gdamore 
     66  1.4.2.2  gdamore 	KASSERT((kmflags & (KM_SLEEP|KM_NOSLEEP)) != 0);
     67  1.4.2.2  gdamore 	KASSERT((~kmflags & (KM_SLEEP|KM_NOSLEEP)) != 0);
     68  1.4.2.2  gdamore 
     69  1.4.2.2  gdamore 	vmflags = 0;
     70  1.4.2.2  gdamore 	if ((kmflags & KM_SLEEP) != 0) {
     71  1.4.2.2  gdamore 		vmflags |= VM_SLEEP;
     72  1.4.2.2  gdamore 	}
     73  1.4.2.2  gdamore 	if ((kmflags & KM_NOSLEEP) != 0) {
     74  1.4.2.2  gdamore 		vmflags |= VM_NOSLEEP;
     75  1.4.2.2  gdamore 	}
     76  1.4.2.2  gdamore 
     77  1.4.2.2  gdamore 	return vmflags;
     78  1.4.2.2  gdamore }
     79  1.4.2.2  gdamore 
     80  1.4.2.2  gdamore /* ---- kmem API */
     81  1.4.2.2  gdamore 
     82  1.4.2.2  gdamore /*
     83  1.4.2.2  gdamore  * kmem_alloc: allocate wired memory.
     84  1.4.2.2  gdamore  *
     85  1.4.2.2  gdamore  * => must not be called from interrupt context.
     86  1.4.2.2  gdamore  */
     87  1.4.2.2  gdamore 
     88  1.4.2.2  gdamore void *
     89  1.4.2.2  gdamore kmem_alloc(size_t size, km_flag_t kmflags)
     90  1.4.2.2  gdamore {
     91  1.4.2.2  gdamore 	void *p;
     92  1.4.2.2  gdamore 
     93  1.4.2.2  gdamore 	p = (void *)vmem_alloc(kmem_arena, size,
     94  1.4.2.2  gdamore 	    kmf_to_vmf(kmflags) | VM_INSTANTFIT);
     95  1.4.2.2  gdamore 	kmem_poison_check(p, size);
     96  1.4.2.2  gdamore 	return p;
     97  1.4.2.2  gdamore }
     98  1.4.2.2  gdamore 
     99  1.4.2.2  gdamore /*
    100  1.4.2.2  gdamore  * kmem_zalloc: allocate wired memory.
    101  1.4.2.2  gdamore  *
    102  1.4.2.2  gdamore  * => must not be called from interrupt context.
    103  1.4.2.2  gdamore  */
    104  1.4.2.2  gdamore 
    105  1.4.2.2  gdamore void *
    106  1.4.2.2  gdamore kmem_zalloc(size_t size, km_flag_t kmflags)
    107  1.4.2.2  gdamore {
    108  1.4.2.2  gdamore 	void *p;
    109  1.4.2.2  gdamore 
    110  1.4.2.2  gdamore 	p = kmem_alloc(size, kmflags);
    111  1.4.2.2  gdamore 	if (p != NULL) {
    112  1.4.2.2  gdamore 		memset(p, 0, size);
    113  1.4.2.2  gdamore 	}
    114  1.4.2.2  gdamore 	return p;
    115  1.4.2.2  gdamore }
    116  1.4.2.2  gdamore 
    117  1.4.2.2  gdamore /*
    118  1.4.2.2  gdamore  * kmem_free: free wired memory allocated by kmem_alloc.
    119  1.4.2.2  gdamore  *
    120  1.4.2.2  gdamore  * => must not be called from interrupt context.
    121  1.4.2.2  gdamore  */
    122  1.4.2.2  gdamore 
    123  1.4.2.2  gdamore void
    124  1.4.2.2  gdamore kmem_free(void *p, size_t size)
    125  1.4.2.2  gdamore {
    126  1.4.2.2  gdamore 
    127  1.4.2.2  gdamore 	kmem_poison_fill(p, size);
    128  1.4.2.2  gdamore 	vmem_free(kmem_arena, (vmem_addr_t)p, size);
    129  1.4.2.2  gdamore }
    130  1.4.2.2  gdamore 
    131  1.4.2.2  gdamore void
    132  1.4.2.2  gdamore kmem_init(void)
    133  1.4.2.2  gdamore {
    134  1.4.2.2  gdamore 
    135  1.4.2.2  gdamore 	kmem_arena = vmem_create("kmem", 0, 0, KMEM_QUANTUM_SIZE,
    136  1.4.2.2  gdamore 	    kmem_backend_alloc, kmem_backend_free, NULL, 0, VM_SLEEP);
    137  1.4.2.2  gdamore }
    138  1.4.2.2  gdamore 
    139  1.4.2.2  gdamore size_t
    140  1.4.2.2  gdamore kmem_roundup_size(size_t size)
    141  1.4.2.2  gdamore {
    142  1.4.2.2  gdamore 
    143  1.4.2.2  gdamore 	return vmem_roundup_size(kmem_arena, size);
    144  1.4.2.2  gdamore }
    145  1.4.2.2  gdamore 
    146  1.4.2.2  gdamore /* ---- uvm glue */
    147  1.4.2.2  gdamore 
    148  1.4.2.2  gdamore #include <uvm/uvm_extern.h>
    149  1.4.2.2  gdamore 
    150  1.4.2.2  gdamore static vmem_addr_t
    151  1.4.2.2  gdamore kmem_backend_alloc(vmem_t *dummy, vmem_size_t size, vmem_size_t *resultsize,
    152  1.4.2.2  gdamore     vm_flag_t vmflags)
    153  1.4.2.2  gdamore {
    154  1.4.2.2  gdamore 	uvm_flag_t uflags;
    155  1.4.2.2  gdamore 	vaddr_t va;
    156  1.4.2.2  gdamore 
    157  1.4.2.2  gdamore 	KASSERT(dummy == NULL);
    158  1.4.2.2  gdamore 	KASSERT(size != 0);
    159  1.4.2.2  gdamore 	KASSERT((vmflags & (VM_SLEEP|VM_NOSLEEP)) != 0);
    160  1.4.2.2  gdamore 	KASSERT((~vmflags & (VM_SLEEP|VM_NOSLEEP)) != 0);
    161  1.4.2.2  gdamore 
    162  1.4.2.2  gdamore 	if ((vmflags & VM_NOSLEEP) != 0) {
    163  1.4.2.2  gdamore 		uflags = UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT;
    164  1.4.2.2  gdamore 	} else {
    165  1.4.2.2  gdamore 		uflags = UVM_KMF_WAITVA;
    166  1.4.2.2  gdamore 	}
    167  1.4.2.2  gdamore 	*resultsize = size = round_page(size);
    168  1.4.2.2  gdamore 	va = uvm_km_alloc(kernel_map, size, 0,
    169  1.4.2.2  gdamore 	    uflags | UVM_KMF_WIRED | UVM_KMF_CANFAIL);
    170  1.4.2.2  gdamore 	kmem_poison_fill((void *)va, size);
    171  1.4.2.2  gdamore 	return (vmem_addr_t)va;
    172  1.4.2.2  gdamore }
    173  1.4.2.2  gdamore 
    174  1.4.2.2  gdamore static void
    175  1.4.2.2  gdamore kmem_backend_free(vmem_t *dummy, vmem_addr_t addr, vmem_size_t size)
    176  1.4.2.2  gdamore {
    177  1.4.2.2  gdamore 
    178  1.4.2.2  gdamore 	KASSERT(dummy == NULL);
    179  1.4.2.2  gdamore 	KASSERT(addr != 0);
    180  1.4.2.2  gdamore 	KASSERT(size != 0);
    181  1.4.2.2  gdamore 	KASSERT(size == round_page(size));
    182  1.4.2.2  gdamore 
    183  1.4.2.2  gdamore 	kmem_poison_check((void *)addr, size);
    184  1.4.2.2  gdamore 	uvm_km_free(kernel_map, (vaddr_t)addr, size, UVM_KMF_WIRED);
    185  1.4.2.2  gdamore }
    186  1.4.2.2  gdamore 
    187  1.4.2.2  gdamore /* ---- debug */
    188  1.4.2.2  gdamore 
    189  1.4.2.2  gdamore #if defined(DEBUG)
    190  1.4.2.2  gdamore 
    191  1.4.2.2  gdamore #if defined(_LP64)
    192  1.4.2.2  gdamore #define	PRIME	0x9e37fffffffc0001UL
    193  1.4.2.2  gdamore #else /* defined(_LP64) */
    194  1.4.2.2  gdamore #define	PRIME	0x9e3779b1
    195  1.4.2.2  gdamore #endif /* defined(_LP64) */
    196  1.4.2.2  gdamore 
    197  1.4.2.2  gdamore static inline uint8_t
    198  1.4.2.2  gdamore kmem_poison_pattern(const void *p)
    199  1.4.2.2  gdamore {
    200  1.4.2.2  gdamore 
    201  1.4.2.2  gdamore 	return (uint8_t)((((uintptr_t)p) * PRIME)
    202  1.4.2.2  gdamore 	    >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
    203  1.4.2.2  gdamore }
    204  1.4.2.2  gdamore 
    205  1.4.2.2  gdamore static void
    206  1.4.2.2  gdamore kmem_poison_fill(void *p, size_t sz)
    207  1.4.2.2  gdamore {
    208  1.4.2.2  gdamore 	uint8_t *cp;
    209  1.4.2.2  gdamore 	const uint8_t *ep;
    210  1.4.2.2  gdamore 
    211  1.4.2.2  gdamore 	cp = p;
    212  1.4.2.2  gdamore 	ep = cp + sz;
    213  1.4.2.2  gdamore 	while (cp < ep) {
    214  1.4.2.2  gdamore 		*cp = kmem_poison_pattern(cp);
    215  1.4.2.2  gdamore 		cp++;
    216  1.4.2.2  gdamore 	}
    217  1.4.2.2  gdamore }
    218  1.4.2.2  gdamore 
    219  1.4.2.2  gdamore static void
    220  1.4.2.2  gdamore kmem_poison_check(void *p, size_t sz)
    221  1.4.2.2  gdamore {
    222  1.4.2.2  gdamore 	uint8_t *cp;
    223  1.4.2.2  gdamore 	const uint8_t *ep;
    224  1.4.2.2  gdamore 
    225  1.4.2.2  gdamore 	cp = p;
    226  1.4.2.2  gdamore 	ep = cp + sz;
    227  1.4.2.2  gdamore 	while (cp < ep) {
    228  1.4.2.2  gdamore 		const uint8_t expected = kmem_poison_pattern(cp);
    229  1.4.2.2  gdamore 
    230  1.4.2.2  gdamore 		if (*cp != expected) {
    231  1.4.2.2  gdamore 			panic("%s: %p: 0x%02x != 0x%02x\n",
    232  1.4.2.2  gdamore 			    __func__, cp, *cp, expected);
    233  1.4.2.2  gdamore 		}
    234  1.4.2.2  gdamore 		cp++;
    235  1.4.2.2  gdamore 	}
    236  1.4.2.2  gdamore }
    237  1.4.2.2  gdamore 
    238  1.4.2.2  gdamore #endif /* defined(DEBUG) */
    239