diff --git a/Cargo.lock b/Cargo.lock index d96a18a6a..13d143bcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,18 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "bitvec" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddcec3d12c579d40898fe0a9a358a803c23e9c52ca3c425707f81c9436211837" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -188,6 +200,14 @@ dependencies = [ "rustls", ] +[[package]] +name = "clazel-a64" +version = "0.0.0" +dependencies = [ + "yaxpeax-arch", + "yaxpeax-arm", +] + [[package]] name = "clazel-base64" version = "0.0.0" @@ -622,6 +642,14 @@ dependencies = [ "webpki-root-certs", ] +[[package]] +name = "clazel-x64" +version = "0.0.0" +dependencies = [ + "yaxpeax-arch", + "yaxpeax-x86", +] + [[package]] name = "codespan-reporting" version = "0.13.1" @@ -923,6 +951,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futures-core" version = "0.3.34" @@ -1770,6 +1804,12 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.10.3" @@ -2152,6 +2192,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tendril" version = "0.5.1" @@ -2549,6 +2595,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "xml5ever" version = "0.39.0" @@ -2570,6 +2625,36 @@ dependencies = [ "sha2", ] +[[package]] +name = "yaxpeax-arch" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb869f86d5a365bf741d9df773f7a5347757b1c466b7383b74989324785ef36b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "yaxpeax-arm" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79f8d9808ad0b7e434882c66e50704138e7c0bdf3408a9f3159c50603dc3168c" +dependencies = [ + "bitvec", + "yaxpeax-arch", +] + +[[package]] +name = "yaxpeax-x86" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9d20d93c5efc168a0f0576ad0a5e8fc7826aa5f41c37ede54527d655e0625d" +dependencies = [ + "cfg-if", + "num-traits", + "yaxpeax-arch", +] + [[package]] name = "zerocopy" version = "0.8.63" diff --git a/crates/clazel-a64/.gitattributes b/crates/clazel-a64/.gitattributes new file mode 100644 index 000000000..fcbb06aca --- /dev/null +++ b/crates/clazel-a64/.gitattributes @@ -0,0 +1,3 @@ +# Vendored data is compared byte for byte (see the root attributes file); the +# upstream files' line endings are part of what they test. +tests/data/** -text diff --git a/crates/clazel-a64/Cargo.toml b/crates/clazel-a64/Cargo.toml new file mode 100644 index 000000000..daf48a048 --- /dev/null +++ b/crates/clazel-a64/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "clazel-a64" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish.workspace = true +description = "The A64 (64-bit Arm) instruction-set subset the JIT emits (docs/phases/jit.md item J.4): semantics, encoder and decoder. Empty until then; it holds the vendored instruction-set sources." + +# No dependencies yet: jit.md item J.2 only vendors this crate's test data and +# inputs. The specification and implementation arrive with the item named in +# src/lib.rs. + +# Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the +# shipped code today (nothing: the crate is empty); `target-tier` is what +# jit.md's "Crates" table sets. The contracts come first (rule 3). +[package.metadata.clazel] +tier = 0 +target-tier = 2 + +[lints] +workspace = true + +[dev-dependencies] +yaxpeax-arm = { version = "=0.5.0", default-features = false, features = ["fmt"] } +yaxpeax-arch = { version = "=0.3.3", default-features = false } diff --git a/crates/clazel-a64/README.md b/crates/clazel-a64/README.md new file mode 100644 index 000000000..29fc90417 --- /dev/null +++ b/crates/clazel-a64/README.md @@ -0,0 +1,43 @@ +# clazel-a64 + +Part of [Clazel](../../README.md). The A64 (64-bit Arm) subset the JIT emits: +its semantics, encoder and decoder ([jit.md](../../docs/phases/jit.md) item +J.4). + +Nothing is implemented yet (tier 0, target tier 2). Item J.2 vendored or +pinned the sources the semantics will be written against, in `tests/data/`: + +| Suite | What | Held as | +|---|---|---| +| `sail-arm` | the Sail Armv9.4-A model: A64 base instructions and shared pseudocode (BSD-3-Clause-Clear) | 3.9 MB, vendored | +| `arm-mrs` | Arm's machine-readable specification, 2026-09 release (BSD-3-Clause) | licence and README vendored; `Instructions.json` (116,678,333 bytes) pinned by SHA-256 | +| `arm-arm` | Arm Architecture Reference Manual for A-profile (DDI 0487), issue M.d | pinned by URL, size (129,014,701 bytes) and SHA-256, not vendored (Arm's terms forbid copying) | + +Each has a `SOURCE.md` with the upstream, the commit (or the SHA-256) and the +licence. The tables in `tests/generated/` list every file's size and hash. + +## Where the transcriptions come from + +Decided by the user on 2026-11-16 (jit.md, the note under J.4): + +- The **primary spec** is transcribed from the Sail Arm model and Arm's ASL. +- The **independent transcription** is made from the Arm Architecture + Reference Manual's prose, by a **different session** that does not read the + primary transcription. +- The **hardware differential tests** on the macOS runner (exit criterion 3) + are the further check against both. + +## The two formal A64 sources are one source + +The Sail Arm model's README says its Sail files "are derived from the 2023-03 +version of Arm's ASL specification using the automatic ASL-to-Sail +translation tool". Arm's machine-readable specification (`Instructions.json`) +carries the same ASL as the operation of each instruction. So the two are one +source in two syntaxes (the Sail model is from the 2023-03 ASL, the JSON from +2026-09), and they share Arm's own errors, if any. That is why the second +transcription comes from the manual's prose. + +The `arm-mrs` JSON is also the only machine-readable encoding table +(`Instructions.json` has every encoding with its fixed and variable bits), so +the decoder differential against `yaxpeax-arm` can be checked against it as +well. diff --git a/crates/clazel-a64/src/lib.rs b/crates/clazel-a64/src/lib.rs new file mode 100644 index 000000000..6eefce323 --- /dev/null +++ b/crates/clazel-a64/src/lib.rs @@ -0,0 +1,8 @@ +//! The A64 (64-bit Arm) instruction-set subset the JIT emits (docs/phases/jit.md item J.4). +//! +//! Empty for now: item J.2 only creates the crate, so that the instruction-set +//! sources (`tests/data/`) have a home. The spec (semantics as a `spec fn`) +//! and its contracts come first (CLAUDE.md rule 3), then the encoder and +//! decoder. +//! +//! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and `docs/VERIFICATION.md`. diff --git a/crates/clazel-a64/tests/data/arm-arm/LICENSE.md b/crates/clazel-a64/tests/data/arm-arm/LICENSE.md new file mode 100644 index 000000000..08ec186f3 --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-arm/LICENSE.md @@ -0,0 +1,17 @@ +# Licence of the Arm Architecture Reference Manual + +None granted for redistribution. The Arm Architecture Reference Manual for +A-profile architecture (DDI 0487) is Arm Limited's copyright and is published +under Arm's documentation terms, not under an open licence. Arm's website +terms (https://www.arm.com/company/policies/terms-and-conditions, read on +2026-11-16) forbid reproducing or copying its content without express written +permission, and allow a personal reference copy only. We found no grant to +redistribute the manual. Arm's own machine-readable specification, a different +product, is BSD-3-Clause (`../arm-mrs/`). The PDF is therefore not vendored: +`SOURCE.md` pins it by URL, size and SHA-256. + +The proprietary notice inside the PDF was not read (the 129 MB file was read +only for its hash); a person should read it before anyone proposes vendoring +any part of the document. + +This file is a statement of that decision, not a copy of a licence. diff --git a/crates/clazel-a64/tests/data/arm-arm/SOURCE.md b/crates/clazel-a64/tests/data/arm-arm/SOURCE.md new file mode 100644 index 000000000..97cfd6e39 --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-arm/SOURCE.md @@ -0,0 +1,34 @@ +# Arm Architecture Reference Manual for A-profile architecture (DDI 0487), issue M.d, pinned and not vendored + +Upstream: https://documentation-service.arm.com/static/6abbec2a6db85c5e6614a4a1 +SHA-256: 80d589a4645feba6bdb9d77edfaf04d4394d5d5444a4c41f97cf4fdd20eee3d5 DDI0487M_d_a-profile_architecture_reference_manual.pdf +Size: 129014701 DDI0487M_d_a-profile_architecture_reference_manual.pdf +License: Arm copyright, proprietary notice, no licence to redistribute. See `LICENSE.md`. + +The file is what Arm's own documentation service serves for DDI 0487 "latest" +on 2026-11-16: its API +(`https://documentation-service.arm.com/documentation/ddi0487/latest`) names +version label **M.d** (document quality "EAC", Arm's early-access label for a +release not yet final) and lists this PDF as the resource +`DDI0487M_d_a-profile_architecture_reference_manual.pdf`. The human-facing +page is https://developer.arm.com/documentation/ddi0487/latest, which +redirects to support.arm.com and is a script-driven page that a plain client +cannot read; the `static/` URL above is the one the API gave, fetched without +its `token` query parameter, and it returned the PDF (`%PDF-1.7`, 129,014,701 +bytes, 129 MB). + +This is the source of the independent A64 transcription of jit.md item J.4: +the instruction pages (Part C, "The A64 Instruction Set") give each +instruction's encoding and its pseudocode, and Part B gives the application +level model. Pinned Arm ARM issues change: when Arm publishes M.e or a final +M.d, the hash no longer matches and a person must repin after reading the +change list. + +The PDF is not in this repository: it is far over the size budget (129 MB), +and Arm's documents carry a proprietary notice with no right to redistribute +(`LICENSE.md`). Nothing was checked against a mirror. The `static/` URL is +Arm's own host, but it is an opaque document id, and Arm may withdraw it when +it replaces the issue; if so, repin from the page above. + +`xtask/src/testdata/isa.rs` writes `tests/generated/arm_arm.rs`, the table of +this pin. diff --git a/crates/clazel-a64/tests/data/arm-mrs/LICENSE.md b/crates/clazel-a64/tests/data/arm-mrs/LICENSE.md new file mode 100644 index 000000000..a3b7eb59d --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-mrs/LICENSE.md @@ -0,0 +1,31 @@ +# Licence of Arm's AARCHMRS open-source package + +Transcribed from `docs/notice.html` of the package (ARM Machine Readable +Schema 2.9.6; the original is `notice.html` beside this file). + +Copyright 2010-2026 Arm Limited or its affiliates. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +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. + +Neither the name of the copyright holder 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. diff --git a/crates/clazel-a64/tests/data/arm-mrs/README.md b/crates/clazel-a64/tests/data/arm-mrs/README.md new file mode 100644 index 000000000..c894bd881 --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-mrs/README.md @@ -0,0 +1,30 @@ +# AARCHMRS containing the JSON files for A-profile Architecture + +## Introduction + +This is the Arm Architecture Machine Readable Specification containing +JSON files representing the architecture as a machine readable format. + +This package contains a subset of the information provided in the packages +with Arm proprietary licenses. Content that is not currently in a machine-readable +format, as well as all descriptive content, is omitted. + +The [notice](docs/notice.html) gives details of the license terms and conditions +under which this package is provided. + +## Package contents + + - `Features.json` contains the architecture feature constraints. + - `Instructions.json` contains the A64, A32, and T32 Instruction Set Architecture. + - `Registers.json` contains the AArch32, AArch64, and MemoryMapped + System Registers and System instructions. + - [`schema`](schema/) contains JSON schema for the data. + - [`docs`](docs/index.html) contains the rendered view of the schema + as well as user guides to help understand the data above. + +## Package quality + + - The architectural content contained within the data files has the same quality + as the equivalent XML releases. + - The schema is still under development and is subject to change. + diff --git a/crates/clazel-a64/tests/data/arm-mrs/SOURCE.md b/crates/clazel-a64/tests/data/arm-mrs/SOURCE.md new file mode 100644 index 000000000..405e06129 --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-mrs/SOURCE.md @@ -0,0 +1,30 @@ +# Arm's open-source machine-readable specification of the A-profile architecture (README and licence vendored, Instructions.json pinned) + +Upstream: https://kernel.googlesource.com/pub/scm/linux/kernel/git/maz/AARCHMRS/+/1346b16d47619400448f3cb6d4f22d62d3b76848 +Commit: 1346b16d47619400448f3cb6d4f22d62d3b76848 +SHA-256: ef75f3ec02df1b9b9c0612f4ea259d6710ac3de686b097d8f9c83d1d97aa800a Instructions.json +Size: 116678333 Instructions.json +License: BSD-3-Clause (Arm Limited), as stated in `notice.html` (the package's own notice; `LICENSE.md` is its text). Redistribution is allowed. + +Arm publishes the package as `AARCHMRS_OPENSOURCE_A_profile_FAT-2026-09` on its +developer site (the "Exploration Tools" page of the A-profile architecture; the +site redirected our client to support.arm.com and was not fetched). The commit +above is a mirror of that release in a git repository (Marc Zyngier's +AARCHMRS repository at kernel.googlesource.com, whose commit message is the +release name, 2026-10-15). The mirror is third-party: a person should compare +its hashes with the tarball from Arm's site before relying on it. + +Vendored, byte for byte: `README.md` and `notice.html` (the licence; schema +2.9.6). + +**`Instructions.json` is pinned, not vendored.** It holds the A64, A32 and T32 +instruction sets with their encodings and their ASL operations, and it is +116,678,333 bytes (116 MB), far over this suite's size budget, and it cannot +be cut to the A64 base instructions without rewriting it. The pin above is +the SHA-256 of the file as stored in the commit (LF line endings). A reader +(jit.md item J.4) fetches the commit, takes `Instructions.json` and checks the +hash. `Features.json` and `Registers.json` are not needed for the subset. + +`xtask/src/testdata/isa.rs` writes `tests/generated/arm_mrs.rs`, the inventory +of the two vendored files and the table of the pin. Nothing reads the +specification yet. The Sail model derived from the same ASL is `sail-arm`. diff --git a/crates/clazel-a64/tests/data/arm-mrs/notice.html b/crates/clazel-a64/tests/data/arm-mrs/notice.html new file mode 100644 index 000000000..97baa37e6 --- /dev/null +++ b/crates/clazel-a64/tests/data/arm-mrs/notice.html @@ -0,0 +1,75 @@ + + + + +
+ + + + + + + + + +Copyright 2010-2026 Arm Limited or its affiliates.
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+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.
+Neither the name of the copyright holder 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.
+This @(see documentation) is based on the directory @('rtl') of the + ACL2 @(see community-books). For a more thorough treatment, see \"Formal Verification + of Floating-Point Hardware Design: A Mathematical Approach\" by David + M. Russinoff. See file @('rtl/README') for additional information about this + library and its connection to this book.
") + +(rtl-order-subtopics rtl (|Register-Transfer Logic| + |Floating-Point Arithmetic| + |Floating-Point Exceptions and Specification of Elementary Arithmetic Instructions| + |Implementation of Elementary Operations| + |Modeling Algorithms in C++ and ACL2| + |Bibliography|)) + +(defun defsection-rtl-list-for-tree (parent trees) + +; Trees is a tail of the children of parent in *rtl-node-tree*. + + (declare (xargs :mode :program)) + (cond ((endp trees) nil) + ((atom (car trees)) + +; Then defsection-rtl will be given explicitly for each tree in trees. + + nil) + (t ; (car trees) is (topic . children) + (list* `(defsection-rtl ,(caar trees) ,parent) + `(rtl-order-subtopics ,(caar trees) ,(cdar trees)) + (append (defsection-rtl-list-for-tree (caar trees) (cdar trees)) + (defsection-rtl-list-for-tree parent (cdr trees))))))) + +(defmacro defsection-rtl-list () + (cons 'progn (defsection-rtl-list-for-tree 'rtl *rtl-node-tree*))) + +(defsection-rtl-list) + +; Handle top-level leaves: + +(defsection-rtl |Bibliography| rtl) +(defsection-rtl |Modeling Algorithms in C++ and ACL2| rtl) diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/excps.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/excps.lisp new file mode 100644 index 000000000..f62050ab0 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/excps.lisp @@ -0,0 +1,1095 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; +; Contact: +; David M. Russinoff +; david@russinoff.com +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; + +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") + +;;;*************************************************************** +;;; SSE Operations +;;;*************************************************************** + +(defsection-rtl |SSE Floating-Point Instructions| + |Floating-Point Exceptions and Specification of Elementary Arithmetic Instructions| + + ;; Exception flag bits (indices shared by SSE and x87): + +(defnd ibit () 0) +(defnd dbit () 1) +(defnd zbit () 2) +(defnd obit () 3) +(defnd ubit () 4) +(defnd pbit () 5) + +(in-theory (disable (ibit) (dbit) (zbit) (obit) (ubit) (pbit))) + +;; Other MXCSR bits: + +(defnd daz () 6) +(defnd imsk () 7) +(defnd dmsk () 8) +(defnd zmsk () 9) +(defnd omsk () 10) +(defnd umsk () 11) +(defnd pmsk () 12) +(defnd ftz () 15) + +(in-theory (disable (daz) (imsk) (dmsk) (zmsk) (omsk) (umsk) (pmsk) (ftz))) + +(defund mxcsr-masks (mxcsr) + (declare (xargs :guard (natp mxcsr))) + (bits mxcsr 12 7)) + +(defund mxcsr-rc (mxcsr) + (declare (xargs :guard (natp mxcsr))) + (case (bits mxcsr 14 13) + (0 'rne) + (1 'rdn) + (2 'rup) + (3 'rtz))) + +;;-------------------------------------------------------------------------------- + +;; This specification applies only to scalar SSE instructions. + +;; The arguments of SSE-BINARY-SPEC are an operation (add, sub, mul, or div), 2 data +;; inputs, the initial MXCSR register, and the significand and exponent widths. It +;; returns a data result, which is NIL in the event of an unmasked exception, and the +;; updated MXCSR. + +;; An implementation is correct if it returns the same MXCSR as SSE-BINARY-SPEC and, +;; in the event that SSE-BINARY-SPEC returns a non-NIL value, it returns the same value. + +;; SSE-BINARY-SPEC is based on two auxiliary functions, SSE-BINARY-PRE-COMP and +;; SSE-BINARY-COMP, each of which returns an optional value and a 6-bit vector +;; of exception flags, which are written to the MXCSR. + +;; SSE-BINARY-PRE-COMP calls SSE-BINARY-PRE-COMP-EXCP, which detects pre-computation +;; exceptions, and SSE-BINARY-PRE-COMP-VAL, which may compute a value. If an unmasked +;; exception occurs, the value is invalid and the operation is terminated. Otherwise, +;; if the value is NIL, then the computation proceeds by calling SSE-BINARY-COMP, and +;; if non-NIL, the operation is terminated and that value is returned. + +;; SSE-BINARY-COMP either returns an infinity or decodes the operands and computes the +;; unrounded result. If that result is 0, then it sets the sign according to the operand +;; signs and the rounding mode and returns. Otherwise, it calls SSE-POST-COMP, which detects +;; post-computation exceptions and computes the rounded result, which is invalid in the +;; event of an unmasked exception. + +(defund set-flag (b flags) + (declare (xargs :guard (and (natp b) + (natp flags)))) + (logior flags (expt 2 b))) + +(defund unmasked-excp-p (flags masks) + (declare (xargs :guard (and (natp flags) + (natp masks)))) + (or (and (= (bitn flags (ibit)) 1) (= (bitn masks (ibit)) 0)) + (and (= (bitn flags (dbit)) 1) (= (bitn masks (dbit)) 0)) + (and (= (bitn flags (zbit)) 1) (= (bitn masks (zbit)) 0)) + (and (= (bitn flags (obit)) 1) (= (bitn masks (obit)) 0)) + (and (= (bitn flags (ubit)) 1) (= (bitn masks (ubit)) 0)) + (and (= (bitn flags (pbit)) 1) (= (bitn masks (pbit)) 0)))) + +(defund dazify (x daz f) + (declare (xargs :guard (and (encodingp x f) + (natp daz)))) + (if (and (= daz 1) (denormp x f)) + (zencode (sgnf x f) f) + x)) + +(defund binary-undefined-p (op a af b bf) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf)))) + (case op + (add (and (infp a af) (infp b bf) (not (= (sgnf a af) (sgnf b bf))))) + (sub (and (infp a af) (infp b bf) (= (sgnf a af) (sgnf b bf)))) + (mul (and (or (infp a af) (infp b bf)) + (or (zerp a af) (zerp b bf)))) + (div (or (and (infp a af) (infp b bf)) + (and (zerp a af) (zerp b bf)))))) + +(defund sse-binary-pre-comp-excp (op a b f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f)))) + (if (or (snanp a f) (snanp b f)) + (set-flag (ibit) 0) + (if (or (qnanp a f) (qnanp b f)) + 0 + (if (binary-undefined-p op a f b f) + (set-flag (ibit) 0) + (if (and (eql op 'div) (zerp b f) (not (infp a f))) + (set-flag (zbit) 0) + (if (or (denormp a f) (denormp b f)) + (set-flag (dbit) 0) + 0)))))) + +(defund sse-binary-pre-comp-val (op a b f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f)))) + (if (nanp a f) + (qnanize a f) + (if (nanp b f) + (qnanize b f) + (if (binary-undefined-p op a f b f) + (indef f) + ())))) + +(defund sse-binary-pre-comp (op a b mxcsr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp mxcsr)))) + (let* ((daz (bitn mxcsr (daz))) + (a (dazify a daz f)) + (b (dazify b daz f))) + (mv a b (sse-binary-pre-comp-val op a b f) (sse-binary-pre-comp-excp op a b f)))) + +(defund sse-post-comp (u mxcsr f) + (declare (xargs :guard (and (real/rationalp u) + (not (= u 0)) + (natp mxcsr) + (formatp f)))) + (let* ((rmode (mxcsr-rc mxcsr)) + (r (rnd u rmode (prec f))) + (rsgn (if (< r 0) 1 0)) + (flags (if (= r u) 0 (set-flag (pbit) 0)))) + (if (> (abs r) (lpn f)) + (let* ((flags (set-flag (obit) flags))) + (if (= (bitn mxcsr (omsk)) 1) + (let ((flags (set-flag (pbit) flags))) + (if (or (and (eql rmode 'rdn) (> r 0)) + (and (eql rmode 'rup) (< r 0)) + (eql rmode 'rtz)) + (mv (nencode (* (sgn r) (lpn f)) f) + flags) + (mv (iencode rsgn f) flags))) + (mv () flags))) + (if (< (abs r) (spn f)) + (if (= (bitn mxcsr (umsk)) 1) + (if (= (bitn mxcsr (ftz)) 1) + (mv (zencode rsgn f) (set-flag (pbit) (set-flag (ubit) flags))) + (let ((d (drnd u rmode f))) + (if (= d u) + (mv (dencode d f) flags) + (let ((flags (set-flag (pbit) (set-flag (ubit) flags)))) + (if (= d 0) + (mv (zencode rsgn f) flags) + (if (= (abs d) (spn f)) + (mv (nencode d f) flags) + (mv (dencode d f) flags))))))) + (mv () (set-flag (ubit) flags))) + (mv (nencode r f) flags))))) + +(defund binary-inf-sgn (op a af b bf) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf)))) + (case op + (add (if (infp a af) (sgnf a af) (sgnf b bf))) + (sub (if (infp a af) (sgnf a af) (if (zerop (sgnf b bf)) 1 0))) + ((mul div) (logxor (sgnf a af) (sgnf b bf))))) + +(defund binary-zero-sgn (op asgn bsgn rmode) + (declare (xargs :guard (and (member op '(add sub mul div)) + (bvecp asgn 1) + (bvecp bsgn 1) + (IEEE-rounding-mode-p rmode)))) + (case op + (add (if (= asgn bsgn) asgn (if (eql rmode 'rdn) 1 0))) + (sub (if (not (= asgn bsgn)) asgn (if (eql rmode 'rdn) 1 0))) + ((mul div) (logxor asgn bsgn)))) + +(defund binary-eval (op aval bval) + (declare (xargs :guard (and (member op '(add sub mul div)) + (real/rationalp aval) + (real/rationalp bval) + (not (and (eql op 'div) (= bval 0)))))) + (case op + (add (+ aval bval)) + (sub (- aval bval)) + (mul (* aval bval)) + (div (/ aval bval)))) + +(defund sse-binary-comp (op a b mxcsr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp mxcsr)))) + (if (or (infp a f) (if (eql op 'div) (zerp b f) (infp b f))) + (mv (iencode (binary-inf-sgn op a f b f) f) 0) + (let* ((asgn (sgnf a f)) + (bsgn (sgnf b f)) + (aval (decode a f)) + (bval (decode b f)) + (u (binary-eval op aval bval))) + (if (or (and (eql op 'div) (infp b f)) (= u 0)) + (mv (zencode (binary-zero-sgn op asgn bsgn (mxcsr-rc mxcsr)) f) 0) + (sse-post-comp u mxcsr f))))) + +(defund sse-binary-spec (op a b mxcsr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp mxcsr)))) + (mv-let (adaz bdaz result pre-flags) (sse-binary-pre-comp op a b mxcsr f) + (if (unmasked-excp-p pre-flags (mxcsr-masks mxcsr)) + (mv () (logior mxcsr pre-flags)) + (if result + (mv result (logior mxcsr pre-flags)) + (mv-let (result post-flags) (sse-binary-comp op adaz bdaz mxcsr f) + (mv (and (not (unmasked-excp-p post-flags (mxcsr-masks mxcsr))) + result) + (logior (logior mxcsr pre-flags) post-flags))))))) + + +;;-------------------------------------------------------------------------------- + +;; The arguments of SSE-SQRT-SPEC are a data input, the initial MXCSR register, and +;; the significand and exponent widths. It returns a data result, which is NIL in +;; the event of an unmasked exception, and the updated MXCSR. + +(defund sse-sqrt-pre-comp-excp (a f) + (declare (xargs :guard (encodingp a f))) + (if (snanp a f) + (set-flag (ibit) 0) + (if (qnanp a f) + 0 + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (set-flag (ibit) 0) + (if (denormp a f) + (set-flag (dbit) 0) + 0))))) + +(defund sse-sqrt-pre-comp-val (a f) + (declare (xargs :guard (encodingp a f))) + (if (nanp a f) + (qnanize a f) + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (indef f) + ()))) + +(defund sse-sqrt-pre-comp (a mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (natp mxcsr)))) + (let ((a (dazify a (bitn mxcsr (daz)) f))) + (mv a (sse-sqrt-pre-comp-val a f) (sse-sqrt-pre-comp-excp a f)))) + +(defund sse-sqrt-comp (a mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (or (zerp a f) (= (sgnf a f) 0)) + (natp mxcsr)))) + (if (or (infp a f) (zerp a f)) + (mv a 0) + (sse-post-comp (qsqrt (decode a f) (+ (prec f) 2)) mxcsr f))) + +(defund sse-sqrt-spec (a mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (natp mxcsr)))) + (mv-let (adaz result pre-flags) (sse-sqrt-pre-comp a mxcsr f) + (if (unmasked-excp-p pre-flags (mxcsr-masks mxcsr)) + (mv () (logior mxcsr pre-flags)) + (if result + (mv result (logior mxcsr pre-flags)) + (mv-let (result post-flags) (sse-sqrt-comp adaz mxcsr f) + (mv (and (not (unmasked-excp-p post-flags (mxcsr-masks mxcsr))) + result) + (logior (logior mxcsr pre-flags) post-flags))))))) + + +;;-------------------------------------------------------------------------------- + +;; The arguments of FMA-SPEC are three data inputs, the initial MXCSR register, and +;; the significand and exponent widths. It returns a data result, which is NIL in the +;; event of an unmasked exception, and the updated MXCSR. + +(defund fma-undefined-p (a b c f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f)))) + (and (or (infp a f) (infp b f)) + (or (zerp a f) + (zerp b f) + (and (not (nanp a f)) + (not (nanp b f)) + (infp c f) + (not (= (sgnf c f) + (logxor (sgnf a f) (sgnf b f)))))))) + +(defund sse-fma-pre-comp-excp (a b c f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f)))) + (if (or (snanp a f) (snanp b f) (snanp c f)) + (set-flag (ibit) 0) + (if (or (qnanp a f) (qnanp b f) (qnanp c f)) + 0 + (if (fma-undefined-p a b c f) + (set-flag (ibit) 0) + (if (or (denormp a f) (denormp b f) (denormp c f)) + (set-flag (dbit) 0) + 0))))) + +(defund sse-fma-pre-comp-val (a b c f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f)))) + (if (nanp a f) + (qnanize a f) + (if (nanp b f) + (qnanize b f) + (if (nanp c f) + (qnanize c f) + (if (fma-undefined-p a b c f) + (indef f) + ()))))) + +(defund sse-fma-pre-comp (a b c mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp mxcsr)))) + (let* ((daz (bitn mxcsr (daz))) + (a (dazify a daz f)) + (b (dazify b daz f)) + (c (dazify c daz f))) + (mv a b c (sse-fma-pre-comp-val a b c f) (sse-fma-pre-comp-excp a b c f)))) + +(defund sse-fma-comp (a b c mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp mxcsr)))) + (let* ((asgn (sgnf a f)) + (bsgn (sgnf b f)) + (csgn (sgnf c f)) + (aval (decode a f)) + (bval (decode b f)) + (cval (decode c f)) + (u (+ (* aval bval) cval))) + (if (or (infp a f) (infp b f)) + (mv (iencode (logxor asgn bsgn) f) 0) + (if (infp c f) + (mv c 0) + (if (= u 0) + (mv (zencode (if (= (logxor asgn bsgn) csgn) + csgn + (if (eql (mxcsr-rc mxcsr) 'rdn) 1 0)) + f) + 0) + (sse-post-comp u mxcsr f)))))) + +(defund sse-fma-spec (a b c mxcsr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp mxcsr)))) + (mv-let (adaz bdaz cdaz result pre-flags) (sse-fma-pre-comp a b c mxcsr f) + (if (unmasked-excp-p pre-flags (mxcsr-masks mxcsr)) + (mv () (logior mxcsr pre-flags)) + (if result + (mv result (logior mxcsr pre-flags)) + (mv-let (result post-flags) (sse-fma-comp adaz bdaz cdaz mxcsr f) + (mv (and (not (unmasked-excp-p post-flags (mxcsr-masks mxcsr))) + result) + (logior (logior mxcsr pre-flags) post-flags))))))) + +) +;;;*************************************************************** +;;; x87 Operations +;;;*************************************************************** + +(defsection-rtl |x87 Instructions| + |Floating-Point Exceptions and Specification of Elementary Arithmetic Instructions| + +;; Rounding and precision control in FCW + +(defund fcw-rc (fcw) + (declare (xargs :guard (natp fcw))) + (case (bits fcw 11 10) + (0 'rne) + (1 'rdn) + (2 'rup) + (3 'rtz))) + +(defund fcw-pc (fcw) + (declare (xargs :guard (natp fcw))) + (case (bits fcw 9 8) + ((0 1) 24) + (2 53) + (3 64))) + +;; Additional FSW status bits that are set by x87 instructions: + +(defnd es () 7) +(defnd bb () 15) +(defnd c1 () 9) + +(in-theory (disable (es) (bb) (c1))) + +;; Whenever ES (FSW[7]) is set, so is B (FSW[15]): + +(defund set-es (fsw) + (declare (xargs :guard (natp fsw))) + (set-flag (bb) (set-flag (es) fsw))) + +;; C1 is cleared by default: + +(defund clear-c1 (fsw) + (declare (xargs :guard (natp fsw))) + (logand fsw #xfdff)) + +;; The arguments of X87-BINARY-SPEC are two data inputs, their formats, and the initial +;; FCW and FSW registers. It returns a data result, which is NIL in the event of an +;; unmasked pre-computation exception, and the updated FSW. + +(defund x87-binary-pre-comp-excp (op a af b bf) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf)))) + (if (or (snanp a af) (unsupp a af) (snanp b bf) (unsupp b bf)) + (set-flag (ibit) 0) + (if (or (qnanp a af) (qnanp b bf)) + 0 + (if (binary-undefined-p op a af b bf) + (set-flag (ibit) 0) + (if (and (eql op 'div) (zerp b bf) (not (infp a af))) + (set-flag (zbit) 0) + (if (or (denormp a af) (pseudop a af) (denormp b bf) (pseudop b bf)) + (set-flag (dbit) 0) + 0)))))) + +(defund convert-nan-to-ep (x f) + (declare (xargs :guard (and (encodingp x f) + (<= (prec f) 64)))) + (cat (sgnf x f) + 1 + (1- (expt 2 15)) + 15 + 1 + 1 + (manf x f) + (1- (prec f)) + 0 + (- 64 (prec f)))) + +(defund x87-binary-pre-comp-val (op a af b bf) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf) + (<= (prec af) 64) + (<= (prec bf) 64)))) + (let ((aep (convert-nan-to-ep a af)) + (bep (convert-nan-to-ep b bf))) + (if (nanp a af) + (if (nanp b bf) + (if (> (sigf aep (ep)) (sigf bep (ep))) + (qnanize aep (ep)) + (if (< (sigf aep (ep)) (sigf bep (ep))) + (qnanize bep (ep)) + (if (= (sgnf aep (ep)) 0) + (qnanize (convert-nan-to-ep a af) (ep)) + (qnanize (convert-nan-to-ep b bf) (ep))))) + (qnanize aep (ep))) + (if (nanp b bf) + (qnanize bep (ep)) + (if (binary-undefined-p op a af b bf) + (indef (ep)) + ()))))) + +(defund x87-binary-pre-comp (op a af b bf) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf) + (<= (prec af) 64) + (<= (prec bf) 64)))) + (mv (x87-binary-pre-comp-val op a af b bf) (x87-binary-pre-comp-excp op a af b bf))) + +(defund x87-post-comp (u fcw) + (declare (xargs :guard (and (real/rationalp u) + (not (= u 0)) + (natp fcw)))) + (let* ((rmode (fcw-rc fcw)) + (r (rnd u rmode (fcw-pc fcw))) + (rsgn (if (< r 0) 1 0)) + (flags (if (= r u) 0 (set-flag (pbit) 0)))) + (if (> (abs r) (lpn (ep))) + (let ((flags (set-flag (obit) flags))) + (if (= (bitn fcw (obit)) 1) + (let ((flags (set-flag (pbit) flags))) + (if (or (and (eql rmode 'rdn) (> r 0)) + (and (eql rmode 'rup) (< r 0)) + (eql rmode 'rtz)) + (mv (nencode (* (sgn r) (lpn (ep))) (ep)) flags) + (mv (iencode rsgn (ep)) (set-flag (c1) flags)))) + (let ((s (* r (expt 2 (- (* 3 (expt 2 13))))))) + (if (> (abs s) (lpn (ep))) + (mv (iencode rsgn (ep)) + (set-flag (c1) (set-flag (pbit) flags))) + (mv (nencode s (ep)) + (if (> (abs r) (abs u)) (set-flag (c1) flags) flags)))))) + (if (< (abs r) (spn (ep))) + (if (= (bitn fcw (ubit)) 1) + (let ((d (drnd u rmode (ep)))) + (if (= d u) + (mv (dencode d (ep)) flags) + (let ((flags (set-flag (pbit) (set-flag (ubit) flags)))) + (if (= d 0) + (mv (zencode rsgn (ep)) flags) + (if (= (abs d) (spn (ep))) + (mv (nencode d (ep)) (set-flag (c1) flags)) + (mv (dencode d (ep)) (if (> (abs d) (abs u)) (set-flag (c1) flags) flags))))))) + (let ((flags (set-flag (ubit) flags)) + (s (* r (expt 2 (* 3 (expt 2 13)))))) + (if (< (abs s) (spn (ep))) + (mv (zencode rsgn (ep)) (set-flag (pbit) flags)) + (mv (nencode s (ep)) (if (> (abs r) (abs u)) (set-flag (c1) flags) flags))))) + (mv (nencode r (ep)) (if (> (abs r) (abs u)) (set-flag (c1) flags) flags)))))) + +(defund x87-binary-comp (op a af b bf fcw) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf) + (natp fcw)))) + (if (or (infp a af) (if (eql op 'div) (zerp b bf) (infp b bf))) + (mv (iencode (binary-inf-sgn op a af b bf) (ep)) 0) + (let* ((asgn (sgnf a af)) + (bsgn (sgnf b bf)) + (aval (decode a af)) + (bval (decode b bf)) + (u (binary-eval op aval bval))) + (if (or (and (eql op 'div) (infp b bf)) (= u 0)) + (mv (zencode (binary-zero-sgn op asgn bsgn (fcw-rc fcw)) (ep)) 0) + (x87-post-comp u fcw))))) + +(defund x87-binary-spec (op a af b bf fcw fsw) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a af) + (encodingp b bf) + (<= (prec af) 64) + (<= (prec bf) 64) + (natp fcw) + (natp fsw)))) + (let ((fsw (clear-c1 fsw))) + (mv-let (result pre-flags) (x87-binary-pre-comp op a af b bf) + (if (unmasked-excp-p pre-flags fcw) + (mv () (set-es (logior fsw pre-flags))) + (if result + (mv result (logior fsw pre-flags)) + (mv-let (result post-flags) (x87-binary-comp op a af b bf fcw) + (mv result + (if (unmasked-excp-p post-flags fcw) + (set-es (logior (logior fsw pre-flags) post-flags)) + (logior (logior fsw pre-flags) post-flags))))))))) + + +;;-------------------------------------------------------------------------------- + +;; The arguments of X87-SQRT-SPEC are a data input, its format, and the initial FCW +;; and FSW registers. It returns a data result, which is NIL in the event of an +;; unmasked pre-computation exception, and the updated FSW. + +(defund x87-sqrt-pre-comp-excp (a f) + (declare (xargs :guard (encodingp a f))) + (if (or (unsupp a f) (snanp a f)) + (set-flag (ibit) 0) + (if (qnanp a f) + 0 + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (set-flag (ibit) 0) + (if (or (denormp a f) (pseudop a f)) + (set-flag (dbit) 0) + 0))))) + +(defund x87-sqrt-pre-comp-val (a f) + (declare (xargs :guard (and (encodingp a f) + (<= (prec f) 64)))) + (if (nanp a f) + (qnanize (convert-nan-to-ep a f) (ep)) + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (indef (ep)) + ()))) + +(defund x87-sqrt-pre-comp (a f) + (declare (xargs :guard (and (encodingp a f) + (<= (prec f) 64)))) + (mv (x87-sqrt-pre-comp-val a f) (x87-sqrt-pre-comp-excp a f))) + +(defund x87-sqrt-comp (a f fcw) + (declare (xargs :guard (and (encodingp a f) + (or (zerp a f) (= (sgnf a f) 0)) + (<= (prec f) 64) + (natp fcw)))) + (if (or (infp a f) (zerp a f)) + (mv a 0) + (x87-post-comp (qsqrt (decode a f) 66) fcw))) + +(defund x87-sqrt-spec (a f fcw fsw) + (declare (xargs :guard (and (encodingp a f) + (<= (prec f) 64) + (natp fcw) + (natp fsw)))) + (let ((fsw (clear-c1 fsw))) + (mv-let (result pre-flags) (x87-sqrt-pre-comp a f) + (if (unmasked-excp-p pre-flags fcw) + (mv () (set-es (logior fsw pre-flags))) + (if result + (mv result (logior fsw pre-flags)) + (mv-let (result post-flags) (x87-sqrt-comp a f fcw) + (mv result + (if (unmasked-excp-p post-flags fcw) + (set-es (logior (logior fsw pre-flags) post-flags)) + (logior (logior fsw pre-flags) post-flags))))))))) +) + +;;;*************************************************************** +;;; ARM Instructions +;;;*************************************************************** + +(defsection-rtl |ARM AArch32 Floating-Point Instructions| + |Floating-Point Exceptions and Specification of Elementary Arithmetic Instructions| + +;; FPSCR bits: + +(defn ioc () 0) ; exception flags +(defn dzc () 1) +(defn ofc () 2) +(defn ufc () 3) +(defn ixc () 4) +(defn idc () 7) + +(defund fpscr-rc (fpscr) ; rounding mode + (declare (xargs :guard (natp fpscr))) + (case (bits fpscr 23 22) + (0 'rne) + (1 'rup) + (2 'rdn) + (3 'rtz))) + +(defn fz () 24) ; force-to-zero mode + +(defn dn () 25) ; default NaN mode + +;; When a NaN is to be returned and DN is set, the default is used: + +(defun process-nan (x fpscr f) + (declare (xargs :guard (and (encodingp x f) + (natp fpscr)))) + (if (= (bitn fpscr (dn)) 1) + (indef f) + (qnanize x f))) + +;;-------------------------------------------------------------------------------- + +;; The arguments of ARM-BINARY-SPEC are an operation (add, sub, mul, or div), 2 data +;; inputs, the initial FPSCR register, and a floating-point format. It returns a data +;; result and the updated FPSCR. + +;; ARM-BINARY-SPEC is based on two auxiliary functions: ARM-BINARY-PRE-COMP returns +;; an optional value and an updated FPSCR, and ARM-BINARY-COMP returns a value +;; and an updated FPSCR. + +;; ARM-BINARY-PRE-COMP calls ARM-BINARY-PRE-COMP-EXCP, which detects pre-computation +;; exceptions, and ARM-BINARY-PRE-COMP-VAL, which may compute a value. If the value +;; is NIL, then the computation proceeds by calling ARM-BINARY-COMP, and if non-NIL, +;; the operation is terminated and that value is returned. + +;; ARM-BINARY-COMP either returns an infinity or decodes the operands and computes the +;; unrounded result. If that result is 0, then it sets the sign according to the operand +;; signs and the rounding mode and returns. Otherwise, it calls ARM-POST-COMP, which detects +;; post-computation exceptions and computes the rounded result. + +(defun arm-binary-pre-comp-excp (op a b fpscr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp fpscr)))) + (if (or (snanp a f) (snanp b f)) + (set-flag (ioc) fpscr) + (if (or (qnanp a f) (qnanp b f)) + fpscr + (if (binary-undefined-p op a f b f) + (set-flag (ioc) fpscr) + (if (and (eql op 'div) (zerp b f) (not (infp a f))) + (set-flag (dzc) fpscr) + fpscr))))) + +(defun arm-binary-pre-comp-val (op a b fpscr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp fpscr)))) + (if (snanp a f) + (process-nan a fpscr f) + (if (snanp b f) + (process-nan b fpscr f) + (if (qnanp a f) + (process-nan a fpscr f) + (if (qnanp b f) + (process-nan b fpscr f) + (if (binary-undefined-p op a f b f) + (indef f) + ())))))) + +(defun arm-binary-pre-comp (op a b fpscr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp fpscr)))) + (mv-let (a b fpscr) + (if (= (bitn fpscr (fz)) 1) + (mv (if (denormp a f) + (zencode (sgnf a f) f) + a) + (if (denormp b f) + (zencode (sgnf b f) f) + b) + (if (and (or (denormp a f) (denormp b f)) + (not (equal f (hp)))) + (set-flag (idc) fpscr) + fpscr)) + (mv a b fpscr)) + (mv a b + (arm-binary-pre-comp-val op a b fpscr f) + (arm-binary-pre-comp-excp op a b fpscr f)))) + +(defun arm-post-comp (u fpscr f) + (declare (xargs :guard (and (real/rationalp u) + (not (= u 0)) + (natp fpscr) + (formatp f)))) + (let* ((rmode (fpscr-rc fpscr)) + (r (rnd u rmode (prec f))) + (sgn (if (< u 0) 1 0))) + (if (> (abs r) (lpn f)) + (let ((fpscr (set-flag (ofc) (set-flag (ixc) fpscr)))) + (if (or (and (eql rmode 'rdn) (> r 0)) + (and (eql rmode 'rup) (< r 0)) + (eql rmode 'rtz)) + (mv (nencode (* (sgn r) (lpn f)) f) + fpscr) + (mv (iencode sgn f) fpscr))) + (if (< (abs u) (spn f)) + (if (= (bitn fpscr (fz)) 1) + (mv (zencode sgn f) (set-flag (ufc) fpscr)) + (let ((d (drnd u rmode f))) + (if (= d u) + (mv (dencode d f) fpscr) + (let ((fpscr (set-flag (ixc) (set-flag (ufc) fpscr)))) + (if (= d 0) + (mv (zencode sgn f) fpscr) + (if (= (abs d) (spn f)) + (mv (nencode d f) fpscr) + (mv (dencode d f) fpscr))))))) + (mv (nencode r f) + (if (= r u) fpscr (set-flag (ixc) fpscr))))))) + +(defun arm-binary-comp (op a b fpscr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp fpscr)))) + (if (or (infp a f) (if (eql op 'div) (zerp b f) (infp b f))) + (mv (iencode (binary-inf-sgn op a f b f) f) fpscr) + (let* ((asgn (sgnf a f)) + (bsgn (sgnf b f)) + (aval (decode a f)) + (bval (decode b f)) + (u (binary-eval op aval bval))) + (if (or (and (eql op 'div) (infp b f)) (= u 0)) + (mv (zencode (binary-zero-sgn op asgn bsgn (fpscr-rc fpscr)) f) fpscr) + (arm-post-comp u fpscr f))))) + +(defun arm-binary-spec (op a b fpscr f) + (declare (xargs :guard (and (member op '(add sub mul div)) + (encodingp a f) + (encodingp b f) + (natp fpscr)))) + (mv-let (a b result fpscr) (arm-binary-pre-comp op a b fpscr f) + (if result + (mv result fpscr) + (arm-binary-comp op a b fpscr f)))) + + +;;-------------------------------------------------------------------------------- + +;; The arguments of ARM-SQRT-SPEC are a data input, the initial FPSCR register, and +;; a floating-point formay. It returns a data result and the updated MXCSR. + +(defun arm-sqrt-pre-comp-excp (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp fpscr)))) + (if (snanp a f) + (set-flag (ioc) fpscr) + (if (qnanp a f) + fpscr + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (set-flag (ioc) fpscr) + fpscr)))) + +(defun arm-sqrt-pre-comp-val (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp fpscr)))) + (if (nanp a f) + (process-nan a fpscr f) + (if (and (not (zerp a f)) (= (sgnf a f) 1)) + (indef f) + ()))) + +(defun arm-sqrt-pre-comp (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp fpscr)))) + (mv-let (a fpscr) + (if (and (denormp a f) (= (bitn fpscr (fz)) 1)) + (mv (zencode (sgnf a f) f) + (if (not (equal f (hp))) + (set-flag (idc) fpscr) + fpscr)) + (mv a fpscr)) + (mv a (arm-sqrt-pre-comp-val a fpscr f) (arm-sqrt-pre-comp-excp a fpscr f)))) + +(defun arm-sqrt-comp (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (or (zerp a f) (= (sgnf a f) 0)) + (natp fpscr)))) + (if (or (infp a f) (zerp a f)) + (mv a fpscr) + (arm-post-comp (qsqrt (decode a f) (+ (prec f) 2)) fpscr f))) + +(defun arm-sqrt-spec (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp fpscr)))) + (mv-let (a result fpscr) (arm-sqrt-pre-comp a fpscr f) + (if result + (mv result fpscr) + (arm-sqrt-comp a fpscr f)))) + + +;;-------------------------------------------------------------------------------- + +;; The arguments of ARM-FMA-SPEC are three data inputs, the initial FPSCR register, and +;; a floating-point format. It returns a data result and the updated FPSCR. +;; Note that this instruction computes A + B * C. + +(defun arm-fma-pre-comp-excp (a b c fpscr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp fpscr)))) + (if (or (snanp a f) (snanp b f) (snanp c f) + (fma-undefined-p b c a f)) + (set-flag (ioc) fpscr) + fpscr)) + +(defun arm-fma-pre-comp-val (a b c fpscr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp fpscr)))) + (if (snanp a f) + (process-nan a fpscr f) + (if (snanp b f) + (process-nan b fpscr f) + (if (snanp c f) + (process-nan c fpscr f) + (if (fma-undefined-p b c a f) + (indef f) + (if (qnanp a f) + (process-nan a fpscr f) + (if (qnanp b f) + (process-nan b fpscr f) + (if (qnanp c f) + (process-nan c fpscr f) + ())))))))) + +(defun arm-fma-pre-comp (a b c fpscr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp fpscr)))) + (mv-let (a b c fpscr) + (if (= (bitn fpscr (fz)) 1) + (mv (if (denormp a f) + (zencode (sgnf a f) f) + a) + (if (denormp b f) + (zencode (sgnf b f) f) + b) + (if (denormp c f) + (zencode (sgnf c f) f) + c) + (if (and (or (denormp a f) (denormp b f) (denormp c f)) + (not (equal f (hp)))) + (set-flag (idc) fpscr) + fpscr)) + (mv a b c fpscr)) + (mv a b c + (arm-fma-pre-comp-val a b c fpscr f) + (arm-fma-pre-comp-excp a b c fpscr f)))) + +(defun arm-fma-comp (a b c fpscr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp fpscr)))) + (let* ((asgn (sgnf a f)) + (bsgn (sgnf b f)) + (csgn (sgnf c f)) + (aval (decode a f)) + (bval (decode b f)) + (cval (decode c f)) + (u (+ aval (* bval cval)))) + (if (or (infp b f) (infp c f)) + (mv (iencode (logxor bsgn csgn) f) fpscr) + (if (infp a f) + (mv a fpscr) + (if (= u 0) + (mv (zencode (if (= (logxor bsgn csgn) asgn) + asgn + (if (eql (fpscr-rc fpscr) 'rdn) 1 0)) + f) + fpscr) + (arm-post-comp u fpscr f)))))) + +(defun arm-fma-spec (a b c fpscr f) + (declare (xargs :guard (and (encodingp a f) + (encodingp b f) + (encodingp c f) + (natp fpscr)))) + (mv-let (a b c result fpscr) (arm-fma-pre-comp a b c fpscr f) + (if result + (mv result fpscr) + (arm-fma-comp a b c fpscr f)))) + +;;-------------------------------------------------------------------------------- + +;; The operands of FSCALE are a floating-point encoding A and a signed integer +;; encoding B. The exponent of A is incremented by the value of B. +;; The arguments of ARM-FSCALE-SPEC are these two operands, the initial FPSCR +;; register, and a floating-point format. It returns a data result and the updated +;; FPSCR. + +(defun arm-fscale-pre-comp (a fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp fpscr)))) + (mv-let (a fpscr) + (if (and (= (bitn fpscr (fz)) 1) + (denormp a f)) + (mv (zencode (sgnf a f) f) + (set-flag (idc) fpscr)) + (mv a fpscr)) + (mv a + (if (nanp a f) + (process-nan a fpscr f) + ()) + (if (snanp a f) + (set-flag (ioc) fpscr) + fpscr)))) + +(defun arm-fscale-comp (a b fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp b) + (natp fpscr)) + :guard-hints (("Goal" :in-theory (enable decode-0))))) + (if (or (zerp a f) (infp a f)) + (mv a fpscr) + (let* ((fwidth (+ 1 (expw f) (sigw f))) + (aval (decode a f)) + (bval (si b fwidth)) + (u (* aval (expt 2 bval)))) + (arm-post-comp u fpscr f)))) + +(defun arm-fscale-spec (a b fpscr f) + (declare (xargs :guard (and (encodingp a f) + (natp b) + (natp fpscr)))) + (mv-let (a result fpscr) (arm-fscale-pre-comp a fpscr f) + (if result + (mv result fpscr) + (arm-fscale-comp a b fpscr f)))) + +;;-------------------------------------------------------------------------------- + +;; We include specifications of two operations in support of the Arm BFloat16 +;; instructions, which are based on the 16-bit BF format (8-bit exponent, 7-bit +;; mantissa). These instructions do not access FPSCR or set exception flags, +;; but assume DN = FZ = 1, return infinity of overflow, and use round-to-odd. + +;; Odd-round a non-zero value u and encode in the SP format, with overflow +;; resulting in an infinity and underflow resulting in a zero: + +(defund bf-post-comp (u) + (declare (xargs :guard (and (real/rationalp u) + (not (= u 0))))) + (let ((sgnf (if (< u 0) 1 0)) + (r (rto u 24))) + (if (> (abs r) (lpn (sp))) + (iencode sgnf (sp)) + (if (< (abs u) (spn (sp))) + (zencode sgnf (sp)) + (nencode r (sp)))))) + +;; SP product of 2 BF operands. Note that in the case of a normal result, +;; the product is 16-exact so no rounding is required: + +(defund bfmul16-spec (a b) + (declare (xargs :guard (and (encodingp a (bf)) + (encodingp b (bf))))) + (let ((sgnr (logxor (sgnf a (bf)) (sgnf b (bf))))) + (if (or (nanp a (bf)) + (nanp b (bf)) + (and (infp a (bf)) + (or (zerp b (bf)) (denormp b (bf)))) + (and (infp b (bf)) + (or (zerp a (bf)) (denormp a (bf))))) + (indef (sp)) + (if (or (infp a (bf)) (infp b (bf))) + (iencode sgnr (sp)) + (if (or (zerp a (bf)) (denormp a (bf)) + (zerp b (bf)) (denormp b (bf))) + (zencode sgnr (sp)) + (bf-post-comp (* (ndecode a (bf)) (ndecode b (bf))))))))) + +;; SP sum of 2 SP operands: + +(defund bfadd32-spec (a b) + (declare (xargs :guard (and (encodingp a (sp)) + (encodingp b (sp))))) + (let* ((sgna (sgnf a (sp))) + (sgnb (sgnf b (sp))) + (aval (if (or (zerp a (sp)) (denormp a (sp))) 0 (ndecode a (sp)))) + (bval (if (or (zerp b (sp)) (denormp b (sp))) 0 (ndecode b (sp)))) + (u (+ aval bval))) + (if (or (nanp a (sp)) + (nanp b (sp)) + (and (infp a (sp)) (infp b (sp)) (not (= sgna sgnb)))) + (indef (sp)) + (if (infp a (sp)) + a + (if (infp b (sp)) + b + (if (= u 0) + (if (= sgna sgnb) + (zencode sgna (sp)) + (zencode 0 (sp))) + (bf-post-comp u))))))) + +) + diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/float.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/float.lisp new file mode 100644 index 000000000..ff9ab673e --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/float.lisp @@ -0,0 +1,507 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; +; Contact: +; David M. Russinoff +; david@russinoff.com +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; + +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") + +;;;********************************************************************** +;;; Sign, Significand, and Exponent +;;;********************************************************************** + +(defsection-rtl |Floating-Point Decomposition| |Floating-Point Numbers| + +(defnd sgn (x) + (if (or (not (rationalp x)) (equal x 0)) + 0 + (if (< x 0) -1 +1))) + +(defnd expo (x) + (declare (xargs :measure (:? x) +; Matt K. addition after bug fix to require well-founded relations to match for +; a redundant definition: + :well-founded-relation e0-ord-<)) + (mbe :logic (cond ((or (not (rationalp x)) (equal x 0)) 0) + ((< x 0) (expo (- x))) + ((< x 1) (1- (expo (* 2 x)))) + ((< x 2) 0) + (t (1+ (expo (/ x 2))))) + :exec (if (rationalp x) + (let* ((n (abs (numerator x))) + (d (denominator x)) + (ln (integer-length n)) + (ld (integer-length d)) + (l (- ln ld))) + (if (>= ln ld) + (if (>= (ash n (- l)) d) l (1- l)) + (if (> ln 1) + (if (> n (ash d l)) l (1- l)) + (- (integer-length (1- d)))))) + 0))) + +(defnd sig (x) + (if (rationalp x) + (if (< x 0) + (- (* x (expt 2 (- (expo x))))) + (* x (expt 2 (- (expo x))))) + 0)) + +(defthm expo-minus + (implies (rationalp x) + (equal (expo (- x)) (expo x)))) + +(defthm sig-minus + (implies (rationalp x) + (equal (sig (- x)) (sig x)))) + +(defthmd expo-lower-bound + (implies (and (rationalp x) + (not (equal x 0))) + (<= (expt 2 (expo x)) (abs x))) + :rule-classes :linear) + +(defthmd expo-upper-bound + (implies (and (rationalp x)) + (< (abs x) (expt 2 (1+ (expo x))))) + :rule-classes :linear) + +(defthm expo-unique + (implies (and (<= (expt 2 n) (abs x)) + (< (abs x) (expt 2 (1+ n))) + (rationalp x) + (integerp n)) + (equal n (expo x))) + :rule-classes ()) + +(defthm fp-rep + (implies (rationalp x) + (equal x (* (sgn x) (sig x) (expt 2 (expo x))))) + :rule-classes ()) + +(defthm fp-abs + (implies (rationalp x) + (equal (abs x) (* (sig x) (expt 2 (expo x))))) + :rule-classes ()) + +(defthmd expo>= + (implies (and (<= (expt 2 n) x) + (rationalp x) + (integerp n)) + (<= n (expo x))) + :rule-classes :linear) + +(defthmd expo<= + (implies (and (< x (* 2 (expt 2 n))) + (< 0 x) + (rationalp x) + (integerp n)) + (<= (expo x) n)) + :rule-classes :linear) + +(defthm expo-2**n + (implies (integerp n) + (equal (expo (expt 2 n)) + n))) + +(defthmd bitn-expo + (implies (not (zp x)) + (equal (bitn x (expo x)) 1))) + +(defthmd expo-monotone + (implies (and (<= (abs x) (abs y)) + (case-split (rationalp x)) + (case-split (not (equal x 0))) + (case-split (rationalp y))) + (<= (expo x) (expo y))) + :rule-classes :linear) + +(defthmd bvecp-expo + (implies (case-split (natp x)) + (bvecp x (1+ (expo x))))) + +(defthmd mod-expo + (implies (and (< 0 x) + (rationalp x)) + (equal (mod x (expt 2 (expo x))) + (- x (expt 2 (expo x)))))) + +(defthmd sig-lower-bound + (implies (and (rationalp x) + (not (equal x 0))) + (<= 1 (sig x))) + :rule-classes (:rewrite :linear)) + +(defthmd sig-upper-bound + (< (sig x) 2) + :rule-classes (:rewrite :linear)) + +(defthm expo-sig + (implies (rationalp x) + (equal (expo (sig x)) 0))) + +(defthmd sig-self + (implies (and (rationalp x) + (<= 1 x) + (< x 2)) + (equal (sig x) x))) + +(defthm sig-sig + (equal (sig (sig x)) + (sig x))) + +(defthm fp-rep-unique + (implies (and (rationalp x) + (rationalp m) + (<= 1 m) + (< m 2) + (integerp e) + (= (abs x) (* m (expt 2 e)))) + (and (= m (sig x)) + (= e (expo x)))) + :rule-classes ()) + +(defthmd sgn-shift + (equal (sgn (* x (expt 2 k))) + (sgn x))) + +(defthmd expo-shift + (implies (and (rationalp x) + (not (equal x 0)) + (integerp n)) + (equal (expo (* (expt 2 n) x)) + (+ n (expo x))))) + +(defthmd sig-shift + (equal (sig (* (expt 2 n) x)) + (sig x))) + +(defthmd sgn-prod + (implies (and (case-split (rationalp x)) + (case-split (rationalp y))) + (equal (sgn (* x y)) + (* (sgn x) (sgn y))))) + +(defthmd expo-prod + (implies (and (rationalp x) + (not (= x 0)) + (rationalp y) + (not (= y 0))) + (equal (expo (* x y)) + (if (< (* (sig x) (sig y)) 2) + (+ (expo x) (expo y)) + (+ 1 (expo x) (expo y)))))) + +(defthmd expo-prod-lower + (implies (and (rationalp x) + (not (= x 0)) + (rationalp y) + (not (= y 0))) + (<= (+ (expo x) (expo y)) (expo (* x y)))) + :rule-classes :linear) + +(defthmd expo-prod-upper + (implies (and (rationalp x) + (not (= x 0)) + (rationalp y) + (not (= y 0))) + (>= (+ (expo x) (expo y) 1) (expo (* x y)))) + :rule-classes :linear) + +(defthmd sig-prod + (implies (and (rationalp x) + (not (= x 0)) + (rationalp y) + (not (= y 0))) + (equal (sig (* x y)) + (if (< (* (sig x) (sig y)) 2) + (* (sig x) (sig y)) + (* 1/2 (sig x) (sig y)))))) + +(defthmd expo-fl + (implies (and (rationalp x) + (>= x 1)) + (equal (expo (fl x)) (expo x)))) + +(defthmd expo-logior + (implies (and (natp x) + (natp y) + (<= (expo x) (expo y))) + (equal (expo (logior x y)) + (expo y)))) +) + +;;;********************************************************************** +;;; Exactness +;;;********************************************************************** + +(defsection-rtl |Exactness| |Floating-Point Numbers| + +(defund exactp (x n) + (declare (xargs :guard (and (real/rationalp x) + (integerp n)))) + (integerp (* (sig x) (expt 2 (1- n))))) + +(defthmd exactp2 + (implies (and (rationalp x) + (integerp n)) + (equal (exactp x n) + (integerp (* x (expt 2 (- (1- n) (expo x)))))))) + +(defthm exactp-sig + (equal (exactp (sig x) n) + (exactp x n))) + +(defthm minus-exactp + (equal (exactp (- x) n) + (exactp x n))) + +(defthm exactp-abs + (equal (exactp (abs x) n) + (exactp x n))) + +(defthmd exactp-shift + (implies (and (rationalp x) + (integerp k) + (integerp n)) + (equal (exactp (* (expt 2 k) x) n) + (exactp x n)))) + +(defthmd exactp-<= + (implies (and (exactp x m) + (<= m n) + (rationalp x) + (integerp n) + (integerp m)) + (exactp x n))) + +(defthmd exactp-2**n + (implies (and (case-split (integerp m)) + (case-split (> m 0))) + (exactp (expt 2 n) m))) + +(defthm bvecp-exactp + (implies (bvecp x n) + (exactp x n))) + +(defthm exactp-prod + (implies (and (rationalp x) + (rationalp y) + (integerp m) + (integerp n) + (exactp x m) + (exactp y n)) + (exactp (* x y) (+ m n))) + :rule-classes ()) + +(defthm exactp-x2 + (implies (and (rationalp x) + (integerp n) + (exactp (* x x) (* 2 n))) + (exactp x n)) + :rule-classes ()) + +(defthm exactp-factors + (implies (and (rationalp x) + (rationalp y) + (integerp k) + (integerp n) + (not (zerop x)) + (not (zerop y)) + (exactp x k) + (exactp y k) + (exactp (* x y) n)) + (exactp x n)) + :rule-classes ()) + +(defthm exact-bits-1 + (implies (and (equal (expo x) (1- n)) + (rationalp x) + (integerp k)) + (equal (integerp (/ x (expt 2 k))) + (exactp x (- n k)))) + :rule-classes ()) + +(defthm exact-bits-2 + (implies (and (equal (expo x) (1- n)) + (rationalp x) + (<= 0 x) + (integerp k) + ) + (equal (integerp (/ x (expt 2 k))) + (equal (bits x (1- n) k) + (/ x (expt 2 k))))) + :rule-classes ()) + +(defthm exact-bits-3 + (implies (integerp x) + (equal (integerp (/ x (expt 2 k))) + (equal (bits x (1- k) 0) + 0))) + :rule-classes ()) + +(defthm exact-k+1 + (implies (and (natp n) + (natp x) + (= (expo x) (1- n)) + (natp k) + (< k (1- n)) + (exactp x (- n k))) + (iff (exactp x (1- (- n k))) + (= (bitn x k) 0))) + :rule-classes ()) + +(defthm exactp-diff + (implies (and (rationalp x) + (rationalp y) + (integerp k) + (integerp n) + (> n 0) + (> n k) + (exactp x n) + (exactp y n) + (<= (+ k (expo (- x y))) (expo x)) + (<= (+ k (expo (- x y))) (expo y))) + (exactp (- x y) (- n k))) + :rule-classes ()) + +(defthm exactp-diff-cor + (implies (and (rationalp x) + (rationalp y) + (integerp n) + (> n 0) + (exactp x n) + (exactp y n) + (<= (abs (- x y)) (abs x)) + (<= (abs (- x y)) (abs y))) + (exactp (- x y) n)) + :rule-classes ()) + +(defun fp+ (x n) + (declare (xargs :guard (and (real/rationalp x) + (integerp n)))) + (+ x (expt 2 (- (1+ (expo x)) n)))) + +(defthm fp+-positive + (implies (<= 0 x) + (< 0 (fp+ x n))) + :rule-classes :type-prescription) + +(defthm fp+1 + (implies (and (rationalp x) + (> x 0) + (integerp n) + (> n 0) + (exactp x n)) + (exactp (fp+ x n) n)) + :rule-classes ()) + +(defthm fp+2 + (implies (and (rationalp x) + (> x 0) + (rationalp y) + (> y x) + (integerp n) + (> n 0) + (exactp x n) + (exactp y n)) + (>= y (fp+ x n))) + :rule-classes ()) + +(defthm fp+expo + (implies (and (rationalp x) + (> x 0) + (integerp n) + (> n 0) + (exactp x n) + (not (= (expo (fp+ x n)) (expo x)))) + (equal (fp+ x n) (expt 2 (1+ (expo x))))) + :rule-classes ()) + +(defthm expo-diff-min + (implies (and (rationalp x) + (rationalp y) + (integerp n) + (> n 0) + (exactp x n) + (exactp y n) + (not (= y x))) + (>= (expo (- y x)) (- (1+ (min (expo x) (expo y))) n))) + :rule-classes ()) + +(defun fp- (x n) + (declare (xargs :guard (and (real/rationalp x) + (integerp n)))) + (if (= x (expt 2 (expo x))) + (- x (expt 2 (- (expo x) n))) + (- x (expt 2 (- (1+ (expo x)) n))))) + +(defthm fp--non-negative + (implies (and (rationalp x) + (integerp n) + (> n 0) + (> x 0)) + (and (rationalp (fp- x n)) + (< 0 (fp- x n)))) + :rule-classes :type-prescription) + +(defthm exactp-fp- + (implies (and (rationalp x) + (> x 0) + (integerp n) + (> n 0) + (exactp x n)) + (exactp (fp- x n) n))) + +(defthm fp+- + (implies (and (rationalp x) + (> x 0) + (integerp n) + (> n 0) + (exactp x n)) + (equal (fp+ (fp- x n) n) + x))) + +(defthm fp-+ + (implies (and (rationalp x) + (> x 0) + (integerp n) + (> n 0) + (exactp x n)) + (equal (fp- (fp+ x n) n) + x))) + +(defthm fp-2 + (implies (and (rationalp x) + (rationalp y) + (> y 0) + (> x y) + (integerp n) + (> n 0) + (exactp x n) + (exactp y n)) + (<= y (fp- x n))) + :rule-classes ()) + + (defthmd expo-fp- + (implies (and (rationalp x) + (> x 0) + (not (= x (expt 2 (expo x)))) + (integerp n) + (> n 0) + (exactp x n)) + (equal (expo (fp- x n)) (expo x)))) +) diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/gl.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/gl.lisp new file mode 100644 index 000000000..f3e0c797f --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/gl.lisp @@ -0,0 +1,88 @@ +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(include-book "centaur/gl/gl" :dir :system) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") +(include-book "rac") + +;;;********************************************************************** + +(defrule bits-for-gl + (equal (bits x i j) + (if (and (integerp x) + (integerp i) + (integerp j)) + (if (< i j) + 0 + (logand (ash x (- j)) + (1- (ash 1 (1+ (- i j)))))) + (let ((msg (cw "WARNING: guard violation for the function BITS during a GL proof:~%(BITS ~x0 ~x1 ~x2)~%" x i j))) + ;(let ((msg (cw "WARNING: guard violation for the function BITS during a GL proof.~%"))) + (declare (ignore msg)) + (if (or (not (integerp i)) + (not (integerp j))) + 0 + (fl (/ (mod x (expt 2 (1+ i))) (expt 2 j))))))) + :rule-classes ()) + +(gl::set-preferred-def bits bits-for-gl) + +(defthm bitn-for-gl + (equal (bitn x n) + (if (and (integerp x) + (integerp n)) + (if (< n 0) + 0 + (if (logbitp n x) 1 0)) + (let ((msg (cw "WARNING: guard violation for the function BITN during a GL proof:~%(BITN ~x0 ~x1)~%" x n))) + ;(let ((msg (cw "WARNING: guard violation for the function BITN during a GL proof.~%"))) + (declare (ignore msg)) + (if (not (integerp n)) + 0 + (fl (/ (mod x (expt 2 (1+ n))) (expt 2 n))))))) + :rule-classes ()) + +(gl::set-preferred-def bitn bitn-for-gl) + +(defthm binary-cat-for-gl + (equal (binary-cat x m y n) + (if (and (natp m) + (natp n)) + (logior (ash (BITS X (1- M) 0) n) + (BITS Y (1- N) 0)) + 0)) + :rule-classes ()) + +(gl::set-preferred-def binary-cat binary-cat-for-gl) + +(defthm expo-for-gl + (equal (expo x) + (if (integerp x) + (if (= x 0) + 0 + (1- (integer-length (abs x)))) + (let ((msg (cw "WARNING: guard violation for the function EXPO during a GL proof.~%"))) + (declare (ignore msg)) + (expo x)))) + :rule-classes ()) + +(gl::set-preferred-def expo expo-for-gl) + +(gl::def-gl-rewrite ag-of-as + (equal (ag a (as wa v r)) + (if (equal a wa) v (ag a r)))) + +(gl::def-gl-rewrite ag-of-nil + (equal (ag a nil) (default-get-valu))) + +(gl::gl-set-uninterpreted ag) + +(gl::gl-set-uninterpreted as) + diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/log.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/log.lisp new file mode 100644 index 000000000..058bdf4c1 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/log.lisp @@ -0,0 +1,545 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; +; Contact: +; David M. Russinoff +; david@russinoff.com +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; + +(in-package "RTL") + +(set-enforce-redundancy t) ; for some reason, acl2 4.3 complains about logand-natp + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") + +;;;********************************************************************** +;;; LOGAND, LOGIOR, and LOGXOR +;;;********************************************************************** + +(defsection-rtl |Binary Operations| |Logical Operations| + +(in-theory (disable logand logior logxor)) + +(defthmd logand-def + (equal (logand x y) + (if (or (zip x) (zip y)) + 0 + (if (= x y) + x + (+ (* 2 (logand (fl (/ x 2)) (fl (/ y 2)))) + (logand (mod x 2) (mod y 2)))))) + :rule-classes ((:definition :controller-alist ((binary-logand t t))))) + +(defthmd logior-def + (implies (and (integerp x) (integerp y)) + (equal (logior x y) + (if (or (zip x) (= x y)) + y + (if (zip y) + x + (+ (* 2 (logior (fl (/ x 2)) (fl (/ y 2)))) + (logior (mod x 2) (mod y 2))))))) + :rule-classes ((:definition :controller-alist ((binary-logior t t))))) + +(defthmd logxor-def + (implies (and (integerp x) (integerp y)) + (equal (logxor x y) + (if (zip x) + y + (if (zip y) + x + (if (= x y) + 0 + (+ (* 2 (logxor (fl (/ x 2)) (fl (/ y 2)))) + (logxor (mod x 2) (mod y 2)))))))) + :rule-classes ((:definition :controller-alist ((binary-logxor t t))))) + +(defun log-induct (x y) + (declare (xargs :measure (abs (* (ifix x) (ifix y))))) + (if (or (not (integerp x)) (not (integerp y)) (= x 0) (= y 0) (= x y)) + (+ x y) + (log-induct (fl (/ x 2)) (fl (/ y 2))))) + +(defthm logand-bvecp + (implies (and (natp n) + (bvecp x n) + (integerp y)) + (bvecp (logand x y) n))) + +(defthm logior-bvecp + (implies (and (bvecp x n) + (bvecp y n)) + (bvecp (logior x y) n))) + +(defthm logxor-bvecp + (implies (and (bvecp x n) + (bvecp y n) + (natp n)) + (bvecp (logxor x y) n))) + +(defthmd logand-plus-logxor + (implies (and (integerp x) + (integerp y)) + (equal (+ (logand x y) (logxor x y)) + (logior x y)))) + +(defthmd logand-mod + (implies (and (integerp x) + (integerp y) + (integerp n)) + (equal (mod (logand x y) (expt 2 n)) + (logand (mod x (expt 2 n)) + (mod y (expt 2 n)))))) + +(defthmd logior-mod + (implies (and (integerp x) + (integerp y) + (integerp n)) + (equal (mod (logior x y) (expt 2 n)) + (logior (mod x (expt 2 n)) + (mod y (expt 2 n)))))) + +(defthmd logxor-mod + (implies (and (integerp x) + (integerp y) + (integerp n)) + (equal (mod (logxor x y) (expt 2 n)) + (logxor (mod x (expt 2 n)) + (mod y (expt 2 n)))))) + +(defthmd fl-logand + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (fl (* (expt 2 (- n)) (logand x y))) + (logand (fl (* (expt 2 (- n)) x)) (fl (* (expt 2 (- n)) y)))))) + +(defthmd fl-logior + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (fl (* (expt 2 (- n)) (logior x y))) + (logior (fl (* (expt 2 (- n)) x)) (fl (* (expt 2 (- n)) y)))))) + +(defthmd fl-logxor + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (fl (* (expt 2 (- n)) (logxor x y))) + (logxor (fl (* (expt 2 (- n)) x)) (fl (* (expt 2 (- n)) y)))))) + + +(defthmd logand-cat + (implies (and (case-split (integerp x1)) + (case-split (integerp y1)) + (case-split (integerp x2)) + (case-split (integerp y2)) + (case-split (natp n)) + (case-split (natp m))) + (equal (logand (cat x1 m y1 n) (cat x2 m y2 n)) + (cat (logand x1 x2) m (logand y1 y2) n)))) + +(defthmd logior-cat + (implies (and (case-split (integerp x1)) + (case-split (integerp y1)) + (case-split (integerp x2)) + (case-split (integerp y2)) + (case-split (natp n)) + (case-split (natp m))) + (equal (logior (cat x1 m y1 n) (cat x2 m y2 n)) + (cat (logior x1 x2) m (logior y1 y2) n)))) + +(defthmd logxor-cat + (implies (and (case-split (integerp x1)) + (case-split (integerp y1)) + (case-split (integerp x2)) + (case-split (integerp y2)) + (case-split (natp n)) + (case-split (natp m))) + (equal (logxor (cat x1 m y1 n) (cat x2 m y2 n)) + (cat (logxor x1 x2) m (logxor y1 y2) n)))) + +(defthmd logand-shift + (implies (and (integerp x) + (integerp y) + (natp k)) + (equal (logand (* (expt 2 k) x) + (* (expt 2 k) y)) + (* (expt 2 k) (logand x y))))) + +(defthmd logior-shift + (implies (and (integerp x) + (integerp y) + (natp k)) + (equal (logior (* (expt 2 k) x) + (* (expt 2 k) y)) + (* (expt 2 k) (logior x y))))) + +(defthmd logxor-shift + (implies (and (integerp x) + (integerp y) + (natp k)) + (equal (logxor (* (expt 2 k) x) + (* (expt 2 k) y)) + (* (expt 2 k) (logxor x y))))) + +(defthmd logand-expt + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (logand (* (expt 2 n) x) y) + (* (expt 2 n) (logand x (fl (/ y (expt 2 n)))))))) + +(defthmd logior-expt + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (logior (* (expt 2 n) x) y) + (+ (* (expt 2 n) (logior x (fl (/ y (expt 2 n))))) + (mod y (expt 2 n)))))) + +(defthmd logxor-expt + (implies (and (integerp x) + (integerp y) + (natp n)) + (equal (logxor (* (expt 2 n) x) y) + (+ (* (expt 2 n) (logxor x (fl (/ y (expt 2 n))))) + (mod y (expt 2 n)))))) + +(defthmd logior-expt-cor + (implies (and (natp n) + (integerp x) + (bvecp y n)) + (equal (logior (* (expt 2 n) x) y) + (+ (* (expt 2 n) x) y)))) + +(defthmd logior-2**n + (implies (and (natp n) + (integerp x)) + (equal (logior (expt 2 n) x) + (if (= (bitn x n) 1) + x + (+ x (expt 2 n)))))) + +(defthmd logand-bits + (implies (and (integerp x) + (natp n) + (natp k) + (< k n)) + (equal (logand x (- (expt 2 n) (expt 2 k))) + (* (expt 2 k) (bits x (1- n) k))))) + +(defthmd logand-bit + (implies (and (integerp x) + (natp n)) + (equal (logand x (expt 2 n)) + (* (expt 2 n) (bitn x n))))) + +(defthmd bits-logand + (implies (and (integerp x) + (integerp y) + (integerp i) + (integerp j)) + (equal (bits (logand x y) i j) + (logand (bits x i j) (bits y i j))))) + +(defthmd bits-logior + (implies (and (integerp x) + (integerp y) + (integerp i) + (integerp j)) + (equal (bits (logior x y) i j) + (logior (bits x i j) (bits y i j))))) + +(defthmd bits-logxor + (implies (and (integerp x) + (integerp y) + (integerp i) + (integerp j)) + (equal (bits (logxor x y) i j) + (logxor (bits x i j) (bits y i j))))) + +(defthmd bitn-logand + (implies (and (integerp x) + (integerp y) + (integerp n)) + (equal (bitn (logand x y) n) + (logand (bitn x n) (bitn y n))))) + +(defthmd bitn-logior + (implies (and (integerp x) + (integerp y) + (integerp n)) + (equal (bitn (logior x y) n) + (logior (bitn x n) (bitn y n))))) + +(defthmd bitn-logxor + (implies (and (case-split (integerp x)) + (case-split (integerp y)) + (case-split (integerp n))) + (equal (bitn (logxor x y) n) + (logxor (bitn x n) (bitn y n))))) +) + +;;;********************************************************************** +;;; LOGNOT +;;;********************************************************************** + +(defsection-rtl |Complementation| |Logical Operations| + +(in-theory (disable lognot)) + +(defthmd lognot-def + (implies (integerp x) + (equal (lognot x) + (1- (- x))))) + +(defthmd lognot-shift + (implies (and (integerp x) + (natp k)) + (equal (lognot (* (expt 2 k) x)) + (+ (* (expt 2 k) (lognot x)) + (1- (expt 2 k)))))) + +(defthmd lognot-fl + (implies (and (integerp x) + (not (zp n))) + (equal (lognot (fl (/ x n))) + (fl (/ (lognot x) n))))) + +(defthmd mod-lognot + (implies (and (integerp x) + (natp n)) + (equal (mod (lognot x) (expt 2 n)) + (1- (- (expt 2 n) (mod x (expt 2 n))))))) + +(defthmd bits-lognot + (implies (and (natp i) + (natp j) + (<= j i) + (integerp x)) + (equal (bits (lognot x) i j) + (- (1- (expt 2 (- (1+ i) j))) (bits x i j))))) + +(defthm bitn-lognot + (implies (and (integerp x) + (natp n)) + (not (equal (bitn (lognot x) n) + (bitn x n)))) + :rule-classes ()) + +(defthmd bits-lognot-bits + (implies (and (integerp x) + (natp i) + (natp j) + (natp k) + (natp l) + (<= l k) + (<= k (- i j))) + (equal (bits (lognot (bits x i j)) k l) + (bits (lognot x) (+ k j) (+ l j))))) + +(defthmd bits-lognot-bits-lognot + (implies (and (integerp x) + (natp i) + (natp j) + (natp k) + (natp l) + (<= l k) + (<= k (- i j))) + (equal (bits (lognot (bits (lognot x) i j)) k l) + (bits x (+ k j) (+ l j))))) + +(defthmd logand-bits-lognot + (implies (and (integerp x) + (integerp n) + (bvecp y n)) + (equal (logand y (bits (lognot x) (1- n) 0)) + (logand y (lognot (bits x (1- n) 0)))))) +) + +;;;********************************************************************** +;;; Algebraic Properties +;;;********************************************************************** + +(defsection-rtl |Algebraic Properties| |Logical Operations| + +(defthm logand-x-0 + (equal (logand x 0) 0)) + +(defthm logand-0-y + (equal (logand 0 y) 0)) + +(defthm logior-x-0 + (implies (integerp x) + (equal (logior x 0) x))) + +(defthm logior-0-y + (implies (integerp y) + (equal (logior 0 y) y))) + +(defthm logxor-x-0 + (implies (integerp x) + (equal (logxor x 0) x))) + +(defthm logxor-0-y + (implies (integerp y) + (equal (logxor 0 y) y))) + +(defthm logand-self + (implies (case-split (integerp x)) + (equal (logand x x) x))) + +(defthm logior-self + (implies (case-split (integerp x)) + (equal (logior x x) x))) + +(defthm logxor-self + (equal (logxor x x) 0)) + +(defthm lognot-lognot + (implies (case-split (integerp i)) + (equal (lognot (lognot i)) + i))) + +(defthmd logior-not-0 + (implies (and (integerp x) + (integerp y)) + (iff (equal (logior x y) 0) + (and (= x 0) (= y 0))))) + +(defthmd logxor-not-0 + (implies (and (integerp x) + (integerp y)) + (iff (equal (logxor x y) 0) + (= x y)))) + +(defthm logand-x-1 + (implies (bvecp x 1) + (equal (logand x 1) x))) + +(defthm logand-1-x + (implies (bvecp x 1) + (equal (logand 1 x) x))) + +(defthm logior-1-x + (implies (bvecp x 1) + (equal (logior 1 x) 1))) + +(defthm logior-x-1 + (implies (bvecp x 1) + (equal (logior x 1) 1))) + +(defthm logand-x-m1 + (implies (integerp x) + (equal (logand x -1) x))) + +(defthm logand-m1-y + (implies (integerp y) + (equal (logand -1 y) y))) + +(defthm logior-x-m1 + (implies (integerp x) + (equal (logior x -1) -1))) + +(defthm logior-m1-y + (implies (integerp y) + (equal (logior -1 y) -1))) + +(defthm logxor-x-m1 + (implies (integerp x) + (equal (logxor x -1) + (lognot x)))) + +(defthm logxor-m1-x + (implies (integerp x) + (equal (logxor -1 x) + (lognot x)))) + +(defthm logand-commutative + (equal (logand y x) (logand x y))) + +(defthm logior-commutative + (equal (logior y x) (logior x y))) + +(defthm logxor-commutative + (equal (logxor y x) (logxor x y))) + +(defthm logand-commutative-2 + (equal (logand y x z) + (logand x y z))) + +(defthm logior-commutative-2 + (equal (logior y x z) + (logior x y z))) + +(defthm logxor-commutative-2 + (equal (logxor y x z) + (logxor x y z))) + +(defthm logand-associative + (equal (logand (logand x y) z) + (logand x (logand y z)))) + +(defthm logior-associative + (equal (logior (logior x y) z) + (logior x (logior y z)))) + +(defthm logxor-associative + (equal (logxor (logxor x y) z) + (logxor x (logxor y z)))) + +(defthmd logior-logand + (implies (and (integerp x) + (integerp y) + (integerp z)) + (equal (logior x (logand y z)) + (logand (logior x y) (logior x z))))) + +(defthmd logior-logand-1 + (implies (and (integerp x) + (integerp y)) + (equal (logior x (logand x y)) + x))) + +(defthmd logand-logior + (implies (and (integerp x) + (integerp y) + (integerp z)) + (equal (logand x (logior y z)) + (logior (logand x y) (logand x z))))) + +(defthmd logior-logand-2 + (implies (and (integerp x) + (integerp y) + (integerp z)) + (equal (logand (logior y z) x) + (logior (logand y x) (logand z x))))) + +(defthmd log3 + (implies (and (integerp x) + (integerp y) + (integerp z)) + (equal (logior (logand x y) (logior (logand x z) (logand y z))) + (logior (logand x y) (logand (logxor x y) z))))) + +(defthmd logxor-rewrite + (implies (and (integerp x) + (integerp y)) + (equal (logxor x y) + (logior (logand x (lognot y)) + (logand y (lognot x)))))) + +(defthmd lognot-logxor + (and (equal (logxor (lognot x) y) + (lognot (logxor x y))) + (equal (logxor y (lognot x)) + (lognot (logxor x y))))) +) diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/mult.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/mult.lisp new file mode 100644 index 000000000..a339f65d8 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/mult.lisp @@ -0,0 +1,632 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; +; Contact: +; David M. Russinoff +; david@russinoff.com +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; + +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") +(local (in-theory nil)) + +(include-book "defs") +(include-book "rac") + +;;;********************************************************************** +;;; Radix-4 Booth Encoding +;;;********************************************************************** + +(defsection-rtl |Radix-4 Booth Encoding| |Multiplication| + +(defun theta (i y) + (+ (bitn y (1- (* 2 i))) + (bitn y (* 2 i)) + (* -2 (bitn y (1+ (* 2 i)))))) + +(defthm theta-bounds + (and (<= -2 (theta i y)) + (<= (theta i y) 2)) + :rule-classes :linear) + +(defun sum-theta (m y) + (if (zp m) + 0 + (+ (* (expt 2 (* 2 (1- m))) (theta (1- m) y)) + (sum-theta (1- m) y)))) + +;; Signed version + +(defthmd sum-theta-lemma-signed + (implies (and (posp m) + (bvecp y (* 2 m))) + (equal (sum-theta m y) + (si y (* 2 m))))) + +(defund bmux4signed (zeta x n) + (case zeta + (1 x) + (-1 (bits (lognot x) (1- n) 0)) + (2 (bits (* 2 x) (1- n) 0)) + (-2 (bits (lognot (* 2 x)) (1- n) 0)) + (0 0))) + +(defthm bvecp-bmux4signed + (implies (and (integerp zeta) + (<= -2 zeta) + (<= zeta 2) + (integerp n) + (bvecp x n)) + (bvecp (bmux4signed zeta x n) + n)) + :hints (("Goal" :in-theory (enable bmux4signed bvecp))) + :rule-classes + ((:rewrite + :corollary + (implies (and (integerp zeta) + (<= -2 zeta) + (<= zeta 2) + (bvecp x n) + (integerp n)) + (bvecp (bmux4signed zeta x n) + n))) + (:type-prescription + :corollary + (implies (and (integerp zeta) + (<= -2 zeta) + (<= zeta 2) + (bvecp x n) + (integerp n)) + (natp (bmux4signed zeta x n)))) + (:linear + :corollary + (implies (and (integerp zeta) + (<= -2 zeta) + (<= zeta 2) + (bvecp x n) + (integerp n)) + (< (bmux4signed zeta x n) + (expt 2 n)))))) + +(defund tau (zeta sign) + (if (equal zeta 0) + 0 + (if (< 0 zeta) + sign + (lognot1 sign)))) + +(defun neg (x) (if (< x 0) 1 0)) + +(defund pp4signed-theta (i x y n) + (if (zerop i) + (cat 1 1 + (lognot1 (tau (theta i y) + (bitn x (1- n)))) + 1 + (bmux4signed (theta i y) x n) + n) + (cat 1 1 + (lognot1 (tau (theta i y) + (bitn x (1- n)))) + 1 + (bmux4signed (theta i y) x n) + n + 0 1 + (neg (theta (1- i) y)) + 1 + 0 (* 2 (1- i))))) + +(defun sum-pp4signed-theta (x y m n) + (if (zp m) + 0 + (+ (pp4signed-theta (1- m) x y n) + (sum-pp4signed-theta x y (1- m) n)))) + +(defthmd booth4signed-corollary + (implies (and (posp m) + (posp n) + (bvecp x n) + (bvecp y (* 2 m))) + (equal (sum-pp4signed-theta x y m n) + (+ (- (expt 2 n)) + (- (* (expt 2 (* 2 (1- m))) + (neg (theta (1- m) y)))) + (expt 2 (+ n (* 2 m))) + (* (si x n) (si y (* 2 m))))))) + +(defthmd booth4signed-corollary-alt + (implies (and (posp m) + (posp n) + (bvecp x n) + (bvecp y (* 2 m))) + (equal (+ (expt 2 n) + (* (expt 2 (* 2 (1- m))) + (neg (theta (1- m) y))) + (sum-pp4signed-theta x y m n)) + (+ (expt 2 (+ n (* 2 m))) + (* (si x n) (si y (* 2 m))))))) + +;; Unsigned versions + +(defthm sum-theta-lemma + (implies (and (not (zp m)) + (bvecp y (1- (* 2 m)))) + (equal y (sum-theta m y))) + :rule-classes ()) + +(defun bmux4 (zeta x n) + (case zeta + (1 x) + (-1 (bits (lognot x) (1- n) 0)) + (2 (* 2 x)) + (-2 (bits (lognot (* 2 x)) (1- n) 0)) + (0 0))) + +(defun pp4-theta (i x y n) + (if (zerop i) + (cat 1 1 + (bitn (lognot (neg (theta i y))) 0) 1 + (bmux4 (theta i y) x n) n) + (cat 1 1 + (bitn (lognot (neg (theta i y))) 0) 1 + (bmux4 (theta i y) x n) n + 0 1 + (neg (theta (1- i) y)) 1 + 0 (* 2 (1- i))))) + +(defun sum-pp4-theta (x y m n) + (if (zp m) + 0 + (+ (pp4-theta (1- m) x y n) + (sum-pp4-theta x y (1- m) n)))) + +(defthm booth4-corollary-1 + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m)))) + (= (+ (expt 2 n) + (sum-pp4-theta x y m n)) + (+ (expt 2 (+ n (* 2 m))) + (* x y)))) + :rule-classes ()) + +;;------------------------------------------------------------------------------------------------ + +(defun pp4p-theta (i x y n) + (if (zerop i) + (cat (bitn (lognot (neg (theta i y))) 0) 1 + (neg (theta i y)) 1 + (neg (theta i y)) 1 + (bmux4 (theta i y) x n) n) + (cat 1 1 + (bitn (lognot (neg (theta i y))) 0) 1 + (bmux4 (theta i y) x n) n + 0 1 + (neg (theta (1- i) y)) 1 + 0 (* 2 (1- i))))) + +(defun sum-pp4p-theta (x y m n) + (if (zp m) + 0 + (+ (pp4p-theta (1- m) x y n) + (sum-pp4p-theta x y (1- m) n)))) + +(defthm booth4-corollary-2 + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m)))) + (= (bits (sum-pp4p-theta x y m n) (1- (+ n (* 2 m))) 0) + (* x y))) + :rule-classes ()) + +;;------------------------------------------------------------------------------------------------ + +(defund mag (i y) + (if (member (bits y (1+ (* 2 i)) (1- (* 2 i))) '(3 4)) + 2 + (if (member (bits y (* 2 i) (1- (* 2 i))) '(1 2)) + 1 + 0))) + +(defund nbit (i y) + (bitn y (1+ (* 2 i)))) + +(defthmd theta-rewrite + (implies (and (natp y) (natp i)) + (equal (theta i y) + (if (= (nbit i y) 1) + (- (mag i y)) + (mag i y))))) + +(defund bmux4p (i x y n) + (if (= (nbit i y) 1) + (bits (lognot (* (mag i y) x)) (1- n) 0) + (* (mag i y) x))) + +(defthmd bvecp-bmux4p + (implies (and (not (zp n)) + (bvecp x (1- n))) + (bvecp (bmux4p i x y n) n))) + +(defthmd bmux4p-rewrite + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m))) + (natp i) + (< i m)) + (equal (bmux4p i x y n) + (+ (* (theta i y) x) + (* (1- (expt 2 n)) (nbit i y)))))) + +(defund pp4p (i x y n) + (if (zerop i) + (cat (if (= (nbit 0 y) 0) 1 0) 1 + (nbit 0 y) 1 + (nbit 0 y) 1 + (bmux4p 0 x y n) n) + (cat 1 1 + (lognot (nbit i y)) 1 + (bmux4p i x y n) n + 0 1 + (nbit (1- i) y) 1 + 0 (* 2 (1- i))))) + +(defthmd pp4p0-rewrite + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m)))) + (equal (pp4p 0 x y n) + (+ (expt 2 (+ n 2)) + (* (theta 0 y) x) + (- (nbit 0 y)))))) + +(defthmd pp4p-rewrite + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m))) + (not (zp i)) + (< i m)) + (equal (pp4p i x y n) + (+ (expt 2 (+ n (* 2 i) 1)) + (expt 2 (+ n (* 2 i))) + (* (expt 2 (* 2 i)) (theta i y) x) + (* (expt 2 (* 2 (1- i))) (nbit (1- i) y)) + (- (* (expt 2 (* 2 i)) (nbit i y))))))) + +(defun sum-pp4p (x y m n) + (if (zp m) + 0 + (+ (pp4p (1- m) x y n) + (sum-pp4p x y (1- m) n)))) + +(defthmd booth4-corollary-3 + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (bvecp y (1- (* 2 m)))) + (equal (sum-pp4p x y m n) + (+ (* x y) (expt 2 (+ n (* 2 m))))))) + +) +;;;********************************************************************** +;;; Statically Encoded Multiplier Arrays +;;;********************************************************************** + +(defsection-rtl |Statically Encoded Multiplier Arrays| |Multiplication| + +(defun m-mu-chi (i mode) + (cond ((equal mode 'mu) + (if (zp i) 1 + (cons (cons 1 i) 1))) + ((equal mode 'chi) + (if (zp i) 0 + (cons (cons 1 i) 0))))) + +(mutual-recursion + (defun mu (i y) + (declare (xargs :measure (m-mu-chi i 'mu))) + (+ (bits y (1+ (* 2 i)) (* 2 i)) (chi i y))) + + (defun chi (i y) + (declare (xargs :measure (m-mu-chi i 'chi))) + (if (zp i) + 0 + (if (>= (mu (1- i) y) 3) + 1 + 0)))) + +(defun phi (i y) + (if (= (bits (mu i y) 1 0) 3) + -1 + (bits (mu i y) 1 0))) + +(defthm phi-bnd + (member (phi i y) '(-1 0 1 2))) + +(defun sum-odd-powers-of-2 (m) + (if (zp m) + 0 + (+ (expt 2 (1- (* 2 m))) + (sum-odd-powers-of-2 (1- m))))) + +(defthm chi-m + (implies (and (natp m) + (natp y) + (<= y (sum-odd-powers-of-2 m))) + (equal (chi m y) 0)) + :rule-classes()) + +(defthm phi-m-1 + (implies (and (natp m) + (natp y) + (<= y (sum-odd-powers-of-2 m))) + (>= (phi (1- m) y) 0)) + :rule-classes()) + +(defun sum-phi (m y) + (if (zp m) + 0 + (+ (* (expt 2 (* 2 (1- m))) (phi (1- m) y)) + (sum-phi (1- m) y)))) + +(defthm sum-phi-lemma + (implies (and (natp m) + (natp y) + (<= y (sum-odd-powers-of-2 m))) + (equal y (sum-phi m y))) + :rule-classes ()) + +(defun pp4-phi (i x y n) + (if (zerop i) + (cat 1 1 + (bitn (lognot (neg (phi i y))) 0) 1 + (bmux4 (phi i y) x n) n) + (cat 1 1 + (bitn (lognot (neg (phi i y))) 0) 1 + (bmux4 (phi i y) x n) n + 0 1 + (neg (phi (1- i) y)) 1 + 0 (* 2 (1- i))))) + +(defun sum-pp4-phi (x y m n) + (if (zp m) + 0 + (+ (pp4-phi (1- m) x y n) + (sum-pp4-phi x y (1- m) n)))) + +(defthm static-booth + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (1- n)) + (natp y) + (<= y (sum-odd-powers-of-2 m))) + (= (+ (expt 2 n) + (sum-pp4-phi x y m n)) + (+ (expt 2 (+ n (* 2 m))) + (* x y)))) + :rule-classes ()) + +) +;;;********************************************************************** +;;; Encoding Redundant Representations +;;;********************************************************************** + +(defsection-rtl |Encoding Redundant Representations| |Multiplication| + +(defun gamma (i a b c) + (if (zp i) + (bitn c 0) + (logior (bitn a (+ -1 (* 2 i))) + (bitn b (+ -1 (* 2 i)))))) + +(defun delta (i a b c d) + (if (zp i) + (bitn d 0) + (logand (logior (logand (bitn a (+ -2 (* 2 i))) + (bitn b (+ -2 (* 2 i)))) + (logior (logand (bitn a (+ -2 (* 2 i))) + (gamma (1- i) a b c)) + (logand (bitn b (+ -2 (* 2 i))) + (gamma (1- i) a b c)))) + (lognot (logxor (bitn a (1- (* 2 i))) + (bitn b (1- (* 2 i)))))))) + +(defun psi (i a b c d) + (if (not (natp i)) + 0 + (+ (bits a (1+ (* 2 i)) (* 2 i)) + (bits b (1+ (* 2 i)) (* 2 i)) + (gamma i a b c) + (delta i a b c d) + (* -4 (+ (gamma (1+ i) a b c) + (delta (1+ i) a b c d)))))) + +(defthm psi-m-1 + (implies (and (natp m) + (>= m 1) + (bvecp a (- (* 2 m) 2)) + (bvecp b (- (* 2 m) 2)) + (bvecp c 1) + (bvecp d 1)) + (and (equal (gamma m a b c) 0) + (equal (delta m a b c d) 0) + (>= (psi (1- m) a b c d) 0))) + :rule-classes ()) + +(defun sum-psi (m a b c d) + (if (zp m) + 0 + (+ (* (expt 2 (* 2 (1- m))) (psi (1- m) a b c d)) + (sum-psi (1- m) a b c d)))) + +(defthm sum-psi-lemma + (implies (and (natp m) + (<= 1 M) ;; add + (bvecp a (- (* 2 m) 2)) + (bvecp b (- (* 2 m) 2)) + (bvecp c 1) + (bvecp d 1)) + (equal (+ a b c d) (sum-psi m a b c d))) + :rule-classes ()) + +(defthmd psi-bnd + (and (integerp (psi i a b c d)) + (<= (psi i a b c d) 2) + (>= (psi i a b c d) -2))) + +(defun pp4-psi (i x a b c d n) + (if (zerop i) + (cat 1 1 + (bitn (lognot (neg (psi i a b c d))) 0) 1 + (bmux4 (psi i a b c d) x n) n) + (cat 1 1 + (bitn (lognot (neg (psi i a b c d))) 0) 1 + (bmux4 (psi i a b c d) x n) n + 0 1 + (neg (psi (1- i) a b c d)) 1 + 0 (* 2 (1- i))))) + +(defun sum-pp4-psi (x a b c d m n) + (if (zp m) + 0 + (+ (pp4-psi (1- m) x a b c d n) + (sum-pp4-psi x a b c d (1- m) n)))) + +(defthm redundant-booth + (implies (and (natp m) + (<= 1 m) + (not (zp n)) + (bvecp x (1- n)) + (bvecp a (- (* 2 m) 2)) + (bvecp b (- (* 2 m) 2)) + (bvecp c 1) + (bvecp d 1) + (= y (+ a b c d))) + (= (+ (expt 2 n) + (sum-pp4-psi x a b c d m n)) + (+ (expt 2 (+ n (* 2 m))) + (* x y)))) + :rule-classes ()) +) + +;;;********************************************************************** +;;; Radix-8 Booth Encoding +;;;********************************************************************** + +(defsection-rtl |Radix-8 Booth Encoding| |Multiplication| + +(defun eta (i y) + (+ (bitn y (1- (* 3 i))) + (bitn y (* 3 i)) + (* 2 (bitn y (1+ (* 3 i)))) + (* -4 (bitn y (+ 2 (* 3 i)))))) + +(defun sum-eta (m y) + (if (zp m) + 0 + (+ (* (expt 2 (* 3 (1- m))) (eta (1- m) y)) + (sum-eta (1- m) y)))) + +(defthm sum-eta-lemma + (implies (and (not (zp m)) + (bvecp y (1- (* 3 m)))) + (equal y (sum-eta m y))) + :rule-classes ()) + +(defun bmux8 (zeta x n) + (case zeta + (1 x) + (-1 (bits (lognot x) (1- n) 0)) + (2 (* 2 x)) + (-2 (bits (lognot (* 2 x)) (1- n) 0)) + (3 (* 3 x)) + (-3 (bits (lognot (* 3 x)) (1- n) 0)) + (4 (* 4 x)) + (-4 (bits (lognot (* 4 x)) (1- n) 0)) + (0 0))) + +(encapsulate ((xi (i) t)) + (local (defun xi (i) (declare (ignore i)) 0)) + (defthm xi-bnd + (and (integerp (xi i)) + (<= (xi i) 4) + (>= (xi i) -4)))) + +(defun pp8 (i x n) + (if (zerop i) + (cat 3 2 + (bitn (lognot (neg (xi i))) 0) 1 + (bmux8 (xi i) x n) n) + (cat 3 2 + (bitn (lognot (neg (xi i))) 0) 1 + (bmux8 (xi i) x n) n + 0 2 + (neg (xi (1- i))) 1 + 0 (* 3 (1- i))))) + +(defun sum-xi (m) + (if (zp m) + 0 + (+ (* (expt 2 (* 3 (1- m))) (xi (1- m))) + (sum-xi (1- m))))) + +(defun sum-pp8 (x m n) + (if (zp m) + 0 + (+ (pp8 (1- m) x n) + (sum-pp8 x (1- m) n)))) + + +(defthm booth8-thm + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (- n 2))) + (= (+ (expt 2 n) + (sum-pp8 x m n)) + (+ (expt 2 (+ n (* 3 m))) + (* x (sum-xi m)) + (- (* (expt 2 (* 3 (1- m))) (neg (xi (1- m)))))))) + :rule-classes ()) + +(defun pp8-eta (i x y n) + (if (zerop i) + (cat 3 2 + (bitn (lognot (neg (eta i y))) 0) 1 + (bmux8 (eta i y) x n) n) + (cat 3 2 + (bitn (lognot (neg (eta i y))) 0) 1 + (bmux8 (eta i y) x n) n + 0 2 + (neg (eta (1- i) y)) 1 + 0 (* 3 (1- i))))) + +(defun sum-pp8-eta (x y m n) + (if (zp m) + 0 + (+ (pp8-eta (1- m) x y n) + (sum-pp8-eta x y (1- m) n)))) + +(defthm booth8-corollary + (implies (and (not (zp n)) + (not (zp m)) + (bvecp x (- n 2)) + (bvecp y (1- (* 3 m)))) + (= (+ (expt 2 n) + (sum-pp8-eta x y m n)) + (+ (expt 2 (+ n (* 3 m))) + (* x y)))) + :rule-classes ()) + +) diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/rac.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/rac.lisp new file mode 100644 index 000000000..07565ad83 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/rac.lisp @@ -0,0 +1,275 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; Copyright (C) 1995-2013 Advanced Mirco Devices, Inc. +; +; Contact: +; David Russinoff +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; +; Author: David M. Russinoff (david@russinoff.com) + +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") + +;;;********************************************************************** +;;; Bit Manipulation +;;;********************************************************************** + +;Allows things like (in-theory (disable cat)) to refer to binary-cat. +(add-macro-alias cat binary-cat) + +(defun setbits (x w i j y) + (declare (xargs :guard (and (natp x) + (integerp y) + (integerp i) + (integerp j) + (<= 0 j) + (<= j i) + (integerp w) + (< i w)))) + (mbe :logic (cat (bits x (1- w) (1+ i)) + (+ -1 w (- i)) + (cat (bits y (+ i (- j)) 0) + (+ 1 i (- j)) + (bits x (1- j) 0) + j) + (1+ i)) + :exec (cond ((int= j 0) + (cond ((int= (1+ i) w) + (bits y (+ i (- j)) 0)) + (t + (cat (bits x (1- w) (1+ i)) + (+ -1 w (- i)) + (bits y (+ i (- j)) 0) + (1+ i))))) + ((int= (1+ i) w) + (cat (bits y (+ i (- j)) 0) + (+ 1 i (- j)) + (bits x (1- j) 0) + j)) + (t + (cat (bits x (1- w) (1+ i)) + (+ -1 w (- i)) + (cat (bits y (+ i (- j)) 0) + (+ 1 i (- j)) + (bits x (1- j) 0) + j) + (1+ i)))))) + +(defun setbitn (x w n y) + (declare (xargs :guard (and (natp x) + (integerp y) + (integerp n) + (<= 0 n) + (integerp w) + (< n w)))) + (setbits x w n n y)) + +;; These help in dealing with calls to ash and rem generated by the translator: + +(defthm ash-rewrite + (equal (ash i c) + (fl (* (ifix i) (expt 2 c)))) + :hints (("Goal" :in-theory (enable floor fl ash)))) + +(defthm rem-mod + (implies (and (natp m) (posp n)) + (equal (rem m n) (mod m n)))) + +;;;********************************************************************** +;;; Boolean Functions +;;;********************************************************************** + +(defun log= (x y) + (declare (xargs :guard t)) + (if (equal x y) 1 0)) + +(defun log<> (x y) + (declare (xargs :guard t)) + (if (equal x y) 0 1)) + +(defun log< (x y) + (declare (xargs :guard (and (rationalp x) (rationalp y)))) + (if (< x y) 1 0)) + +(defun log<= (x y) + (declare (xargs :guard (and (rationalp x) (rationalp y)))) + (if (<= x y) 1 0)) + +(defun log> (x y) + (declare (xargs :guard (and (rationalp x) (rationalp y)))) + (if (> x y) 1 0)) + +(defun log>= (x y) + (declare (xargs :guard (and (rationalp x) (rationalp y)))) + (if (>= x y) 1 0)) + +(defun logior1 (x y) + (if (and (equal x 0) (equal y 0)) 0 1)) + +(defun logand1 (x y) + (if (or (equal x 0) (equal y 0)) 0 1)) + +(defun lognot1 (x) + (if (equal x 0) 1 0)) + +(defun true$ () 1) + +(defun false$ () 0) + +(defmacro if1 (x y z) `(if (eql ,x 0) ,z ,y)) + +;;;********************************************************************** +;;; Miscellaneous Macros +;;;********************************************************************** + +(defmacro in-function (fn term) + `(if1 ,term () (er hard ',fn "Assertion ~x0 failed" ',term))) + +;;;********************************************************************** +;;; Arrays +;;;********************************************************************** + +(include-book "misc/total-order" :dir :system) + +(defmacro default-get-valu () 0) + +(defun arcdp (x) + (declare (xargs :guard t)) + (or (null x) + (and (consp x) + (consp (car x)) + (arcdp (cdr x)) + (not (equal (cdar x) + (default-get-valu))) + (or (null (cdr x)) + (acl2::<< (caar x) (caadr x)))))) + +(defthm arcdp-implies-alistp + (implies (arcdp x) (alistp x))) + +(defmacro aifrp-tag () + ''unlikely-to-ever-occur-in-an-executable-counterpart) + +(defun aifrp (x) ;; ill-formed rcdp + (declare (xargs :guard t)) + (or (not (arcdp x)) + (and (consp x) + (null (cdr x)) + (consp (car x)) + (equal (cdar x) (aifrp-tag)) + (aifrp (caar x))))) + +(defun acl2->arcd (x) ;; function mapping acl2 objects to well-formed records. + (declare (xargs :guard t)) + (if (aifrp x) (list (cons x (aifrp-tag))) x)) + +(defun arcd->acl2 (r) ;; inverse of acl2->rcd. + (declare (xargs :guard (arcdp r))) + (if (aifrp r) (caar r) r)) + +(defun ag-aux (a r) ;; record g(et) when r is a well-formed record. + (declare (xargs :guard (arcdp r))) + (cond ((or (endp r) + (acl2::<< a (caar r))) + (default-get-valu)) + ((equal a (caar r)) + (cdar r)) + (t + (ag-aux a (cdr r))))) + +(defun ag (a x) ;; the generic record g(et) which works on any ACL2 object. + (declare (xargs :guard t)) + (ag-aux a (acl2->arcd x))) + +(defun acons-if (a v r) + (declare (xargs :guard (arcdp r))) + (if (equal v (default-get-valu)) r (acons a v r))) + +(defun as-aux (a v r) ;; record s(et) when x is a well-formed record. + (declare (xargs :guard (arcdp r))) + (cond ((or (endp r) + (acl2::<< a (caar r))) + (acons-if a v r)) + ((equal a (caar r)) + (acons-if a v (cdr r))) + (t + (cons (car r) (as-aux a v (cdr r)))))) + +(defun as (a v x) ;; the generic record s(et) which works on any ACL2 object. + (declare (xargs :guard t)) + (arcd->acl2 (as-aux a v (acl2->arcd x)))) + +(defun ainit (l) + (declare (xargs :guard t)) + (if (consp l) + (if (consp (car l)) + (as (caar l) (cdar l) (ainit (cdr l))) + (ainit (cdr l))) + nil)) + +;;Basic properties of arrays: + +(defthm ag-same-as + (equal (ag a (as a v r)) + v)) + +(defthm ag-diff-as + (implies (not (equal a b)) + (equal (ag a (as b v r)) + (ag a r)))) + +;;;; NOTE: The following can be used instead of the above rules to force ACL2 +;;;; to do a case-split. We disable this rule by default since it can lead to +;;;; an expensive case explosion, but in many cases, this rule may be more +;;;; effective than two rules above and should be enabled. + +(defthm ag-of-as-redux + (equal (ag a (as b v r)) + (if (equal a b) v (ag a r)))) + +(in-theory (disable ag-of-as-redux)) + +(defthm as-same-ag + (equal (as a (ag a r) r) + r)) + +(defthm as-same-as + (equal (as a y (as a x r)) + (as a y r))) + +(defthm as-diff-as + (implies (not (equal a b)) + (equal (as b y (as a x r)) + (as a x (as b y r)))) + :rule-classes ((:rewrite :loop-stopper ((b a as))))) + +;; the following theorems are less relevant but have been useful in dealing +;; with a default record of NIL. + +(defthm ag-of-nil-is-default + (equal (ag a nil) (default-get-valu))) + +(defthm as-non-default-cannot-be-nil + (implies (not (equal v (default-get-valu))) + (as a v r))) + +(defthm non-nil-if-ag-not-default + (implies (not (equal (ag a r) + (default-get-valu))) + r) + :rule-classes :forward-chaining) + +;;We disable as and ag, assuming the rules proved in this book are +;;sufficient to manipulate any record terms that are encountered. + +(in-theory (disable as ag)) diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/rcp.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/rcp.lisp new file mode 100644 index 000000000..e64c18874 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/rcp.lisp @@ -0,0 +1,61 @@ +(in-package "RTL") + +(INCLUDE-BOOK "rac") + +(defun rcp0 () '(134217725 133177280 132152840 131144040 130150521 + 129171946 128207980 127258288 126322566 125400505 124491806 + 123596178 122713348 121843044 120984994 120138944 119304646 + 118481854 117670336 116869856 116080194 115301132 114532460 + 113773968 113025454 112286728 111557592 110837866 110127368 + 109425916 108733350 108049488 107374182 106707262 106048574 + 105397968 104755298 104120420 103493188 102873470 102261126 + 101656030 101058054 100467072 99882960 99305602 98734878 98170678 + 97612894 97061406 96516116 95976924 95443718 94916402 94394884 + 93879066 93368852 92864160 92364888 91870956 91382282 90898780 + 90420364 89946956 89478486 89014868 88556026 88101890 87652394 + 87207458 86767016 86331002 85899346 85471986 85048856 84629897 + 84215046 83804240 83397422 82994534 82595524 82200330 81808902 + 81421180 81037118 80656663 80279762 79906366 79536432 79169904 + 78806738 78446892 78090313 77736964 77386800 77039774 76695844 + 76354973 76017120 75682240 75350304 75021262 74695082 74371728 + 74051160 73733342 73418242 73105826 72796056 72488900 72184324 + 71882297 71582788 71285764 70991194 70699052 70409300 70121914 + 69836868 69554126 69273666 68995460 68719478 68445694 68174084 + 67904624 67637280 67372036)) + +(defun rcp1 () '(131067 129044 127066 125134 123244 121398 119594 117828 + 116102 114414 112762 111144 109562 108014 106498 105014 103560 + 102136 100742 99376 98038 96726 95442 94182 92946 91736 90548 89384 + 88242 87120 86022 84942 83884 82846 81826 80824 79842 78878 77930 + 77000 76086 75188 74306 73440 72588 71752 70928 70120 69326 68544 + 67776 67022 66280 65548 64830 64124 63428 62746 62072 61410 60758 + 60118 59486 58864 58254 57652 57058 56474 55900 55334 54776 54228 + 53686 53154 52628 52111 51602 51100 50604 50116 49636 49162 48696 + 48234 47780 47333 46892 46456 46028 45604 45186 44776 44368 43968 + 43574 43184 42798 42419 42044 41674 41310 40950 40594 40244 39898 + 39556 39218 38886 38557 38232 37912 37595 37282 36974 36668 36368 + 36070 35776 35486 35200 34916 34636 34360 34086 33816 33550 33286 + 33026)) + +(defun rcp2 () '(4047 3956 3864 3780 3690 3608 3532 3452 3378 3307 3236 + 3164 3096 3032 2968 2908 2844 2784 2728 2676 2624 2568 2524 2472 + 2420 2376 2328 2288 2244 2196 2160 2116 2076 2040 2004 1960 1928 + 1896 1860 1828 1796 1764 1732 1704 1672 1648 1612 1588 1560 1532 + 1508 1484 1464 1432 1412 1392 1364 1348 1324 1304 1280 1264 1240 + 1220 1208 1188 1164 1148 1132 1116 1096 1084 1064 1052 1032 1018 + 1004 992 972 960 948 932 924 904 892 882 872 856 848 832 820 816 798 + 788 784 772 756 750 736 728 720 712 700 692 684 676 664 660 650 640 + 636 626 616 612 600 596 588 580 572 572 560 552 548 540 532 528 524 + 520)) + +(defun rcp24 (b) + (let* ((i (fl (* (expt 2 7) (1- b)))) + (x (- b (1+ (* (expt 2 -7) i)))) + (c0 (* (expt 2 -27) (nth i (rcp0)))) + (c1 (- (* (expt 2 -17) (nth i (rcp1))))) + (c2 (* (expt 2 -12) (nth i (rcp2))))) + (rne (+ c0 (* c1 x) (* c2 x x)) 24))) + + + + diff --git a/crates/clazel-x64/tests/data/acl2-rtl/lib/reps.lisp b/crates/clazel-x64/tests/data/acl2-rtl/lib/reps.lisp new file mode 100644 index 000000000..e87a466df --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-rtl/lib/reps.lisp @@ -0,0 +1,506 @@ +; RTL - A Formal Theory of Register-Transfer Logic and Computer Arithmetic +; +; Contact: +; David M. Russinoff +; david@russinoff.com +; http://www.russinoff.com/ +; +; See license file books/rtl/rel11/license.txt. +; + +(in-package "RTL") + +(set-enforce-redundancy t) + +(local (include-book "../support/top")) + +(set-inhibit-warnings "theory") ; avoid warning in the next event +(local (in-theory nil)) + +(include-book "defs") + +;;;*************************************************************** +;;; Classification of Formats +;;;*************************************************************** + +(defsection-rtl |Classification of Formats| |Floating-Point Formats| + +;;Format parameters: + +(defnd formatp (f) + (and (consp f) + (consp (cdr f)) + (consp (cddr f)) + (natp (cadr f)) + (> (cadr f) 1) + (natp (caddr f)) + (> (caddr f) 1))) + +(defund explicitp (f) (declare (xargs :guard (formatp f))) (car f)) + +(defund prec (f) (declare (xargs :guard (formatp f))) (cadr f)) + +(defund expw (f) (declare (xargs :guard (formatp f))) (caddr f)) + +(defund sigw (f) + (declare (xargs :guard (formatp f))) + (if (explicitp f) + (prec f) + (1- (prec f)))) + +(defnd encodingp (x f) + (and (formatp f) (bvecp x (+ 1 (expw f) (sigw f))))) + +;;Examples: + +(defnd hp () '(nil 11 5)) + +(defnd sp () '(nil 24 8)) + +(defnd dp () '(nil 53 11)) + +(defnd bf () '(nil 8 8)) + +(defnd ep () '(t 64 15)) + +(in-theory (disable (sp) (dp) (hp) (bf) (ep))) + +(defthm formatp-sp + (formatp (sp))) + +(defthm formatp-dp + (formatp (dp))) + +(defthm formatp-hp + (formatp (hp))) + +(defthm formatp-ep + (formatp (ep))) + +;;Field extractors: + +(defund sgnf (x f) + (declare (xargs :guard (encodingp x f))) + (bitn x (+ (expw f) (sigw f)))) + +(defund expf (x f) + (declare (xargs :guard (encodingp x f))) + (bits x (1- (+ (expw f) (sigw f))) (sigw f))) + +(defund sigf (x f) + (declare (xargs :guard (encodingp x f))) + (bits x (1- (sigw f)) 0)) + +(defund manf (x f) + (declare (xargs :guard (encodingp x f))) + (bits x (- (prec f) 2) 0)) + +;;Exponent bias: + +(defund bias (f) (declare (xargs :guard (formatp f))) (- (expt 2 (- (expw f) 1)) 1)) + +) +;;;*************************************************************** +;;; Normal Encodings +;;;*************************************************************** + +(defsection-rtl |Normal Encodings| |Floating-Point Formats| + +(defund normp (x f) + (declare (xargs :guard (encodingp x f))) + (and (mbt (encodingp x f)) + (< 0 (expf x f)) + (< (expf x f) (1- (expt 2 (expw f)))) + (implies (explicitp f) (= (bitn x (1- (prec f))) 1)))) + +(defund unsupp (x f) + (declare (xargs :guard (encodingp x f))) + (and (mbt (encodingp x f)) + (explicitp f) + (< 0 (expf x f)) + (= (bitn x (1- (prec f))) 0))) + +;;Decoding function: + +(defund ndecode (x f) + (declare (xargs :guard (encodingp x f))) + (* (if (= (sgnf x f) 0) 1 -1) + (expt 2 (- (expf x f) (bias f))) + (1+ (* (manf x f) (expt 2 (- 1 (prec f))))))) + +(defthmd sgn-ndecode + (implies (normp x f) + (equal (sgn (ndecode x f)) + (if (= (sgnf x f) 0) 1 -1)))) + +(defthmd expo-ndecode + (implies (normp x f) + (equal (expo (ndecode x f)) + (- (expf x f) (bias f))))) + +(defthmd sig-ndecode + (implies (normp x f) + (equal (sig (ndecode x f)) + (+ 1 (/ (manf x f) (expt 2 (1- (prec f)))))))) + + +;;Representable normals: + +(defnd nrepp (x f) + (and (rationalp x) + (formatp f) + (not (= x 0)) + (< 0 (+ (expo x) (bias f))) + (< (+ (expo x) (bias f)) (1- (expt 2 (expw f)))) + (exactp x (prec f)))) + +;;Encoding function: + +(defund nencode (x f) + (declare (xargs :guard (and (rationalp x) + (formatp f) + (exactp x (prec f))))) + (cat (if (= (sgn x) 1) 0 1) + 1 + (+ (expo x) (bias f)) + (expw f) + (* (sig x) (expt 2 (1- (prec f)))) + (sigw f))) + +;;Inversions: + +(defthm nrepp-ndecode + (implies (normp x f) + (nrepp (ndecode x f) f))) + +(defthm nencode-ndecode + (implies (normp x f) + (equal (nencode (ndecode x f) f) + x))) + +(defthm normp-nencode + (implies (nrepp x f) + (normp (nencode x f) f))) + +(defthm ndecode-nencode + (implies (nrepp x f) + (equal (ndecode (nencode x f) f) + x))) + +;; Smallest positive normal: + +(defund spn (f) + (declare (xargs :guard (formatp f))) + (expt 2 (- 1 (bias f)))) + +(defthmd positive-spn + (> (spn f) 0) + :rule-classes ( :linear)) + +(defthmd expo-spn + (implies (formatp f) + (equal (expo (spn f)) + (- 1 (bias f))))) + +(defthmd nrepp-spn + (implies (formatp f) + (nrepp (spn f) f))) + +(defthmd smallest-spn + (implies (nrepp x f) + (>= (abs x) (spn f))) + :rule-classes + ((:rewrite :match-free :once))) + +;; Largest positive normal: + +(defund lpn (f) + (declare (xargs :guard (formatp f))) + (* (expt 2 (- (expt 2 (expw f)) (+ 2 (bias f)))) + (- 2 (expt 2 (- 1 (prec f)))))) + +(defthmd positive-lpn + (implies (formatp f) + (> (lpn f) 0)) + :rule-classes ( :linear)) + +(defthmd expo-lpn + (implies (formatp f) + (equal (expo (lpn f)) + (- (expt 2 (expw f)) (+ 2 (bias f)))))) + +(defthmd sig-lpn + (implies (formatp f) + (equal (sig (lpn f)) + (- 2 (expt 2 (- 1 (prec f))))))) + +(defthmd nrepp-lpn + (implies (formatp f) + (nrepp (lpn f) f))) + +(defthmd largest-lpn + (implies (nrepp x f) + (<= x (lpn f))) + :rule-classes + ((:rewrite :match-free :once))) +) + +;;;*************************************************************** +;;; Denormals and Zeroes +;;;*************************************************************** + +(defsection-rtl |Denormals and Zeroes| |Floating-Point Formats| + +(defund zerp (x f) + (declare (xargs :guard (encodingp x f))) + (and (mbt (encodingp x f)) + (= (expf x f) 0) + (= (sigf x f) 0))) + +(defund zencode (sgn f) + (declare (xargs :guard (and (bvecp sgn 1) + (formatp f)))) + (cat sgn 1 0 (+ (sigw f) (expw f)))) + +(defund denormp (x f) + (declare (xargs :guard (encodingp x f))) + (and (mbt (encodingp x f)) + (= (expf x f) 0) + (not (= (sigf x f) 0)) + (implies (explicitp f) (= (bitn x (1- (prec f))) 0)))) + +(defund pseudop (x f) + (declare (xargs :guard (encodingp x f))) + (and (mbt (encodingp x f)) + (explicitp f) + (= (expf x f) 0) + (= (bitn x (1- (prec f))) 1))) + +(defund ddecode (x f) + (declare (xargs :guard (encodingp x f))) + (* (if (= (sgnf x f) 0) 1 -1) + (sigf x f) + (expt 2 (+ 2 (- (bias f)) (- (prec f)))))) + +(defund decode (x f) + (declare (xargs :guard (encodingp x f))) + (if (= (expf x f) 0) + (ddecode x f) + (ndecode x f))) + +(defthm sgn-ddecode + (implies (or (denormp x f) (pseudop x f)) + (equal (sgn (ddecode x f)) + (if (= (sgnf x f) 0) 1 -1)))) + +(defthm expo-ddecode + (implies (or (denormp x f) (pseudop x f)) + (equal (expo (ddecode x f)) + (+ 2 (- (prec f)) (- (bias f)) (expo (sigf x f)))))) + +(defthm sig-ddecode + (implies (or (denormp x f) (pseudop x f)) + (equal (sig (ddecode x f)) + (sig (sigf x f))))) + +(defnd drepp (x f) + (and (rationalp x) + (formatp f) + (not (= x 0)) + (<= (- 2 (prec f)) (+ (expo x) (bias f))) + (<= (+ (expo x) (bias f)) 0) + (exactp x (+ (1- (prec f)) (bias f) (expo x))))) + +(defthmd drepp-exactp + (implies (drepp x f) + (exactp x (prec f)))) + +(defund dencode (x f) + (declare (xargs :guard (drepp x f))) + (cat (if (= (sgn x) 1) 0 1) + 1 + 0 + (expw f) + (* (sig x) (expt 2 (+ -2 (prec f) (expo x) (bias f)))) + (sigw f))) + +(defthm drepp-ddecode + (implies (denormp x f) + (drepp (ddecode x f) f))) + +(defthm dencode-ddecode + (implies (denormp x f) + (equal (dencode (ddecode x f) f) + x))) + +(defthm denormp-dencode + (implies (drepp x f) + (denormp (dencode x f) f))) + +(defthm ddecode-dencode + (implies (drepp x f) + (equal (ddecode (dencode x f) f) + x))) + +(defthmd dreppThe x86 model can be used in the application-level view + when the value of the field @('app-view') is T.
+ +Some supervisor-level features are neither used nor required for +the analysis of application software. In most cases, a user +cares about the correctness of his application program while assuming +that services like paging and I/O operations are provided reliably by +the operating system. The application-level view of our model attempts +to provide the same environment to an application for reasoning as is +provided by an OS for programming. In this mode, our memory model +provides the 2^48-byte linear address space specified for +modern 64-bit Intel machines.
+ +A 64-bit canonical address can be in either of the two +ranges below:
+ + 0 to 2^47-1 or 2^64-2^47 to 2^64-1 + +Note that this address space is 2^48 bytes of memory. So I can use +the addresses
+0 to 2^47-1 +to map the first range, and the addresses
+-2^47 to -1 +to map the second range.
+ +0 to 2^47-1 | 0 to 2^47-1The advantage of doing so is that we will avoid bignum creation +during address computations.
+ +We define some linear memory read and write functions, like @(see +rvm08) and @(see wvm08). These functions are called by the top-level +functions, like @(see rml08) and @(see wml08) when +@('app-view') is true.
+ +The guards of the linear memory functions are stricter than those +of @('memi') and @('!memi'), because the latter allow reading from and +writing to an address anywhere in the range 0 to 2^52-1, and +@('rvm08') and @('wvm08') allow reads and writes only to the range +0 to 2^48-1 of physical memory: they take as argument a signed 48-bit +integer that represents a canonical address, they convert it to an unsigned +48-bit integer, and they use that to access the @('mem') field. +Basically, we're overloading the @('mem') field +in @('x86') --- when @('app-view') is set, @('mem') +refers to the linear memory; otherwise, it refers to the physical +memory.
" ) + +(defsection linear-memory-in-app-view + + :parents (app-view) + + :short "Definition of linear memory accessor and updater functions, + used when @('app-view') = @('T')" + + :long "Note that the top-level memory accessor function is @(see + rml08) and updater function is @(see wml08). Their 16, 32, and 64 bit + counterparts are also available. These functions behave differently + depending upon the value of @('app-view').
" + + ) + +;; ---------------------------------------------------------------------- + +(define canonical-address-p (lin-addr) + :prepwork ((in-theory (e/d (signed-byte-p) ()))) + :parents (linear-memory-in-app-view) + :inline t + :no-function t + :enabled t + :short "Recognizer of a canonical address" + :long "In 64-bit mode, a linear address is considered to be in + canonical form if address bits 63 through to the most-significant implemented + bit by the microarchitecture (represented by the constant + @('*max-linear-address-size*') in these books) are set to either all ones or + all zeros.
" + (mbe :logic (signed-byte-p #.*max-linear-address-size* lin-addr) + :exec (and (integerp lin-addr) + (<= #.*-2^47* lin-addr) + (< lin-addr #.*2^47*))) + + /// + + (defthm canonical-address-p-and-logext-48 + (implies (canonical-address-p a) + (equal (logext 48 a) a))) + + (defthm canonical-address-p-of-logext-48 + (canonical-address-p (logext 48 a)))) + +;; ---------------------------------------------------------------------- + +;; Memory Read Functions: + + +(define rvm08 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (mbe :logic (if (canonical-address-p addr) + (mv nil (memi (n48 addr) x86) x86) + (mv 'rvm08 0 x86)) + :exec (mv nil (memi (n48 addr) x86) x86)) + + /// + + (defthm rvm08-no-error + (implies (canonical-address-p addr) + (equal (mv-nth 0 (rvm08 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (local (in-theory (e/d () (unsigned-byte-p)))) + + (defthm-unsigned-byte-p n08p-mv-nth-1-rvm08 + :hyp t + :bound 8 + :concl (mv-nth 1 (rvm08 addr x86)) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm08-unchanged + (equal (mv-nth 2 (rvm08 addr x86)) + x86)) + + (defthm xr-rvm08 + (equal (xr fld index (mv-nth 2 (rvm08 addr x86))) + (xr fld index x86))) + + (defthm rvm08-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm08 addr (xw fld index value x86))) + (mv-nth 0 (rvm08 addr x86))) + (equal (mv-nth 1 (rvm08 addr (xw fld index value x86))) + (mv-nth 1 (rvm08 addr x86))))))) + +(local (in-theory (e/d (bitops::unsigned-byte-p-by-find-rule) ()))) + +(define rvm16 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((1+addr (the (signed-byte #.*max-linear-address-size+1*) + (1+ (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p (1+ addr)) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 1+addr) #.*2^47*)) + + (b* (((the (unsigned-byte 8) byte0) (memi (n48 addr) x86)) + ((the (unsigned-byte 8) byte1) (memi (n48 1+addr) x86))) + (mv nil + (the (unsigned-byte 16) + (logior (the (unsigned-byte 16) (ash byte1 8)) + byte0)) + x86)) + + (mv 'rvm16 0 x86))) + + (mv 'rvm16 0 x86)) + + /// + + (defthm rvm16-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 1 addr))) + (equal (mv-nth 0 (rvm16 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + + (defthm-unsigned-byte-p n16p-mv-nth-1-rvm16 + :hyp t + :bound 16 + :concl (mv-nth 1 (rvm16 addr x86)) + :gen-linear t + :hints (("Goal" :in-theory (e/d* () (unsigned-byte-p)))) + :gen-type t) + + (defthm x86p-mv-nth-2-rvm16-unchanged + (equal (mv-nth 2 (rvm16 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm xr-rvm16 + (equal (xr fld index (mv-nth 2 (rvm16 addr x86))) + (xr fld index x86))) + + (defthm rvm16-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm16 addr (xw fld index value x86))) + (mv-nth 0 (rvm16 addr x86))) + (equal (mv-nth 1 (rvm16 addr (xw fld index value x86))) + (mv-nth 1 (rvm16 addr x86))))))) + +(define rvm32 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((1+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 1 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (3+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 3 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p (+ 3 addr)) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 3+addr) + #.*2^47*)) + + (b* (((the (unsigned-byte 8) byte0) (memi (n48 addr) x86)) + ((the (unsigned-byte 8) byte1) (memi (n48 1+addr) x86)) + ((the (unsigned-byte 8) byte2) (memi (n48 2+addr) x86)) + ((the (unsigned-byte 8) byte3) (memi (n48 3+addr) x86)) + ((the (unsigned-byte 16) word1) + (logior byte2 (the (unsigned-byte 16) (ash byte3 8)))) + ((the (unsigned-byte 24) high-24) + (logior byte1 (the (unsigned-byte 24) (ash word1 8)))) + ((the (unsigned-byte 32) dword) + (logior byte0 (the (unsigned-byte 32) (ash high-24 8))))) + (mv nil dword x86)) + + (mv 'rvm32 0 x86))) + + (mv 'rvm32 0 x86)) + + /// + + (defthm rvm32-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 3 addr))) + (equal (mv-nth 0 (rvm32 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm-unsigned-byte-p n32p-mv-nth-1-rvm32 + :hyp t + :bound 32 + :concl (mv-nth 1 (rvm32 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force))))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm32-unchanged + (equal (mv-nth 2 (rvm32 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm xr-rvm32 + (equal (xr fld index (mv-nth 2 (rvm32 addr x86))) + (xr fld index x86))) + + (defthm rvm32-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm32 addr (xw fld index value x86))) + (mv-nth 0 (rvm32 addr x86))) + (equal (mv-nth 1 (rvm32 addr (xw fld index value x86))) + (mv-nth 1 (rvm32 addr x86))))))) + +(define rvm48 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (5+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 5 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 5+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 5+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 16) word0) x86) + (rvm16 addr x86)) + ((mv flg1 (the (unsigned-byte 32) dword1) x86) + (rvm32 2+addr x86)) + ((the (unsigned-byte 48) value) + (the (unsigned-byte 48) + (logior (the (unsigned-byte 48) (ash dword1 16)) + word0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm48 0 x86) + (mv nil value x86)) + :exec (mv nil value x86))) + + (mv 'rvm48 0 x86))) + + (mv 'rvm48 0 x86)) + + /// + + (local (in-theory (e/d () (rvm16 rvm32)))) + + (defthm rvm48-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 5 addr))) + (equal (mv-nth 0 (rvm48 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm-unsigned-byte-p n48p-mv-nth-1-rvm48 + :hyp t + :bound 48 + :concl (mv-nth 1 (rvm48 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force))))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm48-unchanged + (equal (mv-nth 2 (rvm48 addr x86)) x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm xr-rvm48 + (equal (xr fld index (mv-nth 2 (rvm48 addr x86))) + (xr fld index x86))) + + (defthm rvm48-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm48 addr (xw fld index value x86))) + (mv-nth 0 (rvm48 addr x86))) + (equal (mv-nth 1 (rvm48 addr (xw fld index value x86))) + (mv-nth 1 (rvm48 addr x86))))))) + +(define rvm64 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d () (rvm32)))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((4+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 4 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (7+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 7 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 7+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 7+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 32) dword0) x86) + (rvm32 addr x86)) + ((mv flg1 (the (unsigned-byte 32) dword1) x86) + (rvm32 4+addr x86)) + ((the (unsigned-byte 64) qword) + (the (unsigned-byte 64) (logior + (the (unsigned-byte 64) + (ash dword1 32)) + dword0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm64 0 x86) + (mv nil qword x86)) + :exec (mv nil qword x86))) + + (mv 'rvm64 0 x86))) + + (mv 'rvm64 0 x86)) + + /// + + (local (in-theory (e/d () (rvm32)))) + + (defthm rvm64-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 7 addr))) + (equal (mv-nth 0 (rvm64 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + + (defthm-unsigned-byte-p n64p-mv-nth-1-rvm64 + :hyp t + :bound 64 + :concl (mv-nth 1 (rvm64 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force))))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm64-unchanged + (equal (mv-nth 2 (rvm64 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm xr-rvm64 + (equal (xr fld index (mv-nth 2 (rvm64 addr x86))) + (xr fld index x86))) + + (defthm rvm64-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm64 addr (xw fld index value x86))) + (mv-nth 0 (rvm64 addr x86))) + (equal (mv-nth 1 (rvm64 addr (xw fld index value x86))) + (mv-nth 1 (rvm64 addr x86))))))) + +(define rvm80 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d () (rvm32)))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (9+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 9 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 9+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 9+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 16) word0) x86) + (rvm16 addr x86)) + ((mv flg1 (the (unsigned-byte 64) qword1) x86) + (rvm64 2+addr x86)) + ((the (unsigned-byte 80) value) + (the (unsigned-byte 80) + (logior (the (unsigned-byte 80) (ash qword1 16)) + word0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm80 0 x86) + (mv nil value x86)) + :exec (mv nil value x86))) + + (mv 'rvm80 0 x86))) + + (mv 'rvm80 0 x86)) + + /// + + (local (in-theory (e/d () (rvm16 rvm64)))) + + (defthm rvm80-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 9 addr))) + (equal (mv-nth 0 (rvm80 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm-unsigned-byte-p n80p-mv-nth-1-rvm80 + :hyp t + :bound 80 + :concl (mv-nth 1 (rvm80 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force))))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm80-unchanged + (equal (mv-nth 2 (rvm80 addr x86)) x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm xr-rvm80 + (equal (xr fld index (mv-nth 2 (rvm80 addr x86))) + (xr fld index x86))) + + (defthm rvm80-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm80 addr (xw fld index value x86))) + (mv-nth 0 (rvm80 addr x86))) + (equal (mv-nth 1 (rvm80 addr (xw fld index value x86))) + (mv-nth 1 (rvm80 addr x86))))))) + +(define rvm128 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d () (rvm64)))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((8+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 8 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (15+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 15 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 15+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 15+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 64) qword0) x86) + (rvm64 addr x86)) + ((mv flg1 (the (unsigned-byte 64) qword1) x86) + (rvm64 8+addr x86)) + ((the (unsigned-byte 128) oword) + (the (unsigned-byte 128) (logior + (the (unsigned-byte 128) + (ash qword1 64)) + qword0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm128 0 x86) + (mv nil oword x86)) + :exec (mv nil oword x86))) + + (mv 'rvm128 0 x86))) + + (mv 'rvm128 0 x86)) + + /// + + (local (in-theory (e/d () (rvm64)))) + + (defthm rvm128-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 15 addr))) + (equal (mv-nth 0 (rvm128 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force) rvm64))))) + + (defthm-unsigned-byte-p n128p-mv-nth-1-rvm128 + :hyp t + :bound 128 + :concl (mv-nth 1 (rvm128 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force) rvm64)))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm128-unchanged + (equal (mv-nth 2 (rvm128 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force) rvm64))))) + + (defthm xr-rvm128 + (equal (xr fld index (mv-nth 2 (rvm128 addr x86))) + (xr fld index x86))) + + (defthm rvm128-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm128 addr (xw fld index value x86))) + (mv-nth 0 (rvm128 addr x86))) + (equal (mv-nth 1 (rvm128 addr (xw fld index value x86))) + (mv-nth 1 (rvm128 addr x86))))))) + +(define rvm256 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d () (rvm128)))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((16+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 16 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (31+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 31 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 31+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 31+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 128) oword0) x86) + (rvm128 addr x86)) + ((mv flg1 (the (unsigned-byte 128) oword1) x86) + (rvm128 16+addr x86)) + ((the (unsigned-byte 256) xword) + (the (unsigned-byte 256) (logior + (the (unsigned-byte 256) + (ash oword1 128)) + oword0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm256 0 x86) + (mv nil xword x86)) + :exec (mv nil xword x86))) + + (mv 'rvm256 0 x86))) + + (mv 'rvm256 0 x86)) + + /// + + (local (in-theory (e/d () (rvm128)))) + + (defthm rvm256-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 31 addr))) + (equal (mv-nth 0 (rvm256 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force) rvm128))))) + + (defthm-unsigned-byte-p n256p-mv-nth-1-rvm256 + :hyp t + :bound 256 + :concl (mv-nth 1 (rvm256 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force) rvm128)))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm256-unchanged + (equal (mv-nth 2 (rvm256 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force) rvm128))))) + + (defthm xr-rvm256 + (equal (xr fld index (mv-nth 2 (rvm256 addr x86))) + (xr fld index x86))) + + (defthm rvm256-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm256 addr (xw fld index value x86))) + (mv-nth 0 (rvm256 addr x86))) + (equal (mv-nth 1 (rvm256 addr (xw fld index value x86))) + (mv-nth 1 (rvm256 addr x86))))))) + +(define rvm512 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d () (rvm256)))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((32+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 32 (the (signed-byte #.*max-linear-address-size*) + addr)))) + (63+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 63 (the (signed-byte #.*max-linear-address-size*) + addr))))) + + (if (mbe :logic (canonical-address-p 63+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 63+addr) #.*2^47*)) + + (b* (((mv flg0 (the (unsigned-byte 256) xword0) x86) + (rvm256 addr x86)) + ((mv flg1 (the (unsigned-byte 256) xword1) x86) + (rvm256 32+addr x86)) + ((the (unsigned-byte 512) xxword) + (the (unsigned-byte 512) (logior + (the (unsigned-byte 512) + (ash xword1 256)) + xword0)))) + (mbe :logic (if (or flg0 flg1) + (mv 'rvm512 0 x86) + (mv nil xxword x86)) + :exec (mv nil xxword x86))) + + (mv 'rvm512 0 x86))) + + (mv 'rvm512 0 x86)) + + /// + + (local (in-theory (e/d () (rvm256)))) + + (defthm rvm512-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 63 addr))) + (equal (mv-nth 0 (rvm512 addr x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force) rvm256))))) + + (defthm-unsigned-byte-p n512p-mv-nth-1-rvm512 + :hyp t + :bound 512 + :concl (mv-nth 1 (rvm512 addr x86)) + :hints (("Goal" :in-theory (e/d () (unsigned-byte-p force (force) rvm256)))) + :gen-linear t + :gen-type t) + + (defthm x86p-mv-nth-2-rvm512-unchanged + (equal (mv-nth 2 (rvm512 addr x86)) + x86) + :hints (("Goal" :in-theory (e/d () (force (force) rvm256))))) + + (defthm xr-rvm512 + (equal (xr fld index (mv-nth 2 (rvm512 addr x86))) + (xr fld index x86))) + + (defthm rvm512-xw-values + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (rvm512 addr (xw fld index value x86))) + (mv-nth 0 (rvm512 addr x86))) + (equal (mv-nth 1 (rvm512 addr (xw fld index value x86))) + (mv-nth 1 (rvm512 addr x86))))))) + +;; ---------------------------------------------------------------------- + +;; Memory Write Functions: + +(define wvm08 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 8)) + (x86)) + :enabled t + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (mbe :logic (if (canonical-address-p addr) + (let ((x86 (!memi (n48 addr) (n08 val) x86))) + (mv nil x86)) + (mv 'wvm08 x86)) + :exec (let ((x86 (!memi (n48 addr) val x86))) + (mv nil x86))) + /// + + (defthm wvm08-no-error + (implies (canonical-address-p addr) + (equal (mv-nth 0 (wvm08 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm08 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm08 addr val x86)))) + :rule-classes (:rewrite :type-prescription)) + + (defthm xr-wmv08-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm08 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm08) ())))) + + (defthm wvm08-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm08 addr val (xw fld index value x86))) + (mv-nth 0 (wvm08 addr val x86))) + (equal (mv-nth 1 (wvm08 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm08 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm08 wvm08) ()))))) + +(define wvm16 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 16)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((1+addr (the (signed-byte #.*max-linear-address-size+1*) + (1+ (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 1+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) + 1+addr) + #.*2^47*)) + + (b* (((the (unsigned-byte 8) byte0) + (mbe :logic (n08 val) + :exec (logand #xff val))) + ((the (unsigned-byte 8) byte1) + (mbe :logic (part-select val :low 8 :width 8) + :exec (logand #xff (ash val -8)))) + (x86 (!memi (n48 addr) byte0 x86)) + (x86 (!memi (n48 1+addr) byte1 x86))) + (mv nil x86)) + + (mv 'wvm16 x86))) + + (mv 'wvm16 x86)) + + /// + + (defthm wvm16-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 3 addr))) + (equal (mv-nth 0 (wvm16 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm16 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm16 addr val x86)))) + :rule-classes (:rewrite :type-prescription)) + + (defthm xr-wmv16-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm16 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm16) ())))) + + (defthm wvm16-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm16 addr val (xw fld index value x86))) + (mv-nth 0 (wvm16 addr val x86))) + (equal (mv-nth 1 (wvm16 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm16 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm16 wvm16) ()))))) + +(define wvm32 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 32)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((1+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 1 (the (signed-byte #.*max-linear-address-size*) addr)))) + (2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) addr)))) + (3+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 3 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 3+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 3+addr) #.*2^47*)) + + (b* (((the (unsigned-byte 8) byte0) + (mbe :logic (part-select val :low 0 :width 8) + :exec (logand #xff val))) + ((the (unsigned-byte 8) byte1) + (mbe :logic (part-select val :low 8 :width 8) + :exec (logand #xff (ash val -8)))) + ((the (unsigned-byte 8) byte2) + (mbe :logic (part-select val :low 16 :width 8) + :exec (logand #xff (ash val -16)))) + ((the (unsigned-byte 8) byte3) + (mbe :logic (part-select val :low 24 :width 8) + :exec (logand #xff (ash val -24)))) + (x86 (!memi (n48 addr) byte0 x86)) + (x86 (!memi (n48 1+addr) byte1 x86)) + (x86 (!memi (n48 2+addr) byte2 x86)) + (x86 (!memi (n48 3+addr) byte3 x86))) + (mv nil x86)) + + (mv 'wvm32 x86))) + + (mv 'wvm32 x86)) + + /// + + (defthm wvm32-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 3 addr))) + (equal (mv-nth 0 (wvm32 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm32 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm32 addr val x86)))) + :rule-classes (:rewrite :type-prescription)) + + (defthm xr-wmv32-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm32 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm32) ())))) + + (defthm wvm32-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm32 addr val (xw fld index value x86))) + (mv-nth 0 (wvm32 addr val x86))) + (equal (mv-nth 1 (wvm32 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm32 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm32 wvm32) ()))))) + +(define wvm48 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 48)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) addr)))) + (5+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 5 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 5+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 5+addr) #.*2^47*)) + + (b* ((word0 (mbe :logic (part-select val :low 0 :width 16) + :exec (logand #xFFFF val))) + (dword1 (mbe :logic (part-select val :low 16 :width 32) + :exec (logand #xFFFFFFFF (ash val -16)))) + ((mv flg0 x86) (wvm16 addr word0 x86)) + ((mv flg1 x86) (wvm32 2+addr dword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm48 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm48 x86))) + + (mv 'wvm48 x86)) + + /// + + (defthm wvm48-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 5 addr))) + (equal (mv-nth 0 (wvm48 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm48 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm48 addr val x86)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv48-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm48 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm48) ())))) + + (defthm wvm48-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm48 addr val (xw fld index value x86))) + (mv-nth 0 (wvm48 addr val x86))) + (equal (mv-nth 1 (wvm48 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm48 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm48 wvm48) ()))))) + +(define wvm64 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 64)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((4+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 4 (the (signed-byte #.*max-linear-address-size*) addr)))) + (7+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 7 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 7+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 7+addr) #.*2^47*)) + + (b* ((dword0 (mbe :logic (part-select val :low 0 :width 32) + :exec (logand #xFFFFFFFF val))) + (dword1 (mbe :logic (part-select val :low 32 :width 32) + :exec (logand #xFFFFFFFF (ash val -32)))) + ((mv flg0 x86) (wvm32 addr dword0 x86)) + ((mv flg1 x86) (wvm32 4+addr dword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm64 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm64 x86))) + + (mv 'wvm64 x86)) + + /// + + (defthm wvm64-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 7 addr))) + (equal (mv-nth 0 (wvm64 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm64 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm64 addr val x86)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv64-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm64 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm64) ())))) + + (defthm wvm64-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm64 addr val (xw fld index value x86))) + (mv-nth 0 (wvm64 addr val x86))) + (equal (mv-nth 1 (wvm64 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm64 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm64 wvm64) ()))))) + +(define wvm80 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 80)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + + (if (mbt (canonical-address-p addr)) + + (let* ((2+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 2 (the (signed-byte #.*max-linear-address-size*) addr)))) + (9+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 9 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 9+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 9+addr) #.*2^47*)) + + (b* ((word0 (mbe :logic (part-select val :low 0 :width 16) + :exec (logand #xFFFF val))) + (qword1 (mbe :logic (part-select val :low 16 :width 64) + :exec (logand #xFFFFFFFFFFFFFFFF (ash val -16)))) + ((mv flg0 x86) (wvm16 addr word0 x86)) + ((mv flg1 x86) (wvm64 2+addr qword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm80 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm80 x86))) + + (mv 'wvm80 x86)) + + /// + + (defthm wvm80-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 9 addr))) + (equal (mv-nth 0 (wvm80 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm80 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm80 addr val x86)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv80-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm80 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm80) ())))) + + (defthm wvm80-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm80 addr val (xw fld index value x86))) + (mv-nth 0 (wvm80 addr val x86))) + (equal (mv-nth 1 (wvm80 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm80 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm80 wvm80) ()))))) + +(define wvm128 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 128)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + :prepwork ((local (in-theory (e/d () (wvm64))))) + (if (mbt (canonical-address-p addr)) + + (let* ((8+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 8 (the (signed-byte #.*max-linear-address-size*) addr)))) + (15+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 15 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 15+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 15+addr) #.*2^47*)) + + (b* ((qword0 (mbe :logic (part-select val :low 0 :width 64) + :exec (logand #xFFFFFFFFFFFFFFFF val))) + (qword1 (mbe :logic (part-select val :low 64 :width 64) + :exec (logand #xFFFFFFFFFFFFFFFF (ash val -64)))) + ((mv flg0 x86) (wvm64 addr qword0 x86)) + ((mv flg1 x86) (wvm64 8+addr qword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm128 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm128 x86))) + + (mv 'wvm128 x86)) + + /// + + (defthm wvm128-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 15 addr))) + (equal (mv-nth 0 (wvm128 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm128 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm128 addr val x86)))) + :hints (("Goal" :in-theory (e/d () (x86p)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv128-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm128 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm128) ())))) + + (defthm wvm128-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm128 addr val (xw fld index value x86))) + (mv-nth 0 (wvm128 addr val x86))) + (equal (mv-nth 1 (wvm128 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm128 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm128 wvm128) ()))))) + +(define wvm256 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 256)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + :prepwork ((local (in-theory (e/d () (wvm128))))) + (if (mbt (canonical-address-p addr)) + + (let* ((16+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 16 (the (signed-byte #.*max-linear-address-size*) addr)))) + (31+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 31 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 31+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 31+addr) #.*2^47*)) + + (b* ((oword0 (mbe :logic (part-select val :low 0 :width 128) + :exec (logand #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF val))) + (oword1 (mbe :logic (part-select val :low 128 :width 128) + :exec (logand #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF (ash val -128)))) + ((mv flg0 x86) (wvm128 addr oword0 x86)) + ((mv flg1 x86) (wvm128 16+addr oword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm256 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm256 x86))) + + (mv 'wvm256 x86)) + + /// + + (defthm wvm256-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 31 addr))) + (equal (mv-nth 0 (wvm256 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm256 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm256 addr val x86)))) + :hints (("Goal" :in-theory (e/d () (x86p)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv256-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm256 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm256) ())))) + + (defthm wvm256-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm256 addr val (xw fld index value x86))) + (mv-nth 0 (wvm256 addr val x86))) + (equal (mv-nth 1 (wvm256 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm256 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm256 wvm256) ()))))) + +(define wvm512 + ((addr :type (signed-byte #.*max-linear-address-size*)) + (val :type (unsigned-byte 512)) + (x86)) + :enabled t + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :inline t + :no-function t + :parents (linear-memory-in-app-view) + :prepwork ((local (in-theory (e/d () (wvm256))))) + (if (mbt (canonical-address-p addr)) + + (let* ((32+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 32 (the (signed-byte #.*max-linear-address-size*) addr)))) + (63+addr (the (signed-byte #.*max-linear-address-size+1*) + (+ 63 (the (signed-byte #.*max-linear-address-size*) addr))))) + + (if (mbe :logic (canonical-address-p 63+addr) + :exec (< (the (signed-byte #.*max-linear-address-size+1*) 63+addr) #.*2^47*)) + + (b* ((oword0 (mbe :logic (part-select val :low 0 :width 256) + :exec (logand #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + val))) + (oword1 (mbe :logic (part-select val :low 256 :width 256) + :exec (logand #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + (ash val -256)))) + ((mv flg0 x86) (wvm256 addr oword0 x86)) + ((mv flg1 x86) (wvm256 32+addr oword1 x86))) + (mbe :logic (if (or flg0 flg1) + (mv 'wvm512 x86) + (mv nil x86)) + :exec (mv nil x86))) + + (mv 'wvm512 x86))) + + (mv 'wvm512 x86)) + + /// + + (defthm wvm512-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ 63 addr))) + (equal (mv-nth 0 (wvm512 addr val x86)) + nil)) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + (defthm x86p-mv-nth-1-wvm512 + (implies (x86p x86) + (x86p (mv-nth 1 (wvm512 addr val x86)))) + :hints (("Goal" :in-theory (e/d () (x86p)))) + :rule-classes (:type-prescription :rewrite)) + + (defthm xr-wmv512-app-view + (implies (not (equal fld :mem)) + (equal (xr fld index (mv-nth 1 (wvm512 addr val x86))) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wvm512) ())))) + + (defthm wvm512-xw-app-view + (implies (not (equal fld :mem)) + (and (equal (mv-nth 0 (wvm512 addr val (xw fld index value x86))) + (mv-nth 0 (wvm512 addr val x86))) + (equal (mv-nth 1 (wvm512 addr val (xw fld index value x86))) + (xw fld index value (mv-nth 1 (wvm512 addr val x86)))))) + :hints (("Goal" :in-theory (e/d* (wvm512 wvm512) ()))))) + +;; ---------------------------------------------------------------------- + +(globally-disable '(rvm08 rvm16 wvm16 rvm32 rvm48 rvm64 rvm80 rvm128 rvm256 rvm512 + wvm08 wvm16 wvm16 wvm32 wvm48 wvm64 wvm80 wvm128 wvm256 wvm512)) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/decoding-and-spec-utils.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/decoding-and-spec-utils.lisp new file mode 100644 index 000000000..d6feb5f4e --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/decoding-and-spec-utils.lisp @@ -0,0 +1,2615 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2019, Kestrel Technology, LLC + +; 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 GoelWe introduce several utilities trafficking in @('erp') +objects, each of which is a stack of individual error objects +typically of the form (ctx . kwd-alist), where @('ctx') is +typically a function name. We do not enforce the shape of an +individual error object, however.
+ ++ In 64-bit mode, a 64-bit instruction pointer is read from the full RIP. + Since, in the model, this is a 48-bit signed integer, + this function returns a 48-bit signed integer. +
++ In 32-bit mode, a 32-bit or 16-bit instruction pointer is read from + EIP (i.e. the low 32 bits of RIP) + or IP (i.e. the low 16 bits of RIP), + based on the CS.D bit, + i.e. the D bit of the current code segment descriptor. + Either way, this function returns an unsigned 32-bit or 16-bit integer, + which is also a signed 48-bit integer. +
++ See AMD manual, Oct'13, Vol. 1, Sec. 2.2.4 and Sec. 2.5. + AMD manual, Apr'16, Vol. 2, Sec 4.7.2., + and Intel manual, Mar'17, Vol. 1, Sec. 3.6. +
++ In 32-bit mode, the address-size override prefix (if present) + should not affect the instruction pointer size. + It does not seem to make sense + to change the instruction pointer size on a per-instruction basis. +
" + (b* ((*ip (the (signed-byte #.*max-linear-address-size*) (rip x86)))) + (case proc-mode + (#.*64-bit-mode* *ip) + (#.*compatibility-mode* + (b* (((the (unsigned-byte 16) cs-attr) + (seg-hidden-attri #.*cs* x86)) + (cs.d (code-segment-descriptor-attributesBits->d cs-attr))) + (if (= cs.d 1) + (n32 *ip) + (n16 *ip)))) + (otherwise + ;; Unimplemented for other modes! + 0))) + :inline t + :no-function t + /// + + (defthm-signed-byte-p read-*ip-is-i48p + :hyp (x86p x86) + :bound 48 + :concl (read-*ip proc-mode x86) + :gen-type t + :gen-linear t) + + (defrule read-*ip-when-64-bit-modep + (equal (read-*ip #.*64-bit-mode* x86) + (rip x86))) + + (defthm-unsigned-byte-p read-*ip-when-not-64-bit-modep + :hyp (not (equal proc-mode #.*64-bit-mode*)) + :bound 32 + :concl (read-*ip proc-mode x86) + :gen-type t + :gen-linear t)) + + (define add-to-*ip ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (*ip :type (signed-byte #.*max-linear-address-size*)) + (delta :type (signed-byte #.*max-linear-address-size*)) + x86) + :returns (mv flg + (*ip+delta i48p + :hyp (and (i48p *ip) (i48p delta)))) + :prepwork + ((local (in-theory (e/d () (force (force)))))) + :parents (instruction-pointer-operations) + :short "Add a specified amount to an instruction pointer." + :long + "+ The amount may be positive (increment) or negative (decrement). + This just calculates the new instruction pointer value, + without storing it into the register RIP, EIP, or IP. + The starting value is the result of @(tsee read-*ip) + or a previous invocation of @(tsee add-to-*ip). +
++ In 64-bit mode, we check whether the result is a canonical address; + in 32-bit mode, we check whether the result is within the segment limit. + If these checks are not satisfied, + this function returns an error flag (and 0 as incremented address), + which causes the x86 model to stop execution with an error. + It is not clear whether these checks should be performed + when the instruction pointer is incremented + or when an instruction byte is eventually accessed; + the Intel and AMD manuals seem unclear in this respect. + But since the failure of these checks stops execution with an error, + and it is in a way always ``safe'' to stop execution with an error + (in the sense that the model provides no guarantees when this happens), + for now we choose to perform these checks here. +
++ Note that a code segment is never expand-down, + so the valid effective addresses are always between 0 and the segment limit + (cf. @(tsee segment-base-and-bounds)). +
" + + (b* ((*ip+delta + (the (signed-byte #.*max-linear-address-size+1*) + (+ *ip delta)))) + + (case proc-mode + + (#.*64-bit-mode* + (if (mbe :logic (canonical-address-p *ip+delta) + :exec (and (<= #.*-2^47* *ip+delta) + (< *ip+delta #.*2^47*))) + (mv nil *ip+delta) + (mv (list :non-canonical-instruction-pointer *ip+delta) 0))) + + (#.*compatibility-mode* + (b* (((the (unsigned-byte 32) cs.limit) + (mbe :logic (loghead 32 (seg-hidden-limiti #.*cs* x86)) + :exec (seg-hidden-limiti #.*cs* x86)))) + (if (and (<= 0 *ip+delta) + (<= *ip+delta cs.limit)) + (mv nil (the (unsigned-byte 32) *ip+delta)) + (mv (list :out-of-segment-instruction-pointer cs.limit *ip+delta) + 0)))) + + (otherwise + (mv (list :unimplemented-proc-mode proc-mode) + 0)))) + + :inline t + :no-function t + /// + + (defthm-signed-byte-p add-to-*ip-is-i48p + :hyp (and (integerp *ip) + (<= -140737488355328 *ip) + (< *ip 140737488355328) + (integerp delta)) + :bound 48 + :concl (mv-nth 1 (add-to-*ip proc-mode *ip delta x86)) + :gen-type t + :gen-linear t) + + (encapsulate () + (local (include-book "kestrel/arithmetic-light/expt2" :dir :system)) + (defthm signed-byte-p-of-mv-nth-1-of-add-to-*ip + (implies (and (<= 48 size) + (integerp size) + (integerp *ip) + (integerp delta)) + (signed-byte-p size + (mv-nth 1 + (add-to-*ip proc-mode *ip delta x86)))))) + + ;; The following theorem is a rewrite rule version of the theorems + ;; generated by the DEFTHM-SIGNED-BYTE-P just above. This rewrite rule is + ;; somewhat inelegant, but without it some proofs fail. The proofs in + ;; question are the ones that enable this rule explicitly, for example the + ;; guard verification proof of TWO-BYTE-OPCODE-DECODE-AND-EXECUTE. The + ;; proofs fail with subgoals about nested ADD-TO-*IPs being 48-bit signed + ;; integers. Perhaps the reason for the failures is that the type + ;; prescription and linear rules generated by the DEFTHM-SIGNED-BYTE-P + ;; above are not triggered on the outer ADD-TO-*IPs of the nests because no + ;; hypotheses about the inner ADD-TO-*IPs are available in the current + ;; proof context. With a rewrite rule, instead, the prover is able to + ;; backchain. We should see if we arrange things so that we can avoid this + ;; rewrite rule. We leave it disabled by default so it's more clear where + ;; it's needed. + (defruled add-to-*ip-is-i48p-rewrite-rule + (implies + (and (integerp *ip) + (<= -140737488355328 *ip) + (< *ip 140737488355328) + (integerp delta)) + (and (integerp (mv-nth 1 (add-to-*ip proc-mode *ip delta x86))) + (rationalp (mv-nth 1 (add-to-*ip proc-mode *ip delta x86))) + (<= -140737488355328 (mv-nth 1 (add-to-*ip proc-mode *ip delta x86))) + (< (mv-nth 1 (add-to-*ip proc-mode *ip delta x86)) 140737488355328) + ;; The following allow us to disable signed-byte-p in places where + ;; add-to-*ip is followed by a check for canonical addresses. + (signed-byte-p 48 (mv-nth 1 (add-to-*ip proc-mode *ip delta x86))) + (signed-byte-p 64 (mv-nth 1 (add-to-*ip proc-mode *ip delta x86)))))) + + (defrule add-to-*ip-rationalp-type + (implies (and (rationalp *ip) + (rationalp delta)) + (rationalp (mv-nth 1 (add-to-*ip proc-mode *ip delta x86)))) + :rule-classes :type-prescription) + + (defrule mv-nth-0-of-add-to-*ip-when-64-bit-modep + (equal (mv-nth 0 (add-to-*ip #.*64-bit-mode* *ip delta x86)) + (if (canonical-address-p (+ *ip delta)) + nil + (list + :non-canonical-instruction-pointer (+ *ip delta))))) + + (defrule mv-nth-1-of-add-to-*ip-when-64-bit-modep + (equal (mv-nth 1 (add-to-*ip #.*64-bit-mode* *ip delta x86)) + (if (canonical-address-p (+ *ip delta)) + (+ *ip delta) + 0))) + + (defthm-unsigned-byte-p mv-nth-1-of-add-to-*ip-when-compatibility-modep + :hyp (and (not (equal proc-mode #.*64-bit-mode*)) + (integerp (+ *ip delta)) + (not (mv-nth 0 + (add-to-*ip + proc-mode *ip delta x86)))) + :bound 32 + :concl (mv-nth 1 (add-to-*ip proc-mode *ip delta x86)) + :gen-linear t + :gen-type t)) + + (define write-*ip ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (*ip :type (signed-byte #.*max-linear-address-size*)) + x86) + :returns (x86-new x86p + :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (force (force)))))) + + :guard (if (equal proc-mode #.*64-bit-mode*) + t + ;; Could strengthen the following, based on cs.d value... + (unsigned-byte-p 32 *ip) + ;; (b* (((the (unsigned-byte 16) cs-attr) + ;; (xr :seg-hidden-attr #.*cs* x86)) + ;; (cs.d + ;; (code-segment-descriptor-attributesBits->d cs-attr))) + ;; (case cs.d + ;; (0 (unsigned-byte-p 16 *ip)) + ;; (otherwise (unsigned-byte-p 32 *ip)))) + ) + + :parents (instruction-pointer-operations) + :short "Write an instruction pointer into the register RIP, EIP, or IP." + :long + "+ In 64-bit mode, a 64-bit instruction pointer is written into the full RIP. + Since, in the model, this is a 48-bit signed integer, + this function consumes a 48-bit signed integer. +
++ In 32-bit mode, the instruction pointer is 32 or 16 bits + based on the CS.D bit, i.e. the D bit of the current code segment descriptor. + In these cases, the argument to this function should be + a 32-bit or 16-bit unsigned integer, which is also a 48-bit signed integer. +
++ See AMD manual, Oct'13, Vol. 1, Sec. 2.2.4 and Sec. 2.5. + AMD manual, Apr'16, Vol. 2, Sec 4.7.2., + and Intel manual, Mar'17, Vol. 1, Sec. 3.6. +
++ According to Intel manual, Mar'17, Vol. 1, Table 3-1, + it seems that + when writing a 32-bit instruction pointer (EIP) + the high 32 bits of RIP should be set to 0, + and when writing a 16-bit instruction pointer (IP) + the high 48 bits of RIP should be left unmodified; + since in our model the RIP is 48 bits, + the above applies to the high 16 and 32 bits, respectively. + The pseudocode for the JMP instruction in Intel manual, Mar'17, Vol. 2 + shows an assignment @('EIP <- tempEIP AND 0000FFFFh') for the 16-bit case, + which seems to imply that + the high 32 (or 16, in our model) bits are left unmodified + and the high 16 bits of EIP are set to 0, + which would contradict Table 3-1; + the pseudocode for some other instructions + that directly write the instruction pointer (e.g. RET and Jcc) + show similar assignments. + However, it is possible that this assignment has a typo and should be + @('IP <- tempEIP AND 0000FFFFh') instead, + which would be consistent with Table 3-1. + But we also note that the pseudocode for the JMP instruction + shows an assignment @('EIP <- tempEIP') for the 32-bit case, + which seems to imply that + the high 32 (or 16, in our model) bits are left unmodified, + which would contradict Table 3-1. + The AMD manuals do not show pseudocode for these instructions, + and AMD manual, Oct'13, Vol. 1, Fig. 2-10 + (which is somewhat analogous to Intel's Table 3-1) + shows the high bits simply grayed out; + so the AMD manuals do not provide disambiguation help. + It is also possible that Table 3-1 has a typo and should say + that a 16-bit instruction pointer is zero-extended, + but that is not quite consistent with the pseudocode assignments to EIP, + which seem to imply that the high bits are untouched. + Table 3-1 is under a section titled + `Address Calculation in 64-Bit Mode', + which may suggest that the table may not apply to 32-bit mode, + but then it is not clear how it would just apply to 64-bit mode. + For now, we decide to have this function follow Intel's Table 3-1, + but we may revise that if we manage to resolve these ambiguities. + We also note that Intel's Table 3-1 is consistent with the way in which + 32-bit and 16-bit values are written to general-purpose registers + (even though RIP/EIP/IP is not a general-purpose register); + see @(tsee wr32) and @(tsee wr16). +
++ This function should be always called + with an instruction pointer of the right type + (48-bit signed, 32-bit unsigned, or 16-bit unsigned) + based on the mode and code segment. + We may add a guard to ensure that in the future, + but for now in the code below + we coerce the instruction pointer to 32 and 16 bits as appropriate, + to verify guards; + these coercions are expected not to change + the argument instruction pointer. +
" + + :prepwork + ((local + (in-theory (e/d () + (bitops::logand-with-bitmask + bitops::logand-with-negated-bitmask)))) + + (local + (defthm logtail-logbitp-lemma + (equal (logand 1 (logtail n x)) + (bool->bit (logbitp n x))) + :hints (("Goal" :in-theory (e/d* (bitops::ihsext-inductions + bitops::ihsext-recursive-redefs) + ())))))) + + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand) + (unsigned-byte-p)))) + + (case proc-mode + + (#.*64-bit-mode* + (!rip *ip x86)) + + (#.*compatibility-mode* ;; Maybe *protected-mode* too? + (b* ((cs-attr (the (unsigned-byte 16) (seg-hidden-attri #.*cs* x86))) + (cs.d (code-segment-descriptor-attributesBits->d cs-attr))) + (if (= cs.d 1) + (mbe :logic (!rip (n32 *ip) x86) + :exec (!rip (the (unsigned-byte 32) *ip) x86)) + (b* ((rip (the (signed-byte #.*max-linear-address-size*) + (rip x86))) + ((the (signed-byte #.*max-linear-address-size*) rip-new) + (mbe :logic (part-install (n16 *ip) rip :low 0 :width 16) + :exec (logior (logand #ux-1_0000 rip) + (logand #uxFFFF *ip))))) + (!rip rip-new x86))))) + + (otherwise + ;; Unimplemented for other modes! + x86)) + :inline t + :no-function t + /// + + (defrule write-*ip-when-64-bit-modep + (equal (write-*ip #.*64-bit-mode* *ip x86) + (!rip *ip x86))))) + +;; ====================================================================== + +(defsection stack-pointer-operations + :parents (decoding-and-spec-utils) + :short "Operations to manipulate stack pointers." + + (define select-stack-address-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + x86) + :returns (size (member-equal size '(2 4 8))) + :parents (stack-pointer-operations) + :short "Address size of the stack, in bytes." + :long + "+ This is the @('StackAddrSize') attribute + in Intel manual, June 2026, Vol. 2, Sec. 3.1.1.9, + which is also called @('StackAddressSize') or @('StackSize') + in the pseudocode of some instructions that operate on the stack. +
++ In 64-bit mode, the stack address size is always 64 bits. + In 32-bit mode, it is 32 or 16 bits, + based on the SS.B bit, + i.e. the B bit of the current stack segment descriptor. + See Intel manual, Jun 2026, Vol. 1, Sec. 6.2.3 and Sec. 6.2.5, + and AMD manual, Jun'23, Vol. 2, Sec. 2.4.5 and Sec. 4.7.3. + The address-size override prefix does not affect + the stack address size (see @(tsee read-*sp)). +
" + (case proc-mode + (#.*64-bit-mode* 8) + (#.*compatibility-mode* + (b* (((the (unsigned-byte 16) ss-attr) (seg-hidden-attri #.*ss* x86)) + (ss.b (data-segment-descriptor-attributesBits->d/b ss-attr))) + (if (= ss.b 1) 4 2))) + (otherwise + ;; Unimplemented for other modes, + ;; but we return the stack address size of + ;; real-address and virtual-8086 modes (i.e. 16 bits), + ;; to keep the return type of this function simple. + 2)) + :inline t + :no-function t + /// + + (defrule select-stack-address-size-when-64-bit-modep + (equal (select-stack-address-size #.*64-bit-mode* x86) 8)) + + (defrule select-stack-address-size-lower-bound + (<= 2 (select-stack-address-size proc-mode x86)) + :rule-classes :linear) + + (defrule select-stack-address-size-upper-bound + (<= (select-stack-address-size proc-mode x86) 8) + :rule-classes :linear) + + (defrule member-equal-of-select-stack-address-size-1-2-4-8 + (member-equal (select-stack-address-size proc-mode x86) + '(1 2 4 8)))) + + (define read-*sp ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + x86) + :returns (*sp i64p :hyp (x86p x86)) + :parents (stack-pointer-operations) + :short "Read the stack pointer from the register RSP, ESP, or SP." + :long + "+ In 64-bit mode, a 64-bit stack pointer is read from the full RSP. + Since, in the model, this is a 64-bit signed integer, + this function returns a 64-bit signed integer. +
++ In 32-bit mode, a 32-bit or 16-bit stack pointer is read from + ESP (i.e. the low 32 bits of RSP) + or SP (i.e. the low 16 bits of RSP), + based on the SS.B bit, + i.e. the B bit of the current stack segment register. + Either way, this function returns an unsigned 32-bit or 16-bit integer, + which is also a signed 64-bit integer. +
++ See Intel manual, Mar'23, Vol. 1, Sec. 6.2.3 and Sec. 6.2.5, + and AMD manual, Jun'23, Vol. 2, Sec 2.4.5 and Sec. 4.7.3. + The actual size of the value returned by this function + is @('StackAddrSize'), + introduced in Intel manual, Dec'23, Vol. 2, Sec. 3.1.1.9. +
++ In 32-bit mode, the address-size override prefix (if present) + should not affect the stack address size. + It does not seem to make sense + to change the stack address size on a per-instruction basis. +
" + (b* ((*sp (the (signed-byte 64) (rgfi #.*rsp* x86)))) + (case proc-mode + (#.*64-bit-mode* + *sp) + (#.*compatibility-mode* + (b* (((the (unsigned-byte 16) ss-attr) (seg-hidden-attri #.*ss* x86)) + (ss.b (data-segment-descriptor-attributesBits->d/b ss-attr))) + (if (= ss.b 1) + (n32 *sp) + (n16 *sp)))) + (otherwise + ;; Unimplemented for other modes! + 0))) + :inline t + :no-function t + /// + + (defthm-signed-byte-p read-*sp-is-i64p + :hyp (x86p x86) + :bound 64 + :concl (read-*sp proc-mode x86) + :gen-type t + :gen-linear t) + + (defrule read-*sp-when-64-bit-modep + (equal (read-*sp #.*64-bit-mode* x86) (rgfi *rsp* x86))) + + (defthm-unsigned-byte-p read-*sp-when-not-64-bit-modep + :hyp (not (equal proc-mode #.*64-bit-mode*)) + :bound 32 + :concl (read-*sp proc-mode x86) + :gen-type t + :gen-linear t)) + + (define add-to-*sp ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (*sp :type (signed-byte 64)) + (delta :type (signed-byte 64)) + x86) + :returns (mv flg + (*sp+delta i64p)) + :parents (stack-pointer-operations) + :short "Add a specified amount to a stack pointer." + :long + "+ The amount may be positive (increment) or negative (decrement). + This just calculates the new stack pointer value, + without storing it into the register RSP, ESP, or SP. + The starting value is the result of @(tsee read-*sp) + or a previous invocation of @(tsee add-to-*sp). +
++ The increment or decrement is modular: + 64 bits in 64-bit mode, + and either 32 or 16 bits in 32-bit mode (depending on the SS.B bit). + Since our model uses signed 64-bit addresses, we use @(tsee i64) for them, + while we use @(tsee n32) or @(tsee n16) for 32-bit and 16-bit addresses. +
++ In 64-bit mode, we check whether the result is a canonical address; + in 32-bit mode, we check whether the result is within the segment limit. + If these checks are not satisfied, + this function returns an error flag (and 0 as new pointer), + which causes the x86 model to stop execution with an error. + It is not clear whether these checks should be performed + when the stack pointer is updated, + or when the stack is eventually accessed through the updated pointer; + the Intel and AMD manuals seem unclear in this respect. + But since the failure of these checks stops execution with an error, + and it is in a way always ``safe'' to stop execution with an error + (in the sense that the model provides no guarantees when this happens), + for now we choose to perform these checks here. +
++ Note that a stack segment may be expand-down or expand-up + (see Intel manual, Mar'17, Vol. 3, Sec. 3.4.5.1), + so the checks need to cover these two cases. + See @(tsee segment-base-and-bounds) and @(tsee ea-to-la). +
" + (b* ((*sp+delta (the (signed-byte 65) (+ *sp delta)))) + (case proc-mode + (#.*64-bit-mode* + (let ((*sp+delta (i64 *sp+delta))) + (if (mbe :logic (canonical-address-p *sp+delta) + :exec (and (<= #.*-2^47* *sp+delta) + (< *sp+delta #.*2^47*))) + (mv nil *sp+delta) + (mv (list :non-canonical-stack-address *sp+delta) 0)))) + (#.*compatibility-mode* + (b* (((the (unsigned-byte 32) ss.limit) + (seg-hidden-limiti #.*ss* x86)) + ((the (unsigned-byte 16) ss-attr) + (seg-hidden-attri #.*ss* x86)) + (ss.b (data-segment-descriptor-attributesBits->d/b ss-attr)) + (ss.e (data-segment-descriptor-attributesBits->e ss-attr)) + (ss-lower (if (= ss.e 1) (1+ ss.limit) 0)) + (ss-upper (if (= ss.e 1) + (if (= ss.b 1) + #xffffffff + #xffff) + ss.limit)) + (*sp+delta (if (= ss.b 1) (n32 *sp+delta) (n16 *sp+delta))) + ((unless (and (<= ss-lower *sp+delta) + (<= *sp+delta ss-upper))) + (mv (list :out-of-segment-stack-address + *sp+delta ss-lower ss-upper) + 0))) + (mv nil *sp+delta))) + (otherwise + (mv (list :unimplemented-proc-mode proc-mode) + 0)))) + :inline t + :no-function t + /// + + (defthm-signed-byte-p add-to-*sp-is-i64p + :bound 48 + :concl (mv-nth 1 (add-to-*sp proc-mode *sp delta x86)) + :gen-type t + :gen-linear t) + + (defrule mv-nth-0-of-add-to-*sp-when-64-bit-modep + (equal (mv-nth 0 (add-to-*sp #.*64-bit-mode* *sp delta x86)) + (if (canonical-address-p (i64 (+ *sp delta))) + nil + (list :non-canonical-stack-address (i64 (+ *sp delta)))))) + + (defrule mv-nth-1-of-add-to-*sp-when-64-bit-modep + (equal (mv-nth 1 (add-to-*sp #.*64-bit-mode* *sp delta x86)) + (if (canonical-address-p (i64 (+ *sp delta))) + (i64 (+ *sp delta)) + 0))) + + (defthm-unsigned-byte-p mv-nth-1-of-add-to-*sp-when-compatibility-modep + :hyp (and (not (equal proc-mode #.*64-bit-mode*)) + (integerp (+ *sp delta)) + (not (mv-nth 0 (add-to-*sp proc-mode *sp delta x86)))) + :bound 32 + :concl (mv-nth 1 (add-to-*sp proc-mode *sp delta x86)) + :hints (("Goal" :in-theory (e/d () (force (force))))) + :gen-linear t + :gen-type t)) + + (define write-*sp ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (*sp :type (signed-byte 64)) + x86) + :returns (x86-new x86p :hyp (x86p x86)) + :parents (stack-pointer-operations) + + :guard (if (equal proc-mode #.*64-bit-mode*) + t + ;; Could strengthen the following, based on ss.d. + (unsigned-byte-p 32 *sp) + ;; (b* (((the (unsigned-byte 16) ss-attr) + ;; (xr :seg-hidden-attr #.*ss* x86)) + ;; (ss.d + ;; (data-segment-descriptor-attributesBits->d/b ss-attr))) + ;; (case ss.d + ;; (0 (unsigned-byte-p 16 *sp)) + ;; (otherwise (unsigned-byte-p 32 *sp)))) + ) + + :short "Write a stack pointer into the register RSP, ESP, or SP." + :long + "+ In 64-bit mode, a 64-bit stack pointer is written into the full RSP. + Since, in the model, this is a 64-bit signed integer, + this function consumes a 64-bit signed integer. +
++ In 32-bit mode, the stack pointer is 32 or 16 bits based on the SS.B bit, + i.e. the B bit of the current stack segment descriptor. + In these cases, the argument to this function should be + a 32-bit or 16-bit unsigned integer, which is also a 64-bit signed integer. +
++ See Intel manual, Mar'17, Vol. 1, Sec. 6.2.3 and Sec. 6.2.5, + and AMD manual, Apr'16, Vol. 2, Sec 2.4.5 and Sec. 4.7.3. + The actual size of the value consumed by this function + should be @('StackAddrSize'), + introduced in Intel manual, Mar'17, Vol. 2, Sec. 3.1.1.9. +
++ The pseudocode of stack instructions like PUSH + in Intel manual, Mar'17, Vol. 2 + show assignments of the form + @('RSP <- ...'), @('ESP <- ...'), and @('SP <- ...') + based on the stack address size. + This may suggests that + when the stack address size is 32 + the assignment to ESP leaves the high 32 bits of RSP unchanged, + and when the stack address size is 16 + the assignment to SP leaves the high 48 bits of RSP unchanged. + However, + as explained in the documentation of @(tsee wr32) and @(tsee wr16), + normally writing to the low 32 bits of a general-purpose register + (which RSP/ESP/SP is) zeros the high 32 bits, + while writing the low 16 bits leaves the high 48 bits unchanged. + Thus, we follow this requirement also when writing RSP/ESP/SP implicitly, + via stack manipulation instructions like PUSH that use + this @(tsee write-*sp) function to update the stack pointer register. +
++ This function should be always called + with a stack pointer of the right type + (64-bit signed, 32-bit unsigned, or 16-bit unsigned) + based on the stack address size. + We may add a guard to ensure that in the future, + but for now in the code below + we coerce the stack pointer to 32 and 16 bits as appropriate, + to verify guards; + these coercions are expected not to change the argument stack pointer. +
" + + :prepwork + ((local + (in-theory (e/d () + (bitops::logand-with-bitmask + bitops::logand-with-negated-bitmask)))) + + (local + (defthm logtail-logbitp-lemma + (equal (logand 1 (logtail n x)) + (bool->bit (logbitp n x))) + :hints (("Goal" :in-theory (e/d* (bitops::ihsext-inductions + bitops::ihsext-recursive-redefs) + ())))))) + + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand) + (unsigned-byte-p)))) + + (case proc-mode + (#.*64-bit-mode* + (!rgfi #.*rsp* *sp x86)) + (#.*compatibility-mode* + (b* (((the (unsigned-byte 16) ss-attr) (seg-hidden-attri #.*ss* x86)) + (ss.b (data-segment-descriptor-attributesBits->d/b ss-attr))) + (if (= ss.b 1) + (mbe :logic (!rgfi #.*rsp* (n32 *sp) x86) + :exec (!rgfi #.*rsp* (the (unsigned-byte 32) *sp) x86)) + (b* (((the (signed-byte 64) rsp) (rgfi #.*rsp* x86)) + ((the (signed-byte 64) rsp-new) + (mbe :logic (part-install (n16 *sp) rsp :low 0 :width 16) + :exec (logior (logand #ux-1_0000 rsp) + (logand #xFFFF *sp))))) + (!rgfi #.*rsp* rsp-new x86))))) + (otherwise + ;; Unimplemented for other modes! + x86)) + :inline t + :no-function t + /// + + (defrule write-*sp-when-64-bit-modep + (equal (write-*sp #.*64-bit-mode* *sp x86) + (!rgfi #.*rsp* *sp x86))))) + +;; ====================================================================== + +(define select-address-size ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (p4? booleanp) + (x86 x86p)) + :prepwork + ((local (in-theory (e/d () (force (force)))))) + :returns (address-size (member-equal address-size '(2 4 8))) + :inline t + :no-function t + :parents (decoding-and-spec-utils) + :short "Address size of an instruction, in bytes." + :long + "+ This is based on AMD manual, Dec'17, Volume 3, Table 1-3, + and AMD manual, Dec'17, Volume 2, Sections 4.7 and 4.8. +
++ In 64-bit mode, the address size is + 64 bits if there is no address override prefix, + 32 bits if there is an address override prefix. + In 32-bit mode, the address size is + 32 bits if either + (i) the default address size is 32 bits + and there is no address override prefix, or + (ii) the default address size is 16 bits + and there is an eddress override prefix; + otherwise, the address size is 16 bits. + In 32-bit mode, + the default address size is determined by the CS.D bit of the code segment: + 32 bits if CS.D is 1, 16 bits if CS.D is 0. +
++ The boolean argument of this function + indicates whether there is an override prefix or not. +
" + (case proc-mode + (#.*64-bit-mode* (if p4? 4 8)) + (otherwise ;; #.*compatibility-mode* or #.*protected-mode* + (b* (((the (unsigned-byte 16) cs-attr) (seg-hidden-attri #.*cs* x86)) + (cs.d (code-segment-descriptor-attributesBits->d cs-attr))) + (if (= cs.d 1) (if p4? 2 4) (if p4? 4 2))))) + /// + + (defrule select-address-size-not-2-when-64-bit-modep + (not (equal 2 (select-address-size #.*64-bit-mode* p4? x86)))) + + (defrule select-address-size-leq-8 + (<= (select-address-size proc-mode p4? x86) 8) + :rule-classes :linear) + + (encapsulate () + (local (include-book "kestrel/arithmetic-light/expt2" :dir :system)) + (defthm signed-byte-p-of-select-address-size + (implies (and (<= 5 size) + (integerp size)) + (signed-byte-p size (select-address-size proc-mode p4? x86))) + :hints (("Goal" :in-theory (enable signed-byte-p)))))) + +;; ====================================================================== + +(defsection effective-address-computations + + :parents (decoding-and-spec-utils) + + :short "Computing effective address using ModR/M, SIB bytes, and + displacement bytes present in the instruction" + + (local (xdoc::set-default-parents effective-address-computations)) + + ;; Alignment checks are only carried out in data (or stack) + ;; accesses, and not in code fetches or system segment accesses. + ;; Since these functions are used during code fetching, we call all + ;; r*me* functions with check-alignment? nil. + + (define x86-effective-addr-from-sib + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + (rex-byte :type (unsigned-byte 8) "REX byte") + (mod :type (unsigned-byte 2) "mod field of a ModR/M byte") + (sib :type (unsigned-byte 8) "SIB byte") + x86) + + :guard (sib-p sib) + :guard-hints (("Goal" + :use ((:instance bitops::unsigned-byte-p-incr + (a 3) + (b 4) + (x (sib->base sib)))) + :in-theory (e/d () + (unsigned-byte-p + bitops::unsigned-byte-p-incr)))) + + :short "Calculates effective address when SIB is present." + :long "Source: Intel Vol. 2A, Table 2-3.
+Also see Intel Vol. 2A, Table 2-2 and Figure 2-6.
+In 64-bit mode, + we use @('rgfi') to read bases as signed linear addresses, + which encode canonical linear addresses, + which are also effective addresses in 64-bit mode. + In 32-bit mode, + we use @('rr32') to read bases as unsigned effective addresses.
+In 64-bit mode, + we use @('rgfi') to read indices as signed 64-bit values. + In 32-bit mode, + we limit them to signed 32-bit values.
+Note that, in 32-bit mode, + we call this function only when the address size is 32 bits. + When the address size is 16 bits, there is no SIB byte: + See Intel Vol. 2 Table 2-1.
+The displacement is read as a signed values: + see AMD manual, Dec'17, Volume 3, Section 1.5.
" + + :returns (mv flg + (non-truncated-memory-address + integerp :hyp (and (force (x86p x86)) + (integerp temp-rip)) + :rule-classes :type-prescription) + disp + (increment-RIP-by natp :rule-classes :type-prescription) + (x86 x86p :hyp (force (x86p x86)))) + + :prepwork ((local (in-theory (e/d (rime-size riml-size) + (unsigned-byte-p))))) + + (b* (((the (unsigned-byte 3) b) (sib->base sib)) + (check-alignment? nil) + ((mv flg base displacement nrip-bytes x86) + + (case mod + + (0 (if (equal b 5) + (b* (((mv ?flg0 dword x86) + (rime-size-opt + proc-mode 4 temp-RIP #.*cs* :x check-alignment? x86 + :mem-ptr? nil)) ;; sign-extended + ((when flg0) + (mv (cons flg0 'rime-size-opt-error) 0 0 0 x86))) + (mv nil 0 dword 4 x86)) + (mv nil + (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index b rex-byte #.*b*) x86) + (rr32 b x86)) ; rex-byte is 0 in 32-bit mode + 0 + 0 + x86))) + + (1 (b* (((mv ?flg1 byte x86) + (rime-size-opt proc-mode 1 temp-RIP #.*cs* :x check-alignment? x86 + :mem-ptr? nil)) ;; sign-extended + ((when flg1) + (mv (cons flg1 'rime-size-opt-error) 0 0 0 x86))) + (mv nil + (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index b rex-byte #.*b*) x86) + (rr32 b x86)) ; rex-byte is 0 in 32-bit mode + byte + 1 + x86))) + + (2 (b* (((mv ?flg2 dword x86) + (rime-size-opt proc-mode 4 temp-RIP #.*cs* :x check-alignment? x86 + :mem-ptr? nil)) ;; sign-extended + ((when flg2) + (mv (cons flg2 'rime-size-opt-error) 0 0 0 x86))) + (mv nil + (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index b rex-byte #.*b*) x86) + (rr32 b x86)) ; rex-byte is 0 in 32-bit mode + dword + 4 + x86))) + + (otherwise ;; can't happen: (< mod 3) + (mv 'mod-can-not-be-anything-other-than-0-1-or-2 0 0 0 x86)))) + + (ix ;; use REX-BYTE prefix (cf. Intel Vol. 2, Table 2-4) + (reg-index (sib->index sib) rex-byte #.*x*)) + + (index (case ix ; Intel Vol. 2, Figure 2-6 + (4 0) ; no index register; "none" in Intel Table 2-3 + (otherwise (if (equal proc-mode #.*64-bit-mode*) + (rgfi ix x86) + (i32 (rgfi ix x86)))))) ; 32-bit signed index + + (scale (the (unsigned-byte 2) (sib->scale sib))) + (scaled-index (ash index scale)) + + (effective-addr (+ base scaled-index))) + + (mv flg effective-addr displacement nrip-bytes x86)) + + /// + + (defthm x86-effective-addr-from-sib-returns-integerp-displacement + (implies (x86p x86) + (integerp + (mv-nth 2 (x86-effective-addr-from-sib + proc-mode temp-RIP rex-byte mod sib x86)))) + :rule-classes (:rewrite :type-prescription) + :hints (("Goal" :in-theory (e/d (rime-size riml-size) ())))) + + (defthm x86-effective-addr-from-sib-returns-<=-increment-rip-bytes + (<= (mv-nth 3 (x86-effective-addr-from-sib + proc-mode temp-RIP rex-byte mod sib x86)) + 4) + :hints (("Goal" :in-theory (e/d (rime-size riml-size) ()))) + :rule-classes :linear)) + + (encapsulate + () + (local (include-book "arithmetic-5/top" :dir :system)) + + (defthm logext-loghead-identity + (implies (signed-byte-p n x) + (equal (logext n (loghead n x)) x)) + :hints (("Goal" :in-theory (e/d (logext loghead logapp logbitp) ()))))) + + (define x86-effective-addr-16-disp + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*) ) + (mod :type (unsigned-byte 2) "mod field of ModR/M byte") + x86) + :guard (2bits-p mod) + :returns (mv flg + (disp i16p + :hyp (x86p x86) + :hints (("Goal" :in-theory (enable rime-size)))) + (increment-rip-by natp) + (x86 x86p :hyp (x86p x86))) + :short "Calculate the displacement for + 16-bit effective address calculation." + :long + "+ This is according to Intel manual, Mar'17, Vol. 2, Table 2-1. +
++ The displacement is absent (i.e. 0) when Mod is 00b. + An exception to this is when R/M is 110b, + in which case there is a 16-bit displacement that is added to the index. + This case is not handled by this function, + but is instead handled in + its caller function @(tsee x86-effective-addr-16). +
++ The displacement is a signed 8-bit value when Mod is 01b. + The displacement is a signed 16-bit value when Mod is 10b. + This function is not called when Mod is 11b. +
++ If an error occurs when trying to read the displacement, + 0 is returned as displacement, + but the caller ignores the returned displacement given the error. +
++ This function is called only when the address size is 16 bits. +
" + (case mod + (0 (mv nil 0 0 x86)) + (1 (b* (((mv flg byte x86) + (rime-size-opt proc-mode 1 temp-rip #.*cs* :x nil x86 :mem-ptr? nil)) + ((when flg) (mv flg 0 0 x86))) + (mv nil byte 1 x86))) + (2 (b* (((mv flg word x86) + (rime-size-opt proc-mode 2 temp-rip #.*cs* :x nil x86 :mem-ptr? nil)) + ((when flg) (mv flg 0 0 x86))) + (mv nil word 2 x86))) + (otherwise ; shouldn't happen + (mv 'mod-value-wrong 0 0 x86))) + /// + + (more-returns + (disp integerp + :hyp (x86p x86) + :name integerp-of-x86-effective-addr-16-disp.disp + :rule-classes :type-prescription)) + + (defthm mv-nth-2-x86-effective-addr-16-disp-<=-4 + (<= (mv-nth 2 (x86-effective-addr-16-disp proc-mode temp-RIP mod x86)) + 4) + :rule-classes :linear)) + + (define x86-effective-addr-16 + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*) ) + (r/m :type (unsigned-byte 3) "r/m field of ModR/M byte") + (mod :type (unsigned-byte 2) "mod field of ModR/M byte") + x86) + :guard (and (2bits-p mod) + (3bits-p r/m)) + :returns (mv flg + (address n16p) + (increment-rip-by natp) + (x86 x86p :hyp (x86p x86))) + :short "Effective address calculation with 16-bit addressing." + :long + "+ This is according to Intel manual, Mar'17, Vol. 2, Table 2-1. +
++ We assume that the additions in the table are modular, + even though the documentation is not clear in that respect. + So we simply apply @('n16') to the exact integer result. + This is in analogy to the use of @('n32') + for effective address calculation in 64-bit mode + when there is an address size override prefix: + see @(tsee x86-effective-addr-32/64). +
" + (case r/m + (0 (b* ((bx (rr16 #.*rbx* x86)) + (si (rr16 #.*rsi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bx si disp)) increment-rip-by x86))) + (1 (b* ((bx (rr16 #.*rbx* x86)) + (di (rr16 #.*rdi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bx di disp)) increment-rip-by x86))) + (2 (b* ((bp (rr16 #.*rbp* x86)) + (si (rr16 #.*rsi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bp si disp)) increment-rip-by x86))) + (3 (b* ((bp (rr16 #.*rbp* x86)) + (di (rr16 #.*rdi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bp di disp)) increment-rip-by x86))) + (4 (b* ((si (rr16 #.*rsi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ si disp)) increment-rip-by x86))) + (5 (b* ((di (rr16 #.*rdi* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ di disp)) increment-rip-by x86))) + (6 (case mod + (0 (b* (((mv flg disp x86) + (rime-size-opt proc-mode 2 temp-rip #.*cs* :x nil x86 + :mem-ptr? nil)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 disp) 2 x86))) + (otherwise (b* ((bp (rr16 #.*rbp* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp + proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bp disp)) increment-rip-by x86))))) + (7 (b* ((bx (rr16 #.*rbx* x86)) + ((mv flg disp increment-rip-by x86) + (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) + ((when flg) (mv flg 0 0 x86))) + (mv nil (n16 (+ bx disp)) increment-rip-by x86))) + (otherwise ; shouldn't happen + (mv :r/m-out-of-range 0 0 x86))) + /// + + (defthm-signed-byte-p i64p-mv-nth-1-x86-effective-addr-16 + :hyp t + :bound 64 + :concl (mv-nth 1 (x86-effective-addr-16 proc-mode temp-RIP r/m mod x86)) + :gen-linear t + :gen-type t) + + (defthm natp-mv-nth-2-x86-effective-addr-16 + (natp (mv-nth 2 (x86-effective-addr-16 proc-mode temp-RIP r/m mod x86))) + :rule-classes :type-prescription) + + (defthm mv-nth-2-x86-effective-addr-16-<=-4 + (<= (mv-nth 2 (x86-effective-addr-16 proc-mode temp-RIP r/m mod x86)) + 4) + :rule-classes :linear)) + + (define x86-effective-addr-32/64 + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + p4 + (temp-RIP :type (signed-byte #.*max-linear-address-size*) ) + (rex-byte :type (unsigned-byte 8) "Rex byte") + (r/m :type (unsigned-byte 3) "r/m field of ModR/M byte") + (mod :type (unsigned-byte 2) "mod field of ModR/M byte") + (sib :type (unsigned-byte 8) "Sib byte") + ;; num-imm-bytes is needed for computing the next RIP when + ;; RIP-relative addressing is done. Note that this argument is + ;; only relevant when the operand addressing mode is I, i.e., + ;; when the operand value is encoded in subsequent bytes of the + ;; instruction. For details, see *Z-addressing-method-info* in + ;; x86isa/utils/decoding-utilities.lisp. + (num-imm-bytes :type (unsigned-byte 3) + "Number of immediate bytes (0, 1, 2, or 4) + that follow the sib (or displacement bytes, if any).") + x86) + + :guard (and (2bits-p mod) + (3bits-p r/m) + (sib-p sib)) + :guard-hints (("Goal" :in-theory (e/d (n64-to-i64 rime-size) ()))) + + ;; Returns the flag, the effective address (taking the SIB and + ;; ModR/M bytes into account) and the number of bytes to increment + ;; the temp-RIP by. + :returns (mv + flg + i64p-memory-address + increment-RIP-by + (x86 x86p :hyp (force (x86p x86)))) + + :short "Effective address calculation with 32-bit and 64-bit addressing." + + :long "Note that we do not add segment bases + (such as the FS and GS bases, if FS and GS overrides are present) + to the effective address computed in this function. + Addition of those segment base addresses is a part of the + segmentation process --- we handle that in the function @(see + ea-to-la) that performs the segment address translation.
+ +Quoting from Intel Vol 1, Sec 3.3.7:
+ +In 64-bit mode, the effective address components are + added and the effective address is truncated (See for example + the instruction LEA) before adding the full 64-bit segment + base. The base is never truncated, regardless of addressing + mode in 64-bit mode.
+ +Quoting Intel Vol. 1 Sec. 3.3.7 (Address Calculations in + 64-Bit Mode):
+ +All 16-bit and 32-bit address calculations are + zero-extended in IA-32e mode to form 64-bit addresses. Address + calculations are first truncated to the effective address size + of the current mode (64-bit mode or compatibility mode), as + overridden by any address-size prefix. The result is then + zero-extended to the full 64-bit address width. Because of + this, 16-bit and 32-bit applications running in compatibility + mode can access only the low 4 GBytes of the 64-bit mode + effective addresses. Likewise, a 32-bit address generated in + 64-bit mode can access only the low 4 GBytes of the 64-bit + mode effective addresses.
+ +Also: Intel Vol 1, Section 3.3.7 says that we need + sign-extended displacements in effective address calculations. In + Lisp, sign-extension is implicit.
+ +In 64-bit mode, instructions such as LEA use this function to + compute the effective address. LEA, at least, does not check + whether the generated address is canonical or not, which is why we + don't make the canonical-address-p check in this function.
+ +In 64-bit mode, + we use @('rgfi-size') to read bases as signed linear addresses, + which encode canonical linear addresses, + which are also effective addresses in 64-bit mode. + In 32-bit mode, + we use @('rr32') to read bases as unsigned effective addresses.
" + + (b* (((mv flg addr displacement increment-RIP-by x86) + + (case mod + + (0 + (case r/m + (4 + ;; Returns + ;; (mv flg non-truncated-memory-address 0 increment-RIP-by x86) + (x86-effective-addr-from-sib + proc-mode temp-RIP rex-byte mod sib x86)) + (5 + (if (equal proc-mode #.*64-bit-mode*) + ;; RIP-relative addressing in 64-bit mode: + (b* (((mv ?flg0 dword x86) + ;; dword is the sign-extended displacement + ;; present in the instruction. + (rime-size-opt + #.*64-bit-mode* 4 temp-RIP #.*cs* :x nil x86 + :mem-ptr? nil)) + ;; next-rip is the rip of the next instruction. + ;; temp-RIP + 4 bytes of the displacement + ;; mentioned above + bytes of rest of the + ;; instruction (immediate bytes) + ((mv flg next-rip) + (add-to-*ip #.*64-bit-mode* temp-RIP + (+ 4 num-imm-bytes) + x86)) + ((when flg) (mv flg 0 0 0 x86))) + (mv flg0 next-rip dword 4 x86)) + ;; displacement only in 32-bit mode: + (b* (((mv flg dword x86) + ;; dword is the sign-extended displacement + ;; present in the instruction. + (rime-size-opt + proc-mode 4 temp-RIP #.*cs* :x nil x86 :mem-ptr? nil)) + ((when flg) (mv flg 0 0 0 x86))) + (mv nil 0 dword 4 x86)))) + + (otherwise + (mv nil + (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index r/m rex-byte #.*b*) x86) + (rr32 r/m x86)) ; rex-byte is 0 in 32-bit mode + 0 + 0 + x86)))) + + (1 + (case r/m + (4 + ;; Returns + ;; (mv flg non-truncated-memory-address 0 increment-RIP-by x86) + (x86-effective-addr-from-sib + proc-mode temp-RIP rex-byte mod sib x86)) + + (otherwise + (b* (((mv ?flg2 byte2 x86) + (rime-size-opt proc-mode 1 temp-RIP #.*cs* :x nil x86 + :mem-ptr? nil)) ; sign-extended + (reg (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index r/m rex-byte #.*b*) x86) + (rr32 r/m x86)))) ; rex-byte is 0 in 32-bit mode + (mv flg2 reg byte2 1 x86))))) + + (2 + (case r/m + (4 + ;; Returns + ;; (mv flg non-truncated-memory-address 0 increment-RIP-by x86) + (x86-effective-addr-from-sib + proc-mode temp-RIP rex-byte mod sib x86)) + + (otherwise + (b* (((mv ?flg1 dword x86) + (rime-size-opt proc-mode 4 temp-RIP #.*cs* :x nil x86 + :mem-ptr? nil)) ; sign-extended + (reg (if (equal proc-mode #.*64-bit-mode*) + (rgfi (reg-index r/m rex-byte #.*b*) x86) + (rr32 r/m x86)))) ; rex-byte is 0 in 32-bit mode + (mv flg1 reg dword 4 x86))))) + + (otherwise ; shouldn't happen + (mv 'mod-value-wrong 0 0 0 x86)))) + + (dst-base (+ addr displacement)) + + ;; In 64-bit mode, if #x67 (address-override prefix) is + ;; present, the effective address size is 32 bits, else it is + ;; 64 bits. Note that our current virtual memory functions can + ;; cause an error if the address we are reading from/writing to + ;; is >= 2^47-8. + ;; In 32-bit mode, + ;; this function we are in is called when the address size is 32 bits + ;; (X86-EFFECTIVE-ADDR-16 is called when the address size is 16 bits), + ;; so the following code always returns + ;; a 32-bit address in 32-bit mode. + (dst-base (if (equal proc-mode #.*64-bit-mode*) + (if p4 + (n32 dst-base) + (n64-to-i64 (n64 dst-base))) + (n32 dst-base)))) + + (mv flg dst-base increment-RIP-by x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + + (defthm-signed-byte-p i64p-mv-nth-1-x86-effective-addr-32/64 + :hyp t + :bound 64 + :concl (mv-nth 1 (x86-effective-addr-32/64 + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86)) + :gen-linear t + :gen-type t) + + (defthm natp-mv-nth-2-x86-effective-addr-32/64 + (natp (mv-nth 2 (x86-effective-addr-32/64 + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86))) + :rule-classes :type-prescription) + + (defthm mv-nth-2-x86-effective-addr-32/64-<=-4 + (<= (mv-nth 2 (x86-effective-addr-32/64 + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86)) + 4) + :rule-classes :linear)) + + (define x86-effective-addr + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + p4 + (temp-RIP :type (signed-byte #.*max-linear-address-size*) ) + (rex-byte :type (unsigned-byte 8) "Rex byte") + (r/m :type (unsigned-byte 3) "r/m field of ModR/M byte") + (mod :type (unsigned-byte 2) "mod field of ModR/M byte") + (sib :type (unsigned-byte 8) "Sib byte") + ;; num-imm-bytes is needed for computing the next RIP when + ;; RIP-relative addressing is done. Note that this argument is + ;; only relevant when the operand addressing mode is I, i.e., + ;; when the operand value is encoded in subsequent bytes of the + ;; instruction. For details, see *Z-addressing-method-info* in + ;; x86isa/utils/decoding-utilities.lisp. + (num-imm-bytes :type (unsigned-byte 3) + "Number of immediate bytes (0, 1, 2, or 4) + that follow the sib (or displacement bytes, if any).") + x86) + + + :guard (and (2bits-p mod) + (3bits-p r/m) + (sib-p sib)) + + ;; Returns the flag, the effective address (taking the SIB and + ;; ModR/M bytes into account) and the number of bytes to increment + ;; the temp-RIP by. + :returns (mv + flg + i64p-memory-address + increment-RIP-by + (x86 x86p :hyp (force (x86p x86)))) + + :short "Effective address calculation." + + :long + "+ This is a wrapper that calls + @(tsee x86-effective-addr-16) or @(tsee x86-effective-addr-32/64) + based on the address size. +
" + + (if (eql 2 (select-address-size proc-mode (if p4 t nil) x86)) + (x86-effective-addr-16 proc-mode temp-rip r/m mod x86) + (x86-effective-addr-32/64 + proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) + + /// + + (defthm-signed-byte-p i64p-mv-nth-1-x86-effective-addr + :hyp t + :bound 64 + :concl (mv-nth 1 (x86-effective-addr + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86)) + :gen-linear t + :gen-type t) + + (defthm natp-mv-nth-2-x86-effective-addr + (natp (mv-nth 2 (x86-effective-addr + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86))) + :rule-classes :type-prescription) + + (defthm mv-nth-2-x86-effective-addr-<=-4 + (<= (mv-nth 2 (x86-effective-addr + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86)) + 4) + :rule-classes :linear) + + (defthm signed-byte-p-of-mv-nth-2-of-x86-effective-addr + (implies (and (<= 48 size) + (integerp size)) + (signed-byte-p size (mv-nth 2 (x86-effective-addr + proc-mode p4 temp-RIP rex-byte r/m mod sib + num-imm-bytes x86)))) + :hints (("Goal" :in-theory (disable x86-effective-addr)))) + + (defruled x86-effective-addr-when-64-bit-modep + (equal (x86-effective-addr + #.*64-bit-mode* + p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86) + (x86-effective-addr-32/64 + #.*64-bit-mode* + p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))))) + +;; ====================================================================== + +(local + (defthm-unsigned-byte-p usb-80-of-16-and-64 + :hyp (and (unsigned-byte-p 16 n16) + (unsigned-byte-p 64 n64)) + :bound 80 + :concl (logior n16 (ash n64 16)) + :gen-type t + :gen-linear t)) + +(local + (defthm-unsigned-byte-p usb-48-of-16-and-32 + :hyp (and (unsigned-byte-p 16 n16) + (unsigned-byte-p 32 n32)) + :bound 48 + :concl (logior n16 (ash n32 16)) + :gen-type t + :gen-linear t)) + +(defsection read-operands-and-write-results + + :parents (decoding-and-spec-utils) + + :short "Functions to fetch and read operands from an instruction, + and to write results to appropriate registers/memory locations, + based on ModR/M, SIB, immediate, and/or displacement bytes." + + (local (xdoc::set-default-parents read-operands-and-write-results)) + + (define alignment-checking-enabled-p (x86) + :prepwork ((local (in-theory (e/d* () ())))) + :returns (enabled booleanp :rule-classes :type-prescription) + :short "Checking if alignment is enabled" + :long "Source: Intel Manuals, Volume 3, Section 6.15, Exception + and Interrupt Reference:
+ +Description: Indicates that the processor detected an + unaligned memory operand when alignment checking was + enabled. Alignment checks are only carried out in data (or stack) + accesses (not in code fetches or system segment accesses). An example + of an alignment-check violation is a word stored at an odd byte + address, or a doubleword stored at an address that is not an integer + multiple of 4.+ +
Note that the alignment check exception (#AC) is + generated only for data types that must be aligned on word, + doubleword, and quadword boundaries. A general-protection + exception (#GP) is generated 128-bit data types that are not aligned + on a 16-byte boundary.+ +
To enable alignment checking, the following conditions + must be true:+
Alignment-check exceptions (#AC) are generated only when +operating at privilege level 3 (user mode). Memory references that +default to privilege level 0, such as segment descriptor loads, do not +generate alignment-check exceptions, even when caused by a memory +reference made from privilege level 3." + + (b* ((cr0 (the (unsigned-byte 32) (n32 (ctri #.*cr0* x86)))) + (AM (cr0Bits->am cr0)) + (AC (flgi :ac x86)) + (CPL (cpl x86))) + (and (equal AM 1) + (equal AC 1) + (equal CPL 3))) + + /// + + (defthm alignment-checking-enabled-p-and-xw + (implies (and (not (equal fld :ctr)) + (not (equal fld :seg-visible)) + (not (equal fld :rflags))) + (equal (alignment-checking-enabled-p (xw fld index val x86)) + (alignment-checking-enabled-p x86)))) + + (defthm alignment-checking-enabled-p-and-xw-ctr + (implies (case-split (or (not (equal (nfix index) *cr0*)) + (and (equal (nfix index) *cr0*) + (equal (cr0Bits->am (loghead 32 val)) + (cr0Bits->am (xr :ctr *cr0* x86)))))) + (equal (alignment-checking-enabled-p (xw :ctr index val x86)) + (alignment-checking-enabled-p x86))) + :hints (("Goal" :in-theory (e/d (cr0bits->am cr0bits-fix) + ())))) + + (defthm alignment-checking-enabled-p-and-xw-rflags + (implies (equal (rflagsBits->ac val) + (rflagsBits->ac (xr :rflags nil x86))) + (equal (alignment-checking-enabled-p (xw :rflags nil val x86)) + (alignment-checking-enabled-p x86))) + :hints (("Goal" :in-theory (e/d (rflagsBits->ac rflagsBits-fix) ())))) + + (defthm alignment-checking-enabled-p-and-xw-seg-visible + (implies (case-split + (or (not (equal index #.*cs*)) + (and (equal index #.*cs*) + (equal (segment-selectorBits->rpl val) + (segment-selectorBits->rpl + (seg-visiblei #.*cs* x86)))))) + (equal (alignment-checking-enabled-p (xw :seg-visible index val x86)) + (alignment-checking-enabled-p x86))) + :hints (("Goal" :in-theory (e/d (segment-selectorbits->rpl segment-selectorbits-fix) + ())))) + + (defthm alignment-checking-enabled-p-and-mv-nth-1-wb + (equal (alignment-checking-enabled-p (mv-nth 1 (wb n addr w val x86))) + (alignment-checking-enabled-p x86)) + :hints (("Goal" :in-theory (e/d* (wb write-to-physical-memory) ())))) + + (defthm alignment-checking-enabled-p-and-mv-nth-2-rb + (equal (alignment-checking-enabled-p (mv-nth 2 (rb n addr r-x x86))) + (alignment-checking-enabled-p x86)) + :hints (("Goal" :in-theory (e/d* (rb) ())))) + + (defthm alignment-checking-enabled-p-and-mv-nth-2-las-to-pas + (equal (alignment-checking-enabled-p + (mv-nth 2 (las-to-pas n lin-addr r-w-x x86))) + (alignment-checking-enabled-p x86)))) + + (define x86-operand-from-modr/m-and-sib-bytes + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (reg-type :type (member #.*gpr-access* + #.*xmm-access* + #.*vex-xmm-access* + #.*ymm-access* + #.*mmx-access*) + "@('reg-type') is @('*gpr-access*') for GPRs, + @('*xmm-access*') for XMMs (non-VEX-encoded), + @('*vex-xmm-access*') for XMMs (VEX-encoded), + @('*ymm-access*') for YMMs, and + @('*mmx-access*) or MMXs. + (@('*zmm-access*'), for ZMMs, is not yet supported.") + (operand-size :type (member 1 2 4 6 8 10 16 32)) + (inst-ac? booleanp + "@('t') if instruction does alignment checking, + @('nil') otherwise.") + (memory-ptr? booleanp + "@('t') if the operand is a memory operand of the + form m16:16, m16:32, or m16:64") + (seg-reg (integer-range-p 0 *segment-register-names-len* seg-reg) + "Register of the segment to read the operand from + (when reading the operand from memory).") + (p4? :type (or t nil) + "Address-Size Override Prefix Present?") + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + (rex-byte :type (unsigned-byte 8)) + (r/m :type (unsigned-byte 3)) + (mod :type (unsigned-byte 2)) + (sib :type (unsigned-byte 8)) + (num-imm-bytes :type (unsigned-byte 3)) + x86) + + :guard (if (equal mod #b11) + (case reg-type + (#.*gpr-access* (member operand-size '(1 2 4 8))) + (#.*xmm-access* (member operand-size '(4 8 16))) + (#.*vex-xmm-access* (member operand-size '(4 8 16))) + (#.*ymm-access* (member operand-size '(4 8 16 32))) + (#.*mmx-access* (equal operand-size 8)) + (t t)) + (member operand-size '(1 2 4 6 8 10 16 32))) + + ;; [Shilpi] enabled segment-base-and-bounds for rme-size-opt. + :guard-hints (("Goal" :in-theory (e/d* (segment-base-and-bounds) + (las-to-pas + unsigned-byte-p + signed-byte-p)))) + + :returns + (mv flg + (operand natp :rule-classes :type-prescription) + increment-RIP-by + addr + (x86 x86p :hyp (force (x86p x86)))) + + :short "Read an operand from memory or a register." + + :long + "
+ Based on the ModR/M byte, + the operand is read from either a register or memory. + In the latter case, we calculate the effective address + and then we read the operand from it. + Besides returning the operand, + we also return the calculated effective address. + This is useful for instructions that modify the operand after reading it + (e.g. the source/destination operand of ADD), + which pass the effective address calculated by this function + to @(tsee x86-operand-to-reg/mem) (which writes the result to memory). +
" + + (b* (((mv ?flg0 + (the (signed-byte 64) addr) + (the (integer 0 4) increment-RIP-by) + x86) + (if (equal mod #b11) + (mv nil 0 0 x86) + (x86-effective-addr + proc-mode p4? temp-rip rex-byte r/m mod sib num-imm-bytes x86))) + ((when flg0) + (mv (cons 'x86-effective-addr-error flg0) 0 0 0 x86)) + ((mv ?flg2 operand x86) + (if (equal mod #b11) + (case reg-type + (#.*gpr-access* + (mv nil + (rgfi-size operand-size + (reg-index r/m rex-byte #.*b*) + rex-byte + x86) + x86)) + (#.*mmx-access* + (mv nil + (mmx r/m x86) ; only MM0-MM7 exist + x86)) + (#.*xmm-access* + (mv nil + (xmmi-size operand-size + (reg-index r/m rex-byte #.*b*) + x86) + x86)) + (t + (mv nil + (zmmi-size operand-size + (reg-index r/m rex-byte #.*b*) + x86) + x86))) + (b* ((check-alignment? (and inst-ac? + (alignment-checking-enabled-p x86)))) + ;; The operand is being fetched from the memory, not the + ;; instruction stream. That's why we have :r instead of :x + ;; below. + (rme-size-opt + proc-mode operand-size addr seg-reg :r check-alignment? x86 + :mem-ptr? memory-ptr? + :check-canonicity t)))) + ((when flg2) + (mv (cons 'Rm-Size-Error flg2) 0 0 0 x86))) + + (mv nil operand increment-RIP-by addr x86)) + + /// + + (defthm-unsigned-byte-p bound-of-mv-nth-1-x86-operand-from-modr/m-and-sib-bytes-operand + :hyp (and (equal bound (ash operand-size 3)) + (member operand-size '(1 2 4 8 16 32)) + (or (not (equal reg-type #.*mmx-access*)) + (equal operand-size 8)) + (x86p x86)) + :bound bound + :concl (mv-nth 1 (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4? temp-RIP rex-byte r/m mod sib num-imm-bytes + x86)) + :gen-linear t + :gen-type t + :hints (("Goal" :in-theory (e/d (rme-size) ())))) + + (defthm-unsigned-byte-p bigger-bound-of-mv-nth-1-x86-operand-from-modr/m-and-sib-bytes-operand + :hyp (and (<= (ash operand-size 3) bound) + (member operand-size '(1 2 4 8 16 32)) + (or (not (equal reg-type #.*mmx-access*)) + (equal operand-size 8)) + (integerp bound) + (x86p x86)) + :bound bound + :concl (mv-nth 1 (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4? temp-RIP rex-byte r/m mod sib num-imm-bytes + x86)) + :hints (("Goal" :in-theory (e/d () + (x86-operand-from-modr/m-and-sib-bytes + unsigned-byte-p))))) + + (in-theory + (disable + bigger-bound-of-mv-nth-1-x86-operand-from-modr/m-and-sib-bytes-operand)) + + (defthm-unsigned-byte-p bound-of-mv-nth-1-x86-operand-from-modr/m-and-sib-bytes-operand-6-and-10-bytes-read + :hyp (and (equal bound (ash operand-size 3)) + (member operand-size '(6 10)) + (not (equal mod #b11)) + (x86p x86)) + :bound bound + :concl (mv-nth 1 (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4? temp-RIP rex-byte r/m mod sib num-imm-bytes + x86)) + :gen-linear t + :gen-type t + :hints (("Goal" :in-theory (enable rme-size)))) + + (defthm integerp-x86-operand-from-modr/m-and-sib-bytes-increment-RIP-by-type-prescription + (implies (force (x86p x86)) + (natp (mv-nth + 2 + (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type + operand-size inst-ac? memory-ptr? seg-reg p4 temp-RIP + rex-byte r/m mod sib num-imm-bytes x86)))) + :hints (("Goal" :in-theory (e/d (rml-size) ()))) + :rule-classes :type-prescription) + + (defthm mv-nth-2-x86-operand-from-modr/m-and-sib-bytes-increment-RIP-by-linear<=4 + (implies (x86p x86) + (<= (mv-nth + 2 + (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4 temp-RIP rex-byte r/m mod sib num-imm-bytes x86)) + 4)) + :hints (("Goal" :in-theory (e/d (rml-size) ()))) + :rule-classes :linear) + + (defthm-signed-byte-p i48p-x86-operand-from-modr/m-and-sib-bytes + ;; For guard proofs obligations originating from calls to add-to-*ip: + :hyp (forced-and (x86p x86)) + :bound 48 + :concl (mv-nth 2 (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) + :hints (("Goal" :in-theory (e/d (signed-byte-p) ()))) + :gen-linear t + :gen-type t) + + (defthm-signed-byte-p i64p-x86-operand-from-modr/m-and-sib-bytes + :hyp (forced-and (x86p x86)) + :bound 64 + :concl (mv-nth 3 (x86-operand-from-modr/m-and-sib-bytes + proc-mode reg-type operand-size inst-ac? memory-ptr? + seg-reg p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) + :gen-linear t + :gen-type t)) + + (define x86-operand-to-reg/mem + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (operand-size :type (member 1 2 4 6 8 10 16)) + (inst-ac? booleanp + "@('t') if instruction does alignment checking, + @('nil') otherwise") + (memory-ptr? booleanp + "@('t') if the operand is a memory operand + of the form m16:16, m16:32, or m16:64") + (operand :type (integer 0 *)) + (seg-reg (integer-range-p 0 *segment-register-names-len* seg-reg) + "Register of the segment to read the operand from + (when reading the operand from memory).") + (addr :type (signed-byte 64)) + (rex-byte :type (unsigned-byte 8)) + (r/m :type (unsigned-byte 3)) + (mod :type (unsigned-byte 2)) + x86) + + :short "Write an operand to memory or a general-purpose register." + + :long + "+ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @(tsee x86-operand-from-modr/m-and-sib-bytes). +
" + + :guard (and (unsigned-byte-p (ash operand-size 3) operand) + (if (equal mod #b11) + (member operand-size '(1 2 4 8)) + (member operand-size '(1 2 4 6 8 10 16)))) + + (b* (((when (equal mod #b11)) + (let* ((x86 (!rgfi-size operand-size (reg-index r/m rex-byte #.*b*) + operand rex-byte x86))) + (mv nil x86))) + + (check-alignment? (and inst-ac? (alignment-checking-enabled-p x86))) + ((mv flg x86) + (wme-size + proc-mode operand-size addr seg-reg operand check-alignment? x86 + :mem-ptr? memory-ptr?))) + (mv flg x86)) + + /// + + (defthm x86p-x86-operand-to-reg/mem + (implies (force (x86p x86)) + (x86p + (mv-nth + 1 + (x86-operand-to-reg/mem + proc-mode operand-size inst-ac? + memory-ptr? operand addr seg-reg rex-byte r/m mod x86)))) + :hints (("Goal" :in-theory (e/d () (force (force))))))) + + (define x86-operand-to-mmx/mem + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (inst-ac? booleanp + "@('t') if instruction does alignment checking, + @('nil') otherwise") + (operand :type (unsigned-byte 64)) + (seg-reg (integer-range-p 0 *segment-register-names-len* seg-reg) + "Register of the segment to read the operand from + (when reading the operand from memory).") + (addr :type (signed-byte 64)) + (r/m :type (unsigned-byte 3)) + (mod :type (unsigned-byte 2)) + x86) + + :short "Write an operand to memory or an MMX register." + + :long + "+ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @(tsee x86-operand-from-modr/m-and-sib-bytes). +
++ After writing an MMX register, we update floating-point status: + see @(see mmx-registers-reads-and-writes). +
" + + (b* (((when (equal mod #b11)) + (let* ((x86 (!mmx r/m operand x86)) + (x86 (mmx-instruction-updates x86))) + (mv nil x86))) + + (check-alignment? (and inst-ac? (alignment-checking-enabled-p x86))) + + ((mv flg x86) + (wme-size proc-mode + 8 + addr + seg-reg + operand + check-alignment? + x86 + :mem-ptr? nil))) ; operand is never a memory pointer here + (mv flg x86)) + + /// + + (defthm x86p-x86-operand-to-mmx/mem + (implies (force (x86p x86)) + (x86p (mv-nth 1 (x86-operand-to-mmx/mem proc-mode + inst-ac? + operand + seg-reg + addr + r/m + mod + x86)))) + :hints (("Goal" :in-theory (e/d () (force (force))))))) + + (define x86-operand-to-xmm/mem + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (operand-size :type (member 4 8 16)) + (inst-ac? booleanp + "@('t') if instruction does alignment checking, + @('nil') otherwise") + (operand :type (integer 0 *)) + (seg-reg (integer-range-p 0 *segment-register-names-len* seg-reg) + "Register of the segment to read the operand from + (when reading the operand from memory).") + (addr :type (signed-byte 64)) + (rex-byte :type (unsigned-byte 8)) + (r/m :type (unsigned-byte 3)) + (mod :type (unsigned-byte 2)) + x86) + + :short "Write an operand to memory or an XMM register." + + :long + "+ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @(tsee x86-operand-from-modr/m-and-sib-bytes). +
++ The writing to an XMM register is for a non-VEX-encoded instruction. +
" + + :guard (unsigned-byte-p (ash operand-size 3) operand) + + (b* (((when (equal mod #b11)) + (let* ((x86 (!xmmi-size operand-size (reg-index r/m rex-byte #.*b*) + operand x86))) + (mv nil x86))) + + (check-alignment? (and inst-ac? (alignment-checking-enabled-p x86))) + + ((mv flg x86) + ;; operand is never an m16:16 memory pointer here + (wme-size proc-mode operand-size addr seg-reg operand + check-alignment? x86 :mem-ptr? nil))) + (mv flg x86)) + + /// + + (defthm x86p-x86-operand-to-xmm/mem + (implies (force (x86p x86)) + (x86p + (mv-nth + 1 + (x86-operand-to-xmm/mem + proc-mode operand-size inst-ac? operand + seg-reg addr rex-byte r/m mod x86)))) + :hints (("Goal" :in-theory (e/d () (force (force))))))) + + (define x86-operand-to-zmm/mem + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (reg-type :type (member #.*vex-xmm-access* + #.*ymm-access* + #.*zmm-access*)) + (operand-size :type (member 4 8 16 32)) + (inst-ac? booleanp + "@('t') if instruction does alignment checking, + @('nil') otherwise") + (operand :type (integer 0 *)) + (seg-reg (integer-range-p 0 *segment-register-names-len* seg-reg) + "Register of the segment to read the operand from + (when reading the operand from memory).") + (addr :type (signed-byte 64)) + (rex-byte :type (unsigned-byte 8)) + (r/m :type (unsigned-byte 3)) + (mod :type (unsigned-byte 2)) + x86) + + :guard (and (unsigned-byte-p (ash operand-size 3) operand) + (if (equal mod #b11) + (case reg-type + (#.*vex-xmm-access* (member operand-size '(4 8 16))) + (#.*ymm-access* (member operand-size '(4 8 16 32))) + (t t)) + (member operand-size '(4 8 16 32)))) + + :short "Write an operand to memory or a ZMM register." + + :long + "+ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @(tsee x86-operand-from-modr/m-and-sib-bytes). +
++ This covers writing to ZMM or YMM or XMM registers, + the latter for VEX instructions. + The 512-bit (i.e. 64-byte) writes are not supported yet, + because we first need to add appropriate memory writing functions. +
" + + (b* (((when (equal mod #b11)) + (let* ((x86 (!zmmi-size operand-size (reg-index r/m rex-byte #.*b*) + operand x86 :regtype reg-type))) + (mv nil x86))) + + (check-alignment? (and inst-ac? (alignment-checking-enabled-p x86))) + + ((mv flg x86) + ;; operand is never an m16:16 memory pointer here + (wme-size proc-mode operand-size addr seg-reg operand + check-alignment? x86 :mem-ptr? nil))) + (mv flg x86)) + + /// + + (defthm x86p-x86-operand-to-zmm/mem + (implies (force (x86p x86)) + (x86p + (mv-nth + 1 + (x86-operand-to-zmm/mem + proc-mode reg-type operand-size inst-ac? operand + seg-reg addr rex-byte r/m mod x86)))) + :hints (("Goal" :in-theory (e/d () (force (force)))))))) + +;; ====================================================================== + +(define rip-guard-okp + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (rip :type (signed-byte #.*max-linear-address-size*))) + :short "Size constraints on a memory address of some instruction byte" + + :long "This function specifies the maximum size of a memory address that + points to some instruction byte in a certain mode of operation. We usually + use this function to specify the guard on @('start-rip') or @('temp-rip') --- + that way, we can put in suitable type declarations in our specification + functions.
+ +This function doesn't really specify whether a memory address that points + to some instruction byte is valid or not --- that notion is much more + complicated; see @(see canonical-address-p), @(see ea-to-la), and @(see + instruction-pointer-operations) for details.
" + :parents (decoding-and-spec-utils) + :enabled t + (if (equal proc-mode #.*64-bit-mode*) + (signed-byte-p 48 rip) + (unsigned-byte-p 32 rip)) + + /// + + (defthm rip-guard-okp-equiv-for-non-64-bit-modes + (implies (and (rip-guard-okp proc-mode-1 rip) + (not (equal proc-mode-1 0)) + (not (equal proc-mode-2 0))) + (rip-guard-okp proc-mode-2 rip))) + + (defthm rip-guard-okp-for-64-bit-modep + (equal (rip-guard-okp #.*64-bit-mode* rip) + (signed-byte-p #.*max-linear-address-size* rip))) + + (defthm rip-guard-okp-for-compatibility-mode + (equal (rip-guard-okp #.*compatibility-mode* rip) + (unsigned-byte-p 32 rip)))) + +;; ====================================================================== + +(defmacro def-inst + (name &key + (operation 'nil) + (sp/dp 'nil) + (dp-to-sp 'nil) + (high/low 'nil) + (trunc 'nil) + (vex 'nil) + (evex 'nil) + (modr/m 'nil) + (additional-args 'nil) + body parents short long + inline enabled guard-debug guard + guard-hints (verify-guards 't) prepwork thms + returns) + + (if body + `(define ,name + (,@additional-args + ,@(and operation `((operation :type (integer 0 38)))) + ,@(and sp/dp `((sp/dp :type (integer 0 1)))) + ,@(and dp-to-sp `((dp-to-sp :type (integer 0 1)))) + ,@(and high/low `((high/low :type (integer 0 1)))) + ,@(and trunc `((trunc booleanp))) + (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*)) + (prefixes :type (unsigned-byte #.*prefixes-width*)) + (rex-byte :type (unsigned-byte 8)) + ,@(if vex + `((vex-prefixes :type (unsigned-byte 24))) + `()) + ,@(if evex + `((evex-prefixes :type (unsigned-byte 32))) + `()) + (opcode :type (unsigned-byte 8)) + (modr/m :type (unsigned-byte 8)) + (sib :type (unsigned-byte 8)) + x86 + ;; If operation = -1, ignore this field. Note that -1 is + ;; the default value of operation. + ;; &optional ((operation :type (integer -1 8)) '-1 ) + ) + + (declare + (ignorable proc-mode start-rip temp-rip prefixes rex-byte + opcode modr/m sib)) + + ,@(and parents `(:parents ,parents)) + ,@(and short `(:short ,short)) + ,@(and long `(:long ,long)) + + ,@(and prepwork `(:prepwork ,prepwork)) + + :guard (and (prefixes-p prefixes) + (ModR/M-p modr/m) + (sib-p sib) + (rip-guard-okp proc-mode temp-rip) + ,@(and guard `(,guard))) + + ,@(and enabled `(:enabled ,enabled)) + ,@(and inline `(:inline ,inline :no-function t)) + ,@(and (not verify-guards) `(:verify-guards ,verify-guards)) + ,@(and guard-debug `(:guard-debug ,guard-debug)) + ,@(and guard-hints `(:guard-hints ,guard-hints)) + + ,@(and returns `(:returns ,returns)) + + (b* ((?ctx ',name) + ,@(and modr/m + '((?r/m (the (unsigned-byte 3) (modr/m->r/m modr/m))) + (?mod (the (unsigned-byte 2) (modr/m->mod modr/m))) + (?reg (the (unsigned-byte 3) (modr/m->reg modr/m)))))) + ,body) + + /// + + (add-to-ruleset instruction-decoding-and-spec-rules + '(,name)) + + ,@(and thms + `(,@thms))) + + nil)) + +;; All the instruction decoding and spec functions will be added to +;; this ruleset automatically if def-inst is used to define these +;; functions. +(def-ruleset instruction-decoding-and-spec-rules nil) + +;; ====================================================================== + +(define x86-step-unimplemented (message x86) + :parents (instructions) + :short "Semantic function corresponding to Intel's instructions unsupported + in the @('x86isa') books" + :long "Note that the @('ms') field is populated with @('message') here + because this function is called when a model-related error occurs.
" + :returns (x86 x86p :hyp (x86p x86)) + (b* ((ctx 'x86-step-unimplemented)) + (!!ms-fresh :message message))) + +(define x86-illegal-instruction + (message + (start-rip :type (signed-byte #.*max-linear-address-size*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + x86) + :parents (instructions) + :short "Semantic function corresponding to opcodes that Intel deems to be + illegal or reserved" + :long "Note that the @('#UD') (undefined operation) exception should be + thrown here, which is why the @('fault') field is populated with + @('message').
" + :returns (x86 x86p :hyp (x86p x86)) + (b* ((ctx 'x86-illegal-instruction) + ;; We update the RIP to point to the next instruction --- in case we + ;; ever get to the point that we can recover from #UD exceptions, this + ;; may be the right thing to do. + (x86 (!rip temp-rip x86))) + (!!fault-fresh :ud message :instruction-address start-rip))) + +(define x86-general-protection + (message + (start-rip :type (signed-byte #.*max-linear-address-size*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + x86) + :parents (instructions) + :short "Semantic function corresponding to a general protection fault" + :long "Note that the @('#GP') (general protection) exception should be + thrown here, which is why the @('fault') field is populated with + @('message').
" + :returns (x86 x86p :hyp (x86p x86)) + (b* ((ctx 'x86-general-protection) + ;; We update the RIP to point to the next instruction --- in case we + ;; ever get to the point that we can recover from #GP exceptions, this + ;; may be the right thing to do. + (x86 (!rip temp-rip x86))) + (!!fault-fresh :gp message :instruction-address start-rip))) + +(define x86-device-not-available + (message + (start-rip :type (signed-byte #.*max-linear-address-size*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + x86) + :parents (instructions) + :short "Semantic function corresponding to a device not available (no math coprocessor) fault" + :long "Note that the @('#NM') (device not available) exception should be + thrown here, which is why the @('fault') field is populated with + @('message').
" + :returns (x86 x86p :hyp (x86p x86)) + (b* ((ctx 'x86-device-not-available) + ;; We update the RIP to point to the next instruction --- in case we + ;; ever get to the point that we can recover from #NM exceptions, this + ;; may be the right thing to do. + (x86 (!rip temp-rip x86))) + (!!fault-fresh :nm message :instruction-address start-rip))) + +(add-to-ruleset instruction-decoding-and-spec-rules + '(x86-step-unimplemented + x86-illegal-instruction + x86-general-protection + x86-device-not-available)) + +;; ====================================================================== + +(define select-operand-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (byte-operand? :type (or t nil)) + (rex-byte :type (unsigned-byte 8)) + (imm? :type (or t nil)) + (prefixes :type (unsigned-byte #.*prefixes-width*)) + (default64? :type (or t nil)) + (ignore-rex? :type (or t nil)) + (ignore-p3-64? :type (or t nil)) + (x86 x86p)) + + :inline t + :no-function t + :parents (decoding-and-spec-utils) + :guard (prefixes-p prefixes) + + :short "Selecting the operand size for general-purpose instructions" + + :long "@('select-operand-size') selects the operand size of the + instruction. It is cognizant of the instruction prefixes, the + @('rex') byte, the operand type (e.g., immediate operand or not), + and the default operand size (obtained from the state).
+ +This function was written by referring to the following: +
+
+
+
+ The first image above has been captured from Volume 1: Basic Architecture,
+ Intel\(R\) 64 and IA-32 Architectures Software Developer's Manual,
+ Order Number: 253665-062US, March 2017.
This function also includes three additional boolean parameters that serve + to accommodate instructions that do not quite follow the general rules + specified by the table above:
+ ++ If there is a segment register override prefix, + the prefix determines the segment register, + according to Intel manual, Mar'17, Volume 2, Section 2.1.1. +
++ Otherwise, we use the default segment selection rules + in Intel manual, May'18, Volume 1, Table 3-5. + Since we only call this function for instruction operands, + the CS rule does not apply. + The ES rule applies to string instructions, + but our model does not use this function + to determine the ES segment for string instructions + (which cannot be overridden, + at least for the string instructions we currently support), + so this function does not take the ES rule into account either. + So the result is either SS or DS, + based on whether the base register is one of rSP and rBP or not: + this determination is made based on + Intel manual, May'18, Volume 2, Table 2-1 if the address size is 16 bits, + and Intel manual, May'18, Volume 2, Table 2-2 otherwise. + However, when Mod is not 11b and R/M is 100b, + the notation [--][--] in Table 2-2 indicates the use of a SIB byte: + according to Intel manual, May'18, Volume 2, Table 2-3, + when the Base field of the SIB byte is 100b, + the base register is rSP, + and thus in this case the default segment register is SS. +
++ Note that here we may recalculate the address size + even if that has already been calculated as part of + the decoding of the instruction whose operand we are accessing. + Thus, it may be possible to optimize the overall code. +
" + (case p2 + (#x2E #.*cs*) + (#x36 #.*ss*) + (#x3E #.*ds*) + (#x26 #.*es*) + (#x64 #.*fs*) + (#x65 #.*gs*) + (t (b* ((addr-size (select-address-size proc-mode p4? x86))) + (if (= addr-size 2) + (if (and (not (= mod 3)) + (or (= r/m 2) (= r/m 3))) + #.*ss* + #.*ds*) + (if (or (and (or (= mod 1) (= mod 2)) + (= r/m 5)) + (and (not (= mod 3)) + (= r/m 4) + (= (sib->base sib) 4))) + #.*ss* + #.*ds*))))) + /// + + (defret range-of-select-segment-register + (and (<= 0 seg-reg) + (< seg-reg #.*segment-register-names-len*)) + :rule-classes :linear)) + +;; ====================================================================== + +(define check-instruction-length + ((start-rip :type (signed-byte #.*max-linear-address-size*)) + (temp-rip :type (signed-byte #.*max-linear-address-size*)) + (delta-rip :type (unsigned-byte 3))) + :returns (badlength? acl2::maybe-natp + :hints (("Goal" :in-theory (enable acl2::maybe-natp)))) + :inline t + :no-function t + :parents (decoding-and-spec-utils) + :short "Check if the length of an instruction exceeds 15 bytes." + :long + "+ The maximum length of an instruction is 15 bytes; + a longer instruction causes a #GP(0) exception. + See AMD manual, Dec'17, Volume 2, Table 8-6. + This function is used to check this condition. +
++ The @('start-rip') argument is + the instruction pointer at the beginning of the instruction. + The @('temp-rip') argument is generally + the instruction pointer just past the end of the instruction, + in which case the @('delta-rip') argument is 0. + In the other cases, @('delta-rip') is a small non-zero number, + and @('temp-rip + delta-rip') is + the instruction pointer just past the end of the instruction. +
++ This function returns @('nil') if the length does not exceed 15 bytes. + Otherwise, this function returns the offending length (a number above 15), + which is useful for error reporting in the model. +
" + (b* ((start-rip (mbe :logic (ifix start-rip) :exec start-rip)) + (temp-rip (mbe :logic (ifix temp-rip) :exec temp-rip)) + (delta-rip (mbe :logic (nfix delta-rip) :exec delta-rip)) + ((the (signed-byte #.*max-linear-address-size+1*) end-rip) + (+ (the (signed-byte #.*max-linear-address-size*) temp-rip) + (the (unsigned-byte 3) delta-rip))) + ((the (signed-byte #.*max-linear-address-size+2*) length) + (- (the (signed-byte #.*max-linear-address-size+1*) end-rip) + (the (signed-byte #.*max-linear-address-size*) start-rip)))) + (and (> length 15) + length))) + +;; ====================================================================== + +(define rex-byte-from-vex-prefixes ((vex-prefixes vex-prefixes-p)) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (rex-byte (unsigned-byte-p 8 rex-byte)) + :parents (decoding-and-spec-utils) + :short "Calculate a REX byte from a VEX prefix." + :long + "+ The VEX prefix (see Intel manual Volume 2 Section 2.3.5 of Dec 2023) + provides an encoding of the REX byte. + Since some of the functions in our model depend on the REX byte + (e.g. @(tsee x86-operand-from-modr/m-and-sib-bytes)), + here we introduce a function to build a REX byte + from its encoding in the VEX prefix. + We do that for both the 2-byte form and the 3-byte form of the VEX prefix; + see @(tsee vex->r), @(tsee vex->x), @(tsee vex->b), and @(tsee vex->w). + See Figure 2-9 in Intel manual Volume 2 of Dec 2023. + Note that the R, X, B bits are encoded negated, + while the W is encoded directly (not negated). +
" + (b* ((vex.w (vex->w vex-prefixes)) + (vex.r (vex->r vex-prefixes)) + (vex.x (vex->x vex-prefixes)) + (vex.b (vex->b vex-prefixes)) + (rex-byte (+ #x40 ; 40h-4Fh + (if (= vex.w 0) 0 #b1000) + (if (= vex.r 1) 0 #b0100) + (if (= vex.x 1) 0 #b0010) + (if (= vex.b 1) 0 #b0001)))) + rex-byte) + /// + + (defthm-unsigned-byte-p unsigned-byte-p-of-rex-byte-from-vex-prefixes + :bound 8 + :concl (rex-byte-from-vex-prefixes vex-prefixes) + :gen-type t + :gen-linear t)) diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/dispatch-creator.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/dispatch-creator.lisp new file mode 100644 index 000000000..b21a0e773 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/dispatch-creator.lisp @@ -0,0 +1,503 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2019, 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 GoelThe function @('get-prefixes') fetches the legacy and REX prefixes + of an instruction and also returns the first byte following the last such + prefix. The input @('start-rip') points to the first byte of an instruction, + which may potentially be a legacy prefix. The initial value of @('cnt') + should be @('15') so that the result @('(- 15 cnt)') returned at the end of + the recursion is the correct number of legacy and/or REX bytes parsed by this + function.
+ +From Intel Manual, Vol. 2, May 2018, Section 2.1.1 (Instruction + Prefixes):
+ +Instruction prefixes are divided into four groups, each with a set of + allowable prefix codes. For each instruction, it is only useful to include + up to one prefix code from each of the four groups (Groups 1, 2, 3, + 4). Groups 1 through 4 may be placed in any order relative to each + other.
+ +Despite the quote from the Intel Manual above, the order of the legacy + prefixes does matter when there is more than one prefix from the same group + --- all but the last prefix from a single prefix group are ignored. + The only exception in this case is for Group 1 prefixes --- see + below for details.
+ +Examples:
+ +We now discuss the Group 1 exception below.
+ +@('0xf0_f2_a4') is NOT
+ @('repne movsb byte ptr [rdi], byte ptr [rsi]')
+ It is:
+ @('lock repne movsb byte ptr [rdi], byte ptr [rsi]')
+
+ Note that lock and rep/repne are Group 1 prefixes. It is important to record
+ the lock prefix, even if it is overshadowed by a rep/repne prefix, because
+ the former instruction will not @('#UD'), but the latter instruction will.
+ This is akin to the lock prefix being in a separate group than the rep/repne
+ prefixes; in fact, AMD manuals (Section 1.2.1: Summary of Legacy Prefixes,
+ Vol. 3 May 2018 Edition) treat them as such.
For details about how mandatory prefixes are picked from legacy prefixes, + see @(see mandatory-prefixes-computation).
+ +A REX prefix (applicable only to 64-bit mode) is treated as a null prefix + if it is followed by a legacy prefix. Here is an illustrative example (using + Intel's XED, x86 Encoder Decoder --- see + @('https://intelxed.github.io/')):
+ +Note that the prefixes structure output of this function does not include + the REX byte (which is a separate return value of this function), but its + @(':num-prefixes') field includes a count of the REX prefixes encountered. + This is because adding an 8-bit field to the prefixes structure to store a + REX byte will make it a bignum, thereby impacting execution efficiency.
" + + :prepwork + + ((defthm return-type-of-prefixes->num-linear + (< (prefixes->num prefixes) 16) + :hints (("Goal" :in-theory (e/d (prefixes->num) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->lck-linear + (< (prefixes->lck prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->lck) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->rep-linear + (< (prefixes->rep prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->rep) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->seg-linear + (< (prefixes->seg prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->seg) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->opr-linear + (< (prefixes->opr prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->opr) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->adr-linear + (< (prefixes->adr prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->adr) ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->nxt-linear + (< (prefixes->nxt prefixes) 256) + :hints (("Goal" :in-theory (e/d (prefixes->nxt prefixes-fix) + ()))) + :rule-classes :linear) + + (defthm return-type-of-prefixes->num-rewrite + (unsigned-byte-p 4 (prefixes->num prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->num) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->lck-rewrite + (unsigned-byte-p 8 (prefixes->lck prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->lck) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->rep-rewrite + (unsigned-byte-p 8 (prefixes->rep prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->rep) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->seg-rewrite + (unsigned-byte-p 8 (prefixes->seg prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->seg) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->opr-rewrite + (unsigned-byte-p 8 (prefixes->opr prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->opr) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->adr-rewrite + (unsigned-byte-p 8 (prefixes->adr prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->adr) ()))) + :rule-classes :rewrite) + + (defthm return-type-of-prefixes->nxt-rewrite + (unsigned-byte-p 8 (prefixes->nxt prefixes)) + :hints (("Goal" :in-theory (e/d (prefixes->nxt prefixes-fix) + ()))) + :rule-classes :rewrite) + + (defthm return-type-of-!prefixes->*-linear + (and (unsigned-byte-p 52 (!prefixes->num x prefixes)) + (unsigned-byte-p 52 (!prefixes->lck x prefixes)) + (unsigned-byte-p 52 (!prefixes->rep x prefixes)) + (unsigned-byte-p 52 (!prefixes->seg x prefixes)) + (unsigned-byte-p 52 (!prefixes->opr x prefixes)) + (unsigned-byte-p 52 (!prefixes->adr x prefixes)) + (unsigned-byte-p 52 (!prefixes->nxt x prefixes))) + :hints (("Goal" :in-theory + (e/d* (!prefixes->num + !prefixes->lck + !prefixes->rep + !prefixes->seg + !prefixes->opr + !prefixes->adr + !prefixes->nxt + prefixes-fix + 4bits-fix + 8bits-fix) + (unsigned-byte-p + bitops::logand-with-negated-bitmask)))) + :rule-classes + ((:rewrite) + (:linear :corollary + (and (< (!prefixes->num x prefixes) #.*2^52*) + (< (!prefixes->lck x prefixes) #.*2^52*) + (< (!prefixes->rep x prefixes) #.*2^52*) + (< (!prefixes->seg x prefixes) #.*2^52*) + (< (!prefixes->opr x prefixes) #.*2^52*) + (< (!prefixes->adr x prefixes) #.*2^52*) + (< (!prefixes->nxt x prefixes) #.*2^52*))))) + + (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) + ())))) + + (local + (defthm signed-byte-p-48-lemma + (implies (signed-byte-p 48 start-rip) + (equal (signed-byte-p 48 (1+ start-rip)) + (< (1+ start-rip) *2^47*))))) + + (local (in-theory (e/d () (unsigned-byte-p))))) + + + (if (mbe :logic (zp cnt) + :exec (eql cnt 0)) + ;; Error, too many prefix bytes --- invalid instruction length. + (mv t prefixes rex-byte x86) + + (b* ((ctx 'get-prefixes) + ((mv flg (the (unsigned-byte 8) byte) x86) + (rme08-opt proc-mode start-rip #.*cs* :x x86)) + ((when flg) + (mv (cons ctx flg) byte rex-byte x86)) + + (prefix-byte-group-code + (the (integer 0 4) (get-one-byte-prefix-array-code byte)))) + + (case prefix-byte-group-code + + (0 + (b* ((rex? (and + (eql proc-mode #.*64-bit-mode*) + (equal (the (unsigned-byte 4) (ash byte -4)) 4))) + ((when rex?) + (mv-let + (flg next-rip) + (add-to-*ip proc-mode start-rip 1 x86) + (if flg + (mv flg prefixes rex-byte x86) + (get-prefixes + proc-mode next-rip prefixes + byte ;; REX prefix, overwriting a previously encountered REX, + ;; if any + (the (integer 0 15) (1- cnt)) + x86))))) + ;; Storing the number of prefixes seen and the first byte + ;; following the prefixes in "prefixes": + (let ((prefixes + (the (unsigned-byte #.*prefixes-width*) + (!prefixes->nxt byte prefixes)))) + (mv nil + (the (unsigned-byte #.*prefixes-width*) + (!prefixes->num (- 15 cnt) prefixes)) + rex-byte ;; Preserving rex-byte + x86)))) + + (1 + ;; LOCK (F0), REPE (F3), REPNE (F2) + (b* (((mv flg next-rip) + (add-to-*ip proc-mode start-rip 1 x86)) + ((when flg) + (mv flg prefixes rex-byte x86)) + ((the (unsigned-byte #.*prefixes-width*) prefixes) + (if (equal byte #.*lock*) + (!prefixes->lck byte prefixes) + (!prefixes->rep byte prefixes)))) + ;; Storing the group 1 prefix (possibly overwriting a + ;; previously seen group 1 prefix) and going on... + (get-prefixes + proc-mode next-rip prefixes + 0 ;; Nullify a previously read REX prefix, if any + (the (integer 0 15) (1- cnt)) x86))) + + (2 + ;; ES (26), CS (2E), SS (36), DS (3E), FS (64), GS (65) + (b* (((mv flg next-rip) + (add-to-*ip proc-mode start-rip 1 x86)) + ((when flg) + (mv flg prefixes rex-byte x86))) + + (if (or + ;; In 64-bit mode, all segment override prefixes except FS + ;; and GS overrides are treated as null prefixes. So a + ;; segment override prefix other than the FS and GS overrides + ;; cannot overshadow a FS/GS override. In case two or more + ;; FS/GS overrides are present, all but the last are ignored. + + ;; Source: XED (https://intelxed.github.io/) + ;; Some tests: + ;; xed -64 -d 260f0100 => sgdt ptr [rax] + ;; xed -64 -d 640f0100 => sgdt ptr fs:[rax] + ;; xed -64 -d 64260f0100 => sgdt ptr fs:[rax] + ;; xed -64 -d 6426650f0100 => sgdt ptr gs:[rax] + (and (eql proc-mode #.*64-bit-mode*) + (or (equal byte #.*fs-override*) + (equal byte #.*gs-override*))) + ;; All segment overrides are active in the 32-bit mode, and + ;; all but the last one are ignored. + (not (eql proc-mode #.*64-bit-mode*))) + + ;; Storing the group 2 prefix (possibly overwriting a + ;; previously seen group 2 prefix) and going on... + + (get-prefixes + proc-mode next-rip + (the (unsigned-byte #.*prefixes-width*) + (!prefixes->seg byte prefixes)) + 0 ;; Nullify a previously read REX prefix, if any + (the (integer 0 15) (1- cnt)) + x86) + + ;; We will be here if we are in the 64-bit mode and have seen a + ;; null segment override prefix; we will not store the prefix + ;; but simply decrement cnt. + (get-prefixes proc-mode next-rip prefixes + 0 ;; Nullify a previously read REX prefix, if any + (the (integer 0 15) (1- cnt)) x86)))) + + (3 + ;; Operand-Size Override (66) + (mv-let + (flg next-rip) + (add-to-*ip proc-mode start-rip 1 x86) + (if flg + (mv flg prefixes rex-byte x86) + ;; Storing the group 3 prefix (possibly overwriting a + ;; previously seen group 3 prefix) and going on... + (get-prefixes + proc-mode next-rip + (the (unsigned-byte #.*prefixes-width*) + (!prefixes->opr byte prefixes)) + 0 ;; Nullify a previously read REX prefix, if any + (the (integer 0 15) (1- cnt)) + x86)))) + + (4 + ;; Address-Size Override (67) + (mv-let + (flg next-rip) + (add-to-*ip proc-mode start-rip 1 x86) + (if flg + (mv flg prefixes rex-byte x86) + ;; Storing the group 4 prefix (possibly overwriting a + ;; previously seen group 4 prefix) and going on... + (get-prefixes + proc-mode next-rip + (the (unsigned-byte #.*prefixes-width*) + (!prefixes->adr byte prefixes)) + 0 ;; Nullify a previously read REX prefix, if any + (the (integer 0 15) (1- cnt)) + x86)))) + + (otherwise + (mv t prefixes rex-byte x86))))) + + /// + + (local (in-theory (e/d () (acl2::zp-open not)))) + + (defthm-unsigned-byte-p prefixes-width-p-of-get-prefixes.new-prefixes + ;; [Shilpi] I tried to use defret here instead of defthm-unsigned-byte-p, but I got + ;; into trouble, probably because of the different order of lambda + ;; expansions in defret. + :hyp (and (unsigned-byte-p #.*prefixes-width* prefixes) + (canonical-address-p start-rip) + (x86p x86)) + :bound #.*prefixes-width* + :concl (mv-nth 1 (get-prefixes + proc-mode start-rip prefixes rex-byte cnt x86)) + :hints (("Goal" + :induct (get-prefixes + proc-mode start-rip prefixes rex-byte cnt x86) + :in-theory (e/d () + (signed-byte-p + acl2::ash-0 + acl2::zip-open + bitops::logtail-of-logior + unsigned-byte-p-of-logtail + acl2::logtail-identity + bitops::logand-with-negated-bitmask + (:linear bitops::logior-<-0-linear-1) + (:linear bitops::logior-<-0-linear-2) + (:linear bitops::logand->=-0-linear-1) + (:linear bitops::logand->=-0-linear-2) + bitops::logtail-natp + natp-of-get-one-byte-prefix-array-code + acl2::ifix-when-not-integerp + ;; bitops::basic-signed-byte-p-of-+ + default-<-1 + force (force))))) + :gen-linear t) + + (defthm-unsigned-byte-p byte-p-of-get-prefixes.new-rex-byte + ;; [Shilpi] I tried to use defret here instead of defthm-unsigned-byte-p, but I got + ;; into trouble, probably because of the different order of lambda + ;; expansions in defret. + :hyp (and (unsigned-byte-p 8 rex-byte) + (natp prefixes) + (x86p x86)) + :bound 8 + :concl (mv-nth 2 (get-prefixes + proc-mode start-rip prefixes rex-byte cnt x86)) + :hints (("Goal" + :induct (get-prefixes + proc-mode start-rip prefixes rex-byte cnt x86) + :in-theory (e/d () + (unsigned-byte-p + signed-byte-p + acl2::ash-0 + acl2::zip-open + bitops::logtail-of-logior + unsigned-byte-p-of-logtail + acl2::logtail-identity + bitops::logand-with-negated-bitmask + (:linear bitops::logior-<-0-linear-1) + (:linear bitops::logior-<-0-linear-2) + (:linear bitops::logand->=-0-linear-1) + (:linear bitops::logand->=-0-linear-2) + bitops::logtail-natp + natp-of-get-one-byte-prefix-array-code + acl2::ifix-when-not-integerp + ;; bitops::basic-signed-byte-p-of-+ + default-<-1 + force (force))))) + :gen-linear t) + + (defret get-prefixes-does-not-modify-x86-state-in-app-view + (implies (app-view x86) + (equal new-x86 x86)) + :hints (("Goal" + :in-theory + (union-theories + '(get-prefixes + rme08-does-not-affect-state-in-app-view) + (theory 'minimal-theory))))) + + (local + (in-theory (e/d + (rme08 rml08 rvm08) + (force + (force) + signed-byte-p-48-lemma + signed-byte-p + bitops::logior-equal-0 + acl2::zp-open + not + (:congruence acl2::int-equiv-implies-equal-logand-2) + (:congruence acl2::int-equiv-implies-equal-loghead-2))))) + + + (defthm num-prefixes-get-prefixes-bound + (implies (and (<= cnt 15) + (x86p x86) + (canonical-address-p start-rip) + (unsigned-byte-p #.*prefixes-width* prefixes) + (< (part-select prefixes :low 0 :high 2) 5)) + (<= + (prefixes->num + (mv-nth + 1 + (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86))) + 15)) + :hints (("Goal" + :induct (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + :in-theory (e/d (rme08 rme08-value-when-error) + (signed-byte-p + unsigned-byte-p rme08 rml08 + (force) force + canonical-address-p + not acl2::zp-open + acl2::ash-0 + acl2::zip-open + bitops::logtail-of-logior + unsigned-byte-p-of-logtail + acl2::logtail-identity + bitops::logand-with-negated-bitmask + (:linear bitops::logior-<-0-linear-1) + (:linear bitops::logior-<-0-linear-2) + (:linear bitops::logand->=-0-linear-1) + (:linear bitops::logand->=-0-linear-2) + bitops::logtail-natp + natp-of-get-one-byte-prefix-array-code + acl2::ifix-when-not-integerp + ;; bitops::basic-signed-byte-p-of-+ + default-<-1)))) + :rule-classes :linear) + + (defthm get-prefixes-opener-lemma-zero-cnt + (implies (zp cnt) + (equal (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (mv t prefixes rex-byte x86))) + :hints (("Goal" :in-theory (e/d (get-prefixes) ())))) + + (local + (defthmd get-prefixes-opener-lemma-no-prefix-byte-pre + ;; Note that this lemma is applicable in the system-level view too. + ;; This lemma would be used for those instructions which do not have + ;; any prefix byte. + (b* (((mv flg byte byte-x86) + (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code + (get-one-byte-prefix-array-code byte))) + (implies + (and (not flg) + (zp prefix-byte-group-code) + (not (zp cnt))) + (equal + (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (b* ((rex? (and + (eql proc-mode #.*64-bit-mode*) + (equal (ash byte -4) 4))) + ((when rex?) + (mv-let + (flg next-rip) + (add-to-*ip proc-mode start-rip 1 byte-x86) + (if flg + (mv flg prefixes rex-byte byte-x86) + (get-prefixes + proc-mode next-rip prefixes + byte ;; REX prefix, overwriting a previously encountered REX, + ;; if any + (the (integer 0 15) (1- cnt)) + byte-x86))))) + ;; Storing the number of prefixes seen and the first byte + ;; following the prefixes in "prefixes": + (let ((prefixes + (!prefixes->nxt byte prefixes))) + (mv nil + (!prefixes->num (- 15 cnt) prefixes) + rex-byte ;; Preserving rex-byte + byte-x86)))))) + :hints (("Goal" + :induct + (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + :in-theory (e/d () (unsigned-byte-p)))))) + + (defthm get-prefixes-opener-lemma-no-prefix-byte + ;; This lemma is applicable in all the views of the x86isa model. This + ;; lemma would be used for those instructions which do not have any prefix + ;; byte --- either legacy or rex. + (implies + (b* (((mv flg byte &) + (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code + (get-one-byte-prefix-array-code byte))) + (and (not flg) + (zp prefix-byte-group-code) + (if (equal proc-mode #.*64-bit-mode*) + (not (equal (ash byte -4) 4)) + t) + (not (zp cnt)))) + (equal + (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (let ((prefixes + (!prefixes->nxt + (mv-nth 1 (rme08 proc-mode start-rip #.*cs* :x x86)) + prefixes))) + (mv nil + (!prefixes->num (- 15 cnt) prefixes) + rex-byte ;; Preserving rex-byte + (mv-nth 2 (rme08 proc-mode start-rip #.*cs* :x x86)))))) + :hints (("Goal" :in-theory (e/d (get-prefixes-opener-lemma-no-prefix-byte-pre) + (rme08 get-prefixes))))) + + (defthm get-prefixes-opener-lemma-no-legacy-prefix-but-rex-prefix + ;; Note that this lemma is applicable only in 64-bit mode. + ;; This lemma is applicable in all the views of the x86isa model. + (implies + (b* (((mv flg byte &) + (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code + (get-one-byte-prefix-array-code byte))) + (and (equal proc-mode #.*64-bit-mode*) + (not flg) + (zp prefix-byte-group-code) + (equal (ash byte -4) 4) + (not (zp cnt)))) + (equal + (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (b* (((mv & byte byte-x86) + (rme08 proc-mode start-rip #.*cs* :x x86)) + ((mv flg next-rip) + (add-to-*ip proc-mode start-rip 1 byte-x86))) + (if flg + (mv flg prefixes rex-byte byte-x86) + (get-prefixes + proc-mode next-rip prefixes + byte ;; REX prefix, overwriting a previously encountered REX, + ;; if any + (1- cnt) + byte-x86))))) + :hints (("Goal" :in-theory (e/d (get-prefixes-opener-lemma-no-prefix-byte-pre) + (rme08 get-prefixes))))) + + (defthm get-prefixes-opener-lemma-group-1-prefix + (b* (((mv flg byte byte-x86) + (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code (get-one-byte-prefix-array-code byte))) + (implies + (and (or (app-view x86) + (not (marking-view x86))) + (not flg) ;; No error in reading a byte + (equal prefix-byte-group-code 1) + (not (zp cnt)) + (not (mv-nth 0 (add-to-*ip proc-mode start-rip 1 x86)))) + (equal (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (let ((prefixes + (if (equal byte #.*lock*) + (!prefixes->lck byte prefixes) + (!prefixes->rep byte prefixes)))) + (get-prefixes + proc-mode (1+ start-rip) prefixes 0 (1- cnt) byte-x86))))) + :hints (("Goal" + :in-theory + (e/d* (add-to-*ip) + (rb + unsigned-byte-p + negative-logand-to-positive-logand-with-integerp-x))))) + + (defthm get-prefixes-opener-lemma-group-2-prefix + (b* (((mv flg byte byte-x86) + (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code + (get-one-byte-prefix-array-code + byte))) + (implies + (and (or (app-view x86) + (and (not (app-view x86)) + (not (marking-view x86)))) + (not flg) ;; No error in reading a byte + (equal prefix-byte-group-code 2) + (not (zp cnt)) + (not (mv-nth 0 (add-to-*ip proc-mode start-rip 1 x86)))) + (equal (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (if (or + (and (eql proc-mode #.*64-bit-mode*) + (or (equal byte #.*fs-override*) + (equal byte #.*gs-override*))) + (not (eql proc-mode #.*64-bit-mode*))) + (get-prefixes proc-mode (1+ start-rip) + (!prefixes->seg byte prefixes) + 0 + (1- cnt) byte-x86) + (get-prefixes + proc-mode (1+ start-rip) prefixes 0 (1- cnt) byte-x86))))) + :hints (("Goal" + :in-theory + (e/d* (add-to-*ip) + (rb + unsigned-byte-p + negative-logand-to-positive-logand-with-integerp-x))))) + + (defthm get-prefixes-opener-lemma-group-3-prefix + (b* (((mv flg byte x862) (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code (get-one-byte-prefix-array-code byte))) + (implies (and (not flg) ;; No error in reading a byte + (equal prefix-byte-group-code 3) + (not (zp cnt)) + (not (mv-nth 0 (add-to-*ip proc-mode start-rip 1 x862)))) + (equal (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (get-prefixes proc-mode (1+ start-rip) + (!prefixes->opr byte prefixes) + 0 + (1- cnt) x862)))) + :hints (("Goal" + :in-theory + (e/d* (add-to-*ip) + (rb + unsigned-byte-p + negative-logand-to-positive-logand-with-integerp-x))))) + + (defthm get-prefixes-opener-lemma-group-4-prefix + (b* (((mv flg byte x862) (rme08 proc-mode start-rip #.*cs* :x x86)) + (prefix-byte-group-code (get-one-byte-prefix-array-code byte))) + (implies (and (not flg) ;; No error in reading a byte + (equal prefix-byte-group-code 4) + (not (zp cnt)) + (not (mv-nth 0 (add-to-*ip proc-mode start-rip 1 x862)))) + (equal (get-prefixes proc-mode start-rip prefixes rex-byte cnt x86) + (get-prefixes proc-mode (1+ start-rip) + (!prefixes->adr + byte + prefixes) + 0 + (1- cnt) x862)))) + :hints (("Goal" + :in-theory + (e/d* (add-to-*ip) + (rb + unsigned-byte-p + negative-logand-to-positive-logand-with-integerp-x))))) + + (local + (defret xr-msr-and-seg-hidden-of-get-prefixes-in-app-view + (implies (app-view x86) + (and + (equal (xr :msr idx new-x86) + (xr :msr idx x86)) + (equal (xr :seg-hidden-base idx new-x86) + (xr :seg-hidden-base idx x86)) + (equal (xr :seg-hidden-limit idx new-x86) + (xr :seg-hidden-limit idx x86)) + (equal (xr :seg-hidden-attr idx new-x86) + (xr :seg-hidden-attr idx x86)))) + :hints (("Goal" + :in-theory (e/d () (las-to-pas rb rme08 rml08)))))) + + (local + (defret xr-msr-of-get-prefixes-in-sys-view + (implies (not (app-view x86)) + (and + (equal (xr :msr idx new-x86) + (xr :msr idx x86)) + (equal (xr :seg-hidden-base idx new-x86) + (xr :seg-hidden-base idx x86)) + (equal (xr :seg-hidden-limit idx new-x86) + (xr :seg-hidden-limit idx x86)) + (equal (xr :seg-hidden-attr idx new-x86) + (xr :seg-hidden-attr idx x86)))) + :hints (("Goal" + :inductThese instructions are:
+where @('E') is the destination operand and @('G') is the source + operand. Note that @('E') stands for a general-purpose register or + memory operand specified by the @('ModRM.r/m') field, and @('G') + stands for a general-purpose register specified by the + @('ModRM.reg') field.
+ + \[OP R/M, REG\] Flags Affectedwhere @('G') is the destination operand and @('E') is the source + operand. Note that @('G') stands for a general-purpose register + specified by the @('ModRM.reg') field, and @('E') stands for a + general-purpose register or memory operand specified by the + @('ModRM.r/m') field.
+ +These instructions are essentially variations of ADC with different flag + behavior. ADCX sums the two operands and the CF flag (just like ADC), then + sets the CF flag according to unsigned overflow (just like ADC), but doesn't + modify any other flags. ADOX is the same as ADCX but uses/sets OF in place of + CF.
" + + + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d* () + (unsigned-byte-p + signed-byte-p))))) + + :modr/m t + + :additional-args ((adcx booleanp)) + + :body + + (b* ((p2 (prefixes->seg prefixes)) + (p4? (eql #.*addr-size-override* (prefixes->adr prefixes))) + (operand-size (if (logbitp *w* rex-byte) 8 4)) + + (G (rgfi-size operand-size + (the (unsigned-byte 4) + (reg-index reg rex-byte #.*r*)) + rex-byte x86)) + + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + (inst-ac? t) + ((mv flg0 + E + (the (unsigned-byte 3) increment-RIP-by) + (the (signed-byte 64) E-addr) + x86) + (x86-operand-from-modr/m-and-sib-bytes proc-mode #.*gpr-access* + operand-size + inst-ac? + nil ;; Not a memory pointer operand + seg-reg + p4? + temp-rip + rex-byte + r/m + mod + sib + 0 ;; No immediate operand + x86)) + ((when flg0) + (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg0)) + + ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip)) + (add-to-*ip proc-mode temp-rip increment-RIP-by x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + + ;; Computing the flags and the result: + ((the (unsigned-byte 32) input-rflags) (rflags x86)) + ((mv result + (the (unsigned-byte 32) output-rflags) + (the (unsigned-byte 32) undefined-flags)) + (case operand-size + (8 (gpr-adcx/adox-spec-8 adcx G E input-rflags)) + (t (gpr-adcx/adox-spec-4 adcx G E input-rflags)))) + + ;; Updating the x86 state with the result and eflags. + (x86 + (!rgfi-size operand-size (reg-index reg rex-byte #.*r*) result + rex-byte x86)) + + (x86 (write-user-rflags output-rflags undefined-flags x86)) + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) + + +(def-inst x86-add/adc/sub/sbb/or/and/xor/cmp-G-E + + :parents (one-byte-opcodes) + + :short "Operand Fetch and Execute for ADD, ADC, SUB, SBB, OR, AND, + XOR, CMP: Addressing Mode = \(G, E\)" + + :long "where @('G') is the destination operand and @('E') is the source + operand. Note that @('G') stands for a general-purpose register + specified by the @('ModRM.reg') field, and @('E') stands for a + general-purpose register or memory operand specified by the + @('ModRM.r/m') field.
+ + \[OP REG, R/M\] Flags Affectedwhere @('E') is the destination operand and @('I') is the source + operand. Note that @('E') stands for a general-purpose register or + memory operand specified by the @('ModRM.r/m') field, and @('I') + stands for immediate data. All opcodes except those of TEST fall + under Group 1, and have opcode extensions (ModR/M.reg field), as + per Table A-6 of the Intel Manuals, Vol. 2. The opcodes for TEST + fall under Unary Group 3, and also have opcode extensions.
+ + \[OP R/M, IMM\] Flags Affectedwhere @('rAX') is the destination operand and @('I') is the + source operand. Note that @('rAX') stands for AL/AX/EAX/RAX, + depending on the operand size, and @('I') stands for immediate + data.
+ + \[OP rAX, IMM\] Flags AffectedThis function checks whether the operands are SNaN or + QNaN. If at least one of them is NaN or both of them are zeros, then + it returns the second operand. It also handles infinities.
+ +Return values: (mv flag sign exp implicit frac + invalid).
" + (cond + ((or (eq kind1 'snan) (eq kind1 'qnan) (eq kind1 'indef) + (eq kind2 'snan) (eq kind2 'qnan) (eq kind2 'indef)) + (mv t sign2 exp2 implicit2 frac2 t)) + ((and (eq kind1 'zero) (eq kind2 'zero)) + (mv t sign2 exp2 implicit2 frac2 nil)) + ((eq kind1 'inf) + (if (or (and (int= operation #.*OP-MAX*) (int= sign1 0)) + (and (int= operation #.*OP-MIN*) (int= sign1 1))) + (mv t sign1 exp1 implicit1 frac1 nil) + (mv t sign2 exp2 implicit2 frac2 nil))) + ((eq kind2 'inf) + (if (or (and (int= operation #.*OP-MAX*) (int= sign2 0)) + (and (int= operation #.*OP-MIN*) (int= sign2 1))) + (mv t sign2 exp2 implicit2 frac2 nil) + (mv t sign1 exp1 implicit1 frac1 nil))) + (t (mv nil 0 0 0 0 nil))) + /// + + (defthm integerp-sse-max/min-special-1 + (implies (and (integerp sign1) (integerp sign2)) + (integerp (mv-nth 1 (sse-max/min-special + kind1 sign1 exp1 implicit1 frac1 + kind2 sign2 exp2 implicit2 frac2 + operation)))) + :rule-classes :type-prescription) + + (defthm integerp-sse-max/min-special-2 + (implies (and (integerp exp1) (integerp exp2)) + (integerp (mv-nth 2 (sse-max/min-special + kind1 sign1 exp1 implicit1 frac1 + kind2 sign2 exp2 implicit2 frac2 + operation)))) + :rule-classes :type-prescription) + + (defthm integerp-sse-max/min-special-3 + (implies (and (integerp implicit1) (integerp implicit2)) + (integerp (mv-nth 3 (sse-max/min-special + kind1 sign1 exp1 implicit1 frac1 + kind2 sign2 exp2 implicit2 frac2 + operation)))) + :rule-classes :type-prescription) + + (defthm integerp-sse-max/min-special-4 + (implies (and (integerp frac1) (integerp frac2)) + (integerp (mv-nth 4 (sse-max/min-special + kind1 sign1 exp1 implicit1 frac1 + kind2 sign2 exp2 implicit2 frac2 + operation)))) + :rule-classes :type-prescription)) + +(define sse-max/min-sign (rat rat1 sign1 sign2) + :guard (and (rationalp rat) + (rationalp rat1) + (integerp sign1) + (integerp sign2)) + :inline t + :no-function t + (if (eql rat rat1) sign1 sign2) + /// + + (defthm integerp-sse-max/min-sign + (implies (forced-and (integerp sign1) + (integerp sign2)) + (integerp (sse-max/min-sign rat rat1 sign1 sign2))) + :rule-classes :type-prescription)) + +(define sse-max/min ((operation :type (integer 0 38)) + (op1 natp) + (op2 natp) + (mxcsr :type (unsigned-byte 32)) + (exp-width posp) + (frac-width posp)) + + (b* ((mxcsr (mbe :logic (loghead 32 mxcsr) + :exec mxcsr)) + ((mv kind1 sign1 exp1 implicit1 frac1) + (fp-decode op1 exp-width frac-width)) + ((mv kind2 sign2 exp2 implicit2 frac2) + (fp-decode op2 exp-width frac-width)) + (daz (logbitp #.*mxcsr-daz* mxcsr)) + ((mv kind1 exp1 frac1) + (sse-daz kind1 exp1 frac1 daz)) + ((mv kind2 exp2 frac2) + (sse-daz kind2 exp2 frac2 daz)) + ((mv special-ok sign exp & frac invalid) + (sse-max/min-special kind1 sign1 exp1 implicit1 frac1 + kind2 sign2 exp2 implicit2 frac2 + operation)) + + ;; Check invalid operation + (mxcsr (if invalid (!mxcsrBits->ie 1 mxcsr) mxcsr)) + (im (logbitp #.*mxcsr-im* mxcsr)) + ((when (and invalid (not im))) + (mv 'invalid-operand-exception-is-not-masked 0 mxcsr)) + + ;; Check denormal operand + (de (denormal-exception kind1 kind2)) + (mxcsr (if de (!mxcsrBits->de 1 mxcsr) mxcsr)) + (dm (logbitp #.*mxcsr-dm* mxcsr)) + ((when (and de (not dm))) + (mv 'denormal-operand-exception-is-not-masked 0 mxcsr))) + + (if special-ok + (mv nil + (fp-encode-integer sign exp frac exp-width frac-width) + mxcsr) + (b* ((bias (nfix (ec-call (RTL::bias (list nil (1+ frac-width) exp-width))))) + (rat1 (fp-to-rat sign1 exp1 frac1 bias exp-width frac-width)) + (rat2 (fp-to-rat sign2 exp2 frac2 bias exp-width frac-width)) + (rat (case operation + (#.*OP-MAX* (if (> rat1 rat2) rat1 rat2)) + (#.*OP-MIN* (if (< rat1 rat2) rat1 rat2)) + ;; Should never be reached. + (otherwise 0))) + + (sign (sse-max/min-sign rat rat1 sign1 sign2)) + + (fp-result + (rat-to-fp rat sign + nil nil nil 0 ;; rc will not be used here. + ;; I just put a dummy value 0. + exp-width frac-width))) + + (mv nil fp-result mxcsr)))) + /// + + (defthm integerp-result-sse-max/min + (integerp + (mv-nth 1 (sse-max/min operation op1 op2 mxcsr exp-width frac-width))) + :rule-classes :type-prescription) + + (defthm-unsigned-byte-p n32p-mxcsr-sse-max/min + :bound 32 + :concl (mv-nth 2 (sse-max/min operation op1 op2 mxcsr exp-width frac-width)) + :hints (("Goal" :in-theory (e/d* (unsigned-byte-p-when-mxcsrbits-p) (unsigned-byte-p)))) + :gen-type t + :gen-linear t)) + +;; Single-Precision Operations: + +(define sp-sse-max/min ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 32)) + (op2 :type (unsigned-byte 32)) + (mxcsr :type (unsigned-byte 32))) + (b* (((mv flg result mxcsr) + (sse-max/min operation op1 op2 mxcsr + #.*IEEE-SP-EXP-WIDTH* #.*IEEE-SP-FRAC-WIDTH*)) + (result (n32 result)) + (mxcsr (mbe :logic (n32 mxcsr) + :exec mxcsr))) + (mv flg result mxcsr)) + /// + + (defthm-unsigned-byte-p n32p-result-sp-sse-max/min + :bound 32 + :concl (mv-nth 1 (sp-sse-max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-sp-sse-max/min + :bound 32 + :concl (mv-nth 2 (sp-sse-max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t)) + +;; Double-Precision Operations: + +(define dp-sse-max/min ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 64)) + (op2 :type (unsigned-byte 64)) + (mxcsr :type (unsigned-byte 32))) + (b* (((mv flg result mxcsr) + (sse-max/min operation op1 op2 mxcsr + #.*IEEE-DP-EXP-WIDTH* #.*IEEE-DP-FRAC-WIDTH*)) + (result (n64 result)) + (mxcsr (mbe :logic (n32 mxcsr) + :exec mxcsr))) + (mv flg result mxcsr)) + /// + + (defthm-unsigned-byte-p n64p-result-dp-sse-max/min + :bound 64 + :concl (mv-nth 1 (dp-sse-max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-dp-sse-max/min + :bound 32 + :concl (mv-nth 2 (dp-sse-max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t)) + +;; ====================================================================== + +;; Top-level Single and Double Precision Arithmetic Operations: + +(define sp-sse-add/sub/mul/div/max/min ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 32)) + (op2 :type (unsigned-byte 32)) + (mxcsr :type (unsigned-byte 32))) + (if (or (int= operation #.*OP-MAX*) (int= operation #.*OP-MIN*)) + (sp-sse-max/min operation op1 op2 mxcsr) + (sp-sse-add/sub/mul/div operation op1 op2 mxcsr)) + + /// + + (defthm-unsigned-byte-p n32p-result-sp-sse-add/sub/mul/div/max/min + :bound 32 + :concl (mv-nth 1 (sp-sse-add/sub/mul/div/max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-sp-sse-add/sub/mul/div/max/min + :bound 32 + :concl (mv-nth 2 (sp-sse-add/sub/mul/div/max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t)) + +(define dp-sse-add/sub/mul/div/max/min ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 64)) + (op2 :type (unsigned-byte 64)) + (mxcsr :type (unsigned-byte 32))) + (if (or (int= operation #.*OP-MAX*) (int= operation #.*OP-MIN*)) + (dp-sse-max/min operation op1 op2 mxcsr) + (dp-sse-add/sub/mul/div operation op1 op2 mxcsr)) + + /// + + (defthm-unsigned-byte-p n64p-result-dp-sse-add/sub/mul/div/max/min + :bound 64 + :concl (mv-nth 1 (dp-sse-add/sub/mul/div/max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-dp-sse-add/sub/mul/div/max/min + :bound 32 + :concl (mv-nth 2 (dp-sse-add/sub/mul/div/max/min operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t)) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/arithmetic.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/arithmetic.lisp new file mode 100644 index 000000000..0a5678cb5 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/arithmetic.lisp @@ -0,0 +1,721 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2018, Kestrel Technology, LLC +; 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): +; Cuong ChauThis function returns: +
This function checks whether operands are NaN and then + returns the corresponding results. It also handles the + infinities.
+Return values: (mv flag integer-result invalid)
" + (let ((invalid (or (eq kind1 'snan) + (eq kind2 'snan) + (and (or (eq kind1 'qnan) (eq kind1 'indef) + (eq kind2 'qnan) (eq kind2 'indef)) + (or (int= operation #.*OP-CMPLT*) + (int= operation #.*OP-CMPLE*) + (int= operation #.*OP-CMPNLT*) + (int= operation #.*OP-CMPNLE*) + (int= operation #.*OP-COMI*)))))) + (cond + ((or (eq kind1 'snan) (eq kind1 'qnan) (eq kind1 'indef) + (eq kind2 'snan) (eq kind2 'qnan) (eq kind2 'indef)) + (cond ((or (int= operation #.*OP-CMPUNORD*) + (int= operation #.*OP-CMPNEQ*) + (int= operation #.*OP-CMPNLT*) + (int= operation #.*OP-CMPNLE*)) + ;; Return the mask of all 1s. + (mv t + (1- (ash 1 (+ 1 exp-width frac-width))) + invalid)) + ((or (int= operation #.*OP-COMI*) + (int= operation #.*OP-UCOMI*)) + ;; Return a particular integer result for this case (here we + ;; return 7). We then use this result to recognize the unordered + ;; relationship and set the corresponding flags in the EFLAGS + ;; register. + (mv t 7 invalid)) + ;; Return the mask of all 0s. + (t (mv t 0 invalid)))) + ((or (eq kind1 'inf) (eq kind2 'inf)) + ;; Convert -inf to -1, + ;; +inf to 1, + ;; finite to 0. + ;; The purpose of this conversion is used for comparison between two + ;; operands in which at least one of them is infinity. + (b* ((rat1 (if (eq kind1 'inf) + (if (int= sign1 0) 1 -1) + 0)) + (rat2 (if (eq kind2 'inf) + (if (int= sign2 0) 1 -1) + 0)) + (cmp-result + (case operation + (#.*OP-CMPEQ* (= rat1 rat2)) + (#.*OP-CMPLT* (< rat1 rat2)) + (#.*OP-CMPLE* (<= rat1 rat2)) + (#.*OP-CMPUNORD* nil) ;; Neither source operand is a NaN. + (#.*OP-CMPNEQ* (not (= rat1 rat2))) + (#.*OP-CMPNLT* (not (< rat1 rat2))) + (#.*OP-CMPNLE* (not (<= rat1 rat2))) + (#.*OP-CMPORD* t) ;; Neither source operand is a NaN. + (otherwise nil))) + + (result + (if (or (int= operation #.*OP-COMI*) + (int= operation #.*OP-UCOMI*)) + ;; Compare rat1 with rat2. + ;; Return 0 standing for rat1 > rat2, + ;; 1 standing for rat1 < rat2, + ;; 4 standing for ra1 = rat2. + ;; Of course, we can choose any other values to return. + ;; The only requirement is that they must be distinct and + ;; different from the value returned for the case of + ;; unordered relationship, which is 7. + (cond ((> rat1 rat2) 0) + ((< rat1 rat2) 1) + (t 4)) + (if cmp-result + ;; Return the mask of all 1s. + (1- (ash 1 (+ 1 exp-width frac-width))) + ;; Return the mask of all 0s. + 0)))) + (mv t result invalid))) + (t (mv nil 0 invalid)))) + + /// + + (defthm integerp-sse-cmp-special-1 + (integerp + (mv-nth + 1 + (sse-cmp-special kind1 sign1 kind2 sign2 exp-width frac-width operation))) + :rule-classes :type-prescription)) + +(define sse-cmp ((operation :type (integer 0 38)) + (op1 natp) + (op2 natp) + (mxcsr :type (unsigned-byte 32)) + (exp-width posp) + (frac-width posp)) + :prepwork ((local (in-theory (e/d* () + (bitops::logand-with-negated-bitmask +; Matt K. mod April 2016 for addition of type-set bit for the set {1}. + (:t BOOL->BIT)))))) + (b* ((mxcsr (mbe :logic (loghead 32 mxcsr) + :exec mxcsr)) + ((mv kind1 sign1 exp1 ?implicit1 frac1) + (fp-decode op1 exp-width frac-width)) + ((mv kind2 sign2 exp2 ?implicit2 frac2) + (fp-decode op2 exp-width frac-width)) + (daz (equal (mxcsrBits->daz mxcsr) 1)) + ((mv kind1 exp1 frac1) + (sse-daz kind1 exp1 frac1 daz)) + ((mv kind2 exp2 frac2) + (sse-daz kind2 exp2 frac2 daz)) + ((mv special-ok result invalid) + (sse-cmp-special kind1 sign1 kind2 sign2 + exp-width frac-width operation)) + + ;; Check invalid operation + (mxcsr (if invalid (!mxcsrBits->ie 1 mxcsr) mxcsr)) + (im (equal (mxcsrBits->im mxcsr) 1)) + ((when (and invalid (not im))) + (mv 'invalid-operand-exception-is-not-masked 0 mxcsr)) + + ;; Check denormal operand + (de (denormal-exception kind1 kind2)) + (mxcsr (if de (!mxcsrBits->de 1 mxcsr) mxcsr)) + (dm (equal (mxcsrBits->dm mxcsr) 1)) + ((when (and de (not dm))) + (mv 'denormal-operand-exception-is-not-masked 0 mxcsr))) + + (if special-ok + (mv nil result mxcsr) + (b* ((bias (nfix (ec-call (RTL::bias (list nil (1+ frac-width) exp-width))))) + (rat1 (fp-to-rat sign1 exp1 frac1 bias exp-width frac-width)) + (rat2 (fp-to-rat sign2 exp2 frac2 bias exp-width frac-width)) + (cmp-result + (case operation + (#.*OP-CMPEQ* (= rat1 rat2)) + (#.*OP-CMPLT* (< rat1 rat2)) + (#.*OP-CMPLE* (<= rat1 rat2)) + (#.*OP-CMPUNORD* nil) ;; Neither source operand is a NaN. + (#.*OP-CMPNEQ* (not (= rat1 rat2))) + (#.*OP-CMPNLT* (not (< rat1 rat2))) + (#.*OP-CMPNLE* (not (<= rat1 rat2))) + (#.*OP-CMPORD* t) ;; Neither source operand is a NaN. + (otherwise nil))) + + (result + (if (or (int= operation #.*OP-COMI*) + (int= operation #.*OP-UCOMI*)) + ;; Compare rat1 with rat2. + ;; Return 0 standing for rat1 > rat2, + ;; 1 standing for rat1 < rat2, + ;; 4 standing for ra1 = rat2. + ;; Of course, we can choose any other values to return. + ;; The only requirement is that they must be distinct and + ;; different from the value returned by the function + ;; sse-cmp-special for the case of unordered relationship, + ;; which is 7. + (cond ((> rat1 rat2) 0) + ((< rat1 rat2) 1) + (t 4)) + (if cmp-result + ;; Return all 1s. + (1- (ash 1 (+ 1 exp-width frac-width))) + ;; Return all 0s. + 0)))) + + (mv nil result mxcsr)))) + /// + + (defthm integerp-result-sse-cmp + (integerp (mv-nth 1 (sse-cmp operation op1 op2 mxcsr exp-width frac-width))) + :rule-classes :type-prescription) + + (defthm-unsigned-byte-p n32p-mxcsr-sse-cmp + :bound 32 + :concl (mv-nth 2 (sse-cmp operation op1 op2 mxcsr exp-width frac-width)) + :hints (("Goal" :in-theory (e/d* (unsigned-byte-p-when-mxcsrbits-p) (unsigned-byte-p)))) + :gen-type t + :gen-linear t)) + +;; ====================================================================== + +;; Single-Precision: + +(define sp-sse-cmp ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 32)) + (op2 :type (unsigned-byte 32)) + (mxcsr :type (unsigned-byte 32))) + (b* (((mv flg result mxcsr) + (sse-cmp operation op1 op2 mxcsr #.*IEEE-SP-EXP-WIDTH* #.*IEEE-SP-FRAC-WIDTH*)) + (result (n32 result))) + (mv flg result mxcsr)) + /// + (defthm-unsigned-byte-p n32p-result-sp-sse-cmp + :bound 32 + :concl (mv-nth 1 (sp-sse-cmp operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-sp-sse-cmp + :bound 32 + :concl (mv-nth 2 (sp-sse-cmp operation op1 op2 mxcsr)) + :hints (("Goal" :in-theory (e/d* () (unsigned-byte-p)))) + :gen-type t + :gen-linear t)) + +;; Double-Precision: + +(define dp-sse-cmp ((operation :type (integer 0 38)) + (op1 :type (unsigned-byte 64)) + (op2 :type (unsigned-byte 64)) + (mxcsr :type (unsigned-byte 32))) + (b* (((mv flg result mxcsr) + (sse-cmp operation op1 op2 mxcsr #.*IEEE-DP-EXP-WIDTH* #.*IEEE-DP-FRAC-WIDTH*)) + (result (n64 result))) + (mv flg result mxcsr)) + /// + (defthm-unsigned-byte-p n64p-result-dp-sse-cmp + :bound 64 + :concl (mv-nth 1 (dp-sse-cmp operation op1 op2 mxcsr)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mxcsr-dp-sse-cmp + :bound 32 + :concl (mv-nth 2 (dp-sse-cmp operation op1 op2 mxcsr)) + :hints (("Goal" :in-theory (e/d* () (unsigned-byte-p)))) + :gen-type t + :gen-linear t)) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/convert.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/convert.lisp new file mode 100644 index 000000000..939d7bc4e --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/fp/convert.lisp @@ -0,0 +1,537 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2018, Kestrel Technology, LLC +; 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): +; Cuong ChauOp/En: D
+FF/5: JMP m16:16 or m16:32 or m16:64
+ +Source: Intel Manuals \(Vol. 2A\) Instruction Set Reference: the +text below has been edited to contain information only about the +64-bit mode.
+ +The JMP instruction cannot be used to perform +inter-privilege-level far jumps. The processor always uses the +segment selector part of the far address to access the corresponding +descriptor in the GDT or LDT. The descriptor type and access rights +determine the type of jump to be performed.
+ +Far Jump to a Conforming or Non-Conforming Code Segment: If +the selected descriptor is for a code segment, a far jump to a code +segment at the same privilege level is performed. If the selected code +segment is at a different privilege level and the code segment is +non-conforming, a general-protection exception is generated. The +target operand specifies an absolute far address indirectly with a +memory location \(m16:16 or m16:32 or m16:64\). The operand-size +attribute and the REX.w bit determine the size of the offset \(16 or +32 or 64 bits\) in the far address. The new code segment selector and +its descriptor are loaded into CS register, and the offset from the +instruction is loaded into the RIP register.
+ +Far Jump through a Call Gate: When executing a far jump +through a call gate, the segment selector specified by the target +operand identifies the call gate. The offset part of the target +operand is ignored. The processor then jumps to the code segment +specified in the call gate descriptor and begins executing the +instruction at the offset specified in the call gate. No stack switch +occurs. The target operand specifies the far address of the call gate +indirectly with a memory location \(m16:16 or m16:32 or m16:64\).
" + + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" + :in-theory + (union-theories + '(!ms !ms$a !fault !fault$a + !seg-hidden-attri !seg-hidden-attri$a + !seg-hidden-limiti !seg-hidden-limiti$a + ssr-hidden-limiti ssr-hidden-limiti$a + seg-hidden-basei seg-hidden-basei$a + !seg-visiblei !seg-visiblei$a + !seg-hidden-basei !seg-hidden-basei$a + x86-far-jmp-op/en-d + (:rewrite x86p-of-write-*ip) + (:rewrite x86p-of-x86-operand-from-modr/m-and-sib-bytes.x86) + x86p-of-mv-nth-2-of-rml-size + x86p-of-write-*ip + (:rewrite x86p-xw)) + (theory 'minimal-theory))))) + + :prepwork + ((local (in-theory (e/d* (far-jump-guard-helpers + bigger-bound-of-mv-nth-1-x86-operand-from-modr/m-and-sib-bytes-operand) + (unsigned-byte-p + member-equal + acl2::logtail-identity + not + signed-byte-p + (tau-system)))))) + + :guard-hints (("Goal" :in-theory (enable + signed-byte-p-64-when-unsigned-byte-p-48 + signed-byte-p-64-when-unsigned-byte-p-32))) + :modr/m t + + :body + + (b* ( ;; Note that this exception was not mentioned in the Intel + ;; Manuals, but I think that the reason for this omission was + ;; that the JMP instruction reference sheet mentioned direct + ;; addressing opcodes too (which are unavailable in the 64-bit + ;; mode). If the source operand is not a memory location, then + ;; #GP(0) is raised. + ((when (equal mod #b11)) + (!!ms-fresh :source-operand-not-memory-location mod)) + + (p2 (prefixes->seg prefixes)) + (p4? (equal #.*addr-size-override* (prefixes->adr prefixes))) + + ((the (integer 2 8) offset-size) + (select-operand-size + proc-mode nil rex-byte nil prefixes nil nil nil x86)) + + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + (inst-ac? t) + ((mv flg + mem + (the (unsigned-byte 3) increment-RIP-by) + (the (signed-byte 64) ?addr) + x86) + (x86-operand-from-modr/m-and-sib-bytes + proc-mode #.*gpr-access* + ;; offset-size is the number of bytes of the + ;; offset. We need two more bytes for the selector. + (the (integer 2 10) (+ 2 offset-size)) inst-ac? + t ;; A memory pointer operand + seg-reg p4? temp-rip rex-byte r/m mod sib + 0 ;; No immediate operand + x86)) + ((when flg) + (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes-error flg)) + + (badlength? + (check-instruction-length start-rip temp-rip increment-RIP-by)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + (selector (the (unsigned-byte 16) (n16 mem))) + (offset (mbe :logic (part-select mem :low 16 :width + (ash offset-size 3)) + :exec (ash mem -16))) + + ;; Getting the selector's components: + ((the (unsigned-byte 13) sel-index) + (segment-selectorBits->index selector)) + ((the (unsigned-byte 1) sel-ti) + (segment-selectorBits->ti selector)) + ((the (unsigned-byte 2) sel-rpl) + (segment-selectorBits->rpl selector)) + + ;; Is the selector a null selector? A null selector points to + ;; the first entry in the GDT (sel-index=0, ti=0). + ;; The exception #GP(0) is raised when the segment selector in + ;; the destination operand is NULL. + ((when (and (equal sel-ti 0) + (equal sel-index 0))) + (!!fault-fresh :gp 0 :gp-nullselector 0)) ;; #GP(0) + + ((mv dt-base-addr dt-limit) + (if (equal sel-ti 0) + ;; Selector references the GDT. + (b* ((gdtr (the (unsigned-byte 80) (stri #.*gdtr* x86))) + (gdtr-base (if (eql proc-mode #.*64-bit-mode*) + (gdtr/idtrBits->base-addr gdtr) + (n32 (gdtr/idtrBits->base-addr gdtr)))) + (gdtr-limit (gdtr/idtrBits->limit gdtr))) + (mv gdtr-base gdtr-limit)) + ;; Selector references the LDT whose base address is in + ;; LDTR. + (b* ((ldtr-base + (the (unsigned-byte 64) (ssr-hidden-basei #.*ldtr* x86))) + (ldtr-base (if (eql proc-mode #.*64-bit-mode*) + ldtr-base + (n32 ldtr-base))) + (ldtr-limit + (the (unsigned-byte 32) (ssr-hidden-limiti #.*ldtr* x86)))) + (mv ldtr-base ldtr-limit)))) + + ;; We start by reading 8 bytes from the descriptor table, because code + ;; segments are always 8 bytes long. + + ;; Is the limit of the selector within the descriptor table limit? + ;; To obtain the largest address of the descriptor that we are + ;; reading from the GDT or LDT, we multiply the selector index by 8 + ;; and we add 7 to it, because a code descriptor is 8 bytes long (see + ;; AMD manual, Dec'17, Volume 2, Figures 4-14 and 4-20). + (largest-address (+ (ash sel-index 3) 7)) + ((when (< dt-limit largest-address)) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :gp-selector-limit-check-failed (list selector dt-base-addr dt-limit))) + + ;; Now that we know the segment selector is valid, we check if + ;; the segment descriptor address is valid. + (descriptor-addr + ;; The index is scaled by 8. + (+ dt-base-addr (the (unsigned-byte 16) (ash sel-index 3)))) + ((when (not (canonical-address-p descriptor-addr))) + (!!ms-fresh :descriptor-addr-virtual-memory-error descriptor-addr)) + + ;; Now we read the descriptor (or the first 8 bytes of it if we are in + ;; 64-bit mode and it is not a code descriptor): + ((mv flg (the (unsigned-byte 64) descriptor) x86) + ;; [TO-DO@Shilpi]: I believe I should use :x below and not + ;; :r. Double check. + (rml-size 8 descriptor-addr :x x86)) + ((when flg) + (!!ms-fresh :rml-size-error flg)) + + ;; Now we look at the descriptor just read to see what kind it is, + ;; and to process it according to the cases in the pseudocode in + ;; Intel manual, May'18, Volume 2, JMP specification. + + ;; Read the S bit, which is 0 for system segments and 1 for user segments + ;; (AMD manual, Dec'17, Volume 2, Table 4-2). + ;; We use CODE-SEGMENT-DESCRIPTOR-LAYOUTBITS->* even though we don't know + ;; whether this is a code segment yet; it could be a system segment, or + ;; the first half of it in 64-bit mode. But we need to read the S bit + ;; somehow, and CODE-SEGMENT-DESCRIPTOR-LAYOUTBITS->* does that. + ;; TODO: maybe introduce and use a generic function to read the S bit + ;; from a segment descriptor that is not necessarily a code segment? + (s (code-segment-descriptorBits->s descriptor)) + + ;; If S = 1, the descriptor is for a user segment. The only kind of user + ;; segment valid for JMP is a code segment. So we process this descriptor + ;; as a code descriptor segment. + ((when (= s 1)) + + ;; Process conforming or non-conforming code segment: + + ;; The treatment of Conforming and Non-Conforming code segments here + ;; only differs in the privilege check (see pseudocode in Intel manual, + ;; May'18, Volume 2, JMP specification). So we use almost the same ACL2 + ;; code (below) for them. + + (b* ( ;; Ensure that the most significant bit of the Type field is 1 + ;; (i.e. code segment), not 0 (i.e. data segment). See AMD manual, + ;; Dec'17, Volume 2, Table 4-3; the bit is referred to as 'Bit 11' + ;; there, because it is bit 11 of the high quadword of the + ;; descriptor (see Figure 4-14). + (msb-of-type + (code-segment-descriptorBits->msb-of-type descriptor)) + ((unless (= msb-of-type 1)) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :jmp-far-data-segment sel-index)) + + ;; Ensure that D = 0 in 64-bit mode: + (d (code-segment-descriptorBits->d descriptor)) + ;; This condition is equivalent to Intel pseudocode's condition + ;; (see 64-BIT-MODEP): + ((when (and (eql proc-mode #.*64-bit-mode*) (= d 1))) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :cs.d=1-in-64-bit-mode sel-index)) + + ;; Privilege check (differs for conforming and nonconforming + ;; code segments): + (cpl (cpl x86)) + (dpl (code-segment-descriptorBits->dpl descriptor)) + (c (code-segment-descriptorBits->c descriptor)) + (allowed (if (= c 1) ; conforming + ;; Access is allowed to a conforming code segment + ;; when DPL <= CPL (numerically). RPL is ignored + ;; for this privilege check. + (<= dpl cpl) + ;; Access is allowed to a conforming code segment + ;; when RPL <= CPL (numerically) and CPL = DPL. + (and (<= sel-rpl cpl) + (equal cpl dpl)))) + ;; If access is not allowed, #GP(segment-selector) is raised. + ((when (not allowed)) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :privilege-check-fail (acons :dpl dpl + (acons :cpl cpl + (acons :rpl sel-rpl nil))))) + + ;; Ensure that the segment is present: + (p (code-segment-descriptorBits->p descriptor)) + ((unless (= p 1)) + (!!fault-fresh + :np sel-index ;; #NP(selector) + :code-segment-not-present sel-index)) + + ;; Trimming the offset based on the operand-size, + ;; and changing the sign in 64-bit mode: + (jmp-addr + (case offset-size + (2 (n16 offset)) + (4 (n32 offset)) + (t (i64 offset)))) + + ;; #GP(0) is thrown if the target offset in destination is + ;; non-canonical (in 64-bit mode) or outside the segment limit (in + ;; 32-bit mode): + (jmp-addr-ok + (if (eql proc-mode #.*64-bit-mode*) + (canonical-address-p jmp-addr) + (b* ((limit15-0 (code-segment-descriptorBits->limit15-0 + descriptor)) + (limit19-16 (code-segment-descriptorBits->limit19-16 + descriptor)) + (limit (part-install limit15-0 + (ash limit19-16 16) + :low 0 :width 16)) + (g (code-segment-descriptorBits->g descriptor)) + ;; If G = 1, the limit is the number of 4-Kbyte blocks + ;; (AMD manual, Dec'17, Volume 2, Section 4.7.1): + (max-offset + (if (= g 1) + (1- (ash limit 12)) + limit))) + (< jmp-addr max-offset)))) + ((unless jmp-addr-ok) + (!!fault-fresh + :gp 0 ;; #GP(0) + :noncanonical-or-outside-segment-limit jmp-addr)) + + ;; Calculate the new contents of the CS register: + (new-cs-visible + (!segment-selectorBits->rpl cpl selector)) + ;; Update x86 state: + (x86 (!seg-visiblei #.*cs* new-cs-visible x86)) + (x86 (!seg-hidden-basei #.*cs* dt-base-addr x86)) + (x86 (!seg-hidden-limiti #.*cs* dt-limit x86)) + (x86 (!seg-hidden-attri #.*cs* + ;; Get attributes from the descriptor in GDT. + (make-code-segment-attr-field descriptor) + x86)) + (x86 (write-*ip proc-mode jmp-addr x86))) + x86)) + + ;; Here S = 0, so the descriptor just read is a system segment, + ;; or the first half of it in 64-bit mode. + + ;; In 32-bit mode, we only support JMP far in the application-level + ;; view, not in the system-level view of 32-bit mode. Only code + ;; segments, handled above, are used in the application-level view; call + ;; gates, handled below, are used in the system-level view. Thus, at + ;; this point we stop if we are in 32-bit mode. Support for system + ;; segments in 32-bit mode will be added eventually. + + ((when (not (equal proc-mode #.*64-bit-mode*))) + (!!ms-fresh :far-jmp-system-unimplemented-in-32-bit-mode)) + + ;; Here we know we are in 64-bit mode. Thus, we have just read the first + ;; 8 bytes of a system descriptor (which is always 16 bytes in 64-bit + ;; mode). So now we read a 16-byte descriptor from the table, checking + ;; the limit (TODO: maybe optimize this by just reading the next 8 bytes + ;; and joining them with the already read 8 bytes above): + (largest-address (+ (ash sel-index 3) 15)) + ((when (< dt-limit largest-address)) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :gp-selector-limit-check-failed (list selector dt-base-addr dt-limit))) + ((mv flg (the (unsigned-byte 128) descriptor) x86) + ;; [TO-DO@Shilpi]: I believe I should use :x below and not + ;; :r. Double check. + (rml-size 16 descriptor-addr :x x86)) + ((when flg) + (!!ms-fresh :rml-size-error flg)) + + ;; Check if the descriptor is a call gate descriptor: + ((mv call-gate-desc? reason2) + ;; Note that the following predicate reports the + ;; descriptor to be invalid if the P flag of the + ;; descriptor is 0. + (ia32e-valid-call-gate-segment-descriptor-p descriptor)) + + ;; If it is a call gate descriptor, process it accordingly: + ((when call-gate-desc?) + + (b* ((cpl (cpl x86)) + (dpl (call-gate-descriptorBits->dpl descriptor)) + + ;; Access is allowed when: + ;; 1. CPL <= Call gate's DPL + ;; 2. RPL <= Call gate's DPL + ;; 3. If the destination Code segment is conforming, + ;; then: DPL of code segment <= CPL + ;; If the destination Code segment is non-conforming, + ;; then: DPL of code segment = CPL. + ;; If access is not allowed, #GP(segment-selector) is + ;; raised. + ;; Below, we check 1 and 2. We will check for 3 after we + ;; read in the code segment descriptor. + ((when (not (and (<= cpl dpl) + (<= sel-rpl dpl)))) + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :privilege-check-fail (acons :dpl dpl + (acons :cpl cpl + (acons :rpl sel-rpl nil))))) + + (cs-selector + (call-gate-descriptorBits->selector descriptor)) + ((the (unsigned-byte 13) cs-sel-index) + (segment-selectorBits->index cs-selector)) + ((the (unsigned-byte 1) cs-sel-ti) + (segment-selectorBits->ti cs-selector)) + ((the (unsigned-byte 2) cs-sel-rpl) + (segment-selectorBits->rpl cs-selector)) + + ;; If the call gate's code segment selector is NULL, + ;; #GP(0) is raised. + ((when (and (equal cs-sel-ti 0) + (equal cs-sel-index 0))) + (!!fault-fresh :gp 0 ;; #GP(0) + :call-gate-code-segment-nullselector 0)) + + ;; Is the call gate code segment selector index outside + ;; the descriptor table limit? If so, #GP(code segment + ;; selector) is raised. + ((mv cs-dt-base-addr cs-dt-limit) + (if (equal sel-ti 0) + ;; Code Segment Selector references the GDT. + (b* ((gdtr (the (unsigned-byte 80) (stri #.*gdtr* x86))) + (gdtr-base (gdtr/idtrBits->base-addr gdtr)) + (gdtr-base (if (eql proc-mode *64-bit-mode*) + gdtr-base + (n32 gdtr-base))) + (gdtr-limit (gdtr/idtrBits->limit gdtr))) + (mv gdtr-base gdtr-limit)) + ;; Code Segment Selector references the LDT whose base + ;; address is in LDTR. + (b* ((ldtr-base (the (unsigned-byte 64) + (ssr-hidden-basei #.*ldtr* x86))) + (ldtr-base (if (eql proc-mode #.*64-bit-mode*) + ldtr-base + (n32 ldtr-base))) + (ldtr-limit (the (unsigned-byte 32) + (ssr-hidden-limiti #.*ldtr* x86)))) + (mv ldtr-base ldtr-limit)))) + ;; To obtain the largest address of the descriptor that we are + ;; reading from the GDT or LDT, we multiply the selector index by 8 + ;; and we add 7 to it, becausea code descriptor is 8 bytes long (see + ;; AMD manual, Dec'17, Volume 2, Figures 4-14 and 4-20). + (largest-address (+ (ash cs-sel-index 3) 7)) + ((when (< cs-dt-limit largest-address)) + (!!fault-fresh :gp cs-sel-index + :gp-selector-limit-check-failed (list + cs-selector + cs-dt-base-addr + cs-dt-limit))) + + ;; Reading the code segment: we check if the code segment + ;; descriptor is valid. + (cs-descriptor-addr + ;; The index is scaled by 8. + (+ cs-dt-base-addr (the (unsigned-byte 16) (ash cs-sel-index 3)))) + ((when (not (canonical-address-p cs-descriptor-addr))) + (!!ms-fresh :cs-descriptor-addr-virtual-memory-error cs-descriptor-addr)) + ((mv flg (the (unsigned-byte 64) cs-descriptor) x86) + ;; [TO-DO@Shilpi]: I believe I should use :x below and not :r. + (rml-size 8 cs-descriptor-addr :x x86)) + ((when flg) + (!!ms-fresh :rml-size-error flg)) + ((mv valid? reason) + ;; Note that the following predicate reports the + ;; descriptor to be invalid if the P flag of the + ;; descriptor is 0. + (ia32e-valid-code-segment-descriptor-p cs-descriptor)) + ((when (not valid?)) + (!!fault-fresh + :gp cs-sel-index ;; #GP(selector) + :call-gate-code-segment-descriptor-invalid (cons reason cs-descriptor))) + + ;; Checking the privileges of the code segment: + (cs-dpl (code-segment-descriptorBits->dpl cs-descriptor)) + (c-bit (code-segment-descriptorBits->c cs-descriptor)) + ((when (or (and ;; Conforming code segment + (equal c-bit 1) + (not (<= cs-dpl cpl))) + (and ;; Conforming code segment + (equal c-bit 0) + (not (eql cs-dpl cpl))))) + (!!fault-fresh :gp cs-sel-index ;; #GP(selector) + :privilege-check-fail (acons :c-bit c-bit + (acons :cpl cpl + (acons :cs-dpl cs-dpl nil))))) + + (call-gate-offset15-0 + (call-gate-descriptorBits->offset15-0 descriptor)) + (call-gate-offset31-16 + (call-gate-descriptorBits->offset31-16 descriptor)) + (call-gate-offset63-32 ;; this is 0 in 32-bit mode + (call-gate-descriptorBits->offset63-32 descriptor)) + (call-gate-offset31-0 + (mbe :logic (part-install call-gate-offset15-0 + (ash call-gate-offset31-16 16) + :low 0 :width 16) + :exec + (the (unsigned-byte 32) + (logior (the (unsigned-byte 16) call-gate-offset15-0) + (the (unsigned-byte 32) + (ash call-gate-offset31-16 16)))))) + (call-gate-offset + (mbe :logic + (part-install call-gate-offset31-0 + (ash call-gate-offset63-32 32) + :low 0 :width 32) + :exec + (the (unsigned-byte 64) + (logior (the (unsigned-byte 32) call-gate-offset31-0) + (the (unsigned-byte 64) + (ash call-gate-offset63-32 32)))))) + + ;; Trimming the call gate offset based on the operand-size: + (jmp-addr + (case offset-size + (2 (n16 call-gate-offset)) + (4 (n32 call-gate-offset)) + (t call-gate-offset))) + + ;; #GP(0) is thrown if the target offset in destination is + ;; non-canonical. + ((when (not (canonical-address-p jmp-addr))) + (!!ms-fresh :target-offset-virtual-memory-error jmp-addr)) + + (new-cs-visible + (!segment-selectorBits->rpl cpl cs-selector)) + ;; Update x86 state: + (x86 (!seg-visiblei #.*cs* new-cs-visible x86)) + (x86 (!seg-hidden-basei #.*cs* cs-dt-base-addr x86)) + (x86 (!seg-hidden-limiti #.*cs* cs-dt-limit x86)) + (x86 (!seg-hidden-attri #.*cs* + ;; Get attributes from the descriptor in GDT. + (make-code-segment-attr-field cs-descriptor) + x86)) + (x86 (write-*ip proc-mode jmp-addr x86))) + x86))) + + ;; If it is not a code segment or call gate descriptor, since we are in + ;; 64-bit mode for now, #GP(selector) is raised. + (!!fault-fresh + :gp sel-index ;; #GP(selector) + :either-both-code-segment-or-call-gate-are-absent-or-some-other-descriptor-is-present + (cons reason2 descriptor)))) + +;; ====================================================================== +;; INSTRUCTION: LOOP +;; ====================================================================== + +; From Intel Vol. 1, Section 6.3.7: + +;; In 64-bit mode, the operand size for all near branches (CALL, RET, JCC, +;; JCXZ, JMP, and LOOP) is forced to 64 bits. These instructions update the +;; 64-bit RIP without the need for a REX operand-size prefix. + +;; The following aspects of near branches are controlled by the effective +;; operand size: +;; Truncation of the size of the instruction pointer +;; Size of a stack pop or push, due to a CALL or RET +;; Size of a stack-pointer increment or decrement, due to a CALL or RET +;; Indirect-branch operand size + +;; In 64-bit mode, all of the above actions are forced to 64 bits regardless of +;; operand size prefixes (operand size prefixes are silently ignored). However, +;; the displacement field for relative branches is still limited to 32 bits and +;; the address size for near branches is not forced in 64-bit mode. + +;; Address sizes affect the size of RCX used for JCXZ and LOOP; they also +;; impact the address calculation for memory indirect branches. Such addresses +;; are 64 bits by default; but they can be overridden to 32 bits by an address +;; size prefix. + +(def-inst x86-loop + + ;; E0: LOOPNE/LOOPNZ rel8 + ;; E1: LOOPE/LOOPZ rel8 + ;; E2: LOOP rel8 + + ;; Intel Vol2A, LOOP/LOOPcc specification says: + ;; "Performs the loop operation using the RCX, ECX or CX as a counter + ;; (depending on whether address size is 64 bits, 32 bits, or 16 + ;; bits)." + + :parents (one-byte-opcodes) + + :guard-hints (("Goal" :in-theory (e/d (riml08 + riml32 + rime-size + select-address-size) + (unsigned-byte-p)))) + + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" + :in-theory + (e/d* (rime-size) + (unsigned-byte-p + member-equal + acl2::logtail-identity + not + rml-size + select-operand-size))))) + + :prepwork + ((local (in-theory (e/d* (far-jump-guard-helpers) + (unsigned-byte-p + member-equal + acl2::logtail-identity + not))))) + + :body + + (b* (;; temp-rip right now points to the rel8 byte. Add 1 to + ;; temp-rip to account for rel8 when computing the length + ;; of this instruction. + (badlength? (check-instruction-length start-rip temp-rip 1)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + (p4? (equal #.*addr-size-override* (prefixes->adr prefixes))) + + ((the (integer 2 8) counter-size) (select-address-size proc-mode p4? x86)) + (counter (rgfi-size counter-size *rcx* rex-byte x86)) + (counter (trunc counter-size (1- counter))) + + ((the (unsigned-byte 1) zf) (flgi :zf x86)) + + (branch-cond + (if (equal opcode #xE2) ;; LOOP + (not (equal counter 0)) + (if (equal opcode #xE1) ;; LOOPE/LOOPZ + (and (equal zf 1) + (not (equal counter 0))) + ;; #xE0: LOOPNE/LOOPNZ + (and (equal zf 0) + (not (equal counter 0))))))) + + (if branch-cond + + ;; branch condition is true: + (b* ( ;; read rel8 (a value between -128 and +127): + ((mv flg rel8 x86) (rime-size proc-mode 1 temp-rip #.*cs* :x nil x86)) + ((when flg) (!!ms-fresh :rime-size-error flg)) + ;; add rel8 to the address of the next instruction, + ;; which is one past temp-rip to take the rel8 byte into account: + ((mv flg next-rip) (add-to-*ip proc-mode temp-rip (1+ rel8) x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + ;; update counter: + (x86 (!rgfi-size counter-size *rcx* counter rex-byte x86)) + ;; set instruction pointer to new value: + (x86 (write-*ip proc-mode next-rip x86))) + x86) + + ;; branch condition is false: + (b* ( ;; go to the next instruction, + ;; which starts just after the rel8 byte: + ((mv flg next-rip) (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + ;; update counter: + (x86 (!rgfi-size counter-size *rcx* counter rex-byte x86)) + ;; set instruction pointer to new value: + (x86 (write-*ip proc-mode next-rip x86))) + x86)))) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/logical.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/logical.lisp new file mode 100644 index 000000000..94946accc --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/logical.lisp @@ -0,0 +1,395 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2026, Kestrel Technology, LLC +; 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. + +; Authors: +; Cuong Chau
+ NP 0F DB /r PAND mm, mm/m64
+ NP 0F DF /r PANDN mm, mm/m64
+ NP 0F EB /r POR mm, mm/m64
+ NP 0F EF /r PXOR mm, mm/m64
+ "
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :modr/m t
+
+ :body
+
+ (b* ((p2 (prefixes->seg prefixes))
+ (p4? (eql #.*addr-size-override* (prefixes->adr prefixes)))
+ (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))
+
+ ;; The operand size is always 64 bits, i.e. 8 bytes.
+ (operand-size 8)
+
+ ;; The first source operand (Operand 1 in the Intel manual)
+ ;; is the MMX register specified in Reg.
+ ;; This is also the destination operand,
+ ;; and thus we obtain the index for later use.
+ ;; Since there are only 8 MMX registers, the REX byte is not used.
+ ((the (unsigned-byte 4) src1/dst-index) reg)
+ ((the (unsigned-byte 64) src1) (mmx src1/dst-index x86))
+
+ ;; The second source operand (Operand 2 in the Intel manual)
+ ;; is the MMX register, or memory operand, specified in Mod and R/M.
+ (inst-ac? t) ; Intel Manual Volume 2 Table 2-21 (Dec 2023)
+ ((mv flg
+ (the (unsigned-byte 64) src2)
+ (the (integer 0 4) increment-rip-by)
+ ?addr
+ x86)
+ (x86-operand-from-modr/m-and-sib-bytes proc-mode
+ #.*mmx-access*
+ operand-size
+ inst-ac?
+ nil ; not a memory operand
+ seg-reg
+ p4?
+ temp-rip
+ rex-byte
+ r/m
+ mod
+ sib
+ 0 ; no immediate operand
+ x86))
+ ((when flg) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg))
+
+ ;; Increment the instruction pointer in the temp-rip variable.
+ ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
+ (add-to-*ip proc-mode temp-rip increment-rip-by x86))
+ ((when flg) (!!ms-fresh :rip-increment-error flg))
+
+ ;; Ensure the instruction is not too long.
+ (badlength? (check-instruction-length start-rip temp-rip 0))
+ ((when badlength?)
+ (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)
+
+ ;; Calculate the result.
+ (result (case opcode
+ (#xdb (logand src1 src2))
+ (#xdf (logand (lognot src1) src2))
+ (#xeb (logior src1 src2))
+ (#xef (logxor src1 src2))
+ (t 0))) ; unreachable
+
+ ;; Store the result into the destination register.
+ (x86 (!mmx src1/dst-index result x86))
+ (x86 (mmx-instruction-updates x86))
+
+ ;; Update the instruction pointer.
+ (x86 (write-*ip proc-mode temp-rip x86)))
+
+ x86)
+
+ :guard-hints (("Goal" :in-theory (disable unsigned-byte-p))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; SSE variants
+
+(def-inst x86-andp?/andnp?/orp?/xorp?/pand/pandn/por/pxor-Op/En-RM
+
+ :parents (two-byte-opcodes)
+
+ :short "Logical instructions (SSE variants)."
+
+ :long
+ "
+ VANDPD xmm1, xmm2, xmm3/m128
+ VANDPD ymm1, ymm2, ymm3/m256
+
+ VANDPS xmm1, xmm2, xmm3/m128
+ VANDPS ymm1, ymm2, ymm3/m256
+
+ VANDNPD xmm1, xmm2, xmm3/m128
+ VANDNPD ymm1, ymm2, ymm3/m256
+
+ VANDNPS xmm1, xmm2, xmm3/m128
+ VANDNPS ymm1, ymm2, ymm3/m256
+
+ VORPD xmm1, xmm2, xmm3/m128
+ VORPD ymm1, ymm2, ymm3/m256
+
+ VORPS xmm1, xmm2, xmm3/m128
+ VORPS ymm1, ymm2, ymm3/m256
+
+ VXORPD xmm1, xmm2, xmm3/m128
+ VXORPD ymm1, ymm2, ymm3/m256
+
+ VXORPS xmm1, xmm2, xmm3/m128
+ VXORPS ymm1, ymm2, ymm3/m256
+
+ VPAND xmm1, xmm2, xmm3/m128
+ VPAND ymm1, ymm2, ymm3/m256
+
+ VPANDN xmm1, xmm2, xmm3/m128
+ VPANDN ymm1, ymm2, ymm3/m256
+
+ VPOR xmm1, xmm2, xmm3/m128
+ VPOR ymm1, ymm2, ymm3/m256
+
+ VPXOR xmm1, xmm2, xmm3/m128
+ VPXOR ymm1, ymm2, ymm3/m256
+ "
+
+ :modr/m t
+
+ :operation t
+
+ :vex t
+
+ :guard (vex-prefixes-byte0-p vex-prefixes)
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :body
+
+ (b* ((p2 (prefixes->seg prefixes))
+ (p4? (eql #.*addr-size-override* (prefixes->adr prefixes)))
+ (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))
+
+ (rex-byte (rex-byte-from-vex-prefixes vex-prefixes))
+
+ ;; The operand size is determined by VEX.L,
+ ;; based on the VEX.128 and VEX.256 notation
+ ;; (see Intel Manual Volume 2 Section 3.1.1.2 (Dec 2023)).
+ ((the (integer 16 32) operand-size)
+ (if (equal (vex->l vex-prefixes) 1)
+ 32
+ 16))
+
+ ;; The first source operand (Operand 2 in the Intel Manual)
+ ;; is the XMM or YMM register specified in VEX.vvvv.
+ ((the (unsigned-byte 4) src1-index)
+ (vex-vvvv-reg-index (vex->vvvv vex-prefixes)))
+ ((the (unsigned-byte 256) src1) (zmmi-size operand-size src1-index x86))
+
+ ;; The second source operand (Operand 3 in the Intel Manual)
+ ;; is the XMM or YMM register, or memory operand,
+ ;; specified in the ModR/M byte.
+ ;; There is no alignment checking
+ ;; (see Intel Manual Volume 2 Table 2-21 (Dec 2023)).
+ (inst-ac? nil) ; Exceptions Type 4
+ ((mv flg
+ src2
+ (the (integer 0 4) increment-rip-by)
+ (the (signed-byte 64) addr)
+ x86)
+ (x86-operand-from-modr/m-and-sib-bytes proc-mode
+ (if (= operand-size 16)
+ #.*VEX-XMM-ACCESS*
+ #.*YMM-ACCESS*)
+ operand-size
+ inst-ac?
+ nil ; not a memory operand
+ seg-reg
+ p4?
+ temp-rip
+ rex-byte
+ r/m
+ mod
+ sib
+ 0 ; no immediate operand
+ x86))
+ ((when flg) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg))
+
+ ;; The destination operand (Operand 1 in the Intel Manual)
+ ;; is the XMM or YMM register specified in the reg bits of ModR/M.
+ ((the (unsigned-byte 4) dst-index) (reg-index reg rex-byte #.*r*))
+
+ ;; Increment the instruction pointer in the temp-rip variable.
+ ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
+ (add-to-*ip proc-mode temp-rip increment-rip-by x86))
+ ((when flg) (!!ms-fresh :rip-increment-error flg))
+
+ ;; Ensure the instruction is not too long.
+ (badlength? (check-instruction-length start-rip temp-rip 0))
+ ((when badlength?)
+ (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)
+
+ ;; Apply the logical operation to the source operands,
+ ;; and only keep the appropriate number of bits.
+ (result (case operation
+ (#.*OP-AND* (logand src1 src2))
+ (#.*OP-ANDN* (logand (lognot src1) src2))
+ (#.*OP-OR* (logior src1 src2))
+ (#.*OP-XOR* (logxor src1 src2))
+ (otherwise 0))) ; unreachable
+ (result (if (= operand-size 16)
+ (n128 result)
+ (n256 result)))
+
+ ;; Store the result into the destination register.
+ (x86 (!zmmi-size operand-size
+ dst-index
+ result
+ x86
+ :regtype (if (= operand-size 16)
+ #.*vex-xmm-access*
+ #.*ymm-access*)))
+
+ ;; Update the instruction pointer.
+ (x86 (write-*ip proc-mode temp-rip x86)))
+
+ x86)
+
+ :guard-hints (("Goal" :in-theory (e/d () (unsigned-byte-p))))
+
+ :prepwork
+ ((defrulel verify-guards-lemma
+ (implies (unsigned-byte-p 4 x)
+ (unsigned-byte-p 5 x)))))
diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move-sse.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move-sse.lisp
new file mode 100644
index 000000000..0077b1292
--- /dev/null
+++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move-sse.lisp
@@ -0,0 +1,1494 @@
+; X86ISA Library
+
+; Note: The license below is based on the template at:
+; http://opensource.org/licenses/BSD-3-Clause
+
+; Copyright (C) 2015, May - August 2023, Regents of the University of Texas
+; Copyright (C) 2026, Kestrel Technology, LLC
+; Copyright (C) August 2023 - May 2024, Yahya Sohail
+; Copyright (C) May 2024 - August 2024, Intel Corporation
+; 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.
+
+; Authors:
+; Shilpi Goel
+ 66 0F 6F /r MOVDQA xmm1, xmm2/m128
+ "
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :modr/m t
+
+ :body
+
+ (b* (((the (unsigned-byte 4) xmm-index)
+ (reg-index reg rex-byte #.*r*))
+
+ (p2 (the (unsigned-byte 8) (prefixes->seg prefixes)))
+ (p4? (eql #.*addr-size-override* (prefixes->adr prefixes)))
+
+ (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))
+
+ (operand-size 16)
+
+ (inst-ac? t)
+ ((mv flg0
+ (the (unsigned-byte 128) xmm/mem)
+ (the (unsigned-byte 3) increment-rip-by)
+ ?addr
+ x86)
+ (x86-operand-from-modr/m-and-sib-bytes proc-mode
+ #.*xmm-access*
+ operand-size
+ inst-ac?
+ nil ; not memory pointer operand
+ seg-reg
+ p4?
+ temp-rip
+ rex-byte
+ r/m
+ mod
+ sib
+ 0 ; no immediate operand
+ x86))
+ ((when flg0) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg0))
+
+ ((mv flg temp-rip) (add-to-*ip proc-mode temp-rip increment-rip-by x86))
+ ((when flg) (!!ms-fresh :rip-increment-error flg))
+
+ (badlength? (check-instruction-length start-rip temp-rip 0))
+ ((when badlength?)
+ (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)
+
+ ;; Update the x86 state:
+ (x86 (!xmmi-size operand-size xmm-index xmm/mem x86))
+ (x86 (write-*ip proc-mode temp-rip x86)))
+ x86))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(def-inst x86-movdqa-from-xmm
+
+ :parents (two-byte-opcodes)
+
+ :short "Move aligned packed integer values."
+
+ :long
+ "
+ 66 0F 7F /r MOVDQA xmm2/m128, xmm1
+ "
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :modr/m t
+
+ :body
+
+ (b* (((the (unsigned-byte 4) xmm-index)
+ (reg-index reg rex-byte #.*r*))
+
+ ((the (unsigned-byte 128) xmm)
+ (xmmi-size 16 xmm-index x86))
+
+ (p2 (prefixes->seg prefixes))
+ (p4? (eql #.*addr-size-override* (prefixes->adr prefixes)))
+
+ (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))
+
+ (operand-size 16)
+
+ ((mv flg0
+ (the (signed-byte 64) addr)
+ (the (unsigned-byte 3) increment-rip-by)
+ x86)
+ (if (int= mod #b11)
+ (mv nil 0 0 x86)
+ (x86-effective-addr proc-mode
+ p4?
+ temp-rip
+ rex-byte
+ r/m
+ mod
+ sib
+ 0 ; no immediate operand
+ x86)))
+ ((when flg0) (!!ms-fresh :x86-effective-addr-error flg0))
+
+ ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
+ (add-to-*ip proc-mode temp-rip increment-RIP-by x86))
+ ((when flg) (!!ms-fresh :rip-increment-error flg))
+
+ (badlength? (check-instruction-length start-rip temp-rip 0))
+ ((when badlength?)
+ (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)
+
+ ;; Update the x86 state:
+ (inst-ac? ;; Exceptions Type 1
+ t)
+ ((mv flg1 x86)
+ (x86-operand-to-xmm/mem proc-mode
+ operand-size
+ inst-ac?
+ xmm
+ seg-reg
+ addr
+ rex-byte
+ r/m
+ mod
+ x86))
+ ((when flg1)
+ (!!ms-fresh :x86-operand-to-xmm/mem flg1))
+
+ (x86 (write-*ip proc-mode temp-rip x86)))
+ x86))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; MOVSS, MOVSD
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(def-inst x86-movss/movsd-Op/En-RM
+
+ :parents (two-byte-opcodes fp-opcodes)
+
+ :short "Move scalar single/double precision floating-point values"
+
+ :long
+ "Note: The MOVDQU, MOVUPS, and MOVUPD instructions perform 128-bit + unaligned loads or stores. They do not generate general-protection + exceptions (#GP) when operands are not aligned on a 16-byte + boundary. If alignment checking is enabled, alignment-check + exceptions (#AC) may or may not be generated depending on processor + implementation when data addresses are not aligned on an 8-byte + boundary.
" + + :returns (x86 x86p :hyp (x86p x86)) + + :modr/m t + + :body + + (b* (((the (unsigned-byte 4) xmm-index) + (reg-index reg rex-byte #.*r*)) + + (p2 (prefixes->seg prefixes)) + (p4? (eql #.*addr-size-override* (prefixes->adr prefixes))) + + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + (inst-ac? + ;; Exceptions Type 4 but treatment of #AC varies. For now, we + ;; check the alignment. + t) + ((mv flg0 + (the (unsigned-byte 128) xmm/mem) + (the (integer 0 4) increment-RIP-by) + (the (signed-byte 64) ?addr) + x86) + (x86-operand-from-modr/m-and-sib-bytes proc-mode + #.*xmm-access* + 16 + inst-ac? + nil ;; Not a memory pointer operand + seg-reg + p4? + temp-rip + rex-byte + r/m + mod + sib + 0 ;; No immediate operand + x86)) + ((when flg0) + (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg0)) + + ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip)) + (add-to-*ip proc-mode temp-rip increment-RIP-by x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + ;; Update the x86 state: + (x86 (!xmmi-size 16 xmm-index xmm/mem x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + x86)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def-inst x86-movups/movupd/movdqu-Op/En-MR + + :parents (two-byte-opcodes fp-opcodes) + + :long + "
+ NP 0F 12 /r MOVHLPS xmm1, xmm2
+ "
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :modr/m t
+
+ :body
+
+ (b* ((operand-size 16) ; 128 bits
+
+ ;; Operand 1 (destination) is in Reg.
+ ((the (unsigned-byte 4) xmm1-index) (reg-index reg rex-byte #.*r*))
+
+ ;; Operand 2 (source) is in R/M.
+ ((the (unsigned-byte 4) xmm2-index) (reg-index r/m rex-byte #.*b*))
+ ((the (unsigned-byte 128) xmm2) (xmmi-size operand-size xmm2-index x86))
+
+ ;; High quadword of xmm2.
+ ((the (unsigned-byte 64) high-qword)
+ (mbe :logic (part-select xmm2 :low 64 :high 127)
+ :exec (the (unsigned-byte 64)
+ (logand #uxFFFF_FFFF_FFFF_FFFF
+ (ash xmm2 -64)))))
+
+ ;; Update xmm1 (just its low 64 bits).
+ (x86 (!xmmi-size 8 xmm1-index high-qword x86))
+
+ ;; Update instruction pointer.
+ (x86 (write-*ip proc-mode temp-rip x86)))
+
+ x86))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; MOVLHPS
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(def-inst x86-movlhps-sse
+
+ :parents (two-byte-opcodes fp-opcodes)
+
+ :short "Move packed single precision floating-point values low to high."
+
+ :long
+ "
+ NP 0F 16 /r MOVLHPS xmm1, xmm2
+ "
+
+ :returns (x86 x86p :hyp (x86p x86))
+
+ :modr/m t
+
+ :body
+
+ (b* ((operand-size 16) ; 128 bits
+
+ ;; Operand 1 (destination) is in Reg.
+ ((the (unsigned-byte 4) xmm1-index) (reg-index reg rex-byte #.*r*))
+ ((the (unsigned-byte 128) xmm1) (xmmi-size operand-size xmm1-index x86))
+
+ ;; Operand 2 (source) is in R/M.
+ ((the (unsigned-byte 4) xmm2-index) (reg-index r/m rex-byte #.*b*))
+ ((the (unsigned-byte 128) xmm2) (xmmi-size operand-size xmm2-index x86))
+
+ ;; Low quadword of xmm2.
+ ((the (unsigned-byte 64) low-qword)
+ (mbe :logic (part-select xmm2 :low 0 :high 63)
+ :exec (the (unsigned-byte 64)
+ (logand #uxFFFF_FFFF_FFFF_FFFF
+ xmm2))))
+
+ ;; New value for xmm1.
+ ((the (unsigned-byte 128) new-xmm1)
+ (part-install low-qword xmm1 :low 64 :high 127))
+
+ ;; Update xmm1.
+ (x86 (!xmmi-size operand-size xmm1-index new-xmm1 x86))
+
+ ;; Update instruction pointer.
+ (x86 (write-*ip proc-mode temp-rip x86)))
+
+ x86))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; MOVDDUP
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(def-inst x86-movddup-sse
+
+ :parents (two-byte-opcodes)
+
+ :short "MOVDDUP: replicate double precision floating-point values."
+
+ :long
+ "+ This is the SSE variant. +
++ Although the operand size is 128 bits, + the use of @('m64') in @('xmm2/m64') in the Intel manual + suggests that only 64 bits are read from the source operand. + If the access is to memory, + reading 128 and ignoring the 64 high ones can make a difference + if the additional memory accesses cause some kind of exception. +
" + + :returns (x86 x86p :hyp (x86p x86)) + + :modr/m t + + :body + + (b* ((p2 (prefixes->seg prefixes)) + (p4? (eql #.*addr-size-override* + (prefixes->adr prefixes))) + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + ;; index of xmm1: + ((the (unsigned-byte 4) xmm-index) + (reg-index reg rex-byte #.*r*)) + + ;; read xmm2/m64: + (inst-ac? ;; Exceptions Type 5 + t) + ((mv flg0 + (the (unsigned-byte 64) val) + (the (integer 0 4) increment-RIP-by) + & ; address of the operand + x86) + (x86-operand-from-modr/m-and-sib-bytes proc-mode + #.*xmm-access* + 8 ; only 64 bits + inst-ac? + nil ;; Not a memory pointer operand + seg-reg + p4? + temp-rip + rex-byte + r/m + mod + sib + 0 ;; No immediate operand + x86)) + ((when flg0) + (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg0)) + + ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip)) + (add-to-*ip proc-mode temp-rip increment-RIP-by x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + ;; value for xmm1: + (dup-val (logior (ash val 64) val)) + + ;; write to xmm1: + (x86 (!xmmi-size 16 xmm-index dup-val x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + x86)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; MOVMSKPS, MOVMSKPD + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def-inst x86-movmskps-sse + + :parents (two-byte-opcodes) + + :short "MOVMSKPS: extract packed single precision floating-point sign mask." + + :long + (xdoc::topstring + (xdoc::codeblock + "NP 0F 50 /r MOVMSKPS reg, xmm") + (xdoc::p + "This is the SSE variant.")) + + :modr/m t + + :returns (x86 x86p :hyp (x86p x86)) + + :body + + (b* (;; Index of source register. + ((the (unsigned-byte 4) xmm-index) (reg-index reg rex-byte #.*r*)) + + ;; Index of destination register. + ((the (unsigned-byte 4) reg-index) (reg-index r/m rex-byte #.*b*)) + + ;; Collect sign bits. + ((the (unsigned-byte 128) src) (xmmi-size 16 xmm-index x86)) + (sign0 (logbit 31 src)) + (sign1 (logbit 63 src)) + (sign2 (logbit 95 src)) + (sign3 (logbit 127 src)) + + ;; Form value to put into destination. + ((the (unsigned-byte 4) dst) + (acl2::logappn 1 sign0 + 1 sign1 + 1 sign2 + 1 sign3)) + + ;; Write the value into the destination. + ;; We use 64 bits as the operand size, for the following reasons. + ;; In 64-bit mode, that is the default size, + ;; as stated in the manual page for MOVMSKPS; + ;; and there is no way to override it, + ;; because the operand-size-override prefix 66h + ;; is used as mandatory prefix for MOVMSKPD. + ;; In fact, overriding would have no effect, + ;; because writing a 32-bit operand into a GPR in 64-bit mode + ;; zeroes the high 32 bits + ;; (Intel manual, Mar 2026, Vol 1, Sec 3.4.1.1), + ;; which in this case is the same as zero-extending + ;; the 4 bits with the signs to 64 bits. + ;; In 32-bit mode, the upper 32 bits are undefined, + ;; but, as reasoned in the documentation for our WR32 function, + ;; it is adequate to zero those upper 32 bits + ;; when writing the lower 32 bits; + ;; the upper 32 bits can be made undefined + ;; upon switching from 32-bit mode to 64-bit mode. + (x86 (wr64 reg-index dst x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def-inst x86-movmskpd-sse + + :parents (two-byte-opcodes) + + :short "MOVMSKPD: extract packed double precision floating-point sign mask." + + :long + (xdoc::topstring + (xdoc::codeblock + "66 0F 50 /r MOVMSKPD reg, xmm") + (xdoc::p + "This is the SSE variant.")) + + :modr/m t + + :returns (x86 x86p :hyp (x86p x86)) + + :body + + (b* (;; Index of source register. + ((the (unsigned-byte 4) xmm-index) (reg-index reg rex-byte #.*r*)) + + ;; Index of destination register. + ((the (unsigned-byte 4) reg-index) (reg-index r/m rex-byte #.*b*)) + + ;; Collect sign bits. + ((the (unsigned-byte 128) src) (xmmi-size 16 xmm-index x86)) + (sign0 (logbit 63 src)) + (sign1 (logbit 127 src)) + + ;; Form value to put into destination. + ((the (unsigned-byte 2) dst) + (acl2::logappn 1 sign0 + 1 sign1)) + + ;; Write the value into the destination. + ;; We use 64 bits as the operand size, for the following reasons. + ;; In 64-bit mode, that is the default size, + ;; as stated in the manual page for MOVMSKPS; + ;; and there is no way to override it, + ;; because the operand-size-override prefix 66h + ;; is used as mandatory prefix for MOVMSKPD. + ;; In fact, overriding would have no effect, + ;; because writing a 32-bit operand into a GPR in 64-bit mode + ;; zeroes the high 32 bits + ;; (Intel manual, Mar 2026, Vol 1, Sec 3.4.1.1), + ;; which in this case is the same as zero-extending + ;; the 4 bits with the signs to 64 bits. + ;; In 32-bit mode, the upper 32 bits are undefined, + ;; but, as reasoned in the documentation for our WR32 function, + ;; it is adequate to zero those upper 32 bits + ;; when writing the lower 32 bits; + ;; the upper 32 bits can be made undefined + ;; upon switching from 32-bit mode to 64-bit mode. + (x86 (wr64 reg-index dst x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move.lisp new file mode 100644 index 000000000..ff1dfd980 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/move.lisp @@ -0,0 +1,1181 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, May - August 2023, Regents of the University of Texas +; Copyright (C) 2025, Kestrel Technology, LLC +; Copyright (C) August 2023 - May 2024, Yahya Sohail +; Copyright (C) May 2024 - August 2024, Intel Corporation +; 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. + +; Authors: +; Shilpi GoelThis is the variant that moves from memory to register.
" + + :returns (x86 x86p :hyp (x86p x86)) + + :modr/m t + + :body + + (b* ((p2 (the (unsigned-byte 8) (prefixes->seg prefixes))) + (p4? (equal #.*addr-size-override* (prefixes->adr prefixes))) + + ((the (integer 2 8) operand-size) + (select-operand-size proc-mode + nil ; byte-operand? + rex-byte + nil ; imm? + prefixes + nil ; default64? + nil ; ignore-rex? + nil ; ignore-p3-64? + x86)) + + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + ((mv flg operand (the (unsigned-byte 3) increment-rip-by) ?addr x86) + (x86-operand-from-modr/m-and-sib-bytes proc-mode + #.*gpr-access* + operand-size + t ; inst-ac? + nil ; memory-ptr? + seg-reg + p4? + temp-rip + rex-byte + r/m + mod + sib + 0 ; num-imm-bytes + x86)) + ((when flg) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg)) + + ((mv flg temp-rip) (add-to-*ip proc-mode temp-rip increment-rip-by x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ; #GP(0) + + (result (movbe-reverse-bytes operand operand-size)) + + (x86 (!rgfi-size operand-size + (reg-index reg rex-byte #.*r*) + result + rex-byte + x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def-inst x86-movbe-op/en-mr + + :parents (two-byte-opcodes) + + :short "MOVBE: Move data after swapping bytes." + + :long + "This is the variant that moves from register to memory.
" + + :returns (x86 x86p :hyp (x86p x86)) + + :modr/m t + + :body + + (b* ((p2 (the (unsigned-byte 8) (prefixes->seg prefixes))) + (p4? (equal #.*addr-size-override* (prefixes->adr prefixes))) + + ((the (integer 2 8) operand-size) + (select-operand-size proc-mode + nil ; byte-operand? + rex-byte + nil ; imm? + prefixes + nil ; default64? + nil ; ignore-rex? + nil ; ignore-p3-64? + x86)) + + (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86)) + + (operand (rgfi-size operand-size + (reg-index reg rex-byte #.*r*) + rex-byte + x86)) + + (result (movbe-reverse-bytes operand operand-size)) + + ((mv flg + (the (signed-byte 64) addr) + (the (unsigned-byte 3) increment-rip-by) + x86) + (x86-effective-addr proc-mode + p4? + temp-rip + rex-byte + r/m + mod + sib + 0 ; num-imm-bytes + x86)) + ((when flg) (!!ms-fresh :x86-effective-addr-error flg)) + + ((mv flg temp-rip) (add-to-*ip proc-mode temp-rip increment-rip-by x86)) + ((when flg) (!!ms-fresh :rip-increment-error flg)) + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ; #GP(0) + + ((mv flg x86) + (x86-operand-to-reg/mem proc-mode + operand-size + t ; inst-ac? + nil ; memory-ptr? + result + seg-reg + addr + rex-byte + r/m + mod + x86)) + ((when flg) (!!ms-fresh :x86-operand-to-reg/mem flg)) + + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/multiply-spec.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/multiply-spec.lisp new file mode 100644 index 000000000..761a54d26 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/multiply-spec.lisp @@ -0,0 +1,329 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; 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 GoelF6/4:
+ MUL r/m8: AX := AL \* r/m8
+
+ F7/4:
+ MUL r/m16: DX:AX := AX \* r/m16
+ MUL r/m32: EDX:EAX := EAX \* r/m32
+ MUL r/m64: RDX:RAX := RAX \* r/m64
F6/5:
+ IMUL r/m8: AX := AL \* r/m8
+
+ F7/5:
+ IMUL r/m16: DX:AX := AX \* r/m16
+ IMUL r/m32: EDX:EAX := EAX \* r/m32
+ IMUL r/m64: RDX:RAX := RAX \* r/m64
0F AF:
+ IMUL r16, r/m16: r16 := r16 \* r/m16
+ IMUL r32, r/m32: r32 := r32 \* r/m32
+ IMUL r64, r/m64: r64 := r64 \* r/m64
6B ib:
+ IMUL r16, r/m16, imm8
+ IMUL r32, r/m32, imm8
+ IMUL r64, r/m64, imm8
+
+ 69 iw:
+ IMUL r16, r/m16, imm16
+ IMUL r32, r/m32, imm32
+ IMUL r64, r/m64, imm32
+ Op/En: MI
+ C0/0: ROL r/m8, imm8
+ C0/1: ROR r/m8, imm8
+ C0/2: RCL r/m8, imm8
+ C0/3: RCR r/m8, imm8
+ C0/4: SAL/SHL r/m8, imm8
+ C0/5: SHR r/m8, imm8
+ C0/7: SAR r/m8, imm8
+ C1/0: ROL r/m16/32/64, imm8
+ C1/1: ROR r/m16/32/64, imm8
+ C1/2: RCL r/m16/32/64, imm8
+ C1/3: RCR r/m16/32/64, imm8
+ C1/4: SAL/SHL r/m16/32/64, imm8
+ C1/5: SHR r/m16/32/64, imm8
+ C1/7: SAR r/m16/32/64. imm8
+
+ Op/En: M1
+ D0/0: ROL r/m8, 1
+ D0/1: ROR r/m8, 1
+ D0/2: RCL r/m8, 1
+ D0/3: RCR r/m8, 1
+ D0/4: SAL/SHL r/m8, 1
+ D0/5: SHR r/m8, 1
+ D0/7: SAR r/m8, 1
+ D1/0: ROL r/m16/32/64, 1
+ D1/1: ROR r/m16/32/64, 1
+ D1/2: RCL r/m16/32/64, 1
+ D1/3: RCR r/m16/32/64, 1
+ D1/4: SAL/SHL r/m16/32/64, 1
+ D1/5: SHR r/m16/32/64, 1
+ D1/7: SAR r/m16/32/64, 1
+
+ Op/En: MC
+ D2/0: ROL r/m8, CL
+ D2/1: ROR r/m8, CL
+ D2/2: RCL r/m8, CL
+ D2/3: RCR r/m8, CL
+ D2/4: SAL/SHL r/m8, CL
+ D2/5: SHR r/m8, CL
+ D2/7: SAR r/m8, CL
+ D3/0: ROL r/m16/32/64, CL
+ D3/1: ROR r/m16/32/64, CL
+ D3/2: RCL r/m16/32/64, CL
+ D3/3: RCR r/m16/32/64, CL
+ D3/4: SAL/SHL r/m16/32/64, CL
+ D3/5: SHR r/m16/32/64, CL
+ D3/7: SAR r/m16/32/64, CL
+
+ Op/En: MRI
+ 0F A4: SHLD r/m16, r16, imm8
+ 0F A4: SHLD r/m32, r32, imm8
+ 0F A4: SHLD r/m64, r64, imm8
+
+ Op/En: MRC
+ 0F A5: SHLD r/m16, r16, CL
+ 0F A5: SHLD r/m32, r32, CL
+ 0F A5: SHLD r/m64, r64, CL
+
+ Op/En: MRI
+ 0F AC: SHRD r/m16, r16, imm8
+ 0F AC: SHRD r/m32, r32, imm8
+ 0F AC: SHRD r/m64, r64, imm8
+
+ Op/En: MRC
+ 0F AD: SHRD r/m16, r16, CL
+ 0F AD: SHRD r/m32, r32, CL
+ 0F AD: SHRD r/m64, r64, CL
+
Source: Intel Manual, Volume 2B, Instruction Set Reference +\(N-Z\).
+ +The shift arithmetic left \(SAL\) and shift logical left \(SHL\) +instructions perform the same operation; they shift the bits in the +destination operand to the left \(toward more significant bit +locations\). For each shift count, the most significant bit of the +destination operand is shifted into the CF flag, and the least +significant bit is cleared. The OF flag is affected only on 1-bit +shifts. For left shifts, the OF flag is set to 0 if the +most-significant bit of the result is the same as the CF flag (that +is, the top two bits of the original operand were the same); +otherwise, it is set to 1.
" + + (case size + (1 (sal/shl-spec-8 dst src input-rflags)) + (2 (sal/shl-spec-16 dst src input-rflags)) + (4 (sal/shl-spec-32 dst src input-rflags)) + (8 (sal/shl-spec-64 dst src input-rflags)) + (otherwise (mv 0 0 0))) + + /// + + (defthm natp-mv-nth-0-sal/shl-spec + (natp (mv-nth 0 (sal/shl-spec size dst src input-rflags))) + :rule-classes :type-prescription) + + (defthm-unsigned-byte-p n32p-mv-nth-1-sal/shl-spec + :bound 32 + :concl (mv-nth 1 (sal/shl-spec size dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mv-nth-2-sal/shl-spec + :bound 32 + :concl (mv-nth 2 (sal/shl-spec size dst src input-rflags)) + :gen-type t + :gen-linear t)) + +;;;;;;;;;; SHR: + +(define shr-spec-gen ((size :type (member 8 16 32 64))) + :verify-guards nil + + (b* ((size-1 (1- size)) + (size+1 (1+ size)) + (neg-size-1 (- size-1)) + (fn-name (mk-name "SHR-SPEC-" size)) + (?str-nbits (if (eql size 8) "08" size))) + + `(define ,fn-name + ((dst :type (unsigned-byte ,size)) + (src :type (unsigned-byte 6) + "Note that @('src') is masked appropriately + by the caller of this function.") + (input-rflags :type (unsigned-byte 32))) + + :parents (shr-spec) + :guard-hints (("Goal" :in-theory (e/d* (rflag-RoWs-enables) + (unsigned-byte-p + (tau-system))))) + + (b* ((dst (mbe :logic (n-size ,size dst) + :exec dst)) + (src (mbe :logic (n-size 6 src) + :exec src)) + (input-rflags (mbe :logic (n32 input-rflags) + :exec input-rflags)) + + (neg-src (the (signed-byte ,size+1) (- src))) + (raw-result (the (unsigned-byte ,size) + (ash + (the (unsigned-byte ,size) dst) + (the (signed-byte ,size+1) neg-src)))) + (result (the (unsigned-byte ,size) (n-size ,size raw-result))) + + ((mv (the (unsigned-byte 32) output-rflags) + (the (unsigned-byte 32) undefined-flags)) + + (case src + (0 + ;; No flags affected. + (mv input-rflags 0)) + + (1 + (b* + ((cf (mbe :logic (part-select dst :low 0 :width 1) + :exec (the (unsigned-byte 1) + (logand 1 (the (unsigned-byte ,size) + dst))))) + (pf (general-pf-spec ,size result)) + ;; AF is undefined. + (zf (zf-spec result)) + (sf (general-sf-spec ,size result)) + (of (mbe :logic (part-select dst :low ,size-1 :width 1) + :exec (the (unsigned-byte 1) + (ash (the (unsigned-byte ,size) dst) + ,neg-size-1)))) + + (output-rflags (mbe :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf + :of of) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + (!rflagsBits->of + of + input-rflags)))))))) + (undefined-flags (the (unsigned-byte 32) + (!rflagsBits->af 1 0)))) + (mv output-rflags undefined-flags))) + + (otherwise + (if (<= ,size src) + (b* ( + ;; CF is undefined. + (pf (general-pf-spec ,size result)) + ;; AF is undefined. + (zf (zf-spec result)) + (sf (general-sf-spec ,size result)) + ;; OF is undefined. + (output-rflags (mbe :logic + (change-rflagsBits + input-rflags + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags)))))) + + (undefined-flags (mbe :logic + (change-rflagsBits + 0 + :cf 1 + :af 1 + :of 1) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + 1 + (!rflagsBits->af + 1 + (!rflagsBits->of + 1 + 0))))))) + + (mv output-rflags undefined-flags)) + + (b* ((cf (mbe :logic (part-select dst :low (1- src) :width 1) + :exec (let* ((shft + (the (signed-byte ,size) + (- 1 + (the (unsigned-byte ,size) src))))) + (the (unsigned-byte 1) + (logand + 1 + (the (unsigned-byte ,size) + (ash + (the (unsigned-byte ,size) dst) + (the (signed-byte ,size) shft)))))))) + (pf (general-pf-spec ,size result)) + ;; AF is undefined. + (zf (zf-spec result)) + (sf (general-sf-spec ,size result)) + ;; OF is undefined. + + (output-rflags (mbe :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags))))))) + + (undefined-flags (mbe :logic + (change-rflagsBits + 0 + :af 1 + :of 1) + :exec + (!rflagsBits->af + 1 + (!rflagsBits->of + 1 0))))) + (mv output-rflags undefined-flags)))))) + + (output-rflags (mbe :logic (n32 output-rflags) + :exec output-rflags)) + + (undefined-flags (mbe :logic (n32 undefined-flags) + :exec undefined-flags))) + + (mv result output-rflags undefined-flags)) + + /// + + (local (in-theory (e/d () (unsigned-byte-p)))) + + (defthm-unsigned-byte-p ,(mk-name "N" str-nbits "-MV-NTH-0-" fn-name) + :bound ,size + :concl (mv-nth 0 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-1-" fn-name) + :bound 32 + :concl (mv-nth 1 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name) + :bound 32 + :concl (mv-nth 2 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t)))) + +(local + (defthm logand-1-and-logtail-thm + (implies (and (unsigned-byte-p (1+ n) dst) + (natp n)) + (equal (logand 1 (logtail n dst)) + (logtail n dst))) + :hints (("Goal" :in-theory (e/d* (bool->bit + acl2::logtail** + acl2::ihsext-inductions + acl2::unsigned-byte-p**) + (unsigned-byte-p)))))) + +(make-event (shr-spec-gen 8)) +(make-event (shr-spec-gen 16)) +(make-event (shr-spec-gen 32)) +(make-event (shr-spec-gen 64)) + +(define shr-spec + ((size :type (member 1 2 4 8)) + dst src + (input-rflags :type (unsigned-byte 32))) + :guard (and (unsigned-byte-p 6 src) + (case size + (1 (n08p dst)) + (2 (n16p dst)) + (4 (n32p dst)) + (8 (n64p dst)) + (otherwise nil))) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SHR') instruction" + + :long "Source: Intel Manual, Volume 2B, Instruction Set Reference \(N-Z\).
+ +The shift arithmetic right \(SAR\) and shift logical right \(SHR\) +instructions shift the bits of the destination operand to the right +\(toward less significant bit locations\). For each shift count, the +least significant bit of the destination operand is shifted into the +CF flag, and the most significant bit is either set or cleared +depending on the instruction type. The SHR instruction clears the most +significant bit (see Figure 7-8 in the Intel 64 and IA-32 +Architectures Software Developer s Manual, Volume 1)... The OF flag is +affected only on 1-bit shifts. For the SAR instruction, the OF flag is +cleared for all 1-bit shifts. For the SHR instruction, the OF flag is +set to the most-significant bit of the original operand.
" + + (case size + (1 (shr-spec-8 dst src input-rflags)) + (2 (shr-spec-16 dst src input-rflags)) + (4 (shr-spec-32 dst src input-rflags)) + (8 (shr-spec-64 dst src input-rflags)) + (otherwise (mv 0 0 0))) + + /// + + (defthm natp-mv-nth-0-shr-spec + (natp (mv-nth 0 (shr-spec size dst src input-rflags))) + :rule-classes :type-prescription) + + (defthm-unsigned-byte-p n32p-mv-nth-1-shr-spec + :bound 32 + :concl (mv-nth 1 (shr-spec size dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mv-nth-2-shr-spec + :bound 32 + :concl (mv-nth 2 (shr-spec size dst src input-rflags)) + :gen-type t + :gen-linear t)) + +;;;;;;;;;; SAR: + +(define sar-spec-gen ((size :type (member 8 16 32 64))) + :verify-guards nil + + (b* ((size+1 (1+ size)) + (size-1 (1- size)) + (neg-size-1 (- size-1)) +; (size-mask (1- (expt 2 size))) + (fn-name (mk-name "SAR-SPEC-" size)) + (?str-nbits (if (eql size 8) "08" size))) + + `(define ,fn-name + ((dst :type (unsigned-byte ,size)) + (src :type (unsigned-byte 6) + "Note that @('src') is masked appropriately + by the caller of this function.") + (input-rflags :type (unsigned-byte 32))) + + :parents (sar-spec) + :guard-hints + (("Goal" :in-theory (e/d* (rflag-RoWs-enables) + (unsigned-byte-p + bitops::logbit-to-logbitp + (tau-system)))) + (and stable-under-simplificationp + '(:in-theory (enable bitops::logbit-to-logbitp)))) + :prepwork + ((local + (defthm sar-spec-helper + (implies (unsigned-byte-p ,size dst) + (equal (logand ,(1- (expt 2 size)) (logtail src dst)) + (logtail src dst))) + :hints (("Goal" + :do-not-induct t + :use ((:instance unsigned-byte-p-of-logtail + (size1 ,size) + (size src) + (i dst))) + :in-theory (e/d* () + (unsigned-byte-p + unsigned-byte-p-of-logtail)))))) + + (local + (defthm sar-spec-helper-2 + (implies (unsigned-byte-p (1+ n) x) + (equal (logbit n x) + (logtail n x))) + :hints (("Goal" :in-theory + (e/d (bitops::logbitp** bitops::logbitp-induct + bitops::logtail** bitops::logtail-induct + bitops::unsigned-byte-p**) + (unsigned-byte-p))))))) + + (b* ((dst (mbe :logic (n-size ,size dst) + :exec dst)) + (src (mbe :logic (n-size 6 src) + :exec src)) + (input-rflags (mbe :logic (n32 input-rflags) + :exec input-rflags)) + + (neg-src (the (signed-byte ,size+1) (- src))) + (raw-result-not-sign-extended + (the (unsigned-byte ,size) + (ash + (the (unsigned-byte ,size) dst) + (the (signed-byte ,size+1) neg-src)))) + (raw-result + (if (eql (mbe :logic (logbit ,size-1 dst) + :exec (the (unsigned-byte 1) + (ash (the (unsigned-byte ,size) + dst) + ,neg-size-1))) + 1) + (loghead ,size + (ash + (mbe :logic (logext ,size dst) + :exec (bitops::fast-logext ,size dst)) + neg-src)) +; (the (unsigned-byte ,size) +; (logior ,size-mask raw-result-not-sign-extended)) + raw-result-not-sign-extended)) + (result (mbe :logic (n-size ,size raw-result) + :exec raw-result)) + + ((mv (the (unsigned-byte 32) output-rflags) + (the (unsigned-byte 32) undefined-flags)) + + (case src + (0 + ;; No flags affected. + (mv input-rflags 0)) + + (1 + (b* ((cf (mbe :logic (part-select dst :low 0 :width 1) + :exec (the (unsigned-byte 1) + (logand 1 (the (unsigned-byte ,size) + dst))))) + (pf (general-pf-spec ,size result)) + ;; AF is undefined. + (zf (zf-spec result)) + (sf (general-sf-spec ,size result)) + (of 0) + + (output-rflags (mbe :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf + :of of) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + (!rflagsBits->of + of + input-rflags)))))))) + + (undefined-flags + (the (unsigned-byte 32) + (!rflagsBits->af 1 0)))) + (mv output-rflags undefined-flags))) + + (otherwise + (b* ((cf (if (<= ,size src) + (mbe :logic (part-select dst :low ,size-1 :width 1) + :exec (let* ((shft (the (signed-byte ,size) ,neg-size-1))) + (the (unsigned-byte 1) + (ash + (the (unsigned-byte ,size) dst) + (the (signed-byte ,size) shft))))) + (mbe :logic (part-select dst :low (1- src) :width 1) + :exec (let* ((shft + (the (signed-byte ,size) + (- 1 + (the (unsigned-byte ,size) src))))) + (the (unsigned-byte 1) + (logand + 1 + (the (unsigned-byte ,size) + (ash + (the (unsigned-byte ,size) dst) + (the (signed-byte ,size) shft))))))))) + (pf (general-pf-spec ,size result)) + ;; AF is undefined. + (zf (zf-spec result)) + (sf (general-sf-spec ,size result)) + ;; OF is undefined. + (output-rflags (mbe :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags))))))) + + (undefined-flags (mbe :logic + (change-rflagsBits + 0 + :af 1 + :of 1) + :exec + (the (unsigned-byte 32) + (!rflagsBits->af + 1 + (!rflagsBits->of + 1 + 0)))))) + (mv output-rflags undefined-flags))))) + + (output-rflags (mbe :logic (n32 output-rflags) + :exec output-rflags)) + + (undefined-flags (mbe :logic (n32 undefined-flags) + :exec undefined-flags))) + + (mv result output-rflags undefined-flags)) + + /// + + (local (in-theory (e/d () (unsigned-byte-p)))) + + (defthm-unsigned-byte-p ,(mk-name "N" str-nbits "-MV-NTH-0-" fn-name) + :bound ,size + :concl (mv-nth 0 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-1-" fn-name) + :bound 32 + :concl (mv-nth 1 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name) + :bound 32 + :concl (mv-nth 2 (,fn-name dst src input-rflags)) + :gen-type t + :gen-linear t)))) + +(make-event (sar-spec-gen 8)) +(make-event (sar-spec-gen 16)) +(make-event (sar-spec-gen 32)) +(make-event (sar-spec-gen 64)) + +(define sar-spec + ((size :type (member 1 2 4 8)) + dst src + (input-rflags :type (unsigned-byte 32))) + :guard (and (n06p src) + (case size + (1 (n08p dst)) + (2 (n16p dst)) + (4 (n32p dst)) + (8 (n64p dst)) + (otherwise nil))) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SAR') instruction" + + :long "Source: Intel Manual, Volume 2B, Instruction Set Reference \(N-Z\).
+ +The shift arithmetic right \(SAR\) and shift logical right \(SHR\) +instructions shift the bits of the destination operand to the right +\(toward less significant bit locations\). For each shift count, the +least significant bit of the destination operand is shifted into the +CF flag, and the most significant bit is either set or cleared +depending on the instruction type. The SAR instruction sets or clears +the most significant bit to correspond to the sign \(most significant +bit\) of the original value in the destination operand. In effect, the +SAR instruction fills the empty bit position s shifted value with the +sign of the unshifted value. ... The OF flag is affected only on 1-bit +shifts. For the SAR instruction, the OF flag is cleared for all 1-bit +shifts. For the SHR instruction, the OF flag is set to the +most-significant bit of the original operand.
" + + (case size + (1 (sar-spec-8 dst src input-rflags)) + (2 (sar-spec-16 dst src input-rflags)) + (4 (sar-spec-32 dst src input-rflags)) + (8 (sar-spec-64 dst src input-rflags)) + (otherwise (mv 0 0 0))) + + /// + + (defthm natp-mv-nth-0-sar-spec + (natp (mv-nth 0 (sar-spec size dst src input-rflags))) + :rule-classes :type-prescription) + + (defthm-unsigned-byte-p n32p-mv-nth-1-sar-spec + :bound 32 + :concl (mv-nth 1 (sar-spec size dst src input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mv-nth-2-sar-spec + :bound 32 + :concl (mv-nth 2 (sar-spec size dst src input-rflags)) + :gen-type t + :gen-linear t)) + +;;;;;;;;;; SHLD: + +(define shld-spec-gen ((size :type (member 16 32 64))) + :verify-guards nil + + (b* ((size-1 (1- size)) + (neg-size-1 (- size-1)) + (size*2 (* 2 size)) + (fn-name (mk-name "SHLD-SPEC-" size))) + + `(define ,fn-name + ((dst :type (unsigned-byte ,size)) + (src :type (unsigned-byte ,size)) + (cnt :type (unsigned-byte 6) + "Note that @('cnt') is masked appropriately + by the caller of this function.") + (input-rflags :type (unsigned-byte 32))) + :returns (mv output-dst ; result (irrelevant if undefined-dst? is t) + undefined-dst? ; true iff result of operation is undefined + output-rflags ; output flags, ignoring the undefined ones + undefined-flags) ; flags to set to undefined values + + :guard-hints (("Goal" :in-theory (e/d* (rflag-RoWs-enables) + (unsigned-byte-p + (tau-system))))) + + :parents (shld-spec) + + (b* ((dst (mbe :logic (n-size ,size dst) + :exec dst)) + (src (mbe :logic (n-size ,size src) + :exec src)) + (cnt (mbe :logic (n-size 6 cnt) + :exec cnt)) + (input-rflags (mbe :logic (n32 input-rflags) + :exec input-rflags)) + + ;; we juxtapose dst and src, + ;; by shifting dst to the left by size bits + ;; and OR'ing that with src, + ;; obtaining a 2*size bit value: + (dst-src (the (unsigned-byte ,size*2) + (logior (the (unsigned-byte ,size*2) + (ash (the (unsigned-byte ,size) dst) + ,size)) + (the (unsigned-byte ,size) src)))) + ;; we shift the juxtaposed dst and src to the left by cnt bits, + ;; so that the high bits of src go into the low bits of dst: + (output-dst (the (unsigned-byte ,size) + ;; Our result is in the 2*size-1:size bits + (n-size ,size + (logtail ,size + (ash (the (unsigned-byte ,size*2) + dst-src) + (the (unsigned-byte ,size) + cnt)))))) + + ((mv (the (unsigned-byte 32) output-rflags) + (the (unsigned-byte 32) undefined-flags)) + + (case cnt + + (0 + ;; no flags affected + (mv input-rflags 0)) + + (1 + ;; all flags but AF changed accordingly, AF undefined: + (b* ((cf ; last bit shifted out of dst, i.e. highest bit + (mbe :logic (part-select dst :low ,size-1 :width 1) + :exec (the (unsigned-byte 1) + (logand (the (unsigned-byte ,size) + (ash + (the (unsigned-byte ,size) + dst) + ,neg-size-1)) + 1)))) + (of ; set iff a sign change occurred + (if (= (logbit ,size-1 dst) + (logbit ,size-1 output-dst)) + 0 + 1)) + (pf (general-pf-spec ,size output-dst)) + (zf (zf-spec output-dst)) + (sf (general-sf-spec ,size output-dst)) + (output-rflags (mbe + :logic + (change-rflagsBits + input-rflags + :cf cf + :of of + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->of + of + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags)))))))) + (undefined-flags (the (unsigned-byte 32) + (!rflagsBits->af 1 0)))) + (mv output-rflags undefined-flags))) + + (otherwise + + (if (<= cnt ,size) + ;; all flags but OF and AF changed accordingly, + ;; OF and AF undefined: + (b* ((cf ; last bit shifted out of dst + (mbe :logic (part-select dst + :low (- ,size cnt) + :width 1) + :exec (logand + 1 + (the (unsigned-byte ,size) + (ash (the (unsigned-byte ,size) dst) + (the (signed-byte 7) + (- cnt ,size))))))) + (pf (general-pf-spec ,size output-dst)) + (zf (zf-spec output-dst)) + (sf (general-sf-spec ,size output-dst)) + (output-rflags (mbe + :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags))))))) + (undefined-flags (mbe + :logic + (change-rflagsBits + 0 + :af 1 + :of 1) + :exec + (!rflagsBits->af + 1 + (!rflagsBits->of + 1 0))))) + (mv output-rflags undefined-flags)) + + ;; all flags undefined if cnt > size: + (b* ((output-rflags input-rflags) + (undefined-flags (the (unsigned-byte 32) + (!rflagsBits->cf + 1 + (!rflagsBits->of + 1 + (!rflagsBits->pf + 1 + (!rflagsBits->zf + 1 + (!rflagsBits->sf + 1 + (!rflagsBits->af + 1 + input-rflags))))))))) + (mv output-rflags undefined-flags)))))) + + (output-rflags (mbe :logic (n32 output-rflags) + :exec output-rflags)) + + (undefined-flags (mbe :logic (n32 undefined-flags) + :exec undefined-flags))) + + (mv output-dst + (> cnt ,size) ; result undefined if count exceeds operand size + output-rflags + undefined-flags)) + + /// + + (local (in-theory (e/d () (unsigned-byte-p)))) + + (defthm-unsigned-byte-p ,(mk-name "N" size "-MV-NTH-0-" fn-name) + :bound ,size + :concl (mv-nth 0 (,fn-name dst src cnt input-rflags)) + :hints (("Goal" :in-theory (enable bool->bit))) + :gen-type t + :gen-linear t) + + (defthm ,(mk-name "BOOLEANP-MV-NTH-1-" fn-name) + (booleanp (mv-nth 1 (,fn-name dst src cnt input-rflags)))) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name) + :bound 32 + :concl (mv-nth 2 (,fn-name dst src cnt input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-3-" fn-name) + :bound 32 + :concl (mv-nth 3 (,fn-name dst src cnt input-rflags)) + :gen-type t + :gen-linear t)))) + +(make-event (shld-spec-gen 16)) +(make-event (shld-spec-gen 32)) +(make-event (shld-spec-gen 64)) + +(define shld-spec + ((size :type (member 2 4 8)) + dst + src + cnt + (input-rflags :type (unsigned-byte 32))) + :guard (and (unsigned-byte-p 6 cnt) + (case size + (2 (and (n16p dst) (n16p src))) + (4 (and (n32p dst) (n32p src))) + (8 (and (n64p dst) (n64p src))) + (otherwise nil))) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SHLD') instruction." + + (case size + (2 (shld-spec-16 dst src cnt input-rflags)) + (4 (shld-spec-32 dst src cnt input-rflags)) + (8 (shld-spec-64 dst src cnt input-rflags)) + (otherwise (mv 0 nil 0 0))) + + /// + + (defthm natp-mv-nth-0-shld-spec + (natp (mv-nth 0 (shld-spec size dst src cnt input-rflags))) + :rule-classes :type-prescription) + + (defthm booleanp-of-mv-nth-1-shld-spec + (booleanp (mv-nth 1 (shld-spec size dst src cnt input-rflags)))) + + (defthm-unsigned-byte-p n32p-mv-nth-2-shld-spec + :bound 32 + :concl (mv-nth 2 (shld-spec size dst src cnt input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mv-nth-3-shld-spec + :bound 32 + :concl (mv-nth 3 (shld-spec size dst src cnt input-rflags)) + :gen-type t + :gen-linear t)) + +;;;;;;;;;; SHRD: + +(define shrd-spec-gen ((size :type (member 16 32 64))) + :verify-guards nil + + (b* ((size+1 (1+ size)) + (size-1 (1- size)) + (size*2 (* 2 size)) + (fn-name (mk-name "SHRD-SPEC-" size))) + + `(define ,fn-name + ((dst :type (unsigned-byte ,size)) + (src :type (unsigned-byte ,size)) + (cnt :type (unsigned-byte 6) + "Note that @('cnt') is masked appropriately + by the caller of this function..") + (input-rflags :type (unsigned-byte 32))) + :returns (mv output-dst ; result (irrelevant if undefined-dst? is t) + undefined-dst? ; true iff result of operation is undefined + output-rflags ; output flags, ignoring the undefined ones + undefined-flags) ; flags to set to undefined values + + :parents (shrd-spec) + + :guard-hints (("Goal" :in-theory (e/d* (rflag-RoWs-enables) + (unsigned-byte-p + (tau-system))))) + + (b* ((dst (mbe :logic (n-size ,size dst) + :exec dst)) + (src (mbe :logic (n-size ,size src) + :exec src)) + (cnt (mbe :logic (n-size 6 cnt) + :exec cnt)) + (input-rflags (mbe :logic (n32 input-rflags) + :exec input-rflags)) + + ;; we juxtapose src and dst, + ;; by shifting src to the left by size bits + ;; and OR'ing that with dst, + ;; obtaining a 2*size bit value: + (src-dst (the (unsigned-byte ,size*2) + (logior (the (unsigned-byte ,size*2) + (ash (the (unsigned-byte ,size) src) + ,size)) + (the (unsigned-byte ,size) dst)))) + ;; we shift the juxtaposed src and dst to the rigth by cnt bits, + ;; so that the low bits of src go into the high bits of dst: + (neg-cnt (the (signed-byte ,size+1) (- cnt))) + (output-dst (the (unsigned-byte ,size) + (n-size ,size (ash (the (unsigned-byte ,size*2) + src-dst) + (the (signed-byte ,size+1) + neg-cnt))))) + + ((mv (the (unsigned-byte 32) output-rflags) + (the (unsigned-byte 32) undefined-flags)) + + (case cnt + + (0 + ;; no flags affected + (mv input-rflags 0)) + + (1 + ;; all flags but AF changed accordingly, AF undefined: + (b* ((cf ; last bit shifted out of dst, i.e. lowest bit + (mbe :logic (part-select dst :low 0 :width 1) + :exec (the (unsigned-byte 1) + (logand (the (unsigned-byte ,size) dst) + 1)))) + (of ; set iff a sign change occurred + (if (= (logbit ,size-1 dst) + (logbit ,size-1 output-dst)) + 0 + 1)) + (pf (general-pf-spec ,size output-dst)) + (zf (zf-spec output-dst)) + (sf (general-sf-spec ,size output-dst)) + (output-rflags (mbe + :logic + (change-rflagsBits + input-rflags + :cf cf + :of of + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->of + of + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags)))))))) + (undefined-flags (the (unsigned-byte 32) + (!rflagsBits->af 1 0)))) + (mv output-rflags undefined-flags))) + + (otherwise + + (if (<= cnt ,size) + ;; all flags but OF and AF changed accordingly, + ;; OF and AF undefined: + (b* ((cf ; last bit shifted out of dst + (mbe :logic (part-select dst :low (1- cnt) :width 1) + :exec (logand + 1 + (the (unsigned-byte ,size) + (ash (the (unsigned-byte ,size) dst) + (the (signed-byte ,size+1) + (1+ neg-cnt))))))) + (pf (general-pf-spec ,size output-dst)) + (zf (zf-spec output-dst)) + (sf (general-sf-spec ,size output-dst)) + (output-rflags (mbe + :logic + (change-rflagsBits + input-rflags + :cf cf + :pf pf + :zf zf + :sf sf) + :exec + (the (unsigned-byte 32) + (!rflagsBits->cf + cf + (!rflagsBits->pf + pf + (!rflagsBits->zf + zf + (!rflagsBits->sf + sf + input-rflags))))))) + (undefined-flags (mbe + :logic + (change-rflagsBits + 0 + :af 1 + :of 1) + :exec + (!rflagsBits->af + 1 + (!rflagsBits->of + 1 0))))) + (mv output-rflags undefined-flags)) + + ;; all flags undefined if cnt > size: + (b* ((output-rflags input-rflags) + (undefined-flags (the (unsigned-byte 32) + (!rflagsBits->cf + 1 + (!rflagsBits->of + 1 + (!rflagsBits->pf + 1 + (!rflagsBits->zf + 1 + (!rflagsBits->sf + 1 + (!rflagsBits->af + 1 + input-rflags))))))))) + (mv output-rflags undefined-flags)))))) + + (output-rflags (mbe :logic (n32 output-rflags) + :exec output-rflags)) + + (undefined-flags (mbe :logic (n32 undefined-flags) + :exec undefined-flags))) + + (mv output-dst + (> cnt ,size) ; result undefined if count exceeds operand size + output-rflags + undefined-flags)) + + /// + + (local (in-theory (e/d () (unsigned-byte-p)))) + + (defthm-unsigned-byte-p ,(mk-name "N" size "-MV-NTH-0-" fn-name) + :bound ,size + :concl (mv-nth 0 (,fn-name dst src cnt input-rflags)) + :gen-type t + :gen-linear t) + + (defthm ,(mk-name "BOOLEANP-MV-NTH-1-" fn-name) + (booleanp (mv-nth 1 (,fn-name dst src cnt input-rflags)))) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name) + :bound 32 + :concl (mv-nth 2 (,fn-name dst src cnt input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p ,(mk-name "MV-NTH-3-" fn-name) + :bound 32 + :concl (mv-nth 3 (,fn-name dst src cnt input-rflags)) + :gen-type t + :gen-linear t)))) + +(make-event (shrd-spec-gen 16)) +(make-event (shrd-spec-gen 32)) +(make-event (shrd-spec-gen 64)) + +(define shrd-spec + ((size :type (member 2 4 8)) + dst + src + cnt + (input-rflags :type (unsigned-byte 32))) + :guard (and (unsigned-byte-p 6 cnt) + (case size + (2 (and (n16p dst) (n16p src))) + (4 (and (n32p dst) (n32p src))) + (8 (and (n64p dst) (n64p src))) + (otherwise nil))) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SHRD') instruction." + + (case size + (2 (shrd-spec-16 dst src cnt input-rflags)) + (4 (shrd-spec-32 dst src cnt input-rflags)) + (8 (shrd-spec-64 dst src cnt input-rflags)) + (otherwise (mv 0 nil 0 0))) + + /// + + (defthm natp-mv-nth-0-shrd-spec + (natp (mv-nth 0 (shrd-spec size dst src cnt input-rflags))) + :rule-classes :type-prescription) + + (defthm booleanp-of-mv-nth-1-shrd-spec + (booleanp (mv-nth 1 (shrd-spec size dst src cnt input-rflags)))) + + (defthm-unsigned-byte-p n32p-mv-nth-2-shrd-spec + :bound 32 + :concl (mv-nth 2 (shrd-spec size dst src cnt input-rflags)) + :gen-type t + :gen-linear t) + + (defthm-unsigned-byte-p n32p-mv-nth-3-shrd-spec + :bound 32 + :concl (mv-nth 3 (shrd-spec size dst src cnt input-rflags)) + :gen-type t + :gen-linear t)) + +;;;;;;;;;; SHLX: + +(define shlx-spec-gen ((size :type (member 32 64))) + :verify-guards nil + + (b* ((fn-name (mk-name "SHLX-SPEC-" size))) + + `(define ,fn-name + ;; There is no dst parameter because the result does not depend on it: + ;; the result is just stored in the destination operand. + ((src :type (unsigned-byte ,size)) + (cnt :type (unsigned-byte 6))) + + :parents (shlx-spec) + + (b* ((src (mbe :logic (n-size ,size src) + :exec src)) + (cnt (mbe :logic (n-size 6 cnt) + :exec cnt)) + + (raw-result (ash src cnt)) + (result (the (unsigned-byte ,size) (n-size ,size raw-result)))) + + result) + + :guard-hints (("Goal" :in-theory (disable unsigned-byte-p))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N" size "-" fn-name) + :bound ,size + :concl (,fn-name src cnt) + :gen-type t + :gen-linear t)))) + +; Only operand sizes 32 and 64 are suported. +(make-event (shlx-spec-gen 32)) +(make-event (shlx-spec-gen 64)) + +(define shlx-spec ((size :type (member 4 8)) + src + (cnt (unsigned-byte-p 6 cnt))) + :guard (case size + (4 (n32p src)) + (8 (n64p src)) + (otherwise nil)) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SHLX') instruction." + + :long + "Since this instruction does not affect the flags, + this function does not take or return flags.
" + + (case size + (4 (shlx-spec-32 src cnt)) + (8 (shlx-spec-64 src cnt)) + (otherwise 0)) + + /// + + (defthm natp-shlx-spec + (natp (shlx-spec size src cnt)) + :rule-classes :type-prescription)) + +;;;;;;;;;; SHRX: + +(define shrx-spec-gen ((size :type (member 32 64))) + :verify-guards nil + + (b* ((size+1 (1+ size)) + (fn-name (mk-name "SHRX-SPEC-" size))) + + `(define ,fn-name + ;; There is no dst parameter because the result does not depend on it: + ;; the result is just stored in the destination operand. + ((src :type (unsigned-byte ,size)) + (cnt :type (unsigned-byte 6))) + + :parents (shrx-spec) + + (b* ((src (mbe :logic (n-size ,size src) + :exec src)) + (cnt (mbe :logic (n-size 6 cnt) + :exec cnt)) + (neg-cnt (the (signed-byte ,size+1) (- cnt))) + + (raw-result (the (unsigned-byte ,size) + (ash (the (unsigned-byte ,size) src) + (the (signed-byte ,size+1) neg-cnt)))) + (result (the (unsigned-byte ,size) (n-size ,size raw-result)))) + + result) + + :guard-hints (("Goal" :in-theory (disable unsigned-byte-p))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N" size "-" fn-name) + :bound ,size + :concl (,fn-name src cnt) + :gen-type t + :gen-linear t)))) + +; Only operand sizes 32 and 64 are suported. +(make-event (shrx-spec-gen 32)) +(make-event (shrx-spec-gen 64)) + +(define shrx-spec ((size :type (member 4 8)) + src + (cnt (unsigned-byte-p 6 cnt))) + :guard (case size + (4 (n32p src)) + (8 (n64p src)) + (otherwise nil)) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SHRX') instruction." + + :long + "Since this instruction does not affect the flags, + this function does not take or return flags.
" + + (case size + (4 (shrx-spec-32 src cnt)) + (8 (shrx-spec-64 src cnt)) + (otherwise 0)) + + /// + + (defthm natp-shrx-spec + (natp (shrx-spec size src cnt)) + :rule-classes :type-prescription)) + +;;;;;;;;;; SARX: + +(define sarx-spec-gen ((size :type (member 32 64))) + :verify-guards nil + + (b* ((size+1 (1+ size)) + (size-1 (1- size)) + (neg-size-1 (- size-1)) + (fn-name (mk-name "SARX-SPEC-" size))) + + `(define ,fn-name + ;; There is no dst parameter because the result does not depend on it: + ;; the result is just stored in the destination operand. + ((src :type (unsigned-byte ,size)) + (cnt :type (unsigned-byte 6))) + + :parents (sarx-spec) + + (b* ((src (mbe :logic (n-size ,size src) + :exec src)) + (cnt (mbe :logic (n-size 6 cnt) + :exec cnt)) + (neg-cnt (the (signed-byte ,size+1) (- cnt))) + + (raw-result + (if (eql (mbe :logic (logbit ,size-1 src) + :exec (logand 1 + (the (unsigned-byte 1) + (ash (the (unsigned-byte ,size) + src) + ,neg-size-1)))) + 1) + ;; If the operand is negative, shift its logext, + ;; obtaining a non-positive value, which is then loghead'd. + (loghead ,size + (ash (mbe :logic (logext ,size src) + :exec (bitops::fast-logext ,size src)) + neg-cnt)) + ;; If the operand is non-negative, shift it directly, + ;; obtaining a non-negative value. + (the (unsigned-byte ,size) + (ash (the (unsigned-byte ,size) src) + (the (signed-byte ,size+1) neg-cnt))))) + (result (mbe :logic (n-size ,size raw-result) + :exec raw-result))) + + result) + + :guard-hints (("Goal" :in-theory (disable unsigned-byte-p))) + + :prepwork + ((local + (defthm verify-guards-lemma + (implies (unsigned-byte-p ,size src) + (equal (logand ,(1- (expt 2 size)) (logtail cnt src)) + (logtail cnt src))) + :hints (("Goal" + :do-not-induct t + :use ((:instance unsigned-byte-p-of-logtail + (size1 ,size) + (size cnt) + (i src))) + :in-theory (e/d* () + (unsigned-byte-p + unsigned-byte-p-of-logtail))))))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N" size "-" fn-name) + :bound ,size + :concl (,fn-name src cnt) + :gen-type t + :gen-linear t)))) + +; Only operand sizes 32 and 64 are suported. +(make-event (sarx-spec-gen 32)) +(make-event (sarx-spec-gen 64)) + +(define sarx-spec ((size :type (member 4 8)) + src + (cnt (unsigned-byte-p 6 cnt))) + :guard (case size + (4 (n32p src)) + (8 (n64p src)) + (otherwise nil)) + + :inline t + :no-function t + + :parents (instruction-semantic-functions) + + :short "Specification for the @('SARX') instruction." + + :long + "Since this instruction does not affect the flags, + this function does not take or return flags.
" + + (case size + (4 (sarx-spec-32 src cnt)) + (8 (sarx-spec-64 src cnt)) + (otherwise 0)) + + /// + + (defthm natp-sarx-spec + (natp (sarx-spec size src cnt)) + :rule-classes :type-prescription)) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/signextend.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/signextend.lisp new file mode 100644 index 000000000..745027bcd --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/signextend.lisp @@ -0,0 +1,137 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2018, Kestrel Technology, LLC +; 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+ This is the loop in the pseudocode of the ENTER instruction + (Intel manual, Jun 2026, Volume 2A): + when the nesting level is greater than 0, + after pushing the (old) frame pointer rBP, + @('NestingLevel - 1') further frame pointers are copied onto the stack, + by repeatedly decrementing @('frame-ptr') (initially rBP) + and reading the frame pointer at that stack address. + The @('count') argument is @('NestingLevel - 1'), + i.e. the number of frame pointers still to be copied. + The @('rsp') argument is the running stack pointer, + which is decremented and written to on each iteration. +
++ Per the ENTER specification, the operand size is + the size of each frame pointer copied by the loop, + i.e. the size of the data read and written by each iteration, + and thus also the amount by which + @('frame-ptr') and @('rsp') are decremented. +
" + (if (mbe :logic (zp count) :exec (eql count 0)) + (mv nil (the (signed-byte 64) rsp) x86) + (b* ( ;; frame-ptr := frame-ptr - operand-size + ((mv flg (the (signed-byte 64) frame-ptr)) + (add-to-*sp proc-mode frame-ptr (- operand-size) x86)) + ((when flg) (mv flg 0 x86)) + ;; read the frame pointer at [frame-ptr] + ((mv flg val x86) (rme-size-opt proc-mode + operand-size + frame-ptr + #.*ss* + :r + check-alignment? + x86 + :mem-ptr? nil)) + ((when flg) (mv flg 0 x86)) + ;; push it, i.e. rsp := rsp - operand-size and [rsp] := val + ((mv flg (the (signed-byte 64) rsp)) + (add-to-*sp proc-mode rsp (- operand-size) x86)) + ((when flg) (mv flg 0 x86)) + ((mv flg x86) (wme-size-opt proc-mode + operand-size + rsp + #.*ss* + val + check-alignment? + x86 + :mem-ptr? nil)) + ((when flg) (mv flg 0 x86))) + (x86-enter-copy-nested-frame-pointers proc-mode + (- count 1) + operand-size + frame-ptr + rsp + check-alignment? + x86))) + :measure (nfix count) + :guard-hints (("Goal" + :in-theory (e/d (rme-size-of-2-to-rme16 + rme-size-of-4-to-rme32 + rme-size-of-8-to-rme64) + ()) + :cases ((eql operand-size 2) + (eql operand-size 4) + (eql operand-size 8)))) + + /// + + ;; The returned stack pointer is a 64-bit signed integer. We generate the + ;; type-prescription and linear rules (in addition to the rewrite rule that + ;; :RETURNS provides), so that the guards of the caller (X86-ENTER), which + ;; passes the returned stack pointer to operations such as ADD-TO-*SP, can be + ;; verified. + (defthm-signed-byte-p + signed-byte-p-64-of-mv-nth-1-x86-enter-copy-nested-frame-pointers + :hyp (i64p rsp) + :bound 64 + :concl (mv-nth 1 (x86-enter-copy-nested-frame-pointers proc-mode + count + operand-size + frame-ptr + rsp + check-alignment? + x86)) + :gen-type t + :gen-linear t + :hints (("Goal" + :induct t + :in-theory (enable x86-enter-copy-nested-frame-pointers))))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; for speed: +(local (in-theory (disable acl2::member-of-cons member-equal + rgfi-size$inline))) + +(def-inst x86-enter + + ;; The ENTER instruction creates a stack frame for a procedure, + ;; which is later released by the LEAVE instruction (see X86-LEAVE). + + ;; Op/En: II + ;; C8 iw ib ENTER imm16, imm8 + ;; imm16 (the first operand) is the number of bytes reserved on the stack + ;; for the local variables of the procedure; imm8 (the second operand) is + ;; the lexical nesting level, whose low 5 bits are used (i.e. it is taken + ;; modulo 32). + + ;; See the pseudocode in the Intel manual, Jun 2026, Volume 2A, ENTER + ;; specification, as well as the helper function + ;; X86-ENTER-COPY-NESTED-FRAME-POINTERS above. + + ;; rBP is used at two different widths in this instruction (cf. the + ;; comments in X86-LEAVE about the roles of the operand size and of the + ;; stack address size). As data, rBP is operand-size wide: the Description + ;; section of the ENTER specification says that the OperandSize attribute + ;; determines the size of each frame pointer copied onto the stack, and + ;; "the data being transferred from SP/ESP/RSP register into the BP/EBP/RBP + ;; register"; accordingly, Push(rBP) below pushes the operand-size-wide + ;; rBP, and rBP := FrameTemp writes the low operand-size-wide part of + ;; FrameTemp into rBP. As an address, rBP is stack-address-size wide: the + ;; loop in the pseudocode decrements RBP, EBP, or BP according to + ;; StackSize (notably, the full RBP when StackSize = 64 and OperandSize = + ;; 16); accordingly, the base address from which the nested frame pointers + ;; are copied below is the stack-address-size-wide rBP. + + :parents (one-byte-opcodes) + + :guard-hints (("Goal" :in-theory (e/d (rme-size-of-2-to-rme16 + rme-size-of-4-to-rme32 + rme-size-of-8-to-rme64 + ;rme64 + ) + (;signed-byte-p + )))) + + :returns (x86 x86p :hyp (x86p x86)) + + :body + + (b* (((the (integer 2 8) operand-size) + (select-operand-size proc-mode nil rex-byte nil prefixes t t nil x86)) + + ;; Read the two immediate operands: + ;; the allocation size (imm16) and the nesting level (imm8). + ;; These are fetched from the instruction stream, so, like other code + ;; fetches, no alignment checking is done here. + + ((mv flg (the (unsigned-byte 16) alloc-size) x86) + (rme16-opt proc-mode temp-rip #.*cs* :x nil x86)) + ((when flg) (!!ms-fresh :alloc-size-read-error flg)) + ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip)) + (add-to-*ip proc-mode temp-rip 2 x86)) + ((when flg) + (!!fault-fresh :gp 0 :temp-rip-not-canonical temp-rip)) ;; #GP(0) + + ((mv flg (the (unsigned-byte 8) nesting-byte) x86) + (rme08-opt proc-mode temp-rip #.*cs* :x x86)) + ((when flg) (!!ms-fresh :nesting-level-read-error flg)) + ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip)) + (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) + (!!fault-fresh :gp 0 :temp-rip-not-canonical temp-rip)) ;; #GP(0) + + (badlength? (check-instruction-length start-rip temp-rip 0)) + ((when badlength?) + (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0) + + ;; NestingLevel := imm8 MOD 32. + ((the (integer 0 31) nesting-level) (loghead 5 nesting-byte)) + + (check-alignment? (alignment-checking-enabled-p x86)) + + ;; Push rBP. The value pushed is the operand-size-wide rBP; + ;; see the comments at the beginning of this function. + ((the (unsigned-byte 64) rbp) (rgfi-size operand-size *rbp* 0 x86)) + (rsp (read-*sp proc-mode x86)) + ((mv flg (the (signed-byte 64) rsp)) + (add-to-*sp proc-mode rsp (- operand-size) x86)) + ((when flg) (!!fault-fresh :ss 0 :push-rbp flg)) ;; #SS(0) + ((mv flg x86) (wme-size-opt proc-mode + operand-size + rsp + #.*ss* + rbp + check-alignment? + x86 + :mem-ptr? nil)) + ;; The COND below is analogous to the one after the nesting block + ;; further down (see the comment there about the possible errors), + ;; with two differences: + ;; the #SS case omits the error tags :NON-CANONICAL-STACK-ADDRESS + ;; and :OUT-OF-SEGMENT-STACK-ADDRESS, + ;; which can only arise from ADD-TO-*SP, + ;; whose failure for this push of rBP + ;; is already mapped to #SS just above; + ;; the #SS case also omits the error symbols RML16 etc., + ;; which can only arise from reads, while this is a write. + ((when flg) + (cond + ((or (and (consp flg) (eql (car flg) :segment-limit-fail)) + (member-eq flg '(wml16 wml32 wml64))) + (!!fault-fresh :ss 0 :stack-writing-error flg)) ;; #SS(0) + ((and (consp flg) (eql (car flg) :unaligned-linear-address)) + (!!fault-fresh :ac 0 :memory-access-unaligned flg)) ;; #AC(0) + (t ;; Unclassified error! + (!!ms-fresh :stack-writing-error flg)))) + + ;; FrameTemp := rSP, i.e. the stack pointer just after pushing rBP. + ((the (signed-byte 64) frame-temp) rsp) + + ;; If NestingLevel > 0, copy the NestingLevel - 1 nested frame pointers + ;; onto the stack (see X86-ENTER-COPY-NESTED-FRAME-POINTERS) and then + ;; push FrameTemp. If NestingLevel = 0, none of this is done: the + ;; pseudocode in the Intel manual jumps to the CONTINUE label below. + ((mv flg (the (signed-byte 64) rsp) x86) + (if (eql nesting-level 0) + (mv nil rsp x86) + (b* (;; The base address from which the nested frame pointers are + ;; copied is the stack-address-size-wide rBP; + ;; see the comments at the beginning of this function. + ((the (integer 2 8) stack-address-size) + (select-stack-address-size proc-mode x86)) + ((the (signed-byte 64) frame-ptr) + (i64 (rgfi-size stack-address-size *rbp* 0 x86))) + ((mv flg (the (signed-byte 64) rsp) x86) + (x86-enter-copy-nested-frame-pointers proc-mode + (1- nesting-level) + operand-size + frame-ptr + rsp + check-alignment? + x86)) + ((when flg) (mv flg rsp x86)) + ;; Push FrameTemp. + ((mv flg (the (signed-byte 64) rsp)) + (add-to-*sp proc-mode rsp (- operand-size) x86)) + ((when flg) (mv flg rsp x86)) + ((mv flg x86) (wme-size-opt proc-mode + operand-size + rsp + #.*ss* + (loghead (ash operand-size 3) + frame-temp) + check-alignment? + x86 + :mem-ptr? nil)) + ((when flg) (mv flg rsp x86))) + (mv nil rsp x86)))) + ;; In the COND below: + ;; the error tags :NON-CANONICAL-STACK-ADDRESS + ;; and :OUT-OF-SEGMENT-STACK-ADDRESS come from ADD-TO-*SP; + ;; the error tag :SEGMENT-LIMIT-FAIL comes from EA-TO-LA + ;; (via RME-SIZE and WME-SIZE), in 32-bit mode, + ;; for an access that starts within the stack segment limits + ;; but ends past them; + ;; the error symbols RML16 etc. and WML16 etc. + ;; come from the functions with those names + ;; (via RME-SIZE and WME-SIZE), in 64-bit mode, + ;; for an access that starts at a canonical address + ;; but ends past the canonical address space. + ;; The last two kinds of errors are possible because ADD-TO-*SP + ;; only checks the start address of an access. + ;; All the aforementioned errors are stack faults (#SS); + ;; an :UNALIGNED-LINEAR-ADDRESS error from RME-SIZE or WME-SIZE + ;; is instead an alignment check fault (#AC). + ((when flg) + (cond + ((or (and (consp flg) + (or (eql (car flg) :non-canonical-stack-address) + (eql (car flg) :out-of-segment-stack-address) + (eql (car flg) :segment-limit-fail))) + (member-eq flg '(rml16 rml32 rml64 wml16 wml32 wml64))) + (!!fault-fresh :ss 0 :enter-stack-error flg)) ;; #SS(0) + ((and (consp flg) (eql (car flg) :unaligned-linear-address)) + (!!fault-fresh :ac 0 :memory-access-unaligned flg)) ;; #AC(0) + (t ;; Unclassified error! + (!!ms-fresh :enter-error flg)))) + + ;; Update the x86 state: + + ;; Calculate rSP - AllocSize. + ;; We calculate the new rSP, but we write it a few lines below. + ;; We calculate this before writing to rBP because + ;; this calculation can cause a #SS fault, + ;; and according to Intel manual, Jun 2026, Vol. 3, Section 7.5, + ;; a fault must leave the processor state + ;; in the same state as at the start of + ;; the instruction that caused the fault; + ;; Table 7-1 in Chapter 7 classifies #SS as a fault. + ((mv flg (the (signed-byte 64) rsp)) + (add-to-*sp proc-mode rsp (- alloc-size) x86)) + ((when flg) (!!fault-fresh :ss 0 :alloc-error flg)) ;; #SS(0) + + ;; rBP := FrameTemp. + ;; We use !rgfi-size (rather than !rgfi) since it expects an unsigned + ;; value, so we take the low bits of FrameTemp accordingly. + (x86 (!rgfi-size operand-size *rbp* + (loghead (ash operand-size 3) + frame-temp) + rex-byte x86)) + ;; rSP := rSP - AllocSize. + ;; The calculation was done above, for the reason explained above. + (x86 (write-*sp proc-mode rsp x86)) + + (x86 (write-*ip proc-mode temp-rip x86))) + + x86)) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/top.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/top.lisp new file mode 100644 index 000000000..f49f887b9 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/top.lisp @@ -0,0 +1,167 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2026, Kestrel Technology, LLC +; 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 GoelThe instruction semantic functions have dual roles:
+ +Each semantic function takes, among other arguments, the value +@('start-rip') of the instruction pointer at the beginning of the +instruction, and the value @('temp-rip') of the instruction pointer +after the decoding of the prefixes, REX byte, opcode, ModR/M byte, and +SIB byte (some of these bytes may not be present). The semantic +function may further increment the instruction pointer beyond +@('temp-rip') to read the ending bytes of the instruction, e.g. to +read a displacement or an immediate. The semantic function eventually +writes the final value of the instruction pointer into RIP.
") + +;; ---------------------------------------------------------------------- + +(in-theory (e/d () (rip-guard-okp))) + +;; ====================================================================== + +;; To see the rules in the instruction-decoding-and-spec-rules +;; ruleset: + +(define show-inst-decoding-and-spec-fn + ((state)) + :mode :program + (let ((world (w state))) + (ruleset-theory 'instruction-decoding-and-spec-rules))) + +(defmacro show-inst-decoding-and-spec-ruleset () + `(show-inst-decoding-and-spec-fn state)) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/xor-spec.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/xor-spec.lisp new file mode 100644 index 000000000..b3bfe69fe --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/instructions/xor-spec.lisp @@ -0,0 +1,147 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; 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 GoelFirst, a quick note about virtual, linear, and physical + addresses:
+ +The functions @('rb') (read bytes) and @('wb') (write + bytes) are used in reasoning about memory reads and writes. Functions + like @('rml08'), @('rml16'), @('rml32'), and @('rml64') are reduced to + @('rb'), and @('wml08'), @('wml16'), @('wml32'), and @('wml64') to + @('wb') during reasoning.
" + + (local (xdoc::set-default-parents reasoning-about-memory-reads-and-writes)) + + (define canonical-address-listp (lst) + :short "Recognizer of a list of canonical addresses" + :enabled t + (if (equal lst nil) + t + (and (consp lst) + (canonical-address-p (car lst)) + (canonical-address-listp (cdr lst)))) + /// + (defthm cdr-canonical-address-listp + (implies (canonical-address-listp x) + (canonical-address-listp (cdr x))))) + + (define create-canonical-address-list (count addr) + :guard (natp count) + + :long "Given a canonical address @('addr'), + @('create-canonical-address-list') creates a list of canonical + addresses where the first address is @('addr') and the last address + is the last canonical address in the range @('addr') to @('addr + + count').
" + :enabled t + + (if (or (zp count) + (not (canonical-address-p addr))) + nil + (cons addr (create-canonical-address-list (1- count) + (1+ addr)))) + /// + + (defthm true-listp-create-canonical-address-list + (true-listp (create-canonical-address-list cnt lin-addr)) + :rule-classes (:rewrite :type-prescription)) + + (defthm canonical-address-listp-create-canonical-address-list + (canonical-address-listp + (create-canonical-address-list count addr)) + :rule-classes (:rewrite :type-prescription)) + + (defthm create-canonical-address-list-1 + (implies (canonical-address-p x) + (equal (create-canonical-address-list 1 x) + (list x))) + :hints (("Goal" :expand (create-canonical-address-list 1 x)))) + + (defthm len-of-create-canonical-address-list + (implies (and (canonical-address-p (+ -1 addr count)) + (canonical-address-p addr) + (natp count)) + (equal (len (create-canonical-address-list count addr)) + count))) + + (defthm car-create-canonical-address-list + (implies (and (canonical-address-p addr) + (posp count)) + (equal (car (create-canonical-address-list count addr)) + addr))) + + (defthm cdr-create-canonical-address-list + (implies (and (canonical-address-p addr) + (posp count)) + (equal (cdr (create-canonical-address-list count addr)) + (create-canonical-address-list (1- count) (1+ addr))))) + + (defthm consp-of-create-canonical-address-list + (implies (and (canonical-address-p addr) + (natp count) + (< 0 count)) + (consp (create-canonical-address-list count addr))) + :hints (("Goal" :in-theory (e/d (canonical-address-p + signed-byte-p) + ())))) + + (defthmd create-canonical-address-list-split + (implies (and (canonical-address-p addr) + (canonical-address-p (+ k addr)) + (natp j) + (natp k)) + (equal (create-canonical-address-list (+ k j) addr) + (append (create-canonical-address-list k addr) + (create-canonical-address-list j (+ k addr))))) + :hints (("Goal" :in-theory (e/d* (canonical-address-p signed-byte-p) + ()))))) + + (local + (defthm ash-n-3-minus-8-lemma + (implies (posp n) + (equal (+ -8 (ash n 3)) + (ash (+ -1 n) 3))) + :hints (("goal" :in-theory (e/d* (bitops::ihsext-inductions + bitops::ihsext-recursive-redefs + ash floor) + ()))))) + + (local + (defthm unsigned-byte-p-of-ash-x-8-lemma + (implies + (and (unsigned-byte-p (ash (+ -1 n) 3) x) + (< 0 n) + (integerp n)) + (unsigned-byte-p (ash n 3) (ash x 8))) + :hints (("goal" :in-theory (e/d () (unsigned-byte-p-of-ash)) + :use ((:instance unsigned-byte-p-of-ash + (n (ash n 3)) + (x x) + (m 8))))))) + + (local + (defthm byte-width-unsigned-byte-p-ash-incr + (implies (and (unsigned-byte-p 8 b) + (posp n)) + (unsigned-byte-p (ash n 3) b)) + :hints (("Goal" + :use ((:instance bitops::unsigned-byte-p-incr + (a 8) (x b) (b (ash n 3)))) + :in-theory (e/d (ash floor) + (bitops::unsigned-byte-p-incr)))))) + + (local + (defthm unsigned-byte-p-of-logior-ash-x-8-lemma + (implies + (and (unsigned-byte-p (ash (+ -1 n) 3) x) + (unsigned-byte-p 8 b) + (< 0 n) + (integerp n)) + (unsigned-byte-p (ash n 3) (logior b (ash x 8)))) + :hints (("goal" :in-theory (e/d () + (unsigned-byte-p-of-logior + ash-n-3-minus-8-lemma + byte-width-unsigned-byte-p-ash-incr)) + :use ((:instance byte-width-unsigned-byte-p-ash-incr) + (:instance unsigned-byte-p-of-logior + (n (ash n 3)) + (i b) + (j (ash x 8)))))))) + + (define rb-1 ((n natp "Number of bytes to be read") + (addr integerp "First linear address") + (r-x :type (member :r :x) "Type of memory access") + (x86)) + :verify-guards :after-returns + :returns + (mv flg + (val natp :rule-classes :type-prescription) + (new-x86 x86p :hyp (x86p x86))) + + :guard (canonical-address-p (+ -1 n addr)) + :enabled t + + (if (zp n) + (mv nil 0 x86) + ;; rb-1 is used only in the app-level view so it makes sense to + ;; use rvm08 here. + (b* (((unless (canonical-address-p addr)) + (mv 'rb-1 0 x86)) + ((mv flg0 byte0 x86) + (rvm08 addr x86)) + ((when flg0) + (mv flg0 0 x86)) + ((mv rest-flg rest-bytes x86) + (rb-1 (1- n) (1+ addr) r-x x86))) + (mv rest-flg (logior byte0 (ash rest-bytes 8)) x86))) + + /// + + (defret integerp-of-mv-nth-1-rb-1 + (integerp val) + :rule-classes :type-prescription) + + (defret rb-1-returns-x86 + (implies t ;; (app-view x86) + (equal new-x86 x86))) + + (local + (defthm rb-1-returns-no-error-helper + (implies t ;; (xr :app-view nil x86) + (not (mv-nth 0 (rb-1 1 #.*2^47-1* r-x x86)))) + :hints (("Goal" :expand (rb-1 1 #.*2^47-1* r-x x86))))) + + (defthm rb-1-returns-no-error + (implies (and (canonical-address-p addr) + (canonical-address-p (+ -1 n addr)) + ;; (app-view x86) + ) + (equal (mv-nth 0 (rb-1 n addr r-x x86)) nil)) + :hints (("Goal" :in-theory (e/d (rvm08) ())))) + + (defthm-unsigned-byte-p size-of-rb-1 + :hyp (and (equal m (ash n 3)) (natp n)) + :bound m + :concl (mv-nth 1 (rb-1 n addr r-x x86)) + :hints (("Goal" :in-theory (e/d* (rvm08) (unsigned-byte-p signed-byte-p)))) + :gen-linear t) + + (encapsulate () + (local (include-book "kestrel/arithmetic-light/floor" :dir :system)) + (defthm unsigned-byte-p-of-mv-nth-1-of-rb-1 + (implies (and (<= (* 8 n) size) + (natp size)) + (unsigned-byte-p size (mv-nth 1 (rb-1 n addr r-x x86)))) + :hints (("Goal" :use (:instance size-of-rb-1 (m (ash n 3))) + :in-theory (e/d (ash) (size-of-rb-1)))))) + ) + + (define las-to-pas + ((n natp "Number of bytes to be read") + (lin-addr integerp "First linear address") + (r-w-x :type (member :r :w :x) "Type of memory access: read, write, or execute") + x86) + :enabled t + :guard (not (app-view x86)) ;; Not x86 "flat" mode or ``app(lication) mode'' + :verify-guards nil + ;; It'd be sweet if las-to-pas returned the following instead of a + ;; list of physical addresses:We use @('program-at') to state that a program, given by + as a list of @('bytes'), is located at contiguous addresses from + @('prog-addr') to @('(len bytes) + prog-addr - 1') in the memory of + the x86 state. Note that this function is non-executable; we use it + only for reasoning about machine-code.
" + :guard (and (canonical-address-p prog-addr) + (canonical-address-p (+ -1 (len bytes) prog-addr)) + (byte-listp bytes)) + + (b* (((mv flg bytes-read ?x86) + (rb (len bytes) prog-addr :x x86))) + (and + (equal flg nil) + + ;; (combine-n-bytes 2 3 '(#xAA #xBB #xCC #xDD #xEE)) + ;; == + ;; (part-select (combine-bytes '(#xAA #xBB #xCC #xDD #xEE)) + ;; :low (ash 2 3) :width (ash 3 3)) + + ;; I prefer combine-n-bytes here instead of combine-bytes + ;; because combine-n-bytes lets me think of subseqs of a list + ;; in the same way I think about slices of a number a la + ;; part-select. + (equal bytes-read (combine-n-bytes 0 (len bytes) bytes)))) + + /// + + (defthm program-at-xw-in-app-view + (implies (and (app-view x86) + (not (equal fld :mem)) + (not (equal fld :app-view))) + (equal (program-at addr bytes (xw fld index value x86)) + (program-at addr bytes x86))) + :hints (("Goal" :in-theory (e/d* () (rb))))))) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/modes.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/modes.lisp new file mode 100644 index 000000000..14b65d92a --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/modes.lisp @@ -0,0 +1,200 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2018, Kestrel Technology, LLC +; 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): +; Alessandro Coglio+ For now we do not model in detail all the processor modes and transitions + (see Figure 2-3 in Intel Volume 3A). + We implicitly assume that the processor + is always in one of the following modes: +
++ No real-address mode, virtual-8086 mode, or system management mode for now. +
") + +(local (xdoc::set-default-parents x86-modes)) + +;; ---------------------------------------------------------------------- + +(define 64-bit-modep (x86) + :parents (x86-modes) + :short "Check whether we are in 64-bit mode." + :long + "+ Given the modeling assumption stated in @(see x86-modes), + this predicate discriminates between + 64-bit mode and the other two modes (collectively, 32-bit mode). + Based on Intel manual, Mar'17, Vol. 3A, Sec. 2.2 (near Fig. 2-3), + the discrimination is based on the IA32_EFER.LME and CS.L bits: + if they are both 1, we are in 64-bit mode, + otherwise we are in 32-bit mode + (protected mode if IA32_EFER.LME is 0, + compatibility mode if IA32_EFER.LME is 1 and CS.L is 0; + note that when IA32_EFER.LME is 0, CS.L should be 0, + according to Intel manual, Mar'17, Vol. 3A, Sec. 3.4.5). +
++ This predicate does not include state invariants such as + the constraints imposed by the 64-bit mode consistency checks + described in Intel manual, Mar'17, Vol. 3A, Sec. 9.8.5. +
++ This predicate is useful as a hypothesis of theorems + about either 64-bit or 32-bit mode. +
++ Since @('(xr :msr ... x86)') returns a 64-bit value + but the IA32_EFER register consists of 12 bits. + So we use @(tsee n12) to make @('ia32_eferBits') functions applicable. +
" + + :no-function t + :guard-hints (("Goal" :in-theory (e/d (bitsets::bignum-extract) (x86p)))) + (mbe + :logic + (b* ((ia32_efer (n12 (msri #.*ia32_efer-idx* x86))) + (ia32_efer.lma (ia32_eferBits->lma ia32_efer)) + (cs-attr (seg-hidden-attri #.*cs* x86)) + (cs.l (code-segment-descriptor-attributesBits->l cs-attr))) + (and (equal ia32_efer.lma 1) + (equal cs.l 1))) + :exec + ;; [Shilpi] During execution, include the following book for efficiency (to + ;; decrease the bytes allocated on the heap because of all the bignum + ;; operations). This is likely most efficient on CCL. + ;; (include-book "std/bitsets/bignum-extract-opt" :dir :system) + ;; Note that this book requires a trust tag. + (b* (((the (unsigned-byte 32) ia32_efer-low-32) + (bitsets::bignum-extract + (msri #.*ia32_efer-idx* x86) + 0)) + ((the (unsigned-byte 12) ia32_efer) + (mbe :logic (n12 ia32_efer-low-32) + :exec (logand #xFFF (the (unsigned-byte 32) ia32_efer-low-32)))) + (ia32_efer.lma (ia32_eferBits->lma ia32_efer)) + ((the (unsigned-byte 16) cs-attr) + (seg-hidden-attri #.*cs* x86)) + (cs.l (code-segment-descriptor-attributesBits->l cs-attr))) + (and (equal ia32_efer.lma 1) + (equal cs.l 1)))) + /// + + (local (in-theory (e/d () (force (force))))) + + (defrule 64-bit-modep-of-xw ; contributed by Eric Smith + (implies (and (not (equal fld :msr)) + (not (equal fld :seg-hidden-attr))) + (equal (64-bit-modep (xw fld index value x86)) + (64-bit-modep x86)))) + + ;; (defrule 64-bit-modep-of-!flgi ; contributed by Eric Smith + ;; (equal (64-bit-modep (!flgi flag val x86)) + ;; (64-bit-modep x86))) + + ;; (defrule 64-bit-modep-of-!flgi-undefined + ;; (equal (64-bit-modep (!flgi-undefined flg x86)) + ;; (64-bit-modep x86)) + ;; :enable !flgi-undefined) + + (defrule 64-bit-modep-of-write-user-rflags + (equal (64-bit-modep (write-user-rflags vector mask x86)) + (64-bit-modep x86)))) + +;; ---------------------------------------------------------------------- + +(define x86-operation-mode (x86) + :short "Returns the current mode of operation of the x86 machine" + :long + "We only support 64-bit, Compatibility, and 32-bit Protected Modes + for now.
+ +See @(see x86-modes).
" + :parents (x86-modes) + :returns (mode natp :rule-classes (:type-prescription :rewrite)) + (cond ((64-bit-modep x86) #.*64-bit-mode*) + ;; TODO: Other modes of operation + (t #.*compatibility-mode*)) + + /// + + (defret range-of-x86-operation-mode + (and (<= 0 mode) + (< mode #.*num-proc-modes*)) + :rule-classes :linear) + + (defrule x86-operation-mode-of-xw + (implies (and (not (equal fld :msr)) + (not (equal fld :seg-hidden-attr))) + (equal (x86-operation-mode (xw fld index value x86)) + (x86-operation-mode x86)))) + + ;; (defrule x86-operation-mode-of-!flgi + ;; (equal (x86-operation-mode (!flgi flag val x86)) + ;; (x86-operation-mode x86))) + + ;; (defrule x86-operation-mode-of-!flgi-undefined + ;; (equal (x86-operation-mode (!flgi-undefined flg x86)) + ;; (x86-operation-mode x86)) + ;; :enable !flgi-undefined) + + (defrule x86-operation-mode-of-write-user-rflags + (equal (x86-operation-mode (write-user-rflags vector mask x86)) + (x86-operation-mode x86)))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/physical-memory.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/physical-memory.lisp new file mode 100644 index 000000000..0431d4fd3 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/physical-memory.lisp @@ -0,0 +1,671 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; 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): +; Robert KrugNote that the top-level memory accessor function is @(see + rme08) and updater function is @(see wme08). Their 16, 32, 64, and + 128 bit counterparts are also available. These functions behave + differently depending upon the value of + @('app-view').
+ +The functions defined here, like @(see rm-low-32) and @(see +wm-low-32), are low-level read and write functions that access +physical memory directly in the system-level view. We do not +recommend using these functions at the top-level.
") + +(local (xdoc::set-default-parents physical-memory)) + +;; ====================================================================== + +(local (include-book "centaur/bitops/ihs-extensions" :dir :system)) +(local (include-book "centaur/bitops/width-find-rule" :dir :system)) +(local (include-book "centaur/bitops/equal-by-logbitp" :dir :system)) + +;; ====================================================================== + +(define physical-address-p (phy-addr) + :parents (physical-memory) + :inline t + :no-function t + :enabled t + (mbe :logic (unsigned-byte-p #.*physical-address-size* phy-addr) + :exec (and (integerp phy-addr) + (<= 0 phy-addr) + (< phy-addr #.*mem-size-in-bytes*)))) + +(define physical-address-listp (lst) + :parents (physical-memory) + :short "Recognizer of a list of physical addresses" + :enabled t + (if (equal lst nil) + t + (and (consp lst) + (physical-address-p (car lst)) + (physical-address-listp (cdr lst))))) + +(define create-physical-address-list (count addr) + + :guard (and (natp count) + (physical-address-p addr) + (physical-address-p (+ addr count))) + :parents (physical-memory) + + :long "Given a physical address @('addr'), + @('create-physical-address-list') creates a list of physical + addresses where the first address is @('addr') and the last address + is the last physical address in the range @('addr') to @('addr + + count').
" + :enabled t + + (if (or (zp count) + (not (physical-address-p addr))) + nil + (cons addr (create-physical-address-list (1- count) + (1+ addr)))) + /// + + (defthm physical-address-listp-create-physical-address-list + (physical-address-listp + (create-physical-address-list count addr)) + :rule-classes (:rewrite :type-prescription))) + +(define addr-range (count addr) + :guard (natp count) + + :enabled t + + (if (zp count) + nil + ;; The ifix below is intentional --- it allows addr-range to be + ;; used with both integer (canonical addresses, modeled as + ;; signed-byte-p) as well as natp (physical addresses, modeled as + ;; unsigned-byte-p) addresses. + (cons (ifix addr) + (addr-range (1- count) (1+ (ifix addr))))) + + /// + + (defthm neg-addr-range=nil + (implies (negp i) (equal (addr-range i n) nil))) + + (defthm true-listp-addr-range + (true-listp (addr-range count addr)) + :rule-classes :type-prescription) + + (defthm addr-range-1 + (equal (addr-range 1 x) + (list (ifix x))) + :hints (("Goal" :expand (addr-range 1 x)))) + + (defthm len-of-addr-range + (implies (natp n) + (equal (len (addr-range n val)) n)) + :hints (("Goal" :in-theory (e/d (addr-range) ())))) + + (defthm physical-address-listp-addr-range + (implies (and (physical-address-p lin-addr) + (physical-address-p (+ -1 n lin-addr))) + (physical-address-listp (addr-range n lin-addr))) + :hints (("Goal" :in-theory (e/d (addr-range) ())))) + + (defthm member-p-addr-range + (implies (and (<= prog-addr addr) + (< addr (+ n prog-addr)) + (integerp n) + (integerp addr) + (integerp prog-addr)) + (equal (member-p addr (addr-range n prog-addr)) + t))) + + (defthm member-p-addr-range-from-member-p-addr-range + (implies (and (member-p x (addr-range j y)) + (integerp l) + (< j l)) + (equal (member-p x (addr-range l y)) + t))) + + (defthm not-member-p-addr-range + (implies (and (or (< addr prog-addr) + (<= (+ n prog-addr) addr)) + (integerp prog-addr)) + (equal (member-p addr (addr-range n prog-addr)) + nil))) + + (defthm not-member-p-addr-range-from-not-member-p-addr-range + (implies (and (not (member-p x (addr-range j y))) + (integerp j) + (<= l j)) + (equal (member-p x (addr-range l y)) + nil))) + + (defthm subset-p-two-addr-ranges + (implies (and (integerp x) + (integerp y) + (<= y x) + (<= (+ i x) (+ j y)) + (integerp j)) + (subset-p (addr-range i x) + (addr-range j y))) + :hints (("Goal" :in-theory (e/d (subset-p) nil)))) + + (defthm not-disjoint-p-two-addr-ranges-thm + (implies (and (integerp x) + (integerp y) + (integerp i) + (integerp j) + (<= x y) + (< y (+ i x)) + (<= (+ i x) (+ j y))) + (equal (disjoint-p (addr-range i x) + (addr-range j y)) + nil)) + :hints (("Goal" :in-theory (e/d (disjoint-p member-p) ())))) + + (defthm disjoint-p-two-addr-ranges-thm-0 + (implies (and (integerp x) + (integerp y) + (integerp j) + (< 0 j) + (<= (+ i x) y)) + (disjoint-p (addr-range i x) + (addr-range j y))) + :hints (("Goal" :in-theory (e/d (disjoint-p member-p) ())))) + + (defthm disjoint-p-two-addr-ranges-thm-1 + (implies (and (integerp x) + (integerp y) + (integerp j) + (< 0 j) + (<= (+ j y) x)) + (disjoint-p (addr-range i x) + (addr-range j y))) + :hints (("Goal" :in-theory (e/d (disjoint-p member-p) ())))) + + ;; (defthm disjoint-p-two-addr-ranges-thm-2 + ;; (implies (and (disjoint-p (addr-range i x) + ;; (addr-range j y)) + ;; (integerp i) + ;; (integerp j) + ;; (<= k i) + ;; (<= l j)) + ;; (disjoint-p (addr-range k x) + ;; (addr-range l y))) + ;; :hints (("Goal" :in-theory (e/d (disjoint-p member-p) ())))) + + (defthm disjoint-p-two-addr-ranges-thm-2 + (implies (and (disjoint-p (addr-range i x) (addr-range j y)) + (subset-p (addr-range ia a) (addr-range i x)) + (subset-p (addr-range jb b) (addr-range j y))) + (disjoint-p (addr-range ia a) (addr-range jb b))) + :hints (("Goal" :in-theory (e/d (disjoint-p member-p) ())))) + + (defthm disjoint-p-two-addr-ranges-thm-3 + (implies (and (disjoint-p (addr-range j y) (addr-range i x)) + (subset-p (addr-range ia a) (addr-range i x)) + (subset-p (addr-range jb b) (addr-range j y))) + (disjoint-p (addr-range ia a) (addr-range jb b))) + :hints (("Goal" :use ((:instance disjoint-p-commutative + (a (addr-range j y)) + (b (addr-range i x))))))) + + (defthm consp-addr-range + (implies (posp n) + (consp (addr-range n val))) + :rule-classes (:type-prescription :rewrite)) + + (defthm car-addr-range + (implies (posp n) + (equal (car (addr-range n val)) + (ifix val)))) + + (defthm cdr-addr-range + (implies (and (posp n) + (integerp val)) + (equal (cdr (addr-range n val)) + (addr-range (1- n) (1+ val))))) + + (defthm no-duplicates-p-and-addr-range + (no-duplicates-p (addr-range n x)) + :hints (("Goal" :in-theory (e/d* (member-p) ())))) + + (defthm instance-of-pos-1-accumulator-thm + (implies (and (member-p e x) + (natp acc)) + (equal (pos-1 e x (+ 1 acc)) + (+ 1 (pos-1 e x acc))))) + + ;; (defthm nth-pos-of-addr-range-first + ;; (implies (and (integerp index) + ;; (posp n)) + ;; (equal (pos index (addr-range n index)) 0)) + ;; :hints (("Goal" :in-theory (e/d* (pos) ())))) + + (defthm nth-pos-of-addr-range + (implies (and (<= index i) (< i (+ n index)) + (integerp index) (integerp i) (posp n)) + (equal (pos i (addr-range n index)) + (- i index))) + :hints (("Goal" :in-theory (e/d* (pos) ())))) + + (defthmd addr-range-ifix + (equal (addr-range n (ifix x)) (addr-range n x))) + + (defthm integerp-addr-range-member + (implies (member-p x (addr-range n m)) + (integerp x))) + + (defthm addr-range-not-member-out-of-range + (implies (and (natp n) + (integerp addr) + (< el addr) + (>= el (+ addr n))) + (not (member-p el (addr-range n addr)))))) + +;; ====================================================================== + +;; Memory read functions: + +;; These functions are very similar to their linear memory +;; counterparts, rvm* --- they just differ in guards and the number of +;; return values. + +;; Memory write functions: + +;; These functions are very similar to their linear memory +;; counterparts wvm* --- they differ in guards and the number of +;; return values. + +;; ---------------------------------------------------------------------- + +(local (in-theory (e/d () (unsigned-byte-p)))) +(local (in-theory (e/d (bitops::unsigned-byte-p-by-find-rule) ()))) + +(define rm-low-32 + ((addr :type (unsigned-byte #.*physical-address-size*)) + (x86)) + :guard (and (not (app-view x86)) + (integerp addr) + (<= 0 addr) + (< (+ 3 addr) *mem-size-in-bytes*)) + :inline t + :no-function t + :parents (physical-memory) + + (if (mbt (not (app-view x86))) + + (let ((addr (mbe :logic (ifix addr) + :exec addr))) + + (b* (((the (unsigned-byte 8) byte0) (mbe :logic (loghead 8 (memi addr x86)) + :exec (memi addr x86))) + ((the (unsigned-byte 8) byte1) (mbe :logic (loghead 8 (memi (1+ addr) x86)) + :exec (memi (1+ addr) x86))) + ((the (unsigned-byte 16) word0) + (logior (the (unsigned-byte 16) (ash byte1 8)) byte0)) + ((the (unsigned-byte 8) byte2) (mbe :logic (loghead 8 (memi (+ 2 addr) x86)) + :exec (memi (+ 2 addr) x86))) + ((the (unsigned-byte 8) byte3) (mbe :logic (loghead 8 (memi (+ 3 addr) x86)) + :exec (memi (+ 3 addr) x86))) + ((the (unsigned-byte 16) word1) + (the (unsigned-byte 16) (logior (the (unsigned-byte 16) (ash byte3 8)) byte2))) + ((the (unsigned-byte 32) dword) (logior (the (unsigned-byte 32) (ash word1 16)) word0))) + dword)) + 0) + + + /// + + (defthm rm-low-32-in-app-view + (implies (app-view x86) + (equal (rm-low-32 p-addr x86) 0))) + + (defthm-unsigned-byte-p n32p-rm-low-32 + :bound 32 + :concl (rm-low-32 addr x86) + :hints (("Goal" :in-theory (e/d () (force (force))))) + :gen-linear t + :gen-type t) + + (defthm rm-low-32-xw + (implies (and (not (equal fld :mem)) + (not (equal fld :app-view))) + (equal (rm-low-32 addr (xw fld index val x86)) + (rm-low-32 addr x86))) + :hints (("Goal" :in-theory (e/d* (rm-low-32) (force (force))))))) + +(define wm-low-32 + ((addr :type (unsigned-byte #.*physical-address-size*)) + (val :type (unsigned-byte 32)) + (x86)) + :inline t + :no-function t + :guard (and (not (app-view x86)) + (< (+ 3 addr) *mem-size-in-bytes*)) + :guard-hints (("Goal" :in-theory (e/d (logtail) ()))) + :parents (physical-memory) + + (if (mbt (not (app-view x86))) + + (let ((addr (mbe :logic (ifix addr) + :exec addr))) + + (b* (((the (unsigned-byte 8) byte0) + (mbe :logic (part-select val :low 0 :width 8) + :exec (logand #xff val))) + ((the (unsigned-byte 8) byte1) + (mbe :logic (part-select val :low 8 :width 8) + :exec (logand #xff (ash val -8)))) + ((the (unsigned-byte 8) byte2) + (mbe :logic (part-select val :low 16 :width 8) + :exec (logand #xff (ash val -16)))) + ((the (unsigned-byte 8) byte3) + (mbe :logic (part-select val :low 24 :width 8) + :exec (logand #xff (ash val -24)))) + (x86 (!memi addr byte0 x86)) + (x86 (!memi (+ 1 addr) byte1 x86)) + (x86 (!memi (+ 2 addr) byte2 x86)) + (x86 (!memi (+ 3 addr) byte3 x86))) + x86)) + + x86) + + /// + + (defthm x86p-wm-low-32 + (implies (x86p x86) + (x86p (wm-low-32 addr val x86))) + :rule-classes (:rewrite :type-prescription)) + + (defthm xr-wm-low-32 + (implies (not (equal fld :mem)) + (equal (xr fld index (wm-low-32 addr val x86)) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wm-low-32) (force (force)))))) + + (defthm wm-low-32-xw + (implies (and (not (equal fld :mem)) + (not (equal fld :app-view))) + (equal (wm-low-32 addr val (xw fld index value x86)) + (xw fld index value (wm-low-32 addr val x86)))) + :hints (("Goal" :in-theory (e/d* (wm-low-32) (force (force))))))) + + +; MattK mod for ACL2(p): Hint LOGBITP-REASONING modifies state: +; (set-waterfall-parallelism nil) + +(defthmd put-logior-4-bytes-together + (implies (unsigned-byte-p 32 val) + (equal (logior (loghead 8 val) + (ash (loghead 8 (logtail 8 val)) 8) + (ash (logior (loghead 8 (logtail 16 val)) + (ash (logtail 24 val) 8)) + 16)) + val)) + :hints ((bitops::logbitp-reasoning))) + +;; rm-low-32/wm-low-32/xw RoW: + +(defthm |(rm-low-32 addr2 (wm-low-32 addr1 val x86)) --- same addr| + (implies (and (equal addr1 addr2) + (force (n32p val)) + (not (app-view x86))) + (equal (rm-low-32 addr2 (wm-low-32 addr1 val x86)) + val)) + :hints (("Goal" :in-theory (e/d (rm-low-32 wm-low-32 nfix ifix put-logior-4-bytes-together) + (unsigned-byte-p force (force)))))) + +(defthm |(rm-low-32 addr2 (wm-low-32 addr1 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 4 addr1) + (addr-range 4 addr2)) + (equal (rm-low-32 addr2 (wm-low-32 addr1 val x86)) + (rm-low-32 addr2 x86))) + :hints (("Goal" :in-theory (e/d (rm-low-32 wm-low-32 nfix ifix) + (unsigned-byte-p force (force)))))) + +(defthm |(rm-low-32 addr2 (xw :mem addr1 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 1 addr1) + (addr-range 4 addr2)) + (equal (rm-low-32 addr2 (xw :mem addr1 val x86)) + (rm-low-32 addr2 x86))) + :hints (("Goal" :in-theory (e/d (rm-low-32) (force (force)))))) + +(defthm |(xr :mem addr1 (wm-low-32 addr2 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 4 addr2) + (addr-range 1 addr1)) + (equal (xr :mem addr1 (wm-low-32 addr2 val x86)) + (xr :mem addr1 x86))) + :hints (("Goal" :in-theory (e/d (nfix wm-low-32) (force (force)))))) + +;; wm-low-32 WoW: + +(defthm |(wm-low-32 addr2 val2 (wm-low-32 addr1 val1 x86)) --- same addr| + (implies (equal addr1 addr2) + (equal (wm-low-32 addr2 val2 (wm-low-32 addr1 val1 x86)) + (wm-low-32 addr2 val2 x86))) + :hints (("Goal" :in-theory (e/d (wm-low-32 nfix ifix) (force (force)))))) + +(defthm |(wm-low-32 addr2 val2 (wm-low-32 addr1 val1 x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 4 addr1) + (addr-range 4 addr2)) + (equal (wm-low-32 addr2 val2 (wm-low-32 addr1 val1 x86)) + (wm-low-32 addr1 val1 (wm-low-32 addr2 val2 x86)))) + :hints (("Goal" :in-theory (e/d* (wm-low-32) ()))) + :rule-classes ((:rewrite :loop-stopper ((addr2 addr1))))) + +(defthm |(xw :mem addr1 (wm-low-32 addr2 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 4 addr2) + (addr-range 1 addr1)) + (equal (wm-low-32 addr2 val2 (xw :mem addr1 val1 x86)) + (xw :mem addr1 val1 (wm-low-32 addr2 val2 x86)))) + :hints (("Goal" :in-theory (e/d (nfix ifix wm-low-32) (force (force)))))) + +;; ====================================================================== + +(define rm-low-64 + ((addr :type (unsigned-byte #.*physical-address-size*)) + (x86)) + :guard (and (not (app-view x86)) + (integerp addr) + (<= 0 addr) + (< (+ 7 addr) *mem-size-in-bytes*)) + :guard-hints (("Goal" :in-theory (e/d (unsigned-byte-p) + (rm-low-32 force (force))))) + :parents (physical-memory) + + (if (mbt (not (app-view x86))) + + (b* ((addr (mbe :logic (ifix addr) :exec addr)) + ((the (unsigned-byte 32) dword0) + (rm-low-32 addr x86)) + ((the (unsigned-byte 32) dword1) + (rm-low-32 (+ 4 addr) x86)) + ((the (unsigned-byte 64) qword) + (logior + (the (unsigned-byte 64) + (ash dword1 32)) + dword0))) + qword) + + 0) + + /// + + (defthm rm-low-64-in-app-view + (implies (app-view x86) + (equal (rm-low-64 p-addr x86) 0))) + + (defthm-unsigned-byte-p n64p-rm-low-64 + :bound 64 + :concl (rm-low-64 addr x86) + :hints (("Goal" :in-theory (e/d () + (rm-low-32 + force (force))))) + :gen-linear t + :gen-type t) + + (defthm rm-low-64-xw + (implies (and (not (equal fld :mem)) + (not (equal fld :app-view))) + (equal (rm-low-64 addr (xw fld index val x86)) + (rm-low-64 addr x86))) + :hints (("Goal" :in-theory (e/d* (rm-low-64) (force (force))))))) + +(define wm-low-64 + ((addr :type (unsigned-byte #.*physical-address-size*)) + (val :type (unsigned-byte 64)) + (x86)) + :guard (and (not (app-view x86)) + (< (+ 7 addr) *mem-size-in-bytes*)) + :guard-hints (("Goal" :in-theory (e/d (logtail unsigned-byte-p) + (x86p)))) + :parents (physical-memory) + + (if (mbt (not (app-view x86))) + + (b* ((addr (mbe :logic (ifix addr) :exec addr)) + (dword0 (mbe :logic (part-select val :low 0 :width 32) + :exec (logand #xFFFFFFFF val))) + (dword1 (mbe :logic (part-select val :low 32 :width 32) + :exec (logand #xFFFFFFFF (ash val -32)))) + (x86 (wm-low-32 addr dword0 x86)) + (x86 (wm-low-32 (+ 4 addr) dword1 x86))) + x86) + x86) + /// + (defthm x86p-wm-low-64 + (implies (x86p x86) + (x86p (wm-low-64 addr val x86))) + :hints (("Goal" :in-theory (e/d () (x86p)))) + :rule-classes (:rewrite :type-prescription)) + + (defthm xr-wm-low-64 + (implies (not (equal fld :mem)) + (equal (xr fld index (wm-low-64 addr val x86)) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* (wm-low-64) (force (force)))))) + + (defthm wm-low-64-xw + (implies (and (not (equal fld :mem)) + (not (equal fld :app-view))) + (equal (wm-low-64 addr val (xw fld index value x86)) + (xw fld index value (wm-low-64 addr val x86)))) + :hints (("Goal" :in-theory (e/d* (wm-low-64) (force (force))))))) + +;; rm-low-64/wm-low-64 RoW: + +(defthmd put-logior-2-dwords-together + (implies (unsigned-byte-p 64 val) + (equal (logior (loghead 32 val) + (ash (logtail 32 val) 32)) + val)) + :hints ((bitops::logbitp-reasoning))) + +(defthm |(rm-low-64 addr2 (wm-low-64 addr1 val x86)) --- same addr| + (implies (and (equal addr1 addr2) + (force (n64p val)) + (not (app-view x86))) + (equal (rm-low-64 addr2 (wm-low-64 addr1 val x86)) + val)) + :hints (("Goal" :in-theory (e/d (rm-low-64 wm-low-64 put-logior-2-dwords-together) + ())))) + +(defthm |(rm-low-64 addr2 (wm-low-64 addr1 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 8 addr1) + (addr-range 8 addr2)) + (equal (rm-low-64 addr2 (wm-low-64 addr1 val x86)) + (rm-low-64 addr2 x86))) + :hints (("Goal" + :expand ((:free (n x) (addr-range n x))) ;; ugh + :in-theory (e/d (rm-low-64 wm-low-64 ifix) + (disjoint-p))))) + +(defthm |(rm-low-64 addr2 (xw :mem addr1 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 1 addr1) + (addr-range 8 addr2)) + (equal (rm-low-64 addr2 (xw :mem addr1 val x86)) + (rm-low-64 addr2 x86))) + :hints (("Goal" :in-theory (e/d (rm-low-64 rm-low-32) (force (force)))))) + +(defthm |(xr :mem addr1 (wm-low-64 addr2 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 8 addr2) + (addr-range 1 addr1)) + (equal (xr :mem addr1 (wm-low-64 addr2 val x86)) + (xr :mem addr1 x86))) + :hints (("Goal" :in-theory (e/d (ifix wm-low-32 wm-low-64) (force (force)))))) + +;; wm-low-64 WoW: + +(defthm |(wm-low-64 addr2 val2 (wm-low-64 addr1 val1 x86)) --- same addr| + (implies (equal addr1 addr2) + (equal (wm-low-64 addr2 val2 (wm-low-64 addr1 val1 x86)) + (wm-low-64 addr2 val2 x86))) + :hints (("Goal" :in-theory (e/d (wm-low-64) (rm-low-32 wm-low-32))))) + +(defthm |(wm-low-64 addr2 val2 (wm-low-64 addr1 val1 x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 8 addr1) + (addr-range 8 addr2)) + (equal (wm-low-64 addr2 val2 (wm-low-64 addr1 val1 x86)) + (wm-low-64 addr1 val1 (wm-low-64 addr2 val2 x86)))) + :hints (("Goal" :in-theory (e/d (wm-low-64 wm-low-32) ()))) + :rule-classes ((:rewrite :loop-stopper ((addr2 addr1))))) + +(defthm |(xw :mem addr1 (wm-low-64 addr2 val x86)) --- disjoint addr| + (implies (disjoint-p (addr-range 8 addr2) + (addr-range 1 addr1)) + (equal (wm-low-64 addr2 val2 (xw :mem addr1 val1 x86)) + (xw :mem addr1 val1 (wm-low-64 addr2 val2 x86)))) + :hints (("Goal" :in-theory (e/d (ifix wm-low-64 wm-low-32) (force (force)))))) + +;; ====================================================================== + +(globally-disable '(rm-low-32 rm-low-64 wm-low-32 wm-low-64)) + +;; ====================================================================== diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/prefix-modrm-sib-decoding.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/prefix-modrm-sib-decoding.lisp new file mode 100644 index 000000000..fd4578fbb --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/prefix-modrm-sib-decoding.lisp @@ -0,0 +1,2524 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2026, Kestrel Technology, LLC +; 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 GoelArray @('*one-byte-prefixes-group-code-info-ar*') is used for the efficient + lookup of prefix group code information from the one-byte opcode map; see + @(see legacy-prefixes-decoding) and @(see opcode-maps).
+ +See @(see mandatory-prefixes-computation) and @(see + instructions-with-mandatory-prefixes) for details.
+ +Arrays are created for the efficient lookup of modr/m-related information + from the opcode maps. See @(see ModR/M-decoding).
+ +See @(see SIB-decoding) for details.
") + +(local (xdoc::set-default-parents prefix-modrm-sib-decoding)) + +;; ---------------------------------------------------------------------- + +(defsection legacy-prefixes-decoding + + :short "Functions to detect and decode legacy prefixes" + + (local (xdoc::set-default-parents legacy-prefixes-decoding)) + + (define inst-prefix-byte-group-code ((inst inst-p)) + + :short "Takes in a single instruction and figures out if it is a prefix + byte; if so, the prefix group number is returned." + + :long "The return value @('0') corresponds to \"not a prefix\" and + @('1'), @('2'), @('3') and @('4') correspond to the prefix group of + byte.
+ +Note that legacy prefixes are a part of the one-byte opcode map.
" + + (b* (((inst inst))) + (cond + ((keywordp inst.mnemonic) + (case inst.mnemonic + (:prefix-Lock 1) ;; #xF0 + (:prefix-REPNE 1) ;; #xF2 + (:prefix-REP/REPE 1) ;; #xF3 + + (:prefix-ES 2) ;; #x26 + (:prefix-CS 2) ;; #x2E + (:prefix-SS 2) ;; #x36 + (:prefix-DS 2) ;; #x3E + (:prefix-FS 2) ;; #x64 + (:prefix-GS 2) ;; #x65 + + (:prefix-OpSize 3) ;; #x66 + + (:prefix-AddrSize 4) ;; #x67 + + (t 0))) + + (t 0)))) + + (local + (defthm inst-p-of-car-of-inst-list-p + (implies (and (equal (len xs) 1) + (inst-list-p xs)) + (inst-p (car xs))))) + + (define inst-list-prefix-byte-group-code ((first-opcode 24bits-p) + (num-opcodes natp) + (inst-lst inst-list-p)) + :short "Takes in a list of instructions and returns prefix byte group code + for each of the constituent instructions" + + :long "Source: Intel Manuals, Vol. 2, Section 2.1.1.
+ +The value @('0') corresponds to \"not a prefix\" and @('1'), + @('2'), @('3') and @('4') correspond to the prefix group of + byte.
" + + (b* (((when (zp num-opcodes)) nil) + (rest (if (24bits-p (1+ first-opcode)) + (inst-list-prefix-byte-group-code + (1+ first-opcode) (1- num-opcodes) inst-lst) + (er hard? 'inst-list-prefix-byte-group-code + "Illegal opcode ~s0.~%" (str::hexify (1+ first-opcode))))) + (same-opcode-insts (select-insts inst-lst :opcode first-opcode)) + ((when (equal (len same-opcode-insts) 1)) + (cons (inst-prefix-byte-group-code (car same-opcode-insts)) rest))) + (cons 0 rest))) + + (make-event + (b* ((precomputed-table + '( + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 + 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 2 2 3 4 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 + )) + (computed-table + (inst-list-prefix-byte-group-code + #x0 256 *one-byte-opcode-map*)) + ((unless (equal precomputed-table computed-table)) + (er hard 'one-byte-prefixes-group-code-info + "Error: Incorrect legacy prefix info computed!"))) + `(defconst *one-byte-prefixes-group-code-info-ar* + (list-to-array 'one-byte-prefixes-group-code-info + (quote ,computed-table))))) + + (define get-one-byte-prefix-array-code + ((byte :type (unsigned-byte 8))) + :returns (code natp :rule-classes (:rewrite :type-prescription)) + (aref1 'one-byte-prefixes-group-code-info + *one-byte-prefixes-group-code-info-ar* + (mbe :logic (loghead 8 byte) + :exec byte)) + /// + (defthm upper-bound-get-one-byte-prefix-array-code + (<= (get-one-byte-prefix-array-code x) 4)))) + +;; ---------------------------------------------------------------------- + +(defsection instructions-with-mandatory-prefixes + + :short "Based off @(see opcode-maps), pre-computing the opcodes that have a + valid instruction corresponding to the following prefixes: + @(':NO-PREFIX :66 :F3 :F2')." + + (local (xdoc::set-default-parents instructions-with-mandatory-prefixes)) + + (define opcode-present? ((first-opcode 24bits-p) + (num-opcodes natp) + (inst-lst inst-list-p)) + :short "Returns a 256-element list, where a @('1') at a position @('i') + denotes that at least one instruction with opcode @('i') exists in + @('inst-lst'); @('0') denotes no such instruction is present." + + :returns (lst true-listp) + (b* (((when (zp num-opcodes)) nil) + (rest (if (24bits-p (1+ first-opcode)) + (opcode-present? + (1+ first-opcode) (1- num-opcodes) inst-lst) + (er hard? 'opcode-present? + "Illegal opcode ~s0.~%" (str::hexify (1+ first-opcode))))) + (same-opcode-insts (select-insts inst-lst :opcode first-opcode)) + ((when (endp same-opcode-insts)) + (cons 0 rest))) + (cons 1 rest))) + + (make-event + (b* ((64-insts (select-insts *two-byte-opcode-map* + ;; All instructions valid in 64-bit mode. + :get/rem :rem + :mode :i64)) + + (NP-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :NO-PREFIX)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_00 256 F3-pfx-insts))) + `(progn + + (defconst *64-bit-mode-two-byte-no-prefix-ok-ar* + (list-to-array '64-bit-mode-two-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *64-bit-mode-two-byte-66-ok-ar* + (list-to-array '64-bit-mode-two-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *64-bit-mode-two-byte-F2-ok-ar* + (list-to-array '64-bit-mode-two-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + + (defconst *64-bit-mode-two-byte-F3-ok-ar* + (list-to-array '64-bit-mode-two-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table))))))) + + (make-event + (b* ((32-insts (select-insts *two-byte-opcode-map* + ;; All instructions valid in 32-bit mode. + :get/rem :rem + :mode :o64)) + + (NP-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :NO-PREFIX)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_00 256 F3-pfx-insts))) + `(progn + + (defconst *32-bit-mode-two-byte-no-prefix-ok-ar* + (list-to-array '32-bit-mode-two-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *32-bit-mode-two-byte-66-ok-ar* + (list-to-array '32-bit-mode-two-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *32-bit-mode-two-byte-F2-ok-ar* + (list-to-array '32-bit-mode-two-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + + (defconst *32-bit-mode-two-byte-F3-ok-ar* + (list-to-array '32-bit-mode-two-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table))))))) + + (make-event + (b* ((64-insts (select-insts *0F-38-three-byte-opcode-map* + ;; All instructions valid in 64-bit mode. + :get/rem :rem + :mode :i64)) + + (NP-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :NO-PREFIX)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_38_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_38_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_38_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_38_00 256 F3-pfx-insts))) + `(progn + + (defconst *64-bit-mode-0f-38-three-byte-no-prefix-ok-ar* + (list-to-array '64-bit-mode-0f-38-three-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *64-bit-mode-0f-38-three-byte-66-ok-ar* + (list-to-array '64-bit-mode-0f-38-three-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *64-bit-mode-0f-38-three-byte-F2-ok-ar* + (list-to-array '64-bit-mode-0f-38-three-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + + (defconst *64-bit-mode-0f-38-three-byte-F3-ok-ar* + (list-to-array '64-bit-mode-0f-38-three-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table))))))) + + (make-event + (b* ((32-insts (select-insts *0F-38-three-byte-opcode-map* + ;; All instructions valid in 32-bit mode. + :get/rem :rem + :mode :o64)) + + (NP-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :no-prefix)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_38_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_38_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_38_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_38_00 256 F3-pfx-insts))) + `(progn + + (defconst *32-bit-mode-0f-38-three-byte-no-prefix-ok-ar* + (list-to-array '32-bit-mode-0f-38-three-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *32-bit-mode-0f-38-three-byte-66-ok-ar* + (list-to-array '32-bit-mode-0f-38-three-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *32-bit-mode-0f-38-three-byte-F2-ok-ar* + (list-to-array '32-bit-mode-0f-38-three-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + (defconst *32-bit-mode-0f-38-three-byte-F3-ok-ar* + (list-to-array '32-bit-mode-0f-38-three-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table))))))) + + (make-event + (b* ((64-insts (select-insts *0F-3A-three-byte-opcode-map* + ;; All instructions valid in 64-bit mode. + :get/rem :rem + :mode :i64)) + + (NP-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :no-prefix)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_3A_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_3A_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_3A_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 64-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_3A_00 256 F3-pfx-insts))) + `(progn + + (defconst *64-bit-mode-0f-3A-three-byte-no-prefix-ok-ar* + (list-to-array '64-bit-mode-0f-3A-three-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *64-bit-mode-0f-3A-three-byte-66-ok-ar* + (list-to-array '64-bit-mode-0f-3A-three-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *64-bit-mode-0f-3A-three-byte-F2-ok-ar* + (list-to-array '64-bit-mode-0f-3A-three-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + (defconst *64-bit-mode-0f-3A-three-byte-F3-ok-ar* + (list-to-array '64-bit-mode-0f-3A-three-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table))))))) + + (make-event + (b* ((32-insts (select-insts *0F-3A-three-byte-opcode-map* + ;; All instructions valid in 32-bit mode. + :get/rem :rem + :mode :o64)) + + (NP-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :no-prefix)) + (NP-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + NP-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (NP-computed-table (opcode-present? #ux0F_3A_00 256 NP-pfx-insts)) + + (66-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :66)) + (66-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + 66-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (66-computed-table (opcode-present? #ux0F_3A_00 256 66-pfx-insts)) + + (F2-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F2)) + (F2-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F2-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F2-computed-table (opcode-present? #ux0F_3A_00 256 F2-pfx-insts)) + + (F3-pfx-insts (select-insts 32-insts + ;; All instructions with the appropriate + ;; prefix. + :get/rem :get :prefix :F3)) + (F3-pfx-insts (remove-insts-with-feat + ;; All non-AVX instructions. + F3-pfx-insts + (append (list :avx :avx2 :bmi1 :bmi2) + *avx512-feature-flags*))) + (F3-computed-table (opcode-present? #ux0F_3A_00 256 F3-pfx-insts))) + `(progn + + (defconst *32-bit-mode-0f-3A-three-byte-no-prefix-ok-ar* + (list-to-array '32-bit-mode-0f-3A-three-byte-no-prefix-ok + (ints-to-booleans + (quote ,NP-computed-table)))) + + (defconst *32-bit-mode-0f-3A-three-byte-66-ok-ar* + (list-to-array '32-bit-mode-0f-3A-three-byte-66-ok + (ints-to-booleans + (quote ,66-computed-table)))) + + (defconst *32-bit-mode-0f-3A-three-byte-F2-ok-ar* + (list-to-array '32-bit-mode-0f-3A-three-byte-F2-ok + (ints-to-booleans + (quote ,F2-computed-table)))) + (defconst *32-bit-mode-0f-3A-three-byte-F3-ok-ar* + (list-to-array '32-bit-mode-0f-3A-three-byte-F3-ok + (ints-to-booleans + (quote ,F3-computed-table)))))))) + +(defsection mandatory-prefixes-computation + + :short "Picking a mandatory prefix" + + :long "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/')).
+ +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))
+
+
+
+ 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...
+
+
+
+ + This is based on Intel manual, Jan'19, Volume 2, Tables 2-1 and 2-2, + as well as AMD manual, Dec'17, Volume 3, Tables A-33 and A-35. + When the address size is 32 or 64 bits, + Intel Table 2-2 and AMD Table A-35 apply: + a SIB byte is expected exactly when + ModR/M.mod is not #b11 and ModR/M.r/m is #b100. + When the address size is 16 bits, no SIB byte is expected. +
++ Intel Table 2-3 applies to 32-bit addresses, + but Section 2.2.1.3 says that it also applies to 64-bit addresses. + AMD Table A-35 mentions both 32-bit and 64-bit addressing. +
++ Intel manual, Jan'19, Volume 2, Table 2-7 is not very clear, + giving the impression that a SIB byte may be required + when Mod = 00 and R/M = 101 (last column of first row). + But AMD manual, Dec'17, Volume 3, Table 1-16 + (which corresponds to Intel Table 2-7) clearly says that + no SIB byte is present in the first row. + Intel's last column of first row means that, + in order to use @('Disp32') instead of @('RIP+Disp32'), + 64-bit mode must use the encoding with SIB in the second row. +
++ The second argument of this function says whether + the address size is 16 bits or not (i.e. 32 or 64 bits). + In 64-bit mode, this argument is always @('nil'). + In 32-bit mode, this argument may be @('t') or @('nil'). +
" + (and (not 16-bit-addressp) + (b* ((r/m (modr/m->r/m ModR/M)) + (mod (modr/m->mod ModR/M))) + (and (int= r/m 4) + (not (int= mod 3))))))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/register-readers-and-writers.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/register-readers-and-writers.lisp new file mode 100644 index 000000000..3b8825b71 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/register-readers-and-writers.lisp @@ -0,0 +1,1831 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2024, Kestrel Technology LLC +; 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 GoelGeneral-purpose register indices are 3 bits except in +64-bit mode, where they can have 4 bits depending on the @('rex') +prefix.
" + :enabled t + (if (eql rex-byte 0) + (n03p reg) + (n04p reg)) + + /// + + (defthm reg-indexp-for-3-bits + (implies (and (syntaxp (quotep reg)) + (n03p reg)) + (reg-indexp reg rex))) + + (defthm reg-indexp-logand-7 + (reg-indexp (loghead 3 modr/m) rex-byte) + :hints (("Goal" :in-theory (enable reg-indexp))))) + + (define reg-index + ((reg :type (unsigned-byte 3) "Register index") + (rex-byte :type (unsigned-byte 8) "REX prefix") + (index :type (unsigned-byte 2) "One of the W, R, X, or B bits of the REX prefix")) + :inline t + :no-function t + :short "Using the REX prefix to access general-purpose registers" + :long "In 64-bit mode, in addition to generating 64-bit operand sizes, + the REX prefix is used to reference registers R8 to R15. Instructions that + include REX prefixes can access these registers if the relevant W, R, X, or + B bit in the REX prefix is set. E.g., let R be the relevant bit in the REX + prefix and let R be set --- so @('index') = @(`*r*`) for this function. If + @('reg') = 0 (which, in the non-REX world, would refer to rAX), + @('reg-index') would give us the register index corresponding to the + register r8. If R is not set, @('reg-index') will give us the index + corresponding to rAX.
+ +In 32-bit mode, the REX prefix is absent. This function can be used in + 32-bit mode, by passing 0 as REX.
" + + (if (logbitp index rex-byte) + (logior 8 (mbe :logic (n03 reg) :exec reg)) + (mbe :logic (n03 reg) :exec reg)) + + /// + + (local (in-theory (e/d (reg-indexp) ()))) + + (defthm reg-indexp-reg-index + (reg-indexp (reg-index reg rex-byte name) rex-byte)) + + (defthm-unsigned-byte-p n04p-reg-index + :hyp t + :bound 4 + :concl (reg-index reg rex-byte name) + :gen-linear t + :gen-type t) + + (defthm reg-indexp-forward + (implies (reg-indexp reg rex-byte) + (unsigned-byte-p 4 reg)) + :rule-classes :forward-chaining))) + +;; ---------------------------------------------------------------------- + +(define vex-vvvv-reg-index ((vvvv :type (unsigned-byte 4))) + :returns (index natp) + :parents (register-readers-and-writers) + :short "Register index specified by the VEX.vvvv bits." + :long + (xdoc::topstring + (xdoc::p + "The VEX.vvvv bits specify a register index + in inverted (i.e. ones' complement) form: + see Intel Manual Volume 2 Section 2.3.6 (Dec 2023). + We introduce this function to perform the ones' complement, + for greater abstraction.")) + (loghead 4 (lognot vvvv)) + /// + + (defthm-unsigned-byte-p n04p-of-vex-vvvv-reg-index + :hyp t + :bound 4 + :concl (vex-vvvv-reg-index vvvv) + :gen-linear t + :gen-type t)) + +;; ---------------------------------------------------------------------- + +;; [Jared] these rules broke something below +(local (in-theory (disable bitops::logext-of-logand + bitops::logext-of-logior))) + +(defsection GPRs-Reads-and-Writes + + :parents (register-readers-and-writers) + + :short "Functions to read/write 8/16/32/64 bits into the registers" + + :long "@(see rr08), @(see rr16), @(see rr32), and @(see rr64) +will read the contents of the GPRs as natural numbers. Remember that +@('rgfi') will return an @(see i64p) value.
+ +Here is an example:
+\(rr64 0 x86\) returns @('18446744073709551615') and +(rgfi 0 x86) returns @('-1'). Note that +@('18446744073709551615') is a bignum in CCL. This is precisely the +reason why we declared registers as @('i64p') instead of @('n64p'); +@('-1') would be stored as a bignum if the registers were +@('n64p').
+ +Similarly, @(see wr08), @(see wr16), @(see wr32), and @(see wr64) +are used to write natural numbers into the GPRs.
" + + (local (xdoc::set-default-parents GPRs-Reads-and-Writes)) + + (define rr08 + ((reg :type (unsigned-byte 4)) + (rex :type (unsigned-byte 8)) + (x86)) + :inline t + :no-function t + :guard (reg-indexp reg rex) + :short "Read from byte general-purpose registers" + :long "Source: Intel Manual Volume 1 Section 3.4.1.1 (Jun 2024) + (General-Purpose Registers in 64-bit Mode)
+ +In 64-bit mode, there are limitations on accessing byte + registers. An instruction cannot reference legacy high-bytes (for example: AH, + BH, CH, DH) and one of the new byte registers at the same time (for example: + the low byte of the RAX register). However, instructions may reference legacy + low-bytes (for example: AL, BL, CL or DL) and new byte registers at the same + time (for example: the low byte of the R8 register, or R8L). The architecture + enforces this limitation by changing high-byte references (AH, BH, CH, DH) to + low byte references (BPL, SPL, DIL, SIL: the low 8 bits for RBP, RSP, RDI and + RSI) for instructions using a REX prefix.+ +
In other words, without the REX prefix, indices 0-7 refer to byte registers + AL, CL, DL, BL, AH, CH, DH, and BH, whereas with the REX prefix, indices 0-15 + refer to AL, CL, DL, BL, SPL, BPL, SIL, DIL, R8L, R9L, R10L, R11L, R12L, R13L, + R14L, R15L. This applies to 64-bit mode.
+ +In 32-bit mode, this function is called with 0 as REX.
" + + (b* ((reg (mbe :logic (nfix reg) :exec reg))) + (cond ((or (not (eql rex 0)) + (< reg 4)) + (let ((qword (the (signed-byte 64) (rgfi reg x86)))) + (n08 qword))) + (t ;; no rex and reg is at least 4 -- then read from AH, etc. + (let ((qword + (the (signed-byte 64) + (rgfi (the (unsigned-byte 4) (- reg 4)) x86)))) + (mbe :logic (part-select qword :low 8 :width 8) + :exec (n08 (ash qword -8))))))) + + /// + + (defthm-unsigned-byte-p n08p-rr08 + :hyp t + :bound 8 + :concl (rr08 reg rex x86) + :gen-linear t + :gen-type t)) + + (define wr08 + ((reg :type (unsigned-byte 4)) + (rex :type (unsigned-byte 8)) + (byte :type (unsigned-byte 8)) + (x86)) + :inline t + :no-function t + :guard (reg-indexp reg rex) + :guard-hints (("Goal" :in-theory (e/d ( + loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + :short "Writing to byte general-purpose registers" + :long "Note Intel Manual Volume 1 Section 3.4.1.1 (Jun 2024), + which says the following about 64-bit mode:
+ +8-bit and 16-bit operands generate an 8-bit or 16-bit result. The + upper 56 or 48 bits (respectively) of the destination general-purpose + register are not modified by the operation.
+ +This is also confirmed by AMD manual, Jun'15, Vol. 3, App. B.1, under + ‘No Extension of 8-Bit and 16-Bit Results’.
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by the + following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose register + are not preserved when switching from 64-bit mode to a 32-bit mode (to + protected mode or compatibility mode). Software must not depend on these + bits to maintain a value after a 64-bit to 32-bit mode switch.
+ +This function is used both in 64-bit mode and in 32-bit mode. Since in + 32-bit mode the high 32 bits of general-purpose registers are not + accessible, it is fine for this function to leave those bits unchanged, as + opposed to, for example, setting them to undefined values as done by the + semantic functions of certain instructions for certain flags. This way, in + this function, we can treat 32-bit and 64-bit the same, without a case + split. Note that the switching from 32-bit mode to 64-bit mode (when + modeled) will set the high 32 bits of general-purpose registers to undefined + values.
" + + (b* ((reg (mbe :logic (nfix reg) :exec reg))) + (cond ((or (not (eql rex 0)) + (< reg 4)) + (let ((qword (the (signed-byte 64) (rgfi reg x86)))) + (!rgfi reg + (n64-to-i64 + (mbe :logic + (part-install + byte + (part-select qword :low 0 :width 64) + :low 0 :width 8) + :exec + (the (unsigned-byte 64) + (logior (the (unsigned-byte 64) + (logand #xffffffffffffff00 qword)) + byte)))) + x86))) + (t ;; no rex and reg is at least 4 -- then write to AH, etc. + (let* ((index (the (unsigned-byte 4) + (- (the (unsigned-byte 4) reg) 4))) + (qword (the (signed-byte 64) (rgfi index x86)))) + (!rgfi index + (n64-to-i64 + (mbe :logic + (part-install + byte + (part-select qword :low 0 :width 64) + :low 8 :width 8) + :exec + (the (unsigned-byte 64) + (logior (the (unsigned-byte 64) + (logand #xffffffffffff00ff qword)) + (the (unsigned-byte 16) (ash byte 8)))))) + x86))))) + + /// + + (defthm x86p-wr08 + (implies (x86p x86) + (x86p (wr08 reg rex byte x86))))) + + (encapsulate + () + + (local + (include-book "arithmetic-5/top" :dir :system)) + + (defthm loghead-logtail-logext-for-rr08/wr08 + (implies (integerp x) + (equal (loghead 8 (logtail 8 (logext 64 x))) + (loghead 8 (logtail 8 x)))) + :hints (("Goal" :in-theory + (e/d (logtail evenp oddp logbitp logext loghead logapp) + ()))))) + + (defthm rr08-wr08-same + (equal (rr08 reg rex (wr08 reg rex byte x86)) + (loghead 8 byte)) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 + rr08 wr08) + (unsigned-byte-p + (force) force))))) + + (defthm rr08-wr08-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rr08 reg1 rex1 (wr08 reg2 rex2 byte x86)) + (rr08 reg1 rex1 x86))) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 rr08 wr08) + (force (force)))))) + + (define rr16 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :short "Read from word general-purpose registers" + + (n16 (the (signed-byte 64) (rgfi reg x86))) + + /// + + (defthm-unsigned-byte-p n16p-rr16 + :hyp t + :bound 16 + :concl (rr16 reg x86) + :gen-linear t + :gen-type t)) + + (define wr16 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 16)) + (x86)) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d ( + loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + :short "Write to word general-purpose registers" + :long "Note Intel Vol. 1 Sec. 3.4.1.1 + p. 3-17, which says the following about 64-bit mode:
+ +8-bit and 16-bit operands generate an 8-bit or 16-bit result. The + upper 56 or 48 bits (respectively) of the destination general-purpose + register are not modified by the operation.
+ +This is also confirmed by AMD manual, Jun'15, Vol. 3, App. B.1, under + ‘No Extension of 8-Bit and 16-Bit Results’.
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by the + following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose register + are not preserved when switching from 64-bit mode to a 32-bit mode (to + protected mode or compatibility mode). Software must not depend on these + bits to maintain a value after a 64-bit to 32-bit mode switch.
+ +This function is used both in 64-bit mode and in 32-bit mode. Since in + 32-bit mode the high 32 bits of general-purpose registers are not + accessible, it is fine for this function to leave those bits unchanged, as + opposed to, for example, setting them to undefined values as done by the + semantic functions of certain instructions for certain flags. This way, in + this function, we can treat 32-bit and 64-bit the same, without a case + split. Note that the switching from 32-bit mode to 64-bit mode (when + modeled) will set the high 32 bits of general-purpose registers to undefined + values.
" + + (let ((qword (the (signed-byte 64) (rgfi reg x86)))) + (!rgfi reg + (n64-to-i64 + (mbe :logic + (part-install + val + (part-select qword :low 0 :width 64) + :low 0 :width 16) + :exec + (the (unsigned-byte 64) + (logior (the (unsigned-byte 64) + (logand qword #xffffffffffff0000)) + val)))) + x86)) + + /// + + (defthm x86p-wr16 + (implies (x86p x86) + (x86p (wr16 reg val x86))))) + + (defthm rr16-wr16-same + (equal (rr16 reg (wr16 reg val x86)) + (loghead 16 val)) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 + rr16 wr16) + (unsigned-byte-p force (force)))))) + + (defthm rr16-wr16-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rr16 reg1 (wr16 reg2 val x86)) + (rr16 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 rr16 wr16) + (force (force)))))) + + (define rr32 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :short "Read from doubleword general-purpose registers" + + (n32 (the (signed-byte 64) (rgfi reg x86))) + + /// + + (defthm-unsigned-byte-p n32p-rr32 + :hyp t + :bound 32 + :concl (rr32 reg x86) + :gen-linear t + :gen-type t)) + + (define wr32 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 32)) + (x86)) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d ( + loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + + :short "Write to doubleword general-purpose registers" + :long "Note Intel Vol. 1 Sec. 3.4.1.1 + p. 3-17, which says the following about 64-bit mode:
+ +32-bit operands generate a 32-bit result, zero-extended to a 64-bit + result in the destination general-purpose register.
+ +This is also confirmed by AMD manual, Jun'15, Vol. 3, App. B.1, under `No + Extension of 8-Bit and 16-Bit Results'.
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by the + following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose register + are not preserved when switching from 64-bit mode to a 32-bit mode (to + protected mode or compatibility mode). Software must not depend on these + bits to maintain a value after a 64-bit to 32-bit mode switch.
+ +This function is used both in 64-bit mode and in 32-bit mode. Since in + 32-bit mode the high 32 bits of general-purpose registers are not + accessible, it is fine for this function to set those bits to 0, as opposed + to, for example, setting them to undefined values as done by the semantic + functions of certain instructions for certain flags. This way, in this + function, we can treat 32-bit and 64-bit the same, without a case + split. Note that the switching from 32-bit mode to 64-bit mode (when + modeled) will set the high 32 bits of general-purpose registers to undefined + values.
" + + (!rgfi reg (mbe :logic (n32 val) :exec val) x86) + + /// + + (defthm x86p-wr32 + (implies (x86p x86) + (x86p (wr32 reg val x86))))) + + (defthm rr32-wr32-same + (equal (rr32 reg (wr32 reg val x86)) + (loghead 32 val)) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 + rr32 wr32) + (unsigned-byte-p))))) + + (defthm rr32-wr32-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rr32 reg1 (wr32 reg2 val x86)) + (rr32 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 rr32 wr32) + ())))) + + (define rr64 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :short "Read from quadword general-purpose registers" + :long "This function is used only in 64-bit mode.
" + + (n64 (the (signed-byte 64) (rgfi reg x86))) + + /// + + (defthm-unsigned-byte-p n64p-rr64 + :hyp t + :bound 64 + :concl (rr64 reg x86) + :gen-linear t + :gen-type t)) + + (define wr64 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 64)) + (x86)) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d ( + loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + (b* ((x86 (!rgfi reg (n64-to-i64 val) x86))) + x86) + :short "Write to quadword general-purpose registers" + :long "This function is used only in 64-bit mode.
" + + /// + (defthm x86p-wr64 + (implies (x86p x86) + (x86p (wr64 reg val x86))))) + + (defthm rr64-wr64-same + (equal (rr64 reg (wr64 reg val x86)) + (loghead 64 val)) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 + rr64 wr64) + (unsigned-byte-p))))) + + (defthm rr64-wr64-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rr64 reg1 (wr64 reg2 val x86)) + (rr64 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (n64-to-i64 rr64 wr64) + ())))) + + (define rgfi-size + ((nbytes :type (unsigned-byte 4)) + (index :type (unsigned-byte 4)) + (rex :type (unsigned-byte 8)) + x86) + :enabled t + :guard (and (reg-indexp index rex) + (member nbytes '(1 2 4 8))) + :inline t + :no-function t + :returns (val natp :rule-classes :type-prescription) + :short "Read form byte, word, doubleword, or quadword + general-purpose register" + (case nbytes + (1 (rr08 index rex x86)) + (2 (rr16 index x86)) + (4 (rr32 index x86)) + (8 (rr64 index x86)) + (otherwise + ;; This function shouldn't really be used in the case when nbytes + ;; != 1, 2, 4, or 8. Anyway, the guard says nbytes shouldn't be + ;; anything else. + 0))) + + (encapsulate () + (local (include-book "kestrel/arithmetic-light/expt" :dir :system)) + (defthm rgfi-size-linear + (< (rgfi-size nbytes index rex x86) (expt 2 (* 8 nbytes))) + :rule-classes :linear + :hints (("Goal" :in-theory (enable rgfi-size))))) + + (define !rgfi-size + ((nbytes :type (unsigned-byte 4)) + (index :type (unsigned-byte 4)) + (val :type integer) + (rex :type (unsigned-byte 8)) + x86) + :enabled t + :guard (and (reg-indexp index rex) + (member nbytes '(1 2 4 8)) + (unsigned-byte-p (ash nbytes 3) val)) + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (x86p))))) + :inline t + :no-function t + :short "Write to byte, word, doubleword, or quadword + general-purpose register" + (case nbytes + (1 (wr08 index rex val x86)) + (2 (wr16 index val x86)) + (4 (wr32 index val x86)) + (8 (wr64 index val x86)) + (otherwise + ;; The guard says nbytes shouldn't be anything other than 1, 2, + ;; 4, or 8. + x86)))) + +;; ---------------------------------------------------------------------- + +(defsection MMX-Registers-Reads-and-Writes + + :parents (register-readers-and-writers) + + :short "Functions to read/write into the MMX registers" + + :long "From Section 12.2, Intel Manual, Volume 3 \(System + Programming\):
+ +The MMX state consists of eight 64-bit registers \(MM0 +through MM7\). These registers are aliased to the low 64-bits \(bits 0 +through 63\) of floating-point registers R0 through R7 \(see Figure +12-1\). Note that the MMX registers are mapped to the physical +locations of the floating-point registers \(R0 through R7\), not to +the relative locations of the registers in the floating-point register +stack \(ST0 through ST7\). As a result, the MMX register mapping is +fixed and is not affected by value in the Top Of Stack \(TOS\) field +in the floating-point status word \(bits 11 through 13\).
+ +From Section 12.2, Intel Manual, Volume 3 \(System Programming\)
: + +When a value is written into an MMX register using an MMX +instruction, the value also appears in the corresponding +floating-point register in bits 0 through 63. Likewise, when a +floating-point value written into a floating-point register by a x87 +FPU, the low 64 bits of that value also appears in a the corresponding +MMX register.
+ +The execution of MMX instructions have several side effects on +the x87 FPU state contained in the floating-point registers, the x87 +FPU tag word, and the x87 FPU status word. These side effects are as +follows:
+ +* When an MMX instruction writes a value into an MMX register, +at the same time, bits 64 through 79 of the corresponding +floating-point register are set to all 1s.
+ +* When an MMX instruction \(other than the EMMS instruction\) is +executed, each of the tag fields in the x87 FPU tag word is set to 00B +\(valid\). \(See also Section 12.2.1, Effect of MMX, x87 FPU, FXSAVE, +and FXRSTOR Instructions on the x87 FPU Tag Word.\)
+ +* When the EMMS instruction is executed, each tag field in the +x87 FPU tag word is set to 11B \(empty)\.
+ +* Each time an MMX instruction is executed, the TOS value is +set to 000B. Execution of MMX instructions does not affect the other +bits in the x87 FPU status word \(bits 0 through 10 and bits 14 and +15\) or the contents of the other x87 FPU registers that comprise the +x87 FPU state \(the x87 FPU control word, instruction pointer, data +pointer, or opcode registers\).
" + + (local (xdoc::set-default-parents MMX-Registers-Reads-and-Writes)) + + (define mmx + ((i :type (integer 0 7)) + (x86)) + :inline t + :no-function t + (let ((reg80 (the (unsigned-byte 80) (fp-datai i x86)))) + (mbe :logic (part-select reg80 :low 0 :width 64) + :exec (logand #.*2^64-1* reg80))) + + /// + + (defthm-unsigned-byte-p n64p-mmx + :hyp t + :bound 64 + :concl (mmx i x86) + :gen-type t + :gen-linear t)) + + (define !mmx + ((i :type (integer 0 7)) + (v :type (unsigned-byte 64)) + (x86)) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d () + (unsigned-byte-p)))) + + (let ((val80 (mbe :logic (part-install #xFFFF v :low 64 :high 79) + :exec (the (unsigned-byte 80) + (logior #uxFFFF_00000000_00000000 v))))) + (!fp-datai i val80 x86)) + + /// + + (defthm x86p-!mmx + (implies (x86p x86) + (x86p (!mmx i v x86))) + :hints (("Goal" :in-theory (e/d () (force (force))))))) + + (define mmx-instruction-updates (x86) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (unsigned-byte-p-when-fp-statusbits-p) + ()))) + :short "We set the FPU tags and the TOS field + to 00B \(valid\) and 000B respectively." + :long + (xdoc::topstring + (xdoc::p + "This function accounts for the effects of + all MMX instructions except EMMS. + The settting of the FPU tags is described in + Intel manual Volume 1 (Feb 2026) Section 9.5.1. + The setting of the TOS field of the status register is described in + Intel manual Volume 3 (Feb 2026) Section 15.2.")) + (b* ((x86 (!fp-tag 0 x86)) + (fp-status (fp-status x86)) + (x86 (!fp-status (!fp-statusBits->top 0 fp-status) x86))) + x86) + + /// + + (defthm x86p-mmx-instruction-updates + (implies (x86p x86) + (x86p (mmx-instruction-updates x86)))))) + +;; ---------------------------------------------------------------------- + +(defsection ZMMs-Reads-and-Writes + + :parents (register-readers-and-writers) + + :short "Functions to read/write 32/64/128/256/512 bits into the + XMM/YMM/ZMM registers" + + :long "Source: Intel Vol. 1, Section 15.5: ACCESSING XMM, YMM AND ZMM + REGISTERS
+ +The lower 128 bits of a YMM register is aliased to the + corresponding XMM register. Legacy SSE instructions (i.e., SIMD + instructions operating on XMM state but not using the VEX prefix, + also referred to non-VEX encoded SIMD instructions) will not access + the upper bits (MAXVL-1:128) of the YMM registers. AVX and FMA + instructions with a VEX prefix and vector length of 128-bits zeroes + the upper 128 bits of the YMM register.+ +
Upper bits of YMM registers (255:128) can be read and + written to by many instructions with a VEX.256 prefix. XSAVE and + XRSTOR may be used to save and restore the upper bits of the YMM + registers.+ +
The lower 256 bits of a ZMM register are aliased to the + corresponding YMM register. Legacy SSE instructions (i.e., SIMD + instructions operating on XMM state but not using the VEX prefix, + also referred to non-VEX encoded SIMD instructions) will not access + the upper bits (MAXVL-1:128) of the ZMM registers, where MAXVL is + maximum vector length (currently 512 bits). AVX and FMA instructions + with a VEX prefix and vector length of 128-bits zero the upper 384 + bits of the ZMM register, while the VEX prefix and vector length of + 256-bits zeroes the upper 256 bits of the ZMM register. Upper bits + of ZMM registers (511:256) can be read and written to by instructions + with an EVEX.512 prefix.+ +
@(see rz32), @(see rz64), @(see rz128), @(see rz256), and @(see + rz512) will read the contents of the ZMMs as natural numbers.
+ +Similarly, @(see wz32), @(see wz64), @(see wz128), @(see wz256), + and @(see wz512) are used to write natural numbers into the + ZMMs.
" + + (local (xdoc::set-default-parents ZMMs-Reads-and-Writes)) + + (define rz32 + ((reg :type (unsigned-byte 5)) + (x86)) + :inline t + :no-function t + + (n32 (the (unsigned-byte 512) (zmmi reg x86))) + + /// + + (defthm-unsigned-byte-p n32p-rz32 + :hyp t + :bound 32 + :concl (rz32 reg x86) + :gen-linear t + :gen-type t)) + + (define rz64 + ((reg :type (unsigned-byte 5)) + (x86)) + :inline t + :no-function t + + (n64 (the (unsigned-byte 512) (zmmi reg x86))) + + /// + + (defthm-unsigned-byte-p n64p-rz64 + :hyp t + :bound 64 + :concl (rz64 reg x86) + :gen-linear t + :gen-type t)) + + (define rz128 + ((reg :type (unsigned-byte 5)) + (x86)) + :inline t + :no-function t + + (n128 (the (unsigned-byte 512) (zmmi reg x86))) + + /// + + (defthm-unsigned-byte-p n128p-rz128 + :hyp t + :bound 128 + :concl (rz128 reg x86) + :gen-linear t + :gen-type t)) + + (define rz256 + ((reg :type (unsigned-byte 5)) + (x86)) + :inline t + :no-function t + + (n256 (the (unsigned-byte 512) (zmmi reg x86))) + + /// + + (defthm-unsigned-byte-p n256p-rz256 + :hyp t + :bound 256 + :concl (rz256 reg x86) + :gen-linear t + :gen-type t)) + + (define rz512 + ((reg :type (unsigned-byte 5)) + (x86)) + :inline t + :no-function t + + (n512 (the (unsigned-byte 512) (zmmi reg x86))) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm-unsigned-byte-p n512p-rz512 + :hyp t + :bound 512 + :concl (rz512 reg x86) + :gen-linear t + :gen-type t)) + + (define vector-access-type-p (x) + :returns (ok? booleanp :rule-classes :type-prescription) + :enabled t + (or (equal x #.*xmm-access*) + (equal x #.*vex-xmm-access*) + (equal x #.*ymm-access*) + (equal x #.*zmm-access*))) + + (define wz32 + ((reg :type (unsigned-byte 5)) + (val :type (unsigned-byte 32)) + (x86) + &key + ((regtype vector-access-type-p) '*zmm-access*)) + :short "Write @('val') to low 32 bits of a ZMM register" + :long "Upper bits: For XMM registers, upper @(`(- 512 + 128)`) bits are preserved if @('regtype') is @('*xmm-access*'); + for @('*vex-xmm-access*'), these bits are zeroed out. For + @('*ymm-access*'), upper @(`(- 512 256)`) bits are zeroed + out. For @('*zmm-access*'), no upper bits are zeroed out.
" + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + + (b* ((data (the (unsigned-byte 512) (zmmi reg x86))) + (regtype (the (unsigned-byte 3) regtype)) + (data (case regtype + (#.*vex-xmm-access* + (loghead 128 data)) + (#.*ymm-access* + (loghead 256 data)) + (otherwise + ;; Preserve upper bits. + data))) + (val (n32 (the (unsigned-byte 32) val)))) + (!zmmi reg (part-install val data :low 0 :width 32) x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm x86p-wz32 + (implies (x86p x86) + (x86p (wz32 reg val x86 :regtype access))))) + + (defthm rz32-wz32-same + (equal (rz32 reg (wz32 reg val x86 :regtype access)) + (loghead 32 val)) + :hints (("Goal" + :in-theory (e/d (rz32 wz32) + (unsigned-byte-p + force (force)))))) + + (defthm rz32-wz32-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rz32 reg1 (wz32 reg2 val x86 :regtype access)) + (rz32 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (rz32 wz32) + (force (force)))))) + + (define wz64 + ((reg :type (unsigned-byte 5)) + (val :type (unsigned-byte 64)) + (x86) + &key + ((regtype vector-access-type-p) '*zmm-access*)) + :short "Write @('val') to low 64 bits of a ZMM register" + :long "Upper bits: For XMM registers, upper @(`(- 512 + 128)`) bits are preserved if @('regtype') is @('*xmm-access*'); + for @('*vex-xmm-access*'), these bits are zeroed out. For + @('*ymm-access*'), upper @(`(- 512 256)`) bits are zeroed + out. For @('*zmm-access*'), no upper bits are zeroed out.
" + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + (b* ((data (the (unsigned-byte 512) (zmmi reg x86))) + (regtype (the (unsigned-byte 3) regtype)) + (data (case regtype + (#.*vex-xmm-access* + (loghead 128 data)) + (#.*ymm-access* + (loghead 256 data)) + (otherwise + ;; Preserve upper bits. + data))) + (val (the (unsigned-byte 64) (n64 val)))) + (!zmmi reg (part-install val data :low 0 :width 64) x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm x86p-wz64 + (implies (x86p x86) + (x86p (wz64 reg val x86 :regtype access))))) + + (defthm rz64-wz64-same + (equal (rz64 reg (wz64 reg val x86 :regtype access)) + (loghead 64 val)) + :hints (("Goal" + :in-theory (e/d (rz64 wz64) + (unsigned-byte-p + force (force)))))) + + (defthm rz64-wz64-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rz64 reg1 (wz64 reg2 val x86 :regtype access)) + (rz64 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (rz64 wz64) + (force (force)))))) + + (define wz128 + ((reg :type (unsigned-byte 5)) + (val :type (unsigned-byte 128)) + (x86) + &key + ((regtype vector-access-type-p) '*zmm-access*)) + :short "Write @('val') to low 128 bits of a ZMM register" + :long "Upper bits: For XMM registers, upper @(`(- 512 + 128)`) bits are preserved if @('regtype') is @('*xmm-access*'); + for @('*vex-xmm-access*'), these bits are zeroed out. For + @('*ymm-access*'), upper @(`(- 512 256)`) bits are zeroed + out. For @('*zmm-access*'), no upper bits are zeroed out.
" + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + + (b* ((data (the (unsigned-byte 512) (zmmi reg x86))) + (regtype (the (unsigned-byte 3) regtype)) + (data (case regtype + (#.*vex-xmm-access* + (loghead 128 data)) + (#.*ymm-access* + (loghead 256 data)) + (otherwise + ;; Preserve upper bits. + data))) + (val (the (unsigned-byte 128) (n128 val)))) + (!zmmi reg (part-install val data :low 0 :width 128) x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm x86p-wz128 + (implies (x86p x86) + (x86p (wz128 reg val x86 :regtype access))))) + + (defthm rz128-wz128-same + (equal (rz128 reg (wz128 reg val x86 :regtype access)) + (loghead 128 val)) + :hints (("Goal" + :in-theory (e/d (rz128 wz128) + (unsigned-byte-p + force (force)))))) + + (defthm rz128-wz128-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rz128 reg1 (wz128 reg2 val x86 :regtype access)) + (rz128 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (rz128 wz128) + (force (force)))))) + + (define wz256 + ((reg :type (unsigned-byte 5)) + (val :type (unsigned-byte 256)) + (x86) + &key + ((regtype vector-access-type-p) '*zmm-access*)) + + :short "Write @('val') to low 256 bits of a ZMM register." + :long "Upper bits: For @('*ymm-access*'), upper @(`(- + 512 256)`) bits are zeroed out. For @('*zmm-access*'), no upper + bits are zeroed out.
" + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + + (b* ((data (the (unsigned-byte 512) (zmmi reg x86))) + (regtype (the (unsigned-byte 3) regtype)) + (data (case regtype + (#.*ymm-access* + (loghead 256 data)) + (otherwise + ;; Preserve upper bits. + data))) + (val (the (unsigned-byte 256) (n256 val)))) + (!zmmi reg (part-install val data :low 0 :width 256) x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm x86p-wz256 + (implies (x86p x86) + (x86p (wz256 reg val x86 :regtype access))))) + + (defthm rz256-wz256-same + (equal (rz256 reg (wz256 reg val x86 :regtype access)) + (loghead 256 val)) + :hints (("Goal" + :in-theory (e/d (rz256 wz256) + (unsigned-byte-p + force (force)))))) + + (defthm rz256-wz256-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rz256 reg1 (wz256 reg2 val x86 :regtype access)) + (rz256 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (rz256 wz256) + (force (force)))))) + + (define wz512 + ((reg :type (unsigned-byte 5)) + (val :type (unsigned-byte 512)) + (x86)) + :short "Write @('val') to 512 bits of a ZMM register." + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d (loghead-to-logand + bitops::logsquash) + (bitops::logand-with-negated-bitmask + bitops::logand-with-bitmask + unsigned-byte-p)))) + + (let ((val (the (unsigned-byte 512) (n512 val)))) + (!zmmi reg val x86)) + + /// + + (local (in-theory (e/d () (force (force))))) + + (defthm x86p-wz512 + (implies (x86p x86) + (x86p (wz512 reg val x86))))) + + (defthm rz512-wz512-same + (equal (rz512 reg (wz512 reg val x86)) + (loghead 512 val)) + :hints (("Goal" + :in-theory (e/d (rz512 wz512) + (unsigned-byte-p + force (force)))))) + + (defthm rz512-wz512-different + (implies (not (equal (nfix reg1) (nfix reg2))) + (equal (rz512 reg1 (wz512 reg2 val x86)) + (rz512 reg1 x86))) + :hints (("Goal" + :in-theory (e/d (rz512 wz512) + (force (force)))))) + + (define zmmi-size + ((nbytes :type (unsigned-byte 7)) + (index :type (unsigned-byte 5)) + x86) + :enabled t + :guard (member nbytes '(4 8 16 32 64)) + :inline t + :no-function t + :returns (val natp :rule-classes :type-prescription) + (case nbytes + (4 (rz32 index x86)) + (8 (rz64 index x86)) + (16 (rz128 index x86)) + (32 (rz256 index x86)) + (64 (rz512 index x86)) + (otherwise + 0))) + + (define !zmmi-size + ((nbytes :type (unsigned-byte 7)) + (index :type (unsigned-byte 5)) + (val :type integer) + x86 + &key + ((regtype vector-access-type-p) '*zmm-access*)) + :short "Write @('val') to low @('nbytes') of a ZMM register" + :long "Upper bits: For XMM registers, upper @(`(- 512 + 128)`) bits are preserved if @('regtype') is @('*xmm-access*'); + for @('*vex-xmm-access*'), these bits are zeroed out. For + @('*ymm-access*'), upper @(`(- 512 256)`) bits are zeroed + out. For @('*zmm-access*'), no upper bits are zeroed out.
" + :enabled t + :guard (and (member nbytes '(4 8 16 32 64)) + (unsigned-byte-p (ash nbytes 3) val)) + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (x86p))))) + :inline t + :no-function t + (case nbytes + (4 (wz32 index val x86 :regtype regtype)) + (8 (wz64 index val x86 :regtype regtype)) + (16 (wz128 index val x86 :regtype regtype)) + (32 (wz256 index val x86 :regtype regtype)) + (64 (wz512 index val x86)) + (otherwise + x86)))) + +(defsection CTR-Reads-and-Writes + :parents (register-readers-and-writers) + :short "Functions to read/write into the control registers" + + (local (xdoc::set-default-parents CTR-Reads-and-Writes)) + + (define !ctri-write ((ctr-index natp) + (val n64p) + x86) + + :returns (x86 x86p :hyp (x86p x86)) + :guard (< ctr-index #.*control-register-names-len*) + + (b* ((val-write (case ctr-index + (*CR0* (loghead 32 val)) + ;; All but the low 21 bits of CR4 are reserved + (*CR4* (logapp 21 val (ctri *CR4* x86))) + (otherwise val))) + (x86 (!ctri ctr-index val-write x86))) + x86))) + +(defsection MSR-Reads-and-Writes + :parents (register-readers-and-writers) + :short "Functions to read/write into the MSRs" + + (local (xdoc::set-default-parents CTR-Reads-and-Writes)) + + (defun valid-msr-addr-p (msr-addr) + (declare (xargs :guard t)) + (member msr-addr '(#.*IA32_SYSENTER_CS* + #.*IA32_SYSENTER_ESP* + #.*IA32_SYSENTER_EIP* + #.*IA32_EFER* + #.*IA32_FS_BASE* + #.*IA32_GS_BASE* + #.*IA32_KERNEL_GS_BASE* + #.*IA32_STAR* + #.*IA32_LSTAR* + #.*IA32_FMASK* + #.*IA32_MISC_ENABLE*))) + + (defun msr-addr-to-index (msr-addr) + (declare (xargs :guard (valid-msr-addr-p msr-addr))) + (case msr-addr + (#.*IA32_SYSENTER_CS* #.*IA32_SYSENTER_CS-IDX*) + (#.*IA32_SYSENTER_ESP* #.*IA32_SYSENTER_ESP-IDX*) + (#.*IA32_SYSENTER_EIP* #.*IA32_SYSENTER_EIP-IDX*) + (#.*IA32_EFER* #.*IA32_EFER-IDX*) + (#.*IA32_FS_BASE* #.*IA32_FS_BASE-IDX*) + (#.*IA32_GS_BASE* #.*IA32_GS_BASE-IDX*) + (#.*IA32_KERNEL_GS_BASE* #.*IA32_KERNEL_GS_BASE-IDX*) + (#.*IA32_STAR* #.*IA32_STAR-IDX*) + (#.*IA32_LSTAR* #.*IA32_LSTAR-IDX*) + (#.*IA32_FMASK* #.*IA32_FMASK-IDX*) + (#.*IA32_MISC_ENABLE* #.*IA32_MISC_ENABLE-IDX*) + ;; Should not be possible due to the guard + (t -1))) + + (define msra ((msr-addr valid-msr-addr-p) + x86) + + :returns (msr-val n64p) + + (b* ((msr-index (msr-addr-to-index msr-addr))) + (msri msr-index x86))) + + (define !msra ((msr-addr valid-msr-addr-p) + (val :type (unsigned-byte 64)) + x86) + + :returns (x86 x86p :hyp (x86p x86)) + + (b* ((msr-index (msr-addr-to-index msr-addr))) + (!msri msr-index val x86)))) + + +(defsection XMMs-Reads-and-Writes + + :parents (ZMMs-Reads-and-Writes register-readers-and-writers) + + :short "Functions to read/write 32/64/128 bits into the XMM + registers (to be used by non-VEX encoded 128-bit SIMD instructions)" + + :long "These functions are meant to be used by instructions that + do not use a VEX or EVEX prefix --- these functions preserve the + upper bits of ZMM registers. For instructions that use these + prefixes and zero out these bits instead, see @(see + ZMMs-Reads-and-Writes).
+ +Note that the index for accessing the XMM registers is 4-bits + wide (as opposed to the 5-bit index for ZMM registers --- see @(see + ZMMs-Reads-and-Writes)) because only 16 XMM registers were supported + initially in the 64-bit mode (and 8 in the 32-bit mode).
+ +Source: Intel Vol. 2, Section 2.3.10 --- AVX Instructions + and Upper 128-bits of YMM registers:
+ +If an instruction with a destination XMM register is + encoded with a VEX prefix, the processor zeroes the upper + bits (above bit 128) of the equivalent YMM register. Legacy SSE + instructions without VEX preserve the upper bits.+ +
Functions @(see rx32), @(see rx64), and @(see rx128) will read the + contents of the XMMs (i.e., low 128 bits of ZMMs) as natural + numbers.
+ +Similarly, functions @(see wx32), @(see wx64), and @(see wx128) + are used to write natural numbers into the XMMs (and preserve the + upper bits of ZMMs).
" + + (local (xdoc::set-default-parents XMMs-Reads-and-Writes)) + + (define rx32 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :enabled t + (rz32 reg x86)) + + (define rx64 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :enabled t + (rz64 reg x86)) + + (define rx128 + ((reg :type (unsigned-byte 4)) + (x86)) + :inline t + :no-function t + :enabled t + (rz128 reg x86)) + + (define wx32 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 32)) + (x86)) + :short "Write @('val') to low 32 bits of a ZMM register; upper + bits are preserved." + :inline t + :no-function t + :enabled t + (wz32 reg val x86 :regtype #.*xmm-access*)) + + (define wx64 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 64)) + (x86)) + :short "Write @('val') to low 64 bits of a ZMM register; upper + bits are preserved." + :inline t + :no-function t + :enabled t + (wz64 reg val x86 :regtype #.*xmm-access*)) + + (define wx128 + ((reg :type (unsigned-byte 4)) + (val :type (unsigned-byte 128)) + (x86)) + :short "Write @('val') to low 128 bits of a ZMM register; upper + bits are preserved." + :inline t + :no-function t + :enabled t + (wz128 reg val x86 :regtype #.*xmm-access*)) + + (define xmmi-size + ((nbytes :type (unsigned-byte 5)) + (index :type (unsigned-byte 4)) + x86) + :enabled t + :guard (member nbytes '(4 8 16)) + :inline t + :no-function t + :returns (val natp :rule-classes :type-prescription) + (case nbytes + (4 (rx32 index x86)) + (8 (rx64 index x86)) + (16 (rx128 index x86)) + (otherwise + ;; This function shouldn't really be used in the case when nbytes + ;; != 4, 8, or 16. Anyway, the guard says nbytes shouldn't be + ;; anything else. + 0))) + + (define !xmmi-size + ((nbytes :type (unsigned-byte 5)) + (index :type (unsigned-byte 4)) + (val :type integer) + x86) + :short "Write @('val') to low @('nbytes') of an XMM register; + upper bits of the ZMM register are preserved." + :enabled t + :guard (and (member nbytes '(4 8 16)) + (unsigned-byte-p (ash nbytes 3) val)) + :returns (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (x86p))))) + :inline t + :no-function t + (case nbytes + (4 (wx32 index val x86)) + (8 (wx64 index val x86)) + (16 (wx128 index val x86)) + (otherwise + ;; This function shouldn't really be used in the case when nbytes + ;; != 4, 8, or 16. Anyway, the guard says nbytes shouldn't be + ;; anything else. + x86)))) + +;; ---------------------------------------------------------------------- + +(defsection characterizing-undefined-behavior + + :parents (machine) + + :short "An @('undef') field in the @('x86') state feeds unknown + values to processor components that are undefined" + + :long "The @('undef') field is used to feed unknown values to +processor components that are undefined, as per the Intel +specifications. For example, the @('SF'), @('ZF'), @('AF'), and +@('PF') rflags are undefined after a @('MUL') instruction is +executed.
+ +The principle behind the @('undef') field is quite like that of the +@('oracle') sub-field of the @('env') field \(see @(see +environment-field)\). We describe our use of the @('undef') field by +comparing it to the @('oracle') sub-field.
+ +For reasoning about programs that involve commonly occurring +\"undefined events\" \(like flag computations\), using the @('oracle') +sub-field can be quite tedious, since it has to be carefully +initialized; i.e., a list of appropriate \(symbolic or concrete\) +values has to be associated with the instruction pointer where any +such undefined event occurs. Imagine doing that for @('SF'), @('ZF'), +@('AF'), and @('PF') every time a @('MUL') instruction is executed. +The reason why we need this initialization is because the only way to +access the @('oracle') is through the functions @(see pop-x86-oracle) +and @(see env-read); @('pop-x86-oracle') expects the @('oracle') to +contain information in a specific format, and @('env-read') will give +us nothing unless we put something in to begin with.
+ +On the other hand, the @('undef') field doesn't require any such +initialization. The @('undef') field contains a natural number that +is increased every time an undefined value is pulled for use \(using +the constrained function @('create-undef')\) from a pool of undefined +values; thus, every access of the @('undef') field causes it to +contain a new value which is used to seed a unique undefined +value. See @(see undef-read) for details.
+ +There is a reason why we enforced that tediousness in the case of +the @('oracle') sub-field: it provides a way of tracking any +computation that relies on the @('env') field. Such computations don't +happen often, and when they do, it'd probably be better if we knew +exactly what we are expecting from the environment. Initializing the +@('oracle') is a way of expressing these expectations. However, in the +case of undefined values, we aren't really expecting anything from the +environment. All we want is a sort of infinite pool of arbitary +values, seeded from @('undef') in our case, that we don't know +anything about. As such, we wouldn't be able to prove that a value +obtained from @('undef') is equal \(or not\) to any other value, +either obtained from @('undef') or not. This is exactly what we need +when reasoning about undefined values --- an undefined value is +different from another undefined value, and also all the known +values.
" + + ) + +(encapsulate + ( ((create-undef *) => *) ) + + (local + (defun create-undef (x) + (nfix x))) + + (defthm integerp-create-undef + (integerp (create-undef x)) + :rule-classes (:rewrite :type-prescription)) + + (defthm natp-create-undef + (natp (create-undef x)) + :rule-classes (:rewrite :type-prescription))) + +(define unsafe-!undef (v x86) + + :long "Note that @('unsafe-!undef') is enabled, not untouchable, + and non-executable. It can be used in proof attempts but not during + execution.
+ +@('unsafe-!undef') should be used judiciously because updating the + @('undef') field with a value it held previously might contaminate + our 'pool of undefined values', i.e., @(see undef-read) might then + produce a call of @('create-undef') that collides with a previous + call of @('create-undef'), which would make the result of an + equality test between them equal instead of indeterminate.
+ +An example of an acceptable use of @('unsafe-!undef') is to + specify the @('undef') field in a final x86 state during a proof + attempt.
" + + :non-executable t + :enabled t + :returns (x86 x86p :hyp (x86p x86)) + :parents (undef-read) + (!undef v x86)) + +(define undef-read-logic (x86) + :enabled t + :returns (mv (unknown natp :rule-classes :type-prescription) + (x86 x86p :hyp (x86p x86))) + :parents (undef-read) + + (b* ((undef-seed (nfix (undef x86))) + (new-unknown (create-undef undef-seed)) + (x86 (!undef (1+ undef-seed) x86))) + (mv new-unknown x86))) + +(define undef-read (x86) + ;; TO-DO@Shilpi: I'll need to add more args to this function if I + ;; need the corresponding raw Lisp function to have more info. + :inline nil + :enabled t + :returns (mv (unknown natp :rule-classes :type-prescription) + (x86 x86p :hyp (x86p x86))) + :parents (characterizing-undefined-behavior) + + :short "Get a unique unknown to be used when reasoning about + undefined values in the processor" + :long "See @(see characterizing-undefined-behavior) for more + details.
+ +The accessor and updater functions of the @('undef') field are + untouchable so that the only way to create a new seed for unknowns + is via this function.
" + + (undef-read-logic x86)) + +;; We make the following two functions untouchable so that they cannot +;; be used on their own outside function undef-read-logic. + +(push-untouchable + (!undef$inline) + t) + +;; ---------------------------------------------------------------------- + +(defsection rflags-Reads-and-Writes + :parents (rflag-specifications register-readers-and-writers) + :short "Reading from and writing to the @('rflags') register in the @('x86') state" + + :long "We define convenient macros @('flgi') and @('!flgi') to read a + flag's value and to write a flag's value into the @('rflags') field in the + @('x86') state, respectively. Additionally, @('!flgi-undefined') can be + used to write an undefined value into a particular flag.
" + + (local (xdoc::set-default-parents rflags-Reads-and-Writes)) + + ;; Tips for doing flg RoW proofs: + ;; (thm (equal (RFLAGSBITS->CF (!rflagsbits->df df rflags)) + ;; (RFLAGSBITS->CF rflags)) + ;; ;; Need to enable the following rule for the flag being updated so that + ;; ;; the same kind of rule for the other one (RFLAGSBITS->CF-OF-RFLAGSBITS + ;; ;; here) can fire. + ;; :hints (("Goal" :in-theory (e/d (!RFLAGSBITS->DF-IS-RFLAGSBITS) + ;; ())))) + + ;; The following works out of the box. + ;; (thm (equal (RFLAGSBITS->CF (!rflagsbits->cf cf rflags)) + ;; (bfix cf))) + + (defmacro flgi (flg x86) + (declare (xargs :guard (keywordp flg))) + `(b* ((rflags (the (unsigned-byte 32) (rflags ,x86)))) + ,(case flg + (:cf '(rflagsBits->cf rflags)) + (:pf '(rflagsBits->pf rflags)) + (:af '(rflagsBits->af rflags)) + (:zf '(rflagsBits->zf rflags)) + (:sf '(rflagsBits->sf rflags)) + (:tf '(rflagsBits->tf rflags)) + (:if '(rflagsBits->intf rflags)) + (:df '(rflagsBits->df rflags)) + (:of '(rflagsBits->of rflags)) + (:iopl '(rflagsBits->iopl rflags)) + (:nt '(rflagsBits->nt rflags)) + (:rf '(rflagsBits->rf rflags)) + (:vm '(rflagsBits->vm rflags)) + (:ac '(rflagsBits->ac rflags)) + (:vif '(rflagsBits->vif rflags)) + (:vip '(rflagsBits->vip rflags)) + (:id '(rflagsBits->id rflags)) + (otherwise + (er hard 'flgi "~%FLGI: illegal flg keyword: ~x0.~%" flg))))) + + (defmacro !flgi (flg val x86) + (declare (xargs :guard (keywordp flg))) + `(b* ((rflags (the (unsigned-byte 32) (rflags ,x86))) + (new-rflags + (the (unsigned-byte 32) + ,(case flg + (:cf `(!rflagsBits->cf ,val rflags)) + (:pf `(!rflagsBits->pf ,val rflags)) + (:af `(!rflagsBits->af ,val rflags)) + (:zf `(!rflagsBits->zf ,val rflags)) + (:sf `(!rflagsBits->sf ,val rflags)) + (:tf `(!rflagsBits->tf ,val rflags)) + (:if `(!rflagsBits->intf ,val rflags)) + (:df `(!rflagsBits->df ,val rflags)) + (:of `(!rflagsBits->of ,val rflags)) + (:iopl `(!rflagsBits->iopl ,val rflags)) + (:nt `(!rflagsBits->nt ,val rflags)) + (:rf `(!rflagsBits->rf ,val rflags)) + (:vm `(!rflagsBits->vm ,val rflags)) + (:ac `(!rflagsBits->ac ,val rflags)) + (:vif `(!rflagsBits->vif ,val rflags)) + (:vip `(!rflagsBits->vip ,val rflags)) + (:id `(!rflagsBits->id ,val rflags)) + (otherwise + (er hard '!flgi "~%!FLGI: illegal flg keyword: ~x0.~%" flg)))))) + (!rflags new-rflags ,x86))) + + (define undef-flg-logic (x86) + :enabled t + :prepwork ((local (in-theory (e/d* () (undef-read))))) + :returns (mv (unknown natp :rule-classes :type-prescription) + (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (x86p)))))) + (undef-read x86)) + + (define undef-flg (x86) + ;; I have smashed this function under the hood. This is a tad more + ;; efficient than smashing just undef-read when flags are assigned + ;; undefined values because it helps us avoid calling n01 while + ;; execution. This saves one call to the builtin LOGAND that might + ;; create bignums, potentially. + :inline nil + :enabled t + :prepwork ((local (in-theory (e/d* () (undef-flg-logic))))) + :returns (mv (unknown-bit bitp :rule-classes :type-prescription) + (x86 x86p :hyp (x86p x86) + :hints (("Goal" :in-theory (e/d () (x86p)))))) + (b* (((mv val x86) + (undef-flg-logic x86))) + (mv (n01 val) x86))) + + (defmacro !flgi-undefined (flg x86) + (declare (xargs :guard (keywordp flg))) + `(b* ((rflags (the (unsigned-byte 32) (rflags ,x86))) + ((mv val x86) (undef-flg ,x86)) + (new-rflags + (the (unsigned-byte 32) + ,(case flg + (:cf '(!rflagsBits->cf val rflags)) + (:pf '(!rflagsBits->pf val rflags)) + (:af '(!rflagsBits->af val rflags)) + (:zf '(!rflagsBits->zf val rflags)) + (:sf '(!rflagsBits->sf val rflags)) + (:tf '(!rflagsBits->tf val rflags)) + (:if '(!rflagsBits->intf val rflags)) + (:df '(!rflagsBits->df val rflags)) + (:of '(!rflagsBits->of val rflags)) + (:iopl '(!rflagsBits->iopl val rflags)) + (:nt '(!rflagsBits->nt val rflags)) + (:rf '(!rflagsBits->rf val rflags)) + (:vm '(!rflagsBits->vm val rflags)) + (:ac '(!rflagsBits->ac val rflags)) + (:vif '(!rflagsBits->vif val rflags)) + (:vip '(!rflagsBits->vip val rflags)) + (:id '(!rflagsBits->id val rflags)) + (otherwise + (er hard '!flgi "~%!FLGI-UNDEFINED: illegal flg keyword: ~x0.~%" flg)))))) + (!rflags new-rflags ,x86))) + + (define write-user-rflags + ((user-flags-vector :type (unsigned-byte 32)) + (undefined-mask :type (unsigned-byte 32)) + x86) + + :inline t + :no-function t + + :short "Writing user rflags \(CF, PF, AF, ZF, SF, and OF\), + including undefined ones, to the x86 state" + + :long "We set the undefined flags, which are indicated by + @('mask'), to the value returned by @(see undef-read).
" + + :guard-hints (("Goal" :in-theory (e/d (rflagsBits->cf + rflagsBits->pf + rflagsBits->af + rflagsBits->zf + rflagsBits->sf + rflagsBits->of + rflagsbits-fix) + (unsigned-byte-p + not + (tau-system))))) + :prepwork ((local (in-theory (e/d () + (bitops::logand-with-negated-bitmask + fty::unsigned-byte-p-1-when-bitp))))) + + :returns (x86 x86p :hyp (x86p x86)) + + (b* ((user-flags-vector + (mbe :logic (n32 user-flags-vector) :exec user-flags-vector)) + (undefined-mask + (mbe :logic (n32 undefined-mask) :exec undefined-mask)) + ((the (unsigned-byte 32) input-rflags) + (mbe :logic (n32 (rflags x86)) :exec (rflags x86)))) + + (mbe + :logic + (b* ((x86 (if (equal (rflagsBits->cf undefined-mask) 1) + (!flgi-undefined :cf x86) + (!flgi :cf (rflagsBits->cf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->pf undefined-mask) 1) + (!flgi-undefined :pf x86) + (!flgi :pf (rflagsBits->pf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->af undefined-mask) 1) + (!flgi-undefined :af x86) + (!flgi :af (rflagsBits->af user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->zf undefined-mask) 1) + (!flgi-undefined :zf x86) + (!flgi :zf (rflagsBits->zf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->sf undefined-mask) 1) + (!flgi-undefined :sf x86) + (!flgi :sf (rflagsBits->sf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->of undefined-mask) 1) + (!flgi-undefined :of x86) + (!flgi :of (rflagsBits->of user-flags-vector) x86)))) + x86) + :exec + (if (eql undefined-mask 0) + (b* ((x86 (!flgi :cf (rflagsBits->cf user-flags-vector) x86)) + (x86 (!flgi :pf (rflagsBits->pf user-flags-vector) x86)) + (x86 (!flgi :af (rflagsBits->af user-flags-vector) x86)) + (x86 (!flgi :zf (rflagsBits->zf user-flags-vector) x86)) + (x86 (!flgi :sf (rflagsBits->sf user-flags-vector) x86)) + (x86 (!flgi :of (rflagsBits->of user-flags-vector) x86))) + x86) + (b* ((x86 (if (equal (rflagsBits->cf undefined-mask) 1) + (!flgi-undefined :cf x86) + (!flgi :cf (rflagsBits->cf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->pf undefined-mask) 1) + (!flgi-undefined :pf x86) + (!flgi :pf (rflagsBits->pf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->af undefined-mask) 1) + (!flgi-undefined :af x86) + (!flgi :af (rflagsBits->af user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->zf undefined-mask) 1) + (!flgi-undefined :zf x86) + (!flgi :zf (rflagsBits->zf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->sf undefined-mask) 1) + (!flgi-undefined :sf x86) + (!flgi :sf (rflagsBits->sf user-flags-vector) x86))) + (x86 (if (equal (rflagsBits->of undefined-mask) 1) + (!flgi-undefined :of x86) + (!flgi :of (rflagsBits->of user-flags-vector) x86)))) + x86)))) + + /// + + (local (in-theory (e/d (undef-read) ()))) + + (defthm xr-write-user-rflags + (implies (and (not (equal fld :rflags)) + (not (equal fld :undef))) + (equal (xr fld index (write-user-rflags flags mask x86)) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* () (force (force)))))) + + (defthm xr-write-user-rflags-no-mask + ;; Do we need this? + (implies (not (equal fld :rflags)) + (equal (xr fld index (write-user-rflags flags 0 x86)) + (xr fld index x86))) + :hints (("Goal" :in-theory (e/d* () (force (force)))))) + + (defthm rflags-and-write-user-rflags-no-mask + (equal (write-user-rflags user-flags-vector 0 x86) + (b* ((x86 (!flgi :cf (rflagsBits->cf user-flags-vector) x86)) + (x86 (!flgi :pf (rflagsBits->pf user-flags-vector) x86)) + (x86 (!flgi :af (rflagsBits->af user-flags-vector) x86)) + (x86 (!flgi :zf (rflagsBits->zf user-flags-vector) x86)) + (x86 (!flgi :sf (rflagsBits->sf user-flags-vector) x86)) + (x86 (!flgi :of (rflagsBits->of user-flags-vector) x86))) + x86)) + :hints (("Goal" :in-theory (e/d* (rflagsBits->cf + rflagsBits->pf + rflagsBits->af + rflagsBits->zf + rflagsBits->sf + rflagsBits->of + rflagsbits-fix) + (force (force)))))))) + + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/rflags-spec.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/rflags-spec.lisp new file mode 100644 index 000000000..f00a3b70d --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/rflags-spec.lisp @@ -0,0 +1,866 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; 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 GoelGeneral @('CF') Specification (Source: Intel Manuals, + Vol. 1, Section 3.4.3.1):
+ +Carry flag Set if an arithmetic operation generates a +carry or a borrow out of the most-significant bit of the result; +cleared otherwise. This flag indicates an overflow condition for +unsigned-integer arithmetic. It is also used in multiple-precision +arithmetic.
" + :parents (rflag-specifications) + :inline t + :no-function t + :guard (and (natp result-nbits) + (natp raw-result)) + (acl2::bool->bit (not (unsigned-byte-p result-nbits raw-result))) + + /// + + (defthm-unsigned-byte-p n01p-general-cf-spec-fn + :bound 1 + :concl (general-cf-spec-fn result-nbits raw-result) + :gen-linear t + :gen-type t)) + +(define cf-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "CF-SPEC" result-nbits) + ((raw-result :type (unsigned-byte ,(1+ result-nbits)))) + :inline t + :no-function t + :parents (rflag-specifications) + (acl2::bool->bit (mbe :logic (not (unsigned-byte-p ,result-nbits raw-result)) + :exec (not (< raw-result ,(expt 2 result-nbits))))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-CF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "CF-SPEC" result-nbits) raw-result) + :gen-linear t + :gen-type t))) + +(make-event (cf-spec-gen-fn 8)) +(make-event (cf-spec-gen-fn 16)) +(make-event (cf-spec-gen-fn 32)) +(make-event (cf-spec-gen-fn 64)) + +(defmacro general-cf-spec (result-nbits raw-result) + (cond ((eql result-nbits 8) + `(cf-spec8 ,raw-result)) + ((eql result-nbits 16) + `(cf-spec16 ,raw-result)) + ((eql result-nbits 32) + `(cf-spec32 ,raw-result)) + ((eql result-nbits 64) + `(cf-spec64 ,raw-result)) + (t + `(general-cf-spec-fn ,result-nbits ,raw-result)))) + +(add-macro-alias general-cf-spec general-cf-spec-fn) + +;; ---------------------------------------------------------------------- + +(define general-of-spec-fn (result-nbits signed-raw-result) + :guard (and (natp result-nbits) + (integerp signed-raw-result)) + :long "General @('OF') Specification (Source: Intel Manuals, + Vol. 1, Section 3.4.3.1):
+ +Overflow flag Set if the integer result is too large a +positive number or too small a negative number (excluding the +sign-bit) to fit in the destination operand; cleared otherwise. This +flag indicates an overflow condition for signed-integer (two s +complement) arithmetic.
" + :parents (rflag-specifications) + + (acl2::bool->bit (not (signed-byte-p result-nbits signed-raw-result))) + + /// + + (defthm-unsigned-byte-p n01p-general-of-spec-fn + :bound 1 + :concl (general-of-spec-fn result-nbits signed-raw-result))) + +(define of-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "OF-SPEC" result-nbits) + ((signed-raw-result :type (signed-byte ,(1+ result-nbits)))) + :inline t + :no-function t + :parents (rflag-specifications) + (acl2::bool->bit (mbe :logic (not (signed-byte-p ,result-nbits signed-raw-result)) + :exec (or + (not (<= ,(- (expt 2 (1- result-nbits))) signed-raw-result)) + (not (< signed-raw-result ,(expt 2 (1- result-nbits))))))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-OF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "OF-SPEC" result-nbits) signed-raw-result) + :gen-type t + :gen-linear t))) + +(make-event (of-spec-gen-fn 8)) +(make-event (of-spec-gen-fn 16)) +(make-event (of-spec-gen-fn 32)) +(make-event (of-spec-gen-fn 64)) + +(defmacro general-of-spec (result-nbits signed-raw-result) + (cond ((eql result-nbits 8) + `(of-spec8 ,signed-raw-result)) + ((eql result-nbits 16) + `(of-spec16 ,signed-raw-result)) + ((eql result-nbits 32) + `(of-spec32 ,signed-raw-result)) + ((eql result-nbits 64) + `(of-spec64 ,signed-raw-result)) + (t + `(general-of-spec-fn ,result-nbits ,signed-raw-result)))) + +(add-macro-alias general-of-spec general-of-spec-fn) + +;; ---------------------------------------------------------------------- + +(define zf-spec + ;; CCL generates great code for this function, even without type + ;; declarations. + ((result :type (integer 0 *))) + :inline t + :no-function t + :long "General @('ZF') Specification (Source: Intel Manuals, + Vol. 1, Section 3.4.3.1):
+ +Zero flag Set if the result is zero; cleared +otherwise.
" + :parents (rflag-specifications) + + (if (equal result 0) 1 0) + /// + + (defthm-unsigned-byte-p n01p-zf-spec + :bound 1 + :concl (zf-spec result) + :gen-type t + :gen-linear t) + + (defthm zf-spec-thm + (implies (not (equal x 0)) + (equal (zf-spec x) 0)))) + +;; ---------------------------------------------------------------------- + +;; [Shilpi]: I could have put the theorems preceding the define of +;; pf-spec inside its :prepwork, but I believe that these theorems can +;; be useful later on too. Putting them outside the define makes them +;; more visible. + +(local + (encapsulate + () + (local (include-book "arithmetic-5/top" :dir :system)) + + (defthm logbitp-and-loghead + (implies (integerp x) + (equal (acl2::bool->bit (logbitp 0 x)) + (loghead 1 x))) + :hints (("Goal" :in-theory + (e/d (acl2::bool->bit + evenp oddp + logbitp + loghead) + ())))) + + (defthm logbitp-and-logtail + (implies (unsigned-byte-p (1+ n) x) + (equal (acl2::bool->bit (logbitp n x)) + (logtail n x))) + :hints (("Goal" :in-theory + (e/d (acl2::bool->bit + evenp oddp + logbitp + logtail + nfix) + ())))))) + +(defthm logcount-and-loghead + (implies (and (integerp x) + (natp n)) + (<= (logcount (loghead n x)) n)) + :hints (("Goal" :in-theory + (e/d* (acl2::ihsext-inductions acl2::ihsext-recursive-redefs) + (logcount loghead))))) + + +(defthm logcount-bound + (<= (logcount x) (integer-length x)) + :hints (("Goal" :in-theory + (e/d* (acl2::ihsext-inductions + acl2::ihsext-recursive-redefs) + (logcount)))) + :rule-classes :linear) + +(encapsulate + () + (local (include-book "centaur/bitops/signed-byte-p" :dir :system)) + + (defthm unsigned-byte-p-and-integer-length + (implies (and (unsigned-byte-p n x) + (natp n)) + (<= (integer-length x) n)) + :hints (("Goal" :in-theory (e/d* (acl2::ihsext-inductions + acl2::ihsext-recursive-redefs) + ()))) + :rule-classes :linear)) + + +(define bitcount8 + ((x :type (unsigned-byte 8))) + :measure (integer-length x) + :inline t + :no-function t + :verify-guards nil + :enabled t + (if (zp x) + 0 + (+ (the (unsigned-byte 1) + (mbe :logic + ;; (logcar x) + (loghead 1 x) + :exec (logand 1 x))) + (the (integer 0 8) + (bitcount8 + (the (unsigned-byte 8) + (mbe :logic + ;; (logcdr x) + (logtail 1 x) + :exec (ash x -1))))))) + /// + + + (defthm bitcount8-and-logcount + (implies (unsigned-byte-p 8 x) + (equal (bitcount8 x) + (logcount x))) + :hints (("Goal" :in-theory + (e/d* (acl2::ihsext-inductions + acl2::ihsext-recursive-redefs) + (logcount))))) + + (local + (defthm logtail-1-bound-linear + (implies (and (unsigned-byte-p 8 x) + (unsigned-byte-p 7 (logtail 1 x))) + (<= (integer-length (logtail 1 x)) 7)) + :hints (("Goal" :use ((:instance unsigned-byte-p-and-integer-length + (x (logtail 1 x)) + (n 7))) + :in-theory (e/d () + (unsigned-byte-p-and-integer-length)))) + :rule-classes :linear)) + + (verify-guards bitcount8$inline)) + +(define pf-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "PF-SPEC" result-nbits) + ((result :type (unsigned-byte ,result-nbits))) + :parents (rflag-specifications) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d () (unsigned-byte-p)))) + + (mbe :logic (acl2::bool->bit (not + (logbitp + 0 + (logcount + ;; Only the least significant byte + ;; (i.e. the low 8 bits) is considered + ;; for calculating the parity flag + ;; (Intel manual, Vol. 1, Section 3.4.3.1). + ,(if (equal result-nbits 8) + `result + `(loghead 8 result)))))) + :exec (if (eql (logand 1 + (the (integer 0 8) + (bitcount8 + (the (unsigned-byte 8) + ,(if (equal result-nbits 8) + `result + `(logand 255 result)))))) + 0) + 1 0)) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-PF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "PF-SPEC" result-nbits) result) + :gen-type t + :gen-linear t))) + +(make-event (pf-spec-gen-fn 8)) +(make-event (pf-spec-gen-fn 16)) +(make-event (pf-spec-gen-fn 32)) +(make-event (pf-spec-gen-fn 64)) + +(define general-pf-spec-fn + ((result-nbits :type (member 8 16 32 64)) + (result :type (integer 0 *))) + :long "General @('PF') Specification (Source: Intel Manuals, + Vol. 1, Section 3.4.3.1):
+ +Parity flag Set if the least-significant byte of the +result contains an even number of 1 bits; cleared otherwise.
" + :parents (rflag-specifications) + :inline t + :no-function t + :guard-hints (("Goal" :in-theory (e/d () (unsigned-byte-p)))) + :guard (unsigned-byte-p result-nbits result) + (mbe :logic (acl2::bool->bit (not (logbitp 0 (logcount (loghead 8 result))))) + :exec (if (eql (logand 1 + (the (integer 0 8) + (bitcount8 + (the (unsigned-byte 8) + (logand 255 + (if (eql result-nbits 64) + result + (the (unsigned-byte 32) + result))))))) + 0) + 1 0)) + + /// + + (defthm-unsigned-byte-p n01p-general-pf-spec-fn + :bound 1 + :concl (general-pf-spec-fn result-nbits result) + :gen-type t + :gen-linear t)) + +(defmacro general-pf-spec (result-nbits result) + (cond ((eql result-nbits 8) + `(pf-spec8 ,result)) + ((eql result-nbits 16) + `(pf-spec16 ,result)) + ((eql result-nbits 32) + `(pf-spec32 ,result)) + ((eql result-nbits 64) + `(pf-spec64 ,result)) + (t + `(general-pf-spec-fn ,result-nbits ,result)))) + +(add-macro-alias general-pf-spec general-pf-spec-fn) + +;; ---------------------------------------------------------------------- + +(define sf-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "SF-SPEC" result-nbits) + ((result :type (unsigned-byte ,result-nbits))) + :inline t + :no-function t + + :parents (rflag-specifications) + + (mbe + :logic + (part-select result :low ,(1- result-nbits) :width 1) + :exec + (the (unsigned-byte 1) (ash result ,(- (1- result-nbits))))) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-SF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "SF-SPEC" result-nbits) result) + :gen-type t + :gen-linear t) + )) + +(make-event (sf-spec-gen-fn 8)) +(make-event (sf-spec-gen-fn 16)) +(make-event (sf-spec-gen-fn 32)) +(make-event (sf-spec-gen-fn 64)) + +(define general-sf-spec-fn + ((result-nbits :type (member 8 16 32 64)) + (result :type (integer 0 *))) + :inline t + :no-function t + :guard (unsigned-byte-p result-nbits result) + :long "General @('SF') Specification (Source: Intel Manuals, + Vol. 1, Section 3.4.3.1):
+ +Sign flag Set equal to the most-significant bit of the +result, which is the sign bit of a signed integer. (0 indicates a +positive value and 1 indicates a negative value.)
" + :parents (rflag-specifications) + + (mbe + :logic + (part-select result :low (1- result-nbits) :width 1) + :exec + (the (unsigned-byte 1) + (ash + (if (eql result-nbits 64) + result + (the (unsigned-byte 50) result)) + (the (integer -63 0) + (- (the (integer 0 63) + (1- (the (integer 0 64) result-nbits)))))))) + /// + + (defthm-unsigned-byte-p n01p-general-sf-spec-fn + :bound 1 + :concl (general-sf-spec-fn result-nbits result) + :gen-type t + :gen-linear t)) + +(defmacro general-sf-spec (result-nbits result) + (cond ((eql result-nbits 8) + `(sf-spec8 ,result)) + ((eql result-nbits 16) + `(sf-spec16 ,result)) + ((eql result-nbits 32) + `(sf-spec32 ,result)) + ((eql result-nbits 64) + `(sf-spec64 ,result)) + (t + `(general-sf-spec-fn ,result-nbits ,result)))) + +(add-macro-alias general-sf-spec general-sf-spec-fn) + +;; ---------------------------------------------------------------------- + +;; Instruction-specific AF specification: + +(define add-af-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "ADD-AF-SPEC" result-nbits) + ((dst :type (unsigned-byte ,result-nbits)) + (src :type (unsigned-byte ,result-nbits))) + :inline t + :no-function t + :parents (rflag-specifications) + + (b* (((the (unsigned-byte 4) dst-3-0) + (mbe :logic (part-select dst :low 0 :width 4) + :exec (logand #xF dst))) + ((the (unsigned-byte 4) src-3-0) + (mbe :logic (part-select src :low 0 :width 4) + :exec (logand #xF src))) + + (add + (the (unsigned-byte 5) + (+ (the (unsigned-byte 4) dst-3-0) + (the (unsigned-byte 4) src-3-0)))) + + (af (the (unsigned-byte 1) (bool->bit (< #xF add))))) + + af) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-ADD-AF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "ADD-AF-SPEC" result-nbits) dst src) + :gen-linear t + :gen-type t))) + +(make-event (add-af-spec-gen-fn 8)) +(make-event (add-af-spec-gen-fn 16)) +(make-event (add-af-spec-gen-fn 32)) +(make-event (add-af-spec-gen-fn 64)) + +(defmacro add-af-spec (result-nbits dst src) + (cond ((eql result-nbits 8) + `(add-af-spec8 ,dst ,src)) + ((eql result-nbits 16) + `(add-af-spec16 ,dst ,src)) + ((eql result-nbits 32) + `(add-af-spec32 ,dst ,src)) + (t + `(add-af-spec-fn + ,result-nbits ,dst ,src)))) + +(define sub-af-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "SUB-AF-SPEC" result-nbits) + ((dst :type (unsigned-byte ,result-nbits)) + (src :type (unsigned-byte ,result-nbits))) + :inline t + :no-function t + :parents (rflag-specifications) + + (b* (((the (unsigned-byte 4) dst-3-0) + (mbe :logic (part-select dst :low 0 :width 4) + :exec (logand #xF dst))) + ((the (unsigned-byte 4) src-3-0) + (mbe :logic (part-select src :low 0 :width 4) + :exec (logand #xF src))) + + (sub + (the (signed-byte 5) + (- (the (unsigned-byte 4) dst-3-0) + (the (unsigned-byte 4) src-3-0)))) + + (af (the (unsigned-byte 1) (bool->bit (< sub 0))))) + + af) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-SUB-AF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "SUB-AF-SPEC" result-nbits) + dst src) + :gen-linear t + :gen-type t))) + +(make-event (sub-af-spec-gen-fn 8)) +(make-event (sub-af-spec-gen-fn 16)) +(make-event (sub-af-spec-gen-fn 32)) +(make-event (sub-af-spec-gen-fn 64)) + +(defmacro sub-af-spec + (result-nbits dst src) + (cond ((eql result-nbits 8) + `(sub-af-spec8 ,dst ,src)) + ((eql result-nbits 16) + `(sub-af-spec16 ,dst ,src)) + ((eql result-nbits 32) + `(sub-af-spec32 ,dst ,src)) + (t + `(sub-af-spec-fn + ,result-nbits ,dst ,src)))) + +(define adc-af-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "ADC-AF-SPEC" result-nbits) + ((dst :type (unsigned-byte ,result-nbits)) + (src :type (unsigned-byte ,result-nbits)) + (cf :type (unsigned-byte 1))) + :inline t + :no-function t + :parents (rflag-specifications) + + (b* (((the (unsigned-byte 4) dst-3-0) + (mbe :logic (part-select dst :low 0 :width 4) + :exec (logand #xF dst))) + ((the (unsigned-byte 4) src-3-0) + (mbe :logic (part-select src :low 0 :width 4) + :exec (logand #xF src))) + + (adc + (the (unsigned-byte 6) + (+ (the (unsigned-byte 4) dst-3-0) + (the (unsigned-byte 4) src-3-0) + (the (unsigned-byte 1) cf)))) + + (af (the (unsigned-byte 1) (bool->bit (< #xF adc))))) + + af) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-ADC-AF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "ADC-AF-SPEC" result-nbits) + dst src cf) + :gen-linear t + :gen-type t))) + +(make-event (adc-af-spec-gen-fn 8)) +(make-event (adc-af-spec-gen-fn 16)) +(make-event (adc-af-spec-gen-fn 32)) +(make-event (adc-af-spec-gen-fn 64)) + +(defmacro adc-af-spec + (result-nbits dst src cf) + (cond ((eql result-nbits 8) + `(adc-af-spec8 ,dst ,src ,cf)) + ((eql result-nbits 16) + `(adc-af-spec16 ,dst ,src ,cf)) + ((eql result-nbits 32) + `(adc-af-spec32 ,dst ,src ,cf)) + (t + `(adc-af-spec-fn + ,result-nbits ,dst ,src ,cf)))) + +(define sbb-af-spec-gen-fn (result-nbits) + :verify-guards nil + + `(define ,(mk-name "SBB-AF-SPEC" result-nbits) + ((dst :type (unsigned-byte ,result-nbits)) + (src :type (unsigned-byte ,result-nbits)) + (cf :type (unsigned-byte 1))) + :inline t + :no-function t + :parents (rflag-specifications) + + (b* (((the (unsigned-byte 4) dst-3-0) + (mbe :logic (part-select dst :low 0 :width 4) + :exec (logand #xF dst))) + ((the (unsigned-byte 4) src-3-0) + (mbe :logic (part-select src :low 0 :width 4) + :exec (logand #xF src))) + + ((the (unsigned-byte 6) sbb) + (logand #x3F (- (the (unsigned-byte 4) dst-3-0) + (+ (the (unsigned-byte 4) src-3-0) + (the (unsigned-byte 4) cf))))) + + (af (the (unsigned-byte 1) (bool->bit (> sbb #xF))))) + + af) + + /// + + (defthm-unsigned-byte-p ,(mk-name "N01P-SBB-AF-SPEC" result-nbits) + :bound 1 + :concl (,(mk-name "SBB-AF-SPEC" result-nbits) + dst src cf) + :gen-linear t + :gen-type t))) + +(make-event (sbb-af-spec-gen-fn 8)) +(make-event (sbb-af-spec-gen-fn 16)) +(make-event (sbb-af-spec-gen-fn 32)) +(make-event (sbb-af-spec-gen-fn 64)) + +(defmacro sbb-af-spec + (result-nbits dst src cf) + (cond ((eql result-nbits 8) + `(sbb-af-spec8 ,dst ,src ,cf)) + ((eql result-nbits 16) + `(sbb-af-spec16 ,dst ,src ,cf)) + ((eql result-nbits 32) + `(sbb-af-spec32 ,dst ,src ,cf)) + (t + `(sbb-af-spec-fn + ,result-nbits ,dst ,src ,cf)))) + +;; ---------------------------------------------------------------------- + +;; Some arithmetic theorems that will be used in all books higher up +;; (especially for proving away MBEs): + +(encapsulate + () + + (local (include-book "arithmetic-5/top" :dir :system)) + + (defun power-of-2p-measure (x) + (cond ((or (not (natp x)) + (<= x 1)) + 0) + (t (floor x 1)))) + + (defn is-power-of-2p (x) + (declare (xargs :measure (power-of-2p-measure x))) + (if (natp x) + (if (<= x 1) + t + (is-power-of-2p (* 1/2 x))) + nil)) + + (local + (set-default-hints + '((acl2::nonlinearp-default-hint++ acl2::id acl2::stable-under-simplificationp + acl2::hist nil)))) + + (defthm ash-bounds-with-powers-of-two + ;; A general ash bounds theorem + (implies (and (integerp i) + (<= 0 i) + (equal 2-to-x (* 2-to-x-y (expt 2 y))) + (< i 2-to-x) + (syntaxp (quotep 2-to-x)) + (is-power-of-2p 2-to-x) + (< (expt 2 y) 2-to-x) + (syntaxp (quotep y)) + (syntaxp (quotep 2-to-x-y)) + (integerp y) + (<= 0 y)) + (< (ash i (- y)) 2-to-x-y)) + :rule-classes :rewrite) + + ) ;; End of encapsulate + +(local + (defun my-induct (x y) + (if (and (natp x) + (natp y)) + (if (zp x) + y + (if (zp y) + x + (my-induct (logcdr x) (logcdr y)))) + nil))) + +(defthm integer-length-of-logior-of-natp + (implies (and (natp x) + (natp y) + (<= (integer-length x) (integer-length y))) + (equal (integer-length (logior x y)) + (integer-length y))) + :hints (("Goal" :induct (my-induct x y) + :in-theory (e/d (acl2::integer-length**) + (integer-length))))) + +(defthm integer-length-and-loghead-when-natp-identity-theorem + (implies (natp x) + (equal (loghead (integer-length x) x) + x)) + :hints (("Goal" :in-theory (e/d* (acl2::loghead** acl2::integer-length** + acl2::ihsext-inductions) + ((force)))))) + +(defthm mod-expt-and-integer-length-identity-theorem + (implies (natp x) + (equal (mod x (expt 2 (integer-length x))) + x)) + :hints (("Goal" :in-theory (e/d* (acl2::ihsext-arithmetic) + (bitops::ash-1-removal))))) + +(encapsulate + () + + (local (include-book "arithmetic-5/top" :dir :system)) + + ;; The following are useful for MBE kind of proofs. Be careful when + ;; using them though; sometimes, they might cause loops when enabled + ;; alongside logand-with-negated-bitmask and + ;; logand-with-bitmask. + + (defthmd negative-logand-to-positive-logand-with-natp-x + (implies (and (syntaxp (and (quotep n) + (let* ((n-abs (acl2::unquote n))) + (< n-abs 0)))) + (equal m (integer-length x)) + (integerp n) + (natp x)) + (equal (logand n x) + (logand (logand (1- (ash 1 m)) n) x)))) + + (defun find-best-fitting-m (n) + (if (signed-byte-p 8 (acl2::unquote n)) + (list (list (cons 'm ''8)) + (list (cons 'm ''16)) + (list (cons 'm ''32)) + (list (cons 'm ''64)) + (list (cons 'm ''128))) + (if (signed-byte-p 16 (acl2::unquote n)) + (list (list (cons 'm ''16)) + (list (cons 'm ''32)) + (list (cons 'm ''64)) + (list (cons 'm ''128))) + (if (signed-byte-p 32 (acl2::unquote n)) + (list (list (cons 'm ''32)) + (list (cons 'm ''64)) + (list (cons 'm ''128))) + (list (list (cons 'm ''64)) + (list (cons 'm ''128))))))) + + (defthm negative-logand-to-positive-logand-with-integerp-x + (implies (and (syntaxp (and (quotep n) + (let* ((n-abs (acl2::unquote n))) + (< n-abs 0)))) + (bind-free (find-best-fitting-m n) (m)) + (unsigned-byte-p m x) + (integerp n)) + (equal (logand n x) + (logand (logand (1- (ash 1 m)) n) x)))) + + (defthmd loghead-to-logand + ;; This rule causes loop when used alongside + ;; bitops::logand-with-bitmask and bitops::logand-with-negated-bitmask. + (implies (and (natp n) + (syntaxp (quotep n)) + (integerp x)) + (equal (loghead n x) + (logand (1- (expt 2 n)) x))) + :hints (("Goal" :in-theory (e/d (loghead) ())))) + + (defthm logand-redundant + (implies (and (unsigned-byte-p n x) + (equal width (1- (ash 1 n))) + (syntaxp (quotep width))) + (equal (logand width x) + x))) + + (defthm weed-out-irrelevant-logand-when-first-operand-constant + (implies (and + ;; syntaxp will restrict the application of this + ;; theorem... + (syntaxp (quotep x)) + (unsigned-byte-p n y) + (equal (logand (1- (expt 2 n)) x) (1- (expt 2 n)))) + (equal (logand x y) y)) + :hints (("Goal" + :use ((:instance acl2::mod-logand (x x) (y y) (n n))) + :in-theory (disable acl2::mod-logand))))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/state.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/state.lisp new file mode 100644 index 000000000..66f0e1520 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/state.lisp @@ -0,0 +1,993 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, May - August 2023, Regents of the University of Texas +; Copyright (C) August 2023 - May 2024, Yahya Sohail +; Copyright (C) May 2024 - August 2024, Intel Corporation +; 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 GoelWe use the @(tsee bigmems::bigmems) library to model the + memory. The default model is @(tsee bigmem::bigmem). This model is + \"attachable,\" which allows one to replace it in execution with a + logically equivalent stobj. Here's why you'd want to replace it:
+ +@(tsee bigmem::bigmem) provides constant access time for the entire address +space. However, accessing a byte requires walking a 3 level hierarchy.
+ +@(tsee bigmem-asymmetric::bigmem-asymmetric) provides faster access to the +lower 6GB of physical memory, because those values are stored in an array, but +slower access to the rest of the address space, since it stores the rest of the +values in an ordered alist.
+ +If you're only going to be using the lower 6GB of memory (or mostly using +the lower 6 GB) of memory, using the @(tsee +bigmem-asymmetric::bigmem-asymmetric) makes sense, otherwise use @(tsee +bigmem::bigmem)
+ +By default, the model uses @(tsee bigmem::bigmem). To switch to @(tsee +bigmem-asymmetric::bigmem-asymmetric), submit the following events to ACL2 +before including the x86 books:
+ +
+(include-book \"centaur/bigmems/bigmem-asymmetric/bigmem-asymmetric\" :dir :system)
+(attach-stobj bigmem::mem bigmem-asymmetric::mem)
+")
+
+(defsection environment-field
+
+ :parents (x86isa-state)
+
+ :short "An environment field that includes a simple model of the
+ file system and an oracle"
+
+ :long "The environment allows reasoning about non-deterministic + events and computations in the ISA, like the @('RDRAND') instruction + and in the user-level mode, the @('SYSCALL') instruction.
" + + (define rip-ret-alistp (lst) + :short "Recognizer for the @('oracle') sub-field of the environment + field @('env')" + :long "A valid @('rip-ret-alistp') associates canonical linear + addresses, i.e., 48-bit integers in our specification, to a list + of arbitrary values. For example, '((1 . ((-1 . 12) 5000)) (2 + . (0))) means that if the oracle in the environment field is + consulted at address 1, '(-1 . 12) will be + popped out. If the oracle is consulted again at the address + 1, then 5000 will be popped out.
" + :parents (environment-field) + (if (atom lst) + (equal lst nil) + (and (consp (car lst)) + (i48p (caar lst)) + (true-listp (cdar lst)) + (rip-ret-alistp (cdr lst)))) + /// + + (defthm rip-ret-alistp-fwd-chaining-alistp + (implies (rip-ret-alistp x) + (alistp x)) + :rule-classes :forward-chaining)) + + (define env-alistp (env) + :short "Recognizer of the environment field @('env')" + :long "As of now, @('env-alistp') consists of three sub-fields, + all of which are basically alists --- file descriptors, file + contents, and an oracle field.
" + :parents (environment-field) + (or (equal env nil) + (and (alistp env) + (equal (len env) 3) + (consp (assoc :FILE-DESCRIPTORS env)) + (alistp (cdr (assoc :FILE-DESCRIPTORS env))) + (consp (assoc :FILE-CONTENTS env)) + (alistp (cdr (assoc :FILE-CONTENTS env))) + (consp (assoc :ORACLE env)) + (rip-ret-alistp (cdr (assoc :ORACLE env))))) + + /// + + (defun env-alist-fix (x) + (declare (xargs :guard (env-alistp x))) + (mbe :logic (if (env-alistp x) x nil) + :exec x)) + + (defthm env-alistp-fwd-chaining-alistp + (implies (env-alistp x) + (alistp x)) + :rule-classes :forward-chaining) + + (defrulel nth-and-assoc-equal-check + (implies (and (alistp x) + (not (equal (car (nth 0 x)) :oracle)) + (equal (car (nth 1 x)) :oracle)) + (equal (nth 1 x) + (assoc-equal :oracle x)))) + + (defthm env-alistp-implies-rip-ret-alistp + (implies (env-alistp x) + (rip-ret-alistp (cdr (assoc-equal :oracle x)))) + :rule-classes (:rewrite :forward-chaining)) + + (defthm env-alistp-implies-alistp-file-descriptors + (implies (env-alistp x) + (alistp (cdr (assoc-equal :file-descriptors x)))) + :rule-classes (:rewrite :forward-chaining)) + + (defthm env-alistp-implies-alistp-file-contents + (implies (env-alistp x) + (alistp (cdr (assoc-equal :file-contents x)))) + :rule-classes (:rewrite :forward-chaining)))) + +(defn os-info-fix (x) + (if (keywordp x) x :linux)) + +(defconst *x86isa-state* + `( + ;; -------------------------------------------------- + (:doc "These fields are an artifact of our x86 ISA model.
+ +Visible portion: + @({ 16-bit selector INDEX(13)::TI(1)::RPL(2) })
+Hidden/Cache portion (see Intel manual, Mar'17, Vol. 3A, Figure 3-7): + @({ 16-bit Attributes + 32-bit Limit + 64-bit Base Address})
+See Intel manual, Jan'19, Volume 1, Section 3.4.2 and Intel + manual, Jan'19, Volume 3, Sections 3.4.2 and 3.4.3 for + details.
Visible portion: + @({ 16-bit selector INDEX(13)::TI(1)::RPL(2) })
+Hidden/Cache portion: + @({ 16-bit Attributes + 32-bit Limit + 64-bit Base Address })
+See Intel manual, Jan'19, Volume 3, Figure 2-6 for details.
The definition of the state uses nested and abstract stobjs by way of + community books @(tsee rstobj2::defrstobj), @(tsee bigmem::bigmem), or @(tsee + bigmem-asymmetric::bigmem-asymmetric). It may be interesting to read about + the old definition to see how the current definition supports all of its + functionality but in a more maintainable way; see @(see + x86isa-state-history).
+ +The @('bigmem') and @('bigmem-asymmetric') books define a memory model + similar to the old @('x86isa') memory model in that it provides a record + representation for reasoning and an allocate-on-demand, array-like performance + for execution. The x86 concrete stobj has the @('bigmem') stobj for its memory + field; @('defrstobj') exports the @('bigmem') memory accessor and updater + functions alongside those of other x86 fields and gives us a state definition + that's logically a multi-typed record. @('defrstobj') also allows the + definition of a universal accessor and updater, so we still retain that + feature in the @('x86isa') books.
+ +Note that the @('bigmem') books define a 64-bit address space, + though the @('x86isa') state restricts that to a 52-bit address space + because the max. physical address allowed by the x86 architecture is + 52 (as of this writing in mid-2021). If the max. allowed physical + address is increased anywhere up to 64 bits, then we can simply + change the size of the @('mem') field in the @('x86isa') stobj.
+ +Before @(tsee bigmems::bigmems) and @(tsee rstobj2::defrstobj) were used to + define the x86 state, the x86 state's definition was rather tedious. (For + future reference, the following git revision has that old definition: + @('dea40263247bd930077205526934bc596686bfb0')).
+ +This current file @('state.lisp') replaces the following old + files, all of which are deleted now.
+The old x86isa memory model was based on the FMCAD'2012 paper \"A + Formal Model of a Large Memory that Supports Efficient Execution\". + The following comment from the old file @('concrete-state.lisp') + describes it briefly:
+ +
+ ....
+ ;; For our ACL2 model, we define a paging-like mechanism to
+ ;; model the complete x86 52-bit address space. The memory is
+ ;; laid out in a flat array, to be viewed as back-to-back
+ ;; \"pseudo-pages\" each of size 2^27 bytes (128MB). The address
+ ;; of a byte is split into two pieces: a 25-bit pseudo-page
+ ;; address and a 27-bit offset within a page. The mem-table
+ ;; data structure is of size *mem-table-size* = 2^25 -- thus,
+ ;; indices are 25 bits -- and it maps these indices to 25-bit
+ ;; pseudo-page addresses. However, the mem-table values are
+ ;; actually 26-bit values: the least significant bit is
+ ;; initially 1, but is 0 when the entry is valid, in which case
+ ;; the most significant 25 bits represent a pseudo-page address.
+ ;; The offset of a byte address is a 27-bit wide address, which
+ ;; when added to (ash pseudo-page-address 27), gives the \"real\"
+ ;; address of a byte stored in mem-array. Mem-array-next-addr
+ ;; keeps track of the 25-bit index of the pseudo-page to be
+ ;; allocated next.
+
+ ;; Here is an example of how this works. Suppose we have the
+ ;; following, where again, the least significant bit of 0
+ ;; indicates a valid entry, and where {i, 1'bx} denotes
+ ;; concatenation of the bit-vector i with the single bit x.
+
+ ;; mem-table[#x0654321] = {0, 1'b0}
+ ;; mem-table[#x16789ab] = {1, 1'b0}
+ ;; mem-table[#x0111111] = {2, 1'b0}
+
+ ;; All additional values in mem-table are the initial value of
+ ;; 1, which means \"page is not present\".
+
+ ;; Then mem-array starts as follows.
+
+ ;; 2^27 bytes corresponding to byte addresses with top 25 bits = #x0654321
+ ;; 2^27 bytes corresponding to byte addresses with top 25 bits = #x16789ab
+ ;; 2^27 bytes corresponding to byte addresses with top 25 bits = #x0111111
+
+ (mem-table :type (array (unsigned-byte #.*mem-table-size-bits+1*)
+ ;; *mem-table-size-bits* of pseudo-page
+ ;; address, followed by 1 extra bit
+ ;; (LSB) to indicate the
+ ;; absence/presence of pseudo-pages
+ (#.*mem-table-size*))
+ :initially 1
+ :resizable nil)
+
+ (mem-array :type (array (unsigned-byte 8)
+ (#.*initial-mem-array-length*))
+ :initially 0
+ :resizable t)
+
+ (mem-array-next-addr :type (integer 0 #.*mem-table-size*)
+ :initially 0)
+
+ ...
+
+
+ The proof of correctness of this memory model was pretty + involved (see the old file @('concrete-memory.lisp')). Then there + was the overhead of introducing an abstract stobj to logically view + this memory as a record (see old file @('abstract-stobj.lisp')).
+ +All of this was finished before nested stobjs were supported in + ACL2, so there was not much incentive to simplify the memory model or + the state definition.
+ +We used a neat trick for all the accessors and updaters of + the @('x86isa') state's field that is worth mentioning here. Say + @('RGFI*') is the name of the corresponding exported accessor from + defabsstobj; during execution, @('RGFI*') calls are really + @('RGF$CI') calls, the concrete accessor from the corresponding + concrete stobj.
+ + Signature: (RGFI* index x86)
+
+ We also defined a universal accessor function @('XR'), meant to be + used only for reasoning:
+ + Signature: (XR fld-name index x86)
+
+ @('XR') case-split on @('fld-name') and called the appropriate + accessor function (e.g., if fld-name was @(':RGF'), @('XR') called + @('RGFI*'), and so on). We defined a similar universal updater + function @('XW'). We then proved read-over-write/write-over-write, + etc. lemmas about just @('XR') and @('XW'), thereby getting these + properties for all fields of the x86 state. This way, we avoid a + blow-up in the number of such lemmas as the number of fields in the + state increase.
+ +We then defined another function, @('RGFI') (inlined), with the + same signature as @('RGFI*'), whose body was an @('MBE') like + so:
+ +
+ (RGFI index x86) :=
+ (mbe :logic (XR :RGF index x86)
+ :exec (RGFI* index x86))
+
+
+ We then used @('RGFI') as the canonical accessor function for the + @('RGF') field --- we never used @('RGFI*') or @('XR') in our + definitions from this point on, though @('XR') was often used in + definitions where we didn't care about the execution speed (e.g., + non-executable functions used only for reasoning). We kept @('RGFI') + enabled and @('XR') disabled.
+ +The consequence of this was that whenever an @('RGFI') call was + encountered during reasoning, it quickly opened up to @('XR') (about + which we have all those nice theorems). During execution, @('RGFI') + was simply the efficient concrete accessor @('RGF$CI').
") + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/three-byte-opcodes-dispatch.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/three-byte-opcodes-dispatch.lisp new file mode 100644 index 000000000..2b4d1db21 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/three-byte-opcodes-dispatch.lisp @@ -0,0 +1,276 @@ +; 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@('first-three-byte-opcode-execute') is the doorway to the + first three-byte opcode map, i.e., to all three-byte opcodes whose first + two opcode bytes are @('0F 38').
" + :guard-hints (("Goal" + :do-not '(preprocess) + :in-theory + (e/d + () + (unsigned-byte-p signed-byte-p)))) + :guard (rip-guard-okp proc-mode temp-rip) + (case opcode ,@dispatch) + + /// + + (defthm x86p-first-three-byte-opcode-execute + (implies (x86p x86) + (x86p + (first-three-byte-opcode-execute + proc-mode start-rip temp-rip prefixes + mandatory-prefix rex-byte opcode modr/m sib x86))))))) + +(make-event + (b* ((dispatch + (create-dispatch-for-opcodes + #ux0F_3A_00 256 :0F-3A-three-byte (w state)))) + + `(define second-three-byte-opcode-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*)) + (prefixes :type (unsigned-byte #.*prefixes-width*)) + (mandatory-prefix :type (unsigned-byte 8)) + (rex-byte :type (unsigned-byte 8)) + (opcode :type (unsigned-byte 8)) + (modr/m :type (unsigned-byte 8)) + (sib :type (unsigned-byte 8)) + x86) + + ;; #x0F #x3A are the first two opcode bytes. + + :parents (x86-decoder) + ;; The following arg will avoid binding __function__ to + ;; second-three-byte-opcode-execute. The automatic __function__ binding that + ;; comes with define causes stack overflows during the guard proof of this + ;; function. + :no-function t + :ignore-ok t + :short "Second three-byte opcode dispatch function." + :long "@('second-three-byte-opcode-execute') is the doorway to the second + three-byte opcode map, i.e., to all three-byte opcodes whose second two + opcode bytes are @('0F 3A').
" + :guard (rip-guard-okp proc-mode temp-rip) + :guard-hints (("Goal" + :do-not '(preprocess) + :in-theory (e/d () (unsigned-byte-p signed-byte-p)))) + + (case opcode ,@dispatch) + + /// + + (defthm x86p-second-three-byte-opcode-execute + (implies (x86p x86) + (x86p (second-three-byte-opcode-execute + proc-mode start-rip temp-rip prefixes + mandatory-prefix rex-byte opcode modr/m sib x86))))))) + + +(local (defthm unsigned-byte-p-when-lte-loghead + (implies (and (natp x) + (natp n) + (<= x (loghead n y))) + (unsigned-byte-p n x)) + :hints(("Goal" :in-theory (enable unsigned-byte-p) + :use ((:instance acl2::unsigned-byte-p-loghead + (size n) (size1 n) (i y))))))) + + +(define three-byte-opcode-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*)) + (prefixes :type (unsigned-byte #.*prefixes-width*)) + (rex-byte :type (unsigned-byte 8)) + (second-escape-byte :type (unsigned-byte 8)) + x86) + + :ignore-ok t + :guard (or (equal second-escape-byte #x38) + (equal second-escape-byte #x3A)) + :guard-hints (("Goal" :do-not '(preprocess) + :in-theory (e/d* + (add-to-*ip add-to-*ip-is-i48p-rewrite-rule) + (unsigned-byte-p + (:type-prescription bitops::logand-natp-type-2) + (:type-prescription bitops::ash-natp-type) + acl2::loghead-identity + not + tau-system + (tau-system))))) + :parents (x86-decoder) + :short "Decoder and dispatch function for three-byte opcodes, where @('0x0F + 0x38') are the first two opcode bytes" + :long "Source: Intel Manual, Volume 2, Appendix A-2
" + + (b* ((ctx 'three-byte-opcode-decode-and-execute) + + ((mv flg0 (the (unsigned-byte 8) opcode) x86) + (rme08 proc-mode temp-rip #.*cs* :x x86)) + ((when flg0) + (!!ms-fresh :opcode-byte-access-error flg0)) + + ;; Possible values of opcode: all values denote opcodes of the first or + ;; second three-byte map, depending on the value of second-escape-byte. + ;; The function first-three-byte-opcode-execute or + ;; second-three-byte-opcode-execute case-splits on this opcode byte and + ;; calls the appropriate instruction semantic function. + + ((mv flg temp-rip) (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) (!!ms-fresh :increment-error flg)) + + ;; ((mv msg (the (unsigned-byte 8) mandatory-prefix)) + ;; (compute-mandatory-prefix-for-three-byte-opcode + ;; proc-mode second-escape-byte opcode prefixes)) + ;; ((when msg) + ;; (x86-illegal-instruction msg start-rip temp-rip x86)) + + ((the (unsigned-byte 8) mandatory-prefix) + (compute-mandatory-prefix-for-three-byte-opcode + proc-mode second-escape-byte opcode prefixes)) + (modr/m? + (three-byte-opcode-ModR/M-p + proc-mode mandatory-prefix second-escape-byte opcode)) + ((mv flg1 (the (unsigned-byte 8) modr/m) x86) + (if modr/m? + (rme08 proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg1) (!!ms-fresh :modr/m-byte-read-error flg1)) + + ((mv flg temp-rip) (if modr/m? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg)) + + (sib? (and modr/m? + (b* ((p4? (eql #.*addr-size-override* + (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 flg2 (the (unsigned-byte 8) sib) x86) + (if sib? + (rme08 proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg2) + (!!ms-fresh :sib-byte-read-error flg2)) + + ((mv flg temp-rip) (if sib? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg))) + + (case second-escape-byte + (#x38 + (first-three-byte-opcode-execute + proc-mode start-rip temp-rip prefixes mandatory-prefix + rex-byte opcode modr/m sib x86)) + (#x3A + (second-three-byte-opcode-execute + proc-mode start-rip temp-rip prefixes mandatory-prefix + rex-byte opcode modr/m sib x86)) + (otherwise + ;; Unreachable. + (!!ms-fresh :illegal-value-of-second-escape-byte second-escape-byte)))) + + /// + + (defrule x86p-three-byte-opcode-decode-and-execute + (implies (x86p x86) + (x86p (three-byte-opcode-decode-and-execute + proc-mode start-rip temp-rip prefixes rex-byte escape-byte x86))) + :enable add-to-*ip-is-i48p-rewrite-rule + :disable signed-byte-p)) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/top-level-memory.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/top-level-memory.lisp new file mode 100644 index 000000000..a90d33b69 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/top-level-memory.lisp @@ -0,0 +1,1982 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2024, Kestrel Technology, LLC +; 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): +; Alessandro Coglio (www.alessandrocoglio.info) +; Contributing Author(s): +; Dmitry Nadezhin +; Shilpi Goel + +(in-package "X86ISA") + +;; ---------------------------------------------------------------------- + +(include-book "segmentation") + +(local (include-book "centaur/bitops/ihs-extensions" :dir :system)) + +;; ---------------------------------------------------------------------- + +(defsection top-level-memory + :parents (machine) + :short "Top-level Memory Accessor and Updater Functions" + ) + +(local (xdoc::set-default-parents top-level-memory)) + +;; ---------------------------------------------------------------------- + +(define address-aligned-p + ((addr :type (signed-byte #.*max-linear-address-size*)) + (operand-size :type (member 1 2 4 6 8 10 16 32 64)) + (memory-ptr? booleanp)) + :inline t + :no-function t + :returns (yes/no booleanp :rule-classes :type-prescription) + :short "Check the alignment of a linear address." + :long + "+ Besides the address to check for alignment, + this function takes as argument the operand size + (from which the alignment to check is determined) + and a flag indicating whether the address to check for alignment + contains a memory operand of the form m16:16, m16:32, or m16:64 + (see Intel manual, Mar'17, Volume 2, Section 3.1.1.3). +
++ Words, doublewords, quadwords, and double quadwords + must be aligned at boundaries of 2, 4, 8, or 16 bytes. + Memory pointers of the form m16:xx must be aligned so that + their xx portion is aligned as a word, doubleword, or quadword; + this automatically guarantees that their m16 portion is aligned as a word. + See Intel manual, Mar'17, Volume 1, Section 4.1.1. + See AMD manual, Dec'17, Volume 2, Table 8-7 + (note that the table does not mention explicitly + memory pointers of the form m16:64). +
++ If the operand size is 6, the operand must be an m16:32 pointer. + If the operand size is 10, the operand must an m16:64 pointer. + If the operand size is 4, it may be either an m16:16 pointer or not; + in this case, the @('memory-ptr?') argument is used to + determine whether the address should be aligned + at a word or doubleword boundary. + If the operand size is 1, 2, 8, or 16, + it cannot be a memory pointer of the form m16:xx. +
" + (b* ((addr (the (signed-byte #.*max-linear-address-size*) addr)) + (operand-size (the (integer 0 65) operand-size))) + (case operand-size + ;; Every byte access is always aligned. + (1 t) + (6 (equal (logand addr #b11) 0)) ; m16:32 + (10 (equal (logand addr #b111) 0)) ; m16:64 + (otherwise + (if (and memory-ptr? + (eql operand-size 4)) + (equal (logand addr #b1) 0) ; m16:16 + (equal (logand addr (the (integer 0 65) (- operand-size 1))) + 0))))) + + /// + + (defthm memory-byte-accesses-are-always-aligned + (equal (address-aligned-p addr 1 mem-ptr?) t)) + + (defthm address-aligned-p-mem-ptr-input-irrelevant-for-all-but-bytes=4 + (implies (and (syntaxp (not (equal mem-ptr? ''nil))) + (not (equal nbytes 4))) + (equal (address-aligned-p addr nbytes mem-ptr?) + (address-aligned-p addr nbytes nil))))) + +;; ---------------------------------------------------------------------- + +;; Memory Read Functions: + +(define gen-read-function (&key + (size natp) + (signed? booleanp) + (check-alignment?-var booleanp) + (mem-ptr?-var booleanp)) + :mode :program + + (b* ((size-str (symbol-name (if (< size 10) + (acl2::packn (list 0 size)) + (acl2::packn (list size))))) + (fn (str::cat (if signed? "RIME" "RME") size-str)) + (fn-name (intern$ fn "X86ISA")) + (fn-call `(,fn-name proc-mode eff-addr seg-reg r-x + ,@(and check-alignment?-var + '(check-alignment?)) + x86 + ,@(and mem-ptr?-var + `(:mem-ptr? mem-ptr?)))) + (lin-mem-fn (str::cat (if signed? "RIML" "RML") size-str)) + (lin-mem-fn-name (intern$ lin-mem-fn "X86ISA")) + (short-doc-string + (str::cat + "Read " + (if signed? "a signed " "an unsigned ") + (str::nat-to-dec-string size) + "-bit value from memory via an effective address.")) + (long-doc-string + (str::cat + "The effective address @('eff-addr') is translated to a canonical + linear address using @(see ea-to-la). If this translation is + successful and no other errors (like alignment errors) occur, then + @(see " lin-mem-fn ") is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored (see Atmel manual, Dec'17, Volume 2, Section 4.8.1).
"))) + + `(define ,fn-name ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (r-x :type (member :r :x)) + ,@(and check-alignment?-var `((check-alignment? booleanp))) + x86 + ,@(and mem-ptr?-var `(&key ((mem-ptr? booleanp) 'nil)))) + :inline t + :no-function t + :returns (mv flg + (value + (,(if signed? 'signed-byte-p 'unsigned-byte-p) ,size value) + :hyp (x86p x86)) + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + + :short ,short-doc-string + :long ,long-doc-string + + (b* (((when (and + (/= proc-mode #.*64-bit-mode*) + (= seg-reg #.*cs*) + (eq r-x :r) + (b* ((attr (loghead 16 (seg-hidden-attri seg-reg x86))) + (r (code-segment-descriptor-attributesBits->r attr))) + (= r 0)))) + (mv (list :execute-only-code-segment eff-addr) 0 x86)) + ((mv flg lin-addr) + (ea-to-la proc-mode eff-addr seg-reg ,(/ size 8) x86)) + ((when flg) (mv flg 0 x86)) + ,@(and + check-alignment?-var + `(((unless (or (not check-alignment?) + (address-aligned-p + lin-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))))) + (,lin-mem-fn-name lin-addr r-x x86)) + /// + + (,(if signed? 'defthm-signed-byte-p 'defthm-unsigned-byte-p) + ,(mk-name (if signed? "I" "N") size-str "P-OF-MV-NTH-1-" fn) + :hyp t + :bound ,size + :concl (mv-nth 1 ,fn-call) + :gen-linear t + :gen-type t) + + (defrule ,(mk-name fn "-VALUE-WHEN-ERROR") + (implies (mv-nth 0 ,fn-call) + (equal (mv-nth 1 ,fn-call) 0)) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + )) + )) + + (defrule ,(mk-name fn "-DOES-NOT-AFFECT-STATE-IN-APP-VIEW") + (implies (app-view x86) + (equal (mv-nth 2 ,fn-call) x86)) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + )) + )) + + (defrule ,(mk-name "XR-" fn "-STATE-APP-VIEW") + (implies (app-view x86) + (equal (xr fld index (mv-nth 2 ,fn-call)) + (xr fld index x86))) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + )))) + + (defrule ,(mk-name "XR-" fn "-STATE-SYS-VIEW") + (implies (and ;; (not (app-view x86)) + (not (equal fld :mem)) + (not (equal fld :fault)) + (not (equal fld :tlb)) + ;; ,@(if (< size 10) + ;; nil + ;; `((member-equal fld *x86-field-names-as-keywords*))) + ) + (equal (xr fld index (mv-nth 2 ,fn-call)) + (xr fld index x86))) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + ))) + ,@(if (< size 10) + nil + `(:disable (rb unsigned-byte-p signed-byte-p member-equal)))) + + (defrule ,(mk-name fn "-XW-APP-VIEW") + (implies + (and (app-view x86) + (not (equal fld :mem)) + (not (equal fld :app-view)) + (not (equal fld :seg-hidden-base)) + (not (equal fld :seg-hidden-limit)) + (not (equal fld :seg-hidden-attr)) + (not (equal fld :seg-visible)) + (not (equal fld :msr))) + (and + (equal (mv-nth 0 + ,(search-and-replace-once + 'x86 + '(xw fld index value x86) + fn-call)) + (mv-nth 0 ,fn-call)) + (equal (mv-nth 1 ,(search-and-replace-once + 'x86 + '(xw fld index value x86) + fn-call)) + (mv-nth 1 ,fn-call)) + ;; No need for the conclusion about the state; see + ;; ,(mk-name fn "-DOES-NOT-AFFECT-STATE-IN-APP-VIEW") + )) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + ))) + :disable (rb)) + + (defrule ,(mk-name fn "-XW-SYS-VIEW") + (implies + (and (not (app-view x86)) + (not (equal fld :fault)) + (not (equal fld :seg-visible)) + (not (equal fld :seg-hidden-base)) + (not (equal fld :seg-hidden-limit)) + (not (equal fld :seg-hidden-attr)) + (not (equal fld :mem)) + (not (equal fld :ctr)) + (not (equal fld :msr)) + (not (equal fld :rflags)) + (not (equal fld :app-view)) + (not (equal fld :tlb)) + (not (equal fld :marking-view)) + (not (equal fld :implicit-supervisor-access)) + ,@(if (< size 10) + nil + `((member-equal fld *x86-field-names-as-keywords*)))) + (and + (equal + (mv-nth 0 ,(search-and-replace-once + 'x86 + '(xw fld index value x86) + fn-call)) + (mv-nth 0 ,fn-call)) + (equal + (mv-nth 1 ,(search-and-replace-once + 'x86 + '(xw fld index value x86) + fn-call)) + (mv-nth 1 ,fn-call)) + (equal + (mv-nth 2 ,(search-and-replace-once + 'x86 + '(xw fld index value x86) + fn-call)) + (xw fld index value (mv-nth 2 ,fn-call))))) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + ))) + ,@(if (< size 10) + nil + `(:disable (rb unsigned-byte-p signed-byte-p member-equal)))) + + (defrule ,(mk-name fn "-XW-SYS-VIEW-RFLAGS-NOT-AC") + (implies + (and (not (app-view x86)) + (equal (rflagsBits->ac value) + (rflagsBits->ac (rflags x86)))) + (and + (equal + (mv-nth 0 ,(search-and-replace-once + 'x86 + '(xw :rflags nil value x86) + fn-call)) + (mv-nth 0 ,fn-call)) + (equal + (mv-nth 1 ,(search-and-replace-once + 'x86 + '(xw :rflags nil value x86) + fn-call)) + (mv-nth 1 ,fn-call)) + (equal + (mv-nth 2 ,(search-and-replace-once + 'x86 + '(xw :rflags nil value x86) + fn-call)) + (xw :rflags nil value (mv-nth 2 ,fn-call))))) + :enable (,@(and signed? + `(,lin-mem-fn-name + ;; ,(mk-name "RML" size-str) + ))) + :disable (rb unsigned-byte-p signed-byte-p)) + + (defrule ,(mk-name fn "-WHEN-64-BIT-MODEP-AND-NOT-FS/GS") + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + ,@(and check-alignment?-var + `((or (not check-alignment?) + (address-aligned-p + eff-addr ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))))) + (equal ,(search-and-replace-once 'proc-mode + '#.*64-bit-mode* + fn-call) + (,lin-mem-fn-name eff-addr r-x x86)))) + + ,@(and + check-alignment?-var + `((defrule ,(mk-name fn "-UNALIGNED-WHEN-64-BIT-MODEP-AND-NOT-FS/GS") + (implies + (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p + eff-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (canonical-address-p eff-addr)) + (equal ,(search-and-replace-once 'proc-mode + '#.*64-bit-mode* + fn-call) + (list (list :unaligned-linear-address eff-addr) + 0 + x86)))))) + + (defrule ,(mk-name fn "-WHEN-64-BIT-MODEP-AND-FS/GS") + (implies + (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal + ,(search-and-replace-once 'proc-mode '#.*64-bit-mode* fn-call) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg 0 x86)) + ,@(and + check-alignment?-var + `(((unless (or (not check-alignment?) + (address-aligned-p + lin-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))))) + (,lin-mem-fn-name lin-addr r-x x86)))) + :hints (("Goal" :in-theory (e/d (ea-to-la) ()))))))) + +(make-event + `(progn + (local (in-theory (e/d () + (unsigned-byte-p + signed-byte-p + force (force))))) + ;; rme08 + ,(gen-read-function :size 8 + :signed? nil + :check-alignment?-var nil + :mem-ptr?-var nil) + ;; rime08 + ,(gen-read-function :size 8 + :signed? t + :check-alignment?-var nil + :mem-ptr?-var nil) + ;; rme16 + ,(gen-read-function :size 16 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rime16 + ,(gen-read-function :size 16 + :signed? t + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rme32 + ,(gen-read-function :size 32 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var t) + ;; rime32 + ,(gen-read-function :size 32 + :signed? t + :check-alignment?-var t + :mem-ptr?-var t) + ;; rme48 + ,(gen-read-function :size 48 ;; typically a m16:32 pointer + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rme64 + ,(gen-read-function :size 64 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rime64 + ,(gen-read-function :size 64 + :signed? t + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rme80 + ,(gen-read-function :size 80 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rme128 + ;; Note: A #GP exception should be thrown here instead of an #AC fault when + ;; the address is not aligned. See Intel Manuals, Volume 3, Section 6.15, + ;; Exception and Interrupt Reference, Interrupt 17 Alignment Check + ;; Exception (#AC). + ,(gen-read-function :size 128 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; rme256 + ;; Note: A #GP exception should be thrown here instead of an #AC fault when + ;; the address is not aligned. See Intel Manuals, Volume 2, Tables 2-18 and + ;; 2-44. + ,(gen-read-function :size 256 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil))) + +(define rme-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (nbytes :type (member 1 2 4 6 8 10 16 32)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (r-x :type (member :r :x)) + (check-alignment? booleanp) + x86 + &key + ;; Default value for mem-ptr? is nil --- note that this input is + ;; relevant only for nbytes = 4. + ((mem-ptr? booleanp) 'nil)) + :returns (mv flg + (value natp :rule-classes (:rewrite :type-prescription)) + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + :short "Read an unsigned value with the specified number of bytes + from memory via an effective address." + :long + "The effective address is translated to a canonical linear address using + @(see ea-to-la). If this translation is successful and no other errors (like + alignment errors) occur, then @(see rml-size) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored; see Intel manual, Dec'17, Volume 2, Section 4.8.1
" + (b* (((when (and (/= proc-mode #.*64-bit-mode*) + (= seg-reg #.*cs*) + (eq r-x :r) + (b* ((attr (loghead 16 (seg-hidden-attri seg-reg x86))) + (r (code-segment-descriptor-attributesBits->r attr))) + (= r 0)))) + (mv (list :execute-only-code-segment eff-addr) 0 x86)) + ((mv flg lin-addr) (ea-to-la proc-mode eff-addr seg-reg nbytes x86)) + ((when flg) (mv flg 0 x86)) + ((unless (or (not check-alignment?) + (address-aligned-p lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))) + (rml-size nbytes lin-addr r-x x86)) + :inline t + :no-function t + + /// + + (defrule rme-size-when-64-bit-modep-and-not-fs/gs + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (equal (rme-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rml-size nbytes eff-addr r-x x86)))) + + (defrule rme-size-when-64-bit-modep-fs/gs + (implies (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal (rme-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg 0 x86)) + ((unless (or (not check-alignment?) + (address-aligned-p + lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))) + (rml-size nbytes lin-addr r-x x86)))) + :in-theory (e/d (ea-to-la) ())) + + (defthm rme-size-unaligned-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rme-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (canonical-address-p eff-addr)) + (equal (rme-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :unaligned-linear-address eff-addr) + 0 x86))) + :hints (("Goal" :in-theory (e/d (rme-size) ())))) + + (defthm rme-size-non-canonical-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rme-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not (canonical-address-p eff-addr))) + (equal (rme-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :non-canonical-address eff-addr) 0 x86))) + :hints (("Goal" :in-theory (e/d (rme-size) ())))) + + (defruled rme-size-of-1-to-rme08 + (equal + (rme-size proc-mode 1 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme08 proc-mode eff-addr seg-reg r-x x86)) + :enable (rme-size rme08)) + + (defruled rme-size-of-2-to-rme16 + (equal + (rme-size proc-mode 2 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme16 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme16)) + + (defruled rme-size-of-4-to-rme32 + (equal + (rme-size proc-mode 4 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme32 proc-mode eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?)) + :enable (rme-size rme32)) + + (defruled rme-size-of-6-to-rme48 + (equal + (rme-size proc-mode 6 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme48 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme48)) + + (defruled rme-size-of-8-to-rme64 + (equal + (rme-size proc-mode 8 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme64 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme64)) + + (defruled rme-size-of-10-to-rme64 + (equal + (rme-size proc-mode 10 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme80 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme80)) + + (defruled rme-size-of-16-to-rme128 + (equal + (rme-size proc-mode 16 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme128 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme128)) + + (defruled rme-size-of-32-to-rme256 + (equal + (rme-size proc-mode 32 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rme256 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rme-size rme256)) + + (defthm unsigned-byte-p-of-mv-nth-1-of-rme-size + (implies (and (<= (* 8 nbytes) size) + (natp size)) + (unsigned-byte-p size (mv-nth 1 (rme-size proc-mode nbytes eff-addr seg-reg r-x check-alignment? x86)))) + :hints (("Goal" :in-theory (disable rml-size))))) + +(define rime-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (nbytes :type (member 1 2 4 8)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (r-x :type (member :r :x)) + (check-alignment? booleanp) + x86 + &key + ;; Default value for mem-ptr? is nil --- note that this input is + ;; relevant only for nbytes = 4. + ((mem-ptr? booleanp) 'nil)) + :returns (mv flg + (value integerp :hyp (integerp nbytes)) + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + :short "Read a signed value with the specified number of bytes + from memory via an effective address." + :long + "The effective address is translated to a canonical linear address using + @(see ea-to-la). If this translation is successful and no other errors (like + alignment errors) occur, then @(see riml-size) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored; see Atmel manual, Dec'17, Volume 2, Section 4.8.1
" + (b* (((when (and (/= proc-mode #.*64-bit-mode*) + (= seg-reg #.*cs*) + (eq r-x :r) + (b* ((attr (loghead 16 (seg-hidden-attri seg-reg x86))) + (r (code-segment-descriptor-attributesBits->r attr))) + (= r 0)))) + (mv (list :execute-only-code-segment eff-addr) 0 x86)) + ((mv flg lin-addr) (ea-to-la proc-mode eff-addr seg-reg nbytes x86)) + ((when flg) (mv flg 0 x86)) + ((unless (or (not check-alignment?) + (address-aligned-p lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))) + (riml-size nbytes lin-addr r-x x86)) + :inline t + :no-function t + /// + + (defrule rime-size-when-64-bit-modep-and-not-fs/gs + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (equal (rime-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (riml-size nbytes eff-addr r-x x86)))) + + (defrule rime-size-when-64-bit-modep-fs/gs + (implies (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal (rime-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg 0 x86)) + ((unless (or (not check-alignment?) + (address-aligned-p + lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 x86))) + (riml-size nbytes lin-addr r-x x86)))) + :in-theory (e/d (ea-to-la) ())) + + (defthm rime-size-unaligned-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rime-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (canonical-address-p eff-addr)) + (equal (rime-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :unaligned-linear-address eff-addr) + 0 x86))) + :hints (("Goal" :in-theory (e/d (rime-size) ())))) + + (defthm rime-size-non-canonical-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rime-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not (canonical-address-p eff-addr))) + (equal (rime-size #.*64-bit-mode* + nbytes eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :non-canonical-address eff-addr) 0 x86))) + :hints (("Goal" :in-theory (e/d (rime-size) ())))) + + (defruled rime-size-of-1-to-rime08 + (equal (rime-size proc-mode 1 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rime08 proc-mode eff-addr seg-reg r-x x86)) + :enable (rime-size rime08)) + + (defruled rime-size-of-2-to-rime16 + (equal (rime-size proc-mode 2 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rime16 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rime-size rime16)) + + (defruled rime-size-of-4-to-rime32 + (equal + (rime-size proc-mode 4 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rime32 proc-mode eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?)) + :enable (rime-size rime32)) + + (defruled rime-size-of-8-to-rime64 + (equal + (rime-size proc-mode 8 eff-addr seg-reg r-x check-alignment? x86 + :mem-ptr? mem-ptr?) + (rime64 proc-mode eff-addr seg-reg r-x check-alignment? x86)) + :enable (rime-size rime64)) + + (defthm signed-byte-p-of-mv-nth-1-of-rime-size + (implies (and (<= (* 8 nbytes) size) + (member-equal nbytes '(1 2 4 8)) + (integerp size)) + (signed-byte-p size (mv-nth 1 (rime-size proc-mode nbytes eff-addr seg-reg r-x check-alignment? x86)))))) + +;; ---------------------------------------------------------------------- + +;; Memory Write Functions: + +(define gen-write-function (&key + (size natp) + (signed? booleanp) + (check-alignment?-var booleanp) + (mem-ptr?-var booleanp)) + :mode :program + + (b* ((size-str (symbol-name (if (< size 10) + (acl2::packn (list 0 size)) + (acl2::packn (list size))))) + (fn (str::cat (if signed? "WIME" "WME") size-str)) + (fn-name (intern$ fn "X86ISA")) + (fn-call `(,fn-name proc-mode eff-addr seg-reg val + ,@(and check-alignment?-var + '(check-alignment?)) + x86 + ,@(and mem-ptr?-var + `(:mem-ptr? mem-ptr?)))) + (lin-mem-fn (str::cat (if signed? "WIML" "WML") size-str)) + (lin-mem-fn-name (intern$ lin-mem-fn "X86ISA")) + (short-doc-string + (str::cat + "Write " + (if signed? "a signed " "an unsigned ") + (str::nat-to-dec-string size) + "-bit value to memory via an effective address.")) + (long-doc-string + (str::cat + "The effective address @('eff-addr') is translated to a canonical + linear address. If this translation is successful and no other error + occurs (like alignment errors), then @(see " lin-mem-fn ") is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments (DS, ES, FS, GS, and SS) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments (this is + not explicitly mentioned in Intel manual, May'18, Volume 3, Section + 3.4.5.1, but it seems reasonable). In 64-bit mode, the W bit is + ignored (see Atmel manual, Dec'17, Volume 2, Section 4.8.1); by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
"))) + + `(define ,fn-name ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (val :type (,(if signed? 'signed-byte 'unsigned-byte) ,size)) + ,@(and check-alignment?-var `((check-alignment? booleanp))) + x86 + ,@(and mem-ptr?-var `(&key ((mem-ptr? booleanp) 'nil)))) + :inline t + :no-function t + :returns (mv flg + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + + :short ,short-doc-string + :long ,long-doc-string + + (b* (((when (and (/= proc-mode #.*64-bit-mode*) + (or (= seg-reg #.*cs*) + (b* ((attr (loghead + 16 (seg-hidden-attri seg-reg x86))) + (w (data-segment-descriptor-attributesBits->w + attr))) + (= w 0))))) + (mv (list :non-writable-segment eff-addr seg-reg) x86)) + ((mv flg lin-addr) + (ea-to-la proc-mode eff-addr seg-reg ,(/ size 8) x86)) + ((when flg) (mv flg x86)) + ,@(and + check-alignment?-var + `(((unless (or (not check-alignment?) + (address-aligned-p + lin-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (mv (list :unaligned-linear-address lin-addr) x86))))) + (,lin-mem-fn-name lin-addr val x86)) + /// + + (defrule ,(mk-name fn "-WHEN-64-BIT-MODEP-AND-NOT-FS/GS") + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + ,@(and check-alignment?-var + `((or (not check-alignment?) + (address-aligned-p + eff-addr ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))))) + (equal ,(search-and-replace-once 'proc-mode + '#.*64-bit-mode* + fn-call) + (,lin-mem-fn-name eff-addr val x86)))) + + ,@(and + check-alignment?-var + `((defrule ,(mk-name fn "-UNALIGNED-WHEN-64-BIT-MODEP-AND-NOT-FS/GS") + (implies + (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p + eff-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (canonical-address-p eff-addr)) + (equal ,(search-and-replace-once 'proc-mode + '#.*64-bit-mode* + fn-call) + (list (list :unaligned-linear-address eff-addr) x86)))))) + + (defrule ,(mk-name fn "-WHEN-64-BIT-MODEP-AND-FS/GS") + (implies + (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal + ,(search-and-replace-once 'proc-mode '#.*64-bit-mode* fn-call) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg x86)) + ,@(and + check-alignment?-var + `(((unless (or (not check-alignment?) + (address-aligned-p + lin-addr + ,(/ size 8) + ,(if mem-ptr?-var 'mem-ptr? 'nil)))) + (mv (list :unaligned-linear-address lin-addr) x86))))) + (,lin-mem-fn-name lin-addr val x86)))) + :hints (("Goal" :in-theory (e/d (ea-to-la) ()))))))) + +(make-event + `(progn + (local (in-theory (e/d () + (unsigned-byte-p + signed-byte-p + force (force))))) + ;; wme08 + ,(gen-write-function :size 8 + :signed? nil + :check-alignment?-var nil + :mem-ptr?-var nil) + ;; wime08 + ,(gen-write-function :size 8 + :signed? t + :check-alignment?-var nil + :mem-ptr?-var nil) + ;; wme16 + ,(gen-write-function :size 16 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wime16 + ,(gen-write-function :size 16 + :signed? t + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wme32 + ,(gen-write-function :size 32 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var t) + ;; wime32 + ,(gen-write-function :size 32 + :signed? t + :check-alignment?-var t + :mem-ptr?-var t) + ;; wme48 + ,(gen-write-function :size 48 ;; typically a m16:32 pointer + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wme64 + ,(gen-write-function :size 64 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wime64 + ,(gen-write-function :size 64 + :signed? t + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wme80 + ,(gen-write-function :size 80 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wme128 + ;; Note: A #GP exception should be thrown here instead of an #AC fault when + ;; the address is not aligned. See Intel Manuals, Volume 3, Section 6.15, + ;; Exception and Interrupt Reference, Interrupt 17 Alignment Check + ;; Exception (#AC). + ,(gen-write-function :size 128 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil) + ;; wme256 + ;; Note: A #GP exception should be thrown here instead of an #AC fault when + ;; the address is not aligned. See Intel Manuals, Volume 2, Tables 2-18 and + ;; 2-44. + ,(gen-write-function :size 256 + :signed? nil + :check-alignment?-var t + :mem-ptr?-var nil))) + +(define wme-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (nbytes :type (member 1 2 4 6 8 10 16 32)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (val :type (integer 0 *)) + (check-alignment? booleanp) + x86 + &key + ;; Default value for mem-ptr? is nil --- note that this input is + ;; relevant only for nbytes = 4. + ((mem-ptr? booleanp) 'nil)) + :guard (case nbytes + (1 (n08p val)) + (2 (n16p val)) + (4 (n32p val)) + (6 (n48p val)) + (8 (n64p val)) + (10 (n80p val)) + (16 (n128p val)) + (32 (n256p val))) + :returns (mv flg + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + :short "Write an unsigned value with the specified number of bytes + to memory via an effective address." + :long + "The effective address is translated to a canonical linear address. If + this translation is successful and no other errors occur (like alignment + errors), then @(see wml-size) is called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments (DS, ES, FS, GS, and SS) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments (this is + not explicitly mentioned in Intel manual, May'18, Volume 3, Section + 3.4.5.1, but it seems reasonable. In 64-bit mode, the W bit is + ignored (see Atmel manual, Dec'17, Volume 2, Section 4.8.1); by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
" + (b* (((when (and (/= proc-mode #.*64-bit-mode*) + (or (= seg-reg #.*cs*) + (b* ((attr (loghead + 16 (seg-hidden-attri seg-reg x86))) + (w (data-segment-descriptor-attributesBits->w + attr))) + (= w 0))))) + (mv (list :non-writable-segment eff-addr seg-reg) x86)) + ((mv flg lin-addr) (ea-to-la proc-mode eff-addr seg-reg nbytes x86)) + ((when flg) (mv flg x86)) + ((unless (or (not check-alignment?) + (address-aligned-p lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wml-size nbytes lin-addr val x86)) + :inline t + :no-function t + /// + + (defrule wme-size-when-64-bit-modep-and-not-fs/gs + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (equal + (wme-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (wml-size nbytes eff-addr val x86)))) + + (defrule wme-size-when-64-bit-modep-fs/gs + (implies (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal (wme-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg x86)) + ((unless (or (not check-alignment?) + (address-aligned-p + lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wml-size nbytes lin-addr val x86)))) + :in-theory (e/d (ea-to-la) ())) + + (defthm wme-size-64-bit-unaligned-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of wme-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (canonical-address-p eff-addr)) + (equal (wme-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :unaligned-linear-address eff-addr) x86))) + :hints (("Goal" :in-theory (e/d (wme-size) ())))) + + (defthm wme-size-non-canonical-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rme-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not (canonical-address-p eff-addr))) + (equal (wme-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :non-canonical-address eff-addr) x86))) + :hints (("Goal" :in-theory (e/d (wme-size) ()))))) + +(define wime-size + ((proc-mode :type (integer 0 #.*num-proc-modes-1*)) + (nbytes :type (member 1 2 4 8)) + (eff-addr :type (signed-byte 64)) + (seg-reg :type (integer 0 #.*segment-register-names-len-1*)) + (val :type integer) + (check-alignment? booleanp) + x86 + &key + ;; Default value for mem-ptr? is nil --- note that this input is + ;; relevant only for nbytes = 4. + ((mem-ptr? booleanp) 'nil)) + :inline t + :no-function t + :guard (case nbytes + (1 (i08p val)) + (2 (i16p val)) + (4 (i32p val)) + (8 (i64p val))) + :returns (mv flg + (x86-new x86p :hyp (x86p x86))) + :parents (top-level-memory) + :short "Write a signed value with the specified number of bytes + to memory via an effective address." + :long + "The effective address is translated to a canonical linear address. If + this translation is successful and no other errors occur (like alignment + errors), then @(see wiml-size) is called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments (DS, ES, FS, GS, and SS) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments (this is + not explicitly mentioned in Intel manual, May'18, Volume 3, Section + 3.4.5.1, but it seems reasonable. In 64-bit mode, the W bit is + ignored (see Atmel manual, Dec'17, Volume 2, Section 4.8.1); by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
" + (b* (((when (and (/= proc-mode #.*64-bit-mode*) + (or (= seg-reg #.*cs*) + (b* ((attr (loghead + 16 (seg-hidden-attri seg-reg x86))) + (w (data-segment-descriptor-attributesBits->w + attr))) + (= w 0))))) + (mv (list :non-writable-segment eff-addr seg-reg) x86)) + ((mv flg lin-addr) (ea-to-la proc-mode eff-addr seg-reg nbytes x86)) + ((when flg) (mv flg x86)) + ((unless (or (not check-alignment?) + (address-aligned-p lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wiml-size nbytes lin-addr val x86)) + + /// + + (defrule wime-size-when-64-bit-modep-and-not-fs/gs + (implies + (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (canonical-address-p eff-addr) + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (equal (wime-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (wiml-size nbytes eff-addr val x86)))) + + (defrule wime-size-when-64-bit-modep-fs/gs + (implies (or (equal seg-reg #.*fs*) + (equal seg-reg #.*gs*)) + (equal (wime-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (b* (((mv flg lin-addr) + (b* (((mv base & &) + (segment-base-and-bounds 0 seg-reg x86)) + (lin-addr (i64 (+ base (n64 eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0)))) + ((when flg) + (mv flg x86)) + ((unless (or (not check-alignment?) + (address-aligned-p + lin-addr nbytes mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wiml-size nbytes lin-addr val x86)))) + :in-theory (e/d (ea-to-la) ())) + + (defthm wime-size-unaligned-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of wime-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not + (or (not check-alignment?) + (address-aligned-p eff-addr nbytes mem-ptr?))) + (canonical-address-p eff-addr)) + (equal (wime-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :unaligned-linear-address eff-addr) x86)))) + + (defthm wime-size-non-canonical-when-64-bit-modep-and-not-fs/gs + ;; [Shilpi] Added for guard proof obligations generated from the expansion + ;; of rme-size-opt. + (implies (and (not (equal seg-reg #.*fs*)) + (not (equal seg-reg #.*gs*)) + (not (canonical-address-p eff-addr))) + (equal (wime-size #.*64-bit-mode* + nbytes eff-addr seg-reg val check-alignment? x86 + :mem-ptr? mem-ptr?) + (list (list :non-canonical-address eff-addr) x86))) + :hints (("Goal" :in-theory (e/d (wime-size) ()))))) + +;; ---------------------------------------------------------------------- + +;; Macro definitions for efficient 64-bit mode execution: + +;; The idea here is that in 64-bit mode, when we already know that eff-addr is +;; a canonical address (e.g., because it is produced by a function that always +;; returns a canonical address) and if the seg-reg is *cs* (which is the common +;; case), then we needn't check for canonicity of eff-addr. + +(defmacro rme08-opt (proc-mode eff-addr seg-reg r-x x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rme08 ,proc-mode ,eff-addr ,seg-reg ,r-x ,x86) + :exec + (if (eql proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86))) + (rml08 lin-addr ,r-x ,x86)) + (rme08 ,proc-mode ,eff-addr ,seg-reg ,r-x ,x86)))) + +(defmacro rime08-opt (proc-mode eff-addr seg-reg r-x x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rime08 ,proc-mode ,eff-addr ,seg-reg ,r-x ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86))) + (riml08 lin-addr ,r-x ,x86)) + (rime08 ,proc-mode ,eff-addr ,seg-reg ,r-x ,x86)))) + +(defmacro wme08-opt (proc-mode eff-addr seg-reg val x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wme08 ,proc-mode ,eff-addr ,seg-reg ,val ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86))) + (wml08 lin-addr ,val ,x86)) + (wme08 ,proc-mode ,eff-addr ,seg-reg ,val ,x86)))) + +(defmacro wime08-opt (proc-mode eff-addr seg-reg val x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wime08 ,proc-mode ,eff-addr ,seg-reg ,val ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86))) + (wiml08 lin-addr ,val ,x86)) + (wime08 ,proc-mode ,eff-addr ,seg-reg ,val ,x86)))) + +(defmacro rme16-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rme16 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 2 nil))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (rml16 lin-addr ,r-x ,x86)) + (rme16 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86)))) + +(defmacro rime16-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rime16 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 2 nil))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (riml16 lin-addr ,r-x ,x86)) + (rime16 ,proc-mode ,eff-addr ,seg-reg ,r-x ,x86)))) + +(defmacro wme16-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wme16 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 2 nil))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wml16 lin-addr ,val ,x86)) + (wme16 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86)))) + +(defmacro wime16-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wime16 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 2 nil))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wiml16 lin-addr ,val ,x86)) + (wime16 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86)))) + +(defmacro rme32-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rme32 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 4 ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (rml32 lin-addr ,r-x ,x86)) + (rme32 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro rime32-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rime32 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 4 ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (riml32 lin-addr ,r-x ,x86)) + (rime32 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro wme32-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wme32 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 4 ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wml32 lin-addr ,val ,x86)) + (wme32 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro wime32-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wime32 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 4 nil))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wiml32 lin-addr ,val ,x86)) + (wime32 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86)))) + +(defmacro rme48-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rme48 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 6 t))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (rml48 lin-addr ,r-x ,x86)) + (rme48 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86)))) + +(defmacro wme48-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wme48 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 6 t))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wml48 lin-addr ,val ,x86)) + (wme48 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86)))) + +(defmacro rme64-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rme64 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 8 nil))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (rml64 lin-addr ,r-x ,x86)) + (rme64 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86)))) + +(defmacro rime64-opt (proc-mode eff-addr seg-reg r-x check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (rime64 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 8 nil))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (riml64 lin-addr ,r-x ,x86)) + (rime64 ,proc-mode ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86)))) + +(defmacro wme64-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wme64 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr 8 ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) x86))) + (wml64 lin-addr ,val ,x86)) + (wme64 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro wime64-opt (proc-mode eff-addr seg-reg val check-alignment? x86 + &key (check-canonicity 'nil)) + (declare (xargs :guard (natp seg-reg))) + `(mbe + :logic + (wime64 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + ,(if (or (eql seg-reg #.*fs*) + (eql seg-reg #.*gs*)) + `(b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + (if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr ,8 nil))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wiml64 lin-addr ,val ,x86)) + (wime64 ,proc-mode ,eff-addr ,seg-reg ,val ,check-alignment? ,x86)))) + +;; Note that unlike the macros above, the generic rme/wme*-size macros work for +;; all kinds of seg-reg values. + +(defmacro rme-size-opt + (proc-mode nbytes eff-addr seg-reg r-x check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + `(mbe + :logic + (rme-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + (if (or (eql ,seg-reg #.*fs*) + (eql ,seg-reg #.*gs*)) + (b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + ,(if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr ,nbytes ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (rml-size ,nbytes lin-addr ,r-x ,x86)) + (rme-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro rime-size-opt + (proc-mode nbytes eff-addr seg-reg r-x check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + `(mbe + :logic + (rime-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + (if (or (eql ,seg-reg #.*fs*) + (eql ,seg-reg #.*gs*)) + (b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + ,(if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg 0 ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr ,nbytes ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) 0 ,x86))) + (riml-size ,nbytes lin-addr ,r-x ,x86)) + (rime-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,r-x ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro wme-size-opt + (proc-mode nbytes eff-addr seg-reg val check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + `(mbe + :logic + (wme-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + (if (or (eql ,seg-reg #.*fs*) + (eql ,seg-reg #.*gs*)) + (b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + ,(if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr ,nbytes ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wml-size ,nbytes lin-addr ,val ,x86)) + (wme-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +(defmacro wime-size-opt + (proc-mode nbytes eff-addr seg-reg val check-alignment? x86 + &key + (mem-ptr? 'nil) + (check-canonicity 'nil)) + `(mbe + :logic + (wime-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?) + :exec + (if (equal ,proc-mode #.*64-bit-mode*) + (b* (((mv flg (the (signed-byte #.*max-linear-address-size*) lin-addr)) + (if (or (eql ,seg-reg #.*fs*) + (eql ,seg-reg #.*gs*)) + (b* (((mv (the (unsigned-byte 64) base) & &) + (segment-base-and-bounds 0 ,seg-reg ,x86)) + ((the (signed-byte 64) lin-addr) + (i64 (+ base (n64 ,eff-addr))))) + (if (canonical-address-p lin-addr) + (mv nil lin-addr) + (mv (list :non-canonical-address lin-addr) 0))) + ,(if check-canonicity + `(b* (((unless (canonical-address-p ,eff-addr)) + (mv (list :non-canonical-address ,eff-addr) 0))) + (mv nil ,eff-addr)) + `(mv nil ,eff-addr)))) + ((when flg) + (mv flg ,x86)) + ((unless (or (not ,check-alignment?) + (address-aligned-p lin-addr ,nbytes ,mem-ptr?))) + (mv (list :unaligned-linear-address lin-addr) ,x86))) + (wiml-size ,nbytes lin-addr ,val ,x86)) + (wime-size + ,proc-mode ,nbytes ,eff-addr ,seg-reg ,val ,check-alignment? ,x86 + :mem-ptr? ,mem-ptr?)))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/two-byte-opcodes-dispatch.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/two-byte-opcodes-dispatch.lisp new file mode 100644 index 000000000..30f54239c --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/two-byte-opcodes-dispatch.lisp @@ -0,0 +1,245 @@ +; 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@('two-byte-opcode-execute') is the doorway to the two-byte + opcode map, and will lead to the three-byte opcode map if @('opcode') is + either @('#x38') or @('#x3A').
" + :guard (and (prefixes-p prefixes) + (modr/m-p modr/m) + (sib-p sib) + (rip-guard-okp proc-mode temp-rip)) + :guard-hints (("Goal" + :do-not '(preprocess) + :in-theory (e/d (member-equal) + (logbit + bitops::logbit-to-logbitp + unsigned-byte-p + (:t unsigned-byte-p) + signed-byte-p)))) + + (case opcode ,@dispatch) + + /// + + (defthm x86p-two-byte-opcode-execute + (implies (x86p x86) + (x86p (two-byte-opcode-execute + proc-mode start-rip temp-rip prefixes mandatory-prefix + rex-byte opcode modr/m sib x86))))))) + +(local + (defthm unsigned-byte-p-from-<=-loghead + (implies (and (<= x (loghead n y)) + (natp n) + (natp x)) + (unsigned-byte-p n x)))) + +(define two-byte-opcode-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*)) + (prefixes :type (unsigned-byte #.*prefixes-width*)) + (rex-byte :type (unsigned-byte 8)) + (escape-byte :type (unsigned-byte 8)) + x86) + + :ignore-ok t + :guard (and (prefixes-p prefixes) + (equal escape-byte #x0F) + (rip-guard-okp proc-mode temp-rip)) + :guard-hints (("Goal" :do-not '(preprocess) + :in-theory (e/d* + (rip-guard-okp + add-to-*ip + modr/m-p + add-to-*ip-is-i48p-rewrite-rule) + (unsigned-byte-p + (:type-prescription bitops::logand-natp-type-2) + (:type-prescription bitops::ash-natp-type) + acl2::loghead-identity + not + tau-system + (tau-system))))) + :parents (x86-decoder) + :short "Decoder and dispatch function for two-byte opcodes" + :long "Source: Intel Manual, Volume 2, Appendix A-2
" + + (b* ((ctx 'two-byte-opcode-decode-and-execute) + + ((mv flg0 (the (unsigned-byte 8) opcode) x86) + (rme08 proc-mode temp-rip #.*cs* :x x86)) + ((when flg0) + (!!ms-fresh :opcode-byte-access-error flg0)) + + ;; Possible values of opcode: + + ;; 1. #x38 and #x3A: These are escapes to the two three-byte opcode + ;; maps. Function three-byte-opcode-decode-and-execute is called + ;; here, which also fetches the ModR/M and SIB bytes for these + ;; opcodes. + + ;; 2. All other values denote opcodes of the two-byte map. The function + ;; two-byte-opcode-execute case-splits on this opcode byte and calls + ;; the appropriate instruction semantic function. + + ((mv flg temp-rip) (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) (!!ms-fresh :increment-error flg)) + + ;; Initial spec, buggy: + ;; ((mv msg (the (unsigned-byte 8) mandatory-prefix)) + ;; (compute-mandatory-prefix-for-two-byte-opcode + ;; proc-mode opcode prefixes)) + ;; ((when msg) + ;; (x86-illegal-instruction msg start-rip temp-rip x86)) + + ;; (modr/m? + ;; (two-byte-opcode-ModR/M-p + ;; proc-mode mandatory-prefix opcode)) + ;; ((mv flg1 (the (unsigned-byte 8) modr/m) x86) + ;; (if modr/m? + ;; (rme08 proc-mode temp-rip #.*cs* :x x86) + ;; (mv nil 0 x86))) + ;; ((when flg1) (!!ms-fresh :modr/m-byte-read-error flg1)) + + ;; Fixed, but complicated spec.: + ;; ((mv modr/m-mand-err + ;; (the (unsigned-byte 8) mandatory-prefix) + ;; modr/m? + ;; (the (unsigned-byte 8) modr/m) + ;; x86) + ;; (two-byte-opcode-modr/m-and-mandatory-prefix + ;; proc-mode opcode prefixes temp-rip x86)) + ;; ((when modr/m-mand-err) + ;; (if (and (consp modr/m-mand-err) + ;; (equal (car modr/m-mand-err) :illegal-use-of-mandatory-prefix)) + ;; ;; #UD thrown. + ;; (x86-illegal-instruction modr/m-mand-err start-rip temp-rip x86) + ;; ;; Model-related error + ;; (!!ms-fresh :modr/m-error modr/m-mand-err))) + + ;; Simplified spec.: + ((the (unsigned-byte 8) mandatory-prefix) + (compute-mandatory-prefix-for-two-byte-opcode proc-mode opcode prefixes)) + (modr/m? (two-byte-opcode-ModR/M-p proc-mode mandatory-prefix opcode)) + ((mv flg1 (the (unsigned-byte 8) modr/m) x86) + (if modr/m? + (rme08 proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg1) (!!ms-fresh :modr/m-byte-read-error flg1)) + + ((mv flg temp-rip) (if modr/m? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg)) + + (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 flg2 (the (unsigned-byte 8) sib) x86) + (if sib? + (rme08 proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg2) + (!!ms-fresh :sib-byte-read-error flg2)) + + ((mv flg temp-rip) (if sib? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg))) + + (two-byte-opcode-execute + proc-mode start-rip temp-rip prefixes mandatory-prefix + rex-byte opcode modr/m sib x86)) + + /// + + (defrule x86p-two-byte-opcode-decode-and-execute + (implies (x86p x86) + (x86p (two-byte-opcode-decode-and-execute + proc-mode start-rip temp-rip prefixes + rex-byte escape-byte x86))) + :enable add-to-*ip-is-i48p-rewrite-rule + :disable (signed-byte-p))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/machine/x86.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/machine/x86.lisp new file mode 100644 index 000000000..3787268a3 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/machine/x86.lisp @@ -0,0 +1,893 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, May - August 2023, Regents of the University of Texas +; Copyright (C) August 2023 - May 2024, Yahya Sohail +; Copyright (C) May 2024 - August 2024, Intel Corporation +; 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@('one-byte-opcode-execute') is the doorway to all the opcode + maps (for non-AVX/AVX512 instructions).
" + + :guard (and (prefixes-p prefixes) + (modr/m-p modr/m) + (sib-p sib) + (rip-guard-okp proc-mode temp-rip)) + :guard-hints (("Goal" + :do-not '(preprocess) + :in-theory (e/d () (unsigned-byte-p signed-byte-p)))) + + (case opcode ,@dispatch) + + /// + + (defthm x86p-one-byte-opcode-execute + (implies (x86p x86) + (x86p (one-byte-opcode-execute + proc-mode start-rip temp-rip + prefixes rex-byte opcode modr/m sib x86))))))) + +;; ---------------------------------------------------------------------- + +;; Can be smashed by tty-raw.lsp +(define write-tty ((c :type (unsigned-byte 8)) + x86) + :parents (tty) + :short "Write a byte to the TTY." + :long "This function writes a byte to the @('tty-out') field of the x86 + state by consing the byte to the front of it. Including @('tty-raw.lsp') in + raw lisp smashes this function to write to a TCP port instead.
" + :returns (x86 x86p :hyp (x86p x86)) + (!tty-out (cons c (tty-out x86)) x86)) + +(define read-tty (x86) + :parents (tty) + :short "Read a byte from the TTY." + :long "This function reads a byte from the @('tty-in') field of the x86 + stobj by extracting and returning its @('car') and writing the @('cdr') to + the @('tty-in') field. If the @('tty-in') field is empty (i.e. it is + @('nil')), this function returns @('nil') and the @('x86') stobj is + unmodified. Including @('tty-raw.lsp') in raw lisp smashes this function to + read from a TCP port instead.
" + :returns (mv byt + (x86 x86p :hyp (x86p x86))) + :guard-hints (("Goal" :in-theory (disable ELEM-P-OF-XR-TTY-IN) + :use (:instance ELEM-P-OF-XR-TTY-IN (i nil) (x86$a x86)))) + (b* ((buf (tty-in x86)) + ((when (null buf)) (mv nil x86)) + (byt (car buf)) + (x86 (!tty-in (cdr buf) x86))) + (mv byt x86)) + /// + (defthm unsigned-byte-p-8-non-nil-read-tty + (implies (and (x86p x86) + (mv-nth 0 (read-tty x86))) + (unsigned-byte-p 8 (mv-nth 0 (read-tty x86)))) + :hints (("Goal" :in-theory (disable ELEM-P-OF-XR-TTY-IN) + :use (:instance ELEM-P-OF-XR-TTY-IN (i nil) (x86$a x86)))))) + +(define x86-exec-peripherals (x86) + :parents (peripherals) + :short "Execute the peripherals" + :guard-hints (("Goal" :use ((:instance ELEM-P-OF-XR-LAST-CLOCK-EVENT (i nil) (x86$a x86)) + (:instance ELEM-P-OF-XR-TIME-STAMP-COUNTER (i nil) (x86$a x86))) + :in-theory (disable ELEM-P-OF-XR-LAST-CLOCK-EVENT ELEM-P-OF-XR-TIME-STAMP-COUNTER))) + :returns (x86 x86p :hyp (x86p x86)) + (b* (((when (app-view x86)) x86) + ;; Increment the time stamp counter + (time-stamp-counter (n64 (1+ (time-stamp-counter x86)))) + (x86 (!time-stamp-counter time-stamp-counter x86)) + + ;; This is part of the clock device. + (x86 (if (app-view x86) + x86 + (wm-low-64 #x100 time-stamp-counter x86))) + + (last-clock-event (last-clock-event x86)) + (x86 (if (and (> last-clock-event 100000) + (equal (rflagsBits->intf (rflags x86)) 1) + (not (inhibit-interrupts-one-instruction x86)) + (not (equal (memi #x108 x86) 0)) + (not (fault x86))) + (b* ((x86 (!last-clock-event 0 x86)) + (x86 (!fault (cons '(:interrupt 32) (fault x86)) x86))) + x86) + (!last-clock-event (1+ last-clock-event) x86))) + + ;; Time to check for any TTY output + ;; The TTY protocol is simple. If 0x3F9 is nonzero + ;; 0x3F8 has an output character. (physical addresses of course) + (x86 (b* ((tty-byte-valid (not (equal (memi #x3F9 x86) 0))) + ((when (not tty-byte-valid)) x86) + (tty-output-byte (memi #x3F8 x86)) + (x86 (!memi #x3F9 0 x86)) + (x86 (write-tty tty-output-byte x86))) + x86)) + ;; We check if the tty input buffer is empty + ;; If so, we write the new byte and set valid flag + (x86 (b* ((tty-write-byte-valid (not (equal (memi #x3FB x86) 0))) + ((when tty-write-byte-valid) x86) + ((mv tty-input x86) (read-tty x86)) + ((when (equal tty-input nil)) x86) + (x86 (!memi #x3FA tty-input x86)) + (x86 (!memi #x3FB 1 x86))) + x86))) + x86)) + +(define x86-fetch-decode-execute (x86) + + :parents (x86-decoder) + :short "Top-level step function" + + :long "@('x86-fetch-decode-execute') is the step function of our x86 + interpreter. It fetches one instruction by looking up the memory address + indicated by the instruction pointer @('rip'), decodes that instruction, and + dispatches control to the appropriate instruction semantic function.
" + + :prepwork + ((local + (defthm guard-helper-1 + (implies (and (<= 0 (+ x y)) + (<= (+ x y) a) + (unsigned-byte-p 32 a) + (integerp x) (integerp y)) + (signed-byte-p 48 (+ x y))))) + + (local + (defthm guard-helper-2 + (implies (and (<= 0 (+ x y)) + (<= (+ x y) a) + (unsigned-byte-p 32 a) + (integerp x) (integerp y)) + (signed-byte-p 64 (+ x y))))) + + (local + (defthm guard-helper-3 + (implies (unsigned-byte-p 8 b0) + (and + (unsigned-byte-p 32 (logior 98 (ash b0 8))) + (unsigned-byte-p 24 (logior 196 (ash b0 8))) + (unsigned-byte-p 24 (logior 197 (ash b0 8))))))) + + (local + (defthm guard-helper-4 + (implies (and (unsigned-byte-p 4 num) + (signed-byte-p 48 rip)) + (and (signed-byte-p 64 (+ 1 rip num)) + (signed-byte-p 64 (+ 2 rip num)))))) + + (local + (defthm guard-helper-5 + (implies (unsigned-byte-p 4 num) + (signed-byte-p 48 (+ 1 num))))) + + (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-from-<= + (implies (and (<= x y) + (unsigned-byte-p n y) + (natp x)) + (unsigned-byte-p n x)))) + + (local (in-theory (e/d* () + (signed-byte-p + unsigned-byte-p + not (tau-system))))) + + ;; For RoW proofs involving these bitstructs' accessors and updaters: + (local (in-theory (e/d (!vex-prefixes->byte0-is-vex-prefixes + !vex-prefixes->byte1-is-vex-prefixes + !vex-prefixes->byte2-is-vex-prefixes + !evex-prefixes->byte0-is-evex-prefixes + !evex-prefixes->byte1-is-evex-prefixes + !evex-prefixes->byte2-is-evex-prefixes + !evex-prefixes->byte3-is-evex-prefixes) + ())))) + + :guard-hints + (("Goal" :in-theory (e/d (modr/m-p + prefixes-p + vex-prefixes-byte0-p + add-to-*ip + add-to-*ip-is-i48p-rewrite-rule) + ()))) + + (b* (;; We don't want our interpreter to take a step if the machine is in a + ;; bad state. Such checks are made in x86-run but I am duplicating them + ;; here in case this function is being used at the top-level. + ((when (or (ms x86) (fault x86))) x86) + (app-view? (app-view x86)) + + (inhibit-interrupts-one-instruction? (inhibit-interrupts-one-instruction x86)) + (x86 (b* ((ctx 'x86-fetch-decode-execute) + + (proc-mode (x86-operation-mode x86)) + (64-bit-modep (equal proc-mode #.*64-bit-mode*)) + + (start-rip (the (signed-byte #.*max-linear-address-size*) + (read-*ip proc-mode x86))) + + ((mv flg + (the (unsigned-byte #.*prefixes-width*) prefixes) + (the (unsigned-byte 8) rex-byte) + x86) + (get-prefixes proc-mode start-rip 0 0 15 x86)) + ;; Among other errors (including if there are 15 prefix (legacy and REX) + ;; bytes, which leaves no room for an opcode byte in a legal + ;; instruction), if get-prefixes detects a non-canonical address while + ;; attempting to fetch prefixes, flg will be non-nil. + ((when flg) + (!!ms-fresh :error-in-reading-prefixes flg)) + + ((the (unsigned-byte 8) opcode/vex/evex-byte) + (prefixes->nxt prefixes)) + + ((the (unsigned-byte 4) prefix-length) + (prefixes->num prefixes)) + + ((mv flg temp-rip) (add-to-*ip proc-mode start-rip (1+ prefix-length) x86)) + ((when flg) (!!ms-fresh :increment-error flg)) + + (vex-byte0? (or (equal opcode/vex/evex-byte #.*vex2-byte0*) + (equal opcode/vex/evex-byte #.*vex3-byte0*))) + ;; If opcode/vex/evex-byte is either 0xC4 (*vex3-byte0*) or 0xC5 + ;; (*vex2-byte0*), then we always have a VEX-encoded instruction in the + ;; 64-bit mode. But in the 32-bit mode, these bytes may not signal the + ;; start of the VEX prefixes. 0xC4 and 0xC5 map to LES and LDS + ;; instructions (respectively) in the 32-bit mode if bits[7:6] of the + ;; next byte, which we call les/lds-distinguishing-byte below, are *not* + ;; 11b. Otherwise, they signal the start of VEX prefixes in the 32-bit + ;; mode too. + + ;; Though the second byte acts as the distinguishing byte only in the + ;; 32-bit mode, we always read the first two bytes of a VEX prefix in + ;; this function for simplicity. + ((mv flg les/lds-distinguishing-byte x86) + (if vex-byte0? + (rme08-opt proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg) + (!!ms-fresh :les/lds-distinguishing-byte-read-error flg)) + ;; If the instruction is indeed LDS or LES in the 32-bit mode, temp-rip + ;; is incremented after the ModR/M is detected (see add-to-*ip following + ;; modr/m? below). + ((when (and vex-byte0? + (or 64-bit-modep + (and (not 64-bit-modep) + (equal (part-select + les/lds-distinguishing-byte + :low 6 :high 7) + #b11))))) + ;; Handle VEX-encoded instructions separately. + (b* (((mv flg temp-rip) + (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) + (!!ms-fresh :vex-byte1-increment-error flg)) + (vex-prefixes + (!vex-prefixes->byte0 opcode/vex/evex-byte 0)) + (vex-prefixes + (!vex-prefixes->byte1 les/lds-distinguishing-byte vex-prefixes))) + (vex-decode-and-execute + proc-mode + start-rip temp-rip prefixes rex-byte vex-prefixes x86))) + + (opcode/evex-byte opcode/vex/evex-byte) + + (evex-byte0? (equal opcode/evex-byte #.*evex-byte0*)) + ;; Byte 0x62 is byte0 of the 4-byte EVEX prefix. In 64-bit mode, this + ;; byte indicates the beginning of the EVEX prefix --- note that in the + ;; pre-AVX512 era, this would lead to a #UD, but we don't model that + ;; here. + + ;; Similar to the VEX prefix situation, things are more complicated in + ;; the 32-bit mode, where 0x62 aliases to the 32-bit only BOUND + ;; instruction. The Intel Manuals (May, 2018) don't seem to say + ;; anything explicitly about how one differentiates between the EVEX + ;; prefix and the BOUND instruction in 32-bit mode. However, a legal + ;; BOUND instruction must always have a memory operand as its second + ;; operand, which means that ModR/M.mod != 0b11 (see Intel Vol. 2, Table + ;; 2-2). So, if bits [7:6] of the byte following 0x62 are NOT 0b11, + ;; then 0x62 refers to a legal BOUND instruction. Otherwise, it signals + ;; the beginning of the EVEX prefix. + + ;; Again, similar to the VEX prefix situation: though the second byte + ;; acts as the distinguishing byte only in the 32-bit mode, we always + ;; read the first two bytes of an EVEX prefix in this function for + ;; simplicity. + ((mv flg bound-distinguishing-byte x86) + (if evex-byte0? + (rme08-opt proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg) + (!!ms-fresh :bound-distinguishing-byte-read-error flg)) + ;; If the instruction is indeed BOUND in the 32-bit mode, temp-rip is + ;; incremented after the ModR/M is detected (see add-to-*ip following + ;; modr/m? below). + ((when (and evex-byte0? + (or 64-bit-modep + (and (not 64-bit-modep) + (equal (part-select + bound-distinguishing-byte + :low 6 :high 7) + #b11))))) + ;; Handle EVEX-encoded instructions separately. + (b* (((mv flg temp-rip) + (add-to-*ip proc-mode temp-rip 1 x86)) + ((when flg) + (!!ms-fresh :evex-byte1-increment-error flg)) + (evex-prefixes + (!evex-prefixes->byte0 opcode/evex-byte 0)) + (evex-prefixes + (!evex-prefixes->byte1 bound-distinguishing-byte evex-prefixes))) + (evex-decode-and-execute + proc-mode + start-rip temp-rip prefixes rex-byte evex-prefixes x86))) + + + (opcode-byte opcode/evex-byte) + + ;; Possible values of opcode-byte: + + ;; The opcode-byte should not contain any of the (legacy) prefixes, REX + ;; bytes, VEX prefixes, and EVEX prefixes -- by this point, all these + ;; prefix bytes should have been processed. So, here are the kinds of + ;; values opcode-byte can have: + + ;; 1. An opcode of the one-byte opcode map: this function prefetches the + ;; ModR/M and SIB bytes for these opcodes. The function + ;; one-byte-opcode-execute case-splits on this opcode byte and calls + ;; the appropriate instruction semantic function. + + ;; 2. #x0F -- two-byte or three-byte opcode indicator: modr/m? is set to + ;; NIL (see *64-bit-mode-one-byte-has-modr/m-ar* and + ;; *32-bit-mode-one-byte-has-modr/m-ar*). No ModR/M and SIB bytes + ;; are prefetched by this function for the two-byte or three-byte + ;; opcode maps. In one-byte-opcode-execute, we call + ;; two-byte-opcode-decode-and-execute, where we fetch the ModR/M and + ;; SIB bytes for the two-byte opcodes or dispatch control to + ;; three-byte-opcode-decode-and-execute when appropriate (i.e., when + ;; the byte following #x0F is either #x38 or #x3A). Note that in + ;; this function, temp-rip will not be incremented beyond this point + ;; for these opcodes --- i.e., it points at the byte *following* + ;; #x0F. + + ;; The modr/m and sib byte prefetching in this function is biased + ;; towards the one-byte opcode map. The functions + ;; two-byte-opcode-decode-and-execute and + ;; three-byte-opcode-decode-and-execute do their own prefetching. We + ;; made this choice to take advantage of the fact that the most + ;; frequently encountered instructions are from the one-byte opcode map. + ;; Another reason is that the instruction encoding syntax is clearer to + ;; understand this way; this is a nice way of seeing how one opcode map + ;; "escapes" into another. + + (modr/m? (one-byte-opcode-ModR/M-p proc-mode opcode-byte)) + ((mv flg (the (unsigned-byte 8) modr/m) x86) + (if modr/m? + (if (or vex-byte0? evex-byte0?) + ;; The above will be true only if the instruction is LES or LDS + ;; or BOUND in the 32-bit mode. + (mv nil les/lds-distinguishing-byte x86) + (rme08-opt proc-mode temp-rip #.*cs* :x x86)) + (mv nil 0 x86))) + ((when flg) + (!!ms-fresh :modr/m-byte-read-error flg)) + + ((mv flg temp-rip) + (if modr/m? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg)) + + (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 flg (the (unsigned-byte 8) sib) x86) + (if sib? + (rme08-opt proc-mode temp-rip #.*cs* :x x86) + (mv nil 0 x86))) + ((when flg) + (!!ms-fresh :sib-byte-read-error flg)) + + ((mv flg temp-rip) + (if sib? + (add-to-*ip proc-mode temp-rip 1 x86) + (mv nil temp-rip))) + ((when flg) (!!ms-fresh :increment-error flg)) + + (x86 (one-byte-opcode-execute + proc-mode start-rip temp-rip prefixes rex-byte opcode-byte + modr/m sib x86))) + x86)) + + ((when app-view?) x86) + + (x86 (if (enable-peripherals x86) + (x86-exec-peripherals x86) + x86)) + (x86 (if (and (fault x86) + (handle-exceptions x86)) + (handle-faults x86) + x86)) + + (x86 (if inhibit-interrupts-one-instruction? + (!inhibit-interrupts-one-instruction nil x86) + x86))) + x86) + + + /// + + (defrule x86p-x86-fetch-decode-execute + (implies (x86p x86) + (x86p (x86-fetch-decode-execute x86))) + ;; for speed: + :disable (xw-xw-inter-field-arrange-writes + mv-nth-0-of-add-to-*ip-when-64-bit-modep + mv-nth-1-of-add-to-*ip-when-64-bit-modep + bitops::part-select-width-low$inline + bitops::part-select-low-high$inline + mv-nth + i48p-xr-rip + right-shift-to-logtail + acl2::logtail-identity + get-prefixes-opener-lemma-no-legacy-prefix-but-rex-prefix + get-prefixes-opener-lemma-group-2-prefix + get-prefixes-opener-lemma-group-1-prefix + get-prefixes-opener-lemma-group-4-prefix + get-prefixes-opener-lemma-group-3-prefix + get-prefixes-opener-lemma-zero-cnt + get-prefixes-opener-lemma-no-prefix-byte + get-prefixes-opener-lemma-no-legacy-prefix-but-rex-prefix + get-prefixes-does-not-modify-x86-state-in-app-view + num-prefixes-get-prefixes-bound) + :enable add-to-*ip-is-i48p-rewrite-rule) + + (defthmd ms-fault-and-x86-fetch-decode-and-execute + (implies (and (x86p x86) + (or (ms x86) (fault x86))) + (equal (x86-fetch-decode-execute x86) x86))) + + (defthm x86-fetch-decode-execute-opener + ;; Note that this opener lemma applies to all supported modes of operation + ;; (see x86-operation-mode). + + ;; TODO: Extend to VEX and EVEX prefixes. + (implies + (and + (app-view x86) + (not (ms x86)) + (not (fault x86)) + (equal proc-mode (x86-operation-mode x86)) + (equal start-rip (read-*ip proc-mode x86)) + (not (mv-nth 0 (get-prefixes proc-mode start-rip 0 0 15 x86))) + (equal prefixes (mv-nth 1 (get-prefixes proc-mode start-rip 0 0 15 x86))) + (equal rex-byte (mv-nth 2 (get-prefixes proc-mode start-rip 0 0 15 x86))) + (equal opcode/vex/evex-byte (prefixes->nxt prefixes)) + (equal prefix-length (prefixes->num prefixes)) + (equal temp-rip0 + (mv-nth 1 (add-to-*ip proc-mode start-rip (1+ prefix-length) x86))) + + ;; *** No VEX prefixes *** + (not (equal opcode/vex/evex-byte #.*vex3-byte0*)) + (not (equal opcode/vex/evex-byte #.*vex2-byte0*)) + ;; *** No EVEX prefixes *** + (not (equal opcode/vex/evex-byte #.*evex-byte0*)) + + (equal modr/m? + (one-byte-opcode-ModR/M-p proc-mode opcode/vex/evex-byte)) + (equal modr/m (if modr/m? + (mv-nth 1 (rme08 proc-mode temp-rip0 #.*cs* :x x86)) + 0)) + (equal temp-rip1 (if modr/m? + (mv-nth 1 (add-to-*ip proc-mode temp-rip0 1 x86)) + temp-rip0)) + (equal p4? (equal #.*addr-size-override* (prefixes->adr prefixes))) + (equal 16-bit-addressp (equal 2 (select-address-size proc-mode p4? x86))) + (equal sib? (and modr/m? (x86-decode-sib-p modr/m 16-bit-addressp))) + (equal sib (if sib? (mv-nth 1 (rme08 proc-mode temp-rip1 #.*cs* :x x86)) 0)) + + (equal temp-rip2 (if sib? + (mv-nth 1 (add-to-*ip proc-mode temp-rip1 1 x86)) + temp-rip1)) + + (or (app-view x86) (not (marking-view x86))) + (not (mv-nth 0 (add-to-*ip proc-mode start-rip (1+ prefix-length) x86))) + (if modr/m? + (and (not (mv-nth 0 (add-to-*ip proc-mode temp-rip0 1 x86))) + (not (mv-nth 0 (rme08 proc-mode temp-rip0 #.*cs* :x x86)))) + t) + (if sib? + (and (not (mv-nth 0 (add-to-*ip proc-mode temp-rip1 1 x86))) + (not (mv-nth 0 (rme08 proc-mode temp-rip1 #.*cs* :x x86)))) + t) + (x86p x86) + ;; Print the rip and the first opcode byte of the instruction + ;; under consideration after all the non-trivial hyps (above) of + ;; this rule have been relieved: + (syntaxp + (and (not (cw "~% [ x86instr @ rip: ~p0 ~%" start-rip)) + (not (cw " op0: ~s0 ] ~%" + (str::hexify (unquote opcode/vex/evex-byte))))))) + (equal + (x86-fetch-decode-execute x86) + (one-byte-opcode-execute + proc-mode start-rip temp-rip2 prefixes rex-byte + opcode/vex/evex-byte modr/m sib x86))) + :hints (("Goal" + :cases ((app-view x86)) + :in-theory (e/d () + (one-byte-opcode-execute + signed-byte-p + not + member-equal)))))) + +(in-theory (e/d (vex-decode-and-execute + evex-decode-and-execute + one-byte-opcode-execute + two-byte-opcode-execute + first-three-byte-opcode-execute + second-three-byte-opcode-execute) + ())) + +;; ---------------------------------------------------------------------- + +;; Running the interpreter: + +;; Schedule: (in the M1 style) + +(defun binary-clk+ (i j) + (+ (nfix i) (nfix j))) + +(defthm clk+-associative + (implies (binary-clk+ (binary-clk+ i j) k) + (binary-clk+ i (binary-clk+ j k)))) + +(defmacro clk+ (&rest args) + (if (endp args) + 0 + (if (endp (cdr args)) + (car args) + `(binary-clk+ ,(car args) + (clk+ ,@(cdr args)))))) + +(define x86-run + ;; I fixed n to a fixnum for efficiency. Also, executing more than + ;; 2^59 instructions in one go seems kind of daunting. + ((n :type (unsigned-byte 59)) + x86) + + :parents (x86-decoder) + :short "Top-level specification function for the x86 ISA model" + :long "@('x86-run') returns the x86 state obtained by executing + @('n') instructions or until it halts, whatever comes first. It + simply called the step function @(see x86-fetch-decode-execute) + recursively.
" + + :returns (x86 x86p :hyp (x86p x86)) + + (cond ((fault x86) + x86) + ((ms x86) + x86) + ((mbe :logic (zp n) + :exec (equal 0 n)) + x86) + (t (let* ((x86 (x86-fetch-decode-execute x86)) + (n (the (unsigned-byte 59) (1- n)))) + (x86-run n x86)))) + + + /// + + (defthmd x86-run-and-x86-fetch-decode-and-execute-commutative + ;; x86-fetch-decode-execute and x86-run can commute. + (implies (and (natp k) + (x86p x86) + (not (ms x86)) + (not (fault x86))) + (equal (x86-run k (x86-fetch-decode-execute x86)) + (x86-fetch-decode-execute (x86-run k x86)))) + :hints (("Goal" :in-theory (e/d + (ms-fault-and-x86-fetch-decode-and-execute) ())))) + + + ;; Some opener theorems for x86-run: + + (defthm x86-run-halted + (implies (or (ms x86) (fault x86)) + (equal (x86-run n x86) x86))) + + (defthm x86-run-opener-not-ms-not-fault-zp-n + (implies (and (syntaxp (quotep n)) + (zp n)) + (equal (x86-run n x86) x86))) + + (defthm x86-run-opener-not-ms-not-zp-n + (implies (and (not (ms x86)) + (not (fault x86)) + (syntaxp (quotep n)) + (not (zp n))) + (equal (x86-run n x86) + (x86-run (1- n) (x86-fetch-decode-execute x86))))) + + ;; To enable compositional reasoning, we prove the following two + ;; theorems: + + (defthm x86-run-plus + (implies (and (natp n1) + (natp n2) + (syntaxp (quotep n1))) + (equal (x86-run (clk+ n1 n2) x86) + (x86-run n2 (x86-run n1 x86))))) + + (encapsulate + () + + (local (include-book "arithmetic/top" :dir :system)) + + (defthmd x86-run-plus-1 + (implies (and (natp n1) + (natp n2) + (syntaxp (quotep n1))) + (equal (x86-run (clk+ n1 n2) x86) + (x86-run n1 (x86-run n2 x86))))))) + +(in-theory (disable binary-clk+)) + +;; ---------------------------------------------------------------------- + +;; Some variants of x86-run: + +(define x86-run-steps1 + ((n :type (unsigned-byte 59)) + (n0 :type (unsigned-byte 59)) + x86) + + :enabled t + :guard (and (natp n) + (natp n0) + (<= n n0)) + + (b* ((diff (the (unsigned-byte 59) (- n0 n))) + ;; (- (if (equal (mod diff 100000000) 0) + ;; (save-x86 "x86-state" x86) + ;; nil)) + ) + (cond ((ms x86) + (mv diff x86)) + ((fault x86) + (mv diff x86)) + ((zp n) + (let* ((ctx 'x86-run) + (x86 (!!ms-fresh :timeout t))) + (mv diff x86))) + (t (b* ((x86 (x86-fetch-decode-execute x86)) + (n-1 (the (unsigned-byte 59) (1- n)))) + (x86-run-steps1 n-1 n0 x86)))))) + +(defun next-n-bytes-null-p (addr len x86) + (declare (xargs :stobjs (x86) + :mode :program)) + (b* (((when (equal len 0)) (mv t x86)) + ((mv flg val x86) (rvm08 addr x86)) + ((when flg) (mv t x86)) + ((when (not (equal val 0))) (mv nil x86))) + (next-n-bytes-null-p (1+ addr) (1- len) x86))) + +(define x86-run-steps + ((n :type (unsigned-byte 59)) + x86) + + :parents (x86-decoder) + :short "An alternative to @(see x86-run)" + :long "@('x86-run-steps') returns two values --- number of steps + taken by the machine before it comes to a halt and the x86 state. + Note that the number of steps will always be less than or equal to + @('n').
" + + (x86-run-steps1 n n x86) + + /// + + (defthm x86-run-steps1-is-x86-run-ms + (implies (ms x86) + (equal (mv-nth 1 (x86-run-steps1 n n0 x86)) + (x86-run n x86)))) + + (defthm x86-run-steps1-is-x86-run-zp-n + (implies (and (not (ms x86)) + (not (fault x86)) + (zp n)) + (equal (mv-nth 1 (x86-run-steps1 n n0 x86)) + (!ms (list (list* 'x86-run + :rip (rip x86) + '(:timeout t))) + (x86-run n x86)))) + :hints (("Goal" :expand (x86-run n x86)))) + + (defthm x86-run-steps1-open + (implies (and (not (ms x86)) + (not (fault x86)) + (not (zp n))) + (equal (mv-nth 1 (x86-run-steps1 n n0 x86)) + (mv-nth 1 (x86-run-steps1 (1- n) n0 + (x86-fetch-decode-execute x86))))))) + +(in-theory (disable x86-run-steps1)) + +(define x86-fetch-decode-execute-halt + ((halt-address :type (signed-byte #.*max-linear-address-size*)) + x86) + :enabled t + :parents (x86-decoder) + :inline t + :no-function t + + :short "Alternative version of @(tsee x86-fetch-decode-execute) that sets the + @('MS') field if @('rip') is equal to @('halt-address')" + + :returns (x86 x86p :hyp (x86p x86)) + + :prepwork + ((local (in-theory (e/d* () (signed-byte-p unsigned-byte-p not))))) + + (if (equal (the (signed-byte #.*max-linear-address-size*) + (rip x86)) + halt-address) + (b* ((ctx 'x86-fetch-decode-execute-halt)) + (!!ms-fresh)) + (x86-fetch-decode-execute x86))) + +(define x86-run-halt + ((halt-address :type (signed-byte #.*max-linear-address-size*)) + (n :type (unsigned-byte 59)) + x86) + + :parents (x86-decoder) + :short "Alternative version of @(tsee x86-run) that uses @(tsee + x86-fetch-decode-execute-halt) instead of @(tsee x86-fetch-decode-execute)" + + :returns (x86 x86p :hyp (x86p x86)) + + (cond ((fault x86) x86) + ((ms x86) x86) + ((mbe :logic (zp n) :exec (equal 0 n)) x86) + (t (let* ((x86 (x86-fetch-decode-execute-halt halt-address x86)) + (n (the (unsigned-byte 59) (1- n)))) + (x86-run-halt halt-address n x86))))) + +(define x86-run-halt-count + ((halt-address :type (signed-byte #.*max-linear-address-size*)) + (n :type (unsigned-byte 59)) + x86 + (n0 :type (unsigned-byte 59))) + + :guard (<= n n0) ;; n == n0, to begin with. + + :short "A combination in functionality of @(tsee x86-run-steps) and @(tsee + x86-run-halt)" + + :long "Run @('n') instructions or till @('halt-address') is reached, + whichever comes first. This function also returns the number of times @(tsee + x86-fetch-decode-execute-halt) is executed. Note that + @('x86-fetch-decode-execute-halt') is executed one more time than @(tsee + x86-fetch-decode-execute) --- i.e., when @('rip == halt-address').
" + + :returns (mv (steps natp :hyp (and (natp n0) (natp n)) + :rule-classes :type-prescription) + (x86 x86p :hyp (x86p x86))) + + (if (mbt (<= n n0)) + + (cond ((or (fault x86) + (ms x86) + (mbe :logic (zp n) :exec (equal 0 n))) + (mv (the (unsigned-byte 59) (- n0 n)) x86)) + (t + (let* ((x86 (x86-fetch-decode-execute-halt halt-address x86)) + (n (the (unsigned-byte 59) (1- n)))) + (x86-run-halt-count halt-address n x86 n0)))) + + (mv 0 x86))) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/portcullis/package.lsp b/crates/clazel-x64/tests/data/acl2-x86isa/portcullis/package.lsp new file mode 100644 index 000000000..48c8caecf --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/portcullis/package.lsp @@ -0,0 +1,107 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; 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 GoelSource: Intel Manual, Mar 2026, Vol. 1, Section 8.1.3.
" + ((ie bitp) ;; Invalid Operation Flag + (de bitp) ;; Denormalized Operand Flag + (ze bitp) ;; Zero Divide Flag + (oe bitp) ;; Overflow Flag + (ue bitp) ;; Underflow Flag + (pe bitp) ;; Precision Flag + (sf bitp) ;; Stack Fault + (es bitp) ;; Error Summary Status + (c0 bitp) ;; Condition Code + (c1 bitp) ;; Condition Code + (c2 bitp) ;; Condition Code + (top 3bits) ;; Top of stack pointer + (c3 bitp) ;; Condition Code + (b bitp) ;; FPU Busy + ) + :msb-first nil + :inline t) + +(local + (defthm fp-status-layout-ok + (iff (fp-statusBits-p x) + (unsigned-byte-p 16 x)) + :rule-classes nil)) + +(in-theory (disable unsigned-byte-p-when-fp-statusbits-p)) + +(defbitstruct mxcsrBits + :short "The MXCSR Control and Status Register." + :long "Source: Intel Manual, Mar 2026, Vol. 1, Section 10.2.3.
" + ;; From the manual: + ;; Bits 16 through 31 of the MXCSR register are reserved and are + ;; cleared on a power-up or reset of the processor; attempting to + ;; write a non-zero value to these bits, using either the FXRSTOR + ;; or LDMXCSR instructions, will result in a general-protection + ;; exception (#GP) being generated. + ((ie bitp) ;; Invalid Operation Flag + (de bitp) ;; Denormal Flag + (ze bitp) ;; Divide-by-Zero Flag + (oe bitp) ;; Overflow Flag + (ue bitp) ;; Underflow Flag + (pe bitp) ;; Precision Flag + (daz bitp) ;; Denormals are Zeros + (im bitp) ;; Invalid Operation Mask + (dm bitp) ;; Denormal Mask + (zm bitp) ;; Divide-by-Zero Mask + (om bitp) ;; Overflow Mask + (um bitp) ;; Underflow Mask + (pm bitp) ;; Precision Mask + (rc 2bits) ;; Rounding Control + (ftz bitp) ;; Flush to Zero + (reserved 16bits) ;; Reserved bits + ) + :msb-first nil + :inline t) + +(local + (defthm mxcsr-layout-ok + (iff (mxcsrBits-p x) + (unsigned-byte-p 32 x)) + :rule-classes nil)) + +(in-theory (disable unsigned-byte-p-when-mxcsrbits-p)) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/utils/paging-structures.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/utils/paging-structures.lisp new file mode 100644 index 000000000..27bf0e59a --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/utils/paging-structures.lisp @@ -0,0 +1,288 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2026, Kestrel Technology, LLC +; 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 GoelWe define some bit structures using @(see fty::defbitstruct) to + describe the fields of registers and x86 data structures.
" ) + +(local (xdoc::set-default-parents structures)) + +;; ---------------------------------------------------------------------- + +;; We add "Bits" as a suffix for all bitstructs other than the decoding-related +;; ones and the basic ones in basic-structs.lisp. The reason is that +;; otherwise, the names of these other bitstructs may clash with x86 state +;; accessor functions (e.g., rflags, mxcsr, etc.). + +;; Decoding-related Structures: + +(defsection legacy-prefixes-layout-structure + + :short "Structure to collect legacy prefix bytes from an x86 instruction." + + :long + "These prefixes are described in + Intel manual, Mar 2026, Vol. 2, Sections 2.1 and 2.2.
+The field @('num') of @('prefixes') not only includes the number of + legacy prefixes present in an instruction, but also the number of REX bytes, + even though REX bytes are not stored in this structure. See @(tsee + get-prefixes) for details.
" + + (defbitstruct prefixes + ((num 4bits "Number of Prefix Bytes") + (lck 8bits "Lock Prefix") + (rep 8bits "Repeat Prefix") + (seg 8bits "Segment Override Prefix") + (opr 8bits "Operand-Size Override Prefix") + (adr 8bits "Address-Size Override Prefix") + (nxt 8bits "First non-prefix instruction byte")) + :inline t) + + (local + (defthm prefixes-layout-ok + (iff (prefixes-p x) + (unsigned-byte-p *prefixes-width* x)) + :rule-classes nil))) + +(defsection vex-prefixes-layout-structures + + :short "Functions to decode and structures to collect + VEX prefix bytes in an x86 instruction." + :long + "The VEX prefix is described in + Intel manual, Mar 2026, Vol. 2, Section 2.3.
" + + (defbitstruct vex-prefixes + :short "VEX prefixes." + :long "Intel manual, Mar 2026, Vol. 2, Figure 2-9.
" + ((byte0 8bits "Can either be #xC4 or #xC5") + (byte1 8bits "Byte 1") + (byte2 8bits "Byte 2, relevant only for 3-byte VEX prefixes")) + :inline t) + + (local + (defthm vex-prefixes-layout-ok + (iff (vex-prefixes-p x) + (unsigned-byte-p *vex-width* x)) + :rule-classes nil)) + + (define vex-prefixes-byte0-p ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :returns (ok booleanp) + :short "Returns @('t') if byte0 of the @('vex-prefixes') structure is + either @('*vex2-byte0*') (i.e. C5H) or @('*vex3-byte0*') (i.e. C4H); + returns @('nil') otherwise." + (let ((byte0 (vex-prefixes->byte0 vex-prefixes))) + (or (equal byte0 #.*vex2-byte0*) (equal byte0 #.*vex3-byte0*)))) + + ;; From Intel manual, Mar 2026, Vol. 2, Section 2.3.5.6: + ;; "In 32-bit mode the VEX first + ;; byte C4 and C5 alias onto the LES and LDS instructions. To + ;; maintain compatibility with existing programs the VEX 2nd byte, + ;; bits [7:6] must be 11b." + + ;; So, in 32-bit mode, vex2-byte1 must have R and MSB of + ;; vvvv set to 1, and vex3-byte1 must have R and X set to 1 + ;; if VEX is to be used instead of LES/LDS. + + ;; From Intel manual, Mar 2026, Vol. 2, Section 2.3.5.6: + ;; "If an instruction does not use VEX.vvvv then it should be set to + ;; 1111b otherwise instruction will #UD. In 64-bit mode all 4 bits + ;; may be used. See Table 2-8 for the encoding of the XMM or YMM + ;; registers. In 32-bit and 16-bit modes bit 6 must be 1 (if bit 6 + ;; is not 1, the 2-byte VEX version will generate LDS instruction + ;; and the 3-byte VEX version will ignore this bit)." + + ;; The above is the reason why only 8 XMM/YMM registers are available in + ;; 32-bit and 16-bit modes. + + ;; Source for VEX layout constants: + ;; Intel Vol. 2 (Mar 2026), Figure 2-9 (VEX bit fields) + + ;; Note that the 2-byte VEX implies a leading 0F opcode byte, and + ;; the 3-byte VEX implies leading 0F, 0F 38, or 0F 3A bytes. + + (defbitstruct vex2-byte1 + :short "Byte 1 of 2-byte VEX prefix." + :long "Intel manual, Mar 2026, Vol. 2, Figure 2-9.
" + ((pp 2bits + "Opcode extension providing equivalent functionality of a SIMD + prefix:Intel manual, Mar 2026, Vol. 2, Figure 2-9.
" + ((m-mmmm 5bits + "@('00000'): Reserved for future use (will #UD)Intel manual, Mar 2026, Vol. 2, Figure 2-9.
" + ((pp 2bits + "Opcode extension providing equivalent functionality of + a SIMD prefix:Intel manual, Mar 2026, Vol. 2, Section 2.3.6.1 + says that 2-byte VEX implies 0F opcode byte
" + (b* ((byte0 (vex-prefixes->byte0 vex-prefixes)) + (byte1 (vex-prefixes->byte1 vex-prefixes))) + (case bytes + (#ux0F + (or (equal byte0 #.*vex2-byte0*) + (and (equal byte0 #.*vex3-byte0*) + (equal (vex3-byte1->m-mmmm byte1) #.*v0F*)))) + (otherwise ; 0F38 and 0F3A + (and (equal byte0 #.*vex3-byte0*) + (if (equal bytes #ux0F38) + (equal (vex3-byte1->m-mmmm byte1) #.*v0F38*) + (equal (vex3-byte1->m-mmmm byte1) #.*v0F3A*))))))) + + ;; Some convenient accessor functions for those fields of the VEX prefixes + ;; that are common to both the two- and three-byte forms: + + (define vex->vvvv ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (vvvv (unsigned-byte-p 4 vvvv) + :hyp (vex-prefixes-byte0-p vex-prefixes) + :hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + :short "Get the @('vvvv') field of @('vex-prefixes'); + cognizant of the two- or three-byte VEX prefixes form." + ;; We avoid inlining these to avoid slow compilation in the + ;; vex-opcodes-dispatch book: + :inline nil + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex2-byte0* + (vex2-byte1->vvvv (vex-prefixes->byte1 vex-prefixes))) + (#.*vex3-byte0* + (vex3-byte2->vvvv (vex-prefixes->byte2 vex-prefixes))) + (otherwise (prog2$ (acl2::impossible) 0))) + :guard-hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + + (define vex->l ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (l (unsigned-byte-p 1 l) + :hyp (vex-prefixes-byte0-p vex-prefixes) + :hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + :short "Get the @('L') field of @('vex-prefixes'); + cognizant of the two- or three-byte VEX prefixes form." + :inline nil + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex2-byte0* + (vex2-byte1->l (vex-prefixes->byte1 vex-prefixes))) + (#.*vex3-byte0* + (vex3-byte2->l (vex-prefixes->byte2 vex-prefixes))) + (otherwise (prog2$ (acl2::impossible) 0))) + :guard-hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + + (define vex->pp ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (pp (unsigned-byte-p 2 pp) + :hyp (vex-prefixes-byte0-p vex-prefixes) + :hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + :short "Get the @('pp') field of @('vex-prefixes'); + cognizant of the two- or three-byte VEX prefixes form." + :inline nil + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex2-byte0* + (vex2-byte1->pp (vex-prefixes->byte1 vex-prefixes))) + (#.*vex3-byte0* + (vex3-byte2->pp (vex-prefixes->byte2 vex-prefixes))) + (otherwise (prog2$ (acl2::impossible) 0))) + :guard-hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + + (define vex->r ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (r (unsigned-byte-p 1 r) + :hyp (vex-prefixes-byte0-p vex-prefixes) + :hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + :short "Get the @('R') field of @('vex-prefixes'); + cognizant of the two- or three-byte VEX prefixes form." + :inline nil + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex2-byte0* + (vex2-byte1->r (vex-prefixes->byte1 vex-prefixes))) + (#.*vex3-byte0* + (vex3-byte1->r (vex-prefixes->byte1 vex-prefixes))) + (otherwise (prog2$ (acl2::impossible) 0))) + :guard-hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + + (define vex->w ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :guard (vex-prefixes-byte0-p vex-prefixes) + :returns (w (unsigned-byte-p 1 w) + :hyp (vex-prefixes-byte0-p vex-prefixes) + :hints (("Goal" :in-theory (enable vex-prefixes-byte0-p)))) + :short "Get the @('W') field of @('vex-prefixes'); + cognizant of the two- or three-byte VEX prefixes form." + :inline nil + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex3-byte0* + (vex3-byte2->w (vex-prefixes->byte2 vex-prefixes))) + ;; Source: Intel Vol. 2A, Section 3.1.1.2 --- Opcode Column in the + ;; Instruction Summary Table (Instructions with VEX Prefix): + ;; "The presence of W0 in the opcode column does not preclude the opcode + ;; to be encoded using the C5H form of the VEX prefix, if the semantics + ;; of the opcode does not require other VEX subfields not present in the + ;; two-byte form of the VEX prefix." + (otherwise 0)) + + /// + + (defret if-not-vex3-byte0-then-vex-w=0 + (implies (not (equal (vex-prefixes->byte0 vex-prefixes) #.*vex3-byte0*)) + (equal (vex->w vex-prefixes) 0)))) + + ;; Some convenient accessor functions for those fields of the VEX prefixes + ;; that only apply to the three-byte forms, but that can be extended to + ;; the two-byte forms: + + (define vex->x ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :returns (x (unsigned-byte-p 1 x)) + :short "Get the @('X') field of @('vex-prefixes') for the three-byte form, + or return 1 for the two-byte form." + :long "Although the two-byte form has no @('X') bit, + as far as the VEX encoding of the REX byte is concerned, + the two-byte form can be regarded as encoding the value 0 + for the @('X') bit of the REX byte. + Since the REX.X encoding in VEX is negated, + this function returns 1 for the two-byte form.
+See Intel manual Volume 2 Figure 2-9 of Mar 2026.
" + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex3-byte0* (vex3-byte1->x (vex-prefixes->byte1 vex-prefixes))) + (otherwise 1))) + + (define vex->b ((vex-prefixes :type (unsigned-byte #.*vex-width*))) + :returns (b (unsigned-byte-p 1 b)) + :short "Get the @('B') field of @('vex-prefixes') for the three-byte form, + or return 1 for the two-byte form." + :long "Although the two-byte form has no @('B') bit, + as far as the VEX encoding of the REX byte is concerned, + the two-byte form can be regarded as encoding the value 0 + for the @('B') bit of the REX byte. + Since the REX.B encoding in VEX is negated, + this function returns 1 for the two-byte form.
+See Intel manual Volume 2 Figure 2-9 of Mar 2026.
" + (case (vex-prefixes->byte0 vex-prefixes) + (#.*vex3-byte0* (vex3-byte1->b (vex-prefixes->byte1 vex-prefixes))) + (otherwise 1)))) + +(defsection evex-prefixes-layout-structures + + :short "Functions to decode and structures to collect + EVEX prefix bytes in an x86 instruction." + :long + "The EVEX prefix is described in + Intel manual, Mar 2026, Vol. 2, Section 2.7.
" + + ;; Sources: - Intel Vol. 2, Section 2.7 + ;; - Sandpile, under "byte encodings" section: + ;; http://www.sandpile.org/x86/opc_enc.htm + + (defbitstruct evex-prefixes + :short "EVEX prefixes." + :long "Intel manual, Mar 2026, Vol. 2, Figure 2-11.
" + ((byte0 8bits) ;; Should be #ux62 + (byte1 8bits) + (byte2 8bits) + (byte3 8bits)) + :inline t + :msb-first nil) + + (local + (defthm evex-prefixes-layout-ok + (iff (evex-prefixes-p x) + (unsigned-byte-p *evex-width* x)) + :rule-classes nil)) + + (defbitstruct evex-byte1 + :short "Byte 1 of EVEX prefix." + :long "Intel manual, Mar 2026, Vol. 2, + Figure 2-11 and Table 2-32.
" + ((mmm 3bits + "Access to up to 8 decoding maps (currently only 1, 2, 3, 5, 6). + Compressed legacy escape: + low two bits identical to lowest two bits of VEX.m-mmmm.") + (res bitp "Reserved; must be zero." :default '0) + (r-prime bitp + "High-16 register specifier modifier; + combine with EVEX.R and ModR/M.reg.") + ;; R, X, B are the next-8 register specifier modifiers; + ;; combine with ModR/M.reg, ModR/M.r/m (base, index/vidx). + (b bitp) + (x bitp "Must be set to @('1') in 32-bit mode, + otherwise instruction is BOUND.") + (r bitp "Must be set to @('1') in 32-bit mode, + otherwise instruction is BOUND.")) + :inline t + :msb-first nil + :xvar byte1) + + (defbitstruct evex-byte2 + :short "Byte 2 of EVEX prefix." + :long "Intel manual, Mar 2026, Vol. 2, + Figure 2-11 and Table 2-32.
" + ((pp 2bits "Compressed legacy escape: identical to low two bits of VEX.pp.") + (res bitp "Reserved; Must be one." :default '1) + (vvvv 4bits "VVVV register specifier --- same as VEX.vvvv.") + (w bitp "Operand size promotion / opcode extension.")) + :inline t + :msb-first nil) + + (defbitstruct evex-byte3 + :short "Byte 3 of EVEX prefix." + :long "Intel manual, Mar 2026, Vol. 2, + Figure 2-11 and Table 2-32.
" + ((aaa 3bits "Embedded opmask register specifier.") + (v-prime bitp + "High-16 VVVV/VIDX register specifier; + combine with EVEX.vvvv or when VSIB present.") + (b bitp "Broadcast/RC/SAE Context.") + (vl/rc 2bits "Vector length/RC (denoted as L'L in the Intel manuals).") + (z bitp "Zeroing/Merging.")) + :inline t + :msb-first nil) + + ;; Some wrapper functions to the macros above to make the EVEX dispatch + ;; functions' guard proofs simpler: + + (define evex->aaa ((evex-prefixes evex-prefixes-p)) + :returns (aaa (unsigned-byte-p 3 aaa) :hyp :guard) + :short "Get the @('aaa') field of @('evex-prefixes')." + ;; We avoid inlining these to avoid slow compilation in the + ;; evex-opcodes-dispatch book: + :inline nil + (evex-byte3->aaa (evex-prefixes->byte3 evex-prefixes))) + + (define evex->z ((evex-prefixes evex-prefixes-p)) + :returns (z (unsigned-byte-p 1 z) :hyp :guard) + :short "Get the @('z') field of @('evex-prefixes')." + :inline nil + (evex-byte3->z (evex-prefixes->byte3 evex-prefixes))) + + (define evex->vvvv ((evex-prefixes evex-prefixes-p)) + :returns (vvvv (unsigned-byte-p 4 vvvv) :hyp :guard) + :short "Get the @('vvvv') field of @('evex-prefixes')." + :inline nil + (evex-byte2->vvvv (evex-prefixes->byte2 evex-prefixes))) + + (define evex->v-prime ((evex-prefixes evex-prefixes-p)) + :returns (v-prime (unsigned-byte-p 1 v-prime) :hyp :guard) + :short "Get the @('v-prime') field of @('evex-prefixes')." + :inline nil + (evex-byte3->v-prime (evex-prefixes->byte3 evex-prefixes))) + + (define evex->vl/rc ((evex-prefixes evex-prefixes-p)) + :returns (vl/rc (unsigned-byte-p 2 vl/rc) :hyp :guard) + :short "Get the @('vl/rc') field of @('evex-prefixes')." + :inline nil + (evex-byte3->vl/rc (evex-prefixes->byte3 evex-prefixes))) + + (define evex->pp ((evex-prefixes evex-prefixes-p)) + :returns (pp (unsigned-byte-p 2 pp) :hyp :guard) + :short "Get the @('pp') field of @('evex-prefixes')." + :inline nil + (evex-byte2->pp (evex-prefixes->byte2 evex-prefixes))) + + (define evex->w ((evex-prefixes evex-prefixes-p)) + :returns (w (unsigned-byte-p 1 w) :hyp :guard) + :short "Get the @('W') field of @('evex-prefixes')." + :inline nil + (evex-byte2->w (evex-prefixes->byte2 evex-prefixes)))) + +(defsection ModR/M-structures + :parents (ModR/M-decoding structures) + :short "Bitstruct definitions to store a ModR/M byte and its fields." + :long "Intel manual, Mar 2026, Vol. 2, Figure 2-1.
" + + (local (xdoc::set-default-parents ModR/M-structures)) + + (defbitstruct modr/m + :short "ModR/M byte." + ((r/m 3bits) + (reg 3bits) + (mod 2bits)) + :inline t + :msb-first nil) + + (defthm return-type-of-ModR/M->r/m-linear + (< (ModR/M->r/m modr/m) 8) + :hints (("Goal" :in-theory (e/d (ModR/M->r/m) ()))) + :rule-classes :linear) + + (defthm return-type-of-ModR/M->reg-linear + (< (ModR/M->reg modr/m) 8) + :hints (("Goal" :in-theory (e/d (ModR/M->reg) ()))) + :rule-classes :linear) + + (defthm return-type-of-ModR/M->mod-linear + (< (ModR/M->mod modr/m) 4) + :hints (("Goal" :in-theory (e/d (ModR/M->mod modr/m-fix) ()))) + :rule-classes :linear)) + +(defsection SIB-structures + + :parents (SIB-decoding structures) + :short "Bitstruct definitions to store a SIB byte and its fields." + :long "Intel manual, Mar 2026, Vol. 2, Figure 2-1.
" + + (local (xdoc::set-default-parents SIB-structures)) + + (defbitstruct sib + :short "SIB byte." + ((base 3bits) + (index 3bits) + (scale 2bits)) + :inline t + :msb-first nil) + + (defthm return-type-of-sib->base-linear + (< (sib->base sib) 8) + :hints (("Goal" :in-theory (e/d (sib->base) ()))) + :rule-classes :linear) + + (defthm return-type-of-sib->index-linear + (< (sib->index sib) 8) + :hints (("Goal" :in-theory (e/d (sib->index) ()))) + :rule-classes :linear) + + (defthm return-type-of-sib->scale-linear + (< (sib->scale sib) 4) + :hints (("Goal" :in-theory (e/d (sib->scale sib-fix) ()))) + :rule-classes :linear)) + +;; ---------------------------------------------------------------------- + +;; Rflags: + +(defbitstruct rflagsBits + :short "Flags register." + :long "Intel Manual, Mar 2026, Vol. 1, Section 3.4.3.
" + ((cf bitp) ; carry flag + (res1 bitp :default '1) ; 1 (reserved) + (pf bitp) ; parity flag + (res2 bitp :default '0) ; 0 (reserved) + (af bitp) ; auxiliary carry flag + (res3 bitp :default '0) ; 0 (reserved) + (zf bitp) ; zero flag + (sf bitp) ; sign flag + (tf bitp) ; trap flag + (intf bitp) ; interrupt enable flag + (df bitp) ; direction flag + (of bitp) ; overflow flag + (iopl 2bits) ; I/O privilege level + (nt bitp) ; nested task + (res4 bitp :default '0) ; 0 (reserved) + (rf bitp) ; resume flag + (vm bitp) ; virtual-8086 mode + (ac bitp) ; alignment check / access control + (vif bitp) ; virtual interrupt flag + (vip bitp) ; virtual interrupt pending + (id bitp) ; id flag + (res5 10bits) ; 0 (reserved) + ) + :msb-first nil + :inline t) + +(local + (defthm rflagsBits-layout-ok + (iff (rflagsBits-p x) + (unsigned-byte-p 32 x)) + :rule-classes nil)) + +;; Enable the following for RoW proofs involving rflags +;; (bitstruct accessor and updater functions): + +(def-ruleset rflag-RoWs-enables + '(!rflagsBits->cf-is-rflagsBits + !rflagsBits->res1-is-rflagsBits + !rflagsBits->pf-is-rflagsBits + !rflagsBits->res2-is-rflagsBits + !rflagsBits->af-is-rflagsBits + !rflagsBits->res3-is-rflagsBits + !rflagsBits->zf-is-rflagsBits + !rflagsBits->sf-is-rflagsBits + !rflagsBits->tf-is-rflagsBits + !rflagsBits->intf-is-rflagsBits + !rflagsBits->df-is-rflagsBits + !rflagsBits->of-is-rflagsBits + !rflagsBits->iopl-is-rflagsBits + !rflagsBits->nt-is-rflagsBits + !rflagsBits->res4-is-rflagsBits + !rflagsBits->rf-is-rflagsBits + !rflagsBits->vm-is-rflagsBits + !rflagsBits->ac-is-rflagsBits + !rflagsBits->vif-is-rflagsBits + !rflagsBits->vip-is-rflagsBits + !rflagsBits->id-is-rflagsBits + !rflagsBits->res5-is-rflagsBits)) + +;; ---------------------------------------------------------------------- + +;; Control Registers: + +(defbitstruct cr0Bits + :short "CR0 register." + :long "Intel Manual, Mar 2026, Vol. 3A, Section 2.5.
" + ((pe bitp "Protection Enable") + (mp bitp "Monitor coProcessor") + (em bitp "Emulation Bit") + (ts bitp "Task Switched") + (et bitp "Extension Type") + (ne bitp "Numeric Error") + (res1 10bits "0 (Reserved)") + (wp bitp "Write Protect") + (res2 bitp "0 (Reserved)") + (am bitp "Alignment Mask") + (res3 10bits "0 (Reserved)") + (nw bitp "Not Write-through") + (cd bitp "Cache Disable") + (pg bitp "Paging Bit")) + :msb-first nil + :inline t) + +(local + (defthm cr0-layout-ok + (iff (cr0Bits-p x) + (unsigned-byte-p 32 x)) + :rule-classes nil)) + +(defbitstruct cr3Bits + :short "CR3 register." + :long "Intel Manual, Mar 2026, Vol. 3A, Section 2.5.
" + ((res1 3bits) ;; 0 + (pwt bitp) ;; Page-Level Writes Tranparent + (pcd bitp) ;; Page-Level Cache Disable + (res2 7bits) ;; 0 + (pdb 40bits) ;; Page Directory Base + (res3 12bits) ;; Reserved (must be zero) + ;; The reason why res3 are 12 reserved bits is that + ;; CR3[63:MAXPHYADDR] must be 0 (see Intel Manual Vol. 3A Section 2.5), + ;; and in our model we have 52-bit physical addresses. + ) + :msb-first nil + :inline t) + +(local + (defthm cr3-layout-ok + (iff (cr3Bits-p x) + (unsigned-byte-p 64 x)) + :rule-classes nil)) + +(defbitstruct cr4Bits + :short "CR4 register." + :long "Intel Manual, Mar 2026, Vol. 3A, Section 2.5.
" + ((vme bitp) ;; Virtual-8086 Mode Extensions + (pvi bitp) ;; Protected-Mode Virtual Interrupts + (tsd bitp) ;; Time-Stamp Disable + (de bitp) ;; Debugging Extensions + (pse bitp) ;; Page Size Extensions + (pae bitp) ;; Physical Address Extension + (mce bitp) ;; Machine-Check Enable + (pge bitp) ;; Page Global Enable + (pce bitp) ;; Performance Monitoring Counter Enable + (osfxsr bitp) ;; OS Support for FXSAVE and FXRSTOR + (osxmmexcpt bitp) ;; OS Support for unmasked SIMD FP Exceptions + (umip bitp) ;; User-mode instruction prevention + (la57 bitp) ;; enables 5-level paging + (vmxe bitp) ;; VMX Enable Bit + (smxe bitp) ;; SMX Enable Bit + (res1 bitp) ;; 0 (Reserved) + (fsgsbase bitp) ;; FSGSBase-Enable Bit (enables the instructions + ;; RDFSBASE, RDGSBASE, WRFSBASE, and WRGSBASE) + (pcide bitp) ;; Process-Context Identifiers Enable Bit + (osxsave bitp) ;; XSAVE and Processor Extended States + (kl bitp) ;; Key Locker Enable Bit + (smep bitp) ;; Supervisor Mode Execution Prevention + (smap bitp) ;; Supervisor Mode Access Prevention + (pke bitp) ;; Enable protection keys for user-mode pages + (cet bitp) ;; Control Flow Enforcement Technology + (pks bitp) ;; Protection keys for supervisor-mode pages + (uintr bitp) ;; User interrupts enable bit + (res2 2bits) ;; 0 (Reserved) + (lam-sup bitp) ;; Supervisor LAM enable + (res3 3bits) ;; 0 (Reserved) + (fred bitp) ;; FRED enable + ;; The remaining bits are reserved. + ) + :msb-first nil + :inline t) + +(local + (defthm cr4-layout-ok + (iff (cr4Bits-p x) + (unsigned-byte-p 33 x)) + :rule-classes nil)) + +; Intel manual, Mar 2026, Vol. 3A, Section 13.8.6 +(defbitstruct cr8Bits + :short "CR8 register." + :long "Intel Manual, Mar 2026, Vol. 3A, Section 2.5.
" + ( + ;; Task Priority Level (width = 4). This sets + ;; the threshold value corresponding to the + ;; highest- priority interrupt to be + ;; blocked. A value of 0 means all interrupts + ;; are enabled. This field is available in 64- + ;; bit mode. A value of 15 means all + ;; interrupts will be disabled. + (cr8-trpl 4bits) ;; Task Priority Level + ;; The remaining bits are reserved. + ) + :msb-first nil + :inline t) + +(local + (defthm cr8-layout-ok + (iff (cr8Bits-p x) + (unsigned-byte-p 4 x)) + :rule-classes nil)) + +(defbitstruct xcr0Bits + :short "XCR0 register." + :long "Intel manual, Mar 2026, Vol. 3A, Figure 2-8.
" + ;; Software can access XCR0 only if CR4.OSXSAVE[bit 18] = 1. (This bit + ;; is also readable as CPUID.01H:ECX.OSXSAVE[bit 27].) + ((fpu/mmx-state bitp) ;; This bit must be 1. An attempt + ;; to write 0 to this bit causes a + ;; #GP exception. + (sse-state bitp) + (avx-state bitp) + (bndreg-state bitp) + (bndcsr-state bitp) + (opmask-state bitp) + (zmm_hi256-state bitp) + (hi16_zmm-state bitp) + (res1 bitp) ;; 0 (Reserved) + (pkru-state bitp) + (res2 7bits) ;; 0 (Reserved) + (tileconfig-state bitp) + (tiledata-state bitp) + (res3 45bits)) + :msb-first nil + :inline t) + +(local + (defthm xcr0-layout-ok + (iff (xcr0Bits-p x) + (unsigned-byte-p 64 x)) + :rule-classes nil)) + +;; ---------------------------------------------------------------------- + +;; Model-specific Registers: + +(defbitstruct ia32_eferBits + :short "IA32_EFER register." + :long "Intel Manual, Mar 2026, Vol. 3A, Section 2.2.1.
" + ((sce bitp) ;; Syscall Enable (R/W) (enables SYSCALL/SYSRET) + (res1 7bits) ;; Reserved? + (lme bitp) ;; Long Mode Enabled (R/W) + (res2 bitp) ;; Reserved? + (lma bitp) ;; Long Mode Active (R) + (nxe bitp) ;; Execute Disable Bit Enable (R/W) + ;; (Enables page access restriction by + ;; preventing instruction fetches from + ;; PAE pages with the XD bit set) + ;; The remaining bits are reserved. + ) + :msb-first nil + :inline t) + +(local + (defthm ia32_efer-layout-ok + (iff (ia32_eferBits-p x) + (unsigned-byte-p 12 x)) + :rule-classes nil)) + +;; ---------------------------------------------------------------------- diff --git a/crates/clazel-x64/tests/data/acl2-x86isa/utils/utilities.lisp b/crates/clazel-x64/tests/data/acl2-x86isa/utils/utilities.lisp new file mode 100644 index 000000000..d198781d2 --- /dev/null +++ b/crates/clazel-x64/tests/data/acl2-x86isa/utils/utilities.lisp @@ -0,0 +1,485 @@ +; X86ISA Library + +; Note: The license below is based on the template at: +; http://opensource.org/licenses/BSD-3-Clause + +; Copyright (C) 2015, Regents of the University of Texas +; Copyright (C) 2026, Kestrel Technology, LLC +; 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 GoelDefinitions of constants (of the form @('*2^W*'), where @('W') is the +plain decimal natural number) and functions/macros grouped in @('ruleset')s of +the following form are defined (where in the function/macro names @('W') is +zero-padded to at least two digits, so @('8') is represented as @('08')):
+ +The function @('np-def-n') is used to automatically create these +constants and functions.
") + +;; Lemmas to help in the MBE proof obligations +;; of the generated NTOI and ITON functions below: + +(local + (encapsulate () + + (local (include-book "arithmetic-5/top" :dir :system)) + + (defruled logext-when-unsigned-byte-p-and-sign-changes + (implies (and (unsigned-byte-p size x) + (<= (expt 2 (1- size)) x)) + (equal (logext size x) (- x (expt 2 size)))) + :enable (logext logapp loghead)) + + (defruled loghead-when-signed-byte-p-and-sign-changes + (implies (and (signed-byte-p size x) + (< x 0)) + (equal (loghead size x) (+ (expt 2 size) x))) + :enable loghead + :prep-lemmas + ((defrule lemma + (implies (posp size) + (< (expt 2 (1- size)) + (expt 2 size))) + :rule-classes :linear))))) + +(def-ruleset nwp-defs nil) +(def-ruleset nw-defs nil) +(def-ruleset iwp-defs nil) +(def-ruleset iw-defs nil) +(def-ruleset nw-to-iw-defs nil) +(def-ruleset iw-to-nw-defs nil) + +(define np-def-n (n) + :mode :program + :guard (posp n) + :parents (constants-conversions-and-bounds) + (let* ((str-n (symbol-name (if (< n 10) + (acl2::packn (list 0 n)) + (acl2::packn (list n))))) + (digits (symbol-name (acl2::packn (list n)))) + (2^XY (mk-name "*2^" digits "*")) + (nXYp (mk-name "N" str-n "P")) + (nXY (mk-name "N" str-n)) + (iXYp (mk-name "I" str-n "P")) + (iXY (mk-name "I" str-n)) + (ntoi (mk-name "N" str-n "-TO-I" str-n)) + (iton (mk-name "I" str-n "-TO-N" str-n))) + (list + + `(defconst ,2^XY + (expt 2 ,n)) + + ;; All the following functions are kept enabled. + + `(define ,nXYp (x) + ;; XY-bit natural number recognizer + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + (unsigned-byte-p ,n x)) + + `(define ,nXY ((x integerp)) + ;; Truncate input to an XY-bit natural number + ;; This function can be used to convert, say a 32-bit integer + ;; to a 32-bit natural number. + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + (mbe :logic (loghead ,n x) + :exec (logand ,(1- (expt 2 n)) x))) + + `(define ,iXYp (x) + ;; XY-bit integer recognizer + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + (signed-byte-p ,n x)) + + `(define ,iXY ((x integerp)) + ;; Truncate input to an XY-bit signed integer number + ;; This function can be used to convert, say a 32-bit natural number + ;; to a 32-bit integer. + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + (mbe :logic (logext ,n x) + :exec (bitops::fast-logext ,n x))) + + `(define ,ntoi ((x ,nXYp :type (unsigned-byte ,n))) + ;; Convert natural number to integer + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + :guard-hints (("Goal" + :in-theory + (enable logext-when-unsigned-byte-p-and-sign-changes))) + (mbe :logic (logext ,n x) + :exec (if (< x ,(expt 2 (1- n))) + x + (- x ,(expt 2 n))))) + + `(define ,iton ((x ,iXYp :type (signed-byte ,n))) + ;; Convert integer to natural number + :inline t + :no-function t + :enabled t + :parents (constants-conversions-and-bounds) + :guard-hints (("Goal" + :in-theory + (enable loghead-when-signed-byte-p-and-sign-changes))) + (mbe :logic (loghead ,n x) + :exec (if (>= x 0) + x + (+ x ,(expt 2 n))))) + + `(add-to-ruleset nwp-defs '(,nXYp)) + `(add-to-ruleset nw-defs '(,nXY)) + `(add-to-ruleset iwp-defs '(,iXYp)) + `(add-to-ruleset iw-defs '(,iXY)) + `(add-to-ruleset nw-to-iw-defs '(,ntoi)) + `(add-to-ruleset iw-to-nw-defs '(,iton)) + ))) + +(define np-defs (lst) + :mode :program + :guard (acl2::pos-listp lst) + :parents (constants-conversions-and-bounds) + (if (atom lst) nil + (append (np-def-n (car lst)) + (np-defs (cdr lst))))) + +(defmacro defuns-np (&rest lst) + (cons 'progn (np-defs lst))) + +(defuns-np 1 2 3 4 5 6 8 9 11 12 16 17 18 20 21 22 24 25 26 27 28 + 30 32 33 35 43 44 45 47 48 49 51 52 55 59 60 64 65 80 112 120 128 256 512) + +(defmacro n-size (n x) + ;; We prefer using n-size in functions that generate functions. E.g., + ;; see gpr-add-spec-gen-fn in machine/instructions/add-spec.lisp. + (let* ((fn-name (mk-name "N" + (symbol-name (if (< n 10) + (acl2::packn (list 0 n)) + (acl2::packn (list n))))))) + `(,fn-name ,x))) + +(defmacro ntoi (n x) + ;; We prefer using ntoi in functions that generate functions. E.g., + ;; see idiv-spec-gen in machine/instructions/divide-spec.lisp. + (let* ((val (symbol-name (if (< n 10) + (acl2::packn (list 0 n)) + (acl2::packn (list n))))) + (fn-name (mk-name "N" val "-TO-I" val))) + `(,fn-name ,x))) + +(define trunc + ;; We prefer using trunc in function definitions. + ((n :type (integer 0 *)) ; n is in bytes + (x :type integer)) + :inline t + :no-function t + :enabled t + (case n + (1 (n08 x)) + (2 (n16 x)) + (4 (n32 x)) + (8 (n64 x)) + (16 (n128 x)) + (t (part-select x :low 0 :width (* 8 n))))) + +;; ---------------------------------------------------------------------- + +;; Handy utility for turning a positional list into an array + +(defun list-to-alist (x i acc) + (declare (xargs :guard (and (true-listp x) + (natp i) + (alistp acc)))) + (cond ((endp x) (reverse acc)) + (t (list-to-alist (cdr x) + (1+ i) + (acons i (car x) acc))))) + +(defthm alistp-of-list-to-alist + (implies (alistp acc) + (alistp (list-to-alist x i acc)))) + +(defthm bounded-integer-alistp-monotone + (implies (and (bounded-integer-alistp x i) + (natp i) + (natp j) + (<= i j)) + (bounded-integer-alistp x j))) + +(defthm bounded-integer-alistp-revappend + (implies (true-listp x) + (equal (bounded-integer-alistp (revappend x y) i) + (and (bounded-integer-alistp x i) + (bounded-integer-alistp y i)))) + :hints (("Goal" :induct (revappend x y)))) + +(defthm bounded-integer-alistp-list-to-alist + (implies (and (natp i) + (bounded-integer-alistp acc i) + (equal k (+ i (len x)))) + (bounded-integer-alistp (list-to-alist x i acc) + k))) + +(defun list-to-array (name x) + (declare (xargs :guard (and (symbolp name) + (true-listp x) + x + (< (length x) + (acl2::array-maximum-length-bound))))) + (let ((alist (list-to-alist x 0 nil)) + (len (length x))) + (compress1 name + `((:header :dimensions (,len) + :maximum-length ,(1+ len) + :default x + :name ,name) + ,@alist)))) + +;; ---------------------------------------------------------------------- + +;; Maps a list of integers to a corresponding list of Booleans, +;; treating 0 as false. Example: (ints-to-booleans '(0 1 0 0 1)) ==> +;; (nil t nil nil t). + +(defun ints-to-booleans-acc (x acc) + (declare (xargs :guard (and (integer-listp x) + (true-listp acc)))) + (cond ((endp x) (reverse acc)) + (t (ints-to-booleans-acc (cdr x) + (cons (not (zerop (car x))) + acc))))) + +(defun ints-to-booleans (x) + (declare (xargs :guard (integer-listp x))) + (ints-to-booleans-acc x nil)) + +;; ---------------------------------------------------------------------- + +(defsection globally-disabled-events + :parents (utilities) + + :short "A ruleset containing all the events supposed to be mostly + globally disabled in our books" + + :long "The macro @('globally-disable') adds its argument --- + either a ruleset or an event --- to the ruleset called + @('globally-disabled-events'), and then disables + @('globally-disabled-events').
+ +Use the following form to see the events in this ruleset:
+ +
+\(show-globally-disabled-events-ruleset\)
+
+
+The idea behind having a @('globally-disabled-events') ruleset is +to provide some indication to the user of these books which events are +supposed to be mostly disabled throughout the books. This ruleset does +NOT reflect the enabled status of these events at any point, i.e., we +do not guarantee that an event @('FOO') in this ruleset will be +disabled everywhere in these books.
+ +If you need to enable some events in @('globally-disabled-events') +during book/proof development, it is recommended to do so locally.
+ +Use the following form to see the current status (enabled or +disabled) of the events in the @('globally-disabled-events') +ruleset:
+ +
+\(show-globally-disabled-events-status\)
+
+
+@('show-globally-disabled-events-status') simply calls @(see +disabledp) recursively on the events in +@('globally-disabled-events').
" + + (def-ruleset globally-disabled-events nil) + + (define globally-disable-fn + ((events "Can be a symbol (another ruleset) + or a @('true-listp') (set of events)")) + (b* ((events (if (true-listp events) + `(quote ,events) + events))) + `(progn + ;; add-to-ruleset will catch any invalid events, e.g., + ;; those that don't exist. + (add-to-ruleset globally-disabled-events ,events) + (in-theory (disable* globally-disabled-events))))) + + (defmacro globally-disable (events) + `(make-event (globally-disable-fn ,events))) + + (define show-globally-disabled-events-fn + ((state)) + :mode :program + (let ((world (w state))) + (ruleset-theory 'globally-disabled-events))) + + (defmacro show-globally-disabled-events-ruleset () + `(show-globally-disabled-events-fn state)) + + (defun disabledp-lst (lst count state) + (declare (xargs :stobjs (state) + :mode :program)) + (if (endp lst) + (mv (cw "~%~%Number of events in GLOBALLY-DISABLED-EVENTS: ~x0~%~%" + count) + :invisible + state) + + (b* ((- (cw "~%-- ~x0 ~%~t1 ~x2~%" + (car lst) + 1 + (or (disabledp (car lst)) + :ENABLED)))) + (disabledp-lst (cdr lst) (1+ count) state)))) + + (defmacro show-globally-disabled-events-status () + `(disabledp-lst (show-globally-disabled-events-ruleset) 0 state)) + + (globally-disable '(logior logand logxor floor mod ash))) diff --git a/crates/clazel-x64/tests/data/intel-sdm/LICENSE.md b/crates/clazel-x64/tests/data/intel-sdm/LICENSE.md new file mode 100644 index 000000000..9d8c34732 --- /dev/null +++ b/crates/clazel-x64/tests/data/intel-sdm/LICENSE.md @@ -0,0 +1,10 @@ +# Licence of the Intel SDM + +None granted for redistribution. The Intel 64 and IA-32 Architectures +Software Developer's Manual is Copyright Intel Corporation. Intel's Terms of +Use (https://www.intel.com/content/www/us/en/legal/terms-of-use.html, read on +2026-11-16) say its documents may be used for personal, non-commercial +information and may not be copied or posted. The PDF is therefore not +vendored here: `SOURCE.md` pins it by URL, size and SHA-256. + +This file is a statement of that decision, not a copy of a licence. diff --git a/crates/clazel-x64/tests/data/intel-sdm/SOURCE.md b/crates/clazel-x64/tests/data/intel-sdm/SOURCE.md new file mode 100644 index 000000000..dbad1f0d4 --- /dev/null +++ b/crates/clazel-x64/tests/data/intel-sdm/SOURCE.md @@ -0,0 +1,31 @@ +# Intel 64 and IA-32 Architectures Software Developer's Manual, volume 2 (A-Z), pinned and not vendored + +Upstream: https://cdrdv2-public.intel.com/929352/325383-093-sdm-vol-2abcd.pdf +SHA-256: d137a7887787bbb06c1a8b8bafd51958d8d2e16b4dff987a03226c845cca1516 325383-093-sdm-vol-2abcd.pdf +Size: 11294447 325383-093-sdm-vol-2abcd.pdf +License: Intel copyright, no licence to redistribute. See `LICENSE.md`. + +Version 093 of "Combined Volumes 2A, 2B, 2C and 2D: Instruction Set Reference, +A-Z" (order number 325383), the latest on Intel's page +https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html +on 2026-11-16 (the link there, https://cdrdv2.intel.com/v1/dl/getContent/671110, +redirects to the URL above). The encoding tables of jit.md item J.3 are +Appendix A (the opcode maps) and the "Instruction Operand Encoding" tables of +each instruction page; the operations are the pseudocode of each page. By the +user's decision of 2026-11-16 (jit.md, the note under J.3) this is the source +of the independent transcription, written by a different session from the +primary one. + +The PDF is not in this repository, because Intel's terms of use +(https://www.intel.com/content/www/us/en/legal/terms-of-use.html) say its +documents may not be copied or posted, and the PDF carries no open licence. So +the pin is the URL, the size and the SHA-256 above: a reader fetches the file +and compares. If Intel moves or replaces the file, the hash no longer matches +and the pin must be changed by a person, after reading the new edition's +change list. + +`xtask/src/testdata/isa.rs` writes `tests/generated/intel_sdm.rs`, the table +of this pin. Intel's XED encoder/decoder tables (Apache-2.0, +https://github.com/intelxed/xed) are a machine-readable form of the same +encoding data and are not pinned here; whether to pin them is a decision for a +person. diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENCE b/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENCE new file mode 100644 index 000000000..5300da8d0 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENCE @@ -0,0 +1,37 @@ +All files and directories are subject to the BSD two-clause licence +below, except the translated x86 model files in the `model/` +directory, which are subject to the terms of a BSD three-clause licence +and additionally the licence of the original ACL2 x86 model, from which +the Sail model is generated (see `model/LICENSE`). + +Copyright (c) 2020 Patrick Taylor +Copyright (c) 2022 Capabilities Limited + +This software was developed in part by SRI International, the University +of Cambridge Computer Laboratory (Department of Computer Science and +Technology), and Capabilities Limited under Defense Advanced Research +Projects Agency (DARPA) Contract No. HR001122C0110 ("ETC"). + +Portions of this software were developed by Thomas Bauereiss under +contract to Capabilities Limited. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. 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. + +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. diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENSE.md b/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENSE.md new file mode 100644 index 000000000..538667177 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/LICENSE.md @@ -0,0 +1,81 @@ +The files in this directory and its subdirectories are distributed under the +terms of the following two BSD three-clause licences, where the latter is the +licence of the original ACL2 x86 model from which this Sail model is derived. + +Copyright (c) 2020 Patrick Taylor +Copyright (c) 2022 Capabilities Limited + +This software was developed in part by SRI International, the University +of Cambridge Computer Laboratory (Department of Computer Science and +Technology), and Capabilities Limited under Defense Advanced Research +Projects Agency (DARPA) Contract No. HR001122C0110 ("ETC"). + +Portions of this software were developed by Thomas Bauereiss under +contract to Capabilities Limited. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. 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. + +3. Neither the name of the copyright holder 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. + +[Note: The license below is based on the template found July 25, 2014 + at: http://opensource.org/licenses/BSD-3-Clause. Except as + otherwise noted, it applies to all files distributed from + http://www.cs.utexas.edu/users/moore/acl2/current/, + as well as to all files distributed from + https://github.com/acl2/acl2 other than those in the + ACL2 Community Books (the books/ subdirectory). +When a file in the ACL2 Community Books refers to this LICENSE, that file's +specified copyright supersedes the copyright shown immediately below.] + +Copyright (c) 2022, Regents of the University of Texas +Copyright (C) 2022, Shilpi Goel +Copyright (C) 2022, Kestrel Technology, LLC +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 holder 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. diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/README.md b/crates/clazel-x64/tests/data/sail-x86-from-acl2/README.md new file mode 100644 index 000000000..737830c30 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/README.md @@ -0,0 +1,68 @@ +# ACL2-to-Sail translator and the resulting Sail x86 ISA model + +[ToC] + +## Introduction + +This project translates the [X86isa](https://www.cs.utexas.edu/~moore/acl2/v8-5/combined-manual/index.html?topic=ACL2____X86ISA) formal model of the x86 ISA, written in ACL2 by Shilpi Goel, Warren A. Hunt, Jr., and Matt Kaufmann, into [Sail](https://www.cl.cam.ac.uk/~pes20/sail/popl2019.html), a language designed for modelling ISA semantics. + +The translator was originally developed by Patrick Taylor as a student project in 2020, with supervision by Alasdair Armstrong and Peter Sewell. Since then, Thomas Bauereiss has updated and extended it, in particular extending the scope of the translation, making the data type annotations more precise, and making the generated Sail code more idiomatic. + +This README provides a quick start guide for getting the translator working, before detailing the implementation. + +Files and folders: + +* `model/` contains a snapshot of the Sail model generated by the translator. +* `translator/` contains the translator code as well as validation scripts in `translator/validation/`. The `Readme.md` file in the latter directory describes how to use the translated model. +* `doc/` contains documentation of the original prototype of the translator, in particular Patrick's dissertation in `doc/diss/latex/diss.pdf`. Much of this still applies to the current version of the translator, but some aspects have changed; for example, the type inference now generates fixed-width bitvector type annotations where possible, rather than using unbounded integers. + +## Quick Start Guide - Running the Translator + +These first steps give us a current version of the ACL2 x86 model. The certification steps are necessary because the translator must be able to call a running instance of ACL2 with the X86ISA project loaded in order to perform macro expansion. + +1. Download and install ACL2 and certify the community books as detailed here:In 64-bit mode, a linear address is considered to be in + canonical form if address bits 63 through to the most-significant implemented + bit by the microarchitecture ( represented by the constant + @ ( ' *max-linear-address-size* ' ) in these books ) are set to either all ones or + all zeros.
*/ +function canonical_address_p lin_addr = { + signed_byte_p(48, lin_addr) +} + +val rvm08 : sbits(48) -> bits(8) + +function rvm08 addr = { + if canonical_address_p(signed(addr)) then { + memi(sail_zero_extend(addr, 64)) + } else { + x86_model_error("rvm08") + } +} + +val wvm08 : (sbits(48), bits(8)) -> unit + +function wvm08 (addr, sailval) = { + if canonical_address_p(signed(addr)) then { + bang_memi(sail_zero_extend(addr, 64), sailval) + } else { + x86_model_error("wvm08") + } +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/arith_and_logic.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/arith_and_logic.sail new file mode 100644 index 000000000..7517e70c4 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/arith_and_logic.sail @@ -0,0 +1,282 @@ +$include "./arith_and_logic_spec.sail" +$include "./decoding_and_spec_utils.sail" + +/*! Operand Fetch and Execute for ADD, ADC, SUB, SBB, OR, AND, + XOR, CMP, TEST: Addressing Mode = ( E, G ) +where @ ( ' E ' ) is the destination operand and @ ( ' G ' ) is the source + operand. Note that @ ( ' E ' ) stands for a general-purpose register or + memory operand specified by the @ ( ' ModRM.r/m ' ) field, and @ ( ' G ' ) + stands for a general-purpose register specified by the + @ ( ' ModRM.reg ' ) field.
+ + [OP R/M, REG] Flags Affectedwhere @ ( ' G ' ) is the destination operand and @ ( ' E ' ) is the source + operand. Note that @ ( ' G ' ) stands for a general-purpose register + specified by the @ ( ' ModRM.reg ' ) field, and @ ( ' E ' ) stands for a + general-purpose register or memory operand specified by the + @ ( ' ModRM.r/m ' ) field.
+ + [OP REG, R/M] Flags Affectedwhere @ ( ' E ' ) is the destination operand and @ ( ' I ' ) is the source + operand. Note that @ ( ' E ' ) stands for a general-purpose register or + memory operand specified by the @ ( ' ModRM.r/m ' ) field, and @ ( ' I ' ) + stands for immediate data. All opcodes except those of TEST fall + under Group 1, and have opcode extensions ( ModR/M.reg field ) , as + per Table A-6 of the Intel Manuals, Vol. 2. The opcodes for TEST + fall under Unary Group 3, and also have opcode extensions.
+ + [OP R/M, IMM] Flags Affectedwhere @ ( ' rAX ' ) is the destination operand and @ ( ' I ' ) is the + source operand. Note that @ ( ' rAX ' ) stands for AL/AX/EAX/RAX, + depending on the operand size, and @ ( ' I ' ) stands for immediate + data.
+ + [OP rAX, IMM] Flags AffectedThese instructions are:
++ In 64-bit mode, a 64-bit instruction pointer is read from the full RIP. + Since, in the model, this is a 48-bit signed integer, + this function returns a 48-bit signed integer. +
++ In 32-bit mode, a 32-bit or 16-bit instruction pointer is read from + EIP ( i.e. the low 32 bits of RIP ) + or IP ( i.e. the low 16 bits of RIP ) , + based on the CS.D bit, + i.e. the D bit of the current code segment descriptor. + Either way, this function returns an unsigned 32-bit or 16-bit integer, + which is also a signed 48-bit integer. +
++ See AMD manual, Oct ' 13, Vol. 1, Sec. 2.2.4 and Sec. 2.5. + AMD manual, Apr ' 16, Vol. 2, Sec 4.7.2., + and Intel manual, Mar ' 17, Vol. 1, Sec. 3.6. +
++ In 32-bit mode, the address-size override prefix ( if present ) + should not affect the instruction pointer size. + It does not seem to make sense + to change the instruction pointer size on a per-instruction basis. +
*/ +val read_iptr : proc_mode -> sbits(48) + +function read_iptr proc_mode = { + let iptr : sbits(48) = read_rip(); + if in_64bit_mode(proc_mode) then { + iptr + } else if in_compatibility_mode(proc_mode) then { + let cs_attr : bits(16) = seg_hidden_attrs[1]; + let cs_d : bits(1) = Mk_code_segment_descriptor_attributesbits(cs_attr)[d]; + if cs_d == 0b1 then { + sail_zero_extend(truncate(iptr, 32), 48) + } else { + sail_zero_extend(sail_zero_extend(truncate(iptr, 16), 32), 48) + } + } else 0x000000000000 +} + +/*! Add a specified amount to an instruction pointer. ++ The amount may be positive ( increment ) or negative ( decrement ) . + This just calculates the new instruction pointer value, + without storing it into the register RIP, EIP, or IP. + The starting value is the result of @ ( tsee read-*ip ) + or a previous invocation of @ ( tsee add-to-*ip ) . +
++ In 64-bit mode, we check whether the result is a canonical address; + in 32-bit mode, we check whether the result is within the segment limit. + If these checks are not satisfied, + this function returns an error flag ( and 0 as incremented address ) , + which causes the x86 model to stop execution with an error. + It is not clear whether these checks should be performed + when the instruction pointer is incremented + or when an instruction byte is eventually accessed; + the Intel and AMD manuals seem unclear in this respect. + But since the failure of these checks stops execution with an error, + and it is in a way always ` ` safe ' ' to stop execution with an error + ( in the sense that the model provides no guarantees when this happens ) , + for now we choose to perform these checks here. +
++ Note that a code segment is never expand-down, + so the valid effective addresses are always between 0 and the segment limit + ( cf. @ ( tsee segment-base-and-bounds ) ) . +
*/ +val add_to_iptr : (proc_mode, sbits(48), sbits(48)) -> sbits(48) + +function add_to_iptr (proc_mode, iptr, delta) = { + let iptr_plus_delta : sbits(49) = bits_of_int(signed(iptr) + signed(delta), 49); + if in_64bit_mode(proc_mode) then { + if canonical_address_p(signed(iptr_plus_delta)) then { + truncate(iptr_plus_delta, 48) + } else { + x86_model_error(":NON-CANONICAL-INSTRUCTION-POINTER") + } + } else if in_compatibility_mode(proc_mode) then { + let cs_limit : bits(32) = loghead(32, seg_hidden_limits[1]); + if 0 <= signed(iptr_plus_delta) & signed(iptr_plus_delta) <= unsigned(cs_limit) then { + truncate(sail_zero_extend(truncate(iptr_plus_delta, 32), 49), 48) + } else { + x86_model_error(":OUT-OF-SEGMENT-INSTRUCTION-POINTER") + } + } else { + x86_model_error(":UNIMPLEMENTED-PROC-MODE") + } +} + +/*! Write an instruction pointer into the register RIP, EIP, or IP. ++ In 64-bit mode, a 64-bit instruction pointer is written into the full RIP. + Since, in the model, this is a 48-bit signed integer, + this function consumes a 48-bit signed integer. +
++ In 32-bit mode, the instruction pointer is 32 or 16 bits + based on the CS.D bit, i.e. the D bit of the current code segment descriptor. + In these cases, the argument to this function should be + a 32-bit or 16-bit unsigned integer, which is also a 48-bit signed integer. +
++ See AMD manual, Oct ' 13, Vol. 1, Sec. 2.2.4 and Sec. 2.5. + AMD manual, Apr ' 16, Vol. 2, Sec 4.7.2., + and Intel manual, Mar ' 17, Vol. 1, Sec. 3.6. +
++ According to Intel manual, Mar ' 17, Vol. 1, Table 3-1, + it seems that + when writing a 32-bit instruction pointer ( EIP ) + the high 32 bits of RIP should be set to 0, + and when writing a 16-bit instruction pointer ( IP ) + the high 48 bits of RIP should be left unmodified; + since in our model the RIP is 48 bits, + the above applies to the high 16 and 32 bits, respectively. + The pseudocode for the JMP instruction in Intel manual, Mar ' 17, Vol. 2 + shows an assignment @ ( ' EIP <- tempEIP AND 0000FFFFh ' ) for the 16-bit case, + which seems to imply that + the high 32 ( or 16, in our model ) bits are left unmodified + and the high 16 bits of EIP are set to 0, + which would contradict Table 3-1; + the pseudocode for some other instructions + that directly write the instruction pointer ( e.g. RET and Jcc ) + show similar assignments. + However, it is possible that this assignment has a typo and should be + @ ( ' IP <- tempEIP AND 0000FFFFh ' ) instead, + which would be consistent with Table 3-1. + But we also note that the pseudocode for the JMP instruction + shows an assignment @ ( ' EIP <- tempEIP ' ) for the 32-bit case, + which seems to imply that + the high 32 ( or 16, in our model ) bits are left unmodified, + which would contradict Table 3-1. + The AMD manuals do not show pseudocode for these instructions, + and AMD manual, Oct ' 13, Vol. 1, Fig. 2-10 + ( which is somewhat analogous to Intel ' s Table 3-1 ) + shows the high bits simply grayed out; + so the AMD manuals do not provide disambiguation help. + It is also possible that Table 3-1 has a typo and should say + that a 16-bit instruction pointer is zero-extended, + but that is not quite consistent with the pseudocode assignments to EIP, + which seem to imply that the high bits are untouched. + Table 3-1 is under a section titled + ` Address Calculation in 64-Bit Mode ' , + which may suggest that the table may not apply to 32-bit mode, + but then it is not clear how it would just apply to 64-bit mode. + For now, we decide to have this function follow Intel ' s Table 3-1, + but we may revise that if we manage to resolve these ambiguities. + We also note that Intel ' s Table 3-1 is consistent with the way in which + 32-bit and 16-bit values are written to general-purpose registers + ( even though RIP/EIP/IP is not a general-purpose register ) ; + see @ ( tsee wr32 ) and @ ( tsee wr16 ) . +
++ This function should be always called + with an instruction pointer of the right type + ( 48-bit signed, 32-bit unsigned, or 16-bit unsigned ) + based on the mode and code segment. + We may add a guard to ensure that in the future, + but for now in the code below + we coerce the instruction pointer to 32 and 16 bits as appropriate, + to verify guards; + these coercions are expected not to change + the argument instruction pointer. +
*/ +val write_iptr : (proc_mode, sbits(48)) -> unit + +function write_iptr (proc_mode, iptr) = { + if in_64bit_mode(proc_mode) then { + write_rip(iptr) + } else if in_compatibility_mode(proc_mode) then { + let cs_attr : bits(16) = seg_hidden_attrs[1]; + let cs_d : bits(1) = Mk_code_segment_descriptor_attributesbits(cs_attr)[d]; + if cs_d == 0b1 then { + write_rip(sail_zero_extend(truncate(iptr, 32), 48)) + } else { + let rip_var : sbits(48) = read_rip(); + let rip_new : sbits(48) = changeSlice(rip_var, 0, 16, truncate(iptr, 16)); + write_rip(rip_new) + } + } else () +} + +/*! Operations to manipulate stack pointers. +Read the stack pointer from the register RSP, ESP, or SP. ++ In 64-bit mode, a 64-bit stack pointer is read from the full RSP. + Since, in the model, this is a 64-bit signed integer, + this function returns a 64-bit signed integer. +
++ In 32-bit mode, a 32-bit or 16-bit stack pointer is read from + ESP ( i.e. the low 32 bits of RSP ) + or SP ( i.e. the low 16 bits of RSP ) , + based on the SS.B bit, + i.e. the B bit of the current stack segment register. + Either way, this function returns an unsigned 32-bit or 16-bit integer, + which is also a signed 64-bit integer. +
++ See Intel manual, Mar ' 17, Vol. 1, Sec. 6.2.3 and Sec. 6.2.5, + and AMD manual, Apr ' 16, Vol. 2, Sec 2.4.5 and Sec. 4.7.3. + The actual size of the value returned by this function + is @ ( ' StackAddrSize ' ) , + introduced in Intel manual, Mar ' 17, Vol. 2, Sec. 3.1.1.9. +
++ In 32-bit mode, the address-size override prefix ( if present ) + should not affect the stack address size. + It does not seem to make sense + to change the stack address size on a per-instruction basis. +
*/ +val read_sptr : proc_mode -> sbits(64) + +function read_sptr proc_mode = { + let sptr : sbits(64) = rgfi(4); + if in_64bit_mode(proc_mode) then { + sptr + } else if in_compatibility_mode(proc_mode) then { + let ss_attr : bits(16) = seg_hidden_attrs[2]; + let ss_b : bits(1) = Mk_data_segment_descriptor_attributesbits(ss_attr)[d_b]; + if ss_b == 0b1 then { + sail_zero_extend(truncate(sptr, 32), 64) + } else { + sail_zero_extend(sail_zero_extend(truncate(sptr, 16), 32), 64) + } + } else 0x0000000000000000 +} + +/*! Add a specified amount to a stack pointer. ++ The amount may be positive ( increment ) or negative ( decrement ) . + This just calculates the new stack pointer value, + without storing it into the register RSP, ESP, or SP. + The starting value is the result of @ ( tsee read-*sp ) + or a previous invocation of @ ( tsee add-to-*sp ) . +
++ The increment or decrement is modular: + 64 bits in 64-bit mode, + and either 32 or 16 bits in 32-bit mode ( depending on the SS.B bit ) . + Since our model uses signed 64-bit addresses, we use @ ( tsee i64 ) for them, + while we use @ ( tsee n32 ) or @ ( tsee n16 ) for 32-bit and 16-bit addresses. +
++ In 64-bit mode, we check whether the result is a canonical address; + in 32-bit mode, we check whether the result is within the segment limit. + If these checks are not satisfied, + this function returns an error flag ( and 0 as new pointer ) , + which causes the x86 model to stop execution with an error. + It is not clear whether these checks should be performed + when the stack pointer is updated, + or when the stack is eventually accessed through the updated pointer; + the Intel and AMD manuals seem unclear in this respect. + But since the failure of these checks stops execution with an error, + and it is in a way always ` ` safe ' ' to stop execution with an error + ( in the sense that the model provides no guarantees when this happens ) , + for now we choose to perform these checks here. +
++ Note that a stack segment may be expand-down or expand-up + ( see Intel manual, Mar ' 17, Vol. 3, Sec. 3.4.5.1 ) , + so the checks need to cover these two cases. + See @ ( tsee segment-base-and-bounds ) and @ ( tsee ea-to-la ) . +
*/ +val add_to_sptr : (proc_mode, sbits(64), sbits(64)) -> sbits(64) + +function add_to_sptr (proc_mode, sptr, delta) = { + let sptr_plus_delta : sbits(65) = bits_of_int(signed(sptr) + signed(delta), 65); + if in_64bit_mode(proc_mode) then { + let sptr_plus_delta : sbits(64) = truncate(sptr_plus_delta, 64); + if canonical_address_p(signed(sptr_plus_delta)) then { + sptr_plus_delta + } else { + x86_model_error(":NON-CANONICAL-STACK-ADDRESS") + } + } else if in_compatibility_mode(proc_mode) then { + let ss_limit : bits(32) = seg_hidden_limits[2]; + let ss_attr : bits(16) = seg_hidden_attrs[2]; + let ss_b : bits(1) = Mk_data_segment_descriptor_attributesbits(ss_attr)[d_b]; + let ss_e : bits(1) = Mk_data_segment_descriptor_attributesbits(ss_attr)[e]; + let ss_lower : int = { + if ss_e == 0b1 then { + 1 + unsigned(ss_limit) + } else 0 + }; + let ss_upper : bits(32) = { + if ss_e == 0b1 then { + if ss_b == 0b1 then 0xffffffff else 0x0000ffff + } else { + ss_limit + } + }; + let sptr_plus_delta : bits(32) = { + if ss_b == 0b1 then { + truncate(sptr_plus_delta, 32) + } else { + sail_zero_extend(truncate(sptr_plus_delta, 16), 32) + } + }; + if not_bool(ss_lower <= unsigned(sptr_plus_delta) & unsigned(sptr_plus_delta) <= unsigned(ss_upper)) then x86_model_error(":OUT-OF-SEGMENT-STACK-ADDRESS"); + sail_zero_extend(sptr_plus_delta, 64) + } else { + x86_model_error(":UNIMPLEMENTED-PROC-MODE") + } +} + +/*! Write a stack pointer into the register RSP, ESP, or SP. ++ In 64-bit mode, a 64-bit stack pointer is written into the full RSP. + Since, in the model, this is a 64-bit signed integer, + this function consumes a 64-bit signed integer. +
++ In 32-bit mode, the stack pointer is 32 or 16 bits based on the SS.B bit, + i.e. the B bit of the current stack segment descriptor. + In these cases, the argument to this function should be + a 32-bit or 16-bit unsigned integer, which is also a 64-bit signed integer. +
++ See Intel manual, Mar ' 17, Vol. 1, Sec. 6.2.3 and Sec. 6.2.5, + and AMD manual, Apr ' 16, Vol. 2, Sec 2.4.5 and Sec. 4.7.3. + The actual size of the value consumed by this function + should be @ ( ' StackAddrSize ' ) , + introduced in Intel manual, Mar ' 17, Vol. 2, Sec. 3.1.1.9. +
++ The pseudocode of stack instructions like PUSH + in Intel manual, Mar ' 17, Vol. 2 + show assignments of the form + @ ( ' RSP <- ... ' ) , @ ( ' ESP <- ... ' ) , and @ ( ' SP <- ... ' ) + based on the stack address size. + This may suggests that + when the stack address size is 32 + the assignment to ESP leaves the high 32 bits of RSP unchanged, + and when the stack address size is 16 + the assignment to SP leaves the high 48 bits of RSP unchanged. + However, + as explained in the documentation of @ ( tsee wr32 ) and @ ( tsee wr16 ) , + normally writing to the low 32 bits of a general-purpose register + ( which RSP/ESP/SP is ) zeros the high 32 bits, + while writing the low 16 bits leaves the high 48 bits unchanged. + Thus, we follow this requirement also when writing RSP/ESP/SP implicitly, + via stack manipulation instructions like PUSH that use + this @ ( tsee write-*sp ) function to update the stack pointer register. +
++ This function should be always called + with a stack pointer of the right type + ( 64-bit signed, 32-bit unsigned, or 16-bit unsigned ) + based on the stack address size. + We may add a guard to ensure that in the future, + but for now in the code below + we coerce the stack pointer to 32 and 16 bits as appropriate, + to verify guards; + these coercions are expected not to change the argument stack pointer. +
*/ +val write_sptr : (proc_mode, sbits(64)) -> unit + +function write_sptr (proc_mode, sptr) = { + if in_64bit_mode(proc_mode) then { + write_rgfi(4, sptr) + } else if in_compatibility_mode(proc_mode) then { + let ss_attr : bits(16) = seg_hidden_attrs[2]; + let ss_b : bits(1) = Mk_data_segment_descriptor_attributesbits(ss_attr)[d_b]; + if ss_b == 0b1 then { + write_rgfi(4, sail_zero_extend(truncate(sptr, 32), 64)) + } else { + let rsp_var : sbits(64) = rgfi(4); + let rsp_new : sbits(64) = changeSlice(rsp_var, 0, 16, truncate(sptr, 16)); + write_rgfi(4, rsp_new) + } + } else () +} + +/*! Computing effective address using ModR/M, SIB bytes, and + displacement bytes present in the instruction +Calculates effective address when SIB is present. +Source: Intel Vol. 2A, Table 2-3.
+Also see Intel Vol. 2A, Table 2-2 and Figure 2-6.
+In 64-bit mode, + we use @ ( ' rgfi ' ) to read bases as signed linear addresses, + which encode canonical linear addresses, + which are also effective addresses in 64-bit mode. + In 32-bit mode, + we use @ ( ' rr32 ' ) to read bases as unsigned effective addresses.
+In 64-bit mode, + we use @ ( ' rgfi ' ) to read indices as signed 64-bit values. + In 32-bit mode, + we limit them to signed 32-bit values.
+Note that, in 32-bit mode, + we call this function only when the address size is 32 bits. + When the address size is 16 bits, there is no SIB byte: + See Intel Vol. 2 Table 2-1.
+The displacement is read as a signed values: + see AMD manual, Dec ' 17, Volume 3, Section 1.5.
*/ +val x86_effective_addr_from_sib : (proc_mode, sbits(48), bits(8), bits(2), sib) -> (int, sbits(64), {|0, 1, 4|}) + +function x86_effective_addr_from_sib (proc_mode, temp_rip, rex_byte, mod_var, sib) = { + let b : bits(3) = sib[base]; + let check_alignment? : bool = false; + let (base, displacement, nrip_bytes) : (sbits(64), sbits(64), {|0, 1, 4|}) = { + match mod_var { + 0 => { + if b == 0b101 then { + let dword : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 4, sail_sign_extend(temp_rip, 64), None(), 1, ":X", check_alignment?, false); + (0x0000000000000000, dword, 4) + } else { + (if in_64bit_mode(proc_mode) then { + rgfi(unsigned(reg_index(b, rex_byte, 0b00))) + } else { + sail_zero_extend(rr32(sail_zero_extend(b, 4)), 64) + }, 0x0000000000000000, 0) + } + }, + 1 => { + let byte : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 1, sail_sign_extend(temp_rip, 64), None(), 1, ":X", check_alignment?, false); + (if in_64bit_mode(proc_mode) then { + rgfi(unsigned(reg_index(b, rex_byte, 0b00))) + } else { + sail_zero_extend(rr32(sail_zero_extend(b, 4)), 64) + }, byte, 1) + }, + 2 => { + let dword : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 4, sail_sign_extend(temp_rip, 64), None(), 1, ":X", check_alignment?, false); + (if in_64bit_mode(proc_mode) then { + rgfi(unsigned(reg_index(b, rex_byte, 0b00))) + } else { + sail_zero_extend(rr32(sail_zero_extend(b, 4)), 64) + }, dword, 4) + }, + _ => x86_model_error("mod-can-not-be-anything-other-than-0-1-or-2") + } + }; + let ix : bits(4) = reg_index(sib[index], rex_byte, 0b01); + let index : sbits(64) = { + match ix { + 4 => 0x0000000000000000, + _ => { + if in_64bit_mode(proc_mode) then { + rgfi(unsigned(ix)) + } else { + sail_sign_extend(truncate(rgfi(unsigned(ix)), 32), 64) + } + } + } + }; + let scale : bits(2) = sib[scale]; + let scaled_index : int = ash(signed(index), unsigned(scale)); + let effective_addr : int = signed(base) + scaled_index; + (effective_addr, displacement, nrip_bytes) +} + +/*! Calculate the displacement for + 16-bit effective address calculation. ++ This is according to Intel manual, Mar ' 17, Vol. 2, Table 2-1. +
++ The displacement is absent ( i.e. 0 ) when Mod is 00b. + An exception to this is when R/M is 110b, + in which case there is a 16-bit displacement that is added to the index. + This case is not handled by this function, + but is instead handled in + its caller function @ ( tsee x86-effective-addr-16 ) . +
++ The displacement is a signed 8-bit value when Mod is 01b. + The displacement is a signed 16-bit value when Mod is 10b. + This function is not called when Mod is 11b. +
++ If an error occurs when trying to read the displacement, + 0 is returned as displacement, + but the caller ignores the returned displacement given the error. +
++ This function is called only when the address size is 16 bits. +
*/ +val x86_effective_addr_16_disp : (proc_mode, sbits(48), bits(2)) -> (sbits(64), {|0, 1, 2|}) + +function x86_effective_addr_16_disp (proc_mode, temp_rip, mod_var) = { + (match mod_var { + 0 => (0x0000000000000000, 0), + 1 => { + let byte : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 1, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + (byte, 1) + }, + 2 => { + let word : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 2, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + (word, 2) + }, + _ => x86_model_error("mod-value-wrong") + }) : (sbits(64), {|0, 1, 2|}) +} + +/*! Effective address calculation with 16-bit addressing. ++ This is according to Intel manual, Mar ' 17, Vol. 2, Table 2-1. +
++ We assume that the additions in the table are modular, + even though the documentation is not clear in that respect. + So we simply apply @ ( ' n16 ' ) to the exact integer result. + This is in analogy to the use of @ ( ' n32 ' ) + for effective address calculation in 64-bit mode + when there is an address size override prefix: + see @ ( tsee x86-effective-addr-32/64 ) . +
*/ +val x86_effective_addr_16 : (proc_mode, sbits(48), bits(3), bits(2)) -> (bits(16), {|0, 1, 2|}) + +function x86_effective_addr_16 (proc_mode, temp_rip, r_m, mod_var) = { + (match r_m { + 0 => { + let bx : bits(16) = rr16(0x3); + let si : bits(16) = rr16(0x6); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bx) + (unsigned(si) + signed(disp)), 16), increment_rip_by) + }, + 1 => { + let bx : bits(16) = rr16(0x3); + let di : bits(16) = rr16(0x7); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bx) + (unsigned(di) + signed(disp)), 16), increment_rip_by) + }, + 2 => { + let bp : bits(16) = rr16(0x5); + let si : bits(16) = rr16(0x6); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bp) + (unsigned(si) + signed(disp)), 16), increment_rip_by) + }, + 3 => { + let bp : bits(16) = rr16(0x5); + let di : bits(16) = rr16(0x7); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bp) + (unsigned(di) + signed(disp)), 16), increment_rip_by) + }, + 4 => { + let si : bits(16) = rr16(0x6); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(si) + signed(disp), 16), increment_rip_by) + }, + 5 => { + let di : bits(16) = rr16(0x7); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(di) + signed(disp), 16), increment_rip_by) + }, + 6 => { + (match mod_var { + 0 => { + let disp : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 2, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + (truncate(disp, 16), 2) + }, + _ => { + let bp : bits(16) = rr16(0x5); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bp) + signed(disp), 16), increment_rip_by) + } + }) : (bits(16), {|0, 1, 2|}) + }, + 7 => { + let bx : bits(16) = rr16(0x3); + let (disp, increment_rip_by) : (sbits(64), {|0, 1, 2|}) = x86_effective_addr_16_disp(proc_mode, temp_rip, mod_var); + (bits_of_int(unsigned(bx) + signed(disp), 16), increment_rip_by) + }, + _ => x86_model_error(":R/M-OUT-OF-RANGE") + }) : (bits(16), {|0, 1, 2|}) +} + +/*! Effective address calculation with 32-bit and 64-bit addressing. +Note that we do not add segment bases + ( such as the FS and GS bases, if FS and GS overrides are present ) + to the effective address computed in this function. + Addition of those segment base addresses is a part of the + segmentation process --- we handle that in the function @ ( see + ea-to-la ) that performs the segment address translation.
+ +Quoting from Intel Vol 1, Sec 3.3.7:
+ +In 64-bit mode, the effective address components are + added and the effective address is truncated ( See for example + the instruction LEA ) before adding the full 64-bit segment + base. The base is never truncated, regardless of addressing + mode in 64-bit mode.
+ +Quoting Intel Vol. 1 Sec. 3.3.7 ( Address Calculations in + 64-Bit Mode ) :
+ +All 16-bit and 32-bit address calculations are + zero-extended in IA-32e mode to form 64-bit addresses. Address + calculations are first truncated to the effective address size + of the current mode ( 64-bit mode or compatibility mode ) , as + overridden by any address-size prefix. The result is then + zero-extended to the full 64-bit address width. Because of + this, 16-bit and 32-bit applications running in compatibility + mode can access only the low 4 GBytes of the 64-bit mode + effective addresses. Likewise, a 32-bit address generated in + 64-bit mode can access only the low 4 GBytes of the 64-bit + mode effective addresses.
+ +Also: Intel Vol 1, Section 3.3.7 says that we need + sign-extended displacements in effective address calculations. In + Lisp, sign-extension is implicit.
+ +In 64-bit mode, instructions such as LEA use this function to + compute the effective address. LEA, at least, does not check + whether the generated address is canonical or not, which is why we + don ' t make the canonical-address-p check in this function.
+ +In 64-bit mode, + we use @ ( ' rgfi-size ' ) to read bases as signed linear addresses, + which encode canonical linear addresses, + which are also effective addresses in 64-bit mode. + In 32-bit mode, + we use @ ( ' rr32 ' ) to read bases as unsigned effective addresses.
*/ +val x86_effective_addr_32_64 : (proc_mode, bool, sbits(48), bits(8), bits(3), bits(2), sib, bits(3)) -> (sbits(64), {|0, 1, 4|}) + +function x86_effective_addr_32_64 (proc_mode, p4, temp_rip, rex_byte, r_m, mod_var, sib, num_imm_bytes) = { + let (addr, displacement, increment_rip_by) : (int, sbits(64), {|0, 1, 4|}) = { + match mod_var { + 0 => { + (match r_m { + 4 => x86_effective_addr_from_sib(proc_mode, temp_rip, rex_byte, mod_var, sib), + 5 => { + if in_64bit_mode(proc_mode) then { + let dword : sbits(64) = rime_size(Mode_64bit, select_address_size(proc_mode, None()), 4, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + let next_rip : sbits(48) = add_to_iptr(Mode_64bit, temp_rip, bits_of_int(4 + unsigned(num_imm_bytes), 48)); + (signed(next_rip), dword, 4) + } else { + let dword : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 4, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + (signed(0x000000000000), dword, 4) + } + }, + _ => (if in_64bit_mode(proc_mode) then { + signed(rgfi(unsigned(reg_index(r_m, rex_byte, 0b00)))) + } else { + signed(sail_zero_extend(rr32(sail_zero_extend(r_m, 4)), 64)) + }, 0x0000000000000000, 0) + }) : (int, sbits(64), {|0, 1, 4|}) + }, + 1 => { + (match r_m { + 4 => x86_effective_addr_from_sib(proc_mode, temp_rip, rex_byte, mod_var, sib), + _ => { + let byte2 : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 1, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + let reg : sbits(64) = { + if in_64bit_mode(proc_mode) then { + rgfi(unsigned(reg_index(r_m, rex_byte, 0b00))) + } else { + sail_zero_extend(rr32(sail_zero_extend(r_m, 4)), 64) + } + }; + (signed(reg), byte2, 1) + } + }) : (int, sbits(64), {|0, 1, 4|}) + }, + 2 => { + (match r_m { + 4 => x86_effective_addr_from_sib(proc_mode, temp_rip, rex_byte, mod_var, sib), + _ => { + let dword : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, None()), 4, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + let reg : sbits(64) = { + if in_64bit_mode(proc_mode) then { + rgfi(unsigned(reg_index(r_m, rex_byte, 0b00))) + } else { + sail_zero_extend(rr32(sail_zero_extend(r_m, 4)), 64) + } + }; + (signed(reg), dword, 4) + } + }) : (int, sbits(64), {|0, 1, 4|}) + }, + _ => x86_model_error("mod-value-wrong") + } + }; + let dst_base : int = addr + signed(displacement); + let dst_base : sbits(64) = { + if in_64bit_mode(proc_mode) then { + if p4 then { + sail_zero_extend(bits_of_int(dst_base, 32), 64) + } else { + bits_of_int(dst_base, 64) + } + } else { + sail_zero_extend(bits_of_int(dst_base, 32), 64) + } + }; + (dst_base, increment_rip_by) +} + +/*! Effective address calculation. ++ This is a wrapper that calls + @ ( tsee x86-effective-addr-16 ) or @ ( tsee x86-effective-addr-32/64 ) + based on the address size. +
*/ +val x86_effective_addr : (proc_mode, prefixes, sbits(48), bits(8), bits(3), bits(2), sib, bits(3)) -> (sbits(64), {|0, 1, 2, 4|}) + +function x86_effective_addr (proc_mode, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, num_imm_bytes) = { + let p4 = 0x67 == prefixes[adr]; + if 2 == select_address_size(proc_mode, Some(prefixes)) then { + let (elem1, elem2) : (bits(16), {|0, 1, 2|}) = x86_effective_addr_16(proc_mode, temp_rip, r_m, mod_var); + (sail_zero_extend(elem1, 64), elem2) + } else { + x86_effective_addr_32_64(proc_mode, p4, temp_rip, rex_byte, r_m, mod_var, sib, num_imm_bytes) + } +} + +/*! Functions to fetch and read operands from an instruction, + and to write results to appropriate registers/memory locations, + based on ModR/M, SIB, immediate, and/or displacement bytes. +Checking if alignment is enabled +Source: Intel Manuals, Volume 3, Section 6.15, Exception + and Interrupt Reference:
+ +Description: Indicates that the processor detected an + unaligned memory operand when alignment checking was + enabled. Alignment checks are only carried out in data ( or stack ) + accesses ( not in code fetches or system segment accesses ) . An example + of an alignment-check violation is a word stored at an odd byte + address, or a doubleword stored at an address that is not an integer + multiple of 4.+ +
Note that the alignment check exception ( #AC ) is + generated only for data types that must be aligned on word, + doubleword, and quadword boundaries. A general-protection + exception ( #GP ) is generated 128-bit data types that are not aligned + on a 16-byte boundary.+ +
To enable alignment checking, the following conditions + must be true:+
Alignment-check exceptions ( #AC ) are generated only when + operating at privilege level 3 ( user mode ) . Memory references that + default to privilege level 0, such as segment descriptor loads, do not + generate alignment-check exceptions, even when caused by a memory + reference made from privilege level 3.*/ +val alignment_checking_enabled_p : unit -> bool + +function alignment_checking_enabled_p () = { + let cr0 : bits(32) = truncate(ctrs[0], 32); + let am : bits(1) = Mk_cr0bits(cr0)[am]; + let ac : bits(2) = { + let rflags_var : rflagsbits = rflags; + sail_zero_extend(rflags_var[ac], 2) + }; + let cpl : bits(2) = Mk_segment_selectorbits(seg_visibles[1])[rpl]; + am == 0b1 & ac == 0b01 & cpl == 0b11 +} + +/*! Read an operand from memory or a register. +
+ Based on the ModR/M byte, + the operand is read from either a register or memory. + In the latter case, we calculate the effective address + and the we read the operand from it. + Besides returning the operand, + we also return the calculated effective address. + This is useful for instructions that modify the operand after reading it + ( e.g. the source/destination operand of ADD ) , + which pass the effective address calculated by this function + to @ ( tsee x86-operand-to-reg/mem ) ( which writes the result to memory ) . +
*/ +val x86_operand_from_modr_m_and_sib_bytes : (proc_mode, bits(1), {|1, 2, 4, 6, 8, 10, 16|}, bool, bool, range(0, 5), prefixes, sbits(48), bits(8), bits(3), bits(2), sib, bits(3)) -> (bits(128), {|0, 1, 2, 4|}, sbits(64)) + +function x86_operand_from_modr_m_and_sib_bytes (proc_mode, reg_type, operand_size, inst_ac?, memory_ptr?, seg_reg, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, num_imm_bytes) = { + let (addr, increment_rip_by) : (sbits(64), {|0, 1, 2, 4|}) = { + if mod_var == 0b11 then { + (0x0000000000000000, 0) + } else { + x86_effective_addr(proc_mode, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, num_imm_bytes) + } + }; + let operand : bits(128) = { + if mod_var == 0b11 then { + if reg_type == 0b0 then { + sail_zero_extend(rgfi_size(bits_of_int(operand_size, 4), reg_index(r_m, rex_byte, 0b00), rex_byte), 128) + } else { + xmmi_size(bits_of_int(operand_size, 5), reg_index(r_m, rex_byte, 0b00)) + } + } else { + let check_alignment? : bool = inst_ac? & alignment_checking_enabled_p(); + let addr_size = select_address_size(proc_mode, Some(prefixes)); + let base_reg = select_base_register(proc_mode, rex_byte, r_m, mod_var, sib); + rme_size(proc_mode, addr_size, operand_size, addr, base_reg, seg_reg, ":R", check_alignment?, memory_ptr?) + } + }; + (operand, increment_rip_by, addr) +} + +/*! Write an operand to memory or a general-purpose register. ++ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @ ( tsee x86-operand-from-modr/m-and-sib-bytes ) . +
*/ +val x86_operand_to_reg_mem : (proc_mode, {|1, 2, 4, 6, 8, 10, 16|}, bool, bool, bits(128), seg_reg_idx, sbits(64), prefixes, bits(8), bits(3), bits(2), sib) -> unit + +function x86_operand_to_reg_mem (proc_mode, operand_size, inst_ac?, memory_ptr?, operand, seg_reg, addr, prefixes, rex_byte, r_m, mod_var, sib) = { + if mod_var == 0b11 then { + write_rgfi_size(bits_of_int(operand_size, 4), reg_index(r_m, rex_byte, 0b00), truncate(operand, 64), rex_byte) + } else { + let check_alignment? : bool = inst_ac? & alignment_checking_enabled_p(); + let addr_size = select_address_size(proc_mode, Some(prefixes)); + let base_reg : option(base_reg_idx) = select_base_register(proc_mode, rex_byte, r_m, mod_var, sib); + wme_size(proc_mode, addr_size, operand_size, addr, base_reg, seg_reg, operand, check_alignment?, memory_ptr?) + } +} + +/*! Write an operand to memory or an XMM register. ++ Based on the ModR/M byte, + the operand is written to either a register or memory. + The address argument of this function is often + the effective address calculated and returned by + @ ( tsee x86-operand-from-modr/m-and-sib-bytes ) . +
*/ +val x86_operand_to_xmm_mem : (proc_mode, {|4, 8, 16|}, bool, bits(128), seg_reg_idx, sbits(64), prefixes, bits(8), bits(3), bits(2), sib) -> unit + +function x86_operand_to_xmm_mem (proc_mode, operand_size, inst_ac?, operand, seg_reg, addr, prefixes, rex_byte, r_m, mod_var, sib) = { + if mod_var == 0b11 then { + write_xmmi_size(bits_of_int(operand_size, 5), reg_index(r_m, rex_byte, 0b00), unsigned(operand)) + } else { + let check_alignment? : bool = inst_ac? & alignment_checking_enabled_p(); + let addr_size = select_address_size(proc_mode, Some(prefixes)); + let base_reg : option(base_reg_idx) = select_base_register(proc_mode, rex_byte, r_m, mod_var, sib); + wme_size(proc_mode, addr_size, operand_size, addr, base_reg, seg_reg, operand, check_alignment?, false) + } +} + +/*! Selecting the operand size for general-purpose instructions +@ ( ' select-operand-size ' ) selects the operand size of the + instruction. It is cognizant of the instruction prefixes, the + @ ( ' rex ' ) byte, the operand type ( e.g., immediate operand or not ) , + and the default operand size ( obtained from the state ) .
+ +This function was written by referring to the following: +
+
+
+
+ The first image above has been captured from Volume 1: Basic Architecture,
+ Intel\ ( R\ ) 64 and IA-32 Architectures Software Developer ' s Manual,
+ Order Number: 253665-062US, March 2017.
This function also includes three additional boolean parameters that serve + to accommodate instructions that do not quite follow the general rules + specified by the table above:
+ ++ The maximum length of an instruction is 15 bytes; + a longer instruction causes a #GP ( 0 ) exception. + See AMD manual, Dec ' 17, Volume 2, Table 8-6. + This function is used to check this condition. +
++ The @ ( ' start-rip ' ) argument is + the instruction pointer at the beginning of the instruction. + The @ ( ' temp-rip ' ) argument is generally + the instruction pointer just past the end of the instruction, + in which case the @ ( ' delta-rip ' ) argument is 0. + In the other cases, @ ( ' delta-rip ' ) is a small non-zero number, + and @ ( ' temp-rip + delta-rip ' ) is + the instruction pointer just past the end of the instruction. +
++ This function returns @ ( ' nil ' ) if the length does not exceed 15 bytes. + Otherwise, this function returns the offending length ( a number above 15 ) , + which is useful for error reporting in the model. +
*/ +val check_instruction_length : (sbits(48), sbits(48), bits(3)) -> option(int) + +function check_instruction_length (start_rip, temp_rip, delta_rip) = { + let start_rip : int = signed(start_rip); + let temp_rip : int = signed(temp_rip); + let delta_rip : nat = unsigned(delta_rip); + let end_rip : sbits(49) = bits_of_int(signed(bits_of_int(temp_rip, 48)) + unsigned(bits_of_int(delta_rip, 3)), 49); + let length : sbits(50) = bits_of_int(signed(end_rip) - signed(bits_of_int(start_rip, 48)), 50); + if 15 < signed(length) then { + Some(signed(length)) + } else { + None() + } +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/dispatch_macros.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/dispatch_macros.sail new file mode 100644 index 000000000..1aef22f41 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/dispatch_macros.sail @@ -0,0 +1,17 @@ + +val chk_exc_fn : (string, string, list(string), proc_mode, prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function chk_exc_fn (decode_context, type_id, feature_flags, proc_mode, prefixes, rex_byte, opcode, modr_m, sib) = { + if decode_context == ":VEX" & ((0xf3 == prefixes[rep] | 0xf2 == prefixes[rep]) | 0x66 == prefixes[opr] | not_bool(rex_byte == 0x00) | not_bool(in_64bit_mode(proc_mode) | in_compatibility_mode(proc_mode) | in_protected_mode(proc_mode))) then { + x86_fault("(:ud)") + } else if type_id == ":TYPE-VEX-GPR" then { + if feature_flags_fn(feature_flags) == 0 then x86_fault("(:ud)"); + () + } else if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 then { + x86_fault("(:nm)") + } else if not_bool(in_list(type_id, [|":TYPE-22-7", ":TYPE-22-8", ":TYPE-22-9"|])) & Mk_cr4bits(loghead(22, ctrs[4]))[osfxsr] == 0b0 then { + x86_fault("(:ud)") + } else if Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 | 0xf0 == prefixes[lck] | feature_flags_fn(feature_flags) == 0 then { + x86_fault("(:ud)") + } else () +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/environment.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/environment.sail new file mode 100644 index 000000000..c2ca6e592 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/environment.sail @@ -0,0 +1 @@ +$include "./top_level_memory.sail" diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/init.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/init.sail new file mode 100644 index 000000000..e90d6a300 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/init.sail @@ -0,0 +1,8 @@ +$ifndef __X86_INIT +$define __X86_INIT + +// An extension hook for model initialization; called by main() +val initialize_model : unit -> unit +function initialize_model() = () + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/jump_and_loop.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/jump_and_loop.sail new file mode 100644 index 000000000..92a95167c --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/jump_and_loop.sail @@ -0,0 +1,304 @@ + +val x86_near_jmp_op_en_d : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_near_jmp_op_en_d (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-NEAR-JMP-OP/EN-D"; + let byte_operand? : bool = opcode == 0xeb; + let offset_size : {|1, 2, 4|} = check_range(0, 4, select_operand_size(proc_mode, byte_operand?, rex_byte, false, prefixes, false, true, true)); + let offset : sbits(32) = truncate(rime_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), offset_size, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false), 32); + let next_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, bits_of_int(offset_size, 48)); + let badlength? : option(int) = check_instruction_length(start_rip, next_rip, 0b000); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let temp_rip : sbits(48) = add_to_iptr(proc_mode, next_rip, sail_sign_extend(offset, 48)); + let temp_rip : sbits(48) = { + if offset_size == 2 then { + logand(0x00000000ffff, temp_rip) + } else { + temp_rip + } + }; + write_iptr(proc_mode, temp_rip) +} + +val x86_near_jmp_op_en_m : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_near_jmp_op_en_m (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-NEAR-JMP-OP/EN-M"; + let r_m : bits(3) = modr_m[r_m]; + let mod_var : bits(2) = modr_m[mod]; + let reg : bits(3) = modr_m[reg]; + let p2 : bits(8) = prefixes[seg]; + let p4? : bool = 0x67 == prefixes[adr]; + let operand_size : {|2, 4, 8|} = check_range(2, 8, select_operand_size(proc_mode, false, rex_byte, false, prefixes, true, true, true)); + let seg_reg : seg_reg_idx = select_segment_register(proc_mode, prefixes, mod_var, r_m, sib); + let inst_ac? : bool = true; + let (jmp_addr, increment_rip_by, addr) : (bits(128), {|0, 1, 2, 4|}, sbits(64)) = x86_operand_from_modr_m_and_sib_bytes(proc_mode, 0b0, operand_size, inst_ac?, false, seg_reg, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, 0b000); + let jmp_addr : sbits(64) = truncate(jmp_addr, 64); + let increment_rip_by : bits(3) = bits_of_int(increment_rip_by, 3); + let temp_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, sail_zero_extend(increment_rip_by, 48)); + let badlength? : option(int) = check_instruction_length(start_rip, temp_rip, 0b000); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let jmp_addr : sbits(64) = { + if in_64bit_mode(proc_mode) then { + jmp_addr + } else { + jmp_addr + } + }; + if not_bool(if in_64bit_mode(proc_mode) then { + canonical_address_p(signed(jmp_addr)) + } else { + let cs_limit : bits(32) = seg_hidden_limits[1]; + 0 <= signed(jmp_addr) & signed(jmp_addr) <= unsigned(cs_limit) + }) then x86_fault("(!!FAULT-FRESH :GP 0 :BAD-RETURN-ADDRESS JMP-ADDR)"); + write_iptr(proc_mode, truncate(jmp_addr, 48)) +} + +/*! Absolute Indirect Jump: Far +Op/En: D
+FF/5: JMP m16:16 or m16:32 or m16:64
+ +Source: Intel Manuals ( Vol. 2A ) Instruction Set Reference: the + text below has been edited to contain information only about the + 64-bit mode.
+ +The JMP instruction cannot be used to perform + inter-privilege-level far jumps. The processor always uses the + segment selector part of the far address to access the corresponding + descriptor in the GDT or LDT. The descriptor type and access rights + determine the type of jump to be performed.
+ +Far Jump to a Conforming or Non-Conforming Code Segment: If + the selected descriptor is for a code segment, a far jump to a code + segment at the same privilege level is performed. If the selected code + segment is at a different privilege level and the code segment is + non-conforming, a general-protection exception is generated. The + target operand specifies an absolute far address indirectly with a + memory location ( m16:16 or m16:32 or m16:64 ) . The operand-size + attribute and the REX.w bit determine the size of the offset ( 16 or + 32 or 64 bits ) in the far address. The new code segment selector and + its descriptor are loaded into CS register, and the offset from the + instruction is loaded into the RIP register.
+ +Far Jump through a Call Gate: When executing a far jump + through a call gate, the segment selector specified by the target + operand identifies the call gate. The offset part of the target + operand is ignored. The processor then jumps to the code segment + specified in the call gate descriptor and begins executing the + instruction at the offset specified in the call gate. No stack switch + occurs. The target operand specifies the far address of the call gate + indirectly with a memory location ( m16:16 or m16:32 or m16:64 ) .
*/ +val x86_far_jmp_op_en_d : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_far_jmp_op_en_d (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-FAR-JMP-OP/EN-D"; + let r_m : bits(3) = modr_m[r_m]; + let mod_var : bits(2) = modr_m[mod]; + let reg : bits(3) = modr_m[reg]; + if mod_var == 0b11 then x86_model_error(":SOURCE-OPERAND-NOT-MEMORY-LOCATION"); + let p2 : bits(8) = prefixes[seg]; + let p4? : bool = 0x67 == prefixes[adr]; + let offset_size : {|2, 4, 8|} = check_range(2, 8, select_operand_size(proc_mode, false, rex_byte, false, prefixes, false, false, false)); + let seg_reg : seg_reg_idx = select_segment_register(proc_mode, prefixes, mod_var, r_m, sib); + let inst_ac? : bool = true; + let (mem, increment_rip_by, addr) : (bits(128), {|0, 1, 2, 4|}, sbits(64)) = x86_operand_from_modr_m_and_sib_bytes(proc_mode, 0b0, 2 + offset_size, inst_ac?, true, seg_reg, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, 0b000); + let increment_rip_by : bits(3) = bits_of_int(increment_rip_by, 3); + let badlength? : option(int) = check_instruction_length(start_rip, temp_rip, increment_rip_by); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let selector : bits(16) = truncate(mem, 16); + let offset : bits(64) = sail_mask(64, bitslice(mem, 16, ash(offset_size, 3))); + let sel_index : bits(13) = Mk_segment_selectorbits(selector)[index]; + let sel_ti : bits(1) = Mk_segment_selectorbits(selector)[ti]; + let sel_rpl : bits(2) = Mk_segment_selectorbits(selector)[rpl]; + if sel_ti == 0b0 & sel_index == 0b0000000000000 then x86_fault("(!!FAULT-FRESH :GP 0 :GP-NULLSELECTOR 0)"); + let (dt_base_addr, dt_limit) : (bits(64), bits(32)) = { + if sel_ti == 0b0 then { + let gdtr : bits(80) = strs[0]; + let gdtr_base : bits(64) = { + if in_64bit_mode(proc_mode) then { + Mk_gdtr_idtrbits(gdtr)[base_addr] + } else { + sail_zero_extend(truncate(Mk_gdtr_idtrbits(gdtr)[base_addr], 32), 64) + } + }; + let gdtr_limit : bits(16) = Mk_gdtr_idtrbits(gdtr)[limit]; + (gdtr_base, sail_zero_extend(gdtr_limit, 32)) + } else { + let ldtr_base : bits(64) = ssr_hidden_bases[0]; + let ldtr_base : bits(64) = { + if in_64bit_mode(proc_mode) then { + ldtr_base + } else { + sail_zero_extend(truncate(ldtr_base, 32), 64) + } + }; + let ldtr_limit : bits(32) = ssr_hidden_limits[0]; + (ldtr_base, ldtr_limit) + } + }; + let largest_address : int = ash(unsigned(sel_index), 3) + 7; + if unsigned(dt_limit) < largest_address then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :GP-SELECTOR-LIMIT-CHECK-FAILED (LIST SELECTOR DT-BASE-ADDR DT-LIMIT))"); + let descriptor_addr : int = unsigned(dt_base_addr) + unsigned(bits_of_int(ash(unsigned(sel_index), 3), 16)); + if not_bool(canonical_address_p(descriptor_addr)) then x86_model_error(":DESCRIPTOR-ADDR-VIRTUAL-MEMORY-ERROR"); + let descriptor : bits(64) = truncate(rml_size(8, bits_of_int(descriptor_addr, 48), ":X"), 64); + let s : bits(1) = Mk_code_segment_descriptorbits(descriptor)[s]; + if s == 0b1 then { + let msb_of_type : bits(1) = Mk_code_segment_descriptorbits(descriptor)[msb_of_type]; + if not_bool(msb_of_type == 0b1) then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :JMP-FAR-DATA-SEGMENT SEL-INDEX)"); + let d : bits(1) = Mk_code_segment_descriptorbits(descriptor)[d]; + if in_64bit_mode(proc_mode) & d == 0b1 then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :CS.D=1-IN-64-BIT-MODE SEL-INDEX)"); + let cpl : bits(2) = Mk_segment_selectorbits(seg_visibles[1])[rpl]; + let dpl : bits(2) = Mk_code_segment_descriptorbits(descriptor)[dpl]; + let c : bits(1) = Mk_code_segment_descriptorbits(descriptor)[c]; + let allowed : bool = { + if c == 0b1 then { + unsigned(dpl) <= unsigned(cpl) + } else { + unsigned(sel_rpl) <= unsigned(cpl) & cpl == dpl + } + }; + if not_bool(allowed) then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :PRIVILEGE-CHECK-FAIL (ACONS :DPL DPL (ACONS :CPL CPL (ACONS :RPL SEL-RPL NIL))))"); + let p : bits(1) = Mk_code_segment_descriptorbits(descriptor)[p]; + if not_bool(p == 0b1) then x86_fault("(!!FAULT-FRESH :NP SEL-INDEX :CODE-SEGMENT-NOT-PRESENT SEL-INDEX)"); + let jmp_addr : sbits(64) = { + match offset_size { + 2 => sail_zero_extend(truncate(offset, 16), 64), + 4 => sail_zero_extend(truncate(offset, 32), 64), + _ => offset + } + }; + let jmp_addr_ok : bool = { + if in_64bit_mode(proc_mode) then { + canonical_address_p(signed(jmp_addr)) + } else { + let limit15_0 : bits(16) = Mk_code_segment_descriptorbits(descriptor)[limit15_0]; + let limit19_16 : bits(4) = Mk_code_segment_descriptorbits(descriptor)[limit19_16]; + let limit : int = changeSlice(ash(unsigned(limit19_16), 16), 0, 16, unsigned(limit15_0)); + let g : bits(1) = Mk_code_segment_descriptorbits(descriptor)[g]; + let max_offset : int : int = { + if g == 0b1 then { + ash(limit, 12) - 1 + } else { + limit + } + }; + signed(jmp_addr) < max_offset : int + } + }; + if not_bool(jmp_addr_ok) then x86_fault("(!!FAULT-FRESH :GP 0 :NONCANONICAL-OR-OUTSIDE-SEGMENT-LIMIT JMP-ADDR)"); + let new_cs_visible : segment_selectorbits = [Mk_segment_selectorbits(selector) with rpl = cpl]; + seg_visibles[1] = new_cs_visible.bits; + seg_hidden_bases[1] = dt_base_addr; + seg_hidden_limits[1] = dt_limit; + seg_hidden_attrs[1] = make_code_segment_attr_field(descriptor).bits; + write_iptr(proc_mode, truncate(jmp_addr, 48)) + } else { + if not_bool(in_64bit_mode(proc_mode)) then x86_model_error(":FAR-JMP-SYSTEM-UNIMPLEMENTED-IN-32-BIT-MODE"); + let largest_address : int = ash(unsigned(sel_index), 3) + 15; + if unsigned(dt_limit) < largest_address then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :GP-SELECTOR-LIMIT-CHECK-FAILED (LIST SELECTOR DT-BASE-ADDR DT-LIMIT))"); + let descriptor : bits(128) = rml_size(16, bits_of_int(descriptor_addr, 48), ":X"); + let (call_gate_desc?, reason2) : (bool, (string, bits(128))) = ia32e_valid_call_gate_segment_descriptor_p(descriptor); + if call_gate_desc? then { + let cpl : bits(2) = Mk_segment_selectorbits(seg_visibles[1])[rpl]; + let dpl : bits(2) = Mk_call_gate_descriptorbits(descriptor)[dpl]; + if not_bool(unsigned(cpl) <= unsigned(dpl) & unsigned(sel_rpl) <= unsigned(dpl)) then x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :PRIVILEGE-CHECK-FAIL (ACONS :DPL DPL (ACONS :CPL CPL (ACONS :RPL SEL-RPL NIL))))"); + let cs_selector : bits(16) = Mk_call_gate_descriptorbits(descriptor)[selector]; + let cs_sel_index : bits(13) = Mk_segment_selectorbits(cs_selector)[index]; + let cs_sel_ti : bits(1) = Mk_segment_selectorbits(cs_selector)[ti]; + let cs_sel_rpl : bits(2) = Mk_segment_selectorbits(cs_selector)[rpl]; + if cs_sel_ti == 0b0 & cs_sel_index == 0b0000000000000 then x86_fault("(!!FAULT-FRESH :GP 0 :CALL-GATE-CODE-SEGMENT-NULLSELECTOR 0)"); + let (cs_dt_base_addr, cs_dt_limit) : (bits(64), bits(32)) = { + if sel_ti == 0b0 then { + let gdtr : bits(80) = strs[0]; + let gdtr_base : bits(64) = Mk_gdtr_idtrbits(gdtr)[base_addr]; + let gdtr_base : bits(64) = { + if in_64bit_mode(proc_mode) then { + gdtr_base + } else { + sail_zero_extend(truncate(gdtr_base, 32), 64) + } + }; + let gdtr_limit : bits(16) = Mk_gdtr_idtrbits(gdtr)[limit]; + (gdtr_base, sail_zero_extend(gdtr_limit, 32)) + } else { + let ldtr_base : bits(64) = ssr_hidden_bases[0]; + let ldtr_base : bits(64) = { + if in_64bit_mode(proc_mode) then { + ldtr_base + } else { + sail_zero_extend(truncate(ldtr_base, 32), 64) + } + }; + let ldtr_limit : bits(32) = ssr_hidden_limits[0]; + (ldtr_base, ldtr_limit) + } + }; + let largest_address : int = ash(unsigned(cs_sel_index), 3) + 7; + if unsigned(cs_dt_limit) < largest_address then x86_fault("(!!FAULT-FRESH :GP CS-SEL-INDEX :GP-SELECTOR-LIMIT-CHECK-FAILED (LIST CS-SELECTOR CS-DT-BASE-ADDR CS-DT-LIMIT))"); + let cs_descriptor_addr : int = unsigned(cs_dt_base_addr) + unsigned(bits_of_int(ash(unsigned(cs_sel_index), 3), 16)); + if not_bool(canonical_address_p(cs_descriptor_addr)) then x86_model_error(":CS-DESCRIPTOR-ADDR-VIRTUAL-MEMORY-ERROR"); + let cs_descriptor : bits(64) = truncate(rml_size(8, bits_of_int(cs_descriptor_addr, 48), ":X"), 64); + let (valid?, reason) : (bool, (string, bits(64))) = ia32e_valid_code_segment_descriptor_p(cs_descriptor); + if not_bool(valid?) then x86_fault("(!!FAULT-FRESH :GP CS-SEL-INDEX :CALL-GATE-CODE-SEGMENT-DESCRIPTOR-INVALID (CONS REASON CS-DESCRIPTOR))"); + let cs_dpl : bits(2) = Mk_code_segment_descriptorbits(cs_descriptor)[dpl]; + let c_bit : bits(1) = Mk_code_segment_descriptorbits(cs_descriptor)[c]; + if c_bit == 0b1 & not_bool(unsigned(cs_dpl) <= unsigned(cpl)) | c_bit == 0b0 & not_bool(cs_dpl == cpl) then x86_fault("(!!FAULT-FRESH :GP CS-SEL-INDEX :PRIVILEGE-CHECK-FAIL (ACONS :C-BIT C-BIT (ACONS :CPL CPL (ACONS :CS-DPL CS-DPL NIL))))"); + let call_gate_offset15_0 : bits(16) = Mk_call_gate_descriptorbits(descriptor)[offset15_0]; + let call_gate_offset31_16 : bits(16) = Mk_call_gate_descriptorbits(descriptor)[offset31_16]; + let call_gate_offset63_32 : bits(32) = Mk_call_gate_descriptorbits(descriptor)[offset63_32]; + let call_gate_offset31_0 : int = changeSlice(ash(unsigned(call_gate_offset31_16), 16), 0, 16, unsigned(call_gate_offset15_0)); + let call_gate_offset : int = changeSlice(ash(unsigned(call_gate_offset63_32), 32), 0, 32, call_gate_offset31_0); + let jmp_addr : sbits(64) = { + match offset_size { + 2 => sail_zero_extend(bits_of_int(call_gate_offset, 16), 64), + 4 => sail_zero_extend(bits_of_int(call_gate_offset, 32), 64), + _ => bits_of_int(call_gate_offset, 64) + } + }; + if not_bool(canonical_address_p(signed(jmp_addr))) then x86_model_error(":TARGET-OFFSET-VIRTUAL-MEMORY-ERROR"); + let new_cs_visible : segment_selectorbits = [Mk_segment_selectorbits(cs_selector) with rpl = cpl]; + seg_visibles[1] = new_cs_visible.bits; + seg_hidden_bases[1] = cs_dt_base_addr; + seg_hidden_limits[1] = cs_dt_limit; + seg_hidden_attrs[1] = make_code_segment_attr_field(cs_descriptor).bits; + write_iptr(proc_mode, truncate(jmp_addr, 48)) + } else { + x86_fault("(!!FAULT-FRESH :GP SEL-INDEX :EITHER-BOTH-CODE-SEGMENT-OR-CALL-GATE-ARE-ABSENT-OR-SOME-OTHER-DESCRIPTOR-IS-PRESENT (CONS REASON2 DESCRIPTOR))") + } + } +} + +val x86_loop : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_loop (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-LOOP"; + let badlength? : option(int) = check_instruction_length(start_rip, temp_rip, 0b001); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let p4? : bool = 0x67 == prefixes[adr]; + let counter_size : address_size = select_address_size(proc_mode, Some(prefixes)); + let counter : bits(64) = rgfi_size(bits_of_int(counter_size, 4), 0x1, rex_byte); + let counter : bits(64) = sail_mask(64, trunc(counter_size, bits_of_int(unsigned(counter) - 1, 64))); + let zf : bits(1) = { + let rflags_var : rflagsbits = rflags; + rflags_var[zf] + }; + let branch_cond : bool = { + if opcode == 0xe2 then { + not_bool(counter == 0x0000000000000000) + } else if opcode == 0xe1 then { + zf == 0b1 & not_bool(counter == 0x0000000000000000) + } else { + zf == 0b0 & not_bool(counter == 0x0000000000000000) + } + }; + if branch_cond then { + let rel8 : sbits(64) = rime_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), 1, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false); + let next_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, bits_of_int(1 + signed(rel8), 48)); + write_rgfi_size(bits_of_int(counter_size, 4), 0x1, counter, rex_byte); + write_iptr(proc_mode, next_rip) + } else { + let next_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, 0x000000000001); + write_rgfi_size(bits_of_int(counter_size, 4), 0x1, counter, rex_byte); + write_iptr(proc_mode, next_rip) + } +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/linear_memory.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/linear_memory.sail new file mode 100644 index 000000000..38a1042ab --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/linear_memory.sail @@ -0,0 +1,428 @@ +$include "./paging.sail" + +/*! Definitions of @ ( see rb ) and @ ( see wb ) +The functions @ ( ' rb ' ) ( read bytes ) and @ ( ' wb ' ) ( write + bytes ) are used in reasoning about memory reads and writes. Functions + like @ ( ' rml08 ' ) , @ ( ' rml16 ' ) , @ ( ' rml32 ' ) , and @ ( ' rml64 ' ) are reduced to + @ ( ' rb ' ) , and @ ( ' wml08 ' ) , @ ( ' wml16 ' ) , @ ( ' wml32 ' ) , and @ ( ' wml64 ' ) to + @ ( ' wb ' ) during reasoning.
*/ +val rml08 : (sbits(48), string) -> bits(8) + +function rml08 (lin_addr, r_x) = { + rb(1, lin_addr, r_x) +} + +val riml08 : (sbits(48), string) -> sbits(8) + +function riml08 (lin_addr, r_x) = { + let sailval : bits(8) = rml08(lin_addr, r_x); + sailval +} + +val wml08 : (sbits(48), bits(8)) -> unit + +function wml08 (lin_addr, sailval) = { + wb(1, lin_addr, ":W", sailval) +} + +val wiml08 : (sbits(48), sbits(8)) -> unit + +function wiml08 (lin_addr, sailval) = { + wml08(lin_addr, sailval) +} + +val rml16 : (sbits(48), string) -> bits(16) + +function rml16 (lin_addr, r_x) = { + let one__plus_lin_addr : sbits(49) = bits_of_int(1 + signed(lin_addr), 49); + if canonical_address_p(signed(one__plus_lin_addr)) then { + rb(2, lin_addr, r_x) + } else { + x86_model_error("rml16") + } +} + +val riml16 : (sbits(48), string) -> sbits(16) + +function riml16 (lin_addr, r_x) = { + let sailval : bits(16) = rml16(lin_addr, r_x); + sailval +} + +val wml16 : (sbits(48), bits(16)) -> unit + +function wml16 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let one__plus_lin_addr : sbits(49) = bits_of_int(1 + signed(lin_addr), 49); + if canonical_address_p(signed(one__plus_lin_addr)) then { + wb(2, lin_addr, ":W", sailval) + } else { + x86_model_error("wml16") + } +} + +val wiml16 : (sbits(48), sbits(16)) -> unit + +function wiml16 (lin_addr, sailval) = { + wml16(lin_addr, sailval) +} + +val rml32 : (sbits(48), string) -> bits(32) + +function rml32 (lin_addr, r_x) = { + assert(canonical_address_p(signed(lin_addr))); + let three__plus_lin_addr : sbits(49) = bits_of_int(3 + signed(lin_addr), 49); + if canonical_address_p(signed(three__plus_lin_addr)) then { + rb(4, lin_addr, r_x) + } else { + x86_model_error("rml32") + } +} + +val riml32 : (sbits(48), string) -> sbits(32) + +function riml32 (lin_addr, r_x) = { + let sailval : bits(32) = rml32(lin_addr, r_x); + sailval +} + +val wml32 : (sbits(48), bits(32)) -> unit + +function wml32 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let three__plus_lin_addr : sbits(49) = bits_of_int(3 + signed(lin_addr), 49); + if canonical_address_p(signed(three__plus_lin_addr)) then { + wb(4, lin_addr, ":W", sailval) + } else { + x86_model_error("wml32") + } +} + +val wiml32 : (sbits(48), sbits(32)) -> unit + +function wiml32 (lin_addr, sailval) = { + wml32(lin_addr, sailval) +} + +val rml48 : (sbits(48), string) -> bits(48) + +function rml48 (lin_addr, r_x) = { + assert(canonical_address_p(signed(lin_addr))); + let five__plus_lin_addr : sbits(49) = bits_of_int(5 + signed(lin_addr), 49); + if canonical_address_p(signed(five__plus_lin_addr)) then { + rb(6, lin_addr, r_x) + } else { + x86_model_error("rml48") + } +} + +val wml48 : (sbits(48), bits(48)) -> unit + +function wml48 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let five__plus_lin_addr : sbits(49) = bits_of_int(5 + signed(lin_addr), 49); + if canonical_address_p(signed(five__plus_lin_addr)) then { + wb(6, lin_addr, ":W", sailval) + } else { + x86_model_error("wml48") + } +} + +val rml64 : (sbits(48), string) -> bits(64) + +function rml64 (lin_addr, r_x) = { + assert(canonical_address_p(signed(lin_addr))); + let seven__plus_lin_addr : sbits(49) = bits_of_int(7 + signed(lin_addr), 49); + if canonical_address_p(signed(seven__plus_lin_addr)) then { + rb(8, lin_addr, r_x) + } else { + x86_model_error("rml64") + } +} + +val riml64 : (sbits(48), string) -> sbits(64) + +function riml64 (lin_addr, r_x) = { + let sailval : bits(64) = rml64(lin_addr, r_x); + sailval +} + +val wml64 : (sbits(48), bits(64)) -> unit + +function wml64 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let seven__plus_lin_addr : sbits(50) = bits_of_int(7 + signed(lin_addr), 50); + if canonical_address_p(signed(seven__plus_lin_addr)) then { + wb(8, lin_addr, ":W", sailval) + } else { + x86_model_error("wml64") + } +} + +val wiml64 : (sbits(48), sbits(64)) -> unit + +function wiml64 (lin_addr, sailval) = { + wml64(lin_addr, sailval) +} + +val rml80 : (sbits(48), string) -> bits(80) + +function rml80 (lin_addr, r_x) = { + assert(canonical_address_p(signed(lin_addr))); + let nine__plus_lin_addr : sbits(49) = bits_of_int(9 + signed(lin_addr), 49); + if canonical_address_p(signed(nine__plus_lin_addr)) then { + rb(10, lin_addr, r_x) + } else { + x86_model_error("rml80") + } +} + +val wml80 : (sbits(48), bits(80)) -> unit + +function wml80 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let nine__plus_lin_addr : sbits(49) = bits_of_int(9 + signed(lin_addr), 49); + if canonical_address_p(signed(nine__plus_lin_addr)) then { + wb(10, lin_addr, ":W", sailval) + } else { + x86_model_error("wml80") + } +} + +val rml128 : (sbits(48), string) -> bits(128) + +function rml128 (lin_addr, r_x) = { + assert(canonical_address_p(signed(lin_addr))); + let one_5_plus_lin_addr : sbits(49) = bits_of_int(15 + signed(lin_addr), 49); + if canonical_address_p(signed(one_5_plus_lin_addr)) then { + if app_view then { + rb(16, lin_addr, r_x) + } else { + let p_addr0 : bits(52) = la_to_pa(lin_addr, r_x); + let one__plus_lin_addr : sbits(49) = bits_of_int(1 + signed(lin_addr), 49); + let p_addr1 : bits(52) = la_to_pa(truncate(one__plus_lin_addr, 48), r_x); + let _2_plus_lin_addr : sbits(50) = bits_of_int(2 + signed(lin_addr), 50); + let p_addr2 : bits(52) = la_to_pa(truncate(_2_plus_lin_addr, 48), r_x); + let three__plus_lin_addr : sbits(51) = bits_of_int(3 + signed(lin_addr), 51); + let p_addr3 : bits(52) = la_to_pa(truncate(three__plus_lin_addr, 48), r_x); + let _4_plus_lin_addr : sbits(52) = bits_of_int(4 + signed(lin_addr), 52); + let p_addr4 : bits(52) = la_to_pa(truncate(_4_plus_lin_addr, 48), r_x); + let five__plus_lin_addr : sbits(53) = bits_of_int(5 + signed(lin_addr), 53); + let p_addr5 : bits(52) = la_to_pa(truncate(five__plus_lin_addr, 48), r_x); + let _6_plus_lin_addr : sbits(54) = bits_of_int(6 + signed(lin_addr), 54); + let p_addr6 : bits(52) = la_to_pa(truncate(_6_plus_lin_addr, 48), r_x); + let seven__plus_lin_addr : sbits(55) = bits_of_int(7 + signed(lin_addr), 55); + let p_addr7 : bits(52) = la_to_pa(truncate(seven__plus_lin_addr, 48), r_x); + let _8_plus_lin_addr : sbits(56) = bits_of_int(8 + signed(lin_addr), 56); + let p_addr8 : bits(52) = la_to_pa(truncate(_8_plus_lin_addr, 48), r_x); + let nine__plus_lin_addr : sbits(57) = bits_of_int(9 + signed(lin_addr), 57); + let p_addr9 : bits(52) = la_to_pa(truncate(nine__plus_lin_addr, 48), r_x); + let one_0_plus_lin_addr : sbits(58) = bits_of_int(10 + signed(lin_addr), 58); + let p_addr10 : bits(52) = la_to_pa(truncate(one_0_plus_lin_addr, 48), r_x); + let one_1_plus_lin_addr : sbits(59) = bits_of_int(11 + signed(lin_addr), 59); + let p_addr11 : bits(52) = la_to_pa(truncate(one_1_plus_lin_addr, 48), r_x); + let one_2_plus_lin_addr : sbits(60) = bits_of_int(12 + signed(lin_addr), 60); + let p_addr12 : bits(52) = la_to_pa(truncate(one_2_plus_lin_addr, 48), r_x); + let one_3_plus_lin_addr : sbits(61) = bits_of_int(13 + signed(lin_addr), 61); + let p_addr13 : bits(52) = la_to_pa(truncate(one_3_plus_lin_addr, 48), r_x); + let one_4_plus_lin_addr : sbits(62) = bits_of_int(14 + signed(lin_addr), 62); + let p_addr14 : bits(52) = la_to_pa(truncate(one_4_plus_lin_addr, 48), r_x); + let one_5_plus_lin_addr : sbits(63) = bits_of_int(15 + signed(lin_addr), 63); + let p_addr15 : bits(52) = la_to_pa(truncate(one_5_plus_lin_addr, 48), r_x); + let byte0 : bits(8) = memi(sail_zero_extend(p_addr0, 64)); + let byte1 : bits(8) = memi(sail_zero_extend(p_addr1, 64)); + let byte2 : bits(8) = memi(sail_zero_extend(p_addr2, 64)); + let byte3 : bits(8) = memi(sail_zero_extend(p_addr3, 64)); + let byte4 : bits(8) = memi(sail_zero_extend(p_addr4, 64)); + let byte5 : bits(8) = memi(sail_zero_extend(p_addr5, 64)); + let byte6 : bits(8) = memi(sail_zero_extend(p_addr6, 64)); + let byte7 : bits(8) = memi(sail_zero_extend(p_addr7, 64)); + let byte8 : bits(8) = memi(sail_zero_extend(p_addr8, 64)); + let byte9 : bits(8) = memi(sail_zero_extend(p_addr9, 64)); + let byte10 : bits(8) = memi(sail_zero_extend(p_addr10, 64)); + let byte11 : bits(8) = memi(sail_zero_extend(p_addr11, 64)); + let byte12 : bits(8) = memi(sail_zero_extend(p_addr12, 64)); + let byte13 : bits(8) = memi(sail_zero_extend(p_addr13, 64)); + let byte14 : bits(8) = memi(sail_zero_extend(p_addr14, 64)); + let byte15 : bits(8) = memi(sail_zero_extend(p_addr15, 64)); + let oword : bits(128) = byte15 @ (byte14 @ (byte13 @ (byte12 @ (byte11 @ (byte10 @ (byte9 @ (byte8 @ (byte7 @ (byte6 @ (byte5 @ (byte4 @ (byte3 @ (byte2 @ (byte1 @ byte0)))))))))))))); + oword + } + } else { + x86_model_error("rml128") + } +} + +val wml128 : (sbits(48), bits(128)) -> unit + +function wml128 (lin_addr, sailval) = { + assert(canonical_address_p(signed(lin_addr))); + let one_5_plus_lin_addr : sbits(49) = bits_of_int(15 + signed(lin_addr), 49); + if canonical_address_p(signed(one_5_plus_lin_addr)) then { + if app_view then { + wb(16, lin_addr, ":W", sailval) + } else { + let p_addr0 : bits(52) = la_to_pa(lin_addr, ":W"); + let one__plus_lin_addr : sbits(49) = bits_of_int(1 + signed(lin_addr), 49); + let p_addr1 : bits(52) = la_to_pa(truncate(one__plus_lin_addr, 48), ":W"); + let _2_plus_lin_addr : sbits(50) = bits_of_int(2 + signed(lin_addr), 50); + let p_addr2 : bits(52) = la_to_pa(truncate(_2_plus_lin_addr, 48), ":W"); + let three__plus_lin_addr : sbits(51) = bits_of_int(3 + signed(lin_addr), 51); + let p_addr3 : bits(52) = la_to_pa(truncate(three__plus_lin_addr, 48), ":W"); + let _4_plus_lin_addr : sbits(52) = bits_of_int(4 + signed(lin_addr), 52); + let p_addr4 : bits(52) = la_to_pa(truncate(_4_plus_lin_addr, 48), ":W"); + let five__plus_lin_addr : sbits(53) = bits_of_int(5 + signed(lin_addr), 53); + let p_addr5 : bits(52) = la_to_pa(truncate(five__plus_lin_addr, 48), ":W"); + let _6_plus_lin_addr : sbits(54) = bits_of_int(6 + signed(lin_addr), 54); + let p_addr6 : bits(52) = la_to_pa(truncate(_6_plus_lin_addr, 48), ":W"); + let seven__plus_lin_addr : sbits(55) = bits_of_int(7 + signed(lin_addr), 55); + let p_addr7 : bits(52) = la_to_pa(truncate(seven__plus_lin_addr, 48), ":W"); + let _8_plus_lin_addr : sbits(56) = bits_of_int(8 + signed(lin_addr), 56); + let p_addr8 : bits(52) = la_to_pa(truncate(_8_plus_lin_addr, 48), ":W"); + let nine__plus_lin_addr : sbits(57) = bits_of_int(9 + signed(lin_addr), 57); + let p_addr9 : bits(52) = la_to_pa(truncate(nine__plus_lin_addr, 48), ":W"); + let one_0_plus_lin_addr : sbits(58) = bits_of_int(10 + signed(lin_addr), 58); + let p_addr10 : bits(52) = la_to_pa(truncate(one_0_plus_lin_addr, 48), ":W"); + let one_1_plus_lin_addr : sbits(59) = bits_of_int(11 + signed(lin_addr), 59); + let p_addr11 : bits(52) = la_to_pa(truncate(one_1_plus_lin_addr, 48), ":W"); + let one_2_plus_lin_addr : sbits(60) = bits_of_int(12 + signed(lin_addr), 60); + let p_addr12 : bits(52) = la_to_pa(truncate(one_2_plus_lin_addr, 48), ":W"); + let one_3_plus_lin_addr : sbits(61) = bits_of_int(13 + signed(lin_addr), 61); + let p_addr13 : bits(52) = la_to_pa(truncate(one_3_plus_lin_addr, 48), ":W"); + let one_4_plus_lin_addr : sbits(62) = bits_of_int(14 + signed(lin_addr), 62); + let p_addr14 : bits(52) = la_to_pa(truncate(one_4_plus_lin_addr, 48), ":W"); + let one_5_plus_lin_addr : sbits(63) = bits_of_int(15 + signed(lin_addr), 63); + let p_addr15 : bits(52) = la_to_pa(truncate(one_5_plus_lin_addr, 48), ":W"); + let byte0 : bits(8) = sailval[7 .. 0]; + let byte1 : bits(8) = sailval[15 .. 8]; + let byte2 : bits(8) = sailval[23 .. 16]; + let byte3 : bits(8) = sailval[31 .. 24]; + let byte4 : bits(8) = sailval[39 .. 32]; + let byte5 : bits(8) = sailval[47 .. 40]; + let byte6 : bits(8) = sailval[55 .. 48]; + let byte7 : bits(8) = sailval[63 .. 56]; + let byte8 : bits(8) = sailval[71 .. 64]; + let byte9 : bits(8) = sailval[79 .. 72]; + let byte10 : bits(8) = sailval[87 .. 80]; + let byte11 : bits(8) = sailval[95 .. 88]; + let byte12 : bits(8) = sailval[103 .. 96]; + let byte13 : bits(8) = sailval[111 .. 104]; + let byte14 : bits(8) = sailval[119 .. 112]; + let byte15 : bits(8) = sailval[127 .. 120]; + bang_memi(sail_zero_extend(p_addr0, 64), byte0); + bang_memi(sail_zero_extend(p_addr1, 64), byte1); + bang_memi(sail_zero_extend(p_addr2, 64), byte2); + bang_memi(sail_zero_extend(p_addr3, 64), byte3); + bang_memi(sail_zero_extend(p_addr4, 64), byte4); + bang_memi(sail_zero_extend(p_addr5, 64), byte5); + bang_memi(sail_zero_extend(p_addr6, 64), byte6); + bang_memi(sail_zero_extend(p_addr7, 64), byte7); + bang_memi(sail_zero_extend(p_addr8, 64), byte8); + bang_memi(sail_zero_extend(p_addr9, 64), byte9); + bang_memi(sail_zero_extend(p_addr10, 64), byte10); + bang_memi(sail_zero_extend(p_addr11, 64), byte11); + bang_memi(sail_zero_extend(p_addr12, 64), byte12); + bang_memi(sail_zero_extend(p_addr13, 64), byte13); + bang_memi(sail_zero_extend(p_addr14, 64), byte14); + bang_memi(sail_zero_extend(p_addr15, 64), byte15) + } + } else { + x86_model_error("wml128") + } +} + +/*! Functions to read/write 8/16/32/64/128 bits into the memory: */ +val rml_size : ({|1, 2, 4, 6, 8, 10, 16|}, sbits(48), string) -> bits(128) + +function rml_size (nbytes, addr, r_x) = { + (match nbytes { + 1 => sail_zero_extend(rml08(addr, r_x), 128), + 2 => sail_zero_extend(rml16(addr, r_x), 128), + 4 => sail_zero_extend(rml32(addr, r_x), 128), + 6 => sail_zero_extend(rml48(addr, r_x), 128), + 8 => sail_zero_extend(rml64(addr, r_x), 128), + 10 => sail_zero_extend(rml80(addr, r_x), 128), + 16 => rml128(addr, r_x), + _ => { + if canonical_address_p(-1 + (nbytes + signed(addr))) then { + sail_mask(128, rb(nbytes, addr, r_x)) + } else { + x86_model_error("rml-size") + } + } + }) : bits(128) +} + +val riml_size : ({|1, 2, 4, 8|}, sbits(48), string) -> sbits(64) + +function riml_size (nbytes, addr, r_x) = { + (match nbytes { + 1 => sail_sign_extend(riml08(addr, r_x), 64), + 2 => sail_sign_extend(riml16(addr, r_x), 64), + 4 => sail_sign_extend(riml32(addr, r_x), 64), + 8 => riml64(addr, r_x), + _ => x86_model_error("riml-size") + }) : sbits(64) +} + +val wml_size : ({|1, 2, 4, 6, 8, 10, 16|}, sbits(48), nat) -> unit + +function wml_size (nbytes, addr, sailval) = { + (match nbytes { + 1 => wml08(addr, bits_of_int(sailval, 8)), + 2 => wml16(addr, bits_of_int(sailval, 16)), + 4 => wml32(addr, bits_of_int(sailval, 32)), + 6 => wml48(addr, bits_of_int(sailval, 48)), + 8 => wml64(addr, bits_of_int(sailval, 64)), + 10 => wml80(addr, bits_of_int(sailval, 80)), + 16 => wml128(addr, bits_of_int(sailval, 128)), + _ => { + if canonical_address_p(-1 + (nbytes + signed(addr))) then { + wb(nbytes, addr, ":W", bits_of_int(sailval, 8 * nbytes)) + } else { + x86_model_error("wml-size") + } + } + }) : unit +} + +val wiml_size : ({|1, 2, 4, 8|}, sbits(48), int) -> unit + +function wiml_size (nbytes, addr, sailval) = { + (match nbytes { + 1 => wiml08(addr, bits_of_int(sailval, 8)), + 2 => wiml16(addr, bits_of_int(sailval, 16)), + 4 => wiml32(addr, bits_of_int(sailval, 32)), + 8 => wiml64(addr, bits_of_int(sailval, 64)), + _ => x86_model_error("wiml-size") + }) : unit +} + +val write_canonical_address_to_memory_user_exec : (sbits(48), sbits(48)) -> option(string) + +function write_canonical_address_to_memory_user_exec (lin_addr, canonical_address) = { + assert(app_view & canonical_address_p(7 + signed(lin_addr))); + let canonical_address_low_nat : bits(32) = truncate(canonical_address, 32); + let canonical_address_high_int : sbits(32) = sail_sign_extend(sail_zero_extend(canonical_address[47 .. 32], 16), 32); + wml32(lin_addr, canonical_address_low_nat); + let next_addr : sbits(49) = bits_of_int(4 + signed(lin_addr), 49); + if not_bool(canonical_address_p(signed(next_addr))) then x86_model_error("wml64-canonical-address-user-view"); + wiml32(truncate(next_addr, 48), canonical_address_high_int); + None() : option(string) +} + +val write_canonical_address_to_memory : (sbits(48), sbits(48)) -> unit + +function write_canonical_address_to_memory (lin_addr, canonical_address) = { + let seven__plus_lin_addr : sbits(50) = bits_of_int(7 + signed(lin_addr), 50); + if canonical_address_p(signed(seven__plus_lin_addr)) then { + if app_view then { + wb(8, lin_addr, ":W", loghead(64, canonical_address)) + } else { + let canonical_address_unsigned_val : bits(64) = loghead(64, canonical_address); + wml64(lin_addr, canonical_address_unsigned_val) + } + } else { + x86_model_error("write-canonical-address-to-memory-error") + } +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/logging.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/logging.sail new file mode 100644 index 000000000..d1f6068c4 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/logging.sail @@ -0,0 +1,65 @@ +$ifndef __X86_LOGGING +$define __X86_LOGGING + +val printState : unit -> unit +function printState () = { + print_bits("rax = ", rgfi(0)); + print_bits("rbx = ", rgfi(3)); + print_bits("rcx = ", rgfi(1)); + print_bits("rdx = ", rgfi(2)); + print_bits("rsi = ", rgfi(6)); + print_bits("rdi = ", rgfi(7)); + print_bits("rbp = ", rgfi(5)); + print_bits("rsp = ", rgfi(4)); + print_bits("r8 = ", rgfi(8)); + print_bits("r9 = ", rgfi(9)); + print_bits("r10 = ", rgfi(10)); + print_bits("r11 = ", rgfi(11)); + print_bits("r12 = ", rgfi(12)); + print_bits("r13 = ", rgfi(13)); + print_bits("r14 = ", rgfi(14)); + print_bits("r15 = ", rgfi(15)); + + print_bits("rflags = ", rflags.bits); + + print_bits("rip = ", read_rip()); +} + +function log_gpr_write(idx) = { + match idx { + 0 => print_bits("rax = ", rgfi(0)), + 1 => print_bits("rcx = ", rgfi(1)), + 2 => print_bits("rdx = ", rgfi(2)), + 3 => print_bits("rbx = ", rgfi(3)), + 4 => print_bits("rsp = ", rgfi(4)), + 5 => print_bits("rbp = ", rgfi(5)), + 6 => print_bits("rsi = ", rgfi(6)), + 7 => print_bits("rdi = ", rgfi(7)), + 8 => print_bits("r8 = ", rgfi(8)), + 9 => print_bits("r9 = ", rgfi(9)), + 10 => print_bits("r10 = ", rgfi(10)), + 11 => print_bits("r11 = ", rgfi(11)), + 12 => print_bits("r12 = ", rgfi(12)), + 13 => print_bits("r13 = ", rgfi(13)), + 14 => print_bits("r14 = ", rgfi(14)), + 15 => print_bits("r15 = ", rgfi(15)) + } +} + +function log_rflags_write() = { + print_bits("rflags = ", rflags.bits); +} + +function log_rip_write() = { + print_bits("rip = ", read_rip()); +} + +function log_memory_read(addr, len, value) = { + print_endline(bits_str(read_rip()) + ": R " + bits_str(addr) + " " + dec_str(len) + " " + bits_str(value)) +} + +function log_memory_write(addr, len, value) = { + print_endline(bits_str(read_rip()) + ": W " + bits_str(addr) + " " + dec_str(len) + " " + bits_str(value)) +} + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/main.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/main.sail new file mode 100644 index 000000000..7bc360518 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/main.sail @@ -0,0 +1,60 @@ +register haltAddrReg : sbits(48) + +function clause __SetConfig("ha", value) = { haltAddrReg = get_slice_int(48, value, 0) } + +// Set 64 bit mode +val initialise_64_bit_mode : unit -> unit +function initialise_64_bit_mode () = { + let msr0 = read_msr(0); + let efer = Mk_ia32_eferbits(msr0[11 .. 0]); + let new_efer = [efer with lma = 0b1]; + write_msr(0, [msr0 with 11 .. 0 = new_efer.bits]); + seg_hidden_attrs[1] = [Mk_code_segment_descriptor_attributesbits(seg_hidden_attrs[1]) with l = 0b1].bits; +} + +function clause __SetConfig("set64bit", value) = { + // Ignore the value + initialise_64_bit_mode() +} +function clause __SetConfig(c, _) = throw(Emsg("Unrecognised config " + c)) +end __SetConfig + +val x86_step_print_halt : (sbits(48), bool, bool, int) -> bool +function x86_step_print_halt(haltAddr, printEachStep, printCount, count) = { + if read_rip() == haltAddr then { + return true + } else { + x86_fetch_decode_execute(); + if printEachStep then printState(); + if printCount then print_int("Steps = ", count + 1); + return false + } +} + +// Simulates the same ACL2 function +val x86_run_halt : (sbits(48), int, bool, bool, int) -> int +function x86_run_halt (haltAddr, maxSteps, printEachStep, printCount, count) = { + if maxSteps == 0 then { + return count + } else { + try { + let halt? : bool = x86_step_print_halt(haltAddr, printEachStep, printCount, count + 1); + if halt? then { + return count + } else { + return x86_run_halt(haltAddr, maxSteps - 1, printEachStep, printCount, count + 1) + } + } + catch { + Emsg(msg) => { print_endline(msg); return count } + } + } +} + +// A main function for C compilation +val main : unit -> unit +function main () = { + initialize_model(); + steps = x86_run_halt(haltAddrReg, 10000, not_bool(log_register_writes), false, 0); + print_int("Steps = ", steps); +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/memory_accessors.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/memory_accessors.sail new file mode 100644 index 000000000..1bf34a64a --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/memory_accessors.sail @@ -0,0 +1,113 @@ +$ifndef __X86_MEMORY_ACCESSORS +$define __X86_MEMORY_ACCESSORS + +$include "structures.sail" +$include "segmentation_structures.sail" +$include "linear_memory.sail" +$include "segmentation.sail" +// For prefixes bitfield definition: +$include "opcode_ext.sail" + +type address_size = {|2, 4, 8|} +type moffset_size = {|2, 4, 8|} +type base_reg_idx = range(0, 15) +type seg_reg_idx = range(0, 5) + +val address_aligned_p : (sbits(48), {|1, 2, 4, 6, 8, 10, 16, 32, 64|}, bool) -> bool + +val check_linear_memory_access : (proc_mode, address_size, {|1, 2, 4, 6, 8, 10, 16|}, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool, bool) -> unit + +function check_linear_memory_access (proc_mode, addr_size, nbytes, lin_addr, base_reg, seg_reg, access_kind, check_alignment?, mem_ptr?) = { + if not_bool(not_bool(check_alignment?) | address_aligned_p(truncate(lin_addr, 48), nbytes, mem_ptr?)) then { + x86_fault(":UNALIGNED-LINEAR-ADDRESS") + } else { + return () + } +} + +// TODO: Move the following functions? + +val select_base_register : (proc_mode, bits(8), bits(3), bits(2), sib) -> option(base_reg_idx) + +function select_base_register(proc_mode, rex_byte, r_m, mod_var, sib) = { + // TODO + None() +} + +val select_address_size : (proc_mode, option(prefixes)) -> address_size + +function select_address_size (proc_mode, prefixes) = { + let p4? : bool = match prefixes { + Some(prefixes) => 0x67 == prefixes[adr], + None() => false + }; + if in_64bit_mode(proc_mode) then { + if p4? then 4 else 8 + } else { + let cs_attr : bits(16) = seg_hidden_attrs[1]; + let cs_d : bits(1) = Mk_code_segment_descriptor_attributesbits(cs_attr)[d]; + if cs_d == 0b1 then { + if p4? then 2 else 4 + } else if p4? then 4 else 2 + } +} + +val select_moffset_size : (proc_mode, option(prefixes)) -> moffset_size + +function select_moffset_size (proc_mode, prefixes) = { + select_address_size (proc_mode, prefixes) +} + +val select_segment_register : (proc_mode, prefixes, bits(2), bits(3), sib) -> seg_reg_idx + +function select_segment_register (proc_mode, prefixes, mod_var, r_m, sib) = { + match prefixes[seg] { + 46 => 1, + 54 => 2, + 62 => 3, + 38 => 0, + 100 => 4, + 101 => 5, + _ => { + let addr_size : {|2, 4, 8|} = select_address_size(proc_mode, Some(prefixes)); + if addr_size == 2 then { + if not_bool(mod_var == 0b11) & (r_m == 0b010 | r_m == 0b011) then 2 else 3 + } else if (mod_var == 0b01 | mod_var == 0b10) & r_m == 0b101 | not_bool(mod_var == 0b11) & r_m == 0b100 & sib[base] == 0b100 then 2 else 3 + } + } +} + +val load_bytes_from_ea : forall 'nbytes, 'nbytes in {1, 2, 4, 6, 8, 10, 16}. + (proc_mode, address_size, int('nbytes), sbits(64), option(base_reg_idx), seg_reg_idx, string, bool, bool) -> bits(8 * 'nbytes) + +function load_bytes_from_ea (proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?) = { + let cs_attrs = Mk_code_segment_descriptor_attributesbits(seg_hidden_attrs[1]); + if not_bool(in_64bit_mode(proc_mode)) & seg_reg == 1 & r_x == ":R" & cs_attrs[r] == 0b0 then { + x86_fault(":EXECUTE-ONLY-CODE-SEGMENT") + } else { + let lin_addr : sbits(64) = ea_to_la(proc_mode, eff_addr, seg_reg, nbytes); + check_linear_memory_access(proc_mode, addr_size, nbytes, lin_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?); + // Use rb directly instead of rml_size; it returns a + // bitvector of the right width and performs the same + // checks for canonical addresses that rml_size does. + rb(nbytes, lin_addr, r_x) + } +} + +val store_bytes_to_ea : forall 'nbytes, 'nbytes in {1, 2, 4, 6, 8, 10, 16}. + (proc_mode, address_size, int('nbytes), sbits(64), option(base_reg_idx), seg_reg_idx, bits(8 * 'nbytes), bool, bool) -> unit + +function store_bytes_to_ea (proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, data, check_alignment?, mem_ptr?) = { + let seg_attrs = Mk_data_segment_descriptor_attributesbits(seg_hidden_attrs[seg_reg]); + if not_bool(in_64bit_mode(proc_mode)) & (seg_reg == 1 | seg_attrs[w] == 0b0) then { + x86_fault(":NON-WRITABLE-SEGMENT") + } else { + let lin_addr : sbits(64) = ea_to_la(proc_mode, eff_addr, seg_reg, nbytes); + check_linear_memory_access(proc_mode, addr_size, nbytes, lin_addr, base_reg, seg_reg, ":W", check_alignment?, mem_ptr?); + wb(nbytes, lin_addr, ":W", data) + } +} + +function observe_mem_write (addr, len) = () + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/modes.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/modes.sail new file mode 100644 index 000000000..0b603666a --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/modes.sail @@ -0,0 +1,37 @@ +$include "./register_readers_and_writers.sail" + +/*! Check whether we are in 64-bit mode. ++ Given the modeling assumption stated in @ ( see x86-modes ) , + this predicate discriminates between + 64-bit mode and the other two modes ( collectively, 32-bit mode ) . + Based on Intel manual, Mar ' 17, Vol. 3A, Sec. 2.2 ( near Fig. 2-3 ) , + the discrimination is based on the IA32_EFER.LME and CS.L bits: + if they are both 1, we are in 64-bit mode, + otherwise we are in 32-bit mode + ( protected mode if IA32_EFER.LME is 0, + compatibility mode if IA32_EFER.LME is 1 and CS.L is 0; + note that when IA32_EFER.LME is 0, CS.L should be 0, + according to Intel manual, Mar ' 17, Vol. 3A, Sec. 3.4.5 ) . +
++ This predicate does not include state invariants such as + the constraints imposed by the 64-bit mode consistency checks + described in Intel manual, Mar ' 17, Vol. 3A, Sec. 9.8.5. +
++ This predicate is useful as a hypothesis of theorems + about either 64-bit or 32-bit mode. +
++ Since @ ( ' ( xr :msr ... x86 ) ' ) returns a 64-bit value + but the IA32_EFER register consists of 12 bits. + So we use @ ( tsee n12 ) to make @ ( ' ia32_eferBits ' ) functions applicable. +
*/ +function n64_bit_modep () = { + let ia32_efer : bits(12) = truncate(read_msr(0), 12); + let ia32_efer_lma : bits(1) = Mk_ia32_eferbits(ia32_efer)[lma]; + let cs_attr : bits(16) = seg_hidden_attrs[1]; + let cs_l : bits(1) = Mk_code_segment_descriptor_attributesbits(cs_attr)[l]; + ia32_efer_lma == 0b1 & cs_l == 0b1 +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/mov.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/mov.sail new file mode 100644 index 000000000..1b0cf2409 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/mov.sail @@ -0,0 +1,292 @@ + +/*! Move scalar single/double precision floating-point values +Note: The MOVDQU, MOVUPS, and MOVUPD instructions perform 128-bit + unaligned loads or stores. They do not generate general-protection + exceptions ( #GP ) when operands are not aligned on a 16-byte + boundary. If alignment checking is enabled, alignment-check + exceptions ( #AC ) may or may not be generated depending on processor + implementation when data addresses are not aligned on an 8-byte + boundary.
*/ +val x86_movups_movupd_movdqu_op_en_rm : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_movups_movupd_movdqu_op_en_rm (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-MOVUPS/MOVUPD/MOVDQU-OP/EN-RM"; + let r_m : bits(3) = modr_m[r_m]; + let mod_var : bits(2) = modr_m[mod]; + let reg : bits(3) = modr_m[reg]; + let xmm_index : bits(4) = reg_index(reg, rex_byte, 0b10); + let p2 : bits(8) = prefixes[seg]; + let p4? : bool = 0x67 == prefixes[adr]; + let seg_reg : seg_reg_idx = select_segment_register(proc_mode, prefixes, mod_var, r_m, sib); + let inst_ac? : bool = true; + let (xmm_mem, increment_rip_by, addr) : (bits(128), {|0, 1, 2, 4|}, sbits(64)) = x86_operand_from_modr_m_and_sib_bytes(proc_mode, 0b1, 16, inst_ac?, false, seg_reg, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, 0b000); + let temp_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, bits_of_int(increment_rip_by, 48)); + let badlength? : option(int) = check_instruction_length(start_rip, temp_rip, 0b000); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + write_xmmi_size(0b10000, xmm_index, unsigned(xmm_mem)); + write_iptr(proc_mode, temp_rip) +} + +/*!F6/4:
+ MUL r/m8: AX := AL * r/m8
+
+ F7/4:
+ MUL r/m16: DX:AX := AX * r/m16
+ MUL r/m32: EDX:EAX := EAX * r/m32
+ MUL r/m64: RDX:RAX := RAX * r/m64
F6/5:
+ IMUL r/m8: AX := AL * r/m8
+
+ F7/5:
+ IMUL r/m16: DX:AX := AX * r/m16
+ IMUL r/m32: EDX:EAX := EAX * r/m32
+ IMUL r/m64: RDX:RAX := RAX * r/m64
0F AF:
+ IMUL r16, r/m16: r16 := r16 * r/m16
+ IMUL r32, r/m32: r32 := r32 * r/m32
+ IMUL r64, r/m64: r64 := r64 * r/m64
6B ib:
+ IMUL r16, r/m16, imm8
+ IMUL r32, r/m32, imm8
+ IMUL r64, r/m64, imm8
+
+ 69 iw:
+ IMUL r16, r/m16, imm16
+ IMUL r32, r/m32, imm32
+ IMUL r64, r/m64, imm32
We only support 64-bit, Compatibility, and 32-bit Protected Modes + for now.
+See @ ( see x86-modes ) .
*/ +val x86_operation_mode : unit -> proc_mode + +function x86_operation_mode () = { + if n64_bit_modep() then Mode_64bit else Mode_Compatibility +} + +bitfield prefixes : bits(52) = + {nxt : 51..44, adr : 43..36, opr : 35..28, seg : 27..20, rep : 19..12, lck : 11..4, num : 3..0} + +val is_ext_prefix_byte : (proc_mode, prefixes, bits(8), range(0, 15), bits(8)) -> bool +function is_ext_prefix_byte(proc_mode, prefixes, rex_byte, cnt, byte) = false + +val process_ext_prefix_byte : (proc_mode, prefixes, bits(8), range(0, 15), bits(8)) -> (prefixes, bits(8), bool) +function process_ext_prefix_byte(proc_mode, prefixes, rex_byte, cnt, byte) = (prefixes, rex_byte, false) + +val ext_one_byte_opcode_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> bool +function ext_one_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = false + +val ext_two_byte_opcode_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), bits(8), modr_m, sib) -> bool +function ext_two_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, mandatory_prefix, rex_byte, opcode, modr_m, sib) = false + +val ext_vex_0f_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), vex_prefixes, bits(8), modr_m, sib) -> bool +function ext_vex_0f_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, vex_prefixes, opcode, modr_m, sib) = false + +val ext_vex_0f38_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), vex_prefixes, bits(8), modr_m, sib) -> bool +function ext_vex_0f38_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, vex_prefixes, opcode, modr_m, sib) = false + +val ext_vex_0f3a_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), vex_prefixes, bits(8), modr_m, sib) -> bool +function ext_vex_0f3a_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, vex_prefixes, opcode, modr_m, sib) = false + +val ext_evex_0f_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), evex_prefixes, bits(8), modr_m, sib) -> bool +function ext_evex_0f_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, evex_prefixes, opcode, modr_m, sib) = false + +val ext_evex_0f38_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), evex_prefixes, bits(8), modr_m, sib) -> bool +function ext_evex_0f38_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, evex_prefixes, opcode, modr_m, sib) = false + +val ext_evex_0f3a_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), evex_prefixes, bits(8), modr_m, sib) -> bool +function ext_evex_0f3a_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, evex_prefixes, opcode, modr_m, sib) = false + +val unimplemented_x86_syscall_app_view : (proc_mode, sbits(48), sbits(48), bits(52), bits(8), bits(8), bits(8), bits(8)) -> unit +function unimplemented_x86_syscall_app_view (_, _, _, _, _, _, _, _) = throw(Syscall()) + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/or_spec.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/or_spec.sail new file mode 100644 index 000000000..36797fdda --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/or_spec.sail @@ -0,0 +1,56 @@ + +val gpr_or_spec_1 : (bits(8), bits(8), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function gpr_or_spec_1 (dst, src, input_rflags) = { + let result : bits(8) = logior(dst, src)[7 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_or_spec_2 : (bits(16), bits(16), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function gpr_or_spec_2 (dst, src, input_rflags) = { + let result : bits(16) = logior(dst, src)[15 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_or_spec_4 : (bits(32), bits(32), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function gpr_or_spec_4 (dst, src, input_rflags) = { + let result : bits(32) = logior(dst, src)[31 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_or_spec_8 : (bits(64), bits(64), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function gpr_or_spec_8 (dst, src, input_rflags) = { + let result : bits(64) = logior(dst, src)[63 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/physical_memory.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/physical_memory.sail new file mode 100644 index 000000000..455573ad6 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/physical_memory.sail @@ -0,0 +1,57 @@ +$include "./modes.sail" + +val physical_address_p : int -> bool + +function physical_address_p phy_addr = { + unsigned_byte_p(52, phy_addr) +} + +val rm_low_32 : int -> bits(32) + +function rm_low_32 addr = { + assert(not_bool(app_view)); + let byte0 : bits(8) = loghead(8, memi(bits_of_int(addr, 64))); + let byte1 : bits(8) = loghead(8, memi(bits_of_int(1 + addr, 64))); + let word0 : bits(16) = logior(bits_of_int(ash(unsigned(byte1), 8), 16), sail_zero_extend(byte0, 16)); + let byte2 : bits(8) = loghead(8, memi(bits_of_int(2 + addr, 64))); + let byte3 : bits(8) = loghead(8, memi(bits_of_int(3 + addr, 64))); + let word1 : bits(16) = logior(bits_of_int(ash(unsigned(byte3), 8), 16), sail_zero_extend(byte2, 16)); + let dword : bits(32) = logior(bits_of_int(ash(unsigned(word1), 16), 32), sail_zero_extend(word0, 32)); + dword +} + +val wm_low_32 : (bits(52), bits(32)) -> unit + +function wm_low_32 (addr, sailval) = { + assert(not_bool(app_view)); + let addr : int = unsigned(addr); + let byte0 : bits(8) = sailval[7 .. 0]; + let byte1 : bits(8) = sailval[15 .. 8]; + let byte2 : bits(8) = sailval[23 .. 16]; + let byte3 : bits(8) = sailval[31 .. 24]; + bang_memi(bits_of_int(addr, 64), byte0); + bang_memi(bits_of_int(1 + addr, 64), byte1); + bang_memi(bits_of_int(2 + addr, 64), byte2); + bang_memi(bits_of_int(3 + addr, 64), byte3) +} + +val rm_low_64 : int -> bits(64) + +function rm_low_64 addr = { + assert(not_bool(app_view)); + let dword0 : bits(32) = rm_low_32(addr); + let dword1 : bits(32) = rm_low_32(4 + addr); + let qword : bits(64) = logior(bits_of_int(ash(unsigned(dword1), 32), 64), sail_zero_extend(dword0, 64)); + qword +} + +val wm_low_64 : (bits(52), bits(64)) -> unit + +function wm_low_64 (addr, sailval) = { + assert(not_bool(app_view)); + let addr : int = unsigned(addr); + let dword0 : bits(32) = sailval[31 .. 0]; + let dword1 : bits(32) = sailval[63 .. 32]; + wm_low_32(bits_of_int(addr, 52), dword0); + wm_low_32(bits_of_int(4 + addr, 52), dword1) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prefix_modrm_sib_decoding.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prefix_modrm_sib_decoding.sail new file mode 100644 index 000000000..bb72d77be --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prefix_modrm_sib_decoding.sail @@ -0,0 +1,185 @@ +$include "./auxiliary.sail" +$include "./dispatch_macros.sail" + +/*! Functions to detect and decode legacy prefixes */ +val get_one_byte_prefix_array_code : bits(8) -> {|0, 1, 2, 3, 4|} + +function get_one_byte_prefix_array_code byte = { + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] : vector(256, dec, {|0, 1, 2, 3, 4|})[unsigned(loghead(8, byte))] +} + +/*! 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/ ' ) ) .
+ +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 ) )
+
+
+
+ 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...
+
+
+
+ + This is based on Intel manual, Jan ' 19, Volume 2, Tables 2-1 and 2-2, + as well as AMD manual, Dec ' 17, Volume 3, Tables A-33 and A-35. + When the address size is 32 or 64 bits, + Intel Table 2-2 and AMD Table A-35 apply: + a SIB byte is expected exactly when + ModR/M.mod is not #b11 and ModR/M.r/m is #b100. + When the address size is 16 bits, no SIB byte is expected. +
++ Intel Table 2-3 applies to 32-bit addresses, + but Section 2.2.1.3 says that it also applies to 64-bit addresses. + AMD Table A-35 mentions both 32-bit and 64-bit addressing. +
++ Intel manual, Jan ' 19, Volume 2, Table 2-7 is not very clear, + giving the impression that a SIB byte may be required + when Mod = 00 and R/M = 101 ( last column of first row ) . + But AMD manual, Dec ' 17, Volume 3, Table 1-16 + ( which corresponds to Intel Table 2-7 ) clearly says that + no SIB byte is present in the first row. + Intel ' s last column of first row means that, + in order to use @ ( ' Disp32 ' ) instead of @ ( ' RIP+Disp32 ' ) , + 64-bit mode must use the encoding with SIB in the second row. +
++ The second argument of this function says whether + the address size is 16 bits or not ( i.e. 32 or 64 bits ) . + In 64-bit mode, this argument is always @ ( ' nil ' ) . + In 32-bit mode, this argument may be @ ( ' t ' ) or @ ( ' nil ' ) . +
*/ +val x86_decode_sib_p : (modr_m, bool) -> bool + +function x86_decode_sib_p (modr_m, n16_bit_addressp) = { + not_bool(n16_bit_addressp) & (let r_m : bits(3) = modr_m[r_m] in + let mod_var : bits(2) = modr_m[mod] in r_m == 0b100 & not_bool(mod_var == 0b11)) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prelude.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prelude.sail new file mode 100644 index 000000000..db136d67a --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/prelude.sail @@ -0,0 +1,711 @@ +/*-----------------------------------------------------------------*/ +// This file contains the handwritten functions for the ACL2 +// translation. +/*-----------------------------------------------------------------*/ + +$ifndef __X86_PRELUDE +$define __X86_PRELUDE + +$ifndef _DEFAULT_DEC + default Order dec +$endif + +$includeIn 64-bit mode, in addition to generating 64-bit operand sizes, + the REX prefix is used to reference registers R8 to R15. Instructions that + include REX prefixes can access these registers if the relevant W, R, X, or + B bit in the REX prefix is set. E.g., let R be the relevant bit in the REX + prefix and let R be set --- so @ ( ' index ' ) = @ ( ` *r* ` ) for this function. If + @ ( ' reg ' ) = 0 ( which, in the non-REX world, would refer to rAX ) , + @ ( ' reg-index ' ) would give us the register index corresponding to the + register r8. If R is not set, @ ( ' reg-index ' ) will give us the index + corresponding to rAX.
+ +In 32-bit mode, the REX prefix is absent. This function can be used in + 32-bit mode, by passing 0 as REX.
*/ +val reg_index : (bits(3), bits(8), bits(2)) -> bits(4) + +function reg_index (reg, rex_byte, index) = { + if logbitp(unsigned(index), rex_byte) then { + logior(0x8, sail_zero_extend(reg, 4)) + } else { + sail_zero_extend(reg, 4) + } +} + +/*! Functions to read/write 8/16/32/64 bits into the registers +@ ( see rr08 ) , @ ( see rr16 ) , @ ( see rr32 ) , and @ ( see rr64 ) + will read the contents of the GPRs as natural numbers. Remember that + @ ( ' rgfi ' ) will return an @ ( see i64p ) value.
+ +Here is an example:
+\ ( rr64 0 x86\ ) returns @ ( ' 18446744073709551615 ' ) and + ( rgfi 0 x86 ) returns @ ( ' -1 ' ) . Note that + @ ( ' 18446744073709551615 ' ) is a bignum in CCL. This is precisely the + reason why we declared registers as @ ( ' i64p ' ) instead of @ ( ' n64p ' ) ; + @ ( ' -1 ' ) would be stored as a bignum if the registers were + @ ( ' n64p ' ) .
+ +Similarly, @ ( see wr08 ) , @ ( see wr16 ) , @ ( see wr32 ) , and @ ( see wr64 ) + are used to write natural numbers into the GPRs.
+Read from byte general-purpose registers +Source: Intel Manuals, Vol. 1, Section + 3.4.1.1 ( General-Purpose Registers in 64-bit Mode )
+ +In 64-bit mode, there are limitations on accessing byte + registers. An instruction cannot reference legacy high-bytes ( for example: AH, + BH, CH, DH ) and one of the new byte registers at the same time ( for example: + the low byte of the RAX register ) . However, instructions may reference legacy + low-bytes ( for example: AL, BL, CL or DL ) and new byte registers at the same + time ( for example: the low byte of the R8 register, or R8L ) . The architecture + enforces this limitation by changing high-byte references ( AH, BH, CH, DH ) to + low byte references ( BPL, SPL, DIL, SIL: the low 8 bits for RBP, RSP, RDI and + RSI ) for instructions using a REX prefix.+ +
In other words, without the REX prefix, indices 0-7 refer to byte registers + AL, CL, DL, BL, AH, CH, DH, and BH, whereas with the REX prefix, indices 0-15 + refer to AL, CL, DL, BL, SPL, BPL, SIL, DIL, R8L, R9L, R10L, R11L, R12L, R13L, + R14L, R15L. This applies to 64-bit mode.
+ +In 32-bit mode, this function is called with 0 as REX.
*/ +val rr08 : (bits(4), bits(8)) -> bits(8) + +function rr08 (reg, rex) = { + let reg : nat = unsigned(reg); + if not_bool(rex == 0x00) | reg < 4 then { + let qword : sbits(64) = rgfi(reg); + truncate(qword, 8) + } else { + let qword : sbits(64) = rgfi(unsigned(bits_of_int(reg - 4, 4))); + qword[15 .. 8] + } +} + +/*! Writing to byte general-purpose registers +Note Intel Vol. 1 Sec. 3.4.1.1 + p. 3-17, which says the following about 64-bit mode:
+ +8-bit and 16-bit operands generate an 8-bit or 16-bit result. + The upper 56 or 48 bits ( respectively ) of the destination general-purpose + register are not modified by the operation.
+ +This is also confirmed by AMD manual, Jun ' 15, Vol. 3, App. B.1, under + ‘No Extension of 8-Bit and 16-Bit Results’.
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by + the following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose + register are not preserved when switching from 64-bit mode to a 32-bit + mode ( to protected mode or compatibility mode ) . Software must not depend on + these bits to maintain a value after a 64-bit to 32-bit mode + switch.
+ +In 32-bit mode, this function is called with 0 as REX. Since in 32-bit + mode the high 32 bits of general-purpose registers are not accessible, it + is fine for this function to leave those bits unchanged, as opposed to, for + example, setting them to undefined values as done by the semantic functions + of certain instructions for certain flags. The switching from 32-bit mode + to 64-bit mode ( when modeled ) will set the high 32 bits of general-purpose + registers to undefined values.
*/ +val wr08 : (bits(4), bits(8), bits(8)) -> unit + +function wr08 (reg, rex, byte) = { + let reg : nat = unsigned(reg); + if not_bool(rex == 0x00) | reg < 4 then { + let qword : sbits(64) = rgfi(reg); + write_rgfi(reg, changeSlice(qword[63 .. 0], 0, 8, byte)) + } else { + let index : bits(4) = bits_of_int(unsigned(bits_of_int(reg, 4)) - 4, 4); + let qword : sbits(64) = rgfi(unsigned(index)); + write_rgfi(unsigned(index), changeSlice(qword[63 .. 0], 8, 8, byte)) + } +} + +/*! Read from word general-purpose registers */ +val rr16 : bits(4) -> bits(16) + +function rr16 reg = { + truncate(rgfi(unsigned(reg)), 16) +} + +/*! Write to word general-purpose registers +Note Intel Vol. 1 Sec. 3.4.1.1 + p. 3-17, which says the following about 64-bit mode:
+ +8-bit and 16-bit operands generate an 8-bit or 16-bit result. + The upper 56 or 48 bits ( respectively ) of the destination general-purpose + register are not modified by the operation.
+ +This is also confirmed by AMD manual, Jun ' 15, Vol. 3, App. B.1, under + ‘No Extension of 8-Bit and 16-Bit Results’.
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by + the following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose + register are not preserved when switching from 64-bit mode to a 32-bit + mode ( to protected mode or compatibility mode ) . Software must not depend on + these bits to maintain a value after a 64-bit to 32-bit mode + switch.
+ +This function is used both in 64-bit mode and in 32-bit mode. Since in + 32-bit mode the high 32 bits of general-purpose registers are not + accessible, it is fine for this function to leave those bits unchanged, as + opposed to, for example, setting them to undefined values as done by the + semantic functions of certain instructions for certain flags. The switching + from 32-bit mode to 64-bit mode ( when modeled ) will set the high 32 bits of + general-purpose registers to undefined values.
*/ +val wr16 : (bits(4), bits(16)) -> unit + +function wr16 (reg, sailval) = { + let qword : sbits(64) = rgfi(unsigned(reg)); + write_rgfi(unsigned(reg), changeSlice(qword[63 .. 0], 0, 16, sailval)) +} + +/*! Read from doubleword general-purpose registers */ +val rr32 : bits(4) -> bits(32) + +function rr32 reg = { + truncate(rgfi(unsigned(reg)), 32) +} + +/*! Write to doubleword general-purpose registers +Note Intel Vol. 1 Sec. 3.4.1.1 + p. 3-17, which says the following about 64-bit mode:
+ +32-bit operands generate a 32-bit result, zero-extended to a + 64-bit result in the destination general-purpose + register.
+ +This is also confirmed by AMD manual, Jun ' 15, Vol. 3, App. B.1, under + ` No Extension of 8-Bit and 16-Bit Results ' .
+ +In 32-bit mode, the upper 32 bits are undefined, as specified by the + following quote from the same page as above:
+ +Because the upper 32 bits of 64-bit general-purpose registers are + undefined in 32-bit modes, the upper 32 bits of any general-purpose + register are not preserved when switching from 64-bit mode to a 32-bit + mode ( to protected mode or compatibility mode ) . Software must not depend on + these bits to maintain a value after a 64-bit to 32-bit mode + switch.
+ +This function is used both in 64-bit mode and in 32-bit mode. Since in + 32-bit mode the high 32 bits of general-purpose registers are not + accessible, it is fine for this function to set those bits to 0, as opposed + to, for example, setting them to undefined values as done by the semantic + functions of certain instructions for certain flags. The switching from + 32-bit mode to 64-bit mode ( when modeled ) will set the high 32 bits of + general-purpose registers to undefined values.
*/ +val wr32 : (bits(4), bits(32)) -> unit + +function wr32 (reg, sailval) = { + write_rgfi(unsigned(reg), sail_zero_extend(sailval, 64)) +} + +/*! Read from quadword general-purpose registers +This function is used only in 64-bit mode.
*/ +val rr64 : bits(4) -> bits(64) + +function rr64 reg = { + rgfi(unsigned(reg)) +} + +/*! Write to quadword general-purpose registers +This function is used only in 64-bit mode.
*/ +val wr64 : (bits(4), bits(64)) -> unit + +function wr64 (reg, sailval) = { + write_rgfi(unsigned(reg), sailval) +} + +/*! Read form byte, word, doubleword, or quadword + general-purpose register */ +val rgfi_size : (bits(4), bits(4), bits(8)) -> bits(64) + +function rgfi_size (nbytes, index, rex) = { + (match nbytes { + 1 => sail_zero_extend(rr08(index, rex), 64), + 2 => sail_zero_extend(rr16(index), 64), + 4 => sail_zero_extend(rr32(index), 64), + 8 => rr64(index), + _ => 0x0000000000000000 + }) : bits(64) +} + +/*! Write to byte, word, doubleword, or quadword + general-purpose register */ +val write_rgfi_size : (bits(4), bits(4), bits(64), bits(8)) -> unit + +function write_rgfi_size (nbytes, index, sailval, rex) = { + (match nbytes { + 1 => wr08(index, rex, truncate(sailval, 8)), + 2 => wr16(index, truncate(sailval, 16)), + 4 => wr32(index, truncate(sailval, 32)), + 8 => wr64(index, sailval), + _ => () + }) : unit +} + +/*! Functions to read/write into the MMX registers +From Section 12.2, Intel Manual, Volume 3 \ ( System + Programming\ ) :
+ +The MMX state consists of eight 64-bit registers \ ( MM0 + through MM7\ ) . These registers are aliased to the low 64-bits \ ( bits 0 + through 63\ ) of floating-point registers R0 through R7 \ ( see Figure + 12-1\ ) . Note that the MMX registers are mapped to the physical + locations of the floating-point registers \ ( R0 through R7\ ) , not to + the relative locations of the registers in the floating-point register + stack \ ( ST0 through ST7\ ) . As a result, the MMX register mapping is + fixed and is not affected by value in the Top Of Stack \ ( TOS\ ) field + in the floating-point status word \ ( bits 11 through 13\ ) .
+ +From Section 12.2, Intel Manual, Volume 3 \ ( System Programming\ )
: + +When a value is written into an MMX register using an MMX + instruction, the value also appears in the corresponding + floating-point register in bits 0 through 63. Likewise, when a + floating-point value written into a floating-point register by a x87 + FPU, the low 64 bits of that value also appears in a the corresponding + MMX register.
+ +The execution of MMX instructions have several side effects on + the x87 FPU state contained in the floating-point registers, the x87 + FPU tag word, and the x87 FPU status word. These side effects are as + follows:
+ +* When an MMX instruction writes a value into an MMX register, + at the same time, bits 64 through 79 of the corresponding + floating-point register are set to all 1s.
+ +* When an MMX instruction \ ( other than the EMMS instruction\ ) is + executed, each of the tag fields in the x87 FPU tag word is set to 00B + \ ( valid\ ) . \ ( See also Section 12.2.1, Effect of MMX, x87 FPU, FXSAVE, + and FXRSTOR Instructions on the x87 FPU Tag Word.\ )
+ +* When the EMMS instruction is executed, each tag field in the + x87 FPU tag word is set to 11B \ ( empty ) \.
+ +* Each time an MMX instruction is executed, the TOS value is + set to 000B. Execution of MMX instructions does not affect the other + bits in the x87 FPU status word \ ( bits 0 through 10 and bits 14 and + 15\ ) or the contents of the other x87 FPU registers that comprise the + x87 FPU state \ ( the x87 FPU control word, instruction pointer, data + pointer, or opcode registers\ ) .
+Functions to read/write 32/64/128/256/512 bits into the + XMM/YMM/ZMM registers +Source: Intel Vol. 1, Section 15.5: ACCESSING XMM, YMM AND ZMM + REGISTERS
+ +The lower 128 bits of a YMM register is aliased to the + corresponding XMM register. Legacy SSE instructions ( i.e., SIMD + instructions operating on XMM state but not using the VEX prefix, + also referred to non-VEX encoded SIMD instructions ) will not access + the upper bits ( MAXVL-1:128 ) of the YMM registers. AVX and FMA + instructions with a VEX prefix and vector length of 128-bits zeroes + the upper 128 bits of the YMM register.+ +
Upper bits of YMM registers ( 255:128 ) can be read and + written to by many instructions with a VEX.256 prefix. XSAVE and + XRSTOR may be used to save and restore the upper bits of the YMM + registers.+ +
The lower 256 bits of a ZMM register are aliased to the + corresponding YMM register. Legacy SSE instructions ( i.e., SIMD + instructions operating on XMM state but not using the VEX prefix, + also referred to non-VEX encoded SIMD instructions ) will not access + the upper bits ( MAXVL-1:128 ) of the ZMM registers, where MAXVL is + maximum vector length ( currently 512 bits ) . AVX and FMA instructions + with a VEX prefix and vector length of 128-bits zero the upper 384 + bits of the ZMM register, while the VEX prefix and vector length of + 256-bits zeroes the upper 256 bits of the ZMM register. Upper bits + of ZMM registers ( 511:256 ) can be read and written to by instructions + with an EVEX.512 prefix.+ +
@ ( see rz32 ) , @ ( see rz64 ) , @ ( see rz128 ) , @ ( see rz256 ) , and @ ( see + rz512 ) will read the contents of the ZMMs as natural numbers.
+ +Similarly, @ ( see wz32 ) , @ ( see wz64 ) , @ ( see wz128 ) , @ ( see wz256 ) , + and @ ( see wz512 ) are used to write natural numbers into the + ZMMs.
*/ +val rz32 : bits(5) -> bits(32) + +function rz32 reg = { + truncate(zmms[unsigned(reg)], 32) +} + +val rz64 : bits(5) -> bits(64) + +function rz64 reg = { + truncate(zmms[unsigned(reg)], 64) +} + +val rz128 : bits(5) -> bits(128) + +function rz128 reg = { + truncate(zmms[unsigned(reg)], 128) +} + +/*! Write @ ( ' val ' ) to low 32 bits of a ZMM register +Upper bits: For XMM registers, upper @ ( ` ( - 512 + 128 ) ` ) bits are preserved if @ ( ' regtype ' ) is @ ( ' *xmm-access* ' ) ; + for @ ( ' *vex-xmm-access* ' ) , these bits are zeroed out. For + @ ( ' *ymm-access* ' ) , upper @ ( ` ( - 512 256 ) ` ) bits are zeroed + out. For @ ( ' *zmm-access* ' ) , no upper bits are zeroed out.
*/ +struct struct_wz32 = {regtype : int} + +val wz32 : (bits(5), bits(32), struct_wz32) -> unit + +function wz32 (reg, sailval, keywords) = { + let data : bits(512) = zmms[unsigned(reg)]; + let regtype : bits(3) = bits_of_int(keywords.regtype, 3); + let data : bits(512) = { + match regtype { + 2 => sail_zero_extend(loghead(128, data), 512), + 3 => sail_zero_extend(loghead(256, data), 512), + _ => data + } + }; + zmms[unsigned(reg)] = changeSlice(data, 0, 32, sailval) +} + +/*! Write @ ( ' val ' ) to low 64 bits of a ZMM register +Upper bits: For XMM registers, upper @ ( ` ( - 512 + 128 ) ` ) bits are preserved if @ ( ' regtype ' ) is @ ( ' *xmm-access* ' ) ; + for @ ( ' *vex-xmm-access* ' ) , these bits are zeroed out. For + @ ( ' *ymm-access* ' ) , upper @ ( ` ( - 512 256 ) ` ) bits are zeroed + out. For @ ( ' *zmm-access* ' ) , no upper bits are zeroed out.
*/ +struct struct_wz64 = {regtype : int} + +val wz64 : (bits(5), bits(64), struct_wz64) -> unit + +function wz64 (reg, sailval, keywords) = { + let data : bits(512) = zmms[unsigned(reg)]; + let regtype : bits(3) = bits_of_int(keywords.regtype, 3); + let data : bits(512) = { + match regtype { + 2 => sail_zero_extend(loghead(128, data), 512), + 3 => sail_zero_extend(loghead(256, data), 512), + _ => data + } + }; + zmms[unsigned(reg)] = changeSlice(data, 0, 64, sailval) +} + +/*! Write @ ( ' val ' ) to low 128 bits of a ZMM register +Upper bits: For XMM registers, upper @ ( ` ( - 512 + 128 ) ` ) bits are preserved if @ ( ' regtype ' ) is @ ( ' *xmm-access* ' ) ; + for @ ( ' *vex-xmm-access* ' ) , these bits are zeroed out. For + @ ( ' *ymm-access* ' ) , upper @ ( ` ( - 512 256 ) ` ) bits are zeroed + out. For @ ( ' *zmm-access* ' ) , no upper bits are zeroed out.
*/ +struct struct_wz128 = {regtype : int} + +val wz128 : (bits(5), bits(128), struct_wz128) -> unit + +function wz128 (reg, sailval, keywords) = { + let data : bits(512) = zmms[unsigned(reg)]; + let regtype : bits(3) = bits_of_int(keywords.regtype, 3); + let data : bits(512) = { + match regtype { + 2 => sail_zero_extend(loghead(128, data), 512), + 3 => sail_zero_extend(loghead(256, data), 512), + _ => data + } + }; + zmms[unsigned(reg)] = changeSlice(data, 0, 128, sailval) +} + +/*! Functions to read/write 32/64/128 bits into the XMM + registers ( to be used by non-VEX encoded 128-bit SIMD instructions ) +These functions are meant to be used by instructions that + do not use a VEX or EVEX prefix --- these functions preserve the + upper bits of ZMM registers. For instructions that use these + prefixes and zero out these bits instead, see @ ( see + ZMMs-Reads-and-Writes ) .
+ +Note that the index for accessing the XMM registers is 4-bits + wide ( as opposed to the 5-bit index for ZMM registers --- see @ ( see + ZMMs-Reads-and-Writes ) ) because only 16 XMM registers were supported + initially in the 64-bit mode ( and 8 in the 32-bit mode ) .
+ +Source: Intel Vol. 2, Section 2.3.10 --- AVX Instructions + and Upper 128-bits of YMM registers:
+ +If an instruction with a destination XMM register is + encoded with a VEX prefix, the processor zeroes the upper + bits ( above bit 128 ) of the equivalent YMM register. Legacy SSE + instructions without VEX preserve the upper bits.+ +
Functions @ ( see rx32 ) , @ ( see rx64 ) , and @ ( see rx128 ) will read the + contents of the XMMs ( i.e., low 128 bits of ZMMs ) as natural + numbers.
+ +Similarly, functions @ ( see wx32 ) , @ ( see wx64 ) , and @ ( see wx128 ) + are used to write natural numbers into the XMMs ( and preserve the + upper bits of ZMMs ) .
*/ +val rx32 : bits(4) -> bits(32) + +function rx32 reg = { + rz32(sail_zero_extend(reg, 5)) +} + +val rx64 : bits(4) -> bits(64) + +function rx64 reg = { + rz64(sail_zero_extend(reg, 5)) +} + +val rx128 : bits(4) -> bits(128) + +function rx128 reg = { + rz128(sail_zero_extend(reg, 5)) +} + +/*! Write @ ( ' val ' ) to low 32 bits of a ZMM register; upper + bits are preserved. */ +val wx32 : (bits(4), bits(32)) -> unit + +function wx32 (reg, sailval) = { + wz32(sail_zero_extend(reg, 5), sailval, struct { regtype = 1 }) +} + +/*! Write @ ( ' val ' ) to low 64 bits of a ZMM register; upper + bits are preserved. */ +val wx64 : (bits(4), bits(64)) -> unit + +function wx64 (reg, sailval) = { + wz64(sail_zero_extend(reg, 5), sailval, struct { regtype = 1 }) +} + +/*! Write @ ( ' val ' ) to low 128 bits of a ZMM register; upper + bits are preserved. */ +val wx128 : (bits(4), bits(128)) -> unit + +function wx128 (reg, sailval) = { + wz128(sail_zero_extend(reg, 5), sailval, struct { regtype = 1 }) +} + +val xmmi_size : (bits(5), bits(4)) -> bits(128) + +function xmmi_size (nbytes, index) = { + (match nbytes { + 4 => sail_zero_extend(rx32(index), 128), + 8 => sail_zero_extend(rx64(index), 128), + 16 => rx128(index), + _ => 0x00000000000000000000000000000000 + }) : bits(128) +} + +/*! Write @ ( ' val ' ) to low @ ( ' nbytes ' ) of an XMM register; + upper bits of the ZMM register are preserved. */ +val write_xmmi_size : (bits(5), bits(4), int) -> unit + +function write_xmmi_size (nbytes, index, sailval) = { + (match nbytes { + 4 => wx32(index, bits_of_int(sailval, 32)), + 8 => wx64(index, bits_of_int(sailval, 64)), + 16 => wx128(index, bits_of_int(sailval, 128)), + _ => () + }) : unit +} + +/*! Get a unique unknown to be used when reasoning about + undefined values in the processor +See @ ( see characterizing-undefined-behavior ) for more + details.
+ +The accessor and updater functions of the @ ( ' undef ' ) field are + untouchable so that the only way to create a new seed for unknowns + is via this function.
*/ +val undef_read : unit -> nat + +function undef_read () = { + undef_read_logic() +} + +/*! Reading from and writing to the @ ( ' rflags ' ) register in the @ ( ' x86 ' ) state +We define convenient macros @ ( ' flgi ' ) and @ ( ' !flgi ' ) to read a + flag ' s value and to write a flag ' s value into the @ ( ' rflags ' ) field in the + @ ( ' x86 ' ) state, respectively. Additionally, @ ( ' !flgi-undefined ' ) can be + used to write an undefined value into a particular flag.
*/ +val undef_flg_logic : unit -> nat + +function undef_flg_logic () = { + undef_read() +} + +val undef_flg : unit -> bits(1) + +function undef_flg () = { + let sailval : nat = undef_flg_logic(); + bits_of_int(sailval, 1) +} + +/*! Writing user rflags \ ( CF, PF, AF, ZF, SF, and OF\ ) , + including undefined ones, to the x86 state +We set the undefined flags, which are indicated by + @ ( ' mask ' ) , to the value returned by @ ( see undef-read ) .
*/ +val write_user_rflags : (rflagsbits, rflagsbits) -> unit + +function write_user_rflags (user_flags_vector, undefined_mask) = { + let input_rflags : rflagsbits = rflags; + if undefined_mask[cf] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with cf = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with cf = user_flags_vector[cf]]; + rflags = new_rflags + }; + if undefined_mask[pf] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with pf = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with pf = user_flags_vector[pf]]; + rflags = new_rflags + }; + if undefined_mask[af] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with af = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with af = user_flags_vector[af]]; + rflags = new_rflags + }; + if undefined_mask[zf] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with zf = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with zf = user_flags_vector[zf]]; + rflags = new_rflags + }; + if undefined_mask[sf] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with sf = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with sf = user_flags_vector[sf]]; + rflags = new_rflags + }; + if undefined_mask[of] == 0b1 then { + let rflags_var : rflagsbits = rflags; + let sailval : bits(1) = undef_flg(); + let new_rflags : rflagsbits = [rflags_var with of = sailval]; + rflags = new_rflags + } else { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with of = user_flags_vector[of]]; + rflags = new_rflags + }; + if log_register_writes then log_rflags_write(); + () +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/register_types.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/register_types.sail new file mode 100644 index 000000000..5e9636070 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/register_types.sail @@ -0,0 +1,12 @@ +$ifndef __X86_REGISTER_TYPES +$define __X86_REGISTER_TYPES + +type regtype = sbits(64) + +val regval_from_reg : regtype -> sbits(64) +function regval_from_reg(r) = r + +val regval_into_reg : sbits(64) -> regtype +function regval_into_reg(v) = v + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/registers.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/registers.sail new file mode 100644 index 000000000..06f973d89 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/registers.sail @@ -0,0 +1,154 @@ +$ifndef __X86_REGISTERS +$define __X86_REGISTERS + +$include "structures.sail" + +// Although one would expect rflags to be 64 bits, the ACL2 models +// it as 32 bits +register rflags : rflagsbits + +register rip : regtype + +// In ACL2 rgfi() is often called with a `defconst` argument. E.g. +// ` (rgfi *rax*) ` +// Unfortunately the translator currently translates these as number literals. +// So (ignoring the dummy x86 variable) the above will become: +// ` rgfi(0)` +// To find the number *rax* maps to, one can simply type `*rax*` into and ACL2 +// terminal with the x86 ISA project loaded. +// +// Each of the gprs is defined separately below, as opposed to a vector of +// bitvectors as other registers are. This is so they play nicely with +// concurrency tools. It also means rgfi() and write_rgfi() must work out +// which reg to access themselves, instead of indexing an array. +// +// See here for some hints on rgfi: +// http://www.cs.utexas.edu/users/moore/acl2/manuals/current/manual/index-seo.php/X86ISA____GPRS-READS-AND-WRITES?path=3532/29063/10660/6710/25183 + +register rax : regtype +register rbx : regtype +register rcx : regtype +register rdx : regtype +register rsi : regtype +register rdi : regtype +register rsp : regtype +register rbp : regtype +register r8 : regtype +register r9 : regtype +register r10 : regtype +register r11 : regtype +register r12 : regtype +register r13 : regtype +register r14 : regtype +register r15 : regtype + +val read_gpr : range(0, 15) -> regtype +function read_gpr(idx) = { + match idx { + 0 => rax, + 1 => rcx, + 2 => rdx, + 3 => rbx, + 4 => rsp, + 5 => rbp, + 6 => rsi, + 7 => rdi, + 8 => r8, + 9 => r9, + 10 => r10, + 11 => r11, + 12 => r12, + 13 => r13, + 14 => r14, + 15 => r15 + } +} + +val write_gpr : (range(0, 15), regtype) -> unit +function write_gpr(idx, value) = { + match idx { + 0 => rax = value, + 1 => rcx = value, + 2 => rdx = value, + 3 => rbx = value, + 4 => rsp = value, + 5 => rbp = value, + 6 => rsi = value, + 7 => rdi = value, + 8 => r8 = value, + 9 => r9 = value, + 10 => r10 = value, + 11 => r11 = value, + 12 => r12 = value, + 13 => r13 = value, + 14 => r14 = value, + 15 => r15 = value + }; + if log_register_writes then log_gpr_write(idx); +} + +/* +Model Specific Registers + +Footnote b in section 5.1 of Goel thesis says only 6 MSR registers +(I think they mean 7) are thus far defined in the model. For now, +we enumerate some of them explicitely here. Fortuantely, the indexes +range from 0..6, and we can lump them into a vector of registers + +TODO: make this enumeration automatic +*/ +register msrs : vector(7, dec, bits(64)) // 64 bits as per table 5.1 in thesis + +/* +seg-visible registers etc.. From `concrete-state.lisp` we find +there are 6 x 16bit registers + +TODO: make this automatic (including the number 6, which is a global +constant but subject to change) + +TODO: in `xr` in ACL2 we have things like seg_hidden_attri* (i.e. +with a trailing asterisk). Why is this? +*/ +register seg_visibles : vector(6, dec, bits(16)) + +register seg_hidden_attrs : vector(6, dec, bits(16)) + +register seg_hidden_bases : vector(6, dec, bits(64)) + +register seg_hidden_limits : vector(6, dec, bits(32)) + +/* +zmm registers. From `concrete-state.lisp` we find there are +32 x 512bit registers + +TODO: make this automatic (including the number 32, which is a global +constant but subject to change) +*/ +register zmms : vector (32, dec, bits(512)) + +/* +Control registers. From `concrete-state.lisp` we find there are +17 x 64bit registers + +TODO: make this automatic (including the number 17, which is a global +constant but subject to change) +*/ +register ctrs : vector (17, dec, bits(64)) + +/* +We can use `:pe ...` from `stri` and find we need the following. +*/ +register strs : vector(2, dec, bits(80)) + +/* +ssr +*/ +register ssr_visibles : vector(2, dec, bits(16)) + +register ssr_hidden_bases : vector(2, dec, bits(64)) + +register ssr_hidden_limits : vector(2, dec, bits(32)) + +register ssr_hidden_attrs : vector(2, dec, bits(16)) + +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rflags_spec.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rflags_spec.sail new file mode 100644 index 000000000..94bac4872 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rflags_spec.sail @@ -0,0 +1,327 @@ +$include "./application_level_memory.sail" + +/*!General @ ( ' CF ' ) Specification ( Source: Intel Manuals, + Vol. 1, Section 3.4.3.1 ) :
+ +Carry flag Set if an arithmetic operation generates a + carry or a borrow out of the most-significant bit of the result; + cleared otherwise. This flag indicates an overflow condition for + unsigned-integer arithmetic. It is also used in multiple-precision + arithmetic.
*/ +val general_cf_spec_fn : (nat, nat) -> bits(1) + +function general_cf_spec_fn (result_nbits, raw_result) = { + bool_to_bits(not_bool(unsigned_byte_p(result_nbits, raw_result))) +} + +val cf_spec8 : bits(9) -> bits(1) + +function cf_spec8 raw_result = { + bool_to_bits(not_bool(unsigned_byte_p(8, unsigned(raw_result)))) +} + +val cf_spec16 : bits(17) -> bits(1) + +function cf_spec16 raw_result = { + bool_to_bits(not_bool(unsigned_byte_p(16, unsigned(raw_result)))) +} + +val cf_spec32 : bits(33) -> bits(1) + +function cf_spec32 raw_result = { + bool_to_bits(not_bool(unsigned_byte_p(32, unsigned(raw_result)))) +} + +val cf_spec64 : bits(65) -> bits(1) + +function cf_spec64 raw_result = { + bool_to_bits(not_bool(unsigned_byte_p(64, unsigned(raw_result)))) +} + +/*!General @ ( ' OF ' ) Specification ( Source: Intel Manuals, + Vol. 1, Section 3.4.3.1 ) :
+ +Overflow flag Set if the integer result is too large a + positive number or too small a negative number ( excluding the + sign-bit ) to fit in the destination operand; cleared otherwise. This + flag indicates an overflow condition for signed-integer ( two s + complement ) arithmetic.
*/ +val general_of_spec_fn : (nat, int) -> bits(1) + +function general_of_spec_fn (result_nbits, signed_raw_result) = { + bool_to_bits(not_bool(signed_byte_p(result_nbits, signed_raw_result))) +} + +val of_spec8 : sbits(9) -> bits(1) + +function of_spec8 signed_raw_result = { + bool_to_bits(not_bool(signed_byte_p(8, signed(signed_raw_result)))) +} + +val of_spec16 : sbits(17) -> bits(1) + +function of_spec16 signed_raw_result = { + bool_to_bits(not_bool(signed_byte_p(16, signed(signed_raw_result)))) +} + +val of_spec32 : sbits(33) -> bits(1) + +function of_spec32 signed_raw_result = { + bool_to_bits(not_bool(signed_byte_p(32, signed(signed_raw_result)))) +} + +val of_spec64 : sbits(65) -> bits(1) + +function of_spec64 signed_raw_result = { + bool_to_bits(not_bool(signed_byte_p(64, signed(signed_raw_result)))) +} + +/*!General @ ( ' ZF ' ) Specification ( Source: Intel Manuals, + Vol. 1, Section 3.4.3.1 ) :
+ +Zero flag Set if the result is zero; cleared + otherwise.
*/ +val zf_spec : nat -> bits(1) + +function zf_spec result = { + if result == 0 then 0b1 else 0b0 +} + +val bitcount8 : bits(8) -> nat + +function bitcount8 x = { + if x == 0 then 0 else { + nat_of_int(unsigned(loghead(1, x)) + check_range(0, 8, bitcount8(bits_of_int(logtail(1, unsigned(x)), 8)))) + } +} + +val pf_spec8 : bits(8) -> bits(1) + +function pf_spec8 result = { + bool_to_bits(not_bool(logbitp(0, logcount(unsigned(result))))) +} + +val pf_spec16 : bits(16) -> bits(1) + +function pf_spec16 result = { + bool_to_bits(not_bool(logbitp(0, logcount(unsigned(loghead(8, result)))))) +} + +val pf_spec32 : bits(32) -> bits(1) + +function pf_spec32 result = { + bool_to_bits(not_bool(logbitp(0, logcount(unsigned(loghead(8, result)))))) +} + +val pf_spec64 : bits(64) -> bits(1) + +function pf_spec64 result = { + bool_to_bits(not_bool(logbitp(0, logcount(unsigned(loghead(8, result)))))) +} + +/*!General @ ( ' PF ' ) Specification ( Source: Intel Manuals, + Vol. 1, Section 3.4.3.1 ) :
+ +Parity flag Set if the least-significant byte of the + result contains an even number of 1 bits; cleared otherwise.
*/ +val general_pf_spec_fn : ({|8, 16, 32, 64|}, nat) -> bits(1) + +function general_pf_spec_fn (result_nbits, result) = { + bool_to_bits(not_bool(logbitp(0, logcount(loghead(8, result))))) +} + +val sf_spec8 : bits(8) -> bits(1) + +function sf_spec8 result = { + result[7 .. 7] +} + +val sf_spec16 : bits(16) -> bits(1) + +function sf_spec16 result = { + result[15 .. 15] +} + +val sf_spec32 : bits(32) -> bits(1) + +function sf_spec32 result = { + result[31 .. 31] +} + +val sf_spec64 : bits(64) -> bits(1) + +function sf_spec64 result = { + result[63 .. 63] +} + +/*!General @ ( ' SF ' ) Specification ( Source: Intel Manuals, + Vol. 1, Section 3.4.3.1 ) :
+ +Sign flag Set equal to the most-significant bit of the + result, which is the sign bit of a signed integer. ( 0 indicates a + positive value and 1 indicates a negative value. )
*/ +val general_sf_spec_fn : ({|8, 16, 32, 64|}, nat) -> bits(1) + +function general_sf_spec_fn (result_nbits, result) = { + get_slice_int(1, result, result_nbits - 1) +} + +val add_af_spec8 : (bits(8), bits(8)) -> bits(1) + +function add_af_spec8 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let add : bits(5) = bits_of_int(unsigned(dst_3_0) + unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(15 < unsigned(add)); + af +} + +val add_af_spec16 : (bits(16), bits(16)) -> bits(1) + +function add_af_spec16 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let add : bits(5) = bits_of_int(unsigned(dst_3_0) + unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(15 < unsigned(add)); + af +} + +val add_af_spec32 : (bits(32), bits(32)) -> bits(1) + +function add_af_spec32 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let add : bits(5) = bits_of_int(unsigned(dst_3_0) + unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(15 < unsigned(add)); + af +} + +val add_af_spec64 : (bits(64), bits(64)) -> bits(1) + +function add_af_spec64 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let add : bits(5) = bits_of_int(unsigned(dst_3_0) + unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(15 < unsigned(add)); + af +} + +val sub_af_spec8 : (bits(8), bits(8)) -> bits(1) + +function sub_af_spec8 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sub : sbits(5) = bits_of_int(unsigned(dst_3_0) - unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(signed(sub) < 0); + af +} + +val sub_af_spec16 : (bits(16), bits(16)) -> bits(1) + +function sub_af_spec16 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sub : sbits(5) = bits_of_int(unsigned(dst_3_0) - unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(signed(sub) < 0); + af +} + +val sub_af_spec32 : (bits(32), bits(32)) -> bits(1) + +function sub_af_spec32 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sub : sbits(5) = bits_of_int(unsigned(dst_3_0) - unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(signed(sub) < 0); + af +} + +val sub_af_spec64 : (bits(64), bits(64)) -> bits(1) + +function sub_af_spec64 (dst, src) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sub : sbits(5) = bits_of_int(unsigned(dst_3_0) - unsigned(src_3_0), 5); + let af : bits(1) = bool_to_bits(signed(sub) < 0); + af +} + +val adc_af_spec8 : (bits(8), bits(8), bits(1)) -> bits(1) + +function adc_af_spec8 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let adc : bits(6) = bits_of_int(unsigned(dst_3_0) + (unsigned(src_3_0) + unsigned(cf)), 6); + let af : bits(1) = bool_to_bits(15 < unsigned(adc)); + af +} + +val adc_af_spec16 : (bits(16), bits(16), bits(1)) -> bits(1) + +function adc_af_spec16 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let adc : bits(6) = bits_of_int(unsigned(dst_3_0) + (unsigned(src_3_0) + unsigned(cf)), 6); + let af : bits(1) = bool_to_bits(15 < unsigned(adc)); + af +} + +val adc_af_spec32 : (bits(32), bits(32), bits(1)) -> bits(1) + +function adc_af_spec32 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let adc : bits(6) = bits_of_int(unsigned(dst_3_0) + (unsigned(src_3_0) + unsigned(cf)), 6); + let af : bits(1) = bool_to_bits(15 < unsigned(adc)); + af +} + +val adc_af_spec64 : (bits(64), bits(64), bits(1)) -> bits(1) + +function adc_af_spec64 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let adc : bits(6) = bits_of_int(unsigned(dst_3_0) + (unsigned(src_3_0) + unsigned(cf)), 6); + let af : bits(1) = bool_to_bits(15 < unsigned(adc)); + af +} + +val sbb_af_spec8 : (bits(8), bits(8), bits(1)) -> bits(1) + +function sbb_af_spec8 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sbb : int = unsigned(dst_3_0) - (unsigned(src_3_0) + unsigned(sail_zero_extend(cf, 4))); + let af : bits(1) = bool_to_bits(sbb < 0); + af +} + +val sbb_af_spec16 : (bits(16), bits(16), bits(1)) -> bits(1) + +function sbb_af_spec16 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sbb : int = unsigned(dst_3_0) - (unsigned(src_3_0) + unsigned(sail_zero_extend(cf, 4))); + let af : bits(1) = bool_to_bits(sbb < 0); + af +} + +val sbb_af_spec32 : (bits(32), bits(32), bits(1)) -> bits(1) + +function sbb_af_spec32 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sbb : int = unsigned(dst_3_0) - (unsigned(src_3_0) + unsigned(sail_zero_extend(cf, 4))); + let af : bits(1) = bool_to_bits(sbb < 0); + af +} + +val sbb_af_spec64 : (bits(64), bits(64), bits(1)) -> bits(1) + +function sbb_af_spec64 (dst, src, cf) = { + let dst_3_0 : bits(4) = dst[3 .. 0]; + let src_3_0 : bits(4) = src[3 .. 0]; + let sbb : int = unsigned(dst_3_0) - (unsigned(src_3_0) + unsigned(sail_zero_extend(cf, 4))); + let af : bits(1) = bool_to_bits(sbb < 0); + af +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rotate_and_shift.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rotate_and_shift.sail new file mode 100644 index 000000000..f0330d7b7 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/rotate_and_shift.sail @@ -0,0 +1,202 @@ +$include "./shifts_spec.sail" +$include "./rotates_spec.sail" + +/*!
+ Op/En: MI
+ C0/0: ROL r/m8, imm8
+ C0/1: ROR r/m8, imm8
+ C0/2: RCL r/m8, imm8
+ C0/3: RCR r/m8, imm8
+ C0/4: SAL/SHL r/m8, imm8
+ C0/5: SHR r/m8, imm8
+ C0/7: SAR r/m8, imm8
+ C1/0: ROL r/m16/32/64, imm8
+ C1/1: ROR r/m16/32/64, imm8
+ C1/2: RCL r/m16/32/64, imm8
+ C1/3: RCR r/m16/32/64, imm8
+ C1/4: SAL/SHL r/m16/32/64, imm8
+ C1/5: SHR r/m16/32/64, imm8
+ C1/7: SAR r/m16/32/64. imm8
+
+ Op/En: M1
+ D0/0: ROL r/m8, 1
+ D0/1: ROR r/m8, 1
+ D0/2: RCL r/m8, 1
+ D0/3: RCR r/m8, 1
+ D0/4: SAL/SHL r/m8, 1
+ D0/5: SHR r/m8, 1
+ D0/7: SAR r/m8, 1
+ D1/0: ROL r/m16/32/64, 1
+ D1/1: ROR r/m16/32/64, 1
+ D1/2: RCL r/m16/32/64, 1
+ D1/3: RCR r/m16/32/64, 1
+ D1/4: SAL/SHL r/m16/32/64, 1
+ D1/5: SHR r/m16/32/64, 1
+ D1/7: SAR r/m16/32/64, 1
+
+ Op/En: MC
+ D2/0: ROL r/m8, CL
+ D2/1: ROR r/m8, CL
+ D2/2: RCL r/m8, CL
+ D2/3: RCR r/m8, CL
+ D2/4: SAL/SHL r/m8, CL
+ D2/5: SHR r/m8, CL
+ D2/7: SAR r/m8, CL
+ D3/0: ROL r/m16/32/64, CL
+ D3/1: ROR r/m16/32/64, CL
+ D3/2: RCL r/m16/32/64, CL
+ D3/3: RCR r/m16/32/64, CL
+ D3/4: SAL/SHL r/m16/32/64, CL
+ D3/5: SHR r/m16/32/64, CL
+ D3/7: SAR r/m16/32/64, CL
+
+ Op/En: MRI
+ 0F A4: SHLD r/m16, r16, imm8
+ 0F A4: SHLD r/m32, r32, imm8
+ 0F A4: SHLD r/m64, r64, imm8
+
+ Op/En: MRC
+ 0F A5: SHLD r/m16, r16, CL
+ 0F A5: SHLD r/m32, r32, CL
+ 0F A5: SHLD r/m64, r64, CL
+
+ Op/En: MRI
+ 0F AC: SHRD r/m16, r16, imm8
+ 0F AC: SHRD r/m32, r32, imm8
+ 0F AC: SHRD r/m64, r64, imm8
+
+ Op/En: MRC
+ 0F AD: SHRD r/m16, r16, CL
+ 0F AD: SHRD r/m32, r32, CL
+ 0F AD: SHRD r/m64, r64, CL
+
Source: Intel Manual, Volume 2B, Instruction Set Reference + \ ( N-Z\ ) .
+ +The shift arithmetic left \ ( SAL\ ) and shift logical left \ ( SHL\ ) + instructions perform the same operation; they shift the bits in the + destination operand to the left \ ( toward more significant bit + locations\ ) . For each shift count, the most significant bit of the + destination operand is shifted into the CF flag, and the least + significant bit is cleared. The OF flag is affected only on 1-bit + shifts. For left shifts, the OF flag is set to 0 if the + most-significant bit of the result is the same as the CF flag ( that + is, the top two bits of the original operand were the same ) ; + otherwise, it is set to 1.
*/ +val sal_shl_spec : ({|1, 2, 4, 8|}, bits(64), bits(6), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function sal_shl_spec (size, dst, src, input_rflags) = { + (match size { + 1 => { + let (elem0, elem1, elem2) : (bits(8), rflagsbits, rflagsbits) = sal_shl_spec_8(truncate(dst, 8), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 2 => { + let (elem0, elem1, elem2) : (bits(16), rflagsbits, rflagsbits) = sal_shl_spec_16(truncate(dst, 16), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 4 => { + let (elem0, elem1, elem2) : (bits(32), rflagsbits, rflagsbits) = sal_shl_spec_32(truncate(dst, 32), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 8 => sal_shl_spec_64(dst, src, input_rflags), + _ => (0x0000000000000000, Mk_rflagsbits(0x00000000), Mk_rflagsbits(0x00000000)) + }) : (bits(64), rflagsbits, rflagsbits) +} + +val shr_spec_8 : (bits(8), bits(6), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function shr_spec_8 (dst, src, input_rflags) = { + let neg_src : sbits(9) = bits_of_int(negate(unsigned(src)), 9); + let raw_result : bits(8) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 8); + let result : bits(8) = raw_result; + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : bits(1) = dst[7 .. 7]; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 8 <= unsigned(src) then { + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val shr_spec_16 : (bits(16), bits(6), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function shr_spec_16 (dst, src, input_rflags) = { + let neg_src : sbits(17) = bits_of_int(negate(unsigned(src)), 17); + let raw_result : bits(16) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 16); + let result : bits(16) = raw_result; + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : bits(1) = dst[15 .. 15]; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 16 <= unsigned(src) then { + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val shr_spec_32 : (bits(32), bits(6), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function shr_spec_32 (dst, src, input_rflags) = { + let neg_src : sbits(33) = bits_of_int(negate(unsigned(src)), 33); + let raw_result : bits(32) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 32); + let result : bits(32) = raw_result; + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : bits(1) = dst[31 .. 31]; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 32 <= unsigned(src) then { + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val shr_spec_64 : (bits(64), bits(6), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function shr_spec_64 (dst, src, input_rflags) = { + let neg_src : sbits(65) = bits_of_int(negate(unsigned(src)), 65); + let raw_result : bits(64) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 64); + let result : bits(64) = raw_result; + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : bits(1) = dst[63 .. 63]; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 64 <= unsigned(src) then { + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +/*! Specification for the @ ( ' SHR ' ) instruction +Source: Intel Manual, Volume 2B, Instruction Set Reference \ ( N-Z\ ) .
+ +The shift arithmetic right \ ( SAR\ ) and shift logical right \ ( SHR\ ) + instructions shift the bits of the destination operand to the right + \ ( toward less significant bit locations\ ) . For each shift count, the + least significant bit of the destination operand is shifted into the + CF flag, and the most significant bit is either set or cleared + depending on the instruction type. The SHR instruction clears the most + significant bit ( see Figure 7-8 in the Intel 64 and IA-32 + Architectures Software Developer s Manual, Volume 1 ) ... The OF flag is + affected only on 1-bit shifts. For the SAR instruction, the OF flag is + cleared for all 1-bit shifts. For the SHR instruction, the OF flag is + set to the most-significant bit of the original operand.
*/ +val shr_spec : ({|1, 2, 4, 8|}, bits(64), bits(6), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function shr_spec (size, dst, src, input_rflags) = { + (match size { + 1 => { + let (elem0, elem1, elem2) : (bits(8), rflagsbits, rflagsbits) = shr_spec_8(truncate(dst, 8), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 2 => { + let (elem0, elem1, elem2) : (bits(16), rflagsbits, rflagsbits) = shr_spec_16(truncate(dst, 16), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 4 => { + let (elem0, elem1, elem2) : (bits(32), rflagsbits, rflagsbits) = shr_spec_32(truncate(dst, 32), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 8 => shr_spec_64(dst, src, input_rflags), + _ => (0x0000000000000000, Mk_rflagsbits(0x00000000), Mk_rflagsbits(0x00000000)) + }) : (bits(64), rflagsbits, rflagsbits) +} + +val sar_spec_8 : (bits(8), bits(6), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function sar_spec_8 (dst, src, input_rflags) = { + let neg_src : sbits(9) = bits_of_int(negate(unsigned(src)), 9); + let raw_result_not_sign_extended : bits(8) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 8); + let raw_result : int = { + if logbit(7, dst) == 0b1 then { + loghead(8, ash(binary_logext(8, unsigned(dst)), signed(neg_src))) + } else { + unsigned(raw_result_not_sign_extended) + } + }; + let result : bits(8) = bits_of_int(raw_result, 8); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 8 <= unsigned(src) then { + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val sar_spec_16 : (bits(16), bits(6), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function sar_spec_16 (dst, src, input_rflags) = { + let neg_src : sbits(17) = bits_of_int(negate(unsigned(src)), 17); + let raw_result_not_sign_extended : bits(16) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 16); + let raw_result : int = { + if logbit(15, dst) == 0b1 then { + loghead(16, ash(binary_logext(16, unsigned(dst)), signed(neg_src))) + } else { + unsigned(raw_result_not_sign_extended) + } + }; + let result : bits(16) = bits_of_int(raw_result, 16); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 16 <= unsigned(src) then { + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val sar_spec_32 : (bits(32), bits(6), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function sar_spec_32 (dst, src, input_rflags) = { + let neg_src : sbits(33) = bits_of_int(negate(unsigned(src)), 33); + let raw_result_not_sign_extended : bits(32) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 32); + let raw_result : int = { + if logbit(31, dst) == 0b1 then { + loghead(32, ash(binary_logext(32, unsigned(dst)), signed(neg_src))) + } else { + unsigned(raw_result_not_sign_extended) + } + }; + let result : bits(32) = bits_of_int(raw_result, 32); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 32 <= unsigned(src) then { + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +val sar_spec_64 : (bits(64), bits(6), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function sar_spec_64 (dst, src, input_rflags) = { + let neg_src : sbits(65) = bits_of_int(negate(unsigned(src)), 65); + let raw_result_not_sign_extended : bits(64) = bits_of_int(ash(unsigned(dst), signed(neg_src)), 64); + let raw_result : int = { + if logbit(63, dst) == 0b1 then { + loghead(64, ash(binary_logext(64, unsigned(dst)), signed(neg_src))) + } else { + unsigned(raw_result_not_sign_extended) + } + }; + let result : bits(64) = bits_of_int(raw_result, 64); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match src { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if 64 <= unsigned(src) then { + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let output_rflags : rflagsbits = [input_rflags with pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with cf = 0b1, af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let cf : bits(1) = bitslice(dst, unsigned(src) - 1, 1); + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (result, output_rflags, undefined_flags) +} + +/*! Specification for the @ ( ' SAR ' ) instruction +Source: Intel Manual, Volume 2B, Instruction Set Reference \ ( N-Z\ ) .
+ +The shift arithmetic right \ ( SAR\ ) and shift logical right \ ( SHR\ ) + instructions shift the bits of the destination operand to the right + \ ( toward less significant bit locations\ ) . For each shift count, the + least significant bit of the destination operand is shifted into the + CF flag, and the most significant bit is either set or cleared + depending on the instruction type. The SAR instruction sets or clears + the most significant bit to correspond to the sign \ ( most significant + bit\ ) of the original value in the destination operand. In effect, the + SAR instruction fills the empty bit position s shifted value with the + sign of the unshifted value. ... The OF flag is affected only on 1-bit + shifts. For the SAR instruction, the OF flag is cleared for all 1-bit + shifts. For the SHR instruction, the OF flag is set to the + most-significant bit of the original operand.
*/ +val sar_spec : ({|1, 2, 4, 8|}, bits(64), bits(6), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function sar_spec (size, dst, src, input_rflags) = { + (match size { + 1 => { + let (elem0, elem1, elem2) : (bits(8), rflagsbits, rflagsbits) = sar_spec_8(truncate(dst, 8), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 2 => { + let (elem0, elem1, elem2) : (bits(16), rflagsbits, rflagsbits) = sar_spec_16(truncate(dst, 16), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 4 => { + let (elem0, elem1, elem2) : (bits(32), rflagsbits, rflagsbits) = sar_spec_32(truncate(dst, 32), src, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2) + }, + 8 => sar_spec_64(dst, src, input_rflags), + _ => (0x0000000000000000, Mk_rflagsbits(0x00000000), Mk_rflagsbits(0x00000000)) + }) : (bits(64), rflagsbits, rflagsbits) +} + +val shld_spec_16 : (bits(16), bits(16), bits(6), rflagsbits) -> (bits(16), bool, rflagsbits, rflagsbits) + +function shld_spec_16 (dst, src, cnt, input_rflags) = { + let dst_src : bits(32) = logior(bits_of_int(ash(unsigned(dst), 16), 32), sail_zero_extend(src, 32)); + let output_dst : bits(16) = bits_of_int(ash(unsigned(dst_src), unsigned(sail_zero_extend(cnt, 16)) - 16), 16); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[15 .. 15]; + let of : {|0, 1|} = { + if logbit(15, dst) == logbit(15, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec16(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec16(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 16 then { + let cf : bits(1) = bitslice(dst, 16 - unsigned(cnt), 1); + let pf : bits(1) = pf_spec16(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec16(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 16, output_rflags, undefined_flags) +} + +val shld_spec_32 : (bits(32), bits(32), bits(6), rflagsbits) -> (bits(32), bool, rflagsbits, rflagsbits) + +function shld_spec_32 (dst, src, cnt, input_rflags) = { + let dst_src : bits(64) = logior(bits_of_int(ash(unsigned(dst), 32), 64), sail_zero_extend(src, 64)); + let output_dst : bits(32) = bits_of_int(ash(unsigned(dst_src), unsigned(sail_zero_extend(cnt, 32)) - 32), 32); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[31 .. 31]; + let of : {|0, 1|} = { + if logbit(31, dst) == logbit(31, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec32(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec32(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 32 then { + let cf : bits(1) = bitslice(dst, 32 - unsigned(cnt), 1); + let pf : bits(1) = pf_spec32(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec32(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 32, output_rflags, undefined_flags) +} + +val shld_spec_64 : (bits(64), bits(64), bits(6), rflagsbits) -> (bits(64), bool, rflagsbits, rflagsbits) + +function shld_spec_64 (dst, src, cnt, input_rflags) = { + let dst_src : bits(128) = logior(bits_of_int(ash(unsigned(dst), 64), 128), sail_zero_extend(src, 128)); + let output_dst : bits(64) = bits_of_int(ash(unsigned(dst_src), unsigned(sail_zero_extend(cnt, 64)) - 64), 64); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[63 .. 63]; + let of : {|0, 1|} = { + if logbit(63, dst) == logbit(63, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec64(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec64(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 64 then { + let cf : bits(1) = bitslice(dst, 64 - unsigned(cnt), 1); + let pf : bits(1) = pf_spec64(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec64(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 64, output_rflags, undefined_flags) +} + +/*! Specification for the @ ( ' SHLD ' ) instruction. */ +val shld_spec : ({|2, 4, 8|}, bits(64), bits(64), bits(6), rflagsbits) -> (bits(64), bool, rflagsbits, rflagsbits) + +function shld_spec (size, dst, src, cnt, input_rflags) = { + (match size { + 2 => { + let (elem0, elem1, elem2, elem3) : (bits(16), bool, rflagsbits, rflagsbits) = shld_spec_16(truncate(dst, 16), truncate(src, 16), cnt, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2, elem3) + }, + 4 => { + let (elem0, elem1, elem2, elem3) : (bits(32), bool, rflagsbits, rflagsbits) = shld_spec_32(truncate(dst, 32), truncate(src, 32), cnt, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2, elem3) + }, + 8 => shld_spec_64(dst, src, cnt, input_rflags), + _ => (0x0000000000000000, false, Mk_rflagsbits(0x00000000), Mk_rflagsbits(0x00000000)) + }) : (bits(64), bool, rflagsbits, rflagsbits) +} + +val shrd_spec_16 : (bits(16), bits(16), bits(6), rflagsbits) -> (bits(16), bool, rflagsbits, rflagsbits) + +function shrd_spec_16 (dst, src, cnt, input_rflags) = { + let src_dst : bits(32) = logior(bits_of_int(ash(unsigned(src), 16), 32), sail_zero_extend(dst, 32)); + let neg_cnt : sbits(17) = bits_of_int(negate(unsigned(cnt)), 17); + let output_dst : bits(16) = bits_of_int(ash(unsigned(src_dst), signed(neg_cnt)), 16); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let of : {|0, 1|} = { + if logbit(15, dst) == logbit(15, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec16(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec16(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 16 then { + let cf : bits(1) = bitslice(dst, unsigned(cnt) - 1, 1); + let pf : bits(1) = pf_spec16(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec16(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 16, output_rflags, undefined_flags) +} + +val shrd_spec_32 : (bits(32), bits(32), bits(6), rflagsbits) -> (bits(32), bool, rflagsbits, rflagsbits) + +function shrd_spec_32 (dst, src, cnt, input_rflags) = { + let src_dst : bits(64) = logior(bits_of_int(ash(unsigned(src), 32), 64), sail_zero_extend(dst, 64)); + let neg_cnt : sbits(33) = bits_of_int(negate(unsigned(cnt)), 33); + let output_dst : bits(32) = bits_of_int(ash(unsigned(src_dst), signed(neg_cnt)), 32); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let of : {|0, 1|} = { + if logbit(31, dst) == logbit(31, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec32(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec32(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 32 then { + let cf : bits(1) = bitslice(dst, unsigned(cnt) - 1, 1); + let pf : bits(1) = pf_spec32(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec32(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 32, output_rflags, undefined_flags) +} + +val shrd_spec_64 : (bits(64), bits(64), bits(6), rflagsbits) -> (bits(64), bool, rflagsbits, rflagsbits) + +function shrd_spec_64 (dst, src, cnt, input_rflags) = { + let src_dst : bits(128) = logior(bits_of_int(ash(unsigned(src), 64), 128), sail_zero_extend(dst, 128)); + let neg_cnt : sbits(65) = bits_of_int(negate(unsigned(cnt)), 65); + let output_dst : bits(64) = bits_of_int(ash(unsigned(src_dst), signed(neg_cnt)), 64); + let (output_rflags, undefined_flags) : (rflagsbits, rflagsbits) = { + match cnt { + 0 => (input_rflags, Mk_rflagsbits(0x00000000)), + 1 => { + let cf : bits(1) = dst[0 .. 0]; + let of : {|0, 1|} = { + if logbit(63, dst) == logbit(63, output_dst) then 0 else 1 + }; + let pf : bits(1) = pf_spec64(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec64(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, of = bits_of_int(of, 1), pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (output_rflags, undefined_flags) + }, + _ => { + if unsigned(cnt) <= 64 then { + let cf : bits(1) = bitslice(dst, unsigned(cnt) - 1, 1); + let pf : bits(1) = pf_spec64(output_dst); + let zf : bits(1) = zf_spec(unsigned(output_dst)); + let sf : bits(1) = sf_spec64(output_dst); + let output_rflags : rflagsbits = [input_rflags with cf, pf, zf, sf]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1, of = 0b1]; + (output_rflags, undefined_flags) + } else { + let output_rflags : rflagsbits = input_rflags; + let undefined_flags : rflagsbits = [input_rflags with af = 0b1, sf = 0b1, zf = 0b1, pf = 0b1, of = 0b1, cf = 0b1]; + (output_rflags, undefined_flags) + } + } + } + }; + (output_dst, unsigned(cnt) > 64, output_rflags, undefined_flags) +} + +/*! Specification for the @ ( ' SHRD ' ) instruction. */ +val shrd_spec : ({|2, 4, 8|}, bits(64), bits(64), bits(6), rflagsbits) -> (bits(64), bool, rflagsbits, rflagsbits) + +function shrd_spec (size, dst, src, cnt, input_rflags) = { + (match size { + 2 => { + let (elem0, elem1, elem2, elem3) : (bits(16), bool, rflagsbits, rflagsbits) = shrd_spec_16(truncate(dst, 16), truncate(src, 16), cnt, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2, elem3) + }, + 4 => { + let (elem0, elem1, elem2, elem3) : (bits(32), bool, rflagsbits, rflagsbits) = shrd_spec_32(truncate(dst, 32), truncate(src, 32), cnt, input_rflags); + (sail_zero_extend(elem0, 64), elem1, elem2, elem3) + }, + 8 => shrd_spec_64(dst, src, cnt, input_rflags), + _ => (0x0000000000000000, false, Mk_rflagsbits(0x00000000), Mk_rflagsbits(0x00000000)) + }) : (bits(64), bool, rflagsbits, rflagsbits) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/shuffle_and_unpack.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/shuffle_and_unpack.sail new file mode 100644 index 000000000..3735faf3b --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/shuffle_and_unpack.sail @@ -0,0 +1,182 @@ + +val extract_32_bits : (bits(128), bits(2)) -> bits(32) + +function extract_32_bits (x, n) = { + (match n { + 0 => x[31 .. 0], + 1 => x[63 .. 32], + 2 => x[95 .. 64], + _ => x[127 .. 96] + }) : bits(32) +} + +val extract_64_bits : (bits(128), range(0, 1)) -> bits(64) + +function extract_64_bits (x, n) = { + (match n { + 0 => x[63 .. 0], + _ => x[127 .. 64] + }) : bits(64) +} + +/*! Shuffle packed single-precision floating-point values +The function @ ( ' get-prefixes ' ) fetches the legacy and REX prefixes + of an instruction and also returns the first byte following the last such + prefix. The input @ ( ' start-rip ' ) points to the first byte of an instruction, + which may potentially be a legacy prefix. The initial value of @ ( ' cnt ' ) + should be @ ( ' 15 ' ) so that the result @ ( ' ( - 15 cnt ) ' ) returned at the end of + the recursion is the correct number of legacy and/or REX bytes parsed by this + function.
+ +From Intel Manual, Vol. 2, May 2018, Section 2.1.1 ( Instruction + Prefixes ) :
+ +Instruction prefixes are divided into four groups, each with a set of + allowable prefix codes. For each instruction, it is only useful to include + up to one prefix code from each of the four groups ( Groups 1, 2, 3, + 4 ) . Groups 1 through 4 may be placed in any order relative to each + other.
+ +Despite the quote from the Intel Manual above, the order of the legacy + prefixes does matter when there is more than one prefix from the same group + --- all but the last prefix from a single prefix group are ignored. + The only exception in this case is for Group 1 prefixes --- see + below for details.
+ +We now discuss the Group 1 exception below.
+ +@ ( ' 0xf0_f2_a4 ' ) is NOT
+ @ ( ' repne movsb byte ptr [rdi], byte ptr [rsi] ' )
+ It is:
+ @ ( ' lock repne movsb byte ptr [rdi], byte ptr [rsi] ' )
+
+ Note that lock and rep/repne are Group 1 prefixes. It is important to record
+ the lock prefix, even if it is overshadowed by a rep/repne prefix, because
+ the former instruction will not @ ( ' #UD ' ) , but the latter instruction will.
+ This is akin to the lock prefix being in a separate group than the rep/repne
+ prefixes; in fact, AMD manuals ( Section 1.2.1: Summary of Legacy Prefixes,
+ Vol. 3 May 2018 Edition ) treat them as such.
For details about how mandatory prefixes are picked from legacy prefixes, + see @ ( see mandatory-prefixes-computation ) .
+ +A REX prefix ( applicable only to 64-bit mode ) is treated as a null prefix + if it is followed by a legacy prefix. Here is an illustrative example ( using + Intel ' s XED, x86 Encoder Decoder --- see + @ ( ' https://intelxed.github.io/ ' ) ) :
+ +Note that the prefixes structure output of this function does not include + the REX byte ( which is a separate return value of this function ) , but its + @ ( ' :num-prefixes ' ) field includes a count of the REX prefixes encountered. + This is because adding an 8-bit field to the prefixes structure to store a + REX byte will make it a bignum, thereby impacting execution efficiency.
*/ +val get_prefixes : (proc_mode, sbits(48), prefixes, bits(8), range(0, 15)) -> (prefixes, bits(8)) + +function get_prefixes (proc_mode, start_rip, prefixes, rex_byte, cnt) = { + if cnt == 0 then { + (prefixes, rex_byte) + } else { + let byte : bits(8) = rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(start_rip, 64), None(), 1, ":X"); + if is_ext_prefix_byte(proc_mode, prefixes, rex_byte, cnt, byte) then { + let (prefixes, rex_byte, continue?) = process_ext_prefix_byte(proc_mode, prefixes, rex_byte, cnt, byte); + if not_bool(continue?) then { + (prefixes, rex_byte) + } else { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + get_prefixes(proc_mode, next_rip, prefixes, rex_byte, check_range(0, 15, cnt - 1)) + } + } else { + let prefix_byte_group_code : {|0, 1, 2, 3, 4|} = get_one_byte_prefix_array_code(byte); + match prefix_byte_group_code { + 0 => { + let rex? : bool = in_64bit_mode(proc_mode) & bits_of_int(ash(unsigned(byte), -4), 4) == 0x4; + if rex? then { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + get_prefixes(proc_mode, next_rip, prefixes, byte, check_range(0, 15, cnt - 1)) + } else { + let prefixes : prefixes = [prefixes with nxt = byte]; + ([prefixes with num = bits_of_int(15 - cnt, 4)], rex_byte) + } + }, + 1 => { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + let prefixes : prefixes = { + if byte == 0xf0 then { + [prefixes with lck = byte] + } else { + [prefixes with rep = byte] + } + }; + get_prefixes(proc_mode, next_rip, prefixes, 0x00, check_range(0, 15, cnt - 1)) + }, + 2 => { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + if in_64bit_mode(proc_mode) & (byte == 0x64 | byte == 0x65) | not_bool(in_64bit_mode(proc_mode)) then { + get_prefixes(proc_mode, next_rip, [prefixes with seg = byte], 0x00, check_range(0, 15, cnt - 1)) + } else { + get_prefixes(proc_mode, next_rip, prefixes, 0x00, check_range(0, 15, cnt - 1)) + } + }, + 3 => { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + get_prefixes(proc_mode, next_rip, [prefixes with opr = byte], 0x00, check_range(0, 15, cnt - 1)) + }, + 4 => { + let next_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, 0x000000000001); + get_prefixes(proc_mode, next_rip, [prefixes with adr = byte], 0x00, check_range(0, 15, cnt - 1)) + }, + _ => (prefixes, rex_byte) + } + } + } +} + +/*! Top-level dispatch function. +@ ( ' one-byte-opcode-execute ' ) is the doorway to all the opcode + maps ( for non-AVX/AVX512 instructions ) .
*/ +val one_byte_opcode_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function one_byte_opcode_execute (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + if ext_one_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) then { + return() + } else { + (match opcode { + 0 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 1 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 2 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 3 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 4 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 5 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 6 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"PUSH ES is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 7 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"POP ES is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 8 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 9 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 10 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 11 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 12 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 13 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 14 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"PUSH CS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 15 => two_byte_opcode_decode_and_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode), + 16 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 17 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 18 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 19 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 20 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 21 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 22 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"PUSH SS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 23 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"POP SS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 24 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 25 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 26 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 27 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 28 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 29 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 30 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"PUSH DS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 31 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"POP DS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 32 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 33 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 34 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 35 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 36 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 37 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 38 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 39 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"DAA is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 40 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 41 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 42 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 43 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 44 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 45 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 46 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 47 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"DAS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 48 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 49 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 50 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 51 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 52 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 53 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 54 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 55 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"AAA is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 56 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 57 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 58 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 59 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_g_e(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 60 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 61 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 62 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 63 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"AAS is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 64 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 65 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 66 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 67 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 68 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 69 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 70 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 71 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 72 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 73 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 74 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 75 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 76 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 77 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 78 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 79 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_4x(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 80 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 81 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 82 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 83 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 84 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 85 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 86 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 87 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 88 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 89 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 90 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 91 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 92 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 93 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 94 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 95 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_general_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 96 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pusha(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"PUSHA is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 97 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_popa(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"POPA is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 98 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 99 => { + if in_64bit_mode(proc_mode) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movsx(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 100 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 101 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 102 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 103 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 104 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_i(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 105 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_imul_op_en_rmi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 106 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_i(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 107 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_imul_op_en_rmi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 108 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 109 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 110 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 111 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 112 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 113 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 114 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 115 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 116 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 117 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 118 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 119 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 120 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 121 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 122 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 123 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 124 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 125 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 126 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 127 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_one_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 128 => { + if modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 129 => { + if modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 130 => { + if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b100 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b101 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b110 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if not_bool(in_64bit_mode(proc_mode)) & modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"Opcode 0x82 is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 131 => { + if modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(2, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(6, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(3, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(4, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(5, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(8, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 132 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 133 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_g(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 134 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 135 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 136 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 137 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 138 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 139 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 140 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 141 => { + if modr_m[mod] == 0b11 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_lea(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 142 => { + if modr_m[reg] == 0b001 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 143 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pop_ev(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 144 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + if logbitp(0, rex_byte) then { + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_one_byte_nop(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } + }, + 145 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 146 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 147 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 148 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 149 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 150 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 151 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_xchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 152 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cbw_cwd_cdqe(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 153 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cwd_cdq_cqo(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 154 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"far CALL is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 155 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 156 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_pushf(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 157 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_popf(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 158 => { + if 0xf0 == prefixes[lck] | in_64bit_mode(proc_mode) & feature_flag_fn(":LAHF-SAHF") == 0 then { + x86_fault("(:UD)") + }; + x86_sahf(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 159 => { + if 0xf0 == prefixes[lck] | in_64bit_mode(proc_mode) & feature_flag_fn(":LAHF-SAHF") == 0 then { + x86_fault("(:UD)") + }; + x86_lahf(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 160 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_fd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 161 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_fd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 162 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_td(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 163 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_td(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 164 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movs(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 165 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movs(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 166 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmps(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 167 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmps(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 168 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 169 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_rax_i(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 170 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_stos(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 171 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_stos(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 172 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 173 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 174 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 175 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 176 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 177 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 178 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 179 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 180 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 181 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 182 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 183 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 184 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 185 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 186 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 187 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 188 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 189 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 190 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 191 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_oi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 192 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 193 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 194 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_ret(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 195 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_ret(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 196 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 197 => { + if in_64bit_mode(proc_mode) then { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } else if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 198 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_mi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b000 then { + if feature_flags_fn([|":RTM"|]) == 0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 199 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mov_op_en_mi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b000 then { + if feature_flags_fn([|":RTM"|]) == 0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 200 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 201 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_leave(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 202 => x86_model_error("Opcode Unimplemented in x86isa!"), + 203 => x86_model_error("Opcode Unimplemented in x86isa!"), + 204 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 205 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 206 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"INTO is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 207 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 208 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 209 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 210 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 211 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_sal_sar_shl_shr_rcl_rcr_rol_ror(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 212 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"AAM is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 213 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"AAD is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 215 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 216 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 217 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 218 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 219 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 220 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 221 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 222 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 223 => { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 224 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_loop(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 225 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_loop(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 226 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_loop(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 227 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_jrcxz(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 228 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 229 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 230 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 231 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 232 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_call_e8_op_en_m(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 233 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_near_jmp_op_en_d(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 234 => { + if not_bool(in_64bit_mode(proc_mode)) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) then { + if true then { + x86_fault("(:UD)") + }; + x86_fault("(X86-ILLEGAL-INSTRUCTION \"JMP is illegal in the 64-bit mode!\" START-RIP TEMP-RIP X86)") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 235 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_near_jmp_op_en_d(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 236 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 237 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 238 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 239 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 240 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 241 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 242 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 243 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)"), + 244 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_hlt(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 245 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmc_clc_stc_cld_std(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 246 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_not_neg_f6_f7(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_not_neg_f6_f7(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mul(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_imul_op_en_m(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_div(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_idiv(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 247 => { + if modr_m[reg] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_add_adc_sub_sbb_or_and_xor_cmp_test_e_i(7, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_not_neg_f6_f7(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_not_neg_f6_f7(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_mul(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_imul_op_en_m(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_div(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b111 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_idiv(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 248 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmc_clc_stc_cld_std(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 249 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmc_clc_stc_cld_std(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 250 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 251 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 252 => x86_cmc_clc_stc_cld_std(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib), + 253 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmc_clc_stc_cld_std(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 254 => { + if modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_fe_ff(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_fe_ff(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 255 => { + if modr_m[reg] == 0b000 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_fe_ff(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b001 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_inc_dec_fe_ff(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_call_ff_2_op_en_m(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_near_jmp_op_en_m(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_far_jmp_op_en_d(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b110 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_ev(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + _ => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + }) : unit + } +} + +/*! Top-level step function +@ ( ' x86-fetch-decode-execute ' ) is the step function of our x86 + interpreter. It fetches one instruction by looking up the memory address + indicated by the instruction pointer @ ( ' rip ' ) , decodes that instruction, and + dispatches control to the appropriate instruction semantic function.
*/ +val x86_fetch_decode_execute : unit -> unit + +function x86_fetch_decode_execute () = { + let ctx : string = "x86-fetch-decode-execute"; + if ms() | fault() then () else { + let proc_mode : proc_mode = x86_operation_mode(); + let n64_bit_modep_var : bool = in_64bit_mode(proc_mode); + let start_rip : sbits(48) = read_iptr(proc_mode); + let (prefixes, rex_byte) : (prefixes, bits(8)) = get_prefixes(proc_mode, start_rip, Mk_prefixes(0x0000000000000), 0x00, 15); + let prefixes : prefixes = Mk_prefixes(prefixes.bits); + let opcode_vex_evex_byte : bits(8) = prefixes[nxt]; + let prefix_length : bits(4) = prefixes[num]; + let temp_rip : sbits(48) = add_to_iptr(proc_mode, start_rip, bits_of_int(1 + unsigned(prefix_length), 48)); + let vex_byte0? : bool = opcode_vex_evex_byte == 0xc5 | opcode_vex_evex_byte == 0xc4; + let les_lds_distinguishing_byte : bits(8) : bits(8) = { + if vex_byte0? then { + rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X") + } else 0x00 + }; + if vex_byte0? & (n64_bit_modep_var | not_bool(n64_bit_modep_var) & les_lds_distinguishing_byte : bits(8)[7 .. 6] == 0b11) then { + let temp_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, 0x000000000001); + let vex_prefixes : vex_prefixes = [Mk_vex_prefixes(0x000000) with byte0 = opcode_vex_evex_byte]; + let vex_prefixes : vex_prefixes = [vex_prefixes with byte1 = les_lds_distinguishing_byte : bits(8)]; + vex_decode_and_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, vex_prefixes) + } else { + let opcode_evex_byte : bits(8) = opcode_vex_evex_byte; + let evex_byte0? : bool = opcode_evex_byte == 0x62; + let bound_distinguishing_byte : bits(8) = { + if evex_byte0? then { + rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X") + } else 0x00 + }; + if evex_byte0? & (n64_bit_modep_var | not_bool(n64_bit_modep_var) & bound_distinguishing_byte[7 .. 6] == 0b11) then { + let temp_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, 0x000000000001); + let evex_prefixes : evex_prefixes = [Mk_evex_prefixes(0x00000000) with byte0 = opcode_evex_byte]; + let evex_prefixes : evex_prefixes = [evex_prefixes with byte1 = bound_distinguishing_byte]; + evex_decode_and_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, evex_prefixes) + } else { + let opcode_byte : bits(8) = opcode_evex_byte; + let modr_m? : bool = one_byte_opcode_modr_m_p(proc_mode, opcode_byte); + let modr_m : modr_m = { + if modr_m? then { + if vex_byte0? | evex_byte0? then { + Mk_modr_m(les_lds_distinguishing_byte : bits(8)) + } else { + Mk_modr_m(rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X")) + } + } else { + Mk_modr_m(0x00) + } + }; + let temp_rip : sbits(48) = { + if modr_m? then { + add_to_iptr(proc_mode, temp_rip, 0x000000000001) + } else { + temp_rip + } + }; + let sib? : bool = modr_m? & (let p4? : bool = 0x67 == prefixes[adr] in + let n16_bit_addressp : bool = 2 == select_address_size(proc_mode, Some(prefixes)) in + x86_decode_sib_p(modr_m, n16_bit_addressp)); + let sib : sib = { + if sib? then { + Mk_sib(rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X")) + } else { + Mk_sib(0x00) + } + }; + let temp_rip : sbits(48) = { + if sib? then { + add_to_iptr(proc_mode, temp_rip, 0x000000000001) + } else { + temp_rip + } + }; + one_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode_byte, modr_m, sib) + } + } + } +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/structures.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/structures.sail new file mode 100644 index 000000000..8371c0e9b --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/structures.sail @@ -0,0 +1,248 @@ +$ifndef __X86_STRUCTURES +$define __X86_STRUCTURES + +/*! Functions to collect legacy prefix bytes from an x86 instruction +The field @ ( ' num ' ) of @ ( ' prefixes ' ) not only includes the number of + legacy prefixes present in an instruction, but also the number of REX bytes, + even though REX bytes are not stored in this structure. See @ ( tsee + get-prefixes ) for details.
+Functions to decode and collect VEX prefix bytes from an x86 + instruction */ +bitfield vex_prefixes : bits(24) = {byte2 : 23..16, byte1 : 15..8, byte0 : 7..0} + +/*! Returns @ ( ' t ' ) if byte0 of the @ ( ' vex-prefixes ' ) structure is + either @ ( ' *vex2-byte0* ' ) or @ ( ' *vex3-byte0* ' ) ; returns @ ( ' nil ' ) otherwise */ +val vex_prefixes_byte0_p : vex_prefixes -> bool + +function vex_prefixes_byte0_p vex_prefixes = { + let byte0 : bits(8) = vex_prefixes[byte0]; + byte0 == 0xc5 | byte0 == 0xc4 +} + +bitfield vex2_byte1 : bits(8) = {r : 7..7, vvvv : 6..3, l : 2..2, pp : 1..0} + +bitfield vex3_byte1 : bits(8) = {r : 7..7, x : 6..6, b : 5..5, m_mmmm : 4..0} + +bitfield vex3_byte2 : bits(8) = {w : 7..7, vvvv : 6..3, l : 2..2, pp : 1..0} + +/*! Returns @ ( ' t ' ) if the @ ( ' vex-prefixes ' ) , irrespective of whether + they are two- or three-byte form, indicate the map that begins with the + escape bytes @ ( ' bytes ' ) */ +val vex_prefixes_map_p : (bits(16), vex_prefixes) -> bool + +function vex_prefixes_map_p (bytes, vex_prefixes) = { + let byte0 : bits(8) = vex_prefixes[byte0]; + let byte1 : bits(8) = vex_prefixes[byte1]; + (match bytes { + 15 => byte0 == 0xc5 | byte0 == 0xc4 & Mk_vex3_byte1(byte1)[m_mmmm] == 0b00001, + _ => byte0 == 0xc4 & (if bytes == 0x0f38 then { + Mk_vex3_byte1(byte1)[m_mmmm] == 0b00010 + } else { + Mk_vex3_byte1(byte1)[m_mmmm] == 0b00011 + }) + }) : bool +} + +/*! Get the @ ( ' VVVV ' ) field of @ ( ' vex-prefixes ' ) ; cognizant of the two- + or three-byte VEX prefixes form */ +val vex_get_vvvv : vex_prefixes -> sbits(5) + +function vex_get_vvvv vex_prefixes = { + (match vex_prefixes[byte0] { + 197 => sail_zero_extend(Mk_vex2_byte1(vex_prefixes[byte1])[vvvv], 5), + 196 => sail_zero_extend(Mk_vex3_byte2(vex_prefixes[byte2])[vvvv], 5), + _ => 0b11111 + }) : sbits(5) +} + +/*! Get the @ ( ' L ' ) field of @ ( ' vex-prefixes ' ) ; cognizant of the two- or + three-byte VEX prefixes form */ +val vex_get_l : vex_prefixes -> sbits(2) + +function vex_get_l vex_prefixes = { + (match vex_prefixes[byte0] { + 197 => sail_zero_extend(Mk_vex2_byte1(vex_prefixes[byte1])[l], 2), + 196 => sail_zero_extend(Mk_vex3_byte2(vex_prefixes[byte2])[l], 2), + _ => 0b11 + }) : sbits(2) +} + +/*! Get the @ ( ' PP ' ) field of @ ( ' vex-prefixes ' ) ; cognizant of the two- or + three-byte VEX prefixes form */ +val vex_get_pp : vex_prefixes -> sbits(3) + +function vex_get_pp vex_prefixes = { + (match vex_prefixes[byte0] { + 197 => sail_zero_extend(Mk_vex2_byte1(vex_prefixes[byte1])[pp], 3), + 196 => sail_zero_extend(Mk_vex3_byte2(vex_prefixes[byte2])[pp], 3), + _ => 0b111 + }) : sbits(3) +} + +/*! Get the @ ( ' W ' ) field of @ ( ' vex-prefixes ' ) ; cognizant of the two- or + three-byte VEX prefixes form */ +val vex_get_w : vex_prefixes -> bits(1) + +function vex_get_w vex_prefixes = { + (match vex_prefixes[byte0] { + 196 => Mk_vex3_byte2(vex_prefixes[byte2])[w], + _ => 0b0 + }) : bits(1) +} + +/*! Functions to decode and collect EVEX prefix bytes from an x86 + instruction */ +bitfield evex_prefixes : bits(32) = {byte3 : 31..24, byte2 : 23..16, byte1 : 15..8, byte0 : 7..0} + +bitfield evex_byte1 : bits(8) = {r : 7..7, x : 6..6, b : 5..5, r_prime : 4..4, res : 3..2, mm : 1..0} + +bitfield evex_byte2 : bits(8) = {w : 7..7, vvvv : 6..3, res : 2..2, pp : 1..0} + +bitfield evex_byte3 : bits(8) = {z : 7..7, vl_rc : 6..5, b : 4..4, v_prime : 3..3, aaa : 2..0} + +/*! Get the @ ( ' aaa ' ) field ( embedded opmask ) of @ ( ' evex-prefixes ' ) */ +val evex_get_aaa : evex_prefixes -> bits(3) + +function evex_get_aaa evex_prefixes = { + Mk_evex_byte3(evex_prefixes[byte3])[aaa] +} + +/*! Get the @ ( ' z ' ) field ( embedded opmask ) of @ ( ' evex-prefixes ' ) */ +val evex_get_z : evex_prefixes -> bits(1) + +function evex_get_z evex_prefixes = { + Mk_evex_byte3(evex_prefixes[byte3])[z] +} + +/*! Get the @ ( ' VVVV ' ) field of @ ( ' evex-prefixes ' ) */ +val evex_get_vvvv : evex_prefixes -> bits(4) + +function evex_get_vvvv evex_prefixes = { + Mk_evex_byte2(evex_prefixes[byte2])[vvvv] +} + +/*! Get the @ ( ' v-prime ' ) field of @ ( ' evex-prefixes ' ) */ +val evex_get_v_prime : evex_prefixes -> bits(1) + +function evex_get_v_prime evex_prefixes = { + Mk_evex_byte3(evex_prefixes[byte3])[v_prime] +} + +/*! Get the @ ( ' vl/rc ' ) field of @ ( ' evex-prefixes ' ) */ +val evex_get_vl_rc : evex_prefixes -> bits(2) + +function evex_get_vl_rc evex_prefixes = { + Mk_evex_byte3(evex_prefixes[byte3])[vl_rc] +} + +/*! Get the @ ( ' PP ' ) field of @ ( ' evex-prefixes ' ) */ +val evex_get_pp : evex_prefixes -> bits(2) + +function evex_get_pp evex_prefixes = { + Mk_evex_byte2(evex_prefixes[byte2])[pp] +} + +/*! Get the @ ( ' W ' ) field of @ ( ' evex-prefixes ' ) */ +val evex_get_w : evex_prefixes -> bits(1) + +function evex_get_w evex_prefixes = { + Mk_evex_byte2(evex_prefixes[byte2])[w] +} + +/*! Bitstruct definitions to store a ModR/M byte and its fields */ +bitfield modr_m : bits(8) = {mod : 7..6, reg : 5..3, r_m : 2..0} + +/*! Bitstruct definitions to store a SIB byte and its fields */ +bitfield sib : bits(8) = {scale : 7..6, index : 5..3, base : 2..0} + +bitfield rflagsbits : bits(32) = + { + res5 : 31..22, + id : 21..21, + vip : 20..20, + vif : 19..19, + ac : 18..18, + vm : 17..17, + rf : 16..16, + res4 : 15..15, + nt : 14..14, + iopl : 13..12, + of : 11..11, + df : 10..10, + intf : 9..9, + tf : 8..8, + sf : 7..7, + zf : 6..6, + res3 : 5..5, + af : 4..4, + res2 : 3..3, + pf : 2..2, + res1 : 1..1, + cf : 0..0 + } + +bitfield cr0bits : bits(32) = + { + pg : 31..31, + cd : 30..30, + nw : 29..29, + res3 : 28..19, + am : 18..18, + res2 : 17..17, + wp : 16..16, + res1 : 15..6, + ne : 5..5, + et : 4..4, + ts : 3..3, + em : 2..2, + mp : 1..1, + pe : 0..0 + } + +bitfield cr3bits : bits(64) = {res3 : 63..52, pdb : 51..12, res2 : 11..5, pcd : 4..4, pwt : 3..3, res1 : 2..0} + +bitfield cr4bits : bits(22) = + { + smap : 21..21, + smep : 20..20, + res2 : 19..19, + osxsave : 18..18, + pcide : 17..17, + fsgsbase : 16..16, + res1 : 15..15, + smxe : 14..14, + vmxe : 13..13, + la57 : 12..12, + umip : 11..11, + osxmmexcpt : 10..10, + osfxsr : 9..9, + pce : 8..8, + pge : 7..7, + mce : 6..6, + pae : 5..5, + pse : 4..4, + de : 3..3, + tsd : 2..2, + pvi : 1..1, + vme : 0..0 + } + +bitfield cr8bits : bits(4) = {cr8_trpl : 3..0} + +bitfield xcr0bits : bits(64) = + { + res2 : 63..10, + pkru_state : 9..9, + res1 : 8..8, + hi16_zmm_state : 7..7, + zmm_hi256_state : 6..6, + opmask_state : 5..5, + bndcsr_state : 4..4, + bndreg_state : 3..3, + avx_state : 2..2, + sse_state : 1..1, + fpu_mmx_state : 0..0 + } + +bitfield ia32_eferbits : bits(12) = {nxe : 11..11, lma : 10..10, res2 : 9..9, lme : 8..8, res1 : 7..1, sce : 0..0} +$endif diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/sub_spec.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/sub_spec.sail new file mode 100644 index 000000000..6bb62355b --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/sub_spec.sail @@ -0,0 +1,132 @@ + +val gpr_sub_spec_1 : (bits(8), bits(8), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function gpr_sub_spec_1 (dst, src, input_rflags) = { + let signed_raw_result : sbits(9) = bits_of_int(signed(dst) - signed(src), 9); + let result : bits(8) = truncate(signed_raw_result, 8); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(src)); + let pf : bits(1) = pf_spec8(result); + let af : bits(1) = sub_af_spec8(dst, src); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : bits(1) = of_spec8(signed_raw_result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sub_spec_2 : (bits(16), bits(16), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function gpr_sub_spec_2 (dst, src, input_rflags) = { + let signed_raw_result : sbits(17) = bits_of_int(signed(dst) - signed(src), 17); + let result : bits(16) = truncate(signed_raw_result, 16); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(src)); + let pf : bits(1) = pf_spec16(result); + let af : bits(1) = sub_af_spec16(dst, src); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : bits(1) = of_spec16(signed_raw_result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sub_spec_4 : (bits(32), bits(32), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function gpr_sub_spec_4 (dst, src, input_rflags) = { + let signed_raw_result : sbits(33) = bits_of_int(signed(dst) - signed(src), 33); + let result : bits(32) = truncate(signed_raw_result, 32); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(src)); + let pf : bits(1) = pf_spec32(result); + let af : bits(1) = sub_af_spec32(dst, src); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : bits(1) = of_spec32(signed_raw_result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sub_spec_8 : (bits(64), bits(64), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function gpr_sub_spec_8 (dst, src, input_rflags) = { + let signed_raw_result : sbits(65) = bits_of_int(signed(dst) - signed(src), 65); + let result : bits(64) = truncate(signed_raw_result, 64); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(src)); + let pf : bits(1) = pf_spec64(result); + let af : bits(1) = sub_af_spec64(dst, src); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : bits(1) = of_spec64(signed_raw_result); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sbb_spec_1 : (bits(8), bits(8), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function gpr_sbb_spec_1 (dst, src, input_rflags) = { + let input_cf : bits(1) = input_rflags[cf]; + let signed_raw_result : sbits(10) = bits_of_int(signed(dst) - signed(bits_of_int(signed(src) + unsigned(input_cf), 9)), 10); + let result : bits(8) = truncate(signed_raw_result, 8); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(bits_of_int(unsigned(input_cf) + unsigned(src), 9))); + let pf : bits(1) = pf_spec8(result); + let af : bits(1) = sbb_af_spec8(dst, src, input_cf); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : bits(1) = of_spec8(truncate(signed_raw_result, 9)); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sbb_spec_2 : (bits(16), bits(16), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function gpr_sbb_spec_2 (dst, src, input_rflags) = { + let input_cf : bits(1) = input_rflags[cf]; + let signed_raw_result : sbits(18) = bits_of_int(signed(dst) - signed(bits_of_int(signed(src) + unsigned(input_cf), 17)), 18); + let result : bits(16) = truncate(signed_raw_result, 16); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(bits_of_int(unsigned(input_cf) + unsigned(src), 17))); + let pf : bits(1) = pf_spec16(result); + let af : bits(1) = sbb_af_spec16(dst, src, input_cf); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : bits(1) = of_spec16(truncate(signed_raw_result, 17)); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sbb_spec_4 : (bits(32), bits(32), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function gpr_sbb_spec_4 (dst, src, input_rflags) = { + let input_cf : bits(1) = input_rflags[cf]; + let signed_raw_result : sbits(34) = bits_of_int(signed(dst) - signed(bits_of_int(signed(src) + unsigned(input_cf), 33)), 34); + let result : bits(32) = truncate(signed_raw_result, 32); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(bits_of_int(unsigned(input_cf) + unsigned(src), 33))); + let pf : bits(1) = pf_spec32(result); + let af : bits(1) = sbb_af_spec32(dst, src, input_cf); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : bits(1) = of_spec32(truncate(signed_raw_result, 33)); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} + +val gpr_sbb_spec_8 : (bits(64), bits(64), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function gpr_sbb_spec_8 (dst, src, input_rflags) = { + let input_cf : bits(1) = input_rflags[cf]; + let signed_raw_result : sbits(66) = bits_of_int(signed(dst) - signed(bits_of_int(signed(src) + unsigned(input_cf), 65)), 66); + let result : bits(64) = truncate(signed_raw_result, 64); + let cf : bits(1) = bool_to_bits(unsigned(dst) < unsigned(bits_of_int(unsigned(input_cf) + unsigned(src), 65))); + let pf : bits(1) = pf_spec64(result); + let af : bits(1) = sbb_af_spec64(dst, src, input_cf); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : bits(1) = of_spec64(truncate(signed_raw_result, 65)); + let output_rflags : rflagsbits = [input_rflags with cf, pf, af, zf, sf, of]; + let undefined_flags : rflagsbits = Mk_rflagsbits(0x00000000); + (result, output_rflags, undefined_flags) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/subroutine.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/subroutine.sail new file mode 100644 index 000000000..362838bc3 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/subroutine.sail @@ -0,0 +1,123 @@ + +val x86_call_e8_op_en_m : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_call_e8_op_en_m (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-CALL-E8-OP/EN-M"; + let offset_size : {|1, 2, 4|} = check_range(0, 4, select_operand_size(proc_mode, false, rex_byte, false, prefixes, false, true, true)); + let rel16_32 : sbits(32) = truncate(rime_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), offset_size, sail_sign_extend(temp_rip, 64), None(), 1, ":X", false, false), 32); + let next_rip : sbits(49) = sail_sign_extend(add_to_iptr(proc_mode, temp_rip, bits_of_int(offset_size, 48)), 49); + let badlength? : option(int) = check_instruction_length(start_rip, truncate(next_rip, 48), 0b000); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let call_rip : sbits(48) = add_to_iptr(proc_mode, truncate(next_rip, 48), sail_sign_extend(rel16_32, 48)); + let rsp_var : sbits(64) = read_sptr(proc_mode); + let addr_size : address_size = select_address_size(proc_mode, None()); + let new_rsp : sbits(64) = add_to_sptr(proc_mode, rsp_var, bits_of_int(negate(addr_size), 64)); + if in_64bit_mode(proc_mode) then { + wime_size(Mode_64bit, select_address_size(proc_mode, Some(prefixes)), addr_size, sail_sign_extend(truncate(new_rsp, 48), 64), None(), 2, sail_sign_extend(next_rip, 128), alignment_checking_enabled_p(), false) + } else { + wme_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), addr_size, sail_sign_extend(truncate(new_rsp, 48), 64), None(), 2, if addr_size == 2 then { + sail_zero_extend(sail_zero_extend(truncate(next_rip, 16), 32), 128) + } else { + sail_zero_extend(truncate(next_rip, 32), 128) + }, alignment_checking_enabled_p(), false) + }; + write_iptr(proc_mode, call_rip); + write_sptr(proc_mode, new_rsp) +} + +val x86_call_ff_2_op_en_m : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_call_ff_2_op_en_m (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-CALL-FF/2-OP/EN-M"; + let r_m : bits(3) = modr_m[r_m]; + let mod_var : bits(2) = modr_m[mod]; + let reg : bits(3) = modr_m[reg]; + let operand_size : {|2, 4, 8|} = check_range(2, 8, select_operand_size(proc_mode, false, rex_byte, false, prefixes, true, true, true)); + let p2 : bits(8) = prefixes[seg]; + let p4? : bool = 0x67 == prefixes[adr]; + let seg_reg : seg_reg_idx = select_segment_register(proc_mode, prefixes, mod_var, r_m, sib); + let inst_ac? : bool = true; + let (call_rip, increment_rip_by, addr) : (bits(128), {|0, 1, 2, 4|}, sbits(64)) = x86_operand_from_modr_m_and_sib_bytes(proc_mode, 0b0, operand_size, inst_ac?, false, seg_reg, prefixes, temp_rip, rex_byte, r_m, mod_var, sib, 0b000); + let call_rip : sbits(64) = truncate(call_rip, 64); + let increment_rip_by : bits(3) = bits_of_int(increment_rip_by, 3); + let next_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, sail_zero_extend(increment_rip_by, 48)); + let badlength? : option(int) = check_instruction_length(start_rip, next_rip, 0b000); + if is_some(badlength?) then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let call_rip : sbits(64) = { + if in_64bit_mode(proc_mode) then { + call_rip + } else { + call_rip + } + }; + if not_bool(if in_64bit_mode(proc_mode) then { + canonical_address_p(signed(call_rip)) + } else { + 0 <= signed(call_rip) & signed(call_rip) <= unsigned(seg_hidden_limits[1]) + }) then x86_fault("(!!FAULT-FRESH :GP 0 :BAD-RETURN-ADDRESS CALL-RIP)"); + let rsp_var : sbits(64) = read_sptr(proc_mode); + let addr_size : address_size = select_address_size(proc_mode, None()); + let new_rsp : sbits(64) = add_to_sptr(proc_mode, rsp_var, bits_of_int(negate(addr_size), 64)); + let check_alignment? : bool = alignment_checking_enabled_p(); + if in_64bit_mode(proc_mode) then { + wime_size(Mode_64bit, select_address_size(proc_mode, Some(prefixes)), addr_size, sail_sign_extend(truncate(new_rsp, 48), 64), None(), 2, sail_sign_extend(next_rip, 128), alignment_checking_enabled_p(), false) + } else { + wme_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), addr_size, sail_sign_extend(truncate(new_rsp, 48), 64), None(), 2, if addr_size == 2 then { + sail_zero_extend(sail_zero_extend(truncate(next_rip, 16), 32), 128) + } else { + sail_zero_extend(truncate(next_rip, 32), 128) + }, alignment_checking_enabled_p(), false) + }; + write_iptr(proc_mode, truncate(call_rip, 48)); + write_sptr(proc_mode, new_rsp) +} + +val x86_ret : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_ret (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-RET"; + let rsp_var : sbits(64) = read_sptr(proc_mode); + let operand_size : {|2, 4, 8|} = check_range(2, 8, select_operand_size(proc_mode, false, rex_byte, false, Mk_prefixes(0x0000000000000), true, true, true)); + let new_rsp : sbits(48) = { + if opcode == 0xc3 then { + let new_rsp : sbits(64) = add_to_sptr(proc_mode, rsp_var, bits_of_int(operand_size, 64)); + truncate(new_rsp, 48) + } else { + let imm16 : bits(16) = rme16(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X", false); + let new_rsp : sbits(64) = add_to_sptr(proc_mode, rsp_var, bits_of_int(operand_size + unsigned(imm16), 64)); + truncate(new_rsp, 48) + } + }; + let badlength? : bool = opcode == 0xc2 & is_some(check_instruction_length(start_rip, temp_rip, 0b010)); + if badlength? then x86_fault("(!!FAULT-FRESH :GP 0 :INSTRUCTION-LENGTH BADLENGTH?)"); + let check_alignment? : bool = alignment_checking_enabled_p(); + let tos : sbits(64) = { + if operand_size == 8 then { + rime_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), operand_size, rsp_var, None(), 2, ":R", check_alignment?, false) + } else { + truncate(rme_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), operand_size, rsp_var, None(), 2, ":R", check_alignment?, false), 64) + } + }; + if not_bool(if in_64bit_mode(proc_mode) then { + canonical_address_p(signed(tos)) + } else { + let cs_limit : bits(32) = seg_hidden_limits[1]; + 0 <= signed(tos) & signed(tos) <= unsigned(cs_limit) + }) then x86_fault("(!!FAULT-FRESH :GP 0 :BAD-RETURN-ADDRESS TOS)"); + write_sptr(proc_mode, sail_sign_extend(new_rsp, 64)); + write_rip(truncate(tos, 48)) +} + +val x86_leave : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_leave (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-LEAVE"; + let operand_size : {|2, 4, 8|} = check_range(2, 8, select_operand_size(proc_mode, false, rex_byte, false, prefixes, true, true, false)); + let rbp_ebp_bp : bits(64) = rgfi_size(bits_of_int(operand_size, 4), 0x5, 0x00); + let inst_ac? : bool = alignment_checking_enabled_p(); + let sailval : bits(128) = rme_size(proc_mode, select_address_size(proc_mode, Some(prefixes)), operand_size, rbp_ebp_bp, None(), 2, ":R", inst_ac?, false); + let new_rsp : sbits(64) = add_to_sptr(proc_mode, rbp_ebp_bp, bits_of_int(operand_size, 64)); + write_rgfi_size(bits_of_int(operand_size, 4), 0x5, truncate(sailval, 64), rex_byte); + write_sptr(proc_mode, new_rsp); + write_iptr(proc_mode, temp_rip) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/three_byte_opcodes_dispatch.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/three_byte_opcodes_dispatch.sail new file mode 100644 index 000000000..50688ce8c --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/three_byte_opcodes_dispatch.sail @@ -0,0 +1 @@ +$include "./prefix_modrm_sib_decoding.sail" diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top.sail new file mode 100644 index 000000000..eaf48c0e3 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top.sail @@ -0,0 +1,99 @@ +$include "./arith_and_logic.sail" +$include "./bit.sail" +$include "./conditional.sail" +$include "./divide.sail" +$include "./exchange.sail" +$include "./jump_and_loop.sail" +$include "./move.sail" +$include "./multiply.sail" +$include "./push_and_pop.sail" +$include "./rotate_and_shift.sail" +$include "./segmentationinst.sail" +$include "./signextend.sail" +$include "./string.sail" +$include "./syscall.sail" +$include "./subroutine.sail" +$include "./topfp.sail" + +val x86_hlt : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_hlt (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-HLT"; + write_iptr(proc_mode, temp_rip); + x86_model_error(":LEGAL-HALT") +} + +val x86_cmc_clc_stc_cld_std : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_cmc_clc_stc_cld_std (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-CMC/CLC/STC/CLD/STD"; + (match opcode { + 245 => { + let cf : bits(1) = { + let rflags_var : rflagsbits = rflags; + rflags_var[cf] + }; + let not_cf : {|0, 1|} = { + if cf == 0b1 then 0 else 1 + }; + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with cf = bits_of_int(not_cf, 1)]; + rflags = new_rflags + }, + 248 => { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with cf = 0b0]; + rflags = new_rflags + }, + 249 => { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with cf = 0b1]; + rflags = new_rflags + }, + 252 => { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with df = 0b0]; + rflags = new_rflags + }, + _ => { + let rflags_var : rflagsbits = rflags; + let new_rflags : rflagsbits = [rflags_var with df = 0b1]; + rflags = new_rflags + } + }) : unit; + write_iptr(proc_mode, temp_rip) +} + +val x86_sahf : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_sahf (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-SAHF"; + let ax : bits(16) = rr16(0x0); + let ah : bits(8) = bits_of_int(ash(unsigned(ax), -8), 8); + let rflags_var : rflagsbits = rflags; + let cf : bits(1) = Mk_rflagsbits(sail_zero_extend(ah, 32))[cf]; + let pf : bits(1) = Mk_rflagsbits(sail_zero_extend(ah, 32))[pf]; + let af : bits(1) = Mk_rflagsbits(sail_zero_extend(ah, 32))[af]; + let zf : bits(1) = Mk_rflagsbits(sail_zero_extend(ah, 32))[zf]; + let sf : bits(1) = Mk_rflagsbits(sail_zero_extend(ah, 32))[sf]; + let new_rflags : rflagsbits = [rflags_var with sf, zf, af, pf, cf]; + rflags = new_rflags; + write_iptr(proc_mode, temp_rip) +} + +val x86_lahf : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), modr_m, sib) -> unit + +function x86_lahf (proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) = { + let ctx : string = "X86-LAHF"; + let rflags_var : rflagsbits = rflags; + let cf : bits(1) = rflags_var[cf]; + let pf : bits(1) = rflags_var[pf]; + let af : bits(1) = rflags_var[af]; + let zf : bits(1) = rflags_var[zf]; + let sf : bits(1) = rflags_var[sf]; + let new_ah : rflagsbits = [Mk_rflagsbits(0x00000000) with sf, zf, res3 = 0b0, af, res2 = 0b0, pf, res1 = 0b1, cf]; + let ax : bits(16) = rr16(0x0); + let new_ax : bits(16) = bits_of_int(logior(unsigned(logand(0x00ff, ax)), ash(unsigned(new_ah.bits), 8)), 16); + wr16(0x0, new_ax); + write_iptr(proc_mode, temp_rip) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top_level_memory.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top_level_memory.sail new file mode 100644 index 000000000..881cf5030 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/top_level_memory.sail @@ -0,0 +1,491 @@ + +/*! Check the alignment of a linear address. ++ Besides the address to check for alignment, + this function takes as argument the operand size + ( from which the alignment to check is determined ) + and a flag indicating whether the address to check for alignment + contains a memory operand of the form m16:16, m16:32, or m16:64 + ( see Intel manual, Mar ' 17, Volume 2, Section 3.1.1.3 ) . +
++ Words, doublewords, quadwords, and double quadwords + must be aligned at boundaries of 2, 4, 8, or 16 bytes. + Memory pointers of the form m16:xx must be aligned so that + their xx portion is aligned as a word, doubleword, or quadword; + this automatically guarantees that their m16 portion is aligned as a word. + See Intel manual, Mar ' 17, Volume 1, Section 4.1.1. + See AMD manual, Dec ' 17, Volume 2, Table 8-7 + ( note that the table does not mention explicitly + memory pointers of the form m16:64 ) . +
++ If the operand size is 6, the operand must be an m16:32 pointer. + If the operand size is 10, the operand must an m16:64 pointer. + If the operand size is 4, it may be either an m16:16 pointer or not; + in this case, the @ ( ' memory-ptr? ' ) argument is used to + determine whether the address should be aligned + at a word or doubleword boundary. + If the operand size is 1, 2, 8, or 16, + it cannot be a memory pointer of the form m16:xx. +
*/ +function address_aligned_p (addr, operand_size, memory_ptr?) = { + (match operand_size { + 1 => true, + 6 => logand(addr, 0x000000000003) == 0x000000000000, + 10 => logand(addr, 0x000000000007) == 0x000000000000, + _ => { + if memory_ptr? & operand_size == 4 then { + logand(addr, 0x000000000001) == 0x000000000000 + } else { + logand(addr, bits_of_int(check_range(0, 65, operand_size - 1), 48)) == 0x000000000000 + } + } + }) : bool +} + +/*! Read an unsigned 8-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML08 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme08 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string) -> bits(8) + +function rme08 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x) = { + load_bytes_from_ea(proc_mode, addr_size, 1, eff_addr, base_reg, seg_reg, r_x, false, false) +} + +/*! Read a signed 8-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RIML08 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +overload rime08 = {rme08} + +/*! Read an unsigned 16-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML16 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme16 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool) -> bits(16) + +function rme16 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?) = { + load_bytes_from_ea(proc_mode, addr_size, 2, eff_addr, base_reg, seg_reg, r_x, check_alignment?, false) +} + +/*! Read a signed 16-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RIML16 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +overload rime16 = {rme16} + +/*! Read an unsigned 32-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML32 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme32 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool, bool) -> bits(32) + +function rme32 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?) = { + load_bytes_from_ea(proc_mode, addr_size, 4, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?) +} + +/*! Read a signed 32-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RIML32 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +overload rime32 = {rme32} + +/*! Read an unsigned 48-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML48 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme48 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool) -> bits(48) + +function rme48 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?) = { + load_bytes_from_ea(proc_mode, addr_size, 6, eff_addr, base_reg, seg_reg, r_x, check_alignment?, false) +} + +/*! Read an unsigned 64-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML64 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme64 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool) -> bits(64) + +function rme64 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?) = { + load_bytes_from_ea(proc_mode, addr_size, 8, eff_addr, base_reg, seg_reg, r_x, check_alignment?, false) +} + +/*! Read a signed 64-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RIML64 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +overload rime64 = {rme64} + +/*! Read an unsigned 80-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML80 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme80 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool) -> bits(80) + +function rme80 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?) = { + load_bytes_from_ea(proc_mode, addr_size, 10, eff_addr, base_reg, seg_reg, r_x, check_alignment?, false) +} + +/*! Read an unsigned 128-bit value from memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address using @ ( see ea-to-la ) . If this translation is + successful and no other errors ( like alignment errors ) occur, then + @ ( see RML128 ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) .
*/ +val rme128 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool) -> bits(128) + +function rme128 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, r_x, check_alignment?) = { + load_bytes_from_ea(proc_mode, addr_size, 16, eff_addr, base_reg, seg_reg, r_x, check_alignment?, false) +} + +/*! Read an unsigned value with the specified number of bytes + from memory via an effective address. +The effective address is translated to a canonical linear address using + @ ( see ea-to-la ) . If this translation is successful and no other errors ( like + alignment errors ) occur, then @ ( see rml-size ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored; see Intel manual, Dec ' 17, Volume 2, Section 4.8.1
*/ +val rme_size : (proc_mode, address_size, {|1, 2, 4, 6, 8, 10, 16|}, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool, bool) -> bits(128) + +function rme_size (proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?) = { + sail_zero_extend(load_bytes_from_ea(proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?), 128) +} + +/*! Read a signed value with the specified number of bytes + from memory via an effective address. +The effective address is translated to a canonical linear address using + @ ( see ea-to-la ) . If this translation is successful and no other errors ( like + alignment errors ) occur, then @ ( see riml-size ) is called.
+Prior to the effective address translation, we check whether read + access is allowed. The only case in which it is not allowed is when a + read access is attempted on an execute-only code segment, in 32-bit + mode. In 64-bit mode, the R bit of the code segment descriptor is + ignored; see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1
*/ +val rime_size : (proc_mode, address_size, {|1, 2, 4, 6, 8|}, sbits(64), option(base_reg_idx), seg_reg_idx, string, bool, bool) -> sbits(64) + +function rime_size (proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?) = { + sail_sign_extend(load_bytes_from_ea(proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, r_x, check_alignment?, mem_ptr?), 64) +} + +/*! Write an unsigned 8-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML08 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme08 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(8)) -> unit + +function wme08 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data) = { + store_bytes_to_ea(proc_mode, addr_size, 1, eff_addr, base_reg, seg_reg, data, false, false) +} + +/*! Write a signed 8-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WIML08 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +overload wime08 = {wme08} + +/*! Write an unsigned 16-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML16 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme16 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(16), bool) -> unit + +function wme16 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?) = { + store_bytes_to_ea(proc_mode, addr_size, 2, eff_addr, base_reg, seg_reg, data, check_alignment?, false) +} + +/*! Write a signed 16-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WIML16 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +overload wime16 = {wme16} + +/*! Write an unsigned 32-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML32 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme32 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(32), bool, bool) -> unit + +function wme32 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?, mem_ptr?) = { + store_bytes_to_ea(proc_mode, addr_size, 4, eff_addr, base_reg, seg_reg, data, check_alignment?, mem_ptr?) +} + +/*! Write a signed 32-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WIML32 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +overload wime32 = {wme32} + +/*! Write an unsigned 48-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML48 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme48 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(48), bool) -> unit + +function wme48 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?) = { + store_bytes_to_ea(proc_mode, addr_size, 6, eff_addr, base_reg, seg_reg, data, check_alignment?, false) +} + +/*! Write an unsigned 64-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML64 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme64 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(64), bool) -> unit + +function wme64 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?) = { + store_bytes_to_ea(proc_mode, addr_size, 8, eff_addr, base_reg, seg_reg, data, check_alignment?, false) +} + +/*! Write a signed 64-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WIML64 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +overload wime64 = {wme64} + +/*! Write an unsigned 80-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML80 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme80 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(80), bool) -> unit + +function wme80 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?) = { + store_bytes_to_ea(proc_mode, addr_size, 10, eff_addr, base_reg, seg_reg, data, check_alignment?, false) +} + +/*! Write an unsigned 128-bit value to memory via an effective address. +The effective address @ ( ' eff-addr ' ) is translated to a canonical + linear address. If this translation is successful and no other error + occurs ( like alignment errors ) , then @ ( see WML128 ) is + called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme128 : (proc_mode, address_size, sbits(64), option(base_reg_idx), seg_reg_idx, bits(128), bool) -> unit + +function wme128 (proc_mode, addr_size, eff_addr, base_reg, seg_reg, data, check_alignment?) = { + store_bytes_to_ea(proc_mode, addr_size, 16, eff_addr, base_reg, seg_reg, data, check_alignment?, false) +} + +/*! Write an unsigned value with the specified number of bytes + to memory via an effective address. +The effective address is translated to a canonical linear address. If + this translation is successful and no other errors occur ( like alignment + errors ) , then @ ( see wml-size ) is called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +val wme_size : forall 'nbytes, 'nbytes in {1, 2, 4, 6, 8, 10, 16}. + (proc_mode, address_size, int('nbytes), sbits(64), option(base_reg_idx), seg_reg_idx, bits(128), bool, bool) -> unit + +function wme_size (proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, data, check_alignment?, mem_ptr?) = { + store_bytes_to_ea(proc_mode, addr_size, nbytes, eff_addr, base_reg, seg_reg, truncate(data, 8 * nbytes), check_alignment?, mem_ptr?) +} + +/*! Write a signed value with the specified number of bytes + to memory via an effective address. +The effective address is translated to a canonical linear address. If + this translation is successful and no other errors occur ( like alignment + errors ) , then @ ( see wiml-size ) is called.
+Prior to the effective address translation, we check whether write + access is allowed. In 32-bit mode, write access is allowed in data + segments ( DS, ES, FS, GS, and SS ) if the W bit in the segment + descriptor is 1; write access is disallowed in code segments ( this is + not explicitly mentioned in Intel manual, May ' 18, Volume 3, Section + 3.4.5.1, but it seems reasonable ) . In 64-bit mode, the W bit is + ignored ( see Atmel manual, Dec ' 17, Volume 2, Section 4.8.1 ) ; by + analogy, we allow write access to the code segment as well. + These checks may be slightly optimized using the invariant that + SS.W must be 1 when SS is loaded.
*/ +overload wime_size = {wme_size} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/two_byte_opcodes_dispatch.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/two_byte_opcodes_dispatch.sail new file mode 100644 index 000000000..507245df7 --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/two_byte_opcodes_dispatch.sail @@ -0,0 +1,2805 @@ +$include "./three_byte_opcodes_dispatch.sail" + +/*! Two-byte opcode dispatch function. +@ ( ' two-byte-opcode-execute ' ) is the doorway to the two-byte + opcode map, and will lead to the three-byte opcode map if @ ( ' opcode ' ) is + either @ ( ' #x38 ' ) or @ ( ' #x3A ' ) .
*/ +val two_byte_opcode_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8), bits(8), modr_m, sib) -> unit + +function two_byte_opcode_execute (proc_mode, start_rip, temp_rip, prefixes, mandatory_prefix, rex_byte, opcode, modr_m, sib) = { + if ext_two_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, mandatory_prefix, rex_byte, opcode, modr_m, sib) then { + return() + } else { + (match opcode { + 0 => { + if modr_m[reg] == 0b000 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b000 & modr_m[mod] == 0b11 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_lldt(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b101 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 1 => { + if modr_m[reg] == 0b000 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b000 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b001 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b000 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b010 then { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b000 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b011 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b000 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b100 then { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b000 then { + if feature_flags_fn([|":MONITOR"|]) == 0 | not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b001 then { + if feature_flags_fn([|":MONITOR"|]) == 0 | not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b010 then { + if feature_flags_fn([|":SMAP"|]) == 0 | 0xf0 == prefixes[lck] | not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b011 then { + if feature_flags_fn([|":SMAP"|]) == 0 | 0xf0 == prefixes[lck] | not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b111 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_lgdt(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_lidt(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b000 then { + if feature_flags_fn([|":XSAVE"|]) == 0 | 0xf0 == prefixes[lck] | Mk_cr4bits(loghead(22, ctrs[4]))[osxsave] == 0b0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b001 then { + if feature_flags_fn([|":XSAVE"|]) == 0 | 0xf0 == prefixes[lck] | Mk_cr4bits(loghead(22, ctrs[4]))[osxsave] == 0b0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b100 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b101 then { + if feature_flags_fn([|":RTM"|]) == 0 | 0xf0 == prefixes[lck] | 0x66 == prefixes[opr] | 0xf3 == prefixes[rep] | 0xf2 == prefixes[rep] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b110 then { + if feature_flags_fn([|":HLE", ":RTM"|]) == 0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b111 then { + if Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 then { + x86_fault("(:NM)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & modr_m[mod] == 0b11 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[umip] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b111 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if in_64bit_mode(proc_mode) & modr_m[reg] == 0b111 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b000 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b111 & modr_m[mod] == 0b11 & modr_m[r_m] == 0b001 then { + if feature_flags_fn([|":RDTSCP"|]) == 0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 2 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 3 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 5 => { + if in_64bit_mode(proc_mode) then { + if 0xf0 == prefixes[lck] | Mk_ia32_eferbits(truncate(read_msr(0), 12))[sce] == 0b0 then { + x86_fault("(:UD)") + }; + x86_syscall_both_views(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 6 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 7 => { + if in_64bit_mode(proc_mode) then { + if 0xf0 == prefixes[lck] | Mk_ia32_eferbits(truncate(read_msr(0), 12))[sce] == 0b0 then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_sysret(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 8 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 9 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 11 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"UD2 encountered!\" START-RIP TEMP-RIP X86)"), + 13 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 16 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movss_movsd_op_en_rm(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movss_movsd_op_en_rm(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 17 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movss_movsd_op_en_mr(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movss_movsd_op_en_mr(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 18 => { + if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_movlps_movlpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movlps_movlpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movlps_movlpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movlps_movlpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 19 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movlps_movlpd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movlps_movlpd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 20 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_unpck?ps_op_en_rm(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_unpck?pd_op_en_rm(0, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 21 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_unpck?ps_op_en_rm(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_unpck?pd_op_en_rm(1, proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 22 => { + if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_movhps_movhpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movhps_movhpd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 23 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movhps_movhpd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_movhps_movhpd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 24 => { + if modr_m[reg] == 0b000 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b001 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & not_bool(modr_m[mod] == 0b11) then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b101 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b111 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 25 => x86_model_error("Opcode Unimplemented in x86isa!"), + 26 => { + if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x00 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | (if in_64bit_mode(proc_mode) then { + modr_m[mod] == 0b00 & (modr_m[r_m] == 0b101 | modr_m[r_m] == 0b100 & sib[base] == 0b101 & sib[index] == 0b100) + } else if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + if feature_flags_fn([|":MPX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x66 then { + if feature_flags_fn([|":MPX"|]) == 0 | modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[r_m], rex_byte, 0b00)) | (if not_bool(in_64bit_mode(proc_mode)) & prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + if feature_flags_fn([|":MPX"|]) == 0 | modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[r_m], rex_byte, 0b00)) | (if not_bool(in_64bit_mode(proc_mode)) & prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | not_bool(in_64bit_mode(proc_mode)) & (if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | not_bool(in_64bit_mode(proc_mode)) & (if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 27 => { + if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x00 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | (if in_64bit_mode(proc_mode) then { + modr_m[mod] == 0b00 & (modr_m[r_m] == 0b101 | modr_m[r_m] == 0b100 & sib[base] == 0b101 & sib[index] == 0b100) + } else if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + if feature_flags_fn([|":MPX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x66 then { + if feature_flags_fn([|":MPX"|]) == 0 | modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[r_m], rex_byte, 0b00)) | (if not_bool(in_64bit_mode(proc_mode)) & prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + if feature_flags_fn([|":MPX"|]) == 0 | modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[r_m], rex_byte, 0b00)) | (if not_bool(in_64bit_mode(proc_mode)) & prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0xf3 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | (if in_64bit_mode(proc_mode) then { + modr_m[mod] == 0b00 & (modr_m[r_m] == 0b101 | modr_m[r_m] == 0b100 & sib[base] == 0b101 & sib[index] == 0b100) + } else if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0xf3 then { + if feature_flags_fn([|":MPX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + if feature_flags_fn([|":MPX"|]) == 0 | 0xf0 == prefixes[lck] | 4 <= unsigned(reg_index(modr_m[reg], rex_byte, 0b10)) | not_bool(in_64bit_mode(proc_mode)) & (if prefixes[adr] == 0x67 then { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b1 + } else { + (let cs_attr : bits(16) = seg_hidden_attrs[1] in + Mk_code_segment_descriptor_attributesbits(cs_attr)[d]) == 0b0 + }) then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 28 => x86_model_error("Opcode Unimplemented in x86isa!"), + 29 => x86_model_error("Opcode Unimplemented in x86isa!"), + 30 => x86_model_error("Opcode Unimplemented in x86isa!"), + 31 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_nop(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 32 => { + if 0xf0 == prefixes[lck] | (let reg : bits(3) = modr_m[reg] in + if in_64bit_mode(proc_mode) & logbitp(2, rex_byte) then { + not_bool(reg == 0b000) + } else { + reg == 0b001 | reg == 0b101 | reg == 0b110 | reg == 0b111 + }) then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_mov_control_regs_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 33 => { + if 0xf0 == prefixes[lck] | Mk_cr4bits(loghead(22, ctrs[4]))[de] == 0b1 & (modr_m[reg] == 0b100 | modr_m[reg] == 0b101) then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 34 => { + if 0xf0 == prefixes[lck] | (let reg : bits(3) = modr_m[reg] in + if in_64bit_mode(proc_mode) & logbitp(2, rex_byte) then { + not_bool(reg == 0b000) + } else { + reg == 0b001 | reg == 0b101 | reg == 0b110 | reg == 0b111 + }) then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 35 => { + if 0xf0 == prefixes[lck] | Mk_cr4bits(loghead(22, ctrs[4]))[de] == 0b1 & (modr_m[reg] == 0b100 | modr_m[reg] == 0b101) then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 40 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movaps_movapd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movaps_movapd_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 41 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movaps_movapd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movaps_movapd_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 43 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 48 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 49 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 50 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 51 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) & Mk_cr4bits(loghead(22, ctrs[4]))[pce] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 52 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if Mk_cr0bits(loghead(32, ctrs[0]))[pe] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 53 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + } else { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) | Mk_cr0bits(loghead(32, ctrs[0]))[pe] == 0b0 then { + x86_fault("(:GP)") + }; + () + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 55 => x86_model_error("Opcode Unimplemented in x86isa!"), + 64 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 65 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 66 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 67 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 68 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 69 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 70 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 71 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 72 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 73 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 74 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 75 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 76 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 77 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 78 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 79 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmovcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 80 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 82 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 83 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 91 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 96 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 97 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 98 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 99 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 100 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 101 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 102 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 103 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 104 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 105 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 106 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 107 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 108 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 109 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 110 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movd_movq_to_xmm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 111 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 112 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 113 => { + if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 114 => { + if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b100 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 115 => { + if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b010 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b011 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b111 & modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-7", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 117 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 118 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 119 => { + if mandatory_prefix == 0x00 then { + if 0xf0 == prefixes[lck] | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 120 => { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 121 => { + if not_bool(unsigned(Mk_segment_selectorbits(seg_visibles[1])[rpl]) == 0) then { + x86_fault("(:GP)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 124 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 125 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 126 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movd_movq_from_xmm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 127 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_movups_movupd_movdqu_op_en_mr(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 128 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 129 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 130 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 131 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 132 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 133 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 134 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 135 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 136 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 137 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 138 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 139 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 140 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 141 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 142 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 143 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_two_byte_jcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 144 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 145 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 146 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 147 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 148 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 149 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 150 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 151 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 152 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 153 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 154 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 155 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 156 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 157 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 158 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 159 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_setcc(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 160 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 161 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 162 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 163 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_bt_0f_a3(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 164 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_shld_shrd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 165 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_shld_shrd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 168 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_push_segment_register(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 169 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 170 => x86_model_error("Opcode Unimplemented in x86isa!"), + 171 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 172 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_shld_shrd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 173 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_shld_shrd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 175 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_imul_op_en_rm(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 176 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmpxchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 177 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_cmpxchg(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 178 => { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 179 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 180 => { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 181 => { + if 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 182 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movzx(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 183 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movzx(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 184 => { + if mandatory_prefix == 0x00 then { + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + if feature_flags_fn([|":POPCNT"|]) == 0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 185 => x86_fault("(X86-ILLEGAL-INSTRUCTION \"UD1 encountered!\" START-RIP TEMP-RIP X86)"), + 186 => { + if modr_m[reg] == 0b100 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_bt_0f_ba(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if modr_m[reg] == 0b101 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b110 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[reg] == 0b111 then { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 187 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 189 => { + if mandatory_prefix == 0x00 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 190 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movsxd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 191 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_movsxd(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + }, + 192 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 193 => { + if modr_m[mod] == 0b11 & 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 195 => { + if feature_flags_fn([|":SSE2"|]) == 0 | 0xf0 == prefixes[lck] | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 196 => { + if modr_m[mod] == 0b11 & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if modr_m[mod] == 0b11 & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else if not_bool(modr_m[mod] == 0b11) & mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 197 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 198 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_shufps_op_en_rmi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_shufpd_op_en_rmi(proc_mode, start_rip, temp_rip, prefixes, rex_byte, opcode, modr_m, sib) + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 200 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 201 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 202 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 203 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 204 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 205 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 206 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 207 => { + if 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + }, + 208 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 209 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 210 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 211 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 212 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 213 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 214 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + if feature_flags_fn([|":SSE2"|]) == 0 | Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 | Mk_cr4bits(loghead(22, ctrs[4]))[osfxsr] == 0b0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + if feature_flags_fn([|":SSE2"|]) == 0 | Mk_cr0bits(loghead(32, ctrs[0]))[ts] == 0b1 | Mk_cr0bits(loghead(32, ctrs[0]))[em] == 0b1 | Mk_cr4bits(loghead(22, ctrs[4]))[osfxsr] == 0b0 | 0xf0 == prefixes[lck] then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 216 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 217 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 218 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 220 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 221 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 222 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 224 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 225 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 226 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 227 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 228 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 229 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 230 => { + if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf3 then { + chk_exc_fn(":LEGACY", ":TYPE-5", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-2", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 231 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-1", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 232 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 233 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 234 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 236 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 237 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 238 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 240 => { + if mandatory_prefix == 0xf2 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE3"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE3"|]) == 0 | modr_m[mod] == 0b11 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 241 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 242 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 243 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 244 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 245 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 246 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":SSE"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 247 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-8", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 248 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 249 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 250 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 251 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 252 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 253 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 254 => { + if mandatory_prefix == 0x00 then { + chk_exc_fn(":LEGACY", ":TYPE-22-7", [|":MMX"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":MMX"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else if mandatory_prefix == 0x66 then { + chk_exc_fn(":LEGACY", ":TYPE-4", [|":SSE2"|], proc_mode, prefixes, rex_byte, opcode, modr_m, sib); + if feature_flags_fn([|":SSE2"|]) == 0 then { + x86_fault("(:UD)") + }; + x86_model_error("Opcode Unimplemented in x86isa!") + } else { + x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + } + }, + 42 => throw(Emsg("Translation error: two-byte opcode 42 not translated")), + 44 => throw(Emsg("Translation error: two-byte opcode 44 not translated")), + 45 => throw(Emsg("Translation error: two-byte opcode 45 not translated")), + 46 => throw(Emsg("Translation error: two-byte opcode 46 not translated")), + 47 => throw(Emsg("Translation error: two-byte opcode 47 not translated")), + 56 => throw(Emsg("Translation error: two-byte opcode 56 not translated")), + 58 => throw(Emsg("Translation error: two-byte opcode 58 not translated")), + 81 => throw(Emsg("Translation error: two-byte opcode 81 not translated")), + 84 => throw(Emsg("Translation error: two-byte opcode 84 not translated")), + 85 => throw(Emsg("Translation error: two-byte opcode 85 not translated")), + 86 => throw(Emsg("Translation error: two-byte opcode 86 not translated")), + 87 => throw(Emsg("Translation error: two-byte opcode 87 not translated")), + 88 => throw(Emsg("Translation error: two-byte opcode 88 not translated")), + 89 => throw(Emsg("Translation error: two-byte opcode 89 not translated")), + 90 => throw(Emsg("Translation error: two-byte opcode 90 not translated")), + 92 => throw(Emsg("Translation error: two-byte opcode 92 not translated")), + 93 => throw(Emsg("Translation error: two-byte opcode 93 not translated")), + 94 => throw(Emsg("Translation error: two-byte opcode 94 not translated")), + 95 => throw(Emsg("Translation error: two-byte opcode 95 not translated")), + 116 => throw(Emsg("Translation error: two-byte opcode 116 not translated")), + 174 => throw(Emsg("Translation error: two-byte opcode 174 not translated")), + 188 => throw(Emsg("Translation error: two-byte opcode 188 not translated")), + 194 => throw(Emsg("Translation error: two-byte opcode 194 not translated")), + 199 => throw(Emsg("Translation error: two-byte opcode 199 not translated")), + 215 => throw(Emsg("Translation error: two-byte opcode 215 not translated")), + 219 => throw(Emsg("Translation error: two-byte opcode 219 not translated")), + 223 => throw(Emsg("Translation error: two-byte opcode 223 not translated")), + 235 => throw(Emsg("Translation error: two-byte opcode 235 not translated")), + 239 => throw(Emsg("Translation error: two-byte opcode 239 not translated")), + _ => x86_fault("(X86-ILLEGAL-INSTRUCTION \"#UD Encountered!\" START-RIP TEMP-RIP X86)") + }) : unit + } +} + +/*! Decoder and dispatch function for two-byte opcodes +Source: Intel Manual, Volume 2, Appendix A-2
*/ +val two_byte_opcode_decode_and_execute : (proc_mode, sbits(48), sbits(48), prefixes, bits(8), bits(8)) -> unit + +function two_byte_opcode_decode_and_execute (proc_mode, start_rip, temp_rip, prefixes, rex_byte, escape_byte) = { + let ctx : string = "two-byte-opcode-decode-and-execute"; + let opcode : bits(8) = rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X"); + let temp_rip : sbits(48) = add_to_iptr(proc_mode, temp_rip, 0x000000000001); + let mandatory_prefix : bits(8) = compute_mandatory_prefix_for_two_byte_opcode(proc_mode, opcode, prefixes); + let modr_m? : bool = two_byte_opcode_modr_m_p(proc_mode, mandatory_prefix, opcode); + let modr_m : modr_m = { + if modr_m? then { + Mk_modr_m(rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X")) + } else { + Mk_modr_m(0x00) + } + }; + let temp_rip : sbits(48) = { + if modr_m? then { + add_to_iptr(proc_mode, temp_rip, 0x000000000001) + } else { + temp_rip + } + }; + let sib? : bool = modr_m? & (let p4? : bool = 0x67 == prefixes[adr] in + let n16_bit_addressp : bool = 2 == select_address_size(proc_mode, Some(prefixes)) in + x86_decode_sib_p(modr_m, n16_bit_addressp)); + let sib : sib = { + if sib? then { + Mk_sib(rme08(proc_mode, select_address_size(proc_mode, Some(prefixes)), sail_sign_extend(temp_rip, 64), None(), 1, ":X")) + } else { + Mk_sib(0x00) + } + }; + let temp_rip : sbits(48) = { + if sib? then { + add_to_iptr(proc_mode, temp_rip, 0x000000000001) + } else { + temp_rip + } + }; + two_byte_opcode_execute(proc_mode, start_rip, temp_rip, prefixes, mandatory_prefix, rex_byte, opcode, modr_m, sib) +} diff --git a/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/xor_spec.sail b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/xor_spec.sail new file mode 100644 index 000000000..a7001797a --- /dev/null +++ b/crates/clazel-x64/tests/data/sail-x86-from-acl2/model/xor_spec.sail @@ -0,0 +1,56 @@ + +val gpr_xor_spec_1 : (bits(8), bits(8), rflagsbits) -> (bits(8), rflagsbits, rflagsbits) + +function gpr_xor_spec_1 (dst, src, input_rflags) = { + let result : bits(8) = logxor(dst, src)[7 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec8(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec8(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_xor_spec_2 : (bits(16), bits(16), rflagsbits) -> (bits(16), rflagsbits, rflagsbits) + +function gpr_xor_spec_2 (dst, src, input_rflags) = { + let result : bits(16) = logxor(dst, src)[15 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec16(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec16(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_xor_spec_4 : (bits(32), bits(32), rflagsbits) -> (bits(32), rflagsbits, rflagsbits) + +function gpr_xor_spec_4 (dst, src, input_rflags) = { + let result : bits(32) = logxor(dst, src)[31 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec32(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec32(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} + +val gpr_xor_spec_8 : (bits(64), bits(64), rflagsbits) -> (bits(64), rflagsbits, rflagsbits) + +function gpr_xor_spec_8 (dst, src, input_rflags) = { + let result : bits(64) = logxor(dst, src)[63 .. 0]; + let cf : {|0|} = 0; + let pf : bits(1) = pf_spec64(result); + let zf : bits(1) = zf_spec(unsigned(result)); + let sf : bits(1) = sf_spec64(result); + let of : {|0|} = 0; + let output_rflags : rflagsbits = [input_rflags with cf = bits_of_int(cf, 1), pf, zf, sf, of = bits_of_int(of, 1)]; + let undefined_flags : rflagsbits = [Mk_rflagsbits(0x00000000) with af = 0b1]; + (result, output_rflags, undefined_flags) +} diff --git a/crates/clazel-x64/tests/generated/acl2_rtl.rs b/crates/clazel-x64/tests/generated/acl2_rtl.rs new file mode 100644 index 000000000..32e4c3f7a --- /dev/null +++ b/crates/clazel-x64/tests/generated/acl2_rtl.rs @@ -0,0 +1,47 @@ +// @generated by `cargo xtask testdata` from tests/data/acl2-rtl/. +// Do not edit: `cargo xtask verify` fails if this differs from what the +// converter (xtask/src/testdata/isa.rs) produces. +// Source, commit and license: tests/data/acl2-rtl/SOURCE.md. +// +// Russinoff's RTL library, the floating-point definitions acl2-x86isa's SSE books call. +// 24 files, 301839 bytes. Nothing here is parsed yet; see the converter's +// header for why. Paths are relative to tests/data/acl2-rtl/. + +/// One vendored file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct File { + /// Its path under the suite directory, with `/` separators. + pub path: &'static str, + /// Its size in bytes. + pub size: u64, + /// Its SHA-256, in lower-case hex. + pub sha256: &'static str, +} + +/// Every vendored file, sorted by path. +pub const FILES: &[File] = &[ + File { path: "LICENSE.md", size: 1920, sha256: "8ea177065a9bb96c5d0d566df2df39c42735787f1b4e9d378f98390d38e85c62" }, + File { path: "lib/add.lisp", size: 16271, sha256: "608a232c3cb4a122f4a3bd7d9887e357416f51a539c3ca2266eb68c9ad3f6498" }, + File { path: "lib/basic.lisp", size: 15187, sha256: "e89679fa551119c1f1fc3c8af41f8c64bd22979c9f2ea7a2d0753a4a0d0b492e" }, + File { path: "lib/bits.lisp", size: 28564, sha256: "e39da3728e6e2190815366e09f6bd49c33fb0818ebc86c01d94b0358aa3f216e" }, + File { path: "lib/defs.lisp", size: 14485, sha256: "fed508ebeb9fe6f89c368d49eb217fb0decb29de7ebc8f403e4c7c18d5600c95" }, + File { path: "lib/div.lisp", size: 12028, sha256: "9a5c01091d87ab874ad31aa1cbcb97a7a6ab23c481d221aea75fb41d2fab92c1" }, + File { path: "lib/doc.lisp", size: 8622, sha256: "0b0fb8891b6fc4e1a51c6ae758c0e5e9672fd0dbb8d2674359574ddd3e340dee" }, + File { path: "lib/excps.lisp", size: 40527, sha256: "f056ae5f00365b8174893fdcb7f16ab92e990138c5466998dfbbf4fa98134089" }, + File { path: "lib/float.lisp", size: 12252, sha256: "cb08592bc754f16aef537440c6a048a00713d36ca5f9522d21d653cadc47ef3f" }, + File { path: "lib/gl.lisp", size: 2605, sha256: "bd53925cffdd34fbccbdba3cfd2e388bfcf755dfa17b93b054e162594ba879c5" }, + File { path: "lib/log.lisp", size: 14485, sha256: "6446ff07ed2992300f2b9d6df55d734fdc213fbc0095c46fbc12d29efd826533" }, + File { path: "lib/mult.lisp", size: 15348, sha256: "21ab2f30f8e96fd8df8ba5056b1b76559955d142666728ccee493401dc136715" }, + File { path: "lib/rac.lisp", size: 8124, sha256: "d6acfc66d5a8d1f4127948fe44346d7c0b3f825c844f587f19b3902cebb11630" }, + File { path: "lib/rcp.lisp", size: 3042, sha256: "3b68bd732bbca50e8ea1c305f9f58c59a97528e14e6b3a2ed92d406b86e25cb3" }, + File { path: "lib/reps.lisp", size: 12024, sha256: "69f4cf258f089ffb3d16bcebe7f46cfd076efae66c51913dd516ae5d01a2c69a" }, + File { path: "lib/round.lisp", size: 52711, sha256: "1e60cbc7104ae3c34dec23ef39302a4a2193afc5b77c39413398b9357e06c0cf" }, + File { path: "lib/sqrt.lisp", size: 9040, sha256: "cd3d7b6d15a5cf5334959e5983c259c3a014fa7d306bca56f6785028b0f7205b" }, + File { path: "lib/srt.lisp", size: 20694, sha256: "e68edc24714e83b982fe3c58b29c730603329972261414089c8e67c2f3d9bdd0" }, + File { path: "lib/top-alt.lisp", size: 2248, sha256: "1431955c1787a3f9a36d7fd072920063f13e4d22ad2436690091f97d67501749" }, + File { path: "lib/top-doc.lisp", size: 1924, sha256: "bd6d660a676538e74f5411d52364b95bb714229a655525cba886033591bee916" }, + File { path: "lib/top.lisp", size: 2230, sha256: "f0c380dc4dccb0dc894c39c66aeb936023089430371b545faf180d7308e0792f" }, + File { path: "lib/util.lisp", size: 6058, sha256: "1d5a6fbe2c31f1de776cfb24c8de904ff3430483898a8c9cd5aebf85026350a1" }, + File { path: "package.lsp", size: 1245, sha256: "314a89c56a1f8339447bde0bb0693a008a88308f75978d15056e8f556368afcb" }, + File { path: "portcullis.lisp", size: 205, sha256: "4b366f262eb4d507d26b0a05d27506ec7a9922ff0b82a8e778e702aa99849188" }, +]; diff --git a/crates/clazel-x64/tests/generated/acl2_x86isa.rs b/crates/clazel-x64/tests/generated/acl2_x86isa.rs new file mode 100644 index 000000000..0a2e55f88 --- /dev/null +++ b/crates/clazel-x64/tests/generated/acl2_x86isa.rs @@ -0,0 +1,87 @@ +// @generated by `cargo xtask testdata` from tests/data/acl2-x86isa/. +// Do not edit: `cargo xtask verify` fails if this differs from what the +// converter (xtask/src/testdata/isa.rs) produces. +// Source, commit and license: tests/data/acl2-x86isa/SOURCE.md. +// +// ACL2 x86isa model files for the subset of jit.md item J.3. +// 64 files, 2518719 bytes. Nothing here is parsed yet; see the converter's +// header for why. Paths are relative to tests/data/acl2-x86isa/. + +/// One vendored file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct File { + /// Its path under the suite directory, with `/` separators. + pub path: &'static str, + /// Its size in bytes. + pub size: u64, + /// Its SHA-256, in lower-case hex. + pub sha256: &'static str, +} + +/// Every vendored file, sorted by path. +pub const FILES: &[File] = &[ + File { path: "LICENSE.md", size: 2084, sha256: "358ca43cd493116dda153fe67419f197fd0e99ece9345f8fdff60d8584849a9d" }, + File { path: "README", size: 885, sha256: "aa46c3ff7f21b161d694157c06a03e2784f0c3389485b5d7f322eb8b04fbdc08" }, + File { path: "machine/application-level-memory.lisp", size: 51838, sha256: "51bbf413b44a70942ccec94f9e1d247bb23503b81c18cb939275e6374a88f2b0" }, + File { path: "machine/decoding-and-spec-utils.lisp", size: 106265, sha256: "00f4a139072bd13de4e00d827bb225830108d838d8f196dc2a499159fb1fa383" }, + File { path: "machine/dispatch-creator.lisp", size: 21526, sha256: "17f3a704ba6041ebab9b1e1d4254c68ef76635dc116851a4e98a00c49db6d37e" }, + File { path: "machine/dispatch-macros.lisp", size: 13155, sha256: "027979da1fd7d9c26c68cc54e7cb358237a3f0bb04e3a8b5b0fbed7973b6e81d" }, + File { path: "machine/get-prefixes.lisp", size: 38098, sha256: "17617d8444efef6e37b292553518a3adadc009cb486d869fef9814a2445d6877" }, + File { path: "machine/guard-helpers.lisp", size: 19563, sha256: "3095906b0862fa40e749a84c64b6e1f1c57198c0d2afed165afda442c1b6d5ea" }, + File { path: "machine/inst-listing.lisp", size: 729751, sha256: "ccd90ab0a20598ac2580e4cf03f207e6782a43539ae317b0d139018978d69489" }, + File { path: "machine/inst-structs.lisp", size: 38432, sha256: "ca4bc17048ed0f93341aa667f9b984a68cde0b531eeee2a731564e6b34335aac" }, + File { path: "machine/instructions/add-spec.lisp", size: 20163, sha256: "4f639d319104d719445f4b63fb8349049b03a6c22527b0375db4367017e490a5" }, + File { path: "machine/instructions/and-spec.lisp", size: 5947, sha256: "fcb575d589e15efc7502f4ba2ae5e23acf63549ad831ffe7ad519f98703bd465" }, + File { path: "machine/instructions/arith-and-logic-spec.lisp", size: 5516, sha256: "a071236e1611f9ebf664a30b566a18635bd6cfc1e433a6e1600977cad64fde64" }, + File { path: "machine/instructions/arith-and-logic.lisp", size: 38875, sha256: "809dead722587e3360243a70d16c2bdddd8f04691da64bc0b7031d6037d6de64" }, + File { path: "machine/instructions/conditional.lisp", size: 25168, sha256: "f0fcc6408d94369d234ad0eecd1af05703975497c11304f9cad9a6d484a81c15" }, + File { path: "machine/instructions/flags.lisp", size: 6224, sha256: "ef3954e8e310434161956bb50f11c761ef9cc77d0d9b559920977b8301840048" }, + File { path: "machine/instructions/fp/add-mul-spec.lisp", size: 7750, sha256: "83322d45ee3b53caa6ca87d1385b73a6b844ea3d839430a322fb25017438b24a" }, + File { path: "machine/instructions/fp/arith-spec.lisp", size: 12549, sha256: "a6a39f4fc322900e31fc871aa818a14f6ea9e07a12e08b9d910d3603ca37373d" }, + File { path: "machine/instructions/fp/arithmetic.lisp", size: 25835, sha256: "99daa7e127b5c5e4cb438dac7f33d122c3a826d082811ddb7bafee29d6b3955f" }, + File { path: "machine/instructions/fp/base.lisp", size: 14803, sha256: "d8c9a763f6ddbdf0f4f74334ccbecb61b0a4425b78958fc44eaef55d51141e05" }, + File { path: "machine/instructions/fp/cmp-spec.lisp", size: 12052, sha256: "0d54f6ec2fec0c03c9571f53cedc62b93926e461193a7f44b18b2e62a31893da" }, + File { path: "machine/instructions/fp/convert.lisp", size: 18745, sha256: "869f6ed9a170bd0b4690dcc2766888aabd18e1908901fd03b2a00a637e714cef" }, + File { path: "machine/instructions/fp/cvt-spec.lisp", size: 22086, sha256: "64e02b07965a80fcd786df9a95087fba93c4d7e617755f2e0eb263d067f720bb" }, + File { path: "machine/instructions/fp/logical.lisp", size: 20410, sha256: "01ff6c7a41457cf9676da0969667c28349f30ab5ae224e22ef99aaef2c20dd47" }, + File { path: "machine/instructions/fp/mxcsr.lisp", size: 5562, sha256: "7aee2ad7b72525572bfcebd3ec892af0650a312b784c490ad394a4e2e6b26597" }, + File { path: "machine/instructions/fp/sqrt-spec.lisp", size: 5998, sha256: "3c82aa74064b48cc4d235e11fbaa3b6a391cf4491041f0ff8f93be8383db5282" }, + File { path: "machine/instructions/fp/top.lisp", size: 3663, sha256: "1d35633b80b9d1bdb88bac6f747c388dfab0ed1c5adc5e307a2f965b209734d6" }, + File { path: "machine/instructions/jump-and-loop.lisp", size: 43822, sha256: "3f1bcff3bc0f26dd0af399397753c1b574ffdd50ef037fe8656f8aa013a6b7c0" }, + File { path: "machine/instructions/logical.lisp", size: 14329, sha256: "2416720a249e52a0a24950b0c7d8e96efeca577a1a58bf3d14fb1d48d346c42d" }, + File { path: "machine/instructions/move-sse.lisp", size: 49001, sha256: "40c080806fc345cd37d5df6b19edc3ab48ba55f825aabc08c083e6cb7fbaf885" }, + File { path: "machine/instructions/move.lisp", size: 43098, sha256: "74fede806e1fac56165482ccf9cbd31c791ee542e9a6ec0809285f1c092d4a76" }, + File { path: "machine/instructions/multiply-spec.lisp", size: 10396, sha256: "4c0640df3736ffd0f07d76d097b0cc61ee3dc127bab200d301eefd80a11b5b52" }, + File { path: "machine/instructions/multiply.lisp", size: 19277, sha256: "50fcc0670802417562162036ec217e4229e68860f2d34143fe3da29c34c4526a" }, + File { path: "machine/instructions/or-spec.lisp", size: 5746, sha256: "65acf2678d207c9b81e43c6016bdf7f439593814a55b92d649fcc8676d38233c" }, + File { path: "machine/instructions/rotate-and-shift.lisp", size: 23051, sha256: "514ff2887fce7aedf9a143819f8919d58bc61a72e77dfe427555bf9c3bb61545" }, + File { path: "machine/instructions/shifts-spec.lisp", size: 70054, sha256: "4eb4413a621e61c560b6ee3a5a7be414cac2dbaf7938172ccee5a9bed4c587db" }, + File { path: "machine/instructions/signextend.lisp", size: 4783, sha256: "49be47d36fc26ad9625ec082c72d5b43286ad3b4edf7f7429c043a6372944714" }, + File { path: "machine/instructions/sub-spec.lisp", size: 11567, sha256: "a145446a3e2f82fdc295f6e247fd318d98d6d1d965f75e5b7a20d53191fa4b1d" }, + File { path: "machine/instructions/subroutine.lisp", size: 42098, sha256: "875e470add5d25d87193f25f6a724efb38cac4950134f14c337a7b54cb20502f" }, + File { path: "machine/instructions/top.lisp", size: 5993, sha256: "44d6ea575aed8a0749213656ae37ff7fb52b246e52fa3b497451e7e2294adfb5" }, + File { path: "machine/instructions/xor-spec.lisp", size: 5776, sha256: "d8a90003f777a8bb971972a490eca625ba6121c7565b032f7c72cbf34f6aee73" }, + File { path: "machine/linear-memory.lisp", size: 324243, sha256: "0d45ab4110f9b55edd31f66e885f9a8c6c751336bc2771ade080ee32ed0d1e07" }, + File { path: "machine/modes.lisp", size: 7652, sha256: "f105ffdea6e18e37d99acfc9b4b4e5aaf3fb460dc4d2905890297078d7bbdd30" }, + File { path: "machine/physical-memory.lisp", size: 25360, sha256: "ce94b0ebffc17b73cc34598138e8e8959d73b1a2745b2c8585dd401de75da60c" }, + File { path: "machine/prefix-modrm-sib-decoding.lisp", size: 109322, sha256: "a063b7d2d5fe20df7b0419f066fb2d26f75cef9851dc23fcafea926393129adb" }, + File { path: "machine/register-readers-and-writers.lisp", size: 70591, sha256: "af682c8d8bd947b47d67730cc906c36e68e8484f7080f91ae2c25fc8e864dd38" }, + File { path: "machine/rflags-spec.lisp", size: 27997, sha256: "7b46bfb8b3eeb30b28938323359e7c456e7e2d1a672405458431e41afc50b507" }, + File { path: "machine/state.lisp", size: 38582, sha256: "fedde92511c2f38aaabcd95c12b671c88301e8776413f15f44aa76599bad29fa" }, + File { path: "machine/three-byte-opcodes-dispatch.lisp", size: 11337, sha256: "f72a45bd5b12437f5d8116de93ca87978150e736359e893a4cc76f94bf9611e7" }, + File { path: "machine/top-level-memory.lisp", size: 95243, sha256: "6f74e55e7cc6833fd34dad4b52e3e5f444f9067d205ddef52a9307e209d55e59" }, + File { path: "machine/two-byte-opcodes-dispatch.lisp", size: 10096, sha256: "c0f6f332823392a4e03e95e74482e9cead3bc6b2a73acb7278261f0eb9eb4822" }, + File { path: "machine/x86.lisp", size: 37437, sha256: "e73251b5ea07fdb524d3530006ed268be9ebe1756d9e85abb29d34d08c006f85" }, + File { path: "portcullis/package.lsp", size: 3482, sha256: "274d6fe165dc33e6d59d4cd53aec1751497e7de2eabd048ce8d0702c7bb4a5b9" }, + File { path: "portcullis/portcullis.lisp", size: 1880, sha256: "4eec69e015ec858fce1f611349eda9fd84eb25d55b83fe325776122abc60ee3c" }, + File { path: "portcullis/sharp-dot-constants.lisp", size: 25781, sha256: "276a19e1464e6fe21a3c8844bf63be80445de2b35ff2411809f07208d39d16db" }, + File { path: "portcullis/sharp-dot-defuns.lisp", size: 12566, sha256: "a37473f1b09ce248a1f69457daaee98697f8daee4cb186d295cc9dfe361b791e" }, + File { path: "portcullis/utils.lisp", size: 2887, sha256: "ebeebdc4aba0b47501c7e23ca54998bad142fb644961c26ae0c9ff74bfeae9a5" }, + File { path: "utils/basic-structs.lisp", size: 2732, sha256: "e62e2b1b3a3a524a1feadc81df723d0c302aa8b1a416c35b172a38595722a265" }, + File { path: "utils/fp-structures.lisp", size: 4686, sha256: "8eace3885f9ffcb01a825d9731eb848c5adb257a1fdf4609d9e5e01062616f07" }, + File { path: "utils/paging-structures.lisp", size: 11121, sha256: "b179a9bac6bdd9c485d01cda8cc38ea9eb91ae33fd0dce7132e1e98c57f47260" }, + File { path: "utils/records-0.lisp", size: 12865, sha256: "538f27ac8463b3058dcc7f0b3257c05384a5166f82d481be47910335df2e93fd" }, + File { path: "utils/segmentation-structures.lisp", size: 12902, sha256: "811a8ae74230fbeb5ec59b48246ebfe5d5cfcbf87463c3eb5b958cb43ef44862" }, + File { path: "utils/structures.lisp", size: 31620, sha256: "74fa6a8766f1f3a0fe65a91b67598854fa5dc04cb26f7862273a58a765960044" }, + File { path: "utils/utilities.lisp", size: 16403, sha256: "e1d5412ed5467d2b9cb2b3d77429f97bcb3fc6e01df1c6b89e9a41775d392b9b" }, +]; diff --git a/crates/clazel-x64/tests/generated/intel_sdm.rs b/crates/clazel-x64/tests/generated/intel_sdm.rs new file mode 100644 index 000000000..4dbb16fda --- /dev/null +++ b/crates/clazel-x64/tests/generated/intel_sdm.rs @@ -0,0 +1,22 @@ +// @generated by `cargo xtask testdata` from tests/data/intel-sdm/. +// Do not edit: `cargo xtask verify` fails if this differs from what the +// converter (xtask/src/testdata/isa.rs) produces. +// Source, pins and license: tests/data/intel-sdm/SOURCE.md. +// +// Intel SDM volume 2, pinned by URL and SHA-256 (Intel's terms do not allow redistribution). + +/// A file the suite pins but does not vendor. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Pin { + /// The file's name in `SOURCE.md`. + pub file: &'static str, + /// Its size in bytes. + pub size: u64, + /// Its SHA-256, in lower-case hex. + pub sha256: &'static str, +} + +/// Every pinned file. +pub const PINS: &[Pin] = &[ + Pin { file: "325383-093-sdm-vol-2abcd.pdf", size: 11294447, sha256: "d137a7887787bbb06c1a8b8bafd51958d8d2e16b4dff987a03226c845cca1516" }, +]; diff --git a/crates/clazel-x64/tests/generated/sail_x86_from_acl2.rs b/crates/clazel-x64/tests/generated/sail_x86_from_acl2.rs new file mode 100644 index 000000000..c141d5994 --- /dev/null +++ b/crates/clazel-x64/tests/generated/sail_x86_from_acl2.rs @@ -0,0 +1,73 @@ +// @generated by `cargo xtask testdata` from tests/data/sail-x86-from-acl2/. +// Do not edit: `cargo xtask verify` fails if this differs from what the +// converter (xtask/src/testdata/isa.rs) produces. +// Source, commit and license: tests/data/sail-x86-from-acl2/SOURCE.md. +// +// Sail x86 model translated from the ACL2 one (jit.md item J.2); not independent of it. +// 50 files, 709571 bytes. Nothing here is parsed yet; see the converter's +// header for why. Paths are relative to tests/data/sail-x86-from-acl2/. + +/// One vendored file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct File { + /// Its path under the suite directory, with `/` separators. + pub path: &'static str, + /// Its size in bytes. + pub size: u64, + /// Its SHA-256, in lower-case hex. + pub sha256: &'static str, +} + +/// Every vendored file, sorted by path. +pub const FILES: &[File] = &[ + File { path: "LICENCE", size: 2011, sha256: "0d3b38d888099279e9f15482122d37ca994ec368837ad276028bf8a122d09b38" }, + File { path: "LICENSE.md", size: 4273, sha256: "b683c3f63ee3cb0740a59bd111611f34eb32b8629f4cefbcdeafa31639c43d00" }, + File { path: "README.md", size: 6249, sha256: "5e44846278a06d9ace245fbefd232ee6c1d499ababaabd5769eff9ae8b77d03d" }, + File { path: "model/LICENSE", size: 4273, sha256: "b683c3f63ee3cb0740a59bd111611f34eb32b8629f4cefbcdeafa31639c43d00" }, + File { path: "model/Makefile", size: 2025, sha256: "c237d56257789eb3d6818bc1196c73bb6c67d0c6b59711eb4eddbf91187d3d32" }, + File { path: "model/add_spec.sail", size: 7134, sha256: "906c53af7ddf16b8e18f83fde654d3e074e38f1022aa2f229c2e927b18fddf87" }, + File { path: "model/and_spec.sail", size: 2521, sha256: "f9a3c0f0c688be7851f7752ba7676ba5fbc9e7f1f64fdd4913af463f09b5ba6a" }, + File { path: "model/application_level_memory.sail", size: 864, sha256: "505188dad643f3b52474f4baf14be1bd894143fcc3c39005f3de5989f5a8f89e" }, + File { path: "model/arith_and_logic.sail", size: 18140, sha256: "0f710c88a2e3127a115ca1099a1c083975c05b4415ad899a196d9e2184ac7998" }, + File { path: "model/arith_and_logic_spec.sail", size: 4838, sha256: "b8faad856a8156119e46c3680d522a5de81cdd7d8a83af7de593dd66e80e8d8b" }, + File { path: "model/auxiliary.sail", size: 11801, sha256: "787712ec5130d758cb854c6749fde6414473121130b0f8324c9b73be97f67d2b" }, + File { path: "model/conditional.sail", size: 8581, sha256: "26ecaa0eea7a248d9d1c17210c07458a8709f0bb2f81ac0c4ed354b1eae5c4cd" }, + File { path: "model/config.sail", size: 2082, sha256: "bba58e1d5af2cc9401f93e80d4f4fee9602cdb191eb5196b2964c7068ff579f0" }, + File { path: "model/decoding_and_spec_utils.sail", size: 43068, sha256: "f4e70be76f7bb309554722f07bbe71157c08ecef1f37eb0e5cc3993e72296ea7" }, + File { path: "model/dispatch_macros.sail", size: 1067, sha256: "541fcf3e5e3df5dd9c83352d646815f03759369ba0500057ffb50e914506ff4d" }, + File { path: "model/environment.sail", size: 35, sha256: "33b613a3059d023a3b543b7a9ed5f28c2f80b6fefb2710844b9bdf438b5c7f25" }, + File { path: "model/init.sail", size: 180, sha256: "e330b0a9b0099ba699996e3a596890df0fd9caee61e2b814569aaf1388b04b4c" }, + File { path: "model/jump_and_loop.sail", size: 18720, sha256: "06c3c76defaf10697bf345d7a7bbb584d3100f53014b606c2e4694768c9a2ce4" }, + File { path: "model/linear_memory.sail", size: 18531, sha256: "de0f6a226a8d9619359334f3a063071b9baf763f402a82105aed12b1145b4c84" }, + File { path: "model/logging.sail", size: 1938, sha256: "de785bad9420d481654e5cd773892bc02c526b197ceec0747a759de39310b4ea" }, + File { path: "model/main.sail", size: 1795, sha256: "0e125c0e38c51eb076f4d482dc81384121af464972c3b3c7c6c148780571a6e0" }, + File { path: "model/memory_accessors.sail", size: 4529, sha256: "b1373bc368724fb175ba2fb2482c01a5d4ce668e84eae6eed8114002d1d42fb9" }, + File { path: "model/modes.sail", size: 1546, sha256: "0f791685159e7ba823e008f0d557fccc7dcfa5f316dde564fb2617c04a694e06" }, + File { path: "model/mov.sail", size: 16615, sha256: "3ef6e82be78fcb79de1d00df72ff0c2132a4a393ca724b448be49458ae83818d" }, + File { path: "model/move.sail", size: 21127, sha256: "08d9815bf2c7ce204ee4e9f2ac208cae2477048b122cb29780440d44ac24af68" }, + File { path: "model/multiply.sail", size: 13789, sha256: "90516031b4582c583f6036fce09115fb9854e4289f7f32a1965e556e1391feb5" }, + File { path: "model/multiply_spec.sail", size: 6199, sha256: "47211e118e578dc123e7bfac547e9be5fbb41ccf469d314874b89537877391ba" }, + File { path: "model/opcode_ext.sail", size: 3856, sha256: "d33e16eb589ff6e521f9d1a2f12edc5492133cfa79c2dbd7464499420a67ff5c" }, + File { path: "model/or_spec.sail", size: 2513, sha256: "33510144b55a95d75ce981bd90a4dab446787ce014f20ae01b1234a972157294" }, + File { path: "model/physical_memory.sail", size: 2024, sha256: "a2ee81b2527358d2d2bb7dfe1e4b82e639c1c5d80f1a8a2dbc920a879564872e" }, + File { path: "model/prefix_modrm_sib_decoding.sail", size: 25071, sha256: "b6a5fd1c2abd8c1c398809777467d29fa73941ddd8f4154e0769d21793f449a4" }, + File { path: "model/prelude.sail", size: 22998, sha256: "c8459e03dac0bd80620bfd8b91498b153e95cbb2924113f8617a3c3fcbf90bfd" }, + File { path: "model/register_accessors.sail", size: 872, sha256: "9a8d33be19bcd2572929685bd2861c03055914cce148661551fb5e425d39b304" }, + File { path: "model/register_readers_and_writers.sail", size: 24980, sha256: "8de4459b533c20147f2953f2f7a90a3fabcc9debb5f494940f3f573030b0ee5a" }, + File { path: "model/register_types.sail", size: 244, sha256: "530206628dea46dbb35e559008f88ce9946ae4f97da0152154480d50ed6ea9d8" }, + File { path: "model/registers.sail", size: 4160, sha256: "789f81d776682f76b3b70e1a179537713c95327edef9f7eefca58f4a36992902" }, + File { path: "model/rflags_spec.sail", size: 10005, sha256: "2195224ef798d10d0ff7ba6a1ab56c448cbceaa8a503bce570cf5da06b9db7c8" }, + File { path: "model/rotate_and_shift.sail", size: 9525, sha256: "2fba5ec9ce5e519c0cea7d2b7373fe20e45846c4f141eb4d005d309ef26ca4f1" }, + File { path: "model/shifts_spec.sail", size: 46510, sha256: "b9aef38181aadfb1481bef64ac1c6869e43cc107a1b372757cb33ae2c7523705" }, + File { path: "model/shuffle_and_unpack.sail", size: 8699, sha256: "cd48bd0a6e4ca4fe11fcf0334fbba373933dbe1c24ddc46963e87e2b699a5828" }, + File { path: "model/signextend.sail", size: 1844, sha256: "7a988571a3fff6133c3757d997865754d5c46312cda023c42b2e1b720668f696" }, + File { path: "model/step.sail", size: 118010, sha256: "4f3d45bd77edb94b3ec307aa077383d2e4337185d396bda4a894823dc2c86462" }, + File { path: "model/structures.sail", size: 7237, sha256: "fced32f0ec990acaf36e1e48190b94fe480a34db1c980c8744fe1fe114a1eb8f" }, + File { path: "model/sub_spec.sail", size: 6949, sha256: "ad76d5f6b08a99c648bc824dad5da86daefa3a1585be6209bf15d19b98980444" }, + File { path: "model/subroutine.sail", size: 7793, sha256: "0f415d06d8a09dba50dab30dba68d7b2eb6ecd44f8f5022fb90cc8ffd9619b2b" }, + File { path: "model/three_byte_opcodes_dispatch.sail", size: 44, sha256: "1f7a32638be7197875d0828b94460314434cac53b931649c0716594c817222aa" }, + File { path: "model/top.sail", size: 3828, sha256: "920e6a5ac62a52c91f1632319e1b2acf434ea42d666a9c4d4731396d6fdc1a91" }, + File { path: "model/top_level_memory.sail", size: 28897, sha256: "05e0b4b971ccbbce073d0cb28fe05d2fbdf98b4b594158f49aecedfdb011f243" }, + File { path: "model/two_byte_opcodes_dispatch.sail", size: 147060, sha256: "612a6e99b0be5f734c6393a7566abd4497ad3f4bea570cc9ed5ac4f1550f4171" }, + File { path: "model/xor_spec.sail", size: 2521, sha256: "abaf22867709d9f828c9fa1b348316dad793a821cb29695a5d36e2b857f1007f" }, +]; diff --git a/crates/clazel-x64/tests/reference_disassembler.rs b/crates/clazel-x64/tests/reference_disassembler.rs new file mode 100644 index 000000000..058db5314 --- /dev/null +++ b/crates/clazel-x64/tests/reference_disassembler.rs @@ -0,0 +1,45 @@ +//! The reference disassembler builds and decodes (docs/phases/jit.md item J.2). +//! +//! `yaxpeax-x86` is a test dependency only (class `test` in +//! docs/DEPENDENCIES.md). Item J.3's decoder differential will compare it with +//! `clazel-x64` on every encoding of the subset; this test only shows that it +//! is wired in and answers for one instruction of each kind the subset has. +//! It replaced `iced-x86`, whose decoder Miri reports as undefined behaviour +//! under Stacked Borrows (iced-x86 1.22.0, `decoder/handlers/legacy.rs:2006`). + +use yaxpeax_arch::{Decoder, LengthedInstruction, U8Reader}; +use yaxpeax_x86::long_mode::{InstDecoder, Opcode}; + +fn decode(bytes: &[u8]) -> Option<(Opcode, u64)> { + let mut reader = U8Reader::new(bytes); + InstDecoder::default() + .decode(&mut reader) + .ok() + .map(|inst| (inst.opcode(), inst.len().to_const())) +} + +#[test] +fn add_rax_rbx() { + // REX.W 01 /r: add r/m64, r64. + assert_eq!(decode(&[0x48, 0x01, 0xd8]), Some((Opcode::ADD, 3))); +} + +#[test] +fn jmp_rel32_and_ret() { + assert_eq!( + decode(&[0xe9, 0x10, 0x00, 0x00, 0x00]), + Some((Opcode::JMP, 5)) + ); + assert_eq!(decode(&[0xc3]), Some((Opcode::RETURN, 1))); +} + +#[test] +fn addsd_xmm0_xmm1() { + // F2 0F 58 /r: addsd xmm1, xmm2/m64. + assert_eq!(decode(&[0xf2, 0x0f, 0x58, 0xc1]), Some((Opcode::ADDSD, 4))); +} + +#[test] +fn syscall_is_outside_the_subset_but_decodes() { + assert_eq!(decode(&[0x0f, 0x05]), Some((Opcode::SYSCALL, 2))); +} diff --git a/crates/clazel-x64/tests/vendored_sources.rs b/crates/clazel-x64/tests/vendored_sources.rs new file mode 100644 index 000000000..42a23c51a --- /dev/null +++ b/crates/clazel-x64/tests/vendored_sources.rs @@ -0,0 +1,78 @@ +//! The vendored instruction-set sources are the ones the generated inventories +//! list (docs/phases/jit.md item J.2): every listed file exists with the +//! listed size, and the pinned file has a well-formed pin. `cargo xtask +//! testdata` checks the hashes; nothing here reads the content yet. + +use std::path::Path; + +mod acl2_x86isa { + include!("generated/acl2_x86isa.rs"); +} +mod acl2_rtl { + include!("generated/acl2_rtl.rs"); +} +mod sail_x86_from_acl2 { + include!("generated/sail_x86_from_acl2.rs"); +} +mod intel_sdm { + include!("generated/intel_sdm.rs"); +} + +fn check(suite: &str, files: &[(&str, u64)]) { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests") + .join("data") + .join(suite); + assert!(!files.is_empty(), "{suite}: empty inventory"); + for (path, size) in files { + let len = std::fs::metadata(dir.join(path)).map(|m| m.len()).ok(); + assert_eq!(len, Some(*size), "{suite}/{path}"); + } +} + +#[test] +#[cfg_attr(miri, ignore = "reads the vendored files")] +fn acl2_x86isa_files_have_their_listed_sizes() { + let files: Vec<_> = acl2_x86isa::FILES + .iter() + .map(|f| (f.path, f.size)) + .collect(); + check("acl2-x86isa", &files); +} + +#[test] +#[cfg_attr(miri, ignore = "reads the vendored files")] +fn acl2_rtl_files_have_their_listed_sizes() { + let files: Vec<_> = acl2_rtl::FILES.iter().map(|f| (f.path, f.size)).collect(); + check("acl2-rtl", &files); +} + +#[test] +#[cfg_attr(miri, ignore = "reads the vendored files")] +fn sail_x86_from_acl2_files_have_their_listed_sizes() { + let files: Vec<_> = sail_x86_from_acl2::FILES + .iter() + .map(|f| (f.path, f.size)) + .collect(); + check("sail-x86-from-acl2", &files); +} + +#[test] +#[cfg_attr(miri, ignore = "reads the vendored files")] +fn the_sdm_is_pinned_and_not_vendored() { + assert_eq!(intel_sdm::PINS.len(), 1); + for p in intel_sdm::PINS { + assert_eq!(p.sha256.len(), 64, "{}", p.file); + assert!(p.size > 0, "{}", p.file); + let here = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests") + .join("data") + .join("intel-sdm") + .join(p.file); + assert!( + !here.exists(), + "{}: Intel's PDF must not be vendored", + p.file + ); + } +} diff --git a/docs/DEPENDENCIES.md b/docs/DEPENDENCIES.md index 864a5e744..6415b7b39 100644 --- a/docs/DEPENDENCIES.md +++ b/docs/DEPENDENCIES.md @@ -255,6 +255,14 @@ Changing a row is a reviewable act. Adding one means saying why below. | unicode-properties | 0.1.5 | test | Unicode General_Category (the only feature `rustybuzz` enables), required by `rustybuzz`. No dependencies. | | unicode-script | 0.5.9 | test | The Unicode Script property, required by `rustybuzz`. No dependencies. | | ab_glyph_rasterizer | 0.1.10 | test | Alex Butler's coverage rasterizer (used by `ab_glyph`, `glyph_brush` and `wgpu_glyph`), a descendant of `font-rs`: the reference engine in `clazel-glyph-mask`'s differential test (`tests/conformance_masks.rs`, phase-5.md item 5.17), the same signed-area accumulation in `f32` written by another author. Used without default features but with `std`, which is its only default; `cargo tree` shows no dependencies (`libm` is optional and off). It has `unsafe` only to call its line function through a pointer chosen by CPU feature detection (all variants are the same scalar code). Pinned to 0.1.10, its latest release. | +| yaxpeax-x86 | 2.2.1 | test | An x86 decoder (yaxpeax project, 0BSD): the reference in `clazel-x64`'s decoder differential test (jit.md items J.2 and J.3, exit criterion 3). Used without any features (its `std` would turn on `yaxpeax-arch`'s, which `yaxpeax-arm` 0.5.0 does not build with); it needs `yaxpeax-arch`, `cfg-if` and `num-traits`, all listed. It replaced `iced-x86` 1.22.0, whose decoder Miri reports as undefined behaviour under Stacked Borrows. | +| yaxpeax-arm | 0.5.0 | test | An ARM decoder (yaxpeax project, 0BSD): the reference in `clazel-a64`'s decoder differential test (jit.md items J.2 and J.4, exit criterion 3). Used without default features (no `serde`) and with `fmt`; it needs `bitvec` and `yaxpeax-arch`. | +| yaxpeax-arch | 0.3.3 | test | The architecture traits of the yaxpeax project (0BSD), required by `yaxpeax-arm` and `yaxpeax-x86`. Used without default features; its only dependency is `num-traits`, which the table lists under `shipped` (the stricter class, as it is locked under libcrux's `cfg(hax)`). | +| bitvec | 1.1.1 | test | Bit-level views of memory (MIT), required by `yaxpeax-arm`. It needs the four packages in the rows below. | +| funty | 2.0.0 | test | Fundamental-type traits (MIT), required by `bitvec`. No dependencies. | +| radium | 0.7.0 | test | Atomic and non-atomic cell abstraction (MIT), required by `bitvec`. No dependencies. | +| tap | 1.0.1 | test | Method-chaining helpers (MIT), required by `bitvec` and `wyz`. No dependencies. | +| wyz | 0.5.1 | test | A utility collection (MIT), required by `bitvec`. Its only dependency is `tap`. | | naga | 30.0.1 | tooling | The wgpu project's shader translator: `xtask/src/shaders.rs` compiles `clazel-gpu`'s GLSL to the committed SPIR-V with it and `cargo xtask check` confirms the two agree (phase-5.md item 5.17b: "compiled from GLSL by tooling that never ships"). No SPIR-V compiler is installed on the development machine, and this keeps the check in Rust. Features `glsl-in`, `spv-out`, and `wgsl-in`, which is unused but without which 30.0.1 does not build. | | naga-types | 30.0.1 | tooling | Required by `naga` (GLSL to SPIR-V for `clazel-gpu`'s shaders, in `xtask` only). | | arrayvec | 0.7.8 | tooling | Required by `naga` (GLSL to SPIR-V for `clazel-gpu`'s shaders, in `xtask` only). | diff --git a/docs/phases/jit.md b/docs/phases/jit.md index 314918f7d..8d37ab02e 100644 --- a/docs/phases/jit.md +++ b/docs/phases/jit.md @@ -365,10 +365,79 @@ code. to each other must be read before they count as independent; Intel's SDM volume 2 for the encoding tables; - A64: Arm's machine-readable specification and the Sail Arm model. -- Pin reference disassemblers for tests only (for example `iced-x86` and +- Pin reference disassemblers for tests only (for example `yaxpeax-x86` and `yaxpeax-arm`), each a `test` row in DEPENDENCIES.md. - Pin the benchmark set, and record the JIT-off numbers. +**Status (2026-11-16): sources pinned, benchmark numbers not recorded.** +- **Pinned:** each is a suite under `crates/clazel-x64/tests/data/` or + `crates/clazel-a64/tests/data/`, with a `SOURCE.md`, a licence and an + `xtask` converter (`xtask/src/testdata/isa.rs`). The crate READMEs record + what the pins show: + - ACL2 `x86isa` (acl2/acl2 at 33b05575, BSD-3-Clause, 2.6 MB); + - Russinoff's RTL library at the same commit (`books/rtl/rel11`, 0.3 MB), + which the x86isa floating-point books are defined with; + - the Sail x86 model translated from it (rems-project/sail-x86-from-acl2 + at 0c6c023d, 0.8 MB); + - Intel's SDM volume 2, version 093, **pinned by URL and SHA-256 only**: + Intel's terms forbid redistribution; + - the Arm Architecture Reference Manual for A-profile (DDI 0487), issue + M.d (Arm's early-access label), **pinned by URL, size (129 MB) and + SHA-256 only**: Arm's terms forbid copying it; + - the Sail Armv9.4-A model (rems-project/sail-arm at 5416ef6b, + BSD-3-Clause-Clear, 3.9 MB); + - Arm's machine-readable specification (BSD-3-Clause, so it may be + redistributed), release 2026-09: the licence and README are vendored, and + `Instructions.json` (116 MB, over the size budget) is pinned by + SHA-256. +- **The two formal models are not independent, as the pins show:** the Sail + x86 model this item names is a mechanical translation of the ACL2 model, + and the Sail Arm model is a translation of Arm's own ASL, the same text as + the machine-readable specification. **Decision (2026-11-16), by the user:** + the second transcription comes from the vendor's manual, not from another + formal model (see the notes under J.3 and J.4). The Sail x86 translation is + kept only as a check on our reading of ACL2, and **it is not independent of + ACL2**. See the two crate READMEs. +- **Considered and rejected:** `shina-lab/sail-x86`, a Sail x86 model written + from the Intel manuals, as the second x86-64 source. It has a single + author, no review, and leaves floating-point arithmetic external + (`extern`, supplied by its emulator). It is not pinned or vendored. +- **Reference disassemblers:** `yaxpeax-x86` 2.2.1 and `yaxpeax-arm` 0.5.0 + (with `yaxpeax-arch`, `bitvec` and four small packages), each a `test` row + in DEPENDENCIES.md, dev-dependencies of the new crates only. On + 2026-11-17 `yaxpeax-x86` replaced `iced-x86` 1.22.0, whose decoder Miri + reports as undefined behaviour under Stacked Borrows. +- **Proposed benchmark set** (a proposal, for a person to accept): the + compute-bound programs of Octane 2.0, from `chromium/octane` (archived; + BSD-3-Clause for the harness, each benchmark file with its own licence + header) at commit 570ad1ccfe86e3eecba0636c8f932ac08edec517. They need no + DOM, and `run.js` is a shell harness. SHA-256 of each file at that commit: + + | File | SHA-256 | + |---|---| + | `base.js` | `216612c2e7096a02b3e52b57e9cf9351bbaf180d60938d5c60b85fd756232733` | + | `run.js` | `b4b10b05e3ccefd6f49d57763ec67ef746c3e7c093ed0fdd602c7f126af5c12f` | + | `richards.js` | `1246a64a24b931158bf01c24640343259fa74b0226e73bad630bd1f686aa0fa7` | + | `deltablue.js` | `6c4784e82f3e8f5c18306d289653d08b17b38838f1bac16b38611d7318fa5a36` | + | `crypto.js` | `b01b6b3fe534327fdef05131162927bc5508f100a3208e185d0c5a4efb200a39` | + | `raytrace.js` | `64b8ff90969966dd69659100e28754976dfc3e9a4f8ee55b9232f974c66ed08c` | + | `earley-boyer.js` | `8dd28a505f7e705642f86816232b012fd3c770ec8afc9f719ce89ce772dab347` | + | `splay.js` | `f9a6a60d8f205908f5542ad1180abc1902dcdab3dcb4278017c5ce179ee123f7` | + | `navier-stokes.js` | `27926de809451c60b0c49a4185c08f97081310d0db166a30c1d202fb656556a2` | + | `box2d.js` | `83b10c280f004e7b156a9e04d09ce4109892ea92788f7c6c963f7fadf29c7bd4` | + | `regexp.js` | `a292d6047900c5296ea9e2628453832cc3bfe397e49fddade8aff7b5876c8263` | + | `LICENSE` | `cf853177951c6de1b17c029b66edf71f8b194cca1722339a7b5def4e8165aaaa` | + + Left out: `code-load`, `pdfjs`, `typescript`, `mandreel`, `gbemu`, `zlib` + (large, or they need `eval`, typed arrays or the DOM). Octane is retired + and measures peak throughput, so it is a smoke measure for a baseline JIT, + not a claim about page loads; WebKit's JetStream (`WebKit/JetStream`) is + the live alternative and was not pinned (it is large and mixes in + WebAssembly). +- **Not done:** the JIT-off numbers. No engine exists, so there is nothing + to measure. They are recorded when the interpreter runs these files, in + the benchmark suite's README, with the machine and the engine commit. + ### J.3 `clazel-x64` (L) - The subset: 64- and 32-bit integer ALU ops, shifts, `imul`, `cmp`/`test` @@ -387,6 +456,18 @@ code. subset. - Hardware differential tests on the Linux and Windows runners. +*Later note (2026-11-16):* the sources, decided by the user. +- The **primary spec** is transcribed from the ACL2 `x86isa` model. +- The **independent transcription** is made from Intel's SDM, its prose and + pseudocode (volume 2, pinned in J.2), not from another formal model. +- A **different session** writes the independent transcription, and it does + not read the primary transcription. +- The **hardware differential tests** are the further check against both. +- The Sail x86 translation (`sail-x86-from-acl2`) is kept only as a check on + our reading of ACL2. It is a machine translation of ACL2, so **it is not + independent of it** and does not count as a second source. The crate header + says so. + ### J.4 `clazel-a64` (L) As J.3. The subset is integer ALU, compare and conditional select, loads and @@ -395,6 +476,17 @@ stores with register base and scaled register or immediate offset, `b`, has fixed 4-byte words and no prefixes. Hardware tests run on the macOS runner. +*Later note (2026-11-16):* the sources, decided by the user. +- The **primary spec** is transcribed from the Sail Arm model and Arm's ASL, + which are one source: the Sail model is a translation of the ASL, which + Arm's machine-readable specification also carries. +- The **independent transcription** is made from the Arm Architecture + Reference Manual's prose (DDI 0487, pinned in J.2), not from another formal + model. +- A **different session** writes the independent transcription, and it does + not read the primary transcription. +- The **hardware differential tests** are the further check against both. + ### J.5 `clazel-lir`: syntax, typing, semantics (M) - Structured control flow, typed values, region-tagged loads and stores @@ -512,8 +604,9 @@ validator is at tier 2. - **x86-64's semantics is large, and its two public formal models may share ancestry.** This is the assumption the safety claim rests on. Mitigations: - keep the subset small, with canonical encodings only; - - check the independence of the two sources in J.2, and record it in the - crate header if they are not independent; + - the two formal models are not independent (J.2), so the second + transcription comes from Intel's SDM, written by a different session + (J.3's note); - hardware differential tests on two OSes; - a decoder differential over every accepted encoding. - **The code process is new trusted code with a powerful handle.** It is diff --git a/xtask/src/status.rs b/xtask/src/status.rs index 717126812..789eae3c3 100644 --- a/xtask/src/status.rs +++ b/xtask/src/status.rs @@ -87,6 +87,9 @@ const PLACES: &[(&str, Place)] = &[ ("clazel-percent", Place::Primitive), ("clazel-base64", Place::Primitive), ("clazel-mime", Place::Primitive), + // The JIT's instruction-set layer (docs/phases/jit.md item J.2 creates them empty). + ("clazel-x64", Place::Primitive), + ("clazel-a64", Place::Primitive), ("clazel-ipc", Place::Primitive), ("clazel-sandbox", Place::Os), ("clazel-sys", Place::Os), diff --git a/xtask/src/testdata.rs b/xtask/src/testdata.rs index ab380f606..e1528260a 100644 --- a/xtask/src/testdata.rs +++ b/xtask/src/testdata.rs @@ -22,6 +22,7 @@ mod html5lib_tree; mod http_state_dates; mod http_state_parser; mod inventory; +mod isa; mod js; mod llhttp; mod nist_cavp_sigver; @@ -203,13 +204,30 @@ const CONVERTERS: &[(&str, Convert, &[&str])] = &[ inventory::WEBREF_IDL_FILES, ), ("ucd-regexp", inventory::convert_ucd, inventory::UCD_FILES), + // JIT item J.2: instruction-set sources (see isa.rs). + ("acl2-x86isa", isa::convert_acl2_x86isa, &[]), + ("acl2-rtl", isa::convert_acl2_rtl, &[]), + ("sail-x86-from-acl2", isa::convert_sail_x86_from_acl2, &[]), + ("intel-sdm", isa::convert_intel_sdm, &[]), + ("sail-arm", isa::convert_sail_arm, &[]), + ("arm-mrs", isa::convert_arm_mrs, &[]), + ("arm-arm", isa::convert_arm_arm, &[]), ]; /// Suites whose directory is a tree of upstream paths, not a flat list: the /// converter walks it and lists what it finds (its inventory is then the /// record of which files exist), and the directory is checked for /// `SOURCE.md` and `LICENSE.md` only. -const TREE_SUITES: &[&str] = &["wpt-web", "todomvc", "test262"]; +const TREE_SUITES: &[&str] = &[ + "wpt-web", + "todomvc", + "test262", + "acl2-x86isa", + "acl2-rtl", + "sail-x86-from-acl2", + "sail-arm", + "arm-mrs", +]; /// A converter for a binary artefact written beside a suite's Rust table. type ConvertBytes = fn(&Path) -> Result