/*
 * $Header: /smsa/bsd:include/sys/vmmac.h:bsd  1.12  $
 * $Source: /smsa/bsd:include/sys/vmmac.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 *
 */
/* @(#)vmmac.h	1.2 87/05/15 3.2/4.3NFSSRC */
/*
 * Copyright (c) 1982, 1986 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 *
 *	@(#)vmmac.h	7.1 (Berkeley) 6/4/86
 */

#ifndef	__sys_vmmac_h
#define	__sys_vmmac_h

/*
 * Virtual memory related conversion macros
 */

/* Core clicks to number of pages of page tables needed to map that much */
#define	ctopt(x)	(((x)+NPTEPG-1)/NPTEPG)

/* Virtual page numbers to text|data|stack segment page numbers and back */
#define	vtotp(p, v)	((int)(v)-LOWPAGES)
#define	vtodp(p, v)	((int)((v) - stoc(ctos((p)->p_tsize)) - LOWPAGES))
#define	vtosp(p, v)	((int)(p->p_uspg0 - (v)))
#define	tptov(p, i)	((unsigned)(i) + LOWPAGES)
#define	dptov(p, i)	((unsigned)(stoc(ctos((p)->p_tsize)) + (i) + LOWPAGES))
#define	sptov(p, i)	((unsigned)(p->p_uspg0 - (i)))

/* Tell whether virtual page numbers are in text|data|stack|shm segment */
#define	isassv(p, v)	(((v) <= p->p_uspg0) && \
			 ((v) > p->p_uspg0 - (unsigned)(p)->p_ssize))
#define	isatsv(p, v)	(((v) - LOWPAGES) < (unsigned)(p)->p_tsize)
#define	isadsv(p, v)	(((v) - LOWPAGES) >= \
			 (unsigned)stoc(ctos((p)->p_tsize)) && !isassv(p, v))
#define isashmv(p, v)	((v) >= btop(shm_base) && (v) < btop(shm_limit))

/* Tell whether pte's are text|data|stack */
#define	isaspte(p, pte)		((pte) > sptopte(p, (p)->p_ssize))
#define	isatpte(p, pte)		((pte) < dptopte(p, 0))
#define	isadpte(p, pte)		(!isaspte(p, pte) && !isatpte(p, pte))

#define	tptopte(p, i)		(&(p)->PT[LOWPAGES+(i)])
#define tptopod(p, i)		(ptetopod(tptopte(p, i)))
#define	dptopte(p, i)		(&(p)->PT[LOWPAGES+((p)->p_tsize + (i))])
#define	sptopte(p, i)		(&(p)->PT[(p)->p_uspg0 - (i)])

extern struct pte *vtopte();

/* Bytes to pages without rounding, and back */
#define	btop(x)		(((unsigned)(x)) >> PGSHIFT)
#define	ptob(x)		((caddr_t)((x) << PGSHIFT))

/* User area pte and address */
#define	uaddr(p)	(&(p)->PT[MAXPT-UPAGES])
#define	ptou(p)		((struct user *)(((p)->p_addr[0].pg_pfnum*NBPG)+PHYS_MEM_BASE))

/* Average new into old with aging factor time */
#define	ave(smooth, cnt, time) \
	smooth = ((time - 1) * (smooth) + (cnt)) / (time)

#define	outofmem()	wakeup((caddr_t)&proc[2]);

/*
 * Page clustering macros.
 * 
 * dirtycl(pte)			is the page cluster dirty?
 * anycl(pte,fld)		does any pte in the cluster have fld set?
 * zapcl(pte,fld) = val		set all fields fld in the cluster to val
 * distcl(pte)			distribute high bits to cluster; note that
 *				distcl copies everything but pg_pfnum,
 *				INCLUDING pg_m!!!
 *
 * In all cases, pte must be the low pte in the cluster, even if
 * the segment grows backwards (e.g. the stack).
 */

#if CLSIZE == 1
#define	dirtycl(pte)	dirty(pte)
#define	anycl(pte,fld)	((pte)->fld)
#define	zapcl(pte,fld)	(pte)->fld
#define	distcl(pte)
#else
 #error CLSIZE != 1
#endif

/* Historical hangover: M{,UN}LOCK was implemented in a macro on the VAX */
#define MLOCK(c)	mlock(c)
#define MUNLOCK(c)	munlock(c)

#endif/*__sys_vmmac_h*/

/* EOF vmmac.h */
