1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef _VM_SEG_KP_H
  28 #define _VM_SEG_KP_H
  29 
  30 #pragma ident   "%Z%%M% %I%     %E% SMI"
  31 
  32 /*
  33  * segkp (as in kernel pageable) is a segment driver that supports allocation
  34  * of page-aligned variable size of vm resources.
  35  *
  36  * Each vm resource represents a page-aligned range of virtual addresses.
  37  * The caller may specify whether the resource should include a redzone,
  38  * be locked down, or be zero initialized.
  39  */
  40 
  41 #include <vm/seg.h>
  42 #include <sys/vmem.h>
  43 
  44 #ifdef  __cplusplus
  45 extern "C" {
  46 #endif
  47 
  48 #ifdef  _KERNEL
  49 
  50 /*
  51  * Private information per overall segkp segment (as opposed
  52  * to per resource within segment). There are as many anon slots
  53  * allocated as there there are pages in the segment.
  54  */
  55 struct segkp_segdata {
  56         struct anon_hdr *kpsd_anon;     /* anon structs */
  57         vmem_t          *kpsd_arena;    /* virtual memory descriptor */
  58         struct segkp_data **kpsd_hash;  /* Hash table for lookups */
  59 };
  60 
  61 #define SEGKP_VMEM(seg) (((struct segkp_segdata *)(seg)->s_data)->kpsd_arena)
  62 
  63 /*
  64  * A hash table is used to aid in the lookup of a kpd's based on vaddr.
  65  * Since the heaviest use of segkp occurs from segkp_*get and segkp_*release,
  66  * the hashing is based on the vaddr used by these routines.
  67  */
  68 #define SEGKP_HASHSZ            256     /* power of two */
  69 #define SEGKP_HASHMASK          (SEGKP_HASHSZ - 1)
  70 #define SEGKP_HASH(vaddr)       \
  71         ((int)(((uintptr_t)vaddr >> PAGESHIFT) & SEGKP_HASHMASK))
  72 
  73 struct segkp_data {
  74         kmutex_t        kp_lock;        /* per resource lock */
  75         caddr_t         kp_base;        /* starting addr of chunk */
  76         size_t          kp_len;         /* # of bytes */
  77         uint_t          kp_flags;       /* state info */
  78         int             kp_cookie;      /* index into cache array */
  79         ulong_t         kp_anon_idx;    /* index into main anon array */
  80                                         /* in segkp_segdata */
  81         struct anon_hdr *kp_anon;       /* anon structs */
  82         struct segkp_data *kp_next;     /* ptr to next in hash chain */
  83 };
  84 
  85 /*
  86  * Flag bits
  87  *
  88  */
  89 #define KPD_ZERO        0x01            /* initialize resource with 0 */
  90 #define KPD_LOCKED      0x02            /* resources locked */
  91 #define KPD_NO_ANON     0x04            /* no swap resources required */
  92 #define KPD_HASREDZONE  0x08            /* include a redzone */
  93 #define KPD_NOWAIT      0x10            /* do not wait for res. if unavail. */
  94 #define KPD_WRITEDIRTY  0x20            /* dirty pages should be flushed */
  95 #define KPD_HASAMP      0x40            /* anon_hdr managed by caller */
  96 
  97 /*
  98  * A cache of segkp elements may be created via segkp_cache_init().
  99  * The elements on the freelist all have the same len and flags value.
 100  * The cookie passed to the client is an index into the freelist array.
 101  */
 102 struct segkp_cache  {
 103         int             kpf_max;                /* max # of elements allowed */
 104         int             kpf_count;              /* current no. of elments */
 105         int             kpf_inuse;              /* list inuse */
 106         uint_t          kpf_flags;              /* seg_kp flag value */
 107         size_t          kpf_len;                /* len of resource */
 108         struct seg      *kpf_seg;               /* segment */
 109         struct segkp_data *kpf_list;            /* list of kpd's */
 110 };
 111 #define SEGKP_MAX_CACHE         4       /* Number of caches maintained */
 112 
 113 /*
 114  * Define redzone, and stack_to_memory macros.
 115  * The redzone is PAGESIZE bytes.
 116  */
 117 #ifdef  STACK_GROWTH_DOWN
 118 #define KPD_REDZONE(kpd)        (0)
 119 #define stom(v, flags)  (((flags) & KPD_HASREDZONE) ? (v) + PAGESIZE : (v))
 120 
 121 #else   /* STACK_GROWTH_DOWN */
 122 
 123 #define KPD_REDZONE(kpd) (btop(kpd->kp_len) - 1)
 124 #define stom(v) (v)
 125 #endif  /* STACK_GROWTH_DOWN */
 126 
 127 #define SEGKP_MAPLEN(len, flags) \
 128         (((flags) & KPD_HASREDZONE) ? (len) - PAGESIZE : (len))
 129 
 130 extern  struct seg *segkp;
 131 /* If segkp becomes more than one seg this test will need changing. */
 132 #define SEG_IS_SEGKP(SEG)       ((SEG) == segkp)
 133 
 134 /*
 135  * Public routine declarations not part of the segment ops vector go here.
 136  */
 137 int     segkp_create(struct seg *seg);
 138 caddr_t segkp_get(struct seg *seg, size_t len, uint_t flags);
 139 void    segkp_release(struct seg *seg, caddr_t vaddr);
 140 void *  segkp_cache_init(struct seg *seg, int maxsize, size_t len,
 141                 uint_t flags);
 142 void    segkp_cache_free();
 143 caddr_t segkp_cache_get(void *cookie);
 144 int     segkp_map_red(void);
 145 void    segkp_unmap_red(void);
 146 size_t  swapsize(caddr_t v);
 147 
 148 /* Special currently only used by schedctl. */
 149 struct anon_map;        /* Make the compiler happy about the next line. */
 150 caddr_t segkp_get_withanonmap(struct seg *, size_t, uint_t, struct anon_map *);
 151 
 152 /*
 153  * We allow explicit calls to segkp_fault, even though it's part
 154  * of the segkp ops vector.
 155  */
 156 faultcode_t segkp_fault(struct hat *hat, struct seg *seg, caddr_t addr,
 157         size_t len, enum fault_type type, enum seg_rw rw);
 158 
 159 #endif  /* _KERNEL */
 160 
 161 #ifdef  __cplusplus
 162 }
 163 #endif
 164 
 165 #endif  /* _VM_SEG_KP_H */