Blame view

arch/s390/kernel/debug.c 35.6 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
   *   S/390 debug facility
   *
0990d836c   Mikhail Zaslonko   s390/debug: debug...
5
   *    Copyright IBM Corp. 1999, 2020
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
6
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
   *    Author(s): Michael Holzheu (holzheu@de.ibm.com),
496da0d70   Heiko Carstens   s390/debug: adjus...
8
   *		 Holger Smolinski (Holger.Smolinski@de.ibm.com)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
   *
   *    Bugreports to: <Linux390@de.ibm.com>
   */
c5612c195   Michael Holzheu   [S390] convert s3...
12
13
  #define KMSG_COMPONENT "s390dbf"
  #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
  #include <linux/stddef.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/slab.h>
  #include <linux/ctype.h>
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
19
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include <linux/sysctl.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
21
  #include <linux/uaccess.h>
3994a52b5   Paul Gortmaker   s390: kernel: Aud...
22
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/init.h>
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
24
25
  #include <linux/fs.h>
  #include <linux/debugfs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
  
  #include <asm/debug.h>
  
  #define DEBUG_PROLOG_ENTRY -1
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
30
31
  #define ALL_AREAS 0 /* copy all debug areas */
  #define NO_AREAS  1 /* copy no debug areas */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
  /* typedefs */
  
  typedef struct file_private_info {
  	loff_t offset;			/* offset of last read in file */
496da0d70   Heiko Carstens   s390/debug: adjus...
36
37
38
39
40
41
  	int    act_area;		/* number of last formated area */
  	int    act_page;		/* act page in given area */
  	int    act_entry;		/* last formated entry (offset */
  					/* relative to beginning of last */
  					/* formated page) */
  	size_t act_entry_offset;	/* up to this offset we copied */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
  					/* in last read the last formated */
  					/* entry to userland */
  	char   temp_buf[2048];		/* buffer for output */
496da0d70   Heiko Carstens   s390/debug: adjus...
45
  	debug_info_t *debug_info_org;	/* original debug information */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
  	debug_info_t *debug_info_snap;	/* snapshot of debug information */
  	struct debug_view *view;	/* used view of debug info */
  } file_private_info_t;
496da0d70   Heiko Carstens   s390/debug: adjus...
49
  typedef struct {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  	char *string;
496da0d70   Heiko Carstens   s390/debug: adjus...
51
52
53
54
  	/*
  	 * This assumes that all args are converted into longs
  	 * on L/390 this is the case for all types of parameter
  	 * except of floats, and long long (32 bit)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
55
56
  	 *
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
  	long args[0];
  } debug_sprintf_entry_t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
  /* internal function prototyes */
  
  static int debug_init(void);
  static ssize_t debug_output(struct file *file, char __user *user_buf,
496da0d70   Heiko Carstens   s390/debug: adjus...
63
  			    size_t user_len, loff_t *offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  static ssize_t debug_input(struct file *file, const char __user *user_buf,
496da0d70   Heiko Carstens   s390/debug: adjus...
65
  			   size_t user_len, loff_t *offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
  static int debug_open(struct inode *inode, struct file *file);
  static int debug_close(struct inode *inode, struct file *file);
5cbbf16a0   Cornelia Huck   [S390] s390dbf: U...
68
  static debug_info_t *debug_info_create(const char *name, int pages_per_area,
496da0d70   Heiko Carstens   s390/debug: adjus...
69
  				       int nr_areas, int buf_size, umode_t mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
  static void debug_info_get(debug_info_t *);
  static void debug_info_put(debug_info_t *);
496da0d70   Heiko Carstens   s390/debug: adjus...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  static int debug_prolog_level_fn(debug_info_t *id,
  				 struct debug_view *view, char *out_buf);
  static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_buf_size, loff_t *offset);
  static int debug_prolog_pages_fn(debug_info_t *id,
  				 struct debug_view *view, char *out_buf);
  static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_buf_size, loff_t *offset);
  static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_buf_size, loff_t *offset);
  static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  				     char *out_buf, const char *in_buf);
496da0d70   Heiko Carstens   s390/debug: adjus...
87
88
  static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  				   char *out_buf, debug_sprintf_entry_t *curr_event);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
  
  /* globals */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
95
96
97
98
  struct debug_view debug_hex_ascii_view = {
  	"hex_ascii",
  	NULL,
  	&debug_dflt_header_fn,
  	&debug_hex_ascii_format_fn,
  	NULL,
  	NULL
  };
d52743699   Heiko Carstens   s390/debug: remov...
99
  EXPORT_SYMBOL(debug_hex_ascii_view);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100

2b67fc460   Heiko Carstens   [S390] Get rid of...
101
  static struct debug_view debug_level_view = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
106
107
108
  	"level",
  	&debug_prolog_level_fn,
  	NULL,
  	NULL,
  	&debug_input_level_fn,
  	NULL
  };
2b67fc460   Heiko Carstens   [S390] Get rid of...
109
  static struct debug_view debug_pages_view = {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
110
111
112
113
114
115
116
  	"pages",
  	&debug_prolog_pages_fn,
  	NULL,
  	NULL,
  	&debug_input_pages_fn,
  	NULL
  };
2b67fc460   Heiko Carstens   [S390] Get rid of...
117
  static struct debug_view debug_flush_view = {
496da0d70   Heiko Carstens   s390/debug: adjus...
118
119
120
121
122
123
  	"flush",
  	NULL,
  	NULL,
  	NULL,
  	&debug_input_flush_fn,
  	NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
128
129
  };
  
  struct debug_view debug_sprintf_view = {
  	"sprintf",
  	NULL,
  	&debug_dflt_header_fn,
496da0d70   Heiko Carstens   s390/debug: adjus...
130
  	(debug_format_proc_t *)&debug_sprintf_format_fn,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
  	NULL,
  	NULL
  };
d52743699   Heiko Carstens   s390/debug: remov...
134
  EXPORT_SYMBOL(debug_sprintf_view);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135

2b67fc460   Heiko Carstens   [S390] Get rid of...
136
  /* used by dump analysis tools to determine version of debug feature */
a806170e2   Heiko Carstens   [S390] Fix a lot ...
137
  static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
  
  /* static globals */
496da0d70   Heiko Carstens   s390/debug: adjus...
140
141
  static debug_info_t *debug_area_first;
  static debug_info_t *debug_area_last;
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
142
  static DEFINE_MUTEX(debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
  
  static int initialized;
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
145
  static int debug_critical;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146

5dfe4c964   Arjan van de Ven   [PATCH] mark stru...
147
  static const struct file_operations debug_file_ops = {
496da0d70   Heiko Carstens   s390/debug: adjus...
148
149
150
151
  	.owner	 = THIS_MODULE,
  	.read	 = debug_output,
  	.write	 = debug_input,
  	.open	 = debug_open,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	.release = debug_close,
6038f373a   Arnd Bergmann   llseek: automatic...
153
  	.llseek  = no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
  };
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
155
  static struct dentry *debug_debugfs_root_entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
  
  /* functions */
  
  /*
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
160
161
162
163
   * debug_areas_alloc
   * - Debug areas are implemented as a threedimensonal array:
   *   areas[areanumber][pagenumber][pageoffset]
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
164
  static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
165
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
166
167
  	debug_entry_t ***areas;
  	int i, j;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
168

6da2ec560   Kees Cook   treewide: kmalloc...
169
  	areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
170
171
172
  	if (!areas)
  		goto fail_malloc_areas;
  	for (i = 0; i < nr_areas; i++) {
827c49139   Christian Borntraeger   s390/debug: avoid...
173
  		/* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
6da2ec560   Kees Cook   treewide: kmalloc...
174
175
  		areas[i] = kmalloc_array(pages_per_area,
  					 sizeof(debug_entry_t *),
827c49139   Christian Borntraeger   s390/debug: avoid...
176
  					 GFP_KERNEL | __GFP_NOWARN);
496da0d70   Heiko Carstens   s390/debug: adjus...
177
  		if (!areas[i])
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
178
  			goto fail_malloc_areas2;
496da0d70   Heiko Carstens   s390/debug: adjus...
179
  		for (j = 0; j < pages_per_area; j++) {
fb630517f   Eric Sesterhenn   [PATCH] s390: kza...
180
  			areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
496da0d70   Heiko Carstens   s390/debug: adjus...
181
182
  			if (!areas[i][j]) {
  				for (j--; j >= 0 ; j--)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
183
  					kfree(areas[i][j]);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
184
185
  				kfree(areas[i]);
  				goto fail_malloc_areas2;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
186
187
188
189
190
191
  			}
  		}
  	}
  	return areas;
  
  fail_malloc_areas2:
496da0d70   Heiko Carstens   s390/debug: adjus...
192
193
  	for (i--; i >= 0; i--) {
  		for (j = 0; j < pages_per_area; j++)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
194
  			kfree(areas[i][j]);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
195
196
197
198
199
  		kfree(areas[i]);
  	}
  	kfree(areas);
  fail_malloc_areas:
  	return NULL;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
200
  }
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
201
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
   * debug_info_alloc
   * - alloc new debug-info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
205
206
207
  static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
  				      int nr_areas, int buf_size, int level,
  				      int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
209
  	debug_info_t *rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
  
  	/* alloc everything */
5cbded585   Robert P. J. Day   [PATCH] getting r...
212
  	rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
496da0d70   Heiko Carstens   s390/debug: adjus...
213
  	if (!rc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  		goto fail_malloc_rc;
fb630517f   Eric Sesterhenn   [PATCH] s390: kza...
215
  	rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
496da0d70   Heiko Carstens   s390/debug: adjus...
216
  	if (!rc->active_entries)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
217
  		goto fail_malloc_active_entries;
fb630517f   Eric Sesterhenn   [PATCH] s390: kza...
218
  	rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
496da0d70   Heiko Carstens   s390/debug: adjus...
219
  	if (!rc->active_pages)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
220
  		goto fail_malloc_active_pages;
496da0d70   Heiko Carstens   s390/debug: adjus...
221
  	if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
222
  		rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
496da0d70   Heiko Carstens   s390/debug: adjus...
223
  		if (!rc->areas)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
224
225
226
  			goto fail_malloc_areas;
  	} else {
  		rc->areas = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
  	}
  
  	/* initialize members */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
  	spin_lock_init(&rc->lock);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
231
  	rc->pages_per_area = pages_per_area;
496da0d70   Heiko Carstens   s390/debug: adjus...
232
  	rc->nr_areas	   = nr_areas;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
233
  	rc->active_area    = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
234
235
236
  	rc->level	   = level;
  	rc->buf_size	   = buf_size;
  	rc->entry_size	   = sizeof(debug_entry_t) + buf_size;
20cb9e79b   Jean Delvare   [S390] strlcpy is...
237
  	strlcpy(rc->name, name, sizeof(rc->name));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
496da0d70   Heiko Carstens   s390/debug: adjus...
239
  	memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
efc0c21c9   Elena Reshetova   s390: convert deb...
240
  	refcount_set(&(rc->ref_count), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
  
  	return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  fail_malloc_areas:
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
244
245
246
247
  	kfree(rc->active_pages);
  fail_malloc_active_pages:
  	kfree(rc->active_entries);
  fail_malloc_active_entries:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
249
250
251
252
253
  	kfree(rc);
  fail_malloc_rc:
  	return NULL;
  }
  
  /*
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
254
255
   * debug_areas_free
   * - free all debug areas
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
257
  static void debug_areas_free(debug_info_t *db_info)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
258
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
259
  	int i, j;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
260

496da0d70   Heiko Carstens   s390/debug: adjus...
261
  	if (!db_info->areas)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
262
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  	for (i = 0; i < db_info->nr_areas; i++) {
496da0d70   Heiko Carstens   s390/debug: adjus...
264
  		for (j = 0; j < db_info->pages_per_area; j++)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
265
  			kfree(db_info->areas[i][j]);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
266
  		kfree(db_info->areas[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
  	}
  	kfree(db_info->areas);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
269
270
271
272
273
274
275
  	db_info->areas = NULL;
  }
  
  /*
   * debug_info_free
   * - free memory debug-info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
276
277
  static void debug_info_free(debug_info_t *db_info)
  {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
278
279
280
  	debug_areas_free(db_info);
  	kfree(db_info->active_entries);
  	kfree(db_info->active_pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
  	kfree(db_info);
  }
  
  /*
   * debug_info_create
   * - create new debug-info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
288
289
  static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  				       int nr_areas, int buf_size, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
291
  	debug_info_t *rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292

496da0d70   Heiko Carstens   s390/debug: adjus...
293
294
295
  	rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  			      DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  	if (!rc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  		goto out;
9637c3f31   Michael Holzheu   [S390] Add debug_...
297
  	rc->mode = mode & ~S_IFMT;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
298
  	/* create root directory */
496da0d70   Heiko Carstens   s390/debug: adjus...
299
300
  	rc->debugfs_root_entry = debugfs_create_dir(rc->name,
  						    debug_debugfs_root_entry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
  
  	/* append new element to linked list */
496da0d70   Heiko Carstens   s390/debug: adjus...
303
304
305
306
307
308
309
310
311
312
313
  	if (!debug_area_first) {
  		/* first element in list */
  		debug_area_first = rc;
  		rc->prev = NULL;
  	} else {
  		/* append element to end of list */
  		debug_area_last->next = rc;
  		rc->prev = debug_area_last;
  	}
  	debug_area_last = rc;
  	rc->next = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314

efc0c21c9   Elena Reshetova   s390: convert deb...
315
  	refcount_set(&rc->ref_count, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
317
318
319
320
321
322
323
  out:
  	return rc;
  }
  
  /*
   * debug_info_copy
   * - copy debug-info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
324
  static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
326
327
328
  	unsigned long flags;
  	debug_info_t *rc;
  	int i, j;
942eaabd5   Michael Holzheu   [PATCH] s390: deb...
329
330
331
332
333
334
  
  	/* get a consistent copy of the debug areas */
  	do {
  		rc = debug_info_alloc(in->name, in->pages_per_area,
  			in->nr_areas, in->buf_size, in->level, mode);
  		spin_lock_irqsave(&in->lock, flags);
496da0d70   Heiko Carstens   s390/debug: adjus...
335
  		if (!rc)
942eaabd5   Michael Holzheu   [PATCH] s390: deb...
336
337
  			goto out;
  		/* has something changed in the meantime ? */
496da0d70   Heiko Carstens   s390/debug: adjus...
338
339
  		if ((rc->pages_per_area == in->pages_per_area) &&
  		    (rc->nr_areas == in->nr_areas)) {
942eaabd5   Michael Holzheu   [PATCH] s390: deb...
340
341
342
343
344
  			break;
  		}
  		spin_unlock_irqrestore(&in->lock, flags);
  		debug_info_free(rc);
  	} while (1);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
345

acfa922c5   Julia Lawall   [S390] s390: Remo...
346
  	if (mode == NO_AREAS)
496da0d70   Heiko Carstens   s390/debug: adjus...
347
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348

496da0d70   Heiko Carstens   s390/debug: adjus...
349
350
351
352
  	for (i = 0; i < in->nr_areas; i++) {
  		for (j = 0; j < in->pages_per_area; j++)
  			memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
354
355
  	spin_unlock_irqrestore(&in->lock, flags);
  	return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
  }
  
  /*
   * debug_info_get
   * - increments reference count for debug-info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
362
  static void debug_info_get(debug_info_t *db_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
  {
  	if (db_info)
efc0c21c9   Elena Reshetova   s390: convert deb...
365
  		refcount_inc(&db_info->ref_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
369
370
371
  }
  
  /*
   * debug_info_put:
   * - decreases reference count for debug-info and frees it if necessary
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
372
  static void debug_info_put(debug_info_t *db_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
376
377
  {
  	int i;
  
  	if (!db_info)
  		return;
efc0c21c9   Elena Reshetova   s390: convert deb...
378
  	if (refcount_dec_and_test(&db_info->ref_count)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
  		for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
380
  			if (!db_info->views[i])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  				continue;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
382
  			debugfs_remove(db_info->debugfs_entries[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
  		}
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
384
  		debugfs_remove(db_info->debugfs_root_entry);
496da0d70   Heiko Carstens   s390/debug: adjus...
385
  		if (db_info == debug_area_first)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
  			debug_area_first = db_info->next;
496da0d70   Heiko Carstens   s390/debug: adjus...
387
  		if (db_info == debug_area_last)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
  			debug_area_last = db_info->prev;
496da0d70   Heiko Carstens   s390/debug: adjus...
389
390
391
392
  		if (db_info->prev)
  			db_info->prev->next = db_info->next;
  		if (db_info->next)
  			db_info->next->prev = db_info->prev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
397
398
399
400
  		debug_info_free(db_info);
  	}
  }
  
  /*
   * debug_format_entry:
   * - format one debug entry and return size of formated data
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
401
  static int debug_format_entry(file_private_info_t *p_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
403
  	debug_info_t *id_snap	= p_info->debug_info_snap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
405
406
  	struct debug_view *view = p_info->view;
  	debug_entry_t *act_entry;
  	size_t len = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
407
408
  
  	if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
  		/* print prolog */
496da0d70   Heiko Carstens   s390/debug: adjus...
410
411
  		if (view->prolog_proc)
  			len += view->prolog_proc(id_snap, view, p_info->temp_buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
  		goto out;
  	}
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
414
415
  	if (!id_snap->areas) /* this is true, if we have a prolog only view */
  		goto out;    /* or if 'pages_per_area' is 0 */
496da0d70   Heiko Carstens   s390/debug: adjus...
416
417
  	act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
  				       [p_info->act_page] + p_info->act_entry);
0990d836c   Mikhail Zaslonko   s390/debug: debug...
418
  	if (act_entry->clock == 0LL)
496da0d70   Heiko Carstens   s390/debug: adjus...
419
  		goto out; /* empty entry */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
  	if (view->header_proc)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
421
  		len += view->header_proc(id_snap, view, p_info->act_area,
496da0d70   Heiko Carstens   s390/debug: adjus...
422
  					 act_entry, p_info->temp_buf + len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  	if (view->format_proc)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
424
  		len += view->format_proc(id_snap, view, p_info->temp_buf + len,
496da0d70   Heiko Carstens   s390/debug: adjus...
425
  					 DEBUG_DATA(act_entry));
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
426
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
427
  	return len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
430
431
432
433
  }
  
  /*
   * debug_next_entry:
   * - goto next entry in p_info
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
434
  static inline int debug_next_entry(file_private_info_t *p_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
  {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
436
437
438
  	debug_info_t *id;
  
  	id = p_info->debug_info_snap;
496da0d70   Heiko Carstens   s390/debug: adjus...
439
  	if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  		p_info->act_entry = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
441
  		p_info->act_page  = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
  		goto out;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
444
  	if (!id->areas)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
445
446
447
  		return 1;
  	p_info->act_entry += id->entry_size;
  	/* switch to next page, if we reached the end of the page  */
496da0d70   Heiko Carstens   s390/debug: adjus...
448
  	if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
449
  		/* next page */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
  		p_info->act_entry = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
451
  		p_info->act_page += 1;
496da0d70   Heiko Carstens   s390/debug: adjus...
452
  		if ((p_info->act_page % id->pages_per_area) == 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
453
  			/* next area */
496da0d70   Heiko Carstens   s390/debug: adjus...
454
455
  			p_info->act_area++;
  			p_info->act_page = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
456
  		}
496da0d70   Heiko Carstens   s390/debug: adjus...
457
  		if (p_info->act_area >= id->nr_areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
  			return 1;
  	}
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
461
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
465
466
467
468
  }
  
  /*
   * debug_output:
   * - called for user read()
   * - copies formated debug entries to the user buffer
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
469
470
471
472
  static ssize_t debug_output(struct file *file,		/* file descriptor */
  			    char __user *user_buf,	/* user buffer */
  			    size_t len,			/* length of buffer */
  			    loff_t *offset)		/* offset in the file */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
  {
  	size_t count = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
475
  	size_t entry_offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  	file_private_info_t *p_info;
496da0d70   Heiko Carstens   s390/debug: adjus...
477
478
  	p_info = (file_private_info_t *) file->private_data;
  	if (*offset != p_info->offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  		return -EPIPE;
496da0d70   Heiko Carstens   s390/debug: adjus...
480
  	if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
  	entry_offset = p_info->act_entry_offset;
496da0d70   Heiko Carstens   s390/debug: adjus...
483
  	while (count < len) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
484
  		int formatted_line_residue;
496da0d70   Heiko Carstens   s390/debug: adjus...
485
  		int formatted_line_size;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
486
487
488
489
490
491
492
  		int user_buf_residue;
  		size_t copy_size;
  
  		formatted_line_size = debug_format_entry(p_info);
  		formatted_line_residue = formatted_line_size - entry_offset;
  		user_buf_residue = len-count;
  		copy_size = min(user_buf_residue, formatted_line_residue);
496da0d70   Heiko Carstens   s390/debug: adjus...
493
  		if (copy_size) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
494
  			if (copy_to_user(user_buf + count, p_info->temp_buf
496da0d70   Heiko Carstens   s390/debug: adjus...
495
  					 + entry_offset, copy_size))
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
496
497
498
  				return -EFAULT;
  			count += copy_size;
  			entry_offset += copy_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
  		}
496da0d70   Heiko Carstens   s390/debug: adjus...
500
  		if (copy_size == formatted_line_residue) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
501
  			entry_offset = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
502
  			if (debug_next_entry(p_info))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
  				goto out;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
504
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
  	}
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
507
  	p_info->offset		 = *offset + count;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
508
  	p_info->act_entry_offset = entry_offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
511
512
513
514
515
516
517
  	*offset = p_info->offset;
  	return count;
  }
  
  /*
   * debug_input:
   * - called for user write()
   * - calls input function of view
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
518
519
  static ssize_t debug_input(struct file *file, const char __user *user_buf,
  			   size_t length, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
  	file_private_info_t *p_info;
496da0d70   Heiko Carstens   s390/debug: adjus...
522
  	int rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
523

e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
524
  	mutex_lock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525
  	p_info = ((file_private_info_t *) file->private_data);
496da0d70   Heiko Carstens   s390/debug: adjus...
526
  	if (p_info->view->input_proc) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
529
  		rc = p_info->view->input_proc(p_info->debug_info_org,
  					      p_info->view, file, user_buf,
  					      length, offset);
496da0d70   Heiko Carstens   s390/debug: adjus...
530
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
  		rc = -EPERM;
496da0d70   Heiko Carstens   s390/debug: adjus...
532
  	}
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
533
  	mutex_unlock(&debug_mutex);
496da0d70   Heiko Carstens   s390/debug: adjus...
534
  	return rc; /* number of input characters */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
536
537
538
539
540
541
542
  }
  
  /*
   * debug_open:
   * - called for user open()
   * - copies formated output to private_data area of the file
   *   handle
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
543
  static int debug_open(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
544
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
  	debug_info_t *debug_info, *debug_info_snapshot;
496da0d70   Heiko Carstens   s390/debug: adjus...
546
547
  	file_private_info_t *p_info;
  	int i, rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
549
  	mutex_lock(&debug_mutex);
496ad9aa8   Al Viro   new helper: file_...
550
  	debug_info = file_inode(file)->i_private;
942eaabd5   Michael Holzheu   [PATCH] s390: deb...
551
552
553
554
  	/* find debug view */
  	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  		if (!debug_info->views[i])
  			continue;
496da0d70   Heiko Carstens   s390/debug: adjus...
555
556
  		else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
  			goto found; /* found view ! */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
560
  	}
  	/* no entry found */
  	rc = -EINVAL;
  	goto out;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
561
  found:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562

496da0d70   Heiko Carstens   s390/debug: adjus...
563
  	/* Make snapshot of current debug areas to get it consistent.	  */
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
564
565
  	/* To copy all the areas is only needed, if we have a view which  */
  	/* formats the debug areas. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566

496da0d70   Heiko Carstens   s390/debug: adjus...
567
  	if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
568
  		debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
496da0d70   Heiko Carstens   s390/debug: adjus...
569
  	else
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
570
  		debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571

496da0d70   Heiko Carstens   s390/debug: adjus...
572
  	if (!debug_info_snapshot) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
574
575
  		rc = -ENOMEM;
  		goto out;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
576
577
  	p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
  	if (!p_info) {
58ace9f2a   Michael Holzheu   [S390] s390dbf: R...
578
  		debug_info_free(debug_info_snapshot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
581
  		rc = -ENOMEM;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582
583
  	p_info->offset = 0;
  	p_info->debug_info_snap = debug_info_snapshot;
496da0d70   Heiko Carstens   s390/debug: adjus...
584
  	p_info->debug_info_org	= debug_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
586
  	p_info->view = debug_info->views[i];
  	p_info->act_area = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
587
  	p_info->act_page = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
  	p_info->act_entry = DEBUG_PROLOG_ENTRY;
  	p_info->act_entry_offset = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
590
  	file->private_data = p_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591
  	debug_info_get(debug_info);
58ea91c05   Martin Schwidefsky   [S390] avoid defa...
592
  	nonseekable_open(inode, file);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
593
  out:
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
594
  	mutex_unlock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
597
598
599
600
601
602
  	return rc;
  }
  
  /*
   * debug_close:
   * - called for user close()
   * - deletes  private_data area of the file handle
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
603
  static int debug_close(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
  {
  	file_private_info_t *p_info;
496da0d70   Heiko Carstens   s390/debug: adjus...
606

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
607
  	p_info = (file_private_info_t *) file->private_data;
496da0d70   Heiko Carstens   s390/debug: adjus...
608
  	if (p_info->debug_info_snap)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
609
  		debug_info_free(p_info->debug_info_snap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
  	debug_info_put(p_info->debug_info_org);
  	kfree(file->private_data);
496da0d70   Heiko Carstens   s390/debug: adjus...
612
  	return 0; /* success */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613
  }
0328e519a   Steffen Maier   docs: s390: unify...
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  /**
   * debug_register_mode() - creates and initializes debug area.
   *
   * @name:	Name of debug log (e.g. used for debugfs entry)
   * @pages_per_area:	Number of pages, which will be allocated per area
   * @nr_areas:	Number of debug areas
   * @buf_size:	Size of data area in each debug entry
   * @mode:	File mode for debugfs files. E.g. S_IRWXUGO
   * @uid:	User ID for debugfs files. Currently only 0 is supported.
   * @gid:	Group ID for debugfs files. Currently only 0 is supported.
   *
   * Return:
   * - Handle for generated debug area
   * - %NULL if register failed
   *
   * Allocates memory for a debug log.
   * Must not be called within an interrupt handler.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
   */
5cbbf16a0   Cornelia Huck   [S390] s390dbf: U...
632
  debug_info_t *debug_register_mode(const char *name, int pages_per_area,
f4ae40a6a   Al Viro   switch debugfs to...
633
  				  int nr_areas, int buf_size, umode_t mode,
5cbbf16a0   Cornelia Huck   [S390] s390dbf: U...
634
  				  uid_t uid, gid_t gid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
  {
  	debug_info_t *rc = NULL;
9637c3f31   Michael Holzheu   [S390] Add debug_...
637
638
639
  	/* Since debugfs currently does not support uid/gid other than root, */
  	/* we do not allow gid/uid != 0 until we get support for that. */
  	if ((uid != 0) || (gid != 0))
baebc70a4   Joe Perches   s390: Use pr_warn...
640
641
  		pr_warn("Root becomes the owner of all s390dbf files in sysfs
  ");
6aa0d3a92   Stoyan Gaydarov   [S390] BUG to BUG...
642
  	BUG_ON(!initialized);
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
643
  	mutex_lock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644

496da0d70   Heiko Carstens   s390/debug: adjus...
645
  	/* create new debug_info */
9637c3f31   Michael Holzheu   [S390] Add debug_...
646
  	rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
496da0d70   Heiko Carstens   s390/debug: adjus...
647
  	if (!rc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
  		goto out;
  	debug_register_view(rc, &debug_level_view);
496da0d70   Heiko Carstens   s390/debug: adjus...
650
  	debug_register_view(rc, &debug_flush_view);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
651
652
  	debug_register_view(rc, &debug_pages_view);
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
653
  	if (!rc)
c5612c195   Michael Holzheu   [S390] convert s3...
654
655
  		pr_err("Registering debug feature %s failed
  ", name);
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
656
  	mutex_unlock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
  	return rc;
  }
9637c3f31   Michael Holzheu   [S390] Add debug_...
659
  EXPORT_SYMBOL(debug_register_mode);
0328e519a   Steffen Maier   docs: s390: unify...
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
  /**
   * debug_register() - creates and initializes debug area with default file mode.
   *
   * @name:	Name of debug log (e.g. used for debugfs entry)
   * @pages_per_area:	Number of pages, which will be allocated per area
   * @nr_areas:	Number of debug areas
   * @buf_size:	Size of data area in each debug entry
   *
   * Return:
   * - Handle for generated debug area
   * - %NULL if register failed
   *
   * Allocates memory for a debug log.
   * The debugfs file mode access permissions are read and write for user.
   * Must not be called within an interrupt handler.
9637c3f31   Michael Holzheu   [S390] Add debug_...
675
   */
5cbbf16a0   Cornelia Huck   [S390] s390dbf: U...
676
677
  debug_info_t *debug_register(const char *name, int pages_per_area,
  			     int nr_areas, int buf_size)
9637c3f31   Michael Holzheu   [S390] Add debug_...
678
679
680
681
  {
  	return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
  				   S_IRUSR | S_IWUSR, 0, 0);
  }
d52743699   Heiko Carstens   s390/debug: remov...
682
  EXPORT_SYMBOL(debug_register);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683

0328e519a   Steffen Maier   docs: s390: unify...
684
685
686
687
688
689
690
  /**
   * debug_unregister() - give back debug area.
   *
   * @id:		handle for debug log
   *
   * Return:
   *    none
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
691
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
692
  void debug_unregister(debug_info_t *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
694
  {
  	if (!id)
496da0d70   Heiko Carstens   s390/debug: adjus...
695
  		return;
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
696
  	mutex_lock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
  	debug_info_put(id);
e11f0d04c   Christoph Hellwig   [S390] arch/s390/...
698
  	mutex_unlock(&debug_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
  }
d52743699   Heiko Carstens   s390/debug: remov...
700
  EXPORT_SYMBOL(debug_unregister);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
  
  /*
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
703
704
705
   * debug_set_size:
   * - set area size (number of pages) and number of areas
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
706
  static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
707
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
708
  	debug_entry_t ***new_areas;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
709
  	unsigned long flags;
496da0d70   Heiko Carstens   s390/debug: adjus...
710
  	int rc = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
711

496da0d70   Heiko Carstens   s390/debug: adjus...
712
  	if (!id || (nr_areas <= 0) || (pages_per_area < 0))
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
713
  		return -EINVAL;
496da0d70   Heiko Carstens   s390/debug: adjus...
714
  	if (pages_per_area > 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
715
  		new_areas = debug_areas_alloc(pages_per_area, nr_areas);
496da0d70   Heiko Carstens   s390/debug: adjus...
716
  		if (!new_areas) {
c5612c195   Michael Holzheu   [S390] convert s3...
717
718
719
  			pr_info("Allocating memory for %i pages failed
  ",
  				pages_per_area);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
720
721
722
723
724
725
  			rc = -ENOMEM;
  			goto out;
  		}
  	} else {
  		new_areas = NULL;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
726
  	spin_lock_irqsave(&id->lock, flags);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
727
728
729
730
731
  	debug_areas_free(id);
  	id->areas = new_areas;
  	id->nr_areas = nr_areas;
  	id->pages_per_area = pages_per_area;
  	id->active_area = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
732
  	memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
733
  	memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
496da0d70   Heiko Carstens   s390/debug: adjus...
734
735
736
  	spin_unlock_irqrestore(&id->lock, flags);
  	pr_info("%s: set new size (%i pages)
  ", id->name, pages_per_area);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
737
738
739
  out:
  	return rc;
  }
0328e519a   Steffen Maier   docs: s390: unify...
740
741
742
743
744
745
746
747
  /**
   * debug_set_level() - Sets new actual debug level if new_level is valid.
   *
   * @id:		handle for debug log
   * @new_level:	new debug level
   *
   * Return:
   *    none
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
749
  void debug_set_level(debug_info_t *id, int new_level)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
  {
  	unsigned long flags;
496da0d70   Heiko Carstens   s390/debug: adjus...
752
753
754
755
756
757
758
759
760
  
  	if (!id)
  		return;
  	spin_lock_irqsave(&id->lock, flags);
  	if (new_level == DEBUG_OFF_LEVEL) {
  		id->level = DEBUG_OFF_LEVEL;
  		pr_info("%s: switched off
  ", id->name);
  	} else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
c5612c195   Michael Holzheu   [S390] convert s3...
761
762
  		pr_info("%s: level %i is out of range (%i - %i)
  ",
496da0d70   Heiko Carstens   s390/debug: adjus...
763
764
765
766
767
  			id->name, new_level, 0, DEBUG_MAX_LEVEL);
  	} else {
  		id->level = new_level;
  	}
  	spin_unlock_irqrestore(&id->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
  }
d52743699   Heiko Carstens   s390/debug: remov...
769
  EXPORT_SYMBOL(debug_set_level);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
771
772
773
774
  
  /*
   * proceed_active_entry:
   * - set active entry to next in the ring buffer
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
775
  static inline void proceed_active_entry(debug_info_t *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
  {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
777
  	if ((id->active_entries[id->active_area] += id->entry_size)
496da0d70   Heiko Carstens   s390/debug: adjus...
778
  	    > (PAGE_SIZE - id->entry_size)) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
779
780
781
782
783
  		id->active_entries[id->active_area] = 0;
  		id->active_pages[id->active_area] =
  			(id->active_pages[id->active_area] + 1) %
  			id->pages_per_area;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
786
787
788
789
  }
  
  /*
   * proceed_active_area:
   * - set active area to next in the ring buffer
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
790
  static inline void proceed_active_area(debug_info_t *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
793
794
795
796
797
798
  {
  	id->active_area++;
  	id->active_area = id->active_area % id->nr_areas;
  }
  
  /*
   * get_active_entry:
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
799
  static inline debug_entry_t *get_active_entry(debug_info_t *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
  {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
801
  	return (debug_entry_t *) (((char *) id->areas[id->active_area]
496da0d70   Heiko Carstens   s390/debug: adjus...
802
803
  				   [id->active_pages[id->active_area]]) +
  				  id->active_entries[id->active_area]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
806
807
808
809
  }
  
  /*
   * debug_finish_entry:
   * - set timestamp, caller address, cpu number etc.
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
810
811
  static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
  				      int level, int exception)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
  {
0990d836c   Mikhail Zaslonko   s390/debug: debug...
813
814
815
816
817
818
819
820
  	unsigned char clk[STORE_CLOCK_EXT_SIZE];
  	unsigned long timestamp;
  
  	get_tod_clock_ext(clk);
  	timestamp = *(unsigned long *) &clk[0] >> 4;
  	timestamp -= TOD_UNIX_EPOCH >> 12;
  	active->clock = timestamp;
  	active->cpu = smp_processor_id();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
  	active->caller = __builtin_return_address(0);
0990d836c   Mikhail Zaslonko   s390/debug: debug...
822
823
  	active->exception = exception;
  	active->level = level;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
  	proceed_active_entry(id);
496da0d70   Heiko Carstens   s390/debug: adjus...
825
  	if (exception)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
  		proceed_active_area(id);
  }
496da0d70   Heiko Carstens   s390/debug: adjus...
828
829
  static int debug_stoppable = 1;
  static int debug_active = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
832
833
834
835
836
837
838
  #define CTL_S390DBF_STOPPABLE 5678
  #define CTL_S390DBF_ACTIVE 5679
  
  /*
   * proc handler for the running debug_active sysctl
   * always allow read, allow write only if debug_stoppable is set or
   * if debug_active is already off
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
839
  static int s390dbf_procactive(struct ctl_table *table, int write,
32927393d   Christoph Hellwig   sysctl: pass kern...
840
  			      void *buffer, size_t *lenp, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
842
  {
  	if (!write || debug_stoppable || !debug_active)
8d65af789   Alexey Dobriyan   sysctl: remove "s...
843
  		return proc_dointvec(table, write, buffer, lenp, ppos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
845
846
  	else
  		return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
848
  static struct ctl_table s390dbf_table[] = {
  	{
496da0d70   Heiko Carstens   s390/debug: adjus...
849
  		.procname	= "debug_stoppable",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
  		.data		= &debug_stoppable,
  		.maxlen		= sizeof(int),
496da0d70   Heiko Carstens   s390/debug: adjus...
852
853
  		.mode		= S_IRUGO | S_IWUSR,
  		.proc_handler	= proc_dointvec,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
  	},
496da0d70   Heiko Carstens   s390/debug: adjus...
855
856
  	{
  		.procname	= "debug_active",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857
858
  		.data		= &debug_active,
  		.maxlen		= sizeof(int),
496da0d70   Heiko Carstens   s390/debug: adjus...
859
860
  		.mode		= S_IRUGO | S_IWUSR,
  		.proc_handler	= s390dbf_procactive,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
  	},
b05fd35d9   Eric W. Biederman   sysctl s390: Remo...
862
  	{ }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
864
865
866
  };
  
  static struct ctl_table s390dbf_dir_table[] = {
  	{
496da0d70   Heiko Carstens   s390/debug: adjus...
867
868
869
870
  		.procname	= "s390dbf",
  		.maxlen		= 0,
  		.mode		= S_IRUGO | S_IXUGO,
  		.child		= s390dbf_table,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
  	},
b05fd35d9   Eric W. Biederman   sysctl s390: Remo...
872
  	{ }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
  };
2b67fc460   Heiko Carstens   [S390] Get rid of...
874
  static struct ctl_table_header *s390dbf_sysctl_header;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875

0328e519a   Steffen Maier   docs: s390: unify...
876
877
878
879
880
881
882
883
  /**
   * debug_stop_all() - stops the debug feature if stopping is allowed.
   *
   * Return:
   * -   none
   *
   * Currently used in case of a kernel oops.
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
884
  void debug_stop_all(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
886
887
888
  {
  	if (debug_stoppable)
  		debug_active = 0;
  }
d52743699   Heiko Carstens   s390/debug: remov...
889
  EXPORT_SYMBOL(debug_stop_all);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890

0328e519a   Steffen Maier   docs: s390: unify...
891
892
893
894
895
896
897
898
899
900
901
  /**
   * debug_set_critical() - event/exception functions try lock instead of spin.
   *
   * Return:
   * -   none
   *
   * Currently used in case of stopping all CPUs but the current one.
   * Once in this state, functions to write a debug entry for an
   * event or exception no longer spin on the debug area lock,
   * but only try to get it and fail if they do not get the lock.
   */
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
902
903
904
905
  void debug_set_critical(void)
  {
  	debug_critical = 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
907
908
909
  /*
   * debug_event_common:
   * - write debug entry with given size
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
910
911
  debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
  				  int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
913
  	debug_entry_t *active;
496da0d70   Heiko Carstens   s390/debug: adjus...
914
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
916
  	if (!debug_active || !id->areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
  		return NULL;
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
918
919
920
  	if (debug_critical) {
  		if (!spin_trylock_irqsave(&id->lock, flags))
  			return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
921
  	} else {
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
922
  		spin_lock_irqsave(&id->lock, flags);
496da0d70   Heiko Carstens   s390/debug: adjus...
923
  	}
94158e544   Sebastian Ott   s390/debug: impro...
924
925
  	do {
  		active = get_active_entry(id);
94158e544   Sebastian Ott   s390/debug: impro...
926
  		memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
0dcd91a9e   Sebastian Ott   s390/debug: only ...
927
928
  		if (len < id->buf_size)
  			memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
94158e544   Sebastian Ott   s390/debug: impro...
929
930
931
932
  		debug_finish_entry(id, active, level, 0);
  		len -= id->buf_size;
  		buf += id->buf_size;
  	} while (len > 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933

94158e544   Sebastian Ott   s390/debug: impro...
934
  	spin_unlock_irqrestore(&id->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
936
  	return active;
  }
d52743699   Heiko Carstens   s390/debug: remov...
937
  EXPORT_SYMBOL(debug_event_common);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
939
940
941
942
  
  /*
   * debug_exception_common:
   * - write debug entry with given size and switch to next debug area
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
943
944
  debug_entry_t *debug_exception_common(debug_info_t *id, int level,
  				      const void *buf, int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
  	debug_entry_t *active;
496da0d70   Heiko Carstens   s390/debug: adjus...
947
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
948

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
949
  	if (!debug_active || !id->areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
950
  		return NULL;
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
951
952
953
  	if (debug_critical) {
  		if (!spin_trylock_irqsave(&id->lock, flags))
  			return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
954
  	} else {
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
955
  		spin_lock_irqsave(&id->lock, flags);
496da0d70   Heiko Carstens   s390/debug: adjus...
956
  	}
94158e544   Sebastian Ott   s390/debug: impro...
957
958
  	do {
  		active = get_active_entry(id);
94158e544   Sebastian Ott   s390/debug: impro...
959
  		memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
0dcd91a9e   Sebastian Ott   s390/debug: only ...
960
961
  		if (len < id->buf_size)
  			memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
94158e544   Sebastian Ott   s390/debug: impro...
962
963
964
965
  		debug_finish_entry(id, active, level, len <= id->buf_size);
  		len -= id->buf_size;
  		buf += id->buf_size;
  	} while (len > 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
966

94158e544   Sebastian Ott   s390/debug: impro...
967
  	spin_unlock_irqrestore(&id->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
  	return active;
  }
d52743699   Heiko Carstens   s390/debug: remov...
970
  EXPORT_SYMBOL(debug_exception_common);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
973
974
  
  /*
   * counts arguments in format string for sprintf view
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
975
  static inline int debug_count_numargs(char *string)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
976
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
977
  	int numargs = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
978

496da0d70   Heiko Carstens   s390/debug: adjus...
979
980
  	while (*string) {
  		if (*string++ == '%')
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
981
982
  			numargs++;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
983
  	return numargs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
984
985
986
987
988
  }
  
  /*
   * debug_sprintf_event:
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
989
  debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
990
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
991
992
  	debug_sprintf_entry_t *curr_event;
  	debug_entry_t *active;
496da0d70   Heiko Carstens   s390/debug: adjus...
993
994
995
  	unsigned long flags;
  	int numargs, idx;
  	va_list ap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
997
  	if (!debug_active || !id->areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  		return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
999
  	numargs = debug_count_numargs(string);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1000

3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1001
1002
1003
  	if (debug_critical) {
  		if (!spin_trylock_irqsave(&id->lock, flags))
  			return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
1004
  	} else {
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1005
  		spin_lock_irqsave(&id->lock, flags);
496da0d70   Heiko Carstens   s390/debug: adjus...
1006
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
  	active = get_active_entry(id);
496da0d70   Heiko Carstens   s390/debug: adjus...
1008
1009
1010
1011
1012
  	curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
  	va_start(ap, string);
  	curr_event->string = string;
  	for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  		curr_event->args[idx] = va_arg(ap, long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
1014
1015
1016
1017
1018
  	va_end(ap);
  	debug_finish_entry(id, active, level, 0);
  	spin_unlock_irqrestore(&id->lock, flags);
  
  	return active;
  }
832a77103   Christian Borntraeger   s390/debug: avoid...
1019
  EXPORT_SYMBOL(__debug_sprintf_event);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
1021
1022
1023
  
  /*
   * debug_sprintf_exception:
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1024
  debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1025
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1026
1027
  	debug_sprintf_entry_t *curr_event;
  	debug_entry_t *active;
496da0d70   Heiko Carstens   s390/debug: adjus...
1028
1029
1030
  	unsigned long flags;
  	int numargs, idx;
  	va_list ap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1032
  	if (!debug_active || !id->areas)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
  		return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
1034
  	numargs = debug_count_numargs(string);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035

3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1036
1037
1038
  	if (debug_critical) {
  		if (!spin_trylock_irqsave(&id->lock, flags))
  			return NULL;
496da0d70   Heiko Carstens   s390/debug: adjus...
1039
  	} else {
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1040
  		spin_lock_irqsave(&id->lock, flags);
496da0d70   Heiko Carstens   s390/debug: adjus...
1041
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042
  	active = get_active_entry(id);
496da0d70   Heiko Carstens   s390/debug: adjus...
1043
1044
1045
1046
1047
  	curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
  	va_start(ap, string);
  	curr_event->string = string;
  	for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  		curr_event->args[idx] = va_arg(ap, long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1048
1049
1050
1051
1052
1053
  	va_end(ap);
  	debug_finish_entry(id, active, level, 1);
  	spin_unlock_irqrestore(&id->lock, flags);
  
  	return active;
  }
832a77103   Christian Borntraeger   s390/debug: avoid...
1054
  EXPORT_SYMBOL(__debug_sprintf_exception);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055

0328e519a   Steffen Maier   docs: s390: unify...
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
  /**
   * debug_register_view() - registers new debug view and creates debugfs
   *			   dir entry
   *
   * @id:		handle for debug log
   * @view:	pointer to debug view struct
   *
   * Return:
   * -   0  : ok
   * -   < 0: Error
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1067
  int debug_register_view(debug_info_t *id, struct debug_view *view)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1068
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1069
  	unsigned long flags;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1070
  	struct dentry *pde;
496da0d70   Heiko Carstens   s390/debug: adjus...
1071
1072
1073
  	umode_t mode;
  	int rc = 0;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1074
1075
1076
  
  	if (!id)
  		goto out;
9637c3f31   Michael Holzheu   [S390] Add debug_...
1077
1078
1079
1080
1081
  	mode = (id->mode | S_IFREG) & ~S_IXUGO;
  	if (!(view->prolog_proc || view->format_proc || view->header_proc))
  		mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
  	if (!view->input_proc)
  		mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1082
  	pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
496da0d70   Heiko Carstens   s390/debug: adjus...
1083
  				  id, &debug_file_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1084
1085
  	spin_lock_irqsave(&id->lock, flags);
  	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1086
  		if (!id->views[i])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087
1088
1089
  			break;
  	}
  	if (i == DEBUG_MAX_VIEWS) {
c5612c195   Michael Holzheu   [S390] convert s3...
1090
1091
1092
  		pr_err("Registering view %s/%s would exceed the maximum "
  		       "number of views %i
  ", id->name, view->name, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1093
  		rc = -1;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1094
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
  		id->views[i] = view;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1096
  		id->debugfs_entries[i] = pde;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
1098
  	}
  	spin_unlock_irqrestore(&id->lock, flags);
5a334c082   Michael Holzheu   s390/debug: Fix s...
1099
1100
  	if (rc)
  		debugfs_remove(pde);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1101
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
1103
  	return rc;
  }
d52743699   Heiko Carstens   s390/debug: remov...
1104
  EXPORT_SYMBOL(debug_register_view);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1105

0328e519a   Steffen Maier   docs: s390: unify...
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  /**
   * debug_unregister_view() - unregisters debug view and removes debugfs
   *			     dir entry
   *
   * @id:		handle for debug log
   * @view:	pointer to debug view struct
   *
   * Return:
   * -   0  : ok
   * -   < 0: Error
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1116
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1117
  int debug_unregister_view(debug_info_t *id, struct debug_view *view)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1118
  {
5a334c082   Michael Holzheu   s390/debug: Fix s...
1119
  	struct dentry *dentry = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1120
  	unsigned long flags;
5a334c082   Michael Holzheu   s390/debug: Fix s...
1121
  	int i, rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1122
1123
1124
1125
1126
1127
1128
1129
  
  	if (!id)
  		goto out;
  	spin_lock_irqsave(&id->lock, flags);
  	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  		if (id->views[i] == view)
  			break;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1130
  	if (i == DEBUG_MAX_VIEWS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
  		rc = -1;
496da0d70   Heiko Carstens   s390/debug: adjus...
1132
  	} else {
5a334c082   Michael Holzheu   s390/debug: Fix s...
1133
  		dentry = id->debugfs_entries[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
  		id->views[i] = NULL;
5a334c082   Michael Holzheu   s390/debug: Fix s...
1135
  		id->debugfs_entries[i] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
1137
  	}
  	spin_unlock_irqrestore(&id->lock, flags);
5a334c082   Michael Holzheu   s390/debug: Fix s...
1138
  	debugfs_remove(dentry);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1139
1140
1141
  out:
  	return rc;
  }
d52743699   Heiko Carstens   s390/debug: remov...
1142
  EXPORT_SYMBOL(debug_unregister_view);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1143

496da0d70   Heiko Carstens   s390/debug: adjus...
1144
1145
  static inline char *debug_get_user_string(const char __user *user_buf,
  					  size_t user_len)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1146
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1147
  	char *buffer;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
  
  	buffer = kmalloc(user_len + 1, GFP_KERNEL);
  	if (!buffer)
  		return ERR_PTR(-ENOMEM);
  	if (copy_from_user(buffer, user_buf, user_len) != 0) {
  		kfree(buffer);
  		return ERR_PTR(-EFAULT);
  	}
  	/* got the string, now strip linefeed. */
  	if (buffer[user_len - 1] == '
  ')
  		buffer[user_len - 1] = 0;
  	else
  		buffer[user_len] = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
1162
  	return buffer;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1163
  }
496da0d70   Heiko Carstens   s390/debug: adjus...
1164
  static inline int debug_get_uint(char *buf)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1165
1166
  {
  	int rc;
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
1167
  	buf = skip_spaces(buf);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1168
  	rc = simple_strtoul(buf, &buf, 10);
496da0d70   Heiko Carstens   s390/debug: adjus...
1169
  	if (*buf)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1170
  		rc = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
  	return rc;
  }
  
  /*
   * functions for debug-views
   ***********************************
  */
  
  /*
   * prints out actual debug level
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1182
1183
  static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
  				 char *out_buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1184
  {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1185
1186
1187
1188
1189
1190
1191
  	return sprintf(out_buf, "%i
  ", id->pages_per_area);
  }
  
  /*
   * reads new size (number of pages per debug area)
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1192
1193
1194
  static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_len, loff_t *offset)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1195
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1196
  	int rc, new_pages;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1197
  	char *str;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1198
1199
  
  	if (user_len > 0x10000)
496da0d70   Heiko Carstens   s390/debug: adjus...
1200
1201
  		user_len = 0x10000;
  	if (*offset != 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1202
1203
1204
  		rc = -EPIPE;
  		goto out;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1205
1206
  	str = debug_get_user_string(user_buf, user_len);
  	if (IS_ERR(str)) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1207
1208
1209
1210
  		rc = PTR_ERR(str);
  		goto out;
  	}
  	new_pages = debug_get_uint(str);
496da0d70   Heiko Carstens   s390/debug: adjus...
1211
  	if (new_pages < 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1212
1213
1214
  		rc = -EINVAL;
  		goto free_str;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1215
1216
  	rc = debug_set_size(id, id->nr_areas, new_pages);
  	if (rc != 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
  		rc = -EINVAL;
  		goto free_str;
  	}
  	rc = user_len;
  free_str:
  	kfree(str);
  out:
  	*offset += user_len;
  	return rc;		/* number of input characters */
  }
  
  /*
   * prints out actual debug level
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1231
1232
  static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
  				 char *out_buf)
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1233
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
  	int rc = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
1235
1236
1237
1238
  	if (id->level == DEBUG_OFF_LEVEL)
  		rc = sprintf(out_buf, "-
  ");
  	else
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1239
1240
  		rc = sprintf(out_buf, "%i
  ", id->level);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1241
1242
1243
1244
1245
1246
  	return rc;
  }
  
  /*
   * reads new debug level
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1247
1248
1249
  static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_len, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1251
  	int rc, new_level;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1252
  	char *str;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1254
  	if (user_len > 0x10000)
496da0d70   Heiko Carstens   s390/debug: adjus...
1255
1256
  		user_len = 0x10000;
  	if (*offset != 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1257
  		rc = -EPIPE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
  		goto out;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1259
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1260
1261
  	str = debug_get_user_string(user_buf, user_len);
  	if (IS_ERR(str)) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1262
  		rc = PTR_ERR(str);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1263
1264
  		goto out;
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1265
  	if (str[0] == '-') {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
  		debug_set_level(id, DEBUG_OFF_LEVEL);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1267
1268
  		rc = user_len;
  		goto free_str;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1269
  	} else {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1270
  		new_level = debug_get_uint(str);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1271
  	}
496da0d70   Heiko Carstens   s390/debug: adjus...
1272
  	if (new_level < 0) {
baebc70a4   Joe Perches   s390: Use pr_warn...
1273
1274
  		pr_warn("%s is not a valid level for a debug feature
  ", str);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1275
1276
1277
1278
1279
1280
1281
1282
1283
  		rc = -EINVAL;
  	} else {
  		debug_set_level(id, new_level);
  		rc = user_len;
  	}
  free_str:
  	kfree(str);
  out:
  	*offset += user_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1284
1285
  	return rc;		/* number of input characters */
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1286
1287
1288
  /*
   * flushes debug areas
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1289
  static void debug_flush(debug_info_t *id, int area)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
  	unsigned long flags;
  	int i, j;
  
  	if (!id || !id->areas)
  		return;
  	spin_lock_irqsave(&id->lock, flags);
  	if (area == DEBUG_FLUSH_ALL) {
  		id->active_area = 0;
  		memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  		for (i = 0; i < id->nr_areas; i++) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1301
  			id->active_pages[i] = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
1302
1303
  			for (j = 0; j < id->pages_per_area; j++)
  				memset(id->areas[i][j], 0, PAGE_SIZE);
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1304
  		}
496da0d70   Heiko Carstens   s390/debug: adjus...
1305
1306
  	} else if (area >= 0 && area < id->nr_areas) {
  		id->active_entries[area] = 0;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1307
  		id->active_pages[area] = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
1308
1309
1310
1311
  		for (i = 0; i < id->pages_per_area; i++)
  			memset(id->areas[area][i], 0, PAGE_SIZE);
  	}
  	spin_unlock_irqrestore(&id->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1312
1313
1314
  }
  
  /*
496da0d70   Heiko Carstens   s390/debug: adjus...
1315
   * view function: flushes debug areas
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1317
1318
1319
  static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  				struct file *file, const char __user *user_buf,
  				size_t user_len, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1320
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1321
1322
  	char input_buf[1];
  	int rc = user_len;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1323
1324
  
  	if (user_len > 0x10000)
496da0d70   Heiko Carstens   s390/debug: adjus...
1325
1326
  		user_len = 0x10000;
  	if (*offset != 0) {
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1327
  		rc = -EPIPE;
496da0d70   Heiko Carstens   s390/debug: adjus...
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
  		goto out;
  	}
  	if (copy_from_user(input_buf, user_buf, 1)) {
  		rc = -EFAULT;
  		goto out;
  	}
  	if (input_buf[0] == '-') {
  		debug_flush(id, DEBUG_FLUSH_ALL);
  		goto out;
  	}
  	if (isdigit(input_buf[0])) {
  		int area = ((int) input_buf[0] - (int) '0');
  
  		debug_flush(id, area);
  		goto out;
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1343
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344

c5612c195   Michael Holzheu   [S390] convert s3...
1345
1346
1347
  	pr_info("Flushing debug data failed because %c is not a valid "
  		 "area
  ", input_buf[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1348

66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1349
  out:
496da0d70   Heiko Carstens   s390/debug: adjus...
1350
1351
  	*offset += user_len;
  	return rc;		/* number of input characters */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1352
1353
1354
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1355
1356
   * prints debug data in hex/ascii format
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1357
1358
  static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  				     char *out_buf, const char *in_buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1359
1360
  {
  	int i, rc = 0;
496da0d70   Heiko Carstens   s390/debug: adjus...
1361
1362
  	for (i = 0; i < id->buf_size; i++)
  		rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1363
1364
1365
  	rc += sprintf(out_buf + rc, "| ");
  	for (i = 0; i < id->buf_size; i++) {
  		unsigned char c = in_buf[i];
496da0d70   Heiko Carstens   s390/debug: adjus...
1366

3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1367
  		if (isascii(c) && isprint(c))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1368
  			rc += sprintf(out_buf + rc, "%c", c);
3ab121ab1   Michael Holzheu   [S390] kernel: Ad...
1369
1370
  		else
  			rc += sprintf(out_buf + rc, ".");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
1372
1373
1374
1375
1376
1377
1378
1379
  	}
  	rc += sprintf(out_buf + rc, "
  ");
  	return rc;
  }
  
  /*
   * prints header for debug entry
   */
496da0d70   Heiko Carstens   s390/debug: adjus...
1380
1381
  int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
  			 int area, debug_entry_t *entry, char *out_buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1382
  {
0990d836c   Mikhail Zaslonko   s390/debug: debug...
1383
  	unsigned long sec, usec;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1384
  	unsigned long caller;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1385
  	unsigned int level;
496da0d70   Heiko Carstens   s390/debug: adjus...
1386
1387
  	char *except_str;
  	int rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1388

0990d836c   Mikhail Zaslonko   s390/debug: debug...
1389
1390
  	level = entry->level;
  	sec = entry->clock;
ea417aa8a   Martin Schwidefsky   s390/debug: make ...
1391
  	usec = do_div(sec, USEC_PER_SEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392

0990d836c   Mikhail Zaslonko   s390/debug: debug...
1393
  	if (entry->exception)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
1395
1396
  		except_str = "*";
  	else
  		except_str = "-";
9cb1ccecb   Heiko Carstens   s390: remove all ...
1397
  	caller = (unsigned long) entry->caller;
0990d836c   Mikhail Zaslonko   s390/debug: debug...
1398
  	rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %04u %pK  ",
ea417aa8a   Martin Schwidefsky   s390/debug: make ...
1399
  		      area, sec, usec, level, except_str,
0990d836c   Mikhail Zaslonko   s390/debug: debug...
1400
  		      entry->cpu, (void *)caller);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1401
1402
  	return rc;
  }
d52743699   Heiko Carstens   s390/debug: remov...
1403
  EXPORT_SYMBOL(debug_dflt_header_fn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
1405
1406
1407
1408
1409
1410
  
  /*
   * prints debug data sprintf-formated:
   * debug_sprinf_event/exception calls must be used together with this view
   */
  
  #define DEBUG_SPRINTF_MAX_ARGS 10
496da0d70   Heiko Carstens   s390/debug: adjus...
1411
1412
  static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  				   char *out_buf, debug_sprintf_entry_t *curr_event)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1413
  {
496da0d70   Heiko Carstens   s390/debug: adjus...
1414
  	int num_longs, num_used_args = 0, i, rc = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1415
1416
1417
  	int index[DEBUG_SPRINTF_MAX_ARGS];
  
  	/* count of longs fit into one entry */
496da0d70   Heiko Carstens   s390/debug: adjus...
1418
  	num_longs = id->buf_size / sizeof(long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1419

496da0d70   Heiko Carstens   s390/debug: adjus...
1420
  	if (num_longs < 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1421
  		goto out; /* bufsize of entry too small */
496da0d70   Heiko Carstens   s390/debug: adjus...
1422
  	if (num_longs == 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1423
1424
1425
1426
1427
1428
1429
  		/* no args, we use only the string */
  		strcpy(out_buf, curr_event->string);
  		rc = strlen(curr_event->string);
  		goto out;
  	}
  
  	/* number of arguments used for sprintf (without the format string) */
496da0d70   Heiko Carstens   s390/debug: adjus...
1430
  	num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1431

496da0d70   Heiko Carstens   s390/debug: adjus...
1432
  	memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1433

496da0d70   Heiko Carstens   s390/debug: adjus...
1434
  	for (i = 0; i < num_used_args; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1435
  		index[i] = i;
496da0d70   Heiko Carstens   s390/debug: adjus...
1436
1437
1438
1439
1440
1441
  	rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  		     curr_event->args[index[1]], curr_event->args[index[2]],
  		     curr_event->args[index[3]], curr_event->args[index[4]],
  		     curr_event->args[index[5]], curr_event->args[index[6]],
  		     curr_event->args[index[7]], curr_event->args[index[8]],
  		     curr_event->args[index[9]]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1443
1444
1445
1446
  	return rc;
  }
  
  /*
d52743699   Heiko Carstens   s390/debug: remov...
1447
1448
   * debug_init:
   * - is called exactly once to initialize the debug feature
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1449
   */
d52743699   Heiko Carstens   s390/debug: remov...
1450
  static int __init debug_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
  {
d52743699   Heiko Carstens   s390/debug: remov...
1452
1453
1454
1455
1456
1457
  	s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
  	mutex_lock(&debug_mutex);
  	debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
  	initialized = 1;
  	mutex_unlock(&debug_mutex);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1458
  }
66a464dbc   Michael Holzheu   [PATCH] s390: deb...
1459
  postcore_initcall(debug_init);