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