xref: /titanic_53/usr/src/uts/common/os/schedctl.c (revision 2cb27123907a098a777e39eebc349d73e99a518f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50209230bSgjelinek  * Common Development and Distribution License (the "License").
60209230bSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2247eb4d1eSsl108498  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
317c478bd9Sstevel@tonic-gate #include <sys/proc.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/class.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
377c478bd9Sstevel@tonic-gate #include <sys/stack.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
417c478bd9Sstevel@tonic-gate #include <sys/door.h>
427c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
467c478bd9Sstevel@tonic-gate #include <sys/mman.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/swap.h>
497c478bd9Sstevel@tonic-gate #include <sys/lwp.h>
507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
517c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
527c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
537c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
547c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
557c478bd9Sstevel@tonic-gate #include <vm/as.h>
567c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Page handling structures.  This is set up as a list of per-page
617c478bd9Sstevel@tonic-gate  * control structures (sc_page_ctl), with p->p_pagep pointing to
627c478bd9Sstevel@tonic-gate  * the first.  The per-page structures point to the actual pages
637c478bd9Sstevel@tonic-gate  * and contain pointers to the user address for each mapped page.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * All data is protected by p->p_sc_lock.  Since this lock is
667c478bd9Sstevel@tonic-gate  * held while waiting for memory, schedctl_shared_alloc() should
677c478bd9Sstevel@tonic-gate  * not be called while holding p_lock.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate typedef struct sc_page_ctl {
717c478bd9Sstevel@tonic-gate 	struct sc_page_ctl *spc_next;
727c478bd9Sstevel@tonic-gate 	sc_shared_t	*spc_base;	/* base of kernel page */
737c478bd9Sstevel@tonic-gate 	sc_shared_t	*spc_end;	/* end of usable space */
747c478bd9Sstevel@tonic-gate 	ulong_t		*spc_map;	/* bitmap of allocated space on page */
757c478bd9Sstevel@tonic-gate 	size_t		spc_space;	/* amount of space on page */
767c478bd9Sstevel@tonic-gate 	caddr_t		spc_uaddr;	/* user-level address of the page */
777c478bd9Sstevel@tonic-gate 	struct anon_map	*spc_amp;	/* anonymous memory structure */
787c478bd9Sstevel@tonic-gate } sc_page_ctl_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static size_t	sc_pagesize;		/* size of usable space on page */
817c478bd9Sstevel@tonic-gate static size_t	sc_bitmap_len;		/* # of bits in allocation bitmap */
827c478bd9Sstevel@tonic-gate static size_t	sc_bitmap_words;	/* # of words in allocation bitmap */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* Context ops */
857c478bd9Sstevel@tonic-gate static void	schedctl_save(sc_shared_t *);
867c478bd9Sstevel@tonic-gate static void	schedctl_restore(sc_shared_t *);
877c478bd9Sstevel@tonic-gate static void	schedctl_fork(kthread_t *, kthread_t *);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Functions for handling shared pages */
907c478bd9Sstevel@tonic-gate static int	schedctl_shared_alloc(sc_shared_t **, uintptr_t *);
917c478bd9Sstevel@tonic-gate static sc_page_ctl_t *schedctl_page_lookup(sc_shared_t *);
927c478bd9Sstevel@tonic-gate static int	schedctl_map(struct anon_map *, caddr_t *, caddr_t);
937c478bd9Sstevel@tonic-gate static int	schedctl_getpage(struct anon_map **, caddr_t *);
947c478bd9Sstevel@tonic-gate static void	schedctl_freepage(struct anon_map *, caddr_t);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * System call interface to scheduler activations.
987c478bd9Sstevel@tonic-gate  * This always operates on the current lwp.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate caddr_t
1017c478bd9Sstevel@tonic-gate schedctl(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
1047c478bd9Sstevel@tonic-gate 	sc_shared_t	*ssp;
1057c478bd9Sstevel@tonic-gate 	uintptr_t	uaddr;
1067c478bd9Sstevel@tonic-gate 	int		error;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (t->t_schedctl == NULL) {
1097c478bd9Sstevel@tonic-gate 		/*
1107c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the shared structure.
1117c478bd9Sstevel@tonic-gate 		 */
1127c478bd9Sstevel@tonic-gate 		if ((error = schedctl_shared_alloc(&ssp, &uaddr)) != 0)
1137c478bd9Sstevel@tonic-gate 			return ((caddr_t)(uintptr_t)set_errno(error));
1147c478bd9Sstevel@tonic-gate 		bzero(ssp, sizeof (*ssp));
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		installctx(t, ssp, schedctl_save, schedctl_restore,
1177c478bd9Sstevel@tonic-gate 		    schedctl_fork, NULL, NULL, NULL);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		thread_lock(t);	/* protect against ts_tick and ts_update */
1207c478bd9Sstevel@tonic-gate 		t->t_schedctl = ssp;
1217c478bd9Sstevel@tonic-gate 		t->t_sc_uaddr = uaddr;
1227c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return ((caddr_t)t->t_sc_uaddr);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Clean up scheduler activations state associated with an exiting
1317c478bd9Sstevel@tonic-gate  * (or execing) lwp.  t is always the current thread.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate void
1347c478bd9Sstevel@tonic-gate schedctl_lwp_cleanup(kthread_t *t)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	sc_shared_t	*ssp = t->t_schedctl;
1377c478bd9Sstevel@tonic-gate 	proc_t		*p = ttoproc(t);
1387c478bd9Sstevel@tonic-gate 	sc_page_ctl_t	*pagep;
1397c478bd9Sstevel@tonic-gate 	index_t		index;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	thread_lock(t);		/* protect against ts_tick and ts_update */
1447c478bd9Sstevel@tonic-gate 	t->t_schedctl = NULL;
1457c478bd9Sstevel@tonic-gate 	t->t_sc_uaddr = 0;
1467c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * Remove the context op to avoid the final call to
1507c478bd9Sstevel@tonic-gate 	 * schedctl_save when switching away from this lwp.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	(void) removectx(t, ssp, schedctl_save, schedctl_restore,
1537c478bd9Sstevel@tonic-gate 	    schedctl_fork, NULL, NULL, NULL);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Do not unmap the shared page until the process exits.
1577c478bd9Sstevel@tonic-gate 	 * User-level library code relies on this for adaptive mutex locking.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_sc_lock);
1607c478bd9Sstevel@tonic-gate 	ssp->sc_state = SC_FREE;
1617c478bd9Sstevel@tonic-gate 	pagep = schedctl_page_lookup(ssp);
1627c478bd9Sstevel@tonic-gate 	index = (index_t)(ssp - pagep->spc_base);
1637c478bd9Sstevel@tonic-gate 	BT_CLEAR(pagep->spc_map, index);
1647c478bd9Sstevel@tonic-gate 	pagep->spc_space += sizeof (sc_shared_t);
1657c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_sc_lock);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Cleanup the list of schedctl shared pages for the process.
1707c478bd9Sstevel@tonic-gate  * Called from exec() and exit() system calls.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate void
1737c478bd9Sstevel@tonic-gate schedctl_proc_cleanup()
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1767c478bd9Sstevel@tonic-gate 	sc_page_ctl_t *pagep;
1777c478bd9Sstevel@tonic-gate 	sc_page_ctl_t *next;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1);	/* we are single-threaded now */
1807c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedctl == NULL);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Since we are single-threaded, we don't have to hold p->p_sc_lock.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	pagep = p->p_pagep;
1867c478bd9Sstevel@tonic-gate 	p->p_pagep = NULL;
1877c478bd9Sstevel@tonic-gate 	while (pagep != NULL) {
1887c478bd9Sstevel@tonic-gate 		ASSERT(pagep->spc_space == sc_pagesize);
1897c478bd9Sstevel@tonic-gate 		next = pagep->spc_next;
1907c478bd9Sstevel@tonic-gate 		/*
1917c478bd9Sstevel@tonic-gate 		 * Unmap the user space and free the mapping structure.
1927c478bd9Sstevel@tonic-gate 		 */
1937c478bd9Sstevel@tonic-gate 		(void) as_unmap(p->p_as, pagep->spc_uaddr, PAGESIZE);
1947c478bd9Sstevel@tonic-gate 		schedctl_freepage(pagep->spc_amp, (caddr_t)(pagep->spc_base));
1957c478bd9Sstevel@tonic-gate 		kmem_free(pagep->spc_map, sizeof (ulong_t) * sc_bitmap_words);
1967c478bd9Sstevel@tonic-gate 		kmem_free(pagep, sizeof (sc_page_ctl_t));
1977c478bd9Sstevel@tonic-gate 		pagep = next;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Called by resume just before switching away from the current thread.
2037c478bd9Sstevel@tonic-gate  * Save new thread state.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate void
2067c478bd9Sstevel@tonic-gate schedctl_save(sc_shared_t *ssp)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	ssp->sc_state = curthread->t_state;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Called by resume after switching to the current thread.
2147c478bd9Sstevel@tonic-gate  * Save new thread state and CPU.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate void
2177c478bd9Sstevel@tonic-gate schedctl_restore(sc_shared_t *ssp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	ssp->sc_state = SC_ONPROC;
2207c478bd9Sstevel@tonic-gate 	ssp->sc_cpu = CPU->cpu_id;
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * On fork, remove inherited mappings from the child's address space.
2267c478bd9Sstevel@tonic-gate  * The child's threads must call schedctl() to get new shared mappings.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate void
2297c478bd9Sstevel@tonic-gate schedctl_fork(kthread_t *pt, kthread_t *ct)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(pt);
2327c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
2337c478bd9Sstevel@tonic-gate 	sc_page_ctl_t *pagep;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	ASSERT(ct->t_schedctl == NULL);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Do this only once, whether we are doing fork1() or forkall().
2397c478bd9Sstevel@tonic-gate 	 * Don't do it at all if the child process is a child of vfork()
2407c478bd9Sstevel@tonic-gate 	 * because a child of vfork() borrows the parent's address space.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (pt != curthread || (cp->p_flag & SVFORK))
2437c478bd9Sstevel@tonic-gate 		return;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_sc_lock);
2467c478bd9Sstevel@tonic-gate 	for (pagep = pp->p_pagep; pagep != NULL; pagep = pagep->spc_next)
2477c478bd9Sstevel@tonic-gate 		(void) as_unmap(cp->p_as, pagep->spc_uaddr, PAGESIZE);
2487c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_sc_lock);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Returns non-zero if the specified thread shouldn't be preempted at this time.
2537c478bd9Sstevel@tonic-gate  * Called by ts_preempt, ts_tick, and ts_update.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate int
2567c478bd9Sstevel@tonic-gate schedctl_get_nopreempt(kthread_t *t)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2597c478bd9Sstevel@tonic-gate 	return (t->t_schedctl->sc_preemptctl.sc_nopreempt);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Sets the value of the nopreempt field for the specified thread.
2657c478bd9Sstevel@tonic-gate  * Called by ts_preempt to clear the field on preemption.
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate void
2687c478bd9Sstevel@tonic-gate schedctl_set_nopreempt(kthread_t *t, short val)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2717c478bd9Sstevel@tonic-gate 	t->t_schedctl->sc_preemptctl.sc_nopreempt = val;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Sets the value of the yield field for the specified thread.  Called by
2777c478bd9Sstevel@tonic-gate  * ts_preempt and ts_tick to set the field, and ts_yield to clear it.
2787c478bd9Sstevel@tonic-gate  * The kernel never looks at this field so we don't need a schedctl_get_yield
2797c478bd9Sstevel@tonic-gate  * function.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate void
2827c478bd9Sstevel@tonic-gate schedctl_set_yield(kthread_t *t, short val)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2857c478bd9Sstevel@tonic-gate 	t->t_schedctl->sc_preemptctl.sc_yield = val;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Returns non-zero if the specified thread has requested that all
2917c478bd9Sstevel@tonic-gate  * signals be blocked.  Called by signal-related code that tests
2927c478bd9Sstevel@tonic-gate  * the signal mask of a thread that may not be the current thread
2937c478bd9Sstevel@tonic-gate  * and where the process's p_lock cannot be acquired.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate int
2967c478bd9Sstevel@tonic-gate schedctl_sigblock(kthread_t *t)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	sc_shared_t *tdp = t->t_schedctl;
2997c478bd9Sstevel@tonic-gate 
30047eb4d1eSsl108498 	if (tdp != NULL)
3017c478bd9Sstevel@tonic-gate 		return (tdp->sc_sigblock);
3027c478bd9Sstevel@tonic-gate 	return (0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * If the sc_sigblock field is set for the specified thread, set
3087c478bd9Sstevel@tonic-gate  * its signal mask to block all maskable signals, then clear the
3097c478bd9Sstevel@tonic-gate  * sc_sigblock field.  This finishes what user-level code requested
3107c478bd9Sstevel@tonic-gate  * to be done when it set tdp->sc_shared->sc_sigblock non-zero.
3117c478bd9Sstevel@tonic-gate  * Called by signal-related code that holds the process's p_lock.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate void
3147c478bd9Sstevel@tonic-gate schedctl_finish_sigblock(kthread_t *t)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	sc_shared_t *tdp = t->t_schedctl;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
3197c478bd9Sstevel@tonic-gate 
32047eb4d1eSsl108498 	if (tdp != NULL && tdp->sc_sigblock) {
3217c478bd9Sstevel@tonic-gate 		t->t_hold.__sigbits[0] = FILLSET0 & ~CANTMASK0;
3227c478bd9Sstevel@tonic-gate 		t->t_hold.__sigbits[1] = FILLSET1 & ~CANTMASK1;
3237c478bd9Sstevel@tonic-gate 		tdp->sc_sigblock = 0;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Return non-zero if the current thread has declared that
3307c478bd9Sstevel@tonic-gate  * it is calling into the kernel to park, else return zero.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate int
3337c478bd9Sstevel@tonic-gate schedctl_is_park()
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	sc_shared_t *tdp = curthread->t_schedctl;
3367c478bd9Sstevel@tonic-gate 
33747eb4d1eSsl108498 	if (tdp != NULL)
3387c478bd9Sstevel@tonic-gate 		return (tdp->sc_park);
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * If we're here and there is no shared memory (how could
3417c478bd9Sstevel@tonic-gate 	 * that happen?) then just assume we really are here to park.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	return (1);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
34647eb4d1eSsl108498 /*
34747eb4d1eSsl108498  * Declare thread is parking.
34847eb4d1eSsl108498  *
34947eb4d1eSsl108498  * libc will set "sc_park = 1" before calling lwpsys_park(0, tid) in order
35047eb4d1eSsl108498  * to declare that the thread is calling into the kernel to park.
35147eb4d1eSsl108498  *
35247eb4d1eSsl108498  * This interface exists ONLY to support older versions of libthread which
35347eb4d1eSsl108498  * are not aware of the sc_park flag.
35447eb4d1eSsl108498  *
35547eb4d1eSsl108498  * Older versions of libthread which are not aware of the sc_park flag need to
35647eb4d1eSsl108498  * be modified or emulated to call lwpsys_park(4, ...) instead of
35747eb4d1eSsl108498  * lwpsys_park(0, ...).  This will invoke schedctl_set_park() before
35847eb4d1eSsl108498  * lwp_park() to declare that the thread is parking.
35947eb4d1eSsl108498  */
36047eb4d1eSsl108498 void
36147eb4d1eSsl108498 schedctl_set_park()
36247eb4d1eSsl108498 {
36347eb4d1eSsl108498 	sc_shared_t *tdp = curthread->t_schedctl;
36447eb4d1eSsl108498 
36547eb4d1eSsl108498 	if (tdp != NULL)
36647eb4d1eSsl108498 		tdp->sc_park = 1;
36747eb4d1eSsl108498 }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * Clear the shared sc_park flag on return from parking in the kernel.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate void
3737c478bd9Sstevel@tonic-gate schedctl_unpark()
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	sc_shared_t *tdp = curthread->t_schedctl;
3767c478bd9Sstevel@tonic-gate 
37747eb4d1eSsl108498 	if (tdp != NULL)
3787c478bd9Sstevel@tonic-gate 		tdp->sc_park = 0;
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Page handling code.
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate void
3877c478bd9Sstevel@tonic-gate schedctl_init()
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * Amount of page that can hold sc_shared_t structures.  If
3917c478bd9Sstevel@tonic-gate 	 * sizeof (sc_shared_t) is a power of 2, this should just be
3927c478bd9Sstevel@tonic-gate 	 * PAGESIZE.
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	sc_pagesize = PAGESIZE - (PAGESIZE % sizeof (sc_shared_t));
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Allocation bitmap is one bit per struct on a page.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	sc_bitmap_len = sc_pagesize / sizeof (sc_shared_t);
4007c478bd9Sstevel@tonic-gate 	sc_bitmap_words = howmany(sc_bitmap_len, BT_NBIPUL);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate int
4047c478bd9Sstevel@tonic-gate schedctl_shared_alloc(sc_shared_t **kaddrp, uintptr_t *uaddrp)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	proc_t		*p = curproc;
4077c478bd9Sstevel@tonic-gate 	sc_page_ctl_t	*pagep;
4087c478bd9Sstevel@tonic-gate 	sc_shared_t	*ssp;
4097c478bd9Sstevel@tonic-gate 	caddr_t		base;
4107c478bd9Sstevel@tonic-gate 	index_t		index;
4117c478bd9Sstevel@tonic-gate 	int		error;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
4147c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_sc_lock);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Try to find space for the new data in existing pages
4187c478bd9Sstevel@tonic-gate 	 * within the process's list of shared pages.
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	for (pagep = p->p_pagep; pagep != NULL; pagep = pagep->spc_next)
4217c478bd9Sstevel@tonic-gate 		if (pagep->spc_space != 0)
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if (pagep != NULL)
4257c478bd9Sstevel@tonic-gate 		base = pagep->spc_uaddr;
4267c478bd9Sstevel@tonic-gate 	else {
4277c478bd9Sstevel@tonic-gate 		struct anon_map *amp;
4287c478bd9Sstevel@tonic-gate 		caddr_t kaddr;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * No room, need to allocate a new page.  Also set up
4327c478bd9Sstevel@tonic-gate 		 * a mapping to the kernel address space for the new
4337c478bd9Sstevel@tonic-gate 		 * page and lock it in memory.
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		if ((error = schedctl_getpage(&amp, &kaddr)) != 0) {
4367c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_sc_lock);
4377c478bd9Sstevel@tonic-gate 			return (error);
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 		if ((error = schedctl_map(amp, &base, kaddr)) != 0) {
4407c478bd9Sstevel@tonic-gate 			schedctl_freepage(amp, kaddr);
4417c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_sc_lock);
4427c478bd9Sstevel@tonic-gate 			return (error);
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		/*
4467c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the page control structure.
4477c478bd9Sstevel@tonic-gate 		 */
4487c478bd9Sstevel@tonic-gate 		pagep = kmem_alloc(sizeof (sc_page_ctl_t), KM_SLEEP);
4497c478bd9Sstevel@tonic-gate 		pagep->spc_amp = amp;
4507c478bd9Sstevel@tonic-gate 		pagep->spc_base = (sc_shared_t *)kaddr;
4517c478bd9Sstevel@tonic-gate 		pagep->spc_end = (sc_shared_t *)(kaddr + sc_pagesize);
4527c478bd9Sstevel@tonic-gate 		pagep->spc_uaddr = base;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		pagep->spc_map = kmem_zalloc(sizeof (ulong_t) * sc_bitmap_words,
4557c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
4567c478bd9Sstevel@tonic-gate 		pagep->spc_space = sc_pagesize;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		pagep->spc_next = p->p_pagep;
4597c478bd9Sstevel@tonic-gate 		p->p_pagep = pagep;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * Got a page, now allocate space for the data.  There should
4647c478bd9Sstevel@tonic-gate 	 * be space unless something's wrong.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	ASSERT(pagep != NULL && pagep->spc_space >= sizeof (sc_shared_t));
4677c478bd9Sstevel@tonic-gate 	index = bt_availbit(pagep->spc_map, sc_bitmap_len);
4687c478bd9Sstevel@tonic-gate 	ASSERT(index != -1);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Get location with pointer arithmetic.  spc_base is of type
4727c478bd9Sstevel@tonic-gate 	 * sc_shared_t *.  Mark as allocated.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	ssp = pagep->spc_base + index;
4757c478bd9Sstevel@tonic-gate 	BT_SET(pagep->spc_map, index);
4767c478bd9Sstevel@tonic-gate 	pagep->spc_space -= sizeof (sc_shared_t);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_sc_lock);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Return kernel and user addresses.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	*kaddrp = ssp;
4847c478bd9Sstevel@tonic-gate 	*uaddrp = (uintptr_t)base + ((uintptr_t)ssp & PAGEOFFSET);
4857c478bd9Sstevel@tonic-gate 	return (0);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Find the page control structure corresponding to a kernel address.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate static sc_page_ctl_t *
4937c478bd9Sstevel@tonic-gate schedctl_page_lookup(sc_shared_t *ssp)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
4967c478bd9Sstevel@tonic-gate 	sc_page_ctl_t *pagep;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_sc_lock));
4997c478bd9Sstevel@tonic-gate 	for (pagep = p->p_pagep; pagep != NULL; pagep = pagep->spc_next) {
5007c478bd9Sstevel@tonic-gate 		if (ssp >= pagep->spc_base && ssp < pagep->spc_end)
5017c478bd9Sstevel@tonic-gate 			return (pagep);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	return (NULL);		/* This "can't happen".  Should we panic? */
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * This function is called when a page needs to be mapped into a
5097c478bd9Sstevel@tonic-gate  * process's address space.  Allocate the user address space and
5107c478bd9Sstevel@tonic-gate  * set up the mapping to the page.  Assumes the page has already
5117c478bd9Sstevel@tonic-gate  * been allocated and locked in memory via schedctl_getpage.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static int
5147c478bd9Sstevel@tonic-gate schedctl_map(struct anon_map *amp, caddr_t *uaddrp, caddr_t kaddr)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	caddr_t addr;
5177c478bd9Sstevel@tonic-gate 	struct as *as = curproc->p_as;
5187c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
5197c478bd9Sstevel@tonic-gate 	int error;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	as_rangelock(as);
5227c478bd9Sstevel@tonic-gate 	/* pass address of kernel mapping as offset to avoid VAC conflicts */
5237c478bd9Sstevel@tonic-gate 	map_addr(&addr, PAGESIZE, (offset_t)(uintptr_t)kaddr, 1, 0);
5247c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
5257c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
5267c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * Use segvn to set up the mapping to the page.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	vn_a.vp = NULL;
5337c478bd9Sstevel@tonic-gate 	vn_a.offset = 0;
5347c478bd9Sstevel@tonic-gate 	vn_a.cred = NULL;
5357c478bd9Sstevel@tonic-gate 	vn_a.type = MAP_SHARED;
5367c478bd9Sstevel@tonic-gate 	vn_a.prot = vn_a.maxprot = PROT_ALL;
5377c478bd9Sstevel@tonic-gate 	vn_a.flags = 0;
5387c478bd9Sstevel@tonic-gate 	vn_a.amp = amp;
5397c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
5407c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
5417c478bd9Sstevel@tonic-gate 	error = as_map(as, addr, PAGESIZE, segvn_create, &vn_a);
5427c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (error)
5457c478bd9Sstevel@tonic-gate 		return (error);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	*uaddrp = addr;
5487c478bd9Sstevel@tonic-gate 	return (0);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate  * Allocate a new page from anonymous memory.  Also, create a kernel
5547c478bd9Sstevel@tonic-gate  * mapping to the page and lock the page in memory.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate schedctl_getpage(struct anon_map **newamp, caddr_t *newaddr)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	struct anon_map *amp;
5607c478bd9Sstevel@tonic-gate 	caddr_t kaddr;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Set up anonymous memory struct.  No swap reservation is
5647c478bd9Sstevel@tonic-gate 	 * needed since the page will be locked into memory.
5657c478bd9Sstevel@tonic-gate 	 */
566*2cb27123Saguzovsk 	amp = anonmap_alloc(PAGESIZE, 0, ANON_SLEEP);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * Allocate the page.
5707c478bd9Sstevel@tonic-gate 	 */
5710209230bSgjelinek 	kaddr = segkp_get_withanonmap(segkp, PAGESIZE,
5720209230bSgjelinek 	    KPD_NO_ANON | KPD_LOCKED | KPD_ZERO, amp);
5737c478bd9Sstevel@tonic-gate 	if (kaddr == NULL) {
5747c478bd9Sstevel@tonic-gate 		amp->refcnt--;
5757c478bd9Sstevel@tonic-gate 		anonmap_free(amp);
5767c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * The page is left SE_SHARED locked so that it won't be
5817c478bd9Sstevel@tonic-gate 	 * paged out or relocated (KPD_LOCKED above).
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	*newamp = amp;
5857c478bd9Sstevel@tonic-gate 	*newaddr = kaddr;
5867c478bd9Sstevel@tonic-gate 	return (0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * Take the necessary steps to allow a page to be released.
5927c478bd9Sstevel@tonic-gate  * This is called when the process is doing exit() or exec().
5937c478bd9Sstevel@tonic-gate  * There should be no accesses to the page after this.
5947c478bd9Sstevel@tonic-gate  * The kernel mapping of the page is released and the page is unlocked.
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate static void
5977c478bd9Sstevel@tonic-gate schedctl_freepage(struct anon_map *amp, caddr_t kaddr)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * Release the lock on the page and remove the kernel mapping.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
6037c478bd9Sstevel@tonic-gate 	segkp_release(segkp, kaddr);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 * Decrement the refcnt so the anon_map structure will be freed.
6077c478bd9Sstevel@tonic-gate 	 */
6087c478bd9Sstevel@tonic-gate 	if (--amp->refcnt == 0) {
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * The current process no longer has the page mapped, so
6117c478bd9Sstevel@tonic-gate 		 * we have to free everything rather than letting as_free
6127c478bd9Sstevel@tonic-gate 		 * do the work.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		anon_free(amp->ahp, 0, PAGESIZE);
6157c478bd9Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
6167c478bd9Sstevel@tonic-gate 		anonmap_free(amp);
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate }
621