10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53247Sgjelinek * Common Development and Distribution License (the "License"). 63247Sgjelinek * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4389Ssl108498 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <sys/systm.h> 300Sstevel@tonic-gate #include <sys/schedctl.h> 310Sstevel@tonic-gate #include <sys/proc.h> 320Sstevel@tonic-gate #include <sys/thread.h> 330Sstevel@tonic-gate #include <sys/class.h> 340Sstevel@tonic-gate #include <sys/cred.h> 350Sstevel@tonic-gate #include <sys/kmem.h> 360Sstevel@tonic-gate #include <sys/cmn_err.h> 370Sstevel@tonic-gate #include <sys/stack.h> 380Sstevel@tonic-gate #include <sys/debug.h> 390Sstevel@tonic-gate #include <sys/cpuvar.h> 400Sstevel@tonic-gate #include <sys/sobject.h> 410Sstevel@tonic-gate #include <sys/door.h> 420Sstevel@tonic-gate #include <sys/modctl.h> 430Sstevel@tonic-gate #include <sys/syscall.h> 440Sstevel@tonic-gate #include <sys/sysmacros.h> 450Sstevel@tonic-gate #include <sys/vmsystm.h> 460Sstevel@tonic-gate #include <sys/mman.h> 470Sstevel@tonic-gate #include <sys/vnode.h> 480Sstevel@tonic-gate #include <sys/swap.h> 490Sstevel@tonic-gate #include <sys/lwp.h> 500Sstevel@tonic-gate #include <sys/bitmap.h> 510Sstevel@tonic-gate #include <sys/atomic.h> 520Sstevel@tonic-gate #include <sys/fcntl.h> 530Sstevel@tonic-gate #include <vm/seg_kp.h> 540Sstevel@tonic-gate #include <vm/seg_vn.h> 550Sstevel@tonic-gate #include <vm/as.h> 560Sstevel@tonic-gate #include <fs/fs_subr.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Page handling structures. This is set up as a list of per-page 610Sstevel@tonic-gate * control structures (sc_page_ctl), with p->p_pagep pointing to 620Sstevel@tonic-gate * the first. The per-page structures point to the actual pages 630Sstevel@tonic-gate * and contain pointers to the user address for each mapped page. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * All data is protected by p->p_sc_lock. Since this lock is 660Sstevel@tonic-gate * held while waiting for memory, schedctl_shared_alloc() should 670Sstevel@tonic-gate * not be called while holding p_lock. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate typedef struct sc_page_ctl { 710Sstevel@tonic-gate struct sc_page_ctl *spc_next; 720Sstevel@tonic-gate sc_shared_t *spc_base; /* base of kernel page */ 730Sstevel@tonic-gate sc_shared_t *spc_end; /* end of usable space */ 740Sstevel@tonic-gate ulong_t *spc_map; /* bitmap of allocated space on page */ 750Sstevel@tonic-gate size_t spc_space; /* amount of space on page */ 760Sstevel@tonic-gate caddr_t spc_uaddr; /* user-level address of the page */ 770Sstevel@tonic-gate struct anon_map *spc_amp; /* anonymous memory structure */ 780Sstevel@tonic-gate } sc_page_ctl_t; 790Sstevel@tonic-gate 800Sstevel@tonic-gate static size_t sc_pagesize; /* size of usable space on page */ 810Sstevel@tonic-gate static size_t sc_bitmap_len; /* # of bits in allocation bitmap */ 820Sstevel@tonic-gate static size_t sc_bitmap_words; /* # of words in allocation bitmap */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* Context ops */ 850Sstevel@tonic-gate static void schedctl_save(sc_shared_t *); 860Sstevel@tonic-gate static void schedctl_restore(sc_shared_t *); 870Sstevel@tonic-gate static void schedctl_fork(kthread_t *, kthread_t *); 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* Functions for handling shared pages */ 900Sstevel@tonic-gate static int schedctl_shared_alloc(sc_shared_t **, uintptr_t *); 910Sstevel@tonic-gate static sc_page_ctl_t *schedctl_page_lookup(sc_shared_t *); 920Sstevel@tonic-gate static int schedctl_map(struct anon_map *, caddr_t *, caddr_t); 930Sstevel@tonic-gate static int schedctl_getpage(struct anon_map **, caddr_t *); 940Sstevel@tonic-gate static void schedctl_freepage(struct anon_map *, caddr_t); 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * System call interface to scheduler activations. 980Sstevel@tonic-gate * This always operates on the current lwp. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate caddr_t 1010Sstevel@tonic-gate schedctl(void) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate kthread_t *t = curthread; 1040Sstevel@tonic-gate sc_shared_t *ssp; 1050Sstevel@tonic-gate uintptr_t uaddr; 1060Sstevel@tonic-gate int error; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (t->t_schedctl == NULL) { 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * Allocate and initialize the shared structure. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate if ((error = schedctl_shared_alloc(&ssp, &uaddr)) != 0) 1130Sstevel@tonic-gate return ((caddr_t)(uintptr_t)set_errno(error)); 1140Sstevel@tonic-gate bzero(ssp, sizeof (*ssp)); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate installctx(t, ssp, schedctl_save, schedctl_restore, 1170Sstevel@tonic-gate schedctl_fork, NULL, NULL, NULL); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate thread_lock(t); /* protect against ts_tick and ts_update */ 1200Sstevel@tonic-gate t->t_schedctl = ssp; 1210Sstevel@tonic-gate t->t_sc_uaddr = uaddr; 1220Sstevel@tonic-gate thread_unlock(t); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate return ((caddr_t)t->t_sc_uaddr); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * Clean up scheduler activations state associated with an exiting 1310Sstevel@tonic-gate * (or execing) lwp. t is always the current thread. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate void 1340Sstevel@tonic-gate schedctl_lwp_cleanup(kthread_t *t) 1350Sstevel@tonic-gate { 1360Sstevel@tonic-gate sc_shared_t *ssp = t->t_schedctl; 1370Sstevel@tonic-gate proc_t *p = ttoproc(t); 1380Sstevel@tonic-gate sc_page_ctl_t *pagep; 1390Sstevel@tonic-gate index_t index; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&p->p_lock)); 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate thread_lock(t); /* protect against ts_tick and ts_update */ 1440Sstevel@tonic-gate t->t_schedctl = NULL; 1450Sstevel@tonic-gate t->t_sc_uaddr = 0; 1460Sstevel@tonic-gate thread_unlock(t); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * Remove the context op to avoid the final call to 1500Sstevel@tonic-gate * schedctl_save when switching away from this lwp. 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate (void) removectx(t, ssp, schedctl_save, schedctl_restore, 1530Sstevel@tonic-gate schedctl_fork, NULL, NULL, NULL); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Do not unmap the shared page until the process exits. 1570Sstevel@tonic-gate * User-level library code relies on this for adaptive mutex locking. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate mutex_enter(&p->p_sc_lock); 1600Sstevel@tonic-gate ssp->sc_state = SC_FREE; 1610Sstevel@tonic-gate pagep = schedctl_page_lookup(ssp); 1620Sstevel@tonic-gate index = (index_t)(ssp - pagep->spc_base); 1630Sstevel@tonic-gate BT_CLEAR(pagep->spc_map, index); 1640Sstevel@tonic-gate pagep->spc_space += sizeof (sc_shared_t); 1650Sstevel@tonic-gate mutex_exit(&p->p_sc_lock); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Cleanup the list of schedctl shared pages for the process. 1700Sstevel@tonic-gate * Called from exec() and exit() system calls. 1710Sstevel@tonic-gate */ 1720Sstevel@tonic-gate void 1730Sstevel@tonic-gate schedctl_proc_cleanup() 1740Sstevel@tonic-gate { 1750Sstevel@tonic-gate proc_t *p = curproc; 1760Sstevel@tonic-gate sc_page_ctl_t *pagep; 1770Sstevel@tonic-gate sc_page_ctl_t *next; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1); /* we are single-threaded now */ 1800Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * Since we are single-threaded, we don't have to hold p->p_sc_lock. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate pagep = p->p_pagep; 1860Sstevel@tonic-gate p->p_pagep = NULL; 1870Sstevel@tonic-gate while (pagep != NULL) { 1880Sstevel@tonic-gate ASSERT(pagep->spc_space == sc_pagesize); 1890Sstevel@tonic-gate next = pagep->spc_next; 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Unmap the user space and free the mapping structure. 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate (void) as_unmap(p->p_as, pagep->spc_uaddr, PAGESIZE); 1940Sstevel@tonic-gate schedctl_freepage(pagep->spc_amp, (caddr_t)(pagep->spc_base)); 1950Sstevel@tonic-gate kmem_free(pagep->spc_map, sizeof (ulong_t) * sc_bitmap_words); 1960Sstevel@tonic-gate kmem_free(pagep, sizeof (sc_page_ctl_t)); 1970Sstevel@tonic-gate pagep = next; 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate /* 2020Sstevel@tonic-gate * Called by resume just before switching away from the current thread. 2030Sstevel@tonic-gate * Save new thread state. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate void 2060Sstevel@tonic-gate schedctl_save(sc_shared_t *ssp) 2070Sstevel@tonic-gate { 2080Sstevel@tonic-gate ssp->sc_state = curthread->t_state; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * Called by resume after switching to the current thread. 2140Sstevel@tonic-gate * Save new thread state and CPU. 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate void 2170Sstevel@tonic-gate schedctl_restore(sc_shared_t *ssp) 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate ssp->sc_state = SC_ONPROC; 2200Sstevel@tonic-gate ssp->sc_cpu = CPU->cpu_id; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * On fork, remove inherited mappings from the child's address space. 2260Sstevel@tonic-gate * The child's threads must call schedctl() to get new shared mappings. 2270Sstevel@tonic-gate */ 2280Sstevel@tonic-gate void 2290Sstevel@tonic-gate schedctl_fork(kthread_t *pt, kthread_t *ct) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate proc_t *pp = ttoproc(pt); 2320Sstevel@tonic-gate proc_t *cp = ttoproc(ct); 2330Sstevel@tonic-gate sc_page_ctl_t *pagep; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate ASSERT(ct->t_schedctl == NULL); 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Do this only once, whether we are doing fork1() or forkall(). 2390Sstevel@tonic-gate * Don't do it at all if the child process is a child of vfork() 2400Sstevel@tonic-gate * because a child of vfork() borrows the parent's address space. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate if (pt != curthread || (cp->p_flag & SVFORK)) 2430Sstevel@tonic-gate return; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate mutex_enter(&pp->p_sc_lock); 2460Sstevel@tonic-gate for (pagep = pp->p_pagep; pagep != NULL; pagep = pagep->spc_next) 2470Sstevel@tonic-gate (void) as_unmap(cp->p_as, pagep->spc_uaddr, PAGESIZE); 2480Sstevel@tonic-gate mutex_exit(&pp->p_sc_lock); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * Returns non-zero if the specified thread shouldn't be preempted at this time. 2530Sstevel@tonic-gate * Called by ts_preempt, ts_tick, and ts_update. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate int 2560Sstevel@tonic-gate schedctl_get_nopreempt(kthread_t *t) 2570Sstevel@tonic-gate { 2580Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 2590Sstevel@tonic-gate return (t->t_schedctl->sc_preemptctl.sc_nopreempt); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Sets the value of the nopreempt field for the specified thread. 2650Sstevel@tonic-gate * Called by ts_preempt to clear the field on preemption. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate void 2680Sstevel@tonic-gate schedctl_set_nopreempt(kthread_t *t, short val) 2690Sstevel@tonic-gate { 2700Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 2710Sstevel@tonic-gate t->t_schedctl->sc_preemptctl.sc_nopreempt = val; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * Sets the value of the yield field for the specified thread. Called by 2770Sstevel@tonic-gate * ts_preempt and ts_tick to set the field, and ts_yield to clear it. 2780Sstevel@tonic-gate * The kernel never looks at this field so we don't need a schedctl_get_yield 2790Sstevel@tonic-gate * function. 2800Sstevel@tonic-gate */ 2810Sstevel@tonic-gate void 2820Sstevel@tonic-gate schedctl_set_yield(kthread_t *t, short val) 2830Sstevel@tonic-gate { 2840Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 2850Sstevel@tonic-gate t->t_schedctl->sc_preemptctl.sc_yield = val; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * Returns non-zero if the specified thread has requested that all 2910Sstevel@tonic-gate * signals be blocked. Called by signal-related code that tests 2920Sstevel@tonic-gate * the signal mask of a thread that may not be the current thread 2930Sstevel@tonic-gate * and where the process's p_lock cannot be acquired. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate int 2960Sstevel@tonic-gate schedctl_sigblock(kthread_t *t) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate sc_shared_t *tdp = t->t_schedctl; 2990Sstevel@tonic-gate 300*4389Ssl108498 if (tdp != NULL) 3010Sstevel@tonic-gate return (tdp->sc_sigblock); 3020Sstevel@tonic-gate return (0); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate /* 3070Sstevel@tonic-gate * If the sc_sigblock field is set for the specified thread, set 3080Sstevel@tonic-gate * its signal mask to block all maskable signals, then clear the 3090Sstevel@tonic-gate * sc_sigblock field. This finishes what user-level code requested 3100Sstevel@tonic-gate * to be done when it set tdp->sc_shared->sc_sigblock non-zero. 3110Sstevel@tonic-gate * Called by signal-related code that holds the process's p_lock. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate void 3140Sstevel@tonic-gate schedctl_finish_sigblock(kthread_t *t) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate sc_shared_t *tdp = t->t_schedctl; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock)); 3190Sstevel@tonic-gate 320*4389Ssl108498 if (tdp != NULL && tdp->sc_sigblock) { 3210Sstevel@tonic-gate t->t_hold.__sigbits[0] = FILLSET0 & ~CANTMASK0; 3220Sstevel@tonic-gate t->t_hold.__sigbits[1] = FILLSET1 & ~CANTMASK1; 3230Sstevel@tonic-gate tdp->sc_sigblock = 0; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Return non-zero if the current thread has declared that 3300Sstevel@tonic-gate * it is calling into the kernel to park, else return zero. 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate int 3330Sstevel@tonic-gate schedctl_is_park() 3340Sstevel@tonic-gate { 3350Sstevel@tonic-gate sc_shared_t *tdp = curthread->t_schedctl; 3360Sstevel@tonic-gate 337*4389Ssl108498 if (tdp != NULL) 3380Sstevel@tonic-gate return (tdp->sc_park); 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * If we're here and there is no shared memory (how could 3410Sstevel@tonic-gate * that happen?) then just assume we really are here to park. 3420Sstevel@tonic-gate */ 3430Sstevel@tonic-gate return (1); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 346*4389Ssl108498 /* 347*4389Ssl108498 * Declare thread is parking. 348*4389Ssl108498 * 349*4389Ssl108498 * libc will set "sc_park = 1" before calling lwpsys_park(0, tid) in order 350*4389Ssl108498 * to declare that the thread is calling into the kernel to park. 351*4389Ssl108498 * 352*4389Ssl108498 * This interface exists ONLY to support older versions of libthread which 353*4389Ssl108498 * are not aware of the sc_park flag. 354*4389Ssl108498 * 355*4389Ssl108498 * Older versions of libthread which are not aware of the sc_park flag need to 356*4389Ssl108498 * be modified or emulated to call lwpsys_park(4, ...) instead of 357*4389Ssl108498 * lwpsys_park(0, ...). This will invoke schedctl_set_park() before 358*4389Ssl108498 * lwp_park() to declare that the thread is parking. 359*4389Ssl108498 */ 360*4389Ssl108498 void 361*4389Ssl108498 schedctl_set_park() 362*4389Ssl108498 { 363*4389Ssl108498 sc_shared_t *tdp = curthread->t_schedctl; 364*4389Ssl108498 365*4389Ssl108498 if (tdp != NULL) 366*4389Ssl108498 tdp->sc_park = 1; 367*4389Ssl108498 } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * Clear the shared sc_park flag on return from parking in the kernel. 3710Sstevel@tonic-gate */ 3720Sstevel@tonic-gate void 3730Sstevel@tonic-gate schedctl_unpark() 3740Sstevel@tonic-gate { 3750Sstevel@tonic-gate sc_shared_t *tdp = curthread->t_schedctl; 3760Sstevel@tonic-gate 377*4389Ssl108498 if (tdp != NULL) 3780Sstevel@tonic-gate tdp->sc_park = 0; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * Page handling code. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate void 3870Sstevel@tonic-gate schedctl_init() 3880Sstevel@tonic-gate { 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * Amount of page that can hold sc_shared_t structures. If 3910Sstevel@tonic-gate * sizeof (sc_shared_t) is a power of 2, this should just be 3920Sstevel@tonic-gate * PAGESIZE. 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate sc_pagesize = PAGESIZE - (PAGESIZE % sizeof (sc_shared_t)); 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* 3970Sstevel@tonic-gate * Allocation bitmap is one bit per struct on a page. 3980Sstevel@tonic-gate */ 3990Sstevel@tonic-gate sc_bitmap_len = sc_pagesize / sizeof (sc_shared_t); 4000Sstevel@tonic-gate sc_bitmap_words = howmany(sc_bitmap_len, BT_NBIPUL); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate int 4040Sstevel@tonic-gate schedctl_shared_alloc(sc_shared_t **kaddrp, uintptr_t *uaddrp) 4050Sstevel@tonic-gate { 4060Sstevel@tonic-gate proc_t *p = curproc; 4070Sstevel@tonic-gate sc_page_ctl_t *pagep; 4080Sstevel@tonic-gate sc_shared_t *ssp; 4090Sstevel@tonic-gate caddr_t base; 4100Sstevel@tonic-gate index_t index; 4110Sstevel@tonic-gate int error; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&p->p_lock)); 4140Sstevel@tonic-gate mutex_enter(&p->p_sc_lock); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate /* 4170Sstevel@tonic-gate * Try to find space for the new data in existing pages 4180Sstevel@tonic-gate * within the process's list of shared pages. 4190Sstevel@tonic-gate */ 4200Sstevel@tonic-gate for (pagep = p->p_pagep; pagep != NULL; pagep = pagep->spc_next) 4210Sstevel@tonic-gate if (pagep->spc_space != 0) 4220Sstevel@tonic-gate break; 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate if (pagep != NULL) 4250Sstevel@tonic-gate base = pagep->spc_uaddr; 4260Sstevel@tonic-gate else { 4270Sstevel@tonic-gate struct anon_map *amp; 4280Sstevel@tonic-gate caddr_t kaddr; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * No room, need to allocate a new page. Also set up 4320Sstevel@tonic-gate * a mapping to the kernel address space for the new 4330Sstevel@tonic-gate * page and lock it in memory. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if ((error = schedctl_getpage(&, &kaddr)) != 0) { 4360Sstevel@tonic-gate mutex_exit(&p->p_sc_lock); 4370Sstevel@tonic-gate return (error); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate if ((error = schedctl_map(amp, &base, kaddr)) != 0) { 4400Sstevel@tonic-gate schedctl_freepage(amp, kaddr); 4410Sstevel@tonic-gate mutex_exit(&p->p_sc_lock); 4420Sstevel@tonic-gate return (error); 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate /* 4460Sstevel@tonic-gate * Allocate and initialize the page control structure. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate pagep = kmem_alloc(sizeof (sc_page_ctl_t), KM_SLEEP); 4490Sstevel@tonic-gate pagep->spc_amp = amp; 4500Sstevel@tonic-gate pagep->spc_base = (sc_shared_t *)kaddr; 4510Sstevel@tonic-gate pagep->spc_end = (sc_shared_t *)(kaddr + sc_pagesize); 4520Sstevel@tonic-gate pagep->spc_uaddr = base; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate pagep->spc_map = kmem_zalloc(sizeof (ulong_t) * sc_bitmap_words, 4550Sstevel@tonic-gate KM_SLEEP); 4560Sstevel@tonic-gate pagep->spc_space = sc_pagesize; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate pagep->spc_next = p->p_pagep; 4590Sstevel@tonic-gate p->p_pagep = pagep; 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Got a page, now allocate space for the data. There should 4640Sstevel@tonic-gate * be space unless something's wrong. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate ASSERT(pagep != NULL && pagep->spc_space >= sizeof (sc_shared_t)); 4670Sstevel@tonic-gate index = bt_availbit(pagep->spc_map, sc_bitmap_len); 4680Sstevel@tonic-gate ASSERT(index != -1); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * Get location with pointer arithmetic. spc_base is of type 4720Sstevel@tonic-gate * sc_shared_t *. Mark as allocated. 4730Sstevel@tonic-gate */ 4740Sstevel@tonic-gate ssp = pagep->spc_base + index; 4750Sstevel@tonic-gate BT_SET(pagep->spc_map, index); 4760Sstevel@tonic-gate pagep->spc_space -= sizeof (sc_shared_t); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate mutex_exit(&p->p_sc_lock); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * Return kernel and user addresses. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate *kaddrp = ssp; 4840Sstevel@tonic-gate *uaddrp = (uintptr_t)base + ((uintptr_t)ssp & PAGEOFFSET); 4850Sstevel@tonic-gate return (0); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* 4900Sstevel@tonic-gate * Find the page control structure corresponding to a kernel address. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate static sc_page_ctl_t * 4930Sstevel@tonic-gate schedctl_page_lookup(sc_shared_t *ssp) 4940Sstevel@tonic-gate { 4950Sstevel@tonic-gate proc_t *p = curproc; 4960Sstevel@tonic-gate sc_page_ctl_t *pagep; 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_sc_lock)); 4990Sstevel@tonic-gate for (pagep = p->p_pagep; pagep != NULL; pagep = pagep->spc_next) { 5000Sstevel@tonic-gate if (ssp >= pagep->spc_base && ssp < pagep->spc_end) 5010Sstevel@tonic-gate return (pagep); 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate return (NULL); /* This "can't happen". Should we panic? */ 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * This function is called when a page needs to be mapped into a 5090Sstevel@tonic-gate * process's address space. Allocate the user address space and 5100Sstevel@tonic-gate * set up the mapping to the page. Assumes the page has already 5110Sstevel@tonic-gate * been allocated and locked in memory via schedctl_getpage. 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate static int 5140Sstevel@tonic-gate schedctl_map(struct anon_map *amp, caddr_t *uaddrp, caddr_t kaddr) 5150Sstevel@tonic-gate { 5160Sstevel@tonic-gate caddr_t addr; 5170Sstevel@tonic-gate struct as *as = curproc->p_as; 5180Sstevel@tonic-gate struct segvn_crargs vn_a; 5190Sstevel@tonic-gate int error; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate as_rangelock(as); 5220Sstevel@tonic-gate /* pass address of kernel mapping as offset to avoid VAC conflicts */ 5230Sstevel@tonic-gate map_addr(&addr, PAGESIZE, (offset_t)(uintptr_t)kaddr, 1, 0); 5240Sstevel@tonic-gate if (addr == NULL) { 5250Sstevel@tonic-gate as_rangeunlock(as); 5260Sstevel@tonic-gate return (ENOMEM); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* 5300Sstevel@tonic-gate * Use segvn to set up the mapping to the page. 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate vn_a.vp = NULL; 5330Sstevel@tonic-gate vn_a.offset = 0; 5340Sstevel@tonic-gate vn_a.cred = NULL; 5350Sstevel@tonic-gate vn_a.type = MAP_SHARED; 5360Sstevel@tonic-gate vn_a.prot = vn_a.maxprot = PROT_ALL; 5370Sstevel@tonic-gate vn_a.flags = 0; 5380Sstevel@tonic-gate vn_a.amp = amp; 5390Sstevel@tonic-gate vn_a.szc = 0; 5400Sstevel@tonic-gate vn_a.lgrp_mem_policy_flags = 0; 5410Sstevel@tonic-gate error = as_map(as, addr, PAGESIZE, segvn_create, &vn_a); 5420Sstevel@tonic-gate as_rangeunlock(as); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if (error) 5450Sstevel@tonic-gate return (error); 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate *uaddrp = addr; 5480Sstevel@tonic-gate return (0); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate /* 5530Sstevel@tonic-gate * Allocate a new page from anonymous memory. Also, create a kernel 5540Sstevel@tonic-gate * mapping to the page and lock the page in memory. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate static int 5570Sstevel@tonic-gate schedctl_getpage(struct anon_map **newamp, caddr_t *newaddr) 5580Sstevel@tonic-gate { 5590Sstevel@tonic-gate struct anon_map *amp; 5600Sstevel@tonic-gate caddr_t kaddr; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate /* 5630Sstevel@tonic-gate * Set up anonymous memory struct. No swap reservation is 5640Sstevel@tonic-gate * needed since the page will be locked into memory. 5650Sstevel@tonic-gate */ 5663247Sgjelinek amp = anonmap_alloc(PAGESIZE, 0); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* 5690Sstevel@tonic-gate * Allocate the page. 5700Sstevel@tonic-gate */ 5713247Sgjelinek kaddr = segkp_get_withanonmap(segkp, PAGESIZE, 5723247Sgjelinek KPD_NO_ANON | KPD_LOCKED | KPD_ZERO, amp); 5730Sstevel@tonic-gate if (kaddr == NULL) { 5740Sstevel@tonic-gate amp->refcnt--; 5750Sstevel@tonic-gate anonmap_free(amp); 5760Sstevel@tonic-gate return (ENOMEM); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * The page is left SE_SHARED locked so that it won't be 5810Sstevel@tonic-gate * paged out or relocated (KPD_LOCKED above). 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate *newamp = amp; 5850Sstevel@tonic-gate *newaddr = kaddr; 5860Sstevel@tonic-gate return (0); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * Take the necessary steps to allow a page to be released. 5920Sstevel@tonic-gate * This is called when the process is doing exit() or exec(). 5930Sstevel@tonic-gate * There should be no accesses to the page after this. 5940Sstevel@tonic-gate * The kernel mapping of the page is released and the page is unlocked. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate static void 5970Sstevel@tonic-gate schedctl_freepage(struct anon_map *amp, caddr_t kaddr) 5980Sstevel@tonic-gate { 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * Release the lock on the page and remove the kernel mapping. 6010Sstevel@tonic-gate */ 6020Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER); 6030Sstevel@tonic-gate segkp_release(segkp, kaddr); 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* 6060Sstevel@tonic-gate * Decrement the refcnt so the anon_map structure will be freed. 6070Sstevel@tonic-gate */ 6080Sstevel@tonic-gate if (--amp->refcnt == 0) { 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * The current process no longer has the page mapped, so 6110Sstevel@tonic-gate * we have to free everything rather than letting as_free 6120Sstevel@tonic-gate * do the work. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate anon_free(amp->ahp, 0, PAGESIZE); 6150Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 6160Sstevel@tonic-gate anonmap_free(amp); 6170Sstevel@tonic-gate } else { 6180Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate } 621