xref: /freebsd/share/man/man4/mem.4 (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	@(#)mem.4	5.3 (Berkeley) 5/2/91
29.\" $FreeBSD$
30.\"
31.Dd March 11, 2022
32.Dt MEM 4
33.Os
34.Sh NAME
35.Nm mem ,
36.Nm kmem
37.Nd memory files
38.Sh SYNOPSIS
39.Cd "device mem"
40.Sh DESCRIPTION
41The special file
42.Pa /dev/mem
43is an interface to the physical memory of the computer.
44Byte offsets in this file are interpreted as physical memory addresses.
45Reading and writing this file is equivalent to reading and writing
46memory itself.
47Only offsets within the bounds of
48.Pa /dev/mem
49are allowed.
50.Pp
51Kernel virtual memory is accessed through the interface
52.Pa /dev/kmem
53in the same manner as
54.Pa /dev/mem .
55Only kernel virtual addresses that are currently mapped to memory are allowed.
56.Pp
57On ISA the I/O memory space begins at physical address 0x000a0000
58and runs to 0x00100000.
59The
60per-process data
61size
62for the current process
63is
64.Dv UPAGES
65long, and ends at virtual
66address 0xf0000000.
67.Sh IOCTL INTERFACE
68.Ss Address Properties
69The
70.Dv MEM_EXTRACT_PADDR
71ioctl can be used to look up the physical address and NUMA domain of a given
72virtual address in the calling process' address space.
73The request is described by
74.Bd -literal
75struct mem_extract {
76	uint64_t	me_vaddr;	/* input */
77	uint64_t	me_paddr;	/* output */
78	int		me_domain;	/* output */
79	int		me_state;	/* output */
80};
81.Ed
82.Pp
83The ioctl returns an error if the address is not valid.
84The information returned by
85.Dv MEM_EXTRACT_PADDR
86may be out of date by the time that the ioctl call returns.
87Specifically, concurrent system calls, page faults, or system page reclamation
88activity may have unmapped the virtual page or replaced the backing physical
89page before the ioctl call returns.
90Wired pages, e.g., those locked by
91.Xr mlock 2 ,
92will not be reclaimed by the system.
93.Pp
94The
95.Fa me_state
96field provides information about the state of the virtual page:
97.Bl -tag -width indent
98.It Dv ME_STATE_INVALID
99The virtual address is invalid.
100.It Dv ME_STATE_VALID
101The virtual address is valid but is not mapped at the time of the ioctl call.
102.It Dv ME_STATE_MAPPED
103The virtual address corresponds to a physical page mapping, and the
104.Fa me_paddr
105and
106.Fa me_domain
107fields are valid.
108.El
109.Ss Memory Ranges
110.Pp
111Several architectures allow attributes to be associated with ranges of physical
112memory.
113These attributes can be manipulated via
114.Fn ioctl
115calls performed on
116.Pa /dev/mem .
117Declarations and data types are to be found in
118.In sys/memrange.h .
119.Pp
120The specific attributes, and number of programmable ranges may vary between
121architectures.
122The full set of supported attributes is:
123.Bl -tag -width indent
124.It Dv MDF_UNCACHEABLE
125The region is not cached.
126.It Dv MDF_WRITECOMBINE
127Writes to the region may be combined or performed out of order.
128.It Dv MDF_WRITETHROUGH
129Writes to the region are committed synchronously.
130.It Dv MDF_WRITEBACK
131Writes to the region are committed asynchronously.
132.It Dv MDF_WRITEPROTECT
133The region cannot be written to.
134.El
135.Pp
136Memory ranges are described by
137.Bd -literal
138struct mem_range_desc {
139	uint64_t	mr_base;	/* physical base address */
140	uint64_t	mr_len;		/* physical length of region */
141	int		mr_flags;	/* attributes of region */
142	char		mr_owner[8];
143};
144.Ed
145.Pp
146In addition to the region attributes listed above, the following flags
147may also be set in the
148.Fa mr_flags
149field:
150.Bl -tag -width indent
151.It MDF_FIXBASE
152The region's base address cannot be changed.
153.It MDF_FIXLEN
154The region's length cannot be changed.
155.It MDF_FIRMWARE
156The region is believed to have been established by the system firmware.
157.It MDF_ACTIVE
158The region is currently active.
159.It MDF_BOGUS
160We believe the region to be invalid or otherwise erroneous.
161.It MDF_FIXACTIVE
162The region cannot be disabled.
163.It MDF_BUSY
164The region is currently owned by another process and may not be
165altered.
166.El
167.Pp
168Operations are performed using
169.Bd -literal
170struct mem_range_op {
171	struct mem_range_desc	*mo_desc;
172	int			mo_arg[2];
173};
174.Ed
175.Pp
176The
177.Dv MEMRANGE_GET
178ioctl is used to retrieve current memory range attributes.
179If
180.Va mo_arg[0]
181is set to 0, it will be updated with the total number of memory range
182descriptors.
183If greater than 0, the array at
184.Va mo_desc
185will be filled with a corresponding number of descriptor structures,
186or the maximum, whichever is less.
187.Pp
188The
189.Dv MEMRANGE_SET
190ioctl is used to add, alter and remove memory range attributes.
191A range
192with the
193.Dv MDF_FIXACTIVE
194flag may not be removed; a range with the
195.Dv MDF_BUSY
196flag may not be removed or updated.
197.Pp
198.Va mo_arg[0]
199should be set to
200.Dv MEMRANGE_SET_UPDATE
201to update an existing or establish a new range, or to
202.Dv MEMRANGE_SET_REMOVE
203to remove a range.
204.El
205.Ss Live Kernel Dumps
206.Pp
207The
208.Dv MEM_KERNELDUMP
209ioctl will initiate a kernel dump against the running system, the contents of
210which will be written to a process-owned file descriptor.
211The resulting dump output will be in minidump format.
212The request is described by
213.Bd -literal
214struct mem_livedump_arg {
215	int	fd;		/* input */
216	int	flags		/* input */
217	uint8_t	compression	/* input */
218};
219.Ed
220.Pp
221The
222.Va fd
223field is used to pass the file descriptor.
224.Pp
225The
226.Va flags
227field is currently unused and must be set to zero.
228.Pp
229The
230.Va compression
231field can be used to specify the desired compression to
232be applied to the dump output.
233The supported values are defined in
234.In sys/kerneldump.h ;
235that is,
236.Dv KERNELDUMP_COMP_NONE ,
237.Dv KERNELDUMP_COMP_GZIP ,
238or
239.Dv KERNELDUMP_COMP_ZSTD .
240.Pp
241Kernel dumps taken against the running system may have inconsistent kernel data
242structures due to allocation, deallocation, or modification of memory
243concurrent to the dump procedure.
244Thus, the resulting core dump is not guaranteed to be usable.
245A system under load is more likely to produce an inconsistent result.
246Despite this, live kernel dumps can be useful for offline debugging of certain
247types of kernel bugs, such as deadlocks, or in inspecting a particular part of
248the system's state.
249.Sh RETURN VALUES
250.Ss MEM_EXTRACT_PADDR
251The
252.Dv MEM_EXTRACT_PADDR
253ioctl always returns a value of zero.
254.Ss MEMRANGE_GET/MEMRANGE_SET
255.Bl -tag -width Er
256.It Bq Er EOPNOTSUPP
257Memory range operations are not supported on this architecture.
258.It Bq Er ENXIO
259No memory range descriptors are available (e.g., firmware has not enabled
260any).
261.It Bq Er EINVAL
262The memory range supplied as an argument is invalid or overlaps another
263range in a fashion not supported by this architecture.
264.It Bq Er EBUSY
265An attempt to remove or update a range failed because the range is busy.
266.It Bq Er ENOSPC
267An attempt to create a new range failed due to a shortage of hardware
268resources (e.g., descriptor slots).
269.It Bq Er ENOENT
270An attempt to remove a range failed because no range matches the descriptor
271base/length supplied.
272.It Bq Er EPERM
273An attempt to remove a range failed because the range is permanently
274enabled.
275.El
276.Ss MEM_KERNELDUMP
277.Bl -tag -width Er
278.It Bq Er EOPNOTSUPP
279Kernel minidumps are not supported on this architecture.
280.It Bq Er EPERM
281An attempt to begin the kernel dump failed because the calling thread lacks the
282.It Bq Er EBADF
283The supplied file descriptor was invalid, or does not have write permission.
284.It Bq Er EBUSY
285An attempt to begin the kernel dump failed because one is already in progress.
286.It Bq Er EINVAL
287An invalid or unsupported value was specified in
288.Va flags .
289.It Bq Er EINVAL
290An invalid or unsupported compression type was specified.
291.Dv PRIV_KMEM_READ
292privilege.
293.El
294.Sh FILES
295.Bl -tag -width /dev/kmem -compact
296.It Pa /dev/mem
297.It Pa /dev/kmem
298.El
299.Sh SEE ALSO
300.Xr kvm 3 ,
301.Xr memcontrol 8
302.Sh HISTORY
303The
304.Nm mem
305and
306.Nm kmem
307files appeared in
308.At v6 .
309The ioctl interface for memory range attributes was added in
310.Fx 3.2 .
311.Sh BUGS
312Busy range attributes are not yet managed correctly.
313.Pp
314This device is required for all users of
315.Xr kvm 3
316to operate.
317