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