Subject: devel/unproven/pthreads/machdep files for Alpha
To: None <port-alpha@netbsd.org>
From: Olaf Seibert <rhialto@polderland.nl>
List: port-alpha
Date: 08/08/2000 22:58:51
I cobbled together these files for the unproven-pthreads package, which
currently does not compile on Alpha. 
For most changes, I simply followed the example from other machdep/*
files. I submit them here for review, in case I did something stupid. 

I used this to create a thread-enabled version of Python, which is in
turn needed for Mojo Nation (www.mojonation.net). Unfortunately, some
SIGVTALRM is coming from somewhere, reported by someone, and causing an
abort of Python, under some (as yet not exactly pinpointed)
circumstances when fork()/exec()ing a make. Approximately the same
happened with mit-pthreads, except that with those I also got a core
dump.

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	engine-alpha-netbsd-1.3.c
#	engine-alpha-netbsd-1.3.h
#	syscall-alpha-netbsd-1.3.S
#	syscall-template-alpha-netbsd-1.3.S
#
echo x - engine-alpha-netbsd-1.3.c
sed 's/^X//' >engine-alpha-netbsd-1.3.c << 'END-of-engine-alpha-netbsd-1.3.c'
X/* ==== machdep.c ============================================================
X * Copyright (c) 1993, 1994 Chris Provenzano, proven@athena.mit.edu
X *
X * Description : Machine dependent functions for NetBSD/Alpha 1.1(+)
X *
X *	1.00 93/08/04 proven
X *      -Started coding this file.
X *
X *	95/04/22 cgd
X *	-Modified to make it go with NetBSD/Alpha
X */
X
X#ifndef lint
Xstatic const char rcsid[] = "engine-alpha-osf1.c,v 1.4.4.1 1995/12/13 05:41:37 proven Exp";
X#endif
X 
X#include <pthread.h>
X#include <sys/types.h>
X#include <sys/socket.h>
X#include <sys/syscall.h>
X#include <stdlib.h>
X#include <fcntl.h>
X#include <stdio.h>
X
X/* ==========================================================================
X * machdep_pthread_start()
X */
Xvoid machdep_pthread_start(void)
X{
X	context_switch_done();
X	pthread_sched_resume ();
X
X	/* XXXMLG
X	 * This is EXTREMELY bogus, but it seems that this function is called
X	 * with the pthread kernel locked.  If this happens, __errno() will
X	 * return the wrong address until after the first context switch.
X	 *
X	 * Clearly there is a leak of pthread_kernel somewhere, but until
X	 * it is found, we force a context switch here, just before calling
X	 * the thread start routine.  When we return from pthread_yield
X	 * the kernel will be unlocked.
X	 */
X	pthread_yield();
X
X    /* Run current threads start routine with argument */
X    pthread_exit(pthread_run->machdep_data.start_routine
X      (pthread_run->machdep_data.start_argument));
X
X    /* should never reach here */
X    PANIC("setitimer failed");
X}
X
X/* ==========================================================================
X * __machdep_pthread_create()
X */
Xvoid __machdep_pthread_create(struct machdep_pthread *machdep_pthread,
X  void *(* start_routine)(void *), void *start_argument, 
X  long stack_size, long nsec, long flags)
X{
X    machdep_pthread->start_routine = start_routine;
X    machdep_pthread->start_argument = start_argument;
X
X    machdep_pthread->machdep_timer.it_value.tv_sec = 0;
X    machdep_pthread->machdep_timer.it_interval.tv_sec = 0;
X    machdep_pthread->machdep_timer.it_interval.tv_usec = 0;
X    machdep_pthread->machdep_timer.it_value.tv_usec = nsec / 1000;
X
X    /* Set up new stack frame so that it looks like it returned from a
X       longjmp() to the beginning of machdep_pthread_start().  */
X    machdep_pthread->machdep_istate[8/*ISTATE_RA*/] = 0;
X    machdep_pthread->machdep_istate[0/*ISTATE_PC*/] = (long)machdep_pthread_start;
X    machdep_pthread->machdep_istate[10/*ISTATE_PV*/] = (long)machdep_pthread_start;
X
X    /* Alpha stack starts high and builds down. */
X    {
X      long stk_addr = (long) machdep_pthread->machdep_stack;
X      stk_addr += stack_size - 1024;
X      stk_addr &= ~15;
X      machdep_pthread->machdep_istate[9/*ISTATE_SP*/] = stk_addr;
X    }
X}
X
X/* ==========================================================================
X * machdep_save_state()
X */
Xint machdep_save_state(void)
X{
X  return __machdep_save_int_state(pthread_run->machdep_data.machdep_istate);
X}
X
Xvoid machdep_restore_state(void)
X{
X  __machdep_restore_int_state(pthread_run->machdep_data.machdep_istate);
X}
X
Xvoid machdep_save_float_state (struct pthread *pthread)
X{
X  __machdep_save_fp_state(pthread->machdep_data.machdep_fstate);
X}
X
Xvoid machdep_restore_float_state (void)
X{
X  __machdep_restore_fp_state(pthread_run->machdep_data.machdep_fstate);
X}
X
X/* ==========================================================================
X * machdep_set_thread_timer()
X */
Xvoid machdep_set_thread_timer(struct machdep_pthread *machdep_pthread)
X{
X    if (setitimer(ITIMER_VIRTUAL, &(machdep_pthread->machdep_timer), NULL)) {
X        PANIC("setitimer failed");
X    }
X}
X
X/* ==========================================================================
X * machdep_unset_thread_timer()
X */
Xvoid machdep_unset_thread_timer(struct machdep_pthread *machdep_pthread)
X{
X    struct itimerval zeroval = { { 0, 0 }, { 0, 0} };
X
X    if (setitimer(ITIMER_VIRTUAL, &zeroval, NULL)) {
X        PANIC("setitimer failed");
X    }
X}
X
X/* ==========================================================================
X * machdep_pthread_cleanup()
X */
Xvoid *machdep_pthread_cleanup(struct machdep_pthread *machdep_pthread)
X{
X    return(machdep_pthread->machdep_stack);
X}
X
Xint safe_store (int *loc, int new)
X{
X  int locked, old;
X  asm ("mb" : : : "memory");
X  do {
X    asm ("ldl_l %0,%1" : "=r" (old) : "m" (*loc));
X    asm ("stl_c %0,%1" : "=r" (locked), "=m" (*loc) : "0" (new));
X  } while (!locked);
X  asm ("mb" : : : "memory");
X  return old;
X}
X
END-of-engine-alpha-netbsd-1.3.c
echo x - engine-alpha-netbsd-1.3.h
sed 's/^X//' >engine-alpha-netbsd-1.3.h << 'END-of-engine-alpha-netbsd-1.3.h'
X/* ==== machdep.h ============================================================
X * Copyright (c) 1994 Chris Provenzano (proven@athena.mit.edu) and
X * Ken Raeburn (raeburn@mit.edu).
X *
X * engine-alpha-osf1.h,v 1.4.4.1 1995/12/13 05:41:42 proven Exp
X *
X */
X
X#include <unistd.h>
X#include <setjmp.h>
X#include <sys/time.h>
X#include <sys/cdefs.h>
X#include <sys/signal.h>	/* for _NSIG */
X#include <sys/syscall.h>
X
X#include <unistd.h>
X#include <setjmp.h>
X#include <stdlib.h>
X#include <fcntl.h>
X
X#include <pthread/sysfunc.h>
X
X/* The first machine dependent functions are the SEMAPHORES needing
X   the test and set instruction.
X
X   On the Alpha, the actual values here are irrelevant; they just have
X   to be different.  */
X#define SEMAPHORE_CLEAR 0
X#define SEMAPHORE_SET   1
X
X#if 0
X#define SEMAPHORE_TEST_AND_SET(lock)    			\
X({ int *_sem_lock = (lock), locked, old;			\
X   asm ("mb" : : : "memory");					\
X   do { asm ("ldl_l %0,%1" : "=r" (old) : "m" (*_sem_lock));	\
X	/* ?? if (old != SEMAPHORE_CLEAR) break; */		\
X	asm ("stl_c %0,%1" : "=r" (locked), "=m" (*_sem_lock)	\
X			   : "0" (SEMAPHORE_SET));		\
X   } while (!locked);						\
X   asm ("mb" : : : "memory");					\
X   old == SEMAPHORE_CLEAR; })
X
X#define SEMAPHORE_RESET(lock)           \
X({ int *_sem_lock = (lock);		\
X   *_sem_lock = SEMAPHORE_CLEAR;	\
X   asm ("mb" : : : "memory"); })
X#endif
X
X/*
X * New types
X */
Xtypedef int semaphore;
X
X/*
X * sigset_t macros
X */
X#define	SIG_ANY(sig)		(sig)
X#define	SIGMAX			(_NSIG-1)
X
X/*
X * New Strutures
X */
Xstruct machdep_pthread {
X    void        		*(*start_routine)(void *);
X    void        		*start_argument;
X    void        		*machdep_stack;
X    struct itimerval		machdep_timer;
X    unsigned long		machdep_istate[11];
X    unsigned long     		machdep_fstate[9];
X};
X
X/*
X * Static machdep_pthread initialization values.
X * For initial thread only.
X */
X#define MACHDEP_PTHREAD_INIT    \
X	{ NULL, NULL, NULL, { { 0, 0 }, { 0, 100000 } }, { 0 }, { 0 } }
X
X/*
X * Minimum stack size
X */
X#define PTHREAD_STACK_MIN	2048
X
X/*
X * Some fd flag defines that are necessary to distinguish between posix
X * behavior and bsd4.3 behavior.
X */
X#define __FD_NONBLOCK 		O_NONBLOCK
X
X/*
X * New functions
X */
X
X__BEGIN_DECLS
X
X#if defined(PTHREAD_KERNEL)
X
X#define __machdep_stack_get(x)      (x)->machdep_stack
X#define __machdep_stack_set(x, y)   (x)->machdep_stack = y
X#define __machdep_stack_repl(x, y)                          \
X{                                                           \
X    if ((stack = __machdep_stack_get(x))) {                 \
X        __machdep_stack_free(stack);                        \
X    }                                                       \
X    __machdep_stack_set(x, y);                              \
X}
X
Xint machdep_save_state(void);
Xint safe_store (int *loc, int nw);
X
Xint __machdep_save_int_state(unsigned long *);
Xvoid __machdep_restore_int_state(unsigned long *);
Xvoid __machdep_save_fp_state(unsigned long *);
Xvoid __machdep_restore_fp_state(unsigned long *);
X
Xvoid *machdep_pthread_cleanup(struct machdep_pthread *machdep_pthread);
Xvoid machdep_pthread_start(void);
X
X/* ==========================================================================
X * __machdep_stack_free()
X */
Xinline static
Xvoid __machdep_stack_free(void * stack)
X{
X    free(stack);
X}
X
X/* ==========================================================================
X * __machdep_stack_alloc()
X */
Xinline static
Xvoid * __machdep_stack_alloc(size_t size)
X{
X    return(malloc(size));
X}
X
X/* ==========================================================================
X * machdep_sys_creat()
X */
Xstatic inline int
Xmachdep_sys_creat(char * path, int mode)
X{   
X        return(machdep_sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
X}   
X
X/* ==========================================================================
X * machdep_sys_wait3()
X */  
Xstatic inline int
Xmachdep_sys_wait3(int * b, int c, struct rusage *d)
X{    
X        return(machdep_sys_wait4(0, b, c, d));
X}   
X 
X/* ==========================================================================
X * machdep_sys_waitpid()
X */   
Xstatic inline int
Xmachdep_sys_waitpid(int a, int * b, int c)
X{
X        return(machdep_sys_wait4(a, b, c, NULL));
X}
X
X/* ==========================================================================
X * machdep_sys_getdtablesize()
X */
Xstatic inline int
Xmachdep_sys_getdtablesize(void)
X{
X        return(sysconf(_SC_OPEN_MAX));
X}
X
X/* ==========================================================================
X * machdep_sys_lseek()
X */
Xstatic inline
Xoff_t machdep_sys_lseek(int fd, off_t offset, int whence)
X{
X	return(__syscall((quad_t)SYS_lseek, fd, 0, offset, whence));
X}
X
Xstatic inline
Xint machdep_sys_ftruncate( int fd, off_t length)
X{
X	quad_t q;
X	int rv;
X
X	q = __syscall((quad_t)SYS_ftruncate, fd,0, length);
X	if( /* LINTED constant */ sizeof( quad_t ) == sizeof( register_t ) ||
X	    /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN )
X		rv = (int)q;
X	else
X		rv = (int)((u_quad_t)q >> 32);
X
X	return rv;
X}
X
X
X/* ==========================================================================
X * machdep_sys_getdirentries()
X */
Xstatic inline int
Xmachdep_sys_getdirentries(int fd, char * buf, int len, int * seek)
X{
X        return(machdep_sys_getdents(fd, buf, len));
X}  
X
X
X#endif
X
X__END_DECLS
END-of-engine-alpha-netbsd-1.3.h
echo x - syscall-alpha-netbsd-1.3.S
sed 's/^X//' >syscall-alpha-netbsd-1.3.S << 'END-of-syscall-alpha-netbsd-1.3.S'
X#include <machine/asm.h>
X#define	CHMK()		call_pal 0x83
X#define COMPAT_43
X#include <sys/syscall.h>
X#ifndef __CONCAT
X#include <sys/cdefs.h>
X#endif
X#define	CONCAT		__CONCAT
X
X#undef SYSCALL
X
X/* Kernel syscall interface:
X   Input:
X	v0 - system call number
X	a* - arguments, as in C
X   Output:
X	a3 - zero iff successful
X	v0 - errno value on failure, else result
X
X   This macro is similar to SYSCALL in asm.h, but not completely.
X   There's room for optimization, if we assume this will continue to
X   be assembled as one file.
X
X   This macro expansions does not include the return instruction.
X   If there's no other work to be done, use something like:
X	SYSCALL(foo) ; ret
X   If there is other work to do (in fork, maybe?), do it after the
X   SYSCALL invocation.  */
X
X#define SYSCALL(x) \
X	.align	4					;\
X	.globl	CONCAT(machdep_sys_,x)			;\
X	.ent	CONCAT(machdep_sys_,x), 0		;\
XCONCAT(machdep_sys_,x):					;\
X	.frame	sp,0,ra					;\
X	ldiq	v0, CONCAT(SYS_,x)			;\
X	CHMK()						;\
X	beq	a3, CONCAT(Lsys_noerr_,x)		;\
X	br	gp, CONCAT(Lsys_err_,x)			;\
XCONCAT(Lsys_err_,x):					;\
X	/* Load gp so we can find cerror to jump to.  */;\
X	ldgp	gp, 0(gp)				;\
X	jmp	zero, machdep_cerror			;\
XCONCAT(Lsys_noerr_,x):
X
X#define XSYSCALL(x)	SYSCALL(x) ; RET ; .end CONCAT(machdep_sys_,x)
X
X	.globl	machdep_cerror 
Xmachdep_cerror:
X	br	t0, Lmachdep_cerror_setgp
XLmachdep_cerror_setgp:
X	ldgp	gp, 0(t0)
X	stl	v0, errno
X#if 0
X	ldiq	v0, -1
X#else
X	subq	zero, v0, v0
X#endif
X	RET
X
X/* The fork system call is special...  */
XSYSCALL(fork)
X	cmovne	a4, 0, v0
X	RET
X	.end	machdep_sys_fork
X
X/* The pipe system call is special... */
XSYSCALL(pipe)
X	stl	v0, 0(a0)
X	stl	a4, 4(a0)
X	mov	zero, v0
X	RET
X	.end	machdep_sys_pipe
X
X#ifndef SYS___sigsuspend14
X/* The sigsuspend system call is special... */
X	.align 4
X	.globl machdep_sys_sigsuspend
X	.ent machdep_sys_sigsuspend, 0
Xmachdep_sys_sigsuspend:
X	ldl	a0, 0(a0)		/* pass *mask instead of mask */
X	ldiq	v0, SYS_sigsuspend
X	CHMK()
X	mov	zero, v0		/* shouldn't need; just in case... */
X	RET
X	.end 	machdep_sys_sigsuspend
X#endif /* SYS_sigsuspend14 */
X
X#ifndef SYS___sigprocmask14
X/* The sigprocmask system call is special... */
X	.align 4
X	.globl machdep_sys_sigprocmask
X	.ent machdep_sys_sigprocmask, 0
Xmachdep_sys_sigprocmask:
X	mov	a2, a5			/* safe */
X	cmoveq	a1, 1, a0		/* if set == NULL, how = SIG_BLOCK */
X	beq	a1, Ldoit		/* and set = 0, and do it. */
X	ldl	a1, 0(a1)		/* load the set from *set */
XLdoit:	ldiq	v0, SYS_sigprocmask
X	CHMK()
X	beq	a5, Lret		/* if they don't want old mask, done */	
X	stl	v0, 0(a5)		/* otherwise, give it to them. */
XLret:	mov	zero, v0
X	RET
X	.end	machdep_sys_sigprocmask
X#endif /* SYS_sigprocmask14 */
X
X/* More stuff ... */
X	.align 4
X	.global __machdep_save_int_state
X	.ent __machdep_save_int_state, 0
X__machdep_save_int_state:
X	.frame	sp, 16, ra
X	ldgp	gp, 0(t12)
X	lda	sp, -16(sp)
X	stq	ra, 0(sp)
X
X	/* save integer registers */
X	stq	ra, ( 0 * 8)(a0)		/* return address */
X	stq	s0, ( 1 * 8)(a0)		/* callee-saved registers */
X	stq	s1, ( 2 * 8)(a0)
X	stq	s2, ( 3 * 8)(a0)
X	stq	s3, ( 4 * 8)(a0)
X	stq	s4, ( 5 * 8)(a0)
X	stq	s5, ( 6 * 8)(a0)
X	stq	s6, ( 7 * 8)(a0)
X	stq	sp, ( 9 * 8)(a0)
X	stq	ra, ( 8 * 8)(a0)		/* RA on return */
X	stq	pv, (10 * 8)(a0)		/* and PV; we restore it */
X
X	mov	zero, v0
X	lda	sp, 16(sp)
X	RET
X	.end __machdep_save_int_state
X
X	.align 4
X	.global __machdep_restore_int_state
X	.ent __machdep_restore_int_state, 0
X__machdep_restore_int_state:
X	.frame	sp, 16, ra
X	ldgp	gp, 0(t12)
X	lda	sp, -16(sp)
X	stq	ra, 0(sp)
X
X	/* restore integer registers */
X	ldq	t0, ( 0 * 8)(a0)		/* return address */
X	ldq	s0, ( 1 * 8)(a0)		/* callee-saved registers */
X	ldq	s1, ( 2 * 8)(a0)
X	ldq	s2, ( 3 * 8)(a0)
X	ldq	s3, ( 4 * 8)(a0)
X	ldq	s4, ( 5 * 8)(a0)
X	ldq	s5, ( 6 * 8)(a0)
X	ldq	s6, ( 7 * 8)(a0)
X	ldq	ra, ( 8 * 8)(a0)		/* RA after return */
X	ldq	sp, ( 9 * 8)(a0)
X	ldq	pv, (10 * 8)(a0)		/* and PV; we restore it */
X
X	ldiq    v0, 1
X	ret     zero,(t0),1
X	.end __machdep_restore_int_state
X
X	.align 4
X	.global __machdep_save_fp_state
X	.ent __machdep_save_fp_state, 0
X__machdep_save_fp_state:
X	.frame	sp, 16, ra
X	ldgp	gp, 0(t12)
X	lda	sp, -16(sp)
X	stq	ra, 0(sp)
X
X	/* save FP registers */
X	stt	fs0, (0 * 8)(a0)		/* callee-saved registers */
X	stt	fs1, (1 * 8)(a0)
X	stt	fs2, (2 * 8)(a0)
X	stt	fs3, (3 * 8)(a0)
X	stt	fs4, (4 * 8)(a0)
X	stt	fs5, (5 * 8)(a0)
X	stt	fs6, (6 * 8)(a0)
X	stt	fs7, (7 * 8)(a0)
X	mf_fpcr ft0 				/* and FP control reg */
X	stt	ft0, (8 * 8)(a0)
X
X	lda	sp, 16(sp)
X	RET
X	.end __machdep_save_fp_state
X
X	.align 4
X	.global __machdep_restore_fp_state
X	.ent __machdep_restore_fp_state, 0
X__machdep_restore_fp_state:
X	.frame	sp, 16, ra
X	ldgp	gp, 0(t12)
X	lda	sp, -16(sp)
X	stq	ra, 0(sp)
X
X	/* restore FP registers */
X	ldt	fs0, (0 * 8)(a0)		/* callee-saved registers */
X	ldt	fs1, (1 * 8)(a0)
X	ldt	fs2, (2 * 8)(a0)
X	ldt	fs3, (3 * 8)(a0)
X	ldt	fs4, (4 * 8)(a0)
X	ldt	fs5, (5 * 8)(a0)
X	ldt	fs6, (6 * 8)(a0)
X	ldt	fs7, (7 * 8)(a0)
X	ldt	ft0, (8 * 8)(a0)
X	mt_fpcr ft0 				/* and FP control reg */
X
X	lda	sp, 16(sp)
X	RET
X	.end __machdep_restore_fp_state
X
X/* For fstat() we actually syscall fstat13. */
X	.align	4
X	.globl	machdep_sys_fstat
X	.ent	machdep_sys_fstat, 0
Xmachdep_sys_fstat:
X	.frame	sp,0,ra
X	ldiq	v0, SYS___fstat13
X	CHMK()
X	beq	a3, Lsys_noerr_fstat
X	br	gp, Lsys_err_fstat
XLsys_err_fstat:
X	/* Load gp so we can find cerror to jump to.  */
X	ldgp	gp, 0(gp)
X	jmp	zero, machdep_cerror
XLsys_noerr_fstat:
X	RET
X	.end machdep_sys_fstat
END-of-syscall-alpha-netbsd-1.3.S
echo x - syscall-template-alpha-netbsd-1.3.S
sed 's/^X//' >syscall-template-alpha-netbsd-1.3.S << 'END-of-syscall-template-alpha-netbsd-1.3.S'
X#include <machine/asm.h>
X#define COMPAT_43
X#include <sys/syscall.h>
X#define CHMK()		call_pal 0x83
X
X#ifdef SYS___sigsuspend14
X#define SYS_sigsuspend SYS___sigsuspend14
X#endif
X
X#ifdef SYS___sigaction14
X#define SYS_sigaction SYS___sigaction14
X#endif
X
X#ifdef SYS___sigprocmask14
X#define SYS_sigprocmask SYS___sigprocmask14
X#endif
X
X#undef SYSCALL
X
X/* Kernel syscall interface:
X   Input:
X	v0 - system call number
X	a* - arguments, as in C
X   Output:
X	a3 - zero iff successful
X	v0 - errno value on failure, else result
X
X   This macro is similar to SYSCALL in asm.h, but not completely.
X   There's room for optimization, if we assume this will continue to
X   be assembled as one file.
X
X   This macro expansions does not include the return instruction.
X   If there's no other work to be done, use something like:
X	SYSCALL(foo) ; ret
X   If there is other work to do (in fork, maybe?), do it after the
X   SYSCALL invocation.  */
X
X#define SYSCALL(x) \
X	.align	4					;\
X	.globl	machdep_sys_##x				;\
X	.ent	machdep_sys_##x, 0			;\
Xmachdep_sys_##x:					;\
X	.frame	sp,0,ra					;\
X	ldiq	v0, SYS_##x				;\
X	CHMK()						;\
X	beq	a3, Lsys_noerr_##x			;\
X	br	gp, Lsys_err_##x			;\
XLsys_err_##x:						;\
X	/* Load gp so we can find cerror to jump to.  */;\
X	ldgp	gp, 0(gp)				;\
X	jmp	zero, machdep_cerror			;\
XLsys_noerr_##x:
X
X#define SIMPLE_SYSCALL(x)	SYSCALL(x) ; ret ; .end machdep_sys_##x
X
X#define XSYSCALL(x)	SIMPLE_SYSCALL(x)
X
XXSYSCALL(SYSCALL_NAME)
END-of-syscall-template-alpha-netbsd-1.3.S
exit

-Olaf.
-- 
___ Olaf 'Rhialto' Seibert - rhialto@polder   -- Ah only did well at school
\X/ land.nl      -- tae git intae an O level class tae git away fae Begbie.