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 /*
23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2019 Joyent, Inc.
25 */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/signal.h>
31 #include <sys/stack.h>
32 #include <sys/pcb.h>
33 #include <sys/user.h>
34 #include <sys/systm.h>
35 #include <sys/sysinfo.h>
36 #include <sys/errno.h>
37 #include <sys/cmn_err.h>
38 #include <sys/cred.h>
39 #include <sys/resource.h>
40 #include <sys/task.h>
41 #include <sys/project.h>
42 #include <sys/proc.h>
43 #include <sys/debug.h>
44 #include <sys/disp.h>
1013 * CPU -- and we must grab and drop the lock to synchronize with
1014 * a racing thread walking a blocking chain that the zombie thread
1015 * was recently in. By this point, that blocking chain is (by
1016 * definition) stale: the dead thread is not holding any locks, and
1017 * is therefore not in any blocking chains -- but if we do not regrab
1018 * our lock before freeing the dead thread's data structures, the
1019 * thread walking the (stale) blocking chain will die on memory
1020 * corruption when it attempts to drop the dead thread's lock. We
1021 * only need do this once because there is no way for the dead thread
1022 * to ever again be on a blocking chain: once we have grabbed and
1023 * dropped the thread lock, we are guaranteed that anyone that could
1024 * have seen this thread in a blocking chain can no longer see it.
1025 */
1026 thread_lock(t);
1027 thread_unlock(t);
1028
1029 mutex_exit(&reaplock);
1030 }
1031
1032 /*
1033 * Install thread context ops for the current thread.
1034 */
1035 void
1036 installctx(
1037 kthread_t *t,
1038 void *arg,
1039 void (*save)(void *),
1040 void (*restore)(void *),
1041 void (*fork)(void *, void *),
1042 void (*lwp_create)(void *, void *),
1043 void (*exit)(void *),
1044 void (*free)(void *, int))
1045 {
1046 struct ctxop *ctx;
1047
1048 ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
1049 ctx->save_op = save;
1050 ctx->restore_op = restore;
1051 ctx->fork_op = fork;
1052 ctx->lwp_create_op = lwp_create;
1053 ctx->exit_op = exit;
1054 ctx->free_op = free;
1055 ctx->arg = arg;
1056 ctx->save_ts = 0;
1057 ctx->restore_ts = 0;
1058
1059 /*
1060 * Keep ctxops in a doubly-linked list to allow traversal in both
1061 * directions. Using only the newest-to-oldest ordering was adequate
1062 * previously, but reversing the order for restore_op actions is
1063 * necessary if later-added ctxops depends on earlier ones.
1064 *
1065 * One example of such a dependency: Hypervisor software handling the
1066 * guest FPU expects that it save FPU state prior to host FPU handling
1067 * and consequently handle the guest logic _after_ the host FPU has
1068 * been restored.
|
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 /*
23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2021 Joyent, Inc.
25 */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/signal.h>
31 #include <sys/stack.h>
32 #include <sys/pcb.h>
33 #include <sys/user.h>
34 #include <sys/systm.h>
35 #include <sys/sysinfo.h>
36 #include <sys/errno.h>
37 #include <sys/cmn_err.h>
38 #include <sys/cred.h>
39 #include <sys/resource.h>
40 #include <sys/task.h>
41 #include <sys/project.h>
42 #include <sys/proc.h>
43 #include <sys/debug.h>
44 #include <sys/disp.h>
1013 * CPU -- and we must grab and drop the lock to synchronize with
1014 * a racing thread walking a blocking chain that the zombie thread
1015 * was recently in. By this point, that blocking chain is (by
1016 * definition) stale: the dead thread is not holding any locks, and
1017 * is therefore not in any blocking chains -- but if we do not regrab
1018 * our lock before freeing the dead thread's data structures, the
1019 * thread walking the (stale) blocking chain will die on memory
1020 * corruption when it attempts to drop the dead thread's lock. We
1021 * only need do this once because there is no way for the dead thread
1022 * to ever again be on a blocking chain: once we have grabbed and
1023 * dropped the thread lock, we are guaranteed that anyone that could
1024 * have seen this thread in a blocking chain can no longer see it.
1025 */
1026 thread_lock(t);
1027 thread_unlock(t);
1028
1029 mutex_exit(&reaplock);
1030 }
1031
1032 /*
1033 * Provide an allocation function for callers of installctx() that, for
1034 * reasons of incomplete context-op initialization, must call installctx()
1035 * in a kpreempt_disable() block. The caller, therefore, must call this
1036 * without being in such a block.
1037 */
1038 struct ctxop *
1039 installctx_preallocate(void)
1040 {
1041 /*
1042 * NOTE: We could ASSERT/VERIFY that we are not in a place where
1043 * a KM_SLEEP allocation could block indefinitely.
1044 *
1045 * ASSERT(curthread->t_preempt == 0);
1046 */
1047
1048 return (kmem_alloc(sizeof (struct ctxop), KM_SLEEP));
1049 }
1050
1051 /*
1052 * Install thread context ops for the current thread.
1053 * The caller can pass in a preallocated struct ctxop, eliminating the need
1054 * for the requirement of entering with kernel preemption still enabled.
1055 */
1056 void
1057 installctx(
1058 kthread_t *t,
1059 void *arg,
1060 void (*save)(void *),
1061 void (*restore)(void *),
1062 void (*fork)(void *, void *),
1063 void (*lwp_create)(void *, void *),
1064 void (*exit)(void *),
1065 void (*free)(void *, int),
1066 struct ctxop *ctx)
1067 {
1068 if (ctx == NULL)
1069 ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
1070
1071 ctx->save_op = save;
1072 ctx->restore_op = restore;
1073 ctx->fork_op = fork;
1074 ctx->lwp_create_op = lwp_create;
1075 ctx->exit_op = exit;
1076 ctx->free_op = free;
1077 ctx->arg = arg;
1078 ctx->save_ts = 0;
1079 ctx->restore_ts = 0;
1080
1081 /*
1082 * Keep ctxops in a doubly-linked list to allow traversal in both
1083 * directions. Using only the newest-to-oldest ordering was adequate
1084 * previously, but reversing the order for restore_op actions is
1085 * necessary if later-added ctxops depends on earlier ones.
1086 *
1087 * One example of such a dependency: Hypervisor software handling the
1088 * guest FPU expects that it save FPU state prior to host FPU handling
1089 * and consequently handle the guest logic _after_ the host FPU has
1090 * been restored.
|