diff mbox

[U-Boot,[PATCH,V2] ] sandbox: Add improved RAM simulation

Message ID 1320264772-10668-1-git-send-email-weisserm@arcor.de
State Superseded
Headers show

Commit Message

Matthias Weisser Nov. 2, 2011, 8:12 p.m. UTC
Using mmap to allocate memory from the OS for RAM simulation we can use
u-boots own malloc implementation.

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
---
Changes in V2:
  Removed the address hint for mmap
  Removed the special handling of dlmalloc in common
  Set gd->bd->bi_dram[0].start to 0 again

 arch/sandbox/cpu/os.c    |    7 +++++++
 arch/sandbox/lib/board.c |   17 ++++++++++-------
 common/Makefile          |    2 --
 include/os.h             |    8 ++++++++
 4 files changed, 25 insertions(+), 9 deletions(-)

Comments

Mike Frysinger Nov. 2, 2011, 8:39 p.m. UTC | #1
Acked-by: Mike Frysinger <vapier@gentoo.org>
-mike
Simon Glass Nov. 2, 2011, 8:56 p.m. UTC | #2
On Wed, Nov 2, 2011 at 1:12 PM, Matthias Weisser <weisserm@arcor.de> wrote:
> Using mmap to allocate memory from the OS for RAM simulation we can use
> u-boots own malloc implementation.
>
> Signed-off-by: Matthias Weisser <weisserm@arcor.de>
> ---
> Changes in V2:
>  Removed the address hint for mmap
>  Removed the special handling of dlmalloc in common
>  Set gd->bd->bi_dram[0].start to 0 again
>
>  arch/sandbox/cpu/os.c    |    7 +++++++
>  arch/sandbox/lib/board.c |   17 ++++++++++-------
>  common/Makefile          |    2 --
>  include/os.h             |    8 ++++++++
>  4 files changed, 25 insertions(+), 9 deletions(-)
>
> diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
> index 6c175d4..05f91f2 100644
> --- a/arch/sandbox/cpu/os.c
> +++ b/arch/sandbox/cpu/os.c
> @@ -24,6 +24,7 @@
>  #include <unistd.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
> +#include <sys/mman.h>
>
>  #include <os.h>
>
> @@ -53,3 +54,9 @@ void os_exit(int exit_code)
>  {
>        exit(exit_code);
>  }
> +
> +void *os_malloc(size_t length)
> +{
> +       return mmap(NULL, length, PROT_READ | PROT_WRITE,
> +                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +}
> diff --git a/arch/sandbox/lib/board.c b/arch/sandbox/lib/board.c
> index ae5a517..d71cd6f 100644
> --- a/arch/sandbox/lib/board.c
> +++ b/arch/sandbox/lib/board.c
> @@ -45,8 +45,12 @@
>  #include <version.h>
>  #include <serial.h>
>
> +#include <os.h>
> +
>  DECLARE_GLOBAL_DATA_PTR;
>
> +static gd_t gd_mem;
> +
>  /************************************************************************
>  * Init Utilities                                                      *
>  ************************************************************************
> @@ -147,7 +151,7 @@ void board_init_f(ulong bootflag)
>        uchar *mem;
>        unsigned long addr_sp, addr, size;
>
> -       gd = malloc(sizeof(gd_t));
> +       gd = &gd_mem;
>        assert(gd);
>
>        memset((void *)gd, 0, sizeof(gd_t));
> @@ -158,7 +162,8 @@ void board_init_f(ulong bootflag)
>        }
>
>        size = CONFIG_SYS_SDRAM_SIZE;
> -       mem = malloc(size);
> +       mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
> +

Just a suggestion if you like: perhaps also assert that
CONFIG_SYS_SDRAM_SIZE is greater than TOTAL_MALLOC_LEN. Or assert
lower down at the end of board_init_f() that addr_sp is still greatrer
than mem?

>        assert(mem);
>        gd->ram_buf = mem;
>        addr = (ulong)(mem + size);
> @@ -214,11 +219,9 @@ void board_init_r(gd_t *id, ulong dest_addr)
>        post_output_backlog();
>  #endif
>
> -#if 0 /* Sandbox uses system malloc for now */
> -       /* The Malloc area is immediately below the monitor copy in DRAM */
> -       malloc_start = dest_addr - TOTAL_MALLOC_LEN;
> -       mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
> -#endif
> +       /* The Malloc area is at the top of simulated DRAM */
> +       mem_malloc_init(gd->ram_buf + gd->ram_size - TOTAL_MALLOC_LEN,
> +                       TOTAL_MALLOC_LEN);

I get:

board.c:224:4: warning: passing argument 1 of ‘mem_malloc_init’ makes
integer from pointer without a cast [enabled by default]

>
>        /* initialize environment */
>        env_relocate();
> diff --git a/common/Makefile b/common/Makefile
> index 1b672ad..919be42 100644
> --- a/common/Makefile
> +++ b/common/Makefile
> @@ -29,9 +29,7 @@ LIB   = $(obj)libcommon.o
>  ifndef CONFIG_SPL_BUILD
>  COBJS-y += main.o
>  COBJS-y += command.o
> -ifndef CONFIG_SANDBOX
>  COBJS-y += dlmalloc.o
> -endif

I think this can be removed. It was added unconditionally lower down
in the Makefile by this commit:

2d01dd9 omap: spl: fix build break due to changes in FAT

(incidentally this is what has broken sandbox).

>  COBJS-y += exports.o
>  COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
>  COBJS-y += image.o
> diff --git a/include/os.h b/include/os.h
> index 3ea6d2d..2bb2fb7 100644
> --- a/include/os.h
> +++ b/include/os.h
> @@ -71,3 +71,11 @@ int os_close(int fd);
>  * @param exit_code    exit code for U-Boot
>  */
>  void os_exit(int exit_code);
> +
> +/**
> + * Acquires some memory from the underlying os.
> + *
> + * \param length       Number of bytes to be allocated
> + * \return Pointer to length bytes or NULL on error
> + */
> +void *os_malloc(size_t length);
> --
> 1.7.4.1
>
>
Matthias Weisser Nov. 3, 2011, 5:55 p.m. UTC | #3
Am 02.11.2011 21:56, schrieb Simon Glass:
> On Wed, Nov 2, 2011 at 1:12 PM, Matthias Weisser <weisserm@arcor.de> wrote:
>> Using mmap to allocate memory from the OS for RAM simulation we can use
>> u-boots own malloc implementation.
>>
>> Signed-off-by: Matthias Weisser <weisserm@arcor.de>
>> ---
>> Changes in V2:
>>  Removed the address hint for mmap
>>  Removed the special handling of dlmalloc in common
>>  Set gd->bd->bi_dram[0].start to 0 again
>>
>>  arch/sandbox/cpu/os.c    |    7 +++++++
>>  arch/sandbox/lib/board.c |   17 ++++++++++-------
>>  common/Makefile          |    2 --
>>  include/os.h             |    8 ++++++++
>>  4 files changed, 25 insertions(+), 9 deletions(-)
>>
>> diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
>> index 6c175d4..05f91f2 100644
>> --- a/arch/sandbox/cpu/os.c
>> +++ b/arch/sandbox/cpu/os.c
>> @@ -24,6 +24,7 @@
>>  #include <unistd.h>
>>  #include <sys/types.h>
>>  #include <sys/stat.h>
>> +#include <sys/mman.h>
>>
>>  #include <os.h>
>>
>> @@ -53,3 +54,9 @@ void os_exit(int exit_code)
>>  {
>>        exit(exit_code);
>>  }
>> +
>> +void *os_malloc(size_t length)
>> +{
>> +       return mmap(NULL, length, PROT_READ | PROT_WRITE,
>> +                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> +}
>> diff --git a/arch/sandbox/lib/board.c b/arch/sandbox/lib/board.c
>> index ae5a517..d71cd6f 100644
>> --- a/arch/sandbox/lib/board.c
>> +++ b/arch/sandbox/lib/board.c
>> @@ -45,8 +45,12 @@
>>  #include <version.h>
>>  #include <serial.h>
>>
>> +#include <os.h>
>> +
>>  DECLARE_GLOBAL_DATA_PTR;
>>
>> +static gd_t gd_mem;
>> +
>>  /************************************************************************
>>  * Init Utilities                                                      *
>>  ************************************************************************
>> @@ -147,7 +151,7 @@ void board_init_f(ulong bootflag)
>>        uchar *mem;
>>        unsigned long addr_sp, addr, size;
>>
>> -       gd = malloc(sizeof(gd_t));
>> +       gd = &gd_mem;
>>        assert(gd);
>>
>>        memset((void *)gd, 0, sizeof(gd_t));
>> @@ -158,7 +162,8 @@ void board_init_f(ulong bootflag)
>>        }
>>
>>        size = CONFIG_SYS_SDRAM_SIZE;
>> -       mem = malloc(size);
>> +       mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
>> +
> 
> Just a suggestion if you like: perhaps also assert that
> CONFIG_SYS_SDRAM_SIZE is greater than TOTAL_MALLOC_LEN. Or assert
> lower down at the end of board_init_f() that addr_sp is still greatrer
> than mem?

I will leave out this point for a separate patch.

>>        assert(mem);
>>        gd->ram_buf = mem;
>>        addr = (ulong)(mem + size);
>> @@ -214,11 +219,9 @@ void board_init_r(gd_t *id, ulong dest_addr)
>>        post_output_backlog();
>>  #endif
>>
>> -#if 0 /* Sandbox uses system malloc for now */
>> -       /* The Malloc area is immediately below the monitor copy in DRAM */
>> -       malloc_start = dest_addr - TOTAL_MALLOC_LEN;
>> -       mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
>> -#endif
>> +       /* The Malloc area is at the top of simulated DRAM */
>> +       mem_malloc_init(gd->ram_buf + gd->ram_size - TOTAL_MALLOC_LEN,
>> +                       TOTAL_MALLOC_LEN);
> 
> I get:
> 
> board.c:224:4: warning: passing argument 1 of ‘mem_malloc_init’ makes
> integer from pointer without a cast [enabled by default]

Fixed. Will send an updated version.

>>
>>        /* initialize environment */
>>        env_relocate();
>> diff --git a/common/Makefile b/common/Makefile
>> index 1b672ad..919be42 100644
>> --- a/common/Makefile
>> +++ b/common/Makefile
>> @@ -29,9 +29,7 @@ LIB   = $(obj)libcommon.o
>>  ifndef CONFIG_SPL_BUILD
>>  COBJS-y += main.o
>>  COBJS-y += command.o
>> -ifndef CONFIG_SANDBOX
>>  COBJS-y += dlmalloc.o
>> -endif
> 
> I think this can be removed. It was added unconditionally lower down
> in the Makefile by this commit:
> 
> 2d01dd9 omap: spl: fix build break due to changes in FAT
> 
> (incidentally this is what has broken sandbox).

Will do.

Matthias
diff mbox

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 6c175d4..05f91f2 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -24,6 +24,7 @@ 
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 
 #include <os.h>
 
@@ -53,3 +54,9 @@  void os_exit(int exit_code)
 {
 	exit(exit_code);
 }
+
+void *os_malloc(size_t length)
+{
+	return mmap(NULL, length, PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+}
diff --git a/arch/sandbox/lib/board.c b/arch/sandbox/lib/board.c
index ae5a517..d71cd6f 100644
--- a/arch/sandbox/lib/board.c
+++ b/arch/sandbox/lib/board.c
@@ -45,8 +45,12 @@ 
 #include <version.h>
 #include <serial.h>
 
+#include <os.h>
+
 DECLARE_GLOBAL_DATA_PTR;
 
+static gd_t gd_mem;
+
 /************************************************************************
  * Init Utilities							*
  ************************************************************************
@@ -147,7 +151,7 @@  void board_init_f(ulong bootflag)
 	uchar *mem;
 	unsigned long addr_sp, addr, size;
 
-	gd = malloc(sizeof(gd_t));
+	gd = &gd_mem;
 	assert(gd);
 
 	memset((void *)gd, 0, sizeof(gd_t));
@@ -158,7 +162,8 @@  void board_init_f(ulong bootflag)
 	}
 
 	size = CONFIG_SYS_SDRAM_SIZE;
-	mem = malloc(size);
+	mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
+
 	assert(mem);
 	gd->ram_buf = mem;
 	addr = (ulong)(mem + size);
@@ -214,11 +219,9 @@  void board_init_r(gd_t *id, ulong dest_addr)
 	post_output_backlog();
 #endif
 
-#if 0 /* Sandbox uses system malloc for now */
-	/* The Malloc area is immediately below the monitor copy in DRAM */
-	malloc_start = dest_addr - TOTAL_MALLOC_LEN;
-	mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
-#endif
+	/* The Malloc area is at the top of simulated DRAM */
+	mem_malloc_init(gd->ram_buf + gd->ram_size - TOTAL_MALLOC_LEN,
+			TOTAL_MALLOC_LEN);
 
 	/* initialize environment */
 	env_relocate();
diff --git a/common/Makefile b/common/Makefile
index 1b672ad..919be42 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -29,9 +29,7 @@  LIB	= $(obj)libcommon.o
 ifndef CONFIG_SPL_BUILD
 COBJS-y += main.o
 COBJS-y += command.o
-ifndef CONFIG_SANDBOX
 COBJS-y += dlmalloc.o
-endif
 COBJS-y += exports.o
 COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += image.o
diff --git a/include/os.h b/include/os.h
index 3ea6d2d..2bb2fb7 100644
--- a/include/os.h
+++ b/include/os.h
@@ -71,3 +71,11 @@  int os_close(int fd);
  * @param exit_code	exit code for U-Boot
  */
 void os_exit(int exit_code);
+
+/**
+ * Acquires some memory from the underlying os.
+ *
+ * \param length	Number of bytes to be allocated
+ * \return Pointer to length bytes or NULL on error
+ */
+void *os_malloc(size_t length);