Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BEC46E77188 for ; Fri, 10 Jan 2025 20:10:32 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWLK1-00042J-G8; Fri, 10 Jan 2025 15:09:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWLJx-0003zg-2I for qemu-devel@nongnu.org; Fri, 10 Jan 2025 15:09:29 -0500 Received: from anarch128.org ([2001:4801:7825:104:be76:4eff:fe10:52ae]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWLJt-00032i-Ho for qemu-devel@nongnu.org; Fri, 10 Jan 2025 15:09:27 -0500 Received: from [192.168.1.8] (default-rdns.vocus.co.nz [202.150.110.104] (may be forged)) (authenticated bits=0) by anarch128.org (8.15.2/8.15.2/Debian-22+deb11u3) with ESMTPSA id 50AK9Ed01398678 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Fri, 10 Jan 2025 20:09:17 GMT Authentication-Results: anarch128.org; auth=pass; dkim=pass (2048-bit rsa key sha256) header.d=anarch128.org header.i=@anarch128.org header.b=kQaJ+L8k header.a=rsa-sha256 header.s=100003; x-return-mx=pass header.domain=anarch128.org policy.is_org=yes (MX Records found: mail.anarch128.org); x-return-mx=pass smtp.domain=anarch128.org policy.is_org=yes (MX Records found: mail.anarch128.org) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=anarch128.org; s=100003; t=1736539759; bh=3lXjPdltr6+BbrJ/KAMp6fSKB23d/RFmDGGDHcqsheg=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kQaJ+L8kRU+ZpGYs9H/bZt4EtBJDN8LhaXOtc6jLCZaez5LcTWTgxq5dxMI96fgoN 4/cmAsdHQ3nrm21qqyrwMvsKMVM9dTIzPAhEW/PTXoWxqO8cj/HDmnZbrQlkZSi7+z dywCSdjt0JnyYZn5uhsjhhoNqEU5kEW+vGr5gJaVy5N25iXtg+U4H4bZrfWY/CpaKB h2CrvVFJwXd8CTp6mdy+ijaF6KiL8DfXjJB/j309WI2w1g/JeSquwFdiXMne4cJ+gY hAk0cV0LzIxAoktJmJNETI99qUefZdTJXx9YuKOv+S/QvFTITrsx/aK7eJRWE0s77o 19AVIy6ItYx3A== Message-ID: Date: Sat, 11 Jan 2025 09:09:09 +1300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC]: port of embedded x86-mini disassembler to QEMU To: Paolo Bonzini Cc: qemu-devel , Richard Henderson , Eduardo Habkost , Zhao Liu References: <20250110094400.512788-1-michael@anarch128.org> <5244f8e9-b58d-4d37-9fe7-2236a862fd91@anarch128.org> Content-Language: en-US From: Michael Clark In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2001:4801:7825:104:be76:4eff:fe10:52ae; envelope-from=michael@anarch128.org; helo=anarch128.org X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On 1/11/25 05:05, Paolo Bonzini wrote: > Il ven 10 gen 2025, 14:03 Michael Clark ha scritto: > >> On 1/11/25 00:07, Paolo Bonzini wrote: >>> Il ven 10 gen 2025, 10:52 Michael Clark ha >> scritto: >>> >>>> a note to announce a port of the x86-mini disassembler to QEMU. >>>> >>>> - https://github.com/michaeljclark/qemu/tree/x86-mini >>> >>> I assume the huge .h files are autogenerated? If so, QEMU cannot use them >>> without including the human-readable sources in the tree. >> >> yes indeed. there is an x86_tablegen.py python script in the other repo >> but it is not in the current patch. it would be somewhat easy to read >> the tables from CSV files directly into arrays at the expense of several >> more milliseconds during startup. the revised operand formats maps >> relatively strictly to enum definitions with string tables in the source >> so a reader in C would not be impossible > > > Building the tables at compile time is fine, only leaving out the script is > not. fair enough. I wanted to test the disassembler and I figured out how to do that with both QEMU host and target. I haven't learned how to create generative dependencies in meson yet but it can't be as bad as CMake. QEMU running openssl is a pretty good torture test. I am going to spend time analyzing the -d in_asm,out_asm logs for openssl. I don't yet have a pseudo alias translation step so NOP still shows as XCHG eax,eax. and fuzzing x86_64 was extremely interesting as it uncovered some hardware bugs that led to historic findings inside the QEMU translator. so I know that the level of accuracy is somewhat good. for example: NOP -> XCHG eax,eax REX.B XCHG eax,eax -> XCHG eax,r8d PAUSE -> REP NOP -> REP XCHG eax,eax REX.B PAUSE -> REP REX.B XCHG eax,eax -> REP XCHG eax,r8d it seems Intel filters out REX.B for NOP but not REP NOP. and I know what QEMU does. it does what one expects. unused REP is undefined but typically is ignored for non string instructions with the exception of 0F, 0F38, 0F3A where REP/F3 is interpreted as part of the opcode. but Intel has made REP XCHG eax,r8d act like REP NOP. I haven't tested this out on AMD hardware but I consider it a silicon bug on Intel. there is a test case on this binutils issue. in any case, this is in QEMU history. - https://sourceware.org/bugzilla/show_bug.cgi?id=32462 - https://www.blackhat.com/docs/us-17/thursday/us-17-Domas-Breaking-The-x86-ISA.pdf >> I can see how that might be interesting for x86 virtualization where you >>> have only one target and therefore you can get rid of the capstone >>> dependency. At the same time, other virtualization targets like arm64 and >>> RISC-V are going to become more and more important—not less—and not >> having >>> to maintain a disassembler ourselves as part of QEMU is also a big >> plus... >> >> yes indeed. but in an ideal world the encoders and decoders are matched >> pairs. I would like to work on a translator or interpreter that uses the >> same codec as the disassembler > > > Ok, that makes sense. QEMU already has a decoder that is very table-based > though the tables are hand written. I am not wed to it though—as long as > the code generators remain more or less unmodified, I would love to only > keep "these is how the operands are prepared for use in the IR emitters" > and make the details of x86 decoding Someone Else's Problem. So if you can > kill most (certainly not all) of the tables in > target/i386/tcg/decode-new.c.inc that would be interesting. > > (I am sure you'd find some underspecified and/or wrong parts of the x86 > spec, too :) For example many VEX classes are bollocks, plus some more > examples hinted at at the top of that file). yes indeed. the metadata in the Intel SDM is littered with mistakes such as field transpositions, typos and missing data. I would hazard a guess that maybe ~71% of the metadata is usable in a machine readable manner. given that LLVM tablegen has its own format, I consider x86-mini the source of truth for metadata derived from the Intel format. although I haven't fuzz tested again NASM yet, but I found a small number of errors in LLVM. albeit mostly in instructions that are not used in anger. Michael. > Paolo > > anyway, in fact it is just yet another disassembler at this point, but >> the codec emitter works. it doesn't yet have an arch-neutral TCG-like >> API and IR to drive it yet. >> >