Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/alpha Rather than having exception_return() at the ...



details:   https://anonhg.NetBSD.org/src/rev/9b992f730eed
branches:  trunk
changeset: 976306:9b992f730eed
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Fri Sep 18 00:06:35 2020 +0000

description:
Rather than having exception_return() at the root of the call graph for
kernel threads, provide a backstop that will prevent them from ever
accidentally trying to return to user space.

diffstat:

 sys/arch/alpha/alpha/vm_machdep.c |  33 ++++++++++++++++++++++++++++++---
 sys/arch/alpha/include/alpha.h    |   9 +++++----
 2 files changed, 35 insertions(+), 7 deletions(-)

diffs (106 lines):

diff -r 11b2973fab2b -r 9b992f730eed sys/arch/alpha/alpha/vm_machdep.c
--- a/sys/arch/alpha/alpha/vm_machdep.c Fri Sep 18 00:04:58 2020 +0000
+++ b/sys/arch/alpha/alpha/vm_machdep.c Fri Sep 18 00:06:35 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: vm_machdep.c,v 1.116 2020/08/29 20:06:59 thorpej Exp $ */
+/* $NetBSD: vm_machdep.c,v 1.117 2020/09/18 00:06:35 thorpej Exp $ */
 
 /*
  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
@@ -29,7 +29,7 @@
 
 #include <sys/cdefs.h>                 /* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.116 2020/08/29 20:06:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.117 2020/09/18 00:06:35 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -60,6 +60,20 @@
 }
 
 /*
+ * This is a backstop used to ensure that kernel threads never do
+ * something silly like attempt to return to userspace.  We achieve
+ * this by putting this at the root of their call graph instead of
+ * exception_return().
+ */
+void
+alpha_kthread_backstop(void)
+{
+       struct lwp * const l = curlwp;
+
+       panic("kthread lwp %p (%s) hit the backstop", l, l->l_name);
+}
+
+/*
  * Finish a fork operation, with thread l2 nearly set up.
  * Copy and update the pcb and trap frame, making the child ready to run.
  *
@@ -130,6 +144,7 @@
         */
        {
                struct trapframe *l2tf;
+               uint64_t call_root;
 
                /*
                 * Pick a stack pointer, leaving room for a trapframe;
@@ -148,12 +163,24 @@
                l2tf->tf_regs[FRAME_A3] = 0;            /* no error */
                l2tf->tf_regs[FRAME_A4] = 1;            /* is child */
 
+               /*
+                * Normal LWPs have their return address set to
+                * exception_return() so that they'll pop into
+                * user space.  But kernel threads don't have
+                * a user space, so we put a backtop in place
+                * just in case they try.
+                */
+               if (__predict_true(l2->l_proc != &proc0))
+                       call_root = (uint64_t)exception_return;
+               else
+                       call_root = (uint64_t)alpha_kthread_backstop;
+
                pcb2->pcb_hw.apcb_ksp =
                    (uint64_t)l2->l_md.md_tf;
                pcb2->pcb_context[0] =
                    (uint64_t)func;                     /* s0: pc */
                pcb2->pcb_context[1] =
-                   (uint64_t)exception_return;         /* s1: ra */
+                   call_root;                          /* s1: ra */
                pcb2->pcb_context[2] =
                    (uint64_t)arg;                      /* s2: arg */
                pcb2->pcb_context[3] =
diff -r 11b2973fab2b -r 9b992f730eed sys/arch/alpha/include/alpha.h
--- a/sys/arch/alpha/include/alpha.h    Fri Sep 18 00:04:58 2020 +0000
+++ b/sys/arch/alpha/include/alpha.h    Fri Sep 18 00:06:35 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: alpha.h,v 1.39 2020/09/03 02:09:09 thorpej Exp $ */
+/* $NetBSD: alpha.h,v 1.40 2020/09/18 00:07:22 thorpej Exp $ */
 
 /*
  * Copyright (c) 1988 University of Utah.
@@ -73,11 +73,11 @@
 extern int alpha_fp_sync_complete;
 extern int alpha_unaligned_print, alpha_unaligned_fix, alpha_unaligned_sigbus;
 
-void   XentArith(uint64_t, uint64_t, uint64_t);                /* MAGIC */
+void   XentArith(uint64_t, uint64_t, uint64_t);        /* MAGIC */
 void   XentIF(uint64_t, uint64_t, uint64_t);           /* MAGIC */
 void   XentInt(uint64_t, uint64_t, uint64_t);          /* MAGIC */
 void   XentMM(uint64_t, uint64_t, uint64_t);           /* MAGIC */
-void   XentRestart(void);                                      /* MAGIC */
+void   XentRestart(void);                              /* MAGIC */
 void   XentSys(uint64_t, uint64_t, uint64_t);          /* MAGIC */
 void   XentUna(uint64_t, uint64_t, uint64_t);          /* MAGIC */
 void   alpha_init(u_long, u_long, u_long, u_long, u_long);
@@ -86,7 +86,8 @@
 int    badaddr_read(void *, size_t, void *);
 uint64_t console_restart(struct trapframe *);
 void   do_sir(void);
-void   exception_return(void);                                 /* MAGIC */
+void   exception_return(void);                         /* MAGIC */
+void   alpha_kthread_backstop(void);                   /* MAGIC */
 void   frametoreg(const struct trapframe *, struct reg *);
 void   init_bootstrap_console(void);
 void   init_prom_interface(unsigned long, struct rpb *);



Home | Main Index | Thread Index | Old Index