1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/queue.h> 33 #include <sys/blist.h> 34 #include <sys/conf.h> 35 #include <sys/exec.h> 36 #include <sys/filedesc.h> 37 #include <sys/kernel.h> 38 #include <sys/linker.h> 39 #include <sys/malloc.h> 40 #include <sys/mount.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/resourcevar.h> 44 #include <sys/sbuf.h> 45 #include <sys/smp.h> 46 #include <sys/socket.h> 47 #include <sys/vnode.h> 48 #include <sys/bus.h> 49 #include <sys/pciio.h> 50 51 #include <dev/pci/pcivar.h> 52 #include <dev/pci/pcireg.h> 53 54 #include <net/if.h> 55 56 #include <vm/vm.h> 57 #include <vm/pmap.h> 58 #include <vm/vm_map.h> 59 #include <vm/vm_param.h> 60 #include <vm/vm_object.h> 61 #include <vm/swap_pager.h> 62 63 #include <machine/bus.h> 64 65 #include <compat/linux/linux_ioctl.h> 66 #include <compat/linux/linux_mib.h> 67 #include <compat/linux/linux_util.h> 68 #include <fs/pseudofs/pseudofs.h> 69 70 #include <asm/atomic.h> 71 #include <linux/compat.h> 72 #include <linux/debugfs.h> 73 #include <linux/fs.h> 74 75 MALLOC_DEFINE(M_DFSINT, "debugfsint", "Linux debugfs internal"); 76 77 static struct pfs_node *debugfs_root; 78 79 #define DM_SYMLINK 0x1 80 #define DM_DIR 0x2 81 #define DM_FILE 0x3 82 83 struct dentry_meta { 84 struct dentry dm_dnode; 85 const struct file_operations *dm_fops; 86 void *dm_data; 87 umode_t dm_mode; 88 int dm_type; 89 }; 90 91 static int 92 debugfs_attr(PFS_ATTR_ARGS) 93 { 94 struct dentry_meta *dm; 95 96 dm = pn->pn_data; 97 98 vap->va_mode = dm->dm_mode; 99 return (0); 100 } 101 102 static int 103 debugfs_destroy(PFS_DESTROY_ARGS) 104 { 105 struct dentry_meta *dm; 106 107 dm = pn->pn_data; 108 if (dm->dm_type == DM_SYMLINK) 109 free(dm->dm_data, M_DFSINT); 110 111 free(dm, M_DFSINT); 112 return (0); 113 } 114 115 static int 116 debugfs_fill(PFS_FILL_ARGS) 117 { 118 struct dentry_meta *d; 119 struct linux_file lf = {}; 120 struct vnode vn; 121 char *buf; 122 int rc; 123 off_t off = 0; 124 125 if ((rc = linux_set_current_flags(curthread, M_NOWAIT))) 126 return (rc); 127 128 d = pn->pn_data; 129 vn.v_data = d->dm_data; 130 131 rc = d->dm_fops->open(&vn, &lf); 132 if (rc < 0) { 133 #ifdef INVARIANTS 134 printf("%s:%d open failed with %d\n", __FUNCTION__, __LINE__, rc); 135 #endif 136 return (-rc); 137 } 138 139 rc = -ENODEV; 140 if (uio->uio_rw == UIO_READ && d->dm_fops->read) { 141 rc = -ENOMEM; 142 buf = (char *) malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT); 143 if (buf != NULL) { 144 rc = d->dm_fops->read(&lf, buf, sb->s_size, &off); 145 if (rc > 0) 146 sbuf_bcpy(sb, buf, strlen(buf)); 147 148 free(buf, M_DFSINT); 149 } 150 } else if (uio->uio_rw == UIO_WRITE && d->dm_fops->write) { 151 sbuf_finish(sb); 152 rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb), &off); 153 } 154 155 if (d->dm_fops->release) 156 d->dm_fops->release(&vn, &lf); 157 else 158 single_release(&vn, &lf); 159 160 if (rc < 0) { 161 #ifdef INVARIANTS 162 printf("%s:%d read/write failed with %d\n", __FUNCTION__, __LINE__, rc); 163 #endif 164 return (-rc); 165 } 166 return (0); 167 } 168 169 static int 170 debugfs_fill_data(PFS_FILL_ARGS) 171 { 172 struct dentry_meta *dm; 173 174 dm = pn->pn_data; 175 sbuf_printf(sb, "%s", (char *)dm->dm_data); 176 return (0); 177 } 178 179 struct dentry * 180 debugfs_create_file(const char *name, umode_t mode, 181 struct dentry *parent, void *data, 182 const struct file_operations *fops) 183 { 184 struct dentry_meta *dm; 185 struct dentry *dnode; 186 struct pfs_node *pnode; 187 int flags; 188 189 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); 190 if (dm == NULL) 191 return (NULL); 192 dnode = &dm->dm_dnode; 193 dm->dm_fops = fops; 194 dm->dm_data = data; 195 dm->dm_mode = mode; 196 dm->dm_type = DM_FILE; 197 if (parent != NULL) 198 pnode = parent->d_pfs_node; 199 else 200 pnode = debugfs_root; 201 202 flags = fops->write ? PFS_RDWR : PFS_RD; 203 dnode->d_pfs_node = pfs_create_file(pnode, name, debugfs_fill, 204 debugfs_attr, NULL, debugfs_destroy, flags | PFS_NOWAIT); 205 if (dnode->d_pfs_node == NULL) { 206 free(dm, M_DFSINT); 207 return (NULL); 208 } 209 dnode->d_pfs_node->pn_data = dm; 210 211 return (dnode); 212 } 213 214 struct dentry * 215 debugfs_create_file_size(const char *name, umode_t mode, 216 struct dentry *parent, void *data, 217 const struct file_operations *fops, 218 loff_t file_size __unused) 219 { 220 221 return debugfs_create_file(name, mode, parent, data, fops); 222 } 223 224 /* 225 * NOTE: Files created with the _unsafe moniker will not be protected from 226 * debugfs core file removals. It is the responsibility of @fops to protect 227 * its file using debugfs_file_get() and debugfs_file_put(). 228 * 229 * FreeBSD's LinuxKPI lindebugfs does not perform file removals at the time 230 * of writing. Therefore there is no difference between functions with _unsafe 231 * and functions without _unsafe when using lindebugfs. Functions with _unsafe 232 * exist only for Linux compatibility. 233 */ 234 struct dentry * 235 debugfs_create_file_unsafe(const char *name, umode_t mode, 236 struct dentry *parent, void *data, 237 const struct file_operations *fops) 238 { 239 240 return (debugfs_create_file(name, mode, parent, data, fops)); 241 } 242 243 struct dentry * 244 debugfs_create_mode_unsafe(const char *name, umode_t mode, 245 struct dentry *parent, void *data, 246 const struct file_operations *fops, 247 const struct file_operations *fops_ro, 248 const struct file_operations *fops_wo) 249 { 250 umode_t read = mode & S_IRUGO; 251 umode_t write = mode & S_IWUGO; 252 253 if (read && !write) 254 return (debugfs_create_file_unsafe(name, mode, parent, data, fops_ro)); 255 256 if (write && !read) 257 return (debugfs_create_file_unsafe(name, mode, parent, data, fops_wo)); 258 259 return (debugfs_create_file_unsafe(name, mode, parent, data, fops)); 260 } 261 262 struct dentry * 263 debugfs_create_dir(const char *name, struct dentry *parent) 264 { 265 struct dentry_meta *dm; 266 struct dentry *dnode; 267 struct pfs_node *pnode; 268 269 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); 270 if (dm == NULL) 271 return (NULL); 272 dnode = &dm->dm_dnode; 273 dm->dm_mode = 0700; 274 dm->dm_type = DM_DIR; 275 if (parent != NULL) 276 pnode = parent->d_pfs_node; 277 else 278 pnode = debugfs_root; 279 280 dnode->d_pfs_node = pfs_create_dir(pnode, name, debugfs_attr, NULL, debugfs_destroy, PFS_RD | PFS_NOWAIT); 281 if (dnode->d_pfs_node == NULL) { 282 free(dm, M_DFSINT); 283 return (NULL); 284 } 285 dnode->d_pfs_node->pn_data = dm; 286 return (dnode); 287 } 288 289 struct dentry * 290 debugfs_create_symlink(const char *name, struct dentry *parent, 291 const char *dest) 292 { 293 struct dentry_meta *dm; 294 struct dentry *dnode; 295 struct pfs_node *pnode; 296 void *data; 297 298 data = strdup_flags(dest, M_DFSINT, M_NOWAIT); 299 if (data == NULL) 300 return (NULL); 301 dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); 302 if (dm == NULL) 303 goto fail1; 304 dnode = &dm->dm_dnode; 305 dm->dm_mode = 0700; 306 dm->dm_type = DM_SYMLINK; 307 dm->dm_data = data; 308 if (parent != NULL) 309 pnode = parent->d_pfs_node; 310 else 311 pnode = debugfs_root; 312 313 dnode->d_pfs_node = pfs_create_link(pnode, name, &debugfs_fill_data, NULL, NULL, NULL, PFS_NOWAIT); 314 if (dnode->d_pfs_node == NULL) 315 goto fail; 316 dnode->d_pfs_node->pn_data = dm; 317 return (dnode); 318 fail: 319 free(dm, M_DFSINT); 320 fail1: 321 free(data, M_DFSINT); 322 return (NULL); 323 } 324 325 void 326 debugfs_remove(struct dentry *dnode) 327 { 328 if (dnode == NULL) 329 return; 330 331 pfs_destroy(dnode->d_pfs_node); 332 } 333 334 void 335 debugfs_remove_recursive(struct dentry *dnode) 336 { 337 if (dnode == NULL) 338 return; 339 340 pfs_destroy(dnode->d_pfs_node); 341 } 342 343 static int 344 debugfs_bool_get(void *data, uint64_t *ullval) 345 { 346 bool *bval = data; 347 348 if (*bval) 349 *ullval = 1; 350 else 351 *ullval = 0; 352 353 return (0); 354 } 355 356 static int 357 debugfs_bool_set(void *data, uint64_t ullval) 358 { 359 bool *bval = data; 360 361 if (ullval) 362 *bval = 1; 363 else 364 *bval = 0; 365 366 return (0); 367 } 368 369 DEFINE_DEBUGFS_ATTRIBUTE(fops_bool, debugfs_bool_get, debugfs_bool_set, "%llu\n"); 370 DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_ro, debugfs_bool_get, NULL, "%llu\n"); 371 DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_wo, NULL, debugfs_bool_set, "%llu\n"); 372 373 void 374 debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, bool *value) 375 { 376 377 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 378 &fops_bool_ro, &fops_bool_wo); 379 } 380 381 382 static int 383 debugfs_u8_get(void *data, uint64_t *value) 384 { 385 uint8_t *u8data = data; 386 *value = *u8data; 387 return (0); 388 } 389 390 static int 391 debugfs_u8_set(void *data, uint64_t value) 392 { 393 uint8_t *u8data = data; 394 *u8data = (uint8_t)value; 395 return (0); 396 } 397 398 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%u\n"); 399 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%u\n"); 400 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%u\n"); 401 402 void 403 debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value) 404 { 405 406 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 407 &fops_u8_ro, &fops_u8_wo); 408 } 409 410 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%016llx\n"); 411 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%016llx\n"); 412 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%016llx\n"); 413 414 void 415 debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value) 416 { 417 418 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 419 &fops_x8_ro, &fops_x8_wo); 420 } 421 422 423 static int 424 debugfs_u16_get(void *data, uint64_t *value) 425 { 426 uint16_t *u16data = data; 427 *value = *u16data; 428 return (0); 429 } 430 431 static int 432 debugfs_u16_set(void *data, uint64_t value) 433 { 434 uint16_t *u16data = data; 435 *u16data = (uint16_t)value; 436 return (0); 437 } 438 439 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%u\n"); 440 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%u\n"); 441 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%u\n"); 442 443 void 444 debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value) 445 { 446 447 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 448 &fops_u16_ro, &fops_u16_wo); 449 } 450 451 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%016llx\n"); 452 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%016llx\n"); 453 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%016llx\n"); 454 455 void 456 debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value) 457 { 458 459 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 460 &fops_x16_ro, &fops_x16_wo); 461 } 462 463 464 static int 465 debugfs_u32_get(void *data, uint64_t *value) 466 { 467 uint32_t *u32data = data; 468 *value = *u32data; 469 return (0); 470 } 471 472 static int 473 debugfs_u32_set(void *data, uint64_t value) 474 { 475 uint32_t *u32data = data; 476 *u32data = (uint32_t)value; 477 return (0); 478 } 479 480 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%u\n"); 481 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%u\n"); 482 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%u\n"); 483 484 void 485 debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value) 486 { 487 488 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 489 &fops_u32_ro, &fops_u32_wo); 490 } 491 492 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%016llx\n"); 493 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%016llx\n"); 494 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%016llx\n"); 495 496 void 497 debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value) 498 { 499 500 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 501 &fops_x32_ro, &fops_x32_wo); 502 } 503 504 505 static int 506 debugfs_u64_get(void *data, uint64_t *value) 507 { 508 uint64_t *u64data = data; 509 *value = *u64data; 510 return (0); 511 } 512 513 static int 514 debugfs_u64_set(void *data, uint64_t value) 515 { 516 uint64_t *u64data = data; 517 *u64data = (uint64_t)value; 518 return (0); 519 } 520 521 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%u\n"); 522 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%u\n"); 523 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%u\n"); 524 525 void 526 debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value) 527 { 528 529 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 530 &fops_u64_ro, &fops_u64_wo); 531 } 532 533 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 534 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 535 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 536 537 void 538 debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value) 539 { 540 541 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 542 &fops_x64_ro, &fops_x64_wo); 543 } 544 545 546 static int 547 debugfs_ulong_get(void *data, uint64_t *value) 548 { 549 uint64_t *uldata = data; 550 *value = *uldata; 551 return (0); 552 } 553 554 static int 555 debugfs_ulong_set(void *data, uint64_t value) 556 { 557 uint64_t *uldata = data; 558 *uldata = value; 559 return (0); 560 } 561 562 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n"); 563 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 564 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 565 566 void 567 debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, unsigned long *value) 568 { 569 570 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 571 &fops_ulong_ro, &fops_ulong_wo); 572 } 573 574 575 static int 576 debugfs_atomic_t_get(void *data, uint64_t *value) 577 { 578 atomic_t *atomic_data = data; 579 *value = atomic_read(atomic_data); 580 return (0); 581 } 582 583 static int 584 debugfs_atomic_t_set(void *data, uint64_t value) 585 { 586 atomic_t *atomic_data = data; 587 atomic_set(atomic_data, (int)value); 588 return (0); 589 } 590 591 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, debugfs_atomic_t_set, "%d\n"); 592 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%d\n"); 593 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%d\n"); 594 595 void 596 debugfs_create_atomic_t(const char *name, umode_t mode, struct dentry *parent, atomic_t *value) 597 { 598 599 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 600 &fops_atomic_t_ro, &fops_atomic_t_wo); 601 } 602 603 604 static ssize_t 605 fops_blob_read(struct file *filp, char __user *ubuf, size_t read_size, loff_t *ppos) 606 { 607 struct debugfs_blob_wrapper *blob; 608 609 blob = filp->private_data; 610 if (blob == NULL) 611 return (-EINVAL); 612 if (blob->size == 0 || blob->data == NULL) 613 return (-EINVAL); 614 615 return (simple_read_from_buffer(ubuf, read_size, ppos, blob->data, blob->size)); 616 } 617 618 static int 619 fops_blob_open(struct inode *inode, struct file *filp) 620 { 621 622 return (simple_open(inode, filp)); 623 } 624 625 static const struct file_operations __fops_blob_ro = { 626 .owner = THIS_MODULE, 627 .open = fops_blob_open, 628 .read = fops_blob_read, 629 .llseek = no_llseek 630 }; 631 632 struct dentry * 633 debugfs_create_blob(const char *name, umode_t mode, struct dentry *parent, 634 struct debugfs_blob_wrapper *value) 635 { 636 /* Blobs are read-only. */ 637 return (debugfs_create_file(name, mode & 0444, parent, value, &__fops_blob_ro)); 638 } 639 640 641 static int 642 lindebugfs_init(PFS_INIT_ARGS) 643 { 644 645 debugfs_root = pi->pi_root; 646 647 (void)debugfs_create_symlink("kcov", NULL, "/dev/kcov"); 648 649 return (0); 650 } 651 652 static int 653 lindebugfs_uninit(PFS_INIT_ARGS) 654 { 655 656 return (0); 657 } 658 659 PSEUDOFS(lindebugfs, 1, VFCF_JAIL); 660 MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1); 661