kernel - pmap - more invltlb stuff
[dragonfly.git] / sys / kern / imgact_resident.c
bloba1c27d106e611455d5a159e1ee62448fe2fa25fb
1 /*
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
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:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/kern/imgact_resident.c,v 1.17 2007/04/30 07:18:53 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/sysproto.h>
41 #include <sys/exec.h>
42 #include <sys/imgact.h>
43 #include <sys/imgact_aout.h>
44 #include <sys/mman.h>
45 #include <sys/proc.h>
46 #include <sys/priv.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sysent.h>
49 #include <sys/systm.h>
50 #include <sys/stat.h>
51 #include <sys/vnode.h>
52 #include <sys/inflate.h>
53 #include <sys/sysctl.h>
54 #include <sys/lock.h>
55 #include <sys/resident.h>
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_extern.h>
64 #include <sys/sysref2.h>
65 #include <sys/mplock2.h>
67 static int exec_res_id = 0;
69 static TAILQ_HEAD(,vmresident) exec_res_list;
71 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
73 /* lockmgr lock for protecting the exec_res_list */
74 static struct lock exec_list_lock;
76 static void
77 vm_resident_init(void *__dummy)
79 lockinit(&exec_list_lock, "vmres", 0, 0);
80 TAILQ_INIT(&exec_res_list);
82 SYSINIT(vmres, SI_BOOT1_LOCK, SI_ORDER_ANY, vm_resident_init, 0);
84 static int
85 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
87 struct stat st;
88 struct vnode *vrtmp;
89 int error = 0;
91 vrtmp = vr->vr_vnode;
93 in->res_entry_addr = vr->vr_entry_addr;
94 in->res_id = vr->vr_id;
95 if (vrtmp) {
96 char *freepath, *fullpath;
97 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath);
98 if (error != 0) {
99 /* could not retrieve cached path, return zero'ed string */
100 bzero(in->res_file, MAXPATHLEN);
101 error = 0;
102 } else {
103 strlcpy(in->res_file, fullpath, sizeof(in->res_file));
104 kfree(freepath, M_TEMP);
107 /* indicate that we are using the vnode */
108 error = vget(vrtmp, LK_EXCLUSIVE);
109 if (error)
110 goto done;
112 /* retrieve underlying stat information and release vnode */
113 error = vn_stat(vrtmp, &st, td->td_ucred);
114 vput(vrtmp);
115 if (error)
116 goto done;
118 in->res_stat = st;
121 done:
122 if (error)
123 kprintf("fill_xresident, error = %d\n", error);
124 return (error);
127 static int
128 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
130 struct vmresident *vmres;
131 struct thread *td;
132 int error;
133 int count;
135 /* only super-user should call this sysctl */
136 td = req->td;
137 if ((priv_check(td, PRIV_VM_RESIDENT)) != 0)
138 return EPERM;
140 error = count = 0;
142 if (exec_res_id == 0)
143 return error;
145 /* client queried for number of resident binaries */
146 if (!req->oldptr)
147 return SYSCTL_OUT(req, 0, exec_res_id);
149 lockmgr(&exec_list_lock, LK_SHARED);
150 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
151 struct xresident xres;
152 error = fill_xresident(vmres, &xres, td);
153 if (error != 0)
154 break;
156 error = SYSCTL_OUT(req, (void *)&xres,
157 sizeof(struct xresident));
158 if (error != 0)
159 break;
161 lockmgr(&exec_list_lock, LK_RELEASE);
163 return (error);
165 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
166 sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
169 exec_resident_imgact(struct image_params *imgp)
171 struct vmresident *vmres;
174 * resident image activator
176 if ((vmres = imgp->vp->v_resident) == NULL)
177 return(-1);
178 exec_new_vmspace(imgp, vmres->vr_vmspace);
179 imgp->resident = 1;
180 imgp->interpreted = 0;
181 imgp->proc->p_sysent = vmres->vr_sysent;
182 imgp->entry_addr = vmres->vr_entry_addr;
183 return(0);
187 * exec_sys_register(entry)
189 * Register ourselves for resident execution. Only root (i.e. a process with
190 * PRIV_VM_RESIDENT credentials) can do this. This
191 * will snapshot the vmspace and cause future exec's of the specified binary
192 * to use the snapshot directly rather then load & relocate a new copy.
194 * MPALMOSTSAFE
197 sys_exec_sys_register(struct exec_sys_register_args *uap)
199 struct thread *td = curthread;
200 struct vmresident *vmres;
201 struct vnode *vp;
202 struct proc *p;
203 int error;
205 p = td->td_proc;
206 error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
207 if (error)
208 return(error);
210 get_mplock();
212 if ((vp = p->p_textvp) == NULL) {
213 rel_mplock();
214 return(ENOENT);
216 if (vp->v_resident) {
217 rel_mplock();
218 return(EEXIST);
220 vhold(vp);
221 vmres = kmalloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK);
222 vp->v_resident = vmres;
223 vmres->vr_vnode = vp;
224 vmres->vr_sysent = p->p_sysent;
225 vmres->vr_id = ++exec_res_id;
226 vmres->vr_entry_addr = (intptr_t)uap->entry;
227 vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */
228 pmap_pinit2(vmspace_pmap(vmres->vr_vmspace));
230 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
231 TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link);
232 lockmgr(&exec_list_lock, LK_RELEASE);
234 rel_mplock();
235 return(0);
239 * exec_sys_unregister(id)
241 * Unregister the specified id. If an id of -1 is used unregister
242 * the registration associated with the current process. An id of -2
243 * unregisters everything.
245 * MPALMOSTSAFE
248 sys_exec_sys_unregister(struct exec_sys_unregister_args *uap)
250 struct thread *td = curthread;
251 struct vmresident *vmres;
252 struct proc *p;
253 int error;
254 int id;
255 int count;
257 p = td->td_proc;
258 error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
259 if (error)
260 return(error);
263 * If id is -1, unregister ourselves
265 get_mplock();
266 if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
267 id = p->p_textvp->v_resident->vr_id;
270 * Look for the registration
272 error = ENOENT;
273 count = 0;
275 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
276 restart:
277 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
278 if (id == -2 || vmres->vr_id == id) {
279 TAILQ_REMOVE(&exec_res_list, vmres, vr_link);
280 if (vmres->vr_vnode) {
281 vmres->vr_vnode->v_resident = NULL;
282 vdrop(vmres->vr_vnode);
283 vmres->vr_vnode = NULL;
285 if (vmres->vr_vmspace) {
286 sysref_put(&vmres->vr_vmspace->vm_sysref);
287 vmres->vr_vmspace = NULL;
289 kfree(vmres, M_EXEC_RES);
290 exec_res_id--;
291 error = 0;
292 ++count;
293 goto restart;
296 lockmgr(&exec_list_lock, LK_RELEASE);
297 rel_mplock();
298 if (error == 0)
299 uap->sysmsg_result = count;
300 return(error);