3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2019 Joyent, Inc.
24 */
25
26 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
28 /* All Rights Reserved */
29
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/signal.h>
34 #include <sys/systm.h>
35 #include <sys/user.h>
36 #include <sys/mman.h>
37 #include <sys/class.h>
38 #include <sys/proc.h>
39 #include <sys/procfs.h>
40 #include <sys/buf.h>
41 #include <sys/kmem.h>
42 #include <sys/cred.h>
43 #include <sys/archsystm.h>
746
747 /*
748 * If this is a process in a branded zone, then we want it to use the brand
749 * syscall entry points instead of the standard Solaris entry points. This
750 * routine must be called when a new lwp is created within a branded zone
751 * or when an existing lwp moves into a branded zone via a zone_enter()
752 * operation.
753 */
754 void
755 lwp_attach_brand_hdlrs(klwp_t *lwp)
756 {
757 kthread_t *t = lwptot(lwp);
758
759 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
760
761 ASSERT(removectx(t, NULL, brand_interpositioning_disable,
762 brand_interpositioning_enable, NULL, NULL,
763 brand_interpositioning_disable, NULL) == 0);
764 installctx(t, NULL, brand_interpositioning_disable,
765 brand_interpositioning_enable, NULL, NULL,
766 brand_interpositioning_disable, NULL);
767
768 if (t == curthread) {
769 kpreempt_disable();
770 brand_interpositioning_enable();
771 kpreempt_enable();
772 }
773 }
774
775 /*
776 * If this is a process in a branded zone, then we want it to disable the
777 * brand syscall entry points. This routine must be called when the last
778 * lwp in a process is exiting in proc_exit().
779 */
780 void
781 lwp_detach_brand_hdlrs(klwp_t *lwp)
782 {
783 kthread_t *t = lwptot(lwp);
784
785 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
786 if (t == curthread)
812 * invoking lwp_stk_fini(). When the lwp is recycled from death
813 * row, lwp_stk_fini() is invoked, then thread_free(), and thus
814 * freectx(t, 0) as before.
815 *
816 * In the case of exec, the surviving lwp is thoroughly scrubbed
817 * clean; exec invokes freectx(t, 1) to destroy associated contexts.
818 * On the way back to the new image, it invokes setregs() which
819 * in turn invokes this routine.
820 */
821 void
822 lwp_installctx(klwp_t *lwp)
823 {
824 kthread_t *t = lwptot(lwp);
825 int thisthread = t == curthread;
826 #ifdef _SYSCALL32_IMPL
827 void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
828 lwp_segregs_restore : lwp_segregs_restore32;
829 #else
830 void (*restop)(klwp_t *) = lwp_segregs_restore;
831 #endif
832
833 /*
834 * Install the basic lwp context handlers on each lwp.
835 *
836 * On the amd64 kernel, the context handlers are responsible for
837 * virtualizing %ds, %es, %fs, and %gs to the lwp. The register
838 * values are only ever changed via sys_rtt when the
839 * PCB_UPDATE_SEGS bit (1) is set in pcb->pcb_rupdate. Only
840 * sys_rtt gets to clear the bit.
841 *
842 * On the i386 kernel, the context handlers are responsible for
843 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
844 */
845 ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
846 NULL, NULL, NULL, NULL) == 0);
847 if (thisthread)
848 kpreempt_disable();
849 installctx(t, lwp, lwp_segregs_save, restop,
850 NULL, NULL, NULL, NULL);
851 if (thisthread) {
852 /*
853 * Since we're the right thread, set the values in the GDT
854 */
855 restop(lwp);
856 kpreempt_enable();
857 }
858
859 /*
860 * If we have sysenter/sysexit instructions enabled, we need
861 * to ensure that the hardware mechanism is kept up-to-date with the
862 * lwp's kernel stack pointer across context switches.
863 *
864 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
865 * it to the lwp's kernel stack pointer (kstktop).
866 */
867 if (is_x86_feature(x86_featureset, X86FSET_SEP)) {
868 #if defined(__amd64)
869 caddr_t kstktop = (caddr_t)lwp->lwp_regs;
870 #elif defined(__i386)
871 caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
872 SA(sizeof (struct regs) + MINFRAME);
873 #endif
874 ASSERT(removectx(t, kstktop,
875 sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
876
877 if (thisthread)
878 kpreempt_disable();
879 installctx(t, kstktop,
880 sep_save, sep_restore, NULL, NULL, NULL, NULL);
881 if (thisthread) {
882 /*
883 * We're the right thread, so set the stack pointer
884 * for the first sysenter instruction to use
885 */
886 sep_restore(kstktop);
887 kpreempt_enable();
888 }
889 }
890
891 if (PROC_IS_BRANDED(ttoproc(t)))
892 lwp_attach_brand_hdlrs(lwp);
893 }
894
895 /*
896 * Clear registers on exec(2).
897 */
898 void
899 setregs(uarg_t *args)
900 {
|
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2021 Joyent, Inc.
24 */
25
26 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
28 /* All Rights Reserved */
29
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/signal.h>
34 #include <sys/systm.h>
35 #include <sys/user.h>
36 #include <sys/mman.h>
37 #include <sys/class.h>
38 #include <sys/proc.h>
39 #include <sys/procfs.h>
40 #include <sys/buf.h>
41 #include <sys/kmem.h>
42 #include <sys/cred.h>
43 #include <sys/archsystm.h>
746
747 /*
748 * If this is a process in a branded zone, then we want it to use the brand
749 * syscall entry points instead of the standard Solaris entry points. This
750 * routine must be called when a new lwp is created within a branded zone
751 * or when an existing lwp moves into a branded zone via a zone_enter()
752 * operation.
753 */
754 void
755 lwp_attach_brand_hdlrs(klwp_t *lwp)
756 {
757 kthread_t *t = lwptot(lwp);
758
759 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
760
761 ASSERT(removectx(t, NULL, brand_interpositioning_disable,
762 brand_interpositioning_enable, NULL, NULL,
763 brand_interpositioning_disable, NULL) == 0);
764 installctx(t, NULL, brand_interpositioning_disable,
765 brand_interpositioning_enable, NULL, NULL,
766 brand_interpositioning_disable, NULL, NULL);
767
768 if (t == curthread) {
769 kpreempt_disable();
770 brand_interpositioning_enable();
771 kpreempt_enable();
772 }
773 }
774
775 /*
776 * If this is a process in a branded zone, then we want it to disable the
777 * brand syscall entry points. This routine must be called when the last
778 * lwp in a process is exiting in proc_exit().
779 */
780 void
781 lwp_detach_brand_hdlrs(klwp_t *lwp)
782 {
783 kthread_t *t = lwptot(lwp);
784
785 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp)));
786 if (t == curthread)
812 * invoking lwp_stk_fini(). When the lwp is recycled from death
813 * row, lwp_stk_fini() is invoked, then thread_free(), and thus
814 * freectx(t, 0) as before.
815 *
816 * In the case of exec, the surviving lwp is thoroughly scrubbed
817 * clean; exec invokes freectx(t, 1) to destroy associated contexts.
818 * On the way back to the new image, it invokes setregs() which
819 * in turn invokes this routine.
820 */
821 void
822 lwp_installctx(klwp_t *lwp)
823 {
824 kthread_t *t = lwptot(lwp);
825 int thisthread = t == curthread;
826 #ifdef _SYSCALL32_IMPL
827 void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ?
828 lwp_segregs_restore : lwp_segregs_restore32;
829 #else
830 void (*restop)(klwp_t *) = lwp_segregs_restore;
831 #endif
832 struct ctxop *ctx;
833
834 /*
835 * Install the basic lwp context handlers on each lwp.
836 *
837 * On the amd64 kernel, the context handlers are responsible for
838 * virtualizing %ds, %es, %fs, and %gs to the lwp. The register
839 * values are only ever changed via sys_rtt when the
840 * PCB_UPDATE_SEGS bit (1) is set in pcb->pcb_rupdate. Only
841 * sys_rtt gets to clear the bit.
842 *
843 * On the i386 kernel, the context handlers are responsible for
844 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs
845 */
846 ASSERT(removectx(t, lwp, lwp_segregs_save, restop,
847 NULL, NULL, NULL, NULL) == 0);
848 if (thisthread) {
849 ctx = installctx_preallocate();
850 kpreempt_disable();
851 } else {
852 ctx = NULL;
853 }
854 installctx(t, lwp, lwp_segregs_save, restop,
855 NULL, NULL, NULL, NULL, ctx);
856 if (thisthread) {
857 /*
858 * Since we're the right thread, set the values in the GDT
859 */
860 restop(lwp);
861 kpreempt_enable();
862 }
863
864 /*
865 * If we have sysenter/sysexit instructions enabled, we need
866 * to ensure that the hardware mechanism is kept up-to-date with the
867 * lwp's kernel stack pointer across context switches.
868 *
869 * sep_save zeros the sysenter stack pointer msr; sep_restore sets
870 * it to the lwp's kernel stack pointer (kstktop).
871 */
872 if (is_x86_feature(x86_featureset, X86FSET_SEP)) {
873 #if defined(__amd64)
874 caddr_t kstktop = (caddr_t)lwp->lwp_regs;
875 #elif defined(__i386)
876 caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) +
877 SA(sizeof (struct regs) + MINFRAME);
878 #endif
879 ASSERT(removectx(t, kstktop,
880 sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0);
881
882 if (thisthread) {
883 ctx = installctx_preallocate();
884 kpreempt_disable();
885 } else {
886 ctx = NULL;
887 }
888 installctx(t, kstktop,
889 sep_save, sep_restore, NULL, NULL, NULL, NULL, ctx);
890 if (thisthread) {
891 /*
892 * We're the right thread, so set the stack pointer
893 * for the first sysenter instruction to use
894 */
895 sep_restore(kstktop);
896 kpreempt_enable();
897 }
898 }
899
900 if (PROC_IS_BRANDED(ttoproc(t)))
901 lwp_attach_brand_hdlrs(lwp);
902 }
903
904 /*
905 * Clear registers on exec(2).
906 */
907 void
908 setregs(uarg_t *args)
909 {
|