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 2004 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 /* 30*0Sstevel@tonic-gate * Generic memory walker, used by both the genunix and libumem dmods. 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h> 34*0Sstevel@tonic-gate #include <sys/sysmacros.h> 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include "kgrep.h" 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #define KGREP_FULL_MASK (~(uintmax_t)0) 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate typedef struct kgrep_data { 41*0Sstevel@tonic-gate uintmax_t kg_pattern; 42*0Sstevel@tonic-gate uintmax_t kg_mask; /* fancy only */ 43*0Sstevel@tonic-gate uintmax_t kg_dist; /* fancy only */ 44*0Sstevel@tonic-gate uintptr_t kg_minaddr; /* fancy only */ 45*0Sstevel@tonic-gate uintptr_t kg_maxaddr; /* fancy only */ 46*0Sstevel@tonic-gate void *kg_page; 47*0Sstevel@tonic-gate size_t kg_pagesize; 48*0Sstevel@tonic-gate char kg_cbtype; 49*0Sstevel@tonic-gate char kg_seen; 50*0Sstevel@tonic-gate } kgrep_data_t; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #define KG_BASE 0 53*0Sstevel@tonic-gate #define KG_VERBOSE 1 54*0Sstevel@tonic-gate #define KG_PIPE 2 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate static void 57*0Sstevel@tonic-gate kgrep_cb(uintptr_t addr, uintmax_t *val, int type) 58*0Sstevel@tonic-gate { 59*0Sstevel@tonic-gate switch (type) { 60*0Sstevel@tonic-gate case KG_BASE: 61*0Sstevel@tonic-gate default: 62*0Sstevel@tonic-gate mdb_printf("%p\n", addr); 63*0Sstevel@tonic-gate break; 64*0Sstevel@tonic-gate case KG_VERBOSE: 65*0Sstevel@tonic-gate mdb_printf("%p:\t%llx\n", addr, *val); 66*0Sstevel@tonic-gate break; 67*0Sstevel@tonic-gate case KG_PIPE: 68*0Sstevel@tonic-gate mdb_printf("%#lr\n", addr); 69*0Sstevel@tonic-gate break; 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate static int 74*0Sstevel@tonic-gate kgrep_range_basic(uintptr_t base, uintptr_t lim, void *kg_arg) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate kgrep_data_t *kg = kg_arg; 77*0Sstevel@tonic-gate size_t pagesize = kg->kg_pagesize; 78*0Sstevel@tonic-gate uintptr_t pattern = kg->kg_pattern; 79*0Sstevel@tonic-gate uintptr_t *page = kg->kg_page; 80*0Sstevel@tonic-gate uintptr_t *page_end = &page[pagesize / sizeof (uintptr_t)]; 81*0Sstevel@tonic-gate uintptr_t *pos; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate uintptr_t addr, offset; 84*0Sstevel@tonic-gate int seen = 0; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate /* 87*0Sstevel@tonic-gate * page-align everything, to simplify the loop 88*0Sstevel@tonic-gate */ 89*0Sstevel@tonic-gate base = P2ALIGN(base, pagesize); 90*0Sstevel@tonic-gate lim = P2ROUNDUP(lim, pagesize); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate for (addr = base; addr < lim; addr += pagesize) { 93*0Sstevel@tonic-gate if (mdb_vread(page, pagesize, addr) == -1) 94*0Sstevel@tonic-gate continue; 95*0Sstevel@tonic-gate seen = 1; 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate for (pos = page; pos < page_end; pos++) { 98*0Sstevel@tonic-gate if (*pos != pattern) 99*0Sstevel@tonic-gate continue; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate offset = (caddr_t)pos - (caddr_t)page; 102*0Sstevel@tonic-gate kgrep_cb(addr + offset, NULL, kg->kg_cbtype); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate if (seen) 106*0Sstevel@tonic-gate kg->kg_seen = 1; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate return (WALK_NEXT); 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate /* 112*0Sstevel@tonic-gate * Full-service template -- instantiated for each supported size. We support 113*0Sstevel@tonic-gate * the following options: 114*0Sstevel@tonic-gate * 115*0Sstevel@tonic-gate * addr in [minaddr, maxaddr), and 116*0Sstevel@tonic-gate * value in [pattern, pattern + dist) OR 117*0Sstevel@tonic-gate * mask matching: (value & mask) == (pattern & mask) 118*0Sstevel@tonic-gate */ 119*0Sstevel@tonic-gate #define KGREP_FANCY_TEMPLATE(kgrep_range_fancybits, uintbits_t) \ 120*0Sstevel@tonic-gate static int \ 121*0Sstevel@tonic-gate kgrep_range_fancybits(uintptr_t base, uintptr_t lim, void *kg_arg) \ 122*0Sstevel@tonic-gate { \ 123*0Sstevel@tonic-gate kgrep_data_t *kg = kg_arg; \ 124*0Sstevel@tonic-gate \ 125*0Sstevel@tonic-gate uintbits_t pattern = kg->kg_pattern; \ 126*0Sstevel@tonic-gate uintbits_t dist = kg->kg_dist; \ 127*0Sstevel@tonic-gate uintbits_t mask = kg->kg_mask; \ 128*0Sstevel@tonic-gate uintptr_t minaddr = kg->kg_minaddr; \ 129*0Sstevel@tonic-gate uintptr_t maxaddr = kg->kg_maxaddr; \ 130*0Sstevel@tonic-gate size_t pagesize = kg->kg_pagesize; \ 131*0Sstevel@tonic-gate uintbits_t *page = (uintbits_t *)kg->kg_page; \ 132*0Sstevel@tonic-gate uintbits_t *page_end; \ 133*0Sstevel@tonic-gate uintbits_t *pos; \ 134*0Sstevel@tonic-gate uintbits_t cur; \ 135*0Sstevel@tonic-gate uintmax_t out; \ 136*0Sstevel@tonic-gate \ 137*0Sstevel@tonic-gate uintptr_t addr, size, offset; \ 138*0Sstevel@tonic-gate int seen = 0; \ 139*0Sstevel@tonic-gate \ 140*0Sstevel@tonic-gate base = P2ROUNDUP(MAX(base, minaddr), sizeof (uintbits_t)); \ 141*0Sstevel@tonic-gate \ 142*0Sstevel@tonic-gate if (maxaddr != 0 && lim > maxaddr) \ 143*0Sstevel@tonic-gate lim = maxaddr; \ 144*0Sstevel@tonic-gate \ 145*0Sstevel@tonic-gate for (addr = base; addr < lim; addr += size) { \ 146*0Sstevel@tonic-gate /* P2END(...) computes the next page boundry */ \ 147*0Sstevel@tonic-gate size = MIN(lim, P2END(addr, pagesize)) - addr; \ 148*0Sstevel@tonic-gate \ 149*0Sstevel@tonic-gate if (mdb_vread(page, size, addr) == -1) \ 150*0Sstevel@tonic-gate continue; \ 151*0Sstevel@tonic-gate \ 152*0Sstevel@tonic-gate seen = 1; \ 153*0Sstevel@tonic-gate \ 154*0Sstevel@tonic-gate page_end = &page[size / sizeof (uintbits_t)]; \ 155*0Sstevel@tonic-gate for (pos = page; pos < page_end; pos++) { \ 156*0Sstevel@tonic-gate cur = *pos; \ 157*0Sstevel@tonic-gate \ 158*0Sstevel@tonic-gate if (((cur ^ pattern) & mask) != 0 && \ 159*0Sstevel@tonic-gate (cur - pattern) >= dist) \ 160*0Sstevel@tonic-gate continue; \ 161*0Sstevel@tonic-gate \ 162*0Sstevel@tonic-gate out = cur; \ 163*0Sstevel@tonic-gate offset = (caddr_t)pos - (caddr_t)page; \ 164*0Sstevel@tonic-gate kgrep_cb(addr + offset, &out, kg->kg_cbtype); \ 165*0Sstevel@tonic-gate } \ 166*0Sstevel@tonic-gate } \ 167*0Sstevel@tonic-gate if (seen) \ 168*0Sstevel@tonic-gate kg->kg_seen = 1; \ 169*0Sstevel@tonic-gate \ 170*0Sstevel@tonic-gate return (WALK_NEXT); \ 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy8, uint8_t) 174*0Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy16, uint16_t) 175*0Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy32, uint32_t) 176*0Sstevel@tonic-gate KGREP_FANCY_TEMPLATE(kgrep_range_fancy64, uint64_t) 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate #undef KGREP_FANCY_TEMPLATE 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /*ARGSUSED*/ 181*0Sstevel@tonic-gate int 182*0Sstevel@tonic-gate kgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 183*0Sstevel@tonic-gate { 184*0Sstevel@tonic-gate uintmax_t pattern = mdb_get_dot(); 185*0Sstevel@tonic-gate uintmax_t mask = KGREP_FULL_MASK; 186*0Sstevel@tonic-gate uintmax_t invmask = 0; 187*0Sstevel@tonic-gate uintmax_t dist = 0; 188*0Sstevel@tonic-gate uintptr_t size = sizeof (uintptr_t); 189*0Sstevel@tonic-gate uintptr_t minaddr = 0; 190*0Sstevel@tonic-gate uintptr_t maxaddr = 0; 191*0Sstevel@tonic-gate size_t pagesize = kgrep_subr_pagesize(); 192*0Sstevel@tonic-gate int verbose = 0; 193*0Sstevel@tonic-gate int ret; 194*0Sstevel@tonic-gate int args = 0; 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate kgrep_cb_func *func; 197*0Sstevel@tonic-gate kgrep_data_t kg; 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate uintmax_t size_mask; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate if (mdb_getopts(argc, argv, 202*0Sstevel@tonic-gate 'a', MDB_OPT_UINTPTR, &minaddr, 203*0Sstevel@tonic-gate 'A', MDB_OPT_UINTPTR, &maxaddr, 204*0Sstevel@tonic-gate 'd', MDB_OPT_UINT64, &dist, 205*0Sstevel@tonic-gate 'm', MDB_OPT_UINT64, &mask, 206*0Sstevel@tonic-gate 'M', MDB_OPT_UINT64, &invmask, 207*0Sstevel@tonic-gate 's', MDB_OPT_UINTPTR, &size, 208*0Sstevel@tonic-gate 'v', MDB_OPT_SETBITS, B_TRUE, &verbose, NULL) != argc) 209*0Sstevel@tonic-gate return (DCMD_USAGE); 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate if (invmask != 0) 212*0Sstevel@tonic-gate args++; 213*0Sstevel@tonic-gate if (mask != KGREP_FULL_MASK) 214*0Sstevel@tonic-gate args++; 215*0Sstevel@tonic-gate if (dist != 0) 216*0Sstevel@tonic-gate args++; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if (args > 1) { 219*0Sstevel@tonic-gate mdb_warn("only one of -d, -m and -M may be specified\n"); 220*0Sstevel@tonic-gate return (DCMD_USAGE); 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) 224*0Sstevel@tonic-gate return (DCMD_USAGE); 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate if (invmask != 0) 227*0Sstevel@tonic-gate mask = ~invmask; 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate if (pattern & ~mask) 230*0Sstevel@tonic-gate mdb_warn("warning: pattern does not match mask\n"); 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if (size > sizeof (uintmax_t)) { 233*0Sstevel@tonic-gate mdb_warn("sizes greater than %d not supported\n", 234*0Sstevel@tonic-gate sizeof (uintmax_t)); 235*0Sstevel@tonic-gate return (DCMD_ERR); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate if (size == 0 || (size & (size - 1)) != 0) { 239*0Sstevel@tonic-gate mdb_warn("size must be a power of 2\n"); 240*0Sstevel@tonic-gate return (DCMD_ERR); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate if (size == sizeof (uintmax_t)) 244*0Sstevel@tonic-gate size_mask = KGREP_FULL_MASK; 245*0Sstevel@tonic-gate else 246*0Sstevel@tonic-gate size_mask = (1ULL << (size * NBBY)) - 1ULL; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate if (pattern & ~size_mask) 249*0Sstevel@tonic-gate mdb_warn("warning: pattern %llx overflows requested size " 250*0Sstevel@tonic-gate "%d (max: %llx)\n", 251*0Sstevel@tonic-gate pattern, size, size_mask); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if (dist > 0 && 254*0Sstevel@tonic-gate ((dist & ~size_mask) || size_mask + 1 - dist < pattern)) { 255*0Sstevel@tonic-gate mdb_warn("pattern %llx + distance %llx overflows size\n" 256*0Sstevel@tonic-gate "%d (max: %llx)\n", pattern, dist, size, size_mask); 257*0Sstevel@tonic-gate return (DCMD_ERR); 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate /* 261*0Sstevel@tonic-gate * All arguments have now been validated. 262*0Sstevel@tonic-gate */ 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate (void) memset(&kg, '\0', sizeof (kg)); 265*0Sstevel@tonic-gate kg.kg_page = mdb_alloc(pagesize, UM_SLEEP | UM_GC); 266*0Sstevel@tonic-gate kg.kg_pagesize = pagesize; 267*0Sstevel@tonic-gate kg.kg_pattern = pattern; 268*0Sstevel@tonic-gate kg.kg_mask = mask; 269*0Sstevel@tonic-gate kg.kg_dist = dist; 270*0Sstevel@tonic-gate kg.kg_minaddr = minaddr; 271*0Sstevel@tonic-gate kg.kg_maxaddr = maxaddr; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate if (flags & DCMD_PIPE_OUT) { 274*0Sstevel@tonic-gate verbose = 0; 275*0Sstevel@tonic-gate kg.kg_cbtype = KG_PIPE; 276*0Sstevel@tonic-gate } else if (verbose) { 277*0Sstevel@tonic-gate kg.kg_cbtype = KG_VERBOSE; 278*0Sstevel@tonic-gate } else { 279*0Sstevel@tonic-gate kg.kg_cbtype = KG_BASE; 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate /* 283*0Sstevel@tonic-gate * kgrep_range_basic handles the common case (no arguments) 284*0Sstevel@tonic-gate * with dispatch. 285*0Sstevel@tonic-gate */ 286*0Sstevel@tonic-gate if (size == sizeof (uintptr_t) && !verbose && mask == KGREP_FULL_MASK && 287*0Sstevel@tonic-gate dist == 0 && minaddr == 0 && maxaddr == 0) 288*0Sstevel@tonic-gate func = kgrep_range_basic; 289*0Sstevel@tonic-gate else { 290*0Sstevel@tonic-gate switch (size) { 291*0Sstevel@tonic-gate case 1: 292*0Sstevel@tonic-gate func = kgrep_range_fancy8; 293*0Sstevel@tonic-gate break; 294*0Sstevel@tonic-gate case 2: 295*0Sstevel@tonic-gate func = kgrep_range_fancy16; 296*0Sstevel@tonic-gate break; 297*0Sstevel@tonic-gate case 4: 298*0Sstevel@tonic-gate func = kgrep_range_fancy32; 299*0Sstevel@tonic-gate break; 300*0Sstevel@tonic-gate case 8: 301*0Sstevel@tonic-gate func = kgrep_range_fancy64; 302*0Sstevel@tonic-gate break; 303*0Sstevel@tonic-gate default: 304*0Sstevel@tonic-gate mdb_warn("can't happen: non-recognized kgrep size\n"); 305*0Sstevel@tonic-gate return (DCMD_ERR); 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate /* 310*0Sstevel@tonic-gate * Invoke the target, which should invoke func(start, end, &kg) for 311*0Sstevel@tonic-gate * every range [start, end) of vaddrs which might have backing. 312*0Sstevel@tonic-gate * Both start and end must be multiples of kgrep_subr_pagesize(). 313*0Sstevel@tonic-gate */ 314*0Sstevel@tonic-gate ret = kgrep_subr(func, &kg); 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate if (ret == DCMD_OK && !kg.kg_seen) 317*0Sstevel@tonic-gate mdb_warn("warning: nothing searched\n"); 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate return (ret); 320*0Sstevel@tonic-gate } 321