17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 525cf1a30Sjl139090 * Common Development and Distribution License (the "License"). 625cf1a30Sjl139090 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2225cf1a30Sjl139090 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/systm.h> 297c478bd9Sstevel@tonic-gate #include <sys/conf.h> 307c478bd9Sstevel@tonic-gate #include <sys/stat.h> 317c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 347c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h> 357c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h> 367c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 377c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h> 387c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 397c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 407c478bd9Sstevel@tonic-gate #include <sys/machasi.h> 417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 427c478bd9Sstevel@tonic-gate #include <sys/callb.h> 437c478bd9Sstevel@tonic-gate #include <sys/archsystm.h> 447c478bd9Sstevel@tonic-gate #include <sys/trapstat.h> 457c478bd9Sstevel@tonic-gate #ifdef sun4v 467c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h> 477c478bd9Sstevel@tonic-gate #endif 4825cf1a30Sjl139090 #ifndef sun4v 4925cf1a30Sjl139090 #include <sys/chip.h> 5025cf1a30Sjl139090 #endif 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */ 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * trapstat: Trap Statistics through Dynamic Trap Table Interposition 557c478bd9Sstevel@tonic-gate * ------------------------------------------------------------------- 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * Motivation and Overview 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * Despite being a fundamental indicator of system behavior, there has 607c478bd9Sstevel@tonic-gate * historically been very little insight provided into the frequency and cost 617c478bd9Sstevel@tonic-gate * of machine-specific traps. The lack of insight has been especially acute 627c478bd9Sstevel@tonic-gate * on UltraSPARC microprocessors: because these microprocessors handle TLB 637c478bd9Sstevel@tonic-gate * misses as software traps, the frequency and duration of traps play a 647c478bd9Sstevel@tonic-gate * decisive role in the performance of the memory system. As applications have 657c478bd9Sstevel@tonic-gate * increasingly outstripped TLB reach, this has become increasingly true. 667c478bd9Sstevel@tonic-gate * 677c478bd9Sstevel@tonic-gate * Part of the difficulty of observing trap behavior is that the trap handlers 687c478bd9Sstevel@tonic-gate * are so frequently called (e.g. millions of times per second) that any 697c478bd9Sstevel@tonic-gate * permanently enabled instrumentation would induce an unacceptable performance 707c478bd9Sstevel@tonic-gate * degradation. Thus, it is a constraint on any trap observability 717c478bd9Sstevel@tonic-gate * infrastructure that it have no probe effect when not explicitly enabled. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * The basic idea, then, is to create an interposing trap table in which each 747c478bd9Sstevel@tonic-gate * entry increments a per-trap, in-memory counter and then jumps to the actual, 757c478bd9Sstevel@tonic-gate * underlying trap table entry. To enable trapstat, we atomically write to the 767c478bd9Sstevel@tonic-gate * trap base address (%tba) register to point to our interposing trap table. 777c478bd9Sstevel@tonic-gate * (Note that per-CPU statistics fall out by creating a different trap table 787c478bd9Sstevel@tonic-gate * for each CPU.) 797c478bd9Sstevel@tonic-gate * 807c478bd9Sstevel@tonic-gate * Implementation Details 817c478bd9Sstevel@tonic-gate * 827c478bd9Sstevel@tonic-gate * While the idea is straight-forward, a nuance of SPARC V9 slightly 837c478bd9Sstevel@tonic-gate * complicates the implementation. Unlike its predecessors, SPARC V9 supports 847c478bd9Sstevel@tonic-gate * the notion of nested traps. The trap level is kept in the TL register: 857c478bd9Sstevel@tonic-gate * during normal operation it is 0; when a trap is taken, the TL register is 867c478bd9Sstevel@tonic-gate * incremented by 1. To aid system software, SPARC V9 breaks the trap table 877c478bd9Sstevel@tonic-gate * into two halves: the lower half contains the trap handlers for traps taken 887c478bd9Sstevel@tonic-gate * when TL is 0; the upper half contains the trap handlers for traps taken 897c478bd9Sstevel@tonic-gate * when TL is greater than 0. Each half is further subdivided into two 907c478bd9Sstevel@tonic-gate * subsequent halves: the lower half contains the trap handlers for traps 917c478bd9Sstevel@tonic-gate * other than those induced by the trap instruction (Tcc variants); the upper 927c478bd9Sstevel@tonic-gate * half contains the trap handlers for traps induced by the trap instruction. 937c478bd9Sstevel@tonic-gate * This gives a total of four ranges, with each range containing 256 traps: 947c478bd9Sstevel@tonic-gate * 957c478bd9Sstevel@tonic-gate * +--------------------------------+- 3ff 967c478bd9Sstevel@tonic-gate * | | . 977c478bd9Sstevel@tonic-gate * | Trap instruction, TL>0 | . 987c478bd9Sstevel@tonic-gate * | | . 997c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 300 1007c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 2ff 1017c478bd9Sstevel@tonic-gate * | | . 1027c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . 1037c478bd9Sstevel@tonic-gate * | | . 1047c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 1057c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff 1067c478bd9Sstevel@tonic-gate * | | . 1077c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . 1087c478bd9Sstevel@tonic-gate * | | . 1097c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 1107c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff 1117c478bd9Sstevel@tonic-gate * | | . 1127c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . 1137c478bd9Sstevel@tonic-gate * | | . 1147c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * 1177c478bd9Sstevel@tonic-gate * Solaris, however, doesn't have reason to support trap instructions when 1187c478bd9Sstevel@tonic-gate * TL>0 (only privileged code may execute at TL>0; not supporting this only 1197c478bd9Sstevel@tonic-gate * constrains our own implementation). The trap table actually looks like: 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff 1227c478bd9Sstevel@tonic-gate * | | . 1237c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . 1247c478bd9Sstevel@tonic-gate * | | . 1257c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 1267c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff 1277c478bd9Sstevel@tonic-gate * | | . 1287c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . 1297c478bd9Sstevel@tonic-gate * | | . 1307c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 1317c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff 1327c478bd9Sstevel@tonic-gate * | | . 1337c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . 1347c478bd9Sstevel@tonic-gate * | | . 1357c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 1367c478bd9Sstevel@tonic-gate * 1377c478bd9Sstevel@tonic-gate * Putatively to aid system software, SPARC V9 has the notion of multiple 1387c478bd9Sstevel@tonic-gate * sets of global registers. UltraSPARC defines four sets of global 1397c478bd9Sstevel@tonic-gate * registers: 1407c478bd9Sstevel@tonic-gate * 1417c478bd9Sstevel@tonic-gate * Normal Globals 1427c478bd9Sstevel@tonic-gate * Alternate Globals (AGs) 1437c478bd9Sstevel@tonic-gate * MMU Globals (MGs) 1447c478bd9Sstevel@tonic-gate * Interrupt Globals (IGs) 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * The set of globals in use is controlled by bits in PSTATE; when TL is 0 1477c478bd9Sstevel@tonic-gate * (and PSTATE has not been otherwise explicitly modified), the Normal Globals 1487c478bd9Sstevel@tonic-gate * are in use. When a trap is issued, PSTATE is modified to point to a set of 1497c478bd9Sstevel@tonic-gate * globals corresponding to the trap type. Most traps correspond to the 1507c478bd9Sstevel@tonic-gate * Alternate Globals, with a minority corresponding to the MMU Globals, and 1517c478bd9Sstevel@tonic-gate * only the interrupt-vector trap (vector 0x60) corresponding to the Interrupt 1527c478bd9Sstevel@tonic-gate * Globals. (The complete mapping can be found in the UltraSPARC I&II User's 1537c478bd9Sstevel@tonic-gate * Manual.) 1547c478bd9Sstevel@tonic-gate * 1557c478bd9Sstevel@tonic-gate * Note that the sets of globals are per trap _type_, not per trap _level_. 1567c478bd9Sstevel@tonic-gate * Thus, when executing a TL>0 trap handler, one may not have registers 1577c478bd9Sstevel@tonic-gate * available (for example, both trap-instruction traps and spill traps execute 1587c478bd9Sstevel@tonic-gate * on the alternate globals; if a trap-instruction trap induces a window spill, 1597c478bd9Sstevel@tonic-gate * the window spill handler has no available globals). For trapstat, this is 1607c478bd9Sstevel@tonic-gate * problematic: a register is required to transfer control from one arbitrary 1617c478bd9Sstevel@tonic-gate * location (in the interposing trap table) to another (in the actual trap 1627c478bd9Sstevel@tonic-gate * table). 1637c478bd9Sstevel@tonic-gate * 1647c478bd9Sstevel@tonic-gate * We solve this problem by exploiting the trap table's location at the bottom 1657c478bd9Sstevel@tonic-gate * of valid kernel memory (i.e. at KERNELBASE). We locate the interposing trap 1667c478bd9Sstevel@tonic-gate * tables just below KERNELBASE -- thereby allowing us to use a branch-always 1677c478bd9Sstevel@tonic-gate * instruction (ba) instead of a jump instruction (jmp) to transfer control 1687c478bd9Sstevel@tonic-gate * from the TL>0 entries in the interposing trap table to the TL>0 entries in 1697c478bd9Sstevel@tonic-gate * the actual trap table. (N.B. while this allows trap table interposition to 1707c478bd9Sstevel@tonic-gate * work, it necessarily limits trapstat to only recording information about 1717c478bd9Sstevel@tonic-gate * TL=0 traps -- there is no way to increment a counter without using a 1727c478bd9Sstevel@tonic-gate * register.) Diagrammatically: 1737c478bd9Sstevel@tonic-gate * 1747c478bd9Sstevel@tonic-gate * Actual trap table: 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff 1777c478bd9Sstevel@tonic-gate * | | . 1787c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . <-----------------------+ 1797c478bd9Sstevel@tonic-gate * | | . <-----------------------|-+ 1807c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 <-----------------------|-|-+ 1817c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff | | | 1827c478bd9Sstevel@tonic-gate * | | . | | | 1837c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . <-----------------+ | | | 1847c478bd9Sstevel@tonic-gate * | | . <-----------------|-+ | | | 1857c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 <-----------------|-|-+ | | | 1867c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff | | | | | | 1877c478bd9Sstevel@tonic-gate * | | . | | | | | | 1887c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . <-----------+ | | | | | | 1897c478bd9Sstevel@tonic-gate * | | . <-----------|-+ | | | | | | 1907c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 <-----------|-|-+ | | | | | | 1917c478bd9Sstevel@tonic-gate * KERNELBASE | | | | | | | | | 1927c478bd9Sstevel@tonic-gate * | | | | | | | | | 1937c478bd9Sstevel@tonic-gate * | | | | | | | | | 1947c478bd9Sstevel@tonic-gate * Interposing trap table: | | | | | | | | | 1957c478bd9Sstevel@tonic-gate * | | | | | | | | | 1967c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff | | | | | | | | | 1977c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 1987c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 1997c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 2007c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 203 | | | | | | | | | 2017c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|-+ | | 2027c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 202 | | | | | | | | 2037c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|---+ | 2047c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 201 | | | | | | | 2057c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|-----+ 2067c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 | | | | | | 2077c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2087c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2097c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2107c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 103 | | | | | | 2117c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | | | 2127c478bd9Sstevel@tonic-gate * | ba,a | -------------------+ | | 2137c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 102 | | | | | 2147c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | | 2157c478bd9Sstevel@tonic-gate * | ba,a | ---------------------+ | 2167c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 101 | | | | 2177c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | 2187c478bd9Sstevel@tonic-gate * | ba,a | -----------------------+ 2197c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 | | | 2207c478bd9Sstevel@tonic-gate * | ... | . | | | 2217c478bd9Sstevel@tonic-gate * | ... | . | | | 2227c478bd9Sstevel@tonic-gate * | ... | . | | | 2237c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 003 | | | 2247c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | 2257c478bd9Sstevel@tonic-gate * | ba,a | -------------+ | | 2267c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 002 | | 2277c478bd9Sstevel@tonic-gate * | (Increment counter) | | | 2287c478bd9Sstevel@tonic-gate * | ba,a | ---------------+ | 2297c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 001 | 2307c478bd9Sstevel@tonic-gate * | (Increment counter) | | 2317c478bd9Sstevel@tonic-gate * | ba,a | -----------------+ 2327c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 2337c478bd9Sstevel@tonic-gate * KERNELBASE - tstat_total_size 2347c478bd9Sstevel@tonic-gate * 2357c478bd9Sstevel@tonic-gate * tstat_total_size is the number of pages required for each trap table. It 2367c478bd9Sstevel@tonic-gate * must be true that KERNELBASE - tstat_total_size is less than the maximum 2377c478bd9Sstevel@tonic-gate * branch displacement; if each CPU were to consume a disjoint virtual range 2387c478bd9Sstevel@tonic-gate * below KERNELBASE for its trap table, we could support at most 2397c478bd9Sstevel@tonic-gate * (maximum_branch_displacement / tstat_total_size) CPUs. The maximum branch 2407c478bd9Sstevel@tonic-gate * displacement for Bicc variants is just under eight megabytes, and (because 2417c478bd9Sstevel@tonic-gate * the %tba must be 32K aligned), tstat_total_size must be at least 32K; if 2427c478bd9Sstevel@tonic-gate * each CPU were to consume a disjoint virtual range, we would have an 2437c478bd9Sstevel@tonic-gate * unacceptably low upper bound of 256 CPUs. 2447c478bd9Sstevel@tonic-gate * 2457c478bd9Sstevel@tonic-gate * While there are tricks that one could use to address this constraint (e.g., 2467c478bd9Sstevel@tonic-gate * creating trampolines every maximum_branch_displacement bytes), we instead 2477c478bd9Sstevel@tonic-gate * solve this by not permitting each CPU to consume a disjoint virtual range. 2487c478bd9Sstevel@tonic-gate * Rather, we have each CPU's interposing trap table use the _same_ virtual 2497c478bd9Sstevel@tonic-gate * range, but we back the trap tables with disjoint physical memory. Normally, 2507c478bd9Sstevel@tonic-gate * such one-to-many virtual-to-physical mappings are illegal; this is 2517c478bd9Sstevel@tonic-gate * permissible here only because the pages for the interposing trap table are 2527c478bd9Sstevel@tonic-gate * necessarily locked in the TLB. (The CPUs thus never have the opportunity to 2537c478bd9Sstevel@tonic-gate * discover that they have conflicting translations.) 2547c478bd9Sstevel@tonic-gate * 2557c478bd9Sstevel@tonic-gate * On CMT architectures in which CPUs can share MMUs, the above trick will not 2567c478bd9Sstevel@tonic-gate * work: two CPUs that share an MMU cannot have the same virtual address map 2577c478bd9Sstevel@tonic-gate * to disjoint physical pages. On these architectures, any CPUs sharing the 2587c478bd9Sstevel@tonic-gate * same MMU must consume a disjoint 32K virtual address range -- limiting the 2597c478bd9Sstevel@tonic-gate * number of CPUs sharing an MMU on these architectures to 256 due to the 2607c478bd9Sstevel@tonic-gate * branch displacement limitation described above. On the sun4v architecture, 2617c478bd9Sstevel@tonic-gate * there is a further limitation: a guest may not have more than eight locked 2627c478bd9Sstevel@tonic-gate * TLB entries per MMU. To allow operation under this restriction, the 2637c478bd9Sstevel@tonic-gate * interposing trap table and the trap statistics are each accessed through 2647c478bd9Sstevel@tonic-gate * a single 4M TLB entry. This limits the footprint to two locked entries 2657c478bd9Sstevel@tonic-gate * (one for the I-TLB and one for the D-TLB), but further restricts the number 2667c478bd9Sstevel@tonic-gate * of CPUs to 128 per MMU. However, support for more than 128 CPUs can easily 2677c478bd9Sstevel@tonic-gate * be added via a hybrid scheme, where the same 4M virtual address is used 2687c478bd9Sstevel@tonic-gate * on different MMUs. 2697c478bd9Sstevel@tonic-gate * 2707c478bd9Sstevel@tonic-gate * 2717c478bd9Sstevel@tonic-gate * TLB Statistics 2727c478bd9Sstevel@tonic-gate * 2737c478bd9Sstevel@tonic-gate * Because TLB misses are an important component of system performance, we wish 2747c478bd9Sstevel@tonic-gate * to know much more about these traps than simply the number received. 2757c478bd9Sstevel@tonic-gate * Specifically, we wish to know: 2767c478bd9Sstevel@tonic-gate * 2777c478bd9Sstevel@tonic-gate * (a) The amount of time spent executing the TLB miss handler 2787c478bd9Sstevel@tonic-gate * (b) TLB misses versus TSB misses 2797c478bd9Sstevel@tonic-gate * (c) Kernel-level misses versus user-level misses 2807c478bd9Sstevel@tonic-gate * (d) Misses per pagesize 2817c478bd9Sstevel@tonic-gate * 2827c478bd9Sstevel@tonic-gate * TLB Statistics: Time Spent Executing 2837c478bd9Sstevel@tonic-gate * 2847c478bd9Sstevel@tonic-gate * To accurately determine the amount of time spent executing the TLB miss 2857c478bd9Sstevel@tonic-gate * handler, one must get a timestamp on trap entry and trap exit, subtract the 2867c478bd9Sstevel@tonic-gate * latter from the former, and add the result to an accumulating count. 2877c478bd9Sstevel@tonic-gate * Consider flow of control during normal TLB miss processing (where "ldx 2887c478bd9Sstevel@tonic-gate * [%g2], %g2" is an arbitrary TLB-missing instruction): 2897c478bd9Sstevel@tonic-gate * 2907c478bd9Sstevel@tonic-gate * + - - - - - - - -+ 2917c478bd9Sstevel@tonic-gate * : : 2927c478bd9Sstevel@tonic-gate * : ldx [%g2], %g2 :<-------------------------------------------------------+ 2937c478bd9Sstevel@tonic-gate * : : Return from trap: | 2947c478bd9Sstevel@tonic-gate * + - - - - - - - -+ TL <- TL - 1 (0) | 2957c478bd9Sstevel@tonic-gate * | %pc <- TSTATE[TL].TPC (address of load) | 2967c478bd9Sstevel@tonic-gate * | TLB miss: | 2977c478bd9Sstevel@tonic-gate * | TL <- TL + 1 (1) | 2987c478bd9Sstevel@tonic-gate * | %pc <- TLB-miss-trap-handler | 2997c478bd9Sstevel@tonic-gate * | | 3007c478bd9Sstevel@tonic-gate * v | 3017c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + | 3027c478bd9Sstevel@tonic-gate * : : | 3037c478bd9Sstevel@tonic-gate * : Lookup VA in TSB : | 3047c478bd9Sstevel@tonic-gate * : If (hit) : | 3057c478bd9Sstevel@tonic-gate * : Fill TLB : | 3067c478bd9Sstevel@tonic-gate * : Else : | 3077c478bd9Sstevel@tonic-gate * : Lookup VA (hme hash table : | 3087c478bd9Sstevel@tonic-gate * : or segkpm) : | 3097c478bd9Sstevel@tonic-gate * : Fill TLB : | 3107c478bd9Sstevel@tonic-gate * : Endif : | 3117c478bd9Sstevel@tonic-gate * : Issue "retry" ---------------------------------------------------------+ 3127c478bd9Sstevel@tonic-gate * : : 3137c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + 3147c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler 3157c478bd9Sstevel@tonic-gate * 3167c478bd9Sstevel@tonic-gate * 3177c478bd9Sstevel@tonic-gate * As the above diagram indicates, interposing on the trap table allows one 3187c478bd9Sstevel@tonic-gate * only to determine a timestamp on trap _entry_: when the TLB miss handler 3197c478bd9Sstevel@tonic-gate * has completed filling the TLB, a "retry" will be issued, and control will 3207c478bd9Sstevel@tonic-gate * transfer immediately back to the missing %pc. 3217c478bd9Sstevel@tonic-gate * 3227c478bd9Sstevel@tonic-gate * To obtain a timestamp on trap exit, we must then somehow interpose between 3237c478bd9Sstevel@tonic-gate * the "retry" and the subsequent control transfer to the TLB-missing 3247c478bd9Sstevel@tonic-gate * instruction. To do this, we _push_ a trap level. The basic idea is to 3257c478bd9Sstevel@tonic-gate * spoof a TLB miss by raising TL, setting the %tpc to be within text 3267c478bd9Sstevel@tonic-gate * controlled by trapstat (the "TLB return entry") and branching to the 3277c478bd9Sstevel@tonic-gate * underlying TLB miss handler. When the TLB miss handler issues its "retry", 3287c478bd9Sstevel@tonic-gate * control will transfer not to the TLB-missing instruction, but rather to the 3297c478bd9Sstevel@tonic-gate * TLB return entry. This code can then obtain a timestamp, and issue its own 3307c478bd9Sstevel@tonic-gate * "retry" -- thereby correctly returning to the TLB-missing instruction. 3317c478bd9Sstevel@tonic-gate * Here is the above TLB miss flow control diagram modified to reflect 3327c478bd9Sstevel@tonic-gate * trapstat's operation: 3337c478bd9Sstevel@tonic-gate * 3347c478bd9Sstevel@tonic-gate * + - - - - - - - -+ 3357c478bd9Sstevel@tonic-gate * : : 3367c478bd9Sstevel@tonic-gate * : ldx [%g2], %g2 :<-------------------------------------------------------+ 3377c478bd9Sstevel@tonic-gate * : : Return from trap: | 3387c478bd9Sstevel@tonic-gate * + - - - - - - - -+ TL <- TL - 1 (0) | 3397c478bd9Sstevel@tonic-gate * | %pc <- TSTATE[TL].TPC (address of load) | 3407c478bd9Sstevel@tonic-gate * | TLB miss: | 3417c478bd9Sstevel@tonic-gate * | TL <- TL + 1 (1) | 3427c478bd9Sstevel@tonic-gate * | %pc <- TLB-miss-trap-handler (trapstat) | 3437c478bd9Sstevel@tonic-gate * | | 3447c478bd9Sstevel@tonic-gate * v TLB-return-entry (trapstat) | 3457c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - + | 3467c478bd9Sstevel@tonic-gate * : : : : | 3477c478bd9Sstevel@tonic-gate * : Record timestamp : : Record timestamp : | 3487c478bd9Sstevel@tonic-gate * : TL <- 2 : : Take timestamp difference : | 3497c478bd9Sstevel@tonic-gate * : TSTATE[1].TPC <- TLB-return-entry : : Add to running total : | 3507c478bd9Sstevel@tonic-gate * : ba,a TLB-miss-trap-handler -----------+ : Issue "retry" --------------+ 3517c478bd9Sstevel@tonic-gate * : : | : : 3527c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - - - - + | + - - - - - - - - - - - - - + 3537c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler | ^ 3547c478bd9Sstevel@tonic-gate * (trapstat) | | 3557c478bd9Sstevel@tonic-gate * | | 3567c478bd9Sstevel@tonic-gate * | | 3577c478bd9Sstevel@tonic-gate * +-----------------------+ | 3587c478bd9Sstevel@tonic-gate * | | 3597c478bd9Sstevel@tonic-gate * | | 3607c478bd9Sstevel@tonic-gate * v | 3617c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + | 3627c478bd9Sstevel@tonic-gate * : : | 3637c478bd9Sstevel@tonic-gate * : Lookup VA in TSB : | 3647c478bd9Sstevel@tonic-gate * : If (hit) : | 3657c478bd9Sstevel@tonic-gate * : Fill TLB : | 3667c478bd9Sstevel@tonic-gate * : Else : | 3677c478bd9Sstevel@tonic-gate * : Lookup VA (hme hash table : | 3687c478bd9Sstevel@tonic-gate * : or segkpm) : | 3697c478bd9Sstevel@tonic-gate * : Fill TLB : | 3707c478bd9Sstevel@tonic-gate * : Endif : | 3717c478bd9Sstevel@tonic-gate * : Issue "retry" ------------------------------------------+ 3727c478bd9Sstevel@tonic-gate * : : Return from trap: 3737c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + TL <- TL - 1 (1) 3747c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler %pc <- TSTATE[TL].TPC (TLB-return-entry) 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * 3777c478bd9Sstevel@tonic-gate * A final subterfuge is required to complete our artifice: if we miss in 3787c478bd9Sstevel@tonic-gate * the TLB, the TSB _and_ the subsequent hash or segkpm lookup (that is, if 3797c478bd9Sstevel@tonic-gate * there is no valid translation for the TLB-missing address), common system 3807c478bd9Sstevel@tonic-gate * software will need to accurately determine the %tpc as part of its page 3817c478bd9Sstevel@tonic-gate * fault handling. We therefore modify the kernel to check the %tpc in this 3827c478bd9Sstevel@tonic-gate * case: if the %tpc falls within the VA range controlled by trapstat and 3837c478bd9Sstevel@tonic-gate * the TL is 2, TL is simply lowered back to 1 (this check is implemented 3847c478bd9Sstevel@tonic-gate * by the TSTAT_CHECK_TL1 macro). Lowering TL to 1 has the effect of 3857c478bd9Sstevel@tonic-gate * discarding the state pushed by trapstat. 3867c478bd9Sstevel@tonic-gate * 3877c478bd9Sstevel@tonic-gate * TLB Statistics: TLB Misses versus TSB Misses 3887c478bd9Sstevel@tonic-gate * 3897c478bd9Sstevel@tonic-gate * Distinguishing TLB misses from TSB misses requires further interposition 3907c478bd9Sstevel@tonic-gate * on the TLB miss handler: we cannot know a priori or a posteriori if a 3917c478bd9Sstevel@tonic-gate * given VA will or has hit in the TSB. 3927c478bd9Sstevel@tonic-gate * 3937c478bd9Sstevel@tonic-gate * We achieve this distinction by adding a second TLB return entry almost 3947c478bd9Sstevel@tonic-gate * identical to the first -- differing only in the address to which it 3957c478bd9Sstevel@tonic-gate * stores its results. We then modify the TLB miss handlers of the kernel 3967c478bd9Sstevel@tonic-gate * such that they check the %tpc when they determine that a TLB miss has 3977c478bd9Sstevel@tonic-gate * subsequently missed in the TSB: if the %tpc lies within trapstat's VA 3987c478bd9Sstevel@tonic-gate * range and TL is 2 (that is, if trapstat is running), the TLB miss handler 3997c478bd9Sstevel@tonic-gate * _increments_ the %tpc by the size of the TLB return entry. The ensuing 4007c478bd9Sstevel@tonic-gate * "retry" will thus transfer control to the second TLB return entry, and 4017c478bd9Sstevel@tonic-gate * the time spent in the handler will be accumulated in a memory location 4027c478bd9Sstevel@tonic-gate * specific to TSB misses. 4037c478bd9Sstevel@tonic-gate * 4047c478bd9Sstevel@tonic-gate * N.B.: To minimize the amount of knowledge the kernel must have of trapstat, 4057c478bd9Sstevel@tonic-gate * we do not allow the kernel to hard-code the size of the TLB return entry. 4067c478bd9Sstevel@tonic-gate * Rather, the actual tsbmiss handler executes a known instruction at the 4077c478bd9Sstevel@tonic-gate * corresponding tsbmiss patch points (see the tstat_tsbmiss_patch_table) with 4087c478bd9Sstevel@tonic-gate * the %tpc in %g7: when trapstat is not running, these points contain the 4097c478bd9Sstevel@tonic-gate * harmless TSTAT_TSBMISS_INSTR instruction ("add %g7, 0, %g7"). Before 4107c478bd9Sstevel@tonic-gate * running, trapstat modifies the instructions at these patch points such 4117c478bd9Sstevel@tonic-gate * that the simm13 equals the size of the TLB return entry. 4127c478bd9Sstevel@tonic-gate * 4137c478bd9Sstevel@tonic-gate * TLB Statistics: Kernel-level Misses versus User-level Misses 4147c478bd9Sstevel@tonic-gate * 4157c478bd9Sstevel@tonic-gate * Differentiating user-level misses from kernel-level misses employs a 4167c478bd9Sstevel@tonic-gate * similar technique, but is simplified by the ability to distinguish a 4177c478bd9Sstevel@tonic-gate * user-level miss from a kernel-level miss a priori by reading the context 4187c478bd9Sstevel@tonic-gate * register: we implement kernel-/user-level differentiation by again doubling 4197c478bd9Sstevel@tonic-gate * the number of TLB return entries, and setting the %tpc to the appropriate 4207c478bd9Sstevel@tonic-gate * TLB return entry in trapstat's TLB miss handler. Together with the doubling 4217c478bd9Sstevel@tonic-gate * of entries required for TLB-miss/TSB-miss differentiation, this yields a 4227c478bd9Sstevel@tonic-gate * total of four TLB return entries: 4237c478bd9Sstevel@tonic-gate * 4247c478bd9Sstevel@tonic-gate * Level TSB hit? Structure member 4257c478bd9Sstevel@tonic-gate * ------------------------------------------------------------ 4267c478bd9Sstevel@tonic-gate * Kernel Yes tstat_tlbret_t.ttlbr_ktlb 4277c478bd9Sstevel@tonic-gate * Kernel No tstat_tlbret_t.ttlbr_ktsb 4287c478bd9Sstevel@tonic-gate * User Yes tstat_tlbret_t.ttlbr_utlb 4297c478bd9Sstevel@tonic-gate * User No tstat_tlbret_t.ttlbr_utsb 4307c478bd9Sstevel@tonic-gate * 4317c478bd9Sstevel@tonic-gate * TLB Statistics: Misses per Pagesize 4327c478bd9Sstevel@tonic-gate * 4337c478bd9Sstevel@tonic-gate * As with the TLB-/TSB-miss differentiation, we have no way of determining 4347c478bd9Sstevel@tonic-gate * pagesize a priori. This is therefore implemented by mandating a new rule: 4357c478bd9Sstevel@tonic-gate * whenever the kernel fills the TLB in its TLB miss handler, the TTE 4367c478bd9Sstevel@tonic-gate * corresponding to the TLB-missing VA must be in %g5 when the handler 4377c478bd9Sstevel@tonic-gate * executes its "retry". This allows the TLB return entry to determine 4387c478bd9Sstevel@tonic-gate * pagesize by simply looking at the pagesize field in the TTE stored in 4397c478bd9Sstevel@tonic-gate * %g5. 4407c478bd9Sstevel@tonic-gate * 4417c478bd9Sstevel@tonic-gate * TLB Statistics: Probe Effect 4427c478bd9Sstevel@tonic-gate * 4437c478bd9Sstevel@tonic-gate * As one might imagine, gathering TLB statistics by pushing a trap level 4447c478bd9Sstevel@tonic-gate * induces significant probe effect. To account for this probe effect, 4457c478bd9Sstevel@tonic-gate * trapstat attempts to observe it by executing a code sequence with a known 4467c478bd9Sstevel@tonic-gate * number of TLB misses both before and after interposing on the trap table. 4477c478bd9Sstevel@tonic-gate * This allows trapstat to determine a per-trap probe effect which can then be 4487c478bd9Sstevel@tonic-gate * factored into the "%tim" fields of the trapstat command. 4497c478bd9Sstevel@tonic-gate * 4507c478bd9Sstevel@tonic-gate * Note that on sun4v platforms, TLB misses are normally handled by the 4517c478bd9Sstevel@tonic-gate * hypervisor or the hardware TSB walker. Thus no fast MMU miss information 452ce0352ebSgirish * is reported for normal operation. However, when trapstat is invoked 453ce0352ebSgirish * with -t or -T option to collect detailed TLB statistics, kernel takes 4547c478bd9Sstevel@tonic-gate * over TLB miss handling. This results in significantly more overhead 4557c478bd9Sstevel@tonic-gate * and TLB statistics may not be as accurate as on sun4u platforms. 456ce0352ebSgirish * On some processors, hypervisor or hardware may provide a low overhead 457ce0352ebSgirish * interface to collect TSB hit statistics. This support is exposed via 458ce0352ebSgirish * a well defined CPU module interface (cpu_trapstat_conf to enable this 459ce0352ebSgirish * interface and cpu_trapstat_data to get detailed TSB hit statistics). 460ce0352ebSgirish * In this scenario, TSB miss statistics is collected by intercepting the 461ce0352ebSgirish * IMMU_miss and DMMU_miss traps using above mentioned trap interposition 462ce0352ebSgirish * approach. 4637c478bd9Sstevel@tonic-gate * 4647c478bd9Sstevel@tonic-gate * Locking 4657c478bd9Sstevel@tonic-gate * 4667c478bd9Sstevel@tonic-gate * The implementation uses two locks: tstat_lock (a local lock) and the global 4677c478bd9Sstevel@tonic-gate * cpu_lock. tstat_lock is used to assure trapstat's consistency in the 4687c478bd9Sstevel@tonic-gate * presence of multithreaded /dev/trapstat consumers (while as of this writing 4697c478bd9Sstevel@tonic-gate * the only consumer of /dev/trapstat is single threaded, it is obviously 4707c478bd9Sstevel@tonic-gate * necessary to correctly support multithreaded access). cpu_lock is held 4717c478bd9Sstevel@tonic-gate * whenever CPUs are being manipulated directly, to prevent them from 4727c478bd9Sstevel@tonic-gate * disappearing in the process. Because trapstat's DR callback 4737c478bd9Sstevel@tonic-gate * (trapstat_cpu_setup()) must grab tstat_lock and is called with cpu_lock 4747c478bd9Sstevel@tonic-gate * held, the lock ordering is necessarily cpu_lock before tstat_lock. 4757c478bd9Sstevel@tonic-gate * 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate /* END CSTYLED */ 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate static dev_info_t *tstat_devi; /* saved in xxattach() for xxinfo() */ 4807c478bd9Sstevel@tonic-gate static int tstat_open; /* set if driver is open */ 4817c478bd9Sstevel@tonic-gate static kmutex_t tstat_lock; /* serialize access */ 4827c478bd9Sstevel@tonic-gate static vmem_t *tstat_arena; /* arena for TLB-locked pages */ 4837c478bd9Sstevel@tonic-gate static tstat_percpu_t *tstat_percpu; /* per-CPU data */ 4847c478bd9Sstevel@tonic-gate static int tstat_running; /* set if trapstat is running */ 4857c478bd9Sstevel@tonic-gate static tstat_data_t *tstat_buffer; /* staging buffer for outgoing data */ 4867c478bd9Sstevel@tonic-gate static int tstat_options; /* bit-wise indication of options */ 4877c478bd9Sstevel@tonic-gate static int *tstat_enabled; /* map of enabled trap entries */ 4887c478bd9Sstevel@tonic-gate static int tstat_tsbmiss_patched; /* tsbmiss patch flag */ 4897c478bd9Sstevel@tonic-gate static callb_id_t tstat_cprcb; /* CPR callback */ 4907c478bd9Sstevel@tonic-gate static char *tstat_probe_area; /* VA range used for probe effect */ 4917c478bd9Sstevel@tonic-gate static caddr_t tstat_probe_phys; /* physical to back above VA */ 4927c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_time; /* time spent on probe effect */ 4937c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_before[TSTAT_PROBE_NLAPS]; 4947c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_after[TSTAT_PROBE_NLAPS]; 4957c478bd9Sstevel@tonic-gate static uint_t tstat_pgszs; /* # of kernel page sizes */ 4967c478bd9Sstevel@tonic-gate static uint_t tstat_user_pgszs; /* # of user page sizes */ 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate * sizeof tstat_data_t + pgsz data for the kernel. For simplicity's sake, when 5007c478bd9Sstevel@tonic-gate * we collect data, we do it based upon szc, but when we report data back to 5017c478bd9Sstevel@tonic-gate * userland, we have to do it based upon the userszc which may not match. 5027c478bd9Sstevel@tonic-gate * So, these two variables are for internal use and exported use respectively. 5037c478bd9Sstevel@tonic-gate */ 5047c478bd9Sstevel@tonic-gate static size_t tstat_data_t_size; 5057c478bd9Sstevel@tonic-gate static size_t tstat_data_t_exported_size; 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate static size_t tstat_data_pages; /* number of pages of tstat data */ 5087c478bd9Sstevel@tonic-gate static size_t tstat_data_size; /* tstat data size in bytes */ 5097c478bd9Sstevel@tonic-gate static size_t tstat_total_pages; /* #data pages + #instr pages */ 5107c478bd9Sstevel@tonic-gate static size_t tstat_total_size; /* tstat data size + instr size */ 5117c478bd9Sstevel@tonic-gate #ifdef sun4v 5127c478bd9Sstevel@tonic-gate static caddr_t tstat_va; /* VA of memory reserved for TBA */ 5137c478bd9Sstevel@tonic-gate static pfn_t tstat_pfn; /* PFN of memory reserved for TBA */ 514ce0352ebSgirish static boolean_t tstat_fast_tlbstat = B_FALSE; 5157c478bd9Sstevel@tonic-gate #endif 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* 5187c478bd9Sstevel@tonic-gate * In the above block comment, see "TLB Statistics: TLB Misses versus 5197c478bd9Sstevel@tonic-gate * TSB Misses" for an explanation of the tsbmiss patch points. 5207c478bd9Sstevel@tonic-gate */ 5217c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point; 5227c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point_kpm; 5237c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point_kpm_small; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate /* 5267c478bd9Sstevel@tonic-gate * Trapstat tsbmiss patch table 5277c478bd9Sstevel@tonic-gate */ 5287c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t tstat_tsbmiss_patch_table[] = { 5297c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point, 0}, 5307c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point_kpm, 0}, 5317c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point_kpm_small, 0}, 5327c478bd9Sstevel@tonic-gate {(uint32_t *)NULL, 0} 5337c478bd9Sstevel@tonic-gate }; 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate /* 5367c478bd9Sstevel@tonic-gate * We define some general SPARC-specific constants to allow more readable 5377c478bd9Sstevel@tonic-gate * relocations. 5387c478bd9Sstevel@tonic-gate */ 5397c478bd9Sstevel@tonic-gate #define NOP 0x01000000 5407c478bd9Sstevel@tonic-gate #define HI22(v) ((uint32_t)(v) >> 10) 5417c478bd9Sstevel@tonic-gate #define LO10(v) ((uint32_t)(v) & 0x3ff) 5427c478bd9Sstevel@tonic-gate #define LO12(v) ((uint32_t)(v) & 0xfff) 5437c478bd9Sstevel@tonic-gate #define DISP22(from, to) \ 5447c478bd9Sstevel@tonic-gate ((((uintptr_t)(to) - (uintptr_t)(from)) >> 2) & 0x3fffff) 5457c478bd9Sstevel@tonic-gate #define ASI(asi) ((asi) << 5) 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate /* 5487c478bd9Sstevel@tonic-gate * The interposing trap table must be locked in the I-TLB, and any data 5497c478bd9Sstevel@tonic-gate * referred to in the interposing trap handler must be locked in the D-TLB. 5507c478bd9Sstevel@tonic-gate * This function locks these pages in the appropriate TLBs by creating TTEs 5517c478bd9Sstevel@tonic-gate * from whole cloth, and manually loading them into the TLB. This function is 5527c478bd9Sstevel@tonic-gate * called from cross call context. 5537c478bd9Sstevel@tonic-gate * 5547c478bd9Sstevel@tonic-gate * On sun4v platforms, we use 4M page size mappings to minimize the number 5557c478bd9Sstevel@tonic-gate * of locked down entries (i.e. permanent mappings). Each CPU uses a 5567c478bd9Sstevel@tonic-gate * reserved portion of that 4M page for its TBA and data. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate static void 5597c478bd9Sstevel@tonic-gate trapstat_load_tlb(void) 5607c478bd9Sstevel@tonic-gate { 5617c478bd9Sstevel@tonic-gate #ifndef sun4v 5627c478bd9Sstevel@tonic-gate int i; 5637c478bd9Sstevel@tonic-gate #else 5647c478bd9Sstevel@tonic-gate uint64_t ret; 5657c478bd9Sstevel@tonic-gate #endif 5667c478bd9Sstevel@tonic-gate tte_t tte; 5677c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 5687c478bd9Sstevel@tonic-gate caddr_t va = tcpu->tcpu_vabase; 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 5717c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate #ifndef sun4v 5747c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) { 5757c478bd9Sstevel@tonic-gate tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(TTE8K) | 5767c478bd9Sstevel@tonic-gate TTE_PFN_INTHI(tcpu->tcpu_pfn[i]); 5777c478bd9Sstevel@tonic-gate if (i < TSTAT_INSTR_PAGES) { 5787c478bd9Sstevel@tonic-gate tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) | 5797c478bd9Sstevel@tonic-gate TTE_LCK_INT | TTE_CP_INT | TTE_PRIV_INT; 5807c478bd9Sstevel@tonic-gate sfmmu_itlb_ld(va, KCONTEXT, &tte); 5817c478bd9Sstevel@tonic-gate } else { 5827c478bd9Sstevel@tonic-gate tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) | 5837c478bd9Sstevel@tonic-gate TTE_LCK_INT | TTE_CP_INT | TTE_CV_INT | 5847c478bd9Sstevel@tonic-gate TTE_PRIV_INT | TTE_HWWR_INT; 5857c478bd9Sstevel@tonic-gate sfmmu_dtlb_ld(va, KCONTEXT, &tte); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate #else /* sun4v */ 5897c478bd9Sstevel@tonic-gate tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(tstat_pfn); 5907c478bd9Sstevel@tonic-gate tte.tte_intlo = TTE_PFN_INTLO(tstat_pfn) | TTE_CP_INT | 5917c478bd9Sstevel@tonic-gate TTE_CV_INT | TTE_PRIV_INT | TTE_HWWR_INT | 5927c478bd9Sstevel@tonic-gate TTE_SZ_INTLO(TTE4M); 5937c478bd9Sstevel@tonic-gate ret = hv_mmu_map_perm_addr(va, KCONTEXT, *(uint64_t *)&tte, 5947c478bd9Sstevel@tonic-gate MAP_ITLB | MAP_DTLB); 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate if (ret != H_EOK) 5977c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "trapstat: cannot map new TBA " 5987c478bd9Sstevel@tonic-gate "for cpu %d (error: 0x%lx)", CPU->cpu_id, ret); 5997c478bd9Sstevel@tonic-gate #endif /* sun4v */ 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate /* 6037c478bd9Sstevel@tonic-gate * As mentioned in the "TLB Statistics: TLB Misses versus TSB Misses" section 6047c478bd9Sstevel@tonic-gate * of the block comment, TLB misses are differentiated from TSB misses in 6057c478bd9Sstevel@tonic-gate * part by hot-patching the instructions at the tsbmiss patch points (see 6067c478bd9Sstevel@tonic-gate * tstat_tsbmiss_patch_table). This routine is used both to initially patch 6077c478bd9Sstevel@tonic-gate * the instructions, and to patch them back to their original values upon 6087c478bd9Sstevel@tonic-gate * restoring the original trap table. 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate static void 6117c478bd9Sstevel@tonic-gate trapstat_hotpatch() 6127c478bd9Sstevel@tonic-gate { 6137c478bd9Sstevel@tonic-gate uint32_t instr; 6147c478bd9Sstevel@tonic-gate uint32_t simm13; 6157c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t *ep; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_TLBDATA)) 6207c478bd9Sstevel@tonic-gate return; 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate if (!tstat_tsbmiss_patched) { 6237c478bd9Sstevel@tonic-gate /* 6247c478bd9Sstevel@tonic-gate * We haven't patched the TSB paths; do so now. 6257c478bd9Sstevel@tonic-gate */ 6267c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 6277c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_tlbret_t, ttlbr_ktsb) - 6287c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb) == 6297c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_utsb) - 6307c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_utlb)); 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate simm13 = offsetof(tstat_tlbret_t, ttlbr_ktsb) - 6337c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb); 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) { 6367c478bd9Sstevel@tonic-gate ASSERT(ep->tpe_instr == 0); 6377c478bd9Sstevel@tonic-gate instr = ep->tpe_instr = *ep->tpe_addr; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate /* 6407c478bd9Sstevel@tonic-gate * Assert that the instruction we're about to patch is 6417c478bd9Sstevel@tonic-gate * "add %g7, 0, %g7" (0x8e01e000). 6427c478bd9Sstevel@tonic-gate */ 6437c478bd9Sstevel@tonic-gate ASSERT(instr == TSTAT_TSBMISS_INSTR); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate instr |= simm13; 6467c478bd9Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)ep->tpe_addr, 6477c478bd9Sstevel@tonic-gate instr, sizeof (instr)); 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate tstat_tsbmiss_patched = 1; 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate } else { 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * Remove patches from the TSB paths. 6557c478bd9Sstevel@tonic-gate */ 6567c478bd9Sstevel@tonic-gate for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) { 6577c478bd9Sstevel@tonic-gate ASSERT(ep->tpe_instr == TSTAT_TSBMISS_INSTR); 6587c478bd9Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)ep->tpe_addr, 6597c478bd9Sstevel@tonic-gate ep->tpe_instr, sizeof (instr)); 6607c478bd9Sstevel@tonic-gate ep->tpe_instr = 0; 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate tstat_tsbmiss_patched = 0; 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate /* 6687c478bd9Sstevel@tonic-gate * This is the routine executed to clock the performance of the trap table, 6697c478bd9Sstevel@tonic-gate * executed both before and after interposing on the trap table to attempt to 6707c478bd9Sstevel@tonic-gate * determine probe effect. The probe effect is used to adjust the "%tim" 6717c478bd9Sstevel@tonic-gate * fields of trapstat's -t and -T output; we only use TLB misses to clock the 6727c478bd9Sstevel@tonic-gate * trap table. We execute the inner loop (which is designed to exceed the 6737c478bd9Sstevel@tonic-gate * TLB's reach) nlaps times, taking the best time as our time (thereby 6747c478bd9Sstevel@tonic-gate * factoring out the effects of interrupts, cache misses or other perturbing 6757c478bd9Sstevel@tonic-gate * events. 6767c478bd9Sstevel@tonic-gate */ 6777c478bd9Sstevel@tonic-gate static hrtime_t 6787c478bd9Sstevel@tonic-gate trapstat_probe_laps(int nlaps, hrtime_t *buf) 6797c478bd9Sstevel@tonic-gate { 6807c478bd9Sstevel@tonic-gate int i, j = 0; 6817c478bd9Sstevel@tonic-gate hrtime_t ts, best = INT64_MAX; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate while (nlaps--) { 6847c478bd9Sstevel@tonic-gate ts = rdtick(); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_SIZE; i += MMU_PAGESIZE) 6877c478bd9Sstevel@tonic-gate *((volatile char *)&tstat_probe_area[i]); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate if ((ts = rdtick() - ts) < best) 6907c478bd9Sstevel@tonic-gate best = ts; 6917c478bd9Sstevel@tonic-gate buf[j++] = ts; 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate return (best); 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate /* 6987c478bd9Sstevel@tonic-gate * This routine determines the probe effect by calling trapstat_probe_laps() 6997c478bd9Sstevel@tonic-gate * both without and with the interposing trap table. Note that this is 7007c478bd9Sstevel@tonic-gate * called from a cross call on the desired CPU, and that it is called on 7017c478bd9Sstevel@tonic-gate * every CPU (this is necessary because the probe effect may differ from 7027c478bd9Sstevel@tonic-gate * one CPU to another). 7037c478bd9Sstevel@tonic-gate */ 7047c478bd9Sstevel@tonic-gate static void 7057c478bd9Sstevel@tonic-gate trapstat_probe() 7067c478bd9Sstevel@tonic-gate { 7077c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 7087c478bd9Sstevel@tonic-gate hrtime_t before, after; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED)) 7117c478bd9Sstevel@tonic-gate return; 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate if (tstat_probe_area == NULL || (tstat_options & TSTAT_OPT_NOGO)) 7147c478bd9Sstevel@tonic-gate return; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * We very much expect the %tba to be KERNELBASE; this is a 7187c478bd9Sstevel@tonic-gate * precautionary measure to assure that trapstat doesn't melt the 7197c478bd9Sstevel@tonic-gate * machine should the %tba point unexpectedly elsewhere. 7207c478bd9Sstevel@tonic-gate */ 7217c478bd9Sstevel@tonic-gate if (get_tba() != (caddr_t)KERNELBASE) 7227c478bd9Sstevel@tonic-gate return; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate /* 7257c478bd9Sstevel@tonic-gate * Preserve this CPU's data before destroying it by enabling the 7267c478bd9Sstevel@tonic-gate * interposing trap table. We can safely use tstat_buffer because 7277c478bd9Sstevel@tonic-gate * the caller of the trapstat_probe() cross call is holding tstat_lock. 7287c478bd9Sstevel@tonic-gate */ 7297c478bd9Sstevel@tonic-gate bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size); 7307c478bd9Sstevel@tonic-gate 7317c478bd9Sstevel@tonic-gate tstat_probe_time = gethrtime(); 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate before = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_before); 7347c478bd9Sstevel@tonic-gate (void) set_tba(tcpu->tcpu_ibase); 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate after = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_after); 7377c478bd9Sstevel@tonic-gate (void) set_tba((caddr_t)KERNELBASE); 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate tstat_probe_time = gethrtime() - tstat_probe_time; 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size); 7427c478bd9Sstevel@tonic-gate tcpu->tcpu_data->tdata_peffect = (after - before) / TSTAT_PROBE_NPAGES; 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate static void 7467c478bd9Sstevel@tonic-gate trapstat_probe_alloc() 7477c478bd9Sstevel@tonic-gate { 7487c478bd9Sstevel@tonic-gate pfn_t pfn; 7497c478bd9Sstevel@tonic-gate caddr_t va; 7507c478bd9Sstevel@tonic-gate int i; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 7537c478bd9Sstevel@tonic-gate ASSERT(tstat_probe_area == NULL); 7547c478bd9Sstevel@tonic-gate ASSERT(tstat_probe_phys == NULL); 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_TLBDATA)) 7577c478bd9Sstevel@tonic-gate return; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* 7607c478bd9Sstevel@tonic-gate * Grab some virtual from the heap arena. 7617c478bd9Sstevel@tonic-gate */ 7627c478bd9Sstevel@tonic-gate tstat_probe_area = vmem_alloc(heap_arena, TSTAT_PROBE_SIZE, VM_SLEEP); 7637c478bd9Sstevel@tonic-gate va = tstat_probe_area; 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate /* 7667c478bd9Sstevel@tonic-gate * Grab a single physical page. 7677c478bd9Sstevel@tonic-gate */ 7687c478bd9Sstevel@tonic-gate tstat_probe_phys = vmem_alloc(tstat_arena, MMU_PAGESIZE, VM_SLEEP); 7697c478bd9Sstevel@tonic-gate pfn = hat_getpfnum(kas.a_hat, tstat_probe_phys); 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * Now set the translation for every page in our virtual range 7737c478bd9Sstevel@tonic-gate * to be our allocated physical page. 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_NPAGES; i++) { 7767c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, va, MMU_PAGESIZE, pfn, PROT_READ, 7777c478bd9Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 7787c478bd9Sstevel@tonic-gate va += MMU_PAGESIZE; 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate static void 7837c478bd9Sstevel@tonic-gate trapstat_probe_free() 7847c478bd9Sstevel@tonic-gate { 7857c478bd9Sstevel@tonic-gate caddr_t va; 7867c478bd9Sstevel@tonic-gate int i; 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate if ((va = tstat_probe_area) == NULL) 7917c478bd9Sstevel@tonic-gate return; 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_NPAGES; i++) { 7947c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, va, MMU_PAGESIZE, HAT_UNLOAD_UNLOCK); 7957c478bd9Sstevel@tonic-gate va += MMU_PAGESIZE; 7967c478bd9Sstevel@tonic-gate } 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tstat_probe_phys, MMU_PAGESIZE); 7997c478bd9Sstevel@tonic-gate vmem_free(heap_arena, tstat_probe_area, TSTAT_PROBE_SIZE); 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate tstat_probe_phys = NULL; 8027c478bd9Sstevel@tonic-gate tstat_probe_area = NULL; 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate /* 8067c478bd9Sstevel@tonic-gate * This routine actually enables a CPU by setting its %tba to be the 8077c478bd9Sstevel@tonic-gate * CPU's interposing trap table. It is called out of cross call context. 8087c478bd9Sstevel@tonic-gate */ 8097c478bd9Sstevel@tonic-gate static void 8107c478bd9Sstevel@tonic-gate trapstat_enable() 8117c478bd9Sstevel@tonic-gate { 8127c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED)) 8157c478bd9Sstevel@tonic-gate return; 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 8187c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate if (get_tba() != (caddr_t)KERNELBASE) 8217c478bd9Sstevel@tonic-gate return; 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_NOGO)) 8247c478bd9Sstevel@tonic-gate (void) set_tba(tcpu->tcpu_ibase); 8257c478bd9Sstevel@tonic-gate tcpu->tcpu_flags |= TSTAT_CPU_ENABLED; 8267c478bd9Sstevel@tonic-gate #ifdef sun4v 827ce0352ebSgirish if ((tstat_options & TSTAT_OPT_TLBDATA) && 828ce0352ebSgirish !(tstat_options & TSTAT_OPT_NOGO)) { 829ce0352ebSgirish if (tstat_fast_tlbstat) { 8307c478bd9Sstevel@tonic-gate /* 831ce0352ebSgirish * Invoke processor specific interface to enable 832ce0352ebSgirish * collection of TSB hit statistics. 833ce0352ebSgirish */ 834ce0352ebSgirish cpu_trapstat_conf(CPU_TSTATCONF_ENABLE); 835ce0352ebSgirish } else { 836ce0352ebSgirish /* 837ce0352ebSgirish * Collect TLB miss statistics by taking over 838ce0352ebSgirish * TLB miss handling from the hypervisor. This 839ce0352ebSgirish * is done by telling the hypervisor that there 840ce0352ebSgirish * is no TSB configured. Also set TSTAT_TLB_STATS 841ce0352ebSgirish * flag so that no user TSB is configured during 842ce0352ebSgirish * context switch time. 8437c478bd9Sstevel@tonic-gate */ 8447c478bd9Sstevel@tonic-gate cpu_t *cp = CPU; 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate cp->cpu_m.cpu_tstat_flags |= TSTAT_TLB_STATS; 8477c478bd9Sstevel@tonic-gate (void) hv_set_ctx0(NULL, NULL); 8487c478bd9Sstevel@tonic-gate (void) hv_set_ctxnon0(NULL, NULL); 8497c478bd9Sstevel@tonic-gate } 850ce0352ebSgirish } 8517c478bd9Sstevel@tonic-gate #endif 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate /* 8557c478bd9Sstevel@tonic-gate * This routine disables a CPU (vis a vis trapstat) by setting its %tba to be 8567c478bd9Sstevel@tonic-gate * the actual, underlying trap table. It is called out of cross call context. 8577c478bd9Sstevel@tonic-gate */ 8587c478bd9Sstevel@tonic-gate static void 8597c478bd9Sstevel@tonic-gate trapstat_disable() 8607c478bd9Sstevel@tonic-gate { 8617c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 8647c478bd9Sstevel@tonic-gate return; 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 8677c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_NOGO)) 8707c478bd9Sstevel@tonic-gate (void) set_tba((caddr_t)KERNELBASE); 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED; 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate #ifdef sun4v 875ce0352ebSgirish if ((tstat_options & TSTAT_OPT_TLBDATA) && 876ce0352ebSgirish !(tstat_options & TSTAT_OPT_NOGO)) { 877ce0352ebSgirish if (tstat_fast_tlbstat) { 8787c478bd9Sstevel@tonic-gate /* 879ce0352ebSgirish * Invoke processor specific interface to disable 880ce0352ebSgirish * collection of TSB hit statistics on each processor. 881ce0352ebSgirish */ 882ce0352ebSgirish cpu_trapstat_conf(CPU_TSTATCONF_DISABLE); 883ce0352ebSgirish } else { 884ce0352ebSgirish /* 885ce0352ebSgirish * As part of collecting TLB miss statistics, we took 886ce0352ebSgirish * over TLB miss handling from the hypervisor by 887ce0352ebSgirish * telling the hypervisor that NO TSB is configured. 888ce0352ebSgirish * We need to restore that by communicating proper 889ce0352ebSgirish * kernel/user TSB information so that TLB misses 890ce0352ebSgirish * can be handled by the hypervisor or the hardware 891ce0352ebSgirish * more efficiently. 8927c478bd9Sstevel@tonic-gate * 893ce0352ebSgirish * We restore kernel TSB information right away. 894ce0352ebSgirish * However, to minimize any locking dependency, we 895ce0352ebSgirish * don't restore user TSB information right away. 896ce0352ebSgirish * Instead, we simply clear the TSTAT_TLB_STATS flag 897ce0352ebSgirish * so that the user TSB information is automatically 898ce0352ebSgirish * restored on next context switch. 8997c478bd9Sstevel@tonic-gate * 9007c478bd9Sstevel@tonic-gate * Note that the call to restore kernel TSB information 9017c478bd9Sstevel@tonic-gate * will normally not fail, unless wrong information is 9027c478bd9Sstevel@tonic-gate * passed here. In that scenario, system will still 9037c478bd9Sstevel@tonic-gate * continue to function properly with the exception of 9047c478bd9Sstevel@tonic-gate * kernel handling all the TLB misses. 9057c478bd9Sstevel@tonic-gate */ 9067c478bd9Sstevel@tonic-gate struct hv_tsb_block *hvbp = &ksfmmup->sfmmu_hvblock; 9077c478bd9Sstevel@tonic-gate cpu_t *cp = CPU; 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate cp->cpu_m.cpu_tstat_flags &= ~TSTAT_TLB_STATS; 910ce0352ebSgirish (void) hv_set_ctx0(hvbp->hv_tsb_info_cnt, 911ce0352ebSgirish hvbp->hv_tsb_info_pa); 912ce0352ebSgirish } 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate #endif 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * We use %tick as the time base when recording the time spent executing 9197c478bd9Sstevel@tonic-gate * the trap handler. %tick, however, is not necessarily kept in sync 9207c478bd9Sstevel@tonic-gate * across CPUs (indeed, different CPUs may have different %tick frequencies). 9217c478bd9Sstevel@tonic-gate * We therefore cross call onto a CPU to get a snapshot of its data to 9227c478bd9Sstevel@tonic-gate * copy out; this is the routine executed out of that cross call. 9237c478bd9Sstevel@tonic-gate */ 9247c478bd9Sstevel@tonic-gate static void 9257c478bd9Sstevel@tonic-gate trapstat_snapshot() 9267c478bd9Sstevel@tonic-gate { 9277c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 9287c478bd9Sstevel@tonic-gate tstat_data_t *data = tcpu->tcpu_data; 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 9317c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 9327c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ENABLED); 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate data->tdata_snapts = gethrtime(); 9357c478bd9Sstevel@tonic-gate data->tdata_snaptick = rdtick(); 9367c478bd9Sstevel@tonic-gate bcopy(data, tstat_buffer, tstat_data_t_size); 937ce0352ebSgirish #ifdef sun4v 938ce0352ebSgirish /* 939ce0352ebSgirish * Invoke processor specific interface to collect TSB hit 940ce0352ebSgirish * statistics on each processor. 941ce0352ebSgirish */ 942ce0352ebSgirish if ((tstat_options & TSTAT_OPT_TLBDATA) && tstat_fast_tlbstat) 943ce0352ebSgirish cpu_trapstat_data((void *) tstat_buffer->tdata_pgsz, 944ce0352ebSgirish tstat_pgszs); 945ce0352ebSgirish #endif 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate /* 9497c478bd9Sstevel@tonic-gate * The TSTAT_RETENT_* constants define offsets in the TLB return entry. 9507c478bd9Sstevel@tonic-gate * They are used only in trapstat_tlbretent() (below) and #undef'd 9517c478bd9Sstevel@tonic-gate * immediately afterwards. Any change to "retent" in trapstat_tlbretent() 9527c478bd9Sstevel@tonic-gate * will likely require changes to these constants. 9537c478bd9Sstevel@tonic-gate */ 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate #ifndef sun4v 9567c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATHI 1 9577c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATLO 2 9581bd453f3Ssusans #define TSTAT_RETENT_SHIFT 11 9591bd453f3Ssusans #define TSTAT_RETENT_COUNT_LD 13 9601bd453f3Ssusans #define TSTAT_RETENT_COUNT_ST 15 9611bd453f3Ssusans #define TSTAT_RETENT_TMPTSHI 16 9621bd453f3Ssusans #define TSTAT_RETENT_TMPTSLO 17 9631bd453f3Ssusans #define TSTAT_RETENT_TIME_LD 19 9641bd453f3Ssusans #define TSTAT_RETENT_TIME_ST 21 9657c478bd9Sstevel@tonic-gate #else /* sun4v */ 9667c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATHI 1 9677c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATLO 2 9687c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_SHIFT 5 9697c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_COUNT_LD 7 9707c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_COUNT_ST 9 9717c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_TMPTSHI 10 9727c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_TMPTSLO 11 9737c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_TIME_LD 13 9747c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_TIME_ST 15 9757c478bd9Sstevel@tonic-gate #endif /* sun4v */ 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate static void 9787c478bd9Sstevel@tonic-gate trapstat_tlbretent(tstat_percpu_t *tcpu, tstat_tlbretent_t *ret, 9797c478bd9Sstevel@tonic-gate tstat_missdata_t *data) 9807c478bd9Sstevel@tonic-gate { 9817c478bd9Sstevel@tonic-gate uint32_t *ent = ret->ttlbrent_instr, shift; 9827c478bd9Sstevel@tonic-gate uintptr_t base, tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick); 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate /* 9857c478bd9Sstevel@tonic-gate * This is the entry executed upon return from the TLB/TSB miss 9867c478bd9Sstevel@tonic-gate * handler (i.e. the code interpositioned between the "retry" and 9877c478bd9Sstevel@tonic-gate * the actual return to the TLB-missing instruction). Detail on its 9887c478bd9Sstevel@tonic-gate * theory of operation can be found in the "TLB Statistics" section 9897c478bd9Sstevel@tonic-gate * of the block comment. Note that we expect the TTE just loaded 9907c478bd9Sstevel@tonic-gate * into the TLB to be in %g5; all other globals are available as 9917c478bd9Sstevel@tonic-gate * scratch. Finally, note that the page size information in sun4v is 9927c478bd9Sstevel@tonic-gate * located in the lower bits of the TTE -- requiring us to have a 9937c478bd9Sstevel@tonic-gate * different return entry on sun4v. 9947c478bd9Sstevel@tonic-gate */ 9957c478bd9Sstevel@tonic-gate static const uint32_t retent[TSTAT_TLBRET_NINSTR] = { 9967c478bd9Sstevel@tonic-gate #ifndef sun4v 9977c478bd9Sstevel@tonic-gate 0x87410000, /* rd %tick, %g3 */ 9987c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 9997c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(stat), %g1 */ 10007c478bd9Sstevel@tonic-gate 0x89297001, /* sllx %g5, 1, %g4 */ 10017c478bd9Sstevel@tonic-gate 0x8931303e, /* srlx %g4, 62, %g4 */ 10027c478bd9Sstevel@tonic-gate 0x8531702e, /* srlx %g5, 46, %g2 */ 10037c478bd9Sstevel@tonic-gate 0x8408a004, /* and %g2, 4, %g2 */ 10047c478bd9Sstevel@tonic-gate 0x88110002, /* or %g4, %g2, %g4 */ 10051bd453f3Ssusans 0x80a12005, /* cmp %g4, 5 */ 10061bd453f3Ssusans 0x34400002, /* bg,a,pn %icc, +8 */ 10071bd453f3Ssusans 0x88102004, /* mov 4, %g4 */ 10087c478bd9Sstevel@tonic-gate 0x89292000, /* sll %g4, shift, %g4 */ 10097c478bd9Sstevel@tonic-gate 0x82004004, /* add %g1, %g4, %g1 */ 10107c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_count], %g2 */ 10117c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 10127c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_count] */ 10137c478bd9Sstevel@tonic-gate 0x0d000000, /* sethi %hi(tdata_tmptick), %g6 */ 10147c478bd9Sstevel@tonic-gate 0xc459a000, /* ldx [%g6 + %lo(tdata_tmptick)], %g2 */ 10157c478bd9Sstevel@tonic-gate 0x8620c002, /* sub %g3, %g2, %g3 */ 10167c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_time], %g2 */ 10177c478bd9Sstevel@tonic-gate 0x84008003, /* add %g2, %g3, %g2 */ 10187c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_time] */ 10197c478bd9Sstevel@tonic-gate 0x83f00000 /* retry */ 10207c478bd9Sstevel@tonic-gate #else /* sun4v */ 10217c478bd9Sstevel@tonic-gate 0x87410000, /* rd %tick, %g3 */ 10227c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 10237c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(stat), %g1 */ 10247c478bd9Sstevel@tonic-gate 0x8929703d, /* sllx %g5, 61, %g4 */ 10257c478bd9Sstevel@tonic-gate 0x8931303d, /* srlx %g4, 61, %g4 */ 10267c478bd9Sstevel@tonic-gate 0x89292000, /* sll %g4, shift, %g4 */ 10277c478bd9Sstevel@tonic-gate 0x82004004, /* add %g1, %g4, %g1 */ 10287c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_count], %g2 */ 10297c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 10307c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_count] */ 10317c478bd9Sstevel@tonic-gate 0x0d000000, /* sethi %hi(tdata_tmptick), %g6 */ 10327c478bd9Sstevel@tonic-gate 0xc459a000, /* ldx [%g6 + %lo(tdata_tmptick)], %g2 */ 10337c478bd9Sstevel@tonic-gate 0x8620c002, /* sub %g3, %g2, %g3 */ 10347c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_time], %g2 */ 10357c478bd9Sstevel@tonic-gate 0x84008003, /* add %g2, %g3, %g2 */ 10367c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_time] */ 10377c478bd9Sstevel@tonic-gate 0x83f00000 /* retry */ 10387c478bd9Sstevel@tonic-gate #endif /* sun4v */ 10397c478bd9Sstevel@tonic-gate }; 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 10427c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 10437c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_missdata_t, tmiss_count) <= LO10(-1)); 10447c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 10457c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_missdata_t, tmiss_time) <= LO10(-1)); 10467c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 10477c478bd9Sstevel@tonic-gate ASSERT(!((sizeof (tstat_pgszdata_t) - 1) & sizeof (tstat_pgszdata_t))); 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate for (shift = 1; (1 << shift) != sizeof (tstat_pgszdata_t); shift++) 10507c478bd9Sstevel@tonic-gate continue; 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate base = (uintptr_t)tcpu->tcpu_dbase + 10537c478bd9Sstevel@tonic-gate ((uintptr_t)data - (uintptr_t)tcpu->tcpu_data); 10547c478bd9Sstevel@tonic-gate 10557c478bd9Sstevel@tonic-gate bcopy(retent, ent, sizeof (retent)); 10567c478bd9Sstevel@tonic-gate 10577c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_STATHI] |= HI22(base); 10587c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_STATLO] |= LO10(base); 10597c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_SHIFT] |= shift; 10607c478bd9Sstevel@tonic-gate /* LINTED E_EXPR_NULL_EFFECT */ 10617c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_COUNT_LD] |= offsetof(tstat_missdata_t, tmiss_count); 10627c478bd9Sstevel@tonic-gate /* LINTED E_EXPR_NULL_EFFECT */ 10637c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_COUNT_ST] |= offsetof(tstat_missdata_t, tmiss_count); 10647c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TMPTSHI] |= HI22(tmptick); 10657c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TMPTSLO] |= LO10(tmptick); 10667c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TIME_LD] |= offsetof(tstat_missdata_t, tmiss_time); 10677c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TIME_ST] |= offsetof(tstat_missdata_t, tmiss_time); 10687c478bd9Sstevel@tonic-gate } 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATHI 10717c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATLO 10727c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_SHIFT 10737c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_LD 10747c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_ST 10757c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSHI 10767c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSLO 10777c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_LD 10787c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_ST 10797c478bd9Sstevel@tonic-gate 10807c478bd9Sstevel@tonic-gate /* 10817c478bd9Sstevel@tonic-gate * The TSTAT_TLBENT_* constants define offsets in the TLB entry. They are 10827c478bd9Sstevel@tonic-gate * used only in trapstat_tlbent() (below) and #undef'd immediately afterwards. 10837c478bd9Sstevel@tonic-gate * Any change to "tlbent" in trapstat_tlbent() will likely require changes 10847c478bd9Sstevel@tonic-gate * to these constants. 10857c478bd9Sstevel@tonic-gate */ 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate #ifndef sun4v 10887c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATHI 0 10897c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_LD 1 10907c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_ST 3 10917c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_MMUASI 15 10927c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCHI 18 10937c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_USER 19 10947c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_KERN 21 10957c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSHI 25 10967c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSLO 27 10977c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_BA 28 10987c478bd9Sstevel@tonic-gate #else /* sun4v */ 10997c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATHI 0 11007c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_LD 1 11017c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_ST 3 11027c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TAGTARGET 19 11037c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCHI 21 11047c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_USER 22 11057c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_KERN 24 11067c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSHI 28 11077c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSLO 30 11087c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_BA 31 11097c478bd9Sstevel@tonic-gate #endif /* sun4v */ 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate static void 11127c478bd9Sstevel@tonic-gate trapstat_tlbent(tstat_percpu_t *tcpu, int entno) 11137c478bd9Sstevel@tonic-gate { 11147c478bd9Sstevel@tonic-gate uint32_t *ent; 11157c478bd9Sstevel@tonic-gate uintptr_t orig, va, baoffs; 1116ce0352ebSgirish #ifndef sun4v 11177c478bd9Sstevel@tonic-gate int itlb = entno == TSTAT_ENT_ITLBMISS; 1118ce0352ebSgirish #else 1119ce0352ebSgirish int itlb = (entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_ITLBMISS); 1120ce0352ebSgirish #endif 11217c478bd9Sstevel@tonic-gate int entoffs = entno << TSTAT_ENT_SHIFT; 11227c478bd9Sstevel@tonic-gate uintptr_t tmptick, stat, tpc, utpc; 11237c478bd9Sstevel@tonic-gate tstat_pgszdata_t *data = &tcpu->tcpu_data->tdata_pgsz[0]; 11247c478bd9Sstevel@tonic-gate tstat_tlbdata_t *udata, *kdata; 11257c478bd9Sstevel@tonic-gate tstat_tlbret_t *ret; 11267c478bd9Sstevel@tonic-gate #ifndef sun4v 11277c478bd9Sstevel@tonic-gate uint32_t asi = itlb ? ASI(ASI_IMMU) : ASI(ASI_DMMU); 11287c478bd9Sstevel@tonic-gate #else 11297c478bd9Sstevel@tonic-gate uint32_t tagtarget_off = itlb ? MMFSA_I_CTX : MMFSA_D_CTX; 11307c478bd9Sstevel@tonic-gate #endif 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate /* 11337c478bd9Sstevel@tonic-gate * When trapstat is run with TLB statistics, this is the entry for 11347c478bd9Sstevel@tonic-gate * both I- and D-TLB misses; this code performs trap level pushing, 11357c478bd9Sstevel@tonic-gate * as described in the "TLB Statistics" section of the block comment. 11367c478bd9Sstevel@tonic-gate * This code is executing at TL 1; %tstate[0] contains the saved 11377c478bd9Sstevel@tonic-gate * state at the time of the TLB miss. Pushing trap level 1 (and thus 11387c478bd9Sstevel@tonic-gate * raising TL to 2) requires us to fill in %tstate[1] with our %pstate, 11397c478bd9Sstevel@tonic-gate * %cwp and %asi. We leave %tt unchanged, and we set %tpc and %tnpc to 11407c478bd9Sstevel@tonic-gate * the appropriate TLB return entry (based on the context of the miss). 11417c478bd9Sstevel@tonic-gate * Finally, we sample %tick, and stash it in the tdata_tmptick member 11427c478bd9Sstevel@tonic-gate * the per-CPU tstat_data structure. tdata_tmptick will be used in 11437c478bd9Sstevel@tonic-gate * the TLB return entry to determine the amount of time spent in the 11447c478bd9Sstevel@tonic-gate * TLB miss handler. 11457c478bd9Sstevel@tonic-gate * 1146bd46b14cSgirish * Note that on sun4v platforms, we must obtain the context information 1147bd46b14cSgirish * from the MMU fault status area. (The base address of this MMU fault 1148bd46b14cSgirish * status area is kept in the scratchpad register 0.) 11497c478bd9Sstevel@tonic-gate */ 11507c478bd9Sstevel@tonic-gate static const uint32_t tlbent[] = { 11517c478bd9Sstevel@tonic-gate #ifndef sun4v 11527c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 11537c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 11547c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 11557c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 11567c478bd9Sstevel@tonic-gate 0x85524000, /* rdpr %cwp, %g2 */ 11577c478bd9Sstevel@tonic-gate 0x87518000, /* rdpr %pstate, %g3 */ 11587c478bd9Sstevel@tonic-gate 0x8728f008, /* sllx %g3, 8, %g3 */ 11597c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 11607c478bd9Sstevel@tonic-gate 0x8740c000, /* rd %asi, %g3 */ 11617c478bd9Sstevel@tonic-gate 0x8728f018, /* sllx %g3, 24, %g3 */ 11627c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 11637c478bd9Sstevel@tonic-gate 0x8350c000, /* rdpr %tt, %g1 */ 11647c478bd9Sstevel@tonic-gate 0x8f902002, /* wrpr %g0, 2, %tl */ 11657c478bd9Sstevel@tonic-gate 0x85908000, /* wrpr %g2, %g0, %tstate */ 11667c478bd9Sstevel@tonic-gate 0x87904000, /* wrpr %g1, %g0, %tt */ 11677c478bd9Sstevel@tonic-gate 0xc2d80000, /* ldxa [%g0]ASI_MMU, %g1 */ 11687c478bd9Sstevel@tonic-gate 0x83307030, /* srlx %g1, CTXSHIFT, %g1 */ 11697c478bd9Sstevel@tonic-gate 0x02c04004, /* brz,pn %g1, .+0x10 */ 11707c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(new_tpc), %g1 */ 11717c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 11727c478bd9Sstevel@tonic-gate 0x30800002, /* ba,a .+0x8 */ 11737c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 11747c478bd9Sstevel@tonic-gate 0x81904000, /* wrpr %g1, %g0, %tpc */ 11757c478bd9Sstevel@tonic-gate 0x82006004, /* add %g1, 4, %g1 */ 11767c478bd9Sstevel@tonic-gate 0x83904000, /* wrpr %g1, %g0, %tnpc */ 11777c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(tmptick), %g1 */ 11787c478bd9Sstevel@tonic-gate 0x85410000, /* rd %tick, %g2 */ 11797c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(tmptick)] */ 11807c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 11817c478bd9Sstevel@tonic-gate NOP, NOP, NOP 11827c478bd9Sstevel@tonic-gate #else /* sun4v */ 11837c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 11847c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 11857c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 11867c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 11877c478bd9Sstevel@tonic-gate 0x85524000, /* rdpr %cwp, %g2 */ 11887c478bd9Sstevel@tonic-gate 0x87518000, /* rdpr %pstate, %g3 */ 11897c478bd9Sstevel@tonic-gate 0x8728f008, /* sllx %g3, 8, %g3 */ 11907c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 11917c478bd9Sstevel@tonic-gate 0x8740c000, /* rd %asi, %g3 */ 11927c478bd9Sstevel@tonic-gate 0x8728f018, /* sllx %g3, 24, %g3 */ 1193bd46b14cSgirish 0x83540000, /* rdpr %gl, %g1 */ 1194bd46b14cSgirish 0x83287028, /* sllx %g1, 40, %g1 */ 1195bd46b14cSgirish 0x86104003, /* or %g1, %g3, %g3 */ 11967c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 11977c478bd9Sstevel@tonic-gate 0x8350c000, /* rdpr %tt, %g1 */ 11987c478bd9Sstevel@tonic-gate 0x8f902002, /* wrpr %g0, 2, %tl */ 11997c478bd9Sstevel@tonic-gate 0x85908000, /* wrpr %g2, %g0, %tstate */ 12007c478bd9Sstevel@tonic-gate 0x87904000, /* wrpr %g1, %g0, %tt */ 12017c478bd9Sstevel@tonic-gate 0xc2d80400, /* ldxa [%g0]ASI_SCRATCHPAD, %g1 */ 12027c478bd9Sstevel@tonic-gate 0xc2586000, /* ldx [%g1 + MMFSA_?_CTX], %g1 */ 12037c478bd9Sstevel@tonic-gate 0x02c04004, /* brz,pn %g1, .+0x10 */ 12047c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(new_tpc), %g1 */ 12057c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 12067c478bd9Sstevel@tonic-gate 0x30800002, /* ba,a .+0x8 */ 12077c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 12087c478bd9Sstevel@tonic-gate 0x81904000, /* wrpr %g1, %g0, %tpc */ 12097c478bd9Sstevel@tonic-gate 0x82006004, /* add %g1, 4, %g1 */ 12107c478bd9Sstevel@tonic-gate 0x83904000, /* wrpr %g1, %g0, %tnpc */ 12117c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(tmptick), %g1 */ 12127c478bd9Sstevel@tonic-gate 0x85410000, /* rd %tick, %g2 */ 12137c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(tmptick)] */ 12147c478bd9Sstevel@tonic-gate 0x30800000 /* ba,a addr */ 12157c478bd9Sstevel@tonic-gate #endif /* sun4v */ 12167c478bd9Sstevel@tonic-gate }; 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 1219ce0352ebSgirish #ifndef sun4v 12207c478bd9Sstevel@tonic-gate ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS); 1221ce0352ebSgirish #else 1222ce0352ebSgirish ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS || 1223ce0352ebSgirish entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_DMMUMISS); 1224ce0352ebSgirish #endif 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate stat = TSTAT_DATA_OFFS(tcpu, tdata_traps) + entoffs; 12277c478bd9Sstevel@tonic-gate tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick); 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate if (itlb) { 12307c478bd9Sstevel@tonic-gate ret = &tcpu->tcpu_instr->tinst_itlbret; 12317c478bd9Sstevel@tonic-gate udata = &data->tpgsz_user.tmode_itlb; 12327c478bd9Sstevel@tonic-gate kdata = &data->tpgsz_kernel.tmode_itlb; 12337c478bd9Sstevel@tonic-gate tpc = TSTAT_INSTR_OFFS(tcpu, tinst_itlbret.ttlbr_ktlb); 12347c478bd9Sstevel@tonic-gate } else { 12357c478bd9Sstevel@tonic-gate ret = &tcpu->tcpu_instr->tinst_dtlbret; 12367c478bd9Sstevel@tonic-gate udata = &data->tpgsz_user.tmode_dtlb; 12377c478bd9Sstevel@tonic-gate kdata = &data->tpgsz_kernel.tmode_dtlb; 12387c478bd9Sstevel@tonic-gate tpc = TSTAT_INSTR_OFFS(tcpu, tinst_dtlbret.ttlbr_ktlb); 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate utpc = tpc + offsetof(tstat_tlbret_t, ttlbr_utlb) - 12427c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb); 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate ASSERT(HI22(tpc) == HI22(utpc)); 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate ent = (uint32_t *)((uintptr_t)tcpu->tcpu_instr + entoffs); 12477c478bd9Sstevel@tonic-gate orig = KERNELBASE + entoffs; 12487c478bd9Sstevel@tonic-gate va = (uintptr_t)tcpu->tcpu_ibase + entoffs; 12497c478bd9Sstevel@tonic-gate baoffs = TSTAT_TLBENT_BA * sizeof (uint32_t); 12507c478bd9Sstevel@tonic-gate 1251ce0352ebSgirish #ifdef sun4v 1252ce0352ebSgirish if (entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_DMMUMISS) { 1253ce0352ebSgirish /* 1254ce0352ebSgirish * Because of lack of space, interposing tlbent trap 1255ce0352ebSgirish * handler for IMMU_miss and DMMU_miss traps cannot be 1256ce0352ebSgirish * placed in-line. Instead, we copy it to the space set 1257ce0352ebSgirish * aside for these traps in per CPU trapstat area and 1258ce0352ebSgirish * invoke it by placing a branch in the trap table itself. 1259ce0352ebSgirish */ 1260ce0352ebSgirish static const uint32_t mmumiss[TSTAT_ENT_NINSTR] = { 1261ce0352ebSgirish 0x30800000, /* ba,a addr */ 1262ce0352ebSgirish NOP, NOP, NOP, NOP, NOP, NOP, NOP 1263ce0352ebSgirish }; 1264ce0352ebSgirish uint32_t *tent = ent; /* trap vector entry */ 1265ce0352ebSgirish uintptr_t tentva = va; /* trap vector entry va */ 1266ce0352ebSgirish 1267ce0352ebSgirish if (itlb) { 1268ce0352ebSgirish ent = (uint32_t *)((uintptr_t) 1269ce0352ebSgirish &tcpu->tcpu_instr->tinst_immumiss); 1270ce0352ebSgirish va = TSTAT_INSTR_OFFS(tcpu, tinst_immumiss); 1271ce0352ebSgirish } else { 1272ce0352ebSgirish ent = (uint32_t *)((uintptr_t) 1273ce0352ebSgirish &tcpu->tcpu_instr->tinst_dmmumiss); 1274ce0352ebSgirish va = TSTAT_INSTR_OFFS(tcpu, tinst_dmmumiss); 1275ce0352ebSgirish } 1276ce0352ebSgirish bcopy(mmumiss, tent, sizeof (mmumiss)); 1277ce0352ebSgirish tent[0] |= DISP22(tentva, va); 1278ce0352ebSgirish } 1279ce0352ebSgirish #endif /* sun4v */ 1280ce0352ebSgirish 12817c478bd9Sstevel@tonic-gate bcopy(tlbent, ent, sizeof (tlbent)); 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATHI] |= HI22(stat); 12847c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATLO_LD] |= LO10(stat); 12857c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATLO_ST] |= LO10(stat); 12867c478bd9Sstevel@tonic-gate #ifndef sun4v 12877c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_MMUASI] |= asi; 12887c478bd9Sstevel@tonic-gate #else 12897c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TAGTARGET] |= tagtarget_off; 12907c478bd9Sstevel@tonic-gate #endif 12917c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCHI] |= HI22(tpc); 12927c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCLO_USER] |= LO10(utpc); 12937c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCLO_KERN] |= LO10(tpc); 12947c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TSHI] |= HI22(tmptick); 12957c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TSLO] |= LO10(tmptick); 12967c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_BA] |= DISP22(va + baoffs, orig); 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate /* 12997c478bd9Sstevel@tonic-gate * And now set up the TLB return entries. 13007c478bd9Sstevel@tonic-gate */ 13017c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_ktlb, &kdata->ttlb_tlb); 13027c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_ktsb, &kdata->ttlb_tsb); 13037c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_utlb, &udata->ttlb_tlb); 13047c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_utsb, &udata->ttlb_tsb); 13057c478bd9Sstevel@tonic-gate } 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATHI 13087c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_LD 13097c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_ST 13107c478bd9Sstevel@tonic-gate #ifndef sun4v 13117c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_MMUASI 13127c478bd9Sstevel@tonic-gate #else 13137c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TAGTARGET 13147c478bd9Sstevel@tonic-gate #endif 13157c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCHI 13167c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_USER 13177c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_KERN 13187c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSHI 13197c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSLO 13207c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_BA 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate /* 13237c478bd9Sstevel@tonic-gate * The TSTAT_ENABLED_* constants define offsets in the enabled entry; the 13247c478bd9Sstevel@tonic-gate * TSTAT_DISABLED_BA constant defines an offset in the disabled entry. Both 13257c478bd9Sstevel@tonic-gate * sets of constants are used only in trapstat_make_traptab() (below) and 13267c478bd9Sstevel@tonic-gate * #undef'd immediately afterwards. Any change to "enabled" or "disabled" 13277c478bd9Sstevel@tonic-gate * in trapstat_make_traptab() will likely require changes to these constants. 13287c478bd9Sstevel@tonic-gate */ 13297c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATHI 0 13307c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATLO_LD 1 13317c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATLO_ST 3 13327c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_BA 4 13337c478bd9Sstevel@tonic-gate #define TSTAT_DISABLED_BA 0 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate static void 13367c478bd9Sstevel@tonic-gate trapstat_make_traptab(tstat_percpu_t *tcpu) 13377c478bd9Sstevel@tonic-gate { 13387c478bd9Sstevel@tonic-gate uint32_t *ent; 13397c478bd9Sstevel@tonic-gate uint64_t *stat; 13407c478bd9Sstevel@tonic-gate uintptr_t orig, va, en_baoffs, dis_baoffs; 13417c478bd9Sstevel@tonic-gate int nent; 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate /* 13447c478bd9Sstevel@tonic-gate * This is the entry in the interposing trap table for enabled trap 13457c478bd9Sstevel@tonic-gate * table entries. It loads a counter, increments it and stores it 13467c478bd9Sstevel@tonic-gate * back before branching to the actual trap table entry. 13477c478bd9Sstevel@tonic-gate */ 13487c478bd9Sstevel@tonic-gate static const uint32_t enabled[TSTAT_ENT_NINSTR] = { 13497c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 13507c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 13517c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 13527c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 13537c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 13547c478bd9Sstevel@tonic-gate NOP, NOP, NOP 13557c478bd9Sstevel@tonic-gate }; 13567c478bd9Sstevel@tonic-gate 13577c478bd9Sstevel@tonic-gate /* 13587c478bd9Sstevel@tonic-gate * This is the entry in the interposing trap table for disabled trap 13597c478bd9Sstevel@tonic-gate * table entries. It simply branches to the actual, underlying trap 13607c478bd9Sstevel@tonic-gate * table entry. As explained in the "Implementation Details" section 13617c478bd9Sstevel@tonic-gate * of the block comment, all TL>0 traps _must_ use the disabled entry; 13627c478bd9Sstevel@tonic-gate * additional entries may be explicitly disabled through the use 13637c478bd9Sstevel@tonic-gate * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY. 13647c478bd9Sstevel@tonic-gate */ 13657c478bd9Sstevel@tonic-gate static const uint32_t disabled[TSTAT_ENT_NINSTR] = { 13667c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 13677c478bd9Sstevel@tonic-gate NOP, NOP, NOP, NOP, NOP, NOP, NOP, 13687c478bd9Sstevel@tonic-gate }; 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate ent = tcpu->tcpu_instr->tinst_traptab; 13737c478bd9Sstevel@tonic-gate stat = (uint64_t *)TSTAT_DATA_OFFS(tcpu, tdata_traps); 13747c478bd9Sstevel@tonic-gate orig = KERNELBASE; 13757c478bd9Sstevel@tonic-gate va = (uintptr_t)tcpu->tcpu_ibase; 13767c478bd9Sstevel@tonic-gate en_baoffs = TSTAT_ENABLED_BA * sizeof (uint32_t); 13777c478bd9Sstevel@tonic-gate dis_baoffs = TSTAT_DISABLED_BA * sizeof (uint32_t); 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) { 13807c478bd9Sstevel@tonic-gate if (tstat_enabled[nent]) { 13817c478bd9Sstevel@tonic-gate bcopy(enabled, ent, sizeof (enabled)); 13829f1a1f17Sdmick ent[TSTAT_ENABLED_STATHI] |= HI22((uintptr_t)stat); 13839f1a1f17Sdmick ent[TSTAT_ENABLED_STATLO_LD] |= LO10((uintptr_t)stat); 13849f1a1f17Sdmick ent[TSTAT_ENABLED_STATLO_ST] |= LO10((uintptr_t)stat); 13857c478bd9Sstevel@tonic-gate ent[TSTAT_ENABLED_BA] |= DISP22(va + en_baoffs, orig); 13867c478bd9Sstevel@tonic-gate } else { 13877c478bd9Sstevel@tonic-gate bcopy(disabled, ent, sizeof (disabled)); 13887c478bd9Sstevel@tonic-gate ent[TSTAT_DISABLED_BA] |= DISP22(va + dis_baoffs, orig); 13897c478bd9Sstevel@tonic-gate } 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate stat++; 13927c478bd9Sstevel@tonic-gate orig += sizeof (enabled); 13937c478bd9Sstevel@tonic-gate ent += sizeof (enabled) / sizeof (*ent); 13947c478bd9Sstevel@tonic-gate va += sizeof (enabled); 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATHI 13997c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_LD 14007c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_ST 14017c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_BA 14027c478bd9Sstevel@tonic-gate #undef TSTAT_DISABLED_BA 14037c478bd9Sstevel@tonic-gate 140425cf1a30Sjl139090 #ifndef sun4v 140525cf1a30Sjl139090 /* 140625cf1a30Sjl139090 * See Section A.6 in SPARC v9 Manual. 140725cf1a30Sjl139090 * max branch = 4*((2^21)-1) = 8388604 140825cf1a30Sjl139090 */ 140925cf1a30Sjl139090 #define MAX_BICC_BRANCH_DISPLACEMENT (4 * ((1 << 21) - 1)) 141025cf1a30Sjl139090 #endif 141125cf1a30Sjl139090 14127c478bd9Sstevel@tonic-gate static void 14137c478bd9Sstevel@tonic-gate trapstat_setup(processorid_t cpu) 14147c478bd9Sstevel@tonic-gate { 14157c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 14167c478bd9Sstevel@tonic-gate #ifndef sun4v 14177c478bd9Sstevel@tonic-gate int i; 14187c478bd9Sstevel@tonic-gate caddr_t va; 14197c478bd9Sstevel@tonic-gate pfn_t *pfn; 142025cf1a30Sjl139090 cpu_t *cp; 142125cf1a30Sjl139090 uint_t strand_idx; 142225cf1a30Sjl139090 size_t tstat_offset; 14237c478bd9Sstevel@tonic-gate #endif 14247c478bd9Sstevel@tonic-gate 14257c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_pfn == NULL); 14267c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_instr == NULL); 14277c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_data == NULL); 14287c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 14297c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED)); 14307c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 14317c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 14327c478bd9Sstevel@tonic-gate 14337c478bd9Sstevel@tonic-gate /* 14347c478bd9Sstevel@tonic-gate * The lower fifteen bits of the %tba are always read as zero; we must 14357c478bd9Sstevel@tonic-gate * align our instruction base address appropriately. 14367c478bd9Sstevel@tonic-gate */ 14377c478bd9Sstevel@tonic-gate #ifndef sun4v 143825cf1a30Sjl139090 tstat_offset = tstat_total_size; 143925cf1a30Sjl139090 144025cf1a30Sjl139090 cp = cpu_get(cpu); 144125cf1a30Sjl139090 ASSERT(cp != NULL); 144225cf1a30Sjl139090 if ((strand_idx = cpu ^ chip_plat_get_coreid(cp)) != 0) { 144325cf1a30Sjl139090 /* 144425cf1a30Sjl139090 * On sun4u platforms with multiple CPUs sharing the MMU 144525cf1a30Sjl139090 * (Olympus-C has 2 strands per core), each CPU uses a 144625cf1a30Sjl139090 * disjoint trap table. The indexing is based on the 144725cf1a30Sjl139090 * strand id, which is obtained by XOR'ing the cpuid with 144825cf1a30Sjl139090 * the coreid. 144925cf1a30Sjl139090 */ 145025cf1a30Sjl139090 tstat_offset += tstat_total_size * strand_idx; 145125cf1a30Sjl139090 145225cf1a30Sjl139090 /* 145325cf1a30Sjl139090 * Offset must be less than the maximum PC-relative branch 145425cf1a30Sjl139090 * displacement for Bicc variants. See the Implementation 145525cf1a30Sjl139090 * Details comment. 145625cf1a30Sjl139090 */ 145725cf1a30Sjl139090 ASSERT(tstat_offset <= MAX_BICC_BRANCH_DISPLACEMENT); 145825cf1a30Sjl139090 } 145925cf1a30Sjl139090 146025cf1a30Sjl139090 tcpu->tcpu_ibase = (caddr_t)((KERNELBASE - tstat_offset) 14617c478bd9Sstevel@tonic-gate & TSTAT_TBA_MASK); 14627c478bd9Sstevel@tonic-gate tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE; 14637c478bd9Sstevel@tonic-gate tcpu->tcpu_vabase = tcpu->tcpu_ibase; 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate tcpu->tcpu_pfn = vmem_alloc(tstat_arena, tstat_total_pages, VM_SLEEP); 14667c478bd9Sstevel@tonic-gate bzero(tcpu->tcpu_pfn, tstat_total_pages); 14677c478bd9Sstevel@tonic-gate pfn = tcpu->tcpu_pfn; 14687c478bd9Sstevel@tonic-gate 14697c478bd9Sstevel@tonic-gate tcpu->tcpu_instr = vmem_alloc(tstat_arena, TSTAT_INSTR_SIZE, VM_SLEEP); 14707c478bd9Sstevel@tonic-gate 14717c478bd9Sstevel@tonic-gate va = (caddr_t)tcpu->tcpu_instr; 14727c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_INSTR_PAGES; i++, va += MMU_PAGESIZE) 14737c478bd9Sstevel@tonic-gate *pfn++ = hat_getpfnum(kas.a_hat, va); 14747c478bd9Sstevel@tonic-gate 14757c478bd9Sstevel@tonic-gate /* 14767c478bd9Sstevel@tonic-gate * We must be sure that the pages that we will use to examine the data 14777c478bd9Sstevel@tonic-gate * have the same virtual color as the pages to which the data is being 14787c478bd9Sstevel@tonic-gate * recorded, hence the alignment and phase constraints on the 14797c478bd9Sstevel@tonic-gate * allocation. 14807c478bd9Sstevel@tonic-gate */ 14817c478bd9Sstevel@tonic-gate tcpu->tcpu_data = vmem_xalloc(tstat_arena, tstat_data_size, 14827c478bd9Sstevel@tonic-gate shm_alignment, (uintptr_t)tcpu->tcpu_dbase & (shm_alignment - 1), 14837c478bd9Sstevel@tonic-gate 0, 0, NULL, VM_SLEEP); 14847c478bd9Sstevel@tonic-gate bzero(tcpu->tcpu_data, tstat_data_size); 14857c478bd9Sstevel@tonic-gate tcpu->tcpu_data->tdata_cpuid = cpu; 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate va = (caddr_t)tcpu->tcpu_data; 14887c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_data_pages; i++, va += MMU_PAGESIZE) 14897c478bd9Sstevel@tonic-gate *pfn++ = hat_getpfnum(kas.a_hat, va); 14907c478bd9Sstevel@tonic-gate #else /* sun4v */ 14917c478bd9Sstevel@tonic-gate ASSERT(!(tstat_total_size > (1 + ~TSTAT_TBA_MASK))); 14927c478bd9Sstevel@tonic-gate tcpu->tcpu_vabase = (caddr_t)(KERNELBASE - MMU_PAGESIZE4M); 14937c478bd9Sstevel@tonic-gate tcpu->tcpu_ibase = tcpu->tcpu_vabase + (cpu * (1 + ~TSTAT_TBA_MASK)); 14947c478bd9Sstevel@tonic-gate tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE; 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate tcpu->tcpu_pfn = &tstat_pfn; 14977c478bd9Sstevel@tonic-gate tcpu->tcpu_instr = (tstat_instr_t *)(tstat_va + (cpu * 14987c478bd9Sstevel@tonic-gate (1 + ~TSTAT_TBA_MASK))); 14997c478bd9Sstevel@tonic-gate tcpu->tcpu_data = (tstat_data_t *)(tstat_va + (cpu * 15007c478bd9Sstevel@tonic-gate (1 + ~TSTAT_TBA_MASK)) + TSTAT_INSTR_SIZE); 15017c478bd9Sstevel@tonic-gate bzero(tcpu->tcpu_data, tstat_data_size); 15027c478bd9Sstevel@tonic-gate tcpu->tcpu_data->tdata_cpuid = cpu; 15037c478bd9Sstevel@tonic-gate #endif /* sun4v */ 15047c478bd9Sstevel@tonic-gate 15057c478bd9Sstevel@tonic-gate /* 15067c478bd9Sstevel@tonic-gate * Now that we have all of the instruction and data pages allocated, 15077c478bd9Sstevel@tonic-gate * make the trap table from scratch. 15087c478bd9Sstevel@tonic-gate */ 15097c478bd9Sstevel@tonic-gate trapstat_make_traptab(tcpu); 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate if (tstat_options & TSTAT_OPT_TLBDATA) { 15127c478bd9Sstevel@tonic-gate /* 15137c478bd9Sstevel@tonic-gate * TLB Statistics have been specified; set up the I- and D-TLB 15147c478bd9Sstevel@tonic-gate * entries and corresponding TLB return entries. 15157c478bd9Sstevel@tonic-gate */ 1516ce0352ebSgirish #ifndef sun4v 15177c478bd9Sstevel@tonic-gate trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS); 15187c478bd9Sstevel@tonic-gate trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS); 1519ce0352ebSgirish #else 1520ce0352ebSgirish if (tstat_fast_tlbstat) { 1521ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_IMMUMISS); 1522ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_DMMUMISS); 1523ce0352ebSgirish } else { 1524ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS); 1525ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS); 1526ce0352ebSgirish } 1527ce0352ebSgirish #endif 15287c478bd9Sstevel@tonic-gate } 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate tcpu->tcpu_flags |= TSTAT_CPU_ALLOCATED; 15317c478bd9Sstevel@tonic-gate 15327c478bd9Sstevel@tonic-gate /* 15337c478bd9Sstevel@tonic-gate * Finally, get the target CPU to load the locked pages into its TLBs. 15347c478bd9Sstevel@tonic-gate */ 15357c478bd9Sstevel@tonic-gate xc_one(cpu, (xcfunc_t *)trapstat_load_tlb, 0, 0); 15367c478bd9Sstevel@tonic-gate } 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate static void 15397c478bd9Sstevel@tonic-gate trapstat_teardown(processorid_t cpu) 15407c478bd9Sstevel@tonic-gate { 15417c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 15427c478bd9Sstevel@tonic-gate #ifndef sun4v 15437c478bd9Sstevel@tonic-gate int i; 15447c478bd9Sstevel@tonic-gate #endif 15457c478bd9Sstevel@tonic-gate caddr_t va = tcpu->tcpu_vabase; 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_pfn != NULL); 15487c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_instr != NULL); 15497c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_data != NULL); 15507c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 15517c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 15527c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 15537c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 15547c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate #ifndef sun4v 15577c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_pfn, tstat_total_pages); 15587c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_instr, TSTAT_INSTR_SIZE); 15597c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_data, tstat_data_size); 15607c478bd9Sstevel@tonic-gate 15617c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) { 15627c478bd9Sstevel@tonic-gate xt_one(cpu, vtag_flushpage_tl1, (uint64_t)va, KCONTEXT); 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate #else 15657c478bd9Sstevel@tonic-gate xt_one(cpu, vtag_unmap_perm_tl1, (uint64_t)va, KCONTEXT); 15667c478bd9Sstevel@tonic-gate #endif 15677c478bd9Sstevel@tonic-gate 15687c478bd9Sstevel@tonic-gate tcpu->tcpu_pfn = NULL; 15697c478bd9Sstevel@tonic-gate tcpu->tcpu_instr = NULL; 15707c478bd9Sstevel@tonic-gate tcpu->tcpu_data = NULL; 15717c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED; 15727c478bd9Sstevel@tonic-gate } 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate static int 15757c478bd9Sstevel@tonic-gate trapstat_go() 15767c478bd9Sstevel@tonic-gate { 15777c478bd9Sstevel@tonic-gate cpu_t *cp; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 15807c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 15817c478bd9Sstevel@tonic-gate 15827c478bd9Sstevel@tonic-gate if (tstat_running) { 15837c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 15847c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 15857c478bd9Sstevel@tonic-gate return (EBUSY); 15867c478bd9Sstevel@tonic-gate } 15877c478bd9Sstevel@tonic-gate 15887c478bd9Sstevel@tonic-gate #ifdef sun4v 15897c478bd9Sstevel@tonic-gate /* 1590ce0352ebSgirish * Allocate large page to hold interposing tables. 15917c478bd9Sstevel@tonic-gate */ 15927c478bd9Sstevel@tonic-gate tstat_va = contig_mem_alloc(MMU_PAGESIZE4M); 15937c478bd9Sstevel@tonic-gate tstat_pfn = va_to_pfn(tstat_va); 1594ce0352ebSgirish if (tstat_pfn == PFN_INVALID) 15957c478bd9Sstevel@tonic-gate return (EAGAIN); 1596ce0352ebSgirish 1597ce0352ebSgirish /* 1598ce0352ebSgirish * For detailed TLB statistics, invoke CPU specific interface 1599ce0352ebSgirish * to see if it supports a low overhead interface to collect 1600ce0352ebSgirish * TSB hit statistics. If so, make set tstat_fast_tlbstat flag 1601ce0352ebSgirish * to reflect that. 1602ce0352ebSgirish */ 1603ce0352ebSgirish if (tstat_options & TSTAT_OPT_TLBDATA) { 1604ce0352ebSgirish int error; 1605ce0352ebSgirish 1606ce0352ebSgirish error = cpu_trapstat_conf(CPU_TSTATCONF_INIT); 1607ce0352ebSgirish if (error == 0) 1608ce0352ebSgirish tstat_fast_tlbstat = B_TRUE; 1609ce0352ebSgirish else if (error != ENOTSUP) { 1610ce0352ebSgirish contig_mem_free(tstat_va, MMU_PAGESIZE4M); 1611ce0352ebSgirish return (error); 1612ce0352ebSgirish } 16137c478bd9Sstevel@tonic-gate } 16147c478bd9Sstevel@tonic-gate #endif 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate /* 16177c478bd9Sstevel@tonic-gate * First, perform any necessary hot patching. 16187c478bd9Sstevel@tonic-gate */ 16197c478bd9Sstevel@tonic-gate trapstat_hotpatch(); 16207c478bd9Sstevel@tonic-gate 16217c478bd9Sstevel@tonic-gate /* 16227c478bd9Sstevel@tonic-gate * Allocate the resources we'll need to measure probe effect. 16237c478bd9Sstevel@tonic-gate */ 16247c478bd9Sstevel@tonic-gate trapstat_probe_alloc(); 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate 16277c478bd9Sstevel@tonic-gate cp = cpu_list; 16287c478bd9Sstevel@tonic-gate do { 16297c478bd9Sstevel@tonic-gate if (!(tstat_percpu[cp->cpu_id].tcpu_flags & TSTAT_CPU_SELECTED)) 16307c478bd9Sstevel@tonic-gate continue; 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate trapstat_setup(cp->cpu_id); 16337c478bd9Sstevel@tonic-gate 16347c478bd9Sstevel@tonic-gate /* 16357c478bd9Sstevel@tonic-gate * Note that due to trapstat_probe()'s use of global data, 16367c478bd9Sstevel@tonic-gate * we determine the probe effect on each CPU serially instead 16377c478bd9Sstevel@tonic-gate * of in parallel with an xc_all(). 16387c478bd9Sstevel@tonic-gate */ 16397c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_probe, 0, 0); 16407c478bd9Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list); 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate xc_all((xcfunc_t *)trapstat_enable, 0, 0); 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate trapstat_probe_free(); 16457c478bd9Sstevel@tonic-gate tstat_running = 1; 16467c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 16477c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate return (0); 16507c478bd9Sstevel@tonic-gate } 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate static int 16537c478bd9Sstevel@tonic-gate trapstat_stop() 16547c478bd9Sstevel@tonic-gate { 16557c478bd9Sstevel@tonic-gate int i; 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 16587c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 16597c478bd9Sstevel@tonic-gate if (!tstat_running) { 16607c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 16617c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 16627c478bd9Sstevel@tonic-gate return (ENXIO); 16637c478bd9Sstevel@tonic-gate } 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate xc_all((xcfunc_t *)trapstat_disable, 0, 0); 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) { 16687c478bd9Sstevel@tonic-gate if (tstat_percpu[i].tcpu_flags & TSTAT_CPU_ALLOCATED) 16697c478bd9Sstevel@tonic-gate trapstat_teardown(i); 16707c478bd9Sstevel@tonic-gate } 16717c478bd9Sstevel@tonic-gate 16727c478bd9Sstevel@tonic-gate #ifdef sun4v 1673ce0352ebSgirish if (tstat_options & TSTAT_OPT_TLBDATA) 1674ce0352ebSgirish cpu_trapstat_conf(CPU_TSTATCONF_FINI); 16757c478bd9Sstevel@tonic-gate contig_mem_free(tstat_va, MMU_PAGESIZE4M); 16767c478bd9Sstevel@tonic-gate #endif 16777c478bd9Sstevel@tonic-gate trapstat_hotpatch(); 16787c478bd9Sstevel@tonic-gate tstat_running = 0; 16797c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 16807c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate return (0); 16837c478bd9Sstevel@tonic-gate } 16847c478bd9Sstevel@tonic-gate 16857c478bd9Sstevel@tonic-gate /* 16867c478bd9Sstevel@tonic-gate * This is trapstat's DR CPU configuration callback. It's called (with 16877c478bd9Sstevel@tonic-gate * cpu_lock held) to unconfigure a newly powered-off CPU, or to configure a 16887c478bd9Sstevel@tonic-gate * powered-off CPU that is to be brought into the system. We need only take 16897c478bd9Sstevel@tonic-gate * action in the unconfigure case: because a powered-off CPU will have its 16907c478bd9Sstevel@tonic-gate * trap table restored to KERNELBASE if it is ever powered back on, we must 16917c478bd9Sstevel@tonic-gate * update the flags to reflect that trapstat is no longer enabled on the 16927c478bd9Sstevel@tonic-gate * powered-off CPU. Note that this means that a TSTAT_CPU_ENABLED CPU that 16937c478bd9Sstevel@tonic-gate * is unconfigured/powered off and later powered back on/reconfigured will 16947c478bd9Sstevel@tonic-gate * _not_ be re-TSTAT_CPU_ENABLED. 16957c478bd9Sstevel@tonic-gate */ 16967c478bd9Sstevel@tonic-gate static int 16977c478bd9Sstevel@tonic-gate trapstat_cpu_setup(cpu_setup_t what, processorid_t cpu) 16987c478bd9Sstevel@tonic-gate { 16997c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 17027c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate if (!tstat_running) { 17057c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 17067c478bd9Sstevel@tonic-gate return (0); 17077c478bd9Sstevel@tonic-gate } 17087c478bd9Sstevel@tonic-gate 17097c478bd9Sstevel@tonic-gate switch (what) { 17107c478bd9Sstevel@tonic-gate case CPU_CONFIG: 17117c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 17127c478bd9Sstevel@tonic-gate break; 17137c478bd9Sstevel@tonic-gate 17147c478bd9Sstevel@tonic-gate case CPU_UNCONFIG: 1715*1ae08745Sheppo if (tcpu->tcpu_flags & TSTAT_CPU_ENABLED) { 17167c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED; 1717*1ae08745Sheppo #ifdef sun4v 1718*1ae08745Sheppo /* 1719*1ae08745Sheppo * A power-off, causes the cpu mondo queues to be 1720*1ae08745Sheppo * unconfigured on sun4v. Since we can't teardown 1721*1ae08745Sheppo * trapstat's mappings on the cpu that is going away, 1722*1ae08745Sheppo * we simply mark it as not allocated. This will 1723*1ae08745Sheppo * prevent a teardown on a cpu with the same cpu id 1724*1ae08745Sheppo * that might have been added while trapstat is running. 1725*1ae08745Sheppo */ 1726*1ae08745Sheppo if (tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED) { 1727*1ae08745Sheppo tcpu->tcpu_pfn = NULL; 1728*1ae08745Sheppo tcpu->tcpu_instr = NULL; 1729*1ae08745Sheppo tcpu->tcpu_data = NULL; 1730*1ae08745Sheppo tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED; 1731*1ae08745Sheppo } 1732*1ae08745Sheppo #endif 1733*1ae08745Sheppo } 17347c478bd9Sstevel@tonic-gate break; 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate default: 17377c478bd9Sstevel@tonic-gate break; 17387c478bd9Sstevel@tonic-gate } 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 17417c478bd9Sstevel@tonic-gate return (0); 17427c478bd9Sstevel@tonic-gate } 17437c478bd9Sstevel@tonic-gate 17447c478bd9Sstevel@tonic-gate /* 17457c478bd9Sstevel@tonic-gate * This is called before a CPR suspend and after a CPR resume. We don't have 17467c478bd9Sstevel@tonic-gate * anything to do before a suspend, but after a restart we must restore the 17477c478bd9Sstevel@tonic-gate * trap table to be our interposing trap table. However, we don't actually 17487c478bd9Sstevel@tonic-gate * know whether or not the CPUs have been powered off -- this routine may be 17497c478bd9Sstevel@tonic-gate * called while restoring from a failed CPR suspend. We thus run through each 17507c478bd9Sstevel@tonic-gate * TSTAT_CPU_ENABLED CPU, and explicitly destroy and reestablish its 17517c478bd9Sstevel@tonic-gate * interposing trap table. This assures that our state is correct regardless 17527c478bd9Sstevel@tonic-gate * of whether or not the CPU has been newly powered on. 17537c478bd9Sstevel@tonic-gate */ 17547c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 17557c478bd9Sstevel@tonic-gate static boolean_t 17567c478bd9Sstevel@tonic-gate trapstat_cpr(void *arg, int code) 17577c478bd9Sstevel@tonic-gate { 17587c478bd9Sstevel@tonic-gate cpu_t *cp; 17597c478bd9Sstevel@tonic-gate 17607c478bd9Sstevel@tonic-gate if (code == CB_CODE_CPR_CHKPT) 17617c478bd9Sstevel@tonic-gate return (B_TRUE); 17627c478bd9Sstevel@tonic-gate 17637c478bd9Sstevel@tonic-gate ASSERT(code == CB_CODE_CPR_RESUME); 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 17667c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate if (!tstat_running) { 17697c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 17707c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 17717c478bd9Sstevel@tonic-gate return (B_TRUE); 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate cp = cpu_list; 17757c478bd9Sstevel@tonic-gate do { 17767c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cp->cpu_id]; 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 17797c478bd9Sstevel@tonic-gate continue; 17807c478bd9Sstevel@tonic-gate 17817c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 17827c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_disable, 0, 0); 17857c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 17867c478bd9Sstevel@tonic-gate 17877c478bd9Sstevel@tonic-gate /* 17887c478bd9Sstevel@tonic-gate * Preserve this CPU's data in tstat_buffer and rip down its 17897c478bd9Sstevel@tonic-gate * interposing trap table. 17907c478bd9Sstevel@tonic-gate */ 17917c478bd9Sstevel@tonic-gate bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size); 17927c478bd9Sstevel@tonic-gate trapstat_teardown(cp->cpu_id); 17937c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED)); 17947c478bd9Sstevel@tonic-gate 17957c478bd9Sstevel@tonic-gate /* 17967c478bd9Sstevel@tonic-gate * Reestablish the interposing trap table and restore the old 17977c478bd9Sstevel@tonic-gate * data. 17987c478bd9Sstevel@tonic-gate */ 17997c478bd9Sstevel@tonic-gate trapstat_setup(cp->cpu_id); 18007c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 18017c478bd9Sstevel@tonic-gate bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size); 18027c478bd9Sstevel@tonic-gate 18037c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_enable, 0, 0); 18047c478bd9Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list); 18057c478bd9Sstevel@tonic-gate 18067c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 18077c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 18087c478bd9Sstevel@tonic-gate 18097c478bd9Sstevel@tonic-gate return (B_TRUE); 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18137c478bd9Sstevel@tonic-gate static int 18147c478bd9Sstevel@tonic-gate trapstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 18157c478bd9Sstevel@tonic-gate { 18167c478bd9Sstevel@tonic-gate int i; 18177c478bd9Sstevel@tonic-gate 18187c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 18197c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 18207c478bd9Sstevel@tonic-gate if (tstat_open != 0) { 18217c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 18227c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 18237c478bd9Sstevel@tonic-gate return (EBUSY); 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate /* 18277c478bd9Sstevel@tonic-gate * Register this in open() rather than in attach() to prevent deadlock 18287c478bd9Sstevel@tonic-gate * with DR code. During attach, I/O device tree locks are grabbed 18297c478bd9Sstevel@tonic-gate * before trapstat_attach() is invoked - registering in attach 18307c478bd9Sstevel@tonic-gate * will result in the lock order: device tree lock, cpu_lock. 18317c478bd9Sstevel@tonic-gate * DR code however requires that cpu_lock be acquired before 18327c478bd9Sstevel@tonic-gate * device tree locks. 18337c478bd9Sstevel@tonic-gate */ 18347c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 18357c478bd9Sstevel@tonic-gate register_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL); 18367c478bd9Sstevel@tonic-gate 18377c478bd9Sstevel@tonic-gate /* 18387c478bd9Sstevel@tonic-gate * Clear all options. And until specific CPUs are specified, we'll 18397c478bd9Sstevel@tonic-gate * mark all CPUs as selected. 18407c478bd9Sstevel@tonic-gate */ 18417c478bd9Sstevel@tonic-gate tstat_options = 0; 18427c478bd9Sstevel@tonic-gate 18437c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) 18447c478bd9Sstevel@tonic-gate tstat_percpu[i].tcpu_flags |= TSTAT_CPU_SELECTED; 18457c478bd9Sstevel@tonic-gate 18467c478bd9Sstevel@tonic-gate /* 18477c478bd9Sstevel@tonic-gate * By default, all traps at TL=0 are enabled. Traps at TL>0 must 18487c478bd9Sstevel@tonic-gate * be disabled. 18497c478bd9Sstevel@tonic-gate */ 18507c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_TOTAL_NENT; i++) 18517c478bd9Sstevel@tonic-gate tstat_enabled[i] = i < TSTAT_NENT ? 1 : 0; 18527c478bd9Sstevel@tonic-gate 18537c478bd9Sstevel@tonic-gate tstat_open = 1; 18547c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 18557c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate return (0); 18587c478bd9Sstevel@tonic-gate } 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18617c478bd9Sstevel@tonic-gate static int 18627c478bd9Sstevel@tonic-gate trapstat_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 18637c478bd9Sstevel@tonic-gate { 18647c478bd9Sstevel@tonic-gate (void) trapstat_stop(); 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 18677c478bd9Sstevel@tonic-gate 18687c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 18697c478bd9Sstevel@tonic-gate unregister_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL); 18707c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate tstat_open = 0; 18737c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 18747c478bd9Sstevel@tonic-gate } 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate static int 18777c478bd9Sstevel@tonic-gate trapstat_option(int option) 18787c478bd9Sstevel@tonic-gate { 18797c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 18807c478bd9Sstevel@tonic-gate 18817c478bd9Sstevel@tonic-gate if (tstat_running) { 18827c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 18837c478bd9Sstevel@tonic-gate return (EBUSY); 18847c478bd9Sstevel@tonic-gate } 18857c478bd9Sstevel@tonic-gate 18867c478bd9Sstevel@tonic-gate tstat_options |= option; 18877c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate return (0); 18907c478bd9Sstevel@tonic-gate } 18917c478bd9Sstevel@tonic-gate 18927c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18937c478bd9Sstevel@tonic-gate static int 18947c478bd9Sstevel@tonic-gate trapstat_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *crd, int *rval) 18957c478bd9Sstevel@tonic-gate { 18967c478bd9Sstevel@tonic-gate int i, j, out; 18977c478bd9Sstevel@tonic-gate size_t dsize; 18987c478bd9Sstevel@tonic-gate 18997c478bd9Sstevel@tonic-gate switch (cmd) { 19007c478bd9Sstevel@tonic-gate case TSTATIOC_GO: 19017c478bd9Sstevel@tonic-gate return (trapstat_go()); 19027c478bd9Sstevel@tonic-gate 19037c478bd9Sstevel@tonic-gate case TSTATIOC_NOGO: 19047c478bd9Sstevel@tonic-gate return (trapstat_option(TSTAT_OPT_NOGO)); 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate case TSTATIOC_STOP: 19077c478bd9Sstevel@tonic-gate return (trapstat_stop()); 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate case TSTATIOC_CPU: 19107c478bd9Sstevel@tonic-gate if (arg < 0 || arg > max_cpuid) 19117c478bd9Sstevel@tonic-gate return (EINVAL); 19127c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 19137c478bd9Sstevel@tonic-gate 19147c478bd9Sstevel@tonic-gate case TSTATIOC_NOCPU: 19157c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate if (tstat_running) { 19187c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19197c478bd9Sstevel@tonic-gate return (EBUSY); 19207c478bd9Sstevel@tonic-gate } 19217c478bd9Sstevel@tonic-gate 19227c478bd9Sstevel@tonic-gate /* 19237c478bd9Sstevel@tonic-gate * If this is the first CPU to be specified (or if we are 19247c478bd9Sstevel@tonic-gate * being asked to explicitly de-select CPUs), disable all CPUs. 19257c478bd9Sstevel@tonic-gate */ 19267c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_CPU) || cmd == TSTATIOC_NOCPU) { 19277c478bd9Sstevel@tonic-gate tstat_options |= TSTAT_OPT_CPU; 19287c478bd9Sstevel@tonic-gate 19297c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) { 19307c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[i]; 19317c478bd9Sstevel@tonic-gate 19327c478bd9Sstevel@tonic-gate ASSERT(cmd == TSTATIOC_NOCPU || 19337c478bd9Sstevel@tonic-gate (tcpu->tcpu_flags & TSTAT_CPU_SELECTED)); 19347c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_SELECTED; 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate } 19377c478bd9Sstevel@tonic-gate 19387c478bd9Sstevel@tonic-gate if (cmd == TSTATIOC_CPU) 19397c478bd9Sstevel@tonic-gate tstat_percpu[arg].tcpu_flags |= TSTAT_CPU_SELECTED; 19407c478bd9Sstevel@tonic-gate 19417c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19427c478bd9Sstevel@tonic-gate 19437c478bd9Sstevel@tonic-gate return (0); 19447c478bd9Sstevel@tonic-gate 19457c478bd9Sstevel@tonic-gate case TSTATIOC_ENTRY: 19467c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate if (tstat_running) { 19497c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19507c478bd9Sstevel@tonic-gate return (EBUSY); 19517c478bd9Sstevel@tonic-gate } 19527c478bd9Sstevel@tonic-gate 19537c478bd9Sstevel@tonic-gate if (arg >= TSTAT_NENT || arg < 0) { 19547c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19557c478bd9Sstevel@tonic-gate return (EINVAL); 19567c478bd9Sstevel@tonic-gate } 19577c478bd9Sstevel@tonic-gate 19587c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_ENTRY)) { 19597c478bd9Sstevel@tonic-gate /* 19607c478bd9Sstevel@tonic-gate * If this is the first entry that we are explicitly 19617c478bd9Sstevel@tonic-gate * enabling, explicitly disable every TL=0 entry. 19627c478bd9Sstevel@tonic-gate */ 19637c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_NENT; i++) 19647c478bd9Sstevel@tonic-gate tstat_enabled[i] = 0; 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate tstat_options |= TSTAT_OPT_ENTRY; 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate tstat_enabled[arg] = 1; 19707c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19717c478bd9Sstevel@tonic-gate return (0); 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate case TSTATIOC_NOENTRY: 19747c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 19757c478bd9Sstevel@tonic-gate 19767c478bd9Sstevel@tonic-gate if (tstat_running) { 19777c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19787c478bd9Sstevel@tonic-gate return (EBUSY); 19797c478bd9Sstevel@tonic-gate } 19807c478bd9Sstevel@tonic-gate 19817c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_NENT; i++) 19827c478bd9Sstevel@tonic-gate tstat_enabled[i] = 0; 19837c478bd9Sstevel@tonic-gate 19847c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19857c478bd9Sstevel@tonic-gate return (0); 19867c478bd9Sstevel@tonic-gate 19877c478bd9Sstevel@tonic-gate case TSTATIOC_READ: 19887c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 19897c478bd9Sstevel@tonic-gate 19907c478bd9Sstevel@tonic-gate if (tstat_options & TSTAT_OPT_TLBDATA) { 19917c478bd9Sstevel@tonic-gate dsize = tstat_data_t_exported_size; 19927c478bd9Sstevel@tonic-gate } else { 19937c478bd9Sstevel@tonic-gate dsize = sizeof (tstat_data_t); 19947c478bd9Sstevel@tonic-gate } 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate for (i = 0, out = 0; i <= max_cpuid; i++) { 19977c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[i]; 19987c478bd9Sstevel@tonic-gate 19997c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 20007c478bd9Sstevel@tonic-gate continue; 20017c478bd9Sstevel@tonic-gate 20027c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 20037c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 20047c478bd9Sstevel@tonic-gate 20057c478bd9Sstevel@tonic-gate tstat_buffer->tdata_cpuid = -1; 20067c478bd9Sstevel@tonic-gate xc_one(i, (xcfunc_t *)trapstat_snapshot, 0, 0); 20077c478bd9Sstevel@tonic-gate 20087c478bd9Sstevel@tonic-gate if (tstat_buffer->tdata_cpuid == -1) { 20097c478bd9Sstevel@tonic-gate /* 20107c478bd9Sstevel@tonic-gate * This CPU is not currently responding to 20117c478bd9Sstevel@tonic-gate * cross calls; we have caught it while it is 20127c478bd9Sstevel@tonic-gate * being unconfigured. We'll drop tstat_lock 20137c478bd9Sstevel@tonic-gate * and pick up and drop cpu_lock. By the 20147c478bd9Sstevel@tonic-gate * time we acquire cpu_lock, the DR operation 20157c478bd9Sstevel@tonic-gate * will appear consistent and we can assert 20167c478bd9Sstevel@tonic-gate * that trapstat_cpu_setup() has cleared 20177c478bd9Sstevel@tonic-gate * TSTAT_CPU_ENABLED. 20187c478bd9Sstevel@tonic-gate */ 20197c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20207c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 20217c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 20227c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 20237c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 20247c478bd9Sstevel@tonic-gate continue; 20257c478bd9Sstevel@tonic-gate } 20267c478bd9Sstevel@tonic-gate 20277c478bd9Sstevel@tonic-gate /* 20287c478bd9Sstevel@tonic-gate * Need to compensate for the difference between page 20297c478bd9Sstevel@tonic-gate * sizes exported to users and page sizes available 20307c478bd9Sstevel@tonic-gate * within the kernel. 20317c478bd9Sstevel@tonic-gate */ 20327c478bd9Sstevel@tonic-gate if ((tstat_options & TSTAT_OPT_TLBDATA) && 20337c478bd9Sstevel@tonic-gate (tstat_pgszs != tstat_user_pgszs)) { 20347c478bd9Sstevel@tonic-gate tstat_pgszdata_t *tp; 20357c478bd9Sstevel@tonic-gate uint_t szc; 20367c478bd9Sstevel@tonic-gate 20377c478bd9Sstevel@tonic-gate tp = &tstat_buffer->tdata_pgsz[0]; 20387c478bd9Sstevel@tonic-gate for (j = 0; j < tstat_user_pgszs; j++) { 20397c478bd9Sstevel@tonic-gate if ((szc = USERSZC_2_SZC(j)) != j) { 20407c478bd9Sstevel@tonic-gate bcopy(&tp[szc], &tp[j], 20417c478bd9Sstevel@tonic-gate sizeof (tstat_pgszdata_t)); 20427c478bd9Sstevel@tonic-gate } 20437c478bd9Sstevel@tonic-gate } 20447c478bd9Sstevel@tonic-gate } 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate if (copyout(tstat_buffer, (void *)arg, dsize) != 0) { 20477c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20487c478bd9Sstevel@tonic-gate return (EFAULT); 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate out++; 20527c478bd9Sstevel@tonic-gate arg += dsize; 20537c478bd9Sstevel@tonic-gate } 20547c478bd9Sstevel@tonic-gate 20557c478bd9Sstevel@tonic-gate if (out != max_cpuid + 1) { 20567c478bd9Sstevel@tonic-gate processorid_t cpuid = -1; 20577c478bd9Sstevel@tonic-gate arg += offsetof(tstat_data_t, tdata_cpuid); 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) { 20607c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20617c478bd9Sstevel@tonic-gate return (EFAULT); 20627c478bd9Sstevel@tonic-gate } 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20667c478bd9Sstevel@tonic-gate 20677c478bd9Sstevel@tonic-gate return (0); 20687c478bd9Sstevel@tonic-gate 20697c478bd9Sstevel@tonic-gate case TSTATIOC_TLBDATA: 20707c478bd9Sstevel@tonic-gate return (trapstat_option(TSTAT_OPT_TLBDATA)); 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate default: 20737c478bd9Sstevel@tonic-gate break; 20747c478bd9Sstevel@tonic-gate } 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate return (ENOTTY); 20777c478bd9Sstevel@tonic-gate } 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 20807c478bd9Sstevel@tonic-gate static int 20817c478bd9Sstevel@tonic-gate trapstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 20827c478bd9Sstevel@tonic-gate { 20837c478bd9Sstevel@tonic-gate int error; 20847c478bd9Sstevel@tonic-gate 20857c478bd9Sstevel@tonic-gate switch (infocmd) { 20867c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 20877c478bd9Sstevel@tonic-gate *result = (void *)tstat_devi; 20887c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 20897c478bd9Sstevel@tonic-gate break; 20907c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 20917c478bd9Sstevel@tonic-gate *result = (void *)0; 20927c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 20937c478bd9Sstevel@tonic-gate break; 20947c478bd9Sstevel@tonic-gate default: 20957c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 20967c478bd9Sstevel@tonic-gate } 20977c478bd9Sstevel@tonic-gate return (error); 20987c478bd9Sstevel@tonic-gate } 20997c478bd9Sstevel@tonic-gate 21007c478bd9Sstevel@tonic-gate static int 21017c478bd9Sstevel@tonic-gate trapstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 21027c478bd9Sstevel@tonic-gate { 21037c478bd9Sstevel@tonic-gate switch (cmd) { 21047c478bd9Sstevel@tonic-gate case DDI_ATTACH: 21057c478bd9Sstevel@tonic-gate break; 21067c478bd9Sstevel@tonic-gate 21077c478bd9Sstevel@tonic-gate case DDI_RESUME: 21087c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 21097c478bd9Sstevel@tonic-gate 21107c478bd9Sstevel@tonic-gate default: 21117c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 21127c478bd9Sstevel@tonic-gate } 21137c478bd9Sstevel@tonic-gate 21147c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "trapstat", S_IFCHR, 21157c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, 0) == DDI_FAILURE) { 21167c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 21177c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 21187c478bd9Sstevel@tonic-gate } 21197c478bd9Sstevel@tonic-gate 21207c478bd9Sstevel@tonic-gate ddi_report_dev(devi); 21217c478bd9Sstevel@tonic-gate tstat_devi = devi; 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate tstat_pgszs = page_num_pagesizes(); 21247c478bd9Sstevel@tonic-gate tstat_user_pgszs = page_num_user_pagesizes(); 21257c478bd9Sstevel@tonic-gate tstat_data_t_size = sizeof (tstat_data_t) + 21267c478bd9Sstevel@tonic-gate (tstat_pgszs - 1) * sizeof (tstat_pgszdata_t); 21277c478bd9Sstevel@tonic-gate tstat_data_t_exported_size = sizeof (tstat_data_t) + 21287c478bd9Sstevel@tonic-gate (tstat_user_pgszs - 1) * sizeof (tstat_pgszdata_t); 21297c478bd9Sstevel@tonic-gate #ifndef sun4v 21307c478bd9Sstevel@tonic-gate tstat_data_pages = (tstat_data_t_size >> MMU_PAGESHIFT) + 1; 21317c478bd9Sstevel@tonic-gate tstat_total_pages = TSTAT_INSTR_PAGES + tstat_data_pages; 21327c478bd9Sstevel@tonic-gate tstat_data_size = tstat_data_pages * MMU_PAGESIZE; 21337c478bd9Sstevel@tonic-gate tstat_total_size = TSTAT_INSTR_SIZE + tstat_data_size; 21347c478bd9Sstevel@tonic-gate #else 21357c478bd9Sstevel@tonic-gate tstat_data_pages = 0; 21367c478bd9Sstevel@tonic-gate tstat_data_size = tstat_data_t_size; 21377c478bd9Sstevel@tonic-gate tstat_total_pages = ((TSTAT_INSTR_SIZE + tstat_data_size) >> 21387c478bd9Sstevel@tonic-gate MMU_PAGESHIFT) + 1; 21397c478bd9Sstevel@tonic-gate tstat_total_size = tstat_total_pages * MMU_PAGESIZE; 21407c478bd9Sstevel@tonic-gate #endif 21417c478bd9Sstevel@tonic-gate 21427c478bd9Sstevel@tonic-gate tstat_percpu = kmem_zalloc((max_cpuid + 1) * 21437c478bd9Sstevel@tonic-gate sizeof (tstat_percpu_t), KM_SLEEP); 21447c478bd9Sstevel@tonic-gate 21457c478bd9Sstevel@tonic-gate /* 21467c478bd9Sstevel@tonic-gate * Create our own arena backed by segkmem to assure a source of 21477c478bd9Sstevel@tonic-gate * MMU_PAGESIZE-aligned allocations. We allocate out of the 21487c478bd9Sstevel@tonic-gate * heap32_arena to assure that we can address the allocated memory with 21497c478bd9Sstevel@tonic-gate * a single sethi/simm13 pair in the interposing trap table entries. 21507c478bd9Sstevel@tonic-gate */ 21517c478bd9Sstevel@tonic-gate tstat_arena = vmem_create("trapstat", NULL, 0, MMU_PAGESIZE, 21527c478bd9Sstevel@tonic-gate segkmem_alloc_permanent, segkmem_free, heap32_arena, 0, VM_SLEEP); 21537c478bd9Sstevel@tonic-gate 21547c478bd9Sstevel@tonic-gate tstat_enabled = kmem_alloc(TSTAT_TOTAL_NENT * sizeof (int), KM_SLEEP); 21557c478bd9Sstevel@tonic-gate tstat_buffer = kmem_alloc(tstat_data_t_size, KM_SLEEP); 21567c478bd9Sstevel@tonic-gate 21577c478bd9Sstevel@tonic-gate /* 21587c478bd9Sstevel@tonic-gate * CB_CL_CPR_POST_USER is the class that executes from cpr_resume() 21597c478bd9Sstevel@tonic-gate * after user threads can be restarted. By executing in this class, 21607c478bd9Sstevel@tonic-gate * we are assured of the availability of system services needed to 21617c478bd9Sstevel@tonic-gate * resume trapstat (specifically, we are assured that all CPUs are 21627c478bd9Sstevel@tonic-gate * restarted and responding to cross calls). 21637c478bd9Sstevel@tonic-gate */ 21647c478bd9Sstevel@tonic-gate tstat_cprcb = 21657c478bd9Sstevel@tonic-gate callb_add(trapstat_cpr, NULL, CB_CL_CPR_POST_USER, "trapstat"); 21667c478bd9Sstevel@tonic-gate 21677c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21707c478bd9Sstevel@tonic-gate static int 21717c478bd9Sstevel@tonic-gate trapstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 21727c478bd9Sstevel@tonic-gate { 21737c478bd9Sstevel@tonic-gate int rval; 21747c478bd9Sstevel@tonic-gate 21757c478bd9Sstevel@tonic-gate ASSERT(devi == tstat_devi); 21767c478bd9Sstevel@tonic-gate 21777c478bd9Sstevel@tonic-gate switch (cmd) { 21787c478bd9Sstevel@tonic-gate case DDI_DETACH: 21797c478bd9Sstevel@tonic-gate break; 21807c478bd9Sstevel@tonic-gate 21817c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 21827c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 21837c478bd9Sstevel@tonic-gate 21847c478bd9Sstevel@tonic-gate default: 21857c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 21867c478bd9Sstevel@tonic-gate } 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 21897c478bd9Sstevel@tonic-gate 21907c478bd9Sstevel@tonic-gate rval = callb_delete(tstat_cprcb); 21917c478bd9Sstevel@tonic-gate ASSERT(rval == 0); 21927c478bd9Sstevel@tonic-gate 21937c478bd9Sstevel@tonic-gate kmem_free(tstat_buffer, tstat_data_t_size); 21947c478bd9Sstevel@tonic-gate kmem_free(tstat_enabled, TSTAT_TOTAL_NENT * sizeof (int)); 21957c478bd9Sstevel@tonic-gate vmem_destroy(tstat_arena); 21967c478bd9Sstevel@tonic-gate kmem_free(tstat_percpu, (max_cpuid + 1) * sizeof (tstat_percpu_t)); 21977c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 22007c478bd9Sstevel@tonic-gate } 22017c478bd9Sstevel@tonic-gate 22027c478bd9Sstevel@tonic-gate /* 22037c478bd9Sstevel@tonic-gate * Configuration data structures 22047c478bd9Sstevel@tonic-gate */ 22057c478bd9Sstevel@tonic-gate static struct cb_ops trapstat_cb_ops = { 22067c478bd9Sstevel@tonic-gate trapstat_open, /* open */ 22077c478bd9Sstevel@tonic-gate trapstat_close, /* close */ 22087c478bd9Sstevel@tonic-gate nulldev, /* strategy */ 22097c478bd9Sstevel@tonic-gate nulldev, /* print */ 22107c478bd9Sstevel@tonic-gate nodev, /* dump */ 22117c478bd9Sstevel@tonic-gate nodev, /* read */ 22127c478bd9Sstevel@tonic-gate nodev, /* write */ 22137c478bd9Sstevel@tonic-gate trapstat_ioctl, /* ioctl */ 22147c478bd9Sstevel@tonic-gate nodev, /* devmap */ 22157c478bd9Sstevel@tonic-gate nodev, /* mmap */ 22167c478bd9Sstevel@tonic-gate nodev, /* segmap */ 22177c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 22187c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 22197c478bd9Sstevel@tonic-gate 0, /* streamtab */ 22207c478bd9Sstevel@tonic-gate D_MP | D_NEW /* Driver compatibility flag */ 22217c478bd9Sstevel@tonic-gate }; 22227c478bd9Sstevel@tonic-gate 22237c478bd9Sstevel@tonic-gate static struct dev_ops trapstat_ops = { 22247c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 22257c478bd9Sstevel@tonic-gate 0, /* refcnt */ 22267c478bd9Sstevel@tonic-gate trapstat_info, /* getinfo */ 22277c478bd9Sstevel@tonic-gate nulldev, /* identify */ 22287c478bd9Sstevel@tonic-gate nulldev, /* probe */ 22297c478bd9Sstevel@tonic-gate trapstat_attach, /* attach */ 22307c478bd9Sstevel@tonic-gate trapstat_detach, /* detach */ 22317c478bd9Sstevel@tonic-gate nulldev, /* reset */ 22327c478bd9Sstevel@tonic-gate &trapstat_cb_ops, /* cb_ops */ 22337c478bd9Sstevel@tonic-gate (struct bus_ops *)0, /* bus_ops */ 22347c478bd9Sstevel@tonic-gate }; 22357c478bd9Sstevel@tonic-gate 22367c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 22377c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 22387c478bd9Sstevel@tonic-gate "Trap Statistics", /* name of module */ 22397c478bd9Sstevel@tonic-gate &trapstat_ops, /* driver ops */ 22407c478bd9Sstevel@tonic-gate }; 22417c478bd9Sstevel@tonic-gate 22427c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 22437c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 22447c478bd9Sstevel@tonic-gate }; 22457c478bd9Sstevel@tonic-gate 22467c478bd9Sstevel@tonic-gate int 22477c478bd9Sstevel@tonic-gate _init(void) 22487c478bd9Sstevel@tonic-gate { 22497c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 22507c478bd9Sstevel@tonic-gate } 22517c478bd9Sstevel@tonic-gate 22527c478bd9Sstevel@tonic-gate int 22537c478bd9Sstevel@tonic-gate _fini(void) 22547c478bd9Sstevel@tonic-gate { 22557c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 22567c478bd9Sstevel@tonic-gate } 22577c478bd9Sstevel@tonic-gate 22587c478bd9Sstevel@tonic-gate int 22597c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 22607c478bd9Sstevel@tonic-gate { 22617c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 22627c478bd9Sstevel@tonic-gate } 2263