/*------------------------->  ANSI C - sourcefile  <-------------------------*/
/* Copyright (C) 1992/93 by Stephen Crane                                    */
/* This file is part of the GNU Sather library. It is free software; you may */
/* redistribute  and/or modify it under the terms of the GNU Library General */
/* Public  License (LGPL)  as published  by the  Free  Software  Foundation; */
/* either version 2 of the license, or (at your option) any later version.   */
/* This  library  is distributed  in the  hope that it will  be  useful, but */
/* WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY */
/* or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       */
/* The license text is also available from:  Free Software Foundation, Inc., */
/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     */
/*------------>  Please email comments to <bug-sather@gnu.org>  <------------*/

/* This file combines sun4.c and linux.c of the original
 * lwp distribution
 */
#ifdef __linux__
/*
 * linux.c -- lightweight process initialisation for linux
 *
 * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
 * Imperial College of Science, Technology and Medicine, 180 Queen's
 * Gate, London SW7 2BZ, England.
 *
 * Modified to run on 386/486 machines running linux by:
 * Mark Little (M.C.Little@newcastle.ac.uk), Department of Computing
 * Science, The University, Newcastle upon Tyne, NE1 7RU, England.
 * 7/7/93
 */
#include <sys/param.h>

#include "lwp.h"

/*
 * initp -- initialise a new process's context.
 */
void initp (volatile struct pcb *volatile newp, void *sp)
{
/* Nobbi, Sep 11, 99: jmp_buf has new structure as of Linux 2.2.11
	newp->context->__sp = sp;
	newp->context->__bp = sp;
	newp->context->__pc = (void *)wrapp;
*/
	newp->context->__jmpbuf[JB_SP] = (int)sp;
	newp->context->__jmpbuf[JB_BP] = (int)sp;
	newp->context->__jmpbuf[JB_PC] = (int)wrapp;
	newp->context->__mask_was_saved = 0;
}

/*
 * getdtablesize -- linux hasn't got this
 */
int getdtablesize (void)
{
	return (NOFILE);
}

#elif defined(__CYGWIN32__)

#include <sys/param.h>

#include "lwp.h"

/*
 * initp -- initialise a new process's context.
 */
void initp (volatile struct pcb *volatile newp, void *sp)
{
       newp->context->esp = (int)sp;
       newp->context->ebp = (int)sp;
       newp->context->eip = (int)(void *)wrapp;
}

/*
 * getdtablesize -- linux hasn't got this
 */
int getdtablesize (void)
{
       return (NOFILE);
}

#else
/*
 * sun4.c -- lightweight process initialisation for sun4.
 *
 * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
 * Imperial College of Science, Technology and Medicine, 180 Queen's
 * Gate, London SW7 2BZ, England.
 */

#include "lwp.h"

/*
 * initp -- initialise a new process's context.
 */
void initp (volatile struct pcb *volatile newp, void *sp)
{
	static jmp_buf *cpp;
	extern struct pcb *currp;
	
	newp->context[0] = (int)sp;

	/* preserve cpp for new context */
	cpp = (jmp_buf *) &newp->context;
	if (!savep (currp->context)) {
		/* create new context */		
		/* flush registers */
		asm volatile ("ta	0x03");
		/* %o0 <- newp */
		asm volatile ("ld	[%fp+0x44], %o0");
		/* %o1 <- newp->context[0] */
		asm volatile ("ld	[%o0], %o1");
		/* create min frame on new stack */
		asm volatile ("save	%o1,-96, %sp");
		if (!savep (*cpp))
			restorep (currp->context);
		wrapp ();
	}
}
#endif
