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 24, 2024 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.Ss Live Kernel Dumps 204.Pp 205The 206.Dv MEM_KERNELDUMP 207ioctl will initiate a kernel dump against the running system, the contents of 208which will be written to a process-owned file descriptor. 209The resulting dump output will be in minidump format. 210The request is described by 211.Bd -literal 212struct mem_livedump_arg { 213 int fd; /* input */ 214 int flags /* input */ 215 uint8_t compression /* input */ 216}; 217.Ed 218.Pp 219The 220.Va fd 221field is used to pass the file descriptor. 222.Pp 223The 224.Va flags 225field is currently unused and must be set to zero. 226.Pp 227The 228.Va compression 229field can be used to specify the desired compression to 230be applied to the dump output. 231The supported values are defined in 232.In sys/kerneldump.h ; 233that is, 234.Dv KERNELDUMP_COMP_NONE , 235.Dv KERNELDUMP_COMP_GZIP , 236or 237.Dv KERNELDUMP_COMP_ZSTD . 238.Pp 239Kernel dumps taken against the running system may have inconsistent kernel data 240structures due to allocation, deallocation, or modification of memory 241concurrent to the dump procedure. 242Thus, the resulting core dump is not guaranteed to be usable. 243A system under load is more likely to produce an inconsistent result. 244Despite this, live kernel dumps can be useful for offline debugging of certain 245types of kernel bugs, such as deadlocks, or in inspecting a particular part of 246the system's state. 247.Sh RETURN VALUES 248.Ss MEM_EXTRACT_PADDR 249The 250.Dv MEM_EXTRACT_PADDR 251ioctl always returns a value of zero. 252.Ss MEMRANGE_GET/MEMRANGE_SET 253.Bl -tag -width Er 254.It Bq Er EOPNOTSUPP 255Memory range operations are not supported on this architecture. 256.It Bq Er ENXIO 257No memory range descriptors are available (e.g., firmware has not enabled 258any). 259.It Bq Er EINVAL 260The memory range supplied as an argument is invalid or overlaps another 261range in a fashion not supported by this architecture. 262.It Bq Er EBUSY 263An attempt to remove or update a range failed because the range is busy. 264.It Bq Er ENOSPC 265An attempt to create a new range failed due to a shortage of hardware 266resources (e.g., descriptor slots). 267.It Bq Er ENOENT 268An attempt to remove a range failed because no range matches the descriptor 269base/length supplied. 270.It Bq Er EPERM 271An attempt to remove a range failed because the range is permanently 272enabled. 273.El 274.Ss MEM_KERNELDUMP 275.Bl -tag -width Er 276.It Bq Er EOPNOTSUPP 277Kernel minidumps are not supported on this architecture. 278.It Bq Er EPERM 279An attempt to begin the kernel dump failed because the calling thread lacks the 280.It Bq Er EBADF 281The supplied file descriptor was invalid, or does not have write permission. 282.It Bq Er EBUSY 283An attempt to begin the kernel dump failed because one is already in progress. 284.It Bq Er EINVAL 285An invalid or unsupported value was specified in 286.Va flags . 287.It Bq Er EINVAL 288An invalid or unsupported compression type was specified. 289.Dv PRIV_KMEM_READ 290privilege. 291.El 292.Sh FILES 293.Bl -tag -width /dev/kmem -compact 294.It Pa /dev/mem 295.It Pa /dev/kmem 296.El 297.Sh SEE ALSO 298.Xr kvm 3 , 299.Xr memcontrol 8 300.Sh HISTORY 301The 302.Pa /dev/mem 303file appeared in 304.At v1 305and 306.Pa /dev/kmem 307in 308.At v5 . 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