Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / 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/config.h>
14 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/ctype.h>
19 #include <linux/sysctl.h>
20 #include <asm/uaccess.h>
21 #include <asm/semaphore.h>
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25
26 #include <asm/debug.h>
27
28 #define DEBUG_PROLOG_ENTRY -1
29
30 /* typedefs */
31
32 typedef struct file_private_info {
33         loff_t offset;                  /* offset of last read in file */
34         int    act_area;                /* number of last formated area */
35         int    act_entry;               /* last formated entry (offset */
36                                         /* relative to beginning of last */
37                                         /* formated area) */ 
38         size_t act_entry_offset;        /* up to this offset we copied */
39                                         /* in last read the last formated */
40                                         /* entry to userland */
41         char   temp_buf[2048];          /* buffer for output */
42         debug_info_t *debug_info_org;   /* original debug information */
43         debug_info_t *debug_info_snap;  /* snapshot of debug information */
44         struct debug_view *view;        /* used view of debug info */
45 } file_private_info_t;
46
47 typedef struct
48 {
49         char *string;
50         /* 
51          * This assumes that all args are converted into longs 
52          * on L/390 this is the case for all types of parameter 
53          * except of floats, and long long (32 bit) 
54          *
55          */
56         long args[0];
57 } debug_sprintf_entry_t;
58
59
60 extern void tod_to_timeval(uint64_t todval, struct timeval *xtime);
61
62 /* internal function prototyes */
63
64 static int debug_init(void);
65 static ssize_t debug_output(struct file *file, char __user *user_buf,
66                             size_t user_len, loff_t * offset);
67 static ssize_t debug_input(struct file *file, const char __user *user_buf,
68                            size_t user_len, loff_t * offset);
69 static int debug_open(struct inode *inode, struct file *file);
70 static int debug_close(struct inode *inode, struct file *file);
71 static debug_info_t*  debug_info_create(char *name, int page_order, int nr_areas, int buf_size);
72 static void debug_info_get(debug_info_t *);
73 static void debug_info_put(debug_info_t *);
74 static int debug_prolog_level_fn(debug_info_t * id,
75                                  struct debug_view *view, char *out_buf);
76 static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
77                                 struct file *file, const char __user *user_buf,
78                                 size_t user_buf_size, loff_t * offset);
79 static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
80                                 struct file *file, const char __user *user_buf,
81                                 size_t user_buf_size, loff_t * offset);
82 static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
83                                 char *out_buf, const char *in_buf);
84 static int debug_raw_format_fn(debug_info_t * id,
85                                  struct debug_view *view, char *out_buf,
86                                  const char *in_buf);
87 static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
88                          int area, debug_entry_t * entry, char *out_buf);
89
90 static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
91                                    char *out_buf, debug_sprintf_entry_t *curr_event);
92
93 /* globals */
94
95 struct debug_view debug_raw_view = {
96         "raw",
97         NULL,
98         &debug_raw_header_fn,
99         &debug_raw_format_fn,
100         NULL,
101         NULL
102 };
103
104 struct debug_view debug_hex_ascii_view = {
105         "hex_ascii",
106         NULL,
107         &debug_dflt_header_fn,
108         &debug_hex_ascii_format_fn,
109         NULL,
110         NULL
111 };
112
113 struct debug_view debug_level_view = {
114         "level",
115         &debug_prolog_level_fn,
116         NULL,
117         NULL,
118         &debug_input_level_fn,
119         NULL
120 };
121
122 struct debug_view debug_flush_view = {
123         "flush",
124         NULL,
125         NULL,
126         NULL,
127         &debug_input_flush_fn,
128         NULL
129 };
130
131 struct debug_view debug_sprintf_view = {
132         "sprintf",
133         NULL,
134         &debug_dflt_header_fn,
135         (debug_format_proc_t*)&debug_sprintf_format_fn,
136         NULL,
137         NULL
138 };
139
140
141 unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;
142
143 /* static globals */
144
145 static debug_info_t *debug_area_first = NULL;
146 static debug_info_t *debug_area_last = NULL;
147 DECLARE_MUTEX(debug_lock);
148
149 static int initialized;
150
151 static struct file_operations debug_file_ops = {
152         .owner   = THIS_MODULE,
153         .read    = debug_output,
154         .write   = debug_input, 
155         .open    = debug_open,
156         .release = debug_close,
157 };
158
159 static struct proc_dir_entry *debug_proc_root_entry;
160
161 /* functions */
162
163 /*
164  * debug_info_alloc
165  * - alloc new debug-info
166  */
167
168 static debug_info_t*  debug_info_alloc(char *name, int page_order,
169                                         int nr_areas, int buf_size)
170 {
171         debug_info_t* rc;
172         int i;
173
174         /* alloc everything */
175
176         rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_ATOMIC);
177         if(!rc)
178                 goto fail_malloc_rc;
179         rc->active_entry = (int*)kmalloc(nr_areas * sizeof(int), GFP_ATOMIC);
180         if(!rc->active_entry)
181                 goto fail_malloc_active_entry;
182         memset(rc->active_entry, 0, nr_areas * sizeof(int));
183         rc->areas = (debug_entry_t **) kmalloc(nr_areas *
184                                                 sizeof(debug_entry_t *),
185                                                 GFP_ATOMIC);
186         if (!rc->areas)
187                 goto fail_malloc_areas;
188         for (i = 0; i < nr_areas; i++) {
189                 rc->areas[i] = (debug_entry_t *) __get_free_pages(GFP_ATOMIC,
190                                                                 page_order);
191                 if (!rc->areas[i]) {
192                         for (i--; i >= 0; i--) {
193                                 free_pages((unsigned long) rc->areas[i],
194                                                 page_order);
195                         }
196                         goto fail_malloc_areas2;
197                 } else {
198                         memset(rc->areas[i], 0, PAGE_SIZE << page_order);
199                 }
200         }
201
202         /* initialize members */
203
204         spin_lock_init(&rc->lock);
205         rc->page_order  = page_order;
206         rc->nr_areas    = nr_areas;
207         rc->active_area = 0;
208         rc->level       = DEBUG_DEFAULT_LEVEL;
209         rc->buf_size    = buf_size;
210         rc->entry_size  = sizeof(debug_entry_t) + buf_size;
211         strlcpy(rc->name, name, sizeof(rc->name));
212         memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
213 #ifdef CONFIG_PROC_FS
214         memset(rc->proc_entries, 0 ,DEBUG_MAX_VIEWS *
215                 sizeof(struct proc_dir_entry*));
216 #endif /* CONFIG_PROC_FS */
217         atomic_set(&(rc->ref_count), 0);
218
219         return rc;
220
221 fail_malloc_areas2:
222         kfree(rc->areas);
223 fail_malloc_areas:
224         kfree(rc->active_entry);
225 fail_malloc_active_entry:
226         kfree(rc);
227 fail_malloc_rc:
228         return NULL;
229 }
230
231 /*
232  * debug_info_free
233  * - free memory debug-info
234  */
235
236 static void debug_info_free(debug_info_t* db_info){
237         int i;
238         for (i = 0; i < db_info->nr_areas; i++) {
239                 free_pages((unsigned long) db_info->areas[i],
240                 db_info->page_order);
241         }
242         kfree(db_info->areas);
243         kfree(db_info->active_entry);
244         kfree(db_info);
245 }
246
247 /*
248  * debug_info_create
249  * - create new debug-info
250  */
251
252 static debug_info_t*  debug_info_create(char *name, int page_order, 
253                                         int nr_areas, int buf_size)
254 {
255         debug_info_t* rc;
256
257         rc = debug_info_alloc(name, page_order, nr_areas, buf_size);
258         if(!rc) 
259                 goto out;
260
261
262         /* create proc rood directory */
263         rc->proc_root_entry = proc_mkdir(rc->name, debug_proc_root_entry);
264
265         /* append new element to linked list */
266         if (debug_area_first == NULL) {
267                 /* first element in list */
268                 debug_area_first = rc;
269                 rc->prev = NULL;
270         } else {
271                 /* append element to end of list */
272                 debug_area_last->next = rc;
273                 rc->prev = debug_area_last;
274         }
275         debug_area_last = rc;
276         rc->next = NULL;
277
278         debug_info_get(rc);
279 out:
280         return rc;
281 }
282
283 /*
284  * debug_info_copy
285  * - copy debug-info
286  */
287
288 static debug_info_t* debug_info_copy(debug_info_t* in)
289 {
290         int i;
291         debug_info_t* rc;
292         rc = debug_info_alloc(in->name, in->page_order, 
293                                 in->nr_areas, in->buf_size);
294         if(!rc)
295                 goto out;
296
297         for(i = 0; i < in->nr_areas; i++){
298                 memcpy(rc->areas[i],in->areas[i], PAGE_SIZE << in->page_order);
299         }
300 out:
301         return rc;
302 }
303
304 /*
305  * debug_info_get
306  * - increments reference count for debug-info
307  */
308
309 static void debug_info_get(debug_info_t * db_info)
310 {
311         if (db_info)
312                 atomic_inc(&db_info->ref_count);
313 }
314
315 /*
316  * debug_info_put:
317  * - decreases reference count for debug-info and frees it if necessary
318  */
319
320 static void debug_info_put(debug_info_t *db_info)
321 {
322         int i;
323
324         if (!db_info)
325                 return;
326         if (atomic_dec_and_test(&db_info->ref_count)) {
327 #ifdef DEBUG
328                 printk(KERN_INFO "debug: freeing debug area %p (%s)\n",
329                        db_info, db_info->name);
330 #endif
331                 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
332                         if (db_info->views[i] == NULL)
333                                 continue;
334 #ifdef CONFIG_PROC_FS
335                         remove_proc_entry(db_info->proc_entries[i]->name,
336                                           db_info->proc_root_entry);
337 #endif
338                 }
339 #ifdef CONFIG_PROC_FS
340                 remove_proc_entry(db_info->proc_root_entry->name,
341                                   debug_proc_root_entry);
342 #endif
343                 if(db_info == debug_area_first)
344                         debug_area_first = db_info->next;
345                 if(db_info == debug_area_last)
346                         debug_area_last = db_info->prev;
347                 if(db_info->prev) db_info->prev->next = db_info->next;
348                 if(db_info->next) db_info->next->prev = db_info->prev;
349                 debug_info_free(db_info);
350         }
351 }
352
353 /*
354  * debug_format_entry:
355  * - format one debug entry and return size of formated data
356  */
357
358 static int debug_format_entry(file_private_info_t *p_info)
359 {
360         debug_info_t *id_org    = p_info->debug_info_org;
361         debug_info_t *id_snap   = p_info->debug_info_snap;
362         struct debug_view *view = p_info->view;
363         debug_entry_t *act_entry;
364         size_t len = 0;
365         if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
366                 /* print prolog */
367                 if (view->prolog_proc)
368                         len += view->prolog_proc(id_org, view,p_info->temp_buf);
369                 goto out;
370         }
371
372         act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area] +
373                                         p_info->act_entry);
374                         
375         if (act_entry->id.stck == 0LL)
376                         goto out;  /* empty entry */
377         if (view->header_proc)
378                 len += view->header_proc(id_org, view, p_info->act_area, 
379                                         act_entry, p_info->temp_buf + len);
380         if (view->format_proc)
381                 len += view->format_proc(id_org, view, p_info->temp_buf + len,
382                                                 DEBUG_DATA(act_entry));
383       out:
384         return len;
385 }
386
387 /*
388  * debug_next_entry:
389  * - goto next entry in p_info
390  */
391
392 extern inline int debug_next_entry(file_private_info_t *p_info)
393 {
394         debug_info_t *id = p_info->debug_info_snap;
395         if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
396                 p_info->act_entry = 0;
397                 goto out;
398         }
399         if ((p_info->act_entry += id->entry_size)
400                 > ((PAGE_SIZE << (id->page_order)) 
401                 - id->entry_size)){
402
403                 /* next area */
404                 p_info->act_entry = 0;
405                 p_info->act_area++;
406                 if(p_info->act_area >= id->nr_areas)
407                         return 1;
408         }
409 out:
410         return 0;       
411 }
412
413 /*
414  * debug_output:
415  * - called for user read()
416  * - copies formated debug entries to the user buffer
417  */
418
419 static ssize_t debug_output(struct file *file,          /* file descriptor */
420                             char __user *user_buf,      /* user buffer */
421                             size_t  len,                /* length of buffer */
422                             loff_t *offset)           /* offset in the file */
423 {
424         size_t count = 0;
425         size_t entry_offset, size = 0;
426         file_private_info_t *p_info;
427
428         p_info = ((file_private_info_t *) file->private_data);
429         if (*offset != p_info->offset) 
430                 return -EPIPE;
431         if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
432                 return 0;
433
434         entry_offset = p_info->act_entry_offset;
435
436         while(count < len){
437                 size = debug_format_entry(p_info);
438                 size = min((len - count), (size - entry_offset));
439
440                 if(size){
441                         if (copy_to_user(user_buf + count, 
442                                         p_info->temp_buf + entry_offset, size))
443                         return -EFAULT;
444                 }
445                 count += size;
446                 entry_offset = 0;
447                 if(count != len)
448                         if(debug_next_entry(p_info)) 
449                                 goto out;
450         }
451 out:
452         p_info->offset           = *offset + count;
453         p_info->act_entry_offset = size;        
454         *offset = p_info->offset;
455         return count;
456 }
457
458 /*
459  * debug_input:
460  * - called for user write()
461  * - calls input function of view
462  */
463
464 static ssize_t debug_input(struct file *file,
465                            const char __user *user_buf, size_t length,
466                            loff_t *offset)
467 {
468         int rc = 0;
469         file_private_info_t *p_info;
470
471         down(&debug_lock);
472         p_info = ((file_private_info_t *) file->private_data);
473         if (p_info->view->input_proc)
474                 rc = p_info->view->input_proc(p_info->debug_info_org,
475                                               p_info->view, file, user_buf,
476                                               length, offset);
477         else
478                 rc = -EPERM;
479         up(&debug_lock);
480         return rc;              /* number of input characters */
481 }
482
483 /*
484  * debug_open:
485  * - called for user open()
486  * - copies formated output to private_data area of the file
487  *   handle
488  */
489
490 static int debug_open(struct inode *inode, struct file *file)
491 {
492         int i = 0, rc = 0;
493         file_private_info_t *p_info;
494         debug_info_t *debug_info, *debug_info_snapshot;
495
496 #ifdef DEBUG
497         printk("debug_open\n");
498 #endif
499         down(&debug_lock);
500
501         /* find debug log and view */
502
503         debug_info = debug_area_first;
504         while(debug_info != NULL){
505                 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
506                         if (debug_info->views[i] == NULL)
507                                 continue;
508                         else if (debug_info->proc_entries[i] ==
509                                  PDE(file->f_dentry->d_inode)) {
510                                 goto found;     /* found view ! */
511                         }
512                 }
513                 debug_info = debug_info->next;
514         }
515         /* no entry found */
516         rc = -EINVAL;
517         goto out;
518
519       found:
520
521         /* make snapshot of current debug areas to get it consistent */
522
523         debug_info_snapshot = debug_info_copy(debug_info);
524
525         if(!debug_info_snapshot){
526 #ifdef DEBUG
527                 printk(KERN_ERR "debug_open: debug_info_copy failed (out of mem)\n");
528 #endif
529                 rc = -ENOMEM;
530                 goto out;
531         }
532
533         if ((file->private_data =
534              kmalloc(sizeof(file_private_info_t), GFP_ATOMIC)) == 0) {
535 #ifdef DEBUG
536                 printk(KERN_ERR "debug_open: kmalloc failed\n");
537 #endif
538                 debug_info_free(debug_info_snapshot);   
539                 rc = -ENOMEM;
540                 goto out;
541         }
542         p_info = (file_private_info_t *) file->private_data;
543         p_info->offset = 0;
544         p_info->debug_info_snap = debug_info_snapshot;
545         p_info->debug_info_org  = debug_info;
546         p_info->view = debug_info->views[i];
547         p_info->act_area = 0;
548         p_info->act_entry = DEBUG_PROLOG_ENTRY;
549         p_info->act_entry_offset = 0;
550
551         debug_info_get(debug_info);
552
553       out:
554         up(&debug_lock);
555         return rc;
556 }
557
558 /*
559  * debug_close:
560  * - called for user close()
561  * - deletes  private_data area of the file handle
562  */
563
564 static int debug_close(struct inode *inode, struct file *file)
565 {
566         file_private_info_t *p_info;
567 #ifdef DEBUG
568         printk("debug_close\n");
569 #endif
570         p_info = (file_private_info_t *) file->private_data;
571         debug_info_free(p_info->debug_info_snap);
572         debug_info_put(p_info->debug_info_org);
573         kfree(file->private_data);
574         return 0;               /* success */
575 }
576
577 /*
578  * debug_register:
579  * - creates and initializes debug area for the caller
580  * - returns handle for debug area
581  */
582
583 debug_info_t *debug_register
584     (char *name, int page_order, int nr_areas, int buf_size) 
585 {
586         debug_info_t *rc = NULL;
587
588         if (!initialized)
589                 BUG();
590         down(&debug_lock);
591
592         /* create new debug_info */
593
594         rc = debug_info_create(name, page_order, nr_areas, buf_size);
595         if(!rc) 
596                 goto out;
597         debug_register_view(rc, &debug_level_view);
598         debug_register_view(rc, &debug_flush_view);
599 #ifdef DEBUG
600         printk(KERN_INFO
601                "debug: reserved %d areas of %d pages for debugging %s\n",
602                nr_areas, 1 << page_order, rc->name);
603 #endif
604       out:
605         if (rc == NULL){
606                 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
607         }
608         up(&debug_lock);
609         return rc;
610 }
611
612 /*
613  * debug_unregister:
614  * - give back debug area
615  */
616
617 void debug_unregister(debug_info_t * id)
618 {
619         if (!id)
620                 goto out;
621         down(&debug_lock);
622 #ifdef DEBUG
623         printk(KERN_INFO "debug: unregistering %s\n", id->name);
624 #endif
625         debug_info_put(id);
626         up(&debug_lock);
627
628       out:
629         return;
630 }
631
632 /*
633  * debug_set_level:
634  * - set actual debug level
635  */
636
637 void debug_set_level(debug_info_t* id, int new_level)
638 {
639         unsigned long flags;
640         if(!id)
641                 return; 
642         spin_lock_irqsave(&id->lock,flags);
643         if(new_level == DEBUG_OFF_LEVEL){
644                 id->level = DEBUG_OFF_LEVEL;
645                 printk(KERN_INFO "debug: %s: switched off\n",id->name);
646         } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
647                 printk(KERN_INFO
648                         "debug: %s: level %i is out of range (%i - %i)\n",
649                         id->name, new_level, 0, DEBUG_MAX_LEVEL);
650         } else {
651                 id->level = new_level;
652 #ifdef DEBUG
653                 printk(KERN_INFO 
654                         "debug: %s: new level %i\n",id->name,id->level);
655 #endif
656         }
657         spin_unlock_irqrestore(&id->lock,flags);
658 }
659
660
661 /*
662  * proceed_active_entry:
663  * - set active entry to next in the ring buffer
664  */
665
666 extern inline void proceed_active_entry(debug_info_t * id)
667 {
668         if ((id->active_entry[id->active_area] += id->entry_size)
669             > ((PAGE_SIZE << (id->page_order)) - id->entry_size))
670                 id->active_entry[id->active_area] = 0;
671 }
672
673 /*
674  * proceed_active_area:
675  * - set active area to next in the ring buffer
676  */
677
678 extern inline void proceed_active_area(debug_info_t * id)
679 {
680         id->active_area++;
681         id->active_area = id->active_area % id->nr_areas;
682 }
683
684 /*
685  * get_active_entry:
686  */
687
688 extern inline debug_entry_t *get_active_entry(debug_info_t * id)
689 {
690         return (debug_entry_t *) ((char *) id->areas[id->active_area] +
691                                   id->active_entry[id->active_area]);
692 }
693
694 /*
695  * debug_finish_entry:
696  * - set timestamp, caller address, cpu number etc.
697  */
698
699 extern inline void debug_finish_entry(debug_info_t * id, debug_entry_t* active,
700                 int level, int exception)
701 {
702         STCK(active->id.stck);
703         active->id.fields.cpuid = smp_processor_id();
704         active->caller = __builtin_return_address(0);
705         active->id.fields.exception = exception;
706         active->id.fields.level     = level;
707         proceed_active_entry(id);
708         if(exception)
709                 proceed_active_area(id);
710 }
711
712 static int debug_stoppable=1;
713 static int debug_active=1;
714
715 #define CTL_S390DBF 5677
716 #define CTL_S390DBF_STOPPABLE 5678
717 #define CTL_S390DBF_ACTIVE 5679
718
719 /*
720  * proc handler for the running debug_active sysctl
721  * always allow read, allow write only if debug_stoppable is set or
722  * if debug_active is already off
723  */
724 static int s390dbf_procactive(ctl_table *table, int write, struct file *filp,
725                      void __user *buffer, size_t *lenp, loff_t *ppos)
726 {
727         if (!write || debug_stoppable || !debug_active)
728                 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
729         else
730                 return 0;
731 }
732
733
734 static struct ctl_table s390dbf_table[] = {
735         {
736                 .ctl_name       = CTL_S390DBF_STOPPABLE,
737                 .procname       = "debug_stoppable",
738                 .data           = &debug_stoppable,
739                 .maxlen         = sizeof(int),
740                 .mode           = S_IRUGO | S_IWUSR,
741                 .proc_handler   = &proc_dointvec,
742                 .strategy       = &sysctl_intvec,
743         },
744          {
745                 .ctl_name       = CTL_S390DBF_ACTIVE,
746                 .procname       = "debug_active",
747                 .data           = &debug_active,
748                 .maxlen         = sizeof(int),
749                 .mode           = S_IRUGO | S_IWUSR,
750                 .proc_handler   = &s390dbf_procactive,
751                 .strategy       = &sysctl_intvec,
752         },
753         { .ctl_name = 0 }
754 };
755
756 static struct ctl_table s390dbf_dir_table[] = {
757         {
758                 .ctl_name       = CTL_S390DBF,
759                 .procname       = "s390dbf",
760                 .maxlen         = 0,
761                 .mode           = S_IRUGO | S_IXUGO,
762                 .child          = s390dbf_table,
763         },
764         { .ctl_name = 0 }
765 };
766
767 struct ctl_table_header *s390dbf_sysctl_header;
768
769 void debug_stop_all(void)
770 {
771         if (debug_stoppable)
772                 debug_active = 0;
773 }
774
775
776 /*
777  * debug_event_common:
778  * - write debug entry with given size
779  */
780
781 debug_entry_t *debug_event_common(debug_info_t * id, int level, const void *buf,
782                                   int len)
783 {
784         unsigned long flags;
785         debug_entry_t *active;
786
787         if (!debug_active)
788                 return NULL;
789         spin_lock_irqsave(&id->lock, flags);
790         active = get_active_entry(id);
791         memset(DEBUG_DATA(active), 0, id->buf_size);
792         memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
793         debug_finish_entry(id, active, level, 0);
794         spin_unlock_irqrestore(&id->lock, flags);
795
796         return active;
797 }
798
799 /*
800  * debug_exception_common:
801  * - write debug entry with given size and switch to next debug area
802  */
803
804 debug_entry_t *debug_exception_common(debug_info_t * id, int level, 
805                                       const void *buf, int len)
806 {
807         unsigned long flags;
808         debug_entry_t *active;
809
810         if (!debug_active)
811                 return NULL;
812         spin_lock_irqsave(&id->lock, flags);
813         active = get_active_entry(id);
814         memset(DEBUG_DATA(active), 0, id->buf_size);
815         memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
816         debug_finish_entry(id, active, level, 1);
817         spin_unlock_irqrestore(&id->lock, flags);
818
819         return active;
820 }
821
822 /*
823  * counts arguments in format string for sprintf view
824  */
825
826 extern inline int debug_count_numargs(char *string)
827 {
828         int numargs=0;
829
830         while(*string) {
831                 if(*string++=='%')
832                         numargs++;
833         }
834         return(numargs);
835 }
836
837 /*
838  * debug_sprintf_event:
839  */
840
841 debug_entry_t *debug_sprintf_event(debug_info_t* id,
842                                    int level,char *string,...)
843 {
844         va_list   ap;
845         int numargs,idx;
846         unsigned long flags;
847         debug_sprintf_entry_t *curr_event;
848         debug_entry_t *active;
849
850         if((!id) || (level > id->level))
851                 return NULL;
852         if (!debug_active)
853                 return NULL;
854         numargs=debug_count_numargs(string);
855
856         spin_lock_irqsave(&id->lock, flags);
857         active = get_active_entry(id);
858         curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
859         va_start(ap,string);
860         curr_event->string=string;
861         for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
862                 curr_event->args[idx]=va_arg(ap,long);
863         va_end(ap);
864         debug_finish_entry(id, active, level, 0);
865         spin_unlock_irqrestore(&id->lock, flags);
866
867         return active;
868 }
869
870 /*
871  * debug_sprintf_exception:
872  */
873
874 debug_entry_t *debug_sprintf_exception(debug_info_t* id,
875                                        int level,char *string,...)
876 {
877         va_list   ap;
878         int numargs,idx;
879         unsigned long flags;
880         debug_sprintf_entry_t *curr_event;
881         debug_entry_t *active;
882
883         if((!id) || (level > id->level))
884                 return NULL;
885         if (!debug_active)
886                 return NULL;
887
888         numargs=debug_count_numargs(string);
889
890         spin_lock_irqsave(&id->lock, flags);
891         active = get_active_entry(id);
892         curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
893         va_start(ap,string);
894         curr_event->string=string;
895         for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
896                 curr_event->args[idx]=va_arg(ap,long);
897         va_end(ap);
898         debug_finish_entry(id, active, level, 1);
899         spin_unlock_irqrestore(&id->lock, flags);
900
901         return active;
902 }
903
904 /*
905  * debug_init:
906  * - is called exactly once to initialize the debug feature
907  */
908
909 static int __init debug_init(void)
910 {
911         int rc = 0;
912
913         s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
914         down(&debug_lock);
915 #ifdef CONFIG_PROC_FS
916         debug_proc_root_entry = proc_mkdir(DEBUG_DIR_ROOT, NULL);
917 #endif /* CONFIG_PROC_FS */
918         printk(KERN_INFO "debug: Initialization complete\n");
919         initialized = 1;
920         up(&debug_lock);
921
922         return rc;
923 }
924
925 /*
926  * debug_register_view:
927  */
928
929 int debug_register_view(debug_info_t * id, struct debug_view *view)
930 {
931         int rc = 0;
932         int i;
933         unsigned long flags;
934         mode_t mode = S_IFREG;
935         struct proc_dir_entry *pde;
936
937         if (!id)
938                 goto out;
939         if (view->prolog_proc || view->format_proc || view->header_proc)
940                 mode |= S_IRUSR;
941         if (view->input_proc)
942                 mode |= S_IWUSR;
943         pde = create_proc_entry(view->name, mode, id->proc_root_entry);
944         if (!pde){
945                 printk(KERN_WARNING "debug: create_proc_entry() failed! Cannot register view %s/%s\n", id->name,view->name);
946                 rc = -1;
947                 goto out;
948         }
949
950         spin_lock_irqsave(&id->lock, flags);
951         for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
952                 if (id->views[i] == NULL)
953                         break;
954         }
955         if (i == DEBUG_MAX_VIEWS) {
956                 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
957                         id->name,view->name);
958                 printk(KERN_WARNING 
959                         "debug: maximum number of views reached (%i)!\n", i);
960                 remove_proc_entry(pde->name, id->proc_root_entry);
961                 rc = -1;
962         }
963         else {
964                 id->views[i] = view;
965                 pde->proc_fops = &debug_file_ops;
966                 id->proc_entries[i] = pde;
967         }
968         spin_unlock_irqrestore(&id->lock, flags);
969       out:
970         return rc;
971 }
972
973 /*
974  * debug_unregister_view:
975  */
976
977 int debug_unregister_view(debug_info_t * id, struct debug_view *view)
978 {
979         int rc = 0;
980         int i;
981         unsigned long flags;
982
983         if (!id)
984                 goto out;
985         spin_lock_irqsave(&id->lock, flags);
986         for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
987                 if (id->views[i] == view)
988                         break;
989         }
990         if (i == DEBUG_MAX_VIEWS)
991                 rc = -1;
992         else {
993 #ifdef CONFIG_PROC_FS
994                 remove_proc_entry(id->proc_entries[i]->name,
995                                   id->proc_root_entry);
996 #endif
997                 id->views[i] = NULL;
998                 rc = 0;
999         }
1000         spin_unlock_irqrestore(&id->lock, flags);
1001       out:
1002         return rc;
1003 }
1004
1005 /*
1006  * functions for debug-views
1007  ***********************************
1008 */
1009
1010 /*
1011  * prints out actual debug level
1012  */
1013
1014 static int debug_prolog_level_fn(debug_info_t * id,
1015                                  struct debug_view *view, char *out_buf)
1016 {
1017         int rc = 0;
1018
1019         if(id->level == -1) rc = sprintf(out_buf,"-\n");
1020         else rc = sprintf(out_buf, "%i\n", id->level);
1021         return rc;
1022 }
1023
1024 /*
1025  * reads new debug level
1026  */
1027
1028 static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1029                                 struct file *file, const char __user *user_buf,
1030                                 size_t in_buf_size, loff_t * offset)
1031 {
1032         char input_buf[1];
1033         int rc = in_buf_size;
1034
1035         if (*offset != 0)
1036                 goto out;
1037         if (copy_from_user(input_buf, user_buf, 1)){
1038                 rc = -EFAULT;
1039                 goto out;
1040         }
1041         if (isdigit(input_buf[0])) {
1042                 int new_level = ((int) input_buf[0] - (int) '0');
1043                 debug_set_level(id, new_level);
1044         } else if(input_buf[0] == '-') {
1045                 debug_set_level(id, DEBUG_OFF_LEVEL);
1046         } else {
1047                 printk(KERN_INFO "debug: level `%c` is not valid\n",
1048                        input_buf[0]);
1049         }
1050       out:
1051         *offset += in_buf_size;
1052         return rc;              /* number of input characters */
1053 }
1054
1055
1056 /*
1057  * flushes debug areas
1058  */
1059  
1060 void debug_flush(debug_info_t* id, int area)
1061 {
1062         unsigned long flags;
1063         int i;
1064
1065         if(!id)
1066                 return;
1067         spin_lock_irqsave(&id->lock,flags);
1068         if(area == DEBUG_FLUSH_ALL){
1069                 id->active_area = 0;
1070                 memset(id->active_entry, 0, id->nr_areas * sizeof(int));
1071                 for (i = 0; i < id->nr_areas; i++) 
1072                         memset(id->areas[i], 0, PAGE_SIZE << id->page_order);
1073                 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1074         } else if(area >= 0 && area < id->nr_areas) {
1075                 id->active_entry[area] = 0;
1076                 memset(id->areas[area], 0, PAGE_SIZE << id->page_order);
1077                 printk(KERN_INFO
1078                         "debug: %s: area %i has been flushed\n",
1079                         id->name, area);
1080         } else {
1081                 printk(KERN_INFO
1082                         "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
1083                         id->name, area, 0, id->nr_areas-1);
1084         }
1085         spin_unlock_irqrestore(&id->lock,flags);
1086 }
1087
1088 /*
1089  * view function: flushes debug areas 
1090  */
1091
1092 static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1093                                 struct file *file, const char __user *user_buf,
1094                                 size_t in_buf_size, loff_t * offset)
1095 {
1096         char input_buf[1];
1097         int rc = in_buf_size;
1098  
1099         if (*offset != 0)
1100                 goto out;
1101         if (copy_from_user(input_buf, user_buf, 1)){
1102                 rc = -EFAULT;
1103                 goto out;
1104         }
1105         if(input_buf[0] == '-') { 
1106                 debug_flush(id, DEBUG_FLUSH_ALL);
1107                 goto out;
1108         }
1109         if (isdigit(input_buf[0])) {
1110                 int area = ((int) input_buf[0] - (int) '0');
1111                 debug_flush(id, area);
1112                 goto out;
1113         }
1114
1115         printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1116
1117       out:
1118         *offset += in_buf_size;
1119         return rc;              /* number of input characters */
1120 }
1121
1122 /*
1123  * prints debug header in raw format
1124  */
1125
1126 int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1127                          int area, debug_entry_t * entry, char *out_buf)
1128 {
1129         int rc;
1130
1131         rc = sizeof(debug_entry_t);
1132         memcpy(out_buf,entry,sizeof(debug_entry_t));
1133         return rc;
1134 }
1135
1136 /*
1137  * prints debug data in raw format
1138  */
1139
1140 static int debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1141                                char *out_buf, const char *in_buf)
1142 {
1143         int rc;
1144
1145         rc = id->buf_size;
1146         memcpy(out_buf, in_buf, id->buf_size);
1147         return rc;
1148 }
1149
1150 /*
1151  * prints debug data in hex/ascii format
1152  */
1153
1154 static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1155                                   char *out_buf, const char *in_buf)
1156 {
1157         int i, rc = 0;
1158
1159         for (i = 0; i < id->buf_size; i++) {
1160                 rc += sprintf(out_buf + rc, "%02x ",
1161                               ((unsigned char *) in_buf)[i]);
1162         }
1163         rc += sprintf(out_buf + rc, "| ");
1164         for (i = 0; i < id->buf_size; i++) {
1165                 unsigned char c = in_buf[i];
1166                 if (!isprint(c))
1167                         rc += sprintf(out_buf + rc, ".");
1168                 else
1169                         rc += sprintf(out_buf + rc, "%c", c);
1170         }
1171         rc += sprintf(out_buf + rc, "\n");
1172         return rc;
1173 }
1174
1175 /*
1176  * prints header for debug entry
1177  */
1178
1179 int debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1180                          int area, debug_entry_t * entry, char *out_buf)
1181 {
1182         struct timeval time_val;
1183         unsigned long long time;
1184         char *except_str;
1185         unsigned long caller;
1186         int rc = 0;
1187         unsigned int level;
1188
1189         level = entry->id.fields.level;
1190         time = entry->id.stck;
1191         /* adjust todclock to 1970 */
1192         time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
1193         tod_to_timeval(time, &time_val);
1194
1195         if (entry->id.fields.exception)
1196                 except_str = "*";
1197         else
1198                 except_str = "-";
1199         caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1200         rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p  ",
1201                       area, time_val.tv_sec, time_val.tv_usec, level,
1202                       except_str, entry->id.fields.cpuid, (void *) caller);
1203         return rc;
1204 }
1205
1206 /*
1207  * prints debug data sprintf-formated:
1208  * debug_sprinf_event/exception calls must be used together with this view
1209  */
1210
1211 #define DEBUG_SPRINTF_MAX_ARGS 10
1212
1213 int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1214                             char *out_buf, debug_sprintf_entry_t *curr_event)
1215 {
1216         int num_longs, num_used_args = 0,i, rc = 0;
1217         int index[DEBUG_SPRINTF_MAX_ARGS];
1218
1219         /* count of longs fit into one entry */
1220         num_longs = id->buf_size /  sizeof(long); 
1221
1222         if(num_longs < 1)
1223                 goto out; /* bufsize of entry too small */
1224         if(num_longs == 1) {
1225                 /* no args, we use only the string */
1226                 strcpy(out_buf, curr_event->string);
1227                 rc = strlen(curr_event->string);
1228                 goto out;
1229         }
1230
1231         /* number of arguments used for sprintf (without the format string) */
1232         num_used_args   = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1233
1234         memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1235
1236         for(i = 0; i < num_used_args; i++)
1237                 index[i] = i;
1238
1239         rc =  sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1240                 curr_event->args[index[1]], curr_event->args[index[2]],
1241                 curr_event->args[index[3]], curr_event->args[index[4]],
1242                 curr_event->args[index[5]], curr_event->args[index[6]],
1243                 curr_event->args[index[7]], curr_event->args[index[8]],
1244                 curr_event->args[index[9]]);
1245
1246 out:
1247
1248         return rc;
1249 }
1250
1251 /*
1252  * clean up module
1253  */
1254 void __exit debug_exit(void)
1255 {
1256 #ifdef DEBUG
1257         printk("debug_cleanup_module: \n");
1258 #endif
1259 #ifdef CONFIG_PROC_FS
1260         remove_proc_entry(debug_proc_root_entry->name, NULL);
1261 #endif /* CONFIG_PROC_FS */
1262         unregister_sysctl_table(s390dbf_sysctl_header);
1263         return;
1264 }
1265
1266 /*
1267  * module definitions
1268  */
1269 core_initcall(debug_init);
1270 module_exit(debug_exit);
1271 MODULE_LICENSE("GPL");
1272
1273 EXPORT_SYMBOL(debug_register);
1274 EXPORT_SYMBOL(debug_unregister); 
1275 EXPORT_SYMBOL(debug_set_level);
1276 EXPORT_SYMBOL(debug_stop_all);
1277 EXPORT_SYMBOL(debug_register_view);
1278 EXPORT_SYMBOL(debug_unregister_view);
1279 EXPORT_SYMBOL(debug_event_common);
1280 EXPORT_SYMBOL(debug_exception_common);
1281 EXPORT_SYMBOL(debug_hex_ascii_view);
1282 EXPORT_SYMBOL(debug_raw_view);
1283 EXPORT_SYMBOL(debug_dflt_header_fn);
1284 EXPORT_SYMBOL(debug_sprintf_view);
1285 EXPORT_SYMBOL(debug_sprintf_exception);
1286 EXPORT_SYMBOL(debug_sprintf_event);