x86 string/memory inline functions
Apparently, the contents of xen/include/asm-x86/string.h have been derived from Linux i386 code, with a few adjustments for x86-64, and with some memory clobbers removed from functions where the memory is only read. That latter fact is what I'm curious about: Without these clobbers (and without appropriate input constraints added), how is the compiler supposed to know that the memory pointed to by some of the other inputs is being accessed?
I'm specifically running into a problem where I wanted to insert an extra strcmp() in cmdline_parse() (in xen/common/kernel.c) after the zero termination of the entire option and the call to strchr() to determine whether the option has a value specified. gcc 3.3.1 (SuSE version), which I'm using on one of my build machines, reorders things so the zero termination happens only after the newly added strcmp(), clearly because it can't derive that strcmp() relies on this very memory being up-to-date. Surprisingly, using 3.4.4 or 4.0.3 (gnu.org version) doesn't yield the same problem, but I'm nevertheless convinced this is not a compiler bug in 3.3.1.
So there are basically two questions:
1) Why were the (questionable) inline versions from i386 Linux chosen over just using the gcc intrinsics (as x86-64 Linux does, except for a special case of memcpy())?
2) Why were the memory clobbers removed without at least replacing them with appropriate input constraints?
Thanks, Jan
Quoted 2 lines · Jan Beulich
1) Why were the (questionable) inline versions from i386 Linux chosen over just using the gcc intrinsics (as x86-64 Linux does, except for a special case of
memcpy())?
Intrinsics are a total pain. Sometimes the compiler inlines, sometimes it doesn't. Sometimes it emits the __builtin_foo symbol, sometime it emits foo. Sometime when the function __builtin_foo is defined in string.c it gets the name __builtin_foo, but sometimes it gets the name foo. Getting this to work for a range of compiler versions on i386 (that's where I see the wide range of behaviours) would be hassle.
The best solution is just to remove the arch-specific definitions. None of the uses in Xen are performance critical.
Quoted 1 line
2) Why were the memory clobbers removed without at least replacing them with appropriate input constraints?
Maybe I was having a bad day. :-)
-- Keir
Quoted 4 lines
Quoted 2 lines
2) Why were the memory clobbers removed without at least replacing them with appropriate input constraints?
Maybe I was having a bad day. :-)
In case you still want to leave them in - here's a patch to add the missing constraints.
Jan
xen/include/asm-x86/string.h renamed from xen/include/asm-x86/string.h.0 +8 -5
@@ -90,7 +90,7 @@ static inline int strcmp(const char *cs, " or $1,%%al \n" "3: \n" : "=a" (__res), "=&S" (d0), "=&D" (d1) - : "1" (cs), "2" (ct) ); + : "1" (cs), "2" (ct), "m" (*cs), "m" (*ct) );return __res; }
@@ -113,7 +113,7 @@ static inline int strncmp(const char *cs " or $1,%%al \n" "4: \n" : "=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2) - : "1" (cs), "2" (ct), "3" (count) ); + : "1" (cs), "2" (ct), "3" (count), "m" (*cs), "m" (*ct) );return __res; }
@@ -132,7 +132,8 @@ static inline char *strchr(const char *s " mov $1,%1 \n" "2: mov %1,%0 \n" " dec %0 \n" - : "=a" (__res), "=&S" (d0) : "1" (s), "0" (c) ); + : "=a" (__res), "=&S" (d0) + : "1" (s), "0" (c), "m" (*s) );return __res; }
@@ -145,7 +146,8 @@ static inline size_t strlen(const char * " repne ; scasb \n" " notl %0 \n" " decl %0 \n" - : "=c" (__res), "=&D" (d0) : "1" (s), "a" (0), "0" (0xffffffffUL) ); + : "=c" (__res), "=&D" (d0) + : "1" (s), "a" (0), "0" (0xffffffffUL), "m" (*s) );return __res; }
@@ -270,7 +272,8 @@ static inline void *memchr(const void *c " je 1f \n" " mov $1,%0 \n" "1: dec %0 \n" - : "=D" (__res), "=&c" (d0) : "a" (c), "0" (cs), "1" (count) ); + : "=D" (__res), "=&c" (d0) + : "a" (c), "0" (cs), "1" (count), "m" (*(const unsigned char *)cs) );return __res; }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 2 lines
The best solution is just to remove the arch-specific definitions. None of the uses in Xen are performance critical.
I see you removed all the str* stuff. memcmp, however, suffers from the same original problem, and I would doubt it is used in performance critical code if none of the str* functions are.
Jan
Quoted 5 lines · Jan Beulich
Quoted 2 lines
The best solution is just to remove the arch-specific definitions. None of the uses in Xen are performance critical.
I see you removed all the str* stuff. memcmp, however, suffers from the same original problem, and I would doubt it is used in performance critical code if none of the str* functions are.
We use the compiler intrinsic for memcmp, and have no inline asm version.
-- Keir
Quoted 5 lines · Keir Fraser
Quoted 3 lines
I see you removed all the str* stuff. memcmp, however, suffers from the same original problem, and I would doubt it is used in performance critical code if none of the str* functions are.
We use the compiler intrinsic for memcmp, and have no inline asm version.
Oh, ITYM memchr(). You're absolutely right. It doesn't have *any* users so I'll just remove the incorrect inline asm version.
-- Keir
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Keir Fraser <[email protected]> 26.05.06 14:19 >>>
Quoted 4 lines · Jan Beulich
Quoted 3 lines
The best solution is just to remove the arch-specific definitions. None
of the uses in Xen are performance critical.
Quoted 6 lines
Quoted 3 lines
I see you removed all the str* stuff. memcmp, however, suffers from the same original problem, and I would doubt it is
used in performance critical code if none of the str* functions are.We use the compiler intrinsic for memcmp, and have no inline asm version.
Sorry, I meant memchr().
Jan