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