Module Name: src
Committed By: martin
Date: Sat Apr 14 10:11:49 UTC 2018
Modified Files:
src/sys/arch/amd64/amd64 [netbsd-8]: amd64_trap.S copy.S trap.c
src/sys/arch/amd64/include [netbsd-8]: frameasm.h
src/sys/arch/x86/x86 [netbsd-8]: cpu.c patch.c
Log Message:
Pullup the following revisions via patch, requested by maxv in ticket #748:
sys/arch/amd64/amd64/copy.S 1.29 (adapted, via patch)
sys/arch/amd64/amd64/amd64_trap.S 1.16,1.19 (partial) (via patch)
sys/arch/amd64/amd64/trap.c 1.102,1.106 (partial),1.110 (via patch)
sys/arch/amd64/include/frameasm.h 1.22,1.24 (via patch)
sys/arch/x86/x86/cpu.c 1.137 (via patch)
sys/arch/x86/x86/patch.c 1.23,1.26 (partial) (via patch)
Backport of SMAP support.
To generate a diff of this commit:
cvs rdiff -u -r1.5.6.2 -r1.5.6.3 src/sys/arch/amd64/amd64/amd64_trap.S
cvs rdiff -u -r1.20.10.2 -r1.20.10.3 src/sys/arch/amd64/amd64/copy.S
cvs rdiff -u -r1.96.4.3 -r1.96.4.4 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.20.32.2 -r1.20.32.3 src/sys/arch/amd64/include/frameasm.h
cvs rdiff -u -r1.130.2.5 -r1.130.2.6 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.22.22.1 -r1.22.22.2 src/sys/arch/x86/x86/patch.c
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/amd64_trap.S
diff -u src/sys/arch/amd64/amd64/amd64_trap.S:1.5.6.2 src/sys/arch/amd64/amd64/amd64_trap.S:1.5.6.3
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.5.6.2 Thu Mar 22 16:59:03 2018
+++ src/sys/arch/amd64/amd64/amd64_trap.S Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: amd64_trap.S,v 1.5.6.2 2018/03/22 16:59:03 martin Exp $ */
+/* $NetBSD: amd64_trap.S,v 1.5.6.3 2018/04/14 10:11:49 martin Exp $ */
/*
* Copyright (c) 1998, 2007, 2008, 2017 The NetBSD Foundation, Inc.
@@ -136,6 +136,7 @@ IDTVEC(trap02)
INTR_SAVE_GPRS
SVS_ENTER_ALTSTACK
cld
+ SMAP_ENABLE
movw %gs,TF_GS(%rsp)
movw %fs,TF_FS(%rsp)
movw %es,TF_ES(%rsp)
@@ -236,6 +237,7 @@ IDTVEC(trap08)
swapgs
1:
cld
+ SMAP_ENABLE
movw %gs,TF_GS(%rsp)
movw %fs,TF_FS(%rsp)
movw %es,TF_ES(%rsp)
Index: src/sys/arch/amd64/amd64/copy.S
diff -u src/sys/arch/amd64/amd64/copy.S:1.20.10.2 src/sys/arch/amd64/amd64/copy.S:1.20.10.3
--- src/sys/arch/amd64/amd64/copy.S:1.20.10.2 Mon Feb 26 00:49:48 2018
+++ src/sys/arch/amd64/amd64/copy.S Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: copy.S,v 1.20.10.2 2018/02/26 00:49:48 snj Exp $ */
+/* $NetBSD: copy.S,v 1.20.10.3 2018/04/14 10:11:49 martin Exp $ */
/*
* Copyright (c) 2001 Wasabi Systems, Inc.
@@ -187,6 +187,7 @@ ENTRY(copyout)
cmpq %r8,%rdx
ja _C_LABEL(copy_efault) /* jump if end in kernel space */
+ SMAP_DISABLE
.Lcopyout_start:
movq %rax,%rcx /* length */
shrq $3,%rcx /* count of 8-byte words */
@@ -197,6 +198,7 @@ ENTRY(copyout)
rep
movsb /* copy remaining bytes */
.Lcopyout_end:
+ SMAP_ENABLE
xorl %eax,%eax
ret
@@ -215,6 +217,7 @@ ENTRY(copyin)
cmpq %r8,%rdx
ja _C_LABEL(copy_efault) /* j if end in kernel space */
+ SMAP_DISABLE
.Lcopyin_start:
3: /* bcopy(%rsi, %rdi, %rax); */
movq %rax,%rcx
@@ -226,6 +229,7 @@ ENTRY(copyin)
rep
movsb
.Lcopyin_end:
+ SMAP_ENABLE
xorl %eax,%eax
ret
@@ -242,6 +246,7 @@ NENTRY(kcopy_fault)
END(kcopy_fault)
NENTRY(copy_fault)
+ SMAP_ENABLE
ret
END(copy_fault)
@@ -263,6 +268,7 @@ ENTRY(copyoutstr)
movq %rax,%r8
1: incq %rdx
+ SMAP_DISABLE
.Lcopyoutstr_start:
1: decq %rdx
jz 2f
@@ -271,6 +277,7 @@ ENTRY(copyoutstr)
testb %al,%al
jnz 1b
.Lcopyoutstr_end:
+ SMAP_ENABLE
/* Success -- 0 byte reached. */
decq %rdx
@@ -278,6 +285,7 @@ ENTRY(copyoutstr)
jmp copystr_return
2: /* rdx is zero -- return EFAULT or ENAMETOOLONG. */
+ SMAP_ENABLE
movq $VM_MAXUSER_ADDRESS,%r11
cmpq %r11,%rdi
jae _C_LABEL(copystr_efault)
@@ -304,6 +312,7 @@ ENTRY(copyinstr)
movq %rax,%r8
1: incq %rdx
+ SMAP_DISABLE
.Lcopyinstr_start:
1: decq %rdx
jz 2f
@@ -312,6 +321,7 @@ ENTRY(copyinstr)
testb %al,%al
jnz 1b
.Lcopyinstr_end:
+ SMAP_ENABLE
/* Success -- 0 byte reached. */
decq %rdx
@@ -319,6 +329,7 @@ ENTRY(copyinstr)
jmp copystr_return
2: /* rdx is zero -- return EFAULT or ENAMETOOLONG. */
+ SMAP_ENABLE
movq $VM_MAXUSER_ADDRESS,%r11
cmpq %r11,%rsi
jae _C_LABEL(copystr_efault)
@@ -333,6 +344,7 @@ ENTRY(copystr_efault)
END(copystr_efault)
ENTRY(copystr_fault)
+ SMAP_ENABLE
copystr_return:
/* Set *lencopied and return %eax. */
testq %r9,%r9
@@ -382,7 +394,9 @@ ENTRY(fuswintr)
leaq _C_LABEL(fusuintrfailure)(%rip),%r11
movq %r11,PCB_ONFAULT(%rcx)
+ SMAP_DISABLE
movzwl (%rdi),%eax
+ SMAP_ENABLE
movq $0,PCB_ONFAULT(%rcx)
ret
@@ -397,7 +411,9 @@ ENTRY(fubyte)
leaq _C_LABEL(fusufailure)(%rip),%r11
movq %r11,PCB_ONFAULT(%rcx)
+ SMAP_DISABLE
movzbl (%rdi),%eax
+ SMAP_ENABLE
movq $0,PCB_ONFAULT(%rcx)
ret
@@ -414,7 +430,9 @@ ENTRY(suswintr)
leaq _C_LABEL(fusuintrfailure)(%rip),%r11
movq %r11,PCB_ONFAULT(%rcx)
+ SMAP_DISABLE
movw %si,(%rdi)
+ SMAP_ENABLE
xorq %rax,%rax
movq %rax,PCB_ONFAULT(%rcx)
@@ -431,7 +449,9 @@ ENTRY(subyte)
leaq _C_LABEL(fusufailure)(%rip),%r11
movq %r11,PCB_ONFAULT(%rcx)
+ SMAP_DISABLE
movb %sil,(%rdi)
+ SMAP_ENABLE
xorq %rax,%rax
movq %rax,PCB_ONFAULT(%rcx)
@@ -444,12 +464,14 @@ END(subyte)
* because trap.c checks for them.
*/
ENTRY(fusuintrfailure)
+ SMAP_ENABLE
movq $0,PCB_ONFAULT(%rcx)
movl $-1,%eax
ret
END(fusuintrfailure)
ENTRY(fusufailure)
+ SMAP_ENABLE
movq $0,PCB_ONFAULT(%rcx)
movl $-1,%eax
ret
@@ -473,11 +495,13 @@ ENTRY(ucas_64)
ja _C_LABEL(ucas_efault)
movq %rsi,%rax
+ SMAP_DISABLE
.Lucas64_start:
/* Perform the CAS */
lock
cmpxchgq %rdx,(%rdi)
.Lucas64_end:
+ SMAP_ENABLE
/*
* Note: %rax is "old" value.
@@ -500,11 +524,13 @@ ENTRY(ucas_32)
ja _C_LABEL(ucas_efault)
movl %esi,%eax
+ SMAP_DISABLE
.Lucas32_start:
/* Perform the CAS */
lock
cmpxchgl %edx,(%rdi)
.Lucas32_end:
+ SMAP_ENABLE
/*
* Note: %eax is "old" value.
@@ -522,6 +548,7 @@ ENTRY(ucas_efault)
END(ucas_efault)
NENTRY(ucas_fault)
+ SMAP_ENABLE
ret
END(ucas_fault)
Index: src/sys/arch/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.96.4.3 src/sys/arch/amd64/amd64/trap.c:1.96.4.4
--- src/sys/arch/amd64/amd64/trap.c:1.96.4.3 Sun Apr 8 06:14:18 2018
+++ src/sys/arch/amd64/amd64/trap.c Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: trap.c,v 1.96.4.3 2018/04/08 06:14:18 snj Exp $ */
+/* $NetBSD: trap.c,v 1.96.4.4 2018/04/14 10:11:49 martin Exp $ */
/*-
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.96.4.3 2018/04/08 06:14:18 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.96.4.4 2018/04/14 10:11:49 martin Exp $");
#include "opt_ddb.h"
#include "opt_kgdb.h"
@@ -502,6 +502,15 @@ copyfault:
(void *)cr2);
}
+ if ((frame->tf_err & PGEX_P) &&
+ cr2 < VM_MAXUSER_ADDRESS) {
+ /* SMAP might have brought us here */
+ if (onfault_handler(pcb, frame) == NULL) {
+ panic("prevented access to %p (SMAP)",
+ (void *)cr2);
+ }
+ }
+
goto faultcommon;
case T_PAGEFLT|T_USER: { /* page fault */
Index: src/sys/arch/amd64/include/frameasm.h
diff -u src/sys/arch/amd64/include/frameasm.h:1.20.32.2 src/sys/arch/amd64/include/frameasm.h:1.20.32.3
--- src/sys/arch/amd64/include/frameasm.h:1.20.32.2 Thu Mar 22 16:59:03 2018
+++ src/sys/arch/amd64/include/frameasm.h Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: frameasm.h,v 1.20.32.2 2018/03/22 16:59:03 martin Exp $ */
+/* $NetBSD: frameasm.h,v 1.20.32.3 2018/04/14 10:11:49 martin Exp $ */
#ifndef _AMD64_MACHINE_FRAMEASM_H
#define _AMD64_MACHINE_FRAMEASM_H
@@ -36,6 +36,8 @@
#define STI(temp_reg) sti
#endif /* XEN */
+#define HP_NAME_CLAC 1
+#define HP_NAME_STAC 2
#define HP_NAME_SVS_ENTER 5
#define HP_NAME_SVS_LEAVE 6
#define HP_NAME_SVS_ENTER_ALT 7
@@ -49,6 +51,14 @@
.quad 123b ; \
.popsection
+#define SMAP_ENABLE \
+ HOTPATCH(HP_NAME_CLAC, 3) ; \
+ .byte 0x0F, 0x1F, 0x00 ; \
+
+#define SMAP_DISABLE \
+ HOTPATCH(HP_NAME_STAC, 3) ; \
+ .byte 0x0F, 0x1F, 0x00 ; \
+
#define SWAPGS NOT_XEN(swapgs)
/*
@@ -142,6 +152,7 @@
subq $TF_REGSIZE,%rsp ; \
INTR_SAVE_GPRS ; \
cld ; \
+ SMAP_ENABLE ; \
testb $SEL_UPL,TF_CS(%rsp) ; \
je 98f ; \
SWAPGS ; \
Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.130.2.5 src/sys/arch/x86/x86/cpu.c:1.130.2.6
--- src/sys/arch/x86/x86/cpu.c:1.130.2.5 Thu Mar 22 16:59:04 2018
+++ src/sys/arch/x86/x86/cpu.c Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu.c,v 1.130.2.5 2018/03/22 16:59:04 martin Exp $ */
+/* $NetBSD: cpu.c,v 1.130.2.6 2018/04/14 10:11:49 martin Exp $ */
/*-
* Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.130.2.5 2018/03/22 16:59:04 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.130.2.6 2018/04/14 10:11:49 martin Exp $");
#include "opt_ddb.h"
#include "opt_mpbios.h" /* for MPDEBUG */
@@ -628,6 +628,12 @@ cpu_init(struct cpu_info *ci)
if (cpu_feature[5] & CPUID_SEF_SMEP)
cr4 |= CR4_SMEP;
+#ifdef amd64
+ /* If SMAP is supported, enable it */
+ if (cpu_feature[5] & CPUID_SEF_SMAP)
+ cr4 |= CR4_SMAP;
+#endif
+
if (cr4) {
cr4 |= rcr4();
lcr4(cr4);
@@ -1091,7 +1097,7 @@ cpu_init_msrs(struct cpu_info *ci, bool
((uint64_t)LSEL(LSYSRETBASE_SEL, SEL_UPL) << 48));
wrmsr(MSR_LSTAR, (uint64_t)Xsyscall);
wrmsr(MSR_CSTAR, (uint64_t)Xsyscall32);
- wrmsr(MSR_SFMASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
+ wrmsr(MSR_SFMASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D|PSL_AC);
#ifdef SVS
if (svs_enabled)
Index: src/sys/arch/x86/x86/patch.c
diff -u src/sys/arch/x86/x86/patch.c:1.22.22.1 src/sys/arch/x86/x86/patch.c:1.22.22.2
--- src/sys/arch/x86/x86/patch.c:1.22.22.1 Tue Mar 6 10:17:11 2018
+++ src/sys/arch/x86/x86/patch.c Sat Apr 14 10:11:49 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: patch.c,v 1.22.22.1 2018/03/06 10:17:11 martin Exp $ */
+/* $NetBSD: patch.c,v 1.22.22.2 2018/04/14 10:11:49 martin Exp $ */
/*-
* Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.22.22.1 2018/03/06 10:17:11 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.22.22.2 2018/04/14 10:11:49 martin Exp $");
#include "opt_lockdebug.h"
#ifdef i386
@@ -293,5 +293,26 @@ x86_patch(bool early)
}
}
+#ifdef amd64
+ /*
+ * SMAP.
+ */
+ if (!early && cpu_feature[5] & CPUID_SEF_SMAP) {
+ KASSERT(rcr4() & CR4_SMAP);
+ const uint8_t clac_bytes[] = {
+ 0x0F, 0x01, 0xCA /* clac */
+ };
+ const uint8_t stac_bytes[] = {
+ 0x0F, 0x01, 0xCB /* stac */
+ };
+
+ /* nop,nop,nop -> clac */
+ x86_hotpatch(HP_NAME_CLAC, clac_bytes, sizeof(clac_bytes));
+
+ /* nop,nop,nop -> stac */
+ x86_hotpatch(HP_NAME_STAC, stac_bytes, sizeof(stac_bytes));
+ }
+#endif
+
x86_patch_window_close(psl, cr0);
}