Print this page
6583 remove whole-process swapping
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/vm/vm_as.c
+++ new/usr/src/uts/common/vm/vm_as.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 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 * Copyright 2015, Joyent, Inc. All rights reserved.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /*
31 31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 32 * The Regents of the University of California
33 33 * All Rights Reserved
34 34 *
35 35 * University Acknowledgment- Portions of this document are derived from
36 36 * software developed by the University of California, Berkeley, and its
37 37 * contributors.
38 38 */
39 39
40 40 /*
41 41 * VM - address spaces.
42 42 */
43 43
44 44 #include <sys/types.h>
45 45 #include <sys/t_lock.h>
46 46 #include <sys/param.h>
47 47 #include <sys/errno.h>
48 48 #include <sys/systm.h>
49 49 #include <sys/mman.h>
50 50 #include <sys/sysmacros.h>
51 51 #include <sys/cpuvar.h>
52 52 #include <sys/sysinfo.h>
53 53 #include <sys/kmem.h>
54 54 #include <sys/vnode.h>
55 55 #include <sys/vmsystm.h>
56 56 #include <sys/cmn_err.h>
57 57 #include <sys/debug.h>
58 58 #include <sys/tnf_probe.h>
59 59 #include <sys/vtrace.h>
60 60
61 61 #include <vm/hat.h>
62 62 #include <vm/as.h>
63 63 #include <vm/seg.h>
64 64 #include <vm/seg_vn.h>
65 65 #include <vm/seg_dev.h>
66 66 #include <vm/seg_kmem.h>
67 67 #include <vm/seg_map.h>
68 68 #include <vm/seg_spt.h>
69 69 #include <vm/page.h>
70 70
71 71 clock_t deadlk_wait = 1; /* number of ticks to wait before retrying */
72 72
73 73 static struct kmem_cache *as_cache;
74 74
75 75 static void as_setwatchprot(struct as *, caddr_t, size_t, uint_t);
76 76 static void as_clearwatchprot(struct as *, caddr_t, size_t);
77 77 int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
78 78
79 79
80 80 /*
81 81 * Verifying the segment lists is very time-consuming; it may not be
82 82 * desirable always to define VERIFY_SEGLIST when DEBUG is set.
83 83 */
84 84 #ifdef DEBUG
85 85 #define VERIFY_SEGLIST
86 86 int do_as_verify = 0;
87 87 #endif
88 88
89 89 /*
90 90 * Allocate a new callback data structure entry and fill in the events of
91 91 * interest, the address range of interest, and the callback argument.
92 92 * Link the entry on the as->a_callbacks list. A callback entry for the
93 93 * entire address space may be specified with vaddr = 0 and size = -1.
94 94 *
95 95 * CALLERS RESPONSIBILITY: If not calling from within the process context for
96 96 * the specified as, the caller must guarantee persistence of the specified as
97 97 * for the duration of this function (eg. pages being locked within the as
98 98 * will guarantee persistence).
99 99 */
100 100 int
101 101 as_add_callback(struct as *as, void (*cb_func)(), void *arg, uint_t events,
102 102 caddr_t vaddr, size_t size, int sleepflag)
103 103 {
104 104 struct as_callback *current_head, *cb;
105 105 caddr_t saddr;
106 106 size_t rsize;
107 107
108 108 /* callback function and an event are mandatory */
109 109 if ((cb_func == NULL) || ((events & AS_ALL_EVENT) == 0))
110 110 return (EINVAL);
111 111
112 112 /* Adding a callback after as_free has been called is not allowed */
113 113 if (as == &kas)
114 114 return (ENOMEM);
115 115
116 116 /*
117 117 * vaddr = 0 and size = -1 is used to indicate that the callback range
118 118 * is the entire address space so no rounding is done in that case.
119 119 */
120 120 if (size != -1) {
121 121 saddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
122 122 rsize = (((size_t)(vaddr + size) + PAGEOFFSET) & PAGEMASK) -
123 123 (size_t)saddr;
124 124 /* check for wraparound */
125 125 if (saddr + rsize < saddr)
126 126 return (ENOMEM);
127 127 } else {
128 128 if (vaddr != 0)
129 129 return (EINVAL);
130 130 saddr = vaddr;
131 131 rsize = size;
132 132 }
133 133
134 134 /* Allocate and initialize a callback entry */
135 135 cb = kmem_zalloc(sizeof (struct as_callback), sleepflag);
136 136 if (cb == NULL)
137 137 return (EAGAIN);
138 138
139 139 cb->ascb_func = cb_func;
140 140 cb->ascb_arg = arg;
141 141 cb->ascb_events = events;
142 142 cb->ascb_saddr = saddr;
143 143 cb->ascb_len = rsize;
144 144
145 145 /* Add the entry to the list */
146 146 mutex_enter(&as->a_contents);
147 147 current_head = as->a_callbacks;
148 148 as->a_callbacks = cb;
149 149 cb->ascb_next = current_head;
150 150
151 151 /*
152 152 * The call to this function may lose in a race with
153 153 * a pertinent event - eg. a thread does long term memory locking
154 154 * but before the callback is added another thread executes as_unmap.
155 155 * A broadcast here resolves that.
156 156 */
157 157 if ((cb->ascb_events & AS_UNMAPWAIT_EVENT) && AS_ISUNMAPWAIT(as)) {
158 158 AS_CLRUNMAPWAIT(as);
159 159 cv_broadcast(&as->a_cv);
160 160 }
161 161
162 162 mutex_exit(&as->a_contents);
163 163 return (0);
164 164 }
165 165
166 166 /*
167 167 * Search the callback list for an entry which pertains to arg.
168 168 *
169 169 * This is called from within the client upon completion of the callback.
170 170 * RETURN VALUES:
171 171 * AS_CALLBACK_DELETED (callback entry found and deleted)
172 172 * AS_CALLBACK_NOTFOUND (no callback entry found - this is ok)
173 173 * AS_CALLBACK_DELETE_DEFERRED (callback is in process, delete of this
174 174 * entry will be made in as_do_callbacks)
175 175 *
176 176 * If as_delete_callback encounters a matching entry with AS_CALLBACK_CALLED
177 177 * set, it indicates that as_do_callbacks is processing this entry. The
178 178 * AS_ALL_EVENT events are cleared in the entry, and a broadcast is made
179 179 * to unblock as_do_callbacks, in case it is blocked.
180 180 *
181 181 * CALLERS RESPONSIBILITY: If not calling from within the process context for
182 182 * the specified as, the caller must guarantee persistence of the specified as
183 183 * for the duration of this function (eg. pages being locked within the as
184 184 * will guarantee persistence).
185 185 */
186 186 uint_t
187 187 as_delete_callback(struct as *as, void *arg)
188 188 {
189 189 struct as_callback **prevcb = &as->a_callbacks;
190 190 struct as_callback *cb;
191 191 uint_t rc = AS_CALLBACK_NOTFOUND;
192 192
193 193 mutex_enter(&as->a_contents);
194 194 for (cb = as->a_callbacks; cb; prevcb = &cb->ascb_next, cb = *prevcb) {
195 195 if (cb->ascb_arg != arg)
196 196 continue;
197 197
198 198 /*
199 199 * If the events indicate AS_CALLBACK_CALLED, just clear
200 200 * AS_ALL_EVENT in the events field and wakeup the thread
201 201 * that may be waiting in as_do_callbacks. as_do_callbacks
202 202 * will take care of removing this entry from the list. In
203 203 * that case, return AS_CALLBACK_DELETE_DEFERRED. Otherwise
204 204 * (AS_CALLBACK_CALLED not set), just remove it from the
205 205 * list, return the memory and return AS_CALLBACK_DELETED.
206 206 */
207 207 if ((cb->ascb_events & AS_CALLBACK_CALLED) != 0) {
208 208 /* leave AS_CALLBACK_CALLED */
209 209 cb->ascb_events &= ~AS_ALL_EVENT;
210 210 rc = AS_CALLBACK_DELETE_DEFERRED;
211 211 cv_broadcast(&as->a_cv);
212 212 } else {
213 213 *prevcb = cb->ascb_next;
214 214 kmem_free(cb, sizeof (struct as_callback));
215 215 rc = AS_CALLBACK_DELETED;
216 216 }
217 217 break;
218 218 }
219 219 mutex_exit(&as->a_contents);
220 220 return (rc);
221 221 }
222 222
223 223 /*
224 224 * Searches the as callback list for a matching entry.
225 225 * Returns a pointer to the first matching callback, or NULL if
226 226 * nothing is found.
227 227 * This function never sleeps so it is ok to call it with more
228 228 * locks held but the (required) a_contents mutex.
229 229 *
230 230 * See also comment on as_do_callbacks below.
231 231 */
232 232 static struct as_callback *
233 233 as_find_callback(struct as *as, uint_t events, caddr_t event_addr,
234 234 size_t event_len)
235 235 {
236 236 struct as_callback *cb;
237 237
238 238 ASSERT(MUTEX_HELD(&as->a_contents));
239 239 for (cb = as->a_callbacks; cb != NULL; cb = cb->ascb_next) {
240 240 /*
241 241 * If the callback has not already been called, then
242 242 * check if events or address range pertains. An event_len
243 243 * of zero means do an unconditional callback.
244 244 */
245 245 if (((cb->ascb_events & AS_CALLBACK_CALLED) != 0) ||
246 246 ((event_len != 0) && (((cb->ascb_events & events) == 0) ||
247 247 (event_addr + event_len < cb->ascb_saddr) ||
248 248 (event_addr > (cb->ascb_saddr + cb->ascb_len))))) {
249 249 continue;
250 250 }
251 251 break;
252 252 }
253 253 return (cb);
254 254 }
255 255
256 256 /*
257 257 * Executes a given callback and removes it from the callback list for
258 258 * this address space.
259 259 * This function may sleep so the caller must drop all locks except
260 260 * a_contents before calling this func.
261 261 *
262 262 * See also comments on as_do_callbacks below.
263 263 */
264 264 static void
265 265 as_execute_callback(struct as *as, struct as_callback *cb,
266 266 uint_t events)
267 267 {
268 268 struct as_callback **prevcb;
269 269 void *cb_arg;
270 270
271 271 ASSERT(MUTEX_HELD(&as->a_contents) && (cb->ascb_events & events));
272 272 cb->ascb_events |= AS_CALLBACK_CALLED;
273 273 mutex_exit(&as->a_contents);
274 274 (*cb->ascb_func)(as, cb->ascb_arg, events);
275 275 mutex_enter(&as->a_contents);
276 276 /*
277 277 * the callback function is required to delete the callback
278 278 * when the callback function determines it is OK for
279 279 * this thread to continue. as_delete_callback will clear
280 280 * the AS_ALL_EVENT in the events field when it is deleted.
281 281 * If the callback function called as_delete_callback,
282 282 * events will already be cleared and there will be no blocking.
283 283 */
284 284 while ((cb->ascb_events & events) != 0) {
285 285 cv_wait(&as->a_cv, &as->a_contents);
286 286 }
287 287 /*
288 288 * This entry needs to be taken off the list. Normally, the
289 289 * callback func itself does that, but unfortunately the list
290 290 * may have changed while the callback was running because the
291 291 * a_contents mutex was dropped and someone else other than the
292 292 * callback func itself could have called as_delete_callback,
293 293 * so we have to search to find this entry again. The entry
294 294 * must have AS_CALLBACK_CALLED, and have the same 'arg'.
295 295 */
296 296 cb_arg = cb->ascb_arg;
297 297 prevcb = &as->a_callbacks;
298 298 for (cb = as->a_callbacks; cb != NULL;
299 299 prevcb = &cb->ascb_next, cb = *prevcb) {
300 300 if (((cb->ascb_events & AS_CALLBACK_CALLED) == 0) ||
301 301 (cb_arg != cb->ascb_arg)) {
302 302 continue;
303 303 }
304 304 *prevcb = cb->ascb_next;
305 305 kmem_free(cb, sizeof (struct as_callback));
306 306 break;
307 307 }
308 308 }
309 309
310 310 /*
311 311 * Check the callback list for a matching event and intersection of
312 312 * address range. If there is a match invoke the callback. Skip an entry if:
313 313 * - a callback is already in progress for this entry (AS_CALLBACK_CALLED)
314 314 * - not event of interest
315 315 * - not address range of interest
316 316 *
317 317 * An event_len of zero indicates a request for an unconditional callback
318 318 * (regardless of event), only the AS_CALLBACK_CALLED is checked. The
319 319 * a_contents lock must be dropped before a callback, so only one callback
320 320 * can be done before returning. Return -1 (true) if a callback was
321 321 * executed and removed from the list, else return 0 (false).
322 322 *
323 323 * The logically separate parts, i.e. finding a matching callback and
324 324 * executing a given callback have been separated into two functions
325 325 * so that they can be called with different sets of locks held beyond
326 326 * the always-required a_contents. as_find_callback does not sleep so
327 327 * it is ok to call it if more locks than a_contents (i.e. the a_lock
328 328 * rwlock) are held. as_execute_callback on the other hand may sleep
329 329 * so all locks beyond a_contents must be dropped by the caller if one
330 330 * does not want to end comatose.
331 331 */
332 332 static int
333 333 as_do_callbacks(struct as *as, uint_t events, caddr_t event_addr,
334 334 size_t event_len)
335 335 {
336 336 struct as_callback *cb;
337 337
338 338 if ((cb = as_find_callback(as, events, event_addr, event_len))) {
339 339 as_execute_callback(as, cb, events);
340 340 return (-1);
341 341 }
342 342 return (0);
343 343 }
344 344
345 345 /*
346 346 * Search for the segment containing addr. If a segment containing addr
347 347 * exists, that segment is returned. If no such segment exists, and
348 348 * the list spans addresses greater than addr, then the first segment
349 349 * whose base is greater than addr is returned; otherwise, NULL is
350 350 * returned unless tail is true, in which case the last element of the
351 351 * list is returned.
352 352 *
353 353 * a_seglast is used to cache the last found segment for repeated
354 354 * searches to the same addr (which happens frequently).
355 355 */
356 356 struct seg *
357 357 as_findseg(struct as *as, caddr_t addr, int tail)
358 358 {
359 359 struct seg *seg = as->a_seglast;
360 360 avl_index_t where;
361 361
362 362 ASSERT(AS_LOCK_HELD(as));
363 363
364 364 if (seg != NULL &&
365 365 seg->s_base <= addr &&
366 366 addr < seg->s_base + seg->s_size)
367 367 return (seg);
368 368
369 369 seg = avl_find(&as->a_segtree, &addr, &where);
370 370 if (seg != NULL)
371 371 return (as->a_seglast = seg);
372 372
373 373 seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
374 374 if (seg == NULL && tail)
375 375 seg = avl_last(&as->a_segtree);
376 376 return (as->a_seglast = seg);
377 377 }
378 378
379 379 #ifdef VERIFY_SEGLIST
380 380 /*
381 381 * verify that the linked list is coherent
382 382 */
383 383 static void
384 384 as_verify(struct as *as)
385 385 {
386 386 struct seg *seg, *seglast, *p, *n;
387 387 uint_t nsegs = 0;
388 388
389 389 if (do_as_verify == 0)
390 390 return;
391 391
392 392 seglast = as->a_seglast;
393 393
394 394 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
395 395 ASSERT(seg->s_as == as);
396 396 p = AS_SEGPREV(as, seg);
397 397 n = AS_SEGNEXT(as, seg);
398 398 ASSERT(p == NULL || p->s_as == as);
399 399 ASSERT(p == NULL || p->s_base < seg->s_base);
400 400 ASSERT(n == NULL || n->s_base > seg->s_base);
401 401 ASSERT(n != NULL || seg == avl_last(&as->a_segtree));
402 402 if (seg == seglast)
403 403 seglast = NULL;
404 404 nsegs++;
405 405 }
406 406 ASSERT(seglast == NULL);
407 407 ASSERT(avl_numnodes(&as->a_segtree) == nsegs);
408 408 }
409 409 #endif /* VERIFY_SEGLIST */
410 410
411 411 /*
412 412 * Add a new segment to the address space. The avl_find()
413 413 * may be expensive so we attempt to use last segment accessed
414 414 * in as_gap() as an insertion point.
415 415 */
416 416 int
417 417 as_addseg(struct as *as, struct seg *newseg)
418 418 {
419 419 struct seg *seg;
420 420 caddr_t addr;
421 421 caddr_t eaddr;
422 422 avl_index_t where;
423 423
424 424 ASSERT(AS_WRITE_HELD(as));
425 425
426 426 as->a_updatedir = 1; /* inform /proc */
427 427 gethrestime(&as->a_updatetime);
428 428
429 429 if (as->a_lastgaphl != NULL) {
430 430 struct seg *hseg = NULL;
431 431 struct seg *lseg = NULL;
432 432
433 433 if (as->a_lastgaphl->s_base > newseg->s_base) {
434 434 hseg = as->a_lastgaphl;
435 435 lseg = AVL_PREV(&as->a_segtree, hseg);
436 436 } else {
437 437 lseg = as->a_lastgaphl;
438 438 hseg = AVL_NEXT(&as->a_segtree, lseg);
439 439 }
440 440
441 441 if (hseg && lseg && lseg->s_base < newseg->s_base &&
442 442 hseg->s_base > newseg->s_base) {
443 443 avl_insert_here(&as->a_segtree, newseg, lseg,
444 444 AVL_AFTER);
445 445 as->a_lastgaphl = NULL;
446 446 as->a_seglast = newseg;
447 447 return (0);
448 448 }
449 449 as->a_lastgaphl = NULL;
450 450 }
451 451
452 452 addr = newseg->s_base;
453 453 eaddr = addr + newseg->s_size;
454 454 again:
455 455
456 456 seg = avl_find(&as->a_segtree, &addr, &where);
457 457
458 458 if (seg == NULL)
459 459 seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
460 460
461 461 if (seg == NULL)
462 462 seg = avl_last(&as->a_segtree);
463 463
464 464 if (seg != NULL) {
465 465 caddr_t base = seg->s_base;
466 466
467 467 /*
468 468 * If top of seg is below the requested address, then
469 469 * the insertion point is at the end of the linked list,
470 470 * and seg points to the tail of the list. Otherwise,
471 471 * the insertion point is immediately before seg.
472 472 */
473 473 if (base + seg->s_size > addr) {
474 474 if (addr >= base || eaddr > base) {
475 475 #ifdef __sparc
476 476 extern struct seg_ops segnf_ops;
477 477
478 478 /*
479 479 * no-fault segs must disappear if overlaid.
480 480 * XXX need new segment type so
481 481 * we don't have to check s_ops
482 482 */
483 483 if (seg->s_ops == &segnf_ops) {
484 484 seg_unmap(seg);
485 485 goto again;
486 486 }
487 487 #endif
488 488 return (-1); /* overlapping segment */
489 489 }
490 490 }
491 491 }
492 492 as->a_seglast = newseg;
493 493 avl_insert(&as->a_segtree, newseg, where);
494 494
495 495 #ifdef VERIFY_SEGLIST
496 496 as_verify(as);
497 497 #endif
498 498 return (0);
499 499 }
500 500
501 501 struct seg *
502 502 as_removeseg(struct as *as, struct seg *seg)
503 503 {
504 504 avl_tree_t *t;
505 505
506 506 ASSERT(AS_WRITE_HELD(as));
507 507
508 508 as->a_updatedir = 1; /* inform /proc */
509 509 gethrestime(&as->a_updatetime);
510 510
511 511 if (seg == NULL)
512 512 return (NULL);
513 513
514 514 t = &as->a_segtree;
515 515 if (as->a_seglast == seg)
516 516 as->a_seglast = NULL;
517 517 as->a_lastgaphl = NULL;
518 518
519 519 /*
520 520 * if this segment is at an address higher than
521 521 * a_lastgap, set a_lastgap to the next segment (NULL if last segment)
522 522 */
523 523 if (as->a_lastgap &&
524 524 (seg == as->a_lastgap || seg->s_base > as->a_lastgap->s_base))
525 525 as->a_lastgap = AVL_NEXT(t, seg);
526 526
527 527 /*
528 528 * remove the segment from the seg tree
529 529 */
530 530 avl_remove(t, seg);
531 531
532 532 #ifdef VERIFY_SEGLIST
533 533 as_verify(as);
534 534 #endif
535 535 return (seg);
536 536 }
537 537
538 538 /*
539 539 * Find a segment containing addr.
540 540 */
541 541 struct seg *
542 542 as_segat(struct as *as, caddr_t addr)
543 543 {
544 544 struct seg *seg = as->a_seglast;
545 545
546 546 ASSERT(AS_LOCK_HELD(as));
547 547
548 548 if (seg != NULL && seg->s_base <= addr &&
549 549 addr < seg->s_base + seg->s_size)
550 550 return (seg);
551 551
552 552 seg = avl_find(&as->a_segtree, &addr, NULL);
553 553 return (seg);
554 554 }
555 555
556 556 /*
557 557 * Serialize all searches for holes in an address space to
558 558 * prevent two or more threads from allocating the same virtual
559 559 * address range. The address space must not be "read/write"
560 560 * locked by the caller since we may block.
561 561 */
562 562 void
563 563 as_rangelock(struct as *as)
564 564 {
565 565 mutex_enter(&as->a_contents);
566 566 while (AS_ISCLAIMGAP(as))
567 567 cv_wait(&as->a_cv, &as->a_contents);
568 568 AS_SETCLAIMGAP(as);
569 569 mutex_exit(&as->a_contents);
570 570 }
571 571
572 572 /*
573 573 * Release hold on a_state & AS_CLAIMGAP and signal any other blocked threads.
574 574 */
575 575 void
576 576 as_rangeunlock(struct as *as)
577 577 {
578 578 mutex_enter(&as->a_contents);
579 579 AS_CLRCLAIMGAP(as);
580 580 cv_signal(&as->a_cv);
581 581 mutex_exit(&as->a_contents);
582 582 }
583 583
584 584 /*
585 585 * compar segments (or just an address) by segment address range
586 586 */
587 587 static int
588 588 as_segcompar(const void *x, const void *y)
589 589 {
590 590 struct seg *a = (struct seg *)x;
591 591 struct seg *b = (struct seg *)y;
592 592
593 593 if (a->s_base < b->s_base)
594 594 return (-1);
595 595 if (a->s_base >= b->s_base + b->s_size)
596 596 return (1);
597 597 return (0);
598 598 }
599 599
600 600
601 601 void
602 602 as_avlinit(struct as *as)
603 603 {
604 604 avl_create(&as->a_segtree, as_segcompar, sizeof (struct seg),
605 605 offsetof(struct seg, s_tree));
606 606 avl_create(&as->a_wpage, wp_compare, sizeof (struct watched_page),
607 607 offsetof(struct watched_page, wp_link));
608 608 }
609 609
610 610 /*ARGSUSED*/
611 611 static int
612 612 as_constructor(void *buf, void *cdrarg, int kmflags)
613 613 {
614 614 struct as *as = buf;
615 615
616 616 mutex_init(&as->a_contents, NULL, MUTEX_DEFAULT, NULL);
617 617 cv_init(&as->a_cv, NULL, CV_DEFAULT, NULL);
618 618 rw_init(&as->a_lock, NULL, RW_DEFAULT, NULL);
619 619 as_avlinit(as);
620 620 return (0);
621 621 }
622 622
623 623 /*ARGSUSED1*/
624 624 static void
625 625 as_destructor(void *buf, void *cdrarg)
626 626 {
627 627 struct as *as = buf;
628 628
629 629 avl_destroy(&as->a_segtree);
630 630 mutex_destroy(&as->a_contents);
631 631 cv_destroy(&as->a_cv);
632 632 rw_destroy(&as->a_lock);
633 633 }
634 634
635 635 void
636 636 as_init(void)
637 637 {
638 638 as_cache = kmem_cache_create("as_cache", sizeof (struct as), 0,
639 639 as_constructor, as_destructor, NULL, NULL, NULL, 0);
640 640 }
641 641
642 642 /*
643 643 * Allocate and initialize an address space data structure.
644 644 * We call hat_alloc to allow any machine dependent
645 645 * information in the hat structure to be initialized.
646 646 */
647 647 struct as *
648 648 as_alloc(void)
649 649 {
650 650 struct as *as;
651 651
652 652 as = kmem_cache_alloc(as_cache, KM_SLEEP);
653 653
654 654 as->a_flags = 0;
655 655 as->a_vbits = 0;
656 656 as->a_hrm = NULL;
657 657 as->a_seglast = NULL;
658 658 as->a_size = 0;
659 659 as->a_resvsize = 0;
660 660 as->a_updatedir = 0;
661 661 gethrestime(&as->a_updatetime);
662 662 as->a_objectdir = NULL;
663 663 as->a_sizedir = 0;
664 664 as->a_userlimit = (caddr_t)USERLIMIT;
665 665 as->a_lastgap = NULL;
666 666 as->a_lastgaphl = NULL;
667 667 as->a_callbacks = NULL;
668 668
669 669 AS_LOCK_ENTER(as, RW_WRITER);
670 670 as->a_hat = hat_alloc(as); /* create hat for default system mmu */
671 671 AS_LOCK_EXIT(as);
672 672
673 673 return (as);
674 674 }
675 675
676 676 /*
677 677 * Free an address space data structure.
678 678 * Need to free the hat first and then
679 679 * all the segments on this as and finally
680 680 * the space for the as struct itself.
681 681 */
682 682 void
683 683 as_free(struct as *as)
684 684 {
685 685 struct hat *hat = as->a_hat;
686 686 struct seg *seg, *next;
687 687 boolean_t free_started = B_FALSE;
688 688
689 689 top:
690 690 /*
691 691 * Invoke ALL callbacks. as_do_callbacks will do one callback
692 692 * per call, and not return (-1) until the callback has completed.
693 693 * When as_do_callbacks returns zero, all callbacks have completed.
694 694 */
695 695 mutex_enter(&as->a_contents);
696 696 while (as->a_callbacks && as_do_callbacks(as, AS_ALL_EVENT, 0, 0))
697 697 ;
698 698
699 699 mutex_exit(&as->a_contents);
700 700 AS_LOCK_ENTER(as, RW_WRITER);
701 701
702 702 if (!free_started) {
703 703 free_started = B_TRUE;
704 704 hat_free_start(hat);
705 705 }
706 706 for (seg = AS_SEGFIRST(as); seg != NULL; seg = next) {
707 707 int err;
708 708
709 709 next = AS_SEGNEXT(as, seg);
710 710 retry:
711 711 err = SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
712 712 if (err == EAGAIN) {
713 713 mutex_enter(&as->a_contents);
714 714 if (as->a_callbacks) {
715 715 AS_LOCK_EXIT(as);
716 716 } else if (!AS_ISNOUNMAPWAIT(as)) {
717 717 /*
718 718 * Memory is currently locked. Wait for a
719 719 * cv_signal that it has been unlocked, then
720 720 * try the operation again.
721 721 */
722 722 if (AS_ISUNMAPWAIT(as) == 0)
723 723 cv_broadcast(&as->a_cv);
724 724 AS_SETUNMAPWAIT(as);
725 725 AS_LOCK_EXIT(as);
726 726 while (AS_ISUNMAPWAIT(as))
727 727 cv_wait(&as->a_cv, &as->a_contents);
728 728 } else {
729 729 /*
730 730 * We may have raced with
731 731 * segvn_reclaim()/segspt_reclaim(). In this
732 732 * case clean nounmapwait flag and retry since
733 733 * softlockcnt in this segment may be already
734 734 * 0. We don't drop as writer lock so our
735 735 * number of retries without sleeping should
736 736 * be very small. See segvn_reclaim() for
737 737 * more comments.
738 738 */
739 739 AS_CLRNOUNMAPWAIT(as);
740 740 mutex_exit(&as->a_contents);
741 741 goto retry;
742 742 }
743 743 mutex_exit(&as->a_contents);
744 744 goto top;
745 745 } else {
746 746 /*
747 747 * We do not expect any other error return at this
748 748 * time. This is similar to an ASSERT in seg_unmap()
749 749 */
750 750 ASSERT(err == 0);
751 751 }
752 752 }
753 753 hat_free_end(hat);
754 754 AS_LOCK_EXIT(as);
755 755
756 756 /* /proc stuff */
757 757 ASSERT(avl_numnodes(&as->a_wpage) == 0);
758 758 if (as->a_objectdir) {
759 759 kmem_free(as->a_objectdir, as->a_sizedir * sizeof (vnode_t *));
760 760 as->a_objectdir = NULL;
761 761 as->a_sizedir = 0;
762 762 }
763 763
764 764 /*
765 765 * Free the struct as back to kmem. Assert it has no segments.
766 766 */
767 767 ASSERT(avl_numnodes(&as->a_segtree) == 0);
768 768 kmem_cache_free(as_cache, as);
769 769 }
770 770
771 771 int
772 772 as_dup(struct as *as, struct proc *forkedproc)
773 773 {
774 774 struct as *newas;
775 775 struct seg *seg, *newseg;
776 776 size_t purgesize = 0;
777 777 int error;
778 778
779 779 AS_LOCK_ENTER(as, RW_WRITER);
780 780 as_clearwatch(as);
781 781 newas = as_alloc();
782 782 newas->a_userlimit = as->a_userlimit;
783 783 newas->a_proc = forkedproc;
784 784
785 785 AS_LOCK_ENTER(newas, RW_WRITER);
786 786
787 787 (void) hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_SRD);
788 788
789 789 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
790 790
791 791 if (seg->s_flags & S_PURGE) {
792 792 purgesize += seg->s_size;
793 793 continue;
794 794 }
795 795
796 796 newseg = seg_alloc(newas, seg->s_base, seg->s_size);
797 797 if (newseg == NULL) {
798 798 AS_LOCK_EXIT(newas);
799 799 as_setwatch(as);
800 800 AS_LOCK_EXIT(as);
801 801 as_free(newas);
802 802 return (-1);
803 803 }
804 804 if ((error = SEGOP_DUP(seg, newseg)) != 0) {
805 805 /*
806 806 * We call seg_free() on the new seg
807 807 * because the segment is not set up
808 808 * completely; i.e. it has no ops.
809 809 */
810 810 as_setwatch(as);
811 811 AS_LOCK_EXIT(as);
812 812 seg_free(newseg);
813 813 AS_LOCK_EXIT(newas);
814 814 as_free(newas);
815 815 return (error);
816 816 }
817 817 newas->a_size += seg->s_size;
818 818 }
819 819 newas->a_resvsize = as->a_resvsize - purgesize;
820 820
821 821 error = hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_ALL);
822 822
823 823 AS_LOCK_EXIT(newas);
824 824
825 825 as_setwatch(as);
826 826 AS_LOCK_EXIT(as);
827 827 if (error != 0) {
828 828 as_free(newas);
829 829 return (error);
830 830 }
831 831 forkedproc->p_as = newas;
832 832 return (0);
833 833 }
834 834
835 835 /*
836 836 * Handle a ``fault'' at addr for size bytes.
837 837 */
838 838 faultcode_t
839 839 as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size,
840 840 enum fault_type type, enum seg_rw rw)
841 841 {
842 842 struct seg *seg;
843 843 caddr_t raddr; /* rounded down addr */
844 844 size_t rsize; /* rounded up size */
845 845 size_t ssize;
846 846 faultcode_t res = 0;
847 847 caddr_t addrsav;
848 848 struct seg *segsav;
849 849 int as_lock_held;
850 850 klwp_t *lwp = ttolwp(curthread);
851 851
852 852
853 853
854 854 retry:
855 855 /*
856 856 * Indicate that the lwp is not to be stopped while waiting for a
857 857 * pagefault. This is to avoid deadlock while debugging a process
858 858 * via /proc over NFS (in particular).
859 859 */
860 860 if (lwp != NULL)
861 861 lwp->lwp_nostop++;
862 862
863 863 /*
864 864 * same length must be used when we softlock and softunlock. We
865 865 * don't support softunlocking lengths less than the original length
866 866 * when there is largepage support. See seg_dev.c for more
867 867 * comments.
868 868 */
869 869 switch (type) {
870 870
871 871 case F_SOFTLOCK:
872 872 CPU_STATS_ADD_K(vm, softlock, 1);
873 873 break;
874 874
875 875 case F_SOFTUNLOCK:
876 876 break;
877 877
878 878 case F_PROT:
879 879 CPU_STATS_ADD_K(vm, prot_fault, 1);
880 880 break;
881 881
882 882 case F_INVAL:
883 883 CPU_STATS_ENTER_K();
884 884 CPU_STATS_ADDQ(CPU, vm, as_fault, 1);
885 885 if (as == &kas)
886 886 CPU_STATS_ADDQ(CPU, vm, kernel_asflt, 1);
887 887 CPU_STATS_EXIT_K();
888 888 break;
889 889 }
890 890
891 891 /* Kernel probe */
892 892 TNF_PROBE_3(address_fault, "vm pagefault", /* CSTYLED */,
893 893 tnf_opaque, address, addr,
894 894 tnf_fault_type, fault_type, type,
895 895 tnf_seg_access, access, rw);
896 896
897 897 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
898 898 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
899 899 (size_t)raddr;
900 900
901 901 /*
902 902 * XXX -- Don't grab the as lock for segkmap. We should grab it for
903 903 * correctness, but then we could be stuck holding this lock for
904 904 * a LONG time if the fault needs to be resolved on a slow
905 905 * filesystem, and then no-one will be able to exec new commands,
906 906 * as exec'ing requires the write lock on the as.
907 907 */
908 908 if (as == &kas && segkmap && segkmap->s_base <= raddr &&
909 909 raddr + size < segkmap->s_base + segkmap->s_size) {
910 910 seg = segkmap;
911 911 as_lock_held = 0;
912 912 } else {
913 913 AS_LOCK_ENTER(as, RW_READER);
914 914
915 915 seg = as_segat(as, raddr);
916 916 if (seg == NULL) {
917 917 AS_LOCK_EXIT(as);
918 918 if (lwp != NULL)
919 919 lwp->lwp_nostop--;
920 920 return (FC_NOMAP);
921 921 }
922 922
923 923 as_lock_held = 1;
924 924 }
925 925
926 926 addrsav = raddr;
927 927 segsav = seg;
928 928
929 929 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
930 930 if (raddr >= seg->s_base + seg->s_size) {
931 931 seg = AS_SEGNEXT(as, seg);
932 932 if (seg == NULL || raddr != seg->s_base) {
933 933 res = FC_NOMAP;
934 934 break;
935 935 }
936 936 }
937 937 if (raddr + rsize > seg->s_base + seg->s_size)
938 938 ssize = seg->s_base + seg->s_size - raddr;
939 939 else
940 940 ssize = rsize;
941 941
942 942 res = SEGOP_FAULT(hat, seg, raddr, ssize, type, rw);
943 943 if (res != 0)
944 944 break;
945 945 }
946 946
947 947 /*
948 948 * If we were SOFTLOCKing and encountered a failure,
949 949 * we must SOFTUNLOCK the range we already did. (Maybe we
950 950 * should just panic if we are SOFTLOCKing or even SOFTUNLOCKing
951 951 * right here...)
952 952 */
953 953 if (res != 0 && type == F_SOFTLOCK) {
954 954 for (seg = segsav; addrsav < raddr; addrsav += ssize) {
955 955 if (addrsav >= seg->s_base + seg->s_size)
956 956 seg = AS_SEGNEXT(as, seg);
957 957 ASSERT(seg != NULL);
958 958 /*
959 959 * Now call the fault routine again to perform the
960 960 * unlock using S_OTHER instead of the rw variable
961 961 * since we never got a chance to touch the pages.
962 962 */
963 963 if (raddr > seg->s_base + seg->s_size)
964 964 ssize = seg->s_base + seg->s_size - addrsav;
965 965 else
966 966 ssize = raddr - addrsav;
967 967 (void) SEGOP_FAULT(hat, seg, addrsav, ssize,
968 968 F_SOFTUNLOCK, S_OTHER);
969 969 }
970 970 }
971 971 if (as_lock_held)
972 972 AS_LOCK_EXIT(as);
973 973 if (lwp != NULL)
974 974 lwp->lwp_nostop--;
975 975
976 976 /*
977 977 * If the lower levels returned EDEADLK for a fault,
978 978 * It means that we should retry the fault. Let's wait
979 979 * a bit also to let the deadlock causing condition clear.
980 980 * This is part of a gross hack to work around a design flaw
981 981 * in the ufs/sds logging code and should go away when the
982 982 * logging code is re-designed to fix the problem. See bug
983 983 * 4125102 for details of the problem.
984 984 */
985 985 if (FC_ERRNO(res) == EDEADLK) {
986 986 delay(deadlk_wait);
987 987 res = 0;
988 988 goto retry;
989 989 }
990 990 return (res);
991 991 }
992 992
993 993
994 994
995 995 /*
996 996 * Asynchronous ``fault'' at addr for size bytes.
997 997 */
998 998 faultcode_t
999 999 as_faulta(struct as *as, caddr_t addr, size_t size)
1000 1000 {
1001 1001 struct seg *seg;
1002 1002 caddr_t raddr; /* rounded down addr */
1003 1003 size_t rsize; /* rounded up size */
1004 1004 faultcode_t res = 0;
1005 1005 klwp_t *lwp = ttolwp(curthread);
1006 1006
1007 1007 retry:
1008 1008 /*
1009 1009 * Indicate that the lwp is not to be stopped while waiting
1010 1010 * for a pagefault. This is to avoid deadlock while debugging
1011 1011 * a process via /proc over NFS (in particular).
1012 1012 */
1013 1013 if (lwp != NULL)
1014 1014 lwp->lwp_nostop++;
1015 1015
1016 1016 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1017 1017 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
1018 1018 (size_t)raddr;
1019 1019
1020 1020 AS_LOCK_ENTER(as, RW_READER);
1021 1021 seg = as_segat(as, raddr);
1022 1022 if (seg == NULL) {
1023 1023 AS_LOCK_EXIT(as);
1024 1024 if (lwp != NULL)
1025 1025 lwp->lwp_nostop--;
1026 1026 return (FC_NOMAP);
1027 1027 }
1028 1028
1029 1029 for (; rsize != 0; rsize -= PAGESIZE, raddr += PAGESIZE) {
1030 1030 if (raddr >= seg->s_base + seg->s_size) {
1031 1031 seg = AS_SEGNEXT(as, seg);
1032 1032 if (seg == NULL || raddr != seg->s_base) {
1033 1033 res = FC_NOMAP;
1034 1034 break;
1035 1035 }
1036 1036 }
1037 1037 res = SEGOP_FAULTA(seg, raddr);
1038 1038 if (res != 0)
1039 1039 break;
1040 1040 }
1041 1041 AS_LOCK_EXIT(as);
1042 1042 if (lwp != NULL)
1043 1043 lwp->lwp_nostop--;
1044 1044 /*
1045 1045 * If the lower levels returned EDEADLK for a fault,
1046 1046 * It means that we should retry the fault. Let's wait
1047 1047 * a bit also to let the deadlock causing condition clear.
1048 1048 * This is part of a gross hack to work around a design flaw
1049 1049 * in the ufs/sds logging code and should go away when the
1050 1050 * logging code is re-designed to fix the problem. See bug
1051 1051 * 4125102 for details of the problem.
1052 1052 */
1053 1053 if (FC_ERRNO(res) == EDEADLK) {
1054 1054 delay(deadlk_wait);
1055 1055 res = 0;
1056 1056 goto retry;
1057 1057 }
1058 1058 return (res);
1059 1059 }
1060 1060
1061 1061 /*
1062 1062 * Set the virtual mapping for the interval from [addr : addr + size)
1063 1063 * in address space `as' to have the specified protection.
1064 1064 * It is ok for the range to cross over several segments,
1065 1065 * as long as they are contiguous.
1066 1066 */
1067 1067 int
1068 1068 as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
1069 1069 {
1070 1070 struct seg *seg;
1071 1071 struct as_callback *cb;
1072 1072 size_t ssize;
1073 1073 caddr_t raddr; /* rounded down addr */
1074 1074 size_t rsize; /* rounded up size */
1075 1075 int error = 0, writer = 0;
1076 1076 caddr_t saveraddr;
1077 1077 size_t saversize;
1078 1078
1079 1079 setprot_top:
1080 1080 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1081 1081 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
1082 1082 (size_t)raddr;
1083 1083
1084 1084 if (raddr + rsize < raddr) /* check for wraparound */
1085 1085 return (ENOMEM);
1086 1086
1087 1087 saveraddr = raddr;
1088 1088 saversize = rsize;
1089 1089
1090 1090 /*
1091 1091 * Normally we only lock the as as a reader. But
1092 1092 * if due to setprot the segment driver needs to split
1093 1093 * a segment it will return IE_RETRY. Therefore we re-acquire
1094 1094 * the as lock as a writer so the segment driver can change
1095 1095 * the seg list. Also the segment driver will return IE_RETRY
1096 1096 * after it has changed the segment list so we therefore keep
1097 1097 * locking as a writer. Since these opeartions should be rare
1098 1098 * want to only lock as a writer when necessary.
1099 1099 */
1100 1100 if (writer || avl_numnodes(&as->a_wpage) != 0) {
1101 1101 AS_LOCK_ENTER(as, RW_WRITER);
1102 1102 } else {
1103 1103 AS_LOCK_ENTER(as, RW_READER);
1104 1104 }
1105 1105
1106 1106 as_clearwatchprot(as, raddr, rsize);
1107 1107 seg = as_segat(as, raddr);
1108 1108 if (seg == NULL) {
1109 1109 as_setwatch(as);
1110 1110 AS_LOCK_EXIT(as);
1111 1111 return (ENOMEM);
1112 1112 }
1113 1113
1114 1114 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
1115 1115 if (raddr >= seg->s_base + seg->s_size) {
1116 1116 seg = AS_SEGNEXT(as, seg);
1117 1117 if (seg == NULL || raddr != seg->s_base) {
1118 1118 error = ENOMEM;
1119 1119 break;
1120 1120 }
1121 1121 }
1122 1122 if ((raddr + rsize) > (seg->s_base + seg->s_size))
1123 1123 ssize = seg->s_base + seg->s_size - raddr;
1124 1124 else
1125 1125 ssize = rsize;
1126 1126 retry:
1127 1127 error = SEGOP_SETPROT(seg, raddr, ssize, prot);
1128 1128
1129 1129 if (error == IE_NOMEM) {
1130 1130 error = EAGAIN;
1131 1131 break;
1132 1132 }
1133 1133
1134 1134 if (error == IE_RETRY) {
1135 1135 AS_LOCK_EXIT(as);
1136 1136 writer = 1;
1137 1137 goto setprot_top;
1138 1138 }
1139 1139
1140 1140 if (error == EAGAIN) {
1141 1141 /*
1142 1142 * Make sure we have a_lock as writer.
1143 1143 */
1144 1144 if (writer == 0) {
1145 1145 AS_LOCK_EXIT(as);
1146 1146 writer = 1;
1147 1147 goto setprot_top;
1148 1148 }
1149 1149
1150 1150 /*
1151 1151 * Memory is currently locked. It must be unlocked
1152 1152 * before this operation can succeed through a retry.
1153 1153 * The possible reasons for locked memory and
1154 1154 * corresponding strategies for unlocking are:
1155 1155 * (1) Normal I/O
1156 1156 * wait for a signal that the I/O operation
1157 1157 * has completed and the memory is unlocked.
1158 1158 * (2) Asynchronous I/O
1159 1159 * The aio subsystem does not unlock pages when
1160 1160 * the I/O is completed. Those pages are unlocked
1161 1161 * when the application calls aiowait/aioerror.
1162 1162 * So, to prevent blocking forever, cv_broadcast()
1163 1163 * is done to wake up aio_cleanup_thread.
1164 1164 * Subsequently, segvn_reclaim will be called, and
1165 1165 * that will do AS_CLRUNMAPWAIT() and wake us up.
1166 1166 * (3) Long term page locking:
1167 1167 * Drivers intending to have pages locked for a
1168 1168 * period considerably longer than for normal I/O
1169 1169 * (essentially forever) may have registered for a
1170 1170 * callback so they may unlock these pages on
1171 1171 * request. This is needed to allow this operation
1172 1172 * to succeed. Each entry on the callback list is
1173 1173 * examined. If the event or address range pertains
1174 1174 * the callback is invoked (unless it already is in
1175 1175 * progress). The a_contents lock must be dropped
1176 1176 * before the callback, so only one callback can
1177 1177 * be done at a time. Go to the top and do more
1178 1178 * until zero is returned. If zero is returned,
1179 1179 * either there were no callbacks for this event
1180 1180 * or they were already in progress.
1181 1181 */
1182 1182 mutex_enter(&as->a_contents);
1183 1183 if (as->a_callbacks &&
1184 1184 (cb = as_find_callback(as, AS_SETPROT_EVENT,
1185 1185 seg->s_base, seg->s_size))) {
1186 1186 AS_LOCK_EXIT(as);
1187 1187 as_execute_callback(as, cb, AS_SETPROT_EVENT);
1188 1188 } else if (!AS_ISNOUNMAPWAIT(as)) {
1189 1189 if (AS_ISUNMAPWAIT(as) == 0)
1190 1190 cv_broadcast(&as->a_cv);
1191 1191 AS_SETUNMAPWAIT(as);
1192 1192 AS_LOCK_EXIT(as);
1193 1193 while (AS_ISUNMAPWAIT(as))
1194 1194 cv_wait(&as->a_cv, &as->a_contents);
1195 1195 } else {
1196 1196 /*
1197 1197 * We may have raced with
1198 1198 * segvn_reclaim()/segspt_reclaim(). In this
1199 1199 * case clean nounmapwait flag and retry since
1200 1200 * softlockcnt in this segment may be already
1201 1201 * 0. We don't drop as writer lock so our
1202 1202 * number of retries without sleeping should
1203 1203 * be very small. See segvn_reclaim() for
1204 1204 * more comments.
1205 1205 */
1206 1206 AS_CLRNOUNMAPWAIT(as);
1207 1207 mutex_exit(&as->a_contents);
1208 1208 goto retry;
1209 1209 }
1210 1210 mutex_exit(&as->a_contents);
1211 1211 goto setprot_top;
1212 1212 } else if (error != 0)
1213 1213 break;
1214 1214 }
1215 1215 if (error != 0) {
1216 1216 as_setwatch(as);
1217 1217 } else {
1218 1218 as_setwatchprot(as, saveraddr, saversize, prot);
1219 1219 }
1220 1220 AS_LOCK_EXIT(as);
1221 1221 return (error);
1222 1222 }
1223 1223
1224 1224 /*
1225 1225 * Check to make sure that the interval [addr, addr + size)
1226 1226 * in address space `as' has at least the specified protection.
1227 1227 * It is ok for the range to cross over several segments, as long
1228 1228 * as they are contiguous.
1229 1229 */
1230 1230 int
1231 1231 as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
1232 1232 {
1233 1233 struct seg *seg;
1234 1234 size_t ssize;
1235 1235 caddr_t raddr; /* rounded down addr */
1236 1236 size_t rsize; /* rounded up size */
1237 1237 int error = 0;
1238 1238
1239 1239 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1240 1240 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
1241 1241 (size_t)raddr;
1242 1242
1243 1243 if (raddr + rsize < raddr) /* check for wraparound */
1244 1244 return (ENOMEM);
1245 1245
1246 1246 /*
1247 1247 * This is ugly as sin...
1248 1248 * Normally, we only acquire the address space readers lock.
1249 1249 * However, if the address space has watchpoints present,
1250 1250 * we must acquire the writer lock on the address space for
1251 1251 * the benefit of as_clearwatchprot() and as_setwatchprot().
1252 1252 */
1253 1253 if (avl_numnodes(&as->a_wpage) != 0)
1254 1254 AS_LOCK_ENTER(as, RW_WRITER);
1255 1255 else
1256 1256 AS_LOCK_ENTER(as, RW_READER);
1257 1257 as_clearwatchprot(as, raddr, rsize);
1258 1258 seg = as_segat(as, raddr);
1259 1259 if (seg == NULL) {
1260 1260 as_setwatch(as);
1261 1261 AS_LOCK_EXIT(as);
1262 1262 return (ENOMEM);
1263 1263 }
1264 1264
1265 1265 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
1266 1266 if (raddr >= seg->s_base + seg->s_size) {
1267 1267 seg = AS_SEGNEXT(as, seg);
1268 1268 if (seg == NULL || raddr != seg->s_base) {
1269 1269 error = ENOMEM;
1270 1270 break;
1271 1271 }
1272 1272 }
1273 1273 if ((raddr + rsize) > (seg->s_base + seg->s_size))
1274 1274 ssize = seg->s_base + seg->s_size - raddr;
1275 1275 else
1276 1276 ssize = rsize;
1277 1277
1278 1278 error = SEGOP_CHECKPROT(seg, raddr, ssize, prot);
1279 1279 if (error != 0)
1280 1280 break;
1281 1281 }
1282 1282 as_setwatch(as);
1283 1283 AS_LOCK_EXIT(as);
1284 1284 return (error);
1285 1285 }
1286 1286
1287 1287 int
1288 1288 as_unmap(struct as *as, caddr_t addr, size_t size)
1289 1289 {
1290 1290 struct seg *seg, *seg_next;
1291 1291 struct as_callback *cb;
1292 1292 caddr_t raddr, eaddr;
1293 1293 size_t ssize, rsize = 0;
1294 1294 int err;
1295 1295
1296 1296 top:
1297 1297 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1298 1298 eaddr = (caddr_t)(((uintptr_t)(addr + size) + PAGEOFFSET) &
1299 1299 (uintptr_t)PAGEMASK);
1300 1300
1301 1301 AS_LOCK_ENTER(as, RW_WRITER);
1302 1302
1303 1303 as->a_updatedir = 1; /* inform /proc */
1304 1304 gethrestime(&as->a_updatetime);
1305 1305
1306 1306 /*
1307 1307 * Use as_findseg to find the first segment in the range, then
1308 1308 * step through the segments in order, following s_next.
1309 1309 */
1310 1310 as_clearwatchprot(as, raddr, eaddr - raddr);
1311 1311
1312 1312 for (seg = as_findseg(as, raddr, 0); seg != NULL; seg = seg_next) {
1313 1313 if (eaddr <= seg->s_base)
1314 1314 break; /* eaddr was in a gap; all done */
1315 1315
1316 1316 /* this is implied by the test above */
1317 1317 ASSERT(raddr < eaddr);
1318 1318
1319 1319 if (raddr < seg->s_base)
1320 1320 raddr = seg->s_base; /* raddr was in a gap */
1321 1321
1322 1322 if (eaddr > (seg->s_base + seg->s_size))
1323 1323 ssize = seg->s_base + seg->s_size - raddr;
1324 1324 else
1325 1325 ssize = eaddr - raddr;
1326 1326
1327 1327 /*
1328 1328 * Save next segment pointer since seg can be
1329 1329 * destroyed during the segment unmap operation.
1330 1330 */
1331 1331 seg_next = AS_SEGNEXT(as, seg);
1332 1332
1333 1333 /*
1334 1334 * We didn't count /dev/null mappings, so ignore them here.
1335 1335 * We'll handle MAP_NORESERVE cases in segvn_unmap(). (Again,
1336 1336 * we have to do this check here while we have seg.)
1337 1337 */
1338 1338 rsize = 0;
1339 1339 if (!SEG_IS_DEVNULL_MAPPING(seg) &&
1340 1340 !SEG_IS_PARTIAL_RESV(seg))
1341 1341 rsize = ssize;
1342 1342
1343 1343 retry:
1344 1344 err = SEGOP_UNMAP(seg, raddr, ssize);
1345 1345 if (err == EAGAIN) {
1346 1346 /*
1347 1347 * Memory is currently locked. It must be unlocked
1348 1348 * before this operation can succeed through a retry.
1349 1349 * The possible reasons for locked memory and
1350 1350 * corresponding strategies for unlocking are:
1351 1351 * (1) Normal I/O
1352 1352 * wait for a signal that the I/O operation
1353 1353 * has completed and the memory is unlocked.
1354 1354 * (2) Asynchronous I/O
1355 1355 * The aio subsystem does not unlock pages when
1356 1356 * the I/O is completed. Those pages are unlocked
1357 1357 * when the application calls aiowait/aioerror.
1358 1358 * So, to prevent blocking forever, cv_broadcast()
1359 1359 * is done to wake up aio_cleanup_thread.
1360 1360 * Subsequently, segvn_reclaim will be called, and
1361 1361 * that will do AS_CLRUNMAPWAIT() and wake us up.
1362 1362 * (3) Long term page locking:
1363 1363 * Drivers intending to have pages locked for a
1364 1364 * period considerably longer than for normal I/O
1365 1365 * (essentially forever) may have registered for a
1366 1366 * callback so they may unlock these pages on
1367 1367 * request. This is needed to allow this operation
1368 1368 * to succeed. Each entry on the callback list is
1369 1369 * examined. If the event or address range pertains
1370 1370 * the callback is invoked (unless it already is in
1371 1371 * progress). The a_contents lock must be dropped
1372 1372 * before the callback, so only one callback can
1373 1373 * be done at a time. Go to the top and do more
1374 1374 * until zero is returned. If zero is returned,
1375 1375 * either there were no callbacks for this event
1376 1376 * or they were already in progress.
1377 1377 */
1378 1378 mutex_enter(&as->a_contents);
1379 1379 if (as->a_callbacks &&
1380 1380 (cb = as_find_callback(as, AS_UNMAP_EVENT,
1381 1381 seg->s_base, seg->s_size))) {
1382 1382 AS_LOCK_EXIT(as);
1383 1383 as_execute_callback(as, cb, AS_UNMAP_EVENT);
1384 1384 } else if (!AS_ISNOUNMAPWAIT(as)) {
1385 1385 if (AS_ISUNMAPWAIT(as) == 0)
1386 1386 cv_broadcast(&as->a_cv);
1387 1387 AS_SETUNMAPWAIT(as);
1388 1388 AS_LOCK_EXIT(as);
1389 1389 while (AS_ISUNMAPWAIT(as))
1390 1390 cv_wait(&as->a_cv, &as->a_contents);
1391 1391 } else {
1392 1392 /*
1393 1393 * We may have raced with
1394 1394 * segvn_reclaim()/segspt_reclaim(). In this
1395 1395 * case clean nounmapwait flag and retry since
1396 1396 * softlockcnt in this segment may be already
1397 1397 * 0. We don't drop as writer lock so our
1398 1398 * number of retries without sleeping should
1399 1399 * be very small. See segvn_reclaim() for
1400 1400 * more comments.
1401 1401 */
1402 1402 AS_CLRNOUNMAPWAIT(as);
1403 1403 mutex_exit(&as->a_contents);
1404 1404 goto retry;
1405 1405 }
1406 1406 mutex_exit(&as->a_contents);
1407 1407 goto top;
1408 1408 } else if (err == IE_RETRY) {
1409 1409 AS_LOCK_EXIT(as);
1410 1410 goto top;
1411 1411 } else if (err) {
1412 1412 as_setwatch(as);
1413 1413 AS_LOCK_EXIT(as);
1414 1414 return (-1);
1415 1415 }
1416 1416
1417 1417 as->a_size -= ssize;
1418 1418 if (rsize)
1419 1419 as->a_resvsize -= rsize;
1420 1420 raddr += ssize;
1421 1421 }
1422 1422 AS_LOCK_EXIT(as);
1423 1423 return (0);
1424 1424 }
1425 1425
1426 1426 static int
1427 1427 as_map_segvn_segs(struct as *as, caddr_t addr, size_t size, uint_t szcvec,
1428 1428 int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
1429 1429 {
1430 1430 uint_t szc;
1431 1431 uint_t nszc;
1432 1432 int error;
1433 1433 caddr_t a;
1434 1434 caddr_t eaddr;
1435 1435 size_t segsize;
1436 1436 struct seg *seg;
1437 1437 size_t pgsz;
1438 1438 int do_off = (vn_a->vp != NULL || vn_a->amp != NULL);
1439 1439 uint_t save_szcvec;
1440 1440
1441 1441 ASSERT(AS_WRITE_HELD(as));
1442 1442 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
1443 1443 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
1444 1444 ASSERT(vn_a->vp == NULL || vn_a->amp == NULL);
1445 1445 if (!do_off) {
1446 1446 vn_a->offset = 0;
1447 1447 }
1448 1448
1449 1449 if (szcvec <= 1) {
1450 1450 seg = seg_alloc(as, addr, size);
1451 1451 if (seg == NULL) {
1452 1452 return (ENOMEM);
1453 1453 }
1454 1454 vn_a->szc = 0;
1455 1455 error = (*crfp)(seg, vn_a);
1456 1456 if (error != 0) {
1457 1457 seg_free(seg);
1458 1458 } else {
1459 1459 as->a_size += size;
1460 1460 as->a_resvsize += size;
1461 1461 }
1462 1462 return (error);
1463 1463 }
1464 1464
1465 1465 eaddr = addr + size;
1466 1466 save_szcvec = szcvec;
1467 1467 szcvec >>= 1;
1468 1468 szc = 0;
1469 1469 nszc = 0;
1470 1470 while (szcvec) {
1471 1471 if ((szcvec & 0x1) == 0) {
1472 1472 nszc++;
1473 1473 szcvec >>= 1;
1474 1474 continue;
1475 1475 }
1476 1476 nszc++;
1477 1477 pgsz = page_get_pagesize(nszc);
1478 1478 a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
1479 1479 if (a != addr) {
1480 1480 ASSERT(a < eaddr);
1481 1481 segsize = a - addr;
1482 1482 seg = seg_alloc(as, addr, segsize);
1483 1483 if (seg == NULL) {
1484 1484 return (ENOMEM);
1485 1485 }
1486 1486 vn_a->szc = szc;
1487 1487 error = (*crfp)(seg, vn_a);
1488 1488 if (error != 0) {
1489 1489 seg_free(seg);
1490 1490 return (error);
1491 1491 }
1492 1492 as->a_size += segsize;
1493 1493 as->a_resvsize += segsize;
1494 1494 *segcreated = 1;
1495 1495 if (do_off) {
1496 1496 vn_a->offset += segsize;
1497 1497 }
1498 1498 addr = a;
1499 1499 }
1500 1500 szc = nszc;
1501 1501 szcvec >>= 1;
1502 1502 }
1503 1503
1504 1504 ASSERT(addr < eaddr);
1505 1505 szcvec = save_szcvec | 1; /* add 8K pages */
1506 1506 while (szcvec) {
1507 1507 a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
1508 1508 ASSERT(a >= addr);
1509 1509 if (a != addr) {
1510 1510 segsize = a - addr;
1511 1511 seg = seg_alloc(as, addr, segsize);
1512 1512 if (seg == NULL) {
1513 1513 return (ENOMEM);
1514 1514 }
1515 1515 vn_a->szc = szc;
1516 1516 error = (*crfp)(seg, vn_a);
1517 1517 if (error != 0) {
1518 1518 seg_free(seg);
1519 1519 return (error);
1520 1520 }
1521 1521 as->a_size += segsize;
1522 1522 as->a_resvsize += segsize;
1523 1523 *segcreated = 1;
1524 1524 if (do_off) {
1525 1525 vn_a->offset += segsize;
1526 1526 }
1527 1527 addr = a;
1528 1528 }
1529 1529 szcvec &= ~(1 << szc);
1530 1530 if (szcvec) {
1531 1531 szc = highbit(szcvec) - 1;
1532 1532 pgsz = page_get_pagesize(szc);
1533 1533 }
1534 1534 }
1535 1535 ASSERT(addr == eaddr);
1536 1536
1537 1537 return (0);
1538 1538 }
1539 1539
1540 1540 static int
1541 1541 as_map_vnsegs(struct as *as, caddr_t addr, size_t size,
1542 1542 int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
1543 1543 {
1544 1544 uint_t mapflags = vn_a->flags & (MAP_TEXT | MAP_INITDATA);
1545 1545 int type = (vn_a->type == MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
1546 1546 uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
1547 1547 type, 0);
1548 1548 int error;
1549 1549 struct seg *seg;
1550 1550 struct vattr va;
1551 1551 u_offset_t eoff;
1552 1552 size_t save_size = 0;
1553 1553 extern size_t textrepl_size_thresh;
1554 1554
1555 1555 ASSERT(AS_WRITE_HELD(as));
1556 1556 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
1557 1557 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
1558 1558 ASSERT(vn_a->vp != NULL);
1559 1559 ASSERT(vn_a->amp == NULL);
1560 1560
1561 1561 again:
1562 1562 if (szcvec <= 1) {
1563 1563 seg = seg_alloc(as, addr, size);
1564 1564 if (seg == NULL) {
1565 1565 return (ENOMEM);
1566 1566 }
1567 1567 vn_a->szc = 0;
1568 1568 error = (*crfp)(seg, vn_a);
1569 1569 if (error != 0) {
1570 1570 seg_free(seg);
1571 1571 } else {
1572 1572 as->a_size += size;
1573 1573 as->a_resvsize += size;
1574 1574 }
1575 1575 return (error);
1576 1576 }
1577 1577
1578 1578 va.va_mask = AT_SIZE;
1579 1579 if (VOP_GETATTR(vn_a->vp, &va, ATTR_HINT, vn_a->cred, NULL) != 0) {
1580 1580 szcvec = 0;
1581 1581 goto again;
1582 1582 }
1583 1583 eoff = vn_a->offset & PAGEMASK;
1584 1584 if (eoff >= va.va_size) {
1585 1585 szcvec = 0;
1586 1586 goto again;
1587 1587 }
1588 1588 eoff += size;
1589 1589 if (btopr(va.va_size) < btopr(eoff)) {
1590 1590 save_size = size;
1591 1591 size = va.va_size - (vn_a->offset & PAGEMASK);
1592 1592 size = P2ROUNDUP_TYPED(size, PAGESIZE, size_t);
1593 1593 szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
1594 1594 type, 0);
1595 1595 if (szcvec <= 1) {
1596 1596 size = save_size;
1597 1597 goto again;
1598 1598 }
1599 1599 }
1600 1600
1601 1601 if (size > textrepl_size_thresh) {
1602 1602 vn_a->flags |= _MAP_TEXTREPL;
1603 1603 }
1604 1604 error = as_map_segvn_segs(as, addr, size, szcvec, crfp, vn_a,
1605 1605 segcreated);
1606 1606 if (error != 0) {
1607 1607 return (error);
1608 1608 }
1609 1609 if (save_size) {
1610 1610 addr += size;
1611 1611 size = save_size - size;
1612 1612 szcvec = 0;
1613 1613 goto again;
1614 1614 }
1615 1615 return (0);
1616 1616 }
1617 1617
1618 1618 /*
1619 1619 * as_map_ansegs: shared or private anonymous memory. Note that the flags
1620 1620 * passed to map_pgszvec cannot be MAP_INITDATA, for anon.
1621 1621 */
1622 1622 static int
1623 1623 as_map_ansegs(struct as *as, caddr_t addr, size_t size,
1624 1624 int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
1625 1625 {
1626 1626 uint_t szcvec;
1627 1627 uchar_t type;
1628 1628
1629 1629 ASSERT(vn_a->type == MAP_SHARED || vn_a->type == MAP_PRIVATE);
1630 1630 if (vn_a->type == MAP_SHARED) {
1631 1631 type = MAPPGSZC_SHM;
1632 1632 } else if (vn_a->type == MAP_PRIVATE) {
1633 1633 if (vn_a->szc == AS_MAP_HEAP) {
1634 1634 type = MAPPGSZC_HEAP;
1635 1635 } else if (vn_a->szc == AS_MAP_STACK) {
1636 1636 type = MAPPGSZC_STACK;
1637 1637 } else {
1638 1638 type = MAPPGSZC_PRIVM;
1639 1639 }
1640 1640 }
1641 1641 szcvec = map_pgszcvec(addr, size, vn_a->amp == NULL ?
1642 1642 (uintptr_t)addr : (uintptr_t)P2ROUNDUP(vn_a->offset, PAGESIZE),
1643 1643 (vn_a->flags & MAP_TEXT), type, 0);
1644 1644 ASSERT(AS_WRITE_HELD(as));
1645 1645 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
1646 1646 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
1647 1647 ASSERT(vn_a->vp == NULL);
1648 1648
1649 1649 return (as_map_segvn_segs(as, addr, size, szcvec,
1650 1650 crfp, vn_a, segcreated));
1651 1651 }
1652 1652
1653 1653 int
1654 1654 as_map(struct as *as, caddr_t addr, size_t size, int (*crfp)(), void *argsp)
1655 1655 {
1656 1656 AS_LOCK_ENTER(as, RW_WRITER);
1657 1657 return (as_map_locked(as, addr, size, crfp, argsp));
1658 1658 }
1659 1659
1660 1660 int
1661 1661 as_map_locked(struct as *as, caddr_t addr, size_t size, int (*crfp)(),
1662 1662 void *argsp)
1663 1663 {
1664 1664 struct seg *seg = NULL;
1665 1665 caddr_t raddr; /* rounded down addr */
1666 1666 size_t rsize; /* rounded up size */
1667 1667 int error;
1668 1668 int unmap = 0;
1669 1669 struct proc *p = curproc;
1670 1670 struct segvn_crargs crargs;
1671 1671
1672 1672 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1673 1673 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
1674 1674 (size_t)raddr;
1675 1675
1676 1676 /*
1677 1677 * check for wrap around
1678 1678 */
1679 1679 if ((raddr + rsize < raddr) || (as->a_size > (ULONG_MAX - size))) {
1680 1680 AS_LOCK_EXIT(as);
1681 1681 return (ENOMEM);
1682 1682 }
1683 1683
1684 1684 as->a_updatedir = 1; /* inform /proc */
1685 1685 gethrestime(&as->a_updatetime);
1686 1686
1687 1687 if (as != &kas && as->a_size + rsize > (size_t)p->p_vmem_ctl) {
1688 1688 AS_LOCK_EXIT(as);
1689 1689
1690 1690 (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
1691 1691 RCA_UNSAFE_ALL);
1692 1692
1693 1693 return (ENOMEM);
1694 1694 }
1695 1695
1696 1696 if (AS_MAP_CHECK_VNODE_LPOOB(crfp, argsp)) {
1697 1697 crargs = *(struct segvn_crargs *)argsp;
1698 1698 error = as_map_vnsegs(as, raddr, rsize, crfp, &crargs, &unmap);
1699 1699 if (error != 0) {
1700 1700 AS_LOCK_EXIT(as);
1701 1701 if (unmap) {
1702 1702 (void) as_unmap(as, addr, size);
1703 1703 }
1704 1704 return (error);
1705 1705 }
1706 1706 } else if (AS_MAP_CHECK_ANON_LPOOB(crfp, argsp)) {
1707 1707 crargs = *(struct segvn_crargs *)argsp;
1708 1708 error = as_map_ansegs(as, raddr, rsize, crfp, &crargs, &unmap);
1709 1709 if (error != 0) {
1710 1710 AS_LOCK_EXIT(as);
1711 1711 if (unmap) {
1712 1712 (void) as_unmap(as, addr, size);
1713 1713 }
1714 1714 return (error);
1715 1715 }
1716 1716 } else {
1717 1717 seg = seg_alloc(as, addr, size);
1718 1718 if (seg == NULL) {
1719 1719 AS_LOCK_EXIT(as);
1720 1720 return (ENOMEM);
1721 1721 }
1722 1722
1723 1723 error = (*crfp)(seg, argsp);
1724 1724 if (error != 0) {
1725 1725 seg_free(seg);
1726 1726 AS_LOCK_EXIT(as);
1727 1727 return (error);
1728 1728 }
1729 1729 /*
1730 1730 * Add size now so as_unmap will work if as_ctl fails.
1731 1731 */
1732 1732 as->a_size += rsize;
1733 1733 as->a_resvsize += rsize;
1734 1734 }
1735 1735
1736 1736 as_setwatch(as);
1737 1737
1738 1738 /*
1739 1739 * If the address space is locked,
1740 1740 * establish memory locks for the new segment.
1741 1741 */
1742 1742 mutex_enter(&as->a_contents);
1743 1743 if (AS_ISPGLCK(as)) {
1744 1744 mutex_exit(&as->a_contents);
1745 1745 AS_LOCK_EXIT(as);
1746 1746 error = as_ctl(as, addr, size, MC_LOCK, 0, 0, NULL, 0);
1747 1747 if (error != 0)
1748 1748 (void) as_unmap(as, addr, size);
1749 1749 } else {
1750 1750 mutex_exit(&as->a_contents);
1751 1751 AS_LOCK_EXIT(as);
1752 1752 }
1753 1753 return (error);
1754 1754 }
1755 1755
1756 1756
1757 1757 /*
1758 1758 * Delete all segments in the address space marked with S_PURGE.
1759 1759 * This is currently used for Sparc V9 nofault ASI segments (seg_nf.c).
1760 1760 * These segments are deleted as a first step before calls to as_gap(), so
1761 1761 * that they don't affect mmap() or shmat().
1762 1762 */
1763 1763 void
1764 1764 as_purge(struct as *as)
1765 1765 {
1766 1766 struct seg *seg;
1767 1767 struct seg *next_seg;
1768 1768
1769 1769 /*
1770 1770 * the setting of NEEDSPURGE is protect by as_rangelock(), so
1771 1771 * no need to grab a_contents mutex for this check
1772 1772 */
1773 1773 if ((as->a_flags & AS_NEEDSPURGE) == 0)
1774 1774 return;
1775 1775
1776 1776 AS_LOCK_ENTER(as, RW_WRITER);
1777 1777 next_seg = NULL;
1778 1778 seg = AS_SEGFIRST(as);
1779 1779 while (seg != NULL) {
1780 1780 next_seg = AS_SEGNEXT(as, seg);
1781 1781 if (seg->s_flags & S_PURGE)
1782 1782 SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
1783 1783 seg = next_seg;
1784 1784 }
1785 1785 AS_LOCK_EXIT(as);
1786 1786
1787 1787 mutex_enter(&as->a_contents);
1788 1788 as->a_flags &= ~AS_NEEDSPURGE;
1789 1789 mutex_exit(&as->a_contents);
1790 1790 }
1791 1791
1792 1792 /*
1793 1793 * Find a hole within [*basep, *basep + *lenp), which contains a mappable
1794 1794 * range of addresses at least "minlen" long, where the base of the range is
1795 1795 * at "off" phase from an "align" boundary and there is space for a
1796 1796 * "redzone"-sized redzone on eithe rside of the range. Thus,
1797 1797 * if align was 4M and off was 16k, the user wants a hole which will start
1798 1798 * 16k into a 4M page.
1799 1799 *
1800 1800 * If flags specifies AH_HI, the hole will have the highest possible address
1801 1801 * in the range. We use the as->a_lastgap field to figure out where to
1802 1802 * start looking for a gap.
1803 1803 *
1804 1804 * Otherwise, the gap will have the lowest possible address.
1805 1805 *
1806 1806 * If flags specifies AH_CONTAIN, the hole will contain the address addr.
1807 1807 *
1808 1808 * If an adequate hole is found, *basep and *lenp are set to reflect the part of
1809 1809 * the hole that is within range, and 0 is returned. On failure, -1 is returned.
1810 1810 *
1811 1811 * NOTE: This routine is not correct when base+len overflows caddr_t.
1812 1812 */
1813 1813 int
1814 1814 as_gap_aligned(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp,
1815 1815 uint_t flags, caddr_t addr, size_t align, size_t redzone, size_t off)
1816 1816 {
1817 1817 caddr_t lobound = *basep;
1818 1818 caddr_t hibound = lobound + *lenp;
1819 1819 struct seg *lseg, *hseg;
1820 1820 caddr_t lo, hi;
1821 1821 int forward;
1822 1822 caddr_t save_base;
1823 1823 size_t save_len;
1824 1824 size_t save_minlen;
1825 1825 size_t save_redzone;
1826 1826 int fast_path = 1;
1827 1827
1828 1828 save_base = *basep;
1829 1829 save_len = *lenp;
1830 1830 save_minlen = minlen;
1831 1831 save_redzone = redzone;
1832 1832
1833 1833 /*
1834 1834 * For the first pass/fast_path, just add align and redzone into
1835 1835 * minlen since if we get an allocation, we can guarantee that it
1836 1836 * will fit the alignment and redzone requested.
1837 1837 * This increases the chance that hibound will be adjusted to
1838 1838 * a_lastgap->s_base which will likely allow us to find an
1839 1839 * acceptable hole in the address space quicker.
1840 1840 * If we can't find a hole with this fast_path, then we look for
1841 1841 * smaller holes in which the alignment and offset may allow
1842 1842 * the allocation to fit.
1843 1843 */
1844 1844 minlen += align;
1845 1845 minlen += 2 * redzone;
1846 1846 redzone = 0;
1847 1847
1848 1848 AS_LOCK_ENTER(as, RW_READER);
1849 1849 if (AS_SEGFIRST(as) == NULL) {
1850 1850 if (valid_va_range_aligned(basep, lenp, minlen, flags & AH_DIR,
1851 1851 align, redzone, off)) {
1852 1852 AS_LOCK_EXIT(as);
1853 1853 return (0);
1854 1854 } else {
1855 1855 AS_LOCK_EXIT(as);
1856 1856 *basep = save_base;
1857 1857 *lenp = save_len;
1858 1858 return (-1);
1859 1859 }
1860 1860 }
1861 1861
1862 1862 retry:
1863 1863 /*
1864 1864 * Set up to iterate over all the inter-segment holes in the given
1865 1865 * direction. lseg is NULL for the lowest-addressed hole and hseg is
1866 1866 * NULL for the highest-addressed hole. If moving backwards, we reset
1867 1867 * sseg to denote the highest-addressed segment.
1868 1868 */
1869 1869 forward = (flags & AH_DIR) == AH_LO;
1870 1870 if (forward) {
1871 1871 hseg = as_findseg(as, lobound, 1);
1872 1872 lseg = AS_SEGPREV(as, hseg);
1873 1873 } else {
1874 1874
1875 1875 /*
1876 1876 * If allocating at least as much as the last allocation,
1877 1877 * use a_lastgap's base as a better estimate of hibound.
1878 1878 */
1879 1879 if (as->a_lastgap &&
1880 1880 minlen >= as->a_lastgap->s_size &&
1881 1881 hibound >= as->a_lastgap->s_base)
1882 1882 hibound = as->a_lastgap->s_base;
1883 1883
1884 1884 hseg = as_findseg(as, hibound, 1);
1885 1885 if (hseg->s_base + hseg->s_size < hibound) {
1886 1886 lseg = hseg;
1887 1887 hseg = NULL;
1888 1888 } else {
1889 1889 lseg = AS_SEGPREV(as, hseg);
1890 1890 }
1891 1891 }
1892 1892
1893 1893 for (;;) {
1894 1894 /*
1895 1895 * Set lo and hi to the hole's boundaries. (We should really
1896 1896 * use MAXADDR in place of hibound in the expression below,
1897 1897 * but can't express it easily; using hibound in its place is
1898 1898 * harmless.)
1899 1899 */
1900 1900 lo = (lseg == NULL) ? 0 : lseg->s_base + lseg->s_size;
1901 1901 hi = (hseg == NULL) ? hibound : hseg->s_base;
1902 1902 /*
1903 1903 * If the iteration has moved past the interval from lobound
1904 1904 * to hibound it's pointless to continue.
1905 1905 */
1906 1906 if ((forward && lo > hibound) || (!forward && hi < lobound))
1907 1907 break;
1908 1908 else if (lo > hibound || hi < lobound)
1909 1909 goto cont;
1910 1910 /*
1911 1911 * Candidate hole lies at least partially within the allowable
1912 1912 * range. Restrict it to fall completely within that range,
1913 1913 * i.e., to [max(lo, lobound), min(hi, hibound)].
1914 1914 */
1915 1915 if (lo < lobound)
1916 1916 lo = lobound;
1917 1917 if (hi > hibound)
1918 1918 hi = hibound;
1919 1919 /*
1920 1920 * Verify that the candidate hole is big enough and meets
1921 1921 * hardware constraints. If the hole is too small, no need
1922 1922 * to do the further checks since they will fail.
1923 1923 */
1924 1924 *basep = lo;
1925 1925 *lenp = hi - lo;
1926 1926 if (*lenp >= minlen && valid_va_range_aligned(basep, lenp,
1927 1927 minlen, forward ? AH_LO : AH_HI, align, redzone, off) &&
1928 1928 ((flags & AH_CONTAIN) == 0 ||
1929 1929 (*basep <= addr && *basep + *lenp > addr))) {
1930 1930 if (!forward)
1931 1931 as->a_lastgap = hseg;
1932 1932 if (hseg != NULL)
1933 1933 as->a_lastgaphl = hseg;
1934 1934 else
1935 1935 as->a_lastgaphl = lseg;
1936 1936 AS_LOCK_EXIT(as);
1937 1937 return (0);
1938 1938 }
1939 1939 cont:
1940 1940 /*
1941 1941 * Move to the next hole.
1942 1942 */
1943 1943 if (forward) {
1944 1944 lseg = hseg;
1945 1945 if (lseg == NULL)
1946 1946 break;
1947 1947 hseg = AS_SEGNEXT(as, hseg);
1948 1948 } else {
1949 1949 hseg = lseg;
1950 1950 if (hseg == NULL)
1951 1951 break;
1952 1952 lseg = AS_SEGPREV(as, lseg);
1953 1953 }
1954 1954 }
1955 1955 if (fast_path && (align != 0 || save_redzone != 0)) {
1956 1956 fast_path = 0;
1957 1957 minlen = save_minlen;
1958 1958 redzone = save_redzone;
1959 1959 goto retry;
1960 1960 }
1961 1961 *basep = save_base;
1962 1962 *lenp = save_len;
1963 1963 AS_LOCK_EXIT(as);
1964 1964 return (-1);
1965 1965 }
1966 1966
1967 1967 /*
1968 1968 * Find a hole of at least size minlen within [*basep, *basep + *lenp).
1969 1969 *
1970 1970 * If flags specifies AH_HI, the hole will have the highest possible address
1971 1971 * in the range. We use the as->a_lastgap field to figure out where to
1972 1972 * start looking for a gap.
1973 1973 *
1974 1974 * Otherwise, the gap will have the lowest possible address.
1975 1975 *
1976 1976 * If flags specifies AH_CONTAIN, the hole will contain the address addr.
1977 1977 *
1978 1978 * If an adequate hole is found, base and len are set to reflect the part of
1979 1979 * the hole that is within range, and 0 is returned, otherwise,
1980 1980 * -1 is returned.
1981 1981 *
1982 1982 * NOTE: This routine is not correct when base+len overflows caddr_t.
1983 1983 */
1984 1984 int
1985 1985 as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, uint_t flags,
1986 1986 caddr_t addr)
1987 1987 {
1988 1988
1989 1989 return (as_gap_aligned(as, minlen, basep, lenp, flags, addr, 0, 0, 0));
1990 1990 }
1991 1991
1992 1992 /*
1993 1993 * Return the next range within [base, base + len) that is backed
1994 1994 * with "real memory". Skip holes and non-seg_vn segments.
1995 1995 * We're lazy and only return one segment at a time.
1996 1996 */
1997 1997 int
1998 1998 as_memory(struct as *as, caddr_t *basep, size_t *lenp)
1999 1999 {
2000 2000 extern struct seg_ops segspt_shmops; /* needs a header file */
2001 2001 struct seg *seg;
2002 2002 caddr_t addr, eaddr;
2003 2003 caddr_t segend;
2004 2004
2005 2005 AS_LOCK_ENTER(as, RW_READER);
2006 2006
2007 2007 addr = *basep;
2008 2008 eaddr = addr + *lenp;
2009 2009
2010 2010 seg = as_findseg(as, addr, 0);
2011 2011 if (seg != NULL)
2012 2012 addr = MAX(seg->s_base, addr);
2013 2013
2014 2014 for (;;) {
2015 2015 if (seg == NULL || addr >= eaddr || eaddr <= seg->s_base) {
2016 2016 AS_LOCK_EXIT(as);
2017 2017 return (EINVAL);
2018 2018 }
2019 2019
2020 2020 if (seg->s_ops == &segvn_ops) {
2021 2021 segend = seg->s_base + seg->s_size;
2022 2022 break;
2023 2023 }
2024 2024
2025 2025 /*
2026 2026 * We do ISM by looking into the private data
2027 2027 * to determine the real size of the segment.
2028 2028 */
2029 2029 if (seg->s_ops == &segspt_shmops) {
2030 2030 segend = seg->s_base + spt_realsize(seg);
2031 2031 if (addr < segend)
2032 2032 break;
2033 2033 }
2034 2034
2035 2035 seg = AS_SEGNEXT(as, seg);
2036 2036
2037 2037 if (seg != NULL)
2038 2038 addr = seg->s_base;
2039 2039 }
2040 2040
2041 2041 *basep = addr;
2042 2042
↓ open down ↓ |
2042 lines elided |
↑ open up ↑ |
2043 2043 if (segend > eaddr)
2044 2044 *lenp = eaddr - addr;
2045 2045 else
2046 2046 *lenp = segend - addr;
2047 2047
2048 2048 AS_LOCK_EXIT(as);
2049 2049 return (0);
2050 2050 }
2051 2051
2052 2052 /*
2053 - * Swap the pages associated with the address space as out to
2054 - * secondary storage, returning the number of bytes actually
2055 - * swapped.
2056 - *
2057 - * The value returned is intended to correlate well with the process's
2058 - * memory requirements. Its usefulness for this purpose depends on
2059 - * how well the segment-level routines do at returning accurate
2060 - * information.
2061 - */
2062 -size_t
2063 -as_swapout(struct as *as)
2064 -{
2065 - struct seg *seg;
2066 - size_t swpcnt = 0;
2067 -
2068 - /*
2069 - * Kernel-only processes have given up their address
2070 - * spaces. Of course, we shouldn't be attempting to
2071 - * swap out such processes in the first place...
2072 - */
2073 - if (as == NULL)
2074 - return (0);
2075 -
2076 - AS_LOCK_ENTER(as, RW_READER);
2077 -
2078 - /*
2079 - * Free all mapping resources associated with the address
2080 - * space. The segment-level swapout routines capitalize
2081 - * on this unmapping by scavanging pages that have become
2082 - * unmapped here.
2083 - */
2084 - hat_swapout(as->a_hat);
2085 -
2086 - /*
2087 - * Call the swapout routines of all segments in the address
2088 - * space to do the actual work, accumulating the amount of
2089 - * space reclaimed.
2090 - */
2091 - for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
2092 - struct seg_ops *ov = seg->s_ops;
2093 -
2094 - /*
2095 - * We have to check to see if the seg has
2096 - * an ops vector because the seg may have
2097 - * been in the middle of being set up when
2098 - * the process was picked for swapout.
2099 - */
2100 - if ((ov != NULL) && (ov->swapout != NULL))
2101 - swpcnt += SEGOP_SWAPOUT(seg);
2102 - }
2103 - AS_LOCK_EXIT(as);
2104 - return (swpcnt);
2105 -}
2106 -
2107 -/*
2108 2053 * Determine whether data from the mappings in interval [addr, addr + size)
2109 2054 * are in the primary memory (core) cache.
2110 2055 */
2111 2056 int
2112 2057 as_incore(struct as *as, caddr_t addr,
2113 2058 size_t size, char *vec, size_t *sizep)
2114 2059 {
2115 2060 struct seg *seg;
2116 2061 size_t ssize;
2117 2062 caddr_t raddr; /* rounded down addr */
2118 2063 size_t rsize; /* rounded up size */
2119 2064 size_t isize; /* iteration size */
2120 2065 int error = 0; /* result, assume success */
2121 2066
2122 2067 *sizep = 0;
2123 2068 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
2124 2069 rsize = ((((size_t)addr + size) + PAGEOFFSET) & PAGEMASK) -
2125 2070 (size_t)raddr;
2126 2071
2127 2072 if (raddr + rsize < raddr) /* check for wraparound */
2128 2073 return (ENOMEM);
2129 2074
2130 2075 AS_LOCK_ENTER(as, RW_READER);
2131 2076 seg = as_segat(as, raddr);
2132 2077 if (seg == NULL) {
2133 2078 AS_LOCK_EXIT(as);
2134 2079 return (-1);
2135 2080 }
2136 2081
2137 2082 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
2138 2083 if (raddr >= seg->s_base + seg->s_size) {
2139 2084 seg = AS_SEGNEXT(as, seg);
2140 2085 if (seg == NULL || raddr != seg->s_base) {
2141 2086 error = -1;
2142 2087 break;
2143 2088 }
2144 2089 }
2145 2090 if ((raddr + rsize) > (seg->s_base + seg->s_size))
2146 2091 ssize = seg->s_base + seg->s_size - raddr;
2147 2092 else
2148 2093 ssize = rsize;
2149 2094 *sizep += isize = SEGOP_INCORE(seg, raddr, ssize, vec);
2150 2095 if (isize != ssize) {
2151 2096 error = -1;
2152 2097 break;
2153 2098 }
2154 2099 vec += btopr(ssize);
2155 2100 }
2156 2101 AS_LOCK_EXIT(as);
2157 2102 return (error);
2158 2103 }
2159 2104
2160 2105 static void
2161 2106 as_segunlock(struct seg *seg, caddr_t addr, int attr,
2162 2107 ulong_t *bitmap, size_t position, size_t npages)
2163 2108 {
2164 2109 caddr_t range_start;
2165 2110 size_t pos1 = position;
2166 2111 size_t pos2;
2167 2112 size_t size;
2168 2113 size_t end_pos = npages + position;
2169 2114
2170 2115 while (bt_range(bitmap, &pos1, &pos2, end_pos)) {
2171 2116 size = ptob((pos2 - pos1));
2172 2117 range_start = (caddr_t)((uintptr_t)addr +
2173 2118 ptob(pos1 - position));
2174 2119
2175 2120 (void) SEGOP_LOCKOP(seg, range_start, size, attr, MC_UNLOCK,
2176 2121 (ulong_t *)NULL, (size_t)NULL);
2177 2122 pos1 = pos2;
2178 2123 }
2179 2124 }
2180 2125
2181 2126 static void
2182 2127 as_unlockerr(struct as *as, int attr, ulong_t *mlock_map,
2183 2128 caddr_t raddr, size_t rsize)
2184 2129 {
2185 2130 struct seg *seg = as_segat(as, raddr);
2186 2131 size_t ssize;
2187 2132
2188 2133 while (rsize != 0) {
2189 2134 if (raddr >= seg->s_base + seg->s_size)
2190 2135 seg = AS_SEGNEXT(as, seg);
2191 2136
2192 2137 if ((raddr + rsize) > (seg->s_base + seg->s_size))
2193 2138 ssize = seg->s_base + seg->s_size - raddr;
2194 2139 else
2195 2140 ssize = rsize;
2196 2141
2197 2142 as_segunlock(seg, raddr, attr, mlock_map, 0, btopr(ssize));
2198 2143
2199 2144 rsize -= ssize;
2200 2145 raddr += ssize;
2201 2146 }
2202 2147 }
2203 2148
2204 2149 /*
2205 2150 * Cache control operations over the interval [addr, addr + size) in
2206 2151 * address space "as".
2207 2152 */
2208 2153 /*ARGSUSED*/
2209 2154 int
2210 2155 as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr,
2211 2156 uintptr_t arg, ulong_t *lock_map, size_t pos)
2212 2157 {
2213 2158 struct seg *seg; /* working segment */
2214 2159 caddr_t raddr; /* rounded down addr */
2215 2160 caddr_t initraddr; /* saved initial rounded down addr */
2216 2161 size_t rsize; /* rounded up size */
2217 2162 size_t initrsize; /* saved initial rounded up size */
2218 2163 size_t ssize; /* size of seg */
2219 2164 int error = 0; /* result */
2220 2165 size_t mlock_size; /* size of bitmap */
2221 2166 ulong_t *mlock_map; /* pointer to bitmap used */
2222 2167 /* to represent the locked */
2223 2168 /* pages. */
2224 2169 retry:
2225 2170 if (error == IE_RETRY)
2226 2171 AS_LOCK_ENTER(as, RW_WRITER);
2227 2172 else
2228 2173 AS_LOCK_ENTER(as, RW_READER);
2229 2174
2230 2175 /*
2231 2176 * If these are address space lock/unlock operations, loop over
2232 2177 * all segments in the address space, as appropriate.
2233 2178 */
2234 2179 if (func == MC_LOCKAS) {
2235 2180 size_t npages, idx;
2236 2181 size_t rlen = 0; /* rounded as length */
2237 2182
2238 2183 idx = pos;
2239 2184
2240 2185 if (arg & MCL_FUTURE) {
2241 2186 mutex_enter(&as->a_contents);
2242 2187 AS_SETPGLCK(as);
2243 2188 mutex_exit(&as->a_contents);
2244 2189 }
2245 2190 if ((arg & MCL_CURRENT) == 0) {
2246 2191 AS_LOCK_EXIT(as);
2247 2192 return (0);
2248 2193 }
2249 2194
2250 2195 seg = AS_SEGFIRST(as);
2251 2196 if (seg == NULL) {
2252 2197 AS_LOCK_EXIT(as);
2253 2198 return (0);
2254 2199 }
2255 2200
2256 2201 do {
2257 2202 raddr = (caddr_t)((uintptr_t)seg->s_base &
2258 2203 (uintptr_t)PAGEMASK);
2259 2204 rlen += (((uintptr_t)(seg->s_base + seg->s_size) +
2260 2205 PAGEOFFSET) & PAGEMASK) - (uintptr_t)raddr;
2261 2206 } while ((seg = AS_SEGNEXT(as, seg)) != NULL);
2262 2207
2263 2208 mlock_size = BT_BITOUL(btopr(rlen));
2264 2209 if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
2265 2210 sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
2266 2211 AS_LOCK_EXIT(as);
2267 2212 return (EAGAIN);
2268 2213 }
2269 2214
2270 2215 for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
2271 2216 error = SEGOP_LOCKOP(seg, seg->s_base,
2272 2217 seg->s_size, attr, MC_LOCK, mlock_map, pos);
2273 2218 if (error != 0)
2274 2219 break;
2275 2220 pos += seg_pages(seg);
2276 2221 }
2277 2222
2278 2223 if (error) {
2279 2224 for (seg = AS_SEGFIRST(as); seg != NULL;
2280 2225 seg = AS_SEGNEXT(as, seg)) {
2281 2226
2282 2227 raddr = (caddr_t)((uintptr_t)seg->s_base &
2283 2228 (uintptr_t)PAGEMASK);
2284 2229 npages = seg_pages(seg);
2285 2230 as_segunlock(seg, raddr, attr, mlock_map,
2286 2231 idx, npages);
2287 2232 idx += npages;
2288 2233 }
2289 2234 }
2290 2235
2291 2236 kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
2292 2237 AS_LOCK_EXIT(as);
2293 2238 goto lockerr;
2294 2239 } else if (func == MC_UNLOCKAS) {
2295 2240 mutex_enter(&as->a_contents);
2296 2241 AS_CLRPGLCK(as);
2297 2242 mutex_exit(&as->a_contents);
2298 2243
2299 2244 for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
2300 2245 error = SEGOP_LOCKOP(seg, seg->s_base,
2301 2246 seg->s_size, attr, MC_UNLOCK, NULL, 0);
2302 2247 if (error != 0)
2303 2248 break;
2304 2249 }
2305 2250
2306 2251 AS_LOCK_EXIT(as);
2307 2252 goto lockerr;
2308 2253 }
2309 2254
2310 2255 /*
2311 2256 * Normalize addresses and sizes.
2312 2257 */
2313 2258 initraddr = raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
2314 2259 initrsize = rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
2315 2260 (size_t)raddr;
2316 2261
2317 2262 if (raddr + rsize < raddr) { /* check for wraparound */
2318 2263 AS_LOCK_EXIT(as);
2319 2264 return (ENOMEM);
2320 2265 }
2321 2266
2322 2267 /*
2323 2268 * Get initial segment.
2324 2269 */
2325 2270 if ((seg = as_segat(as, raddr)) == NULL) {
2326 2271 AS_LOCK_EXIT(as);
2327 2272 return (ENOMEM);
2328 2273 }
2329 2274
2330 2275 if (func == MC_LOCK) {
2331 2276 mlock_size = BT_BITOUL(btopr(rsize));
2332 2277 if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
2333 2278 sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
2334 2279 AS_LOCK_EXIT(as);
2335 2280 return (EAGAIN);
2336 2281 }
2337 2282 }
2338 2283
2339 2284 /*
2340 2285 * Loop over all segments. If a hole in the address range is
2341 2286 * discovered, then fail. For each segment, perform the appropriate
2342 2287 * control operation.
2343 2288 */
2344 2289 while (rsize != 0) {
2345 2290
2346 2291 /*
2347 2292 * Make sure there's no hole, calculate the portion
2348 2293 * of the next segment to be operated over.
2349 2294 */
2350 2295 if (raddr >= seg->s_base + seg->s_size) {
2351 2296 seg = AS_SEGNEXT(as, seg);
2352 2297 if (seg == NULL || raddr != seg->s_base) {
2353 2298 if (func == MC_LOCK) {
2354 2299 as_unlockerr(as, attr, mlock_map,
2355 2300 initraddr, initrsize - rsize);
2356 2301 kmem_free(mlock_map,
2357 2302 mlock_size * sizeof (ulong_t));
2358 2303 }
2359 2304 AS_LOCK_EXIT(as);
2360 2305 return (ENOMEM);
2361 2306 }
2362 2307 }
2363 2308 if ((raddr + rsize) > (seg->s_base + seg->s_size))
2364 2309 ssize = seg->s_base + seg->s_size - raddr;
2365 2310 else
2366 2311 ssize = rsize;
2367 2312
2368 2313 /*
2369 2314 * Dispatch on specific function.
2370 2315 */
2371 2316 switch (func) {
2372 2317
2373 2318 /*
2374 2319 * Synchronize cached data from mappings with backing
2375 2320 * objects.
2376 2321 */
2377 2322 case MC_SYNC:
2378 2323 if (error = SEGOP_SYNC(seg, raddr, ssize,
2379 2324 attr, (uint_t)arg)) {
2380 2325 AS_LOCK_EXIT(as);
2381 2326 return (error);
2382 2327 }
2383 2328 break;
2384 2329
2385 2330 /*
2386 2331 * Lock pages in memory.
2387 2332 */
2388 2333 case MC_LOCK:
2389 2334 if (error = SEGOP_LOCKOP(seg, raddr, ssize,
2390 2335 attr, func, mlock_map, pos)) {
2391 2336 as_unlockerr(as, attr, mlock_map, initraddr,
2392 2337 initrsize - rsize + ssize);
2393 2338 kmem_free(mlock_map, mlock_size *
2394 2339 sizeof (ulong_t));
2395 2340 AS_LOCK_EXIT(as);
2396 2341 goto lockerr;
2397 2342 }
2398 2343 break;
2399 2344
2400 2345 /*
2401 2346 * Unlock mapped pages.
2402 2347 */
2403 2348 case MC_UNLOCK:
2404 2349 (void) SEGOP_LOCKOP(seg, raddr, ssize, attr, func,
2405 2350 (ulong_t *)NULL, (size_t)NULL);
2406 2351 break;
2407 2352
2408 2353 /*
2409 2354 * Store VM advise for mapped pages in segment layer.
2410 2355 */
2411 2356 case MC_ADVISE:
2412 2357 error = SEGOP_ADVISE(seg, raddr, ssize, (uint_t)arg);
2413 2358
2414 2359 /*
2415 2360 * Check for regular errors and special retry error
2416 2361 */
2417 2362 if (error) {
2418 2363 if (error == IE_RETRY) {
2419 2364 /*
2420 2365 * Need to acquire writers lock, so
2421 2366 * have to drop readers lock and start
2422 2367 * all over again
2423 2368 */
2424 2369 AS_LOCK_EXIT(as);
2425 2370 goto retry;
2426 2371 } else if (error == IE_REATTACH) {
2427 2372 /*
2428 2373 * Find segment for current address
2429 2374 * because current segment just got
2430 2375 * split or concatenated
2431 2376 */
2432 2377 seg = as_segat(as, raddr);
2433 2378 if (seg == NULL) {
2434 2379 AS_LOCK_EXIT(as);
2435 2380 return (ENOMEM);
2436 2381 }
2437 2382 } else {
2438 2383 /*
2439 2384 * Regular error
2440 2385 */
2441 2386 AS_LOCK_EXIT(as);
2442 2387 return (error);
2443 2388 }
2444 2389 }
2445 2390 break;
2446 2391
2447 2392 case MC_INHERIT_ZERO:
2448 2393 if (seg->s_ops->inherit == NULL) {
2449 2394 error = ENOTSUP;
2450 2395 } else {
2451 2396 error = SEGOP_INHERIT(seg, raddr, ssize,
2452 2397 SEGP_INH_ZERO);
2453 2398 }
2454 2399 if (error != 0) {
2455 2400 AS_LOCK_EXIT(as);
2456 2401 return (error);
2457 2402 }
2458 2403 break;
2459 2404
2460 2405 /*
2461 2406 * Can't happen.
2462 2407 */
2463 2408 default:
2464 2409 panic("as_ctl: bad operation %d", func);
2465 2410 /*NOTREACHED*/
2466 2411 }
2467 2412
2468 2413 rsize -= ssize;
2469 2414 raddr += ssize;
2470 2415 }
2471 2416
2472 2417 if (func == MC_LOCK)
2473 2418 kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
2474 2419 AS_LOCK_EXIT(as);
2475 2420 return (0);
2476 2421 lockerr:
2477 2422
2478 2423 /*
2479 2424 * If the lower levels returned EDEADLK for a segment lockop,
2480 2425 * it means that we should retry the operation. Let's wait
2481 2426 * a bit also to let the deadlock causing condition clear.
2482 2427 * This is part of a gross hack to work around a design flaw
2483 2428 * in the ufs/sds logging code and should go away when the
2484 2429 * logging code is re-designed to fix the problem. See bug
2485 2430 * 4125102 for details of the problem.
2486 2431 */
2487 2432 if (error == EDEADLK) {
2488 2433 delay(deadlk_wait);
2489 2434 error = 0;
2490 2435 goto retry;
2491 2436 }
2492 2437 return (error);
2493 2438 }
2494 2439
2495 2440 int
2496 2441 fc_decode(faultcode_t fault_err)
2497 2442 {
2498 2443 int error = 0;
2499 2444
2500 2445 switch (FC_CODE(fault_err)) {
2501 2446 case FC_OBJERR:
2502 2447 error = FC_ERRNO(fault_err);
2503 2448 break;
2504 2449 case FC_PROT:
2505 2450 error = EACCES;
2506 2451 break;
2507 2452 default:
2508 2453 error = EFAULT;
2509 2454 break;
2510 2455 }
2511 2456 return (error);
2512 2457 }
2513 2458
2514 2459 /*
2515 2460 * Pagelock pages from a range that spans more than 1 segment. Obtain shadow
2516 2461 * lists from each segment and copy them to one contiguous shadow list (plist)
2517 2462 * as expected by the caller. Save pointers to per segment shadow lists at
2518 2463 * the tail of plist so that they can be used during as_pageunlock().
2519 2464 */
2520 2465 static int
2521 2466 as_pagelock_segs(struct as *as, struct seg *seg, struct page ***ppp,
2522 2467 caddr_t addr, size_t size, enum seg_rw rw)
2523 2468 {
2524 2469 caddr_t sv_addr = addr;
2525 2470 size_t sv_size = size;
2526 2471 struct seg *sv_seg = seg;
2527 2472 ulong_t segcnt = 1;
2528 2473 ulong_t cnt;
2529 2474 size_t ssize;
2530 2475 pgcnt_t npages = btop(size);
2531 2476 page_t **plist;
2532 2477 page_t **pl;
2533 2478 int error;
2534 2479 caddr_t eaddr;
2535 2480 faultcode_t fault_err = 0;
2536 2481 pgcnt_t pl_off;
2537 2482 extern struct seg_ops segspt_shmops;
2538 2483
2539 2484 ASSERT(AS_LOCK_HELD(as));
2540 2485 ASSERT(seg != NULL);
2541 2486 ASSERT(addr >= seg->s_base && addr < seg->s_base + seg->s_size);
2542 2487 ASSERT(addr + size > seg->s_base + seg->s_size);
2543 2488 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
2544 2489 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
2545 2490
2546 2491 /*
2547 2492 * Count the number of segments covered by the range we are about to
2548 2493 * lock. The segment count is used to size the shadow list we return
2549 2494 * back to the caller.
2550 2495 */
2551 2496 for (; size != 0; size -= ssize, addr += ssize) {
2552 2497 if (addr >= seg->s_base + seg->s_size) {
2553 2498
2554 2499 seg = AS_SEGNEXT(as, seg);
2555 2500 if (seg == NULL || addr != seg->s_base) {
2556 2501 AS_LOCK_EXIT(as);
2557 2502 return (EFAULT);
2558 2503 }
2559 2504 /*
2560 2505 * Do a quick check if subsequent segments
2561 2506 * will most likely support pagelock.
2562 2507 */
2563 2508 if (seg->s_ops == &segvn_ops) {
2564 2509 vnode_t *vp;
2565 2510
2566 2511 if (SEGOP_GETVP(seg, addr, &vp) != 0 ||
2567 2512 vp != NULL) {
2568 2513 AS_LOCK_EXIT(as);
2569 2514 goto slow;
2570 2515 }
2571 2516 } else if (seg->s_ops != &segspt_shmops) {
2572 2517 AS_LOCK_EXIT(as);
2573 2518 goto slow;
2574 2519 }
2575 2520 segcnt++;
2576 2521 }
2577 2522 if (addr + size > seg->s_base + seg->s_size) {
2578 2523 ssize = seg->s_base + seg->s_size - addr;
2579 2524 } else {
2580 2525 ssize = size;
2581 2526 }
2582 2527 }
2583 2528 ASSERT(segcnt > 1);
2584 2529
2585 2530 plist = kmem_zalloc((npages + segcnt) * sizeof (page_t *), KM_SLEEP);
2586 2531
2587 2532 addr = sv_addr;
2588 2533 size = sv_size;
2589 2534 seg = sv_seg;
2590 2535
2591 2536 for (cnt = 0, pl_off = 0; size != 0; size -= ssize, addr += ssize) {
2592 2537 if (addr >= seg->s_base + seg->s_size) {
2593 2538 seg = AS_SEGNEXT(as, seg);
2594 2539 ASSERT(seg != NULL && addr == seg->s_base);
2595 2540 cnt++;
2596 2541 ASSERT(cnt < segcnt);
2597 2542 }
2598 2543 if (addr + size > seg->s_base + seg->s_size) {
2599 2544 ssize = seg->s_base + seg->s_size - addr;
2600 2545 } else {
2601 2546 ssize = size;
2602 2547 }
2603 2548 pl = &plist[npages + cnt];
2604 2549 error = SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
2605 2550 L_PAGELOCK, rw);
2606 2551 if (error) {
2607 2552 break;
2608 2553 }
2609 2554 ASSERT(plist[npages + cnt] != NULL);
2610 2555 ASSERT(pl_off + btop(ssize) <= npages);
2611 2556 bcopy(plist[npages + cnt], &plist[pl_off],
2612 2557 btop(ssize) * sizeof (page_t *));
2613 2558 pl_off += btop(ssize);
2614 2559 }
2615 2560
2616 2561 if (size == 0) {
2617 2562 AS_LOCK_EXIT(as);
2618 2563 ASSERT(cnt == segcnt - 1);
2619 2564 *ppp = plist;
2620 2565 return (0);
2621 2566 }
2622 2567
2623 2568 /*
2624 2569 * one of pagelock calls failed. The error type is in error variable.
2625 2570 * Unlock what we've locked so far and retry with F_SOFTLOCK if error
2626 2571 * type is either EFAULT or ENOTSUP. Otherwise just return the error
2627 2572 * back to the caller.
2628 2573 */
2629 2574
2630 2575 eaddr = addr;
2631 2576 seg = sv_seg;
2632 2577
2633 2578 for (cnt = 0, addr = sv_addr; addr < eaddr; addr += ssize) {
2634 2579 if (addr >= seg->s_base + seg->s_size) {
2635 2580 seg = AS_SEGNEXT(as, seg);
2636 2581 ASSERT(seg != NULL && addr == seg->s_base);
2637 2582 cnt++;
2638 2583 ASSERT(cnt < segcnt);
2639 2584 }
2640 2585 if (eaddr > seg->s_base + seg->s_size) {
2641 2586 ssize = seg->s_base + seg->s_size - addr;
2642 2587 } else {
2643 2588 ssize = eaddr - addr;
2644 2589 }
2645 2590 pl = &plist[npages + cnt];
2646 2591 ASSERT(*pl != NULL);
2647 2592 (void) SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
2648 2593 L_PAGEUNLOCK, rw);
2649 2594 }
2650 2595
2651 2596 AS_LOCK_EXIT(as);
2652 2597
2653 2598 kmem_free(plist, (npages + segcnt) * sizeof (page_t *));
2654 2599
2655 2600 if (error != ENOTSUP && error != EFAULT) {
2656 2601 return (error);
2657 2602 }
2658 2603
2659 2604 slow:
2660 2605 /*
2661 2606 * If we are here because pagelock failed due to the need to cow fault
2662 2607 * in the pages we want to lock F_SOFTLOCK will do this job and in
2663 2608 * next as_pagelock() call for this address range pagelock will
2664 2609 * hopefully succeed.
2665 2610 */
2666 2611 fault_err = as_fault(as->a_hat, as, sv_addr, sv_size, F_SOFTLOCK, rw);
2667 2612 if (fault_err != 0) {
2668 2613 return (fc_decode(fault_err));
2669 2614 }
2670 2615 *ppp = NULL;
2671 2616
2672 2617 return (0);
2673 2618 }
2674 2619
2675 2620 /*
2676 2621 * lock pages in a given address space. Return shadow list. If
2677 2622 * the list is NULL, the MMU mapping is also locked.
2678 2623 */
2679 2624 int
2680 2625 as_pagelock(struct as *as, struct page ***ppp, caddr_t addr,
2681 2626 size_t size, enum seg_rw rw)
2682 2627 {
2683 2628 size_t rsize;
2684 2629 caddr_t raddr;
2685 2630 faultcode_t fault_err;
2686 2631 struct seg *seg;
2687 2632 int err;
2688 2633
2689 2634 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_START,
2690 2635 "as_pagelock_start: addr %p size %ld", addr, size);
2691 2636
2692 2637 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
2693 2638 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
2694 2639 (size_t)raddr;
2695 2640
2696 2641 /*
2697 2642 * if the request crosses two segments let
2698 2643 * as_fault handle it.
2699 2644 */
2700 2645 AS_LOCK_ENTER(as, RW_READER);
2701 2646
2702 2647 seg = as_segat(as, raddr);
2703 2648 if (seg == NULL) {
2704 2649 AS_LOCK_EXIT(as);
2705 2650 return (EFAULT);
2706 2651 }
2707 2652 ASSERT(raddr >= seg->s_base && raddr < seg->s_base + seg->s_size);
2708 2653 if (raddr + rsize > seg->s_base + seg->s_size) {
2709 2654 return (as_pagelock_segs(as, seg, ppp, raddr, rsize, rw));
2710 2655 }
2711 2656 if (raddr + rsize <= raddr) {
2712 2657 AS_LOCK_EXIT(as);
2713 2658 return (EFAULT);
2714 2659 }
2715 2660
2716 2661 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_START,
2717 2662 "seg_lock_1_start: raddr %p rsize %ld", raddr, rsize);
2718 2663
2719 2664 /*
2720 2665 * try to lock pages and pass back shadow list
2721 2666 */
2722 2667 err = SEGOP_PAGELOCK(seg, raddr, rsize, ppp, L_PAGELOCK, rw);
2723 2668
2724 2669 TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_END, "seg_lock_1_end");
2725 2670
2726 2671 AS_LOCK_EXIT(as);
2727 2672
2728 2673 if (err == 0 || (err != ENOTSUP && err != EFAULT)) {
2729 2674 return (err);
2730 2675 }
2731 2676
2732 2677 /*
2733 2678 * Use F_SOFTLOCK to lock the pages because pagelock failed either due
2734 2679 * to no pagelock support for this segment or pages need to be cow
2735 2680 * faulted in. If fault is needed F_SOFTLOCK will do this job for
2736 2681 * this as_pagelock() call and in the next as_pagelock() call for the
2737 2682 * same address range pagelock call will hopefull succeed.
2738 2683 */
2739 2684 fault_err = as_fault(as->a_hat, as, addr, size, F_SOFTLOCK, rw);
2740 2685 if (fault_err != 0) {
2741 2686 return (fc_decode(fault_err));
2742 2687 }
2743 2688 *ppp = NULL;
2744 2689
2745 2690 TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_END, "as_pagelock_end");
2746 2691 return (0);
2747 2692 }
2748 2693
2749 2694 /*
2750 2695 * unlock pages locked by as_pagelock_segs(). Retrieve per segment shadow
2751 2696 * lists from the end of plist and call pageunlock interface for each segment.
2752 2697 * Drop as lock and free plist.
2753 2698 */
2754 2699 static void
2755 2700 as_pageunlock_segs(struct as *as, struct seg *seg, caddr_t addr, size_t size,
2756 2701 struct page **plist, enum seg_rw rw)
2757 2702 {
2758 2703 ulong_t cnt;
2759 2704 caddr_t eaddr = addr + size;
2760 2705 pgcnt_t npages = btop(size);
2761 2706 size_t ssize;
2762 2707 page_t **pl;
2763 2708
2764 2709 ASSERT(AS_LOCK_HELD(as));
2765 2710 ASSERT(seg != NULL);
2766 2711 ASSERT(addr >= seg->s_base && addr < seg->s_base + seg->s_size);
2767 2712 ASSERT(addr + size > seg->s_base + seg->s_size);
2768 2713 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
2769 2714 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
2770 2715 ASSERT(plist != NULL);
2771 2716
2772 2717 for (cnt = 0; addr < eaddr; addr += ssize) {
2773 2718 if (addr >= seg->s_base + seg->s_size) {
2774 2719 seg = AS_SEGNEXT(as, seg);
2775 2720 ASSERT(seg != NULL && addr == seg->s_base);
2776 2721 cnt++;
2777 2722 }
2778 2723 if (eaddr > seg->s_base + seg->s_size) {
2779 2724 ssize = seg->s_base + seg->s_size - addr;
2780 2725 } else {
2781 2726 ssize = eaddr - addr;
2782 2727 }
2783 2728 pl = &plist[npages + cnt];
2784 2729 ASSERT(*pl != NULL);
2785 2730 (void) SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
2786 2731 L_PAGEUNLOCK, rw);
2787 2732 }
2788 2733 ASSERT(cnt > 0);
2789 2734 AS_LOCK_EXIT(as);
2790 2735
2791 2736 cnt++;
2792 2737 kmem_free(plist, (npages + cnt) * sizeof (page_t *));
2793 2738 }
2794 2739
2795 2740 /*
2796 2741 * unlock pages in a given address range
2797 2742 */
2798 2743 void
2799 2744 as_pageunlock(struct as *as, struct page **pp, caddr_t addr, size_t size,
2800 2745 enum seg_rw rw)
2801 2746 {
2802 2747 struct seg *seg;
2803 2748 size_t rsize;
2804 2749 caddr_t raddr;
2805 2750
2806 2751 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_START,
2807 2752 "as_pageunlock_start: addr %p size %ld", addr, size);
2808 2753
2809 2754 /*
2810 2755 * if the shadow list is NULL, as_pagelock was
2811 2756 * falling back to as_fault
2812 2757 */
2813 2758 if (pp == NULL) {
2814 2759 (void) as_fault(as->a_hat, as, addr, size, F_SOFTUNLOCK, rw);
2815 2760 return;
2816 2761 }
2817 2762
2818 2763 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
2819 2764 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
2820 2765 (size_t)raddr;
2821 2766
2822 2767 AS_LOCK_ENTER(as, RW_READER);
2823 2768 seg = as_segat(as, raddr);
2824 2769 ASSERT(seg != NULL);
2825 2770
2826 2771 TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_UNLOCK_START,
2827 2772 "seg_unlock_start: raddr %p rsize %ld", raddr, rsize);
2828 2773
2829 2774 ASSERT(raddr >= seg->s_base && raddr < seg->s_base + seg->s_size);
2830 2775 if (raddr + rsize <= seg->s_base + seg->s_size) {
2831 2776 SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGEUNLOCK, rw);
2832 2777 } else {
2833 2778 as_pageunlock_segs(as, seg, raddr, rsize, pp, rw);
2834 2779 return;
2835 2780 }
2836 2781 AS_LOCK_EXIT(as);
2837 2782 TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_END, "as_pageunlock_end");
2838 2783 }
2839 2784
2840 2785 int
2841 2786 as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc,
2842 2787 boolean_t wait)
2843 2788 {
2844 2789 struct seg *seg;
2845 2790 size_t ssize;
2846 2791 caddr_t raddr; /* rounded down addr */
2847 2792 size_t rsize; /* rounded up size */
2848 2793 int error = 0;
2849 2794 size_t pgsz = page_get_pagesize(szc);
2850 2795
2851 2796 setpgsz_top:
2852 2797 if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(size, pgsz)) {
2853 2798 return (EINVAL);
2854 2799 }
2855 2800
2856 2801 raddr = addr;
2857 2802 rsize = size;
2858 2803
2859 2804 if (raddr + rsize < raddr) /* check for wraparound */
2860 2805 return (ENOMEM);
2861 2806
2862 2807 AS_LOCK_ENTER(as, RW_WRITER);
2863 2808 as_clearwatchprot(as, raddr, rsize);
2864 2809 seg = as_segat(as, raddr);
2865 2810 if (seg == NULL) {
2866 2811 as_setwatch(as);
2867 2812 AS_LOCK_EXIT(as);
2868 2813 return (ENOMEM);
2869 2814 }
2870 2815
2871 2816 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
2872 2817 if (raddr >= seg->s_base + seg->s_size) {
2873 2818 seg = AS_SEGNEXT(as, seg);
2874 2819 if (seg == NULL || raddr != seg->s_base) {
2875 2820 error = ENOMEM;
2876 2821 break;
2877 2822 }
2878 2823 }
2879 2824 if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
2880 2825 ssize = seg->s_base + seg->s_size - raddr;
2881 2826 } else {
2882 2827 ssize = rsize;
2883 2828 }
2884 2829
2885 2830 retry:
2886 2831 error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
2887 2832
2888 2833 if (error == IE_NOMEM) {
2889 2834 error = EAGAIN;
2890 2835 break;
2891 2836 }
2892 2837
2893 2838 if (error == IE_RETRY) {
2894 2839 AS_LOCK_EXIT(as);
2895 2840 goto setpgsz_top;
2896 2841 }
2897 2842
2898 2843 if (error == ENOTSUP) {
2899 2844 error = EINVAL;
2900 2845 break;
2901 2846 }
2902 2847
2903 2848 if (wait && (error == EAGAIN)) {
2904 2849 /*
2905 2850 * Memory is currently locked. It must be unlocked
2906 2851 * before this operation can succeed through a retry.
2907 2852 * The possible reasons for locked memory and
2908 2853 * corresponding strategies for unlocking are:
2909 2854 * (1) Normal I/O
2910 2855 * wait for a signal that the I/O operation
2911 2856 * has completed and the memory is unlocked.
2912 2857 * (2) Asynchronous I/O
2913 2858 * The aio subsystem does not unlock pages when
2914 2859 * the I/O is completed. Those pages are unlocked
2915 2860 * when the application calls aiowait/aioerror.
2916 2861 * So, to prevent blocking forever, cv_broadcast()
2917 2862 * is done to wake up aio_cleanup_thread.
2918 2863 * Subsequently, segvn_reclaim will be called, and
2919 2864 * that will do AS_CLRUNMAPWAIT() and wake us up.
2920 2865 * (3) Long term page locking:
2921 2866 * This is not relevant for as_setpagesize()
2922 2867 * because we cannot change the page size for
2923 2868 * driver memory. The attempt to do so will
2924 2869 * fail with a different error than EAGAIN so
2925 2870 * there's no need to trigger as callbacks like
2926 2871 * as_unmap, as_setprot or as_free would do.
2927 2872 */
2928 2873 mutex_enter(&as->a_contents);
2929 2874 if (!AS_ISNOUNMAPWAIT(as)) {
2930 2875 if (AS_ISUNMAPWAIT(as) == 0) {
2931 2876 cv_broadcast(&as->a_cv);
2932 2877 }
2933 2878 AS_SETUNMAPWAIT(as);
2934 2879 AS_LOCK_EXIT(as);
2935 2880 while (AS_ISUNMAPWAIT(as)) {
2936 2881 cv_wait(&as->a_cv, &as->a_contents);
2937 2882 }
2938 2883 } else {
2939 2884 /*
2940 2885 * We may have raced with
2941 2886 * segvn_reclaim()/segspt_reclaim(). In this
2942 2887 * case clean nounmapwait flag and retry since
2943 2888 * softlockcnt in this segment may be already
2944 2889 * 0. We don't drop as writer lock so our
2945 2890 * number of retries without sleeping should
2946 2891 * be very small. See segvn_reclaim() for
2947 2892 * more comments.
2948 2893 */
2949 2894 AS_CLRNOUNMAPWAIT(as);
2950 2895 mutex_exit(&as->a_contents);
2951 2896 goto retry;
2952 2897 }
2953 2898 mutex_exit(&as->a_contents);
2954 2899 goto setpgsz_top;
2955 2900 } else if (error != 0) {
2956 2901 break;
2957 2902 }
2958 2903 }
2959 2904 as_setwatch(as);
2960 2905 AS_LOCK_EXIT(as);
2961 2906 return (error);
2962 2907 }
2963 2908
2964 2909 /*
2965 2910 * as_iset3_default_lpsize() just calls SEGOP_SETPAGESIZE() on all segments
2966 2911 * in its chunk where s_szc is less than the szc we want to set.
2967 2912 */
2968 2913 static int
2969 2914 as_iset3_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
2970 2915 int *retry)
2971 2916 {
2972 2917 struct seg *seg;
2973 2918 size_t ssize;
2974 2919 int error;
2975 2920
2976 2921 ASSERT(AS_WRITE_HELD(as));
2977 2922
2978 2923 seg = as_segat(as, raddr);
2979 2924 if (seg == NULL) {
2980 2925 panic("as_iset3_default_lpsize: no seg");
2981 2926 }
2982 2927
2983 2928 for (; rsize != 0; rsize -= ssize, raddr += ssize) {
2984 2929 if (raddr >= seg->s_base + seg->s_size) {
2985 2930 seg = AS_SEGNEXT(as, seg);
2986 2931 if (seg == NULL || raddr != seg->s_base) {
2987 2932 panic("as_iset3_default_lpsize: as changed");
2988 2933 }
2989 2934 }
2990 2935 if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
2991 2936 ssize = seg->s_base + seg->s_size - raddr;
2992 2937 } else {
2993 2938 ssize = rsize;
2994 2939 }
2995 2940
2996 2941 if (szc > seg->s_szc) {
2997 2942 error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
2998 2943 /* Only retry on EINVAL segments that have no vnode. */
2999 2944 if (error == EINVAL) {
3000 2945 vnode_t *vp = NULL;
3001 2946 if ((SEGOP_GETTYPE(seg, raddr) & MAP_SHARED) &&
3002 2947 (SEGOP_GETVP(seg, raddr, &vp) != 0 ||
3003 2948 vp == NULL)) {
3004 2949 *retry = 1;
3005 2950 } else {
3006 2951 *retry = 0;
3007 2952 }
3008 2953 }
3009 2954 if (error) {
3010 2955 return (error);
3011 2956 }
3012 2957 }
3013 2958 }
3014 2959 return (0);
3015 2960 }
3016 2961
3017 2962 /*
3018 2963 * as_iset2_default_lpsize() calls as_iset3_default_lpsize() to set the
3019 2964 * pagesize on each segment in its range, but if any fails with EINVAL,
3020 2965 * then it reduces the pagesizes to the next size in the bitmap and
3021 2966 * retries as_iset3_default_lpsize(). The reason why the code retries
3022 2967 * smaller allowed sizes on EINVAL is because (a) the anon offset may not
3023 2968 * match the bigger sizes, and (b) it's hard to get this offset (to begin
3024 2969 * with) to pass to map_pgszcvec().
3025 2970 */
3026 2971 static int
3027 2972 as_iset2_default_lpsize(struct as *as, caddr_t addr, size_t size, uint_t szc,
3028 2973 uint_t szcvec)
3029 2974 {
3030 2975 int error;
3031 2976 int retry;
3032 2977
3033 2978 ASSERT(AS_WRITE_HELD(as));
3034 2979
3035 2980 for (;;) {
3036 2981 error = as_iset3_default_lpsize(as, addr, size, szc, &retry);
3037 2982 if (error == EINVAL && retry) {
3038 2983 szcvec &= ~(1 << szc);
3039 2984 if (szcvec <= 1) {
3040 2985 return (EINVAL);
3041 2986 }
3042 2987 szc = highbit(szcvec) - 1;
3043 2988 } else {
3044 2989 return (error);
3045 2990 }
3046 2991 }
3047 2992 }
3048 2993
3049 2994 /*
3050 2995 * as_iset1_default_lpsize() breaks its chunk into areas where existing
3051 2996 * segments have a smaller szc than we want to set. For each such area,
3052 2997 * it calls as_iset2_default_lpsize()
3053 2998 */
3054 2999 static int
3055 3000 as_iset1_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
3056 3001 uint_t szcvec)
3057 3002 {
3058 3003 struct seg *seg;
3059 3004 size_t ssize;
3060 3005 caddr_t setaddr = raddr;
3061 3006 size_t setsize = 0;
3062 3007 int set;
3063 3008 int error;
3064 3009
3065 3010 ASSERT(AS_WRITE_HELD(as));
3066 3011
3067 3012 seg = as_segat(as, raddr);
3068 3013 if (seg == NULL) {
3069 3014 panic("as_iset1_default_lpsize: no seg");
3070 3015 }
3071 3016 if (seg->s_szc < szc) {
3072 3017 set = 1;
3073 3018 } else {
3074 3019 set = 0;
3075 3020 }
3076 3021
3077 3022 for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
3078 3023 if (raddr >= seg->s_base + seg->s_size) {
3079 3024 seg = AS_SEGNEXT(as, seg);
3080 3025 if (seg == NULL || raddr != seg->s_base) {
3081 3026 panic("as_iset1_default_lpsize: as changed");
3082 3027 }
3083 3028 if (seg->s_szc >= szc && set) {
3084 3029 ASSERT(setsize != 0);
3085 3030 error = as_iset2_default_lpsize(as,
3086 3031 setaddr, setsize, szc, szcvec);
3087 3032 if (error) {
3088 3033 return (error);
3089 3034 }
3090 3035 set = 0;
3091 3036 } else if (seg->s_szc < szc && !set) {
3092 3037 setaddr = raddr;
3093 3038 setsize = 0;
3094 3039 set = 1;
3095 3040 }
3096 3041 }
3097 3042 if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
3098 3043 ssize = seg->s_base + seg->s_size - raddr;
3099 3044 } else {
3100 3045 ssize = rsize;
3101 3046 }
3102 3047 }
3103 3048 error = 0;
3104 3049 if (set) {
3105 3050 ASSERT(setsize != 0);
3106 3051 error = as_iset2_default_lpsize(as, setaddr, setsize,
3107 3052 szc, szcvec);
3108 3053 }
3109 3054 return (error);
3110 3055 }
3111 3056
3112 3057 /*
3113 3058 * as_iset_default_lpsize() breaks its chunk according to the size code bitmap
3114 3059 * returned by map_pgszcvec() (similar to as_map_segvn_segs()), and passes each
3115 3060 * chunk to as_iset1_default_lpsize().
3116 3061 */
3117 3062 static int
3118 3063 as_iset_default_lpsize(struct as *as, caddr_t addr, size_t size, int flags,
3119 3064 int type)
3120 3065 {
3121 3066 int rtype = (type & MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
3122 3067 uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr,
3123 3068 flags, rtype, 1);
3124 3069 uint_t szc;
3125 3070 uint_t nszc;
3126 3071 int error;
3127 3072 caddr_t a;
3128 3073 caddr_t eaddr;
3129 3074 size_t segsize;
3130 3075 size_t pgsz;
3131 3076 uint_t save_szcvec;
3132 3077
3133 3078 ASSERT(AS_WRITE_HELD(as));
3134 3079 ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
3135 3080 ASSERT(IS_P2ALIGNED(size, PAGESIZE));
3136 3081
3137 3082 szcvec &= ~1;
3138 3083 if (szcvec <= 1) { /* skip if base page size */
3139 3084 return (0);
3140 3085 }
3141 3086
3142 3087 /* Get the pagesize of the first larger page size. */
3143 3088 szc = lowbit(szcvec) - 1;
3144 3089 pgsz = page_get_pagesize(szc);
3145 3090 eaddr = addr + size;
3146 3091 addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
3147 3092 eaddr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
3148 3093
3149 3094 save_szcvec = szcvec;
3150 3095 szcvec >>= (szc + 1);
3151 3096 nszc = szc;
3152 3097 while (szcvec) {
3153 3098 if ((szcvec & 0x1) == 0) {
3154 3099 nszc++;
3155 3100 szcvec >>= 1;
3156 3101 continue;
3157 3102 }
3158 3103 nszc++;
3159 3104 pgsz = page_get_pagesize(nszc);
3160 3105 a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
3161 3106 if (a != addr) {
3162 3107 ASSERT(szc > 0);
3163 3108 ASSERT(a < eaddr);
3164 3109 segsize = a - addr;
3165 3110 error = as_iset1_default_lpsize(as, addr, segsize, szc,
3166 3111 save_szcvec);
3167 3112 if (error) {
3168 3113 return (error);
3169 3114 }
3170 3115 addr = a;
3171 3116 }
3172 3117 szc = nszc;
3173 3118 szcvec >>= 1;
3174 3119 }
3175 3120
3176 3121 ASSERT(addr < eaddr);
3177 3122 szcvec = save_szcvec;
3178 3123 while (szcvec) {
3179 3124 a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
3180 3125 ASSERT(a >= addr);
3181 3126 if (a != addr) {
3182 3127 ASSERT(szc > 0);
3183 3128 segsize = a - addr;
3184 3129 error = as_iset1_default_lpsize(as, addr, segsize, szc,
3185 3130 save_szcvec);
3186 3131 if (error) {
3187 3132 return (error);
3188 3133 }
3189 3134 addr = a;
3190 3135 }
3191 3136 szcvec &= ~(1 << szc);
3192 3137 if (szcvec) {
3193 3138 szc = highbit(szcvec) - 1;
3194 3139 pgsz = page_get_pagesize(szc);
3195 3140 }
3196 3141 }
3197 3142 ASSERT(addr == eaddr);
3198 3143
3199 3144 return (0);
3200 3145 }
3201 3146
3202 3147 /*
3203 3148 * Set the default large page size for the range. Called via memcntl with
3204 3149 * page size set to 0. as_set_default_lpsize breaks the range down into
3205 3150 * chunks with the same type/flags, ignores-non segvn segments, and passes
3206 3151 * each chunk to as_iset_default_lpsize().
3207 3152 */
3208 3153 int
3209 3154 as_set_default_lpsize(struct as *as, caddr_t addr, size_t size)
3210 3155 {
3211 3156 struct seg *seg;
3212 3157 caddr_t raddr;
3213 3158 size_t rsize;
3214 3159 size_t ssize;
3215 3160 int rtype, rflags;
3216 3161 int stype, sflags;
3217 3162 int error;
3218 3163 caddr_t setaddr;
3219 3164 size_t setsize;
3220 3165 int segvn;
3221 3166
3222 3167 if (size == 0)
3223 3168 return (0);
3224 3169
3225 3170 AS_LOCK_ENTER(as, RW_WRITER);
3226 3171 again:
3227 3172 error = 0;
3228 3173
3229 3174 raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3230 3175 rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
3231 3176 (size_t)raddr;
3232 3177
3233 3178 if (raddr + rsize < raddr) { /* check for wraparound */
3234 3179 AS_LOCK_EXIT(as);
3235 3180 return (ENOMEM);
3236 3181 }
3237 3182 as_clearwatchprot(as, raddr, rsize);
3238 3183 seg = as_segat(as, raddr);
3239 3184 if (seg == NULL) {
3240 3185 as_setwatch(as);
3241 3186 AS_LOCK_EXIT(as);
3242 3187 return (ENOMEM);
3243 3188 }
3244 3189 if (seg->s_ops == &segvn_ops) {
3245 3190 rtype = SEGOP_GETTYPE(seg, addr);
3246 3191 rflags = rtype & (MAP_TEXT | MAP_INITDATA);
3247 3192 rtype = rtype & (MAP_SHARED | MAP_PRIVATE);
3248 3193 segvn = 1;
3249 3194 } else {
3250 3195 segvn = 0;
3251 3196 }
3252 3197 setaddr = raddr;
3253 3198 setsize = 0;
3254 3199
3255 3200 for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
3256 3201 if (raddr >= (seg->s_base + seg->s_size)) {
3257 3202 seg = AS_SEGNEXT(as, seg);
3258 3203 if (seg == NULL || raddr != seg->s_base) {
3259 3204 error = ENOMEM;
3260 3205 break;
3261 3206 }
3262 3207 if (seg->s_ops == &segvn_ops) {
3263 3208 stype = SEGOP_GETTYPE(seg, raddr);
3264 3209 sflags = stype & (MAP_TEXT | MAP_INITDATA);
3265 3210 stype &= (MAP_SHARED | MAP_PRIVATE);
3266 3211 if (segvn && (rflags != sflags ||
3267 3212 rtype != stype)) {
3268 3213 /*
3269 3214 * The next segment is also segvn but
3270 3215 * has different flags and/or type.
3271 3216 */
3272 3217 ASSERT(setsize != 0);
3273 3218 error = as_iset_default_lpsize(as,
3274 3219 setaddr, setsize, rflags, rtype);
3275 3220 if (error) {
3276 3221 break;
3277 3222 }
3278 3223 rflags = sflags;
3279 3224 rtype = stype;
3280 3225 setaddr = raddr;
3281 3226 setsize = 0;
3282 3227 } else if (!segvn) {
3283 3228 rflags = sflags;
3284 3229 rtype = stype;
3285 3230 setaddr = raddr;
3286 3231 setsize = 0;
3287 3232 segvn = 1;
3288 3233 }
3289 3234 } else if (segvn) {
3290 3235 /* The next segment is not segvn. */
3291 3236 ASSERT(setsize != 0);
3292 3237 error = as_iset_default_lpsize(as,
3293 3238 setaddr, setsize, rflags, rtype);
3294 3239 if (error) {
3295 3240 break;
3296 3241 }
3297 3242 segvn = 0;
3298 3243 }
3299 3244 }
3300 3245 if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
3301 3246 ssize = seg->s_base + seg->s_size - raddr;
3302 3247 } else {
3303 3248 ssize = rsize;
3304 3249 }
3305 3250 }
3306 3251 if (error == 0 && segvn) {
3307 3252 /* The last chunk when rsize == 0. */
3308 3253 ASSERT(setsize != 0);
3309 3254 error = as_iset_default_lpsize(as, setaddr, setsize,
3310 3255 rflags, rtype);
3311 3256 }
3312 3257
3313 3258 if (error == IE_RETRY) {
3314 3259 goto again;
3315 3260 } else if (error == IE_NOMEM) {
3316 3261 error = EAGAIN;
3317 3262 } else if (error == ENOTSUP) {
3318 3263 error = EINVAL;
3319 3264 } else if (error == EAGAIN) {
3320 3265 mutex_enter(&as->a_contents);
3321 3266 if (!AS_ISNOUNMAPWAIT(as)) {
3322 3267 if (AS_ISUNMAPWAIT(as) == 0) {
3323 3268 cv_broadcast(&as->a_cv);
3324 3269 }
3325 3270 AS_SETUNMAPWAIT(as);
3326 3271 AS_LOCK_EXIT(as);
3327 3272 while (AS_ISUNMAPWAIT(as)) {
3328 3273 cv_wait(&as->a_cv, &as->a_contents);
3329 3274 }
3330 3275 mutex_exit(&as->a_contents);
3331 3276 AS_LOCK_ENTER(as, RW_WRITER);
3332 3277 } else {
3333 3278 /*
3334 3279 * We may have raced with
3335 3280 * segvn_reclaim()/segspt_reclaim(). In this case
3336 3281 * clean nounmapwait flag and retry since softlockcnt
3337 3282 * in this segment may be already 0. We don't drop as
3338 3283 * writer lock so our number of retries without
3339 3284 * sleeping should be very small. See segvn_reclaim()
3340 3285 * for more comments.
3341 3286 */
3342 3287 AS_CLRNOUNMAPWAIT(as);
3343 3288 mutex_exit(&as->a_contents);
3344 3289 }
3345 3290 goto again;
3346 3291 }
3347 3292
3348 3293 as_setwatch(as);
3349 3294 AS_LOCK_EXIT(as);
3350 3295 return (error);
3351 3296 }
3352 3297
3353 3298 /*
3354 3299 * Setup all of the uninitialized watched pages that we can.
3355 3300 */
3356 3301 void
3357 3302 as_setwatch(struct as *as)
3358 3303 {
3359 3304 struct watched_page *pwp;
3360 3305 struct seg *seg;
3361 3306 caddr_t vaddr;
3362 3307 uint_t prot;
3363 3308 int err, retrycnt;
3364 3309
3365 3310 if (avl_numnodes(&as->a_wpage) == 0)
3366 3311 return;
3367 3312
3368 3313 ASSERT(AS_WRITE_HELD(as));
3369 3314
3370 3315 for (pwp = avl_first(&as->a_wpage); pwp != NULL;
3371 3316 pwp = AVL_NEXT(&as->a_wpage, pwp)) {
3372 3317 retrycnt = 0;
3373 3318 retry:
3374 3319 vaddr = pwp->wp_vaddr;
3375 3320 if (pwp->wp_oprot != 0 || /* already set up */
3376 3321 (seg = as_segat(as, vaddr)) == NULL ||
3377 3322 SEGOP_GETPROT(seg, vaddr, 0, &prot) != 0)
3378 3323 continue;
3379 3324
3380 3325 pwp->wp_oprot = prot;
3381 3326 if (pwp->wp_read)
3382 3327 prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3383 3328 if (pwp->wp_write)
3384 3329 prot &= ~PROT_WRITE;
3385 3330 if (pwp->wp_exec)
3386 3331 prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3387 3332 if (!(pwp->wp_flags & WP_NOWATCH) && prot != pwp->wp_oprot) {
3388 3333 err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
3389 3334 if (err == IE_RETRY) {
3390 3335 pwp->wp_oprot = 0;
3391 3336 ASSERT(retrycnt == 0);
3392 3337 retrycnt++;
3393 3338 goto retry;
3394 3339 }
3395 3340 }
3396 3341 pwp->wp_prot = prot;
3397 3342 }
3398 3343 }
3399 3344
3400 3345 /*
3401 3346 * Clear all of the watched pages in the address space.
3402 3347 */
3403 3348 void
3404 3349 as_clearwatch(struct as *as)
3405 3350 {
3406 3351 struct watched_page *pwp;
3407 3352 struct seg *seg;
3408 3353 caddr_t vaddr;
3409 3354 uint_t prot;
3410 3355 int err, retrycnt;
3411 3356
3412 3357 if (avl_numnodes(&as->a_wpage) == 0)
3413 3358 return;
3414 3359
3415 3360 ASSERT(AS_WRITE_HELD(as));
3416 3361
3417 3362 for (pwp = avl_first(&as->a_wpage); pwp != NULL;
3418 3363 pwp = AVL_NEXT(&as->a_wpage, pwp)) {
3419 3364 retrycnt = 0;
3420 3365 retry:
3421 3366 vaddr = pwp->wp_vaddr;
3422 3367 if (pwp->wp_oprot == 0 || /* not set up */
3423 3368 (seg = as_segat(as, vaddr)) == NULL)
3424 3369 continue;
3425 3370
3426 3371 if ((prot = pwp->wp_oprot) != pwp->wp_prot) {
3427 3372 err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
3428 3373 if (err == IE_RETRY) {
3429 3374 ASSERT(retrycnt == 0);
3430 3375 retrycnt++;
3431 3376 goto retry;
3432 3377 }
3433 3378 }
3434 3379 pwp->wp_oprot = 0;
3435 3380 pwp->wp_prot = 0;
3436 3381 }
3437 3382 }
3438 3383
3439 3384 /*
3440 3385 * Force a new setup for all the watched pages in the range.
3441 3386 */
3442 3387 static void
3443 3388 as_setwatchprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
3444 3389 {
3445 3390 struct watched_page *pwp;
3446 3391 struct watched_page tpw;
3447 3392 caddr_t eaddr = addr + size;
3448 3393 caddr_t vaddr;
3449 3394 struct seg *seg;
3450 3395 int err, retrycnt;
3451 3396 uint_t wprot;
3452 3397 avl_index_t where;
3453 3398
3454 3399 if (avl_numnodes(&as->a_wpage) == 0)
3455 3400 return;
3456 3401
3457 3402 ASSERT(AS_WRITE_HELD(as));
3458 3403
3459 3404 tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3460 3405 if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
3461 3406 pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
3462 3407
3463 3408 while (pwp != NULL && pwp->wp_vaddr < eaddr) {
3464 3409 retrycnt = 0;
3465 3410 vaddr = pwp->wp_vaddr;
3466 3411
3467 3412 wprot = prot;
3468 3413 if (pwp->wp_read)
3469 3414 wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3470 3415 if (pwp->wp_write)
3471 3416 wprot &= ~PROT_WRITE;
3472 3417 if (pwp->wp_exec)
3473 3418 wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3474 3419 if (!(pwp->wp_flags & WP_NOWATCH) && wprot != pwp->wp_oprot) {
3475 3420 retry:
3476 3421 seg = as_segat(as, vaddr);
3477 3422 if (seg == NULL) {
3478 3423 panic("as_setwatchprot: no seg");
3479 3424 /*NOTREACHED*/
3480 3425 }
3481 3426 err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, wprot);
3482 3427 if (err == IE_RETRY) {
3483 3428 ASSERT(retrycnt == 0);
3484 3429 retrycnt++;
3485 3430 goto retry;
3486 3431 }
3487 3432 }
3488 3433 pwp->wp_oprot = prot;
3489 3434 pwp->wp_prot = wprot;
3490 3435
3491 3436 pwp = AVL_NEXT(&as->a_wpage, pwp);
3492 3437 }
3493 3438 }
3494 3439
3495 3440 /*
3496 3441 * Clear all of the watched pages in the range.
3497 3442 */
3498 3443 static void
3499 3444 as_clearwatchprot(struct as *as, caddr_t addr, size_t size)
3500 3445 {
3501 3446 caddr_t eaddr = addr + size;
3502 3447 struct watched_page *pwp;
3503 3448 struct watched_page tpw;
3504 3449 uint_t prot;
3505 3450 struct seg *seg;
3506 3451 int err, retrycnt;
3507 3452 avl_index_t where;
3508 3453
3509 3454 if (avl_numnodes(&as->a_wpage) == 0)
3510 3455 return;
3511 3456
3512 3457 tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3513 3458 if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
3514 3459 pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
3515 3460
3516 3461 ASSERT(AS_WRITE_HELD(as));
3517 3462
3518 3463 while (pwp != NULL && pwp->wp_vaddr < eaddr) {
3519 3464
3520 3465 if ((prot = pwp->wp_oprot) != 0) {
3521 3466 retrycnt = 0;
3522 3467
3523 3468 if (prot != pwp->wp_prot) {
3524 3469 retry:
3525 3470 seg = as_segat(as, pwp->wp_vaddr);
3526 3471 if (seg == NULL)
3527 3472 continue;
3528 3473 err = SEGOP_SETPROT(seg, pwp->wp_vaddr,
3529 3474 PAGESIZE, prot);
3530 3475 if (err == IE_RETRY) {
3531 3476 ASSERT(retrycnt == 0);
3532 3477 retrycnt++;
3533 3478 goto retry;
3534 3479
3535 3480 }
3536 3481 }
3537 3482 pwp->wp_oprot = 0;
3538 3483 pwp->wp_prot = 0;
3539 3484 }
3540 3485
3541 3486 pwp = AVL_NEXT(&as->a_wpage, pwp);
3542 3487 }
3543 3488 }
3544 3489
3545 3490 void
3546 3491 as_signal_proc(struct as *as, k_siginfo_t *siginfo)
3547 3492 {
3548 3493 struct proc *p;
3549 3494
3550 3495 mutex_enter(&pidlock);
3551 3496 for (p = practive; p; p = p->p_next) {
3552 3497 if (p->p_as == as) {
3553 3498 mutex_enter(&p->p_lock);
3554 3499 if (p->p_as == as)
3555 3500 sigaddq(p, NULL, siginfo, KM_NOSLEEP);
3556 3501 mutex_exit(&p->p_lock);
3557 3502 }
3558 3503 }
3559 3504 mutex_exit(&pidlock);
3560 3505 }
3561 3506
3562 3507 /*
3563 3508 * return memory object ID
3564 3509 */
3565 3510 int
3566 3511 as_getmemid(struct as *as, caddr_t addr, memid_t *memidp)
3567 3512 {
3568 3513 struct seg *seg;
3569 3514 int sts;
3570 3515
3571 3516 AS_LOCK_ENTER(as, RW_READER);
3572 3517 seg = as_segat(as, addr);
3573 3518 if (seg == NULL) {
3574 3519 AS_LOCK_EXIT(as);
3575 3520 return (EFAULT);
3576 3521 }
3577 3522 /*
3578 3523 * catch old drivers which may not support getmemid
3579 3524 */
3580 3525 if (seg->s_ops->getmemid == NULL) {
3581 3526 AS_LOCK_EXIT(as);
3582 3527 return (ENODEV);
3583 3528 }
3584 3529
3585 3530 sts = SEGOP_GETMEMID(seg, addr, memidp);
3586 3531
3587 3532 AS_LOCK_EXIT(as);
3588 3533 return (sts);
3589 3534 }
↓ open down ↓ |
1472 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX