• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
      • X86isa
        • Program-execution
        • Sdm-instruction-set-summary
        • Tlb
        • Running-linux
        • Introduction
        • Asmtest
        • X86isa-build-instructions
        • Publications
        • Contributors
        • Machine
          • X86isa-state
          • Syscalls
          • Cpuid
          • Linear-memory
          • Rflag-specifications
          • Characterizing-undefined-behavior
          • Top-level-memory
          • App-view
          • X86-decoder
          • Physical-memory
          • Decoding-and-spec-utils
            • Read-operands-and-write-results
            • Effective-address-computations
            • Select-operand-size
            • Instruction-pointer-operations
            • Stack-pointer-operations
            • Select-segment-register
            • Prefix-modrm-sib-decoding
              • Modr/m-decoding
              • Mandatory-prefixes-computation
                • Modr/m-detection
                • Legacy-prefixes-decoding
                • Compute-mandatory-prefix-for-three-byte-opcode
                • 64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                • 64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
                • 32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                • 32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
                • 64-bit-compute-mandatory-prefix-for-two-byte-opcode
                • 32-bit-compute-mandatory-prefix-for-two-byte-opcode
                • Instructions-with-mandatory-prefixes
                • Compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                • Compute-mandatory-prefix-for-0f-38-three-byte-opcode
                • Compute-mandatory-prefix-for-two-byte-opcode
              • Select-address-size
              • Rex-byte-from-vex-prefixes
              • Check-instruction-length
              • Error-objects
              • Rip-guard-okp
              • Sib-decoding
            • Instructions
            • Register-readers-and-writers
            • X86-modes
            • Segmentation
            • Other-non-deterministic-computations
            • Environment
            • Paging
          • Implemented-opcodes
          • To-do
          • Proof-utilities
          • Peripherals
          • Model-validation
          • Modelcalls
          • Concrete-simulation-examples
          • Utils
          • Debugging-code-proofs
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Prefix-modrm-sib-decoding

    Mandatory-prefixes-computation

    Picking a mandatory prefix

    When should we interpret a SIMD prefix (66, F2, F3) as the mandatory prefix for a given opcode in the two- and three-byte opcode maps?

    Here are some decoding rules for SIMD mandatory prefixes; note that these do NOT apply for VEX/EVEX-encoded instructions because the mandatory prefixes are explicitly stated there. All the examples listed below are from Intel's XED (x86 Encoder Decoder: https://intelxed.github.io/).

    1. For opcodes that can take mandatory prefixes, 66 is ignored when F2/F3 are present. Also, a mandatory prefix does not have to immediately precede the opcode byte --- see (4) below.

      Examples:

      (1) xed -64 -d   660f6f00     ;; movdqa xmm0, xmmword ptr [rax] 
      (2) xed -64 -d   f30f6f00     ;; movdqu xmm0, xmmword ptr [rax] 
      (3) xed -64 -d 66f30f6f00     ;; movdqu xmm0, xmmword ptr [rax] (same as (2)) 
      (4) xed -64 -d f3660f6f00     ;; movdqu xmm0, xmmword ptr [rax] (same as (2)) 
       
      
    2. For opcodes that can take mandatory prefixes, the presence of an unsupported SIMD prefix translates to a reserved instruction; such a prefix does NOT act as a modifier prefix.

      Examples: Opcode 0f 6b has a no-prefix form and 66 mandatory prefix form. When used with f3, it leads to an error; see (3) below.

      (1) xed -64 -d     0f6b00     ;; packssdw mmx0, qword ptr [rax] 
      (2) xed -64 -d   660f6b00     ;; packssdw xmm0, xmmword ptr [rax] 
      (3) xed -64 -d f3660f6b00     ;; GENERAL_ERROR Could not decode... 
       
      

    Definitions and Theorems

    Function: 64-bit-compute-mandatory-prefix-for-two-byte-opcode$inline

    (defun 64-bit-compute-mandatory-prefix-for-two-byte-opcode$inline
           (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let ((__function__
                '64-bit-compute-mandatory-prefix-for-two-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '64-bit-mode-two-byte-f3-ok
                                *64-bit-mode-two-byte-f3-ok-ar* opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '64-bit-mode-two-byte-f2-ok
                                *64-bit-mode-two-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '64-bit-mode-two-byte-66-ok
                           *64-bit-mode-two-byte-66-ok-ar* opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-64-bit-compute-mandatory-prefix-for-two-byte-opcode

    (defthm
     return-type-of-64-bit-compute-mandatory-prefix-for-two-byte-opcode
     (b* ((mandatory-prefix (64-bit-compute-mandatory-prefix-for-two-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: 32-bit-compute-mandatory-prefix-for-two-byte-opcode$inline

    (defun 32-bit-compute-mandatory-prefix-for-two-byte-opcode$inline
           (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let ((__function__
                '32-bit-compute-mandatory-prefix-for-two-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '32-bit-mode-two-byte-f3-ok
                                *32-bit-mode-two-byte-f3-ok-ar* opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '32-bit-mode-two-byte-f2-ok
                                *32-bit-mode-two-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '32-bit-mode-two-byte-66-ok
                           *32-bit-mode-two-byte-66-ok-ar* opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-32-bit-compute-mandatory-prefix-for-two-byte-opcode

    (defthm
     return-type-of-32-bit-compute-mandatory-prefix-for-two-byte-opcode
     (b* ((mandatory-prefix (32-bit-compute-mandatory-prefix-for-two-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: 64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline

    (defun
     64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
     (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let
      ((__function__
          '64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '64-bit-mode-0f-38-three-byte-f3-ok
                                *64-bit-mode-0f-38-three-byte-f3-ok-ar*
                                opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '64-bit-mode-0f-38-three-byte-f2-ok
                                *64-bit-mode-0f-38-three-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '64-bit-mode-0f-38-three-byte-66-ok
                           *64-bit-mode-0f-38-three-byte-66-ok-ar*
                           opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode

    (defthm
     return-type-of-64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
     (b* ((mandatory-prefix (64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: 32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline

    (defun
     32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
     (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let
      ((__function__
          '32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '32-bit-mode-0f-38-three-byte-f3-ok
                                *32-bit-mode-0f-38-three-byte-f3-ok-ar*
                                opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '32-bit-mode-0f-38-three-byte-f2-ok
                                *32-bit-mode-0f-38-three-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '32-bit-mode-0f-38-three-byte-66-ok
                           *32-bit-mode-0f-38-three-byte-66-ok-ar*
                           opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode

    (defthm
     return-type-of-32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
     (b* ((mandatory-prefix (32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: 64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline

    (defun
     64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
     (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let
      ((__function__
          '64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '64-bit-mode-0f-3a-three-byte-f3-ok
                                *64-bit-mode-0f-3a-three-byte-f3-ok-ar*
                                opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '64-bit-mode-0f-3a-three-byte-f2-ok
                                *64-bit-mode-0f-3a-three-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '64-bit-mode-0f-3a-three-byte-66-ok
                           *64-bit-mode-0f-3a-three-byte-66-ok-ar*
                           opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode

    (defthm
     return-type-of-64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
     (b* ((mandatory-prefix (64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: 32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline

    (defun
     32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
     (opcode prefixes)
     (declare (type (unsigned-byte 8) opcode)
              (type (unsigned-byte 52) prefixes))
     (declare (xargs :guard (prefixes-p prefixes)))
     (let
      ((__function__
          '32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode))
      (declare (ignorable __function__))
      (let ((rep-pfx (the (unsigned-byte 8)
                          (prefixes->rep prefixes))))
        (if (not (eql rep-pfx 0))
            (if (or (and (equal rep-pfx 243)
                         (aref1 '32-bit-mode-0f-3a-three-byte-f3-ok
                                *32-bit-mode-0f-3a-three-byte-f3-ok-ar*
                                opcode))
                    (and (equal rep-pfx 242)
                         (aref1 '32-bit-mode-0f-3a-three-byte-f2-ok
                                *32-bit-mode-0f-3a-three-byte-f2-ok-ar*
                                opcode)))
                rep-pfx
              0)
          (let ((opr-pfx (the (unsigned-byte 8)
                              (prefixes->opr prefixes))))
            (if (not (eql opr-pfx 0))
                (if (aref1 '32-bit-mode-0f-3a-three-byte-66-ok
                           *32-bit-mode-0f-3a-three-byte-66-ok-ar*
                           opcode)
                    opr-pfx
                  0)
              0))))))

    Theorem: return-type-of-32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode

    (defthm
     return-type-of-32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
     (b* ((mandatory-prefix (32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
                                 opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: compute-mandatory-prefix-for-two-byte-opcode$inline

    (defun compute-mandatory-prefix-for-two-byte-opcode$inline
           (proc-mode opcode prefixes)
      (declare (type (integer 0 4) proc-mode)
               (type (unsigned-byte 8) opcode)
               (type (unsigned-byte 52) prefixes))
      (case proc-mode
        (0 (64-bit-compute-mandatory-prefix-for-two-byte-opcode
                opcode prefixes))
        (otherwise (32-bit-compute-mandatory-prefix-for-two-byte-opcode
                        opcode prefixes))))

    Theorem: return-type-of-compute-mandatory-prefix-for-two-byte-opcode

    (defthm return-type-of-compute-mandatory-prefix-for-two-byte-opcode
      (b* ((mandatory-prefix (compute-mandatory-prefix-for-two-byte-opcode$inline
                                  proc-mode opcode prefixes)))
        (unsigned-byte-p 8 mandatory-prefix))
      :rule-classes :rewrite)

    Function: compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline

    (defun compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
           (proc-mode opcode prefixes)
      (declare (type (integer 0 4) proc-mode)
               (type (unsigned-byte 8) opcode)
               (type (unsigned-byte 52) prefixes))
      (case proc-mode
        (0 (64-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
                opcode prefixes))
        (otherwise (32-bit-compute-mandatory-prefix-for-0f-38-three-byte-opcode
                        opcode prefixes))))

    Theorem: return-type-of-compute-mandatory-prefix-for-0f-38-three-byte-opcode

    (defthm
     return-type-of-compute-mandatory-prefix-for-0f-38-three-byte-opcode
     (b* ((mandatory-prefix (compute-mandatory-prefix-for-0f-38-three-byte-opcode$inline
                                 proc-mode opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline

    (defun compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
           (proc-mode opcode prefixes)
      (declare (type (integer 0 4) proc-mode)
               (type (unsigned-byte 8) opcode)
               (type (unsigned-byte 52) prefixes))
      (case proc-mode
        (0 (64-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                opcode prefixes))
        (otherwise (32-bit-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                        opcode prefixes))))

    Theorem: return-type-of-compute-mandatory-prefix-for-0f-3a-three-byte-opcode

    (defthm
     return-type-of-compute-mandatory-prefix-for-0f-3a-three-byte-opcode
     (b* ((mandatory-prefix (compute-mandatory-prefix-for-0f-3a-three-byte-opcode$inline
                                 proc-mode opcode prefixes)))
       (unsigned-byte-p 8 mandatory-prefix))
     :rule-classes :rewrite)

    Function: compute-mandatory-prefix-for-three-byte-opcode$inline

    (defun compute-mandatory-prefix-for-three-byte-opcode$inline
           (proc-mode second-escape-byte opcode prefixes)
      (declare (type (integer 0 4) proc-mode)
               (type (unsigned-byte 8)
                     second-escape-byte)
               (type (unsigned-byte 8) opcode)
               (type (unsigned-byte 52) prefixes))
      (declare (xargs :guard (or (equal second-escape-byte 56)
                                 (equal second-escape-byte 58))))
      (case second-escape-byte
        (56 (compute-mandatory-prefix-for-0f-38-three-byte-opcode
                 proc-mode opcode prefixes))
        (58 (compute-mandatory-prefix-for-0f-3a-three-byte-opcode
                 proc-mode opcode prefixes))
        (otherwise 0)))

    Theorem: return-type-of-compute-mandatory-prefix-for-three-byte-opcode

    (defthm
          return-type-of-compute-mandatory-prefix-for-three-byte-opcode
      (b* ((mandatory-prefix (compute-mandatory-prefix-for-three-byte-opcode$inline
                                  proc-mode
                                  second-escape-byte opcode prefixes)))
        (unsigned-byte-p 8 mandatory-prefix))
      :rule-classes :rewrite)