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 /*
  31  * segkp (as in kernel pageable) is a segment driver that supports allocation
  32  * of page-aligned variable size of vm resources.
  33  *
  34  * Each vm resource represents a page-aligned range of virtual addresses.
  35  * The caller may specify whether the resource should include a redzone,
  36  * be locked down, or be zero initialized.
  37  */
  38 
  39 #include <vm/seg.h>
  40 #include <sys/vmem.h>
  41 
  42 #ifdef  __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 #ifdef  _KERNEL
  47 
  48 /*
  49  * Private information per overall segkp segment (as opposed
  50  * to per resource within segment). There are as many anon slots
  51  * allocated as there there are pages in the segment.
  52  */
  53 struct segkp_segdata {
  54         struct anon_hdr *kpsd_anon;     /* anon structs */
  55         vmem_t          *kpsd_arena;    /* virtual memory descriptor */
  56         struct segkp_data **kpsd_hash;  /* Hash table for lookups */
  57 };
  58 
  59 #define SEGKP_VMEM(seg) (((struct segkp_segdata *)(seg)->s_data)->kpsd_arena)
  60 
  61 /*
  62  * A hash table is used to aid in the lookup of a kpd's based on vaddr.
  63  * Since the heaviest use of segkp occurs from segkp_*get and segkp_*release,
  64  * the hashing is based on the vaddr used by these routines.
  65  */
  66 #define SEGKP_HASHSZ            256     /* power of two */
  67 #define SEGKP_HASHMASK          (SEGKP_HASHSZ - 1)
  68 #define SEGKP_HASH(vaddr)       \
  69         ((int)(((uintptr_t)vaddr >> PAGESHIFT) & SEGKP_HASHMASK))
  70 
  71 struct segkp_data {
  72         kmutex_t        kp_lock;        /* per resource lock */
  73         caddr_t         kp_base;        /* starting addr of chunk */
  74         size_t          kp_len;         /* # of bytes */
  75         uint_t          kp_flags;       /* state info */
  76         int             kp_cookie;      /* index into cache array */
  77         ulong_t         kp_anon_idx;    /* index into main anon array */
  78                                         /* in segkp_segdata */
  79         struct anon_hdr *kp_anon;       /* anon structs */
  80         struct segkp_data *kp_next;     /* ptr to next in hash chain */
  81 };
  82 
  83 /*
  84  * Flag bits
  85  *
  86  */
  87 #define KPD_ZERO        0x01            /* initialize resource with 0 */
  88 #define KPD_LOCKED      0x02            /* resources locked */
  89 #define KPD_NO_ANON     0x04            /* no swap resources required */
  90 #define KPD_HASREDZONE  0x08            /* include a redzone */
  91 #define KPD_NOWAIT      0x10            /* do not wait for res. if unavail. */
  92 #define KPD_WRITEDIRTY  0x20            /* dirty pages should be flushed */
  93 #define KPD_HASAMP      0x40            /* anon_hdr managed by caller */
  94 
  95 /*
  96  * A cache of segkp elements may be created via segkp_cache_init().
  97  * The elements on the freelist all have the same len and flags value.
  98  * The cookie passed to the client is an index into the freelist array.
  99  */
 100 struct segkp_cache  {
 101         int             kpf_max;                /* max # of elements allowed */
 102         int             kpf_count;              /* current no. of elments */
 103         int             kpf_inuse;              /* list inuse */
 104         uint_t          kpf_flags;              /* seg_kp flag value */
 105         size_t          kpf_len;                /* len of resource */
 106         struct seg      *kpf_seg;               /* segment */
 107         struct segkp_data *kpf_list;            /* list of kpd's */
 108 };
 109 #define SEGKP_MAX_CACHE         4       /* Number of caches maintained */
 110 
 111 /*
 112  * Define redzone, and stack_to_memory macros.
 113  * The redzone is PAGESIZE bytes.
 114  */
 115 #ifdef  STACK_GROWTH_DOWN
 116 #define KPD_REDZONE(kpd)        (0)
 117 #define stom(v, flags)  (((flags) & KPD_HASREDZONE) ? (v) + PAGESIZE : (v))
 118 
 119 #else   /* STACK_GROWTH_DOWN */
 120 
 121 #define KPD_REDZONE(kpd) (btop(kpd->kp_len) - 1)
 122 #define stom(v) (v)
 123 #endif  /* STACK_GROWTH_DOWN */
 124 
 125 #define SEGKP_MAPLEN(len, flags) \
 126         (((flags) & KPD_HASREDZONE) ? (len) - PAGESIZE : (len))
 127 
 128 extern  struct seg *segkp;
 129 /* If segkp becomes more than one seg this test will need changing. */
 130 #define SEG_IS_SEGKP(SEG)       ((SEG) == segkp)
 131 
 132 /*
 133  * Public routine declarations not part of the segment ops vector go here.
 134  */
 135 int     segkp_create(struct seg *seg);
 136 caddr_t segkp_get(struct seg *seg, size_t len, uint_t flags);
 137 void    segkp_release(struct seg *seg, caddr_t vaddr);
 138 void *  segkp_cache_init(struct seg *seg, int maxsize, size_t len,
 139                 uint_t flags);
 140 void    segkp_cache_free();
 141 caddr_t segkp_cache_get(void *cookie);
 142 int     segkp_map_red(void);
 143 void    segkp_unmap_red(void);
 144 
 145 /* Special currently only used by schedctl. */
 146 struct anon_map;        /* Make the compiler happy about the next line. */
 147 caddr_t segkp_get_withanonmap(struct seg *, size_t, uint_t, struct anon_map *);
 148 
 149 /*
 150  * We allow explicit calls to segkp_fault, even though it's part
 151  * of the segkp ops vector.
 152  */
 153 faultcode_t segkp_fault(struct hat *hat, struct seg *seg, caddr_t addr,
 154         size_t len, enum fault_type type, enum seg_rw rw);
 155 
 156 #endif  /* _KERNEL */
 157 
 158 #ifdef  __cplusplus
 159 }
 160 #endif
 161 
 162 #endif  /* _VM_SEG_KP_H */