Changeset 792807f in mainline for kernel/generic/src


Ignore:
Timestamp:
2011-12-16T21:16:23Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8708be3b, 9fe4db3
Parents:
9916841 (diff), c4be33a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

the Intel E1000 is now working and usable on ia32
(other platforms untested so far)

Location:
kernel/generic/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/ddi/ddi.c

    r9916841 r792807f  
    4545#include <mm/frame.h>
    4646#include <mm/as.h>
     47#include <mm/page.h>
    4748#include <synch/mutex.h>
    4849#include <syscall/copy.h>
     
    5253#include <errno.h>
    5354#include <trace.h>
     55#include <bitops.h>
    5456
    5557/** This lock protects the parea_btree. */
     
    8789/** Map piece of physical memory into virtual address space of current task.
    8890 *
    89  * @param p  Physical address of the starting frame.
    90  * @param v  Virtual address of the starting page.
     91 * @param phys  Physical address of the starting frame.
     92 * @param virt  Virtual address of the starting page.
    9193 * @param pages Number of pages to map.
    9294 * @param flags Address space area flags for the mapping.
    9395 *
    94  * @return 0 on success, EPERM if the caller lacks capabilities to use this
    95  *         syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there
    96  *         is no task matching the specified ID or the physical address space
    97  *         is not enabled for mapping and ENOMEM if there was a problem in
    98  *         creating address space area.
    99  *
    100  */
    101 NO_TRACE static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
     96 * @return EOK on success.
     97 * @return EPERM if the caller lacks capabilities to use this syscall.
     98 * @return EBADMEM if phys or virt is not page aligned.
     99 * @return ENOENT if there is no task matching the specified ID or
     100 *         the physical address space is not enabled for mapping.
     101 * @return ENOMEM if there was a problem in creating address space area.
     102 *
     103 */
     104NO_TRACE static int ddi_physmem_map(uintptr_t phys, uintptr_t virt, size_t pages,
    102105    unsigned int flags)
    103106{
    104107        ASSERT(TASK);
    105108       
    106         if ((pf % FRAME_SIZE) != 0)
     109        if ((phys % FRAME_SIZE) != 0)
    107110                return EBADMEM;
    108111       
    109         if ((vp % PAGE_SIZE) != 0)
     112        if ((virt % PAGE_SIZE) != 0)
    110113                return EBADMEM;
    111114       
     
    118121       
    119122        mem_backend_data_t backend_data;
    120         backend_data.base = pf;
     123        backend_data.base = phys;
    121124        backend_data.frames = pages;
    122125       
     
    129132        btree_node_t *nodep;
    130133        parea_t *parea = (parea_t *) btree_search(&parea_btree,
    131             (btree_key_t) pf, &nodep);
     134            (btree_key_t) phys, &nodep);
    132135       
    133136        if ((parea != NULL) && (parea->frames >= pages)) {
     
    149152       
    150153        irq_spinlock_lock(&zones.lock, true);
    151         size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
     154        size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
    152155       
    153156        if (znum == (size_t) -1) {
     
    182185       
    183186map:
    184         if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
     187        if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages), virt,
    185188            AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
    186189                /*
     
    253256/** Wrapper for SYS_PHYSMEM_MAP syscall.
    254257 *
    255  * @param phys_base Physical base address to map
    256  * @param virt_base Destination virtual address
     258 * @param phys  Physical base address to map
     259 * @param virt  Destination virtual address
    257260 * @param pages Number of pages
    258261 * @param flags Flags of newly mapped pages
     
    261264 *
    262265 */
    263 sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
    264     sysarg_t pages, sysarg_t flags)
    265 {
    266         return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
    267             FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
    268             (size_t) pages, (int) flags);
     266sysarg_t sys_physmem_map(uintptr_t phys, uintptr_t virt,
     267    size_t pages, unsigned int flags)
     268{
     269        return (sysarg_t)
     270            ddi_physmem_map(ALIGN_DOWN(phys, FRAME_SIZE),
     271            ALIGN_DOWN(virt, PAGE_SIZE), pages, flags);
    269272}
    270273
     
    287290}
    288291
     292NO_TRACE static int dmamem_map(uintptr_t virt, size_t size,
     293    unsigned int map_flags, unsigned int flags, void **phys)
     294{
     295        ASSERT(TASK);
     296       
     297        if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
     298                // TODO: implement locking of non-anonymous mapping
     299                return page_find_mapping(virt, phys);
     300        } else {
     301                // TODO: implement locking
     302               
     303                if ((virt % PAGE_SIZE) != 0)
     304                        return EBADMEM;
     305               
     306                size_t pages = SIZE2FRAMES(size);
     307                uint8_t order;
     308               
     309                /* We need the 2^order >= pages */
     310                if (pages == 1)
     311                        order = 0;
     312                else
     313                        order = fnzb(pages - 1) + 1;
     314               
     315                *phys = frame_alloc_noreserve(order, 0);
     316                if (*phys == NULL)
     317                        return ENOMEM;
     318               
     319                mem_backend_data_t backend_data;
     320                backend_data.base = (uintptr_t) *phys;
     321                backend_data.frames = pages;
     322               
     323                if (!as_area_create(TASK->as, map_flags, size, virt,
     324                    AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
     325                        frame_free_noreserve((uintptr_t) *phys);
     326                        return ENOMEM;
     327                }
     328               
     329                return EOK;
     330        }
     331}
     332
     333NO_TRACE static int dmamem_unmap(uintptr_t virt, size_t size,
     334    unsigned int flags)
     335{
     336        // TODO: implement unlocking & unmap
     337        return EOK;
     338}
     339
     340sysarg_t sys_dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
     341    unsigned int flags, void *phys_ptr)
     342{
     343        void *phys;
     344        int rc = dmamem_map(virt, size, map_flags, flags, &phys);
     345        if (rc != EOK)
     346                return rc;
     347       
     348        rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
     349        if (rc != EOK) {
     350                dmamem_unmap(virt, size, flags);
     351                return rc;
     352        }
     353       
     354        return EOK;
     355}
     356
     357sysarg_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
     358{
     359        return dmamem_unmap(virt, size, flags);
     360}
     361
    289362/** @}
    290363 */
  • kernel/generic/src/ipc/irq.c

    r9916841 r792807f  
    365365                return IRQ_DECLINE;
    366366       
    367 #define CMD_MEM_READ(target) \
    368 do { \
    369         void *va = code->cmds[i].addr; \
    370         if (AS != irq->driver_as) \
    371                 as_switch(AS, irq->driver_as); \
    372         memcpy_from_uspace(&target, va, (sizeof(target))); \
    373         if (dstarg) \
    374                 scratch[dstarg] = target; \
    375 } while(0)
    376 
    377 #define CMD_MEM_WRITE(val) \
    378 do { \
    379         void *va = code->cmds[i].addr; \
    380         if (AS != irq->driver_as) \
    381                 as_switch(AS, irq->driver_as); \
    382         memcpy_to_uspace(va, &val, sizeof(val)); \
    383 } while (0)
    384 
    385367        as_t *current_as = AS;
    386         size_t i;
    387         for (i = 0; i < code->cmdcount; i++) {
     368        if (current_as != irq->driver_as)
     369                as_switch(AS, irq->driver_as);
     370       
     371        for (size_t i = 0; i < code->cmdcount; i++) {
    388372                uint32_t dstval;
     373                void *va;
     374                uint8_t val8;
     375                uint16_t val16;
     376                uint32_t val32;
     377               
    389378                uintptr_t srcarg = code->cmds[i].srcarg;
    390379                uintptr_t dstarg = code->cmds[i].dstarg;
     
    442431                        }
    443432                        break;
    444                 case CMD_MEM_READ_8: {
    445                         uint8_t val;
    446                         CMD_MEM_READ(val);
    447                         break;
    448                         }
    449                 case CMD_MEM_READ_16: {
    450                         uint16_t val;
    451                         CMD_MEM_READ(val);
    452                         break;
    453                         }
    454                 case CMD_MEM_READ_32: {
    455                         uint32_t val;
    456                         CMD_MEM_READ(val);
    457                         break;
    458                         }
    459                 case CMD_MEM_WRITE_8: {
    460                         uint8_t val = code->cmds[i].value;
    461                         CMD_MEM_WRITE(val);
    462                         break;
    463                         }
    464                 case CMD_MEM_WRITE_16: {
    465                         uint16_t val = code->cmds[i].value;
    466                         CMD_MEM_WRITE(val);
    467                         break;
    468                         }
    469                 case CMD_MEM_WRITE_32: {
    470                         uint32_t val = code->cmds[i].value;
    471                         CMD_MEM_WRITE(val);
    472                         break;
    473                         }
     433                case CMD_MEM_READ_8:
     434                        va = code->cmds[i].addr;
     435                        memcpy_from_uspace(&val8, va, sizeof(val8));
     436                        if (dstarg)
     437                                scratch[dstarg] = val8;
     438                        break;
     439                case CMD_MEM_READ_16:
     440                        va = code->cmds[i].addr;
     441                        memcpy_from_uspace(&val16, va, sizeof(val16));
     442                        if (dstarg)
     443                                scratch[dstarg] = val16;
     444                        break;
     445                case CMD_MEM_READ_32:
     446                        va = code->cmds[i].addr;
     447                        memcpy_from_uspace(&val32, va, sizeof(val32));
     448                        if (dstarg)
     449                                scratch[dstarg] = val32;
     450                        break;
     451                case CMD_MEM_WRITE_8:
     452                        val8 = code->cmds[i].value;
     453                        va = code->cmds[i].addr;
     454                        memcpy_to_uspace(va, &val8, sizeof(val8));
     455                        break;
     456                case CMD_MEM_WRITE_16:
     457                        val16 = code->cmds[i].value;
     458                        va = code->cmds[i].addr;
     459                        memcpy_to_uspace(va, &val16, sizeof(val16));
     460                        break;
     461                case CMD_MEM_WRITE_32:
     462                        val32 = code->cmds[i].value;
     463                        va = code->cmds[i].addr;
     464                        memcpy_to_uspace(va, &val32, sizeof(val32));
     465                        break;
    474466                case CMD_MEM_WRITE_A_8:
    475467                        if (srcarg) {
    476                                 uint8_t val = scratch[srcarg];
    477                                 CMD_MEM_WRITE(val);
     468                                val8 = scratch[srcarg];
     469                                va = code->cmds[i].addr;
     470                                memcpy_to_uspace(va, &val8, sizeof(val8));
    478471                        }
    479472                        break;
    480473                case CMD_MEM_WRITE_A_16:
    481474                        if (srcarg) {
    482                                 uint16_t val = scratch[srcarg];
    483                                 CMD_MEM_WRITE(val);
     475                                val16 = scratch[srcarg];
     476                                va = code->cmds[i].addr;
     477                                memcpy_to_uspace(va, &val16, sizeof(val16));
    484478                        }
    485479                        break;
    486480                case CMD_MEM_WRITE_A_32:
    487481                        if (srcarg) {
    488                                 uint32_t val = scratch[srcarg];
    489                                 CMD_MEM_WRITE(val);
     482                                val32 = scratch[srcarg];
     483                                va = code->cmds[i].addr;
     484                                memcpy_to_uspace(va, &val32, sizeof(val32));
    490485                        }
    491486                        break;
     
    513508                }
    514509        }
     510       
    515511        if (AS != current_as)
    516512                as_switch(AS, current_as);
  • kernel/generic/src/mm/page.c

    r9916841 r792807f  
    5353 * We assume that the other processors are either not using the mapping yet
    5454 * (i.e. during the bootstrap) or are executing the TLB shootdown code.  While
    55  * we don't care much about the former case, the processors in the latter case 
     55 * we don't care much about the former case, the processors in the latter case
    5656 * will do an implicit serialization by virtue of running the TLB shootdown
    5757 * interrupt handler.
     
    7474#include <syscall/copy.h>
    7575#include <errno.h>
     76#include <align.h>
    7677
    7778/** Virtual operations for page subsystem. */
     
    176177}
    177178
     179int page_find_mapping(uintptr_t virt, void **phys)
     180{
     181        mutex_lock(&AS->lock);
     182       
     183        pte_t *pte = page_mapping_find(AS, virt, false);
     184        if ((!PTE_VALID(pte)) || (!PTE_PRESENT(pte))) {
     185                mutex_unlock(&AS->lock);
     186                return ENOENT;
     187        }
     188       
     189        *phys = (void *) PTE_GET_FRAME(pte) +
     190            (virt - ALIGN_DOWN(virt, PAGE_SIZE));
     191       
     192        mutex_unlock(&AS->lock);
     193       
     194        return EOK;
     195}
     196
    178197/** Syscall wrapper for getting mapping of a virtual page.
    179  *
    180  * @retval EOK Everything went find, @p uspace_frame and @p uspace_node
    181  *             contains correct values.
    182  * @retval ENOENT Virtual address has no mapping.
    183  */
    184 sysarg_t sys_page_find_mapping(uintptr_t virt_address,
    185     uintptr_t *uspace_frame)
    186 {
    187         mutex_lock(&AS->lock);
    188        
    189         pte_t *pte = page_mapping_find(AS, virt_address, false);
    190         if (!PTE_VALID(pte) || !PTE_PRESENT(pte)) {
    191                 mutex_unlock(&AS->lock);
    192                
    193                 return (sysarg_t) ENOENT;
    194         }
    195        
    196         uintptr_t phys_address = PTE_GET_FRAME(pte);
    197        
    198         mutex_unlock(&AS->lock);
    199        
    200         int rc = copy_to_uspace(uspace_frame,
    201             &phys_address, sizeof(phys_address));
    202         if (rc != EOK) {
    203                 return (sysarg_t) rc;
    204         }
    205        
    206         return EOK;
     198 *
     199 * @return EOK on success.
     200 * @return ENOENT if no virtual address mapping found.
     201 *
     202 */
     203sysarg_t sys_page_find_mapping(uintptr_t virt, void *phys_ptr)
     204{
     205        void *phys;
     206        int rc = page_find_mapping(virt, &phys);
     207        if (rc != EOK)
     208                return rc;
     209       
     210        rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
     211        return (sysarg_t) rc;
    207212}
    208213
  • kernel/generic/src/syscall/syscall.c

    r9916841 r792807f  
    176176        (syshandler_t) sys_device_assign_devno,
    177177        (syshandler_t) sys_physmem_map,
     178        (syshandler_t) sys_dmamem_map,
     179        (syshandler_t) sys_dmamem_unmap,
    178180        (syshandler_t) sys_iospace_enable,
    179181        (syshandler_t) sys_register_irq,
Note: See TracChangeset for help on using the changeset viewer.