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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * Portions of this source code were derived from Berkeley 4.3 BSD 31 * under license from the Regents of the University of California. 32 */ 33 34 /* 35 * VM - segment for non-faulting loads. 36 */ 37 38 #include <sys/types.h> 39 #include <sys/t_lock.h> 40 #include <sys/param.h> 41 #include <sys/mman.h> 42 #include <sys/errno.h> 43 #include <sys/kmem.h> 44 #include <sys/cmn_err.h> 45 #include <sys/vnode.h> 46 #include <sys/proc.h> 47 #include <sys/conf.h> 48 #include <sys/debug.h> 49 #include <sys/archsystm.h> 50 #include <sys/lgrp.h> 51 52 #include <vm/page.h> 53 #include <vm/hat.h> 54 #include <vm/as.h> 55 #include <vm/seg.h> 56 #include <vm/vpage.h> 57 58 /* 59 * Private seg op routines. 60 */ 61 static int segnf_dup(struct seg *seg, struct seg *newseg); 62 static int segnf_unmap(struct seg *seg, caddr_t addr, size_t len); 63 static void segnf_free(struct seg *seg); 64 static faultcode_t segnf_nomap(void); 65 static int segnf_setprot(struct seg *seg, caddr_t addr, 66 size_t len, uint_t prot); 67 static int segnf_checkprot(struct seg *seg, caddr_t addr, 68 size_t len, uint_t prot); 69 static void segnf_badop(void); 70 static int segnf_nop(void); 71 static int segnf_getprot(struct seg *seg, caddr_t addr, 72 size_t len, uint_t *protv); 73 static u_offset_t segnf_getoffset(struct seg *seg, caddr_t addr); 74 static int segnf_gettype(struct seg *seg, caddr_t addr); 75 static int segnf_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp); 76 static void segnf_dump(struct seg *seg); 77 static int segnf_pagelock(struct seg *seg, caddr_t addr, size_t len, 78 struct page ***ppp, enum lock_type type, enum seg_rw rw); 79 static int segnf_setpagesize(struct seg *seg, caddr_t addr, size_t len, 80 uint_t szc); 81 82 83 struct seg_ops segnf_ops = { 84 .dup = segnf_dup, 85 .unmap = segnf_unmap, 86 .free = segnf_free, 87 .fault = (faultcode_t (*)(struct hat *, struct seg *, caddr_t, 88 size_t, enum fault_type, enum seg_rw))segnf_nomap, 89 .faulta = (faultcode_t (*)(struct seg *, caddr_t)) segnf_nomap, 90 .setprot = segnf_setprot, 91 .checkprot = segnf_checkprot, 92 .kluster = (int (*)())segnf_badop, 93 .sync = (int (*)(struct seg *, caddr_t, size_t, int, uint_t)) 94 segnf_nop, 95 .incore = (size_t (*)(struct seg *, caddr_t, size_t, char *)) 96 segnf_nop, 97 .lockop = (int (*)(struct seg *, caddr_t, size_t, int, int, 98 ulong_t *, size_t))segnf_nop, 99 .getprot = segnf_getprot, 100 .getoffset = segnf_getoffset, 101 .gettype = segnf_gettype, 102 .getvp = segnf_getvp, 103 .advise = (int (*)(struct seg *, caddr_t, size_t, uint_t)) 104 segnf_nop, 105 .dump = segnf_dump, 106 .pagelock = segnf_pagelock, 107 .setpagesize = segnf_setpagesize, 108 }; 109 110 /* 111 * vnode and page for the page of zeros we use for the nf mappings. 112 */ 113 static kmutex_t segnf_lock; 114 static struct vnode nfvp; 115 static struct page **nfpp; 116 117 #define addr_to_vcolor(addr) \ 118 (shm_alignment) ? \ 119 ((int)(((uintptr_t)(addr) & (shm_alignment - 1)) >> PAGESHIFT)) : 0 120 121 /* 122 * We try to limit the number of Non-fault segments created. 123 * Non fault segments are created to optimize sparc V9 code which uses 124 * the sparc nonfaulting load ASI (ASI_PRIMARY_NOFAULT). 125 * 126 * There are several reasons why creating too many non-fault segments 127 * could cause problems. 128 * 129 * First, excessive allocation of kernel resources for the seg 130 * structures and the HAT data to map the zero pages. 131 * 132 * Secondly, creating nofault segments actually uses up user virtual 133 * address space. This makes it unavailable for subsequent mmap(0, ...) 134 * calls which use as_gap() to find empty va regions. Creation of too 135 * many nofault segments could thus interfere with the ability of the 136 * runtime linker to load a shared object. 137 */ 138 #define MAXSEGFORNF (10000) 139 #define MAXNFSEARCH (5) 140 141 142 /* 143 * Must be called from startup() 144 */ 145 void 146 segnf_init() 147 { 148 mutex_init(&segnf_lock, NULL, MUTEX_DEFAULT, NULL); 149 } 150 151 152 /* 153 * Create a no-fault segment. 154 * 155 * The no-fault segment is not technically necessary, as the code in 156 * nfload() in trap.c will emulate the SPARC instruction and load 157 * a value of zero in the destination register. 158 * 159 * However, this code tries to put a page of zero's at the nofault address 160 * so that subsequent non-faulting loads to the same page will not 161 * trap with a tlb miss. 162 * 163 * In order to help limit the number of segments we merge adjacent nofault 164 * segments into a single segment. If we get a large number of segments 165 * we'll also try to delete a random other nf segment. 166 */ 167 /* ARGSUSED */ 168 int 169 segnf_create(struct seg *seg, void *argsp) 170 { 171 uint_t prot; 172 pgcnt_t vacpgs; 173 u_offset_t off = 0; 174 caddr_t vaddr = NULL; 175 int i, color; 176 struct seg *s1; 177 struct seg *s2; 178 size_t size; 179 struct as *as = seg->s_as; 180 181 ASSERT(as && AS_WRITE_HELD(as, &as->a_lock)); 182 183 /* 184 * Need a page per virtual color or just 1 if no vac. 185 */ 186 mutex_enter(&segnf_lock); 187 if (nfpp == NULL) { 188 struct seg kseg; 189 190 vacpgs = 1; 191 if (shm_alignment > PAGESIZE) { 192 vacpgs = shm_alignment >> PAGESHIFT; 193 } 194 195 nfpp = kmem_alloc(sizeof (*nfpp) * vacpgs, KM_SLEEP); 196 197 kseg.s_as = &kas; 198 for (i = 0; i < vacpgs; i++, off += PAGESIZE, 199 vaddr += PAGESIZE) { 200 nfpp[i] = page_create_va(&nfvp, off, PAGESIZE, 201 PG_WAIT | PG_NORELOC, &kseg, vaddr); 202 page_io_unlock(nfpp[i]); 203 page_downgrade(nfpp[i]); 204 pagezero(nfpp[i], 0, PAGESIZE); 205 } 206 } 207 mutex_exit(&segnf_lock); 208 209 hat_map(as->a_hat, seg->s_base, seg->s_size, HAT_MAP); 210 211 /* 212 * s_data can't be NULL because of ASSERTS in the common vm code. 213 */ 214 seg->s_ops = &segnf_ops; 215 seg->s_data = seg; 216 seg->s_flags |= S_PURGE; 217 218 mutex_enter(&as->a_contents); 219 as->a_flags |= AS_NEEDSPURGE; 220 mutex_exit(&as->a_contents); 221 222 prot = PROT_READ; 223 color = addr_to_vcolor(seg->s_base); 224 if (as != &kas) 225 prot |= PROT_USER; 226 hat_memload(as->a_hat, seg->s_base, nfpp[color], 227 prot | HAT_NOFAULT, HAT_LOAD); 228 229 /* 230 * At this point see if we can concatenate a segment to 231 * a non-fault segment immediately before and/or after it. 232 */ 233 if ((s1 = AS_SEGPREV(as, seg)) != NULL && 234 s1->s_ops == &segnf_ops && 235 s1->s_base + s1->s_size == seg->s_base) { 236 size = s1->s_size; 237 seg_free(s1); 238 seg->s_base -= size; 239 seg->s_size += size; 240 } 241 242 if ((s2 = AS_SEGNEXT(as, seg)) != NULL && 243 s2->s_ops == &segnf_ops && 244 seg->s_base + seg->s_size == s2->s_base) { 245 size = s2->s_size; 246 seg_free(s2); 247 seg->s_size += size; 248 } 249 250 /* 251 * if we already have a lot of segments, try to delete some other 252 * nofault segment to reduce the probability of uncontrolled segment 253 * creation. 254 * 255 * the code looks around quickly (no more than MAXNFSEARCH segments 256 * each way) for another NF segment and then deletes it. 257 */ 258 if (avl_numnodes(&as->a_segtree) > MAXSEGFORNF) { 259 size = 0; 260 s2 = NULL; 261 s1 = AS_SEGPREV(as, seg); 262 while (size++ < MAXNFSEARCH && s1 != NULL) { 263 if (s1->s_ops == &segnf_ops) 264 s2 = s1; 265 s1 = AS_SEGPREV(s1->s_as, seg); 266 } 267 if (s2 == NULL) { 268 s1 = AS_SEGNEXT(as, seg); 269 while (size-- > 0 && s1 != NULL) { 270 if (s1->s_ops == &segnf_ops) 271 s2 = s1; 272 s1 = AS_SEGNEXT(as, seg); 273 } 274 } 275 if (s2 != NULL) 276 seg_unmap(s2); 277 } 278 279 return (0); 280 } 281 282 /* 283 * Never really need "No fault" segments, so they aren't dup'd. 284 */ 285 /* ARGSUSED */ 286 static int 287 segnf_dup(struct seg *seg, struct seg *newseg) 288 { 289 panic("segnf_dup"); 290 return (0); 291 } 292 293 /* 294 * Split a segment at addr for length len. 295 */ 296 static int 297 segnf_unmap(struct seg *seg, caddr_t addr, size_t len) 298 { 299 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock)); 300 301 /* 302 * Check for bad sizes. 303 */ 304 if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size || 305 (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) { 306 cmn_err(CE_PANIC, "segnf_unmap: bad unmap size"); 307 } 308 309 /* 310 * Unload any hardware translations in the range to be taken out. 311 */ 312 hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP); 313 314 if (addr == seg->s_base && len == seg->s_size) { 315 /* 316 * Freeing entire segment. 317 */ 318 seg_free(seg); 319 } else if (addr == seg->s_base) { 320 /* 321 * Freeing the beginning of the segment. 322 */ 323 seg->s_base += len; 324 seg->s_size -= len; 325 } else if (addr + len == seg->s_base + seg->s_size) { 326 /* 327 * Freeing the end of the segment. 328 */ 329 seg->s_size -= len; 330 } else { 331 /* 332 * The section to go is in the middle of the segment, so we 333 * have to cut it into two segments. We shrink the existing 334 * "seg" at the low end, and create "nseg" for the high end. 335 */ 336 caddr_t nbase = addr + len; 337 size_t nsize = (seg->s_base + seg->s_size) - nbase; 338 struct seg *nseg; 339 340 /* 341 * Trim down "seg" before trying to stick "nseg" into the as. 342 */ 343 seg->s_size = addr - seg->s_base; 344 nseg = seg_alloc(seg->s_as, nbase, nsize); 345 if (nseg == NULL) 346 cmn_err(CE_PANIC, "segnf_unmap: seg_alloc failed"); 347 348 /* 349 * s_data can't be NULL because of ASSERTs in common VM code. 350 */ 351 nseg->s_ops = seg->s_ops; 352 nseg->s_data = nseg; 353 nseg->s_flags |= S_PURGE; 354 mutex_enter(&seg->s_as->a_contents); 355 seg->s_as->a_flags |= AS_NEEDSPURGE; 356 mutex_exit(&seg->s_as->a_contents); 357 } 358 359 return (0); 360 } 361 362 /* 363 * Free a segment. 364 */ 365 static void 366 segnf_free(struct seg *seg) 367 { 368 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock)); 369 } 370 371 /* 372 * No faults allowed on segnf. 373 */ 374 static faultcode_t 375 segnf_nomap(void) 376 { 377 return (FC_NOMAP); 378 } 379 380 /* ARGSUSED */ 381 static int 382 segnf_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot) 383 { 384 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 385 return (EACCES); 386 } 387 388 /* ARGSUSED */ 389 static int 390 segnf_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot) 391 { 392 uint_t sprot; 393 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 394 395 sprot = seg->s_as == &kas ? PROT_READ : PROT_READ|PROT_USER; 396 return ((prot & sprot) == prot ? 0 : EACCES); 397 } 398 399 static void 400 segnf_badop(void) 401 { 402 panic("segnf_badop"); 403 /*NOTREACHED*/ 404 } 405 406 static int 407 segnf_nop(void) 408 { 409 return (0); 410 } 411 412 static int 413 segnf_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv) 414 { 415 size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1; 416 size_t p; 417 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 418 419 for (p = 0; p < pgno; ++p) 420 protv[p] = PROT_READ; 421 return (0); 422 } 423 424 /* ARGSUSED */ 425 static u_offset_t 426 segnf_getoffset(struct seg *seg, caddr_t addr) 427 { 428 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 429 430 return ((u_offset_t)0); 431 } 432 433 /* ARGSUSED */ 434 static int 435 segnf_gettype(struct seg *seg, caddr_t addr) 436 { 437 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 438 439 return (MAP_SHARED); 440 } 441 442 /* ARGSUSED */ 443 static int 444 segnf_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp) 445 { 446 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 447 448 *vpp = &nfvp; 449 return (0); 450 } 451 452 /* 453 * segnf pages are not dumped, so we just return 454 */ 455 /* ARGSUSED */ 456 static void 457 segnf_dump(struct seg *seg) 458 {} 459 460 /*ARGSUSED*/ 461 static int 462 segnf_pagelock(struct seg *seg, caddr_t addr, size_t len, 463 struct page ***ppp, enum lock_type type, enum seg_rw rw) 464 { 465 return (ENOTSUP); 466 } 467 468 /*ARGSUSED*/ 469 static int 470 segnf_setpagesize(struct seg *seg, caddr_t addr, size_t len, 471 uint_t szc) 472 { 473 return (ENOTSUP); 474 }