Skip to content
  • Ricardo Neri's avatar
    x86/insn-eval: Add utility functions to get segment selector · adc37e20
    Ricardo Neri authored
    commit 32d0b953
    
     upstream.
    
    [note, only the inat.h portion, to get objtool back in sync - gregkh]
    
    b0caa8c8c6bbc422bc3c32b64852d6d618f32b49 Mon Sep 17 00:00:00 2001
    When computing a linear address and segmentation is used, we need to know
    the base address of the segment involved in the computation. In most of
    the cases, the segment base address will be zero as in USER_DS/USER32_DS.
    However, it may be possible that a user space program defines its own
    segments via a local descriptor table. In such a case, the segment base
    address may not be zero. Thus, the segment base address is needed to
    calculate correctly the linear address.
    
    If running in protected mode, the segment selector to be used when
    computing a linear address is determined by either any of segment override
    prefixes in the instruction or inferred from the registers involved in the
    computation of the effective address; in that order. Also, there are cases
    when the segment override prefixes shall be ignored (i.e., code segments
    are always selected by the CS segment register; string instructions always
    use the ES segment register when using rDI register as operand). In long
    mode, segment registers are ignored, except for FS and GS. In these two
    cases, base addresses are obtained from the respective MSRs.
    
    For clarity, this process can be split into four steps (and an equal
    number of functions): determine if segment prefixes overrides can be used;
    parse the segment override prefixes, and use them if found; if not found
    or cannot be used, use the default segment registers associated with the
    operand registers. Once the segment register to use has been identified,
    read its value to obtain the segment selector.
    
    The method to obtain the segment selector depends on several factors. In
    32-bit builds, segment selectors are saved into a pt_regs structure
    when switching to kernel mode. The same is also true for virtual-8086
    mode. In 64-bit builds, segmentation is mostly ignored, except when
    running a program in 32-bit legacy mode. In this case, CS and SS can be
    obtained from pt_regs. DS, ES, FS and GS can be read directly from
    the respective segment registers.
    
    In order to identify the segment registers, a new set of #defines is
    introduced. It also includes two special identifiers. One of them
    indicates when the default segment register associated with instruction
    operands shall be used. Another one indicates that the contents of the
    segment register shall be ignored; this identifier is used when in long
    mode.
    
    Improvements-by: default avatarBorislav Petkov <bp@suse.de>
    Signed-off-by: default avatarRicardo Neri <ricardo.neri-calderon@linux.intel.com>
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Reviewed-by: default avatarBorislav Petkov <bp@suse.de>
    Cc: "Michael S. Tsirkin" <mst@redhat.com>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Dave Hansen <dave.hansen@linux.intel.com>
    Cc: ricardo.neri@intel.com
    Cc: Adrian Hunter <adrian.hunter@intel.com>
    Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
    Cc: Huang Rui <ray.huang@amd.com>
    Cc: Qiaowei Ren <qiaowei.ren@intel.com>
    Cc: Shuah Khan <shuah@kernel.org>
    Cc: Kees Cook <keescook@chromium.org>
    Cc: Jonathan Corbet <corbet@lwn.net>
    Cc: Jiri Slaby <jslaby@suse.cz>
    Cc: Dmitry Vyukov <dvyukov@google.com>
    Cc: "Ravi V. Shankar" <ravi.v.shankar@intel.com>
    Cc: Chris Metcalf <cmetcalf@mellanox.com>
    Cc: Brian Gerst <brgerst@gmail.com>
    Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
    Cc: Andy Lutomirski <luto@kernel.org>
    Cc: Colin Ian King <colin.king@canonical.com>
    Cc: Chen Yucong <slaoub@gmail.com>
    Cc: Adam Buchbinder <adam.buchbinder@gmail.com>
    Cc: Vlastimil Babka <vbabka@suse.cz>
    Cc: Lorenzo Stoakes <lstoakes@gmail.com>
    Cc: Masami Hiramatsu <mhiramat@kernel.org>
    Cc: Paolo Bonzini <pbonzini@redhat.com>
    Cc: Andrew Morton <akpm@linux-foundation.org>
    Cc: Thomas Garnier <thgarnie@google.com>
    Link: https://lkml.kernel.org/r/1509135945-13762-14-git-send-email-ricardo.neri-calderon@linux.intel.com
    
    
    Cc: Ingo Molnar <mingo@kernel.org>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    adc37e20