Interesting code in exec_setregs

David G. Lawrence dg at dglawrence.com
Sun Sep 26 23:04:38 PDT 2004


> 
> --- David Xu <davidxu at freebsd.org> wrote:
> 
> > I found following code in  sys/i386/i386/machdep.c:
> > function exec_setregs:
> > 
> >  bzero((char *)regs, sizeof(struct trapframe));
> >  regs->tf_eip = entry;
> >  regs->tf_esp = stack;
> >  regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
> > 
> > the regs is cleared to zero, why do we bother to test it
> > again ? regs->tf_eflags & PSL_T  is useless code.
> > 
> > AMD64 also has this, I also found it in releng_4, why ?
> 
> FYI, it's first incarnation was in v1.87 [1] (1994-11-06):
> 
>         !           864: ? ? ? ?bzero(regs, sizeof(struct
> trapframe));
>         !           865: ? ? ? ?regs[tEIP] = entry;
>         !           866: ? ? ? ?regs[tESP] = stack;
>         !           867: ? ? ? ?regs[tEFLAGS] = PSL_USERSET |
> (regs[tEFLAGS] & PSL_T);
>         !           868: ? ? ? ?regs[tSS] = _udatasel;
>         !           869: ? ? ? ?regs[tDS] = _udatasel;
>         !           870: ? ? ? ?regs[tES] = _udatasel;
>         !           871: ? ? ? ?regs[tCS] = _ucodesel;
> 
> Same issue, it seems.


   Hmmm. That would make it my bug. :-/ My only excuse is that things were
pretty crazy back then - this would have happened when John Dyson and I
were working on the x86 port of 4.4-lite which became FreeBSD 2.0. We did
the port in 10 days. :-) My best explaination is that the bzero got added
after the tEFLAGS stuff - probably to solve some other issue, and the test
of PSL_T (actually should be called TF bit to match Intel literature, which
stands for "trap flag") a few lines lower was overlooked. The bit enables
debug traps after every instruction in order to facilitate instruction
single stepping.
   It's not clear to me if instruction single stepping should be preserved
after an exec. I'd defer to Bruce Evans on this since he's the expert on
debuggers.

-DG

David G. Lawrence
President
Download Technologies, Inc. - http://www.downloadtech.com - (866) 399 8500
TeraSolutions, Inc. - http://www.terasolutions.com - (888) 346 7175
The FreeBSD Project - http://www.freebsd.org
Pave the road of life with opportunities.


More information about the freebsd-current mailing list