GNU Linux-libre 4.19.264-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
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:
653  * - Creates and initializes debug area for the caller
654  *   The mode parameter allows to specify access rights for the s390dbf files
655  * - Returns handle for debug area
656  */
657 debug_info_t *debug_register_mode(const char *name, int pages_per_area,
658                                   int nr_areas, int buf_size, umode_t mode,
659                                   uid_t uid, gid_t gid)
660 {
661         debug_info_t *rc = NULL;
662
663         /* Since debugfs currently does not support uid/gid other than root, */
664         /* we do not allow gid/uid != 0 until we get support for that. */
665         if ((uid != 0) || (gid != 0))
666                 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
667         BUG_ON(!initialized);
668         mutex_lock(&debug_mutex);
669
670         /* create new debug_info */
671         rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
672         if (!rc)
673                 goto out;
674         debug_register_view(rc, &debug_level_view);
675         debug_register_view(rc, &debug_flush_view);
676         debug_register_view(rc, &debug_pages_view);
677 out:
678         if (!rc)
679                 pr_err("Registering debug feature %s failed\n", name);
680         mutex_unlock(&debug_mutex);
681         return rc;
682 }
683 EXPORT_SYMBOL(debug_register_mode);
684
685 /*
686  * debug_register:
687  * - creates and initializes debug area for the caller
688  * - returns handle for debug area
689  */
690 debug_info_t *debug_register(const char *name, int pages_per_area,
691                              int nr_areas, int buf_size)
692 {
693         return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
694                                    S_IRUSR | S_IWUSR, 0, 0);
695 }
696 EXPORT_SYMBOL(debug_register);
697
698 /*
699  * debug_unregister:
700  * - give back debug area
701  */
702 void debug_unregister(debug_info_t *id)
703 {
704         if (!id)
705                 return;
706         mutex_lock(&debug_mutex);
707         debug_info_put(id);
708         mutex_unlock(&debug_mutex);
709 }
710 EXPORT_SYMBOL(debug_unregister);
711
712 /*
713  * debug_set_size:
714  * - set area size (number of pages) and number of areas
715  */
716 static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
717 {
718         debug_entry_t ***new_areas;
719         unsigned long flags;
720         int rc = 0;
721
722         if (!id || (nr_areas <= 0) || (pages_per_area < 0))
723                 return -EINVAL;
724         if (pages_per_area > 0) {
725                 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
726                 if (!new_areas) {
727                         pr_info("Allocating memory for %i pages failed\n",
728                                 pages_per_area);
729                         rc = -ENOMEM;
730                         goto out;
731                 }
732         } else {
733                 new_areas = NULL;
734         }
735         spin_lock_irqsave(&id->lock, flags);
736         debug_areas_free(id);
737         id->areas = new_areas;
738         id->nr_areas = nr_areas;
739         id->pages_per_area = pages_per_area;
740         id->active_area = 0;
741         memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
742         memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
743         spin_unlock_irqrestore(&id->lock, flags);
744         pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
745 out:
746         return rc;
747 }
748
749 /*
750  * debug_set_level:
751  * - set actual debug level
752  */
753 void debug_set_level(debug_info_t *id, int new_level)
754 {
755         unsigned long flags;
756
757         if (!id)
758                 return;
759         spin_lock_irqsave(&id->lock, flags);
760         if (new_level == DEBUG_OFF_LEVEL) {
761                 id->level = DEBUG_OFF_LEVEL;
762                 pr_info("%s: switched off\n", id->name);
763         } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
764                 pr_info("%s: level %i is out of range (%i - %i)\n",
765                         id->name, new_level, 0, DEBUG_MAX_LEVEL);
766         } else {
767                 id->level = new_level;
768         }
769         spin_unlock_irqrestore(&id->lock, flags);
770 }
771 EXPORT_SYMBOL(debug_set_level);
772
773 /*
774  * proceed_active_entry:
775  * - set active entry to next in the ring buffer
776  */
777 static inline void proceed_active_entry(debug_info_t *id)
778 {
779         if ((id->active_entries[id->active_area] += id->entry_size)
780             > (PAGE_SIZE - id->entry_size)) {
781                 id->active_entries[id->active_area] = 0;
782                 id->active_pages[id->active_area] =
783                         (id->active_pages[id->active_area] + 1) %
784                         id->pages_per_area;
785         }
786 }
787
788 /*
789  * proceed_active_area:
790  * - set active area to next in the ring buffer
791  */
792 static inline void proceed_active_area(debug_info_t *id)
793 {
794         id->active_area++;
795         id->active_area = id->active_area % id->nr_areas;
796 }
797
798 /*
799  * get_active_entry:
800  */
801 static inline debug_entry_t *get_active_entry(debug_info_t *id)
802 {
803         return (debug_entry_t *) (((char *) id->areas[id->active_area]
804                                    [id->active_pages[id->active_area]]) +
805                                   id->active_entries[id->active_area]);
806 }
807
808 /*
809  * debug_finish_entry:
810  * - set timestamp, caller address, cpu number etc.
811  */
812
813 static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
814                                       int level, int exception)
815 {
816         active->id.stck = get_tod_clock_fast() -
817                 *(unsigned long long *) &tod_clock_base[1];
818         active->id.fields.cpuid = smp_processor_id();
819         active->caller = __builtin_return_address(0);
820         active->id.fields.exception = exception;
821         active->id.fields.level = level;
822         proceed_active_entry(id);
823         if (exception)
824                 proceed_active_area(id);
825 }
826
827 static int debug_stoppable = 1;
828 static int debug_active = 1;
829
830 #define CTL_S390DBF_STOPPABLE 5678
831 #define CTL_S390DBF_ACTIVE 5679
832
833 /*
834  * proc handler for the running debug_active sysctl
835  * always allow read, allow write only if debug_stoppable is set or
836  * if debug_active is already off
837  */
838 static int s390dbf_procactive(struct ctl_table *table, int write,
839                               void __user *buffer, size_t *lenp, loff_t *ppos)
840 {
841         if (!write || debug_stoppable || !debug_active)
842                 return proc_dointvec(table, write, buffer, lenp, ppos);
843         else
844                 return 0;
845 }
846
847 static struct ctl_table s390dbf_table[] = {
848         {
849                 .procname       = "debug_stoppable",
850                 .data           = &debug_stoppable,
851                 .maxlen         = sizeof(int),
852                 .mode           = S_IRUGO | S_IWUSR,
853                 .proc_handler   = proc_dointvec,
854         },
855         {
856                 .procname       = "debug_active",
857                 .data           = &debug_active,
858                 .maxlen         = sizeof(int),
859                 .mode           = S_IRUGO | S_IWUSR,
860                 .proc_handler   = s390dbf_procactive,
861         },
862         { }
863 };
864
865 static struct ctl_table s390dbf_dir_table[] = {
866         {
867                 .procname       = "s390dbf",
868                 .maxlen         = 0,
869                 .mode           = S_IRUGO | S_IXUGO,
870                 .child          = s390dbf_table,
871         },
872         { }
873 };
874
875 static struct ctl_table_header *s390dbf_sysctl_header;
876
877 void debug_stop_all(void)
878 {
879         if (debug_stoppable)
880                 debug_active = 0;
881 }
882 EXPORT_SYMBOL(debug_stop_all);
883
884 void debug_set_critical(void)
885 {
886         debug_critical = 1;
887 }
888
889 /*
890  * debug_event_common:
891  * - write debug entry with given size
892  */
893 debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
894                                   int len)
895 {
896         debug_entry_t *active;
897         unsigned long flags;
898
899         if (!debug_active || !id->areas)
900                 return NULL;
901         if (debug_critical) {
902                 if (!spin_trylock_irqsave(&id->lock, flags))
903                         return NULL;
904         } else {
905                 spin_lock_irqsave(&id->lock, flags);
906         }
907         do {
908                 active = get_active_entry(id);
909                 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
910                 if (len < id->buf_size)
911                         memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
912                 debug_finish_entry(id, active, level, 0);
913                 len -= id->buf_size;
914                 buf += id->buf_size;
915         } while (len > 0);
916
917         spin_unlock_irqrestore(&id->lock, flags);
918         return active;
919 }
920 EXPORT_SYMBOL(debug_event_common);
921
922 /*
923  * debug_exception_common:
924  * - write debug entry with given size and switch to next debug area
925  */
926 debug_entry_t *debug_exception_common(debug_info_t *id, int level,
927                                       const void *buf, int len)
928 {
929         debug_entry_t *active;
930         unsigned long flags;
931
932         if (!debug_active || !id->areas)
933                 return NULL;
934         if (debug_critical) {
935                 if (!spin_trylock_irqsave(&id->lock, flags))
936                         return NULL;
937         } else {
938                 spin_lock_irqsave(&id->lock, flags);
939         }
940         do {
941                 active = get_active_entry(id);
942                 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
943                 if (len < id->buf_size)
944                         memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
945                 debug_finish_entry(id, active, level, len <= id->buf_size);
946                 len -= id->buf_size;
947                 buf += id->buf_size;
948         } while (len > 0);
949
950         spin_unlock_irqrestore(&id->lock, flags);
951         return active;
952 }
953 EXPORT_SYMBOL(debug_exception_common);
954
955 /*
956  * counts arguments in format string for sprintf view
957  */
958 static inline int debug_count_numargs(char *string)
959 {
960         int numargs = 0;
961
962         while (*string) {
963                 if (*string++ == '%')
964                         numargs++;
965         }
966         return numargs;
967 }
968
969 /*
970  * debug_sprintf_event:
971  */
972 debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
973 {
974         debug_sprintf_entry_t *curr_event;
975         debug_entry_t *active;
976         unsigned long flags;
977         int numargs, idx;
978         va_list ap;
979
980         if (!debug_active || !id->areas)
981                 return NULL;
982         numargs = debug_count_numargs(string);
983
984         if (debug_critical) {
985                 if (!spin_trylock_irqsave(&id->lock, flags))
986                         return NULL;
987         } else {
988                 spin_lock_irqsave(&id->lock, flags);
989         }
990         active = get_active_entry(id);
991         curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
992         va_start(ap, string);
993         curr_event->string = string;
994         for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
995                 curr_event->args[idx] = va_arg(ap, long);
996         va_end(ap);
997         debug_finish_entry(id, active, level, 0);
998         spin_unlock_irqrestore(&id->lock, flags);
999
1000         return active;
1001 }
1002 EXPORT_SYMBOL(__debug_sprintf_event);
1003
1004 /*
1005  * debug_sprintf_exception:
1006  */
1007 debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
1008 {
1009         debug_sprintf_entry_t *curr_event;
1010         debug_entry_t *active;
1011         unsigned long flags;
1012         int numargs, idx;
1013         va_list ap;
1014
1015         if (!debug_active || !id->areas)
1016                 return NULL;
1017
1018         numargs = debug_count_numargs(string);
1019
1020         if (debug_critical) {
1021                 if (!spin_trylock_irqsave(&id->lock, flags))
1022                         return NULL;
1023         } else {
1024                 spin_lock_irqsave(&id->lock, flags);
1025         }
1026         active = get_active_entry(id);
1027         curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
1028         va_start(ap, string);
1029         curr_event->string = string;
1030         for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1031                 curr_event->args[idx] = va_arg(ap, long);
1032         va_end(ap);
1033         debug_finish_entry(id, active, level, 1);
1034         spin_unlock_irqrestore(&id->lock, flags);
1035
1036         return active;
1037 }
1038 EXPORT_SYMBOL(__debug_sprintf_exception);
1039
1040 /*
1041  * debug_register_view:
1042  */
1043 int debug_register_view(debug_info_t *id, struct debug_view *view)
1044 {
1045         unsigned long flags;
1046         struct dentry *pde;
1047         umode_t mode;
1048         int rc = 0;
1049         int i;
1050
1051         if (!id)
1052                 goto out;
1053         mode = (id->mode | S_IFREG) & ~S_IXUGO;
1054         if (!(view->prolog_proc || view->format_proc || view->header_proc))
1055                 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1056         if (!view->input_proc)
1057                 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1058         pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1059                                   id, &debug_file_ops);
1060         if (!pde) {
1061                 pr_err("Registering view %s/%s failed due to out of "
1062                        "memory\n", id->name, view->name);
1063                 rc = -1;
1064                 goto out;
1065         }
1066         spin_lock_irqsave(&id->lock, flags);
1067         for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1068                 if (!id->views[i])
1069                         break;
1070         }
1071         if (i == DEBUG_MAX_VIEWS) {
1072                 pr_err("Registering view %s/%s would exceed the maximum "
1073                        "number of views %i\n", id->name, view->name, i);
1074                 rc = -1;
1075         } else {
1076                 id->views[i] = view;
1077                 id->debugfs_entries[i] = pde;
1078         }
1079         spin_unlock_irqrestore(&id->lock, flags);
1080         if (rc)
1081                 debugfs_remove(pde);
1082 out:
1083         return rc;
1084 }
1085 EXPORT_SYMBOL(debug_register_view);
1086
1087 /*
1088  * debug_unregister_view:
1089  */
1090 int debug_unregister_view(debug_info_t *id, struct debug_view *view)
1091 {
1092         struct dentry *dentry = NULL;
1093         unsigned long flags;
1094         int i, rc = 0;
1095
1096         if (!id)
1097                 goto out;
1098         spin_lock_irqsave(&id->lock, flags);
1099         for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1100                 if (id->views[i] == view)
1101                         break;
1102         }
1103         if (i == DEBUG_MAX_VIEWS) {
1104                 rc = -1;
1105         } else {
1106                 dentry = id->debugfs_entries[i];
1107                 id->views[i] = NULL;
1108                 id->debugfs_entries[i] = NULL;
1109         }
1110         spin_unlock_irqrestore(&id->lock, flags);
1111         debugfs_remove(dentry);
1112 out:
1113         return rc;
1114 }
1115 EXPORT_SYMBOL(debug_unregister_view);
1116
1117 static inline char *debug_get_user_string(const char __user *user_buf,
1118                                           size_t user_len)
1119 {
1120         char *buffer;
1121
1122         buffer = kmalloc(user_len + 1, GFP_KERNEL);
1123         if (!buffer)
1124                 return ERR_PTR(-ENOMEM);
1125         if (copy_from_user(buffer, user_buf, user_len) != 0) {
1126                 kfree(buffer);
1127                 return ERR_PTR(-EFAULT);
1128         }
1129         /* got the string, now strip linefeed. */
1130         if (buffer[user_len - 1] == '\n')
1131                 buffer[user_len - 1] = 0;
1132         else
1133                 buffer[user_len] = 0;
1134         return buffer;
1135 }
1136
1137 static inline int debug_get_uint(char *buf)
1138 {
1139         int rc;
1140
1141         buf = skip_spaces(buf);
1142         rc = simple_strtoul(buf, &buf, 10);
1143         if (*buf)
1144                 rc = -EINVAL;
1145         return rc;
1146 }
1147
1148 /*
1149  * functions for debug-views
1150  ***********************************
1151 */
1152
1153 /*
1154  * prints out actual debug level
1155  */
1156
1157 static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1158                                  char *out_buf)
1159 {
1160         return sprintf(out_buf, "%i\n", id->pages_per_area);
1161 }
1162
1163 /*
1164  * reads new size (number of pages per debug area)
1165  */
1166
1167 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
1168                                 struct file *file, const char __user *user_buf,
1169                                 size_t user_len, loff_t *offset)
1170 {
1171         int rc, new_pages;
1172         char *str;
1173
1174         if (user_len > 0x10000)
1175                 user_len = 0x10000;
1176         if (*offset != 0) {
1177                 rc = -EPIPE;
1178                 goto out;
1179         }
1180         str = debug_get_user_string(user_buf, user_len);
1181         if (IS_ERR(str)) {
1182                 rc = PTR_ERR(str);
1183                 goto out;
1184         }
1185         new_pages = debug_get_uint(str);
1186         if (new_pages < 0) {
1187                 rc = -EINVAL;
1188                 goto free_str;
1189         }
1190         rc = debug_set_size(id, id->nr_areas, new_pages);
1191         if (rc != 0) {
1192                 rc = -EINVAL;
1193                 goto free_str;
1194         }
1195         rc = user_len;
1196 free_str:
1197         kfree(str);
1198 out:
1199         *offset += user_len;
1200         return rc;              /* number of input characters */
1201 }
1202
1203 /*
1204  * prints out actual debug level
1205  */
1206 static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1207                                  char *out_buf)
1208 {
1209         int rc = 0;
1210
1211         if (id->level == DEBUG_OFF_LEVEL)
1212                 rc = sprintf(out_buf, "-\n");
1213         else
1214                 rc = sprintf(out_buf, "%i\n", id->level);
1215         return rc;
1216 }
1217
1218 /*
1219  * reads new debug level
1220  */
1221 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
1222                                 struct file *file, const char __user *user_buf,
1223                                 size_t user_len, loff_t *offset)
1224 {
1225         int rc, new_level;
1226         char *str;
1227
1228         if (user_len > 0x10000)
1229                 user_len = 0x10000;
1230         if (*offset != 0) {
1231                 rc = -EPIPE;
1232                 goto out;
1233         }
1234         str = debug_get_user_string(user_buf, user_len);
1235         if (IS_ERR(str)) {
1236                 rc = PTR_ERR(str);
1237                 goto out;
1238         }
1239         if (str[0] == '-') {
1240                 debug_set_level(id, DEBUG_OFF_LEVEL);
1241                 rc = user_len;
1242                 goto free_str;
1243         } else {
1244                 new_level = debug_get_uint(str);
1245         }
1246         if (new_level < 0) {
1247                 pr_warn("%s is not a valid level for a debug feature\n", str);
1248                 rc = -EINVAL;
1249         } else {
1250                 debug_set_level(id, new_level);
1251                 rc = user_len;
1252         }
1253 free_str:
1254         kfree(str);
1255 out:
1256         *offset += user_len;
1257         return rc;              /* number of input characters */
1258 }
1259
1260 /*
1261  * flushes debug areas
1262  */
1263 static void debug_flush(debug_info_t *id, int area)
1264 {
1265         unsigned long flags;
1266         int i, j;
1267
1268         if (!id || !id->areas)
1269                 return;
1270         spin_lock_irqsave(&id->lock, flags);
1271         if (area == DEBUG_FLUSH_ALL) {
1272                 id->active_area = 0;
1273                 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1274                 for (i = 0; i < id->nr_areas; i++) {
1275                         id->active_pages[i] = 0;
1276                         for (j = 0; j < id->pages_per_area; j++)
1277                                 memset(id->areas[i][j], 0, PAGE_SIZE);
1278                 }
1279         } else if (area >= 0 && area < id->nr_areas) {
1280                 id->active_entries[area] = 0;
1281                 id->active_pages[area] = 0;
1282                 for (i = 0; i < id->pages_per_area; i++)
1283                         memset(id->areas[area][i], 0, PAGE_SIZE);
1284         }
1285         spin_unlock_irqrestore(&id->lock, flags);
1286 }
1287
1288 /*
1289  * view function: flushes debug areas
1290  */
1291 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
1292                                 struct file *file, const char __user *user_buf,
1293                                 size_t user_len, loff_t *offset)
1294 {
1295         char input_buf[1];
1296         int rc = user_len;
1297
1298         if (user_len > 0x10000)
1299                 user_len = 0x10000;
1300         if (*offset != 0) {
1301                 rc = -EPIPE;
1302                 goto out;
1303         }
1304         if (copy_from_user(input_buf, user_buf, 1)) {
1305                 rc = -EFAULT;
1306                 goto out;
1307         }
1308         if (input_buf[0] == '-') {
1309                 debug_flush(id, DEBUG_FLUSH_ALL);
1310                 goto out;
1311         }
1312         if (isdigit(input_buf[0])) {
1313                 int area = ((int) input_buf[0] - (int) '0');
1314
1315                 debug_flush(id, area);
1316                 goto out;
1317         }
1318
1319         pr_info("Flushing debug data failed because %c is not a valid "
1320                  "area\n", input_buf[0]);
1321
1322 out:
1323         *offset += user_len;
1324         return rc;              /* number of input characters */
1325 }
1326
1327 /*
1328  * prints debug header in raw format
1329  */
1330 static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
1331                                int area, debug_entry_t *entry, char *out_buf)
1332 {
1333         int rc;
1334
1335         rc = sizeof(debug_entry_t);
1336         memcpy(out_buf, entry, sizeof(debug_entry_t));
1337         return rc;
1338 }
1339
1340 /*
1341  * prints debug data in raw format
1342  */
1343 static int debug_raw_format_fn(debug_info_t *id, struct debug_view *view,
1344                                char *out_buf, const char *in_buf)
1345 {
1346         int rc;
1347
1348         rc = id->buf_size;
1349         memcpy(out_buf, in_buf, id->buf_size);
1350         return rc;
1351 }
1352
1353 /*
1354  * prints debug data in hex/ascii format
1355  */
1356 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1357                                      char *out_buf, const char *in_buf)
1358 {
1359         int i, rc = 0;
1360
1361         for (i = 0; i < id->buf_size; i++)
1362                 rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
1363         rc += sprintf(out_buf + rc, "| ");
1364         for (i = 0; i < id->buf_size; i++) {
1365                 unsigned char c = in_buf[i];
1366
1367                 if (isascii(c) && isprint(c))
1368                         rc += sprintf(out_buf + rc, "%c", c);
1369                 else
1370                         rc += sprintf(out_buf + rc, ".");
1371         }
1372         rc += sprintf(out_buf + rc, "\n");
1373         return rc;
1374 }
1375
1376 /*
1377  * prints header for debug entry
1378  */
1379 int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1380                          int area, debug_entry_t *entry, char *out_buf)
1381 {
1382         unsigned long base, sec, usec;
1383         unsigned long caller;
1384         unsigned int level;
1385         char *except_str;
1386         int rc = 0;
1387
1388         level = entry->id.fields.level;
1389         base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
1390         sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
1391         usec = do_div(sec, USEC_PER_SEC);
1392
1393         if (entry->id.fields.exception)
1394                 except_str = "*";
1395         else
1396                 except_str = "-";
1397         caller = (unsigned long) entry->caller;
1398         rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %02i %pK  ",
1399                       area, sec, usec, level, except_str,
1400                       entry->id.fields.cpuid, (void *)caller);
1401         return rc;
1402 }
1403 EXPORT_SYMBOL(debug_dflt_header_fn);
1404
1405 /*
1406  * prints debug data sprintf-formated:
1407  * debug_sprinf_event/exception calls must be used together with this view
1408  */
1409
1410 #define DEBUG_SPRINTF_MAX_ARGS 10
1411
1412 static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1413                                    char *out_buf, debug_sprintf_entry_t *curr_event)
1414 {
1415         int num_longs, num_used_args = 0, i, rc = 0;
1416         int index[DEBUG_SPRINTF_MAX_ARGS];
1417
1418         /* count of longs fit into one entry */
1419         num_longs = id->buf_size / sizeof(long);
1420
1421         if (num_longs < 1)
1422                 goto out; /* bufsize of entry too small */
1423         if (num_longs == 1) {
1424                 /* no args, we use only the string */
1425                 strcpy(out_buf, curr_event->string);
1426                 rc = strlen(curr_event->string);
1427                 goto out;
1428         }
1429
1430         /* number of arguments used for sprintf (without the format string) */
1431         num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1432
1433         memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1434
1435         for (i = 0; i < num_used_args; i++)
1436                 index[i] = i;
1437
1438         rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1439                      curr_event->args[index[1]], curr_event->args[index[2]],
1440                      curr_event->args[index[3]], curr_event->args[index[4]],
1441                      curr_event->args[index[5]], curr_event->args[index[6]],
1442                      curr_event->args[index[7]], curr_event->args[index[8]],
1443                      curr_event->args[index[9]]);
1444 out:
1445         return rc;
1446 }
1447
1448 /*
1449  * debug_init:
1450  * - is called exactly once to initialize the debug feature
1451  */
1452 static int __init debug_init(void)
1453 {
1454         s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1455         mutex_lock(&debug_mutex);
1456         debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
1457         initialized = 1;
1458         mutex_unlock(&debug_mutex);
1459         return 0;
1460 }
1461 postcore_initcall(debug_init);