xref: /illumos-gate/usr/src/cmd/mdb/common/modules/cpc/cpc.c (revision 3b442230)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*3b442230SJordan Paige Hendricks  *
26*3b442230SJordan Paige Hendricks  * Copyright 2019 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/mdb_modapi.h>
307c478bd9Sstevel@tonic-gate #include <sys/kcpc.h>
317c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define	 KCPC_HASH_BUCKETS	(1l << KCPC_LOG2_HASH_BUCKETS)
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Assume 64-bit kernel address max is 100000000000 - 1.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate #ifdef _LP64
397c478bd9Sstevel@tonic-gate #define	ADDR_WIDTH 11
407c478bd9Sstevel@tonic-gate #else
417c478bd9Sstevel@tonic-gate #define	ADDR_WIDTH 8
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct cpc_ctx_aux {
457c478bd9Sstevel@tonic-gate 	uintptr_t  cca_hash[KCPC_HASH_BUCKETS];
467c478bd9Sstevel@tonic-gate 	int	   cca_bucket;
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
507c478bd9Sstevel@tonic-gate static int
cpc(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)517c478bd9Sstevel@tonic-gate cpc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	kcpc_ctx_t	ctx;
547c478bd9Sstevel@tonic-gate 	kcpc_set_t	set;
557c478bd9Sstevel@tonic-gate 	kcpc_request_t	*reqs;
567c478bd9Sstevel@tonic-gate 	uint64_t	*data;
577c478bd9Sstevel@tonic-gate 	kcpc_attr_t	*attr;
587c478bd9Sstevel@tonic-gate 	int		i;
597c478bd9Sstevel@tonic-gate 	int		j;
607c478bd9Sstevel@tonic-gate 	uint_t		opt_v = FALSE;
617c478bd9Sstevel@tonic-gate 
62*3b442230SJordan Paige Hendricks 	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL) !=
63*3b442230SJordan Paige Hendricks 	    argc)
647c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0) {
677c478bd9Sstevel@tonic-gate 		/*
687c478bd9Sstevel@tonic-gate 		 * We weren't given the address of any specific cpc ctx, so
697c478bd9Sstevel@tonic-gate 		 * invoke the walker to find them all.
707c478bd9Sstevel@tonic-gate 		 */
717c478bd9Sstevel@tonic-gate 		mdb_walk_dcmd("cpc_ctx", "cpc", argc, argv);
727c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ctx, sizeof (ctx), addr) == -1) {
767c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read kcpc_ctx_t at %p", addr);
777c478bd9Sstevel@tonic-gate 		return (DCMD_ABORT);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (mdb_vread(&set, sizeof (set), (uintptr_t)ctx.kc_set) == -1) {
817c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read kcpc_set_t at %p", ctx.kc_set);
827c478bd9Sstevel@tonic-gate 		return (DCMD_ABORT);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	reqs = mdb_alloc(set.ks_nreqs * sizeof (*reqs), UM_GC);
867c478bd9Sstevel@tonic-gate 	data = mdb_alloc(set.ks_nreqs * sizeof (*data), UM_GC);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (mdb_vread(reqs, set.ks_nreqs * sizeof (*reqs),
897c478bd9Sstevel@tonic-gate 	    (uintptr_t)set.ks_req) == -1) {
907c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read requests at %p", set.ks_req);
917c478bd9Sstevel@tonic-gate 		return (DCMD_ABORT);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (mdb_vread(data, set.ks_nreqs * sizeof (*data),
957c478bd9Sstevel@tonic-gate 	    (uintptr_t)set.ks_data) == -1) {
967c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read set data at %p", set.ks_data);
977c478bd9Sstevel@tonic-gate 		return (DCMD_ABORT);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
1017c478bd9Sstevel@tonic-gate 		mdb_printf("N PIC NDX %16s FLG %16s %*s EVENT\n", "VAL",
1027c478bd9Sstevel@tonic-gate 		    "PRESET", ADDR_WIDTH, "CFG");
1037c478bd9Sstevel@tonic-gate 	mdb_printf("-----------------------------------------------------------"
1047c478bd9Sstevel@tonic-gate 	    "---------------------\n");
1057c478bd9Sstevel@tonic-gate 	if (opt_v)
1067c478bd9Sstevel@tonic-gate 		mdb_printf("Set: %p\t%d requests. Flags = %x\n", ctx.kc_set,
1077c478bd9Sstevel@tonic-gate 		    set.ks_nreqs, set.ks_flags);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	for (i = 0; i < set.ks_nreqs; i++) {
1107c478bd9Sstevel@tonic-gate 		mdb_printf("%d %3d %3d %16llx %1s%1s%1s %16llx %8p %s\n", i,
1117c478bd9Sstevel@tonic-gate 		    reqs[i].kr_picnum, reqs[i].kr_index,
1127c478bd9Sstevel@tonic-gate 		    data[reqs[i].kr_index],
1137c478bd9Sstevel@tonic-gate 		    (reqs[i].kr_flags & CPC_OVF_NOTIFY_EMT)	? "O" : "",
1147c478bd9Sstevel@tonic-gate 		    (reqs[i].kr_flags & CPC_COUNT_USER)		? "U" : "",
1157c478bd9Sstevel@tonic-gate 		    (reqs[i].kr_flags & CPC_COUNT_SYSTEM)	? "S" : "",
1167c478bd9Sstevel@tonic-gate 		    reqs[i].kr_preset, reqs[i].kr_config,
1177c478bd9Sstevel@tonic-gate 		    reqs[i].kr_event);
1187c478bd9Sstevel@tonic-gate 		if (opt_v == 0)
1197c478bd9Sstevel@tonic-gate 			continue;
1207c478bd9Sstevel@tonic-gate 		if (reqs[i].kr_nattrs > 0) {
1217c478bd9Sstevel@tonic-gate 			attr = mdb_alloc(reqs[i].kr_nattrs * sizeof (*attr),
1227c478bd9Sstevel@tonic-gate 			    UM_GC);
1237c478bd9Sstevel@tonic-gate 			if (mdb_vread(attr, reqs[i].kr_nattrs * sizeof (*attr),
1247c478bd9Sstevel@tonic-gate 			    (uintptr_t)reqs[i].kr_attr) == -1) {
1257c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read attributes at %p",
1267c478bd9Sstevel@tonic-gate 				    reqs[i].kr_attr);
1277c478bd9Sstevel@tonic-gate 				return (DCMD_ABORT);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 			for (j = 0; j < reqs[i].kr_nattrs; j++)
1307c478bd9Sstevel@tonic-gate 				mdb_printf("\t%s = %llx", attr[j].ka_name,
1317c478bd9Sstevel@tonic-gate 				    attr[j].ka_val);
1327c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void
cpc_help(void)1407c478bd9Sstevel@tonic-gate cpc_help(void)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	mdb_printf("Displays the contents of the CPC context at the supplied "
1437c478bd9Sstevel@tonic-gate 	    "address.  If no address is given, displays contents of all active "
1447c478bd9Sstevel@tonic-gate 	    "CPC contexts.\n");
1457c478bd9Sstevel@tonic-gate 	mdb_printf("Flag codes: \n"
1467c478bd9Sstevel@tonic-gate 	    "O = overflow notify     U = count user events     "
1477c478bd9Sstevel@tonic-gate 	    "S = count system events\n");
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * Initialize the global walk by grabbing the hash table in the
1537c478bd9Sstevel@tonic-gate  * cpc module.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate static int
cpc_ctx_walk_init(mdb_walk_state_t * wsp)1567c478bd9Sstevel@tonic-gate cpc_ctx_walk_init(mdb_walk_state_t *wsp)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	struct cpc_ctx_aux *cca;
1597c478bd9Sstevel@tonic-gate 
160892ad162SToomas Soome 	if (wsp->walk_addr != 0) {
1617c478bd9Sstevel@tonic-gate 		mdb_warn("only global cpc_ctx walk supported\n");
1627c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	cca = mdb_zalloc(sizeof (*cca), UM_SLEEP);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (mdb_readsym(&cca->cca_hash, sizeof (cca->cca_hash),
1687c478bd9Sstevel@tonic-gate 	    "kcpc_ctx_list") == -1) {
1697c478bd9Sstevel@tonic-gate 		mdb_warn("cannot read cpc_ctx hash table");
1707c478bd9Sstevel@tonic-gate 		mdb_free(cca, sizeof (*cca));
1717c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	wsp->walk_data = cca;
175892ad162SToomas Soome 	wsp->walk_addr = 0;
1767c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static int
cpc_ctx_walk_step(mdb_walk_state_t * wsp)1807c478bd9Sstevel@tonic-gate cpc_ctx_walk_step(mdb_walk_state_t *wsp)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	int status;
1837c478bd9Sstevel@tonic-gate 	kcpc_ctx_t ctx;
1847c478bd9Sstevel@tonic-gate 	struct cpc_ctx_aux *cca = wsp->walk_data;
1857c478bd9Sstevel@tonic-gate 
186892ad162SToomas Soome 	while (wsp->walk_addr == 0) {
1877c478bd9Sstevel@tonic-gate 		if (cca->cca_bucket == KCPC_HASH_BUCKETS)
1887c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
1897c478bd9Sstevel@tonic-gate 		wsp->walk_addr = cca->cca_hash[cca->cca_bucket++];
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ctx, sizeof (ctx), wsp->walk_addr) == -1) {
1937c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read cpc_ctx at %p", wsp->walk_addr);
1947c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &ctx,
1987c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)ctx.kc_next;
2017c478bd9Sstevel@tonic-gate 	return (status);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static void
cpc_ctx_walk_fini(mdb_walk_state_t * wsp)2057c478bd9Sstevel@tonic-gate cpc_ctx_walk_fini(mdb_walk_state_t *wsp)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (struct cpc_ctx_aux));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
2117c478bd9Sstevel@tonic-gate 	{ "cpc_ctx", "walk global list of cpc contexts",
2127c478bd9Sstevel@tonic-gate 		cpc_ctx_walk_init, cpc_ctx_walk_step, cpc_ctx_walk_fini },
2137c478bd9Sstevel@tonic-gate 	{ NULL }
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
2177c478bd9Sstevel@tonic-gate 	{ "cpc", "?[-v]", "Display contents of CPC context", cpc, cpc_help },
2187c478bd9Sstevel@tonic-gate 	{ NULL }
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
2227c478bd9Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, walkers
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)2267c478bd9Sstevel@tonic-gate _mdb_init(void)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	return (&modinfo);
2297c478bd9Sstevel@tonic-gate }
230