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