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