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