Home | History | Annotate | Line # | Download | only in linux
vmalloc.h revision 1.5
      1 /*	$NetBSD: vmalloc.h,v 1.5 2018/08/06 00:30:07 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013, 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_VMALLOC_H_
     33 #define _LINUX_VMALLOC_H_
     34 
     35 #include <sys/malloc.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <linux/mm_types.h>
     40 
     41 #include <asm/page.h>
     42 
     43 static inline bool
     44 is_vmalloc_addr(void *addr)
     45 {
     46 	/* XXX Assumes vmalloc and kmalloc both use malloc(9).  */
     47 	return true;
     48 }
     49 
     50 static inline void *
     51 vmalloc(unsigned long size)
     52 {
     53 	return malloc(size, M_TEMP, M_WAITOK);
     54 }
     55 
     56 static inline void *
     57 vmalloc_user(unsigned long size)
     58 {
     59 	return malloc(size, M_TEMP, (M_WAITOK | M_ZERO));
     60 }
     61 
     62 static inline void *
     63 vzalloc(unsigned long size)
     64 {
     65 	return malloc(size, M_TEMP, (M_WAITOK | M_ZERO));
     66 }
     67 
     68 static inline void
     69 vfree(void *ptr)
     70 {
     71 	return free(ptr, M_TEMP);
     72 }
     73 
     74 #define	PAGE_KERNEL	UVM_PROT_RW
     75 
     76 /*
     77  * vmap(pages, npages, flags, prot)
     78  *
     79  *	Map pages[0], pages[1], ..., pages[npages-1] into contiguous
     80  *	kernel virtual address space with the specified protection, and
     81  *	return a KVA pointer to the start.
     82  *
     83  *	prot may be a bitwise ior of UVM_PROT_READ/WRITE/EXEC and
     84  *	PMAP_* cache flags accepted by pmap_enter().
     85  */
     86 static inline void *
     87 vmap(struct page **pages, unsigned npages, unsigned long flags,
     88     pgprot_t protflags)
     89 {
     90 	vm_prot_t justprot = protflags & UVM_PROT_ALL;
     91 	vaddr_t va;
     92 	unsigned i;
     93 
     94 	/* Allocate some KVA, or return NULL if we can't.  */
     95 	va = uvm_km_alloc(kernel_map, (vsize_t)npages << PAGE_SHIFT, PAGE_SIZE,
     96 	    UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
     97 	if (va == 0)
     98 		return NULL;
     99 
    100 	/* Ask pmap to map the KVA to the specified page addresses.  */
    101 	for (i = 0; i < npages; i++) {
    102 		pmap_kenter_pa(va + i*PAGE_SIZE, page_to_phys(pages[i]),
    103 		    justprot, protflags);
    104 	}
    105 
    106 	/* Commit the pmap updates.  */
    107 	pmap_update(pmap_kernel());
    108 
    109 	return (void *)va;
    110 }
    111 
    112 /*
    113  * vunmap(ptr, npages)
    114  *
    115  *	Unmap the KVA pages starting at ptr that were mapped by a call
    116  *	to vmap with the same npages parameter.
    117  */
    118 static inline void
    119 vunmap(void *ptr, unsigned npages)
    120 {
    121 	vaddr_t va = (vaddr_t)ptr;
    122 
    123 	/* Ask pmap to unmap the KVA.  */
    124 	pmap_kremove(va, (vsize_t)npages << PAGE_SHIFT);
    125 
    126 	/* Commit the pmap updates.  */
    127 	pmap_update(pmap_kernel());
    128 
    129 	/*
    130 	 * Now that the pmap is no longer mapping the KVA we allocated
    131 	 * on any CPU, it is safe to free the KVA.
    132 	 */
    133 	uvm_km_free(kernel_map, va, (vsize_t)npages << PAGE_SHIFT,
    134 	    UVM_KMF_VAONLY);
    135 }
    136 
    137 #endif  /* _LINUX_VMALLOC_H_ */
    138