]> git.saurik.com Git - apple/xnu.git/blame - osfmk/default_pager/dp_memory_object.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / default_pager / dp_memory_object.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57/*
58 * Default Pager.
59 * Memory Object Management.
60 */
61
62#include "default_pager_internal.h"
91447636
A
63#include <default_pager/default_pager_object_server.h>
64#include <mach/memory_object_default_server.h>
65#include <mach/memory_object_control.h>
0b4e3aa0 66#include <mach/memory_object_types.h>
1c79356b 67#include <mach/memory_object_server.h>
91447636
A
68#include <mach/upl.h>
69#include <mach/vm_map.h>
0b4e3aa0
A
70#include <vm/memory_object.h>
71#include <vm/vm_pageout.h>
91447636
A
72#include <vm/vm_map.h>
73#include <vm/vm_protos.h>
1c79356b 74
91447636
A
75/* forward declaration */
76vstruct_t vs_object_create(vm_size_t size);
1c79356b
A
77
78/*
79 * List of all vstructs. A specific vstruct is
80 * found directly via its port, this list is
81 * only used for monitoring purposes by the
82 * default_pager_object* calls and by ps_delete
83 * when abstract memory objects must be scanned
84 * to remove any live storage on a segment which
85 * is to be removed.
86 */
87struct vstruct_list_head vstruct_list;
88
0b4e3aa0 89__private_extern__ void
1c79356b
A
90vstruct_list_insert(
91 vstruct_t vs)
92{
93 VSL_LOCK();
94 queue_enter(&vstruct_list.vsl_queue, vs, vstruct_t, vs_links);
95 vstruct_list.vsl_count++;
96 VSL_UNLOCK();
97}
98
1c79356b 99
0b4e3aa0 100__private_extern__ void
1c79356b
A
101vstruct_list_delete(
102 vstruct_t vs)
103{
104 queue_remove(&vstruct_list.vsl_queue, vs, vstruct_t, vs_links);
105 vstruct_list.vsl_count--;
106}
107
108/*
109 * We use the sequence numbers on requests to regulate
110 * our parallelism. In general, we allow multiple reads and writes
111 * to proceed in parallel, with the exception that reads must
112 * wait for previous writes to finish. (Because the kernel might
113 * generate a data-request for a page on the heels of a data-write
114 * for the same page, and we must avoid returning stale data.)
115 * terminate requests wait for proceeding reads and writes to finish.
116 */
117
0b4e3aa0
A
118static unsigned int default_pager_total = 0; /* debugging */
119static unsigned int default_pager_wait_seqno = 0; /* debugging */
120static unsigned int default_pager_wait_read = 0; /* debugging */
121static unsigned int default_pager_wait_write = 0; /* debugging */
1c79356b 122
0b4e3aa0 123__private_extern__ void
1c79356b
A
124vs_async_wait(
125 vstruct_t vs)
126{
1c79356b
A
127
128 ASSERT(vs->vs_async_pending >= 0);
129 while (vs->vs_async_pending > 0) {
130 vs->vs_waiting_async = TRUE;
0b4e3aa0 131 assert_wait(&vs->vs_async_pending, THREAD_UNINT);
1c79356b 132 VS_UNLOCK(vs);
9bccf70c 133 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
134 VS_LOCK(vs);
135 }
136 ASSERT(vs->vs_async_pending == 0);
137}
138
1c79356b 139
0b4e3aa0 140#if PARALLEL
1c79356b
A
141/*
142 * Waits for correct sequence number. Leaves pager locked.
0b4e3aa0
A
143 *
144 * JMM - Sequence numbers guarantee ordering of requests generated
145 * by a single thread if the receiver is multithreaded and
146 * the interfaces are asynchronous (i.e. sender can generate
147 * more than one request before the first is received in the
148 * pager). Normally, IPC would generate these number in that
149 * case. But we are trying to avoid using IPC for the in-kernel
150 * scenario. Since these are actually invoked synchronously
151 * anyway (in-kernel), we can just fake the sequence number
152 * generation here (thus avoiding the dependence on IPC).
1c79356b 153 */
0b4e3aa0 154__private_extern__ void
1c79356b 155vs_lock(
0b4e3aa0 156 vstruct_t vs)
1c79356b 157{
0b4e3aa0
A
158 mach_port_seqno_t seqno;
159
1c79356b
A
160 default_pager_total++;
161 VS_LOCK(vs);
162
163 seqno = vs->vs_next_seqno++;
164
165 while (vs->vs_seqno != seqno) {
166 default_pager_wait_seqno++;
167 vs->vs_waiting_seqno = TRUE;
0b4e3aa0 168 assert_wait(&vs->vs_seqno, THREAD_UNINT);
1c79356b 169 VS_UNLOCK(vs);
9bccf70c 170 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
171 VS_LOCK(vs);
172 }
173}
174
175/*
176 * Increments sequence number and unlocks pager.
177 */
0b4e3aa0 178__private_extern__ void
1c79356b
A
179vs_unlock(vstruct_t vs)
180{
1c79356b 181 vs->vs_seqno++;
0b4e3aa0
A
182 if (vs->vs_waiting_seqno) {
183 vs->vs_waiting_seqno = FALSE;
184 VS_UNLOCK(vs);
185 thread_wakeup(&vs->vs_seqno);
186 return;
187 }
1c79356b 188 VS_UNLOCK(vs);
1c79356b
A
189}
190
191/*
192 * Start a read - one more reader. Pager must be locked.
193 */
0b4e3aa0 194__private_extern__ void
1c79356b
A
195vs_start_read(
196 vstruct_t vs)
197{
198 vs->vs_readers++;
199}
200
201/*
202 * Wait for readers. Unlocks and relocks pager if wait needed.
203 */
0b4e3aa0 204__private_extern__ void
1c79356b
A
205vs_wait_for_readers(
206 vstruct_t vs)
207{
208 while (vs->vs_readers != 0) {
209 default_pager_wait_read++;
210 vs->vs_waiting_read = TRUE;
0b4e3aa0 211 assert_wait(&vs->vs_readers, THREAD_UNINT);
1c79356b 212 VS_UNLOCK(vs);
9bccf70c 213 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
214 VS_LOCK(vs);
215 }
216}
217
218/*
219 * Finish a read. Pager is unlocked and returns unlocked.
220 */
0b4e3aa0 221__private_extern__ void
1c79356b
A
222vs_finish_read(
223 vstruct_t vs)
224{
225 VS_LOCK(vs);
0b4e3aa0 226 if (--vs->vs_readers == 0 && vs->vs_waiting_read) {
1c79356b
A
227 vs->vs_waiting_read = FALSE;
228 VS_UNLOCK(vs);
0b4e3aa0
A
229 thread_wakeup(&vs->vs_readers);
230 return;
231 }
232 VS_UNLOCK(vs);
1c79356b
A
233}
234
235/*
236 * Start a write - one more writer. Pager must be locked.
237 */
0b4e3aa0 238__private_extern__ void
1c79356b
A
239vs_start_write(
240 vstruct_t vs)
241{
242 vs->vs_writers++;
243}
244
245/*
246 * Wait for writers. Unlocks and relocks pager if wait needed.
247 */
0b4e3aa0 248__private_extern__ void
1c79356b
A
249vs_wait_for_writers(
250 vstruct_t vs)
251{
252 while (vs->vs_writers != 0) {
253 default_pager_wait_write++;
254 vs->vs_waiting_write = TRUE;
0b4e3aa0 255 assert_wait(&vs->vs_writers, THREAD_UNINT);
1c79356b 256 VS_UNLOCK(vs);
9bccf70c 257 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
258 VS_LOCK(vs);
259 }
260 vs_async_wait(vs);
261}
262
263/* This is to be used for the transfer from segment code ONLY */
264/* The transfer code holds off vs destruction by keeping the */
265/* vs_async_wait count non-zero. It will not ocnflict with */
266/* other writers on an async basis because it only writes on */
267/* a cluster basis into fresh (as of sync time) cluster locations */
0b4e3aa0
A
268
269__private_extern__ void
1c79356b
A
270vs_wait_for_sync_writers(
271 vstruct_t vs)
272{
273 while (vs->vs_writers != 0) {
274 default_pager_wait_write++;
275 vs->vs_waiting_write = TRUE;
0b4e3aa0 276 assert_wait(&vs->vs_writers, THREAD_UNINT);
1c79356b 277 VS_UNLOCK(vs);
9bccf70c 278 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
279 VS_LOCK(vs);
280 }
281}
282
283
284/*
285 * Finish a write. Pager is unlocked and returns unlocked.
286 */
0b4e3aa0 287__private_extern__ void
1c79356b
A
288vs_finish_write(
289 vstruct_t vs)
290{
291 VS_LOCK(vs);
0b4e3aa0 292 if (--vs->vs_writers == 0 && vs->vs_waiting_write) {
1c79356b
A
293 vs->vs_waiting_write = FALSE;
294 VS_UNLOCK(vs);
0b4e3aa0
A
295 thread_wakeup(&vs->vs_writers);
296 return;
1c79356b 297 }
0b4e3aa0 298 VS_UNLOCK(vs);
1c79356b 299}
1c79356b
A
300#endif /* PARALLEL */
301
1c79356b
A
302vstruct_t
303vs_object_create(
304 vm_size_t size)
305{
306 vstruct_t vs;
1c79356b
A
307
308 /*
309 * Allocate a vstruct. If there are any problems, then report them
310 * to the console.
311 */
312 vs = ps_vstruct_create(size);
313 if (vs == VSTRUCT_NULL) {
314 dprintf(("vs_object_create: unable to allocate %s\n",
315 "-- either run swapon command or reboot"));
316 return VSTRUCT_NULL;
317 }
318
319 return vs;
320}
321
0b4e3aa0 322#if 0
1c79356b
A
323void default_pager_add(vstruct_t, boolean_t); /* forward */
324
325void
326default_pager_add(
327 vstruct_t vs,
328 boolean_t internal)
329{
0b4e3aa0
A
330 memory_object_t mem_obj = vs->vs_mem_obj;
331 mach_port_t pset;
1c79356b 332 mach_port_mscount_t sync;
0b4e3aa0 333 mach_port_t previous;
1c79356b
A
334 kern_return_t kr;
335 static char here[] = "default_pager_add";
336
337 /*
338 * The port currently has a make-send count of zero,
339 * because either we just created the port or we just
340 * received the port in a memory_object_create request.
341 */
342
343 if (internal) {
344 /* possibly generate an immediate no-senders notification */
345 sync = 0;
346 pset = default_pager_internal_set;
347 } else {
348 /* delay notification till send right is created */
349 sync = 1;
350 pset = default_pager_external_set;
351 }
352
353 ipc_port_make_sonce(mem_obj);
354 ip_lock(mem_obj); /* unlocked in nsrequest below */
355 ipc_port_nsrequest(mem_obj, sync, mem_obj, &previous);
356}
357
0b4e3aa0 358#endif
1c79356b 359
4452a7af
A
360const struct memory_object_pager_ops default_pager_ops = {
361 dp_memory_object_reference,
362 dp_memory_object_deallocate,
363 dp_memory_object_init,
364 dp_memory_object_terminate,
365 dp_memory_object_data_request,
366 dp_memory_object_data_return,
367 dp_memory_object_data_initialize,
368 dp_memory_object_data_unlock,
369 dp_memory_object_synchronize,
370 dp_memory_object_unmap,
371 "default pager"
372};
373
1c79356b
A
374kern_return_t
375dp_memory_object_init(
0b4e3aa0
A
376 memory_object_t mem_obj,
377 memory_object_control_t control,
91447636 378 __unused vm_size_t pager_page_size)
1c79356b 379{
1c79356b 380 vstruct_t vs;
1c79356b
A
381
382 assert(pager_page_size == vm_page_size);
383
0b4e3aa0
A
384 memory_object_control_reference(control);
385
1c79356b 386 vs_lookup(mem_obj, vs);
0b4e3aa0 387 vs_lock(vs);
1c79356b 388
0b4e3aa0 389 if (vs->vs_control != MEMORY_OBJECT_CONTROL_NULL)
1c79356b
A
390 Panic("bad request");
391
0b4e3aa0 392 vs->vs_control = control;
1c79356b
A
393 vs_unlock(vs);
394
395 return KERN_SUCCESS;
396}
397
398kern_return_t
399dp_memory_object_synchronize(
0b4e3aa0
A
400 memory_object_t mem_obj,
401 memory_object_offset_t offset,
402 vm_size_t length,
91447636 403 __unused vm_sync_t flags)
1c79356b 404{
1c79356b 405 vstruct_t vs;
1c79356b
A
406
407 vs_lookup(mem_obj, vs);
0b4e3aa0 408 vs_lock(vs);
1c79356b
A
409 vs_unlock(vs);
410
0b4e3aa0 411 memory_object_synchronize_completed(vs->vs_control, offset, length);
1c79356b
A
412
413 return KERN_SUCCESS;
414}
415
0b4e3aa0
A
416kern_return_t
417dp_memory_object_unmap(
91447636 418 __unused memory_object_t mem_obj)
0b4e3aa0
A
419{
420 panic("dp_memory_object_unmap");
421
422 return KERN_FAILURE;
423}
424
1c79356b
A
425kern_return_t
426dp_memory_object_terminate(
0b4e3aa0 427 memory_object_t mem_obj)
1c79356b 428{
0b4e3aa0 429 memory_object_control_t control;
1c79356b 430 vstruct_t vs;
1c79356b
A
431
432 /*
433 * control port is a receive right, not a send right.
434 */
435
436 vs_lookup(mem_obj, vs);
0b4e3aa0 437 vs_lock(vs);
1c79356b
A
438
439 /*
440 * Wait for read and write requests to terminate.
441 */
442
443 vs_wait_for_readers(vs);
444 vs_wait_for_writers(vs);
445
446 /*
447 * After memory_object_terminate both memory_object_init
448 * and a no-senders notification are possible, so we need
0b4e3aa0
A
449 * to clean up our reference to the memory_object_control
450 * to prepare for a new init.
1c79356b
A
451 */
452
0b4e3aa0
A
453 control = vs->vs_control;
454 vs->vs_control = MEMORY_OBJECT_CONTROL_NULL;
1c79356b
A
455
456 /* a bit of special case ugliness here. Wakeup any waiting reads */
457 /* these data requests had to be removed from the seqno traffic */
458 /* based on a performance bottleneck with large memory objects */
459 /* the problem will right itself with the new component based */
460 /* synchronous interface. The new async will be able to return */
461 /* failure during its sync phase. In the mean time ... */
462
0b4e3aa0
A
463 thread_wakeup(&vs->vs_writers);
464 thread_wakeup(&vs->vs_async_pending);
1c79356b
A
465
466 vs_unlock(vs);
467
468 /*
0b4e3aa0 469 * Now we deallocate our reference on the control.
1c79356b 470 */
0b4e3aa0 471 memory_object_control_deallocate(control);
1c79356b
A
472 return KERN_SUCCESS;
473}
474
475void
0b4e3aa0
A
476dp_memory_object_reference(
477 memory_object_t mem_obj)
478{
479 vstruct_t vs;
480
481 vs_lookup_safe(mem_obj, vs);
482 if (vs == VSTRUCT_NULL)
483 return;
484
485 VS_LOCK(vs);
486 assert(vs->vs_references > 0);
487 vs->vs_references++;
488 VS_UNLOCK(vs);
489}
490
0b4e3aa0
A
491void
492dp_memory_object_deallocate(
493 memory_object_t mem_obj)
1c79356b
A
494{
495 vstruct_t vs;
0b4e3aa0 496 mach_port_seqno_t seqno;
1c79356b
A
497
498 /*
0b4e3aa0 499 * Because we don't give out multiple first references
1c79356b 500 * for a memory object, there can't be a race
0b4e3aa0
A
501 * between getting a deallocate call and creating
502 * a new reference for the object.
1c79356b
A
503 */
504
0b4e3aa0
A
505 vs_lookup_safe(mem_obj, vs);
506 if (vs == VSTRUCT_NULL)
507 return;
508
509 VS_LOCK(vs);
510 if (--vs->vs_references > 0) {
511 VS_UNLOCK(vs);
512 return;
513 }
514
515 seqno = vs->vs_next_seqno++;
516 while (vs->vs_seqno != seqno) {
517 default_pager_wait_seqno++;
518 vs->vs_waiting_seqno = TRUE;
519 assert_wait(&vs->vs_seqno, THREAD_UNINT);
520 VS_UNLOCK(vs);
9bccf70c 521 thread_block(THREAD_CONTINUE_NULL);
0b4e3aa0
A
522 VS_LOCK(vs);
523 }
524
1c79356b
A
525 vs_async_wait(vs); /* wait for pending async IO */
526
527 /* do not delete the vs structure until the referencing pointers */
528 /* in the vstruct list have been expunged */
529
530 /* get VSL_LOCK out of order by using TRY mechanism */
531 while(!VSL_LOCK_TRY()) {
532 VS_UNLOCK(vs);
533 VSL_LOCK();
534 VSL_UNLOCK();
535 VS_LOCK(vs);
536 vs_async_wait(vs); /* wait for pending async IO */
537 }
0b4e3aa0
A
538
539
1c79356b 540 /*
0b4e3aa0 541 * We shouldn't get a deallocation call
1c79356b
A
542 * when the kernel has the object cached.
543 */
0b4e3aa0 544 if (vs->vs_control != MEMORY_OBJECT_CONTROL_NULL)
1c79356b
A
545 Panic("bad request");
546
547 /*
548 * Unlock the pager (though there should be no one
549 * waiting for it).
550 */
551 VS_UNLOCK(vs);
552
0b4e3aa0
A
553 /* Lock out paging segment removal for the duration of this */
554 /* call. We are vulnerable to losing a paging segment we rely */
555 /* on as soon as we remove ourselves from the VSL and unlock */
556
557 /* Keep our thread from blocking on attempt to trigger backing */
558 /* store release */
559 backing_store_release_trigger_disable += 1;
560
1c79356b
A
561 /*
562 * Remove the memory object port association, and then
563 * the destroy the port itself. We must remove the object
564 * from the port list before deallocating the pager,
565 * because of default_pager_objects.
566 */
567 vstruct_list_delete(vs);
0b4e3aa0
A
568 VSL_UNLOCK();
569
1c79356b
A
570 ps_vstruct_dealloc(vs);
571
0b4e3aa0
A
572 VSL_LOCK();
573 backing_store_release_trigger_disable -= 1;
574 if(backing_store_release_trigger_disable == 0) {
9bccf70c 575 thread_wakeup((event_t)&backing_store_release_trigger_disable);
1c79356b
A
576 }
577 VSL_UNLOCK();
578}
579
580kern_return_t
581dp_memory_object_data_request(
0b4e3aa0
A
582 memory_object_t mem_obj,
583 memory_object_offset_t offset,
1c79356b 584 vm_size_t length,
91447636 585 __unused vm_prot_t protection_required)
1c79356b 586{
1c79356b 587 vstruct_t vs;
1c79356b
A
588
589 GSTAT(global_stats.gs_pagein_calls++);
590
591
592 /* CDY at this moment vs_lookup panics when presented with the wrong */
593 /* port. As we are expanding this pager to support user interfaces */
594 /* this should be changed to return kern_failure */
595 vs_lookup(mem_obj, vs);
0b4e3aa0 596 vs_lock(vs);
1c79356b
A
597
598 /* We are going to relax the strict sequencing here for performance */
599 /* reasons. We can do this because we know that the read and */
600 /* write threads are different and we rely on synchronization */
601 /* of read and write requests at the cache memory_object level */
602 /* break out wait_for_writers, all of this goes away when */
603 /* we get real control of seqno with the new component interface */
0b4e3aa0 604
1c79356b
A
605 if (vs->vs_writers != 0) {
606 /* you can't hold on to the seqno and go */
607 /* to sleep like that */
608 vs_unlock(vs); /* bump internal count of seqno */
609 VS_LOCK(vs);
610 while (vs->vs_writers != 0) {
611 default_pager_wait_write++;
612 vs->vs_waiting_write = TRUE;
0b4e3aa0 613 assert_wait(&vs->vs_writers, THREAD_UNINT);
1c79356b 614 VS_UNLOCK(vs);
9bccf70c 615 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
616 VS_LOCK(vs);
617 vs_async_wait(vs);
618 }
0b4e3aa0 619 if(vs->vs_control == MEMORY_OBJECT_CONTROL_NULL) {
1c79356b
A
620 VS_UNLOCK(vs);
621 return KERN_FAILURE;
622 }
623 vs_start_read(vs);
624 VS_UNLOCK(vs);
625 } else {
626 vs_start_read(vs);
627 vs_unlock(vs);
628 }
629
630 /*
631 * Request must be on a page boundary and a multiple of pages.
632 */
633 if ((offset & vm_page_mask) != 0 || (length & vm_page_mask) != 0)
634 Panic("bad alignment");
635
636 pvs_cluster_read(vs, (vm_offset_t)offset, length);
637
638 vs_finish_read(vs);
639
640 return KERN_SUCCESS;
641}
642
643/*
644 * memory_object_data_initialize: check whether we already have each page, and
645 * write it if we do not. The implementation is far from optimized, and
646 * also assumes that the default_pager is single-threaded.
647 */
648/* It is questionable whether or not a pager should decide what is relevant */
649/* and what is not in data sent from the kernel. Data initialize has been */
650/* changed to copy back all data sent to it in preparation for its eventual */
651/* merge with data return. It is the kernel that should decide what pages */
652/* to write back. As of the writing of this note, this is indeed the case */
653/* the kernel writes back one page at a time through this interface */
654
655kern_return_t
656dp_memory_object_data_initialize(
0b4e3aa0
A
657 memory_object_t mem_obj,
658 memory_object_offset_t offset,
659 vm_size_t size)
1c79356b 660{
1c79356b 661 vstruct_t vs;
1c79356b 662
91447636
A
663 DP_DEBUG(DEBUG_MO_EXTERNAL,
664 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
665 (int)mem_obj, (int)offset, (int)size));
55e303ae 666 GSTAT(global_stats.gs_pages_init += atop_32(size));
1c79356b
A
667
668 vs_lookup(mem_obj, vs);
0b4e3aa0 669 vs_lock(vs);
1c79356b
A
670 vs_start_write(vs);
671 vs_unlock(vs);
672
673 /*
674 * Write the data via clustered writes. vs_cluster_write will
675 * loop if the address range specified crosses cluster
676 * boundaries.
677 */
0b4e3aa0 678 vs_cluster_write(vs, 0, (vm_offset_t)offset, size, FALSE, 0);
1c79356b
A
679
680 vs_finish_write(vs);
681
682 return KERN_SUCCESS;
683}
684
1c79356b
A
685kern_return_t
686dp_memory_object_data_unlock(
91447636
A
687 __unused memory_object_t mem_obj,
688 __unused memory_object_offset_t offset,
689 __unused vm_size_t size,
690 __unused vm_prot_t desired_access)
1c79356b 691{
0b4e3aa0 692 Panic("dp_memory_object_data_unlock: illegal");
1c79356b
A
693 return KERN_FAILURE;
694}
695
696
91447636 697/*ARGSUSED8*/
1c79356b
A
698kern_return_t
699dp_memory_object_data_return(
0b4e3aa0
A
700 memory_object_t mem_obj,
701 memory_object_offset_t offset,
91447636
A
702 vm_size_t size,
703 __unused memory_object_offset_t *resid_offset,
704 __unused int *io_error,
705 __unused boolean_t dirty,
706 __unused boolean_t kernel_copy,
707 __unused int upl_flags)
1c79356b 708{
1c79356b 709 vstruct_t vs;
1c79356b 710
91447636
A
711 DP_DEBUG(DEBUG_MO_EXTERNAL,
712 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
713 (int)mem_obj, (int)offset, (int)size));
1c79356b
A
714 GSTAT(global_stats.gs_pageout_calls++);
715
716 /* This routine is called by the pageout thread. The pageout thread */
717 /* cannot be blocked by read activities unless the read activities */
718 /* Therefore the grant of vs lock must be done on a try versus a */
719 /* blocking basis. The code below relies on the fact that the */
720 /* interface is synchronous. Should this interface be again async */
721 /* for some type of pager in the future the pages will have to be */
722 /* returned through a separate, asynchronous path. */
723
724 vs_lookup(mem_obj, vs);
725
726 default_pager_total++;
727 if(!VS_TRY_LOCK(vs)) {
728 /* the call below will not be done by caller when we have */
729 /* a synchronous interface */
730 /* return KERN_LOCK_OWNED; */
731 upl_t upl;
4452a7af 732 unsigned int page_list_count = 0;
0b4e3aa0
A
733 memory_object_super_upl_request(vs->vs_control,
734 (memory_object_offset_t)offset,
735 size, size,
736 &upl, NULL, &page_list_count,
737 UPL_NOBLOCK | UPL_CLEAN_IN_PLACE
1c79356b 738 | UPL_NO_SYNC | UPL_COPYOUT_FROM);
0b4e3aa0
A
739 upl_abort(upl,0);
740 upl_deallocate(upl);
1c79356b
A
741 return KERN_SUCCESS;
742 }
743
d12e1678
A
744 if ((vs->vs_seqno != vs->vs_next_seqno++)
745 || (vs->vs_readers)
746 || (vs->vs_xfer_pending)) {
4452a7af
A
747 upl_t upl;
748 unsigned int page_list_count = 0;
0b4e3aa0 749
1c79356b
A
750 vs->vs_next_seqno--;
751 VS_UNLOCK(vs);
0b4e3aa0 752
1c79356b
A
753 /* the call below will not be done by caller when we have */
754 /* a synchronous interface */
755 /* return KERN_LOCK_OWNED; */
0b4e3aa0
A
756 memory_object_super_upl_request(vs->vs_control,
757 (memory_object_offset_t)offset,
758 size, size,
759 &upl, NULL, &page_list_count,
1c79356b
A
760 UPL_NOBLOCK | UPL_CLEAN_IN_PLACE
761 | UPL_NO_SYNC | UPL_COPYOUT_FROM);
0b4e3aa0
A
762 upl_abort(upl,0);
763 upl_deallocate(upl);
1c79356b
A
764 return KERN_SUCCESS;
765 }
766
0b4e3aa0 767 if ((size % vm_page_size) != 0)
1c79356b
A
768 Panic("bad alignment");
769
770 vs_start_write(vs);
771
772
773 vs->vs_async_pending += 1; /* protect from backing store contraction */
0b4e3aa0 774 vs_unlock(vs);
1c79356b
A
775
776 /*
777 * Write the data via clustered writes. vs_cluster_write will
778 * loop if the address range specified crosses cluster
779 * boundaries.
780 */
0b4e3aa0 781 vs_cluster_write(vs, 0, (vm_offset_t)offset, size, FALSE, 0);
1c79356b
A
782
783 vs_finish_write(vs);
784
785 /* temporary, need a finer lock based on cluster */
786
787 VS_LOCK(vs);
788 vs->vs_async_pending -= 1; /* release vs_async_wait */
0b4e3aa0
A
789 if (vs->vs_async_pending == 0 && vs->vs_waiting_async) {
790 vs->vs_waiting_async = FALSE;
1c79356b 791 VS_UNLOCK(vs);
0b4e3aa0 792 thread_wakeup(&vs->vs_async_pending);
1c79356b
A
793 } else {
794 VS_UNLOCK(vs);
795 }
796
797
798 return KERN_SUCCESS;
799}
800
0b4e3aa0
A
801/*
802 * Routine: default_pager_memory_object_create
803 * Purpose:
804 * Handle requests for memory objects from the
805 * kernel.
806 * Notes:
807 * Because we only give out the default memory
808 * manager port to the kernel, we don't have to
809 * be so paranoid about the contents.
810 */
1c79356b 811kern_return_t
0b4e3aa0 812default_pager_memory_object_create(
91447636 813 __unused memory_object_default_t dmm,
0b4e3aa0
A
814 vm_size_t new_size,
815 memory_object_t *new_mem_obj)
1c79356b 816{
0b4e3aa0 817 vstruct_t vs;
1c79356b 818
0b4e3aa0
A
819 assert(dmm == default_pager_object);
820
821 vs = vs_object_create(new_size);
822 if (vs == VSTRUCT_NULL)
823 return KERN_RESOURCE_SHORTAGE;
824
825 vs->vs_next_seqno = 0;
826
827 /*
828 * Set up associations between this memory object
829 * and this default_pager structure
830 */
831
4452a7af 832 vs->vs_pager_ops = &default_pager_ops;
0b4e3aa0
A
833 vs->vs_mem_obj_ikot = IKOT_MEMORY_OBJECT;
834
835 /*
836 * After this, other threads might receive requests
837 * for this memory object or find it in the port list.
838 */
839
840 vstruct_list_insert(vs);
841 *new_mem_obj = vs_to_mem_obj(vs);
842 return KERN_SUCCESS;
1c79356b
A
843}
844
845/*
846 * Create an external object.
847 */
848kern_return_t
849default_pager_object_create(
91447636 850 default_pager_t default_pager,
0b4e3aa0
A
851 vm_size_t size,
852 memory_object_t *mem_objp)
1c79356b
A
853{
854 vstruct_t vs;
1c79356b 855
91447636 856 if (default_pager != default_pager_object)
1c79356b
A
857 return KERN_INVALID_ARGUMENT;
858
859 vs = vs_object_create(size);
0b4e3aa0
A
860 if (vs == VSTRUCT_NULL)
861 return KERN_RESOURCE_SHORTAGE;
1c79356b 862
1c79356b 863 /*
0b4e3aa0 864 * Set up associations between the default pager
1c79356b
A
865 * and this vstruct structure
866 */
4452a7af 867 vs->vs_pager_ops = &default_pager_ops;
1c79356b 868 vstruct_list_insert(vs);
0b4e3aa0 869 *mem_objp = vs_to_mem_obj(vs);
1c79356b
A
870 return KERN_SUCCESS;
871}
872
873kern_return_t
874default_pager_objects(
91447636 875 default_pager_t default_pager,
1c79356b
A
876 default_pager_object_array_t *objectsp,
877 mach_msg_type_number_t *ocountp,
91447636 878 mach_port_array_t *portsp,
1c79356b
A
879 mach_msg_type_number_t *pcountp)
880{
881 vm_offset_t oaddr = 0; /* memory for objects */
882 vm_size_t osize = 0; /* current size */
883 default_pager_object_t * objects;
91447636 884 unsigned int opotential = 0;
1c79356b 885
91447636 886 vm_map_copy_t pcopy = 0; /* copy handle for pagers */
1c79356b 887 vm_size_t psize = 0; /* current size */
0b4e3aa0 888 memory_object_t * pagers;
91447636 889 unsigned int ppotential = 0;
1c79356b
A
890
891 unsigned int actual;
892 unsigned int num_objects;
893 kern_return_t kr;
894 vstruct_t entry;
1c79356b 895
91447636
A
896 if (default_pager != default_pager_object)
897 return KERN_INVALID_ARGUMENT;
1c79356b
A
898
899 /*
900 * We will send no more than this many
901 */
902 actual = vstruct_list.vsl_count;
1c79356b 903
91447636
A
904 /*
905 * Out out-of-line port arrays are simply kalloc'ed.
906 */
907 psize = round_page(actual * sizeof * pagers);
908 ppotential = psize / sizeof * pagers;
909 pagers = (memory_object_t *)kalloc(psize);
910 if (0 == pagers)
911 return KERN_RESOURCE_SHORTAGE;
912
913 /*
914 * returned out of line data must be allocated out
915 * the ipc_kernel_map, wired down, filled in, and
916 * then "copied in" as if it had been sent by a
917 * user process.
918 */
919 osize = round_page(actual * sizeof * objects);
920 opotential = osize / sizeof * objects;
921 kr = kmem_alloc(ipc_kernel_map, &oaddr, osize);
922 if (KERN_SUCCESS != kr) {
923 kfree(pagers, psize);
924 return KERN_RESOURCE_SHORTAGE;
1c79356b 925 }
91447636 926 objects = (default_pager_object_t *)oaddr;
1c79356b 927
1c79356b
A
928
929 /*
930 * Now scan the list.
931 */
932
933 VSL_LOCK();
934
935 num_objects = 0;
936 queue_iterate(&vstruct_list.vsl_queue, entry, vstruct_t, vs_links) {
937
91447636
A
938 memory_object_t pager;
939 vm_size_t size;
1c79356b
A
940
941 if ((num_objects >= opotential) ||
942 (num_objects >= ppotential)) {
943
944 /*
945 * This should be rare. In any case,
946 * we will only miss recent objects,
947 * because they are added at the end.
948 */
949 break;
950 }
951
952 /*
953 * Avoid interfering with normal operations
954 */
955 if (!VS_MAP_TRY_LOCK(entry))
956 goto not_this_one;
957 size = ps_vstruct_allocated_size(entry);
958 VS_MAP_UNLOCK(entry);
959
960 VS_LOCK(entry);
961
1c79356b 962 /*
0b4e3aa0
A
963 * We need a reference for our caller. Adding this
964 * reference through the linked list could race with
965 * destruction of the object. If we find the object
966 * has no references, just give up on it.
1c79356b 967 */
0b4e3aa0
A
968 VS_LOCK(entry);
969 if (entry->vs_references == 0) {
1c79356b 970 VS_UNLOCK(entry);
0b4e3aa0 971 goto not_this_one;
1c79356b 972 }
91447636
A
973 pager = vs_to_mem_obj(entry);
974 dp_memory_object_reference(pager);
1c79356b
A
975 VS_UNLOCK(entry);
976
977 /* the arrays are wired, so no deadlock worries */
978
979 objects[num_objects].dpo_object = (vm_offset_t) entry;
980 objects[num_objects].dpo_size = size;
0b4e3aa0 981 pagers [num_objects++] = pager;
1c79356b
A
982 continue;
983
984 not_this_one:
985 /*
986 * Do not return garbage
987 */
988 objects[num_objects].dpo_object = (vm_offset_t) 0;
989 objects[num_objects].dpo_size = 0;
0b4e3aa0 990 pagers[num_objects++] = MEMORY_OBJECT_NULL;
1c79356b
A
991
992 }
993
994 VSL_UNLOCK();
995
91447636
A
996 /* clear out any excess allocation */
997 while (num_objects < opotential) {
998 objects[--opotential].dpo_object = (vm_offset_t) 0;
999 objects[opotential].dpo_size = 0;
1c79356b 1000 }
91447636
A
1001 while (num_objects < ppotential) {
1002 pagers[--ppotential] = MEMORY_OBJECT_NULL;
1c79356b
A
1003 }
1004
91447636
A
1005 kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(oaddr),
1006 vm_map_round_page(oaddr + osize), FALSE);
1007 assert(KERN_SUCCESS == kr);
1008 kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)oaddr,
1009 (vm_map_size_t)osize, TRUE, &pcopy);
1010 assert(KERN_SUCCESS == kr);
1c79356b 1011
91447636
A
1012 *objectsp = (default_pager_object_array_t)objects;
1013 *ocountp = num_objects;
1014 *portsp = (mach_port_array_t)pcopy;
1015 *pcountp = num_objects;
1c79356b 1016
91447636 1017 return KERN_SUCCESS;
1c79356b
A
1018}
1019
1020kern_return_t
1021default_pager_object_pages(
91447636
A
1022 default_pager_t default_pager,
1023 mach_port_t memory_object,
1c79356b
A
1024 default_pager_page_array_t *pagesp,
1025 mach_msg_type_number_t *countp)
1026{
91447636 1027 vm_offset_t addr = 0; /* memory for page offsets */
1c79356b 1028 vm_size_t size = 0; /* current memory size */
91447636
A
1029 vm_map_copy_t copy;
1030 default_pager_page_t * pages = 0;
1031 unsigned int potential;
1032 unsigned int actual;
1c79356b 1033 kern_return_t kr;
91447636 1034 memory_object_t object;
1c79356b 1035
91447636 1036 if (default_pager != default_pager_object)
1c79356b 1037 return KERN_INVALID_ARGUMENT;
0b4e3aa0 1038
91447636 1039 object = (memory_object_t) memory_object;
1c79356b 1040
91447636 1041 potential = 0;
1c79356b
A
1042 for (;;) {
1043 vstruct_t entry;
1044
1045 VSL_LOCK();
1046 queue_iterate(&vstruct_list.vsl_queue, entry, vstruct_t,
1047 vs_links) {
1048 VS_LOCK(entry);
0b4e3aa0 1049 if (vs_to_mem_obj(entry) == object) {
1c79356b
A
1050 VSL_UNLOCK();
1051 goto found_object;
1052 }
1053 VS_UNLOCK(entry);
1054 }
1055 VSL_UNLOCK();
1056
1057 /* did not find the object */
91447636
A
1058 if (0 != addr)
1059 kmem_free(ipc_kernel_map, addr, size);
1c79356b 1060
1c79356b
A
1061 return KERN_INVALID_ARGUMENT;
1062
1063 found_object:
1064
1065 if (!VS_MAP_TRY_LOCK(entry)) {
1066 /* oh well bad luck */
9bccf70c 1067 int wresult;
1c79356b
A
1068
1069 VS_UNLOCK(entry);
1070
91447636 1071 assert_wait_timeout((event_t)assert_wait_timeout, THREAD_UNINT, 1, 1000*NSEC_PER_USEC);
9bccf70c
A
1072 wresult = thread_block(THREAD_CONTINUE_NULL);
1073 assert(wresult == THREAD_TIMED_OUT);
1c79356b
A
1074 continue;
1075 }
1076
1077 actual = ps_vstruct_allocated_pages(entry, pages, potential);
1078 VS_MAP_UNLOCK(entry);
1079 VS_UNLOCK(entry);
1080
1081 if (actual <= potential)
1082 break;
1083
1084 /* allocate more memory */
91447636
A
1085 if (0 != addr)
1086 kmem_free(ipc_kernel_map, addr, size);
1087
1088 size = round_page(actual * sizeof * pages);
1089 kr = kmem_alloc(ipc_kernel_map, &addr, size);
1090 if (KERN_SUCCESS != kr)
1091 return KERN_RESOURCE_SHORTAGE;
1c79356b 1092
1c79356b
A
1093 pages = (default_pager_page_t *)addr;
1094 potential = size / sizeof * pages;
1095 }
1096
1097 /*
91447636 1098 * Clear unused memory.
1c79356b 1099 */
91447636
A
1100 while (actual < potential)
1101 pages[--potential].dpp_offset = 0;
1102
1103 kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
1104 vm_map_round_page(addr + size), FALSE);
1105 assert(KERN_SUCCESS == kr);
1106 kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
1107 (vm_map_size_t)size, TRUE, &copy);
1108 assert(KERN_SUCCESS == kr);
1109
1110
1111 *pagesp = (default_pager_page_array_t)copy;
1112 *countp = actual;
1c79356b
A
1113 return KERN_SUCCESS;
1114}