예제 #1
0
Kernel::Result Kernel::loadBootProcess(BootImage *image, Address imagePAddr, Size index)
{
    Address imageVAddr = (Address) image, args;
    Size args_size = ARGV_SIZE;
    BootSymbol *program;
    BootSegment *segment;
    Process *proc;
    char *vaddr;
    Arch::MemoryMap map;

    // Point to the program and segments table
    program = &((BootSymbol *) (imageVAddr + image->symbolTableOffset))[index];
    segment = &((BootSegment *) (imageVAddr + image->segmentsTableOffset))[program->segmentsOffset];

    // Ignore non-BootProgram entries
    if (program->type != BootProgram)
        return InvalidBootImage;

    // Create process
    proc = m_procs->create(program->entry, map);
    if (!proc)
    {
        FATAL("failed to create boot program: " << program->name);
        return ProcessError;
    }

    proc->setState(Process::Ready);

    // Obtain process memory
    MemoryContext *mem = proc->getMemoryContext();

    // Map program segment into it's virtual memory
    for (Size i = 0; i < program->segmentsCount; i++)
    {
        for (Size j = 0; j < segment[i].size; j += PAGESIZE)
        {
            mem->map(segment[i].virtualAddress + j,
                     imagePAddr + segment[i].offset + j,
                     Memory::User     |
                     Memory::Readable |
                     Memory::Writable |
                     Memory::Executable);
        }
    }
    
    // Map program arguments into the process
    // TODO: move into the high memory???
    m_alloc->allocateLow(args_size, &args);
    mem->map(ARGV_ADDR, args, Memory::User | Memory::Readable | Memory::Writable);

    // Copy program arguments
    vaddr = (char *) m_alloc->toVirtual(args);
    MemoryBlock::set(vaddr, 0, PAGESIZE);
    MemoryBlock::copy(vaddr, program->name, ARGV_SIZE);

    // Done
    NOTICE("loaded: " << program->name);
    return Success;
}
예제 #2
0
파일: IO.cpp 프로젝트: 0xbda2d2f8/FreeNOS
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;
}
예제 #3
0
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;
}