xref: /freebsd/sys/dev/mem/memdev.c (revision 1631382cf2820245cc72965498ff174bb548dd63)
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/param.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <sys/ioccom.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/memrange.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/signalvar.h>
44 #include <sys/systm.h>
45 #include <sys/uio.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_phys.h>
54 
55 #include <machine/memdev.h>
56 
57 static struct cdev *memdev, *kmemdev;
58 
59 static d_ioctl_t memioctl;
60 
61 static struct cdevsw mem_cdevsw = {
62 	.d_version =	D_VERSION,
63 	.d_flags =	D_MEM,
64 	.d_open =	memopen,
65 	.d_read =	memrw,
66 	.d_write =	memrw,
67 	.d_ioctl =	memioctl,
68 	.d_mmap =	memmmap,
69 	.d_name =	"mem",
70 };
71 
72 /* ARGSUSED */
73 int
74 memopen(struct cdev *dev __unused, int flags, int fmt __unused,
75     struct thread *td)
76 {
77 	int error = 0;
78 
79 	if (flags & FREAD)
80 		error = priv_check(td, PRIV_KMEM_READ);
81 	if (flags & FWRITE) {
82 		if (error == 0)
83 			error = priv_check(td, PRIV_KMEM_WRITE);
84 		if (error == 0)
85 			error = securelevel_gt(td->td_ucred, 0);
86 	}
87 
88 	return (error);
89 }
90 
91 static int
92 memioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
93     struct thread *td)
94 {
95 	vm_map_t map;
96 	vm_map_entry_t entry;
97 	const struct mem_livedump_arg *marg;
98 	struct mem_extract *me;
99 	int error;
100 
101 	error = 0;
102 	switch (cmd) {
103 	case MEM_EXTRACT_PADDR:
104 		me = (struct mem_extract *)data;
105 
106 		map = &td->td_proc->p_vmspace->vm_map;
107 		vm_map_lock_read(map);
108 		if (vm_map_lookup_entry(map, me->me_vaddr, &entry)) {
109 			me->me_paddr = pmap_extract(
110 			    &td->td_proc->p_vmspace->vm_pmap, me->me_vaddr);
111 			if (me->me_paddr != 0) {
112 				me->me_state = ME_STATE_MAPPED;
113 				me->me_domain = vm_phys_domain(me->me_paddr);
114 			} else {
115 				me->me_state = ME_STATE_VALID;
116 			}
117 		} else {
118 			me->me_state = ME_STATE_INVALID;
119 		}
120 		vm_map_unlock_read(map);
121 		break;
122 	case MEM_KERNELDUMP:
123 		marg = (const struct mem_livedump_arg *)data;
124 		error = livedump_start(marg->fd, marg->flags, marg->compression);
125 		break;
126 	default:
127 		error = memioctl_md(dev, cmd, data, flags, td);
128 		break;
129 	}
130 	return (error);
131 }
132 
133 /* ARGSUSED */
134 static int
135 mem_modevent(module_t mod __unused, int type, void *data __unused)
136 {
137 	switch(type) {
138 	case MOD_LOAD:
139 		if (bootverbose)
140 			printf("mem: <memory>\n");
141 		mem_range_init();
142 		memdev = make_dev(&mem_cdevsw, CDEV_MINOR_MEM,
143 			UID_ROOT, GID_KMEM, 0640, "mem");
144 		kmemdev = make_dev(&mem_cdevsw, CDEV_MINOR_KMEM,
145 			UID_ROOT, GID_KMEM, 0640, "kmem");
146 		break;
147 
148 	case MOD_UNLOAD:
149 		mem_range_destroy();
150 		destroy_dev(memdev);
151 		destroy_dev(kmemdev);
152 		break;
153 
154 	case MOD_SHUTDOWN:
155 		break;
156 
157 	default:
158 		return(EOPNOTSUPP);
159 	}
160 
161 	return (0);
162 }
163 
164 DEV_MODULE(mem, mem_modevent, NULL);
165 MODULE_VERSION(mem, 1);
166