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