xref: /freebsd/sys/dev/mem/memdev.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Mark R V Murray
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/fcntl.h>
34 #include <sys/ioccom.h>
35 #include <sys/kernel.h>
36 #include <sys/kerneldump.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/memrange.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/signalvar.h>
45 #include <sys/systm.h>
46 #include <sys/uio.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_phys.h>
55 
56 #include <machine/memdev.h>
57 
58 static struct cdev *memdev, *kmemdev;
59 
60 static d_ioctl_t memioctl;
61 
62 static struct cdevsw mem_cdevsw = {
63 	.d_version =	D_VERSION,
64 	.d_flags =	D_MEM,
65 	.d_open =	memopen,
66 	.d_read =	memrw,
67 	.d_write =	memrw,
68 	.d_ioctl =	memioctl,
69 	.d_mmap =	memmmap,
70 	.d_name =	"mem",
71 };
72 
73 /* ARGSUSED */
74 int
75 memopen(struct cdev *dev __unused, int flags, int fmt __unused,
76     struct thread *td)
77 {
78 	int error = 0;
79 
80 	if (flags & FREAD)
81 		error = priv_check(td, PRIV_KMEM_READ);
82 	if (flags & FWRITE) {
83 		if (error == 0)
84 			error = priv_check(td, PRIV_KMEM_WRITE);
85 		if (error == 0)
86 			error = securelevel_gt(td->td_ucred, 0);
87 	}
88 
89 	return (error);
90 }
91 
92 static int
93 memioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
94     struct thread *td)
95 {
96 	vm_map_t map;
97 	vm_map_entry_t entry;
98 	const struct mem_livedump_arg *marg;
99 	struct mem_extract *me;
100 	int error;
101 
102 	error = 0;
103 	switch (cmd) {
104 	case MEM_EXTRACT_PADDR:
105 		me = (struct mem_extract *)data;
106 
107 		map = &td->td_proc->p_vmspace->vm_map;
108 		vm_map_lock_read(map);
109 		if (vm_map_lookup_entry(map, me->me_vaddr, &entry)) {
110 			me->me_paddr = pmap_extract(
111 			    &td->td_proc->p_vmspace->vm_pmap, me->me_vaddr);
112 			if (me->me_paddr != 0) {
113 				me->me_state = ME_STATE_MAPPED;
114 				me->me_domain = vm_phys_domain(me->me_paddr);
115 			} else {
116 				me->me_state = ME_STATE_VALID;
117 			}
118 		} else {
119 			me->me_state = ME_STATE_INVALID;
120 		}
121 		vm_map_unlock_read(map);
122 		break;
123 	case MEM_KERNELDUMP:
124 		marg = (const struct mem_livedump_arg *)data;
125 		error = livedump_start(marg->fd, marg->flags, marg->compression);
126 		break;
127 	default:
128 		error = memioctl_md(dev, cmd, data, flags, td);
129 		break;
130 	}
131 	return (error);
132 }
133 
134 /* ARGSUSED */
135 static int
136 mem_modevent(module_t mod __unused, int type, void *data __unused)
137 {
138 	switch(type) {
139 	case MOD_LOAD:
140 		if (bootverbose)
141 			printf("mem: <memory>\n");
142 		mem_range_init();
143 		memdev = make_dev(&mem_cdevsw, CDEV_MINOR_MEM,
144 			UID_ROOT, GID_KMEM, 0640, "mem");
145 		kmemdev = make_dev(&mem_cdevsw, CDEV_MINOR_KMEM,
146 			UID_ROOT, GID_KMEM, 0640, "kmem");
147 		break;
148 
149 	case MOD_UNLOAD:
150 		mem_range_destroy();
151 		destroy_dev(memdev);
152 		destroy_dev(kmemdev);
153 		break;
154 
155 	case MOD_SHUTDOWN:
156 		break;
157 
158 	default:
159 		return(EOPNOTSUPP);
160 	}
161 
162 	return (0);
163 }
164 
165 DEV_MODULE(mem, mem_modevent, NULL);
166 MODULE_VERSION(mem, 1);
167