From 82e16433fcb5978a41fa9d2322307bd1767a9120 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 23 Sep 2014 16:07:12 -0600 Subject: Completes the implemenation of the core shared memory logic: shmget(), shmctl(), shmat(), and shmdt(). This is still some unfinish platform-specific code that needs to be done before we can begin testing. --- nuttx/mm/shm/shmat.c | 96 +++++++++++++++++++++++++++++++-- nuttx/mm/shm/shmdt.c | 148 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 223 insertions(+), 21 deletions(-) mode change 100755 => 100644 nuttx/mm/shm/shmat.c mode change 100755 => 100644 nuttx/mm/shm/shmdt.c (limited to 'nuttx/mm') diff --git a/nuttx/mm/shm/shmat.c b/nuttx/mm/shm/shmat.c old mode 100755 new mode 100644 index a76327e13..3a9bc4c08 --- a/nuttx/mm/shm/shmat.c +++ b/nuttx/mm/shm/shmat.c @@ -42,6 +42,12 @@ #include #include +#include +#include +#include + +#include "shm/shm.h" + #ifdef CONFIG_MM_SHM /**************************************************************************** @@ -94,7 +100,8 @@ * Returned Value: * Upon successful completion, shmat() will increment the value of * shm_nattch in the data structure associated with the shared memory ID - * of the attached shared memory segment and return the segment's start address. + * of the attached shared memory segment and return the segment's start + * address. * * Otherwise, the shared memory segment will not be attached, shmat() will * return -1, and errno will be set to indicate the error. @@ -119,8 +126,91 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg) { -#warning Not implemented - set_errno(ENOSYS); + FAR struct shm_region_s *region; + FAR struct task_group_s *group; + FAR struct tcb_s *tcb; + uintptr_t vaddr; + unsigned int npages; + int ret; + + /* Get the region associated with the shmid */ + + DEBUGASSERT(shmid >= 0 && shmid < CONFIG_ARCH_SHM_MAXREGIONS); + region = &g_shminfo.si_region[shmid]; + DEBUGASSERT((region->sr_flags & SRFLAG_INUSE) != 0); + + /* Get the TCB and group containing our virtual memory allocator */ + + tcb = sched_self(); + DEBUGASSERT(tcb && tcb->group); + group = tcb->group; + DEBUGASSERT(group->tg_shm.gs_handle != NULL && + group->tg_shm.gs_vaddr[shmid] == 0); + + /* Get exclusive access to the region data structure */ + + ret = sem_wait(®ion->sr_sem); + if (ret < 0) + { + shmdbg("sem_wait failed: %d\n", ret); + goto errout; + } + + /* Set aside a virtual address space to span this physical region */ + + vaddr = (uintptr_t)gran_alloc(group->tg_shm.gs_handle, + region->sr_ds.shm_segsz); + if (vaddr == 0) + { + shmdbg("gran_alloc() failed\n"); + ret = -ENOMEM; + goto errout_with_semaphore; + } + + /* Convert the region size to pages */ + + npages = MM_PGALIGNUP(region->sr_ds.shm_segsz); + + /* Attach, i.e, map, on shared memory region to the user virtual address. */ + + ret = up_shmat(region->sr_pages, npages, vaddr); + if (ret < 0) + { + shmdbg("up_shmat() failed\n"); + goto errout_with_vaddr; + } + + /* Save the virtual address of the region. We will need that in shmat() + * to do the reverse lookup: Give the virtual address of the region to + * detach, we need to get the region table index. + */ + + group->tg_shm.gs_vaddr[shmid] = vaddr; + + /* Increment the count of processes attached to this region */ + + region->sr_ds.shm_nattch++; + + /* Save the process ID of the the last operation */ + + region->sr_ds.shm_lpid = tcb->pid; + + /* Save the time of the last shmat() */ + + region->sr_ds.shm_atime = time(NULL); + + /* Release our lock on the entry */ + + sem_post(®ion->sr_sem); + return (FAR void *)vaddr; + +errout_with_vaddr: + gran_free(group->tg_shm.gs_handle, (FAR void *)vaddr, + region->sr_ds.shm_segsz); +errout_with_semaphore: + sem_post(®ion->sr_sem); + set_errno(-ret); +errout: return (FAR void *)ERROR; } diff --git a/nuttx/mm/shm/shmdt.c b/nuttx/mm/shm/shmdt.c old mode 100755 new mode 100644 index a350e5ca0..8acfc6197 --- a/nuttx/mm/shm/shmdt.c +++ b/nuttx/mm/shm/shmdt.c @@ -33,17 +33,23 @@ * ****************************************************************************/ -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#ifdef CONFIG_MM_SHM - +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +#include "shm/shm.h" + +#ifdef CONFIG_MM_SHM + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -87,13 +93,119 @@ * The value of shmaddr is not the data segment start address of a * shared memory segment. * - ****************************************************************************/ - + ****************************************************************************/ + int shmdt(FAR const void *shmaddr) -{ -#warning Not implemented - set_errno(ENOSYS); - return ERROR; -} +{ + FAR struct shm_region_s *region; + FAR struct task_group_s *group; + FAR struct tcb_s *tcb; + unsigned int npages; + int shmid; + int ret; + + /* Get the TCB and group containing our virtual memory allocator */ + + tcb = sched_self(); + DEBUGASSERT(tcb && tcb->group); + group = tcb->group; + DEBUGASSERT(group->tg_shm.gs_handle != NULL); + + /* Perform the reverse lookup to get the shmid corresponding to this + * shmaddr. + */ + + for (shmid = 0; + shmid < CONFIG_ARCH_SHM_MAXREGIONS && + group->tg_shm.gs_vaddr[shmid] != (uintptr_t)shmaddr; + shmid++); + + if (shmid >= CONFIG_ARCH_SHM_MAXREGIONS) + { + shmdbg("No region matching this virtual address: %p\n", shmaddr); + ret = -EINVAL; + goto errout_with_errno; + } + + /* Get the region associated with the shmid */ + + region = &g_shminfo.si_region[shmid]; + DEBUGASSERT((region->sr_flags & SRFLAG_INUSE) != 0); + + /* Get exclusive access to the region data structure */ + + ret = sem_wait(®ion->sr_sem); + if (ret < 0) + { + shmdbg("sem_wait failed: %d\n", ret); + goto errout; + } + + /* Free the virtual address space */ + + gran_free(group->tg_shm.gs_handle, (FAR void *)shmaddr, + region->sr_ds.shm_segsz); + + /* Convert the region size to pages */ + + npages = MM_PGALIGNUP(region->sr_ds.shm_segsz); + + /* Detach, i.e, unmap, on shared memory region from a user virtual + * address. + */ + + ret = up_shmdt((uintptr_t)shmaddr, npages); + if (ret < 0) + { + shmdbg("up_shmdt() failed\n"); + } + + /* Indicate that there is no longer any mapping for this region. */ + + group->tg_shm.gs_vaddr[shmid] = 0; + + /* Decrement the count of processes attached to this region. + * If the count decrements to zero and there is a pending unlink, + * then destroy the shared memory region now and stop any further + * operations on it. + */ + + DEBUGASSERT(region->sr_ds.shm_nattch > 0); + if (region->sr_ds.shm_nattch <= 1) + { + region->sr_ds.shm_nattch = 0; + if ((region->sr_flags & SRFLAG_UNLINKED) != 0) + { + shm_destroy(shmid); + return OK; + } + } + else + { + /* Just decrement the number of processes attached to the shared + * memory region. + */ + + region->sr_ds.shm_nattch--; + } + + /* Save the process ID of the the last operation */ + + region->sr_ds.shm_lpid = tcb->pid; + + /* Save the time of the last shmdt() */ + + region->sr_ds.shm_dtime = time(NULL); + + /* Release our lock on the entry */ + + sem_post(®ion->sr_sem); + return OK; + +errout_with_errno: + set_errno(-ret); +errout: + return ERROR; +} #endif /* CONFIG_MM_SHM */ -- cgit v1.2.3