• Denys Vlasenko's avatar
    x86/asm/entry/64: Implement better check for canonical addresses · 17be0aec
    Denys Vlasenko authored
    This change makes the check exact (no more false positives
    on "negative" addresses).
    
    Andy explains:
    
     "Canonical addresses either start with 17 zeros or 17 ones.
    
      In the old code, we checked that the top (64-47) = 17 bits were all
      zero.  We did this by shifting right by 47 bits and making sure that
      nothing was left.
    
      In the new code, we're shifting left by (64 - 48) = 16 bits and then
      signed shifting right by the same amount, this propagating the 17th
      highest bit to all positions to its left.  If we get the same value we
      started with, then we're good to go."
    
    While it isn't really important to be fully correct here -
    almost all addresses we'll ever see will be userspace ones,
    but OTOH it looks to be cheap enough: the new code uses
    two more ALU ops but preserves %rcx, allowing to not
    reload it from pt_regs->cx again.
    
    On disassembly level, the changes are:
    
      cmp %rcx,0x80(%rsp) -> mov 0x80(%rsp),%r11; cmp %rcx,%r11
      shr $0x2f,%rcx      -> shl $0x10,%rcx; sar $0x10,%rcx; cmp %rcx,%r11
      mov 0x58(%rsp),%rcx -> (eliminated)
    Signed-off-by: default avatarDenys Vlasenko <dvlasenk@redhat.com>
    Acked-by: default avatarAndy Lutomirski <luto@kernel.org>
    Cc: Alexei Starovoitov <ast@plumgrid.com>
    Cc: Andy Lutomirski <luto@amacapital.net>
    Cc: Borislav Petkov <bp@alien8.de>
    Cc: Brian Gerst <brgerst@gmail.com>
    Cc: Frederic Weisbecker <fweisbec@gmail.com>
    Cc: H. Peter Anvin <hpa@zytor.com>
    Cc: Kees Cook <keescook@chromium.org>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: Oleg Nesterov <oleg@redhat.com>
    Cc: Steven Rostedt <rostedt@goodmis.org>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: Will Drewry <wad@chromium.org>
    Link: http://lkml.kernel.org/r/1429633649-20169-1-git-send-email-dvlasenk@redhat.com
    [ Changelog massage. ]
    Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
    17be0aec
entry_64.S 42.1 KB