Changeset fbcdeb8 in mainline for kernel/generic/src/ddi/ddi.c


Ignore:
Timestamp:
2011-12-19T17:30:39Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
58f6229
Parents:
24cf31f1
Message:

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

  • SYS_AS_GET_UNMAPPED_AREA was removed
  • dummy implementations of SYS_PHYSMEM_UNMAP and SYS_IOSPACE_DISABLE were added for the sake of symmetry (they do nothing yet)
  • SYS_PHYSMEM_MAP and SYS_DMAMEM_MAP were altered to accept (void *) -1 as address space area base and a lower bound
  • kernel as_area_create() and as_area_share() were altered to accept (void *) -1 as address space area base and a lower bound
  • uspace libraries and programs were altered to reflect the new API
File:
1 edited

Legend:

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

    r24cf31f1 rfbcdeb8  
    9090 *
    9191 * @param phys  Physical address of the starting frame.
    92  * @param virt  Virtual address of the starting page.
    9392 * @param pages Number of pages to map.
    9493 * @param flags Address space area flags for the mapping.
     94 * @param virt  Virtual address of the starting page.
     95 * @param bound Lowest virtual address bound.
    9596 *
    9697 * @return EOK on success.
    9798 * @return EPERM if the caller lacks capabilities to use this syscall.
    98  * @return EBADMEM if phys or virt is not page aligned.
     99 * @return EBADMEM if phys is not page aligned.
    99100 * @return ENOENT if there is no task matching the specified ID or
    100101 *         the physical address space is not enabled for mapping.
     
    102103 *
    103104 */
    104 NO_TRACE static int ddi_physmem_map(uintptr_t phys, uintptr_t virt, size_t pages,
    105     unsigned int flags)
     105NO_TRACE static int physmem_map(uintptr_t phys, size_t pages,
     106    unsigned int flags, uintptr_t *virt, uintptr_t bound)
    106107{
    107108        ASSERT(TASK);
    108109       
    109110        if ((phys % FRAME_SIZE) != 0)
    110                 return EBADMEM;
    111        
    112         if ((virt % PAGE_SIZE) != 0)
    113111                return EBADMEM;
    114112       
     
    185183       
    186184map:
    187         if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages), virt,
    188             AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
     185        if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
     186            AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
    189187                /*
    190188                 * The address space area was not created.
     
    210208}
    211209
     210NO_TRACE static int physmem_unmap(uintptr_t virt)
     211{
     212        // TODO: implement unmap
     213        return EOK;
     214}
     215
     216/** Wrapper for SYS_PHYSMEM_MAP syscall.
     217 *
     218 * @param phys     Physical base address to map
     219 * @param pages    Number of pages
     220 * @param flags    Flags of newly mapped pages
     221 * @param virt_ptr Destination virtual address
     222 * @param bound    Lowest virtual address bound.
     223 *
     224 * @return 0 on success, otherwise it returns error code found in errno.h
     225 *
     226 */
     227sysarg_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
     228    void *virt_ptr, uintptr_t bound)
     229{
     230        uintptr_t virt = (uintptr_t) -1;
     231        int rc = physmem_map(ALIGN_DOWN(phys, FRAME_SIZE), pages, flags,
     232            &virt, bound);
     233        if (rc != EOK)
     234                return rc;
     235       
     236        rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
     237        if (rc != EOK) {
     238                physmem_unmap((uintptr_t) virt);
     239                return rc;
     240        }
     241       
     242        return EOK;
     243}
     244
     245sysarg_t sys_physmem_unmap(uintptr_t virt)
     246{
     247        return physmem_unmap(virt);
     248}
     249
    212250/** Enable range of I/O space for task.
    213251 *
     
    220258 *
    221259 */
    222 NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
    223     size_t size)
     260NO_TRACE static int iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
    224261{
    225262        /*
     
    246283        /* Lock the task and release the lock protecting tasks_btree. */
    247284        irq_spinlock_exchange(&tasks_lock, &task->lock);
    248        
    249285        int rc = ddi_iospace_enable_arch(task, ioaddr, size);
    250        
    251286        irq_spinlock_unlock(&task->lock, true);
    252287       
    253288        return rc;
    254 }
    255 
    256 /** Wrapper for SYS_PHYSMEM_MAP syscall.
    257  *
    258  * @param phys  Physical base address to map
    259  * @param virt  Destination virtual address
    260  * @param pages Number of pages
    261  * @param flags Flags of newly mapped pages
    262  *
    263  * @return 0 on success, otherwise it returns error code found in errno.h
    264  *
    265  */
    266 sysarg_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);
    272289}
    273290
     
    286303                return (sysarg_t) rc;
    287304       
    288         return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
     305        return (sysarg_t) iospace_enable((task_id_t) arg.task_id,
    289306            (uintptr_t) arg.ioaddr, (size_t) arg.size);
    290307}
    291308
    292 NO_TRACE static int dmamem_map(uintptr_t virt, size_t size,
    293     unsigned int map_flags, unsigned int flags, void **phys)
     309sysarg_t sys_iospace_disable(ddi_ioarg_t *uspace_io_arg)
     310{
     311        // TODO: implement
     312        return ENOTSUP;
     313}
     314
     315NO_TRACE static int dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
     316    unsigned int flags, void **phys)
    294317{
    295318        ASSERT(TASK);
    296319       
     320        // TODO: implement locking of non-anonymous mapping
     321        return page_find_mapping(virt, phys);
     322}
     323
     324NO_TRACE static int dmamem_map_anonymous(size_t size, unsigned int map_flags,
     325    unsigned int flags, void **phys, uintptr_t *virt, uintptr_t bound)
     326{
     327        ASSERT(TASK);
     328       
     329        size_t pages = SIZE2FRAMES(size);
     330        uint8_t order;
     331       
     332        /* We need the 2^order >= pages */
     333        if (pages == 1)
     334                order = 0;
     335        else
     336                order = fnzb(pages - 1) + 1;
     337       
     338        *phys = frame_alloc_noreserve(order, 0);
     339        if (*phys == NULL)
     340                return ENOMEM;
     341       
     342        mem_backend_data_t backend_data;
     343        backend_data.base = (uintptr_t) *phys;
     344        backend_data.frames = pages;
     345       
     346        if (!as_area_create(TASK->as, map_flags, size,
     347            AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
     348                frame_free_noreserve((uintptr_t) *phys);
     349                return ENOMEM;
     350        }
     351       
     352        return EOK;
     353}
     354
     355NO_TRACE static int dmamem_unmap(uintptr_t virt, size_t size)
     356{
     357        // TODO: implement unlocking & unmap
     358        return EOK;
     359}
     360
     361NO_TRACE static int dmamem_unmap_anonymous(uintptr_t virt)
     362{
     363        // TODO: implement unlocking & unmap
     364        return EOK;
     365}
     366
     367sysarg_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
     368    void *phys_ptr, void *virt_ptr, uintptr_t bound)
     369{
    297370        if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
    298                 // TODO: implement locking of non-anonymous mapping
    299                 return page_find_mapping(virt, phys);
     371                /*
     372                 * Non-anonymous DMA mapping
     373                 */
     374               
     375                void *phys;
     376                int rc = dmamem_map((uintptr_t) virt_ptr, size, map_flags,
     377                    flags, &phys);
     378               
     379                if (rc != EOK)
     380                        return rc;
     381               
     382                rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
     383                if (rc != EOK) {
     384                        dmamem_unmap((uintptr_t) virt_ptr, size);
     385                        return rc;
     386                }
    300387        } 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;
     388                /*
     389                 * Anonymous DMA mapping
     390                 */
     391               
     392                void *phys;
     393                uintptr_t virt = (uintptr_t) -1;
     394                int rc = dmamem_map_anonymous(size, map_flags, flags,
     395                    &phys, &virt, bound);
     396                if (rc != EOK)
     397                        return rc;
     398               
     399                rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
     400                if (rc != EOK) {
     401                        dmamem_unmap_anonymous((uintptr_t) virt);
     402                        return rc;
    327403                }
    328404               
    329                 return EOK;
    330         }
    331 }
    332 
    333 NO_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 
    340 sysarg_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;
     405                rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
     406                if (rc != EOK) {
     407                        dmamem_unmap_anonymous((uintptr_t) virt);
     408                        return rc;
     409                }
    352410        }
    353411       
     
    357415sysarg_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
    358416{
    359         return dmamem_unmap(virt, size, flags);
     417        if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
     418                return dmamem_unmap(virt, size);
     419        else
     420                return dmamem_unmap_anonymous(virt);
    360421}
    361422
Note: See TracChangeset for help on using the changeset viewer.