IO::Result IO::map(Address phys, Size size, Memory::Access access) { m_range.virt = 0; m_range.phys = phys; m_range.access = access; m_range.size = size; if (!isKernel) { if (VMCtl(SELF, Map, &m_range) != API::Success) return MapFailure; } else { m_range.access &= ~Memory::User; MemoryContext *ctx = MemoryContext::getCurrent(); if (!ctx) return MapFailure; if (ctx->findFree(size, MemoryMap::KernelPrivate, &m_range.virt) != MemoryContext::Success) return OutOfMemory; if (ctx->map(m_range.virt, phys, m_range.access) != MemoryContext::Success) return MapFailure; } m_base = m_range.virt; return Success; }
Error VMCopyHandler(ProcessID procID, API::Operation how, Address ours, Address theirs, Size sz) { ProcessManager *procs = Kernel::instance->getProcessManager(); Process *proc; Address paddr, vaddr; Size bytes = 0, pageOff, total = 0; // Find the corresponding Process if (procID == SELF) proc = procs->current(); else if (!(proc = procs->get(procID))) return API::NotFound; // TODO: Verify memory addresses MemoryContext *local = procs->current()->getMemoryContext(); MemoryContext *remote = proc->getMemoryContext(); // Keep on going until all memory is processed while (total < sz) { /* Update variables. */ if (how == API::ReadPhys) paddr = theirs & PAGEMASK; else if (remote->lookup(theirs, &paddr) != MemoryContext::Success) return API::AccessViolation; pageOff = theirs & ~PAGEMASK; bytes = (PAGESIZE - pageOff) < (sz - total) ? (PAGESIZE - pageOff) : (sz - total); /* Valid address? */ if (!paddr) break; // Map their address into our local address space if (local->findFree(PAGESIZE, MemoryMap::KernelPrivate, &vaddr) != MemoryContext::Success) return API::RangeError; local->map(vaddr, paddr, Memory::Readable | Memory::Writable); /* Process the action appropriately. */ switch (how) { case API::Read: case API::ReadPhys: MemoryBlock::copy((void *)ours, (void *)(vaddr + pageOff), bytes); break; case API::Write: MemoryBlock::copy((void *)(vaddr + pageOff), (void *)ours, bytes); break; default: ; } // Unmap local->unmap(vaddr); // Update counters ours += bytes; theirs += bytes; total += bytes; } return total; }