xref: /freebsd/sys/dev/mem/memdev.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
18ab2f5ecSMark Murray /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
48ab2f5ecSMark Murray  * Copyright (c) 2004 Mark R V Murray
58ab2f5ecSMark Murray  * All rights reserved.
68ab2f5ecSMark Murray  *
78ab2f5ecSMark Murray  * Redistribution and use in source and binary forms, with or without
88ab2f5ecSMark Murray  * modification, are permitted provided that the following conditions
98ab2f5ecSMark Murray  * are met:
108ab2f5ecSMark Murray  * 1. Redistributions of source code must retain the above copyright
118ab2f5ecSMark Murray  *    notice, this list of conditions and the following disclaimer
128ab2f5ecSMark Murray  *    in this position and unchanged.
138ab2f5ecSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
148ab2f5ecSMark Murray  *    notice, this list of conditions and the following disclaimer in the
158ab2f5ecSMark Murray  *    documentation and/or other materials provided with the distribution.
168ab2f5ecSMark Murray  *
178ab2f5ecSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
188ab2f5ecSMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198ab2f5ecSMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208ab2f5ecSMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218ab2f5ecSMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228ab2f5ecSMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238ab2f5ecSMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248ab2f5ecSMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258ab2f5ecSMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268ab2f5ecSMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278ab2f5ecSMark Murray  *
288ab2f5ecSMark Murray  */
298ab2f5ecSMark Murray 
308ab2f5ecSMark Murray #include <sys/param.h>
318ab2f5ecSMark Murray #include <sys/conf.h>
328ab2f5ecSMark Murray #include <sys/fcntl.h>
332d838cd8SMark Johnston #include <sys/ioccom.h>
348ab2f5ecSMark Murray #include <sys/kernel.h>
35c9114f9fSMitchell Horne #include <sys/kerneldump.h>
368ab2f5ecSMark Murray #include <sys/lock.h>
378ab2f5ecSMark Murray #include <sys/malloc.h>
382fea6431SJung-uk Kim #include <sys/memrange.h>
398ab2f5ecSMark Murray #include <sys/module.h>
408ab2f5ecSMark Murray #include <sys/mutex.h>
41c71e3362SJamie Gritton #include <sys/priv.h>
428ab2f5ecSMark Murray #include <sys/proc.h>
438ab2f5ecSMark Murray #include <sys/signalvar.h>
448ab2f5ecSMark Murray #include <sys/systm.h>
458ab2f5ecSMark Murray #include <sys/uio.h>
468ab2f5ecSMark Murray 
478ab2f5ecSMark Murray #include <vm/vm.h>
482d838cd8SMark Johnston #include <vm/vm_param.h>
498ab2f5ecSMark Murray #include <vm/pmap.h>
502d838cd8SMark Johnston #include <vm/vm_map.h>
512d838cd8SMark Johnston #include <vm/vm_object.h>
522d838cd8SMark Johnston #include <vm/vm_page.h>
532d838cd8SMark Johnston #include <vm/vm_phys.h>
548ab2f5ecSMark Murray 
558ab2f5ecSMark Murray #include <machine/memdev.h>
568ab2f5ecSMark Murray 
578ab2f5ecSMark Murray static struct cdev *memdev, *kmemdev;
588ab2f5ecSMark Murray 
592d838cd8SMark Johnston static d_ioctl_t memioctl;
602d838cd8SMark Johnston 
618ab2f5ecSMark Murray static struct cdevsw mem_cdevsw = {
628ab2f5ecSMark Murray 	.d_version =	D_VERSION,
638f4548ffSKonstantin Belousov 	.d_flags =	D_MEM,
648ab2f5ecSMark Murray 	.d_open =	memopen,
658ab2f5ecSMark Murray 	.d_read =	memrw,
668ab2f5ecSMark Murray 	.d_write =	memrw,
678ab2f5ecSMark Murray 	.d_ioctl =	memioctl,
688ab2f5ecSMark Murray 	.d_mmap =	memmmap,
698ab2f5ecSMark Murray 	.d_name =	"mem",
708ab2f5ecSMark Murray };
718ab2f5ecSMark Murray 
728ab2f5ecSMark Murray /* ARGSUSED */
738ab2f5ecSMark Murray int
memopen(struct cdev * dev __unused,int flags,int fmt __unused,struct thread * td)748ab2f5ecSMark Murray memopen(struct cdev *dev __unused, int flags, int fmt __unused,
758ab2f5ecSMark Murray     struct thread *td)
768ab2f5ecSMark Murray {
778ab2f5ecSMark Murray 	int error = 0;
788ab2f5ecSMark Murray 
79c71e3362SJamie Gritton 	if (flags & FREAD)
80c71e3362SJamie Gritton 		error = priv_check(td, PRIV_KMEM_READ);
81c71e3362SJamie Gritton 	if (flags & FWRITE) {
82c71e3362SJamie Gritton 		if (error == 0)
83c71e3362SJamie Gritton 			error = priv_check(td, PRIV_KMEM_WRITE);
84c71e3362SJamie Gritton 		if (error == 0)
858ab2f5ecSMark Murray 			error = securelevel_gt(td->td_ucred, 0);
86c71e3362SJamie Gritton 	}
878ab2f5ecSMark Murray 
888ab2f5ecSMark Murray 	return (error);
898ab2f5ecSMark Murray }
908ab2f5ecSMark Murray 
912d838cd8SMark Johnston static int
memioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)922d838cd8SMark Johnston memioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
932d838cd8SMark Johnston     struct thread *td)
942d838cd8SMark Johnston {
952d838cd8SMark Johnston 	vm_map_t map;
962d838cd8SMark Johnston 	vm_map_entry_t entry;
97c9114f9fSMitchell Horne 	const struct mem_livedump_arg *marg;
982d838cd8SMark Johnston 	struct mem_extract *me;
992d838cd8SMark Johnston 	int error;
1002d838cd8SMark Johnston 
1012d838cd8SMark Johnston 	error = 0;
1022d838cd8SMark Johnston 	switch (cmd) {
1032d838cd8SMark Johnston 	case MEM_EXTRACT_PADDR:
1042d838cd8SMark Johnston 		me = (struct mem_extract *)data;
1052d838cd8SMark Johnston 
1062d838cd8SMark Johnston 		map = &td->td_proc->p_vmspace->vm_map;
1072d838cd8SMark Johnston 		vm_map_lock_read(map);
1082d838cd8SMark Johnston 		if (vm_map_lookup_entry(map, me->me_vaddr, &entry)) {
1092d838cd8SMark Johnston 			me->me_paddr = pmap_extract(
1102d838cd8SMark Johnston 			    &td->td_proc->p_vmspace->vm_pmap, me->me_vaddr);
1112d838cd8SMark Johnston 			if (me->me_paddr != 0) {
1122d838cd8SMark Johnston 				me->me_state = ME_STATE_MAPPED;
113431fb8abSMark Johnston 				me->me_domain = vm_phys_domain(me->me_paddr);
1142d838cd8SMark Johnston 			} else {
1152d838cd8SMark Johnston 				me->me_state = ME_STATE_VALID;
1162d838cd8SMark Johnston 			}
1172d838cd8SMark Johnston 		} else {
1182d838cd8SMark Johnston 			me->me_state = ME_STATE_INVALID;
1192d838cd8SMark Johnston 		}
1202d838cd8SMark Johnston 		vm_map_unlock_read(map);
1212d838cd8SMark Johnston 		break;
122c9114f9fSMitchell Horne 	case MEM_KERNELDUMP:
123c9114f9fSMitchell Horne 		marg = (const struct mem_livedump_arg *)data;
124c9114f9fSMitchell Horne 		error = livedump_start(marg->fd, marg->flags, marg->compression);
125c9114f9fSMitchell Horne 		break;
1262d838cd8SMark Johnston 	default:
1272d838cd8SMark Johnston 		error = memioctl_md(dev, cmd, data, flags, td);
1282d838cd8SMark Johnston 		break;
1292d838cd8SMark Johnston 	}
1302d838cd8SMark Johnston 	return (error);
1312d838cd8SMark Johnston }
1322d838cd8SMark Johnston 
1338ab2f5ecSMark Murray /* ARGSUSED */
1348ab2f5ecSMark Murray static int
mem_modevent(module_t mod __unused,int type,void * data __unused)1358ab2f5ecSMark Murray mem_modevent(module_t mod __unused, int type, void *data __unused)
1368ab2f5ecSMark Murray {
1378ab2f5ecSMark Murray 	switch(type) {
1388ab2f5ecSMark Murray 	case MOD_LOAD:
1398ab2f5ecSMark Murray 		if (bootverbose)
1408ab2f5ecSMark Murray 			printf("mem: <memory>\n");
1412fea6431SJung-uk Kim 		mem_range_init();
1428ab2f5ecSMark Murray 		memdev = make_dev(&mem_cdevsw, CDEV_MINOR_MEM,
1438ab2f5ecSMark Murray 			UID_ROOT, GID_KMEM, 0640, "mem");
1448ab2f5ecSMark Murray 		kmemdev = make_dev(&mem_cdevsw, CDEV_MINOR_KMEM,
1458ab2f5ecSMark Murray 			UID_ROOT, GID_KMEM, 0640, "kmem");
1468ab2f5ecSMark Murray 		break;
1478ab2f5ecSMark Murray 
1488ab2f5ecSMark Murray 	case MOD_UNLOAD:
1492fea6431SJung-uk Kim 		mem_range_destroy();
1508ab2f5ecSMark Murray 		destroy_dev(memdev);
1518ab2f5ecSMark Murray 		destroy_dev(kmemdev);
1528ab2f5ecSMark Murray 		break;
1538ab2f5ecSMark Murray 
1548ab2f5ecSMark Murray 	case MOD_SHUTDOWN:
1558ab2f5ecSMark Murray 		break;
1568ab2f5ecSMark Murray 
1578ab2f5ecSMark Murray 	default:
1588ab2f5ecSMark Murray 		return(EOPNOTSUPP);
1598ab2f5ecSMark Murray 	}
1608ab2f5ecSMark Murray 
1618ab2f5ecSMark Murray 	return (0);
1628ab2f5ecSMark Murray }
1638ab2f5ecSMark Murray 
1648ab2f5ecSMark Murray DEV_MODULE(mem, mem_modevent, NULL);
165ce46e205SMark Murray MODULE_VERSION(mem, 1);
166