/*
 * $Header: /smsa/bsd:include/sys/shm.h:bsd  1.12  $
 * $Source: /smsa/bsd:include/sys/shm.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 *
 */
/*
**	IPC Shared Memory Facility.
*/

#ifndef __sys_shm_h
#define __sys_shm_h

#ifndef __sys_param_h
#include "param.h"
#endif
#ifndef __sys_ipc_h
#include "ipc.h"
#endif


/*
**	Implementation Constants and system limits.
*/

#define	SHMLBA		NBPG	/* segment low boundary address multiple    */
				/* (SHMLBA must be a multiple of page size) */

#define NSHMID		(16)	/* Max shared memory segs in system */
#define NSHMAT		(8)	/* Max segs attached to one process */
#define NSHMVPAGES	(32)	/* Amount of virtual address space  */
				/* reserved for shm in each process */
#define SHMMINPAGES	(1)	/* Min shared region size */
#define SHMMAXPAGES	(32)	/* Max shared region size */
#define SHMTOTPAGES	(64)	/* Limit on total system shm size  */

/*
**	Permission Definitions.
*/

#define	SHM_R	0400	/* read permission */
#define	SHM_W	0200	/* write permission */

/*
**	ipc_perm Mode Definitions.
*/

#define	SHM_INIT	01000	/* grow segment on next attach */
#define	SHM_DEST	02000	/* destroy segment when # attached = 0 */

/*
**	Message Operation Flags.
*/

#define	SHM_RDONLY	010000	/* attach read-only (else read-write) */
#define	SHM_RND		020000	/* round attach address to SHMLBA */

/*
**	Structure Definitions.
*/

/*
**	There is a shared mem id data structure for each segment in the system.
*/

struct shmid_ds {
	struct ipc_perm	shm_perm;	/* operation permission struct */
	int		shm_segsz;	/* size of segment in bytes */
	pid_t		shm_lpid;	/* pid of last shmop */
	pid_t		shm_cpid;	/* pid of creator */
	time_t		shm_atime;	/* last shmat time */
	time_t		shm_dtime;	/* last shmdt time */
	time_t		shm_ctime;	/* last change time */
	unsigned short	shm_nattch;	/* number of current attachments */
	struct shmat_ds *shm_shmat;	/* linked list of attachments */
	struct region	*shm_reg;	/* ptr to region structure */
};

/*
 * There is a shmat_ds for each instance of a shared region being 
 * attached to a process's address space.
 */

struct shmat_ds
{
  struct shmid_ds *sa_shm;       /* Pointer to shared memory table entry     */
                                 /* (0 if this shmat_ds is not in use)       */
  struct shmat_ds *sa_link;	 /* Link to other attachments of this seg    */
  struct proc     *sa_proc;      /* Process to which attached                */
  caddr_t         sa_baseaddr;   /* Where segment mapped in proc addr space  */
  int             sa_access;     /* Access allowed to segment (SHM_R always, */
				 /* SHM_W optional).                         */
};

#if defined(_BSD_C) || defined(_KERNEL)
#ifndef __sys_map_h
#include "map.h"
#endif
struct proc_shm
{
    /* map to alloc virtual pages to shared mem */
    struct	map ps_shmmap[NSHMAT + 3];
    /* record of attached shared regions */
    struct	shmat_ds ps_shmat[NSHMAT];
};
#endif/*_BSD_C || _KERNEL*/


#ifndef	_KERNEL
/* Library interfaces */
extern char *shmat (int __shmid, char *__shmaddr, int __shmflg);
extern int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf);
extern int shmdt (char *__shmaddr);
extern int shmget (key_t __key, int __size, int __shmflg);
#endif/*_KERNEL*/

#ifdef	_KERNEL
/* The rest is for the kernel only */
extern char shm_base[];	/* Lowest virtual address available for mapping      */
                        /* shared memory segments. There are SHMVPAGES       */
                        /* available starting here.                          */
extern char shm_limit[];/* First address after highest virtual address         */
                        /* reserved for mapping shared memory.               */

extern struct pte *shm_vtopte(); /* Find pte for given shm virtual page no. */
#endif/*_KERNEL*/

#endif/*__sys_shm_h*/

/* EOF shm.h */
