vm_util.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <sys/mman.h>
  5. #include <err.h>
  6. #include <strings.h> /* ffsl() */
  7. #include <unistd.h> /* _SC_PAGESIZE */
  8. #define BIT_ULL(nr) (1ULL << (nr))
  9. #define PM_SOFT_DIRTY BIT_ULL(55)
  10. #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
  11. #define PM_UFFD_WP BIT_ULL(57)
  12. #define PM_FILE BIT_ULL(61)
  13. #define PM_SWAP BIT_ULL(62)
  14. #define PM_PRESENT BIT_ULL(63)
  15. extern unsigned int __page_size;
  16. extern unsigned int __page_shift;
  17. static inline unsigned int psize(void)
  18. {
  19. if (!__page_size)
  20. __page_size = sysconf(_SC_PAGESIZE);
  21. return __page_size;
  22. }
  23. static inline unsigned int pshift(void)
  24. {
  25. if (!__page_shift)
  26. __page_shift = (ffsl(psize()) - 1);
  27. return __page_shift;
  28. }
  29. uint64_t pagemap_get_entry(int fd, char *start);
  30. bool pagemap_is_softdirty(int fd, char *start);
  31. bool pagemap_is_swapped(int fd, char *start);
  32. bool pagemap_is_populated(int fd, char *start);
  33. unsigned long pagemap_get_pfn(int fd, char *start);
  34. void clear_softdirty(void);
  35. bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
  36. uint64_t read_pmd_pagesize(void);
  37. unsigned long rss_anon(void);
  38. bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
  39. bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
  40. bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
  41. int64_t allocate_transhuge(void *ptr, int pagemap_fd);
  42. unsigned long default_huge_page_size(void);
  43. int detect_hugetlb_page_sizes(size_t sizes[], int max);
  44. int uffd_register(int uffd, void *addr, uint64_t len,
  45. bool miss, bool wp, bool minor);
  46. int uffd_unregister(int uffd, void *addr, uint64_t len);
  47. int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
  48. bool miss, bool wp, bool minor, uint64_t *ioctls);
  49. unsigned long get_free_hugepages(void);
  50. bool softdirty_supported(void);
  51. /*
  52. * On ppc64 this will only work with radix 2M hugepage size
  53. */
  54. #define HPAGE_SHIFT 21
  55. #define HPAGE_SIZE (1 << HPAGE_SHIFT)
  56. #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)
  57. #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))