xref: /freebsd/sys/dev/mem/memdev.c (revision 148a8da8)
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/kernel.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/pmap.h>
50 
51 #include <machine/memdev.h>
52 
53 static struct cdev *memdev, *kmemdev;
54 
55 static struct cdevsw mem_cdevsw = {
56 	.d_version =	D_VERSION,
57 	.d_flags =	D_MEM,
58 	.d_open =	memopen,
59 	.d_read =	memrw,
60 	.d_write =	memrw,
61 	.d_ioctl =	memioctl,
62 	.d_mmap =	memmmap,
63 	.d_name =	"mem",
64 };
65 
66 /* ARGSUSED */
67 int
68 memopen(struct cdev *dev __unused, int flags, int fmt __unused,
69     struct thread *td)
70 {
71 	int error = 0;
72 
73 	if (flags & FREAD)
74 		error = priv_check(td, PRIV_KMEM_READ);
75 	if (flags & FWRITE) {
76 		if (error == 0)
77 			error = priv_check(td, PRIV_KMEM_WRITE);
78 		if (error == 0)
79 			error = securelevel_gt(td->td_ucred, 0);
80 	}
81 
82 	return (error);
83 }
84 
85 /* ARGSUSED */
86 static int
87 mem_modevent(module_t mod __unused, int type, void *data __unused)
88 {
89 	switch(type) {
90 	case MOD_LOAD:
91 		if (bootverbose)
92 			printf("mem: <memory>\n");
93 		mem_range_init();
94 		memdev = make_dev(&mem_cdevsw, CDEV_MINOR_MEM,
95 			UID_ROOT, GID_KMEM, 0640, "mem");
96 		kmemdev = make_dev(&mem_cdevsw, CDEV_MINOR_KMEM,
97 			UID_ROOT, GID_KMEM, 0640, "kmem");
98 		break;
99 
100 	case MOD_UNLOAD:
101 		mem_range_destroy();
102 		destroy_dev(memdev);
103 		destroy_dev(kmemdev);
104 		break;
105 
106 	case MOD_SHUTDOWN:
107 		break;
108 
109 	default:
110 		return(EOPNOTSUPP);
111 
112 	}
113 
114 	return (0);
115 }
116 
117 DEV_MODULE(mem, mem_modevent, NULL);
118 MODULE_VERSION(mem, 1);
119