1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
30*0Sstevel@tonic-gate #include <sys/buf.h>
31*0Sstevel@tonic-gate #include <sys/var.h>
32*0Sstevel@tonic-gate #include <vm/page.h>
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include "bio.h"
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate typedef struct buf_walk {
37*0Sstevel@tonic-gate uintptr_t bw_hbufbase; /* Base address of hbuf buckets */
38*0Sstevel@tonic-gate struct hbuf *bw_hbufs; /* Snapshot of hbuf buckets */
39*0Sstevel@tonic-gate size_t bw_nhbufs; /* Number of hbuf buckets */
40*0Sstevel@tonic-gate size_t bw_hbufi; /* Current hbuf index */
41*0Sstevel@tonic-gate buf_t *bw_bufp; /* Current buffer */
42*0Sstevel@tonic-gate } buf_walk_t;
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate int
buf_walk_init(mdb_walk_state_t * wsp)45*0Sstevel@tonic-gate buf_walk_init(mdb_walk_state_t *wsp)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate struct hbuf *hbufs;
48*0Sstevel@tonic-gate struct var v;
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate uintptr_t hbuf_addr;
51*0Sstevel@tonic-gate size_t nbytes;
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate buf_walk_t *bwp;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate if (wsp->walk_addr != NULL) {
56*0Sstevel@tonic-gate mdb_warn("only global buf walk supported\n");
57*0Sstevel@tonic-gate return (WALK_ERR);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate if (mdb_readvar(&v, "v") == -1) {
61*0Sstevel@tonic-gate mdb_warn("failed to read var struct");
62*0Sstevel@tonic-gate return (WALK_ERR);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate if (mdb_readvar(&hbuf_addr, "hbuf") == -1) {
66*0Sstevel@tonic-gate mdb_warn("failed to read hbuf pointer");
67*0Sstevel@tonic-gate return (WALK_ERR);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate nbytes = sizeof (struct hbuf) * v.v_hbuf;
71*0Sstevel@tonic-gate hbufs = mdb_alloc(nbytes, UM_SLEEP);
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate if (mdb_vread(hbufs, nbytes, hbuf_addr) != nbytes) {
74*0Sstevel@tonic-gate mdb_warn("failed to read hbufs");
75*0Sstevel@tonic-gate mdb_free(hbufs, nbytes);
76*0Sstevel@tonic-gate return (WALK_ERR);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate bwp = mdb_alloc(sizeof (buf_walk_t), UM_SLEEP);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate bwp->bw_hbufbase = hbuf_addr;
82*0Sstevel@tonic-gate bwp->bw_hbufs = hbufs;
83*0Sstevel@tonic-gate bwp->bw_nhbufs = v.v_hbuf;
84*0Sstevel@tonic-gate bwp->bw_hbufi = 0;
85*0Sstevel@tonic-gate bwp->bw_bufp = mdb_alloc(sizeof (buf_t), UM_SLEEP);
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)hbufs[0].b_forw;
88*0Sstevel@tonic-gate wsp->walk_data = bwp;
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate return (WALK_NEXT);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate int
buf_walk_step(mdb_walk_state_t * wsp)94*0Sstevel@tonic-gate buf_walk_step(mdb_walk_state_t *wsp)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate buf_walk_t *bwp = wsp->walk_data;
97*0Sstevel@tonic-gate uintptr_t addr;
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate * If the next buf_t address we want is NULL or points back at the
101*0Sstevel@tonic-gate * hbuf itself, advance to the next hash bucket. When we reach
102*0Sstevel@tonic-gate * bw_nhbufs, we're done.
103*0Sstevel@tonic-gate */
104*0Sstevel@tonic-gate while (wsp->walk_addr == NULL || wsp->walk_addr == (bwp->bw_hbufbase +
105*0Sstevel@tonic-gate bwp->bw_hbufi * sizeof (struct hbuf))) {
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate if (++bwp->bw_hbufi == bwp->bw_nhbufs)
108*0Sstevel@tonic-gate return (WALK_DONE);
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)
111*0Sstevel@tonic-gate bwp->bw_hbufs[bwp->bw_hbufi].b_forw;
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * When we have a buf_t address, read the buffer and invoke our
116*0Sstevel@tonic-gate * walk callback. We keep the next buf_t address in wsp->walk_addr.
117*0Sstevel@tonic-gate */
118*0Sstevel@tonic-gate addr = wsp->walk_addr;
119*0Sstevel@tonic-gate (void) mdb_vread(bwp->bw_bufp, sizeof (buf_t), addr);
120*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)bwp->bw_bufp->b_forw;
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate return (wsp->walk_callback(addr, bwp->bw_bufp, wsp->walk_cbdata));
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate void
buf_walk_fini(mdb_walk_state_t * wsp)126*0Sstevel@tonic-gate buf_walk_fini(mdb_walk_state_t *wsp)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate buf_walk_t *bwp = wsp->walk_data;
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate mdb_free(bwp->bw_hbufs, sizeof (struct hbuf) * bwp->bw_nhbufs);
131*0Sstevel@tonic-gate mdb_free(bwp->bw_bufp, sizeof (buf_t));
132*0Sstevel@tonic-gate mdb_free(bwp, sizeof (buf_walk_t));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /*ARGSUSED*/
136*0Sstevel@tonic-gate int
bufpagefind(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)137*0Sstevel@tonic-gate bufpagefind(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate uintptr_t b_addr = addr;
140*0Sstevel@tonic-gate uintptr_t arg;
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate page_t p;
143*0Sstevel@tonic-gate buf_t b;
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate if (argc != 1)
146*0Sstevel@tonic-gate return (DCMD_USAGE);
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate if (argv->a_type == MDB_TYPE_IMMEDIATE)
149*0Sstevel@tonic-gate arg = (uintptr_t)argv->a_un.a_val;
150*0Sstevel@tonic-gate else
151*0Sstevel@tonic-gate arg = (uintptr_t)mdb_strtoull(argv->a_un.a_str);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate if (mdb_vread(&b, sizeof (buf_t), b_addr) == -1)
154*0Sstevel@tonic-gate return (DCMD_ERR);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate for (addr = (uintptr_t)b.b_pages; addr != NULL;
157*0Sstevel@tonic-gate addr = (uintptr_t)p.p_next) {
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate if (addr == arg) {
160*0Sstevel@tonic-gate mdb_printf("buf %p has page %p on b_pages list\n",
161*0Sstevel@tonic-gate b_addr, addr);
162*0Sstevel@tonic-gate break;
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate if (mdb_vread(&p, sizeof (page_t), addr) == -1)
166*0Sstevel@tonic-gate return (DCMD_ERR);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate return (DCMD_OK);
170*0Sstevel@tonic-gate }
171