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