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