Re: [PATCH 1/2] x86, crypto, Generate .byte code for some new instructions via gas macro

From: Brian Gerst
Date: Thu Nov 05 2009 - 09:40:40 EST


On Thu, Nov 5, 2009 at 1:44 AM, Huang Ying <ying.huang@xxxxxxxxx> wrote:
> It will take some time for binutils (gas) to support some newly added
> instructions, such as SSE4.1 instructions or the AES-NI instructions
> found in upcoming Intel CPU.
>
> To make the source code can be compiled by old binutils, .byte code is
> used instead of the assembly instruction. But the readability and
> flexibility of raw .byte code is not good.
>
> This patch solves the issue of raw .byte code via generating it via
> assembly instruction like gas macro. The syntax is as close as
> possible to real assembly instruction.
>
> Some helper macros such as MODRM is not a full feature
> implementation. It can be extended when necessary.
>
> Signed-off-by: Huang Ying <ying.huang@xxxxxxxxx>
> ---
> Âarch/x86/include/asm/inst.h | Â150 ++++++++++++++++++++++++++++++++++++++++++++
> Â1 file changed, 150 insertions(+)
>
> --- /dev/null
> +++ b/arch/x86/include/asm/inst.h
> @@ -0,0 +1,150 @@
> +/*
> + * Generate .byte code for some instructions not supported by old
> + * binutils.
> + */
> +#ifndef X86_ASM_INST_H
> +#define X86_ASM_INST_H
> +
> +#ifdef __ASSEMBLY__
> +
> + Â Â Â .macro XMM_NUM opd xmm
> + Â Â Â .ifc \xmm,%xmm0
> + Â Â Â \opd = 0
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm1
> + Â Â Â \opd = 1
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm2
> + Â Â Â \opd = 2
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm3
> + Â Â Â \opd = 3
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm4
> + Â Â Â \opd = 4
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm5
> + Â Â Â \opd = 5
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm6
> + Â Â Â \opd = 6
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm7
> + Â Â Â \opd = 7
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm8
> + Â Â Â \opd = 8
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm9
> + Â Â Â \opd = 9
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm10
> + Â Â Â \opd = 10
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm11
> + Â Â Â \opd = 11
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm12
> + Â Â Â \opd = 12
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm13
> + Â Â Â \opd = 13
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm14
> + Â Â Â \opd = 14
> + Â Â Â .endif
> + Â Â Â .ifc \xmm,%xmm15
> + Â Â Â \opd = 15
> + Â Â Â .endif
> + Â Â Â .endm
> +
> + Â Â Â .macro PFX_OPD_SIZE
> + Â Â Â .byte 0x66
> + Â Â Â .endm
> +
> + Â Â Â .macro PFX_REX opd1 opd2
> + Â Â Â .if (\opd1 | \opd2) & 8
> + Â Â Â .byte 0x40 | ((\opd1 & 8) >> 3) | ((\opd2 & 8) >> 1)
> + Â Â Â .endif
> + Â Â Â .endm
> +
> + Â Â Â .macro MODRM mod opd1 opd2
> + Â Â Â .byte \mod | (\opd1 & 7) | ((\opd2 & 7) << 3)
> + Â Â Â .endm
> +
> + Â Â Â .macro PSHUFB_XMM xmm1 xmm2
> + Â Â Â XMM_NUM pshufb_opd1 \xmm1
> + Â Â Â XMM_NUM pshufb_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX pshufb_opd1 pshufb_opd2
> + Â Â Â .byte 0x0f, 0x38, 0x00
> + Â Â Â MODRM 0xc0 pshufb_opd1 pshufb_opd2
> + Â Â Â .endm
> +
> + Â Â Â .macro PCLMULQDQ imm8 xmm1 xmm2
> + Â Â Â XMM_NUM clmul_opd1 \xmm1
> + Â Â Â XMM_NUM clmul_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX clmul_opd1 clmul_opd2
> + Â Â Â .byte 0x0f, 0x3a, 0x44
> + Â Â Â MODRM 0xc0 clmul_opd1 clmul_opd2
> + Â Â Â .byte \imm8
> + Â Â Â .endm
> +
> + Â Â Â .macro AESKEYGENASSIST rcon xmm1 xmm2
> + Â Â Â XMM_NUM aeskeygen_opd1 \xmm1
> + Â Â Â XMM_NUM aeskeygen_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aeskeygen_opd1 aeskeygen_opd2
> + Â Â Â .byte 0x0f, 0x3a, 0xdf
> + Â Â Â MODRM 0xc0 aeskeygen_opd1 aeskeygen_opd2
> + Â Â Â .byte \rcon
> + Â Â Â .endm
> +
> + Â Â Â .macro AESIMC xmm1 xmm2
> + Â Â Â XMM_NUM aesimc_opd1 \xmm1
> + Â Â Â XMM_NUM aesimc_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aesimc_opd1 aesimc_opd2
> + Â Â Â .byte 0x0f, 0x38, 0xdb
> + Â Â Â MODRM 0xc0 aesimc_opd1 aesimc_opd2
> + Â Â Â .endm
> +
> + Â Â Â .macro AESENC xmm1 xmm2
> + Â Â Â XMM_NUM aesenc_opd1 \xmm1
> + Â Â Â XMM_NUM aesenc_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aesenc_opd1 aesenc_opd2
> + Â Â Â .byte 0x0f, 0x38, 0xdc
> + Â Â Â MODRM 0xc0 aesenc_opd1 aesenc_opd2
> + Â Â Â .endm
> +
> + Â Â Â .macro AESENCLAST xmm1 xmm2
> + Â Â Â XMM_NUM aesenclast_opd1 \xmm1
> + Â Â Â XMM_NUM aesenclast_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aesenclast_opd1 aesenclast_opd2
> + Â Â Â .byte 0x0f, 0x38, 0xdd
> + Â Â Â MODRM 0xc0 aesenclast_opd1 aesenclast_opd2
> + Â Â Â .endm
> +
> + Â Â Â .macro AESDEC xmm1 xmm2
> + Â Â Â XMM_NUM aesdec_opd1 \xmm1
> + Â Â Â XMM_NUM aesdec_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aesdec_opd1 aesdec_opd2
> + Â Â Â .byte 0x0f, 0x38, 0xde
> + Â Â Â MODRM 0xc0 aesdec_opd1 aesdec_opd2
> + Â Â Â .endm
> +
> + Â Â Â .macro AESDECLAST xmm1 xmm2
> + Â Â Â XMM_NUM aesdeclast_opd1 \xmm1
> + Â Â Â XMM_NUM aesdeclast_opd2 \xmm2
> + Â Â Â PFX_OPD_SIZE
> + Â Â Â PFX_REX aesdeclast_opd1 aesdeclast_opd2
> + Â Â Â .byte 0x0f, 0x38, 0xdf
> + Â Â Â MODRM 0xc0 aesdeclast_opd1 aesdeclast_opd2
> + Â Â Â .endm
> +#endif
> +
> +#endif

It would be nice to document which version of GAS added support for
each instruction, so that if/when that version becomes the minimum
supported these macros can be removed.

--
Brian Gerst
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/