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>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
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.
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.
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>
42 #include <asm/pgtable.h>
44 #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE)
47 * Make sure our LinuxKPI defined virtual memory flags don't conflict
48 * with the ones defined by FreeBSD:
50 CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0);
52 #define VM_PFNINTERNAL (1 << 8) /* FreeBSD private flag to vm_insert_pfn() */
53 #define VM_MIXEDMAP (1 << 9)
54 #define VM_NORESERVE (1 << 10)
55 #define VM_PFNMAP (1 << 11)
56 #define VM_IO (1 << 12)
57 #define VM_MAYWRITE (1 << 13)
58 #define VM_DONTCOPY (1 << 14)
59 #define VM_DONTEXPAND (1 << 15)
60 #define VM_DONTDUMP (1 << 16)
62 #define VMA_MAX_PREFAULT_RECORD 1
64 #define FOLL_WRITE (1 << 0)
65 #define FOLL_FORCE (1 << 1)
67 #define VM_FAULT_OOM (1 << 0)
68 #define VM_FAULT_SIGBUS (1 << 1)
69 #define VM_FAULT_MAJOR (1 << 2)
70 #define VM_FAULT_WRITE (1 << 3)
71 #define VM_FAULT_HWPOISON (1 << 4)
72 #define VM_FAULT_HWPOISON_LARGE (1 << 5)
73 #define VM_FAULT_SIGSEGV (1 << 6)
74 #define VM_FAULT_NOPAGE (1 << 7)
75 #define VM_FAULT_LOCKED (1 << 8)
76 #define VM_FAULT_RETRY (1 << 9)
77 #define VM_FAULT_FALLBACK (1 << 10)
79 #define FAULT_FLAG_WRITE (1 << 0)
80 #define FAULT_FLAG_MKWRITE (1 << 1)
81 #define FAULT_FLAG_ALLOW_RETRY (1 << 2)
82 #define FAULT_FLAG_RETRY_NOWAIT (1 << 3)
83 #define FAULT_FLAG_KILLABLE (1 << 4)
84 #define FAULT_FLAG_TRIED (1 << 5)
85 #define FAULT_FLAG_USER (1 << 6)
86 #define FAULT_FLAG_REMOTE (1 << 7)
87 #define FAULT_FLAG_INSTRUCTION (1 << 8)
89 typedef int (*pte_fn_t)(pte_t *, pgtable_t, unsigned long addr, void *data);
91 struct vm_area_struct {
95 vm_paddr_t vm_pfn; /* PFN For mmap. */
96 vm_size_t vm_len; /* length for mmap. */
97 vm_memattr_t vm_page_prot;
103 void *virtual_address; /* user-space address */
107 struct vm_operations_struct {
108 void (*open) (struct vm_area_struct *);
109 void (*close) (struct vm_area_struct *);
110 int (*fault) (struct vm_area_struct *, struct vm_fault *);
114 * Compute log2 of the power of two rounded up count of pages
115 * needed for size bytes.
118 get_order(unsigned long size)
122 size = (size - 1) >> PAGE_SHIFT;
132 lowmem_page_address(struct page *page)
134 return (page_address(page));
138 * This only works via memory map operations.
141 io_remap_pfn_range(struct vm_area_struct *vma,
142 unsigned long addr, unsigned long pfn, unsigned long size,
145 vma->vm_page_prot = prot;
153 apply_to_page_range(struct mm_struct *mm, unsigned long address,
154 unsigned long size, pte_fn_t fn, void *data)
160 zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
167 remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
168 unsigned long pfn, unsigned long size, pgprot_t prot)
173 static inline unsigned long
174 vma_pages(struct vm_area_struct *vma)
176 return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
179 #define offset_in_page(off) ((off) & (PAGE_SIZE - 1))
182 set_page_dirty(struct vm_page *page)
188 set_page_dirty_lock(struct vm_page *page)
192 vm_page_unlock(page);
196 mark_page_accessed(struct vm_page *page)
198 vm_page_reference(page);
202 get_page(struct vm_page *page)
207 vm_page_unlock(page);
211 get_user_pages(unsigned long start, unsigned long nr_pages,
212 int gup_flags, struct page **,
213 struct vm_area_struct **);
216 __get_user_pages_fast(unsigned long start, int nr_pages, int write,
220 get_user_pages_remote(struct task_struct *, struct mm_struct *,
221 unsigned long start, unsigned long nr_pages,
222 int gup_flags, struct page **,
223 struct vm_area_struct **);
226 put_page(struct vm_page *page)
229 vm_page_unwire(page, PQ_ACTIVE);
230 vm_page_unhold(page);
231 vm_page_unlock(page);
234 #define copy_highpage(to, from) pmap_copy_page(from, to)
236 static inline pgprot_t
237 vm_get_page_prot(unsigned long vm_flags)
239 return (vm_flags & VM_PROT_ALL);
242 extern int vm_insert_mixed(struct vm_area_struct *, unsigned long addr, pfn_t pfn);
245 vm_insert_pfn(struct vm_area_struct *, unsigned long addr,
249 vm_insert_pfn_prot(struct vm_area_struct *, unsigned long addr,
250 unsigned long pfn, pgprot_t pgprot);
252 static inline vm_page_t
253 vmalloc_to_page(const void *addr)
257 paddr = pmap_kextract((vm_offset_t)addr);
258 return (PHYS_TO_VM_PAGE(paddr));
261 extern int is_vmalloc_addr(const void *addr);
263 #endif /* _LINUX_MM_H_ */