File: vex-opcodes-dispatch.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (389 lines) | stat: -rw-r--r-- 16,371 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
; X86ISA Library

; Note: The license below is based on the template at:
; http://opensource.org/licenses/BSD-3-Clause

; Copyright (C) 2018, Shilpi Goel
; All rights reserved.

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are
; met:

; o Redistributions of source code must retain the above copyright
;   notice, this list of conditions and the following disclaimer.

; o Redistributions in binary form must reproduce the above copyright
;   notice, this list of conditions and the following disclaimer in the
;   documentation and/or other materials provided with the distribution.

; o Neither the name of the copyright holders nor the names of its
;   contributors may be used to endorse or promote products derived
;   from this software without specific prior written permission.

; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

; Original Author(s):
; Shilpi Goel         <shigoel@gmail.com>

(in-package "X86ISA")

(include-book "instructions/top"
              :ttags (:syscall-exec :other-non-det :undef-flg))
(include-book "prefix-modrm-sib-decoding")
(include-book "dispatch-macros")
(include-book "cpuid")
(include-book "std/strings/hexify" :dir :system)

(local (include-book "dispatch-creator"))
(local (include-book "centaur/bitops/ihs-extensions" :dir :system))
(local (include-book "centaur/bitops/signed-byte-p" :dir :system))

(local (in-theory (e/d ()
                       (app-view-rml08-no-error
                        (:meta acl2::mv-nth-cons-meta)
                        rme08-value-when-error
                        member-equal))))

(local (xdoc::set-default-parents x86-decoder))

;; ----------------------------------------------------------------------

(local
 (defthm dumb-integerp-of-mv-nth-1-rme08-rule
   (implies (force (x86p x86))
            (integerp (mv-nth 1 (rme08 proc-mode eff-addr seg-reg r-x x86))))
   :rule-classes (:rewrite :type-prescription)))

(local
 (defthm unsigned-byte-p-24-of-vex-prefixes-rule
   (implies
    (unsigned-byte-p 8 byte)
    (and (unsigned-byte-p 24 (logior #xC400 (ash byte 16)))
         (unsigned-byte-p 24 (logior #xC500 (ash byte 16)))))))

(local
 (in-theory (e/d ()
                 (unsigned-byte-p
                  signed-byte-p
                  (:forward-chaining acl2::unsigned-byte-p-forward)
                  ash
                  (tau-system)))))

(make-event
 `(define vex-0F-execute
    ((proc-mode              :type (integer 0 #.*num-proc-modes-1*))
     (start-rip              :type (signed-byte   #.*max-linear-address-size*))
     (temp-rip               :type (signed-byte   #.*max-linear-address-size*)
                             "@('temp-rip') points to the byte following
                              the SIB byte (if present),
                              or the ModR/M byte (if present with no SIB byte),
                              or the opcode byte (if there is no ModR/M byte).")
     (prefixes               :type (unsigned-byte #.*prefixes-width*))
     (rex-byte               :type (unsigned-byte 8))
     (vex-prefixes           :type (unsigned-byte 24)
                             "Completely populated when this function is
                              called")
     (opcode                 :type (unsigned-byte 8))
     (modr/m                 :type (unsigned-byte 8))
     (sib                    :type (unsigned-byte 8))
     x86)

    :ignore-ok t

    :parents (x86-decoder)
    :no-function t
    :short "Dispatch function for VEX-encoded instructions in the two-byte opcode map"
    :guard (and (vex-prefixes-byte0-p vex-prefixes)
                (modr/m-p modr/m)
                (sib-p sib)
                (rip-guard-okp proc-mode temp-rip))
    :guard-hints (("Goal" :do-not '(preprocess)))
    :returns (x86 x86p :hyp (x86p x86))

    (case opcode
      ,@(create-dispatch-for-opcodes
         #ux0F_00 256 :vex-0F (w state)))))

(make-event
 `(define vex-0F38-execute
    ((proc-mode              :type (integer 0 #.*num-proc-modes-1*))
     (start-rip              :type (signed-byte   #.*max-linear-address-size*))
     (temp-rip               :type (signed-byte   #.*max-linear-address-size*)
                             "@('temp-rip') points to the byte following
                              the SIB byte (if present),
                              or the ModR/M byte (if present with no SIB byte),
                              or the opcode byte (if there is no ModR/M byte).")
     (prefixes               :type (unsigned-byte #.*prefixes-width*))
     (rex-byte               :type (unsigned-byte 8))
     (vex-prefixes           :type (unsigned-byte 24)
                             "Completely populated when this function is
                              called")
     (opcode                 :type (unsigned-byte 8))
     (modr/m                 :type (unsigned-byte 8))
     (sib                    :type (unsigned-byte 8))
     x86)

    :ignore-ok t

    :parents (x86-decoder)
    :no-function t
    :short "Dispatch function for VEX-encoded instructions in the first
    three-byte opcode map"
    :guard (and (vex-prefixes-byte0-p vex-prefixes)
                (modr/m-p modr/m)
                (sib-p sib)
                (rip-guard-okp proc-mode temp-rip))
    :guard-hints (("Goal" :do-not '(preprocess)))

    :returns (x86 x86p :hyp (x86p x86))

    (case opcode
      ,@(create-dispatch-for-opcodes
         #ux0F_38_00 256 :VEX-0F-38 (w state)))))

(make-event
 `(define vex-0F3A-execute
    ((proc-mode              :type (integer 0 #.*num-proc-modes-1*))
     (start-rip              :type (signed-byte   #.*max-linear-address-size*))
     (temp-rip               :type (signed-byte   #.*max-linear-address-size*)
                             "@('temp-rip') points to the byte following
                              the SIB byte (if present),
                              or the ModR/M byte (if present with no SIB byte),
                              or the opcode byte (if there is no ModR/M byte).")
     (prefixes               :type (unsigned-byte #.*prefixes-width*))
     (rex-byte               :type (unsigned-byte 8))
     (vex-prefixes           :type (unsigned-byte 24)
                             "Completely populated when this function is
                              called")
     (opcode                 :type (unsigned-byte 8))
     (modr/m                 :type (unsigned-byte 8))
     (sib                    :type (unsigned-byte 8))
     x86)

    :ignore-ok t

    :parents (x86-decoder)
    :no-function t
    :short "Dispatch function for VEX-encoded instructions in the second
    three-byte opcode map"
    :guard (and (vex-prefixes-byte0-p vex-prefixes)
                (modr/m-p modr/m)
                (sib-p sib)
                (rip-guard-okp proc-mode temp-rip))
    :guard-hints (("Goal" :do-not '(preprocess)))

    :returns (x86 x86p :hyp (x86p x86))

    (case opcode
      ,@(create-dispatch-for-opcodes
         #ux0F_3A_00 256 :vex-0f-3a (w state)))))


(define vex-decode-and-execute
  ((proc-mode              :type (integer 0 #.*num-proc-modes-1*))
   (start-rip              :type (signed-byte   #.*max-linear-address-size*))
   (temp-rip               :type (signed-byte   #.*max-linear-address-size*)
                           "@('temp-rip') points to the byte following the
                            first two VEX prefixes that were already read and
                            placed in the @('vex-prefixes') structure in @(tsee
                            x86-fetch-decode-execute).")
   (prefixes               :type (unsigned-byte #.*prefixes-width*))
   (rex-byte               :type (unsigned-byte 8))
   (vex-prefixes           :type (unsigned-byte 24)
                           "Only @('byte0') and @('byte1') fields are populated
                            when this function is called.")
   x86)

  :guard (and (prefixes-p prefixes)
              (vex-prefixes-byte0-p vex-prefixes))

  :returns (x86 x86p :hyp (x86p x86))

  :guard-hints
  (("Goal"
    :in-theory
    (e/d (modr/m-p
          vex-prefixes-byte0-p
          vex-prefixes-map-p add-to-*ip
          add-to-*ip-is-i48p-rewrite-rule
          unsigned-byte-p)
         (bitops::logand-with-negated-bitmask not (tau-system)))))
  :prepwork
  ((local
    (defthm signed-byte-p-of-temp-rip-rule
      (implies (and (signed-byte-p 48 temp-rip)
                    (integerp n)
                    (<= 0 (+ n temp-rip))
                    (<= (+ n temp-rip) (xr :seg-hidden-limit i x86))
                    (x86p x86))
               (signed-byte-p 48 (+ n temp-rip)))
      :hints (("Goal" :in-theory (e/d (signed-byte-p
                                       unsigned-byte-p)
                                      ())))))

   ;; Enabling for RoW proofs of vex-prefixes->* and !vex-prefixes->*.
   (local (in-theory (e/d (!vex-prefixes->byte0-is-vex-prefixes
                           !vex-prefixes->byte1-is-vex-prefixes
                           !vex-prefixes->byte2-is-vex-prefixes)
                          ())))

   (local
    (defthm logtail-16-of-unsigned-byte-p-8
      (implies (unsigned-byte-p 8 byte)
               (equal (logtail 16 byte) 0))))

   (local
    (defthm loghead-ash-0
      (implies (and (natp i)
                    (natp j)
                    (natp x)
                    (<= i j))
               (equal (loghead i (ash x j))
                      0))
      :hints (("Goal"
               :in-theory (e/d* (acl2::ihsext-inductions
                                 acl2::ihsext-recursive-redefs)
                                ()))))))

  :parents (x86-decoder)

  :long "<p>@('vex-decode-and-execute') dispatches control to VEX-encoded
  instructions.</p>

  <p><i>Reference: Intel Vol. 2A, Section 2.3: Intel Advanced Vector
   Extensions (Intel AVX)</i></p>"

  (b* ((ctx 'vex-decode-and-execute)
       ;; Reference for the following checks that lead to #UD:
       ;; Intel Vol. 2,
       ;; Section 2.3.2 - VEX and the LOCK prefix
       ;; Section 2.3.3 - VEX and the 66H, F2H, and F3H prefixes
       ;; Section 2.3.4 - VEX and the REX prefix

       ;; Any VEX-encoded instruction with mandatory (SIMD) prefixes, lock
       ;; prefix, and REX prefixes (i.e., 66, F2, F3, F0, and 40-4F) preceding
       ;; VEX will #UD.  Therefore, we fetch VEX prefixes after the legacy
       ;; prefixes (function get-prefixes) and the REX prefix have been
       ;; detected and fetched in x86-fetch-decode-execute.

       ((when (not (equal rex-byte 0)))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :rex rex-byte))
       ;; TODO: Intel Vol. 2A Sections 2.3.2 and 2.3.3 say "Any VEX-encoded
       ;; instruction with a LOCK/66H/F2H/F3H prefix preceding VEX will #UD."
       ;; So, should I check :last-byte here instead?
       ((when (equal (the (unsigned-byte 8) (prefixes->lck prefixes)) #.*lock*))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :lock-prefix))
       ((when (equal (the (unsigned-byte 8) (prefixes->rep prefixes)) #.*mandatory-f2h*))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :F2-prefix))
       ((when (equal (the (unsigned-byte 8) (prefixes->rep prefixes)) #.*mandatory-f3h*))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :F3-prefix))
       ((when (equal (the (unsigned-byte 8) (prefixes->opr prefixes)) #.*mandatory-66h*))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :66-prefix))

       (vex2-prefix?
        (equal (vex-prefixes->byte0 vex-prefixes) #.*vex2-byte0*))
       (vex3-prefix?
        (equal (vex-prefixes->byte0 vex-prefixes) #.*vex3-byte0*))
       (vex-byte1 (vex-prefixes->byte1 vex-prefixes))
       ;; VEX3 Byte 1 #UD Checks for M-MMMM field:
       ;; References: Intel Vol. 2, Figure 2-9 and Section 2.3.6.1
       ((mv vex3-0F-map? vex3-0F38-map? vex3-0F3A-map?)
        (if vex3-prefix?
            (mv (equal (vex3-byte1->m-mmmm vex-byte1) #.*v0F*)
                (equal (vex3-byte1->m-mmmm vex-byte1) #.*v0F38*)
                (equal (vex3-byte1->m-mmmm vex-byte1) #.*v0F3A*))
          (mv nil nil nil)))
       ((when (and vex3-prefix?
                   (not (or vex3-0F-map? vex3-0F38-map? vex3-0F3A-map?))))
        (!!fault-fresh :ud :vex-prefixes vex-prefixes :m-mmmm vex-byte1))

       ;; Completely populating the vex-prefixes structure --- filling out only
       ;; next-byte for vex2-prefixes and both byte2 and next-byte for
       ;; vex3-prefix:
       ((mv flg0 (the (unsigned-byte 8) byte2/next-byte) x86)
        (rme08 proc-mode temp-rip #.*cs* :x x86))
       ((when flg0)
        (!!ms-fresh :vex-byte2/next-byte-read-error flg0))
       ((mv flg1 temp-rip)
        (add-to-*ip proc-mode temp-rip 1 x86))
       ((when flg1)
        (!!ms-fresh :increment-error flg1))
       (vex-prefixes
        (if vex3-prefix?
            (!vex-prefixes->byte2 byte2/next-byte vex-prefixes)
          vex-prefixes))
       ((mv flg2 (the (unsigned-byte 8) next-byte) x86)
        (if vex3-prefix?
            (rme08 proc-mode temp-rip #.*cs* :x x86)
          ;; This 0 assigned to next-byte is ignored if vex3-prefix? is nil
          ;; (see assignment to opcode a few lines below):
          (mv nil 0 x86)))
       ((when flg2)
        (!!ms-fresh :next-byte-read-error flg2))
       ((mv flg3 temp-rip)
        (if vex3-prefix?
            (add-to-*ip proc-mode temp-rip 1 x86)
          (mv nil temp-rip)))
       ((when flg3)
        (!!ms-fresh :increment-error flg3))

       (opcode
        (the (unsigned-byte 8)
          (if vex3-prefix? next-byte byte2/next-byte)))

       (modr/m? (vex-opcode-ModR/M-p vex-prefixes opcode))
       ((mv flg4 (the (unsigned-byte 8) modr/m) x86)
        (if modr/m?
            (rme08 proc-mode temp-rip #.*cs* :x x86)
          (mv nil 0 x86)))
       ((when flg4)
        (!!ms-fresh :modr/m-byte-read-error flg4))
       ((mv flg5 temp-rip)
        (if modr/m?
            (add-to-*ip proc-mode temp-rip 1 x86)
          (mv nil temp-rip)))
       ((when flg5) (!!ms-fresh :increment-error flg5))

       (sib? (and modr/m?
                  (b* ((p4? (eql #.*addr-size-override*
                                 (the (unsigned-byte 8) (prefixes->adr prefixes))))
                       (16-bit-addressp (eql 2 (select-address-size proc-mode p4? x86))))
                    (x86-decode-SIB-p modr/m 16-bit-addressp))))
       ((mv flg6 (the (unsigned-byte 8) sib) x86)
        (if sib?
            (rme08 proc-mode temp-rip #.*cs* :x x86)
          (mv nil 0 x86)))
       ((when flg6)
        (!!ms-fresh :sib-byte-read-error flg6))
       ((mv flg7 temp-rip)
        (if sib?
            (add-to-*ip proc-mode temp-rip 1 x86)
          (mv nil temp-rip)))
       ((when flg7) (!!ms-fresh :increment-error flg7)))

    (cond
     ((mbe :logic (vex-prefixes-map-p #ux0F vex-prefixes)
           :exec (or vex2-prefix? (and vex3-prefix? vex3-0F-map?)))
      (vex-0F-execute proc-mode start-rip temp-rip prefixes rex-byte vex-prefixes opcode modr/m sib x86))
     ((mbe :logic (vex-prefixes-map-p #ux0F_38 vex-prefixes)
           :exec (and vex3-prefix? vex3-0F38-map?))
      (vex-0F38-execute proc-mode start-rip temp-rip prefixes rex-byte vex-prefixes opcode modr/m sib x86))
     ((mbe :logic (vex-prefixes-map-p #ux0F_3A vex-prefixes)
           :exec (and vex3-prefix? vex3-0F3A-map?))
      (vex-0F3A-execute proc-mode start-rip temp-rip prefixes rex-byte vex-prefixes opcode modr/m sib x86))
     (t
      ;; Unreachable.
      (!!ms-fresh :illegal-value-of-VEX-m-mmmm)))))

;; ----------------------------------------------------------------------