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 #include <sys/mdb_modapi.h>
28 #include <mdb/mdb_ctf.h>
29 #include <sys/random.h>
30 
31 /*
32  * rnd_stats dcmd - Print out the global rnd_stats structure, nicely formatted.
33  */
34 /*ARGSUSED*/
35 static int
rnd_get_stats(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)36 rnd_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
37 {
38 	rnd_stats_t rnd_stats, rnd_stats_cpu;
39 	uint32_t random_max_ncpus;
40 	size_t rndmag_t_size;
41 	ulong_t rndmag_t_offset;
42 	uintptr_t rndmag;
43 	mdb_ctf_id_t rndmag_id;
44 	int i;
45 
46 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
47 		return (DCMD_USAGE);
48 
49 	if (mdb_readvar(&rnd_stats, "rnd_stats") == -1) {
50 		mdb_warn("failed to read rnd_stats structure");
51 		return (DCMD_ERR);
52 	}
53 
54 	if ((mdb_ctf_lookup_by_name("rndmag_t", &rndmag_id) != 0) ||
55 	    (mdb_ctf_offsetof(rndmag_id, "rm_stats", &rndmag_t_offset) != 0) ||
56 	    (mdb_readvar(&random_max_ncpus, "random_max_ncpus") == -1) ||
57 	    (mdb_readvar(&rndmag, "rndmag") == -1) ||
58 	    ((rndmag_t_size = mdb_ctf_type_size(rndmag_id)) == 0)) {
59 		/* Can't find per-cpu stats.  Don't add them in. */
60 		random_max_ncpus = 0;
61 	}
62 
63 	rndmag_t_offset /= 8;
64 
65 	/*
66 	 * Read and aggregate per-cpu stats if we have them.
67 	 */
68 	for (i = 0; i < random_max_ncpus; i++) {
69 		mdb_vread(&rnd_stats_cpu, sizeof (rnd_stats_cpu),
70 		    rndmag + rndmag_t_offset + i * rndmag_t_size);
71 
72 		rnd_stats.rs_rndOut += rnd_stats_cpu.rs_rndOut;
73 		rnd_stats.rs_rndcOut += rnd_stats_cpu.rs_rndcOut;
74 		rnd_stats.rs_urndOut += rnd_stats_cpu.rs_urndOut;
75 	}
76 
77 	mdb_printf("Random number device statistics:\n");
78 
79 	mdb_printf("%8llu bytes generated for /dev/random\n",
80 	    rnd_stats.rs_rndOut);
81 	mdb_printf("%8llu bytes read from /dev/random cache\n",
82 	    rnd_stats.rs_rndcOut);
83 	mdb_printf("%8llu bytes generated for /dev/urandom\n",
84 	    rnd_stats.rs_urndOut);
85 
86 	return (DCMD_OK);
87 }
88 
89 /*
90  * swrand_stats dcmd - Print out the global swrand_stats structure,
91  * nicely formatted.
92  */
93 /*ARGSUSED*/
94 static int
swrand_get_stats(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)95 swrand_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
96 {
97 	swrand_stats_t swrand_stats;
98 
99 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
100 		return (DCMD_USAGE);
101 
102 	if (mdb_readvar(&swrand_stats, "swrand_stats") == -1) {
103 		mdb_warn("failed to read swrand_stats structure");
104 		return (DCMD_ERR);
105 	}
106 
107 	mdb_printf("Software-based Random number generator statistics:\n");
108 
109 	mdb_printf("%8u bits of entropy estimate\n", swrand_stats.ss_entEst);
110 	mdb_printf("%8llu bits of entropy added to the pool\n",
111 	    swrand_stats.ss_entIn);
112 	mdb_printf("%8llu bits of entropy extracted from the pool\n",
113 	    swrand_stats.ss_entOut);
114 	mdb_printf("%8llu bytes added to the random pool\n",
115 	    swrand_stats.ss_bytesIn);
116 	mdb_printf("%8llu bytes extracted from the random pool\n",
117 	    swrand_stats.ss_bytesOut);
118 
119 	return (DCMD_OK);
120 }
121 
122 static const mdb_dcmd_t dcmds[] = {
123 	{ "rnd_stats",
124 	    NULL,
125 	    "print random number device statistics",
126 	    rnd_get_stats },
127 	{ "swrand_stats",
128 	    NULL,
129 	    "print kernel random number provider statistics",
130 	    swrand_get_stats },
131 	{ NULL }
132 };
133 
134 static const mdb_modinfo_t modinfo = {
135 	MDB_API_VERSION, dcmds, NULL
136 };
137 
138 const mdb_modinfo_t *
_mdb_init(void)139 _mdb_init(void)
140 {
141 	return (&modinfo);
142 }
143