blob: d374f9b218b4c278101ebcd451c7304c0ef390a0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * S/390 debug facility
4 *
Michael Holzheu3ab121a2012-03-11 11:59:32 -04005 * Copyright IBM Corp. 1999, 2012
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
Heiko Carstens496da0d2017-10-13 09:06:29 +02008 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Bugreports to: <Linux390@de.ibm.com>
11 */
12
Michael Holzheuc5612c12008-12-25 13:39:43 +010013#define KMSG_COMPONENT "s390dbf"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 Rosae7d28602009-12-14 18:01:06 -080021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/sysctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Paul Gortmaker3994a522017-02-09 15:20:23 -050024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Michael Holzheu66a464d2005-06-25 14:55:33 -070026#include <linux/fs.h>
27#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/debug.h>
30
31#define DEBUG_PROLOG_ENTRY -1
32
Michael Holzheu66a464d2005-06-25 14:55:33 -070033#define ALL_AREAS 0 /* copy all debug areas */
34#define NO_AREAS 1 /* copy no debug areas */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* typedefs */
37
38typedef struct file_private_info {
39 loff_t offset; /* offset of last read in file */
Heiko Carstens496da0d2017-10-13 09:06:29 +020040 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 Torvalds1da177e2005-04-16 15:20:36 -070046 /* in last read the last formated */
47 /* entry to userland */
48 char temp_buf[2048]; /* buffer for output */
Heiko Carstens496da0d2017-10-13 09:06:29 +020049 debug_info_t *debug_info_org; /* original debug information */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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 Carstens496da0d2017-10-13 09:06:29 +020054typedef struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char *string;
Heiko Carstens496da0d2017-10-13 09:06:29 +020056 /*
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 Holzheu66a464d2005-06-25 14:55:33 -070060 *
61 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 long args[0];
63} debug_sprintf_entry_t;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* internal function prototyes */
66
67static int debug_init(void);
68static ssize_t debug_output(struct file *file, char __user *user_buf,
Heiko Carstens496da0d2017-10-13 09:06:29 +020069 size_t user_len, loff_t *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static ssize_t debug_input(struct file *file, const char __user *user_buf,
Heiko Carstens496da0d2017-10-13 09:06:29 +020071 size_t user_len, loff_t *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int debug_open(struct inode *inode, struct file *file);
73static int debug_close(struct inode *inode, struct file *file);
Cornelia Huck5cbbf162008-05-15 16:52:35 +020074static debug_info_t *debug_info_create(const char *name, int pages_per_area,
Heiko Carstens496da0d2017-10-13 09:06:29 +020075 int nr_areas, int buf_size, umode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void debug_info_get(debug_info_t *);
77static void debug_info_put(debug_info_t *);
Heiko Carstens496da0d2017-10-13 09:06:29 +020078static int debug_prolog_level_fn(debug_info_t *id,
79 struct debug_view *view, char *out_buf);
80static 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);
83static int debug_prolog_pages_fn(debug_info_t *id,
84 struct debug_view *view, char *out_buf);
85static 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);
88static 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);
91static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
92 char *out_buf, const char *in_buf);
93static int debug_raw_format_fn(debug_info_t *id,
94 struct debug_view *view, char *out_buf,
95 const char *in_buf);
96static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
97 int area, debug_entry_t *entry, char *out_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Heiko Carstens496da0d2017-10-13 09:06:29 +020099static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
100 char *out_buf, debug_sprintf_entry_t *curr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* globals */
103
104struct 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 Carstensd5274362012-07-24 14:27:53 +0200112EXPORT_SYMBOL(debug_raw_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114struct 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 Carstensd5274362012-07-24 14:27:53 +0200122EXPORT_SYMBOL(debug_hex_ascii_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100124static struct debug_view debug_level_view = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 "level",
126 &debug_prolog_level_fn,
127 NULL,
128 NULL,
129 &debug_input_level_fn,
130 NULL
131};
132
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100133static struct debug_view debug_pages_view = {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700134 "pages",
135 &debug_prolog_pages_fn,
136 NULL,
137 NULL,
138 &debug_input_pages_fn,
139 NULL
140};
141
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100142static struct debug_view debug_flush_view = {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200143 "flush",
144 NULL,
145 NULL,
146 NULL,
147 &debug_input_flush_fn,
148 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct debug_view debug_sprintf_view = {
152 "sprintf",
153 NULL,
154 &debug_dflt_header_fn,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200155 (debug_format_proc_t *)&debug_sprintf_format_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 NULL,
157 NULL
158};
Heiko Carstensd5274362012-07-24 14:27:53 +0200159EXPORT_SYMBOL(debug_sprintf_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100161/* used by dump analysis tools to determine version of debug feature */
Heiko Carstensa8061702008-04-17 07:46:26 +0200162static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/* static globals */
165
Heiko Carstens496da0d2017-10-13 09:06:29 +0200166static debug_info_t *debug_area_first;
167static debug_info_t *debug_area_last;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200168static DEFINE_MUTEX(debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170static int initialized;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400171static int debug_critical;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800173static const struct file_operations debug_file_ops = {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200174 .owner = THIS_MODULE,
175 .read = debug_output,
176 .write = debug_input,
177 .open = debug_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .release = debug_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200179 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Michael Holzheu66a464d2005-06-25 14:55:33 -0700182static struct dentry *debug_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/* functions */
185
186/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700187 * debug_areas_alloc
188 * - Debug areas are implemented as a threedimensonal array:
189 * areas[areanumber][pagenumber][pageoffset]
190 */
191
Heiko Carstens496da0d2017-10-13 09:06:29 +0200192static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700193{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200194 debug_entry_t ***areas;
195 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700196
Kees Cook6da2ec52018-06-12 13:55:00 -0700197 areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700198 if (!areas)
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700201 areas[i] = kmalloc_array(pages_per_area,
202 sizeof(debug_entry_t *),
203 GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200204 if (!areas[i])
Michael Holzheu66a464d2005-06-25 14:55:33 -0700205 goto fail_malloc_areas2;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200206 for (j = 0; j < pages_per_area; j++) {
Eric Sesterhennfb630512006-03-24 03:15:31 -0800207 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200208 if (!areas[i][j]) {
209 for (j--; j >= 0 ; j--)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700210 kfree(areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700211 kfree(areas[i]);
212 goto fail_malloc_areas2;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700213 }
214 }
215 }
216 return areas;
217
218fail_malloc_areas2:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200219 for (i--; i >= 0; i--) {
220 for (j = 0; j < pages_per_area; j++)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700221 kfree(areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700222 kfree(areas[i]);
223 }
224 kfree(areas);
225fail_malloc_areas:
226 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700227}
228
Michael Holzheu66a464d2005-06-25 14:55:33 -0700229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * debug_info_alloc
231 * - alloc new debug-info
232 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200233static 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 Torvalds1da177e2005-04-16 15:20:36 -0700236{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200237 debug_info_t *rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* alloc everything */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800240 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200241 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto fail_malloc_rc;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800243 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200244 if (!rc->active_entries)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700245 goto fail_malloc_active_entries;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800246 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200247 if (!rc->active_pages)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700248 goto fail_malloc_active_pages;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200249 if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700250 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200251 if (!rc->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700252 goto fail_malloc_areas;
253 } else {
254 rc->areas = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
257 /* initialize members */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_lock_init(&rc->lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700259 rc->pages_per_area = pages_per_area;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200260 rc->nr_areas = nr_areas;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700261 rc->active_area = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200262 rc->level = level;
263 rc->buf_size = buf_size;
264 rc->entry_size = sizeof(debug_entry_t) + buf_size;
Jean Delvare20cb9e72007-03-19 13:18:53 +0100265 strlcpy(rc->name, name, sizeof(rc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
Heiko Carstens496da0d2017-10-13 09:06:29 +0200267 memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100268 refcount_set(&(rc->ref_count), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return rc;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272fail_malloc_areas:
Michael Holzheu66a464d2005-06-25 14:55:33 -0700273 kfree(rc->active_pages);
274fail_malloc_active_pages:
275 kfree(rc->active_entries);
276fail_malloc_active_entries:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 kfree(rc);
278fail_malloc_rc:
279 return NULL;
280}
281
282/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700283 * debug_areas_free
284 * - free all debug areas
285 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200286static void debug_areas_free(debug_info_t *db_info)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700287{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200288 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700289
Heiko Carstens496da0d2017-10-13 09:06:29 +0200290 if (!db_info->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700291 return;
292 for (i = 0; i < db_info->nr_areas; i++) {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200293 for (j = 0; j < db_info->pages_per_area; j++)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700294 kfree(db_info->areas[i][j]);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700295 kfree(db_info->areas[i]);
296 }
297 kfree(db_info->areas);
298 db_info->areas = NULL;
299}
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * debug_info_free
303 * - free memory debug-info
304 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200305static void debug_info_free(debug_info_t *db_info)
306{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700307 debug_areas_free(db_info);
308 kfree(db_info->active_entries);
309 kfree(db_info->active_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 kfree(db_info);
311}
312
313/*
314 * debug_info_create
315 * - create new debug-info
316 */
317
Heiko Carstens496da0d2017-10-13 09:06:29 +0200318static debug_info_t *debug_info_create(const char *name, int pages_per_area,
319 int nr_areas, int buf_size, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200321 debug_info_t *rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Heiko Carstens496da0d2017-10-13 09:06:29 +0200323 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
324 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
325 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto out;
327
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200328 rc->mode = mode & ~S_IFMT;
329
Michael Holzheu66a464d2005-06-25 14:55:33 -0700330 /* create root directory */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200331 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
332 debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* append new element to linked list */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200335 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 Torvalds1da177e2005-04-16 15:20:36 -0700346
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100347 refcount_set(&rc->ref_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348out:
349 return rc;
350}
351
352/*
353 * debug_info_copy
354 * - copy debug-info
355 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200356static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200358 unsigned long flags;
359 debug_info_t *rc;
360 int i, j;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700361
Michael Holzheu942eaab2005-09-03 15:57:58 -0700362 /* 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 Carstens496da0d2017-10-13 09:06:29 +0200367 if (!rc)
Michael Holzheu942eaab2005-09-03 15:57:58 -0700368 goto out;
369 /* has something changed in the meantime ? */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200370 if ((rc->pages_per_area == in->pages_per_area) &&
371 (rc->nr_areas == in->nr_areas)) {
Michael Holzheu942eaab2005-09-03 15:57:58 -0700372 break;
373 }
374 spin_unlock_irqrestore(&in->lock, flags);
375 debug_info_free(rc);
376 } while (1);
377
Julia Lawallacfa9222008-12-25 13:39:30 +0100378 if (mode == NO_AREAS)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Heiko Carstens496da0d2017-10-13 09:06:29 +0200381 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 Torvalds1da177e2005-04-16 15:20:36 -0700385out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200386 spin_unlock_irqrestore(&in->lock, flags);
387 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/*
391 * debug_info_get
392 * - increments reference count for debug-info
393 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200394static void debug_info_get(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 if (db_info)
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100397 refcount_inc(&db_info->ref_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/*
401 * debug_info_put:
402 * - decreases reference count for debug-info and frees it if necessary
403 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200404static void debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 int i;
407
408 if (!db_info)
409 return;
Elena Reshetovaefc0c212017-03-02 12:23:45 +0100410 if (refcount_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700412 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700414 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700416 debugfs_remove(db_info->debugfs_root_entry);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200417 if (db_info == debug_area_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 debug_area_first = db_info->next;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200419 if (db_info == debug_area_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 debug_area_last = db_info->prev;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200421 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 Torvalds1da177e2005-04-16 15:20:36 -0700425 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 Carstens496da0d2017-10-13 09:06:29 +0200433static int debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200435 debug_info_t *id_snap = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct debug_view *view = p_info->view;
437 debug_entry_t *act_entry;
438 size_t len = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200439
440 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* print prolog */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200442 if (view->prolog_proc)
443 len += view->prolog_proc(id_snap, view, p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out;
445 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700446 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 Carstens496da0d2017-10-13 09:06:29 +0200448 act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
449 [p_info->act_page] + p_info->act_entry);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (act_entry->id.stck == 0LL)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200452 goto out; /* empty entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700454 len += view->header_proc(id_snap, view, p_info->act_area,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200455 act_entry, p_info->temp_buf + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700457 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Heiko Carstens496da0d2017-10-13 09:06:29 +0200458 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700459out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200460 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * debug_next_entry:
465 * - goto next entry in p_info
466 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200467static inline int debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700469 debug_info_t *id;
470
471 id = p_info->debug_info_snap;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200472 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700474 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto out;
476 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200477 if (!id->areas)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700478 return 1;
479 p_info->act_entry += id->entry_size;
480 /* switch to next page, if we reached the end of the page */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200481 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700482 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700484 p_info->act_page += 1;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200485 if ((p_info->act_page % id->pages_per_area) == 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700486 /* next area */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200487 p_info->act_area++;
488 p_info->act_page = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700489 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200490 if (p_info->act_area >= id->nr_areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 1;
492 }
493out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/*
498 * debug_output:
499 * - called for user read()
500 * - copies formated debug entries to the user buffer
501 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200502static 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 Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700508 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 file_private_info_t *p_info;
510
Heiko Carstens496da0d2017-10-13 09:06:29 +0200511 p_info = (file_private_info_t *) file->private_data;
512 if (*offset != p_info->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EPIPE;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200514 if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 entry_offset = p_info->act_entry_offset;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200517 while (count < len) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700518 int formatted_line_residue;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200519 int formatted_line_size;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700520 int user_buf_residue;
521 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Michael Holzheu66a464d2005-06-25 14:55:33 -0700523 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 Carstens496da0d2017-10-13 09:06:29 +0200527 if (copy_size) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700528 if (copy_to_user(user_buf + count, p_info->temp_buf
Heiko Carstens496da0d2017-10-13 09:06:29 +0200529 + entry_offset, copy_size))
Michael Holzheu66a464d2005-06-25 14:55:33 -0700530 return -EFAULT;
531 count += copy_size;
532 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200534 if (copy_size == formatted_line_residue) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700535 entry_offset = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200536 if (debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200541 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700542 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *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 Carstens496da0d2017-10-13 09:06:29 +0200552static ssize_t debug_input(struct file *file, const char __user *user_buf,
553 size_t length, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 file_private_info_t *p_info;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200556 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200558 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 p_info = ((file_private_info_t *) file->private_data);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200560 if (p_info->view->input_proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 rc = p_info->view->input_proc(p_info->debug_info_org,
562 p_info->view, file, user_buf,
563 length, offset);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200564 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = -EPERM;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200566 }
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200567 mutex_unlock(&debug_mutex);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200568 return rc; /* number of input characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
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 Carstens496da0d2017-10-13 09:06:29 +0200577static int debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 debug_info_t *debug_info, *debug_info_snapshot;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200580 file_private_info_t *p_info;
581 int i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200583 mutex_lock(&debug_mutex);
Al Viro496ad9a2013-01-23 17:07:38 -0500584 debug_info = file_inode(file)->i_private;
Michael Holzheu942eaab2005-09-03 15:57:58 -0700585 /* find debug view */
586 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
587 if (!debug_info->views[i])
588 continue;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200589 else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
590 goto found; /* found view ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 /* no entry found */
593 rc = -EINVAL;
594 goto out;
595
Michael Holzheu66a464d2005-06-25 14:55:33 -0700596found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Heiko Carstens496da0d2017-10-13 09:06:29 +0200598 /* Make snapshot of current debug areas to get it consistent. */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700599 /* To copy all the areas is only needed, if we have a view which */
600 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Heiko Carstens496da0d2017-10-13 09:06:29 +0200602 if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700603 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200604 else
Michael Holzheu66a464d2005-06-25 14:55:33 -0700605 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Heiko Carstens496da0d2017-10-13 09:06:29 +0200607 if (!debug_info_snapshot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 rc = -ENOMEM;
609 goto out;
610 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200611 p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
612 if (!p_info) {
Michael Holzheu58ace9f2009-03-26 15:24:50 +0100613 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 rc = -ENOMEM;
615 goto out;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 p_info->offset = 0;
618 p_info->debug_info_snap = debug_info_snapshot;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200619 p_info->debug_info_org = debug_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 p_info->view = debug_info->views[i];
621 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700622 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 p_info->act_entry = DEBUG_PROLOG_ENTRY;
624 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700625 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 debug_info_get(debug_info);
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200627 nonseekable_open(inode, file);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700628out:
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200629 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return rc;
631}
632
633/*
634 * debug_close:
635 * - called for user close()
636 * - deletes private_data area of the file handle
637 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200638static int debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 file_private_info_t *p_info;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 p_info = (file_private_info_t *) file->private_data;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200643 if (p_info->debug_info_snap)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700644 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 debug_info_put(p_info->debug_info_org);
646 kfree(file->private_data);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200647 return 0; /* success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200651 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Cornelia Huck5cbbf162008-05-15 16:52:35 +0200656debug_info_t *debug_register_mode(const char *name, int pages_per_area,
Al Virof4ae40a62011-07-24 04:33:43 -0400657 int nr_areas, int buf_size, umode_t mode,
Cornelia Huck5cbbf162008-05-15 16:52:35 +0200658 uid_t uid, gid_t gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 debug_info_t *rc = NULL;
661
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200662 /* 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 Perchesbaebc702016-03-03 20:49:57 -0800665 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100666 BUG_ON(!initialized);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200667 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Heiko Carstens496da0d2017-10-13 09:06:29 +0200669 /* create new debug_info */
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200670 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200671 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto out;
673 debug_register_view(rc, &debug_level_view);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200674 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700675 debug_register_view(rc, &debug_pages_view);
676out:
Heiko Carstens496da0d2017-10-13 09:06:29 +0200677 if (!rc)
Michael Holzheuc5612c12008-12-25 13:39:43 +0100678 pr_err("Registering debug feature %s failed\n", name);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200679 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return rc;
681}
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200682EXPORT_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 Huck5cbbf162008-05-15 16:52:35 +0200689debug_info_t *debug_register(const char *name, int pages_per_area,
690 int nr_areas, int buf_size)
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200691{
692 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
693 S_IRUSR | S_IWUSR, 0, 0);
694}
Heiko Carstensd5274362012-07-24 14:27:53 +0200695EXPORT_SYMBOL(debug_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697/*
698 * debug_unregister:
699 * - give back debug area
700 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200701void debug_unregister(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 if (!id)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200704 return;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200705 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 debug_info_put(id);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200707 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
Heiko Carstensd5274362012-07-24 14:27:53 +0200709EXPORT_SYMBOL(debug_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700712 * debug_set_size:
713 * - set area size (number of pages) and number of areas
714 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200715static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700716{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200717 debug_entry_t ***new_areas;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700718 unsigned long flags;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200719 int rc = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700720
Heiko Carstens496da0d2017-10-13 09:06:29 +0200721 if (!id || (nr_areas <= 0) || (pages_per_area < 0))
Michael Holzheu66a464d2005-06-25 14:55:33 -0700722 return -EINVAL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200723 if (pages_per_area > 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700724 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200725 if (!new_areas) {
Michael Holzheuc5612c12008-12-25 13:39:43 +0100726 pr_info("Allocating memory for %i pages failed\n",
727 pages_per_area);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700728 rc = -ENOMEM;
729 goto out;
730 }
731 } else {
732 new_areas = NULL;
733 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200734 spin_lock_irqsave(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700735 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 Carstens496da0d2017-10-13 09:06:29 +0200740 memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700741 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200742 spin_unlock_irqrestore(&id->lock, flags);
743 pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700744out:
745 return rc;
746}
747
748/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * debug_set_level:
750 * - set actual debug level
751 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200752void debug_set_level(debug_info_t *id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned long flags;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200755
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 Holzheuc5612c12008-12-25 13:39:43 +0100763 pr_info("%s: level %i is out of range (%i - %i)\n",
Heiko Carstens496da0d2017-10-13 09:06:29 +0200764 id->name, new_level, 0, DEBUG_MAX_LEVEL);
765 } else {
766 id->level = new_level;
767 }
768 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
Heiko Carstensd5274362012-07-24 14:27:53 +0200770EXPORT_SYMBOL(debug_set_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/*
773 * proceed_active_entry:
774 * - set active entry to next in the ring buffer
775 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200776static inline void proceed_active_entry(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700778 if ((id->active_entries[id->active_area] += id->entry_size)
Heiko Carstens496da0d2017-10-13 09:06:29 +0200779 > (PAGE_SIZE - id->entry_size)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700780 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 Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * proceed_active_area:
789 * - set active area to next in the ring buffer
790 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200791static inline void proceed_active_area(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 id->active_area++;
794 id->active_area = id->active_area % id->nr_areas;
795}
796
797/*
798 * get_active_entry:
799 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200800static inline debug_entry_t *get_active_entry(debug_info_t *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700802 return (debug_entry_t *) (((char *) id->areas[id->active_area]
Heiko Carstens496da0d2017-10-13 09:06:29 +0200803 [id->active_pages[id->active_area]]) +
804 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/*
808 * debug_finish_entry:
809 * - set timestamp, caller address, cpu number etc.
810 */
811
Heiko Carstens496da0d2017-10-13 09:06:29 +0200812static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
813 int level, int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +0200815 active->id.stck = get_tod_clock_fast() -
816 *(unsigned long long *) &tod_clock_base[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 active->id.fields.cpuid = smp_processor_id();
818 active->caller = __builtin_return_address(0);
819 active->id.fields.exception = exception;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200820 active->id.fields.level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 proceed_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200822 if (exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 proceed_active_area(id);
824}
825
Heiko Carstens496da0d2017-10-13 09:06:29 +0200826static int debug_stoppable = 1;
827static int debug_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829#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 Carstens496da0d2017-10-13 09:06:29 +0200837static int s390dbf_procactive(struct ctl_table *table, int write,
838 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 if (!write || debug_stoppable || !debug_active)
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700841 return proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else
843 return 0;
844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static struct ctl_table s390dbf_table[] = {
847 {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200848 .procname = "debug_stoppable",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .data = &debug_stoppable,
850 .maxlen = sizeof(int),
Heiko Carstens496da0d2017-10-13 09:06:29 +0200851 .mode = S_IRUGO | S_IWUSR,
852 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 },
Heiko Carstens496da0d2017-10-13 09:06:29 +0200854 {
855 .procname = "debug_active",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 .data = &debug_active,
857 .maxlen = sizeof(int),
Heiko Carstens496da0d2017-10-13 09:06:29 +0200858 .mode = S_IRUGO | S_IWUSR,
859 .proc_handler = s390dbf_procactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700861 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
864static struct ctl_table s390dbf_dir_table[] = {
865 {
Heiko Carstens496da0d2017-10-13 09:06:29 +0200866 .procname = "s390dbf",
867 .maxlen = 0,
868 .mode = S_IRUGO | S_IXUGO,
869 .child = s390dbf_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700871 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872};
873
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100874static struct ctl_table_header *s390dbf_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Heiko Carstens496da0d2017-10-13 09:06:29 +0200876void debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 if (debug_stoppable)
879 debug_active = 0;
880}
Heiko Carstensd5274362012-07-24 14:27:53 +0200881EXPORT_SYMBOL(debug_stop_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400883void debug_set_critical(void)
884{
885 debug_critical = 1;
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * debug_event_common:
890 * - write debug entry with given size
891 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200892debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
893 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200896 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Michael Holzheu66a464d2005-06-25 14:55:33 -0700898 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return NULL;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400900 if (debug_critical) {
901 if (!spin_trylock_irqsave(&id->lock, flags))
902 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200903 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400904 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200905 }
Sebastian Ott94158e52017-10-09 17:49:38 +0200906 do {
907 active = get_active_entry(id);
Sebastian Ott94158e52017-10-09 17:49:38 +0200908 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
Sebastian Ott0dcd91a2017-10-12 13:57:26 +0200909 if (len < id->buf_size)
910 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
Sebastian Ott94158e52017-10-09 17:49:38 +0200911 debug_finish_entry(id, active, level, 0);
912 len -= id->buf_size;
913 buf += id->buf_size;
914 } while (len > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Sebastian Ott94158e52017-10-09 17:49:38 +0200916 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return active;
918}
Heiko Carstensd5274362012-07-24 14:27:53 +0200919EXPORT_SYMBOL(debug_event_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921/*
922 * debug_exception_common:
923 * - write debug entry with given size and switch to next debug area
924 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200925debug_entry_t *debug_exception_common(debug_info_t *id, int level,
926 const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200929 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Michael Holzheu66a464d2005-06-25 14:55:33 -0700931 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return NULL;
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400933 if (debug_critical) {
934 if (!spin_trylock_irqsave(&id->lock, flags))
935 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200936 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400937 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200938 }
Sebastian Ott94158e52017-10-09 17:49:38 +0200939 do {
940 active = get_active_entry(id);
Sebastian Ott94158e52017-10-09 17:49:38 +0200941 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
Sebastian Ott0dcd91a2017-10-12 13:57:26 +0200942 if (len < id->buf_size)
943 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
Sebastian Ott94158e52017-10-09 17:49:38 +0200944 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 Torvalds1da177e2005-04-16 15:20:36 -0700948
Sebastian Ott94158e52017-10-09 17:49:38 +0200949 spin_unlock_irqrestore(&id->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return active;
951}
Heiko Carstensd5274362012-07-24 14:27:53 +0200952EXPORT_SYMBOL(debug_exception_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/*
955 * counts arguments in format string for sprintf view
956 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200957static inline int debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Heiko Carstens496da0d2017-10-13 09:06:29 +0200959 int numargs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Heiko Carstens496da0d2017-10-13 09:06:29 +0200961 while (*string) {
962 if (*string++ == '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 numargs++;
964 }
Heiko Carstens496da0d2017-10-13 09:06:29 +0200965 return numargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * debug_sprintf_event:
970 */
Heiko Carstens496da0d2017-10-13 09:06:29 +0200971debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 debug_sprintf_entry_t *curr_event;
974 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200975 unsigned long flags;
976 int numargs, idx;
977 va_list ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Michael Holzheu66a464d2005-06-25 14:55:33 -0700979 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200981 numargs = debug_count_numargs(string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400983 if (debug_critical) {
984 if (!spin_trylock_irqsave(&id->lock, flags))
985 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +0200986 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400987 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 active = get_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +0200990 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 Torvalds1da177e2005-04-16 15:20:36 -0700995 va_end(ap);
996 debug_finish_entry(id, active, level, 0);
997 spin_unlock_irqrestore(&id->lock, flags);
998
999 return active;
1000}
Christian Borntraeger832a7712014-12-01 09:16:45 +01001001EXPORT_SYMBOL(__debug_sprintf_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003/*
1004 * debug_sprintf_exception:
1005 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001006debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 debug_sprintf_entry_t *curr_event;
1009 debug_entry_t *active;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001010 unsigned long flags;
1011 int numargs, idx;
1012 va_list ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Michael Holzheu66a464d2005-06-25 14:55:33 -07001014 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return NULL;
1016
Heiko Carstens496da0d2017-10-13 09:06:29 +02001017 numargs = debug_count_numargs(string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001019 if (debug_critical) {
1020 if (!spin_trylock_irqsave(&id->lock, flags))
1021 return NULL;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001022 } else {
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001023 spin_lock_irqsave(&id->lock, flags);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 active = get_active_entry(id);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001026 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 Torvalds1da177e2005-04-16 15:20:36 -07001031 va_end(ap);
1032 debug_finish_entry(id, active, level, 1);
1033 spin_unlock_irqrestore(&id->lock, flags);
1034
1035 return active;
1036}
Christian Borntraeger832a7712014-12-01 09:16:45 +01001037EXPORT_SYMBOL(__debug_sprintf_exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039/*
1040 * debug_register_view:
1041 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001042int debug_register_view(debug_info_t *id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001044 unsigned long flags;
1045 struct dentry *pde;
1046 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int rc = 0;
1048 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (!id)
1051 goto out;
Michael Holzheu9637c3f2008-04-17 07:46:18 +02001052 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 Holzheu66a464d2005-06-25 14:55:33 -07001057 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
Heiko Carstens496da0d2017-10-13 09:06:29 +02001058 id, &debug_file_ops);
1059 if (!pde) {
Michael Holzheuc5612c12008-12-25 13:39:43 +01001060 pr_err("Registering view %s/%s failed due to out of "
Heiko Carstens496da0d2017-10-13 09:06:29 +02001061 "memory\n", id->name, view->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 rc = -1;
1063 goto out;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_lock_irqsave(&id->lock, flags);
1066 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001067 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
1069 }
1070 if (i == DEBUG_MAX_VIEWS) {
Michael Holzheuc5612c12008-12-25 13:39:43 +01001071 pr_err("Registering view %s/%s would exceed the maximum "
1072 "number of views %i\n", id->name, view->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001076 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu5a334c02012-12-20 17:30:52 +01001079 if (rc)
1080 debugfs_remove(pde);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return rc;
1083}
Heiko Carstensd5274362012-07-24 14:27:53 +02001084EXPORT_SYMBOL(debug_register_view);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086/*
1087 * debug_unregister_view:
1088 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001089int debug_unregister_view(debug_info_t *id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Michael Holzheu5a334c02012-12-20 17:30:52 +01001091 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 unsigned long flags;
Michael Holzheu5a334c02012-12-20 17:30:52 +01001093 int i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
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 Carstens496da0d2017-10-13 09:06:29 +02001102 if (i == DEBUG_MAX_VIEWS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rc = -1;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001104 } else {
Michael Holzheu5a334c02012-12-20 17:30:52 +01001105 dentry = id->debugfs_entries[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 id->views[i] = NULL;
Michael Holzheu5a334c02012-12-20 17:30:52 +01001107 id->debugfs_entries[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu5a334c02012-12-20 17:30:52 +01001110 debugfs_remove(dentry);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001111out:
1112 return rc;
1113}
Heiko Carstensd5274362012-07-24 14:27:53 +02001114EXPORT_SYMBOL(debug_unregister_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001115
Heiko Carstens496da0d2017-10-13 09:06:29 +02001116static inline char *debug_get_user_string(const char __user *user_buf,
1117 size_t user_len)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001118{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001119 char *buffer;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001120
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 Carstens496da0d2017-10-13 09:06:29 +02001133 return buffer;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001134}
1135
Heiko Carstens496da0d2017-10-13 09:06:29 +02001136static inline int debug_get_uint(char *buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001137{
1138 int rc;
1139
André Goddard Rosae7d28602009-12-14 18:01:06 -08001140 buf = skip_spaces(buf);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001141 rc = simple_strtoul(buf, &buf, 10);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001142 if (*buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001143 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return rc;
1145}
1146
1147/*
1148 * functions for debug-views
1149 ***********************************
1150*/
1151
1152/*
1153 * prints out actual debug level
1154 */
1155
Heiko Carstens496da0d2017-10-13 09:06:29 +02001156static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1157 char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001159 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 Carstens496da0d2017-10-13 09:06:29 +02001166static 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 Holzheu66a464d2005-06-25 14:55:33 -07001169{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001170 int rc, new_pages;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001171 char *str;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001172
1173 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001174 user_len = 0x10000;
1175 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001176 rc = -EPIPE;
1177 goto out;
1178 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001179 str = debug_get_user_string(user_buf, user_len);
1180 if (IS_ERR(str)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001181 rc = PTR_ERR(str);
1182 goto out;
1183 }
1184 new_pages = debug_get_uint(str);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001185 if (new_pages < 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001186 rc = -EINVAL;
1187 goto free_str;
1188 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001189 rc = debug_set_size(id, id->nr_areas, new_pages);
1190 if (rc != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001191 rc = -EINVAL;
1192 goto free_str;
1193 }
1194 rc = user_len;
1195free_str:
1196 kfree(str);
1197out:
1198 *offset += user_len;
1199 return rc; /* number of input characters */
1200}
1201
1202/*
1203 * prints out actual debug level
1204 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001205static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1206 char *out_buf)
Michael Holzheu66a464d2005-06-25 14:55:33 -07001207{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int rc = 0;
1209
Heiko Carstens496da0d2017-10-13 09:06:29 +02001210 if (id->level == DEBUG_OFF_LEVEL)
1211 rc = sprintf(out_buf, "-\n");
1212 else
Michael Holzheu66a464d2005-06-25 14:55:33 -07001213 rc = sprintf(out_buf, "%i\n", id->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return rc;
1215}
1216
1217/*
1218 * reads new debug level
1219 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001220static 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 Torvalds1da177e2005-04-16 15:20:36 -07001223{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001224 int rc, new_level;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001225 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Michael Holzheu66a464d2005-06-25 14:55:33 -07001227 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001228 user_len = 0x10000;
1229 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001230 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 goto out;
1232 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001233 str = debug_get_user_string(user_buf, user_len);
1234 if (IS_ERR(str)) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001235 rc = PTR_ERR(str);
1236 goto out;
1237 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001238 if (str[0] == '-') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001240 rc = user_len;
1241 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001243 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001245 if (new_level < 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -08001246 pr_warn("%s is not a valid level for a debug feature\n", str);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001247 rc = -EINVAL;
1248 } else {
1249 debug_set_level(id, new_level);
1250 rc = user_len;
1251 }
1252free_str:
1253 kfree(str);
1254out:
1255 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return rc; /* number of input characters */
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/*
1260 * flushes debug areas
1261 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001262static void debug_flush(debug_info_t *id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001264 unsigned long flags;
1265 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Heiko Carstens496da0d2017-10-13 09:06:29 +02001267 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 Holzheu66a464d2005-06-25 14:55:33 -07001274 id->active_pages[i] = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001275 for (j = 0; j < id->pages_per_area; j++)
1276 memset(id->areas[i][j], 0, PAGE_SIZE);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001277 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001278 } else if (area >= 0 && area < id->nr_areas) {
1279 id->active_entries[area] = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001280 id->active_pages[area] = 0;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001281 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 Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
Heiko Carstens496da0d2017-10-13 09:06:29 +02001288 * view function: flushes debug areas
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001290static 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 Torvalds1da177e2005-04-16 15:20:36 -07001293{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001294 char input_buf[1];
1295 int rc = user_len;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001296
1297 if (user_len > 0x10000)
Heiko Carstens496da0d2017-10-13 09:06:29 +02001298 user_len = 0x10000;
1299 if (*offset != 0) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001300 rc = -EPIPE;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001301 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001302 }
Heiko Carstens496da0d2017-10-13 09:06:29 +02001303 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 Torvalds1da177e2005-04-16 15:20:36 -07001317
Michael Holzheuc5612c12008-12-25 13:39:43 +01001318 pr_info("Flushing debug data failed because %c is not a valid "
1319 "area\n", input_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Michael Holzheu66a464d2005-06-25 14:55:33 -07001321out:
Heiko Carstens496da0d2017-10-13 09:06:29 +02001322 *offset += user_len;
1323 return rc; /* number of input characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/*
1327 * prints debug header in raw format
1328 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001329static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
1330 int area, debug_entry_t *entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001332 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 rc = sizeof(debug_entry_t);
Heiko Carstens496da0d2017-10-13 09:06:29 +02001335 memcpy(out_buf, entry, sizeof(debug_entry_t));
1336 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339/*
1340 * prints debug data in raw format
1341 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001342static int debug_raw_format_fn(debug_info_t *id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 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 Carstens496da0d2017-10-13 09:06:29 +02001355static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1356 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 int i, rc = 0;
1359
Heiko Carstens496da0d2017-10-13 09:06:29 +02001360 for (i = 0; i < id->buf_size; i++)
1361 rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 rc += sprintf(out_buf + rc, "| ");
1363 for (i = 0; i < id->buf_size; i++) {
1364 unsigned char c = in_buf[i];
Heiko Carstens496da0d2017-10-13 09:06:29 +02001365
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001366 if (isascii(c) && isprint(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 rc += sprintf(out_buf + rc, "%c", c);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001368 else
1369 rc += sprintf(out_buf + rc, ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 rc += sprintf(out_buf + rc, "\n");
1372 return rc;
1373}
1374
1375/*
1376 * prints header for debug entry
1377 */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001378int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1379 int area, debug_entry_t *entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +02001381 unsigned long base, sec, usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned long caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 unsigned int level;
Heiko Carstens496da0d2017-10-13 09:06:29 +02001384 char *except_str;
1385 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 level = entry->id.fields.level;
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +02001388 base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
1389 sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
Martin Schwidefskyea417aa2016-11-16 09:48:41 +01001390 usec = do_div(sec, USEC_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (entry->id.fields.exception)
1393 except_str = "*";
1394 else
1395 except_str = "-";
Heiko Carstens9cb1cce2016-01-18 13:12:19 +01001396 caller = (unsigned long) entry->caller;
Christian Borntraeger860ec7c2017-11-23 19:26:20 +01001397 rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %02i %pK ",
Martin Schwidefskyea417aa2016-11-16 09:48:41 +01001398 area, sec, usec, level, except_str,
Xunlei Pang689911c2015-04-09 09:04:41 +08001399 entry->id.fields.cpuid, (void *)caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return rc;
1401}
Heiko Carstensd5274362012-07-24 14:27:53 +02001402EXPORT_SYMBOL(debug_dflt_header_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
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 Carstens496da0d2017-10-13 09:06:29 +02001411static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1412 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Heiko Carstens496da0d2017-10-13 09:06:29 +02001414 int num_longs, num_used_args = 0, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int index[DEBUG_SPRINTF_MAX_ARGS];
1416
1417 /* count of longs fit into one entry */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001418 num_longs = id->buf_size / sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Heiko Carstens496da0d2017-10-13 09:06:29 +02001420 if (num_longs < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto out; /* bufsize of entry too small */
Heiko Carstens496da0d2017-10-13 09:06:29 +02001422 if (num_longs == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* 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 Carstens496da0d2017-10-13 09:06:29 +02001430 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Heiko Carstens496da0d2017-10-13 09:06:29 +02001432 memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Heiko Carstens496da0d2017-10-13 09:06:29 +02001434 for (i = 0; i < num_used_args; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 index[i] = i;
1436
Heiko Carstens496da0d2017-10-13 09:06:29 +02001437 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 Torvalds1da177e2005-04-16 15:20:36 -07001443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return rc;
1445}
1446
1447/*
Heiko Carstensd5274362012-07-24 14:27:53 +02001448 * debug_init:
1449 * - is called exactly once to initialize the debug feature
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
Heiko Carstensd5274362012-07-24 14:27:53 +02001451static int __init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Heiko Carstensd5274362012-07-24 14:27:53 +02001453 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 Torvalds1da177e2005-04-16 15:20:36 -07001459}
Michael Holzheu66a464d2005-06-25 14:55:33 -07001460postcore_initcall(debug_init);