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