From nobody  Wed Apr 22 15:42:11 1998
Received: (from nobody@localhost)
          by hub.freebsd.org (8.8.8/8.8.8) id PAA22024;
          Wed, 22 Apr 1998 15:42:11 -0700 (PDT)
          (envelope-from nobody)
Message-Id: <199804222242.PAA22024@hub.freebsd.org>
Date: Wed, 22 Apr 1998 15:42:11 -0700 (PDT)
From: gilham@csl.sri.com
To: freebsd-gnats-submit@freebsd.org
Subject: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run.
X-Send-Pr-Version: www-1.0

>Number:         6389
>Category:       kern
>Synopsis:       Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run.
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Apr 22 15:50:02 PDT 1998
>Closed-Date:    Tue Apr 28 21:21:23 PDT 1998
>Last-Modified:  Tue Apr 28 21:22:10 PDT 1998
>Originator:     Fred Gilham
>Release:        FreeBSD 2.2?, FreeBSD 3.0
>Organization:
SRI International
>Environment:
N/A
>Description:
The mapping between traps and signals is slightly different in FreeBSD
and Linux.  Ordinarily this doesn't cause problems but some applications
depend on being able to handle certain traps.  In particular, Allegro
Common Lisp, a commercial version of Common Lisp that was released to
the Linux community for free non-commercial use, depends on being able
to intercept T_PROTFLT traps to expand its heap if necessary.  In Linux
these traps are mapped to segmentation violations.  In FreeBSD they are
bus errors.  As a result, ACL will come up under the FreeBSD Linux
emulator and execute small programs but will die with a bus error if you
try to do anything significant.

I've worked with Mike Smith to resolve this problem.  He suggested
adding a mapping function to the sigentvec structure that emulators
could use to remap traps to the signals they wish.  I've implemented a
test version of this change and it allows ACL to work and run
significant applications.
>How-To-Repeat:
Run Allegro Common Lisp (available from www.franz.com) under the Linux
emulator.  Try to compile a lisp program.  You will get a bus error.
>Fix:
My test fixes are available from
ftp.csl.sri.com:/pub/gilham/trap-fixes.tar.gz.  They are directly
applicable to FreeBSD 3.0-980414-SNAP but they are relatively trivial
to apply to other releases.

>Release-Note:
>Audit-Trail:

From: Eivind Eklund <eivind@yes.no>
To: gilham@csl.sri.com, freebsd-gnats-submit@FreeBSD.ORG
Cc: msmith@FreeBSD.ORG
Subject: Re: kern/6389: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run.
Date: Thu, 23 Apr 1998 01:56:22 +0200

 I've gone through these changes and fixed style-problems; here are the
 changes as diffs against today's current with hopefully OK stule.
 This pass a LINT compile (I've not yet tested running with a new
 kernel on my own machine; I'll do this in a few minutes).
 
 Index: i386/i386/trap.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/i386/i386/trap.c,v
 retrieving revision 1.126
 diff -u -r1.126 trap.c
 --- trap.c	1998/04/15 17:45:07	1.126
 +++ trap.c	1998/04/22 23:31:20
 @@ -545,6 +545,9 @@
  		return;
  	}
  
 +	if (*p->p_sysent->sv_transtrap)
 +		i = (*p->p_sysent->sv_transtrap)(i, type);
 +
  	trapsignal(p, i, ucode);
  
  #ifdef DEBUG
 Index: i386/ibcs2/ibcs2_sysvec.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/i386/ibcs2/ibcs2_sysvec.c,v
 retrieving revision 1.6
 diff -u -r1.6 ibcs2_sysvec.c
 --- ibcs2_sysvec.c	1997/02/22 09:33:28	1.6
 +++ ibcs2_sysvec.c	1998/04/22 02:59:52
 @@ -49,6 +49,7 @@
          bsd_to_ibcs2_sig,
          ELAST,
          bsd_to_ibcs2_errno,
 +	0,              /* Trap-to-signal translation function */
  	0,		/* fixup */
  	sendsig,
  	sigcode,	/* use generic trampoline */
 Index: i386/linux/linux_sysvec.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/i386/linux/linux_sysvec.c,v
 retrieving revision 1.27
 diff -u -r1.27 linux_sysvec.c
 --- linux_sysvec.c	1998/04/13 17:49:51	1.27
 +++ linux_sysvec.c	1998/04/22 23:21:09
 @@ -98,6 +98,31 @@
  	SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGURG, SIGURG, 0
  };
  
 +
 +/*
 +  If FreeBSD & Linux have a difference of opinion about what a trap
 +  means, deal with it here.
 +  */
 +static int
 +translate_traps(int signal, int trap_code)
 +{
 +	switch(signal) {
 +	case SIGBUS:
 +		switch(trap_code) {
 +
 +		case T_PROTFLT:
 +			return SIGSEGV;
 +
 +		default:
 +			return signal;
 +		}
 +
 +	default:
 +		return signal;
 +	}
 +}
 +
 +
  static int
  linux_fixup(int **stack_base, struct image_params *imgp)
  {
 @@ -374,6 +399,7 @@
  	bsd_to_linux_signal,
  	ELAST, 
  	bsd_to_linux_errno,
 +	translate_traps,
  	linux_fixup,
  	linux_sendsig,
  	linux_sigcode,	
 @@ -390,6 +416,7 @@
          bsd_to_linux_signal,
          ELAST,
          bsd_to_linux_errno,
 +        translate_traps,
          elf_linux_fixup,
          linux_sendsig,
          linux_sigcode,
 Index: kern/imgact_elf.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/kern/imgact_elf.c,v
 retrieving revision 1.23
 diff -u -r1.23 imgact_elf.c
 --- imgact_elf.c	1998/03/28 13:24:52	1.23
 +++ imgact_elf.c	1998/04/22 23:22:31
 @@ -78,6 +78,7 @@
          0,
          0,
          0,
 +        0,
          elf_freebsd_fixup,
          sendsig,
          sigcode,
 Index: kern/init_sysvec.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/kern/init_sysvec.c,v
 retrieving revision 1.5
 diff -u -r1.5 init_sysvec.c
 --- init_sysvec.c	1997/08/02 14:31:26	1.5
 +++ init_sysvec.c	1998/04/22 02:57:51
 @@ -19,6 +19,7 @@
  	0,
  	0,
  	0,
 +	0,
  	sendsig,
  	sigcode,
  	&szsigcode,
 Index: sys/sysent.h
 ===================================================================
 RCS file: /home/ncvs/src/sys/sys/sysent.h,v
 retrieving revision 1.16
 diff -u -r1.16 sysent.h
 --- sysent.h	1998/02/03 21:51:57	1.16
 +++ sysent.h	1998/04/22 23:32:51
 @@ -58,6 +58,8 @@
  	int		*sv_sigtbl;	/* signal translation table */
  	int		sv_errsize;	/* size of errno translation table */
  	int 		*sv_errtbl;	/* errno translation table */
 +	int		(*sv_transtrap) __P((int, int));
 +					/* translate trap-to-signal mapping */
  	int		(*sv_fixup) __P((int **, struct image_params *));
  					/* stack fixup function */
  	void		(*sv_sendsig) __P((void (*)(int), int, int, u_long));

From: Mike Smith <mike@smith.net.au>
To: Eivind Eklund <eivind@yes.no>
Cc: gilham@csl.sri.com, freebsd-gnats-submit@FreeBSD.ORG, msmith@FreeBSD.ORG
Subject: Re: kern/6389: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run. 
Date: Thu, 23 Apr 1998 12:07:24 -0700

 > I've gone through these changes and fixed style-problems; here are the
 > changes as diffs against today's current with hopefully OK stule.
 > This pass a LINT compile (I've not yet tested running with a new
 > kernel on my own machine; I'll do this in a few minutes).
 
 Just of curiosity, how does this handle the case where the traphandler 
 does:
 
  	psignal();
 	return;
 
 ie. never gets to the trapsignal() call?
 
 In particular, I thought this was the case with the T_PROTFLT trap...
 
 > Index: i386/i386/trap.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/i386/i386/trap.c,v
 > retrieving revision 1.126
 > diff -u -r1.126 trap.c
 > --- trap.c	1998/04/15 17:45:07	1.126
 > +++ trap.c	1998/04/22 23:31:20
 > @@ -545,6 +545,9 @@
 >  		return;
 >  	}
 >  
 > +	if (*p->p_sysent->sv_transtrap)
 > +		i = (*p->p_sysent->sv_transtrap)(i, type);
 > +
 >  	trapsignal(p, i, ucode);
 >  
 >  #ifdef DEBUG
 > Index: i386/ibcs2/ibcs2_sysvec.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/i386/ibcs2/ibcs2_sysvec.c,v
 > retrieving revision 1.6
 > diff -u -r1.6 ibcs2_sysvec.c
 > --- ibcs2_sysvec.c	1997/02/22 09:33:28	1.6
 > +++ ibcs2_sysvec.c	1998/04/22 02:59:52
 > @@ -49,6 +49,7 @@
 >          bsd_to_ibcs2_sig,
 >          ELAST,
 >          bsd_to_ibcs2_errno,
 > +	0,              /* Trap-to-signal translation function */
 >  	0,		/* fixup */
 >  	sendsig,
 >  	sigcode,	/* use generic trampoline */
 > Index: i386/linux/linux_sysvec.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/i386/linux/linux_sysvec.c,v
 > retrieving revision 1.27
 > diff -u -r1.27 linux_sysvec.c
 > --- linux_sysvec.c	1998/04/13 17:49:51	1.27
 > +++ linux_sysvec.c	1998/04/22 23:21:09
 > @@ -98,6 +98,31 @@
 >  	SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGURG, SIGURG, 0
 >  };
 >  
 > +
 > +/*
 > +  If FreeBSD & Linux have a difference of opinion about what a trap
 > +  means, deal with it here.
 > +  */
 > +static int
 > +translate_traps(int signal, int trap_code)
 > +{
 > +	switch(signal) {
 > +	case SIGBUS:
 > +		switch(trap_code) {
 > +
 > +		case T_PROTFLT:
 > +			return SIGSEGV;
 > +
 > +		default:
 > +			return signal;
 > +		}
 > +
 > +	default:
 > +		return signal;
 > +	}
 > +}
 > +
 > +
 >  static int
 >  linux_fixup(int **stack_base, struct image_params *imgp)
 >  {
 > @@ -374,6 +399,7 @@
 >  	bsd_to_linux_signal,
 >  	ELAST, 
 >  	bsd_to_linux_errno,
 > +	translate_traps,
 >  	linux_fixup,
 >  	linux_sendsig,
 >  	linux_sigcode,	
 > @@ -390,6 +416,7 @@
 >          bsd_to_linux_signal,
 >          ELAST,
 >          bsd_to_linux_errno,
 > +        translate_traps,
 >          elf_linux_fixup,
 >          linux_sendsig,
 >          linux_sigcode,
 > Index: kern/imgact_elf.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/kern/imgact_elf.c,v
 > retrieving revision 1.23
 > diff -u -r1.23 imgact_elf.c
 > --- imgact_elf.c	1998/03/28 13:24:52	1.23
 > +++ imgact_elf.c	1998/04/22 23:22:31
 > @@ -78,6 +78,7 @@
 >          0,
 >          0,
 >          0,
 > +        0,
 >          elf_freebsd_fixup,
 >          sendsig,
 >          sigcode,
 > Index: kern/init_sysvec.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/kern/init_sysvec.c,v
 > retrieving revision 1.5
 > diff -u -r1.5 init_sysvec.c
 > --- init_sysvec.c	1997/08/02 14:31:26	1.5
 > +++ init_sysvec.c	1998/04/22 02:57:51
 > @@ -19,6 +19,7 @@
 >  	0,
 >  	0,
 >  	0,
 > +	0,
 >  	sendsig,
 >  	sigcode,
 >  	&szsigcode,
 > Index: sys/sysent.h
 > ===================================================================
 > RCS file: /home/ncvs/src/sys/sys/sysent.h,v
 > retrieving revision 1.16
 > diff -u -r1.16 sysent.h
 > --- sysent.h	1998/02/03 21:51:57	1.16
 > +++ sysent.h	1998/04/22 23:32:51
 > @@ -58,6 +58,8 @@
 >  	int		*sv_sigtbl;	/* signal translation table */
 >  	int		sv_errsize;	/* size of errno translation table */
 >  	int 		*sv_errtbl;	/* errno translation table */
 > +	int		(*sv_transtrap) __P((int, int));
 > +					/* translate trap-to-signal mapping */
 >  	int		(*sv_fixup) __P((int **, struct image_params *));
 >  					/* stack fixup function */
 >  	void		(*sv_sendsig) __P((void (*)(int), int, int, u_long));
 > 
 
 -- 
 \\  Sometimes you're ahead,       \\  Mike Smith
 \\  sometimes you're behind.      \\  mike@smith.net.au
 \\  The race is long, and in the  \\  msmith@freebsd.org
 \\  end it's only with yourself.  \\  msmith@cdrom.com
 
 

From: Eivind Eklund <eivind@yes.no>
To: Mike Smith <mike@smith.net.au>
Cc: gilham@csl.sri.com, freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: kern/6389: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run.
Date: Thu, 23 Apr 1998 21:25:59 +0200

 On Thu, Apr 23, 1998 at 12:07:24PM -0700, Mike Smith wrote:
 > > I've gone through these changes and fixed style-problems; here are the
 > > changes as diffs against today's current with hopefully OK stule.
 > > This pass a LINT compile (I've not yet tested running with a new
 > > kernel on my own machine; I'll do this in a few minutes).
 > 
 > Just of curiosity, how does this handle the case where the traphandler 
 > does:
 > 
 >  	psignal();
 > 	return;
 > 
 > ie. never gets to the trapsignal() call?
 > 
 > In particular, I thought this was the case with the T_PROTFLT trap...
 
 The psignal() here is wrapped in
    if (intr_nesting_level == 0) {
 which seems to not trigger, at least not for the Linux emulator case.
 
 I run with the patch, and it _do_ make Allegro Common Lisp for Linux
 work properly.
 
 If somebody that know this code (trap.c with friends) better than me
 care to comment, that would be nice.
 
 Eivind.

From: Fred Gilham <gilham@csl.sri.com>
To: Mike Smith <mike@smith.net.au>
Cc: Eivind Eklund <eivind@yes.no>, freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: kern/6389: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run. 
Date: Thu, 23 Apr 1998 17:01:47 -0700

 Mike asked:
 >Just of curiosity, how does this handle the case where the traphandler 
 >does:
 >
 > 	psignal();
 >	return;
 >
 >ie. never gets to the trapsignal() call?
 
 Note that the trap() function is basically broken into a user trap
 branch and a kernel trap branch.
 
 Your scenario would only happen in the kernel trap branch of the
 trap() function.  I'd actually verified that just fixing what the
 T_PROTFLT case does in the user trap branch would make ACL work.  So I
 was pretty sure I didn't need to worry about the kernel branch.
 However, that does somewhat limit the generality of the fix.  But I
 don't know enough to know whether it's even desirable to translate
 things that happen in the kernel trap branch.
 
 I think someone who understands how the trap stuff works needs to
 review this change.
 
 -Fred

From: Fred Gilham <gilham@csl.sri.com>
To: Eivind Eklund <eivind@yes.no>, Mike Smith <mike@smith.net.au>,
        freebsd-gnats-submit@FreeBSD.ORG
Cc: dg@root.com
Subject: Re: kern/6389: Need to be able to access trap-to-signal mapping to for Linux emulation to allow Allegro Common Lisp (and maybe other apps) to run. 
Date: Sat, 25 Apr 1998 08:36:01 -0700

 David Greenman wrote:
 
 >   I haven't been following this thread, but the kernel branch is only for
 >traps that occur within the kernel. You would not want any translation to
 >occur there.
 
 
 If this is the case, then the lines
 
         if(*p->p_sysent->sv_trantrap != 0)
           i = (*p->p_sysent->sv_trantrap)(i, type);
 
 in trap.c should be moved up before the
 
         } else {
 #ifdef VM86
 kernel_trap:
 #endif
                 /* kernel trap */
 
 
 
 E.g. apply this patch:
 
 *** trap.c.old  Sat Apr 25 08:32:46 1998
 --- trap.c      Sat Apr 25 08:33:41 1998
 ***************
 *** 385,390 ****
 --- 385,394 ----
                         i = SIGILL;
                         break;
                 }
 + 
 +               if(*p->p_sysent->sv_trantrap != 0)
 +                 i = (*p->p_sysent->sv_trantrap)(i, type);
 + 
         } else {
   #ifdef VM86
   kernel_trap:
 ***************
 *** 544,552 ****
                 trap_fatal(&frame);
                 return;
         }
 - 
 -         if(*p->p_sysent->sv_trantrap != 0)
 -           i = (*p->p_sysent->sv_trantrap)(i, type);
   
         trapsignal(p, i, ucode);
   
 --- 548,553 ----
 
 
 This will avoid any strange problems with translating kernel traps by
 accident.
 
 -Fred Gilham    gilham@csl.sri.com
State-Changed-From-To: open->closed 
State-Changed-By: eivind 
State-Changed-When: Tue Apr 28 21:21:23 PDT 1998 
State-Changed-Why:  
The patch has been integrated in -current -my personal thanks! 
>Unformatted:
