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 5fb2f18f8Sesaxe * Common Development and Distribution License (the "License"). 6fb2f18f8Sesaxe * 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 /* 22fb2f18f8Sesaxe * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24b819cea2SGordon Ross * 25b819cea2SGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 26*455e370cSJohn Levon * 27*455e370cSJohn Levon * Copyright 2018 Joyent, Inc. 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 31b4203d75SMarcel Telka /* All Rights Reserved */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #ifndef _SYS_DISP_H 357c478bd9Sstevel@tonic-gate #define _SYS_DISP_H 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include <sys/priocntl.h> 387c478bd9Sstevel@tonic-gate #include <sys/thread.h> 397c478bd9Sstevel@tonic-gate #include <sys/class.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #ifdef __cplusplus 427c478bd9Sstevel@tonic-gate extern "C" { 437c478bd9Sstevel@tonic-gate #endif 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * The following is the format of a dispatcher queue entry. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate typedef struct dispq { 497c478bd9Sstevel@tonic-gate kthread_t *dq_first; /* first thread on queue or NULL */ 507c478bd9Sstevel@tonic-gate kthread_t *dq_last; /* last thread on queue or NULL */ 517c478bd9Sstevel@tonic-gate int dq_sruncnt; /* number of loaded, runnable */ 527c478bd9Sstevel@tonic-gate /* threads on queue */ 537c478bd9Sstevel@tonic-gate } dispq_t; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * Dispatch queue structure. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate typedef struct _disp { 597c478bd9Sstevel@tonic-gate disp_lock_t disp_lock; /* protects dispatching fields */ 607c478bd9Sstevel@tonic-gate pri_t disp_npri; /* # of priority levels in queue */ 617c478bd9Sstevel@tonic-gate dispq_t *disp_q; /* the dispatch queue */ 627c478bd9Sstevel@tonic-gate dispq_t *disp_q_limit; /* ptr past end of dispatch queue */ 637c478bd9Sstevel@tonic-gate ulong_t *disp_qactmap; /* bitmap of active dispatch queues */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Priorities: 677c478bd9Sstevel@tonic-gate * disp_maxrunpri is the maximum run priority of runnable threads 68*455e370cSJohn Levon * on this queue. It is -1 if nothing is runnable. 697c478bd9Sstevel@tonic-gate * 707c478bd9Sstevel@tonic-gate * disp_max_unbound_pri is the maximum run priority of threads on 717c478bd9Sstevel@tonic-gate * this dispatch queue but runnable by any CPU. This may be left 72*455e370cSJohn Levon * artificially high, then corrected when some CPU tries to take 737c478bd9Sstevel@tonic-gate * an unbound thread. It is -1 if nothing is runnable. 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate pri_t disp_maxrunpri; /* maximum run priority */ 767c478bd9Sstevel@tonic-gate pri_t disp_max_unbound_pri; /* max pri of unbound threads */ 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate volatile int disp_nrunnable; /* runnable threads in cpu dispq */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate struct cpu *disp_cpu; /* cpu owning this queue or NULL */ 81685679f7Sakolb hrtime_t disp_steal; /* time when threads become stealable */ 827c478bd9Sstevel@tonic-gate } disp_t; 837c478bd9Sstevel@tonic-gate 84b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL) 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #define MAXCLSYSPRI 99 877c478bd9Sstevel@tonic-gate #define MINCLSYSPRI 60 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * Global scheduling variables. 927c478bd9Sstevel@tonic-gate * - See sys/cpuvar.h for CPU-local variables. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate extern int nswapped; /* number of swapped threads */ 957c478bd9Sstevel@tonic-gate /* nswapped protected by swap_lock */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate extern pri_t minclsyspri; /* minimum level of any system class */ 987c478bd9Sstevel@tonic-gate extern pri_t maxclsyspri; /* maximum level of any system class */ 997c478bd9Sstevel@tonic-gate extern pri_t intr_pri; /* interrupt thread priority base level */ 1007c478bd9Sstevel@tonic-gate 101b819cea2SGordon Ross #endif /* _KERNEL || _FAKE_KERNEL */ 102b819cea2SGordon Ross #if defined(_KERNEL) 103b819cea2SGordon Ross 104685679f7Sakolb /* 105685679f7Sakolb * Minimum amount of time that a thread can remain runnable before it can 106685679f7Sakolb * be stolen by another CPU (in nanoseconds). 107685679f7Sakolb */ 108685679f7Sakolb extern hrtime_t nosteal_nsec; 109685679f7Sakolb 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * Kernel preemption occurs if a higher-priority thread is runnable with 1127c478bd9Sstevel@tonic-gate * a priority at or above kpreemptpri. 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * So that other processors can watch for such threads, a separate 1157c478bd9Sstevel@tonic-gate * dispatch queue with unbound work above kpreemptpri is maintained. 1167c478bd9Sstevel@tonic-gate * This is part of the CPU partition structure (cpupart_t). 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate extern pri_t kpreemptpri; /* level above which preemption takes place */ 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate extern void disp_kp_alloc(disp_t *, pri_t); /* allocate kp queue */ 1217c478bd9Sstevel@tonic-gate extern void disp_kp_free(disp_t *); /* free kp queue */ 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * Macro for use by scheduling classes to decide whether the thread is about 1257c478bd9Sstevel@tonic-gate * to be scheduled or not. This returns the maximum run priority. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate #define DISP_MAXRUNPRI(t) ((t)->t_disp_queue->disp_maxrunpri) 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * Platform callbacks for various dispatcher operations 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * idle_cpu() is invoked when a cpu goes idle, and has nothing to do. 1337c478bd9Sstevel@tonic-gate * disp_enq_thread() is invoked when a thread is placed on a run queue. 1347c478bd9Sstevel@tonic-gate */ 1357c478bd9Sstevel@tonic-gate extern void (*idle_cpu)(); 1367c478bd9Sstevel@tonic-gate extern void (*disp_enq_thread)(struct cpu *, int); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate extern int dispdeq(kthread_t *); 1407c478bd9Sstevel@tonic-gate extern void dispinit(void); 1417c478bd9Sstevel@tonic-gate extern void disp_add(sclass_t *); 1427c478bd9Sstevel@tonic-gate extern int intr_active(struct cpu *, int); 1437c478bd9Sstevel@tonic-gate extern int servicing_interrupt(void); 1447c478bd9Sstevel@tonic-gate extern void preempt(void); 1457c478bd9Sstevel@tonic-gate extern void setbackdq(kthread_t *); 1467c478bd9Sstevel@tonic-gate extern void setfrontdq(kthread_t *); 1477c478bd9Sstevel@tonic-gate extern void swtch(void); 1487c478bd9Sstevel@tonic-gate extern void swtch_to(kthread_t *); 1497c478bd9Sstevel@tonic-gate extern void swtch_from_zombie(void) 1507c478bd9Sstevel@tonic-gate __NORETURN; 1517c478bd9Sstevel@tonic-gate extern void dq_sruninc(kthread_t *); 1527c478bd9Sstevel@tonic-gate extern void dq_srundec(kthread_t *); 1537c478bd9Sstevel@tonic-gate extern void cpu_rechoose(kthread_t *); 1547c478bd9Sstevel@tonic-gate extern void cpu_surrender(kthread_t *); 1557c478bd9Sstevel@tonic-gate extern void kpreempt(int); 156*455e370cSJohn Levon extern struct cpu *disp_lowpri_cpu(struct cpu *, kthread_t *, pri_t); 1577c478bd9Sstevel@tonic-gate extern int disp_bound_threads(struct cpu *, int); 1587c478bd9Sstevel@tonic-gate extern int disp_bound_anythreads(struct cpu *, int); 1597c478bd9Sstevel@tonic-gate extern int disp_bound_partition(struct cpu *, int); 1607c478bd9Sstevel@tonic-gate extern void disp_cpu_init(struct cpu *); 1617c478bd9Sstevel@tonic-gate extern void disp_cpu_fini(struct cpu *); 1627c478bd9Sstevel@tonic-gate extern void disp_cpu_inactive(struct cpu *); 1637c478bd9Sstevel@tonic-gate extern void disp_adjust_unbound_pri(kthread_t *); 1647c478bd9Sstevel@tonic-gate extern void resume(kthread_t *); 1657c478bd9Sstevel@tonic-gate extern void resume_from_intr(kthread_t *); 1667c478bd9Sstevel@tonic-gate extern void resume_from_zombie(kthread_t *) 1677c478bd9Sstevel@tonic-gate __NORETURN; 1687c478bd9Sstevel@tonic-gate extern void disp_swapped_enq(kthread_t *); 1697c478bd9Sstevel@tonic-gate extern int disp_anywork(void); 1707c478bd9Sstevel@tonic-gate 171*455e370cSJohn Levon extern struct cpu *disp_choose_best_cpu(void); 172*455e370cSJohn Levon 1737c478bd9Sstevel@tonic-gate #define KPREEMPT_SYNC (-1) 1747c478bd9Sstevel@tonic-gate #define kpreempt_disable() \ 1757c478bd9Sstevel@tonic-gate { \ 1767c478bd9Sstevel@tonic-gate curthread->t_preempt++; \ 1777c478bd9Sstevel@tonic-gate ASSERT(curthread->t_preempt >= 1); \ 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate #define kpreempt_enable() \ 1807c478bd9Sstevel@tonic-gate { \ 1817c478bd9Sstevel@tonic-gate ASSERT(curthread->t_preempt >= 1); \ 1827c478bd9Sstevel@tonic-gate if (--curthread->t_preempt == 0 && \ 1837c478bd9Sstevel@tonic-gate CPU->cpu_kprunrun) \ 1847c478bd9Sstevel@tonic-gate kpreempt(KPREEMPT_SYNC); \ 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 1887c478bd9Sstevel@tonic-gate 189*455e370cSJohn Levon #define CPU_IDLE_PRI (-1) 190*455e370cSJohn Levon 1917c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate #endif 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate #endif /* _SYS_DISP_H */ 196