shared.h 3.4 KB

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