summaryrefslogtreecommitdiff
path: root/nuttx/arch/z80
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-12-18 16:15:27 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-12-18 16:15:27 +0000
commitbbef9f93daeb6a3565c4c28572d73130dfb6ac85 (patch)
tree6b46318bc451e64bb4ee0b84c499f58ab572d4e9 /nuttx/arch/z80
parent14a5bc69504838d858fbb9981c9171d9fd625f10 (diff)
downloadpx4-nuttx-bbef9f93daeb6a3565c4c28572d73130dfb6ac85.tar.gz
px4-nuttx-bbef9f93daeb6a3565c4c28572d73130dfb6ac85.tar.bz2
px4-nuttx-bbef9f93daeb6a3565c4c28572d73130dfb6ac85.zip
Restructre address environment interfaces in preparation for incorporation into binfmt/ logic
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5442 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/arch/z80')
-rw-r--r--nuttx/arch/z80/include/z180/arch.h23
-rw-r--r--nuttx/arch/z80/src/z180/z180_mmu.c253
2 files changed, 208 insertions, 68 deletions
diff --git a/nuttx/arch/z80/include/z180/arch.h b/nuttx/arch/z80/include/z180/arch.h
index 9e2197522..d7cf82a26 100644
--- a/nuttx/arch/z80/include/z180/arch.h
+++ b/nuttx/arch/z80/include/z180/arch.h
@@ -45,6 +45,12 @@
* Included Files
****************************************************************************/
+#include <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <arch/irq.h>
+
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@@ -52,6 +58,23 @@
/****************************************************************************
* Public Types
****************************************************************************/
+/* The z180 address environment is represented in hardware as the 8-bit
+ * Common Base Register (CBR). CBR specifies the base address (on 4KB
+ * boundaries) used to generate a 20-bit physical address for Common Area 1
+ * accesses. CBR is the upper 8-bits of the 20-bit address; the lower 14-bits
+ * of the base address are implicitly zero (hence the 4KB boundary alignment).
+ */
+
+#ifdef CONFIG_ADDRENV
+typedef uint8_t hw_addrenv_t;
+
+/* At the task-level, the z180 address environment is represented as struct
+ * z180_cbr_s which is defined in irq.h.
+ */
+
+struct z180_cbr_s;
+typedef FAR struct z180_cbr_s *task_addrenv_t;
+#endif
/****************************************************************************
* Public Variables
diff --git a/nuttx/arch/z80/src/z180/z180_mmu.c b/nuttx/arch/z80/src/z180/z180_mmu.c
index e24b22b8e..5f9b26be2 100644
--- a/nuttx/arch/z80/src/z180/z180_mmu.c
+++ b/nuttx/arch/z80/src/z180/z180_mmu.c
@@ -79,7 +79,7 @@ static struct z180_cbr_s g_cbrs[CONFIG_MAX_TASKS];
****************************************************************************/
/****************************************************************************
- * Name: z180_mmu_findcbr
+ * Name: z180_mmu_alloccbr
*
* Description:
* Find an unused struture in g_cbrs (i.e., one with reference count == 0).
@@ -88,7 +88,7 @@ static struct z180_cbr_s g_cbrs[CONFIG_MAX_TASKS];
*
****************************************************************************/
-static FAR struct z180_cbr_s *z180_mmu_findcbr(void)
+static inline FAR struct z180_cbr_s *z180_mmu_alloccbr(void)
{
int i;
@@ -177,24 +177,53 @@ return g_physhandle ? OK : -ENOMEM;
}
/****************************************************************************
+ * Address Environment Interfaces
+ *
+ * Low-level interfaces used in binfmt/ to instantiate tasks with address
+ * environments. These interfaces all operate on task_addrenv_t which is an
+ * abstract representation of the address environment and must be provided
+ * by arch/arch.h is CONFIG_ADDRENV is defined.
+ *
+ * up_addrenv_create - Create an address environment
+ * up_addrenv_vaddr - Returns the virtual base address of the address
+ * environment
+ * up_addrenv_select - Instantiate an address environment
+ * up_addrenv_destroy - Destroy an address environment.
+ * up_addrenv_assign - Assign an address environment to a TCB
+ *
+ * Higher-level interfaces used by the tasking logic. These interfaces are
+ * used by the functions in sched/ and all operate on the TCB which as been
+ * assigned an address environment by up_addrenv_assign().
+ *
+ * up_addrenv_share - Clone the address environment assigned to one TCB
+ * to another. This operation is done when a pthread
+ * is created that share's the same address
+ * environment.
+ * up_addrenv_release - Release the TCBs reference to an address
+ * environment when a task/thread exists.
+ *
+ ****************************************************************************/
+/****************************************************************************
* Name: up_addrenv_create
*
* Description:
* This function is called from the binary loader logic when a new
- * task is created in RAM in order to instantiate an address environment for
- * the task.
+ * task is created in order to instantiate an address environment for the
+ * task. up_addrenv_create is essentially the allocator of the physical
+ * memory for the new task.
*
* Input Parameters:
- * tcb - The TCB of the task needing the address environment.
* envsize - The size (in bytes) of the address environment needed by the
* task.
+ * addrenv - The location to return the representation of the task address
+ * environment.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
-int up_addrenv_create(FAR _TCB *tcb, size_t envsize)
+int up_addrenv_create(size_t envsize, FAR task_addrenv_t *addrenv)
{
FAR struct z180_cbr_s *cbr;
irqstate_t flags;
@@ -202,10 +231,6 @@ int up_addrenv_create(FAR _TCB *tcb, size_t envsize)
unsigned int npages;
int ret;
- /* Make sure that there is no address environment in place on this TCB */
-
- DEBUGASSERT(tcb->xcp.cbr == NULL);
-
/* Convert the size from bytes to numbers of pages */
npages = PHYS_ALIGNUP(envsize);
@@ -225,7 +250,7 @@ int up_addrenv_create(FAR _TCB *tcb, size_t envsize)
*/
flags = irqsave();
- cbr = z180_mmu_findcbr();
+ cbr = z180_mmu_alloccbr();
if (!cbr)
{
sdbg("ERROR: No free CBR structures\n");
@@ -235,7 +260,6 @@ int up_addrenv_create(FAR _TCB *tcb, size_t envsize)
/* Now allocate the physical memory to back up the address environment */
-
#ifdef CONFIG_GRAN_SINGLE
alloc = (uintptr_t)gran_alloc(npages);
#else
@@ -254,9 +278,9 @@ int up_addrenv_create(FAR _TCB *tcb, size_t envsize)
DEBUGASSERT(alloc <= 0xff);
- cbr->cbr = alloc;
- cbr->pages = npages;
- tcb->xcp.cbr = cbr;
+ cbr->cbr = (uint8_t)alloc;
+ cbr->pages = (uint8_t)npages;
+ *addrenv = (task_addrenv_t)cbr;
irqrestore(flags);
return OK;
@@ -270,111 +294,204 @@ errout_with_irq:
}
/****************************************************************************
- * Name: up_addrenv_share
+ * Name: up_addrenv_vaddr
*
* Description:
- * This function is called from the core scheduler logic when a thread
- * is created that needs to share the address ennvironment of its parent
- * task. In this case, the parent's address environment needs to be
- * "cloned" for the child.
+ * Return the virtual address associated with the newly create address
+ * environment. This function is used by the binary loaders in order
+ * get an address that can be used to initialize the new task..
*
* Input Parameters:
- * ptcb - The TCB of the parent task that has the address environment.
- * ctcb - The TCB of the child thread needing the address environment.
+ * addrenv - The representation of the task address environment previously
+ * returned by up_addrenv_create.
+ * vaddr - The location to return the virtual address.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
-int up_addrenv_share(FAR const _TCB *ptcb, FAR _TCB *ctcb)
+int up_addrenv_vaddr(FAR task_addrenv_t addrenv, FAR void **vaddr)
+{
+ return CONFIG_Z180_COMMON1AREA_VIRTBASE;
+}
+
+/****************************************************************************
+ * Name: up_addrenv_select
+ *
+ * Description:
+ * After an address environment has been established for a task (via
+ * up_addrenv_create()), this function may be called to to instantiate
+ * that address environment in the virtual address space. This might be
+ * necessary, for example, to load the code for the task from a file or
+ * to access address environment private data.
+ *
+ * Input Parameters:
+ * addrenv - The representation of the task address environment previously
+ * returned by up_addrenv_create.
+ * oldenv
+ * The address environment that was in place before up_addrenv_select().
+ * This may be used with up_addrenv_restore() to restore the original
+ * address environment that was in place before up_addrenv_select() was
+ * called. Note that this may be a task agnostic, hardware
+ * representation that is different from task_addrenv_t.
+ *
+ * Returned Value:
+ * Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int up_addrenv_select(task_addrenv_t addrenv, hw_addrenv_t *oldenv)
{
+ FAR struct z180_cbr_s *cbr = (FAR struct z180_cbr_s *)addrenv;
irqstate_t flags;
- /* Make sure that the child has no address environment. It is okay if
- * if the parent does not have one.
- */
+ DEBUGASSERT(cbr && oldenv);
- DEBUGASSERT(ctcb->xcp.cbr == NULL);
+ /* Return the current CBR value from the CBR register */
flags = irqsave();
- if (ptcb->xcp.cbr)
- {
- /* Clone the CBR by incrementing the reference counting and saving a
- * copy in the child thread's TCB.
- */
+ *oldenv = (hw_addrenv_t)inp(Z180_MMU_CBR);
- ptcb->xcp.cbr->crefs++;
- ctcb->xcp.cbr = ptcb->xcp.cbr;
- }
+ /* Write the new CBR value into CBR register */
+ outp(Z180_MMU_CBR, cbr->cbr);
irqrestore(flags);
return OK;
}
/****************************************************************************
- * Name: up_addrenv_instantiate
+ * Name: up_addrenv_restore
*
* Description:
- * After an address environment has been established for a task (via
- * up_addrenv_create(). This function may be called to to instantiate
- * that address environment in the virtual address space. this might be
- * necessary, for example, to load the code for the task from a file or
- * to access address environment private data.
+ * After an address environment has been temporarilty instantiated by
+ * up_addrenv_select, this function may be called to to restore the
+ * original address environment.
*
* Input Parameters:
- * tcb - The TCB of the task or thread whose the address environment will
- * be instantiated.
+ * oldenv - The hardware representation of the address environment
+ * previously returned by up_addrenv_select.
*
* Returned Value:
- * A handle that may be used with up_addrenv_restore() to restore the
- * address environment before up_addrenv_instantiate() was called.
+ * Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
-FAR void *up_addrenv_instantiate(FAR _TCB *tcb)
+int up_addrenv_restore(hw_addrenv_t oldenv)
{
- uint8_t oldcbr;
- irqstate_t flags;
+ outp(Z180_MMU_CBR, (uint8_t)oldenv);
+ return OK;
+}
- /* Get the current CBR value from the CBR register */
+/****************************************************************************
+ * Name: up_addrenv_destroy
+ *
+ * Description:
+ * Called from the binary loader loader during error handling to destroy
+ * the address environment previously created by up_addrenv_create().
+ *
+ * Input Parameters:
+ * addrenv - The representation of the task address environment previously
+ * returned by up_addrenv_create.
+ *
+ * Returned Value:
+ * Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
- flags = irqsave();
- oldcbr = inp(Z180_MMU_CBR);
+int up_addrenv_destroy(task_addrenv_t addrenv)
+{
+ FAR struct z180_cbr_s *cbr = (FAR struct z180_cbr_s *)addrenv;
- /* Check if the task has an address environment. */
+ DEBUGASSERT(cbr);
- if (tcb->xcp.cbr)
- {
- /* Yes.. Write the new CBR value into CBR register */
+ /* Free the physical address space backing up the mapping */
- outp(Z180_MMU_CBR, tcb->xcp.cbr->cbr);
- }
+#ifdef CONFIG_GRAN_SINGLE
+ gran_free((FAR void *)cbr->cbr, cbr->pages);
+#else
+ gran_free(g_physhandle, (FAR void *)cbr->cbr, cbr->pages);
+#endif
- irqrestore(flags);
- return (FAR void *)oldcbr;
+ /* And make the CBR structure available for re-use */
+
+ z180_mmu_freecbr(cbr);
+ return OK;
}
/****************************************************************************
- * Name: up_addrenv_restore
+ * Name: up_addrenv_assign
*
* Description:
- * Restore an address environment using a handle previously returned by
- * up_addrenv_instantiate().
+ * Assign an address environment to a TCB.
*
* Input Parameters:
- * handle - A handle previously returned by up_addrenv_instantiate.
+ * addrenv - The representation of the task address environment previously
+ * returned by up_addrenv_create.
+ * tcb - The TCB of the task to receive the address environment.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
-int up_addrenv_restore(FAR void *handle)
+int up_addrenv_assign(task_addrenv_t addrenv, FAR _TCB *tcb)
{
- /* Restore the CBR value */
+ FAR struct z180_cbr_s *cbr = (FAR struct z180_cbr_s *)addrenv;
+ int ret;
+
+ /* Make sure that there is no address environment in place on this TCB */
- outp(Z180_MMU_CBR, (uint8_t)handle);
+ DEBUGASSERT(cbr && tcb->xcp.cbr == NULL);
+
+ /* Save the CBR strucure in the TCB. This is an atomic operation so no
+ * special precautions should be needed.
+ */
+
+ tcb->xcp.cbr = cbr;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: up_addrenv_share
+ *
+ * Description:
+ * This function is called from the core scheduler logic when a thread
+ * is created that needs to share the address ennvironment of its parent
+ * task. In this case, the parent's address environment needs to be
+ * "cloned" for the child.
+ *
+ * Input Parameters:
+ * ptcb - The TCB of the parent task that has the address environment.
+ * ctcb - The TCB of the child thread needing the address environment.
+ *
+ * Returned Value:
+ * Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int up_addrenv_share(FAR const _TCB *ptcb, FAR _TCB *ctcb)
+{
+ irqstate_t flags;
+
+ /* Make sure that the child has no address environment. It is okay if
+ * if the parent does not have one.
+ */
+
+ DEBUGASSERT(ctcb->xcp.cbr == NULL);
+
+ flags = irqsave();
+ if (ptcb->xcp.cbr)
+ {
+ /* Clone the CBR by incrementing the reference counting and saving a
+ * copy in the child thread's TCB.
+ */
+
+ ptcb->xcp.cbr->crefs++;
+ ctcb->xcp.cbr = ptcb->xcp.cbr;
+ }
+
+ irqrestore(flags);
return OK;
}
@@ -407,7 +524,7 @@ int up_addrenv_release(FAR _TCB *tcb)
cbr = tcb->xcp.cbr;
if (cbr)
{
- /* Nullify the reference to the CBR structgure and decrement the number
+ /* Nullify the reference to the CBR structure and decrement the number
* of references on the CBR.
*/
@@ -419,7 +536,7 @@ int up_addrenv_release(FAR _TCB *tcb)
if (cbr->crefs <= 1)
{
- z180_mmu_freecbr(cbr);
+ up_addrenv_destroy(cbr);
}
else
{