custom.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #pragma once
  3. /*
  4. * Contains declarations that exist in the kernel which have been CUSTOMISED for
  5. * testing purposes to faciliate userland VMA testing.
  6. */
  7. #ifdef CONFIG_MMU
  8. extern unsigned long mmap_min_addr;
  9. extern unsigned long dac_mmap_min_addr;
  10. #else
  11. #define mmap_min_addr 0UL
  12. #define dac_mmap_min_addr 0UL
  13. #endif
  14. #define VM_WARN_ON(_expr) (WARN_ON(_expr))
  15. #define VM_WARN_ON_ONCE(_expr) (WARN_ON_ONCE(_expr))
  16. #define VM_WARN_ON_VMG(_expr, _vmg) (WARN_ON(_expr))
  17. #define VM_BUG_ON(_expr) (BUG_ON(_expr))
  18. #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr))
  19. /* We hardcode this for now. */
  20. #define sysctl_max_map_count 0x1000000UL
  21. #define TASK_SIZE ((1ul << 47)-PAGE_SIZE)
  22. /*
  23. * The shared stubs do not implement this, it amounts to an fprintf(STDERR,...)
  24. * either way :)
  25. */
  26. #define pr_warn_once pr_err
  27. #define pgtable_supports_soft_dirty() 1
  28. struct anon_vma {
  29. struct anon_vma *root;
  30. struct rb_root_cached rb_root;
  31. /* Test fields. */
  32. bool was_cloned;
  33. bool was_unlinked;
  34. };
  35. static inline void unlink_anon_vmas(struct vm_area_struct *vma)
  36. {
  37. /* For testing purposes, indicate that the anon_vma was unlinked. */
  38. vma->anon_vma->was_unlinked = true;
  39. }
  40. static inline void vma_start_write(struct vm_area_struct *vma)
  41. {
  42. /* Used to indicate to tests that a write operation has begun. */
  43. vma->vm_lock_seq++;
  44. }
  45. static inline __must_check
  46. int vma_start_write_killable(struct vm_area_struct *vma)
  47. {
  48. /* Used to indicate to tests that a write operation has begun. */
  49. vma->vm_lock_seq++;
  50. return 0;
  51. }
  52. static inline int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src,
  53. enum vma_operation operation)
  54. {
  55. /* For testing purposes. We indicate that an anon_vma has been cloned. */
  56. if (src->anon_vma != NULL) {
  57. dst->anon_vma = src->anon_vma;
  58. dst->anon_vma->was_cloned = true;
  59. }
  60. return 0;
  61. }
  62. static inline int __anon_vma_prepare(struct vm_area_struct *vma)
  63. {
  64. struct anon_vma *anon_vma = calloc(1, sizeof(struct anon_vma));
  65. if (!anon_vma)
  66. return -ENOMEM;
  67. anon_vma->root = anon_vma;
  68. vma->anon_vma = anon_vma;
  69. return 0;
  70. }
  71. static inline int anon_vma_prepare(struct vm_area_struct *vma)
  72. {
  73. if (likely(vma->anon_vma))
  74. return 0;
  75. return __anon_vma_prepare(vma);
  76. }
  77. static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt)
  78. {
  79. if (reset_refcnt)
  80. refcount_set(&vma->vm_refcnt, 0);
  81. }
  82. static inline vma_flags_t __mk_vma_flags(size_t count, const vma_flag_t *bits)
  83. {
  84. vma_flags_t flags;
  85. int i;
  86. /*
  87. * For testing purposes: allow invalid bit specification so we can
  88. * easily test.
  89. */
  90. vma_flags_clear_all(&flags);
  91. for (i = 0; i < count; i++)
  92. if (bits[i] < NUM_VMA_FLAG_BITS)
  93. vma_flag_set(&flags, bits[i]);
  94. return flags;
  95. }