Skip to content

Commit 0888e10

Browse files
davejiangsuryasaimadhu
authored andcommitted
x86/asm: Carve out a generic movdir64b() helper for general usage
Carve out the MOVDIR64B inline asm primitive into a generic helper so that it can be used by other functions. Move it to special_insns.h and have iosubmit_cmds512() call it. [ bp: Massage commit message. ] Suggested-by: Michael Matz <matz@suse.de> Signed-off-by: Dave Jiang <dave.jiang@intel.com> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Tony Luck <tony.luck@intel.com> Reviewed-by: Borislav Petkov <bp@suse.de> Link: https://lkml.kernel.org/r/20201005151126.657029-2-dave.jiang@intel.com
1 parent 20f0afd commit 0888e10

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

arch/x86/include/asm/io.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ extern bool phys_mem_access_encrypted(unsigned long phys_addr,
401401

402402
/**
403403
* iosubmit_cmds512 - copy data to single MMIO location, in 512-bit units
404-
* @__dst: destination, in MMIO space (must be 512-bit aligned)
404+
* @dst: destination, in MMIO space (must be 512-bit aligned)
405405
* @src: source
406406
* @count: number of 512 bits quantities to submit
407407
*
@@ -412,25 +412,14 @@ extern bool phys_mem_access_encrypted(unsigned long phys_addr,
412412
* Warning: Do not use this helper unless your driver has checked that the CPU
413413
* instruction is supported on the platform.
414414
*/
415-
static inline void iosubmit_cmds512(void __iomem *__dst, const void *src,
415+
static inline void iosubmit_cmds512(void __iomem *dst, const void *src,
416416
size_t count)
417417
{
418-
/*
419-
* Note that this isn't an "on-stack copy", just definition of "dst"
420-
* as a pointer to 64-bytes of stuff that is going to be overwritten.
421-
* In the MOVDIR64B case that may be needed as you can use the
422-
* MOVDIR64B instruction to copy arbitrary memory around. This trick
423-
* lets the compiler know how much gets clobbered.
424-
*/
425-
volatile struct { char _[64]; } *dst = __dst;
426418
const u8 *from = src;
427419
const u8 *end = from + count * 64;
428420

429421
while (from < end) {
430-
/* MOVDIR64B [rdx], rax */
431-
asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
432-
: "=m" (dst)
433-
: "d" (from), "a" (dst));
422+
movdir64b(dst, from);
434423
from += 64;
435424
}
436425
}

arch/x86/include/asm/special_insns.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,28 @@ static inline void clwb(volatile void *__p)
234234

235235
#define nop() asm volatile ("nop")
236236

237+
/* The dst parameter must be 64-bytes aligned */
238+
static inline void movdir64b(void *dst, const void *src)
239+
{
240+
const struct { char _[64]; } *__src = src;
241+
struct { char _[64]; } *__dst = dst;
242+
243+
/*
244+
* MOVDIR64B %(rdx), rax.
245+
*
246+
* Both __src and __dst must be memory constraints in order to tell the
247+
* compiler that no other memory accesses should be reordered around
248+
* this one.
249+
*
250+
* Also, both must be supplied as lvalues because this tells
251+
* the compiler what the object is (its size) the instruction accesses.
252+
* I.e., not the pointers but what they point to, thus the deref'ing '*'.
253+
*/
254+
asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
255+
: "+m" (*__dst)
256+
: "m" (*__src), "a" (__dst), "d" (__src));
257+
}
258+
237259
#endif /* __KERNEL__ */
238260

239261
#endif /* _ASM_X86_SPECIAL_INSNS_H */

0 commit comments

Comments
 (0)