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