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