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