x86: Some MISRA Rule 5.3 fixes
cover x86: Some MISRA Rule 5.3 fixes
'debug' and 'str' account for an awefully large number of shadowed variable violations.
Andrew Cooper (3):
x86/traps: Move do_general_protection() earlier
x86/entry: Rename the exception entrypoints
x86: Delete str()
xen/arch/x86/hvm/svm/svm.c | 2 - xen/arch/x86/include/asm/desc.h | 9 -- xen/arch/x86/include/asm/processor.h | 34 +--- xen/arch/x86/pv/traps.c | 2 + xen/arch/x86/traps.c | 227 ++++++++++++++------------- xen/arch/x86/x86_64/entry.S | 36 ++--- 6 files changed, 142 insertions(+), 168 deletions(-)
base-commit: dbd566ab729d2839c3c0be5c47cdcc06c2c477f0
Signature · 1 line
2.30.2
PATCH 1/3 x86/traps: Move do_general_protection() earlier
... in order to clean up the declarations without needing to forward declare it for handle_gdt_ldt_mapping_fault()
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>
--- xen/arch/x86/traps.c | 157 +++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 79 deletions(-)
xen/arch/x86/traps.c +78 -79
@@ -1224,6 +1224,84 @@ void do_int3(struct cpu_user_regs *regs)pv_inject_hw_exception (X86_EXC_BP ,X86_EVENT_NO_EC ); } +void do_general_protection (struct cpu_user_regs *regs) +{ +#ifdef CONFIG_PV +struct vcpu *v = current; +#endif + +if ( regs->error_code &X86_XEC_EXT ) + goto hardware_gp; + +if ( !guest_mode (regs) ) + goto gp_in_kernel; + +#ifdef CONFIG_PV +/* +* Cunning trick to allow arbitrary "INT n" handling. +* +* We set DPL == 0 on all vectors in the IDT. This prevents any INT <n> +* instruction from trapping to the appropriate vector, when that might not +* be expected by Xen or the guest OS. For example, that entry might be for +* a fault handler (unlike traps, faults don't increment EIP), or might +* expect an error code on the stack (which a software trap never +* provides), or might be a hardware interrupt handler that doesn't like +* being called spuriously. +* +* Instead, a GPF occurs with the faulting IDT vector in the error code. +* Bit 1 is set to indicate that an IDT entry caused the fault. Bit 0 is +* clear (which got already checked above) to indicate that it's a software +* fault, not a hardware one. +* +* NOTE: Vectors 3 and 4 are dealt with from their own handler. This is +* okay because they can only be triggered by an explicit DPL-checked +* instruction. The DPL specified by the guest OS for these vectors is NOT +* CHECKED!! +*/ +if ( regs->error_code &X86_XEC_IDT ) + { +/* This fault must be due to <INT n> instruction. */ +uint8_t vector = regs->error_code >>3 ; +const struct trap_info *ti = &v->arch.pv.trap_ctxt[vector]; + +if (permit_softint (TI_GET_DPL (ti), v, regs) ) + { + regs->rip +=2 ; +pv_inject_sw_interrupt (vector); +return ; + } + } +else if (is_pv_32bit_vcpu (v) && regs->error_code ) + { +pv_emulate_gate_op (regs); +return ; + } + +/* Emulate some simple privileged and I/O instructions. */ +if ( (regs->error_code ==0 ) && +pv_emulate_privileged_op (regs) ) + { +trace_trap_one_addr (TRC_PV_EMULATE_PRIVOP , regs->rip); +return ; + } + +/* Pass on GPF as is. */ +pv_inject_hw_exception (X86_EXC_GP , regs->error_code); +return ; +#endif + + gp_in_kernel: +if (likely (extable_fixup (regs, true)) ) +return ; + + hardware_gp: +if (debugger_trap_fatal (X86_EXC_GP , regs) ) +return ; + +show_execution_state (regs); +panic ("GENERAL PROTECTION FAULT\n[error_code=%04x]\n" , regs->error_code); +} +#ifdef CONFIG_PV static int handle_ldt_mapping_fault (unsigned int offset,struct cpu_user_regs *regs)
@@ -1589,85 +1667,6 @@ void __init do_early_page_fault(struct cpu_user_regs *regs) } } -void do_general_protection (struct cpu_user_regs *regs) -{ -#ifdef CONFIG_PV -struct vcpu *v = current; -#endif - -if ( regs->error_code &X86_XEC_EXT ) - goto hardware_gp; - -if ( !guest_mode (regs) ) - goto gp_in_kernel; - -#ifdef CONFIG_PV -/* -* Cunning trick to allow arbitrary "INT n" handling. -* -* We set DPL == 0 on all vectors in the IDT. This prevents any INT <n> -* instruction from trapping to the appropriate vector, when that might not -* be expected by Xen or the guest OS. For example, that entry might be for -* a fault handler (unlike traps, faults don't increment EIP), or might -* expect an error code on the stack (which a software trap never -* provides), or might be a hardware interrupt handler that doesn't like -* being called spuriously. -* -* Instead, a GPF occurs with the faulting IDT vector in the error code. -* Bit 1 is set to indicate that an IDT entry caused the fault. Bit 0 is -* clear (which got already checked above) to indicate that it's a software -* fault, not a hardware one. -* -* NOTE: Vectors 3 and 4 are dealt with from their own handler. This is -* okay because they can only be triggered by an explicit DPL-checked -* instruction. The DPL specified by the guest OS for these vectors is NOT -* CHECKED!! -*/ -if ( regs->error_code &X86_XEC_IDT ) - { -/* This fault must be due to <INT n> instruction. */ -uint8_t vector = regs->error_code >>3 ; -const struct trap_info *ti = &v->arch.pv.trap_ctxt[vector]; - -if (permit_softint (TI_GET_DPL (ti), v, regs) ) - { - regs->rip +=2 ; -pv_inject_sw_interrupt (vector); -return ; - } - } -else if (is_pv_32bit_vcpu (v) && regs->error_code ) - { -pv_emulate_gate_op (regs); -return ; - } - -/* Emulate some simple privileged and I/O instructions. */ -if ( (regs->error_code ==0 ) && -pv_emulate_privileged_op (regs) ) - { -trace_trap_one_addr (TRC_PV_EMULATE_PRIVOP , regs->rip); -return ; - } - -/* Pass on GPF as is. */ -pv_inject_hw_exception (X86_EXC_GP , regs->error_code); -return ; -#endif - - gp_in_kernel: - -if (likely (extable_fixup (regs, true)) ) -return ; - - hardware_gp: -if (debugger_trap_fatal (X86_EXC_GP , regs) ) -return ; - -show_execution_state (regs); -panic ("GENERAL PROTECTION FAULT\n[error_code=%04x]\n" , regs->error_code); -} -static bool pci_serr_cont;static bool pci_serr_nmicont (void )
Signature · 1 line
2.30.2
PATCH 2/3 x86/entry: Rename the exception entrypoints
In reply to Andrew CooperThis makes the names match the architectural short names that we use elsewhere. This avoids 'debug' in particular from being a global symbol shadowed by many local parameter names.
Remove the DECLARE_TRAP_HANDLER{,_CONST}() infrastructure. Only NMI/#MC are referenced externally (and NMI will cease to be soon, as part of adding FRED support). Move the entrypoint declarations into the respective traps.c where they're used, rather than keeping them visible across ~all of Xen.
Drop the long-stale comment at the top of init_idt_traps(). It's mostly discussing a 32bit Xen, and bogus otherwise as it's impossible to use trap gates correctly for these purposes.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>
This is half of a previous patch, cut down to just the rename and header file cleanup.
--- xen/arch/x86/include/asm/processor.h | 34 +------------- xen/arch/x86/pv/traps.c | 2 + xen/arch/x86/traps.c | 70 +++++++++++++++++----------- xen/arch/x86/x86_64/entry.S | 36 +++++++------- 4 files changed, 64 insertions(+), 78 deletions(-)
xen/arch/x86/include/asm/processor.h +2 -32
@@ -417,38 +417,8 @@ extern void mtrr_bp_init(void);void mcheck_init (struct cpuinfo_x86 *c,bool_t bsp); -#define DECLARE_TRAP_HANDLER (_name) \ - void _name(void); \ - void do_ ## _name(struct cpu_user_regs *regs) -#define DECLARE_TRAP_HANDLER_CONST (_name) \ - void _name(void); \ - void do_ ## _name(const struct cpu_user_regs *regs) - -DECLARE_TRAP_HANDLER (divide_error); -DECLARE_TRAP_HANDLER (debug); -DECLARE_TRAP_HANDLER_CONST (nmi); -DECLARE_TRAP_HANDLER (int3); -DECLARE_TRAP_HANDLER (overflow); -DECLARE_TRAP_HANDLER (bounds); -DECLARE_TRAP_HANDLER (invalid_op); -DECLARE_TRAP_HANDLER (device_not_available); -DECLARE_TRAP_HANDLER (double_fault); -DECLARE_TRAP_HANDLER (invalid_TSS); -DECLARE_TRAP_HANDLER (segment_not_present); -DECLARE_TRAP_HANDLER (stack_segment); -DECLARE_TRAP_HANDLER (general_protection); -DECLARE_TRAP_HANDLER (page_fault); -DECLARE_TRAP_HANDLER (early_page_fault); -DECLARE_TRAP_HANDLER (coprocessor_error); -DECLARE_TRAP_HANDLER (simd_coprocessor_error); -DECLARE_TRAP_HANDLER_CONST (machine_check); -DECLARE_TRAP_HANDLER (alignment_check); -DECLARE_TRAP_HANDLER (entry_CP); - -DECLARE_TRAP_HANDLER (entry_int82); - -#undef DECLARE_TRAP_HANDLER_CONST -#undef DECLARE_TRAP_HANDLER +void do_nmi (const struct cpu_user_regs *regs); +void do_machine_check (const struct cpu_user_regs *regs);void trap_nop (void );
xen/arch/x86/pv/traps.c +2 -0
@@ -132,6 +132,8 @@ static void cf_check nmi_softirq(void) *v_ptr =NULL ; } +void nocall entry_int82(void); +void __init pv_trap_init (void ) {#ifdef CONFIG_PV32
xen/arch/x86/traps.c +42 -28
84 lines not on this page · raw message
xen/arch/x86/x86_64/entry.S +18 -18
@@ -708,7 +708,7 @@ ENTRY(common_interrupt) mov %bl, STACK_CPUINFO_FIELD(use_pv_cr3)(%r14) jmp ret_from_intr -ENTRY(page_fault) +ENTRY(entry_PF) ENDBR64 movl $X86_EXC_PF, 4(%rsp) /* No special register assumptions. */
@@ -881,81 +881,81 @@ FATAL_exception_with_ints_disabled: movq %rsp,%rdi tailcall fatal_trap -ENTRY(divide_error) +ENTRY(entry_DE) ENDBR64 pushq $0 movl $X86_EXC_DE, 4(%rsp) jmp handle_exception -ENTRY(coprocessor_error) +ENTRY(entry_MF) ENDBR64 pushq $0 movl $X86_EXC_MF, 4(%rsp) jmp handle_exception -ENTRY(simd_coprocessor_error) +ENTRY(entry_XM) ENDBR64 pushq $0 movl $X86_EXC_XM, 4(%rsp) jmp handle_exception -ENTRY(device_not_available) +ENTRY(entry_NM) ENDBR64 pushq $0 movl $X86_EXC_NM, 4(%rsp) jmp handle_exception -ENTRY(debug) +ENTRY(entry_DB) ENDBR64 pushq $0 movl $X86_EXC_DB, 4(%rsp) jmp handle_ist_exception -ENTRY(int3) +ENTRY(entry_BP) ENDBR64 pushq $0 movl $X86_EXC_BP, 4(%rsp) jmp handle_exception -ENTRY(overflow) +ENTRY(entry_OF) ENDBR64 pushq $0 movl $X86_EXC_OF, 4(%rsp) jmp handle_exception -ENTRY(bounds) +ENTRY(entry_BR) ENDBR64 pushq $0 movl $X86_EXC_BR, 4(%rsp) jmp handle_exception -ENTRY(invalid_op) +ENTRY(entry_UD) ENDBR64 pushq $0 movl $X86_EXC_UD, 4(%rsp) jmp handle_exception -ENTRY(invalid_TSS) +ENTRY(entry_TS) ENDBR64 movl $X86_EXC_TS, 4(%rsp) jmp handle_exception -ENTRY(segment_not_present) +ENTRY(entry_NP) ENDBR64 movl $X86_EXC_NP, 4(%rsp) jmp handle_exception -ENTRY(stack_segment) +ENTRY(entry_SS) ENDBR64 movl $X86_EXC_SS, 4(%rsp) jmp handle_exception -ENTRY(general_protection) +ENTRY(entry_GP) ENDBR64 movl $X86_EXC_GP, 4(%rsp) jmp handle_exception -ENTRY(alignment_check) +ENTRY(entry_AC) ENDBR64 movl $X86_EXC_AC, 4(%rsp) jmp handle_exception
@@ -965,7 +965,7 @@ ENTRY(entry_CP) movl $X86_EXC_CP, 4(%rsp) jmp handle_exception -ENTRY(double_fault) +ENTRY(entry_DF) ENDBR64 movl $X86_EXC_DF, 4(%rsp) /* Set AC to reduce chance of further SMAP faults */
@@ -989,7 +989,7 @@ ENTRY(double_fault) movq %rsp,%rdi tailcall do_double_fault -ENTRY(nmi) +ENTRY(entry_NMI) ENDBR64 pushq $0 movl $X86_EXC_NMI, 4(%rsp)
@@ -1117,7 +1117,7 @@ handle_ist_exception: jmp restore_all_xen #endif -ENTRY(machine_check) +ENTRY(entry_MC) ENDBR64 pushq $0 movl $X86_EXC_MC, 4(%rsp)
Signature · 1 line
2.30.2
Re: [PATCH 2/3] x86/entry: Rename the exception entrypoints
Quoted 8 lines · Andrew Cooper
This makes the names match the architectural short names that we use elsewhere. This avoids 'debug' in particular from being a global symbol shadowed by many local parameter names.
Remove the DECLARE_TRAP_HANDLER{,_CONST}() infrastructure. Only NMI/#MC are referenced externally (and NMI will cease to be soon, as part of adding FRED support). Move the entrypoint declarations into the respective
traps.cwhere they're used, rather than keeping them visible across ~all of Xen.
Coming back to my Misra concern here: There's no way we can avoid violating rule 8.7 (and maybe also 8.6) when it comes to symbols defined in assembly code. Rule 8.5 otoh is a little more relaxed than I recalled when commenting on the earlier version of the patch, but it still talks of exclusively "header files". Roberto, could you please clarify whether putting such declarations in .c files is indeed not a violation of 8.5?
In any event I see you've added "nocall" - I guess that's an easy identification for (some of) the symbols defined in assembly code, and hence needing special casing in scans.
Quoted 3 lines
Drop the long-stale comment at the top of
init_idt_traps(). It's mostly discussing a 32bit Xen, and bogus otherwise as it's impossible to use trap gates correctly for these purposes.
I don't think I follow the "impossible", but I'm also not going to make my (eventual) ack dependent upon you further adjusting this part of the description.
Jan
Re: [PATCH 2/3] x86/entry: Rename the exception entrypoints
In reply to Andrew CooperQuoted 23 lines · Andrew Cooper
This makes the names match the architectural short names that we use elsewhere. This avoids 'debug' in particular from being a global symbol shadowed by many local parameter names.
Remove the DECLARE_TRAP_HANDLER{,_CONST}() infrastructure. Only NMI/#MC are referenced externally (and NMI will cease to be soon, as part of adding FRED support). Move the entrypoint declarations into the respective
traps.cwhere they're used, rather than keeping them visible across ~all of Xen.Drop the long-stale comment at the top of
init_idt_traps(). It's mostly discussing a 32bit Xen, and bogus otherwise as it's impossible to use trap gates correctly for these purposes.No functional change.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>
Quoted 9 lines
This is half of a previous patch, cut down to just the rename and header file cleanup.
--- xen/arch/x86/include/asm/processor.h | 34 +------------- xen/arch/x86/pv/traps.c | 2 + xen/arch/x86/traps.c | 70 +++++++++++++++++----------- xen/arch/x86/x86_64/entry.S | 36 +++++++------- 4 files changed, 64 insertions(+), 78 deletions(-)
With respect to Rule 5.3:
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
PATCH 3/3 x86: Delete str()
In reply to Andrew CooperThis is used in an assertion only, which is somewhat dubious to begin with and won't surivive the x86-S work (where TR will become be a NUL selector).
Delete it now. This avoids many cases where as a global symbol, it shadows local string variables.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>
--- xen/arch/x86/hvm/svm/svm.c | 2 -- xen/arch/x86/include/asm/desc.h | 9 --------- 2 files changed, 11 deletions(-)
xen/arch/x86/hvm/svm/svm.c +0 -2
@@ -1614,8 +1614,6 @@ static int _svm_cpu_up(bool bsp)/* Initialize OSVW bits to be used by guests */ svm_host_osvw_init (); -/* Minimal checking that enough CPU setup was done by now. */ -ASSERT (str () ==TSS_SELECTOR );svm_vmsave_pa (per_cpu (host_vmcb, cpu));return 0 ;
xen/arch/x86/include/asm/desc.h +0 -9
@@ -238,15 +238,6 @@ static inline void ltr(unsigned int sel) __asm__ __volatile__ ("ltr %w0" ::"rm" (sel) :"memory" ); } -static inline unsigned int str (void ) -{ -unsigned int sel; - - __asm__ ("str %0" :"=r" (sel) ); - -return sel; -} -#endif /* !__ASSEMBLY__ */ #endif /* __ARCH_DESC_H */
Signature · 1 line
2.30.2
Re: [PATCH 3/3] x86: Delete str()
Quoted 2 lines · Andrew Cooper
This is used in an assertion only, which is somewhat dubious to begin with and won't surivive the x86-S work (where TR will become be a NUL selector).
I'm kind of okay with the removal, but I can't read anything like the above out of the doc. Can you point me at where this is said?
Jan
Quoted 48 lines
Delete it now. This avoids many cases where as a global symbol, it shadows local string variables.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>--- xen/arch/x86/hvm/svm/svm.c | 2 -- xen/arch/x86/include/asm/desc.h | 9 --------- 2 files changed, 11 deletions(-)xen/arch/x86/hvm/svm/svm.c +0
-2@@ -1614,8 +1614,6 @@ static int _svm_cpu_up(bool bsp)/* Initialize OSVW bits to be used by guests */ svm_host_osvw_init (); -/* Minimal checking that enough CPU setup was done by now. */ -ASSERT (str () ==TSS_SELECTOR );svm_vmsave_pa (per_cpu (host_vmcb, cpu));return 0 ;xen/arch/x86/include/asm/desc.h +0
-9@@ -238,15 +238,6 @@ static inline void ltr(unsigned int sel) __asm__ __volatile__ ("ltr %w0" ::"rm" (sel) :"memory" ); } -static inline unsigned int str (void ) -{ -unsigned int sel; - - __asm__ ("str %0" :"=r" (sel) ); - -return sel; -} -#endif /* !__ASSEMBLY__ */ #endif /* __ARCH_DESC_H */
Re: [PATCH 3/3] x86: Delete str()
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
This is used in an assertion only, which is somewhat dubious to begin with and won't surivive the x86-S work (where TR will become be a NUL selector).
I'm kind of okay with the removal, but I can't read anything like the above out of the doc. Can you point me at where this is said?
A future draft of the spec.
FRED removes the IDT completely, most of the TSS, and can let you get away with GDT/LDT limits of 0.
x86-S removes the final aspects of the TSS (the IO perm bitmap, and PVI).
Intel have agreed that being able to (effectively) ltr $0x0000 to set the TSS invalid (like NULL selectors do for all other segments) would be useful, as it means you don't need to have a transiently non-empty GDT just to load an empty TR.
~Andrew
Re: [PATCH 3/3] x86: Delete str()
In reply to Andrew CooperQuoted 41 lines · Andrew Cooper
This is used in an assertion only, which is somewhat dubious to begin with and won't surivive the x86-S work (where TR will become be a NUL selector).
Delete it now. This avoids many cases where as a global symbol, it shadows local string variables.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>--- xen/arch/x86/hvm/svm/svm.c | 2 -- xen/arch/x86/include/asm/desc.h | 9 --------- 2 files changed, 11 deletions(-)xen/arch/x86/hvm/svm/svm.c +0
-2@@ -1614,8 +1614,6 @@ static int _svm_cpu_up(bool bsp)/* Initialize OSVW bits to be used by guests */ svm_host_osvw_init (); -/* Minimal checking that enough CPU setup was done by now. */ -ASSERT (str () ==TSS_SELECTOR );svm_vmsave_pa (per_cpu (host_vmcb, cpu));return 0 ;xen/arch/x86/include/asm/desc.h +0
-2@@ -238,15 +238,6 @@ static inline void ltr(unsigned int sel) __asm__ __volatile__ ("ltr %w0" ::"rm" (sel) :"memory" ); } -static inline unsigned int str (void ) -{
xen/arch/x86/include/asm/desc.h static inline void ltr(unsigned int sel) · line 243
Quoted 10 lines
xen/arch/x86/include/asm/desc.h +0
-7-unsigned int sel; - - __asm__ ("str %0" :"=r" (sel) ); - -return sel; -} -#endif /* !__ASSEMBLY__ */ #endif /* __ARCH_DESC_H */
With respect to shadowing (Rule 5.3)
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Quoted 7 lines · Andrew Cooper
'debug' and 'str' account for an awefully large number of shadowed variable violations.
Andrew Cooper (3):
x86/traps: Movedo_general_protection()earlier
x86/entry: Rename the exception entrypoints
x86: Deletestr()
Series
with a slightly amended description in patch 3, and preferably also with at least mentioning the Misra angle in patch 2.
Jan