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