From: Andrew Cooper Date: Mon, 13 May 2019 10:26:22 +0000 (+0000) Subject: Introduce addr_t for linear addresses in hardware registers/datastructrues X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=refs%2Fheads%2Fdevel-misc;p=people%2Fandrewcoop%2Fxen-test-framework.git Introduce addr_t for linear addresses in hardware registers/datastructrues Update write_dr[0-3]() to use the new functionality, and drop the casts at the callsites. Signed-off-by: Andrew Cooper --- diff --git a/Doxyfile b/Doxyfile index f63d04f..3427804 100644 --- a/Doxyfile +++ b/Doxyfile @@ -2262,6 +2262,7 @@ PREDEFINED = __alias(x)= \ __maybe_unused \ __noinline \ __noreturn \ + __transparent \ __used \ __user_data \ __user_page_aligned_bss \ diff --git a/arch/x86/include/arch/x86-dbg-reg.h b/arch/x86/include/arch/x86-dbg-reg.h index b70185d..1353ea8 100644 --- a/arch/x86/include/arch/x86-dbg-reg.h +++ b/arch/x86/include/arch/x86-dbg-reg.h @@ -108,9 +108,9 @@ static inline unsigned long read_dr0(void) return val; } -static inline void write_dr0(unsigned long linear) +static inline void write_dr0(addr_t linear) { - asm volatile ("mov %0, %%dr0" :: "r" (linear)); + asm volatile ("mov %0, %%dr0" :: "r" (linear.val)); } static inline unsigned long read_dr1(void) @@ -122,9 +122,9 @@ static inline unsigned long read_dr1(void) return val; } -static inline void write_dr1(unsigned long linear) +static inline void write_dr1(addr_t linear) { - asm volatile ("mov %0, %%dr1" :: "r" (linear)); + asm volatile ("mov %0, %%dr1" :: "r" (linear.val)); } static inline unsigned long read_dr2(void) @@ -136,9 +136,9 @@ static inline unsigned long read_dr2(void) return val; } -static inline void write_dr2(unsigned long linear) +static inline void write_dr2(addr_t linear) { - asm volatile ("mov %0, %%dr2" :: "r" (linear)); + asm volatile ("mov %0, %%dr2" :: "r" (linear.val)); } static inline unsigned long read_dr3(void) @@ -150,9 +150,9 @@ static inline unsigned long read_dr3(void) return val; } -static inline void write_dr3(unsigned long linear) +static inline void write_dr3(addr_t linear) { - asm volatile ("mov %0, %%dr3" :: "r" (linear)); + asm volatile ("mov %0, %%dr3" :: "r" (linear.val)); } static inline unsigned long read_dr6(void) diff --git a/include/xtf/compiler.h b/include/xtf/compiler.h index 19d9349..1c24d9b 100644 --- a/include/xtf/compiler.h +++ b/include/xtf/compiler.h @@ -11,6 +11,7 @@ #define __packed __attribute__((__packed__)) #define __printf(f, v) __attribute__((__format__(__printf__, f, v))) #define __maybe_unused __attribute__((__unused__)) +#define __transparent __attribute__((__transparent_union__)) #define __used __attribute__((__used__)) #define __weak __attribute__((__weak__)) diff --git a/include/xtf/types.h b/include/xtf/types.h index a2ab15e..a4c46b4 100644 --- a/include/xtf/types.h +++ b/include/xtf/types.h @@ -23,6 +23,18 @@ */ extern char zeroptr[]; +/** + * Type (ab)use for helpers which take a linear address, and would like to + * accept it in either pointer or integer form. + * + * Useful for programming hardware registers and datastructures to point to a + * specific C object/function, given the flat memory layout. + */ +typedef union { + unsigned long val; + void *ptr; +} __attribute__((__transparent_union__)) addr_t; + #endif /* !__ASSEMBLY__ */ #endif /* XTF_TYPES_H */ diff --git a/tests/debug-regs/main.c b/tests/debug-regs/main.c index 5e5cd84..d4b794a 100644 --- a/tests/debug-regs/main.c +++ b/tests/debug-regs/main.c @@ -99,7 +99,7 @@ static void test_pv_dr7_latch(void) write_dr7(0); /* Point %dr0 at dummy, %dr7 set with %dr0 enabled. */ - write_dr0(_u(&dummy)); + write_dr0(&dummy); dr7 = X86_DR7_GE | DR7_SYM(0, G, RW, 32); /* diff --git a/tests/xsa-260/main.c b/tests/xsa-260/main.c index cdf5274..d0d480c 100644 --- a/tests/xsa-260/main.c +++ b/tests/xsa-260/main.c @@ -140,7 +140,7 @@ void test_main(void) { unsigned int ss = read_ss(); - write_dr0(_u(&ss)); + write_dr0(&ss); unsigned long dr7 = DR7_SYM(0, L, G, RW, 32) | X86_DR7_LE | X86_DR7_GE; @@ -162,7 +162,7 @@ void test_main(void) exp, _p(exp), fault, _p(fault)); /* Prime the user code for its exploit attempt. */ - write_dr0(_u(&user_ss)); + write_dr0(&user_ss); printk("Testing native syscall\n"); exec_user_void(user_syscall); diff --git a/tests/xsa-265/main.c b/tests/xsa-265/main.c index 2bb5f5c..1bb1609 100644 --- a/tests/xsa-265/main.c +++ b/tests/xsa-265/main.c @@ -31,7 +31,7 @@ void test_main(void) write_dr6(X86_DR6_BD); /* Data breakpoint for `ss`, working around Xen's %dr7 latching bug. */ - write_dr0(_u(&ss)); + write_dr0(&ss); write_dr7(dr7); write_dr7(dr7);