1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <linux/userfaultfd.h>
24#include <poll.h>
25#include <unistd.h>
26#include <sys/ioctl.h>
27#include <sys/syscall.h>
28#include <fcntl.h>
29#include <sys/mman.h>
30#include <pthread.h>
31#include <signal.h>
32#include <errno.h>
33
34#include "tm.h"
35
36
37#define UF_MEM_SIZE 655360
38
39
40static char *uf_mem;
41static size_t uf_mem_offset = 0;
42
43
44
45
46
47
48
49
50static char backing_mem[UF_MEM_SIZE];
51
52static size_t pagesize;
53
54
55
56
57
58
59
60void *get_uf_mem(size_t size, void *backing_data)
61{
62 void *ret;
63
64 if (uf_mem_offset + size > UF_MEM_SIZE) {
65 fprintf(stderr, "Requesting more uf_mem than expected!\n");
66 exit(EXIT_FAILURE);
67 }
68
69 ret = &uf_mem[uf_mem_offset];
70
71
72 if (backing_data != NULL)
73 memcpy(&backing_mem[uf_mem_offset], backing_data, size);
74
75
76 uf_mem_offset += size;
77
78 uf_mem_offset = (uf_mem_offset + pagesize - 1) & ~(pagesize - 1);
79
80 return ret;
81}
82
83void *fault_handler_thread(void *arg)
84{
85 struct uffd_msg msg;
86 long uffd;
87 struct uffdio_copy uffdio_copy;
88 struct pollfd pollfd;
89 ssize_t nread, offset;
90
91 uffd = (long) arg;
92
93 for (;;) {
94 pollfd.fd = uffd;
95 pollfd.events = POLLIN;
96 if (poll(&pollfd, 1, -1) == -1) {
97 perror("poll() failed");
98 exit(EXIT_FAILURE);
99 }
100
101 nread = read(uffd, &msg, sizeof(msg));
102 if (nread == 0) {
103 fprintf(stderr, "read(): EOF on userfaultfd\n");
104 exit(EXIT_FAILURE);
105 }
106
107 if (nread == -1) {
108 perror("read() failed");
109 exit(EXIT_FAILURE);
110 }
111
112
113 if (msg.event != UFFD_EVENT_PAGEFAULT) {
114 fprintf(stderr, "Unexpected event on userfaultfd\n");
115 exit(EXIT_FAILURE);
116 }
117
118
119
120
121
122 uffdio_copy.dst = msg.arg.pagefault.address & ~(pagesize-1);
123
124 offset = (char *) uffdio_copy.dst - uf_mem;
125 uffdio_copy.src = (unsigned long) &backing_mem[offset];
126
127 uffdio_copy.len = pagesize;
128 uffdio_copy.mode = 0;
129 uffdio_copy.copy = 0;
130 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1) {
131 perror("ioctl-UFFDIO_COPY failed");
132 exit(EXIT_FAILURE);
133 }
134 }
135}
136
137void setup_uf_mem(void)
138{
139 long uffd;
140 pthread_t thr;
141 struct uffdio_api uffdio_api;
142 struct uffdio_register uffdio_register;
143 int ret;
144
145 pagesize = sysconf(_SC_PAGE_SIZE);
146
147
148 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
149 if (uffd == -1) {
150 perror("userfaultfd() failed");
151 exit(EXIT_FAILURE);
152 }
153 uffdio_api.api = UFFD_API;
154 uffdio_api.features = 0;
155 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) {
156 perror("ioctl-UFFDIO_API failed");
157 exit(EXIT_FAILURE);
158 }
159
160
161
162
163
164
165 uf_mem = mmap(NULL, UF_MEM_SIZE, PROT_READ | PROT_WRITE,
166 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
167 if (uf_mem == MAP_FAILED) {
168 perror("mmap() failed");
169 exit(EXIT_FAILURE);
170 }
171
172
173
174
175
176
177 uffdio_register.range.start = (unsigned long) uf_mem;
178 uffdio_register.range.len = UF_MEM_SIZE;
179 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
180 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) {
181 perror("ioctl-UFFDIO_REGISTER");
182 exit(EXIT_FAILURE);
183 }
184
185
186 ret = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd);
187 if (ret != 0) {
188 fprintf(stderr, "pthread_create(): Error. Returned %d\n", ret);
189 exit(EXIT_FAILURE);
190 }
191}
192
193
194
195
196
197void signal_handler(int signo, siginfo_t *si, void *uc)
198{
199 ucontext_t *ucp = uc;
200
201
202 ucp->uc_link->uc_mcontext.regs->nip += 4;
203
204 ucp->uc_mcontext.v_regs =
205 get_uf_mem(sizeof(elf_vrreg_t), ucp->uc_mcontext.v_regs);
206
207 ucp->uc_link->uc_mcontext.v_regs =
208 get_uf_mem(sizeof(elf_vrreg_t), ucp->uc_link->uc_mcontext.v_regs);
209
210 ucp->uc_link = get_uf_mem(sizeof(ucontext_t), ucp->uc_link);
211}
212
213bool have_userfaultfd(void)
214{
215 long rc;
216
217 errno = 0;
218 rc = syscall(__NR_userfaultfd, -1);
219
220 return rc == 0 || errno != ENOSYS;
221}
222
223int tm_signal_pagefault(void)
224{
225 struct sigaction sa;
226 stack_t ss;
227
228 SKIP_IF(!have_htm());
229 SKIP_IF(htm_is_synthetic());
230 SKIP_IF(!have_userfaultfd());
231
232 setup_uf_mem();
233
234
235
236
237
238
239 ss.ss_sp = get_uf_mem(SIGSTKSZ, NULL);
240 ss.ss_size = SIGSTKSZ;
241 ss.ss_flags = 0;
242 if (sigaltstack(&ss, NULL) == -1) {
243 perror("sigaltstack() failed");
244 exit(EXIT_FAILURE);
245 }
246
247 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
248 sa.sa_sigaction = signal_handler;
249 if (sigaction(SIGTRAP, &sa, NULL) == -1) {
250 perror("sigaction() failed");
251 exit(EXIT_FAILURE);
252 }
253
254
255 asm __volatile__(
256 "tbegin.;"
257 "beq 1f;"
258 "trap;"
259 "1: ;"
260 : : : "memory");
261
262
263 asm __volatile__(
264 "tbegin.;"
265 "beq 1f;"
266 "tsuspend.;"
267 "trap;"
268 "tresume.;"
269 "1: ;"
270 : : : "memory");
271
272 return EXIT_SUCCESS;
273}
274
275int main(int argc, char **argv)
276{
277
278
279
280
281
282
283 test_harness_set_timeout(2);
284 return test_harness(tm_signal_pagefault, "tm_signal_pagefault");
285}
286