aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/sched_mc2.c
blob: a000242d50453f1291bd9892510fa7f9641d772e (plain) (blame)
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
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
/*
 * litmus/sched_mc2.c
 *
 * Implementation of the Mixed-Criticality on MultiCore scheduler
 *
 * Thus plugin implements a scheduling algorithm proposed in 
 * "Mixed-Criticality Real-Time Scheduling for Multicore System" paper.
 */ 
 
#include <linux/percpu.h>
#include <linux/slab.h>
#include <linux/rwlock.h>
#include <asm/uaccess.h>
#include <linux/delay.h>

#include <litmus/sched_plugin.h>
#include <litmus/preempt.h>
#include <litmus/debug_trace.h>

#include <litmus/litmus.h>
#include <litmus/jobs.h>
#include <litmus/budget.h>
#include <litmus/litmus_proc.h>
#include <litmus/sched_trace.h>
#include <litmus/cache_proc.h>
#include <litmus/trace.h>

#include <litmus/mc2_common.h>
#include <litmus/reservation.h>
#include <litmus/polling_reservations.h>

#ifdef CONFIG_PGMRT_SUPPORT
#include <litmus/pgm.h>
#endif

//#define TRACE(fmt, args...) do {} while (false)
//#define TRACE_TASK(fmt, args...) do {} while (false)

#define BUDGET_ENFORCEMENT_AT_C 0

extern int num_sync_released;
extern void do_partition(enum crit_level lv, int cpu);

/* _global_env - reservation container for level-C tasks*/
struct gmp_reservation_environment _global_env_modes[NR_MODES];
struct gmp_reservation_environment *_global_env;
raw_spinlock_t global_lock;

/* cpu_entry - keep track of a running task on a cpu
 * This state is used to decide the lowest priority cpu
 */
struct cpu_entry {
	struct task_struct *scheduled;
	lt_t deadline;
	int cpu;
	enum crit_level lv;
	/* if will_schedule is true, this cpu is already selected and
	   call mc2_schedule() soon. */
	bool will_schedule;
};

/* cpu_priority - a global state for choosing the lowest priority CPU */
struct cpu_priority {
	raw_spinlock_t lock;
	struct cpu_entry cpu_entries[NR_CPUS];
};

struct cpu_priority _lowest_prio_cpu;
	
/* mc2_task_state - a task state structure */
struct mc2_task_state {
	struct task_client res_info[NR_MODES];
	/* if cpu == -1, this task is a global task (level C) */
	int cpu;
	bool has_departed;
	struct mc2_task mc2_param;
};

/* mc2_cpu_state - maintain the scheduled state and ghost jobs
 * timer : timer for partitioned tasks (level A and B)
 * g_timer : timer for global tasks (level C)
 */
struct mc2_cpu_state {
	raw_spinlock_t lock;

	struct sup_reservation_environment sup_env_modes[NR_MODES];
	struct sup_reservation_environment *sup_env;
	struct hrtimer timer;

	int cpu;
	struct task_struct* scheduled;
	//struct crit_entry crit_entries[NUM_CRIT_LEVELS];
	bool spin_flag; //not used on cpu 0
};

static void mc2_update_timer_and_unlock(struct mc2_cpu_state *state);

static int resched_cpu[NR_CPUS];
static DEFINE_PER_CPU(struct mc2_cpu_state, mc2_cpu_state);
//level_a_priorities unused
//static int level_a_priorities[NR_CPUS];

#define cpu_state_for(cpu_id)	(&per_cpu(mc2_cpu_state, cpu_id))
#define local_cpu_state()	(this_cpu_ptr(&mc2_cpu_state))


unsigned int mode; //currently executing mode, from 0 to NR_MODES-1
unsigned int requested_mode; //The pending mode
/* Prevent multiple requests from entering and prevent request from entering while old
 * is being enacted */
raw_spinlock_t mode_lock;

unsigned int mode_sizes[NR_MODES];
unsigned int res_reported;
bool cpu_0_spin_flag;
bool seen_once;
bool cpu_0_task_exist;
bool mode_changed;
static DEFINE_PER_CPU(unsigned long, mode_counter);
#define local_mode_counter()	(this_cpu_ptr(&mode_counter))
#define cpu_0_mode_counter()	(&per_cpu(mode_counter, 0))
#define in_mode(t, modenum)	(tsk_mc2_data(t)->mode_mask & (1 << modenum))
#define pending			mode != requested_mode
#define ready			!res_reported

/*
 * To be called from level A task's with period equal to
 * A and B hyperperiod
 */

asmlinkage long sys_enact_mode(void)
{
	struct mc2_cpu_state *state = local_cpu_state();
	struct reservation *res;
	struct list_head *pos;
	unsigned long flags;
	TRACE_TASK(current, "ENACTING SYSCALL\n");
	if (state->cpu == 0){
		mode_changed = false;
		local_irq_save(flags);
		if (pending){ //MCR has entered 
			raw_spin_lock(&state->lock);
			raw_spin_lock(&global_lock);
			raw_spin_lock(&mode_lock);
		
			if (!seen_once){
				TRACE_TASK(current, "REQUEST\n");
				sched_trace_request_mode(current);
				//clean up jobs that are already done
				//after this jobs report themselves
				list_for_each(pos, &_global_env->active_reservations){
					res = list_entry(pos, struct reservation, list);
					if (tsk_rt(res->tsk)->completed && res->mode == mode && !res->reported){
						res->reported = 1;
						TRACE_CUR("R%d RES_REPORTED_ACTIVE = %d mode %d\n", res->id, res_reported, res->mode);
						res_reported--;
					}
				}
				list_for_each(pos, &_global_env->depleted_reservations){
					res = list_entry(pos, struct reservation, list);
					if (tsk_rt(res->tsk)->completed && res->mode == mode && !res->reported){
						res->reported = 1;
						TRACE_CUR("R%d RES_REPORTED_DEPLETED = %d mode %d\n",res->id, res_reported, res->mode);
						res_reported--;
					}

				}
				list_for_each(pos, &_global_env->inactive_reservations){
					res = list_entry(pos, struct reservation, list);
					if (tsk_rt(res->tsk)->completed && res->mode == mode && !res->reported){
						res->reported = 1;
						TRACE_CUR("R%d RES_REPORTED_INACTIVE = %d mode %d\n", res->id, res_reported, res->mode);
						res_reported--;
					}
				}
				seen_once = true;
			}
			if( ready ){ //C is throttled
				lt_t new_mode_basetime = get_release(current);
				//TRACE("Timer canceled\n");
				//hrtimer_cancel(&state->timer);//stop listening to old mode timers
				mode = requested_mode;
				TRACE("Mode has been changed.\n");
				mode_changed = true;
				_global_env = &_global_env_modes[mode];
				//set res->reported for new global tasks
				list_for_each(pos, &_global_env->active_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
					res->reported = 0;
				}
				list_for_each(pos, &_global_env->depleted_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
					res->reported = 0;
				}
				list_for_each(pos, &_global_env->inactive_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
					res->reported = 0;
				}
				gmp_update_time(_global_env, litmus_clock());
				//raw_spin_lock(&state->lock);
		
				state->sup_env = &state->sup_env_modes[mode];
				list_for_each(pos, &state->sup_env->active_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
				}
				list_for_each(pos, &state->sup_env->depleted_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
				}
				list_for_each(pos, &state->sup_env->inactive_reservations){
					res = list_entry(pos, struct reservation, list);
					release_at(res->tsk, new_mode_basetime);
				}
				sup_update_time(state->sup_env, litmus_clock());
				//raw_spin_unlock(&state->lock);
				sched_trace_enact_mode(current);
				TRACE("ENACT\n");
			}
			raw_spin_unlock(&mode_lock);
			raw_spin_unlock(&global_lock);
			//raw_spin_unlock(&state->lock);
			mc2_update_timer_and_unlock(state);
		}
		this_cpu_inc(mode_counter);
		local_irq_restore(flags);
		//cpu_0_spin_flag = !cpu_0_spin_flag;
	}
	else if (cpu_0_task_exist) {
		//spin, wait for CPU 0 to stabilize mode decision
		//before scheduling next hyperperiod
		//TRACE("CPU%d start spinning. %d\n",state->cpu, mode_changed);
		unsigned long *cpu0_counter = cpu_0_mode_counter();
/*
		if (state->spin_flag) {
			while(cpu_0_spin_flag)
				udelay(1);
		}
		else {
			while(!cpu_0_spin_flag)
				udelay(1);
		}
		*/
		//TRACE("CPU%d flag check. %d\n",state->cpu, mode_changed);
		while (*cpu0_counter == this_cpu_read(mode_counter))
			udelay(1);
		TRACE("CPU%d counter check. %d\n",state->cpu, this_cpu_read(mode_counter));
		local_irq_save(flags);
		if (mode_changed) {
			lt_t new_mode_basetime = get_release(current);
			//TRACE("CPU%d mode changed\n",state->cpu);
			hrtimer_cancel(&state->timer); //stop listening to old mode timers
			TRACE("Timer is cancelled at %llu. mode-change\n", litmus_clock());
			//local_irq_save(flags);
			raw_spin_lock(&state->lock);
			state->sup_env = &state->sup_env_modes[mode];
			list_for_each(pos, &state->sup_env->active_reservations){
				res = list_entry(pos, struct reservation, list);
				release_at(res->tsk, new_mode_basetime);
			}
			list_for_each(pos, &state->sup_env->depleted_reservations){
				res = list_entry(pos, struct reservation, list);
				release_at(res->tsk, new_mode_basetime);
			}
			list_for_each(pos, &state->sup_env->inactive_reservations){
				res = list_entry(pos, struct reservation, list);
				release_at(res->tsk, new_mode_basetime);
			}
			sup_update_time(state->sup_env, litmus_clock());
			//raw_spin_unlock(&state->lock);
			mc2_update_timer_and_unlock(state);
			//local_irq_restore(flags);
	
		}
		this_cpu_write(mode_counter, *cpu0_counter);
		local_irq_restore(flags);
		//state->spin_flag = !state->spin_flag;
	}
	else {
		//TRACE("CPU%d no cpu_0_task_exist.%d\n",state->cpu, mode_changed);
		return 0;
	}
	TRACE("CPU%d enact syscall ends m_c? %d new_mode %d\n",state->cpu, mode_changed, mode);
	//if mode didn't change this has no effect on what's being scheduled
	//raw_spin_lock(&state->lock);
	//state->sup_env = &state->sup_env_modes[mode];
	//raw_spin_unlock(&state->lock);
	//sup_update_time(state->sup_env, litmus_clock());
	//raw_spin_lock(&state->lock);
	//mc2_update_timer_and_unlock(state);
	TRACE("is timer active? %d remaining %llu\n",hrtimer_active(&state->timer), hrtimer_get_remaining(&state->timer));
	
	return 0;
}


/*
 * Called from non-real time program
 * Protect by exclusive lock to prevent from occuring while mode change is enacted
 */
asmlinkage long sys_request_mode(int new_mode){
	preempt_disable();
	raw_spin_lock(&mode_lock);
	if (pending){
		raw_spin_unlock(&mode_lock);
		preempt_enable();
		return -EAGAIN;
	}
	if (mode == new_mode){
		raw_spin_unlock(&mode_lock);
		preempt_enable();
		return 0;
	}
	requested_mode = new_mode;
	TRACE("MCR received\n");
	res_reported = mode_sizes[mode];
	TRACE_CUR("RES_REPORTED = %d\n",res_reported);
	seen_once = false;
	raw_spin_unlock(&mode_lock);
	preempt_enable();
	return 0;
}
	

/* get_mc2_state - get the task's state */
static struct mc2_task_state* get_mc2_state(struct task_struct *tsk)
{
	struct mc2_task_state* tinfo;
	
	tinfo = (struct mc2_task_state*)tsk_rt(tsk)->plugin_state;
	
	if (tinfo)
		return tinfo;
	else
		return NULL;
}

/* get_task_crit_level - return the criticaility level of a task */
static enum crit_level get_task_crit_level(struct task_struct *tsk)
{
	struct mc2_task *mp;
	
	if (!tsk || !is_realtime(tsk))
		return NUM_CRIT_LEVELS;
	
	mp = tsk_rt(tsk)->mc2_data;
	
	if (!mp)
		return NUM_CRIT_LEVELS;
	else
		return mp->crit;
}

static int is_init_finished(struct task_struct *tsk)
{
	struct mc2_task *mp;
	
	if (!tsk || !is_realtime(tsk))
		return 0;
	
	mp = tsk_rt(tsk)->mc2_data;
	
	if (!mp)
		return 0;
	else
		return mp->init_finished;
}

/* task_depart - remove a task from its reservation
 *               If the job has remaining budget, convert it to a ghost job
 *               and update crit_entries[]
 *               
 * @job_complete	indicate whether job completes or not              
 */
static void task_departs(struct task_struct *tsk, int job_complete)
{
	struct mc2_task_state* tinfo = get_mc2_state(tsk);
	//struct mc2_cpu_state* state = local_cpu_state();
	struct reservation* res = NULL;
	struct reservation_client *client = NULL;
	int i;
	BUG_ON(!is_realtime(tsk));

	for(i = 0; i < NR_MODES; i++){
		if (! in_mode(tsk, i) && i != 0)
			continue;	
		res    = tinfo->res_info[i].client.reservation;
		client = &tinfo->res_info[i].client;
		BUG_ON(!res);
		BUG_ON(!client);

		if (job_complete)
			res->cur_budget = 0;
		
		res->ops->client_departs(res, client, job_complete);
	}

/* 9/18/2015 fix start - no ghost job handling, empty remaining budget */
/*
	if (job_complete) {
		//res->cur_budget = 0;
	}
*/
/* fix end */

	tinfo->has_departed = true;
	TRACE_TASK(tsk, "CLIENT DEPART with budget %llu at %llu\n", res->cur_budget, litmus_clock());
}

/* task_arrive - put a task into its reservation
 *               If the job was a ghost job, remove it from crit_entries[]
 */
static void task_arrives(struct mc2_cpu_state *state, struct task_struct *tsk)
{
	struct mc2_task_state* tinfo = get_mc2_state(tsk);
	struct reservation* res;
	struct reservation_client *client;
	enum crit_level lv = get_task_crit_level(tsk);
	int i;

	switch(lv) {
		case CRIT_LEVEL_A:
		case CRIT_LEVEL_B:
			TS_RELEASE_START;
			break;
		case CRIT_LEVEL_C:
			TS_RELEASE_C_START;
			break;
		default:
			break;
	}

	tinfo->has_departed = false;

	TRACE_TASK(tsk, "CLIENT ARRIVES at %llu\n", litmus_clock());
	
	for(i = 0; i < NR_MODES; i++){
		if (! in_mode(tsk, i) && i != 0)
			continue;	
		res    = tinfo->res_info[i].client.reservation;
		client = &tinfo->res_info[i].client;

		res->ops->client_arrives(res, client);
	}	

	switch(lv) {
		case CRIT_LEVEL_A:
		case CRIT_LEVEL_B:
			TS_RELEASE_END;
			break;
		case CRIT_LEVEL_C:
			TS_RELEASE_C_END;
			break;
		default:
			break;
	}
}

/* get_lowest_prio_cpu - return the lowest priority cpu
 *                       This will be used for scheduling level-C tasks.
 *                       If all CPUs are running tasks which has
 *                       higher priority than level C, return NO_CPU.
 */
static int get_lowest_prio_cpu(lt_t priority)
{
	struct cpu_entry *ce;
	int cpu, ret = NO_CPU;
	lt_t latest_deadline = 0;
	
	if (priority == LITMUS_NO_PRIORITY)
		return ret;

	ce = &_lowest_prio_cpu.cpu_entries[local_cpu_state()->cpu];
	if (!ce->will_schedule && !ce->scheduled) {
		TRACE("CPU %d (local) is the lowest!\n", ce->cpu);
		return ce->cpu;
	} else {
		TRACE("Local CPU will_schedule=%d, scheduled=(%s/%d)\n", ce->will_schedule, ce->scheduled ? (ce->scheduled)->comm : "null", ce->scheduled ? (ce->scheduled)->pid : 0);
	}

	for_each_online_cpu(cpu) {
		ce = &_lowest_prio_cpu.cpu_entries[cpu];
		/* If a CPU will call schedule() in the near future, we don't
		   return that CPU. */
		TRACE("CPU %d will_schedule=%d, scheduled=(%s/%d:%d)\n", cpu, ce->will_schedule,
	      ce->scheduled ? (ce->scheduled)->comm : "null",
	      ce->scheduled ? (ce->scheduled)->pid : 0,
	      ce->scheduled ? (ce->scheduled)->rt_param.job_params.job_no : 0);
		if (!ce->will_schedule) {
			if (!ce->scheduled) {
				/* Idle cpu, return this. */
				TRACE("CPU %d is the lowest!\n", ce->cpu);
				return ce->cpu;
			} else if (ce->lv == CRIT_LEVEL_C && 
			           ce->deadline > latest_deadline) {
				latest_deadline = ce->deadline;
				ret = ce->cpu;
			}
		}
	}		
	
	if (priority >= latest_deadline)
		ret = NO_CPU;
	
	TRACE("CPU %d is the lowest!\n", ret);

	return ret;
}

/* NOTE: drops state->lock */
/* mc2_update_timer_and_unlock - set a timer and unlock state->lock.
 *                               Whenever res_env.current_time is updated,
 *                               we check next_scheduler_update and set 
 *                               a timer.
 *                               If there exist a global event which is 
 *                               not armed on any CPU and g_timer is not
 *                               active, set a g_timer for that event.
 */
static void mc2_update_timer_and_unlock(struct mc2_cpu_state *state)
{
	int local, cpus;
	lt_t update, now;
	//enum crit_level lv = get_task_crit_level(state->scheduled);
	struct next_timer_event *event, *next;
	int reschedule[NR_CPUS];

	for (cpus = 0; cpus<NR_CPUS; cpus++)
		reschedule[cpus] = 0;
	
	update = state->sup_env->next_scheduler_update;
	now = state->sup_env->env.current_time;

	/* Be sure we're actually running on the right core,
	 * as pres_update_timer() is also called from pres_task_resume(),
	 * which might be called on any CPU when a thread resumes.
	 */
	local = local_cpu_state() == state;

	raw_spin_lock(&global_lock);
		
	list_for_each_entry_safe(event, next, &_global_env->next_events, list) {
		/* If the event time is already passed, we call schedule() on
		   the lowest priority cpu */
		if (event->next_update >= update) {
			break;
		}
		
		if (event->next_update < litmus_clock()) {
			if (event->timer_armed_on == NO_CPU) {
				struct reservation *res = gmp_find_by_id(_global_env, event->id);
				int cpu = get_lowest_prio_cpu(res?res->priority:LITMUS_NO_PRIORITY);
				//TRACE("GLOBAL EVENT PASSED!! poking CPU %d to reschedule\n", cpu);
				list_del(&event->list);
				kfree(event);
				if (cpu != NO_CPU) {
					//raw_spin_lock(&_lowest_prio_cpu.lock);
					_lowest_prio_cpu.cpu_entries[cpu].will_schedule = true;
					//raw_spin_unlock(&_lowest_prio_cpu.lock);
					if (cpu == local_cpu_state()->cpu)
						litmus_reschedule_local();
					else
						reschedule[cpu] = 1;
				}
			}
		} else if (event->next_update < update && (event->timer_armed_on == NO_CPU || event->timer_armed_on == state->cpu)) {
			event->timer_armed_on = state->cpu;
			update = event->next_update;
			break;
		}
	}
	
	/* Must drop state lock before calling into hrtimer_start(), which
	 * may raise a softirq, which in turn may wake ksoftirqd. */
	raw_spin_unlock(&global_lock);
	raw_spin_unlock(&state->lock);

	if ((update <= now) || reschedule[state->cpu]) {
		reschedule[state->cpu] = 0;
		litmus_reschedule(state->cpu);
		/*
		raw_spin_lock(&state->lock);
		preempt_if_preemptable(state->scheduled, state->cpu);
		raw_spin_unlock(&state->lock);
		*/
	} else if (likely(local && update != SUP_NO_SCHEDULER_UPDATE)) {
		/* Reprogram only if not already set correctly. */
		if (!hrtimer_active(&state->timer) ||
		    ktime_to_ns(hrtimer_get_expires(&state->timer)) != update) {
			TRACE("canceling timer...at %llu\n", 
			      ktime_to_ns(hrtimer_get_expires(&state->timer)));
			hrtimer_cancel(&state->timer);
			TRACE("setting scheduler timer for %llu\n", update);
			/* We cannot use hrtimer_start() here because the
			 * wakeup flag must be set to zero. */
			__hrtimer_start_range_ns(&state->timer,
					ns_to_ktime(update),
					0 /* timer coalescing slack */,
					HRTIMER_MODE_ABS_PINNED,
					0 /* wakeup */);
			if (update < litmus_clock()) {
				/* uh oh, timer expired while trying to set it */
				TRACE("timer expired during setting "
				      "update:%llu now:%llu actual:%llu\n",
				      update, now, litmus_clock());
	 			/* The timer HW may not have been reprogrammed
	 			 * correctly; force rescheduling now. */
				litmus_reschedule(state->cpu);
			}
		}
	} else if (unlikely(!local && update != SUP_NO_SCHEDULER_UPDATE)) {
		/* Poke remote core only if timer needs to be set earlier than
		 * it is currently set.
		 */
		TRACE("mc2_update_timer for remote CPU %d (update=%llu, "
		      "active:%d, set:%llu)\n",
			state->cpu, update, hrtimer_active(&state->timer),
			ktime_to_ns(hrtimer_get_expires(&state->timer)));
		if (!hrtimer_active(&state->timer) ||
		    ktime_to_ns(hrtimer_get_expires(&state->timer)) > update) {
			TRACE("poking CPU %d so that it can update its "
			      "scheduling timer (active:%d, set:%llu)\n",
			      state->cpu,
			      hrtimer_active(&state->timer),
			      ktime_to_ns(hrtimer_get_expires(&state->timer)));
			//litmus_reschedule(state->cpu);
/*
			raw_spin_lock(&state->lock);
			preempt_if_preemptable(state->scheduled, state->cpu);
			raw_spin_unlock(&state->lock);
			reschedule[state->cpu] = 0;
*/			
		}
	}
/*	
	for (cpus = 0; cpus<NR_CPUS; cpus++) {
		if (reschedule[cpus]) {
			litmus_reschedule(cpus);
		}
	}
*/
}

/* update_cpu_prio - Update cpu's priority
 *                   When a cpu picks a new task, call this function
 *                   to update cpu priorities.
 */
static void update_cpu_prio(struct mc2_cpu_state *state)
{
	struct cpu_entry *ce = &_lowest_prio_cpu.cpu_entries[state->cpu];
	enum crit_level lv = get_task_crit_level(state->scheduled);
	
	if (!state->scheduled) {
		/* cpu is idle. */
		ce->scheduled = NULL;
		ce->deadline = ULLONG_MAX;
		ce->lv = NUM_CRIT_LEVELS;
	} else if (lv == CRIT_LEVEL_C) {
		ce->scheduled = state->scheduled;
		ce->deadline = get_deadline(state->scheduled);
		ce->lv = lv;
	} else if (lv < CRIT_LEVEL_C) {
		/* If cpu is running level A or B tasks, it is not eligible
		   to run level-C tasks */
		ce->scheduled = state->scheduled;
		ce->deadline = 0;
		ce->lv = lv;
	}
};

/* on_scheduling_timer - timer event for partitioned tasks
 */                       
static enum hrtimer_restart on_scheduling_timer(struct hrtimer *timer)
{
	unsigned long flags;
	enum hrtimer_restart restart = HRTIMER_NORESTART;
	struct mc2_cpu_state *state;
	lt_t update, now;
	int global_schedule_now;
	//lt_t remain_budget; // no ghost jobs
	int reschedule[NR_CPUS];
	int cpus;
	
	if (mode_changed)
		return restart;
	
	for (cpus = 0; cpus<NR_CPUS; cpus++)
		reschedule[cpus] = 0;
	
	state = container_of(timer, struct mc2_cpu_state, timer);

	/* The scheduling timer should only fire on the local CPU, because
	 * otherwise deadlocks via timer_cancel() are possible.
	 * Note: this does not interfere with dedicated interrupt handling, as
	 * even under dedicated interrupt handling scheduling timers for
	 * budget enforcement must occur locally on each CPU.
	 */
	BUG_ON(state->cpu != raw_smp_processor_id());

	TS_ISR_START;
	
	TRACE("Timer fired at %llu\n", litmus_clock());
	raw_spin_lock_irqsave(&state->lock, flags);
	//raw_spin_lock(&state->lock);
	//local_irq_save(flags);
	now = litmus_clock();
	sup_update_time(state->sup_env, now);

/* 9/20/2015 fix - no ghost job 	
	remain_budget = mc2_update_ghost_state(state);
*/	
	update = state->sup_env->next_scheduler_update;
	now = state->sup_env->env.current_time;


	if (update <= now) {
		litmus_reschedule_local();
	} else if (update != SUP_NO_SCHEDULER_UPDATE) {
		hrtimer_set_expires(timer, ns_to_ktime(update));
		restart = HRTIMER_RESTART;
	}

	raw_spin_lock(&global_lock);
	global_schedule_now = gmp_update_time(_global_env, now);
	BUG_ON(global_schedule_now < 0 || global_schedule_now > 4);
	
	/* Find the lowest cpu, and call reschedule */
	while (global_schedule_now--) {
		int cpu = get_lowest_prio_cpu(0);
		if (cpu != NO_CPU && _lowest_prio_cpu.cpu_entries[cpu].will_schedule == false) {
			//raw_spin_lock(&_lowest_prio_cpu.lock);
			_lowest_prio_cpu.cpu_entries[cpu].will_schedule = true;
			//raw_spin_unlock(&_lowest_prio_cpu.lock);
			TRACE("LOWEST CPU = P%d\n", cpu);
			if (cpu == state->cpu && update > now)
				litmus_reschedule_local();
			else
				reschedule[cpu] = 1;
		}
	} 
	raw_spin_unlock(&global_lock);
	raw_spin_unlock_irqrestore(&state->lock, flags);
	//raw_spin_unlock(&state->lock);
	//local_irq_restore(flags);
	
	TS_ISR_END;
	
	for (cpus = 0; cpus<NR_CPUS; cpus++) {
		if (reschedule[cpus]) {
			litmus_reschedule(cpus);
			/*
			struct mc2_cpu_state *remote_state;

			remote_state = cpu_state_for(cpus);
			raw_spin_lock(&remote_state->lock);
			preempt_if_preemptable(remote_state->scheduled, remote_state->cpu);
			raw_spin_unlock(&remote_state->lock);
			*/
		}
	}
	
	
	return restart;
}

/* mc2_complete_job - syscall backend for job completions
 */
static long mc2_complete_job(void)
{
	ktime_t next_release;
	long err;
	
	enum crit_level lv;
	
	raw_spin_lock(&mode_lock);
	tsk_rt(current)->completed = 1;
	raw_spin_unlock(&mode_lock);

	lv = get_task_crit_level(current);

	/* If this the first job instance, we need to reset replenish
	   time to the next release time */
	if (tsk_rt(current)->sporadic_release) {
		struct mc2_cpu_state *state;
		struct mc2_task_state *tinfo;
		struct reservation *res = NULL;
		unsigned long flags;

		local_irq_save(flags);
		
		tinfo = get_mc2_state(current);
		
		if (lv < CRIT_LEVEL_C) {
			int i;
			state = cpu_state_for(tinfo->cpu);
			raw_spin_lock(&state->lock);
			for (i = 0; i<NR_MODES; i++) {
				if (in_mode(current,i) || i == 0) {
					state->sup_env_modes[i].env.time_zero = tsk_rt(current)->sporadic_release_time;
				}
			}
			res = sup_find_by_id(state->sup_env, tinfo->mc2_param.res_id);
			
		}
		else if (lv == CRIT_LEVEL_C) {
			int i;
			state = local_cpu_state();		
			raw_spin_lock(&state->lock);
			raw_spin_lock(&global_lock);
			for (i = 0; i < NR_MODES; i++) {
				if (in_mode(current,i) || i == 0) {
					_global_env_modes[i].env.time_zero = tsk_rt(current)->sporadic_release_time;
				}
			}
			res = gmp_find_by_id(_global_env, tinfo->mc2_param.res_id);
		}
		else
			BUG();

		/* set next_replenish to synchronous release time */
		BUG_ON(!res);
		res->next_replenishment = tsk_rt(current)->sporadic_release_time;
/*		
		if (get_task_crit_level(current) == CRIT_LEVEL_A) {
			struct table_driven_reservation *tdres;
			tdres = container_of(res, struct table_driven_reservation, res);
			tdres->next_interval = 0;
			tdres->major_cycle_start = tsk_rt(current)->sporadic_release_time;
			res->next_replenishment += tdres->intervals[0].start;			
		}
*/		
		res->cur_budget = 0;
		res->env->change_state(res->env, res, RESERVATION_DEPLETED);
		
	//	TRACE_CUR("CHANGE NEXT_REP = %llu NEXT_UPDATE = %llu\n", res->next_replenishment, state->sup_env->next_scheduler_update);
		if (lv == CRIT_LEVEL_C)
			raw_spin_unlock(&global_lock);
		
		raw_spin_unlock(&state->lock);
		local_irq_restore(flags);
	}
	
	sched_trace_task_completion(current, 0);		
	/* update the next release time and deadline */
	prepare_for_next_period(current);
	sched_trace_task_release(current);
	next_release = ns_to_ktime(get_release(current));
	preempt_disable();
	TRACE_CUR("next_release=%llu\n", get_release(current));

	/*
	 * Changed logic for mode switch case
	 * In case of mode switch, do not want to release
	 * new job even if release time has passed
	 */

	if (lv == CRIT_LEVEL_C && pending) {
		struct reservation *res = NULL;

		raw_spin_lock(&mode_lock);
		res = gmp_find_by_id(_global_env, tsk_mc2_data(current)->res_id);
		if (res && !res->reported){
			res_reported--;
			TRACE_CUR("RES_REPORTED = %d\n",res_reported);
			res->reported = 1;
			//Current task doesn't exist in new mode
			if ( !in_mode(current, requested_mode) ){
				litmus_reschedule_local();
			}
		}
		raw_spin_unlock(&mode_lock);
	}
	

	if (get_release(current) > litmus_clock()) {
		/* sleep until next_release */
		set_current_state(TASK_INTERRUPTIBLE);
		preempt_enable_no_resched();
		TRACE_CUR("Sleep until %llu\n", next_release);
		err = schedule_hrtimeout(&next_release, HRTIMER_MODE_ABS);
	} else {
		/* release the next job immediately */
		err = 0;
		TRACE_CUR("TARDY: release=%llu now=%llu\n", get_release(current), litmus_clock());
		preempt_enable();
	}

	TRACE_CUR("mc2_complete_job returns at %llu\n", litmus_clock());

	raw_spin_lock(&mode_lock);
	tsk_rt(current)->completed = 0;
	raw_spin_unlock(&mode_lock);
	
	return err;
}

/* mc2_dispatch - Select the next task to schedule.
 */
struct task_struct* mc2_dispatch(struct sup_reservation_environment* sup_env, struct mc2_cpu_state* state)
{
	struct reservation *res, *next;
	struct task_struct *tsk = NULL;
	//struct crit_entry *ce;
	enum crit_level lv;
	lt_t time_slice;


	list_for_each_entry_safe(res, next, &sup_env->active_reservations, list) {
		if (res->state == RESERVATION_ACTIVE) 
			TRACE_TASK(tsk, "ACT_LIST R%d cur.mode = %d, res->mode = %d budget = %llu\n", res->id, mode, res->mode, res->cur_budget);
	}
		
	list_for_each_entry_safe(res, next, &sup_env->active_reservations, list) {
		if (res->state == RESERVATION_ACTIVE) {
			tsk = res->ops->dispatch_client(res, &time_slice);
			if (likely(tsk)) {
				lv = get_task_crit_level(tsk);
				if (lv == NUM_CRIT_LEVELS) {
					sup_scheduler_update_after(sup_env, res->cur_budget);
					return tsk;
				} else {
					//if (!is_init_finished(tsk)) {
					TRACE_TASK(tsk, "num_sync_released = %d, mode = %d\n", num_sync_released, mode);
					if (num_sync_released != 0 && mode == 0) {
						//ce = &state->crit_entries[lv];
						sup_scheduler_update_after(sup_env, res->cur_budget);
						res->blocked_by_ghost = 0;
						res->is_ghost = NO_CPU;
						return tsk;
					} else if (res->mode == mode) {
						sup_scheduler_update_after(sup_env, res->cur_budget);
						res->blocked_by_ghost = 0;
						res->is_ghost = NO_CPU;
						return tsk;
					}
				}
			}
		}
	}
	
	return NULL;
}

struct task_struct* mc2_global_dispatch(struct mc2_cpu_state* state)
{
	struct reservation *res, *next;
	struct task_struct *tsk = NULL;
	//struct crit_entry *ce;
	enum crit_level lv;
	lt_t time_slice;
	
	raw_spin_lock(&mode_lock);
	list_for_each_entry_safe(res, next, &_global_env->active_reservations, list) {
		BUG_ON(!res);
		if (res->state == RESERVATION_ACTIVE && res->scheduled_on == NO_CPU) {
			tsk = res->ops->dispatch_client(res, &time_slice);
			if (pending && res->reported && !in_mode(tsk, requested_mode)){
				continue;
			}
			if (likely(tsk)) {
				lv = get_task_crit_level(tsk);
				if (lv == NUM_CRIT_LEVELS) {
#if BUDGET_ENFORCEMENT_AT_C			
					gmp_add_event_after(_global_env, res->cur_budget, res->id, EVENT_DRAIN);
#endif
					res->event_added = 1;
					res->blocked_by_ghost = 0;
					res->is_ghost = NO_CPU;
					res->scheduled_on = state->cpu;
					raw_spin_unlock(&mode_lock);
					return tsk;
				} else {
						//if (!is_init_finished(tsk)) {
						if (num_sync_released != 0 && mode == 0) {
					//ce = &state->crit_entries[lv];
					//if (likely(!ce->running)) {
#if BUDGET_ENFORCEMENT_AT_C
							gmp_add_event_after(_global_env, res->cur_budget, res->id, EVENT_DRAIN);
#endif
							res->event_added = 1;
							res->blocked_by_ghost = 0;
							res->is_ghost = NO_CPU;
							res->scheduled_on = state->cpu;
							raw_spin_unlock(&mode_lock);
							return tsk;
						} else if (res->mode == mode) {
#if BUDGET_ENFORCEMENT_AT_C
							gmp_add_event_after(_global_env, res->cur_budget, res->id, EVENT_DRAIN);
#endif
							res->event_added = 1;
							res->blocked_by_ghost = 0;
							res->is_ghost = NO_CPU;
							res->scheduled_on = state->cpu;
							raw_spin_unlock(&mode_lock);
							return tsk;
						}
					//} else {
					//	res->blocked_by_ghost = 1;
					//	TRACE_TASK(ce->running, " is GHOST\n");
					//	return NULL;
					//}
				}
			}
		}
	}
	raw_spin_unlock(&mode_lock);
	return NULL;
}

static inline void pre_schedule(struct task_struct *prev, int cpu)
{
	TS_SCHED_A_START;
	TS_SCHED_C_START;
	
	if (!prev || !is_realtime(prev))
		return;
	
	do_partition(CRIT_LEVEL_C, cpu);
}

static inline void post_schedule(struct task_struct *next, int cpu)
{
	enum crit_level lev;
	if ((!next) || !is_realtime(next))
		return;

	lev = get_task_crit_level(next);
	if (is_mode_poll_task(next))  {
		lev = MODE_POLL_TASK;
	}

	do_partition(lev, cpu);

	switch(lev) {
		case CRIT_LEVEL_A:
		case CRIT_LEVEL_B:
		case MODE_POLL_TASK:
			TS_SCHED_A_END(next);
			break;
		case CRIT_LEVEL_C:
			TS_SCHED_C_END(next);
			break;
		default:
			break;
	}
	
}

/* mc2_schedule - main scheduler function. pick the next task to run
 */
static struct task_struct* mc2_schedule(struct task_struct * prev)
{
	int np, blocks, exists, preempt, to_schedule;
	/* next == NULL means "schedule background work". */
	lt_t now = litmus_clock();
	struct mc2_cpu_state *state = local_cpu_state();
	
	raw_spin_lock(&state->lock);

	raw_spin_lock(&global_lock);
	preempt = resched_cpu[state->cpu];
	resched_cpu[state->cpu] = 0;
	raw_spin_unlock(&global_lock);
	
	pre_schedule(prev, state->cpu);

	BUG_ON(state->scheduled && state->scheduled != prev);
	BUG_ON(state->scheduled && !is_realtime(prev));
	
	//if (state->scheduled && state->scheduled != prev)
	//	printk(KERN_ALERT "BUG1!!!!!!!! %s %s\n", state->scheduled ? (state->scheduled)->comm : "null", prev ? (prev)->comm : "null");
	//if (state->scheduled && !is_realtime(prev))
	//	printk(KERN_ALERT "BUG2!!!!!!!! \n");

	/* (0) Determine state */
	exists = state->scheduled != NULL;
	blocks = exists && !is_current_running();
	np = exists && is_np(state->scheduled);

	/* update time */
	state->sup_env->will_schedule = true;
	sup_update_time(state->sup_env, now);
	/* 9/20/2015 fix */
	//raw_spin_lock(&_global_env.lock);
	//to_schedule = gmp_update_time(&_global_env, now);
	//raw_spin_unlock(&_global_env.lock);
	
	/* 9/20/2015 fix 
	mc2_update_ghost_state(state);	
	*/
	
	/* remove task from reservation if it blocks */
	/*
	if (is_realtime(prev) && !is_running(prev)) {
		if (get_task_crit_level(prev) == CRIT_LEVEL_C)
			raw_spin_lock(&_global_env.lock);
		task_departs(prev, is_completed(prev));
		if (get_task_crit_level(prev) == CRIT_LEVEL_C)
			raw_spin_unlock(&_global_env.lock);
	}*/
	if (is_realtime(current) && blocks) {
		if (get_task_crit_level(current) == CRIT_LEVEL_C)
			raw_spin_lock(&global_lock);
		task_departs(current, is_completed(current));
		if (get_task_crit_level(current) == CRIT_LEVEL_C)
			raw_spin_unlock(&global_lock);
	}
	
	/* figure out what to schedule next */
	if (!np)
		state->scheduled = mc2_dispatch(state->sup_env, state);

	if (!state->scheduled) {
		raw_spin_lock(&global_lock);
		if (is_realtime(prev))
			gmp_update_time(_global_env, now);
		state->scheduled = mc2_global_dispatch(state);
		raw_spin_unlock(&global_lock);
	}
	
	/*
	if (!state->scheduled) {
		raw_spin_lock(&global_lock);
		//to_schedule = gmp_update_time(_global_env, now);
		state->scheduled = mc2_global_dispatch(state);
		_lowest_prio_cpu.cpu_entries[state->cpu].will_schedule = false;
		update_cpu_prio(state);
		raw_spin_unlock(&global_lock);
	} else {
		raw_spin_lock(&global_lock);
		_lowest_prio_cpu.cpu_entries[state->cpu].will_schedule = false;
		update_cpu_prio(state);
		raw_spin_unlock(&global_lock);
	}
	*/
	
	//raw_spin_lock(&_lowest_prio_cpu.lock);
	//_lowest_prio_cpu.cpu_entries[state->cpu].will_schedule = false;
	//update_cpu_prio(state);
	//raw_spin_unlock(&_lowest_prio_cpu.lock);
	
	/* Notify LITMUS^RT core that we've arrived at a scheduling decision. */
	sched_state_task_picked();

	/* program scheduler timer */
	state->sup_env->will_schedule = false;
		
	/* NOTE: drops state->lock */
	mc2_update_timer_and_unlock(state);

	raw_spin_lock(&state->lock);
	if (prev != state->scheduled && is_realtime(prev)) {
		struct mc2_task_state* tinfo = get_mc2_state(prev);
		struct reservation* res = tinfo->res_info[mode].client.reservation;
		if (res) {
			TRACE_TASK(prev, "PREV JOB was scheduled_on = P%d\n", res->scheduled_on);
			res->scheduled_on = NO_CPU;
		}
		TRACE_TASK(prev, "descheduled at %llu.\n", litmus_clock());
		/* if prev is preempted and a global task, find the lowest cpu and reschedule */
		if (tinfo->has_departed == false && get_task_crit_level(prev) == CRIT_LEVEL_C) {
			int cpu;
			raw_spin_lock(&global_lock);
			cpu = get_lowest_prio_cpu(res?res->priority:LITMUS_NO_PRIORITY);
			if (cpu != NO_CPU && _lowest_prio_cpu.cpu_entries[cpu].will_schedule == false) {
				//raw_spin_lock(&_lowest_prio_cpu.lock);
				_lowest_prio_cpu.cpu_entries[cpu].will_schedule = true;
				resched_cpu[cpu] = 1;
				//raw_spin_unlock(&_lowest_prio_cpu.lock);
				TRACE("LEVEL-C TASK PREEMPTED!! poking CPU %d to reschedule\n", cpu);
			}
			raw_spin_unlock(&global_lock);
		}
	}

/*	
	if (to_schedule != 0) {
		raw_spin_lock(&global_lock);
		while (to_schedule--) {
			int cpu = get_lowest_prio_cpu(0);
			if (cpu != NO_CPU && _lowest_prio_cpu.cpu_entries[cpu].will_schedule == false) {
				_lowest_prio_cpu.cpu_entries[cpu].will_schedule = true;
				resched_cpu[cpu] = 1;
			}
		}
		raw_spin_unlock(&global_lock);	
	}
*/
	post_schedule(state->scheduled, state->cpu);

	raw_spin_lock(&global_lock);
	_lowest_prio_cpu.cpu_entries[state->cpu].will_schedule = false;
	update_cpu_prio(state);
	raw_spin_unlock(&global_lock);
	
	raw_spin_unlock(&state->lock);
	if (state->scheduled) {
		TRACE_TASK(state->scheduled, "scheduled.\n");
	}
	
	
	return state->scheduled;
}

static void resume_legacy_task_model_updates(struct task_struct *tsk)
{
	lt_t now;
	if (is_sporadic(tsk)) {
		/* If this sporadic task was gone for a "long" time and woke up past
		 * its deadline, then give it a new budget by triggering a job
		 * release. This is purely cosmetic and has no effect on the
		 * MC2 scheduler. */

		now = litmus_clock();
		if (is_tardy(tsk, now)) {
			release_at(tsk, now);
		}
	}
}

/* mc2_task_resume - Called when the state of tsk changes back to 
 *                   TASK_RUNNING. We need to requeue the task.
 */
static void mc2_task_resume(struct task_struct  *tsk)
{
	unsigned long flags;
	struct mc2_task_state* tinfo;
	struct mc2_cpu_state *state;

	TRACE_TASK(tsk, "thread wakes up at %llu\n", litmus_clock());

	//local_irq_save(flags);
	TRACE_TASK(tsk, "preemptible?? %d\n", preemptible());
	preempt_disable();
	tinfo = get_mc2_state(tsk);
	if (tinfo->cpu != -1)
		state = cpu_state_for(tinfo->cpu);
	else
		state = local_cpu_state();
	preempt_enable();
	
	/* 9/20/2015 fix
	raw_spin_lock(&_global_env.lock);
	*/
	/* Requeue only if self-suspension was already processed. */
	if (tinfo->has_departed)
	{

		/* We don't want to consider jobs before synchronous releases */
		if (tsk_rt(tsk)->job_params.job_no == 2) {
/*
			switch(get_task_crit_level(tsk)) {
				case CRIT_LEVEL_A:
					TS_RELEASE_LATENCY_A(get_release(tsk));
					break;
				case CRIT_LEVEL_B:
					TS_RELEASE_LATENCY_B(get_release(tsk));
					break;
				case CRIT_LEVEL_C:
					TS_RELEASE_LATENCY_C(get_release(tsk));
					break;
				default:
					break;
			}
*/
			TRACE_TASK(tsk, "INIT_FINISHED is SET\n");
			tsk_mc2_data(tsk)->init_finished = 1;
			raw_spin_lock(&global_lock);
			num_sync_released--;
			raw_spin_unlock(&global_lock);
			TRACE_TASK(tsk, "INIT_FINISHED is SET, num_sync_released decreased to %d\n", num_sync_released);			
		}

		raw_spin_lock(&state->lock);
		local_irq_save(flags);
		/* Assumption: litmus_clock() is synchronized across cores,
		 * since we might not actually be executing on tinfo->cpu
		 * at the moment. */
		if (tinfo->cpu != -1) {
			sup_update_time(state->sup_env, litmus_clock());
			task_arrives(state, tsk);
		} else {
			raw_spin_lock(&global_lock);
			gmp_update_time(_global_env, litmus_clock());
			task_arrives(state, tsk);
			raw_spin_unlock(&global_lock);
		}
			
		/* 9/20/2015 fix 
		mc2_update_ghost_state(state);
		*/
		//task_arrives(state, tsk);
		/* NOTE: drops state->lock */
		TRACE_TASK(tsk, "mc2_resume()\n");
		local_irq_restore(flags);
		mc2_update_timer_and_unlock(state);	
	} else {
		TRACE_TASK(tsk, "resume event ignored, still scheduled\n");
		//raw_spin_unlock(&_global_env.lock);
	}

	//local_irq_restore(flags);
	
	//gmp_free_passed_event();
	resume_legacy_task_model_updates(tsk);
}


/* mc2_admit_task - Setup mc2 task parameters
 */
static long mc2_admit_task(struct task_struct *tsk)
{
	long err = 0;
	unsigned long flags;
	struct reservation *res;
	struct mc2_cpu_state *state;
	struct mc2_task_state *tinfo = kzalloc(sizeof(*tinfo), GFP_ATOMIC);
	struct mc2_task *mp = tsk_rt(tsk)->mc2_data;
	enum crit_level lv;
	int i;
		
	TRACE_TASK(tsk, "MC2 admitting task\n");
	if (!tinfo)
		return -ENOMEM;

	if (!mp) {
		TRACE("mc2_admit_task: criticality level has not been set\n");
		return -ESRCH;
	}
	
	lv = mp->crit;

	if (lv < CRIT_LEVEL_C) {
		state = cpu_state_for(task_cpu(tsk));
		raw_spin_lock(&state->lock);
		local_irq_save(flags);
		//raw_spin_lock_irqsave(&state->lock, flags);
		
		tinfo->mc2_param.crit = mp->crit;
		tinfo->cpu = task_cpu(tsk);
		tinfo->has_departed = true;
		tinfo->mc2_param.res_id = mp->res_id;
		tinfo->mc2_param.mode_mask = mp->mode_mask;
		tinfo->mc2_param.init_finished = 0;
//		TRACE_TASK(tsk, "mode_mask = %x\n", mp->mode_mask);
		
//		TRACE_TASK(tsk, "Mode 0\n");
		res = sup_find_by_id(&(state->sup_env_modes[0]), mp->res_id);
	
		/* found the appropriate reservation */
		if (res) {
//			TRACE_TASK(tsk, "SUP FOUND RES ID in mode 0\n");			
			
			/* initial values */
			err = err? err:mc2_task_client_init(&tinfo->res_info[0], &tinfo->mc2_param, tsk, res);
		}
		else {
			//failed to find an expected reservation
			err = -ESRCH;
		}
			
		for(i = 1; i < NR_MODES; i++){
			if (!in_mode(tsk, i)){
				//task not present in mode
				continue;
			}
//			TRACE_TASK(tsk, "Mode %d\n",i);
			res = sup_find_by_id(&(state->sup_env_modes[i]), mp->res_id);
	
			/* found the appropriate reservation */
			if (res) {
	//			TRACE_TASK(tsk, "SUP FOUND RES ID in mode %d\n", i);			
				
				/* initial values */
				err = err? err:mc2_task_client_init(&tinfo->res_info[i], &tinfo->mc2_param, tsk, res);
			}
			else{
				//failed to find an expected reservation
				err = -ESRCH;
			}
		}

		if (!err){
			/* disable LITMUS^RT's per-thread budget enforcement */
			tsk_rt(tsk)->plugin_state = tinfo;
			tsk_rt(tsk)->task_params.budget_policy = NO_ENFORCEMENT;
		}

		if (is_mode_poll_task(tsk) && tinfo->cpu == 0) {
			cpu_0_task_exist = true;
		}
		raw_spin_lock(&global_lock);
		num_sync_released++;
		raw_spin_unlock(&global_lock);
		local_irq_restore(flags);
		raw_spin_unlock(&state->lock);
		//raw_spin_unlock_irqrestore(&state->lock, flags);
	} else if (lv == CRIT_LEVEL_C) {
//		TRACE_TASK(tsk, "Task being admitted is Level C\n");
		state = local_cpu_state();
		//raw_spin_lock_irqsave(&state->lock, flags);
		raw_spin_lock(&state->lock);
		local_irq_save(flags);
		//state = local_cpu_state();
		
		//raw_spin_lock(&state->lock);
	
		tinfo->mc2_param.crit = mp->crit;
		tinfo->cpu = -1;
		tinfo->has_departed = true;	
		tinfo->mc2_param.res_id = mp->res_id;
		tinfo->mc2_param.mode_mask = mp->mode_mask;
		tinfo->mc2_param.init_finished = 0;
		
	//	TRACE_TASK(tsk, "mode_mask = %x\n", mp->mode_mask);
		
//		TRACE_TASK(tsk, "Mode 0\n");

		raw_spin_lock(&global_lock);
		res = gmp_find_by_id(&(_global_env_modes[0]), mp->res_id);
	
		/* found the appropriate reservation */
		if (res) {
		//	TRACE_TASK(tsk, "GMP FOUND RES ID in mode 0\n");			
			
			/* initial values */
			err = err? err:mc2_task_client_init(&tinfo->res_info[0], &tinfo->mc2_param, tsk, res);
		}
		else {
			//failed to find an expected reservation
			err = -ESRCH;
		}
		
		for(i = 1; i < NR_MODES; i++){
			if (!in_mode(tsk, i)) 
				continue;
			res = gmp_find_by_id(&(_global_env_modes[i]), mp->res_id);

			/* found the appropriate reservation (or vCPU) */
			if (res) {
				TRACE_TASK(tsk, "GMP FOUND RES ID in mode %d\n", i);
				
				/* initial values */
				err = err? err:mc2_task_client_init(&tinfo->res_info[i], &tinfo->mc2_param, tsk, res);
	
			}
		}
		
		if (!err){
			/* disable LITMUS^RT's per-thread budget enforcement */
			tsk_rt(tsk)->plugin_state = tinfo;
			tsk_rt(tsk)->task_params.budget_policy = NO_ENFORCEMENT;
			raw_spin_lock(&mode_lock);
			for(i = 0; i < NR_MODES; i++){
				if (in_mode(tsk, i)){
					mode_sizes[i]++;
				}
			}
			raw_spin_unlock(&mode_lock);

		}
		num_sync_released++;
		raw_spin_unlock(&global_lock);
		//raw_spin_unlock_irqrestore(&state->lock, flags);	
		local_irq_restore(flags);
		raw_spin_unlock(&state->lock);
	}
	

	if (err)
		kfree(tinfo);

	TRACE_TASK(tsk, "MC2 task admitted %d\n", err);
	return err;
}

/* mc2_task_new - A new real-time job is arrived. Release the next job
 *                at the next reservation replenish time
 */
static void mc2_task_new(struct task_struct *tsk, int on_runqueue,
			  int is_running)
{
	unsigned long flags;
	struct mc2_task_state* tinfo = get_mc2_state(tsk);
	struct mc2_cpu_state *state; // = cpu_state_for(tinfo->cpu);
	struct reservation *res;
	enum crit_level lv = get_task_crit_level(tsk);
	lt_t release = 0;

	BUG_ON(lv < CRIT_LEVEL_A || lv > CRIT_LEVEL_C);

	TRACE_TASK(tsk, "new RT task %llu (on_rq:%d, running:%d)\n",
		   litmus_clock(), on_runqueue, is_running);

	if (tinfo->cpu == -1)
		state = local_cpu_state();
	else 
		state = cpu_state_for(tinfo->cpu);
	
	
	/* acquire the lock protecting the state and disable interrupts */
	//raw_spin_lock(&_global_env.lock);
	//raw_spin_lock(&state->lock);
	if (is_running) {
		state->scheduled = tsk;
		/* make sure this task should actually be running */
		litmus_reschedule_local();
	}
	
	local_irq_save(flags);
	raw_spin_lock(&state->lock);

	if (lv == CRIT_LEVEL_C) {
		raw_spin_lock(&global_lock);
		res = gmp_find_by_id(_global_env, tinfo->mc2_param.res_id);
	}
	else {
		res = sup_find_by_id(state->sup_env, tinfo->mc2_param.res_id);
	}

	//BUG_ON(!res);
	// the current mode doesn't have this task.
	// do not update timer and set the next release time.

	//res = res_find_by_id(state, tinfo->mc2_param.res_id);
	BUG_ON(!res);
	
	if (on_runqueue || is_running) {
		/* Assumption: litmus_clock() is synchronized across cores
		 * [see comment in pres_task_resume()] */
		if (lv == CRIT_LEVEL_C) {
			gmp_update_time(_global_env, litmus_clock());
			//raw_spin_unlock(&_global_env.lock);
		}
		else
			sup_update_time(state->sup_env, litmus_clock());
		//mc2_update_time(lv, state, litmus_clock());
		/* 9/20/2015 fix 
		mc2_update_ghost_state(state);
		*/
		task_arrives(state, tsk);
		if (lv == CRIT_LEVEL_C)
			raw_spin_unlock(&global_lock);
		/* NOTE: drops state->lock */
		TRACE("mc2_new()\n");
		
		mc2_update_timer_and_unlock(state);
	} else {
		if (lv == CRIT_LEVEL_C)
			raw_spin_unlock(&global_lock);
		raw_spin_unlock(&state->lock);
		//raw_spin_unlock(&_global_env.lock);
	}
	release = res->next_replenishment;
	local_irq_restore(flags);
	
	if (!release) {
		/*TRACE_TASK(tsk, "mc2_task_new() next_release = NULL\n");
		release = res->next_replenishment;
		TRACE_TASK(tsk, "mc2_task_new() next_release SET! = %llu\n", release);
		release_at(tsk, release);
		*/
		BUG();
	}
	else
		TRACE_TASK(tsk, "mc2_task_new() next_release = %llu\n", release);
}

/* mc2_reservation_destroy - reservation_destroy system call backend
 */
static long mc2_reservation_destroy(unsigned int reservation_id, int cpu)
{
	long ret = -EINVAL;
	struct mc2_cpu_state *state;
	struct reservation *res = NULL, *next;
	struct sup_reservation_environment *sup_env;
	int found = 0;
	//enum crit_level lv = get_task_crit_level(current);
	unsigned long flags;
	int i;
	
	if (cpu == -1) {
		struct next_timer_event *event, *e_next;
		local_irq_save(flags);
		raw_spin_lock(&global_lock);

		/* if the reservation is global reservation */
		//state = local_cpu_state();
		//delete reservation id in all modes
		for(i = 0; i < NR_MODES; i++) {
			//raw_spin_lock(&state->lock);
		
			list_for_each_entry_safe(res, next, &_global_env_modes[i].depleted_reservations, list) {
				if (res->id == reservation_id) {
					list_del(&res->list);
					kfree(res);
					found = 1;
					ret = 0;
				}
			}
			if (!found) {
				list_for_each_entry_safe(res, next, &_global_env_modes[i].inactive_reservations, list) {
					if (res->id == reservation_id) {
						list_del(&res->list);
						kfree(res);
						found = 1;
						ret = 0;
					}
				}
			}
			if (!found) {
				list_for_each_entry_safe(res, next, &_global_env_modes[i].active_reservations, list) {
					if (res->id == reservation_id) {
						list_del(&res->list);
						kfree(res);
						found = 1;
						ret = 0;
					}
				}
			}

			//raw_spin_unlock(&state->lock);
			list_for_each_entry_safe(event, e_next, &_global_env_modes[i].next_events, list) {
				if (event->id == reservation_id) {
					list_del(&event->list);
					TRACE("EVENT id %d deleted\n", event->id);
					kfree(event);
				} 
			}
		}
		
		raw_spin_unlock(&global_lock);
		local_irq_restore(flags);
	} else {
		/* if the reservation is partitioned reservation */
		state = cpu_state_for(cpu);
		for (i = 0; i < NR_MODES; i++){
			local_irq_save(flags);
			raw_spin_lock(&state->lock);
			
	//		res = sup_find_by_id(state->sup_env, reservation_id);
			sup_env = &(state->sup_env_modes[i]);
			list_for_each_entry_safe(res, next, &sup_env->depleted_reservations, list) {
				if (res->id == reservation_id) {
/*
					if (lv == CRIT_LEVEL_A) {
							struct table_driven_reservation *tdres;
							tdres = container_of(res, struct table_driven_reservation, res);
							kfree(tdres->intervals);
					}
*/
					list_del(&res->list);
					kfree(res);
					found = 1;
					ret = 0;
					TRACE_CUR("FOUND id %d mode %d\n",res->id, res->mode);
				}
			}
			if (!found) {
				list_for_each_entry_safe(res, next, &sup_env->inactive_reservations, list) {
					if (res->id == reservation_id) {
/*						if (lv == CRIT_LEVEL_A) {
							struct table_driven_reservation *tdres;
							tdres = container_of(res, struct table_driven_reservation, res);
							kfree(tdres->intervals);
						}
*/
						list_del(&res->list);
						kfree(res);
						found = 1;
						ret = 0;
						TRACE_CUR("FOUND id %d mode %d\n",res->id, res->mode);
					}
				}
			}
			if (!found) {
				list_for_each_entry_safe(res, next, &sup_env->active_reservations, list) {
					if (res->id == reservation_id) {
/*						if (lv == CRIT_LEVEL_A) {
							struct table_driven_reservation *tdres;
							tdres = container_of(res, struct table_driven_reservation, res);
							kfree(tdres->intervals);
						}
*/		
						list_del(&res->list);
						kfree(res);
						found = 1;
						ret = 0;
						TRACE_CUR("FOUND id %d mode %d\n",res->id, res->mode);
					}
				}
			}

			raw_spin_unlock(&state->lock);
			local_irq_restore(flags);
		}
	}
	
	TRACE("Rerservation destroyed ret = %d\n", ret);
	return ret;
}

/* mc2_task_exit - Task became a normal task (not real-time task)
 */
static void mc2_task_exit(struct task_struct *tsk)
{
	unsigned long flags;
	struct mc2_task_state* tinfo = get_mc2_state(tsk);
	struct mc2_cpu_state *state;
	enum crit_level lv = tinfo->mc2_param.crit;
	//struct crit_entry* ce;
	int cpu;
	int i;

	local_irq_save(flags);
	if (tinfo->cpu != -1)
		state = cpu_state_for(tinfo->cpu);
	else 
		state = local_cpu_state();
	
	raw_spin_lock(&state->lock);
	
	if (state->scheduled == tsk)
		state->scheduled = NULL;

	//ce = &state->crit_entries[lv];
	//if (ce->running == tsk)
	//	ce->running = NULL;
	
	/* remove from queues */
	if (is_running(tsk)) {
		/* Assumption: litmus_clock() is synchronized across cores
		 * [see comment in pres_task_resume()] */
		
		/* update both global and partitioned */
		if (lv < CRIT_LEVEL_C) {
			sup_update_time(state->sup_env, litmus_clock());
/*			raw_spin_lock(&global_lock);
			gmp_update_time(_global_env, litmus_clock());
			raw_spin_unlock(&global_lock);
*/		}
		else if (lv == CRIT_LEVEL_C) {
			raw_spin_lock(&global_lock);
			gmp_update_time(_global_env, litmus_clock());
			//raw_spin_unlock(&_global_env.lock);
		}
		/* 9/20/2015 fix 
		mc2_update_ghost_state(state);
		*/
		task_departs(tsk, 0);
		if (lv == CRIT_LEVEL_C)
			raw_spin_unlock(&global_lock);
		
		/* NOTE: drops state->lock */
		TRACE("mc2_exit()\n");

		raw_spin_lock(&global_lock);
		num_sync_released--;
		raw_spin_unlock(&global_lock);
		mc2_update_timer_and_unlock(state);	
	} else {
		raw_spin_unlock(&state->lock);
		
	}

	if (lv == CRIT_LEVEL_C) {
		raw_spin_lock(&global_lock);
		raw_spin_lock(&mode_lock);
		for(i = 0; i < NR_MODES; i++){
			if ( !(tsk_mc2_data(tsk)->mode_mask & (1<<i)) )
				continue;
			mode_sizes[i]--;
		}
		raw_spin_unlock(&mode_lock);
		raw_spin_unlock(&global_lock);

		for_each_online_cpu(cpu) {
			state = cpu_state_for(cpu);
			if (state == local_cpu_state())
				continue;
			raw_spin_lock(&state->lock);
			
			if (state->scheduled == tsk)
				state->scheduled = NULL;
			
			//ce = &state->crit_entries[lv];
			//if (ce->running == tsk)
			//	ce->running = NULL;
			
			raw_spin_unlock(&state->lock);
		}
	}
	
	local_irq_restore(flags);

	if (is_mode_poll_task(tsk) && (tinfo->cpu == 0))  {
		cpu_0_spin_flag = !cpu_0_spin_flag; // release other cpu before exit.
		cpu_0_task_exist = false;
	}
	
	kfree(tsk_rt(tsk)->plugin_state);
	tsk_rt(tsk)->plugin_state = NULL;
	kfree(tsk_rt(tsk)->mc2_data);
	tsk_rt(tsk)->mc2_data = NULL;
}

/* create_polling_reservation - create a new polling reservation
 */
static long create_polling_reservation(
	int res_type,
	struct reservation_config *config)
{
	struct mc2_cpu_state *state = NULL;
	//struct reservation* res = NULL;
	struct polling_reservation *pres;
	unsigned long flags;
	int use_edf  = config->priority == LITMUS_NO_PRIORITY;
	int periodic =  res_type == PERIODIC_POLLING;
	long err = -EINVAL;
	bool resExist = false;

	/* sanity checks */
	if (config->polling_params.budget >
	    config->polling_params.period) {
		printk(KERN_ERR "invalid polling reservation (%u): "   "budget > period\n", config->id);
		return -EINVAL;
	}
	if (config->polling_params.budget >
	    config->polling_params.relative_deadline
	    && config->polling_params.relative_deadline) {
		printk(KERN_ERR "invalid polling reservation (%u): "
		       "budget > deadline\n", config->id);
		return -EINVAL;
	}
	if (config->polling_params.offset >
	    config->polling_params.period) {
		printk(KERN_ERR "invalid polling reservation (%u): "
		       "offset > period\n", config->id);
		return -EINVAL;
	}
	//Added sanity check for mode
	if (config->mode < 0 || config->mode >= NR_MODES){
		printk(KERN_ERR "invalid polling reservation (%u): "
		       "Mode outside range\n", config->id);
		return -EINVAL;
	}

	/* Allocate before we grab a spin lock.
	 * Todo: would be nice to use a core-local allocation.
	 */
	pres = kzalloc(sizeof(*pres), GFP_KERNEL);
	if (!pres)
		return -ENOMEM;

	TRACE("CREATE_POLLING_RESERVATION id %d mode %d\n", config->id, config->mode);
	if (config->cpu != -1) {
		int i, is_exist = 0;
		//raw_spin_lock_irqsave(&_global_env.lock, flags);
		state = cpu_state_for(config->cpu);
		raw_spin_lock_irqsave(&state->lock, flags);
		
		/* check if it is the first creation of reservartion */
		for (i = 0; i < NR_MODES; i++) {
			if( sup_find_by_id(&(state->sup_env_modes[i]), config->id) )
				is_exist = 1;
		}
		if (!is_exist && config->mode != 0) {
			/* create mode 0 reservation first */
			struct polling_reservation *pres_0 = kzalloc(sizeof(*pres_0), GFP_ATOMIC);
			
			TRACE_CUR("The first mode_num = %d\n",config->mode);
			
			if (!pres_0) {
				raw_spin_unlock_irqrestore(&state->lock, flags);
				kfree(pres);
				return -ENOMEM;
			}
			polling_reservation_init(pres_0, use_edf, periodic,
				config->polling_params.budget,
				config->polling_params.period,
				config->polling_params.relative_deadline,
				config->polling_params.offset);
			pres_0->res.id = config->id;
			pres_0->res.blocked_by_ghost = 0;
			pres_0->res.is_ghost = NO_CPU;
			pres_0->res.mode = config->mode;

			if (!use_edf)
				pres_0->res.priority = config->priority;
			sup_add_new_reservation(&(state->sup_env_modes[0]), &pres_0->res);
			TRACE_CUR("SUP reservation created R%d for mode 0 priority : %llu\n", config->id, pres_0->res.priority);
			pres_0->res.reported = 0;
			pres_0->res.tsk = current;
		}

		//force reservation id unique inside of res_config->mode
		if( sup_find_by_id(&(state->sup_env_modes[config->mode]), config->id) ){
			resExist = true;
		}
		if (!resExist) {
			polling_reservation_init(pres, use_edf, periodic,
				config->polling_params.budget,
				config->polling_params.period,
				config->polling_params.relative_deadline,
				config->polling_params.offset);
			pres->res.id = config->id;
			pres->res.blocked_by_ghost = 0;
			pres->res.is_ghost = NO_CPU;
			pres->res.mode = config->mode;
			/*if (config->priority == LITMUS_MAX_PRIORITY) {
				level_a_priorities[config->cpu]++;
				pres->res.priority = level_a_priorities[config->cpu];
			}*/
			if (!use_edf)
				pres->res.priority = config->priority;
			sup_add_new_reservation(&(state->sup_env_modes[config->mode]), &pres->res);
			err = config->id;
			TRACE_CUR("SUP reservation created R%d for mode %d priority : %llu\n", config->id, config->mode, pres->res.priority);
		} else {
			err = -EEXIST;
		}

		raw_spin_unlock_irqrestore(&state->lock, flags);
		//raw_spin_unlock_irqrestore(&_global_env.lock, flags);

	} else {
		int i, is_exist = 0;
		raw_spin_lock_irqsave(&global_lock, flags);
		
		/* check if it is the first creation of reservartion */
		for (i = 0; i < NR_MODES; i++) {
			if(gmp_find_by_id(&(_global_env_modes[i]), config->id))
				is_exist = 1;
		}
		if (!is_exist && config->mode != 0) {
			/* create mode 0 reservation first */
			struct polling_reservation *pres_0 = kzalloc(sizeof(*pres_0), GFP_ATOMIC);
			
			TRACE_CUR("The first mode_num = %d\n",config->mode);
			
			if (!pres_0) {
				raw_spin_unlock_irqrestore(&global_lock, flags);
				kfree(pres);
				return -ENOMEM;
			}
			polling_reservation_init(pres_0, use_edf, periodic,
				config->polling_params.budget,
				config->polling_params.period,
				config->polling_params.relative_deadline,
				config->polling_params.offset);
			pres_0->res.id = config->id;
			pres_0->res.blocked_by_ghost = 0;
			pres_0->res.scheduled_on = NO_CPU;
			pres_0->res.is_ghost = NO_CPU;
			pres_0->res.mode = config->mode;

			if (!use_edf)
				pres_0->res.priority = config->priority;
			gmp_add_new_reservation(&(_global_env_modes[0]), &pres_0->res);
			TRACE_CUR("GMP reservation created R%d for mode 0 priority : %llu\n", config->id, pres_0->res.priority);
			pres_0->res.reported = 0;
			pres_0->res.tsk = current;
		}

		
		//force id's unique within desired mode
		if (gmp_find_by_id(&(_global_env_modes[config->mode]), config->id)){
			resExist = true;
		}
		if (!resExist) {
			polling_reservation_init(pres, use_edf, periodic,
				config->polling_params.budget,
				config->polling_params.period,
				config->polling_params.relative_deadline,
				config->polling_params.offset);
			pres->res.id = config->id;
			pres->res.blocked_by_ghost = 0;
			pres->res.scheduled_on = NO_CPU;
			pres->res.is_ghost = NO_CPU;
			pres->res.mode = config->mode;
			if (!use_edf)
				pres->res.priority = config->priority;
			gmp_add_new_reservation(&(_global_env_modes[config->mode]), &pres->res);
			TRACE_CUR("GMP reservation created R%d for mode %d priority : %llu\n", config->id, config->mode, pres->res.priority);
			err = config->id;
		} else {
			err = -EEXIST;
		}
		raw_spin_unlock_irqrestore(&global_lock, flags);		
	}
	

	pres->res.reported = 0;
	pres->res.tsk = current;

	if (err < 0)
		kfree(pres);

	return err;
}

#define MAX_INTERVALS 1024

/* create_table_driven_reservation - create a table_driven reservation
 */
static long create_table_driven_reservation(
	struct reservation_config *config)
{
	struct mc2_cpu_state *state;
	//struct reservation* res = NULL;
	struct table_driven_reservation *td_res = NULL;
	struct lt_interval *slots = NULL;
	size_t slots_size;
	unsigned int i, num_slots;
	unsigned long flags;
	long err = -EINVAL;
	bool resExist = false;

	if (!config->table_driven_params.num_intervals) {
		printk(KERN_ERR "invalid table-driven reservation (%u): "
		       "no intervals\n", config->id);
		return -EINVAL;
	}

	if (config->table_driven_params.num_intervals > MAX_INTERVALS) {
		printk(KERN_ERR "invalid table-driven reservation (%u): "
		       "too many intervals (max: %d)\n", config->id, MAX_INTERVALS);
		return -EINVAL;
	}

	if (config->mode >= NR_MODES || config->mode < 0){
		printk(KERN_ERR "invalid table-driven reservation (%u): "
		       "mode outside of range\n", config->id);
		return -EINVAL;
	}

	num_slots = config->table_driven_params.num_intervals;
	slots_size = sizeof(slots[0]) * num_slots;
	slots = kzalloc(slots_size, GFP_KERNEL);
	if (!slots)
		return -ENOMEM;

	td_res = kzalloc(sizeof(*td_res), GFP_KERNEL);
	if (!td_res)
		err = -ENOMEM;
	else
		err = copy_from_user(slots,
			config->table_driven_params.intervals, slots_size);

	if (!err) {
		/* sanity checks */
		for (i = 0; !err && i < num_slots; i++)
			if (slots[i].end <= slots[i].start) {
				printk(KERN_ERR
				       "invalid table-driven reservation (%u): "
				       "invalid interval %u => [%llu, %llu]\n",
				       config->id, i,
				       slots[i].start, slots[i].end);
				err = -EINVAL;
			}

		for (i = 0; !err && i + 1 < num_slots; i++)
			if (slots[i + 1].start <= slots[i].end) {
				printk(KERN_ERR
				       "invalid table-driven reservation (%u): "
				       "overlapping intervals %u, %u\n",
				       config->id, i, i + 1);
				err = -EINVAL;
			}

		if (slots[num_slots - 1].end >
			config->table_driven_params.major_cycle_length) {
			printk(KERN_ERR
				"invalid table-driven reservation (%u): last "
				"interval ends past major cycle %llu > %llu\n",
				config->id,
				slots[num_slots - 1].end,
				config->table_driven_params.major_cycle_length);
			err = -EINVAL;
		}
	}

	if (!err) {
		state = cpu_state_for(config->cpu);
		raw_spin_lock_irqsave(&state->lock, flags);
		
		//force unique id's across all modes
		for(i = 0; i < NR_MODES; i++){
			if (sup_find_by_id(&(state->sup_env_modes[i]), config->id)){
				resExist = true;
				break;
			}
		}
		if (!resExist) {
			table_driven_reservation_init(td_res,
				config->table_driven_params.major_cycle_length,
				slots, num_slots);
			td_res->res.id = config->id;
			td_res->res.priority = config->priority;
			td_res->res.blocked_by_ghost = 0;
			td_res->res.mode = config->mode;
			sup_add_new_reservation(&(state->sup_env_modes[config->mode]), &td_res->res);
			err = config->id;
		} else {
			err = -EEXIST;
		}

		raw_spin_unlock_irqrestore(&state->lock, flags);
	}

	td_res->res.reported = 0;
	td_res->res.tsk = current;
	
	if (err < 0) {
		kfree(slots);
		kfree(td_res);
	}

	return err;
}

/* mc2_reservation_create - reservation_create system call backend
 */
static long mc2_reservation_create(int res_type, void* __user _config)
{
	long ret = -EINVAL;
	struct reservation_config config;

	TRACE("Attempt to create reservation (%d)\n", res_type);
	
	if (copy_from_user(&config, _config, sizeof(config)))
		return -EFAULT;

	TRACE("Attempt to create reservation id %d mode %d\n", config.id, config.mode);

	if (config.cpu != -1) {
		if (config.cpu < 0 || !cpu_online(config.cpu)) {
			printk(KERN_ERR "invalid polling reservation (%u): "
				   "CPU %d offline\n", config.id, config.cpu);
			return -EINVAL;
		}
	}

	switch (res_type) {
		case PERIODIC_POLLING:
		case SPORADIC_POLLING:
			ret = create_polling_reservation(res_type, &config);
			break;

		case TABLE_DRIVEN:
			ret = create_table_driven_reservation(&config);
			break;

		default:
			return -EINVAL;
	};

	return ret;
}

static struct domain_proc_info mc2_domain_proc_info;

static long mc2_get_domain_proc_info(struct domain_proc_info **ret)
{
	*ret = &mc2_domain_proc_info;
	return 0;
}

static void mc2_setup_domain_proc(void)
{
	int i, cpu;
	int num_rt_cpus = num_online_cpus();

	struct cd_mapping *cpu_map, *domain_map;

	memset(&mc2_domain_proc_info, sizeof(mc2_domain_proc_info), 0);
	init_domain_proc_info(&mc2_domain_proc_info, num_rt_cpus, num_rt_cpus);
	mc2_domain_proc_info.num_cpus = num_rt_cpus;
	mc2_domain_proc_info.num_domains = num_rt_cpus;

	i = 0;
	for_each_online_cpu(cpu) {
		cpu_map = &mc2_domain_proc_info.cpu_to_domains[i];
		domain_map = &mc2_domain_proc_info.domain_to_cpus[i];

		cpu_map->id = cpu;
		domain_map->id = i;
		cpumask_set_cpu(i, cpu_map->mask);
		cpumask_set_cpu(cpu, domain_map->mask);
		++i;
	}
}

static long mc2_activate_plugin(void)
{
	int cpu;//, lv;
	struct mc2_cpu_state *state;
	struct cpu_entry *ce;
	int i;
	
	for(i = 0; i < NR_MODES; i++){
		gmp_init(&(_global_env_modes[i]));
	}
	_global_env = &_global_env_modes[0];
	
	//raw_spin_lock_init(&_lowest_prio_cpu.lock);
	raw_spin_lock_init(&mode_lock);
	raw_spin_lock_init(&global_lock);

	seen_once = false;

	for_each_online_cpu(cpu) {
		TRACE("Initializing CPU%d...\n", cpu);

		resched_cpu[cpu] = 0;
		//level_a_priorities[cpu] = 0;
		this_cpu_write(mode_counter, 0);
		
		state = cpu_state_for(cpu);
		ce = &_lowest_prio_cpu.cpu_entries[cpu];
		
		ce->cpu = cpu;
		ce->scheduled = NULL;
		ce->deadline = ULLONG_MAX;
		ce->lv = NUM_CRIT_LEVELS;
		ce->will_schedule = false;

		raw_spin_lock_init(&state->lock);
		state->cpu = cpu;
		state->scheduled = NULL;
		//for (lv = 0; lv < NUM_CRIT_LEVELS; lv++) {
		//	struct crit_entry *cr_entry = &state->crit_entries[lv];
		//	cr_entry->level = lv;
		//	cr_entry->running = NULL;
		//}
		for(i = 0; i < NR_MODES; i++){
			sup_init(&(state->sup_env_modes[i]));
		}
		state->sup_env = &(state->sup_env_modes[0]);

		hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
		state->timer.function = on_scheduling_timer;
		state->spin_flag = false;
	}

	mc2_setup_domain_proc();

	mode = 0;
	requested_mode = 0;

	for(i = 0; i < NR_MODES; i++){
		mode_sizes[i] = 0;
	}
	res_reported = 0;
	cpu_0_spin_flag = false;
	cpu_0_task_exist = false;

	return 0;
}

static void mc2_finish_switch(struct task_struct *prev)
{
	int cpus;
	enum crit_level lv = get_task_crit_level(prev);
	struct mc2_cpu_state *state = local_cpu_state();
	
	state->scheduled = is_realtime(current) ? current : NULL;
	if (lv == CRIT_LEVEL_C) {
		for (cpus = 0; cpus<NR_CPUS; cpus++) {
			if (resched_cpu[cpus]) {
				litmus_reschedule(cpus);
			}
		}
	}
}

static long mc2_deactivate_plugin(void)
{
	int cpu;
	struct mc2_cpu_state *state;
	struct reservation *res;
	struct next_timer_event *event;
	struct cpu_entry *ce;
	int i;

	for_each_online_cpu(cpu) {
		state = cpu_state_for(cpu);
		raw_spin_lock(&state->lock);

		hrtimer_cancel(&state->timer);

		ce = &_lowest_prio_cpu.cpu_entries[cpu];
		
		ce->cpu = cpu;
		ce->scheduled = NULL;
		ce->deadline = ULLONG_MAX;
		ce->lv = NUM_CRIT_LEVELS;
		ce->will_schedule = false;


		for(i = 0; i < NR_MODES; i++){
			/* Delete all reservations --- assumes struct reservation
			 * is prefix of containing struct. */
			state->sup_env = &(state->sup_env_modes[i]);
			while (!list_empty(&state->sup_env->active_reservations)) {
				res = list_first_entry(
					&state->sup_env->active_reservations,
				        struct reservation, list);
				list_del(&res->list);
				kfree(res);
			}

			while (!list_empty(&state->sup_env->inactive_reservations)) {
				res = list_first_entry(
					&state->sup_env->inactive_reservations,
				        struct reservation, list);
				list_del(&res->list);
				kfree(res);
			}

			while (!list_empty(&state->sup_env->depleted_reservations)) {
				res = list_first_entry(
					&state->sup_env->depleted_reservations,
				        struct reservation, list);
				list_del(&res->list);
				kfree(res);
			}
		}

		raw_spin_unlock(&state->lock);
	}

	raw_spin_lock(&global_lock);
	for(i = 0; i < NR_MODES; i++){
		_global_env = &_global_env_modes[i];
		while (!list_empty(&_global_env->active_reservations)) {
			res = list_first_entry(
				&_global_env->active_reservations,
					struct reservation, list);
			list_del(&res->list);
			kfree(res);
		}
	
		while (!list_empty(&_global_env->inactive_reservations)) {
			res = list_first_entry(
				&_global_env->inactive_reservations,
					struct reservation, list);
			list_del(&res->list);
			kfree(res);
		}

		while (!list_empty(&_global_env->depleted_reservations)) {
			res = list_first_entry(
				&_global_env->depleted_reservations,
					struct reservation, list);
			list_del(&res->list);
			kfree(res);
		}
	
		while (!list_empty(&_global_env->next_events)) {
			event = list_first_entry(
				&_global_env->next_events,
					struct next_timer_event, list);
			list_del(&event->list);
			kfree(event);
		}
	
	}
	num_sync_released = 0;
	raw_spin_unlock(&global_lock);
	destroy_domain_proc_info(&mc2_domain_proc_info);
	return 0;
}

static struct sched_plugin mc2_plugin = {
	.plugin_name			= "MC2",
	.schedule				= mc2_schedule,
	.finish_switch			= mc2_finish_switch,
	.task_wake_up			= mc2_task_resume,
	.admit_task				= mc2_admit_task,
	.task_new				= mc2_task_new,
	.task_exit				= mc2_task_exit,
	.complete_job           = mc2_complete_job,
	.get_domain_proc_info   = mc2_get_domain_proc_info,
	.activate_plugin		= mc2_activate_plugin,
	.deactivate_plugin      = mc2_deactivate_plugin,
	.reservation_create     = mc2_reservation_create,
	.reservation_destroy	= mc2_reservation_destroy,
};

static int __init init_mc2(void)
{
	return register_sched_plugin(&mc2_plugin);
}

module_init(init_mc2);