xref: /titanic_53/usr/src/uts/sun4/io/trapstat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
36*7c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
37*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
38*7c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
39*7c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/machasi.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/callb.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/trapstat.h>
46*7c478bd9Sstevel@tonic-gate #ifdef sun4v
47*7c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
48*7c478bd9Sstevel@tonic-gate #endif
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * trapstat:  Trap Statistics through Dynamic Trap Table Interposition
53*7c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  * Motivation and Overview
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  * Despite being a fundamental indicator of system behavior, there has
58*7c478bd9Sstevel@tonic-gate  * historically been very little insight provided into the frequency and cost
59*7c478bd9Sstevel@tonic-gate  * of machine-specific traps.  The lack of insight has been especially acute
60*7c478bd9Sstevel@tonic-gate  * on UltraSPARC microprocessors:  because these microprocessors handle TLB
61*7c478bd9Sstevel@tonic-gate  * misses as software traps, the frequency and duration of traps play a
62*7c478bd9Sstevel@tonic-gate  * decisive role in the performance of the memory system.  As applications have
63*7c478bd9Sstevel@tonic-gate  * increasingly outstripped TLB reach, this has become increasingly true.
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * Part of the difficulty of observing trap behavior is that the trap handlers
66*7c478bd9Sstevel@tonic-gate  * are so frequently called (e.g. millions of times per second) that any
67*7c478bd9Sstevel@tonic-gate  * permanently enabled instrumentation would induce an unacceptable performance
68*7c478bd9Sstevel@tonic-gate  * degradation.  Thus, it is a constraint on any trap observability
69*7c478bd9Sstevel@tonic-gate  * infrastructure that it have no probe effect when not explicitly enabled.
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * The basic idea, then, is to create an interposing trap table in which each
72*7c478bd9Sstevel@tonic-gate  * entry increments a per-trap, in-memory counter and then jumps to the actual,
73*7c478bd9Sstevel@tonic-gate  * underlying trap table entry.  To enable trapstat, we atomically write to the
74*7c478bd9Sstevel@tonic-gate  * trap base address (%tba) register to point to our interposing trap table.
75*7c478bd9Sstevel@tonic-gate  * (Note that per-CPU statistics fall out by creating a different trap table
76*7c478bd9Sstevel@tonic-gate  * for each CPU.)
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * Implementation Details
79*7c478bd9Sstevel@tonic-gate  *
80*7c478bd9Sstevel@tonic-gate  * While the idea is straight-forward, a nuance of SPARC V9 slightly
81*7c478bd9Sstevel@tonic-gate  * complicates the implementation.  Unlike its predecessors, SPARC V9 supports
82*7c478bd9Sstevel@tonic-gate  * the notion of nested traps.  The trap level is kept in the TL register:
83*7c478bd9Sstevel@tonic-gate  * during normal operation it is 0; when a trap is taken, the TL register is
84*7c478bd9Sstevel@tonic-gate  * incremented by 1.  To aid system software, SPARC V9 breaks the trap table
85*7c478bd9Sstevel@tonic-gate  * into two halves:  the lower half contains the trap handlers for traps taken
86*7c478bd9Sstevel@tonic-gate  * when TL is 0; the upper half contains the trap handlers for traps taken
87*7c478bd9Sstevel@tonic-gate  * when TL is greater than 0.  Each half is further subdivided into two
88*7c478bd9Sstevel@tonic-gate  * subsequent halves:  the lower half contains the trap handlers for traps
89*7c478bd9Sstevel@tonic-gate  * other than those induced by the trap instruction (Tcc variants); the upper
90*7c478bd9Sstevel@tonic-gate  * half contains the trap handlers for traps induced by the trap instruction.
91*7c478bd9Sstevel@tonic-gate  * This gives a total of four ranges, with each range containing 256 traps:
92*7c478bd9Sstevel@tonic-gate  *
93*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 3ff
94*7c478bd9Sstevel@tonic-gate  *       |                                |   .
95*7c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL>0     |   .
96*7c478bd9Sstevel@tonic-gate  *       |                                |   .
97*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 300
98*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 2ff
99*7c478bd9Sstevel@tonic-gate  *       |                                |   .
100*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .
101*7c478bd9Sstevel@tonic-gate  *       |                                |   .
102*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200
103*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff
104*7c478bd9Sstevel@tonic-gate  *       |                                |   .
105*7c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .
106*7c478bd9Sstevel@tonic-gate  *       |                                |   .
107*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100
108*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff
109*7c478bd9Sstevel@tonic-gate  *       |                                |   .
110*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .
111*7c478bd9Sstevel@tonic-gate  *       |                                |   .
112*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  *
115*7c478bd9Sstevel@tonic-gate  * Solaris, however, doesn't have reason to support trap instructions when
116*7c478bd9Sstevel@tonic-gate  * TL>0 (only privileged code may execute at TL>0; not supporting this only
117*7c478bd9Sstevel@tonic-gate  * constrains our own implementation).  The trap table actually looks like:
118*7c478bd9Sstevel@tonic-gate  *
119*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff
120*7c478bd9Sstevel@tonic-gate  *       |                                |   .
121*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .
122*7c478bd9Sstevel@tonic-gate  *       |                                |   .
123*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200
124*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff
125*7c478bd9Sstevel@tonic-gate  *       |                                |   .
126*7c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .
127*7c478bd9Sstevel@tonic-gate  *       |                                |   .
128*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100
129*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff
130*7c478bd9Sstevel@tonic-gate  *       |                                |   .
131*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .
132*7c478bd9Sstevel@tonic-gate  *       |                                |   .
133*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
134*7c478bd9Sstevel@tonic-gate  *
135*7c478bd9Sstevel@tonic-gate  * Putatively to aid system software, SPARC V9 has the notion of multiple
136*7c478bd9Sstevel@tonic-gate  * sets of global registers.  UltraSPARC defines four sets of global
137*7c478bd9Sstevel@tonic-gate  * registers:
138*7c478bd9Sstevel@tonic-gate  *
139*7c478bd9Sstevel@tonic-gate  *    Normal Globals
140*7c478bd9Sstevel@tonic-gate  *    Alternate Globals (AGs)
141*7c478bd9Sstevel@tonic-gate  *    MMU Globals (MGs)
142*7c478bd9Sstevel@tonic-gate  *    Interrupt Globals (IGs)
143*7c478bd9Sstevel@tonic-gate  *
144*7c478bd9Sstevel@tonic-gate  * The set of globals in use is controlled by bits in PSTATE; when TL is 0
145*7c478bd9Sstevel@tonic-gate  * (and PSTATE has not been otherwise explicitly modified), the Normal Globals
146*7c478bd9Sstevel@tonic-gate  * are in use.  When a trap is issued, PSTATE is modified to point to a set of
147*7c478bd9Sstevel@tonic-gate  * globals corresponding to the trap type.  Most traps correspond to the
148*7c478bd9Sstevel@tonic-gate  * Alternate Globals, with a minority corresponding to the MMU Globals, and
149*7c478bd9Sstevel@tonic-gate  * only the interrupt-vector trap (vector 0x60) corresponding to the Interrupt
150*7c478bd9Sstevel@tonic-gate  * Globals.  (The complete mapping can be found in the UltraSPARC I&II User's
151*7c478bd9Sstevel@tonic-gate  * Manual.)
152*7c478bd9Sstevel@tonic-gate  *
153*7c478bd9Sstevel@tonic-gate  * Note that the sets of globals are per trap _type_, not per trap _level_.
154*7c478bd9Sstevel@tonic-gate  * Thus, when executing a TL>0 trap handler, one may not have registers
155*7c478bd9Sstevel@tonic-gate  * available (for example, both trap-instruction traps and spill traps execute
156*7c478bd9Sstevel@tonic-gate  * on the alternate globals; if a trap-instruction trap induces a window spill,
157*7c478bd9Sstevel@tonic-gate  * the window spill handler has no available globals).  For trapstat, this is
158*7c478bd9Sstevel@tonic-gate  * problematic:  a register is required to transfer control from one arbitrary
159*7c478bd9Sstevel@tonic-gate  * location (in the interposing trap table) to another (in the actual trap
160*7c478bd9Sstevel@tonic-gate  * table).
161*7c478bd9Sstevel@tonic-gate  *
162*7c478bd9Sstevel@tonic-gate  * We solve this problem by exploiting the trap table's location at the bottom
163*7c478bd9Sstevel@tonic-gate  * of valid kernel memory (i.e. at KERNELBASE).  We locate the interposing trap
164*7c478bd9Sstevel@tonic-gate  * tables just below KERNELBASE -- thereby allowing us to use a branch-always
165*7c478bd9Sstevel@tonic-gate  * instruction (ba) instead of a jump instruction (jmp) to transfer control
166*7c478bd9Sstevel@tonic-gate  * from the TL>0 entries in the interposing trap table to the TL>0 entries in
167*7c478bd9Sstevel@tonic-gate  * the actual trap table.  (N.B. while this allows trap table interposition to
168*7c478bd9Sstevel@tonic-gate  * work, it necessarily limits trapstat to only recording information about
169*7c478bd9Sstevel@tonic-gate  * TL=0 traps -- there is no way to increment a counter without using a
170*7c478bd9Sstevel@tonic-gate  * register.)  Diagrammatically:
171*7c478bd9Sstevel@tonic-gate  *
172*7c478bd9Sstevel@tonic-gate  *  Actual trap table:
173*7c478bd9Sstevel@tonic-gate  *
174*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff
175*7c478bd9Sstevel@tonic-gate  *       |                                |   .
176*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL>0   |   .   <-----------------------+
177*7c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------------------|-+
178*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200  <-----------------------|-|-+
179*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 1ff                          | | |
180*7c478bd9Sstevel@tonic-gate  *       |                                |   .                           | | |
181*7c478bd9Sstevel@tonic-gate  *       |     Trap instruction, TL=0     |   .   <-----------------+     | | |
182*7c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------------|-+   | | |
183*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100  <-----------------|-|-+ | | |
184*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 0ff                    | | | | | |
185*7c478bd9Sstevel@tonic-gate  *       |                                |   .                     | | | | | |
186*7c478bd9Sstevel@tonic-gate  *       |   Non-trap instruction, TL=0   |   .   <-----------+     | | | | | |
187*7c478bd9Sstevel@tonic-gate  *       |                                |   .   <-----------|-+   | | | | | |
188*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000  <-----------|-|-+ | | | | | |
189*7c478bd9Sstevel@tonic-gate  *        KERNELBASE                                          | | | | | | | | |
190*7c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
191*7c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
192*7c478bd9Sstevel@tonic-gate  *  Interposing trap table:                                   | | | | | | | | |
193*7c478bd9Sstevel@tonic-gate  *                                                            | | | | | | | | |
194*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 2ff              | | | | | | | | |
195*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
196*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
197*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | | | | |
198*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 203              | | | | | | | | |
199*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|-+ | |
200*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 202              | | | | | |   | |
201*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|---+ |
202*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 201              | | | | | |     |
203*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------|-|-|-|-|-|-----+
204*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 200              | | | | | |
205*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
206*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
207*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | | | | |
208*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 103              | | | | | |
209*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | | | | |
210*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------------+ | |
211*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 102              | | |   | |
212*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |   | |
213*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      ---------------------+ |
214*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 101              | | |     |
215*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |     |
216*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -----------------------+
217*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 100              | | |
218*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
219*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
220*7c478bd9Sstevel@tonic-gate  *       |  ...                           |   .               | | |
221*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 003              | | |
222*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                   | | |
223*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -------------+ | |
224*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 002                | |
225*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                     | |
226*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      ---------------+ |
227*7c478bd9Sstevel@tonic-gate  *       |- - - - - - - - - - - - - - - - +- 001                  |
228*7c478bd9Sstevel@tonic-gate  *       |  (Increment counter)           |                       |
229*7c478bd9Sstevel@tonic-gate  *       |  ba,a                          |      -----------------+
230*7c478bd9Sstevel@tonic-gate  *       +--------------------------------+- 000
231*7c478bd9Sstevel@tonic-gate  *        KERNELBASE - tstat_total_size
232*7c478bd9Sstevel@tonic-gate  *
233*7c478bd9Sstevel@tonic-gate  * tstat_total_size is the number of pages required for each trap table.  It
234*7c478bd9Sstevel@tonic-gate  * must be true that KERNELBASE - tstat_total_size is less than the maximum
235*7c478bd9Sstevel@tonic-gate  * branch displacement; if each CPU were to consume a disjoint virtual range
236*7c478bd9Sstevel@tonic-gate  * below KERNELBASE for its trap table, we could support at most
237*7c478bd9Sstevel@tonic-gate  * (maximum_branch_displacement / tstat_total_size) CPUs.  The maximum branch
238*7c478bd9Sstevel@tonic-gate  * displacement for Bicc variants is just under eight megabytes, and (because
239*7c478bd9Sstevel@tonic-gate  * the %tba must be 32K aligned), tstat_total_size must be at least 32K; if
240*7c478bd9Sstevel@tonic-gate  * each CPU were to consume a disjoint virtual range, we would have an
241*7c478bd9Sstevel@tonic-gate  * unacceptably low upper bound of 256 CPUs.
242*7c478bd9Sstevel@tonic-gate  *
243*7c478bd9Sstevel@tonic-gate  * While there are tricks that one could use to address this constraint (e.g.,
244*7c478bd9Sstevel@tonic-gate  * creating trampolines every maximum_branch_displacement bytes), we instead
245*7c478bd9Sstevel@tonic-gate  * solve this by not permitting each CPU to consume a disjoint virtual range.
246*7c478bd9Sstevel@tonic-gate  * Rather, we have each CPU's interposing trap table use the _same_ virtual
247*7c478bd9Sstevel@tonic-gate  * range, but we back the trap tables with disjoint physical memory.  Normally,
248*7c478bd9Sstevel@tonic-gate  * such one-to-many virtual-to-physical mappings are illegal; this is
249*7c478bd9Sstevel@tonic-gate  * permissible here only because the pages for the interposing trap table are
250*7c478bd9Sstevel@tonic-gate  * necessarily locked in the TLB.  (The CPUs thus never have the opportunity to
251*7c478bd9Sstevel@tonic-gate  * discover that they have conflicting translations.)
252*7c478bd9Sstevel@tonic-gate  *
253*7c478bd9Sstevel@tonic-gate  * On CMT architectures in which CPUs can share MMUs, the above trick will not
254*7c478bd9Sstevel@tonic-gate  * work: two CPUs that share an MMU cannot have the same virtual address map
255*7c478bd9Sstevel@tonic-gate  * to disjoint physical pages.  On these architectures, any CPUs sharing the
256*7c478bd9Sstevel@tonic-gate  * same MMU must consume a disjoint 32K virtual address range -- limiting the
257*7c478bd9Sstevel@tonic-gate  * number of CPUs sharing an MMU on these architectures to 256 due to the
258*7c478bd9Sstevel@tonic-gate  * branch displacement limitation described above.  On the sun4v architecture,
259*7c478bd9Sstevel@tonic-gate  * there is a further limitation: a guest may not have more than eight locked
260*7c478bd9Sstevel@tonic-gate  * TLB entries per MMU.  To allow operation under this restriction, the
261*7c478bd9Sstevel@tonic-gate  * interposing trap table and the trap statistics are each accessed through
262*7c478bd9Sstevel@tonic-gate  * a single 4M TLB entry.  This limits the footprint to two locked entries
263*7c478bd9Sstevel@tonic-gate  * (one for the I-TLB and one for the D-TLB), but further restricts the number
264*7c478bd9Sstevel@tonic-gate  * of CPUs to 128 per MMU.  However, support for more than 128 CPUs can easily
265*7c478bd9Sstevel@tonic-gate  * be added via a hybrid scheme, where the same 4M virtual address is used
266*7c478bd9Sstevel@tonic-gate  * on different MMUs.
267*7c478bd9Sstevel@tonic-gate  *
268*7c478bd9Sstevel@tonic-gate  *
269*7c478bd9Sstevel@tonic-gate  * TLB Statistics
270*7c478bd9Sstevel@tonic-gate  *
271*7c478bd9Sstevel@tonic-gate  * Because TLB misses are an important component of system performance, we wish
272*7c478bd9Sstevel@tonic-gate  * to know much more about these traps than simply the number received.
273*7c478bd9Sstevel@tonic-gate  * Specifically, we wish to know:
274*7c478bd9Sstevel@tonic-gate  *
275*7c478bd9Sstevel@tonic-gate  *  (a)	The amount of time spent executing the TLB miss handler
276*7c478bd9Sstevel@tonic-gate  *  (b)	TLB misses versus TSB misses
277*7c478bd9Sstevel@tonic-gate  *  (c) Kernel-level misses versus user-level misses
278*7c478bd9Sstevel@tonic-gate  *  (d) Misses per pagesize
279*7c478bd9Sstevel@tonic-gate  *
280*7c478bd9Sstevel@tonic-gate  * TLB Statistics: Time Spent Executing
281*7c478bd9Sstevel@tonic-gate  *
282*7c478bd9Sstevel@tonic-gate  * To accurately determine the amount of time spent executing the TLB miss
283*7c478bd9Sstevel@tonic-gate  * handler, one must get a timestamp on trap entry and trap exit, subtract the
284*7c478bd9Sstevel@tonic-gate  * latter from the former, and add the result to an accumulating count.
285*7c478bd9Sstevel@tonic-gate  * Consider flow of control during normal TLB miss processing (where "ldx
286*7c478bd9Sstevel@tonic-gate  * [%g2], %g2" is an arbitrary TLB-missing instruction):
287*7c478bd9Sstevel@tonic-gate  *
288*7c478bd9Sstevel@tonic-gate  * + - - - - - - - -+
289*7c478bd9Sstevel@tonic-gate  * :                :
290*7c478bd9Sstevel@tonic-gate  * : ldx [%g2], %g2 :<-------------------------------------------------------+
291*7c478bd9Sstevel@tonic-gate  * :                :              Return from trap:                         |
292*7c478bd9Sstevel@tonic-gate  * + - - - - - - - -+                TL <- TL - 1 (0)                        |
293*7c478bd9Sstevel@tonic-gate  *	  |                          %pc <- TSTATE[TL].TPC (address of load) |
294*7c478bd9Sstevel@tonic-gate  *	  | TLB miss:                                                        |
295*7c478bd9Sstevel@tonic-gate  *        |   TL <- TL + 1 (1)                                               |
296*7c478bd9Sstevel@tonic-gate  *        |   %pc <- TLB-miss-trap-handler                                   |
297*7c478bd9Sstevel@tonic-gate  *        |                                                                  |
298*7c478bd9Sstevel@tonic-gate  *        v                                                                  |
299*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +                                         |
300*7c478bd9Sstevel@tonic-gate  * :                               :                                         |
301*7c478bd9Sstevel@tonic-gate  * : Lookup VA in TSB              :                                         |
302*7c478bd9Sstevel@tonic-gate  * : If (hit)                      :                                         |
303*7c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                                         |
304*7c478bd9Sstevel@tonic-gate  * : Else                          :                                         |
305*7c478bd9Sstevel@tonic-gate  * :     Lookup VA (hme hash table :                                         |
306*7c478bd9Sstevel@tonic-gate  * :                or segkpm)     :                                         |
307*7c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                                         |
308*7c478bd9Sstevel@tonic-gate  * : Endif                         :                                         |
309*7c478bd9Sstevel@tonic-gate  * : Issue "retry"  ---------------------------------------------------------+
310*7c478bd9Sstevel@tonic-gate  * :                               :
311*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +
312*7c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler
313*7c478bd9Sstevel@tonic-gate  *
314*7c478bd9Sstevel@tonic-gate  *
315*7c478bd9Sstevel@tonic-gate  * As the above diagram indicates, interposing on the trap table allows one
316*7c478bd9Sstevel@tonic-gate  * only to determine a timestamp on trap _entry_:  when the TLB miss handler
317*7c478bd9Sstevel@tonic-gate  * has completed filling the TLB, a "retry" will be issued, and control will
318*7c478bd9Sstevel@tonic-gate  * transfer immediately back to the missing %pc.
319*7c478bd9Sstevel@tonic-gate  *
320*7c478bd9Sstevel@tonic-gate  * To obtain a timestamp on trap exit, we must then somehow interpose between
321*7c478bd9Sstevel@tonic-gate  * the "retry" and the subsequent control transfer to the TLB-missing
322*7c478bd9Sstevel@tonic-gate  * instruction.  To do this, we _push_ a trap level.  The basic idea is to
323*7c478bd9Sstevel@tonic-gate  * spoof a TLB miss by raising TL, setting the %tpc to be within text
324*7c478bd9Sstevel@tonic-gate  * controlled by trapstat (the "TLB return entry") and branching to the
325*7c478bd9Sstevel@tonic-gate  * underlying TLB miss handler.  When the TLB miss handler issues its "retry",
326*7c478bd9Sstevel@tonic-gate  * control will transfer not to the TLB-missing instruction, but rather to the
327*7c478bd9Sstevel@tonic-gate  * TLB return entry.  This code can then obtain a timestamp, and issue its own
328*7c478bd9Sstevel@tonic-gate  * "retry" -- thereby correctly returning to the TLB-missing instruction.
329*7c478bd9Sstevel@tonic-gate  * Here is the above TLB miss flow control diagram modified to reflect
330*7c478bd9Sstevel@tonic-gate  * trapstat's operation:
331*7c478bd9Sstevel@tonic-gate  *
332*7c478bd9Sstevel@tonic-gate  * + - - - - - - - -+
333*7c478bd9Sstevel@tonic-gate  * :                :
334*7c478bd9Sstevel@tonic-gate  * : ldx [%g2], %g2 :<-------------------------------------------------------+
335*7c478bd9Sstevel@tonic-gate  * :                :             Return from trap:                          |
336*7c478bd9Sstevel@tonic-gate  * + - - - - - - - -+               TL <- TL - 1 (0)                         |
337*7c478bd9Sstevel@tonic-gate  *	  |                         %pc <- TSTATE[TL].TPC (address of load)  |
338*7c478bd9Sstevel@tonic-gate  *	  | TLB miss:                                                        |
339*7c478bd9Sstevel@tonic-gate  *        |   TL <- TL + 1 (1)                                               |
340*7c478bd9Sstevel@tonic-gate  *        |   %pc <- TLB-miss-trap-handler (trapstat)                        |
341*7c478bd9Sstevel@tonic-gate  *        |                                                                  |
342*7c478bd9Sstevel@tonic-gate  *        v                                    TLB-return-entry (trapstat)   |
343*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - - - - +    + - - - - - - - - - - - - - +  |
344*7c478bd9Sstevel@tonic-gate  * :                                     :    :                           :  |
345*7c478bd9Sstevel@tonic-gate  * : Record timestamp                    :    : Record timestamp          :  |
346*7c478bd9Sstevel@tonic-gate  * : TL <- 2                             :    : Take timestamp difference :  |
347*7c478bd9Sstevel@tonic-gate  * : TSTATE[1].TPC <- TLB-return-entry   :    : Add to running total      :  |
348*7c478bd9Sstevel@tonic-gate  * : ba,a TLB-miss-trap-handler -----------+  : Issue "retry"  --------------+
349*7c478bd9Sstevel@tonic-gate  * :                                     : |  :                           :
350*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - - - - + |  + - - - - - - - - - - - - - +
351*7c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler	           |                  ^
352*7c478bd9Sstevel@tonic-gate  *  (trapstat)                             |                  |
353*7c478bd9Sstevel@tonic-gate  *                                         |                  |
354*7c478bd9Sstevel@tonic-gate  *                                         |                  |
355*7c478bd9Sstevel@tonic-gate  *                 +-----------------------+                  |
356*7c478bd9Sstevel@tonic-gate  *                 |                                          |
357*7c478bd9Sstevel@tonic-gate  *                 |                                          |
358*7c478bd9Sstevel@tonic-gate  *                 v                                          |
359*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +                          |
360*7c478bd9Sstevel@tonic-gate  * :                               :                          |
361*7c478bd9Sstevel@tonic-gate  * : Lookup VA in TSB              :                          |
362*7c478bd9Sstevel@tonic-gate  * : If (hit)                      :                          |
363*7c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                          |
364*7c478bd9Sstevel@tonic-gate  * : Else                          :                          |
365*7c478bd9Sstevel@tonic-gate  * :     Lookup VA (hme hash table :                          |
366*7c478bd9Sstevel@tonic-gate  * :                or segkpm)     :                          |
367*7c478bd9Sstevel@tonic-gate  * :     Fill TLB                  :                          |
368*7c478bd9Sstevel@tonic-gate  * : Endif                         :                          |
369*7c478bd9Sstevel@tonic-gate  * : Issue "retry"  ------------------------------------------+
370*7c478bd9Sstevel@tonic-gate  * :                               : Return from trap:
371*7c478bd9Sstevel@tonic-gate  * + - - - - - - - - - - - - - - - +   TL <- TL - 1 (1)
372*7c478bd9Sstevel@tonic-gate  *  TLB-miss-trap-handler              %pc <- TSTATE[TL].TPC (TLB-return-entry)
373*7c478bd9Sstevel@tonic-gate  *
374*7c478bd9Sstevel@tonic-gate  *
375*7c478bd9Sstevel@tonic-gate  * A final subterfuge is required to complete our artifice:  if we miss in
376*7c478bd9Sstevel@tonic-gate  * the TLB, the TSB _and_ the subsequent hash or segkpm lookup (that is, if
377*7c478bd9Sstevel@tonic-gate  * there is no valid translation for the TLB-missing address), common system
378*7c478bd9Sstevel@tonic-gate  * software will need to accurately determine the %tpc as part of its page
379*7c478bd9Sstevel@tonic-gate  * fault handling. We therefore modify the kernel to check the %tpc in this
380*7c478bd9Sstevel@tonic-gate  * case: if the %tpc falls within the VA range controlled by trapstat and
381*7c478bd9Sstevel@tonic-gate  * the TL is 2, TL is simply lowered back to 1 (this check is implemented
382*7c478bd9Sstevel@tonic-gate  * by the TSTAT_CHECK_TL1 macro).  Lowering TL to 1 has the effect of
383*7c478bd9Sstevel@tonic-gate  * discarding the state pushed by trapstat.
384*7c478bd9Sstevel@tonic-gate  *
385*7c478bd9Sstevel@tonic-gate  * TLB Statistics: TLB Misses versus TSB Misses
386*7c478bd9Sstevel@tonic-gate  *
387*7c478bd9Sstevel@tonic-gate  * Distinguishing TLB misses from TSB misses requires further interposition
388*7c478bd9Sstevel@tonic-gate  * on the TLB miss handler:  we cannot know a priori or a posteriori if a
389*7c478bd9Sstevel@tonic-gate  * given VA will or has hit in the TSB.
390*7c478bd9Sstevel@tonic-gate  *
391*7c478bd9Sstevel@tonic-gate  * We achieve this distinction by adding a second TLB return entry almost
392*7c478bd9Sstevel@tonic-gate  * identical to the first -- differing only in the address to which it
393*7c478bd9Sstevel@tonic-gate  * stores its results.  We then modify the TLB miss handlers of the kernel
394*7c478bd9Sstevel@tonic-gate  * such that they check the %tpc when they determine that a TLB miss has
395*7c478bd9Sstevel@tonic-gate  * subsequently missed in the TSB:  if the %tpc lies within trapstat's VA
396*7c478bd9Sstevel@tonic-gate  * range and TL is 2 (that is, if trapstat is running), the TLB miss handler
397*7c478bd9Sstevel@tonic-gate  * _increments_ the %tpc by the size of the TLB return entry.  The ensuing
398*7c478bd9Sstevel@tonic-gate  * "retry" will thus transfer control to the second TLB return entry, and
399*7c478bd9Sstevel@tonic-gate  * the time spent in the handler will be accumulated in a memory location
400*7c478bd9Sstevel@tonic-gate  * specific to TSB misses.
401*7c478bd9Sstevel@tonic-gate  *
402*7c478bd9Sstevel@tonic-gate  * N.B.:  To minimize the amount of knowledge the kernel must have of trapstat,
403*7c478bd9Sstevel@tonic-gate  * we do not allow the kernel to hard-code the size of the TLB return entry.
404*7c478bd9Sstevel@tonic-gate  * Rather, the actual tsbmiss handler executes a known instruction at the
405*7c478bd9Sstevel@tonic-gate  * corresponding tsbmiss patch points (see the tstat_tsbmiss_patch_table) with
406*7c478bd9Sstevel@tonic-gate  * the %tpc in %g7:  when trapstat is not running, these points contain the
407*7c478bd9Sstevel@tonic-gate  * harmless TSTAT_TSBMISS_INSTR instruction ("add %g7, 0, %g7"). Before
408*7c478bd9Sstevel@tonic-gate  * running, trapstat modifies the instructions at these patch points such
409*7c478bd9Sstevel@tonic-gate  * that the simm13 equals the size of the TLB return entry.
410*7c478bd9Sstevel@tonic-gate  *
411*7c478bd9Sstevel@tonic-gate  * TLB Statistics: Kernel-level Misses versus User-level Misses
412*7c478bd9Sstevel@tonic-gate  *
413*7c478bd9Sstevel@tonic-gate  * Differentiating user-level misses from kernel-level misses employs a
414*7c478bd9Sstevel@tonic-gate  * similar technique, but is simplified by the ability to distinguish a
415*7c478bd9Sstevel@tonic-gate  * user-level miss from a kernel-level miss a priori by reading the context
416*7c478bd9Sstevel@tonic-gate  * register:  we implement kernel-/user-level differentiation by again doubling
417*7c478bd9Sstevel@tonic-gate  * the number of TLB return entries, and setting the %tpc to the appropriate
418*7c478bd9Sstevel@tonic-gate  * TLB return entry in trapstat's TLB miss handler.  Together with the doubling
419*7c478bd9Sstevel@tonic-gate  * of entries required for TLB-miss/TSB-miss differentiation, this yields a
420*7c478bd9Sstevel@tonic-gate  * total of four TLB return entries:
421*7c478bd9Sstevel@tonic-gate  *
422*7c478bd9Sstevel@tonic-gate  *	Level		TSB hit?	Structure member
423*7c478bd9Sstevel@tonic-gate  *	------------------------------------------------------------
424*7c478bd9Sstevel@tonic-gate  *	Kernel		Yes		tstat_tlbret_t.ttlbr_ktlb
425*7c478bd9Sstevel@tonic-gate  *	Kernel		No		tstat_tlbret_t.ttlbr_ktsb
426*7c478bd9Sstevel@tonic-gate  *	User		Yes		tstat_tlbret_t.ttlbr_utlb
427*7c478bd9Sstevel@tonic-gate  *	User		No		tstat_tlbret_t.ttlbr_utsb
428*7c478bd9Sstevel@tonic-gate  *
429*7c478bd9Sstevel@tonic-gate  * TLB Statistics: Misses per Pagesize
430*7c478bd9Sstevel@tonic-gate  *
431*7c478bd9Sstevel@tonic-gate  * As with the TLB-/TSB-miss differentiation, we have no way of determining
432*7c478bd9Sstevel@tonic-gate  * pagesize a priori.  This is therefore implemented by mandating a new rule:
433*7c478bd9Sstevel@tonic-gate  * whenever the kernel fills the TLB in its TLB miss handler, the TTE
434*7c478bd9Sstevel@tonic-gate  * corresponding to the TLB-missing VA must be in %g5 when the handler
435*7c478bd9Sstevel@tonic-gate  * executes its "retry".  This allows the TLB return entry to determine
436*7c478bd9Sstevel@tonic-gate  * pagesize by simply looking at the pagesize field in the TTE stored in
437*7c478bd9Sstevel@tonic-gate  * %g5.
438*7c478bd9Sstevel@tonic-gate  *
439*7c478bd9Sstevel@tonic-gate  * TLB Statistics: Probe Effect
440*7c478bd9Sstevel@tonic-gate  *
441*7c478bd9Sstevel@tonic-gate  * As one might imagine, gathering TLB statistics by pushing a trap level
442*7c478bd9Sstevel@tonic-gate  * induces significant probe effect.  To account for this probe effect,
443*7c478bd9Sstevel@tonic-gate  * trapstat attempts to observe it by executing a code sequence with a known
444*7c478bd9Sstevel@tonic-gate  * number of TLB misses both before and after interposing on the trap table.
445*7c478bd9Sstevel@tonic-gate  * This allows trapstat to determine a per-trap probe effect which can then be
446*7c478bd9Sstevel@tonic-gate  * factored into the "%tim" fields of the trapstat command.
447*7c478bd9Sstevel@tonic-gate  *
448*7c478bd9Sstevel@tonic-gate  * Note that on sun4v platforms, TLB misses are normally handled by the
449*7c478bd9Sstevel@tonic-gate  * hypervisor or the hardware TSB walker. Thus no fast MMU miss information
450*7c478bd9Sstevel@tonic-gate  * is reported for normal operation. However, when trapstat is invoked with
451*7c478bd9Sstevel@tonic-gate  * -t or -T option to collect detailed TLB statistics, kernel takes
452*7c478bd9Sstevel@tonic-gate  * over TLB miss handling. This results in significantly more overhead
453*7c478bd9Sstevel@tonic-gate  * and TLB statistics may not be as accurate as on sun4u platforms.
454*7c478bd9Sstevel@tonic-gate  *
455*7c478bd9Sstevel@tonic-gate  * Locking
456*7c478bd9Sstevel@tonic-gate  *
457*7c478bd9Sstevel@tonic-gate  * The implementation uses two locks:  tstat_lock (a local lock) and the global
458*7c478bd9Sstevel@tonic-gate  * cpu_lock.  tstat_lock is used to assure trapstat's consistency in the
459*7c478bd9Sstevel@tonic-gate  * presence of multithreaded /dev/trapstat consumers (while as of this writing
460*7c478bd9Sstevel@tonic-gate  * the only consumer of /dev/trapstat is single threaded, it is obviously
461*7c478bd9Sstevel@tonic-gate  * necessary to correctly support multithreaded access).  cpu_lock is held
462*7c478bd9Sstevel@tonic-gate  * whenever CPUs are being manipulated directly, to prevent them from
463*7c478bd9Sstevel@tonic-gate  * disappearing in the process.  Because trapstat's DR callback
464*7c478bd9Sstevel@tonic-gate  * (trapstat_cpu_setup()) must grab tstat_lock and is called with cpu_lock
465*7c478bd9Sstevel@tonic-gate  * held, the lock ordering is necessarily cpu_lock before tstat_lock.
466*7c478bd9Sstevel@tonic-gate  *
467*7c478bd9Sstevel@tonic-gate  */
468*7c478bd9Sstevel@tonic-gate /* END CSTYLED */
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate static dev_info_t	*tstat_devi;	/* saved in xxattach() for xxinfo() */
471*7c478bd9Sstevel@tonic-gate static int		tstat_open;	/* set if driver is open */
472*7c478bd9Sstevel@tonic-gate static kmutex_t		tstat_lock;	/* serialize access */
473*7c478bd9Sstevel@tonic-gate static vmem_t		*tstat_arena;	/* arena for TLB-locked pages */
474*7c478bd9Sstevel@tonic-gate static tstat_percpu_t	*tstat_percpu;	/* per-CPU data */
475*7c478bd9Sstevel@tonic-gate static int		tstat_running;	/* set if trapstat is running */
476*7c478bd9Sstevel@tonic-gate static tstat_data_t	*tstat_buffer;	/* staging buffer for outgoing data */
477*7c478bd9Sstevel@tonic-gate static int		tstat_options;	/* bit-wise indication of options */
478*7c478bd9Sstevel@tonic-gate static int		*tstat_enabled;	/* map of enabled trap entries */
479*7c478bd9Sstevel@tonic-gate static int		tstat_tsbmiss_patched; /* tsbmiss patch flag */
480*7c478bd9Sstevel@tonic-gate static callb_id_t	tstat_cprcb;	/* CPR callback */
481*7c478bd9Sstevel@tonic-gate static char		*tstat_probe_area; /* VA range used for probe effect */
482*7c478bd9Sstevel@tonic-gate static caddr_t		tstat_probe_phys; /* physical to back above VA */
483*7c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_time; /* time spent on probe effect */
484*7c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_before[TSTAT_PROBE_NLAPS];
485*7c478bd9Sstevel@tonic-gate static hrtime_t		tstat_probe_after[TSTAT_PROBE_NLAPS];
486*7c478bd9Sstevel@tonic-gate static uint_t		tstat_pgszs;		/* # of kernel page sizes */
487*7c478bd9Sstevel@tonic-gate static uint_t		tstat_user_pgszs;	/* # of user page sizes */
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate /*
490*7c478bd9Sstevel@tonic-gate  * sizeof tstat_data_t + pgsz data for the kernel.  For simplicity's sake, when
491*7c478bd9Sstevel@tonic-gate  * we collect data, we do it based upon szc, but when we report data back to
492*7c478bd9Sstevel@tonic-gate  * userland, we have to do it based upon the userszc which may not match.
493*7c478bd9Sstevel@tonic-gate  * So, these two variables are for internal use and exported use respectively.
494*7c478bd9Sstevel@tonic-gate  */
495*7c478bd9Sstevel@tonic-gate static size_t		tstat_data_t_size;
496*7c478bd9Sstevel@tonic-gate static size_t		tstat_data_t_exported_size;
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate static size_t		tstat_data_pages;  /* number of pages of tstat data */
499*7c478bd9Sstevel@tonic-gate static size_t		tstat_data_size;   /* tstat data size in bytes */
500*7c478bd9Sstevel@tonic-gate static size_t		tstat_total_pages; /* #data pages + #instr pages */
501*7c478bd9Sstevel@tonic-gate static size_t		tstat_total_size;  /* tstat data size + instr size */
502*7c478bd9Sstevel@tonic-gate #ifdef sun4v
503*7c478bd9Sstevel@tonic-gate static caddr_t		tstat_va;	/* VA of memory reserved for TBA */
504*7c478bd9Sstevel@tonic-gate static pfn_t		tstat_pfn;	/* PFN of memory reserved for TBA */
505*7c478bd9Sstevel@tonic-gate #endif
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate /*
508*7c478bd9Sstevel@tonic-gate  * In the above block comment, see "TLB Statistics: TLB Misses versus
509*7c478bd9Sstevel@tonic-gate  * TSB Misses" for an explanation of the tsbmiss patch points.
510*7c478bd9Sstevel@tonic-gate  */
511*7c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point;
512*7c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point_kpm;
513*7c478bd9Sstevel@tonic-gate extern uint32_t		tsbmiss_trapstat_patch_point_kpm_small;
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate /*
516*7c478bd9Sstevel@tonic-gate  * Trapstat tsbmiss patch table
517*7c478bd9Sstevel@tonic-gate  */
518*7c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t tstat_tsbmiss_patch_table[] = {
519*7c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point, 0},
520*7c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point_kpm, 0},
521*7c478bd9Sstevel@tonic-gate 	{(uint32_t *)&tsbmiss_trapstat_patch_point_kpm_small, 0},
522*7c478bd9Sstevel@tonic-gate 	{(uint32_t *)NULL, 0}
523*7c478bd9Sstevel@tonic-gate };
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate /*
526*7c478bd9Sstevel@tonic-gate  * We define some general SPARC-specific constants to allow more readable
527*7c478bd9Sstevel@tonic-gate  * relocations.
528*7c478bd9Sstevel@tonic-gate  */
529*7c478bd9Sstevel@tonic-gate #define	NOP	0x01000000
530*7c478bd9Sstevel@tonic-gate #define	HI22(v) ((uint32_t)(v) >> 10)
531*7c478bd9Sstevel@tonic-gate #define	LO10(v) ((uint32_t)(v) & 0x3ff)
532*7c478bd9Sstevel@tonic-gate #define	LO12(v) ((uint32_t)(v) & 0xfff)
533*7c478bd9Sstevel@tonic-gate #define	DISP22(from, to) \
534*7c478bd9Sstevel@tonic-gate 	((((uintptr_t)(to) - (uintptr_t)(from)) >> 2) & 0x3fffff)
535*7c478bd9Sstevel@tonic-gate #define	ASI(asi)	((asi) << 5)
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate /*
538*7c478bd9Sstevel@tonic-gate  * The interposing trap table must be locked in the I-TLB, and any data
539*7c478bd9Sstevel@tonic-gate  * referred to in the interposing trap handler must be locked in the D-TLB.
540*7c478bd9Sstevel@tonic-gate  * This function locks these pages in the appropriate TLBs by creating TTEs
541*7c478bd9Sstevel@tonic-gate  * from whole cloth, and manually loading them into the TLB.  This function is
542*7c478bd9Sstevel@tonic-gate  * called from cross call context.
543*7c478bd9Sstevel@tonic-gate  *
544*7c478bd9Sstevel@tonic-gate  * On sun4v platforms, we use 4M page size mappings to minimize the number
545*7c478bd9Sstevel@tonic-gate  * of locked down entries (i.e. permanent mappings). Each CPU uses a
546*7c478bd9Sstevel@tonic-gate  * reserved portion of that 4M page for its TBA and data.
547*7c478bd9Sstevel@tonic-gate  */
548*7c478bd9Sstevel@tonic-gate static void
549*7c478bd9Sstevel@tonic-gate trapstat_load_tlb(void)
550*7c478bd9Sstevel@tonic-gate {
551*7c478bd9Sstevel@tonic-gate #ifndef sun4v
552*7c478bd9Sstevel@tonic-gate 	int i;
553*7c478bd9Sstevel@tonic-gate #else
554*7c478bd9Sstevel@tonic-gate 	uint64_t ret;
555*7c478bd9Sstevel@tonic-gate #endif
556*7c478bd9Sstevel@tonic-gate 	tte_t tte;
557*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
558*7c478bd9Sstevel@tonic-gate 	caddr_t va = tcpu->tcpu_vabase;
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
561*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate #ifndef sun4v
564*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) {
565*7c478bd9Sstevel@tonic-gate 		tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(TTE8K) |
566*7c478bd9Sstevel@tonic-gate 			TTE_PFN_INTHI(tcpu->tcpu_pfn[i]);
567*7c478bd9Sstevel@tonic-gate 		if (i < TSTAT_INSTR_PAGES) {
568*7c478bd9Sstevel@tonic-gate 			tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) |
569*7c478bd9Sstevel@tonic-gate 				TTE_LCK_INT | TTE_CP_INT | TTE_PRIV_INT;
570*7c478bd9Sstevel@tonic-gate 			sfmmu_itlb_ld(va, KCONTEXT, &tte);
571*7c478bd9Sstevel@tonic-gate 		} else {
572*7c478bd9Sstevel@tonic-gate 			tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) |
573*7c478bd9Sstevel@tonic-gate 				TTE_LCK_INT | TTE_CP_INT | TTE_CV_INT |
574*7c478bd9Sstevel@tonic-gate 				TTE_PRIV_INT | TTE_HWWR_INT;
575*7c478bd9Sstevel@tonic-gate 			sfmmu_dtlb_ld(va, KCONTEXT, &tte);
576*7c478bd9Sstevel@tonic-gate 		}
577*7c478bd9Sstevel@tonic-gate 	}
578*7c478bd9Sstevel@tonic-gate #else /* sun4v */
579*7c478bd9Sstevel@tonic-gate 	tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(tstat_pfn);
580*7c478bd9Sstevel@tonic-gate 	tte.tte_intlo = TTE_PFN_INTLO(tstat_pfn) | TTE_CP_INT |
581*7c478bd9Sstevel@tonic-gate 		TTE_CV_INT | TTE_PRIV_INT | TTE_HWWR_INT |
582*7c478bd9Sstevel@tonic-gate 		TTE_SZ_INTLO(TTE4M);
583*7c478bd9Sstevel@tonic-gate 	ret = hv_mmu_map_perm_addr(va, KCONTEXT, *(uint64_t *)&tte,
584*7c478bd9Sstevel@tonic-gate 		MAP_ITLB | MAP_DTLB);
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 	if (ret != H_EOK)
587*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "trapstat: cannot map new TBA "
588*7c478bd9Sstevel@tonic-gate 		    "for cpu %d  (error: 0x%lx)", CPU->cpu_id, ret);
589*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
590*7c478bd9Sstevel@tonic-gate }
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate /*
593*7c478bd9Sstevel@tonic-gate  * As mentioned in the "TLB Statistics: TLB Misses versus TSB Misses" section
594*7c478bd9Sstevel@tonic-gate  * of the block comment, TLB misses are differentiated from TSB misses in
595*7c478bd9Sstevel@tonic-gate  * part by hot-patching the instructions at the tsbmiss patch points (see
596*7c478bd9Sstevel@tonic-gate  * tstat_tsbmiss_patch_table). This routine is used both to initially patch
597*7c478bd9Sstevel@tonic-gate  * the instructions, and to patch them back to their original values upon
598*7c478bd9Sstevel@tonic-gate  * restoring the original trap table.
599*7c478bd9Sstevel@tonic-gate  */
600*7c478bd9Sstevel@tonic-gate static void
601*7c478bd9Sstevel@tonic-gate trapstat_hotpatch()
602*7c478bd9Sstevel@tonic-gate {
603*7c478bd9Sstevel@tonic-gate 	uint32_t instr;
604*7c478bd9Sstevel@tonic-gate 	uint32_t simm13;
605*7c478bd9Sstevel@tonic-gate 	tstat_tsbmiss_patch_entry_t *ep;
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_TLBDATA))
610*7c478bd9Sstevel@tonic-gate 		return;
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	if (!tstat_tsbmiss_patched) {
613*7c478bd9Sstevel@tonic-gate 		/*
614*7c478bd9Sstevel@tonic-gate 		 * We haven't patched the TSB paths; do so now.
615*7c478bd9Sstevel@tonic-gate 		 */
616*7c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/
617*7c478bd9Sstevel@tonic-gate 		ASSERT(offsetof(tstat_tlbret_t, ttlbr_ktsb) -
618*7c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_ktlb) ==
619*7c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_utsb) -
620*7c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_utlb));
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 		simm13 = offsetof(tstat_tlbret_t, ttlbr_ktsb) -
623*7c478bd9Sstevel@tonic-gate 		    offsetof(tstat_tlbret_t, ttlbr_ktlb);
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 		for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) {
626*7c478bd9Sstevel@tonic-gate 			ASSERT(ep->tpe_instr == 0);
627*7c478bd9Sstevel@tonic-gate 			instr = ep->tpe_instr = *ep->tpe_addr;
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 			/*
630*7c478bd9Sstevel@tonic-gate 			 * Assert that the instruction we're about to patch is
631*7c478bd9Sstevel@tonic-gate 			 * "add %g7, 0, %g7" (0x8e01e000).
632*7c478bd9Sstevel@tonic-gate 			 */
633*7c478bd9Sstevel@tonic-gate 			ASSERT(instr == TSTAT_TSBMISS_INSTR);
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 			instr |= simm13;
636*7c478bd9Sstevel@tonic-gate 			hot_patch_kernel_text((caddr_t)ep->tpe_addr,
637*7c478bd9Sstevel@tonic-gate 			    instr, sizeof (instr));
638*7c478bd9Sstevel@tonic-gate 		}
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 		tstat_tsbmiss_patched = 1;
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	} else {
643*7c478bd9Sstevel@tonic-gate 		/*
644*7c478bd9Sstevel@tonic-gate 		 * Remove patches from the TSB paths.
645*7c478bd9Sstevel@tonic-gate 		 */
646*7c478bd9Sstevel@tonic-gate 		for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) {
647*7c478bd9Sstevel@tonic-gate 			ASSERT(ep->tpe_instr == TSTAT_TSBMISS_INSTR);
648*7c478bd9Sstevel@tonic-gate 			hot_patch_kernel_text((caddr_t)ep->tpe_addr,
649*7c478bd9Sstevel@tonic-gate 			    ep->tpe_instr, sizeof (instr));
650*7c478bd9Sstevel@tonic-gate 			ep->tpe_instr = 0;
651*7c478bd9Sstevel@tonic-gate 		}
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate 		tstat_tsbmiss_patched = 0;
654*7c478bd9Sstevel@tonic-gate 	}
655*7c478bd9Sstevel@tonic-gate }
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate /*
658*7c478bd9Sstevel@tonic-gate  * This is the routine executed to clock the performance of the trap table,
659*7c478bd9Sstevel@tonic-gate  * executed both before and after interposing on the trap table to attempt to
660*7c478bd9Sstevel@tonic-gate  * determine probe effect.  The probe effect is used to adjust the "%tim"
661*7c478bd9Sstevel@tonic-gate  * fields of trapstat's -t and -T output; we only use TLB misses to clock the
662*7c478bd9Sstevel@tonic-gate  * trap table.  We execute the inner loop (which is designed to exceed the
663*7c478bd9Sstevel@tonic-gate  * TLB's reach) nlaps times, taking the best time as our time (thereby
664*7c478bd9Sstevel@tonic-gate  * factoring out the effects of interrupts, cache misses or other perturbing
665*7c478bd9Sstevel@tonic-gate  * events.
666*7c478bd9Sstevel@tonic-gate  */
667*7c478bd9Sstevel@tonic-gate static hrtime_t
668*7c478bd9Sstevel@tonic-gate trapstat_probe_laps(int nlaps, hrtime_t *buf)
669*7c478bd9Sstevel@tonic-gate {
670*7c478bd9Sstevel@tonic-gate 	int i, j = 0;
671*7c478bd9Sstevel@tonic-gate 	hrtime_t ts, best = INT64_MAX;
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	while (nlaps--) {
674*7c478bd9Sstevel@tonic-gate 		ts = rdtick();
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSTAT_PROBE_SIZE; i += MMU_PAGESIZE)
677*7c478bd9Sstevel@tonic-gate 			*((volatile char *)&tstat_probe_area[i]);
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 		if ((ts = rdtick() - ts) < best)
680*7c478bd9Sstevel@tonic-gate 			best = ts;
681*7c478bd9Sstevel@tonic-gate 		buf[j++] = ts;
682*7c478bd9Sstevel@tonic-gate 	}
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate 	return (best);
685*7c478bd9Sstevel@tonic-gate }
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate /*
688*7c478bd9Sstevel@tonic-gate  * This routine determines the probe effect by calling trapstat_probe_laps()
689*7c478bd9Sstevel@tonic-gate  * both without and with the interposing trap table.  Note that this is
690*7c478bd9Sstevel@tonic-gate  * called from a cross call on the desired CPU, and that it is called on
691*7c478bd9Sstevel@tonic-gate  * every CPU (this is necessary because the probe effect may differ from
692*7c478bd9Sstevel@tonic-gate  * one CPU to another).
693*7c478bd9Sstevel@tonic-gate  */
694*7c478bd9Sstevel@tonic-gate static void
695*7c478bd9Sstevel@tonic-gate trapstat_probe()
696*7c478bd9Sstevel@tonic-gate {
697*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
698*7c478bd9Sstevel@tonic-gate 	hrtime_t before, after;
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED))
701*7c478bd9Sstevel@tonic-gate 		return;
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	if (tstat_probe_area == NULL || (tstat_options & TSTAT_OPT_NOGO))
704*7c478bd9Sstevel@tonic-gate 		return;
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 	/*
707*7c478bd9Sstevel@tonic-gate 	 * We very much expect the %tba to be KERNELBASE; this is a
708*7c478bd9Sstevel@tonic-gate 	 * precautionary measure to assure that trapstat doesn't melt the
709*7c478bd9Sstevel@tonic-gate 	 * machine should the %tba point unexpectedly elsewhere.
710*7c478bd9Sstevel@tonic-gate 	 */
711*7c478bd9Sstevel@tonic-gate 	if (get_tba() != (caddr_t)KERNELBASE)
712*7c478bd9Sstevel@tonic-gate 		return;
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/*
715*7c478bd9Sstevel@tonic-gate 	 * Preserve this CPU's data before destroying it by enabling the
716*7c478bd9Sstevel@tonic-gate 	 * interposing trap table.  We can safely use tstat_buffer because
717*7c478bd9Sstevel@tonic-gate 	 * the caller of the trapstat_probe() cross call is holding tstat_lock.
718*7c478bd9Sstevel@tonic-gate 	 */
719*7c478bd9Sstevel@tonic-gate 	bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size);
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate 	tstat_probe_time = gethrtime();
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	before = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_before);
724*7c478bd9Sstevel@tonic-gate 	(void) set_tba(tcpu->tcpu_ibase);
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	after = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_after);
727*7c478bd9Sstevel@tonic-gate 	(void) set_tba((caddr_t)KERNELBASE);
728*7c478bd9Sstevel@tonic-gate 
729*7c478bd9Sstevel@tonic-gate 	tstat_probe_time = gethrtime() - tstat_probe_time;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size);
732*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data->tdata_peffect = (after - before) / TSTAT_PROBE_NPAGES;
733*7c478bd9Sstevel@tonic-gate }
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate static void
736*7c478bd9Sstevel@tonic-gate trapstat_probe_alloc()
737*7c478bd9Sstevel@tonic-gate {
738*7c478bd9Sstevel@tonic-gate 	pfn_t pfn;
739*7c478bd9Sstevel@tonic-gate 	caddr_t va;
740*7c478bd9Sstevel@tonic-gate 	int i;
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
743*7c478bd9Sstevel@tonic-gate 	ASSERT(tstat_probe_area == NULL);
744*7c478bd9Sstevel@tonic-gate 	ASSERT(tstat_probe_phys == NULL);
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_TLBDATA))
747*7c478bd9Sstevel@tonic-gate 		return;
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	/*
750*7c478bd9Sstevel@tonic-gate 	 * Grab some virtual from the heap arena.
751*7c478bd9Sstevel@tonic-gate 	 */
752*7c478bd9Sstevel@tonic-gate 	tstat_probe_area = vmem_alloc(heap_arena, TSTAT_PROBE_SIZE, VM_SLEEP);
753*7c478bd9Sstevel@tonic-gate 	va = tstat_probe_area;
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate 	/*
756*7c478bd9Sstevel@tonic-gate 	 * Grab a single physical page.
757*7c478bd9Sstevel@tonic-gate 	 */
758*7c478bd9Sstevel@tonic-gate 	tstat_probe_phys = vmem_alloc(tstat_arena, MMU_PAGESIZE, VM_SLEEP);
759*7c478bd9Sstevel@tonic-gate 	pfn = hat_getpfnum(kas.a_hat, tstat_probe_phys);
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	/*
762*7c478bd9Sstevel@tonic-gate 	 * Now set the translation for every page in our virtual range
763*7c478bd9Sstevel@tonic-gate 	 * to be our allocated physical page.
764*7c478bd9Sstevel@tonic-gate 	 */
765*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_PROBE_NPAGES; i++) {
766*7c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, va, MMU_PAGESIZE, pfn, PROT_READ,
767*7c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
768*7c478bd9Sstevel@tonic-gate 		va += MMU_PAGESIZE;
769*7c478bd9Sstevel@tonic-gate 	}
770*7c478bd9Sstevel@tonic-gate }
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate static void
773*7c478bd9Sstevel@tonic-gate trapstat_probe_free()
774*7c478bd9Sstevel@tonic-gate {
775*7c478bd9Sstevel@tonic-gate 	caddr_t va;
776*7c478bd9Sstevel@tonic-gate 	int i;
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	if ((va = tstat_probe_area) == NULL)
781*7c478bd9Sstevel@tonic-gate 		return;
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_PROBE_NPAGES; i++) {
784*7c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, va, MMU_PAGESIZE, HAT_UNLOAD_UNLOCK);
785*7c478bd9Sstevel@tonic-gate 		va += MMU_PAGESIZE;
786*7c478bd9Sstevel@tonic-gate 	}
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tstat_probe_phys, MMU_PAGESIZE);
789*7c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, tstat_probe_area, TSTAT_PROBE_SIZE);
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 	tstat_probe_phys = NULL;
792*7c478bd9Sstevel@tonic-gate 	tstat_probe_area = NULL;
793*7c478bd9Sstevel@tonic-gate }
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate /*
796*7c478bd9Sstevel@tonic-gate  * This routine actually enables a CPU by setting its %tba to be the
797*7c478bd9Sstevel@tonic-gate  * CPU's interposing trap table.  It is called out of cross call context.
798*7c478bd9Sstevel@tonic-gate  */
799*7c478bd9Sstevel@tonic-gate static void
800*7c478bd9Sstevel@tonic-gate trapstat_enable()
801*7c478bd9Sstevel@tonic-gate {
802*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED))
805*7c478bd9Sstevel@tonic-gate 		return;
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
808*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	if (get_tba() != (caddr_t)KERNELBASE)
811*7c478bd9Sstevel@tonic-gate 		return;
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_NOGO))
814*7c478bd9Sstevel@tonic-gate 		(void) set_tba(tcpu->tcpu_ibase);
815*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags |= TSTAT_CPU_ENABLED;
816*7c478bd9Sstevel@tonic-gate #ifdef sun4v
817*7c478bd9Sstevel@tonic-gate 	if (tstat_options & (TSTAT_OPT_TLBDATA | TSTAT_OPT_NOGO)) {
818*7c478bd9Sstevel@tonic-gate 		/*
819*7c478bd9Sstevel@tonic-gate 		 * On sun4v platforms, TLB misses are normally handled by the
820*7c478bd9Sstevel@tonic-gate 		 * hypervisor or the hardware -- provided one or more TSBs
821*7c478bd9Sstevel@tonic-gate 		 * have been setup and communicated via hv_set_ctx0 and
822*7c478bd9Sstevel@tonic-gate 		 * hv_set_nonctx0 API.  However, as part of collecting TLB
823*7c478bd9Sstevel@tonic-gate 		 * statistics, we disabled this miss processing by telling the
824*7c478bd9Sstevel@tonic-gate 		 * hypervisor that there was not a TSB; we now need to
825*7c478bd9Sstevel@tonic-gate 		 * communicate the proper kernel/user TSB information to
826*7c478bd9Sstevel@tonic-gate 		 * resume efficient operation.
827*7c478bd9Sstevel@tonic-gate 		 *
828*7c478bd9Sstevel@tonic-gate 		 * While we restore kernel TSB information immediately, to
829*7c478bd9Sstevel@tonic-gate 		 * avoid any locking dependency, we don't restore user TSB
830*7c478bd9Sstevel@tonic-gate 		 * information right away.  Rather, we simply clear the
831*7c478bd9Sstevel@tonic-gate 		 * TSTAT_TLB_STATS flag so that the user TSB information is
832*7c478bd9Sstevel@tonic-gate 		 * automatically restored on the next context switch.
833*7c478bd9Sstevel@tonic-gate 		 *
834*7c478bd9Sstevel@tonic-gate 		 * Note that the call to restore kernel TSB information is not
835*7c478bd9Sstevel@tonic-gate 		 * expected to fail.  Even in the event of failure, the system
836*7c478bd9Sstevel@tonic-gate 		 * will still continue to function properly, if in a state of
837*7c478bd9Sstevel@tonic-gate 		 * reduced performance due to the guest kernel handling all
838*7c478bd9Sstevel@tonic-gate 		 * TLB misses.
839*7c478bd9Sstevel@tonic-gate 		 */
840*7c478bd9Sstevel@tonic-gate 		cpu_t *cp = CPU;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 		cp->cpu_m.cpu_tstat_flags |= TSTAT_TLB_STATS;
843*7c478bd9Sstevel@tonic-gate 		(void) hv_set_ctx0(NULL, NULL);
844*7c478bd9Sstevel@tonic-gate 		(void) hv_set_ctxnon0(NULL, NULL);
845*7c478bd9Sstevel@tonic-gate 	}
846*7c478bd9Sstevel@tonic-gate #endif
847*7c478bd9Sstevel@tonic-gate }
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate /*
850*7c478bd9Sstevel@tonic-gate  * This routine disables a CPU (vis a vis trapstat) by setting its %tba to be
851*7c478bd9Sstevel@tonic-gate  * the actual, underlying trap table.  It is called out of cross call context.
852*7c478bd9Sstevel@tonic-gate  */
853*7c478bd9Sstevel@tonic-gate static void
854*7c478bd9Sstevel@tonic-gate trapstat_disable()
855*7c478bd9Sstevel@tonic-gate {
856*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate 	if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
859*7c478bd9Sstevel@tonic-gate 		return;
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
862*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
863*7c478bd9Sstevel@tonic-gate 
864*7c478bd9Sstevel@tonic-gate 	if (!(tstat_options & TSTAT_OPT_NOGO))
865*7c478bd9Sstevel@tonic-gate 		(void) set_tba((caddr_t)KERNELBASE);
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED;
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate #ifdef sun4v
870*7c478bd9Sstevel@tonic-gate 	if (tstat_options & (TSTAT_OPT_TLBDATA | TSTAT_OPT_NOGO)) {
871*7c478bd9Sstevel@tonic-gate 		/*
872*7c478bd9Sstevel@tonic-gate 		 * On sun4v platforms, TlB misses are normally handled by
873*7c478bd9Sstevel@tonic-gate 		 * the hypervisor or the hardware provided one or more TSBs
874*7c478bd9Sstevel@tonic-gate 		 * have been setup and communicated via hv_set_ctx0 and
875*7c478bd9Sstevel@tonic-gate 		 * hv_set_nonctx0 API. However, as part of collecting TLB
876*7c478bd9Sstevel@tonic-gate 		 * statistics, we disabled that by faking NO TSB and we
877*7c478bd9Sstevel@tonic-gate 		 * need to communicate proper kernel/user TSB information
878*7c478bd9Sstevel@tonic-gate 		 * so that TLB misses can be handled by the hypervisor or
879*7c478bd9Sstevel@tonic-gate 		 * the hardware more efficiently.
880*7c478bd9Sstevel@tonic-gate 		 *
881*7c478bd9Sstevel@tonic-gate 		 * We restore kernel TSB information right away. However,
882*7c478bd9Sstevel@tonic-gate 		 * to minimize any locking dependency, we don't restore
883*7c478bd9Sstevel@tonic-gate 		 * user TSB information right away. Instead, we simply
884*7c478bd9Sstevel@tonic-gate 		 * clear the TSTAT_TLB_STATS flag so that the user TSB
885*7c478bd9Sstevel@tonic-gate 		 * information is automatically restored on next context
886*7c478bd9Sstevel@tonic-gate 		 * switch.
887*7c478bd9Sstevel@tonic-gate 		 *
888*7c478bd9Sstevel@tonic-gate 		 * Note that the call to restore kernel TSB information
889*7c478bd9Sstevel@tonic-gate 		 * will normally not fail, unless wrong information is
890*7c478bd9Sstevel@tonic-gate 		 * passed here. In that scenario, system will still
891*7c478bd9Sstevel@tonic-gate 		 * continue to function properly with the exception of
892*7c478bd9Sstevel@tonic-gate 		 * kernel handling all the TLB misses.
893*7c478bd9Sstevel@tonic-gate 		 */
894*7c478bd9Sstevel@tonic-gate 		struct hv_tsb_block *hvbp = &ksfmmup->sfmmu_hvblock;
895*7c478bd9Sstevel@tonic-gate 		cpu_t *cp = CPU;
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 		cp->cpu_m.cpu_tstat_flags &= ~TSTAT_TLB_STATS;
898*7c478bd9Sstevel@tonic-gate 		(void) hv_set_ctx0(hvbp->hv_tsb_info_cnt, hvbp->hv_tsb_info_pa);
899*7c478bd9Sstevel@tonic-gate 	}
900*7c478bd9Sstevel@tonic-gate #endif
901*7c478bd9Sstevel@tonic-gate }
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate /*
904*7c478bd9Sstevel@tonic-gate  * We use %tick as the time base when recording the time spent executing
905*7c478bd9Sstevel@tonic-gate  * the trap handler.  %tick, however, is not necessarily kept in sync
906*7c478bd9Sstevel@tonic-gate  * across CPUs (indeed, different CPUs may have different %tick frequencies).
907*7c478bd9Sstevel@tonic-gate  * We therefore cross call onto a CPU to get a snapshot of its data to
908*7c478bd9Sstevel@tonic-gate  * copy out; this is the routine executed out of that cross call.
909*7c478bd9Sstevel@tonic-gate  */
910*7c478bd9Sstevel@tonic-gate static void
911*7c478bd9Sstevel@tonic-gate trapstat_snapshot()
912*7c478bd9Sstevel@tonic-gate {
913*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id];
914*7c478bd9Sstevel@tonic-gate 	tstat_data_t *data = tcpu->tcpu_data;
915*7c478bd9Sstevel@tonic-gate 
916*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
917*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
918*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ENABLED);
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 	data->tdata_snapts = gethrtime();
921*7c478bd9Sstevel@tonic-gate 	data->tdata_snaptick = rdtick();
922*7c478bd9Sstevel@tonic-gate 	bcopy(data, tstat_buffer, tstat_data_t_size);
923*7c478bd9Sstevel@tonic-gate }
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate /*
926*7c478bd9Sstevel@tonic-gate  * The TSTAT_RETENT_* constants define offsets in the TLB return entry.
927*7c478bd9Sstevel@tonic-gate  * They are used only in trapstat_tlbretent() (below) and #undef'd
928*7c478bd9Sstevel@tonic-gate  * immediately afterwards.  Any change to "retent" in trapstat_tlbretent()
929*7c478bd9Sstevel@tonic-gate  * will likely require changes to these constants.
930*7c478bd9Sstevel@tonic-gate  */
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate #ifndef	sun4v
933*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATHI	1
934*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATLO	2
935*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_SHIFT	8
936*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_COUNT_LD	10
937*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_COUNT_ST	12
938*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TMPTSHI	13
939*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TMPTSLO	14
940*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TIME_LD	16
941*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TIME_ST	18
942*7c478bd9Sstevel@tonic-gate #else /* sun4v */
943*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATHI	1
944*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_STATLO	2
945*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_SHIFT	5
946*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_COUNT_LD	7
947*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_COUNT_ST	9
948*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TMPTSHI	10
949*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TMPTSLO	11
950*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TIME_LD	13
951*7c478bd9Sstevel@tonic-gate #define	TSTAT_RETENT_TIME_ST	15
952*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate static void
955*7c478bd9Sstevel@tonic-gate trapstat_tlbretent(tstat_percpu_t *tcpu, tstat_tlbretent_t *ret,
956*7c478bd9Sstevel@tonic-gate     tstat_missdata_t *data)
957*7c478bd9Sstevel@tonic-gate {
958*7c478bd9Sstevel@tonic-gate 	uint32_t *ent = ret->ttlbrent_instr, shift;
959*7c478bd9Sstevel@tonic-gate 	uintptr_t base, tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick);
960*7c478bd9Sstevel@tonic-gate 
961*7c478bd9Sstevel@tonic-gate 	/*
962*7c478bd9Sstevel@tonic-gate 	 * This is the entry executed upon return from the TLB/TSB miss
963*7c478bd9Sstevel@tonic-gate 	 * handler (i.e. the code interpositioned between the "retry" and
964*7c478bd9Sstevel@tonic-gate 	 * the actual return to the TLB-missing instruction).  Detail on its
965*7c478bd9Sstevel@tonic-gate 	 * theory of operation can be found in the "TLB Statistics" section
966*7c478bd9Sstevel@tonic-gate 	 * of the block comment.  Note that we expect the TTE just loaded
967*7c478bd9Sstevel@tonic-gate 	 * into the TLB to be in %g5; all other globals are available as
968*7c478bd9Sstevel@tonic-gate 	 * scratch.  Finally, note that the page size information in sun4v is
969*7c478bd9Sstevel@tonic-gate 	 * located in the lower bits of the TTE -- requiring us to have a
970*7c478bd9Sstevel@tonic-gate 	 * different return entry on sun4v.
971*7c478bd9Sstevel@tonic-gate 	 */
972*7c478bd9Sstevel@tonic-gate 	static const uint32_t retent[TSTAT_TLBRET_NINSTR] = {
973*7c478bd9Sstevel@tonic-gate #ifndef sun4v
974*7c478bd9Sstevel@tonic-gate 	    0x87410000,		/* rd    %tick, %g3			*/
975*7c478bd9Sstevel@tonic-gate 	    0x03000000, 	/* sethi %hi(stat), %g1			*/
976*7c478bd9Sstevel@tonic-gate 	    0x82106000,		/* or    %g1, %lo(stat), %g1		*/
977*7c478bd9Sstevel@tonic-gate 	    0x89297001,		/* sllx  %g5, 1, %g4			*/
978*7c478bd9Sstevel@tonic-gate 	    0x8931303e,		/* srlx  %g4, 62, %g4			*/
979*7c478bd9Sstevel@tonic-gate 	    0x8531702e,		/* srlx  %g5, 46, %g2			*/
980*7c478bd9Sstevel@tonic-gate 	    0x8408a004,		/* and   %g2, 4, %g2			*/
981*7c478bd9Sstevel@tonic-gate 	    0x88110002,		/* or    %g4, %g2, %g4			*/
982*7c478bd9Sstevel@tonic-gate 	    0x89292000,		/* sll   %g4, shift, %g4		*/
983*7c478bd9Sstevel@tonic-gate 	    0x82004004,		/* add   %g1, %g4, %g1			*/
984*7c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_count], %g2	*/
985*7c478bd9Sstevel@tonic-gate 	    0x8400a001,		/* add   %g2, 1, %g2			*/
986*7c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_count]	*/
987*7c478bd9Sstevel@tonic-gate 	    0x0d000000, 	/* sethi %hi(tdata_tmptick), %g6	*/
988*7c478bd9Sstevel@tonic-gate 	    0xc459a000, 	/* ldx   [%g6 + %lo(tdata_tmptick)], %g2 */
989*7c478bd9Sstevel@tonic-gate 	    0x8620c002,		/* sub   %g3, %g2, %g3			*/
990*7c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_time], %g2	*/
991*7c478bd9Sstevel@tonic-gate 	    0x84008003,		/* add   %g2, %g3, %g2			*/
992*7c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_time]	*/
993*7c478bd9Sstevel@tonic-gate 	    0x83f00000		/* retry				*/
994*7c478bd9Sstevel@tonic-gate #else /* sun4v */
995*7c478bd9Sstevel@tonic-gate 	    0x87410000,		/* rd    %tick, %g3			*/
996*7c478bd9Sstevel@tonic-gate 	    0x03000000, 	/* sethi %hi(stat), %g1			*/
997*7c478bd9Sstevel@tonic-gate 	    0x82106000,		/* or    %g1, %lo(stat), %g1		*/
998*7c478bd9Sstevel@tonic-gate 	    0x8929703d,		/* sllx  %g5, 61, %g4			*/
999*7c478bd9Sstevel@tonic-gate 	    0x8931303d,		/* srlx  %g4, 61, %g4			*/
1000*7c478bd9Sstevel@tonic-gate 	    0x89292000,		/* sll   %g4, shift, %g4		*/
1001*7c478bd9Sstevel@tonic-gate 	    0x82004004,		/* add   %g1, %g4, %g1			*/
1002*7c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_count], %g2	*/
1003*7c478bd9Sstevel@tonic-gate 	    0x8400a001,		/* add   %g2, 1, %g2			*/
1004*7c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_count]	*/
1005*7c478bd9Sstevel@tonic-gate 	    0x0d000000, 	/* sethi %hi(tdata_tmptick), %g6	*/
1006*7c478bd9Sstevel@tonic-gate 	    0xc459a000, 	/* ldx   [%g6 + %lo(tdata_tmptick)], %g2 */
1007*7c478bd9Sstevel@tonic-gate 	    0x8620c002,		/* sub   %g3, %g2, %g3			*/
1008*7c478bd9Sstevel@tonic-gate 	    0xc4586000,		/* ldx   [%g1 + tmiss_time], %g2	*/
1009*7c478bd9Sstevel@tonic-gate 	    0x84008003,		/* add   %g2, %g3, %g2			*/
1010*7c478bd9Sstevel@tonic-gate 	    0xc4706000,		/* stx   %g2, [%g1 + tmiss_time]	*/
1011*7c478bd9Sstevel@tonic-gate 	    0x83f00000		/* retry				*/
1012*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
1013*7c478bd9Sstevel@tonic-gate 	};
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1016*7c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
1017*7c478bd9Sstevel@tonic-gate 	ASSERT(offsetof(tstat_missdata_t, tmiss_count) <= LO10(-1));
1018*7c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
1019*7c478bd9Sstevel@tonic-gate 	ASSERT(offsetof(tstat_missdata_t, tmiss_time) <= LO10(-1));
1020*7c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
1021*7c478bd9Sstevel@tonic-gate 	ASSERT(!((sizeof (tstat_pgszdata_t) - 1) & sizeof (tstat_pgszdata_t)));
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	for (shift = 1; (1 << shift) != sizeof (tstat_pgszdata_t); shift++)
1024*7c478bd9Sstevel@tonic-gate 		continue;
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 	base = (uintptr_t)tcpu->tcpu_dbase +
1027*7c478bd9Sstevel@tonic-gate 	    ((uintptr_t)data - (uintptr_t)tcpu->tcpu_data);
1028*7c478bd9Sstevel@tonic-gate 
1029*7c478bd9Sstevel@tonic-gate 	bcopy(retent, ent, sizeof (retent));
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_STATHI] |= HI22(base);
1032*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_STATLO] |= LO10(base);
1033*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_SHIFT] |= shift;
1034*7c478bd9Sstevel@tonic-gate 	/* LINTED E_EXPR_NULL_EFFECT */
1035*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_COUNT_LD] |= offsetof(tstat_missdata_t, tmiss_count);
1036*7c478bd9Sstevel@tonic-gate 	/* LINTED E_EXPR_NULL_EFFECT */
1037*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_COUNT_ST] |= offsetof(tstat_missdata_t, tmiss_count);
1038*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TMPTSHI] |= HI22(tmptick);
1039*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TMPTSLO] |= LO10(tmptick);
1040*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TIME_LD] |= offsetof(tstat_missdata_t, tmiss_time);
1041*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_RETENT_TIME_ST] |= offsetof(tstat_missdata_t, tmiss_time);
1042*7c478bd9Sstevel@tonic-gate }
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATHI
1045*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATLO
1046*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_SHIFT
1047*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_LD
1048*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_ST
1049*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSHI
1050*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSLO
1051*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_LD
1052*7c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_ST
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate /*
1055*7c478bd9Sstevel@tonic-gate  * The TSTAT_TLBENT_* constants define offsets in the TLB entry.  They are
1056*7c478bd9Sstevel@tonic-gate  * used only in trapstat_tlbent() (below) and #undef'd immediately afterwards.
1057*7c478bd9Sstevel@tonic-gate  * Any change to "tlbent" in trapstat_tlbent() will likely require changes
1058*7c478bd9Sstevel@tonic-gate  * to these constants.
1059*7c478bd9Sstevel@tonic-gate  */
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1062*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATHI	0
1063*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_LD	1
1064*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_ST	3
1065*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_MMUASI	15
1066*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCHI	18
1067*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_USER	19
1068*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_KERN	21
1069*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSHI	25
1070*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSLO	27
1071*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_BA		28
1072*7c478bd9Sstevel@tonic-gate #else /* sun4v */
1073*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATHI	0
1074*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_LD	1
1075*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_STATLO_ST	3
1076*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TAGTARGET	19
1077*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCHI	21
1078*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_USER	22
1079*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TPCLO_KERN	24
1080*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSHI	28
1081*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_TSLO	30
1082*7c478bd9Sstevel@tonic-gate #define	TSTAT_TLBENT_BA		31
1083*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate static void
1086*7c478bd9Sstevel@tonic-gate trapstat_tlbent(tstat_percpu_t *tcpu, int entno)
1087*7c478bd9Sstevel@tonic-gate {
1088*7c478bd9Sstevel@tonic-gate 	uint32_t *ent;
1089*7c478bd9Sstevel@tonic-gate 	uintptr_t orig, va, baoffs;
1090*7c478bd9Sstevel@tonic-gate 	int itlb = entno == TSTAT_ENT_ITLBMISS;
1091*7c478bd9Sstevel@tonic-gate 	int entoffs = entno << TSTAT_ENT_SHIFT;
1092*7c478bd9Sstevel@tonic-gate 	uintptr_t tmptick, stat, tpc, utpc;
1093*7c478bd9Sstevel@tonic-gate 	tstat_pgszdata_t *data = &tcpu->tcpu_data->tdata_pgsz[0];
1094*7c478bd9Sstevel@tonic-gate 	tstat_tlbdata_t *udata, *kdata;
1095*7c478bd9Sstevel@tonic-gate 	tstat_tlbret_t *ret;
1096*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1097*7c478bd9Sstevel@tonic-gate 	uint32_t asi = itlb ? ASI(ASI_IMMU) : ASI(ASI_DMMU);
1098*7c478bd9Sstevel@tonic-gate #else
1099*7c478bd9Sstevel@tonic-gate 	uint32_t tagtarget_off = itlb ? MMFSA_I_CTX : MMFSA_D_CTX;
1100*7c478bd9Sstevel@tonic-gate #endif
1101*7c478bd9Sstevel@tonic-gate 
1102*7c478bd9Sstevel@tonic-gate 	/*
1103*7c478bd9Sstevel@tonic-gate 	 * When trapstat is run with TLB statistics, this is the entry for
1104*7c478bd9Sstevel@tonic-gate 	 * both I- and D-TLB misses; this code performs trap level pushing,
1105*7c478bd9Sstevel@tonic-gate 	 * as described in the "TLB Statistics" section of the block comment.
1106*7c478bd9Sstevel@tonic-gate 	 * This code is executing at TL 1; %tstate[0] contains the saved
1107*7c478bd9Sstevel@tonic-gate 	 * state at the time of the TLB miss.  Pushing trap level 1 (and thus
1108*7c478bd9Sstevel@tonic-gate 	 * raising TL to 2) requires us to fill in %tstate[1] with our %pstate,
1109*7c478bd9Sstevel@tonic-gate 	 * %cwp and %asi.  We leave %tt unchanged, and we set %tpc and %tnpc to
1110*7c478bd9Sstevel@tonic-gate 	 * the appropriate TLB return entry (based on the context of the miss).
1111*7c478bd9Sstevel@tonic-gate 	 * Finally, we sample %tick, and stash it in the tdata_tmptick member
1112*7c478bd9Sstevel@tonic-gate 	 * the per-CPU tstat_data structure.  tdata_tmptick will be used in
1113*7c478bd9Sstevel@tonic-gate 	 * the TLB return entry to determine the amount of time spent in the
1114*7c478bd9Sstevel@tonic-gate 	 * TLB miss handler.
1115*7c478bd9Sstevel@tonic-gate 	 *
1116*7c478bd9Sstevel@tonic-gate 	 * Note that on sun4v platforms, we must also force the %gl value to 1
1117*7c478bd9Sstevel@tonic-gate 	 * in %tstate and we must obtain the context information from the MMU
1118*7c478bd9Sstevel@tonic-gate 	 * fault status area. (The base address of this MMU fault status area
1119*7c478bd9Sstevel@tonic-gate 	 * is kept in the scratchpad register 0.)
1120*7c478bd9Sstevel@tonic-gate 	 */
1121*7c478bd9Sstevel@tonic-gate 	static const uint32_t tlbent[] = {
1122*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1123*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
1124*7c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
1125*7c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
1126*7c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
1127*7c478bd9Sstevel@tonic-gate 	    0x85524000,			/* rdpr  %cwp, %g2		*/
1128*7c478bd9Sstevel@tonic-gate 	    0x87518000,			/* rdpr  %pstate, %g3		*/
1129*7c478bd9Sstevel@tonic-gate 	    0x8728f008,			/* sllx  %g3, 8, %g3		*/
1130*7c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
1131*7c478bd9Sstevel@tonic-gate 	    0x8740c000,			/* rd    %asi, %g3		*/
1132*7c478bd9Sstevel@tonic-gate 	    0x8728f018,			/* sllx  %g3, 24, %g3		*/
1133*7c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
1134*7c478bd9Sstevel@tonic-gate 	    0x8350c000,			/* rdpr  %tt, %g1		*/
1135*7c478bd9Sstevel@tonic-gate 	    0x8f902002,			/* wrpr  %g0, 2, %tl		*/
1136*7c478bd9Sstevel@tonic-gate 	    0x85908000,			/* wrpr  %g2, %g0, %tstate	*/
1137*7c478bd9Sstevel@tonic-gate 	    0x87904000,			/* wrpr  %g1, %g0, %tt		*/
1138*7c478bd9Sstevel@tonic-gate 	    0xc2d80000,			/* ldxa  [%g0]ASI_MMU, %g1	*/
1139*7c478bd9Sstevel@tonic-gate 	    0x83307030,			/* srlx  %g1, CTXSHIFT, %g1	*/
1140*7c478bd9Sstevel@tonic-gate 	    0x02c04004,			/* brz,pn %g1, .+0x10		*/
1141*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(new_tpc), %g1	*/
1142*7c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
1143*7c478bd9Sstevel@tonic-gate 	    0x30800002,			/* ba,a  .+0x8			*/
1144*7c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
1145*7c478bd9Sstevel@tonic-gate 	    0x81904000,			/* wrpr  %g1, %g0, %tpc		*/
1146*7c478bd9Sstevel@tonic-gate 	    0x82006004,			/* add   %g1, 4, %g1		*/
1147*7c478bd9Sstevel@tonic-gate 	    0x83904000,			/* wrpr  %g1, %g0, %tnpc	*/
1148*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(tmptick), %g1	*/
1149*7c478bd9Sstevel@tonic-gate 	    0x85410000,			/* rd    %tick, %g2		*/
1150*7c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(tmptick)] */
1151*7c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a  addr			*/
1152*7c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP
1153*7c478bd9Sstevel@tonic-gate #else /* sun4v */
1154*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
1155*7c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
1156*7c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
1157*7c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
1158*7c478bd9Sstevel@tonic-gate 	    0x85524000,			/* rdpr  %cwp, %g2		*/
1159*7c478bd9Sstevel@tonic-gate 	    0x87518000,			/* rdpr  %pstate, %g3		*/
1160*7c478bd9Sstevel@tonic-gate 	    0x8728f008,			/* sllx  %g3, 8, %g3		*/
1161*7c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
1162*7c478bd9Sstevel@tonic-gate 	    0x8740c000,			/* rd    %asi, %g3		*/
1163*7c478bd9Sstevel@tonic-gate 	    0x03000040,			/* sethi %hi(0x10000), %g1	*/
1164*7c478bd9Sstevel@tonic-gate 	    0x86104003,			/* or    %g1, %g3, %g3		*/
1165*7c478bd9Sstevel@tonic-gate 	    0x8728f018,			/* sllx  %g3, 24, %g3		*/
1166*7c478bd9Sstevel@tonic-gate 	    0x84108003,			/* or    %g2, %g3, %g2		*/
1167*7c478bd9Sstevel@tonic-gate 	    0x8350c000,			/* rdpr  %tt, %g1		*/
1168*7c478bd9Sstevel@tonic-gate 	    0x8f902002,			/* wrpr  %g0, 2, %tl		*/
1169*7c478bd9Sstevel@tonic-gate 	    0x85908000,			/* wrpr  %g2, %g0, %tstate	*/
1170*7c478bd9Sstevel@tonic-gate 	    0x87904000,			/* wrpr  %g1, %g0, %tt		*/
1171*7c478bd9Sstevel@tonic-gate 	    0xa1902001,			/* wrpr  %g0, 1, %gl		*/
1172*7c478bd9Sstevel@tonic-gate 	    0xc2d80400,			/* ldxa  [%g0]ASI_SCRATCHPAD, %g1 */
1173*7c478bd9Sstevel@tonic-gate 	    0xc2586000,			/* ldx  [%g1 + MMFSA_?_CTX], %g1 */
1174*7c478bd9Sstevel@tonic-gate 	    0x02c04004,			/* brz,pn %g1, .+0x10		*/
1175*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(new_tpc), %g1	*/
1176*7c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
1177*7c478bd9Sstevel@tonic-gate 	    0x30800002,			/* ba,a  .+0x8			*/
1178*7c478bd9Sstevel@tonic-gate 	    0x82106000,			/* or    %g1, %lo(new_tpc), %g1	*/
1179*7c478bd9Sstevel@tonic-gate 	    0x81904000,			/* wrpr  %g1, %g0, %tpc		*/
1180*7c478bd9Sstevel@tonic-gate 	    0x82006004,			/* add   %g1, 4, %g1		*/
1181*7c478bd9Sstevel@tonic-gate 	    0x83904000,			/* wrpr  %g1, %g0, %tnpc	*/
1182*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(tmptick), %g1	*/
1183*7c478bd9Sstevel@tonic-gate 	    0x85410000,			/* rd    %tick, %g2		*/
1184*7c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(tmptick)] */
1185*7c478bd9Sstevel@tonic-gate 	    0x30800000			/* ba,a  addr			*/
1186*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
1187*7c478bd9Sstevel@tonic-gate 	};
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1190*7c478bd9Sstevel@tonic-gate 	ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS);
1191*7c478bd9Sstevel@tonic-gate 
1192*7c478bd9Sstevel@tonic-gate 	stat = TSTAT_DATA_OFFS(tcpu, tdata_traps) + entoffs;
1193*7c478bd9Sstevel@tonic-gate 	tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick);
1194*7c478bd9Sstevel@tonic-gate 
1195*7c478bd9Sstevel@tonic-gate 	if (itlb) {
1196*7c478bd9Sstevel@tonic-gate 		ret = &tcpu->tcpu_instr->tinst_itlbret;
1197*7c478bd9Sstevel@tonic-gate 		udata = &data->tpgsz_user.tmode_itlb;
1198*7c478bd9Sstevel@tonic-gate 		kdata = &data->tpgsz_kernel.tmode_itlb;
1199*7c478bd9Sstevel@tonic-gate 		tpc = TSTAT_INSTR_OFFS(tcpu, tinst_itlbret.ttlbr_ktlb);
1200*7c478bd9Sstevel@tonic-gate 	} else {
1201*7c478bd9Sstevel@tonic-gate 		ret = &tcpu->tcpu_instr->tinst_dtlbret;
1202*7c478bd9Sstevel@tonic-gate 		udata = &data->tpgsz_user.tmode_dtlb;
1203*7c478bd9Sstevel@tonic-gate 		kdata = &data->tpgsz_kernel.tmode_dtlb;
1204*7c478bd9Sstevel@tonic-gate 		tpc = TSTAT_INSTR_OFFS(tcpu, tinst_dtlbret.ttlbr_ktlb);
1205*7c478bd9Sstevel@tonic-gate 	}
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate 	utpc = tpc + offsetof(tstat_tlbret_t, ttlbr_utlb) -
1208*7c478bd9Sstevel@tonic-gate 	    offsetof(tstat_tlbret_t, ttlbr_ktlb);
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 	ASSERT(HI22(tpc) == HI22(utpc));
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate 	ent = (uint32_t *)((uintptr_t)tcpu->tcpu_instr + entoffs);
1213*7c478bd9Sstevel@tonic-gate 	orig = KERNELBASE + entoffs;
1214*7c478bd9Sstevel@tonic-gate 	va = (uintptr_t)tcpu->tcpu_ibase + entoffs;
1215*7c478bd9Sstevel@tonic-gate 	baoffs = TSTAT_TLBENT_BA * sizeof (uint32_t);
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	bcopy(tlbent, ent, sizeof (tlbent));
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATHI] |= HI22(stat);
1220*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATLO_LD] |= LO10(stat);
1221*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_STATLO_ST] |= LO10(stat);
1222*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1223*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_MMUASI] |= asi;
1224*7c478bd9Sstevel@tonic-gate #else
1225*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TAGTARGET] |= tagtarget_off;
1226*7c478bd9Sstevel@tonic-gate #endif
1227*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCHI] |= HI22(tpc);
1228*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCLO_USER] |= LO10(utpc);
1229*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TPCLO_KERN] |= LO10(tpc);
1230*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TSHI] |= HI22(tmptick);
1231*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_TSLO] |= LO10(tmptick);
1232*7c478bd9Sstevel@tonic-gate 	ent[TSTAT_TLBENT_BA] |= DISP22(va + baoffs, orig);
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate 	/*
1235*7c478bd9Sstevel@tonic-gate 	 * And now set up the TLB return entries.
1236*7c478bd9Sstevel@tonic-gate 	 */
1237*7c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_ktlb, &kdata->ttlb_tlb);
1238*7c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_ktsb, &kdata->ttlb_tsb);
1239*7c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_utlb, &udata->ttlb_tlb);
1240*7c478bd9Sstevel@tonic-gate 	trapstat_tlbretent(tcpu, &ret->ttlbr_utsb, &udata->ttlb_tsb);
1241*7c478bd9Sstevel@tonic-gate }
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATHI
1244*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_LD
1245*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_ST
1246*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1247*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_MMUASI
1248*7c478bd9Sstevel@tonic-gate #else
1249*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TAGTARGET
1250*7c478bd9Sstevel@tonic-gate #endif
1251*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCHI
1252*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_USER
1253*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_KERN
1254*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSHI
1255*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSLO
1256*7c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_BA
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate /*
1259*7c478bd9Sstevel@tonic-gate  * The TSTAT_ENABLED_* constants define offsets in the enabled entry; the
1260*7c478bd9Sstevel@tonic-gate  * TSTAT_DISABLED_BA constant defines an offset in the disabled entry.  Both
1261*7c478bd9Sstevel@tonic-gate  * sets of constants are used only in trapstat_make_traptab() (below) and
1262*7c478bd9Sstevel@tonic-gate  * #undef'd immediately afterwards.  Any change to "enabled" or "disabled"
1263*7c478bd9Sstevel@tonic-gate  * in trapstat_make_traptab() will likely require changes to these constants.
1264*7c478bd9Sstevel@tonic-gate  */
1265*7c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATHI	0
1266*7c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATLO_LD	1
1267*7c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_STATLO_ST 3
1268*7c478bd9Sstevel@tonic-gate #define	TSTAT_ENABLED_BA	4
1269*7c478bd9Sstevel@tonic-gate #define	TSTAT_DISABLED_BA	0
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate static void
1272*7c478bd9Sstevel@tonic-gate trapstat_make_traptab(tstat_percpu_t *tcpu)
1273*7c478bd9Sstevel@tonic-gate {
1274*7c478bd9Sstevel@tonic-gate 	uint32_t *ent;
1275*7c478bd9Sstevel@tonic-gate 	uint64_t *stat;
1276*7c478bd9Sstevel@tonic-gate 	uintptr_t orig, va, en_baoffs, dis_baoffs;
1277*7c478bd9Sstevel@tonic-gate 	int nent;
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate 	/*
1280*7c478bd9Sstevel@tonic-gate 	 * This is the entry in the interposing trap table for enabled trap
1281*7c478bd9Sstevel@tonic-gate 	 * table entries.  It loads a counter, increments it and stores it
1282*7c478bd9Sstevel@tonic-gate 	 * back before branching to the actual trap table entry.
1283*7c478bd9Sstevel@tonic-gate 	 */
1284*7c478bd9Sstevel@tonic-gate 	static const uint32_t enabled[TSTAT_ENT_NINSTR] = {
1285*7c478bd9Sstevel@tonic-gate 	    0x03000000, 		/* sethi %hi(stat), %g1		*/
1286*7c478bd9Sstevel@tonic-gate 	    0xc4586000,			/* ldx   [%g1 + %lo(stat)], %g2	*/
1287*7c478bd9Sstevel@tonic-gate 	    0x8400a001,			/* add   %g2, 1, %g2		*/
1288*7c478bd9Sstevel@tonic-gate 	    0xc4706000,			/* stx   %g2, [%g1 + %lo(stat)]	*/
1289*7c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a addr			*/
1290*7c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP
1291*7c478bd9Sstevel@tonic-gate 	};
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 	/*
1294*7c478bd9Sstevel@tonic-gate 	 * This is the entry in the interposing trap table for disabled trap
1295*7c478bd9Sstevel@tonic-gate 	 * table entries.  It simply branches to the actual, underlying trap
1296*7c478bd9Sstevel@tonic-gate 	 * table entry.  As explained in the "Implementation Details" section
1297*7c478bd9Sstevel@tonic-gate 	 * of the block comment, all TL>0 traps _must_ use the disabled entry;
1298*7c478bd9Sstevel@tonic-gate 	 * additional entries may be explicitly disabled through the use
1299*7c478bd9Sstevel@tonic-gate 	 * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY.
1300*7c478bd9Sstevel@tonic-gate 	 */
1301*7c478bd9Sstevel@tonic-gate 	static const uint32_t disabled[TSTAT_ENT_NINSTR] = {
1302*7c478bd9Sstevel@tonic-gate 	    0x30800000,			/* ba,a addr			*/
1303*7c478bd9Sstevel@tonic-gate 	    NOP, NOP, NOP, NOP, NOP, NOP, NOP,
1304*7c478bd9Sstevel@tonic-gate 	};
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1307*7c478bd9Sstevel@tonic-gate 
1308*7c478bd9Sstevel@tonic-gate 	ent = tcpu->tcpu_instr->tinst_traptab;
1309*7c478bd9Sstevel@tonic-gate 	stat = (uint64_t *)TSTAT_DATA_OFFS(tcpu, tdata_traps);
1310*7c478bd9Sstevel@tonic-gate 	orig = KERNELBASE;
1311*7c478bd9Sstevel@tonic-gate 	va = (uintptr_t)tcpu->tcpu_ibase;
1312*7c478bd9Sstevel@tonic-gate 	en_baoffs = TSTAT_ENABLED_BA * sizeof (uint32_t);
1313*7c478bd9Sstevel@tonic-gate 	dis_baoffs = TSTAT_DISABLED_BA * sizeof (uint32_t);
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 	for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) {
1316*7c478bd9Sstevel@tonic-gate 		if (tstat_enabled[nent]) {
1317*7c478bd9Sstevel@tonic-gate 			bcopy(enabled, ent, sizeof (enabled));
1318*7c478bd9Sstevel@tonic-gate 			ent[TSTAT_ENABLED_STATHI] |= HI22(stat);
1319*7c478bd9Sstevel@tonic-gate 			ent[TSTAT_ENABLED_STATLO_LD] |= LO10(stat);
1320*7c478bd9Sstevel@tonic-gate 			ent[TSTAT_ENABLED_STATLO_ST] |= LO10(stat);
1321*7c478bd9Sstevel@tonic-gate 			ent[TSTAT_ENABLED_BA] |= DISP22(va + en_baoffs, orig);
1322*7c478bd9Sstevel@tonic-gate 		} else {
1323*7c478bd9Sstevel@tonic-gate 			bcopy(disabled, ent, sizeof (disabled));
1324*7c478bd9Sstevel@tonic-gate 			ent[TSTAT_DISABLED_BA] |= DISP22(va + dis_baoffs, orig);
1325*7c478bd9Sstevel@tonic-gate 		}
1326*7c478bd9Sstevel@tonic-gate 
1327*7c478bd9Sstevel@tonic-gate 		stat++;
1328*7c478bd9Sstevel@tonic-gate 		orig += sizeof (enabled);
1329*7c478bd9Sstevel@tonic-gate 		ent += sizeof (enabled) / sizeof (*ent);
1330*7c478bd9Sstevel@tonic-gate 		va += sizeof (enabled);
1331*7c478bd9Sstevel@tonic-gate 	}
1332*7c478bd9Sstevel@tonic-gate }
1333*7c478bd9Sstevel@tonic-gate 
1334*7c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATHI
1335*7c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_LD
1336*7c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_ST
1337*7c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_BA
1338*7c478bd9Sstevel@tonic-gate #undef TSTAT_DISABLED_BA
1339*7c478bd9Sstevel@tonic-gate 
1340*7c478bd9Sstevel@tonic-gate static void
1341*7c478bd9Sstevel@tonic-gate trapstat_setup(processorid_t cpu)
1342*7c478bd9Sstevel@tonic-gate {
1343*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
1344*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1345*7c478bd9Sstevel@tonic-gate 	int i;
1346*7c478bd9Sstevel@tonic-gate 	caddr_t va;
1347*7c478bd9Sstevel@tonic-gate 	pfn_t *pfn;
1348*7c478bd9Sstevel@tonic-gate #endif
1349*7c478bd9Sstevel@tonic-gate 
1350*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_pfn == NULL);
1351*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_instr == NULL);
1352*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_data == NULL);
1353*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
1354*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED));
1355*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1356*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1357*7c478bd9Sstevel@tonic-gate 
1358*7c478bd9Sstevel@tonic-gate 	/*
1359*7c478bd9Sstevel@tonic-gate 	 * The lower fifteen bits of the %tba are always read as zero; we must
1360*7c478bd9Sstevel@tonic-gate 	 * align our instruction base address appropriately.
1361*7c478bd9Sstevel@tonic-gate 	 */
1362*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1363*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_ibase = (caddr_t)((KERNELBASE - tstat_total_size)
1364*7c478bd9Sstevel@tonic-gate 		& TSTAT_TBA_MASK);
1365*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE;
1366*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_vabase = tcpu->tcpu_ibase;
1367*7c478bd9Sstevel@tonic-gate 
1368*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_pfn = vmem_alloc(tstat_arena, tstat_total_pages, VM_SLEEP);
1369*7c478bd9Sstevel@tonic-gate 	bzero(tcpu->tcpu_pfn, tstat_total_pages);
1370*7c478bd9Sstevel@tonic-gate 	pfn = tcpu->tcpu_pfn;
1371*7c478bd9Sstevel@tonic-gate 
1372*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_instr = vmem_alloc(tstat_arena, TSTAT_INSTR_SIZE, VM_SLEEP);
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 	va = (caddr_t)tcpu->tcpu_instr;
1375*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_INSTR_PAGES; i++, va += MMU_PAGESIZE)
1376*7c478bd9Sstevel@tonic-gate 		*pfn++ = hat_getpfnum(kas.a_hat, va);
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate 	/*
1379*7c478bd9Sstevel@tonic-gate 	 * We must be sure that the pages that we will use to examine the data
1380*7c478bd9Sstevel@tonic-gate 	 * have the same virtual color as the pages to which the data is being
1381*7c478bd9Sstevel@tonic-gate 	 * recorded, hence the alignment and phase constraints on the
1382*7c478bd9Sstevel@tonic-gate 	 * allocation.
1383*7c478bd9Sstevel@tonic-gate 	 */
1384*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data = vmem_xalloc(tstat_arena, tstat_data_size,
1385*7c478bd9Sstevel@tonic-gate 	    shm_alignment, (uintptr_t)tcpu->tcpu_dbase & (shm_alignment - 1),
1386*7c478bd9Sstevel@tonic-gate 	    0, 0, NULL, VM_SLEEP);
1387*7c478bd9Sstevel@tonic-gate 	bzero(tcpu->tcpu_data, tstat_data_size);
1388*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data->tdata_cpuid = cpu;
1389*7c478bd9Sstevel@tonic-gate 
1390*7c478bd9Sstevel@tonic-gate 	va = (caddr_t)tcpu->tcpu_data;
1391*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_data_pages; i++, va += MMU_PAGESIZE)
1392*7c478bd9Sstevel@tonic-gate 		*pfn++ = hat_getpfnum(kas.a_hat, va);
1393*7c478bd9Sstevel@tonic-gate #else /* sun4v */
1394*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tstat_total_size > (1 + ~TSTAT_TBA_MASK)));
1395*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_vabase = (caddr_t)(KERNELBASE - MMU_PAGESIZE4M);
1396*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_ibase = tcpu->tcpu_vabase + (cpu * (1 + ~TSTAT_TBA_MASK));
1397*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE;
1398*7c478bd9Sstevel@tonic-gate 
1399*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_pfn = &tstat_pfn;
1400*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_instr = (tstat_instr_t *)(tstat_va + (cpu *
1401*7c478bd9Sstevel@tonic-gate 		(1 + ~TSTAT_TBA_MASK)));
1402*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data = (tstat_data_t *)(tstat_va + (cpu *
1403*7c478bd9Sstevel@tonic-gate 		(1 + ~TSTAT_TBA_MASK)) + TSTAT_INSTR_SIZE);
1404*7c478bd9Sstevel@tonic-gate 	bzero(tcpu->tcpu_data, tstat_data_size);
1405*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data->tdata_cpuid = cpu;
1406*7c478bd9Sstevel@tonic-gate #endif /* sun4v */
1407*7c478bd9Sstevel@tonic-gate 
1408*7c478bd9Sstevel@tonic-gate 	/*
1409*7c478bd9Sstevel@tonic-gate 	 * Now that we have all of the instruction and data pages allocated,
1410*7c478bd9Sstevel@tonic-gate 	 * make the trap table from scratch.
1411*7c478bd9Sstevel@tonic-gate 	 */
1412*7c478bd9Sstevel@tonic-gate 	trapstat_make_traptab(tcpu);
1413*7c478bd9Sstevel@tonic-gate 
1414*7c478bd9Sstevel@tonic-gate 	if (tstat_options & TSTAT_OPT_TLBDATA) {
1415*7c478bd9Sstevel@tonic-gate 		/*
1416*7c478bd9Sstevel@tonic-gate 		 * TLB Statistics have been specified; set up the I- and D-TLB
1417*7c478bd9Sstevel@tonic-gate 		 * entries and corresponding TLB return entries.
1418*7c478bd9Sstevel@tonic-gate 		 */
1419*7c478bd9Sstevel@tonic-gate 		trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS);
1420*7c478bd9Sstevel@tonic-gate 		trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS);
1421*7c478bd9Sstevel@tonic-gate 	}
1422*7c478bd9Sstevel@tonic-gate 
1423*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags |= TSTAT_CPU_ALLOCATED;
1424*7c478bd9Sstevel@tonic-gate 
1425*7c478bd9Sstevel@tonic-gate 	/*
1426*7c478bd9Sstevel@tonic-gate 	 * Finally, get the target CPU to load the locked pages into its TLBs.
1427*7c478bd9Sstevel@tonic-gate 	 */
1428*7c478bd9Sstevel@tonic-gate 	xc_one(cpu, (xcfunc_t *)trapstat_load_tlb, 0, 0);
1429*7c478bd9Sstevel@tonic-gate }
1430*7c478bd9Sstevel@tonic-gate 
1431*7c478bd9Sstevel@tonic-gate static void
1432*7c478bd9Sstevel@tonic-gate trapstat_teardown(processorid_t cpu)
1433*7c478bd9Sstevel@tonic-gate {
1434*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
1435*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1436*7c478bd9Sstevel@tonic-gate 	int i;
1437*7c478bd9Sstevel@tonic-gate #endif
1438*7c478bd9Sstevel@tonic-gate 	caddr_t va = tcpu->tcpu_vabase;
1439*7c478bd9Sstevel@tonic-gate 
1440*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_pfn != NULL);
1441*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_instr != NULL);
1442*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_data != NULL);
1443*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
1444*7c478bd9Sstevel@tonic-gate 	ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
1445*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
1446*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1447*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tstat_lock));
1448*7c478bd9Sstevel@tonic-gate 
1449*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1450*7c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_pfn, tstat_total_pages);
1451*7c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_instr, TSTAT_INSTR_SIZE);
1452*7c478bd9Sstevel@tonic-gate 	vmem_free(tstat_arena, tcpu->tcpu_data, tstat_data_size);
1453*7c478bd9Sstevel@tonic-gate 
1454*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) {
1455*7c478bd9Sstevel@tonic-gate 		xt_one(cpu, vtag_flushpage_tl1, (uint64_t)va, KCONTEXT);
1456*7c478bd9Sstevel@tonic-gate 	}
1457*7c478bd9Sstevel@tonic-gate #else
1458*7c478bd9Sstevel@tonic-gate 	xt_one(cpu, vtag_unmap_perm_tl1, (uint64_t)va, KCONTEXT);
1459*7c478bd9Sstevel@tonic-gate #endif
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_pfn = NULL;
1462*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_instr = NULL;
1463*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_data = NULL;
1464*7c478bd9Sstevel@tonic-gate 	tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED;
1465*7c478bd9Sstevel@tonic-gate }
1466*7c478bd9Sstevel@tonic-gate 
1467*7c478bd9Sstevel@tonic-gate static int
1468*7c478bd9Sstevel@tonic-gate trapstat_go()
1469*7c478bd9Sstevel@tonic-gate {
1470*7c478bd9Sstevel@tonic-gate 	cpu_t *cp;
1471*7c478bd9Sstevel@tonic-gate 
1472*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1473*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1474*7c478bd9Sstevel@tonic-gate 
1475*7c478bd9Sstevel@tonic-gate 	if (tstat_running) {
1476*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1477*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1478*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
1479*7c478bd9Sstevel@tonic-gate 	}
1480*7c478bd9Sstevel@tonic-gate 
1481*7c478bd9Sstevel@tonic-gate #ifdef sun4v
1482*7c478bd9Sstevel@tonic-gate 	/*
1483*7c478bd9Sstevel@tonic-gate 	 * Allocate large page to hold interposing tables
1484*7c478bd9Sstevel@tonic-gate 	 */
1485*7c478bd9Sstevel@tonic-gate 	tstat_va = contig_mem_alloc(MMU_PAGESIZE4M);
1486*7c478bd9Sstevel@tonic-gate 	tstat_pfn = va_to_pfn(tstat_va);
1487*7c478bd9Sstevel@tonic-gate 	if (tstat_pfn == PFN_INVALID) {
1488*7c478bd9Sstevel@tonic-gate 		contig_mem_free(tstat_va, MMU_PAGESIZE4M);
1489*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1490*7c478bd9Sstevel@tonic-gate 	}
1491*7c478bd9Sstevel@tonic-gate #endif
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 	/*
1494*7c478bd9Sstevel@tonic-gate 	 * First, perform any necessary hot patching.
1495*7c478bd9Sstevel@tonic-gate 	 */
1496*7c478bd9Sstevel@tonic-gate 	trapstat_hotpatch();
1497*7c478bd9Sstevel@tonic-gate 
1498*7c478bd9Sstevel@tonic-gate 	/*
1499*7c478bd9Sstevel@tonic-gate 	 * Allocate the resources we'll need to measure probe effect.
1500*7c478bd9Sstevel@tonic-gate 	 */
1501*7c478bd9Sstevel@tonic-gate 	trapstat_probe_alloc();
1502*7c478bd9Sstevel@tonic-gate 
1503*7c478bd9Sstevel@tonic-gate 
1504*7c478bd9Sstevel@tonic-gate 	cp = cpu_list;
1505*7c478bd9Sstevel@tonic-gate 	do {
1506*7c478bd9Sstevel@tonic-gate 		if (!(tstat_percpu[cp->cpu_id].tcpu_flags & TSTAT_CPU_SELECTED))
1507*7c478bd9Sstevel@tonic-gate 			continue;
1508*7c478bd9Sstevel@tonic-gate 
1509*7c478bd9Sstevel@tonic-gate 		trapstat_setup(cp->cpu_id);
1510*7c478bd9Sstevel@tonic-gate 
1511*7c478bd9Sstevel@tonic-gate 		/*
1512*7c478bd9Sstevel@tonic-gate 		 * Note that due to trapstat_probe()'s use of global data,
1513*7c478bd9Sstevel@tonic-gate 		 * we determine the probe effect on each CPU serially instead
1514*7c478bd9Sstevel@tonic-gate 		 * of in parallel with an xc_all().
1515*7c478bd9Sstevel@tonic-gate 		 */
1516*7c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_probe, 0, 0);
1517*7c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 	xc_all((xcfunc_t *)trapstat_enable, 0, 0);
1520*7c478bd9Sstevel@tonic-gate 
1521*7c478bd9Sstevel@tonic-gate 	trapstat_probe_free();
1522*7c478bd9Sstevel@tonic-gate 	tstat_running = 1;
1523*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1524*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1525*7c478bd9Sstevel@tonic-gate 
1526*7c478bd9Sstevel@tonic-gate 	return (0);
1527*7c478bd9Sstevel@tonic-gate }
1528*7c478bd9Sstevel@tonic-gate 
1529*7c478bd9Sstevel@tonic-gate static int
1530*7c478bd9Sstevel@tonic-gate trapstat_stop()
1531*7c478bd9Sstevel@tonic-gate {
1532*7c478bd9Sstevel@tonic-gate 	int i;
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1535*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1536*7c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
1537*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1538*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1539*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
1540*7c478bd9Sstevel@tonic-gate 	}
1541*7c478bd9Sstevel@tonic-gate 
1542*7c478bd9Sstevel@tonic-gate 	xc_all((xcfunc_t *)trapstat_disable, 0, 0);
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= max_cpuid; i++) {
1545*7c478bd9Sstevel@tonic-gate 		if (tstat_percpu[i].tcpu_flags & TSTAT_CPU_ALLOCATED)
1546*7c478bd9Sstevel@tonic-gate 			trapstat_teardown(i);
1547*7c478bd9Sstevel@tonic-gate 	}
1548*7c478bd9Sstevel@tonic-gate 
1549*7c478bd9Sstevel@tonic-gate #ifdef sun4v
1550*7c478bd9Sstevel@tonic-gate 	contig_mem_free(tstat_va, MMU_PAGESIZE4M);
1551*7c478bd9Sstevel@tonic-gate #endif
1552*7c478bd9Sstevel@tonic-gate 	trapstat_hotpatch();
1553*7c478bd9Sstevel@tonic-gate 	tstat_running = 0;
1554*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1555*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1556*7c478bd9Sstevel@tonic-gate 
1557*7c478bd9Sstevel@tonic-gate 	return (0);
1558*7c478bd9Sstevel@tonic-gate }
1559*7c478bd9Sstevel@tonic-gate 
1560*7c478bd9Sstevel@tonic-gate /*
1561*7c478bd9Sstevel@tonic-gate  * This is trapstat's DR CPU configuration callback.  It's called (with
1562*7c478bd9Sstevel@tonic-gate  * cpu_lock held) to unconfigure a newly powered-off CPU, or to configure a
1563*7c478bd9Sstevel@tonic-gate  * powered-off CPU that is to be brought into the system.  We need only take
1564*7c478bd9Sstevel@tonic-gate  * action in the unconfigure case:  because a powered-off CPU will have its
1565*7c478bd9Sstevel@tonic-gate  * trap table restored to KERNELBASE if it is ever powered back on, we must
1566*7c478bd9Sstevel@tonic-gate  * update the flags to reflect that trapstat is no longer enabled on the
1567*7c478bd9Sstevel@tonic-gate  * powered-off CPU.  Note that this means that a TSTAT_CPU_ENABLED CPU that
1568*7c478bd9Sstevel@tonic-gate  * is unconfigured/powered off and later powered back on/reconfigured will
1569*7c478bd9Sstevel@tonic-gate  * _not_ be re-TSTAT_CPU_ENABLED.
1570*7c478bd9Sstevel@tonic-gate  */
1571*7c478bd9Sstevel@tonic-gate static int
1572*7c478bd9Sstevel@tonic-gate trapstat_cpu_setup(cpu_setup_t what, processorid_t cpu)
1573*7c478bd9Sstevel@tonic-gate {
1574*7c478bd9Sstevel@tonic-gate 	tstat_percpu_t *tcpu = &tstat_percpu[cpu];
1575*7c478bd9Sstevel@tonic-gate 
1576*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1577*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1578*7c478bd9Sstevel@tonic-gate 
1579*7c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
1580*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1581*7c478bd9Sstevel@tonic-gate 		return (0);
1582*7c478bd9Sstevel@tonic-gate 	}
1583*7c478bd9Sstevel@tonic-gate 
1584*7c478bd9Sstevel@tonic-gate 	switch (what) {
1585*7c478bd9Sstevel@tonic-gate 	case CPU_CONFIG:
1586*7c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
1587*7c478bd9Sstevel@tonic-gate 		break;
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate 	case CPU_UNCONFIG:
1590*7c478bd9Sstevel@tonic-gate 		if (tcpu->tcpu_flags & TSTAT_CPU_ENABLED)
1591*7c478bd9Sstevel@tonic-gate 			tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED;
1592*7c478bd9Sstevel@tonic-gate 		break;
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	default:
1595*7c478bd9Sstevel@tonic-gate 		break;
1596*7c478bd9Sstevel@tonic-gate 	}
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1599*7c478bd9Sstevel@tonic-gate 	return (0);
1600*7c478bd9Sstevel@tonic-gate }
1601*7c478bd9Sstevel@tonic-gate 
1602*7c478bd9Sstevel@tonic-gate /*
1603*7c478bd9Sstevel@tonic-gate  * This is called before a CPR suspend and after a CPR resume.  We don't have
1604*7c478bd9Sstevel@tonic-gate  * anything to do before a suspend, but after a restart we must restore the
1605*7c478bd9Sstevel@tonic-gate  * trap table to be our interposing trap table.  However, we don't actually
1606*7c478bd9Sstevel@tonic-gate  * know whether or not the CPUs have been powered off -- this routine may be
1607*7c478bd9Sstevel@tonic-gate  * called while restoring from a failed CPR suspend.  We thus run through each
1608*7c478bd9Sstevel@tonic-gate  * TSTAT_CPU_ENABLED CPU, and explicitly destroy and reestablish its
1609*7c478bd9Sstevel@tonic-gate  * interposing trap table.  This assures that our state is correct regardless
1610*7c478bd9Sstevel@tonic-gate  * of whether or not the CPU has been newly powered on.
1611*7c478bd9Sstevel@tonic-gate  */
1612*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1613*7c478bd9Sstevel@tonic-gate static boolean_t
1614*7c478bd9Sstevel@tonic-gate trapstat_cpr(void *arg, int code)
1615*7c478bd9Sstevel@tonic-gate {
1616*7c478bd9Sstevel@tonic-gate 	cpu_t *cp;
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate 	if (code == CB_CODE_CPR_CHKPT)
1619*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
1620*7c478bd9Sstevel@tonic-gate 
1621*7c478bd9Sstevel@tonic-gate 	ASSERT(code == CB_CODE_CPR_RESUME);
1622*7c478bd9Sstevel@tonic-gate 
1623*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1624*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1625*7c478bd9Sstevel@tonic-gate 
1626*7c478bd9Sstevel@tonic-gate 	if (!tstat_running) {
1627*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1628*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1629*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
1630*7c478bd9Sstevel@tonic-gate 	}
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 	cp = cpu_list;
1633*7c478bd9Sstevel@tonic-gate 	do {
1634*7c478bd9Sstevel@tonic-gate 		tstat_percpu_t *tcpu = &tstat_percpu[cp->cpu_id];
1635*7c478bd9Sstevel@tonic-gate 
1636*7c478bd9Sstevel@tonic-gate 		if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
1637*7c478bd9Sstevel@tonic-gate 			continue;
1638*7c478bd9Sstevel@tonic-gate 
1639*7c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
1640*7c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
1641*7c478bd9Sstevel@tonic-gate 
1642*7c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_disable, 0, 0);
1643*7c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
1644*7c478bd9Sstevel@tonic-gate 
1645*7c478bd9Sstevel@tonic-gate 		/*
1646*7c478bd9Sstevel@tonic-gate 		 * Preserve this CPU's data in tstat_buffer and rip down its
1647*7c478bd9Sstevel@tonic-gate 		 * interposing trap table.
1648*7c478bd9Sstevel@tonic-gate 		 */
1649*7c478bd9Sstevel@tonic-gate 		bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size);
1650*7c478bd9Sstevel@tonic-gate 		trapstat_teardown(cp->cpu_id);
1651*7c478bd9Sstevel@tonic-gate 		ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED));
1652*7c478bd9Sstevel@tonic-gate 
1653*7c478bd9Sstevel@tonic-gate 		/*
1654*7c478bd9Sstevel@tonic-gate 		 * Reestablish the interposing trap table and restore the old
1655*7c478bd9Sstevel@tonic-gate 		 * data.
1656*7c478bd9Sstevel@tonic-gate 		 */
1657*7c478bd9Sstevel@tonic-gate 		trapstat_setup(cp->cpu_id);
1658*7c478bd9Sstevel@tonic-gate 		ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
1659*7c478bd9Sstevel@tonic-gate 		bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size);
1660*7c478bd9Sstevel@tonic-gate 
1661*7c478bd9Sstevel@tonic-gate 		xc_one(cp->cpu_id, (xcfunc_t *)trapstat_enable, 0, 0);
1662*7c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
1663*7c478bd9Sstevel@tonic-gate 
1664*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1665*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1666*7c478bd9Sstevel@tonic-gate 
1667*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
1668*7c478bd9Sstevel@tonic-gate }
1669*7c478bd9Sstevel@tonic-gate 
1670*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1671*7c478bd9Sstevel@tonic-gate static int
1672*7c478bd9Sstevel@tonic-gate trapstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1673*7c478bd9Sstevel@tonic-gate {
1674*7c478bd9Sstevel@tonic-gate 	int i;
1675*7c478bd9Sstevel@tonic-gate 
1676*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1677*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1678*7c478bd9Sstevel@tonic-gate 	if (tstat_open != 0) {
1679*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1680*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1681*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
1682*7c478bd9Sstevel@tonic-gate 	}
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate 	/*
1685*7c478bd9Sstevel@tonic-gate 	 * Register this in open() rather than in attach() to prevent deadlock
1686*7c478bd9Sstevel@tonic-gate 	 * with DR code. During attach, I/O device tree locks are grabbed
1687*7c478bd9Sstevel@tonic-gate 	 * before trapstat_attach() is invoked - registering in attach
1688*7c478bd9Sstevel@tonic-gate 	 * will result in the lock order: device tree lock, cpu_lock.
1689*7c478bd9Sstevel@tonic-gate 	 * DR code however requires that cpu_lock be acquired before
1690*7c478bd9Sstevel@tonic-gate 	 * device tree locks.
1691*7c478bd9Sstevel@tonic-gate 	 */
1692*7c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
1693*7c478bd9Sstevel@tonic-gate 	register_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL);
1694*7c478bd9Sstevel@tonic-gate 
1695*7c478bd9Sstevel@tonic-gate 	/*
1696*7c478bd9Sstevel@tonic-gate 	 * Clear all options.  And until specific CPUs are specified, we'll
1697*7c478bd9Sstevel@tonic-gate 	 * mark all CPUs as selected.
1698*7c478bd9Sstevel@tonic-gate 	 */
1699*7c478bd9Sstevel@tonic-gate 	tstat_options = 0;
1700*7c478bd9Sstevel@tonic-gate 
1701*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= max_cpuid; i++)
1702*7c478bd9Sstevel@tonic-gate 		tstat_percpu[i].tcpu_flags |= TSTAT_CPU_SELECTED;
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate 	/*
1705*7c478bd9Sstevel@tonic-gate 	 * By default, all traps at TL=0 are enabled.  Traps at TL>0 must
1706*7c478bd9Sstevel@tonic-gate 	 * be disabled.
1707*7c478bd9Sstevel@tonic-gate 	 */
1708*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_TOTAL_NENT; i++)
1709*7c478bd9Sstevel@tonic-gate 		tstat_enabled[i] = i < TSTAT_NENT ? 1 : 0;
1710*7c478bd9Sstevel@tonic-gate 
1711*7c478bd9Sstevel@tonic-gate 	tstat_open = 1;
1712*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1713*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1714*7c478bd9Sstevel@tonic-gate 
1715*7c478bd9Sstevel@tonic-gate 	return (0);
1716*7c478bd9Sstevel@tonic-gate }
1717*7c478bd9Sstevel@tonic-gate 
1718*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1719*7c478bd9Sstevel@tonic-gate static int
1720*7c478bd9Sstevel@tonic-gate trapstat_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1721*7c478bd9Sstevel@tonic-gate {
1722*7c478bd9Sstevel@tonic-gate 	(void) trapstat_stop();
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1727*7c478bd9Sstevel@tonic-gate 	unregister_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL);
1728*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1729*7c478bd9Sstevel@tonic-gate 
1730*7c478bd9Sstevel@tonic-gate 	tstat_open = 0;
1731*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1732*7c478bd9Sstevel@tonic-gate }
1733*7c478bd9Sstevel@tonic-gate 
1734*7c478bd9Sstevel@tonic-gate static int
1735*7c478bd9Sstevel@tonic-gate trapstat_option(int option)
1736*7c478bd9Sstevel@tonic-gate {
1737*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tstat_lock);
1738*7c478bd9Sstevel@tonic-gate 
1739*7c478bd9Sstevel@tonic-gate 	if (tstat_running) {
1740*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1741*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
1742*7c478bd9Sstevel@tonic-gate 	}
1743*7c478bd9Sstevel@tonic-gate 
1744*7c478bd9Sstevel@tonic-gate 	tstat_options |= option;
1745*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tstat_lock);
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate 	return (0);
1748*7c478bd9Sstevel@tonic-gate }
1749*7c478bd9Sstevel@tonic-gate 
1750*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1751*7c478bd9Sstevel@tonic-gate static int
1752*7c478bd9Sstevel@tonic-gate trapstat_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *crd, int *rval)
1753*7c478bd9Sstevel@tonic-gate {
1754*7c478bd9Sstevel@tonic-gate 	int i, j, out;
1755*7c478bd9Sstevel@tonic-gate 	size_t dsize;
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
1758*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_GO:
1759*7c478bd9Sstevel@tonic-gate 		return (trapstat_go());
1760*7c478bd9Sstevel@tonic-gate 
1761*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOGO:
1762*7c478bd9Sstevel@tonic-gate 		return (trapstat_option(TSTAT_OPT_NOGO));
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_STOP:
1765*7c478bd9Sstevel@tonic-gate 		return (trapstat_stop());
1766*7c478bd9Sstevel@tonic-gate 
1767*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_CPU:
1768*7c478bd9Sstevel@tonic-gate 		if (arg < 0 || arg > max_cpuid)
1769*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1770*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1771*7c478bd9Sstevel@tonic-gate 
1772*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOCPU:
1773*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
1774*7c478bd9Sstevel@tonic-gate 
1775*7c478bd9Sstevel@tonic-gate 		if (tstat_running) {
1776*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
1777*7c478bd9Sstevel@tonic-gate 			return (EBUSY);
1778*7c478bd9Sstevel@tonic-gate 		}
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 		/*
1781*7c478bd9Sstevel@tonic-gate 		 * If this is the first CPU to be specified (or if we are
1782*7c478bd9Sstevel@tonic-gate 		 * being asked to explicitly de-select CPUs), disable all CPUs.
1783*7c478bd9Sstevel@tonic-gate 		 */
1784*7c478bd9Sstevel@tonic-gate 		if (!(tstat_options & TSTAT_OPT_CPU) || cmd == TSTATIOC_NOCPU) {
1785*7c478bd9Sstevel@tonic-gate 			tstat_options |= TSTAT_OPT_CPU;
1786*7c478bd9Sstevel@tonic-gate 
1787*7c478bd9Sstevel@tonic-gate 			for (i = 0; i <= max_cpuid; i++) {
1788*7c478bd9Sstevel@tonic-gate 				tstat_percpu_t *tcpu = &tstat_percpu[i];
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate 				ASSERT(cmd == TSTATIOC_NOCPU ||
1791*7c478bd9Sstevel@tonic-gate 				    (tcpu->tcpu_flags & TSTAT_CPU_SELECTED));
1792*7c478bd9Sstevel@tonic-gate 				tcpu->tcpu_flags &= ~TSTAT_CPU_SELECTED;
1793*7c478bd9Sstevel@tonic-gate 			}
1794*7c478bd9Sstevel@tonic-gate 		}
1795*7c478bd9Sstevel@tonic-gate 
1796*7c478bd9Sstevel@tonic-gate 		if (cmd == TSTATIOC_CPU)
1797*7c478bd9Sstevel@tonic-gate 			tstat_percpu[arg].tcpu_flags |= TSTAT_CPU_SELECTED;
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1800*7c478bd9Sstevel@tonic-gate 
1801*7c478bd9Sstevel@tonic-gate 		return (0);
1802*7c478bd9Sstevel@tonic-gate 
1803*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_ENTRY:
1804*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate 		if (tstat_running) {
1807*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
1808*7c478bd9Sstevel@tonic-gate 			return (EBUSY);
1809*7c478bd9Sstevel@tonic-gate 		}
1810*7c478bd9Sstevel@tonic-gate 
1811*7c478bd9Sstevel@tonic-gate 		if (arg >= TSTAT_NENT || arg < 0) {
1812*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
1813*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1814*7c478bd9Sstevel@tonic-gate 		}
1815*7c478bd9Sstevel@tonic-gate 
1816*7c478bd9Sstevel@tonic-gate 		if (!(tstat_options & TSTAT_OPT_ENTRY)) {
1817*7c478bd9Sstevel@tonic-gate 			/*
1818*7c478bd9Sstevel@tonic-gate 			 * If this is the first entry that we are explicitly
1819*7c478bd9Sstevel@tonic-gate 			 * enabling, explicitly disable every TL=0 entry.
1820*7c478bd9Sstevel@tonic-gate 			 */
1821*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < TSTAT_NENT; i++)
1822*7c478bd9Sstevel@tonic-gate 				tstat_enabled[i] = 0;
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 			tstat_options |= TSTAT_OPT_ENTRY;
1825*7c478bd9Sstevel@tonic-gate 		}
1826*7c478bd9Sstevel@tonic-gate 
1827*7c478bd9Sstevel@tonic-gate 		tstat_enabled[arg] = 1;
1828*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1829*7c478bd9Sstevel@tonic-gate 		return (0);
1830*7c478bd9Sstevel@tonic-gate 
1831*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_NOENTRY:
1832*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
1833*7c478bd9Sstevel@tonic-gate 
1834*7c478bd9Sstevel@tonic-gate 		if (tstat_running) {
1835*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tstat_lock);
1836*7c478bd9Sstevel@tonic-gate 			return (EBUSY);
1837*7c478bd9Sstevel@tonic-gate 		}
1838*7c478bd9Sstevel@tonic-gate 
1839*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSTAT_NENT; i++)
1840*7c478bd9Sstevel@tonic-gate 			tstat_enabled[i] = 0;
1841*7c478bd9Sstevel@tonic-gate 
1842*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1843*7c478bd9Sstevel@tonic-gate 		return (0);
1844*7c478bd9Sstevel@tonic-gate 
1845*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_READ:
1846*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tstat_lock);
1847*7c478bd9Sstevel@tonic-gate 
1848*7c478bd9Sstevel@tonic-gate 		if (tstat_options & TSTAT_OPT_TLBDATA) {
1849*7c478bd9Sstevel@tonic-gate 			dsize = tstat_data_t_exported_size;
1850*7c478bd9Sstevel@tonic-gate 		} else {
1851*7c478bd9Sstevel@tonic-gate 			dsize = sizeof (tstat_data_t);
1852*7c478bd9Sstevel@tonic-gate 		}
1853*7c478bd9Sstevel@tonic-gate 
1854*7c478bd9Sstevel@tonic-gate 		for (i = 0, out = 0; i <= max_cpuid; i++) {
1855*7c478bd9Sstevel@tonic-gate 			tstat_percpu_t *tcpu = &tstat_percpu[i];
1856*7c478bd9Sstevel@tonic-gate 
1857*7c478bd9Sstevel@tonic-gate 			if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED))
1858*7c478bd9Sstevel@tonic-gate 				continue;
1859*7c478bd9Sstevel@tonic-gate 
1860*7c478bd9Sstevel@tonic-gate 			ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED);
1861*7c478bd9Sstevel@tonic-gate 			ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED);
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 			tstat_buffer->tdata_cpuid = -1;
1864*7c478bd9Sstevel@tonic-gate 			xc_one(i, (xcfunc_t *)trapstat_snapshot, 0, 0);
1865*7c478bd9Sstevel@tonic-gate 
1866*7c478bd9Sstevel@tonic-gate 			if (tstat_buffer->tdata_cpuid == -1) {
1867*7c478bd9Sstevel@tonic-gate 				/*
1868*7c478bd9Sstevel@tonic-gate 				 * This CPU is not currently responding to
1869*7c478bd9Sstevel@tonic-gate 				 * cross calls; we have caught it while it is
1870*7c478bd9Sstevel@tonic-gate 				 * being unconfigured.  We'll drop tstat_lock
1871*7c478bd9Sstevel@tonic-gate 				 * and pick up and drop cpu_lock.  By the
1872*7c478bd9Sstevel@tonic-gate 				 * time we acquire cpu_lock, the DR operation
1873*7c478bd9Sstevel@tonic-gate 				 * will appear consistent and we can assert
1874*7c478bd9Sstevel@tonic-gate 				 * that trapstat_cpu_setup() has cleared
1875*7c478bd9Sstevel@tonic-gate 				 * TSTAT_CPU_ENABLED.
1876*7c478bd9Sstevel@tonic-gate 				 */
1877*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
1878*7c478bd9Sstevel@tonic-gate 				mutex_enter(&cpu_lock);
1879*7c478bd9Sstevel@tonic-gate 				mutex_exit(&cpu_lock);
1880*7c478bd9Sstevel@tonic-gate 				mutex_enter(&tstat_lock);
1881*7c478bd9Sstevel@tonic-gate 				ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED));
1882*7c478bd9Sstevel@tonic-gate 				continue;
1883*7c478bd9Sstevel@tonic-gate 			}
1884*7c478bd9Sstevel@tonic-gate 
1885*7c478bd9Sstevel@tonic-gate 			/*
1886*7c478bd9Sstevel@tonic-gate 			 * Need to compensate for the difference between page
1887*7c478bd9Sstevel@tonic-gate 			 * sizes exported to users and page sizes available
1888*7c478bd9Sstevel@tonic-gate 			 * within the kernel.
1889*7c478bd9Sstevel@tonic-gate 			 */
1890*7c478bd9Sstevel@tonic-gate 			if ((tstat_options & TSTAT_OPT_TLBDATA) &&
1891*7c478bd9Sstevel@tonic-gate 			    (tstat_pgszs != tstat_user_pgszs)) {
1892*7c478bd9Sstevel@tonic-gate 				tstat_pgszdata_t *tp;
1893*7c478bd9Sstevel@tonic-gate 				uint_t szc;
1894*7c478bd9Sstevel@tonic-gate 
1895*7c478bd9Sstevel@tonic-gate 				tp = &tstat_buffer->tdata_pgsz[0];
1896*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < tstat_user_pgszs; j++) {
1897*7c478bd9Sstevel@tonic-gate 					if ((szc = USERSZC_2_SZC(j)) != j) {
1898*7c478bd9Sstevel@tonic-gate 						bcopy(&tp[szc], &tp[j],
1899*7c478bd9Sstevel@tonic-gate 						    sizeof (tstat_pgszdata_t));
1900*7c478bd9Sstevel@tonic-gate 					}
1901*7c478bd9Sstevel@tonic-gate 				}
1902*7c478bd9Sstevel@tonic-gate 			}
1903*7c478bd9Sstevel@tonic-gate 
1904*7c478bd9Sstevel@tonic-gate 			if (copyout(tstat_buffer, (void *)arg, dsize) != 0) {
1905*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
1906*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1907*7c478bd9Sstevel@tonic-gate 			}
1908*7c478bd9Sstevel@tonic-gate 
1909*7c478bd9Sstevel@tonic-gate 			out++;
1910*7c478bd9Sstevel@tonic-gate 			arg += dsize;
1911*7c478bd9Sstevel@tonic-gate 		}
1912*7c478bd9Sstevel@tonic-gate 
1913*7c478bd9Sstevel@tonic-gate 		if (out != max_cpuid + 1) {
1914*7c478bd9Sstevel@tonic-gate 			processorid_t cpuid = -1;
1915*7c478bd9Sstevel@tonic-gate 			arg += offsetof(tstat_data_t, tdata_cpuid);
1916*7c478bd9Sstevel@tonic-gate 
1917*7c478bd9Sstevel@tonic-gate 			if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) {
1918*7c478bd9Sstevel@tonic-gate 				mutex_exit(&tstat_lock);
1919*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1920*7c478bd9Sstevel@tonic-gate 			}
1921*7c478bd9Sstevel@tonic-gate 		}
1922*7c478bd9Sstevel@tonic-gate 
1923*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tstat_lock);
1924*7c478bd9Sstevel@tonic-gate 
1925*7c478bd9Sstevel@tonic-gate 		return (0);
1926*7c478bd9Sstevel@tonic-gate 
1927*7c478bd9Sstevel@tonic-gate 	case TSTATIOC_TLBDATA:
1928*7c478bd9Sstevel@tonic-gate 		return (trapstat_option(TSTAT_OPT_TLBDATA));
1929*7c478bd9Sstevel@tonic-gate 
1930*7c478bd9Sstevel@tonic-gate 	default:
1931*7c478bd9Sstevel@tonic-gate 		break;
1932*7c478bd9Sstevel@tonic-gate 	}
1933*7c478bd9Sstevel@tonic-gate 
1934*7c478bd9Sstevel@tonic-gate 	return (ENOTTY);
1935*7c478bd9Sstevel@tonic-gate }
1936*7c478bd9Sstevel@tonic-gate 
1937*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1938*7c478bd9Sstevel@tonic-gate static int
1939*7c478bd9Sstevel@tonic-gate trapstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1940*7c478bd9Sstevel@tonic-gate {
1941*7c478bd9Sstevel@tonic-gate 	int error;
1942*7c478bd9Sstevel@tonic-gate 
1943*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
1944*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1945*7c478bd9Sstevel@tonic-gate 		*result = (void *)tstat_devi;
1946*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1947*7c478bd9Sstevel@tonic-gate 		break;
1948*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1949*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
1950*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1951*7c478bd9Sstevel@tonic-gate 		break;
1952*7c478bd9Sstevel@tonic-gate 	default:
1953*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
1954*7c478bd9Sstevel@tonic-gate 	}
1955*7c478bd9Sstevel@tonic-gate 	return (error);
1956*7c478bd9Sstevel@tonic-gate }
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate static int
1959*7c478bd9Sstevel@tonic-gate trapstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1960*7c478bd9Sstevel@tonic-gate {
1961*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
1962*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
1963*7c478bd9Sstevel@tonic-gate 		break;
1964*7c478bd9Sstevel@tonic-gate 
1965*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
1966*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1967*7c478bd9Sstevel@tonic-gate 
1968*7c478bd9Sstevel@tonic-gate 	default:
1969*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1970*7c478bd9Sstevel@tonic-gate 	}
1971*7c478bd9Sstevel@tonic-gate 
1972*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "trapstat", S_IFCHR,
1973*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, 0) == DDI_FAILURE) {
1974*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
1975*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1976*7c478bd9Sstevel@tonic-gate 	}
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
1979*7c478bd9Sstevel@tonic-gate 	tstat_devi = devi;
1980*7c478bd9Sstevel@tonic-gate 
1981*7c478bd9Sstevel@tonic-gate 	tstat_pgszs = page_num_pagesizes();
1982*7c478bd9Sstevel@tonic-gate 	tstat_user_pgszs = page_num_user_pagesizes();
1983*7c478bd9Sstevel@tonic-gate 	tstat_data_t_size = sizeof (tstat_data_t) +
1984*7c478bd9Sstevel@tonic-gate 	    (tstat_pgszs - 1) * sizeof (tstat_pgszdata_t);
1985*7c478bd9Sstevel@tonic-gate 	tstat_data_t_exported_size = sizeof (tstat_data_t) +
1986*7c478bd9Sstevel@tonic-gate 	    (tstat_user_pgszs - 1) * sizeof (tstat_pgszdata_t);
1987*7c478bd9Sstevel@tonic-gate #ifndef sun4v
1988*7c478bd9Sstevel@tonic-gate 	tstat_data_pages = (tstat_data_t_size >> MMU_PAGESHIFT) + 1;
1989*7c478bd9Sstevel@tonic-gate 	tstat_total_pages = TSTAT_INSTR_PAGES + tstat_data_pages;
1990*7c478bd9Sstevel@tonic-gate 	tstat_data_size = tstat_data_pages * MMU_PAGESIZE;
1991*7c478bd9Sstevel@tonic-gate 	tstat_total_size = TSTAT_INSTR_SIZE + tstat_data_size;
1992*7c478bd9Sstevel@tonic-gate #else
1993*7c478bd9Sstevel@tonic-gate 	tstat_data_pages = 0;
1994*7c478bd9Sstevel@tonic-gate 	tstat_data_size = tstat_data_t_size;
1995*7c478bd9Sstevel@tonic-gate 	tstat_total_pages = ((TSTAT_INSTR_SIZE + tstat_data_size) >>
1996*7c478bd9Sstevel@tonic-gate 		MMU_PAGESHIFT) + 1;
1997*7c478bd9Sstevel@tonic-gate 	tstat_total_size = tstat_total_pages * MMU_PAGESIZE;
1998*7c478bd9Sstevel@tonic-gate #endif
1999*7c478bd9Sstevel@tonic-gate 
2000*7c478bd9Sstevel@tonic-gate 	tstat_percpu = kmem_zalloc((max_cpuid + 1) *
2001*7c478bd9Sstevel@tonic-gate 	    sizeof (tstat_percpu_t), KM_SLEEP);
2002*7c478bd9Sstevel@tonic-gate 
2003*7c478bd9Sstevel@tonic-gate 	/*
2004*7c478bd9Sstevel@tonic-gate 	 * Create our own arena backed by segkmem to assure a source of
2005*7c478bd9Sstevel@tonic-gate 	 * MMU_PAGESIZE-aligned allocations.  We allocate out of the
2006*7c478bd9Sstevel@tonic-gate 	 * heap32_arena to assure that we can address the allocated memory with
2007*7c478bd9Sstevel@tonic-gate 	 * a single sethi/simm13 pair in the interposing trap table entries.
2008*7c478bd9Sstevel@tonic-gate 	 */
2009*7c478bd9Sstevel@tonic-gate 	tstat_arena = vmem_create("trapstat", NULL, 0, MMU_PAGESIZE,
2010*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc_permanent, segkmem_free, heap32_arena, 0, VM_SLEEP);
2011*7c478bd9Sstevel@tonic-gate 
2012*7c478bd9Sstevel@tonic-gate 	tstat_enabled = kmem_alloc(TSTAT_TOTAL_NENT * sizeof (int), KM_SLEEP);
2013*7c478bd9Sstevel@tonic-gate 	tstat_buffer = kmem_alloc(tstat_data_t_size, KM_SLEEP);
2014*7c478bd9Sstevel@tonic-gate 
2015*7c478bd9Sstevel@tonic-gate 	/*
2016*7c478bd9Sstevel@tonic-gate 	 * CB_CL_CPR_POST_USER is the class that executes from cpr_resume()
2017*7c478bd9Sstevel@tonic-gate 	 * after user threads can be restarted.  By executing in this class,
2018*7c478bd9Sstevel@tonic-gate 	 * we are assured of the availability of system services needed to
2019*7c478bd9Sstevel@tonic-gate 	 * resume trapstat (specifically, we are assured that all CPUs are
2020*7c478bd9Sstevel@tonic-gate 	 * restarted and responding to cross calls).
2021*7c478bd9Sstevel@tonic-gate 	 */
2022*7c478bd9Sstevel@tonic-gate 	tstat_cprcb =
2023*7c478bd9Sstevel@tonic-gate 	    callb_add(trapstat_cpr, NULL, CB_CL_CPR_POST_USER, "trapstat");
2024*7c478bd9Sstevel@tonic-gate 
2025*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2026*7c478bd9Sstevel@tonic-gate }
2027*7c478bd9Sstevel@tonic-gate 
2028*7c478bd9Sstevel@tonic-gate static int
2029*7c478bd9Sstevel@tonic-gate trapstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2030*7c478bd9Sstevel@tonic-gate {
2031*7c478bd9Sstevel@tonic-gate 	int rval;
2032*7c478bd9Sstevel@tonic-gate 
2033*7c478bd9Sstevel@tonic-gate 	ASSERT(devi == tstat_devi);
2034*7c478bd9Sstevel@tonic-gate 
2035*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
2036*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
2037*7c478bd9Sstevel@tonic-gate 		break;
2038*7c478bd9Sstevel@tonic-gate 
2039*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
2040*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2041*7c478bd9Sstevel@tonic-gate 
2042*7c478bd9Sstevel@tonic-gate 	default:
2043*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2044*7c478bd9Sstevel@tonic-gate 	}
2045*7c478bd9Sstevel@tonic-gate 
2046*7c478bd9Sstevel@tonic-gate 	ASSERT(!tstat_running);
2047*7c478bd9Sstevel@tonic-gate 
2048*7c478bd9Sstevel@tonic-gate 	rval = callb_delete(tstat_cprcb);
2049*7c478bd9Sstevel@tonic-gate 	ASSERT(rval == 0);
2050*7c478bd9Sstevel@tonic-gate 
2051*7c478bd9Sstevel@tonic-gate 	kmem_free(tstat_buffer, tstat_data_t_size);
2052*7c478bd9Sstevel@tonic-gate 	kmem_free(tstat_enabled, TSTAT_TOTAL_NENT * sizeof (int));
2053*7c478bd9Sstevel@tonic-gate 	vmem_destroy(tstat_arena);
2054*7c478bd9Sstevel@tonic-gate 	kmem_free(tstat_percpu, (max_cpuid + 1) * sizeof (tstat_percpu_t));
2055*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2056*7c478bd9Sstevel@tonic-gate 
2057*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2058*7c478bd9Sstevel@tonic-gate }
2059*7c478bd9Sstevel@tonic-gate 
2060*7c478bd9Sstevel@tonic-gate /*
2061*7c478bd9Sstevel@tonic-gate  * Configuration data structures
2062*7c478bd9Sstevel@tonic-gate  */
2063*7c478bd9Sstevel@tonic-gate static struct cb_ops trapstat_cb_ops = {
2064*7c478bd9Sstevel@tonic-gate 	trapstat_open,		/* open */
2065*7c478bd9Sstevel@tonic-gate 	trapstat_close,		/* close */
2066*7c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
2067*7c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
2068*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
2069*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
2070*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
2071*7c478bd9Sstevel@tonic-gate 	trapstat_ioctl,		/* ioctl */
2072*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
2073*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
2074*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
2075*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
2076*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2077*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab */
2078*7c478bd9Sstevel@tonic-gate 	D_MP | D_NEW		/* Driver compatibility flag */
2079*7c478bd9Sstevel@tonic-gate };
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate static struct dev_ops trapstat_ops = {
2082*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
2083*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
2084*7c478bd9Sstevel@tonic-gate 	trapstat_info,		/* getinfo */
2085*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
2086*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
2087*7c478bd9Sstevel@tonic-gate 	trapstat_attach,	/* attach */
2088*7c478bd9Sstevel@tonic-gate 	trapstat_detach,	/* detach */
2089*7c478bd9Sstevel@tonic-gate 	nulldev,		/* reset */
2090*7c478bd9Sstevel@tonic-gate 	&trapstat_cb_ops,	/* cb_ops */
2091*7c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus_ops */
2092*7c478bd9Sstevel@tonic-gate };
2093*7c478bd9Sstevel@tonic-gate 
2094*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2095*7c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module.  This one is a driver */
2096*7c478bd9Sstevel@tonic-gate 	"Trap Statistics",	/* name of module */
2097*7c478bd9Sstevel@tonic-gate 	&trapstat_ops,		/* driver ops */
2098*7c478bd9Sstevel@tonic-gate };
2099*7c478bd9Sstevel@tonic-gate 
2100*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2101*7c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
2102*7c478bd9Sstevel@tonic-gate };
2103*7c478bd9Sstevel@tonic-gate 
2104*7c478bd9Sstevel@tonic-gate int
2105*7c478bd9Sstevel@tonic-gate _init(void)
2106*7c478bd9Sstevel@tonic-gate {
2107*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2108*7c478bd9Sstevel@tonic-gate }
2109*7c478bd9Sstevel@tonic-gate 
2110*7c478bd9Sstevel@tonic-gate int
2111*7c478bd9Sstevel@tonic-gate _fini(void)
2112*7c478bd9Sstevel@tonic-gate {
2113*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2114*7c478bd9Sstevel@tonic-gate }
2115*7c478bd9Sstevel@tonic-gate 
2116*7c478bd9Sstevel@tonic-gate int
2117*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2118*7c478bd9Sstevel@tonic-gate {
2119*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2120*7c478bd9Sstevel@tonic-gate }
2121