public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Thomas Chou <thomas@wytron.com.tw>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3] Fix board init code to use a valid C runtime environment
Date: Sun, 15 Nov 2015 10:11:02 +0800	[thread overview]
Message-ID: <5647E9B6.3050404@wytron.com.tw> (raw)
In-Reply-To: <20151114160608.3cda262c@lilith>

Hi Albert,

On 2015?11?14? 23:06, Albert ARIBAUD wrote:
> Hello Thomas,
>
> On Sat, 14 Nov 2015 14:23:31 +0800, Thomas Chou <thomas@wytron.com.tw>
> wrote:
>> Hi Albert,
>>
>> On 2015?11?13? 22:43, Albert ARIBAUD wrote:
>>> +/*
>>> + * Initialize reserved space (which has been safely allocated on the C
>>> + * stack from the C runtime environment handling code).
>>> + *
>>> + * Do not use 'gd->' until arch_setup_gd() has been called!
>>> + */
>>> +
>>> +void board_init_f_init_reserve(ulong base)
>>>    {
>>>    	struct global_data *gd_ptr;
>>>    #ifndef _USE_MEMCPY
>>>    	int *ptr;
>>>    #endif
>>>
>>> -	/* Leave space for the stack we are running with now */
>>> -	top -= 0x40;
>>> +	/*
>>> +	 * clear GD entirely
>>> +	 */
>>>
>>> -	top -= sizeof(struct global_data);
>>> -	top = ALIGN(top, 16);
>>> -	gd_ptr = (struct global_data *)top;
>>> +	gd_ptr = (struct global_data *)base;
>>> +	/* go down one GD plus 16-byte alignment */
>>> +	base += roundup(sizeof(struct global_data), 16);
>>
>> Maybe it can keep the original order, top--gd--malloc--base.
>
> Actually, I inverted the two between v2 and v3 for a reason, but I
> should have made it more explicit.
>
> This reason is, for architectures which may not be able to write to
> gd from arch_setup_gd(), the C runtime environment handling code (crt0.S
> for ARM, etc) needs a way to easily find our where gd_ptr was allocated.
>
> So, it was either:
>
> - keeping *gd_ptr above the malloc arena and having to pass gd_ptr back
>    to board_init_f_reserve_size's caller; but board_init_f_reserve_size
>    already returns a value back to its caller, and returning two values
>    from a C function takes resources (a pointer argument and a memory
>    location at least);
>
> or:
>
> - putting *gd_ptr at the base of the allocated space, below the malloc
>    arena, so that the C runtime environment handling code knows where to
>    point gd to without incurring the cost of passing additional results
>    back from board_init_f_reserve_init.
>
> This is the reason why I placed the global data below the malloc arena.
>
> [besides, considering that our malloc implementation starts from the
> base of the arena and allocates upward, it is (admittedly very slightly)
> safer to have GD placed below it than above.]

Agree.

>
> I will add a note in v4 about the reason for this (re)ordering of
> allocations.
>
>>> +	base += CONFIG_SYS_MALLOC_F_LEN;
>>
>> Useless statement.
>
> Right now it is indeed useless. However, if and when some other space
> gets reserved and initialized besides GD and the malloc arena, this
> statement will be needed before allocating / accessing the space
> allocated above the malloc arena.
>
> Note that this statement will not result in any useless *code*, as the
> compiler's optimizer detects that base remains unused after it, and
> thus removes it from the generated object code.
>
> So I'll insist on keeping this statement, although I will add a comment
> next to it explaining why it matters despite not having any visible
> effect.
>

OK.

Best regards,
Thomas

      reply	other threads:[~2015-11-15  2:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-13 14:43 [U-Boot] [PATCH v3] Fix board init code to use a valid C runtime environment Albert ARIBAUD
2015-11-14  2:29 ` Simon Glass
2015-11-14 14:36   ` Albert ARIBAUD
2015-11-14 15:41     ` Simon Glass
2015-11-15 11:44       ` Albert ARIBAUD
2015-11-14  6:23 ` Thomas Chou
2015-11-14 15:06   ` Albert ARIBAUD
2015-11-15  2:11     ` Thomas Chou [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5647E9B6.3050404@wytron.com.tw \
    --to=thomas@wytron.com.tw \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox