Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp293607pxu; Fri, 4 Dec 2020 03:32:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4gQsYJhIcJtfrGQZdE7EwubYTL8m0moPzLB04HUNmDg98GCRCdTQLjaLV9fTlkb+Cxjfn X-Received: by 2002:a05:6402:1c04:: with SMTP id ck4mr7069155edb.320.1607081560353; Fri, 04 Dec 2020 03:32:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607081560; cv=none; d=google.com; s=arc-20160816; b=O+dSIxIefNHnhxacZflCp90r9A9AVzSJTs1EimLtL50Lnzj8sqFPh25oVHHBkV9fmB o6Nhjm2Zx7qitsNbtSq4mY9PIftUQO4ozwzkY+gI2YKmH0y8smiPkrxJoiM7bYNvh881 cB4C706tCWN4z3EvrXD4m9chtB4eNagN+h5agFrnXMKZCDp2cFwfVBh1QDbSP8uv7UfA YCytbB7SVFwCZTeeJSOHVCbRIoGYW/lzXuuakRvDzGISymhAMdqki8UIuyE7c42uBVGK JYWtM/JVcUtm81cjVspNtjAzTWB9JoVtdz6eTsHl3m3Oe++yURo3vdSZSpk1EiN9e0Cp F/hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:dkim-signature :date; bh=Jpg+dZQ4OQyCS7KbLNOnKqzq7crs+r4+VkGODqWdjZw=; b=1HnwLmy002YKpl3KnoGA+aZYA1o30hBlqDMka1h9O9MfXVV0AndcZTQbm1Ki8xPxbJ mCnqcSZJzExX6l4rUcTFpQnQlWKaAQOCUeHLOKS6F7AXG1cKFy0CQvGrvloJdycaj9ut jO+v3Rt0Tv59lE2nxQrgUBJ49QPwnaRBPY7dN4y7NsxCToZJUHkPtovovihH165UF/1O ucivPuGTXFpCWJUI2WO4ACFlFaG8ybeOpt52sGzOoq+F3NJ0oR/aQPGI3oLw7vRYG/UA Ndtlw8nG99s0xdLL1FR2NMXrcjSfNIKjIZufD0TvjwiizjqtFqEnCuL12ReChq5PEwwg SvvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YbkoIyK0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r8si2693404edc.489.2020.12.04.03.32.15; Fri, 04 Dec 2020 03:32:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YbkoIyK0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728018AbgLDL3m (ORCPT + 99 others); Fri, 4 Dec 2020 06:29:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:33354 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726851AbgLDL3m (ORCPT ); Fri, 4 Dec 2020 06:29:42 -0500 Date: Fri, 4 Dec 2020 20:28:55 +0900 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1607081340; bh=hmKCjNT8d8BgoJTOCu0r6BK15sF+O97c4V7ccAAURUA=; h=From:To:Cc:Subject:In-Reply-To:References:From; b=YbkoIyK08oU2va8+jOgWK3NYMVFfpd0NQz8ZHtGEUQdJ7CscXQrTEj/gxSaQcmV1x 7h2pixp6/c1YsRfC+BWaXwt+qj+3P1tx+LgmmMI2I/uOrbHOTPb7w3dLuiZy4U+rp9 7We1GUYabXuaR8AHun817W+YulZ8Tg5pvgM57Nuhm+sB2PuBTmy2/2vtfKCen7UDR5 N/vxKZYa4P5BRVbfTqGRVhQKJPAVGscKgqB3nTOg0eOjZKEcKkidFh3GtZ71J32VSf pQ2lyqObT/X+8ovkgHH4JJ1I5hp4nzJB5ZLII9n6zSSD0Ui6YN2TUQEgWh8sf1VgWM 5W70RrFetttsw== From: Masami Hiramatsu To: Borislav Petkov Cc: Tom Lendacky , x86@kernel.org, Thomas Gleixner , Ingo Molnar , Kees Cook , "H . Peter Anvin" , Joerg Roedel , "Gustavo A . R . Silva" , Jann Horn , Srikar Dronamraju , Ricardo Neri , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes Message-Id: <20201204202855.2851ece0930402faf49b0baa@kernel.org> In-Reply-To: <20201204110644.GB31534@zn.tnic> References: <20201203123757.GH3059@zn.tnic> <20201203124121.GI3059@zn.tnic> <20201203124820.GJ3059@zn.tnic> <1c1b265f-34e3-f5cc-0e7b-186dc26c94b7@amd.com> <20201203165420.GL3059@zn.tnic> <20201203170140.GM3059@zn.tnic> <20201203181712.GN3059@zn.tnic> <20201204095653.c948106a294fd6e731df5594@kernel.org> <20201204110644.GB31534@zn.tnic> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 4 Dec 2020 12:06:44 +0100 Borislav Petkov wrote: > On Fri, Dec 04, 2020 at 09:56:53AM +0900, Masami Hiramatsu wrote: > > Hmm, there is a difference between Intel SDM and AMD APM. > > > > Intel SDM vol.2 > > > > 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). > > > > AMD APM vol.3 > > > > 1.2.1 Summary of Legacy Prefixes > > Table 1-1 on page 7 shows the legacy prefixes. The legacy prefixes are organized into five groups, as > > shown in the left-most column of Table 1-1. An instruction encoding may include a maximum of one > > prefix from each of the five groups. > > > > So, Intel CPU doesn't accept LOCK-REP because those are in a same prefix > > group, but AMD says it is acceptable. > > That would be a huge problem for code if both vendors would behave > differently wrt prefixes. > > > Actually, insn.c only accepts the prefix up to 4, so if there is any > > instruction which has 5 prefixes, it will fail to parse. > > Well, actually it looks more like a difference in how both vendors group > things: > > AMD has 5 groups and Intel 4 by putting LOCK and REP together. > > The most important aspect, however, is that you can have as many > prefixes as you want and there's no hardware limitation on the number - > I'm being told - just that you can overflow the instruction limit of 15 > and then get a #GP for invalid insn. See here: > > https://sandpile.org/x86/opc_enc.htm > > note #1 > > with examples how you can overflow the 15 bytes limit even with a valid > insn. > > > Note that anyway the same prefix can be repeated, we can see a good example > > in K8_NOP*. > > Yap. > > > In this case, insn.c just store the 1 osp in the prefixes.bytes[], and > > just increment prefixes.nbytes for the repeated prefixes. > > > > Anyway, if there is LOCK-REP prefix combination, I have to introduce new > > insn_field for legacy prefix. > > Well, the legacy prefixes field needs to be of 4 fields because REP and > LOCK really are two separate but mutually exclusive groups. Why? > > They're used by a disjoint set of instructions, see the AMD doc for both > REP and LOCK prefixes. > > Which means, you can either have a REP (exclusive or) LOCK but not both. Yeah, I found that. So I think the "max number of legacy groups on one instruction" is 4. > Which means, as a stable@ fix I can use Tom's ARRAY_SIZE() suggestion > and then later on we can make the legacy prefixes a separate struct. > Maybe even a struct with a bitfield: Sorry, but I don't think we need such optimization. It seems over- optimized the code for me. Moreover, the last-prefix is meaningful for switching the opcode, so we need to keep it. Thank you, > > struct legacy_prefixes { > /* operand-size override: 0x66 */ > u8 os_over: 1, > /* address-size override: 0x67 */ > as_over: 1, > /* > * segment override: 0x2e(CS), 0x3e(DS), 0x26(ES), 0x64(FS), 0x65(GS), > * 0x36(SS) > */ > s_over: 1, > /* lock prefix: 0xf0 */ > lock: 1, > /* repeat prefixes: 0xf2: REPNx, 0xf3: REPx */ > rep: 1, > __resv: 3; > }; > > or so which you can set to denote when you've seen the respective > prefixes. > > But that we can discuss later. > > -- > Regards/Gruss, > Boris. > > https://people.kernel.org/tglx/notes-about-netiquette -- Masami Hiramatsu