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

Simon Glass sjg at chromium.org
Tue Nov 1 17:42:32 CET 2011


Hi Matthias,

On Tue, Nov 1, 2011 at 7:07 AM, Matthias Weisser <weisserm at arcor.de> wrote:
> Using mmap we can simulate RAM at a specific address. This also
> eliminated the use of system malloc function.
>
> Signed-off-by: Matthias Weisser <weisserm at arcor.de>

Thanks for the patch!

> ---
>  arch/sandbox/cpu/os.c     |    9 +++++++++
>  arch/sandbox/lib/board.c  |   19 +++++++++++--------
>  include/configs/sandbox.h |    1 +
>  include/os.h              |   13 +++++++++++++
>  4 files changed, 34 insertions(+), 8 deletions(-)
>
> diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
> index 6c175d4..cd2fb05 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,11 @@ void os_exit(int exit_code)
>  {
>        exit(exit_code);
>  }
> +
> +void *os_mmap(void *addr, size_t length, int prot, int flags, int fd,
> +               off_t offset)

I would prefer that you define something like os_malloc() instead, and
your implementation use mmap(). After all your flags cannot sensibly
be used elsewhere in U-Boot, and we might as well define only the
feature that we actually want to use. I don't see a general use for
mmap().

> +{
> +       return mmap((void *)addr, 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..0912a8b 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                                                      *
>  ************************************************************************
> @@ -112,7 +116,7 @@ typedef int (init_fnc_t) (void);
>
>  void __dram_init_banksize(void)
>  {
> -       gd->bd->bi_dram[0].start = 0;
> +       gd->bd->bi_dram[0].start = (ulong) gd->ram_buf;
>        gd->bd->bi_dram[0].size =  gd->ram_size;
>  }
>
> @@ -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_mmap((void *)CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_SDRAM_SIZE,

What is the base used for here?

> +                       0, 0, -1, 0);
>        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/include/configs/sandbox.h b/include/configs/sandbox.h
> index 0230256..ce4675d 100644
> --- a/include/configs/sandbox.h
> +++ b/include/configs/sandbox.h
> @@ -60,6 +60,7 @@
>  #define CONFIG_PHYS_64BIT
>
>  /* Size of our emulated memory */
> +#define CONFIG_SYS_SDRAM_BASE          0x20000000
>  #define CONFIG_SYS_SDRAM_SIZE          (128 << 20)
>
>  #define CONFIG_BAUDRATE                        115200
> diff --git a/include/os.h b/include/os.h
> index 3ea6d2d..ed4cd4f 100644
> --- a/include/os.h
> +++ b/include/os.h
> @@ -71,3 +71,16 @@ int os_close(int fd);
>  * @param exit_code    exit code for U-Boot
>  */
>  void os_exit(int exit_code);
> +
> +/**
> + * Access to the OS mmap() system call. Currently all
> + *
> + * \param addr         Prefered address of the mapping
> + * \param length       Length in bytes
> + * \param prot         Ignored. Always PROT_READ | PROT_WRITE
> + * \param flags                Ignored. Always MAP_PRIVATE | MAP_ANONYMOUS
> + * \param fd           Ignored. Always -1
> + * \param offset       Ignored. Always 0

Hmm please see above - we don't have to mimic the POSIX API exactly.
The preferred address doesn't really matter since in sandbox it will
be accessed as an offset from the start. Apart from length the other
parameters should probably not be included.

Regards,
Simon

> + */
> +void *os_mmap(void *addr, size_t length, int prot, int flags, int fd,
> +               off_t offset);
> --
> 1.7.4.1
>
>


More information about the U-Boot mailing list