Skip to contentSkip to conversation

x86/asm: Add support for MOVDIR64B instruction

PATCH Alexey Dobriyan 6 participants 10 messages Aug 1, 2019 – Aug 5, 2019

lore
Share & tools
lore
#0 lore raw

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

#1 lore raw
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

{
	struct dstbytes {
		char pad[64];
	};

	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}

Or did you have something else in mind?

-Tony

#2 lore raw
Quoted 27 lines · Tony
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

{
	struct dstbytes {
		char pad[64];
	};

	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}
Quoted 1 line

Or did you have something else in mind?

Or should we add "memory" clobber instead, like we do for string operations?

static inline void movdir64b(void *dst, const void *src)

{
	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
			: : "d" (src), "a" (dst) : "memory");
}
Signature · 1 line
 Kirill A. Shutemov
#3 lore raw
Quoted 28 lines · Kirill A. Shutemov
Quoted 27 lines · Tony
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

{
	struct dstbytes {
		char pad[64];
	};

	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}
Quoted 4 lines
Quoted 1 line

Or did you have something else in mind?

Or should we add "memory" clobber instead, like we do for string operations?

I think Tony's in the right direction. We already do dst "sizing" like that for the compiler in clwb().

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#4 lore raw
Quoted 2 lines

I think Tony's in the right direction. We already do dst "sizing" like that for the compiler in clwb().

The clwb case does look like what we want for movdir64b().

But is it right for clwb() ... that doesn't modify anything, just pushes things from cache to memory. So why is it using "+m"?

-Tony

#5 lore raw
Quoted 7 lines · Tony
Quoted 2 lines

I think Tony's in the right direction. We already do dst "sizing" like that for the compiler in clwb().

The clwb case does look like what we want for movdir64b().

But is it right for clwb() ... that doesn't modify anything, just pushes things from cache to memory. So why is it using "+m"?

Here some hints from to my notes, if you want to know more detail, I can ping my gcc guy.

It needs to be an input and an output operand so that it prevents gcc from reordering accesses to it after the insn happens, i.e., you don't want to touch it after CLFLUSH has executed.

And also, you want to make sure it works with all gcc versions and this is, I was told, the right way to do it. For example, some gcc versions consider it not limited to 64 bytes of memory being touched but a full memory clobber.

HTH.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#6 lore raw

+Dave, who is the DSA developer.

Thanks,
Jing

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: Friday, August 2, 2019 7:41 AM
To: Luck, Tony <[email protected]>
Cc: Kirill A. Shutemov <[email protected]>; Alexey Dobriyan <[email protected]>; [email protected]; [email protected]; Lin, Jing <[email protected]>; [email protected]
Subject: Re: [PATCH] x86/asm: Add support for MOVDIR64B instruction

Quoted 7 lines · Tony
Quoted 2 lines

I think Tony's in the right direction. We already do dst "sizing" like that for the compiler in clwb().

The clwb case does look like what we want for movdir64b().

But is it right for clwb() ... that doesn't modify anything, just pushes things from cache to memory. So why is it using "+m"?

Here some hints from to my notes, if you want to know more detail, I can ping my gcc guy.

It needs to be an input and an output operand so that it prevents gcc from reordering accesses to it after the insn happens, i.e., you don't want to touch it after CLFLUSH has executed.

And also, you want to make sure it works with all gcc versions and this is, I was told, the right way to do it. For example, some gcc versions consider it not limited to 64 bytes of memory being touched but a full memory clobber.

HTH.

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

#7 lore raw
In reply to Luck, Tony
Quoted 27 lines · Tony
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

{
	struct dstbytes {
		char pad[64];
	};

	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}
Quoted 1 line

Or did you have something else in mind?

Well, it doesn't need a name:

	: "=m" (*(struct{char _[64];}*)dst)

But yes, something like that.

Can't cast to "char[64]" :-(

#8 lore raw
In reply to Luck, Tony
Quoted 16 lines · Tony
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

Quoted 10 lines
{
	struct dstbytes {
		char pad[64];
	};

	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}

Can the source and destination overlap? The SDM doesn't seem to mention this.

Also, it bugs me something fierce that this provides a single-copy-atomic 64b store, but there is no matching load operation.

#9 lore raw
Quoted 22 lines · Peter Zijlstra
Quoted 21 lines · Tony
Quoted 11 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline void movdir64b(void *dst, const void *src)
+{
+	/* movdir64b [rdx], rax */
+	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
+			: "=m" (*(char *)dst)
                               ^^^^^^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+			: "d" (src), "a" (dst));
+}

Probably needs fake 64-byte type, so that compiler knows what is dirty.

Would that be something like this?

static inline void movdir64b(void *dst, const void *src)

{
	struct dstbytes {
		char pad[64];
	};
Quoted 8 lines
Quoted 5 lines
	/* movdir64b [rdx], rax */
	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
		     : "=m" (*(struct dstbytes *)dst)
		     : "d" (src), "a" (dst));
}

Can the source and destination overlap? The SDM doesn't seem to mention this.

Good question. I'll ask to clarify this.

Signature · 1 line
 Kirill A. Shutemov