/*
 *	mobproc.h -- interface for mobile process specials
 *	Copyright (C) 2004-2005 Fred Barnes <frmb@kent.ac.uk>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program 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 the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __MOBPROC_H
#define __MOBPROC_H

typedef struct mp_filehdr_struct {
	char hdr[8];		/* OCMP[majver][minver][patch]\0 */
	unsigned int typehash;	/* of the process type */
	unsigned int wsbytes;
	unsigned int vsbytes;
	unsigned int msbytes;
	unsigned int wsmapbytes;
	unsigned int cmapentries;
	unsigned int objbytes;

	unsigned int iptr;	/* index into codemap */
	unsigned int wsptr;	/* byte offset into workspace */
	unsigned int barrier;	/* byte offset into workspace */
	unsigned int unused[2];	/* unused */

	unsigned int pname_offs;	/* offset of process name from the base of this structure */
	unsigned int wsdata_offs;	/* offset of workspace data from the base of this structure */
	unsigned int vsdata_offs;	/* offset of vectorspace data from the base of this structure */
	unsigned int msdata_offs;	/* offset of mobilespace data from the base of this structure */
	unsigned int wsmap_offs;	/* offset of workspace map from the base of this structure */
	unsigned int obj_offs;		/* offset of "object space" from the base of this structure */
} mp_filehdr;

struct mp_mapchain_struct {
	struct mp_mapchain_struct *next;
	unsigned char *mapdata;
	int wsoffset;		/* in words */
	int mapsize;		/* extracted from data (in entries) */
};

typedef struct mp_mapchain_struct mp_mapchain;

struct mp_ctrlblk_struct {
	/* 0-3 */
	void *wptr;		/* Wptr of inactive mobile process, or Wptr of activator */
	void *iptr;		/* Iptr of inactive mobile process */
	void *aiptr;		/* activator Iptr */
	mp_mapchain *mapchain;	/* map-chain for the process */
	/* 4-7 */
	void *wsbase;		/* workspace base */
	int wssize;		/* workspace size (bytes) */
	void *vsbase;		/* vectorspace base */
	void *msbase;		/* mobilespace base */
	/* 8-13 */
	mproc_bar_t *barrier;	/* barrier */
	int unused[3];		/* unused */
	unsigned int typehash;	/* typehash for this process */
	char *codemap;		/* pointer to the compiled-in code map */
	/* 14-17 */
	int vssize;		/* vectorspace size (bytes) */
	void **mshook;		/* MS hook pointer */
	int mssize;		/* mobilespace size (bytes) */
	void *msdesc;		/* mobilespace descriptor-block */
#ifdef __GNUC__
	} __attribute__ ((packed));
#else
	};
	#warning Not GNU C -- might have problems CLONE-ing/serialing mobile processes
#endif
typedef struct mp_ctrlblk_struct mp_ctrlblk;


extern void mpcb_add_wsmap (mp_ctrlblk *mp, unsigned char *mapdata, unsigned int *wptr);
extern void mpcb_del_wsmap (mp_ctrlblk *mp, unsigned char *mapdata, unsigned int *wptr);
extern void mpcb_rm_wsmap (mp_ctrlblk *mp);
extern mp_ctrlblk *mpcb_mpp_clone (mp_ctrlblk *mp);
extern int mpcb_mpp_serialise (mp_ctrlblk **mpp, unsigned int *thashp, int *raddr, int *rsize);
extern int mpcb_mpp_deserialise (int addr, int size, mp_ctrlblk **mpp, unsigned int *thashp);
extern void mpcb_dump_process (mp_ctrlblk *mp);

extern int mpp_checkroutine (char *name);
extern int mpp_loadlibrary (char *lname);
extern int mpp_unloadlibrary (char *lname);


/*
 *	these are constants generated by the compiler for mapping
 *	defined in include/genhdr.h for occ21
 */

#define WSMAP_INVALID		0x00 
#define WSMAP_CHANWORD		0x01
#define WSMAP_CHANPTR		0x02
#define WSMAP_FIRSTPARAM	0x03
#define WSMAP_CHANARRAY		0x04
#define WSMAP_GENPTR		0x05
#define WSMAP_MOB_DA		0x06
#define WSMAP_MOB_CT		0x07
#define WSMAP_MOB_PT		0x08
#define WSMAP_VSPTR		0x09
#define WSMAP_FB		0x0a
#define WSMAP_MSPTR		0x0b
#define WSMAP_MPP		0x0c
#define WSMAP_MOB_SHADOW	0x0d
#define WSMAP_STATICLINK	0x0e
#define WSMAP_CODEPTR		0x0f

#define WSMAP_TYPEMASK		0x0f

#define WSMAP_FLAG_VS		0x80	/* in-vectorspace flag */

/*
 *	uses a fairly conservative encoding scheme for the constants:
 *	    [0xxxxxxx]                          -- 7-bit(x)
 *	    [10xxxxxx][xxxxxxxx]                -- 14-bit(x)
 *	    [11xxxxxx][xxxxxxxx][xxxxxxxx]      -- 22-bit(x)
 *	these entries are *signed* integers, so must be extended appropriately
 *	if the first "x"-bit is 1.
 *
 *	the map starts with two 2-byte words:
 *	    [h.......][.......l]                -- 16-bit number of individual entries
 *	    [h.......][.......l]                -- 16-bit number of bytes remaining
 *
 *	map is pairs of: <offset> <type> [optional type-info]
 */


/*
 *	when serialised, some encodings are specisl:
 */

#define ENCODED_NOTPROCESS	((void *)0x80000000)
#define ENCODED_AIPTR		((void *)0x80000004)
#define ENCODED_MPP		((void *)0x80000008)


#endif	/* !__MOBPROC_H */

