From 4388221844f64775ff88557368021e3093b41bde Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Tue, 5 Apr 2011 17:33:50 +0000
Subject: [PATCH] More separation of kernel- and user-memory management

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3468 42af7a65-404d-4744-a932-0658087f49c3
---
 arch/arm/src/sam3u/sam3u_allocateheap.c |   6 +-
 configs/sam3u-ek/kernel/Makefile        |   4 +-
 configs/sam3u-ek/kernel/kernel.ld       |   9 +-
 include/nuttx/kmalloc.h                 |  20 +++-
 sched/Makefile                          |   3 +-
 sched/kmm_addregion.c                   | 113 +++++++++++++++++++
 sched/kmm_initialize.c                  | 113 +++++++++++++++++++
 sched/kmm_kfree.c                       |   2 +-
 sched/kmm_kmalloc.c                     |   2 +-
 sched/kmm_krealloc.c                    |   3 +-
 sched/kmm_kzalloc.c                     |   2 +-
 sched/kmm_semaphore.c                   | 140 ++++++++++++++++++++++++
 sched/os_start.c                        |  11 +-
 sched/sched_free.c                      |   6 +-
 sched/sched_garbage.c                   |   4 +-
 15 files changed, 410 insertions(+), 28 deletions(-)
 create mode 100644 sched/kmm_addregion.c
 create mode 100644 sched/kmm_initialize.c
 create mode 100644 sched/kmm_semaphore.c

diff --git a/arch/arm/src/sam3u/sam3u_allocateheap.c b/arch/arm/src/sam3u/sam3u_allocateheap.c
index 2bd8cacab8..c8ea98ade2 100755
--- a/arch/arm/src/sam3u/sam3u_allocateheap.c
+++ b/arch/arm/src/sam3u/sam3u_allocateheap.c
@@ -43,6 +43,8 @@
 #include <debug.h>
 
 #include <nuttx/arch.h>
+#include <nuttx/kmalloc.h>
+
 #include <arch/board/board.h>
 
 #include "chip.h"
@@ -117,10 +119,10 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size)
 #if CONFIG_MM_REGIONS > 1
 void up_addregion(void)
 {
-  mm_addregion((FAR void*)SAM3U_INTSRAM1_BASE, CONFIG_SAM3U_SRAM1_SIZE);
+  kmm_addregion((FAR void*)SAM3U_INTSRAM1_BASE, CONFIG_SAM3U_SRAM1_SIZE);
 
 #if CONFIG_MM_REGIONS > 2
-  mm_addregion((FAR void*)SAM3U_NFCSRAM_BASE, CONFIG_SAM3U_NFCSRAM_SIZE);
+  kmm_addregion((FAR void*)SAM3U_NFCSRAM_BASE, CONFIG_SAM3U_NFCSRAM_SIZE);
 #endif
 }
 #endif
diff --git a/configs/sam3u-ek/kernel/Makefile b/configs/sam3u-ek/kernel/Makefile
index 5f7d939b23..688ad0e623 100755
--- a/configs/sam3u-ek/kernel/Makefile
+++ b/configs/sam3u-ek/kernel/Makefile
@@ -96,8 +96,8 @@ $(BOARD_INCLUDE)/user_map.h: $(TOPDIR)/User.map
 	@echo "" >> $(BOARD_INCLUDE)/user_map.h
 	@echo "#define CONFIG_USER_MMINIT        0x`grep \" mm_initialize$\"    $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
 	@echo "#define CONFIG_USER_MMADDREGION   0x`grep \" mm_addregion$\"     $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
-	@echo "#define CONFIG_USER_TRYSEM        0x`grep \" mm_trysemaphore$\"  $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
-	@echo "#define CONFIG_USER_GIVSEM        0x`grep \" mm_givesemaphore$\" $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
+	@echo "#define CONFIG_USER_MMTRYSEM      0x`grep \" mm_trysemaphore$\"  $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
+	@echo "#define CONFIG_USER_MMGIVESEM     0x`grep \" mm_givesemaphore$\" $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
 	@echo "" >> $(BOARD_INCLUDE)/user_map.h
 	@echo "#define CONFIG_USER_MALLOC        0x`grep \" malloc$\"  $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
 	@echo "#define CONFIG_USER_REALLOC       0x`grep \" realloc$\" $(TOPDIR)/User.map | cut -d' ' -f1`" >> $(BOARD_INCLUDE)/user_map.h
diff --git a/configs/sam3u-ek/kernel/kernel.ld b/configs/sam3u-ek/kernel/kernel.ld
index b15188787c..8b0ea0244c 100644
--- a/configs/sam3u-ek/kernel/kernel.ld
+++ b/configs/sam3u-ek/kernel/kernel.ld
@@ -62,13 +62,14 @@ MEMORY
     sram2 (rwx) : ORIGIN = 0x20080000, LENGTH = 16K
 }
 
-# Force user_start into the link.  This is the application entry point
+/* Force user_start into the link.  This is the application entry point */
 
 EXTERN(user_start)
 
-# Make sure that the critical memory management functions are in user-space.
-# Currently, the plan is that the memory manager will reside in user-space
-# but be usable both by kernel- and user-space code
+/* Make sure that the critical memory management functions are in user-space.
+ * Currently, the plan is that the memory manager will reside in user-space
+ * but be usable both by kernel- and user-space code
+ */
 
 EXTERN(mm_initialize)
 EXTERN(mm_addregion)
diff --git a/include/nuttx/kmalloc.h b/include/nuttx/kmalloc.h
index 08b1b319bb..aafe150fdd 100644
--- a/include/nuttx/kmalloc.h
+++ b/include/nuttx/kmalloc.h
@@ -41,10 +41,12 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+
 #include <sys/types.h>
 
 #ifndef CONFIG_NUTTX_KERNEL
 #  include <stdlib.h>
+#  include <nuttx/mm.h>
 #endif
 
 /****************************************************************************
@@ -74,13 +76,23 @@ extern "C" {
 
 #ifndef CONFIG_NUTTX_KERNEL
 
-# define kmalloc(s) malloc(s)
-# define kzalloc(s) zalloc(s)
-# define krealloc(p,s) realloc(p,s)
-# define kfree(p) free(p)
+# define kmm_initialize(h,s)    mm_initialize(h,s)
+# define kmm_addregion(h,s)     mm_addregion(h,s)
+# define kmm_trysemaphore(h,s)  mm_trysemaphore(h,s)
+# define kmm_givesemaphore(h,s) mm_givesemaphore(h,s)
+
+# define kmalloc(s)             malloc(s)
+# define kzalloc(s)             zalloc(s)
+# define krealloc(p,s)          realloc(p,s)
+# define kfree(p)               free(p)
 
 #else
 
+KMALLOC_EXTERN void kmm_initialize(FAR void *heap_start, size_t heap_size);
+KMALLOC_EXTERN void kmm_addregion(FAR void *heapstart, size_t heapsize);
+KMALLOC_EXTERN int kmm_trysemaphore(void);
+KMALLOC_EXTERN void kmm_givesemaphore(void);
+
 KMALLOC_EXTERN FAR void *kmalloc(size_t);
 KMALLOC_EXTERN FAR void *kzalloc(size_t);
 KMALLOC_EXTERN FAR void *krealloc(FAR void*, size_t);
diff --git a/sched/Makefile b/sched/Makefile
index f164630bfd..93807b6957 100644
--- a/sched/Makefile
+++ b/sched/Makefile
@@ -134,7 +134,8 @@ endif
 
 IRQ_SRCS	= irq_initialize.c irq_attach.c irq_dispatch.c irq_unexpectedisr.c
 
-KMM_SRCS	= kmm_kmalloc.c kmm_kzalloc.c kmm_krealloc.c kmm_kfree.c
+KMM_SRCS	= kmm_initialize.c kmm_addregion.c kmm_semaphore.c \
+		  kmm_kmalloc.c kmm_kzalloc.c kmm_krealloc.c kmm_kfree.c
 
 CSRCS		= $(MISC_SRCS) $(TSK_SRCS) $(SCHED_SRCS) $(WDOG_SRCS) $(TIME_SRCS) \
 		  $(SEM_SRCS) $(TIMER_SRCS) $(WORK_SRCS) $(PGFILL_SRCS) $(IRQ_SRCS)
diff --git a/sched/kmm_addregion.c b/sched/kmm_addregion.c
new file mode 100644
index 0000000000..ed923a56f4
--- /dev/null
+++ b/sched/kmm_addregion.c
@@ -0,0 +1,113 @@
+/************************************************************************
+ * sched/kmm_addregion.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************************/
+
+/************************************************************************
+ * Included Files
+ ************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/kmalloc.h>
+
+#ifdef CONFIG_NUTTX_KERNEL
+
+/* This logic is all tentatively and, hopefully, will grow in usability.
+ * For now, the kernel-mode build uses the memory manager that is
+ * provided in the user-space build.  That is awkward but reasonable for
+ * the current level of support:  At present, only memory protection is
+ * provided.  Kernel-mode code may call into user-mode code, but not
+ * vice-versa.  So hosting the memory manager in user-space allows the
+ * memory manager to be shared in both kernel- and user-mode spaces.
+ *
+ * In the longer run, if an MMU is support that can provide virtualized
+ * memory, then some SLAB memory manager will be required in kernel-space
+ * with some kind of brk() system call to obtain mapped heap space.
+ *
+ * In the current build model, the user-space module is built first. The
+ * file user_map.h is generated in the first pass and contains the
+ * addresses of the memory manager needed in this file:
+ */
+
+#include <arch/board/user_map.h>
+
+/************************************************************************
+ * Pre-processor definition
+ ************************************************************************/
+
+/* This value is obtained from user_map.h */
+
+#define KADDREGION(h,s) ((kmaddregion_t)CONFIG_USER_MMADDREGION)(h,s)
+
+/************************************************************************
+ * Private Types
+ ************************************************************************/
+
+typedef void (*kmaddregion_t)(FAR void*, size_t);
+
+/************************************************************************
+ * Private Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Public Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Function:  kmm_addregion
+ *
+ * Description:
+ *   This is a simple redirection to the user-space mm_addregion()
+ *   function.
+ *
+ * Parameters:
+ *   heap_start - Address of the beginning of the memory region
+ *   heap_size  - The size (in bytes) if the memory region.
+ *
+ * Return Value:
+ *   None
+ *
+ * Assumptions:
+ *   1. mm_addregion() resides in user-space
+ *   2. The address of the user space mm_addregion() is provided in
+ *      user_map.h
+ *   3. The user-space mm_addregion() is callable from kernel-space.
+ *
+ ************************************************************************/
+
+void kmm_addregion(FAR void *heap_start, size_t heap_size)
+{
+  return KADDREGION(heap_start, heap_size);
+}
+
+#endif /* CONFIG_NUTTX_KERNEL */
diff --git a/sched/kmm_initialize.c b/sched/kmm_initialize.c
new file mode 100644
index 0000000000..59a554adcc
--- /dev/null
+++ b/sched/kmm_initialize.c
@@ -0,0 +1,113 @@
+/************************************************************************
+ * sched/kmm_initialize.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************************/
+
+/************************************************************************
+ * Included Files
+ ************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/kmalloc.h>
+
+#ifdef CONFIG_NUTTX_KERNEL
+
+/* This logic is all tentatively and, hopefully, will grow in usability.
+ * For now, the kernel-mode build uses the memory manager that is
+ * provided in the user-space build.  That is awkward but reasonable for
+ * the current level of support:  At present, only memory protection is
+ * provided.  Kernel-mode code may call into user-mode code, but not
+ * vice-versa.  So hosting the memory manager in user-space allows the
+ * memory manager to be shared in both kernel- and user-mode spaces.
+ *
+ * In the longer run, if an MMU is support that can provide virtualized
+ * memory, then some SLAB memory manager will be required in kernel-space
+ * with some kind of brk() system call to obtain mapped heap space.
+ *
+ * In the current build model, the user-space module is built first. The
+ * file user_map.h is generated in the first pass and contains the
+ * addresses of the memory manager needed in this file:
+ */
+
+#include <arch/board/user_map.h>
+
+/************************************************************************
+ * Pre-processor definition
+ ************************************************************************/
+
+/* This value is obtained from user_map.h */
+
+#define KINITIALIZE(h,s) ((kminitialize_t)CONFIG_USER_MMINIT)(h,s)
+
+/************************************************************************
+ * Private Types
+ ************************************************************************/
+
+typedef void (*kminitialize_t)(FAR void*, size_t);
+
+/************************************************************************
+ * Private Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Public Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Function:  kmm_initialize
+ *
+ * Description:
+ *   This is a simple redirection to the user-space mm_initialize()
+ *   function.
+ *
+ * Parameters:
+ *   heap_start - Address of the beginning of the (initial) memory region
+ *   heap_size  - The size (in bytes) if the (initial) memory region.
+ *
+ * Return Value:
+ *   None
+ *
+ * Assumptions:
+ *   1. mm_initialize() resides in user-space
+ *   2. The address of the user space mm_initialize() is provided in
+ *      user_map.h
+ *   3. The user-space mm_initialize() is callable from kernel-space.
+ *
+ ************************************************************************/
+
+void kmm_initialize(FAR void *heap_start, size_t heap_size)
+{
+  return KINITIALIZE(heap_start, heap_size);
+}
+
+#endif /* CONFIG_NUTTX_KERNEL */
diff --git a/sched/kmm_kfree.c b/sched/kmm_kfree.c
index 0370ce8ee9..2e64be4bc8 100644
--- a/sched/kmm_kfree.c
+++ b/sched/kmm_kfree.c
@@ -102,7 +102,7 @@ typedef void (*kfree_t)(FAR void *);
  *
  ************************************************************************/
 
-void free(FAR void *mem)
+void kfree(FAR void *mem)
 {
   return KFREE(mem);
 }
diff --git a/sched/kmm_kmalloc.c b/sched/kmm_kmalloc.c
index 01267ab486..aee5306f27 100644
--- a/sched/kmm_kmalloc.c
+++ b/sched/kmm_kmalloc.c
@@ -90,7 +90,7 @@ typedef FAR void *(*kmalloc_t)(size_t);
  *   This is a simple redirection to the user-space malloc() function.
  *
  * Parameters:
- *   None
+ *   size - Size (in bytes) of the memory region to be allocated.
  *
  * Return Value:
  *   The address of the allocated memory (NULL on failure to allocate)
diff --git a/sched/kmm_krealloc.c b/sched/kmm_krealloc.c
index b52d864995..91b3448b8c 100644
--- a/sched/kmm_krealloc.c
+++ b/sched/kmm_krealloc.c
@@ -90,7 +90,8 @@ typedef FAR void *(*krealloc_t)(FAR void*, size_t);
  *   This is a simple redirection to the user-space realloc() function.
  *
  * Parameters:
- *   None
+ *   oldmem - The old memory allocated
+ *   size   - Size (in bytes) of the new memory region to be re-allocated.
  *
  * Return Value:
  *   The address of the re-allocated memory (NULL on failure to re-allocate)
diff --git a/sched/kmm_kzalloc.c b/sched/kmm_kzalloc.c
index 567cd37d43..aba3cf26fd 100644
--- a/sched/kmm_kzalloc.c
+++ b/sched/kmm_kzalloc.c
@@ -90,7 +90,7 @@ typedef FAR void *(*kzalloc_t)(size_t);
  *   This is a simple redirection to the user-space zalloc() function.
  *
  * Parameters:
- *   None
+ *   size - Size (in bytes) of the memory region to be allocated.
  *
  * Return Value:
  *   The address of the allocated memory (NULL on failure to allocate)
diff --git a/sched/kmm_semaphore.c b/sched/kmm_semaphore.c
new file mode 100644
index 0000000000..a78a0bed7b
--- /dev/null
+++ b/sched/kmm_semaphore.c
@@ -0,0 +1,140 @@
+/************************************************************************
+ * sched/kmm_semaphore.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************************/
+
+/************************************************************************
+ * Included Files
+ ************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/kmalloc.h>
+
+#ifdef CONFIG_NUTTX_KERNEL
+
+/* This logic is all tentatively and, hopefully, will grow in usability.
+ * For now, the kernel-mode build uses the memory manager that is
+ * provided in the user-space build.  That is awkward but reasonable for
+ * the current level of support:  At present, only memory protection is
+ * provided.  Kernel-mode code may call into user-mode code, but not
+ * vice-versa.  So hosting the memory manager in user-space allows the
+ * memory manager to be shared in both kernel- and user-mode spaces.
+ *
+ * In the longer run, if an MMU is support that can provide virtualized
+ * memory, then some SLAB memory manager will be required in kernel-space
+ * with some kind of brk() system call to obtain mapped heap space.
+ *
+ * In the current build model, the user-space module is built first. The
+ * file user_map.h is generated in the first pass and contains the
+ * addresses of the memory manager needed in this file:
+ */
+
+#include <arch/board/user_map.h>
+
+/************************************************************************
+ * Pre-processor definition
+ ************************************************************************/
+
+/* These values are obtained from user_map.h */
+
+#define KTRYSEMAPHORE()  ((kmtrysemaphore_t) CONFIG_USER_MMTRYSEM )()
+#define KGIVESEMAPHORE() ((kmgivesemaphore_t)CONFIG_USER_MMGIVESEM)()
+
+/************************************************************************
+ * Private Types
+ ************************************************************************/
+
+typedef int  (*kmtrysemaphore_t)(void);
+typedef void (*kmgivesemaphore_t)(void);
+
+/************************************************************************
+ * Private Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Public Functions
+ ************************************************************************/
+
+/************************************************************************
+ * Function:  kmm_trysemaphore
+ *
+ * Description:
+ *   This is a simple redirection to the user-space mm_trysemaphore()
+ *   function.
+ *
+ * Parameters:
+ *   None
+ *
+ * Return Value:
+ *   OK on success; a negated errno on failure
+ *
+ * Assumptions:
+ *   1. mm_trysemaphore() resides in user-space
+ *   2. The address of the user space mm_trysemaphore() is provided in
+ *      user_map.h
+ *   3. The user-space mm_semaphore() is callable from kernel-space.
+ *
+ ************************************************************************/
+
+int kmm_trysemaphore(void)
+{
+  return KTRYSEMAPHORE();
+}
+
+/************************************************************************
+ * Function:  kmm_givesemaphore
+ *
+ * Description:
+ *   This is a simple redirection to the user-space mm_givesemaphore()
+ *   function.
+ *
+ * Parameters:
+ *   None
+ *
+ * Return Value:
+ *   OK on success; a negated errno on failure
+ *
+ * Assumptions:
+ *   1. mm_givesemaphore() resides in user-space
+ *   2. The address of the user space mm_givesemaphore() is provided in
+ *      user_map.h
+ *   3. The user-space mm_semaphore() is callable from kernel-space.
+ *
+ ************************************************************************/
+
+void kmm_givesemaphore(void)
+{
+  KGIVESEMAPHORE();
+}
+
+#endif /* CONFIG_NUTTX_KERNEL */
diff --git a/sched/os_start.c b/sched/os_start.c
index 5b5e659e71..0e44099c83 100644
--- a/sched/os_start.c
+++ b/sched/os_start.c
@@ -47,8 +47,9 @@
 #include  <nuttx/fs.h>
 #include  <nuttx/net.h>
 #include  <nuttx/lib.h>
-#include  <nuttx/mm.h>
+#include  <nuttx/kmalloc.h>
 #include  <nuttx/init.h>
+
 #include  "os_internal.h"
 #include  "sig_internal.h"
 #include  "wd_internal.h"
@@ -295,10 +296,10 @@ void os_start(void)
     FAR void *heap_start;
     size_t heap_size;
     up_allocate_heap(&heap_start, &heap_size);
-    mm_initialize(heap_start, heap_size);
+    kmm_initialize(heap_start, heap_size);
   }
 #else
-  mm_initialize((void*)CONFIG_HEAP_BASE, CONFIG_HEAP_SIZE);
+  kmm_initialize((void*)CONFIG_HEAP_BASE, CONFIG_HEAP_SIZE);
 #endif
 
   /* Initialize the interrupt handling subsystem (if included) */
@@ -460,10 +461,10 @@ void os_start(void)
        * possible because if the IDLE thread is running, no other task is!
        */
 
-      if (mm_trysemaphore() == 0)
+      if (kmm_trysemaphore() == 0)
         {
           sched_garbagecollection();
-          mm_givesemaphore();
+          kmm_givesemaphore();
         }
 #endif
 
diff --git a/sched/sched_free.c b/sched/sched_free.c
index 998f5458ce..2bdf9670ed 100644
--- a/sched/sched_free.c
+++ b/sched/sched_free.c
@@ -45,8 +45,6 @@
 #include <nuttx/kmalloc.h>
 #include <nuttx/arch.h>
 #include <nuttx/wqueue.h>
-#include <nuttx/mm.h>
-
 #include "os_internal.h"
 
 /************************************************************************
@@ -92,7 +90,7 @@ void sched_free(FAR void *address)
    * manager to do this.
    */
 
-  if (up_interrupt_context() || mm_trysemaphore() != 0)
+  if (up_interrupt_context() || kmm_trysemaphore() != 0)
     {
       /* Yes.. Delay the deallocation until a more appropriate time. */
 
@@ -111,7 +109,7 @@ void sched_free(FAR void *address)
       /* No.. just deallocate the memory now. */
 
       kfree(address);
-      mm_givesemaphore();
+      kmm_givesemaphore();
     }
 }
 
diff --git a/sched/sched_garbage.c b/sched/sched_garbage.c
index 946198ede1..714422bd67 100755
--- a/sched/sched_garbage.c
+++ b/sched/sched_garbage.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/work_garbage.c
+ * sched/sched_garbage.c
  *
- *   Copyright (C) 2009 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
-- 
GitLab