1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 1999, 2012
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
10 * Bugreports to: <Linux390@de.ibm.com>
13 #define KMSG_COMPONENT "s390dbf"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/sysctl.h>
23 #include <linux/uaccess.h>
24 #include <linux/export.h>
25 #include <linux/init.h>
27 #include <linux/debugfs.h>
29 #include <asm/debug.h>
31 #define DEBUG_PROLOG_ENTRY -1
33 #define ALL_AREAS 0 /* copy all debug areas */
34 #define NO_AREAS 1 /* copy no debug areas */
38 typedef struct file_private_info {
39 loff_t offset; /* offset of last read in file */
40 int act_area; /* number of last formated area */
41 int act_page; /* act page in given area */
42 int act_entry; /* last formated entry (offset */
43 /* relative to beginning of last */
45 size_t act_entry_offset; /* up to this offset we copied */
46 /* in last read the last formated */
47 /* entry to userland */
48 char temp_buf[2048]; /* buffer for output */
49 debug_info_t *debug_info_org; /* original debug information */
50 debug_info_t *debug_info_snap; /* snapshot of debug information */
51 struct debug_view *view; /* used view of debug info */
52 } file_private_info_t;
57 * This assumes that all args are converted into longs
58 * on L/390 this is the case for all types of parameter
59 * except of floats, and long long (32 bit)
63 } debug_sprintf_entry_t;
65 /* internal function prototyes */
67 static int debug_init(void);
68 static ssize_t debug_output(struct file *file, char __user *user_buf,
69 size_t user_len, loff_t *offset);
70 static ssize_t debug_input(struct file *file, const char __user *user_buf,
71 size_t user_len, loff_t *offset);
72 static int debug_open(struct inode *inode, struct file *file);
73 static int debug_close(struct inode *inode, struct file *file);
74 static debug_info_t *debug_info_create(const char *name, int pages_per_area,
75 int nr_areas, int buf_size, umode_t mode);
76 static void debug_info_get(debug_info_t *);
77 static void debug_info_put(debug_info_t *);
78 static int debug_prolog_level_fn(debug_info_t *id,
79 struct debug_view *view, char *out_buf);
80 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
81 struct file *file, const char __user *user_buf,
82 size_t user_buf_size, loff_t *offset);
83 static int debug_prolog_pages_fn(debug_info_t *id,
84 struct debug_view *view, char *out_buf);
85 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
86 struct file *file, const char __user *user_buf,
87 size_t user_buf_size, loff_t *offset);
88 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
89 struct file *file, const char __user *user_buf,
90 size_t user_buf_size, loff_t *offset);
91 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
92 char *out_buf, const char *in_buf);
93 static int debug_raw_format_fn(debug_info_t *id,
94 struct debug_view *view, char *out_buf,
96 static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
97 int area, debug_entry_t *entry, char *out_buf);
99 static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
100 char *out_buf, debug_sprintf_entry_t *curr_event);
104 struct debug_view debug_raw_view = {
107 &debug_raw_header_fn,
108 &debug_raw_format_fn,
112 EXPORT_SYMBOL(debug_raw_view);
114 struct debug_view debug_hex_ascii_view = {
117 &debug_dflt_header_fn,
118 &debug_hex_ascii_format_fn,
122 EXPORT_SYMBOL(debug_hex_ascii_view);
124 static struct debug_view debug_level_view = {
126 &debug_prolog_level_fn,
129 &debug_input_level_fn,
133 static struct debug_view debug_pages_view = {
135 &debug_prolog_pages_fn,
138 &debug_input_pages_fn,
142 static struct debug_view debug_flush_view = {
147 &debug_input_flush_fn,
151 struct debug_view debug_sprintf_view = {
154 &debug_dflt_header_fn,
155 (debug_format_proc_t *)&debug_sprintf_format_fn,
159 EXPORT_SYMBOL(debug_sprintf_view);
161 /* used by dump analysis tools to determine version of debug feature */
162 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
166 static debug_info_t *debug_area_first;
167 static debug_info_t *debug_area_last;
168 static DEFINE_MUTEX(debug_mutex);
170 static int initialized;
171 static int debug_critical;
173 static const struct file_operations debug_file_ops = {
174 .owner = THIS_MODULE,
175 .read = debug_output,
176 .write = debug_input,
178 .release = debug_close,
182 static struct dentry *debug_debugfs_root_entry;
188 * - Debug areas are implemented as a threedimensonal array:
189 * areas[areanumber][pagenumber][pageoffset]
192 static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
194 debug_entry_t ***areas;
197 areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
201 /* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
202 areas[i] = kmalloc_array(pages_per_area,
203 sizeof(debug_entry_t *),
204 GFP_KERNEL | __GFP_NOWARN);
206 goto fail_malloc_areas2;
207 for (j = 0; j < pages_per_area; j++) {
208 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
210 for (j--; j >= 0 ; j--)
213 goto fail_malloc_areas2;
220 for (i--; i >= 0; i--) {
221 for (j = 0; j < pages_per_area; j++)
232 * - alloc new debug-info
234 static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
235 int nr_areas, int buf_size, int level,
240 /* alloc everything */
241 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
244 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
245 if (!rc->active_entries)
246 goto fail_malloc_active_entries;
247 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
248 if (!rc->active_pages)
249 goto fail_malloc_active_pages;
250 if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
251 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
253 goto fail_malloc_areas;
258 /* initialize members */
259 spin_lock_init(&rc->lock);
260 rc->pages_per_area = pages_per_area;
261 rc->nr_areas = nr_areas;
264 rc->buf_size = buf_size;
265 rc->entry_size = sizeof(debug_entry_t) + buf_size;
266 strlcpy(rc->name, name, sizeof(rc->name));
267 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
268 memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
269 refcount_set(&(rc->ref_count), 0);
274 kfree(rc->active_pages);
275 fail_malloc_active_pages:
276 kfree(rc->active_entries);
277 fail_malloc_active_entries:
285 * - free all debug areas
287 static void debug_areas_free(debug_info_t *db_info)
293 for (i = 0; i < db_info->nr_areas; i++) {
294 for (j = 0; j < db_info->pages_per_area; j++)
295 kfree(db_info->areas[i][j]);
296 kfree(db_info->areas[i]);
298 kfree(db_info->areas);
299 db_info->areas = NULL;
304 * - free memory debug-info
306 static void debug_info_free(debug_info_t *db_info)
308 debug_areas_free(db_info);
309 kfree(db_info->active_entries);
310 kfree(db_info->active_pages);
316 * - create new debug-info
319 static debug_info_t *debug_info_create(const char *name, int pages_per_area,
320 int nr_areas, int buf_size, umode_t mode)
324 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
325 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
329 rc->mode = mode & ~S_IFMT;
330 refcount_set(&rc->ref_count, 1);
339 static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
345 /* get a consistent copy of the debug areas */
347 rc = debug_info_alloc(in->name, in->pages_per_area,
348 in->nr_areas, in->buf_size, in->level, mode);
349 spin_lock_irqsave(&in->lock, flags);
352 /* has something changed in the meantime ? */
353 if ((rc->pages_per_area == in->pages_per_area) &&
354 (rc->nr_areas == in->nr_areas)) {
357 spin_unlock_irqrestore(&in->lock, flags);
361 if (mode == NO_AREAS)
364 for (i = 0; i < in->nr_areas; i++) {
365 for (j = 0; j < in->pages_per_area; j++)
366 memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
369 spin_unlock_irqrestore(&in->lock, flags);
375 * - increments reference count for debug-info
377 static void debug_info_get(debug_info_t *db_info)
380 refcount_inc(&db_info->ref_count);
385 * - decreases reference count for debug-info and frees it if necessary
387 static void debug_info_put(debug_info_t *db_info)
391 if (refcount_dec_and_test(&db_info->ref_count))
392 debug_info_free(db_info);
396 * debug_format_entry:
397 * - format one debug entry and return size of formated data
399 static int debug_format_entry(file_private_info_t *p_info)
401 debug_info_t *id_snap = p_info->debug_info_snap;
402 struct debug_view *view = p_info->view;
403 debug_entry_t *act_entry;
406 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
408 if (view->prolog_proc)
409 len += view->prolog_proc(id_snap, view, p_info->temp_buf);
412 if (!id_snap->areas) /* this is true, if we have a prolog only view */
413 goto out; /* or if 'pages_per_area' is 0 */
414 act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
415 [p_info->act_page] + p_info->act_entry);
417 if (act_entry->id.stck == 0LL)
418 goto out; /* empty entry */
419 if (view->header_proc)
420 len += view->header_proc(id_snap, view, p_info->act_area,
421 act_entry, p_info->temp_buf + len);
422 if (view->format_proc)
423 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
424 DEBUG_DATA(act_entry));
431 * - goto next entry in p_info
433 static inline int debug_next_entry(file_private_info_t *p_info)
437 id = p_info->debug_info_snap;
438 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
439 p_info->act_entry = 0;
440 p_info->act_page = 0;
445 p_info->act_entry += id->entry_size;
446 /* switch to next page, if we reached the end of the page */
447 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
449 p_info->act_entry = 0;
450 p_info->act_page += 1;
451 if ((p_info->act_page % id->pages_per_area) == 0) {
454 p_info->act_page = 0;
456 if (p_info->act_area >= id->nr_areas)
465 * - called for user read()
466 * - copies formated debug entries to the user buffer
468 static ssize_t debug_output(struct file *file, /* file descriptor */
469 char __user *user_buf, /* user buffer */
470 size_t len, /* length of buffer */
471 loff_t *offset) /* offset in the file */
475 file_private_info_t *p_info;
477 p_info = (file_private_info_t *) file->private_data;
478 if (*offset != p_info->offset)
480 if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
482 entry_offset = p_info->act_entry_offset;
483 while (count < len) {
484 int formatted_line_residue;
485 int formatted_line_size;
486 int user_buf_residue;
489 formatted_line_size = debug_format_entry(p_info);
490 formatted_line_residue = formatted_line_size - entry_offset;
491 user_buf_residue = len-count;
492 copy_size = min(user_buf_residue, formatted_line_residue);
494 if (copy_to_user(user_buf + count, p_info->temp_buf
495 + entry_offset, copy_size))
498 entry_offset += copy_size;
500 if (copy_size == formatted_line_residue) {
502 if (debug_next_entry(p_info))
507 p_info->offset = *offset + count;
508 p_info->act_entry_offset = entry_offset;
509 *offset = p_info->offset;
515 * - called for user write()
516 * - calls input function of view
518 static ssize_t debug_input(struct file *file, const char __user *user_buf,
519 size_t length, loff_t *offset)
521 file_private_info_t *p_info;
524 mutex_lock(&debug_mutex);
525 p_info = ((file_private_info_t *) file->private_data);
526 if (p_info->view->input_proc) {
527 rc = p_info->view->input_proc(p_info->debug_info_org,
528 p_info->view, file, user_buf,
533 mutex_unlock(&debug_mutex);
534 return rc; /* number of input characters */
539 * - called for user open()
540 * - copies formated output to private_data area of the file
543 static int debug_open(struct inode *inode, struct file *file)
545 debug_info_t *debug_info, *debug_info_snapshot;
546 file_private_info_t *p_info;
549 mutex_lock(&debug_mutex);
550 debug_info = file_inode(file)->i_private;
551 /* find debug view */
552 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
553 if (!debug_info->views[i])
555 else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
556 goto found; /* found view ! */
564 /* Make snapshot of current debug areas to get it consistent. */
565 /* To copy all the areas is only needed, if we have a view which */
566 /* formats the debug areas. */
568 if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
569 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
571 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
573 if (!debug_info_snapshot) {
577 p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
579 debug_info_free(debug_info_snapshot);
584 p_info->debug_info_snap = debug_info_snapshot;
585 p_info->debug_info_org = debug_info;
586 p_info->view = debug_info->views[i];
587 p_info->act_area = 0;
588 p_info->act_page = 0;
589 p_info->act_entry = DEBUG_PROLOG_ENTRY;
590 p_info->act_entry_offset = 0;
591 file->private_data = p_info;
592 debug_info_get(debug_info);
593 nonseekable_open(inode, file);
595 mutex_unlock(&debug_mutex);
601 * - called for user close()
602 * - deletes private_data area of the file handle
604 static int debug_close(struct inode *inode, struct file *file)
606 file_private_info_t *p_info;
608 p_info = (file_private_info_t *) file->private_data;
609 if (p_info->debug_info_snap)
610 debug_info_free(p_info->debug_info_snap);
611 debug_info_put(p_info->debug_info_org);
612 kfree(file->private_data);
613 return 0; /* success */
616 /* Create debugfs entries and add to internal list. */
617 static void _debug_register(debug_info_t *id)
619 /* create root directory */
620 id->debugfs_root_entry = debugfs_create_dir(id->name,
621 debug_debugfs_root_entry);
623 /* append new element to linked list */
624 if (!debug_area_first) {
625 /* first element in list */
626 debug_area_first = id;
629 /* append element to end of list */
630 debug_area_last->next = id;
631 id->prev = debug_area_last;
633 debug_area_last = id;
636 debug_register_view(id, &debug_level_view);
637 debug_register_view(id, &debug_flush_view);
638 debug_register_view(id, &debug_pages_view);
642 * debug_register_mode() - creates and initializes debug area.
644 * @name: Name of debug log (e.g. used for debugfs entry)
645 * @pages_per_area: Number of pages, which will be allocated per area
646 * @nr_areas: Number of debug areas
647 * @buf_size: Size of data area in each debug entry
648 * @mode: File mode for debugfs files. E.g. S_IRWXUGO
649 * @uid: User ID for debugfs files. Currently only 0 is supported.
650 * @gid: Group ID for debugfs files. Currently only 0 is supported.
653 * - Handle for generated debug area
654 * - %NULL if register failed
656 * Allocates memory for a debug log.
657 * Must not be called within an interrupt handler.
659 debug_info_t *debug_register_mode(const char *name, int pages_per_area,
660 int nr_areas, int buf_size, umode_t mode,
661 uid_t uid, gid_t gid)
663 debug_info_t *rc = NULL;
665 /* Since debugfs currently does not support uid/gid other than root, */
666 /* we do not allow gid/uid != 0 until we get support for that. */
667 if ((uid != 0) || (gid != 0))
668 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
669 BUG_ON(!initialized);
671 /* create new debug_info */
672 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
674 mutex_lock(&debug_mutex);
676 mutex_unlock(&debug_mutex);
678 pr_err("Registering debug feature %s failed\n", name);
682 EXPORT_SYMBOL(debug_register_mode);
685 * debug_register() - creates and initializes debug area with default file mode.
687 * @name: Name of debug log (e.g. used for debugfs entry)
688 * @pages_per_area: Number of pages, which will be allocated per area
689 * @nr_areas: Number of debug areas
690 * @buf_size: Size of data area in each debug entry
693 * - Handle for generated debug area
694 * - %NULL if register failed
696 * Allocates memory for a debug log.
697 * The debugfs file mode access permissions are read and write for user.
698 * Must not be called within an interrupt handler.
700 debug_info_t *debug_register(const char *name, int pages_per_area,
701 int nr_areas, int buf_size)
703 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
704 S_IRUSR | S_IWUSR, 0, 0);
706 EXPORT_SYMBOL(debug_register);
708 /* Remove debugfs entries and remove from internal list. */
709 static void _debug_unregister(debug_info_t *id)
713 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
716 debugfs_remove(id->debugfs_entries[i]);
718 debugfs_remove(id->debugfs_root_entry);
719 if (id == debug_area_first)
720 debug_area_first = id->next;
721 if (id == debug_area_last)
722 debug_area_last = id->prev;
724 id->prev->next = id->next;
726 id->next->prev = id->prev;
730 * debug_unregister() - give back debug area.
732 * @id: handle for debug log
737 void debug_unregister(debug_info_t *id)
741 mutex_lock(&debug_mutex);
742 _debug_unregister(id);
743 mutex_unlock(&debug_mutex);
747 EXPORT_SYMBOL(debug_unregister);
751 * - set area size (number of pages) and number of areas
753 static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
755 debug_entry_t ***new_areas;
759 if (!id || (nr_areas <= 0) || (pages_per_area < 0))
761 if (pages_per_area > 0) {
762 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
764 pr_info("Allocating memory for %i pages failed\n",
772 spin_lock_irqsave(&id->lock, flags);
773 debug_areas_free(id);
774 id->areas = new_areas;
775 id->nr_areas = nr_areas;
776 id->pages_per_area = pages_per_area;
778 memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
779 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
780 spin_unlock_irqrestore(&id->lock, flags);
781 pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
787 * debug_set_level() - Sets new actual debug level if new_level is valid.
789 * @id: handle for debug log
790 * @new_level: new debug level
795 void debug_set_level(debug_info_t *id, int new_level)
801 spin_lock_irqsave(&id->lock, flags);
802 if (new_level == DEBUG_OFF_LEVEL) {
803 id->level = DEBUG_OFF_LEVEL;
804 pr_info("%s: switched off\n", id->name);
805 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
806 pr_info("%s: level %i is out of range (%i - %i)\n",
807 id->name, new_level, 0, DEBUG_MAX_LEVEL);
809 id->level = new_level;
811 spin_unlock_irqrestore(&id->lock, flags);
813 EXPORT_SYMBOL(debug_set_level);
816 * proceed_active_entry:
817 * - set active entry to next in the ring buffer
819 static inline void proceed_active_entry(debug_info_t *id)
821 if ((id->active_entries[id->active_area] += id->entry_size)
822 > (PAGE_SIZE - id->entry_size)) {
823 id->active_entries[id->active_area] = 0;
824 id->active_pages[id->active_area] =
825 (id->active_pages[id->active_area] + 1) %
831 * proceed_active_area:
832 * - set active area to next in the ring buffer
834 static inline void proceed_active_area(debug_info_t *id)
837 id->active_area = id->active_area % id->nr_areas;
843 static inline debug_entry_t *get_active_entry(debug_info_t *id)
845 return (debug_entry_t *) (((char *) id->areas[id->active_area]
846 [id->active_pages[id->active_area]]) +
847 id->active_entries[id->active_area]);
851 * debug_finish_entry:
852 * - set timestamp, caller address, cpu number etc.
855 static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
856 int level, int exception)
858 active->id.stck = get_tod_clock_fast() -
859 *(unsigned long long *) &tod_clock_base[1];
860 active->id.fields.cpuid = smp_processor_id();
861 active->caller = __builtin_return_address(0);
862 active->id.fields.exception = exception;
863 active->id.fields.level = level;
864 proceed_active_entry(id);
866 proceed_active_area(id);
869 static int debug_stoppable = 1;
870 static int debug_active = 1;
872 #define CTL_S390DBF_STOPPABLE 5678
873 #define CTL_S390DBF_ACTIVE 5679
876 * proc handler for the running debug_active sysctl
877 * always allow read, allow write only if debug_stoppable is set or
878 * if debug_active is already off
880 static int s390dbf_procactive(struct ctl_table *table, int write,
881 void __user *buffer, size_t *lenp, loff_t *ppos)
883 if (!write || debug_stoppable || !debug_active)
884 return proc_dointvec(table, write, buffer, lenp, ppos);
889 static struct ctl_table s390dbf_table[] = {
891 .procname = "debug_stoppable",
892 .data = &debug_stoppable,
893 .maxlen = sizeof(int),
894 .mode = S_IRUGO | S_IWUSR,
895 .proc_handler = proc_dointvec,
898 .procname = "debug_active",
899 .data = &debug_active,
900 .maxlen = sizeof(int),
901 .mode = S_IRUGO | S_IWUSR,
902 .proc_handler = s390dbf_procactive,
907 static struct ctl_table s390dbf_dir_table[] = {
909 .procname = "s390dbf",
911 .mode = S_IRUGO | S_IXUGO,
912 .child = s390dbf_table,
917 static struct ctl_table_header *s390dbf_sysctl_header;
920 * debug_stop_all() - stops the debug feature if stopping is allowed.
925 * Currently used in case of a kernel oops.
927 void debug_stop_all(void)
932 EXPORT_SYMBOL(debug_stop_all);
935 * debug_set_critical() - event/exception functions try lock instead of spin.
940 * Currently used in case of stopping all CPUs but the current one.
941 * Once in this state, functions to write a debug entry for an
942 * event or exception no longer spin on the debug area lock,
943 * but only try to get it and fail if they do not get the lock.
945 void debug_set_critical(void)
951 * debug_event_common:
952 * - write debug entry with given size
954 debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
957 debug_entry_t *active;
960 if (!debug_active || !id->areas)
962 if (debug_critical) {
963 if (!spin_trylock_irqsave(&id->lock, flags))
966 spin_lock_irqsave(&id->lock, flags);
969 active = get_active_entry(id);
970 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
971 if (len < id->buf_size)
972 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
973 debug_finish_entry(id, active, level, 0);
978 spin_unlock_irqrestore(&id->lock, flags);
981 EXPORT_SYMBOL(debug_event_common);
984 * debug_exception_common:
985 * - write debug entry with given size and switch to next debug area
987 debug_entry_t *debug_exception_common(debug_info_t *id, int level,
988 const void *buf, int len)
990 debug_entry_t *active;
993 if (!debug_active || !id->areas)
995 if (debug_critical) {
996 if (!spin_trylock_irqsave(&id->lock, flags))
999 spin_lock_irqsave(&id->lock, flags);
1002 active = get_active_entry(id);
1003 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
1004 if (len < id->buf_size)
1005 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
1006 debug_finish_entry(id, active, level, len <= id->buf_size);
1007 len -= id->buf_size;
1008 buf += id->buf_size;
1011 spin_unlock_irqrestore(&id->lock, flags);
1014 EXPORT_SYMBOL(debug_exception_common);
1017 * counts arguments in format string for sprintf view
1019 static inline int debug_count_numargs(char *string)
1024 if (*string++ == '%')
1031 * debug_sprintf_event:
1033 debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
1035 debug_sprintf_entry_t *curr_event;
1036 debug_entry_t *active;
1037 unsigned long flags;
1041 if (!debug_active || !id->areas)
1043 numargs = debug_count_numargs(string);
1045 if (debug_critical) {
1046 if (!spin_trylock_irqsave(&id->lock, flags))
1049 spin_lock_irqsave(&id->lock, flags);
1051 active = get_active_entry(id);
1052 curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
1053 va_start(ap, string);
1054 curr_event->string = string;
1055 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1056 curr_event->args[idx] = va_arg(ap, long);
1058 debug_finish_entry(id, active, level, 0);
1059 spin_unlock_irqrestore(&id->lock, flags);
1063 EXPORT_SYMBOL(__debug_sprintf_event);
1066 * debug_sprintf_exception:
1068 debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
1070 debug_sprintf_entry_t *curr_event;
1071 debug_entry_t *active;
1072 unsigned long flags;
1076 if (!debug_active || !id->areas)
1079 numargs = debug_count_numargs(string);
1081 if (debug_critical) {
1082 if (!spin_trylock_irqsave(&id->lock, flags))
1085 spin_lock_irqsave(&id->lock, flags);
1087 active = get_active_entry(id);
1088 curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
1089 va_start(ap, string);
1090 curr_event->string = string;
1091 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1092 curr_event->args[idx] = va_arg(ap, long);
1094 debug_finish_entry(id, active, level, 1);
1095 spin_unlock_irqrestore(&id->lock, flags);
1099 EXPORT_SYMBOL(__debug_sprintf_exception);
1102 * debug_register_view() - registers new debug view and creates debugfs
1105 * @id: handle for debug log
1106 * @view: pointer to debug view struct
1112 int debug_register_view(debug_info_t *id, struct debug_view *view)
1114 unsigned long flags;
1122 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1123 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1124 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1125 if (!view->input_proc)
1126 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1127 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1128 id, &debug_file_ops);
1129 spin_lock_irqsave(&id->lock, flags);
1130 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1134 if (i == DEBUG_MAX_VIEWS) {
1135 pr_err("Registering view %s/%s would exceed the maximum "
1136 "number of views %i\n", id->name, view->name, i);
1139 id->views[i] = view;
1140 id->debugfs_entries[i] = pde;
1142 spin_unlock_irqrestore(&id->lock, flags);
1144 debugfs_remove(pde);
1148 EXPORT_SYMBOL(debug_register_view);
1151 * debug_unregister_view() - unregisters debug view and removes debugfs
1154 * @id: handle for debug log
1155 * @view: pointer to debug view struct
1161 int debug_unregister_view(debug_info_t *id, struct debug_view *view)
1163 struct dentry *dentry = NULL;
1164 unsigned long flags;
1169 spin_lock_irqsave(&id->lock, flags);
1170 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1171 if (id->views[i] == view)
1174 if (i == DEBUG_MAX_VIEWS) {
1177 dentry = id->debugfs_entries[i];
1178 id->views[i] = NULL;
1179 id->debugfs_entries[i] = NULL;
1181 spin_unlock_irqrestore(&id->lock, flags);
1182 debugfs_remove(dentry);
1186 EXPORT_SYMBOL(debug_unregister_view);
1188 static inline char *debug_get_user_string(const char __user *user_buf,
1193 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1195 return ERR_PTR(-ENOMEM);
1196 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1198 return ERR_PTR(-EFAULT);
1200 /* got the string, now strip linefeed. */
1201 if (buffer[user_len - 1] == '\n')
1202 buffer[user_len - 1] = 0;
1204 buffer[user_len] = 0;
1208 static inline int debug_get_uint(char *buf)
1212 buf = skip_spaces(buf);
1213 rc = simple_strtoul(buf, &buf, 10);
1220 * functions for debug-views
1221 ***********************************
1225 * prints out actual debug level
1228 static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1231 return sprintf(out_buf, "%i\n", id->pages_per_area);
1235 * reads new size (number of pages per debug area)
1238 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
1239 struct file *file, const char __user *user_buf,
1240 size_t user_len, loff_t *offset)
1245 if (user_len > 0x10000)
1251 str = debug_get_user_string(user_buf, user_len);
1256 new_pages = debug_get_uint(str);
1257 if (new_pages < 0) {
1261 rc = debug_set_size(id, id->nr_areas, new_pages);
1270 *offset += user_len;
1271 return rc; /* number of input characters */
1275 * prints out actual debug level
1277 static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1282 if (id->level == DEBUG_OFF_LEVEL)
1283 rc = sprintf(out_buf, "-\n");
1285 rc = sprintf(out_buf, "%i\n", id->level);
1290 * reads new debug level
1292 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
1293 struct file *file, const char __user *user_buf,
1294 size_t user_len, loff_t *offset)
1299 if (user_len > 0x10000)
1305 str = debug_get_user_string(user_buf, user_len);
1310 if (str[0] == '-') {
1311 debug_set_level(id, DEBUG_OFF_LEVEL);
1315 new_level = debug_get_uint(str);
1317 if (new_level < 0) {
1318 pr_warn("%s is not a valid level for a debug feature\n", str);
1321 debug_set_level(id, new_level);
1327 *offset += user_len;
1328 return rc; /* number of input characters */
1332 * flushes debug areas
1334 static void debug_flush(debug_info_t *id, int area)
1336 unsigned long flags;
1339 if (!id || !id->areas)
1341 spin_lock_irqsave(&id->lock, flags);
1342 if (area == DEBUG_FLUSH_ALL) {
1343 id->active_area = 0;
1344 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1345 for (i = 0; i < id->nr_areas; i++) {
1346 id->active_pages[i] = 0;
1347 for (j = 0; j < id->pages_per_area; j++)
1348 memset(id->areas[i][j], 0, PAGE_SIZE);
1350 } else if (area >= 0 && area < id->nr_areas) {
1351 id->active_entries[area] = 0;
1352 id->active_pages[area] = 0;
1353 for (i = 0; i < id->pages_per_area; i++)
1354 memset(id->areas[area][i], 0, PAGE_SIZE);
1356 spin_unlock_irqrestore(&id->lock, flags);
1360 * view function: flushes debug areas
1362 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
1363 struct file *file, const char __user *user_buf,
1364 size_t user_len, loff_t *offset)
1369 if (user_len > 0x10000)
1375 if (copy_from_user(input_buf, user_buf, 1)) {
1379 if (input_buf[0] == '-') {
1380 debug_flush(id, DEBUG_FLUSH_ALL);
1383 if (isdigit(input_buf[0])) {
1384 int area = ((int) input_buf[0] - (int) '0');
1386 debug_flush(id, area);
1390 pr_info("Flushing debug data failed because %c is not a valid "
1391 "area\n", input_buf[0]);
1394 *offset += user_len;
1395 return rc; /* number of input characters */
1399 * prints debug header in raw format
1401 static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
1402 int area, debug_entry_t *entry, char *out_buf)
1406 rc = sizeof(debug_entry_t);
1407 memcpy(out_buf, entry, sizeof(debug_entry_t));
1412 * prints debug data in raw format
1414 static int debug_raw_format_fn(debug_info_t *id, struct debug_view *view,
1415 char *out_buf, const char *in_buf)
1420 memcpy(out_buf, in_buf, id->buf_size);
1425 * prints debug data in hex/ascii format
1427 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1428 char *out_buf, const char *in_buf)
1432 for (i = 0; i < id->buf_size; i++)
1433 rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
1434 rc += sprintf(out_buf + rc, "| ");
1435 for (i = 0; i < id->buf_size; i++) {
1436 unsigned char c = in_buf[i];
1438 if (isascii(c) && isprint(c))
1439 rc += sprintf(out_buf + rc, "%c", c);
1441 rc += sprintf(out_buf + rc, ".");
1443 rc += sprintf(out_buf + rc, "\n");
1448 * prints header for debug entry
1450 int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1451 int area, debug_entry_t *entry, char *out_buf)
1453 unsigned long base, sec, usec;
1454 unsigned long caller;
1459 level = entry->id.fields.level;
1460 base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
1461 sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
1462 usec = do_div(sec, USEC_PER_SEC);
1464 if (entry->id.fields.exception)
1468 caller = (unsigned long) entry->caller;
1469 rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %02i %pK ",
1470 area, sec, usec, level, except_str,
1471 entry->id.fields.cpuid, (void *)caller);
1474 EXPORT_SYMBOL(debug_dflt_header_fn);
1477 * prints debug data sprintf-formated:
1478 * debug_sprinf_event/exception calls must be used together with this view
1481 #define DEBUG_SPRINTF_MAX_ARGS 10
1483 static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1484 char *out_buf, debug_sprintf_entry_t *curr_event)
1486 int num_longs, num_used_args = 0, i, rc = 0;
1487 int index[DEBUG_SPRINTF_MAX_ARGS];
1489 /* count of longs fit into one entry */
1490 num_longs = id->buf_size / sizeof(long);
1493 goto out; /* bufsize of entry too small */
1494 if (num_longs == 1) {
1495 /* no args, we use only the string */
1496 strcpy(out_buf, curr_event->string);
1497 rc = strlen(curr_event->string);
1501 /* number of arguments used for sprintf (without the format string) */
1502 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1504 memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1506 for (i = 0; i < num_used_args; i++)
1509 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1510 curr_event->args[index[1]], curr_event->args[index[2]],
1511 curr_event->args[index[3]], curr_event->args[index[4]],
1512 curr_event->args[index[5]], curr_event->args[index[6]],
1513 curr_event->args[index[7]], curr_event->args[index[8]],
1514 curr_event->args[index[9]]);
1521 * - is called exactly once to initialize the debug feature
1523 static int __init debug_init(void)
1525 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1526 mutex_lock(&debug_mutex);
1527 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
1529 mutex_unlock(&debug_mutex);
1532 postcore_initcall(debug_init);