17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 525cf1a30Sjl139090 * Common Development and Distribution License (the "License"). 625cf1a30Sjl139090 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 224df55fdeSJanie Lu * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/systm.h> 287c478bd9Sstevel@tonic-gate #include <sys/conf.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 327c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 337c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h> 347c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h> 357c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 367c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h> 377c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 387c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 397c478bd9Sstevel@tonic-gate #include <sys/machasi.h> 407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 417c478bd9Sstevel@tonic-gate #include <sys/callb.h> 427c478bd9Sstevel@tonic-gate #include <sys/archsystm.h> 437c478bd9Sstevel@tonic-gate #include <sys/trapstat.h> 447c478bd9Sstevel@tonic-gate #ifdef sun4v 457c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h> 467c478bd9Sstevel@tonic-gate #endif 4725cf1a30Sjl139090 #ifndef sun4v 48fb2f18f8Sesaxe #include <sys/pghw.h> 4925cf1a30Sjl139090 #endif 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */ 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * trapstat: Trap Statistics through Dynamic Trap Table Interposition 547c478bd9Sstevel@tonic-gate * ------------------------------------------------------------------- 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * Motivation and Overview 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * Despite being a fundamental indicator of system behavior, there has 597c478bd9Sstevel@tonic-gate * historically been very little insight provided into the frequency and cost 607c478bd9Sstevel@tonic-gate * of machine-specific traps. The lack of insight has been especially acute 617c478bd9Sstevel@tonic-gate * on UltraSPARC microprocessors: because these microprocessors handle TLB 627c478bd9Sstevel@tonic-gate * misses as software traps, the frequency and duration of traps play a 637c478bd9Sstevel@tonic-gate * decisive role in the performance of the memory system. As applications have 647c478bd9Sstevel@tonic-gate * increasingly outstripped TLB reach, this has become increasingly true. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * Part of the difficulty of observing trap behavior is that the trap handlers 677c478bd9Sstevel@tonic-gate * are so frequently called (e.g. millions of times per second) that any 687c478bd9Sstevel@tonic-gate * permanently enabled instrumentation would induce an unacceptable performance 697c478bd9Sstevel@tonic-gate * degradation. Thus, it is a constraint on any trap observability 707c478bd9Sstevel@tonic-gate * infrastructure that it have no probe effect when not explicitly enabled. 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * The basic idea, then, is to create an interposing trap table in which each 737c478bd9Sstevel@tonic-gate * entry increments a per-trap, in-memory counter and then jumps to the actual, 747c478bd9Sstevel@tonic-gate * underlying trap table entry. To enable trapstat, we atomically write to the 757c478bd9Sstevel@tonic-gate * trap base address (%tba) register to point to our interposing trap table. 767c478bd9Sstevel@tonic-gate * (Note that per-CPU statistics fall out by creating a different trap table 777c478bd9Sstevel@tonic-gate * for each CPU.) 787c478bd9Sstevel@tonic-gate * 797c478bd9Sstevel@tonic-gate * Implementation Details 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * While the idea is straight-forward, a nuance of SPARC V9 slightly 827c478bd9Sstevel@tonic-gate * complicates the implementation. Unlike its predecessors, SPARC V9 supports 837c478bd9Sstevel@tonic-gate * the notion of nested traps. The trap level is kept in the TL register: 847c478bd9Sstevel@tonic-gate * during normal operation it is 0; when a trap is taken, the TL register is 857c478bd9Sstevel@tonic-gate * incremented by 1. To aid system software, SPARC V9 breaks the trap table 867c478bd9Sstevel@tonic-gate * into two halves: the lower half contains the trap handlers for traps taken 877c478bd9Sstevel@tonic-gate * when TL is 0; the upper half contains the trap handlers for traps taken 887c478bd9Sstevel@tonic-gate * when TL is greater than 0. Each half is further subdivided into two 897c478bd9Sstevel@tonic-gate * subsequent halves: the lower half contains the trap handlers for traps 907c478bd9Sstevel@tonic-gate * other than those induced by the trap instruction (Tcc variants); the upper 917c478bd9Sstevel@tonic-gate * half contains the trap handlers for traps induced by the trap instruction. 927c478bd9Sstevel@tonic-gate * This gives a total of four ranges, with each range containing 256 traps: 937c478bd9Sstevel@tonic-gate * 947c478bd9Sstevel@tonic-gate * +--------------------------------+- 3ff 957c478bd9Sstevel@tonic-gate * | | . 967c478bd9Sstevel@tonic-gate * | Trap instruction, TL>0 | . 977c478bd9Sstevel@tonic-gate * | | . 987c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 300 997c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 2ff 1007c478bd9Sstevel@tonic-gate * | | . 1017c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . 1027c478bd9Sstevel@tonic-gate * | | . 1037c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 1047c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff 1057c478bd9Sstevel@tonic-gate * | | . 1067c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . 1077c478bd9Sstevel@tonic-gate * | | . 1087c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 1097c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff 1107c478bd9Sstevel@tonic-gate * | | . 1117c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . 1127c478bd9Sstevel@tonic-gate * | | . 1137c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 1147c478bd9Sstevel@tonic-gate * 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * Solaris, however, doesn't have reason to support trap instructions when 1177c478bd9Sstevel@tonic-gate * TL>0 (only privileged code may execute at TL>0; not supporting this only 1187c478bd9Sstevel@tonic-gate * constrains our own implementation). The trap table actually looks like: 1197c478bd9Sstevel@tonic-gate * 1207c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff 1217c478bd9Sstevel@tonic-gate * | | . 1227c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . 1237c478bd9Sstevel@tonic-gate * | | . 1247c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 1257c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff 1267c478bd9Sstevel@tonic-gate * | | . 1277c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . 1287c478bd9Sstevel@tonic-gate * | | . 1297c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 1307c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff 1317c478bd9Sstevel@tonic-gate * | | . 1327c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . 1337c478bd9Sstevel@tonic-gate * | | . 1347c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 1357c478bd9Sstevel@tonic-gate * 1367c478bd9Sstevel@tonic-gate * Putatively to aid system software, SPARC V9 has the notion of multiple 1377c478bd9Sstevel@tonic-gate * sets of global registers. UltraSPARC defines four sets of global 1387c478bd9Sstevel@tonic-gate * registers: 1397c478bd9Sstevel@tonic-gate * 1407c478bd9Sstevel@tonic-gate * Normal Globals 1417c478bd9Sstevel@tonic-gate * Alternate Globals (AGs) 1427c478bd9Sstevel@tonic-gate * MMU Globals (MGs) 1437c478bd9Sstevel@tonic-gate * Interrupt Globals (IGs) 1447c478bd9Sstevel@tonic-gate * 1457c478bd9Sstevel@tonic-gate * The set of globals in use is controlled by bits in PSTATE; when TL is 0 1467c478bd9Sstevel@tonic-gate * (and PSTATE has not been otherwise explicitly modified), the Normal Globals 1477c478bd9Sstevel@tonic-gate * are in use. When a trap is issued, PSTATE is modified to point to a set of 1487c478bd9Sstevel@tonic-gate * globals corresponding to the trap type. Most traps correspond to the 1497c478bd9Sstevel@tonic-gate * Alternate Globals, with a minority corresponding to the MMU Globals, and 1507c478bd9Sstevel@tonic-gate * only the interrupt-vector trap (vector 0x60) corresponding to the Interrupt 1517c478bd9Sstevel@tonic-gate * Globals. (The complete mapping can be found in the UltraSPARC I&II User's 1527c478bd9Sstevel@tonic-gate * Manual.) 1537c478bd9Sstevel@tonic-gate * 1547c478bd9Sstevel@tonic-gate * Note that the sets of globals are per trap _type_, not per trap _level_. 1557c478bd9Sstevel@tonic-gate * Thus, when executing a TL>0 trap handler, one may not have registers 1567c478bd9Sstevel@tonic-gate * available (for example, both trap-instruction traps and spill traps execute 1577c478bd9Sstevel@tonic-gate * on the alternate globals; if a trap-instruction trap induces a window spill, 1587c478bd9Sstevel@tonic-gate * the window spill handler has no available globals). For trapstat, this is 1597c478bd9Sstevel@tonic-gate * problematic: a register is required to transfer control from one arbitrary 1607c478bd9Sstevel@tonic-gate * location (in the interposing trap table) to another (in the actual trap 1617c478bd9Sstevel@tonic-gate * table). 1627c478bd9Sstevel@tonic-gate * 1637c478bd9Sstevel@tonic-gate * We solve this problem by exploiting the trap table's location at the bottom 1647c478bd9Sstevel@tonic-gate * of valid kernel memory (i.e. at KERNELBASE). We locate the interposing trap 1657c478bd9Sstevel@tonic-gate * tables just below KERNELBASE -- thereby allowing us to use a branch-always 1667c478bd9Sstevel@tonic-gate * instruction (ba) instead of a jump instruction (jmp) to transfer control 1677c478bd9Sstevel@tonic-gate * from the TL>0 entries in the interposing trap table to the TL>0 entries in 1687c478bd9Sstevel@tonic-gate * the actual trap table. (N.B. while this allows trap table interposition to 1697c478bd9Sstevel@tonic-gate * work, it necessarily limits trapstat to only recording information about 1707c478bd9Sstevel@tonic-gate * TL=0 traps -- there is no way to increment a counter without using a 1717c478bd9Sstevel@tonic-gate * register.) Diagrammatically: 1727c478bd9Sstevel@tonic-gate * 1737c478bd9Sstevel@tonic-gate * Actual trap table: 1747c478bd9Sstevel@tonic-gate * 1757c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff 1767c478bd9Sstevel@tonic-gate * | | . 1777c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL>0 | . <-----------------------+ 1787c478bd9Sstevel@tonic-gate * | | . <-----------------------|-+ 1797c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 <-----------------------|-|-+ 1807c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 1ff | | | 1817c478bd9Sstevel@tonic-gate * | | . | | | 1827c478bd9Sstevel@tonic-gate * | Trap instruction, TL=0 | . <-----------------+ | | | 1837c478bd9Sstevel@tonic-gate * | | . <-----------------|-+ | | | 1847c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 <-----------------|-|-+ | | | 1857c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 0ff | | | | | | 1867c478bd9Sstevel@tonic-gate * | | . | | | | | | 1877c478bd9Sstevel@tonic-gate * | Non-trap instruction, TL=0 | . <-----------+ | | | | | | 1887c478bd9Sstevel@tonic-gate * | | . <-----------|-+ | | | | | | 1897c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 <-----------|-|-+ | | | | | | 1907c478bd9Sstevel@tonic-gate * KERNELBASE | | | | | | | | | 1917c478bd9Sstevel@tonic-gate * | | | | | | | | | 1927c478bd9Sstevel@tonic-gate * | | | | | | | | | 1937c478bd9Sstevel@tonic-gate * Interposing trap table: | | | | | | | | | 1947c478bd9Sstevel@tonic-gate * | | | | | | | | | 1957c478bd9Sstevel@tonic-gate * +--------------------------------+- 2ff | | | | | | | | | 1967c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 1977c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 1987c478bd9Sstevel@tonic-gate * | ... | . | | | | | | | | | 1997c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 203 | | | | | | | | | 2007c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|-+ | | 2017c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 202 | | | | | | | | 2027c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|---+ | 2037c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 201 | | | | | | | 2047c478bd9Sstevel@tonic-gate * | ba,a | -------------|-|-|-|-|-|-----+ 2057c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 200 | | | | | | 2067c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2077c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2087c478bd9Sstevel@tonic-gate * | ... | . | | | | | | 2097c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 103 | | | | | | 2107c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | | | 2117c478bd9Sstevel@tonic-gate * | ba,a | -------------------+ | | 2127c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 102 | | | | | 2137c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | | 2147c478bd9Sstevel@tonic-gate * | ba,a | ---------------------+ | 2157c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 101 | | | | 2167c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | | 2177c478bd9Sstevel@tonic-gate * | ba,a | -----------------------+ 2187c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 100 | | | 2197c478bd9Sstevel@tonic-gate * | ... | . | | | 2207c478bd9Sstevel@tonic-gate * | ... | . | | | 2217c478bd9Sstevel@tonic-gate * | ... | . | | | 2227c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 003 | | | 2237c478bd9Sstevel@tonic-gate * | (Increment counter) | | | | 2247c478bd9Sstevel@tonic-gate * | ba,a | -------------+ | | 2257c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 002 | | 2267c478bd9Sstevel@tonic-gate * | (Increment counter) | | | 2277c478bd9Sstevel@tonic-gate * | ba,a | ---------------+ | 2287c478bd9Sstevel@tonic-gate * |- - - - - - - - - - - - - - - - +- 001 | 2297c478bd9Sstevel@tonic-gate * | (Increment counter) | | 2307c478bd9Sstevel@tonic-gate * | ba,a | -----------------+ 2317c478bd9Sstevel@tonic-gate * +--------------------------------+- 000 2327c478bd9Sstevel@tonic-gate * KERNELBASE - tstat_total_size 2337c478bd9Sstevel@tonic-gate * 2347c478bd9Sstevel@tonic-gate * tstat_total_size is the number of pages required for each trap table. It 2357c478bd9Sstevel@tonic-gate * must be true that KERNELBASE - tstat_total_size is less than the maximum 2367c478bd9Sstevel@tonic-gate * branch displacement; if each CPU were to consume a disjoint virtual range 2377c478bd9Sstevel@tonic-gate * below KERNELBASE for its trap table, we could support at most 2387c478bd9Sstevel@tonic-gate * (maximum_branch_displacement / tstat_total_size) CPUs. The maximum branch 2397c478bd9Sstevel@tonic-gate * displacement for Bicc variants is just under eight megabytes, and (because 2407c478bd9Sstevel@tonic-gate * the %tba must be 32K aligned), tstat_total_size must be at least 32K; if 2417c478bd9Sstevel@tonic-gate * each CPU were to consume a disjoint virtual range, we would have an 2427c478bd9Sstevel@tonic-gate * unacceptably low upper bound of 256 CPUs. 2437c478bd9Sstevel@tonic-gate * 2447c478bd9Sstevel@tonic-gate * While there are tricks that one could use to address this constraint (e.g., 2457c478bd9Sstevel@tonic-gate * creating trampolines every maximum_branch_displacement bytes), we instead 2467c478bd9Sstevel@tonic-gate * solve this by not permitting each CPU to consume a disjoint virtual range. 2477c478bd9Sstevel@tonic-gate * Rather, we have each CPU's interposing trap table use the _same_ virtual 2487c478bd9Sstevel@tonic-gate * range, but we back the trap tables with disjoint physical memory. Normally, 2497c478bd9Sstevel@tonic-gate * such one-to-many virtual-to-physical mappings are illegal; this is 2507c478bd9Sstevel@tonic-gate * permissible here only because the pages for the interposing trap table are 2517c478bd9Sstevel@tonic-gate * necessarily locked in the TLB. (The CPUs thus never have the opportunity to 2527c478bd9Sstevel@tonic-gate * discover that they have conflicting translations.) 2537c478bd9Sstevel@tonic-gate * 2547c478bd9Sstevel@tonic-gate * On CMT architectures in which CPUs can share MMUs, the above trick will not 2557c478bd9Sstevel@tonic-gate * work: two CPUs that share an MMU cannot have the same virtual address map 2567c478bd9Sstevel@tonic-gate * to disjoint physical pages. On these architectures, any CPUs sharing the 2577c478bd9Sstevel@tonic-gate * same MMU must consume a disjoint 32K virtual address range -- limiting the 2587c478bd9Sstevel@tonic-gate * number of CPUs sharing an MMU on these architectures to 256 due to the 2597c478bd9Sstevel@tonic-gate * branch displacement limitation described above. On the sun4v architecture, 2607c478bd9Sstevel@tonic-gate * there is a further limitation: a guest may not have more than eight locked 2617c478bd9Sstevel@tonic-gate * TLB entries per MMU. To allow operation under this restriction, the 2627c478bd9Sstevel@tonic-gate * interposing trap table and the trap statistics are each accessed through 2637c478bd9Sstevel@tonic-gate * a single 4M TLB entry. This limits the footprint to two locked entries 2647c478bd9Sstevel@tonic-gate * (one for the I-TLB and one for the D-TLB), but further restricts the number 2657c478bd9Sstevel@tonic-gate * of CPUs to 128 per MMU. However, support for more than 128 CPUs can easily 2667c478bd9Sstevel@tonic-gate * be added via a hybrid scheme, where the same 4M virtual address is used 2677c478bd9Sstevel@tonic-gate * on different MMUs. 2687c478bd9Sstevel@tonic-gate * 2694df55fdeSJanie Lu * On sun4v architecture, we cannot use the hybrid scheme as the architecture 2704df55fdeSJanie Lu * imposes additional restriction on the number of permanent mappings per 2714df55fdeSJanie Lu * guest and it is illegal to use the same virtual address to map different 2724df55fdeSJanie Lu * TTEs on different MMUs. Instead, we increase the number of supported CPUs 2734df55fdeSJanie Lu * by reducing the virtual address space requirements per CPU via shared 2744df55fdeSJanie Lu * interposing trap table as follows: 27559ac0c16Sdavemq * 27659ac0c16Sdavemq * Offset (within 4MB page) 27759ac0c16Sdavemq * +------------------------------------+- 0x400000 2784df55fdeSJanie Lu * | CPU 1015 trap statistics (4KB) | . 2794df55fdeSJanie Lu * |- - - - - - - - - - - - - - - - - - +- 0x3ff000 28059ac0c16Sdavemq * | | 28159ac0c16Sdavemq * | ... | 28259ac0c16Sdavemq * | | 28359ac0c16Sdavemq * |- - - - - - - - - - - - - - - - - - +- 0x00a000 2844df55fdeSJanie Lu * | CPU 1 trap statistics (4KB) | . 2854df55fdeSJanie Lu * |- - - - - - - - - - - - - - - - - - +- 0x009000 2864df55fdeSJanie Lu * | CPU 0 trap statistics (4KB) | . 28759ac0c16Sdavemq * |- - - - - - - - - - - - - - - - - - +- 0x008000 28859ac0c16Sdavemq * | Shared trap handler continuation | . 28959ac0c16Sdavemq * |- - - - - - - - - - - - - - - - - - +- 0x006000 29059ac0c16Sdavemq * | Non-trap instruction, TL>0 | . 29159ac0c16Sdavemq * |- - - - - - - - - - - - - - - - - - +- 0x004000 29259ac0c16Sdavemq * | Trap instruction, TL=0 | . 29359ac0c16Sdavemq * |- - - - - - - - - - - - - - - - - - +- 0x002000 29459ac0c16Sdavemq * | Non-trap instruction, TL=0 | . 29559ac0c16Sdavemq * +------------------------------------+- 0x000000 29659ac0c16Sdavemq * 2974df55fdeSJanie Lu * Note that each CPU has its own 4K space for its trap statistics but 29859ac0c16Sdavemq * shares the same interposing trap handlers. Interposing trap handlers 29959ac0c16Sdavemq * use the CPU ID to determine the location of per CPU trap statistics 30059ac0c16Sdavemq * area dynamically. This increases the interposing trap handler overhead, 3014df55fdeSJanie Lu * but is acceptable as it allows us to support up to 1016 CPUs with one 30259ac0c16Sdavemq * 4MB page on sun4v architecture. Support for additional CPUs can be 3034df55fdeSJanie Lu * added with another 4MB page to 2040 cpus (or 3064 cpus with 2 additional 3044df55fdeSJanie Lu * 4MB pages). With additional 4MB pages, we cannot use displacement branch 3054df55fdeSJanie Lu * (ba instruction) and we have to use jmp instruction instead. Note that 3064df55fdeSJanie Lu * with sun4v, globals are nested (not per-trap type as in sun4u), so it is 3074df55fdeSJanie Lu * ok to use additional global reg to do jmp. This option is not available in 3084df55fdeSJanie Lu * sun4u which mandates the usage of displacement branches since no global reg 3094df55fdeSJanie Lu * is available at TL>1 3107c478bd9Sstevel@tonic-gate * 3117c478bd9Sstevel@tonic-gate * TLB Statistics 3127c478bd9Sstevel@tonic-gate * 3137c478bd9Sstevel@tonic-gate * Because TLB misses are an important component of system performance, we wish 3147c478bd9Sstevel@tonic-gate * to know much more about these traps than simply the number received. 3157c478bd9Sstevel@tonic-gate * Specifically, we wish to know: 3167c478bd9Sstevel@tonic-gate * 3177c478bd9Sstevel@tonic-gate * (a) The amount of time spent executing the TLB miss handler 3187c478bd9Sstevel@tonic-gate * (b) TLB misses versus TSB misses 3197c478bd9Sstevel@tonic-gate * (c) Kernel-level misses versus user-level misses 3207c478bd9Sstevel@tonic-gate * (d) Misses per pagesize 3217c478bd9Sstevel@tonic-gate * 3227c478bd9Sstevel@tonic-gate * TLB Statistics: Time Spent Executing 3237c478bd9Sstevel@tonic-gate * 3247c478bd9Sstevel@tonic-gate * To accurately determine the amount of time spent executing the TLB miss 3257c478bd9Sstevel@tonic-gate * handler, one must get a timestamp on trap entry and trap exit, subtract the 3267c478bd9Sstevel@tonic-gate * latter from the former, and add the result to an accumulating count. 3277c478bd9Sstevel@tonic-gate * Consider flow of control during normal TLB miss processing (where "ldx 3287c478bd9Sstevel@tonic-gate * [%g2], %g2" is an arbitrary TLB-missing instruction): 3297c478bd9Sstevel@tonic-gate * 3307c478bd9Sstevel@tonic-gate * + - - - - - - - -+ 3317c478bd9Sstevel@tonic-gate * : : 3327c478bd9Sstevel@tonic-gate * : ldx [%g2], %g2 :<-------------------------------------------------------+ 3337c478bd9Sstevel@tonic-gate * : : Return from trap: | 3347c478bd9Sstevel@tonic-gate * + - - - - - - - -+ TL <- TL - 1 (0) | 3357c478bd9Sstevel@tonic-gate * | %pc <- TSTATE[TL].TPC (address of load) | 3367c478bd9Sstevel@tonic-gate * | TLB miss: | 3377c478bd9Sstevel@tonic-gate * | TL <- TL + 1 (1) | 3387c478bd9Sstevel@tonic-gate * | %pc <- TLB-miss-trap-handler | 3397c478bd9Sstevel@tonic-gate * | | 3407c478bd9Sstevel@tonic-gate * v | 3417c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + | 3427c478bd9Sstevel@tonic-gate * : : | 3437c478bd9Sstevel@tonic-gate * : Lookup VA in TSB : | 3447c478bd9Sstevel@tonic-gate * : If (hit) : | 3457c478bd9Sstevel@tonic-gate * : Fill TLB : | 3467c478bd9Sstevel@tonic-gate * : Else : | 3477c478bd9Sstevel@tonic-gate * : Lookup VA (hme hash table : | 3487c478bd9Sstevel@tonic-gate * : or segkpm) : | 3497c478bd9Sstevel@tonic-gate * : Fill TLB : | 3507c478bd9Sstevel@tonic-gate * : Endif : | 3517c478bd9Sstevel@tonic-gate * : Issue "retry" ---------------------------------------------------------+ 3527c478bd9Sstevel@tonic-gate * : : 3537c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + 3547c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler 3557c478bd9Sstevel@tonic-gate * 3567c478bd9Sstevel@tonic-gate * 3577c478bd9Sstevel@tonic-gate * As the above diagram indicates, interposing on the trap table allows one 3587c478bd9Sstevel@tonic-gate * only to determine a timestamp on trap _entry_: when the TLB miss handler 3597c478bd9Sstevel@tonic-gate * has completed filling the TLB, a "retry" will be issued, and control will 3607c478bd9Sstevel@tonic-gate * transfer immediately back to the missing %pc. 3617c478bd9Sstevel@tonic-gate * 3627c478bd9Sstevel@tonic-gate * To obtain a timestamp on trap exit, we must then somehow interpose between 3637c478bd9Sstevel@tonic-gate * the "retry" and the subsequent control transfer to the TLB-missing 3647c478bd9Sstevel@tonic-gate * instruction. To do this, we _push_ a trap level. The basic idea is to 3657c478bd9Sstevel@tonic-gate * spoof a TLB miss by raising TL, setting the %tpc to be within text 3667c478bd9Sstevel@tonic-gate * controlled by trapstat (the "TLB return entry") and branching to the 3677c478bd9Sstevel@tonic-gate * underlying TLB miss handler. When the TLB miss handler issues its "retry", 3687c478bd9Sstevel@tonic-gate * control will transfer not to the TLB-missing instruction, but rather to the 3697c478bd9Sstevel@tonic-gate * TLB return entry. This code can then obtain a timestamp, and issue its own 3707c478bd9Sstevel@tonic-gate * "retry" -- thereby correctly returning to the TLB-missing instruction. 3717c478bd9Sstevel@tonic-gate * Here is the above TLB miss flow control diagram modified to reflect 3727c478bd9Sstevel@tonic-gate * trapstat's operation: 3737c478bd9Sstevel@tonic-gate * 3747c478bd9Sstevel@tonic-gate * + - - - - - - - -+ 3757c478bd9Sstevel@tonic-gate * : : 3767c478bd9Sstevel@tonic-gate * : ldx [%g2], %g2 :<-------------------------------------------------------+ 3777c478bd9Sstevel@tonic-gate * : : Return from trap: | 3787c478bd9Sstevel@tonic-gate * + - - - - - - - -+ TL <- TL - 1 (0) | 3797c478bd9Sstevel@tonic-gate * | %pc <- TSTATE[TL].TPC (address of load) | 3807c478bd9Sstevel@tonic-gate * | TLB miss: | 3817c478bd9Sstevel@tonic-gate * | TL <- TL + 1 (1) | 3827c478bd9Sstevel@tonic-gate * | %pc <- TLB-miss-trap-handler (trapstat) | 3837c478bd9Sstevel@tonic-gate * | | 3847c478bd9Sstevel@tonic-gate * v TLB-return-entry (trapstat) | 3857c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - + | 3867c478bd9Sstevel@tonic-gate * : : : : | 3877c478bd9Sstevel@tonic-gate * : Record timestamp : : Record timestamp : | 3887c478bd9Sstevel@tonic-gate * : TL <- 2 : : Take timestamp difference : | 3897c478bd9Sstevel@tonic-gate * : TSTATE[1].TPC <- TLB-return-entry : : Add to running total : | 3907c478bd9Sstevel@tonic-gate * : ba,a TLB-miss-trap-handler -----------+ : Issue "retry" --------------+ 3917c478bd9Sstevel@tonic-gate * : : | : : 3927c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - - - - + | + - - - - - - - - - - - - - + 3937c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler | ^ 3947c478bd9Sstevel@tonic-gate * (trapstat) | | 3957c478bd9Sstevel@tonic-gate * | | 3967c478bd9Sstevel@tonic-gate * | | 3977c478bd9Sstevel@tonic-gate * +-----------------------+ | 3987c478bd9Sstevel@tonic-gate * | | 3997c478bd9Sstevel@tonic-gate * | | 4007c478bd9Sstevel@tonic-gate * v | 4017c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + | 4027c478bd9Sstevel@tonic-gate * : : | 4037c478bd9Sstevel@tonic-gate * : Lookup VA in TSB : | 4047c478bd9Sstevel@tonic-gate * : If (hit) : | 4057c478bd9Sstevel@tonic-gate * : Fill TLB : | 4067c478bd9Sstevel@tonic-gate * : Else : | 4077c478bd9Sstevel@tonic-gate * : Lookup VA (hme hash table : | 4087c478bd9Sstevel@tonic-gate * : or segkpm) : | 4097c478bd9Sstevel@tonic-gate * : Fill TLB : | 4107c478bd9Sstevel@tonic-gate * : Endif : | 4117c478bd9Sstevel@tonic-gate * : Issue "retry" ------------------------------------------+ 4127c478bd9Sstevel@tonic-gate * : : Return from trap: 4137c478bd9Sstevel@tonic-gate * + - - - - - - - - - - - - - - - + TL <- TL - 1 (1) 4147c478bd9Sstevel@tonic-gate * TLB-miss-trap-handler %pc <- TSTATE[TL].TPC (TLB-return-entry) 4157c478bd9Sstevel@tonic-gate * 4167c478bd9Sstevel@tonic-gate * 4177c478bd9Sstevel@tonic-gate * A final subterfuge is required to complete our artifice: if we miss in 4187c478bd9Sstevel@tonic-gate * the TLB, the TSB _and_ the subsequent hash or segkpm lookup (that is, if 4197c478bd9Sstevel@tonic-gate * there is no valid translation for the TLB-missing address), common system 4207c478bd9Sstevel@tonic-gate * software will need to accurately determine the %tpc as part of its page 4217c478bd9Sstevel@tonic-gate * fault handling. We therefore modify the kernel to check the %tpc in this 4227c478bd9Sstevel@tonic-gate * case: if the %tpc falls within the VA range controlled by trapstat and 4237c478bd9Sstevel@tonic-gate * the TL is 2, TL is simply lowered back to 1 (this check is implemented 4247c478bd9Sstevel@tonic-gate * by the TSTAT_CHECK_TL1 macro). Lowering TL to 1 has the effect of 4257c478bd9Sstevel@tonic-gate * discarding the state pushed by trapstat. 4267c478bd9Sstevel@tonic-gate * 4277c478bd9Sstevel@tonic-gate * TLB Statistics: TLB Misses versus TSB Misses 4287c478bd9Sstevel@tonic-gate * 4297c478bd9Sstevel@tonic-gate * Distinguishing TLB misses from TSB misses requires further interposition 4307c478bd9Sstevel@tonic-gate * on the TLB miss handler: we cannot know a priori or a posteriori if a 4317c478bd9Sstevel@tonic-gate * given VA will or has hit in the TSB. 4327c478bd9Sstevel@tonic-gate * 4337c478bd9Sstevel@tonic-gate * We achieve this distinction by adding a second TLB return entry almost 4347c478bd9Sstevel@tonic-gate * identical to the first -- differing only in the address to which it 4357c478bd9Sstevel@tonic-gate * stores its results. We then modify the TLB miss handlers of the kernel 4367c478bd9Sstevel@tonic-gate * such that they check the %tpc when they determine that a TLB miss has 4377c478bd9Sstevel@tonic-gate * subsequently missed in the TSB: if the %tpc lies within trapstat's VA 4387c478bd9Sstevel@tonic-gate * range and TL is 2 (that is, if trapstat is running), the TLB miss handler 4397c478bd9Sstevel@tonic-gate * _increments_ the %tpc by the size of the TLB return entry. The ensuing 4407c478bd9Sstevel@tonic-gate * "retry" will thus transfer control to the second TLB return entry, and 4417c478bd9Sstevel@tonic-gate * the time spent in the handler will be accumulated in a memory location 4427c478bd9Sstevel@tonic-gate * specific to TSB misses. 4437c478bd9Sstevel@tonic-gate * 4447c478bd9Sstevel@tonic-gate * N.B.: To minimize the amount of knowledge the kernel must have of trapstat, 4457c478bd9Sstevel@tonic-gate * we do not allow the kernel to hard-code the size of the TLB return entry. 4467c478bd9Sstevel@tonic-gate * Rather, the actual tsbmiss handler executes a known instruction at the 4477c478bd9Sstevel@tonic-gate * corresponding tsbmiss patch points (see the tstat_tsbmiss_patch_table) with 4487c478bd9Sstevel@tonic-gate * the %tpc in %g7: when trapstat is not running, these points contain the 4497c478bd9Sstevel@tonic-gate * harmless TSTAT_TSBMISS_INSTR instruction ("add %g7, 0, %g7"). Before 4507c478bd9Sstevel@tonic-gate * running, trapstat modifies the instructions at these patch points such 4517c478bd9Sstevel@tonic-gate * that the simm13 equals the size of the TLB return entry. 4527c478bd9Sstevel@tonic-gate * 4537c478bd9Sstevel@tonic-gate * TLB Statistics: Kernel-level Misses versus User-level Misses 4547c478bd9Sstevel@tonic-gate * 4557c478bd9Sstevel@tonic-gate * Differentiating user-level misses from kernel-level misses employs a 4567c478bd9Sstevel@tonic-gate * similar technique, but is simplified by the ability to distinguish a 4577c478bd9Sstevel@tonic-gate * user-level miss from a kernel-level miss a priori by reading the context 4587c478bd9Sstevel@tonic-gate * register: we implement kernel-/user-level differentiation by again doubling 4597c478bd9Sstevel@tonic-gate * the number of TLB return entries, and setting the %tpc to the appropriate 4607c478bd9Sstevel@tonic-gate * TLB return entry in trapstat's TLB miss handler. Together with the doubling 4617c478bd9Sstevel@tonic-gate * of entries required for TLB-miss/TSB-miss differentiation, this yields a 4627c478bd9Sstevel@tonic-gate * total of four TLB return entries: 4637c478bd9Sstevel@tonic-gate * 4647c478bd9Sstevel@tonic-gate * Level TSB hit? Structure member 4657c478bd9Sstevel@tonic-gate * ------------------------------------------------------------ 4667c478bd9Sstevel@tonic-gate * Kernel Yes tstat_tlbret_t.ttlbr_ktlb 4677c478bd9Sstevel@tonic-gate * Kernel No tstat_tlbret_t.ttlbr_ktsb 4687c478bd9Sstevel@tonic-gate * User Yes tstat_tlbret_t.ttlbr_utlb 4697c478bd9Sstevel@tonic-gate * User No tstat_tlbret_t.ttlbr_utsb 4707c478bd9Sstevel@tonic-gate * 4717c478bd9Sstevel@tonic-gate * TLB Statistics: Misses per Pagesize 4727c478bd9Sstevel@tonic-gate * 4737c478bd9Sstevel@tonic-gate * As with the TLB-/TSB-miss differentiation, we have no way of determining 4747c478bd9Sstevel@tonic-gate * pagesize a priori. This is therefore implemented by mandating a new rule: 4757c478bd9Sstevel@tonic-gate * whenever the kernel fills the TLB in its TLB miss handler, the TTE 4767c478bd9Sstevel@tonic-gate * corresponding to the TLB-missing VA must be in %g5 when the handler 4777c478bd9Sstevel@tonic-gate * executes its "retry". This allows the TLB return entry to determine 4787c478bd9Sstevel@tonic-gate * pagesize by simply looking at the pagesize field in the TTE stored in 4797c478bd9Sstevel@tonic-gate * %g5. 4807c478bd9Sstevel@tonic-gate * 4817c478bd9Sstevel@tonic-gate * TLB Statistics: Probe Effect 4827c478bd9Sstevel@tonic-gate * 4837c478bd9Sstevel@tonic-gate * As one might imagine, gathering TLB statistics by pushing a trap level 4847c478bd9Sstevel@tonic-gate * induces significant probe effect. To account for this probe effect, 4857c478bd9Sstevel@tonic-gate * trapstat attempts to observe it by executing a code sequence with a known 4867c478bd9Sstevel@tonic-gate * number of TLB misses both before and after interposing on the trap table. 4877c478bd9Sstevel@tonic-gate * This allows trapstat to determine a per-trap probe effect which can then be 4887c478bd9Sstevel@tonic-gate * factored into the "%tim" fields of the trapstat command. 4897c478bd9Sstevel@tonic-gate * 4907c478bd9Sstevel@tonic-gate * Note that on sun4v platforms, TLB misses are normally handled by the 4917c478bd9Sstevel@tonic-gate * hypervisor or the hardware TSB walker. Thus no fast MMU miss information 492ce0352ebSgirish * is reported for normal operation. However, when trapstat is invoked 493ce0352ebSgirish * with -t or -T option to collect detailed TLB statistics, kernel takes 4947c478bd9Sstevel@tonic-gate * over TLB miss handling. This results in significantly more overhead 4957c478bd9Sstevel@tonic-gate * and TLB statistics may not be as accurate as on sun4u platforms. 496ce0352ebSgirish * On some processors, hypervisor or hardware may provide a low overhead 497ce0352ebSgirish * interface to collect TSB hit statistics. This support is exposed via 498ce0352ebSgirish * a well defined CPU module interface (cpu_trapstat_conf to enable this 499ce0352ebSgirish * interface and cpu_trapstat_data to get detailed TSB hit statistics). 500ce0352ebSgirish * In this scenario, TSB miss statistics is collected by intercepting the 501ce0352ebSgirish * IMMU_miss and DMMU_miss traps using above mentioned trap interposition 502ce0352ebSgirish * approach. 5037c478bd9Sstevel@tonic-gate * 5047c478bd9Sstevel@tonic-gate * Locking 5057c478bd9Sstevel@tonic-gate * 5067c478bd9Sstevel@tonic-gate * The implementation uses two locks: tstat_lock (a local lock) and the global 5077c478bd9Sstevel@tonic-gate * cpu_lock. tstat_lock is used to assure trapstat's consistency in the 5087c478bd9Sstevel@tonic-gate * presence of multithreaded /dev/trapstat consumers (while as of this writing 5097c478bd9Sstevel@tonic-gate * the only consumer of /dev/trapstat is single threaded, it is obviously 5107c478bd9Sstevel@tonic-gate * necessary to correctly support multithreaded access). cpu_lock is held 5117c478bd9Sstevel@tonic-gate * whenever CPUs are being manipulated directly, to prevent them from 5127c478bd9Sstevel@tonic-gate * disappearing in the process. Because trapstat's DR callback 5137c478bd9Sstevel@tonic-gate * (trapstat_cpu_setup()) must grab tstat_lock and is called with cpu_lock 5147c478bd9Sstevel@tonic-gate * held, the lock ordering is necessarily cpu_lock before tstat_lock. 5157c478bd9Sstevel@tonic-gate * 5167c478bd9Sstevel@tonic-gate */ 5177c478bd9Sstevel@tonic-gate /* END CSTYLED */ 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate static dev_info_t *tstat_devi; /* saved in xxattach() for xxinfo() */ 5207c478bd9Sstevel@tonic-gate static int tstat_open; /* set if driver is open */ 5217c478bd9Sstevel@tonic-gate static kmutex_t tstat_lock; /* serialize access */ 5227c478bd9Sstevel@tonic-gate static vmem_t *tstat_arena; /* arena for TLB-locked pages */ 5237c478bd9Sstevel@tonic-gate static tstat_percpu_t *tstat_percpu; /* per-CPU data */ 5247c478bd9Sstevel@tonic-gate static int tstat_running; /* set if trapstat is running */ 5257c478bd9Sstevel@tonic-gate static tstat_data_t *tstat_buffer; /* staging buffer for outgoing data */ 5267c478bd9Sstevel@tonic-gate static int tstat_options; /* bit-wise indication of options */ 5277c478bd9Sstevel@tonic-gate static int *tstat_enabled; /* map of enabled trap entries */ 5287c478bd9Sstevel@tonic-gate static int tstat_tsbmiss_patched; /* tsbmiss patch flag */ 5297c478bd9Sstevel@tonic-gate static callb_id_t tstat_cprcb; /* CPR callback */ 5307c478bd9Sstevel@tonic-gate static char *tstat_probe_area; /* VA range used for probe effect */ 5317c478bd9Sstevel@tonic-gate static caddr_t tstat_probe_phys; /* physical to back above VA */ 5327c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_time; /* time spent on probe effect */ 5337c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_before[TSTAT_PROBE_NLAPS]; 5347c478bd9Sstevel@tonic-gate static hrtime_t tstat_probe_after[TSTAT_PROBE_NLAPS]; 5357c478bd9Sstevel@tonic-gate static uint_t tstat_pgszs; /* # of kernel page sizes */ 5367c478bd9Sstevel@tonic-gate static uint_t tstat_user_pgszs; /* # of user page sizes */ 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * sizeof tstat_data_t + pgsz data for the kernel. For simplicity's sake, when 5407c478bd9Sstevel@tonic-gate * we collect data, we do it based upon szc, but when we report data back to 5417c478bd9Sstevel@tonic-gate * userland, we have to do it based upon the userszc which may not match. 5427c478bd9Sstevel@tonic-gate * So, these two variables are for internal use and exported use respectively. 5437c478bd9Sstevel@tonic-gate */ 5447c478bd9Sstevel@tonic-gate static size_t tstat_data_t_size; 5457c478bd9Sstevel@tonic-gate static size_t tstat_data_t_exported_size; 5467c478bd9Sstevel@tonic-gate 54759ac0c16Sdavemq #ifndef sun4v 54859ac0c16Sdavemq 5497c478bd9Sstevel@tonic-gate static size_t tstat_data_pages; /* number of pages of tstat data */ 5507c478bd9Sstevel@tonic-gate static size_t tstat_data_size; /* tstat data size in bytes */ 5517c478bd9Sstevel@tonic-gate static size_t tstat_total_pages; /* #data pages + #instr pages */ 5527c478bd9Sstevel@tonic-gate static size_t tstat_total_size; /* tstat data size + instr size */ 55359ac0c16Sdavemq 55459ac0c16Sdavemq #else /* sun4v */ 55559ac0c16Sdavemq 5564df55fdeSJanie Lu static caddr_t tstat_va[TSTAT_NUM4M_LIMIT]; /* VAs of 4MB pages */ 5574df55fdeSJanie Lu static pfn_t tstat_pfn[TSTAT_NUM4M_LIMIT]; /* PFNs of 4MB pages */ 558ce0352ebSgirish static boolean_t tstat_fast_tlbstat = B_FALSE; 55959ac0c16Sdavemq static int tstat_traptab_initialized; 5604df55fdeSJanie Lu static int tstat_perm_mapping_failed; 5614df55fdeSJanie Lu static int tstat_hv_nopanic; 5624df55fdeSJanie Lu static int tstat_num4m_mapping; 56359ac0c16Sdavemq 56459ac0c16Sdavemq #endif /* sun4v */ 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate /* 5677c478bd9Sstevel@tonic-gate * In the above block comment, see "TLB Statistics: TLB Misses versus 5687c478bd9Sstevel@tonic-gate * TSB Misses" for an explanation of the tsbmiss patch points. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point; 5717c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point_kpm; 5727c478bd9Sstevel@tonic-gate extern uint32_t tsbmiss_trapstat_patch_point_kpm_small; 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate /* 5757c478bd9Sstevel@tonic-gate * Trapstat tsbmiss patch table 5767c478bd9Sstevel@tonic-gate */ 5777c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t tstat_tsbmiss_patch_table[] = { 5787c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point, 0}, 5797c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point_kpm, 0}, 5807c478bd9Sstevel@tonic-gate {(uint32_t *)&tsbmiss_trapstat_patch_point_kpm_small, 0}, 5817c478bd9Sstevel@tonic-gate {(uint32_t *)NULL, 0} 5827c478bd9Sstevel@tonic-gate }; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate /* 5857c478bd9Sstevel@tonic-gate * We define some general SPARC-specific constants to allow more readable 5867c478bd9Sstevel@tonic-gate * relocations. 5877c478bd9Sstevel@tonic-gate */ 5887c478bd9Sstevel@tonic-gate #define NOP 0x01000000 5897c478bd9Sstevel@tonic-gate #define HI22(v) ((uint32_t)(v) >> 10) 5907c478bd9Sstevel@tonic-gate #define LO10(v) ((uint32_t)(v) & 0x3ff) 5917c478bd9Sstevel@tonic-gate #define LO12(v) ((uint32_t)(v) & 0xfff) 5927c478bd9Sstevel@tonic-gate #define DISP22(from, to) \ 5937c478bd9Sstevel@tonic-gate ((((uintptr_t)(to) - (uintptr_t)(from)) >> 2) & 0x3fffff) 5947c478bd9Sstevel@tonic-gate #define ASI(asi) ((asi) << 5) 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * The interposing trap table must be locked in the I-TLB, and any data 5987c478bd9Sstevel@tonic-gate * referred to in the interposing trap handler must be locked in the D-TLB. 5997c478bd9Sstevel@tonic-gate * This function locks these pages in the appropriate TLBs by creating TTEs 6007c478bd9Sstevel@tonic-gate * from whole cloth, and manually loading them into the TLB. This function is 6017c478bd9Sstevel@tonic-gate * called from cross call context. 6027c478bd9Sstevel@tonic-gate * 6037c478bd9Sstevel@tonic-gate * On sun4v platforms, we use 4M page size mappings to minimize the number 6047c478bd9Sstevel@tonic-gate * of locked down entries (i.e. permanent mappings). Each CPU uses a 6057c478bd9Sstevel@tonic-gate * reserved portion of that 4M page for its TBA and data. 6067c478bd9Sstevel@tonic-gate */ 6077c478bd9Sstevel@tonic-gate static void 6087c478bd9Sstevel@tonic-gate trapstat_load_tlb(void) 6097c478bd9Sstevel@tonic-gate { 6107c478bd9Sstevel@tonic-gate int i; 6114df55fdeSJanie Lu #ifdef sun4v 6127c478bd9Sstevel@tonic-gate uint64_t ret; 6137c478bd9Sstevel@tonic-gate #endif 6147c478bd9Sstevel@tonic-gate tte_t tte; 6157c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 6167c478bd9Sstevel@tonic-gate caddr_t va = tcpu->tcpu_vabase; 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 6197c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate #ifndef sun4v 6227c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) { 6237c478bd9Sstevel@tonic-gate tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(TTE8K) | 6247c478bd9Sstevel@tonic-gate TTE_PFN_INTHI(tcpu->tcpu_pfn[i]); 6257c478bd9Sstevel@tonic-gate if (i < TSTAT_INSTR_PAGES) { 6267c478bd9Sstevel@tonic-gate tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) | 6277c478bd9Sstevel@tonic-gate TTE_LCK_INT | TTE_CP_INT | TTE_PRIV_INT; 6281e2e7a75Shuah sfmmu_itlb_ld_kva(va, &tte); 6297c478bd9Sstevel@tonic-gate } else { 6307c478bd9Sstevel@tonic-gate tte.tte_intlo = TTE_PFN_INTLO(tcpu->tcpu_pfn[i]) | 6317c478bd9Sstevel@tonic-gate TTE_LCK_INT | TTE_CP_INT | TTE_CV_INT | 6327c478bd9Sstevel@tonic-gate TTE_PRIV_INT | TTE_HWWR_INT; 6331e2e7a75Shuah sfmmu_dtlb_ld_kva(va, &tte); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate #else /* sun4v */ 6374df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) { 6384df55fdeSJanie Lu tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(tstat_pfn[i]); 6394df55fdeSJanie Lu tte.tte_intlo = TTE_PFN_INTLO(tstat_pfn[i]) | TTE_CP_INT | 6407c478bd9Sstevel@tonic-gate TTE_CV_INT | TTE_PRIV_INT | TTE_HWWR_INT | 6417c478bd9Sstevel@tonic-gate TTE_SZ_INTLO(TTE4M); 6427c478bd9Sstevel@tonic-gate ret = hv_mmu_map_perm_addr(va, KCONTEXT, *(uint64_t *)&tte, 6437c478bd9Sstevel@tonic-gate MAP_ITLB | MAP_DTLB); 6447c478bd9Sstevel@tonic-gate 6454df55fdeSJanie Lu if (ret != H_EOK) { 6464df55fdeSJanie Lu if (tstat_hv_nopanic) { 6474df55fdeSJanie Lu int j; 6484df55fdeSJanie Lu /* 6494df55fdeSJanie Lu * The first attempt to create perm mapping 6504df55fdeSJanie Lu * failed. The guest might have exhausted its 6514df55fdeSJanie Lu * perm mapping limit. We don't panic on first 6524df55fdeSJanie Lu * try. 6534df55fdeSJanie Lu */ 6544df55fdeSJanie Lu tstat_perm_mapping_failed = 1; 6554df55fdeSJanie Lu va = tcpu->tcpu_vabase; 6564df55fdeSJanie Lu for (j = 0; j < i; j++) { 6574df55fdeSJanie Lu (void) hv_mmu_unmap_perm_addr(va, 6584df55fdeSJanie Lu KCONTEXT, MAP_ITLB | MAP_DTLB); 6594df55fdeSJanie Lu va += MMU_PAGESIZE4M; 6604df55fdeSJanie Lu } 6614df55fdeSJanie Lu break; 6624df55fdeSJanie Lu } 6634df55fdeSJanie Lu /* 6644df55fdeSJanie Lu * We failed on subsequent cpus trying to 6654df55fdeSJanie Lu * create the same perm mappings. This 6664df55fdeSJanie Lu * should not happen. Panic here. 6674df55fdeSJanie Lu */ 6684df55fdeSJanie Lu cmn_err(CE_PANIC, "trapstat: cannot create " 6694df55fdeSJanie Lu "perm mappings for cpu %d " 6704df55fdeSJanie Lu "(error: 0x%lx)", CPU->cpu_id, ret); 6714df55fdeSJanie Lu } 6724df55fdeSJanie Lu va += MMU_PAGESIZE4M; 6734df55fdeSJanie Lu } 6747c478bd9Sstevel@tonic-gate #endif /* sun4v */ 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate /* 6787c478bd9Sstevel@tonic-gate * As mentioned in the "TLB Statistics: TLB Misses versus TSB Misses" section 6797c478bd9Sstevel@tonic-gate * of the block comment, TLB misses are differentiated from TSB misses in 6807c478bd9Sstevel@tonic-gate * part by hot-patching the instructions at the tsbmiss patch points (see 6817c478bd9Sstevel@tonic-gate * tstat_tsbmiss_patch_table). This routine is used both to initially patch 6827c478bd9Sstevel@tonic-gate * the instructions, and to patch them back to their original values upon 6837c478bd9Sstevel@tonic-gate * restoring the original trap table. 6847c478bd9Sstevel@tonic-gate */ 6857c478bd9Sstevel@tonic-gate static void 6867c478bd9Sstevel@tonic-gate trapstat_hotpatch() 6877c478bd9Sstevel@tonic-gate { 6887c478bd9Sstevel@tonic-gate uint32_t instr; 6897c478bd9Sstevel@tonic-gate uint32_t simm13; 6907c478bd9Sstevel@tonic-gate tstat_tsbmiss_patch_entry_t *ep; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_TLBDATA)) 6957c478bd9Sstevel@tonic-gate return; 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate if (!tstat_tsbmiss_patched) { 6987c478bd9Sstevel@tonic-gate /* 6997c478bd9Sstevel@tonic-gate * We haven't patched the TSB paths; do so now. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 7027c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_tlbret_t, ttlbr_ktsb) - 7037c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb) == 7047c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_utsb) - 7057c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_utlb)); 7067c478bd9Sstevel@tonic-gate 7077c478bd9Sstevel@tonic-gate simm13 = offsetof(tstat_tlbret_t, ttlbr_ktsb) - 7087c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb); 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) { 7117c478bd9Sstevel@tonic-gate ASSERT(ep->tpe_instr == 0); 7127c478bd9Sstevel@tonic-gate instr = ep->tpe_instr = *ep->tpe_addr; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate /* 7157c478bd9Sstevel@tonic-gate * Assert that the instruction we're about to patch is 7167c478bd9Sstevel@tonic-gate * "add %g7, 0, %g7" (0x8e01e000). 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate ASSERT(instr == TSTAT_TSBMISS_INSTR); 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate instr |= simm13; 7217c478bd9Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)ep->tpe_addr, 7227c478bd9Sstevel@tonic-gate instr, sizeof (instr)); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate tstat_tsbmiss_patched = 1; 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate } else { 7287c478bd9Sstevel@tonic-gate /* 7297c478bd9Sstevel@tonic-gate * Remove patches from the TSB paths. 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate for (ep = tstat_tsbmiss_patch_table; ep->tpe_addr; ep++) { 7327c478bd9Sstevel@tonic-gate ASSERT(ep->tpe_instr == TSTAT_TSBMISS_INSTR); 7337c478bd9Sstevel@tonic-gate hot_patch_kernel_text((caddr_t)ep->tpe_addr, 7347c478bd9Sstevel@tonic-gate ep->tpe_instr, sizeof (instr)); 7357c478bd9Sstevel@tonic-gate ep->tpe_instr = 0; 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate tstat_tsbmiss_patched = 0; 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate /* 7437c478bd9Sstevel@tonic-gate * This is the routine executed to clock the performance of the trap table, 7447c478bd9Sstevel@tonic-gate * executed both before and after interposing on the trap table to attempt to 7457c478bd9Sstevel@tonic-gate * determine probe effect. The probe effect is used to adjust the "%tim" 7467c478bd9Sstevel@tonic-gate * fields of trapstat's -t and -T output; we only use TLB misses to clock the 7477c478bd9Sstevel@tonic-gate * trap table. We execute the inner loop (which is designed to exceed the 7487c478bd9Sstevel@tonic-gate * TLB's reach) nlaps times, taking the best time as our time (thereby 7497c478bd9Sstevel@tonic-gate * factoring out the effects of interrupts, cache misses or other perturbing 7507c478bd9Sstevel@tonic-gate * events. 7517c478bd9Sstevel@tonic-gate */ 7527c478bd9Sstevel@tonic-gate static hrtime_t 7537c478bd9Sstevel@tonic-gate trapstat_probe_laps(int nlaps, hrtime_t *buf) 7547c478bd9Sstevel@tonic-gate { 7557c478bd9Sstevel@tonic-gate int i, j = 0; 7567c478bd9Sstevel@tonic-gate hrtime_t ts, best = INT64_MAX; 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate while (nlaps--) { 7597c478bd9Sstevel@tonic-gate ts = rdtick(); 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_SIZE; i += MMU_PAGESIZE) 7627c478bd9Sstevel@tonic-gate *((volatile char *)&tstat_probe_area[i]); 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate if ((ts = rdtick() - ts) < best) 7657c478bd9Sstevel@tonic-gate best = ts; 7667c478bd9Sstevel@tonic-gate buf[j++] = ts; 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate return (best); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate /* 7737c478bd9Sstevel@tonic-gate * This routine determines the probe effect by calling trapstat_probe_laps() 7747c478bd9Sstevel@tonic-gate * both without and with the interposing trap table. Note that this is 7757c478bd9Sstevel@tonic-gate * called from a cross call on the desired CPU, and that it is called on 7767c478bd9Sstevel@tonic-gate * every CPU (this is necessary because the probe effect may differ from 7777c478bd9Sstevel@tonic-gate * one CPU to another). 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate static void 7807c478bd9Sstevel@tonic-gate trapstat_probe() 7817c478bd9Sstevel@tonic-gate { 7827c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 7837c478bd9Sstevel@tonic-gate hrtime_t before, after; 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED)) 7867c478bd9Sstevel@tonic-gate return; 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate if (tstat_probe_area == NULL || (tstat_options & TSTAT_OPT_NOGO)) 7897c478bd9Sstevel@tonic-gate return; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate /* 7927c478bd9Sstevel@tonic-gate * We very much expect the %tba to be KERNELBASE; this is a 7937c478bd9Sstevel@tonic-gate * precautionary measure to assure that trapstat doesn't melt the 7947c478bd9Sstevel@tonic-gate * machine should the %tba point unexpectedly elsewhere. 7957c478bd9Sstevel@tonic-gate */ 7967c478bd9Sstevel@tonic-gate if (get_tba() != (caddr_t)KERNELBASE) 7977c478bd9Sstevel@tonic-gate return; 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate /* 8007c478bd9Sstevel@tonic-gate * Preserve this CPU's data before destroying it by enabling the 8017c478bd9Sstevel@tonic-gate * interposing trap table. We can safely use tstat_buffer because 8027c478bd9Sstevel@tonic-gate * the caller of the trapstat_probe() cross call is holding tstat_lock. 8037c478bd9Sstevel@tonic-gate */ 8044df55fdeSJanie Lu #ifdef sun4v 8054df55fdeSJanie Lu bcopy(tcpu->tcpu_data, tstat_buffer, TSTAT_DATA_SIZE); 8064df55fdeSJanie Lu #else 8077c478bd9Sstevel@tonic-gate bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size); 8084df55fdeSJanie Lu #endif 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate tstat_probe_time = gethrtime(); 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate before = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_before); 8137c478bd9Sstevel@tonic-gate (void) set_tba(tcpu->tcpu_ibase); 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate after = trapstat_probe_laps(TSTAT_PROBE_NLAPS, tstat_probe_after); 8167c478bd9Sstevel@tonic-gate (void) set_tba((caddr_t)KERNELBASE); 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate tstat_probe_time = gethrtime() - tstat_probe_time; 8197c478bd9Sstevel@tonic-gate 8204df55fdeSJanie Lu #ifdef sun4v 8214df55fdeSJanie Lu bcopy(tstat_buffer, tcpu->tcpu_data, TSTAT_DATA_SIZE); 8224df55fdeSJanie Lu tcpu->tcpu_tdata_peffect = (after - before) / TSTAT_PROBE_NPAGES; 8234df55fdeSJanie Lu #else 8247c478bd9Sstevel@tonic-gate bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size); 8257c478bd9Sstevel@tonic-gate tcpu->tcpu_data->tdata_peffect = (after - before) / TSTAT_PROBE_NPAGES; 8264df55fdeSJanie Lu #endif 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate static void 8307c478bd9Sstevel@tonic-gate trapstat_probe_alloc() 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate pfn_t pfn; 8337c478bd9Sstevel@tonic-gate caddr_t va; 8347c478bd9Sstevel@tonic-gate int i; 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 8377c478bd9Sstevel@tonic-gate ASSERT(tstat_probe_area == NULL); 8387c478bd9Sstevel@tonic-gate ASSERT(tstat_probe_phys == NULL); 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_TLBDATA)) 8417c478bd9Sstevel@tonic-gate return; 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate /* 8447c478bd9Sstevel@tonic-gate * Grab some virtual from the heap arena. 8457c478bd9Sstevel@tonic-gate */ 8467c478bd9Sstevel@tonic-gate tstat_probe_area = vmem_alloc(heap_arena, TSTAT_PROBE_SIZE, VM_SLEEP); 8477c478bd9Sstevel@tonic-gate va = tstat_probe_area; 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate /* 8507c478bd9Sstevel@tonic-gate * Grab a single physical page. 8517c478bd9Sstevel@tonic-gate */ 8527c478bd9Sstevel@tonic-gate tstat_probe_phys = vmem_alloc(tstat_arena, MMU_PAGESIZE, VM_SLEEP); 8537c478bd9Sstevel@tonic-gate pfn = hat_getpfnum(kas.a_hat, tstat_probe_phys); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate /* 8567c478bd9Sstevel@tonic-gate * Now set the translation for every page in our virtual range 8577c478bd9Sstevel@tonic-gate * to be our allocated physical page. 8587c478bd9Sstevel@tonic-gate */ 8597c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_NPAGES; i++) { 8607c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, va, MMU_PAGESIZE, pfn, PROT_READ, 8617c478bd9Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 8627c478bd9Sstevel@tonic-gate va += MMU_PAGESIZE; 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate static void 8677c478bd9Sstevel@tonic-gate trapstat_probe_free() 8687c478bd9Sstevel@tonic-gate { 8697c478bd9Sstevel@tonic-gate caddr_t va; 8707c478bd9Sstevel@tonic-gate int i; 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate if ((va = tstat_probe_area) == NULL) 8757c478bd9Sstevel@tonic-gate return; 8767c478bd9Sstevel@tonic-gate 8777c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_PROBE_NPAGES; i++) { 8787c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, va, MMU_PAGESIZE, HAT_UNLOAD_UNLOCK); 8797c478bd9Sstevel@tonic-gate va += MMU_PAGESIZE; 8807c478bd9Sstevel@tonic-gate } 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tstat_probe_phys, MMU_PAGESIZE); 8837c478bd9Sstevel@tonic-gate vmem_free(heap_arena, tstat_probe_area, TSTAT_PROBE_SIZE); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate tstat_probe_phys = NULL; 8867c478bd9Sstevel@tonic-gate tstat_probe_area = NULL; 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate /* 8907c478bd9Sstevel@tonic-gate * This routine actually enables a CPU by setting its %tba to be the 8917c478bd9Sstevel@tonic-gate * CPU's interposing trap table. It is called out of cross call context. 8927c478bd9Sstevel@tonic-gate */ 8937c478bd9Sstevel@tonic-gate static void 8947c478bd9Sstevel@tonic-gate trapstat_enable() 8957c478bd9Sstevel@tonic-gate { 8967c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 8977c478bd9Sstevel@tonic-gate 8987c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_SELECTED)) 8997c478bd9Sstevel@tonic-gate return; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 9027c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate if (get_tba() != (caddr_t)KERNELBASE) 9057c478bd9Sstevel@tonic-gate return; 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_NOGO)) 9087c478bd9Sstevel@tonic-gate (void) set_tba(tcpu->tcpu_ibase); 9097c478bd9Sstevel@tonic-gate tcpu->tcpu_flags |= TSTAT_CPU_ENABLED; 9107c478bd9Sstevel@tonic-gate #ifdef sun4v 911ce0352ebSgirish if ((tstat_options & TSTAT_OPT_TLBDATA) && 912ce0352ebSgirish !(tstat_options & TSTAT_OPT_NOGO)) { 913ce0352ebSgirish if (tstat_fast_tlbstat) { 9147c478bd9Sstevel@tonic-gate /* 915ce0352ebSgirish * Invoke processor specific interface to enable 916ce0352ebSgirish * collection of TSB hit statistics. 917ce0352ebSgirish */ 918*07d06da5SSurya Prakki (void) cpu_trapstat_conf(CPU_TSTATCONF_ENABLE); 919ce0352ebSgirish } else { 920ce0352ebSgirish /* 921ce0352ebSgirish * Collect TLB miss statistics by taking over 922ce0352ebSgirish * TLB miss handling from the hypervisor. This 923ce0352ebSgirish * is done by telling the hypervisor that there 924ce0352ebSgirish * is no TSB configured. Also set TSTAT_TLB_STATS 925ce0352ebSgirish * flag so that no user TSB is configured during 926ce0352ebSgirish * context switch time. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate cpu_t *cp = CPU; 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate cp->cpu_m.cpu_tstat_flags |= TSTAT_TLB_STATS; 9317c478bd9Sstevel@tonic-gate (void) hv_set_ctx0(NULL, NULL); 9327c478bd9Sstevel@tonic-gate (void) hv_set_ctxnon0(NULL, NULL); 9337c478bd9Sstevel@tonic-gate } 934ce0352ebSgirish } 9357c478bd9Sstevel@tonic-gate #endif 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate /* 9397c478bd9Sstevel@tonic-gate * This routine disables a CPU (vis a vis trapstat) by setting its %tba to be 9407c478bd9Sstevel@tonic-gate * the actual, underlying trap table. It is called out of cross call context. 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate static void 9437c478bd9Sstevel@tonic-gate trapstat_disable() 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 9487c478bd9Sstevel@tonic-gate return; 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 9517c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_NOGO)) 9547c478bd9Sstevel@tonic-gate (void) set_tba((caddr_t)KERNELBASE); 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED; 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate #ifdef sun4v 959ce0352ebSgirish if ((tstat_options & TSTAT_OPT_TLBDATA) && 960ce0352ebSgirish !(tstat_options & TSTAT_OPT_NOGO)) { 961ce0352ebSgirish if (tstat_fast_tlbstat) { 9627c478bd9Sstevel@tonic-gate /* 963ce0352ebSgirish * Invoke processor specific interface to disable 964ce0352ebSgirish * collection of TSB hit statistics on each processor. 965ce0352ebSgirish */ 966*07d06da5SSurya Prakki (void) cpu_trapstat_conf(CPU_TSTATCONF_DISABLE); 967ce0352ebSgirish } else { 968ce0352ebSgirish /* 969ce0352ebSgirish * As part of collecting TLB miss statistics, we took 970ce0352ebSgirish * over TLB miss handling from the hypervisor by 971ce0352ebSgirish * telling the hypervisor that NO TSB is configured. 972ce0352ebSgirish * We need to restore that by communicating proper 973ce0352ebSgirish * kernel/user TSB information so that TLB misses 974ce0352ebSgirish * can be handled by the hypervisor or the hardware 975ce0352ebSgirish * more efficiently. 9767c478bd9Sstevel@tonic-gate * 977ce0352ebSgirish * We restore kernel TSB information right away. 978ce0352ebSgirish * However, to minimize any locking dependency, we 979ce0352ebSgirish * don't restore user TSB information right away. 980ce0352ebSgirish * Instead, we simply clear the TSTAT_TLB_STATS flag 981ce0352ebSgirish * so that the user TSB information is automatically 982ce0352ebSgirish * restored on next context switch. 9837c478bd9Sstevel@tonic-gate * 9847c478bd9Sstevel@tonic-gate * Note that the call to restore kernel TSB information 9857c478bd9Sstevel@tonic-gate * will normally not fail, unless wrong information is 9867c478bd9Sstevel@tonic-gate * passed here. In that scenario, system will still 9877c478bd9Sstevel@tonic-gate * continue to function properly with the exception of 9887c478bd9Sstevel@tonic-gate * kernel handling all the TLB misses. 9897c478bd9Sstevel@tonic-gate */ 9907c478bd9Sstevel@tonic-gate struct hv_tsb_block *hvbp = &ksfmmup->sfmmu_hvblock; 9917c478bd9Sstevel@tonic-gate cpu_t *cp = CPU; 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate cp->cpu_m.cpu_tstat_flags &= ~TSTAT_TLB_STATS; 994ce0352ebSgirish (void) hv_set_ctx0(hvbp->hv_tsb_info_cnt, 995ce0352ebSgirish hvbp->hv_tsb_info_pa); 996ce0352ebSgirish } 9977c478bd9Sstevel@tonic-gate } 9987c478bd9Sstevel@tonic-gate #endif 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate /* 10027c478bd9Sstevel@tonic-gate * We use %tick as the time base when recording the time spent executing 10037c478bd9Sstevel@tonic-gate * the trap handler. %tick, however, is not necessarily kept in sync 10047c478bd9Sstevel@tonic-gate * across CPUs (indeed, different CPUs may have different %tick frequencies). 10057c478bd9Sstevel@tonic-gate * We therefore cross call onto a CPU to get a snapshot of its data to 10067c478bd9Sstevel@tonic-gate * copy out; this is the routine executed out of that cross call. 10077c478bd9Sstevel@tonic-gate */ 10087c478bd9Sstevel@tonic-gate static void 10097c478bd9Sstevel@tonic-gate trapstat_snapshot() 10107c478bd9Sstevel@tonic-gate { 10117c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[CPU->cpu_id]; 10127c478bd9Sstevel@tonic-gate tstat_data_t *data = tcpu->tcpu_data; 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 10157c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 10167c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ENABLED); 10177c478bd9Sstevel@tonic-gate 10184df55fdeSJanie Lu #ifndef sun4v 10197c478bd9Sstevel@tonic-gate data->tdata_snapts = gethrtime(); 10207c478bd9Sstevel@tonic-gate data->tdata_snaptick = rdtick(); 10217c478bd9Sstevel@tonic-gate bcopy(data, tstat_buffer, tstat_data_t_size); 10224df55fdeSJanie Lu #else 1023ce0352ebSgirish /* 10244df55fdeSJanie Lu * For sun4v, in order to conserve space in the limited 10254df55fdeSJanie Lu * per-cpu 4K buffer, we derive certain info somewhere else and 10264df55fdeSJanie Lu * copy them directly into the tstat_buffer output. 10274df55fdeSJanie Lu * Note that we either are collecting tlb stats or 10284df55fdeSJanie Lu * regular trapstats but never both. 1029ce0352ebSgirish */ 10304df55fdeSJanie Lu tstat_buffer->tdata_cpuid = CPU->cpu_id; 10314df55fdeSJanie Lu tstat_buffer->tdata_peffect = tcpu->tcpu_tdata_peffect; 10324df55fdeSJanie Lu tstat_buffer->tdata_snapts = gethrtime(); 10334df55fdeSJanie Lu tstat_buffer->tdata_snaptick = rdtick(); 10344df55fdeSJanie Lu 10354df55fdeSJanie Lu if (tstat_options & TSTAT_OPT_TLBDATA) { 10364df55fdeSJanie Lu /* Copy tlb/tsb stats collected in the per-cpu trapdata */ 10374df55fdeSJanie Lu tstat_tdata_t *tdata = (tstat_tdata_t *)data; 10384df55fdeSJanie Lu bcopy(&tdata->tdata_pgsz[0], 10394df55fdeSJanie Lu &tstat_buffer->tdata_pgsz[0], 10404df55fdeSJanie Lu tstat_pgszs * sizeof (tstat_pgszdata_t)); 10414df55fdeSJanie Lu 10424df55fdeSJanie Lu /* 10434df55fdeSJanie Lu * Invoke processor specific interface to collect TLB stats 10444df55fdeSJanie Lu * on each processor if enabled. 10454df55fdeSJanie Lu */ 10464df55fdeSJanie Lu if (tstat_fast_tlbstat) { 1047ce0352ebSgirish cpu_trapstat_data((void *) tstat_buffer->tdata_pgsz, 1048ce0352ebSgirish tstat_pgszs); 10494df55fdeSJanie Lu } 10504df55fdeSJanie Lu } else { 10514df55fdeSJanie Lu /* 10524df55fdeSJanie Lu * Normal trapstat collection. 10534df55fdeSJanie Lu * Copy all the 4K data area into tstat_buffer tdata_trap 10544df55fdeSJanie Lu * area. 10554df55fdeSJanie Lu */ 10564df55fdeSJanie Lu bcopy(data, &tstat_buffer->tdata_traps[0], TSTAT_DATA_SIZE); 10574df55fdeSJanie Lu } 10584df55fdeSJanie Lu #endif /* sun4v */ 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate /* 10627c478bd9Sstevel@tonic-gate * The TSTAT_RETENT_* constants define offsets in the TLB return entry. 10637c478bd9Sstevel@tonic-gate * They are used only in trapstat_tlbretent() (below) and #undef'd 10647c478bd9Sstevel@tonic-gate * immediately afterwards. Any change to "retent" in trapstat_tlbretent() 10657c478bd9Sstevel@tonic-gate * will likely require changes to these constants. 10667c478bd9Sstevel@tonic-gate */ 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate #ifndef sun4v 10697c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATHI 1 10707c478bd9Sstevel@tonic-gate #define TSTAT_RETENT_STATLO 2 10711bd453f3Ssusans #define TSTAT_RETENT_SHIFT 11 10721bd453f3Ssusans #define TSTAT_RETENT_COUNT_LD 13 10731bd453f3Ssusans #define TSTAT_RETENT_COUNT_ST 15 10741bd453f3Ssusans #define TSTAT_RETENT_TMPTSHI 16 10751bd453f3Ssusans #define TSTAT_RETENT_TMPTSLO 17 10761bd453f3Ssusans #define TSTAT_RETENT_TIME_LD 19 10771bd453f3Ssusans #define TSTAT_RETENT_TIME_ST 21 10787c478bd9Sstevel@tonic-gate #else /* sun4v */ 107959ac0c16Sdavemq #define TSTAT_RETENT_TDATASHFT 2 108059ac0c16Sdavemq #define TSTAT_RETENT_STATHI 4 108159ac0c16Sdavemq #define TSTAT_RETENT_STATLO 6 108259ac0c16Sdavemq #define TSTAT_RETENT_SHIFT 9 108359ac0c16Sdavemq #define TSTAT_RETENT_COUNT_LD 11 108459ac0c16Sdavemq #define TSTAT_RETENT_COUNT_ST 13 108559ac0c16Sdavemq #define TSTAT_RETENT_TMPTSHI 14 108659ac0c16Sdavemq #define TSTAT_RETENT_TMPTSLO 16 108759ac0c16Sdavemq #define TSTAT_RETENT_TIME_LD 18 108859ac0c16Sdavemq #define TSTAT_RETENT_TIME_ST 20 10897c478bd9Sstevel@tonic-gate #endif /* sun4v */ 10907c478bd9Sstevel@tonic-gate 10917c478bd9Sstevel@tonic-gate static void 10927c478bd9Sstevel@tonic-gate trapstat_tlbretent(tstat_percpu_t *tcpu, tstat_tlbretent_t *ret, 10937c478bd9Sstevel@tonic-gate tstat_missdata_t *data) 10947c478bd9Sstevel@tonic-gate { 10957c478bd9Sstevel@tonic-gate uint32_t *ent = ret->ttlbrent_instr, shift; 109659ac0c16Sdavemq uintptr_t base; 109759ac0c16Sdavemq #ifndef sun4v 109859ac0c16Sdavemq uintptr_t tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick); 109959ac0c16Sdavemq #else 11004df55fdeSJanie Lu uintptr_t tmptick = TSTAT_CPU0_TLBDATA_OFFS(tcpu, tdata_tmptick); 110159ac0c16Sdavemq #endif 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate /* 11047c478bd9Sstevel@tonic-gate * This is the entry executed upon return from the TLB/TSB miss 11057c478bd9Sstevel@tonic-gate * handler (i.e. the code interpositioned between the "retry" and 11067c478bd9Sstevel@tonic-gate * the actual return to the TLB-missing instruction). Detail on its 11077c478bd9Sstevel@tonic-gate * theory of operation can be found in the "TLB Statistics" section 11087c478bd9Sstevel@tonic-gate * of the block comment. Note that we expect the TTE just loaded 11097c478bd9Sstevel@tonic-gate * into the TLB to be in %g5; all other globals are available as 11107c478bd9Sstevel@tonic-gate * scratch. Finally, note that the page size information in sun4v is 11117c478bd9Sstevel@tonic-gate * located in the lower bits of the TTE -- requiring us to have a 11127c478bd9Sstevel@tonic-gate * different return entry on sun4v. 11137c478bd9Sstevel@tonic-gate */ 11147c478bd9Sstevel@tonic-gate static const uint32_t retent[TSTAT_TLBRET_NINSTR] = { 11157c478bd9Sstevel@tonic-gate #ifndef sun4v 11167c478bd9Sstevel@tonic-gate 0x87410000, /* rd %tick, %g3 */ 11177c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 11187c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(stat), %g1 */ 11197c478bd9Sstevel@tonic-gate 0x89297001, /* sllx %g5, 1, %g4 */ 11207c478bd9Sstevel@tonic-gate 0x8931303e, /* srlx %g4, 62, %g4 */ 11217c478bd9Sstevel@tonic-gate 0x8531702e, /* srlx %g5, 46, %g2 */ 11227c478bd9Sstevel@tonic-gate 0x8408a004, /* and %g2, 4, %g2 */ 11237c478bd9Sstevel@tonic-gate 0x88110002, /* or %g4, %g2, %g4 */ 11241bd453f3Ssusans 0x80a12005, /* cmp %g4, 5 */ 11251bd453f3Ssusans 0x34400002, /* bg,a,pn %icc, +8 */ 11261bd453f3Ssusans 0x88102004, /* mov 4, %g4 */ 11277c478bd9Sstevel@tonic-gate 0x89292000, /* sll %g4, shift, %g4 */ 11287c478bd9Sstevel@tonic-gate 0x82004004, /* add %g1, %g4, %g1 */ 11297c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_count], %g2 */ 11307c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 11317c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_count] */ 11327c478bd9Sstevel@tonic-gate 0x0d000000, /* sethi %hi(tdata_tmptick), %g6 */ 11337c478bd9Sstevel@tonic-gate 0xc459a000, /* ldx [%g6 + %lo(tdata_tmptick)], %g2 */ 11347c478bd9Sstevel@tonic-gate 0x8620c002, /* sub %g3, %g2, %g3 */ 11357c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_time], %g2 */ 11367c478bd9Sstevel@tonic-gate 0x84008003, /* add %g2, %g3, %g2 */ 11377c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_time] */ 11387c478bd9Sstevel@tonic-gate 0x83f00000 /* retry */ 11397c478bd9Sstevel@tonic-gate #else /* sun4v */ 114059ac0c16Sdavemq 0x82102008, /* mov SCRATCHPAD_CPUID, %g1 */ 114159ac0c16Sdavemq 0xced84400, /* ldxa [%g1]ASI_SCRATCHPAD, %g7 */ 114259ac0c16Sdavemq 0x8f29f000, /* sllx %g7, TSTAT_DATA_SHIFT, %g7 */ 11437c478bd9Sstevel@tonic-gate 0x87410000, /* rd %tick, %g3 */ 11447c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 114559ac0c16Sdavemq 0x82004007, /* add %g1, %g7, %g1 */ 11467c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(stat), %g1 */ 11477c478bd9Sstevel@tonic-gate 0x8929703d, /* sllx %g5, 61, %g4 */ 11487c478bd9Sstevel@tonic-gate 0x8931303d, /* srlx %g4, 61, %g4 */ 11497c478bd9Sstevel@tonic-gate 0x89292000, /* sll %g4, shift, %g4 */ 11507c478bd9Sstevel@tonic-gate 0x82004004, /* add %g1, %g4, %g1 */ 11517c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_count], %g2 */ 11527c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 11537c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_count] */ 11547c478bd9Sstevel@tonic-gate 0x0d000000, /* sethi %hi(tdata_tmptick), %g6 */ 115559ac0c16Sdavemq 0x8c018007, /* add %g6, %g7, %g6 */ 11567c478bd9Sstevel@tonic-gate 0xc459a000, /* ldx [%g6 + %lo(tdata_tmptick)], %g2 */ 11577c478bd9Sstevel@tonic-gate 0x8620c002, /* sub %g3, %g2, %g3 */ 11587c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + tmiss_time], %g2 */ 11597c478bd9Sstevel@tonic-gate 0x84008003, /* add %g2, %g3, %g2 */ 11607c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + tmiss_time] */ 11617c478bd9Sstevel@tonic-gate 0x83f00000 /* retry */ 11627c478bd9Sstevel@tonic-gate #endif /* sun4v */ 11637c478bd9Sstevel@tonic-gate }; 11647c478bd9Sstevel@tonic-gate 11657c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 11667c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 11677c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_missdata_t, tmiss_count) <= LO10(-1)); 11687c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 11697c478bd9Sstevel@tonic-gate ASSERT(offsetof(tstat_missdata_t, tmiss_time) <= LO10(-1)); 11707c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 11717c478bd9Sstevel@tonic-gate ASSERT(!((sizeof (tstat_pgszdata_t) - 1) & sizeof (tstat_pgszdata_t))); 11727c478bd9Sstevel@tonic-gate 11737c478bd9Sstevel@tonic-gate for (shift = 1; (1 << shift) != sizeof (tstat_pgszdata_t); shift++) 11747c478bd9Sstevel@tonic-gate continue; 11757c478bd9Sstevel@tonic-gate 117659ac0c16Sdavemq base = (uintptr_t)tcpu->tcpu_ibase + TSTAT_INSTR_SIZE + 11777c478bd9Sstevel@tonic-gate ((uintptr_t)data - (uintptr_t)tcpu->tcpu_data); 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate bcopy(retent, ent, sizeof (retent)); 11807c478bd9Sstevel@tonic-gate 118159ac0c16Sdavemq #if defined(sun4v) 118259ac0c16Sdavemq ent[TSTAT_RETENT_TDATASHFT] |= LO10((uintptr_t)TSTAT_DATA_SHIFT); 118359ac0c16Sdavemq #endif 11847c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_STATHI] |= HI22(base); 11857c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_STATLO] |= LO10(base); 11867c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_SHIFT] |= shift; 11877c478bd9Sstevel@tonic-gate /* LINTED E_EXPR_NULL_EFFECT */ 11887c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_COUNT_LD] |= offsetof(tstat_missdata_t, tmiss_count); 11897c478bd9Sstevel@tonic-gate /* LINTED E_EXPR_NULL_EFFECT */ 11907c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_COUNT_ST] |= offsetof(tstat_missdata_t, tmiss_count); 11917c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TMPTSHI] |= HI22(tmptick); 11927c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TMPTSLO] |= LO10(tmptick); 11937c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TIME_LD] |= offsetof(tstat_missdata_t, tmiss_time); 11947c478bd9Sstevel@tonic-gate ent[TSTAT_RETENT_TIME_ST] |= offsetof(tstat_missdata_t, tmiss_time); 11957c478bd9Sstevel@tonic-gate } 11967c478bd9Sstevel@tonic-gate 119759ac0c16Sdavemq #if defined(sun4v) 119859ac0c16Sdavemq #undef TSTAT_RETENT_TDATASHFT 119959ac0c16Sdavemq #endif 12007c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATHI 12017c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_STATLO 12027c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_SHIFT 12037c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_LD 12047c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_COUNT_ST 12057c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSHI 12067c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TMPTSLO 12077c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_LD 12087c478bd9Sstevel@tonic-gate #undef TSTAT_RETENT_TIME_ST 12097c478bd9Sstevel@tonic-gate 12107c478bd9Sstevel@tonic-gate /* 12117c478bd9Sstevel@tonic-gate * The TSTAT_TLBENT_* constants define offsets in the TLB entry. They are 12127c478bd9Sstevel@tonic-gate * used only in trapstat_tlbent() (below) and #undef'd immediately afterwards. 12137c478bd9Sstevel@tonic-gate * Any change to "tlbent" in trapstat_tlbent() will likely require changes 12147c478bd9Sstevel@tonic-gate * to these constants. 12157c478bd9Sstevel@tonic-gate */ 12167c478bd9Sstevel@tonic-gate 12177c478bd9Sstevel@tonic-gate #ifndef sun4v 12187c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATHI 0 12197c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_LD 1 12207c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_STATLO_ST 3 12217c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_MMUASI 15 12227c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCHI 18 12237c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_USER 19 12247c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TPCLO_KERN 21 12257c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSHI 25 12267c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_TSLO 27 12277c478bd9Sstevel@tonic-gate #define TSTAT_TLBENT_BA 28 12287c478bd9Sstevel@tonic-gate #else /* sun4v */ 122959ac0c16Sdavemq #define TSTAT_TLBENT_TDATASHFT 2 123059ac0c16Sdavemq #define TSTAT_TLBENT_STATHI 3 123159ac0c16Sdavemq #define TSTAT_TLBENT_STATLO_LD 5 123259ac0c16Sdavemq #define TSTAT_TLBENT_STATLO_ST 7 123359ac0c16Sdavemq #define TSTAT_TLBENT_TAGTARGET 23 123459ac0c16Sdavemq #define TSTAT_TLBENT_TPCHI 25 123559ac0c16Sdavemq #define TSTAT_TLBENT_TPCLO_USER 26 123659ac0c16Sdavemq #define TSTAT_TLBENT_TPCLO_KERN 28 123759ac0c16Sdavemq #define TSTAT_TLBENT_TSHI 32 123859ac0c16Sdavemq #define TSTAT_TLBENT_TSLO 35 12394df55fdeSJanie Lu #define TSTAT_TLBENT_ADDRHI 36 12404df55fdeSJanie Lu #define TSTAT_TLBENT_ADDRLO 37 12417c478bd9Sstevel@tonic-gate #endif /* sun4v */ 12427c478bd9Sstevel@tonic-gate 12437c478bd9Sstevel@tonic-gate static void 12447c478bd9Sstevel@tonic-gate trapstat_tlbent(tstat_percpu_t *tcpu, int entno) 12457c478bd9Sstevel@tonic-gate { 12467c478bd9Sstevel@tonic-gate uint32_t *ent; 12474df55fdeSJanie Lu uintptr_t orig, va; 1248ce0352ebSgirish #ifndef sun4v 12494df55fdeSJanie Lu uintptr_t baoffs; 12507c478bd9Sstevel@tonic-gate int itlb = entno == TSTAT_ENT_ITLBMISS; 125159ac0c16Sdavemq uint32_t asi = itlb ? ASI(ASI_IMMU) : ASI(ASI_DMMU); 1252ce0352ebSgirish #else 1253ce0352ebSgirish int itlb = (entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_ITLBMISS); 125459ac0c16Sdavemq uint32_t tagtarget_off = itlb ? MMFSA_I_CTX : MMFSA_D_CTX; 125559ac0c16Sdavemq uint32_t *tent; /* MMU trap vector entry */ 125659ac0c16Sdavemq uintptr_t tentva; /* MMU trap vector entry va */ 125759ac0c16Sdavemq static const uint32_t mmumiss[TSTAT_ENT_NINSTR] = { 125859ac0c16Sdavemq 0x30800000, /* ba,a addr */ 125959ac0c16Sdavemq NOP, NOP, NOP, NOP, NOP, NOP, NOP 126059ac0c16Sdavemq }; 1261ce0352ebSgirish #endif 12627c478bd9Sstevel@tonic-gate int entoffs = entno << TSTAT_ENT_SHIFT; 12637c478bd9Sstevel@tonic-gate uintptr_t tmptick, stat, tpc, utpc; 12644df55fdeSJanie Lu tstat_pgszdata_t *data; 12657c478bd9Sstevel@tonic-gate tstat_tlbdata_t *udata, *kdata; 12667c478bd9Sstevel@tonic-gate tstat_tlbret_t *ret; 12677c478bd9Sstevel@tonic-gate 12684df55fdeSJanie Lu #ifdef sun4v 12694df55fdeSJanie Lu data = &((tstat_tdata_t *)tcpu->tcpu_data)->tdata_pgsz[0]; 12704df55fdeSJanie Lu #else 12714df55fdeSJanie Lu data = &tcpu->tcpu_data->tdata_pgsz[0]; 12724df55fdeSJanie Lu #endif /* sun4v */ 12734df55fdeSJanie Lu 12747c478bd9Sstevel@tonic-gate /* 12757c478bd9Sstevel@tonic-gate * When trapstat is run with TLB statistics, this is the entry for 12767c478bd9Sstevel@tonic-gate * both I- and D-TLB misses; this code performs trap level pushing, 12777c478bd9Sstevel@tonic-gate * as described in the "TLB Statistics" section of the block comment. 12787c478bd9Sstevel@tonic-gate * This code is executing at TL 1; %tstate[0] contains the saved 12797c478bd9Sstevel@tonic-gate * state at the time of the TLB miss. Pushing trap level 1 (and thus 12807c478bd9Sstevel@tonic-gate * raising TL to 2) requires us to fill in %tstate[1] with our %pstate, 12817c478bd9Sstevel@tonic-gate * %cwp and %asi. We leave %tt unchanged, and we set %tpc and %tnpc to 12827c478bd9Sstevel@tonic-gate * the appropriate TLB return entry (based on the context of the miss). 12837c478bd9Sstevel@tonic-gate * Finally, we sample %tick, and stash it in the tdata_tmptick member 12847c478bd9Sstevel@tonic-gate * the per-CPU tstat_data structure. tdata_tmptick will be used in 12857c478bd9Sstevel@tonic-gate * the TLB return entry to determine the amount of time spent in the 12867c478bd9Sstevel@tonic-gate * TLB miss handler. 12877c478bd9Sstevel@tonic-gate * 1288bd46b14cSgirish * Note that on sun4v platforms, we must obtain the context information 1289bd46b14cSgirish * from the MMU fault status area. (The base address of this MMU fault 1290bd46b14cSgirish * status area is kept in the scratchpad register 0.) 12917c478bd9Sstevel@tonic-gate */ 12927c478bd9Sstevel@tonic-gate static const uint32_t tlbent[] = { 12937c478bd9Sstevel@tonic-gate #ifndef sun4v 12947c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 12957c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 12967c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 12977c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 12987c478bd9Sstevel@tonic-gate 0x85524000, /* rdpr %cwp, %g2 */ 12997c478bd9Sstevel@tonic-gate 0x87518000, /* rdpr %pstate, %g3 */ 13007c478bd9Sstevel@tonic-gate 0x8728f008, /* sllx %g3, 8, %g3 */ 13017c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 13027c478bd9Sstevel@tonic-gate 0x8740c000, /* rd %asi, %g3 */ 13037c478bd9Sstevel@tonic-gate 0x8728f018, /* sllx %g3, 24, %g3 */ 13047c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 13057c478bd9Sstevel@tonic-gate 0x8350c000, /* rdpr %tt, %g1 */ 13067c478bd9Sstevel@tonic-gate 0x8f902002, /* wrpr %g0, 2, %tl */ 13077c478bd9Sstevel@tonic-gate 0x85908000, /* wrpr %g2, %g0, %tstate */ 13087c478bd9Sstevel@tonic-gate 0x87904000, /* wrpr %g1, %g0, %tt */ 13097c478bd9Sstevel@tonic-gate 0xc2d80000, /* ldxa [%g0]ASI_MMU, %g1 */ 13107c478bd9Sstevel@tonic-gate 0x83307030, /* srlx %g1, CTXSHIFT, %g1 */ 13117c478bd9Sstevel@tonic-gate 0x02c04004, /* brz,pn %g1, .+0x10 */ 13127c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(new_tpc), %g1 */ 13137c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 13147c478bd9Sstevel@tonic-gate 0x30800002, /* ba,a .+0x8 */ 13157c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 13167c478bd9Sstevel@tonic-gate 0x81904000, /* wrpr %g1, %g0, %tpc */ 13177c478bd9Sstevel@tonic-gate 0x82006004, /* add %g1, 4, %g1 */ 13187c478bd9Sstevel@tonic-gate 0x83904000, /* wrpr %g1, %g0, %tnpc */ 13197c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(tmptick), %g1 */ 13207c478bd9Sstevel@tonic-gate 0x85410000, /* rd %tick, %g2 */ 13217c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(tmptick)] */ 13227c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 13237c478bd9Sstevel@tonic-gate NOP, NOP, NOP 13247c478bd9Sstevel@tonic-gate #else /* sun4v */ 132559ac0c16Sdavemq 0x82102008, /* mov SCRATCHPAD_CPUID, %g1 */ 132659ac0c16Sdavemq 0xc8d84400, /* ldxa [%g1]ASI_SCRATCHPAD, %g4 */ 132759ac0c16Sdavemq 0x89293000, /* sllx %g4, TSTAT_DATA_SHIFT, %g4 */ 13287c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 132959ac0c16Sdavemq 0x82004004, /* add %g1, %g4, %g1 */ 13307c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 13317c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 13327c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 13337c478bd9Sstevel@tonic-gate 0x85524000, /* rdpr %cwp, %g2 */ 13347c478bd9Sstevel@tonic-gate 0x87518000, /* rdpr %pstate, %g3 */ 13357c478bd9Sstevel@tonic-gate 0x8728f008, /* sllx %g3, 8, %g3 */ 13367c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 13377c478bd9Sstevel@tonic-gate 0x8740c000, /* rd %asi, %g3 */ 13387c478bd9Sstevel@tonic-gate 0x8728f018, /* sllx %g3, 24, %g3 */ 1339bd46b14cSgirish 0x83540000, /* rdpr %gl, %g1 */ 1340bd46b14cSgirish 0x83287028, /* sllx %g1, 40, %g1 */ 1341bd46b14cSgirish 0x86104003, /* or %g1, %g3, %g3 */ 13427c478bd9Sstevel@tonic-gate 0x84108003, /* or %g2, %g3, %g2 */ 13437c478bd9Sstevel@tonic-gate 0x8350c000, /* rdpr %tt, %g1 */ 13447c478bd9Sstevel@tonic-gate 0x8f902002, /* wrpr %g0, 2, %tl */ 13457c478bd9Sstevel@tonic-gate 0x85908000, /* wrpr %g2, %g0, %tstate */ 13467c478bd9Sstevel@tonic-gate 0x87904000, /* wrpr %g1, %g0, %tt */ 13477c478bd9Sstevel@tonic-gate 0xc2d80400, /* ldxa [%g0]ASI_SCRATCHPAD, %g1 */ 13487c478bd9Sstevel@tonic-gate 0xc2586000, /* ldx [%g1 + MMFSA_?_CTX], %g1 */ 13497c478bd9Sstevel@tonic-gate 0x02c04004, /* brz,pn %g1, .+0x10 */ 13507c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(new_tpc), %g1 */ 13517c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 13527c478bd9Sstevel@tonic-gate 0x30800002, /* ba,a .+0x8 */ 13537c478bd9Sstevel@tonic-gate 0x82106000, /* or %g1, %lo(new_tpc), %g1 */ 13547c478bd9Sstevel@tonic-gate 0x81904000, /* wrpr %g1, %g0, %tpc */ 13557c478bd9Sstevel@tonic-gate 0x82006004, /* add %g1, 4, %g1 */ 13567c478bd9Sstevel@tonic-gate 0x83904000, /* wrpr %g1, %g0, %tnpc */ 13577c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(tmptick), %g1 */ 135859ac0c16Sdavemq 0x82004004, /* add %g1, %g4, %g1 */ 13597c478bd9Sstevel@tonic-gate 0x85410000, /* rd %tick, %g2 */ 13607c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(tmptick)] */ 13614df55fdeSJanie Lu 0x05000000, /* sethi %hi(addr), %g2 */ 13624df55fdeSJanie Lu 0x8410a000, /* or %g2, %lo(addr), %g2 */ 13634df55fdeSJanie Lu 0x81c08000, /* jmp %g2 */ 13644df55fdeSJanie Lu NOP, 13657c478bd9Sstevel@tonic-gate #endif /* sun4v */ 13667c478bd9Sstevel@tonic-gate }; 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 1369ce0352ebSgirish #ifndef sun4v 13707c478bd9Sstevel@tonic-gate ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS); 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate stat = TSTAT_DATA_OFFS(tcpu, tdata_traps) + entoffs; 13737c478bd9Sstevel@tonic-gate tmptick = TSTAT_DATA_OFFS(tcpu, tdata_tmptick); 137459ac0c16Sdavemq #else /* sun4v */ 137559ac0c16Sdavemq ASSERT(entno == TSTAT_ENT_ITLBMISS || entno == TSTAT_ENT_DTLBMISS || 137659ac0c16Sdavemq entno == TSTAT_ENT_IMMUMISS || entno == TSTAT_ENT_DMMUMISS); 137759ac0c16Sdavemq 13784df55fdeSJanie Lu stat = TSTAT_CPU0_TLBDATA_OFFS(tcpu, tdata_traps[entno]); 13794df55fdeSJanie Lu tmptick = TSTAT_CPU0_TLBDATA_OFFS(tcpu, tdata_tmptick); 138059ac0c16Sdavemq #endif /* sun4v */ 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate if (itlb) { 13837c478bd9Sstevel@tonic-gate ret = &tcpu->tcpu_instr->tinst_itlbret; 13847c478bd9Sstevel@tonic-gate udata = &data->tpgsz_user.tmode_itlb; 13857c478bd9Sstevel@tonic-gate kdata = &data->tpgsz_kernel.tmode_itlb; 13867c478bd9Sstevel@tonic-gate tpc = TSTAT_INSTR_OFFS(tcpu, tinst_itlbret.ttlbr_ktlb); 13877c478bd9Sstevel@tonic-gate } else { 13887c478bd9Sstevel@tonic-gate ret = &tcpu->tcpu_instr->tinst_dtlbret; 13897c478bd9Sstevel@tonic-gate udata = &data->tpgsz_user.tmode_dtlb; 13907c478bd9Sstevel@tonic-gate kdata = &data->tpgsz_kernel.tmode_dtlb; 13917c478bd9Sstevel@tonic-gate tpc = TSTAT_INSTR_OFFS(tcpu, tinst_dtlbret.ttlbr_ktlb); 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate utpc = tpc + offsetof(tstat_tlbret_t, ttlbr_utlb) - 13957c478bd9Sstevel@tonic-gate offsetof(tstat_tlbret_t, ttlbr_ktlb); 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate ASSERT(HI22(tpc) == HI22(utpc)); 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate ent = (uint32_t *)((uintptr_t)tcpu->tcpu_instr + entoffs); 14007c478bd9Sstevel@tonic-gate orig = KERNELBASE + entoffs; 14017c478bd9Sstevel@tonic-gate va = (uintptr_t)tcpu->tcpu_ibase + entoffs; 14027c478bd9Sstevel@tonic-gate 1403ce0352ebSgirish #ifdef sun4v 1404ce0352ebSgirish /* 140559ac0c16Sdavemq * Because of lack of space, interposing tlbent trap handler 140659ac0c16Sdavemq * for TLB and MMU miss traps cannot be placed in-line. Instead, 140759ac0c16Sdavemq * we copy it to the space set aside for shared trap handlers 140859ac0c16Sdavemq * continuation in the interposing trap table and invoke it by 140959ac0c16Sdavemq * placing a branch in the trap table itself. 1410ce0352ebSgirish */ 141159ac0c16Sdavemq tent = ent; /* trap vector entry */ 141259ac0c16Sdavemq tentva = va; /* trap vector entry va */ 1413ce0352ebSgirish 1414ce0352ebSgirish if (itlb) { 1415ce0352ebSgirish ent = (uint32_t *)((uintptr_t) 1416ce0352ebSgirish &tcpu->tcpu_instr->tinst_immumiss); 1417ce0352ebSgirish va = TSTAT_INSTR_OFFS(tcpu, tinst_immumiss); 1418ce0352ebSgirish } else { 1419ce0352ebSgirish ent = (uint32_t *)((uintptr_t) 1420ce0352ebSgirish &tcpu->tcpu_instr->tinst_dmmumiss); 1421ce0352ebSgirish va = TSTAT_INSTR_OFFS(tcpu, tinst_dmmumiss); 1422ce0352ebSgirish } 1423ce0352ebSgirish bcopy(mmumiss, tent, sizeof (mmumiss)); 1424ce0352ebSgirish tent[0] |= DISP22(tentva, va); 1425ce0352ebSgirish #endif /* sun4v */ 1426ce0352ebSgirish 14277c478bd9Sstevel@tonic-gate bcopy(tlbent, ent, sizeof (tlbent)); 14287c478bd9Sstevel@tonic-gate 142959ac0c16Sdavemq #if defined(sun4v) 143059ac0c16Sdavemq ent[TSTAT_TLBENT_TDATASHFT] |= LO10((uintptr_t)TSTAT_DATA_SHIFT); 143159ac0c16Sdavemq #endif 14327c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATHI] |= HI22(stat); 14337c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATLO_LD] |= LO10(stat); 14347c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_STATLO_ST] |= LO10(stat); 14357c478bd9Sstevel@tonic-gate #ifndef sun4v 14367c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_MMUASI] |= asi; 14377c478bd9Sstevel@tonic-gate #else 14387c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TAGTARGET] |= tagtarget_off; 14397c478bd9Sstevel@tonic-gate #endif 14407c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCHI] |= HI22(tpc); 14417c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCLO_USER] |= LO10(utpc); 14427c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TPCLO_KERN] |= LO10(tpc); 14437c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TSHI] |= HI22(tmptick); 14447c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_TSLO] |= LO10(tmptick); 14454df55fdeSJanie Lu #ifndef sun4v 14464df55fdeSJanie Lu baoffs = TSTAT_TLBENT_BA * sizeof (uint32_t); 14477c478bd9Sstevel@tonic-gate ent[TSTAT_TLBENT_BA] |= DISP22(va + baoffs, orig); 14484df55fdeSJanie Lu #else 14494df55fdeSJanie Lu ent[TSTAT_TLBENT_ADDRHI] |= HI22(orig); 14504df55fdeSJanie Lu ent[TSTAT_TLBENT_ADDRLO] |= LO10(orig); 14514df55fdeSJanie Lu #endif /* sun4v */ 14527c478bd9Sstevel@tonic-gate 14537c478bd9Sstevel@tonic-gate /* 14547c478bd9Sstevel@tonic-gate * And now set up the TLB return entries. 14557c478bd9Sstevel@tonic-gate */ 14567c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_ktlb, &kdata->ttlb_tlb); 14577c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_ktsb, &kdata->ttlb_tsb); 14587c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_utlb, &udata->ttlb_tlb); 14597c478bd9Sstevel@tonic-gate trapstat_tlbretent(tcpu, &ret->ttlbr_utsb, &udata->ttlb_tsb); 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 146259ac0c16Sdavemq #if defined(sun4v) 146359ac0c16Sdavemq #undef TSTAT_TLBENT_TDATASHFT 146459ac0c16Sdavemq #endif 14657c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATHI 14667c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_LD 14677c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_STATLO_ST 14687c478bd9Sstevel@tonic-gate #ifndef sun4v 14697c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_MMUASI 14707c478bd9Sstevel@tonic-gate #else 14717c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TAGTARGET 14727c478bd9Sstevel@tonic-gate #endif 14737c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCHI 14747c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_USER 14757c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TPCLO_KERN 14767c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSHI 14777c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_TSLO 14787c478bd9Sstevel@tonic-gate #undef TSTAT_TLBENT_BA 14797c478bd9Sstevel@tonic-gate 14807c478bd9Sstevel@tonic-gate /* 14817c478bd9Sstevel@tonic-gate * The TSTAT_ENABLED_* constants define offsets in the enabled entry; the 14827c478bd9Sstevel@tonic-gate * TSTAT_DISABLED_BA constant defines an offset in the disabled entry. Both 14837c478bd9Sstevel@tonic-gate * sets of constants are used only in trapstat_make_traptab() (below) and 14847c478bd9Sstevel@tonic-gate * #undef'd immediately afterwards. Any change to "enabled" or "disabled" 14857c478bd9Sstevel@tonic-gate * in trapstat_make_traptab() will likely require changes to these constants. 14867c478bd9Sstevel@tonic-gate */ 148759ac0c16Sdavemq #ifndef sun4v 14887c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATHI 0 14897c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATLO_LD 1 14907c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_STATLO_ST 3 14917c478bd9Sstevel@tonic-gate #define TSTAT_ENABLED_BA 4 14927c478bd9Sstevel@tonic-gate #define TSTAT_DISABLED_BA 0 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate static void 14957c478bd9Sstevel@tonic-gate trapstat_make_traptab(tstat_percpu_t *tcpu) 14967c478bd9Sstevel@tonic-gate { 14977c478bd9Sstevel@tonic-gate uint32_t *ent; 14987c478bd9Sstevel@tonic-gate uint64_t *stat; 14997c478bd9Sstevel@tonic-gate uintptr_t orig, va, en_baoffs, dis_baoffs; 15007c478bd9Sstevel@tonic-gate int nent; 15017c478bd9Sstevel@tonic-gate 15027c478bd9Sstevel@tonic-gate /* 15037c478bd9Sstevel@tonic-gate * This is the entry in the interposing trap table for enabled trap 15047c478bd9Sstevel@tonic-gate * table entries. It loads a counter, increments it and stores it 15057c478bd9Sstevel@tonic-gate * back before branching to the actual trap table entry. 15067c478bd9Sstevel@tonic-gate */ 15077c478bd9Sstevel@tonic-gate static const uint32_t enabled[TSTAT_ENT_NINSTR] = { 15087c478bd9Sstevel@tonic-gate 0x03000000, /* sethi %hi(stat), %g1 */ 15097c478bd9Sstevel@tonic-gate 0xc4586000, /* ldx [%g1 + %lo(stat)], %g2 */ 15107c478bd9Sstevel@tonic-gate 0x8400a001, /* add %g2, 1, %g2 */ 15117c478bd9Sstevel@tonic-gate 0xc4706000, /* stx %g2, [%g1 + %lo(stat)] */ 15127c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 15137c478bd9Sstevel@tonic-gate NOP, NOP, NOP 15147c478bd9Sstevel@tonic-gate }; 15157c478bd9Sstevel@tonic-gate 15167c478bd9Sstevel@tonic-gate /* 15177c478bd9Sstevel@tonic-gate * This is the entry in the interposing trap table for disabled trap 15187c478bd9Sstevel@tonic-gate * table entries. It simply branches to the actual, underlying trap 15197c478bd9Sstevel@tonic-gate * table entry. As explained in the "Implementation Details" section 15207c478bd9Sstevel@tonic-gate * of the block comment, all TL>0 traps _must_ use the disabled entry; 15217c478bd9Sstevel@tonic-gate * additional entries may be explicitly disabled through the use 15227c478bd9Sstevel@tonic-gate * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY. 15237c478bd9Sstevel@tonic-gate */ 15247c478bd9Sstevel@tonic-gate static const uint32_t disabled[TSTAT_ENT_NINSTR] = { 15257c478bd9Sstevel@tonic-gate 0x30800000, /* ba,a addr */ 15267c478bd9Sstevel@tonic-gate NOP, NOP, NOP, NOP, NOP, NOP, NOP, 15277c478bd9Sstevel@tonic-gate }; 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 15307c478bd9Sstevel@tonic-gate 15317c478bd9Sstevel@tonic-gate ent = tcpu->tcpu_instr->tinst_traptab; 15327c478bd9Sstevel@tonic-gate stat = (uint64_t *)TSTAT_DATA_OFFS(tcpu, tdata_traps); 15337c478bd9Sstevel@tonic-gate orig = KERNELBASE; 15347c478bd9Sstevel@tonic-gate va = (uintptr_t)tcpu->tcpu_ibase; 15357c478bd9Sstevel@tonic-gate en_baoffs = TSTAT_ENABLED_BA * sizeof (uint32_t); 15367c478bd9Sstevel@tonic-gate dis_baoffs = TSTAT_DISABLED_BA * sizeof (uint32_t); 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) { 15397c478bd9Sstevel@tonic-gate if (tstat_enabled[nent]) { 15407c478bd9Sstevel@tonic-gate bcopy(enabled, ent, sizeof (enabled)); 15419f1a1f17Sdmick ent[TSTAT_ENABLED_STATHI] |= HI22((uintptr_t)stat); 15429f1a1f17Sdmick ent[TSTAT_ENABLED_STATLO_LD] |= LO10((uintptr_t)stat); 15439f1a1f17Sdmick ent[TSTAT_ENABLED_STATLO_ST] |= LO10((uintptr_t)stat); 15447c478bd9Sstevel@tonic-gate ent[TSTAT_ENABLED_BA] |= DISP22(va + en_baoffs, orig); 15457c478bd9Sstevel@tonic-gate } else { 15467c478bd9Sstevel@tonic-gate bcopy(disabled, ent, sizeof (disabled)); 15477c478bd9Sstevel@tonic-gate ent[TSTAT_DISABLED_BA] |= DISP22(va + dis_baoffs, orig); 15487c478bd9Sstevel@tonic-gate } 15497c478bd9Sstevel@tonic-gate 15507c478bd9Sstevel@tonic-gate stat++; 15517c478bd9Sstevel@tonic-gate orig += sizeof (enabled); 15527c478bd9Sstevel@tonic-gate ent += sizeof (enabled) / sizeof (*ent); 15537c478bd9Sstevel@tonic-gate va += sizeof (enabled); 15547c478bd9Sstevel@tonic-gate } 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate 15577c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATHI 15587c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_LD 15597c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_STATLO_ST 15607c478bd9Sstevel@tonic-gate #undef TSTAT_ENABLED_BA 15617c478bd9Sstevel@tonic-gate #undef TSTAT_DISABLED_BA 15627c478bd9Sstevel@tonic-gate 156359ac0c16Sdavemq #else /* sun4v */ 156459ac0c16Sdavemq 156559ac0c16Sdavemq #define TSTAT_ENABLED_STATHI 0 156659ac0c16Sdavemq #define TSTAT_ENABLED_STATLO 1 156759ac0c16Sdavemq #define TSTAT_ENABLED_ADDRHI 2 156859ac0c16Sdavemq #define TSTAT_ENABLED_ADDRLO 3 156959ac0c16Sdavemq #define TSTAT_ENABLED_CONTBA 6 157059ac0c16Sdavemq #define TSTAT_ENABLED_TDATASHFT 7 15714df55fdeSJanie Lu #define TSTAT_DISABLED_ADDRHI 0 15724df55fdeSJanie Lu #define TSTAT_DISABLED_ADDRLO 1 157359ac0c16Sdavemq 157459ac0c16Sdavemq static void 157559ac0c16Sdavemq trapstat_make_traptab(tstat_percpu_t *tcpu) 157659ac0c16Sdavemq { 157759ac0c16Sdavemq uint32_t *ent; 157859ac0c16Sdavemq uint64_t *stat; 15794df55fdeSJanie Lu uintptr_t orig, va, en_baoffs; 158059ac0c16Sdavemq uintptr_t tstat_cont_va; 158159ac0c16Sdavemq int nent; 158259ac0c16Sdavemq 158359ac0c16Sdavemq /* 158459ac0c16Sdavemq * This is the entry in the interposing trap table for enabled trap 158559ac0c16Sdavemq * table entries. It loads a counter, increments it and stores it 158659ac0c16Sdavemq * back before branching to the actual trap table entry. 158759ac0c16Sdavemq * 158859ac0c16Sdavemq * All CPUs share the same interposing trap entry to count the 158959ac0c16Sdavemq * number of traps. Note that the trap counter is kept in per CPU 159059ac0c16Sdavemq * trap statistics area. Its address is obtained dynamically by 159159ac0c16Sdavemq * adding the offset of that CPU's trap statistics area from CPU 0 159259ac0c16Sdavemq * (i.e. cpu_id * TSTAT_DATA_SIZE) to the address of the CPU 0 159359ac0c16Sdavemq * trap counter already coded in the interposing trap entry itself. 159459ac0c16Sdavemq * 159559ac0c16Sdavemq * Since this interposing code sequence to count traps takes more 159659ac0c16Sdavemq * than 8 instructions, it's split in two parts as follows: 159759ac0c16Sdavemq * 159859ac0c16Sdavemq * tstat_trapcnt: 159959ac0c16Sdavemq * sethi %hi(stat), %g1 160059ac0c16Sdavemq * or %g1, %lo[stat), %g1 ! %g1 = CPU0 trap counter addr 160159ac0c16Sdavemq * sethi %hi(addr), %g2 160259ac0c16Sdavemq * or %g2, %lo(addr), %g2 ! %g2 = real trap handler addr 160359ac0c16Sdavemq * mov ASI_SCRATCHPAD_CPUID, %g3 160459ac0c16Sdavemq * ldxa [%g3]ASI_SCRATCHPAD, %g3 ! %g3 = CPU ID 160559ac0c16Sdavemq * ba tstat_trapcnt_cont ! branch to tstat_trapcnt_cont 160659ac0c16Sdavemq * sllx %g3, TSTAT_DATA_SHIFT, %g3 ! %g3 = CPU trapstat data offset 160759ac0c16Sdavemq * 160859ac0c16Sdavemq * tstat_trapcnt_cont: 160959ac0c16Sdavemq * ldx [%g1 + %g3], %g4 ! get counter value 161059ac0c16Sdavemq * add %g4, 1, %g4 ! increment value 161159ac0c16Sdavemq * jmp %g2 ! jump to original trap handler 161259ac0c16Sdavemq * stx %g4, [%g1 + %g3] ! store counter value 161359ac0c16Sdavemq * 161459ac0c16Sdavemq * First part, i.e. tstat_trapcnt, is per trap and is kept in-line in 161559ac0c16Sdavemq * the interposing trap table. However, the tstat_trapcnt_cont code 161659ac0c16Sdavemq * sequence is shared by all traps and is kept right after the 161759ac0c16Sdavemq * the interposing trap table. 161859ac0c16Sdavemq */ 161959ac0c16Sdavemq static const uint32_t enabled[TSTAT_ENT_NINSTR] = { 162059ac0c16Sdavemq 0x03000000, /* sethi %hi(stat), %g1 */ 162159ac0c16Sdavemq 0x82106000, /* or %g1, %lo[stat), %g1 */ 162259ac0c16Sdavemq 0x05000000, /* sethi %hi(addr), %g2 */ 162359ac0c16Sdavemq 0x8410a000, /* or %g2, %lo(addr), %g2 */ 162459ac0c16Sdavemq 0x86102008, /* mov ASI_SCRATCHPAD_CPUID, %g3 */ 162559ac0c16Sdavemq 0xc6d8c400, /* ldxa [%g3]ASI_SCRATCHPAD, %g3 */ 162659ac0c16Sdavemq 0x10800000, /* ba enabled_cont */ 162759ac0c16Sdavemq 0x8728f000 /* sllx %g3, TSTAT_DATA_SHIFT, %g3 */ 162859ac0c16Sdavemq }; 162959ac0c16Sdavemq 163059ac0c16Sdavemq static const uint32_t enabled_cont[TSTAT_ENT_NINSTR] = { 163159ac0c16Sdavemq 0xc8584003, /* ldx [%g1 + %g3], %g4 */ 163259ac0c16Sdavemq 0x88012001, /* add %g4, 1, %g4 */ 163359ac0c16Sdavemq 0x81c08000, /* jmp %g2 */ 163459ac0c16Sdavemq 0xc8704003, /* stx %g4, [%g1 + %g3] */ 163559ac0c16Sdavemq NOP, NOP, NOP, NOP 163659ac0c16Sdavemq }; 163759ac0c16Sdavemq 163859ac0c16Sdavemq /* 163959ac0c16Sdavemq * This is the entry in the interposing trap table for disabled trap 16404df55fdeSJanie Lu * table entries. It simply "jmp" to the actual, underlying trap 164159ac0c16Sdavemq * table entry. As explained in the "Implementation Details" section 164259ac0c16Sdavemq * of the block comment, all TL>0 traps _must_ use the disabled entry; 164359ac0c16Sdavemq * additional entries may be explicitly disabled through the use 164459ac0c16Sdavemq * of TSTATIOC_ENTRY/TSTATIOC_NOENTRY. 164559ac0c16Sdavemq */ 164659ac0c16Sdavemq static const uint32_t disabled[TSTAT_ENT_NINSTR] = { 16474df55fdeSJanie Lu 0x05000000, /* sethi %hi(addr), %g2 */ 16484df55fdeSJanie Lu 0x8410a000, /* or %g2, %lo(addr), %g2 */ 16494df55fdeSJanie Lu 0x81c08000, /* jmp %g2 */ 16504df55fdeSJanie Lu NOP, NOP, NOP, NOP, NOP, 165159ac0c16Sdavemq }; 165259ac0c16Sdavemq 165359ac0c16Sdavemq ASSERT(MUTEX_HELD(&tstat_lock)); 165459ac0c16Sdavemq ent = tcpu->tcpu_instr->tinst_traptab; 165559ac0c16Sdavemq stat = (uint64_t *)TSTAT_CPU0_DATA_OFFS(tcpu, tdata_traps); 165659ac0c16Sdavemq orig = KERNELBASE; 165759ac0c16Sdavemq va = (uintptr_t)tcpu->tcpu_ibase; 165859ac0c16Sdavemq en_baoffs = TSTAT_ENABLED_CONTBA * sizeof (uint32_t); 165959ac0c16Sdavemq tstat_cont_va = TSTAT_INSTR_OFFS(tcpu, tinst_trapcnt); 166059ac0c16Sdavemq 166159ac0c16Sdavemq for (nent = 0; nent < TSTAT_TOTAL_NENT; nent++) { 16624df55fdeSJanie Lu /* 16634df55fdeSJanie Lu * If TSTAT_OPT_TLBDATA option is enabled (-t or -T option) 16644df55fdeSJanie Lu * we make sure only TSTAT_TLB_NENT traps can be enabled. 16654df55fdeSJanie Lu * Note that this logic is somewhat moot since trapstat 16664df55fdeSJanie Lu * cmd actually use TSTATIOC_NOENTRY ioctl to disable all 16674df55fdeSJanie Lu * traps when performing Tlb stats collection. 16684df55fdeSJanie Lu */ 16694df55fdeSJanie Lu if ((!(tstat_options & TSTAT_OPT_TLBDATA) || 16704df55fdeSJanie Lu nent < TSTAT_TLB_NENT) && tstat_enabled[nent]) { 167159ac0c16Sdavemq bcopy(enabled, ent, sizeof (enabled)); 167259ac0c16Sdavemq ent[TSTAT_ENABLED_STATHI] |= HI22((uintptr_t)stat); 167359ac0c16Sdavemq ent[TSTAT_ENABLED_STATLO] |= LO10((uintptr_t)stat); 167459ac0c16Sdavemq ent[TSTAT_ENABLED_ADDRHI] |= HI22((uintptr_t)orig); 167559ac0c16Sdavemq ent[TSTAT_ENABLED_ADDRLO] |= LO10((uintptr_t)orig); 167659ac0c16Sdavemq ent[TSTAT_ENABLED_CONTBA] |= 167759ac0c16Sdavemq DISP22(va + en_baoffs, tstat_cont_va); 167859ac0c16Sdavemq ent[TSTAT_ENABLED_TDATASHFT] |= 167959ac0c16Sdavemq LO10((uintptr_t)TSTAT_DATA_SHIFT); 168059ac0c16Sdavemq } else { 168159ac0c16Sdavemq bcopy(disabled, ent, sizeof (disabled)); 16824df55fdeSJanie Lu ent[TSTAT_DISABLED_ADDRHI] |= HI22((uintptr_t)orig); 16834df55fdeSJanie Lu ent[TSTAT_DISABLED_ADDRLO] |= LO10((uintptr_t)orig); 168459ac0c16Sdavemq } 168559ac0c16Sdavemq 168659ac0c16Sdavemq stat++; 168759ac0c16Sdavemq orig += sizeof (enabled); 168859ac0c16Sdavemq ent += sizeof (enabled) / sizeof (*ent); 168959ac0c16Sdavemq va += sizeof (enabled); 169059ac0c16Sdavemq } 169159ac0c16Sdavemq bcopy(enabled_cont, (uint32_t *)tcpu->tcpu_instr->tinst_trapcnt, 169259ac0c16Sdavemq sizeof (enabled_cont)); 169359ac0c16Sdavemq } 169459ac0c16Sdavemq 169559ac0c16Sdavemq #undef TSTAT_ENABLED_TDATASHFT 169659ac0c16Sdavemq #undef TSTAT_ENABLED_STATHI 169759ac0c16Sdavemq #undef TSTAT_ENABLED_STATLO 169859ac0c16Sdavemq #undef TSTAT_ENABLED_ADDRHI 169959ac0c16Sdavemq #undef TSTAT_ENABLED_ADDRLO 170059ac0c16Sdavemq #undef TSTAT_ENABLED_CONTBA 170159ac0c16Sdavemq #undef TSTAT_DISABLED_BA 170259ac0c16Sdavemq 170359ac0c16Sdavemq #endif /* sun4v */ 170459ac0c16Sdavemq 170525cf1a30Sjl139090 #ifndef sun4v 170625cf1a30Sjl139090 /* 170725cf1a30Sjl139090 * See Section A.6 in SPARC v9 Manual. 170825cf1a30Sjl139090 * max branch = 4*((2^21)-1) = 8388604 170925cf1a30Sjl139090 */ 171025cf1a30Sjl139090 #define MAX_BICC_BRANCH_DISPLACEMENT (4 * ((1 << 21) - 1)) 171125cf1a30Sjl139090 #endif 171225cf1a30Sjl139090 17137c478bd9Sstevel@tonic-gate static void 17147c478bd9Sstevel@tonic-gate trapstat_setup(processorid_t cpu) 17157c478bd9Sstevel@tonic-gate { 17167c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 17177c478bd9Sstevel@tonic-gate #ifndef sun4v 17187c478bd9Sstevel@tonic-gate int i; 17197c478bd9Sstevel@tonic-gate caddr_t va; 17207c478bd9Sstevel@tonic-gate pfn_t *pfn; 172125cf1a30Sjl139090 cpu_t *cp; 172225cf1a30Sjl139090 uint_t strand_idx; 172325cf1a30Sjl139090 size_t tstat_offset; 17244df55fdeSJanie Lu #else 17254df55fdeSJanie Lu uint64_t offset; 17267c478bd9Sstevel@tonic-gate #endif 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_pfn == NULL); 17297c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_instr == NULL); 17307c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_data == NULL); 17317c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 17327c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED)); 17337c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 17347c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 17357c478bd9Sstevel@tonic-gate 173659ac0c16Sdavemq #ifndef sun4v 17377c478bd9Sstevel@tonic-gate /* 17387c478bd9Sstevel@tonic-gate * The lower fifteen bits of the %tba are always read as zero; we must 17397c478bd9Sstevel@tonic-gate * align our instruction base address appropriately. 17407c478bd9Sstevel@tonic-gate */ 174125cf1a30Sjl139090 tstat_offset = tstat_total_size; 174225cf1a30Sjl139090 174325cf1a30Sjl139090 cp = cpu_get(cpu); 174425cf1a30Sjl139090 ASSERT(cp != NULL); 1745fb2f18f8Sesaxe if ((strand_idx = cpu ^ pg_plat_hw_instance_id(cp, PGHW_IPIPE)) != 0) { 174625cf1a30Sjl139090 /* 174725cf1a30Sjl139090 * On sun4u platforms with multiple CPUs sharing the MMU 174825cf1a30Sjl139090 * (Olympus-C has 2 strands per core), each CPU uses a 174925cf1a30Sjl139090 * disjoint trap table. The indexing is based on the 175025cf1a30Sjl139090 * strand id, which is obtained by XOR'ing the cpuid with 175125cf1a30Sjl139090 * the coreid. 175225cf1a30Sjl139090 */ 175325cf1a30Sjl139090 tstat_offset += tstat_total_size * strand_idx; 175425cf1a30Sjl139090 175525cf1a30Sjl139090 /* 175625cf1a30Sjl139090 * Offset must be less than the maximum PC-relative branch 175725cf1a30Sjl139090 * displacement for Bicc variants. See the Implementation 175825cf1a30Sjl139090 * Details comment. 175925cf1a30Sjl139090 */ 176025cf1a30Sjl139090 ASSERT(tstat_offset <= MAX_BICC_BRANCH_DISPLACEMENT); 176125cf1a30Sjl139090 } 176225cf1a30Sjl139090 176325cf1a30Sjl139090 tcpu->tcpu_ibase = (caddr_t)((KERNELBASE - tstat_offset) 17647c478bd9Sstevel@tonic-gate & TSTAT_TBA_MASK); 17657c478bd9Sstevel@tonic-gate tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE; 17667c478bd9Sstevel@tonic-gate tcpu->tcpu_vabase = tcpu->tcpu_ibase; 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate tcpu->tcpu_pfn = vmem_alloc(tstat_arena, tstat_total_pages, VM_SLEEP); 17697c478bd9Sstevel@tonic-gate bzero(tcpu->tcpu_pfn, tstat_total_pages); 17707c478bd9Sstevel@tonic-gate pfn = tcpu->tcpu_pfn; 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate tcpu->tcpu_instr = vmem_alloc(tstat_arena, TSTAT_INSTR_SIZE, VM_SLEEP); 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate va = (caddr_t)tcpu->tcpu_instr; 17757c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_INSTR_PAGES; i++, va += MMU_PAGESIZE) 17767c478bd9Sstevel@tonic-gate *pfn++ = hat_getpfnum(kas.a_hat, va); 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate /* 17797c478bd9Sstevel@tonic-gate * We must be sure that the pages that we will use to examine the data 17807c478bd9Sstevel@tonic-gate * have the same virtual color as the pages to which the data is being 17817c478bd9Sstevel@tonic-gate * recorded, hence the alignment and phase constraints on the 17827c478bd9Sstevel@tonic-gate * allocation. 17837c478bd9Sstevel@tonic-gate */ 17847c478bd9Sstevel@tonic-gate tcpu->tcpu_data = vmem_xalloc(tstat_arena, tstat_data_size, 17857c478bd9Sstevel@tonic-gate shm_alignment, (uintptr_t)tcpu->tcpu_dbase & (shm_alignment - 1), 17867c478bd9Sstevel@tonic-gate 0, 0, NULL, VM_SLEEP); 17877c478bd9Sstevel@tonic-gate bzero(tcpu->tcpu_data, tstat_data_size); 17887c478bd9Sstevel@tonic-gate tcpu->tcpu_data->tdata_cpuid = cpu; 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate va = (caddr_t)tcpu->tcpu_data; 17917c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_data_pages; i++, va += MMU_PAGESIZE) 17927c478bd9Sstevel@tonic-gate *pfn++ = hat_getpfnum(kas.a_hat, va); 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate /* 17957c478bd9Sstevel@tonic-gate * Now that we have all of the instruction and data pages allocated, 17967c478bd9Sstevel@tonic-gate * make the trap table from scratch. 17977c478bd9Sstevel@tonic-gate */ 17987c478bd9Sstevel@tonic-gate trapstat_make_traptab(tcpu); 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate if (tstat_options & TSTAT_OPT_TLBDATA) { 18017c478bd9Sstevel@tonic-gate /* 18027c478bd9Sstevel@tonic-gate * TLB Statistics have been specified; set up the I- and D-TLB 18037c478bd9Sstevel@tonic-gate * entries and corresponding TLB return entries. 18047c478bd9Sstevel@tonic-gate */ 18057c478bd9Sstevel@tonic-gate trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS); 18067c478bd9Sstevel@tonic-gate trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS); 180759ac0c16Sdavemq } 180859ac0c16Sdavemq 180959ac0c16Sdavemq #else /* sun4v */ 181059ac0c16Sdavemq 181159ac0c16Sdavemq /* 181259ac0c16Sdavemq * The lower fifteen bits of the %tba are always read as zero; hence 181359ac0c16Sdavemq * it must be aligned at least on 512K boundary. 181459ac0c16Sdavemq */ 18154df55fdeSJanie Lu tcpu->tcpu_vabase = (caddr_t)(KERNELBASE - 18164df55fdeSJanie Lu MMU_PAGESIZE4M * tstat_num4m_mapping); 181759ac0c16Sdavemq tcpu->tcpu_ibase = tcpu->tcpu_vabase; 181859ac0c16Sdavemq tcpu->tcpu_dbase = tcpu->tcpu_ibase + TSTAT_INSTR_SIZE + 181959ac0c16Sdavemq cpu * TSTAT_DATA_SIZE; 182059ac0c16Sdavemq 18214df55fdeSJanie Lu tcpu->tcpu_pfn = &tstat_pfn[0]; 18224df55fdeSJanie Lu tcpu->tcpu_instr = (tstat_instr_t *)tstat_va[0]; 18234df55fdeSJanie Lu 18244df55fdeSJanie Lu offset = TSTAT_INSTR_SIZE + cpu * TSTAT_DATA_SIZE; 18254df55fdeSJanie Lu tcpu->tcpu_data = (tstat_data_t *)(tstat_va[offset >> MMU_PAGESHIFT4M] + 18264df55fdeSJanie Lu (offset & MMU_PAGEOFFSET4M)); 182759ac0c16Sdavemq bzero(tcpu->tcpu_data, TSTAT_DATA_SIZE); 182859ac0c16Sdavemq 182959ac0c16Sdavemq /* 183059ac0c16Sdavemq * Now that we have all of the instruction and data pages allocated, 183159ac0c16Sdavemq * make the trap table from scratch. It should be done only once 183259ac0c16Sdavemq * as it is shared by all CPUs. 183359ac0c16Sdavemq */ 183459ac0c16Sdavemq if (!tstat_traptab_initialized) 183559ac0c16Sdavemq trapstat_make_traptab(tcpu); 183659ac0c16Sdavemq 183759ac0c16Sdavemq if (tstat_options & TSTAT_OPT_TLBDATA) { 183859ac0c16Sdavemq /* 183959ac0c16Sdavemq * TLB Statistics have been specified; set up the I- and D-TLB 184059ac0c16Sdavemq * entries and corresponding TLB return entries. 184159ac0c16Sdavemq */ 184259ac0c16Sdavemq if (!tstat_traptab_initialized) { 1843ce0352ebSgirish if (tstat_fast_tlbstat) { 1844ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_IMMUMISS); 1845ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_DMMUMISS); 1846ce0352ebSgirish } else { 1847ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_ITLBMISS); 1848ce0352ebSgirish trapstat_tlbent(tcpu, TSTAT_ENT_DTLBMISS); 1849ce0352ebSgirish } 18507c478bd9Sstevel@tonic-gate } 185159ac0c16Sdavemq } 185259ac0c16Sdavemq tstat_traptab_initialized = 1; 185359ac0c16Sdavemq #endif /* sun4v */ 18547c478bd9Sstevel@tonic-gate 18557c478bd9Sstevel@tonic-gate tcpu->tcpu_flags |= TSTAT_CPU_ALLOCATED; 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate /* 18587c478bd9Sstevel@tonic-gate * Finally, get the target CPU to load the locked pages into its TLBs. 18597c478bd9Sstevel@tonic-gate */ 18607c478bd9Sstevel@tonic-gate xc_one(cpu, (xcfunc_t *)trapstat_load_tlb, 0, 0); 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate static void 18647c478bd9Sstevel@tonic-gate trapstat_teardown(processorid_t cpu) 18657c478bd9Sstevel@tonic-gate { 18667c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 18677c478bd9Sstevel@tonic-gate int i; 18687c478bd9Sstevel@tonic-gate caddr_t va = tcpu->tcpu_vabase; 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_pfn != NULL); 18717c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_instr != NULL); 18727c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_data != NULL); 18737c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 18747c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 18757c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 18767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18777c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tstat_lock)); 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate #ifndef sun4v 18807c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_pfn, tstat_total_pages); 18817c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_instr, TSTAT_INSTR_SIZE); 18827c478bd9Sstevel@tonic-gate vmem_free(tstat_arena, tcpu->tcpu_data, tstat_data_size); 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate for (i = 0; i < tstat_total_pages; i++, va += MMU_PAGESIZE) { 18851e2e7a75Shuah xt_one(cpu, vtag_flushpage_tl1, (uint64_t)va, 18861e2e7a75Shuah (uint64_t)ksfmmup); 18877c478bd9Sstevel@tonic-gate } 18887c478bd9Sstevel@tonic-gate #else 18894df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) { 18907c478bd9Sstevel@tonic-gate xt_one(cpu, vtag_unmap_perm_tl1, (uint64_t)va, KCONTEXT); 18914df55fdeSJanie Lu va += MMU_PAGESIZE4M; 18924df55fdeSJanie Lu } 18937c478bd9Sstevel@tonic-gate #endif 18947c478bd9Sstevel@tonic-gate 18957c478bd9Sstevel@tonic-gate tcpu->tcpu_pfn = NULL; 18967c478bd9Sstevel@tonic-gate tcpu->tcpu_instr = NULL; 18977c478bd9Sstevel@tonic-gate tcpu->tcpu_data = NULL; 18987c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED; 18997c478bd9Sstevel@tonic-gate } 19007c478bd9Sstevel@tonic-gate 19017c478bd9Sstevel@tonic-gate static int 19027c478bd9Sstevel@tonic-gate trapstat_go() 19037c478bd9Sstevel@tonic-gate { 19047c478bd9Sstevel@tonic-gate cpu_t *cp; 19054df55fdeSJanie Lu #ifdef sun4v 19064df55fdeSJanie Lu int i; 19074df55fdeSJanie Lu #endif /* sun4v */ 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 19107c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate if (tstat_running) { 19137c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 19147c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 19157c478bd9Sstevel@tonic-gate return (EBUSY); 19167c478bd9Sstevel@tonic-gate } 19177c478bd9Sstevel@tonic-gate 19187c478bd9Sstevel@tonic-gate #ifdef sun4v 19197c478bd9Sstevel@tonic-gate /* 19204df55fdeSJanie Lu * Compute the actual number of 4MB mappings 19214df55fdeSJanie Lu * we need based on the guest's ncpu_guest_max value. 19224df55fdeSJanie Lu * Note that earlier at compiled time, we did establish 19234df55fdeSJanie Lu * and check against the sun4v solaris arch limit 19244df55fdeSJanie Lu * (TSTAT_NUM4M_LIMIT) which is based on NCPU. 19257c478bd9Sstevel@tonic-gate */ 19264df55fdeSJanie Lu tstat_num4m_mapping = TSTAT_NUM4M_MACRO(ncpu_guest_max); 19274df55fdeSJanie Lu ASSERT(tstat_num4m_mapping <= TSTAT_NUM4M_LIMIT); 19284df55fdeSJanie Lu 19294df55fdeSJanie Lu /* 19304df55fdeSJanie Lu * Allocate large pages to hold interposing tables. 19314df55fdeSJanie Lu */ 19324df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) { 19334df55fdeSJanie Lu tstat_va[i] = contig_mem_alloc(MMU_PAGESIZE4M); 19344df55fdeSJanie Lu tstat_pfn[i] = va_to_pfn(tstat_va[i]); 19354df55fdeSJanie Lu if (tstat_pfn[i] == PFN_INVALID) { 19364df55fdeSJanie Lu int j; 19374df55fdeSJanie Lu for (j = 0; j < i; j++) { 19384df55fdeSJanie Lu contig_mem_free(tstat_va[j], MMU_PAGESIZE4M); 19394df55fdeSJanie Lu } 1940aaa10e67Sha137994 mutex_exit(&tstat_lock); 1941aaa10e67Sha137994 mutex_exit(&cpu_lock); 19427c478bd9Sstevel@tonic-gate return (EAGAIN); 1943aaa10e67Sha137994 } 19444df55fdeSJanie Lu } 19454df55fdeSJanie Lu 1946ce0352ebSgirish 1947ce0352ebSgirish /* 1948ce0352ebSgirish * For detailed TLB statistics, invoke CPU specific interface 1949ce0352ebSgirish * to see if it supports a low overhead interface to collect 1950ce0352ebSgirish * TSB hit statistics. If so, make set tstat_fast_tlbstat flag 1951ce0352ebSgirish * to reflect that. 1952ce0352ebSgirish */ 1953ce0352ebSgirish if (tstat_options & TSTAT_OPT_TLBDATA) { 1954ce0352ebSgirish int error; 1955ce0352ebSgirish 195659ac0c16Sdavemq tstat_fast_tlbstat = B_FALSE; 1957ce0352ebSgirish error = cpu_trapstat_conf(CPU_TSTATCONF_INIT); 1958ce0352ebSgirish if (error == 0) 1959ce0352ebSgirish tstat_fast_tlbstat = B_TRUE; 1960ce0352ebSgirish else if (error != ENOTSUP) { 19614df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) { 19624df55fdeSJanie Lu contig_mem_free(tstat_va[i], MMU_PAGESIZE4M); 19634df55fdeSJanie Lu } 1964aaa10e67Sha137994 mutex_exit(&tstat_lock); 1965aaa10e67Sha137994 mutex_exit(&cpu_lock); 1966ce0352ebSgirish return (error); 1967ce0352ebSgirish } 19687c478bd9Sstevel@tonic-gate } 19694df55fdeSJanie Lu 19704df55fdeSJanie Lu tstat_hv_nopanic = 1; 19714df55fdeSJanie Lu tstat_perm_mapping_failed = 0; 197259ac0c16Sdavemq #endif /* sun4v */ 19737c478bd9Sstevel@tonic-gate 19747c478bd9Sstevel@tonic-gate /* 19757c478bd9Sstevel@tonic-gate * First, perform any necessary hot patching. 19767c478bd9Sstevel@tonic-gate */ 19777c478bd9Sstevel@tonic-gate trapstat_hotpatch(); 19787c478bd9Sstevel@tonic-gate 19797c478bd9Sstevel@tonic-gate /* 19807c478bd9Sstevel@tonic-gate * Allocate the resources we'll need to measure probe effect. 19817c478bd9Sstevel@tonic-gate */ 19827c478bd9Sstevel@tonic-gate trapstat_probe_alloc(); 19837c478bd9Sstevel@tonic-gate 19847c478bd9Sstevel@tonic-gate cp = cpu_list; 19857c478bd9Sstevel@tonic-gate do { 19867c478bd9Sstevel@tonic-gate if (!(tstat_percpu[cp->cpu_id].tcpu_flags & TSTAT_CPU_SELECTED)) 19877c478bd9Sstevel@tonic-gate continue; 19887c478bd9Sstevel@tonic-gate 19897c478bd9Sstevel@tonic-gate trapstat_setup(cp->cpu_id); 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate /* 19927c478bd9Sstevel@tonic-gate * Note that due to trapstat_probe()'s use of global data, 19937c478bd9Sstevel@tonic-gate * we determine the probe effect on each CPU serially instead 19947c478bd9Sstevel@tonic-gate * of in parallel with an xc_all(). 19957c478bd9Sstevel@tonic-gate */ 19967c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_probe, 0, 0); 19974df55fdeSJanie Lu 19984df55fdeSJanie Lu #ifdef sun4v 19994df55fdeSJanie Lu /* 20004df55fdeSJanie Lu * Check to see if the first cpu's attempt to create 20014df55fdeSJanie Lu * the perm mappings failed. This might happen if the 20024df55fdeSJanie Lu * guest somehow exhausted all its limited perm mappings. 20034df55fdeSJanie Lu * Note that we only check this once for the first 20044df55fdeSJanie Lu * attempt since it shouldn't fail for subsequent cpus 20054df55fdeSJanie Lu * mapping the same TTEs if the first attempt was successful. 20064df55fdeSJanie Lu */ 20074df55fdeSJanie Lu if (tstat_hv_nopanic && tstat_perm_mapping_failed) { 20084df55fdeSJanie Lu tstat_percpu_t *tcpu = &tstat_percpu[cp->cpu_id]; 20094df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) { 20104df55fdeSJanie Lu contig_mem_free(tstat_va[i], MMU_PAGESIZE4M); 20114df55fdeSJanie Lu } 20124df55fdeSJanie Lu 20134df55fdeSJanie Lu /* 20144df55fdeSJanie Lu * Do clean up before returning. 20154df55fdeSJanie Lu * Cleanup is manageable since we 20164df55fdeSJanie Lu * only need to do it for the first cpu 20174df55fdeSJanie Lu * iteration that failed. 20184df55fdeSJanie Lu */ 20194df55fdeSJanie Lu trapstat_probe_free(); 20204df55fdeSJanie Lu trapstat_hotpatch(); 20214df55fdeSJanie Lu tcpu->tcpu_pfn = NULL; 20224df55fdeSJanie Lu tcpu->tcpu_instr = NULL; 20234df55fdeSJanie Lu tcpu->tcpu_data = NULL; 20244df55fdeSJanie Lu tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED; 20254df55fdeSJanie Lu mutex_exit(&tstat_lock); 20264df55fdeSJanie Lu mutex_exit(&cpu_lock); 20274df55fdeSJanie Lu return (EAGAIN); 20284df55fdeSJanie Lu } 20294df55fdeSJanie Lu tstat_hv_nopanic = 0; 20304df55fdeSJanie Lu #endif /* sun4v */ 20314df55fdeSJanie Lu 20327c478bd9Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list); 20337c478bd9Sstevel@tonic-gate 20347c478bd9Sstevel@tonic-gate xc_all((xcfunc_t *)trapstat_enable, 0, 0); 20357c478bd9Sstevel@tonic-gate 20367c478bd9Sstevel@tonic-gate trapstat_probe_free(); 20377c478bd9Sstevel@tonic-gate tstat_running = 1; 20387c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20397c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 20407c478bd9Sstevel@tonic-gate 20417c478bd9Sstevel@tonic-gate return (0); 20427c478bd9Sstevel@tonic-gate } 20437c478bd9Sstevel@tonic-gate 20447c478bd9Sstevel@tonic-gate static int 20457c478bd9Sstevel@tonic-gate trapstat_stop() 20467c478bd9Sstevel@tonic-gate { 20477c478bd9Sstevel@tonic-gate int i; 20487c478bd9Sstevel@tonic-gate 20497c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 20507c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 20517c478bd9Sstevel@tonic-gate if (!tstat_running) { 20527c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20537c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 20547c478bd9Sstevel@tonic-gate return (ENXIO); 20557c478bd9Sstevel@tonic-gate } 20567c478bd9Sstevel@tonic-gate 20577c478bd9Sstevel@tonic-gate xc_all((xcfunc_t *)trapstat_disable, 0, 0); 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) { 20607c478bd9Sstevel@tonic-gate if (tstat_percpu[i].tcpu_flags & TSTAT_CPU_ALLOCATED) 20617c478bd9Sstevel@tonic-gate trapstat_teardown(i); 20627c478bd9Sstevel@tonic-gate } 20637c478bd9Sstevel@tonic-gate 20647c478bd9Sstevel@tonic-gate #ifdef sun4v 206559ac0c16Sdavemq tstat_traptab_initialized = 0; 2066ce0352ebSgirish if (tstat_options & TSTAT_OPT_TLBDATA) 2067*07d06da5SSurya Prakki (void) cpu_trapstat_conf(CPU_TSTATCONF_FINI); 20684df55fdeSJanie Lu for (i = 0; i < tstat_num4m_mapping; i++) 20694df55fdeSJanie Lu contig_mem_free(tstat_va[i], MMU_PAGESIZE4M); 20707c478bd9Sstevel@tonic-gate #endif 20717c478bd9Sstevel@tonic-gate trapstat_hotpatch(); 20727c478bd9Sstevel@tonic-gate tstat_running = 0; 20737c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 20747c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate return (0); 20777c478bd9Sstevel@tonic-gate } 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate /* 20807c478bd9Sstevel@tonic-gate * This is trapstat's DR CPU configuration callback. It's called (with 20817c478bd9Sstevel@tonic-gate * cpu_lock held) to unconfigure a newly powered-off CPU, or to configure a 20827c478bd9Sstevel@tonic-gate * powered-off CPU that is to be brought into the system. We need only take 20837c478bd9Sstevel@tonic-gate * action in the unconfigure case: because a powered-off CPU will have its 20847c478bd9Sstevel@tonic-gate * trap table restored to KERNELBASE if it is ever powered back on, we must 20857c478bd9Sstevel@tonic-gate * update the flags to reflect that trapstat is no longer enabled on the 20867c478bd9Sstevel@tonic-gate * powered-off CPU. Note that this means that a TSTAT_CPU_ENABLED CPU that 20877c478bd9Sstevel@tonic-gate * is unconfigured/powered off and later powered back on/reconfigured will 20887c478bd9Sstevel@tonic-gate * _not_ be re-TSTAT_CPU_ENABLED. 20897c478bd9Sstevel@tonic-gate */ 20907c478bd9Sstevel@tonic-gate static int 20917c478bd9Sstevel@tonic-gate trapstat_cpu_setup(cpu_setup_t what, processorid_t cpu) 20927c478bd9Sstevel@tonic-gate { 20937c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cpu]; 20947c478bd9Sstevel@tonic-gate 20957c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 20967c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 20977c478bd9Sstevel@tonic-gate 20987c478bd9Sstevel@tonic-gate if (!tstat_running) { 20997c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 21007c478bd9Sstevel@tonic-gate return (0); 21017c478bd9Sstevel@tonic-gate } 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate switch (what) { 21047c478bd9Sstevel@tonic-gate case CPU_CONFIG: 21057c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 21067c478bd9Sstevel@tonic-gate break; 21077c478bd9Sstevel@tonic-gate 21087c478bd9Sstevel@tonic-gate case CPU_UNCONFIG: 21091ae08745Sheppo if (tcpu->tcpu_flags & TSTAT_CPU_ENABLED) { 21107c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_ENABLED; 21111ae08745Sheppo #ifdef sun4v 21121ae08745Sheppo /* 21131ae08745Sheppo * A power-off, causes the cpu mondo queues to be 21141ae08745Sheppo * unconfigured on sun4v. Since we can't teardown 21151ae08745Sheppo * trapstat's mappings on the cpu that is going away, 21161ae08745Sheppo * we simply mark it as not allocated. This will 21171ae08745Sheppo * prevent a teardown on a cpu with the same cpu id 21181ae08745Sheppo * that might have been added while trapstat is running. 21191ae08745Sheppo */ 21201ae08745Sheppo if (tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED) { 21211ae08745Sheppo tcpu->tcpu_pfn = NULL; 21221ae08745Sheppo tcpu->tcpu_instr = NULL; 21231ae08745Sheppo tcpu->tcpu_data = NULL; 21241ae08745Sheppo tcpu->tcpu_flags &= ~TSTAT_CPU_ALLOCATED; 21251ae08745Sheppo } 21261ae08745Sheppo #endif 21271ae08745Sheppo } 21287c478bd9Sstevel@tonic-gate break; 21297c478bd9Sstevel@tonic-gate 21307c478bd9Sstevel@tonic-gate default: 21317c478bd9Sstevel@tonic-gate break; 21327c478bd9Sstevel@tonic-gate } 21337c478bd9Sstevel@tonic-gate 21347c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 21357c478bd9Sstevel@tonic-gate return (0); 21367c478bd9Sstevel@tonic-gate } 21377c478bd9Sstevel@tonic-gate 21387c478bd9Sstevel@tonic-gate /* 21397c478bd9Sstevel@tonic-gate * This is called before a CPR suspend and after a CPR resume. We don't have 21407c478bd9Sstevel@tonic-gate * anything to do before a suspend, but after a restart we must restore the 21417c478bd9Sstevel@tonic-gate * trap table to be our interposing trap table. However, we don't actually 21427c478bd9Sstevel@tonic-gate * know whether or not the CPUs have been powered off -- this routine may be 21437c478bd9Sstevel@tonic-gate * called while restoring from a failed CPR suspend. We thus run through each 21447c478bd9Sstevel@tonic-gate * TSTAT_CPU_ENABLED CPU, and explicitly destroy and reestablish its 21457c478bd9Sstevel@tonic-gate * interposing trap table. This assures that our state is correct regardless 21467c478bd9Sstevel@tonic-gate * of whether or not the CPU has been newly powered on. 21477c478bd9Sstevel@tonic-gate */ 21487c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 21497c478bd9Sstevel@tonic-gate static boolean_t 21507c478bd9Sstevel@tonic-gate trapstat_cpr(void *arg, int code) 21517c478bd9Sstevel@tonic-gate { 21527c478bd9Sstevel@tonic-gate cpu_t *cp; 21537c478bd9Sstevel@tonic-gate 21547c478bd9Sstevel@tonic-gate if (code == CB_CODE_CPR_CHKPT) 21557c478bd9Sstevel@tonic-gate return (B_TRUE); 21567c478bd9Sstevel@tonic-gate 21577c478bd9Sstevel@tonic-gate ASSERT(code == CB_CODE_CPR_RESUME); 21587c478bd9Sstevel@tonic-gate 21597c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 21607c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 21617c478bd9Sstevel@tonic-gate 21627c478bd9Sstevel@tonic-gate if (!tstat_running) { 21637c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 21647c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 21657c478bd9Sstevel@tonic-gate return (B_TRUE); 21667c478bd9Sstevel@tonic-gate } 21677c478bd9Sstevel@tonic-gate 21687c478bd9Sstevel@tonic-gate cp = cpu_list; 21697c478bd9Sstevel@tonic-gate do { 21707c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[cp->cpu_id]; 21717c478bd9Sstevel@tonic-gate 21727c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 21737c478bd9Sstevel@tonic-gate continue; 21747c478bd9Sstevel@tonic-gate 21757c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 21767c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 21777c478bd9Sstevel@tonic-gate 21787c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_disable, 0, 0); 21797c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 21807c478bd9Sstevel@tonic-gate 21817c478bd9Sstevel@tonic-gate /* 21827c478bd9Sstevel@tonic-gate * Preserve this CPU's data in tstat_buffer and rip down its 21837c478bd9Sstevel@tonic-gate * interposing trap table. 21847c478bd9Sstevel@tonic-gate */ 21854df55fdeSJanie Lu #ifdef sun4v 21864df55fdeSJanie Lu bcopy(tcpu->tcpu_data, tstat_buffer, TSTAT_DATA_SIZE); 21874df55fdeSJanie Lu #else 21887c478bd9Sstevel@tonic-gate bcopy(tcpu->tcpu_data, tstat_buffer, tstat_data_t_size); 21894df55fdeSJanie Lu #endif /* sun4v */ 21907c478bd9Sstevel@tonic-gate trapstat_teardown(cp->cpu_id); 21917c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED)); 21927c478bd9Sstevel@tonic-gate 21937c478bd9Sstevel@tonic-gate /* 21947c478bd9Sstevel@tonic-gate * Reestablish the interposing trap table and restore the old 21957c478bd9Sstevel@tonic-gate * data. 21967c478bd9Sstevel@tonic-gate */ 21977c478bd9Sstevel@tonic-gate trapstat_setup(cp->cpu_id); 21987c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 21994df55fdeSJanie Lu #ifdef sun4v 22004df55fdeSJanie Lu bcopy(tstat_buffer, tcpu->tcpu_data, TSTAT_DATA_SIZE); 22014df55fdeSJanie Lu #else 22027c478bd9Sstevel@tonic-gate bcopy(tstat_buffer, tcpu->tcpu_data, tstat_data_t_size); 22034df55fdeSJanie Lu #endif /* sun4v */ 22047c478bd9Sstevel@tonic-gate 22057c478bd9Sstevel@tonic-gate xc_one(cp->cpu_id, (xcfunc_t *)trapstat_enable, 0, 0); 22067c478bd9Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list); 22077c478bd9Sstevel@tonic-gate 22087c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 22097c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 22107c478bd9Sstevel@tonic-gate 22117c478bd9Sstevel@tonic-gate return (B_TRUE); 22127c478bd9Sstevel@tonic-gate } 22137c478bd9Sstevel@tonic-gate 22147c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22157c478bd9Sstevel@tonic-gate static int 22167c478bd9Sstevel@tonic-gate trapstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 22177c478bd9Sstevel@tonic-gate { 22187c478bd9Sstevel@tonic-gate int i; 22197c478bd9Sstevel@tonic-gate 22207c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 22217c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 22227c478bd9Sstevel@tonic-gate if (tstat_open != 0) { 22237c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 22247c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 22257c478bd9Sstevel@tonic-gate return (EBUSY); 22267c478bd9Sstevel@tonic-gate } 22277c478bd9Sstevel@tonic-gate 22287c478bd9Sstevel@tonic-gate /* 22297c478bd9Sstevel@tonic-gate * Register this in open() rather than in attach() to prevent deadlock 22307c478bd9Sstevel@tonic-gate * with DR code. During attach, I/O device tree locks are grabbed 22317c478bd9Sstevel@tonic-gate * before trapstat_attach() is invoked - registering in attach 22327c478bd9Sstevel@tonic-gate * will result in the lock order: device tree lock, cpu_lock. 22337c478bd9Sstevel@tonic-gate * DR code however requires that cpu_lock be acquired before 22347c478bd9Sstevel@tonic-gate * device tree locks. 22357c478bd9Sstevel@tonic-gate */ 22367c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 22377c478bd9Sstevel@tonic-gate register_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL); 22387c478bd9Sstevel@tonic-gate 22397c478bd9Sstevel@tonic-gate /* 22407c478bd9Sstevel@tonic-gate * Clear all options. And until specific CPUs are specified, we'll 22417c478bd9Sstevel@tonic-gate * mark all CPUs as selected. 22427c478bd9Sstevel@tonic-gate */ 22437c478bd9Sstevel@tonic-gate tstat_options = 0; 22447c478bd9Sstevel@tonic-gate 22457c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) 22467c478bd9Sstevel@tonic-gate tstat_percpu[i].tcpu_flags |= TSTAT_CPU_SELECTED; 22477c478bd9Sstevel@tonic-gate 22487c478bd9Sstevel@tonic-gate /* 22497c478bd9Sstevel@tonic-gate * By default, all traps at TL=0 are enabled. Traps at TL>0 must 22507c478bd9Sstevel@tonic-gate * be disabled. 22517c478bd9Sstevel@tonic-gate */ 22527c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_TOTAL_NENT; i++) 22537c478bd9Sstevel@tonic-gate tstat_enabled[i] = i < TSTAT_NENT ? 1 : 0; 22547c478bd9Sstevel@tonic-gate 22557c478bd9Sstevel@tonic-gate tstat_open = 1; 22567c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 22577c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate return (0); 22607c478bd9Sstevel@tonic-gate } 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22637c478bd9Sstevel@tonic-gate static int 22647c478bd9Sstevel@tonic-gate trapstat_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 22657c478bd9Sstevel@tonic-gate { 22667c478bd9Sstevel@tonic-gate (void) trapstat_stop(); 22677c478bd9Sstevel@tonic-gate 22687c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 22697c478bd9Sstevel@tonic-gate 22707c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 22717c478bd9Sstevel@tonic-gate unregister_cpu_setup_func((cpu_setup_func_t *)trapstat_cpu_setup, NULL); 22727c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 22737c478bd9Sstevel@tonic-gate 22747c478bd9Sstevel@tonic-gate tstat_open = 0; 22757c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 22767c478bd9Sstevel@tonic-gate } 22777c478bd9Sstevel@tonic-gate 22787c478bd9Sstevel@tonic-gate static int 22797c478bd9Sstevel@tonic-gate trapstat_option(int option) 22807c478bd9Sstevel@tonic-gate { 22817c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 22827c478bd9Sstevel@tonic-gate 22837c478bd9Sstevel@tonic-gate if (tstat_running) { 22847c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 22857c478bd9Sstevel@tonic-gate return (EBUSY); 22867c478bd9Sstevel@tonic-gate } 22877c478bd9Sstevel@tonic-gate 22887c478bd9Sstevel@tonic-gate tstat_options |= option; 22897c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate return (0); 22927c478bd9Sstevel@tonic-gate } 22937c478bd9Sstevel@tonic-gate 22947c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22957c478bd9Sstevel@tonic-gate static int 22967c478bd9Sstevel@tonic-gate trapstat_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *crd, int *rval) 22977c478bd9Sstevel@tonic-gate { 22987c478bd9Sstevel@tonic-gate int i, j, out; 22997c478bd9Sstevel@tonic-gate size_t dsize; 23007c478bd9Sstevel@tonic-gate 23017c478bd9Sstevel@tonic-gate switch (cmd) { 23027c478bd9Sstevel@tonic-gate case TSTATIOC_GO: 23037c478bd9Sstevel@tonic-gate return (trapstat_go()); 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate case TSTATIOC_NOGO: 23067c478bd9Sstevel@tonic-gate return (trapstat_option(TSTAT_OPT_NOGO)); 23077c478bd9Sstevel@tonic-gate 23087c478bd9Sstevel@tonic-gate case TSTATIOC_STOP: 23097c478bd9Sstevel@tonic-gate return (trapstat_stop()); 23107c478bd9Sstevel@tonic-gate 23117c478bd9Sstevel@tonic-gate case TSTATIOC_CPU: 23127c478bd9Sstevel@tonic-gate if (arg < 0 || arg > max_cpuid) 23137c478bd9Sstevel@tonic-gate return (EINVAL); 23147c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 23157c478bd9Sstevel@tonic-gate 23167c478bd9Sstevel@tonic-gate case TSTATIOC_NOCPU: 23177c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 23187c478bd9Sstevel@tonic-gate 23197c478bd9Sstevel@tonic-gate if (tstat_running) { 23207c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23217c478bd9Sstevel@tonic-gate return (EBUSY); 23227c478bd9Sstevel@tonic-gate } 23237c478bd9Sstevel@tonic-gate 23247c478bd9Sstevel@tonic-gate /* 23257c478bd9Sstevel@tonic-gate * If this is the first CPU to be specified (or if we are 23267c478bd9Sstevel@tonic-gate * being asked to explicitly de-select CPUs), disable all CPUs. 23277c478bd9Sstevel@tonic-gate */ 23287c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_CPU) || cmd == TSTATIOC_NOCPU) { 23297c478bd9Sstevel@tonic-gate tstat_options |= TSTAT_OPT_CPU; 23307c478bd9Sstevel@tonic-gate 23317c478bd9Sstevel@tonic-gate for (i = 0; i <= max_cpuid; i++) { 23327c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[i]; 23337c478bd9Sstevel@tonic-gate 23347c478bd9Sstevel@tonic-gate ASSERT(cmd == TSTATIOC_NOCPU || 23357c478bd9Sstevel@tonic-gate (tcpu->tcpu_flags & TSTAT_CPU_SELECTED)); 23367c478bd9Sstevel@tonic-gate tcpu->tcpu_flags &= ~TSTAT_CPU_SELECTED; 23377c478bd9Sstevel@tonic-gate } 23387c478bd9Sstevel@tonic-gate } 23397c478bd9Sstevel@tonic-gate 23407c478bd9Sstevel@tonic-gate if (cmd == TSTATIOC_CPU) 23417c478bd9Sstevel@tonic-gate tstat_percpu[arg].tcpu_flags |= TSTAT_CPU_SELECTED; 23427c478bd9Sstevel@tonic-gate 23437c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate return (0); 23467c478bd9Sstevel@tonic-gate 23477c478bd9Sstevel@tonic-gate case TSTATIOC_ENTRY: 23487c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 23497c478bd9Sstevel@tonic-gate 23507c478bd9Sstevel@tonic-gate if (tstat_running) { 23517c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23527c478bd9Sstevel@tonic-gate return (EBUSY); 23537c478bd9Sstevel@tonic-gate } 23547c478bd9Sstevel@tonic-gate 23557c478bd9Sstevel@tonic-gate if (arg >= TSTAT_NENT || arg < 0) { 23567c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23577c478bd9Sstevel@tonic-gate return (EINVAL); 23587c478bd9Sstevel@tonic-gate } 23597c478bd9Sstevel@tonic-gate 23607c478bd9Sstevel@tonic-gate if (!(tstat_options & TSTAT_OPT_ENTRY)) { 23617c478bd9Sstevel@tonic-gate /* 23627c478bd9Sstevel@tonic-gate * If this is the first entry that we are explicitly 23637c478bd9Sstevel@tonic-gate * enabling, explicitly disable every TL=0 entry. 23647c478bd9Sstevel@tonic-gate */ 23657c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_NENT; i++) 23667c478bd9Sstevel@tonic-gate tstat_enabled[i] = 0; 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate tstat_options |= TSTAT_OPT_ENTRY; 23697c478bd9Sstevel@tonic-gate } 23707c478bd9Sstevel@tonic-gate 23717c478bd9Sstevel@tonic-gate tstat_enabled[arg] = 1; 23727c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23737c478bd9Sstevel@tonic-gate return (0); 23747c478bd9Sstevel@tonic-gate 23757c478bd9Sstevel@tonic-gate case TSTATIOC_NOENTRY: 23767c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 23777c478bd9Sstevel@tonic-gate 23787c478bd9Sstevel@tonic-gate if (tstat_running) { 23797c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23807c478bd9Sstevel@tonic-gate return (EBUSY); 23817c478bd9Sstevel@tonic-gate } 23827c478bd9Sstevel@tonic-gate 23837c478bd9Sstevel@tonic-gate for (i = 0; i < TSTAT_NENT; i++) 23847c478bd9Sstevel@tonic-gate tstat_enabled[i] = 0; 23857c478bd9Sstevel@tonic-gate 23867c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 23877c478bd9Sstevel@tonic-gate return (0); 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate case TSTATIOC_READ: 23907c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate if (tstat_options & TSTAT_OPT_TLBDATA) { 23937c478bd9Sstevel@tonic-gate dsize = tstat_data_t_exported_size; 23947c478bd9Sstevel@tonic-gate } else { 23957c478bd9Sstevel@tonic-gate dsize = sizeof (tstat_data_t); 23967c478bd9Sstevel@tonic-gate } 23977c478bd9Sstevel@tonic-gate 23987c478bd9Sstevel@tonic-gate for (i = 0, out = 0; i <= max_cpuid; i++) { 23997c478bd9Sstevel@tonic-gate tstat_percpu_t *tcpu = &tstat_percpu[i]; 24007c478bd9Sstevel@tonic-gate 24017c478bd9Sstevel@tonic-gate if (!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)) 24027c478bd9Sstevel@tonic-gate continue; 24037c478bd9Sstevel@tonic-gate 24047c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_SELECTED); 24057c478bd9Sstevel@tonic-gate ASSERT(tcpu->tcpu_flags & TSTAT_CPU_ALLOCATED); 24067c478bd9Sstevel@tonic-gate 24077c478bd9Sstevel@tonic-gate tstat_buffer->tdata_cpuid = -1; 24087c478bd9Sstevel@tonic-gate xc_one(i, (xcfunc_t *)trapstat_snapshot, 0, 0); 24097c478bd9Sstevel@tonic-gate 24107c478bd9Sstevel@tonic-gate if (tstat_buffer->tdata_cpuid == -1) { 24117c478bd9Sstevel@tonic-gate /* 24127c478bd9Sstevel@tonic-gate * This CPU is not currently responding to 24137c478bd9Sstevel@tonic-gate * cross calls; we have caught it while it is 24147c478bd9Sstevel@tonic-gate * being unconfigured. We'll drop tstat_lock 24157c478bd9Sstevel@tonic-gate * and pick up and drop cpu_lock. By the 24167c478bd9Sstevel@tonic-gate * time we acquire cpu_lock, the DR operation 24177c478bd9Sstevel@tonic-gate * will appear consistent and we can assert 24187c478bd9Sstevel@tonic-gate * that trapstat_cpu_setup() has cleared 24197c478bd9Sstevel@tonic-gate * TSTAT_CPU_ENABLED. 24207c478bd9Sstevel@tonic-gate */ 24217c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 24227c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 24237c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 24247c478bd9Sstevel@tonic-gate mutex_enter(&tstat_lock); 24257c478bd9Sstevel@tonic-gate ASSERT(!(tcpu->tcpu_flags & TSTAT_CPU_ENABLED)); 24267c478bd9Sstevel@tonic-gate continue; 24277c478bd9Sstevel@tonic-gate } 24287c478bd9Sstevel@tonic-gate 24297c478bd9Sstevel@tonic-gate /* 24307c478bd9Sstevel@tonic-gate * Need to compensate for the difference between page 24317c478bd9Sstevel@tonic-gate * sizes exported to users and page sizes available 24327c478bd9Sstevel@tonic-gate * within the kernel. 24337c478bd9Sstevel@tonic-gate */ 24347c478bd9Sstevel@tonic-gate if ((tstat_options & TSTAT_OPT_TLBDATA) && 24357c478bd9Sstevel@tonic-gate (tstat_pgszs != tstat_user_pgszs)) { 24367c478bd9Sstevel@tonic-gate tstat_pgszdata_t *tp; 24377c478bd9Sstevel@tonic-gate uint_t szc; 24387c478bd9Sstevel@tonic-gate 24397c478bd9Sstevel@tonic-gate tp = &tstat_buffer->tdata_pgsz[0]; 24407c478bd9Sstevel@tonic-gate for (j = 0; j < tstat_user_pgszs; j++) { 24417c478bd9Sstevel@tonic-gate if ((szc = USERSZC_2_SZC(j)) != j) { 24427c478bd9Sstevel@tonic-gate bcopy(&tp[szc], &tp[j], 24437c478bd9Sstevel@tonic-gate sizeof (tstat_pgszdata_t)); 24447c478bd9Sstevel@tonic-gate } 24457c478bd9Sstevel@tonic-gate } 24467c478bd9Sstevel@tonic-gate } 24477c478bd9Sstevel@tonic-gate 24487c478bd9Sstevel@tonic-gate if (copyout(tstat_buffer, (void *)arg, dsize) != 0) { 24497c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 24507c478bd9Sstevel@tonic-gate return (EFAULT); 24517c478bd9Sstevel@tonic-gate } 24527c478bd9Sstevel@tonic-gate 24537c478bd9Sstevel@tonic-gate out++; 24547c478bd9Sstevel@tonic-gate arg += dsize; 24557c478bd9Sstevel@tonic-gate } 24567c478bd9Sstevel@tonic-gate 24577c478bd9Sstevel@tonic-gate if (out != max_cpuid + 1) { 24587c478bd9Sstevel@tonic-gate processorid_t cpuid = -1; 24597c478bd9Sstevel@tonic-gate arg += offsetof(tstat_data_t, tdata_cpuid); 24607c478bd9Sstevel@tonic-gate 24617c478bd9Sstevel@tonic-gate if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) { 24627c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 24637c478bd9Sstevel@tonic-gate return (EFAULT); 24647c478bd9Sstevel@tonic-gate } 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate 24677c478bd9Sstevel@tonic-gate mutex_exit(&tstat_lock); 24687c478bd9Sstevel@tonic-gate 24697c478bd9Sstevel@tonic-gate return (0); 24707c478bd9Sstevel@tonic-gate 24717c478bd9Sstevel@tonic-gate case TSTATIOC_TLBDATA: 24727c478bd9Sstevel@tonic-gate return (trapstat_option(TSTAT_OPT_TLBDATA)); 24737c478bd9Sstevel@tonic-gate 24747c478bd9Sstevel@tonic-gate default: 24757c478bd9Sstevel@tonic-gate break; 24767c478bd9Sstevel@tonic-gate } 24777c478bd9Sstevel@tonic-gate 24787c478bd9Sstevel@tonic-gate return (ENOTTY); 24797c478bd9Sstevel@tonic-gate } 24807c478bd9Sstevel@tonic-gate 24817c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 24827c478bd9Sstevel@tonic-gate static int 24837c478bd9Sstevel@tonic-gate trapstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 24847c478bd9Sstevel@tonic-gate { 24857c478bd9Sstevel@tonic-gate int error; 24867c478bd9Sstevel@tonic-gate 24877c478bd9Sstevel@tonic-gate switch (infocmd) { 24887c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 24897c478bd9Sstevel@tonic-gate *result = (void *)tstat_devi; 24907c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 24917c478bd9Sstevel@tonic-gate break; 24927c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 24937c478bd9Sstevel@tonic-gate *result = (void *)0; 24947c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 24957c478bd9Sstevel@tonic-gate break; 24967c478bd9Sstevel@tonic-gate default: 24977c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 24987c478bd9Sstevel@tonic-gate } 24997c478bd9Sstevel@tonic-gate return (error); 25007c478bd9Sstevel@tonic-gate } 25017c478bd9Sstevel@tonic-gate 25027c478bd9Sstevel@tonic-gate static int 25037c478bd9Sstevel@tonic-gate trapstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 25047c478bd9Sstevel@tonic-gate { 25057c478bd9Sstevel@tonic-gate switch (cmd) { 25067c478bd9Sstevel@tonic-gate case DDI_ATTACH: 25077c478bd9Sstevel@tonic-gate break; 25087c478bd9Sstevel@tonic-gate 25097c478bd9Sstevel@tonic-gate case DDI_RESUME: 25107c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 25117c478bd9Sstevel@tonic-gate 25127c478bd9Sstevel@tonic-gate default: 25137c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 25147c478bd9Sstevel@tonic-gate } 25157c478bd9Sstevel@tonic-gate 25167c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "trapstat", S_IFCHR, 25177c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, 0) == DDI_FAILURE) { 25187c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 25197c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 25207c478bd9Sstevel@tonic-gate } 25217c478bd9Sstevel@tonic-gate 25227c478bd9Sstevel@tonic-gate ddi_report_dev(devi); 25237c478bd9Sstevel@tonic-gate tstat_devi = devi; 25247c478bd9Sstevel@tonic-gate 25257c478bd9Sstevel@tonic-gate tstat_pgszs = page_num_pagesizes(); 252602bc52beSkchow tstat_user_pgszs = page_num_user_pagesizes(0); 25277c478bd9Sstevel@tonic-gate tstat_data_t_size = sizeof (tstat_data_t) + 25287c478bd9Sstevel@tonic-gate (tstat_pgszs - 1) * sizeof (tstat_pgszdata_t); 25297c478bd9Sstevel@tonic-gate tstat_data_t_exported_size = sizeof (tstat_data_t) + 25307c478bd9Sstevel@tonic-gate (tstat_user_pgszs - 1) * sizeof (tstat_pgszdata_t); 25317c478bd9Sstevel@tonic-gate #ifndef sun4v 25327c478bd9Sstevel@tonic-gate tstat_data_pages = (tstat_data_t_size >> MMU_PAGESHIFT) + 1; 25337c478bd9Sstevel@tonic-gate tstat_total_pages = TSTAT_INSTR_PAGES + tstat_data_pages; 25347c478bd9Sstevel@tonic-gate tstat_data_size = tstat_data_pages * MMU_PAGESIZE; 25357c478bd9Sstevel@tonic-gate tstat_total_size = TSTAT_INSTR_SIZE + tstat_data_size; 25367c478bd9Sstevel@tonic-gate #else 25374df55fdeSJanie Lu /* 25384df55fdeSJanie Lu * For sun4v, the tstat_data_t_size reflect the tstat_buffer 25394df55fdeSJanie Lu * output size based on tstat_data_t structure. For tlbstats 25404df55fdeSJanie Lu * collection, we use the internal tstat_tdata_t structure 25414df55fdeSJanie Lu * to collect the tlbstats for the pages. Therefore we 25424df55fdeSJanie Lu * need to adjust the size for the assertion. 25434df55fdeSJanie Lu */ 25444df55fdeSJanie Lu ASSERT((tstat_data_t_size - sizeof (tstat_data_t) + 25454df55fdeSJanie Lu sizeof (tstat_tdata_t)) <= TSTAT_DATA_SIZE); 25467c478bd9Sstevel@tonic-gate #endif 25477c478bd9Sstevel@tonic-gate 25487c478bd9Sstevel@tonic-gate tstat_percpu = kmem_zalloc((max_cpuid + 1) * 25497c478bd9Sstevel@tonic-gate sizeof (tstat_percpu_t), KM_SLEEP); 25507c478bd9Sstevel@tonic-gate 25517c478bd9Sstevel@tonic-gate /* 25527c478bd9Sstevel@tonic-gate * Create our own arena backed by segkmem to assure a source of 25537c478bd9Sstevel@tonic-gate * MMU_PAGESIZE-aligned allocations. We allocate out of the 25547c478bd9Sstevel@tonic-gate * heap32_arena to assure that we can address the allocated memory with 25557c478bd9Sstevel@tonic-gate * a single sethi/simm13 pair in the interposing trap table entries. 25567c478bd9Sstevel@tonic-gate */ 25577c478bd9Sstevel@tonic-gate tstat_arena = vmem_create("trapstat", NULL, 0, MMU_PAGESIZE, 25587c478bd9Sstevel@tonic-gate segkmem_alloc_permanent, segkmem_free, heap32_arena, 0, VM_SLEEP); 25597c478bd9Sstevel@tonic-gate 25607c478bd9Sstevel@tonic-gate tstat_enabled = kmem_alloc(TSTAT_TOTAL_NENT * sizeof (int), KM_SLEEP); 25617c478bd9Sstevel@tonic-gate tstat_buffer = kmem_alloc(tstat_data_t_size, KM_SLEEP); 25627c478bd9Sstevel@tonic-gate 25637c478bd9Sstevel@tonic-gate /* 25647c478bd9Sstevel@tonic-gate * CB_CL_CPR_POST_USER is the class that executes from cpr_resume() 25657c478bd9Sstevel@tonic-gate * after user threads can be restarted. By executing in this class, 25667c478bd9Sstevel@tonic-gate * we are assured of the availability of system services needed to 25677c478bd9Sstevel@tonic-gate * resume trapstat (specifically, we are assured that all CPUs are 25687c478bd9Sstevel@tonic-gate * restarted and responding to cross calls). 25697c478bd9Sstevel@tonic-gate */ 25707c478bd9Sstevel@tonic-gate tstat_cprcb = 25717c478bd9Sstevel@tonic-gate callb_add(trapstat_cpr, NULL, CB_CL_CPR_POST_USER, "trapstat"); 25727c478bd9Sstevel@tonic-gate 25737c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 25747c478bd9Sstevel@tonic-gate } 25757c478bd9Sstevel@tonic-gate 25767c478bd9Sstevel@tonic-gate static int 25777c478bd9Sstevel@tonic-gate trapstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 25787c478bd9Sstevel@tonic-gate { 25797c478bd9Sstevel@tonic-gate int rval; 25807c478bd9Sstevel@tonic-gate 25817c478bd9Sstevel@tonic-gate ASSERT(devi == tstat_devi); 25827c478bd9Sstevel@tonic-gate 25837c478bd9Sstevel@tonic-gate switch (cmd) { 25847c478bd9Sstevel@tonic-gate case DDI_DETACH: 25857c478bd9Sstevel@tonic-gate break; 25867c478bd9Sstevel@tonic-gate 25877c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 25887c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 25897c478bd9Sstevel@tonic-gate 25907c478bd9Sstevel@tonic-gate default: 25917c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 25927c478bd9Sstevel@tonic-gate } 25937c478bd9Sstevel@tonic-gate 25947c478bd9Sstevel@tonic-gate ASSERT(!tstat_running); 25957c478bd9Sstevel@tonic-gate 25967c478bd9Sstevel@tonic-gate rval = callb_delete(tstat_cprcb); 25977c478bd9Sstevel@tonic-gate ASSERT(rval == 0); 25987c478bd9Sstevel@tonic-gate 25997c478bd9Sstevel@tonic-gate kmem_free(tstat_buffer, tstat_data_t_size); 26007c478bd9Sstevel@tonic-gate kmem_free(tstat_enabled, TSTAT_TOTAL_NENT * sizeof (int)); 26017c478bd9Sstevel@tonic-gate vmem_destroy(tstat_arena); 26027c478bd9Sstevel@tonic-gate kmem_free(tstat_percpu, (max_cpuid + 1) * sizeof (tstat_percpu_t)); 26037c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 26047c478bd9Sstevel@tonic-gate 26057c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 26067c478bd9Sstevel@tonic-gate } 26077c478bd9Sstevel@tonic-gate 26087c478bd9Sstevel@tonic-gate /* 26097c478bd9Sstevel@tonic-gate * Configuration data structures 26107c478bd9Sstevel@tonic-gate */ 26117c478bd9Sstevel@tonic-gate static struct cb_ops trapstat_cb_ops = { 26127c478bd9Sstevel@tonic-gate trapstat_open, /* open */ 26137c478bd9Sstevel@tonic-gate trapstat_close, /* close */ 26147c478bd9Sstevel@tonic-gate nulldev, /* strategy */ 26157c478bd9Sstevel@tonic-gate nulldev, /* print */ 26167c478bd9Sstevel@tonic-gate nodev, /* dump */ 26177c478bd9Sstevel@tonic-gate nodev, /* read */ 26187c478bd9Sstevel@tonic-gate nodev, /* write */ 26197c478bd9Sstevel@tonic-gate trapstat_ioctl, /* ioctl */ 26207c478bd9Sstevel@tonic-gate nodev, /* devmap */ 26217c478bd9Sstevel@tonic-gate nodev, /* mmap */ 26227c478bd9Sstevel@tonic-gate nodev, /* segmap */ 26237c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 26247c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 26257c478bd9Sstevel@tonic-gate 0, /* streamtab */ 26267c478bd9Sstevel@tonic-gate D_MP | D_NEW /* Driver compatibility flag */ 26277c478bd9Sstevel@tonic-gate }; 26287c478bd9Sstevel@tonic-gate 26297c478bd9Sstevel@tonic-gate static struct dev_ops trapstat_ops = { 26307c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 26317c478bd9Sstevel@tonic-gate 0, /* refcnt */ 26327c478bd9Sstevel@tonic-gate trapstat_info, /* getinfo */ 26337c478bd9Sstevel@tonic-gate nulldev, /* identify */ 26347c478bd9Sstevel@tonic-gate nulldev, /* probe */ 26357c478bd9Sstevel@tonic-gate trapstat_attach, /* attach */ 26367c478bd9Sstevel@tonic-gate trapstat_detach, /* detach */ 26377c478bd9Sstevel@tonic-gate nulldev, /* reset */ 26387c478bd9Sstevel@tonic-gate &trapstat_cb_ops, /* cb_ops */ 26397c478bd9Sstevel@tonic-gate (struct bus_ops *)0, /* bus_ops */ 264019397407SSherry Moore NULL, /* power */ 264119397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 26427c478bd9Sstevel@tonic-gate }; 26437c478bd9Sstevel@tonic-gate 26447c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 26457c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 26464df55fdeSJanie Lu "Trap Statistics 1.1", /* name of module */ 26477c478bd9Sstevel@tonic-gate &trapstat_ops, /* driver ops */ 26487c478bd9Sstevel@tonic-gate }; 26497c478bd9Sstevel@tonic-gate 26507c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 26517c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 26527c478bd9Sstevel@tonic-gate }; 26537c478bd9Sstevel@tonic-gate 26547c478bd9Sstevel@tonic-gate int 26557c478bd9Sstevel@tonic-gate _init(void) 26567c478bd9Sstevel@tonic-gate { 26577c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 26587c478bd9Sstevel@tonic-gate } 26597c478bd9Sstevel@tonic-gate 26607c478bd9Sstevel@tonic-gate int 26617c478bd9Sstevel@tonic-gate _fini(void) 26627c478bd9Sstevel@tonic-gate { 26637c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 26647c478bd9Sstevel@tonic-gate } 26657c478bd9Sstevel@tonic-gate 26667c478bd9Sstevel@tonic-gate int 26677c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 26687c478bd9Sstevel@tonic-gate { 26697c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 26707c478bd9Sstevel@tonic-gate } 2671