FreeBSD/Linux Kernel Cross Reference
sys/uvm/uvm_mremap.c
1 /* $NetBSD: uvm_mremap.c,v 1.3 2006/01/31 14:03:53 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2006 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: uvm_mremap.c,v 1.3 2006/01/31 14:03:53 yamt Exp $");
31
32 #include <sys/param.h>
33
34 #include <uvm/uvm.h>
35
36 static int
37 uvm_mapent_extend(struct vm_map *map, vaddr_t endva, vsize_t size)
38 {
39 struct vm_map_entry *entry;
40 struct vm_map_entry *reserved_entry;
41 struct uvm_object *uobj;
42 int error = 0;
43
44 vm_map_lock(map);
45 if (!uvm_map_lookup_entry(map, endva, &reserved_entry)) {
46 error = ENOENT;
47 goto done;
48 }
49 if (reserved_entry->start != endva ||
50 reserved_entry->end != endva + size ||
51 reserved_entry->object.uvm_obj != NULL ||
52 reserved_entry->aref.ar_amap != NULL) {
53 error = EINVAL;
54 goto done;
55 }
56 entry = reserved_entry->prev;
57 KASSERT(&map->header != entry);
58 if (entry->end != endva) {
59 error = EINVAL;
60 goto done;
61 }
62
63 /*
64 * now, make reserved_entry compatible with entry, and then
65 * try to merge.
66 */
67
68 uobj = entry->object.uvm_obj;
69 if (uobj) {
70 voff_t offset = entry->offset;
71 voff_t newoffset;
72
73 newoffset = offset + entry->end - entry->start;
74 if (newoffset <= offset) {
75 error = E2BIG; /* XXX */
76 goto done;
77 }
78 simple_lock(&uobj->vmobjlock);
79 KASSERT(uobj->uo_refs > 0);
80 uobj->uo_refs++;
81 simple_unlock(&uobj->vmobjlock);
82 reserved_entry->object.uvm_obj = uobj;
83 reserved_entry->offset = newoffset;
84 }
85 reserved_entry->etype = entry->etype;
86 if (UVM_ET_ISCOPYONWRITE(entry)) {
87 reserved_entry->etype |= UVM_ET_NEEDSCOPY;
88 }
89 reserved_entry->flags &= ~UVM_MAP_NOMERGE;
90 reserved_entry->protection = entry->protection;
91 reserved_entry->max_protection = entry->max_protection;
92 reserved_entry->inheritance = entry->inheritance;
93 reserved_entry->advice = entry->advice;
94 reserved_entry->wired_count = 0; /* XXX should inherit? */
95 uvm_mapent_trymerge(map, reserved_entry, 0);
96 done:
97 vm_map_unlock(map);
98
99 return error;
100 }
101
102 /*
103 * uvm_mremap: move and/or resize existing mappings.
104 */
105
106 int
107 uvm_mremap(struct vm_map *oldmap, vaddr_t oldva, vsize_t oldsize,
108 struct vm_map *newmap, vaddr_t *newvap, vsize_t newsize,
109 struct proc *newproc, int flags)
110 {
111 vaddr_t dstva;
112 vsize_t movesize;
113 vaddr_t newva;
114 int error = 0;
115 const boolean_t fixed = (flags & UVM_MREMAP_FIXED) != 0;
116
117 if (fixed) {
118 newva = *newvap;
119 } else {
120 newva = 0;
121 }
122 if ((oldva & PAGE_MASK) != 0 ||
123 (newva & PAGE_MASK) != 0 ||
124 (oldsize & PAGE_MASK) != 0 ||
125 (newsize & PAGE_MASK) != 0) {
126 return EINVAL;
127 }
128 /* XXX zero-size should be allowed? */
129 if (oldva + oldsize <= oldva || newva + newsize <= newva) {
130 return EINVAL;
131 }
132
133 /*
134 * check the easy cases first.
135 */
136
137 if ((!fixed || newva == oldva) && newmap == oldmap) {
138 vaddr_t va;
139
140 if (newsize == oldsize) {
141 newva = oldva;
142 goto done;
143 }
144 if (newsize < oldsize) {
145 uvm_unmap(oldmap, oldva + newsize, oldva + oldsize);
146 newva = oldva;
147 goto done;
148 }
149 va = oldva + oldsize;
150 if (uvm_map_reserve(oldmap, newsize - oldsize, 0, 0, &va,
151 UVM_FLAG_FIXED)) {
152 newva = oldva;
153 goto extend;
154 }
155 if (fixed) {
156 return ENOMEM;
157 }
158 }
159
160 /*
161 * we need to move mappings.
162 */
163
164 if (!fixed) {
165 KASSERT(&newproc->p_vmspace->vm_map == newmap);
166 newva = newproc->p_emul->e_vm_default_addr(newproc,
167 (vaddr_t)newproc->p_vmspace->vm_daddr, newsize);
168 }
169 dstva = newva;
170 if (!uvm_map_reserve(newmap, newsize, oldva, 0, &dstva,
171 fixed ? UVM_FLAG_FIXED : 0)) {
172 return ENOMEM;
173 }
174 KASSERT(!fixed || dstva == newva);
175 newva = dstva;
176 movesize = MIN(oldsize, newsize);
177 error = uvm_map_extract(oldmap, oldva, movesize, newmap, &dstva,
178 UVM_EXTRACT_RESERVED);
179 KASSERT(dstva == newva);
180 if (error != 0) {
181 /* undo uvm_map_reserve */
182 uvm_unmap(newmap, newva, newva + newsize);
183 return error;
184 }
185 if (newsize > oldsize) {
186 extend:
187 error = uvm_mapent_extend(newmap, newva + oldsize,
188 newsize - oldsize);
189 if (error != 0) {
190 /* undo uvm_map_reserve and uvm_map_extract */
191 uvm_unmap(newmap, newva, newva + newsize);
192 return error;
193 }
194 }
195
196 /*
197 * now we won't fail. remove original entries.
198 */
199
200 if (oldva != newva || oldmap != newmap) {
201 uvm_unmap(oldmap, oldva, oldva + oldsize);
202 }
203 done:
204 *newvap = newva;
205 return 0;
206 }
Cache object: eae0490addee6dc34b7680d5eaefea0c
|