--- old/usr/src/uts/intel/syscall/getcontext.c	Wed Apr 12 20:36:00 2023
+++ new/usr/src/uts/intel/syscall/getcontext.c	Wed Apr 12 20:36:00 2023
@@ -20,6 +20,9 @@
  */
 
 /*
+ * Copyright 2015 Joyent, Inc.
+ */
+/*
  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
@@ -50,6 +53,7 @@
 #include <sys/schedctl.h>
 #include <sys/debug.h>
 #include <sys/sysmacros.h>
+#include <sys/sdt.h>
 
 /*
  * This is a wrapper around copyout_noerr that returns a guaranteed error code.
@@ -191,7 +195,15 @@
 	else
 		ucp->uc_flags &= ~UC_FPU;
 
-	sigktou(mask, &ucp->uc_sigmask);
+	if (mask != NULL) {
+		/*
+		 * Save signal mask.
+		 */
+		sigktou(mask, &ucp->uc_sigmask);
+	} else {
+		ucp->uc_flags &= ~UC_SIGMASK;
+		bzero(&ucp->uc_sigmask, sizeof (ucp->uc_sigmask));
+	}
 
 	/*
 	 * Determine if we need to get the reset of the xsave context out here.
@@ -220,6 +232,15 @@
 		ret = fpu_signal_copyout(lwp, ucp->uc_xsave, copyout);
 	}
 
+	if (PROC_IS_BRANDED(p) && BROP(p)->b_savecontext != NULL) {
+		/*
+		 * Allow the brand the chance to modify the context we
+		 * saved:
+		 */
+		/* XXX KEBE SAYS FIX ME! */
+		BROP(p)->b_savecontext(ucp);
+	}
+
 	return (ret);
 }
 
@@ -231,7 +252,19 @@
 {
 	kthread_t *t = curthread;
 	klwp_t *lwp = ttolwp(t);
+	proc_t *p = lwptoproc(lwp);
 
+	if (PROC_IS_BRANDED(p) && BROP(p)->b_restorecontext != NULL) {
+		/*
+		 * Allow the brand the chance to modify the context before
+		 * we restore it:
+		 */
+		BROP(p)->b_restorecontext(ucp);
+	}
+
+	DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
+	    uintptr_t, lwp->lwp_oldcontext,
+	    uintptr_t, (uintptr_t)ucp->uc_link);
 	lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link;
 
 	if (ucp->uc_flags & UC_STACK) {
@@ -290,6 +323,7 @@
 	klwp_t *lwp = ttolwp(curthread);
 	void *fpu = NULL;
 	stack_t dummy_stk;
+	proc_t *p = lwptoproc(lwp);
 	int ret;
 
 	/*
@@ -354,7 +388,7 @@
 		 * Don't copyin filler or floating state unless we need it.
 		 * The ucontext_t struct and fields are specified in the ABI.
 		 */
-		if (copyin(ucp, &uc, offsetof(ucontext_t, uc_filler) -
+		if (copyin(ucp, &uc, offsetof(ucontext_t, uc_brand_data) -
 		    sizeof (uc.uc_mcontext.fpregs))) {
 			return (set_errno(EFAULT));
 		}
@@ -367,6 +401,15 @@
 			return (set_errno(EFAULT));
 		}
 
+		/*
+		 * If this is a branded process, copy in the brand-private
+		 * data:
+		 */
+		if (PROC_IS_BRANDED(p) && copyin(&ucp->uc_brand_data,
+		    &uc.uc_brand_data, sizeof (uc.uc_brand_data)) != 0) {
+			return (set_errno(EFAULT));
+		}
+
 		uc.uc_xsave = 0;
 		if ((uc.uc_flags & UC_XSAVE) != 0) {
 			int ret;
@@ -484,7 +527,15 @@
 	else
 		ucp->uc_flags &= ~UC_FPU;
 
-	sigktou(mask, &ucp->uc_sigmask);
+	if (mask != NULL) {
+		/*
+		 * Save signal mask.
+		 */
+		sigktou(mask, &ucp->uc_sigmask);
+	} else {
+		ucp->uc_flags &= ~UC_SIGMASK;
+		bzero(&ucp->uc_sigmask, sizeof (ucp->uc_sigmask));
+	}
 
 	if (!need_xsave || !fpu_en) {
 		return (0);
@@ -507,6 +558,16 @@
 		ret = fpu_signal_copyout(lwp, uaddr, copyout);
 	}
 
+
+	if (PROC_IS_BRANDED(p) && BROP(p)->b_savecontext32 != NULL) {
+		/*
+		 * Allow the brand the chance to modify the context we
+		 * saved:
+		 */
+		/* XXX KEBE SAYS FIX ME */
+		BROP(p)->b_savecontext32(ucp);
+	}
+
 	return (ret);
 }
 
@@ -519,6 +580,7 @@
 	klwp_t *lwp = ttolwp(curthread);
 	caddr32_t ustack32;
 	stack32_t dummy_stk32;
+	proc_t *p = lwptoproc(lwp);
 	int ret;
 
 	switch (flag) {
@@ -559,7 +621,7 @@
 		ucp = arg;
 		if (ucp == NULL)
 			exit(CLD_EXITED, 0);
-		if (copyin(ucp, &uc, offsetof(ucontext32_t, uc_filler) -
+		if (copyin(ucp, &uc, offsetof(ucontext32_t, uc_brand_data) -
 		    sizeof (uc.uc_mcontext.fpregs))) {
 			return (set_errno(EFAULT));
 		}
@@ -570,6 +632,15 @@
 		    sizeof (uc.uc_mcontext.fpregs))) {
 			return (set_errno(EFAULT));
 		}
+
+		/*
+		 * If this is a branded process, copy in the brand-private
+		 * data:
+		 */
+		if (PROC_IS_BRANDED(p) && copyin(&ucp->uc_brand_data,
+		    &uc.uc_brand_data, sizeof (uc.uc_brand_data)) != 0) {
+			return (set_errno(EFAULT));
+		}
 
 		uc.uc_xsave = 0;
 		if ((uc.uc_flags & UC_XSAVE) != 0 &&
