1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_DEBUGREG_H
3 #define _ASM_X86_DEBUGREG_H
6 #include <linux/percpu.h>
7 #include <uapi/asm/debugreg.h>
9 DECLARE_PER_CPU(unsigned long, cpu_dr7);
11 #ifndef CONFIG_PARAVIRT_XXL
13 * These special macros can be used to get or set a debugging register
15 #define get_debugreg(var, register) \
16 (var) = native_get_debugreg(register)
17 #define set_debugreg(value, register) \
18 native_set_debugreg(register, value)
21 static __always_inline unsigned long native_get_debugreg(int regno)
23 unsigned long val = 0; /* Damn you, gcc! */
27 asm("mov %%db0, %0" :"=r" (val));
30 asm("mov %%db1, %0" :"=r" (val));
33 asm("mov %%db2, %0" :"=r" (val));
36 asm("mov %%db3, %0" :"=r" (val));
39 asm("mov %%db6, %0" :"=r" (val));
43 * Apply __FORCE_ORDER to DR7 reads to forbid re-ordering them
46 * This is needed because a DR7 access can cause a #VC exception
47 * when running under SEV-ES. Taking a #VC exception is not a
48 * safe thing to do just anywhere in the entry code and
49 * re-ordering might place the access into an unsafe location.
51 * This happened in the NMI handler, where the DR7 read was
52 * re-ordered to happen before the call to sev_es_ist_enter(),
53 * causing stack recursion.
55 asm volatile("mov %%db7, %0" : "=r" (val) : __FORCE_ORDER);
63 static __always_inline void native_set_debugreg(int regno, unsigned long value)
67 asm("mov %0, %%db0" ::"r" (value));
70 asm("mov %0, %%db1" ::"r" (value));
73 asm("mov %0, %%db2" ::"r" (value));
76 asm("mov %0, %%db3" ::"r" (value));
79 asm("mov %0, %%db6" ::"r" (value));
83 * Apply __FORCE_ORDER to DR7 writes to forbid re-ordering them
86 * While is didn't happen with a DR7 write (see the DR7 read
87 * comment above which explains where it happened), add the
88 * __FORCE_ORDER here too to avoid similar problems in the
91 asm volatile("mov %0, %%db7" ::"r" (value), __FORCE_ORDER);
98 static inline void hw_breakpoint_disable(void)
100 /* Zero the control register for HW Breakpoint */
101 set_debugreg(0UL, 7);
103 /* Zero-out the individual HW breakpoint address registers */
104 set_debugreg(0UL, 0);
105 set_debugreg(0UL, 1);
106 set_debugreg(0UL, 2);
107 set_debugreg(0UL, 3);
110 static __always_inline bool hw_breakpoint_active(void)
112 return __this_cpu_read(cpu_dr7) & DR_GLOBAL_ENABLE_MASK;
115 extern void hw_breakpoint_restore(void);
117 static __always_inline unsigned long local_db_save(void)
121 if (static_cpu_has(X86_FEATURE_HYPERVISOR) && !hw_breakpoint_active())
124 get_debugreg(dr7, 7);
125 dr7 &= ~0x400; /* architecturally set bit */
129 * Ensure the compiler doesn't lower the above statements into
130 * the critical section; disabling breakpoints late would not
138 static __always_inline void local_db_restore(unsigned long dr7)
141 * Ensure the compiler doesn't raise this statement into
142 * the critical section; enabling breakpoints early would
147 set_debugreg(dr7, 7);
150 #ifdef CONFIG_CPU_SUP_AMD
151 extern void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr);
152 extern unsigned long amd_get_dr_addr_mask(unsigned int dr);
154 static inline void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr) { }
155 static inline unsigned long amd_get_dr_addr_mask(unsigned int dr)
161 #endif /* _ASM_X86_DEBUGREG_H */