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