Module Name:    src
Committed By:   sborrill
Date:           Mon Jun  1 14:43:48 UTC 2015

Modified Files:
        src/sys/arch/amd64/amd64 [netbsd-5-1]: cpufunc.S
        src/sys/arch/i386/i386 [netbsd-5-1]: cpufunc.S
        src/sys/arch/x86/include [netbsd-5-1]: cpufunc.h

Log Message:
Pull up the following revisions(s) (requested by msaitoh in ticket #1969):
        sys/arch/x86/include/cpufunc.h: revision 1.13
        sys/arch/amd64/amd64/cpufunc.S: revision 1.20-1.21 via patch
        sys/arch/i386/i386/cpufunc.S:   revision 1.16-1.17, 1.21 via patch

Backport rdmsr_safe() to access MSR safely.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.15.16.1 src/sys/arch/amd64/amd64/cpufunc.S
cvs rdiff -u -r1.13 -r1.13.10.1 src/sys/arch/i386/i386/cpufunc.S
cvs rdiff -u -r1.8.10.3 -r1.8.10.3.6.1 src/sys/arch/x86/include/cpufunc.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/cpufunc.S
diff -u src/sys/arch/amd64/amd64/cpufunc.S:1.15 src/sys/arch/amd64/amd64/cpufunc.S:1.15.16.1
--- src/sys/arch/amd64/amd64/cpufunc.S:1.15	Tue Jun 24 16:32:53 2008
+++ src/sys/arch/amd64/amd64/cpufunc.S	Mon Jun  1 14:43:47 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpufunc.S,v 1.15 2008/06/24 16:32:53 ad Exp $	*/
+/*	$NetBSD: cpufunc.S,v 1.15.16.1 2015/06/01 14:43:47 sborrill Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2007, 2008 The NetBSD Foundation, Inc.
@@ -33,6 +33,8 @@
  * Functions to provide access to i386-specific instructions.
  */
 
+#include <sys/errno.h>
+
 #include <machine/asm.h>
 #include <machine/specialreg.h>
 #include <machine/segments.h>
@@ -215,6 +217,37 @@ ENTRY(wrmsr_locked)
 	wrmsr
 	ret
 
+/*
+ * Support for reading MSRs in the safe manner (returns EFAULT on fault)
+ */
+/* int rdmsr_safe(u_int msr, uint64_t *data) */
+ENTRY(rdmsr_safe)
+	movq	CPUVAR(CURLWP), %r8
+	movq	L_ADDR(%r8), %r8
+	movq	$_C_LABEL(msr_onfault), PCB_ONFAULT(%r8)
+
+	movl	%edi, %ecx /* u_int msr */
+	rdmsr			/* Read MSR pointed by %ecx. Returns
+				   hi byte in edx, lo in %eax */
+	salq	$32, %rdx	/* sign-shift %rdx left */
+	movl	%eax, %eax	/* zero-extend %eax -> %rax */
+	orq	%rdx, %rax
+	movq	%rax, (%rsi)  /* *data */
+	xorq	%rax, %rax    /* "no error" */
+
+	movq	%rax, PCB_ONFAULT(%r8)
+	ret
+
+/*
+ * MSR operations fault handler
+ */
+NENTRY(msr_onfault)
+	movq	CPUVAR(CURLWP), %r8
+	movq	L_ADDR(%r8), %r8
+	movq	$0, PCB_ONFAULT(%r8)
+	movl	$EFAULT, %eax
+	ret
+
 #ifndef XEN
 ENTRY(wbinvd)
 	wbinvd

Index: src/sys/arch/i386/i386/cpufunc.S
diff -u src/sys/arch/i386/i386/cpufunc.S:1.13 src/sys/arch/i386/i386/cpufunc.S:1.13.10.1
--- src/sys/arch/i386/i386/cpufunc.S:1.13	Tue Sep 23 08:50:11 2008
+++ src/sys/arch/i386/i386/cpufunc.S	Mon Jun  1 14:43:48 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpufunc.S,v 1.13 2008/09/23 08:50:11 ad Exp $	*/
+/*	$NetBSD: cpufunc.S,v 1.13.10.1 2015/06/01 14:43:48 sborrill Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
@@ -35,8 +35,10 @@
  * These are shared with NetBSD/xen.
  */
 
+#include <sys/errno.h>
+
 #include <machine/asm.h>
-__KERNEL_RCSID(0, "$NetBSD: cpufunc.S,v 1.13 2008/09/23 08:50:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpufunc.S,v 1.13.10.1 2015/06/01 14:43:48 sborrill Exp $");
 
 #include "opt_xen.h"
 
@@ -141,6 +143,39 @@ ENTRY(wrmsr_locked)
 	ret
 END(wrmsr_locked)
 
+/*
+ * Support for reading MSRs in the safe manner (returns EFAULT on fault)
+ */
+/* int rdmsr_safe(u_int msr, uint64_t *data) */
+ENTRY(rdmsr_safe)
+	movl	CPUVAR(CURLWP), %ecx
+	movl	L_ADDR(%ecx), %ecx
+	movl	$_C_LABEL(msr_onfault), PCB_ONFAULT(%ecx)
+
+	movl	4(%esp), %ecx /* u_int msr */
+	rdmsr
+	movl	8(%esp), %ecx /* *data */
+	movl	%eax, (%ecx)  /* low-order bits */
+	movl	%edx, 4(%ecx) /* high-order bits */
+	xorl	%eax, %eax    /* "no error" */
+
+	movl	CPUVAR(CURLWP), %ecx
+	movl	L_ADDR(%ecx), %ecx
+	movl	%eax, PCB_ONFAULT(%ecx)
+
+	ret
+
+/*
+ * MSR operations fault handler
+ */
+NENTRY(msr_onfault)
+	movl	CPUVAR(CURLWP), %ecx
+	movl	L_ADDR(%ecx), %ecx
+	movl	$0, PCB_ONFAULT(%ecx)
+	movl	$EFAULT, %eax
+	ret
+END(msr_onfault)
+
 ENTRY(cpu_counter)
 	rdtsc
 	addl	CPUVAR(CC_SKEW), %eax

Index: src/sys/arch/x86/include/cpufunc.h
diff -u src/sys/arch/x86/include/cpufunc.h:1.8.10.3 src/sys/arch/x86/include/cpufunc.h:1.8.10.3.6.1
--- src/sys/arch/x86/include/cpufunc.h:1.8.10.3	Mon Feb  2 21:38:50 2009
+++ src/sys/arch/x86/include/cpufunc.h	Mon Jun  1 14:43:48 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpufunc.h,v 1.8.10.3 2009/02/02 21:38:50 snj Exp $	*/
+/*	$NetBSD: cpufunc.h,v 1.8.10.3.6.1 2015/06/01 14:43:48 sborrill Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
@@ -118,6 +118,7 @@ void	x86_reset(void);
 
 uint64_t	rdmsr(u_int);
 uint64_t	rdmsr_locked(u_int, u_int);
+int		rdmsr_safe(u_int, uint64_t *);
 uint64_t	rdtsc(void);
 uint64_t	rdpmc(u_int);
 void		wrmsr(u_int, uint64_t);

Reply via email to