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