Print this page
6147 segop_getpolicy already checks for a NULL op
Reviewed by: Garrett D'Amore <garrett@damore.org>
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);
71 71 static int segnf_getprot(struct seg *seg, caddr_t addr,
↓ open down ↓ |
71 lines elided |
↑ open up ↑ |
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 81 static int segnf_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp);
82 -static lgrp_mem_policy_info_t *segnf_getpolicy(struct seg *seg,
83 - caddr_t addr);
84 82
85 83
86 84 struct seg_ops segnf_ops = {
87 85 .dup = segnf_dup,
88 86 .unmap = segnf_unmap,
89 87 .free = segnf_free,
90 88 .fault = (faultcode_t (*)(struct hat *, struct seg *, caddr_t,
91 89 size_t, enum fault_type, enum seg_rw))segnf_nomap,
92 90 .faulta = (faultcode_t (*)(struct seg *, caddr_t)) segnf_nomap,
93 91 .setprot = segnf_setprot,
94 92 .checkprot = segnf_checkprot,
95 93 .kluster = (int (*)())segnf_badop,
96 94 .sync = (int (*)(struct seg *, caddr_t, size_t, int, uint_t))
97 95 segnf_nop,
98 96 .incore = (size_t (*)(struct seg *, caddr_t, size_t, char *))
99 97 segnf_nop,
100 98 .lockop = (int (*)(struct seg *, caddr_t, size_t, int, int,
101 99 ulong_t *, size_t))segnf_nop,
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
102 100 .getprot = segnf_getprot,
103 101 .getoffset = segnf_getoffset,
104 102 .gettype = segnf_gettype,
105 103 .getvp = segnf_getvp,
106 104 .advise = (int (*)(struct seg *, caddr_t, size_t, uint_t))
107 105 segnf_nop,
108 106 .dump = segnf_dump,
109 107 .pagelock = segnf_pagelock,
110 108 .setpagesize = segnf_setpagesize,
111 109 .getmemid = segnf_getmemid,
112 - .getpolicy = segnf_getpolicy,
113 110 };
114 111
115 112 /*
116 113 * vnode and page for the page of zeros we use for the nf mappings.
117 114 */
118 115 static kmutex_t segnf_lock;
119 116 static struct vnode nfvp;
120 117 static struct page **nfpp;
121 118
122 119 #define addr_to_vcolor(addr) \
123 120 (shm_alignment) ? \
124 121 ((int)(((uintptr_t)(addr) & (shm_alignment - 1)) >> PAGESHIFT)) : 0
125 122
126 123 /*
127 124 * We try to limit the number of Non-fault segments created.
128 125 * Non fault segments are created to optimize sparc V9 code which uses
129 126 * the sparc nonfaulting load ASI (ASI_PRIMARY_NOFAULT).
130 127 *
131 128 * There are several reasons why creating too many non-fault segments
132 129 * could cause problems.
133 130 *
134 131 * First, excessive allocation of kernel resources for the seg
135 132 * structures and the HAT data to map the zero pages.
136 133 *
137 134 * Secondly, creating nofault segments actually uses up user virtual
138 135 * address space. This makes it unavailable for subsequent mmap(0, ...)
139 136 * calls which use as_gap() to find empty va regions. Creation of too
140 137 * many nofault segments could thus interfere with the ability of the
141 138 * runtime linker to load a shared object.
142 139 */
143 140 #define MAXSEGFORNF (10000)
144 141 #define MAXNFSEARCH (5)
145 142
146 143
147 144 /*
148 145 * Must be called from startup()
149 146 */
150 147 void
151 148 segnf_init()
152 149 {
153 150 mutex_init(&segnf_lock, NULL, MUTEX_DEFAULT, NULL);
154 151 }
155 152
156 153
157 154 /*
158 155 * Create a no-fault segment.
159 156 *
160 157 * The no-fault segment is not technically necessary, as the code in
161 158 * nfload() in trap.c will emulate the SPARC instruction and load
162 159 * a value of zero in the destination register.
163 160 *
164 161 * However, this code tries to put a page of zero's at the nofault address
165 162 * so that subsequent non-faulting loads to the same page will not
166 163 * trap with a tlb miss.
167 164 *
168 165 * In order to help limit the number of segments we merge adjacent nofault
169 166 * segments into a single segment. If we get a large number of segments
170 167 * we'll also try to delete a random other nf segment.
171 168 */
172 169 /* ARGSUSED */
173 170 int
174 171 segnf_create(struct seg *seg, void *argsp)
175 172 {
176 173 uint_t prot;
177 174 pgcnt_t vacpgs;
178 175 u_offset_t off = 0;
179 176 caddr_t vaddr = NULL;
180 177 int i, color;
181 178 struct seg *s1;
182 179 struct seg *s2;
183 180 size_t size;
184 181 struct as *as = seg->s_as;
185 182
186 183 ASSERT(as && AS_WRITE_HELD(as, &as->a_lock));
187 184
188 185 /*
189 186 * Need a page per virtual color or just 1 if no vac.
190 187 */
191 188 mutex_enter(&segnf_lock);
192 189 if (nfpp == NULL) {
193 190 struct seg kseg;
194 191
195 192 vacpgs = 1;
196 193 if (shm_alignment > PAGESIZE) {
197 194 vacpgs = shm_alignment >> PAGESHIFT;
198 195 }
199 196
200 197 nfpp = kmem_alloc(sizeof (*nfpp) * vacpgs, KM_SLEEP);
201 198
202 199 kseg.s_as = &kas;
203 200 for (i = 0; i < vacpgs; i++, off += PAGESIZE,
204 201 vaddr += PAGESIZE) {
205 202 nfpp[i] = page_create_va(&nfvp, off, PAGESIZE,
206 203 PG_WAIT | PG_NORELOC, &kseg, vaddr);
207 204 page_io_unlock(nfpp[i]);
208 205 page_downgrade(nfpp[i]);
209 206 pagezero(nfpp[i], 0, PAGESIZE);
210 207 }
211 208 }
212 209 mutex_exit(&segnf_lock);
213 210
214 211 hat_map(as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
215 212
216 213 /*
217 214 * s_data can't be NULL because of ASSERTS in the common vm code.
218 215 */
219 216 seg->s_ops = &segnf_ops;
220 217 seg->s_data = seg;
221 218 seg->s_flags |= S_PURGE;
222 219
223 220 mutex_enter(&as->a_contents);
224 221 as->a_flags |= AS_NEEDSPURGE;
225 222 mutex_exit(&as->a_contents);
226 223
227 224 prot = PROT_READ;
228 225 color = addr_to_vcolor(seg->s_base);
229 226 if (as != &kas)
230 227 prot |= PROT_USER;
231 228 hat_memload(as->a_hat, seg->s_base, nfpp[color],
232 229 prot | HAT_NOFAULT, HAT_LOAD);
233 230
234 231 /*
235 232 * At this point see if we can concatenate a segment to
236 233 * a non-fault segment immediately before and/or after it.
237 234 */
238 235 if ((s1 = AS_SEGPREV(as, seg)) != NULL &&
239 236 s1->s_ops == &segnf_ops &&
240 237 s1->s_base + s1->s_size == seg->s_base) {
241 238 size = s1->s_size;
242 239 seg_free(s1);
243 240 seg->s_base -= size;
244 241 seg->s_size += size;
245 242 }
246 243
247 244 if ((s2 = AS_SEGNEXT(as, seg)) != NULL &&
248 245 s2->s_ops == &segnf_ops &&
249 246 seg->s_base + seg->s_size == s2->s_base) {
250 247 size = s2->s_size;
251 248 seg_free(s2);
252 249 seg->s_size += size;
253 250 }
254 251
255 252 /*
256 253 * if we already have a lot of segments, try to delete some other
257 254 * nofault segment to reduce the probability of uncontrolled segment
258 255 * creation.
259 256 *
260 257 * the code looks around quickly (no more than MAXNFSEARCH segments
261 258 * each way) for another NF segment and then deletes it.
262 259 */
263 260 if (avl_numnodes(&as->a_segtree) > MAXSEGFORNF) {
264 261 size = 0;
265 262 s2 = NULL;
266 263 s1 = AS_SEGPREV(as, seg);
267 264 while (size++ < MAXNFSEARCH && s1 != NULL) {
268 265 if (s1->s_ops == &segnf_ops)
269 266 s2 = s1;
270 267 s1 = AS_SEGPREV(s1->s_as, seg);
271 268 }
272 269 if (s2 == NULL) {
273 270 s1 = AS_SEGNEXT(as, seg);
274 271 while (size-- > 0 && s1 != NULL) {
275 272 if (s1->s_ops == &segnf_ops)
276 273 s2 = s1;
277 274 s1 = AS_SEGNEXT(as, seg);
278 275 }
279 276 }
280 277 if (s2 != NULL)
281 278 seg_unmap(s2);
282 279 }
283 280
284 281 return (0);
285 282 }
286 283
287 284 /*
288 285 * Never really need "No fault" segments, so they aren't dup'd.
289 286 */
290 287 /* ARGSUSED */
291 288 static int
292 289 segnf_dup(struct seg *seg, struct seg *newseg)
293 290 {
294 291 panic("segnf_dup");
295 292 return (0);
296 293 }
297 294
298 295 /*
299 296 * Split a segment at addr for length len.
300 297 */
301 298 static int
302 299 segnf_unmap(struct seg *seg, caddr_t addr, size_t len)
303 300 {
304 301 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
305 302
306 303 /*
307 304 * Check for bad sizes.
308 305 */
309 306 if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
310 307 (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) {
311 308 cmn_err(CE_PANIC, "segnf_unmap: bad unmap size");
312 309 }
313 310
314 311 /*
315 312 * Unload any hardware translations in the range to be taken out.
316 313 */
317 314 hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP);
318 315
319 316 if (addr == seg->s_base && len == seg->s_size) {
320 317 /*
321 318 * Freeing entire segment.
322 319 */
323 320 seg_free(seg);
324 321 } else if (addr == seg->s_base) {
325 322 /*
326 323 * Freeing the beginning of the segment.
327 324 */
328 325 seg->s_base += len;
329 326 seg->s_size -= len;
330 327 } else if (addr + len == seg->s_base + seg->s_size) {
331 328 /*
332 329 * Freeing the end of the segment.
333 330 */
334 331 seg->s_size -= len;
335 332 } else {
336 333 /*
337 334 * The section to go is in the middle of the segment, so we
338 335 * have to cut it into two segments. We shrink the existing
339 336 * "seg" at the low end, and create "nseg" for the high end.
340 337 */
341 338 caddr_t nbase = addr + len;
342 339 size_t nsize = (seg->s_base + seg->s_size) - nbase;
343 340 struct seg *nseg;
344 341
345 342 /*
346 343 * Trim down "seg" before trying to stick "nseg" into the as.
347 344 */
348 345 seg->s_size = addr - seg->s_base;
349 346 nseg = seg_alloc(seg->s_as, nbase, nsize);
350 347 if (nseg == NULL)
351 348 cmn_err(CE_PANIC, "segnf_unmap: seg_alloc failed");
352 349
353 350 /*
354 351 * s_data can't be NULL because of ASSERTs in common VM code.
355 352 */
356 353 nseg->s_ops = seg->s_ops;
357 354 nseg->s_data = nseg;
358 355 nseg->s_flags |= S_PURGE;
359 356 mutex_enter(&seg->s_as->a_contents);
360 357 seg->s_as->a_flags |= AS_NEEDSPURGE;
361 358 mutex_exit(&seg->s_as->a_contents);
362 359 }
363 360
364 361 return (0);
365 362 }
366 363
367 364 /*
368 365 * Free a segment.
369 366 */
370 367 static void
371 368 segnf_free(struct seg *seg)
372 369 {
373 370 ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
374 371 }
375 372
376 373 /*
377 374 * No faults allowed on segnf.
378 375 */
379 376 static faultcode_t
380 377 segnf_nomap(void)
381 378 {
382 379 return (FC_NOMAP);
383 380 }
384 381
385 382 /* ARGSUSED */
386 383 static int
387 384 segnf_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
388 385 {
389 386 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
390 387 return (EACCES);
391 388 }
392 389
393 390 /* ARGSUSED */
394 391 static int
395 392 segnf_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
396 393 {
397 394 uint_t sprot;
398 395 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
399 396
400 397 sprot = seg->s_as == &kas ? PROT_READ : PROT_READ|PROT_USER;
401 398 return ((prot & sprot) == prot ? 0 : EACCES);
402 399 }
403 400
404 401 static void
405 402 segnf_badop(void)
406 403 {
407 404 panic("segnf_badop");
408 405 /*NOTREACHED*/
409 406 }
410 407
411 408 static int
412 409 segnf_nop(void)
413 410 {
414 411 return (0);
415 412 }
416 413
417 414 static int
418 415 segnf_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
419 416 {
420 417 size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
421 418 size_t p;
422 419 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
423 420
424 421 for (p = 0; p < pgno; ++p)
425 422 protv[p] = PROT_READ;
426 423 return (0);
427 424 }
428 425
429 426 /* ARGSUSED */
430 427 static u_offset_t
431 428 segnf_getoffset(struct seg *seg, caddr_t addr)
432 429 {
433 430 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
434 431
435 432 return ((u_offset_t)0);
436 433 }
437 434
438 435 /* ARGSUSED */
439 436 static int
440 437 segnf_gettype(struct seg *seg, caddr_t addr)
441 438 {
442 439 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
443 440
444 441 return (MAP_SHARED);
445 442 }
446 443
447 444 /* ARGSUSED */
448 445 static int
449 446 segnf_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
450 447 {
451 448 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
452 449
453 450 *vpp = &nfvp;
454 451 return (0);
455 452 }
456 453
457 454 /*
458 455 * segnf pages are not dumped, so we just return
459 456 */
460 457 /* ARGSUSED */
461 458 static void
462 459 segnf_dump(struct seg *seg)
463 460 {}
464 461
465 462 /*ARGSUSED*/
466 463 static int
467 464 segnf_pagelock(struct seg *seg, caddr_t addr, size_t len,
468 465 struct page ***ppp, enum lock_type type, enum seg_rw rw)
469 466 {
470 467 return (ENOTSUP);
471 468 }
472 469
473 470 /*ARGSUSED*/
474 471 static int
475 472 segnf_setpagesize(struct seg *seg, caddr_t addr, size_t len,
↓ open down ↓ |
353 lines elided |
↑ open up ↑ |
476 473 uint_t szc)
477 474 {
478 475 return (ENOTSUP);
479 476 }
480 477
481 478 /*ARGSUSED*/
482 479 static int
483 480 segnf_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
484 481 {
485 482 return (ENODEV);
486 -}
487 -
488 -/*ARGSUSED*/
489 -static lgrp_mem_policy_info_t *
490 -segnf_getpolicy(struct seg *seg, caddr_t addr)
491 -{
492 - return (NULL);
493 483 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX