1.\" Copyright (c) 2003-2008 Joseph Koshy 2.\" Copyright (c) 2007 The FreeBSD Foundation 3.\" All rights reserved. 4.\" 5.\" Portions of this software were developed by A. Joseph Koshy under 6.\" sponsorship from the FreeBSD Foundation and Google, Inc. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd April 3, 2021 32.Dt HWPMC 4 33.Os 34.Sh NAME 35.Nm hwpmc 36.Nd "Hardware Performance Monitoring Counter support" 37.Sh SYNOPSIS 38.Cd "options HWPMC_HOOKS" 39.Cd "device hwpmc" 40.Pp 41Additionally, for i386 systems: 42.Cd "device apic" 43.Sh DESCRIPTION 44The 45.Nm 46driver virtualizes the hardware performance monitoring facilities in 47modern CPUs and provides support for using these facilities from 48user level processes. 49.Pp 50The driver supports multi-processor systems. 51.Pp 52PMCs are allocated using the 53.Dv PMC_OP_PMCALLOCATE 54request. 55A successful 56.Dv PMC_OP_PMCALLOCATE 57request will return a handle to the requesting process. 58Subsequent operations on the allocated PMC use this handle to denote 59the specific PMC. 60A process that has successfully allocated a PMC is termed an 61.Dq "owner process" . 62.Pp 63PMCs may be allocated with process or system scope. 64.Bl -tag -width ".Em Process-scope" 65.It Em "Process-scope" 66The PMC is active only when a thread belonging 67to a process it is attached to is scheduled on a CPU. 68.It Em "System-scope" 69The PMC operates independently of processes and 70measures hardware events for the system as a whole. 71.El 72.Pp 73PMCs may be allocated for counting or for sampling: 74.Bl -tag -width ".Em Counting" 75.It Em Counting 76In counting modes, the PMCs count hardware events. 77These counts are retrievable using the 78.Dv PMC_OP_PMCREAD 79system call on all architectures. 80Some architectures offer faster methods of reading these counts. 81.It Em Sampling 82In sampling modes, the PMCs are configured to sample the CPU 83instruction pointer (and optionally to capture the call chain leading 84up to the sampled instruction pointer) after a configurable number of 85hardware events have been observed. 86Instruction pointer samples and call chain records are usually 87directed to a log file for subsequent analysis. 88.El 89.Pp 90Scope and operational mode are orthogonal; a PMC may thus be 91configured to operate in one of the following four modes: 92.Bl -tag -width indent 93.It Process-scope, counting 94These PMCs count hardware events whenever a thread in their attached process is 95scheduled on a CPU. 96These PMCs normally count from zero, but the initial count may be 97set using the 98.Dv PMC_OP_SETCOUNT 99operation. 100Applications can read the value of the PMC anytime using the 101.Dv PMC_OP_PMCRW 102operation. 103.It Process-scope, sampling 104These PMCs sample the target processes instruction pointer after they 105have seen the configured number of hardware events. 106The PMCs only count events when a thread belonging to their attached 107process is active. 108The desired frequency of sampling is set using the 109.Dv PMC_OP_SETCOUNT 110operation prior to starting the PMC. 111Log files are configured using the 112.Dv PMC_OP_CONFIGURELOG 113operation. 114.It System-scope, counting 115These PMCs count hardware events seen by them independent of the 116processes that are executing. 117The current count on these PMCs can be read using the 118.Dv PMC_OP_PMCRW 119request. 120These PMCs normally count from zero, but the initial count may be 121set using the 122.Dv PMC_OP_SETCOUNT 123operation. 124.It System-scope, sampling 125These PMCs will periodically sample the instruction pointer of the CPU 126they are allocated on, and will write the sample to a log for further 127processing. 128The desired frequency of sampling is set using the 129.Dv PMC_OP_SETCOUNT 130operation prior to starting the PMC. 131Log files are configured using the 132.Dv PMC_OP_CONFIGURELOG 133operation. 134.Pp 135System-wide statistical sampling can only be enabled by a process with 136super-user privileges. 137.El 138.Pp 139Processes are allowed to allocate as many PMCs as the hardware and 140current operating conditions permit. 141Processes may mix allocations of system-wide and process-private 142PMCs. 143Multiple processes may be using PMCs simultaneously. 144.Pp 145Allocated PMCs are started using the 146.Dv PMC_OP_PMCSTART 147operation, and stopped using the 148.Dv PMC_OP_PMCSTOP 149operation. 150Stopping and starting a PMC is permitted at any time the owner process 151has a valid handle to the PMC. 152.Pp 153Process-private PMCs need to be attached to a target process before 154they can be used. 155Attaching a process to a PMC is done using the 156.Dv PMC_OP_PMCATTACH 157operation. 158An already attached PMC may be detached from its target process 159using the converse 160.Dv PMC_OP_PMCDETACH 161operation. 162Issuing a 163.Dv PMC_OP_PMCSTART 164operation on an as yet unattached PMC will cause it to be attached 165to its owner process. 166The following rules determine whether a given process may attach 167a PMC to another target process: 168.Bl -bullet -compact 169.It 170A non-jailed process with super-user privileges is allowed to attach 171to any other process in the system. 172.It 173Other processes are only allowed to attach to targets that they would 174be able to attach to for debugging (as determined by 175.Xr p_candebug 9 ) . 176.El 177.Pp 178PMCs are released using 179.Dv PMC_OP_PMCRELEASE . 180After a successful 181.Dv PMC_OP_PMCRELEASE 182operation the handle to the PMC will become invalid. 183.Ss Modifier Flags 184The 185.Dv PMC_OP_PMCALLOCATE 186operation supports the following flags that modify the behavior 187of an allocated PMC: 188.Bl -tag -width indent 189.It Dv PMC_F_CALLCHAIN 190This modifier informs sampling PMCs to record a callchain when 191capturing a sample. 192The maximum depth to which call chains are recorded is specified 193by the 194.Va "kern.hwpmc.callchaindepth" 195kernel tunable. 196.It Dv PMC_F_DESCENDANTS 197This modifier is valid only for a PMC being allocated in process-private 198mode. 199It signifies that the PMC will track hardware events for its 200target process and the target's current and future descendants. 201.It Dv PMC_F_LOG_PROCCSW 202This modifier is valid only for a PMC being allocated in process-private 203mode. 204When this modifier is present, at every context switch, 205.Nm 206will log a record containing the number of hardware events 207seen by the target process when it was scheduled on the CPU. 208.It Dv PMC_F_LOG_PROCEXIT 209This modifier is valid only for a PMC being allocated in process-private 210mode. 211With this modifier present, 212.Nm 213will maintain per-process counts for each target process attached to 214a PMC. 215At process exit time, a record containing the target process' PID and 216the accumulated per-process count for that process will be written to the 217configured log file. 218.El 219.Pp 220Modifiers 221.Dv PMC_F_LOG_PROCEXIT 222and 223.Dv PMC_F_LOG_PROCCSW 224may be used in combination with modifier 225.Dv PMC_F_DESCENDANTS 226to track the behavior of complex pipelines of processes. 227PMCs with modifiers 228.Dv PMC_F_LOG_PROCEXIT 229and 230.Dv PMC_F_LOG_PROCCSW 231cannot be started until their owner process has configured a log file. 232.Ss Signals 233The 234.Nm 235driver may deliver signals to processes that have allocated PMCs: 236.Bl -tag -width ".Dv SIGBUS" 237.It Dv SIGIO 238A 239.Dv PMC_OP_PMCRW 240operation was attempted on a process-private PMC that does not have 241attached target processes. 242.It Dv SIGBUS 243The 244.Nm 245driver is being unloaded from the kernel. 246.El 247.Ss PMC ROW DISPOSITIONS 248A PMC row is defined as the set of PMC resources at the same hardware 249address in the CPUs in a system. 250Since process scope PMCs need to move between CPUs following their 251target threads, allocation of a process scope PMC reserves all PMCs in 252a PMC row for use only with process scope PMCs. 253Accordingly a PMC row will be in one of the following dispositions: 254.Bl -tag -width ".Dv PMC_DISP_STANDALONE" -compact 255.It Dv PMC_DISP_FREE 256Hardware counters in this row are free and may be use to satisfy 257either of system scope or process scope allocation requests. 258.It Dv PMC_DISP_THREAD 259Hardware counters in this row are in use by process scope PMCs 260and are only available for process scope allocation requests. 261.It Dv PMC_DISP_STANDALONE 262Some hardware counters in this row have been administratively 263disabled or are in use by system scope PMCs. 264Non-disabled hardware counters in such a row may be used 265for satisfying system scope allocation requests. 266No process scope PMCs will use hardware counters in this row. 267.El 268.Sh PROGRAMMING API 269The recommended way for application programs to use the facilities of 270the 271.Nm 272driver is using the API provided by the 273.Xr pmc 3 274library. 275.Pp 276The 277.Nm 278driver operates using a system call number that is dynamically 279allotted to it when it is loaded into the kernel. 280.Pp 281The 282.Nm 283driver supports the following operations: 284.Bl -tag -width indent 285.It Dv PMC_OP_CONFIGURELOG 286Configure a log file for PMCs that require a log file. 287The 288.Nm 289driver will write log data to this file asynchronously. 290If it encounters an error, logging will be stopped and the error code 291encountered will be saved for subsequent retrieval by a 292.Dv PMC_OP_FLUSHLOG 293request. 294.It Dv PMC_OP_FLUSHLOG 295Transfer buffered log data inside 296.Nm 297to a configured output file. 298This operation returns to the caller after the write operation 299has returned. 300The returned error code reflects any pending error state inside 301.Nm . 302.It Dv PMC_OP_GETCPUINFO 303Retrieve information about the highest possible CPU number for the system, 304and the number of hardware performance monitoring counters available per CPU. 305.It Dv PMC_OP_GETDRIVERSTATS 306Retrieve module statistics (for analyzing the behavior of 307.Nm 308itself). 309.It Dv PMC_OP_GETMODULEVERSION 310Retrieve the version number of API. 311.It Dv PMC_OP_GETPMCINFO 312Retrieve information about the current state of the PMCs on a 313given CPU. 314.It Dv PMC_OP_PMCADMIN 315Set the administrative state (i.e., whether enabled or disabled) for 316the hardware PMCs managed by the 317.Nm 318driver. 319The invoking process needs to possess the 320.Dv PRIV_PMC_MANAGE 321privilege. 322.It Dv PMC_OP_PMCALLOCATE 323Allocate and configure a PMC. 324On successful allocation, a handle to the PMC (a 32 bit value) 325is returned. 326.It Dv PMC_OP_PMCATTACH 327Attach a process mode PMC to a target process. 328The PMC will be active whenever a thread in the target process is 329scheduled on a CPU. 330.Pp 331If the 332.Dv PMC_F_DESCENDANTS 333flag had been specified at PMC allocation time, then the PMC is 334attached to all current and future descendants of the target process. 335.It Dv PMC_OP_PMCDETACH 336Detach a PMC from its target process. 337.It Dv PMC_OP_PMCRELEASE 338Release a PMC. 339.It Dv PMC_OP_PMCRW 340Read and write a PMC. 341This operation is valid only for PMCs configured in counting modes. 342.It Dv PMC_OP_SETCOUNT 343Set the initial count (for counting mode PMCs) or the desired sampling 344rate (for sampling mode PMCs). 345.It Dv PMC_OP_PMCSTART 346Start a PMC. 347.It Dv PMC_OP_PMCSTOP 348Stop a PMC. 349.It Dv PMC_OP_WRITELOG 350Insert a timestamped user record into the log file. 351.El 352.Ss i386 Specific API 353Some i386 family CPUs support the RDPMC instruction which allows a 354user process to read a PMC value without needing to invoke a 355.Dv PMC_OP_PMCRW 356operation. 357On such CPUs, the machine address associated with an allocated PMC is 358retrievable using the 359.Dv PMC_OP_PMCX86GETMSR 360system call. 361.Bl -tag -width indent 362.It Dv PMC_OP_PMCX86GETMSR 363Retrieve the MSR (machine specific register) number associated with 364the given PMC handle. 365.Pp 366The PMC needs to be in process-private mode and allocated without the 367.Dv PMC_F_DESCENDANTS 368modifier flag, and should be attached only to its owner process at the 369time of the call. 370.El 371.Ss amd64 Specific API 372AMD64 CPUs support the RDPMC instruction which allows a 373user process to read a PMC value without needing to invoke a 374.Dv PMC_OP_PMCRW 375operation. 376The machine address associated with an allocated PMC is 377retrievable using the 378.Dv PMC_OP_PMCX86GETMSR 379system call. 380.Bl -tag -width indent 381.It Dv PMC_OP_PMCX86GETMSR 382Retrieve the MSR (machine specific register) number associated with 383the given PMC handle. 384.Pp 385The PMC needs to be in process-private mode and allocated without the 386.Dv PMC_F_DESCENDANTS 387modifier flag, and should be attached only to its owner process at the 388time of the call. 389.El 390.Sh SYSCTL VARIABLES AND LOADER TUNABLES 391The behavior of 392.Nm 393is influenced by the following 394.Xr sysctl 8 395and 396.Xr loader 8 397tunables: 398.Bl -tag -width indent 399.It Va kern.hwpmc.callchaindepth Pq integer, read-only 400The maximum number of call chain records to capture per sample. 401The default is 8. 402.It Va kern.hwpmc.debugflags Pq string, read-write 403(Only available if the 404.Nm 405driver was compiled with 406.Fl DDEBUG . ) 407Control the verbosity of debug messages from the 408.Nm 409driver. 410.It Va kern.hwpmc.hashsize Pq integer, read-only 411The number of rows in the hash tables used to keep track of owner and 412target processes. 413The default is 16. 414.It Va kern.hwpmc.logbuffersize Pq integer, read-only 415The size in kilobytes of each log buffer used by 416.Nm Ns 's 417logging function. 418The default buffer size is 4KB. 419.It Va kern.hwpmc.mincount Pq integer, read-write 420The minimum sampling rate for sampling mode PMCs. 421The default count is 1000 events. 422.It Va kern.hwpmc.mtxpoolsize Pq integer, read-only 423The size of the spin mutex pool used by the PMC driver. 424The default is 32. 425.It Va kern.hwpmc.nbuffers_pcpu Pq integer, read-only 426The number of log buffers used by 427.Nm 428for logging. 429The default is 64. 430.It Va kern.hwpmc.nsamples Pq integer, read-only 431The number of entries in the per-CPU ring buffer used during sampling. 432The default is 512. 433.It Va security.bsd.unprivileged_syspmcs Pq boolean, read-write 434If set to non-zero, allow unprivileged processes to allocate system-wide 435PMCs. 436The default value is 0. 437.It Va security.bsd.unprivileged_proc_debug Pq boolean, read-write 438If set to 0, the 439.Nm 440driver will only allow privileged processes to attach PMCs to other 441processes. 442.El 443.Pp 444These variables may be set in the kernel environment using 445.Xr kenv 1 446before 447.Nm 448is loaded. 449.Sh IMPLEMENTATION NOTES 450.Ss SMP Symmetry 451The kernel driver requires all physical CPUs in an SMP system to have 452identical performance monitoring counter hardware. 453.Ss Sparse CPU Numbering 454On platforms that sparsely number CPUs and which support hot-plugging 455of CPUs, requests that specify non-existent or disabled CPUs will fail 456with an error. 457Applications allocating system-scope PMCs need to be aware of 458the possibility of such transient failures. 459.Ss x86 TSC Handling 460Historically, on the x86 architecture, 461.Fx 462has permitted user processes running at a processor CPL of 3 to 463read the TSC using the RDTSC instruction. 464The 465.Nm 466driver preserves this behavior. 467.Ss Intel P4/HTT Handling 468On CPUs with HTT support, Intel P4 PMCs are capable of qualifying 469only a subset of hardware events on a per-logical CPU basis. 470Consequently, if HTT is enabled on a system with Intel Pentium P4 471PMCs, then the 472.Nm 473driver will reject allocation requests for process-private PMCs that 474request counting of hardware events that cannot be counted separately 475for each logical CPU. 476.Ss Intel Pentium-Pro Handling 477Writing a value to the PMC MSRs found in Intel Pentium-Pro style PMCs 478(found in 479.Tn "Intel Pentium Pro" , 480.Tn "Pentium II" , 481.Tn "Pentium III" , 482.Tn "Pentium M" 483and 484.Tn "Celeron" 485processors) will replicate bit 31 of the 486value being written into the upper 8 bits of the MSR, 487bringing down the usable width of these PMCs to 31 bits. 488For process-virtual PMCs, the 489.Nm 490driver implements a workaround in software and makes the corrected 64 491bit count available via the 492.Dv PMC_OP_RW 493operation. 494Processes that intend to use RDPMC instructions directly or 495that intend to write values larger than 2^31 into these PMCs with 496.Dv PMC_OP_RW 497need to be aware of this hardware limitation. 498.Sh DIAGNOSTICS 499.Bl -diag 500.It "hwpmc: [class/npmc/capabilities]..." 501Announce the presence of 502.Va npmc 503PMCs of class 504.Va class , 505with capabilities described by bit string 506.Va capabilities . 507.It "hwpmc: kernel version (0x%x) does not match module version (0x%x)." 508The module loading process failed because a version mismatch was detected 509between the currently executing kernel and the module being loaded. 510.It "hwpmc: this kernel has not been compiled with 'options HWPMC_HOOKS'." 511The module loading process failed because the currently executing kernel 512was not configured with the required configuration option 513.Dv HWPMC_HOOKS . 514.It "hwpmc: tunable hashsize=%d must be greater than zero." 515A negative value was supplied for tunable 516.Va kern.hwpmc.hashsize . 517.It "hwpmc: tunable logbuffersize=%d must be greater than zero." 518A negative value was supplied for tunable 519.Va kern.hwpmc.logbuffersize . 520.It "hwpmc: tunable nlogbuffers=%d must be greater than zero." 521A negative value was supplied for tunable 522.Va kern.hwpmc.nlogbuffers . 523.It "hwpmc: tunable nsamples=%d out of range." 524The value for tunable 525.Va kern.hwpmc.nsamples 526was negative or greater than 65535. 527.El 528.Sh COMPATIBILITY 529The 530.Nm 531driver is 532.Ud 533The API and ABI documented in this manual page may change in 534the future. 535The recommended method of accessing this driver is using the 536.Xr pmc 3 537API. 538.Sh ERRORS 539A command issued to the 540.Nm 541driver may fail with the following errors: 542.Bl -tag -width Er 543.It Bq Er EAGAIN 544Helper process creation failed for a 545.Dv PMC_OP_CONFIGURELOG 546request due to a temporary resource shortage in the kernel. 547.It Bq Er EBUSY 548A 549.Dv PMC_OP_CONFIGURELOG 550operation was requested while an existing log was active. 551.It Bq Er EBUSY 552A DISABLE operation was requested using the 553.Dv PMC_OP_PMCADMIN 554request for a set of hardware resources currently in use for 555process-private PMCs. 556.It Bq Er EBUSY 557A 558.Dv PMC_OP_PMCADMIN 559operation was requested on an active system mode PMC. 560.It Bq Er EBUSY 561A 562.Dv PMC_OP_PMCATTACH 563operation was requested for a target process that already had another 564PMC using the same hardware resources attached to it. 565.It Bq Er EBUSY 566A 567.Dv PMC_OP_PMCRW 568request writing a new value was issued on a PMC that was active. 569.It Bq Er EBUSY 570A 571.Dv PMC_OP_PMCSETCOUNT 572request was issued on a PMC that was active. 573.It Bq Er EDOOFUS 574A 575.Dv PMC_OP_PMCSTART 576operation was requested without a log file being configured for a 577PMC allocated with 578.Dv PMC_F_LOG_PROCCSW 579and 580.Dv PMC_F_LOG_PROCEXIT 581modifiers. 582.It Bq Er EDOOFUS 583A 584.Dv PMC_OP_PMCSTART 585operation was requested on a system-wide sampling PMC without a log 586file being configured. 587.It Bq Er EEXIST 588A 589.Dv PMC_OP_PMCATTACH 590request was reissued for a target process that already is the target 591of this PMC. 592.It Bq Er EFAULT 593A bad address was passed in to the driver. 594.It Bq Er EINVAL 595An invalid PMC handle was specified. 596.It Bq Er EINVAL 597An invalid CPU number was passed in for a 598.Dv PMC_OP_GETPMCINFO 599operation. 600.It Bq Er EINVAL 601A 602.Dv PMC_OP_CONFIGURELOG 603request to de-configure a log file was issued without a log file 604being configured. 605.It Bq Er EINVAL 606A 607.Dv PMC_OP_FLUSHLOG 608request was issued without a log file being configured. 609.It Bq Er EINVAL 610An invalid CPU number was passed in for a 611.Dv PMC_OP_PMCADMIN 612operation. 613.It Bq Er EINVAL 614An invalid operation request was passed in for a 615.Dv PMC_OP_PMCADMIN 616operation. 617.It Bq Er EINVAL 618An invalid PMC ID was passed in for a 619.Dv PMC_OP_PMCADMIN 620operation. 621.It Bq Er EINVAL 622A suitable PMC matching the parameters passed in to a 623.Dv PMC_OP_PMCALLOCATE 624request could not be allocated. 625.It Bq Er EINVAL 626An invalid PMC mode was requested during a 627.Dv PMC_OP_PMCALLOCATE 628request. 629.It Bq Er EINVAL 630An invalid CPU number was specified during a 631.Dv PMC_OP_PMCALLOCATE 632request. 633.It Bq Er EINVAL 634A CPU other than 635.Dv PMC_CPU_ANY 636was specified in a 637.Dv PMC_OP_PMCALLOCATE 638request for a process-private PMC. 639.It Bq Er EINVAL 640A CPU number of 641.Dv PMC_CPU_ANY 642was specified in a 643.Dv PMC_OP_PMCALLOCATE 644request for a system-wide PMC. 645.It Bq Er EINVAL 646The 647.Ar pm_flags 648argument to an 649.Dv PMC_OP_PMCALLOCATE 650request contained unknown flags. 651.It Bq Er EINVAL 652(On Intel Pentium 4 CPUs with HTT support) 653A 654.Dv PMC_OP_PMCALLOCATE 655request for a process-private PMC was issued for an event that does 656not support counting on a per-logical CPU basis. 657.It Bq Er EINVAL 658A PMC allocated for system-wide operation was specified with a 659.Dv PMC_OP_PMCATTACH 660or 661.Dv PMC_OP_PMCDETACH 662request. 663.It Bq Er EINVAL 664The 665.Ar pm_pid 666argument to a 667.Dv PMC_OP_PMCATTACH 668or 669.Dv PMC_OP_PMCDETACH 670request specified an illegal process ID. 671.It Bq Er EINVAL 672A 673.Dv PMC_OP_PMCDETACH 674request was issued for a PMC not attached to the target process. 675.It Bq Er EINVAL 676Argument 677.Ar pm_flags 678to a 679.Dv PMC_OP_PMCRW 680request contained illegal flags. 681.It Bq Er EINVAL 682A 683.Dv PMC_OP_PMCX86GETMSR 684operation was requested for a PMC not in process-virtual mode, or 685for a PMC that is not solely attached to its owner process, or for 686a PMC that was allocated with flag 687.Dv PMC_F_DESCENDANTS . 688.It Bq Er EINVAL 689A 690.Dv PMC_OP_WRITELOG 691request was issued for an owner process without a log file 692configured. 693.It Bq Er ENOMEM 694The system was not able to allocate kernel memory. 695.It Bq Er ENOSYS 696(On i386 and amd64 architectures) 697A 698.Dv PMC_OP_PMCX86GETMSR 699operation was requested for hardware that does not support reading 700PMCs directly with the RDPMC instruction. 701.It Bq Er ENXIO 702A 703.Dv PMC_OP_GETPMCINFO 704operation was requested for an absent or disabled CPU. 705.It Bq Er ENXIO 706A 707.Dv PMC_OP_PMCALLOCATE 708operation specified allocation of a system-wide PMC on an absent or 709disabled CPU. 710.It Bq Er ENXIO 711A 712.Dv PMC_OP_PMCSTART 713or 714.Dv PMC_OP_PMCSTOP 715request was issued for a system-wide PMC that was allocated on a CPU 716that is currently absent or disabled. 717.It Bq Er EOPNOTSUPP 718A 719.Dv PMC_OP_PMCALLOCATE 720request was issued for PMC capabilities not supported 721by the specified PMC class. 722.It Bq Er EOPNOTSUPP 723(i386 architectures) 724A sampling mode PMC was requested on a CPU lacking an APIC. 725.It Bq Er EPERM 726A 727.Dv PMC_OP_PMCADMIN 728request was issued by a process without super-user 729privilege or by a jailed super-user process. 730.It Bq Er EPERM 731A 732.Dv PMC_OP_PMCATTACH 733operation was issued for a target process that the current process 734does not have permission to attach to. 735.It Bq Er EPERM 736(i386 and amd64 architectures) 737A 738.Dv PMC_OP_PMCATTACH 739operation was issued on a PMC whose MSR has been retrieved using 740.Dv PMC_OP_PMCX86GETMSR . 741.It Bq Er ESRCH 742A process issued a PMC operation request without having allocated any 743PMCs. 744.It Bq Er ESRCH 745A process issued a PMC operation request after the PMC was detached 746from all of its target processes. 747.It Bq Er ESRCH 748A 749.Dv PMC_OP_PMCATTACH 750or 751.Dv PMC_OP_PMCDETACH 752request specified a non-existent process ID. 753.It Bq Er ESRCH 754The target process for a 755.Dv PMC_OP_PMCDETACH 756operation is not being monitored by 757.Nm . 758.El 759.Sh SEE ALSO 760.Xr kenv 1 , 761.Xr pmc 3 , 762.Xr pmclog 3 , 763.Xr kldload 8 , 764.Xr pmccontrol 8 , 765.Xr pmcstat 8 , 766.Xr sysctl 8 , 767.Xr kproc_create 9 , 768.Xr p_candebug 9 769.Sh HISTORY 770The 771.Nm 772driver first appeared in 773.Fx 6.0 . 774.Sh AUTHORS 775The 776.Nm 777driver was written by 778.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . 779.Sh BUGS 780The driver samples the state of the kernel's logical processor support 781at the time of initialization (i.e., at module load time). 782On CPUs supporting logical processors, the driver could misbehave if 783logical processors are subsequently enabled or disabled while the 784driver is active. 785.Pp 786On the i386 architecture, the driver requires that the local APIC on the 787CPU be enabled for sampling mode to be supported. 788Many single-processor motherboards keep the APIC disabled in BIOS; on 789such systems 790.Nm 791will not support sampling PMCs. 792.Sh SECURITY CONSIDERATIONS 793PMCs may be used to monitor the actual behavior of the system on hardware. 794In situations where this constitutes an undesirable information leak, 795the following options are available: 796.Bl -enum 797.It 798Set the 799.Xr sysctl 8 800tunable 801.Va security.bsd.unprivileged_syspmcs 802to 0. 803This ensures that unprivileged processes cannot allocate system-wide 804PMCs and thus cannot observe the hardware behavior of the system 805as a whole. 806This tunable may also be set at boot time using 807.Xr loader 8 , 808or with 809.Xr kenv 1 810prior to loading the 811.Nm 812driver into the kernel. 813.It 814Set the 815.Xr sysctl 8 816tunable 817.Va security.bsd.unprivileged_proc_debug 818to 0. 819This will ensure that an unprivileged process cannot attach a PMC 820to any process other than itself and thus cannot observe the hardware 821behavior of other processes with the same credentials. 822.El 823.Pp 824System administrators should note that on IA-32 platforms 825.Fx 826makes the content of the IA-32 TSC counter available to all processes 827via the RDTSC instruction. 828