xref: /freebsd/sys/dev/mem/memdev.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/memrange.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/signalvar.h>
46 #include <sys/systm.h>
47 #include <sys/uio.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_phys.h>
56 
57 #include <machine/memdev.h>
58 
59 static struct cdev *memdev, *kmemdev;
60 
61 static d_ioctl_t memioctl;
62 
63 static struct cdevsw mem_cdevsw = {
64 	.d_version =	D_VERSION,
65 	.d_flags =	D_MEM,
66 	.d_open =	memopen,
67 	.d_read =	memrw,
68 	.d_write =	memrw,
69 	.d_ioctl =	memioctl,
70 	.d_mmap =	memmmap,
71 	.d_name =	"mem",
72 };
73 
74 /* ARGSUSED */
75 int
76 memopen(struct cdev *dev __unused, int flags, int fmt __unused,
77     struct thread *td)
78 {
79 	int error = 0;
80 
81 	if (flags & FREAD)
82 		error = priv_check(td, PRIV_KMEM_READ);
83 	if (flags & FWRITE) {
84 		if (error == 0)
85 			error = priv_check(td, PRIV_KMEM_WRITE);
86 		if (error == 0)
87 			error = securelevel_gt(td->td_ucred, 0);
88 	}
89 
90 	return (error);
91 }
92 
93 static int
94 memioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
95     struct thread *td)
96 {
97 	vm_map_t map;
98 	vm_map_entry_t entry;
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 	default:
124 		error = memioctl_md(dev, cmd, data, flags, td);
125 		break;
126 	}
127 	return (error);
128 }
129 
130 /* ARGSUSED */
131 static int
132 mem_modevent(module_t mod __unused, int type, void *data __unused)
133 {
134 	switch(type) {
135 	case MOD_LOAD:
136 		if (bootverbose)
137 			printf("mem: <memory>\n");
138 		mem_range_init();
139 		memdev = make_dev(&mem_cdevsw, CDEV_MINOR_MEM,
140 			UID_ROOT, GID_KMEM, 0640, "mem");
141 		kmemdev = make_dev(&mem_cdevsw, CDEV_MINOR_KMEM,
142 			UID_ROOT, GID_KMEM, 0640, "kmem");
143 		break;
144 
145 	case MOD_UNLOAD:
146 		mem_range_destroy();
147 		destroy_dev(memdev);
148 		destroy_dev(kmemdev);
149 		break;
150 
151 	case MOD_SHUTDOWN:
152 		break;
153 
154 	default:
155 		return(EOPNOTSUPP);
156 	}
157 
158 	return (0);
159 }
160 
161 DEV_MODULE(mem, mem_modevent, NULL);
162 MODULE_VERSION(mem, 1);
163