aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mvp/mvpkm/mvpkm_main.c
blob: d32a4c1bc757fba62363e97549e820ab9f0c1a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
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
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
/*
 * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
 *
 * Copyright (C) 2010-2012 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING.  If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#line 5

/**
 * @file
 *
 * @brief The kernel level driver.
 */

#define __KERNEL_SYSCALLS__
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/syscalls.h>
#include <linux/kmod.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/miscdevice.h>
#include <linux/poll.h>
#include <linux/smp.h>
#include <linux/capability.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/sysfs.h>
#include <linux/pid.h>
#include <linux/highmem.h>
#include <linux/syscalls.h>

#ifdef CONFIG_HAS_WAKELOCK
#include <linux/wakelock.h>
#endif

#include <net/sock.h>

#include <asm/cacheflush.h>
#include <asm/memory.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#include "mvp.h"
#include "mvp_version.h"
#include "mvpkm_types.h"
#include "mvpkm_private.h"
#include "mvpkm_kernel.h"
#include "actions.h"
#include "wscalls.h"
#include "arm_inline.h"
#include "tsc.h"
#include "mksck_kernel.h"
#include "mmu_types.h"
#include "mvp_timer.h"
#include "qp.h"
#include "qp_host_kernel.h"
#include "cpufreq_kernel.h"
#include "mvpkm_comm_ev.h"
#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER
#include "mvp_balloon.h"
#endif


/*********************************************************************
 *
 * Definition of the file operations
 *
 *********************************************************************/
static _Bool LockedListAdd(MvpkmVM *vm,
                           __u32 mpn,
                           __u32 order,
                           PhysMem_RegionType forRegion);
static _Bool LockedListDel(MvpkmVM *vm, __u32 mpn);
static void  LockedListUnlockAll(MvpkmVM *vm);
static _Bool LockedListLookup(MvpkmVM *vm, __u32 mpn);
static int   SetupMonitor(MvpkmVM *vm);
static int   RunMonitor(MvpkmVM *vm);
static MPN   AllocZeroedFreePages(MvpkmVM *vm,
                                  uint32 order,
                                  _Bool highmem,
                                  PhysMem_RegionType forRegion,
                                  HKVA *hkvaRet);
static HKVA  MapWSPHKVA(MvpkmVM *vm, HkvaMapInfo *mapInfo);
static void  UnmapWSPHKVA(MvpkmVM *vm);
static int   MvpkmWaitForInt(MvpkmVM *vm, _Bool suspend);
static void  ReleaseVM(MvpkmVM *vm);

/*
 * Mksck open request must come from this uid. It must be root until
 * it is set via an ioctl from mvpd.
 */
uid_t Mvpkm_vmwareUid = 0;
EXPORT_SYMBOL(Mvpkm_vmwareUid);

/*
 * Minimum hidden app oom_adj, provided by mvpd, since we can't get it directly
 * from the lowmemorykiller module.
 */
static int minHiddenAppOOMAdj;

/*
 * vCPU cpu affinity to let monitor/guest run on some CPUs only (when possible)
 */
static DECLARE_BITMAP(vcpuAffinity, NR_CPUS);

/*********************************************************************
 *
 * Sysfs nodes
 *
 *********************************************************************/
/*
 * kobject for our sysfs representation, used for global nodes.
 */
static struct kobject *mvpkmKObj;

/*
 * kobject for the balloon exports.
 */
static struct kobject *balloonKObj;

/**
 * @brief sysfs show function for global version attribute.
 *
 * @param kobj reference to kobj nested in MvpkmVM struct.
 * @param attr kobj_attribute reference, not used.
 * @param buf PAGE_SIZEd buffer to write to.
 *
 * @return number of characters printed (not including trailing null character).
 */
static ssize_t
version_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
   return snprintf(buf, PAGE_SIZE, MVP_VERSION_FORMATSTR "\n", MVP_VERSION_FORMATARGS);
}

static struct kobj_attribute versionAttr = __ATTR_RO(version);

/**
 * @brief sysfs show function for global background_pages attribute.
 *
 * Used by vmx balloon policy controller to gauge the amount of freeable
 * anonymous memory.
 *
 * @param kobj reference to kobj nested in MvpkmVM struct.
 * @param attr kobj_attribute reference, not used.
 * @param buf PAGE_SIZEd buffer to write to.
 *
 * @return number of characters printed (not including trailing null character).
 */
static ssize_t
background_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
#ifndef CONFIG_ANDROID_LOW_MEMORY_KILLER
   return snprintf(buf, PAGE_SIZE, "0\n");
#else
   return snprintf(buf, PAGE_SIZE, "%d\n", Balloon_AndroidBackgroundPages(minHiddenAppOOMAdj));
#endif
}

static struct kobj_attribute backgroundAttr = __ATTR_RO(background);

/**
 * @brief sysfs show function to export the other_file calculation in
 *        lowmemorykiller.
 *
 * It's helpful, in the balloon controller, to know what the lowmemorykiller
 * module is using to know when the system has crossed a minfree threshold.
 * Since there exists a number of different other_file calculations in various
 * lowmemorykiller patches (@see{MVP-1674}), and the module itself doesn't
 * provide a clean export of this figure, we provide it on a case-by-case basis
 * for the various supported hosts here.
 *
 * @param kobj reference to kobj nested in MvpkmVM struct.
 * @param attr kobj_attribute reference, not used.
 * @param buf PAGE_SIZEd buffer to write to.
 *
 * @return number of characters printed (not including trailing null character).
 */
static ssize_t
other_file_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
   int32 other_file = 0;

#ifndef LOWMEMKILLER_VARIANT
#define LOWMEMKILLER_VARIANT 0
#endif

#ifndef LOWMEMKILLER_MD5
#define LOWMEMKILLER_MD5 0
#endif

#ifndef LOWMEMKILLER_SHRINK_MD5
#define LOWMEMKILLER_SHRINK_MD5 0
#endif

   /*
    * The build system hashes the lowmemorykiller section related to the
    * other_file calculation in the kernel source for us, here we have to
    * provide the code.
    */
#if LOWMEMKILLER_VARIANT == 1
   /*
    * This is the same as the non-exported global_reclaimable_pages() when there
    * is no swap.
    */
   other_file = global_page_state(NR_ACTIVE_FILE) +
      global_page_state(NR_INACTIVE_FILE);
#elif LOWMEMKILLER_VARIANT == 2
   other_file = global_page_state(NR_FILE_PAGES);
#elif LOWMEMKILLER_VARIANT == 3
   other_file = global_page_state(NR_FILE_PAGES) - global_page_state(NR_SHMEM);
#elif LOWMEMKILLER_VARIANT == 4
   /*
    * Here free/file pages are fungible and max(free, file) isn't used, but we
    * can continue to use max(free, file) since max(free, file) = other_file in
    * this case.
    */
   other_file = global_page_state(NR_FREE_PAGES) + global_page_state(NR_FILE_PAGES);
#elif defined(NONANDROID)
   /*
    * Non-Android host platforms don't have ballooning enabled.
    */
#else
   /*
    * If you get this message, you need to run 'make lowmem-info' and inspect
    * lowmemorykiller.c. If the "other_file = ..." calculation in lowmem_shrink
    * appears above, simply add the "Shrink#" to an existing entry in
    * lowmemkiller-variant.sh, pointing to the variant number above. Otherwise,
    * provide a new entry above and variant number, with the appropriate
    * other_file calculation and update lowmemkiller-variant.sh accordingly.
    */
//#warning "Unknown lowmemorykiller variant in hosted/module/mvpkm_main.c, falling back on default (see other_file_show for the remedy)"
   /*
    * Fall back on default - this may bias strangely for/against the host, but
    * nothing catastrophic should result.
    */
   other_file = global_page_state(NR_FILE_PAGES);
#endif

#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
   return snprintf(buf,
                   PAGE_SIZE,
                   "%d %d %s %s\n",
                   other_file,
                   LOWMEMKILLER_VARIANT,
                   STRINGIFY(LOWMEMKILLER_MD5),
                   STRINGIFY(LOWMEMKILLER_SHRINK_MD5));
#undef _STRINGIFY
#undef STRINGIFY
}

static struct kobj_attribute otherFileAttr = __ATTR_RO(other_file);

/*
 * kset for our sysfs representation, used for per-VM nodes.
 */
static struct kset *mvpkmKSet;

static ssize_t MvpkmAttrShow(struct kobject *kobj,
                             struct attribute *attr,
                             char *buf);
static ssize_t MvpkmAttrStore(struct kobject *kobj,
                              struct attribute *attr,
                              const char *buf,
                              size_t count);

static void MvpkmKObjRelease(struct kobject *kobj)
   __attribute__ ((optimize ("-fomit-frame-pointer")));


/**
 * @brief Releases the vm structure containing the kobject.
 *
 * @param kobj the vm's kobject.
 */

static void
MvpkmKObjRelease(struct kobject *kobj)
{
   MvpkmVM *vm = container_of(kobj, MvpkmVM, kobj);

   ReleaseVM(vm);

   module_put(THIS_MODULE);
}


/**
 * @name mvpkm ktype attribute structures for locked_pages.
 *
 * @{
 */
static struct sysfs_ops mvpkmSysfsOps = {
   .show = MvpkmAttrShow,
   .store = MvpkmAttrStore
};

static struct attribute mvpkmLockedPagesAttr = {
   .name = "locked_pages",
   .mode = 0444,
};

static struct attribute mvpkmBalloonWatchdogAttr = {
   .name = "balloon_watchdog",
   .mode = 0666
};

static struct attribute mvpkmMonitorAttr = {
   .name = "monitor",
   .mode = 0400,
};

static struct attribute *mvpkmDefaultAttrs[] = {
   &mvpkmLockedPagesAttr,
   &mvpkmBalloonWatchdogAttr,
   &mvpkmMonitorAttr,
   NULL,
};

static struct kobj_type mvpkmKType = {
   .sysfs_ops = &mvpkmSysfsOps,
   .release = MvpkmKObjRelease,
   .default_attrs = mvpkmDefaultAttrs,
};
/*@}*/

/*
 * As it is not very common for host kernels to have SYS_HYPERVISOR enabled and
 * you have to "hack" a Kconfig file to enable it, just include the
 * functionality inline if it is not enabled.
 */
#ifndef CONFIG_SYS_HYPERVISOR
struct kobject *hypervisor_kobj;
EXPORT_SYMBOL_GPL(hypervisor_kobj);
#endif


/*
 * kobject and kset utilities.
 */

extern struct kobject *kset_find_obj(struct kset *, const char *)
   __attribute__((weak));


/**
 * @brief Finds a kobject in a kset. The actual implementation is copied from
 *    kernel source in lib/kobject.c. Although the symbol is extern-declared,
 *    it is not EXPORT_SYMBOL-ed. We use a weak reference in case the symbol
 *    might be exported in future kernel versions.
 *
 * @param kset set to search.
 * @param name object name.
 *
 * @return retained kobject if found, NULL otherwise.
 */

struct kobject *
kset_find_obj(struct kset *kset,
              const char *name)
{
   struct kobject *k;
   struct kobject *ret = NULL;

   spin_lock(&kset->list_lock);
   list_for_each_entry(k, &kset->list, entry) {
      if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
         ret = kobject_get(k);
         break;
     }
   }
   spin_unlock(&kset->list_lock);
   return ret;
}


/**
 * @brief Finds one of the VM's pre-defined ksets.
 *
 * @param vmID a VM ID.
 * @param name name of one of the VM's pre-defined ksets.
 *
 * @return retained kset if found, NULL otherwise.
 */

struct kset *
Mvpkm_FindVMNamedKSet(int vmID,
                      const char *name)
{
   MvpkmVM *vm;
   struct kobject *kobj;
   char vmName[32] = {}; /* Large enough to hold externally-formatted int32. */
   struct kset *res = NULL;

   if (!mvpkmKSet) {
      return NULL;
   }

   snprintf(vmName, sizeof vmName, "%d", vmID);
   vmName[sizeof vmName - 1] = '\0'; /* Always null-terminate, no overflow. */

   kobj = kset_find_obj(mvpkmKSet, vmName);
   if (!kobj) {
      return NULL;
   }

   vm = container_of(kobj, MvpkmVM, kobj);

   if (!strcmp(name, "devices")) {
      res = kset_get(vm->devicesKSet);
   } else if (!strcmp(name, "misc")) {
      res = kset_get(vm->miscKSet);
   }

   kobject_put(kobj);
   return res;
}

EXPORT_SYMBOL(Mvpkm_FindVMNamedKSet);



/*********************************************************************
 *
 * Standard Linux miscellaneous device registration
 *
 *********************************************************************/

MODULE_LICENSE("GPL"); // for kallsyms_lookup_name

static int MvpkmFault(struct vm_area_struct *vma, struct vm_fault *vmf);


/**
 * @brief Linux vma operations for /dev/mem-like kernel module mmap. We
 *        enforce the restriction that only MPNs that have been allocated
 *        to the opened VM may be mapped and also increment the reference
 *        count (via vm_insert_page), so that even if the memory is later
 *        freed by the VM, host process vma's containing the MPN can't
 *        compromise the system.
 *
 *        However, only trusted host processes (e.g. the vmx) should be allowed
 *        to use this interface, since you can mmap the monitor's code/data/
 *        page tables etc. with it. Untrusted host processes are limited to
 *        typed messages for sharing memory with the monitor. Unix file system
 *        access permissions are the intended method of restricting access.
 *        Unfortunately, today _any_ host process utilizing Mksck requires
 *        access to mvpkm to setup its Mksck pages and obtain socket info via
 *        ioctls - we probably should be exporting two devices, one for trusted
 *        and one for arbitrary host processes to avoid this confusion of
 *        concerns.
 */
static struct vm_operations_struct mvpkmVMOps = {
   .fault = MvpkmFault
};

/*
 * Generic kernel module file ops. These functions will be registered
 * at the time the kernel module is loaded.
 */
static long MvpkmUnlockedIoctl(struct file *filep,
                               unsigned int cmd,
                               unsigned long arg);
static int MvpkmOpen(struct inode *inode, struct file *filp);
static int MvpkmRelease(struct inode *inode, struct file *filp);
static int MvpkmMMap(struct file *file, struct vm_area_struct *vma);

/**
 * @brief the file_operation structure contains the callback functions
 *        that are registered with Linux to handle file operations on
 *        the mvpkm device.
 *
 *        The structure contains other members that the mvpkm device
 *        does not use. Those members are auto-initialized to NULL.
 *
 *        WARNING, this structure has changed after Linux kernel 2.6.19:
 *        readv/writev are changed to aio_read/aio_write (neither is used here).
 */
static const struct file_operations mvpkmFileOps = {
   .owner            = THIS_MODULE,
   .unlocked_ioctl   = MvpkmUnlockedIoctl,
   .open             = MvpkmOpen,
   .release          = MvpkmRelease,
   .mmap             = MvpkmMMap
};

/**
 * @brief The mvpkm device identifying information to be used to register
 *        the device with the Linux kernel.
 */
static struct miscdevice mvpkmDev = {
   .minor  = 165,
   .name   = "mvpkm",
   .fops   = &mvpkmFileOps
};

/**
 * Mvpkm is loaded by mvpd and only mvpd will be allowed to open
 * it. There is a very simple way to verify that: record the process
 * id (thread group id) at the time the module is loaded and test it
 * at the time the module is opened.
 */
static struct pid *initTgid;


#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER
/**
 * @name Slab shrinker for triggering balloon adjustment.
 *
 * @note shrinker us used as a trigger for guest balloon.
 *
 * @{
 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
static int MvpkmShrink(struct shrinker *this, struct shrink_control *sc);
#else
static int MvpkmShrink(struct shrinker *this, int nrToScan, gfp_t gfpMask);
#endif

static struct shrinker mvpkmShrinker = {
   .shrink = MvpkmShrink,
   .seeks = DEFAULT_SEEKS
};
/*@}*/
#endif

module_param_array(vcpuAffinity, ulong, NULL, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(vcpuAffinity, "vCPU affinity");


/**
 * @brief Initialize the mvpkm device, register it with the Linux kernel.
 *
 * @return A zero is returned on success and a negative errno code for failure.
 *         (Same as the return policy of misc_register(9).)
 */

static int __init
MvpkmInit(void)
{
   int err = 0;
   _Bool mksckInited = false;
   _Bool cpuFreqInited = false;

   printk(KERN_INFO "Mvpkm: " MVP_VERSION_FORMATSTR "\n", MVP_VERSION_FORMATARGS);
   printk(KERN_INFO "Mvpkm: loaded from process %s tgid=%d, pid=%d\n",
          current->comm,
          task_tgid_vnr(current),
          task_pid_vnr(current));

   if (bitmap_empty(vcpuAffinity, NR_CPUS)) {
      bitmap_copy(vcpuAffinity, cpumask_bits(cpu_possible_mask), NR_CPUS);
   }

   if ((err = misc_register(&mvpkmDev))) {
      return -ENOENT;
   }

   if ((err = Mksck_Init())) {
      goto error;
   } else {
      mksckInited = true;
   }

   QP_HostInit();

   CpuFreq_Init();
   cpuFreqInited = true;

   /*
    * Reference mvpd (module loader) tgid struct, so that we can avoid
    * attacks based on pid number wraparound.
    */
   initTgid = get_pid(task_tgid(current));

#ifndef CONFIG_SYS_HYPERVISOR
   hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);
   if (!hypervisor_kobj) {
      err = -ENOMEM;
      goto error;
   }
#endif

   if (!(mvpkmKObj = kobject_create_and_add("mvp", hypervisor_kobj)) ||
       !(balloonKObj = kobject_create_and_add("lowmem", mvpkmKObj)) ||
       !(mvpkmKSet = kset_create_and_add("vm", NULL, mvpkmKObj))) {
      err = -ENOMEM;
      goto error;
   }

   if ((err = sysfs_create_file(mvpkmKObj, &versionAttr.attr))) {
      goto error;
   }

   if ((err = sysfs_create_file(balloonKObj, &backgroundAttr.attr))) {
      goto error;
   }

   if ((err = sysfs_create_file(balloonKObj, &otherFileAttr.attr))) {
      goto error;
   }

#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER
   register_shrinker(&mvpkmShrinker);
#endif

   MksckPageInfo_Init();

   return 0;

error:
   if (mvpkmKSet) {
      kset_unregister(mvpkmKSet);
   }

   if (balloonKObj) {
      kobject_del(balloonKObj);
      kobject_put(balloonKObj);
   }

   if (mvpkmKObj) {
      kobject_del(mvpkmKObj);
      kobject_put(mvpkmKObj);
   }

#ifndef CONFIG_SYS_HYPERVISOR
   if (hypervisor_kobj) {
      kobject_del(hypervisor_kobj);
      kobject_put(hypervisor_kobj);
   }
#endif

   if (cpuFreqInited) {
      CpuFreq_Exit();
   }

   if (mksckInited) {
      Mksck_Exit();
   }

   if (initTgid) {
      put_pid(initTgid);
   }

   misc_deregister(&mvpkmDev);
   return err;
}

/**
 * @brief De-register the mvpkm device with the Linux kernel.
 */
void
MvpkmExit(void)
{
   PRINTK(KERN_INFO "MvpkmExit called !\n");

   MksckPageInfo_Exit();

#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER
   unregister_shrinker(&mvpkmShrinker);
#endif

   kset_unregister(mvpkmKSet);
   kobject_del(balloonKObj);
   kobject_put(balloonKObj);
   kobject_del(mvpkmKObj);
   kobject_put(mvpkmKObj);
#ifndef CONFIG_SYS_HYPERVISOR
   kobject_del(hypervisor_kobj);
   kobject_put(hypervisor_kobj);
#endif

   CpuFreq_Exit();

   Mksck_Exit();

   put_pid(initTgid);

   misc_deregister(&mvpkmDev);
}

/*
 * The standard module registration macros of Linux.
 */
module_init(MvpkmInit);
module_exit(MvpkmExit);

module_param_named(minHiddenAppOOMAdj, minHiddenAppOOMAdj, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(minHiddenAppOOMAdj, "minimum hidden app oom_adj, as per lowmemorykiller");

#ifdef CONFIG_ANDROID_LOW_MEMORY_KILLER
/**
 * @brief Balloon watchdog timeout callback.
 *
 * Terminate the VM since it's not responsive.
 *
 * @param data vm reference representation.
 */
static void
WatchdogCB(unsigned long data)
{
   MvpkmVM *vm = (MvpkmVM *)data;

   printk("Balloon watchdog expired (%d s)!\n", BALLOON_WATCHDOG_TIMEOUT_SECS);

   Mvpkm_WakeGuest(vm, ACTION_ABORT);
}

/**
 * @brief Slab shrinker.
 *
 * Called by Linux kernel when we're under memory pressure. We treat all locked
 * pages as a slab for this purpose, similar to the Android low memory killer.
 *
 * @param this     reference to registered shrinker for callback context.
 * @param nrToScan number of entries to scan. If 0 then just return the number
 *                 of present entries. We ignore the value of nrToScan when > 1
 *                 since the shrinker is a trigger to readjust guest balloons,
 *                 where the actual balloon size is determined in conjunction
 *                 with the guest.
 * @param gfpMask ignored.
 *
 * @return number of locked pages.
 */
static int
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
MvpkmShrink(struct shrinker *this, struct shrink_control *sc)
#else
MvpkmShrink(struct shrinker *this, int nrToScan, gfp_t gfpMask)
#endif
{
   uint32 locked = 0;
   struct kobject *k;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
   int nrToScan = sc->nr_to_scan;
#endif

   spin_lock(&mvpkmKSet->list_lock);

   list_for_each_entry(k, &mvpkmKSet->list, entry) {
      MvpkmVM *vm = container_of(k, MvpkmVM, kobj);

      locked += ATOMIC_GETO(vm->usedPages);

      /*
       * Try and grab the WSP semaphore - if we fail, we must be VM setup or
       * teardown, no point trying to wake the guest.
       */
      if (nrToScan > 0 &&
          down_read_trylock(&vm->wspSem)) {

         if (vm->wsp) {
            Mvpkm_WakeGuest(vm, ACTION_BALLOON);

            /*
             * Balloon watchdog.
             */
            if (vm->balloonWDEnabled) {
               struct timer_list *t = &vm->balloonWDTimer;

               if (!timer_pending(t)) {
                  t->data = (unsigned long)vm;
                  t->function = WatchdogCB;
                  t->expires = jiffies + BALLOON_WATCHDOG_TIMEOUT_SECS * HZ;
                  add_timer(t);
               }
            }
         }

         up_read(&vm->wspSem);
      }
   }

   spin_unlock(&mvpkmKSet->list_lock);

   return locked;
}
#endif


/**
 * @brief The open file operation. Initializes the vm specific structure.
 */
int
MvpkmOpen(struct inode *inode, struct file *filp)
{
   MvpkmVM *vm;

   if (initTgid != task_tgid(current)) {
      printk(KERN_ERR "%s: MVPKM can be opened only from MVPD (process %d).\n",
             __FUNCTION__, pid_vnr(initTgid));
      return -EPERM;
   }
   printk(KERN_DEBUG "%s: Allocating an MvpkmVM structure from process %s tgid=%d, pid=%d\n",
          __FUNCTION__,
          current->comm,
          task_tgid_vnr(current),
          task_pid_vnr(current));

   vm = kmalloc(sizeof(MvpkmVM), GFP_KERNEL);
   if (!vm) {
      return -ENOMEM;
   }

   memset(vm, 0, sizeof *vm);

   init_timer(&vm->balloonWDTimer);
   init_rwsem(&vm->lockedSem);
   init_rwsem(&vm->wspSem);
   init_rwsem(&vm->monThreadTaskSem);
   vm->monThreadTask = NULL;
   vm->isMonitorInited = false;

   filp->private_data = vm;

   if (!Mvpkm_vmwareUid) {
      Mvpkm_vmwareUid = current_euid();
   }

   return 0;
}

/**
 * @brief Releases a VMs resources
 * @param  vm vm to release
 */
static void
ReleaseVM(MvpkmVM *vm)
{
   del_timer_sync(&vm->balloonWDTimer);

   down_write(&vm->wspSem);

   if (vm->isMonitorInited) {
      MonitorTimer_Request(&vm->monTimer, 0);
#ifdef CONFIG_HAS_WAKELOCK
      wake_lock_destroy(&vm->wakeLock);
#endif
      Mksck_WspRelease(vm->wsp);
      vm->wsp = NULL;
   }

   up_write(&vm->wspSem);

   LockedListUnlockAll(vm);

   UnmapWSPHKVA(vm);

   /*
    * All sockets potentially connected to sockets of this vm's vmId will fail
    * at send now. DGRAM sockets are note required to tear down connection
    * explicitly.
    */

   kfree(vm);
}

/**
 * @brief The release file operation. Releases the vm specific
 *        structure including all the locked pages.
 *
 * @param inode Unused
 * @param filp  which VM we're dealing with
 * @return 0
 */
int
MvpkmRelease(struct inode *inode, struct file *filp)
{
   MvpkmVM *vm = filp->private_data;

   /*
    * Tear down any queue pairs associated with this VM
    */
   if (vm->isMonitorInited) {
      ASSERT(vm->wsp);
      QP_DetachAll(vm->wsp->guestId);
   }

   /*
    * Release the VM's ksets.
    */

   kset_unregister(vm->miscKSet);
   kset_unregister(vm->devicesKSet);

   if (vm->haveKObj) {
      /*
       * Release the VM's kobject.
       * 'vm' will be kfree-d in its kobject's release function.
       */

      kobject_del(&vm->kobj);
      kobject_put(&vm->kobj);
   } else {
      ReleaseVM(vm);
   }

   filp->private_data = NULL;

   printk(KERN_INFO "%s: Released MvpkmVM structure from process %s tgid=%d, pid=%d\n",
          __FUNCTION__,
          current->comm,
          task_tgid_vnr(current),
          task_pid_vnr(current));

   return 0;
}

/**
 * @brief Page fault handler for /dev/mem-like regions (see mvpkmVMOps
 *        block comment).
 */
static int
MvpkmFault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
   unsigned long address = (unsigned long)vmf->virtual_address;
   MPN mpn = vmf->pgoff;
   MvpkmVM *vm = vma->vm_file->private_data;


   /*
    * Only insert pages belonging to the VM. The check is slow, O(n) in the
    * number of MPNs associated with the VM, but it doesn't matter - the mmap
    * interface should only be used by trusted processes at initialization
    * time and for debugging.
    *
    * The mpn can be either in the memory reserved the monitor or mvpd
    * through the regular mechanisms or it could be a mksck page.
    */
   if (!pfn_valid(mpn)) {
      printk(KERN_ERR "MvpkmMMap: Failed to insert %x @ %lx, mpn invalid\n",
             mpn,
             address);
   } else if (LockedListLookup(vm, mpn)) {
      if (vm_insert_page(vma, address, pfn_to_page(mpn)) == 0) {
         return VM_FAULT_NOPAGE;
      }

      printk(KERN_ERR "MvpkmMMap: Failed to insert %x @ %lx \n",
             mpn,
             address);
   } else if (MksckPage_LookupAndInsertPage(vma, address, mpn) == 0) {
      return VM_FAULT_NOPAGE;
   }

   if (vm->stubPageMPN) {
      if (vm_insert_page(vma, address, pfn_to_page(vm->stubPageMPN)) == 0) {
         printk(KERN_INFO "MvpkmMMap: mapped the stub page at %x @ %lx \n",
                mpn,
                address);
         return VM_FAULT_NOPAGE;
      }

      printk(KERN_ERR "MvpkmMMap: Could not insert stub page %x @ %lx \n",
             mpn,
             address);

   }

   return VM_FAULT_SIGBUS;
}

/**
 * @brief sysfs show function for per-VM locked_pages attribute.
 *
 * @param kobj reference to kobj nested in MvpkmVM struct.
 * @param attr attribute reference.
 * @param buf PAGE_SIZEd buffer to write to.
 *
 * @return number of characters printed (not including trailing null character).
 */
static ssize_t
MvpkmAttrShow(struct kobject *kobj,
              struct attribute *attr,
              char *buf)
{
   if (attr == &mvpkmLockedPagesAttr) {
      MvpkmVM *vm = container_of(kobj, MvpkmVM, kobj);

      return snprintf(buf, PAGE_SIZE, "%d\n", ATOMIC_GETO(vm->usedPages));
   } else if (attr == &mvpkmMonitorAttr) {
      MvpkmVM *vm = container_of(kobj, MvpkmVM, kobj);

      return snprintf(buf,
                      PAGE_SIZE,
                      "hostActions %x callno %d\n",
                      ATOMIC_GETO(vm->wsp->hostActions),
                      WSP_Params(vm->wsp)->callno);
   } else {
      return -EPERM;
   }
}

/**
 * @brief sysfs store function for per-VM locked_pages attribute.
 *
 * @param kobj reference to kobj nested in MvpkmVM struct.
 * @param attr attribute reference.
 * @param buf PAGE_SIZEd buffer to write to.
 * @param buf input buffer.
 * @param count input buffer length.
 *
 * @return number of bytes consumed or negative error code.
 */
static ssize_t
MvpkmAttrStore(struct kobject *kobj,
               struct attribute *attr,
               const char *buf,
               size_t count)
{
   if (attr == &mvpkmBalloonWatchdogAttr) {
      MvpkmVM *vm = container_of(kobj, MvpkmVM, kobj);

      /*
       * Enable balloon watchdog on first write.  This includes all ballooning
       * capable guest.
       */
      vm->balloonWDEnabled = true;
      del_timer_sync(&vm->balloonWDTimer);

      return 1;
   } else {
      return -EPERM;
   }
}

/**
 * @brief Map machine address space region into host process.
 *
 * @param file file reference (ignored).
 * @param vma Linux virtual memory area defining the region.
 *
 * @return 0 on success, otherwise error code.
 */
static int
MvpkmMMap(struct file *file, struct vm_area_struct *vma)
{
   vma->vm_ops = &mvpkmVMOps;

   return 0;
}

#ifdef CONFIG_ARM_LPAE
/**
 * @brief Determine host cacheability/shareability attributes.
 *
 * Used to ensure monitor/guest shared mappings are consistent with
 * those of host user/kernel.
 *
 * @param[out] attribMAN when setting up the HW monitor this provides the
 *                       attributes in the generic ARM_MemAttrNormal form,
 *                       suitable for configuring the monitor and guest's
 *                       [H]MAIR0 and setting the shareability attributes of
 *                       the LPAE descriptors.
 */
static void
DetermineMemAttrLPAE(ARM_MemAttrNormal *attribMAN)
{
   /*
    * We use set_pte_ext to sample what {S,TEX,CB} bits Linux is using for
    * normal kernel/user L2D mappings. These bits should be consistent both
    * with each other and what we use in the monitor since we share various
    * pages with both host processes, the kernel module and monitor, and the
    * ARM ARM requires that synonyms have the same cacheability attributes,
    * see end of A3.5.{4,7} ARM DDI 0406A.
    */
   HKVA hkva = __get_free_pages(GFP_KERNEL, 0);

   ARM_LPAE_L3D *pt = (ARM_LPAE_L3D *)hkva;
   ARM_LPAE_L3D *kernL3D = &pt[0], *userL3D = &pt[1];
   uint32 attr, mair0, mair1;

   set_pte_ext((pte_t *)kernL3D, pfn_pte(0, PAGE_KERNEL), 0);
   set_pte_ext((pte_t *)userL3D, pfn_pte(0, PAGE_NONE), 0);

   printk(KERN_INFO
          "DetermineMemAttr: Kernel L3D AttrIndx=%x SH=%x\n",
          kernL3D->blockS1.attrIndx,
          kernL3D->blockS1.sh);

   printk(KERN_INFO
          "DetermineMemAttr: User   L3D AttrIndx=%x SH=%x\n",
          userL3D->blockS1.attrIndx,
          userL3D->blockS1.sh);

   ASSERT(kernL3D->blockS1.attrIndx == userL3D->blockS1.attrIndx);
   ASSERT(kernL3D->blockS1.sh == userL3D->blockS1.sh);

   switch (kernL3D->blockS1.sh) {
      case 0: {
         attribMAN->share = ARM_SHARE_ATTR_NONE;
         break;
      }
      case 2: {
         attribMAN->share = ARM_SHARE_ATTR_OUTER;
         break;
      }
      case 3: {
         attribMAN->share = ARM_SHARE_ATTR_INNER;
         break;
      }
      default: {
         FATAL();
      }
   }

   ARM_MRC_CP15(MAIR0, mair0);
   ARM_MRC_CP15(MAIR1, mair1);

   attr = MVP_EXTRACT_FIELD(kernL3D->blockS1.attrIndx >= 4 ? mair1 : mair0,
                            8 * (kernL3D->blockS1.attrIndx % 4),
                            8);

   /*
    * See B4-1615 ARM DDI 0406C-2c for magic.
    */
#define MAIR_ATTR_2_CACHE_ATTR(x, y) \
   switch (x) { \
      case 2: { \
         (y) = ARM_CACHE_ATTR_NORMAL_WT; \
         break; \
      } \
      case 3: { \
         (y) = ARM_CACHE_ATTR_NORMAL_WB; \
         break; \
      } \
      default: { \
         FATAL(); \
      } \
   }

   MAIR_ATTR_2_CACHE_ATTR(MVP_EXTRACT_FIELD(attr, 2, 2), attribMAN->innerCache);
   MAIR_ATTR_2_CACHE_ATTR(MVP_EXTRACT_FIELD(attr, 6, 2), attribMAN->outerCache);

#undef MAIR_ATTR_2_CACHE_ATTR

   printk(KERN_INFO
          "DetermineMemAttr: innerCache %x outerCache %x share %x\n",
          attribMAN->innerCache,
          attribMAN->outerCache,
          attribMAN->share);

   free_pages(hkva, 0);
}

#else

/**
 * @brief Determine host cacheability/shareability attributes.
 *
 * Used to ensure monitor/guest shared mappings are consistent with
 * those of host user/kernel.
 *
 * @param[out] attribL2D when setting up the LPV monitor a template L2D
 *                       containing cacheability attributes {S, TEX,CB} used by
 *                       host kernel for normal memory mappings. These may be
 *                       used directly for monitor/guest mappings, since both
 *                       worlds share a common {TRE, PRRR, NMRR}.
 * @param[out] attribMAN when setting up TTBR0 in the LPV monitor and the page
 *                       tables for the HW monitor this provides the attributes
 *                       in the generic ARM_MemAttrNormal form, suitable for
 *                       configuring TTBR0 + the monitor and guest's [H]MAIR0
 *                       and setting the shareability attributes of the LPAE
 *                       descriptors.
 */
static void
DetermineMemAttrNonLPAE(ARM_L2D *attribL2D, ARM_MemAttrNormal *attribMAN)
{
   /*
    * We use set_pte_ext to sample what {S,TEX,CB} bits Linux is using for
    * normal kernel/user L2D mappings. These bits should be consistent both
    * with each other and what we use in the monitor since we share various
    * pages with both host processes, the kernel module and monitor, and the
    * ARM ARM requires that synonyms have the same cacheability attributes,
    * see end of A3.5.{4,7} ARM DDI 0406A.
    */
   HKVA hkva = __get_free_pages(GFP_KERNEL, 0);
   uint32 sctlr;
   ARM_L2D *pt = (ARM_L2D *)hkva;
   ARM_L2D *kernL2D = &pt[0], *userL2D = &pt[1];

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
   /*
    * Linux uses the magic 2048 offset in set_pte_ext. See include/asm/pgtable.h
    * for PAGE_NONE and PAGE_KERNEL semantics.
    */
   const uint32 set_pte_ext_offset = 2048;
#else
   /*
    * Linux 2.6.38 switched the order of Linux vs hardware page tables.
    * See mainline d30e45eeabefadc6039d7f876a59e5f5f6cb11c6.
    */
   const uint32 set_pte_ext_offset = 0;
#endif

   set_pte_ext((pte_t *)(kernL2D + set_pte_ext_offset/sizeof(ARM_L2D)),
               pfn_pte(0, PAGE_KERNEL),
               0);
   set_pte_ext((pte_t *)(userL2D + set_pte_ext_offset/sizeof(ARM_L2D)),
               pfn_pte(0, PAGE_NONE),
               0);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
   /*
    * Linux 2.6.38 switched the order of Linux vs hardware page tables.
    * See mainline d30e45eeabefadc6039d7f876a59e5f5f6cb11c6.
    */
   kernL2D += 2048/sizeof(ARM_L2D);
   userL2D += 2048/sizeof(ARM_L2D);
#endif

   printk(KERN_INFO
          "DetermineMemAttr: Kernel L2D TEX=%x CB=%x S=%x\n",
          kernL2D->small.tex,
          kernL2D->small.cb,
          kernL2D->small.s);

   printk(KERN_INFO
          "DetermineMemAttr: User   L2D TEX=%x CB=%x S=%x\n",
          userL2D->small.tex,
          userL2D->small.cb,
          userL2D->small.s);

   ASSERT((kernL2D->small.tex & 1) == (userL2D->small.tex & 1));
   ASSERT(kernL2D->small.cb == userL2D->small.cb);
   ASSERT(kernL2D->small.s == userL2D->small.s);

   *attribL2D = *kernL2D;

   /*
    * We now decode TEX remap and obtain the more generic form for use in
    * the LPV monitor's TTBR0 initialization and the HW monitor.
    */

   ARM_MRC_CP15(CONTROL_REGISTER, sctlr);

   if (sctlr & ARM_CP15_CNTL_TRE) {
      uint32 prrr, nmrr, indx, type, innerCache, outerCache, outerShare,
             share;

      printk(KERN_INFO
             "DetermineMemAttr: TEX remapping enabled\n");

      ARM_MRC_CP15(PRIMARY_REGION_REMAP, prrr);
      ARM_MRC_CP15(NORMAL_MEMORY_REMAP, nmrr);

      printk(KERN_INFO
             "DetermineMemAttr: PRRR=%x NMRR=%x\n",
             prrr,
             nmrr);

      /*
       * Decode PRRR/NMRR below. See B3.7 ARM DDI 0406B for register
       * encodings, tables and magic numbers.
       */

      indx = (MVP_BIT(kernL2D->small.tex, 0) << 2) | kernL2D->small.cb;

      /*
       * Only normal memory makes sense here.
       */
      type = MVP_EXTRACT_FIELD(prrr, 2 * indx, 2);
      ASSERT(type == 2);

      innerCache = MVP_EXTRACT_FIELD(nmrr, 2 * indx, 2);
      outerCache = MVP_EXTRACT_FIELD(nmrr, 16 + 2 * indx, 2);
      outerShare = !MVP_BIT(prrr, 24 + indx);
      share = MVP_BIT(prrr, 18 + kernL2D->small.s);

      printk(KERN_INFO
             "DetermineMemAttr: type %x innerCache %x outerCache %x"
             " share %x outerShare %x\n",
             type,
             innerCache,
             outerCache,
             share,
             outerShare);

      if (share) {
         if (outerShare) {
            attribMAN->share = ARM_SHARE_ATTR_OUTER;
         } else {
            attribMAN->share = ARM_SHARE_ATTR_INNER;
         }
      } else {
         attribMAN->share = ARM_SHARE_ATTR_NONE;
      }

      attribMAN->innerCache = innerCache;
      attribMAN->outerCache = outerCache;
   } else {
      NOT_IMPLEMENTED_JIRA(1849);
   }

   free_pages(hkva, 0);
}
#endif

/**
 * @brief The ioctl file operation.
 *
 * The ioctl command is the main communication method between the
 * vmx and the mvpkm kernel module.
 *
 * @param filp which VM we're dealing with
 * @param cmd select which cmd function needs to be performed
 * @param arg argument for command
 * @return error code, 0 on success
 */
long
MvpkmUnlockedIoctl(struct file  *filp,
                   unsigned int  cmd,
                   unsigned long arg)
{
   MvpkmVM *vm = filp->private_data;
   int retval = 0;

   switch (cmd) {


      case MVPKM_DISABLE_FAULT: {
         if (!vm->stubPageMPN) {
            uint32 *ptr;

            vm->stubPageMPN =
               AllocZeroedFreePages(vm, 0, false, MEMREGION_MAINMEM, (HKVA*)&ptr);
            if (!vm->stubPageMPN) {
               break;
            }
            ptr[0] = MVPKM_STUBPAGE_BEG;
            ptr[PAGE_SIZE/sizeof(uint32) - 1] = MVPKM_STUBPAGE_END;
         }
         break;
      }

      /*
       * Allocate some pinned pages from kernel.
       * Returns -ENOMEM if no host pages available for allocation.
       */
      case MVPKM_LOCK_MPN: {
         struct MvpkmLockMPN buf;

         if (copy_from_user(&buf, (void *)arg, sizeof buf)) {
            return -EFAULT;
         }

         buf.mpn = AllocZeroedFreePages(vm,
                                        buf.order,
                                        false,
                                        buf.forRegion,
                                        NULL);
         if (buf.mpn == 0) {
            return -ENOMEM;
         }

         if (copy_to_user((void *)arg, &buf, sizeof buf)) {
            return -EFAULT;
         }
         break;
      }

      case MVPKM_UNLOCK_MPN: {
         struct MvpkmLockMPN buf;

         if (copy_from_user(&buf, (void *)arg, sizeof buf)) {
            return -EFAULT;
         }

         if (!LockedListDel(vm, buf.mpn)) {
            return -EINVAL;
         }
         break;
      }

      case MVPKM_MAP_WSPHKVA: {
         MvpkmMapHKVA mvpkmMapInfo;
         HkvaMapInfo mapInfo[WSP_PAGE_COUNT];

         if (copy_from_user(&mvpkmMapInfo, (void *)arg, sizeof mvpkmMapInfo)) {
            return -EFAULT;
         }

         if (copy_from_user(mapInfo, (void *)mvpkmMapInfo.mapInfo, sizeof mapInfo)) {
            return -EFAULT;
         }

         mvpkmMapInfo.hkva = MapWSPHKVA(vm, mapInfo);
         BUG_ON(mvpkmMapInfo.hkva == 0);

         if (mvpkmMapInfo.forRegion == MEMREGION_WSP) {
            vm->wsp = (WorldSwitchPage *) mvpkmMapInfo.hkva;
         }

         if (copy_to_user((void *)arg, &mvpkmMapInfo, sizeof mvpkmMapInfo)) {
            return -EFAULT;
         }
         break;
      }

      case MVPKM_RUN_MONITOR: {
         if (!vm->isMonitorInited) {
            vm->isMonitorInited = ((retval = SetupMonitor(vm)) == 0);
         }

         if (vm->isMonitorInited) {
            retval = RunMonitor(vm);
         }

         break;
      }

      case MVPKM_ABORT_MONITOR: {
         if (!vm->isMonitorInited) {
            return -EINVAL;
         }

         ASSERT(vm->wsp != NULL);

         Mvpkm_WakeGuest(vm, ACTION_ABORT);
         break;
      }

      case MVPKM_CPU_INFO: {
         struct MvpkmCpuInfo buf;
         uint32 mpidr;

#ifdef CONFIG_ARM_LPAE
         DetermineMemAttrLPAE(&buf.attribMAN);
         /**
          * We need to add support to the LPV monitor for LPAE page tables if we
          * want to use it on a LPAE host, due to the costs involved in
          * transitioning between LPAE and non-LPAE page tables without Hyp
          * assistance.
          *
          * @knownjira{MVP-2184}
          */
         buf.attribL2D.u = 0;
#else
         DetermineMemAttrNonLPAE(&buf.attribL2D, &buf.attribMAN);
#endif
         /*
          * Are MP extensions implemented? See B4-1618 ARM DDI 0406C-2c for
          * magic.
          */
         ARM_MRC_CP15(MPIDR, mpidr);

         buf.mpExt = mpidr & ARM_CP15_MPIDR_MP;

         if (copy_to_user((int *)arg, &buf, sizeof(struct MvpkmCpuInfo))) {
            retval = -EFAULT;
         }
         break;
      }

      default: {
         retval = -EINVAL;
         break;
      }
   }

   PRINTK(KERN_INFO "returning from IOCTL(%d) retval = %d %s\n",
          cmd, retval, signal_pending(current)?"(pending signal)":"" );

   return retval;
}



/*********************************************************************
 *
 * Locked page management
 *
 *********************************************************************/

/*
 * Pages locked by the kernel module are remembered so an unlockAll
 * operation can be performed when the vmm is closed. The locked page
 * identifiers are stored in a red-black tree to support O(log n)
 * removal and search (required for /dev/mem-like mmap).
 */

/**
 * @brief Descriptor of a locked page range
 */
typedef struct {
   struct {
      __u32 mpn       : 20; ///< MPN.
      __u32 order     : 6;  ///< Size/alignment exponent for page.
      __u32 forRegion : 6;  ///< Annotation to identify guest page allocation
   } page;
   struct rb_node rb;
} LockedPage;

static void FreeLockedPages(LockedPage *lp);

/**
 * @brief Search for an mpn inside a RB tree of LockedPages. The mpn
 *        will match a LockedPage as long as it is covered by the
 *        entry, i.e. in a non-zero order entry it doesn't have to be
 *        the base MPN.
 *
 *        This must be called with the relevant vm->lockedSem held.
 *
 * @param root RB tree root.
 * @param mpn MPN to search for.
 *
 * @return reference to LockedPage entry if found, otherwise NULL.
 */
static LockedPage *
LockedListSearch(struct rb_root *root, __u32 mpn)
{
   struct rb_node *n = root->rb_node;

   while (n) {
      LockedPage *lp = rb_entry(n, LockedPage, rb);

      if (lp->page.mpn == (mpn & (~0UL << lp->page.order))) {
         return lp;
      }

      if (mpn < lp->page.mpn) {
         n = n->rb_left;
      } else {
         n = n->rb_right;
      }
   }

   return NULL;
}

/**
 * @brief Delete an mpn from the list of locked pages.
 *
 * @param vm Mvpkm module control structure pointer
 * @param mpn MPN to be unlocked and freed for reuse
 * @return true if list contained MPN and it was deleted from list
 */

static _Bool
LockedListDel(MvpkmVM *vm, __u32 mpn)
{
   LockedPage *lp;

   down_write(&vm->lockedSem);

   lp = LockedListSearch(&vm->lockedRoot, mpn);

   /*
    * The MPN should be in the locked pages RB tree and it should be the
    * base of an entry, i.e. we can't fragment existing allocations for
    * a VM.
    */
   if (lp == NULL || lp->page.mpn != mpn) {
      up_write(&vm->lockedSem);
      return false;
   }

   FreeLockedPages(lp);

   if (lp->page.forRegion == MEMREGION_MAINMEM) {
      ATOMIC_SUBV(vm->usedPages, 1U << lp->page.order);
   }

   rb_erase(&lp->rb, &vm->lockedRoot);
   kfree(lp);

   up_write(&vm->lockedSem);

   return true;
}

/**
 * @brief Scan the list of locked pages to see if an MPN matches.
 *
 * @param vm Mvpkm module control structure pointer
 * @param mpn MPN to check
 *
 * @return true iff list contains MPN.
 */
static _Bool
LockedListLookup(MvpkmVM *vm, __u32 mpn)
{
   LockedPage *lp;

   down_read(&vm->lockedSem);

   lp = LockedListSearch(&vm->lockedRoot, mpn);

   up_read(&vm->lockedSem);

   return lp != NULL;
}

/**
 * @brief Add a new mpn to the locked pages RB tree.
 *
 * @param vm control structure pointer
 *
 * @param mpn mpn of page that was locked with get_user_pages or some sort of
 *            get that is undone by put_page.
 *            The mpn is assumed to be non-zero
 * @param order size/alignment exponent for page
 * @param forRegion Annotation for Page pool to identify guest page allocations
 *
 * @return false: couldn't allocate internal memory to record mpn in<br>
 *         true:  successful.
 */
static _Bool
LockedListAdd(MvpkmVM *vm,
              __u32 mpn,
              __u32 order,
              PhysMem_RegionType forRegion)
{
   struct rb_node *parent, **p;
   LockedPage *tp, *lp = kmalloc(sizeof *lp, GFP_KERNEL);

   if (!lp) {
      return false;
   }

   lp->page.mpn       = mpn;
   lp->page.order     = order;
   lp->page.forRegion = forRegion;

   down_write(&vm->lockedSem);

   if (forRegion == MEMREGION_MAINMEM) {
      ATOMIC_ADDV(vm->usedPages, 1U << order);
   }

   /*
    * Insert as a red leaf in the tree (see include/linux/rbtree.h).
    */
   p = &vm->lockedRoot.rb_node;
   parent = NULL;

   while (*p) {
      parent = *p;
      tp = rb_entry(parent, LockedPage, rb);

      /*
       * MPN should not already exist in the tree.
       */
      ASSERT(tp->page.mpn != (mpn & (~0UL << tp->page.order)));

      if (mpn < tp->page.mpn) {
         p = &(*p)->rb_left;
      } else {
         p = &(*p)->rb_right;
      }
   }

   rb_link_node(&lp->rb, parent, p);

   /*
    * Restructure tree if necessary (see include/linux/rbtree.h).
    */
   rb_insert_color(&lp->rb, &vm->lockedRoot);

   up_write(&vm->lockedSem);

   return true;
}

/**
 * @brief Traverse RB locked tree, freeing every entry.
 *
 *        This must be called with the relevant vm->lockedSem held.
 *
 * @param node reference to RB node at root of subtree.
 */
static void
LockedListNuke(struct rb_node *node)
{
   while (node) {
      if (node->rb_left) {
         node = node->rb_left;
      } else if (node->rb_right) {
         node = node->rb_right;
      } else {
         /*
          * We found a leaf, free it and go back to parent.
          */
         LockedPage *lp = rb_entry(node, LockedPage, rb);

         if ((node = rb_parent(node))) {
            if (node->rb_left) {
               node->rb_left = NULL;
            } else {
               node->rb_right = NULL;
            }
         }

         FreeLockedPages(lp);
         kfree(lp);
      }
   }
}

/**
 * @brief Unlock all pages at vm close time.
 *
 * @param vm control structure pointer
 */
static void
LockedListUnlockAll(MvpkmVM *vm)
{

   down_write(&vm->lockedSem);

   LockedListNuke(vm->lockedRoot.rb_node);

   ATOMIC_SETV(vm->usedPages, 0);

   up_write(&vm->lockedSem);
}


/**
 * @brief Allocate zeroed free pages
 *
 * @param[in] vm which VM the pages are for so they will be freed when the vm
 *               closes
 * @param[in] order log2(number of contiguous pages to allocate)
 * @param[in] highmem is it OK to allocate this page in ZONE_HIGHMEM? This
 *                    option should only be specified for pages the host kernel
 *                    will not need to address directly.
 * @param[out] hkvaRet where to return host kernel virtual address of the
 *                     allocated pages, if non-NULL, and ONLY IF !highmem.
 * @param forRegion Annotation for Page pool to identify guest page allocations
 * @return 0: no host memory available<br>
 *      else: starting MPN<br>
 *            *hkvaRet = filled in
 */
static MPN
AllocZeroedFreePages(MvpkmVM *vm,
                     uint32 order,
                     _Bool highmem,
                     PhysMem_RegionType forRegion,
                     HKVA *hkvaRet)
{
   MPN mpn;
   struct page *page;

   if (order > PAGE_ALLOC_COSTLY_ORDER) {
      printk(KERN_WARNING "Order %d allocation for region %d exceeds the safe "
             "maximum order %d\n",
             order,
             forRegion,
             PAGE_ALLOC_COSTLY_ORDER);
   }

   /*
    * Get some pages for the requested range.  They will be physically
    * contiguous and have the requested alignment.  They will also
    * have a kernel virtual mapping if !highmem.
    *
    * We allocate out of ZONE_MOVABLE even though we can't just pick up our
    * bags. We do this to support platforms that explicitly configure
    * ZONE_MOVABLE, such as the Qualcomm MSM8960, to enable deep power down of
    * memory banks. When the kernel attempts to take a memory bank offline, it
    * will try and place the pages on the isolate LRU - only pages already on an
    * LRU, such as anon/file, can get there, so it will not be able to
    * migrate/move our pages (and hence the bank will not be offlined). The
    * other alternative is to live withing ZONE_NORMAL, and only have available
    * a small fraction of system memory. Long term we plan on hooking the
    * offlining callback in mvpkm and perform our own migration with the
    * cooperation of the monitor, but we don't have dev board to support this
    * today.
    *
    * @knownjira{MVP-3477}
    */
   page = alloc_pages(GFP_USER | __GFP_COMP | __GFP_ZERO |
                      (highmem ? __GFP_HIGHMEM | __GFP_MOVABLE : 0),
                      order);

   if (page == NULL) {
      return 0;
   }

   /*
    * Return the corresponding page number.
    */
   mpn = page_to_pfn(page);
   ASSERT(mpn != 0);

   /*
    * Remember to unlock the pages when the FD is closed.
    */
   if (!LockedListAdd(vm, mpn, order, forRegion)) {
      __free_pages(page, order);
      return 0;
   }

   if (hkvaRet) {
      *hkvaRet = highmem ? 0 : __phys_to_virt(page_to_phys(page));
   }

   return mpn;
}

/**
 * @brief Map already-pinned WSP memory in host kernel virtual address(HKVA)
 * space. Assumes 2 world switch pages on an 8k boundary.
 *
 * @param[in] vm which VM the HKVA Area is to be mapped for
 * @param[in] mapInfo array of MPNs and execute permission flags to be used in
                      inserting a new contiguous map in HKVA space
 * @return 0: HKVA space could not be mapped
           else: HKVA where mapping was inserted
 */
static HKVA
MapWSPHKVA(MvpkmVM *vm, HkvaMapInfo *mapInfo)
{
   unsigned int i;
   struct page **pages = NULL;
   struct page **pagesPtr;
   pgprot_t prot;
   int retval;
   int allocateCount = WSP_PAGE_COUNT + 1; // Reserve one page for alignment
   int pageIndex = 0;
   HKVA dummyPage = (HKVA)NULL;
   HKVA start;
   HKVA startSegment;
   HKVA endSegment;

   /*
    * Add one page for alignment purposes in case __get_vm_area returns an
    * unaligned address.
    */
   ASSERT(allocateCount == 3);
   ASSERT_ON_COMPILE(WSP_PAGE_COUNT == 2);

   /*
    * NOT_IMPLEMENTED if MapHKVA is called more than once.
    */
   BUG_ON(vm->wspHkvaArea);

   /*
    * Reserve virtual address space.
    */
   vm->wspHkvaArea = __get_vm_area((allocateCount * PAGE_SIZE), VM_ALLOC, MODULES_VADDR, MODULES_END);
   if (!vm->wspHkvaArea) {
      return 0;
   }

   pages = kmalloc(allocateCount * sizeof(struct page *), GFP_TEMPORARY);
   if (!pages) {
      goto err;
   }
   pagesPtr = pages;

   /*
    * Use a dummy page to boundary align the section, if needed.
    */
   dummyPage = __get_free_pages(GFP_KERNEL, 0);
   if (!dummyPage) {
      goto err;
   }
   vm->wspHKVADummyPage = dummyPage;

   /*
    * Back every entry with the dummy page.
    */
   for (i = 0; i < allocateCount; i++) {
      pages[i] = virt_to_page(dummyPage);
   }

   /*
    * World switch pages must not span a 1MB boundary in order to maintain only
    * a single L2 page table.
    */
   start = (HKVA)vm->wspHkvaArea->addr;
   startSegment = start & ~(ARM_L1D_SECTION_SIZE - 1);
   endSegment   = (start + PAGE_SIZE) & ~(ARM_L1D_SECTION_SIZE - 1);
   /*
    * Insert dummy page at pageIndex, if needed.
    */
   pageIndex = (startSegment != endSegment);

   /*
    * Back the rest with the actual world switch pages
    */
   for (i = pageIndex; i < pageIndex + WSP_PAGE_COUNT; i++) {
      pages[i] = pfn_to_page(mapInfo[i - pageIndex].mpn);
   }

   /*
    * Given the lack of functionality in the kernel for being able to mark
    * mappings for a given vm area with different sets of protection bits,
    * we simply mark the entire vm area as PAGE_KERNEL_EXEC for now
    * (i.e., union of all the protection bits). Given that the kernel
    * itself does something similar while loading modules, this should be a
    * reasonable workaround for now. In the future, we should set the
    * protection bits to strictly adhere to what has been requested in the
    * mapInfo parameter.
    */
   prot = PAGE_KERNEL_EXEC;

   retval = map_vm_area(vm->wspHkvaArea, prot, &pagesPtr);
   if (retval < 0) {
      goto err;
   }

   kfree(pages);

   return (HKVA)(vm->wspHkvaArea->addr) + pageIndex * PAGE_SIZE;

err:
   if (dummyPage) {
      free_pages(dummyPage, 0);
      vm->wspHKVADummyPage = (HKVA)NULL;
   }

   if (pages) {
      kfree(pages);
   }

   free_vm_area(vm->wspHkvaArea);
   vm->wspHkvaArea = (HKVA)NULL;

   return 0;
}

static void
UnmapWSPHKVA(MvpkmVM *vm)
{
   if (vm->wspHkvaArea) {
      free_vm_area(vm->wspHkvaArea);
   }

   if (vm->wspHKVADummyPage) {
      free_pages(vm->wspHKVADummyPage, 0);
      vm->wspHKVADummyPage = (HKVA)NULL;
   }
}

/**
 * @brief Clean and release locked pages
 *
 * @param lp Reference to the locked pages
 */
static void
FreeLockedPages(LockedPage *lp)
{
   struct page *page;
   int count;

   page = pfn_to_page(lp->page.mpn);
   count = page_count(page);

   if (count == 0) {
      printk(KERN_ERR "%s: found locked page with 0 reference (mpn %05x)\n",
             __func__, lp->page.mpn);
      return;
   }

   if (count == 1) {
      int i;

      /*
       * There is no other user for this page, clean it.
       *
       * We don't bother checking if the page was highmem or not, clear_highmem
       * works for both.
       * We clear the content of the page, and rely on the fact that the previous
       * worldswitch has cleaned the potential VIVT I-CACHE.
       */
      for (i = 0; i < (1 << lp->page.order); i++) {
         clear_highpage(page + i);
      }
   } else if (lp->page.forRegion != MEMREGION_MAINMEM) {
      printk(KERN_WARNING "%s: mpn 0x%05x for region %d is still in use\n",
             __func__, lp->page.mpn, lp->page.forRegion);
   }

   __free_pages(page, lp->page.order);
}

/*********************************************************************
 *
 * Communicate with monitor
 *
 *********************************************************************/

/**
 * @brief Register a new monitor page.
 *
 * @param vm  which virtual machine we're running
 * @return 0: successful<br>
 *      else: -errno
 */
static int
SetupMonitor(MvpkmVM *vm)
{
   int retval;
   WorldSwitchPage *wsp = vm->wsp;

   if (!wsp ||
       wsp->wspHKVA != (HKVA)wsp) {
      return -EINVAL;
   }

   if ((retval = Mksck_WspInitialize(vm))) {
      return retval;
   }

   vm->kobj.kset = mvpkmKSet;
   retval = kobject_init_and_add(&vm->kobj, &mvpkmKType, NULL, "%d", wsp->guestId);
   if (retval) {
      goto error;
   }

   /*
    * Get a reference to this module such that it cannot be unloaded until
    * our kobject's release function completes.
    */

   __module_get(THIS_MODULE);
   vm->haveKObj = true;

   /*
    * Caution: From here on, if we fail, we must not call kobject_put()
    * on vm->kobj since that may / will deallocate 'vm'. Unregistering VM
    * ksets on failures, is fine and should be done for proper ref counting.
    */

   vm->devicesKSet = kset_create_and_add("devices", NULL, &vm->kobj);
   if (!vm->devicesKSet) {
      retval = -ENOMEM;
      goto error;
   }

   vm->miscKSet = kset_create_and_add("misc", NULL, &vm->kobj);
   if (!vm->miscKSet) {
      kset_unregister(vm->devicesKSet);
      vm->devicesKSet = NULL;
      retval = -ENOMEM;
      goto error;
   }

   down_write(&vm->wspSem);

   /*
    * The VE monitor needs to issue a SMC to bootstrap Hyp mode.
    */
   if (wsp->monType == MONITOR_TYPE_VE) {
      /*
       * Here we assemble the monitor's HMAIR0 based on wsp->memAttr. We map
       * from the inner/outer normal page cacheability attributes obtained
       * from DetermineCacheabilityAttribs to the format required in 4.2.8
       * ARM PRD03-GENC-008469 13.0 (see this document for the magic numbers).
       *
       * Where a choice is available, we opt for read and/or write allocation.
       */
      static const uint32 normalCacheAttr2MAIR[4] = { 0x4, 0xf, 0xa, 0xe };

      uint32 hmair0 =
         ((normalCacheAttr2MAIR[wsp->memAttr.innerCache] |
           (normalCacheAttr2MAIR[wsp->memAttr.outerCache] << 4))
          << 8 * MVA_MEMORY) |
         (0x4 << 8 * MVA_DEVICE);

      /*
       * See B4.1.74 ARM DDI 0406C-2c for the HTCR magic.
       */
      uint32 htcr =
         0x80000000 |
         (wsp->memAttr.innerCache << 8) |
         (wsp->memAttr.outerCache << 10) |
         (wsp->memAttr.share << 12);

      /**
       * @knownjira{MVP-377}
       * Set HSCTLR to enable MMU and caches. We should really run the
       * monitor WXN, in non-MVP_DEVEL builds. See
       * 13.18 ARM PRD03-GENC-008353 11.0 for the magic.
       */
      static const uint32 hsctlr = 0x30c5187d;

      register uint32 r0 asm("r0") = wsp->monVA.excVec;
      register uint32 r1 asm("r1") = wsp->regSave.ve.mHTTBR;
      register uint32 r2 asm("r2") = htcr;
      register uint32 r3 asm("r3") = hmair0;
      register uint32 r4 asm("r4") = hsctlr;

      asm volatile (
         ".arch_extension sec\n"
         "smc 0"
         :
         : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4)
         : "memory"
         );
   }

   /*
    * Initialize guest wait-for-interrupt waitqueue.
    */
   init_waitqueue_head(&vm->wfiWaitQ);

   MonitorTimer_Setup(vm);

#ifdef CONFIG_HAS_WAKELOCK
   wake_lock_init(&vm->wakeLock, WAKE_LOCK_SUSPEND, "mvpkm");
#endif

   wsp->mvpkmVersion = MVP_VERSION_CODE;
   up_write(&vm->wspSem);
   /*
    * Ensure coherence of monitor loading and page tables.
    */
   flush_cache_all();
   return 0;

error:
   Mksck_WspRelease(wsp);
   vm->wsp = NULL;
   return retval;
}

/**
 * @brief dummy function to drop the info parameter
 * @param info ignored
 */
static
void FlushAllCpuCaches(void *info)
{
   flush_cache_all();
}

/**
 * @brief return to where monitor called worldswitch
 *
 * @param vm  which virtual machine we're running
 * @return 0: successful, just call back when ready<br>
 *         1: successful, process code in WSP_Params(wsp)->callno<br>
 *      else: -errno
 */
static int
RunMonitor(MvpkmVM *vm)
{
   int ii;
   unsigned long flags;
   WorldSwitchPage *wsp = vm->wsp;
   int retval = 0;

   ASSERT(wsp);

#ifdef CONFIG_HAS_WAKELOCK
   wake_lock(&vm->wakeLock);
#endif

   /*
    * Set VCPUThread affinity
    */
   if (cpumask_intersects(to_cpumask(vcpuAffinity), cpu_active_mask)) {
      set_cpus_allowed_ptr(current, to_cpumask(vcpuAffinity));
   }

   /*
    * Record the the current task structure, so an ABORT will know,
    * who to wake.
    */
   down_write(&vm->monThreadTaskSem);
   vm->monThreadTask = get_current();
   up_write(&vm->monThreadTaskSem);

   /*
    * Keep going as long as the monitor is in critical section or
    * there are no pending signals such as SIGINT or SIGKILL.  Block
    * interrupts before checking so any IPI sent will remain pending
    * if our check just misses detecting the signal.
    */
   local_irq_save(flags);
   while (wsp->critSecCount > 0 ||
          (!signal_pending(current) &&
           !(ATOMIC_GETO(wsp->hostActions) & ACTION_ABORT))) {
      /*
       * ARMv7 Performance counters are per CPU core and might be disabled over
       * CPU core sleep if there is nothing else in the system to re-enable
       * them, so now that we have been allocated a CPU core to run the guest,
       * enable them and in particular the TSC (CCNT) which is used for monitor
       * timing between world switches.
       */
      {
         uint32 pmnc;
         uint32 pmcnt;

         /* make sure that the Performance Counters are enabled */
         ARM_MRC_CP15(PERF_MON_CONTROL_REGISTER, pmnc);
         if ((pmnc & (ARM_PMNC_E | ARM_PMNC_D)) != (ARM_PMNC_E)) {
            pmnc |=  ARM_PMNC_E;  // Enable TSC
            pmnc &= ~ARM_PMNC_D;  // Disable cycle count divider
            ARM_MCR_CP15(PERF_MON_CONTROL_REGISTER, pmnc);
         }

         /* make sure that the CCNT is enabled */
         ARM_MRC_CP15(PERF_MON_COUNT_SET, pmcnt);
         if ((pmcnt & ARM_PMCNT_C) != ARM_PMCNT_C) {
            pmcnt |= ARM_PMCNT_C;
            ARM_MCR_CP15(PERF_MON_COUNT_SET, pmcnt);
         }
      }

      /*
       * Update TSC to RATE64 ratio
       */
      {
         struct TscToRate64Cb *ttr = &__get_cpu_var(tscToRate64);
         wsp->tscToRate64Mult = ttr->mult;
         wsp->tscToRate64Shift = ttr->shift;
      }

      /*
       * Save the time of day for the monitor's timer facility.  The timing
       * facility in the vmm needs to compute current time in the host linux's
       * time representation.  It uses the formula:
       *    now = wsp->switchedAt64 + (uint32)(TSC_READ() - wsp->lowerTSC)
       *
       * Read the timestamp counter *immediately after* ktime_get() as that
       * will give the most consistent offset between reading the hardware
       * clock register in ktime_get() and reading the hardware timestamp
       * counter with TSC_READ().
       */
      ASSERT_ON_COMPILE(MVP_TIMER_RATE64 == NSEC_PER_SEC);
      {
         ktime_t now = ktime_get();
         TSC_READ(wsp->switchedAtTSC);
         wsp->switchedAt64 = ktime_to_ns(now);
      }

      /*
       * Save host FPU contents and load monitor contents.
       */
      SWITCH_VFP_TO_MONITOR;

      /*
       * Call into the monitor to run guest instructions until it wants us to
       * do something for it.  Note that any hardware interrupt request will
       * cause it to volunteer.
       */
      switch (wsp->monType) {
         case MONITOR_TYPE_LPV: {
            uint32 hostVBAR;

            ARM_MRC_CP15(VECTOR_BASE, hostVBAR);
            (*wsp->switchToMonitor)(&wsp->regSave);
            ARM_MCR_CP15(VECTOR_BASE, hostVBAR);
            break;
         }
         case MONITOR_TYPE_VE: {
            register uint32 r1 asm("r1") = wsp->regSave.ve.mHTTBR;

            asm volatile (
               ".word " MVP_STRINGIFY(ARM_INSTR_HVC_A1_ENC(0))
               : "=r" (r1) : "r" (r1) : "r0", "r2", "memory"
            );
            break;
         }
         default: FATAL();
      }

      /*
       * Save monitor FPU contents and load host contents.
       */
      SWITCH_VFP_TO_HOST;

      /*
       * Re-enable local interrupts now that we are back in the host world
       */
      local_irq_restore(flags);


      /*
       * Maybe the monitor wrote some messages to monitor->host sockets.
       * This will wake the corresponding host threads to receive them.
       */
      /**
       * @todo This lousy loop is in the critical path. It should be changed
       * to some faster algorithm to wake blocked host sockets.
       */
      for (ii = 0; ii < MKSCK_MAX_SHARES; ii++) {
         if (wsp->isPageMapped[ii]) {
            Mksck_WakeBlockedSockets(MksckPage_GetFromIdx(ii));
         }
      }

      switch (WSP_Params(wsp)->callno) {
         case WSCALL_ACQUIRE_PAGE: {
            uint32 i;

            for (i = 0; i < WSP_Params(wsp)->pages.pages; ++i) {
               MPN mpn = AllocZeroedFreePages(vm,
                                              WSP_Params(wsp)->pages.order,
                                              true,
                                              WSP_Params(wsp)->pages.forRegion,
                                              NULL);
               if (mpn == 0) {
                  printk(KERN_WARNING "WSCALL_ACQUIRE_PAGE: no order %u pages available\n",
                        WSP_Params(wsp)->pages.order);
                  WSP_Params(wsp)->pages.pages = i;
                  break;
               }

               WSP_Params(wsp)->pages.mpns[i] = mpn;
            }

            break;
         }
         case WSCALL_RELEASE_PAGE: {
            uint32 i;

            for (i = 0; i < WSP_Params(wsp)->pages.pages; ++i) {
               if (!LockedListDel(vm, WSP_Params(wsp)->pages.mpns[i])) {
                  WSP_Params(wsp)->pages.pages = i;
                  break;
               }
            }

            break;
         }
         case WSCALL_MUTEXLOCK: {
            retval = Mutex_Lock((void *)WSP_Params(wsp)->mutex.mtxHKVA,
                                WSP_Params(wsp)->mutex.mode);

            if (retval < 0) {
               WSP_Params(wsp)->mutex.ok = false;
               goto monitorExit;
            }

            /*
             * The locking succeeded. From this point on the monitor
             * is in critical section. Even if an interrupt comes
             * right here, it must return to the monitor to unlock the
             * mutex.
             */
            wsp->critSecCount++;
            WSP_Params(wsp)->mutex.ok = true;
            break;
         }
         case WSCALL_MUTEXUNLOCK: {
            Mutex_Unlock((void *)WSP_Params(wsp)->mutex.mtxHKVA,
                         WSP_Params(wsp)->mutex.mode);
            break;
         }
         case WSCALL_MUTEXUNLSLEEP: {
            /*
             * The vcpu has just come back from the monitor. During
             * the transition interrupts were disabled. Above,
             * however, interrupts were enabled again and it is
             * possible that a context switch happened into a thread
             * (serve_vmx) that instructed the vcpu thread to
             * abort. After returning to this thread the vcpu may
             * enter a sleep below never to return from it. To avoid
             * this deadlock we need to test the abort flag in
             * Mutex_UnlSleepTest.
             */
            retval =
               Mutex_UnlSleepTest((void *)WSP_Params(wsp)->mutex.mtxHKVA,
                                  WSP_Params(wsp)->mutex.mode,
                                  WSP_Params(wsp)->mutex.cvi,
                                  &wsp->hostActions,
                                  ACTION_ABORT);
            if (retval < 0) {
               goto monitorExit;
            }
            break;
         }
         case WSCALL_MUTEXUNLWAKE: {
            Mutex_UnlWake((void *)WSP_Params(wsp)->mutex.mtxHKVA,
                          WSP_Params(wsp)->mutex.mode,
                          WSP_Params(wsp)->mutex.cvi,
                          WSP_Params(wsp)->mutex.all);
            break;
         }

         /*
          * The monitor wants us to block (allowing other host threads to run)
          * until an async message is waiting for the monitor to process.
          *
          * If MvpkmWaitForInt() returns an error, it should only be if there
          * is another signal pending (such as SIGINT).  So we pretend it
          * completed normally, as the monitor is ready to be called again (it
          * will see no messages to process and wait again), and return to user
          * mode so the signals can be processed.
          */
         case WSCALL_WAIT: {
#ifdef CONFIG_HAS_WAKELOCK
            if (WSP_Params(wsp)->wait.suspendMode) {
               /* guest has ok'ed suspend mode, so release SUSPEND wakelock */
               wake_unlock(&vm->wakeLock);
               retval = MvpkmWaitForInt(vm, true);
               wake_lock(&vm->wakeLock);
               WSP_Params(wsp)->wait.suspendMode = 0;
            } else {
               /* guest has asked for WFI not suspend so keep holding SUSPEND
                * wakelock */
               retval = MvpkmWaitForInt(vm, false);
            }
#else
            retval = MvpkmWaitForInt(vm, WSP_Params(wsp)->wait.suspendMode);
#endif
            if (retval < 0) {
               goto monitorExit;
            }
            break;
         }

         /*
          * The only reason the monitor returned was because there was a
          * pending hardware interrupt.  The host serviced and cleared that
          * interrupt when we enabled interrupts above.  Now we call the
          * scheduler in case that interrupt woke another thread, we want to
          * allow that thread to run before returning to do more guest code.
          */
         case WSCALL_IRQ: {
            break;
         }

         case WSCALL_GET_PAGE_FROM_VMID: {
            MksckPage *mksckPage;
            mksckPage = MksckPage_GetFromVmIdIncRefc(WSP_Params(wsp)->pageMgmnt.vmId);

            if (mksckPage) {
               int ii;

               WSP_Params(wsp)->pageMgmnt.found = true;
               for (ii = 0; ii < MKSCKPAGE_TOTAL; ii++) {
                  WSP_Params(wsp)->pageMgmnt.mpn[ii] =
                     vmalloc_to_pfn( (void*)(((HKVA)mksckPage) + ii*PAGE_SIZE) );
               }

               ASSERT(!wsp->isPageMapped[MKSCK_VMID2IDX(mksckPage->vmId)]);
               wsp->isPageMapped[MKSCK_VMID2IDX(mksckPage->vmId)] = true;
            } else {
               WSP_Params(wsp)->pageMgmnt.found = false;
            }
            break;
         }

         case WSCALL_REMOVE_PAGE_FROM_VMID: {
            MksckPage *mksckPage;
            mksckPage = MksckPage_GetFromVmId(WSP_Params(wsp)->pageMgmnt.vmId);
            ASSERT(wsp->isPageMapped[MKSCK_VMID2IDX(mksckPage->vmId)]);
            wsp->isPageMapped[MKSCK_VMID2IDX(mksckPage->vmId)] = false;
            MksckPage_DecRefc(mksckPage);
            break;
         }

         /*
          * Read current wallclock time.
          */
         case WSCALL_READTOD: {
            struct timeval nowTV;
            do_gettimeofday(&nowTV);
            WSP_Params(wsp)->tod.now = nowTV.tv_sec;
            WSP_Params(wsp)->tod.nowusec = nowTV.tv_usec;
            break;
         }

         case WSCALL_LOG: {
            int len = strlen(WSP_Params(wsp)->log.messg);
            printk(KERN_INFO
                   "VMM: %s%s",
                   WSP_Params(wsp)->log.messg,
                   (WSP_Params(wsp)->log.messg[len-1] == '\n') ? "" : "\n");
            break;
         }

         case WSCALL_ABORT: {
            retval = WSP_Params(wsp)->abort.status;
            goto monitorExit;
         }

         case WSCALL_QP_GUEST_ATTACH: {
            int32 rc;
            QPInitArgs args;
            uint32 base;
            uint32 nrPages;

            args.id       = WSP_Params(wsp)->qp.id;
            args.capacity = WSP_Params(wsp)->qp.capacity;
            args.type     = WSP_Params(wsp)->qp.type;
            base          = WSP_Params(wsp)->qp.base;
            nrPages       = WSP_Params(wsp)->qp.nrPages;

            rc = QP_GuestAttachRequest(vm, &args, base, nrPages);

            WSP_Params(wsp)->qp.rc           = rc;
            WSP_Params(wsp)->qp.id           = args.id;
            break;
         }

         case WSCALL_QP_NOTIFY: {
            QPInitArgs args;

            args.id       = WSP_Params(wsp)->qp.id;
            args.capacity = WSP_Params(wsp)->qp.capacity;
            args.type     = WSP_Params(wsp)->qp.type;

            WSP_Params(wsp)->qp.rc = QP_NotifyListener(&args);
            break;
         }

         case WSCALL_MONITOR_TIMER: {
            MonitorTimer_Request(&vm->monTimer, WSP_Params(wsp)->timer.when64);
            break;
         }

         case WSCALL_COMM_SIGNAL: {
            Mvpkm_CommEvSignal(&WSP_Params(wsp)->commEvent.transpID,
                                WSP_Params(wsp)->commEvent.event);
            break;
         }

         case WSCALL_FLUSH_ALL_DCACHES: {
            /*
             * Broadcast Flush DCache request to all cores.
             * Block while waiting for all of them to get done.
             */
            on_each_cpu(FlushAllCpuCaches, NULL, 1);
            break;
         }
         default: {
            retval = -EPIPE;
            goto monitorExit;
         }
      }

      /*
       * The params.callno callback was handled in kernel mode and completed
       * successfully.  Repeat for another call without returning to user mode,
       * unless there are signals pending.
       *
       * But first, call the Linux scheduler to switch threads if there is
       * some other thread Linux wants to run now.
       */
      if (need_resched()) {
         schedule();
      }

      /*
       * Check if cpus allowed mask has to be updated.
       * Updating it must be done outside of an atomic context.
       */
      if (cpumask_intersects(to_cpumask(vcpuAffinity), cpu_active_mask) &&
          !cpumask_equal(to_cpumask(vcpuAffinity), &current->cpus_allowed)) {
         set_cpus_allowed_ptr(current, to_cpumask(vcpuAffinity));
      }

      local_irq_save(flags);
   }

   /*
    * There are signals pending so don't try to do any more monitor/guest
    * stuff.  But since we were at the point of just about to run the monitor,
    * return success status as user mode can simply call us back to run the
    * monitor again.
    */
   local_irq_restore(flags);

monitorExit:
   ASSERT(wsp->critSecCount == 0);

   if (ATOMIC_GETO(wsp->hostActions) & ACTION_ABORT) {
      PRINTK(KERN_INFO "Monitor has ABORT flag set.\n");
      retval = ExitStatusHostRequest;
   }

#ifdef CONFIG_HAS_WAKELOCK
   wake_unlock(&vm->wakeLock);
#endif

   down_write(&vm->monThreadTaskSem);
   vm->monThreadTask = NULL;
   up_write(&vm->monThreadTaskSem);

   return retval;
}

/**
 * @brief Guest is waiting for interrupts, sleep if necessary
 *
 * @param vm  which virtual machine we're running
 * @param suspend is the guest entering suspend or just WFI?
 * @return 0: woken up, hostActions should have pending events
 *        -ERESTARTSYS: broke out because other signals are pending
 *
 * This function is called in the VCPU context after the world switch to wait
 * for an incoming message.  If any message gets queued to this VCPU, the
 * sender will wake us up.
 */
int
MvpkmWaitForInt(MvpkmVM *vm, _Bool suspend)
{
   WorldSwitchPage *wsp = vm->wsp;
   wait_queue_head_t *q = &vm->wfiWaitQ;

   if (suspend) {
      return wait_event_interruptible(*q, ATOMIC_GETO(wsp->hostActions) != 0);
   } else {
      int ret;
      ret = wait_event_interruptible_timeout(*q, ATOMIC_GETO(wsp->hostActions) != 0, 10*HZ);
      if (ret == 0) {
         printk("MvpkmWaitForInt: guest stuck for 10s in WFI! (hostActions %08x)\n",
               ATOMIC_GETO(wsp->hostActions));
      }
      return ret > 0 ? 0 : ret;
   }
}


/**
 * @brief Force the guest to evaluate its hostActions flag field
 *
 * @param vm which guest needs waking
 * @param why why should be guest be woken up?
 *
 * This function updates the hostAction flag field as and wakes up the guest as
 * required so that it can evaluate it.  The guest could be executing guest
 * code in an SMP system, in that case send an IPI; or it could be sleeping, in
 * the case wake it up.
 */
void
Mvpkm_WakeGuest(MvpkmVM *vm, int why)
{
   ASSERT(why != 0);

   /* set the host action */
   if (ATOMIC_ORO(vm->wsp->hostActions, why) & why) {
      /* guest has already been woken up so no need to do it again */
      return;
   }

   /*
    * VCPU is certainly in 'wait for interrupt' wait. Wake it up !
    */
#ifdef CONFIG_HAS_WAKELOCK
   /*
    * To prevent the system to go in suspend mode before the monitor had a
    * chance on being scheduled, we will hold the VM wakelock from now.
    * As the wakelocks are not managed as reference counts, this is not an
    * an issue to take a wake_lock twice in a row.
    */
   wake_lock(&vm->wakeLock);
#endif

   /*
    * On a UP system, we ensure the monitor thread isn't blocked.
    *
    * On an MP system the other CPU might be running the guest. This
    * is noop on UP.
    *
    * When the guest is running, it is an invariant that monThreadTaskSem is not
    * held as a write lock, so we should not fail to acquire the lock.
    * Mvpkm_WakeGuest may be called from an atomic context, so we can't sleep
    * here.
    */
   if (down_read_trylock(&vm->monThreadTaskSem)) {
      if (vm->monThreadTask) {
         wake_up_process(vm->monThreadTask);
         kick_process(vm->monThreadTask);
      }
      up_read(&vm->monThreadTaskSem);
   } else {
      printk("Unexpected failure to acquire monThreadTaskSem!\n");
   }
}