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