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 2003 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef __ZULU_HAT_INCL__
  28 #define __ZULU_HAT_INCL__
  29 
  30 #pragma ident   "%Z%%M% %I%     %E% SMI"
  31 
  32 #ifdef  __cplusplus
  33 extern "C" {
  34 #endif
  35 
  36 #define ZULU_TTE8K              0
  37 #define ZULU_TTE64K             1
  38 #define ZULU_TTE512K            2
  39 #define ZULU_TTE4M              3
  40 #define ZULUM_MAX_PG_SIZES      4
  41 
  42 #define ZULU_CTX_MASK           0x1fff
  43 
  44 #ifndef _ASM
  45 
  46 #include <sys/types.h>
  47 #include <sys/atomic.h>
  48 #include <vm/xhat.h>
  49 #include <sys/avl.h>
  50 
  51 
  52 #define ZULU_HAT_BP_SHIFT               13
  53 #define ZULU_HAT_SZ_SHIFT(sz)           ((sz) * 3)
  54 #define ZULU_HAT_NUM_PGS(sz)            (1<<ZULU_HAT_SZ_SHIFT(sz))
  55 #define ZULU_HAT_PGSHIFT(s)             (ZULU_HAT_BP_SHIFT + \
  56                                         ZULU_HAT_SZ_SHIFT(s))
  57 #define ZULU_HAT_PGSZ(s)                ((uint64_t)1<<ZULU_HAT_PGSHIFT(s))
  58 #define ZULU_HAT_PGOFFSET(s)            (ZULU_HAT_PGSZ(s) - 1)
  59 #define ZULU_HAT_PGMASK(s)              (~ZULU_HAT_PGOFFSET((uint64_t)s))
  60 #define ZULU_HAT_PGADDR(a, s)           ((uintptr_t)(a) & ZULU_HAT_PGMASK(s))
  61 #define ZULU_HAT_PGADDROFF(a, s)        ((uintptr_t)(a) & ZULU_HAT_PGOFFSET(s))
  62 #define ZULU_HAT_PGDIFF(a, s)           (ZULU_HAT_PGSZ(s) - \
  63                                         ZULU_HAT_PGADDROFF(a, s))
  64 
  65 #define ZULU_HAT_PFN_MASK(sz)           ((1 << ZULU_HAT_SZ_SHIFT(sz)) - 1)
  66 #define ZULU_HAT_ADJ_PFN(ttep, vaddr) \
  67         ((ttep->zulu_tte_pfn & ~ZULU_HAT_PFN_MASK(ttep->zulu_tte_size)) | \
  68         (((uintptr_t)vaddr >> ZULU_HAT_BP_SHIFT) & \
  69         ZULU_HAT_PFN_MASK(ttep->zulu_tte_size)))
  70 
  71 /*
  72  * zulu_ctx_tab is an array of pointers to zulu hat structures.
  73  * since the addresses are 8 byte aligned we use bit 0 as a lock flag.
  74  * This will synchronize TL1 access to the tsb and the mappings.
  75  */
  76 
  77 #define ZULU_CTX_LOCK   0x1
  78 
  79 #define ZULU_CTX_LOCK_INIT(c)           zulu_ctx_tab[c] = NULL
  80 #define ZULU_CTX_IS_FREE(c)             (zulu_ctx_tab[c] == NULL)
  81 #define ZULU_CTX_SET_HAT(c, h)          zulu_ctx_tab[c] = h
  82 
  83 #define ZULU_CTX_GET_HAT(c)     (struct zulu_hat *)((uint64_t) \
  84                                     zulu_ctx_tab[c] & ~ZULU_CTX_LOCK)
  85 
  86 struct zulu_tag {
  87         uint64_t        zulu_tag_page:51;       /* [63:13] vpage */
  88 };
  89 
  90 struct zulu_tte {
  91         union {
  92                 struct zulu_tag zulu_tte_tag;
  93                 uint64_t        zulu_tte_addr;
  94         } un;
  95         uint_t  zulu_tte_valid  :1;
  96         uint_t  zulu_tte_perm   :1;
  97         uint_t  zulu_tte_size   :3;
  98         uint_t  zulu_tte_locked :1;
  99         uint_t  zulu_tte_pfn;
 100 };
 101 
 102 /*
 103  * zulu hat stores its list of translation in a hash table.
 104  * TODO: size this table. 256 buckets may be too small.
 105  */
 106 #define ZULU_HASH_TBL_NUM  0x100
 107 #define ZULU_HASH_TBL_MASK (ZULU_HASH_TBL_NUM - 1)
 108 #define ZULU_HASH_TBL_SHIFT(_s) (ZULU_HAT_BP_SHIFT + (3 * _s))
 109 #define ZULU_HASH_TBL_SZ  (ZULU_HASH_TBL_NUM * sizeof (struct zulu_hat_blk *))
 110 #define ZULU_MAP_HASH_VAL(_v, _s) (((_v) >> ZULU_HASH_TBL_SHIFT(_s)) & \
 111                                                         ZULU_HASH_TBL_MASK)
 112 #define ZULU_MAP_HASH_HEAD(_zh, _v, _s) \
 113                 (_zh->hash_tbl[ZULU_MAP_HASH_VAL(_v, _s)])
 114 
 115 /*
 116  *
 117  * TODO: need finalize the number of entries in the TSB
 118  * 32K tsb entries caused us to never get a tsb miss that didn't cause
 119  * a page fault.
 120  *
 121  * Reducing TSB_NUM to 512 entries caused tsb_miss > tsb_hit
 122  * in a yoyo run.
 123  */
 124 #define ZULU_TSB_NUM            4096
 125 #define ZULU_TSB_SZ             (ZULU_TSB_NUM * sizeof (struct zulu_tte))
 126 #define ZULU_TSB_HASH(a, ts, s) (((uintptr_t)(a) >> ZULU_HAT_PGSHIFT(ts)) & \
 127                                         (s-1))
 128 
 129 #define ZULU_VADDR(tag)         (tag & ~ZULU_CTX_MASK)
 130 #define ZULU_TTE_TO_PAGE(a)     a.un.zulu_tte_tag.zulu_tag_page
 131 
 132 
 133 struct zulu_hat_blk {
 134         struct zulu_hat_blk     *zulu_hash_next;
 135         struct zulu_hat_blk     *zulu_hash_prev;
 136         struct zulu_shadow_blk  *zulu_shadow_blk;
 137         struct zulu_tte         zulu_hat_blk_tte;
 138 };
 139 
 140 #define zulu_hat_blk_vaddr      zulu_hat_blk_tte.un.zulu_tte_addr
 141 #define zulu_hat_blk_pfn        zulu_hat_blk_tte.zulu_tte_pfn
 142 #define zulu_hat_blk_page       ZULU_TTE_TO_PAGE(zulu_hat_blk_tte)
 143 #define zulu_hat_blk_perm       zulu_hat_blk_tte.zulu_tte_perm
 144 #define zulu_hat_blk_size       zulu_hat_blk_tte.zulu_tte_size
 145 #define zulu_hat_blk_valid      zulu_hat_blk_tte.zulu_tte_valid
 146 
 147 /*
 148  * for fast lookups by address, len we use an avl list to shadow occupied
 149  * 4Mb regions that have mappings.
 150  */
 151 #define ZULU_SHADOW_BLK_RANGE 0x400000
 152 #define ZULU_SHADOW_BLK_MASK (~(ZULU_SHADOW_BLK_RANGE - 1))
 153 
 154 struct zulu_shadow_blk {
 155         avl_node_t      link;           /* must be at beginning of struct */
 156         uint64_t        ivaddr;         /* base address of this node */
 157         uint64_t        ref_count;
 158         uint64_t        min_addr;
 159         uint64_t        max_addr;
 160 };
 161 #define ZULU_SHADOW_BLK_LINK_OFFSET (0)
 162 
 163 struct zulu_hat {
 164         struct xhat             zulu_xhat;
 165         kmutex_t                lock;
 166         avl_tree_t              shadow_tree;
 167         char                    magic;      /* =42 to mark our data for mdb */
 168         unsigned                in_fault  : 1;
 169         unsigned                freed     : 1;
 170         unsigned                map8k     : 1;
 171         unsigned                map64k    : 1;
 172         unsigned                map512k   : 1;
 173         unsigned                map4m     : 1;
 174         short                   zulu_ctx;
 175         unsigned short          zulu_tsb_size;  /* TODO why not a constant? */
 176         struct zulu_hat_blk     **hash_tbl;
 177         struct zulu_tte         *zulu_tsb;
 178         struct zulu_shadow_blk  *sblk_last;     /* last sblk looked up */
 179         uint64_t                fault_ivaddr_last; /* last translation loaded */
 180         caddr_t                 vaddr_max;
 181         hrtime_t                last_used;
 182         void                    *zdev;
 183 };
 184 
 185 #define ZULU_HAT2AS(h)  ((h)->zulu_xhat.xhat_as)
 186 
 187 /*
 188  * Assembly language function for TSB lookups
 189  */
 190 uint64_t zulu_hat_tsb_lookup_tl0(struct zulu_hat *zhat, caddr_t vaddr);
 191 
 192 /*
 193  * zuluvm's interface to zulu_hat
 194  */
 195 
 196 int zulu_hat_load(struct zulu_hat *zhat, caddr_t vaddr, enum seg_rw rw, int *);
 197 
 198 int zulu_hat_init();
 199 int zulu_hat_destroy();
 200 int zulu_hat_attach(void *arg);
 201 int zulu_hat_detach(void *arg);
 202 struct zulu_hat *zulu_hat_proc_attach(struct as *as, void *zdev);
 203 void zulu_hat_proc_detach(struct zulu_hat *zhat);
 204 
 205 void zulu_hat_validate_ctx(struct zulu_hat *zhat);
 206 void zulu_hat_terminate(struct zulu_hat *zhat);
 207 
 208 #endif /* _ASM */
 209 
 210 #ifdef  __cplusplus
 211 }
 212 #endif
 213 
 214 #endif  /* __ZULU_HAT_INCL__ */