summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/timer.c
blob: f587430625fbe48f70825c77e4494512c0a095e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2020 Joyent, Inc.
 */

#include <sys/timer.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <sys/port_impl.h>
#include <sys/port_kernel.h>
#include <sys/contract/process_impl.h>

static kmem_cache_t *clock_timer_cache;
static clock_backend_t *clock_backend[CLOCK_MAX];
static int timer_port_callback(void *, int *, pid_t, int, void *);
static void timer_close_port(void *, int, pid_t, int);

#define	CLOCK_BACKEND(clk) \
	((clk) < CLOCK_MAX && (clk) >= 0 ? clock_backend[(clk)] : NULL)

/*
 * Tunable to increase the maximum number of POSIX timers per-process.  This
 * may _only_ be tuned in /etc/system or by patching the kernel binary; it
 * _cannot_ be tuned on a running system.
 */
int timer_max = _TIMER_MAX;

/*
 * timer_lock() locks the specified interval timer.  It doesn't look at the
 * ITLK_REMOVE bit; it's up to callers to look at this if they need to
 * care.  p_lock must be held on entry; it may be dropped and reaquired,
 * but timer_lock() will always return with p_lock held.
 *
 * Note that timer_create() doesn't call timer_lock(); it creates timers
 * with the ITLK_LOCKED bit explictly set.
 */
static void
timer_lock(proc_t *p, itimer_t *it)
{
	ASSERT(MUTEX_HELD(&p->p_lock));

	while (it->it_lock & ITLK_LOCKED) {
		it->it_blockers++;
		cv_wait(&it->it_cv, &p->p_lock);
		it->it_blockers--;
	}

	it->it_lock |= ITLK_LOCKED;
}

/*
 * timer_unlock() unlocks the specified interval timer, waking up any
 * waiters.  p_lock must be held on entry; it will not be dropped by
 * timer_unlock().
 */
/* ARGSUSED */
static void
timer_unlock(proc_t *p, itimer_t *it)
{
	ASSERT(MUTEX_HELD(&p->p_lock));
	ASSERT(it->it_lock & ITLK_LOCKED);
	it->it_lock &= ~ITLK_LOCKED;
	cv_signal(&it->it_cv);
}

/*
 * timer_delete_locked() takes a proc pointer, timer ID and locked interval
 * timer, and deletes the specified timer.  It must be called with p_lock
 * held, and cannot be called on a timer which already has ITLK_REMOVE set;
 * the caller must check this.  timer_delete_locked() will set the ITLK_REMOVE
 * bit and will iteratively unlock and lock the interval timer until all
 * blockers have seen the ITLK_REMOVE and cleared out.  It will then zero
 * out the specified entry in the p_itimer array, and call into the clock
 * backend to complete the deletion.
 *
 * This function will always return with p_lock held.
 */
static void
timer_delete_locked(proc_t *p, timer_t tid, itimer_t *it)
{
	ASSERT(MUTEX_HELD(&p->p_lock));
	ASSERT(!(it->it_lock & ITLK_REMOVE));
	ASSERT(it->it_lock & ITLK_LOCKED);

	it->it_lock |= ITLK_REMOVE;

	/*
	 * If there are threads waiting to lock this timer, we'll unlock
	 * the timer, and block on the cv.  Threads blocking our removal will
	 * have the opportunity to run; when they see the ITLK_REMOVE flag
	 * set, they will immediately unlock the timer.
	 */
	while (it->it_blockers) {
		timer_unlock(p, it);
		cv_wait(&it->it_cv, &p->p_lock);
		timer_lock(p, it);
	}

	ASSERT(p->p_itimer_sz > tid);
	ASSERT(p->p_itimer[tid] == it);
	p->p_itimer[tid] = NULL;

	/*
	 * No one is blocked on this timer, and no one will be (we've set
	 * p_itimer[tid] to be NULL; no one can find it).  Now we call into
	 * the clock backend to delete the timer; it is up to the backend to
	 * guarantee that timer_fire() has completed (and will never again
	 * be called) for this timer.
	 */
	mutex_exit(&p->p_lock);

	it->it_backend->clk_timer_delete(it);

	if (it->it_flags & IT_PORT) {
		mutex_enter(&it->it_mutex);
		if (it->it_portev) {
			port_kevent_t	*pev;
			/* dissociate timer from the event port */
			(void) port_dissociate_ksource(it->it_portfd,
			    PORT_SOURCE_TIMER, (port_source_t *)it->it_portsrc);
			pev = (port_kevent_t *)it->it_portev;
			it->it_portev = NULL;
			it->it_flags &= ~IT_PORT;
			it->it_pending = 0;
			mutex_exit(&it->it_mutex);
			(void) port_remove_done_event(pev);
			port_free_event(pev);
		} else {
			mutex_exit(&it->it_mutex);
		}
	}

	mutex_enter(&p->p_lock);

	/*
	 * We need to be careful freeing the sigqueue for this timer;
	 * if a signal is pending, the sigqueue needs to be freed
	 * synchronously in siginfofree().  The need to free the sigqueue
	 * in siginfofree() is indicated by setting sq_func to NULL.
	 */
	if (it->it_pending > 0) {
		it->it_sigq->sq_func = NULL;
	} else {
		kmem_free(it->it_sigq, sizeof (sigqueue_t));
	}

	ASSERT(it->it_blockers == 0);
	kmem_cache_free(clock_timer_cache, it);
}

/*
 * timer_grab() and its companion routine, timer_release(), are wrappers
 * around timer_lock()/_unlock() which allow the timer_*(3C) routines to
 * (a) share error handling code and (b) not grab p_lock themselves.  Routines
 * which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
 * must call timer_lock()/_unlock() explictly.
 *
 * timer_grab() takes a proc and a timer ID, and returns a pointer to a
 * locked interval timer.  p_lock must _not_ be held on entry; timer_grab()
 * may acquire p_lock, but will always return with p_lock dropped.
 *
 * If timer_grab() fails, it will return NULL.  timer_grab() will fail if
 * one or more of the following is true:
 *
 *  (a)	The specified timer ID is out of range.
 *
 *  (b)	The specified timer ID does not correspond to a timer ID returned
 *	from timer_create(3C).
 *
 *  (c)	The specified timer ID is currently being removed.
 *
 */
static itimer_t *
timer_grab(proc_t *p, timer_t tid)
{
	itimer_t *it;

	if (tid < 0) {
		return (NULL);
	}

	mutex_enter(&p->p_lock);
	if (p->p_itimer == NULL || tid >= p->p_itimer_sz ||
	    (it = p->p_itimer[tid]) == NULL) {
		mutex_exit(&p->p_lock);
		return (NULL);
	}

	/* This may drop p_lock temporarily. */
	timer_lock(p, it);

	if (it->it_lock & ITLK_REMOVE) {
		/*
		 * Someone is removing this timer; it will soon be invalid.
		 */
		timer_unlock(p, it);
		mutex_exit(&p->p_lock);
		return (NULL);
	}

	mutex_exit(&p->p_lock);

	return (it);
}

/*
 * timer_release() releases a timer acquired with timer_grab().  p_lock
 * should not be held on entry; timer_release() will acquire p_lock but
 * will drop it before returning.
 */
void
timer_release(proc_t *p, itimer_t *it)
{
	mutex_enter(&p->p_lock);
	timer_unlock(p, it);
	mutex_exit(&p->p_lock);
}

/*
 * timer_delete_grabbed() deletes a timer acquired with timer_grab().
 * p_lock should not be held on entry; timer_delete_grabbed() will acquire
 * p_lock, but will drop it before returning.
 */
void
timer_delete_grabbed(proc_t *p, timer_t tid, itimer_t *it)
{
	mutex_enter(&p->p_lock);
	timer_delete_locked(p, tid, it);
	mutex_exit(&p->p_lock);
}

void
clock_timer_init()
{
	clock_timer_cache = kmem_cache_create("timer_cache",
	    sizeof (itimer_t), 0, NULL, NULL, NULL, NULL, NULL, 0);

	/*
	 * Push the timer_max limit up to at least 4 * NCPU.  Due to the way
	 * NCPU is defined, proper initialization of the timer limit is
	 * performed at runtime.
	 */
	timer_max = MAX(NCPU * 4, timer_max);
}

void
clock_add_backend(clockid_t clock, clock_backend_t *backend)
{
	ASSERT(clock >= 0 && clock < CLOCK_MAX);
	ASSERT(clock_backend[clock] == NULL);

	clock_backend[clock] = backend;
}

clock_backend_t *
clock_get_backend(clockid_t clock)
{
	if (clock < 0 || clock >= CLOCK_MAX)
		return (NULL);

	return (clock_backend[clock]);
}

int
clock_settime(clockid_t clock, timespec_t *tp)
{
	timespec_t t;
	clock_backend_t *backend;
	int error;

	if ((backend = CLOCK_BACKEND(clock)) == NULL)
		return (set_errno(EINVAL));

	if (secpolicy_settime(CRED()) != 0)
		return (set_errno(EPERM));

	if (get_udatamodel() == DATAMODEL_NATIVE) {
		if (copyin(tp, &t, sizeof (timespec_t)) != 0)
			return (set_errno(EFAULT));
	} else {
		timespec32_t t32;

		if (copyin(tp, &t32, sizeof (timespec32_t)) != 0)
			return (set_errno(EFAULT));

		TIMESPEC32_TO_TIMESPEC(&t, &t32);
	}

	if (itimerspecfix(&t))
		return (set_errno(EINVAL));

	error = backend->clk_clock_settime(&t);

	if (error)
		return (set_errno(error));

	return (0);
}

int
clock_gettime(clockid_t clock, timespec_t *tp)
{
	timespec_t t;
	clock_backend_t *backend;
	int error;

	if ((backend = CLOCK_BACKEND(clock)) == NULL)
		return (set_errno(EINVAL));

	error = backend->clk_clock_gettime(&t);

	if (error)
		return (set_errno(error));

	if (get_udatamodel() == DATAMODEL_NATIVE) {
		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
			return (set_errno(EFAULT));
	} else {
		timespec32_t t32;

		if (TIMESPEC_OVERFLOW(&t))
			return (set_errno(EOVERFLOW));
		TIMESPEC_TO_TIMESPEC32(&t32, &t);

		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
			return (set_errno(EFAULT));
	}

	return (0);
}

int
clock_getres(clockid_t clock, timespec_t *tp)
{
	timespec_t t;
	clock_backend_t *backend;
	int error;

	/*
	 * Strangely, the standard defines clock_getres() with a NULL tp
	 * to do nothing (regardless of the validity of the specified
	 * clock_id).  Go figure.
	 */
	if (tp == NULL)
		return (0);

	if ((backend = CLOCK_BACKEND(clock)) == NULL)
		return (set_errno(EINVAL));

	error = backend->clk_clock_getres(&t);

	if (error)
		return (set_errno(error));

	if (get_udatamodel() == DATAMODEL_NATIVE) {
		if (copyout(&t, tp, sizeof (timespec_t)) != 0)
			return (set_errno(EFAULT));
	} else {
		timespec32_t t32;

		if (TIMESPEC_OVERFLOW(&t))
			return (set_errno(EOVERFLOW));
		TIMESPEC_TO_TIMESPEC32(&t32, &t);

		if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
			return (set_errno(EFAULT));
	}

	return (0);
}

void
timer_signal(sigqueue_t *sigq)
{
	itimer_t *it = (itimer_t *)sigq->sq_backptr;

	/*
	 * There are some conditions during a fork or an exit when we can
	 * call siginfofree() without p_lock held.  To prevent a race
	 * between timer_signal() and timer_fire() with regard to it_pending,
	 * we therefore acquire it_mutex in both paths.
	 */
	mutex_enter(&it->it_mutex);
	ASSERT(it->it_pending > 0);
	it->it_overrun = it->it_pending - 1;
	it->it_pending = 0;
	mutex_exit(&it->it_mutex);
}

/*
 * This routine is called from the clock backend.
 */
static void
timer_fire(itimer_t *it)
{
	proc_t *p = NULL;
	int proc_lock_held;

	if (it->it_flags & IT_SIGNAL) {
		/*
		 * See the comment in timer_signal() for why it is not
		 * sufficient to only grab p_lock here. Because p_lock can be
		 * held on entry to timer_signal(), the lock ordering is
		 * necessarily p_lock before it_mutex.
		 */

		p = it->it_proc;
		proc_lock_held = 1;
		mutex_enter(&p->p_lock);
	} else {
		/*
		 * IT_PORT:
		 * If a timer was ever programmed to send events to a port,
		 * the IT_PORT flag will remain set until:
		 * a) the timer is deleted (see timer_delete_locked()) or
		 * b) the port is being closed (see timer_close_port()).
		 * Both cases are synchronized with the it_mutex.
		 * We don't need to use the p_lock because it is only
		 * required in the IT_SIGNAL case.
		 * If IT_PORT was set and the port is being closed then
		 * the timer notification is set to NONE. In such a case
		 * the timer itself and the it_pending counter remain active
		 * until the application deletes the counter or the process
		 * exits.
		 */
		proc_lock_held = 0;
	}
	mutex_enter(&it->it_mutex);

	if (it->it_pending > 0) {
		if (it->it_pending < INT_MAX)
			it->it_pending++;
		mutex_exit(&it->it_mutex);
	} else {
		if (it->it_flags & IT_PORT) {
			it->it_pending = 1;
			port_send_event((port_kevent_t *)it->it_portev);
			mutex_exit(&it->it_mutex);
		} else if (it->it_flags & IT_CALLBACK) {
			it->it_cb_func(it);
			ASSERT(MUTEX_NOT_HELD(&it->it_mutex));
		} else if (it->it_flags & IT_SIGNAL) {
			it->it_pending = 1;
			mutex_exit(&it->it_mutex);
			sigaddqa(p, NULL, it->it_sigq);
		} else {
			mutex_exit(&it->it_mutex);
		}
	}

	if (proc_lock_held)
		mutex_exit(&p->p_lock);
}

/*
 * Find an unused (i.e. NULL) entry in p->p_itimer and set *id to the
 * index of the unused entry, growing p->p_itimer as necessary (up to timer_max
 * entries). Returns B_TRUE (with *id set) on success, B_FALSE on failure
 * (e.g. the process already has the maximum number of allowed timers
 * allocated).
 */
static boolean_t
timer_get_id(proc_t *p, timer_t *id)
{
	itimer_t **itp = NULL, **itp_new;
	uint_t target_sz;
	uint_t i;

	ASSERT(MUTEX_HELD(&p->p_lock));

	if (p->p_itimer == NULL) {
		/*
		 * No timers have been allocated for this process, allocate
		 * the initial array.
		 */
		ASSERT0(p->p_itimer_sz);
		target_sz = _TIMER_ALLOC_INIT;

		mutex_exit(&p->p_lock);
		itp_new = kmem_zalloc(target_sz * sizeof (itimer_t *),
		    KM_SLEEP);
		mutex_enter(&p->p_lock);

		if (p->p_itimer == NULL) {
			/*
			 * As long as no other thread beat us to allocating
			 * the initial p_itimer array, use what we allocated.
			 * Since we just allocated it, we know slot 0 is
			 * free.
			 */
			p->p_itimer = itp_new;
			p->p_itimer_sz = target_sz;
			i = 0;
			goto done;
		}

		/*
		 * Another thread beat us to allocating the initial array.
		 * Proceed to searching for an empty slot and growing the
		 * array if needed.
		 */
		kmem_free(itp_new, target_sz * sizeof (itimer_t *));
	}

retry:
	/* Use the first empty slot (if any exist) */
	for (i = 0; i < p->p_itimer_sz; i++) {
		if (p->p_itimer[i] == NULL) {
			goto done;
		}
	}

	/* No empty slots, try to grow p->p_itimer and retry */
	target_sz = p->p_itimer_sz * 2;
	if (target_sz > timer_max || target_sz > INT_MAX ||
	    target_sz < p->p_itimer_sz) {
		/* Protect against exceeding the max or overflow */
		return (B_FALSE);
	}

	mutex_exit(&p->p_lock);
	itp_new = kmem_zalloc(target_sz * sizeof (itimer_t *), KM_SLEEP);
	mutex_enter(&p->p_lock);

	if (target_sz <= p->p_itimer_sz) {
		/*
		 * A racing thread performed the resize while we were
		 * waiting outside p_lock.  Discard our now-useless
		 * allocation and retry.
		 */
		kmem_free(itp_new, target_sz * sizeof (itimer_t *));
		goto retry;
	}

	ASSERT3P(p->p_itimer, !=, NULL);
	bcopy(p->p_itimer, itp_new, p->p_itimer_sz * sizeof (itimer_t *));
	kmem_free(p->p_itimer, p->p_itimer_sz * sizeof (itimer_t *));

	/*
	 * Short circuit to use the first free entry in the new allocation.
	 * It's possible that other lower-indexed timers were freed while
	 * p_lock was dropped, but skipping over them is not harmful at all.
	 * In the common case, we skip the need to walk over an array filled
	 * with timers before arriving at the slot we know is fresh from the
	 * allocation.
	 */
	i = p->p_itimer_sz;

	p->p_itimer = itp_new;
	p->p_itimer_sz = target_sz;

done:
	ASSERT3U(i, <=, INT_MAX);
	*id = (timer_t)i;
	return (B_TRUE);
}

/*
 * Setup a timer
 *
 * This allocates an itimer_t (including a timer_t ID and slot in the process),
 * wires it up according to the provided sigevent, and associates it with the
 * desired clock backend.  Upon successful completion, the timer will be
 * locked, preventing it from being armed via timer_settime() or deleted via
 * timer_delete().  This gives the caller a chance to perform any last minute
 * manipulations (such as configuring the IT_CALLBACK functionality and/or
 * copying the timer_t out to userspace) before using timer_release() to unlock
 * it or timer_delete_grabbed() to delete it.
 */
int
timer_setup(clock_backend_t *backend, struct sigevent *evp, port_notify_t *pnp,
    itimer_t **itp, timer_t *tidp)
{
	proc_t *p = curproc;
	int error = 0;
	itimer_t *it;
	sigqueue_t *sigq;
	timer_t tid;

	/*
	 * We'll allocate our sigqueue now, before we grab p_lock.
	 * If we can't find an empty slot, we'll free it before returning.
	 */
	sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);

	/*
	 * Allocate a timer and choose a slot for it.
	 */
	it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP);
	bzero(it, sizeof (*it));
	mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL);

	mutex_enter(&p->p_lock);
	if (!timer_get_id(p, &tid)) {
		mutex_exit(&p->p_lock);
		kmem_free(sigq, sizeof (sigqueue_t));
		return (set_errno(EAGAIN));
	}

	ASSERT(tid < p->p_itimer_sz && p->p_itimer[tid] == NULL);

	/*
	 * If we develop other notification mechanisms, this will need
	 * to call into (yet another) backend.
	 */
	sigq->sq_info.si_signo = evp->sigev_signo;
	sigq->sq_info.si_value = evp->sigev_value;
	sigq->sq_info.si_code = SI_TIMER;
	sigq->sq_info.si_pid = p->p_pid;
	sigq->sq_info.si_ctid = PRCTID(p);
	sigq->sq_info.si_zoneid = getzoneid();
	sigq->sq_info.si_uid = crgetruid(CRED());
	sigq->sq_func = timer_signal;
	sigq->sq_next = NULL;
	sigq->sq_backptr = it;
	it->it_sigq = sigq;
	it->it_backend = backend;
	it->it_lock = ITLK_LOCKED;

	if (evp->sigev_notify == SIGEV_THREAD ||
	    evp->sigev_notify == SIGEV_PORT) {
		int port;
		port_kevent_t *pkevp = NULL;

		ASSERT(pnp != NULL);

		/*
		 * This timer is programmed to use event port notification when
		 * the timer fires:
		 * - allocate a port event structure and prepare it to be sent
		 *   to the port as soon as the timer fires.
		 * - when the timer fires :
		 *   - if event structure was already sent to the port then this
		 *	is a timer fire overflow => increment overflow counter.
		 *   - otherwise send pre-allocated event structure to the port.
		 * - the events field of the port_event_t structure counts the
		 *   number of timer fired events.
		 * - The event structured is allocated using the
		 *   PORT_ALLOC_CACHED flag.
		 *   This flag indicates that the timer itself will manage and
		 *   free the event structure when required.
		 */

		it->it_flags |= IT_PORT;
		port = pnp->portnfy_port;

		/* associate timer as event source with the port */
		error = port_associate_ksource(port, PORT_SOURCE_TIMER,
		    (port_source_t **)&it->it_portsrc, timer_close_port,
		    (void *)it, NULL);
		if (error) {
			mutex_exit(&p->p_lock);
			kmem_cache_free(clock_timer_cache, it);
			kmem_free(sigq, sizeof (sigqueue_t));
			return (error);
		}

		/* allocate an event structure/slot */
		error = port_alloc_event(port, PORT_ALLOC_SCACHED,
		    PORT_SOURCE_TIMER, &pkevp);
		if (error) {
			(void) port_dissociate_ksource(port, PORT_SOURCE_TIMER,
			    (port_source_t *)it->it_portsrc);
			mutex_exit(&p->p_lock);
			kmem_cache_free(clock_timer_cache, it);
			kmem_free(sigq, sizeof (sigqueue_t));
			return (error);
		}

		/* initialize event data */
		port_init_event(pkevp, tid, pnp->portnfy_user,
		    timer_port_callback, it);
		it->it_portev = pkevp;
		it->it_portfd = port;
	} else {
		if (evp->sigev_notify == SIGEV_SIGNAL)
			it->it_flags |= IT_SIGNAL;
	}

	/* Populate the slot now that the timer is prepped. */
	p->p_itimer[tid] = it;
	mutex_exit(&p->p_lock);

	/*
	 * Call on the backend to verify the event argument (or return
	 * EINVAL if this clock type does not support timers).
	 */
	if ((error = backend->clk_timer_create(it, timer_fire)) != 0)
		goto err;

	it->it_lwp = ttolwp(curthread);
	it->it_proc = p;

	*itp = it;
	*tidp = tid;
	return (0);

err:
	/*
	 * If we're here, an error has occurred late in the timer creation
	 * process.  We need to regrab p_lock, and delete the incipient timer.
	 * Since we never unlocked the timer (it was born locked), it's
	 * impossible for a removal to be pending.
	 */
	ASSERT(!(it->it_lock & ITLK_REMOVE));
	timer_delete_grabbed(p, tid, it);

	return (error);
}


int
timer_create(clockid_t clock, struct sigevent *evp, timer_t *tidp)
{
	int error = 0;
	proc_t *p = curproc;
	clock_backend_t *backend;
	struct sigevent ev;
	itimer_t *it;
	timer_t tid;
	port_notify_t tim_pnevp;

	if ((backend = CLOCK_BACKEND(clock)) == NULL)
		return (set_errno(EINVAL));

	if (evp != NULL) {
		/*
		 * short copyin() for binary compatibility
		 * fetch oldsigevent to determine how much to copy in.
		 */
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(evp, &ev, sizeof (struct oldsigevent)))
				return (set_errno(EFAULT));

			if (ev.sigev_notify == SIGEV_PORT ||
			    ev.sigev_notify == SIGEV_THREAD) {
				if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp,
				    sizeof (port_notify_t)))
					return (set_errno(EFAULT));
			}
#ifdef	_SYSCALL32_IMPL
		} else {
			struct sigevent32 ev32;
			port_notify32_t tim_pnevp32;

			if (copyin(evp, &ev32, sizeof (struct oldsigevent32)))
				return (set_errno(EFAULT));
			ev.sigev_notify = ev32.sigev_notify;
			ev.sigev_signo = ev32.sigev_signo;
			/*
			 * See comment in sigqueue32() on handling of 32-bit
			 * sigvals in a 64-bit kernel.
			 */
			ev.sigev_value.sival_int = ev32.sigev_value.sival_int;
			if (ev.sigev_notify == SIGEV_PORT ||
			    ev.sigev_notify == SIGEV_THREAD) {
				if (copyin((void *)(uintptr_t)
				    ev32.sigev_value.sival_ptr,
				    (void *)&tim_pnevp32,
				    sizeof (port_notify32_t)))
					return (set_errno(EFAULT));
				tim_pnevp.portnfy_port =
				    tim_pnevp32.portnfy_port;
				tim_pnevp.portnfy_user =
				    (void *)(uintptr_t)tim_pnevp32.portnfy_user;
			}
#endif
		}
		switch (ev.sigev_notify) {
		case SIGEV_NONE:
			break;
		case SIGEV_SIGNAL:
			if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG)
				return (set_errno(EINVAL));
			break;
		case SIGEV_THREAD:
		case SIGEV_PORT:
			break;
		default:
			return (set_errno(EINVAL));
		}
	} else {
		/*
		 * Use the clock's default sigevent (this is a structure copy).
		 */
		ev = backend->clk_default;
	}

	if ((error = timer_setup(backend, &ev, &tim_pnevp, &it, &tid)) != 0) {
		return (set_errno(error));
	}

	/*
	 * Populate si_value with the timer ID if no sigevent was passed in.
	 */
	if (evp == NULL) {
		it->it_sigq->sq_info.si_value.sival_int = tid;
	}

	if (copyout(&tid, tidp, sizeof (timer_t)) != 0) {
		timer_delete_grabbed(p, tid, it);
		return (set_errno(EFAULT));
	}

	/*
	 * If we're here, then we have successfully created the timer; we
	 * just need to release the timer and return.
	 */
	timer_release(p, it);

	return (0);
}


int
timer_gettime(timer_t tid, itimerspec_t *val)
{
	proc_t *p = curproc;
	itimer_t *it;
	itimerspec_t when;
	int error;

	if ((it = timer_grab(p, tid)) == NULL)
		return (set_errno(EINVAL));

	error = it->it_backend->clk_timer_gettime(it, &when);

	timer_release(p, it);

	if (error == 0) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyout(&when, val, sizeof (itimerspec_t)))
				error = EFAULT;
		} else {
			if (ITIMERSPEC_OVERFLOW(&when))
				error = EOVERFLOW;
			else {
				itimerspec32_t w32;

				ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when)
				if (copyout(&w32, val, sizeof (itimerspec32_t)))
					error = EFAULT;
			}
		}
	}

	return (error ? set_errno(error) : 0);
}

int
timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval)
{
	itimerspec_t when;
	itimer_t *it;
	proc_t *p = curproc;
	int error;

	if (oval != NULL) {
		if ((error = timer_gettime(tid, oval)) != 0)
			return (error);
	}

	if (get_udatamodel() == DATAMODEL_NATIVE) {
		if (copyin(val, &when, sizeof (itimerspec_t)))
			return (set_errno(EFAULT));
	} else {
		itimerspec32_t w32;

		if (copyin(val, &w32, sizeof (itimerspec32_t)))
			return (set_errno(EFAULT));

		ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32);
	}

	if (itimerspecfix(&when.it_value) ||
	    (itimerspecfix(&when.it_interval) &&
	    timerspecisset(&when.it_value))) {
		return (set_errno(EINVAL));
	}

	if ((it = timer_grab(p, tid)) == NULL)
		return (set_errno(EINVAL));

	error = it->it_backend->clk_timer_settime(it, flags, &when);

	timer_release(p, it);

	return (error ? set_errno(error) : 0);
}

int
timer_delete(timer_t tid)
{
	proc_t *p = curproc;
	itimer_t *it;

	if ((it = timer_grab(p, tid)) == NULL)
		return (set_errno(EINVAL));

	timer_delete_grabbed(p, tid, it);

	return (0);
}

int
timer_getoverrun(timer_t tid)
{
	int overrun;
	proc_t *p = curproc;
	itimer_t *it;

	if ((it = timer_grab(p, tid)) == NULL)
		return (set_errno(EINVAL));

	/*
	 * The it_overrun field is protected by p_lock; we need to acquire
	 * it before looking at the value.
	 */
	mutex_enter(&p->p_lock);
	overrun = it->it_overrun;
	mutex_exit(&p->p_lock);

	timer_release(p, it);

	return (overrun);
}

/*
 * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock.
 */
void
timer_lwpexit(void)
{
	uint_t i;
	proc_t *p = curproc;
	klwp_t *lwp = ttolwp(curthread);
	itimer_t *it;

	ASSERT(MUTEX_HELD(&p->p_lock));

	if (p->p_itimer == NULL) {
		return;
	}

	for (i = 0; i < p->p_itimer_sz; i++) {
		if ((it = p->p_itimer[i]) == NULL) {
			continue;
		}

		/* This may drop p_lock temporarily. */
		timer_lock(p, it);

		if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) {
			/*
			 * This timer is either being removed or it isn't
			 * associated with this lwp.
			 */
			timer_unlock(p, it);
			continue;
		}

		/*
		 * The LWP that created this timer is going away.  To the user,
		 * our behavior here is explicitly undefined.  We will simply
		 * null out the it_lwp field; if the LWP was bound to a CPU,
		 * the cyclic will stay bound to that CPU until the process
		 * exits.
		 */
		it->it_lwp = NULL;
		timer_unlock(p, it);
	}
}

/*
 * Called to notify of an LWP binding change.  Entered/exited with p_lock
 * held, but will repeatedly drop and regrab p_lock.
 */
void
timer_lwpbind()
{
	uint_t i;
	proc_t *p = curproc;
	klwp_t *lwp = ttolwp(curthread);
	itimer_t *it;

	ASSERT(MUTEX_HELD(&p->p_lock));

	if (p->p_itimer == NULL) {
		return;
	}

	for (i = 0; i < p->p_itimer_sz; i++) {
		if ((it = p->p_itimer[i]) == NULL)
			continue;

		/* This may drop p_lock temporarily. */
		timer_lock(p, it);

		if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) {
			/*
			 * Drop p_lock and jump into the backend.
			 */
			mutex_exit(&p->p_lock);
			it->it_backend->clk_timer_lwpbind(it);
			mutex_enter(&p->p_lock);
		}

		timer_unlock(p, it);
	}
}

/*
 * This function should only be called if p_itimer is non-NULL.
 */
void
timer_exit(void)
{
	uint_t i;
	proc_t *p = curproc;

	ASSERT(p->p_itimer != NULL);
	ASSERT(p->p_itimer_sz != 0);

	for (i = 0; i < p->p_itimer_sz; i++) {
		(void) timer_delete((timer_t)i);
	}

	kmem_free(p->p_itimer, p->p_itimer_sz * sizeof (itimer_t *));
	p->p_itimer = NULL;
	p->p_itimer_sz = 0;
}

/*
 * timer_port_callback() is a callback function which is associated with the
 * timer event and is activated just before the event is delivered to the user.
 * The timer uses this function to update/set the overflow counter and
 * to reenable the use of the event structure.
 */

/* ARGSUSED */
static int
timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
{
	itimer_t	*it = arg;

	mutex_enter(&it->it_mutex);
	if (curproc != it->it_proc) {
		/* can not deliver timer events to another proc */
		mutex_exit(&it->it_mutex);
		return (EACCES);
	}
	*events = it->it_pending;	/* 1 = 1 event, >1 # of overflows */
	it->it_pending = 0;		/* reinit overflow counter	*/
	/*
	 * This function can also be activated when the port is being closed
	 * and a timer event is already submitted to the port.
	 * In such a case the event port framework will use the
	 * close-callback function to notify the events sources.
	 * The timer close-callback function is timer_close_port() which
	 * will free all allocated resources (including the allocated
	 * port event structure).
	 * For that reason we don't need to check the value of flag here.
	 */
	mutex_exit(&it->it_mutex);
	return (0);
}

/*
 * port is being closed ... free all allocated port event structures
 * The delivered arg currently correspond to the first timer associated with
 * the port and it is not useable in this case.
 * We have to scan the list of activated timers in the current proc and
 * compare them with the delivered port id.
 */

/* ARGSUSED */
static void
timer_close_port(void *arg, int port, pid_t pid, int lastclose)
{
	proc_t		*p = curproc;
	timer_t		tid;
	itimer_t	*it;

	for (tid = 0; tid < timer_max; tid++) {
		if ((it = timer_grab(p, tid)) == NULL)
			continue;
		if (it->it_flags & IT_PORT) {
			mutex_enter(&it->it_mutex);
			if (it->it_portfd == port) {
				port_kevent_t *pev;
				pev = (port_kevent_t *)it->it_portev;
				it->it_portev = NULL;
				it->it_flags &= ~IT_PORT;
				mutex_exit(&it->it_mutex);
				(void) port_remove_done_event(pev);
				port_free_event(pev);
			} else {
				mutex_exit(&it->it_mutex);
			}
		}
		timer_release(p, it);
	}
}