Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/disp/rt.c
+++ new/usr/src/uts/common/disp/rt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2015 Joyent, Inc.
26 26 */
27 27
28 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 29 /* All Rights Reserved */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/param.h>
33 33 #include <sys/sysmacros.h>
34 34 #include <sys/cred.h>
35 35 #include <sys/proc.h>
36 36 #include <sys/pcb.h>
37 37 #include <sys/signal.h>
38 38 #include <sys/user.h>
39 39 #include <sys/priocntl.h>
40 40 #include <sys/class.h>
41 41 #include <sys/disp.h>
42 42 #include <sys/procset.h>
43 43 #include <sys/cmn_err.h>
44 44 #include <sys/debug.h>
45 45 #include <sys/rt.h>
46 46 #include <sys/rtpriocntl.h>
47 47 #include <sys/kmem.h>
48 48 #include <sys/systm.h>
49 49 #include <sys/schedctl.h>
50 50 #include <sys/errno.h>
51 51 #include <sys/cpuvar.h>
52 52 #include <sys/vmsystm.h>
53 53 #include <sys/time.h>
54 54 #include <sys/policy.h>
55 55 #include <sys/sdt.h>
56 56 #include <sys/cpupart.h>
57 57 #include <sys/modctl.h>
58 58
59 59 static pri_t rt_init(id_t, int, classfuncs_t **);
60 60
61 61 static struct sclass csw = {
62 62 "RT",
63 63 rt_init,
64 64 0
65 65 };
66 66
67 67 static struct modlsched modlsched = {
68 68 &mod_schedops, "realtime scheduling class", &csw
69 69 };
70 70
71 71 static struct modlinkage modlinkage = {
72 72 MODREV_1, (void *)&modlsched, NULL
73 73 };
74 74
75 75 int
76 76 _init()
77 77 {
78 78 return (mod_install(&modlinkage));
79 79 }
80 80
81 81 int
82 82 _fini()
83 83 {
84 84 return (EBUSY); /* don't remove RT for now */
85 85 }
86 86
87 87 int
88 88 _info(struct modinfo *modinfop)
89 89 {
90 90 return (mod_info(&modlinkage, modinfop));
91 91 }
92 92
93 93
94 94 /*
95 95 * Class specific code for the real-time class
96 96 */
97 97
98 98 /*
99 99 * Extern declarations for variables defined in the rt master file
100 100 */
101 101 #define RTMAXPRI 59
102 102
103 103 pri_t rt_maxpri = RTMAXPRI; /* maximum real-time priority */
104 104 rtdpent_t *rt_dptbl; /* real-time dispatcher parameter table */
105 105
106 106 static int rt_admin(caddr_t, cred_t *);
107 107 static int rt_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
108 108 static int rt_fork(kthread_t *, kthread_t *, void *);
109 109 static int rt_getclinfo(void *);
110 110 static int rt_getclpri(pcpri_t *);
111 111 static int rt_parmsin(void *);
112 112 static int rt_parmsout(void *, pc_vaparms_t *);
113 113 static int rt_vaparmsin(void *, pc_vaparms_t *);
114 114 static int rt_vaparmsout(void *, pc_vaparms_t *);
115 115 static int rt_parmsset(kthread_t *, void *, id_t, cred_t *);
116 116 static int rt_donice(kthread_t *, cred_t *, int, int *);
117 117 static int rt_doprio(kthread_t *, cred_t *, int, int *);
118 118 static void rt_exitclass(void *);
119 119 static int rt_canexit(kthread_t *, cred_t *);
120 120 static void rt_forkret(kthread_t *, kthread_t *);
121 121 static void rt_nullsys();
122 122 static void rt_parmsget(kthread_t *, void *);
123 123 static void rt_preempt(kthread_t *);
124 124 static void rt_setrun(kthread_t *);
125 125 static void rt_tick(kthread_t *);
126 126 static void rt_wakeup(kthread_t *);
127 127 static pri_t rt_swapin(kthread_t *, int);
128 128 static pri_t rt_swapout(kthread_t *, int);
129 129 static pri_t rt_globpri(kthread_t *);
130 130 static void rt_yield(kthread_t *);
131 131 static int rt_alloc(void **, int);
132 132 static void rt_free(void *);
133 133
134 134 static void rt_change_priority(kthread_t *, rtproc_t *);
135 135
136 136 static id_t rt_cid; /* real-time class ID */
137 137 static rtproc_t rt_plisthead; /* dummy rtproc at head of rtproc list */
138 138 static kmutex_t rt_dptblock; /* protects realtime dispatch table */
139 139 static kmutex_t rt_list_lock; /* protects RT thread list */
140 140
141 141 extern rtdpent_t *rt_getdptbl(void);
142 142
143 143 static struct classfuncs rt_classfuncs = {
144 144 /* class ops */
145 145 rt_admin,
146 146 rt_getclinfo,
147 147 rt_parmsin,
148 148 rt_parmsout,
149 149 rt_vaparmsin,
150 150 rt_vaparmsout,
151 151 rt_getclpri,
152 152 rt_alloc,
153 153 rt_free,
154 154 /* thread ops */
155 155 rt_enterclass,
156 156 rt_exitclass,
157 157 rt_canexit,
158 158 rt_fork,
159 159 rt_forkret,
160 160 rt_parmsget,
161 161 rt_parmsset,
162 162 rt_nullsys, /* stop */
163 163 rt_nullsys, /* exit */
164 164 rt_nullsys, /* active */
165 165 rt_nullsys, /* inactive */
166 166 rt_swapin,
167 167 rt_swapout,
168 168 rt_nullsys, /* trapret */
169 169 rt_preempt,
170 170 rt_setrun,
171 171 rt_nullsys, /* sleep */
172 172 rt_tick,
173 173 rt_wakeup,
174 174 rt_donice,
175 175 rt_globpri,
176 176 rt_nullsys, /* set_process_group */
177 177 rt_yield,
178 178 rt_doprio,
179 179 };
180 180
181 181 /*
182 182 * Real-time class initialization. Called by dispinit() at boot time.
183 183 * We can ignore the clparmsz argument since we know that the smallest
184 184 * possible parameter buffer is big enough for us.
185 185 */
186 186 /* ARGSUSED */
187 187 pri_t
188 188 rt_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
189 189 {
190 190 rt_dptbl = rt_getdptbl();
191 191 rt_cid = cid; /* Record our class ID */
192 192
193 193 /*
194 194 * Initialize the rtproc list.
195 195 */
196 196 rt_plisthead.rt_next = rt_plisthead.rt_prev = &rt_plisthead;
197 197
198 198 /*
199 199 * We're required to return a pointer to our classfuncs
200 200 * structure and the highest global priority value we use.
201 201 */
202 202 *clfuncspp = &rt_classfuncs;
203 203 mutex_init(&rt_dptblock, NULL, MUTEX_DEFAULT, NULL);
204 204 mutex_init(&rt_list_lock, NULL, MUTEX_DEFAULT, NULL);
205 205 return (rt_dptbl[rt_maxpri].rt_globpri);
206 206 }
207 207
208 208 /*
209 209 * Get or reset the rt_dptbl values per the user's request.
210 210 */
211 211 /* ARGSUSED */
212 212 static int
213 213 rt_admin(caddr_t uaddr, cred_t *reqpcredp)
214 214 {
215 215 rtadmin_t rtadmin;
216 216 rtdpent_t *tmpdpp;
217 217 size_t userdpsz;
218 218 size_t rtdpsz;
219 219 int i;
220 220
221 221 if (get_udatamodel() == DATAMODEL_NATIVE) {
222 222 if (copyin(uaddr, &rtadmin, sizeof (rtadmin_t)))
223 223 return (EFAULT);
224 224 }
225 225 #ifdef _SYSCALL32_IMPL
226 226 else {
227 227 /* rtadmin struct from ILP32 callers */
228 228 rtadmin32_t rtadmin32;
229 229 if (copyin(uaddr, &rtadmin32, sizeof (rtadmin32_t)))
230 230 return (EFAULT);
231 231 rtadmin.rt_dpents =
232 232 (struct rtdpent *)(uintptr_t)rtadmin32.rt_dpents;
233 233 rtadmin.rt_ndpents = rtadmin32.rt_ndpents;
234 234 rtadmin.rt_cmd = rtadmin32.rt_cmd;
235 235 }
236 236 #endif /* _SYSCALL32_IMPL */
237 237
238 238 rtdpsz = (rt_maxpri + 1) * sizeof (rtdpent_t);
239 239
240 240 switch (rtadmin.rt_cmd) {
241 241
242 242 case RT_GETDPSIZE:
243 243 rtadmin.rt_ndpents = rt_maxpri + 1;
244 244
245 245 if (get_udatamodel() == DATAMODEL_NATIVE) {
246 246 if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
247 247 return (EFAULT);
248 248 }
249 249 #ifdef _SYSCALL32_IMPL
250 250 else {
251 251 /* return rtadmin struct to ILP32 callers */
252 252 rtadmin32_t rtadmin32;
253 253 rtadmin32.rt_dpents =
254 254 (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
255 255 rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
256 256 rtadmin32.rt_cmd = rtadmin.rt_cmd;
257 257 if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
258 258 return (EFAULT);
259 259 }
260 260 #endif /* _SYSCALL32_IMPL */
261 261
262 262 break;
263 263
264 264 case RT_GETDPTBL:
265 265 userdpsz = MIN(rtadmin.rt_ndpents * sizeof (rtdpent_t),
266 266 rtdpsz);
267 267 if (copyout(rt_dptbl, rtadmin.rt_dpents, userdpsz))
268 268 return (EFAULT);
269 269 rtadmin.rt_ndpents = userdpsz / sizeof (rtdpent_t);
270 270
271 271 if (get_udatamodel() == DATAMODEL_NATIVE) {
272 272 if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
273 273 return (EFAULT);
274 274 }
275 275 #ifdef _SYSCALL32_IMPL
276 276 else {
277 277 /* return rtadmin struct to ILP32 callers */
278 278 rtadmin32_t rtadmin32;
279 279 rtadmin32.rt_dpents =
280 280 (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
281 281 rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
282 282 rtadmin32.rt_cmd = rtadmin.rt_cmd;
283 283 if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
284 284 return (EFAULT);
285 285 }
286 286 #endif /* _SYSCALL32_IMPL */
287 287 break;
288 288
289 289 case RT_SETDPTBL:
290 290 /*
291 291 * We require that the requesting process has sufficient
292 292 * priveleges. We also require that the table supplied by
293 293 * the user exactly match the current rt_dptbl in size.
294 294 */
295 295 if (secpolicy_dispadm(reqpcredp) != 0)
296 296 return (EPERM);
297 297 if (rtadmin.rt_ndpents * sizeof (rtdpent_t) != rtdpsz)
298 298 return (EINVAL);
299 299
300 300 /*
301 301 * We read the user supplied table into a temporary buffer
302 302 * where the time quantum values are validated before
303 303 * being copied to the rt_dptbl.
304 304 */
305 305 tmpdpp = kmem_alloc(rtdpsz, KM_SLEEP);
306 306 if (copyin(rtadmin.rt_dpents, tmpdpp, rtdpsz)) {
307 307 kmem_free(tmpdpp, rtdpsz);
308 308 return (EFAULT);
309 309 }
310 310 for (i = 0; i < rtadmin.rt_ndpents; i++) {
311 311
312 312 /*
313 313 * Validate the user supplied time quantum values.
314 314 */
315 315 if (tmpdpp[i].rt_quantum <= 0 &&
316 316 tmpdpp[i].rt_quantum != RT_TQINF) {
317 317 kmem_free(tmpdpp, rtdpsz);
318 318 return (EINVAL);
319 319 }
320 320 }
321 321
322 322 /*
323 323 * Copy the user supplied values over the current rt_dptbl
324 324 * values. The rt_globpri member is read-only so we don't
325 325 * overwrite it.
326 326 */
327 327 mutex_enter(&rt_dptblock);
328 328 for (i = 0; i < rtadmin.rt_ndpents; i++)
329 329 rt_dptbl[i].rt_quantum = tmpdpp[i].rt_quantum;
330 330 mutex_exit(&rt_dptblock);
331 331 kmem_free(tmpdpp, rtdpsz);
332 332 break;
333 333
334 334 default:
335 335 return (EINVAL);
336 336 }
337 337 return (0);
338 338 }
339 339
340 340
341 341 /*
342 342 * Allocate a real-time class specific proc structure and
343 343 * initialize it with the parameters supplied. Also move thread
344 344 * to specified real-time priority.
345 345 */
346 346 /* ARGSUSED */
347 347 static int
348 348 rt_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
349 349 void *bufp)
350 350 {
351 351 rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
352 352 rtproc_t *rtpp;
353 353
354 354 /*
355 355 * For a thread to enter the real-time class the thread
356 356 * which initiates the request must be privileged.
357 357 * This may have been checked previously but if our
358 358 * caller passed us a credential structure we assume it
359 359 * hasn't and we check it here.
360 360 */
361 361 if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
362 362 return (EPERM);
363 363
364 364 rtpp = (rtproc_t *)bufp;
365 365 ASSERT(rtpp != NULL);
366 366
367 367 /*
368 368 * If this thread's lwp is swapped out, it will be brought in
369 369 * when it is put onto the runqueue.
370 370 *
371 371 * Now, Initialize the rtproc structure.
372 372 */
373 373 if (rtkparmsp == NULL) {
374 374 /*
375 375 * Use default values
376 376 */
377 377 rtpp->rt_pri = 0;
378 378 rtpp->rt_pquantum = rt_dptbl[0].rt_quantum;
379 379 rtpp->rt_tqsignal = 0;
380 380 } else {
381 381 /*
382 382 * Use supplied values
383 383 */
384 384 if ((rtkparmsp->rt_cflags & RT_DOPRI) == 0)
385 385 rtpp->rt_pri = 0;
386 386 else
387 387 rtpp->rt_pri = rtkparmsp->rt_pri;
388 388
389 389 if (rtkparmsp->rt_tqntm == RT_TQINF)
390 390 rtpp->rt_pquantum = RT_TQINF;
391 391 else if (rtkparmsp->rt_tqntm == RT_TQDEF ||
392 392 (rtkparmsp->rt_cflags & RT_DOTQ) == 0)
393 393 rtpp->rt_pquantum = rt_dptbl[rtpp->rt_pri].rt_quantum;
394 394 else
395 395 rtpp->rt_pquantum = rtkparmsp->rt_tqntm;
396 396
397 397 if ((rtkparmsp->rt_cflags & RT_DOSIG) == 0)
398 398 rtpp->rt_tqsignal = 0;
399 399 else
400 400 rtpp->rt_tqsignal = rtkparmsp->rt_tqsig;
401 401 }
402 402 rtpp->rt_flags = 0;
403 403 rtpp->rt_tp = t;
404 404 /*
405 405 * Reset thread priority
406 406 */
407 407 thread_lock(t);
408 408 t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
409 409 t->t_cid = cid;
410 410 t->t_cldata = (void *)rtpp;
411 411 t->t_schedflag &= ~TS_RUNQMATCH;
412 412 rt_change_priority(t, rtpp);
413 413 thread_unlock(t);
414 414 /*
415 415 * Link new structure into rtproc list
416 416 */
417 417 mutex_enter(&rt_list_lock);
418 418 rtpp->rt_next = rt_plisthead.rt_next;
419 419 rtpp->rt_prev = &rt_plisthead;
420 420 rt_plisthead.rt_next->rt_prev = rtpp;
421 421 rt_plisthead.rt_next = rtpp;
422 422 mutex_exit(&rt_list_lock);
423 423 return (0);
424 424 }
425 425
426 426
427 427 /*
428 428 * Free rtproc structure of thread.
429 429 */
430 430 static void
431 431 rt_exitclass(void *procp)
432 432 {
433 433 rtproc_t *rtprocp = (rtproc_t *)procp;
434 434
435 435 mutex_enter(&rt_list_lock);
436 436 rtprocp->rt_prev->rt_next = rtprocp->rt_next;
437 437 rtprocp->rt_next->rt_prev = rtprocp->rt_prev;
438 438 mutex_exit(&rt_list_lock);
439 439 kmem_free(rtprocp, sizeof (rtproc_t));
440 440 }
441 441
442 442
443 443 /*
444 444 * Allocate and initialize real-time class specific
445 445 * proc structure for child.
446 446 */
447 447 /* ARGSUSED */
448 448 static int
449 449 rt_fork(kthread_t *t, kthread_t *ct, void *bufp)
450 450 {
451 451 rtproc_t *prtpp;
452 452 rtproc_t *crtpp;
453 453
454 454 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
455 455
456 456 /*
457 457 * Initialize child's rtproc structure
458 458 */
459 459 crtpp = (rtproc_t *)bufp;
460 460 ASSERT(crtpp != NULL);
461 461 prtpp = (rtproc_t *)t->t_cldata;
462 462 thread_lock(t);
463 463 crtpp->rt_timeleft = crtpp->rt_pquantum = prtpp->rt_pquantum;
464 464 crtpp->rt_pri = prtpp->rt_pri;
465 465 crtpp->rt_flags = prtpp->rt_flags & ~RTBACKQ;
466 466 crtpp->rt_tqsignal = prtpp->rt_tqsignal;
467 467
468 468 crtpp->rt_tp = ct;
469 469 thread_unlock(t);
470 470
471 471 /*
472 472 * Link new structure into rtproc list
473 473 */
474 474 ct->t_cldata = (void *)crtpp;
475 475 mutex_enter(&rt_list_lock);
476 476 crtpp->rt_next = rt_plisthead.rt_next;
477 477 crtpp->rt_prev = &rt_plisthead;
478 478 rt_plisthead.rt_next->rt_prev = crtpp;
479 479 rt_plisthead.rt_next = crtpp;
480 480 mutex_exit(&rt_list_lock);
481 481 return (0);
482 482 }
483 483
484 484
485 485 /*
486 486 * The child goes to the back of its dispatcher queue while the
487 487 * parent continues to run after a real time thread forks.
488 488 */
489 489 /* ARGSUSED */
490 490 static void
491 491 rt_forkret(kthread_t *t, kthread_t *ct)
492 492 {
493 493 proc_t *pp = ttoproc(t);
494 494 proc_t *cp = ttoproc(ct);
495 495
496 496 ASSERT(t == curthread);
497 497 ASSERT(MUTEX_HELD(&pidlock));
498 498
499 499 /*
500 500 * Grab the child's p_lock before dropping pidlock to ensure
501 501 * the process does not disappear before we set it running.
502 502 */
503 503 mutex_enter(&cp->p_lock);
504 504 mutex_exit(&pidlock);
505 505 continuelwps(cp);
506 506 mutex_exit(&cp->p_lock);
507 507
508 508 mutex_enter(&pp->p_lock);
509 509 continuelwps(pp);
510 510 mutex_exit(&pp->p_lock);
511 511 }
512 512
513 513
514 514 /*
515 515 * Get information about the real-time class into the buffer
516 516 * pointed to by rtinfop. The maximum configured real-time
517 517 * priority is the only information we supply. We ignore the
518 518 * class and credential arguments because anyone can have this
519 519 * information.
520 520 */
521 521 /* ARGSUSED */
522 522 static int
523 523 rt_getclinfo(void *infop)
524 524 {
525 525 rtinfo_t *rtinfop = (rtinfo_t *)infop;
526 526 rtinfop->rt_maxpri = rt_maxpri;
527 527 return (0);
528 528 }
529 529
530 530 /*
531 531 * Return the user mode scheduling priority range.
532 532 */
533 533 static int
534 534 rt_getclpri(pcpri_t *pcprip)
535 535 {
536 536 pcprip->pc_clpmax = rt_maxpri;
537 537 pcprip->pc_clpmin = 0;
538 538 return (0);
539 539 }
540 540
541 541 static void
542 542 rt_nullsys()
543 543 {
544 544 }
545 545
546 546 /* ARGSUSED */
547 547 static int
548 548 rt_canexit(kthread_t *t, cred_t *cred)
549 549 {
550 550 /*
551 551 * Thread can always leave RT class
552 552 */
553 553 return (0);
554 554 }
555 555
556 556 /*
557 557 * Get the real-time scheduling parameters of the thread pointed to by
558 558 * rtprocp into the buffer pointed to by rtkparmsp.
559 559 */
560 560 static void
561 561 rt_parmsget(kthread_t *t, void *parmsp)
562 562 {
563 563 rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
564 564 rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
565 565
566 566 rtkparmsp->rt_pri = rtprocp->rt_pri;
567 567 rtkparmsp->rt_tqntm = rtprocp->rt_pquantum;
568 568 rtkparmsp->rt_tqsig = rtprocp->rt_tqsignal;
569 569 }
570 570
571 571
572 572
573 573 /*
574 574 * Check the validity of the real-time parameters in the buffer
575 575 * pointed to by rtprmsp.
576 576 * We convert the rtparms buffer from the user supplied format to
577 577 * our internal format (i.e. time quantum expressed in ticks).
578 578 */
579 579 static int
580 580 rt_parmsin(void *prmsp)
581 581 {
582 582 rtparms_t *rtprmsp = (rtparms_t *)prmsp;
583 583 longlong_t ticks;
584 584 uint_t cflags;
585 585
586 586 /*
587 587 * First check the validity of parameters and convert
588 588 * the buffer to kernel format.
589 589 */
590 590 if ((rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) &&
591 591 rtprmsp->rt_pri != RT_NOCHANGE)
592 592 return (EINVAL);
593 593
594 594 cflags = (rtprmsp->rt_pri != RT_NOCHANGE ? RT_DOPRI : 0);
595 595
596 596 if ((rtprmsp->rt_tqsecs == 0 && rtprmsp->rt_tqnsecs == 0) ||
597 597 rtprmsp->rt_tqnsecs >= NANOSEC)
598 598 return (EINVAL);
599 599
600 600 if (rtprmsp->rt_tqnsecs != RT_NOCHANGE)
601 601 cflags |= RT_DOTQ;
602 602
603 603 if (rtprmsp->rt_tqnsecs >= 0) {
604 604 if ((ticks = SEC_TO_TICK((longlong_t)rtprmsp->rt_tqsecs) +
605 605 NSEC_TO_TICK_ROUNDUP(rtprmsp->rt_tqnsecs)) > INT_MAX)
606 606 return (ERANGE);
607 607
608 608 ((rtkparms_t *)rtprmsp)->rt_tqntm = (int)ticks;
609 609 } else {
610 610 if (rtprmsp->rt_tqnsecs != RT_NOCHANGE &&
611 611 rtprmsp->rt_tqnsecs != RT_TQINF &&
612 612 rtprmsp->rt_tqnsecs != RT_TQDEF)
613 613 return (EINVAL);
614 614
615 615 ((rtkparms_t *)rtprmsp)->rt_tqntm = rtprmsp->rt_tqnsecs;
616 616 }
617 617 ((rtkparms_t *)rtprmsp)->rt_cflags = cflags;
618 618
619 619 return (0);
620 620 }
621 621
622 622
623 623 /*
624 624 * Check the validity of the real-time parameters in the pc_vaparms_t
625 625 * structure vaparmsp and put them in the buffer pointed to by rtprmsp.
626 626 * pc_vaparms_t contains (key, value) pairs of parameter.
627 627 * rt_vaparmsin() is the variable parameter version of rt_parmsin().
628 628 */
629 629 static int
630 630 rt_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
631 631 {
632 632 uint_t secs = 0;
633 633 uint_t cnt;
634 634 int nsecs = 0;
635 635 int priflag, secflag, nsecflag, sigflag;
636 636 longlong_t ticks;
637 637 rtkparms_t *rtprmsp = (rtkparms_t *)prmsp;
638 638 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
639 639
640 640
641 641 /*
642 642 * First check the validity of parameters and convert them
643 643 * from the user supplied format to the internal format.
644 644 */
645 645 priflag = secflag = nsecflag = sigflag = 0;
646 646 rtprmsp->rt_cflags = 0;
647 647
648 648 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
649 649 return (EINVAL);
650 650
651 651 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
652 652
653 653 switch (vpp->pc_key) {
654 654 case RT_KY_PRI:
655 655 if (priflag++)
656 656 return (EINVAL);
657 657 rtprmsp->rt_cflags |= RT_DOPRI;
658 658 rtprmsp->rt_pri = (pri_t)vpp->pc_parm;
659 659 if (rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri)
660 660 return (EINVAL);
661 661 break;
662 662
663 663 case RT_KY_TQSECS:
664 664 if (secflag++)
665 665 return (EINVAL);
666 666 rtprmsp->rt_cflags |= RT_DOTQ;
667 667 secs = (uint_t)vpp->pc_parm;
668 668 break;
669 669
670 670 case RT_KY_TQNSECS:
671 671 if (nsecflag++)
672 672 return (EINVAL);
673 673 rtprmsp->rt_cflags |= RT_DOTQ;
674 674 nsecs = (int)vpp->pc_parm;
675 675 break;
676 676
677 677 case RT_KY_TQSIG:
678 678 if (sigflag++)
679 679 return (EINVAL);
680 680 rtprmsp->rt_cflags |= RT_DOSIG;
681 681 rtprmsp->rt_tqsig = (int)vpp->pc_parm;
682 682 if (rtprmsp->rt_tqsig < 0 || rtprmsp->rt_tqsig >= NSIG)
683 683 return (EINVAL);
684 684 break;
685 685
686 686 default:
687 687 return (EINVAL);
688 688 }
689 689 }
690 690
691 691 if (vaparmsp->pc_vaparmscnt == 0) {
692 692 /*
693 693 * Use default parameters.
694 694 */
695 695 rtprmsp->rt_pri = 0;
696 696 rtprmsp->rt_tqntm = RT_TQDEF;
697 697 rtprmsp->rt_tqsig = 0;
698 698 rtprmsp->rt_cflags = RT_DOPRI | RT_DOTQ | RT_DOSIG;
699 699 } else if ((rtprmsp->rt_cflags & RT_DOTQ) != 0) {
700 700 if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
701 701 return (EINVAL);
702 702
703 703 if (nsecs >= 0) {
704 704 if ((ticks = SEC_TO_TICK((longlong_t)secs) +
705 705 NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
706 706 return (ERANGE);
707 707
708 708 rtprmsp->rt_tqntm = (int)ticks;
709 709 } else {
710 710 if (nsecs != RT_TQINF && nsecs != RT_TQDEF)
711 711 return (EINVAL);
712 712 rtprmsp->rt_tqntm = nsecs;
713 713 }
714 714 }
715 715
716 716 return (0);
717 717 }
718 718
719 719 /*
720 720 * Do required processing on the real-time parameter buffer
721 721 * before it is copied out to the user.
722 722 * All we have to do is convert the buffer from kernel to user format
723 723 * (i.e. convert time quantum from ticks to seconds-nanoseconds).
724 724 */
725 725 /* ARGSUSED */
726 726 static int
727 727 rt_parmsout(void *prmsp, pc_vaparms_t *vaparmsp)
728 728 {
729 729 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
730 730
731 731 if (vaparmsp != NULL)
732 732 return (0);
733 733
734 734 if (rtkprmsp->rt_tqntm < 0) {
735 735 /*
736 736 * Quantum field set to special value (e.g. RT_TQINF)
737 737 */
738 738 ((rtparms_t *)rtkprmsp)->rt_tqnsecs = rtkprmsp->rt_tqntm;
739 739 ((rtparms_t *)rtkprmsp)->rt_tqsecs = 0;
740 740 } else {
741 741 /* Convert quantum from ticks to seconds-nanoseconds */
742 742
743 743 timestruc_t ts;
744 744 TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
745 745 ((rtparms_t *)rtkprmsp)->rt_tqsecs = ts.tv_sec;
746 746 ((rtparms_t *)rtkprmsp)->rt_tqnsecs = ts.tv_nsec;
747 747 }
748 748
749 749 return (0);
750 750 }
751 751
752 752
753 753 /*
754 754 * Copy all selected real-time class parameters to the user.
755 755 * The parameters are specified by a key.
756 756 */
757 757 static int
758 758 rt_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
759 759 {
760 760 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
761 761 timestruc_t ts;
762 762 uint_t cnt;
763 763 uint_t secs;
764 764 int nsecs;
765 765 int priflag, secflag, nsecflag, sigflag;
766 766 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
767 767
768 768 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
769 769
770 770 priflag = secflag = nsecflag = sigflag = 0;
771 771
772 772 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
773 773 return (EINVAL);
774 774
775 775 if (rtkprmsp->rt_tqntm < 0) {
776 776 /*
777 777 * Quantum field set to special value (e.g. RT_TQINF).
778 778 */
779 779 secs = 0;
780 780 nsecs = rtkprmsp->rt_tqntm;
781 781 } else {
782 782 /*
783 783 * Convert quantum from ticks to seconds-nanoseconds.
784 784 */
785 785 TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
786 786 secs = ts.tv_sec;
787 787 nsecs = ts.tv_nsec;
788 788 }
789 789
790 790
791 791 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
792 792
793 793 switch (vpp->pc_key) {
794 794 case RT_KY_PRI:
795 795 if (priflag++)
796 796 return (EINVAL);
797 797 if (copyout(&rtkprmsp->rt_pri,
798 798 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
799 799 return (EFAULT);
800 800 break;
801 801
802 802 case RT_KY_TQSECS:
803 803 if (secflag++)
804 804 return (EINVAL);
805 805 if (copyout(&secs, (caddr_t)(uintptr_t)vpp->pc_parm,
806 806 sizeof (uint_t)))
807 807 return (EFAULT);
808 808 break;
809 809
810 810 case RT_KY_TQNSECS:
811 811 if (nsecflag++)
812 812 return (EINVAL);
813 813 if (copyout(&nsecs, (caddr_t)(uintptr_t)vpp->pc_parm,
814 814 sizeof (int)))
815 815 return (EFAULT);
816 816 break;
817 817
818 818 case RT_KY_TQSIG:
819 819 if (sigflag++)
820 820 return (EINVAL);
821 821 if (copyout(&rtkprmsp->rt_tqsig,
822 822 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
823 823 return (EFAULT);
824 824 break;
825 825
826 826 default:
827 827 return (EINVAL);
828 828 }
829 829 }
830 830
831 831 return (0);
832 832 }
833 833
834 834
835 835 /*
836 836 * Set the scheduling parameters of the thread pointed to by rtprocp
837 837 * to those specified in the buffer pointed to by rtkprmsp.
838 838 * Note that the parameters are expected to be in kernel format
839 839 * (i.e. time quantm expressed in ticks). Real time parameters copied
840 840 * in from the user should be processed by rt_parmsin() before they are
841 841 * passed to this function.
842 842 */
843 843 static int
844 844 rt_parmsset(kthread_t *tx, void *prmsp, id_t reqpcid, cred_t *reqpcredp)
845 845 {
846 846 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
847 847 rtproc_t *rtpp = (rtproc_t *)tx->t_cldata;
848 848
849 849 ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
850 850
851 851 /*
852 852 * Basic permissions enforced by generic kernel code
853 853 * for all classes require that a thread attempting
854 854 * to change the scheduling parameters of a target thread
855 855 * be privileged or have a real or effective UID
856 856 * matching that of the target thread. We are not
857 857 * called unless these basic permission checks have
858 858 * already passed. The real-time class requires in addition
859 859 * that the requesting thread be real-time unless it is privileged.
860 860 * This may also have been checked previously but if our caller
861 861 * passes us a credential structure we assume it hasn't and
862 862 * we check it here.
863 863 */
864 864 if (reqpcredp != NULL && reqpcid != rt_cid &&
865 865 secpolicy_raisepriority(reqpcredp) != 0)
866 866 return (EPERM);
867 867
868 868 thread_lock(tx);
869 869 if ((rtkprmsp->rt_cflags & RT_DOPRI) != 0) {
870 870 rtpp->rt_pri = rtkprmsp->rt_pri;
871 871 rt_change_priority(tx, rtpp);
872 872 }
873 873 if (rtkprmsp->rt_tqntm == RT_TQINF)
874 874 rtpp->rt_pquantum = RT_TQINF;
875 875 else if (rtkprmsp->rt_tqntm == RT_TQDEF)
876 876 rtpp->rt_timeleft = rtpp->rt_pquantum =
877 877 rt_dptbl[rtpp->rt_pri].rt_quantum;
878 878 else if ((rtkprmsp->rt_cflags & RT_DOTQ) != 0)
879 879 rtpp->rt_timeleft = rtpp->rt_pquantum = rtkprmsp->rt_tqntm;
880 880
881 881 if ((rtkprmsp->rt_cflags & RT_DOSIG) != 0)
882 882 rtpp->rt_tqsignal = rtkprmsp->rt_tqsig;
883 883
884 884 thread_unlock(tx);
885 885 return (0);
886 886 }
887 887
888 888
889 889 /*
890 890 * Arrange for thread to be placed in appropriate location
891 891 * on dispatcher queue. Runs at splhi() since the clock
892 892 * interrupt can cause RTBACKQ to be set.
893 893 */
894 894 static void
895 895 rt_preempt(kthread_t *t)
896 896 {
897 897 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
898 898 klwp_t *lwp;
899 899
900 900 ASSERT(THREAD_LOCK_HELD(t));
901 901
902 902 /*
903 903 * If the state is user I allow swapping because I know I won't
904 904 * be holding any locks.
905 905 */
906 906 if ((lwp = curthread->t_lwp) != NULL && lwp->lwp_state == LWP_USER)
907 907 t->t_schedflag &= ~TS_DONT_SWAP;
908 908 if ((rtpp->rt_flags & RTBACKQ) != 0) {
909 909 rtpp->rt_timeleft = rtpp->rt_pquantum;
910 910 rtpp->rt_flags &= ~RTBACKQ;
911 911 setbackdq(t);
912 912 } else
913 913 setfrontdq(t);
914 914
915 915 }
916 916
917 917 /*
918 918 * Return the global priority associated with this rt_pri.
919 919 */
920 920 static pri_t
921 921 rt_globpri(kthread_t *t)
922 922 {
923 923 rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
924 924 return (rt_dptbl[rtprocp->rt_pri].rt_globpri);
925 925 }
926 926
927 927 static void
928 928 rt_setrun(kthread_t *t)
929 929 {
930 930 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
931 931
932 932 ASSERT(THREAD_LOCK_HELD(t));
933 933
934 934 rtpp->rt_timeleft = rtpp->rt_pquantum;
935 935 rtpp->rt_flags &= ~RTBACKQ;
936 936 setbackdq(t);
937 937 }
938 938
939 939 /*
940 940 * Returns the priority of the thread, -1 if the thread is loaded or ineligible
941 941 * for swapin.
942 942 *
943 943 * FX and RT threads are designed so that they don't swapout; however, it
944 944 * is possible that while the thread is swapped out and in another class, it
945 945 * can be changed to FX or RT. Since these threads should be swapped in as
946 946 * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
947 947 * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT
948 948 * threads.
949 949 */
950 950 /* ARGSUSED */
951 951 static pri_t
952 952 rt_swapin(kthread_t *t, int flags)
953 953 {
954 954 pri_t tpri = -1;
955 955
956 956 ASSERT(THREAD_LOCK_HELD(t));
957 957
958 958 if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
959 959 tpri = (pri_t)SHRT_MAX;
960 960 }
961 961
962 962 return (tpri);
963 963 }
964 964
965 965 /*
966 966 * Return an effective priority for swapout.
967 967 */
968 968 /* ARGSUSED */
969 969 static pri_t
970 970 rt_swapout(kthread_t *t, int flags)
971 971 {
972 972 ASSERT(THREAD_LOCK_HELD(t));
973 973
974 974 return (-1);
975 975 }
976 976
977 977 /*
978 978 * Check for time slice expiration (unless thread has infinite time
979 979 * slice). If time slice has expired arrange for thread to be preempted
980 980 * and placed on back of queue.
981 981 */
982 982 static void
983 983 rt_tick(kthread_t *t)
984 984 {
985 985 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
986 986
987 987 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
988 988
989 989 thread_lock(t);
990 990 if ((rtpp->rt_pquantum != RT_TQINF && --rtpp->rt_timeleft == 0) ||
991 991 (t->t_state == TS_ONPROC && DISP_MUST_SURRENDER(t))) {
992 992 if (rtpp->rt_timeleft == 0 && rtpp->rt_tqsignal) {
993 993 thread_unlock(t);
994 994 sigtoproc(ttoproc(t), t, rtpp->rt_tqsignal);
995 995 thread_lock(t);
996 996 }
997 997 rtpp->rt_flags |= RTBACKQ;
998 998 cpu_surrender(t);
999 999 }
1000 1000 thread_unlock(t);
1001 1001 }
1002 1002
1003 1003
1004 1004 /*
1005 1005 * Place the thread waking up on the dispatcher queue.
1006 1006 */
1007 1007 static void
1008 1008 rt_wakeup(kthread_t *t)
1009 1009 {
1010 1010 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1011 1011
1012 1012 ASSERT(THREAD_LOCK_HELD(t));
1013 1013
1014 1014 rtpp->rt_timeleft = rtpp->rt_pquantum;
1015 1015 rtpp->rt_flags &= ~RTBACKQ;
1016 1016 setbackdq(t);
1017 1017 }
1018 1018
1019 1019 static void
1020 1020 rt_yield(kthread_t *t)
1021 1021 {
1022 1022 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1023 1023
1024 1024 ASSERT(t == curthread);
1025 1025 ASSERT(THREAD_LOCK_HELD(t));
1026 1026
1027 1027 rtpp->rt_flags &= ~RTBACKQ;
1028 1028 setbackdq(t);
1029 1029 }
1030 1030
1031 1031 /* ARGSUSED */
1032 1032 static int
1033 1033 rt_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1034 1034 {
1035 1035 return (EINVAL);
1036 1036 }
1037 1037
1038 1038 /*
1039 1039 * Increment the priority of the specified thread by incr and
1040 1040 * return the new value in *retvalp.
1041 1041 */
1042 1042 static int
1043 1043 rt_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1044 1044 {
1045 1045 int newpri;
1046 1046 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1047 1047 rtkparms_t rtkparms;
1048 1048
1049 1049 /* If there's no change to the priority, just return current setting */
1050 1050 if (incr == 0) {
1051 1051 *retvalp = rtpp->rt_pri;
1052 1052 return (0);
1053 1053 }
1054 1054
1055 1055 newpri = rtpp->rt_pri + incr;
1056 1056 if (newpri > rt_maxpri || newpri < 0)
1057 1057 return (EINVAL);
1058 1058
1059 1059 *retvalp = newpri;
1060 1060 rtkparms.rt_pri = newpri;
1061 1061 rtkparms.rt_tqntm = RT_NOCHANGE;
1062 1062 rtkparms.rt_tqsig = 0;
1063 1063 rtkparms.rt_cflags = RT_DOPRI;
1064 1064 return (rt_parmsset(t, &rtkparms, rt_cid, cr));
1065 1065 }
1066 1066
1067 1067 static int
1068 1068 rt_alloc(void **p, int flag)
1069 1069 {
1070 1070 void *bufp;
1071 1071 bufp = kmem_alloc(sizeof (rtproc_t), flag);
1072 1072 if (bufp == NULL) {
1073 1073 return (ENOMEM);
1074 1074 } else {
1075 1075 *p = bufp;
1076 1076 return (0);
1077 1077 }
1078 1078 }
1079 1079
1080 1080 static void
1081 1081 rt_free(void *bufp)
1082 1082 {
1083 1083 if (bufp)
1084 1084 kmem_free(bufp, sizeof (rtproc_t));
1085 1085 }
1086 1086
1087 1087 static void
1088 1088 rt_change_priority(kthread_t *t, rtproc_t *rtpp)
1089 1089 {
1090 1090 pri_t new_pri;
1091 1091
1092 1092 ASSERT(THREAD_LOCK_HELD(t));
1093 1093
1094 1094 new_pri = rt_dptbl[rtpp->rt_pri].rt_globpri;
1095 1095
1096 1096 t->t_cpri = rtpp->rt_pri;
1097 1097 if (t == curthread || t->t_state == TS_ONPROC) {
1098 1098 cpu_t *cp = t->t_disp_queue->disp_cpu;
1099 1099 THREAD_CHANGE_PRI(t, new_pri);
1100 1100 if (t == cp->cpu_dispthread)
1101 1101 cp->cpu_dispatch_pri = DISP_PRIO(t);
1102 1102 if (DISP_MUST_SURRENDER(t)) {
1103 1103 rtpp->rt_flags |= RTBACKQ;
1104 1104 cpu_surrender(t);
1105 1105 } else {
1106 1106 rtpp->rt_timeleft = rtpp->rt_pquantum;
1107 1107 }
1108 1108 } else {
1109 1109 /*
1110 1110 * When the priority of a thread is changed,
1111 1111 * it may be necessary to adjust its position
1112 1112 * on a sleep queue or dispatch queue. The
1113 1113 * function thread_change_pri() accomplishes this.
1114 1114 */
1115 1115 if (thread_change_pri(t, new_pri, 0)) {
1116 1116 /*
1117 1117 * The thread was on a run queue.
1118 1118 * Reset its CPU timeleft.
1119 1119 */
1120 1120 rtpp->rt_timeleft = rtpp->rt_pquantum;
1121 1121 } else {
1122 1122 rtpp->rt_flags |= RTBACKQ;
1123 1123 }
1124 1124 }
1125 1125 }
|
↓ open down ↓ |
1125 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX