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