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 /*
229505SBhargava.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",
489505SBhargava.Yenduri@Sun.COM 	"KCF_PROV_UNREGISTERING",
499505SBhargava.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;
619505SBhargava.Yenduri@Sun.COM 	kcf_prov_cpu_t stats;
629505SBhargava.Yenduri@Sun.COM 	uint64_t dtotal, ftotal, btotal;
639505SBhargava.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 
1039505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_sid:\t\t\t%u\n", desc.pd_sid);
1049505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_taskq:\t\t%p\n", desc.pd_taskq);
1059505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_nbins:\t\t%u\n", desc.pd_nbins);
1069505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_percpu_bins:\t\t%p\n", desc.pd_percpu_bins);
1079505SBhargava.Yenduri@Sun.COM 
1089505SBhargava.Yenduri@Sun.COM 	dtotal = ftotal = btotal = 0;
1099505SBhargava.Yenduri@Sun.COM 	holdcnt = jobcnt = 0;
1109505SBhargava.Yenduri@Sun.COM 	for (i = 0; i < desc.pd_nbins; i++) {
1119505SBhargava.Yenduri@Sun.COM 		if (mdb_vread(&stats, sizeof (kcf_prov_cpu_t),
1129505SBhargava.Yenduri@Sun.COM 		    (uintptr_t)(desc.pd_percpu_bins + i)) == -1) {
1139505SBhargava.Yenduri@Sun.COM 			mdb_warn("cannot read addr %p",
1149505SBhargava.Yenduri@Sun.COM 			    desc.pd_percpu_bins + i);
1159505SBhargava.Yenduri@Sun.COM 			return (DCMD_ERR);
1169505SBhargava.Yenduri@Sun.COM 		}
1179505SBhargava.Yenduri@Sun.COM 
1189505SBhargava.Yenduri@Sun.COM 		holdcnt += stats.kp_holdcnt;
1199505SBhargava.Yenduri@Sun.COM 		jobcnt += stats.kp_jobcnt;
1209505SBhargava.Yenduri@Sun.COM 		dtotal += stats.kp_ndispatches;
1219505SBhargava.Yenduri@Sun.COM 		ftotal += stats.kp_nfails;
1229505SBhargava.Yenduri@Sun.COM 		btotal += stats.kp_nbusy_rval;
1239505SBhargava.Yenduri@Sun.COM 	}
1249505SBhargava.Yenduri@Sun.COM 	mdb_inc_indent(4);
1259505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_holdcnt:\t\t%d\n", holdcnt);
1269505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_jobcnt:\t\t%u\n", jobcnt);
1279505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_ndispatches:\t%llu\n", dtotal);
1289505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_nfails:\t\t%llu\n", ftotal);
1299505SBhargava.Yenduri@Sun.COM 	mdb_printf("total kp_nbusy_rval:\t%llu\n", btotal);
1309505SBhargava.Yenduri@Sun.COM 	mdb_dec_indent(4);
1319505SBhargava.Yenduri@Sun.COM 
1329505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_prov_handle:\t\t%p\n", desc.pd_prov_handle);
1339505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_kcf_prov_handle:\t%u\n", desc.pd_kcf_prov_handle);
1349505SBhargava.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 ||
1719505SBhargava.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]);
1759505SBhargava.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);
178*11304SJanie.Lu@Sun.COM 	mdb_printf("pd_flags:\t\t%s %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" : " ",
183*11304SJanie.Lu@Sun.COM 	    (desc.pd_flags & CRYPTO_HMAC_NO_UPDATE) ?
184*11304SJanie.Lu@Sun.COM 	    "CRYPTO_HMAC_NO_UPDATE" : " ",
1858384SBhargava.Yenduri@Sun.COM 	    (desc.pd_flags & CRYPTO_SYNCHRONOUS) ?
1868384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_SYNCHRONOUS" : " ",
1873708Skrishna 	    (desc.pd_flags & KCF_LPROV_MEMBER) ?
1888384SBhargava.Yenduri@Sun.COM 	    "KCF_LPROV_MEMBER" : " ",
1893708Skrishna 	    (desc.pd_flags & KCF_PROV_RESTRICTED) ?
1908384SBhargava.Yenduri@Sun.COM 	    "KCF_PROV_RESTRICTED" : " ");
1914072Skrishna 	if (desc.pd_flags & CRYPTO_HASH_NO_UPDATE)
1924072Skrishna 		mdb_printf("pd_hash_limit:\t\t%u\n", desc.pd_hash_limit);
193*11304SJanie.Lu@Sun.COM 	if (desc.pd_flags & CRYPTO_HMAC_NO_UPDATE)
194*11304SJanie.Lu@Sun.COM 		mdb_printf("pd_hmac_limit:\t\t%u\n", desc.pd_hmac_limit);
1959505SBhargava.Yenduri@Sun.COM 
1969505SBhargava.Yenduri@Sun.COM 	mdb_printf("pd_kstat:\t\t%p\n", desc.pd_kstat);
1979505SBhargava.Yenduri@Sun.COM 
1980Sstevel@tonic-gate 	return (DCMD_OK);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate #define	GOT_NONE	(-2)
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*ARGSUSED*/
2040Sstevel@tonic-gate int
2050Sstevel@tonic-gate prov_tab(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	kcf_provider_desc_t **tab;
2080Sstevel@tonic-gate 	kcf_provider_desc_t desc;
2090Sstevel@tonic-gate 	kcf_provider_desc_t *ptr;
2100Sstevel@tonic-gate 	uint_t prov_tab_max;
2110Sstevel@tonic-gate 	int i;
2120Sstevel@tonic-gate 	int gotzero = GOT_NONE;
2130Sstevel@tonic-gate 	char string[MAXNAMELEN + 1];
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
2160Sstevel@tonic-gate 		return (DCMD_USAGE);
2170Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "prov_tab")
2180Sstevel@tonic-gate 	    == -1) {
2190Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab");
2200Sstevel@tonic-gate 		return (DCMD_ERR);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	} else if (mdb_readvar(&prov_tab_max, "prov_tab_max") == -1) {
2230Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab_max");
2240Sstevel@tonic-gate 		return (DCMD_ERR);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	mdb_printf("%<b>prov_tab = %p%</b>\n", ptr);
2270Sstevel@tonic-gate 	tab = mdb_zalloc(prov_tab_max * sizeof (kcf_provider_desc_t *),
2280Sstevel@tonic-gate 	    UM_SLEEP| UM_GC);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate #ifdef DEBUG
2310Sstevel@tonic-gate 	mdb_printf("DEBUG: tab = %p, prov_tab_max = %d\n", tab, prov_tab_max);
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (mdb_vread(tab, prov_tab_max * sizeof (kcf_provider_desc_t *),
2350Sstevel@tonic-gate 	    (uintptr_t)ptr) == -1) {
2360Sstevel@tonic-gate 		mdb_warn("cannot read prov_tab");
2370Sstevel@tonic-gate 		return (DCMD_ERR);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate #ifdef DEBUG
2400Sstevel@tonic-gate 	mdb_printf("DEBUG: got past mdb_vread of tab\n");
2410Sstevel@tonic-gate 	mdb_printf("DEBUG: *tab = %p\n", *tab);
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 	for (i = 0;  i <  prov_tab_max; i++) {
2440Sstevel@tonic-gate 		/* save space, only print range for long list of nulls */
2450Sstevel@tonic-gate 		if (tab[i] == NULL) {
2460Sstevel@tonic-gate 			if (gotzero == GOT_NONE) {
2478384SBhargava.Yenduri@Sun.COM 				mdb_printf("prov_tab[%d", i);
2488384SBhargava.Yenduri@Sun.COM 				gotzero = i;
2490Sstevel@tonic-gate 			}
2500Sstevel@tonic-gate 		} else {
2510Sstevel@tonic-gate 			/* first non-null in awhile, print index of prev null */
2520Sstevel@tonic-gate 			if (gotzero != GOT_NONE) {
2530Sstevel@tonic-gate 				if (gotzero == (i - 1))
2540Sstevel@tonic-gate 					mdb_printf("] = NULL\n", i - 1);
2550Sstevel@tonic-gate 				else
2560Sstevel@tonic-gate 					mdb_printf(" - %d] = NULL\n", i - 1);
2570Sstevel@tonic-gate 				gotzero = GOT_NONE;
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 			/* interesting value, print it */
2600Sstevel@tonic-gate 			mdb_printf("prov_tab[%d] = %p ", i, tab[i]);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			if (mdb_vread(&desc, sizeof (kcf_provider_desc_t),
2630Sstevel@tonic-gate 			    (uintptr_t)tab[i]) == -1) {
2640Sstevel@tonic-gate 				mdb_warn("cannot read at address %p",
2650Sstevel@tonic-gate 				    (uintptr_t)tab[i]);
2660Sstevel@tonic-gate 				return (DCMD_ERR);
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 			(void) mdb_readstr(string, MAXNAMELEN + 1,
2700Sstevel@tonic-gate 			    (uintptr_t)desc.pd_name);
2710Sstevel@tonic-gate 			mdb_printf("(%s\t%s)\n", string,
2720Sstevel@tonic-gate 			    prov_states[desc.pd_state]);
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 	/* if we've printed the first of many nulls but left the brace open */
2760Sstevel@tonic-gate 	if ((i > 0) && (tab[i-1] == NULL)) {
2770Sstevel@tonic-gate 		if (gotzero == GOT_NONE)
2780Sstevel@tonic-gate 			mdb_printf("] = NULL\n");
2790Sstevel@tonic-gate 		else
2800Sstevel@tonic-gate 			mdb_printf(" - %d] = NULL\n", i - 1);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (DCMD_OK);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*ARGSUSED*/
2870Sstevel@tonic-gate int
2880Sstevel@tonic-gate policy_tab(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	kcf_policy_desc_t **tab;
2910Sstevel@tonic-gate 	kcf_policy_desc_t *ptr;
2920Sstevel@tonic-gate 	uint_t policy_tab_max;
2930Sstevel@tonic-gate 	int num, i;
2940Sstevel@tonic-gate 	int gotzero = GOT_NONE;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
2970Sstevel@tonic-gate 		return (DCMD_USAGE);
2980Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "policy_tab")
2990Sstevel@tonic-gate 	    == -1) {
3000Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab");
3010Sstevel@tonic-gate 		return (DCMD_ERR);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	} else if (mdb_readvar(&policy_tab_max, "policy_tab_max") == -1) {
3040Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab_max");
3050Sstevel@tonic-gate 		return (DCMD_ERR);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/* get the current number of descriptors in the table */
3090Sstevel@tonic-gate 	if (mdb_readvar(&num, "policy_tab_num") == -1) {
3100Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab_num");
3110Sstevel@tonic-gate 		return (DCMD_ERR);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	mdb_printf("%<b>policy_tab = %p%</b> \tpolicy_tab_num = %d\n",
3140Sstevel@tonic-gate 	    ptr, num);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	tab = mdb_zalloc(policy_tab_max * sizeof (kcf_policy_desc_t *),
3170Sstevel@tonic-gate 	    UM_SLEEP| UM_GC);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if (mdb_vread(tab, policy_tab_max * sizeof (kcf_policy_desc_t *),
3200Sstevel@tonic-gate 	    (uintptr_t)ptr) == -1) {
3210Sstevel@tonic-gate 		mdb_warn("cannot read policy_tab");
3220Sstevel@tonic-gate 		return (DCMD_ERR);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate #ifdef DEBUG
3250Sstevel@tonic-gate 	mdb_printf("DEBUG: got past mdb_vread of tab\n");
3260Sstevel@tonic-gate 	mdb_printf("DEBUG: *tab = %p\n", *tab);
3270Sstevel@tonic-gate #endif
3280Sstevel@tonic-gate 	for (i = 0;  i < policy_tab_max; i++) {
3290Sstevel@tonic-gate 		/* save space, only print range for long list of nulls */
3300Sstevel@tonic-gate 		if (tab[i] == NULL) {
3310Sstevel@tonic-gate 			if (gotzero == GOT_NONE) {
3328384SBhargava.Yenduri@Sun.COM 				mdb_printf("policy_tab[%d", i);
3338384SBhargava.Yenduri@Sun.COM 				gotzero = i;
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 		} else {
3360Sstevel@tonic-gate 			/* first non-null in awhile, print index of prev null */
3370Sstevel@tonic-gate 			if (gotzero != GOT_NONE) {
3380Sstevel@tonic-gate 				if (gotzero == (i - 1))
3390Sstevel@tonic-gate 					mdb_printf("] = NULL\n", i - 1);
3400Sstevel@tonic-gate 				else
3410Sstevel@tonic-gate 					mdb_printf(" - %d] = NULL\n", i - 1);
3420Sstevel@tonic-gate 				gotzero = GOT_NONE;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			/* interesting value, print it */
3450Sstevel@tonic-gate 			mdb_printf("policy_tab[%d] = %p\n", i, tab[i]);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 	/* if we've printed the first of many nulls but left the brace open */
3490Sstevel@tonic-gate 	if ((i > 0) && (tab[i-1] == NULL)) {
3500Sstevel@tonic-gate 		if (gotzero == GOT_NONE)
3510Sstevel@tonic-gate 			mdb_printf("] = NULL\n");
3520Sstevel@tonic-gate 		else
3530Sstevel@tonic-gate 			mdb_printf(" - %d] = NULL\n", i - 1);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	return (DCMD_OK);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate static void
3600Sstevel@tonic-gate prt_mechs(int count, crypto_mech_name_t *mechs)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	int i;
3630Sstevel@tonic-gate 	char name[CRYPTO_MAX_MECH_NAME + 1];
3640Sstevel@tonic-gate 	char name2[CRYPTO_MAX_MECH_NAME + 3];
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
3670Sstevel@tonic-gate 		if (mdb_readstr(name, CRYPTO_MAX_MECH_NAME,
3680Sstevel@tonic-gate 		    (uintptr_t)((char *)mechs)) == -1)
3690Sstevel@tonic-gate 			continue;
3700Sstevel@tonic-gate 		/* put in quotes */
3710Sstevel@tonic-gate 		(void) mdb_snprintf(name2, sizeof (name2), "\"%s\"", name);
3720Sstevel@tonic-gate 		/* yes, length is 32, but then it will wrap */
3730Sstevel@tonic-gate 		/* this shorter size formats nicely for most cases */
3740Sstevel@tonic-gate 		mdb_printf("mechs[%d]=%-28s", i, name2);
3750Sstevel@tonic-gate 		mdb_printf("%s", i%2 ? "\n" : "  "); /* 2-columns */
3760Sstevel@tonic-gate 		mechs++;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /* ARGSUSED2 */
3810Sstevel@tonic-gate static int
3820Sstevel@tonic-gate prt_soft_conf_entry(kcf_soft_conf_entry_t *addr, kcf_soft_conf_entry_t *entry,
3830Sstevel@tonic-gate     void *cbdata)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	char name[MAXNAMELEN + 1];
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	mdb_printf("\n%<b>kcf_soft_conf_entry_t at %p:%</b>\n", addr);
3880Sstevel@tonic-gate 	mdb_printf("ce_next: %p", entry->ce_next);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (entry->ce_name == NULL)
3918384SBhargava.Yenduri@Sun.COM 		mdb_printf("\tce_name: NULL\n");
3920Sstevel@tonic-gate 	else if (mdb_readstr(name, MAXNAMELEN, (uintptr_t)entry->ce_name)
3938384SBhargava.Yenduri@Sun.COM 	    == -1)
3940Sstevel@tonic-gate 		mdb_printf("could not read ce_name from %p\n",
3958384SBhargava.Yenduri@Sun.COM 		    entry->ce_name);
3960Sstevel@tonic-gate 	else
3970Sstevel@tonic-gate 		mdb_printf("\tce_name: %s\n", name);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	mdb_printf("ce_count: %d\n", entry->ce_count);
4000Sstevel@tonic-gate 	prt_mechs(entry->ce_count, entry->ce_mechs);
4010Sstevel@tonic-gate 	return (WALK_NEXT);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate int
4050Sstevel@tonic-gate soft_conf_walk_init(mdb_walk_state_t *wsp)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	uintptr_t *soft;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (mdb_readsym(&soft, sizeof (kcf_soft_conf_entry_t *),
4100Sstevel@tonic-gate 	    "soft_config_list") == -1) {
4110Sstevel@tonic-gate 		mdb_warn("failed to find 'soft_config_list'");
4120Sstevel@tonic-gate 		return (WALK_ERR);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)soft;
4150Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (kcf_soft_conf_entry_t), UM_SLEEP);
4160Sstevel@tonic-gate 	wsp->walk_callback = (mdb_walk_cb_t)prt_soft_conf_entry;
4170Sstevel@tonic-gate 	return (WALK_NEXT);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * At each step, read a kcf_soft_conf_entry_t into our private storage, then
4220Sstevel@tonic-gate  * invoke the callback function.  We terminate when we reach a NULL ce_next
4230Sstevel@tonic-gate  * pointer.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate int
4260Sstevel@tonic-gate soft_conf_walk_step(mdb_walk_state_t *wsp)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	int status;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)	/* then we're done */
4310Sstevel@tonic-gate 		return (WALK_DONE);
4320Sstevel@tonic-gate #ifdef DEBUG
4330Sstevel@tonic-gate 	else
4348384SBhargava.Yenduri@Sun.COM 		mdb_printf("DEBUG: wsp->walk_addr == %p\n", wsp->walk_addr);
4350Sstevel@tonic-gate #endif
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data, sizeof (kcf_soft_conf_entry_t),
4380Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
4390Sstevel@tonic-gate 		mdb_warn("failed to read kcf_soft_conf_entry at %p",
4400Sstevel@tonic-gate 		    wsp->walk_addr);
4410Sstevel@tonic-gate 		return (WALK_DONE);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
4450Sstevel@tonic-gate 	    wsp->walk_cbdata);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	wsp->walk_addr =
4480Sstevel@tonic-gate 	    (uintptr_t)(((kcf_soft_conf_entry_t *)wsp->walk_data)->ce_next);
4490Sstevel@tonic-gate 	return (status);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * The walker's fini function is invoked at the end of each walk.  Since we
4540Sstevel@tonic-gate  * dynamically allocated a kcf_soft_conf_entry_t in soft_conf_walk_init,
4550Sstevel@tonic-gate  * we must free it now.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate void
4580Sstevel@tonic-gate soft_conf_walk_fini(mdb_walk_state_t *wsp)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate #ifdef	DEBUG
4610Sstevel@tonic-gate 	mdb_printf("...end of kcf_soft_conf_entry walk\n");
4620Sstevel@tonic-gate #endif
4630Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (kcf_soft_conf_entry_t));
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate /* ARGSUSED2 */
4660Sstevel@tonic-gate int
4670Sstevel@tonic-gate kcf_soft_conf_entry(uintptr_t addr, uint_t flags, int argc,
4680Sstevel@tonic-gate     const mdb_arg_t *argv)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	kcf_soft_conf_entry_t entry;
4710Sstevel@tonic-gate 	kcf_soft_conf_entry_t *ptr;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == DCMD_ADDRSPEC) {
4740Sstevel@tonic-gate 		if (addr == NULL) 	/* not allowed with DCMD_ADDRSPEC */
4750Sstevel@tonic-gate 			return (DCMD_USAGE);
4760Sstevel@tonic-gate 		else
4770Sstevel@tonic-gate 			ptr = (kcf_soft_conf_entry_t *)addr;
4780Sstevel@tonic-gate 	} else if (mdb_readsym(&ptr, sizeof (void *), "soft_config_list")
4798384SBhargava.Yenduri@Sun.COM 	    == -1) {
4808384SBhargava.Yenduri@Sun.COM 		mdb_warn("cannot read soft_config_list");
4818384SBhargava.Yenduri@Sun.COM 		return (DCMD_ERR);
4820Sstevel@tonic-gate 	} else
4830Sstevel@tonic-gate 		mdb_printf("soft_config_list = %p\n", ptr);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if (ptr == NULL)
4860Sstevel@tonic-gate 		return (DCMD_OK);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (mdb_vread(&entry, sizeof (kcf_soft_conf_entry_t), (uintptr_t)ptr)
4890Sstevel@tonic-gate 	    == -1) {
4908384SBhargava.Yenduri@Sun.COM 		mdb_warn("cannot read at address %p", (uintptr_t)ptr);
4918384SBhargava.Yenduri@Sun.COM 		return (DCMD_ERR);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* this could change in the future to have more than one ret val */
4950Sstevel@tonic-gate 	if (prt_soft_conf_entry(ptr, &entry, NULL) != WALK_ERR)
4960Sstevel@tonic-gate 		return (DCMD_OK);
4970Sstevel@tonic-gate 	return (DCMD_ERR);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /* ARGSUSED1 */
5010Sstevel@tonic-gate int
5020Sstevel@tonic-gate kcf_policy_desc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	kcf_policy_desc_t  desc;
5050Sstevel@tonic-gate 	char name[MAXNAMELEN + 1];
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) != DCMD_ADDRSPEC)
5090Sstevel@tonic-gate 		return (DCMD_USAGE);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (mdb_vread(&desc, sizeof (kcf_policy_desc_t), (uintptr_t)addr)
5120Sstevel@tonic-gate 	    == -1) {
5130Sstevel@tonic-gate 		mdb_warn("Could not read kcf_policy_desc_t at %p\n", addr);
5140Sstevel@tonic-gate 		return (DCMD_ERR);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	mdb_printf("pd_prov_type:  %s",
5178384SBhargava.Yenduri@Sun.COM 	    desc.pd_prov_type == CRYPTO_HW_PROVIDER ? "CRYPTO_HW_PROVIDER" :
5188384SBhargava.Yenduri@Sun.COM 	    "CRYPTO_SW_PROVIDER");
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (desc.pd_name == NULL)
5210Sstevel@tonic-gate 		mdb_printf("\tpd_name: NULL\n");
5220Sstevel@tonic-gate 	else if (mdb_readstr(name, MAXNAMELEN, (uintptr_t)desc.pd_name)
5230Sstevel@tonic-gate 	    == -1)
5240Sstevel@tonic-gate 		mdb_printf("could not read pd_name from %p\n",
5250Sstevel@tonic-gate 		    desc.pd_name);
5260Sstevel@tonic-gate 	else
5270Sstevel@tonic-gate 		mdb_printf("\tpd_name: %s\n", name);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	mdb_printf("pd_instance: %d ", desc.pd_instance);
5300Sstevel@tonic-gate 	mdb_printf("\t\tpd_refcnt: %d\n", desc.pd_refcnt);
5310Sstevel@tonic-gate 	mdb_printf("pd_mutex: %p", desc.pd_mutex);
5320Sstevel@tonic-gate 	mdb_printf("\t\tpd_disabled_count: %d", desc.pd_disabled_count);
5330Sstevel@tonic-gate 	mdb_printf("\npd_disabled_mechs:\n");
5340Sstevel@tonic-gate 	mdb_inc_indent(4);
5350Sstevel@tonic-gate 	prt_mechs(desc.pd_disabled_count, desc.pd_disabled_mechs);
5360Sstevel@tonic-gate 	mdb_dec_indent(4);
5370Sstevel@tonic-gate 	return (DCMD_OK);
5380Sstevel@tonic-gate }
539