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