Deleted Added
full compact
kern_procctl.c (143820) kern_procctl.c (147692)
1/*-
2 * Copyright (c) 1994, Sean Eric Fagan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 16 unchanged lines hidden (view full) ---

25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1994, Sean Eric Fagan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 16 unchanged lines hidden (view full) ---

25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/sys_process.c 143820 2005-03-18 21:22:28Z das $");
33__FBSDID("$FreeBSD: head/sys/kern/sys_process.c 147692 2005-06-30 07:49:22Z peter $");
34
34
35#include "opt_compat.h"
36
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/syscallsubr.h>
40#include <sys/sysproto.h>
41#include <sys/proc.h>
42#include <sys/vnode.h>

--- 7 unchanged lines hidden (view full) ---

50#include <vm/vm.h>
51#include <vm/pmap.h>
52#include <vm/vm_extern.h>
53#include <vm/vm_map.h>
54#include <vm/vm_kern.h>
55#include <vm/vm_object.h>
56#include <vm/vm_page.h>
57
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/syscallsubr.h>
42#include <sys/sysproto.h>
43#include <sys/proc.h>
44#include <sys/vnode.h>

--- 7 unchanged lines hidden (view full) ---

52#include <vm/vm.h>
53#include <vm/pmap.h>
54#include <vm/vm_extern.h>
55#include <vm/vm_map.h>
56#include <vm/vm_kern.h>
57#include <vm/vm_object.h>
58#include <vm/vm_page.h>
59
60#ifdef COMPAT_IA32
61#include <sys/procfs.h>
62#include <machine/fpu.h>
63#include <compat/ia32/ia32_reg.h>
64
65extern struct sysentvec ia32_freebsd_sysvec;
66
67struct ptrace_io_desc32 {
68 int piod_op;
69 u_int32_t piod_offs;
70 u_int32_t piod_addr;
71 u_int32_t piod_len;
72};
73#endif
74
58/*
59 * Functions implemented using PROC_ACTION():
60 *
61 * proc_read_regs(proc, regs)
62 * Get the current user-visible register set from the process
63 * and copy it into the regs structure (<machine/reg.h>).
64 * The process is stopped at the time read_regs is called.
65 *

--- 67 unchanged lines hidden (view full) ---

133
134int
135proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
136{
137
138 PROC_ACTION(set_fpregs(td, fpregs));
139}
140
75/*
76 * Functions implemented using PROC_ACTION():
77 *
78 * proc_read_regs(proc, regs)
79 * Get the current user-visible register set from the process
80 * and copy it into the regs structure (<machine/reg.h>).
81 * The process is stopped at the time read_regs is called.
82 *

--- 67 unchanged lines hidden (view full) ---

150
151int
152proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
153{
154
155 PROC_ACTION(set_fpregs(td, fpregs));
156}
157
158#ifdef COMPAT_IA32
159/* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
141int
160int
161proc_read_regs32(struct thread *td, struct reg32 *regs32)
162{
163
164 PROC_ACTION(fill_regs32(td, regs32));
165}
166
167int
168proc_write_regs32(struct thread *td, struct reg32 *regs32)
169{
170
171 PROC_ACTION(set_regs32(td, regs32));
172}
173
174int
175proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
176{
177
178 PROC_ACTION(fill_dbregs32(td, dbregs32));
179}
180
181int
182proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
183{
184
185 PROC_ACTION(set_dbregs32(td, dbregs32));
186}
187
188int
189proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
190{
191
192 PROC_ACTION(fill_fpregs32(td, fpregs32));
193}
194
195int
196proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
197{
198
199 PROC_ACTION(set_fpregs32(td, fpregs32));
200}
201#endif
202
203int
142proc_sstep(struct thread *td)
143{
144
145 PROC_ACTION(ptrace_single_step(td));
146}
147
148int
149proc_rwmem(struct proc *p, struct uio *uio)

--- 135 unchanged lines hidden (view full) ---

285struct ptrace_args {
286 int req;
287 pid_t pid;
288 caddr_t addr;
289 int data;
290};
291#endif
292
204proc_sstep(struct thread *td)
205{
206
207 PROC_ACTION(ptrace_single_step(td));
208}
209
210int
211proc_rwmem(struct proc *p, struct uio *uio)

--- 135 unchanged lines hidden (view full) ---

347struct ptrace_args {
348 int req;
349 pid_t pid;
350 caddr_t addr;
351 int data;
352};
353#endif
354
355#ifdef COMPAT_IA32
293/*
356/*
357 * This CPP subterfuge is to try and reduce the number of ifdefs in
358 * the body of the code.
359 * COPYIN(uap->addr, &r.reg, sizeof r.reg);
360 * becomes either:
361 * copyin(uap->addr, &r.reg, sizeof r.reg);
362 * or
363 * copyin(uap->addr, &r.reg32, sizeof r.reg32);
364 * .. except this is done at runtime.
365 */
366#define COPYIN(u, k, s) wrap32 ? \
367 copyin(u, k ## 32, s ## 32) : \
368 copyin(u, k, s)
369#define COPYOUT(k, u, s) wrap32 ? \
370 copyout(k ## 32, u, s ## 32) : \
371 copyout(k, u, s)
372#else
373#define COPYIN(u, k, s) copyin(u, k, s)
374#define COPYOUT(k, u, s) copyout(k, u, s)
375#endif
376/*
294 * MPSAFE
295 */
296int
297ptrace(struct thread *td, struct ptrace_args *uap)
298{
299 /*
300 * XXX this obfuscation is to reduce stack usage, but the register
301 * structs may be too large to put on the stack anyway.
302 */
303 union {
304 struct ptrace_io_desc piod;
305 struct ptrace_lwpinfo pl;
306 struct dbreg dbreg;
307 struct fpreg fpreg;
308 struct reg reg;
377 * MPSAFE
378 */
379int
380ptrace(struct thread *td, struct ptrace_args *uap)
381{
382 /*
383 * XXX this obfuscation is to reduce stack usage, but the register
384 * structs may be too large to put on the stack anyway.
385 */
386 union {
387 struct ptrace_io_desc piod;
388 struct ptrace_lwpinfo pl;
389 struct dbreg dbreg;
390 struct fpreg fpreg;
391 struct reg reg;
392#ifdef COMPAT_IA32
393 struct dbreg32 dbreg32;
394 struct fpreg32 fpreg32;
395 struct reg32 reg32;
396 struct ptrace_io_desc32 piod32;
397#endif
309 } r;
310 void *addr;
311 int error = 0;
398 } r;
399 void *addr;
400 int error = 0;
401#ifdef COMPAT_IA32
402 int wrap32 = 0;
312
403
404 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec)
405 wrap32 = 1;
406#endif
313 addr = &r;
314 switch (uap->req) {
315 case PT_GETREGS:
316 case PT_GETFPREGS:
317 case PT_GETDBREGS:
318 case PT_LWPINFO:
319 break;
320 case PT_SETREGS:
407 addr = &r;
408 switch (uap->req) {
409 case PT_GETREGS:
410 case PT_GETFPREGS:
411 case PT_GETDBREGS:
412 case PT_LWPINFO:
413 break;
414 case PT_SETREGS:
321 error = copyin(uap->addr, &r.reg, sizeof r.reg);
415 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
322 break;
323 case PT_SETFPREGS:
416 break;
417 case PT_SETFPREGS:
324 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
418 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
325 break;
326 case PT_SETDBREGS:
419 break;
420 case PT_SETDBREGS:
327 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
421 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
328 break;
329 case PT_IO:
422 break;
423 case PT_IO:
330 error = copyin(uap->addr, &r.piod, sizeof r.piod);
424 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
331 break;
332 default:
333 addr = uap->addr;
334 break;
335 }
336 if (error)
337 return (error);
338
339 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
340 if (error)
341 return (error);
342
343 switch (uap->req) {
344 case PT_IO:
425 break;
426 default:
427 addr = uap->addr;
428 break;
429 }
430 if (error)
431 return (error);
432
433 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
434 if (error)
435 return (error);
436
437 switch (uap->req) {
438 case PT_IO:
345 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
439 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
346 break;
347 case PT_GETREGS:
440 break;
441 case PT_GETREGS:
348 error = copyout(&r.reg, uap->addr, sizeof r.reg);
442 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
349 break;
350 case PT_GETFPREGS:
443 break;
444 case PT_GETFPREGS:
351 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
445 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
352 break;
353 case PT_GETDBREGS:
446 break;
447 case PT_GETDBREGS:
354 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
448 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
355 break;
356 case PT_LWPINFO:
357 error = copyout(&r.pl, uap->addr, uap->data);
358 break;
359 }
360
361 return (error);
362}
449 break;
450 case PT_LWPINFO:
451 error = copyout(&r.pl, uap->addr, uap->data);
452 break;
453 }
454
455 return (error);
456}
457#undef COPYIN
458#undef COPYOUT
363
459
460#ifdef COMPAT_IA32
461/*
462 * PROC_READ(regs, td2, addr);
463 * becomes either:
464 * proc_read_regs(td2, addr);
465 * or
466 * proc_read_regs32(td2, addr);
467 * .. except this is done at runtime. There is an additional
468 * complication in that PROC_WRITE disallows 32 bit consumers
469 * from writing to 64 bit address space targets.
470 */
471#define PROC_READ(w, t, a) wrap32 ? \
472 proc_read_ ## w ## 32(t, a) : \
473 proc_read_ ## w (t, a)
474#define PROC_WRITE(w, t, a) wrap32 ? \
475 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
476 proc_write_ ## w (t, a)
477#else
478#define PROC_READ(w, t, a) proc_read_ ## w (t, a)
479#define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
480#endif
481
364int
365kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
366{
367 struct iovec iov;
368 struct uio uio;
369 struct proc *curp, *p, *pp;
370 struct thread *td2 = NULL;
482int
483kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
484{
485 struct iovec iov;
486 struct uio uio;
487 struct proc *curp, *p, *pp;
488 struct thread *td2 = NULL;
371 struct ptrace_io_desc *piod;
489 struct ptrace_io_desc *piod = NULL;
372 struct ptrace_lwpinfo *pl;
373 int error, write, tmp, num;
374 int proctree_locked = 0;
375 lwpid_t tid = 0, *buf;
376 pid_t saved_pid = pid;
490 struct ptrace_lwpinfo *pl;
491 int error, write, tmp, num;
492 int proctree_locked = 0;
493 lwpid_t tid = 0, *buf;
494 pid_t saved_pid = pid;
495#ifdef COMPAT_IA32
496 int wrap32 = 0, safe = 0;
497 struct ptrace_io_desc32 *piod32 = NULL;
498#endif
377
378 curp = td->td_proc;
379
380 /* Lock proctree before locking the process. */
381 switch (req) {
382 case PT_TRACE_ME:
383 case PT_ATTACH:
384 case PT_STEP:

--- 59 unchanged lines hidden (view full) ---

444 goto fail;
445 }
446
447 if (tid == 0) {
448 td2 = FIRST_THREAD_IN_PROC(p);
449 tid = td2->td_tid;
450 }
451
499
500 curp = td->td_proc;
501
502 /* Lock proctree before locking the process. */
503 switch (req) {
504 case PT_TRACE_ME:
505 case PT_ATTACH:
506 case PT_STEP:

--- 59 unchanged lines hidden (view full) ---

566 goto fail;
567 }
568
569 if (tid == 0) {
570 td2 = FIRST_THREAD_IN_PROC(p);
571 tid = td2->td_tid;
572 }
573
574#ifdef COMPAT_IA32
452 /*
575 /*
576 * Test if we're a 32 bit client and what the target is.
577 * Set the wrap controls accordingly.
578 */
579 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
580 if (td2->td_proc->p_sysent == &ia32_freebsd_sysvec)
581 safe = 1;
582 wrap32 = 1;
583 }
584#endif
585 /*
453 * Permissions check
454 */
455 switch (req) {
456 case PT_TRACE_ME:
457 /* Always legal. */
458 break;
459
460 case PT_ATTACH:

--- 257 unchanged lines hidden (view full) ---

718 error = EINVAL; /* EOF */
719 }
720 if (!write)
721 td->td_retval[0] = tmp;
722 return (error);
723
724 case PT_IO:
725 PROC_UNLOCK(p);
586 * Permissions check
587 */
588 switch (req) {
589 case PT_TRACE_ME:
590 /* Always legal. */
591 break;
592
593 case PT_ATTACH:

--- 257 unchanged lines hidden (view full) ---

851 error = EINVAL; /* EOF */
852 }
853 if (!write)
854 td->td_retval[0] = tmp;
855 return (error);
856
857 case PT_IO:
858 PROC_UNLOCK(p);
726 piod = addr;
727 iov.iov_base = piod->piod_addr;
728 iov.iov_len = piod->piod_len;
859#ifdef COMPAT_IA32
860 if (wrap32) {
861 piod32 = addr;
862 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
863 iov.iov_len = piod32->piod_len;
864 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
865 uio.uio_resid = piod32->piod_len;
866 } else
867#endif
868 {
869 piod = addr;
870 iov.iov_base = piod->piod_addr;
871 iov.iov_len = piod->piod_len;
872 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
873 uio.uio_resid = piod->piod_len;
874 }
729 uio.uio_iov = &iov;
730 uio.uio_iovcnt = 1;
875 uio.uio_iov = &iov;
876 uio.uio_iovcnt = 1;
731 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
732 uio.uio_resid = piod->piod_len;
733 uio.uio_segflg = UIO_USERSPACE;
734 uio.uio_td = td;
877 uio.uio_segflg = UIO_USERSPACE;
878 uio.uio_td = td;
735 switch (piod->piod_op) {
879#ifdef COMPAT_IA32
880 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
881#else
882 tmp = piod->piod_op;
883#endif
884 switch (tmp) {
736 case PIOD_READ_D:
737 case PIOD_READ_I:
738 uio.uio_rw = UIO_READ;
739 break;
740 case PIOD_WRITE_D:
741 case PIOD_WRITE_I:
742 uio.uio_rw = UIO_WRITE;
743 break;
744 default:
745 return (EINVAL);
746 }
747 error = proc_rwmem(p, &uio);
885 case PIOD_READ_D:
886 case PIOD_READ_I:
887 uio.uio_rw = UIO_READ;
888 break;
889 case PIOD_WRITE_D:
890 case PIOD_WRITE_I:
891 uio.uio_rw = UIO_WRITE;
892 break;
893 default:
894 return (EINVAL);
895 }
896 error = proc_rwmem(p, &uio);
748 piod->piod_len -= uio.uio_resid;
897#ifdef COMPAT_IA32
898 if (wrap32)
899 piod32->piod_len -= uio.uio_resid;
900 else
901#endif
902 piod->piod_len -= uio.uio_resid;
749 return (error);
750
751 case PT_KILL:
752 data = SIGKILL;
753 goto sendsig; /* in PT_CONTINUE above */
754
755 case PT_SETREGS:
756 _PHOLD(p);
903 return (error);
904
905 case PT_KILL:
906 data = SIGKILL;
907 goto sendsig; /* in PT_CONTINUE above */
908
909 case PT_SETREGS:
910 _PHOLD(p);
757 error = proc_write_regs(td2, addr);
911 error = PROC_WRITE(regs, td2, addr);
758 _PRELE(p);
759 PROC_UNLOCK(p);
760 return (error);
761
762 case PT_GETREGS:
763 _PHOLD(p);
912 _PRELE(p);
913 PROC_UNLOCK(p);
914 return (error);
915
916 case PT_GETREGS:
917 _PHOLD(p);
764 error = proc_read_regs(td2, addr);
918 error = PROC_READ(regs, td2, addr);
765 _PRELE(p);
766 PROC_UNLOCK(p);
767 return (error);
768
769 case PT_SETFPREGS:
770 _PHOLD(p);
919 _PRELE(p);
920 PROC_UNLOCK(p);
921 return (error);
922
923 case PT_SETFPREGS:
924 _PHOLD(p);
771 error = proc_write_fpregs(td2, addr);
925 error = PROC_WRITE(fpregs, td2, addr);
772 _PRELE(p);
773 PROC_UNLOCK(p);
774 return (error);
775
776 case PT_GETFPREGS:
777 _PHOLD(p);
926 _PRELE(p);
927 PROC_UNLOCK(p);
928 return (error);
929
930 case PT_GETFPREGS:
931 _PHOLD(p);
778 error = proc_read_fpregs(td2, addr);
932 error = PROC_READ(fpregs, td2, addr);
779 _PRELE(p);
780 PROC_UNLOCK(p);
781 return (error);
782
783 case PT_SETDBREGS:
784 _PHOLD(p);
933 _PRELE(p);
934 PROC_UNLOCK(p);
935 return (error);
936
937 case PT_SETDBREGS:
938 _PHOLD(p);
785 error = proc_write_dbregs(td2, addr);
939 error = PROC_WRITE(dbregs, td2, addr);
786 _PRELE(p);
787 PROC_UNLOCK(p);
788 return (error);
789
790 case PT_GETDBREGS:
791 _PHOLD(p);
940 _PRELE(p);
941 PROC_UNLOCK(p);
942 return (error);
943
944 case PT_GETDBREGS:
945 _PHOLD(p);
792 error = proc_read_dbregs(td2, addr);
946 error = PROC_READ(dbregs, td2, addr);
793 _PRELE(p);
794 PROC_UNLOCK(p);
795 return (error);
796
797 case PT_LWPINFO:
798 if (data == 0 || data > sizeof(*pl))
799 return (EINVAL);
800 pl = addr;

--- 66 unchanged lines hidden (view full) ---

867
868fail:
869 PROC_UNLOCK(p);
870fail_noproc:
871 if (proctree_locked)
872 sx_xunlock(&proctree_lock);
873 return (error);
874}
947 _PRELE(p);
948 PROC_UNLOCK(p);
949 return (error);
950
951 case PT_LWPINFO:
952 if (data == 0 || data > sizeof(*pl))
953 return (EINVAL);
954 pl = addr;

--- 66 unchanged lines hidden (view full) ---

1021
1022fail:
1023 PROC_UNLOCK(p);
1024fail_noproc:
1025 if (proctree_locked)
1026 sx_xunlock(&proctree_lock);
1027 return (error);
1028}
1029#undef PROC_READ
1030#undef PROC_WRITE
875
876/*
877 * Stop a process because of a debugging event;
878 * stay stopped until p->p_step is cleared
879 * (cleared by PIOCCONT in procfs).
880 */
881void
882stopevent(struct proc *p, unsigned int event, unsigned int val)

--- 12 unchanged lines hidden ---
1031
1032/*
1033 * Stop a process because of a debugging event;
1034 * stay stopped until p->p_step is cleared
1035 * (cleared by PIOCCONT in procfs).
1036 */
1037void
1038stopevent(struct proc *p, unsigned int event, unsigned int val)

--- 12 unchanged lines hidden ---