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