xen/arm64: io: Decode ldr/str post-indexing instruction
v6 cover xen/arm64: io: Decode ldr/str post-indexing instruction
Hi All,
I have split "[XEN v5] xen/arm64: io: Decode ldr/str post-indexing instructions" into the following three patches :-
Ayan Kumar Halder (3):
xen/arm64: Decode ldr/str post increment operations xen/arm64: io: Support instructions (for which ISS is not valid) on
emulated MMIO region using MMIO handler
xen/arm64: io: Support instructions (for which ISS is not valid) on
emulated MMIO region using ioreq handler
The third patch introduces quite a bit of complexity. So, I decided to keep it separate to ease the review and help me to understand better.
I haven't tested the patches. I am sending it out to get a feedback to see if it is going in the correct direction.
xen/arch/arm/arm32/traps.c | 6 +++ xen/arch/arm/arm64/traps.c | 45 ++++++++++++++++ xen/arch/arm/decode.c | 81 +++++++++++++++++++++++++++- xen/arch/arm/decode.h | 48 ++++++++++++++--- xen/arch/arm/include/asm/mmio.h | 17 ++++++ xen/arch/arm/include/asm/traps.h | 2 + xen/arch/arm/io.c | 92 ++++++++++++++++++++++++-------- xen/arch/arm/ioreq.c | 13 +++-- xen/arch/arm/traps.c | 56 ++++++++++++++++++- xen/include/public/hvm/ioreq.h | 19 +++---- 10 files changed, 333 insertions(+), 46 deletions(-)
Changelog :-
v2 - 1. Updated the rn register after reading from it. (Pointed by Julien,
Stefano)
2. Used a union to represent the instruction opcode (Suggestd by Bertrand)
3. Fixed coding style issues (Pointed by Julien)
4. In the previous patch, I was updating dabt->sign based on the signedness of imm9. This was incorrect. As mentioned in ARMv8 ARM DDI 0487G.b, Page 3221, SSE indicates the signedness of the data item loaded. In our case, the data item loaded is always unsigned.
v3- 1. Handled all the variants of ldr/str (ie 64, 32, 16, 8 bit variants).
Thus, I have removed the check for "instr->code.opc == 0" (Suggested by Andre)
2. Handled the scenario when rn = SP, rt = XZR (Suggested by Jan, Andre)
3. Added restriction for "rt != rn" (Suggested by Andre)
4. Moved union ldr_str_instr_class {} to decode.h. This is the header included by io.c and decode.c (where the union is referred). (Suggested by Jan)
5. Indentation and typo fixes (Suggested by Jan)
v4- 1. Fixed the patch as per Stefano's comments on v3. They are as follows :-
1.1 Use macros to determine the fixed values in the instruction opcode 1.2 Checked if instr != NULL
1.3 Changed some data types and added #define ARM_64 for AArch64 specific
code
1.4 Moved post_increment_register() to decode.c so that the decoding
logic is confined to a single file.
1.5 Moved some checks from post_increment_register() to
decode_loadstore_postindexing()
1.6 Removed a duplicate check
2. Updated the commit message as per Andre's comments.
3. Changed the names of a label and some comments. *32bit* was erroneously mentioned in a label and comments in decode_loadstore_postindexing() although the function handled all variants of ldr/str post indexing.
v5- 1. Renamed decode_loadstore_postindexing() to decode_arm64(). The reason
being this will be extended in future to support more instructions for which hsr_badt.isv = 0
2. Introduce a function try_decode_instruction_invalid_iss() to determine if the instruction needs to be decoded before invoking decode_instruction().
It checks :-
2.1 dabt->s1ptw - Returns IO_UNHANDLED
2.2 dabt->cache - Returns IO_IGNORED. (new enum instroduced to let the
caller know that the instruction needs to be ignored by Xen. Thus the caller needs to increment the PC and return to the guest.
3. Invoked try_decode_instruction_invalid_iss() from the following 2 places :- 3.a - try_handle_mmio() - When we have determined that there is a valid mmio handler.
3.b - try_fwd_ioserv()
When ioserver completes the io request, the acknowledgement is sent via handle_ioserv(). Here, we need to increment the register. As there is no common data shared between try_fwd_ioserv() and handle_ioserv(), we need to decode the instruction again in handle_ioserv() to determine rn, imm9.
(NOTE to Reviewers) - This does not feel correct. However, I could not think of a better approach. Please provide your inputs.
4. Augumented struct hsr_dabt{} with struct hsr_dabt_instr_details{} to hold rn and imm9. This is passed to post_increment_register() to update rn.
5. Other style changes as suggested in v4.
v6 - 1. Split the patch into three parts.
Signature · 1 line
2.17.1
PATCH v6 1/3 xen/arm64: Decode ldr/str post increment operations
At the moment, Xen does not decode any of the arm64 instructions. This means that hsr_dabt.isv = 0, Xen cannot handle those instructions. This will lead to Xen abort the guests (from which those instructions originated).
With this patch, Xen is able to decode ldr/str post indexing instructions. These are a subset of instructions for which hsr_dabt.isv = 0
The following instructions are now supported by Xen :-
1. ldr x2, [x1], #8 2. ldr w2, [x1], #-4 3. ldr x2, [x1], #-8 4. ldr w2, [x1], #4 5. ldrh w2, [x1], #2 6. ldrb w2, [x1], #1 7. str x2, [x1], #8 8. str w2, [x1], #-4 9. strh w2, [x1], #2 10. strb w2, [x1], #1
In the subsequent patches, decode_arm64() will get invoked when hsr_dabt.isv=0.
---
Changelog :-
v2..v5 - Mentioned in the cover letter.
v6 - 1. Fixed the code style issues as mentioned in v5.
xen/arch/arm/decode.c | 80 ++++++++++++++++++++++++++++++++- xen/arch/arm/decode.h | 49 +++++++++++++++++--- xen/arch/arm/include/asm/mmio.h | 4 ++ xen/arch/arm/io.c | 2 +- 4 files changed, 125 insertions(+), 10 deletions(-)
xen/arch/arm/decode.c +78 -2
@@ -23,6 +23,7 @@#include <xen/types.h> #include <asm/current.h> +#include <asm/mmio.h> #include "decode.h"
@@ -84,6 +85,78 @@ bad_thumb2:return 1 ; } +static int decode_arm64 (register_t pc,mmio_info_t *info) +{ +union instr opcode = {0 }; +struct hsr_dabt *dabt = &info->dabt; +struct instr_details *dabt_instr = &info->dabt_instr; + +if (raw_copy_from_guest (&opcode.value, (void *__user )pc,sizeof (opcode)) ) + { +gprintk (XENLOG_ERR ,"Could not copy the instruction from PC\n" ); + goto bad_loadstore; + } + +/* +* Refer Arm v8 ARM DDI 0487G.b, Page - C6-1107 +* "Shared decode for all encodings" (under ldr immediate) +* If n == t && n != 31, then the return value is implementation defined +* (can be WBSUPPRESS, UNKNOWN, UNDEFINED or NOP). Thus, we do not support +* this. This holds true for ldrb/ldrh immediate as well. +* +* Also refer, Page - C6-1384, the above described behaviour is same for +* str immediate. This holds true for strb/strh immediate as well +*/ +if ( (opcode.ldr_str.rn == opcode.ldr_str.rt) && (opcode.ldr_str.rn !=31 ) ) + { +gprintk (XENLOG_ERR ,"Rn should not be equal to Rt except for r31\n" ); + goto bad_loadstore; + } + +/* First, let's check for the fixed values */ +if ( (opcode.value &POST_INDEX_FIXED_MASK ) !=POST_INDEX_FIXED_VALUE ) + { +gprintk (XENLOG_ERR , +"Decoding instruction 0x%x is not supported" , opcode.value); + goto bad_loadstore; + } + +if ( opcode.ldr_str.v !=0 ) + { +gprintk (XENLOG_ERR , +"ldr/str post indexing for vector types are not supported\n" ); + goto bad_loadstore; + } + +/* Check for STR (immediate) */ +if ( opcode.ldr_str.opc ==0 ) + dabt->write =1 ; +/* Check for LDR (immediate) */ +else if ( opcode.ldr_str.opc ==1 ) + dabt->write =0 ; +else + { +gprintk (XENLOG_ERR , +"Decoding ldr/str post indexing is not supported for this variant\n" ); + goto bad_loadstore; + } + +gprintk (XENLOG_INFO , +"opcode->ldr_str.rt = 0x%x, opcode->ldr_str.size = 0x%x, opcode->ldr_str.imm9 = %d\n" , + opcode.ldr_str.rt, opcode.ldr_str.size, opcode.ldr_str.imm9); + +update_dabt (dabt, opcode.ldr_str.rt, opcode.ldr_str.size, false); + + dabt_instr->rn = opcode.ldr_str.rn; + dabt_instr->imm9 = opcode.ldr_str.imm9; + +return 0 ; + + bad_loadstore: +gprintk (XENLOG_ERR ,"unhandled Arm instruction 0x%x\n" , opcode.value); +return 1 ; +} +static int decode_thumb (register_t pc,struct hsr_dabt *dabt) {uint16_t instr;
@@ -150,10 +223,13 @@ bad_thumb:return 1 ; } -int decode_instruction (const struct cpu_user_regs *regs,struct hsr_dabt *dabt) +int decode_instruction (const struct cpu_user_regs *regs,mmio_info_t *info) {if (is_32bit_domain (current->domain) && regs->cpsr &PSR_THUMB ) -return decode_thumb (regs->pc, dabt); +return decode_thumb (regs->pc, &info->dabt); + +if ( !psr_mode_is_32bit (regs) ) +return decode_arm64 (regs->pc, info);/* TODO: Handle ARM instruction */ gprintk (XENLOG_ERR ,"unhandled ARM instruction\n" );
xen/arch/arm/decode.h +42 -7
61 lines not on this page · raw message
xen/arch/arm/include/asm/mmio.h +4 -0
@@ -29,6 +29,10 @@typedef struct {struct hsr_dabt dabt; +struct instr_details { +unsigned long rn:5 ; +signed int imm9:9 ; + } dabt_instr;paddr_t gpa; }mmio_info_t ;
xen/arch/arm/io.c +1 -1
@@ -134,7 +134,7 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, {int rc; - rc =decode_instruction (regs, &info.dabt); + rc =decode_instruction (regs, &info);if ( rc ) {gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" );
Signature · 1 line
2.17.1
PATCH v6 2/3 xen/arm64: io: Support instructions (for which ISS is not valid) on emulated MMIO region using MMIO handler
In reply to Ayan Kumar HalderFor instructions on MMIO regions emulated by Xen, Xen reads the remaining bits of the HSR. It determines if the instruction is to be ignored, retried or decoded. If it gets an error while decoding the instruction, then it sends an abort to the guest.
If the instruction is valid or successfully decoded, Xen tries to execute the instruction for the emulated MMIO region. If the instruction was successfully executed, then Xen determines if the instruction needs further processing. For eg:- In case of ldr/str post indexing on arm64, the rn register needs to be updated.
---
Changelog :-
v2..v5 - Mentioned in the cover letter.
v6 - 1. Mantained the decoding state of the instruction. This is used by the caller to either abort the guest or retry or ignore or perform read/write on the mmio region.
2. try_decode() invokes decoding for both aarch64 and thumb state. (Previously it used to invoke decoding only for aarch64 state). Thus, it handles all the checking of the registers before invoking any decoding of instruction. try_decode_instruction_invalid_iss() has thus been removed.
xen/arch/arm/arm32/traps.c | 6 ++ xen/arch/arm/arm64/traps.c | 41 ++++++++++++ xen/arch/arm/decode.h | 12 +++- xen/arch/arm/include/asm/traps.h | 2 + xen/arch/arm/io.c | 108 +++++++++++++++++++++++++------ 5 files changed, 148 insertions(+), 21 deletions(-)
xen/arch/arm/arm32/traps.c +6 -0
@@ -21,6 +21,7 @@#include <public/xen.h> +#include <asm/mmio.h> #include <asm/processor.h> #include <asm/traps.h>
@@ -82,6 +83,11 @@ void do_trap_data_abort(struct cpu_user_regs *regs)do_unexpected_trap ("Data Abort" , regs); } +void post_increment_register (const struct instr_details *instr) +{ +ASSERT_UNREACHABLE (); +} +/* * Local variables: * mode: C
xen/arch/arm/arm64/traps.c +41 -0
56 lines not on this page · raw message
xen/arch/arm/decode.h +11 -1
@@ -52,7 +52,17 @@ union instr {#define POST_INDEX_FIXED_MASK 0x3B200C00#define POST_INDEX_FIXED_VALUE 0x38000400 -/* Decode an instruction from pc +enum instr_decode_state +{ +INSTR_ERROR ,/* Error encountered while decoding the instruction */ +INSTR_VALID ,/* ISS is valid, so there is no need to decode */ +INSTR_SUCCESS ,/* Instruction is decoded successfully */ +INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ +INSTR_RETRY /* Instruction is to be retried */ +}; + +/* +* Decode an instruction from pc * /!\ This function is intended to decode an instruction. It considers that the * instruction is valid. *
xen/arch/arm/include/asm/traps.h +2 -0
@@ -109,6 +109,8 @@ static inline register_t sign_extend(const struct hsr_dabt dabt, register_t r) return r; } +void post_increment_register (const struct instr_details *instr); +#endif /* __ASM_ARM_TRAPS__ */ /* * Local variables:
xen/arch/arm/io.c +88 -20
139 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [XEN v6 2/3] xen/arm64: io: Support instructions (for which ISS is not valid) on emulated MMIO region using MMIO handler
Quoted 172 lines · Ayan Kumar Halder
For instructions on MMIO regions emulated by Xen, Xen reads the remaining bits of the HSR. It determines if the instruction is to be ignored, retried or decoded. If it gets an error while decoding the instruction, then it sends an abort to the guest.
If the instruction is valid or successfully decoded, Xen tries to execute the instruction for the emulated MMIO region. If the instruction was successfully executed, then Xen determines if the instruction needs further processing. For eg:- In case of ldr/str post indexing on arm64, the rn register needs to be updated.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changelog :-
v2..v5 - Mentioned in the cover letter.
v6 - 1. Mantained the decoding state of the instruction. This is used by the caller to either abort the guest or retry or ignore or perform read/write on the mmio region.
2.
try_decode()invokes decoding for both aarch64 and thumb state. (Previously it used to invoke decoding only for aarch64 state). Thus, it handles all the checking of the registers before invoking any decoding of instruction.try_decode_instruction_invalid_iss()has thus been removed.xen/arch/arm/arm32/traps.c | 6 ++ xen/arch/arm/arm64/traps.c | 41 ++++++++++++ xen/arch/arm/decode.h | 12 +++- xen/arch/arm/include/asm/traps.h | 2 + xen/arch/arm/io.c | 108 +++++++++++++++++++++++++------ 5 files changed, 148 insertions(+), 21 deletions(-)xen/arch/arm/arm32/traps.c +6
-0@@ -21,6 +21,7 @@#include <public/xen.h> +#include <asm/mmio.h> #include <asm/processor.h> #include <asm/traps.h> @@ -82,6 +83,11 @@ void do_trap_data_abort(struct cpu_user_regs *regs)do_unexpected_trap ("Data Abort" , regs); } +void post_increment_register (const struct instr_details *instr) +{ +ASSERT_UNREACHABLE (); +} +/* * Local variables: * mode: C xen/arch/arm/arm64/traps.c +41
-0@@ -18,9 +18,12 @@#include <xen/lib.h> +#include <asm/current.h> #include <asm/hsr.h> +#include <asm/mmio.h> #include <asm/system.h> #include <asm/processor.h> +#include <asm/regs.h> #include <public/xen.h> @@ -44,6 +47,44 @@ void do_bad_mode(struct cpu_user_regs *regs, int reason)panic ("bad mode\n" ); } +void post_increment_register (const struct instr_details *instr) +{ +struct cpu_user_regs *regs =guest_cpu_user_regs (); +register_t val; + +/* +* Handle when rn = SP +* Refer ArmV8 ARM DDI 0487G.b, Page - D1-2463 "Stack pointer register selection" +* t = SP_EL0 +* h = SP_ELx +* and M[3:0] (Page - C5-474 "When exception taken from AArch64 state:") +*/ +if (instr->rn ==31 ) + { +if ( (regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1h ) + val = regs->sp_el1; +else if ( ((regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1t) || + ((regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL0t) ) + val = regs->sp_el0; +else +ASSERT_UNREACHABLE (); + } +else + val =get_user_reg (regs, instr->rn); + + val += instr->imm9; + +if ( instr->rn ==31 ) + { +if ( (regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1h ) + regs->sp_el1 = val; +else + regs->sp_el0 = val; + } +else +set_user_reg (regs, instr->rn, val); +} +/* * Local variables: * mode: C xen/arch/arm/decode.h +11
-1@@ -52,7 +52,17 @@ union instr {#define POST_INDEX_FIXED_MASK 0x3B200C00#define POST_INDEX_FIXED_VALUE 0x38000400 -/* Decode an instruction from pc +enum instr_decode_state +{ +INSTR_ERROR ,/* Error encountered while decoding the instruction */ +INSTR_VALID ,/* ISS is valid, so there is no need to decode */ +INSTR_SUCCESS ,/* Instruction is decoded successfully */ +INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ +INSTR_RETRY /* Instruction is to be retried */ +}; + +/* +* Decode an instruction from pc * /!\ This function is intended to decode an instruction. It considers that the * instruction is valid. * xen/arch/arm/include/asm/traps.h +2
-0@@ -109,6 +109,8 @@ static inline register_t sign_extend(const struct hsr_dabt dabt, register_t r) return r; } +void post_increment_register (const struct instr_details *instr); +#endif /* __ASM_ARM_TRAPS__ */ /* * Local variables: xen/arch/arm/io.c +8
-0@@ -95,6 +95,59 @@ static const struct mmio_handler *find_mmio_handler(struct domain *d,return handler; } +enum instr_decode_state try_decode_instruction (const struct cpu_user_regs *regs, +mmio_info_t *info) +{ +int rc; + +/* +* Erratum 766422: Thumb store translation fault to Hypervisor may +* not have correct HSR Rt value.
xen/arch/arm/io.c static const struct mmio_handler *find_mmio_handler(struct domain *d, · line 115
Quoted 10 lines
xen/arch/arm/io.c +10
-0+*/ +if (check_workaround_766422 () && (regs->cpsr &PSR_THUMB ) && + info->dabt.write ) + { + rc =decode_instruction (regs, info); +if ( rc ) + { +gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" ); +return INSTR_ERROR ; + }
It looks like we want a "return" here? But it should work either way because it should return with the if ( info->dabt.valid ) check right after anyway.
xen/arch/arm/io.c static const struct mmio_handler *find_mmio_handler(struct domain *d, · line 120
Quoted 5 lines
xen/arch/arm/io.c +5
-0+ } + +/* If ISS is valid, then no need to decode the instruction any further */ +if (info->dabt.valid) +return INSTR_VALID ;
code style
Quoted 50 lines
xen/arch/arm/io.c +31
-2+/* +* Xen should not decode the instruction when it was trapped due to +* translation fault. +*/ +if ( info->dabt.s1ptw ) +return INSTR_RETRY ; + +/* +* If the fault occurred due to cache maintenance or address translation +* instructions, then Xen needs to ignore these instructions. +*/ +if ( info->dabt.cache ) +return INSTR_IGNORE ; + +/* +* Armv8 processor does not provide a valid syndrome for decoding some +* instructions. So in order to process these instructions, Xen must +* decode them. +*/ + rc =decode_instruction (regs, info); +if ( rc ) + { +gprintk (XENLOG_ERR ,"Unable to decode instruction\n" ); +return INSTR_ERROR ; + } +else +return INSTR_SUCCESS ; +} +enum io_state try_handle_mmio (struct cpu_user_regs *regs,const union hsr hsr,paddr_t gpa)@@ -106,14 +159,14 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, .gpa = gpa, .dabt = dabt }; +int rc; +enum instr_decode_state state;ASSERT (hsr.ec ==HSR_EC_DATA_ABORT_LOWER_EL ); handler =find_mmio_handler (v->domain, info.gpa);if ( !handler ) { -int rc; - rc =try_fwd_ioserv (regs, v, &info);if ( rc ==IO_HANDLED )return handle_ioserv (regs, v);
xen/arch/arm/io.c enum io_state try_handle_mmio(struct cpu_user_regs *regs, · line 177
Quoted 6 lines
xen/arch/arm/io.c +1
-2@@ -121,31 +174,46 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs,return rc; } -/* All the instructions used on emulated MMIO region should be valid */ -if ( !dabt.valid ) + state =try_decode_instruction (regs, &info);
We still have the issue that try_fwd_ioserv (called above) doesn't work properly if !dabt.valid. I think we need to call try_decode_instruction and do the "state" checks before find_mmio_handler/try_fwd_ioserv.
Quoted 38 lines
xen/arch/arm/io.c +22
-11+/* +* If the instruction was to be ignored by Xen, then it should return to the +* caller which will increment the PC, so that the guest can execute the +* next instruction. +*/ +if ( state ==INSTR_IGNORE ) +return IO_HANDLED ; +/* +* If Xen could not decode the instruction for any reason, then it should +* ask the caller to abort the guest. +*/ +else if ( state ==INSTR_ERROR )return IO_ABORT ; +/* When the instruction needs to be retried by the guest */ +else if ( state ==INSTR_RETRY ) +return IO_UNHANDLED ;/* -* Erratum 766422: Thumb store translation fault to Hypervisor may -* not have correct HSR Rt value. +* At this point, we know that the instruction is either valid or has been +* decoded successfully. Thus, Xen should be allowed to execute the +* instruction on the emulated MMIO region. */ -if (check_workaround_766422 () && (regs->cpsr &PSR_THUMB ) && - dabt.write ) - { -int rc; +if ( info.dabt.write ) + rc =handle_write (handler, v, &info); +else + rc =handle_read (handler, v, &info); - rc =decode_instruction (regs, &info); -if ( rc ) - { -gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" ); -return IO_ABORT ;
xen/arch/arm/io.c enum io_state try_handle_mmio(struct cpu_user_regs *regs, · line 214
Quoted 10 lines
xen/arch/arm/io.c +8
-1- } +/* +* If the instruction was decoded and has executed successfully on the MMIO +* region, then Xen should execute the next part of the instruction. (for eg +* increment the rn if it is a post-indexing instruction. +*/ +if ( (rc ==IO_HANDLED ) && (state ==INSTR_SUCCESS ) ) + { +post_increment_register (&info.dabt_instr); }
We need to call post_increment_register also from arch_ioreq_complete_mmio for the IOREQ case.
Quoted 11 lines
xen/arch/arm/io.c +1
-4-if ( info.dabt.write ) -return handle_write (handler, v, &info); -else -return handle_read (handler, v, &info); +return rc; } void register_mmio_handler(struct domain *d,Signature · 1 line
2.17.1
Re: [XEN v6 2/3] xen/arm64: io: Support instructions (for which ISS is not valid) on emulated MMIO region using MMIO handler
Quoted 76 lines · Stefano Stabellini
Quoted 75 lines · Ayan Kumar Halder
For instructions on MMIO regions emulated by Xen, Xen reads the remaining bits of the HSR. It determines if the instruction is to be ignored, retried or decoded. If it gets an error while decoding the instruction, then it sends an abort to the guest.
If the instruction is valid or successfully decoded, Xen tries to execute the instruction for the emulated MMIO region. If the instruction was successfully executed, then Xen determines if the instruction needs further processing. For eg:- In case of ldr/str post indexing on arm64, the rn register needs to be updated.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changelog :-
v2..v5 - Mentioned in the cover letter.
v6 - 1. Mantained the decoding state of the instruction. This is used by the caller to either abort the guest or retry or ignore or perform read/write on the mmio region.
2.
try_decode()invokes decoding for both aarch64 and thumb state. (Previously it used to invoke decoding only for aarch64 state). Thus, it handles all the checking of the registers before invoking any decoding of instruction.try_decode_instruction_invalid_iss()has thus been removed.xen/arch/arm/arm32/traps.c | 6 ++ xen/arch/arm/arm64/traps.c | 41 ++++++++++++ xen/arch/arm/decode.h | 12 +++- xen/arch/arm/include/asm/traps.h | 2 + xen/arch/arm/io.c | 108 +++++++++++++++++++++++++------ 5 files changed, 148 insertions(+), 21 deletions(-)xen/arch/arm/arm32/traps.c +6
-0@@ -21,6 +21,7 @@#include <public/xen.h> +#include <asm/mmio.h> #include <asm/processor.h> #include <asm/traps.h> @@ -82,6 +83,11 @@ void do_trap_data_abort(struct cpu_user_regs *regs)do_unexpected_trap ("Data Abort" , regs); } +void post_increment_register (const struct instr_details *instr) +{ +ASSERT_UNREACHABLE (); +} +/* * Local variables: * mode: C xen/arch/arm/arm64/traps.c +3
-0@@ -18,9 +18,12 @@#include <xen/lib.h> +#include <asm/current.h> #include <asm/hsr.h> +#include <asm/mmio.h> #include <asm/system.h> #include <asm/processor.h> +#include <asm/regs.h> #include <public/xen.h>
Quoted 8 lines
xen/arch/arm/arm64/traps.c void do_bad_mode(struct cpu_user_regs *regs, int reason) · line 53
Quoted 8 lines
xen/arch/arm/arm64/traps.c +4
-0@@ -44,6 +47,44 @@ void do_bad_mode(struct cpu_user_regs *regs, int reason)panic ("bad mode\n" ); } +void post_increment_register (const struct instr_details *instr) +{ +struct cpu_user_regs *regs =guest_cpu_user_regs (); +register_t val;
val needs to be initialized:
traps.c: In function ‘post_increment_register’:
traps.c:79:9: error: ‘val’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
79 | val += instr->imm9;
| ~~~~^~~~~~~~~~~~~~Quoted 239 lines
Quoted 88 lines
xen/arch/arm/arm64/traps.c +33
-0+/* +* Handle when rn = SP +* Refer ArmV8 ARM DDI 0487G.b, Page - D1-2463 "Stack pointer register selection" +* t = SP_EL0 +* h = SP_ELx +* and M[3:0] (Page - C5-474 "When exception taken from AArch64 state:") +*/ +if (instr->rn ==31 ) + { +if ( (regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1h ) + val = regs->sp_el1; +else if ( ((regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1t) || + ((regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL0t) ) + val = regs->sp_el0; +else +ASSERT_UNREACHABLE (); + } +else + val =get_user_reg (regs, instr->rn); + + val += instr->imm9; + +if ( instr->rn ==31 ) + { +if ( (regs->cpsr &PSR_MODE_MASK ) == PSR_MODE_EL1h ) + regs->sp_el1 = val; +else + regs->sp_el0 = val; + } +else +set_user_reg (regs, instr->rn, val); +} +/* * Local variables: * mode: C xen/arch/arm/decode.h +11
-1@@ -52,7 +52,17 @@ union instr {#define POST_INDEX_FIXED_MASK 0x3B200C00#define POST_INDEX_FIXED_VALUE 0x38000400 -/* Decode an instruction from pc +enum instr_decode_state +{ +INSTR_ERROR ,/* Error encountered while decoding the instruction */ +INSTR_VALID ,/* ISS is valid, so there is no need to decode */ +INSTR_SUCCESS ,/* Instruction is decoded successfully */ +INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ +INSTR_RETRY /* Instruction is to be retried */ +}; + +/* +* Decode an instruction from pc * /!\ This function is intended to decode an instruction. It considers that the * instruction is valid. * xen/arch/arm/include/asm/traps.h +2
-0@@ -109,6 +109,8 @@ static inline register_t sign_extend(const struct hsr_dabt dabt, register_t r) return r; } +void post_increment_register (const struct instr_details *instr); +#endif /* __ASM_ARM_TRAPS__ */ /* * Local variables: xen/arch/arm/io.c +8
-0@@ -95,6 +95,59 @@ static const struct mmio_handler *find_mmio_handler(struct domain *d,return handler; } +enum instr_decode_state try_decode_instruction (const struct cpu_user_regs *regs, +mmio_info_t *info) +{ +int rc; + +/* +* Erratum 766422: Thumb store translation fault to Hypervisor may +* not have correct HSR Rt value. xen/arch/arm/io.c static const struct mmio_handler *find_mmio_handler(struct domain *d, · line 115
Quoted 10 lines
xen/arch/arm/io.c +10
-0+*/ +if (check_workaround_766422 () && (regs->cpsr &PSR_THUMB ) && + info->dabt.write ) + { + rc =decode_instruction (regs, info); +if ( rc ) + { +gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" ); +return INSTR_ERROR ; + }It looks like we want a "return" here? But it should work either way because it should return with the if (
info->dabt.valid) check right after anyway.xen/arch/arm/io.c static const struct mmio_handler *find_mmio_handler(struct domain *d, · line 120
Quoted 5 lines
xen/arch/arm/io.c +5
-0+ } + +/* If ISS is valid, then no need to decode the instruction any further */ +if (info->dabt.valid) +return INSTR_VALID ;code style
Quoted 50 lines
xen/arch/arm/io.c +31
-2+/* +* Xen should not decode the instruction when it was trapped due to +* translation fault. +*/ +if ( info->dabt.s1ptw ) +return INSTR_RETRY ; + +/* +* If the fault occurred due to cache maintenance or address translation +* instructions, then Xen needs to ignore these instructions. +*/ +if ( info->dabt.cache ) +return INSTR_IGNORE ; + +/* +* Armv8 processor does not provide a valid syndrome for decoding some +* instructions. So in order to process these instructions, Xen must +* decode them. +*/ + rc =decode_instruction (regs, info); +if ( rc ) + { +gprintk (XENLOG_ERR ,"Unable to decode instruction\n" ); +return INSTR_ERROR ; + } +else +return INSTR_SUCCESS ; +} +enum io_state try_handle_mmio (struct cpu_user_regs *regs,const union hsr hsr,paddr_t gpa)@@ -106,14 +159,14 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, .gpa = gpa, .dabt = dabt }; +int rc; +enum instr_decode_state state;ASSERT (hsr.ec ==HSR_EC_DATA_ABORT_LOWER_EL ); handler =find_mmio_handler (v->domain, info.gpa);if ( !handler ) { -int rc; - rc =try_fwd_ioserv (regs, v, &info);if ( rc ==IO_HANDLED )return handle_ioserv (regs, v);xen/arch/arm/io.c enum io_state try_handle_mmio(struct cpu_user_regs *regs, · line 177
Quoted 6 lines
xen/arch/arm/io.c +1
-2@@ -121,31 +174,46 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs,return rc; } -/* All the instructions used on emulated MMIO region should be valid */ -if ( !dabt.valid ) + state =try_decode_instruction (regs, &info);We still have the issue that
try_fwd_ioserv(called above) doesn't work properly if !dabt.valid. I think we need to calltry_decode_instructionand do the "state" checks before find_mmio_handler/try_fwd_ioserv.Quoted 38 lines
xen/arch/arm/io.c +22
-11+/* +* If the instruction was to be ignored by Xen, then it should return to the +* caller which will increment the PC, so that the guest can execute the +* next instruction. +*/ +if ( state ==INSTR_IGNORE ) +return IO_HANDLED ; +/* +* If Xen could not decode the instruction for any reason, then it should +* ask the caller to abort the guest. +*/ +else if ( state ==INSTR_ERROR )return IO_ABORT ; +/* When the instruction needs to be retried by the guest */ +else if ( state ==INSTR_RETRY ) +return IO_UNHANDLED ;/* -* Erratum 766422: Thumb store translation fault to Hypervisor may -* not have correct HSR Rt value. +* At this point, we know that the instruction is either valid or has been +* decoded successfully. Thus, Xen should be allowed to execute the +* instruction on the emulated MMIO region. */ -if (check_workaround_766422 () && (regs->cpsr &PSR_THUMB ) && - dabt.write ) - { -int rc; +if ( info.dabt.write ) + rc =handle_write (handler, v, &info); +else + rc =handle_read (handler, v, &info); - rc =decode_instruction (regs, &info); -if ( rc ) - { -gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" ); -return IO_ABORT ;xen/arch/arm/io.c enum io_state try_handle_mmio(struct cpu_user_regs *regs, · line 214
Quoted 10 lines
xen/arch/arm/io.c +8
-1- } +/* +* If the instruction was decoded and has executed successfully on the MMIO +* region, then Xen should execute the next part of the instruction. (for eg +* increment the rn if it is a post-indexing instruction. +*/ +if ( (rc ==IO_HANDLED ) && (state ==INSTR_SUCCESS ) ) + { +post_increment_register (&info.dabt_instr); }We need to call
post_increment_registeralso fromarch_ioreq_complete_mmiofor the IOREQ case.Quoted 11 lines
xen/arch/arm/io.c +1
-4-if ( info.dabt.write ) -return handle_write (handler, v, &info); -else -return handle_read (handler, v, &info); +return rc; } void register_mmio_handler(struct domain *d,Signature · 1 line
2.17.1
PATCH v6 3/3 xen/arm64: io: Support instructions (for which ISS is not valid) on emulated MMIO region using ioreq handler
In reply to Ayan Kumar HalderWhen an instruction is trapped in Xen due to translation fault, Xen checks if the ISS is valid. If not, Xen tries to resolve the translation fault using p2m page tables. In case if it is a data abort, Xen will try to map the mmio region to the guest (ie tries to emulate the mmio region).
If it is not successfull, then it tries to decode the instruction. It saves the decoding state, rn and imm9 to ioreq. Whenever the vcpu handles the ioreq successfully, it will read the decoding state to determine if the instruction decoded was a ldr/str post indexing (ie INSTR_LDR_STR_POSTINDEXING). If so, it uses these details to post increment rn.
---
Changelog :-
v2..v5 - Provided in cover letter.
v6 - 1. Introduced a new field('enum instr_decode_state state') inside 'struct instr_details'. This holds the decoding state of the instruction. This is later read by the post_increment_register() to determine if rn needs to be incremented. Also, this is read by the callers of try_decode_instruction() to determine if the instruction was valid or ignored or to be retried or error or decoded successfully.
2. Also stored 'instr_details' inside 'struct ioreq'. This enables arch_ioreq_complete_mmio() to invoke post_increment_register() without decoding the instruction again.
3. Check hsr.dabt.valid in do_trap_stage2_abort_guest(). If it is not valid, then decode the instruction. This ensures that try_handle_mmio() is invoked only when the instruction is either valid or decoded successfully.
4. Inside do_trap_stage2_abort_guest(), if hsr.dabt.valid is not set, then resolve the translation fault before trying to decode the instruction. If translation fault is resolved, then return to the guest to execute the instruction again.
xen/arch/arm/arm64/traps.c | 4 +++ xen/arch/arm/decode.c | 1 + xen/arch/arm/decode.h | 9 ------ xen/arch/arm/include/asm/mmio.h | 13 ++++++++ xen/arch/arm/io.c | 56 ++++++++++----------------------- xen/arch/arm/ioreq.c | 13 +++++--- xen/arch/arm/traps.c | 56 +++++++++++++++++++++++++++++++-- xen/include/public/hvm/ioreq.h | 19 +++++------ 8 files changed, 108 insertions(+), 63 deletions(-)
xen/arch/arm/arm64/traps.c +4 -0
@@ -52,6 +52,10 @@ void post_increment_register(const struct instr_details *instr)struct cpu_user_regs *regs =guest_cpu_user_regs ();register_t val; +/* Currently, we handle only ldr/str post indexing instructions */ +if ( instr->state !=INSTR_LDR_STR_POSTINDEXING ) +return ; +/* * Handle when rn = SP * Refer ArmV8 ARM DDI 0487G.b, Page - D1-2463 "Stack pointer register selection"
xen/arch/arm/decode.c +1 -0
@@ -147,6 +147,7 @@ static int decode_arm64(register_t pc, mmio_info_t *info)update_dabt (dabt, opcode.ldr_str.rt, opcode.ldr_str.size, false); + dabt_instr->state =INSTR_LDR_STR_POSTINDEXING ; dabt_instr->rn = opcode.ldr_str.rn; dabt_instr->imm9 = opcode.ldr_str.imm9;
xen/arch/arm/decode.h +0 -9
@@ -52,15 +52,6 @@ union instr {#define POST_INDEX_FIXED_MASK 0x3B200C00#define POST_INDEX_FIXED_VALUE 0x38000400 -enum instr_decode_state -{ -INSTR_ERROR ,/* Error encountered while decoding the instruction */ -INSTR_VALID ,/* ISS is valid, so there is no need to decode */ -INSTR_SUCCESS ,/* Instruction is decoded successfully */ -INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ -INSTR_RETRY /* Instruction is to be retried */ -}; -/* * Decode an instruction from pc * /!\ This function is intended to decode an instruction. It considers that the
xen/arch/arm/include/asm/mmio.h +13 -0
@@ -26,12 +26,23 @@#define MAX_IO_HANDLER 16 +enum instr_decode_state +{ +INSTR_ERROR ,/* Error encountered while decoding the instruction */ +INSTR_VALID ,/* ISS is valid, so there is no need to decode */ +INSTR_LDR_STR_POSTINDEXING ,/* Instruction is decoded successfully. +It is ldr/str post indexing */ +INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ +INSTR_RETRY /* Instruction is to be retried */ +}; +typedef struct {struct hsr_dabt dabt;struct instr_details {unsigned long rn:5 ;signed int imm9:9 ; +enum instr_decode_state state; } dabt_instr;paddr_t gpa; }mmio_info_t ;
@@ -77,6 +88,8 @@ void register_mmio_handler(struct domain *d,int domain_io_init (struct domain *d,int max_count);void domain_io_free (struct domain *d); +void try_decode_instruction (const struct cpu_user_regs *regs, +mmio_info_t *info);#endif /* __ASM_ARM_MMIO_H__ */
xen/arch/arm/io.c +17 -39
110 lines not on this page · raw message
xen/arch/arm/ioreq.c +9 -4
@@ -23,10 +23,13 @@#include <public/hvm/ioreq.h> +#include "decode.h" +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v) {const union hsr hsr = { .bits = regs->hsr }; -const struct hsr_dabt dabt = hsr.dabt; +struct hsr_dabt dabt = hsr.dabt; +/* Code is similar to handle_read */ register_t r = v->io.req.data;
@@ -61,10 +64,13 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,*/ .df = 0, .data = get_user_reg(regs, info->dabt.reg), + .dabt_instr = &info->dabt_instr, .state = STATE_IOREQ_READY, };struct ioreq_server *s =NULL ;enum io_state rc; +bool instr_decoded = false; +const union hsr hsr = { .bits = regs->hsr };if ( vio->req.state !=STATE_IOREQ_NONE ) {
@@ -76,9 +82,6 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,if ( !s )return IO_UNHANDLED ; -if ( !info->dabt.valid ) -return IO_ABORT ; - vio->req = p; rc =ioreq_send (s, &p,0 );
@@ -95,6 +98,7 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,bool arch_ioreq_complete_mmio (void ) {struct vcpu *v = current; +struct instr_details *dabt_instr = v->io.req.dabt_instr;struct cpu_user_regs *regs =guest_cpu_user_regs ();const union hsr hsr = { .bits = regs->hsr };
@@ -106,6 +110,7 @@ bool arch_ioreq_complete_mmio(void)if (handle_ioserv (regs, v) ==IO_HANDLED ) { +post_increment_register (dabt_instr);advance_pc (regs, hsr);return true; }
xen/arch/arm/traps.c +54 -2
80 lines not on this page · raw message
xen/include/public/hvm/ioreq.h +10 -9
@@ -50,19 +50,20 @@* SEGMENT |BUS |DEV |FN |OFFSET */ struct ioreq { -uint64_t addr;/* physical address */ -uint64_t data;/* data (or paddr of data) */ -uint32_t count;/* for rep prefixes */ -uint32_t size;/* size in bytes */ -uint32_t vp_eport;/* evtchn for notifications to/from device model */ +uint64_t addr;/* physical address */ +uint64_t data;/* data (or paddr of data) */ +uint32_t count;/* for rep prefixes */ +uint32_t size;/* size in bytes */ +uint32_t vp_eport;/* evtchn for notifications to/from device model */ uint16_t _pad0;uint8_t state:4 ; -uint8_t data_is_ptr:1 ;/* if 1, data above is the guest paddr -* of the real data to use. */ -uint8_t dir:1 ;/* 1=read, 0=write */ +uint8_t data_is_ptr:1 ;/* if 1, data above is the guest paddr +* of the real data to use. */ +uint8_t dir:1 ;/* 1=read, 0=write */ uint8_t df:1 ;uint8_t _pad1:1 ; -uint8_t type;/* I/O type */ +uint8_t type;/* I/O type */ +struct instr_details *dabt_instr;/* when the instruction is decoded */ };typedef struct ioreq ioreq_t ;
Signature · 1 line
2.17.1
Re: [XEN v6 3/3] xen/arm64: io: Support instructions (for which ISS is not valid) on emulated MMIO region using ioreq handler
Quoted 5 lines · Ayan Kumar Halder
When an instruction is trapped in Xen due to translation fault, Xen checks if the ISS is valid. If not, Xen tries to resolve the translation fault using p2m page tables. In case if it is a data abort, Xen will try to map the mmio region to the guest (ie tries to emulate the mmio region).
Quoted 7 lines
If it is not successfull, then it tries to decode the instruction. It saves the decoding state, rn and imm9 to ioreq. Whenever the vcpu handles the ioreq successfully, it will read the decoding state to determine if the instruction decoded was a ldr/str post indexing (ie
INSTR_LDR_STR_POSTINDEXING). If so, it uses these details to post increment rn.Signed-off-by Ayan Kumar Halder <[email protected]>
Typically, we try to avoid introducing code in one patch, then moving it to a different place in the next patch. We try to add code where it needs to be directly in the first place.
Most of my feedback below is really to do things directly in patch #2. It could be that once you do that, there is nothing left here in patch #3 and you could even drop it.
Quoted 41 lines
---
Changelog :-
v2..v5 - Provided in cover letter.
v6 - 1. Introduced a new
field('enum instr_decode_state state')inside 'structinstr_details'. This holds the decoding state of the instruction. This is later read by thepost_increment_register()to determine if rn needs to be incremented. Also, this is read by the callers oftry_decode_instruction()to determine if the instruction was valid or ignored or to be retried or error or decoded successfully.2. Also stored '
instr_details' inside 'struct ioreq'. This enablesarch_ioreq_complete_mmio()to invokepost_increment_register()without decoding the instruction again.3. Check hsr.dabt.valid in
do_trap_stage2_abort_guest(). If it is not valid, then decode the instruction. This ensures thattry_handle_mmio()is invoked only when the instruction is either valid or decoded successfully.4. Inside
do_trap_stage2_abort_guest(), if hsr.dabt.valid is not set, then resolve the translation fault before trying to decode the instruction. If translation fault is resolved, then return to the guest to execute the instruction again.xen/arch/arm/arm64/traps.c | 4 +++ xen/arch/arm/decode.c | 1 + xen/arch/arm/decode.h | 9 ------ xen/arch/arm/include/asm/mmio.h | 13 ++++++++ xen/arch/arm/io.c | 56 ++++++++++----------------------- xen/arch/arm/ioreq.c | 13 +++++--- xen/arch/arm/traps.c | 56 +++++++++++++++++++++++++++++++-- xen/include/public/hvm/ioreq.h | 19 +++++------ 8 files changed, 108 insertions(+), 63 deletions(-)xen/arch/arm/arm64/traps.c +0
-0
xen/arch/arm/arm64/traps.c void post_increment_register(const struct instr_details *instr) · line 57
Quoted 6 lines
xen/arch/arm/arm64/traps.c +3
-0@@ -52,6 +52,10 @@ void post_increment_register(const struct instr_details *instr)struct cpu_user_regs *regs =guest_cpu_user_regs ();register_t val; +/* Currently, we handle only ldr/str post indexing instructions */ +if ( instr->state !=INSTR_LDR_STR_POSTINDEXING ) +return ;
This change should be in patch #2
xen/arch/arm/decode.c static int decode_arm64(register_t pc, mmio_info_t *info) · line 150
Quoted 12 lines
xen/arch/arm/arm64/traps.c +0
-0/* * Handle when rn = SP * Refer ArmV8 ARM DDI 0487G.b, Page - D1-2463 "Stack pointer register selection" xen/arch/arm/decode.c +1
-0@@ -147,6 +147,7 @@ static int decode_arm64(register_t pc, mmio_info_t *info)update_dabt (dabt, opcode.ldr_str.rt, opcode.ldr_str.size, false); + dabt_instr->state =INSTR_LDR_STR_POSTINDEXING ;
This one should also be in patch #2
Quoted 224 lines
xen/arch/arm/decode.c +0
-0dabt_instr->rn = opcode.ldr_str.rn; dabt_instr->imm9 = opcode.ldr_str.imm9;xen/arch/arm/decode.h +0
-9@@ -52,15 +52,6 @@ union instr {#define POST_INDEX_FIXED_MASK 0x3B200C00#define POST_INDEX_FIXED_VALUE 0x38000400 -enum instr_decode_state -{ -INSTR_ERROR ,/* Error encountered while decoding the instruction */ -INSTR_VALID ,/* ISS is valid, so there is no need to decode */ -INSTR_SUCCESS ,/* Instruction is decoded successfully */ -INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ -INSTR_RETRY /* Instruction is to be retried */ -}; -/* * Decode an instruction from pc * /!\ This function is intended to decode an instruction. It considers that the xen/arch/arm/include/asm/mmio.h +13
-0@@ -26,12 +26,23 @@#define MAX_IO_HANDLER 16 +enum instr_decode_state +{ +INSTR_ERROR ,/* Error encountered while decoding the instruction */ +INSTR_VALID ,/* ISS is valid, so there is no need to decode */ +INSTR_LDR_STR_POSTINDEXING ,/* Instruction is decoded successfully. +It is ldr/str post indexing */ +INSTR_IGNORE ,/* Instruction is to be ignored (similar to NOP) */ +INSTR_RETRY /* Instruction is to be retried */ +}; +typedef struct {struct hsr_dabt dabt;struct instr_details {unsigned long rn:5 ;signed int imm9:9 ; +enum instr_decode_state state; } dabt_instr;paddr_t gpa; }mmio_info_t ;@@ -77,6 +88,8 @@ void register_mmio_handler(struct domain *d,int domain_io_init (struct domain *d,int max_count);void domain_io_free (struct domain *d); +void try_decode_instruction (const struct cpu_user_regs *regs, +mmio_info_t *info);#endif /* __ASM_ARM_MMIO_H__ */ xen/arch/arm/io.c +17
-38@@ -95,8 +95,8 @@ static const struct mmio_handler *find_mmio_handler(struct domain *d,return handler; } -enum instr_decode_state try_decode_instruction (const struct cpu_user_regs *regs, -mmio_info_t *info) +void try_decode_instruction (const struct cpu_user_regs *regs, +mmio_info_t *info) {int rc;@@ -111,27 +111,37 @@ enum instr_decode_state try_decode_instruction(const struct cpu_user_regs *regs,if ( rc ) {gprintk (XENLOG_DEBUG ,"Unable to decode instruction\n" ); -return INSTR_ERROR ; + info->dabt_instr.state =INSTR_ERROR ; +return ; } }/* If ISS is valid, then no need to decode the instruction any further */ if (info->dabt.valid) -return INSTR_VALID ; + { + info->dabt_instr.state =INSTR_VALID ; +return ; + }/* * Xen should not decode the instruction when it was trapped due to * translation fault. */ if ( info->dabt.s1ptw ) -return INSTR_RETRY ; + { + info->dabt_instr.state =INSTR_RETRY ; +return ; + }/* * If the fault occurred due to cache maintenance or address translation * instructions, then Xen needs to ignore these instructions. */ if ( info->dabt.cache ) -return INSTR_IGNORE ; + { + info->dabt_instr.state =INSTR_IGNORE ; +return ; + }/* * Armv8 processor does not provide a valid syndrome for decoding some @@ -142,10 +152,8 @@ enum instr_decode_state try_decode_instruction(const struct cpu_user_regs *regs,if ( rc ) {gprintk (XENLOG_ERR ,"Unable to decode instruction\n" ); -return INSTR_ERROR ; + info->dabt_instr.state =INSTR_ERROR ; } -else -return INSTR_SUCCESS ; } enum io_state try_handle_mmio(struct cpu_user_regs *regs,@@ -160,7 +168,6 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, .dabt = dabt };int rc; -enum instr_decode_state state;ASSERT (hsr.ec ==HSR_EC_DATA_ABORT_LOWER_EL );@@ -174,25 +181,6 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs,return rc; } - state =try_decode_instruction (regs, &info); - -/* -* If the instruction was to be ignored by Xen, then it should return to the -* caller which will increment the PC, so that the guest can execute the -* next instruction. -*/ -if ( state ==INSTR_IGNORE ) -return IO_HANDLED ; -/* -* If Xen could not decode the instruction for any reason, then it should -* ask the caller to abort the guest. -*/ -else if ( state ==INSTR_ERROR ) -return IO_ABORT ; -/* When the instruction needs to be retried by the guest */ -else if ( state ==INSTR_RETRY ) -return IO_UNHANDLED ;/* * At this point, we know that the instruction is either valid or has been * decoded successfully. Thus, Xen should be allowed to execute the @@ -203,16 +191,6 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs,else rc =handle_read (handler, v, &info); -/* -* If the instruction was decoded and has executed successfully on the MMIO -* region, then Xen should execute the next part of the instruction. (for eg -* increment the rn if it is a post-indexing instruction. -*/ -if ( (rc ==IO_HANDLED ) && (state ==INSTR_SUCCESS ) ) - { -post_increment_register (&info.dabt_instr); - } -return rc; }xen/arch/arm/ioreq.c +7
-4@@ -23,10 +23,13 @@#include <public/hvm/ioreq.h> +#include "decode.h" +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v) {const union hsr hsr = { .bits = regs->hsr }; -const struct hsr_dabt dabt = hsr.dabt; +struct hsr_dabt dabt = hsr.dabt; +/* Code is similar to handle_read */ register_t r = v->io.req.data;@@ -61,10 +64,13 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,*/ .df = 0, .data = get_user_reg(regs, info->dabt.reg), + .dabt_instr = &info->dabt_instr, .state = STATE_IOREQ_READY, };struct ioreq_server *s =NULL ;enum io_state rc; +bool instr_decoded = false; +const union hsr hsr = { .bits = regs->hsr };if ( vio->req.state !=STATE_IOREQ_NONE ) {@@ -76,9 +82,6 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,if ( !s )return IO_UNHANDLED ; -if ( !info->dabt.valid ) -return IO_ABORT ; - vio->req = p; rc =ioreq_send (s, &p,0 );
xen/arch/arm/ioreq.c enum io_state try_fwd_ioserv(struct cpu_user_regs *regs, · line 101
Quoted 4 lines
xen/arch/arm/ioreq.c +1
-0@@ -95,6 +98,7 @@ enum io_state try_fwd_ioserv(struct cpu_user_regs *regs,bool arch_ioreq_complete_mmio (void ) {struct vcpu *v = current; +struct instr_details *dabt_instr = v->io.req.dabt_instr;
As Julien suggested, we can add the instruction details to vcpu_io (not v->io.req).
xen/arch/arm/ioreq.c bool arch_ioreq_complete_mmio(void) · line 116
Quoted 11 lines
xen/arch/arm/ioreq.c +1
-0struct cpu_user_regs *regs =guest_cpu_user_regs ();const union hsr hsr = { .bits = regs->hsr };@@ -106,6 +110,7 @@ bool arch_ioreq_complete_mmio(void)if (handle_ioserv (regs, v) ==IO_HANDLED ) { +post_increment_register (dabt_instr);advance_pc (regs, hsr);return true; }
This change should also be in the previous patch.
Quoted 50 lines
xen/arch/arm/traps.c +35
-0@@ -1906,6 +1906,7 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,paddr_t gpa;uint8_t fsc = xabt.fsc & ~FSC_LL_MASK ;bool is_data = (hsr.ec ==HSR_EC_DATA_ABORT_LOWER_EL ); +mmio_info_t info;/* * If this bit has been set, it means that this stage-2 abort is caused @@ -1959,6 +1960,51 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,return ; }case FSC_FLT_TRANS : + + info.gpa = gpa; + info.dabt = hsr.dabt; + +/* Check if the ISS is valid. */ +if ( !hsr.dabt.valid ) + { + +/* +* Assumption :- Most of the times when we get a translation fault +* and the ISS is invalid, the underlying cause is that the page +* tables have not been set up correctly. +* First check if the translation fault can be resolved by the +* P2M subsystem. If that's the case nothing else to do. +*/ +if (p2m_resolve_translation_fault (current->domain, +gaddr_to_gfn (gpa)) ) +return ; + +if ( is_data &&try_map_mmio (gaddr_to_gfn (gpa)) ) +return ; + +try_decode_instruction (regs, &info); + +/* +* If the instruction was to be ignored by Xen, then it should return to the +* caller which will increment the PC, so that the guest can execute the +* next instruction. +*/ +if ( info.dabt_instr.state ==INSTR_IGNORE ) + { +advance_pc (regs, hsr); +return ; + }
xen/arch/arm/traps.c static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, · line 2006
Quoted 10 lines
xen/arch/arm/traps.c +10
-0+/* +* If Xen could not decode the instruction for any reason, then it should +* ask the caller to abort the guest. +*/ + else if ( info.dabt_instr.state == INSTR_ERROR ) + goto inject_abt; +/* When the instruction needs to be retried by the guest */ + else if ( info.dabt_instr.state == INSTR_RETRY ) + return; + }
It is best to add the code directly here in the first place, rather than adding it to try_handle_mmio first, then moving it here.
I think we should move the two calls to p2m_resolve_translation_fault and try_map_mmio to a function to avoid repetation:
xen/arch/arm/traps.c +19 -22
@@ -1893,6 +1893,23 @@ static bool try_map_mmio(gfn_t gfn)return !map_regions_p2mt (d, gfn,1 , mfn, p2m_mmio_direct_c); } +static inline void check_p2m (bool is_data,paddr_t gpa) +{ +/* +* Assumption :- Most of the times when we get a translation fault +* and the ISS is invalid, the underlying cause is that the page +* tables have not been set up correctly. +* First check if the translation fault can be resolved by the +* P2M subsystem. If that's the case nothing else to do. +*/ +if (p2m_resolve_translation_fault (current->domain, +gaddr_to_gfn (gpa)) ) +return ; + +if ( is_data &&try_map_mmio (gaddr_to_gfn (gpa)) ) +return ; +} +static void do_trap_stage2_abort_guest (struct cpu_user_regs *regs,const union hsr hsr) {
@@ -1968,19 +1985,7 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,if ( !hsr.dabt.valid ) { -/* -* Assumption :- Most of the times when we get a translation fault -* and the ISS is invalid, the underlying cause is that the page -* tables have not been set up correctly. -* First check if the translation fault can be resolved by the -* P2M subsystem. If that's the case nothing else to do. -*/ -if (p2m_resolve_translation_fault (current->domain, -gaddr_to_gfn (gpa)) ) -return ; - -if ( is_data &&try_map_mmio (gaddr_to_gfn (gpa)) ) -return ; +check_p2m (is_data, gpa);try_decode_instruction (regs, &info);
@@ -2038,15 +2043,7 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, } } -/* First check if the translation fault can be resolved by the -* P2M subsystem. If that's the case nothing else to do. -*/ -if (p2m_resolve_translation_fault (current->domain, -gaddr_to_gfn (gpa)) ) -return ; - -if ( is_data &&try_map_mmio (gaddr_to_gfn (gpa)) ) -return ; +check_p2m (is_data, gpa);break ;default :
Quoted 4 lines
xen/arch/arm/traps.c +0
-0/* * Attempt first to emulate the MMIO as the data abort will * likely happen in an emulated region.
xen/arch/arm/traps.c static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, · line 2030
Quoted 10 lines
xen/arch/arm/traps.c +7
-0@@ -1975,6 +2021,13 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, goto inject_abt;case IO_HANDLED :advance_pc (regs, hsr); +/* +* If the instruction was decoded and has executed successfully +* on the MMIO region, then Xen should execute the next part of +* the instruction. (for eg increment the rn if it is a +* post-indexing instruction. +*/ +post_increment_register (&info.dabt_instr);
I would introduce the post_increment_register() call here directly in patch #2, rather than introducing it first in one place, then moving it in the next patch
xen/arch/arm/traps.c static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, · line 2043
Quoted 12 lines
xen/arch/arm/traps.c +1
-2return ;case IO_RETRY :/* finish later */ @@ -1985,8 +2038,7 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs, } } -/* -* First check if the translation fault can be resolved by the +/* First check if the translation fault can be resolved by the * P2M subsystem. If that's the case nothing else to do. */
Spurious change
Quoted 24 lines
xen/arch/arm/traps.c +0
-0if ( p2m_resolve_translation_fault(current->domain,xen/include/public/hvm/ioreq.h +5
-8@@ -50,19 +50,20 @@* SEGMENT |BUS |DEV |FN |OFFSET */ struct ioreq { -uint64_t addr;/* physical address */ -uint64_t data;/* data (or paddr of data) */ -uint32_t count;/* for rep prefixes */ -uint32_t size;/* size in bytes */ -uint32_t vp_eport;/* evtchn for notifications to/from device model */ +uint64_t addr;/* physical address */ +uint64_t data;/* data (or paddr of data) */ +uint32_t count;/* for rep prefixes */ +uint32_t size;/* size in bytes */ +uint32_t vp_eport;/* evtchn for notifications to/from device model */ uint16_t _pad0;uint8_t state:4 ; -uint8_t data_is_ptr:1 ;/* if 1, data above is the guest paddr -* of the real data to use. */ -uint8_t dir:1 ;/* 1=read, 0=write */
xen/include/public/hvm/ioreq.h line 68
Quoted 10 lines
xen/include/public/hvm/ioreq.h +5
-1+ uint8_t data_is_ptr:1; /* if 1, data above is the guest paddr +* of the real data to use. */ + uint8_t dir:1; /* 1=read, 0=write */ uint8_t df:1; uint8_t _pad1:1; -uint8_t type;/* I/O type */ +uint8_t type;/* I/O type */ +struct instr_details *dabt_instr;/* when the instruction is decoded */ };typedef struct ioreq ioreq_t ;
This structure is a public interface (public as in exposed and used by other project.) We cannot change it easily as we need to maintain backward compatibility. In any case, I don't think that changing struct ioreq is required for what we are trying to achieve with this patch.