Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 Jul 1999 14:31:29 -0400 (EDT)
From:      Brian Dean <brdean@unx.sas.com>
To:        freebsd-hackers@freebsd.org
Cc:        jlemon@americantv.com, peter@netplex.com.au, freebsd-current@freebsd.org
Subject:   Re: support for i386 hardware debug watch points
Message-ID:  <199907071831.OAA16835@dean.pc.sas.com>
In-Reply-To: <19990706142523.38487@right.PCS> from Jonathan Lemon at "Jul 6, 1999 02:25:23 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

(I've CC'd -current because of the implications there ...)

Here are my patches for hardware debug register support for the i386
port.  I think this is ready to be reviewed and hopefully committed.
It consists of modifications to 13 files and the addition of 1 new
file.  The new file is listed out at the bottom of this message.  My
implementation consists of:

	* saving and restoring the debug registers at context switch
          time if necessary

        * procfs modifications to support reading/writing to the
          'dbregs' file of the process, which works in an identical
          manner as the interface to 'regs' and 'fpregs'.

        * two additional request implementations for the 'ptrace()'
          call: PT_GETDBREGS and PT_SETDBREGS.

The only thing that it does not do right now that might be useful
(that I'm aware of) is include the state of the debug registers in a
core dump.  This was more than I wanted to get into at the moment, and
would require modification of any tools that read a core dump image.

Also, I've made a first pass attempt at security by denying a process
the ability to set a debug watch or breakpoint outside of his address
space, unless the process is owned by root.  It can certainly be
useful to use these registers for kernel debugging, but I thought that
should be limited to the root user.

I haven't done anything with the Alpha, though some of the procfs mods
will affect the Alpha port, at least to the point of possibly needing
to include a couple of stub functions in MD code to avoid unresolved
references for 'procfs_read_dbregs()' and 'procfs_write_dbregs()'.

Also, I have not included anything for 'gdb' because I just have not
had time to figure it out yet.  I'm hoping that someone else, who
knows the gdb code better than I, can make the required modifications
there for hardware debug support.  Essentially, you would need to make
a call like this:

	{
	  struct dbreg dbregs

	  ptrace ( PT_GETDBREGS, pid, &dbregs, 0 );
	  dbregs.dr7 |= 0x000d0002;
	  dbregs.dr0 = watch_addr;
	  ptrace ( PT_SETDBREGS, pid, &dbregs, 0 );
	}

This would enable the first watch address and would break whenever the
4 bytes at watch_addr were written to.  An Intel ix86 systems
programming manual will have detailed information on how to set up the
registers for various breakpoint and watchpoint conditions.  The
manuals can be downloaded in pdf format from
http://developer.intel.com if anyone is interested.  If no one wants
to take this on, I will give it a try, but I can't commit to it being
done within the next week or so.

Here is the patch set, generated against a current tree about 4 hours
or so old.  The new file that is required is listed out after the
patches.  Please let me know if anything else is needed or if anything
needs to be changed (style or feature).

Thanks to Jonathan Lemon, Peter Wemm, and others for answering my
questions.

Thanks,
--
Brian Dean		SAS Institute Inc	brdean@unx.sas.com


Index: conf/files
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/conf/files,v
retrieving revision 1.228
diff -r1.228 files
364a365
> miscfs/procfs/procfs_dbregs.c	standard
Index: i386/i386/genassym.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/i386/genassym.c,v
retrieving revision 1.72
diff -r1.72 genassym.c
130a131,137
>         printf("#define\tPCB_DR0 %#x\n", OS(pcb, pcb_dr0));
>         printf("#define\tPCB_DR1 %#x\n", OS(pcb, pcb_dr1));
>         printf("#define\tPCB_DR2 %#x\n", OS(pcb, pcb_dr2));
>         printf("#define\tPCB_DR3 %#x\n", OS(pcb, pcb_dr3));
>         printf("#define\tPCB_DR6 %#x\n", OS(pcb, pcb_dr6));
>         printf("#define\tPCB_DR7 %#x\n", OS(pcb, pcb_dr7));
>         printf("#define\tPCB_DBREGS %#x\n", PCB_DBREGS );
Index: i386/i386/machdep.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/i386/machdep.c,v
retrieving revision 1.353
diff -r1.353 machdep.c
1929a1930,2010
> int
> fill_dbregs(p, dbregs)
>         struct proc *p;
>         struct dbreg *dbregs;
> {
>         struct pcb *pcb;
> 
>         pcb = &p->p_addr->u_pcb;
>         dbregs->dr0 = pcb->pcb_dr0;
>         dbregs->dr1 = pcb->pcb_dr1;
>         dbregs->dr2 = pcb->pcb_dr2;
>         dbregs->dr3 = pcb->pcb_dr3;
>         dbregs->dr4 = 0;
>         dbregs->dr5 = 0;
>         dbregs->dr6 = pcb->pcb_dr6;
>         dbregs->dr7 = pcb->pcb_dr7;
>         return (0);
> }
> 
> int
> set_dbregs(p, dbregs)
>         struct proc *p;
>         struct dbreg *dbregs;
> {
>         struct pcb *pcb;
> 
>         pcb = &p->p_addr->u_pcb;
> 
>         /*
>          * Don't let a process set a breakpoint that is not within the
>          * process's address space.  If a process could do this, it
>          * could halt the system by setting a breakpoint in the kernel
>          * (if ddb was enabled).  Thus, we need to check to make sure
>          * that no breakpoints are being enabled for addresses outside
>          * process's address space, unless, perhaps, we were called by
>          * uid 0.
>          *
>          * XXX - what about when the watched area of the user's
>          * address space is written into from within the kernel
>          * ... wouldn't that still cause a breakpoint to be generated
>          * from within kernel mode?
>          */
> 
>         if (p->p_cred->pc_ucred->cr_uid != 0) {
>                 if (dbregs->dr7 & 0x3) {
>                         /* dr0 is enabled */
>                         if (dbregs->dr0 >= VM_MAXUSER_ADDRESS)
>                                 return (EINVAL);
>                 }
>                 
>                 if (dbregs->dr7 & (0x3<<2)) {
>                         /* dr1 is enabled */
>                         if (dbregs->dr1 >= VM_MAXUSER_ADDRESS)
>                                 return (EINVAL);
>                 }
>                 
>                 if (dbregs->dr7 & (0x3<<4)) {
>                         /* dr2 is enabled */
>                         if (dbregs->dr2 >= VM_MAXUSER_ADDRESS)
>                                 return (EINVAL);
>                 }
>                 
>                 if (dbregs->dr7 & (0x3<<6)) {
>                         /* dr3 is enabled */
>                         if (dbregs->dr3 >= VM_MAXUSER_ADDRESS)
>                                 return (EINVAL);
>                 }
>         }
> 
>         pcb->pcb_dr0 = dbregs->dr0;
>         pcb->pcb_dr1 = dbregs->dr1;
>         pcb->pcb_dr2 = dbregs->dr2;
>         pcb->pcb_dr3 = dbregs->dr3;
>         pcb->pcb_dr6 = dbregs->dr6;
>         pcb->pcb_dr7 = dbregs->dr7;
>         
>         pcb->pcb_flags |= PCB_DBREGS;
> 
>         return (0);
> }
> 
Index: i386/i386/procfs_machdep.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/i386/procfs_machdep.c,v
retrieving revision 1.11
diff -r1.11 procfs_machdep.c
61a62,64
>  * procfs_read_dbregs, procfs_write_dbregs
>  *	deal with the processor debug register set, otherwise as above.
>  *
100a104,123
> }
> 
> int
> procfs_read_dbregs(p, dbregs)
> 	struct proc *p;
> 	struct dbreg *dbregs;
> {
> 	if ((p->p_flag & P_INMEM) == 0)
> 		return (EIO);
> 	return (fill_dbregs(p, dbregs));
> }
> 
> int
> procfs_write_dbregs(p, dbregs)
> 	struct proc *p;
> 	struct dbreg *dbregs;
> {
> 	if ((p->p_flag & P_INMEM) == 0)
> 		return (EIO);
> 	return (set_dbregs(p, dbregs));
Index: i386/i386/swtch.s
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/i386/swtch.s,v
retrieving revision 1.83
diff -r1.83 swtch.s
482a483,502
> 	/* test if debug regisers should be saved */
> 	movb    PCB_FLAGS(%edx),%al
> 	andb    $PCB_DBREGS,%al
> 	jz      1f                              /* no, skip over */
> 	movl    %dr7,%eax                       /* yes, do the save */
> 	movl    %eax,PCB_DR7(%edx)
> 	andl    $0x0000ff00, %eax               /* disable all watchpoints */
> 	movl    %eax,%dr7
> 	movl    %dr6,%eax
> 	movl    %eax,PCB_DR6(%edx)
> 	movl    %dr3,%eax
> 	movl    %eax,PCB_DR3(%edx)
> 	movl    %dr2,%eax
> 	movl    %eax,PCB_DR2(%edx)
> 	movl    %dr1,%eax
> 	movl    %eax,PCB_DR1(%edx)
> 	movl    %dr0,%eax
> 	movl    %eax,PCB_DR0(%edx)
> 1:
>  
719a740,757
> 
> 	/* test if debug regisers should be restored */
> 	movb    PCB_FLAGS(%edx),%al
> 	andb    $PCB_DBREGS,%al
> 	jz      1f                              /* no, skip over */
> 	movl    PCB_DR6(%edx),%eax              /* yes, do the restore */
> 	movl    %eax,%dr6
> 	movl    PCB_DR3(%edx),%eax
> 	movl    %eax,%dr3
> 	movl    PCB_DR2(%edx),%eax
> 	movl    %eax,%dr2
> 	movl    PCB_DR1(%edx),%eax
> 	movl    %eax,%dr1
> 	movl    PCB_DR0(%edx),%eax
> 	movl    %eax,%dr0
> 	movl    PCB_DR7(%edx),%eax
> 	movl    %eax,%dr7
> 1:
Index: i386/include/md_var.h
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/include/md_var.h,v
retrieving revision 1.29
diff -r1.29 md_var.h
66a67
> struct  dbreg;
82a84
> int	fill_dbregs __P((struct proc *p, struct dbreg *dbregs));
Index: i386/include/pcb.h
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/include/pcb.h,v
retrieving revision 1.28
diff -r1.28 pcb.h
56a57,64
> 
>         int     pcb_dr0;
>         int     pcb_dr1;
>         int     pcb_dr2;
>         int     pcb_dr3;
>         int     pcb_dr6;
>         int     pcb_dr7;
> 
61a70
> #define	PCB_DBREGS	0x02	/* process using debug registers */
Index: i386/include/ptrace.h
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/include/ptrace.h,v
retrieving revision 1.6
diff -r1.6 ptrace.h
46a47,48
> #define PT_GETDBREGS    (PT_FIRSTMACH + 5)
> #define PT_SETDBREGS    (PT_FIRSTMACH + 6)
Index: i386/include/reg.h
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/i386/include/reg.h,v
retrieving revision 1.18
diff -r1.18 reg.h
120a121,132
> struct dbreg {
> 	unsigned int  dr0;	/* debug address register 0 */
> 	unsigned int  dr1;	/* debug address register 1 */
> 	unsigned int  dr2;	/* debug address register 2 */
> 	unsigned int  dr3;	/* debug address register 3 */
> 	unsigned int  dr4;	/* reserved */
> 	unsigned int  dr5;	/* reserved */
> 	unsigned int  dr6;	/* debug status register */
> 	unsigned int  dr7;	/* debug control register */
> };
> 
> 
127a140
> int	set_dbregs __P((struct proc *p, struct dbreg *dbregs));
Index: kern/sys_process.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/kern/sys_process.c,v
retrieving revision 1.46
diff -r1.46 sys_process.c
277a278,283
> #ifdef PT_GETDBREGS
> 	case PT_GETDBREGS:
> #endif
> #ifdef PT_SETDBREGS
> 	case PT_SETDBREGS:
> #endif
503a510,535
> 
> #ifdef PT_SETDBREGS
> 	case PT_SETDBREGS:
> 		write = 1;
> 		/* fallthrough */
> #endif /* PT_SETDBREGS */
> #ifdef PT_GETDBREGS
> 	case PT_GETDBREGS:
> 		/* write = 0 above */
> #endif /* PT_SETDBREGS */
> #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
> 		if (!procfs_validdbregs(p))	/* no P_SYSTEM procs please */
> 			return EINVAL;
> 		else {
> 			iov.iov_base = uap->addr;
> 			iov.iov_len = sizeof(struct dbreg);
> 			uio.uio_iov = &iov;
> 			uio.uio_iovcnt = 1;
> 			uio.uio_offset = 0;
> 			uio.uio_resid = sizeof(struct dbreg);
> 			uio.uio_segflg = UIO_USERSPACE;
> 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
> 			uio.uio_procp = curp;
> 			return (procfs_dodbregs(curp, p, NULL, &uio));
> 		}
> #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
Index: miscfs/procfs/procfs.h
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/miscfs/procfs/procfs.h,v
retrieving revision 1.26
diff -r1.26 procfs.h
53a54
> 	Pdbregs,	/* the process's debug register set */
126a128
> struct dbreg;
139a142,143
> int procfs_read_dbregs __P((struct proc *, struct dbreg *));
> int procfs_write_dbregs __P((struct proc *, struct dbreg *));
142a147
> int procfs_dodbregs __P((struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio));
157a163
> int procfs_validdbregs __P((struct proc *));
Index: miscfs/procfs/procfs_subr.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/miscfs/procfs/procfs_subr.c,v
retrieving revision 1.24
diff -r1.24 procfs_subr.c
169a170
> 	case Pdbregs:
265a267,270
> 
>         case Pdbregs:
>                 rtval = procfs_dodbregs(curp, p, pfs, uio);
>                 break;
Index: miscfs/procfs/procfs_vnops.c
===================================================================
RCS file: /usr/mirror/ncvs/src/sys/miscfs/procfs/procfs_vnops.c,v
retrieving revision 1.69
diff -r1.69 procfs_vnops.c
97a98
>         { DT_REG, N("dbregs"),  Pdbregs,        procfs_validdbregs },
493a495
> 	case Pdbregs:
572a575,578
> 
>         case Pdbregs:
>                 vap->va_bytes = vap->va_size = sizeof(struct dbreg);
>                 break;


----------------------------------------------------------------------
And here is the new file:

/*
 * Copyright (c) 1999 Brian Scott Dean, brdean@unx.sas.com.
 *                    All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Jan-Simon Pendry under the following copyrights and conditions:
 *
 * Copyright (c) 1993 Jan-Simon Pendry
 * Copyright (c) 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Jan-Simon Pendry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <machine/reg.h>
#include <miscfs/procfs/procfs.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>

int
procfs_dodbregs(curp, p, pfs, uio)
	struct proc *curp;
	struct proc *p;
	struct pfsnode *pfs;
	struct uio *uio;
{
	int error;
	struct dbreg r;
	char *kv;
	int kl;

	if (!CHECKIO(curp, p))
		return EPERM;
	kl = sizeof(r);
	kv = (char *) &r;

	kv += uio->uio_offset;
	kl -= uio->uio_offset;
	if (kl > uio->uio_resid)
		kl = uio->uio_resid;

	PHOLD(p);

	if (kl < 0)
		error = EINVAL;
	else
		error = procfs_read_dbregs(p, &r);
	if (error == 0)
		error = uiomove(kv, kl, uio);
	if (error == 0 && uio->uio_rw == UIO_WRITE) {
		if (p->p_stat != SSTOP)
			error = EBUSY;
		else
			error = procfs_write_dbregs(p, &r);
	}
	PRELE(p);

	uio->uio_offset = 0;
	return (error);
}

int
procfs_validdbregs(p)
	struct proc *p;
{
	return ((p->p_flag & P_SYSTEM) == 0);
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199907071831.OAA16835>