/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: user.h 1.37 90/10/01 $"

#ifndef	_SYS_USER_H_

/*
 * user.h
 *	Per process structure containing data that
 *	isn't needed in core when the process is swapped out.
 */

/* $Log:	user.h,v $
 */

#include "sys/dmap.h"
#include "sys/procstats.h"
#ifdef	INKERNEL
#include "sys/resource.h"
#endif
#include "sys/mman.h"
#include "sys/ofile.h"
#include "sys/fpu.h"
#include "sys/ptrace.h"
#include "sys/timer.h"

/*
 * Per-process swappable process-representation (U-area).
 */

#define	MAXCOMLEN	36			/* sizeof u_comm, <= UFS_MAXNAMLEN*/

#define	PSARGSZ		80			/* space in u-block for exec
						 * arguments. used by ps
						 */

struct	user	{
	caddr_t		u_sp;			/* saved sp (context switch) */
	struct proc	*u_procp;		/* pointer to proc structure */
	int		*u_ar0;			/* address of users saved R0 */

	/*
	 * Page-fault frequency related fields.
	 */
	long		u_pffcount;		/* #faults since last adjust */
	long		u_pffvtime;		/* virt-time since last adjust*/

	/*
	 * Syscall parameters, results and catches.
	 */
	int		u_arg[6];		/* args to current syscall */
#define	u_ap		u_arg			/* pointer to arglist */
#ifdef	i386
	unsigned	u_fltaddr;		/* addr for kernel page fault */
#endif	/* i386 */
	label_t		u_qsave;		/* non-local gotos on signals */
	char		u_error;		/* return error code */
	union	{				/* syscall return values */
		struct	{
			int	R_val1;
			int	R_val2;
		} u_rv;
#define	r_val1	u_rv.R_val1
#define	r_val2	u_rv.R_val2
		off_t	r_off;
		time_t	r_time;
	} u_r;
#ifdef	ns32000
	unchar		u_swtrap;		/* SW traps (see below) */
#endif	/* ns32000 */
#ifdef	i386
	uint		u_swtrap;		/* SW traps (see below) */
#endif	/* i386 */

	/*
	 * Processes and protection
	 */
	struct	ucred	*u_cred;		/* user credentials */
#define	u_uid	u_cred->cr_uid
#define	u_gid	u_cred->cr_gid
#define	u_groups u_cred->cr_groups
#define	u_ruid	u_cred->cr_ruid
#define	u_rgid	u_cred->cr_rgid

	/*
	 * Memory management
	 * u_tsize holds only the initial size of the text after exec.
	 * u_dsize holds the sum of text and data.  mmap() can affect u_dsize.
	 * u_dsegsz holds the sum of data, bss and brk. sbreak changes u_dsegsz.
	 */
	size_t		u_tsize;		/* text size (clicks) */
	size_t		u_dsize;		/* text+data size (clicks) */
	size_t		u_ssize;		/* stack size (clicks) */
	struct	dmap	u_dmap;			/* disk map for data segment */
	struct	dmap	u_smap;			/* disk map for stack segment */
	struct	dmap	u_cdmap, u_csmap;	/* shadows u_dmap, u_smap, for
						   use of parent during fork */
	label_t		u_ssave;		/* label variable for swap */
	size_t		u_odsize, u_ossize;	/* for expansion swaps */
	size_t		u_dsegsz;		/* size of data+bss+sbrk */

	/*
	 * Signal management
	 */
	struct		sigaction u_sa[NSIG+1]; /* per signal information */

	void		(*u_sigreturn)();	/* signal catcher return */
	sigset_t	u_oldmask;		/* saved mask for sigpause */
	int		u_code;			/* ``code'' to trap */
	int		u_segvcode;		/* address for SIGSEGV hdlr */
	struct sigaltstack u_altstack;		/* alternative stack */
	struct sig_frame *u_sigframe;		/* current signal frame */

	/*
	 * Descriptor management
	 */
	struct	ofile_tab *u_ofile_tab;		/* open-file table */
	struct	file	*u_fpref;		/* syscall made ref to this */
	struct	file *u_openfp;			/* for benefit of cloneopen */
	struct	vnode	*u_cdir;		/* current directory */
	struct	vnode	*u_rdir;		/* root dir of current process*/
	char		u_ctty;			/* request to establish a */
						/* a controlling tty flag */

	short		u_cmask;		/* mask for file creation */

	daddr_t		u_limit;		/* maximum write address */

	short		u_lock;			/* process/text locking flags */

	/*
	 * Floating-point save area.
	 */
	struct	fpusave	u_fpusave;		/* float unit save area */
#ifdef	FPA
	struct	fpasave	u_fpasave;		/* float accel. save area */
#endif	/* FPA */

	/*
	 * Mmap state information.
	 * All u_mmap[]'s in use are < u_mmapmax; cleared in exec, only grows.
	 * u_mmapdel is a boolean, tells if must copy back u_mmap[]
	 * array into parent after vfork child exit|exec's.
	 */
	struct	mmap	u_mmap[NUMMAP];		/* per mapped object */
	struct	mmap	*u_mmapmax;		/* valid mmaps < mmapmax */
	char		u_pmapcnt;		/* # MM_PHYS maps in use */
	char		u_mmapdel;		/* page delted? for vfork */

	/*
	 * Multi-process debugging fields.
	 */
	int		u_sigpass;		/* mask of pass-thru signals */
	struct	proc	*u_mptdbgr;		/* MP debugger in control */
#ifdef	i386
	struct	watchpt	u_watchpt;		/* watchpoint support */
#endif	/* i386 */

	unsigned int	u_count;		/* bytes remaining for IO */
	off_t		u_offset;		/* offset in file for IO */
	char		u_psargs[PSARGSZ];	/* arguments from exec */

	/*
	 * Timing and statistics
	 */
	timeval_t	u_syst;
	timeval_t	u_start;
	long		u_ioch;			/* chars transferred */
	short		u_acflag;
	char		u_scgacct;		/* account identifier */
	struct process_stats	u_pst;		/* stats for this proc */
	struct process_stats	u_cpst;		/* sume of reaped child stats */
#define	u_utime		u_pst.ps_utime.tv_sec
#define	u_stime		u_pst.ps_stime.tv_sec
#define	u_cutime	u_cpst.ps_utime.tv_sec
#define	u_cstime	u_cpst.ps_stime.tv_sec
	struct	uprof	{			/* profile arguments */
		short	*pr_base;		/* buffer base */
		unsigned pr_size;		/* buffer size */
		unsigned pr_off;		/* pc offset */
		unsigned pr_scale;		/* pc scaling */
	} u_prof;

	/*
	 * Exec command save area.
	 */
#ifdef	ns32000
	char		u_comm[MAXCOMLEN + 1];	/* exec path last component */
#endif	/* ns32000 */
#ifdef	i386					/* No Chip Bugs! */
	char		u_comm[MAXCOMLEN + 1];	/* exec path last component */
#endif	/* i386 */
	/*
	 * This is a temporary workaround that permits higher level
	 * routines (e.g., spec_open()) to know if a cloneopen(),
	 * rather than a direct device open, has occurred.  This
	 * will be the pointer to the new dev structure returned
	 * by cloneopen().  This entry only has meaning in the
	 * spec_open() routine.  This workaround should be removed 
	 * when mailbug #31990 is addressed.
	 */
	struct	dev	*u_clonedev;		/* ptr to clone'd dev */

	struct	itimerval u_timer[3];

        /*
         * Flag single-step of lcall on a system call (when using BCS).
         * The signal SIGTRAP is delivered after the system call completes.
         */
        char            u_debugpend;            /* SIGTRAP pending */

	/*
	 * Indicate the type of the process (bcs, posix, abi, etc.)
	 */
	unsigned int	u_ptype;		/* process type */

	int		u_b1_rsvd[5];		/* reserved for b1 product */

#ifdef FPA
	struct fpaesave *u_fpaesave;		/* ptr to emulation save area */
	long		u_fpae_extra_status;
#endif

	/*
	 * The following space is reserved for future use.  If the
	 * NEED arises to add another field to the user structure, we
	 * can use a portion of the u_pad space, thus maintaining binary
	 * compatibility.  Also, in the unlikely event that we no longer 
	 * need an existing entry, the excess space should be returned to 
	 * u_pad (to maintain the exact size of the uarea).
	 */
	unsigned char	u_pad[4];
};

/* u_ptype encodings */
#define POSIX_PROC      0	/* Default Dynix/ptx process type */
#define BCS_PROC        1	/* BCS process type */
#define ABI_PROC        2	/* future use */

/*
 * User credential structure.  Holds information for identity and access
 * checking.
 */

struct	ucred	{
	lock_t		cr_lock;		/* Mutex for ref count */
	short		cr_ref;			/* reference count */
	uid_t		cr_uid;			/* effective user id */
	gid_t		cr_gid;			/* effective group id */
	gid_t		cr_groups[NGROUPS];	/* groups, filled w/INVAL_GID */
	uid_t		cr_ruid;		/* real user id */
	gid_t		cr_rgid;		/* real group id */
};

/*
 * These are reserved uid and gids for system use.
 */
#define	INVAL_UID	0xFFFF
#define	INVAL_GID	0xFFFF

#ifdef	INKERNEL
extern	struct user u;

/*
 * Software traps queue work at interrupt level to be done in a process
 * before re-enterring user mode.  They handle various per-process bookkeepping
 * functions.  Most SW traps are turned on only in an interrupt from
 * user mode, thus guarantee the code will be executed before the process
 * returns from the kernel.  Callers of SWTON() should insure the test/set
 * of l.runrun can't be interrupted (typically invoked only at SPLHI).
 */

#define	SWT_PFF		0x01			/* PFF adjustment */
#define	SWT_PROF	0x02			/* PROFile tick */

#define	SWT_FPU_PGXBUG	0x80			/* FPU Instr Cross Page Bug */

#define	SWTON(mask) { \
	u.u_swtrap |= (mask); \
	if (l.runrun == 0) l.runrun = -1; \
}

/*
 * request to set controlling terminal
 * used by stream drivers to tell stream head that this is terminal
 * device willing to become a controlling terminal.
 */
#define	REQ_CTTY()	{u.u_ctty = 1;}

/*
 * Credential manipulation procedures, public data-structures.
 */

struct	ucred	*crget();
struct	ucred	*crcopy();
struct	ucred	*crdup();
void		crhold();
void		crfree();

extern	struct	ucred	*ucred_free;		/* credentials free list */
extern	int		nucred;			/* # cred's allocated */

#endif	/* INKERNEL */

#define	_SYS_USER_H_
#endif	/* _SYS_USER_H_ */
