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 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Kernel Physical Mapping (kpm) segment driver (segkpm). 31 * 32 * This driver delivers along with the hat_kpm* interfaces an alternative 33 * mechanism for kernel mappings within the 64-bit Solaris operating system, 34 * which allows the mapping of all physical memory into the kernel address 35 * space at once. This is feasible in 64 bit kernels, e.g. for Ultrasparc II 36 * and beyond processors, since the available VA range is much larger than 37 * possible physical memory. Momentarily all physical memory is supported, 38 * that is represented by the list of memory segments (memsegs). 39 * 40 * Segkpm mappings have also very low overhead and large pages are used 41 * (when possible) to minimize the TLB and TSB footprint. It is also 42 * extentable for other than Sparc architectures (e.g. AMD64). Main 43 * advantage is the avoidance of the TLB-shootdown X-calls, which are 44 * normally needed when a kernel (global) mapping has to be removed. 45 * 46 * First example of a kernel facility that uses the segkpm mapping scheme 47 * is seg_map, where it is used as an alternative to hat_memload(). 48 * See also hat layer for more information about the hat_kpm* routines. 49 * The kpm facilty can be turned off at boot time (e.g. /etc/system). 50 */ 51 52 #include <sys/types.h> 53 #include <sys/param.h> 54 #include <sys/sysmacros.h> 55 #include <sys/systm.h> 56 #include <sys/vnode.h> 57 #include <sys/cmn_err.h> 58 #include <sys/debug.h> 59 #include <sys/thread.h> 60 #include <sys/cpuvar.h> 61 #include <sys/bitmap.h> 62 #include <sys/atomic.h> 63 64 #include <vm/seg_kmem.h> 65 #include <vm/seg_kpm.h> 66 #include <vm/hat.h> 67 #include <vm/as.h> 68 #include <vm/seg.h> 69 #include <vm/page.h> 70 71 /* 72 * Global kpm controls. 73 * See also platform and mmu specific controls. 74 * 75 * kpm_enable -- global on/off switch for segkpm. 76 * . Set by default on 64bit platforms that have kpm support. 77 * . Will be disabled from platform layer if not supported. 78 * . Can be disabled via /etc/system. 79 * 80 * kpm_smallpages -- use only regular/system pagesize for kpm mappings. 81 * . Can be useful for critical debugging of kpm clients. 82 * . Set to zero by default for platforms that support kpm large pages. 83 * The use of kpm large pages reduces the footprint of kpm meta data 84 * and has all the other advantages of using large pages (e.g TLB 85 * miss reduction). 86 * . Set by default for platforms that don't support kpm large pages or 87 * where large pages cannot be used for other reasons (e.g. there are 88 * only few full associative TLB entries available for large pages). 89 * 90 * segmap_kpm -- separate on/off switch for segmap using segkpm: 91 * . Set by default. 92 * . Will be disabled when kpm_enable is zero. 93 * . Will be disabled when MAXBSIZE != PAGESIZE. 94 * . Can be disabled via /etc/system. 95 * 96 */ 97 int kpm_enable = 1; 98 int kpm_smallpages = 0; 99 int segmap_kpm = 1; 100 101 /* 102 * Private seg op routines. 103 */ 104 faultcode_t segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr, 105 size_t len, enum fault_type type, enum seg_rw rw); 106 static void segkpm_dump(struct seg *); 107 static void segkpm_badop(void); 108 static int segkpm_notsup(void); 109 110 #define SEGKPM_BADOP(t) (t(*)())segkpm_badop 111 #define SEGKPM_NOTSUP (int(*)())segkpm_notsup 112 113 static struct seg_ops segkpm_ops = { 114 SEGKPM_BADOP(int), /* dup */ 115 SEGKPM_BADOP(int), /* unmap */ 116 SEGKPM_BADOP(void), /* free */ 117 segkpm_fault, 118 SEGKPM_BADOP(int), /* faulta */ 119 SEGKPM_BADOP(int), /* setprot */ 120 SEGKPM_BADOP(int), /* checkprot */ 121 SEGKPM_BADOP(int), /* kluster */ 122 SEGKPM_BADOP(size_t), /* swapout */ 123 SEGKPM_BADOP(int), /* sync */ 124 SEGKPM_BADOP(size_t), /* incore */ 125 SEGKPM_BADOP(int), /* lockop */ 126 SEGKPM_BADOP(int), /* getprot */ 127 SEGKPM_BADOP(u_offset_t), /* getoffset */ 128 SEGKPM_BADOP(int), /* gettype */ 129 SEGKPM_BADOP(int), /* getvp */ 130 SEGKPM_BADOP(int), /* advise */ 131 segkpm_dump, /* dump */ 132 SEGKPM_NOTSUP, /* pagelock */ 133 SEGKPM_BADOP(int), /* setpgsz */ 134 SEGKPM_BADOP(int), /* getmemid */ 135 }; 136 137 /* 138 * kpm_pgsz and kpm_pgshft are set by platform layer. 139 */ 140 size_t kpm_pgsz; /* kpm page size */ 141 uint_t kpm_pgshft; /* kpm page shift */ 142 u_offset_t kpm_pgoff; /* kpm page offset mask */ 143 uint_t kpmp2pshft; /* kpm page to page shift */ 144 pgcnt_t kpmpnpgs; /* how many pages per kpm page */ 145 146 147 #ifdef SEGKPM_SUPPORT 148 149 int 150 segkpm_create(struct seg *seg, void *argsp) 151 { 152 struct segkpm_data *skd; 153 struct segkpm_crargs *b = (struct segkpm_crargs *)argsp; 154 ushort_t *p; 155 int i, j; 156 157 ASSERT(seg->s_as && RW_WRITE_HELD(&seg->s_as->a_lock)); 158 ASSERT(btokpmp(seg->s_size) >= 1 && 159 kpmpageoff((uintptr_t)seg->s_base) == 0 && 160 kpmpageoff((uintptr_t)seg->s_base + seg->s_size) == 0); 161 162 skd = kmem_zalloc(sizeof (struct segkpm_data), KM_SLEEP); 163 164 seg->s_data = (void *)skd; 165 seg->s_ops = &segkpm_ops; 166 skd->skd_prot = b->prot; 167 168 /* 169 * (1) Segkpm virtual addresses are based on physical adresses. 170 * From this and in opposite to other segment drivers it is 171 * often required to allocate a page first to be able to 172 * calculate the final segkpm virtual address. 173 * (2) Page allocation is done by calling page_create_va(), 174 * one important input argument is a virtual address (also 175 * expressed by the "va" in the function name). This function 176 * is highly optimized to select the right page for an optimal 177 * processor and platform support (e.g. virtual addressed 178 * caches (VAC), physical addressed caches, NUMA). 179 * 180 * Because of (1) the approach is to generate a faked virtual 181 * address for calling page_create_va(). In order to exploit 182 * the abilities of (2), especially to utilize the cache 183 * hierarchy (3) and to avoid VAC alias conflicts (4) the 184 * selection has to be done carefully. For each virtual color 185 * a separate counter is provided (4). The count values are 186 * used for the utilization of all cache lines (3) and are 187 * corresponding to the cache bins. 188 */ 189 skd->skd_nvcolors = b->nvcolors; 190 191 p = skd->skd_va_select = 192 kmem_zalloc(NCPU * b->nvcolors * sizeof (ushort_t), KM_SLEEP); 193 194 for (i = 0; i < NCPU; i++) 195 for (j = 0; j < b->nvcolors; j++, p++) 196 *p = j; 197 198 return (0); 199 } 200 201 /* 202 * This routine is called via a machine specific fault handling 203 * routine. 204 */ 205 /* ARGSUSED */ 206 faultcode_t 207 segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len, 208 enum fault_type type, enum seg_rw rw) 209 { 210 faultcode_t error; 211 212 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 213 214 error = hat_kpm_fault(hat, addr); 215 216 return (error); 217 } 218 219 #define addr_to_vcolor(addr, vcolors) \ 220 ((int)(((uintptr_t)(addr) & ((vcolors << PAGESHIFT) - 1)) >> PAGESHIFT)) 221 222 /* 223 * Create a virtual address that can be used for invocations of 224 * page_create_va. Goal is to utilize the cache hierarchy (round 225 * robin bins) and to select the right color for virtual indexed 226 * caches. It isn't exact since we also increment the bin counter 227 * when the caller uses VOP_GETPAGE and gets a hit in the page 228 * cache, but we keep the bins turning for cache distribution 229 * (see also segkpm_create block comment). 230 */ 231 caddr_t 232 segkpm_create_va(u_offset_t off) 233 { 234 int vcolor; 235 ushort_t *p; 236 struct segkpm_data *skd = (struct segkpm_data *)segkpm->s_data; 237 int nvcolors = skd->skd_nvcolors; 238 caddr_t va; 239 240 vcolor = (nvcolors > 1) ? addr_to_vcolor(off, nvcolors) : 0; 241 p = &skd->skd_va_select[(CPU->cpu_id * nvcolors) + vcolor]; 242 va = (caddr_t)ptob(*p); 243 244 atomic_add_16(p, nvcolors); 245 246 return (va); 247 } 248 249 /* 250 * Unload mapping if the instance has an active kpm mapping. 251 */ 252 void 253 segkpm_mapout_validkpme(struct kpme *kpme) 254 { 255 caddr_t vaddr; 256 page_t *pp; 257 258 retry: 259 if ((pp = kpme->kpe_page) == NULL) { 260 return; 261 } 262 263 if (page_lock(pp, SE_SHARED, (kmutex_t *)NULL, P_RECLAIM) == 0) 264 goto retry; 265 266 /* 267 * Check if segkpm mapping is not unloaded in the meantime 268 */ 269 if (kpme->kpe_page == NULL) { 270 page_unlock(pp); 271 return; 272 } 273 274 vaddr = hat_kpm_page2va(pp, 1); 275 hat_kpm_mapout(pp, kpme, vaddr); 276 page_unlock(pp); 277 } 278 279 static void 280 segkpm_badop() 281 { 282 panic("segkpm_badop"); 283 } 284 285 #else /* SEGKPM_SUPPORT */ 286 287 /* segkpm stubs */ 288 289 /*ARGSUSED*/ 290 int segkpm_create(struct seg *seg, void *argsp) { return (0); } 291 292 /* ARGSUSED */ 293 faultcode_t 294 segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len, 295 enum fault_type type, enum seg_rw rw) 296 { 297 return ((faultcode_t)0); 298 } 299 300 /* ARGSUSED */ 301 caddr_t segkpm_create_va(u_offset_t off) { return (NULL); } 302 303 /* ARGSUSED */ 304 void segkpm_mapout_validkpme(struct kpme *kpme) {} 305 306 static void 307 segkpm_badop() {} 308 309 #endif /* SEGKPM_SUPPORT */ 310 311 static int 312 segkpm_notsup() 313 { 314 return (ENOTSUP); 315 } 316 317 /* 318 * segkpm pages are not dumped, so we just return 319 */ 320 /*ARGSUSED*/ 321 static void 322 segkpm_dump(struct seg *seg) 323 {} 324