xref: /freebsd/sys/compat/linuxkpi/common/include/linux/mm.h (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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	FAULT_FLAG_WRITE	(1 << 0)
86 #define	FAULT_FLAG_MKWRITE	(1 << 1)
87 #define	FAULT_FLAG_ALLOW_RETRY	(1 << 2)
88 #define	FAULT_FLAG_RETRY_NOWAIT	(1 << 3)
89 #define	FAULT_FLAG_KILLABLE	(1 << 4)
90 #define	FAULT_FLAG_TRIED	(1 << 5)
91 #define	FAULT_FLAG_USER		(1 << 6)
92 #define	FAULT_FLAG_REMOTE	(1 << 7)
93 #define	FAULT_FLAG_INSTRUCTION	(1 << 8)
94 
95 #define fault_flag_allow_retry_first(flags) \
96 	(((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY)
97 
98 typedef int (*pte_fn_t)(linux_pte_t *, pgtable_t, unsigned long addr, void *data);
99 
100 struct vm_area_struct {
101 	vm_offset_t vm_start;
102 	vm_offset_t vm_end;
103 	vm_offset_t vm_pgoff;
104 	pgprot_t vm_page_prot;
105 	unsigned long vm_flags;
106 	struct mm_struct *vm_mm;
107 	void   *vm_private_data;
108 	const struct vm_operations_struct *vm_ops;
109 	struct linux_file *vm_file;
110 
111 	/* internal operation */
112 	vm_paddr_t vm_pfn;		/* PFN for memory map */
113 	vm_size_t vm_len;		/* length for memory map */
114 	vm_pindex_t vm_pfn_first;
115 	int	vm_pfn_count;
116 	int    *vm_pfn_pcount;
117 	vm_object_t vm_obj;
118 	vm_map_t vm_cached_map;
119 	TAILQ_ENTRY(vm_area_struct) vm_entry;
120 };
121 
122 struct vm_fault {
123 	unsigned int flags;
124 	pgoff_t	pgoff;
125 	union {
126 		/* user-space address */
127 		void *virtual_address;	/* < 4.11 */
128 		unsigned long address;	/* >= 4.11 */
129 	};
130 	struct page *page;
131 	struct vm_area_struct *vma;
132 };
133 
134 struct vm_operations_struct {
135 	void    (*open) (struct vm_area_struct *);
136 	void    (*close) (struct vm_area_struct *);
137 	int     (*fault) (struct vm_area_struct *, struct vm_fault *);
138 	int	(*access) (struct vm_area_struct *, unsigned long, void *, int, int);
139 };
140 
141 struct sysinfo {
142 	uint64_t totalram;
143 	uint64_t totalhigh;
144 	uint32_t mem_unit;
145 };
146 
147 /*
148  * Compute log2 of the power of two rounded up count of pages
149  * needed for size bytes.
150  */
151 static inline int
152 get_order(unsigned long size)
153 {
154 	int order;
155 
156 	size = (size - 1) >> PAGE_SHIFT;
157 	order = 0;
158 	while (size) {
159 		order++;
160 		size >>= 1;
161 	}
162 	return (order);
163 }
164 
165 static inline void *
166 lowmem_page_address(struct page *page)
167 {
168 	return (page_address(page));
169 }
170 
171 /*
172  * This only works via memory map operations.
173  */
174 static inline int
175 io_remap_pfn_range(struct vm_area_struct *vma,
176     unsigned long addr, unsigned long pfn, unsigned long size,
177     vm_memattr_t prot)
178 {
179 	vma->vm_page_prot = prot;
180 	vma->vm_pfn = pfn;
181 	vma->vm_len = size;
182 
183 	return (0);
184 }
185 
186 static inline int
187 apply_to_page_range(struct mm_struct *mm, unsigned long address,
188     unsigned long size, pte_fn_t fn, void *data)
189 {
190 	return (-ENOTSUP);
191 }
192 
193 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
194     unsigned long size);
195 
196 static inline int
197 remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
198     unsigned long pfn, unsigned long size, pgprot_t prot)
199 {
200 	return (-ENOTSUP);
201 }
202 
203 static inline unsigned long
204 vma_pages(struct vm_area_struct *vma)
205 {
206 	return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
207 }
208 
209 #define	offset_in_page(off)	((off) & (PAGE_SIZE - 1))
210 
211 static inline void
212 set_page_dirty(struct vm_page *page)
213 {
214 	vm_page_dirty(page);
215 }
216 
217 static inline void
218 mark_page_accessed(struct vm_page *page)
219 {
220 	vm_page_reference(page);
221 }
222 
223 static inline void
224 get_page(struct vm_page *page)
225 {
226 	vm_page_wire(page);
227 }
228 
229 extern long
230 get_user_pages(unsigned long start, unsigned long nr_pages,
231     int gup_flags, struct page **,
232     struct vm_area_struct **);
233 
234 extern int
235 __get_user_pages_fast(unsigned long start, int nr_pages, int write,
236     struct page **);
237 
238 extern long
239 get_user_pages_remote(struct task_struct *, struct mm_struct *,
240     unsigned long start, unsigned long nr_pages,
241     int gup_flags, struct page **,
242     struct vm_area_struct **);
243 
244 static inline void
245 put_page(struct vm_page *page)
246 {
247 	vm_page_unwire(page, PQ_ACTIVE);
248 }
249 
250 #define	copy_highpage(to, from) pmap_copy_page(from, to)
251 
252 static inline pgprot_t
253 vm_get_page_prot(unsigned long vm_flags)
254 {
255 	return (vm_flags & VM_PROT_ALL);
256 }
257 
258 static inline vm_page_t
259 vmalloc_to_page(const void *addr)
260 {
261 	vm_paddr_t paddr;
262 
263 	paddr = pmap_kextract((vm_offset_t)addr);
264 	return (PHYS_TO_VM_PAGE(paddr));
265 }
266 
267 extern int is_vmalloc_addr(const void *addr);
268 void si_meminfo(struct sysinfo *si);
269 
270 #endif					/* _LINUX_MM_H_ */
271