xref: /freebsd/share/man/man4/hwpmc.4 (revision 19261079b74319502c6ffa1249920079f0f69a72)
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.mtxpoolsize Pq integer, read-only
420The size of the spin mutex pool used by the PMC driver.
421The default is 32.
422.It Va kern.hwpmc.nbuffers_pcpu Pq integer, read-only
423The number of log buffers used by
424.Nm
425for logging.
426The default is 64.
427.It Va kern.hwpmc.nsamples Pq integer, read-only
428The number of entries in the per-CPU ring buffer used during sampling.
429The default is 512.
430.It Va security.bsd.unprivileged_syspmcs Pq boolean, read-write
431If set to non-zero, allow unprivileged processes to allocate system-wide
432PMCs.
433The default value is 0.
434.It Va security.bsd.unprivileged_proc_debug Pq boolean, read-write
435If set to 0, the
436.Nm
437driver will only allow privileged processes to attach PMCs to other
438processes.
439.El
440.Pp
441These variables may be set in the kernel environment using
442.Xr kenv 1
443before
444.Nm
445is loaded.
446.Sh IMPLEMENTATION NOTES
447.Ss SMP Symmetry
448The kernel driver requires all physical CPUs in an SMP system to have
449identical performance monitoring counter hardware.
450.Ss Sparse CPU Numbering
451On platforms that sparsely number CPUs and which support hot-plugging
452of CPUs, requests that specify non-existent or disabled CPUs will fail
453with an error.
454Applications allocating system-scope PMCs need to be aware of
455the possibility of such transient failures.
456.Ss x86 TSC Handling
457Historically, on the x86 architecture,
458.Fx
459has permitted user processes running at a processor CPL of 3 to
460read the TSC using the RDTSC instruction.
461The
462.Nm
463driver preserves this behavior.
464.Ss Intel P4/HTT Handling
465On CPUs with HTT support, Intel P4 PMCs are capable of qualifying
466only a subset of hardware events on a per-logical CPU basis.
467Consequently, if HTT is enabled on a system with Intel Pentium P4
468PMCs, then the
469.Nm
470driver will reject allocation requests for process-private PMCs that
471request counting of hardware events that cannot be counted separately
472for each logical CPU.
473.Ss Intel Pentium-Pro Handling
474Writing a value to the PMC MSRs found in Intel Pentium-Pro style PMCs
475(found in
476.Tn "Intel Pentium Pro" ,
477.Tn "Pentium II" ,
478.Tn "Pentium III" ,
479.Tn "Pentium M"
480and
481.Tn "Celeron"
482processors) will replicate bit 31 of the
483value being written into the upper 8 bits of the MSR,
484bringing down the usable width of these PMCs to 31 bits.
485For process-virtual PMCs, the
486.Nm
487driver implements a workaround in software and makes the corrected 64
488bit count available via the
489.Dv PMC_OP_RW
490operation.
491Processes that intend to use RDPMC instructions directly or
492that intend to write values larger than 2^31 into these PMCs with
493.Dv PMC_OP_RW
494need to be aware of this hardware limitation.
495.Sh DIAGNOSTICS
496.Bl -diag
497.It "hwpmc: [class/npmc/capabilities]..."
498Announce the presence of
499.Va npmc
500PMCs of class
501.Va class ,
502with capabilities described by bit string
503.Va capabilities .
504.It "hwpmc: kernel version (0x%x) does not match module version (0x%x)."
505The module loading process failed because a version mismatch was detected
506between the currently executing kernel and the module being loaded.
507.It "hwpmc: this kernel has not been compiled with 'options HWPMC_HOOKS'."
508The module loading process failed because the currently executing kernel
509was not configured with the required configuration option
510.Dv HWPMC_HOOKS .
511.It "hwpmc: tunable hashsize=%d must be greater than zero."
512A negative value was supplied for tunable
513.Va kern.hwpmc.hashsize .
514.It "hwpmc: tunable logbuffersize=%d must be greater than zero."
515A negative value was supplied for tunable
516.Va kern.hwpmc.logbuffersize .
517.It "hwpmc: tunable nlogbuffers=%d must be greater than zero."
518A negative value was supplied for tunable
519.Va kern.hwpmc.nlogbuffers .
520.It "hwpmc: tunable nsamples=%d out of range."
521The value for tunable
522.Va kern.hwpmc.nsamples
523was negative or greater than 65535.
524.El
525.Sh COMPATIBILITY
526The
527.Nm
528driver is
529.Ud
530The API and ABI documented in this manual page may change in
531the future.
532The recommended method of accessing this driver is using the
533.Xr pmc 3
534API.
535.Sh ERRORS
536A command issued to the
537.Nm
538driver may fail with the following errors:
539.Bl -tag -width Er
540.It Bq Er EAGAIN
541Helper process creation failed for a
542.Dv PMC_OP_CONFIGURELOG
543request due to a temporary resource shortage in the kernel.
544.It Bq Er EBUSY
545A
546.Dv PMC_OP_CONFIGURELOG
547operation was requested while an existing log was active.
548.It Bq Er EBUSY
549A DISABLE operation was requested using the
550.Dv PMC_OP_PMCADMIN
551request for a set of hardware resources currently in use for
552process-private PMCs.
553.It Bq Er EBUSY
554A
555.Dv PMC_OP_PMCADMIN
556operation was requested on an active system mode PMC.
557.It Bq Er EBUSY
558A
559.Dv PMC_OP_PMCATTACH
560operation was requested for a target process that already had another
561PMC using the same hardware resources attached to it.
562.It Bq Er EBUSY
563A
564.Dv PMC_OP_PMCRW
565request writing a new value was issued on a PMC that was active.
566.It Bq Er EBUSY
567A
568.Dv PMC_OP_PMCSETCOUNT
569request was issued on a PMC that was active.
570.It Bq Er EDOOFUS
571A
572.Dv PMC_OP_PMCSTART
573operation was requested without a log file being configured for a
574PMC allocated with
575.Dv PMC_F_LOG_PROCCSW
576and
577.Dv PMC_F_LOG_PROCEXIT
578modifiers.
579.It Bq Er EDOOFUS
580A
581.Dv PMC_OP_PMCSTART
582operation was requested on a system-wide sampling PMC without a log
583file being configured.
584.It Bq Er EEXIST
585A
586.Dv PMC_OP_PMCATTACH
587request was reissued for a target process that already is the target
588of this PMC.
589.It Bq Er EFAULT
590A bad address was passed in to the driver.
591.It Bq Er EINVAL
592An invalid PMC handle was specified.
593.It Bq Er EINVAL
594An invalid CPU number was passed in for a
595.Dv PMC_OP_GETPMCINFO
596operation.
597.It Bq Er EINVAL
598A
599.Dv PMC_OP_CONFIGURELOG
600request to de-configure a log file was issued without a log file
601being configured.
602.It Bq Er EINVAL
603A
604.Dv PMC_OP_FLUSHLOG
605request was issued without a log file being configured.
606.It Bq Er EINVAL
607An invalid CPU number was passed in for a
608.Dv PMC_OP_PMCADMIN
609operation.
610.It Bq Er EINVAL
611An invalid operation request was passed in for a
612.Dv PMC_OP_PMCADMIN
613operation.
614.It Bq Er EINVAL
615An invalid PMC ID was passed in for a
616.Dv PMC_OP_PMCADMIN
617operation.
618.It Bq Er EINVAL
619A suitable PMC matching the parameters passed in to a
620.Dv PMC_OP_PMCALLOCATE
621request could not be allocated.
622.It Bq Er EINVAL
623An invalid PMC mode was requested during a
624.Dv PMC_OP_PMCALLOCATE
625request.
626.It Bq Er EINVAL
627An invalid CPU number was specified during a
628.Dv PMC_OP_PMCALLOCATE
629request.
630.It Bq Er EINVAL
631A CPU other than
632.Dv PMC_CPU_ANY
633was specified in a
634.Dv PMC_OP_PMCALLOCATE
635request for a process-private PMC.
636.It Bq Er EINVAL
637A CPU number of
638.Dv PMC_CPU_ANY
639was specified in a
640.Dv PMC_OP_PMCALLOCATE
641request for a system-wide PMC.
642.It Bq Er EINVAL
643The
644.Ar pm_flags
645argument to an
646.Dv PMC_OP_PMCALLOCATE
647request contained unknown flags.
648.It Bq Er EINVAL
649(On Intel Pentium 4 CPUs with HTT support)
650A
651.Dv PMC_OP_PMCALLOCATE
652request for a process-private PMC was issued for an event that does
653not support counting on a per-logical CPU basis.
654.It Bq Er EINVAL
655A PMC allocated for system-wide operation was specified with a
656.Dv PMC_OP_PMCATTACH
657or
658.Dv PMC_OP_PMCDETACH
659request.
660.It Bq Er EINVAL
661The
662.Ar pm_pid
663argument to a
664.Dv PMC_OP_PMCATTACH
665or
666.Dv PMC_OP_PMCDETACH
667request specified an illegal process ID.
668.It Bq Er EINVAL
669A
670.Dv PMC_OP_PMCDETACH
671request was issued for a PMC not attached to the target process.
672.It Bq Er EINVAL
673Argument
674.Ar pm_flags
675to a
676.Dv PMC_OP_PMCRW
677request contained illegal flags.
678.It Bq Er EINVAL
679A
680.Dv PMC_OP_PMCX86GETMSR
681operation was requested for a PMC not in process-virtual mode, or
682for a PMC that is not solely attached to its owner process, or for
683a PMC that was allocated with flag
684.Dv PMC_F_DESCENDANTS .
685.It Bq Er EINVAL
686A
687.Dv PMC_OP_WRITELOG
688request was issued for an owner process without a log file
689configured.
690.It Bq Er ENOMEM
691The system was not able to allocate kernel memory.
692.It Bq Er ENOSYS
693(On i386 and amd64 architectures)
694A
695.Dv PMC_OP_PMCX86GETMSR
696operation was requested for hardware that does not support reading
697PMCs directly with the RDPMC instruction.
698.It Bq Er ENXIO
699A
700.Dv PMC_OP_GETPMCINFO
701operation was requested for an absent or disabled CPU.
702.It Bq Er ENXIO
703A
704.Dv PMC_OP_PMCALLOCATE
705operation specified allocation of a system-wide PMC on an absent or
706disabled CPU.
707.It Bq Er ENXIO
708A
709.Dv PMC_OP_PMCSTART
710or
711.Dv PMC_OP_PMCSTOP
712request was issued for a system-wide PMC that was allocated on a CPU
713that is currently absent or disabled.
714.It Bq Er EOPNOTSUPP
715A
716.Dv PMC_OP_PMCALLOCATE
717request was issued for PMC capabilities not supported
718by the specified PMC class.
719.It Bq Er EOPNOTSUPP
720(i386 architectures)
721A sampling mode PMC was requested on a CPU lacking an APIC.
722.It Bq Er EPERM
723A
724.Dv PMC_OP_PMCADMIN
725request was issued by a process without super-user
726privilege or by a jailed super-user process.
727.It Bq Er EPERM
728A
729.Dv PMC_OP_PMCATTACH
730operation was issued for a target process that the current process
731does not have permission to attach to.
732.It Bq Er EPERM
733(i386 and amd64 architectures)
734A
735.Dv PMC_OP_PMCATTACH
736operation was issued on a PMC whose MSR has been retrieved using
737.Dv PMC_OP_PMCX86GETMSR .
738.It Bq Er ESRCH
739A process issued a PMC operation request without having allocated any
740PMCs.
741.It Bq Er ESRCH
742A process issued a PMC operation request after the PMC was detached
743from all of its target processes.
744.It Bq Er ESRCH
745A
746.Dv PMC_OP_PMCATTACH
747or
748.Dv PMC_OP_PMCDETACH
749request specified a non-existent process ID.
750.It Bq Er ESRCH
751The target process for a
752.Dv PMC_OP_PMCDETACH
753operation is not being monitored by
754.Nm .
755.El
756.Sh SEE ALSO
757.Xr kenv 1 ,
758.Xr pmc 3 ,
759.Xr pmclog 3 ,
760.Xr kldload 8 ,
761.Xr pmccontrol 8 ,
762.Xr pmcstat 8 ,
763.Xr sysctl 8 ,
764.Xr kproc_create 9 ,
765.Xr p_candebug 9
766.Sh HISTORY
767The
768.Nm
769driver first appeared in
770.Fx 6.0 .
771.Sh AUTHORS
772The
773.Nm
774driver was written by
775.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .
776.Sh BUGS
777The driver samples the state of the kernel's logical processor support
778at the time of initialization (i.e., at module load time).
779On CPUs supporting logical processors, the driver could misbehave if
780logical processors are subsequently enabled or disabled while the
781driver is active.
782.Pp
783On the i386 architecture, the driver requires that the local APIC on the
784CPU be enabled for sampling mode to be supported.
785Many single-processor motherboards keep the APIC disabled in BIOS; on
786such systems
787.Nm
788will not support sampling PMCs.
789.Sh SECURITY CONSIDERATIONS
790PMCs may be used to monitor the actual behavior of the system on hardware.
791In situations where this constitutes an undesirable information leak,
792the following options are available:
793.Bl -enum
794.It
795Set the
796.Xr sysctl 8
797tunable
798.Va security.bsd.unprivileged_syspmcs
799to 0.
800This ensures that unprivileged processes cannot allocate system-wide
801PMCs and thus cannot observe the hardware behavior of the system
802as a whole.
803This tunable may also be set at boot time using
804.Xr loader 8 ,
805or with
806.Xr kenv 1
807prior to loading the
808.Nm
809driver into the kernel.
810.It
811Set the
812.Xr sysctl 8
813tunable
814.Va security.bsd.unprivileged_proc_debug
815to 0.
816This will ensure that an unprivileged process cannot attach a PMC
817to any process other than itself and thus cannot observe the hardware
818behavior of other processes with the same credentials.
819.El
820.Pp
821System administrators should note that on IA-32 platforms
822.Fx
823makes the content of the IA-32 TSC counter available to all processes
824via the RDTSC instruction.
825