at commit afb1ecfe

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