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 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2025 Oxide Computer Company
26 */
27
28 #include <mdb/mdb_modapi.h>
29 #include <sys/buf.h>
30 #include <sys/var.h>
31 #include <vm/page.h>
32
33 #include "bio.h"
34
35 typedef struct buf_walk {
36 uintptr_t bw_hbufbase; /* Base address of hbuf buckets */
37 struct hbuf *bw_hbufs; /* Snapshot of hbuf buckets */
38 size_t bw_nhbufs; /* Number of hbuf buckets */
39 size_t bw_hbufi; /* Current hbuf index */
40 buf_t *bw_bufp; /* Current buffer */
41 } buf_walk_t;
42
43 int
buf_walk_init(mdb_walk_state_t * wsp)44 buf_walk_init(mdb_walk_state_t *wsp)
45 {
46 struct hbuf *hbufs;
47 struct var v;
48
49 uintptr_t hbuf_addr;
50 size_t nbytes;
51
52 buf_walk_t *bwp;
53
54 if (wsp->walk_addr != 0) {
55 mdb_warn("only global buf walk supported\n");
56 return (WALK_ERR);
57 }
58
59 if (mdb_readvar(&v, "v") == -1) {
60 mdb_warn("failed to read var struct");
61 return (WALK_ERR);
62 }
63
64 if (mdb_readvar(&hbuf_addr, "hbuf") == -1) {
65 mdb_warn("failed to read hbuf pointer");
66 return (WALK_ERR);
67 }
68
69 nbytes = sizeof (struct hbuf) * v.v_hbuf;
70 hbufs = mdb_alloc(nbytes, UM_SLEEP);
71
72 if (mdb_vread(hbufs, nbytes, hbuf_addr) != nbytes) {
73 mdb_warn("failed to read hbufs");
74 mdb_free(hbufs, nbytes);
75 return (WALK_ERR);
76 }
77
78 bwp = mdb_alloc(sizeof (buf_walk_t), UM_SLEEP);
79
80 bwp->bw_hbufbase = hbuf_addr;
81 bwp->bw_hbufs = hbufs;
82 bwp->bw_nhbufs = v.v_hbuf;
83 bwp->bw_hbufi = 0;
84 bwp->bw_bufp = mdb_alloc(sizeof (buf_t), UM_SLEEP);
85
86 wsp->walk_addr = (uintptr_t)hbufs[0].b_forw;
87 wsp->walk_data = bwp;
88
89 return (WALK_NEXT);
90 }
91
92 int
buf_walk_step(mdb_walk_state_t * wsp)93 buf_walk_step(mdb_walk_state_t *wsp)
94 {
95 buf_walk_t *bwp = wsp->walk_data;
96 uintptr_t addr;
97
98 /*
99 * If the next buf_t address we want is NULL or points back at the
100 * hbuf itself, advance to the next hash bucket. When we reach
101 * bw_nhbufs, we're done.
102 */
103 while (wsp->walk_addr == 0 || wsp->walk_addr == (bwp->bw_hbufbase +
104 bwp->bw_hbufi * sizeof (struct hbuf))) {
105
106 if (++bwp->bw_hbufi == bwp->bw_nhbufs)
107 return (WALK_DONE);
108
109 wsp->walk_addr = (uintptr_t)
110 bwp->bw_hbufs[bwp->bw_hbufi].b_forw;
111 }
112
113 /*
114 * When we have a buf_t address, read the buffer and invoke our
115 * walk callback. We keep the next buf_t address in wsp->walk_addr.
116 */
117 addr = wsp->walk_addr;
118 (void) mdb_vread(bwp->bw_bufp, sizeof (buf_t), addr);
119 wsp->walk_addr = (uintptr_t)bwp->bw_bufp->b_forw;
120
121 return (wsp->walk_callback(addr, bwp->bw_bufp, wsp->walk_cbdata));
122 }
123
124 void
buf_walk_fini(mdb_walk_state_t * wsp)125 buf_walk_fini(mdb_walk_state_t *wsp)
126 {
127 buf_walk_t *bwp = wsp->walk_data;
128
129 mdb_free(bwp->bw_hbufs, sizeof (struct hbuf) * bwp->bw_nhbufs);
130 mdb_free(bwp->bw_bufp, sizeof (buf_t));
131 mdb_free(bwp, sizeof (buf_walk_t));
132 }
133
134 /*ARGSUSED*/
135 int
bufpagefind(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)136 bufpagefind(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
137 {
138 uintptr_t b_addr = addr;
139 uintptr_t arg;
140
141 page_t p;
142 buf_t b;
143
144 if (argc != 1)
145 return (DCMD_USAGE);
146
147 arg = (uintptr_t)mdb_argtoull(argv);
148
149 if (mdb_vread(&b, sizeof (buf_t), b_addr) == -1)
150 return (DCMD_ERR);
151
152 for (addr = (uintptr_t)b.b_pages; addr != 0;
153 addr = (uintptr_t)p.p_next) {
154
155 if (addr == arg) {
156 mdb_printf("buf %p has page %p on b_pages list\n",
157 b_addr, addr);
158 break;
159 }
160
161 if (mdb_vread(&p, sizeof (page_t), addr) == -1)
162 return (DCMD_ERR);
163 }
164
165 return (DCMD_OK);
166 }
167