xref: /freebsd/sys/compat/linuxkpi/common/include/linux/mm.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #ifndef	_LINUXKPI_LINUX_MM_H_
32 #define	_LINUXKPI_LINUX_MM_H_
33 
34 #include <linux/spinlock.h>
35 #include <linux/gfp.h>
36 #include <linux/kernel.h>
37 #include <linux/mm_types.h>
38 #include <linux/pfn.h>
39 #include <linux/list.h>
40 #include <linux/mmap_lock.h>
41 #include <linux/shrinker.h>
42 #include <linux/page.h>
43 
44 #include <asm/pgtable.h>
45 
46 #define	PAGE_ALIGN(x)	ALIGN(x, PAGE_SIZE)
47 
48 /*
49  * Make sure our LinuxKPI defined virtual memory flags don't conflict
50  * with the ones defined by FreeBSD:
51  */
52 CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0);
53 
54 #define	VM_READ			VM_PROT_READ
55 #define	VM_WRITE		VM_PROT_WRITE
56 #define	VM_EXEC			VM_PROT_EXECUTE
57 
58 #define	VM_PFNINTERNAL		(1 << 8)	/* FreeBSD private flag to vm_insert_pfn() */
59 #define	VM_MIXEDMAP		(1 << 9)
60 #define	VM_NORESERVE		(1 << 10)
61 #define	VM_PFNMAP		(1 << 11)
62 #define	VM_IO			(1 << 12)
63 #define	VM_MAYWRITE		(1 << 13)
64 #define	VM_DONTCOPY		(1 << 14)
65 #define	VM_DONTEXPAND		(1 << 15)
66 #define	VM_DONTDUMP		(1 << 16)
67 #define	VM_SHARED		(1 << 17)
68 
69 #define	VMA_MAX_PREFAULT_RECORD	1
70 
71 #define	FOLL_WRITE		(1 << 0)
72 #define	FOLL_FORCE		(1 << 1)
73 
74 #define	VM_FAULT_OOM		(1 << 0)
75 #define	VM_FAULT_SIGBUS		(1 << 1)
76 #define	VM_FAULT_MAJOR		(1 << 2)
77 #define	VM_FAULT_WRITE		(1 << 3)
78 #define	VM_FAULT_HWPOISON	(1 << 4)
79 #define	VM_FAULT_HWPOISON_LARGE	(1 << 5)
80 #define	VM_FAULT_SIGSEGV	(1 << 6)
81 #define	VM_FAULT_NOPAGE		(1 << 7)
82 #define	VM_FAULT_LOCKED		(1 << 8)
83 #define	VM_FAULT_RETRY		(1 << 9)
84 #define	VM_FAULT_FALLBACK	(1 << 10)
85 
86 #define	VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \
87 	VM_FAULT_HWPOISON |VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
88 
89 #define	FAULT_FLAG_WRITE	(1 << 0)
90 #define	FAULT_FLAG_MKWRITE	(1 << 1)
91 #define	FAULT_FLAG_ALLOW_RETRY	(1 << 2)
92 #define	FAULT_FLAG_RETRY_NOWAIT	(1 << 3)
93 #define	FAULT_FLAG_KILLABLE	(1 << 4)
94 #define	FAULT_FLAG_TRIED	(1 << 5)
95 #define	FAULT_FLAG_USER		(1 << 6)
96 #define	FAULT_FLAG_REMOTE	(1 << 7)
97 #define	FAULT_FLAG_INSTRUCTION	(1 << 8)
98 
99 #define fault_flag_allow_retry_first(flags) \
100 	(((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY)
101 
102 typedef int (*pte_fn_t)(linux_pte_t *, unsigned long addr, void *data);
103 
104 struct vm_area_struct {
105 	vm_offset_t vm_start;
106 	vm_offset_t vm_end;
107 	vm_offset_t vm_pgoff;
108 	pgprot_t vm_page_prot;
109 	unsigned long vm_flags;
110 	struct mm_struct *vm_mm;
111 	void   *vm_private_data;
112 	const struct vm_operations_struct *vm_ops;
113 	struct linux_file *vm_file;
114 
115 	/* internal operation */
116 	vm_paddr_t vm_pfn;		/* PFN for memory map */
117 	vm_size_t vm_len;		/* length for memory map */
118 	vm_pindex_t vm_pfn_first;
119 	int	vm_pfn_count;
120 	int    *vm_pfn_pcount;
121 	vm_object_t vm_obj;
122 	vm_map_t vm_cached_map;
123 	TAILQ_ENTRY(vm_area_struct) vm_entry;
124 };
125 
126 struct vm_fault {
127 	unsigned int flags;
128 	pgoff_t	pgoff;
129 	union {
130 		/* user-space address */
131 		void *virtual_address;	/* < 4.11 */
132 		unsigned long address;	/* >= 4.11 */
133 	};
134 	struct page *page;
135 	struct vm_area_struct *vma;
136 };
137 
138 struct vm_operations_struct {
139 	void    (*open) (struct vm_area_struct *);
140 	void    (*close) (struct vm_area_struct *);
141 	int     (*fault) (struct vm_fault *);
142 	int	(*access) (struct vm_area_struct *, unsigned long, void *, int, int);
143 };
144 
145 struct sysinfo {
146 	uint64_t totalram;	/* Total usable main memory size */
147 	uint64_t freeram;	/* Available memory size */
148 	uint64_t totalhigh;	/* Total high memory size */
149 	uint64_t freehigh;	/* Available high memory size */
150 	uint32_t mem_unit;	/* Memory unit size in bytes */
151 };
152 
153 static inline struct page *
154 virt_to_head_page(const void *p)
155 {
156 
157 	return (virt_to_page(p));
158 }
159 
160 /*
161  * Compute log2 of the power of two rounded up count of pages
162  * needed for size bytes.
163  */
164 static inline int
165 get_order(unsigned long size)
166 {
167 	int order;
168 
169 	size = (size - 1) >> PAGE_SHIFT;
170 	order = 0;
171 	while (size) {
172 		order++;
173 		size >>= 1;
174 	}
175 	return (order);
176 }
177 
178 static inline void *
179 lowmem_page_address(struct page *page)
180 {
181 	return (page_address(page));
182 }
183 
184 /*
185  * This only works via memory map operations.
186  */
187 static inline int
188 io_remap_pfn_range(struct vm_area_struct *vma,
189     unsigned long addr, unsigned long pfn, unsigned long size,
190     vm_memattr_t prot)
191 {
192 	vma->vm_page_prot = prot;
193 	vma->vm_pfn = pfn;
194 	vma->vm_len = size;
195 
196 	return (0);
197 }
198 
199 vm_fault_t
200 lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr,
201     unsigned long pfn, pgprot_t prot);
202 
203 static inline vm_fault_t
204 vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
205     unsigned long pfn, pgprot_t prot)
206 {
207 	vm_fault_t ret;
208 
209 	VM_OBJECT_WLOCK(vma->vm_obj);
210 	ret = lkpi_vmf_insert_pfn_prot_locked(vma, addr, pfn, prot);
211 	VM_OBJECT_WUNLOCK(vma->vm_obj);
212 
213 	return (ret);
214 }
215 #define	vmf_insert_pfn_prot(...)	\
216 	_Static_assert(false,		\
217 "This function is always called in a loop. Consider using the locked version")
218 
219 static inline int
220 apply_to_page_range(struct mm_struct *mm, unsigned long address,
221     unsigned long size, pte_fn_t fn, void *data)
222 {
223 	return (-ENOTSUP);
224 }
225 
226 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
227     unsigned long size);
228 
229 int lkpi_remap_pfn_range(struct vm_area_struct *vma,
230     unsigned long start_addr, unsigned long start_pfn, unsigned long size,
231     pgprot_t prot);
232 
233 static inline int
234 remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
235     unsigned long pfn, unsigned long size, pgprot_t prot)
236 {
237 	return (lkpi_remap_pfn_range(vma, addr, pfn, size, prot));
238 }
239 
240 static inline unsigned long
241 vma_pages(struct vm_area_struct *vma)
242 {
243 	return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
244 }
245 
246 #define	offset_in_page(off)	((unsigned long)(off) & (PAGE_SIZE - 1))
247 
248 static inline void
249 set_page_dirty(struct page *page)
250 {
251 	vm_page_dirty(page);
252 }
253 
254 static inline void
255 mark_page_accessed(struct page *page)
256 {
257 	vm_page_reference(page);
258 }
259 
260 static inline void
261 get_page(struct page *page)
262 {
263 	vm_page_wire(page);
264 }
265 
266 extern long
267 get_user_pages(unsigned long start, unsigned long nr_pages,
268     unsigned int gup_flags, struct page **,
269     struct vm_area_struct **);
270 
271 static inline long
272 pin_user_pages(unsigned long start, unsigned long nr_pages,
273     unsigned int gup_flags, struct page **pages,
274     struct vm_area_struct **vmas)
275 {
276 	return get_user_pages(start, nr_pages, gup_flags, pages, vmas);
277 }
278 
279 extern int
280 __get_user_pages_fast(unsigned long start, int nr_pages, int write,
281     struct page **);
282 
283 static inline int
284 pin_user_pages_fast(unsigned long start, int nr_pages,
285     unsigned int gup_flags, struct page **pages)
286 {
287 	return __get_user_pages_fast(
288 	    start, nr_pages, !!(gup_flags & FOLL_WRITE), pages);
289 }
290 
291 extern long
292 get_user_pages_remote(struct task_struct *, struct mm_struct *,
293     unsigned long start, unsigned long nr_pages,
294     unsigned int gup_flags, struct page **,
295     struct vm_area_struct **);
296 
297 static inline long
298 pin_user_pages_remote(struct task_struct *task, struct mm_struct *mm,
299     unsigned long start, unsigned long nr_pages,
300     unsigned int gup_flags, struct page **pages,
301     struct vm_area_struct **vmas)
302 {
303 	return get_user_pages_remote(
304 	    task, mm, start, nr_pages, gup_flags, pages, vmas);
305 }
306 
307 static inline void
308 put_page(struct page *page)
309 {
310 	vm_page_unwire(page, PQ_ACTIVE);
311 }
312 
313 #define	unpin_user_page(page) put_page(page)
314 #define	unpin_user_pages(pages, npages) release_pages(pages, npages)
315 
316 #define	copy_highpage(to, from) pmap_copy_page(from, to)
317 
318 static inline pgprot_t
319 vm_get_page_prot(unsigned long vm_flags)
320 {
321 	return (vm_flags & VM_PROT_ALL);
322 }
323 
324 static inline struct page *
325 vmalloc_to_page(const void *addr)
326 {
327 	vm_paddr_t paddr;
328 
329 	paddr = pmap_kextract((vm_offset_t)addr);
330 	return (PHYS_TO_VM_PAGE(paddr));
331 }
332 
333 static inline int
334 trylock_page(struct page *page)
335 {
336 	return (vm_page_trylock(page));
337 }
338 
339 static inline void
340 unlock_page(struct page *page)
341 {
342 
343 	vm_page_unlock(page);
344 }
345 
346 extern int is_vmalloc_addr(const void *addr);
347 void si_meminfo(struct sysinfo *si);
348 
349 static inline unsigned long
350 totalram_pages(void)
351 {
352 	return ((unsigned long)physmem);
353 }
354 
355 #define	unmap_mapping_range(...)	lkpi_unmap_mapping_range(__VA_ARGS__)
356 void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused,
357     loff_t const holelen, int even_cows __unused);
358 
359 #define PAGE_ALIGNED(p)	__is_aligned(p, PAGE_SIZE)
360 
361 void vma_set_file(struct vm_area_struct *vma, struct linux_file *file);
362 
363 static inline void
364 might_alloc(gfp_t gfp_mask __unused)
365 {
366 }
367 
368 #define	is_cow_mapping(flags)	(false)
369 
370 #endif					/* _LINUXKPI_LINUX_MM_H_ */
371