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 * Copyright 2019 Joyent, Inc.
27 */
28
29 #include <sys/mdb_modapi.h>
30 #include <sys/kcpc.h>
31 #include <sys/cpc_impl.h>
32
33 #define KCPC_HASH_BUCKETS (1l << KCPC_LOG2_HASH_BUCKETS)
34
35 /*
36 * Assume 64-bit kernel address max is 100000000000 - 1.
37 */
38 #ifdef _LP64
39 #define ADDR_WIDTH 11
40 #else
41 #define ADDR_WIDTH 8
42 #endif
43
44 struct cpc_ctx_aux {
45 uintptr_t cca_hash[KCPC_HASH_BUCKETS];
46 int cca_bucket;
47 };
48
49 /*ARGSUSED*/
50 static int
cpc(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)51 cpc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
52 {
53 kcpc_ctx_t ctx;
54 kcpc_set_t set;
55 kcpc_request_t *reqs;
56 uint64_t *data;
57 kcpc_attr_t *attr;
58 int i;
59 int j;
60 uint_t opt_v = FALSE;
61
62 if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL) !=
63 argc)
64 return (DCMD_USAGE);
65
66 if ((flags & DCMD_ADDRSPEC) == 0) {
67 /*
68 * We weren't given the address of any specific cpc ctx, so
69 * invoke the walker to find them all.
70 */
71 mdb_walk_dcmd("cpc_ctx", "cpc", argc, argv);
72 return (DCMD_OK);
73 }
74
75 if (mdb_vread(&ctx, sizeof (ctx), addr) == -1) {
76 mdb_warn("failed to read kcpc_ctx_t at %p", addr);
77 return (DCMD_ABORT);
78 }
79
80 if (mdb_vread(&set, sizeof (set), (uintptr_t)ctx.kc_set) == -1) {
81 mdb_warn("failed to read kcpc_set_t at %p", ctx.kc_set);
82 return (DCMD_ABORT);
83 }
84
85 reqs = mdb_alloc(set.ks_nreqs * sizeof (*reqs), UM_GC);
86 data = mdb_alloc(set.ks_nreqs * sizeof (*data), UM_GC);
87
88 if (mdb_vread(reqs, set.ks_nreqs * sizeof (*reqs),
89 (uintptr_t)set.ks_req) == -1) {
90 mdb_warn("failed to read requests at %p", set.ks_req);
91 return (DCMD_ABORT);
92 }
93
94 if (mdb_vread(data, set.ks_nreqs * sizeof (*data),
95 (uintptr_t)set.ks_data) == -1) {
96 mdb_warn("failed to read set data at %p", set.ks_data);
97 return (DCMD_ABORT);
98 }
99
100 if (DCMD_HDRSPEC(flags))
101 mdb_printf("N PIC NDX %16s FLG %16s %*s EVENT\n", "VAL",
102 "PRESET", ADDR_WIDTH, "CFG");
103 mdb_printf("-----------------------------------------------------------"
104 "---------------------\n");
105 if (opt_v)
106 mdb_printf("Set: %p\t%d requests. Flags = %x\n", ctx.kc_set,
107 set.ks_nreqs, set.ks_flags);
108
109 for (i = 0; i < set.ks_nreqs; i++) {
110 mdb_printf("%d %3d %3d %16llx %1s%1s%1s %16llx %8p %s\n", i,
111 reqs[i].kr_picnum, reqs[i].kr_index,
112 data[reqs[i].kr_index],
113 (reqs[i].kr_flags & CPC_OVF_NOTIFY_EMT) ? "O" : "",
114 (reqs[i].kr_flags & CPC_COUNT_USER) ? "U" : "",
115 (reqs[i].kr_flags & CPC_COUNT_SYSTEM) ? "S" : "",
116 reqs[i].kr_preset, reqs[i].kr_config,
117 reqs[i].kr_event);
118 if (opt_v == 0)
119 continue;
120 if (reqs[i].kr_nattrs > 0) {
121 attr = mdb_alloc(reqs[i].kr_nattrs * sizeof (*attr),
122 UM_GC);
123 if (mdb_vread(attr, reqs[i].kr_nattrs * sizeof (*attr),
124 (uintptr_t)reqs[i].kr_attr) == -1) {
125 mdb_warn("failed to read attributes at %p",
126 reqs[i].kr_attr);
127 return (DCMD_ABORT);
128 }
129 for (j = 0; j < reqs[i].kr_nattrs; j++)
130 mdb_printf("\t%s = %llx", attr[j].ka_name,
131 attr[j].ka_val);
132 mdb_printf("\n");
133 }
134 }
135
136 return (DCMD_OK);
137 }
138
139 static void
cpc_help(void)140 cpc_help(void)
141 {
142 mdb_printf("Displays the contents of the CPC context at the supplied "
143 "address. If no address is given, displays contents of all active "
144 "CPC contexts.\n");
145 mdb_printf("Flag codes: \n"
146 "O = overflow notify U = count user events "
147 "S = count system events\n");
148 }
149
150
151 /*
152 * Initialize the global walk by grabbing the hash table in the
153 * cpc module.
154 */
155 static int
cpc_ctx_walk_init(mdb_walk_state_t * wsp)156 cpc_ctx_walk_init(mdb_walk_state_t *wsp)
157 {
158 struct cpc_ctx_aux *cca;
159
160 if (wsp->walk_addr != 0) {
161 mdb_warn("only global cpc_ctx walk supported\n");
162 return (WALK_ERR);
163 }
164
165 cca = mdb_zalloc(sizeof (*cca), UM_SLEEP);
166
167 if (mdb_readsym(&cca->cca_hash, sizeof (cca->cca_hash),
168 "kcpc_ctx_list") == -1) {
169 mdb_warn("cannot read cpc_ctx hash table");
170 mdb_free(cca, sizeof (*cca));
171 return (WALK_ERR);
172 }
173
174 wsp->walk_data = cca;
175 wsp->walk_addr = 0;
176 return (WALK_NEXT);
177 }
178
179 static int
cpc_ctx_walk_step(mdb_walk_state_t * wsp)180 cpc_ctx_walk_step(mdb_walk_state_t *wsp)
181 {
182 int status;
183 kcpc_ctx_t ctx;
184 struct cpc_ctx_aux *cca = wsp->walk_data;
185
186 while (wsp->walk_addr == 0) {
187 if (cca->cca_bucket == KCPC_HASH_BUCKETS)
188 return (WALK_DONE);
189 wsp->walk_addr = cca->cca_hash[cca->cca_bucket++];
190 }
191
192 if (mdb_vread(&ctx, sizeof (ctx), wsp->walk_addr) == -1) {
193 mdb_warn("failed to read cpc_ctx at %p", wsp->walk_addr);
194 return (WALK_ERR);
195 }
196
197 status = wsp->walk_callback(wsp->walk_addr, &ctx,
198 wsp->walk_cbdata);
199
200 wsp->walk_addr = (uintptr_t)ctx.kc_next;
201 return (status);
202 }
203
204 static void
cpc_ctx_walk_fini(mdb_walk_state_t * wsp)205 cpc_ctx_walk_fini(mdb_walk_state_t *wsp)
206 {
207 mdb_free(wsp->walk_data, sizeof (struct cpc_ctx_aux));
208 }
209
210 static const mdb_walker_t walkers[] = {
211 { "cpc_ctx", "walk global list of cpc contexts",
212 cpc_ctx_walk_init, cpc_ctx_walk_step, cpc_ctx_walk_fini },
213 { NULL }
214 };
215
216 static const mdb_dcmd_t dcmds[] = {
217 { "cpc", "?[-v]", "Display contents of CPC context", cpc, cpc_help },
218 { NULL }
219 };
220
221 static const mdb_modinfo_t modinfo = {
222 MDB_API_VERSION, dcmds, walkers
223 };
224
225 const mdb_modinfo_t *
_mdb_init(void)226 _mdb_init(void)
227 {
228 return (&modinfo);
229 }
230