1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * S/390 debug facility 4 * 5 * Copyright IBM Corp. 1999, 2020 6 * 7 * Author(s): Michael Holzheu (holzheu@de.ibm.com), 8 * Holger Smolinski (Holger.Smolinski@de.ibm.com) 9 * 10 * Bugreports to: <Linux390@de.ibm.com> 11 */ 12 13 #define pr_fmt(fmt) "s390dbf: " fmt 14 15 #include <linux/stddef.h> 16 #include <linux/kernel.h> 17 #include <linux/errno.h> 18 #include <linux/slab.h> 19 #include <linux/ctype.h> 20 #include <linux/string.h> 21 #include <linux/sysctl.h> 22 #include <linux/uaccess.h> 23 #include <linux/export.h> 24 #include <linux/init.h> 25 #include <linux/fs.h> 26 #include <linux/math.h> 27 #include <linux/minmax.h> 28 #include <linux/debugfs.h> 29 30 #include <asm/debug.h> 31 32 #define DEBUG_PROLOG_ENTRY -1 33 34 #define ALL_AREAS 0 /* copy all debug areas */ 35 #define NO_AREAS 1 /* copy no debug areas */ 36 37 /* typedefs */ 38 39 typedef struct file_private_info { 40 loff_t offset; /* offset of last read in file */ 41 int act_area; /* number of last formatted area */ 42 int act_page; /* act page in given area */ 43 int act_entry; /* last formatted entry (offset */ 44 /* relative to beginning of last */ 45 /* formatted page) */ 46 size_t act_entry_offset; /* up to this offset we copied */ 47 /* in last read the last formatted */ 48 /* entry to userland */ 49 char temp_buf[2048]; /* buffer for output */ 50 debug_info_t *debug_info_org; /* original debug information */ 51 debug_info_t *debug_info_snap; /* snapshot of debug information */ 52 struct debug_view *view; /* used view of debug info */ 53 } file_private_info_t; 54 55 typedef struct { 56 char *string; 57 /* 58 * This assumes that all args are converted into longs 59 * on L/390 this is the case for all types of parameter 60 * except of floats, and long long (32 bit) 61 * 62 */ 63 long args[]; 64 } debug_sprintf_entry_t; 65 66 /* internal function prototypes */ 67 68 static int debug_init(void); 69 static ssize_t debug_output(struct file *file, char __user *user_buf, 70 size_t user_len, loff_t *offset); 71 static ssize_t debug_input(struct file *file, const char __user *user_buf, 72 size_t user_len, loff_t *offset); 73 static int debug_open(struct inode *inode, struct file *file); 74 static int debug_close(struct inode *inode, struct file *file); 75 static debug_info_t *debug_info_create(const char *name, int pages_per_area, 76 int nr_areas, int buf_size, umode_t mode); 77 static void debug_info_get(debug_info_t *); 78 static void debug_info_put(debug_info_t *); 79 static int debug_prolog_level_fn(debug_info_t *id, 80 struct debug_view *view, char *out_buf, 81 size_t out_buf_size); 82 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view, 83 struct file *file, const char __user *user_buf, 84 size_t user_buf_size, loff_t *offset); 85 static int debug_prolog_pages_fn(debug_info_t *id, 86 struct debug_view *view, char *out_buf, 87 size_t out_buf_size); 88 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view, 89 struct file *file, const char __user *user_buf, 90 size_t user_buf_size, loff_t *offset); 91 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view, 92 struct file *file, const char __user *user_buf, 93 size_t user_buf_size, loff_t *offset); 94 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view, 95 char *out_buf, size_t out_buf_size, 96 const char *in_buf); 97 static void debug_areas_swap(debug_info_t *a, debug_info_t *b); 98 static void debug_events_append(debug_info_t *dest, debug_info_t *src); 99 100 /* globals */ 101 102 struct debug_view debug_hex_ascii_view = { 103 "hex_ascii", 104 NULL, 105 &debug_dflt_header_fn, 106 &debug_hex_ascii_format_fn, 107 NULL, 108 NULL 109 }; 110 EXPORT_SYMBOL(debug_hex_ascii_view); 111 112 static struct debug_view debug_level_view = { 113 "level", 114 &debug_prolog_level_fn, 115 NULL, 116 NULL, 117 &debug_input_level_fn, 118 NULL 119 }; 120 121 static struct debug_view debug_pages_view = { 122 "pages", 123 &debug_prolog_pages_fn, 124 NULL, 125 NULL, 126 &debug_input_pages_fn, 127 NULL 128 }; 129 130 static struct debug_view debug_flush_view = { 131 "flush", 132 NULL, 133 NULL, 134 NULL, 135 &debug_input_flush_fn, 136 NULL 137 }; 138 139 struct debug_view debug_sprintf_view = { 140 "sprintf", 141 NULL, 142 &debug_dflt_header_fn, 143 &debug_sprintf_format_fn, 144 NULL, 145 NULL 146 }; 147 EXPORT_SYMBOL(debug_sprintf_view); 148 149 /* used by dump analysis tools to determine version of debug feature */ 150 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION; 151 152 /* static globals */ 153 154 static debug_info_t *debug_area_first; 155 static debug_info_t *debug_area_last; 156 static DEFINE_MUTEX(debug_mutex); 157 158 static int initialized; 159 static int debug_critical; 160 161 static const struct file_operations debug_file_ops = { 162 .owner = THIS_MODULE, 163 .read = debug_output, 164 .write = debug_input, 165 .open = debug_open, 166 .release = debug_close, 167 }; 168 169 static struct dentry *debug_debugfs_root_entry; 170 171 /* functions */ 172 173 /* 174 * debug_areas_alloc 175 * - Debug areas are implemented as a threedimensonal array: 176 * areas[areanumber][pagenumber][pageoffset] 177 */ 178 179 static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas) 180 { 181 debug_entry_t ***areas; 182 int i, j; 183 184 areas = kmalloc_objs(debug_entry_t **, nr_areas); 185 if (!areas) 186 goto fail_malloc_areas; 187 for (i = 0; i < nr_areas; i++) { 188 /* GFP_NOWARN to avoid user triggerable WARN, we handle fails */ 189 areas[i] = kmalloc_objs(debug_entry_t *, pages_per_area, 190 GFP_KERNEL | __GFP_NOWARN); 191 if (!areas[i]) 192 goto fail_malloc_areas2; 193 for (j = 0; j < pages_per_area; j++) { 194 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL); 195 if (!areas[i][j]) { 196 for (j--; j >= 0 ; j--) 197 kfree(areas[i][j]); 198 kfree(areas[i]); 199 goto fail_malloc_areas2; 200 } 201 } 202 } 203 return areas; 204 205 fail_malloc_areas2: 206 for (i--; i >= 0; i--) { 207 for (j = 0; j < pages_per_area; j++) 208 kfree(areas[i][j]); 209 kfree(areas[i]); 210 } 211 kfree(areas); 212 fail_malloc_areas: 213 return NULL; 214 } 215 216 /* 217 * debug_info_alloc 218 * - alloc new debug-info 219 */ 220 static debug_info_t *debug_info_alloc(const char *name, int pages_per_area, 221 int nr_areas, int buf_size, int level, 222 int mode) 223 { 224 debug_info_t *rc; 225 226 /* alloc everything */ 227 rc = kmalloc_obj(debug_info_t); 228 if (!rc) 229 goto fail_malloc_rc; 230 rc->active_entries = kzalloc_objs(int, nr_areas); 231 if (!rc->active_entries) 232 goto fail_malloc_active_entries; 233 rc->active_pages = kzalloc_objs(int, nr_areas); 234 if (!rc->active_pages) 235 goto fail_malloc_active_pages; 236 if ((mode == ALL_AREAS) && (pages_per_area != 0)) { 237 rc->areas = debug_areas_alloc(pages_per_area, nr_areas); 238 if (!rc->areas) 239 goto fail_malloc_areas; 240 } else { 241 rc->areas = NULL; 242 } 243 244 /* initialize members */ 245 raw_spin_lock_init(&rc->lock); 246 rc->pages_per_area = pages_per_area; 247 rc->nr_areas = nr_areas; 248 rc->active_area = 0; 249 rc->level = level; 250 rc->buf_size = buf_size; 251 rc->entry_size = sizeof(debug_entry_t) + buf_size; 252 strscpy(rc->name, name); 253 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *)); 254 memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *)); 255 refcount_set(&(rc->ref_count), 0); 256 257 return rc; 258 259 fail_malloc_areas: 260 kfree(rc->active_pages); 261 fail_malloc_active_pages: 262 kfree(rc->active_entries); 263 fail_malloc_active_entries: 264 kfree(rc); 265 fail_malloc_rc: 266 return NULL; 267 } 268 269 /* 270 * debug_areas_free 271 * - free all debug areas 272 */ 273 static void debug_areas_free(debug_info_t *db_info) 274 { 275 int i, j; 276 277 if (!db_info->areas) 278 return; 279 for (i = 0; i < db_info->nr_areas; i++) { 280 for (j = 0; j < db_info->pages_per_area; j++) 281 kfree(db_info->areas[i][j]); 282 kfree(db_info->areas[i]); 283 } 284 kfree(db_info->areas); 285 db_info->areas = NULL; 286 } 287 288 /* 289 * debug_info_free 290 * - free memory debug-info 291 */ 292 static void debug_info_free(debug_info_t *db_info) 293 { 294 debug_areas_free(db_info); 295 kfree(db_info->active_entries); 296 kfree(db_info->active_pages); 297 kfree(db_info); 298 } 299 300 /* 301 * debug_info_create 302 * - create new debug-info 303 */ 304 305 static debug_info_t *debug_info_create(const char *name, int pages_per_area, 306 int nr_areas, int buf_size, umode_t mode) 307 { 308 debug_info_t *rc; 309 310 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size, 311 DEBUG_DEFAULT_LEVEL, ALL_AREAS); 312 if (!rc) 313 goto out; 314 315 rc->mode = mode & ~S_IFMT; 316 refcount_set(&rc->ref_count, 1); 317 out: 318 return rc; 319 } 320 321 /* 322 * debug_info_copy 323 * - copy debug-info 324 */ 325 static debug_info_t *debug_info_copy(debug_info_t *in, int mode) 326 { 327 unsigned long flags; 328 debug_info_t *rc; 329 int i, j; 330 331 /* get a consistent copy of the debug areas */ 332 do { 333 rc = debug_info_alloc(in->name, in->pages_per_area, 334 in->nr_areas, in->buf_size, in->level, mode); 335 raw_spin_lock_irqsave(&in->lock, flags); 336 if (!rc) 337 goto out; 338 /* has something changed in the meantime ? */ 339 if ((rc->pages_per_area == in->pages_per_area) && 340 (rc->nr_areas == in->nr_areas)) { 341 break; 342 } 343 raw_spin_unlock_irqrestore(&in->lock, flags); 344 debug_info_free(rc); 345 } while (1); 346 347 if (mode == NO_AREAS) 348 goto out; 349 350 for (i = 0; i < in->nr_areas; i++) { 351 for (j = 0; j < in->pages_per_area; j++) 352 memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE); 353 rc->active_pages[i] = in->active_pages[i]; 354 rc->active_entries[i] = in->active_entries[i]; 355 } 356 rc->active_area = in->active_area; 357 out: 358 raw_spin_unlock_irqrestore(&in->lock, flags); 359 return rc; 360 } 361 362 /* 363 * debug_info_get 364 * - increments reference count for debug-info 365 */ 366 static void debug_info_get(debug_info_t *db_info) 367 { 368 if (db_info) 369 refcount_inc(&db_info->ref_count); 370 } 371 372 /* 373 * debug_info_put: 374 * - decreases reference count for debug-info and frees it if necessary 375 */ 376 static void debug_info_put(debug_info_t *db_info) 377 { 378 if (!db_info) 379 return; 380 if (refcount_dec_and_test(&db_info->ref_count)) 381 debug_info_free(db_info); 382 } 383 384 /* 385 * debug_format_entry: 386 * - format one debug entry and return size of formatted data 387 */ 388 static int debug_format_entry(file_private_info_t *p_info) 389 { 390 debug_info_t *id_snap = p_info->debug_info_snap; 391 struct debug_view *view = p_info->view; 392 debug_entry_t *act_entry; 393 size_t len = 0; 394 395 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) { 396 /* print prolog */ 397 if (view->prolog_proc) { 398 len += view->prolog_proc(id_snap, view, p_info->temp_buf, 399 sizeof(p_info->temp_buf)); 400 } 401 goto out; 402 } 403 if (!id_snap->areas) /* this is true, if we have a prolog only view */ 404 goto out; /* or if 'pages_per_area' is 0 */ 405 act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area] 406 [p_info->act_page] + p_info->act_entry); 407 408 if (act_entry->clock == 0LL) 409 goto out; /* empty entry */ 410 if (view->header_proc) { 411 len += view->header_proc(id_snap, view, p_info->act_area, 412 act_entry, p_info->temp_buf + len, 413 sizeof(p_info->temp_buf) - len); 414 } 415 if (view->format_proc) { 416 len += view->format_proc(id_snap, view, p_info->temp_buf + len, 417 sizeof(p_info->temp_buf) - len, 418 DEBUG_DATA(act_entry)); 419 } 420 out: 421 return len; 422 } 423 424 /** 425 * debug_next_entry - Go to the next entry 426 * @p_info: Private info that is manipulated 427 * 428 * Sets the current position in @p_info to the next entry. If no further entry 429 * exists the current position is set to one after the end the return value 430 * indicates that no further entries exist. 431 * 432 * Return: True if there are more following entries, false otherwise 433 */ 434 static inline bool debug_next_entry(file_private_info_t *p_info) 435 { 436 debug_info_t *id; 437 438 id = p_info->debug_info_snap; 439 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) { 440 p_info->act_entry = 0; 441 p_info->act_page = 0; 442 return true; 443 } 444 if (!id->areas) 445 return false; 446 p_info->act_entry += id->entry_size; 447 /* switch to next page, if we reached the end of the page */ 448 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) { 449 /* next page */ 450 p_info->act_entry = 0; 451 p_info->act_page += 1; 452 if ((p_info->act_page % id->pages_per_area) == 0) { 453 /* next area */ 454 p_info->act_area++; 455 p_info->act_page = 0; 456 } 457 if (p_info->act_area >= id->nr_areas) 458 return false; 459 } 460 return true; 461 } 462 463 /** 464 * debug_to_act_entry - Go to the currently active entry 465 * @p_info: Private info that is manipulated 466 * 467 * Sets the current position in @p_info to the currently active 468 * entry of @p_info->debug_info_snap 469 */ 470 static void debug_to_act_entry(file_private_info_t *p_info) 471 { 472 debug_info_t *snap_id; 473 474 snap_id = p_info->debug_info_snap; 475 p_info->act_area = snap_id->active_area; 476 p_info->act_page = snap_id->active_pages[snap_id->active_area]; 477 p_info->act_entry = snap_id->active_entries[snap_id->active_area]; 478 } 479 480 /** 481 * debug_prev_entry - Go to the previous entry 482 * @p_info: Private info that is manipulated 483 * 484 * Sets the current position in @p_info to the previous entry. If no previous entry 485 * exists the current position is set left as DEBUG_PROLOG_ENTRY and the return value 486 * indicates that no previous entries exist. 487 * 488 * Return: True if there are more previous entries, false otherwise 489 */ 490 491 static inline bool debug_prev_entry(file_private_info_t *p_info) 492 { 493 debug_info_t *id; 494 495 id = p_info->debug_info_snap; 496 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) 497 debug_to_act_entry(p_info); 498 if (!id->areas) 499 return false; 500 p_info->act_entry -= id->entry_size; 501 /* switch to prev page, if we reached the beginning of the page */ 502 if (p_info->act_entry < 0) { 503 /* end of previous page */ 504 p_info->act_entry = rounddown(PAGE_SIZE, id->entry_size) - id->entry_size; 505 p_info->act_page--; 506 if (p_info->act_page < 0) { 507 /* previous area */ 508 p_info->act_area--; 509 p_info->act_page = id->pages_per_area - 1; 510 } 511 if (p_info->act_area < 0) 512 p_info->act_area = (id->nr_areas - 1) % id->nr_areas; 513 } 514 /* check full circle */ 515 if (id->active_area == p_info->act_area && 516 id->active_pages[id->active_area] == p_info->act_page && 517 id->active_entries[id->active_area] == p_info->act_entry) 518 return false; 519 return true; 520 } 521 522 /** 523 * debug_move_entry - Go to next entry in either the forward or backward direction 524 * @p_info: Private info that is manipulated 525 * @reverse: If true go to the next entry in reverse i.e. previous 526 * 527 * Sets the current position in @p_info to the next (@reverse == false) or 528 * previous (@reverse == true) entry. 529 * 530 * Return: True if there are further entries in that direction, 531 * false otherwise. 532 */ 533 static bool debug_move_entry(file_private_info_t *p_info, bool reverse) 534 { 535 if (reverse) 536 return debug_prev_entry(p_info); 537 else 538 return debug_next_entry(p_info); 539 } 540 541 /* 542 * debug_output: 543 * - called for user read() 544 * - copies formatted debug entries to the user buffer 545 */ 546 static ssize_t debug_output(struct file *file, /* file descriptor */ 547 char __user *user_buf, /* user buffer */ 548 size_t len, /* length of buffer */ 549 loff_t *offset) /* offset in the file */ 550 { 551 size_t count = 0; 552 size_t entry_offset; 553 file_private_info_t *p_info; 554 555 p_info = (file_private_info_t *) file->private_data; 556 if (*offset != p_info->offset) 557 return -EPIPE; 558 if (p_info->act_area >= p_info->debug_info_snap->nr_areas) 559 return 0; 560 entry_offset = p_info->act_entry_offset; 561 while (count < len) { 562 int formatted_line_residue; 563 int formatted_line_size; 564 int user_buf_residue; 565 size_t copy_size; 566 567 formatted_line_size = debug_format_entry(p_info); 568 formatted_line_residue = formatted_line_size - entry_offset; 569 user_buf_residue = len-count; 570 copy_size = min(user_buf_residue, formatted_line_residue); 571 if (copy_size) { 572 if (copy_to_user(user_buf + count, p_info->temp_buf 573 + entry_offset, copy_size)) 574 return -EFAULT; 575 count += copy_size; 576 entry_offset += copy_size; 577 } 578 if (copy_size == formatted_line_residue) { 579 entry_offset = 0; 580 if (!debug_next_entry(p_info)) 581 goto out; 582 } 583 } 584 out: 585 p_info->offset = *offset + count; 586 p_info->act_entry_offset = entry_offset; 587 *offset = p_info->offset; 588 return count; 589 } 590 591 /* 592 * debug_input: 593 * - called for user write() 594 * - calls input function of view 595 */ 596 static ssize_t debug_input(struct file *file, const char __user *user_buf, 597 size_t length, loff_t *offset) 598 { 599 file_private_info_t *p_info; 600 int rc = 0; 601 602 mutex_lock(&debug_mutex); 603 p_info = ((file_private_info_t *) file->private_data); 604 if (p_info->view->input_proc) { 605 rc = p_info->view->input_proc(p_info->debug_info_org, 606 p_info->view, file, user_buf, 607 length, offset); 608 } else { 609 rc = -EPERM; 610 } 611 mutex_unlock(&debug_mutex); 612 return rc; /* number of input characters */ 613 } 614 615 static file_private_info_t *debug_file_private_alloc(debug_info_t *debug_info, 616 struct debug_view *view) 617 { 618 debug_info_t *debug_info_snapshot; 619 file_private_info_t *p_info; 620 621 /* 622 * Make snapshot of current debug areas to get it consistent. 623 * To copy all the areas is only needed, if we have a view which 624 * formats the debug areas. 625 */ 626 if (!view->format_proc && !view->header_proc) 627 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS); 628 else 629 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS); 630 631 if (!debug_info_snapshot) 632 return NULL; 633 p_info = kmalloc_obj(file_private_info_t); 634 if (!p_info) { 635 debug_info_free(debug_info_snapshot); 636 return NULL; 637 } 638 p_info->offset = 0; 639 p_info->debug_info_snap = debug_info_snapshot; 640 p_info->debug_info_org = debug_info; 641 p_info->view = view; 642 p_info->act_area = 0; 643 p_info->act_page = 0; 644 p_info->act_entry = DEBUG_PROLOG_ENTRY; 645 p_info->act_entry_offset = 0; 646 debug_info_get(debug_info); 647 648 return p_info; 649 } 650 651 /* 652 * debug_open: 653 * - called for user open() 654 * - copies formatted output to private_data area of the file 655 * handle 656 */ 657 static int debug_open(struct inode *inode, struct file *file) 658 { 659 debug_info_t *debug_info; 660 file_private_info_t *p_info; 661 int i, rc = 0; 662 663 mutex_lock(&debug_mutex); 664 debug_info = file_inode(file)->i_private; 665 /* find debug view */ 666 for (i = 0; i < DEBUG_MAX_VIEWS; i++) { 667 if (!debug_info->views[i]) 668 continue; 669 else if (debug_info->debugfs_entries[i] == file->f_path.dentry) 670 goto found; /* found view ! */ 671 } 672 /* no entry found */ 673 rc = -EINVAL; 674 goto out; 675 676 found: 677 p_info = debug_file_private_alloc(debug_info, debug_info->views[i]); 678 if (!p_info) { 679 rc = -ENOMEM; 680 goto out; 681 } 682 file->private_data = p_info; 683 nonseekable_open(inode, file); 684 out: 685 mutex_unlock(&debug_mutex); 686 return rc; 687 } 688 689 static void debug_file_private_free(file_private_info_t *p_info) 690 { 691 if (p_info->debug_info_snap) 692 debug_info_free(p_info->debug_info_snap); 693 debug_info_put(p_info->debug_info_org); 694 kfree(p_info); 695 } 696 697 /* 698 * debug_close: 699 * - called for user close() 700 * - deletes private_data area of the file handle 701 */ 702 static int debug_close(struct inode *inode, struct file *file) 703 { 704 file_private_info_t *p_info; 705 706 p_info = (file_private_info_t *) file->private_data; 707 debug_file_private_free(p_info); 708 file->private_data = NULL; 709 return 0; /* success */ 710 } 711 712 /** 713 * debug_dump - Get a textual representation of debug info, or as much as fits 714 * @id: Debug information to use 715 * @view: View with which to dump the debug information 716 * @buf: Buffer the textual debug data representation is written to 717 * @buf_size: Size of the buffer, including the trailing '\0' byte 718 * @reverse: Go backwards from the last written entry 719 * 720 * This function may be used whenever a textual representation of the debug 721 * information is required without using an s390dbf file. 722 * 723 * Note: It is the callers responsibility to supply a view that is compatible 724 * with the debug information data. 725 * 726 * Return: On success returns the number of bytes written to the buffer not 727 * including the trailing '\0' byte. If bug_size == 0 the function returns 0. 728 * On failure an error code less than 0 is returned. 729 */ 730 ssize_t debug_dump(debug_info_t *id, struct debug_view *view, 731 char *buf, size_t buf_size, bool reverse) 732 { 733 file_private_info_t *p_info; 734 size_t size, offset = 0; 735 736 /* Need space for '\0' byte */ 737 if (buf_size < 1) 738 return 0; 739 buf_size--; 740 741 p_info = debug_file_private_alloc(id, view); 742 if (!p_info) 743 return -ENOMEM; 744 745 /* There is always at least the DEBUG_PROLOG_ENTRY */ 746 do { 747 size = debug_format_entry(p_info); 748 size = min(size, buf_size - offset); 749 memcpy(buf + offset, p_info->temp_buf, size); 750 offset += size; 751 if (offset >= buf_size) 752 break; 753 } while (debug_move_entry(p_info, reverse)); 754 debug_file_private_free(p_info); 755 buf[offset] = '\0'; 756 757 return offset; 758 } 759 760 /* Create debugfs entries and add to internal list. */ 761 static void _debug_register(debug_info_t *id) 762 { 763 /* create root directory */ 764 id->debugfs_root_entry = debugfs_create_dir(id->name, 765 debug_debugfs_root_entry); 766 767 /* append new element to linked list */ 768 if (!debug_area_first) { 769 /* first element in list */ 770 debug_area_first = id; 771 id->prev = NULL; 772 } else { 773 /* append element to end of list */ 774 debug_area_last->next = id; 775 id->prev = debug_area_last; 776 } 777 debug_area_last = id; 778 id->next = NULL; 779 780 debug_register_view(id, &debug_level_view); 781 debug_register_view(id, &debug_flush_view); 782 debug_register_view(id, &debug_pages_view); 783 } 784 785 /** 786 * debug_register_mode() - creates and initializes debug area. 787 * 788 * @name: Name of debug log (e.g. used for debugfs entry) 789 * @pages_per_area: Number of pages, which will be allocated per area 790 * @nr_areas: Number of debug areas 791 * @buf_size: Size of data area in each debug entry 792 * @mode: File mode for debugfs files. E.g. S_IRWXUGO 793 * @uid: User ID for debugfs files. Currently only 0 is supported. 794 * @gid: Group ID for debugfs files. Currently only 0 is supported. 795 * 796 * Return: 797 * - Handle for generated debug area 798 * - %NULL if register failed 799 * 800 * Allocates memory for a debug log. 801 * Must not be called within an interrupt handler. 802 */ 803 debug_info_t *debug_register_mode(const char *name, int pages_per_area, 804 int nr_areas, int buf_size, umode_t mode, 805 uid_t uid, gid_t gid) 806 { 807 debug_info_t *rc = NULL; 808 809 /* Since debugfs currently does not support uid/gid other than root, */ 810 /* we do not allow gid/uid != 0 until we get support for that. */ 811 if ((uid != 0) || (gid != 0)) 812 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n"); 813 BUG_ON(!initialized); 814 815 /* create new debug_info */ 816 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode); 817 if (rc) { 818 mutex_lock(&debug_mutex); 819 _debug_register(rc); 820 mutex_unlock(&debug_mutex); 821 } else { 822 pr_err("Registering debug feature %s failed\n", name); 823 } 824 return rc; 825 } 826 EXPORT_SYMBOL(debug_register_mode); 827 828 /** 829 * debug_register() - creates and initializes debug area with default file mode. 830 * 831 * @name: Name of debug log (e.g. used for debugfs entry) 832 * @pages_per_area: Number of pages, which will be allocated per area 833 * @nr_areas: Number of debug areas 834 * @buf_size: Size of data area in each debug entry 835 * 836 * Return: 837 * - Handle for generated debug area 838 * - %NULL if register failed 839 * 840 * Allocates memory for a debug log. 841 * The debugfs file mode access permissions are read and write for user. 842 * Must not be called within an interrupt handler. 843 */ 844 debug_info_t *debug_register(const char *name, int pages_per_area, 845 int nr_areas, int buf_size) 846 { 847 return debug_register_mode(name, pages_per_area, nr_areas, buf_size, 848 S_IRUSR | S_IWUSR, 0, 0); 849 } 850 EXPORT_SYMBOL(debug_register); 851 852 /** 853 * debug_register_static() - registers a static debug area 854 * 855 * @id: Handle for static debug area 856 * @pages_per_area: Number of pages per area 857 * @nr_areas: Number of debug areas 858 * 859 * Register debug_info_t defined using DEFINE_STATIC_DEBUG_INFO. 860 * 861 * Note: This function is called automatically via an initcall generated by 862 * DEFINE_STATIC_DEBUG_INFO. 863 */ 864 void debug_register_static(debug_info_t *id, int pages_per_area, int nr_areas) 865 { 866 unsigned long flags; 867 debug_info_t *copy; 868 869 if (!initialized) { 870 pr_err("Tried to register debug feature %s too early\n", 871 id->name); 872 return; 873 } 874 875 copy = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size, 876 id->level, ALL_AREAS); 877 if (!copy) { 878 pr_err("Registering debug feature %s failed\n", id->name); 879 880 /* Clear pointers to prevent tracing into released initdata. */ 881 raw_spin_lock_irqsave(&id->lock, flags); 882 id->areas = NULL; 883 id->active_pages = NULL; 884 id->active_entries = NULL; 885 raw_spin_unlock_irqrestore(&id->lock, flags); 886 887 return; 888 } 889 890 /* Replace static trace area with dynamic copy. */ 891 raw_spin_lock_irqsave(&id->lock, flags); 892 debug_events_append(copy, id); 893 debug_areas_swap(id, copy); 894 raw_spin_unlock_irqrestore(&id->lock, flags); 895 896 /* Clear pointers to initdata and discard copy. */ 897 copy->areas = NULL; 898 copy->active_pages = NULL; 899 copy->active_entries = NULL; 900 debug_info_free(copy); 901 902 mutex_lock(&debug_mutex); 903 _debug_register(id); 904 mutex_unlock(&debug_mutex); 905 } 906 907 /* Remove debugfs entries and remove from internal list. */ 908 static void _debug_unregister(debug_info_t *id) 909 { 910 int i; 911 912 for (i = 0; i < DEBUG_MAX_VIEWS; i++) { 913 if (!id->views[i]) 914 continue; 915 debugfs_remove(id->debugfs_entries[i]); 916 } 917 debugfs_remove(id->debugfs_root_entry); 918 if (id == debug_area_first) 919 debug_area_first = id->next; 920 if (id == debug_area_last) 921 debug_area_last = id->prev; 922 if (id->prev) 923 id->prev->next = id->next; 924 if (id->next) 925 id->next->prev = id->prev; 926 } 927 928 /** 929 * debug_unregister() - give back debug area. 930 * 931 * @id: handle for debug log 932 * 933 * Return: 934 * none 935 */ 936 void debug_unregister(debug_info_t *id) 937 { 938 if (!id) 939 return; 940 mutex_lock(&debug_mutex); 941 _debug_unregister(id); 942 mutex_unlock(&debug_mutex); 943 944 debug_info_put(id); 945 } 946 EXPORT_SYMBOL(debug_unregister); 947 948 /* 949 * debug_set_size: 950 * - set area size (number of pages) and number of areas 951 */ 952 static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area) 953 { 954 debug_info_t *new_id; 955 unsigned long flags; 956 957 if (!id || (nr_areas <= 0) || (pages_per_area < 0)) 958 return -EINVAL; 959 960 new_id = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size, 961 id->level, ALL_AREAS); 962 if (!new_id) { 963 pr_info("Allocating memory for %i pages failed\n", 964 pages_per_area); 965 return -ENOMEM; 966 } 967 968 raw_spin_lock_irqsave(&id->lock, flags); 969 debug_events_append(new_id, id); 970 debug_areas_swap(new_id, id); 971 raw_spin_unlock_irqrestore(&id->lock, flags); 972 debug_info_free(new_id); 973 pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area); 974 975 return 0; 976 } 977 978 /** 979 * debug_set_level() - Sets new actual debug level if new_level is valid. 980 * 981 * @id: handle for debug log 982 * @new_level: new debug level 983 * 984 * Return: 985 * none 986 */ 987 void debug_set_level(debug_info_t *id, int new_level) 988 { 989 unsigned long flags; 990 991 if (!id) 992 return; 993 994 if (new_level == DEBUG_OFF_LEVEL) { 995 pr_info("%s: switched off\n", id->name); 996 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) { 997 pr_info("%s: level %i is out of range (%i - %i)\n", 998 id->name, new_level, 0, DEBUG_MAX_LEVEL); 999 return; 1000 } 1001 1002 raw_spin_lock_irqsave(&id->lock, flags); 1003 id->level = new_level; 1004 raw_spin_unlock_irqrestore(&id->lock, flags); 1005 } 1006 EXPORT_SYMBOL(debug_set_level); 1007 1008 /* 1009 * proceed_active_entry: 1010 * - set active entry to next in the ring buffer 1011 */ 1012 static inline void proceed_active_entry(debug_info_t *id) 1013 { 1014 if ((id->active_entries[id->active_area] += id->entry_size) 1015 > (PAGE_SIZE - id->entry_size)) { 1016 id->active_entries[id->active_area] = 0; 1017 id->active_pages[id->active_area] = 1018 (id->active_pages[id->active_area] + 1) % 1019 id->pages_per_area; 1020 } 1021 } 1022 1023 /* 1024 * proceed_active_area: 1025 * - set active area to next in the ring buffer 1026 */ 1027 static inline void proceed_active_area(debug_info_t *id) 1028 { 1029 id->active_area++; 1030 id->active_area = id->active_area % id->nr_areas; 1031 } 1032 1033 /* 1034 * get_active_entry: 1035 */ 1036 static inline debug_entry_t *get_active_entry(debug_info_t *id) 1037 { 1038 return (debug_entry_t *) (((char *) id->areas[id->active_area] 1039 [id->active_pages[id->active_area]]) + 1040 id->active_entries[id->active_area]); 1041 } 1042 1043 /* Swap debug areas of a and b. */ 1044 static void debug_areas_swap(debug_info_t *a, debug_info_t *b) 1045 { 1046 swap(a->nr_areas, b->nr_areas); 1047 swap(a->pages_per_area, b->pages_per_area); 1048 swap(a->areas, b->areas); 1049 swap(a->active_area, b->active_area); 1050 swap(a->active_pages, b->active_pages); 1051 swap(a->active_entries, b->active_entries); 1052 } 1053 1054 /* Append all debug events in active area from source to destination log. */ 1055 static void debug_events_append(debug_info_t *dest, debug_info_t *src) 1056 { 1057 debug_entry_t *from, *to, *last; 1058 1059 if (!src->areas || !dest->areas) 1060 return; 1061 1062 /* Loop over all entries in src, starting with oldest. */ 1063 from = get_active_entry(src); 1064 last = from; 1065 do { 1066 if (from->clock != 0LL) { 1067 to = get_active_entry(dest); 1068 memset(to, 0, dest->entry_size); 1069 memcpy(to, from, min(src->entry_size, 1070 dest->entry_size)); 1071 proceed_active_entry(dest); 1072 } 1073 1074 proceed_active_entry(src); 1075 from = get_active_entry(src); 1076 } while (from != last); 1077 } 1078 1079 /* 1080 * debug_finish_entry: 1081 * - set timestamp, caller address, cpu number etc. 1082 */ 1083 1084 static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active, 1085 int level, int exception) 1086 { 1087 unsigned long timestamp; 1088 union tod_clock clk; 1089 1090 store_tod_clock_ext(&clk); 1091 timestamp = clk.us; 1092 timestamp -= TOD_UNIX_EPOCH >> 12; 1093 active->clock = timestamp; 1094 active->cpu = smp_processor_id(); 1095 active->caller = __builtin_return_address(0); 1096 active->exception = exception; 1097 active->level = level; 1098 proceed_active_entry(id); 1099 if (exception) 1100 proceed_active_area(id); 1101 } 1102 1103 static int debug_stoppable = 1; 1104 static int debug_active = 1; 1105 1106 #define CTL_S390DBF_STOPPABLE 5678 1107 #define CTL_S390DBF_ACTIVE 5679 1108 1109 /* 1110 * proc handler for the running debug_active sysctl 1111 * always allow read, allow write only if debug_stoppable is set or 1112 * if debug_active is already off 1113 */ 1114 static int s390dbf_procactive(const struct ctl_table *table, int write, 1115 void *buffer, size_t *lenp, loff_t *ppos) 1116 { 1117 if (!write || debug_stoppable || !debug_active) 1118 return proc_dointvec(table, write, buffer, lenp, ppos); 1119 else 1120 return 0; 1121 } 1122 1123 static const struct ctl_table s390dbf_table[] = { 1124 { 1125 .procname = "debug_stoppable", 1126 .data = &debug_stoppable, 1127 .maxlen = sizeof(int), 1128 .mode = S_IRUGO | S_IWUSR, 1129 .proc_handler = proc_dointvec, 1130 }, 1131 { 1132 .procname = "debug_active", 1133 .data = &debug_active, 1134 .maxlen = sizeof(int), 1135 .mode = S_IRUGO | S_IWUSR, 1136 .proc_handler = s390dbf_procactive, 1137 }, 1138 }; 1139 1140 static struct ctl_table_header *s390dbf_sysctl_header; 1141 1142 /** 1143 * debug_stop_all() - stops the debug feature if stopping is allowed. 1144 * 1145 * Return: 1146 * - none 1147 * 1148 * Currently used in case of a kernel oops. 1149 */ 1150 void debug_stop_all(void) 1151 { 1152 if (debug_stoppable) 1153 debug_active = 0; 1154 } 1155 EXPORT_SYMBOL(debug_stop_all); 1156 1157 /** 1158 * debug_set_critical() - event/exception functions try lock instead of spin. 1159 * 1160 * Return: 1161 * - none 1162 * 1163 * Currently used in case of stopping all CPUs but the current one. 1164 * Once in this state, functions to write a debug entry for an 1165 * event or exception no longer spin on the debug area lock, 1166 * but only try to get it and fail if they do not get the lock. 1167 */ 1168 void debug_set_critical(void) 1169 { 1170 debug_critical = 1; 1171 } 1172 1173 /* 1174 * debug_event_common: 1175 * - write debug entry with given size 1176 */ 1177 debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf, 1178 int len) 1179 { 1180 debug_entry_t *active; 1181 unsigned long flags; 1182 1183 if (!debug_active || !id->areas) 1184 return NULL; 1185 if (debug_critical) { 1186 if (!raw_spin_trylock_irqsave(&id->lock, flags)) 1187 return NULL; 1188 } else { 1189 raw_spin_lock_irqsave(&id->lock, flags); 1190 } 1191 do { 1192 active = get_active_entry(id); 1193 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size)); 1194 if (len < id->buf_size) 1195 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len); 1196 debug_finish_entry(id, active, level, 0); 1197 len -= id->buf_size; 1198 buf += id->buf_size; 1199 } while (len > 0); 1200 1201 raw_spin_unlock_irqrestore(&id->lock, flags); 1202 return active; 1203 } 1204 EXPORT_SYMBOL(debug_event_common); 1205 1206 /* 1207 * debug_exception_common: 1208 * - write debug entry with given size and switch to next debug area 1209 */ 1210 debug_entry_t *debug_exception_common(debug_info_t *id, int level, 1211 const void *buf, int len) 1212 { 1213 debug_entry_t *active; 1214 unsigned long flags; 1215 1216 if (!debug_active || !id->areas) 1217 return NULL; 1218 if (debug_critical) { 1219 if (!raw_spin_trylock_irqsave(&id->lock, flags)) 1220 return NULL; 1221 } else { 1222 raw_spin_lock_irqsave(&id->lock, flags); 1223 } 1224 do { 1225 active = get_active_entry(id); 1226 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size)); 1227 if (len < id->buf_size) 1228 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len); 1229 debug_finish_entry(id, active, level, len <= id->buf_size); 1230 len -= id->buf_size; 1231 buf += id->buf_size; 1232 } while (len > 0); 1233 1234 raw_spin_unlock_irqrestore(&id->lock, flags); 1235 return active; 1236 } 1237 EXPORT_SYMBOL(debug_exception_common); 1238 1239 /* 1240 * counts arguments in format string for sprintf view 1241 */ 1242 static inline int debug_count_numargs(char *string) 1243 { 1244 int numargs = 0; 1245 1246 while (*string) { 1247 if (*string++ == '%') 1248 numargs++; 1249 } 1250 return numargs; 1251 } 1252 1253 /* 1254 * debug_sprintf_event: 1255 */ 1256 debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...) 1257 { 1258 debug_sprintf_entry_t *curr_event; 1259 debug_entry_t *active; 1260 unsigned long flags; 1261 int numargs, idx; 1262 va_list ap; 1263 1264 if (!debug_active || !id->areas) 1265 return NULL; 1266 numargs = debug_count_numargs(string); 1267 1268 if (debug_critical) { 1269 if (!raw_spin_trylock_irqsave(&id->lock, flags)) 1270 return NULL; 1271 } else { 1272 raw_spin_lock_irqsave(&id->lock, flags); 1273 } 1274 active = get_active_entry(id); 1275 curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active); 1276 va_start(ap, string); 1277 curr_event->string = string; 1278 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++) 1279 curr_event->args[idx] = va_arg(ap, long); 1280 va_end(ap); 1281 debug_finish_entry(id, active, level, 0); 1282 raw_spin_unlock_irqrestore(&id->lock, flags); 1283 1284 return active; 1285 } 1286 EXPORT_SYMBOL(__debug_sprintf_event); 1287 1288 /* 1289 * debug_sprintf_exception: 1290 */ 1291 debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...) 1292 { 1293 debug_sprintf_entry_t *curr_event; 1294 debug_entry_t *active; 1295 unsigned long flags; 1296 int numargs, idx; 1297 va_list ap; 1298 1299 if (!debug_active || !id->areas) 1300 return NULL; 1301 1302 numargs = debug_count_numargs(string); 1303 1304 if (debug_critical) { 1305 if (!raw_spin_trylock_irqsave(&id->lock, flags)) 1306 return NULL; 1307 } else { 1308 raw_spin_lock_irqsave(&id->lock, flags); 1309 } 1310 active = get_active_entry(id); 1311 curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active); 1312 va_start(ap, string); 1313 curr_event->string = string; 1314 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++) 1315 curr_event->args[idx] = va_arg(ap, long); 1316 va_end(ap); 1317 debug_finish_entry(id, active, level, 1); 1318 raw_spin_unlock_irqrestore(&id->lock, flags); 1319 1320 return active; 1321 } 1322 EXPORT_SYMBOL(__debug_sprintf_exception); 1323 1324 /** 1325 * debug_register_view() - registers new debug view and creates debugfs 1326 * dir entry 1327 * 1328 * @id: handle for debug log 1329 * @view: pointer to debug view struct 1330 * 1331 * Return: 1332 * - 0 : ok 1333 * - < 0: Error 1334 */ 1335 int debug_register_view(debug_info_t *id, struct debug_view *view) 1336 { 1337 unsigned long flags; 1338 struct dentry *pde; 1339 umode_t mode; 1340 int rc = 0; 1341 int i; 1342 1343 if (!id) 1344 goto out; 1345 mode = (id->mode | S_IFREG) & ~S_IXUGO; 1346 if (!(view->prolog_proc || view->format_proc || view->header_proc)) 1347 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH); 1348 if (!view->input_proc) 1349 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); 1350 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry, 1351 id, &debug_file_ops); 1352 raw_spin_lock_irqsave(&id->lock, flags); 1353 for (i = 0; i < DEBUG_MAX_VIEWS; i++) { 1354 if (!id->views[i]) 1355 break; 1356 } 1357 if (i == DEBUG_MAX_VIEWS) { 1358 rc = -1; 1359 } else { 1360 id->views[i] = view; 1361 id->debugfs_entries[i] = pde; 1362 } 1363 raw_spin_unlock_irqrestore(&id->lock, flags); 1364 if (rc) { 1365 pr_err("Registering view %s/%s would exceed the maximum " 1366 "number of views %i\n", id->name, view->name, i); 1367 debugfs_remove(pde); 1368 } 1369 out: 1370 return rc; 1371 } 1372 EXPORT_SYMBOL(debug_register_view); 1373 1374 /** 1375 * debug_unregister_view() - unregisters debug view and removes debugfs 1376 * dir entry 1377 * 1378 * @id: handle for debug log 1379 * @view: pointer to debug view struct 1380 * 1381 * Return: 1382 * - 0 : ok 1383 * - < 0: Error 1384 */ 1385 int debug_unregister_view(debug_info_t *id, struct debug_view *view) 1386 { 1387 struct dentry *dentry = NULL; 1388 unsigned long flags; 1389 int i, rc = 0; 1390 1391 if (!id) 1392 goto out; 1393 raw_spin_lock_irqsave(&id->lock, flags); 1394 for (i = 0; i < DEBUG_MAX_VIEWS; i++) { 1395 if (id->views[i] == view) 1396 break; 1397 } 1398 if (i == DEBUG_MAX_VIEWS) { 1399 rc = -1; 1400 } else { 1401 dentry = id->debugfs_entries[i]; 1402 id->views[i] = NULL; 1403 id->debugfs_entries[i] = NULL; 1404 } 1405 raw_spin_unlock_irqrestore(&id->lock, flags); 1406 debugfs_remove(dentry); 1407 out: 1408 return rc; 1409 } 1410 EXPORT_SYMBOL(debug_unregister_view); 1411 1412 static inline char *debug_get_user_string(const char __user *user_buf, 1413 size_t user_len) 1414 { 1415 char *buffer; 1416 1417 buffer = memdup_user_nul(user_buf, user_len); 1418 if (IS_ERR(buffer)) 1419 return buffer; 1420 /* got the string, now strip linefeed. */ 1421 if (buffer[user_len - 1] == '\n') 1422 buffer[user_len - 1] = 0; 1423 return buffer; 1424 } 1425 1426 static inline int debug_get_uint(char *buf) 1427 { 1428 int rc; 1429 1430 buf = skip_spaces(buf); 1431 rc = simple_strtoul(buf, &buf, 10); 1432 if (*buf) 1433 rc = -EINVAL; 1434 return rc; 1435 } 1436 1437 /* 1438 * functions for debug-views 1439 *********************************** 1440 */ 1441 1442 /* 1443 * prints out actual debug level 1444 */ 1445 1446 static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view, 1447 char *out_buf, size_t out_buf_size) 1448 { 1449 return scnprintf(out_buf, out_buf_size, "%i\n", id->pages_per_area); 1450 } 1451 1452 /* 1453 * reads new size (number of pages per debug area) 1454 */ 1455 1456 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view, 1457 struct file *file, const char __user *user_buf, 1458 size_t user_len, loff_t *offset) 1459 { 1460 int rc, new_pages; 1461 char *str; 1462 1463 if (user_len > 0x10000) 1464 user_len = 0x10000; 1465 if (*offset != 0) { 1466 rc = -EPIPE; 1467 goto out; 1468 } 1469 str = debug_get_user_string(user_buf, user_len); 1470 if (IS_ERR(str)) { 1471 rc = PTR_ERR(str); 1472 goto out; 1473 } 1474 new_pages = debug_get_uint(str); 1475 if (new_pages < 0) { 1476 rc = -EINVAL; 1477 goto free_str; 1478 } 1479 rc = debug_set_size(id, id->nr_areas, new_pages); 1480 if (rc != 0) { 1481 rc = -EINVAL; 1482 goto free_str; 1483 } 1484 rc = user_len; 1485 free_str: 1486 kfree(str); 1487 out: 1488 *offset += user_len; 1489 return rc; /* number of input characters */ 1490 } 1491 1492 /* 1493 * prints out actual debug level 1494 */ 1495 static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view, 1496 char *out_buf, size_t out_buf_size) 1497 { 1498 int rc = 0; 1499 1500 if (id->level == DEBUG_OFF_LEVEL) 1501 rc = scnprintf(out_buf, out_buf_size, "-\n"); 1502 else 1503 rc = scnprintf(out_buf, out_buf_size, "%i\n", id->level); 1504 return rc; 1505 } 1506 1507 /* 1508 * reads new debug level 1509 */ 1510 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view, 1511 struct file *file, const char __user *user_buf, 1512 size_t user_len, loff_t *offset) 1513 { 1514 int rc, new_level; 1515 char *str; 1516 1517 if (user_len > 0x10000) 1518 user_len = 0x10000; 1519 if (*offset != 0) { 1520 rc = -EPIPE; 1521 goto out; 1522 } 1523 str = debug_get_user_string(user_buf, user_len); 1524 if (IS_ERR(str)) { 1525 rc = PTR_ERR(str); 1526 goto out; 1527 } 1528 if (str[0] == '-') { 1529 debug_set_level(id, DEBUG_OFF_LEVEL); 1530 rc = user_len; 1531 goto free_str; 1532 } else { 1533 new_level = debug_get_uint(str); 1534 } 1535 if (new_level < 0) { 1536 pr_warn("%s is not a valid level for a debug feature\n", str); 1537 rc = -EINVAL; 1538 } else { 1539 debug_set_level(id, new_level); 1540 rc = user_len; 1541 } 1542 free_str: 1543 kfree(str); 1544 out: 1545 *offset += user_len; 1546 return rc; /* number of input characters */ 1547 } 1548 1549 /* 1550 * flushes debug areas 1551 */ 1552 static void debug_flush(debug_info_t *id, int area) 1553 { 1554 unsigned long flags; 1555 int i, j; 1556 1557 if (!id || !id->areas) 1558 return; 1559 raw_spin_lock_irqsave(&id->lock, flags); 1560 if (area == DEBUG_FLUSH_ALL) { 1561 id->active_area = 0; 1562 memset(id->active_entries, 0, id->nr_areas * sizeof(int)); 1563 for (i = 0; i < id->nr_areas; i++) { 1564 id->active_pages[i] = 0; 1565 for (j = 0; j < id->pages_per_area; j++) 1566 memset(id->areas[i][j], 0, PAGE_SIZE); 1567 } 1568 } else if (area >= 0 && area < id->nr_areas) { 1569 id->active_entries[area] = 0; 1570 id->active_pages[area] = 0; 1571 for (i = 0; i < id->pages_per_area; i++) 1572 memset(id->areas[area][i], 0, PAGE_SIZE); 1573 } 1574 raw_spin_unlock_irqrestore(&id->lock, flags); 1575 } 1576 1577 /* 1578 * view function: flushes debug areas 1579 */ 1580 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view, 1581 struct file *file, const char __user *user_buf, 1582 size_t user_len, loff_t *offset) 1583 { 1584 char input_buf[1]; 1585 int rc = user_len; 1586 1587 if (user_len > 0x10000) 1588 user_len = 0x10000; 1589 if (*offset != 0) { 1590 rc = -EPIPE; 1591 goto out; 1592 } 1593 if (copy_from_user(input_buf, user_buf, 1)) { 1594 rc = -EFAULT; 1595 goto out; 1596 } 1597 if (input_buf[0] == '-') { 1598 debug_flush(id, DEBUG_FLUSH_ALL); 1599 goto out; 1600 } 1601 if (isdigit(input_buf[0])) { 1602 int area = ((int) input_buf[0] - (int) '0'); 1603 1604 debug_flush(id, area); 1605 goto out; 1606 } 1607 1608 pr_info("Flushing debug data failed because %c is not a valid " 1609 "area\n", input_buf[0]); 1610 1611 out: 1612 *offset += user_len; 1613 return rc; /* number of input characters */ 1614 } 1615 1616 /* 1617 * prints debug data in hex/ascii format 1618 */ 1619 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view, 1620 char *out_buf, size_t out_buf_size, const char *in_buf) 1621 { 1622 int i, rc = 0; 1623 1624 for (i = 0; i < id->buf_size; i++) { 1625 rc += scnprintf(out_buf + rc, out_buf_size - rc, 1626 "%02x ", ((unsigned char *)in_buf)[i]); 1627 } 1628 rc += scnprintf(out_buf + rc, out_buf_size - rc, "| "); 1629 for (i = 0; i < id->buf_size; i++) { 1630 unsigned char c = in_buf[i]; 1631 1632 if (isascii(c) && isprint(c)) 1633 rc += scnprintf(out_buf + rc, out_buf_size - rc, "%c", c); 1634 else 1635 rc += scnprintf(out_buf + rc, out_buf_size - rc, "."); 1636 } 1637 rc += scnprintf(out_buf + rc, out_buf_size - rc, "\n"); 1638 return rc; 1639 } 1640 1641 /* 1642 * prints header for debug entry 1643 */ 1644 int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view, 1645 int area, debug_entry_t *entry, char *out_buf, 1646 size_t out_buf_size) 1647 { 1648 unsigned long sec, usec; 1649 unsigned long caller; 1650 unsigned int level; 1651 char *except_str; 1652 int rc = 0; 1653 1654 level = entry->level; 1655 sec = entry->clock; 1656 usec = do_div(sec, USEC_PER_SEC); 1657 1658 if (entry->exception) 1659 except_str = "*"; 1660 else 1661 except_str = "-"; 1662 caller = (unsigned long) entry->caller; 1663 rc += scnprintf(out_buf, out_buf_size, "%02i %011ld:%06lu %1u %1s %04u %px ", 1664 area, sec, usec, level, except_str, 1665 entry->cpu, (void *)caller); 1666 return rc; 1667 } 1668 EXPORT_SYMBOL(debug_dflt_header_fn); 1669 1670 /* 1671 * prints debug data sprintf-formatted: 1672 * debug_sprintf_event/exception calls must be used together with this view 1673 */ 1674 1675 #define DEBUG_SPRINTF_MAX_ARGS 10 1676 1677 int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view, 1678 char *out_buf, size_t out_buf_size, const char *inbuf) 1679 { 1680 debug_sprintf_entry_t *curr_event = (debug_sprintf_entry_t *)inbuf; 1681 int num_longs, num_used_args = 0, i, rc = 0; 1682 int index[DEBUG_SPRINTF_MAX_ARGS]; 1683 1684 /* count of longs fit into one entry */ 1685 num_longs = id->buf_size / sizeof(long); 1686 1687 if (num_longs < 1) 1688 goto out; /* bufsize of entry too small */ 1689 if (num_longs == 1) { 1690 /* no args, we use only the string */ 1691 rc = strscpy(out_buf, curr_event->string, out_buf_size); 1692 if (rc == -E2BIG) 1693 rc = out_buf_size; 1694 goto out; 1695 } 1696 1697 /* number of arguments used for sprintf (without the format string) */ 1698 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1)); 1699 1700 memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int)); 1701 1702 for (i = 0; i < num_used_args; i++) 1703 index[i] = i; 1704 1705 rc = scnprintf(out_buf, out_buf_size, 1706 curr_event->string, curr_event->args[index[0]], 1707 curr_event->args[index[1]], curr_event->args[index[2]], 1708 curr_event->args[index[3]], curr_event->args[index[4]], 1709 curr_event->args[index[5]], curr_event->args[index[6]], 1710 curr_event->args[index[7]], curr_event->args[index[8]], 1711 curr_event->args[index[9]]); 1712 out: 1713 return rc; 1714 } 1715 EXPORT_SYMBOL(debug_sprintf_format_fn); 1716 1717 /* 1718 * debug_init: 1719 * - is called exactly once to initialize the debug feature 1720 */ 1721 static int __init debug_init(void) 1722 { 1723 s390dbf_sysctl_header = register_sysctl("s390dbf", s390dbf_table); 1724 mutex_lock(&debug_mutex); 1725 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL); 1726 initialized = 1; 1727 mutex_unlock(&debug_mutex); 1728 return 0; 1729 } 1730 postcore_initcall(debug_init); 1731