xref: /titanic_44/usr/src/cmd/mdb/common/modules/random/random.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/mdb_modapi.h>
30 #include <mdb/mdb_ctf.h>
31 #include <sys/random.h>
32 
33 /*
34  * rnd_stats dcmd - Print out the global rnd_stats structure, nicely formatted.
35  */
36 /*ARGSUSED*/
37 static int
38 rnd_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
39 {
40 	rnd_stats_t rnd_stats, rnd_stats_cpu;
41 	uint32_t random_max_ncpus;
42 	size_t rndmag_t_size;
43 	ulong_t rndmag_t_offset;
44 	uintptr_t rndmag;
45 	mdb_ctf_id_t rndmag_id;
46 	int i;
47 
48 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
49 		return (DCMD_USAGE);
50 
51 	if (mdb_readvar(&rnd_stats, "rnd_stats") == -1) {
52 		mdb_warn("failed to read rnd_stats structure");
53 		return (DCMD_ERR);
54 	}
55 
56 	if ((mdb_ctf_lookup_by_name("rndmag_t", &rndmag_id) != 0) ||
57 	    (mdb_ctf_offsetof(rndmag_id, "rm_stats", &rndmag_t_offset) != 0) ||
58 	    (mdb_readvar(&random_max_ncpus, "random_max_ncpus") == -1) ||
59 	    (mdb_readvar(&rndmag, "rndmag") == -1) ||
60 	    ((rndmag_t_size = mdb_ctf_type_size(rndmag_id)) == 0)) {
61 		/* Can't find per-cpu stats.  Don't add them in. */
62 		random_max_ncpus = 0;
63 	}
64 
65 	rndmag_t_offset /= 8;
66 
67 	/*
68 	 * Read and aggregate per-cpu stats if we have them.
69 	 */
70 	for (i = 0; i < random_max_ncpus; i++) {
71 		mdb_vread(&rnd_stats_cpu, sizeof (rnd_stats_cpu),
72 		    rndmag + rndmag_t_offset + i * rndmag_t_size);
73 
74 		rnd_stats.rs_rndOut += rnd_stats_cpu.rs_rndOut;
75 		rnd_stats.rs_rndcOut += rnd_stats_cpu.rs_rndcOut;
76 		rnd_stats.rs_urndOut += rnd_stats_cpu.rs_urndOut;
77 	}
78 
79 	mdb_printf("Random number device statistics:\n");
80 
81 	mdb_printf("%8llu bytes generated for /dev/random\n",
82 	    rnd_stats.rs_rndOut);
83 	mdb_printf("%8llu bytes read from /dev/random cache\n",
84 	    rnd_stats.rs_rndcOut);
85 	mdb_printf("%8llu bytes generated for /dev/urandom\n",
86 	    rnd_stats.rs_urndOut);
87 
88 	return (DCMD_OK);
89 }
90 
91 /*
92  * swrand_stats dcmd - Print out the global swrand_stats structure,
93  * nicely formatted.
94  */
95 /*ARGSUSED*/
96 static int
97 swrand_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
98 {
99 	swrand_stats_t swrand_stats;
100 
101 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
102 		return (DCMD_USAGE);
103 
104 	if (mdb_readvar(&swrand_stats, "swrand_stats") == -1) {
105 		mdb_warn("failed to read swrand_stats structure");
106 		return (DCMD_ERR);
107 	}
108 
109 	mdb_printf("Software-based Random number generator statistics:\n");
110 
111 	mdb_printf("%8u bits of entropy estimate\n", swrand_stats.ss_entEst);
112 	mdb_printf("%8llu bits of entropy added to the pool\n",
113 	    swrand_stats.ss_entIn);
114 	mdb_printf("%8llu bits of entropy extracted from the pool\n",
115 	    swrand_stats.ss_entOut);
116 	mdb_printf("%8llu bytes added to the random pool\n",
117 	    swrand_stats.ss_bytesIn);
118 	mdb_printf("%8llu bytes extracted from the random pool\n",
119 	    swrand_stats.ss_bytesOut);
120 
121 	return (DCMD_OK);
122 }
123 
124 static const mdb_dcmd_t dcmds[] = {
125 	{ "rnd_stats",
126 	    NULL,
127 	    "print random number device statistics",
128 	    rnd_get_stats },
129 	{ "swrand_stats",
130 	    NULL,
131 	    "print kernel random number provider statistics",
132 	    swrand_get_stats },
133 	{ NULL }
134 };
135 
136 static const mdb_modinfo_t modinfo = {
137 	MDB_API_VERSION, dcmds, NULL
138 };
139 
140 const mdb_modinfo_t *
141 _mdb_init(void)
142 {
143 	return (&modinfo);
144 }
145