MCPcopy Create free account
hub / github.com/danoon2/Boxedwine / mremap

Method mremap

source/kernel/kmemory.cpp:213–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

211}
212
213U32 KMemory::mremap(KThread* thread, U32 oldaddress, U32 oldsize, U32 newsize, U32 flags) {
214 if (flags > 1) {
215 kpanic_fmt("mremap not implemented: flags=%X", flags);
216 }
217 // is page aligned
218 if (oldaddress & 0xFFF) {
219 return -K_EINVAL;
220 }
221 if (newsize == 0) {
222 return -K_EINVAL;
223 }
224 if (oldsize == 0) {
225 kpanic("mremap not implemented for oldsize==0");
226 }
227 U32 oldPageCount = oldsize >> K_PAGE_SHIFT;
228 U32 oldPageStart = oldaddress >> K_PAGE_SHIFT;
229 U32 pageFlags = getPageFlags(oldPageStart);
230
231 for (U32 i = 0; i < oldPageCount; i++) {
232 if (getPageFlags(oldPageStart + i) != pageFlags) {
233 return -K_EFAULT;
234 }
235 }
236 if (newsize < oldsize) {
237 this->unmap(oldaddress + newsize, oldsize - newsize);
238 return oldaddress;
239 } else {
240 U32 prot = 0;
241 U32 f = 0;
242 if (pageFlags & PAGE_READ) {
243 prot |= K_PROT_READ;
244 }
245 if (pageFlags & PAGE_WRITE) {
246 prot |= K_PROT_WRITE;
247 }
248 if (pageFlags & PAGE_EXEC) {
249 prot |= K_PROT_EXEC;
250 }
251 if (pageFlags & PAGE_SHARED) {
252 f |= K_MAP_SHARED;
253 } else {
254 f |= K_MAP_PRIVATE;
255 }
256 U32 result = this->mmap(thread, oldaddress + oldsize, newsize - oldsize, prot, f | K_MAP_FIXED_NOREPLACE, -1, 0, true);
257 if (result == oldaddress + oldsize) {
258 return oldaddress;
259 }
260 if ((flags & 1) != 0) { // MREMAP_MAYMOVE
261 result = this->mmap(thread, 0, newsize, prot, f | K_MAP_ANONYMOUS, -1, 0);
262 this->memcpy(result, oldaddress, oldsize);
263 this->unmap(oldaddress, oldsize);
264 return result;
265 }
266 return -K_ENOMEM;
267 }
268}
269
270U32 KMemory::unmap(U32 address, U32 len) {

Callers 1

syscall_mremapFunction · 0.80

Calls 5

unmapMethod · 0.95
mmapMethod · 0.95
memcpyMethod · 0.95
kpanic_fmtFunction · 0.85
kpanicFunction · 0.85

Tested by

no test coverage detected