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.

This commit is contained in:
Gregory Nutt 2014-09-23 16:07:12 -06:00
parent d546ebabe4
commit 0f97d81f8d
2 changed files with 223 additions and 21 deletions

96
mm/shm/shmat.c Executable file → Normal file
View File

@ -42,6 +42,12 @@
#include <sys/shm.h>
#include <errno.h>
#include <nuttx/sched.h>
#include <nuttx/arch.h>
#include <nuttx/pgalloc.h>
#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(&region->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(&region->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(&region->sr_sem);
set_errno(-ret);
errout:
return (FAR void *)ERROR;
}

148
mm/shm/shmdt.c Executable file → Normal file
View File

@ -33,17 +33,23 @@
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/shm.h>
#include <errno.h>
#ifdef CONFIG_MM_SHM
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/shm.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/shm.h>
#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(&region->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(&region->sr_sem);
return OK;
errout_with_errno:
set_errno(-ret);
errout:
return ERROR;
}
#endif /* CONFIG_MM_SHM */