Patchwork cld before call

login
register
about
Submitter Carl-Daniel Hailfinger
Date 2009-07-29 23:51:30
Message ID <4A70E082.5010000@gmx.net>
Download mbox | patch
Permalink /patch/83/
State Accepted
Headers show

Comments

Carl-Daniel Hailfinger - 2009-07-29 23:51:30
Hi Segher,

my apologies for the missing context.
The original mail starting this thread is at
http://www.coreboot.org/pipermail/coreboot/2009-July/050984.html

and the patch in there was:



Regards,
Carl-Daniel

On 29.07.2009 13:08, Carl-Daniel Hailfinger wrote:
> On 29.07.2009 12:51, Carl-Daniel Hailfinger wrote:
>   
>> On 29.07.2009 05:24, ron minnich wrote:
>>   
>>     
>>> my understanding of all this is:
>>> 1. if you want it set a certain way, you have to set it
>>> 2. don't ever assume it has any particular value.
>>>   
>>>     
>>>       
>> There is an excellent LWN.net article about this:
>> http://lwn.net/Articles/272048/
>>
>> "GCC relies on the ABI mandate that the direction flag is cleared before
>> entry to a function"
>>
>> That means if you call the payload via a pointer, gcc will make sure the
>> direction flag is cleared (either by clearing it directly before the
>> call or by never changing it after the start of the program).
>>
>> AFAIK calling a function via inline asm is not something gcc can see, so
>> this special case might be an exception. Then again, it's pretty
>> unlikely that gcc sets the direction flag for some operations and defers
>> clearing to the last possible moment.
>>   
>>     
>
> Turns out gcc developers disagreed with each other about direction flag
> guarantees for inline asm. The following is an attempt at summarizing
> the discussion on the gcc mailing list linked from the lwn article.
>
> Michael Matz said there are no guarantees for inline asm, but he also
> said that std is not emitted lazily, so having cld inside inline asm
> will not break any code afterwards, i.e. code after an inline asm does
> never depend on the DF being set.
> Chris Lattner said it's reasonable for inline asm to expect the DF to be
> cleared.
> Richard Guenther said inline asm shouldn't make assumptions about DF.
> Jan Hubicka said gcc does not emit std at the moment.
>
> Anyone up for reposting that question to the gcc list?
>
> Regards,
> Carl-Daniel
>
>

Patch

Index: arch/x86/stage1.c
===================================================================
--- arch/x86/stage1.c	(revision 1174)
+++ arch/x86/stage1.c	(working copy)
@@ -172,7 +172,7 @@ 
 static int run_address_multiboot(void *f, struct multiboot_info *mbi)
 {
 	int ret, dummy;
-	__asm__ __volatile__ ("call *%4" : "=a" (ret), "=c" (dummy) : "a" (MB_MAGIC2), "b" (mbi), "c" (f) : "edx", "memory");
+	__asm__ __volatile__ ("cld; call *%4" : "=a" (ret), "=c" (dummy) : "a" (MB_MAGIC2), "b" (mbi), "c" (f) : "edx", "memory");
 	return ret;
 }