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