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