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