1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions * Copyright (c) 2009 Apple Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions *
4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions *
6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions *
15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions *
18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions * limitations under the License.
25*0f4c859eSApple OSS Distributions *
26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions */
28*0f4c859eSApple OSS Distributions
29*0f4c859eSApple OSS Distributions #include <vm/vm_map.h>
30*0f4c859eSApple OSS Distributions
31*0f4c859eSApple OSS Distributions bool
first_free_is_valid_ll(vm_map_t map)32*0f4c859eSApple OSS Distributions first_free_is_valid_ll(vm_map_t map)
33*0f4c859eSApple OSS Distributions {
34*0f4c859eSApple OSS Distributions vm_map_offset_t map_page_mask = VM_MAP_PAGE_MASK(map);
35*0f4c859eSApple OSS Distributions vm_map_entry_t entry, next;
36*0f4c859eSApple OSS Distributions
37*0f4c859eSApple OSS Distributions entry = vm_map_to_entry(map);
38*0f4c859eSApple OSS Distributions next = entry->vme_next;
39*0f4c859eSApple OSS Distributions while (vm_map_trunc_page(next->vme_start, map_page_mask) ==
40*0f4c859eSApple OSS Distributions vm_map_trunc_page(entry->vme_end, map_page_mask) ||
41*0f4c859eSApple OSS Distributions (vm_map_trunc_page(next->vme_start, map_page_mask) ==
42*0f4c859eSApple OSS Distributions vm_map_trunc_page(entry->vme_start, map_page_mask) &&
43*0f4c859eSApple OSS Distributions next != vm_map_to_entry(map))) {
44*0f4c859eSApple OSS Distributions entry = next;
45*0f4c859eSApple OSS Distributions next = entry->vme_next;
46*0f4c859eSApple OSS Distributions if (entry == vm_map_to_entry(map)) {
47*0f4c859eSApple OSS Distributions break;
48*0f4c859eSApple OSS Distributions }
49*0f4c859eSApple OSS Distributions }
50*0f4c859eSApple OSS Distributions if (map->first_free != entry) {
51*0f4c859eSApple OSS Distributions printf("Bad first_free for map %p: %p should be %p\n",
52*0f4c859eSApple OSS Distributions map, map->first_free, entry);
53*0f4c859eSApple OSS Distributions return FALSE;
54*0f4c859eSApple OSS Distributions }
55*0f4c859eSApple OSS Distributions return TRUE;
56*0f4c859eSApple OSS Distributions }
57*0f4c859eSApple OSS Distributions
58*0f4c859eSApple OSS Distributions void
vm_map_store_init_ll(struct vm_map_header * hdr)59*0f4c859eSApple OSS Distributions vm_map_store_init_ll(struct vm_map_header *hdr)
60*0f4c859eSApple OSS Distributions {
61*0f4c859eSApple OSS Distributions hdr->links.next = hdr->links.prev = CAST_TO_VM_MAP_ENTRY(hdr);
62*0f4c859eSApple OSS Distributions }
63*0f4c859eSApple OSS Distributions
64*0f4c859eSApple OSS Distributions void
vm_map_store_entry_link_ll(struct vm_map_header * hdr,vm_map_entry_t after_where,vm_map_entry_t entry)65*0f4c859eSApple OSS Distributions vm_map_store_entry_link_ll(
66*0f4c859eSApple OSS Distributions struct vm_map_header *hdr,
67*0f4c859eSApple OSS Distributions vm_map_entry_t after_where,
68*0f4c859eSApple OSS Distributions vm_map_entry_t entry)
69*0f4c859eSApple OSS Distributions {
70*0f4c859eSApple OSS Distributions if (entry->map_aligned) {
71*0f4c859eSApple OSS Distributions assert(VM_MAP_PAGE_ALIGNED(entry->vme_start,
72*0f4c859eSApple OSS Distributions VM_MAP_HDR_PAGE_MASK(hdr)));
73*0f4c859eSApple OSS Distributions assert(VM_MAP_PAGE_ALIGNED(entry->vme_end,
74*0f4c859eSApple OSS Distributions VM_MAP_HDR_PAGE_MASK(hdr)));
75*0f4c859eSApple OSS Distributions }
76*0f4c859eSApple OSS Distributions hdr->nentries++;
77*0f4c859eSApple OSS Distributions entry->vme_prev = after_where;
78*0f4c859eSApple OSS Distributions entry->vme_next = after_where->vme_next;
79*0f4c859eSApple OSS Distributions entry->vme_prev->vme_next = entry->vme_next->vme_prev = entry;
80*0f4c859eSApple OSS Distributions }
81*0f4c859eSApple OSS Distributions
82*0f4c859eSApple OSS Distributions void
vm_map_store_entry_unlink_ll(struct vm_map_header * hdr,vm_map_entry_t entry)83*0f4c859eSApple OSS Distributions vm_map_store_entry_unlink_ll(struct vm_map_header *hdr, vm_map_entry_t entry)
84*0f4c859eSApple OSS Distributions {
85*0f4c859eSApple OSS Distributions hdr->nentries--;
86*0f4c859eSApple OSS Distributions entry->vme_next->vme_prev = entry->vme_prev;
87*0f4c859eSApple OSS Distributions entry->vme_prev->vme_next = entry->vme_next;
88*0f4c859eSApple OSS Distributions }
89*0f4c859eSApple OSS Distributions
90*0f4c859eSApple OSS Distributions void
vm_map_store_copy_reset_ll(vm_map_copy_t copy,__unused vm_map_entry_t entry,__unused int nentries)91*0f4c859eSApple OSS Distributions vm_map_store_copy_reset_ll(
92*0f4c859eSApple OSS Distributions vm_map_copy_t copy,
93*0f4c859eSApple OSS Distributions __unused vm_map_entry_t entry,
94*0f4c859eSApple OSS Distributions __unused int nentries)
95*0f4c859eSApple OSS Distributions {
96*0f4c859eSApple OSS Distributions copy->cpy_hdr.nentries = 0;
97*0f4c859eSApple OSS Distributions vm_map_copy_first_entry(copy) =
98*0f4c859eSApple OSS Distributions vm_map_copy_last_entry(copy) =
99*0f4c859eSApple OSS Distributions vm_map_copy_to_entry(copy);
100*0f4c859eSApple OSS Distributions }
101*0f4c859eSApple OSS Distributions
102*0f4c859eSApple OSS Distributions /*
103*0f4c859eSApple OSS Distributions * UPDATE_FIRST_FREE:
104*0f4c859eSApple OSS Distributions *
105*0f4c859eSApple OSS Distributions * Updates the map->first_free pointer to the
106*0f4c859eSApple OSS Distributions * entry immediately before the first hole in the map.
107*0f4c859eSApple OSS Distributions * The map should be locked.
108*0f4c859eSApple OSS Distributions */
109*0f4c859eSApple OSS Distributions void
update_first_free_ll(vm_map_t map,vm_map_entry_t new_first_free)110*0f4c859eSApple OSS Distributions update_first_free_ll(vm_map_t map, vm_map_entry_t new_first_free)
111*0f4c859eSApple OSS Distributions {
112*0f4c859eSApple OSS Distributions vm_map_offset_t map_page_mask = VM_MAP_PAGE_MASK(map);
113*0f4c859eSApple OSS Distributions vm_map_entry_t next;
114*0f4c859eSApple OSS Distributions
115*0f4c859eSApple OSS Distributions if (map->holelistenabled || map->disable_vmentry_reuse) {
116*0f4c859eSApple OSS Distributions return;
117*0f4c859eSApple OSS Distributions }
118*0f4c859eSApple OSS Distributions
119*0f4c859eSApple OSS Distributions next = new_first_free->vme_next;
120*0f4c859eSApple OSS Distributions while (vm_map_trunc_page(next->vme_start, map_page_mask) ==
121*0f4c859eSApple OSS Distributions vm_map_trunc_page(new_first_free->vme_end, map_page_mask) ||
122*0f4c859eSApple OSS Distributions (vm_map_trunc_page(next->vme_start, map_page_mask) ==
123*0f4c859eSApple OSS Distributions vm_map_trunc_page(new_first_free->vme_start, map_page_mask) &&
124*0f4c859eSApple OSS Distributions next != vm_map_to_entry(map))) {
125*0f4c859eSApple OSS Distributions new_first_free = next;
126*0f4c859eSApple OSS Distributions next = new_first_free->vme_next;
127*0f4c859eSApple OSS Distributions if (new_first_free == vm_map_to_entry(map)) {
128*0f4c859eSApple OSS Distributions break;
129*0f4c859eSApple OSS Distributions }
130*0f4c859eSApple OSS Distributions }
131*0f4c859eSApple OSS Distributions
132*0f4c859eSApple OSS Distributions map->first_free = new_first_free;
133*0f4c859eSApple OSS Distributions assert(first_free_is_valid(map));
134*0f4c859eSApple OSS Distributions }
135