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