[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v9 2/5] x86/asm, x86/boot: expose inline memcmp()



On 2026-09-08 12:32, Borislav Petkov wrote:
> On Tue, Sep 08, 2026 at 03:04:53PM -0300, Mauricio Faria de Oliveira wrote:
>> Boris, perhaps the approach here could be changed to add an actual
>> memcmp()-like inline implementation (e.g., as provided in a previous
>> revision, without return value differences), or continue with the
>> memeq()-like memcmp() from arch/x86/boot/ but rename it to memeq() ?
> 
> Nah, new functionality is not needed. We can add it later when it is really 
> needed.
> 

I personally think it would be a good reason to explicitly rename it memeq(),
after all it indicates what it actually *does*.

        bool memeq(const void *m1, const void *m2, size_t n);

Note, however, that the sense of the return value is opposite -- true means
equal, so !memcmp(...) needs to be replaced with memeq(...) and vice versa.

The other issue is when gcc/clang wants to call memcmp() out of line. Although
a theoretical concern, there really isn't any reason not to DTRT there since
there is only one instance in the code, ever.

Here is an out-of-line compact memcmp() which works for both 16/32 and 64 bits:

int memcmp(const void *s1, const void *s2, size_t len)
{
        int rv;

        asm volatile("xor %0,%0 ; "
                     "test %3, %3 ; "   /* Handle len == 0 correctly */
                     "repe cmpsb ; "}g
                     "setz %b0 ; "
                     "sbb $0, %0"
                : "=&r" (rv), "+D" (s1), "+S" (s2), "+c" (len)
                : : "cc", "memory");

        return rv;
}

On 64 bits it compiles to:

0000000000000000 <memcmp>:
   0:   48 89 d1                mov    %rdx,%rcx
   3:   31 d2                   xor    %edx,%edx
   5:   31 c0                   xor    %eax,%eax
   7:   f3 a6                   repz cmpsb (%rdi),(%rsi)
   9:   0f 97 c2                seta   %dl
   c:   0f 92 c0                setb   %al
   f:   29 d0                   sub    %edx,%eax
  11:   c3                      ret

On 32 bits (-mregparm=3):

00000000 <memcmp>:
   0:   57                      push   %edi
   1:   56                      push   %esi
   2:   89 c7                   mov    %eax,%edi
   4:   89 d6                   mov    %edx,%esi
   6:   31 d2                   xor    %edx,%edx
   8:   31 c0                   xor    %eax,%eax
   a:   f3 a6                   repz cmpsb %es:(%edi),%ds:(%esi)
   c:   0f 97 c2                seta   %dl
   f:   0f 92 c0                setb   %al
  12:   29 d0                   sub    %edx,%eax
  14:   5e                      pop    %esi
  15:   5f                      pop    %edi
  16:   c3                      ret


On 16 bits:

00000000 <memcmp>:
   0:   66 57                   push   %edi
   2:   66 56                   push   %esi
   4:   66 89 c7                mov    %eax,%edi
   7:   66 89 d6                mov    %edx,%esi
   a:   66 31 d2                xor    %edx,%edx
   d:   66 31 c0                xor    %eax,%eax
  10:   f3 a6                   repz cmpsb %es:(%di),%ds:(%si)
  12:   0f 97 c2                seta   %dl
  15:   0f 92 c0                setb   %al
  18:   66 29 d0                sub    %edx,%eax
  1b:   66 5e                   pop    %esi
  1d:   66 5f                   pop    %edi
  1f:   66 c3                   retl

        -hpa




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.