aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mvp/mvpkm/mksck_kernel.c
blob: 6811a68ae408c455e7602f6bb3c5149934d6915c (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
/*
 * 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 monitor/kernel socket interface kernel extension.
 */

#define __KERNEL_SYSCALLS__
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.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/rcupdate.h>
#include <linux/smp.h>
#include <linux/spinlock.h>

#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/file.h>
#include <linux/vmalloc.h>

#include <linux/debugfs.h>
#include <linux/seq_file.h>

#include <net/sock.h>

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

#include "mvp.h"
#include "actions.h"
#include "mvpkm_kernel.h"
#include "mksck_kernel.h"
#include "mksck_sockaddr.h"
#include "mutex_kernel.h"

void NORETURN FatalError(char const *file,
                int line,
                FECode feCode,
                int bugno,
                char const *fmt,
                ...)
{
   /* Lock around printing the error details so that the messages from multiple
    * threads are not interleaved. */
   static DEFINE_MUTEX(fatalErrorMutex);
   mutex_lock(&fatalErrorMutex);

   FATALERROR_COMMON(printk, vprintk, file, line, feCode, bugno, fmt);

   dump_stack();

   /* done printing */
   mutex_unlock(&fatalErrorMutex);

   /* do_exit below exits the current thread but does not crash the kernel.
    * Hence the stack dump will actually be readable from other user threads.
    */
   do_exit(1);
}


/*
 * The project uses a new address family: AF_MKSCK. Optimally this address
 * family were accepted with the Linux community and a permanent number
 * were assigned. This, however, is a dream only, not even the x86 team
 * has been able to pull it off.
 *
 * Instead we ASSUME that DECnet is dead and re-use it's address family number.
 * This is what the x86 world is moving too in the latest versions.
 */

static struct proto mksckProto = {
   .name     = "AF_MKSCK",
   .owner    = THIS_MODULE,
   .obj_size = sizeof (struct sock),
};

static int MksckCreate(struct net *net,
                       struct socket *sock,
                       int protocol,
                       int kern);

static struct net_proto_family mksckFamilyOps = {
   .family = AF_MKSCK,
   .owner  = THIS_MODULE,
   .create = MksckCreate,
};

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


/**
 * @brief Linux vma operations for receive windows established via Mksck
 *        mmap.
 */
static struct vm_operations_struct mksckVMOps = {
   .fault = MksckFault
};

/*
 * List of hosts and guests we know about.
 */
static spinlock_t mksckPageListLock;
static MksckPage *mksckPages[MKSCK_MAX_SHARES];

/*
 * The following functions form the AF_MKSCK DGRAM operations.
 */
static int MksckRelease(struct socket *sock);
static int MksckBacklogRcv(struct sock *sk, struct sk_buff *skb);
static void MksckSkDestruct(struct sock *sk);
static int MksckBind(struct socket *sock,
                     struct sockaddr *addr,
                     int addrLen);
static int MksckBindGeneric(struct sock *sk,
                            Mksck_Address addr);
static int MksckDgramRecvMsg(struct kiocb *kiocb,
                             struct socket *sock,
                             struct msghdr *msg,
                             size_t len,
                             int flags);
static int MksckDgramSendMsg(struct kiocb *kiocb,
                             struct socket *sock,
                             struct msghdr *msg,
                             size_t len);
static int MksckGetName(struct socket *sock,
                        struct sockaddr *addr,
                        int *addrLen,
                        int peer);
static unsigned int MksckPoll(struct file *filp,
                              struct socket *sock,
                              poll_table *wait);
static int MksckDgramConnect(struct socket *sock,
                             struct sockaddr *addr,
                             int addrLen,
                             int flags);
static int MksckMMap(struct file *file,
                     struct socket *sock,
                     struct vm_area_struct *vma);

static void MksckPageRelease(MksckPage *mksckPage);

static struct proto_ops mksckDgramOps = {
   .family     = AF_MKSCK,
   .owner      = THIS_MODULE,
   .release    = MksckRelease,
   .bind       = MksckBind,
   .connect    = MksckDgramConnect,
   .socketpair = sock_no_socketpair,
   .accept     = sock_no_accept,
   .getname    = MksckGetName,
   .poll       = MksckPoll,
   .ioctl      = sock_no_ioctl,
   .listen     = sock_no_listen,
   .shutdown   = sock_no_shutdown, /* MksckShutdown, */
   .setsockopt = sock_no_setsockopt,
   .getsockopt = sock_no_getsockopt,
   .sendmsg    = MksckDgramSendMsg,
   .recvmsg    = MksckDgramRecvMsg,
   .mmap       = MksckMMap,
   .sendpage   = sock_no_sendpage,
};


/**
 * @brief Initialize the MKSCK protocol
 *
 * @return 0 on success, -errno on failure
 */
int
Mksck_Init(void)
{
   int err;

   spin_lock_init(&mksckPageListLock);

   /*
    * Create a slab to allocate socket structs from.
    */
   err = proto_register(&mksckProto, 1);
   if (err != 0) {
      printk(KERN_INFO
             "Mksck_Init: Cannot register MKSCK protocol, errno = %d.\n", err);
      return err;
   }

   /*
    * Register the socket family
    */
   err = sock_register(&mksckFamilyOps);
   if (err < 0) {
      printk(KERN_INFO
            "Mksck_Init: Could not register address family AF_MKSCK"
            " (errno = %d).\n", err);
      return err;
   }

   return 0;
}


/**
 * @brief De-register the MKSCK protocol
 */
void
Mksck_Exit(void)
{
   sock_unregister(mksckFamilyOps.family);
   proto_unregister(&mksckProto);
}


/**
 * @brief Create a new MKSCK socket
 *
 * @param net      network namespace (2.6.24 or above)
 * @param sock     user socket structure
 * @param protocol protocol to be used
 * @param kern     called from kernel mode
 *
 * @return 0 on success, -errno on failure
 */
static int
MksckCreate(struct net *net,
            struct socket *sock,
            int protocol,
            int kern)
{
   struct sock *sk;
   uid_t currentUid = current_euid();

   if (!(currentUid == 0 ||
         currentUid == Mvpkm_vmwareUid)) {
      printk(KERN_WARNING
             "MksckCreate: rejected from process %s tgid=%d, pid=%d euid:%d.\n",
             current->comm,
             task_tgid_vnr(current),
             task_pid_vnr(current),
             currentUid);
      return -EPERM;
   }

   if (!sock) {
      return -EINVAL;
   }

   if (protocol) {
      return -EPROTONOSUPPORT;
   }

   switch (sock->type) {
      case SOCK_DGRAM: {
         sock->ops = &mksckDgramOps;
         break;
      }
      default: {
         return -ESOCKTNOSUPPORT;
      }
   }

   sock->state = SS_UNCONNECTED;

   /*
    * Most recently (in 2.6.24), sk_alloc() was changed to expect the
    * network namespace, and the option to zero the sock was dropped.
    */
   sk = sk_alloc(net, mksckFamilyOps.family, GFP_KERNEL, &mksckProto);

   if (!sk) {
      return -ENOMEM;
   }

   sock_init_data(sock, sk);

   sk->sk_type        = SOCK_DGRAM;
   sk->sk_destruct    = MksckSkDestruct;
   sk->sk_backlog_rcv = MksckBacklogRcv;

   /*
    * On socket lock...
    *
    * A bound socket will have an associated private area, the Mksck
    * structure part of MksckPage. That area is pointed to by
    * sk->sk_protinfo. In addition, a connected socket will have the
    * peer field in its associated area set to point to the associated
    * private area of the peer socket. A mechanism is needed to ensure
    * that these private areas area not freed while they are being
    * accessed within the scope of a function. A simple lock would not
    * suffice as the interface functions (like MksckDgramRecvMsg())
    * may block. Hence a reference count mechanism is employed. When
    * the mentioned references (sk->sk_protinfo and mksck->peer) to
    * the respective private areas are set a refcount is incremented,
    * and decremented when the references are deleted.
    *
    * The refcounts of areas pointed to by sk->sk_protinfo and
    * mksck->peer will be decremented under the lock of the socket.
    * Hence these private areas cannot disappear as long as the socket
    * lock is held.
    *
    * The interface functions will have one of the following
    * structures:
    *
    * simpleFn(sk)
    * {
    *    lock_sock(sk);
    *    if ((mksck = sk->sk_protinfo)) {
    *        <non-blocking use of mksck>
    *    }
    *    release_sock(sk);
    * }
    *
    * complexFn(sk)
    * {
    *    lock_sock(sk);
    *    if ((mksck = sk->sk_protinfo)) {
    *       IncRefc(mksck);
    *    }
    *    release_sock(sk);
    *
    *    if (mksck) {
    *       <use of mksck in a potentially blocking manner>
    *       DecRefc(mksck);
    *    }
    * }
    */
   sk->sk_protinfo = NULL;
   sock_reset_flag(sk, SOCK_DONE);

   return 0;
}


/**
 * @brief Delete a MKSCK socket
 *
 * @param sock user socket structure
 *
 * @return 0 on success, -errno on failure
 */
static int
MksckRelease(struct socket *sock)
{
   struct sock *sk = sock->sk;

   if (sk) {
      lock_sock(sk);
      sock_orphan(sk);
      release_sock(sk);
      sock_put(sk);
   }

   sock->sk = NULL;
   sock->state = SS_FREE;

   return 0;
}


static int
MksckBacklogRcv(struct sock *sk, struct sk_buff *skb)
{
   /*
    * We should never get these as we never queue an skb.
    */
   printk("MksckBacklogRcv: should never get here\n");
   return -EIO;
}


/**
 * @brief Callback at socket destruction
 *
 * @param sk pointer to kernel socket structure
 */
static void
MksckSkDestruct(struct sock *sk)
{
   Mksck *mksck;

   lock_sock(sk);
   mksck = sk->sk_protinfo;

   if (mksck != NULL) {
      sk->sk_protinfo = NULL;
      Mksck_CloseCommon(mksck);
   }

   if (sk->sk_user_data != NULL) {
      sock_kfree_s(sk, sk->sk_user_data, sizeof(int));
      sk->sk_user_data = NULL;
   }

   release_sock(sk);
}


/**
 * @brief Set the local address of a MKSCK socket
 *
 * @param sk   kernel socket structure
 * @param addr the new address of the socket
 *
 * @return 0 on success, -errno on failure
 *
 * If addr.port is undefined a new random port is assigned.
 * If addr.vmId is undefined then the vmId computed from the tgid is used.
 * Hence the vmId of a socket does not determine the host all the time.
 *
 * Assumed that the socket is locked.
 * This function is called by explicit set (MksckBind) and implicit (Send).
 */
static int
MksckBindGeneric(struct sock *sk,
                 Mksck_Address addr)
{
   int err;
   Mksck *mksck;
   MksckPage *mksckPage;

   if (sk->sk_protinfo != NULL) {
      return -EISCONN;
   }

   /*
    * Locate the page for the given host and increment its reference
    * count so it can't get freed off while we are working on it.
    */
   if (addr.vmId == MKSCK_VMID_UNDEF) {
      mksckPage = MksckPage_GetFromTgidIncRefc();
   } else {
      printk(KERN_WARNING "MksckBind: host bind called on vmid 0x%X\n", addr.vmId);
      mksckPage = MksckPage_GetFromVmIdIncRefc(addr.vmId);
   }

   if (mksckPage == NULL) {
      printk(KERN_INFO "MksckBind: no mksckPage for vm 0x%X\n", addr.vmId);
      return -ENETUNREACH;
   }
   addr.vmId = mksckPage->vmId;

   /*
    * Before we can find an unused socket port on the page we have to
    * lock the page for exclusive access so another thread can't
    * allocate the same port.
    */
   err = Mutex_Lock(&mksckPage->mutex, MutexModeEX);
   if (err < 0) {
      goto outDec;
   }

   addr.port = MksckPage_GetFreePort(mksckPage, addr.port);
   if (addr.port == MKSCK_PORT_UNDEF) {
      err = -EINVAL;
      goto outUnlockDec;
   }

   /*
    * At this point we have the mksckPage locked for exclusive access
    * and its reference count incremented.  Also, addr is completely
    * filled in with vmId and port that we want to bind.
    *
    * Find an available mksck struct on the shared page and initialize
    * it.
    */
   mksck = MksckPage_AllocSocket(mksckPage, addr);
   if (mksck == NULL) {
      err = -EMFILE;
      goto outUnlockDec;
   }

   /*
    * Stable, release mutex.  Leave mksckPage->refCount incremented so
    * mksckPage can't be freed until socket is closed.
    */
   Mutex_Unlock(&mksckPage->mutex, MutexModeEX);

   /*
    * This is why we start mksck->refCount at 1.  When sk_protinfo gets
    * cleared, we decrement mksck->refCount.
    */
   sk->sk_protinfo = mksck;

   PRINTK(KERN_DEBUG "MksckBind: socket bound to %08X\n", mksck->addr.addr);

   return 0;

outUnlockDec:
   Mutex_Unlock(&mksckPage->mutex, MutexModeEX);
outDec:
   MksckPage_DecRefc(mksckPage);
   return err;
}


/**
 * @brief Test if the socket is already bound to a local address and,
 *        if not, bind it to an unused address.
 *
 * @param sk   kernel socket structure
 * @return 0 on success, -errno on failure
 *
 * Assumed that the socket is locked.
 */
static inline int
MksckTryBind(struct sock *sk)
{
   int err = 0;

   if (!sk->sk_protinfo) {
      static const Mksck_Address addr = { .addr = MKSCK_ADDR_UNDEF };
      err = MksckBindGeneric(sk, addr);
   }
   return err;
}



/**
 * @brief Set the address of a MKSCK socket (user call)
 *
 * @param sock    user socket structure
 * @param addr    the new address of the socket
 * @param addrLen length of the address
 *
 * @return 0 on success, -errno on failure
 */
static int
MksckBind(struct socket *sock,
          struct sockaddr *addr,
          int addrLen)
{
   int err;
   struct sock *sk            = sock->sk;
   struct sockaddr_mk *addrMk = (struct sockaddr_mk *)addr;

   if (addrLen != sizeof *addrMk) {
      return -EINVAL;
   }
   if (addrMk->mk_family != AF_MKSCK) {
      return -EAFNOSUPPORT;
   }

   /*
    * Obtain the socket lock and call the generic Bind function.
    */
   lock_sock(sk);
   err = MksckBindGeneric(sk, addrMk->mk_addr);
   release_sock(sk);

   return err;
}

/**
 * @brief Lock the peer socket by locating it, incrementing its refc
 * @param addr the address of the peer socket
 * @param[out] peerMksckR set to the locked peer socket pointer
 *                   upon successful lookup
 * @return 0 on success, -errno on failure
 */
static int
LockPeer(Mksck_Address addr, Mksck **peerMksckR)
{
   int        err = 0;
   MksckPage *peerMksckPage = MksckPage_GetFromVmIdIncRefc(addr.vmId);
   Mksck     *peerMksck;

   /*
    * Find corresponding destination shared page and increment its
    * reference count so it can't be freed while we are sending to the
    * socket. Make sure that the address is indeed an address of a
    * monitor/guest socket.
    */
   if (peerMksckPage == NULL) {
      printk(KERN_INFO "LockPeer: vmId %x is not in use!\n", addr.vmId);
      return -ENETUNREACH;
   }
   if (!peerMksckPage->isGuest) {
      MksckPage_DecRefc(peerMksckPage);
      printk(KERN_INFO "LockPeer: vmId %x does not belong to a guest!\n",
             addr.vmId);
      return -ENETUNREACH;
   }


   err = Mutex_Lock(&peerMksckPage->mutex, MutexModeSH);
   if (err < 0) {
      MksckPage_DecRefc(peerMksckPage);
      return err;
   }

   /*
    * Find corresponding destination socket on that shared page and
    * increment its reference count so it can't be freed while we are
    * trying to send to it.
    */
   peerMksck = MksckPage_GetFromAddr(peerMksckPage, addr);

   if (peerMksck) {
      ATOMIC_ADDV(peerMksck->refCount, 1);
      *peerMksckR = peerMksck;
   } else {
      printk(KERN_INFO "LockPeer: addr %x is not a defined socket!\n",
             addr.addr);
      err = -ENETUNREACH;
   }

   Mutex_Unlock(&peerMksckPage->mutex, MutexModeSH);
   MksckPage_DecRefc(peerMksckPage);
   return err;
}

/**
 * @brief Set the peer address of a MKSCK socket
 *
 * @param sock    user socket structure
 * @param addr    the new address of the socket
 * @param addrLen length of the address
 * @param flags flags
 *
 * @return 0 on success, -errno on failure
 */
static int
MksckDgramConnect(struct socket *sock,
                  struct sockaddr *addr,
                  int addrLen,
                  int flags)
{
   struct sock *sk = sock->sk;
   Mksck *mksck;
   struct sockaddr_mk *peerAddrMk = (struct sockaddr_mk *)addr;
   int err = 0;

   if (addrLen != sizeof *peerAddrMk) {
      printk(KERN_INFO "MksckConnect: wrong address length!\n");
      return -EINVAL;
   }
   if (peerAddrMk->mk_family != AF_MKSCK) {
      printk(KERN_INFO "MksckConnect: wrong address family!\n");
      return -EAFNOSUPPORT;
   }

   lock_sock(sk);

   if ((err = MksckTryBind(sk))) {
      goto releaseSock;
   }
   mksck = sk->sk_protinfo;

   /*
    * First severe any past peer connections
    */
   Mksck_DisconnectPeer(mksck);
   sock->state = SS_UNCONNECTED;

   /*
    * Then build new connections ...
    */
   if (peerAddrMk->mk_addr.addr != MKSCK_ADDR_UNDEF) {
      sock->state = SS_CONNECTED;
      mksck->peerAddr = peerAddrMk->mk_addr;
      err = LockPeer(mksck->peerAddr, &mksck->peer);
      PRINTK(KERN_DEBUG "MksckConnect: socket %x is connected to %x!\n",
             mksck->addr.addr,  mksck->peerAddr.addr);
   }

releaseSock:
   release_sock(sk);

   return err;
}


/**
 * @brief returns the address of a MKSCK socket/peer address
 *
 * @param sock    user socket structure
 * @param addr    the new address of the socket
 * @param addrLen length of the address
 * @param peer    1 if the peer address is sought
 *
 * @return 0 on success, -errno on failure
 */
static int
MksckGetName(struct socket *sock,
             struct sockaddr *addr,
             int *addrLen,
             int peer)
{
   int err;
   Mksck *mksck;
   struct sock *sk = sock->sk;

   // MAX_SOCK_ADDR is size of *addr, Linux doesn't export it!
   // ASSERT_ON_COMPILE(sizeof (struct sockaddr_mk) <= MAX_SOCK_ADDR);

   lock_sock(sk);
   mksck = sk->sk_protinfo;

   if (mksck == NULL) {
      if (peer) {
          err = -ENOTCONN;
      } else {
         ((struct sockaddr_mk *)addr)->mk_family    = AF_MKSCK;
         ((struct sockaddr_mk *)addr)->mk_addr.addr = MKSCK_ADDR_UNDEF;
         *addrLen = sizeof (struct sockaddr_mk);
         err = 0;
      }
   } else if (!peer) {
      ((struct sockaddr_mk *)addr)->mk_family = AF_MKSCK;
      ((struct sockaddr_mk *)addr)->mk_addr   = mksck->addr;
      *addrLen = sizeof (struct sockaddr_mk);
      err = 0;
   } else if (mksck->peerAddr.addr == MKSCK_ADDR_UNDEF) {
      err = -ENOTCONN;
   } else {
      ((struct sockaddr_mk *)addr)->mk_family = AF_MKSCK;
      ((struct sockaddr_mk *)addr)->mk_addr   = mksck->peerAddr;
      *addrLen = sizeof (struct sockaddr_mk);
      err = 0;
   }

   release_sock(sk);

   return err;
}


/**
 * @brief VMX polling a receipted packet from VMM.
 *
 * @param filp  kernel file pointer to poll for
 * @param sock  user socket structure
 * @param wait  kernel polling table where to poll if not null
 *
 * @return poll mask state given from socket state.
 */
static unsigned int MksckPoll(struct file *filp,
                              struct socket *sock,
                              poll_table *wait)
{
   struct sock *sk = sock->sk;
   unsigned int mask = 0;
   Mksck *mksck = NULL;
   uint32 read;
   int err;

   lock_sock(sk);
   if ((err = MksckTryBind(sk))) {
      release_sock(sk);
      return err;
   }
   mksck = sk->sk_protinfo;

   /*
    * To avoid mksck disappearing right after the release_sock the
    * refcount needs to be incremented. For more details read the
    * block comment on locking in MksckCreate.
    */
   ATOMIC_ADDV(mksck->refCount, 1);
   release_sock(sk);

   /*
    * Wait to make sure this is the only thread trying to access socket.
    */
   if ((err = Mutex_Lock(&mksck->mutex, MutexModeEX)) < 0) {
      /* we might get in this situation if we are signaled
         (select() may handle this, so leave) */
      PRINTK(KERN_INFO "MksckPoll: try to abort\n");
      return mask;
   }

   /*
    * See if packet in ring.
    */
   read = mksck->read;
   if (read != mksck->write) {
      mask |= POLLIN | POLLRDNORM; /* readable, socket is unlocked */
      /* Note that if we are implementing support for POLLOUT, we SHOULD
         change this Mutex_Unlock by Mutex_UnlPoll, because there is no
         obvious knowledge about the sleepy reason that is intended by user */
      Mutex_Unlock(&mksck->mutex, MutexModeEX);
   } else {
      Mutex_UnlPoll(&mksck->mutex, MutexModeEX, MKSCK_CVAR_FILL, filp, wait);
   }

   /*
    * Note that locking rules differ a little inside MksckPoll, since we are
    * not only given a pointer to the struct socket but also a pointer to a
    * struct file. This means that during the whole operation of this function
    * and during any pending wait (registered with poll_wait()), the file itself
    * is reference counted up, and we should rely on that 'upper' reference
    * counting to prevent from tearing the Mksck down. That holds true since one
    * never re-bind sockets !
    */
   Mksck_DecRefc(mksck);
   return mask;
}

/**
 * @brief Manage a set of Mksck_PageDesc from a message or a stored array.
 *
 * @param pd       set of Mksck_PageDesc
 * @param pages    Mksck_PageDesc pages count for this management operation
 * @param incr     ternary used to indicate if we want to reference (+1), or
 *                 dereference (-1), or count (0) 4k pages
 *
 * @return length of bytes processed.
 */
static size_t
MksckPageDescManage(Mksck_PageDesc *pd,
                    uint32 pages,
                    int incr)
{
   size_t payloadLen = 0;
   uint32 i;

   for (i = 0; i < pages && pd[i].mpn != INVALID_MPN; ++i) {
      uint32 j;

      for (j = 0; j < 1 << pd[i].order; ++j) {
         struct page *page;
         MPN currMPN = pd[i].mpn + j;

         /*
          * The monitor tried to send an invalid MPN, bad.
          */
         if (!pfn_valid(currMPN)) {
            printk("MksckPageDescManage: Invalid MPN %x\n", currMPN);
         } else {
            page = pfn_to_page(currMPN);

            if (incr == +1) {
               get_page(page);
            }
            if (incr == -1) {
               put_page(page);
            }
         }

         payloadLen += PAGE_SIZE;
      }
   }

   return payloadLen;
}

/**
 * @brief Management values to be used as third parameter of MksckPageDescManage
 */
#define MANAGE_INCREMENT +1
#define MANAGE_DECREMENT -1
#define MANAGE_COUNT      0


/**
 * @brief Map a set of Mksck_PageDesc from a message or a stored array.
 *
 * @param pd       set of Mksck_PageDesc
 * @param pages    pages count for this mapping
 * @param iov      vectored user virtual addresses of the recv commands
 * @param iovCount size for iov parameter
 * @param vma      virtual memory area used for the mapping, note that
 *                 this is mandatorily required MksckPageDescMap is used
 *                 on an indirect PageDesc context (i.e whenever iov is
 *                 not computed by the kernel but by ourselves).
 *
 * Since find_vma() and vm_insert_page() are used, this function must
 * be called with current's mmap_sem locked, or inside an MMap operation.
 *
 * @return length of bytes mapped.
 */
static size_t
MksckPageDescMap(Mksck_PageDesc *pd,
                 uint32 pages,
                 struct iovec *iov,
                 int iovCount,
                 struct vm_area_struct *vma)
{
   size_t payloadLen = 0;
   uint32 i;

   for (i = 0; i < pages && pd[i].mpn != INVALID_MPN; ++i) {
      uint32 j;

      for (j = 0; j < 1 << pd[i].order; ++j) {
         HUVA huva = 0;
         struct page *page;
         MPN currMPN = pd[i].mpn + j;

         while (iovCount > 0 && iov->iov_len == 0) {
            iovCount--;
            iov++;
         }

         if (iovCount == 0) {
            printk("MksckPageDescMap: Invalid iov length\n");
            goto map_done;
         }

         huva = (HUVA)iov->iov_base;

         /*
          * iovecs for receiving the typed component of the message should
          * have page aligned base and size sufficient for page descriptor's
          * mappings.
          */
         if (huva & (PAGE_SIZE - 1) || iov->iov_len < PAGE_SIZE) {
            printk("MksckPageDescMap: Invalid huva %x or iov_len %d\n",
                   huva,
                   iov->iov_len);
            goto map_done;
         }

         /*
          * Might be in a new vma...
          */
         if (vma == NULL || huva < vma->vm_start || huva >= vma->vm_end) {
            vma = find_vma(current->mm, huva);

            /*
             * Couldn't find a matching vma for huva.
             */
            if (vma == NULL ||
                huva < vma->vm_start ||
                vma->vm_ops != &mksckVMOps) {
               printk("MksckPageDescMap: Invalid vma\n");
               goto map_done;
            }
         }

         /*
          * The monitor tried to send an invalid MPN, bad.
          */
         if (!pfn_valid(currMPN)) {
            printk("MksckPageDescMap: Invalid MPN %x\n", currMPN);
         } else {
            int rc;

            page = pfn_to_page(currMPN);

            /*
             * Map into the receive window.
             */
            rc = vm_insert_page(vma, huva, page);
            if (rc) {
               printk("MksckPageDescMap: Failed to insert %x at %x, error %d\n",
                      currMPN,
                      huva,
                      rc);
               goto map_done;
            }

            ASSERT(iov->iov_len >= PAGE_SIZE);
            iov->iov_base += PAGE_SIZE;
            iov->iov_len -= PAGE_SIZE;
         }

         payloadLen += PAGE_SIZE;
      }
   }

map_done:
   return payloadLen;
}


/**
 * @brief Check if the provided MsgHdr has still room for a receive operation.
 *
 * @param msg   user buffer
 * @return 1 if MsgHdr has IO space room in order to receive a mapping, 0 otherwise.
 */
static int
MsgHdrHasAvailableRoom(struct msghdr *msg)
{
   struct iovec *vec = msg->msg_iov;
   uint32 count = msg->msg_iovlen;

   while (count > 0 && vec->iov_len == 0) {
      count--;
      vec++;
   }

   return (count != 0);
}


/**
 * Whenever a typed message is received from the monitor, we may choose to store
 * all the page descriptor content in a linked state of descriptors, through the
 * following information context
 */
typedef struct MksckPageDescInfo {
   struct MksckPageDescInfo *next;
   uint32 flags;
   uint32 pages;
   uint32 mapCounts;
   Mksck_PageDesc descs[0];
} MksckPageDescInfo;

static void MksckPageDescSkDestruct(struct sock *sk);
static int MksckPageDescMMap(struct file *file,
                             struct socket *sock,
                             struct vm_area_struct *vma);
static int MksckPageDescIoctl(struct socket *sock,
                              unsigned int cmd,
                              unsigned long arg);

/**
 * @brief Delete a page descriptor container socket
 *
 * @param sock user socket structure
 * @return 0 on success, -errno on failure
 */
static int
MksckPageDescRelease(struct socket *sock)
{
   /* This is generic socket release */
   struct sock *sk = sock->sk;

   if (sk) {
      lock_sock(sk);
      sock_orphan(sk);
      release_sock(sk);
      sock_put(sk);
   }

   sock->sk = NULL;
   sock->state = SS_FREE;

   return 0;
}


/**
 * Whenever a typed message is received from the monitor, we may choose to store
 * all the page descriptor content for a future mapping. One shall put a context
 * usable by host userland, that means trough a file descriptor, and as a secure
 * implementation we choose to define a strict set of operations that are used
 * only for that purpose. This set of operation is reduced to leaving the
 * default "PageDesc(s) accumulating" mode (inside ioctl), mapping the context,
 * and generic socket destruction.
 */
static struct proto_ops mksckPageDescOps = {
   .family     = AF_MKSCK,
   .owner      = THIS_MODULE,
   .release    = MksckPageDescRelease,
   .bind       = sock_no_bind,
   .connect    = sock_no_connect,
   .socketpair = sock_no_socketpair,
   .accept     = sock_no_accept,
   .getname    = sock_no_getname,
   .poll       = sock_no_poll,
   .ioctl      = MksckPageDescIoctl,
   .listen     = sock_no_listen,
   .shutdown   = sock_no_shutdown,
   .setsockopt = sock_no_setsockopt,
   .getsockopt = sock_no_getsockopt,
   .sendmsg    = sock_no_sendmsg,
   .recvmsg    = sock_no_recvmsg,
   .mmap       = MksckPageDescMMap,
   .sendpage   = sock_no_sendpage,
};


/**
 * @brief Create or accumulate to a PageDesc context, backed as a descriptor.
 *
 * @param sock  user socket structure
 * @param msg   user buffer to receive the file descriptor as ancillary data
 * @param pd    source descriptor part of a message
 * @param pages pages count for this mapping
 *
 * @return error if negative,  0 otherwise
 *
 */
static int
MksckPageDescToFd(struct socket *sock,
                  struct msghdr *msg,
                  Mksck_PageDesc *pd,
                  uint32 pages)
{
   int retval;
   int newfd;
   struct socket *newsock;
   struct sock *newsk;
   struct sock *sk = sock->sk;
   MksckPageDescInfo **pmpdi, *mpdi;
   lock_sock(sk);

   /*
    * Relation between any mk socket and the PageDesc context is as follow:
    *
    * From the mk socket to the PageDesc context:
    * - sk->sk_user_data is a WEAK LINK, containing only a file descriptor
    *                    numerical value such that accumulating is keyed on it.
    *
    * From the PageDesc context to the mk socket:
    * - sk->sk_protinfo contains a MksckPageDescInfo struct.
    * - sk->sk_user_data is a pointer REF-COUNTED sock_hold() LINK, also it is
    *                    rarely dereferenced but usually used to check that the
    *                    right socket pair is used. Full dereferencing is used
    *                    only to break the described links.
    */
   if (sk->sk_user_data) {
      MksckPageDescInfo *mpdi2;

      /* continue any previous on-going mapping, i.e accumulate */
      newfd = *((int *)sk->sk_user_data);
      newsock = sockfd_lookup(newfd, &retval); // promote the weak link
      if (!newsock) {
         retval = -EINVAL;
         goto endProcessingReleaseSock;
      }

      newsk = newsock->sk;
      lock_sock(newsk);
      sockfd_put(newsock);

      if (((struct sock *)newsk->sk_user_data) != sk) {
         /* One way of going into this situation would be for userland to dup
            the file descriptor just received, close the original number, and
            open a new mk socket in the very same spot. The userland code have
            a lot of way of interacting with the kernel without this driver
            code to be notified. */
         retval = -EINVAL;
         release_sock(newsk);
         goto endProcessingReleaseSock;
      }

      mpdi = sock_kmalloc(newsk, sizeof(MksckPageDescInfo) +
                          pages*sizeof(Mksck_PageDesc), GFP_KERNEL);
      if (IS_ERR(mpdi)) {
         retval = PTR_ERR(mpdi);
         release_sock(newsk);
         goto endProcessingReleaseSock;
      }

      /* There is no mandatory needs for us to notify userland from
         the progress in "appending" to the file descriptor, but it
         would feel strange if the userland would have no mean to
         tell if the received message was just not thrown away. So, in
         order to be consistent one fill the ancillary message while
         "creating" and "appending to" file descriptors. */
      retval = put_cmsg(msg, SOL_DECNET, 0, sizeof(int), &newfd);
      if (retval < 0) {
         goto endProcessingKFreeReleaseSock;
      }

      release_sock(sk);

      mpdi2 = (MksckPageDescInfo *)newsk->sk_protinfo;
      while (mpdi2->next) {
         mpdi2 = mpdi2->next;
      }
      pmpdi = &(mpdi2->next);

   } else {
      /* Create a new socket, new context and a new file descriptor. */
      retval = sock_create(sk->sk_family, sock->type, 0, &newsock);
      if (retval < 0) {
         goto endProcessingReleaseSock;
      }

      newsk = newsock->sk;
      lock_sock(newsk);
      newsk->sk_destruct = &MksckPageDescSkDestruct;
      newsk->sk_user_data = sk;
      sock_hold(sk); // keeps a reference to parent mk socket
      newsock->ops = &mksckPageDescOps;

      mpdi = sock_kmalloc(newsk, sizeof(MksckPageDescInfo) +
                          pages*sizeof(Mksck_PageDesc), GFP_KERNEL);
      if (IS_ERR(mpdi)) {
         retval = PTR_ERR(mpdi);
         goto endProcessingFreeNewSock;
      }

      sk->sk_user_data = sock_kmalloc(sk, sizeof(int), GFP_KERNEL);
      if (IS_ERR(sk->sk_user_data)) {
         retval = PTR_ERR(sk->sk_user_data);
         sk->sk_user_data = NULL;
         goto endProcessingKFreeAndNewSock;
      }

      /* mapping to a file descriptor may fail if a thread is closing
         in parallel of sock_map_fd/sock_alloc_fd, or kernel memory is full */
      newfd = sock_map_fd(newsock, O_CLOEXEC);
      if (newfd < 0) {
         retval = newfd;
         sock_kfree_s(sk, sk->sk_user_data, sizeof(int));
         sk->sk_user_data = NULL;
         goto endProcessingKFreeAndNewSock;
      }

      /* notify userland from a new file descriptor, alike AF_UNIX ancillary */
      retval = put_cmsg(msg, SOL_DECNET, 0, sizeof(int), &newfd);
      if (retval < 0) {
         sock_kfree_s(sk, sk->sk_user_data, sizeof(int));
         sk->sk_user_data = NULL;
         sock_kfree_s(newsk, mpdi, sizeof(MksckPageDescInfo) +
                      mpdi->pages*sizeof(Mksck_PageDesc));
         release_sock(newsk);
         sockfd_put(newsock);
         sock_release(newsock);
         put_unused_fd(newfd);
         goto endProcessingReleaseSock;
      }

      *(int*)sk->sk_user_data = newfd;
      release_sock(sk);
      pmpdi = (MksckPageDescInfo **)(&(newsk->sk_protinfo));
   }

   mpdi->next  = NULL;
   mpdi->flags = 0;
   mpdi->mapCounts = 0;
   mpdi->pages = pages;
   memcpy(mpdi->descs, pd, pages*sizeof(Mksck_PageDesc));

   *pmpdi = mpdi; // link
   release_sock(newsk);

   /* increment all reference counters for the pages */
   MksckPageDescManage(pd, pages, MANAGE_INCREMENT);
   return 0;

endProcessingKFreeAndNewSock:
   sock_kfree_s(newsk, mpdi, sizeof(MksckPageDescInfo) +
                mpdi->pages*sizeof(Mksck_PageDesc));
endProcessingFreeNewSock:
   release_sock(newsk);
   sock_release(newsock);
   release_sock(sk);
   return retval;

endProcessingKFreeReleaseSock:
   sock_kfree_s(newsk, mpdi, sizeof(MksckPageDescInfo) +
                mpdi->pages*sizeof(Mksck_PageDesc));
   release_sock(newsk);
endProcessingReleaseSock:
   release_sock(sk);
   return retval;
}

/**
 * @brief Callback at socket destruction
 *
 * @param sk pointer to kernel socket structure
 */
static void
MksckPageDescSkDestruct(struct sock *sk)
{
   struct sock *mkSk = NULL;
   MksckPageDescInfo *mpdi;
   lock_sock(sk);
   mpdi = sk->sk_protinfo;
   while (mpdi) {
      MksckPageDescInfo *next = mpdi->next;
      MksckPageDescManage(mpdi->descs, mpdi->pages,
                          MANAGE_DECREMENT);
      sock_kfree_s(sk, mpdi, sizeof(MksckPageDescInfo) +
                   mpdi->pages*sizeof(Mksck_PageDesc));
      mpdi = next;
   }
   if (sk->sk_user_data) {
      mkSk = (struct sock *)sk->sk_user_data;
      sk->sk_user_data = NULL;
   }
   sk->sk_protinfo  = NULL;
   release_sock(sk);
   /* clean the monki socket that we are holding */
   if (mkSk) {
      lock_sock(mkSk);
      sock_kfree_s(mkSk, mkSk->sk_user_data, sizeof(int));
      mkSk->sk_user_data = NULL;
      release_sock(mkSk);
      sock_put(mkSk); // revert of sock_hold()
   }
}

/**
 * @brief The mmap operation of the PageDesc context file descriptor.
 *
 * The mmap command is used to mmap any detached (i.e. no more accumulating)
 * PageDesc context, full of the content from its parent communication mk
 * socket. Mapping may be done a specified number of times, so that the
 * PageDesc context could become useless (as a security restriction).
 *
 * Also note that mapping from an offset different from zero is considered
 * as a userland invalid operation.
 *
 * @param file  user file structure
 * @param sock  user socket structure
 * @param vma   virtual memory area structure
 *
 * @return error code, 0 on success
 */
static int
MksckPageDescMMap(struct file *file,
                  struct socket *sock,
                  struct vm_area_struct *vma)
{
   struct sock *sk = sock->sk;
   MksckPageDescInfo *mpdi;
   struct iovec iov;
   unsigned long vm_flags;
   int freed = 0;

   iov.iov_base = (void*)vma->vm_start;
   iov.iov_len  = vma->vm_end - vma->vm_start;

   lock_sock(sk);
   mpdi = sk->sk_protinfo;

   // vma->vm_pgoff is checked, since offsetting the map is not supported
   if (!mpdi || sk->sk_user_data || vma->vm_pgoff) {
      release_sock(sk);
      printk(KERN_INFO "MMAP failed for virt %lx size %lx\n",
             vma->vm_start, vma->vm_end - vma->vm_start);
      return -EINVAL;
   }

   vm_flags = mpdi->flags;
   if ((vma->vm_flags & ~vm_flags) & (VM_READ|VM_WRITE)) {
      release_sock(sk);
      return -EACCES;
   }

   while (mpdi) {
      MksckPageDescInfo *next = mpdi->next;
      MksckPageDescMap(mpdi->descs, mpdi->pages, &iov, 1, vma);
      if (mpdi->mapCounts && !--mpdi->mapCounts) {
         MksckPageDescManage(mpdi->descs, mpdi->pages,
                             MANAGE_DECREMENT);
         sock_kfree_s(sk, mpdi, sizeof(MksckPageDescInfo) +
                      mpdi->pages*sizeof(Mksck_PageDesc));
         freed = 1;
      }
      mpdi = next;
   }

   if (freed) {
      sk->sk_protinfo  = NULL;
   }
   vma->vm_ops = &mksckVMOps;
   release_sock(sk);
   return 0;
}

/**
 * @brief The ioctl operation of the PageDesc context file descriptor.
 *
 * The ioctl MKSCK_DETACH command is used to detach the PageDesc context
 * from its parent communication mk socket. Once done, the context
 * is able to remap the transferred PageDesc(s) of typed messages accumulated
 * into the context.
 *
 * @param sock  user socket structure
 * @param cmd   select which cmd function needs to be performed
 * @param arg   argument for command
 *
 * @return error code, 0 on success
 */
static int
MksckPageDescIoctl(struct socket *sock,
                   unsigned int  cmd,
                   unsigned long arg)
{
   struct sock *monkiSk = NULL;
   struct sock *sk = sock->sk;
   MksckPageDescInfo *mpdi;
   int retval = 0;

   switch (cmd) {
      /**
       * ioctl MKSCK_DETACH (in and out):
       * Detach, compute size and define allowed protection access rights
       *
       * [in]:  unsigned long flags, similar to prot argument of mmap()
       *        unsigned long number of available further mappings
       *           with 0 meaning unlimited number of mappings
       * [out]: unsigned long size of the available mappable area
       */
      case MKSCK_DETACH: {
         unsigned long ul[2];
         lock_sock(sk);
         mpdi = sk->sk_protinfo;
         // read unsigned long argument that contains the mmap alike flags
         if (copy_from_user(ul, (void *)arg, sizeof ul)) {
            retval = -EFAULT;
         // check that the file descriptor has a parent and some context there
         } else if (!mpdi || !sk->sk_user_data) {
            retval = -EINVAL;
         } else {
            /* compute mapping protection bits from argument and size of the
             * mapping, that is also given back to userland as unsigned long.
             */
            uint32 flags = calc_vm_prot_bits(ul[0]);
            ul[0] = 0;
            while (mpdi) {
               MksckPageDescInfo *next = mpdi->next;
               ul[0] += MksckPageDescManage(mpdi->descs, mpdi->pages,
                                            MANAGE_COUNT);
               mpdi->mapCounts = ul[1];
               mpdi = next;
            }
            if (copy_to_user((void *)arg, ul, sizeof(ul[0]))) {
               retval = -EFAULT;
            } else {
               mpdi = sk->sk_protinfo;
               mpdi->flags = flags;
               monkiSk = (struct sock *)sk->sk_user_data;
               sk->sk_user_data = NULL;
            }
         }
         release_sock(sk);
         // clean the monki socket that we are holding
         if ((sk = monkiSk)) {
            lock_sock(sk);
            sock_kfree_s(sk, sk->sk_user_data, sizeof(int));
            sk->sk_user_data = NULL;
            release_sock(sk);
            sock_put(sk);
         }
         break;
      }
      default: {
         retval = -EINVAL;
         break;
      }
   }
   return retval;
}


/**
 * @brief VMX receiving a packet from VMM.
 *
 * @param kiocb kernel io control block (unused)
 * @param sock  user socket structure
 * @param msg   user buffer to receive the packet
 * @param len   size of the user buffer
 * @param flags flags
 *
 * @return -errno on failure, else length of untyped portion + total number
 *           of bytes mapped for typed portion.
 */
static int
MksckDgramRecvMsg(struct kiocb *kiocb,
                  struct socket *sock,
                  struct msghdr *msg,
                  size_t len,
                  int flags)
{
   int err = 0;
   struct sock *sk = sock->sk;
   Mksck *mksck;
   Mksck_Datagram *dg;
   struct sockaddr_mk *fromAddr;
   uint32 read;
   struct iovec *iov;
   size_t payloadLen, untypedLen;
   uint32 iovCount;

   if (flags & MSG_OOB || flags & MSG_ERRQUEUE) {
      return -EOPNOTSUPP;
   }

   if ((msg->msg_name != NULL) && (msg->msg_namelen < sizeof *fromAddr)) {
      return -EINVAL;
   }

   lock_sock(sk);
   if ((err = MksckTryBind(sk))) {
      release_sock(sk);
      return err;
   }
   mksck = sk->sk_protinfo;

   /*
    * To avoid mksck disappearing right after the release_sock the
    * refcount needs to be incremented. For more details read the
    * block comment on locking in MksckCreate.
    */
   ATOMIC_ADDV(mksck->refCount, 1);
   release_sock(sk);

   /*
    * Get pointer to next packet in ring to be dequeued.
    */
   while (1) {

      /*
       * Wait to make sure this is the only thread trying to access socket.
       */
      if ((err = Mutex_Lock(&mksck->mutex, MutexModeEX)) < 0) {
         goto decRefc;
      }

      /*
       * See if packet in ring.
       */
      read = mksck->read;
      if (read != mksck->write) {
         break;
      }

      /*
       * Nothing there, if user wants us not to block then just return EAGAIN.
       */
      if (flags & MSG_DONTWAIT) {
         Mutex_Unlock(&mksck->mutex, MutexModeEX);
         err = -EAGAIN;
         goto decRefc;
      }

      /*
       * Nothing there, unlock socket and wait for data.
       */
      mksck->foundEmpty ++;
      err = Mutex_UnlSleep(&mksck->mutex, MutexModeEX, MKSCK_CVAR_FILL);
      if (err < 0) {
         PRINTK(KERN_INFO "MksckDgramRecvMsg: aborted\n");
         goto decRefc;
      }
   }

   /*
    * Point to packet in ring.
    */
   dg = (void *)&mksck->buff[read];

   /*
    * Provide the address of the sender.
    */
   if (msg->msg_name != NULL) {
      fromAddr            = (void *)msg->msg_name;
      fromAddr->mk_addr   = dg->fromAddr;
      fromAddr->mk_family = AF_MKSCK;
      msg->msg_namelen    = sizeof *fromAddr;
   } else {
      msg->msg_namelen = 0;
   }

   /*
    * Copy data from ring buffer to caller's buffer and remove packet from
    * ring buffer.
    */
   iov = msg->msg_iov;
   iovCount = msg->msg_iovlen;
   payloadLen = untypedLen =
      dg->len - dg->pages * sizeof(Mksck_PageDesc) - dg->pad;

   /*
    * Handle the untyped portion of the message.
    */
   if (untypedLen <= len) {
      err = memcpy_toiovec(iov,
                           dg->data,
                           untypedLen);
      if (err < 0) {
         printk("MksckDgramRecvMsg: Failed to memcpy_to_iovec untyped message component "
                "(buf len %d datagram len %d (untyped %d))\n",
                len,
                dg->len,
                untypedLen);
      }
   } else {
      err = -EINVAL;
   }

   /*
    * Map in the typed descriptor.
    */
   if (err >= 0 && dg->pages > 0) {
      Mksck_PageDesc *pd = (Mksck_PageDesc *)(dg->data + untypedLen + dg->pad);

      /*
       * There are 3 ways of receiving typed messages from the monitor.
       * - The typed message is mapped directly into a VMA. To indicate this the
       *   userland sets msg_controllen == 0.
       * - The typed message is mapped directly into a VMA and a file descriptor
       *   created for further mappings on the host (in same userland address
       *   space or an alternate userland address space). In this case
       *   msg_controllen should be set to sizeof(fd).
       * - The typed message is not mapped directly into a VMA, but a file
       *   descriptor is created for later mapping on the host. In this case
       *   msg_controllen should be set to sizeof(fd) and the supplied iovec
       *   shall not specify a receive window.
       *
       * The conjuncts below decide on which of these 3 cases we've encountered.
       */

      if ((msg->msg_controllen <= 0) ||
          ((err = MksckPageDescToFd(sock, msg, pd, dg->pages)) != 0) ||
          (MsgHdrHasAvailableRoom(msg) != 0)) {

         down_write(&current->mm->mmap_sem); // lock for a change of mapping
         payloadLen += MksckPageDescMap(pd, dg->pages, iov, iovCount, NULL);
         up_write(&current->mm->mmap_sem);
      }
   }

   /*
    * Now that packet is removed, it is safe to unlock socket so another thread
    * can do a recv().  We also want to wake someone waiting for room to insert
    * a new packet.
    */
   if ((err >= 0) && Mksck_IncReadIndex(mksck, read, dg)) {
      Mutex_UnlWake(&mksck->mutex, MutexModeEX, MKSCK_CVAR_ROOM, true);
   } else {
      Mutex_Unlock(&mksck->mutex, MutexModeEX);
   }

   /*
    * If memcpy error, return error status.
    * Otherwise, return number of bytes copied.
    */
   if (err >= 0) {
      err = payloadLen;
   }

decRefc:
   Mksck_DecRefc(mksck);
   return err;
}


/**
 * @brief VMX sending a packet to VMM.
 *
 * @param kiocb kernel io control block
 * @param sock  user socket structure
 * @param msg   packet to be transmitted
 * @param len   length of the packet
 *
 * @return length of the sent msg on success, -errno on failure
 */
static int
MksckDgramSendMsg(struct kiocb *kiocb,
                  struct socket *sock,
                  struct msghdr *msg,
                  size_t len)
{
   int             err = 0;
   struct sock    *sk = sock->sk;
   Mksck          *peerMksck;
   Mksck_Datagram *dg;
   uint32          needed;
   uint32          write;
   Mksck_Address   fromAddr;

   if (msg->msg_flags & MSG_OOB) {
      return -EOPNOTSUPP;
   }

   if (len > MKSCK_XFER_MAX) {
      return -EMSGSIZE;
   }

   /*
    * In the next locked section peerMksck pointer needs to be set and
    * its refcount needs to be incremented.
    */
   lock_sock(sk);
   do {
      Mksck *mksck;
      Mksck_Address peerAddr =
         { .addr = (msg->msg_name ?
                    ((struct sockaddr_mk *)msg->msg_name)->mk_addr.addr :
                    MKSCK_ADDR_UNDEF) };

      if ((err = MksckTryBind(sk))) {
         break;
      }
      mksck = sk->sk_protinfo;
      fromAddr = mksck->addr;

      /*
       * If the socket is connected, use that address (no sendto for
       * connected sockets). Otherwise, use the provided address if any.
       */
       if ((peerMksck = mksck->peer)) {
         if (peerAddr.addr != MKSCK_ADDR_UNDEF &&
             peerAddr.addr != mksck->peerAddr.addr) {
            err = -EISCONN;
            break;
         }
         /*
          * To avoid mksckPeer disappearing right after the
          * release_sock the refcount needs to be incremented. For
          * more details read the block comment on locking in
          * MksckCreate.
          */
         ATOMIC_ADDV(peerMksck->refCount, 1);
       } else if (peerAddr.addr == MKSCK_ADDR_UNDEF) {
          err = -ENOTCONN;
       } else {
          /*
           * LockPeer also increments the refc on the peer.
           */
          err = LockPeer(peerAddr, &peerMksck);
       }
   } while(0);
   release_sock(sk);

   if (err) {
      return err;
   }

   /*
    * Get pointer to sufficient empty space in ring buffer.
    */
   needed = MKSCK_DGSIZE(len);
   while (1) {
      /*
       * Wait to make sure this is the only thread trying to write to ring.
       */
      if ((err = Mutex_Lock(&peerMksck->mutex, MutexModeEX)) < 0) {
         goto decRefc;
      }

      /*
       * Check if socket can receive data.
       */
      if (peerMksck->shutDown & MKSCK_SHUT_RD) {
         err = -ENOTCONN;
         goto unlockDecRefc;
      }

      /*
       * See if there is room for the packet.
       */
      write = Mksck_FindSendRoom(peerMksck, needed);
      if (write != MKSCK_FINDSENDROOM_FULL) {
         break;
      }

      /*
       * No room, unlock socket and maybe wait for room.
       */
      if (msg->msg_flags & MSG_DONTWAIT) {
         err = -EAGAIN;
         goto unlockDecRefc;
      }

      peerMksck->foundFull ++;
      err = Mutex_UnlSleep(&peerMksck->mutex,
                           MutexModeEX,
                           MKSCK_CVAR_ROOM);
      if (err < 0) {
         PRINTK(KERN_INFO "MksckDgramSendMsg: aborted\n");
         goto decRefc;
      }
   }

   /*
    * Point to room in ring and fill in message.
    */
   dg = (void *)&peerMksck->buff[write];

   dg->fromAddr = fromAddr;
   dg->len      = len;

   if ((err = memcpy_fromiovec(dg->data, msg->msg_iov, len)) != 0) {
      goto unlockDecRefc;
   }

   /*
    * Increment past message.
    */
   Mksck_IncWriteIndex(peerMksck, write, needed);

   /*
    * Unlock socket and wake someone trying to receive, ie, we filled
    * in a message.
    */
   Mutex_UnlWake(&peerMksck->mutex, MutexModeEX, MKSCK_CVAR_FILL, false);

   /*
    * Maybe guest is in a general 'wait for interrupt' wait or
    * grinding away executing guest instructions.
    *
    * If it has a receive callback armed for the socket and is
    * waiting a message, just wake it up.  Else send an IPI to the CPU
    * running the guest so it will interrupt whatever it is doing and
    * read the message.
    *
    * Holding the mksckPage->mutex prevents mksckPage->vmHKVA from
    * clearing on us.
    */
   if (peerMksck->rcvCBEntryMVA != 0) {
      MksckPage *peerMksckPage = Mksck_ToSharedPage(peerMksck);

      if ((err = Mutex_Lock(&peerMksckPage->mutex, MutexModeSH)) == 0) {
         uint32 sockIdx = peerMksck->index;
         MvpkmVM *vm = (MvpkmVM *) peerMksckPage->vmHKVA;

         /*
          * The destruction of vm and wsp is blocked by the
          * mksckPage->mutex.
          */
         if (vm) {
            WorldSwitchPage *wsp = vm->wsp;

            ASSERT(sockIdx < 8 * sizeof peerMksckPage->wakeVMMRecv);
            ATOMIC_ORV(peerMksckPage->wakeVMMRecv, 1U << sockIdx);

            if (wsp) {
               Mvpkm_WakeGuest(vm, ACTION_MKSCK);
            }
         }
         Mutex_Unlock(&peerMksckPage->mutex, MutexModeSH);
      }
   }

   /*
    * If all are happy tell the caller the number of transferred bytes.
    */
   if (!err) {
      err = len;
   }

   /*
    * Now that we are done with target socket, allow it to be freed.
    */
decRefc:
   Mksck_DecRefc(peerMksck);
   return err;

unlockDecRefc:
   Mutex_Unlock(&peerMksck->mutex, MutexModeEX);
   goto decRefc;
}


/**
 * @brief Page fault handler for receive windows. Since the host process
 *        should not be faulting in this region and only be accessing
 *        memory that has been established via a typed message transfer,
 *        we always signal the fault back to the process.
 */
static int
MksckFault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
   return VM_FAULT_SIGBUS;
}

/**
 * @brief Establish a region in the host process suitable for use as a
 *        receive window.
 *
 * @param file file reference (ignored).
 * @param sock user socket structure.
 * @param vma Linux virtual memory area defining the region.
 *
 * @return 0 on success, otherwise error code.
 */
static int
MksckMMap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
{
   /*
    * All the hard work is done in MksckDgramRecvMsg. Here we simply mark the
    * vma as belonging to Mksck.
    */
   vma->vm_ops = &mksckVMOps;

   return 0;
}

/**
 * @brief This gets called after returning from the monitor.
 *        Since the monitor doesn't directly wake VMX threads when it sends
 *        something to VMX (for efficiency), this routine checks for the
 *        omitted wakes and does them.
 * @param mksckPage some shared page that the monitor writes packets to, ie
 *                  an host shared page
 */
void
Mksck_WakeBlockedSockets(MksckPage *mksckPage)
{
   Mksck *mksck;
   uint32 i, wakeHostRecv;

   wakeHostRecv = mksckPage->wakeHostRecv;
   if (wakeHostRecv != 0) {
      mksckPage->wakeHostRecv = 0;
      for (i = 0; wakeHostRecv != 0; i ++) {
         if (wakeHostRecv & 1) {
             mksck = &mksckPage->sockets[i];
             Mutex_CondSig(&mksck->mutex, MKSCK_CVAR_FILL, true);
         }
         wakeHostRecv >>= 1;
      }
   }
}

/**
 * @brief allocate and initialize a shared page.
 * @return pointer to shared page.<br>
 *         NULL on error
 */
MksckPage *
MksckPageAlloc(void)
{
   uint32 jj;
   /*
    * Ask for pages in the virtual kernel space. There is no
    * requirement to be physically contiguous.
    */
   MksckPage *mksckPage = vmalloc(MKSCKPAGE_SIZE);

   if (mksckPage) {

      /*
       * Initialize its contents.  Start refCount at 1 and decrement it
       * when the worldswitch or VM page gets freed.
       */
      memset(mksckPage, 0, MKSCKPAGE_SIZE);
      ATOMIC_SETV(mksckPage->refCount, 1);
      mksckPage->portStore = MKSCK_PORT_HIGH;

      Mutex_Init(&mksckPage->mutex);
      for (jj = 0; jj<MKSCK_SOCKETS_PER_PAGE; jj++) {
         Mutex_Init(&mksckPage->sockets[jj].mutex);
      }
   }

   return mksckPage;
}

/**
 * @brief Release the allocated pages.
 * @param mksckPage the address of the mksckPage to be released
 */
static void
MksckPageRelease(MksckPage *mksckPage)
{
   int ii;

   for (ii = 0; ii<MKSCK_SOCKETS_PER_PAGE; ii++) {
      Mutex_Destroy(&mksckPage->sockets[ii].mutex);
   }
   Mutex_Destroy(&mksckPage->mutex);

   vfree(mksckPage);
}

/**
 * @brief Using the tgid locate the vmid of this process.
 *        Assumed that mksckPageListLock is held
 * @return the vmId if page is already allocated,
 *         the first vacant vmid if not yet allocated.<br>
 *         MKSCK_PORT_UNDEF if no slot is vacant
 */
static inline Mksck_VmId
GetHostVmId(void)
{
   uint32 jj;
   Mksck_VmId vmId, vmIdFirstVacant = MKSCK_VMID_UNDEF;
   MksckPage *mksckPage;
   uint32 tgid = task_tgid_vnr(current);
   /*
    * Assign an unique vmId to the shared page. Start the search from
    * the vmId that is the result of hashing tgid to 15 bits. As a
    * used page with a given vmId can occupy only a given slot in the
    * mksckPages array, it is enough to search through the
    * MKSCK_MAX_SHARES slots for a vacancy.
    */
   for (jj = 0, vmId = MKSCK_TGID2VMID(tgid);
        jj < MKSCK_MAX_SHARES;
        jj++, vmId++) {
      if (vmId > MKSCK_VMID_HIGH) {
         vmId = 0;
      }
      mksckPage = mksckPages[MKSCK_VMID2IDX(vmId)];

      if (mksckPage) {
         if (mksckPage->tgid == tgid &&
             !mksckPage->isGuest) {
            return mksckPage->vmId;
         }

      } else if (vmIdFirstVacant == MKSCK_VMID_UNDEF) {
         vmIdFirstVacant = vmId;
      }
   }
   return vmIdFirstVacant;
}


/**
 * @brief Locate the first empty slot
 *        Assumed that mksckPageListLock is held
 * @return the first vacant vmid.<br>
 *         MKSCK_PORT_UNDEF if no slot is vacant
 */
static inline Mksck_VmId
GetNewGuestVmId(void)
{
   Mksck_VmId vmId;

   for (vmId = 0; vmId < MKSCK_MAX_SHARES; vmId++) {
      if (!mksckPages[MKSCK_VMID2IDX(vmId)]) {
         return vmId;
      }
   }
   return MKSCK_VMID_UNDEF;
}


/**
 * @brief Find shared page for a given idx. The page referred to be the
 *        idx should exist and be locked by the caller.
 * @param idx index of the page in the array
 * @return pointer to shared page
 */
MksckPage *
MksckPage_GetFromIdx(uint32 idx)
{
   MksckPage *mksckPage = mksckPages[idx];
   ASSERT(mksckPage);
   ASSERT(idx<MKSCK_MAX_SHARES);
   ASSERT(ATOMIC_GETO(mksckPage->refCount));
   return mksckPage;
}

/**
 * @brief find shared page for a given vmId
 *        The vmid should exist and be locked by the caller.
 * @param vmId vmId to look for, either an host vmId or a guest vmId
 * @return pointer to shared page
 */
MksckPage *
MksckPage_GetFromVmId(Mksck_VmId vmId)
{
   MksckPage *mksckPage = mksckPages[MKSCK_VMID2IDX(vmId)];
   ASSERT(mksckPage);
   ASSERT(mksckPage->vmId == vmId);
   ASSERT(ATOMIC_GETO(mksckPage->refCount));
   return mksckPage;
}


/**
 * @brief find shared page for a given vmId
 * @param vmId vmId to look for, either an host vmId or a guest vmId
 * @return NULL: no such shared page exists<br>
 *         else: pointer to shared page.
 *               Call Mksck_DecRefc() when done with pointer
 */
MksckPage *
MksckPage_GetFromVmIdIncRefc(Mksck_VmId vmId)
{
   MksckPage *mksckPage;

   spin_lock(&mksckPageListLock);
   mksckPage = mksckPages[MKSCK_VMID2IDX(vmId)];

   if (!mksckPage || (mksckPage->vmId != vmId)) {
      printk(KERN_INFO "MksckPage_GetFromVmIdIncRefc: vmId %04X not found\n",
             vmId);
      mksckPage = NULL;
   } else {
      ATOMIC_ADDV(mksckPage->refCount, 1);
   }
   spin_unlock(&mksckPageListLock);
   return mksckPage;
}


/**
 * @brief find or allocate shared page using tgid
 * @return NULL: no such shared page exists<br>
 *         else: pointer to shared page.
 *               Call Mksck_DecRefc() when done with pointer
 */
MksckPage *
MksckPage_GetFromTgidIncRefc(void)
{
   MksckPage *mksckPage;
   Mksck_VmId vmId;

   while (1) {
      spin_lock(&mksckPageListLock);
      vmId = GetHostVmId();

      if (vmId == MKSCK_VMID_UNDEF) {
         /*
          * No vmId has been allocated yet and there is no free slot.
          */
         spin_unlock(&mksckPageListLock);
         return NULL;
      }

      mksckPage = mksckPages[MKSCK_VMID2IDX(vmId)];
      if (mksckPage != NULL) {
         /*
          * There is a vmid already allocated, increment the refc on it.
          */
         ATOMIC_ADDV(mksckPage->refCount, 1);
         spin_unlock(&mksckPageListLock);
         return mksckPage;
      }

      /*
       * Have to release spinlock to allocate a new page.
       */
      spin_unlock(&mksckPageListLock);
      mksckPage = MksckPageAlloc();
      if (mksckPage == NULL) {
         return NULL;
      }

      /*
       * Re-lock and make sure no one else allocated while unlocked.
       * If someone else did allocate, free ours off and use theirs.
       */
      spin_lock(&mksckPageListLock);
      vmId = GetHostVmId();
      if ((vmId != MKSCK_VMID_UNDEF) &&
          (mksckPages[MKSCK_VMID2IDX(vmId)] == NULL)) {
         break;
      }
      spin_unlock(&mksckPageListLock);
      MksckPageRelease(mksckPage);
   }

   /*
    * This is a successful new allocation. insert it into the table
    * and initialize the fields.
    */
   mksckPages[MKSCK_VMID2IDX(vmId)] = mksckPage;
   mksckPage->vmId    = vmId;
   mksckPage->isGuest = false;
   mksckPage->vmHKVA  = 0;
   mksckPage->tgid    = task_tgid_vnr(current);
   printk(KERN_DEBUG "New host mksck page is allocated: idx %x, vmId %x, tgid %d\n",
          MKSCK_VMID2IDX(vmId), vmId, mksckPage->tgid);

   spin_unlock(&mksckPageListLock);
   return mksckPage;
}

/**
 * @brief Initialize the VMX provided wsp. Allocate communication page.
 * @param vm  which virtual machine we're running
 * @return 0 if all OK, error value otherwise
 */
int
Mksck_WspInitialize(MvpkmVM *vm)
{
   WorldSwitchPage *wsp = vm->wsp;
   int err;
   Mksck_VmId vmId;
   MksckPage *mksckPage;

   if (wsp->guestId) {
      err = -EBUSY;
   } else if (!(mksckPage = MksckPageAlloc())) {
      err = -ENOMEM;
   } else {
      spin_lock(&mksckPageListLock);

      if ((vmId = GetNewGuestVmId()) == MKSCK_VMID_UNDEF) {

         err = -EMFILE;
         MksckPageRelease(mksckPage);

         printk(KERN_INFO "Mksck_WspInitialize: Cannot allocate vmId\n");

      } else {
         /*
          * Now that the mksckPage is all initialized, let others see it.
          */
         mksckPages[MKSCK_VMID2IDX(vmId)] = mksckPage;
         mksckPage->vmId    = vmId;
         mksckPage->isGuest = true;
         mksckPage->vmHKVA  = (HKVA)vm;
         /* mksckPage->tgid is undefined when isGuest is true */

         wsp->guestId = vmId;

         printk(KERN_DEBUG "New guest mksck page is allocated: idx %x, vmId %x\n",
                MKSCK_VMID2IDX(vmId), vmId);

         err = 0;
      }

      /*
       * All stable, ie, mksckPages[] written, ok to unlock now.
       */
      spin_unlock(&mksckPageListLock);
   }

   return err;
}

/**
 * @brief Release the wsp. Clean up after the monitor. Free the
 *        associated communication page.
 * @param wsp which worldswitch page (VCPU)
 */
void
Mksck_WspRelease(WorldSwitchPage *wsp)
{
   int ii;
   int err;
   MksckPage *mksckPage = MksckPage_GetFromVmId(wsp->guestId);

   /*
    * The worldswitch page for a particular VCPU is about to be freed
    * off, so we know the monitor will never execute again.  But the
    * monitor most likely left some sockets open. Those may have
    * outbound connections to host sockets that we must close.
    *
    * Loop through all possibly open sockets.
    */
   uint32 isOpened = wsp->isOpened;
   Mksck *mksck = mksckPage->sockets;
   while (isOpened) {
      if (isOpened & 1) {
         ASSERT(ATOMIC_GETO(mksck->refCount) != 0);
         /*
          * The socket may be connected to a peer (host) socket, so we
          * have to decrement that target socket's reference
          * count. Unfortunately, Mksck_DisconnectPeer(mksck) cannot
          * be called as mksck->peer is an mva not an hkva. Translate
          * the address first.
          */
         if (mksck->peer) {
            MksckPage *mksckPagePeer = MksckPage_GetFromVmId(mksck->peerAddr.vmId);
            ASSERT(mksckPagePeer);
            mksck->peer = MksckPage_GetFromAddr(mksckPagePeer, mksck->peerAddr);
            ASSERT(mksck->peer);
            /* mksck->peer is now a hkva */
         }

         Mksck_CloseCommon(mksck);
      }
      isOpened >>= 1;
      mksck++;
   }

   /*
    * A host socket may be in the process of sending to the guest. It
    * will attempt to wake up the guest using mksckPage->vmHKVA and
    * mksckPage->vmHKVA->wsp. To assure that the vm and wsp structures
    * are not disappearing from under the sending thread we lock the
    * page here.
    */
   err = Mutex_Lock(&mksckPage->mutex, MutexModeEX);
   ASSERT(!err);
   mksckPage->vmHKVA = 0;
   Mutex_Unlock(&mksckPage->mutex, MutexModeEX);
   /*
    * Decrement refcount set by MksckPageAlloc() call in
    * Mksck_WspInitialize().
    */
   MksckPage_DecRefc(mksckPage);

   /*
    * Decrement refcount set by VMM:Mksck_Init() referring to the local
    * variable guestMksckPage.
    */
   if (wsp->guestPageMapped) {
      wsp->guestPageMapped = false;
      MksckPage_DecRefc(mksckPage);
   }

   /*
    * Another task is to decrement the reference count on the mksck
    * pages the monitor accessed. Those pages are listed in the
    * wsp->isPageMapped list. They were locked by the monitor
    * calling WSCALL_GET_PAGE_FROM_VMID
    */
   for (ii = 0; ii < MKSCK_MAX_SHARES; ii++) {
      if (wsp->isPageMapped[ii]) {
         MksckPage *mksckPageOther = MksckPage_GetFromIdx(ii);

         wsp->isPageMapped[ii] = false;
         MksckPage_DecRefc(mksckPageOther);
      }
   }
}

/**
 * @brief disconnect from peer by decrementing
 *        peer socket's reference count and clearing the pointer.
 * @param mksck local socket to check for connection
 */
void
Mksck_DisconnectPeer(Mksck *mksck)
{
   Mksck *peerMksck = mksck->peer;
   if (peerMksck != NULL) {
      mksck->peer = NULL;
      mksck->peerAddr.addr = MKSCK_ADDR_UNDEF;
      Mksck_DecRefc(peerMksck);
   }
}


/**
 * @brief decrement shared page reference count, free page if it goes zero.
 *        also do a dmb first to make sure all activity on the struct is
 *        finished before decrementing the ref count.
 * @param mksckPage shared page
 */
void
MksckPage_DecRefc(MksckPage *mksckPage)
{
   uint32 oldRefc;

   DMB();
   do {
      while ((oldRefc = ATOMIC_GETO(mksckPage->refCount)) == 1) {

         /*
          * Find corresponding entry in list of known shared pages and
          * clear it so we can't open any new sockets on this shared
          * page, thus preventing its refCount from being incremented.
          */
         spin_lock(&mksckPageListLock);
         if (ATOMIC_SETIF(mksckPage->refCount, 0, 1)) {
            uint32 ii = MKSCK_VMID2IDX(mksckPage->vmId);
            ASSERT(ii < MKSCK_MAX_SHARES);
            ASSERT(mksckPages[ii] == mksckPage);
            mksckPages[ii] = NULL;
            spin_unlock(&mksckPageListLock);
            printk(KERN_DEBUG "%s mksck page is released: idx %x, vmId %x, tgid %d\n",
                   mksckPage->isGuest?"Guest":"Host",
                   ii, mksckPage->vmId, mksckPage->tgid);
            MksckPageRelease(mksckPage);
            return;
         }
         spin_unlock(&mksckPageListLock);
      }
      ASSERT(oldRefc != 0);
   } while (!ATOMIC_SETIF(mksckPage->refCount, oldRefc - 1, oldRefc));
}

/**
 * @brief Lookup if the provided mpn belongs to one of the Mksck pages. Map if found.
 * @return 0 if all OK, error value otherwise
 */
int
MksckPage_LookupAndInsertPage(struct vm_area_struct *vma,
                              unsigned long address,
                              MPN mpn)
{
   int ii, jj;
   MksckPage **mksckPagePtr = mksckPages;

   spin_lock(&mksckPageListLock);
   for (jj = MKSCK_MAX_SHARES; jj--; mksckPagePtr++) {
      if (*mksckPagePtr) {
         for (ii = 0; ii < MKSCKPAGE_TOTAL; ii++) {
            if (vmalloc_to_pfn((void*)(((HKVA)*mksckPagePtr) + ii*PAGE_SIZE)) == mpn &&
                vm_insert_page(vma, address, pfn_to_page(mpn)) == 0) {
               spin_unlock(&mksckPageListLock);
               return 0;
            }
         }
      }
   }
   spin_unlock(&mksckPageListLock);
   return -1;
}


/**
 * @brief Print information on the allocated shared pages
 *
 * This function reports (among many other things) on the use of locks
 * on the mksck page (page lock and individual socket locks). To avoid
 * the Hiesenberg effect it avoids using locks unless there is a
 * danger of dereferencing freed memory. In particular, holding
 * mksckPageListLock ensures that the mksck page is not freed while it
 * is read. But under very rare conditions this function may report
 * inconsistent or garbage data.
 */
static int
MksckPageInfoShow(struct seq_file *m, void *private)
{
   int ii, jj;
   uint32 isPageMapped = 0;
   int err;
   MvpkmVM *vm;

   /*
    * Lock is needed to atomize the test and dereference of
    * mksckPages[ii]
    */
   spin_lock(&mksckPageListLock);
   for (ii = 0; ii < MKSCK_MAX_SHARES; ii++) {
      MksckPage *mksckPage  = mksckPages[ii];
      if (mksckPage != NULL && mksckPage->isGuest) {
         /*
          * After the refcount is incremented mksckPage will not be
          * freed and it can continued to be dereferenced after the
          * unlock of mksckPageListLock.
          */
         ATOMIC_ADDV(mksckPage->refCount, 1);
         spin_unlock(&mksckPageListLock);

         /*
          * To dereference mksckPage->vmHKVA, we need to have the page
          * lock.
          */
         err = Mutex_Lock(&mksckPage->mutex, MutexModeEX);
         vm = (MvpkmVM *) mksckPage->vmHKVA;

         if (err == 0 && vm && vm->wsp) {
            for (jj = 0; jj < MKSCK_MAX_SHARES; jj++) {
               if (vm->wsp->isPageMapped[jj]) isPageMapped |= 1<<jj;
            }
         }
         Mutex_Unlock(&mksckPage->mutex, MutexModeEX);
         /*
          * Decrement the page refcount and relock the
          * mksckPageListLock for the next for loop.
          */
         MksckPage_DecRefc(mksckPage);
         spin_lock(&mksckPageListLock);
         break;
      }
   }

   /* mksckPageListLock is still locked,  mksckPages[ii] can be dereferenced */
   for (ii = 0; ii < MKSCK_MAX_SHARES; ii++) {
      MksckPage *mksckPage  = mksckPages[ii];
      if (mksckPage != NULL) {
         uint32 lState = ATOMIC_GETO(mksckPage->mutex.state);
         uint32 isOpened = 0; /* guest has an implicit ref */

         seq_printf(m, "MksckPage[%02d]: { vmId = %4x(%c), refC = %2d%s",
                    ii, mksckPage->vmId,
                    mksckPage->isGuest?'G':'H',
                    ATOMIC_GETO(mksckPage->refCount),
                    (isPageMapped&(1<<ii) ? "*" : ""));

         if (lState) {
            seq_printf(m, ", lock=%x locked by line %d, unlocked by %d",
                       lState, mksckPage->mutex.line, mksckPage->mutex.lineUnl);
         }


         if (!mksckPage->isGuest) {
            struct task_struct *target;
            seq_printf(m, ", tgid = %d", mksckPage->tgid);

            rcu_read_lock();

            target = pid_task(find_vpid(mksckPage->tgid), PIDTYPE_PID);
            seq_printf(m, "(%s)", target ? target->comm : "no such process");

            rcu_read_unlock();
         } else {
            ATOMIC_ADDV(mksckPage->refCount, 1);
            spin_unlock(&mksckPageListLock);

            err = Mutex_Lock(&mksckPage->mutex, MutexModeEX);
            vm = (MvpkmVM *) mksckPage->vmHKVA;

            if (err == 0 && vm && vm->wsp) {
               isOpened = vm->wsp->isOpened;
            }
            Mutex_Unlock(&mksckPage->mutex, MutexModeEX);
            MksckPage_DecRefc(mksckPage);
            spin_lock(&mksckPageListLock);
            /*
             * As the mksckPageListLock was unlocked, nothing
             * prevented the MksckPage_DecRefc from actually freeing
             * the page. Lets verify that the page is still there.
             */
            if (mksckPage != mksckPages[ii]) {
               seq_printf(m, " released }\n");
               continue;
            }
         }
         seq_printf(m, ", sockets[] = {");

         for (jj = 0; jj < mksckPage->numAllocSocks; jj++, isOpened >>= 1) {
            Mksck *mksck = mksckPage->sockets + jj;

            if (ATOMIC_GETO(mksck->refCount)) {
               uint32 blocked;
               lState = ATOMIC_GETO(mksck->mutex.state);
               seq_printf(m, "\n             { addr = %8x, refC = %2d%s%s%s",
                          mksck->addr.addr,
                          ATOMIC_GETO(mksck->refCount),
                          (isOpened & 1 ? "*" : ""),
                          (mksck->shutDown & MKSCK_SHUT_RD ? " SHUTD_RD":""),
                          (mksck->shutDown & MKSCK_SHUT_WR ? " SHUTD_WR":""));

               if (mksck->peer) {
                  seq_printf(m, ", peerAddr = %8x",
                             mksck->peerAddr.addr);
               }

               if (lState) {
                  seq_printf(m, ", lock=%x locked by line %d, unlocked by %d",
                             lState, mksck->mutex.line, mksck->mutex.lineUnl);
               }

               if ((blocked = ATOMIC_GETO(mksck->mutex.blocked))) {
                  seq_printf(m, ", blocked=%d", blocked);
               }

               seq_printf(m, " }");
            }
         }
         seq_printf(m, " } }\n");
      }
   }
   spin_unlock(&mksckPageListLock);

   return 0;
}


static int
MksckPageInfoOpen(struct inode *inode, struct file *file)
{
   return single_open(file, MksckPageInfoShow, inode->i_private);
}

static const struct file_operations mksckPageInfoFops = {
   .open = MksckPageInfoOpen,
   .read = seq_read,
   .llseek = seq_lseek,
   .release = single_release,
};

static struct dentry *mksckPageDentry = NULL;

void
MksckPageInfo_Init(void)
{
   mksckPageDentry = debugfs_create_file("mksckPage",
                                         S_IROTH,
                                         NULL,
                                         NULL,
                                         &mksckPageInfoFops);
}

void
MksckPageInfo_Exit(void)
{
   if (mksckPageDentry) {
      debugfs_remove(mksckPageDentry);
   }
}