1 /* 2 * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/queue.h> 28 #include <vm/vm_extern.h> 29 30 #include <linux/vmalloc.h> 31 #include <linux/slab.h> 32 #include <linux/mm.h> 33 34 struct vmap { 35 void *addr; 36 int npages; 37 SLIST_ENTRY(vmap) vm_vmaps; 38 }; 39 40 struct lock vmap_lock = LOCK_INITIALIZER("dlvml", 0, LK_CANRECURSE); 41 42 SLIST_HEAD(vmap_list_head, vmap) vmap_list = SLIST_HEAD_INITIALIZER(vmap_list); 43 44 /* vmap: map an array of pages into virtually contiguous space */ 45 void * 46 vmap(struct page **pages, unsigned int count, 47 unsigned long flags, pgprot_t prot) 48 { 49 struct vmap *vmp; 50 vm_offset_t off; 51 size_t size; 52 53 vmp = kmalloc(sizeof(struct vmap), M_DRM, M_WAITOK | M_ZERO); 54 55 size = count * PAGE_SIZE; 56 off = kmem_alloc_nofault(kernel_map, size, 57 VM_SUBSYS_DRM_VMAP, PAGE_SIZE); 58 if (off == 0) 59 return (NULL); 60 61 vmp->addr = (void *)off; 62 vmp->npages = count; 63 pmap_qenter(off, (struct vm_page **)pages, count); 64 lockmgr(&vmap_lock, LK_EXCLUSIVE); 65 SLIST_INSERT_HEAD(&vmap_list, vmp, vm_vmaps); 66 lockmgr(&vmap_lock, LK_RELEASE); 67 68 return (void *)off; 69 } 70 71 void 72 vunmap(const void *addr) 73 { 74 struct vmap *vmp, *tmp_vmp; 75 size_t size; 76 77 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 78 if (vmp->addr == addr) { 79 size = vmp->npages * PAGE_SIZE; 80 81 pmap_qremove((vm_offset_t)addr, vmp->npages); 82 kmem_free(kernel_map, (vm_offset_t)addr, size); 83 goto found; 84 } 85 } 86 87 found: 88 lockmgr(&vmap_lock, LK_EXCLUSIVE); 89 SLIST_REMOVE(&vmap_list, vmp, vmap, vm_vmaps); 90 lockmgr(&vmap_lock, LK_RELEASE); 91 kfree(vmp); 92 } 93 94 int 95 is_vmalloc_addr(const void *x) 96 { 97 struct vmap *vmp, *tmp_vmp; 98 99 SLIST_FOREACH_MUTABLE(vmp, &vmap_list, vm_vmaps, tmp_vmp) { 100 if (vmp->addr == x) 101 return 1; 102 } 103 104 return false; 105 } 106 107 void * 108 vmalloc(unsigned long size) 109 { 110 return kmalloc(size, M_DRM, M_WAITOK); 111 } 112 113 void * 114 vzalloc(unsigned long size) 115 { 116 return kmalloc(size, M_DRM, M_WAITOK | M_ZERO); 117 } 118 119 /* allocate zeroed virtually contiguous memory for userspace */ 120 void * 121 vmalloc_user(unsigned long size) 122 { 123 return kmalloc(size, M_DRM, M_WAITOK | M_ZERO); 124 } 125 126 void 127 vfree(const void *addr) 128 { 129 void *nc_addr; 130 131 memcpy(&nc_addr, &addr, sizeof(void *)); 132 kfree(nc_addr); 133 } 134 135 void * 136 kvmalloc_array(size_t n, size_t size, gfp_t flags) 137 { 138 if (n == 0) 139 return NULL; 140 141 if (n > SIZE_MAX / size) 142 return NULL; 143 144 return kmalloc(n * size, M_DRM, flags); 145 } 146