1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6 * Copyright (c) 2015 François Tigeot 7 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com> 8 * All rights reserved. 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 unmodified, this list of conditions, and the following 15 * disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 #ifndef _LINUX_MM_H_ 34 #define _LINUX_MM_H_ 35 36 #include <linux/spinlock.h> 37 #include <linux/gfp.h> 38 #include <linux/kernel.h> 39 #include <linux/mm_types.h> 40 #include <linux/pfn.h> 41 #include <linux/list.h> 42 43 #include <asm/pgtable.h> 44 45 #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) 46 47 /* 48 * Make sure our LinuxKPI defined virtual memory flags don't conflict 49 * with the ones defined by FreeBSD: 50 */ 51 CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0); 52 53 #define VM_READ VM_PROT_READ 54 #define VM_WRITE VM_PROT_WRITE 55 #define VM_EXEC VM_PROT_EXECUTE 56 57 #define VM_PFNINTERNAL (1 << 8) /* FreeBSD private flag to vm_insert_pfn() */ 58 #define VM_MIXEDMAP (1 << 9) 59 #define VM_NORESERVE (1 << 10) 60 #define VM_PFNMAP (1 << 11) 61 #define VM_IO (1 << 12) 62 #define VM_MAYWRITE (1 << 13) 63 #define VM_DONTCOPY (1 << 14) 64 #define VM_DONTEXPAND (1 << 15) 65 #define VM_DONTDUMP (1 << 16) 66 #define VM_SHARED (1 << 17) 67 68 #define VMA_MAX_PREFAULT_RECORD 1 69 70 #define FOLL_WRITE (1 << 0) 71 #define FOLL_FORCE (1 << 1) 72 73 #define VM_FAULT_OOM (1 << 0) 74 #define VM_FAULT_SIGBUS (1 << 1) 75 #define VM_FAULT_MAJOR (1 << 2) 76 #define VM_FAULT_WRITE (1 << 3) 77 #define VM_FAULT_HWPOISON (1 << 4) 78 #define VM_FAULT_HWPOISON_LARGE (1 << 5) 79 #define VM_FAULT_SIGSEGV (1 << 6) 80 #define VM_FAULT_NOPAGE (1 << 7) 81 #define VM_FAULT_LOCKED (1 << 8) 82 #define VM_FAULT_RETRY (1 << 9) 83 #define VM_FAULT_FALLBACK (1 << 10) 84 85 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \ 86 VM_FAULT_HWPOISON |VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 87 88 #define FAULT_FLAG_WRITE (1 << 0) 89 #define FAULT_FLAG_MKWRITE (1 << 1) 90 #define FAULT_FLAG_ALLOW_RETRY (1 << 2) 91 #define FAULT_FLAG_RETRY_NOWAIT (1 << 3) 92 #define FAULT_FLAG_KILLABLE (1 << 4) 93 #define FAULT_FLAG_TRIED (1 << 5) 94 #define FAULT_FLAG_USER (1 << 6) 95 #define FAULT_FLAG_REMOTE (1 << 7) 96 #define FAULT_FLAG_INSTRUCTION (1 << 8) 97 98 #define fault_flag_allow_retry_first(flags) \ 99 (((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY) 100 101 typedef int (*pte_fn_t)(linux_pte_t *, unsigned long addr, void *data); 102 103 struct vm_area_struct { 104 vm_offset_t vm_start; 105 vm_offset_t vm_end; 106 vm_offset_t vm_pgoff; 107 pgprot_t vm_page_prot; 108 unsigned long vm_flags; 109 struct mm_struct *vm_mm; 110 void *vm_private_data; 111 const struct vm_operations_struct *vm_ops; 112 struct linux_file *vm_file; 113 114 /* internal operation */ 115 vm_paddr_t vm_pfn; /* PFN for memory map */ 116 vm_size_t vm_len; /* length for memory map */ 117 vm_pindex_t vm_pfn_first; 118 int vm_pfn_count; 119 int *vm_pfn_pcount; 120 vm_object_t vm_obj; 121 vm_map_t vm_cached_map; 122 TAILQ_ENTRY(vm_area_struct) vm_entry; 123 }; 124 125 struct vm_fault { 126 unsigned int flags; 127 pgoff_t pgoff; 128 union { 129 /* user-space address */ 130 void *virtual_address; /* < 4.11 */ 131 unsigned long address; /* >= 4.11 */ 132 }; 133 struct page *page; 134 struct vm_area_struct *vma; 135 }; 136 137 struct vm_operations_struct { 138 void (*open) (struct vm_area_struct *); 139 void (*close) (struct vm_area_struct *); 140 int (*fault) (struct vm_fault *); 141 int (*access) (struct vm_area_struct *, unsigned long, void *, int, int); 142 }; 143 144 struct sysinfo { 145 uint64_t totalram; 146 uint64_t totalhigh; 147 uint32_t mem_unit; 148 }; 149 150 /* 151 * Compute log2 of the power of two rounded up count of pages 152 * needed for size bytes. 153 */ 154 static inline int 155 get_order(unsigned long size) 156 { 157 int order; 158 159 size = (size - 1) >> PAGE_SHIFT; 160 order = 0; 161 while (size) { 162 order++; 163 size >>= 1; 164 } 165 return (order); 166 } 167 168 static inline void * 169 lowmem_page_address(struct page *page) 170 { 171 return (page_address(page)); 172 } 173 174 /* 175 * This only works via memory map operations. 176 */ 177 static inline int 178 io_remap_pfn_range(struct vm_area_struct *vma, 179 unsigned long addr, unsigned long pfn, unsigned long size, 180 vm_memattr_t prot) 181 { 182 vma->vm_page_prot = prot; 183 vma->vm_pfn = pfn; 184 vma->vm_len = size; 185 186 return (0); 187 } 188 189 vm_fault_t 190 lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr, 191 unsigned long pfn, pgprot_t prot); 192 193 static inline vm_fault_t 194 vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 195 unsigned long pfn, pgprot_t prot) 196 { 197 vm_fault_t ret; 198 199 VM_OBJECT_WLOCK(vma->vm_obj); 200 ret = lkpi_vmf_insert_pfn_prot_locked(vma, addr, pfn, prot); 201 VM_OBJECT_WUNLOCK(vma->vm_obj); 202 203 return (ret); 204 } 205 #define vmf_insert_pfn_prot(...) \ 206 _Static_assert(false, \ 207 "This function is always called in a loop. Consider using the locked version") 208 209 static inline int 210 apply_to_page_range(struct mm_struct *mm, unsigned long address, 211 unsigned long size, pte_fn_t fn, void *data) 212 { 213 return (-ENOTSUP); 214 } 215 216 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 217 unsigned long size); 218 219 static inline int 220 remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 221 unsigned long pfn, unsigned long size, pgprot_t prot) 222 { 223 return (-ENOTSUP); 224 } 225 226 static inline unsigned long 227 vma_pages(struct vm_area_struct *vma) 228 { 229 return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); 230 } 231 232 #define offset_in_page(off) ((unsigned long)(off) & (PAGE_SIZE - 1)) 233 234 static inline void 235 set_page_dirty(struct vm_page *page) 236 { 237 vm_page_dirty(page); 238 } 239 240 static inline void 241 mark_page_accessed(struct vm_page *page) 242 { 243 vm_page_reference(page); 244 } 245 246 static inline void 247 get_page(struct vm_page *page) 248 { 249 vm_page_wire(page); 250 } 251 252 extern long 253 get_user_pages(unsigned long start, unsigned long nr_pages, 254 int gup_flags, struct page **, 255 struct vm_area_struct **); 256 257 extern int 258 __get_user_pages_fast(unsigned long start, int nr_pages, int write, 259 struct page **); 260 261 extern long 262 get_user_pages_remote(struct task_struct *, struct mm_struct *, 263 unsigned long start, unsigned long nr_pages, 264 int gup_flags, struct page **, 265 struct vm_area_struct **); 266 267 static inline void 268 put_page(struct vm_page *page) 269 { 270 vm_page_unwire(page, PQ_ACTIVE); 271 } 272 273 #define copy_highpage(to, from) pmap_copy_page(from, to) 274 275 static inline pgprot_t 276 vm_get_page_prot(unsigned long vm_flags) 277 { 278 return (vm_flags & VM_PROT_ALL); 279 } 280 281 static inline vm_page_t 282 vmalloc_to_page(const void *addr) 283 { 284 vm_paddr_t paddr; 285 286 paddr = pmap_kextract((vm_offset_t)addr); 287 return (PHYS_TO_VM_PAGE(paddr)); 288 } 289 290 extern int is_vmalloc_addr(const void *addr); 291 void si_meminfo(struct sysinfo *si); 292 293 #define unmap_mapping_range(...) lkpi_unmap_mapping_range(__VA_ARGS__) 294 void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused, 295 loff_t const holelen, int even_cows __unused); 296 297 #define PAGE_ALIGNED(p) __is_aligned(p, PAGE_SIZE) 298 299 #endif /* _LINUX_MM_H_ */ 300