Re: [PATCH v2 0/6] x86: Clean up fast syscall return validation

From: H. Peter Anvin
Date: Fri Oct 06 2023 - 20:03:46 EST


On 10/6/23 11:59, H. Peter Anvin wrote:

Incidentally, it is possible to save one instruction and use only *one* alternative immediate:

    leaq (%rax,%rax),%rdx
    xorq %rax,%rdx
    shrq $(63 - LA),%rdx        # Yes, 63, not 64
    # ZF=1 if canonical

This works because if bit [x] is set in the output, then bit [x] and [x-1] in the input are different (bit [-1] considered to be zero); and by definition a bit is canonical if and only if all the bits [63:LA] are identical, thus bits [63:LA+1] in the output must all be zero.


Yes, I'm a doofus. Bits [63:LA-1] must be identical, so 64 is correct :$)

-hpa