Add generic MMIO instruction deconding to be used in SEV and TDX
cover Add generic MMIO instruction deconding to be used in SEV and TDX
Both AMD SEV and Intel TDX has to decode MMIO instruction to be able to handle MMIO.
Extract insn_decode_mmio() from SEV code. TDX will also use this helper.
Kirill A. Shutemov (3):
x86/insn-eval: Introduce insn_get_modrm_reg_ptr() x86/insn-eval: Introduce insn_decode_mmio() x86/sev-es: Use insn_decode_mmio() for MMIO implementation
arch/x86/include/asm/insn-eval.h | 13 +++ arch/x86/kernel/sev.c | 171 ++++++++----------------------- arch/x86/lib/insn-eval.c | 102 ++++++++++++++++++ 3 files changed, 155 insertions(+), 131 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/3 x86/insn-eval: Introduce insn_get_modrm_reg_ptr()
The helper returns a pointer to the register indicated by ModRM byte.
It's going to replace vc_insn_get_reg() in the SEV MMIO implementation. TDX MMIO implementation will also use it.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -19,6 +19,7 @@ bool insn_has_rep_prefix(struct insn *insn);void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs); +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs); int insn_fetch_from_user(struct pt_regs *regs,
arch/x86/lib/insn-eval.c +20 -0
@@ -850,6 +850,26 @@ int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs)return get_reg_offset (insn, regs,REG_TYPE_REG ); } +/** +* insn_get_modrm_reg_ptr() - Obtain register pointer based on ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the reg part of the ModRM byte. +* The register is obtained as a pointer within pt_regs. +*/ +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs) +{ +int offset; + + offset =insn_get_modrm_reg_off (insn, regs); +if (offset <0 ) +return NULL ; +return (void *)regs + offset; +} +/** * get_seg_base_limit() - obtain base address and limit of a segment * @insn: Instruction. Must be valid.
Signature · 1 line
2.32.0
Re: [PATCH 1/3] x86/insn-eval: Introduce insn_get_modrm_reg_ptr()
arch/x86/lib/insn-eval.c int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs) · line 863
Quoted 11 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +11
-0+/** +* insn_get_modrm_reg_ptr() - Obtain register pointer based on ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the reg part of the ModRM byte. +* The register is obtained as a pointer within pt_regs. +*/ +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs)
Doesn't that return type want to be 'unsigned long *'?
Quoted 15 lines
arch/x86/lib/insn-eval.c +9
-0+{ +int offset; + + offset =insn_get_modrm_reg_off (insn, regs); +if (offset <0 ) +return NULL ; +return (void *)regs + offset; +} +/** * get_seg_base_limit() - obtain base address and limit of a segment * @insn: Instruction. Must be valid. Signature · 1 line
2.32.0
Re: [PATCH 1/3] x86/insn-eval: Introduce insn_get_modrm_reg_ptr()
Quoted 14 lines · Peter Zijlstra
arch/x86/lib/insn-eval.c int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs) · line 863
Quoted 11 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +11
-0+/** +* insn_get_modrm_reg_ptr() - Obtain register pointer based on ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the reg part of the ModRM byte. +* The register is obtained as a pointer within pt_regs. +*/ +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs)Doesn't that return type want to be 'unsigned long *'?
Right, will fix.
Signature · 1 line
Kirill A. Shutemov
PATCH 2/3 x86/insn-eval: Introduce insn_decode_mmio()
In reply to Kirill A. ShutemovIn preparation for sharing MMIO instruction decode between SEV-ES and TDX, factor out the common decode into a new insn_decode_mmio() helper.
For regular virtual machine, MMIO is handled by the VMM and KVM emulates instructions that caused MMIO. But, this model doesn't work for a secure VMs (like SEV or TDX) as VMM doesn't have access to the guest memory and register state. So, for TDX or SEV VMM needs assistance in handling MMIO. It induces exception in the guest. Guest has to decode the instruction and handle it on its own.
The code is based on the current SEV MMIO implementation.
--- arch/x86/include/asm/insn-eval.h | 12 +++++ arch/x86/lib/insn-eval.c | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+)
arch/x86/include/asm/insn-eval.h +12 -0
@@ -29,4 +29,16 @@ int insn_fetch_from_user_inatomic(struct pt_regs *regs,bool insn_decode_from_regs (struct insn *insn,struct pt_regs *regs,unsigned char buf[MAX_INSN_SIZE ],int buf_size); +enum mmio_type { +MMIO_DECODE_FAILED , +MMIO_WRITE , +MMIO_WRITE_IMM , +MMIO_READ , +MMIO_READ_ZERO_EXTEND , +MMIO_READ_SIGN_EXTEND , +MMIO_MOVS , +}; + +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes); +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +82 -0
85 lines not on this page · raw message
Signature · 1 line
2.32.0
Re: [PATCH 2/3] x86/insn-eval: Introduce insn_decode_mmio()
Quoted 18 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +10
-0@@ -1559,3 +1559,85 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,return true; } + +/** +* insn_decode_mmio() - Decode a MMIO instruction +* @insn: Structure to store decoded instruction +* @bytes: Returns size of memory operand +* +* Decodes instruction that used for Memory-mapped I/O. +* +* Returns: +*
arch/x86/lib/insn-eval.c bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs, · line 1581
Quoted 10 lines
arch/x86/lib/insn-eval.c +10
-0+* Type of the instruction. Size of the memory operand is stored in +* @bytes. If decode failed, MMIO_DECODE_FAILED returned. +*/ +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes) +{ +int type =MMIO_DECODE_FAILED ; + + *bytes =0 ; + +insn_get_opcode (insn);
insn_get_opcode() can fail. Either you assume it's already called and don't call it, or you can't assume anything and get to do error handling.
Quoted 1 line
arch/x86/lib/insn-eval.c +1
-0+switch (insn->opcode.bytes[0 ]) {
...
Quoted 7 lines
arch/x86/lib/insn-eval.c +4
-0+ } + +return type; +}Signature · 1 line
2.32.0
Re: [PATCH 2/3] x86/insn-eval: Introduce insn_decode_mmio()
Quoted 26 lines · Peter Zijlstra
Quoted 24 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +16
-0@@ -1559,3 +1559,85 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,return true; } + +/** +* insn_decode_mmio() - Decode a MMIO instruction +* @insn: Structure to store decoded instruction +* @bytes: Returns size of memory operand +* +* Decodes instruction that used for Memory-mapped I/O. +* +* Returns: +* +* Type of the instruction. Size of the memory operand is stored in +* @bytes. If decode failed, MMIO_DECODE_FAILED returned. +*/ +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes) +{ +int type =MMIO_DECODE_FAILED ;
Quoted 8 lines
arch/x86/lib/insn-eval.c bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs, · line 1581
Quoted 4 lines
arch/x86/lib/insn-eval.c +4
-0+ + *bytes =0 ; + +insn_get_opcode (insn);
insn_get_opcode()can fail. Either you assume it's already called and don't call it, or you can't assume anything and get to do error handling.
Fair enough. I will return MMIO_DECODE_FAILED if insn_get_opcode() fails.
BTW, looks like is_string_insn() suffers from the same issue. Not sure how to fix it though.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCH 2/3] x86/insn-eval: Introduce insn_decode_mmio()
Quoted 36 lines · Kirill A. Shutemov
Quoted 26 lines · Peter Zijlstra
Quoted 24 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +16
-0@@ -1559,3 +1559,85 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,return true; } + +/** +* insn_decode_mmio() - Decode a MMIO instruction +* @insn: Structure to store decoded instruction +* @bytes: Returns size of memory operand +* +* Decodes instruction that used for Memory-mapped I/O. +* +* Returns: +* +* Type of the instruction. Size of the memory operand is stored in +* @bytes. If decode failed, MMIO_DECODE_FAILED returned. +*/ +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes) +{ +int type =MMIO_DECODE_FAILED ;Quoted 8 lines
arch/x86/lib/insn-eval.c bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs, · line 1581
Quoted 4 lines
arch/x86/lib/insn-eval.c +4
-0+ + *bytes =0 ; + +insn_get_opcode (insn);
insn_get_opcode()can fail. Either you assume it's already called and don't call it, or you can't assume anything and get to do error handling.
Quoted 4 lines
Fair enough. I will return
MMIO_DECODE_FAILEDifinsn_get_opcode()fails.BTW, looks like
is_string_insn()suffers from the same issue. Not sure how to fix it though.
AFAICT all callers of insn_get_addr_ref() (which is what is_string_insn() seems to be part of) do a insn_decode_*() call with error checking before.
So it looks like that insn_get_opcode() in there is superfluous. probably same for insn_has_rep_prefix() / get_seg_reg_override_idx() / get_eff_addr_modrm_*(). That all wants cleaning up.
The esaiest way is probably to push those things up the callchains into the !static function and have it fail early there.
Re: [PATCH 2/3] x86/insn-eval: Introduce insn_decode_mmio()
Quoted 46 lines · Peter Zijlstra
Quoted 36 lines · Kirill A. Shutemov
Quoted 26 lines · Peter Zijlstra
Quoted 24 lines · Kirill A. Shutemov
arch/x86/lib/insn-eval.c +16
-0@@ -1559,3 +1559,85 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,return true; } + +/** +* insn_decode_mmio() - Decode a MMIO instruction +* @insn: Structure to store decoded instruction +* @bytes: Returns size of memory operand +* +* Decodes instruction that used for Memory-mapped I/O. +* +* Returns: +* +* Type of the instruction. Size of the memory operand is stored in +* @bytes. If decode failed, MMIO_DECODE_FAILED returned. +*/ +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes) +{ +int type =MMIO_DECODE_FAILED ;Quoted 8 lines
arch/x86/lib/insn-eval.c bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs, · line 1581
Quoted 4 lines
arch/x86/lib/insn-eval.c +4
-0+ + *bytes =0 ; + +insn_get_opcode (insn);
insn_get_opcode()can fail. Either you assume it's already called and don't call it, or you can't assume anything and get to do error handling.Quoted 4 lines
Fair enough. I will return
MMIO_DECODE_FAILEDifinsn_get_opcode()fails.BTW, looks like
is_string_insn()suffers from the same issue. Not sure how to fix it though.AFAICT all callers of
insn_get_addr_ref()(which is whatis_string_insn()seems to be part of) do a insn_decode_*() call with error checking before.
Quoted 6 lines
So it looks like that
insn_get_opcode()in there is superfluous. probably same forinsn_has_rep_prefix()/get_seg_reg_override_idx()/ get_eff_addr_modrm_*(). That all wants cleaning up.The esaiest way is probably to push those things up the callchains into the !static function and have it fail early there.
Looks like insn_get_addr_ref() is the only non-static callsite:
is_string_insn
check_seg_overrides
resolve_seg_reg
get_seg_base_limit
get_addr_ref_XX
insn_get_addr_ref
resolve_default_seg
resolve_seg_reg (as above)
What about this:
From 576a390de4759f591709c9961b5d7add02c814f0 Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <[email protected]>
Date: Sat, 23 Oct 2021 19:26:07 +0300
Subject: [PATCH] x86/insn-eval: Handle insn_get_opcode() failure
is_string_insn() calls insn_get_opcode() that can fail, but does not handle the failure.
is_string_insn() interface does not allow to communicate an error to the caller.
Push insn_get_opcode() to the only non-static user of is_string_insn() and fail it early if insn_get_opcode() fails.
--- arch/x86/lib/insn-eval.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/lib/insn-eval.c +3 -2
@@ -37,8 +37,6 @@ enum reg_type {*/ static bool is_string_insn (struct insn *insn) { -insn_get_opcode (insn); -/* All string instructions have a 1-byte opcode. */ if (insn->opcode.nbytes !=1 )return false;
@@ -1425,6 +1423,9 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)if (!insn || !regs)return (void __user *)-1L ; +if (insn_get_opcode (insn)) +return (void __user *)-1L ; +switch (insn->addr_bytes) {case 2 :return get_addr_ref_16 (insn, regs);
Signature · 1 line
Kirill A. Shutemov
PATCH 3/3 x86/sev-es: Use insn_decode_mmio() for MMIO implementation
In reply to Kirill A. ShutemovSwitch SEV implementation to insn_decode_mmio(). The helper is going to be used by TDX too.
No functional changes. It is only build-tested.
--- arch/x86/kernel/sev.c | 171 ++++++++++-------------------------------- 1 file changed, 40 insertions(+), 131 deletions(-)
arch/x86/kernel/sev.c +40 -131
@@ -807,22 +807,6 @@ static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)do_early_exception (ctxt->regs, trapnr); } -static long *vc_insn_get_reg (struct es_em_ctxt *ctxt) -{ -long *reg_array; -int offset; - - reg_array = (long *)ctxt->regs; - offset =insn_get_modrm_reg_off (&ctxt->insn, ctxt->regs); - -if (offset <0 ) -return NULL ; - - offset /=sizeof (long ); - -return reg_array + offset; -} -static long *vc_insn_get_rm (struct es_em_ctxt *ctxt) {long *reg_array;
@@ -870,76 +854,6 @@ static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,return sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, exit_info_2); } -static enum es_result vc_handle_mmio_twobyte_ops (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) -{ -struct insn *insn = &ctxt->insn; -unsigned int bytes =0 ; -enum es_result ret; -int sign_byte; -long *reg_data; - -switch (insn->opcode.bytes[1 ]) { -/* MMIO Read w/ zero-extension */ -case 0xb6 : - bytes =1 ; - fallthrough; -case 0xb7 : -if (!bytes) - bytes =2 ; - - ret =vc_do_mmio (ghcb, ctxt, bytes, true); -if (ret) -break ; - -/* Zero extend based on operand size */ - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; - -memset (reg_data,0 , insn->opnd_bytes); - -memcpy (reg_data, ghcb->shared_buffer, bytes); -break ; - -/* MMIO Read w/ sign-extension */ -case 0xbe : - bytes =1 ; - fallthrough; -case 0xbf : -if (!bytes) - bytes =2 ; - - ret =vc_do_mmio (ghcb, ctxt, bytes, true); -if (ret) -break ; - -/* Sign extend based on operand size */ - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; - -if (bytes ==1 ) { -u8 *val = (u8 *)ghcb->shared_buffer; - - sign_byte = (*val &0x80 ) ?0xff :0x00 ; - }else { -u16 *val = (u16 *)ghcb->shared_buffer; - - sign_byte = (*val &0x8000 ) ?0xff :0x00 ; - } -memset (reg_data, sign_byte, insn->opnd_bytes); - -memcpy (reg_data, ghcb->shared_buffer, bytes); -break ; - -default : - ret =ES_UNSUPPORTED ; - } - -return ret; -} -/* * The MOVS instruction has two memory operands, which raises the * problem that it is not known whether the access to the source or the
@@ -1007,83 +921,78 @@ static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,return ES_RETRY ; } -static enum es_result vc_handle_mmio (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) +static enum es_result vc_handle_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {struct insn *insn = &ctxt->insn;unsigned int bytes =0 ; +enum mmio_type mmio;enum es_result ret; +u8 sign_byte;long *reg_data; -switch (insn->opcode.bytes[0 ]) { -/* MMIO Write */ -case 0x88 : - bytes =1 ; - fallthrough; -case 0x89 : -if (!bytes) - bytes = insn->opnd_bytes; + mmio =insn_decode_mmio (insn, &bytes); +if (mmio ==MMIO_DECODE_FAILED ) +return ES_DECODE_FAILED ; - reg_data =vc_insn_get_reg (ctxt); +if (mmio !=MMIO_WRITE_IMM && mmio !=MMIO_MOVS ) { + reg_data =insn_get_modrm_reg_ptr (insn, ctxt->regs);if (!reg_data)return ES_DECODE_FAILED ; + } +switch (mmio) { +case MMIO_WRITE :memcpy (ghcb->shared_buffer, reg_data, bytes); - ret =vc_do_mmio (ghcb, ctxt, bytes, false);break ; - -case 0xc6 : - bytes =1 ; - fallthrough; -case 0xc7 : -if (!bytes) - bytes = insn->opnd_bytes; - +case MMIO_WRITE_IMM :memcpy (ghcb->shared_buffer, insn->immediate1.bytes, bytes); - ret =vc_do_mmio (ghcb, ctxt, bytes, false);break ; - -/* MMIO Read */ -case 0x8a : - bytes =1 ; - fallthrough; -case 0x8b : -if (!bytes) - bytes = insn->opnd_bytes; - +case MMIO_READ : ret =vc_do_mmio (ghcb, ctxt, bytes, true);if (ret)break ; - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; -/* Zero-extend for 32-bit operation */ if (bytes ==4 ) *reg_data =0 ;memcpy (reg_data, ghcb->shared_buffer, bytes);break ; +case MMIO_READ_ZERO_EXTEND : + ret =vc_do_mmio (ghcb, ctxt, bytes, true); +if (ret) +break ; + +memset (reg_data,0 , insn->opnd_bytes); +memcpy (reg_data, ghcb->shared_buffer, bytes); +break ; +case MMIO_READ_SIGN_EXTEND : + ret =vc_do_mmio (ghcb, ctxt, bytes, true); +if (ret) +break ; -/* MOVS instruction */ -case 0xa4 : - bytes =1 ; - fallthrough; -case 0xa5 : -if (!bytes) - bytes = insn->opnd_bytes; +if (bytes ==1 ) { +u8 *val = (u8 *)ghcb->shared_buffer; - ret =vc_handle_mmio_movs (ctxt, bytes); + sign_byte = (*val &0x80 ) ?0xff :0x00 ; + }else { +u16 *val = (u16 *)ghcb->shared_buffer; + + sign_byte = (*val &0x8000 ) ?0xff :0x00 ; + } + +/* Sign extend based on operand size */ +memset (reg_data, sign_byte, insn->opnd_bytes); +memcpy (reg_data, ghcb->shared_buffer, bytes);break ; -/* Two-Byte Opcodes */ -case 0x0f : - ret =vc_handle_mmio_twobyte_ops (ghcb, ctxt); +case MMIO_MOVS : + ret =vc_handle_mmio_movs (ctxt, bytes);break ;default : ret =ES_UNSUPPORTED ; +break ; }return ret;
Signature · 1 line
2.32.0