1*6aacab30SLorenzo Stoakes // SPDX-License-Identifier: GPL-2.0-or-later
2*6aacab30SLorenzo Stoakes
3*6aacab30SLorenzo Stoakes #pragma once
4*6aacab30SLorenzo Stoakes
5*6aacab30SLorenzo Stoakes #include <stdbool.h>
6*6aacab30SLorenzo Stoakes #include <stdio.h>
7*6aacab30SLorenzo Stoakes #include <stdlib.h>
8*6aacab30SLorenzo Stoakes
9*6aacab30SLorenzo Stoakes #include "generated/bit-length.h"
10*6aacab30SLorenzo Stoakes #include "maple-shared.h"
11*6aacab30SLorenzo Stoakes #include "vma_internal.h"
12*6aacab30SLorenzo Stoakes #include "../../../mm/vma.h"
13*6aacab30SLorenzo Stoakes
14*6aacab30SLorenzo Stoakes /* Simple test runner. Assumes local num_[fail, tests] counters. */
15*6aacab30SLorenzo Stoakes #define TEST(name) \
16*6aacab30SLorenzo Stoakes do { \
17*6aacab30SLorenzo Stoakes (*num_tests)++; \
18*6aacab30SLorenzo Stoakes if (!test_##name()) { \
19*6aacab30SLorenzo Stoakes (*num_fail)++; \
20*6aacab30SLorenzo Stoakes fprintf(stderr, "Test " #name " FAILED\n"); \
21*6aacab30SLorenzo Stoakes } \
22*6aacab30SLorenzo Stoakes } while (0)
23*6aacab30SLorenzo Stoakes
24*6aacab30SLorenzo Stoakes #define ASSERT_TRUE(_expr) \
25*6aacab30SLorenzo Stoakes do { \
26*6aacab30SLorenzo Stoakes if (!(_expr)) { \
27*6aacab30SLorenzo Stoakes fprintf(stderr, \
28*6aacab30SLorenzo Stoakes "Assert FAILED at %s:%d:%s(): %s is FALSE.\n", \
29*6aacab30SLorenzo Stoakes __FILE__, __LINE__, __FUNCTION__, #_expr); \
30*6aacab30SLorenzo Stoakes return false; \
31*6aacab30SLorenzo Stoakes } \
32*6aacab30SLorenzo Stoakes } while (0)
33*6aacab30SLorenzo Stoakes
34*6aacab30SLorenzo Stoakes #define ASSERT_FALSE(_expr) ASSERT_TRUE(!(_expr))
35*6aacab30SLorenzo Stoakes #define ASSERT_EQ(_val1, _val2) ASSERT_TRUE((_val1) == (_val2))
36*6aacab30SLorenzo Stoakes #define ASSERT_NE(_val1, _val2) ASSERT_TRUE((_val1) != (_val2))
37*6aacab30SLorenzo Stoakes
38*6aacab30SLorenzo Stoakes #define IS_SET(_val, _flags) ((_val & _flags) == _flags)
39*6aacab30SLorenzo Stoakes
40*6aacab30SLorenzo Stoakes extern bool fail_prealloc;
41*6aacab30SLorenzo Stoakes
42*6aacab30SLorenzo Stoakes /* Override vma_iter_prealloc() so we can choose to fail it. */
43*6aacab30SLorenzo Stoakes #define vma_iter_prealloc(vmi, vma) \
44*6aacab30SLorenzo Stoakes (fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
45*6aacab30SLorenzo Stoakes
46*6aacab30SLorenzo Stoakes #define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
47*6aacab30SLorenzo Stoakes
48*6aacab30SLorenzo Stoakes extern unsigned long mmap_min_addr;
49*6aacab30SLorenzo Stoakes extern unsigned long dac_mmap_min_addr;
50*6aacab30SLorenzo Stoakes extern unsigned long stack_guard_gap;
51*6aacab30SLorenzo Stoakes
52*6aacab30SLorenzo Stoakes extern const struct vm_operations_struct vma_dummy_vm_ops;
53*6aacab30SLorenzo Stoakes extern struct anon_vma dummy_anon_vma;
54*6aacab30SLorenzo Stoakes extern struct task_struct __current;
55*6aacab30SLorenzo Stoakes
56*6aacab30SLorenzo Stoakes /*
57*6aacab30SLorenzo Stoakes * Helper function which provides a wrapper around a merge existing VMA
58*6aacab30SLorenzo Stoakes * operation.
59*6aacab30SLorenzo Stoakes *
60*6aacab30SLorenzo Stoakes * Declared in main.c as uses static VMA function.
61*6aacab30SLorenzo Stoakes */
62*6aacab30SLorenzo Stoakes struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg);
63*6aacab30SLorenzo Stoakes
64*6aacab30SLorenzo Stoakes /*
65*6aacab30SLorenzo Stoakes * Helper function to allocate a VMA and link it to the tree.
66*6aacab30SLorenzo Stoakes *
67*6aacab30SLorenzo Stoakes * Declared in main.c as uses static VMA function.
68*6aacab30SLorenzo Stoakes */
69*6aacab30SLorenzo Stoakes int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma);
70*6aacab30SLorenzo Stoakes
71*6aacab30SLorenzo Stoakes /* Helper function providing a dummy vm_ops->close() method.*/
dummy_close(struct vm_area_struct *)72*6aacab30SLorenzo Stoakes static inline void dummy_close(struct vm_area_struct *)
73*6aacab30SLorenzo Stoakes {
74*6aacab30SLorenzo Stoakes }
75*6aacab30SLorenzo Stoakes
76*6aacab30SLorenzo Stoakes /* Helper function to simply allocate a VMA. */
77*6aacab30SLorenzo Stoakes struct vm_area_struct *alloc_vma(struct mm_struct *mm,
78*6aacab30SLorenzo Stoakes unsigned long start, unsigned long end,
79*6aacab30SLorenzo Stoakes pgoff_t pgoff, vm_flags_t vm_flags);
80*6aacab30SLorenzo Stoakes
81*6aacab30SLorenzo Stoakes /* Helper function to detach and free a VMA. */
82*6aacab30SLorenzo Stoakes void detach_free_vma(struct vm_area_struct *vma);
83*6aacab30SLorenzo Stoakes
84*6aacab30SLorenzo Stoakes /* Helper function to allocate a VMA and link it to the tree. */
85*6aacab30SLorenzo Stoakes struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
86*6aacab30SLorenzo Stoakes unsigned long start, unsigned long end,
87*6aacab30SLorenzo Stoakes pgoff_t pgoff, vm_flags_t vm_flags);
88*6aacab30SLorenzo Stoakes
89*6aacab30SLorenzo Stoakes /*
90*6aacab30SLorenzo Stoakes * Helper function to reset the dummy anon_vma to indicate it has not been
91*6aacab30SLorenzo Stoakes * duplicated.
92*6aacab30SLorenzo Stoakes */
93*6aacab30SLorenzo Stoakes void reset_dummy_anon_vma(void);
94*6aacab30SLorenzo Stoakes
95*6aacab30SLorenzo Stoakes /*
96*6aacab30SLorenzo Stoakes * Helper function to remove all VMAs and destroy the maple tree associated with
97*6aacab30SLorenzo Stoakes * a virtual address space. Returns a count of VMAs in the tree.
98*6aacab30SLorenzo Stoakes */
99*6aacab30SLorenzo Stoakes int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi);
100*6aacab30SLorenzo Stoakes
101*6aacab30SLorenzo Stoakes /* Helper function to determine if VMA has had vma_start_write() performed. */
102*6aacab30SLorenzo Stoakes bool vma_write_started(struct vm_area_struct *vma);
103*6aacab30SLorenzo Stoakes
104*6aacab30SLorenzo Stoakes void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
105*6aacab30SLorenzo Stoakes struct anon_vma_chain *avc, struct anon_vma *anon_vma);
106*6aacab30SLorenzo Stoakes
107*6aacab30SLorenzo Stoakes /* Provide a simple dummy VMA/anon_vma dummy setup for testing. */
108*6aacab30SLorenzo Stoakes void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
109*6aacab30SLorenzo Stoakes struct anon_vma_chain *avc);
110*6aacab30SLorenzo Stoakes
111*6aacab30SLorenzo Stoakes /* Helper function to specify a VMA's range. */
112*6aacab30SLorenzo Stoakes void vma_set_range(struct vm_area_struct *vma,
113*6aacab30SLorenzo Stoakes unsigned long start, unsigned long end,
114*6aacab30SLorenzo Stoakes pgoff_t pgoff);
115