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