Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch Add SMAP support for i386.



details:   https://anonhg.NetBSD.org/src/rev/c42dcc86ee02
branches:  trunk
changeset: 829361:c42dcc86ee02
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sat Jan 27 09:33:25 2018 +0000

description:
Add SMAP support for i386.

diffstat:

 sys/arch/i386/i386/copy.S        |  34 ++++++++++++++++++++++++++++++++--
 sys/arch/i386/i386/trap.c        |  13 +++++++++++--
 sys/arch/i386/include/frameasm.h |  11 ++++++++++-
 sys/arch/x86/x86/cpu.c           |   6 ++----
 sys/arch/x86/x86/patch.c         |   6 ++----
 5 files changed, 57 insertions(+), 13 deletions(-)

diffs (truncated from 354 to 300 lines):

diff -r 33aac68cddda -r c42dcc86ee02 sys/arch/i386/i386/copy.S
--- a/sys/arch/i386/i386/copy.S Sat Jan 27 08:12:27 2018 +0000
+++ b/sys/arch/i386/i386/copy.S Sat Jan 27 09:33:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: copy.S,v 1.26 2018/01/27 07:45:57 maxv Exp $   */
+/*     $NetBSD: copy.S,v 1.27 2018/01/27 09:33:25 maxv Exp $   */
 
 /*
  * Copyright (c) 1998, 2000, 2004, 2008 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include <machine/asm.h>
-__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.26 2018/01/27 07:45:57 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.27 2018/01/27 09:33:25 maxv Exp $");
 
 #include "assym.h"
 
@@ -234,6 +234,7 @@
        cmpl    $VM_MAXUSER_ADDRESS,%edx
        ja      _C_LABEL(copy_efault)
 
+       SMAP_DISABLE
 .Lcopyout_start:
        movl    %eax,%ecx
        shrl    $2,%ecx
@@ -245,6 +246,7 @@
        rep
        movsb
 .Lcopyout_end:
+       SMAP_ENABLE
 
        popl    %edi
        popl    %esi
@@ -272,6 +274,7 @@
        cmpl    $VM_MAXUSER_ADDRESS,%edx
        ja      _C_LABEL(copy_efault)
 
+       SMAP_DISABLE
 .Lcopyin_start:
        movl    %eax,%ecx
        shrl    $2,%ecx
@@ -283,6 +286,7 @@
        rep
        movsb
 .Lcopyin_end:
+       SMAP_ENABLE
 
        popl    %edi
        popl    %esi
@@ -311,6 +315,7 @@
 END(kcopy_fault)
 
 NENTRY(copy_fault)
+       SMAP_ENABLE
        popl    %edi
        popl    %esi
        ret
@@ -350,6 +355,7 @@
        movl    %eax,20(%esp)
 1:     incl    %edx
 
+       SMAP_DISABLE
 .Lcopyoutstr_start:
 1:     decl    %edx
        jz      2f
@@ -358,6 +364,7 @@
        testb   %al,%al
        jnz     1b
 .Lcopyoutstr_end:
+       SMAP_ENABLE
 
        /* Success -- 0 byte reached. */
        decl    %edx
@@ -365,6 +372,7 @@
        jmp     copystr_return
 
 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
+       SMAP_ENABLE
        cmpl    $VM_MAXUSER_ADDRESS,%edi
        jae     _C_LABEL(copystr_efault)
        movl    $ENAMETOOLONG,%eax
@@ -400,6 +408,7 @@
        movl    %eax,20(%esp)
 1:     incl    %edx
 
+       SMAP_DISABLE
 .Lcopyinstr_start:
 1:     decl    %edx
        jz      2f
@@ -408,6 +417,7 @@
        testb   %al,%al
        jnz     1b
 .Lcopyinstr_end:
+       SMAP_ENABLE
 
        /* Success -- 0 byte reached. */
        decl    %edx
@@ -415,6 +425,7 @@
        jmp     copystr_return
 
 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
+       SMAP_ENABLE
        cmpl    $VM_MAXUSER_ADDRESS,%esi
        jae     _C_LABEL(copystr_efault)
        movl    $ENAMETOOLONG,%eax
@@ -428,6 +439,7 @@
 END(copystr_efault)
 
 NENTRY(copystr_fault)
+       SMAP_ENABLE
 copystr_return:
        /* Set *lencopied and return %eax. */
        movl    20(%esp),%ecx
@@ -499,7 +511,9 @@
        GET_CURPCB(%ecx)
        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
 
+       SMAP_DISABLE
        movl    (%edx),%eax
+       SMAP_ENABLE
 
        movl    $0,PCB_ONFAULT(%ecx)
        ret
@@ -522,7 +536,9 @@
        movl    L_PCB(%ecx),%ecx
        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
 
+       SMAP_DISABLE
        movzwl  (%edx),%eax
+       SMAP_ENABLE
 
        movl    $0,PCB_ONFAULT(%ecx)
        ret
@@ -541,7 +557,9 @@
        GET_CURPCB(%ecx)
        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
 
+       SMAP_DISABLE
        movzbl  (%edx),%eax
+       SMAP_ENABLE
 
        movl    $0,PCB_ONFAULT(%ecx)
        ret
@@ -552,6 +570,7 @@
  * Handle faults from [fs]u*().  Clean up and return -1.
  */
 NENTRY(fusufault)
+       SMAP_ENABLE
        movl    $0,PCB_ONFAULT(%ecx)
        movl    $-1,%eax
        ret
@@ -563,6 +582,7 @@
  * than trying to page fault.
  */
 NENTRY(fusubail)
+       SMAP_ENABLE
        movl    $0,PCB_ONFAULT(%ecx)
        movl    $-1,%eax
        ret
@@ -593,7 +613,9 @@
        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
        movl    8(%esp),%eax
 
+       SMAP_DISABLE
        movw    %ax,(%edx)
+       SMAP_ENABLE
 
        xorl    %eax,%eax
        movl    %eax,PCB_ONFAULT(%ecx)
@@ -614,7 +636,9 @@
        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
        movb    8(%esp),%al
 
+       SMAP_DISABLE
        movb    %al,(%edx)
+       SMAP_ENABLE
 
        xorl    %eax,%eax
        movl    %eax,PCB_ONFAULT(%ecx)
@@ -636,11 +660,13 @@
        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
        ja      _C_LABEL(ucas_efault)
 
+       SMAP_DISABLE
 .Lucas32_start:
        /* Perform the CAS */
        lock
        cmpxchgl %ecx,(%edx)
 .Lucas32_end:
+       SMAP_ENABLE
 
        /*
         * Note: %eax is "old" value.
@@ -659,6 +685,7 @@
 END(ucas_efault)
 
 NENTRY(ucas_fault)
+       SMAP_ENABLE
        ret
 END(ucas_fault)
 
@@ -693,6 +720,7 @@
        cmpl    $VM_MAXUSER_ADDRESS,%eax
        ja      _C_LABEL(x86_copyargs_efault)
 
+       SMAP_DISABLE
 .Lx86_copyargs_start:
        /* There are a maximum of 8 args + 2 for syscall indirect */
        cmp     $16,%ecx
@@ -722,6 +750,7 @@
        movl    %eax,32(%edx)
        movl    %ecx,36(%edx)
 .Lx86_copyargs_end:
+       SMAP_ENABLE
 
        popl    %esi
        xorl    %eax,%eax
@@ -736,6 +765,7 @@
 END(x86_copyargs_efault)
 
 NENTRY(x86_copyargs_fault)
+       SMAP_ENABLE
        popl    %esi
        ret
 END(x86_copyargs_fault)
diff -r 33aac68cddda -r c42dcc86ee02 sys/arch/i386/i386/trap.c
--- a/sys/arch/i386/i386/trap.c Sat Jan 27 08:12:27 2018 +0000
+++ b/sys/arch/i386/i386/trap.c Sat Jan 27 09:33:25 2018 +0000
@@ -1,5 +1,5 @@
 
-/*     $NetBSD: trap.c,v 1.291 2017/09/17 09:41:35 maxv Exp $  */
+/*     $NetBSD: trap.c,v 1.292 2018/01/27 09:33:25 maxv Exp $  */
 
 /*-
  * Copyright (c) 1998, 2000, 2005, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.291 2017/09/17 09:41:35 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.292 2018/01/27 09:33:25 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -564,6 +564,15 @@
                                    (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 */
diff -r 33aac68cddda -r c42dcc86ee02 sys/arch/i386/include/frameasm.h
--- a/sys/arch/i386/include/frameasm.h  Sat Jan 27 08:12:27 2018 +0000
+++ b/sys/arch/i386/include/frameasm.h  Sat Jan 27 09:33:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: frameasm.h,v 1.23 2018/01/27 08:12:27 maxv Exp $       */
+/*     $NetBSD: frameasm.h,v 1.24 2018/01/27 09:33:25 maxv Exp $       */
 
 #ifndef _I386_FRAMEASM_H_
 #define _I386_FRAMEASM_H_
@@ -40,10 +40,19 @@
        .long           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
+
 /*
  * These are used on interrupt or trap entry or exit.
  */
 #define        INTRENTRY \
+       SMAP_ENABLE                     ; \
        subl    $TF_PUSHSIZE,%esp       ; \
        movw    %gs,TF_GS(%esp)         ; \
        movw    %fs,TF_FS(%esp)         ; \
diff -r 33aac68cddda -r c42dcc86ee02 sys/arch/x86/x86/cpu.c
--- a/sys/arch/x86/x86/cpu.c    Sat Jan 27 08:12:27 2018 +0000
+++ b/sys/arch/x86/x86/cpu.c    Sat Jan 27 09:33:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cpu.c,v 1.146 2018/01/11 13:35:15 maxv Exp $   */
+/*     $NetBSD: cpu.c,v 1.147 2018/01/27 09:33:25 maxv Exp $   */
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@



Home | Main Index | Thread Index | Old Index