From nobody@FreeBSD.org  Sun Oct  3 03:10:05 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 3D241106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  3 Oct 2010 03:10:05 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 2CAFF8FC08
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  3 Oct 2010 03:10:05 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o933A4ix095897
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 3 Oct 2010 03:10:04 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o933A41i095896;
	Sun, 3 Oct 2010 03:10:04 GMT
	(envelope-from nobody)
Message-Id: <201010030310.o933A41i095896@www.freebsd.org>
Date: Sun, 3 Oct 2010 03:10:04 GMT
From: Zhouyi Zhou <zhouzhouyi@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: amd64 remote debug fails
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         151167
>Category:       amd64
>Synopsis:       amd64 remote debug fails
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Oct 03 03:20:02 UTC 2010
>Closed-Date:    Sun Oct 10 07:30:44 UTC 2010
>Last-Modified:  Sun Oct 10 07:30:44 UTC 2010
>Originator:     Zhouyi Zhou
>Release:        FreeBSD 8.0
>Organization:
Institute of Computing Technology, CAS
>Environment:
FreeBSD zzy 8.0-RELEASE FreeBSD 8.0-RELEASE #137: Sun Oct  3 14:25:54 UTC 2010     root@zzy:/usr/src/sys/amd64/compile/GENERIC  amd64
>Description:
When remote debug FreeBSD 8.0 using gdb on a Intel x86_64 machine, the next command will cause kernel panic.
>How-To-Repeat:
use next command to step over a function all will cause kernel panic
for example:
Breakpoint 1, fork1 (td=0xffffff0002fce390, flags=20, pages=4, procp=0xffffff804a0afaf0) at ../../../kern/kern_fork.c:283
283             newproc = uma_zalloc(proc_zone, M_WAITOK);
(gdb) n

>Fix:
The problems is in some intel x86_64 machines, the computing the rsp from trap frame is not correct.
void
makectx(struct trapframe *tf, struct pcb *pcb)
{

    pcb->pcb_r12 = tf->tf_r12;
    pcb->pcb_r13 = tf->tf_r13;
    pcb->pcb_r14 = tf->tf_r14;
    pcb->pcb_r15 = tf->tf_r15;
    pcb->pcb_rbp = tf->tf_rbp;
    pcb->pcb_rbx = tf->tf_rbx;
    pcb->pcb_rip = tf->tf_rip;
    pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8;
}

But according to <Intel 64 and IA-32 Architecutres Software Developer's Manual Volume 3A: System Programming Guide, Part 1>  section 5.14.2: (64-bit mode also pushes SS:RSP unconditionally, rather than only on a CPL change).

So the function makectx in sys/amd64/amd64/machdep.c should be modified as
void
makectx(struct trapframe *tf, struct pcb *pcb)
{

    pcb->pcb_r12 = tf->tf_r12;
    pcb->pcb_r13 = tf->tf_r13;
    pcb->pcb_r14 = tf->tf_r14;
    pcb->pcb_r15 = tf->tf_r15;
    pcb->pcb_rbp = tf->tf_rbp;
    pcb->pcb_rbx = tf->tf_rbx;
    pcb->pcb_rip = tf->tf_rip;
    pcb->pcb_rsp = tf->tf_rsp;
}

>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/151167: commit references a PR
Date: Sun,  3 Oct 2010 13:52:22 +0000 (UTC)

 Author: kib
 Date: Sun Oct  3 13:52:17 2010
 New Revision: 213382
 URL: http://svn.freebsd.org/changeset/base/213382
 
 Log:
   The makectx() function, used by kdb_trap() to reconstruct pcb from
   trap frame when trap initiated kdb entry, incorrectly calculated the
   value of %rsp for trapped thread.
   
   According to Intel(R) 64 and IA-32 Architectures Software Developer's Manual
   Volume 3A: System Programming Guide, Part 1, rev. 035, 6.14.2 64-Bit Mode
   Stack Frame, "64-bit mode ... pushes SS:RSP unconditionally, rather than
   only on a CPL change."
   Even assuming the conditional push of the %ss:%rsp, the calculation
   was still wrong because sizeof(tf_ss) + sizeof(tf_rsp) == 16 on amd64.
   
   Always use the tf_rsp from trap frame. The change supposedly fixes
   stepping when using kgdb backend for kdb.
   
   Submitted by:	Zhouyi Zhou <zhouzhouyi gmail com>
   PR:	amd64/151167
   Reviewed by:	avg
   MFC after:	1 week
 
 Modified:
   head/sys/amd64/amd64/machdep.c
 
 Modified: head/sys/amd64/amd64/machdep.c
 ==============================================================================
 --- head/sys/amd64/amd64/machdep.c	Sun Oct  3 13:13:10 2010	(r213381)
 +++ head/sys/amd64/amd64/machdep.c	Sun Oct  3 13:52:17 2010	(r213382)
 @@ -1799,7 +1799,7 @@ makectx(struct trapframe *tf, struct pcb
  	pcb->pcb_rbp = tf->tf_rbp;
  	pcb->pcb_rbx = tf->tf_rbx;
  	pcb->pcb_rip = tf->tf_rip;
 -	pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8;
 +	pcb->pcb_rsp = tf->tf_rsp;
  }
  
  int
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: kib 
State-Changed-When: Fri Oct 8 09:19:37 UTC 2010 
State-Changed-Why:  
Take. 


Responsible-Changed-From-To: freebsd-amd64->kib 
Responsible-Changed-By: kib 
Responsible-Changed-When: Fri Oct 8 09:19:37 UTC 2010 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=151167 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/151167: commit references a PR
Date: Sun, 10 Oct 2010 07:07:27 +0000 (UTC)

 Author: kib
 Date: Sun Oct 10 07:07:21 2010
 New Revision: 213665
 URL: http://svn.freebsd.org/changeset/base/213665
 
 Log:
   MFC r213382:
   In makectx(), always use the tf_rsp from trap frame. %rsp is pushed
   unconditionally by hardware on the trap.
   
   PR:   amd64/151167
 
 Modified:
   stable/8/sys/amd64/amd64/machdep.c
 Directory Properties:
   stable/8/sys/   (props changed)
   stable/8/sys/amd64/include/xen/   (props changed)
   stable/8/sys/cddl/contrib/opensolaris/   (props changed)
   stable/8/sys/contrib/dev/acpica/   (props changed)
   stable/8/sys/contrib/pf/   (props changed)
   stable/8/sys/dev/xen/xenpci/   (props changed)
 
 Modified: stable/8/sys/amd64/amd64/machdep.c
 ==============================================================================
 --- stable/8/sys/amd64/amd64/machdep.c	Sun Oct 10 07:05:47 2010	(r213664)
 +++ stable/8/sys/amd64/amd64/machdep.c	Sun Oct 10 07:07:21 2010	(r213665)
 @@ -1795,7 +1795,7 @@ makectx(struct trapframe *tf, struct pcb
  	pcb->pcb_rbp = tf->tf_rbp;
  	pcb->pcb_rbx = tf->tf_rbx;
  	pcb->pcb_rip = tf->tf_rip;
 -	pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8;
 +	pcb->pcb_rsp = tf->tf_rsp;
  }
  
  int
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/151167: commit references a PR
Date: Sun, 10 Oct 2010 07:29:06 +0000 (UTC)

 Author: kib
 Date: Sun Oct 10 07:28:56 2010
 New Revision: 213666
 URL: http://svn.freebsd.org/changeset/base/213666
 
 Log:
   MFC r213382:
   In makectx(), always use the tf_rsp from trap frame. %rsp is pushed
   unconditionally by hardware on the trap.
   
   PR:   amd64/151167
 
 Modified:
   stable/7/sys/amd64/amd64/machdep.c
 Directory Properties:
   stable/7/sys/   (props changed)
   stable/7/sys/cddl/contrib/opensolaris/   (props changed)
   stable/7/sys/contrib/dev/acpica/   (props changed)
   stable/7/sys/contrib/pf/   (props changed)
 
 Modified: stable/7/sys/amd64/amd64/machdep.c
 ==============================================================================
 --- stable/7/sys/amd64/amd64/machdep.c	Sun Oct 10 07:07:21 2010	(r213665)
 +++ stable/7/sys/amd64/amd64/machdep.c	Sun Oct 10 07:28:56 2010	(r213666)
 @@ -1443,7 +1443,7 @@ makectx(struct trapframe *tf, struct pcb
  	pcb->pcb_rbp = tf->tf_rbp;
  	pcb->pcb_rbx = tf->tf_rbx;
  	pcb->pcb_rip = tf->tf_rip;
 -	pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8;
 +	pcb->pcb_rsp = tf->tf_rsp;
  }
  
  int
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: kib 
State-Changed-When: Sun Oct 10 07:30:09 UTC 2010 
State-Changed-Why:  
Committed to stable/7 and stable/8. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=151167 
>Unformatted:
