File Index
Files and directories
Sign in
FILE

debug.c

File preview with syntax highlighting.

linux-6.19.14/arch/s390/kernel/debug.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *   S/390 debug facility
   4 *
   5 *    Copyright IBM Corp. 1999, 2020
   6 *
   7 *    Author(s): Michael Holzheu (holzheu@de.ibm.com),
   8 *		 Holger Smolinski (Holger.Smolinski@de.ibm.com)
   9 *
  10 *    Bugreports to: <Linux390@de.ibm.com>
  11 */
  12
  13#define pr_fmt(fmt) "s390dbf: " fmt
  14
  15#include <linux/stddef.h>
  16#include <linux/kernel.h>
  17#include <linux/errno.h>
  18#include <linux/slab.h>
  19#include <linux/ctype.h>
  20#include <linux/string.h>
  21#include <linux/sysctl.h>
  22#include <linux/uaccess.h>
  23#include <linux/export.h>
  24#include <linux/init.h>
  25#include <linux/fs.h>
  26#include <linux/math.h>
  27#include <linux/minmax.h>
  28#include <linux/debugfs.h>
  29
  30#include <asm/debug.h>
  31
  32#define DEBUG_PROLOG_ENTRY -1
  33
  34#define ALL_AREAS 0 /* copy all debug areas */
  35#define NO_AREAS  1 /* copy no debug areas */
  36
  37/* typedefs */
  38
  39typedef struct file_private_info {
  40	loff_t offset;			/* offset of last read in file */
  41	int    act_area;		/* number of last formatted area */
  42	int    act_page;		/* act page in given area */
  43	int    act_entry;		/* last formatted entry (offset */
  44					/* relative to beginning of last */
  45					/* formatted page) */
  46	size_t act_entry_offset;	/* up to this offset we copied */
  47					/* in last read the last formatted */
  48					/* entry to userland */
  49	char   temp_buf[2048];		/* buffer for output */
  50	debug_info_t *debug_info_org;	/* original debug information */
  51	debug_info_t *debug_info_snap;	/* snapshot of debug information */
  52	struct debug_view *view;	/* used view of debug info */
  53} file_private_info_t;
  54
  55typedef struct {
  56	char *string;
  57	/*
  58	 * This assumes that all args are converted into longs
  59	 * on L/390 this is the case for all types of parameter
  60	 * except of floats, and long long (32 bit)
  61	 *
  62	 */
  63	long args[];
  64} debug_sprintf_entry_t;
  65
  66/* internal function prototypes */
  67
  68static int debug_init(void);
  69static ssize_t debug_output(struct file *file, char __user *user_buf,
  70			    size_t user_len, loff_t *offset);
  71static ssize_t debug_input(struct file *file, const char __user *user_buf,
  72			   size_t user_len, loff_t *offset);
  73static int debug_open(struct inode *inode, struct file *file);
  74static int debug_close(struct inode *inode, struct file *file);
  75static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  76				       int nr_areas, int buf_size, umode_t mode);
  77static void debug_info_get(debug_info_t *);
  78static void debug_info_put(debug_info_t *);
  79static int debug_prolog_level_fn(debug_info_t *id,
  80				 struct debug_view *view, char *out_buf,
  81				 size_t out_buf_size);
  82static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  83				struct file *file, const char __user *user_buf,
  84				size_t user_buf_size, loff_t *offset);
  85static int debug_prolog_pages_fn(debug_info_t *id,
  86				 struct debug_view *view, char *out_buf,
  87				 size_t out_buf_size);
  88static int debug_input_pages_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_input_flush_fn(debug_info_t *id, struct debug_view *view,
  92				struct file *file, const char __user *user_buf,
  93				size_t user_buf_size, loff_t *offset);
  94static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  95				     char *out_buf, size_t out_buf_size,
  96				     const char *in_buf);
  97static void debug_areas_swap(debug_info_t *a, debug_info_t *b);
  98static void debug_events_append(debug_info_t *dest, debug_info_t *src);
  99
 100/* globals */
 101
 102struct debug_view debug_hex_ascii_view = {
 103	"hex_ascii",
 104	NULL,
 105	&debug_dflt_header_fn,
 106	&debug_hex_ascii_format_fn,
 107	NULL,
 108	NULL
 109};
 110EXPORT_SYMBOL(debug_hex_ascii_view);
 111
 112static struct debug_view debug_level_view = {
 113	"level",
 114	&debug_prolog_level_fn,
 115	NULL,
 116	NULL,
 117	&debug_input_level_fn,
 118	NULL
 119};
 120
 121static struct debug_view debug_pages_view = {
 122	"pages",
 123	&debug_prolog_pages_fn,
 124	NULL,
 125	NULL,
 126	&debug_input_pages_fn,
 127	NULL
 128};
 129
 130static struct debug_view debug_flush_view = {
 131	"flush",
 132	NULL,
 133	NULL,
 134	NULL,
 135	&debug_input_flush_fn,
 136	NULL
 137};
 138
 139struct debug_view debug_sprintf_view = {
 140	"sprintf",
 141	NULL,
 142	&debug_dflt_header_fn,
 143	&debug_sprintf_format_fn,
 144	NULL,
 145	NULL
 146};
 147EXPORT_SYMBOL(debug_sprintf_view);
 148
 149/* used by dump analysis tools to determine version of debug feature */
 150static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
 151
 152/* static globals */
 153
 154static debug_info_t *debug_area_first;
 155static debug_info_t *debug_area_last;
 156static DEFINE_MUTEX(debug_mutex);
 157
 158static int initialized;
 159static int debug_critical;
 160
 161static const struct file_operations debug_file_ops = {
 162	.owner	 = THIS_MODULE,
 163	.read	 = debug_output,
 164	.write	 = debug_input,
 165	.open	 = debug_open,
 166	.release = debug_close,
 167};
 168
 169static struct dentry *debug_debugfs_root_entry;
 170
 171/* functions */
 172
 173/*
 174 * debug_areas_alloc
 175 * - Debug areas are implemented as a threedimensonal array:
 176 *   areas[areanumber][pagenumber][pageoffset]
 177 */
 178
 179static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
 180{
 181	debug_entry_t ***areas;
 182	int i, j;
 183
 184	areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
 185	if (!areas)
 186		goto fail_malloc_areas;
 187	for (i = 0; i < nr_areas; i++) {
 188		/* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
 189		areas[i] = kmalloc_array(pages_per_area,
 190					 sizeof(debug_entry_t *),
 191					 GFP_KERNEL | __GFP_NOWARN);
 192		if (!areas[i])
 193			goto fail_malloc_areas2;
 194		for (j = 0; j < pages_per_area; j++) {
 195			areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
 196			if (!areas[i][j]) {
 197				for (j--; j >= 0 ; j--)
 198					kfree(areas[i][j]);
 199				kfree(areas[i]);
 200				goto fail_malloc_areas2;
 201			}
 202		}
 203	}
 204	return areas;
 205
 206fail_malloc_areas2:
 207	for (i--; i >= 0; i--) {
 208		for (j = 0; j < pages_per_area; j++)
 209			kfree(areas[i][j]);
 210		kfree(areas[i]);
 211	}
 212	kfree(areas);
 213fail_malloc_areas:
 214	return NULL;
 215}
 216
 217/*
 218 * debug_info_alloc
 219 * - alloc new debug-info
 220 */
 221static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
 222				      int nr_areas, int buf_size, int level,
 223				      int mode)
 224{
 225	debug_info_t *rc;
 226
 227	/* alloc everything */
 228	rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
 229	if (!rc)
 230		goto fail_malloc_rc;
 231	rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
 232	if (!rc->active_entries)
 233		goto fail_malloc_active_entries;
 234	rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
 235	if (!rc->active_pages)
 236		goto fail_malloc_active_pages;
 237	if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
 238		rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
 239		if (!rc->areas)
 240			goto fail_malloc_areas;
 241	} else {
 242		rc->areas = NULL;
 243	}
 244
 245	/* initialize members */
 246	spin_lock_init(&rc->lock);
 247	rc->pages_per_area = pages_per_area;
 248	rc->nr_areas	   = nr_areas;
 249	rc->active_area    = 0;
 250	rc->level	   = level;
 251	rc->buf_size	   = buf_size;
 252	rc->entry_size	   = sizeof(debug_entry_t) + buf_size;
 253	strscpy(rc->name, name);
 254	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
 255	memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
 256	refcount_set(&(rc->ref_count), 0);
 257
 258	return rc;
 259
 260fail_malloc_areas:
 261	kfree(rc->active_pages);
 262fail_malloc_active_pages:
 263	kfree(rc->active_entries);
 264fail_malloc_active_entries:
 265	kfree(rc);
 266fail_malloc_rc:
 267	return NULL;
 268}
 269
 270/*
 271 * debug_areas_free
 272 * - free all debug areas
 273 */
 274static void debug_areas_free(debug_info_t *db_info)
 275{
 276	int i, j;
 277
 278	if (!db_info->areas)
 279		return;
 280	for (i = 0; i < db_info->nr_areas; i++) {
 281		for (j = 0; j < db_info->pages_per_area; j++)
 282			kfree(db_info->areas[i][j]);
 283		kfree(db_info->areas[i]);
 284	}
 285	kfree(db_info->areas);
 286	db_info->areas = NULL;
 287}
 288
 289/*
 290 * debug_info_free
 291 * - free memory debug-info
 292 */
 293static void debug_info_free(debug_info_t *db_info)
 294{
 295	debug_areas_free(db_info);
 296	kfree(db_info->active_entries);
 297	kfree(db_info->active_pages);
 298	kfree(db_info);
 299}
 300
 301/*
 302 * debug_info_create
 303 * - create new debug-info
 304 */
 305
 306static debug_info_t *debug_info_create(const char *name, int pages_per_area,
 307				       int nr_areas, int buf_size, umode_t mode)
 308{
 309	debug_info_t *rc;
 310
 311	rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
 312			      DEBUG_DEFAULT_LEVEL, ALL_AREAS);
 313	if (!rc)
 314		goto out;
 315
 316	rc->mode = mode & ~S_IFMT;
 317	refcount_set(&rc->ref_count, 1);
 318out:
 319	return rc;
 320}
 321
 322/*
 323 * debug_info_copy
 324 * - copy debug-info
 325 */
 326static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
 327{
 328	unsigned long flags;
 329	debug_info_t *rc;
 330	int i, j;
 331
 332	/* get a consistent copy of the debug areas */
 333	do {
 334		rc = debug_info_alloc(in->name, in->pages_per_area,
 335			in->nr_areas, in->buf_size, in->level, mode);
 336		spin_lock_irqsave(&in->lock, flags);
 337		if (!rc)
 338			goto out;
 339		/* has something changed in the meantime ? */
 340		if ((rc->pages_per_area == in->pages_per_area) &&
 341		    (rc->nr_areas == in->nr_areas)) {
 342			break;
 343		}
 344		spin_unlock_irqrestore(&in->lock, flags);
 345		debug_info_free(rc);
 346	} while (1);
 347
 348	if (mode == NO_AREAS)
 349		goto out;
 350
 351	for (i = 0; i < in->nr_areas; i++) {
 352		for (j = 0; j < in->pages_per_area; j++)
 353			memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
 354		rc->active_pages[i] = in->active_pages[i];
 355		rc->active_entries[i] = in->active_entries[i];
 356	}
 357	rc->active_area = in->active_area;
 358out:
 359	spin_unlock_irqrestore(&in->lock, flags);
 360	return rc;
 361}
 362
 363/*
 364 * debug_info_get
 365 * - increments reference count for debug-info
 366 */
 367static void debug_info_get(debug_info_t *db_info)
 368{
 369	if (db_info)
 370		refcount_inc(&db_info->ref_count);
 371}
 372
 373/*
 374 * debug_info_put:
 375 * - decreases reference count for debug-info and frees it if necessary
 376 */
 377static void debug_info_put(debug_info_t *db_info)
 378{
 379	if (!db_info)
 380		return;
 381	if (refcount_dec_and_test(&db_info->ref_count))
 382		debug_info_free(db_info);
 383}
 384
 385/*
 386 * debug_format_entry:
 387 * - format one debug entry and return size of formatted data
 388 */
 389static int debug_format_entry(file_private_info_t *p_info)
 390{
 391	debug_info_t *id_snap	= p_info->debug_info_snap;
 392	struct debug_view *view = p_info->view;
 393	debug_entry_t *act_entry;
 394	size_t len = 0;
 395
 396	if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
 397		/* print prolog */
 398		if (view->prolog_proc) {
 399			len += view->prolog_proc(id_snap, view, p_info->temp_buf,
 400						 sizeof(p_info->temp_buf));
 401		}
 402		goto out;
 403	}
 404	if (!id_snap->areas) /* this is true, if we have a prolog only view */
 405		goto out;    /* or if 'pages_per_area' is 0 */
 406	act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
 407				       [p_info->act_page] + p_info->act_entry);
 408
 409	if (act_entry->clock == 0LL)
 410		goto out; /* empty entry */
 411	if (view->header_proc) {
 412		len += view->header_proc(id_snap, view, p_info->act_area,
 413					 act_entry, p_info->temp_buf + len,
 414					 sizeof(p_info->temp_buf) - len);
 415	}
 416	if (view->format_proc) {
 417		len += view->format_proc(id_snap, view, p_info->temp_buf + len,
 418					 sizeof(p_info->temp_buf) - len,
 419					 DEBUG_DATA(act_entry));
 420	}
 421out:
 422	return len;
 423}
 424
 425/**
 426 * debug_next_entry - Go to the next entry
 427 * @p_info:	Private info that is manipulated
 428 *
 429 * Sets the current position in @p_info to the next entry. If no further entry
 430 * exists the current position is set to one after the end the return value
 431 * indicates that no further entries exist.
 432 *
 433 * Return: True if there are more following entries, false otherwise
 434 */
 435static inline bool debug_next_entry(file_private_info_t *p_info)
 436{
 437	debug_info_t *id;
 438
 439	id = p_info->debug_info_snap;
 440	if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
 441		p_info->act_entry = 0;
 442		p_info->act_page  = 0;
 443		return true;
 444	}
 445	if (!id->areas)
 446		return false;
 447	p_info->act_entry += id->entry_size;
 448	/* switch to next page, if we reached the end of the page  */
 449	if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
 450		/* next page */
 451		p_info->act_entry = 0;
 452		p_info->act_page += 1;
 453		if ((p_info->act_page % id->pages_per_area) == 0) {
 454			/* next area */
 455			p_info->act_area++;
 456			p_info->act_page = 0;
 457		}
 458		if (p_info->act_area >= id->nr_areas)
 459			return false;
 460	}
 461	return true;
 462}
 463
 464/**
 465 * debug_to_act_entry - Go to the currently active entry
 466 * @p_info:	Private info that is manipulated
 467 *
 468 * Sets the current position in @p_info to the currently active
 469 * entry of @p_info->debug_info_snap
 470 */
 471static void debug_to_act_entry(file_private_info_t *p_info)
 472{
 473	debug_info_t *snap_id;
 474
 475	snap_id = p_info->debug_info_snap;
 476	p_info->act_area = snap_id->active_area;
 477	p_info->act_page = snap_id->active_pages[snap_id->active_area];
 478	p_info->act_entry = snap_id->active_entries[snap_id->active_area];
 479}
 480
 481/**
 482 * debug_prev_entry - Go to the previous entry
 483 * @p_info:	Private info that is manipulated
 484 *
 485 * Sets the current position in @p_info to the previous entry. If no previous entry
 486 * exists the current position is set left as DEBUG_PROLOG_ENTRY and the return value
 487 * indicates that no previous entries exist.
 488 *
 489 * Return: True if there are more previous entries, false otherwise
 490 */
 491
 492static inline bool debug_prev_entry(file_private_info_t *p_info)
 493{
 494	debug_info_t *id;
 495
 496	id = p_info->debug_info_snap;
 497	if (p_info->act_entry == DEBUG_PROLOG_ENTRY)
 498		debug_to_act_entry(p_info);
 499	if (!id->areas)
 500		return false;
 501	p_info->act_entry -= id->entry_size;
 502	/* switch to prev page, if we reached the beginning of the page  */
 503	if (p_info->act_entry < 0) {
 504		/* end of previous page */
 505		p_info->act_entry = rounddown(PAGE_SIZE, id->entry_size) - id->entry_size;
 506		p_info->act_page--;
 507		if (p_info->act_page < 0) {
 508			/* previous area */
 509			p_info->act_area--;
 510			p_info->act_page = id->pages_per_area - 1;
 511		}
 512		if (p_info->act_area < 0)
 513			p_info->act_area = (id->nr_areas - 1) % id->nr_areas;
 514	}
 515	/* check full circle */
 516	if (id->active_area == p_info->act_area &&
 517	    id->active_pages[id->active_area] == p_info->act_page &&
 518	    id->active_entries[id->active_area] == p_info->act_entry)
 519		return false;
 520	return true;
 521}
 522
 523/**
 524 * debug_move_entry - Go to next entry in either the forward or backward direction
 525 * @p_info:	Private info that is manipulated
 526 * @reverse:	If true go to the next entry in reverse i.e. previous
 527 *
 528 * Sets the current position in @p_info to the next (@reverse == false) or
 529 * previous (@reverse == true) entry.
 530 *
 531 * Return: True if there are further entries in that direction,
 532 * false otherwise.
 533 */
 534static bool debug_move_entry(file_private_info_t *p_info, bool reverse)
 535{
 536	if (reverse)
 537		return debug_prev_entry(p_info);
 538	else
 539		return debug_next_entry(p_info);
 540}
 541
 542/*
 543 * debug_output:
 544 * - called for user read()
 545 * - copies formatted debug entries to the user buffer
 546 */
 547static ssize_t debug_output(struct file *file,		/* file descriptor */
 548			    char __user *user_buf,	/* user buffer */
 549			    size_t len,			/* length of buffer */
 550			    loff_t *offset)		/* offset in the file */
 551{
 552	size_t count = 0;
 553	size_t entry_offset;
 554	file_private_info_t *p_info;
 555
 556	p_info = (file_private_info_t *) file->private_data;
 557	if (*offset != p_info->offset)
 558		return -EPIPE;
 559	if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
 560		return 0;
 561	entry_offset = p_info->act_entry_offset;
 562	while (count < len) {
 563		int formatted_line_residue;
 564		int formatted_line_size;
 565		int user_buf_residue;
 566		size_t copy_size;
 567
 568		formatted_line_size = debug_format_entry(p_info);
 569		formatted_line_residue = formatted_line_size - entry_offset;
 570		user_buf_residue = len-count;
 571		copy_size = min(user_buf_residue, formatted_line_residue);
 572		if (copy_size) {
 573			if (copy_to_user(user_buf + count, p_info->temp_buf
 574					 + entry_offset, copy_size))
 575				return -EFAULT;
 576			count += copy_size;
 577			entry_offset += copy_size;
 578		}
 579		if (copy_size == formatted_line_residue) {
 580			entry_offset = 0;
 581			if (!debug_next_entry(p_info))
 582				goto out;
 583		}
 584	}
 585out:
 586	p_info->offset		 = *offset + count;
 587	p_info->act_entry_offset = entry_offset;
 588	*offset = p_info->offset;
 589	return count;
 590}
 591
 592/*
 593 * debug_input:
 594 * - called for user write()
 595 * - calls input function of view
 596 */
 597static ssize_t debug_input(struct file *file, const char __user *user_buf,
 598			   size_t length, loff_t *offset)
 599{
 600	file_private_info_t *p_info;
 601	int rc = 0;
 602
 603	mutex_lock(&debug_mutex);
 604	p_info = ((file_private_info_t *) file->private_data);
 605	if (p_info->view->input_proc) {
 606		rc = p_info->view->input_proc(p_info->debug_info_org,
 607					      p_info->view, file, user_buf,
 608					      length, offset);
 609	} else {
 610		rc = -EPERM;
 611	}
 612	mutex_unlock(&debug_mutex);
 613	return rc; /* number of input characters */
 614}
 615
 616static file_private_info_t *debug_file_private_alloc(debug_info_t *debug_info,
 617						     struct debug_view *view)
 618{
 619	debug_info_t *debug_info_snapshot;
 620	file_private_info_t *p_info;
 621
 622	/*
 623	 * Make snapshot of current debug areas to get it consistent.
 624	 * To copy all the areas is only needed, if we have a view which
 625	 * formats the debug areas.
 626	 */
 627	if (!view->format_proc && !view->header_proc)
 628		debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
 629	else
 630		debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
 631
 632	if (!debug_info_snapshot)
 633		return NULL;
 634	p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
 635	if (!p_info) {
 636		debug_info_free(debug_info_snapshot);
 637		return NULL;
 638	}
 639	p_info->offset = 0;
 640	p_info->debug_info_snap = debug_info_snapshot;
 641	p_info->debug_info_org	= debug_info;
 642	p_info->view = view;
 643	p_info->act_area = 0;
 644	p_info->act_page = 0;
 645	p_info->act_entry = DEBUG_PROLOG_ENTRY;
 646	p_info->act_entry_offset = 0;
 647	debug_info_get(debug_info);
 648
 649	return p_info;
 650}
 651
 652/*
 653 * debug_open:
 654 * - called for user open()
 655 * - copies formatted output to private_data area of the file
 656 *   handle
 657 */
 658static int debug_open(struct inode *inode, struct file *file)
 659{
 660	debug_info_t *debug_info;
 661	file_private_info_t *p_info;
 662	int i, rc = 0;
 663
 664	mutex_lock(&debug_mutex);
 665	debug_info = file_inode(file)->i_private;
 666	/* find debug view */
 667	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
 668		if (!debug_info->views[i])
 669			continue;
 670		else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
 671			goto found; /* found view ! */
 672	}
 673	/* no entry found */
 674	rc = -EINVAL;
 675	goto out;
 676
 677found:
 678	p_info = debug_file_private_alloc(debug_info, debug_info->views[i]);
 679	if (!p_info) {
 680		rc = -ENOMEM;
 681		goto out;
 682	}
 683	file->private_data = p_info;
 684	nonseekable_open(inode, file);
 685out:
 686	mutex_unlock(&debug_mutex);
 687	return rc;
 688}
 689
 690static void debug_file_private_free(file_private_info_t *p_info)
 691{
 692	if (p_info->debug_info_snap)
 693		debug_info_free(p_info->debug_info_snap);
 694	debug_info_put(p_info->debug_info_org);
 695	kfree(p_info);
 696}
 697
 698/*
 699 * debug_close:
 700 * - called for user close()
 701 * - deletes  private_data area of the file handle
 702 */
 703static int debug_close(struct inode *inode, struct file *file)
 704{
 705	file_private_info_t *p_info;
 706
 707	p_info = (file_private_info_t *) file->private_data;
 708	debug_file_private_free(p_info);
 709	file->private_data = NULL;
 710	return 0; /* success */
 711}
 712
 713/**
 714 * debug_dump - Get a textual representation of debug info, or as much as fits
 715 * @id:		Debug information to use
 716 * @view:	View with which to dump the debug information
 717 * @buf:	Buffer the textual debug data representation is written to
 718 * @buf_size:	Size of the buffer, including the trailing '\0' byte
 719 * @reverse:	Go backwards from the last written entry
 720 *
 721 * This function may be used whenever a textual representation of the debug
 722 * information is required without using an s390dbf file.
 723 *
 724 * Note: It is the callers responsibility to supply a view that is compatible
 725 * with the debug information data.
 726 *
 727 * Return: On success returns the number of bytes written to the buffer not
 728 * including the trailing '\0' byte. If bug_size == 0 the function returns 0.
 729 * On failure an error code less than 0 is returned.
 730 */
 731ssize_t debug_dump(debug_info_t *id, struct debug_view *view,
 732		   char *buf, size_t buf_size, bool reverse)
 733{
 734	file_private_info_t *p_info;
 735	size_t size, offset = 0;
 736
 737	/* Need space for '\0' byte */
 738	if (buf_size < 1)
 739		return 0;
 740	buf_size--;
 741
 742	p_info = debug_file_private_alloc(id, view);
 743	if (!p_info)
 744		return -ENOMEM;
 745
 746	/* There is always at least the DEBUG_PROLOG_ENTRY */
 747	do {
 748		size = debug_format_entry(p_info);
 749		size = min(size, buf_size - offset);
 750		memcpy(buf + offset, p_info->temp_buf, size);
 751		offset += size;
 752		if (offset >= buf_size)
 753			break;
 754	} while (debug_move_entry(p_info, reverse));
 755	debug_file_private_free(p_info);
 756	buf[offset] = '\0';
 757
 758	return offset;
 759}
 760
 761/* Create debugfs entries and add to internal list. */
 762static void _debug_register(debug_info_t *id)
 763{
 764	/* create root directory */
 765	id->debugfs_root_entry = debugfs_create_dir(id->name,
 766						    debug_debugfs_root_entry);
 767
 768	/* append new element to linked list */
 769	if (!debug_area_first) {
 770		/* first element in list */
 771		debug_area_first = id;
 772		id->prev = NULL;
 773	} else {
 774		/* append element to end of list */
 775		debug_area_last->next = id;
 776		id->prev = debug_area_last;
 777	}
 778	debug_area_last = id;
 779	id->next = NULL;
 780
 781	debug_register_view(id, &debug_level_view);
 782	debug_register_view(id, &debug_flush_view);
 783	debug_register_view(id, &debug_pages_view);
 784}
 785
 786/**
 787 * debug_register_mode() - creates and initializes debug area.
 788 *
 789 * @name:	Name of debug log (e.g. used for debugfs entry)
 790 * @pages_per_area:	Number of pages, which will be allocated per area
 791 * @nr_areas:	Number of debug areas
 792 * @buf_size:	Size of data area in each debug entry
 793 * @mode:	File mode for debugfs files. E.g. S_IRWXUGO
 794 * @uid:	User ID for debugfs files. Currently only 0 is supported.
 795 * @gid:	Group ID for debugfs files. Currently only 0 is supported.
 796 *
 797 * Return:
 798 * - Handle for generated debug area
 799 * - %NULL if register failed
 800 *
 801 * Allocates memory for a debug log.
 802 * Must not be called within an interrupt handler.
 803 */
 804debug_info_t *debug_register_mode(const char *name, int pages_per_area,
 805				  int nr_areas, int buf_size, umode_t mode,
 806				  uid_t uid, gid_t gid)
 807{
 808	debug_info_t *rc = NULL;
 809
 810	/* Since debugfs currently does not support uid/gid other than root, */
 811	/* we do not allow gid/uid != 0 until we get support for that. */
 812	if ((uid != 0) || (gid != 0))
 813		pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
 814	BUG_ON(!initialized);
 815
 816	/* create new debug_info */
 817	rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
 818	if (rc) {
 819		mutex_lock(&debug_mutex);
 820		_debug_register(rc);
 821		mutex_unlock(&debug_mutex);
 822	} else {
 823		pr_err("Registering debug feature %s failed\n", name);
 824	}
 825	return rc;
 826}
 827EXPORT_SYMBOL(debug_register_mode);
 828
 829/**
 830 * debug_register() - creates and initializes debug area with default file mode.
 831 *
 832 * @name:	Name of debug log (e.g. used for debugfs entry)
 833 * @pages_per_area:	Number of pages, which will be allocated per area
 834 * @nr_areas:	Number of debug areas
 835 * @buf_size:	Size of data area in each debug entry
 836 *
 837 * Return:
 838 * - Handle for generated debug area
 839 * - %NULL if register failed
 840 *
 841 * Allocates memory for a debug log.
 842 * The debugfs file mode access permissions are read and write for user.
 843 * Must not be called within an interrupt handler.
 844 */
 845debug_info_t *debug_register(const char *name, int pages_per_area,
 846			     int nr_areas, int buf_size)
 847{
 848	return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
 849				   S_IRUSR | S_IWUSR, 0, 0);
 850}
 851EXPORT_SYMBOL(debug_register);
 852
 853/**
 854 * debug_register_static() - registers a static debug area
 855 *
 856 * @id: Handle for static debug area
 857 * @pages_per_area: Number of pages per area
 858 * @nr_areas: Number of debug areas
 859 *
 860 * Register debug_info_t defined using DEFINE_STATIC_DEBUG_INFO.
 861 *
 862 * Note: This function is called automatically via an initcall generated by
 863 *	 DEFINE_STATIC_DEBUG_INFO.
 864 */
 865void debug_register_static(debug_info_t *id, int pages_per_area, int nr_areas)
 866{
 867	unsigned long flags;
 868	debug_info_t *copy;
 869
 870	if (!initialized) {
 871		pr_err("Tried to register debug feature %s too early\n",
 872		       id->name);
 873		return;
 874	}
 875
 876	copy = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size,
 877				id->level, ALL_AREAS);
 878	if (!copy) {
 879		pr_err("Registering debug feature %s failed\n", id->name);
 880
 881		/* Clear pointers to prevent tracing into released initdata. */
 882		spin_lock_irqsave(&id->lock, flags);
 883		id->areas = NULL;
 884		id->active_pages = NULL;
 885		id->active_entries = NULL;
 886		spin_unlock_irqrestore(&id->lock, flags);
 887
 888		return;
 889	}
 890
 891	/* Replace static trace area with dynamic copy. */
 892	spin_lock_irqsave(&id->lock, flags);
 893	debug_events_append(copy, id);
 894	debug_areas_swap(id, copy);
 895	spin_unlock_irqrestore(&id->lock, flags);
 896
 897	/* Clear pointers to initdata and discard copy. */
 898	copy->areas = NULL;
 899	copy->active_pages = NULL;
 900	copy->active_entries = NULL;
 901	debug_info_free(copy);
 902
 903	mutex_lock(&debug_mutex);
 904	_debug_register(id);
 905	mutex_unlock(&debug_mutex);
 906}
 907
 908/* Remove debugfs entries and remove from internal list. */
 909static void _debug_unregister(debug_info_t *id)
 910{
 911	int i;
 912
 913	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
 914		if (!id->views[i])
 915			continue;
 916		debugfs_remove(id->debugfs_entries[i]);
 917	}
 918	debugfs_remove(id->debugfs_root_entry);
 919	if (id == debug_area_first)
 920		debug_area_first = id->next;
 921	if (id == debug_area_last)
 922		debug_area_last = id->prev;
 923	if (id->prev)
 924		id->prev->next = id->next;
 925	if (id->next)
 926		id->next->prev = id->prev;
 927}
 928
 929/**
 930 * debug_unregister() - give back debug area.
 931 *
 932 * @id:		handle for debug log
 933 *
 934 * Return:
 935 *    none
 936 */
 937void debug_unregister(debug_info_t *id)
 938{
 939	if (!id)
 940		return;
 941	mutex_lock(&debug_mutex);
 942	_debug_unregister(id);
 943	mutex_unlock(&debug_mutex);
 944
 945	debug_info_put(id);
 946}
 947EXPORT_SYMBOL(debug_unregister);
 948
 949/*
 950 * debug_set_size:
 951 * - set area size (number of pages) and number of areas
 952 */
 953static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
 954{
 955	debug_info_t *new_id;
 956	unsigned long flags;
 957
 958	if (!id || (nr_areas <= 0) || (pages_per_area < 0))
 959		return -EINVAL;
 960
 961	new_id = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size,
 962				  id->level, ALL_AREAS);
 963	if (!new_id) {
 964		pr_info("Allocating memory for %i pages failed\n",
 965			pages_per_area);
 966		return -ENOMEM;
 967	}
 968
 969	spin_lock_irqsave(&id->lock, flags);
 970	debug_events_append(new_id, id);
 971	debug_areas_swap(new_id, id);
 972	debug_info_free(new_id);
 973	spin_unlock_irqrestore(&id->lock, flags);
 974	pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
 975
 976	return 0;
 977}
 978
 979/**
 980 * debug_set_level() - Sets new actual debug level if new_level is valid.
 981 *
 982 * @id:		handle for debug log
 983 * @new_level:	new debug level
 984 *
 985 * Return:
 986 *    none
 987 */
 988void debug_set_level(debug_info_t *id, int new_level)
 989{
 990	unsigned long flags;
 991
 992	if (!id)
 993		return;
 994
 995	if (new_level == DEBUG_OFF_LEVEL) {
 996		pr_info("%s: switched off\n", id->name);
 997	} else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
 998		pr_info("%s: level %i is out of range (%i - %i)\n",
 999			id->name, new_level, 0, DEBUG_MAX_LEVEL);
1000		return;
1001	}
1002
1003	spin_lock_irqsave(&id->lock, flags);
1004	id->level = new_level;
1005	spin_unlock_irqrestore(&id->lock, flags);
1006}
1007EXPORT_SYMBOL(debug_set_level);
1008
1009/*
1010 * proceed_active_entry:
1011 * - set active entry to next in the ring buffer
1012 */
1013static inline void proceed_active_entry(debug_info_t *id)
1014{
1015	if ((id->active_entries[id->active_area] += id->entry_size)
1016	    > (PAGE_SIZE - id->entry_size)) {
1017		id->active_entries[id->active_area] = 0;
1018		id->active_pages[id->active_area] =
1019			(id->active_pages[id->active_area] + 1) %
1020			id->pages_per_area;
1021	}
1022}
1023
1024/*
1025 * proceed_active_area:
1026 * - set active area to next in the ring buffer
1027 */
1028static inline void proceed_active_area(debug_info_t *id)
1029{
1030	id->active_area++;
1031	id->active_area = id->active_area % id->nr_areas;
1032}
1033
1034/*
1035 * get_active_entry:
1036 */
1037static inline debug_entry_t *get_active_entry(debug_info_t *id)
1038{
1039	return (debug_entry_t *) (((char *) id->areas[id->active_area]
1040				   [id->active_pages[id->active_area]]) +
1041				  id->active_entries[id->active_area]);
1042}
1043
1044/* Swap debug areas of a and b. */
1045static void debug_areas_swap(debug_info_t *a, debug_info_t *b)
1046{
1047	swap(a->nr_areas, b->nr_areas);
1048	swap(a->pages_per_area, b->pages_per_area);
1049	swap(a->areas, b->areas);
1050	swap(a->active_area, b->active_area);
1051	swap(a->active_pages, b->active_pages);
1052	swap(a->active_entries, b->active_entries);
1053}
1054
1055/* Append all debug events in active area from source to destination log. */
1056static void debug_events_append(debug_info_t *dest, debug_info_t *src)
1057{
1058	debug_entry_t *from, *to, *last;
1059
1060	if (!src->areas || !dest->areas)
1061		return;
1062
1063	/* Loop over all entries in src, starting with oldest. */
1064	from = get_active_entry(src);
1065	last = from;
1066	do {
1067		if (from->clock != 0LL) {
1068			to = get_active_entry(dest);
1069			memset(to, 0, dest->entry_size);
1070			memcpy(to, from, min(src->entry_size,
1071					     dest->entry_size));
1072			proceed_active_entry(dest);
1073		}
1074
1075		proceed_active_entry(src);
1076		from = get_active_entry(src);
1077	} while (from != last);
1078}
1079
1080/*
1081 * debug_finish_entry:
1082 * - set timestamp, caller address, cpu number etc.
1083 */
1084
1085static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
1086				      int level, int exception)
1087{
1088	unsigned long timestamp;
1089	union tod_clock clk;
1090
1091	store_tod_clock_ext(&clk);
1092	timestamp = clk.us;
1093	timestamp -= TOD_UNIX_EPOCH >> 12;
1094	active->clock = timestamp;
1095	active->cpu = smp_processor_id();
1096	active->caller = __builtin_return_address(0);
1097	active->exception = exception;
1098	active->level = level;
1099	proceed_active_entry(id);
1100	if (exception)
1101		proceed_active_area(id);
1102}
1103
1104static int debug_stoppable = 1;
1105static int debug_active = 1;
1106
1107#define CTL_S390DBF_STOPPABLE 5678
1108#define CTL_S390DBF_ACTIVE 5679
1109
1110/*
1111 * proc handler for the running debug_active sysctl
1112 * always allow read, allow write only if debug_stoppable is set or
1113 * if debug_active is already off
1114 */
1115static int s390dbf_procactive(const struct ctl_table *table, int write,
1116			      void *buffer, size_t *lenp, loff_t *ppos)
1117{
1118	if (!write || debug_stoppable || !debug_active)
1119		return proc_dointvec(table, write, buffer, lenp, ppos);
1120	else
1121		return 0;
1122}
1123
1124static const struct ctl_table s390dbf_table[] = {
1125	{
1126		.procname	= "debug_stoppable",
1127		.data		= &debug_stoppable,
1128		.maxlen		= sizeof(int),
1129		.mode		= S_IRUGO | S_IWUSR,
1130		.proc_handler	= proc_dointvec,
1131	},
1132	{
1133		.procname	= "debug_active",
1134		.data		= &debug_active,
1135		.maxlen		= sizeof(int),
1136		.mode		= S_IRUGO | S_IWUSR,
1137		.proc_handler	= s390dbf_procactive,
1138	},
1139};
1140
1141static struct ctl_table_header *s390dbf_sysctl_header;
1142
1143/**
1144 * debug_stop_all() - stops the debug feature if stopping is allowed.
1145 *
1146 * Return:
1147 * -   none
1148 *
1149 * Currently used in case of a kernel oops.
1150 */
1151void debug_stop_all(void)
1152{
1153	if (debug_stoppable)
1154		debug_active = 0;
1155}
1156EXPORT_SYMBOL(debug_stop_all);
1157
1158/**
1159 * debug_set_critical() - event/exception functions try lock instead of spin.
1160 *
1161 * Return:
1162 * -   none
1163 *
1164 * Currently used in case of stopping all CPUs but the current one.
1165 * Once in this state, functions to write a debug entry for an
1166 * event or exception no longer spin on the debug area lock,
1167 * but only try to get it and fail if they do not get the lock.
1168 */
1169void debug_set_critical(void)
1170{
1171	debug_critical = 1;
1172}
1173
1174/*
1175 * debug_event_common:
1176 * - write debug entry with given size
1177 */
1178debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
1179				  int len)
1180{
1181	debug_entry_t *active;
1182	unsigned long flags;
1183
1184	if (!debug_active || !id->areas)
1185		return NULL;
1186	if (debug_critical) {
1187		if (!spin_trylock_irqsave(&id->lock, flags))
1188			return NULL;
1189	} else {
1190		spin_lock_irqsave(&id->lock, flags);
1191	}
1192	do {
1193		active = get_active_entry(id);
1194		memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
1195		if (len < id->buf_size)
1196			memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
1197		debug_finish_entry(id, active, level, 0);
1198		len -= id->buf_size;
1199		buf += id->buf_size;
1200	} while (len > 0);
1201
1202	spin_unlock_irqrestore(&id->lock, flags);
1203	return active;
1204}
1205EXPORT_SYMBOL(debug_event_common);
1206
1207/*
1208 * debug_exception_common:
1209 * - write debug entry with given size and switch to next debug area
1210 */
1211debug_entry_t *debug_exception_common(debug_info_t *id, int level,
1212				      const void *buf, int len)
1213{
1214	debug_entry_t *active;
1215	unsigned long flags;
1216
1217	if (!debug_active || !id->areas)
1218		return NULL;
1219	if (debug_critical) {
1220		if (!spin_trylock_irqsave(&id->lock, flags))
1221			return NULL;
1222	} else {
1223		spin_lock_irqsave(&id->lock, flags);
1224	}
1225	do {
1226		active = get_active_entry(id);
1227		memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
1228		if (len < id->buf_size)
1229			memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
1230		debug_finish_entry(id, active, level, len <= id->buf_size);
1231		len -= id->buf_size;
1232		buf += id->buf_size;
1233	} while (len > 0);
1234
1235	spin_unlock_irqrestore(&id->lock, flags);
1236	return active;
1237}
1238EXPORT_SYMBOL(debug_exception_common);
1239
1240/*
1241 * counts arguments in format string for sprintf view
1242 */
1243static inline int debug_count_numargs(char *string)
1244{
1245	int numargs = 0;
1246
1247	while (*string) {
1248		if (*string++ == '%')
1249			numargs++;
1250	}
1251	return numargs;
1252}
1253
1254/*
1255 * debug_sprintf_event:
1256 */
1257debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
1258{
1259	debug_sprintf_entry_t *curr_event;
1260	debug_entry_t *active;
1261	unsigned long flags;
1262	int numargs, idx;
1263	va_list ap;
1264
1265	if (!debug_active || !id->areas)
1266		return NULL;
1267	numargs = debug_count_numargs(string);
1268
1269	if (debug_critical) {
1270		if (!spin_trylock_irqsave(&id->lock, flags))
1271			return NULL;
1272	} else {
1273		spin_lock_irqsave(&id->lock, flags);
1274	}
1275	active = get_active_entry(id);
1276	curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
1277	va_start(ap, string);
1278	curr_event->string = string;
1279	for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1280		curr_event->args[idx] = va_arg(ap, long);
1281	va_end(ap);
1282	debug_finish_entry(id, active, level, 0);
1283	spin_unlock_irqrestore(&id->lock, flags);
1284
1285	return active;
1286}
1287EXPORT_SYMBOL(__debug_sprintf_event);
1288
1289/*
1290 * debug_sprintf_exception:
1291 */
1292debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
1293{
1294	debug_sprintf_entry_t *curr_event;
1295	debug_entry_t *active;
1296	unsigned long flags;
1297	int numargs, idx;
1298	va_list ap;
1299
1300	if (!debug_active || !id->areas)
1301		return NULL;
1302
1303	numargs = debug_count_numargs(string);
1304
1305	if (debug_critical) {
1306		if (!spin_trylock_irqsave(&id->lock, flags))
1307			return NULL;
1308	} else {
1309		spin_lock_irqsave(&id->lock, flags);
1310	}
1311	active = get_active_entry(id);
1312	curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
1313	va_start(ap, string);
1314	curr_event->string = string;
1315	for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1316		curr_event->args[idx] = va_arg(ap, long);
1317	va_end(ap);
1318	debug_finish_entry(id, active, level, 1);
1319	spin_unlock_irqrestore(&id->lock, flags);
1320
1321	return active;
1322}
1323EXPORT_SYMBOL(__debug_sprintf_exception);
1324
1325/**
1326 * debug_register_view() - registers new debug view and creates debugfs
1327 *			   dir entry
1328 *
1329 * @id:		handle for debug log
1330 * @view:	pointer to debug view struct
1331 *
1332 * Return:
1333 * -   0  : ok
1334 * -   < 0: Error
1335 */
1336int debug_register_view(debug_info_t *id, struct debug_view *view)
1337{
1338	unsigned long flags;
1339	struct dentry *pde;
1340	umode_t mode;
1341	int rc = 0;
1342	int i;
1343
1344	if (!id)
1345		goto out;
1346	mode = (id->mode | S_IFREG) & ~S_IXUGO;
1347	if (!(view->prolog_proc || view->format_proc || view->header_proc))
1348		mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1349	if (!view->input_proc)
1350		mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1351	pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1352				  id, &debug_file_ops);
1353	spin_lock_irqsave(&id->lock, flags);
1354	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1355		if (!id->views[i])
1356			break;
1357	}
1358	if (i == DEBUG_MAX_VIEWS) {
1359		rc = -1;
1360	} else {
1361		id->views[i] = view;
1362		id->debugfs_entries[i] = pde;
1363	}
1364	spin_unlock_irqrestore(&id->lock, flags);
1365	if (rc) {
1366		pr_err("Registering view %s/%s would exceed the maximum "
1367		       "number of views %i\n", id->name, view->name, i);
1368		debugfs_remove(pde);
1369	}
1370out:
1371	return rc;
1372}
1373EXPORT_SYMBOL(debug_register_view);
1374
1375/**
1376 * debug_unregister_view() - unregisters debug view and removes debugfs
1377 *			     dir entry
1378 *
1379 * @id:		handle for debug log
1380 * @view:	pointer to debug view struct
1381 *
1382 * Return:
1383 * -   0  : ok
1384 * -   < 0: Error
1385 */
1386int debug_unregister_view(debug_info_t *id, struct debug_view *view)
1387{
1388	struct dentry *dentry = NULL;
1389	unsigned long flags;
1390	int i, rc = 0;
1391
1392	if (!id)
1393		goto out;
1394	spin_lock_irqsave(&id->lock, flags);
1395	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1396		if (id->views[i] == view)
1397			break;
1398	}
1399	if (i == DEBUG_MAX_VIEWS) {
1400		rc = -1;
1401	} else {
1402		dentry = id->debugfs_entries[i];
1403		id->views[i] = NULL;
1404		id->debugfs_entries[i] = NULL;
1405	}
1406	spin_unlock_irqrestore(&id->lock, flags);
1407	debugfs_remove(dentry);
1408out:
1409	return rc;
1410}
1411EXPORT_SYMBOL(debug_unregister_view);
1412
1413static inline char *debug_get_user_string(const char __user *user_buf,
1414					  size_t user_len)
1415{
1416	char *buffer;
1417
1418	buffer = memdup_user_nul(user_buf, user_len);
1419	if (IS_ERR(buffer))
1420		return buffer;
1421	/* got the string, now strip linefeed. */
1422	if (buffer[user_len - 1] == '\n')
1423		buffer[user_len - 1] = 0;
1424	return buffer;
1425}
1426
1427static inline int debug_get_uint(char *buf)
1428{
1429	int rc;
1430
1431	buf = skip_spaces(buf);
1432	rc = simple_strtoul(buf, &buf, 10);
1433	if (*buf)
1434		rc = -EINVAL;
1435	return rc;
1436}
1437
1438/*
1439 * functions for debug-views
1440 ***********************************
1441*/
1442
1443/*
1444 * prints out actual debug level
1445 */
1446
1447static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1448				 char *out_buf, size_t out_buf_size)
1449{
1450	return scnprintf(out_buf, out_buf_size, "%i\n", id->pages_per_area);
1451}
1452
1453/*
1454 * reads new size (number of pages per debug area)
1455 */
1456
1457static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
1458				struct file *file, const char __user *user_buf,
1459				size_t user_len, loff_t *offset)
1460{
1461	int rc, new_pages;
1462	char *str;
1463
1464	if (user_len > 0x10000)
1465		user_len = 0x10000;
1466	if (*offset != 0) {
1467		rc = -EPIPE;
1468		goto out;
1469	}
1470	str = debug_get_user_string(user_buf, user_len);
1471	if (IS_ERR(str)) {
1472		rc = PTR_ERR(str);
1473		goto out;
1474	}
1475	new_pages = debug_get_uint(str);
1476	if (new_pages < 0) {
1477		rc = -EINVAL;
1478		goto free_str;
1479	}
1480	rc = debug_set_size(id, id->nr_areas, new_pages);
1481	if (rc != 0) {
1482		rc = -EINVAL;
1483		goto free_str;
1484	}
1485	rc = user_len;
1486free_str:
1487	kfree(str);
1488out:
1489	*offset += user_len;
1490	return rc;		/* number of input characters */
1491}
1492
1493/*
1494 * prints out actual debug level
1495 */
1496static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1497				 char *out_buf, size_t out_buf_size)
1498{
1499	int rc = 0;
1500
1501	if (id->level == DEBUG_OFF_LEVEL)
1502		rc = scnprintf(out_buf, out_buf_size, "-\n");
1503	else
1504		rc = scnprintf(out_buf, out_buf_size, "%i\n", id->level);
1505	return rc;
1506}
1507
1508/*
1509 * reads new debug level
1510 */
1511static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
1512				struct file *file, const char __user *user_buf,
1513				size_t user_len, loff_t *offset)
1514{
1515	int rc, new_level;
1516	char *str;
1517
1518	if (user_len > 0x10000)
1519		user_len = 0x10000;
1520	if (*offset != 0) {
1521		rc = -EPIPE;
1522		goto out;
1523	}
1524	str = debug_get_user_string(user_buf, user_len);
1525	if (IS_ERR(str)) {
1526		rc = PTR_ERR(str);
1527		goto out;
1528	}
1529	if (str[0] == '-') {
1530		debug_set_level(id, DEBUG_OFF_LEVEL);
1531		rc = user_len;
1532		goto free_str;
1533	} else {
1534		new_level = debug_get_uint(str);
1535	}
1536	if (new_level < 0) {
1537		pr_warn("%s is not a valid level for a debug feature\n", str);
1538		rc = -EINVAL;
1539	} else {
1540		debug_set_level(id, new_level);
1541		rc = user_len;
1542	}
1543free_str:
1544	kfree(str);
1545out:
1546	*offset += user_len;
1547	return rc;		/* number of input characters */
1548}
1549
1550/*
1551 * flushes debug areas
1552 */
1553static void debug_flush(debug_info_t *id, int area)
1554{
1555	unsigned long flags;
1556	int i, j;
1557
1558	if (!id || !id->areas)
1559		return;
1560	spin_lock_irqsave(&id->lock, flags);
1561	if (area == DEBUG_FLUSH_ALL) {
1562		id->active_area = 0;
1563		memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1564		for (i = 0; i < id->nr_areas; i++) {
1565			id->active_pages[i] = 0;
1566			for (j = 0; j < id->pages_per_area; j++)
1567				memset(id->areas[i][j], 0, PAGE_SIZE);
1568		}
1569	} else if (area >= 0 && area < id->nr_areas) {
1570		id->active_entries[area] = 0;
1571		id->active_pages[area] = 0;
1572		for (i = 0; i < id->pages_per_area; i++)
1573			memset(id->areas[area][i], 0, PAGE_SIZE);
1574	}
1575	spin_unlock_irqrestore(&id->lock, flags);
1576}
1577
1578/*
1579 * view function: flushes debug areas
1580 */
1581static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
1582				struct file *file, const char __user *user_buf,
1583				size_t user_len, loff_t *offset)
1584{
1585	char input_buf[1];
1586	int rc = user_len;
1587
1588	if (user_len > 0x10000)
1589		user_len = 0x10000;
1590	if (*offset != 0) {
1591		rc = -EPIPE;
1592		goto out;
1593	}
1594	if (copy_from_user(input_buf, user_buf, 1)) {
1595		rc = -EFAULT;
1596		goto out;
1597	}
1598	if (input_buf[0] == '-') {
1599		debug_flush(id, DEBUG_FLUSH_ALL);
1600		goto out;
1601	}
1602	if (isdigit(input_buf[0])) {
1603		int area = ((int) input_buf[0] - (int) '0');
1604
1605		debug_flush(id, area);
1606		goto out;
1607	}
1608
1609	pr_info("Flushing debug data failed because %c is not a valid "
1610		 "area\n", input_buf[0]);
1611
1612out:
1613	*offset += user_len;
1614	return rc;		/* number of input characters */
1615}
1616
1617/*
1618 * prints debug data in hex/ascii format
1619 */
1620static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1621				     char *out_buf, size_t out_buf_size, const char *in_buf)
1622{
1623	int i, rc = 0;
1624
1625	for (i = 0; i < id->buf_size; i++) {
1626		rc += scnprintf(out_buf + rc, out_buf_size - rc,
1627				"%02x ", ((unsigned char *)in_buf)[i]);
1628	}
1629	rc += scnprintf(out_buf + rc, out_buf_size - rc, "| ");
1630	for (i = 0; i < id->buf_size; i++) {
1631		unsigned char c = in_buf[i];
1632
1633		if (isascii(c) && isprint(c))
1634			rc += scnprintf(out_buf + rc, out_buf_size - rc, "%c", c);
1635		else
1636			rc += scnprintf(out_buf + rc, out_buf_size - rc, ".");
1637	}
1638	rc += scnprintf(out_buf + rc, out_buf_size - rc, "\n");
1639	return rc;
1640}
1641
1642/*
1643 * prints header for debug entry
1644 */
1645int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1646			 int area, debug_entry_t *entry, char *out_buf,
1647			 size_t out_buf_size)
1648{
1649	unsigned long sec, usec;
1650	unsigned long caller;
1651	unsigned int level;
1652	char *except_str;
1653	int rc = 0;
1654
1655	level = entry->level;
1656	sec = entry->clock;
1657	usec = do_div(sec, USEC_PER_SEC);
1658
1659	if (entry->exception)
1660		except_str = "*";
1661	else
1662		except_str = "-";
1663	caller = (unsigned long) entry->caller;
1664	rc += scnprintf(out_buf, out_buf_size, "%02i %011ld:%06lu %1u %1s %04u %px  ",
1665			area, sec, usec, level, except_str,
1666			entry->cpu, (void *)caller);
1667	return rc;
1668}
1669EXPORT_SYMBOL(debug_dflt_header_fn);
1670
1671/*
1672 * prints debug data sprintf-formatted:
1673 * debug_sprintf_event/exception calls must be used together with this view
1674 */
1675
1676#define DEBUG_SPRINTF_MAX_ARGS 10
1677
1678int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1679			    char *out_buf, size_t out_buf_size, const char *inbuf)
1680{
1681	debug_sprintf_entry_t *curr_event = (debug_sprintf_entry_t *)inbuf;
1682	int num_longs, num_used_args = 0, i, rc = 0;
1683	int index[DEBUG_SPRINTF_MAX_ARGS];
1684
1685	/* count of longs fit into one entry */
1686	num_longs = id->buf_size / sizeof(long);
1687
1688	if (num_longs < 1)
1689		goto out; /* bufsize of entry too small */
1690	if (num_longs == 1) {
1691		/* no args, we use only the string */
1692		rc = strscpy(out_buf, curr_event->string, out_buf_size);
1693		if (rc == -E2BIG)
1694			rc = out_buf_size;
1695		goto out;
1696	}
1697
1698	/* number of arguments used for sprintf (without the format string) */
1699	num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1700
1701	memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1702
1703	for (i = 0; i < num_used_args; i++)
1704		index[i] = i;
1705
1706	rc = scnprintf(out_buf, out_buf_size,
1707		       curr_event->string, curr_event->args[index[0]],
1708		       curr_event->args[index[1]], curr_event->args[index[2]],
1709		       curr_event->args[index[3]], curr_event->args[index[4]],
1710		       curr_event->args[index[5]], curr_event->args[index[6]],
1711		       curr_event->args[index[7]], curr_event->args[index[8]],
1712		       curr_event->args[index[9]]);
1713out:
1714	return rc;
1715}
1716EXPORT_SYMBOL(debug_sprintf_format_fn);
1717
1718/*
1719 * debug_init:
1720 * - is called exactly once to initialize the debug feature
1721 */
1722static int __init debug_init(void)
1723{
1724	s390dbf_sysctl_header = register_sysctl("s390dbf", s390dbf_table);
1725	mutex_lock(&debug_mutex);
1726	debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
1727	initialized = 1;
1728	mutex_unlock(&debug_mutex);
1729	return 0;
1730}
1731postcore_initcall(debug_init);
~/ Read-only file preview.