1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Userfaultfd tests common header
4  *
5  * Copyright (C) 2015-2023  Red Hat, Inc.
6  */
7 #ifndef __UFFD_COMMON_H__
8 #define __UFFD_COMMON_H__
9 
10 #define _GNU_SOURCE
11 #define __SANE_USERSPACE_TYPES__ // Use ll64
12 #include <stdio.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <poll.h>
22 #include <string.h>
23 #include <linux/mman.h>
24 #include <sys/mman.h>
25 #include <sys/syscall.h>
26 #include <sys/ioctl.h>
27 #include <sys/wait.h>
28 #include <pthread.h>
29 #include <linux/userfaultfd.h>
30 #include <setjmp.h>
31 #include <stdbool.h>
32 #include <assert.h>
33 #include <inttypes.h>
34 #include <stdint.h>
35 #include <sys/random.h>
36 #include <stdatomic.h>
37 
38 #include "../kselftest.h"
39 #include "vm_util.h"
40 
41 #define UFFD_FLAGS	(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY)
42 
43 #define _err(fmt, ...)						\
44 	do {							\
45 		int ret = errno;				\
46 		fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__);	\
47 		fprintf(stderr, " (errno=%d, @%s:%d)\n",	\
48 			ret, __FILE__, __LINE__);		\
49 	} while (0)
50 
51 #define errexit(exitcode, fmt, ...)		\
52 	do {					\
53 		_err(fmt, ##__VA_ARGS__);	\
54 		exit(exitcode);			\
55 	} while (0)
56 
57 #define err(fmt, ...) errexit(1, fmt, ##__VA_ARGS__)
58 
59 /* pthread_mutex_t starts at page offset 0 */
60 #define area_mutex(___area, ___nr)					\
61 	((pthread_mutex_t *) ((___area) + (___nr)*page_size))
62 /*
63  * count is placed in the page after pthread_mutex_t naturally aligned
64  * to avoid non alignment faults on non-x86 archs.
65  */
66 #define area_count(___area, ___nr)					\
67 	((volatile unsigned long long *) ((unsigned long)		\
68 				 ((___area) + (___nr)*page_size +	\
69 				  sizeof(pthread_mutex_t) +		\
70 				  sizeof(unsigned long long) - 1) &	\
71 				 ~(unsigned long)(sizeof(unsigned long long) \
72 						  -  1)))
73 
74 /* Userfaultfd test statistics */
75 struct uffd_args {
76 	int cpu;
77 	/* Whether apply wr-protects when installing pages */
78 	bool apply_wp;
79 	unsigned long missing_faults;
80 	unsigned long wp_faults;
81 	unsigned long minor_faults;
82 
83 	/* A custom fault handler; defaults to uffd_handle_page_fault. */
84 	void (*handle_fault)(struct uffd_msg *msg, struct uffd_args *args);
85 };
86 
87 struct uffd_test_ops {
88 	int (*allocate_area)(void **alloc_area, bool is_src);
89 	void (*release_pages)(char *rel_area);
90 	void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
91 	void (*check_pmd_mapping)(void *p, int expect_nr_hpages);
92 };
93 typedef struct uffd_test_ops uffd_test_ops_t;
94 
95 extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
96 extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;
97 extern int uffd, uffd_flags, finished, *pipefd, test_type;
98 extern bool map_shared;
99 extern bool test_uffdio_wp;
100 extern unsigned long long *count_verify;
101 extern volatile bool test_uffdio_copy_eexist;
102 extern atomic_bool ready_for_fork;
103 
104 extern uffd_test_ops_t anon_uffd_test_ops;
105 extern uffd_test_ops_t shmem_uffd_test_ops;
106 extern uffd_test_ops_t hugetlb_uffd_test_ops;
107 extern uffd_test_ops_t *uffd_test_ops;
108 
109 void uffd_stats_report(struct uffd_args *args, int n_cpus);
110 int uffd_test_ctx_init(uint64_t features, const char **errmsg);
111 int userfaultfd_open(uint64_t *features);
112 int uffd_read_msg(int ufd, struct uffd_msg *msg);
113 void wp_range(int ufd, __u64 start, __u64 len, bool wp);
114 void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_args *args);
115 int __copy_page(int ufd, unsigned long offset, bool retry, bool wp);
116 int copy_page(int ufd, unsigned long offset, bool wp);
117 void *uffd_poll_thread(void *arg);
118 
119 int uffd_open_dev(unsigned int flags);
120 int uffd_open_sys(unsigned int flags);
121 int uffd_open(unsigned int flags);
122 int uffd_get_features(uint64_t *features);
123 
124 #define TEST_ANON	1
125 #define TEST_HUGETLB	2
126 #define TEST_SHMEM	3
127 
128 #endif
129