xref: /freebsd/sys/kern/kern_kcov.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
5  * Copyright (C) 2018, 2019 Andrew Turner
6  *
7  * This software was developed by Mitchell Horne under sponsorship of
8  * the FreeBSD Foundation.
9  *
10  * This software was developed by SRI International and the University of
11  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
12  * ("CTSRD"), as part of the DARPA CRASH research programme.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 #ifdef KCSAN
39 #define	SAN_RUNTIME
40 #endif
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/eventhandler.h>
49 #include <sys/kcov.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mman.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/rwlock.h>
58 #include <sys/sysctl.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_pager.h>
66 #include <vm/vm_param.h>
67 
68 MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type");
69 
70 #define	KCOV_ELEMENT_SIZE	sizeof(uint64_t)
71 
72 /*
73  * To know what the code can safely perform at any point in time we use a
74  * state machine. In the normal case the state transitions are:
75  *
76  * OPEN -> READY -> RUNNING -> DYING
77  *  |       | ^        |        ^ ^
78  *  |       | +--------+        | |
79  *  |       +-------------------+ |
80  *  +-----------------------------+
81  *
82  * The states are:
83  *  OPEN:   The kcov fd has been opened, but no buffer is available to store
84  *          coverage data.
85  *  READY:  The buffer to store coverage data has been allocated. Userspace
86  *          can set this by using ioctl(fd, KIOSETBUFSIZE, entries);. When
87  *          this has been set the buffer can be written to by the kernel,
88  *          and mmaped by userspace.
89  * RUNNING: The coverage probes are able to store coverage data in the buffer.
90  *          This is entered with ioctl(fd, KIOENABLE, mode);. The READY state
91  *          can be exited by ioctl(fd, KIODISABLE); or exiting the thread to
92  *          return to the READY state to allow tracing to be reused, or by
93  *          closing the kcov fd to enter the DYING state.
94  * DYING:   The fd has been closed. All states can enter into this state when
95  *          userspace closes the kcov fd.
96  *
97  * We need to be careful when moving into and out of the RUNNING state. As
98  * an interrupt may happen while this is happening the ordering of memory
99  * operations is important so struct kcov_info is valid for the tracing
100  * functions.
101  *
102  * When moving into the RUNNING state prior stores to struct kcov_info need
103  * to be observed before the state is set. This allows for interrupts that
104  * may call into one of the coverage functions to fire at any point while
105  * being enabled and see a consistent struct kcov_info.
106  *
107  * When moving out of the RUNNING state any later stores to struct kcov_info
108  * need to be observed after the state is set. As with entering this is to
109  * present a consistent struct kcov_info to interrupts.
110  */
111 typedef enum {
112 	KCOV_STATE_INVALID,
113 	KCOV_STATE_OPEN,	/* The device is open, but with no buffer */
114 	KCOV_STATE_READY,	/* The buffer has been allocated */
115 	KCOV_STATE_RUNNING,	/* Recording trace data */
116 	KCOV_STATE_DYING,	/* The fd was closed */
117 } kcov_state_t;
118 
119 /*
120  * (l) Set while holding the kcov_lock mutex and not in the RUNNING state.
121  * (o) Only set once while in the OPEN state. Cleaned up while in the DYING
122  *     state, and with no thread associated with the struct kcov_info.
123  * (s) Set atomically to enter or exit the RUNNING state, non-atomically
124  *     otherwise. See above for a description of the other constraints while
125  *     moving into or out of the RUNNING state.
126  */
127 struct kcov_info {
128 	struct thread	*thread;	/* (l) */
129 	vm_object_t	bufobj;		/* (o) */
130 	vm_offset_t	kvaddr;		/* (o) */
131 	size_t		entries;	/* (o) */
132 	size_t		bufsize;	/* (o) */
133 	kcov_state_t	state;		/* (s) */
134 	int		mode;		/* (l) */
135 };
136 
137 /* Prototypes */
138 static d_open_t		kcov_open;
139 static d_close_t	kcov_close;
140 static d_mmap_single_t	kcov_mmap_single;
141 static d_ioctl_t	kcov_ioctl;
142 
143 static int  kcov_alloc(struct kcov_info *info, size_t entries);
144 static void kcov_free(struct kcov_info *info);
145 static void kcov_init(const void *unused);
146 
147 static struct cdevsw kcov_cdevsw = {
148 	.d_version =	D_VERSION,
149 	.d_open =	kcov_open,
150 	.d_close =	kcov_close,
151 	.d_mmap_single = kcov_mmap_single,
152 	.d_ioctl =	kcov_ioctl,
153 	.d_name =	"kcov",
154 };
155 
156 SYSCTL_NODE(_kern, OID_AUTO, kcov, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
157     "Kernel coverage");
158 
159 static u_int kcov_max_entries = KCOV_MAXENTRIES;
160 SYSCTL_UINT(_kern_kcov, OID_AUTO, max_entries, CTLFLAG_RW,
161     &kcov_max_entries, 0,
162     "Maximum number of entries in the kcov buffer");
163 
164 static struct mtx kcov_lock;
165 static int active_count;
166 
167 static struct kcov_info *
168 get_kinfo(struct thread *td)
169 {
170 	struct kcov_info *info;
171 
172 	/* We might have a NULL thread when releasing the secondary CPUs */
173 	if (td == NULL)
174 		return (NULL);
175 
176 	/*
177 	 * We are in an interrupt, stop tracing as it is not explicitly
178 	 * part of a syscall.
179 	 */
180 	if (td->td_intr_nesting_level > 0 || td->td_intr_frame != NULL)
181 		return (NULL);
182 
183 	/*
184 	 * If info is NULL or the state is not running we are not tracing.
185 	 */
186 	info = td->td_kcov_info;
187 	if (info == NULL ||
188 	    atomic_load_acq_int(&info->state) != KCOV_STATE_RUNNING)
189 		return (NULL);
190 
191 	return (info);
192 }
193 
194 static void
195 trace_pc(uintptr_t ret)
196 {
197 	struct thread *td;
198 	struct kcov_info *info;
199 	uint64_t *buf, index;
200 
201 	td = curthread;
202 	info = get_kinfo(td);
203 	if (info == NULL)
204 		return;
205 
206 	/*
207 	 * Check we are in the PC-trace mode.
208 	 */
209 	if (info->mode != KCOV_MODE_TRACE_PC)
210 		return;
211 
212 	KASSERT(info->kvaddr != 0,
213 	    ("__sanitizer_cov_trace_pc: NULL buf while running"));
214 
215 	buf = (uint64_t *)info->kvaddr;
216 
217 	/* The first entry of the buffer holds the index */
218 	index = buf[0];
219 	if (index + 2 > info->entries)
220 		return;
221 
222 	buf[index + 1] = ret;
223 	buf[0] = index + 1;
224 }
225 
226 static bool
227 trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret)
228 {
229 	struct thread *td;
230 	struct kcov_info *info;
231 	uint64_t *buf, index;
232 
233 	td = curthread;
234 	info = get_kinfo(td);
235 	if (info == NULL)
236 		return (false);
237 
238 	/*
239 	 * Check we are in the comparison-trace mode.
240 	 */
241 	if (info->mode != KCOV_MODE_TRACE_CMP)
242 		return (false);
243 
244 	KASSERT(info->kvaddr != 0,
245 	    ("__sanitizer_cov_trace_pc: NULL buf while running"));
246 
247 	buf = (uint64_t *)info->kvaddr;
248 
249 	/* The first entry of the buffer holds the index */
250 	index = buf[0];
251 
252 	/* Check we have space to store all elements */
253 	if (index * 4 + 4 + 1 > info->entries)
254 		return (false);
255 
256 	while (1) {
257 		buf[index * 4 + 1] = type;
258 		buf[index * 4 + 2] = arg1;
259 		buf[index * 4 + 3] = arg2;
260 		buf[index * 4 + 4] = ret;
261 
262 		if (atomic_cmpset_64(&buf[0], index, index + 1))
263 			break;
264 		buf[0] = index;
265 	}
266 
267 	return (true);
268 }
269 
270 /*
271  * The fd is being closed, cleanup everything we can.
272  */
273 static void
274 kcov_mmap_cleanup(void *arg)
275 {
276 	struct kcov_info *info = arg;
277 	struct thread *thread;
278 
279 	mtx_lock_spin(&kcov_lock);
280 	/*
281 	 * Move to KCOV_STATE_DYING to stop adding new entries.
282 	 *
283 	 * If the thread is running we need to wait until thread exit to
284 	 * clean up as it may currently be adding a new entry. If this is
285 	 * the case being in KCOV_STATE_DYING will signal that the buffer
286 	 * needs to be cleaned up.
287 	 */
288 	atomic_store_int(&info->state, KCOV_STATE_DYING);
289 	atomic_thread_fence_seq_cst();
290 	thread = info->thread;
291 	mtx_unlock_spin(&kcov_lock);
292 
293 	if (thread != NULL)
294 		return;
295 
296 	/*
297 	 * We can safely clean up the info struct as it is in the
298 	 * KCOV_STATE_DYING state with no thread associated.
299 	 *
300 	 * The KCOV_STATE_DYING stops new threads from using it.
301 	 * The lack of a thread means nothing is currently using the buffers.
302 	 */
303 	kcov_free(info);
304 }
305 
306 static int
307 kcov_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
308 {
309 	struct kcov_info *info;
310 	int error;
311 
312 	info = malloc(sizeof(struct kcov_info), M_KCOV_INFO, M_ZERO | M_WAITOK);
313 	info->state = KCOV_STATE_OPEN;
314 	info->thread = NULL;
315 	info->mode = -1;
316 
317 	if ((error = devfs_set_cdevpriv(info, kcov_mmap_cleanup)) != 0)
318 		kcov_mmap_cleanup(info);
319 
320 	return (error);
321 }
322 
323 static int
324 kcov_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
325 {
326 	struct kcov_info *info;
327 	int error;
328 
329 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
330 		return (error);
331 
332 	KASSERT(info != NULL, ("kcov_close with no kcov_info structure"));
333 
334 	/* Trying to close, but haven't disabled */
335 	if (info->state == KCOV_STATE_RUNNING)
336 		return (EBUSY);
337 
338 	return (0);
339 }
340 
341 static int
342 kcov_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
343     struct vm_object **object, int nprot)
344 {
345 	struct kcov_info *info;
346 	int error;
347 
348 	if ((nprot & (PROT_EXEC | PROT_READ | PROT_WRITE)) !=
349 	    (PROT_READ | PROT_WRITE))
350 		return (EINVAL);
351 
352 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
353 		return (error);
354 
355 	if (info->kvaddr == 0 || size / KCOV_ELEMENT_SIZE != info->entries)
356 		return (EINVAL);
357 
358 	vm_object_reference(info->bufobj);
359 	*offset = 0;
360 	*object = info->bufobj;
361 	return (0);
362 }
363 
364 static int
365 kcov_alloc(struct kcov_info *info, size_t entries)
366 {
367 	size_t n, pages;
368 	vm_page_t m;
369 
370 	KASSERT(info->kvaddr == 0, ("kcov_alloc: Already have a buffer"));
371 	KASSERT(info->state == KCOV_STATE_OPEN,
372 	    ("kcov_alloc: Not in open state (%x)", info->state));
373 
374 	if (entries < 2 || entries > kcov_max_entries)
375 		return (EINVAL);
376 
377 	/* Align to page size so mmap can't access other kernel memory */
378 	info->bufsize = roundup2(entries * KCOV_ELEMENT_SIZE, PAGE_SIZE);
379 	pages = info->bufsize / PAGE_SIZE;
380 
381 	if ((info->kvaddr = kva_alloc(info->bufsize)) == 0)
382 		return (ENOMEM);
383 
384 	info->bufobj = vm_pager_allocate(OBJT_PHYS, 0, info->bufsize,
385 	    PROT_READ | PROT_WRITE, 0, curthread->td_ucred);
386 
387 	VM_OBJECT_WLOCK(info->bufobj);
388 	for (n = 0; n < pages; n++) {
389 		m = vm_page_grab(info->bufobj, n,
390 		    VM_ALLOC_ZERO | VM_ALLOC_WIRED);
391 		vm_page_valid(m);
392 		vm_page_xunbusy(m);
393 		pmap_qenter(info->kvaddr + n * PAGE_SIZE, &m, 1);
394 	}
395 	VM_OBJECT_WUNLOCK(info->bufobj);
396 
397 	info->entries = entries;
398 
399 	return (0);
400 }
401 
402 static void
403 kcov_free(struct kcov_info *info)
404 {
405 	vm_page_t m;
406 	size_t i;
407 
408 	if (info->kvaddr != 0) {
409 		pmap_qremove(info->kvaddr, info->bufsize / PAGE_SIZE);
410 		kva_free(info->kvaddr, info->bufsize);
411 	}
412 	if (info->bufobj != NULL) {
413 		VM_OBJECT_WLOCK(info->bufobj);
414 		m = vm_page_lookup(info->bufobj, 0);
415 		for (i = 0; i < info->bufsize / PAGE_SIZE; i++) {
416 			vm_page_unwire_noq(m);
417 			m = vm_page_next(m);
418 		}
419 		VM_OBJECT_WUNLOCK(info->bufobj);
420 		vm_object_deallocate(info->bufobj);
421 	}
422 	free(info, M_KCOV_INFO);
423 }
424 
425 static int
426 kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused,
427     struct thread *td)
428 {
429 	struct kcov_info *info;
430 	int mode, error;
431 
432 	if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
433 		return (error);
434 
435 	if (cmd == KIOSETBUFSIZE) {
436 		/*
437 		 * Set the size of the coverage buffer. Should be called
438 		 * before enabling coverage collection for that thread.
439 		 */
440 		if (info->state != KCOV_STATE_OPEN) {
441 			return (EBUSY);
442 		}
443 		error = kcov_alloc(info, *(u_int *)data);
444 		if (error == 0)
445 			info->state = KCOV_STATE_READY;
446 		return (error);
447 	}
448 
449 	mtx_lock_spin(&kcov_lock);
450 	switch (cmd) {
451 	case KIOENABLE:
452 		if (info->state != KCOV_STATE_READY) {
453 			error = EBUSY;
454 			break;
455 		}
456 		if (td->td_kcov_info != NULL) {
457 			error = EINVAL;
458 			break;
459 		}
460 		mode = *(int *)data;
461 		if (mode != KCOV_MODE_TRACE_PC && mode != KCOV_MODE_TRACE_CMP) {
462 			error = EINVAL;
463 			break;
464 		}
465 
466 		/* Lets hope nobody opens this 2 billion times */
467 		KASSERT(active_count < INT_MAX,
468 		    ("%s: Open too many times", __func__));
469 		active_count++;
470 		if (active_count == 1) {
471 			cov_register_pc(&trace_pc);
472 			cov_register_cmp(&trace_cmp);
473 		}
474 
475 		KASSERT(info->thread == NULL,
476 		    ("Enabling kcov when already enabled"));
477 		info->thread = td;
478 		info->mode = mode;
479 		/*
480 		 * Ensure the mode has been set before starting coverage
481 		 * tracing.
482 		 */
483 		atomic_store_rel_int(&info->state, KCOV_STATE_RUNNING);
484 		td->td_kcov_info = info;
485 		break;
486 	case KIODISABLE:
487 		/* Only the currently enabled thread may disable itself */
488 		if (info->state != KCOV_STATE_RUNNING ||
489 		    info != td->td_kcov_info) {
490 			error = EINVAL;
491 			break;
492 		}
493 		KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
494 		active_count--;
495 		if (active_count == 0) {
496 			cov_unregister_pc();
497 			cov_unregister_cmp();
498 		}
499 
500 		td->td_kcov_info = NULL;
501 		atomic_store_int(&info->state, KCOV_STATE_READY);
502 		/*
503 		 * Ensure we have exited the READY state before clearing the
504 		 * rest of the info struct.
505 		 */
506 		atomic_thread_fence_rel();
507 		info->mode = -1;
508 		info->thread = NULL;
509 		break;
510 	default:
511 		error = EINVAL;
512 		break;
513 	}
514 	mtx_unlock_spin(&kcov_lock);
515 
516 	return (error);
517 }
518 
519 static void
520 kcov_thread_dtor(void *arg __unused, struct thread *td)
521 {
522 	struct kcov_info *info;
523 
524 	info = td->td_kcov_info;
525 	if (info == NULL)
526 		return;
527 
528 	mtx_lock_spin(&kcov_lock);
529 	KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
530 	active_count--;
531 	if (active_count == 0) {
532 		cov_unregister_pc();
533 		cov_unregister_cmp();
534 	}
535 	td->td_kcov_info = NULL;
536 	if (info->state != KCOV_STATE_DYING) {
537 		/*
538 		 * The kcov file is still open. Mark it as unused and
539 		 * wait for it to be closed before cleaning up.
540 		 */
541 		atomic_store_int(&info->state, KCOV_STATE_READY);
542 		atomic_thread_fence_seq_cst();
543 		/* This info struct is unused */
544 		info->thread = NULL;
545 		mtx_unlock_spin(&kcov_lock);
546 		return;
547 	}
548 	mtx_unlock_spin(&kcov_lock);
549 
550 	/*
551 	 * We can safely clean up the info struct as it is in the
552 	 * KCOV_STATE_DYING state where the info struct is associated with
553 	 * the current thread that's about to exit.
554 	 *
555 	 * The KCOV_STATE_DYING stops new threads from using it.
556 	 * It also stops the current thread from trying to use the info struct.
557 	 */
558 	kcov_free(info);
559 }
560 
561 static void
562 kcov_init(const void *unused)
563 {
564 	struct make_dev_args args;
565 	struct cdev *dev;
566 
567 	mtx_init(&kcov_lock, "kcov lock", NULL, MTX_SPIN);
568 
569 	make_dev_args_init(&args);
570 	args.mda_devsw = &kcov_cdevsw;
571 	args.mda_uid = UID_ROOT;
572 	args.mda_gid = GID_WHEEL;
573 	args.mda_mode = 0600;
574 	if (make_dev_s(&args, &dev, "kcov") != 0) {
575 		printf("%s", "Failed to create kcov device");
576 		return;
577 	}
578 
579 	EVENTHANDLER_REGISTER(thread_dtor, kcov_thread_dtor, NULL,
580 	    EVENTHANDLER_PRI_ANY);
581 }
582 
583 SYSINIT(kcovdev, SI_SUB_LAST, SI_ORDER_ANY, kcov_init, NULL);
584