x86: instruction decorder API
PATCH v4 3/6 x86: instruction decorder API
Add x86 instruction decoder to arch-specific libraries. This decoder can decode all x86 instructions into prefix, opcode, modrm, sib, displacement and immediates. This can also show the length of instructions.
---
arch/x86/include/asm/insn.h | 130 +++++++++ arch/x86/lib/Makefile | 1 arch/x86/lib/insn.c | 627 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 758 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/insn.h create mode 100644 arch/x86/lib/insn.c
arch/x86/include/asm/insn.h new file +130 -0
@@ -0,0 +1,130 @@ +#ifndef _ASM_X86_INSN_H +#define _ASM_X86_INSN_H +/* +* x86 instruction analysis +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +* Copyright (C) IBM Corporation, 2009 +*/ + +#include <linux/types.h> + +/* legacy instruction prefixes */ +#define X86_PFX_OPNDSZ 0x1/* 0x66 */ +#define X86_PFX_ADDRSZ 0x2/* 0x67 */ +#define X86_PFX_CS 0x4/* 0x2E */ +#define X86_PFX_DS 0x8/* 0x3E */ +#define X86_PFX_ES 0x10/* 0x26 */ +#define X86_PFX_FS 0x20/* 0x64 */ +#define X86_PFX_GS 0x40/* 0x65 */ +#define X86_PFX_SS 0x80/* 0x36 */ +#define X86_PFX_LOCK 0x100/* 0xF0 */ +#define X86_PFX_REPE 0x200/* 0xF3 */ +#define X86_PFX_REPNE 0x400/* 0xF2 */ +/* REX prefix */ +#define X86_PFX_REX 0x800/* 0x4X */ +/* REX prefix dissected */ +#define X86_PFX_REX_BASE 0x1000 +#define X86_PFX_REXB 0x1000/* 0x41 bit */ +#define X86_PFX_REXX 0x2000/* 0x42 bit */ +#define X86_PFX_REXR 0x4000/* 0x44 bit */ +#define X86_PFX_REXW 0x8000/* 0x48 bit */ + +struct insn_field { +union { +s32 value; +u8 bytes[4 ]; + }; +bool got;/* true if we've run insn_get_xxx() for this field */ +u8 nbytes; +}; + +struct insn { +struct insn_field prefixes;/* prefixes.value is a bitmap */ +struct insn_field opcode;/* +* opcode.bytes[0]: opcode1 +* opcode.bytes[1]: opcode2 +* opcode.bytes[2]: opcode3 +*/ +struct insn_field modrm; +struct insn_field sib; +struct insn_field displacement; +union { +struct insn_field immediate; +struct insn_field moffset1;/* for 64bit MOV */ +struct insn_field immediate1;/* for 64bit imm or off16/32 */ + }; +union { +struct insn_field moffset2;/* for 64bit MOV */ +struct insn_field immediate2;/* for 64bit imm or seg16 */ + }; + +u8 opnd_bytes; +u8 addr_bytes; +u8 length; +bool x86_64; + +const u8 *kaddr;/* kernel address of insn (copy) to analyze */ +const u8 *next_byte; +}; + +#define OPCODE1 (insn) ((insn)->opcode.bytes[0]) +#define OPCODE2 (insn) ((insn)->opcode.bytes[1]) +#define OPCODE3 (insn) ((insn)->opcode.bytes[2]) + +#define MODRM_MOD (insn) (((insn)->modrm.value & 0xc0) >> 6) +#define MODRM_REG (insn) (((insn)->modrm.value & 0x38) >> 3) +#define MODRM_RM (insn) ((insn)->modrm.value & 0x07) + +#define SIB_SCALE (insn) (((insn)->sib.value & 0xc0) >> 6) +#define SIB_INDEX (insn) (((insn)->sib.value & 0x38) >> 3) +#define SIB_BASE (insn) ((insn)->sib.value & 0x07) + +#define MOFFSET64 (insn) (((u64)((insn)->moffset2.value) << 32) | \ + (u32)((insn)->moffset1.value)) + +#define IMMEDIATE64 (insn) (((u64)((insn)->immediate2.value) << 32) | \ + (u32)((insn)->immediate1.value)) + +extern void insn_init (struct insn *insn,const u8 *kaddr,bool x86_64); +extern void insn_get_prefixes (struct insn *insn); +extern void insn_get_opcode (struct insn *insn); +extern void insn_get_modrm (struct insn *insn); +extern void insn_get_sib (struct insn *insn); +extern void insn_get_displacement (struct insn *insn); +extern void insn_get_immediate (struct insn *insn); +extern void insn_get_length (struct insn *insn); + +#ifdef CONFIG_X86_64 +/* Init insn for kernel text */ +#define insn_init_kernel (insn, kaddr) insn_init(insn, kaddr, 1) +extern bool insn_rip_relative (struct insn *insn); + +#else /* CONFIG_X86_32 */ + +#define insn_init_kernel (insn, kaddr) insn_init(insn, kaddr, 0) +static inline bool insn_rip_relative (struct insn *insn) +{ +return false; +} +#endif + +static inline bool insn_field_exists (const struct insn_field *field) +{ +return (field->nbytes >0 ); +} + +#endif /* _ASM_X86_INSN_H */
arch/x86/lib/Makefile +1 -0
@@ -8,6 +8,7 @@ lib-y := delay.o lib-y += thunk_$(BITS).o lib-y += usercopy_$(BITS).o getuser.o putuser.o lib-y += memcpy_$(BITS).o +lib-y += insn.o ifeq ($(CONFIG_X86_32),y) lib-y += checksum_32.o
arch/x86/lib/insn.c new file +627 -0
627 lines not on this page · raw message
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
PATCH 3/6 x86: instruction decorder API
Add x86 instruction decoder to arch-specific libraries. This decoder can decode all x86 instructions into prefix, opcode, modrm, sib, displacement and immediates. This can also show the length of instructions.
changes from v4:
- make bitmap tables static.
---
arch/x86/include/asm/insn.h | 130 +++++++++ arch/x86/lib/Makefile | 1 arch/x86/lib/insn.c | 627 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 758 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/insn.h create mode 100644 arch/x86/lib/insn.c
arch/x86/include/asm/insn.h new file +130 -0
@@ -0,0 +1,130 @@ +#ifndef _ASM_X86_INSN_H +#define _ASM_X86_INSN_H +/* +* x86 instruction analysis +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +* Copyright (C) IBM Corporation, 2009 +*/ + +#include <linux/types.h> + +/* legacy instruction prefixes */ +#define X86_PFX_OPNDSZ 0x1/* 0x66 */ +#define X86_PFX_ADDRSZ 0x2/* 0x67 */ +#define X86_PFX_CS 0x4/* 0x2E */ +#define X86_PFX_DS 0x8/* 0x3E */ +#define X86_PFX_ES 0x10/* 0x26 */ +#define X86_PFX_FS 0x20/* 0x64 */ +#define X86_PFX_GS 0x40/* 0x65 */ +#define X86_PFX_SS 0x80/* 0x36 */ +#define X86_PFX_LOCK 0x100/* 0xF0 */ +#define X86_PFX_REPE 0x200/* 0xF3 */ +#define X86_PFX_REPNE 0x400/* 0xF2 */ +/* REX prefix */ +#define X86_PFX_REX 0x800/* 0x4X */ +/* REX prefix dissected */ +#define X86_PFX_REX_BASE 0x1000 +#define X86_PFX_REXB 0x1000/* 0x41 bit */ +#define X86_PFX_REXX 0x2000/* 0x42 bit */ +#define X86_PFX_REXR 0x4000/* 0x44 bit */ +#define X86_PFX_REXW 0x8000/* 0x48 bit */ + +struct insn_field { +union { +s32 value; +u8 bytes[4 ]; + }; +bool got;/* true if we've run insn_get_xxx() for this field */ +u8 nbytes; +}; + +struct insn { +struct insn_field prefixes;/* prefixes.value is a bitmap */ +struct insn_field opcode;/* +* opcode.bytes[0]: opcode1 +* opcode.bytes[1]: opcode2 +* opcode.bytes[2]: opcode3 +*/ +struct insn_field modrm; +struct insn_field sib; +struct insn_field displacement; +union { +struct insn_field immediate; +struct insn_field moffset1;/* for 64bit MOV */ +struct insn_field immediate1;/* for 64bit imm or off16/32 */ + }; +union { +struct insn_field moffset2;/* for 64bit MOV */ +struct insn_field immediate2;/* for 64bit imm or seg16 */ + }; + +u8 opnd_bytes; +u8 addr_bytes; +u8 length; +bool x86_64; + +const u8 *kaddr;/* kernel address of insn (copy) to analyze */ +const u8 *next_byte; +}; + +#define OPCODE1 (insn) ((insn)->opcode.bytes[0]) +#define OPCODE2 (insn) ((insn)->opcode.bytes[1]) +#define OPCODE3 (insn) ((insn)->opcode.bytes[2]) + +#define MODRM_MOD (insn) (((insn)->modrm.value & 0xc0) >> 6) +#define MODRM_REG (insn) (((insn)->modrm.value & 0x38) >> 3) +#define MODRM_RM (insn) ((insn)->modrm.value & 0x07) + +#define SIB_SCALE (insn) (((insn)->sib.value & 0xc0) >> 6) +#define SIB_INDEX (insn) (((insn)->sib.value & 0x38) >> 3) +#define SIB_BASE (insn) ((insn)->sib.value & 0x07) + +#define MOFFSET64 (insn) (((u64)((insn)->moffset2.value) << 32) | \ + (u32)((insn)->moffset1.value)) + +#define IMMEDIATE64 (insn) (((u64)((insn)->immediate2.value) << 32) | \ + (u32)((insn)->immediate1.value)) + +extern void insn_init (struct insn *insn,const u8 *kaddr,bool x86_64); +extern void insn_get_prefixes (struct insn *insn); +extern void insn_get_opcode (struct insn *insn); +extern void insn_get_modrm (struct insn *insn); +extern void insn_get_sib (struct insn *insn); +extern void insn_get_displacement (struct insn *insn); +extern void insn_get_immediate (struct insn *insn); +extern void insn_get_length (struct insn *insn); + +#ifdef CONFIG_X86_64 +/* Init insn for kernel text */ +#define insn_init_kernel (insn, kaddr) insn_init(insn, kaddr, 1) +extern bool insn_rip_relative (struct insn *insn); + +#else /* CONFIG_X86_32 */ + +#define insn_init_kernel (insn, kaddr) insn_init(insn, kaddr, 0) +static inline bool insn_rip_relative (struct insn *insn) +{ +return false; +} +#endif + +static inline bool insn_field_exists (const struct insn_field *field) +{ +return (field->nbytes >0 ); +} + +#endif /* _ASM_X86_INSN_H */
arch/x86/lib/Makefile +1 -0
@@ -8,6 +8,7 @@ lib-y := delay.o lib-y += thunk_$(BITS).o lib-y += usercopy_$(BITS).o getuser.o putuser.o lib-y += memcpy_$(BITS).o +lib-y += insn.o ifeq ($(CONFIG_X86_32),y) lib-y += checksum_32.o
arch/x86/lib/insn.c new file +627 -0
627 lines not on this page · raw message
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
H. Peter Anvin
reviewerQuoted 7 lines · Masami Hiramatsu
Add x86 instruction decoder to arch-specific libraries. This decoder can decode all x86 instructions into prefix, opcode, modrm, sib, displacement and immediates. This can also show the length of instructions.
changes from v4:
- make bitmap tables static.
Hi Masami,
On the surface the overall structure looks fine, but I have a couple of concerns:
1. is this meant to be able to decode userspace code or just kernel code? If it is supposed to be able to decode userspace code, is there a reason you're not dealing with 16-bit or V86 mode code at all? If not, why are you including the 32-bit decoder in a 64-bit kernel (as well as instructions which we're pretty much guaranteed to never use in the kernel, such as ENTER.)
2. you're already not dealing with all existing three-byte opcode spaces, nor with DREX or VEX encodings for upcoming processors. This doesn't matter so much for the kernel, but it does matter if this is supposed to be used for user-space code.
3. is there any need to deal with instruction set differences among processors? (Again, this depends on the usage model.)
4. you have a bunch of magic opcode constants all over the place. This means that as new instructions come in -- and they're going to be coming in -- this is going to be hard to update. It would be cleaner if we could have an intermediate format that preprocesses down to all the relevant tables and perhaps even some of the code rather than open-coding everything in C.
This matters... for example you have:
+ } else if (opcode == 0xea /* jmp far seg:offs */) {
+ __get_immptr(insn);
... but nothing similar for opcode 0x9a. This is extremely hard to spot with this kind of structure.
The more data-driven we can make it (without bloating the code too much) the better off we are, I believe.
-hpa
Hi Peter,
Quoted 14 lines · H. Peter Anvin
Quoted 7 lines · Masami Hiramatsu
Add x86 instruction decoder to arch-specific libraries. This decoder can decode all x86 instructions into prefix, opcode, modrm, sib, displacement and immediates. This can also show the length of instructions.
changes from v4:
- make bitmap tables static.Hi Masami,
On the surface the overall structure looks fine, but I have a couple of concerns:
Quoted 6 lines
1. is this meant to be able to decode userspace code or just kernel code? If it is supposed to be able to decode userspace code, is there a reason you're not dealing with 16-bit or V86 mode code at all? If not, why are you including the 32-bit decoder in a 64-bit kernel (as well as instructions which we're pretty much guaranteed to never use in the kernel, such as ENTER.)
Actually, this aims to decode both of user space and kernel code. At this point, it just needs to cover kernel code, because kprobes just want to decode kernel binary.
However, this is just a starting point, uprobe developers want to use it to decode user-space code. In that case, it needs to be enhanced.
Quoted 7 lines
2. you're already not dealing with all existing three-byte opcode spaces, nor with DREX or VEX encodings for upcoming processors. This doesn't matter so much for the kernel, but it does matter if this is supposed to be used for user-space code.
3. is there any need to deal with instruction set differences among processors? (Again, this depends on the usage model.)
Agreed. When it support decoding user-space code, it should support all kind of instructions.
Quoted 13 lines
4. you have a bunch of magic opcode constants all over the place. This means that as new instructions come in -- and they're going to be coming in -- this is going to be hard to update. It would be cleaner if we could have an intermediate format that preprocesses down to all the relevant tables and perhaps even some of the code rather than open-coding everything in C.
This matters... for example you have:
arch/x86/lib/insn.c new file +2
-0+ }else if (opcode ==0xea /* jmp far seg:offs */ ) { +__get_immptr (insn);
Quoted 2 lines
... but nothing similar for opcode
0x9a. This is extremely hard to spot with this kind of structure.
Oops, that should be a bug. Hmm, I think we'd better bit-flags tables for classifying opcodes.
Jim, can your INAT idea help this situation?
http://sources.redhat.com/ml/systemtap/2009-q2/msg00109.html
Quoted 5 lines
The more data-driven we can make it (without bloating the code too much) the better off we are, I believe.
-hpa
Thank you for good advice!
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
Quoted 9 lines · Masami Hiramatsu
Hi Peter,
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · Masami Hiramatsu
Add x86 instruction decoder to arch-specific libraries. This decoder can decode all x86 instructions into prefix, opcode, modrm, sib, displacement and immediates. This can also show the length of instructions.
...
Quoted 13 lines
Quoted 6 lines
Hi Masami,
On the surface the overall structure looks fine, but I have a couple of concerns:
Quoted 6 lines
1. is this meant to be able to decode userspace code or just kernel code? If it is supposed to be able to decode userspace code, is there a reason you're not dealing with 16-bit or V86 mode code at all? If not, why are you including the 32-bit decoder in a 64-bit kernel (as well as instructions which we're pretty much guaranteed to never use in the kernel, such as ENTER.)
Quoted 6 lines
Actually, this aims to decode both of user space and kernel code. At this point, it just needs to cover kernel code, because kprobes just want to decode kernel binary.
However, this is just a starting point, uprobe developers want to use it to decode user-space code. In that case, it needs to be enhanced.
For user-space probing, we've been concentrating on native-built executables. Am I correct in thinking that we'll see 16-bit or V86 mode only on legacy apps built elsewhere? In any case, it only makes sense to build on the kvm folks' work in this regard.
...
Quoted 14 lines
Quoted 14 lines
4. you have a bunch of magic opcode constants all over the place. This means that as new instructions come in -- and they're going to be coming in -- this is going to be hard to update. It would be cleaner if we could have an intermediate format that preprocesses down to all the relevant tables and perhaps even some of the code rather than open-coding everything in C.
This matters... for example you have:
arch/x86/lib/insn.c new file +2
-0+ }else if (opcode ==0xea /* jmp far seg:offs */ ) { +__get_immptr (insn);
Quoted 9 lines
Quoted 2 lines
... but nothing similar for opcode
0x9a. This is extremely hard to spot with this kind of structure.Oops, that should be a bug. Hmm, I think we'd better bit-flags tables for classifying opcodes.
Jim, can your INAT idea help this situation?http://sources.redhat.com/ml/systemtap/2009-q2/msg00109.html
As noted, the INAT tables follow the kvm model of one fat bitmap of attributes per opcode, rather than the kprobes/uprobes model of one or two 256-bit tables per attribute. (This latter approach was due to the gradual accumulation of tables over the years.)
I like the bitmap-per-opcode approach because it's relatively easy to see in one place everything you're saying about a particular opcode. But with all the potential clients for this service, it's not clear that we'll get by with a single bitmap for every opcode. (x86 kvm uses 32 bits per opcode, I think, and the INAT tables use 10. Seems like we could overrun 64 bits pretty quickly.) So I guess that means we'll have to get a little creative as to how we expose these attribute sets to the client.
...
Quoted 3 lines
Thank you for good advice!
Ditto.
Jim Keniston
H. Peter Anvin
reviewerQuoted 6 lines · Jim Keniston
For user-space probing, we've been concentrating on native-built executables. Am I correct in thinking that we'll see 16-bit or V86 mode only on legacy apps built elsewhere? In any case, it only makes sense to build on the kvm folks' work in this regard.
That's a fair assumption; you will of course need to test it and take appropriate action if it doesn't pan out.
Quoted 6 lines
As noted, the INAT tables follow the kvm model of one fat bitmap of attributes per opcode, rather than the kprobes/uprobes model of one or two 256-bit tables per attribute. (This latter approach was due to the gradual accumulation of tables over the years.)
Quoted 9 lines
I like the bitmap-per-opcode approach because it's relatively easy to see in one place everything you're saying about a particular opcode. But with all the potential clients for this service, it's not clear that we'll get by with a single bitmap for every opcode. (x86 kvm uses 32 bits per opcode, I think, and the INAT tables use 10. Seems like we could overrun 64 bits pretty quickly.) So I guess that means we'll have to get a little creative as to how we expose these attribute sets to the client.
This is another very good reason to use an instruction table which is preprocessed into a usable format: it means that if the internal data structures change -- and they almost certainly will have to at some point -- the raw data isn't lost.
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Quoted 25 lines · H. Peter Anvin
Quoted 5 lines · Jim Keniston
For user-space probing, we've been concentrating on native-built executables. Am I correct in thinking that we'll see 16-bit or V86 mode only on legacy apps built elsewhere? In any case, it only makes sense to build on the kvm folks' work in this regard.
That's a fair assumption; you will of course need to test it and take appropriate action if it doesn't pan out.
Quoted 5 lines
As noted, the INAT tables follow the kvm model of one fat bitmap of attributes per opcode, rather than the kprobes/uprobes model of one or two 256-bit tables per attribute. (This latter approach was due to the gradual accumulation of tables over the years.)
Quoted 9 lines
I like the bitmap-per-opcode approach because it's relatively easy to see in one place everything you're saying about a particular opcode. But with all the potential clients for this service, it's not clear that we'll get by with a single bitmap for every opcode. (x86 kvm uses 32 bits per opcode, I think, and the INAT tables use 10. Seems like we could overrun 64 bits pretty quickly.) So I guess that means we'll have to get a little creative as to how we expose these attribute sets to the client.
Quoted 4 lines
This is another very good reason to use an instruction table which is preprocessed into a usable format: it means that if the internal data structures change -- and they almost certainly will have to at some point -- the raw data isn't lost.
Hmm, I have an idea about instruction table. Usually, instruction tables are encoded with code defined by each decoder/emulator. This method will show their internal code directly, and is hard to maintain when the opcode map is updated. Instead of that, I'd like to suggest using the expressions in the opcode maps in a vender's genuine document (in this case, Intel/AMD's manual) or www.sandpile.org for instruction tables.
e.g.
const insn_attr_t onebyte_attr_table[ATTR_TABLE_SIZE] = { /* 0x00-0x0f */AT2(Eb,Gb), AT2(Ev,Gv), AT2(Gb,Eb), AT2(Gv,Ev), AT2(AL,Ib), AT2(rAX,Iz), AT2(ES,i64), AT2(ES,i64), AT2(Eb,Gb), AT2(Ev,Gv), AT2(Gb,Eb), AT2(Gv,Ev), AT2(AL,Ib), AT2(rAX,Iz), AT2(CS,i64), AT(ESC),
...
Here, AT and AT2 macros are defined as follows:
#define AT(a) (INAT_OMEXP_##a)
#define AT2(a1, a2) (INAT_OMEXP_##a1 | INAT_OMEXP_##a2)
(OMEXP means Opcode Map Expression)
And each INAT_OMEXP_* is translated into internal format.
#define INAT_OMEXP_Eb INAT_ENCODE_RM(TYPE_MEMREG, SIZE_BYTE)
#define INAT_OMEXP_Gb INAT_ENCODE_REG(TYPE_MEMREG, SIZE_BYTE)
...
This idea will allow everyone to easily maintain instruction tables by comparing instruction tables with vender's opcode map.
Designing internal instruction tables is harder. Currently, I'm working on below layout.
Comments are welcome!
Instruction Attribute Encoding
==============================
Bitmap layout:
[ESC]
0 0 [(padding)][OPFLG][IMM][REG][RGT][RGS][RM][RMT][RMS] 0 1 [(padding)][PFXGRP][PFXEXT][Prefix code]
1 0 [(padding)][EID]
1 1 [(padding)][GID]
ESC(2): Switching normal/escape/group/prefix.
(0:normal opcode, 1:(Legacy)Prefix, 2:Escape, 3:Group)
- Normal opcodes
OPFLG(7): Flag bits: [REX][LPFX][I64][F64][NOPR][EREG][AIMM]
REX(1): Opcode is a REX prefix.
LPFX(1): Opcode can be modified by Last Prefix(SSE2-4) I64(1): Opcode is invalid in 64bit mode.
F64(1): Oprand is 64bits width in 64bit mode. NOPR(1): Opcode has no operand.
EREG(1) : Opcode byte encodes Registers
AIMM(1) : Opcode has another 1 byte Immediate(2nd Immediate).
IMM(3): Immediate size bits
(0:none, 1:byte, 2:word, 3:dword, 4:qword, 5:pointer,
6:word/dword, 7:word/dword/qword)
REG(1): Opcode has ModRM 'reg'
RGT(3): ModRM 'reg' type or special operand bits
(0:none,
REG=0: 1:DS/SI
REG=1: 1:GPR, 2:MMX, 3:XMM, 4:DBG, 5:CTR, 6:FP, 7:SR)
RGS(3): ModRM 'reg' or special operand size bits
(GPR: 1:byte, 2:word, 3:dword, 4:qword, 5:N/A, 6:word/dword,
7:word/dword/qword)
(MMX: 3:dword, 4:qword)
(XMM: 2:Scalar-SingleFP, 3:Scaler-DoubleFP, 4:qword, 5:d-qword,
6:Packed-SingleFP, 7:Packed-DoubleFP)
(FP: ?)
(Others: same as GPR)
RM(1) : Opcode has ModRM 'rm'
RMT(3) : ModRM 'rm' type or special operand bits
(0:none,
RM=0: 1:ES/DI
RM=1: 1:GPR, 2:MMX, 3:XMM, 4:Memory, 5:GPR/Mem, 6:MMX/MEM, 7:XMM/Mem)RMS(3): ModRM 'rm' or special operand size bits. see RGS.
- Legacy prefixes
PFXGRP(4): Prefix group bits: [PGRP1][PGRP2][PGRP3][PGRP4]
PGRP1(1): opcode is prefix group1
PGRP2(1): opcode is prefix group2
PGRP3(1): opcode is prefix group3
PGRP4(1): opcode is prefix group4
PFXEXT(2): Mandatory prefix extent
(0:none, 1:66H, 2:F2H, 3:F3H)
Prefix code(11): Prefix code bits
- Escape opcode
EID(2): Escape code id.
(0:2byte escape, 1:FPU escape, 2:3byte escape1, 3:3byte escape2)
- Group opcode
GID(5): Group Number
(0:Group1, 1:Group1A, 2:Group2, ... 16:Group16)
Thanks,
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
H. Peter Anvin
reviewerQuoted 9 lines · Masami Hiramatsu
Hmm, I have an idea about instruction table. Usually, instruction tables are encoded with code defined by each decoder/emulator. This method will show their internal code directly, and is hard to maintain when the opcode map is updated. Instead of that, I'd like to suggest using the expressions in the opcode maps in a vender's genuine document (in this case, Intel/AMD's manual) or www.sandpile.org for instruction tables.
Yes, we discussed this at the Collab Summit. I think it's the only sane thing.
Quoted 10 lines
e.g.
const
insn_attr_tonebyte_attr_table[ATTR_TABLE_SIZE] = { /* 0x00-0x0f */AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(ES,i64),AT2(ES,i64),AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(CS,i64), AT(ESC),
...
Quoted 2 lines
Here, AT and AT2 macros are defined as follows:
I would suggest using an actual parser, rather than relying on cpp for this. The parser will be much more powerful, and will make it much easier to change data structure radically as we discussed.
-hpa
Quoted 24 lines · H. Peter Anvin
Quoted 9 lines · Masami Hiramatsu
Hmm, I have an idea about instruction table. Usually, instruction tables are encoded with code defined by each decoder/emulator. This method will show their internal code directly, and is hard to maintain when the opcode map is updated. Instead of that, I'd like to suggest using the expressions in the opcode maps in a vender's genuine document (in this case, Intel/AMD's manual) or www.sandpile.org for instruction tables.
Yes, we discussed this at the Collab Summit. I think it's the only sane thing.
Quoted 10 lines
e.g.
const
insn_attr_tonebyte_attr_table[ATTR_TABLE_SIZE] = { /* 0x00-0x0f */AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(ES,i64),AT2(ES,i64),AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(CS,i64), AT(ESC),
...
Quoted 6 lines
Quoted 2 lines
Here, AT and AT2 macros are defined as follows:
I would suggest using an actual parser, rather than relying on cpp for this. The parser will be much more powerful, and will make it much easier to change data structure radically as we discussed.
Aah, I see. So we'd better make a parser which generates internal data structure from genuine opcode map in compilation time.
And I changed my mind about internal data structure too. In this version, I'll use a smallest bits which are needed for the decoder.
Thank you,
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
H. Peter Anvin
reviewerQuoted 8 lines · Masami Hiramatsu
Aah, I see. So we'd better make a parser which generates internal data structure from genuine opcode map in compilation time.
And I changed my mind about internal data structure too. In this version, I'll use a smallest bits which are needed for the decoder.
Yes, and with a proper compile-time parser, that kind of things can be done, and be changed later if it is no longer appropriate.
-hpa
On Thu, 2009-04-16 at 19:31 -0400, Masami Hiramatsu wrote:
...
Quoted 19 lines
Hmm, I have an idea about instruction table. Usually, instruction tables are encoded with code defined by each decoder/emulator. This method will show their internal code directly, and is hard to maintain when the opcode map is updated. Instead of that, I'd like to suggest using the expressions in the opcode maps in a vender's genuine document (in this case, Intel/AMD's manual) or www.sandpile.org for instruction tables.
e.g.
const
insn_attr_tonebyte_attr_table[ATTR_TABLE_SIZE] = { /* 0x00-0x0f */AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(ES,i64),AT2(ES,i64),AT2(Eb,Gb),AT2(Ev,Gv),AT2(Gb,Eb),AT2(Gv,Ev),AT2(AL,Ib),AT2(rAX,Iz),AT2(CS,i64), AT(ESC),
...
Quoted 5 lines
Here, AT and AT2 macros are defined as follows:
#define AT(a) (INAT_OMEXP_##a)
#defineAT2(a1, a2)(INAT_OMEXP_##a1 | INAT_OMEXP_##a2)
...
It looks like AT2(Ev,Gv) would yield the same bits as AT2(Gv,Ev). It'd be nice not to lose the operand-order information. And we'd have to make clear whether which notation we're using -- src,dest as in the gnu assembler, or dest,src as in the AMD (and Intel?) manuals.
Jim
H. Peter Anvin
reviewerQuoted 6 lines · Jim Keniston
It looks like
AT2(Ev,Gv)would yield the same bits asAT2(Gv,Ev). It'd be nice not to lose the operand-order information. And we'd have to make clear whether which notation we're using -- src,dest as in the gnu assembler, or dest,src as in the AMD (and Intel?) manuals.
Since the information would come from the manuals, I would recommend following them (dst first.)
-hpa
Quoted 10 lines · H. Peter Anvin
Quoted 5 lines · Jim Keniston
It looks like
AT2(Ev,Gv)would yield the same bits asAT2(Gv,Ev). It'd be nice not to lose the operand-order information. And we'd have to make clear whether which notation we're using -- src,dest as in the gnu assembler, or dest,src as in the AMD (and Intel?) manuals.Since the information would come from the manuals, I would recommend following them (dst first.)
Hi Peter and Jim,
Now what I'm doing is making opcode tables like this.
Table: 1-byte opcode
Alias: none
00: ADD Eb,Gb
01: ADD Ev,Gv
02: ADD Gb,Eb
03: ADD Gv,Ev
04: ADD AL,Ib
05: ADD rAX,Iz
06: PUSH ES (i64)
07: POP ES (i64)
08: OR Eb,Gb
09: OR Ev,Gv
0a: OR Gb,Eb
0b: OR Gv,Ev
0c: OR AL,Ib
0d: OR rAX,Iz
0e: PUSH CS
0f: 2-byte escape
...
and a parser script which parses them into,
const insn_attr_t primary_table[INAT_TABLE_SIZE] = {
[0x04] = INAT_IMM(IMM_SIZE_BYTE)
[0x05] = INAT_IMM(IMM_SIZE_VWORD32)
[0x0c] = INAT_IMM(IMM_SIZE_BYTE)
[0x0d] = INAT_IMM(IMM_SIZE_VWORD32)
[0x0f] = INAT_ESC(IMM_ESC_2BYTE)...
(note, instructions which has no attributes for decoder, are just ignored)
By the way, I'm worried about legal things of Intel's instruction encoding expressions. Would you think there is any problem if we have those tables in the kernel tree?
Thanks,
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
On Tue, 2009-04-21 at 20:17 -0400, Masami Hiramatsu wrote:
...
Quoted 15 lines
Hi Peter and Jim,
Now what I'm doing is making opcode tables like this.
Table: 1-byte opcode
Alias: none
00: ADD Eb,Gb
01: ADD Ev,Gv
02: ADD Gb,Eb
03: ADD Gv,Ev
04: ADD AL,Ib
05: ADD rAX,Iz
06: PUSH ES (i64)
07: POP ES (i64)
08: OR Eb,Gb
Quoted 8 lines
09: OR Ev,Gv
0a: OR Gb,Eb
0b: OR Gv,Ev
0c: OR AL,Ib
0d: OR rAX,Iz
0e: PUSH CS
0f: 2-byte escape
...
We want to keep this info easy to parse. (Who knows how it might be used, and by whom?) Your format seems to be
opcode: mnemonic [comma,separated,operands] [(extra_info)]
which is fine if you stick to it... but your entry for 0f doesn't match that.
Also, something like
+ extra_info
would be easier to parse (using, say, awk) than
(extra_info)
Quoted 9 lines
and a parser script which parses them into,
const insn_attr_t primary_table[INAT_TABLE_SIZE] = { [0x04] = INAT_IMM(IMM_SIZE_BYTE) [0x05] = INAT_IMM(IMM_SIZE_VWORD32) [0x0c] = INAT_IMM(IMM_SIZE_BYTE) [0x0d] = INAT_IMM(IMM_SIZE_VWORD32) [0x0f] = INAT_ESC(IMM_ESC_2BYTE)
Quoted 8 lines
...
(note, instructions which has no attributes for decoder, are just ignored)
By the way, I'm worried about legal things of Intel's instruction encoding expressions. Would you think there is any problem if we have those tables in the kernel tree?
Good question. Sorry, I'm not a lawyer. Intel and AMD and sandpile.org all seem to be using the same notation, so the notation's inventor must not be feeling too proprietary. Interestingly, sandpile.org asserts a copyright.
Quoted 3 lines
Thanks,
Jim
Quoted 26 lines · Jim Keniston
On Tue, 2009-04-21 at 20:17 -0400, Masami Hiramatsu wrote:
...Quoted 15 lines
Hi Peter and Jim,
Now what I'm doing is making opcode tables like this.
Table: 1-byte opcode
Alias: none
00: ADD Eb,Gb
01: ADD Ev,Gv
02: ADD Gb,Eb
03: ADD Gv,Ev
04: ADD AL,Ib
05: ADD rAX,Iz
06: PUSH ES (i64)
07: POP ES (i64)
08: OR Eb,GbQuoted 8 lines
09: OR Ev,Gv
0a: OR Gb,Eb
0b: OR Gv,Ev
0c: OR AL,Ib
0d: OR rAX,Iz
0e: PUSH CS
0f: 2-byte escape
...
Quoted 5 lines
We want to keep this info easy to parse. (Who knows how it might be used, and by whom?) Your format seems to be
opcode: mnemonic [comma,separated,operands] [(extra_info)]
which is fine if you stick to it... but your entry for 0f doesn't match that.
Sure, it was just based on the original opcode map. Maybe, we can have something special expressions for that. e.g.
0f: ESC # 2-byte escape
Quoted 4 lines
Also, something like
+extra_info
would be easier to parse (using, say, awk) than
(extra_info)
Hmm, maybe, parser can handle "(extra_info)" as a solid keyword. so let's define actual format.
<opcode maps>
Table: table-name
Referrer: escamed-name
opcode: mnemonic|Grp [operand1[,operand2...]] [(extra1)[,(extra2)...] [| 2nd-mnemonic ...]
opcode: ESC # escaped-name
<group maps>
reg: mnemonic ...
Thank you,
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]
On Thu, 2009-04-23 at 13:29 -0400, Masami Hiramatsu wrote:
...
Quoted 4 lines
Hmm, maybe, parser can handle "(
extra_info)" as a solid keyword. so let's define actual format.
Quoted 8 lines
<opcode maps>
Table: table-name
Referrer: escamed-name
opcode: mnemonic|Grp [operand1[,operand2...]] [(extra1)[,(extra2)...] [| 2nd-mnemonic ...]
opcode: ESC # escaped-name<group maps>
reg: mnemonic ...
For some instruction groups -- e.g., Groups 12, 13, 14 -- the instruction prefix (66, f2, f3) and the reg field both affect the instruction type. And for some x87 instructions, the value of the modrm byte's rm field also affects the instruction type. (For others, rm just selects among the st(0)..st(7) registers, as one might expect.)
Of course, that's all about floating-point instructions, which are of more interest to uprobes than kprobes.
Jim
Quoted 9 lines · Jim Keniston
On Thu, 2009-04-23 at 13:29 -0400, Masami Hiramatsu wrote:
...Quoted 7 lines
Hmm, maybe, parser can handle "(
extra_info)" as a solid keyword. so let's define actual format.<opcode maps>
Table: table-name
Referrer: escamed-name
opcode: mnemonic|Grp [operand1[,operand2...]] [(extra1)[,(extra2)...] [| 2nd-mnemonic ...]
This should be:
opcode: mnemonic|GrpXXX ...
Quoted 1 line
Quoted 1 line
opcode: ESC # escaped-name
This should be
opcode: escape # escaped-name
because distinguishing from x87 ESC ops.
Quoted 9 lines
Quoted 3 lines
<group maps>
reg: mnemonic ...For some instruction groups -- e.g., Groups 12, 13, 14 -- the instruction prefix (66, f2, f3) and the reg field both affect the instruction type. And for some x87 instructions, the value of the modrm byte's rm field also affects the instruction type. (For others, rm just selects among the st(0)..st(7) registers, as one might expect.)
Sure, I updated the format. There are some special cases,
(1) instructions which are switched by 64bit mode 40: INC eAX (i64) | REX (o64)
(2) instructions which are switched by the last prefix 13: movlps Mq,Vq | movlpd Mq,Vq (66)
(3) group instructions which are switched by modr/m
0: SGDT Ms | VMCALL (11B),(001) | VMLAUNCH (11B),(010) | VMRESUME (11B),(011) | VMXOFF (11B),(100)
Quoted 3 lines
Of course, that's all about floating-point instructions, which are of more interest to uprobes than kprobes.
Hmm, x87 instructions may need to have some special format...
Thank you,
Signature · 7 lines
Masami Hiramatsu Software Engineer Hitachi Computer Products (America) Inc. Software Solutions Division e-mail: [email protected]