]> git.saurik.com Git - apple/xnu.git/blame - osfmk/default_pager/dp_memory_object.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / osfmk / default_pager / dp_memory_object.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 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 75/* forward declaration */
b0d623f7 76vstruct_t vs_object_create(dp_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(
b0d623f7 304 dp_size_t size)
1c79356b
A
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
0c530ab8
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,
593a1d5f
A
370 dp_memory_object_map,
371 dp_memory_object_last_unmap,
0c530ab8
A
372 "default pager"
373};
374
1c79356b
A
375kern_return_t
376dp_memory_object_init(
0b4e3aa0
A
377 memory_object_t mem_obj,
378 memory_object_control_t control,
b0d623f7 379 __unused memory_object_cluster_size_t pager_page_size)
1c79356b 380{
1c79356b 381 vstruct_t vs;
1c79356b
A
382
383 assert(pager_page_size == vm_page_size);
384
0b4e3aa0
A
385 memory_object_control_reference(control);
386
1c79356b 387 vs_lookup(mem_obj, vs);
0b4e3aa0 388 vs_lock(vs);
1c79356b 389
0b4e3aa0 390 if (vs->vs_control != MEMORY_OBJECT_CONTROL_NULL)
1c79356b
A
391 Panic("bad request");
392
0b4e3aa0 393 vs->vs_control = control;
1c79356b
A
394 vs_unlock(vs);
395
396 return KERN_SUCCESS;
397}
398
399kern_return_t
400dp_memory_object_synchronize(
0b4e3aa0
A
401 memory_object_t mem_obj,
402 memory_object_offset_t offset,
b0d623f7 403 memory_object_size_t length,
91447636 404 __unused vm_sync_t flags)
1c79356b 405{
1c79356b 406 vstruct_t vs;
1c79356b
A
407
408 vs_lookup(mem_obj, vs);
0b4e3aa0 409 vs_lock(vs);
1c79356b
A
410 vs_unlock(vs);
411
0b4e3aa0 412 memory_object_synchronize_completed(vs->vs_control, offset, length);
1c79356b
A
413
414 return KERN_SUCCESS;
415}
416
0b4e3aa0 417kern_return_t
593a1d5f
A
418dp_memory_object_map(
419 __unused memory_object_t mem_obj,
420 __unused vm_prot_t prot)
0b4e3aa0 421{
593a1d5f
A
422 panic("dp_memory_object_map");
423 return KERN_FAILURE;
424}
0b4e3aa0 425
593a1d5f
A
426kern_return_t
427dp_memory_object_last_unmap(
428 __unused memory_object_t mem_obj)
429{
430 panic("dp_memory_object_last_unmap");
0b4e3aa0
A
431 return KERN_FAILURE;
432}
433
1c79356b
A
434kern_return_t
435dp_memory_object_terminate(
0b4e3aa0 436 memory_object_t mem_obj)
1c79356b 437{
0b4e3aa0 438 memory_object_control_t control;
1c79356b 439 vstruct_t vs;
1c79356b
A
440
441 /*
442 * control port is a receive right, not a send right.
443 */
444
445 vs_lookup(mem_obj, vs);
0b4e3aa0 446 vs_lock(vs);
1c79356b
A
447
448 /*
449 * Wait for read and write requests to terminate.
450 */
451
452 vs_wait_for_readers(vs);
453 vs_wait_for_writers(vs);
454
455 /*
456 * After memory_object_terminate both memory_object_init
457 * and a no-senders notification are possible, so we need
0b4e3aa0
A
458 * to clean up our reference to the memory_object_control
459 * to prepare for a new init.
1c79356b
A
460 */
461
0b4e3aa0
A
462 control = vs->vs_control;
463 vs->vs_control = MEMORY_OBJECT_CONTROL_NULL;
1c79356b
A
464
465 /* a bit of special case ugliness here. Wakeup any waiting reads */
466 /* these data requests had to be removed from the seqno traffic */
467 /* based on a performance bottleneck with large memory objects */
468 /* the problem will right itself with the new component based */
469 /* synchronous interface. The new async will be able to return */
470 /* failure during its sync phase. In the mean time ... */
471
0b4e3aa0
A
472 thread_wakeup(&vs->vs_writers);
473 thread_wakeup(&vs->vs_async_pending);
1c79356b
A
474
475 vs_unlock(vs);
476
477 /*
0b4e3aa0 478 * Now we deallocate our reference on the control.
1c79356b 479 */
0b4e3aa0 480 memory_object_control_deallocate(control);
1c79356b
A
481 return KERN_SUCCESS;
482}
483
484void
0b4e3aa0
A
485dp_memory_object_reference(
486 memory_object_t mem_obj)
487{
488 vstruct_t vs;
489
490 vs_lookup_safe(mem_obj, vs);
491 if (vs == VSTRUCT_NULL)
492 return;
493
494 VS_LOCK(vs);
495 assert(vs->vs_references > 0);
496 vs->vs_references++;
497 VS_UNLOCK(vs);
498}
499
0b4e3aa0
A
500void
501dp_memory_object_deallocate(
502 memory_object_t mem_obj)
1c79356b
A
503{
504 vstruct_t vs;
0b4e3aa0 505 mach_port_seqno_t seqno;
1c79356b
A
506
507 /*
0b4e3aa0 508 * Because we don't give out multiple first references
1c79356b 509 * for a memory object, there can't be a race
0b4e3aa0
A
510 * between getting a deallocate call and creating
511 * a new reference for the object.
1c79356b
A
512 */
513
0b4e3aa0
A
514 vs_lookup_safe(mem_obj, vs);
515 if (vs == VSTRUCT_NULL)
516 return;
517
518 VS_LOCK(vs);
519 if (--vs->vs_references > 0) {
520 VS_UNLOCK(vs);
521 return;
522 }
523
524 seqno = vs->vs_next_seqno++;
525 while (vs->vs_seqno != seqno) {
526 default_pager_wait_seqno++;
527 vs->vs_waiting_seqno = TRUE;
528 assert_wait(&vs->vs_seqno, THREAD_UNINT);
529 VS_UNLOCK(vs);
9bccf70c 530 thread_block(THREAD_CONTINUE_NULL);
0b4e3aa0
A
531 VS_LOCK(vs);
532 }
533
1c79356b
A
534 vs_async_wait(vs); /* wait for pending async IO */
535
536 /* do not delete the vs structure until the referencing pointers */
537 /* in the vstruct list have been expunged */
538
539 /* get VSL_LOCK out of order by using TRY mechanism */
540 while(!VSL_LOCK_TRY()) {
541 VS_UNLOCK(vs);
542 VSL_LOCK();
543 VSL_UNLOCK();
544 VS_LOCK(vs);
545 vs_async_wait(vs); /* wait for pending async IO */
546 }
0b4e3aa0
A
547
548
1c79356b 549 /*
0b4e3aa0 550 * We shouldn't get a deallocation call
1c79356b
A
551 * when the kernel has the object cached.
552 */
0b4e3aa0 553 if (vs->vs_control != MEMORY_OBJECT_CONTROL_NULL)
1c79356b
A
554 Panic("bad request");
555
556 /*
557 * Unlock the pager (though there should be no one
558 * waiting for it).
559 */
560 VS_UNLOCK(vs);
561
0b4e3aa0
A
562 /* Lock out paging segment removal for the duration of this */
563 /* call. We are vulnerable to losing a paging segment we rely */
564 /* on as soon as we remove ourselves from the VSL and unlock */
565
566 /* Keep our thread from blocking on attempt to trigger backing */
567 /* store release */
568 backing_store_release_trigger_disable += 1;
569
1c79356b
A
570 /*
571 * Remove the memory object port association, and then
572 * the destroy the port itself. We must remove the object
573 * from the port list before deallocating the pager,
574 * because of default_pager_objects.
575 */
576 vstruct_list_delete(vs);
0b4e3aa0
A
577 VSL_UNLOCK();
578
1c79356b
A
579 ps_vstruct_dealloc(vs);
580
0b4e3aa0
A
581 VSL_LOCK();
582 backing_store_release_trigger_disable -= 1;
583 if(backing_store_release_trigger_disable == 0) {
9bccf70c 584 thread_wakeup((event_t)&backing_store_release_trigger_disable);
1c79356b
A
585 }
586 VSL_UNLOCK();
587}
588
589kern_return_t
590dp_memory_object_data_request(
0b4e3aa0
A
591 memory_object_t mem_obj,
592 memory_object_offset_t offset,
b0d623f7 593 memory_object_cluster_size_t length,
2d21ac55
A
594 __unused vm_prot_t protection_required,
595 memory_object_fault_info_t fault_info)
1c79356b 596{
1c79356b 597 vstruct_t vs;
b0d623f7 598 kern_return_t kr = KERN_SUCCESS;
1c79356b
A
599
600 GSTAT(global_stats.gs_pagein_calls++);
601
602
603 /* CDY at this moment vs_lookup panics when presented with the wrong */
604 /* port. As we are expanding this pager to support user interfaces */
605 /* this should be changed to return kern_failure */
606 vs_lookup(mem_obj, vs);
0b4e3aa0 607 vs_lock(vs);
1c79356b
A
608
609 /* We are going to relax the strict sequencing here for performance */
610 /* reasons. We can do this because we know that the read and */
611 /* write threads are different and we rely on synchronization */
612 /* of read and write requests at the cache memory_object level */
613 /* break out wait_for_writers, all of this goes away when */
614 /* we get real control of seqno with the new component interface */
0b4e3aa0 615
1c79356b
A
616 if (vs->vs_writers != 0) {
617 /* you can't hold on to the seqno and go */
618 /* to sleep like that */
619 vs_unlock(vs); /* bump internal count of seqno */
620 VS_LOCK(vs);
621 while (vs->vs_writers != 0) {
622 default_pager_wait_write++;
623 vs->vs_waiting_write = TRUE;
0b4e3aa0 624 assert_wait(&vs->vs_writers, THREAD_UNINT);
1c79356b 625 VS_UNLOCK(vs);
9bccf70c 626 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
627 VS_LOCK(vs);
628 vs_async_wait(vs);
629 }
0b4e3aa0 630 if(vs->vs_control == MEMORY_OBJECT_CONTROL_NULL) {
1c79356b
A
631 VS_UNLOCK(vs);
632 return KERN_FAILURE;
633 }
634 vs_start_read(vs);
635 VS_UNLOCK(vs);
636 } else {
637 vs_start_read(vs);
638 vs_unlock(vs);
639 }
640
641 /*
642 * Request must be on a page boundary and a multiple of pages.
643 */
644 if ((offset & vm_page_mask) != 0 || (length & vm_page_mask) != 0)
645 Panic("bad alignment");
646
b0d623f7
A
647 assert((dp_offset_t) offset == offset);
648 kr = pvs_cluster_read(vs, (dp_offset_t) offset, length, fault_info);
649
650 /* Regular data requests have a non-zero length and always return KERN_SUCCESS.
651 Their actual success is determined by the fact that they provide a page or not,
652 i.e whether we call upl_commit() or upl_abort(). A length of 0 means that the
653 caller is only asking if the pager has a copy of that page or not. The answer to
654 that question is provided by the return value. KERN_SUCCESS means that the pager
655 does have that page.
656 */
657 if(length) {
658 kr = KERN_SUCCESS;
659 }
660
1c79356b
A
661 vs_finish_read(vs);
662
b0d623f7 663 return kr;
1c79356b
A
664}
665
666/*
667 * memory_object_data_initialize: check whether we already have each page, and
668 * write it if we do not. The implementation is far from optimized, and
669 * also assumes that the default_pager is single-threaded.
670 */
671/* It is questionable whether or not a pager should decide what is relevant */
672/* and what is not in data sent from the kernel. Data initialize has been */
673/* changed to copy back all data sent to it in preparation for its eventual */
674/* merge with data return. It is the kernel that should decide what pages */
675/* to write back. As of the writing of this note, this is indeed the case */
676/* the kernel writes back one page at a time through this interface */
677
678kern_return_t
679dp_memory_object_data_initialize(
0b4e3aa0
A
680 memory_object_t mem_obj,
681 memory_object_offset_t offset,
b0d623f7 682 memory_object_cluster_size_t size)
1c79356b 683{
1c79356b 684 vstruct_t vs;
1c79356b 685
91447636
A
686 DP_DEBUG(DEBUG_MO_EXTERNAL,
687 ("mem_obj=0x%x,offset=0x%x,cnt=0x%x\n",
688 (int)mem_obj, (int)offset, (int)size));
55e303ae 689 GSTAT(global_stats.gs_pages_init += atop_32(size));
1c79356b
A
690
691 vs_lookup(mem_obj, vs);
0b4e3aa0 692 vs_lock(vs);
1c79356b
A
693 vs_start_write(vs);
694 vs_unlock(vs);
695
696 /*
697 * Write the data via clustered writes. vs_cluster_write will
698 * loop if the address range specified crosses cluster
699 * boundaries.
700 */
b0d623f7
A
701 assert((upl_offset_t) offset == offset);
702 vs_cluster_write(vs, 0, (upl_offset_t)offset, size, FALSE, 0);
1c79356b
A
703
704 vs_finish_write(vs);
705
706 return KERN_SUCCESS;
707}
708
1c79356b
A
709kern_return_t
710dp_memory_object_data_unlock(
91447636
A
711 __unused memory_object_t mem_obj,
712 __unused memory_object_offset_t offset,
b0d623f7 713 __unused memory_object_size_t size,
91447636 714 __unused vm_prot_t desired_access)
1c79356b 715{
0b4e3aa0 716 Panic("dp_memory_object_data_unlock: illegal");
1c79356b
A
717 return KERN_FAILURE;
718}
719
720
91447636 721/*ARGSUSED8*/
1c79356b
A
722kern_return_t
723dp_memory_object_data_return(
0b4e3aa0
A
724 memory_object_t mem_obj,
725 memory_object_offset_t offset,
b0d623f7 726 memory_object_cluster_size_t size,
91447636
A
727 __unused memory_object_offset_t *resid_offset,
728 __unused int *io_error,
729 __unused boolean_t dirty,
730 __unused boolean_t kernel_copy,
731 __unused int upl_flags)
1c79356b 732{
1c79356b 733 vstruct_t vs;
1c79356b 734
91447636
A
735 DP_DEBUG(DEBUG_MO_EXTERNAL,
736 ("mem_obj=0x%x,offset=0x%x,size=0x%x\n",
737 (int)mem_obj, (int)offset, (int)size));
1c79356b
A
738 GSTAT(global_stats.gs_pageout_calls++);
739
740 /* This routine is called by the pageout thread. The pageout thread */
741 /* cannot be blocked by read activities unless the read activities */
742 /* Therefore the grant of vs lock must be done on a try versus a */
743 /* blocking basis. The code below relies on the fact that the */
744 /* interface is synchronous. Should this interface be again async */
745 /* for some type of pager in the future the pages will have to be */
746 /* returned through a separate, asynchronous path. */
747
748 vs_lookup(mem_obj, vs);
749
750 default_pager_total++;
751 if(!VS_TRY_LOCK(vs)) {
752 /* the call below will not be done by caller when we have */
753 /* a synchronous interface */
754 /* return KERN_LOCK_OWNED; */
755 upl_t upl;
0c530ab8 756 unsigned int page_list_count = 0;
0b4e3aa0
A
757 memory_object_super_upl_request(vs->vs_control,
758 (memory_object_offset_t)offset,
759 size, size,
760 &upl, NULL, &page_list_count,
761 UPL_NOBLOCK | UPL_CLEAN_IN_PLACE
1c79356b 762 | UPL_NO_SYNC | UPL_COPYOUT_FROM);
0b4e3aa0
A
763 upl_abort(upl,0);
764 upl_deallocate(upl);
1c79356b
A
765 return KERN_SUCCESS;
766 }
767
d12e1678
A
768 if ((vs->vs_seqno != vs->vs_next_seqno++)
769 || (vs->vs_readers)
770 || (vs->vs_xfer_pending)) {
0c530ab8
A
771 upl_t upl;
772 unsigned int page_list_count = 0;
0b4e3aa0 773
1c79356b
A
774 vs->vs_next_seqno--;
775 VS_UNLOCK(vs);
0b4e3aa0 776
1c79356b
A
777 /* the call below will not be done by caller when we have */
778 /* a synchronous interface */
779 /* return KERN_LOCK_OWNED; */
0b4e3aa0
A
780 memory_object_super_upl_request(vs->vs_control,
781 (memory_object_offset_t)offset,
782 size, size,
783 &upl, NULL, &page_list_count,
1c79356b
A
784 UPL_NOBLOCK | UPL_CLEAN_IN_PLACE
785 | UPL_NO_SYNC | UPL_COPYOUT_FROM);
0b4e3aa0
A
786 upl_abort(upl,0);
787 upl_deallocate(upl);
1c79356b
A
788 return KERN_SUCCESS;
789 }
790
0b4e3aa0 791 if ((size % vm_page_size) != 0)
1c79356b
A
792 Panic("bad alignment");
793
794 vs_start_write(vs);
795
796
797 vs->vs_async_pending += 1; /* protect from backing store contraction */
0b4e3aa0 798 vs_unlock(vs);
1c79356b
A
799
800 /*
801 * Write the data via clustered writes. vs_cluster_write will
802 * loop if the address range specified crosses cluster
803 * boundaries.
804 */
b0d623f7
A
805 assert((upl_offset_t) offset == offset);
806 vs_cluster_write(vs, 0, (upl_offset_t) offset, size, FALSE, 0);
1c79356b
A
807
808 vs_finish_write(vs);
809
810 /* temporary, need a finer lock based on cluster */
811
812 VS_LOCK(vs);
813 vs->vs_async_pending -= 1; /* release vs_async_wait */
0b4e3aa0
A
814 if (vs->vs_async_pending == 0 && vs->vs_waiting_async) {
815 vs->vs_waiting_async = FALSE;
1c79356b 816 VS_UNLOCK(vs);
0b4e3aa0 817 thread_wakeup(&vs->vs_async_pending);
1c79356b
A
818 } else {
819 VS_UNLOCK(vs);
820 }
821
822
823 return KERN_SUCCESS;
824}
825
0b4e3aa0
A
826/*
827 * Routine: default_pager_memory_object_create
828 * Purpose:
829 * Handle requests for memory objects from the
830 * kernel.
831 * Notes:
832 * Because we only give out the default memory
833 * manager port to the kernel, we don't have to
834 * be so paranoid about the contents.
835 */
1c79356b 836kern_return_t
0b4e3aa0 837default_pager_memory_object_create(
91447636 838 __unused memory_object_default_t dmm,
0b4e3aa0
A
839 vm_size_t new_size,
840 memory_object_t *new_mem_obj)
1c79356b 841{
0b4e3aa0 842 vstruct_t vs;
1c79356b 843
0b4e3aa0
A
844 assert(dmm == default_pager_object);
845
b0d623f7
A
846 if ((dp_size_t) new_size != new_size) {
847 /* 32-bit overflow */
848 return KERN_INVALID_ARGUMENT;
849 }
850
851 vs = vs_object_create((dp_size_t) new_size);
0b4e3aa0
A
852 if (vs == VSTRUCT_NULL)
853 return KERN_RESOURCE_SHORTAGE;
854
855 vs->vs_next_seqno = 0;
856
857 /*
858 * Set up associations between this memory object
859 * and this default_pager structure
860 */
861
0c530ab8 862 vs->vs_pager_ops = &default_pager_ops;
b0d623f7 863 vs->vs_pager_header.io_bits = IKOT_MEMORY_OBJECT;
0b4e3aa0
A
864
865 /*
866 * After this, other threads might receive requests
867 * for this memory object or find it in the port list.
868 */
869
870 vstruct_list_insert(vs);
871 *new_mem_obj = vs_to_mem_obj(vs);
872 return KERN_SUCCESS;
1c79356b
A
873}
874
875/*
876 * Create an external object.
877 */
878kern_return_t
879default_pager_object_create(
91447636 880 default_pager_t default_pager,
0b4e3aa0
A
881 vm_size_t size,
882 memory_object_t *mem_objp)
1c79356b
A
883{
884 vstruct_t vs;
1c79356b 885
91447636 886 if (default_pager != default_pager_object)
1c79356b
A
887 return KERN_INVALID_ARGUMENT;
888
b0d623f7
A
889 if ((dp_size_t) size != size) {
890 /* 32-bit overflow */
891 return KERN_INVALID_ARGUMENT;
892 }
893
894 vs = vs_object_create((dp_size_t) size);
0b4e3aa0
A
895 if (vs == VSTRUCT_NULL)
896 return KERN_RESOURCE_SHORTAGE;
1c79356b 897
1c79356b 898 /*
0b4e3aa0 899 * Set up associations between the default pager
1c79356b
A
900 * and this vstruct structure
901 */
0c530ab8 902 vs->vs_pager_ops = &default_pager_ops;
1c79356b 903 vstruct_list_insert(vs);
0b4e3aa0 904 *mem_objp = vs_to_mem_obj(vs);
1c79356b
A
905 return KERN_SUCCESS;
906}
907
908kern_return_t
909default_pager_objects(
91447636 910 default_pager_t default_pager,
1c79356b
A
911 default_pager_object_array_t *objectsp,
912 mach_msg_type_number_t *ocountp,
91447636 913 mach_port_array_t *portsp,
1c79356b
A
914 mach_msg_type_number_t *pcountp)
915{
916 vm_offset_t oaddr = 0; /* memory for objects */
917 vm_size_t osize = 0; /* current size */
918 default_pager_object_t * objects;
91447636 919 unsigned int opotential = 0;
1c79356b 920
91447636 921 vm_map_copy_t pcopy = 0; /* copy handle for pagers */
1c79356b 922 vm_size_t psize = 0; /* current size */
0b4e3aa0 923 memory_object_t * pagers;
91447636 924 unsigned int ppotential = 0;
1c79356b
A
925
926 unsigned int actual;
927 unsigned int num_objects;
928 kern_return_t kr;
929 vstruct_t entry;
1c79356b 930
91447636
A
931 if (default_pager != default_pager_object)
932 return KERN_INVALID_ARGUMENT;
1c79356b
A
933
934 /*
935 * We will send no more than this many
936 */
937 actual = vstruct_list.vsl_count;
1c79356b 938
91447636
A
939 /*
940 * Out out-of-line port arrays are simply kalloc'ed.
941 */
b0d623f7
A
942 psize = round_page(actual * sizeof (*pagers));
943 ppotential = (unsigned int) (psize / sizeof (*pagers));
91447636
A
944 pagers = (memory_object_t *)kalloc(psize);
945 if (0 == pagers)
946 return KERN_RESOURCE_SHORTAGE;
947
948 /*
949 * returned out of line data must be allocated out
950 * the ipc_kernel_map, wired down, filled in, and
951 * then "copied in" as if it had been sent by a
952 * user process.
953 */
b0d623f7
A
954 osize = round_page(actual * sizeof (*objects));
955 opotential = (unsigned int) (osize / sizeof (*objects));
91447636
A
956 kr = kmem_alloc(ipc_kernel_map, &oaddr, osize);
957 if (KERN_SUCCESS != kr) {
958 kfree(pagers, psize);
959 return KERN_RESOURCE_SHORTAGE;
1c79356b 960 }
91447636 961 objects = (default_pager_object_t *)oaddr;
1c79356b 962
1c79356b
A
963
964 /*
965 * Now scan the list.
966 */
967
968 VSL_LOCK();
969
970 num_objects = 0;
971 queue_iterate(&vstruct_list.vsl_queue, entry, vstruct_t, vs_links) {
972
91447636
A
973 memory_object_t pager;
974 vm_size_t size;
1c79356b
A
975
976 if ((num_objects >= opotential) ||
977 (num_objects >= ppotential)) {
978
979 /*
980 * This should be rare. In any case,
981 * we will only miss recent objects,
982 * because they are added at the end.
983 */
984 break;
985 }
986
987 /*
988 * Avoid interfering with normal operations
989 */
990 if (!VS_MAP_TRY_LOCK(entry))
991 goto not_this_one;
992 size = ps_vstruct_allocated_size(entry);
993 VS_MAP_UNLOCK(entry);
994
995 VS_LOCK(entry);
996
1c79356b 997 /*
0b4e3aa0
A
998 * We need a reference for our caller. Adding this
999 * reference through the linked list could race with
1000 * destruction of the object. If we find the object
1001 * has no references, just give up on it.
1c79356b 1002 */
0b4e3aa0
A
1003 VS_LOCK(entry);
1004 if (entry->vs_references == 0) {
1c79356b 1005 VS_UNLOCK(entry);
0b4e3aa0 1006 goto not_this_one;
1c79356b 1007 }
91447636
A
1008 pager = vs_to_mem_obj(entry);
1009 dp_memory_object_reference(pager);
1c79356b
A
1010 VS_UNLOCK(entry);
1011
1012 /* the arrays are wired, so no deadlock worries */
1013
1014 objects[num_objects].dpo_object = (vm_offset_t) entry;
1015 objects[num_objects].dpo_size = size;
0b4e3aa0 1016 pagers [num_objects++] = pager;
1c79356b
A
1017 continue;
1018
1019 not_this_one:
1020 /*
1021 * Do not return garbage
1022 */
1023 objects[num_objects].dpo_object = (vm_offset_t) 0;
1024 objects[num_objects].dpo_size = 0;
0b4e3aa0 1025 pagers[num_objects++] = MEMORY_OBJECT_NULL;
1c79356b
A
1026
1027 }
1028
1029 VSL_UNLOCK();
1030
91447636
A
1031 /* clear out any excess allocation */
1032 while (num_objects < opotential) {
1033 objects[--opotential].dpo_object = (vm_offset_t) 0;
1034 objects[opotential].dpo_size = 0;
1c79356b 1035 }
91447636
A
1036 while (num_objects < ppotential) {
1037 pagers[--ppotential] = MEMORY_OBJECT_NULL;
1c79356b
A
1038 }
1039
91447636
A
1040 kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(oaddr),
1041 vm_map_round_page(oaddr + osize), FALSE);
1042 assert(KERN_SUCCESS == kr);
1043 kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)oaddr,
1044 (vm_map_size_t)osize, TRUE, &pcopy);
1045 assert(KERN_SUCCESS == kr);
1c79356b 1046
91447636
A
1047 *objectsp = (default_pager_object_array_t)objects;
1048 *ocountp = num_objects;
1049 *portsp = (mach_port_array_t)pcopy;
1050 *pcountp = num_objects;
1c79356b 1051
91447636 1052 return KERN_SUCCESS;
1c79356b
A
1053}
1054
1055kern_return_t
1056default_pager_object_pages(
91447636
A
1057 default_pager_t default_pager,
1058 mach_port_t memory_object,
1c79356b
A
1059 default_pager_page_array_t *pagesp,
1060 mach_msg_type_number_t *countp)
1061{
91447636 1062 vm_offset_t addr = 0; /* memory for page offsets */
1c79356b 1063 vm_size_t size = 0; /* current memory size */
91447636
A
1064 vm_map_copy_t copy;
1065 default_pager_page_t * pages = 0;
1066 unsigned int potential;
1067 unsigned int actual;
1c79356b 1068 kern_return_t kr;
91447636 1069 memory_object_t object;
1c79356b 1070
91447636 1071 if (default_pager != default_pager_object)
1c79356b 1072 return KERN_INVALID_ARGUMENT;
0b4e3aa0 1073
91447636 1074 object = (memory_object_t) memory_object;
1c79356b 1075
91447636 1076 potential = 0;
1c79356b
A
1077 for (;;) {
1078 vstruct_t entry;
1079
1080 VSL_LOCK();
1081 queue_iterate(&vstruct_list.vsl_queue, entry, vstruct_t,
1082 vs_links) {
1083 VS_LOCK(entry);
0b4e3aa0 1084 if (vs_to_mem_obj(entry) == object) {
1c79356b
A
1085 VSL_UNLOCK();
1086 goto found_object;
1087 }
1088 VS_UNLOCK(entry);
1089 }
1090 VSL_UNLOCK();
1091
1092 /* did not find the object */
91447636
A
1093 if (0 != addr)
1094 kmem_free(ipc_kernel_map, addr, size);
1c79356b 1095
1c79356b
A
1096 return KERN_INVALID_ARGUMENT;
1097
1098 found_object:
1099
1100 if (!VS_MAP_TRY_LOCK(entry)) {
1101 /* oh well bad luck */
9bccf70c 1102 int wresult;
1c79356b
A
1103
1104 VS_UNLOCK(entry);
1105
91447636 1106 assert_wait_timeout((event_t)assert_wait_timeout, THREAD_UNINT, 1, 1000*NSEC_PER_USEC);
9bccf70c
A
1107 wresult = thread_block(THREAD_CONTINUE_NULL);
1108 assert(wresult == THREAD_TIMED_OUT);
1c79356b
A
1109 continue;
1110 }
1111
1112 actual = ps_vstruct_allocated_pages(entry, pages, potential);
1113 VS_MAP_UNLOCK(entry);
1114 VS_UNLOCK(entry);
1115
1116 if (actual <= potential)
1117 break;
1118
1119 /* allocate more memory */
91447636
A
1120 if (0 != addr)
1121 kmem_free(ipc_kernel_map, addr, size);
1122
b0d623f7 1123 size = round_page(actual * sizeof (*pages));
91447636
A
1124 kr = kmem_alloc(ipc_kernel_map, &addr, size);
1125 if (KERN_SUCCESS != kr)
1126 return KERN_RESOURCE_SHORTAGE;
1c79356b 1127
1c79356b 1128 pages = (default_pager_page_t *)addr;
b0d623f7 1129 potential = (unsigned int) (size / sizeof (*pages));
1c79356b
A
1130 }
1131
1132 /*
91447636 1133 * Clear unused memory.
1c79356b 1134 */
91447636
A
1135 while (actual < potential)
1136 pages[--potential].dpp_offset = 0;
1137
1138 kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
1139 vm_map_round_page(addr + size), FALSE);
1140 assert(KERN_SUCCESS == kr);
1141 kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
1142 (vm_map_size_t)size, TRUE, &copy);
1143 assert(KERN_SUCCESS == kr);
1144
1145
1146 *pagesp = (default_pager_page_array_t)copy;
1147 *countp = actual;
1c79356b
A
1148 return KERN_SUCCESS;
1149}