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