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 5*3434Sesaxe * Common Development and Distribution License (the "License"). 6*3434Sesaxe * 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*3434Sesaxe * 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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #ifndef _SYS_DISP_H 310Sstevel@tonic-gate #define _SYS_DISP_H 320Sstevel@tonic-gate 330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.11 */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <sys/priocntl.h> 360Sstevel@tonic-gate #include <sys/thread.h> 370Sstevel@tonic-gate #include <sys/class.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef __cplusplus 400Sstevel@tonic-gate extern "C" { 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * The following is the format of a dispatcher queue entry. 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate typedef struct dispq { 470Sstevel@tonic-gate kthread_t *dq_first; /* first thread on queue or NULL */ 480Sstevel@tonic-gate kthread_t *dq_last; /* last thread on queue or NULL */ 490Sstevel@tonic-gate int dq_sruncnt; /* number of loaded, runnable */ 500Sstevel@tonic-gate /* threads on queue */ 510Sstevel@tonic-gate } dispq_t; 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * Dispatch queue structure. 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate typedef struct _disp { 570Sstevel@tonic-gate disp_lock_t disp_lock; /* protects dispatching fields */ 580Sstevel@tonic-gate pri_t disp_npri; /* # of priority levels in queue */ 590Sstevel@tonic-gate dispq_t *disp_q; /* the dispatch queue */ 600Sstevel@tonic-gate dispq_t *disp_q_limit; /* ptr past end of dispatch queue */ 610Sstevel@tonic-gate ulong_t *disp_qactmap; /* bitmap of active dispatch queues */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate /* 640Sstevel@tonic-gate * Priorities: 650Sstevel@tonic-gate * disp_maxrunpri is the maximum run priority of runnable threads 660Sstevel@tonic-gate * on this queue. It is -1 if nothing is runnable. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * disp_max_unbound_pri is the maximum run priority of threads on 690Sstevel@tonic-gate * this dispatch queue but runnable by any CPU. This may be left 700Sstevel@tonic-gate * artificially high, then corrected when some CPU tries to take 710Sstevel@tonic-gate * an unbound thread. It is -1 if nothing is runnable. 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate pri_t disp_maxrunpri; /* maximum run priority */ 740Sstevel@tonic-gate pri_t disp_max_unbound_pri; /* max pri of unbound threads */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate volatile int disp_nrunnable; /* runnable threads in cpu dispq */ 770Sstevel@tonic-gate 780Sstevel@tonic-gate struct cpu *disp_cpu; /* cpu owning this queue or NULL */ 792041Sakolb hrtime_t disp_steal; /* time when threads become stealable */ 800Sstevel@tonic-gate } disp_t; 810Sstevel@tonic-gate 820Sstevel@tonic-gate #if defined(_KERNEL) 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define MAXCLSYSPRI 99 850Sstevel@tonic-gate #define MINCLSYSPRI 60 860Sstevel@tonic-gate 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * Global scheduling variables. 900Sstevel@tonic-gate * - See sys/cpuvar.h for CPU-local variables. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate extern int nswapped; /* number of swapped threads */ 930Sstevel@tonic-gate /* nswapped protected by swap_lock */ 940Sstevel@tonic-gate 950Sstevel@tonic-gate extern pri_t minclsyspri; /* minimum level of any system class */ 960Sstevel@tonic-gate extern pri_t maxclsyspri; /* maximum level of any system class */ 970Sstevel@tonic-gate extern pri_t intr_pri; /* interrupt thread priority base level */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1002041Sakolb * Minimum amount of time that a thread can remain runnable before it can 1012041Sakolb * be stolen by another CPU (in nanoseconds). 1022041Sakolb */ 1032041Sakolb extern hrtime_t nosteal_nsec; 1042041Sakolb 1052041Sakolb /* 1060Sstevel@tonic-gate * Kernel preemption occurs if a higher-priority thread is runnable with 1070Sstevel@tonic-gate * a priority at or above kpreemptpri. 1080Sstevel@tonic-gate * 1090Sstevel@tonic-gate * So that other processors can watch for such threads, a separate 1100Sstevel@tonic-gate * dispatch queue with unbound work above kpreemptpri is maintained. 1110Sstevel@tonic-gate * This is part of the CPU partition structure (cpupart_t). 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate extern pri_t kpreemptpri; /* level above which preemption takes place */ 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate extern void disp_kp_alloc(disp_t *, pri_t); /* allocate kp queue */ 1160Sstevel@tonic-gate extern void disp_kp_free(disp_t *); /* free kp queue */ 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * Macro for use by scheduling classes to decide whether the thread is about 1200Sstevel@tonic-gate * to be scheduled or not. This returns the maximum run priority. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate #define DISP_MAXRUNPRI(t) ((t)->t_disp_queue->disp_maxrunpri) 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * Platform callbacks for various dispatcher operations 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * idle_cpu() is invoked when a cpu goes idle, and has nothing to do. 1280Sstevel@tonic-gate * disp_enq_thread() is invoked when a thread is placed on a run queue. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate extern void (*idle_cpu)(); 1310Sstevel@tonic-gate extern void (*disp_enq_thread)(struct cpu *, int); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate extern int dispdeq(kthread_t *); 1350Sstevel@tonic-gate extern void dispinit(void); 1360Sstevel@tonic-gate extern void disp_add(sclass_t *); 1370Sstevel@tonic-gate extern int intr_active(struct cpu *, int); 1380Sstevel@tonic-gate extern int servicing_interrupt(void); 1390Sstevel@tonic-gate extern void preempt(void); 1400Sstevel@tonic-gate extern void setbackdq(kthread_t *); 1410Sstevel@tonic-gate extern void setfrontdq(kthread_t *); 1420Sstevel@tonic-gate extern void swtch(void); 1430Sstevel@tonic-gate extern void swtch_to(kthread_t *); 1440Sstevel@tonic-gate extern void swtch_from_zombie(void) 1450Sstevel@tonic-gate __NORETURN; 1460Sstevel@tonic-gate extern void dq_sruninc(kthread_t *); 1470Sstevel@tonic-gate extern void dq_srundec(kthread_t *); 1480Sstevel@tonic-gate extern void cpu_rechoose(kthread_t *); 1490Sstevel@tonic-gate extern void cpu_surrender(kthread_t *); 1500Sstevel@tonic-gate extern void kpreempt(int); 1510Sstevel@tonic-gate extern struct cpu *disp_lowpri_cpu(struct cpu *, struct lgrp_ld *, pri_t, 1520Sstevel@tonic-gate struct cpu *); 1530Sstevel@tonic-gate extern int disp_bound_threads(struct cpu *, int); 1540Sstevel@tonic-gate extern int disp_bound_anythreads(struct cpu *, int); 1550Sstevel@tonic-gate extern int disp_bound_partition(struct cpu *, int); 1560Sstevel@tonic-gate extern void disp_cpu_init(struct cpu *); 1570Sstevel@tonic-gate extern void disp_cpu_fini(struct cpu *); 1580Sstevel@tonic-gate extern void disp_cpu_inactive(struct cpu *); 1590Sstevel@tonic-gate extern void disp_adjust_unbound_pri(kthread_t *); 1600Sstevel@tonic-gate extern void resume(kthread_t *); 1610Sstevel@tonic-gate extern void resume_from_intr(kthread_t *); 1620Sstevel@tonic-gate extern void resume_from_zombie(kthread_t *) 1630Sstevel@tonic-gate __NORETURN; 1640Sstevel@tonic-gate extern void disp_swapped_enq(kthread_t *); 1650Sstevel@tonic-gate extern int disp_anywork(void); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate #define KPREEMPT_SYNC (-1) 1680Sstevel@tonic-gate #define kpreempt_disable() \ 1690Sstevel@tonic-gate { \ 1700Sstevel@tonic-gate curthread->t_preempt++; \ 1710Sstevel@tonic-gate ASSERT(curthread->t_preempt >= 1); \ 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate #define kpreempt_enable() \ 1740Sstevel@tonic-gate { \ 1750Sstevel@tonic-gate ASSERT(curthread->t_preempt >= 1); \ 1760Sstevel@tonic-gate if (--curthread->t_preempt == 0 && \ 1770Sstevel@tonic-gate CPU->cpu_kprunrun) \ 1780Sstevel@tonic-gate kpreempt(KPREEMPT_SYNC); \ 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate #endif /* _KERNEL */ 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate #ifdef __cplusplus 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate #endif 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate #endif /* _SYS_DISP_H */ 188