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