162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#include <stdint.h>
362306a36Sopenharmony_ci#include <stdbool.h>
462306a36Sopenharmony_ci#include <sys/mman.h>
562306a36Sopenharmony_ci#include <err.h>
662306a36Sopenharmony_ci#include <string.h> /* ffsl() */
762306a36Sopenharmony_ci#include <unistd.h> /* _SC_PAGESIZE */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#define BIT_ULL(nr)                   (1ULL << (nr))
1062306a36Sopenharmony_ci#define PM_SOFT_DIRTY                 BIT_ULL(55)
1162306a36Sopenharmony_ci#define PM_MMAP_EXCLUSIVE             BIT_ULL(56)
1262306a36Sopenharmony_ci#define PM_UFFD_WP                    BIT_ULL(57)
1362306a36Sopenharmony_ci#define PM_FILE                       BIT_ULL(61)
1462306a36Sopenharmony_ci#define PM_SWAP                       BIT_ULL(62)
1562306a36Sopenharmony_ci#define PM_PRESENT                    BIT_ULL(63)
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciextern unsigned int __page_size;
1862306a36Sopenharmony_ciextern unsigned int __page_shift;
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic inline unsigned int psize(void)
2162306a36Sopenharmony_ci{
2262306a36Sopenharmony_ci	if (!__page_size)
2362306a36Sopenharmony_ci		__page_size = sysconf(_SC_PAGESIZE);
2462306a36Sopenharmony_ci	return __page_size;
2562306a36Sopenharmony_ci}
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic inline unsigned int pshift(void)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	if (!__page_shift)
3062306a36Sopenharmony_ci		__page_shift = (ffsl(psize()) - 1);
3162306a36Sopenharmony_ci	return __page_shift;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciuint64_t pagemap_get_entry(int fd, char *start);
3562306a36Sopenharmony_cibool pagemap_is_softdirty(int fd, char *start);
3662306a36Sopenharmony_cibool pagemap_is_swapped(int fd, char *start);
3762306a36Sopenharmony_cibool pagemap_is_populated(int fd, char *start);
3862306a36Sopenharmony_ciunsigned long pagemap_get_pfn(int fd, char *start);
3962306a36Sopenharmony_civoid clear_softdirty(void);
4062306a36Sopenharmony_cibool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
4162306a36Sopenharmony_ciuint64_t read_pmd_pagesize(void);
4262306a36Sopenharmony_cibool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
4362306a36Sopenharmony_cibool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
4462306a36Sopenharmony_cibool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
4562306a36Sopenharmony_ciint64_t allocate_transhuge(void *ptr, int pagemap_fd);
4662306a36Sopenharmony_ciunsigned long default_huge_page_size(void);
4762306a36Sopenharmony_ciint detect_hugetlb_page_sizes(size_t sizes[], int max);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciint uffd_register(int uffd, void *addr, uint64_t len,
5062306a36Sopenharmony_ci		  bool miss, bool wp, bool minor);
5162306a36Sopenharmony_ciint uffd_unregister(int uffd, void *addr, uint64_t len);
5262306a36Sopenharmony_ciint uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
5362306a36Sopenharmony_ci			      bool miss, bool wp, bool minor, uint64_t *ioctls);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/*
5662306a36Sopenharmony_ci * On ppc64 this will only work with radix 2M hugepage size
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_ci#define HPAGE_SHIFT 21
5962306a36Sopenharmony_ci#define HPAGE_SIZE (1 << HPAGE_SHIFT)
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
6262306a36Sopenharmony_ci#define PAGEMAP_PFN(ent)	((ent) & ((1ull << 55) - 1))
63