xref: /titanic_44/usr/src/uts/sun4/io/trapstat.c (revision 02bc52be7430b2f7fafe1a2c981bff49ef11d6fa)
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
525cf1a30Sjl139090  * Common Development and Distribution License (the "License").
625cf1a30Sjl139090  * 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 /*
22fb2f18f8Sesaxe  * Copyright 2007 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 #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/conf.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
357c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
367c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
377c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
387c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
397c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
407c478bd9Sstevel@tonic-gate #include <sys/machasi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/callb.h>
437c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/trapstat.h>
457c478bd9Sstevel@tonic-gate #ifdef sun4v
467c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
477c478bd9Sstevel@tonic-gate #endif
4825cf1a30Sjl139090 #ifndef sun4v
49fb2f18f8Sesaxe #include <sys/pghw.h>
5025cf1a30Sjl139090 #endif
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * trapstat:  Trap Statistics through Dynamic Trap Table Interposition
557c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Motivation and Overview
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Despite being a fundamental indicator of system behavior, there has
607c478bd9Sstevel@tonic-gate  * historically been very little insight provided into the frequency and cost
617c478bd9Sstevel@tonic-gate  * of machine-specific traps.  The lack of insight has been especially acute
627c478bd9Sstevel@tonic-gate  * on UltraSPARC microprocessors:  because these microprocessors handle TLB
637c478bd9Sstevel@tonic-gate  * misses as software traps, the frequency and duration of traps play a
647c478bd9Sstevel@tonic-gate  * decisive role in the performance of the memory system.  As applications have
657c478bd9Sstevel@tonic-gate  * increasingly outstripped TLB reach, this has become increasingly true.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * Part of the difficulty of observing trap behavior is that the trap handlers
687c478bd9Sstevel@tonic-gate  * are so frequently called (e.g. millions of times per second) that any
697c478bd9Sstevel@tonic-gate  * permanently enabled instrumentation would induce an unacceptable performance
707c478bd9Sstevel@tonic-gate  * degradation.  Thus, it is a constraint on any trap observability
717c478bd9Sstevel@tonic-gate  * infrastructure that it have no probe effect when not explicitly enabled.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * The basic idea, then, is to create an interposing trap table in which each
747c478bd9Sstevel@tonic-gate  * entry increments a per-trap, in-memory counter and then jumps to the actual,
757c478bd9Sstevel@tonic-gate  * underlying trap table entry.  To enable trapstat, we atomically write to the
767c478bd9Sstevel@tonic-gate  * trap base address (%tba) register to point to our interposing trap table.
777c478bd9Sstevel@tonic-gate  * (Note that per-CPU statistics fall out by creating a different trap table
787c478bd9Sstevel@tonic-gate  * for each CPU.)
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * Implementation Details
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * While the idea is straight-forward, a nuance of SPARC V9 slightly
837c478bd9Sstevel@tonic-gate  * complicates the implementation.  Unlike its predecessors, SPARC V9 supports
847c478bd9Sstevel@tonic-gate  * the notion of nested traps.  The trap level is kept in the TL register:
857c478bd9Sstevel@tonic-gate  * during normal operation it is 0; when a trap is taken, the TL register is
867c478bd9Sstevel@tonic-gate  * incremented by 1.  To aid system software, SPARC V9 breaks the trap table
877c478bd9Sstevel@tonic-gate  * into two halves:  the lower half contains the trap handlers for traps taken
887c478bd9Sstevel@tonic-gate  * when TL is 0; the upper half contains the trap handlers for traps taken
897c478bd9Sstevel@tonic-gate  * when TL is greater than 0.  Each half is further subdivided into two
907c478bd9Sstevel@tonic-gate  * subsequent halves:  the lower half contains the trap handlers for traps
917c478bd9Sstevel@tonic-gate  * other than those induced by the trap instruction (Tcc variants); the upper
927c478bd9Sstevel@tonic-gate  * half contains the trap handlers for traps induced by the trap instruction.
937c478bd9Sstevel@tonic-gate  * This gives a total of four ranges, with each range containing 256 traps:
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 3ff
967c478bd9Sstevel@tonic-gate  *       |                                |   .
977c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL>0     |   .
987c478bd9Sstevel@tonic-gate  *       |                                |   .
997c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 300
1007c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 2ff
1017c478bd9Sstevel@tonic-gate  *       |                                |   .
1027c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .
1037c478bd9Sstevel@tonic-gate  *       |                                |   .
1047c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200
1057c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff
1067c478bd9Sstevel@tonic-gate  *       |                                |   .
1077c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .
1087c478bd9Sstevel@tonic-gate  *       |                                |   .
1097c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100
1107c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff
1117c478bd9Sstevel@tonic-gate  *       |                                |   .
1127c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .
1137c478bd9Sstevel@tonic-gate  *       |                                |   .
1147c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * Solaris, however, doesn't have reason to support trap instructions when
1187c478bd9Sstevel@tonic-gate  * TL>0 (only privileged code may execute at TL>0; not supporting this only
1197c478bd9Sstevel@tonic-gate  * constrains our own implementation).  The trap table actually looks like:
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff
1227c478bd9Sstevel@tonic-gate  *       |                                |   .
1237c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .
1247c478bd9Sstevel@tonic-gate  *       |                                |   .
1257c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200
1267c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff
1277c478bd9Sstevel@tonic-gate  *       |                                |   .
1287c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .
1297c478bd9Sstevel@tonic-gate  *       |                                |   .
1307c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100
1317c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff
1327c478bd9Sstevel@tonic-gate  *       |                                |   .
1337c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .
1347c478bd9Sstevel@tonic-gate  *       |                                |   .
1357c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * Putatively to aid system software, SPARC V9 has the notion of multiple
1387c478bd9Sstevel@tonic-gate  * sets of global registers.  UltraSPARC defines four sets of global
1397c478bd9Sstevel@tonic-gate  * registers:
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  *    Normal Globals
1427c478bd9Sstevel@tonic-gate  *    Alternate Globals (AGs)
1437c478bd9Sstevel@tonic-gate  *    MMU Globals (MGs)
1447c478bd9Sstevel@tonic-gate  *    Interrupt Globals (IGs)
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * The set of globals in use is controlled by bits in PSTATE; when TL is 0
1477c478bd9Sstevel@tonic-gate  * (and PSTATE has not been otherwise explicitly modified), the Normal Globals
1487c478bd9Sstevel@tonic-gate  * are in use.  When a trap is issued, PSTATE is modified to point to a set of
1497c478bd9Sstevel@tonic-gate  * globals corresponding to the trap type.  Most traps correspond to the
1507c478bd9Sstevel@tonic-gate  * Alternate Globals, with a minority corresponding to the MMU Globals, and
1517c478bd9Sstevel@tonic-gate  * only the interrupt-vector trap (vector 0x60) corresponding to the Interrupt
1527c478bd9Sstevel@tonic-gate  * Globals.  (The complete mapping can be found in the UltraSPARC I&II User's
1537c478bd9Sstevel@tonic-gate  * Manual.)
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * Note that the sets of globals are per trap _type_, not per trap _level_.
1567c478bd9Sstevel@tonic-gate  * Thus, when executing a TL>0 trap handler, one may not have registers
1577c478bd9Sstevel@tonic-gate  * available (for example, both trap-instruction traps and spill traps execute
1587c478bd9Sstevel@tonic-gate  * on the alternate globals; if a trap-instruction trap induces a window spill,
1597c478bd9Sstevel@tonic-gate  * the window spill handler has no available globals).  For trapstat, this is
1607c478bd9Sstevel@tonic-gate  * problematic:  a register is required to transfer control from one arbitrary
1617c478bd9Sstevel@tonic-gate  * location (in the interposing trap table) to another (in the actual trap
1627c478bd9Sstevel@tonic-gate  * table).
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * We solve this problem by exploiting the trap table's location at the bottom
1657c478bd9Sstevel@tonic-gate  * of valid kernel memory (i.e. at KERNELBASE).  We locate the interposing trap
1667c478bd9Sstevel@tonic-gate  * tables just below KERNELBASE -- thereby allowing us to use a branch-always
1677c478bd9Sstevel@tonic-gate  * instruction (ba) instead of a jump instruction (jmp) to transfer control
1687c478bd9Sstevel@tonic-gate  * from the TL>0 entries in the interposing trap table to the TL>0 entries in
1697c478bd9Sstevel@tonic-gate  * the actual trap table.  (N.B. while this allows trap table interposition to
1707c478bd9Sstevel@tonic-gate  * work, it necessarily limits trapstat to only recording information about
1717c478bd9Sstevel@tonic-gate  * TL=0 traps -- there is no way to increment a counter without using a
1727c478bd9Sstevel@tonic-gate  * register.)  Diagrammatically:
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  *  Actual trap table:
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff
1777c478bd9Sstevel@tonic-gate  *       |                                |   .
1787c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .   <-----------------------+
1797c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------------------|-+
1807c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200  <-----------------------|-|-+
1817c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff                          | | |
1827c478bd9Sstevel@tonic-gate  *       |                                |   .                           | | |
1837c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .   <-----------------+     | | |
1847c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------------|-+   | | |
1857c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100  <-----------------|-|-+ | | |
1867c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff                    | | | | | |
1877c478bd9Sstevel@tonic-gate  *       |                                |   .                     | | | | | |
1887c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .   <-----------+     | | | | | |
1897c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------|-+   | | | | | |
1907c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000  <-----------|-|-+ | | | | | |
1917c478bd9Sstevel@tonic-gate  *        KERNELBASE                                          | | | | | | | | |
1927c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
1937c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
1947c478bd9Sstevel@tonic-gate  *  Interposing trap table:                                   | | | | | | | | |
1957c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
1967c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff              | | | | | | | | |
1977c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
1987c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
1997c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
2007c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 203              | | | | | | | | |
2017c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|-+ | |
2027c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 202              | | | | | |   | |
2037c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|---+ |
2047c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 201              | | | | | |     |
2057c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|-----+
2067c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200              | | | | | |
2077c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
2087c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
2097c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
2107c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 103              | | | | | |
2117c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | | | | |
2127c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------------+ | |
2137c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 102              | | |   | |
2147c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |   | |
2157c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      ---------------------+ |
2167c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 101              | | |     |
2177c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |     |
2187c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -----------------------+
2197c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100              | | |
2207c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
2217c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
2227c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
2237c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 003              | | |
2247c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |
2257c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------+ | |
2267c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 002                | |
2277c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                     | |
2287c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      ---------------+ |
2297c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 001                  |
2307c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                       |
2317c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -----------------+
2327c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
2337c478bd9Sstevel@tonic-gate  *        KERNELBASE - tstat_total_size
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * tstat_total_size is the number of pages required for each trap table.  It
2367c478bd9Sstevel@tonic-gate  * must be true that KERNELBASE - tstat_total_size is less than the maximum
2377c478bd9Sstevel@tonic-gate  * branch displacement; if each CPU were to consume a disjoint virtual range
2387c478bd9Sstevel@tonic-gate  * below KERNELBASE for its trap table, we could support at most
2397c478bd9Sstevel@tonic-gate  * (maximum_branch_displacement / tstat_total_size) CPUs.  The maximum branch
2407c478bd9Sstevel@tonic-gate  * displacement for Bicc variants is just under eight megabytes, and (because
2417c478bd9Sstevel@tonic-gate  * the %tba must be 32K aligned), tstat_total_size must be at least 32K; if
2427c478bd9Sstevel@tonic-gate  * each CPU were to consume a disjoint virtual range, we would have an
2437c478bd9Sstevel@tonic-gate  * unacceptably low upper bound of 256 CPUs.
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * While there are tricks that one could use to address this constraint (e.g.,
2467c478bd9Sstevel@tonic-gate  * creating trampolines every maximum_branch_displacement bytes), we instead
2477c478bd9Sstevel@tonic-gate  * solve this by not permitting each CPU to consume a disjoint virtual range.
2487c478bd9Sstevel@tonic-gate  * Rather, we have each CPU's interposing trap table use the _same_ virtual
2497c478bd9Sstevel@tonic-gate  * range, but we back the trap tables with disjoint physical memory.  Normally,
2507c478bd9Sstevel@tonic-gate  * such one-to-many virtual-to-physical mappings are illegal; this is
2517c478bd9Sstevel@tonic-gate  * permissible here only because the pages for the interposing trap table are
2527c478bd9Sstevel@tonic-gate  * necessarily locked in the TLB.  (The CPUs thus never have the opportunity to
2537c478bd9Sstevel@tonic-gate  * discover that they have conflicting translations.)
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * On CMT architectures in which CPUs can share MMUs, the above trick will not
2567c478bd9Sstevel@tonic-gate  * work: two CPUs that share an MMU cannot have the same virtual address map
2577c478bd9Sstevel@tonic-gate  * to disjoint physical pages.  On these architectures, any CPUs sharing the
2587c478bd9Sstevel@tonic-gate  * same MMU must consume a disjoint 32K virtual address range -- limiting the
2597c478bd9Sstevel@tonic-gate  * number of CPUs sharing an MMU on these architectures to 256 due to the
2607c478bd9Sstevel@tonic-gate  * branch displacement limitation described above.  On the sun4v architecture,
2617c478bd9Sstevel@tonic-gate  * there is a further limitation: a guest may not have more than eight locked
2627c478bd9Sstevel@tonic-gate  * TLB entries per MMU.  To allow operation under this restriction, the
2637c478bd9Sstevel@tonic-gate  * interposing trap table and the trap statistics are each accessed through
2647c478bd9Sstevel@tonic-gate  * a single 4M TLB entry.  This limits the footprint to two locked entries
2657c478bd9Sstevel@tonic-gate  * (one for the I-TLB and one for the D-TLB), but further restricts the number
2667c478bd9Sstevel@tonic-gate  * of CPUs to 128 per MMU.  However, support for more than 128 CPUs can easily
2677c478bd9Sstevel@tonic-gate  * be added via a hybrid scheme, where the same 4M virtual address is used
2687c478bd9Sstevel@tonic-gate  * on different MMUs.
2697c478bd9Sstevel@tonic-gate  *
27059ac0c16Sdavemq  * On sun4v architecture, we currently don't use hybrid scheme as it imposes
27159ac0c16Sdavemq  * additional restriction on live migration and transparent CPU replacement.
27259ac0c16Sdavemq  * Instead, we increase the number of supported CPUs by reducing the virtual
27359ac0c16Sdavemq  * address space requirements per CPU via shared interposing trap table as
27459ac0c16Sdavemq  * follows:
27559ac0c16Sdavemq  *
27659ac0c16Sdavemq  *                                          Offset (within 4MB page)
27759ac0c16Sdavemq  *       +------------------------------------+- 0x400000
27859ac0c16Sdavemq  *       |  CPU 507 trap statistics (8KB)     |   .
27959ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x3fe000
28059ac0c16Sdavemq  *       |                                    |
28159ac0c16Sdavemq  *       |   ...                              |
28259ac0c16Sdavemq  *       |                                    |
28359ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x00c000
28459ac0c16Sdavemq  *       |  CPU 1 trap statistics (8KB)       |   .
28559ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x00a000
28659ac0c16Sdavemq  *       |  CPU 0 trap statistics (8KB)       |   .
28759ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x008000
28859ac0c16Sdavemq  *       |  Shared trap handler continuation  |   .
28959ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x006000
29059ac0c16Sdavemq  *       |  Non-trap instruction, TL>0        |   .
29159ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x004000
29259ac0c16Sdavemq  *       |  Trap instruction, TL=0            |   .
29359ac0c16Sdavemq  *       |- - - - - - - - - - - - - - - - - - +- 0x002000
29459ac0c16Sdavemq  *       |  Non-trap instruction, TL=0        |   .
29559ac0c16Sdavemq  *       +------------------------------------+- 0x000000
29659ac0c16Sdavemq  *
29759ac0c16Sdavemq  * Note that each CPU has its own 8K space for its trap statistics but
29859ac0c16Sdavemq  * shares the same interposing trap handlers.  Interposing trap handlers
29959ac0c16Sdavemq  * use the CPU ID to determine the location of per CPU trap statistics
30059ac0c16Sdavemq  * area dynamically. This increases the interposing trap handler overhead,
30159ac0c16Sdavemq  * but is acceptable as it allows us to support up to 508 CPUs with one
30259ac0c16Sdavemq  * 4MB page on sun4v architecture. Support for additional CPUs can be
30359ac0c16Sdavemq  * added via hybrid scheme as mentioned earlier.
3047c478bd9Sstevel@tonic-gate  *
3057c478bd9Sstevel@tonic-gate  * TLB Statistics
3067c478bd9Sstevel@tonic-gate  *
3077c478bd9Sstevel@tonic-gate  * Because TLB misses are an important component of system performance, we wish
3087c478bd9Sstevel@tonic-gate  * to know much more about these traps than simply the number received.
3097c478bd9Sstevel@tonic-gate  * Specifically, we wish to know:
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  *  (a)	The amount of time spent executing the TLB miss handler
3127c478bd9Sstevel@tonic-gate  *  (b)	TLB misses versus TSB misses
3137c478bd9Sstevel@tonic-gate  *  (c) Kernel-level misses versus user-level misses
3147c478bd9Sstevel@tonic-gate  *  (d) Misses per pagesize
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  * TLB Statistics: Time Spent Executing
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  * To accurately determine the amount of time spent executing the TLB miss
3197c478bd9Sstevel@tonic-gate  * handler, one must get a timestamp on trap entry and trap exit, subtract the
3207c478bd9Sstevel@tonic-gate  * latter from the former, and add the result to an accumulating count.
3217c478bd9Sstevel@tonic-gate  * Consider flow of control during normal TLB miss processing (where "ldx
3227c478bd9Sstevel@tonic-gate  * [%g2], %g2" is an arbitrary TLB-missing instruction):
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  * + - - - - - - - -+
3257c478bd9Sstevel@tonic-gate  * :                :
3267c478bd9Sstevel@tonic-gate  * : ldx [%g2], %g2 :<-------------------------------------------------------+
3277c478bd9Sstevel@tonic-gate  * :                :              Return from trap:                         |
3287c478bd9Sstevel@tonic-gate  * + - - - - - - - -+                TL <- TL - 1 (0)                        |
3297c478bd9Sstevel@tonic-gate  *	  |                          %pc <- TSTATE[TL].TPC (address of load) |
3307c478bd9Sstevel@tonic-gate  *	  | TLB miss:                                                        |
3317c478bd9Sstevel@tonic-gate  *        |   TL <- TL + 1 (1)                                               |
3327c478bd9Sstevel@tonic-gate  *        |   %pc <- TLB-miss-trap-handler                                   |
3337c478bd9Sstevel@tonic-gate  *        |                                                                  |
3347c478bd9Sstevel@tonic-gate  *        v                                                                  |
3357c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +                                         |
3367c478bd9Sstevel@tonic-gate  * :                               :                                         |
3377c478bd9Sstevel@tonic-gate  * : Lookup VA in TSB              :                                         |
3387c478bd9Sstevel@tonic-gate  * : If (hit)                      :                                         |
3397c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                                         |
3407c478bd9Sstevel@tonic-gate  * : Else                          :                                         |
3417c478bd9Sstevel@tonic-gate  * :     Lookup VA (hme hash table :                                         |
3427c478bd9Sstevel@tonic-gate  * :                or segkpm)     :                                         |
3437c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                                         |
3447c478bd9Sstevel@tonic-gate  * : Endif                         :                                         |
3457c478bd9Sstevel@tonic-gate  * : Issue "retry"  ---------------------------------------------------------+
3467c478bd9Sstevel@tonic-gate  * :                               :
3477c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +
3487c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  *
3517c478bd9Sstevel@tonic-gate  * As the above diagram indicates, interposing on the trap table allows one
3527c478bd9Sstevel@tonic-gate  * only to determine a timestamp on trap _entry_:  when the TLB miss handler
3537c478bd9Sstevel@tonic-gate  * has completed filling the TLB, a "retry" will be issued, and control will
3547c478bd9Sstevel@tonic-gate  * transfer immediately back to the missing %pc.
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * To obtain a timestamp on trap exit, we must then somehow interpose between
3577c478bd9Sstevel@tonic-gate  * the "retry" and the subsequent control transfer to the TLB-missing
3587c478bd9Sstevel@tonic-gate  * instruction.  To do this, we _push_ a trap level.  The basic idea is to
3597c478bd9Sstevel@tonic-gate  * spoof a TLB miss by raising TL, setting the %tpc to be within text
3607c478bd9Sstevel@tonic-gate  * controlled by trapstat (the "TLB return entry") and branching to the
3617c478bd9Sstevel@tonic-gate  * underlying TLB miss handler.  When the TLB miss handler issues its "retry",
3627c478bd9Sstevel@tonic-gate  * control will transfer not to the TLB-missing instruction, but rather to the
3637c478bd9Sstevel@tonic-gate  * TLB return entry.  This code can then obtain a timestamp, and issue its own
3647c478bd9Sstevel@tonic-gate  * "retry" -- thereby correctly returning to the TLB-missing instruction.
3657c478bd9Sstevel@tonic-gate  * Here is the above TLB miss flow control diagram modified to reflect
3667c478bd9Sstevel@tonic-gate  * trapstat's operation:
3677c478bd9Sstevel@tonic-gate  *
3687c478bd9Sstevel@tonic-gate  * + - - - - - - - -+
3697c478bd9Sstevel@tonic-gate  * :                :
3707c478bd9Sstevel@tonic-gate  * : ldx [%g2], %g2 :<-------------------------------------------------------+
3717c478bd9Sstevel@tonic-gate  * :                :             Return from trap:                          |
3727c478bd9Sstevel@tonic-gate  * + - - - - - - - -+               TL <- TL - 1 (0)                         |
3737c478bd9Sstevel@tonic-gate  *	  |                         %pc <- TSTATE[TL].TPC (address of load)  |
3747c478bd9Sstevel@tonic-gate  *	  | TLB miss:                                                        |
3757c478bd9Sstevel@tonic-gate  *        |   TL <- TL + 1 (1)                                               |
3767c478bd9Sstevel@tonic-gate  *        |   %pc <- TLB-miss-trap-handler (trapstat)                        |
3777c478bd9Sstevel@tonic-gate  *        |                                                                  |
3787c478bd9Sstevel@tonic-gate  *        v                                    TLB-return-entry (trapstat)   |
3797c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - - - - +    + - - - - - - - - - - - - - +  |
3807c478bd9Sstevel@tonic-gate  * :                                     :    :                           :  |
3817c478bd9Sstevel@tonic-gate  * : Record timestamp                    :    : Record timestamp          :  |
3827c478bd9Sstevel@tonic-gate  * : TL <- 2                             :    : Take timestamp difference :  |
3837c478bd9Sstevel@tonic-gate  * : TSTATE[1].TPC <- TLB-return-entry   :    : Add to running total      :  |
3847c478bd9Sstevel@tonic-gate  * : ba,a TLB-miss-trap-handler -----------+  : Issue "retry"  --------------+
3857c478bd9Sstevel@tonic-gate  * :                                     : |  :                           :
3867c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - - - - + |  + - - - - - - - - - - - - - +
3877c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler	           |                  ^
3887c478bd9Sstevel@tonic-gate  *  (trapstat)                             |                  |
3897c478bd9Sstevel@tonic-gate  *                                         |                  |
3907c478bd9Sstevel@tonic-gate  *                                         |                  |
3917c478bd9Sstevel@tonic-gate  *                 +-----------------------+                  |
3927c478bd9Sstevel@tonic-gate  *                 |                                          |
3937c478bd9Sstevel@tonic-gate  *                 |                                          |
3947c478bd9Sstevel@tonic-gate  *                 v                                          |
3957c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +                          |
3967c478bd9Sstevel@tonic-gate  * :                               :                          |
3977c478bd9Sstevel@tonic-gate  * : Lookup VA in TSB              :                          |
3987c478bd9Sstevel@tonic-gate  * : If (hit)                      :                          |
3997c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                          |
4007c478bd9Sstevel@tonic-gate  * : Else                          :                          |
4017c478bd9Sstevel@tonic-gate  * :     Lookup VA (hme hash table :                          |
4027c478bd9Sstevel@tonic-gate  * :                or segkpm)     :                          |
4037c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                          |
4047c478bd9Sstevel@tonic-gate  * : Endif                         :                          |
4057c478bd9Sstevel@tonic-gate  * : Issue "retry"  ------------------------------------------+
4067c478bd9Sstevel@tonic-gate  * :                               : Return from trap:
4077c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +   TL <- TL - 1 (1)
4087c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler              %pc <- TSTATE[TL].TPC (TLB-return-entry)
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  *
4117c478bd9Sstevel@tonic-gate  * A final subterfuge is required to complete our artifice:  if we miss in
4127c478bd9Sstevel@tonic-gate  * the TLB, the TSB _and_ the subsequent hash or segkpm lookup (that is, if
4137c478bd9Sstevel@tonic-gate  * there is no valid translation for the TLB-missing address), common system
4147c478bd9Sstevel@tonic-gate  * software will need to accurately determine the %tpc as part of its page
4157c478bd9Sstevel@tonic-gate  * fault handling. We therefore modify the kernel to check the %tpc in this
4167c478bd9Sstevel@tonic-gate  * case: if the %tpc falls within the VA range controlled by trapstat and
4177c478bd9Sstevel@tonic-gate  * the TL is 2, TL is simply lowered back to 1 (this check is implemented
4187c478bd9Sstevel@tonic-gate  * by the TSTAT_CHECK_TL1 macro).  Lowering TL to 1 has the effect of
4197c478bd9Sstevel@tonic-gate  * discarding the state pushed by trapstat.
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * TLB Statistics: TLB Misses versus TSB Misses
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * Distinguishing TLB misses from TSB misses requires further interposition
4247c478bd9Sstevel@tonic-gate  * on the TLB miss handler:  we cannot know a priori or a posteriori if a
4257c478bd9Sstevel@tonic-gate  * given VA will or has hit in the TSB.
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * We achieve this distinction by adding a second TLB return entry almost
4287c478bd9Sstevel@tonic-gate  * identical to the first -- differing only in the address to which it
4297c478bd9Sstevel@tonic-gate  * stores its results.  We then modify the TLB miss handlers of the kernel
4307c478bd9Sstevel@tonic-gate  * such that they check the %tpc when they determine that a TLB miss has
4317c478bd9Sstevel@tonic-gate  * subsequently missed in the TSB:  if the %tpc lies within trapstat's VA
4327c478bd9Sstevel@tonic-gate  * range and TL is 2 (that is, if trapstat is running), the TLB miss handler
4337c478bd9Sstevel@tonic-gate  * _increments_ the %tpc by the size of the TLB return entry.  The ensuing
4347c478bd9Sstevel@tonic-gate  * "retry" will thus transfer control to the second TLB return entry, and
4357c478bd9Sstevel@tonic-gate  * the time spent in the handler will be accumulated in a memory location
4367c478bd9Sstevel@tonic-gate  * specific to TSB misses.
4377c478bd9Sstevel@tonic-gate  *
4387c478bd9Sstevel@tonic-gate  * N.B.:  To minimize the amount of knowledge the kernel must have of trapstat,
4397c478bd9Sstevel@tonic-gate  * we do not allow the kernel to hard-code the size of the TLB return entry.
4407c478bd9Sstevel@tonic-gate  * Rather, the actual tsbmiss handler executes a known instruction at the
4417c478bd9Sstevel@tonic-gate  * corresponding tsbmiss patch points (see the tstat_tsbmiss_patch_table) with
4427c478bd9Sstevel@tonic-gate  * the %tpc in %g7:  when trapstat is not running, these points contain the
4437c478bd9Sstevel@tonic-gate  * harmless TSTAT_TSBMISS_INSTR instruction ("add %g7, 0, %g7"). Before
4447c478bd9Sstevel@tonic-gate  * running, trapstat modifies the instructions at these patch points such
4457c478bd9Sstevel@tonic-gate  * that the simm13 equals the size of the TLB return entry.
4467c478bd9Sstevel@tonic-gate  *
4477c478bd9Sstevel@tonic-gate  * TLB Statistics: Kernel-level Misses versus User-level Misses
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * Differentiating user-level misses from kernel-level misses employs a
4507c478bd9Sstevel@tonic-gate  * similar technique, but is simplified by the ability to distinguish a
4517c478bd9Sstevel@tonic-gate  * user-level miss from a kernel-level miss a priori by reading the context
4527c478bd9Sstevel@tonic-gate  * register:  we implement kernel-/user-level differentiation by again doubling
4537c478bd9Sstevel@tonic-gate  * the number of TLB return entries, and setting the %tpc to the appropriate
4547c478bd9Sstevel@tonic-gate  * TLB return entry in trapstat's TLB miss handler.  Together with the doubling
4557c478bd9Sstevel@tonic-gate  * of entries required for TLB-miss/TSB-miss differentiation, this yields a
4567c478bd9Sstevel@tonic-gate  * total of four TLB return entries:
4577c478bd9Sstevel@tonic-gate  *
4587c478bd9Sstevel@tonic-gate  *	Level		TSB hit?	Structure member
4597c478bd9Sstevel@tonic-gate  *	------------------------------------------------------------
4607c478bd9Sstevel@tonic-gate  *	Kernel		Yes		tstat_tlbret_t.ttlbr_ktlb
4617c478bd9Sstevel@tonic-gate  *	Kernel		No		tstat_tlbret_t.ttlbr_ktsb
4627c478bd9Sstevel@tonic-gate  *	User		Yes		tstat_tlbret_t.ttlbr_utlb
4637c478bd9Sstevel@tonic-gate  *	User		No		tstat_tlbret_t.ttlbr_utsb
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  * TLB Statistics: Misses per Pagesize
4667c478bd9Sstevel@tonic-gate  *
4677c478bd9Sstevel@tonic-gate  * As with the TLB-/TSB-miss differentiation, we have no way of determining
4687c478bd9Sstevel@tonic-gate  * pagesize a priori.  This is therefore implemented by mandating a new rule:
4697c478bd9Sstevel@tonic-gate  * whenever the kernel fills the TLB in its TLB miss handler, the TTE
4707c478bd9Sstevel@tonic-gate  * corresponding to the TLB-missing VA must be in %g5 when the handler
4717c478bd9Sstevel@tonic-gate  * executes its "retry".  This allows the TLB return entry to determine
4727c478bd9Sstevel@tonic-gate  * pagesize by simply looking at the pagesize field in the TTE stored in
4737c478bd9Sstevel@tonic-gate  * %g5.
4747c478bd9Sstevel@tonic-gate  *
4757c478bd9Sstevel@tonic-gate  * TLB Statistics: Probe Effect
4767c478bd9Sstevel@tonic-gate  *
4777c478bd9Sstevel@tonic-gate  * As one might imagine, gathering TLB statistics by pushing a trap level
4787c478bd9Sstevel@tonic-gate  * induces significant probe effect.  To account for this probe effect,
4797c478bd9Sstevel@tonic-gate  * trapstat attempts to observe it by executing a code sequence with a known
4807c478bd9Sstevel@tonic-gate  * number of TLB misses both before and after interposing on the trap table.
4817c478bd9Sstevel@tonic-gate  * This allows trapstat to determine a per-trap probe effect which can then be
4827c478bd9Sstevel@tonic-gate  * factored into the "%tim" fields of the trapstat command.
4837c478bd9Sstevel@tonic-gate  *
4847c478bd9Sstevel@tonic-gate  * Note that on sun4v platforms, TLB misses are normally handled by the
4857c478bd9Sstevel@tonic-gate  * hypervisor or the hardware TSB walker. Thus no fast MMU miss information
486ce0352ebSgirish  * is reported for normal operation. However, when trapstat is invoked
487ce0352ebSgirish  * with -t or -T option to collect detailed TLB statistics, kernel takes
4887c478bd9Sstevel@tonic-gate  * over TLB miss handling. This results in significantly more overhead
4897c478bd9Sstevel@tonic-gate  * and TLB statistics may not be as accurate as on sun4u platforms.
490ce0352ebSgirish  * On some processors, hypervisor or hardware may provide a low overhead
491ce0352ebSgirish  * interface to collect TSB hit statistics. This support is exposed via
492ce0352ebSgirish  * a well defined CPU module interface (cpu_trapstat_conf to enable this
493ce0352ebSgirish  * interface and cpu_trapstat_data to get detailed TSB hit statistics).
494ce0352ebSgirish  * In this scenario, TSB miss statistics is collected by intercepting the
495ce0352ebSgirish  * IMMU_miss and DMMU_miss traps using above mentioned trap interposition
496ce0352ebSgirish  * approach.
4977c478bd9Sstevel@tonic-gate  *
4987c478bd9Sstevel@tonic-gate  * Locking
4997c478bd9Sstevel@tonic-gate  *
5007c478bd9Sstevel@tonic-gate  * The implementation uses two locks:  tstat_lock (a local lock) and the global
5017c478bd9Sstevel@tonic-gate  * cpu_lock.  tstat_lock is used to assure trapstat's consistency in the
5027c478bd9Sstevel@tonic-gate  * presence of multithreaded /dev/trapstat consumers (while as of this writing
5037c478bd9Sstevel@tonic-gate  * the only consumer of /dev/trapstat is single threaded, it is obviously
5047c478bd9Sstevel@tonic-gate  * necessary to correctly support multithreaded access).  cpu_lock is held
5057c478bd9Sstevel@tonic-gate  * whenever CPUs are being manipulated directly, to prevent them from
5067c478bd9Sstevel@tonic-gate  * disappearing in the process.  Because trapstat's DR callback
5077c478bd9Sstevel@tonic-gate  * (trapstat_cpu_setup()) must grab tstat_lock and is called with cpu_lock
5087c478bd9Sstevel@tonic-gate  * held, the lock ordering is necessarily cpu_lock before tstat_lock.
5097c478bd9Sstevel@tonic-gate  *
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate /* END CSTYLED */
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static dev_info_t	*tstat_devi;	/* saved in xxattach() for xxinfo() */
5147c478bd9Sstevel@tonic-gate static int		tstat_open;	/* set if driver is open */
5157c478bd9Sstevel@tonic-gate static kmutex_t		tstat_lock;	/* serialize access */
5167c478bd9Sstevel@tonic-gate static vmem_t		*tstat_arena;	/* arena for TLB-locked pages */
5177c478bd9Sstevel@tonic-gate static tstat_percpu_t	*tstat_percpu;	/* per-CPU data */
5187c478bd9Sstevel@tonic-gate static int		tstat_running;	/* set if trapstat is running */
5197c478bd9Sstevel@tonic-gate static tstat_data_t	*tstat_buffer;	/* staging buffer for outgoing data */
5207c478bd9Sstevel@tonic-gate static int		tstat_options;	/* bit-wise indication of options */
5217c478bd9Sstevel@tonic-gate static int		*tstat_enabled;	/* map of enabled trap entries */
5227c478bd9Sstevel@tonic-gate static int		tstat_tsbmiss_patched; /* tsbmiss patch flag */
5237c478bd9Sstevel@tonic-gate static callb_id_t	tstat_cprcb;	/* CPR callback */
5247c478bd9Sstevel@tonic-gate static char		*tstat_probe_area; /* VA range used for probe effect */
5257c478bd9Sstevel@tonic-gate static caddr_t		tstat_probe_phys; /* physical to back above VA */
5267c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_time; /* time spent on probe effect */
5277c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_before[TSTAT_PROBE_NLAPS];
5287c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_after[TSTAT_PROBE_NLAPS];
5297c478bd9Sstevel@tonic-gate static uint_t		tstat_pgszs;		/* # of kernel page sizes */
5307c478bd9Sstevel@tonic-gate static uint_t		tstat_user_pgszs;	/* # of user page sizes */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate  * sizeof tstat_data_t + pgsz data for the kernel.  For simplicity's sake, when
5347c478bd9Sstevel@tonic-gate  * we collect data, we do it based upon szc, but when we report data back to
5357c478bd9Sstevel@tonic-gate  * userland, we have to do it based upon the userszc which may not match.
5367c478bd9Sstevel@tonic-gate  * So, these two variables are for internal use and exported use respectively.
5377c478bd9Sstevel@tonic-gate  */
5387c478bd9Sstevel@tonic-gate static size_t		tstat_data_t_size;
5397c478bd9Sstevel@tonic-gate static size_t		tstat_data_t_exported_size;
5407c478bd9Sstevel@tonic-gate 
54159ac0c16Sdavemq #ifndef sun4v
54259ac0c16Sdavemq 
5437c478bd9Sstevel@tonic-gate static size_t		tstat_data_pages;  /* number of pages of tstat data */
5447c478bd9Sstevel@tonic-gate static size_t		tstat_data_size;   /* tstat data size in bytes */
5457c478bd9Sstevel@tonic-gate static size_t		tstat_total_pages; /* #data pages + #instr pages */
5467c478bd9Sstevel@tonic-gate static size_t		tstat_total_size;  /* tstat data size + instr size */
54759ac0c16Sdavemq 
54859ac0c16Sdavemq #else /* sun4v */
54959ac0c16Sdavemq 
5507c478bd9Sstevel@tonic-gate static caddr_t		tstat_va;	/* VA of memory reserved for TBA */
5517c478bd9Sstevel@tonic-gate static pfn_t		tstat_pfn;	/* PFN of memory reserved for TBA */
552ce0352ebSgirish static boolean_t	tstat_fast_tlbstat = B_FALSE;
55359ac0c16Sdavemq static int		tstat_traptab_initialized;
55459ac0c16Sdavemq 
55559ac0c16Sdavemq #endif /* sun4v */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * In the above block comment, see "TLB Statistics: TLB Misses versus
5597c478bd9Sstevel@tonic-gate  * TSB Misses" for an explanation of the tsbmiss patch points.
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point;
5627c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point_kpm;
5637c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point_kpm_small;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Trapstat tsbmiss patch table
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t tstat_tsbmiss_patch_table[] = {
5697c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point, 0},
5707c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point_kpm, 0},
5717c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point_kpm_small, 0},
5727c478bd9Sstevel@tonic-gate 	{(uint32_t *)NULL, 0}
5737c478bd9Sstevel@tonic-gate };
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * We define some general SPARC-specific constants to allow more readable
5777c478bd9Sstevel@tonic-gate  * relocations.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate #define	NOP	0x01000000
5807c478bd9Sstevel@tonic-gate #define	HI22(v) ((uint32_t)(v) >> 10)
5817c478bd9Sstevel@tonic-gate #define	LO10(v) ((uint32_t)(v) & 0x3ff)
5827c478bd9Sstevel@tonic-gate #define	LO12(v) ((uint32_t)(v) & 0xfff)
5837c478bd9Sstevel@tonic-gate #define	DISP22(from, to) \
5847c478bd9Sstevel@tonic-gate 	((((uintptr_t)(to) - (uintptr_t)(from)) >> 2) & 0x3fffff)
5857c478bd9Sstevel@tonic-gate #define	ASI(asi)	((asi) << 5)
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * The interposing trap table must be locked in the I-TLB, and any data
5897c478bd9Sstevel@tonic-gate  * referred to in the interposing trap handler must be locked in the D-TLB.
5907c478bd9Sstevel@tonic-gate  * This function locks these pages in the appropriate TLBs by creating TTEs
5917c478bd9Sstevel@tonic-gate  * from whole cloth, and manually loading them into the TLB.  This function is
5927c478bd9Sstevel@tonic-gate  * called from cross call context.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * On sun4v platforms, we use 4M page size mappings to minimize the number
5957c478bd9Sstevel@tonic-gate  * of locked down entries (i.e. permanent mappings). Each CPU uses a
5967c478bd9Sstevel@tonic-gate  * reserved portion of that 4M page for its TBA and data.
5977c478bd9Sstevel@tonic-gate  */
5987c478bd9Sstevel@tonic-gate static void
5997c478bd9Sstevel@tonic-gate trapstat_load_tlb(void)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate #ifndef sun4v
6027c478bd9Sstevel@tonic-gate 	int i;
6037c478bd9Sstevel@tonic-gate #else
6047c478bd9Sstevel@tonic-gate 	uint64_t ret;
6057c478bd9Sstevel@tonic-gate #endif
6067c478bd9Sstevel@tonic-gate 	tte_t tte;
6077c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
6087c478bd9Sstevel@tonic-gate 	caddr_t va = tcpu->tcpu_vabase;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
6117c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate #ifndef sun4v
6147c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) {
6157c478bd9Sstevel@tonic-gate 		tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(TTE8K) |
6167c478bd9Sstevel@tonic-gate 		    TTE_PFN_INTHI(tcpu->tcpu_pfn[i]);
6177c478bd9Sstevel@tonic-gate 		if (i < TSTAT_INSTR_PAGES) {
6187c478bd9Sstevel@tonic-gate 			tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) |
6197c478bd9Sstevel@tonic-gate 			    TTE_LCK_INT | TTE_CP_INT | TTE_PRIV_INT;
6201e2e7a75Shuah 			sfmmu_itlb_ld_kva(va, &tte);
6217c478bd9Sstevel@tonic-gate 		} else {
6227c478bd9Sstevel@tonic-gate 			tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) |
6237c478bd9Sstevel@tonic-gate 			    TTE_LCK_INT | TTE_CP_INT | TTE_CV_INT |
6247c478bd9Sstevel@tonic-gate 			    TTE_PRIV_INT | TTE_HWWR_INT;
6251e2e7a75Shuah 			sfmmu_dtlb_ld_kva(va, &tte);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate #else /* sun4v */
6297c478bd9Sstevel@tonic-gate 	tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(tstat_pfn);
6307c478bd9Sstevel@tonic-gate 	tte.tte_intlo = TTE_PFN_INTLO(tstat_pfn) | TTE_CP_INT |
6317c478bd9Sstevel@tonic-gate 	    TTE_CV_INT | TTE_PRIV_INT | TTE_HWWR_INT |
6327c478bd9Sstevel@tonic-gate 	    TTE_SZ_INTLO(TTE4M);
6337c478bd9Sstevel@tonic-gate 	ret = hv_mmu_map_perm_addr(va, KCONTEXT, *(uint64_t *)&tte,
6347c478bd9Sstevel@tonic-gate 	    MAP_ITLB | MAP_DTLB);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (ret != H_EOK)
6377c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "trapstat: cannot map new TBA "
6387c478bd9Sstevel@tonic-gate 		    "for cpu %d  (error: 0x%lx)", CPU->cpu_id, ret);
6397c478bd9Sstevel@tonic-gate #endif /* sun4v */
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * As mentioned in the "TLB Statistics: TLB Misses versus TSB Misses" section
6447c478bd9Sstevel@tonic-gate  * of the block comment, TLB misses are differentiated from TSB misses in
6457c478bd9Sstevel@tonic-gate  * part by hot-patching the instructions at the tsbmiss patch points (see
6467c478bd9Sstevel@tonic-gate  * tstat_tsbmiss_patch_table). This routine is used both to initially patch
6477c478bd9Sstevel@tonic-gate  * the instructions, and to patch them back to their original values upon
6487c478bd9Sstevel@tonic-gate  * restoring the original trap table.
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate static void
6517c478bd9Sstevel@tonic-gate trapstat_hotpatch()
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	uint32_t instr;
6547c478bd9Sstevel@tonic-gate 	uint32_t simm13;
6557c478bd9Sstevel@tonic-gate 	tstat_tsbmiss_patch_entry_t *ep;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_TLBDATA))
6607c478bd9Sstevel@tonic-gate 		return;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (!tstat_tsbmiss_patched) {
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * We haven't patched the TSB paths; do so now.
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/
6677c478bd9Sstevel@tonic-gate 		ASSERT(offsetof(tstat_tlbret_t, ttlbr_ktsb) -
6687c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_ktlb) ==
6697c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_utsb) -
6707c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_utlb));
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		simm13 = offsetof(tstat_tlbret_t, ttlbr_ktsb) -
6737c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_ktlb);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) {
6767c478bd9Sstevel@tonic-gate 			ASSERT(ep->tpe_instr == 0);
6777c478bd9Sstevel@tonic-gate 			instr = ep->tpe_instr = *ep->tpe_addr;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 			/*
6807c478bd9Sstevel@tonic-gate 			 * Assert that the instruction we're about to patch is
6817c478bd9Sstevel@tonic-gate 			 * "add %g7, 0, %g7" (0x8e01e000).
6827c478bd9Sstevel@tonic-gate 			 */
6837c478bd9Sstevel@tonic-gate 			ASSERT(instr == TSTAT_TSBMISS_INSTR);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			instr |= simm13;
6867c478bd9Sstevel@tonic-gate 			hot_patch_kernel_text((caddr_t)ep->tpe_addr,
6877c478bd9Sstevel@tonic-gate 			    instr, sizeof (instr));
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		tstat_tsbmiss_patched = 1;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	} else {
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		 * Remove patches from the TSB paths.
6957c478bd9Sstevel@tonic-gate 		 */
6967c478bd9Sstevel@tonic-gate 		for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) {
6977c478bd9Sstevel@tonic-gate 			ASSERT(ep->tpe_instr == TSTAT_TSBMISS_INSTR);
6987c478bd9Sstevel@tonic-gate 			hot_patch_kernel_text((caddr_t)ep->tpe_addr,
6997c478bd9Sstevel@tonic-gate 			    ep->tpe_instr, sizeof (instr));
7007c478bd9Sstevel@tonic-gate 			ep->tpe_instr = 0;
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		tstat_tsbmiss_patched = 0;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * This is the routine executed to clock the performance of the trap table,
7097c478bd9Sstevel@tonic-gate  * executed both before and after interposing on the trap table to attempt to
7107c478bd9Sstevel@tonic-gate  * determine probe effect.  The probe effect is used to adjust the "%tim"
7117c478bd9Sstevel@tonic-gate  * fields of trapstat's -t and -T output; we only use TLB misses to clock the
7127c478bd9Sstevel@tonic-gate  * trap table.  We execute the inner loop (which is designed to exceed the
7137c478bd9Sstevel@tonic-gate  * TLB's reach) nlaps times, taking the best time as our time (thereby
7147c478bd9Sstevel@tonic-gate  * factoring out the effects of interrupts, cache misses or other perturbing
7157c478bd9Sstevel@tonic-gate  * events.
7167c478bd9Sstevel@tonic-gate  */
7177c478bd9Sstevel@tonic-gate static hrtime_t
7187c478bd9Sstevel@tonic-gate trapstat_probe_laps(int nlaps, hrtime_t *buf)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	int i, j = 0;
7217c478bd9Sstevel@tonic-gate 	hrtime_t ts, best = INT64_MAX;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	while (nlaps--) {
7247c478bd9Sstevel@tonic-gate 		ts = rdtick();
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSTAT_PROBE_SIZE; i += MMU_PAGESIZE)
7277c478bd9Sstevel@tonic-gate 			*((volatile char *)&tstat_probe_area[i]);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		if ((ts = rdtick() - ts) < best)
7307c478bd9Sstevel@tonic-gate 			best = ts;
7317c478bd9Sstevel@tonic-gate 		buf[j++] = ts;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	return (best);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate  * This routine determines the probe effect by calling trapstat_probe_laps()
7397c478bd9Sstevel@tonic-gate  * both without and with the interposing trap table.  Note that this is
7407c478bd9Sstevel@tonic-gate  * called from a cross call on the desired CPU, and that it is called on
7417c478bd9Sstevel@tonic-gate  * every CPU (this is necessary because the probe effect may differ from
7427c478bd9Sstevel@tonic-gate  * one CPU to another).
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate static void
7457c478bd9Sstevel@tonic-gate trapstat_probe()
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
7487c478bd9Sstevel@tonic-gate 	hrtime_t before, after;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED))
7517c478bd9Sstevel@tonic-gate 		return;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (tstat_probe_area == NULL || (tstat_options & TSTAT_OPT_NOGO))
7547c478bd9Sstevel@tonic-gate 		return;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * We very much expect the %tba to be KERNELBASE; this is a
7587c478bd9Sstevel@tonic-gate 	 * precautionary measure to assure that trapstat doesn't melt the
7597c478bd9Sstevel@tonic-gate 	 * machine should the %tba point unexpectedly elsewhere.
7607c478bd9Sstevel@tonic-gate 	 */
7617c478bd9Sstevel@tonic-gate 	if (get_tba() != (caddr_t)KERNELBASE)
7627c478bd9Sstevel@tonic-gate 		return;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * Preserve this CPU's data before destroying it by enabling the
7667c478bd9Sstevel@tonic-gate 	 * interposing trap table.  We can safely use tstat_buffer because
7677c478bd9Sstevel@tonic-gate 	 * the caller of the trapstat_probe() cross call is holding tstat_lock.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	tstat_probe_time = gethrtime();
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	before = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_before);
7747c478bd9Sstevel@tonic-gate 	(void) set_tba(tcpu->tcpu_ibase);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	after = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_after);
7777c478bd9Sstevel@tonic-gate 	(void) set_tba((caddr_t)KERNELBASE);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	tstat_probe_time = gethrtime() - tstat_probe_time;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size);
7827c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data->tdata_peffect = (after - before) / TSTAT_PROBE_NPAGES;
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate static void
7867c478bd9Sstevel@tonic-gate trapstat_probe_alloc()
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	pfn_t pfn;
7897c478bd9Sstevel@tonic-gate 	caddr_t va;
7907c478bd9Sstevel@tonic-gate 	int i;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
7937c478bd9Sstevel@tonic-gate 	ASSERT(tstat_probe_area == NULL);
7947c478bd9Sstevel@tonic-gate 	ASSERT(tstat_probe_phys == NULL);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_TLBDATA))
7977c478bd9Sstevel@tonic-gate 		return;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * Grab some virtual from the heap arena.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	tstat_probe_area = vmem_alloc(heap_arena, TSTAT_PROBE_SIZE, VM_SLEEP);
8037c478bd9Sstevel@tonic-gate 	va = tstat_probe_area;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * Grab a single physical page.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	tstat_probe_phys = vmem_alloc(tstat_arena, MMU_PAGESIZE, VM_SLEEP);
8097c478bd9Sstevel@tonic-gate 	pfn = hat_getpfnum(kas.a_hat, tstat_probe_phys);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * Now set the translation for every page in our virtual range
8137c478bd9Sstevel@tonic-gate 	 * to be our allocated physical page.
8147c478bd9Sstevel@tonic-gate 	 */
8157c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_PROBE_NPAGES; i++) {
8167c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, va, MMU_PAGESIZE, pfn, PROT_READ,
8177c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
8187c478bd9Sstevel@tonic-gate 		va += MMU_PAGESIZE;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate static void
8237c478bd9Sstevel@tonic-gate trapstat_probe_free()
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	caddr_t va;
8267c478bd9Sstevel@tonic-gate 	int i;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if ((va = tstat_probe_area) == NULL)
8317c478bd9Sstevel@tonic-gate 		return;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_PROBE_NPAGES; i++) {
8347c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, va, MMU_PAGESIZE, HAT_UNLOAD_UNLOCK);
8357c478bd9Sstevel@tonic-gate 		va += MMU_PAGESIZE;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tstat_probe_phys, MMU_PAGESIZE);
8397c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, tstat_probe_area, TSTAT_PROBE_SIZE);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	tstat_probe_phys = NULL;
8427c478bd9Sstevel@tonic-gate 	tstat_probe_area = NULL;
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * This routine actually enables a CPU by setting its %tba to be the
8477c478bd9Sstevel@tonic-gate  * CPU's interposing trap table.  It is called out of cross call context.
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate static void
8507c478bd9Sstevel@tonic-gate trapstat_enable()
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED))
8557c478bd9Sstevel@tonic-gate 		return;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
8587c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (get_tba() != (caddr_t)KERNELBASE)
8617c478bd9Sstevel@tonic-gate 		return;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_NOGO))
8647c478bd9Sstevel@tonic-gate 		(void) set_tba(tcpu->tcpu_ibase);
8657c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags |= TSTAT_CPU_ENABLED;
8667c478bd9Sstevel@tonic-gate #ifdef sun4v
867ce0352ebSgirish 	if ((tstat_options & TSTAT_OPT_TLBDATA) &&
868ce0352ebSgirish 	    !(tstat_options & TSTAT_OPT_NOGO)) {
869ce0352ebSgirish 		if (tstat_fast_tlbstat) {
8707c478bd9Sstevel@tonic-gate 			/*
871ce0352ebSgirish 			 * Invoke processor specific interface to enable
872ce0352ebSgirish 			 * collection of TSB hit statistics.
873ce0352ebSgirish 			 */
874ce0352ebSgirish 			cpu_trapstat_conf(CPU_TSTATCONF_ENABLE);
875ce0352ebSgirish 		} else {
876ce0352ebSgirish 			/*
877ce0352ebSgirish 			 * Collect TLB miss statistics by taking over
878ce0352ebSgirish 			 * TLB miss handling from the hypervisor. This
879ce0352ebSgirish 			 * is done by telling the hypervisor that there
880ce0352ebSgirish 			 * is no TSB configured. Also set TSTAT_TLB_STATS
881ce0352ebSgirish 			 * flag so that no user TSB is configured during
882ce0352ebSgirish 			 * context switch time.
8837c478bd9Sstevel@tonic-gate 			 */
8847c478bd9Sstevel@tonic-gate 			cpu_t *cp = CPU;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 			cp->cpu_m.cpu_tstat_flags |= TSTAT_TLB_STATS;
8877c478bd9Sstevel@tonic-gate 			(void) hv_set_ctx0(NULL, NULL);
8887c478bd9Sstevel@tonic-gate 			(void) hv_set_ctxnon0(NULL, NULL);
8897c478bd9Sstevel@tonic-gate 		}
890ce0352ebSgirish 	}
8917c478bd9Sstevel@tonic-gate #endif
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * This routine disables a CPU (vis a vis trapstat) by setting its %tba to be
8967c478bd9Sstevel@tonic-gate  * the actual, underlying trap table.  It is called out of cross call context.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate static void
8997c478bd9Sstevel@tonic-gate trapstat_disable()
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
9047c478bd9Sstevel@tonic-gate 		return;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
9077c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_NOGO))
9107c478bd9Sstevel@tonic-gate 		(void) set_tba((caddr_t)KERNELBASE);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate #ifdef sun4v
915ce0352ebSgirish 	if ((tstat_options & TSTAT_OPT_TLBDATA) &&
916ce0352ebSgirish 	    !(tstat_options & TSTAT_OPT_NOGO)) {
917ce0352ebSgirish 		if (tstat_fast_tlbstat) {
9187c478bd9Sstevel@tonic-gate 			/*
919ce0352ebSgirish 			 * Invoke processor specific interface to disable
920ce0352ebSgirish 			 * collection of TSB hit statistics on each processor.
921ce0352ebSgirish 			 */
922ce0352ebSgirish 			cpu_trapstat_conf(CPU_TSTATCONF_DISABLE);
923ce0352ebSgirish 		} else {
924ce0352ebSgirish 			/*
925ce0352ebSgirish 			 * As part of collecting TLB miss statistics, we took
926ce0352ebSgirish 			 * over TLB miss handling from the hypervisor by
927ce0352ebSgirish 			 * telling the hypervisor that NO TSB is configured.
928ce0352ebSgirish 			 * We need to restore that by communicating proper
929ce0352ebSgirish 			 * kernel/user TSB information so that TLB misses
930ce0352ebSgirish 			 * can be handled by the hypervisor or the hardware
931ce0352ebSgirish 			 * more efficiently.
9327c478bd9Sstevel@tonic-gate 			 *
933ce0352ebSgirish 			 * We restore kernel TSB information right away.
934ce0352ebSgirish 			 * However, to minimize any locking dependency, we
935ce0352ebSgirish 			 * don't restore user TSB information right away.
936ce0352ebSgirish 			 * Instead, we simply clear the TSTAT_TLB_STATS flag
937ce0352ebSgirish 			 * so that the user TSB information is automatically
938ce0352ebSgirish 			 * restored on next context switch.
9397c478bd9Sstevel@tonic-gate 			 *
9407c478bd9Sstevel@tonic-gate 			 * Note that the call to restore kernel TSB information
9417c478bd9Sstevel@tonic-gate 			 * will normally not fail, unless wrong information is
9427c478bd9Sstevel@tonic-gate 			 * passed here. In that scenario, system will still
9437c478bd9Sstevel@tonic-gate 			 * continue to function properly with the exception of
9447c478bd9Sstevel@tonic-gate 			 * kernel handling all the TLB misses.
9457c478bd9Sstevel@tonic-gate 			 */
9467c478bd9Sstevel@tonic-gate 			struct hv_tsb_block *hvbp = &ksfmmup->sfmmu_hvblock;
9477c478bd9Sstevel@tonic-gate 			cpu_t *cp = CPU;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 			cp->cpu_m.cpu_tstat_flags &= ~TSTAT_TLB_STATS;
950ce0352ebSgirish 			(void) hv_set_ctx0(hvbp->hv_tsb_info_cnt,
951ce0352ebSgirish 			    hvbp->hv_tsb_info_pa);
952ce0352ebSgirish 		}
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate #endif
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate  * We use %tick as the time base when recording the time spent executing
9597c478bd9Sstevel@tonic-gate  * the trap handler.  %tick, however, is not necessarily kept in sync
9607c478bd9Sstevel@tonic-gate  * across CPUs (indeed, different CPUs may have different %tick frequencies).
9617c478bd9Sstevel@tonic-gate  * We therefore cross call onto a CPU to get a snapshot of its data to
9627c478bd9Sstevel@tonic-gate  * copy out; this is the routine executed out of that cross call.
9637c478bd9Sstevel@tonic-gate  */
9647c478bd9Sstevel@tonic-gate static void
9657c478bd9Sstevel@tonic-gate trapstat_snapshot()
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
9687c478bd9Sstevel@tonic-gate 	tstat_data_t *data = tcpu->tcpu_data;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
9717c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
9727c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ENABLED);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	data->tdata_snapts = gethrtime();
9757c478bd9Sstevel@tonic-gate 	data->tdata_snaptick = rdtick();
9767c478bd9Sstevel@tonic-gate 	bcopy(data, tstat_buffer, tstat_data_t_size);
977ce0352ebSgirish #ifdef sun4v
978ce0352ebSgirish 	/*
979ce0352ebSgirish 	 * Invoke processor specific interface to collect TSB hit
980ce0352ebSgirish 	 * statistics on each processor.
981ce0352ebSgirish 	 */
982ce0352ebSgirish 	if ((tstat_options & TSTAT_OPT_TLBDATA) && tstat_fast_tlbstat)
983ce0352ebSgirish 		cpu_trapstat_data((void *) tstat_buffer->tdata_pgsz,
984ce0352ebSgirish 		    tstat_pgszs);
985ce0352ebSgirish #endif
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * The TSTAT_RETENT_* constants define offsets in the TLB return entry.
9907c478bd9Sstevel@tonic-gate  * They are used only in trapstat_tlbretent() (below) and #undef'd
9917c478bd9Sstevel@tonic-gate  * immediately afterwards.  Any change to "retent" in trapstat_tlbretent()
9927c478bd9Sstevel@tonic-gate  * will likely require changes to these constants.
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate #ifndef sun4v
9967c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATHI	1
9977c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATLO	2
9981bd453f3Ssusans #define	TSTAT_RETENT_SHIFT	11
9991bd453f3Ssusans #define	TSTAT_RETENT_COUNT_LD	13
10001bd453f3Ssusans #define	TSTAT_RETENT_COUNT_ST	15
10011bd453f3Ssusans #define	TSTAT_RETENT_TMPTSHI	16
10021bd453f3Ssusans #define	TSTAT_RETENT_TMPTSLO	17
10031bd453f3Ssusans #define	TSTAT_RETENT_TIME_LD	19
10041bd453f3Ssusans #define	TSTAT_RETENT_TIME_ST	21
10057c478bd9Sstevel@tonic-gate #else /* sun4v */
100659ac0c16Sdavemq #define	TSTAT_RETENT_TDATASHFT	2
100759ac0c16Sdavemq #define	TSTAT_RETENT_STATHI	4
100859ac0c16Sdavemq #define	TSTAT_RETENT_STATLO	6
100959ac0c16Sdavemq #define	TSTAT_RETENT_SHIFT	9
101059ac0c16Sdavemq #define	TSTAT_RETENT_COUNT_LD	11
101159ac0c16Sdavemq #define	TSTAT_RETENT_COUNT_ST	13
101259ac0c16Sdavemq #define	TSTAT_RETENT_TMPTSHI	14
101359ac0c16Sdavemq #define	TSTAT_RETENT_TMPTSLO	16
101459ac0c16Sdavemq #define	TSTAT_RETENT_TIME_LD	18
101559ac0c16Sdavemq #define	TSTAT_RETENT_TIME_ST	20
10167c478bd9Sstevel@tonic-gate #endif /* sun4v */
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate static void
10197c478bd9Sstevel@tonic-gate trapstat_tlbretent(tstat_percpu_t *tcpu, tstat_tlbretent_t *ret,
10207c478bd9Sstevel@tonic-gate     tstat_missdata_t *data)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	uint32_t *ent = ret->ttlbrent_instr, shift;
102359ac0c16Sdavemq 	uintptr_t base;
102459ac0c16Sdavemq #ifndef sun4v
102559ac0c16Sdavemq 	uintptr_t tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick);
102659ac0c16Sdavemq #else
102759ac0c16Sdavemq 	uintptr_t tmptick = TSTAT_CPU0_DATA_OFFS(tcpu, tdata_tmptick);
102859ac0c16Sdavemq #endif
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * This is the entry executed upon return from the TLB/TSB miss
10327c478bd9Sstevel@tonic-gate 	 * handler (i.e. the code interpositioned between the "retry" and
10337c478bd9Sstevel@tonic-gate 	 * the actual return to the TLB-missing instruction).  Detail on its
10347c478bd9Sstevel@tonic-gate 	 * theory of operation can be found in the "TLB Statistics" section
10357c478bd9Sstevel@tonic-gate 	 * of the block comment.  Note that we expect the TTE just loaded
10367c478bd9Sstevel@tonic-gate 	 * into the TLB to be in %g5; all other globals are available as
10377c478bd9Sstevel@tonic-gate 	 * scratch.  Finally, note that the page size information in sun4v is
10387c478bd9Sstevel@tonic-gate 	 * located in the lower bits of the TTE -- requiring us to have a
10397c478bd9Sstevel@tonic-gate 	 * different return entry on sun4v.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	static const uint32_t retent[TSTAT_TLBRET_NINSTR] = {
10427c478bd9Sstevel@tonic-gate #ifndef sun4v
10437c478bd9Sstevel@tonic-gate 	    0x87410000,		/* rd    %tick, %g3			*/
10447c478bd9Sstevel@tonic-gate 	    0x03000000, 	/* sethi %hi(stat), %g1			*/
10457c478bd9Sstevel@tonic-gate 	    0x82106000,		/* or    %g1, %lo(stat), %g1		*/
10467c478bd9Sstevel@tonic-gate 	    0x89297001,		/* sllx  %g5, 1, %g4			*/
10477c478bd9Sstevel@tonic-gate 	    0x8931303e,		/* srlx  %g4, 62, %g4			*/
10487c478bd9Sstevel@tonic-gate 	    0x8531702e,		/* srlx  %g5, 46, %g2			*/
10497c478bd9Sstevel@tonic-gate 	    0x8408a004,		/* and   %g2, 4, %g2			*/
10507c478bd9Sstevel@tonic-gate 	    0x88110002,		/* or    %g4, %g2, %g4			*/
10511bd453f3Ssusans 	    0x80a12005,		/* cmp   %g4, 5				*/
10521bd453f3Ssusans 	    0x34400002,		/* bg,a,pn %icc, +8			*/
10531bd453f3Ssusans 	    0x88102004,		/* mov   4, %g4				*/
10547c478bd9Sstevel@tonic-gate 	    0x89292000,		/* sll   %g4, shift, %g4		*/
10557c478bd9Sstevel@tonic-gate 	    0x82004004,		/* add   %g1, %g4, %g1			*/
10567c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_count], %g2	*/
10577c478bd9Sstevel@tonic-gate 	    0x8400a001,		/* add   %g2, 1, %g2			*/
10587c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_count]	*/
10597c478bd9Sstevel@tonic-gate 	    0x0d000000, 	/* sethi %hi(tdata_tmptick), %g6	*/
10607c478bd9Sstevel@tonic-gate 	    0xc459a000, 	/* ldx   [%g6 + %lo(tdata_tmptick)], %g2 */
10617c478bd9Sstevel@tonic-gate 	    0x8620c002,		/* sub   %g3, %g2, %g3			*/
10627c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_time], %g2	*/
10637c478bd9Sstevel@tonic-gate 	    0x84008003,		/* add   %g2, %g3, %g2			*/
10647c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_time]	*/
10657c478bd9Sstevel@tonic-gate 	    0x83f00000		/* retry				*/
10667c478bd9Sstevel@tonic-gate #else /* sun4v */
106759ac0c16Sdavemq 	    0x82102008,		/* mov   SCRATCHPAD_CPUID, %g1 		*/
106859ac0c16Sdavemq 	    0xced84400,		/* ldxa  [%g1]ASI_SCRATCHPAD, %g7	*/
106959ac0c16Sdavemq 	    0x8f29f000,		/* sllx  %g7, TSTAT_DATA_SHIFT, %g7	*/
10707c478bd9Sstevel@tonic-gate 	    0x87410000,		/* rd    %tick, %g3			*/
10717c478bd9Sstevel@tonic-gate 	    0x03000000, 	/* sethi %hi(stat), %g1			*/
107259ac0c16Sdavemq 	    0x82004007,		/* add   %g1, %g7, %g1			*/
10737c478bd9Sstevel@tonic-gate 	    0x82106000,		/* or    %g1, %lo(stat), %g1		*/
10747c478bd9Sstevel@tonic-gate 	    0x8929703d,		/* sllx  %g5, 61, %g4			*/
10757c478bd9Sstevel@tonic-gate 	    0x8931303d,		/* srlx  %g4, 61, %g4			*/
10767c478bd9Sstevel@tonic-gate 	    0x89292000,		/* sll   %g4, shift, %g4		*/
10777c478bd9Sstevel@tonic-gate 	    0x82004004,		/* add   %g1, %g4, %g1			*/
10787c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_count], %g2	*/
10797c478bd9Sstevel@tonic-gate 	    0x8400a001,		/* add   %g2, 1, %g2			*/
10807c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_count]	*/
10817c478bd9Sstevel@tonic-gate 	    0x0d000000, 	/* sethi %hi(tdata_tmptick), %g6	*/
108259ac0c16Sdavemq 	    0x8c018007,		/* add   %g6, %g7, %g6			*/
10837c478bd9Sstevel@tonic-gate 	    0xc459a000, 	/* ldx   [%g6 + %lo(tdata_tmptick)], %g2 */
10847c478bd9Sstevel@tonic-gate 	    0x8620c002,		/* sub   %g3, %g2, %g3			*/
10857c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_time], %g2	*/
10867c478bd9Sstevel@tonic-gate 	    0x84008003,		/* add   %g2, %g3, %g2			*/
10877c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_time]	*/
10887c478bd9Sstevel@tonic-gate 	    0x83f00000		/* retry				*/
10897c478bd9Sstevel@tonic-gate #endif /* sun4v */
10907c478bd9Sstevel@tonic-gate 	};
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
10937c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
10947c478bd9Sstevel@tonic-gate 	ASSERT(offsetof(tstat_missdata_t, tmiss_count) <= LO10(-1));
10957c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
10967c478bd9Sstevel@tonic-gate 	ASSERT(offsetof(tstat_missdata_t, tmiss_time) <= LO10(-1));
10977c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
10987c478bd9Sstevel@tonic-gate 	ASSERT(!((sizeof (tstat_pgszdata_t) - 1) & sizeof (tstat_pgszdata_t)));
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	for (shift = 1; (1 << shift) != sizeof (tstat_pgszdata_t); shift++)
11017c478bd9Sstevel@tonic-gate 		continue;
11027c478bd9Sstevel@tonic-gate 
110359ac0c16Sdavemq 	base = (uintptr_t)tcpu->tcpu_ibase + TSTAT_INSTR_SIZE +
11047c478bd9Sstevel@tonic-gate 	    ((uintptr_t)data - (uintptr_t)tcpu->tcpu_data);
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	bcopy(retent, ent, sizeof (retent));
11077c478bd9Sstevel@tonic-gate 
110859ac0c16Sdavemq #if defined(sun4v)
110959ac0c16Sdavemq 	ent[TSTAT_RETENT_TDATASHFT] |= LO10((uintptr_t)TSTAT_DATA_SHIFT);
111059ac0c16Sdavemq #endif
11117c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_STATHI] |= HI22(base);
11127c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_STATLO] |= LO10(base);
11137c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_SHIFT] |= shift;
11147c478bd9Sstevel@tonic-gate 	/* LINTED E_EXPR_NULL_EFFECT */
11157c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_COUNT_LD] |= offsetof(tstat_missdata_t, tmiss_count);
11167c478bd9Sstevel@tonic-gate 	/* LINTED E_EXPR_NULL_EFFECT */
11177c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_COUNT_ST] |= offsetof(tstat_missdata_t, tmiss_count);
11187c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TMPTSHI] |= HI22(tmptick);
11197c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TMPTSLO] |= LO10(tmptick);
11207c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TIME_LD] |= offsetof(tstat_missdata_t, tmiss_time);
11217c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TIME_ST] |= offsetof(tstat_missdata_t, tmiss_time);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
112459ac0c16Sdavemq #if defined(sun4v)
112559ac0c16Sdavemq #undef TSTAT_RETENT_TDATASHFT
112659ac0c16Sdavemq #endif
11277c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATHI
11287c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATLO
11297c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_SHIFT
11307c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_LD
11317c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_ST
11327c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSHI
11337c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSLO
11347c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_LD
11357c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_ST
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * The TSTAT_TLBENT_* constants define offsets in the TLB entry.  They are
11397c478bd9Sstevel@tonic-gate  * used only in trapstat_tlbent() (below) and #undef'd immediately afterwards.
11407c478bd9Sstevel@tonic-gate  * Any change to "tlbent" in trapstat_tlbent() will likely require changes
11417c478bd9Sstevel@tonic-gate  * to these constants.
11427c478bd9Sstevel@tonic-gate  */
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate #ifndef sun4v
11457c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATHI	0
11467c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_LD	1
11477c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_ST	3
11487c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_MMUASI	15
11497c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCHI	18
11507c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_USER	19
11517c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_KERN	21
11527c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSHI	25
11537c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSLO	27
11547c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_BA		28
11557c478bd9Sstevel@tonic-gate #else /* sun4v */
115659ac0c16Sdavemq #define	TSTAT_TLBENT_TDATASHFT	2
115759ac0c16Sdavemq #define	TSTAT_TLBENT_STATHI	3
115859ac0c16Sdavemq #define	TSTAT_TLBENT_STATLO_LD	5
115959ac0c16Sdavemq #define	TSTAT_TLBENT_STATLO_ST	7
116059ac0c16Sdavemq #define	TSTAT_TLBENT_TAGTARGET	23
116159ac0c16Sdavemq #define	TSTAT_TLBENT_TPCHI	25
116259ac0c16Sdavemq #define	TSTAT_TLBENT_TPCLO_USER	26
116359ac0c16Sdavemq #define	TSTAT_TLBENT_TPCLO_KERN	28
116459ac0c16Sdavemq #define	TSTAT_TLBENT_TSHI	32
116559ac0c16Sdavemq #define	TSTAT_TLBENT_TSLO	35
116659ac0c16Sdavemq #define	TSTAT_TLBENT_BA		36
11677c478bd9Sstevel@tonic-gate #endif /* sun4v */
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate static void
11707c478bd9Sstevel@tonic-gate trapstat_tlbent(tstat_percpu_t *tcpu, int entno)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	uint32_t *ent;
11737c478bd9Sstevel@tonic-gate 	uintptr_t orig, va, baoffs;
1174ce0352ebSgirish #ifndef sun4v
11757c478bd9Sstevel@tonic-gate 	int itlb = entno == TSTAT_ENT_ITLBMISS;
117659ac0c16Sdavemq 	uint32_t asi = itlb ? ASI(ASI_IMMU) : ASI(ASI_DMMU);
1177ce0352ebSgirish #else
1178ce0352ebSgirish 	int itlb = (entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_ITLBMISS);
117959ac0c16Sdavemq 	uint32_t tagtarget_off = itlb ? MMFSA_I_CTX : MMFSA_D_CTX;
118059ac0c16Sdavemq 	uint32_t *tent;			/* MMU trap vector entry */
118159ac0c16Sdavemq 	uintptr_t tentva;		/* MMU trap vector entry va */
118259ac0c16Sdavemq 	static const uint32_t mmumiss[TSTAT_ENT_NINSTR] = {
118359ac0c16Sdavemq 	    0x30800000,			/* ba,a addr */
118459ac0c16Sdavemq 	    NOP, NOP, NOP, NOP, NOP, NOP, NOP
118559ac0c16Sdavemq 	};
1186ce0352ebSgirish #endif
11877c478bd9Sstevel@tonic-gate 	int entoffs = entno << TSTAT_ENT_SHIFT;
11887c478bd9Sstevel@tonic-gate 	uintptr_t tmptick, stat, tpc, utpc;
11897c478bd9Sstevel@tonic-gate 	tstat_pgszdata_t *data = &tcpu->tcpu_data->tdata_pgsz[0];
11907c478bd9Sstevel@tonic-gate 	tstat_tlbdata_t *udata, *kdata;
11917c478bd9Sstevel@tonic-gate 	tstat_tlbret_t *ret;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	/*
11947c478bd9Sstevel@tonic-gate 	 * When trapstat is run with TLB statistics, this is the entry for
11957c478bd9Sstevel@tonic-gate 	 * both I- and D-TLB misses; this code performs trap level pushing,
11967c478bd9Sstevel@tonic-gate 	 * as described in the "TLB Statistics" section of the block comment.
11977c478bd9Sstevel@tonic-gate 	 * This code is executing at TL 1; %tstate[0] contains the saved
11987c478bd9Sstevel@tonic-gate 	 * state at the time of the TLB miss.  Pushing trap level 1 (and thus
11997c478bd9Sstevel@tonic-gate 	 * raising TL to 2) requires us to fill in %tstate[1] with our %pstate,
12007c478bd9Sstevel@tonic-gate 	 * %cwp and %asi.  We leave %tt unchanged, and we set %tpc and %tnpc to
12017c478bd9Sstevel@tonic-gate 	 * the appropriate TLB return entry (based on the context of the miss).
12027c478bd9Sstevel@tonic-gate 	 * Finally, we sample %tick, and stash it in the tdata_tmptick member
12037c478bd9Sstevel@tonic-gate 	 * the per-CPU tstat_data structure.  tdata_tmptick will be used in
12047c478bd9Sstevel@tonic-gate 	 * the TLB return entry to determine the amount of time spent in the
12057c478bd9Sstevel@tonic-gate 	 * TLB miss handler.
12067c478bd9Sstevel@tonic-gate 	 *
1207bd46b14cSgirish 	 * Note that on sun4v platforms, we must obtain the context information
1208bd46b14cSgirish 	 * from the MMU fault status area. (The base address of this MMU fault
1209bd46b14cSgirish 	 * status area is kept in the scratchpad register 0.)
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	static const uint32_t tlbent[] = {
12127c478bd9Sstevel@tonic-gate #ifndef sun4v
12137c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
12147c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
12157c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
12167c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
12177c478bd9Sstevel@tonic-gate 	    0x85524000,			/* rdpr  %cwp, %g2		*/
12187c478bd9Sstevel@tonic-gate 	    0x87518000,			/* rdpr  %pstate, %g3		*/
12197c478bd9Sstevel@tonic-gate 	    0x8728f008,			/* sllx  %g3, 8, %g3		*/
12207c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
12217c478bd9Sstevel@tonic-gate 	    0x8740c000,			/* rd    %asi, %g3		*/
12227c478bd9Sstevel@tonic-gate 	    0x8728f018,			/* sllx  %g3, 24, %g3		*/
12237c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
12247c478bd9Sstevel@tonic-gate 	    0x8350c000,			/* rdpr  %tt, %g1		*/
12257c478bd9Sstevel@tonic-gate 	    0x8f902002,			/* wrpr  %g0, 2, %tl		*/
12267c478bd9Sstevel@tonic-gate 	    0x85908000,			/* wrpr  %g2, %g0, %tstate	*/
12277c478bd9Sstevel@tonic-gate 	    0x87904000,			/* wrpr  %g1, %g0, %tt		*/
12287c478bd9Sstevel@tonic-gate 	    0xc2d80000,			/* ldxa  [%g0]ASI_MMU, %g1	*/
12297c478bd9Sstevel@tonic-gate 	    0x83307030,			/* srlx  %g1, CTXSHIFT, %g1	*/
12307c478bd9Sstevel@tonic-gate 	    0x02c04004,			/* brz,pn %g1, .+0x10		*/
12317c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(new_tpc), %g1	*/
12327c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
12337c478bd9Sstevel@tonic-gate 	    0x30800002,			/* ba,a  .+0x8			*/
12347c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
12357c478bd9Sstevel@tonic-gate 	    0x81904000,			/* wrpr  %g1, %g0, %tpc		*/
12367c478bd9Sstevel@tonic-gate 	    0x82006004,			/* add   %g1, 4, %g1		*/
12377c478bd9Sstevel@tonic-gate 	    0x83904000,			/* wrpr  %g1, %g0, %tnpc	*/
12387c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(tmptick), %g1	*/
12397c478bd9Sstevel@tonic-gate 	    0x85410000,			/* rd    %tick, %g2		*/
12407c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(tmptick)] */
12417c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a  addr			*/
12427c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP
12437c478bd9Sstevel@tonic-gate #else /* sun4v */
124459ac0c16Sdavemq 	    0x82102008,			/* mov SCRATCHPAD_CPUID, %g1	*/
124559ac0c16Sdavemq 	    0xc8d84400,			/* ldxa [%g1]ASI_SCRATCHPAD, %g4 */
124659ac0c16Sdavemq 	    0x89293000,			/* sllx %g4, TSTAT_DATA_SHIFT, %g4 */
12477c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
124859ac0c16Sdavemq 	    0x82004004,			/* add %g1, %g4, %g1		*/
12497c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
12507c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
12517c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
12527c478bd9Sstevel@tonic-gate 	    0x85524000,			/* rdpr  %cwp, %g2		*/
12537c478bd9Sstevel@tonic-gate 	    0x87518000,			/* rdpr  %pstate, %g3		*/
12547c478bd9Sstevel@tonic-gate 	    0x8728f008,			/* sllx  %g3, 8, %g3		*/
12557c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
12567c478bd9Sstevel@tonic-gate 	    0x8740c000,			/* rd    %asi, %g3		*/
12577c478bd9Sstevel@tonic-gate 	    0x8728f018,			/* sllx  %g3, 24, %g3		*/
1258bd46b14cSgirish 	    0x83540000,			/* rdpr  %gl, %g1		*/
1259bd46b14cSgirish 	    0x83287028,			/* sllx  %g1, 40, %g1		*/
1260bd46b14cSgirish 	    0x86104003,			/* or    %g1, %g3, %g3		*/
12617c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
12627c478bd9Sstevel@tonic-gate 	    0x8350c000,			/* rdpr  %tt, %g1		*/
12637c478bd9Sstevel@tonic-gate 	    0x8f902002,			/* wrpr  %g0, 2, %tl		*/
12647c478bd9Sstevel@tonic-gate 	    0x85908000,			/* wrpr  %g2, %g0, %tstate	*/
12657c478bd9Sstevel@tonic-gate 	    0x87904000,			/* wrpr  %g1, %g0, %tt		*/
12667c478bd9Sstevel@tonic-gate 	    0xc2d80400,			/* ldxa  [%g0]ASI_SCRATCHPAD, %g1 */
12677c478bd9Sstevel@tonic-gate 	    0xc2586000,			/* ldx  [%g1 + MMFSA_?_CTX], %g1 */
12687c478bd9Sstevel@tonic-gate 	    0x02c04004,			/* brz,pn %g1, .+0x10		*/
12697c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(new_tpc), %g1	*/
12707c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
12717c478bd9Sstevel@tonic-gate 	    0x30800002,			/* ba,a  .+0x8			*/
12727c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
12737c478bd9Sstevel@tonic-gate 	    0x81904000,			/* wrpr  %g1, %g0, %tpc		*/
12747c478bd9Sstevel@tonic-gate 	    0x82006004,			/* add   %g1, 4, %g1		*/
12757c478bd9Sstevel@tonic-gate 	    0x83904000,			/* wrpr  %g1, %g0, %tnpc	*/
12767c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(tmptick), %g1	*/
127759ac0c16Sdavemq 	    0x82004004,			/* add %g1, %g4, %g1		*/
12787c478bd9Sstevel@tonic-gate 	    0x85410000,			/* rd    %tick, %g2		*/
12797c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(tmptick)] */
12807c478bd9Sstevel@tonic-gate 	    0x30800000			/* ba,a  addr			*/
12817c478bd9Sstevel@tonic-gate #endif /* sun4v */
12827c478bd9Sstevel@tonic-gate 	};
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1285ce0352ebSgirish #ifndef sun4v
12867c478bd9Sstevel@tonic-gate 	ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS);
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	stat = TSTAT_DATA_OFFS(tcpu, tdata_traps) + entoffs;
12897c478bd9Sstevel@tonic-gate 	tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick);
129059ac0c16Sdavemq #else /* sun4v */
129159ac0c16Sdavemq 	ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS ||
129259ac0c16Sdavemq 	    entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_DMMUMISS);
129359ac0c16Sdavemq 
129459ac0c16Sdavemq 	stat = TSTAT_CPU0_DATA_OFFS(tcpu, tdata_traps) + entoffs;
129559ac0c16Sdavemq 	tmptick = TSTAT_CPU0_DATA_OFFS(tcpu, tdata_tmptick);
129659ac0c16Sdavemq #endif /* sun4v */
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	if (itlb) {
12997c478bd9Sstevel@tonic-gate 		ret = &tcpu->tcpu_instr->tinst_itlbret;
13007c478bd9Sstevel@tonic-gate 		udata = &data->tpgsz_user.tmode_itlb;
13017c478bd9Sstevel@tonic-gate 		kdata = &data->tpgsz_kernel.tmode_itlb;
13027c478bd9Sstevel@tonic-gate 		tpc = TSTAT_INSTR_OFFS(tcpu, tinst_itlbret.ttlbr_ktlb);
13037c478bd9Sstevel@tonic-gate 	} else {
13047c478bd9Sstevel@tonic-gate 		ret = &tcpu->tcpu_instr->tinst_dtlbret;
13057c478bd9Sstevel@tonic-gate 		udata = &data->tpgsz_user.tmode_dtlb;
13067c478bd9Sstevel@tonic-gate 		kdata = &data->tpgsz_kernel.tmode_dtlb;
13077c478bd9Sstevel@tonic-gate 		tpc = TSTAT_INSTR_OFFS(tcpu, tinst_dtlbret.ttlbr_ktlb);
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	utpc = tpc + offsetof(tstat_tlbret_t, ttlbr_utlb) -
13117c478bd9Sstevel@tonic-gate 	    offsetof(tstat_tlbret_t, ttlbr_ktlb);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	ASSERT(HI22(tpc) == HI22(utpc));
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	ent = (uint32_t *)((uintptr_t)tcpu->tcpu_instr + entoffs);
13167c478bd9Sstevel@tonic-gate 	orig = KERNELBASE + entoffs;
13177c478bd9Sstevel@tonic-gate 	va = (uintptr_t)tcpu->tcpu_ibase + entoffs;
13187c478bd9Sstevel@tonic-gate 	baoffs = TSTAT_TLBENT_BA * sizeof (uint32_t);
13197c478bd9Sstevel@tonic-gate 
1320ce0352ebSgirish #ifdef sun4v
1321ce0352ebSgirish 	/*
132259ac0c16Sdavemq 	 * Because of lack of space, interposing tlbent trap handler
132359ac0c16Sdavemq 	 * for TLB and MMU miss traps cannot be placed in-line. Instead,
132459ac0c16Sdavemq 	 * we copy it to the space set aside for shared trap handlers
132559ac0c16Sdavemq 	 * continuation in the interposing trap table and invoke it by
132659ac0c16Sdavemq 	 * placing a branch in the trap table itself.
1327ce0352ebSgirish 	 */
132859ac0c16Sdavemq 	tent = ent;		/* trap vector entry */
132959ac0c16Sdavemq 	tentva = va;		/* trap vector entry va */
1330ce0352ebSgirish 
1331ce0352ebSgirish 	if (itlb) {
1332ce0352ebSgirish 		ent = (uint32_t *)((uintptr_t)
1333ce0352ebSgirish 		    &tcpu->tcpu_instr->tinst_immumiss);
1334ce0352ebSgirish 		va = TSTAT_INSTR_OFFS(tcpu, tinst_immumiss);
1335ce0352ebSgirish 	} else {
1336ce0352ebSgirish 		ent = (uint32_t *)((uintptr_t)
1337ce0352ebSgirish 		    &tcpu->tcpu_instr->tinst_dmmumiss);
1338ce0352ebSgirish 		va = TSTAT_INSTR_OFFS(tcpu, tinst_dmmumiss);
1339ce0352ebSgirish 	}
1340ce0352ebSgirish 	bcopy(mmumiss, tent, sizeof (mmumiss));
1341ce0352ebSgirish 	tent[0] |= DISP22(tentva, va);
1342ce0352ebSgirish #endif /* sun4v */
1343ce0352ebSgirish 
13447c478bd9Sstevel@tonic-gate 	bcopy(tlbent, ent, sizeof (tlbent));
13457c478bd9Sstevel@tonic-gate 
134659ac0c16Sdavemq #if defined(sun4v)
134759ac0c16Sdavemq 	ent[TSTAT_TLBENT_TDATASHFT] |= LO10((uintptr_t)TSTAT_DATA_SHIFT);
134859ac0c16Sdavemq #endif
13497c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATHI] |= HI22(stat);
13507c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATLO_LD] |= LO10(stat);
13517c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATLO_ST] |= LO10(stat);
13527c478bd9Sstevel@tonic-gate #ifndef sun4v
13537c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_MMUASI] |= asi;
13547c478bd9Sstevel@tonic-gate #else
13557c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TAGTARGET] |= tagtarget_off;
13567c478bd9Sstevel@tonic-gate #endif
13577c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCHI] |= HI22(tpc);
13587c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCLO_USER] |= LO10(utpc);
13597c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCLO_KERN] |= LO10(tpc);
13607c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TSHI] |= HI22(tmptick);
13617c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TSLO] |= LO10(tmptick);
13627c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_BA] |= DISP22(va + baoffs, orig);
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	/*
13657c478bd9Sstevel@tonic-gate 	 * And now set up the TLB return entries.
13667c478bd9Sstevel@tonic-gate 	 */
13677c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_ktlb, &kdata->ttlb_tlb);
13687c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_ktsb, &kdata->ttlb_tsb);
13697c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_utlb, &udata->ttlb_tlb);
13707c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_utsb, &udata->ttlb_tsb);
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
137359ac0c16Sdavemq #if defined(sun4v)
137459ac0c16Sdavemq #undef TSTAT_TLBENT_TDATASHFT
137559ac0c16Sdavemq #endif
13767c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATHI
13777c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_LD
13787c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_ST
13797c478bd9Sstevel@tonic-gate #ifndef sun4v
13807c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_MMUASI
13817c478bd9Sstevel@tonic-gate #else
13827c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TAGTARGET
13837c478bd9Sstevel@tonic-gate #endif
13847c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCHI
13857c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_USER
13867c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_KERN
13877c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSHI
13887c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSLO
13897c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_BA
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate /*
13927c478bd9Sstevel@tonic-gate  * The TSTAT_ENABLED_* constants define offsets in the enabled entry; the
13937c478bd9Sstevel@tonic-gate  * TSTAT_DISABLED_BA constant defines an offset in the disabled entry.  Both
13947c478bd9Sstevel@tonic-gate  * sets of constants are used only in trapstat_make_traptab() (below) and
13957c478bd9Sstevel@tonic-gate  * #undef'd immediately afterwards.  Any change to "enabled" or "disabled"
13967c478bd9Sstevel@tonic-gate  * in trapstat_make_traptab() will likely require changes to these constants.
13977c478bd9Sstevel@tonic-gate  */
139859ac0c16Sdavemq #ifndef sun4v
13997c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATHI	0
14007c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATLO_LD	1
14017c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATLO_ST 3
14027c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_BA	4
14037c478bd9Sstevel@tonic-gate #define	TSTAT_DISABLED_BA	0
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate static void
14067c478bd9Sstevel@tonic-gate trapstat_make_traptab(tstat_percpu_t *tcpu)
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate 	uint32_t *ent;
14097c478bd9Sstevel@tonic-gate 	uint64_t *stat;
14107c478bd9Sstevel@tonic-gate 	uintptr_t orig, va, en_baoffs, dis_baoffs;
14117c478bd9Sstevel@tonic-gate 	int nent;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * This is the entry in the interposing trap table for enabled trap
14157c478bd9Sstevel@tonic-gate 	 * table entries.  It loads a counter, increments it and stores it
14167c478bd9Sstevel@tonic-gate 	 * back before branching to the actual trap table entry.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	static const uint32_t enabled[TSTAT_ENT_NINSTR] = {
14197c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
14207c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
14217c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
14227c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
14237c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a addr			*/
14247c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP
14257c478bd9Sstevel@tonic-gate 	};
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	/*
14287c478bd9Sstevel@tonic-gate 	 * This is the entry in the interposing trap table for disabled trap
14297c478bd9Sstevel@tonic-gate 	 * table entries.  It simply branches to the actual, underlying trap
14307c478bd9Sstevel@tonic-gate 	 * table entry.  As explained in the "Implementation Details" section
14317c478bd9Sstevel@tonic-gate 	 * of the block comment, all TL>0 traps _must_ use the disabled entry;
14327c478bd9Sstevel@tonic-gate 	 * additional entries may be explicitly disabled through the use
14337c478bd9Sstevel@tonic-gate 	 * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY.
14347c478bd9Sstevel@tonic-gate 	 */
14357c478bd9Sstevel@tonic-gate 	static const uint32_t disabled[TSTAT_ENT_NINSTR] = {
14367c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a addr			*/
14377c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP, NOP, NOP, NOP, NOP,
14387c478bd9Sstevel@tonic-gate 	};
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	ent = tcpu->tcpu_instr->tinst_traptab;
14437c478bd9Sstevel@tonic-gate 	stat = (uint64_t *)TSTAT_DATA_OFFS(tcpu, tdata_traps);
14447c478bd9Sstevel@tonic-gate 	orig = KERNELBASE;
14457c478bd9Sstevel@tonic-gate 	va = (uintptr_t)tcpu->tcpu_ibase;
14467c478bd9Sstevel@tonic-gate 	en_baoffs = TSTAT_ENABLED_BA * sizeof (uint32_t);
14477c478bd9Sstevel@tonic-gate 	dis_baoffs = TSTAT_DISABLED_BA * sizeof (uint32_t);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) {
14507c478bd9Sstevel@tonic-gate 		if (tstat_enabled[nent]) {
14517c478bd9Sstevel@tonic-gate 			bcopy(enabled, ent, sizeof (enabled));
14529f1a1f17Sdmick 			ent[TSTAT_ENABLED_STATHI] |= HI22((uintptr_t)stat);
14539f1a1f17Sdmick 			ent[TSTAT_ENABLED_STATLO_LD] |= LO10((uintptr_t)stat);
14549f1a1f17Sdmick 			ent[TSTAT_ENABLED_STATLO_ST] |= LO10((uintptr_t)stat);
14557c478bd9Sstevel@tonic-gate 			ent[TSTAT_ENABLED_BA] |= DISP22(va + en_baoffs, orig);
14567c478bd9Sstevel@tonic-gate 		} else {
14577c478bd9Sstevel@tonic-gate 			bcopy(disabled, ent, sizeof (disabled));
14587c478bd9Sstevel@tonic-gate 			ent[TSTAT_DISABLED_BA] |= DISP22(va + dis_baoffs, orig);
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 		stat++;
14627c478bd9Sstevel@tonic-gate 		orig += sizeof (enabled);
14637c478bd9Sstevel@tonic-gate 		ent += sizeof (enabled) / sizeof (*ent);
14647c478bd9Sstevel@tonic-gate 		va += sizeof (enabled);
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATHI
14697c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_LD
14707c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_ST
14717c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_BA
14727c478bd9Sstevel@tonic-gate #undef TSTAT_DISABLED_BA
14737c478bd9Sstevel@tonic-gate 
147459ac0c16Sdavemq #else /* sun4v */
147559ac0c16Sdavemq 
147659ac0c16Sdavemq #define	TSTAT_ENABLED_STATHI	0
147759ac0c16Sdavemq #define	TSTAT_ENABLED_STATLO	1
147859ac0c16Sdavemq #define	TSTAT_ENABLED_ADDRHI	2
147959ac0c16Sdavemq #define	TSTAT_ENABLED_ADDRLO	3
148059ac0c16Sdavemq #define	TSTAT_ENABLED_CONTBA	6
148159ac0c16Sdavemq #define	TSTAT_ENABLED_TDATASHFT	7
148259ac0c16Sdavemq #define	TSTAT_DISABLED_BA	0
148359ac0c16Sdavemq 
148459ac0c16Sdavemq static void
148559ac0c16Sdavemq trapstat_make_traptab(tstat_percpu_t *tcpu)
148659ac0c16Sdavemq {
148759ac0c16Sdavemq 	uint32_t *ent;
148859ac0c16Sdavemq 	uint64_t *stat;
148959ac0c16Sdavemq 	uintptr_t orig, va, en_baoffs, dis_baoffs;
149059ac0c16Sdavemq 	uintptr_t tstat_cont_va;
149159ac0c16Sdavemq 	int nent;
149259ac0c16Sdavemq 
149359ac0c16Sdavemq 	/*
149459ac0c16Sdavemq 	 * This is the entry in the interposing trap table for enabled trap
149559ac0c16Sdavemq 	 * table entries.  It loads a counter, increments it and stores it
149659ac0c16Sdavemq 	 * back before branching to the actual trap table entry.
149759ac0c16Sdavemq 	 *
149859ac0c16Sdavemq 	 * All CPUs share the same interposing trap entry to count the
149959ac0c16Sdavemq 	 * number of traps. Note that the trap counter is kept in per CPU
150059ac0c16Sdavemq 	 * trap statistics area. Its address is obtained dynamically by
150159ac0c16Sdavemq 	 * adding the offset of that CPU's trap statistics area from CPU 0
150259ac0c16Sdavemq 	 * (i.e. cpu_id * TSTAT_DATA_SIZE) to the address of the CPU 0
150359ac0c16Sdavemq 	 * trap counter already coded in the interposing trap entry itself.
150459ac0c16Sdavemq 	 *
150559ac0c16Sdavemq 	 * Since this interposing code sequence to count traps takes more
150659ac0c16Sdavemq 	 * than 8 instructions, it's split in two parts as follows:
150759ac0c16Sdavemq 	 *
150859ac0c16Sdavemq 	 *   tstat_trapcnt:
150959ac0c16Sdavemq 	 *	sethi %hi(stat), %g1
151059ac0c16Sdavemq 	 *	or    %g1, %lo[stat), %g1	! %g1 = CPU0 trap counter addr
151159ac0c16Sdavemq 	 *	sethi %hi(addr), %g2
151259ac0c16Sdavemq 	 *	or    %g2, %lo(addr), %g2	! %g2 = real trap handler addr
151359ac0c16Sdavemq 	 *	mov   ASI_SCRATCHPAD_CPUID, %g3
151459ac0c16Sdavemq 	 *	ldxa [%g3]ASI_SCRATCHPAD, %g3	! %g3 = CPU ID
151559ac0c16Sdavemq 	 *	ba tstat_trapcnt_cont		! branch to tstat_trapcnt_cont
151659ac0c16Sdavemq 	 *	sllx %g3, TSTAT_DATA_SHIFT, %g3	! %g3 = CPU trapstat data offset
151759ac0c16Sdavemq 	 *
151859ac0c16Sdavemq 	 *   tstat_trapcnt_cont:
151959ac0c16Sdavemq 	 *	ldx [%g1 + %g3], %g4		! get counter value
152059ac0c16Sdavemq 	 *	add %g4, 1, %g4			! increment value
152159ac0c16Sdavemq 	 *	jmp %g2				! jump to original trap handler
152259ac0c16Sdavemq 	 *	stx %g4, [%g1 + %g3]		! store counter value
152359ac0c16Sdavemq 	 *
152459ac0c16Sdavemq 	 * First part, i.e. tstat_trapcnt, is per trap and is kept in-line in
152559ac0c16Sdavemq 	 * the interposing trap table. However, the tstat_trapcnt_cont code
152659ac0c16Sdavemq 	 * sequence is shared by all traps and is kept right after the
152759ac0c16Sdavemq 	 * the interposing trap table.
152859ac0c16Sdavemq 	 */
152959ac0c16Sdavemq 	static const uint32_t enabled[TSTAT_ENT_NINSTR] = {
153059ac0c16Sdavemq 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
153159ac0c16Sdavemq 	    0x82106000,			/* or   %g1, %lo[stat), %g1	*/
153259ac0c16Sdavemq 	    0x05000000, 		/* sethi %hi(addr), %g2		*/
153359ac0c16Sdavemq 	    0x8410a000,			/* or   %g2, %lo(addr), %g2	*/
153459ac0c16Sdavemq 	    0x86102008,			/* mov	ASI_SCRATCHPAD_CPUID, %g3 */
153559ac0c16Sdavemq 	    0xc6d8c400,			/* ldxa [%g3]ASI_SCRATCHPAD, %g3 */
153659ac0c16Sdavemq 	    0x10800000,			/* ba enabled_cont		*/
153759ac0c16Sdavemq 	    0x8728f000			/* sllx %g3, TSTAT_DATA_SHIFT, %g3 */
153859ac0c16Sdavemq 	};
153959ac0c16Sdavemq 
154059ac0c16Sdavemq 	static const uint32_t enabled_cont[TSTAT_ENT_NINSTR] = {
154159ac0c16Sdavemq 	    0xc8584003, 		/* ldx [%g1 + %g3], %g4		*/
154259ac0c16Sdavemq 	    0x88012001,			/* add %g4, 1, %g4		*/
154359ac0c16Sdavemq 	    0x81c08000,			/* jmp %g2			*/
154459ac0c16Sdavemq 	    0xc8704003,			/* stx %g4, [%g1 + %g3]		*/
154559ac0c16Sdavemq 	    NOP, NOP, NOP, NOP
154659ac0c16Sdavemq 	};
154759ac0c16Sdavemq 
154859ac0c16Sdavemq 	/*
154959ac0c16Sdavemq 	 * This is the entry in the interposing trap table for disabled trap
155059ac0c16Sdavemq 	 * table entries.  It simply branches to the actual, underlying trap
155159ac0c16Sdavemq 	 * table entry.  As explained in the "Implementation Details" section
155259ac0c16Sdavemq 	 * of the block comment, all TL>0 traps _must_ use the disabled entry;
155359ac0c16Sdavemq 	 * additional entries may be explicitly disabled through the use
155459ac0c16Sdavemq 	 * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY.
155559ac0c16Sdavemq 	 */
155659ac0c16Sdavemq 	static const uint32_t disabled[TSTAT_ENT_NINSTR] = {
155759ac0c16Sdavemq 	    0x30800000,			/* ba,a addr			*/
155859ac0c16Sdavemq 	    NOP, NOP, NOP, NOP, NOP, NOP, NOP,
155959ac0c16Sdavemq 	};
156059ac0c16Sdavemq 
156159ac0c16Sdavemq 	ASSERT(MUTEX_HELD(&tstat_lock));
156259ac0c16Sdavemq 	ent = tcpu->tcpu_instr->tinst_traptab;
156359ac0c16Sdavemq 	stat = (uint64_t *)TSTAT_CPU0_DATA_OFFS(tcpu, tdata_traps);
156459ac0c16Sdavemq 	orig = KERNELBASE;
156559ac0c16Sdavemq 	va = (uintptr_t)tcpu->tcpu_ibase;
156659ac0c16Sdavemq 	en_baoffs = TSTAT_ENABLED_CONTBA * sizeof (uint32_t);
156759ac0c16Sdavemq 	dis_baoffs = TSTAT_DISABLED_BA * sizeof (uint32_t);
156859ac0c16Sdavemq 	tstat_cont_va = TSTAT_INSTR_OFFS(tcpu, tinst_trapcnt);
156959ac0c16Sdavemq 
157059ac0c16Sdavemq 	for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) {
157159ac0c16Sdavemq 		if (tstat_enabled[nent]) {
157259ac0c16Sdavemq 			bcopy(enabled, ent, sizeof (enabled));
157359ac0c16Sdavemq 			ent[TSTAT_ENABLED_STATHI] |= HI22((uintptr_t)stat);
157459ac0c16Sdavemq 			ent[TSTAT_ENABLED_STATLO] |= LO10((uintptr_t)stat);
157559ac0c16Sdavemq 			ent[TSTAT_ENABLED_ADDRHI] |= HI22((uintptr_t)orig);
157659ac0c16Sdavemq 			ent[TSTAT_ENABLED_ADDRLO] |= LO10((uintptr_t)orig);
157759ac0c16Sdavemq 			ent[TSTAT_ENABLED_CONTBA] |=
157859ac0c16Sdavemq 			    DISP22(va + en_baoffs, tstat_cont_va);
157959ac0c16Sdavemq 			ent[TSTAT_ENABLED_TDATASHFT] |=
158059ac0c16Sdavemq 			    LO10((uintptr_t)TSTAT_DATA_SHIFT);
158159ac0c16Sdavemq 		} else {
158259ac0c16Sdavemq 			bcopy(disabled, ent, sizeof (disabled));
158359ac0c16Sdavemq 			ent[TSTAT_DISABLED_BA] |= DISP22(va + dis_baoffs, orig);
158459ac0c16Sdavemq 		}
158559ac0c16Sdavemq 
158659ac0c16Sdavemq 		stat++;
158759ac0c16Sdavemq 		orig += sizeof (enabled);
158859ac0c16Sdavemq 		ent += sizeof (enabled) / sizeof (*ent);
158959ac0c16Sdavemq 		va += sizeof (enabled);
159059ac0c16Sdavemq 	}
159159ac0c16Sdavemq 	bcopy(enabled_cont, (uint32_t *)tcpu->tcpu_instr->tinst_trapcnt,
159259ac0c16Sdavemq 	    sizeof (enabled_cont));
159359ac0c16Sdavemq }
159459ac0c16Sdavemq 
159559ac0c16Sdavemq #undef	TSTAT_ENABLED_TDATASHFT
159659ac0c16Sdavemq #undef	TSTAT_ENABLED_STATHI
159759ac0c16Sdavemq #undef	TSTAT_ENABLED_STATLO
159859ac0c16Sdavemq #undef	TSTAT_ENABLED_ADDRHI
159959ac0c16Sdavemq #undef	TSTAT_ENABLED_ADDRLO
160059ac0c16Sdavemq #undef	TSTAT_ENABLED_CONTBA
160159ac0c16Sdavemq #undef	TSTAT_DISABLED_BA
160259ac0c16Sdavemq 
160359ac0c16Sdavemq #endif /* sun4v */
160459ac0c16Sdavemq 
160525cf1a30Sjl139090 #ifndef sun4v
160625cf1a30Sjl139090 /*
160725cf1a30Sjl139090  * See Section A.6 in SPARC v9 Manual.
160825cf1a30Sjl139090  * max branch = 4*((2^21)-1) = 8388604
160925cf1a30Sjl139090  */
161025cf1a30Sjl139090 #define	MAX_BICC_BRANCH_DISPLACEMENT (4 * ((1 << 21) - 1))
161125cf1a30Sjl139090 #endif
161225cf1a30Sjl139090 
16137c478bd9Sstevel@tonic-gate static void
16147c478bd9Sstevel@tonic-gate trapstat_setup(processorid_t cpu)
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
16177c478bd9Sstevel@tonic-gate #ifndef sun4v
16187c478bd9Sstevel@tonic-gate 	int i;
16197c478bd9Sstevel@tonic-gate 	caddr_t va;
16207c478bd9Sstevel@tonic-gate 	pfn_t *pfn;
162125cf1a30Sjl139090 	cpu_t *cp;
162225cf1a30Sjl139090 	uint_t strand_idx;
162325cf1a30Sjl139090 	size_t tstat_offset;
16247c478bd9Sstevel@tonic-gate #endif
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_pfn == NULL);
16277c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_instr == NULL);
16287c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_data == NULL);
16297c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
16307c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED));
16317c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16327c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
16337c478bd9Sstevel@tonic-gate 
163459ac0c16Sdavemq #ifndef sun4v
16357c478bd9Sstevel@tonic-gate 	/*
16367c478bd9Sstevel@tonic-gate 	 * The lower fifteen bits of the %tba are always read as zero; we must
16377c478bd9Sstevel@tonic-gate 	 * align our instruction base address appropriately.
16387c478bd9Sstevel@tonic-gate 	 */
163925cf1a30Sjl139090 	tstat_offset = tstat_total_size;
164025cf1a30Sjl139090 
164125cf1a30Sjl139090 	cp = cpu_get(cpu);
164225cf1a30Sjl139090 	ASSERT(cp != NULL);
1643fb2f18f8Sesaxe 	if ((strand_idx = cpu ^ pg_plat_hw_instance_id(cp, PGHW_IPIPE)) != 0) {
164425cf1a30Sjl139090 		/*
164525cf1a30Sjl139090 		 * On sun4u platforms with multiple CPUs sharing the MMU
164625cf1a30Sjl139090 		 * (Olympus-C has 2 strands per core), each CPU uses a
164725cf1a30Sjl139090 		 * disjoint trap table.  The indexing is based on the
164825cf1a30Sjl139090 		 * strand id, which is obtained by XOR'ing the cpuid with
164925cf1a30Sjl139090 		 * the coreid.
165025cf1a30Sjl139090 		 */
165125cf1a30Sjl139090 		tstat_offset += tstat_total_size * strand_idx;
165225cf1a30Sjl139090 
165325cf1a30Sjl139090 		/*
165425cf1a30Sjl139090 		 * Offset must be less than the maximum PC-relative branch
165525cf1a30Sjl139090 		 * displacement for Bicc variants.  See the Implementation
165625cf1a30Sjl139090 		 * Details comment.
165725cf1a30Sjl139090 		 */
165825cf1a30Sjl139090 		ASSERT(tstat_offset <= MAX_BICC_BRANCH_DISPLACEMENT);
165925cf1a30Sjl139090 	}
166025cf1a30Sjl139090 
166125cf1a30Sjl139090 	tcpu->tcpu_ibase = (caddr_t)((KERNELBASE - tstat_offset)
16627c478bd9Sstevel@tonic-gate 	    & TSTAT_TBA_MASK);
16637c478bd9Sstevel@tonic-gate 	tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE;
16647c478bd9Sstevel@tonic-gate 	tcpu->tcpu_vabase = tcpu->tcpu_ibase;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	tcpu->tcpu_pfn = vmem_alloc(tstat_arena, tstat_total_pages, VM_SLEEP);
16677c478bd9Sstevel@tonic-gate 	bzero(tcpu->tcpu_pfn, tstat_total_pages);
16687c478bd9Sstevel@tonic-gate 	pfn = tcpu->tcpu_pfn;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	tcpu->tcpu_instr = vmem_alloc(tstat_arena, TSTAT_INSTR_SIZE, VM_SLEEP);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	va = (caddr_t)tcpu->tcpu_instr;
16737c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_INSTR_PAGES; i++, va += MMU_PAGESIZE)
16747c478bd9Sstevel@tonic-gate 		*pfn++ = hat_getpfnum(kas.a_hat, va);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * We must be sure that the pages that we will use to examine the data
16787c478bd9Sstevel@tonic-gate 	 * have the same virtual color as the pages to which the data is being
16797c478bd9Sstevel@tonic-gate 	 * recorded, hence the alignment and phase constraints on the
16807c478bd9Sstevel@tonic-gate 	 * allocation.
16817c478bd9Sstevel@tonic-gate 	 */
16827c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data = vmem_xalloc(tstat_arena, tstat_data_size,
16837c478bd9Sstevel@tonic-gate 	    shm_alignment, (uintptr_t)tcpu->tcpu_dbase & (shm_alignment - 1),
16847c478bd9Sstevel@tonic-gate 	    0, 0, NULL, VM_SLEEP);
16857c478bd9Sstevel@tonic-gate 	bzero(tcpu->tcpu_data, tstat_data_size);
16867c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data->tdata_cpuid = cpu;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	va = (caddr_t)tcpu->tcpu_data;
16897c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_data_pages; i++, va += MMU_PAGESIZE)
16907c478bd9Sstevel@tonic-gate 		*pfn++ = hat_getpfnum(kas.a_hat, va);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * Now that we have all of the instruction and data pages allocated,
16947c478bd9Sstevel@tonic-gate 	 * make the trap table from scratch.
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 	trapstat_make_traptab(tcpu);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	if (tstat_options & TSTAT_OPT_TLBDATA) {
16997c478bd9Sstevel@tonic-gate 		/*
17007c478bd9Sstevel@tonic-gate 		 * TLB Statistics have been specified; set up the I- and D-TLB
17017c478bd9Sstevel@tonic-gate 		 * entries and corresponding TLB return entries.
17027c478bd9Sstevel@tonic-gate 		 */
17037c478bd9Sstevel@tonic-gate 		trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS);
17047c478bd9Sstevel@tonic-gate 		trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS);
170559ac0c16Sdavemq 	}
170659ac0c16Sdavemq 
170759ac0c16Sdavemq #else /* sun4v */
170859ac0c16Sdavemq 
170959ac0c16Sdavemq 	/*
171059ac0c16Sdavemq 	 * The lower fifteen bits of the %tba are always read as zero; hence
171159ac0c16Sdavemq 	 * it must be aligned at least on 512K boundary.
171259ac0c16Sdavemq 	 */
171359ac0c16Sdavemq 	tcpu->tcpu_vabase = (caddr_t)(KERNELBASE - MMU_PAGESIZE4M);
171459ac0c16Sdavemq 	tcpu->tcpu_ibase = tcpu->tcpu_vabase;
171559ac0c16Sdavemq 	tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE +
171659ac0c16Sdavemq 	    cpu * TSTAT_DATA_SIZE;
171759ac0c16Sdavemq 
171859ac0c16Sdavemq 	tcpu->tcpu_pfn = &tstat_pfn;
171959ac0c16Sdavemq 	tcpu->tcpu_instr = (tstat_instr_t *)tstat_va;
172059ac0c16Sdavemq 	tcpu->tcpu_data = (tstat_data_t *)(tstat_va + TSTAT_INSTR_SIZE +
172159ac0c16Sdavemq 	    cpu * TSTAT_DATA_SIZE);
172259ac0c16Sdavemq 	bzero(tcpu->tcpu_data, TSTAT_DATA_SIZE);
172359ac0c16Sdavemq 	tcpu->tcpu_data->tdata_cpuid = cpu;
172459ac0c16Sdavemq 
172559ac0c16Sdavemq 	/*
172659ac0c16Sdavemq 	 * Now that we have all of the instruction and data pages allocated,
172759ac0c16Sdavemq 	 * make the trap table from scratch. It should be done only once
172859ac0c16Sdavemq 	 * as it is shared by all CPUs.
172959ac0c16Sdavemq 	 */
173059ac0c16Sdavemq 	if (!tstat_traptab_initialized)
173159ac0c16Sdavemq 		trapstat_make_traptab(tcpu);
173259ac0c16Sdavemq 
173359ac0c16Sdavemq 	if (tstat_options & TSTAT_OPT_TLBDATA) {
173459ac0c16Sdavemq 		/*
173559ac0c16Sdavemq 		 * TLB Statistics have been specified; set up the I- and D-TLB
173659ac0c16Sdavemq 		 * entries and corresponding TLB return entries.
173759ac0c16Sdavemq 		 */
173859ac0c16Sdavemq 		if (!tstat_traptab_initialized) {
1739ce0352ebSgirish 			if (tstat_fast_tlbstat) {
1740ce0352ebSgirish 				trapstat_tlbent(tcpu, TSTAT_ENT_IMMUMISS);
1741ce0352ebSgirish 				trapstat_tlbent(tcpu, TSTAT_ENT_DMMUMISS);
1742ce0352ebSgirish 			} else {
1743ce0352ebSgirish 				trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS);
1744ce0352ebSgirish 				trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS);
1745ce0352ebSgirish 			}
17467c478bd9Sstevel@tonic-gate 		}
174759ac0c16Sdavemq 	}
174859ac0c16Sdavemq 	tstat_traptab_initialized = 1;
174959ac0c16Sdavemq #endif /* sun4v */
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags |= TSTAT_CPU_ALLOCATED;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/*
17547c478bd9Sstevel@tonic-gate 	 * Finally, get the target CPU to load the locked pages into its TLBs.
17557c478bd9Sstevel@tonic-gate 	 */
17567c478bd9Sstevel@tonic-gate 	xc_one(cpu, (xcfunc_t *)trapstat_load_tlb, 0, 0);
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate static void
17607c478bd9Sstevel@tonic-gate trapstat_teardown(processorid_t cpu)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
17637c478bd9Sstevel@tonic-gate #ifndef sun4v
17647c478bd9Sstevel@tonic-gate 	int i;
17657c478bd9Sstevel@tonic-gate #endif
17667c478bd9Sstevel@tonic-gate 	caddr_t va = tcpu->tcpu_vabase;
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_pfn != NULL);
17697c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_instr != NULL);
17707c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_data != NULL);
17717c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
17727c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
17737c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
17747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate #ifndef sun4v
17787c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_pfn, tstat_total_pages);
17797c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_instr, TSTAT_INSTR_SIZE);
17807c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_data, tstat_data_size);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) {
17831e2e7a75Shuah 		xt_one(cpu, vtag_flushpage_tl1, (uint64_t)va,
17841e2e7a75Shuah 		    (uint64_t)ksfmmup);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate #else
17877c478bd9Sstevel@tonic-gate 	xt_one(cpu, vtag_unmap_perm_tl1, (uint64_t)va, KCONTEXT);
17887c478bd9Sstevel@tonic-gate #endif
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	tcpu->tcpu_pfn = NULL;
17917c478bd9Sstevel@tonic-gate 	tcpu->tcpu_instr = NULL;
17927c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data = NULL;
17937c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED;
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate static int
17977c478bd9Sstevel@tonic-gate trapstat_go()
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	cpu_t *cp;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
18027c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (tstat_running) {
18057c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
18067c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
18077c478bd9Sstevel@tonic-gate 		return (EBUSY);
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate #ifdef sun4v
18117c478bd9Sstevel@tonic-gate 	/*
1812ce0352ebSgirish 	 * Allocate large page to hold interposing tables.
18137c478bd9Sstevel@tonic-gate 	 */
18147c478bd9Sstevel@tonic-gate 	tstat_va = contig_mem_alloc(MMU_PAGESIZE4M);
18157c478bd9Sstevel@tonic-gate 	tstat_pfn = va_to_pfn(tstat_va);
1816aaa10e67Sha137994 	if (tstat_pfn == PFN_INVALID) {
1817aaa10e67Sha137994 		mutex_exit(&tstat_lock);
1818aaa10e67Sha137994 		mutex_exit(&cpu_lock);
18197c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1820aaa10e67Sha137994 	}
1821ce0352ebSgirish 
1822ce0352ebSgirish 	/*
1823ce0352ebSgirish 	 * For detailed TLB statistics, invoke CPU specific interface
1824ce0352ebSgirish 	 * to see if it supports a low overhead interface to collect
1825ce0352ebSgirish 	 * TSB hit statistics. If so, make set tstat_fast_tlbstat flag
1826ce0352ebSgirish 	 * to reflect that.
1827ce0352ebSgirish 	 */
1828ce0352ebSgirish 	if (tstat_options & TSTAT_OPT_TLBDATA) {
1829ce0352ebSgirish 		int error;
1830ce0352ebSgirish 
183159ac0c16Sdavemq 		tstat_fast_tlbstat = B_FALSE;
1832ce0352ebSgirish 		error = cpu_trapstat_conf(CPU_TSTATCONF_INIT);
1833ce0352ebSgirish 		if (error == 0)
1834ce0352ebSgirish 			tstat_fast_tlbstat = B_TRUE;
1835ce0352ebSgirish 		else if (error != ENOTSUP) {
1836ce0352ebSgirish 			contig_mem_free(tstat_va, MMU_PAGESIZE4M);
1837aaa10e67Sha137994 			mutex_exit(&tstat_lock);
1838aaa10e67Sha137994 			mutex_exit(&cpu_lock);
1839ce0352ebSgirish 			return (error);
1840ce0352ebSgirish 		}
18417c478bd9Sstevel@tonic-gate 	}
184259ac0c16Sdavemq #endif /* sun4v */
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	/*
18457c478bd9Sstevel@tonic-gate 	 * First, perform any necessary hot patching.
18467c478bd9Sstevel@tonic-gate 	 */
18477c478bd9Sstevel@tonic-gate 	trapstat_hotpatch();
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	/*
18507c478bd9Sstevel@tonic-gate 	 * Allocate the resources we'll need to measure probe effect.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	trapstat_probe_alloc();
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	cp = cpu_list;
18567c478bd9Sstevel@tonic-gate 	do {
18577c478bd9Sstevel@tonic-gate 		if (!(tstat_percpu[cp->cpu_id].tcpu_flags & TSTAT_CPU_SELECTED))
18587c478bd9Sstevel@tonic-gate 			continue;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		trapstat_setup(cp->cpu_id);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		/*
18637c478bd9Sstevel@tonic-gate 		 * Note that due to trapstat_probe()'s use of global data,
18647c478bd9Sstevel@tonic-gate 		 * we determine the probe effect on each CPU serially instead
18657c478bd9Sstevel@tonic-gate 		 * of in parallel with an xc_all().
18667c478bd9Sstevel@tonic-gate 		 */
18677c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_probe, 0, 0);
18687c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	xc_all((xcfunc_t *)trapstat_enable, 0, 0);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	trapstat_probe_free();
18737c478bd9Sstevel@tonic-gate 	tstat_running = 1;
18747c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
18757c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	return (0);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate static int
18817c478bd9Sstevel@tonic-gate trapstat_stop()
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	int i;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
18867c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
18877c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
18887c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
18897c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
18907c478bd9Sstevel@tonic-gate 		return (ENXIO);
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	xc_all((xcfunc_t *)trapstat_disable, 0, 0);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	for (i = 0; i <= max_cpuid; i++) {
18967c478bd9Sstevel@tonic-gate 		if (tstat_percpu[i].tcpu_flags & TSTAT_CPU_ALLOCATED)
18977c478bd9Sstevel@tonic-gate 			trapstat_teardown(i);
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate #ifdef sun4v
190159ac0c16Sdavemq 	tstat_traptab_initialized = 0;
1902ce0352ebSgirish 	if (tstat_options & TSTAT_OPT_TLBDATA)
1903ce0352ebSgirish 		cpu_trapstat_conf(CPU_TSTATCONF_FINI);
19047c478bd9Sstevel@tonic-gate 	contig_mem_free(tstat_va, MMU_PAGESIZE4M);
19057c478bd9Sstevel@tonic-gate #endif
19067c478bd9Sstevel@tonic-gate 	trapstat_hotpatch();
19077c478bd9Sstevel@tonic-gate 	tstat_running = 0;
19087c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
19097c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	return (0);
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * This is trapstat's DR CPU configuration callback.  It's called (with
19167c478bd9Sstevel@tonic-gate  * cpu_lock held) to unconfigure a newly powered-off CPU, or to configure a
19177c478bd9Sstevel@tonic-gate  * powered-off CPU that is to be brought into the system.  We need only take
19187c478bd9Sstevel@tonic-gate  * action in the unconfigure case:  because a powered-off CPU will have its
19197c478bd9Sstevel@tonic-gate  * trap table restored to KERNELBASE if it is ever powered back on, we must
19207c478bd9Sstevel@tonic-gate  * update the flags to reflect that trapstat is no longer enabled on the
19217c478bd9Sstevel@tonic-gate  * powered-off CPU.  Note that this means that a TSTAT_CPU_ENABLED CPU that
19227c478bd9Sstevel@tonic-gate  * is unconfigured/powered off and later powered back on/reconfigured will
19237c478bd9Sstevel@tonic-gate  * _not_ be re-TSTAT_CPU_ENABLED.
19247c478bd9Sstevel@tonic-gate  */
19257c478bd9Sstevel@tonic-gate static int
19267c478bd9Sstevel@tonic-gate trapstat_cpu_setup(cpu_setup_t what, processorid_t cpu)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19317c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
19347c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
19357c478bd9Sstevel@tonic-gate 		return (0);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	switch (what) {
19397c478bd9Sstevel@tonic-gate 	case CPU_CONFIG:
19407c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
19417c478bd9Sstevel@tonic-gate 		break;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	case CPU_UNCONFIG:
19441ae08745Sheppo 		if (tcpu->tcpu_flags & TSTAT_CPU_ENABLED) {
19457c478bd9Sstevel@tonic-gate 			tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED;
19461ae08745Sheppo #ifdef	sun4v
19471ae08745Sheppo 			/*
19481ae08745Sheppo 			 * A power-off, causes the cpu mondo queues to be
19491ae08745Sheppo 			 * unconfigured on sun4v. Since we can't teardown
19501ae08745Sheppo 			 * trapstat's mappings on the cpu that is going away,
19511ae08745Sheppo 			 * we simply mark it as not allocated. This will
19521ae08745Sheppo 			 * prevent a teardown on a cpu with the same cpu id
19531ae08745Sheppo 			 * that might have been added while trapstat is running.
19541ae08745Sheppo 			 */
19551ae08745Sheppo 			if (tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED) {
19561ae08745Sheppo 				tcpu->tcpu_pfn = NULL;
19571ae08745Sheppo 				tcpu->tcpu_instr = NULL;
19581ae08745Sheppo 				tcpu->tcpu_data = NULL;
19591ae08745Sheppo 				tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED;
19601ae08745Sheppo 			}
19611ae08745Sheppo #endif
19621ae08745Sheppo 		}
19637c478bd9Sstevel@tonic-gate 		break;
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	default:
19667c478bd9Sstevel@tonic-gate 		break;
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
19707c478bd9Sstevel@tonic-gate 	return (0);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate /*
19747c478bd9Sstevel@tonic-gate  * This is called before a CPR suspend and after a CPR resume.  We don't have
19757c478bd9Sstevel@tonic-gate  * anything to do before a suspend, but after a restart we must restore the
19767c478bd9Sstevel@tonic-gate  * trap table to be our interposing trap table.  However, we don't actually
19777c478bd9Sstevel@tonic-gate  * know whether or not the CPUs have been powered off -- this routine may be
19787c478bd9Sstevel@tonic-gate  * called while restoring from a failed CPR suspend.  We thus run through each
19797c478bd9Sstevel@tonic-gate  * TSTAT_CPU_ENABLED CPU, and explicitly destroy and reestablish its
19807c478bd9Sstevel@tonic-gate  * interposing trap table.  This assures that our state is correct regardless
19817c478bd9Sstevel@tonic-gate  * of whether or not the CPU has been newly powered on.
19827c478bd9Sstevel@tonic-gate  */
19837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19847c478bd9Sstevel@tonic-gate static boolean_t
19857c478bd9Sstevel@tonic-gate trapstat_cpr(void *arg, int code)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	cpu_t *cp;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	if (code == CB_CODE_CPR_CHKPT)
19907c478bd9Sstevel@tonic-gate 		return (B_TRUE);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	ASSERT(code == CB_CODE_CPR_RESUME);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
19957c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
19987c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
19997c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
20007c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	cp = cpu_list;
20047c478bd9Sstevel@tonic-gate 	do {
20057c478bd9Sstevel@tonic-gate 		tstat_percpu_t *tcpu = &tstat_percpu[cp->cpu_id];
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 		if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
20087c478bd9Sstevel@tonic-gate 			continue;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
20117c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_disable, 0, 0);
20147c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 		/*
20177c478bd9Sstevel@tonic-gate 		 * Preserve this CPU's data in tstat_buffer and rip down its
20187c478bd9Sstevel@tonic-gate 		 * interposing trap table.
20197c478bd9Sstevel@tonic-gate 		 */
20207c478bd9Sstevel@tonic-gate 		bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size);
20217c478bd9Sstevel@tonic-gate 		trapstat_teardown(cp->cpu_id);
20227c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED));
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 		/*
20257c478bd9Sstevel@tonic-gate 		 * Reestablish the interposing trap table and restore the old
20267c478bd9Sstevel@tonic-gate 		 * data.
20277c478bd9Sstevel@tonic-gate 		 */
20287c478bd9Sstevel@tonic-gate 		trapstat_setup(cp->cpu_id);
20297c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
20307c478bd9Sstevel@tonic-gate 		bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_enable, 0, 0);
20337c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
20367c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	return (B_TRUE);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20427c478bd9Sstevel@tonic-gate static int
20437c478bd9Sstevel@tonic-gate trapstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
20447c478bd9Sstevel@tonic-gate {
20457c478bd9Sstevel@tonic-gate 	int i;
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
20487c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
20497c478bd9Sstevel@tonic-gate 	if (tstat_open != 0) {
20507c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
20517c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
20527c478bd9Sstevel@tonic-gate 		return (EBUSY);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	/*
20567c478bd9Sstevel@tonic-gate 	 * Register this in open() rather than in attach() to prevent deadlock
20577c478bd9Sstevel@tonic-gate 	 * with DR code. During attach, I/O device tree locks are grabbed
20587c478bd9Sstevel@tonic-gate 	 * before trapstat_attach() is invoked - registering in attach
20597c478bd9Sstevel@tonic-gate 	 * will result in the lock order: device tree lock, cpu_lock.
20607c478bd9Sstevel@tonic-gate 	 * DR code however requires that cpu_lock be acquired before
20617c478bd9Sstevel@tonic-gate 	 * device tree locks.
20627c478bd9Sstevel@tonic-gate 	 */
20637c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
20647c478bd9Sstevel@tonic-gate 	register_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/*
20677c478bd9Sstevel@tonic-gate 	 * Clear all options.  And until specific CPUs are specified, we'll
20687c478bd9Sstevel@tonic-gate 	 * mark all CPUs as selected.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	tstat_options = 0;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	for (i = 0; i <= max_cpuid; i++)
20737c478bd9Sstevel@tonic-gate 		tstat_percpu[i].tcpu_flags |= TSTAT_CPU_SELECTED;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * By default, all traps at TL=0 are enabled.  Traps at TL>0 must
20777c478bd9Sstevel@tonic-gate 	 * be disabled.
20787c478bd9Sstevel@tonic-gate 	 */
20797c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_TOTAL_NENT; i++)
20807c478bd9Sstevel@tonic-gate 		tstat_enabled[i] = i < TSTAT_NENT ? 1 : 0;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	tstat_open = 1;
20837c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
20847c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	return (0);
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20907c478bd9Sstevel@tonic-gate static int
20917c478bd9Sstevel@tonic-gate trapstat_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate 	(void) trapstat_stop();
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
20987c478bd9Sstevel@tonic-gate 	unregister_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL);
20997c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	tstat_open = 0;
21027c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
21037c478bd9Sstevel@tonic-gate }
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate static int
21067c478bd9Sstevel@tonic-gate trapstat_option(int option)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	if (tstat_running) {
21117c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
21127c478bd9Sstevel@tonic-gate 		return (EBUSY);
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	tstat_options |= option;
21167c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	return (0);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21227c478bd9Sstevel@tonic-gate static int
21237c478bd9Sstevel@tonic-gate trapstat_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *crd, int *rval)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	int i, j, out;
21267c478bd9Sstevel@tonic-gate 	size_t dsize;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	switch (cmd) {
21297c478bd9Sstevel@tonic-gate 	case TSTATIOC_GO:
21307c478bd9Sstevel@tonic-gate 		return (trapstat_go());
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOGO:
21337c478bd9Sstevel@tonic-gate 		return (trapstat_option(TSTAT_OPT_NOGO));
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	case TSTATIOC_STOP:
21367c478bd9Sstevel@tonic-gate 		return (trapstat_stop());
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	case TSTATIOC_CPU:
21397c478bd9Sstevel@tonic-gate 		if (arg < 0 || arg > max_cpuid)
21407c478bd9Sstevel@tonic-gate 			return (EINVAL);
21417c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOCPU:
21447c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		if (tstat_running) {
21477c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
21487c478bd9Sstevel@tonic-gate 			return (EBUSY);
21497c478bd9Sstevel@tonic-gate 		}
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		/*
21527c478bd9Sstevel@tonic-gate 		 * If this is the first CPU to be specified (or if we are
21537c478bd9Sstevel@tonic-gate 		 * being asked to explicitly de-select CPUs), disable all CPUs.
21547c478bd9Sstevel@tonic-gate 		 */
21557c478bd9Sstevel@tonic-gate 		if (!(tstat_options & TSTAT_OPT_CPU) || cmd == TSTATIOC_NOCPU) {
21567c478bd9Sstevel@tonic-gate 			tstat_options |= TSTAT_OPT_CPU;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 			for (i = 0; i <= max_cpuid; i++) {
21597c478bd9Sstevel@tonic-gate 				tstat_percpu_t *tcpu = &tstat_percpu[i];
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 				ASSERT(cmd == TSTATIOC_NOCPU ||
21627c478bd9Sstevel@tonic-gate 				    (tcpu->tcpu_flags & TSTAT_CPU_SELECTED));
21637c478bd9Sstevel@tonic-gate 				tcpu->tcpu_flags &= ~TSTAT_CPU_SELECTED;
21647c478bd9Sstevel@tonic-gate 			}
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 		if (cmd == TSTATIOC_CPU)
21687c478bd9Sstevel@tonic-gate 			tstat_percpu[arg].tcpu_flags |= TSTAT_CPU_SELECTED;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 		return (0);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	case TSTATIOC_ENTRY:
21757c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		if (tstat_running) {
21787c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
21797c478bd9Sstevel@tonic-gate 			return (EBUSY);
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		if (arg >= TSTAT_NENT || arg < 0) {
21837c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
21847c478bd9Sstevel@tonic-gate 			return (EINVAL);
21857c478bd9Sstevel@tonic-gate 		}
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 		if (!(tstat_options & TSTAT_OPT_ENTRY)) {
21887c478bd9Sstevel@tonic-gate 			/*
21897c478bd9Sstevel@tonic-gate 			 * If this is the first entry that we are explicitly
21907c478bd9Sstevel@tonic-gate 			 * enabling, explicitly disable every TL=0 entry.
21917c478bd9Sstevel@tonic-gate 			 */
21927c478bd9Sstevel@tonic-gate 			for (i = 0; i < TSTAT_NENT; i++)
21937c478bd9Sstevel@tonic-gate 				tstat_enabled[i] = 0;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 			tstat_options |= TSTAT_OPT_ENTRY;
21967c478bd9Sstevel@tonic-gate 		}
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 		tstat_enabled[arg] = 1;
21997c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
22007c478bd9Sstevel@tonic-gate 		return (0);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOENTRY:
22037c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 		if (tstat_running) {
22067c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
22077c478bd9Sstevel@tonic-gate 			return (EBUSY);
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSTAT_NENT; i++)
22117c478bd9Sstevel@tonic-gate 			tstat_enabled[i] = 0;
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
22147c478bd9Sstevel@tonic-gate 		return (0);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	case TSTATIOC_READ:
22177c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 		if (tstat_options & TSTAT_OPT_TLBDATA) {
22207c478bd9Sstevel@tonic-gate 			dsize = tstat_data_t_exported_size;
22217c478bd9Sstevel@tonic-gate 		} else {
22227c478bd9Sstevel@tonic-gate 			dsize = sizeof (tstat_data_t);
22237c478bd9Sstevel@tonic-gate 		}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 		for (i = 0, out = 0; i <= max_cpuid; i++) {
22267c478bd9Sstevel@tonic-gate 			tstat_percpu_t *tcpu = &tstat_percpu[i];
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 			if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
22297c478bd9Sstevel@tonic-gate 				continue;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 			ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
22327c478bd9Sstevel@tonic-gate 			ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 			tstat_buffer->tdata_cpuid = -1;
22357c478bd9Sstevel@tonic-gate 			xc_one(i, (xcfunc_t *)trapstat_snapshot, 0, 0);
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 			if (tstat_buffer->tdata_cpuid == -1) {
22387c478bd9Sstevel@tonic-gate 				/*
22397c478bd9Sstevel@tonic-gate 				 * This CPU is not currently responding to
22407c478bd9Sstevel@tonic-gate 				 * cross calls; we have caught it while it is
22417c478bd9Sstevel@tonic-gate 				 * being unconfigured.  We'll drop tstat_lock
22427c478bd9Sstevel@tonic-gate 				 * and pick up and drop cpu_lock.  By the
22437c478bd9Sstevel@tonic-gate 				 * time we acquire cpu_lock, the DR operation
22447c478bd9Sstevel@tonic-gate 				 * will appear consistent and we can assert
22457c478bd9Sstevel@tonic-gate 				 * that trapstat_cpu_setup() has cleared
22467c478bd9Sstevel@tonic-gate 				 * TSTAT_CPU_ENABLED.
22477c478bd9Sstevel@tonic-gate 				 */
22487c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
22497c478bd9Sstevel@tonic-gate 				mutex_enter(&cpu_lock);
22507c478bd9Sstevel@tonic-gate 				mutex_exit(&cpu_lock);
22517c478bd9Sstevel@tonic-gate 				mutex_enter(&tstat_lock);
22527c478bd9Sstevel@tonic-gate 				ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
22537c478bd9Sstevel@tonic-gate 				continue;
22547c478bd9Sstevel@tonic-gate 			}
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 			/*
22577c478bd9Sstevel@tonic-gate 			 * Need to compensate for the difference between page
22587c478bd9Sstevel@tonic-gate 			 * sizes exported to users and page sizes available
22597c478bd9Sstevel@tonic-gate 			 * within the kernel.
22607c478bd9Sstevel@tonic-gate 			 */
22617c478bd9Sstevel@tonic-gate 			if ((tstat_options & TSTAT_OPT_TLBDATA) &&
22627c478bd9Sstevel@tonic-gate 			    (tstat_pgszs != tstat_user_pgszs)) {
22637c478bd9Sstevel@tonic-gate 				tstat_pgszdata_t *tp;
22647c478bd9Sstevel@tonic-gate 				uint_t szc;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 				tp = &tstat_buffer->tdata_pgsz[0];
22677c478bd9Sstevel@tonic-gate 				for (j = 0; j < tstat_user_pgszs; j++) {
22687c478bd9Sstevel@tonic-gate 					if ((szc = USERSZC_2_SZC(j)) != j) {
22697c478bd9Sstevel@tonic-gate 						bcopy(&tp[szc], &tp[j],
22707c478bd9Sstevel@tonic-gate 						    sizeof (tstat_pgszdata_t));
22717c478bd9Sstevel@tonic-gate 					}
22727c478bd9Sstevel@tonic-gate 				}
22737c478bd9Sstevel@tonic-gate 			}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 			if (copyout(tstat_buffer, (void *)arg, dsize) != 0) {
22767c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
22777c478bd9Sstevel@tonic-gate 				return (EFAULT);
22787c478bd9Sstevel@tonic-gate 			}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 			out++;
22817c478bd9Sstevel@tonic-gate 			arg += dsize;
22827c478bd9Sstevel@tonic-gate 		}
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 		if (out != max_cpuid + 1) {
22857c478bd9Sstevel@tonic-gate 			processorid_t cpuid = -1;
22867c478bd9Sstevel@tonic-gate 			arg += offsetof(tstat_data_t, tdata_cpuid);
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 			if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) {
22897c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
22907c478bd9Sstevel@tonic-gate 				return (EFAULT);
22917c478bd9Sstevel@tonic-gate 			}
22927c478bd9Sstevel@tonic-gate 		}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		return (0);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	case TSTATIOC_TLBDATA:
22997c478bd9Sstevel@tonic-gate 		return (trapstat_option(TSTAT_OPT_TLBDATA));
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	default:
23027c478bd9Sstevel@tonic-gate 		break;
23037c478bd9Sstevel@tonic-gate 	}
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	return (ENOTTY);
23067c478bd9Sstevel@tonic-gate }
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23097c478bd9Sstevel@tonic-gate static int
23107c478bd9Sstevel@tonic-gate trapstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
23117c478bd9Sstevel@tonic-gate {
23127c478bd9Sstevel@tonic-gate 	int error;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	switch (infocmd) {
23157c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
23167c478bd9Sstevel@tonic-gate 		*result = (void *)tstat_devi;
23177c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
23187c478bd9Sstevel@tonic-gate 		break;
23197c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
23207c478bd9Sstevel@tonic-gate 		*result = (void *)0;
23217c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
23227c478bd9Sstevel@tonic-gate 		break;
23237c478bd9Sstevel@tonic-gate 	default:
23247c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
23257c478bd9Sstevel@tonic-gate 	}
23267c478bd9Sstevel@tonic-gate 	return (error);
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate static int
23307c478bd9Sstevel@tonic-gate trapstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
23317c478bd9Sstevel@tonic-gate {
23327c478bd9Sstevel@tonic-gate 	switch (cmd) {
23337c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
23347c478bd9Sstevel@tonic-gate 		break;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
23377c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	default:
23407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
23417c478bd9Sstevel@tonic-gate 	}
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "trapstat", S_IFCHR,
23447c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, 0) == DDI_FAILURE) {
23457c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
23467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
23477c478bd9Sstevel@tonic-gate 	}
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
23507c478bd9Sstevel@tonic-gate 	tstat_devi = devi;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	tstat_pgszs = page_num_pagesizes();
2353*02bc52beSkchow 	tstat_user_pgszs = page_num_user_pagesizes(0);
23547c478bd9Sstevel@tonic-gate 	tstat_data_t_size = sizeof (tstat_data_t) +
23557c478bd9Sstevel@tonic-gate 	    (tstat_pgszs - 1) * sizeof (tstat_pgszdata_t);
23567c478bd9Sstevel@tonic-gate 	tstat_data_t_exported_size = sizeof (tstat_data_t) +
23577c478bd9Sstevel@tonic-gate 	    (tstat_user_pgszs - 1) * sizeof (tstat_pgszdata_t);
23587c478bd9Sstevel@tonic-gate #ifndef sun4v
23597c478bd9Sstevel@tonic-gate 	tstat_data_pages = (tstat_data_t_size >> MMU_PAGESHIFT) + 1;
23607c478bd9Sstevel@tonic-gate 	tstat_total_pages = TSTAT_INSTR_PAGES + tstat_data_pages;
23617c478bd9Sstevel@tonic-gate 	tstat_data_size = tstat_data_pages * MMU_PAGESIZE;
23627c478bd9Sstevel@tonic-gate 	tstat_total_size = TSTAT_INSTR_SIZE + tstat_data_size;
23637c478bd9Sstevel@tonic-gate #else
236459ac0c16Sdavemq 	ASSERT(tstat_data_t_size <= TSTAT_DATA_SIZE);
23657c478bd9Sstevel@tonic-gate #endif
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	tstat_percpu = kmem_zalloc((max_cpuid + 1) *
23687c478bd9Sstevel@tonic-gate 	    sizeof (tstat_percpu_t), KM_SLEEP);
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	/*
23717c478bd9Sstevel@tonic-gate 	 * Create our own arena backed by segkmem to assure a source of
23727c478bd9Sstevel@tonic-gate 	 * MMU_PAGESIZE-aligned allocations.  We allocate out of the
23737c478bd9Sstevel@tonic-gate 	 * heap32_arena to assure that we can address the allocated memory with
23747c478bd9Sstevel@tonic-gate 	 * a single sethi/simm13 pair in the interposing trap table entries.
23757c478bd9Sstevel@tonic-gate 	 */
23767c478bd9Sstevel@tonic-gate 	tstat_arena = vmem_create("trapstat", NULL, 0, MMU_PAGESIZE,
23777c478bd9Sstevel@tonic-gate 	    segkmem_alloc_permanent, segkmem_free, heap32_arena, 0, VM_SLEEP);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	tstat_enabled = kmem_alloc(TSTAT_TOTAL_NENT * sizeof (int), KM_SLEEP);
23807c478bd9Sstevel@tonic-gate 	tstat_buffer = kmem_alloc(tstat_data_t_size, KM_SLEEP);
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	/*
23837c478bd9Sstevel@tonic-gate 	 * CB_CL_CPR_POST_USER is the class that executes from cpr_resume()
23847c478bd9Sstevel@tonic-gate 	 * after user threads can be restarted.  By executing in this class,
23857c478bd9Sstevel@tonic-gate 	 * we are assured of the availability of system services needed to
23867c478bd9Sstevel@tonic-gate 	 * resume trapstat (specifically, we are assured that all CPUs are
23877c478bd9Sstevel@tonic-gate 	 * restarted and responding to cross calls).
23887c478bd9Sstevel@tonic-gate 	 */
23897c478bd9Sstevel@tonic-gate 	tstat_cprcb =
23907c478bd9Sstevel@tonic-gate 	    callb_add(trapstat_cpr, NULL, CB_CL_CPR_POST_USER, "trapstat");
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate static int
23967c478bd9Sstevel@tonic-gate trapstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
23977c478bd9Sstevel@tonic-gate {
23987c478bd9Sstevel@tonic-gate 	int rval;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	ASSERT(devi == tstat_devi);
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	switch (cmd) {
24037c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
24047c478bd9Sstevel@tonic-gate 		break;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
24077c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	default:
24107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
24117c478bd9Sstevel@tonic-gate 	}
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	rval = callb_delete(tstat_cprcb);
24167c478bd9Sstevel@tonic-gate 	ASSERT(rval == 0);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	kmem_free(tstat_buffer, tstat_data_t_size);
24197c478bd9Sstevel@tonic-gate 	kmem_free(tstat_enabled, TSTAT_TOTAL_NENT * sizeof (int));
24207c478bd9Sstevel@tonic-gate 	vmem_destroy(tstat_arena);
24217c478bd9Sstevel@tonic-gate 	kmem_free(tstat_percpu, (max_cpuid + 1) * sizeof (tstat_percpu_t));
24227c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate /*
24287c478bd9Sstevel@tonic-gate  * Configuration data structures
24297c478bd9Sstevel@tonic-gate  */
24307c478bd9Sstevel@tonic-gate static struct cb_ops trapstat_cb_ops = {
24317c478bd9Sstevel@tonic-gate 	trapstat_open,		/* open */
24327c478bd9Sstevel@tonic-gate 	trapstat_close,		/* close */
24337c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
24347c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
24357c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
24367c478bd9Sstevel@tonic-gate 	nodev,			/* read */
24377c478bd9Sstevel@tonic-gate 	nodev,			/* write */
24387c478bd9Sstevel@tonic-gate 	trapstat_ioctl,		/* ioctl */
24397c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
24407c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
24417c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
24427c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
24437c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
24447c478bd9Sstevel@tonic-gate 	0,			/* streamtab */
24457c478bd9Sstevel@tonic-gate 	D_MP | D_NEW		/* Driver compatibility flag */
24467c478bd9Sstevel@tonic-gate };
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate static struct dev_ops trapstat_ops = {
24497c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
24507c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
24517c478bd9Sstevel@tonic-gate 	trapstat_info,		/* getinfo */
24527c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
24537c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
24547c478bd9Sstevel@tonic-gate 	trapstat_attach,	/* attach */
24557c478bd9Sstevel@tonic-gate 	trapstat_detach,	/* detach */
24567c478bd9Sstevel@tonic-gate 	nulldev,		/* reset */
24577c478bd9Sstevel@tonic-gate 	&trapstat_cb_ops,	/* cb_ops */
24587c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus_ops */
24597c478bd9Sstevel@tonic-gate };
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
24627c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module.  This one is a driver */
24637c478bd9Sstevel@tonic-gate 	"Trap Statistics",	/* name of module */
24647c478bd9Sstevel@tonic-gate 	&trapstat_ops,		/* driver ops */
24657c478bd9Sstevel@tonic-gate };
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
24687c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
24697c478bd9Sstevel@tonic-gate };
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate int
24727c478bd9Sstevel@tonic-gate _init(void)
24737c478bd9Sstevel@tonic-gate {
24747c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
24757c478bd9Sstevel@tonic-gate }
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate int
24787c478bd9Sstevel@tonic-gate _fini(void)
24797c478bd9Sstevel@tonic-gate {
24807c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate int
24847c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
24877c478bd9Sstevel@tonic-gate }
2488