DragonFlyBSD Kernel Audit
sys/kern/kern_event.c
← back
   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
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
/*-
 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/kern/kern_event.c,v 1.2.2.10 2004/04/04 07:03:14 cperciva Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/unistd.h>
#include <sys/file.h>
#include <sys/lock.h>
#include <sys/fcntl.h>
#include <sys/queue.h>
#include <sys/event.h>
#include <sys/eventvar.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/sysmsg.h>
#include <sys/thread.h>
#include <sys/uio.h>
#include <sys/signalvar.h>
#include <sys/filio.h>
#include <sys/ktr.h>
#include <sys/spinlock.h>

#include <sys/thread2.h>
#include <sys/file2.h>
#include <sys/mplock2.h>
#include <sys/spinlock2.h>

#define EVENT_REGISTER	1
#define EVENT_PROCESS	2

static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");

struct kevent_copyin_args {
	const struct kevent_args *ka;
	struct kevent		*eventlist;
	const struct kevent	*changelist;
	int			pchanges;
};

#define KNOTE_CACHE_MAX		64

struct knote_cache_list {
	struct klist		knote_cache;
	int			knote_cache_cnt;
} __cachealign;

static int	kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
		    struct knote *marker, int closedcounter, int flags);
static int 	kqueue_read(struct file *fp, struct uio *uio,
		    struct ucred *cred, int flags);
static int	kqueue_write(struct file *fp, struct uio *uio,
		    struct ucred *cred, int flags);
static int	kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
		    struct ucred *cred, struct sysmsg *msg);
static int 	kqueue_kqfilter(struct file *fp, struct knote *kn);
static int 	kqueue_stat(struct file *fp, struct stat *st,
		    struct ucred *cred);
static int 	kqueue_close(struct file *fp);
static void	kqueue_wakeup(struct kqueue *kq);
static int	filter_attach(struct knote *kn);
static int	filter_event(struct knote *kn, long hint);

/*
 * MPSAFE
 */
static struct fileops kqueueops = {
	.fo_read = kqueue_read,
	.fo_write = kqueue_write,
	.fo_ioctl = kqueue_ioctl,
	.fo_kqfilter = kqueue_kqfilter,
	.fo_stat = kqueue_stat,
	.fo_close = kqueue_close,
	.fo_shutdown = nofo_shutdown,
	.fo_seek = badfo_seek
};

static void 	knote_attach(struct knote *kn);
static void 	knote_drop(struct knote *kn);
static void	knote_detach_and_drop(struct knote *kn);
static void 	knote_enqueue(struct knote *kn);
static void 	knote_dequeue(struct knote *kn);
static struct 	knote *knote_alloc(void);
static void 	knote_free(struct knote *kn);

static void	precise_sleep_intr(systimer_t info, int in_ipi,
				   struct intrframe *frame);
static int	precise_sleep(void *ident, int flags, const char *wmesg,
			      int us);

static void	filt_kqdetach(struct knote *kn);
static int	filt_kqueue(struct knote *kn, long hint);
static int	filt_procattach(struct knote *kn);
static void	filt_procdetach(struct knote *kn);
static int	filt_proc(struct knote *kn, long hint);
static int	filt_fileattach(struct knote *kn);
static void	filt_timerexpire(void *knx);
static int	filt_timerattach(struct knote *kn);
static void	filt_timerdetach(struct knote *kn);
static int	filt_timer(struct knote *kn, long hint);
static int	filt_userattach(struct knote *kn);
static void	filt_userdetach(struct knote *kn);
static int	filt_user(struct knote *kn, long hint);
static void	filt_usertouch(struct knote *kn, struct kevent *kev,
				u_long type);
static int	filt_fsattach(struct knote *kn);
static void	filt_fsdetach(struct knote *kn);
static int	filt_fs(struct knote *kn, long hint);

static struct filterops file_filtops =
	{ FILTEROP_ISFD | FILTEROP_MPSAFE, filt_fileattach, NULL, NULL };
static struct filterops kqread_filtops =
	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_kqdetach, filt_kqueue };
static struct filterops proc_filtops =
	{ FILTEROP_MPSAFE, filt_procattach, filt_procdetach, filt_proc };
static struct filterops timer_filtops =
	{ FILTEROP_MPSAFE, filt_timerattach, filt_timerdetach, filt_timer };
static struct filterops user_filtops =
	{ FILTEROP_MPSAFE, filt_userattach, filt_userdetach, filt_user };
static struct filterops fs_filtops =
	{ FILTEROP_MPSAFE, filt_fsattach, filt_fsdetach, filt_fs };

static int 		kq_ncallouts = 0;
static int 		kq_calloutmax = 65536;
SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
    &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
static int		kq_checkloop = 1000000;
SYSCTL_INT(_kern, OID_AUTO, kq_checkloop, CTLFLAG_RW,
    &kq_checkloop, 0, "Maximum number of loops for kqueue scan");
static int		kq_sleep_threshold = 20000;
SYSCTL_INT(_kern, OID_AUTO, kq_sleep_threshold, CTLFLAG_RW,
    &kq_sleep_threshold, 0, "Minimum sleep duration without busy-looping");

#define KNOTE_ACTIVATE(kn) do { 					\
	kn->kn_status |= KN_ACTIVE;					\
	if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)		\
		knote_enqueue(kn);					\
} while(0)

#define	KN_HASHSIZE		64		/* XXX should be tunable */
#define KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))

extern struct filterops aio_filtops;
extern struct filterops sig_filtops;

/*
 * Table for for all system-defined filters.
 */
static struct filterops *sysfilt_ops[] = {
	&file_filtops,			/* EVFILT_READ */
	&file_filtops,			/* EVFILT_WRITE */
	&aio_filtops,			/* EVFILT_AIO */
	&file_filtops,			/* EVFILT_VNODE */
	&proc_filtops,			/* EVFILT_PROC */
	&sig_filtops,			/* EVFILT_SIGNAL */
	&timer_filtops,			/* EVFILT_TIMER */
	&file_filtops,			/* EVFILT_EXCEPT */
	&user_filtops,			/* EVFILT_USER */
	&fs_filtops,			/* EVFILT_FS */
};

static struct knote_cache_list	knote_cache_lists[MAXCPU];

/*
 * Acquire a knote, return non-zero on success, 0 on failure.
 *
 * If we cannot acquire the knote we sleep and return 0.  The knote
 * may be stale on return in this case and the caller must restart
 * whatever loop they are in.
 *
 * Related kq token must be held.
 */
static __inline int
knote_acquire(struct knote *kn)
{
	if (kn->kn_status & KN_PROCESSING) {
		kn->kn_status |= KN_WAITING | KN_REPROCESS;
		tsleep(kn, 0, "kqepts", hz);
		/* knote may be stale now */
		return(0);
	}
	kn->kn_status |= KN_PROCESSING;
	return(1);
}

/*
 * Release an acquired knote, clearing KN_PROCESSING and handling any
 * KN_REPROCESS events.
 *
 * Caller must be holding the related kq token
 *
 * Non-zero is returned if the knote is destroyed or detached.
 */
static __inline int
knote_release(struct knote *kn)
{
	int ret;

	while (kn->kn_status & KN_REPROCESS) {
		kn->kn_status &= ~KN_REPROCESS;
		if (kn->kn_status & KN_WAITING) {
			kn->kn_status &= ~KN_WAITING;
			wakeup(kn);
		}
		if (kn->kn_status & KN_DELETING) {
			knote_detach_and_drop(kn);
			return(1);
			/* NOT REACHED */
		}
		if (filter_event(kn, 0))
			KNOTE_ACTIVATE(kn);
	}
	if (kn->kn_status & KN_DETACHED)
		ret = 1;
	else
		ret = 0;
	kn->kn_status &= ~KN_PROCESSING;
	/* kn should not be accessed anymore */
	return ret;
}

static int
filt_fileattach(struct knote *kn)
{
	return (fo_kqfilter(kn->kn_fp, kn));
}

/*
 * MPSAFE
 */
static int
kqueue_kqfilter(struct file *fp, struct knote *kn)
{
	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;

	if (kn->kn_filter != EVFILT_READ)
		return (EOPNOTSUPP);

	kn->kn_fop = &kqread_filtops;
	knote_insert(&kq->kq_kqinfo.ki_note, kn);
	return (0);
}

static void
filt_kqdetach(struct knote *kn)
{
	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;

	knote_remove(&kq->kq_kqinfo.ki_note, kn);
}

/*ARGSUSED*/
static int
filt_kqueue(struct knote *kn, long hint)
{
	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;

	kn->kn_data = kq->kq_count;
	return (kn->kn_data > 0);
}

static int
filt_procattach(struct knote *kn)
{
	struct proc *p;
	int immediate;

	immediate = 0;
	p = pfind(kn->kn_id);
	if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
		p = zpfind(kn->kn_id);
		immediate = 1;
	}
	if (p == NULL) {
		return (ESRCH);
	}
	if (!PRISON_CHECK(curthread->td_ucred, p->p_ucred)) {
		if (p)
			PRELE(p);
		return (EACCES);
	}

	lwkt_gettoken(&p->p_token);
	kn->kn_ptr.p_proc = p;
	kn->kn_flags |= EV_CLEAR;		/* automatically set */

	/*
	 * internal flag indicating registration done by kernel
	 */
	if (kn->kn_flags & EV_FLAG1) {
		kn->kn_data = kn->kn_sdata;		/* ppid */
		kn->kn_fflags = NOTE_CHILD;
		kn->kn_flags &= ~EV_FLAG1;
	}

	knote_insert(&p->p_klist, kn);

	/*
	 * Immediately activate any exit notes if the target process is a
	 * zombie.  This is necessary to handle the case where the target
	 * process, e.g. a child, dies before the kevent is negistered.
	 */
	if (immediate && filt_proc(kn, NOTE_EXIT))
		KNOTE_ACTIVATE(kn);
	lwkt_reltoken(&p->p_token);
	PRELE(p);

	return (0);
}

/*
 * The knote may be attached to a different process, which may exit,
 * leaving nothing for the knote to be attached to.  So when the process
 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
 * it will be deleted when read out.  However, as part of the knote deletion,
 * this routine is called, so a check is needed to avoid actually performing
 * a detach, because the original process does not exist any more.
 */
static void
filt_procdetach(struct knote *kn)
{
	struct proc *p;

	if (kn->kn_status & KN_DETACHED)
		return;
	p = kn->kn_ptr.p_proc;
	knote_remove(&p->p_klist, kn);
}

static int
filt_proc(struct knote *kn, long hint)
{
	u_int event;

	/*
	 * mask off extra data
	 */
	event = (u_int)hint & NOTE_PCTRLMASK;

	/*
	 * if the user is interested in this event, record it.
	 */
	if (kn->kn_sfflags & event)
		kn->kn_fflags |= event;

	/*
	 * Process is gone, so flag the event as finished.  Detach the
	 * knote from the process now because the process will be poof,
	 * gone later on.
	 */
	if (event == NOTE_EXIT) {
		struct proc *p = kn->kn_ptr.p_proc;
		if ((kn->kn_status & KN_DETACHED) == 0) {
			PHOLD(p);
			knote_remove(&p->p_klist, kn);
			kn->kn_status |= KN_DETACHED;
			kn->kn_data = p->p_xstat;
			kn->kn_ptr.p_proc = NULL;
			PRELE(p);
		}
		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
		return (1);
	}

	/*
	 * process forked, and user wants to track the new process,
	 * so attach a new knote to it, and immediately report an
	 * event with the parent's pid.
	 */
	if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
		struct kevent kev;
		int error;
		int n;

		/*
		 * register knote with new process.
		 */
		kev.ident = hint & NOTE_PDATAMASK;	/* pid */
		kev.filter = kn->kn_filter;
		kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
		kev.fflags = kn->kn_sfflags;
		kev.data = kn->kn_id;			/* parent */
		kev.udata = kn->kn_kevent.udata;	/* preserve udata */
		n = 1;
		error = kqueue_register(kn->kn_kq, &kev, &n, 0);
		if (error)
			kn->kn_fflags |= NOTE_TRACKERR;
	}

	return (kn->kn_fflags != 0);
}

static void
filt_timerreset(struct knote *kn)
{
	struct callout *calloutp;
	struct timeval tv;
	int tticks;

	tv.tv_sec = kn->kn_sdata / 1000;
	tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
	tticks = tvtohz_high(&tv);
	calloutp = (struct callout *)kn->kn_hook;
	callout_reset(calloutp, tticks, filt_timerexpire, kn);
}

/*
 * The callout interlocks with callout_stop() but can still
 * race a deletion so if KN_DELETING is set we just don't touch
 * the knote.
 */
static void
filt_timerexpire(void *knx)
{
	struct knote *kn = knx;
	struct kqueue *kq = kn->kn_kq;

	lwkt_getpooltoken(kq);

	/*
	 * Open knote_acquire(), since we can't sleep in callout,
	 * however, we do need to record this expiration.
	 */
	kn->kn_data++;
	if (kn->kn_status & KN_PROCESSING) {
		kn->kn_status |= KN_REPROCESS;
		if ((kn->kn_status & KN_DELETING) == 0 &&
		    (kn->kn_flags & EV_ONESHOT) == 0)
			filt_timerreset(kn);
		lwkt_relpooltoken(kq);
		return;
	}
	KASSERT((kn->kn_status & KN_DELETING) == 0,
	    ("acquire a deleting knote %#x", kn->kn_status));
	kn->kn_status |= KN_PROCESSING;

	KNOTE_ACTIVATE(kn);
	if ((kn->kn_flags & EV_ONESHOT) == 0)
		filt_timerreset(kn);

	knote_release(kn);

	lwkt_relpooltoken(kq);
}

/*
 * data contains amount of time to sleep, in milliseconds
 */
static int
filt_timerattach(struct knote *kn)
{
	struct callout *calloutp;
	int prev_ncallouts;

	prev_ncallouts = atomic_fetchadd_int(&kq_ncallouts, 1);
	if (prev_ncallouts >= kq_calloutmax) {
		atomic_subtract_int(&kq_ncallouts, 1);
		kn->kn_hook = NULL;
		return (ENOMEM);
	}

	kn->kn_flags |= EV_CLEAR;		/* automatically set */
	calloutp = kmalloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
	callout_init_mp(calloutp);
	kn->kn_hook = (caddr_t)calloutp;

	filt_timerreset(kn);
	return (0);
}

/*
 * This function is called with the knote flagged locked but it is
 * still possible to race a callout event due to the callback blocking.
 */
static void
filt_timerdetach(struct knote *kn)
{
	struct callout *calloutp;

	calloutp = (struct callout *)kn->kn_hook;
	callout_terminate(calloutp);
	kn->kn_hook = NULL;
	kfree(calloutp, M_KQUEUE);
	atomic_subtract_int(&kq_ncallouts, 1);
}

static int
filt_timer(struct knote *kn, long hint)
{
	return (kn->kn_data != 0);
}

/*
 * EVFILT_USER
 */
static int
filt_userattach(struct knote *kn)
{
	u_int ffctrl;

	kn->kn_hook = NULL;
	if (kn->kn_sfflags & NOTE_TRIGGER)
		kn->kn_ptr.hookid = 1;
	else
		kn->kn_ptr.hookid = 0;

	ffctrl = kn->kn_sfflags & NOTE_FFCTRLMASK;
	kn->kn_sfflags &= NOTE_FFLAGSMASK;
	switch (ffctrl) {
	case NOTE_FFNOP:
		break;

	case NOTE_FFAND:
		kn->kn_fflags &= kn->kn_sfflags;
		break;

	case NOTE_FFOR:
		kn->kn_fflags |= kn->kn_sfflags;
		break;

	case NOTE_FFCOPY:
		kn->kn_fflags = kn->kn_sfflags;
		break;

	default:
		/* XXX Return error? */
		break;
	}
	/* We just happen to copy this value as well. Undocumented. */
	kn->kn_data = kn->kn_sdata;

	return 0;
}

static void
filt_userdetach(struct knote *kn)
{
	/* nothing to do */
}

static int
filt_user(struct knote *kn, long hint)
{
	return (kn->kn_ptr.hookid);
}

static void
filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
{
	u_int ffctrl;

	switch (type) {
	case EVENT_REGISTER:
		if (kev->fflags & NOTE_TRIGGER)
			kn->kn_ptr.hookid = 1;

		ffctrl = kev->fflags & NOTE_FFCTRLMASK;
		kev->fflags &= NOTE_FFLAGSMASK;
		switch (ffctrl) {
		case NOTE_FFNOP:
			break;

		case NOTE_FFAND:
			kn->kn_fflags &= kev->fflags;
			break;

		case NOTE_FFOR:
			kn->kn_fflags |= kev->fflags;
			break;

		case NOTE_FFCOPY:
			kn->kn_fflags = kev->fflags;
			break;

		default:
			/* XXX Return error? */
			break;
		}
		/* We just happen to copy this value as well. Undocumented. */
		kn->kn_data = kev->data;

		/*
		 * This is not the correct use of EV_CLEAR in an event
		 * modification, it should have been passed as a NOTE instead.
		 * But we need to maintain compatibility with Apple & FreeBSD.
		 *
		 * Note however that EV_CLEAR can still be used when doing
		 * the initial registration of the event and works as expected
		 * (clears the event on reception).
		 */
		if (kev->flags & EV_CLEAR) {
			kn->kn_ptr.hookid = 0;
			/*
			 * Clearing kn->kn_data is fine, since it gets set
			 * every time anyway. We just shouldn't clear
			 * kn->kn_fflags here, since that would limit the
			 * possible uses of this API. NOTE_FFAND or
			 * NOTE_FFCOPY should be used for explicitly clearing
			 * kn->kn_fflags.
			 */
			kn->kn_data = 0;
		}
		break;

        case EVENT_PROCESS:
		*kev = kn->kn_kevent;
		kev->fflags = kn->kn_fflags;
		kev->data = kn->kn_data;
		if (kn->kn_flags & EV_CLEAR) {
			kn->kn_ptr.hookid = 0;
			/* kn_data, kn_fflags handled by parent */
		}
		break;

	default:
		panic("filt_usertouch() - invalid type (%ld)", type);
		break;
	}
}

/*
 * EVFILT_FS
 */
struct klist fs_klist = SLIST_HEAD_INITIALIZER(&fs_klist);

static int
filt_fsattach(struct knote *kn)
{
	kn->kn_flags |= EV_CLEAR;
	knote_insert(&fs_klist, kn);

	return (0);
}

static void
filt_fsdetach(struct knote *kn)
{
	knote_remove(&fs_klist, kn);
}

static int
filt_fs(struct knote *kn, long hint)
{
	kn->kn_fflags |= hint;
	return (kn->kn_fflags != 0);
}

/*
 * Initialize a kqueue.
 *
 * NOTE: The lwp/proc code initializes a kqueue for select/poll ops.
 */
void
kqueue_init(struct kqueue *kq, struct filedesc *fdp)
{
	bzero(kq, sizeof(*kq));
	TAILQ_INIT(&kq->kq_knpend);
	TAILQ_INIT(&kq->kq_knlist);
	kq->kq_fdp = fdp;
	SLIST_INIT(&kq->kq_kqinfo.ki_note);
}

/*
 * Terminate a kqueue.  Freeing the actual kq itself is left up to the
 * caller (it might be embedded in a lwp so we don't do it here).
 *
 * The kq's knlist must be completely eradicated so block on any
 * processing races.
 */
void
kqueue_terminate(struct kqueue *kq)
{
	struct knote *kn;

	lwkt_getpooltoken(kq);
	while ((kn = TAILQ_FIRST(&kq->kq_knlist)) != NULL) {
		if (knote_acquire(kn))
			knote_detach_and_drop(kn);
	}
	lwkt_relpooltoken(kq);

	if (kq->kq_knhash) {
		hashdestroy(kq->kq_knhash, M_KQUEUE, kq->kq_knhashmask);
		kq->kq_knhash = NULL;
		kq->kq_knhashmask = 0;
	}
}

/*
 * MPSAFE
 */
int
sys_kqueue(struct sysmsg *sysmsg, const struct kqueue_args *uap)
{
	struct thread *td = curthread;
	struct kqueue *kq;
	struct file *fp;
	int fd, error;

	error = falloc(td->td_lwp, &fp, &fd);
	if (error)
		return (error);
	fp->f_flag = FREAD | FWRITE;
	fp->f_type = DTYPE_KQUEUE;
	fp->f_ops = &kqueueops;

	kq = kmalloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
	kqueue_init(kq, td->td_proc->p_fd);
	fp->f_data = kq;

	fsetfd(kq->kq_fdp, fp, fd);
	sysmsg->sysmsg_result = fd;
	fdrop(fp);
	return (0);
}

/*
 * Copy 'count' items into the destination list pointed to by uap->eventlist.
 */
static int
kevent_copyout(void *arg, struct kevent *kevp, int count, int *res)
{
	struct kevent_copyin_args *kap;
	int error;

	kap = (struct kevent_copyin_args *)arg;

	error = copyout(kevp, kap->eventlist, count * sizeof(*kevp));
	if (error == 0) {
		kap->eventlist += count;
		*res += count;
	} else {
		*res = -1;
	}

	return (error);
}

/*
 * Copy at most 'max' items from the list pointed to by kap->changelist,
 * return number of items in 'events'.
 */
static int
kevent_copyin(void *arg, struct kevent *kevp, int max, int *events)
{
	struct kevent_copyin_args *kap;
	int error, count;

	kap = (struct kevent_copyin_args *)arg;

	count = min(kap->ka->nchanges - kap->pchanges, max);
	error = copyin(kap->changelist, kevp, count * sizeof *kevp);
	if (error == 0) {
		kap->changelist += count;
		kap->pchanges += count;
		*events = count;
	}

	return (error);
}

/*
 * MPSAFE
 */
int
kern_kevent(struct kqueue *kq, int nevents, int *res, void *uap,
	    k_copyin_fn kevent_copyinfn, k_copyout_fn kevent_copyoutfn,
	    struct timespec *tsp_in, int flags)
{
	struct kevent *kevp;
	struct timespec *tsp, ats;
	int i, n, total, error, nerrors = 0;
	int gobbled;
	int lres;
	int limit = kq_checkloop;
	int closedcounter;
	struct kevent kev[KQ_NEVENTS];
	struct knote marker;
	struct lwkt_token *tok;

	if (tsp_in == NULL || tsp_in->tv_sec || tsp_in->tv_nsec)
		atomic_set_int(&curthread->td_mpflags, TDF_MP_BATCH_DEMARC);

	tsp = tsp_in;
	*res = 0;

	closedcounter = kq->kq_fdp->fd_closedcounter;

	for (;;) {
		n = 0;
		error = kevent_copyinfn(uap, kev, KQ_NEVENTS, &n);
		if (error)
			return error;
		if (n == 0)
			break;
		for (i = 0; i < n; ++i)
			kev[i].flags &= ~EV_SYSFLAGS;
		for (i = 0; i < n; ++i) {
			gobbled = n - i;

			error = kqueue_register(kq, &kev[i], &gobbled, flags);
			i += gobbled - 1;
			kevp = &kev[i];

			/*
			 * If a registration returns an error we
			 * immediately post the error.  The kevent()
			 * call itself will fail with the error if
			 * no space is available for posting.
			 *
			 * Such errors normally bypass the timeout/blocking
			 * code.  However, if the copyoutfn function refuses
			 * to post the error (see sys_poll()), then we
			 * ignore it too.
			 */
			if (error || (kevp->flags & EV_RECEIPT)) {
				kevp->flags = EV_ERROR;
				kevp->data = error;
				lres = *res;
				kevent_copyoutfn(uap, kevp, 1, res);
				if (*res < 0) {
					return error;
				} else if (lres != *res) {
					nevents--;
					nerrors++;
				}
			}
		}
	}
	if (nerrors)
		return 0;

	/*
	 * Acquire/wait for events - setup timeout
	 *
	 * If no timeout specified clean up the run path by clearing the
	 * PRECISE flag.
	 */
	if (tsp != NULL) {
		if (tsp->tv_sec || tsp->tv_nsec) {
			getnanouptime(&ats);
			timespecadd(tsp, &ats, tsp);	/* tsp = target time */
		}
	} else {
		flags &= ~KEVENT_TIMEOUT_PRECISE;
	}

	/*
	 * Loop as required.
	 *
	 * Collect as many events as we can. Sleeping on successive
	 * loops is disabled if copyoutfn has incremented (*res).
	 *
	 * The loop stops if an error occurs, all events have been
	 * scanned (the marker has been reached), or fewer than the
	 * maximum number of events is found.
	 *
	 * The copyoutfn function does not have to increment (*res) in
	 * order for the loop to continue.
	 *
	 * NOTE: doselect() usually passes 0x7FFFFFFF for nevents.
	 */
	total = 0;
	error = 0;
	marker.kn_filter = EVFILT_MARKER;
	marker.kn_status = KN_PROCESSING;

	tok = lwkt_token_pool_lookup(kq);
	flags = (flags & ~KEVENT_SCAN_MASK) | KEVENT_SCAN_INSERT_MARKER;

	while ((n = nevents - total) > 0) {
		if (n > KQ_NEVENTS)
			n = KQ_NEVENTS;

		/*
		 * Process all received events
		 * Account for all non-spurious events in our total
		 */
		i = kqueue_scan(kq, kev, n, &marker, closedcounter, flags);
		flags = (flags & ~KEVENT_SCAN_MASK) | KEVENT_SCAN_KEEP_MARKER;
		if (i) {
			lres = *res;
			error = kevent_copyoutfn(uap, kev, i, res);
			total += *res - lres;
			if (error)
				break;
		}
		if (limit && --limit == 0)
			panic("kqueue: checkloop failed i=%d", i);

		/*
		 * Normally when fewer events are returned than requested
		 * we can stop.  However, if only spurious events were
		 * collected the copyout will not bump (*res) and we have
		 * to continue.
		 */
		if (i < n && *res)
			break;

		/*
		 * If no events were recorded (no events happened or the events
		 * that did happen were all spurious), block until an event
		 * occurs or the timeout occurs and reload the marker.
		 *
		 * If we saturated n (i == n) loop up without sleeping to
		 * continue processing the list.
		 */
		if (i != n && kq->kq_count == 0 && *res == 0) {
			int timeout;
			int ustimeout;

			if (tsp == NULL) {
				timeout = 0;
				ustimeout = 0;
			} else if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) {
				error = EWOULDBLOCK;
				break;
			} else {
				struct timespec atx = *tsp;

				getnanouptime(&ats);
				timespecsub(&atx, &ats, &atx);
				if (atx.tv_sec < 0 ||
				    (atx.tv_sec == 0 && atx.tv_nsec <= 0)) {
					error = EWOULDBLOCK;
					break;
				}
				if (flags & KEVENT_TIMEOUT_PRECISE) {
					if (atx.tv_sec == 0 &&
					    atx.tv_nsec < kq_sleep_threshold) {
						ustimeout = kq_sleep_threshold /
							    1000;
					} else if (atx.tv_sec < 60) {
						ustimeout =
							atx.tv_sec * 1000000 +
							atx.tv_nsec / 1000;
					} else {
						ustimeout = 60 * 1000000;
					}
					if (ustimeout == 0)
						ustimeout = 1;
					timeout = 0;
				} else if (atx.tv_sec > 60 * 60) {
					timeout = 60 * 60 * hz;
					ustimeout = 0;
				} else {
					timeout = tstohz_high(&atx);
					ustimeout = 0;
				}
			}

			lwkt_gettoken(tok);
			if (kq->kq_count == 0) {
				kq->kq_sleep_cnt++;
				if (__predict_false(kq->kq_sleep_cnt == 0)) {
					/*
					 * Guard against possible wrapping.  And
					 * set it to 2, so that kqueue_wakeup()
					 * can wake everyone up.
					 */
					kq->kq_sleep_cnt = 2;
				}
				if (flags & KEVENT_TIMEOUT_PRECISE) {
					error = precise_sleep(kq, PCATCH,
							"kqread", ustimeout);
				} else {
					error = tsleep(kq, PCATCH,
							"kqread", timeout);
				}

				/* don't restart after signals... */
				if (error == ERESTART)
					error = EINTR;
				if (error == EWOULDBLOCK)
					error = 0;
				if (error) {
					lwkt_reltoken(tok);
					break;
				}
				flags = (flags & ~KEVENT_SCAN_MASK) |
					KEVENT_SCAN_RELOAD_MARKER;
			}
			lwkt_reltoken(tok);
		}

		/*
		 * Deal with an edge case where spurious events can cause
		 * a loop to occur without moving the marker.  This can
		 * prevent kqueue_scan() from picking up new events which
		 * race us.  We must be sure to move the marker for this
		 * case.
		 *
		 * NOTE: We do not want to move the marker if events
		 *	 were scanned because normal kqueue operations
		 *	 may reactivate events.  Moving the marker in
		 *	 that case could result in duplicates for the
		 *	 same event.
		 */
		if (i == 0) {
			flags = (flags & ~KEVENT_SCAN_MASK) |
				KEVENT_SCAN_RELOAD_MARKER;
		}
	}

	/*
	 * Remove the marker
	 */
	if ((flags & KEVENT_SCAN_INSERT_MARKER) == 0) {
		lwkt_gettoken(tok);
		TAILQ_REMOVE(&kq->kq_knpend, &marker, kn_tqe);
		lwkt_reltoken(tok);
	}

	/* Timeouts do not return EWOULDBLOCK. */
	if (error == EWOULDBLOCK)
		error = 0;
	return error;
}

/*
 * MPALMOSTSAFE
 */
int
sys_kevent(struct sysmsg *sysmsg, const struct kevent_args *uap)
{
	struct thread *td = curthread;
	struct timespec ts, *tsp;
	struct kqueue *kq;
	struct file *fp = NULL;
	struct kevent_copyin_args *kap, ka;
	int error;

	if (uap->timeout) {
		error = copyin(uap->timeout, &ts, sizeof(ts));
		if (error)
			return (error);
		tsp = &ts;
	} else {
		tsp = NULL;
	}
	fp = holdfp(td, uap->fd, -1);
	if (fp == NULL)
		return (EBADF);
	if (fp->f_type != DTYPE_KQUEUE) {
		fdrop(fp);
		return (EBADF);
	}

	kq = (struct kqueue *)fp->f_data;

	kap = &ka;
	kap->ka = uap;
	kap->pchanges = 0;
	kap->eventlist = uap->eventlist;
	kap->changelist = uap->changelist;

	error = kern_kevent(kq, uap->nevents, &sysmsg->sysmsg_result, kap,
			    kevent_copyin, kevent_copyout, tsp, 0);

	dropfp(td, uap->fd, fp);

	return (error);
}

/*
 * Efficiently load multiple file pointers.  This significantly reduces
 * threaded overhead.  When doing simple polling we can depend on the
 * per-thread (fd,fp) cache.  With more descriptors, we batch.
 */
static
void
floadkevfps(thread_t td, struct filedesc *fdp, struct kevent *kev,
	    struct file **fp, int climit)
{
	struct filterops *fops;
	int tdcache;

	if (climit <= 2 && td->td_proc && td->td_proc->p_fd == fdp) {
		tdcache = 1;
	} else {
		tdcache = 0;
		spin_lock_shared(&fdp->fd_spin);
	}

	while (climit) {
		*fp = NULL;
		if (kev->filter < 0 &&
		    kev->filter + EVFILT_SYSCOUNT >= 0) {
			fops = sysfilt_ops[~kev->filter];
			if (fops->f_flags & FILTEROP_ISFD) {
				if (tdcache) {
					*fp = holdfp(td, kev->ident, -1);
				} else {
					*fp = holdfp_fdp_locked(fdp,
								kev->ident, -1);
				}
			}
		}
		--climit;
		++fp;
		++kev;
	}
	if (tdcache == 0)
		spin_unlock_shared(&fdp->fd_spin);
}

/*
 * Register up to *countp kev's.  Always registers at least 1.
 *
 * The number registered is returned in *countp.
 *
 * If an error occurs or a kev is flagged EV_RECEIPT, it is
 * processed and included in *countp, and processing then
 * stops.
 *
 * If flags contains KEVENT_UNIQUE_NOTES, kev->data contains an identifier
 * to further distinguish knotes which might otherwise have the same kq,
 * ident, and filter (used by *poll() because multiple pfds are allowed to
 * reference the same descriptor and implied kq filter).  kev->data is
 * implied to be zero for event processing when this flag is set.
 */
int
kqueue_register(struct kqueue *kq, struct kevent *kev, int *countp, int flags)
{
	struct filedesc *fdp = kq->kq_fdp;
	struct klist *list = NULL;
	struct filterops *fops;
	struct file *fp[KQ_NEVENTS];
	struct knote *kn = NULL;
	struct thread *td;
	int error;
	int count;
	int climit;
	int closedcounter;
	int uniqifier = 0;
	struct knote_cache_list *cache_list;

	td = curthread;
	climit = *countp;
	if (climit > KQ_NEVENTS)
		climit = KQ_NEVENTS;
	closedcounter = fdp->fd_closedcounter;
	floadkevfps(td, fdp, kev, fp, climit);

	lwkt_getpooltoken(kq);
	count = 0;
	error = 0;

	/*
	 * To avoid races, only one thread can register events on this
	 * kqueue at a time.
	 */
	while (__predict_false(kq->kq_regtd != NULL && kq->kq_regtd != td)) {
		kq->kq_state |= KQ_REGWAIT;
		tsleep(&kq->kq_regtd, 0, "kqreg", 0);
	}
	if (__predict_false(kq->kq_regtd != NULL)) {
		/* Recursive calling of kqueue_register() */
		td = NULL;
	} else {
		/* Owner of the kq_regtd, i.e. td != NULL */
		kq->kq_regtd = td;
	}

loop:
	/*
	 * knote uniqifiers are used by *poll() because there may be
	 * multiple pfd[] entries for the same descriptor and filter.
	 * The unique id is stored in kev->data and kev->data for the
	 * kevent is implied to be zero.
	 */
	if (flags & KEVENT_UNIQUE_NOTES) {
		uniqifier = kev->data;
		kev->data = 0;
	}

	if (kev->filter < 0) {
		if (kev->filter + EVFILT_SYSCOUNT < 0) {
			error = EINVAL;
			++count;
			goto done;
		}
		fops = sysfilt_ops[~kev->filter];	/* to 0-base index */
	} else {
		/*
		 * XXX
		 * filter attach routine is responsible for insuring that
		 * the identifier can be attached to it.
		 */
		error = EINVAL;
		++count;
		goto done;
	}

	if (fops->f_flags & FILTEROP_ISFD) {
		/* validate descriptor */
		if (fp[count] == NULL) {
			error = EBADF;
			++count;
			goto done;
		}
	}

	cache_list = &knote_cache_lists[mycpuid];
	if (SLIST_EMPTY(&cache_list->knote_cache)) {
		struct knote *new_kn;

		new_kn = knote_alloc();
		crit_enter();
		SLIST_INSERT_HEAD(&cache_list->knote_cache, new_kn, kn_link);
		cache_list->knote_cache_cnt++;
		crit_exit();
	}

	if (fp[count] != NULL) {
		list = &fp[count]->f_klist;
	} else if (kq->kq_knhashmask) {
		list = &kq->kq_knhash[
			    KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
	}
	if (list != NULL) {
		lwkt_getpooltoken(list);
again:
		SLIST_FOREACH(kn, list, kn_link) {
			if (kn->kn_kq == kq &&
			    kn->kn_filter == kev->filter &&
			    kn->kn_id == kev->ident &&
			    kn->kn_uniqifier == uniqifier)
			{
				if (knote_acquire(kn) == 0)
					goto again;
				break;
			}
		}
		lwkt_relpooltoken(list);
	}

	/*
	 * NOTE: At this point if kn is non-NULL we will have acquired
	 *	 it and set KN_PROCESSING.
	 */
	if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
		error = ENOENT;
		++count;
		goto done;
	}

	/*
	 * kn now contains the matching knote, or NULL if no match
	 */
	if (kev->flags & EV_ADD) {
		if (kn == NULL) {
			crit_enter();
			kn = SLIST_FIRST(&cache_list->knote_cache);
			if (kn == NULL) {
				crit_exit();
				kn = knote_alloc();
			} else {
				SLIST_REMOVE_HEAD(&cache_list->knote_cache,
				    kn_link);
				cache_list->knote_cache_cnt--;
				crit_exit();
			}
			kn->kn_fp = fp[count];
			kn->kn_kq = kq;
			kn->kn_fop = fops;
			kn->kn_uniqifier = uniqifier;

			/*
			 * apply reference count to knote structure, and
			 * do not release it at the end of this routine.
			 */
			fp[count] = NULL;	/* safety */

			kn->kn_sfflags = kev->fflags;
			kn->kn_sdata = kev->data;
			kev->fflags = 0;
			kev->data = 0;
			kn->kn_kevent = *kev;

			/*
			 * KN_PROCESSING prevents the knote from getting
			 * ripped out from under us while we are trying
			 * to attach it, in case the attach blocks.
			 */
			kn->kn_status = KN_PROCESSING;
			knote_attach(kn);
			if ((error = filter_attach(kn)) != 0) {
				kn->kn_status |= KN_DELETING | KN_REPROCESS;
				knote_drop(kn);
				++count;
				goto done;
			}

			/*
			 * Interlock against close races which either tried
			 * to remove our knote while we were blocked or missed
			 * it entirely prior to our attachment.  We do not
			 * want to end up with a knote on a closed descriptor.
			 */
			if ((fops->f_flags & FILTEROP_ISFD) &&
			    checkfdclosed(curthread, fdp, kev->ident, kn->kn_fp,
					  closedcounter)) {
				kn->kn_status |= KN_DELETING | KN_REPROCESS;
			}
		} else {
			/*
			 * The user may change some filter values after the
			 * initial EV_ADD, but doing so will not reset any
			 * filter which have already been triggered.
			 */
			KKASSERT(kn->kn_status & KN_PROCESSING);
			if (fops == &user_filtops) {
				filt_usertouch(kn, kev, EVENT_REGISTER);
			} else {
				kn->kn_sfflags = kev->fflags;
				kn->kn_sdata = kev->data;
				kn->kn_kevent.udata = kev->udata;
			}
		}

		/*
		 * Execute the filter event to immediately activate the
		 * knote if necessary.  If reprocessing events are pending
		 * due to blocking above we do not run the filter here
		 * but instead let knote_release() do it.  Otherwise we
		 * might run the filter on a deleted event.
		 */
		if ((kn->kn_status & KN_REPROCESS) == 0) {
			if (filter_event(kn, 0))
				KNOTE_ACTIVATE(kn);
		}
	} else if (kev->flags & EV_DELETE) {
		/*
		 * Delete the existing knote
		 */
		knote_detach_and_drop(kn);
		error = 0;
		++count;
		goto done;
	} else {
		/*
		 * Modify an existing event.
		 *
		 * The user may change some filter values after the
		 * initial EV_ADD, but doing so will not reset any
		 * filter which have already been triggered.
		 */
		KKASSERT(kn->kn_status & KN_PROCESSING);
		if (fops == &user_filtops) {
			filt_usertouch(kn, kev, EVENT_REGISTER);
		} else {
			kn->kn_sfflags = kev->fflags;
			kn->kn_sdata = kev->data;
			kn->kn_kevent.udata = kev->udata;
		}

		/*
		 * Execute the filter event to immediately activate the
		 * knote if necessary.  If reprocessing events are pending
		 * due to blocking above we do not run the filter here
		 * but instead let knote_release() do it.  Otherwise we
		 * might run the filter on a deleted event.
		 */
		if ((kn->kn_status & KN_REPROCESS) == 0) {
			if (filter_event(kn, 0))
				KNOTE_ACTIVATE(kn);
		}
	}

	/*
	 * Disablement does not deactivate a knote here.
	 */
	if ((kev->flags & EV_DISABLE) &&
	    ((kn->kn_status & KN_DISABLED) == 0))
	{
		kn->kn_status |= KN_DISABLED;
	}

	/*
	 * Re-enablement may have to immediately enqueue an active knote.
	 */
	if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
		kn->kn_status &= ~KN_DISABLED;
		if ((kn->kn_status & KN_ACTIVE) &&
		    ((kn->kn_status & KN_QUEUED) == 0))
		{
			knote_enqueue(kn);
		}
	}

	/*
	 * Handle any required reprocessing
	 */
	knote_release(kn);
	/* kn may be invalid now */

	/*
	 * Loop control.  We stop on errors (above), and also stop after
	 * processing EV_RECEIPT, so the caller can process it.
	 */
	++count;
	if (kev->flags & EV_RECEIPT) {
		error = 0;
		goto done;
	}
	++kev;
	if (count < climit) {
		if (fp[count-1])		/* drop unprocessed fp */
			fdrop(fp[count-1]);
		goto loop;
	}

	/*
	 * Cleanup
	 */
done:
	if (td != NULL) { /* Owner of the kq_regtd */
		kq->kq_regtd = NULL;
		if (__predict_false(kq->kq_state & KQ_REGWAIT)) {
			kq->kq_state &= ~KQ_REGWAIT;
			wakeup(&kq->kq_regtd);
		}
	}
	lwkt_relpooltoken(kq);

	/*
	 * Drop unprocessed file pointers
	 */
	*countp = count;
	if (count && fp[count-1])
		fdrop(fp[count-1]);
	while (count < climit) {
		if (fp[count])
			fdrop(fp[count]);
		++count;
	}
	return (error);
}

/*
 * Scan the kqueue, return the number of active events placed in kevp up
 * to count.
 *
 * Continuous mode events may get recycled, do not continue scanning past
 * marker unless no events have been collected.
 */
static int
kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
            struct knote *marker, int closedcounter, int flags)
{
	struct knote *kn, local_marker;
	thread_t td = curthread;
	int total;

	total = 0;
	local_marker.kn_filter = EVFILT_MARKER;
	local_marker.kn_status = KN_PROCESSING;

	lwkt_getpooltoken(kq);

	/*
	 * Adjust marker, insert initial marker, or leave the marker alone.
	 *
	 * Also setup our local_marker.
	 */
	switch(flags & KEVENT_SCAN_MASK) {
	case KEVENT_SCAN_RELOAD_MARKER:
		TAILQ_REMOVE(&kq->kq_knpend, marker, kn_tqe);
		/* fall through */
	case KEVENT_SCAN_INSERT_MARKER:
		TAILQ_INSERT_TAIL(&kq->kq_knpend, marker, kn_tqe);
		break;
	}
	TAILQ_INSERT_HEAD(&kq->kq_knpend, &local_marker, kn_tqe);

	/*
	 * Collect events.
	 */
	while (count) {
		kn = TAILQ_NEXT(&local_marker, kn_tqe);
		if (kn->kn_filter == EVFILT_MARKER) {
			/* Marker reached, we are done */
			if (kn == marker)
				break;

			/* Move local marker past some other threads marker */
			kn = TAILQ_NEXT(kn, kn_tqe);
			TAILQ_REMOVE(&kq->kq_knpend, &local_marker, kn_tqe);
			TAILQ_INSERT_BEFORE(kn, &local_marker, kn_tqe);
			continue;
		}

		/*
		 * We can't skip a knote undergoing processing, otherwise
		 * we risk not returning it when the user process expects
		 * it should be returned.  Sleep and retry.
		 */
		if (knote_acquire(kn) == 0)
			continue;

		/*
		 * Remove the event for processing.
		 *
		 * WARNING!  We must leave KN_QUEUED set to prevent the
		 *	     event from being KNOTE_ACTIVATE()d while
		 *	     the queue state is in limbo, in case we
		 *	     block.
		 */
		TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
		kq->kq_count--;

		/*
		 * Kernel select() and poll() functions cache previous
		 * operations on the assumption that future operations
		 * will use similr descriptor sets.  This removes any
		 * stale entries in a way that does not require a descriptor
		 * lookup and is thus not affected by close() races.
		 *
		 * Do not report to *_copyout()
		 */
		if (flags & KEVENT_AUTO_STALE) {
			if ((uint64_t)kn->kn_kevent.udata <
			    curthread->td_lwp->lwp_kqueue_serial)
			{
				kn->kn_status |= KN_DELETING | KN_REPROCESS |
						 KN_DISABLED;
			}
		}

		/*
		 * If a descriptor is close()d out from under a poll/select,
		 * we want to report the event but delete the note because
		 * the note can wind up being 'stuck' on kq_knpend.
		 */
		if ((kn->kn_fop->f_flags & FILTEROP_ISFD) &&
		    checkfdclosed(td, kq->kq_fdp, kn->kn_kevent.ident,
				  kn->kn_fp, closedcounter))
		{
			kn->kn_status |= KN_DELETING | KN_REPROCESS;
		}

		if (kn->kn_status & KN_DISABLED) {
			/*
			 * If disabled we ensure the event is not queued
			 * but leave its active bit set.  On re-enablement
			 * the event may be immediately triggered.
			 */
			kn->kn_status &= ~KN_QUEUED;
		} else if ((kn->kn_flags & EV_ONESHOT) == 0 &&
			   (kn->kn_status & KN_DELETING) == 0 &&
			   filter_event(kn, 0) == 0) {
			/*
			 * If not running in one-shot mode and the event
			 * is no longer present we ensure it is removed
			 * from the queue and ignore it.
			 */
			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
		} else {
			/*
			 * Post the event
			 */
			if (kn->kn_fop == &user_filtops)
				filt_usertouch(kn, kevp, EVENT_PROCESS);
			else
				*kevp = kn->kn_kevent;
			++kevp;
			++total;
			--count;

			if (kn->kn_flags & EV_ONESHOT) {
				kn->kn_status &= ~KN_QUEUED;
				kn->kn_status |= KN_DELETING | KN_REPROCESS;
			} else {
				if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
					if (kn->kn_flags & EV_CLEAR) {
						kn->kn_data = 0;
						kn->kn_fflags = 0;
					}
					if (kn->kn_flags & EV_DISPATCH) {
						kn->kn_status |= KN_DISABLED;
					}
					kn->kn_status &= ~(KN_QUEUED |
							   KN_ACTIVE);
				} else {
					TAILQ_INSERT_TAIL(&kq->kq_knpend,
							  kn,
							  kn_tqe);
					kq->kq_count++;
				}
			}
		}

		/*
		 * Handle any post-processing states
		 */
		knote_release(kn);
	}
	TAILQ_REMOVE(&kq->kq_knpend, &local_marker, kn_tqe);

	lwkt_relpooltoken(kq);
	return (total);
}

/*
 * XXX
 * This could be expanded to call kqueue_scan, if desired.
 *
 * MPSAFE
 */
static int
kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
{
	return (ENXIO);
}

/*
 * MPSAFE
 */
static int
kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
{
	return (ENXIO);
}

/*
 * MPALMOSTSAFE
 */
static int
kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
	     struct ucred *cred, struct sysmsg *msg)
{
	struct kqueue *kq;
	int error;

	kq = (struct kqueue *)fp->f_data;
	lwkt_getpooltoken(kq);
	switch(com) {
	case FIOASYNC:
		if (*(int *)data)
			kq->kq_state |= KQ_ASYNC;
		else
			kq->kq_state &= ~KQ_ASYNC;
		error = 0;
		break;
	case FIOSETOWN:
		error = fsetown(*(int *)data, &kq->kq_sigio);
		break;
	default:
		error = ENOTTY;
		break;
	}
	lwkt_relpooltoken(kq);
	return (error);
}

/*
 * MPSAFE
 */
static int
kqueue_stat(struct file *fp, struct stat *st, struct ucred *cred)
{
	struct kqueue *kq = (struct kqueue *)fp->f_data;

	bzero((void *)st, sizeof(*st));
	st->st_size = kq->kq_count;
	st->st_blksize = sizeof(struct kevent);
	st->st_mode = S_IFIFO;
	return (0);
}

/*
 * MPSAFE
 */
static int
kqueue_close(struct file *fp)
{
	struct kqueue *kq = (struct kqueue *)fp->f_data;

	kqueue_terminate(kq);

	fp->f_data = NULL;
	funsetown(&kq->kq_sigio);

	kfree(kq, M_KQUEUE);
	return (0);
}

static void
kqueue_wakeup(struct kqueue *kq)
{
	if (kq->kq_sleep_cnt) {
		u_int sleep_cnt = kq->kq_sleep_cnt;

		kq->kq_sleep_cnt = 0;
		if (sleep_cnt == 1)
			wakeup_one(kq);
		else
			wakeup(kq);
	}
	KNOTE(&kq->kq_kqinfo.ki_note, 0);
}

/*
 * Calls filterops f_attach function, acquiring mplock if filter is not
 * marked as FILTEROP_MPSAFE.
 *
 * Caller must be holding the related kq token
 */
static int
filter_attach(struct knote *kn)
{
	int ret;

	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
		ret = kn->kn_fop->f_attach(kn);
	} else {
		get_mplock();
		ret = kn->kn_fop->f_attach(kn);
		rel_mplock();
	}
	return (ret);
}

/*
 * Detach the knote and drop it, destroying the knote.
 *
 * Calls filterops f_detach function, acquiring mplock if filter is not
 * marked as FILTEROP_MPSAFE.
 *
 * Caller must be holding the related kq token
 */
static void
knote_detach_and_drop(struct knote *kn)
{
	kn->kn_status |= KN_DELETING | KN_REPROCESS;
	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
		kn->kn_fop->f_detach(kn);
	} else {
		get_mplock();
		kn->kn_fop->f_detach(kn);
		rel_mplock();
	}
	knote_drop(kn);
}

/*
 * Calls filterops f_event function, acquiring mplock if filter is not
 * marked as FILTEROP_MPSAFE.
 *
 * If the knote is in the middle of being created or deleted we cannot
 * safely call the filter op.
 *
 * Caller must be holding the related kq token
 */
static int
filter_event(struct knote *kn, long hint)
{
	int ret;

	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
		ret = kn->kn_fop->f_event(kn, hint);
	} else {
		get_mplock();
		ret = kn->kn_fop->f_event(kn, hint);
		rel_mplock();
	}
	return (ret);
}

/*
 * Walk down a list of knotes, activating them if their event has triggered.
 *
 * If we encounter any knotes which are undergoing processing we just mark
 * them for reprocessing and do not try to [re]activate the knote.  However,
 * if a hint is being passed we have to wait and that makes things a bit
 * sticky.
 */
void
knote(struct klist *list, long hint)
{
	struct kqueue *kq;
	struct knote *kn;
	struct knote *kntmp;

	lwkt_getpooltoken(list);
restart:
	SLIST_FOREACH(kn, list, kn_next) {
		kq = kn->kn_kq;
		lwkt_getpooltoken(kq);

		/* temporary verification hack */
		SLIST_FOREACH(kntmp, list, kn_next) {
			if (kn == kntmp)
				break;
		}
		if (kn != kntmp || kn->kn_kq != kq) {
			lwkt_relpooltoken(kq);
			goto restart;
		}

		if (kn->kn_status & KN_PROCESSING) {
			/*
			 * Someone else is processing the knote, ask the
			 * other thread to reprocess it and don't mess
			 * with it otherwise.
			 */
			if (hint == 0) {
				kn->kn_status |= KN_REPROCESS;
				lwkt_relpooltoken(kq);
				continue;
			}

			/*
			 * If the hint is non-zero we have to wait or risk
			 * losing the state the caller is trying to update.
			 *
			 * XXX This is a real problem, certain process
			 *     and signal filters will bump kn_data for
			 *     already-processed notes more than once if
			 *     we restart the list scan.  FIXME.
			 */
			kn->kn_status |= KN_WAITING | KN_REPROCESS;
			tsleep(kn, 0, "knotec", hz);
			lwkt_relpooltoken(kq);
			goto restart;
		}

		/*
		 * Become the reprocessing master ourselves.
		 *
		 * If hint is non-zero running the event is mandatory
		 * when not deleting so do it whether reprocessing is
		 * set or not.
		 */
		kn->kn_status |= KN_PROCESSING;
		if ((kn->kn_status & KN_DELETING) == 0) {
			if (filter_event(kn, hint))
				KNOTE_ACTIVATE(kn);
		}
		if (knote_release(kn)) {
			lwkt_relpooltoken(kq);
			goto restart;
		}
		lwkt_relpooltoken(kq);
	}
	lwkt_relpooltoken(list);
}

/*
 * Insert knote at head of klist.
 *
 * This function may only be called via a filter function and thus
 * kq_token should already be held and marked for processing.
 */
void
knote_insert(struct klist *klist, struct knote *kn)
{
	lwkt_getpooltoken(klist);
	KKASSERT(kn->kn_status & KN_PROCESSING);
	SLIST_INSERT_HEAD(klist, kn, kn_next);
	lwkt_relpooltoken(klist);
}

/*
 * Remove knote from a klist
 *
 * This function may only be called via a filter function and thus
 * kq_token should already be held and marked for processing.
 */
void
knote_remove(struct klist *klist, struct knote *kn)
{
	lwkt_getpooltoken(klist);
	KKASSERT(kn->kn_status & KN_PROCESSING);
	SLIST_REMOVE(klist, kn, knote, kn_next);
	lwkt_relpooltoken(klist);
}

void
knote_assume_knotes(struct kqinfo *src, struct kqinfo *dst,
		    struct filterops *ops, void *hook)
{
	struct kqueue *kq;
	struct knote *kn;

	lwkt_getpooltoken(&src->ki_note);
	lwkt_getpooltoken(&dst->ki_note);
	while ((kn = SLIST_FIRST(&src->ki_note)) != NULL) {
		kq = kn->kn_kq;
		lwkt_getpooltoken(kq);
		if (SLIST_FIRST(&src->ki_note) != kn || kn->kn_kq != kq) {
			lwkt_relpooltoken(kq);
			continue;
		}
		if (knote_acquire(kn)) {
			knote_remove(&src->ki_note, kn);
			kn->kn_fop = ops;
			kn->kn_hook = hook;
			knote_insert(&dst->ki_note, kn);
			knote_release(kn);
			/* kn may be invalid now */
		}
		lwkt_relpooltoken(kq);
	}
	lwkt_relpooltoken(&dst->ki_note);
	lwkt_relpooltoken(&src->ki_note);
}

/*
 * Remove all knotes referencing a specified fd
 */
void
knote_fdclose(struct file *fp, struct filedesc *fdp, int fd)
{
	struct kqueue *kq;
	struct knote *kn;
	struct knote *kntmp;

	lwkt_getpooltoken(&fp->f_klist);
restart:
	SLIST_FOREACH(kn, &fp->f_klist, kn_link) {
		if (kn->kn_kq->kq_fdp == fdp && kn->kn_id == fd) {
			kq = kn->kn_kq;
			lwkt_getpooltoken(kq);

			/* temporary verification hack */
			SLIST_FOREACH(kntmp, &fp->f_klist, kn_link) {
				if (kn == kntmp)
					break;
			}
			if (kn != kntmp || kn->kn_kq->kq_fdp != fdp ||
			    kn->kn_id != fd || kn->kn_kq != kq) {
				lwkt_relpooltoken(kq);
				goto restart;
			}
			if (knote_acquire(kn))
				knote_detach_and_drop(kn);
			lwkt_relpooltoken(kq);
			goto restart;
		}
	}
	lwkt_relpooltoken(&fp->f_klist);
}

/*
 * Low level attach function.
 *
 * The knote should already be marked for processing.
 * Caller must hold the related kq token.
 */
static void
knote_attach(struct knote *kn)
{
	struct klist *list;
	struct kqueue *kq = kn->kn_kq;

	if (kn->kn_fop->f_flags & FILTEROP_ISFD) {
		KKASSERT(kn->kn_fp);
		list = &kn->kn_fp->f_klist;
	} else {
		if (kq->kq_knhashmask == 0)
			kq->kq_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
						 &kq->kq_knhashmask);
		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
	}
	lwkt_getpooltoken(list);
	SLIST_INSERT_HEAD(list, kn, kn_link);
	lwkt_relpooltoken(list);
	TAILQ_INSERT_HEAD(&kq->kq_knlist, kn, kn_kqlink);
}

/*
 * Low level drop function.
 *
 * The knote should already be marked for processing.
 * Caller must hold the related kq token.
 */
static void
knote_drop(struct knote *kn)
{
	struct kqueue *kq;
	struct klist *list;

	kq = kn->kn_kq;

	if (kn->kn_fop->f_flags & FILTEROP_ISFD)
		list = &kn->kn_fp->f_klist;
	else
		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];

	lwkt_getpooltoken(list);
	SLIST_REMOVE(list, kn, knote, kn_link);
	lwkt_relpooltoken(list);
	TAILQ_REMOVE(&kq->kq_knlist, kn, kn_kqlink);
	if (kn->kn_status & KN_QUEUED)
		knote_dequeue(kn);
	if (kn->kn_fop->f_flags & FILTEROP_ISFD) {
		fdrop(kn->kn_fp);
		kn->kn_fp = NULL;
	}
	knote_free(kn);
}

/*
 * Low level enqueue function.
 *
 * The knote should already be marked for processing.
 * Caller must be holding the kq token
 */
static void
knote_enqueue(struct knote *kn)
{
	struct kqueue *kq = kn->kn_kq;

	KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
	TAILQ_INSERT_TAIL(&kq->kq_knpend, kn, kn_tqe);
	kn->kn_status |= KN_QUEUED;
	++kq->kq_count;

	/*
	 * Send SIGIO on request (typically set up as a mailbox signal)
	 */
	if (kq->kq_sigio && (kq->kq_state & KQ_ASYNC) && kq->kq_count == 1)
		pgsigio(kq->kq_sigio, SIGIO, 0);

	kqueue_wakeup(kq);
}

/*
 * Low level dequeue function.
 *
 * The knote should already be marked for processing.
 * Caller must be holding the kq token
 */
static void
knote_dequeue(struct knote *kn)
{
	struct kqueue *kq = kn->kn_kq;

	KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
	TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
	kn->kn_status &= ~KN_QUEUED;
	kq->kq_count--;
}

static struct knote *
knote_alloc(void)
{
	return kmalloc(sizeof(struct knote), M_KQUEUE, M_WAITOK);
}

static void
knote_free(struct knote *kn)
{
	struct knote_cache_list *cache_list;

	cache_list = &knote_cache_lists[mycpuid];
	if (cache_list->knote_cache_cnt < KNOTE_CACHE_MAX) {
		crit_enter();
		SLIST_INSERT_HEAD(&cache_list->knote_cache, kn, kn_link);
		cache_list->knote_cache_cnt++;
		crit_exit();
		return;
	}
	kfree(kn, M_KQUEUE);
}

struct sleepinfo {
	void *ident;
	int timedout;
};

static void
precise_sleep_intr(systimer_t info, int in_ipi, struct intrframe *frame)
{
	struct sleepinfo *si;

	si = info->data;
	si->timedout = 1;
	wakeup(si->ident);
}

static int
precise_sleep(void *ident, int flags, const char *wmesg, int us)
{
	struct systimer info;
	struct sleepinfo si = {
		.ident = ident,
		.timedout = 0,
	};
	int r;

	tsleep_interlock(ident, flags);
	systimer_init_oneshot(&info, precise_sleep_intr, &si, us);
	r = tsleep(ident, flags | PINTERLOCKED, wmesg, 0);
	systimer_del(&info);
	if (si.timedout)
		r = EWOULDBLOCK;

	return r;
}