Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 08 Sep 2004 15:33:23 +0400
From:      Andrew Belashov <bel@orel.ru>
To:        freebsd-sparc64@freebsd.org
Subject:   libkse
Message-ID:  <413EEE03.2030904@orel.ru>

next in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigF7855C20816B3F65024CFCEE
Content-Type: multipart/mixed;
 boundary="------------060403050407010607090508"

This is a multi-part message in MIME format.
--------------060403050407010607090508
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hello, All!

I long time work on libkse library for FreeBSD/sparc64. Some work is done.
Recently I have found a bug in kernel. Bug in sys/sparc64/sparc64/vm_machdep.c:

Thread constructor (void cpu_thread_setup(struct thread *td)) does not
zeroing number of frames, saved in pcb.

I offer a patch for testing and cleanup.

<http://www.orel.ru/~bel/patches/libkse.patch>;

NOTE: Patch fixing kernel bug. It is necessary rebuild the kernel.
It is also necessary rebuild and reinstall libkse library:

==========================================================================
# cd /usr/obj/usr/src/sys/GENERIC
# make && make install
# cd /usr/src/lib/libpthread
# make obj && make depend && make && make install
# ldconfig
==========================================================================

NOTE: default pthread library for sparc64 is libc_r. For libkse, use -lkse
option for linking or use libmap.conf(5).

Here is log of simple tests:

==========================================================================
$ cd /usr/src/lib/libpthread/test
$ make LIBS="-lkse"
Test static library:
--------------------------------------------------------------------------
Test                                      c_user c_system c_total     chng
  passed/FAILED                            h_user h_system h_total   % chng
--------------------------------------------------------------------------
hello_d                                     0.02     0.00    0.02
  passed
--------------------------------------------------------------------------
hello_s                                     0.00     0.02    0.02
  passed
--------------------------------------------------------------------------
join_leak_d                                 0.33     0.03    0.36
  *** FAILED ***
--------------------------------------------------------------------------
mutex_d                                    18.48   101.69  120.17
  passed
--------------------------------------------------------------------------
sem_d                                       0.00     0.03    0.03
  *** FAILED ***
--------------------------------------------------------------------------
sigsuspend_d                                0.02     0.01    0.03
  *** FAILED ***
--------------------------------------------------------------------------
sigwait_d                                   0.01     0.02    0.02
  *** FAILED ***
--------------------------------------------------------------------------
guard_s.pl                                  0.65     1.49    2.14
  *** FAILED *** (30/30 failed)
--------------------------------------------------------------------------
propagate_s.pl                              0.58     0.11    0.69
  *** FAILED *** (1/1 failed)
--------------------------------------------------------------------------
Totals                                     18.50   101.71  120.21     0.00
  3 / 9 passed (33.33%)                      0.00     0.00    0.00    0.00%
--------------------------------------------------------------------------
*** Error code 1

Stop in /usr/src/lib/libpthread/test.
==========================================================================

My TODO list:
o Floating Point registers store/restore in _thr_[gs]etcontext.

--
With best regards,
Andrew Belashov.

--------------060403050407010607090508
Content-Type: text/plain;
 name="libkse.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="libkse.patch"

--- ./sys/sparc64/sparc64/vm_machdep.c.orig	Thu Aug  5 09:42:43 2004
+++ ./sys/sparc64/sparc64/vm_machdep.c	Wed Sep  8 09:08:43 2004
@@ -141,6 +141,7 @@
 
 	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
 	    sizeof(struct pcb)) & ~0x3fUL);
+	pcb->pcb_nsaved = 0;
 	td->td_frame = (struct trapframe *)pcb - 1;
 	td->td_pcb = pcb;
 }
--- ./lib/libpthread/arch/sparc64/include/pthread_md.h.orig	Thu Aug  5 09:29:06 2004
+++ ./lib/libpthread/arch/sparc64/include/pthread_md.h	Wed Sep  8 10:28:46 2004
@@ -191,7 +191,10 @@
 static __inline struct pthread *
 _get_curthread(void)
 {
-	return (_tcb->tcb_thread);
+	if (_tcb != NULL)
+		return (_tcb->tcb_thread);
+	else
+		return (NULL);
 }
 
 /*
@@ -230,8 +233,13 @@
 	extern int _libkse_debug;
 	mcontext_t *mc;
 
+	if ((kcb == NULL) || (tcb == NULL))
+		return (-1);
 	_tcb_set(kcb, tcb);
 	mc = &tcb->tcb_tmbx.tm_context.uc_mcontext;
+
+	/* XXX Setup new tcb_tp pointer in machine context */
+	mc->mc_global[6] = (uint64_t)_tp;
 	if (_libkse_debug == 0) {
 		tcb->tcb_tmbx.tm_lwp = kcb->kcb_kmbx.km_lwp;
 		if (setmbox)
--- ./lib/libpthread/arch/sparc64/sparc64/thr_getcontext.S.orig	Mon Aug  9 12:20:25 2004
+++ ./lib/libpthread/arch/sparc64/sparc64/thr_getcontext.S	Wed Sep  8 11:30:27 2004
@@ -27,24 +27,64 @@
 #include <machine/asm.h>
 __FBSDID("$FreeBSD: src/lib/libpthread/arch/sparc64/sparc64/thr_getcontext.S,v 1.3 2003/10/09 20:52:17 deischen Exp $");
 
+#include <machine/fsr.h>
+#include <machine/tstate.h>
+
 #include "assym.s"
 
+/*
+ * int thr_getcontext(mcontext_t *mcp);
+ *
+ * Returns -1 if there is an error, 0 no errors; 1 upon return
+ * from a setcontext().
+ */
 	.weak	CNAME(_thr_getcontext)
 	.set	CNAME(_thr_getcontext),CNAME(__thr_getcontext)
 ENTRY(__thr_getcontext)
-	add	%o7, 8, %o1
-	add	%o1, 4, %o2
-	stx	%sp, [%o0 + MC_OUT + (6 * 8)]
-	stx	%o1, [%o0 + MC_TPC]
-	stx	%o2, [%o0 + MC_TNPC]
-	mov	MC_VALID_FLAGS, %l0		/* Validate the context. */
-	stx	%l0, [%o0 + MC_FLAGS]
-	mov	1, %l0
-	stx	%l0, [%o0 + MC_OUT + (0 * 8)]	/* return 1 when resumed */
-	retl
-	mov	0, %o0				/* return 0 */
+	save	%sp, -CCFSZ, %sp
+	add	%i7, 8, %i1
+	add	%i1, 4, %i2
+#if 1
+	mov	%i0, %o0
+	mov	MC_SIZEOF, %o2
+	call	memset				/* bzero(mcp, sizeof(*mcp)) */
+	 clr	%o1
+#endif
+	stx	%g6, [%i0 + MC_GLOBAL + (6 * 8)] /* Current tcb pointer */
+	stx	%g7, [%i0 + MC_GLOBAL + (7 * 8)] /* XXX %g7 Reserved? */
+	stx	%fp, [%i0 + MC_OUT + (6 * 8)]
+	stx	%i1, [%i0 + MC_TPC]
+	stx	%i2, [%i0 + MC_TNPC]
+	rd	%y, %g1				/* XXX %y deprecated */
+	stx	%g1, [%i0 + MC_Y]
+	rd	%fprs, %g1
+	stx	%g1, [%i0 + MC_FPRS]
+	wr	%g0, FPRS_FEF, %fprs		/* Enable FPU */
+	stx	%fsr, [%i0 + MC_FSR]		/* Store FPU Status Register */
+	rd	%gsr, %g2
+	stx	%g2, [%i0 + MC_GSR]		/* Store %gsr */
+	wr	%g1, 0, %fprs			/* Restore %fprs */
+	set	TSTATE_IE | TSTATE_PEF | TSTATE_CWP(0) | TSTATE_ASI(0), %g1
+	stx	%g1, [%i0 + MC_TSTATE]		/* XXX Emulated %tstate */
+	mov	T_SYSCALL, %g1
+	stx	%g1, [%i0 + MC_TYPE]		/* XXX Emulated trap type */
+	clr	[%i0 + MC_WSTATE]		/* XXX Emulated %wstate */
+
+	mov	MC_VALID_FLAGS, %i3		/* Validate the context. */
+	stx	%i3, [%i0 + MC_FLAGS]
+	mov	1, %i3
+	stx	%i3, [%i0 + MC_OUT + (0 * 8)]	/* return 1 when resumed */
+	return	%i7 + 8
+	 clr	%o0				/* return 0 */
 END(__thr_getcontext)
 
+/*
+ * int thr_setcontext(mcontext_t *mcp, intptr_t val, intptr_t *loc)
+ *
+ * Restores the context in mcp.
+ *
+ * Returns 0 if there are no errors; -1 otherwise
+ */
 	.weak	CNAME(_thr_setcontext)
 	.set	CNAME(_thr_setcontext),CNAME(__thr_setcontext)
 ENTRY(__thr_setcontext)
@@ -53,28 +93,51 @@
 	mov	%i0, %l0
 	mov	%i1, %l1
 	mov	%i2, %l2
+	ldx	[%l0 + MC_FLAGS], %l5
+	cmp	%l5, MC_VALID_FLAGS
+	be,pt	%icc, 2f			/* XXX Checking lower 32bit */
+	 nop
+	return	%i7 + 8
+	 mov	-1, %i0				/* Return -1 */
+2:
 	ldx	[%l0 + MC_GLOBAL + (1 * 8)], %g1
 	ldx	[%l0 + MC_GLOBAL + (2 * 8)], %g2
 	ldx	[%l0 + MC_GLOBAL + (3 * 8)], %g3
 	ldx	[%l0 + MC_GLOBAL + (4 * 8)], %g4
 	ldx	[%l0 + MC_GLOBAL + (5 * 8)], %g5
-	ldx	[%l0 + MC_GLOBAL + (6 * 8)], %g6
-	ldx	[%l0 + MC_GLOBAL + (7 * 8)], %g7
-	ldx	[%l0 + MC_OUT + (0 * 8)], %i0
+	ldx	[%l0 + MC_GLOBAL + (6 * 8)], %g6 /* Current tcb pointer */
+	ldx	[%l0 + MC_GLOBAL + (7 * 8)], %g7 /* XXX %g7 Reserved? */
+	ldx	[%l0 + MC_OUT + (0 * 8)], %i0	/* Return value */
 	ldx	[%l0 + MC_OUT + (1 * 8)], %i1
 	ldx     [%l0 + MC_OUT + (2 * 8)], %i2
 	ldx	[%l0 + MC_OUT + (3 * 8)], %i3
 	ldx	[%l0 + MC_OUT + (4 * 8)], %i4
 	ldx	[%l0 + MC_OUT + (5 * 8)], %i5
-	ldx	[%l0 + MC_OUT + (6 * 8)], %i6
+	ldx	[%l0 + MC_OUT + (6 * 8)], %fp /* %fp */
 	ldx	[%l0 + MC_OUT + (7 * 8)], %i7
+	ldx	[%l0 + MC_FPRS], %l5
+	ldx	[%l0 + MC_GSR], %l4
+	ldx	[%l0 + MC_Y], %l3
+	wr	%g0, FPRS_FEF, %fprs		/* Enable FPU */
+	ldx	[%l0 + MC_FSR], %fsr		/* Restore %fsr */
+	wr	%l4, 0, %gsr			/* Restore %gsr */
+	wr	%g0, 0, %fprs			/* Disable FPU */
+	wr	%l3, 0, %y			/* XXX %y deprecated */
+	wr	%l5, 0, %fprs			/* Restore %fprs */
 	ldx	[%l0 + MC_TPC], %l4
 	ldx	[%l0 + MC_TNPC], %l3
-	brz	%l2, 1f
-	nop
-	stx	%l1, [%l2]
-1:	jmpl	%l3, %g0
-	return	%l4
+	ldx	[%l0 + MC_TSTATE], %l5
+	brnz,a,pn	%l2, 1f
+	 stx	%l1, [%l2]			/* Set mbox */
+1:	
+	srlx	%l5, TSTATE_CCR_SHIFT, %l1
+	and	%l1, CCR_MASK, %l1
+	srlx	%l5, TSTATE_ASI_SHIFT, %l2
+	and	%l2, ASI_MASK, %l2
+	wr	%l2, 0, %asi			/* XXX Restore %asi needed? */
+	wr	%l1, 0, %ccr			/* XXX Restore %ccr needed? */
+	jmpl	%l4, %g0
+	 return	%l3
 END(__thr_setcontext)
 
 ENTRY(_sparc64_enter_uts)
@@ -83,5 +146,5 @@
 	add	%i2, %i3, %i2
 	sub	%i2, SPOFF + CCFSZ, %sp
 	jmpl	%i0, %g0
-	mov	%i1, %o0
+	 mov	%i1, %o0
 END(_sparc64_enter_uts)
--- ./lib/libpthread/arch/sparc64/sparc64/assym.s.orig	Thu Oct  9 18:48:09 2003
+++ ./lib/libpthread/arch/sparc64/sparc64/assym.s	Tue Aug 31 08:33:38 2004
@@ -11,5 +11,21 @@
 #define	MC_VALID_FLAGS	0x1
 #define	MC_GLOBAL	0x0
 #define	MC_OUT		0x40
-#define	MC_TPC		0xc8
+#define	MC_LOCAL	0x80
+#define	MC_IN		0xc0
+#define MC_FP		0x100
+#define MC_SIZEOF	512
+
+#define	MC_FPRS		0x80
+#define	MC_FSR		0x88
+#define	MC_GSR		0x90
 #define	MC_TNPC		0xc0
+#define	MC_TPC		0xc8
+#define MC_TSTATE	0xd0
+#define MC_TYPE		0xd8
+#define MC_Y		0xe0
+#define MC_WSTATE	0xe8
+
+#define CCR_MASK	0xff
+#define ASI_MASK	0xff
+#define T_SYSCALL	48

--------------060403050407010607090508--

--------------enigF7855C20816B3F65024CFCEE
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFBPu4HwF8YpH80o/IRAjgOAJ4wUfMMrcO9Sexb9kt+hvUoRv3YywCdE/+P
f/spGY816f4RqUF743656YA=
=cIdr
-----END PGP SIGNATURE-----

--------------enigF7855C20816B3F65024CFCEE--



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