/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos72V src/bos/kernel/sys/rset.h 1.37.4.3                              */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* Restricted Materials of IBM                                            */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2000,2019              */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/* @(#)25     1.37.4.3  src/bos/kernel/sys/rset.h, sysnuma, bos72V, v2019_45B6 11/6/19 13:00:36 */
/*
 * COMPONENT_NAME: (SYSNUMA) Non-Uniform Memory Access
 *
 * FUNCTIONS: NUMA API definitions and prototypes
 *
 * ORIGINS: 83
 */

#ifndef _H_RSET
#define _H_RSET

#include <sys/types.h>
#include <sys/processor.h>	    /* for sradid_t */

#ifdef __cplusplus
extern "C" {
#endif

typedef struct subrange subrange_t;

/* Type to identify a resource set handle: rsethandle_t */
typedef void * rsethandle_t;

/* Values to identify a work component or resource: rstype_t */
typedef int rstype_t;
#define R_NADA          0           /* Nothing - invalid specification */
#define R_PROCESS       1           /* Process */
#define R_RSET          2           /* Resource set */
#define R_SUBRANGE	3	    /* Memory range */
#define R_SHM           4 	    /* Shared Memory */
#define R_FILDES        5           /* File identified by an open file */
                                    /* descriptor */
#define R_THREAD        6           /* Thread */
#define R_SRADID	13          /* SRADID */
#define R_PROCMEM	14	    /* Process Memory */

/* Union to contain a work component or resource:  rsid_t */
typedef union {
	pid_t at_pid;           /* Process id (for R_PROCESS and R_PROCMEM */ 
	tid_t at_tid;           /* Kernel thread id (for R_THREAD) */
	int at_shmid;		/* Shared memory id (for R_SHM) */
	int at_fd;		/* File descriptor (for R_FILDES) */
	rsethandle_t at_rset;	/* Resource set handle (for R_RSET) */
        subrange_t *at_subrange;  /* Memory ranges (for R_SUBRANGE) */
	sradid_t at_sradid;	/* SRAD id (for R_SRADID) */
#ifdef _KERNEL
	ulong_t at_raw_val;	/* raw value: used to avoid copy typecasting */
#endif
} rsid_t;

/* Structure to specify a set of memory subranges attachment: subrange_t */
struct subrange {
           off64_t su_offset;           /* Offset of the subrange (bytes) */
           size64_t su_length;          /* Length of the subrange (bytes) */
                                        /* 0 indicates an extent from
					 * the offset to the end of
					 * the object. */
           rstype_t su_rstype;          /* Type of resource the subrange */
                                        /* is being attached to  */
	   uint_t su_policy;            /* Memory allocation policy */
           rsid_t su_rsid;              /* Id of the resource the subrange */
                                        /* is being attached to  */
           off64_t su_rsoffset;         /* reserved for future use */
           size64_t su_rslength;        /* reserved for future use */
};

/* Structure to describe a resource attachment */
typedef struct attachinfo { 
           rstype_t at_rstype;		/* Type of resource */
           rsid_t at_rsid;		/* Id of the resource */
           uint_t at_policy;		/* Allocation policy */
           int pad0;			/* 64 bit padding */
           struct attachinfo *next_resource; /* Next involved resource */
} attachinfo_t; 

/*
 * Borrowing priorities
 */
enum {
    BORROW_PRIO_0 = 0, /* Strict attachments. Least likely to be borrowed */
    BORROW_PRIO_1,
    BORROW_PRIO_2, /* Advisory attachments */
    BORROW_PRIO_3,
    BORROW_PRIO_4  /* System home SRADs */
};
#define BORROW_PRIO_MAX 	BORROW_PRIO_4
#define BORROW_PRIO_STRICT	BORROW_PRIO_0
#define BORROW_PRIO_ADVISORY	BORROW_PRIO_2
#define BORROW_PRIO_SYSTEM	BORROW_PRIO_4

/*
 * Structure passed to ra_mmap: contains the mmap characterics
 * - code depends on this being ILP32/LP64 size-invariant
 */
typedef struct mmap_struct {
        uint64_t mm_addr;
        size64_t mm_len;
        off64_t  mm_pos;
        int      mm_prot;
        int      mm_flags;
        int      mm_fd;
} mmap_struct_t;


/* Values to identify memory and other policies */
#define P_DEFAULT       0               /* Default memory policy */
#define P_FIRST_TOUCH   1               /* First Access memory policy */
#define P_BALANCED      2               /* Balanced memory policy */

#define R_MEMP_MASK	0x000000ff	/* 256 possible memory policies */
#define R_ATTACH_STRSET 0x00000100	/* ST Rset scheduling policy */	
#define R_MIGRATE_ASYNC 0x00000200	/* Async migrate memory */
#define R_MIGRATE_SYNC	0x00000400      /* Synchronous migrate memory */	
#define R_MIGRATE_NOATT	0x00000800      /* Synchronous migrate w/o attach */	
#define R_STRICT_SRAD	0x00001000      /* Strict SRAD attachment */
#define R_ADVISORY_RSET 0x00002000      /* Advisory RSETs */
#define R_NOMIGRATE	0x00004000	/* ra_detach only: don't move threads */
#define R_SCATTER_CAP	0x00008000	/* Scatter threads based on capacity */
#define R_SCATTER_LOAD	0x00010000	/* Scatter threads based on capacity */
#define R_RROBIN_SRAD	0x00020000      /* Round robin SRAD attachment */
#define R_PROCESS_SRAD	0x00040000      /* Process SRAD attachment */
#define R_NOINHERIT	0x00080000	/* Do not inherit parent attachments */

/* Flags used by ra_attach */
#define R_FLAGS_MASK (R_ATTACH_STRSET | R_MIGRATE_ASYNC | R_MIGRATE_SYNC | \
		      R_MIGRATE_NOATT | R_STRICT_SRAD | R_ADVISORY_RSET |  \
			R_SCATTER_CAP | R_SCATTER_LOAD)

/* Flags used by ra_detach */
#define R_DETACH_MASK (R_ADVISORY_RSET | R_NOMIGRATE | R_SCATTER_CAP | \
			R_SCATTER_LOAD)

#define R_DETACH_ALLTHRDS 0x00000001	/* Detach all threads in a process */

#ifdef _KERNSYS
/* Kernel only, macros that operate on memory placement policies */
#define VALID_POLICY(p) ( (p) >= 0 && (p) <= P_BALANCED ) 
#endif

/* Value to identify current execution object */
#define RS_MYSELF       (-1)

/* Bit layout for flags parameter */
#define R_TYPE_MASK     0x000000FF  /* bits 24-31 are types */
#define R_AVAIL_MASK    0x0000FF00  /* bits 16-23 are availability */

/* Values passed to rs_alloc: */
#define RS_EMPTY        0           /* Resource set with no resources */
#define RS_SYSTEM       1           /* Resource set with system resources */
#define RS_ALL          2           /* Resource set with all resources */
#define RS_PARTITION    3           /* Resource set with calling process's */
                                    /* partition rset resources */

/* Values to identify type of information requested: rsinfo_t */
typedef int rsinfo_t;
#define R_NUMPROCS      0           /* Number of processors */
#define R_NUMMEMPS      1           /* Number of memory pools */
#define R_MEMSIZE       2           /* Memory size (in MBytes) */
#define R_MAXSDL        3           /* Maximum system detail level */
#define R_SMPSDL        4           /* SMP system detail level */
#define R_MCMSDL        5           /* MCM system detail level */
#define R_SRADSDL	R_MCMSDL    /* SRAD system detail level */
#define R_MAXPROCS      6           /* Maximum number of processors in rset */
#define R_MAXMEMPS      7           /* Maximum number of memory pools in rset */
#define R_LGPGFREE      8           /* Number of free large pages in rset */
#define R_LGPGDEF       9           /* Number of defined large pages in rset */
#define R_SHMATTCAP	10          /* Support for ra_shmget(v) interfaces */
#define R_L2CSDL	11	    /* L2 cache system detail level */
#define R_PCORESDL	12	    /* Processor Core system detail level */
#define R_REF1SDL	13	    /* First reference point SDL */
#define R_GENERATION    14	    /* Affinity topology generation number */
#define R_MAXSRADS	15	    /* Maximum number of srads permitted in 
				     * partition */

/* Values to identify a physical resource type for rs_op() and others */
#define R_PROCS         1           /* Processors */
#define R_MEMPS         2           /* Memory pools */
#define R_ALL_RESOURCES (R_MEMPS | R_PROCS)

/*
 * Commands for rs_op():
 * RS_UNION and RS_EXCLUSION may be also used for rs_getrad flags.
 */
#define RS_UNION        1           /* Union */
#define RS_INTERSECTION 2           /* Intersection */
#define RS_EXCLUSION    3           /* Exclusion */
#define RS_COPY         4           /* Resource set to resource set copy */
#define RS_FIRST        5           /* First resource of a given type */
#define RS_NEXT         6           /* Resource following a specified one */
#define RS_NEXT_WRAP    7           /* Resource following a specified one */
                                    /* in a resource set, with wrapping */
                                    /* for the last resource */
#define RS_ISEMPTY      8           /* Emptiness */
#define RS_ISEQUAL      9           /* Equality */
#define RS_ISCONTAINED  10          /* Inclusion */

#define RS_TESTRESOURCE 11          /* Test if resource in rset */
#define RS_ADDRESOURCE  12          /* Add resource to rset */
#define RS_DELRESOURCE  13          /* Delete resource from rset */
#define RS_STSET  	14          /* Get ST rset */

/*
 * Flags for rs_op():
 */
#define RS_NON_EXCLUSIVE    0x00000001

/*
 * Values for rs_info() commands 
 */
#define RS_CONTAINING_RAD   1	    /* Containing RAD index */
#define RS_SRADID_LOADAVG   2	    /* Dispatcher load average for sradid */
#define RS_SRADID_USABLE_LOADAVG 3  /* Dispatcher load average for CPUs in  */
                                    /* specied SRAD that are usable by the  */
				    /* the calling thread */  

/*
 * Output structure for RS_SRADID_LOADAVG command
 */ 
typedef struct loadavg_info {
	int load_average;	    /* dispatcher load average */
	int cpu_count;		    /* number of CPUs included in the 
				     * load average */
} loadavg_info_t;

#define SRADID_ANY	    (-1)    /* SRADID for all SRADs in a partition */

/*
 * Attached rset types
 */
#define RS_EFFECTIVE_RSET             2       /* process effective rset */
#define RS_PARTITION_RSET             3       /* process partition rset */
#define RS_DEFAULT_RSET               4       /* Default rset           */
#define RS_THREAD_RSET                5       /* Thread effective rset  */
#define RS_THREAD_PARTITION_RSET      6       /* Thread partition rset  */
#define RS_ADVISORY_RSET	      7       /* Process advisory rset  */

/* Value to specify default rset to rs_setpartition() */
#define RS_DEFAULT	((rsethandle_t)RS_DEFAULT_RSET)

/* Maximum length of a RAD name */
#define RAD_NAME_SIZE   32

/* Maximum length of a registered Rset name or namespace */
#define RSET_NAME_SIZE  255

/* Commands passed to rs_registername: */
#define RS_DEFINE       0           /* The name's registration is not */
                                    /* modified if it already exists */
#define RS_REDEFINE     1           /* The name's registration is */
                                    /* modified if it already exists */

/* structure used by rs_getrsetnames syscall */
typedef struct rsetinfo {
				/* all offsets are relative to the */
				/* start of the current rsetinfo_t */
	uint_t	nsloc;		/* relative byte offset of start */
				/* of namespace character string */
	uint_t	rsloc;		/* relative byte offset of start */
				/* of rsname character string */
	uint_t	nextloc;	/* relative byte offset of start */
				/* of next rsetinfo_t */
	uid_t	owner;		/* owner */
	gid_t	group;		/* group */
	uint_t	mode;		/* permission bits and type */
} rsetinfo_t;

/* structure for rs_getnameattr and rs_setnameattr */
typedef struct rs_attributes {
        uid_t owner; gid_t group; uint_t mode;
} rs_attributes_t;

/* Commands passed to rs_setnameattr: */
#define RS_OWNER        1           /* Set owner attribute */
#define RS_GROUP        2           /* Set group attribute */
#define RS_PERM         4           /* Set permission attribute */

/* Values to identify the resource sets types */
#define RS_PERMMASK     0000666     /* to extract permission from mode */
#define RS_TYPMASK      0003000     /* to extract type from mode */
#define RS_APPDEF       0000000     /* Application-defined Rset */
#define RS_SYSADM       0001000     /* System-Admin. defined Rset */
#define RS_SYSRAD       0002000     /* System RAD */

/* Values to identify the resource sets names access rights */
#define RS_IRUSR        0000400     /* read permission: owner */
#define RS_IWUSR        0000200     /* write permission: owner */
#define RS_IRGRP        0000040     /* read permission: group */
#define RS_IWGRP        0000020     /* write permission: group */
#define RS_IROTH        0000004     /* read permission: other */
#define RS_IWOTH        0000002     /* write permission: other */

#ifdef _NO_PROTO

extern int rs_numrads();
extern int rs_getrad();
extern int rs_getinfo();
extern int rs_info();
extern int rs_alloc();
extern void rs_free();
extern int rs_init();
extern int rs_op();

extern int rs_registername();
extern int rs_getnameattr();
extern int rs_setnameattr();
extern int rs_discardname();
extern int rs_getnamedrset();

extern pid_t ra_fork();
extern int ra_execl();
extern int ra_execle();
extern int ra_execlp();
extern int ra_execv();
extern int ra_execve();
extern int ra_execvp();
extern int ra_exect();

extern int ra_attach();
extern int ra_detach();

extern int ra_attachrset();
extern int ra_detachrset();

extern int ra_getrset();

extern int ra_shmget();
extern int ra_shmgetv();

extern void *ra_mmap();
extern void *ra_mmapv();

extern int rs_getpartition();
extern int rs_setpartition();

extern int rs_getassociativity();
extern attachinfo_t *ra_get_attachinfo();
extern int ra_free_attachinfo();

extern sradid_t rs_get_homesrad();
#else /* _NO_PROTO */

/* rset services */
extern int rs_numrads(rsethandle_t rseth,
                    uint_t sdl,
                    uint_t flags);

extern int rs_getrad(rsethandle_t rseth_in,
                    rsethandle_t rseth_out,
                    uint_t sdl,
                    uint_t index,
                    uint_t flags);

extern int rs_getinfo(rsethandle_t rseth,
                    rsinfo_t info_type,
                    uint_t flags);

extern int rs_info(void *out,
		    int command,
		    long arg1,
		    long arg2);

extern rsethandle_t rs_alloc(uint_t flags);

extern void rs_free(rsethandle_t rseth);

extern int rs_init(rsethandle_t rseth,
                    uint_t flags);

extern int rs_op(uint_t command,
                    rsethandle_t rseth1,
                    rsethandle_t rseth2,
                    uint_t flags,
                    uint_t id);

/* registry services */
extern int rs_registername(rsethandle_t rseth,
                    char *name_space,
                    char *name,
                    uint_t mode,
                    uint_t command);

extern int rs_getnameattr(char *name_space,
                    char *name,
                    rs_attributes_t *attr);

extern int rs_setnameattr(char *name_space,
                    char *name,
                    uint_t command,
                    rs_attributes_t *attr);

extern int rs_discardname(char *name_space,
                    char *name);

extern int rs_getnamedrset(char *name_space,
                    char *name,
                    rsethandle_t rseth);

extern int rs_getrsetnames (struct rsetinfo *buffer,
		    uint_t buffersize);

/* attachment services */
extern pid_t ra_fork(rstype_t rstype_resource,
                    rsid_t rsid_resource,
                    uint_t flags);

extern int ra_execl(rstype_t, rsid_t, uint_t, const char *, ...);
extern int ra_execle(rstype_t, rsid_t, uint_t, const char *, ...);
extern int ra_execlp(rstype_t, rsid_t, uint_t, const char *, const char *, ...);
extern int ra_execv(rstype_t, rsid_t, uint_t, const char *, char *const[]);
extern int ra_execve(rstype_t, rsid_t, uint_t, const char *, char *const[],
                     char *const[]);
extern int ra_execvp(rstype_t, rsid_t, uint_t, const char *, char *const[]);
extern int ra_exect(rstype_t, rsid_t, uint_t, char *, char *[], char *[]);

extern int ra_attach(rstype_t rstype_work_component,
		    rsid_t rsid_work_component,
		    rstype_t rstype_resource,
		    rsid_t rsid_resource,
		    uint_t flags);

extern int ra_detach(rstype_t rstype_work_component,
		    rsid_t rsid_work_component,
		    rstype_t rstype_resource,
		    rsid_t rsid_rsource,
		    uint_t flags);

extern int ra_attachrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    rsethandle_t rseth,
                    uint_t flags);

extern int ra_detachrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    uint_t flags);

extern int ra_getrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    uint_t flags,
                    rsethandle_t rseth);

extern int ra_shmget(key_t key, size_t size, int shmflg, rstype_t rstype,
		     rsid_t rsid, uint_t policy);
extern int ra_shmgetv(key_t key, size_t size, int shmflg, int rangecnt,
		      subrange_t *user_subranges);

extern void * ra_mmap( void *addr, size_t len, int prot, int flags,
		       int fildes, off64_t off, rstype_t rstype,
		       rsid_t rsid, uint policy );
extern void * ra_mmapv( void *addr, size_t len, int prot, int flags,
		        int fildes, off64_t off, int rangecnt,
			subrange_t *user_subranges );


/* partition services */
extern int rs_getpartition(pid_t pid,
                    rsethandle_t rseth);

extern int rs_setpartition(pid_t pid,
                    rsethandle_t rseth,
                    uint_t flags);

extern int rs_getassociativity(uint_t type,
		    uint_t id,
		    uint_t *associativity_array,
		    uint_t associativity_array_size);

extern attachinfo_t *ra_get_attachinfo(rstype_t rstype,
                    rsid_t rsid,
                    off64_t offset,
                    size64_t length,
                    uint_t flags);    

extern int ra_free_attachinfo(attachinfo_t *info);

extern sradid_t rs_get_homesrad(void);
#endif /* _NO_PROTO */

#ifdef __cplusplus
}
#endif

#if defined(_KERNEL)

#if !defined(_H_PROCESSOR)
#include <sys/processor.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* Define an anonymous alias for the system rset */
#define RS_SYSRSET      NULL

extern int krs_numrads(rsethandle_t rseth,
                    uint_t sdl,
                    uint_t flags);

extern int krs_getrad(rsethandle_t rseth,
                    uint_t sdl,
                    uint_t index,
                    uint_t flags);

extern int krs_getinfo(rsethandle_t rseth,
                    rsinfo_t info_type,
                    uint_t flags,
		    int *result);

extern int krs_alloc(rsethandle_t *rseth,
                    uint_t flags);

extern void krs_free(rsethandle_t rseth);

extern int krs_init(rsethandle_t rseth,
                    uint_t flags);

extern int krs_op(uint_t command,
                    rsethandle_t rseth1,
                    rsethandle_t rseth2,
                    uint_t flags,
                    uint_t id);

extern int kra_creatp(pid_t *pid,
                    rstype_t rstype_resource,
                    rsid_t rsid_resource,
                    uint_t flags);

extern int kra_attachrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    rsethandle_t rseth,
                    uint_t flags);

extern int kra_detachrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    uint_t flags);

extern int kra_getrset(rstype_t rstype_work_component,
                    rsid_t rsid_work_component,
                    uint_t flags,
                    rsethandle_t rseth,
                    uint_t *rset_type);

extern int krs_getpartition(pid_t pid,
                    uint_t flags,
                    rsethandle_t rseth,
                    uint_t *rset_type);

extern int krs_setpartition(pid_t pid,
                    rsethandle_t rseth,
                    uint_t flags);

extern int krs_getassociativity(uint_t type,
                    uint_t l_id,        
                    uint_t *assoc,              
                    uint_t len);  

extern uint_t krs_radid(uint_t sdl);

extern sradid_t krs_get_homesrad(void);

#ifdef __cplusplus
}
#endif

#endif          /* _KERNEL */

#endif /*_H_RSET*/


