Loading Documentation/x86/exception-tables.txt +35 −0 Original line number Diff line number Diff line Loading @@ -290,3 +290,38 @@ Due to the way that the exception table is built and needs to be ordered, only use exceptions for code in the .text section. Any other section will cause the exception table to not be sorted correctly, and the exceptions will fail. Things changed when 64-bit support was added to x86 Linux. Rather than double the size of the exception table by expanding the two entries from 32-bits to 64 bits, a clever trick was used to store addresses as relative offsets from the table itself. The assembly code changed from: .long 1b,3b to: .long (from) - . .long (to) - . and the C-code that uses these values converts back to absolute addresses like this: ex_insn_addr(const struct exception_table_entry *x) { return (unsigned long)&x->insn + x->insn; } In v4.6 the exception table entry was expanded with a new field "handler". This is also 32-bits wide and contains a third relative function pointer which points to one of: 1) int ex_handler_default(const struct exception_table_entry *fixup) This is legacy case that just jumps to the fixup code 2) int ex_handler_fault(const struct exception_table_entry *fixup) This case provides the fault number of the trap that occurred at entry->insn. It is used to distinguish page faults from machine check. 3) int ex_handler_ext(const struct exception_table_entry *fixup) This case is used for uaccess_err ... we need to set a flag in the task structure. Before the handler functions existed this case was handled by adding a large offset to the fixup to tag it as special. More functions can easily be added. arch/x86/include/asm/asm.h +24 −16 Original line number Diff line number Diff line Loading @@ -44,19 +44,22 @@ /* Exception table entry */ #ifdef __ASSEMBLY__ # define _ASM_EXTABLE(from,to) \ # define _ASM_EXTABLE_HANDLE(from, to, handler) \ .pushsection "__ex_table","a" ; \ .balign 8 ; \ .balign 4 ; \ .long (from) - . ; \ .long (to) - . ; \ .long (handler) - . ; \ .popsection # define _ASM_EXTABLE(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_default) # define _ASM_EXTABLE_FAULT(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_fault) # define _ASM_EXTABLE_EX(from, to) \ .pushsection "__ex_table","a" ; \ .balign 8 ; \ .long (from) - . ; \ .long (to) - . + 0x7ffffff0 ; \ .popsection _ASM_EXTABLE_HANDLE(from, to, ex_handler_ext) # define _ASM_NOKPROBE(entry) \ .pushsection "_kprobe_blacklist","aw" ; \ Loading Loading @@ -89,19 +92,24 @@ .endm #else # define _ASM_EXTABLE(from,to) \ # define _EXPAND_EXTABLE_HANDLE(x) #x # define _ASM_EXTABLE_HANDLE(from, to, handler) \ " .pushsection \"__ex_table\",\"a\"\n" \ " .balign 8\n" \ " .balign 4\n" \ " .long (" #from ") - .\n" \ " .long (" #to ") - .\n" \ " .long (" _EXPAND_EXTABLE_HANDLE(handler) ") - .\n" \ " .popsection\n" # define _ASM_EXTABLE(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_default) # define _ASM_EXTABLE_FAULT(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_fault) # define _ASM_EXTABLE_EX(from, to) \ " .pushsection \"__ex_table\",\"a\"\n" \ " .balign 8\n" \ " .long (" #from ") - .\n" \ " .long (" #to ") - . + 0x7ffffff0\n" \ " .popsection\n" _ASM_EXTABLE_HANDLE(from, to, ex_handler_ext) /* For C file, we already have NOKPROBE_SYMBOL macro */ #endif Loading arch/x86/include/asm/msr-index.h +4 −0 Original line number Diff line number Diff line Loading @@ -269,6 +269,10 @@ #define MSR_IA32_MC0_CTL2 0x00000280 #define MSR_IA32_MCx_CTL2(x) (MSR_IA32_MC0_CTL2 + (x)) /* 'SMCA': AMD64 Scalable MCA */ #define MSR_AMD64_SMCA_MC0_CONFIG 0xc0002004 #define MSR_AMD64_SMCA_MCx_CONFIG(x) (MSR_AMD64_SMCA_MC0_CONFIG + 0x10*(x)) #define MSR_P6_PERFCTR0 0x000000c1 #define MSR_P6_PERFCTR1 0x000000c2 #define MSR_P6_EVNTSEL0 0x00000186 Loading arch/x86/include/asm/uaccess.h +8 −8 Original line number Diff line number Diff line Loading @@ -90,12 +90,11 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un likely(!__range_not_ok(addr, size, user_addr_max())) /* * The exception table consists of pairs of addresses relative to the * exception table enty itself: the first is the address of an * instruction that is allowed to fault, and the second is the address * at which the program should continue. No registers are modified, * so it is entirely up to the continuation code to figure out what to * do. * The exception table consists of triples of addresses relative to the * exception table entry itself. The first address is of an instruction * that is allowed to fault, the second is the target at which the program * should continue. The third is a handler function to deal with the fault * caused by the instruction in the first field. * * All the routines below use bits of fixup code that are out of line * with the main instruction path. This means when everything is well, Loading @@ -104,13 +103,14 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un */ struct exception_table_entry { int insn, fixup; int insn, fixup, handler; }; /* This is not the generic standard exception_table_entry format */ #define ARCH_HAS_SORT_EXTABLE #define ARCH_HAS_SEARCH_EXTABLE extern int fixup_exception(struct pt_regs *regs); extern int fixup_exception(struct pt_regs *regs, int trapnr); extern bool ex_has_fault_handler(unsigned long ip); extern int early_fixup_exception(unsigned long *ip); /* Loading arch/x86/kernel/cpu/mcheck/mce-severity.c +20 −2 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ #include <linux/init.h> #include <linux/debugfs.h> #include <asm/mce.h> #include <asm/uaccess.h> #include "mce-internal.h" Loading @@ -29,7 +30,7 @@ * panic situations) */ enum context { IN_KERNEL = 1, IN_USER = 2 }; enum context { IN_KERNEL = 1, IN_USER = 2, IN_KERNEL_RECOV = 3 }; enum ser { SER_REQUIRED = 1, NO_SER = 2 }; enum exception { EXCP_CONTEXT = 1, NO_EXCP = 2 }; Loading @@ -48,6 +49,7 @@ static struct severity { #define MCESEV(s, m, c...) { .sev = MCE_ ## s ## _SEVERITY, .msg = m, ## c } #define KERNEL .context = IN_KERNEL #define USER .context = IN_USER #define KERNEL_RECOV .context = IN_KERNEL_RECOV #define SER .ser = SER_REQUIRED #define NOSER .ser = NO_SER #define EXCP .excp = EXCP_CONTEXT Loading Loading @@ -86,6 +88,10 @@ static struct severity { PANIC, "In kernel and no restart IP", EXCP, KERNEL, MCGMASK(MCG_STATUS_RIPV, 0) ), MCESEV( PANIC, "In kernel and no restart IP", EXCP, KERNEL_RECOV, MCGMASK(MCG_STATUS_RIPV, 0) ), MCESEV( DEFERRED, "Deferred error", NOSER, MASK(MCI_STATUS_UC|MCI_STATUS_DEFERRED|MCI_STATUS_POISON, MCI_STATUS_DEFERRED) Loading Loading @@ -122,6 +128,11 @@ static struct severity { SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR, MCI_UC_SAR|MCI_ADDR), MCGMASK(MCG_STATUS_RIPV|MCG_STATUS_EIPV, MCG_STATUS_RIPV) ), MCESEV( AR, "Action required: data load in error recoverable area of kernel", SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA), KERNEL_RECOV ), MCESEV( AR, "Action required: data load error in a user process", SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA), Loading Loading @@ -170,6 +181,9 @@ static struct severity { ) /* always matches. keep at end */ }; #define mc_recoverable(mcg) (((mcg) & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) == \ (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) /* * If mcgstatus indicated that ip/cs on the stack were * no good, then "m->cs" will be zero and we will have Loading @@ -183,7 +197,11 @@ static struct severity { */ static int error_context(struct mce *m) { return ((m->cs & 3) == 3) ? IN_USER : IN_KERNEL; if ((m->cs & 3) == 3) return IN_USER; if (mc_recoverable(m->mcgstatus) && ex_has_fault_handler(m->ip)) return IN_KERNEL_RECOV; return IN_KERNEL; } /* Loading Loading
Documentation/x86/exception-tables.txt +35 −0 Original line number Diff line number Diff line Loading @@ -290,3 +290,38 @@ Due to the way that the exception table is built and needs to be ordered, only use exceptions for code in the .text section. Any other section will cause the exception table to not be sorted correctly, and the exceptions will fail. Things changed when 64-bit support was added to x86 Linux. Rather than double the size of the exception table by expanding the two entries from 32-bits to 64 bits, a clever trick was used to store addresses as relative offsets from the table itself. The assembly code changed from: .long 1b,3b to: .long (from) - . .long (to) - . and the C-code that uses these values converts back to absolute addresses like this: ex_insn_addr(const struct exception_table_entry *x) { return (unsigned long)&x->insn + x->insn; } In v4.6 the exception table entry was expanded with a new field "handler". This is also 32-bits wide and contains a third relative function pointer which points to one of: 1) int ex_handler_default(const struct exception_table_entry *fixup) This is legacy case that just jumps to the fixup code 2) int ex_handler_fault(const struct exception_table_entry *fixup) This case provides the fault number of the trap that occurred at entry->insn. It is used to distinguish page faults from machine check. 3) int ex_handler_ext(const struct exception_table_entry *fixup) This case is used for uaccess_err ... we need to set a flag in the task structure. Before the handler functions existed this case was handled by adding a large offset to the fixup to tag it as special. More functions can easily be added.
arch/x86/include/asm/asm.h +24 −16 Original line number Diff line number Diff line Loading @@ -44,19 +44,22 @@ /* Exception table entry */ #ifdef __ASSEMBLY__ # define _ASM_EXTABLE(from,to) \ # define _ASM_EXTABLE_HANDLE(from, to, handler) \ .pushsection "__ex_table","a" ; \ .balign 8 ; \ .balign 4 ; \ .long (from) - . ; \ .long (to) - . ; \ .long (handler) - . ; \ .popsection # define _ASM_EXTABLE(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_default) # define _ASM_EXTABLE_FAULT(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_fault) # define _ASM_EXTABLE_EX(from, to) \ .pushsection "__ex_table","a" ; \ .balign 8 ; \ .long (from) - . ; \ .long (to) - . + 0x7ffffff0 ; \ .popsection _ASM_EXTABLE_HANDLE(from, to, ex_handler_ext) # define _ASM_NOKPROBE(entry) \ .pushsection "_kprobe_blacklist","aw" ; \ Loading Loading @@ -89,19 +92,24 @@ .endm #else # define _ASM_EXTABLE(from,to) \ # define _EXPAND_EXTABLE_HANDLE(x) #x # define _ASM_EXTABLE_HANDLE(from, to, handler) \ " .pushsection \"__ex_table\",\"a\"\n" \ " .balign 8\n" \ " .balign 4\n" \ " .long (" #from ") - .\n" \ " .long (" #to ") - .\n" \ " .long (" _EXPAND_EXTABLE_HANDLE(handler) ") - .\n" \ " .popsection\n" # define _ASM_EXTABLE(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_default) # define _ASM_EXTABLE_FAULT(from, to) \ _ASM_EXTABLE_HANDLE(from, to, ex_handler_fault) # define _ASM_EXTABLE_EX(from, to) \ " .pushsection \"__ex_table\",\"a\"\n" \ " .balign 8\n" \ " .long (" #from ") - .\n" \ " .long (" #to ") - . + 0x7ffffff0\n" \ " .popsection\n" _ASM_EXTABLE_HANDLE(from, to, ex_handler_ext) /* For C file, we already have NOKPROBE_SYMBOL macro */ #endif Loading
arch/x86/include/asm/msr-index.h +4 −0 Original line number Diff line number Diff line Loading @@ -269,6 +269,10 @@ #define MSR_IA32_MC0_CTL2 0x00000280 #define MSR_IA32_MCx_CTL2(x) (MSR_IA32_MC0_CTL2 + (x)) /* 'SMCA': AMD64 Scalable MCA */ #define MSR_AMD64_SMCA_MC0_CONFIG 0xc0002004 #define MSR_AMD64_SMCA_MCx_CONFIG(x) (MSR_AMD64_SMCA_MC0_CONFIG + 0x10*(x)) #define MSR_P6_PERFCTR0 0x000000c1 #define MSR_P6_PERFCTR1 0x000000c2 #define MSR_P6_EVNTSEL0 0x00000186 Loading
arch/x86/include/asm/uaccess.h +8 −8 Original line number Diff line number Diff line Loading @@ -90,12 +90,11 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un likely(!__range_not_ok(addr, size, user_addr_max())) /* * The exception table consists of pairs of addresses relative to the * exception table enty itself: the first is the address of an * instruction that is allowed to fault, and the second is the address * at which the program should continue. No registers are modified, * so it is entirely up to the continuation code to figure out what to * do. * The exception table consists of triples of addresses relative to the * exception table entry itself. The first address is of an instruction * that is allowed to fault, the second is the target at which the program * should continue. The third is a handler function to deal with the fault * caused by the instruction in the first field. * * All the routines below use bits of fixup code that are out of line * with the main instruction path. This means when everything is well, Loading @@ -104,13 +103,14 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un */ struct exception_table_entry { int insn, fixup; int insn, fixup, handler; }; /* This is not the generic standard exception_table_entry format */ #define ARCH_HAS_SORT_EXTABLE #define ARCH_HAS_SEARCH_EXTABLE extern int fixup_exception(struct pt_regs *regs); extern int fixup_exception(struct pt_regs *regs, int trapnr); extern bool ex_has_fault_handler(unsigned long ip); extern int early_fixup_exception(unsigned long *ip); /* Loading
arch/x86/kernel/cpu/mcheck/mce-severity.c +20 −2 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ #include <linux/init.h> #include <linux/debugfs.h> #include <asm/mce.h> #include <asm/uaccess.h> #include "mce-internal.h" Loading @@ -29,7 +30,7 @@ * panic situations) */ enum context { IN_KERNEL = 1, IN_USER = 2 }; enum context { IN_KERNEL = 1, IN_USER = 2, IN_KERNEL_RECOV = 3 }; enum ser { SER_REQUIRED = 1, NO_SER = 2 }; enum exception { EXCP_CONTEXT = 1, NO_EXCP = 2 }; Loading @@ -48,6 +49,7 @@ static struct severity { #define MCESEV(s, m, c...) { .sev = MCE_ ## s ## _SEVERITY, .msg = m, ## c } #define KERNEL .context = IN_KERNEL #define USER .context = IN_USER #define KERNEL_RECOV .context = IN_KERNEL_RECOV #define SER .ser = SER_REQUIRED #define NOSER .ser = NO_SER #define EXCP .excp = EXCP_CONTEXT Loading Loading @@ -86,6 +88,10 @@ static struct severity { PANIC, "In kernel and no restart IP", EXCP, KERNEL, MCGMASK(MCG_STATUS_RIPV, 0) ), MCESEV( PANIC, "In kernel and no restart IP", EXCP, KERNEL_RECOV, MCGMASK(MCG_STATUS_RIPV, 0) ), MCESEV( DEFERRED, "Deferred error", NOSER, MASK(MCI_STATUS_UC|MCI_STATUS_DEFERRED|MCI_STATUS_POISON, MCI_STATUS_DEFERRED) Loading Loading @@ -122,6 +128,11 @@ static struct severity { SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR, MCI_UC_SAR|MCI_ADDR), MCGMASK(MCG_STATUS_RIPV|MCG_STATUS_EIPV, MCG_STATUS_RIPV) ), MCESEV( AR, "Action required: data load in error recoverable area of kernel", SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA), KERNEL_RECOV ), MCESEV( AR, "Action required: data load error in a user process", SER, MASK(MCI_STATUS_OVER|MCI_UC_SAR|MCI_ADDR|MCACOD, MCI_UC_SAR|MCI_ADDR|MCACOD_DATA), Loading Loading @@ -170,6 +181,9 @@ static struct severity { ) /* always matches. keep at end */ }; #define mc_recoverable(mcg) (((mcg) & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) == \ (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) /* * If mcgstatus indicated that ip/cs on the stack were * no good, then "m->cs" will be zero and we will have Loading @@ -183,7 +197,11 @@ static struct severity { */ static int error_context(struct mce *m) { return ((m->cs & 3) == 3) ? IN_USER : IN_KERNEL; if ((m->cs & 3) == 3) return IN_USER; if (mc_recoverable(m->mcgstatus) && ex_has_fault_handler(m->ip)) return IN_KERNEL_RECOV; return IN_KERNEL; } /* Loading