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
5*a85a6733Sjosephb  * Common Development and Distribution License (the "License").
6*a85a6733Sjosephb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a85a6733Sjosephb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This part of the file contains the mdb support for dcmds:
307c478bd9Sstevel@tonic-gate  *	::memseg_list
317c478bd9Sstevel@tonic-gate  *	::page_num2pp
327c478bd9Sstevel@tonic-gate  * and walkers for:
337c478bd9Sstevel@tonic-gate  *	memseg - a memseg list walker for ::memseg_list
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
397c478bd9Sstevel@tonic-gate #include <sys/controlregs.h>
407c478bd9Sstevel@tonic-gate #include <vm/as.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
437c478bd9Sstevel@tonic-gate #include <mdb/mdb_target.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <vm/page.h>
467c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate struct pfn2pp {
497c478bd9Sstevel@tonic-gate 	pfn_t pfn;
507c478bd9Sstevel@tonic-gate 	page_t *pp;
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static int do_va2pfn(uintptr_t, struct as *, int, physaddr_t *);
547c478bd9Sstevel@tonic-gate static void get_mmu(void);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate int
577c478bd9Sstevel@tonic-gate platform_vtop(uintptr_t addr, struct as *asp, physaddr_t *pap)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	if (asp == NULL)
607c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/*
637c478bd9Sstevel@tonic-gate 	 * The kernel has to at least have made it thru mmu_init()
647c478bd9Sstevel@tonic-gate 	 */
657c478bd9Sstevel@tonic-gate 	get_mmu();
667c478bd9Sstevel@tonic-gate 	if (mmu.num_level == 0)
677c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	return (do_va2pfn(addr, asp, 0, pap));
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
747c478bd9Sstevel@tonic-gate int
757c478bd9Sstevel@tonic-gate page_num2pp_cb(uintptr_t addr, void *ignored, uintptr_t *data)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	struct memseg ms, *msp = &ms;
787c478bd9Sstevel@tonic-gate 	struct pfn2pp *p = (struct pfn2pp *)data;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (mdb_vread(msp, sizeof (struct memseg), addr) == -1) {
817c478bd9Sstevel@tonic-gate 		mdb_warn("can't read memseg at %#lx", addr);
827c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (p->pfn >= msp->pages_base && p->pfn < msp->pages_end) {
867c478bd9Sstevel@tonic-gate 		p->pp = msp->pages + (p->pfn - msp->pages_base);
877c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * ::page_num2pp dcmd
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
977c478bd9Sstevel@tonic-gate int
987c478bd9Sstevel@tonic-gate page_num2pp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	struct pfn2pp pfn2pp;
1017c478bd9Sstevel@tonic-gate 	page_t page;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0) {
1047c478bd9Sstevel@tonic-gate 		mdb_warn("page frame number missing\n");
1057c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	pfn2pp.pfn = (pfn_t)addr;
1097c478bd9Sstevel@tonic-gate 	pfn2pp.pp = NULL;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (mdb_walk("memseg", (mdb_walk_cb_t)page_num2pp_cb,
1127c478bd9Sstevel@tonic-gate 	    (void *)&pfn2pp) == -1) {
1137c478bd9Sstevel@tonic-gate 		mdb_warn("can't walk memseg");
1147c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (pfn2pp.pp == NULL)
1187c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	mdb_printf("%x has page at %p\n", pfn2pp.pfn, pfn2pp.pp);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (mdb_vread(&page, sizeof (page_t),
1237c478bd9Sstevel@tonic-gate 	    (uintptr_t)pfn2pp.pp) == -1) {
1247c478bd9Sstevel@tonic-gate 		mdb_warn("can't read page at %p", &page);
1257c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (page.p_pagenum != pfn2pp.pfn) {
1297c478bd9Sstevel@tonic-gate 		mdb_warn("WARNING! Found page structure contains "
1307c478bd9Sstevel@tonic-gate 			"different pagenumber %x\n", page.p_pagenum);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * ::memseg_list dcmd and walker to implement it.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1447c478bd9Sstevel@tonic-gate int
1457c478bd9Sstevel@tonic-gate memseg_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	struct memseg ms;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
1507c478bd9Sstevel@tonic-gate 		if (mdb_pwalk_dcmd("memseg", "memseg_list",
1517c478bd9Sstevel@tonic-gate 		    0, NULL, 0) == -1) {
1527c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk memseg");
1537c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
1597c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %?s %?s %?s%</u>\n", "ADDR",
1607c478bd9Sstevel@tonic-gate 			"PAGES", "EPAGES", "BASE", "END");
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ms, sizeof (struct memseg), addr) == -1) {
1637c478bd9Sstevel@tonic-gate 		mdb_warn("can't read memseg at %#lx", addr);
1647c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	mdb_printf("%0?lx %0?lx %0?lx %0?lx %0?lx\n", addr,
1687c478bd9Sstevel@tonic-gate 		ms.pages, ms.epages, ms.pages_base, ms.pages_end);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * walk the memseg structures
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate int
1777c478bd9Sstevel@tonic-gate memseg_walk_init(mdb_walk_state_t *wsp)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
1807c478bd9Sstevel@tonic-gate 		mdb_warn("memseg only supports global walks\n");
1817c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&wsp->walk_addr, "memsegs") == -1) {
1857c478bd9Sstevel@tonic-gate 		mdb_warn("symbol 'memsegs' not found");
1867c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (struct memseg), UM_SLEEP);
1907c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
1957c478bd9Sstevel@tonic-gate memseg_walk_step(mdb_walk_state_t *wsp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	int status;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == 0) {
2007c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data, sizeof (struct memseg),
2047c478bd9Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
2057c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read struct memseg at %p", wsp->walk_addr);
2067c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
2107c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)(((struct memseg *)wsp->walk_data)->next);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (status);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate void
2187c478bd9Sstevel@tonic-gate memseg_walk_fini(mdb_walk_state_t *wsp)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (struct memseg));
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * HAT related dcmds:
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * ::pte [-p XXXXXXXX] [-l 0/1/2/3]
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * dcmd that interprets the -p argument as a page table entry and
2297c478bd9Sstevel@tonic-gate  * prints it in more human readable form. The PTE is assumed to be in
2307c478bd9Sstevel@tonic-gate  * a level 0 page table, unless -l specifies another level.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * ::vatopfn [-v] [-a as]
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * Given a virtual address, returns the PFN, if any, mapped at the address.
2357c478bd9Sstevel@tonic-gate  * -v shows the intermediate htable/page table entries used to resolve the
2367c478bd9Sstevel@tonic-gate  * mapping. By default the virtual address is assumed to be in the kernel's
2377c478bd9Sstevel@tonic-gate  * address space.  -a is used to specify a different address space.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate struct hat *khat;		/* value of kas.a_hat */
2417c478bd9Sstevel@tonic-gate struct hat_mmu_info mmu;
2427c478bd9Sstevel@tonic-gate uintptr_t kernelbase;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * read mmu parameters from kernel
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate static void
2487c478bd9Sstevel@tonic-gate get_mmu(void)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	struct as kas;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (mmu.num_level != 0)
2537c478bd9Sstevel@tonic-gate 		return;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (mdb_readsym(&mmu, sizeof (mmu), "mmu") == -1)
2567c478bd9Sstevel@tonic-gate 		mdb_warn("Can't use HAT information before mmu_init()\n");
2577c478bd9Sstevel@tonic-gate 	if (mdb_readsym(&kas, sizeof (kas), "kas") == -1)
2587c478bd9Sstevel@tonic-gate 		mdb_warn("Couldn't find kas - kernel's struct as\n");
2597c478bd9Sstevel@tonic-gate 	if (mdb_readsym(&kernelbase, sizeof (kernelbase), "kernelbase") == -1)
2607c478bd9Sstevel@tonic-gate 		mdb_warn("Couldn't find kernelbase\n");
2617c478bd9Sstevel@tonic-gate 	khat = kas.a_hat;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * Print a PTE in more human friendly way. The PTE is assumed to be in
2667c478bd9Sstevel@tonic-gate  * a level 0 page table, unless -l specifies another level.
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  * The PTE value can be specified as the -p option, since on a 32 bit kernel
2697c478bd9Sstevel@tonic-gate  * with PAE running it's larger than a uintptr_t.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate static int
2727c478bd9Sstevel@tonic-gate do_pte_dcmd(int level, uint64_t pte)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	static char *attr[] = {
2757c478bd9Sstevel@tonic-gate 	    "wrback", "wrthru", "uncached", "uncached",
2767c478bd9Sstevel@tonic-gate 	    "wrback", "wrthru", "wrcombine", "uncached"};
2777c478bd9Sstevel@tonic-gate 	int pat_index = 0;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	mdb_printf("PTE=%llx: ", pte);
2807c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, mmu.pt_nx))
2817c478bd9Sstevel@tonic-gate 		mdb_printf("noexec ");
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	mdb_printf("page=0x%llx ", PTE2PFN(pte, level));
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_NOCONSIST))
2867c478bd9Sstevel@tonic-gate 		mdb_printf("noconsist ");
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_NOSYNC))
2897c478bd9Sstevel@tonic-gate 		mdb_printf("nosync ");
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, mmu.pt_global))
2927c478bd9Sstevel@tonic-gate 		mdb_printf("global ");
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (level > 0 && PTE_GET(pte, PT_PAGESIZE))
2957c478bd9Sstevel@tonic-gate 		mdb_printf("largepage ");
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (level > 0 && PTE_GET(pte, PT_MOD))
2987c478bd9Sstevel@tonic-gate 		mdb_printf("mod ");
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (level > 0 && PTE_GET(pte, PT_REF))
3017c478bd9Sstevel@tonic-gate 		mdb_printf("ref ");
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_USER))
3047c478bd9Sstevel@tonic-gate 		mdb_printf("user ");
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_WRITABLE))
3077c478bd9Sstevel@tonic-gate 		mdb_printf("write ");
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Report non-standard cacheability
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	pat_index = 0;
3137c478bd9Sstevel@tonic-gate 	if (level > 0) {
3147c478bd9Sstevel@tonic-gate 		if (PTE_GET(pte, PT_PAGESIZE) && PTE_GET(pte, PT_PAT_LARGE))
3157c478bd9Sstevel@tonic-gate 			pat_index += 4;
3167c478bd9Sstevel@tonic-gate 	} else {
3177c478bd9Sstevel@tonic-gate 		if (PTE_GET(pte, PT_PAT_4K))
3187c478bd9Sstevel@tonic-gate 			pat_index += 4;
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_NOCACHE))
3227c478bd9Sstevel@tonic-gate 		pat_index += 2;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_WRITETHRU))
3257c478bd9Sstevel@tonic-gate 		pat_index += 1;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (pat_index != 0)
3287c478bd9Sstevel@tonic-gate 		mdb_printf("%s", attr[pat_index]);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_VALID) == 0)
3317c478bd9Sstevel@tonic-gate 		mdb_printf(" !VALID ");
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	mdb_printf("\n");
3347c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Print a PTE in more human friendly way. The PTE is assumed to be in
3397c478bd9Sstevel@tonic-gate  * a level 0 page table, unless -l specifies another level.
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  * The PTE value can be specified as the -p option, since on a 32 bit kernel
3427c478bd9Sstevel@tonic-gate  * with PAE running it's larger than a uintptr_t.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3457c478bd9Sstevel@tonic-gate int
3467c478bd9Sstevel@tonic-gate pte_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	int level = 0;
3497c478bd9Sstevel@tonic-gate 	uint64_t pte = 0;
3507c478bd9Sstevel@tonic-gate 	char *level_str = NULL;
3517c478bd9Sstevel@tonic-gate 	char *pte_str = NULL;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/*
3547c478bd9Sstevel@tonic-gate 	 * The kernel has to at least have made it thru mmu_init()
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	get_mmu();
3577c478bd9Sstevel@tonic-gate 	if (mmu.num_level == 0)
3587c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
3617c478bd9Sstevel@tonic-gate 	    'p', MDB_OPT_STR, &pte_str,
3627c478bd9Sstevel@tonic-gate 	    'l', MDB_OPT_STR, &level_str) != argc)
3637c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * parse the PTE to decode, if it's 0, we don't do anything
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (pte_str != NULL) {
3697c478bd9Sstevel@tonic-gate 		pte = mdb_strtoull(pte_str);
3707c478bd9Sstevel@tonic-gate 	} else {
3717c478bd9Sstevel@tonic-gate 		if ((flags & DCMD_ADDRSPEC) == 0)
3727c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
3737c478bd9Sstevel@tonic-gate 		pte = addr;
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	if (pte == 0)
3767c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * parse the level if supplied
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if (level_str != NULL) {
3827c478bd9Sstevel@tonic-gate 		level = mdb_strtoull(level_str);
3837c478bd9Sstevel@tonic-gate 		if (level < 0 || level > mmu.max_level)
3847c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	return (do_pte_dcmd(level, pte));
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static int
3917c478bd9Sstevel@tonic-gate do_va2pfn(uintptr_t addr, struct as *asp, int print_level, physaddr_t *pap)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	struct as as;
3947c478bd9Sstevel@tonic-gate 	struct hat *hatp;
3957c478bd9Sstevel@tonic-gate 	struct hat hat;
3967c478bd9Sstevel@tonic-gate 	htable_t *ht;
3977c478bd9Sstevel@tonic-gate 	htable_t htable;
3987c478bd9Sstevel@tonic-gate 	uintptr_t base;
3997c478bd9Sstevel@tonic-gate 	int h;
4007c478bd9Sstevel@tonic-gate 	int level;
4017c478bd9Sstevel@tonic-gate 	int found = 0;
4027c478bd9Sstevel@tonic-gate 	x86pte_t pte;
4037c478bd9Sstevel@tonic-gate 	x86pte_t buf;
4047c478bd9Sstevel@tonic-gate 	x86pte32_t *pte32 = (x86pte32_t *)&buf;
4057c478bd9Sstevel@tonic-gate 	physaddr_t paddr;
4067c478bd9Sstevel@tonic-gate 	size_t len;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (asp != NULL) {
4097c478bd9Sstevel@tonic-gate 		if (mdb_vread(&as, sizeof (as), (uintptr_t)asp) == -1) {
4107c478bd9Sstevel@tonic-gate 			mdb_warn("Couldn't read struct as\n");
4117c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		hatp = as.a_hat;
4147c478bd9Sstevel@tonic-gate 	} else {
4157c478bd9Sstevel@tonic-gate 		hatp = khat;
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * read the hat and its hash table
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (mdb_vread(&hat, sizeof (hat), (uintptr_t)hatp) == -1) {
4227c478bd9Sstevel@tonic-gate 		mdb_warn("Couldn't read struct hat\n");
4237c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * read the htable hashtable
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	*pap = 0;
4307c478bd9Sstevel@tonic-gate 	for (level = 0; level <= mmu.max_level; ++level) {
4317c478bd9Sstevel@tonic-gate 		if (level == mmu.max_level)
4327c478bd9Sstevel@tonic-gate 			base = 0;
4337c478bd9Sstevel@tonic-gate 		else
4347c478bd9Sstevel@tonic-gate 			base = addr & mmu.level_mask[level + 1];
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		for (h = 0; h < hat.hat_num_hash; ++h) {
4377c478bd9Sstevel@tonic-gate 			if (mdb_vread(&ht, sizeof (htable_t *),
4387c478bd9Sstevel@tonic-gate 			    (uintptr_t)(hat.hat_ht_hash + h)) == -1) {
4397c478bd9Sstevel@tonic-gate 				mdb_warn("Couldn't read htable\n");
4407c478bd9Sstevel@tonic-gate 				return (DCMD_ERR);
4417c478bd9Sstevel@tonic-gate 			}
4427c478bd9Sstevel@tonic-gate 			for (; ht != NULL; ht = htable.ht_next) {
4437c478bd9Sstevel@tonic-gate 				if (mdb_vread(&htable, sizeof (htable_t),
4447c478bd9Sstevel@tonic-gate 				    (uintptr_t)ht) == -1) {
4457c478bd9Sstevel@tonic-gate 					mdb_warn("Couldn't read htable\n");
4467c478bd9Sstevel@tonic-gate 					return (DCMD_ERR);
4477c478bd9Sstevel@tonic-gate 				}
4487c478bd9Sstevel@tonic-gate 				if (htable.ht_vaddr != base ||
4497c478bd9Sstevel@tonic-gate 				    htable.ht_level != level)
4507c478bd9Sstevel@tonic-gate 					continue;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 				/*
4537c478bd9Sstevel@tonic-gate 				 * found - read the page table entry
4547c478bd9Sstevel@tonic-gate 				 */
4557c478bd9Sstevel@tonic-gate 				paddr = htable.ht_pfn << MMU_PAGESHIFT;
4567c478bd9Sstevel@tonic-gate 				paddr += ((addr - base) >>
4577c478bd9Sstevel@tonic-gate 				    mmu.level_shift[level]) <<
4587c478bd9Sstevel@tonic-gate 				    mmu.pte_size_shift;
4597c478bd9Sstevel@tonic-gate 				len = mdb_pread(&buf, mmu.pte_size, paddr);
4607c478bd9Sstevel@tonic-gate 				if (len != mmu.pte_size)
4617c478bd9Sstevel@tonic-gate 					return (DCMD_ERR);
4627c478bd9Sstevel@tonic-gate 				if (mmu.pte_size == sizeof (x86pte_t))
4637c478bd9Sstevel@tonic-gate 					pte = buf;
4647c478bd9Sstevel@tonic-gate 				else
4657c478bd9Sstevel@tonic-gate 					pte = *pte32;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 				if (!found) {
4687c478bd9Sstevel@tonic-gate 					if (PTE_IS_LGPG(pte, level))
4697c478bd9Sstevel@tonic-gate 						paddr = pte & PT_PADDR_LGPG;
4707c478bd9Sstevel@tonic-gate 					else
4717c478bd9Sstevel@tonic-gate 						paddr = pte & PT_PADDR;
4727c478bd9Sstevel@tonic-gate 					paddr += addr & mmu.level_offset[level];
4737c478bd9Sstevel@tonic-gate 					*pap = paddr;
4747c478bd9Sstevel@tonic-gate 					found = 1;
4757c478bd9Sstevel@tonic-gate 				}
4767c478bd9Sstevel@tonic-gate 				if (print_level == 0)
4777c478bd9Sstevel@tonic-gate 					continue;
4787c478bd9Sstevel@tonic-gate 				mdb_printf("\tlevel=%d htable=%p pte=%llx\n",
4797c478bd9Sstevel@tonic-gate 				    level, ht, pte);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate done:
4857c478bd9Sstevel@tonic-gate 	if (!found)
4867c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
4877c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate int
4917c478bd9Sstevel@tonic-gate va2pfn_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	uintptr_t addrspace;
4947c478bd9Sstevel@tonic-gate 	char *addrspace_str = NULL;
4957c478bd9Sstevel@tonic-gate 	uint64_t physaddr;
4967c478bd9Sstevel@tonic-gate 	int rc;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * The kernel has to at least have made it thru mmu_init()
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	get_mmu();
5027c478bd9Sstevel@tonic-gate 	if (mmu.num_level == 0)
5037c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
5067c478bd9Sstevel@tonic-gate 	    'a', MDB_OPT_STR, &addrspace_str) != argc)
5077c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
5107c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * parse the address space
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	if (addrspace_str != NULL)
5167c478bd9Sstevel@tonic-gate 		addrspace = mdb_strtoull(addrspace_str);
5177c478bd9Sstevel@tonic-gate 	else
5187c478bd9Sstevel@tonic-gate 		addrspace = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	rc = do_va2pfn(addr, (struct as *)addrspace, 1, &physaddr);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (rc == DCMD_OK)
5237c478bd9Sstevel@tonic-gate 		mdb_printf("Virtual %p maps Physical %llx\n", addr, physaddr);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	return (rc);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Report all hat's that either use PFN as a page table or that map the page.
5307c478bd9Sstevel@tonic-gate  */
5317c478bd9Sstevel@tonic-gate static int
5327c478bd9Sstevel@tonic-gate do_report_maps(pfn_t pfn)
5337c478bd9Sstevel@tonic-gate {
534*a85a6733Sjosephb 	struct hat *hatp;
5357c478bd9Sstevel@tonic-gate 	struct hat hat;
5367c478bd9Sstevel@tonic-gate 	htable_t *ht;
5377c478bd9Sstevel@tonic-gate 	htable_t htable;
5387c478bd9Sstevel@tonic-gate 	uintptr_t base;
5397c478bd9Sstevel@tonic-gate 	int h;
5407c478bd9Sstevel@tonic-gate 	int level;
5417c478bd9Sstevel@tonic-gate 	int entry;
5427c478bd9Sstevel@tonic-gate 	x86pte_t pte;
5437c478bd9Sstevel@tonic-gate 	x86pte_t buf;
5447c478bd9Sstevel@tonic-gate 	x86pte32_t *pte32 = (x86pte32_t *)&buf;
5457c478bd9Sstevel@tonic-gate 	physaddr_t paddr;
5467c478bd9Sstevel@tonic-gate 	size_t len;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
549*a85a6733Sjosephb 	 * The hats are kept in a list with khat at the head.
5507c478bd9Sstevel@tonic-gate 	 */
551*a85a6733Sjosephb 	for (hatp = khat; hatp != NULL; hatp = hat.hat_next) {
5527c478bd9Sstevel@tonic-gate 		/*
5537c478bd9Sstevel@tonic-gate 		 * read the hat and its hash table
5547c478bd9Sstevel@tonic-gate 		 */
5557c478bd9Sstevel@tonic-gate 		if (mdb_vread(&hat, sizeof (hat), (uintptr_t)hatp) == -1) {
5567c478bd9Sstevel@tonic-gate 			mdb_warn("Couldn't read struct hat\n");
5577c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		/*
5617c478bd9Sstevel@tonic-gate 		 * read the htable hashtable
5627c478bd9Sstevel@tonic-gate 		 */
5637c478bd9Sstevel@tonic-gate 		paddr = 0;
5647c478bd9Sstevel@tonic-gate 		for (h = 0; h < hat.hat_num_hash; ++h) {
5657c478bd9Sstevel@tonic-gate 			if (mdb_vread(&ht, sizeof (htable_t *),
5667c478bd9Sstevel@tonic-gate 			    (uintptr_t)(hat.hat_ht_hash + h)) == -1) {
5677c478bd9Sstevel@tonic-gate 				mdb_warn("Couldn't read htable\n");
5687c478bd9Sstevel@tonic-gate 				return (DCMD_ERR);
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 			for (; ht != NULL; ht = htable.ht_next) {
5717c478bd9Sstevel@tonic-gate 				if (mdb_vread(&htable, sizeof (htable_t),
5727c478bd9Sstevel@tonic-gate 				    (uintptr_t)ht) == -1) {
5737c478bd9Sstevel@tonic-gate 					mdb_warn("Couldn't read htable\n");
5747c478bd9Sstevel@tonic-gate 					return (DCMD_ERR);
5757c478bd9Sstevel@tonic-gate 				}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 				/*
5787c478bd9Sstevel@tonic-gate 				 * only report kernel addresses once
5797c478bd9Sstevel@tonic-gate 				 */
5807c478bd9Sstevel@tonic-gate 				if (hatp != khat &&
5817c478bd9Sstevel@tonic-gate 				    htable.ht_vaddr >= kernelbase)
5827c478bd9Sstevel@tonic-gate 					continue;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 				/*
5857c478bd9Sstevel@tonic-gate 				 * Is the PFN a pagetable itself?
5867c478bd9Sstevel@tonic-gate 				 */
5877c478bd9Sstevel@tonic-gate 				if (htable.ht_pfn == pfn) {
5887c478bd9Sstevel@tonic-gate 					mdb_printf("Pagetable for "
5897c478bd9Sstevel@tonic-gate 					    "hat=%p htable=%p\n", hatp, ht);
5907c478bd9Sstevel@tonic-gate 					continue;
5917c478bd9Sstevel@tonic-gate 				}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 				/*
5947c478bd9Sstevel@tonic-gate 				 * otherwise, examine page mappings
5957c478bd9Sstevel@tonic-gate 				 */
5967c478bd9Sstevel@tonic-gate 				level = htable.ht_level;
5977c478bd9Sstevel@tonic-gate 				if (level > mmu.max_page_level)
5987c478bd9Sstevel@tonic-gate 					continue;
5997c478bd9Sstevel@tonic-gate 				paddr = htable.ht_pfn << MMU_PAGESHIFT;
6007c478bd9Sstevel@tonic-gate 				for (entry = 0; entry < htable.ht_num_ptes;
6017c478bd9Sstevel@tonic-gate 				    ++entry) {
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 					base = htable.ht_vaddr + entry *
6047c478bd9Sstevel@tonic-gate 					    mmu.level_size[level];
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 					/*
6077c478bd9Sstevel@tonic-gate 					 * only report kernel addresses once
6087c478bd9Sstevel@tonic-gate 					 */
6097c478bd9Sstevel@tonic-gate 					if (hatp != khat &&
6107c478bd9Sstevel@tonic-gate 					    base >= kernelbase)
6117c478bd9Sstevel@tonic-gate 						continue;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 					len = mdb_pread(&buf, mmu.pte_size,
6147c478bd9Sstevel@tonic-gate 					    paddr + entry * mmu.pte_size);
6157c478bd9Sstevel@tonic-gate 					if (len != mmu.pte_size)
6167c478bd9Sstevel@tonic-gate 						return (DCMD_ERR);
6177c478bd9Sstevel@tonic-gate 					if (mmu.pte_size == sizeof (x86pte_t))
6187c478bd9Sstevel@tonic-gate 						pte = buf;
6197c478bd9Sstevel@tonic-gate 					else
6207c478bd9Sstevel@tonic-gate 						pte = *pte32;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 					if ((pte & PT_VALID) == 0)
6237c478bd9Sstevel@tonic-gate 						continue;
6247c478bd9Sstevel@tonic-gate 					if (level == 0 || !(pte & PT_PAGESIZE))
6257c478bd9Sstevel@tonic-gate 						pte &= PT_PADDR;
6267c478bd9Sstevel@tonic-gate 					else
6277c478bd9Sstevel@tonic-gate 						pte &= PT_PADDR_LGPG;
6287c478bd9Sstevel@tonic-gate 					if ((pte >> MMU_PAGESHIFT) != pfn)
6297c478bd9Sstevel@tonic-gate 						continue;
6307c478bd9Sstevel@tonic-gate 					mdb_printf("hat=%p maps addr=%p\n",
6317c478bd9Sstevel@tonic-gate 						hatp, (caddr_t)base);
6327c478bd9Sstevel@tonic-gate 				}
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate done:
6387c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  * given a PFN as its address argument, prints out the uses of it
6437c478bd9Sstevel@tonic-gate  */
6447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6457c478bd9Sstevel@tonic-gate int
6467c478bd9Sstevel@tonic-gate report_maps_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * The kernel has to at least have made it thru mmu_init()
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	get_mmu();
6527c478bd9Sstevel@tonic-gate 	if (mmu.num_level == 0)
6537c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
6567c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	return (do_report_maps((pfn_t)addr));
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Dump the page table at the given PFN
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate static int
6657c478bd9Sstevel@tonic-gate do_ptable_dcmd(pfn_t pfn)
6667c478bd9Sstevel@tonic-gate {
667*a85a6733Sjosephb 	struct hat *hatp;
6687c478bd9Sstevel@tonic-gate 	struct hat hat;
6697c478bd9Sstevel@tonic-gate 	htable_t *ht;
6707c478bd9Sstevel@tonic-gate 	htable_t htable;
6717c478bd9Sstevel@tonic-gate 	uintptr_t base;
6727c478bd9Sstevel@tonic-gate 	int h;
6737c478bd9Sstevel@tonic-gate 	int level;
6747c478bd9Sstevel@tonic-gate 	int entry;
6757c478bd9Sstevel@tonic-gate 	uintptr_t pagesize;
6767c478bd9Sstevel@tonic-gate 	x86pte_t pte;
6777c478bd9Sstevel@tonic-gate 	x86pte_t buf;
6787c478bd9Sstevel@tonic-gate 	x86pte32_t *pte32 = (x86pte32_t *)&buf;
6797c478bd9Sstevel@tonic-gate 	physaddr_t paddr;
6807c478bd9Sstevel@tonic-gate 	size_t len;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
683*a85a6733Sjosephb 	 * The hats are kept in a list with khat at the head.
6847c478bd9Sstevel@tonic-gate 	 */
685*a85a6733Sjosephb 	for (hatp = khat; hatp != NULL; hatp = hat.hat_next) {
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * read the hat and its hash table
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		if (mdb_vread(&hat, sizeof (hat), (uintptr_t)hatp) == -1) {
6907c478bd9Sstevel@tonic-gate 			mdb_warn("Couldn't read struct hat\n");
6917c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		/*
6957c478bd9Sstevel@tonic-gate 		 * read the htable hashtable
6967c478bd9Sstevel@tonic-gate 		 */
6977c478bd9Sstevel@tonic-gate 		paddr = 0;
6987c478bd9Sstevel@tonic-gate 		for (h = 0; h < hat.hat_num_hash; ++h) {
6997c478bd9Sstevel@tonic-gate 			if (mdb_vread(&ht, sizeof (htable_t *),
7007c478bd9Sstevel@tonic-gate 			    (uintptr_t)(hat.hat_ht_hash + h)) == -1) {
7017c478bd9Sstevel@tonic-gate 				mdb_warn("Couldn't read htable\n");
7027c478bd9Sstevel@tonic-gate 				return (DCMD_ERR);
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			for (; ht != NULL; ht = htable.ht_next) {
7057c478bd9Sstevel@tonic-gate 				if (mdb_vread(&htable, sizeof (htable_t),
7067c478bd9Sstevel@tonic-gate 				    (uintptr_t)ht) == -1) {
7077c478bd9Sstevel@tonic-gate 					mdb_warn("Couldn't read htable\n");
7087c478bd9Sstevel@tonic-gate 					return (DCMD_ERR);
7097c478bd9Sstevel@tonic-gate 				}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 				/*
7127c478bd9Sstevel@tonic-gate 				 * Is this the PFN for this htable
7137c478bd9Sstevel@tonic-gate 				 */
7147c478bd9Sstevel@tonic-gate 				if (htable.ht_pfn == pfn)
7157c478bd9Sstevel@tonic-gate 					goto found_it;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate found_it:
7217c478bd9Sstevel@tonic-gate 	if (htable.ht_pfn == pfn) {
7227c478bd9Sstevel@tonic-gate 		mdb_printf("htable=%p\n", ht);
7237c478bd9Sstevel@tonic-gate 		level = htable.ht_level;
7247c478bd9Sstevel@tonic-gate 		base = htable.ht_vaddr;
7257c478bd9Sstevel@tonic-gate 		pagesize = mmu.level_size[level];
7267c478bd9Sstevel@tonic-gate 	} else {
7277c478bd9Sstevel@tonic-gate 		mdb_printf("Unknown pagetable - assuming level/addr 0");
7287c478bd9Sstevel@tonic-gate 		level = 0;	/* assume level == 0 for PFN */
7297c478bd9Sstevel@tonic-gate 		base = 0;
7307c478bd9Sstevel@tonic-gate 		pagesize = MMU_PAGESIZE;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	paddr = pfn << MMU_PAGESHIFT;
7347c478bd9Sstevel@tonic-gate 	for (entry = 0; entry < mmu.ptes_per_table; ++entry) {
7357c478bd9Sstevel@tonic-gate 		len = mdb_pread(&buf, mmu.pte_size,
7367c478bd9Sstevel@tonic-gate 		    paddr + entry * mmu.pte_size);
7377c478bd9Sstevel@tonic-gate 		if (len != mmu.pte_size)
7387c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
7397c478bd9Sstevel@tonic-gate 		if (mmu.pte_size == sizeof (x86pte_t))
7407c478bd9Sstevel@tonic-gate 			pte = buf;
7417c478bd9Sstevel@tonic-gate 		else
7427c478bd9Sstevel@tonic-gate 			pte = *pte32;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		if (pte == 0)
7457c478bd9Sstevel@tonic-gate 			continue;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		mdb_printf("[%3d] va=%p ", entry, base + entry * pagesize);
7487c478bd9Sstevel@tonic-gate 		do_pte_dcmd(level, pte);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate done:
7527c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * given a PFN as its address argument, prints out the uses of it
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7597c478bd9Sstevel@tonic-gate int
7607c478bd9Sstevel@tonic-gate ptable_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * The kernel has to at least have made it thru mmu_init()
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	get_mmu();
7667c478bd9Sstevel@tonic-gate 	if (mmu.num_level == 0)
7677c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0)
7707c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	return (do_ptable_dcmd((pfn_t)addr));
7737c478bd9Sstevel@tonic-gate }
774