10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53708Skrishna  * Common Development and Distribution License (the "License").
63708Skrishna  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9505SBhargava.Yenduri@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * mdb dcmds for selected structures from
280Sstevel@tonic-gate  * usr/src/uts/common/sys/crypto/impl.h
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <sys/mdb_modapi.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/crypto/api.h>
350Sstevel@tonic-gate #include <sys/crypto/common.h>
360Sstevel@tonic-gate #include <sys/crypto/impl.h>
370Sstevel@tonic-gate #include "crypto_cmds.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static const char *prov_states[] = {
400Sstevel@tonic-gate 	"none",
410Sstevel@tonic-gate 	"KCF_PROV_ALLOCATED",
420Sstevel@tonic-gate 	"KCF_PROV_UNVERIFIED",
434373Skrishna 	"KCF_PROV_VERIFICATION_FAILED",
440Sstevel@tonic-gate 	"KCF_PROV_READY",
450Sstevel@tonic-gate 	"KCF_PROV_BUSY",
460Sstevel@tonic-gate 	"KCF_PROV_FAILED",
470Sstevel@tonic-gate 	"KCF_PROV_DISABLED",
48*9505SBhargava.Yenduri@Sun.COM 	"KCF_PROV_UNREGISTERING",
49*9505SBhargava.Yenduri@Sun.COM 	"KCF_PROV_UNREGISTERED"
500Sstevel@tonic-gate };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*ARGSUSED*/
530Sstevel@tonic-gate int
540Sstevel@tonic-gate kcf_provider_desc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	kcf_provider_desc_t desc;
570Sstevel@tonic-gate 	kcf_provider_desc_t *ptr;
580Sstevel@tonic-gate 	char string[MAXNAMELEN + 1];
590Sstevel@tonic-gate 	int i, j;
600Sstevel@tonic-gate 	crypto_mech_info_t *mech_pointer;
61*9505SBhargava.Yenduri@Sun.COM 	kcf_prov_cpu_t stats;
62*9505SBhargava.Yenduri@Sun.COM 	uint64_t dtotal, ftotal, btotal;
63*9505SBhargava.Yenduri@Sun.COM 	int holdcnt, jobcnt;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) != DCMD_ADDRSPEC)
660Sstevel@tonic-gate 		return (DCMD_USAGE);
670Sstevel@tonic-gate 	ptr = (kcf_provider_desc_t *)addr;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #ifdef DEBUG
700Sstevel@tonic-gate 	mdb_printf("DEBUG: reading kcf_provider_desc at %p\n", ptr);
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (mdb_vread(&desc, sizeof (kcf_provider_desc_t), (uintptr_t)ptr)
740Sstevel@tonic-gate 	    == -1) {
758384SBhargava.Yenduri@Sun.COM 		mdb_warn("cannot read at address %p", (uintptr_t)ptr);
768384SBhargava.Yenduri@Sun.COM 		return (DCMD_ERR);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 	mdb_printf("%<b>kcf_provider_desc at %p%</b>\n", ptr);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	switch (desc.pd_prov_type) {
810Sstevel@tonic-gate 	case CRYPTO_HW_PROVIDER:
820Sstevel@tonic-gate 		mdb_printf("pd_prov_type:\t\tCRYPTO_HW_PROVIDER\n");
830Sstevel@tonic-gate 		break;
840Sstevel@tonic-gate 	case CRYPTO_SW_PROVIDER:
850Sstevel@tonic-gate 		mdb_printf("pd_prov_type:\t\tCRYPTO_SW_PROVIDER\n");
860Sstevel@tonic-gate 		break;
870Sstevel@tonic-gate 	case CRYPTO_LOGICAL_PROVIDER:
880Sstevel@tonic-gate 		mdb_printf("pd_prov_type:\t\tCRYPTO_LOGICAL_PROVIDER\n");
890Sstevel@tonic-gate 		break;
900Sstevel@tonic-gate 	default:
910Sstevel@tonic-gate 		mdb_printf("bad pd_prov_type:\t%d\n", desc.pd_prov_type);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	mdb_printf("pd_prov_id:\t\t%u\n", desc.pd_prov_id);
950Sstevel@tonic-gate 	if (desc.pd_description == NULL)
960Sstevel@tonic-gate 		mdb_printf("pd_description:\t\tNULL\n");
970Sstevel@tonic-gate 	else if (mdb_readstr(string, MAXNAMELEN + 1,
988384SBhargava.Yenduri@Sun.COM 	    (uintptr_t)desc.pd_description) == -1) {
990Sstevel@tonic-gate 		mdb_warn("cannot read %p", desc.pd_description);
1000Sstevel@tonic-gate 	} else
1018384SBhargava.Yenduri@Sun.COM 		mdb_printf("pd_description:\t\t%s\n", string);
1020Sstevel@tonic-gate 
103*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_sid:\t\t\t%u\n", desc.pd_sid);
104*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_taskq:\t\t%p\n", desc.pd_taskq);
105*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_nbins:\t\t%u\n", desc.pd_nbins);
106*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_percpu_bins:\t\t%p\n", desc.pd_percpu_bins);
107*9505SBhargava.Yenduri@Sun.COM 
108*9505SBhargava.Yenduri@Sun.COM 	dtotal = ftotal = btotal = 0;
109*9505SBhargava.Yenduri@Sun.COM 	holdcnt = jobcnt = 0;
110*9505SBhargava.Yenduri@Sun.COM 	for (i = 0; i < desc.pd_nbins; i++) {
111*9505SBhargava.Yenduri@Sun.COM 		if (mdb_vread(&stats, sizeof (kcf_prov_cpu_t),
112*9505SBhargava.Yenduri@Sun.COM 		    (uintptr_t)(desc.pd_percpu_bins + i)) == -1) {
113*9505SBhargava.Yenduri@Sun.COM 			mdb_warn("cannot read addr %p",
114*9505SBhargava.Yenduri@Sun.COM 			    desc.pd_percpu_bins + i);
115*9505SBhargava.Yenduri@Sun.COM 			return (DCMD_ERR);
116*9505SBhargava.Yenduri@Sun.COM 		}
117*9505SBhargava.Yenduri@Sun.COM 
118*9505SBhargava.Yenduri@Sun.COM 		holdcnt += stats.kp_holdcnt;
119*9505SBhargava.Yenduri@Sun.COM 		jobcnt += stats.kp_jobcnt;
120*9505SBhargava.Yenduri@Sun.COM 		dtotal += stats.kp_ndispatches;
121*9505SBhargava.Yenduri@Sun.COM 		ftotal += stats.kp_nfails;
122*9505SBhargava.Yenduri@Sun.COM 		btotal += stats.kp_nbusy_rval;
123*9505SBhargava.Yenduri@Sun.COM 	}
124*9505SBhargava.Yenduri@Sun.COM 	mdb_inc_indent(4);
125*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_holdcnt:\t\t%d\n", holdcnt);
126*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_jobcnt:\t\t%u\n", jobcnt);
127*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_ndispatches:\t%llu\n", dtotal);
128*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_nfails:\t\t%llu\n", ftotal);
129*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_nbusy_rval:\t%llu\n", btotal);
130*9505SBhargava.Yenduri@Sun.COM 	mdb_dec_indent(4);
131*9505SBhargava.Yenduri@Sun.COM 
132*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_prov_handle:\t\t%p\n", desc.pd_prov_handle);
133*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_kcf_prov_handle:\t%u\n", desc.pd_kcf_prov_handle);
134*9505SBhargava.Yenduri@Sun.COM 
1350Sstevel@tonic-gate 	mdb_printf("pd_ops_vector:\t\t%p\n", desc.pd_ops_vector);
1360Sstevel@tonic-gate 	mdb_printf("pd_mech_list_count:\t%u\n", desc.pd_mech_list_count);
1370Sstevel@tonic-gate 	/* mechanisms */
1380Sstevel@tonic-gate 	mdb_inc_indent(4);
1390Sstevel@tonic-gate 	for (i = 0; i < desc.pd_mech_list_count; i++) {
1400Sstevel@tonic-gate 		mech_pointer = desc.pd_mechanisms + i;
1410Sstevel@tonic-gate 		mdb_call_dcmd("crypto_mech_info",
1428384SBhargava.Yenduri@Sun.COM 		    (uintptr_t)mech_pointer, DCMD_ADDRSPEC, 0, NULL);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	mdb_dec_indent(4);
1453708Skrishna 	mdb_printf("pd_mech_indx:\n");
1460Sstevel@tonic-gate 	mdb_inc_indent(8);
1470Sstevel@tonic-gate 	for (i = 0; i < KCF_OPS_CLASSSIZE; i++) {
1488384SBhargava.Yenduri@Sun.COM 		for (j = 0; j < KCF_MAXMECHTAB; j++) {
1498384SBhargava.Yenduri@Sun.COM 			if (desc.pd_mech_indx[i][j] == KCF_INVALID_INDX)
1508384SBhargava.Yenduri@Sun.COM 				mdb_printf("N ");
1518384SBhargava.Yenduri@Sun.COM 			else
1528384SBhargava.Yenduri@Sun.COM 				mdb_printf("%u ", desc.pd_mech_indx[i][j]);
1538384SBhargava.Yenduri@Sun.COM 		}
1548384SBhargava.Yenduri@Sun.COM 		mdb_printf("\n");
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	mdb_dec_indent(8);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (desc.pd_name == NULL)
1598384SBhargava.Yenduri@Sun.COM 		mdb_printf("pd_name:\t\t NULL\n");
1600Sstevel@tonic-gate 	else if (mdb_readstr(string, MAXNAMELEN + 1, (uintptr_t)desc.pd_name)
1618384SBhargava.Yenduri@Sun.COM 	    == -1)
1620Sstevel@tonic-gate 		mdb_warn("could not read pd_name from %X\n", desc.pd_name);
1630Sstevel@tonic-gate 	else
1648384SBhargava.Yenduri@Sun.COM 		mdb_printf("pd_name:\t\t%s\n", string);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	mdb_printf("pd_instance:\t\t%u\n", desc.pd_instance);
1670Sstevel@tonic-gate 	mdb_printf("pd_module_id:\t\t%d\n", desc.pd_module_id);
1680Sstevel@tonic-gate 	mdb_printf("pd_mctlp:\t\t%p\n", desc.pd_mctlp);
1690Sstevel@tonic-gate 	mdb_printf("pd_lock:\t\t%p\n", desc.pd_lock);
1700Sstevel@tonic-gate 	if (desc.pd_state < KCF_PROV_ALLOCATED ||
171*9505SBhargava.Yenduri@Sun.COM 	    desc.pd_state > KCF_PROV_UNREGISTERED)
1720Sstevel@tonic-gate 		mdb_printf("pd_state is invalid:\t%d\n", desc.pd_state);
1730Sstevel@tonic-gate 	else
1740Sstevel@tonic-gate 		mdb_printf("pd_state:\t%s\n", prov_states[desc.pd_state]);
175*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_provider_list:\t%p\n", desc.pd_provider_list);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	mdb_printf("pd_resume_cv:\t\t%hd\n", desc.pd_resume_cv._opaque);
1788384SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_flags:\t\t%s %s %s %s %s\n",
1793708Skrishna 	    (desc.pd_flags & CRYPTO_HIDE_PROVIDER) ?
1808384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_HIDE_PROVIDER" : " ",
1814072Skrishna 	    (desc.pd_flags & CRYPTO_HASH_NO_UPDATE) ?
1828384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_HASH_NO_UPDATE" : " ",
1838384SBhargava.Yenduri@Sun.COM 	    (desc.pd_flags & CRYPTO_SYNCHRONOUS) ?
1848384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_SYNCHRONOUS" : " ",
1853708Skrishna 	    (desc.pd_flags & KCF_LPROV_MEMBER) ?
1868384SBhargava.Yenduri@Sun.COM 	    "KCF_LPROV_MEMBER" : " ",
1873708Skrishna 	    (desc.pd_flags & KCF_PROV_RESTRICTED) ?
1888384SBhargava.Yenduri@Sun.COM 	    "KCF_PROV_RESTRICTED" : " ");
1894072Skrishna 	if (desc.pd_flags & CRYPTO_HASH_NO_UPDATE)
1904072Skrishna 		mdb_printf("pd_hash_limit:\t\t%u\n", desc.pd_hash_limit);
191*9505SBhargava.Yenduri@Sun.COM 
192*9505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_kstat:\t\t%p\n", desc.pd_kstat);
193*9505SBhargava.Yenduri@Sun.COM 
1940Sstevel@tonic-gate 	return (DCMD_OK);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #define	GOT_NONE	(-2)
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*ARGSUSED*/
2000Sstevel@tonic-gate int
2010Sstevel@tonic-gate prov_tab(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	kcf_provider_desc_t **tab;
2040Sstevel@tonic-gate 	kcf_provider_desc_t desc;
2050Sstevel@tonic-gate 	kcf_provider_desc_t *ptr;
2060Sstevel@tonic-gate 	uint_t prov_tab_max;
2070Sstevel@tonic-gate 	int i;
2080Sstevel@tonic-gate 	int gotzero = GOT_NONE;
2090Sstevel@tonic-gate 	char string[MAXNAMELEN + 1];
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
2120Sstevel@tonic-gate 		return (DCMD_USAGE);
2130Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "prov_tab")
2140Sstevel@tonic-gate 	    == -1) {
2150Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab");
2160Sstevel@tonic-gate 		return (DCMD_ERR);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	} else if (mdb_readvar(&prov_tab_max, "prov_tab_max") == -1) {
2190Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab_max");
2200Sstevel@tonic-gate 		return (DCMD_ERR);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 	mdb_printf("%<b>prov_tab = %p%</b>\n", ptr);
2230Sstevel@tonic-gate 	tab = mdb_zalloc(prov_tab_max * sizeof (kcf_provider_desc_t *),
2240Sstevel@tonic-gate 	    UM_SLEEP| UM_GC);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #ifdef DEBUG
2270Sstevel@tonic-gate 	mdb_printf("DEBUG: tab = %p, prov_tab_max = %d\n", tab, prov_tab_max);
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (mdb_vread(tab, prov_tab_max * sizeof (kcf_provider_desc_t *),
2310Sstevel@tonic-gate 	    (uintptr_t)ptr) == -1) {
2320Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab");
2330Sstevel@tonic-gate 		return (DCMD_ERR);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate #ifdef DEBUG
2360Sstevel@tonic-gate 	mdb_printf("DEBUG: got past mdb_vread of tab\n");
2370Sstevel@tonic-gate 	mdb_printf("DEBUG: *tab = %p\n", *tab);
2380Sstevel@tonic-gate #endif
2390Sstevel@tonic-gate 	for (i = 0;  i <  prov_tab_max; i++) {
2400Sstevel@tonic-gate 		/* save space, only print range for long list of nulls */
2410Sstevel@tonic-gate 		if (tab[i] == NULL) {
2420Sstevel@tonic-gate 			if (gotzero == GOT_NONE) {
2438384SBhargava.Yenduri@Sun.COM 				mdb_printf("prov_tab[%d", i);
2448384SBhargava.Yenduri@Sun.COM 				gotzero = i;
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 		} else {
2470Sstevel@tonic-gate 			/* first non-null in awhile, print index of prev null */
2480Sstevel@tonic-gate 			if (gotzero != GOT_NONE) {
2490Sstevel@tonic-gate 				if (gotzero == (i - 1))
2500Sstevel@tonic-gate 					mdb_printf("] = NULL\n", i - 1);
2510Sstevel@tonic-gate 				else
2520Sstevel@tonic-gate 					mdb_printf(" - %d] = NULL\n", i - 1);
2530Sstevel@tonic-gate 				gotzero = GOT_NONE;
2540Sstevel@tonic-gate 			}
2550Sstevel@tonic-gate 			/* interesting value, print it */
2560Sstevel@tonic-gate 			mdb_printf("prov_tab[%d] = %p ", i, tab[i]);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 			if (mdb_vread(&desc, sizeof (kcf_provider_desc_t),
2590Sstevel@tonic-gate 			    (uintptr_t)tab[i]) == -1) {
2600Sstevel@tonic-gate 				mdb_warn("cannot read at address %p",
2610Sstevel@tonic-gate 				    (uintptr_t)tab[i]);
2620Sstevel@tonic-gate 				return (DCMD_ERR);
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 			(void) mdb_readstr(string, MAXNAMELEN + 1,
2660Sstevel@tonic-gate 			    (uintptr_t)desc.pd_name);
2670Sstevel@tonic-gate 			mdb_printf("(%s\t%s)\n", string,
2680Sstevel@tonic-gate 			    prov_states[desc.pd_state]);
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	/* if we've printed the first of many nulls but left the brace open */
2720Sstevel@tonic-gate 	if ((i > 0) && (tab[i-1] == NULL)) {
2730Sstevel@tonic-gate 		if (gotzero == GOT_NONE)
2740Sstevel@tonic-gate 			mdb_printf("] = NULL\n");
2750Sstevel@tonic-gate 		else
2760Sstevel@tonic-gate 			mdb_printf(" - %d] = NULL\n", i - 1);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	return (DCMD_OK);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*ARGSUSED*/
2830Sstevel@tonic-gate int
2840Sstevel@tonic-gate policy_tab(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	kcf_policy_desc_t **tab;
2870Sstevel@tonic-gate 	kcf_policy_desc_t *ptr;
2880Sstevel@tonic-gate 	uint_t policy_tab_max;
2890Sstevel@tonic-gate 	int num, i;
2900Sstevel@tonic-gate 	int gotzero = GOT_NONE;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
2930Sstevel@tonic-gate 		return (DCMD_USAGE);
2940Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "policy_tab")
2950Sstevel@tonic-gate 	    == -1) {
2960Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab");
2970Sstevel@tonic-gate 		return (DCMD_ERR);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	} else if (mdb_readvar(&policy_tab_max, "policy_tab_max") == -1) {
3000Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab_max");
3010Sstevel@tonic-gate 		return (DCMD_ERR);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* get the current number of descriptors in the table */
3050Sstevel@tonic-gate 	if (mdb_readvar(&num, "policy_tab_num") == -1) {
3060Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab_num");
3070Sstevel@tonic-gate 		return (DCMD_ERR);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	mdb_printf("%<b>policy_tab = %p%</b> \tpolicy_tab_num = %d\n",
3100Sstevel@tonic-gate 	    ptr, num);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	tab = mdb_zalloc(policy_tab_max * sizeof (kcf_policy_desc_t *),
3130Sstevel@tonic-gate 	    UM_SLEEP| UM_GC);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (mdb_vread(tab, policy_tab_max * sizeof (kcf_policy_desc_t *),
3160Sstevel@tonic-gate 	    (uintptr_t)ptr) == -1) {
3170Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab");
3180Sstevel@tonic-gate 		return (DCMD_ERR);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate #ifdef DEBUG
3210Sstevel@tonic-gate 	mdb_printf("DEBUG: got past mdb_vread of tab\n");
3220Sstevel@tonic-gate 	mdb_printf("DEBUG: *tab = %p\n", *tab);
3230Sstevel@tonic-gate #endif
3240Sstevel@tonic-gate 	for (i = 0;  i < policy_tab_max; i++) {
3250Sstevel@tonic-gate 		/* save space, only print range for long list of nulls */
3260Sstevel@tonic-gate 		if (tab[i] == NULL) {
3270Sstevel@tonic-gate 			if (gotzero == GOT_NONE) {
3288384SBhargava.Yenduri@Sun.COM 				mdb_printf("policy_tab[%d", i);
3298384SBhargava.Yenduri@Sun.COM 				gotzero = i;
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 		} else {
3320Sstevel@tonic-gate 			/* first non-null in awhile, print index of prev null */
3330Sstevel@tonic-gate 			if (gotzero != GOT_NONE) {
3340Sstevel@tonic-gate 				if (gotzero == (i - 1))
3350Sstevel@tonic-gate 					mdb_printf("] = NULL\n", i - 1);
3360Sstevel@tonic-gate 				else
3370Sstevel@tonic-gate 					mdb_printf(" - %d] = NULL\n", i - 1);
3380Sstevel@tonic-gate 				gotzero = GOT_NONE;
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 			/* interesting value, print it */
3410Sstevel@tonic-gate 			mdb_printf("policy_tab[%d] = %p\n", i, tab[i]);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	/* if we've printed the first of many nulls but left the brace open */
3450Sstevel@tonic-gate 	if ((i > 0) && (tab[i-1] == NULL)) {
3460Sstevel@tonic-gate 		if (gotzero == GOT_NONE)
3470Sstevel@tonic-gate 			mdb_printf("] = NULL\n");
3480Sstevel@tonic-gate 		else
3490Sstevel@tonic-gate 			mdb_printf(" - %d] = NULL\n", i - 1);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (DCMD_OK);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate static void
3560Sstevel@tonic-gate prt_mechs(int count, crypto_mech_name_t *mechs)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	int i;
3590Sstevel@tonic-gate 	char name[CRYPTO_MAX_MECH_NAME + 1];
3600Sstevel@tonic-gate 	char name2[CRYPTO_MAX_MECH_NAME + 3];
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
3630Sstevel@tonic-gate 		if (mdb_readstr(name, CRYPTO_MAX_MECH_NAME,
3640Sstevel@tonic-gate 		    (uintptr_t)((char *)mechs)) == -1)
3650Sstevel@tonic-gate 			continue;
3660Sstevel@tonic-gate 		/* put in quotes */
3670Sstevel@tonic-gate 		(void) mdb_snprintf(name2, sizeof (name2), "\"%s\"", name);
3680Sstevel@tonic-gate 		/* yes, length is 32, but then it will wrap */
3690Sstevel@tonic-gate 		/* this shorter size formats nicely for most cases */
3700Sstevel@tonic-gate 		mdb_printf("mechs[%d]=%-28s", i, name2);
3710Sstevel@tonic-gate 		mdb_printf("%s", i%2 ? "\n" : "  "); /* 2-columns */
3720Sstevel@tonic-gate 		mechs++;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /* ARGSUSED2 */
3770Sstevel@tonic-gate static int
3780Sstevel@tonic-gate prt_soft_conf_entry(kcf_soft_conf_entry_t *addr, kcf_soft_conf_entry_t *entry,
3790Sstevel@tonic-gate     void *cbdata)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	char name[MAXNAMELEN + 1];
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	mdb_printf("\n%<b>kcf_soft_conf_entry_t at %p:%</b>\n", addr);
3840Sstevel@tonic-gate 	mdb_printf("ce_next: %p", entry->ce_next);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (entry->ce_name == NULL)
3878384SBhargava.Yenduri@Sun.COM 		mdb_printf("\tce_name: NULL\n");
3880Sstevel@tonic-gate 	else if (mdb_readstr(name, MAXNAMELEN, (uintptr_t)entry->ce_name)
3898384SBhargava.Yenduri@Sun.COM 	    == -1)
3900Sstevel@tonic-gate 		mdb_printf("could not read ce_name from %p\n",
3918384SBhargava.Yenduri@Sun.COM 		    entry->ce_name);
3920Sstevel@tonic-gate 	else
3930Sstevel@tonic-gate 		mdb_printf("\tce_name: %s\n", name);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	mdb_printf("ce_count: %d\n", entry->ce_count);
3960Sstevel@tonic-gate 	prt_mechs(entry->ce_count, entry->ce_mechs);
3970Sstevel@tonic-gate 	return (WALK_NEXT);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate int
4010Sstevel@tonic-gate soft_conf_walk_init(mdb_walk_state_t *wsp)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	uintptr_t *soft;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (mdb_readsym(&soft, sizeof (kcf_soft_conf_entry_t *),
4060Sstevel@tonic-gate 	    "soft_config_list") == -1) {
4070Sstevel@tonic-gate 		mdb_warn("failed to find 'soft_config_list'");
4080Sstevel@tonic-gate 		return (WALK_ERR);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)soft;
4110Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (kcf_soft_conf_entry_t), UM_SLEEP);
4120Sstevel@tonic-gate 	wsp->walk_callback = (mdb_walk_cb_t)prt_soft_conf_entry;
4130Sstevel@tonic-gate 	return (WALK_NEXT);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * At each step, read a kcf_soft_conf_entry_t into our private storage, then
4180Sstevel@tonic-gate  * invoke the callback function.  We terminate when we reach a NULL ce_next
4190Sstevel@tonic-gate  * pointer.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate int
4220Sstevel@tonic-gate soft_conf_walk_step(mdb_walk_state_t *wsp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	int status;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)	/* then we're done */
4270Sstevel@tonic-gate 		return (WALK_DONE);
4280Sstevel@tonic-gate #ifdef DEBUG
4290Sstevel@tonic-gate 	else
4308384SBhargava.Yenduri@Sun.COM 		mdb_printf("DEBUG: wsp->walk_addr == %p\n", wsp->walk_addr);
4310Sstevel@tonic-gate #endif
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data, sizeof (kcf_soft_conf_entry_t),
4340Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
4350Sstevel@tonic-gate 		mdb_warn("failed to read kcf_soft_conf_entry at %p",
4360Sstevel@tonic-gate 		    wsp->walk_addr);
4370Sstevel@tonic-gate 		return (WALK_DONE);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
4410Sstevel@tonic-gate 	    wsp->walk_cbdata);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	wsp->walk_addr =
4440Sstevel@tonic-gate 	    (uintptr_t)(((kcf_soft_conf_entry_t *)wsp->walk_data)->ce_next);
4450Sstevel@tonic-gate 	return (status);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate  * The walker's fini function is invoked at the end of each walk.  Since we
4500Sstevel@tonic-gate  * dynamically allocated a kcf_soft_conf_entry_t in soft_conf_walk_init,
4510Sstevel@tonic-gate  * we must free it now.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate void
4540Sstevel@tonic-gate soft_conf_walk_fini(mdb_walk_state_t *wsp)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate #ifdef	DEBUG
4570Sstevel@tonic-gate 	mdb_printf("...end of kcf_soft_conf_entry walk\n");
4580Sstevel@tonic-gate #endif
4590Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (kcf_soft_conf_entry_t));
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate /* ARGSUSED2 */
4620Sstevel@tonic-gate int
4630Sstevel@tonic-gate kcf_soft_conf_entry(uintptr_t addr, uint_t flags, int argc,
4640Sstevel@tonic-gate     const mdb_arg_t *argv)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	kcf_soft_conf_entry_t entry;
4670Sstevel@tonic-gate 	kcf_soft_conf_entry_t *ptr;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
4700Sstevel@tonic-gate 		if (addr == NULL) 	/* not allowed with DCMD_ADDRSPEC */
4710Sstevel@tonic-gate 			return (DCMD_USAGE);
4720Sstevel@tonic-gate 		else
4730Sstevel@tonic-gate 			ptr = (kcf_soft_conf_entry_t *)addr;
4740Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "soft_config_list")
4758384SBhargava.Yenduri@Sun.COM 	    == -1) {
4768384SBhargava.Yenduri@Sun.COM 		mdb_warn("cannot read soft_config_list");
4778384SBhargava.Yenduri@Sun.COM 		return (DCMD_ERR);
4780Sstevel@tonic-gate 	} else
4790Sstevel@tonic-gate 		mdb_printf("soft_config_list = %p\n", ptr);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (ptr == NULL)
4820Sstevel@tonic-gate 		return (DCMD_OK);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (mdb_vread(&entry, sizeof (kcf_soft_conf_entry_t), (uintptr_t)ptr)
4850Sstevel@tonic-gate 	    == -1) {
4868384SBhargava.Yenduri@Sun.COM 		mdb_warn("cannot read at address %p", (uintptr_t)ptr);
4878384SBhargava.Yenduri@Sun.COM 		return (DCMD_ERR);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/* this could change in the future to have more than one ret val */
4910Sstevel@tonic-gate 	if (prt_soft_conf_entry(ptr, &entry, NULL) != WALK_ERR)
4920Sstevel@tonic-gate 		return (DCMD_OK);
4930Sstevel@tonic-gate 	return (DCMD_ERR);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /* ARGSUSED1 */
4970Sstevel@tonic-gate int
4980Sstevel@tonic-gate kcf_policy_desc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	kcf_policy_desc_t  desc;
5010Sstevel@tonic-gate 	char name[MAXNAMELEN + 1];
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) != DCMD_ADDRSPEC)
5050Sstevel@tonic-gate 		return (DCMD_USAGE);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (mdb_vread(&desc, sizeof (kcf_policy_desc_t), (uintptr_t)addr)
5080Sstevel@tonic-gate 	    == -1) {
5090Sstevel@tonic-gate 		mdb_warn("Could not read kcf_policy_desc_t at %p\n", addr);
5100Sstevel@tonic-gate 		return (DCMD_ERR);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	mdb_printf("pd_prov_type:  %s",
5138384SBhargava.Yenduri@Sun.COM 	    desc.pd_prov_type == CRYPTO_HW_PROVIDER ? "CRYPTO_HW_PROVIDER" :
5148384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_SW_PROVIDER");
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (desc.pd_name == NULL)
5170Sstevel@tonic-gate 		mdb_printf("\tpd_name: NULL\n");
5180Sstevel@tonic-gate 	else if (mdb_readstr(name, MAXNAMELEN, (uintptr_t)desc.pd_name)
5190Sstevel@tonic-gate 	    == -1)
5200Sstevel@tonic-gate 		mdb_printf("could not read pd_name from %p\n",
5210Sstevel@tonic-gate 		    desc.pd_name);
5220Sstevel@tonic-gate 	else
5230Sstevel@tonic-gate 		mdb_printf("\tpd_name: %s\n", name);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	mdb_printf("pd_instance: %d ", desc.pd_instance);
5260Sstevel@tonic-gate 	mdb_printf("\t\tpd_refcnt: %d\n", desc.pd_refcnt);
5270Sstevel@tonic-gate 	mdb_printf("pd_mutex: %p", desc.pd_mutex);
5280Sstevel@tonic-gate 	mdb_printf("\t\tpd_disabled_count: %d", desc.pd_disabled_count);
5290Sstevel@tonic-gate 	mdb_printf("\npd_disabled_mechs:\n");
5300Sstevel@tonic-gate 	mdb_inc_indent(4);
5310Sstevel@tonic-gate 	prt_mechs(desc.pd_disabled_count, desc.pd_disabled_mechs);
5320Sstevel@tonic-gate 	mdb_dec_indent(4);
5330Sstevel@tonic-gate 	return (DCMD_OK);
5340Sstevel@tonic-gate }
535