summaryrefslogtreecommitdiff
path: root/viengoos/server.c
blob: 6a8f72eb48832421f7eb67d694304b06f5ffae9b (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
/* server.c - Server loop implementation.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd 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.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include <l4.h>
#include <l4/pagefault.h>
#include <viengoos/cap.h>
#include <hurd/stddef.h>
#include <viengoos/thread.h>
#include <viengoos/activity.h>
#include <viengoos/futex.h>
#include <hurd/trace.h>
#include <hurd/as.h>
#include <viengoos/ipc.h>

#include "server.h"

#include <viengoos/misc.h>

#include "output.h"
#include "cap.h"
#include "object.h"
#include "thread.h"
#include "activity.h"
#include "messenger.h"
#include "viengoos.h"
#include "profile.h"

#ifndef NDEBUG
struct futex_waiter_list futex_waiters;
#endif

#ifndef NDEBUG

struct trace_buffer rpc_trace = TRACE_BUFFER_INIT ("rpcs", 0,
						   true, false, false);

/* Like debug but also prints the method id and saves to the trace
   buffer if level is less than or equal to 4.  */
# define DEBUG(level, format, args...)					\
  do									\
    {									\
      if (level <= 4)							\
	trace_buffer_add (&rpc_trace, "(%x %s %d) " format,		\
			  thread->tid,					\
			  l4_is_pagefault (msg_tag) ? "pagefault"	\
			  : label == 8194 ? "IPC"			\
			  : vg_method_id_string (label),		\
			  label,					\
			  ##args);					\
      debug (level, "(%x %s:%d %d) " format,				\
	     thread->tid, l4_is_pagefault (msg_tag) ? "pagefault"	\
	     : label == 8194 ? "IPC" : vg_method_id_string (label),	\
	     __LINE__, label,						\
	     ##args);							\
    }									\
  while (0)

#else
# define DEBUG(level, format, args...)					\
      debug (level, "(%x %s:%d %d) " format,				\
	     thread->tid, l4_is_pagefault (msg_tag) ? "pagefault"	\
	     : label == 8194 ? "IPC" : vg_method_id_string (label),	\
	     __LINE__, label,						\
	     ##args)
#endif

#define PAGEFAULT_METHOD 2

void
server_loop (void)
{
  error_t err;

  int do_reply = 0;
  l4_thread_id_t to = l4_nilthread;
  l4_msg_t msg;

  bool have_lock = false;
#ifndef NDEBUG
  bool rpc_trace_just_dumped = false;
#endif

  /* Profiling.  */
  int method = -1;

  for (;;)
    {
      if (method != -1)
	{
	  profile_end (method);
	  method = -1;
	}

      if (have_lock)
	{
	  ss_mutex_unlock (&kernel_lock);
	  have_lock = false;
	}

      l4_thread_id_t from = l4_anythread;
      l4_msg_tag_t msg_tag;

#ifndef NDEBUG
      l4_time_t max_idle
	= rpc_trace_just_dumped ? L4_NEVER : l4_time_period (5000 * 1000);
#else
      l4_time_t max_idle = L4_NEVER;
#endif

      /* Only accept untyped items--no strings, no mappings.  */
      l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);
      if (do_reply)
	{
	  l4_msg_load (msg);

	  msg_tag = l4_reply_wait_timeout (to, max_idle, &from);
	}
      else
	msg_tag = l4_wait_timeout (max_idle, &from);

      if (l4_ipc_failed (msg_tag))
	{
#ifndef NDEBUG
	  if ((l4_error_code () & 1) && ((l4_error_code () >> 1) & 0x7) == 1)
	    /* Receive timeout.  This means that we have not gotten
	       any message in the last few seconds.  Perhaps there is
	       a dead-lock.  Dump the rpc trace.  */
	    {
	      debug (0, "No IPCs for some time.  Deadlock?");

	      struct messenger *messenger;
	      while ((messenger = futex_waiter_list_head (&futex_waiters)))
		{
		  object_wait_queue_unlink (root_activity, messenger);
		  rpc_error_reply (root_activity, messenger, EDEADLK);
		}

	      trace_buffer_dump (&rpc_trace, 0);
	      rpc_trace_just_dumped = true;
	    }
#endif

	  debug (4, "%s %x failed: %u", 
		 l4_error_code () & 1 ? "Receiving from" : "Sending to",
		 l4_error_code () & 1 ? from : to,
		 (l4_error_code () >> 1) & 0x7);

	  do_reply = 0;
	  continue;
	}
#ifndef NDEBUG
      else
	rpc_trace_just_dumped = false;
#endif

      l4_msg_store (msg_tag, msg);
      uintptr_t label;
      label = l4_label (msg_tag);

      /* By default we reply to the sender.  */
      to = from;
      /* Unless explicitly overridden, don't reply.  */
      do_reply = 0;

      debug (5, "%x (p: %d, %x) sent %s (%x)",
	     from, l4_ipc_propagated (msg_tag), l4_actual_sender (),
	     (l4_is_pagefault (msg_tag) ? "fault handler"
	      : vg_method_id_string (label)),
	     label);

      if (l4_version (l4_myself ()) == l4_version (from))
	/* Message from a kernel thread.  */
	panic ("Kernel thread %x (propagated: %d, actual: %x) sent %s? (%x)!",
	       from, l4_ipc_propagated (msg_tag), l4_actual_sender (),
	       (l4_is_pagefault (msg_tag) ? "fault handler"
		      : vg_method_id_string (label)),
	       label);

      ss_mutex_lock (&kernel_lock);
      have_lock = true;


      /* Start timer.  */
      if (l4_is_pagefault (msg_tag))
	method = PAGEFAULT_METHOD;
      else
	method = label;
      profile_start (method,
		     method == PAGEFAULT_METHOD ? "fault handler"
		     : vg_method_id_string (method), NULL);

      /* Find the sender.  */
      struct thread *thread = thread_lookup (from);
      assert (thread);

      /* XXX: We can't charge THREAD's activity until we have the
	 activity object, however, getting the activity object may
	 require not only a few cycles but also storage and disk
	 activity.  What we could do is have a special type of
	 activity called a charge that acts as the resource principal
	 and then once we find the real principal, we just add the
	 charges to the former to the latter.  */
      struct activity *activity
	= (struct activity *) vg_cap_to_object (root_activity,
					     &thread->activity);
      if (! activity)
	{
	  DEBUG (1, "Caller has no assigned activity");
	  continue;
	}
      if (object_type ((struct vg_object *) activity)
	  != vg_cap_activity_control)
	{
	  DEBUG (1, "Caller's activity slot contains a %s,"
		 "not an activity_control",
		 vg_cap_type_string
		  (object_type ((struct vg_object *) activity)));
	  continue;
	}

      if (l4_is_pagefault (msg_tag))
	/* The label is not constant: it includes the type of fault.
	   Thus, it is difficult to incorporate it into the case
	   switch below.  */
	{
	  uintptr_t access;
	  uintptr_t ip;
	  uintptr_t fault = l4_pagefault (msg_tag, &access, &ip);
	  bool write_fault = !! (access & L4_FPAGE_WRITABLE);

	  DEBUG (4, "%s fault at %x (ip: %x)",
		 write_fault ? "Write" : "Read", fault, ip);

	  uintptr_t page_addr = fault & ~(PAGESIZE - 1);

	  struct vg_cap cap;
	  bool writable;
	  cap = as_object_lookup_rel (activity, &thread->aspace,
				      vg_addr_chop (VG_PTR_TO_ADDR (page_addr),
						 PAGESIZE_LOG2),
				      write_fault ? vg_cap_page : vg_cap_rpage,
				      &writable);

	  assert (cap.type == vg_cap_void
		  || cap.type == vg_cap_page
		  || cap.type == vg_cap_rpage);

	  bool discarded = false;
	  if (write_fault && ! writable)
	    {
	      debug (5, "Access fault at %x", page_addr);
	      goto do_fault;
	    }

	  if (! writable && cap.discardable)
	    {
	      DEBUG (4, "Ignoring discardable predicate for cap designating "
		     VG_OID_FMT " (%s)",
		     VG_OID_PRINTF (cap.oid), vg_cap_type_string (cap.type));
	      cap.discardable = false;
	    }

	  struct vg_object *page = vg_cap_to_object (activity, &cap);
	  if (! page && cap.type != vg_cap_void)
	    /* It's not in-memory.  See if it was discarded.  If not,
	       load it using vg_cap_to_object.  */
	    {
	      int object = (cap.oid % (VG_FOLIO_OBJECTS + 1)) - 1;
	      vg_oid_t foid = cap.oid - object - 1;
	      struct vg_folio *folio
		= (struct vg_folio *) object_find (activity, foid,
						VG_OBJECT_POLICY_DEFAULT);
	      assert (folio);
	      assert (object_type ((struct vg_object *) folio) == vg_cap_folio);

	      if (cap.version == folio_object_version (folio, object))
		{
		  if (folio_object_discarded (folio, object))
		    {
		      DEBUG (4, VG_OID_FMT " (%s) was discarded",
			     VG_OID_PRINTF (cap.oid),
			     vg_cap_type_string (vg_folio_object_type (folio,
								       object)));

		      assert (! folio_object_content (folio, object));

		      discarded = true;

		      DEBUG (5, "Raising discarded fault at %x", page_addr);
		    }
		}
	    }

	  if (! page)
	    {
	    do_fault:
	      DEBUG (4, "Reflecting fault (ip: %x; fault: %x.%c%s)!",
		     ip, fault, write_fault ? 'w' : 'r',
		     discarded ? " discarded" : "");

	      uintptr_t c = _L4_XCHG_REGS_DELIVER;
	      l4_thread_id_t targ = thread->tid;
	      uintptr_t sp = 0;
	      uintptr_t dummy = 0;
	      _L4_exchange_registers (&targ, &c,
				      &sp, &dummy, &dummy, &dummy, &dummy);

	      struct vg_activation_fault_info info;
	      info.access = access;
	      info.type = write_fault ? vg_cap_page : vg_cap_rpage;

	      info.discarded = discarded;

	      vg_activation_fault_send_marshal (reply_buffer,
						VG_PTR_TO_ADDR (fault),
						sp, ip, info, VG_ADDR_VOID);
	      thread_raise_exception (activity, thread, reply_buffer);

	      continue;
	    }

	  DEBUG (4, "%s fault at " DEBUG_BOLD ("%x") " (ip=%x), "
		 "replying with %p(r%s)",
		 write_fault ? "Write" : "Read", fault, ip, page,
		 writable ? "w" : "");

	  object_to_object_desc (page)->mapped = true;

	  access = L4_FPAGE_READABLE;
	  if (writable)
	    access |= L4_FPAGE_WRITABLE;

	  l4_map_item_t map_item
	    = l4_map_item
	       (l4_fpage_add_rights (l4_fpage ((uintptr_t) page, PAGESIZE),
				     access),
		page_addr);

	  /* Formulate the reply message.  */
	  l4_pagefault_reply_formulate_in (msg, &map_item);

#if 0
	  int count = 0;
	  while (sizeof (l4_map_item_t) / sizeof (l4_word_t)
		 < (L4_NUM_MRS - 1
		    - l4_untyped_words (l4_msg_msg_tag (msg))
		    - l4_typed_words (l4_msg_msg_tag (msg))))
	    {
	      page_addr += PAGESIZE;

	      cap = as_object_lookup_rel (activity, &thread->aspace,
					  vg_addr_chop (VG_PTR_TO_ADDR (page_addr),
						     PAGESIZE_LOG2),
					  vg_cap_rpage, &writable);

	      if (cap.type != vg_cap_page && cap.type != vg_cap_rpage)
		break;

	      if (! writable && cap.discardable)
		cap.discardable = false;

	      struct vg_object *page = vg_cap_to_object (activity, &cap);
	      if (! page)
		break;

	      object_to_object_desc (page)->mapped = true;

	      access = L4_FPAGE_READABLE;
	      if (writable)
		access |= L4_FPAGE_WRITABLE;

	      l4_fpage_t fpage = l4_fpage ((uintptr_t) page, PAGESIZE);
	      fpage = l4_fpage_add_rights (fpage, access);
	      l4_map_item_t map_item = l4_map_item (fpage, page_addr);

	      l4_msg_append_map_item (msg, map_item);

	      DEBUG (5, "Prefaulting " DEBUG_BOLD ("%x") " <- %p (%x/%x/%x) %s",
		     page_addr,
		     page, l4_address (fpage), l4_size (fpage),
		     l4_rights (fpage), vg_cap_type_string (cap.type));

	      count ++;
	    }

	  if (count > 0)
	    DEBUG (5, "Prefaulted %d pages (%d/%d)", count,
		   l4_untyped_words (l4_msg_msg_tag (msg)),
		   l4_typed_words (l4_msg_msg_tag (msg)));
#endif

	  do_reply = 1;
	  continue;
	}

      struct activity *principal;

  /* Create a message indicating an error with the error code ERR_.
     Go to the start of the server loop.  */
#define REPLY(err_)						\
      do							\
	{							\
	  if (err_)						\
	    DEBUG (1, DEBUG_BOLD ("Returning error %d to %x"),	\
		   err_, from);					\
	  l4_msg_clear (msg);					\
	  l4_msg_put_word (msg, 0, (err_));			\
	  l4_msg_set_untyped_words (msg, 1);			\
	  do_reply = 1;						\
	  goto out;						\
	}							\
      while (0)

      /* Return the capability slot corresponding to address ADDR in
	 the address space rooted at ROOT.  */
      error_t SLOT_ (struct vg_cap *root, vg_addr_t addr, struct vg_cap **capp)
	{
	  bool w;
	  if (! as_slot_lookup_rel_use (activity, root, addr,
					({
					  w = writable;
					  *capp = slot;
					})))
	    {
	      DEBUG (0, "No capability slot at 0x%llx/%d",
		     vg_addr_prefix (addr), vg_addr_depth (addr));
	      as_dump_from (activity, root, "");
	      return ENOENT;
	    }
	  if (! w)
	    {
	      DEBUG (1, "Capability slot at 0x%llx/%d not writable",
		     vg_addr_prefix (addr), vg_addr_depth (addr));
	      as_dump_from (activity, root, "");
	      return EPERM;
	    }

	  return 0;
      }
#define SLOT(root_, addr_)				\
      ({						\
	struct vg_cap *SLOT_ret;				\
	error_t err = SLOT_ (root_, addr_, &SLOT_ret);	\
	if (err)					\
	  REPLY (err);					\
	SLOT_ret;					\
      })

      /* Return a cap referencing the object at address ADDR of the
	 callers capability space if it is of type TYPE (-1 = don't care).
	 Whether the object is writable is stored in *WRITABLEP_.  */
      error_t CAP_ (struct vg_cap *root,
		    vg_addr_t addr, int type, bool require_writable,
		    struct vg_cap *cap)
	{
	  bool writable = true;
	  *cap = as_cap_lookup_rel (principal, root, addr,
				    type, require_writable ? &writable : NULL);
	  if (type != -1 && ! vg_cap_types_compatible (cap->type, type))
	    {
	      DEBUG (1, "Addr 0x%llx/%d does not reference object of "
		     "type %s but %s",
		     vg_addr_prefix (addr), vg_addr_depth (addr),
		     vg_cap_type_string (type), vg_cap_type_string (cap->type));
	      as_dump_from (activity, root, "");
	      return ENOENT;
	    }

	  if (require_writable && ! writable)
	    {
	      DEBUG (1, "Addr " VG_ADDR_FMT " not writable",
		     VG_ADDR_PRINTF (addr));
	      return EPERM;
	    }

	  return 0;
	}
#define CAP(root_, addr_, type_, require_writable_)			\
      ({								\
	struct vg_cap CAP_ret;						\
	error_t err = CAP_ (root_, addr_, type_, require_writable_,	\
			    &CAP_ret);					\
	if (err)							\
	  REPLY (err);							\
	CAP_ret;							\
      })

      error_t OBJECT_ (struct vg_cap *root,
		       vg_addr_t addr, int type, bool require_writable,
		       struct vg_object **objectp, bool *writable)
	{
	  bool w = true;
	  struct vg_cap cap;
	  cap = as_object_lookup_rel (principal, root, addr, type, &w);
	  if (type != -1 && ! vg_cap_types_compatible (cap.type, type))
	    {
	      DEBUG (0, "Addr 0x%llx/%d does not reference object of "
		     "type %s but %s",
		     vg_addr_prefix (addr), vg_addr_depth (addr),
		     vg_cap_type_string (type), vg_cap_type_string (cap.type));
	      return ENOENT;
	    }

	  if (writable)
	    *writable = w;

	  if (require_writable && ! w)
	    {
	      DEBUG (0, "Addr " VG_ADDR_FMT " not writable",
		     VG_ADDR_PRINTF (addr));
	      return EPERM;
	    }

	  *objectp = vg_cap_to_object (principal, &cap);
	  if (! *objectp)
	    {
	      do_debug (4)
		DEBUG (0, "Addr " VG_ADDR_FMT " contains a dangling pointer: "
		       VG_CAP_FMT,
		       VG_ADDR_PRINTF (addr), VG_CAP_PRINTF (&cap));
	      return ENOENT;
	    }

	  return 0;
	}
#define OBJECT(root_, addr_, type_, require_writable_, writablep_)	\
      ({								\
	struct vg_object *OBJECT_ret;					\
	error_t err = OBJECT_ (root_, addr_, type_, require_writable_,	\
			       &OBJECT_ret, writablep_);		\
	if (err)							\
	  REPLY (err);							\
	OBJECT_ret;							\
      })

      /* Find an address space root.  If VG_ADDR_VOID, the current
	 thread's.  Otherwise, the object identified by ROOT_ADDR_ in
	 the caller's address space.  If that is a thread object, then
	 it's address space root.  */
#define ROOT(root_addr_)						\
      ({								\
	struct vg_cap *root_;						\
	if (VG_ADDR_IS_VOID (root_addr_))					\
	  root_ = &thread->aspace;					\
	else								\
	  {								\
	    /* This is annoying: 1) a thread could be in a folio so we  \
	       can't directly lookup the slot, 2) we only want the      \
	       thread if it matches the guard exactly.  */		\
	    struct vg_object *t_;						\
	    error_t err = OBJECT_ (&thread->aspace, root_addr_,		\
				   vg_cap_thread, true, &t_, NULL);	\
	    if (! err)							\
	      root_ = &((struct thread *) t_)->aspace;			\
	    else							\
	      root_ = SLOT (&thread->aspace, root_addr_);		\
	  }								\
	DEBUG (4, "root: " VG_CAP_FMT, VG_CAP_PRINTF (root_));		\
									\
	root_;								\
      })

  /* Return the next word.  */
#define ARG(word_) l4_msg_word (msg, word_);

  /* Return word WORD_.  */
#if L4_WORDSIZE == 32
#define ARG64(word_) \
  ({ \
    union { l4_uint64_t raw; struct { l4_uint32_t word[2]; }; } value_; \
    value_.word[0] = ARG (word_); \
    value_.word[1] = ARG (word_ + 1); \
    value_.raw; \
  })
#define ARG64_WORDS 2
#else
#define ARG64(word_) ARG(word_)
#define ARG64_WORDS 1
#endif

#define ARG_ADDR(word_) ((vg_addr_t) { ARG64(word_) })

      if (label == 2132)
	/* write.  */
	{
	  int len = msg[1];
	  char *buffer = (char *) &msg[2];
	  buffer[len] = 0;
	  s_printf ("%s", buffer);
	  continue;
	}

      if (label != 8194)
	{
	  DEBUG (0, "Invalid label: %d", label);
	  continue;
	}

      int i = 0;
      uintptr_t flags = ARG (i);
      i ++;
      vg_addr_t recv_activity = ARG_ADDR (i);
      i += ARG64_WORDS;
      vg_addr_t recv_messenger = ARG_ADDR (i);
      i += ARG64_WORDS;
      vg_addr_t recv_buf = ARG_ADDR (i);
      i += ARG64_WORDS;
      vg_addr_t recv_inline_cap = ARG_ADDR (i);
      i += ARG64_WORDS;

      vg_addr_t send_activity = ARG_ADDR (i);
      i += ARG64_WORDS;
      vg_addr_t target_messenger = ARG_ADDR (i);
      i += ARG64_WORDS;

      vg_addr_t send_messenger = ARG_ADDR (i);
      i += ARG64_WORDS;
      vg_addr_t send_buf = ARG_ADDR (i);
      i += ARG64_WORDS;

      uintptr_t inline_word1 = ARG (i);
      i ++;
      uintptr_t inline_word2 = ARG (i);
      i ++;
      vg_addr_t inline_cap = ARG_ADDR (i);

#ifndef NDEBUG
      /* Get the label early to improve debugging output in case the
	 target is invalid.  */
      if ((flags & VG_IPC_SEND))
	{
	  if ((flags & VG_IPC_SEND_INLINE))
	    label = inline_word1;
	  else
	    {
	      principal = activity;

	      struct vg_cap cap = VG_CAP_VOID;
	      if (! VG_ADDR_IS_VOID (send_buf))
		/* Caller provided a send buffer.  */
		CAP_ (&thread->aspace, send_buf, vg_cap_page, true, &cap);
	      else
		{
		  struct vg_object *object = NULL;
		  OBJECT_ (&thread->aspace, send_messenger,
			   vg_cap_messenger, true, &object, NULL);
		  if (object)
		    cap = ((struct messenger *) object)->buffer;
		}
		
	      struct vg_message *message;
	      message = (struct vg_message *) vg_cap_to_object (principal,
							     &cap);
	      if (message)
		label = vg_message_word (message, 0);
	    }
	}
#endif

      DEBUG (4, "flags: %s%s%s%s%s%s %s%s%s%s%s%s %s %s%s%s%s(%x),"
	     "recv (" VG_ADDR_FMT ", " VG_ADDR_FMT ", " VG_ADDR_FMT "), "
	     "send (" VG_ADDR_FMT ", " VG_ADDR_FMT ", " VG_ADDR_FMT ", " VG_ADDR_FMT "), "
	     "inline (" VG_ADDR_FMT "; %x, %x, " VG_ADDR_FMT ")",
	     (flags & VG_IPC_RECEIVE) ? "R" : "-",
	     (flags & VG_IPC_RECEIVE_NONBLOCKING) ? "N" : "B",
	     (flags & VG_IPC_RECEIVE_ACTIVATE) ? "A" : "-",
	     (flags & VG_IPC_RECEIVE_SET_THREAD_TO_CALLER) ? "T" : "-",
	     (flags & VG_IPC_RECEIVE_SET_ASROOT_TO_CALLERS) ? "A" : "-",
	     (flags & VG_IPC_RECEIVE_INLINE) ? "I" : "-",
	     (flags & VG_IPC_SEND) ? "S" : "-",
	     (flags & VG_IPC_SEND_NONBLOCKING) ? "N" : "B",
	     (flags & VG_IPC_SEND_ACTIVATE) ? "A" : "-",
	     (flags & VG_IPC_SEND_SET_THREAD_TO_CALLER) ? "T" : "-",
	     (flags & VG_IPC_SEND_SET_ASROOT_TO_CALLERS) ? "A" : "-",
	     (flags & VG_IPC_SEND_INLINE) ? "I" : "-",
	     (flags & VG_IPC_RETURN) ? "R" : "-",
	     (flags & VG_IPC_RECEIVE_INLINE_CAP1) ? "C" : "-",
	     (flags & VG_IPC_SEND_INLINE_WORD1) ? "1" : "-",
	     (flags & VG_IPC_SEND_INLINE_WORD2) ? "2" : "-",
	     (flags & VG_IPC_SEND_INLINE_CAP1) ? "C" : "-",
	     flags,
	     VG_ADDR_PRINTF (recv_activity), VG_ADDR_PRINTF (recv_messenger),
	     VG_ADDR_PRINTF (recv_buf),
	     VG_ADDR_PRINTF (send_activity), VG_ADDR_PRINTF (target_messenger),
	     VG_ADDR_PRINTF (send_messenger), VG_ADDR_PRINTF (send_buf),
	     VG_ADDR_PRINTF (recv_inline_cap),
	     inline_word1, inline_word2, VG_ADDR_PRINTF (inline_cap));

      if ((flags & VG_IPC_RECEIVE))
	/* IPC includes a receive phase.  */
	{
	  principal = activity;
	  if (! VG_ADDR_IS_VOID (recv_activity))
	    {
	      principal = (struct activity *) OBJECT (&thread->aspace,
						      recv_activity,
						      vg_cap_activity, false,
						      NULL);
	      if (! principal)
		{
		  DEBUG (0, "Invalid receive activity.");
		  REPLY (ENOENT);
		}
	    }

	  struct messenger *messenger
	    = (struct messenger *) OBJECT (&thread->aspace,
					   recv_messenger, vg_cap_messenger,
					   true, NULL);
	  if (! messenger)
	    {
	      DEBUG (0, "IPC includes receive phase, however, "
		     "no receive messenger provided.");
	      REPLY (EINVAL);
	    }

	  if ((flags & VG_IPC_RECEIVE_INLINE))
	    {
	      messenger->out_of_band = false;
	      if ((flags & VG_IPC_RECEIVE_INLINE_CAP1))
		messenger->inline_caps[0] = recv_inline_cap;
	    }
	  else
	    {
	      messenger->out_of_band = true;
	      if (unlikely (! VG_ADDR_IS_VOID (recv_buf)))
		/* Associate RECV_BUF with RECV_MESSENGER.  */
		messenger->buffer = CAP (&thread->aspace, recv_buf,
					 vg_cap_page, true);
	    }

	  if (unlikely ((flags & VG_IPC_RECEIVE_SET_THREAD_TO_CALLER)))
	    messenger->thread = object_to_cap ((struct vg_object *) thread);

	  if (unlikely ((flags & VG_IPC_RECEIVE_SET_ASROOT_TO_CALLERS)))
	    messenger->as_root = thread->aspace;

	  messenger->activate_on_receive = (flags & VG_IPC_RECEIVE_ACTIVATE);

	  /* See if there is a messenger trying to send to
	     MESSENGER.  */
	  struct messenger *sender;
	  object_wait_queue_for_each (principal,
				      (struct vg_object *) messenger, sender)
	    if (sender->wait_reason == MESSENGER_WAIT_TRANSFER_MESSAGE)
	      /* There is.  Transfer SENDER's message to MESSENGER.  */
	      {
		object_wait_queue_unlink (principal, sender);

		assert (messenger->blocked);
		messenger->blocked = 0;
		bool ret = messenger_message_transfer (principal,
						       messenger, sender,
						       true);
		assert (ret);

		break;
	      }

	  if (! sender)
	    /* There was no sender waiting.  */
	    {
	      if ((flags & VG_IPC_RECEIVE_NONBLOCKING))
		/* The receive phase is non-blocking.  */
		REPLY (EWOULDBLOCK);
	      else
		/* Unblock MESSENGER.  */
		messenger->blocked = 0;
	    }
	}

      if (! (flags & VG_IPC_SEND))
	/* No send phase.  */
	{
	  if ((flags & VG_IPC_RETURN))
	    /* But a return phase.  */
	    REPLY (0);

	  continue;
	}

      /* Send phase.  */

      if ((flags & VG_IPC_SEND_INLINE))
	label = inline_word1;

      principal = activity;
      struct vg_cap principal_cap;
      if (! VG_ADDR_IS_VOID (send_activity))
	{
	  /* We need the cap below, otherwise, we could just use
	     OBJECT.  */
	  principal_cap = CAP (&thread->aspace,
			       send_activity, vg_cap_activity, false);
	  principal = (struct activity *) vg_cap_to_object (principal,
							 &principal_cap);
	  if (! principal)
	    {
	      DEBUG (4, "Dangling pointer at " VG_ADDR_FMT,
		     VG_ADDR_PRINTF (send_activity));
	      REPLY (ENOENT);
	    }
	}
      else
	{
	  principal_cap = thread->activity;
	  principal = activity;
	}

      struct messenger *source
	= (struct messenger *) OBJECT (&thread->aspace,
				       send_messenger, vg_cap_messenger,
				       true, NULL);
      if (unlikely (! source))
	{
	  DEBUG (0, "Source not valid.");
	  REPLY (ENOENT);
	}

      if (! (flags & VG_IPC_SEND_INLINE)
	  && unlikely (! VG_ADDR_IS_VOID (send_buf)))
	source->buffer = CAP (&thread->aspace, send_buf, vg_cap_page, true);

      if (unlikely ((flags & VG_IPC_SEND_SET_THREAD_TO_CALLER)))
	source->thread = object_to_cap ((struct vg_object *) thread);

      if (unlikely ((flags & VG_IPC_SEND_SET_ASROOT_TO_CALLERS)))
	source->as_root = thread->aspace;

      source->activate_on_send = (flags & VG_IPC_SEND_ACTIVATE);

      bool target_writable = true;
      struct vg_object *target;
      /* We special case VOID to mean the current thread.  */
      if (VG_ADDR_IS_VOID (target_messenger))
	target = (struct vg_object *) thread;
      else
	target = OBJECT (&thread->aspace, target_messenger, -1, false,
			 &target_writable);
      if (! target)
	{
	  DEBUG (0, "Target not valid.");
	  REPLY (ENOENT);
	}

      if (object_type (target) == vg_cap_messenger && ! target_writable)
	/* TARGET is a weak reference to a messenger.  Forward the
	   message.  */
	{
	  DEBUG (5, "IPC: " VG_OID_FMT " -> " VG_OID_FMT,
		 VG_OID_PRINTF (object_oid ((struct vg_object *) source)),
		 VG_OID_PRINTF (object_oid ((struct vg_object *) target)));

	  if ((flags & VG_IPC_SEND_INLINE))
	    {
	      source->out_of_band = false;
	      source->inline_words[0] = inline_word1;
	      source->inline_words[1] = inline_word2;
	      source->inline_caps[0] = inline_cap;

	      if ((flags & VG_IPC_SEND_INLINE_WORD1)
		  && (flags & VG_IPC_SEND_INLINE_WORD2))
		source->inline_word_count = 2;
	      else if ((flags & VG_IPC_SEND_INLINE_WORD1))
		source->inline_word_count = 1;
	      else
		source->inline_word_count = 0;

	      if ((flags & VG_IPC_SEND_INLINE_CAP1))
		source->inline_cap_count = 1;
	      else
		source->inline_cap_count = 0;
	    }
	  else
	    source->out_of_band = true;

	  if (messenger_message_transfer (principal,
					  (struct messenger *) target,
					  source,
					  ! (flags & VG_IPC_SEND_NONBLOCKING)))
	    /* The messenger has been enqueued.  */
	    {
	      if ((flags & VG_IPC_RETURN))
		REPLY (0);
	      continue;
	    }
	  else
	    REPLY (ETIMEDOUT);
	}

      /* TARGET designates a kernel implemented object.  Implement
	 it.  */

      /* The reply messenger (if any).  */
      struct messenger *reply = NULL;

      /* We are now so far that we should not reply to the caller but
	 to TARGET.  Set up our handy REPLY macro to do so.  */
#undef REPLY
  /* Send a reply indicating that an error with the error code ERR_.
     Go to the start of the server loop.  */
#define REPLY(err_)						\
      do							\
	{							\
	  if (err_)						\
	    DEBUG (0, DEBUG_BOLD ("Returning error %d"), err_);	\
	  if (reply)						\
	    if (rpc_error_reply (principal, reply, err_))	\
	      DEBUG (0, DEBUG_BOLD ("Failed to send reply"));	\
	  goto out;						\
	}							\
      while (0)


      struct vg_message *message;
      if ((flags & VG_IPC_SEND_INLINE))
	{
	  message = reply_buffer;
	  vg_message_clear (message);
	  if ((flags & VG_IPC_SEND_INLINE_WORD1))
	    vg_message_append_word (message, inline_word1);
	  if ((flags & VG_IPC_SEND_INLINE_WORD2))
	    vg_message_append_word (message, inline_word2);
	  if ((flags & VG_IPC_SEND_INLINE_CAP1))
	    vg_message_append_cap (message, inline_cap);
	}
      else
	{
	  if (source->buffer.type != vg_cap_page)
	    {
	      DEBUG (0, "Sender user-buffer has wrong type: %s",
		     vg_cap_type_string (source->buffer.type));
	      REPLY (EINVAL);
	    }
	  message = (struct vg_message *) vg_cap_to_object (principal,
							 &source->buffer);
	  if (! message)
	    {
	      DEBUG (0, "Sender user-buffer has wrong type: %s",
		     vg_cap_type_string (source->buffer.type));
	      REPLY (EINVAL);
	    }
	}

      label = vg_message_word (message, 0);

      do_debug (5)
	{
	  DEBUG (0, "");
	  vg_message_dump (message);
	}

      /* Extract the reply messenger (if any).  */
      if (vg_message_cap_count (message) > 0)
	/* We only look for a messenger here.  We know that any reply
	   that a kernel object generates that is sent to a kernel
	   object will just result in a discarded EINVAL.  */
	reply = (struct messenger *)
	  OBJECT (&thread->aspace,
		  vg_message_cap (message, 0),
		  vg_cap_rmessenger, false, NULL);

      /* There are a number of methods that look up an object relative
	 to the invoked object.  Generate an appropriate root for
	 them.  */
      struct vg_cap target_root_cap;
      struct vg_cap *target_root;
      if (likely (target == (struct vg_object *) thread))
	target_root = &thread->aspace;
      else if (object_type (target) == vg_cap_thread)
	target_root = &((struct thread *) target)->aspace;
      else
	{
	  target_root_cap = object_to_cap (target);
	  target_root = &target_root_cap;
	}

      DEBUG (4, VG_OID_FMT " %s(%llx) -> " VG_OID_FMT " %s(%llx)",
	     VG_OID_PRINTF (object_oid ((struct vg_object *) source)),
	     vg_cap_type_string (object_type ((struct vg_object *) source)),
	     source->id,
	     VG_OID_PRINTF (object_oid ((struct vg_object *) target)),
	     vg_cap_type_string (object_type (target)),
	     object_type (target) == vg_cap_messenger
	     ? ((struct messenger *) target)->id : 0);
      if (reply)
	DEBUG (4, "reply to: " VG_OID_FMT "(%llx)",
	       VG_OID_PRINTF (object_oid ((struct vg_object *) reply)),
	       reply->id);

      switch (label)
	{
	case VG_write:
	  {
	    struct io_buffer buffer;
	    err = vg_write_send_unmarshal (message, &buffer, NULL);
	    if (! err)
	      {
		int i;
		for (i = 0; i < buffer.len; i ++)
		  putchar (buffer.data[i]);
	      }

	    vg_write_reply (activity, reply);
	    break;
	  }
	case VG_read:
	  {
	    int max;
	    err = vg_read_send_unmarshal (message, &max, NULL);
	    if (err)
	      {
		DEBUG (0, "Read error!");
		REPLY (EINVAL);
	      }

	    struct io_buffer buffer;
	    buffer.len = 0;

	    if (max > 0)
	      {
		buffer.len = 1;
		buffer.data[0] = getchar ();
	      }

	    vg_read_reply (activity, reply, buffer);
	    break;
	  }

	case VG_fault:
	  {
	    uintptr_t start;
	    int max;

	    err = vg_fault_send_unmarshal (message, &start, &max, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(%p, %d)", (void *) start, max);

	    start &= ~(PAGESIZE - 1);

	    vg_fault_reply (activity, reply, 0);
	    int limit = (L4_NUM_MRS - 1
			 - l4_untyped_words (l4_msg_msg_tag (msg)))
	      * sizeof (uintptr_t) / sizeof (l4_map_item_t);
	    if (max > limit)
	      max = limit;

	    l4_map_item_t map_items[max];
	    int count = 0;
	    for (count = 0; count < max; count ++, start += PAGESIZE)
	      {
		struct vg_cap cap;
		bool writable;
		cap = as_object_lookup_rel (activity, target_root,
					    vg_addr_chop (VG_PTR_TO_ADDR (start),
						       PAGESIZE_LOG2),
					    vg_cap_rpage, &writable);

		if (cap.type != vg_cap_page && cap.type != vg_cap_rpage)
		  break;

		if (! writable && cap.discardable)
		  cap.discardable = false;

		struct vg_object *page = vg_cap_to_object (activity, &cap);
		if (! page)
		  break;

		object_desc_unmap (object_to_object_desc (page));
		object_to_object_desc (page)->mapped = true;

		int access = L4_FPAGE_READABLE;
		if (writable)
		  access |= L4_FPAGE_WRITABLE;

		l4_fpage_t fpage = l4_fpage ((uintptr_t) page, PAGESIZE);
		fpage = l4_fpage_add_rights (fpage, access);
		map_items[count] = l4_map_item (fpage, start);

		DEBUG (4, "Prefault %d: " DEBUG_BOLD ("%x") " <- %x/%x %s",
		       count + 1, start,
		       l4_address (fpage), l4_rights (fpage),
		       vg_cap_type_string (cap.type));
	      }

	    if (count > 0)
	      DEBUG (4, "Prefaulted %d pages (%d/%d)", count,
		     l4_untyped_words (l4_msg_msg_tag (msg)),
		     l4_typed_words (l4_msg_msg_tag (msg)));

	    vg_fault_reply (activity, reply, count);
	    int i;
	    for (i = 0; i < count; i ++)
	      l4_msg_append_map_item (msg, map_items[i]);

	    break;
	  }

	case VG_folio_alloc:
	  {
	    if (object_type (target) != vg_cap_activity_control)
	      {
		DEBUG (0, "target " VG_ADDR_FMT " not an activity but a %s",
		       VG_ADDR_PRINTF (target_messenger),
		       vg_cap_type_string (object_type (target)));
		REPLY (EINVAL);
	      }

	    struct activity *activity = (struct activity *) target;

	    struct vg_folio_policy policy;
	    err = vg_folio_alloc_send_unmarshal (message, &policy, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(" VG_ADDR_FMT ")", VG_ADDR_PRINTF (target_messenger));

	    struct vg_folio *folio = folio_alloc (activity, policy);
	    if (! folio)
	      REPLY (ENOMEM);

	    vg_folio_alloc_reply (principal, reply,
				  object_to_cap ((struct vg_object *) folio));
	    break;
	  }

	case VG_folio_free:
	  {
	    if (object_type (target) != vg_cap_folio)
	      REPLY (EINVAL);

	    struct vg_folio *folio = (struct vg_folio *) target;

	    err = vg_folio_free_send_unmarshal (message, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(" VG_ADDR_FMT ")", VG_ADDR_PRINTF (target_messenger));

	    folio_free (principal, folio);

	    vg_folio_free_reply (activity, reply);
	    break;
	  }

	case VG_folio_object_alloc:
	  {
	    if (object_type (target) != vg_cap_folio)
	      REPLY (EINVAL);

	    struct vg_folio *folio = (struct vg_folio *) target;

	    uint32_t idx;
	    uint32_t type;
	    struct vg_object_policy policy;
	    uintptr_t return_code;

	    err = vg_folio_object_alloc_send_unmarshal (message,
							&idx, &type, &policy,
							&return_code, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(" VG_ADDR_FMT ", %d (" VG_ADDR_FMT "), %s, (%s, %d), %d)",
		   VG_ADDR_PRINTF (target_messenger), idx,
		   vg_addr_depth (target_messenger) + VG_FOLIO_OBJECTS_LOG2
		   <= VG_ADDR_BITS
		   ? VG_ADDR_PRINTF (vg_addr_extend (target_messenger,
					       idx, VG_FOLIO_OBJECTS_LOG2))
		   : VG_ADDR_PRINTF (VG_ADDR_VOID),
		   vg_cap_type_string (type),
		   policy.discardable ? "discardable" : "precious",
		   policy.priority,
		   return_code);

	    if (idx >= VG_FOLIO_OBJECTS)
	      REPLY (EINVAL);

	    if (! (VG_CAP_TYPE_MIN <= type && type <= VG_CAP_TYPE_MAX))
	      REPLY (EINVAL);

	    struct vg_cap cap;
	    cap = folio_object_alloc (principal,
				      folio, idx, type, policy, return_code);

	    struct vg_cap weak = cap;
	    weak.type = vg_cap_type_weaken (cap.type);

	    vg_folio_object_alloc_reply (activity, reply, cap, weak);
	    break;
	  }

	case VG_folio_policy:
	  {
	    if (object_type (target) != vg_cap_folio)
	      REPLY (EINVAL);

	    struct vg_folio *folio = (struct vg_folio *) target;

	    uintptr_t flags;
	    struct vg_folio_policy in, out;

	    err = vg_folio_policy_send_unmarshal (message, &flags, &in, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(" VG_ADDR_FMT ", %d)",
		   VG_ADDR_PRINTF (target_messenger), flags);

	    folio_policy (principal, folio, flags, in, &out);

	    vg_folio_policy_reply (activity, reply, out);
	    break;
	  }

	case VG_cap_copy:
	  {
	    vg_addr_t source_as_addr;
	    vg_addr_t source_addr;
	    struct vg_cap source;
	    vg_addr_t target_as_addr;
	    vg_addr_t target_addr;
	    uint32_t flags;
	    struct vg_cap_properties properties;

	    err = vg_cap_copy_send_unmarshal (message,
					      &target_addr, 
					      &source_as_addr, &source_addr,
					      &flags, &properties, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, "(" VG_ADDR_FMT "@" VG_ADDR_FMT " <- "
		   VG_ADDR_FMT "@" VG_ADDR_FMT ", %s%s%s%s%s%s, %s/%d %lld/%d %d/%d",

		   VG_ADDR_PRINTF (target_messenger),
		   VG_ADDR_PRINTF (target_addr),
		   VG_ADDR_PRINTF (source_as_addr),
		   VG_ADDR_PRINTF (source_addr),
		   
		   VG_CAP_COPY_COPY_ADDR_TRANS_SUBPAGE & flags
		   ? "copy subpage/" : "",
		   VG_CAP_COPY_COPY_ADDR_TRANS_GUARD & flags
		   ? "copy trans guard/" : "",
		   VG_CAP_COPY_COPY_SOURCE_GUARD & flags
		   ? "copy src guard/" : "",
		   VG_CAP_COPY_WEAKEN & flags ? "weak/" : "",
		   VG_CAP_COPY_DISCARDABLE_SET & flags ? "discardable/" : "",
		   VG_CAP_COPY_PRIORITY_SET & flags ? "priority" : "",

		   properties.policy.discardable ? "discardable" : "precious",
		   properties.policy.priority,
		   VG_CAP_ADDR_TRANS_GUARD (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_GUARD_BITS (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_SUBPAGE (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_SUBPAGES (properties.addr_trans));

	    struct vg_cap *target;
	    target = SLOT (target_root, target_addr);

	    target_root = ROOT (source_as_addr);
	    source = CAP (target_root, source_addr, -1, false);

	    if ((flags & ~(VG_CAP_COPY_COPY_ADDR_TRANS_SUBPAGE
			   | VG_CAP_COPY_COPY_ADDR_TRANS_GUARD
			   | VG_CAP_COPY_COPY_SOURCE_GUARD
			   | VG_CAP_COPY_WEAKEN
			   | VG_CAP_COPY_DISCARDABLE_SET
			   | VG_CAP_COPY_PRIORITY_SET)))
	      REPLY (EINVAL);

	    DEBUG (4, "(target: (" VG_ADDR_FMT ") " VG_ADDR_FMT ", "
		   "source: (" VG_ADDR_FMT ") " VG_ADDR_FMT ", "
		   "%s|%s, %s {%llx/%d %d/%d})",
		   VG_ADDR_PRINTF (target_as_addr),
		   VG_ADDR_PRINTF (target_addr),
		   VG_ADDR_PRINTF (source_as_addr),
		   VG_ADDR_PRINTF (source_addr),
		   flags & VG_CAP_COPY_COPY_ADDR_TRANS_GUARD ? "copy trans"
		   : (flags & VG_CAP_COPY_COPY_SOURCE_GUARD ? "source"
		      : "preserve"),
		   flags & VG_CAP_COPY_COPY_ADDR_TRANS_SUBPAGE ? "copy"
		   : "preserve",
		   flags & VG_CAP_COPY_WEAKEN ? "weaken" : "no weaken",
		   VG_CAP_ADDR_TRANS_GUARD (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_GUARD_BITS (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_SUBPAGE (properties.addr_trans),
		   VG_CAP_ADDR_TRANS_SUBPAGES (properties.addr_trans));

	    bool r = vg_cap_copy_x (principal,
				 VG_ADDR_VOID, target, VG_ADDR_VOID,
				 VG_ADDR_VOID, source, VG_ADDR_VOID,
				 flags, properties);
	    if (! r)
	      REPLY (EINVAL);

	    if ((flags & (VG_CAP_COPY_DISCARDABLE_SET
			  | VG_CAP_COPY_PRIORITY_SET)))
	      /* The caller changed the policy.  Also change it on the
		 object.  */
	      {
		struct vg_object *object = cap_to_object_soft (principal,
							       target);
		if (object)
		  {
		    struct object_desc *desc
		      = object_to_object_desc (object);

		    struct vg_object_policy p = desc->policy;

		    /* XXX: This should only be allowed if TARGET
		       grants writable access to the object.  */
		    if ((flags & VG_CAP_COPY_DISCARDABLE_SET))
		      p.discardable = properties.policy.discardable;

		    /* Only the current claimant can set the
		       priority.  */
		    if ((flags & VG_CAP_COPY_PRIORITY_SET)
			&& desc->activity == principal)
		      p.priority = properties.policy.priority;

		    object_desc_claim (desc->activity, desc, p, true);
		  }
	      }

	    vg_cap_copy_reply (activity, reply);

#if 0
	    /* XXX: Surprisingly, it appears that this may be
	       more expensive than just faulting the pages
	       normally.  This needs more investivation.  */
	    if (VG_ADDR_IS_VOID (target_as_addr)
		&& vg_cap_types_compatible (target->type, vg_cap_page)
		&& VG_CAP_GUARD_BITS (target) == 0
		&& vg_addr_depth (target_addr) == VG_ADDR_BITS - PAGESIZE_LOG2)
	      /* The target address space is the caller's.  The target
		 object appears to be a page.  It seems to be
		 installed at a point where it would appear in the
		 hardware address space.  If this is really the case,
		 then we can map it now and save a fault later.  */
	      {
		profile_region ("vg_cap_copy-prefault");

		struct vg_cap cap = *target;
		if (target->type == vg_cap_rpage)
		  cap.discardable = false;

		struct vg_object *page = cap_to_object_soft (principal, &cap);
		if (page)
		  {
		    object_to_object_desc (page)->mapped = true;

		    l4_fpage_t fpage
		      = l4_fpage ((uintptr_t) page, PAGESIZE);
		    fpage = l4_fpage_add_rights (fpage, L4_FPAGE_READABLE);
		    if (cap.type == vg_cap_page)
		      fpage = l4_fpage_add_rights (fpage,
						   L4_FPAGE_WRITABLE);

		    uintptr_t page_addr = vg_addr_prefix (target_addr);

		    l4_map_item_t map_item = l4_map_item (fpage, page_addr);

		    l4_msg_append_map_item (msg, map_item);
		  }

		profile_region_end ();
	      }
#endif

	    break;
	  }

	case VG_cap_rubout:
	  {
	    vg_addr_t addr;

	    err = vg_cap_rubout_send_unmarshal (message, &addr, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT "@" VG_ADDR_FMT,
		   VG_ADDR_PRINTF (target_messenger),
		   VG_ADDR_PRINTF (addr));

	    /* We don't look up the argument directly as we need to
	       respect any subpage specification for cappages.  */
	    struct vg_cap *slot = SLOT (target_root, addr);

	    cap_shootdown (principal, slot);

	    memset (target, 0, sizeof (*slot));

	    vg_cap_rubout_reply (activity, reply);
	    break;
	  }

	case VG_cap_read:
	  {
	    vg_addr_t cap_addr;

	    err = vg_cap_read_send_unmarshal (message, &cap_addr, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT "@" VG_ADDR_FMT,
		   VG_ADDR_PRINTF (target_messenger),
		   VG_ADDR_PRINTF (cap_addr));

	    struct vg_cap cap = CAP (target_root, cap_addr, -1, false);
	    /* Even if CAP.TYPE is not void, the cap may not designate
	       an object.  Looking up the object will set CAP.TYPE to
	       vg_cap_void if this is the case.  */
	    if (cap.type != vg_cap_void)
	      vg_cap_to_object (principal, &cap);

	    vg_cap_read_reply (activity, reply, cap.type,
			       VG_CAP_PROPERTIES_GET (cap));
	    break;
	  }

	case VG_object_discarded_clear:
	  {
	    vg_addr_t object_addr;

	    err = vg_object_discarded_clear_send_unmarshal
	      (message, &object_addr, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT, VG_ADDR_PRINTF (object_addr));

	    /* We can't look up the object use OBJECT as object_lookup
	       returns NULL if the object's discardable bit is set!
	       Instead, we lookup the capability, find the object's
	       folio and then clear its discarded bit.  */
	    struct vg_cap cap = CAP (&thread->aspace, object_addr, -1, true);
	    if (cap.type == vg_cap_void)
	      REPLY (ENOENT);
	    if (vg_cap_type_weak_p (cap.type))
	      REPLY (EPERM);

	    int idx = (cap.oid % (1 + VG_FOLIO_OBJECTS)) - 1;
	    vg_oid_t foid = cap.oid - idx - 1;

	    struct vg_folio *folio = (struct vg_folio *)
	      object_find (activity, foid, VG_OBJECT_POLICY_VOID);

	    if (folio_object_version (folio, idx) != cap.version)
	      REPLY (ENOENT);

	    bool was_discarded = folio_object_discarded (folio, idx);
	    folio_object_discarded_set (folio, idx, false);

	    vg_object_discarded_clear_reply (activity, reply);

#if 0
	    /* XXX: Surprisingly, it appears that this may be more
	       expensive than just faulting the pages normally.  This
	       needs more investivation.  */
	    if (was_discarded
		&& cap.type == vg_cap_page
		&& VG_CAP_GUARD_BITS (&cap) == 0
		&& (vg_addr_depth (object_addr) == VG_ADDR_BITS - PAGESIZE_LOG2))
	      /* The target object was discarded, appears to be a page
		 and seems to be installed at a point where it would
		 appear in the hardware address space.  If this is
		 really the case, then we can map it now and save a
		 fault later.  */
	      {
		profile_region ("object_discard-prefault");

		struct vg_object *page = vg_cap_to_object (principal, &cap);
		if (page)
		  {
		    object_to_object_desc (page)->mapped = true;

		    l4_fpage_t fpage = l4_fpage ((uintptr_t) page, PAGESIZE);
		    fpage = l4_fpage_add_rights (fpage,
						 L4_FPAGE_READABLE
						 | L4_FPAGE_WRITABLE);

		    uintptr_t page_addr = vg_addr_prefix (object_addr);

		    l4_map_item_t map_item = l4_map_item (fpage, page_addr);

		    l4_msg_append_map_item (msg, map_item);

		    DEBUG (4, "Prefaulting "VG_ADDR_FMT"(%x) <- %p (%x/%x/%x)",
			   VG_ADDR_PRINTF (object_addr), page_addr,
			   page, l4_address (fpage), l4_size (fpage),
			   l4_rights (fpage));
		  }

		profile_region_end ();
	      }
#endif

	    break;
	  }

	case VG_object_discard:
	  {
	    err = vg_object_discard_send_unmarshal (message, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT, VG_ADDR_PRINTF (target_messenger));

	    struct vg_folio *folio = objects_folio (principal, target);

	    folio_object_content_set (folio,
				      objects_folio_offset (target), false);

	    vg_object_discard_reply (activity, reply);
	    break;
	  }

	case VG_object_status:
	  {
	    bool clear;
	    err = vg_object_status_send_unmarshal (message, &clear, NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT ", %sclear",
		   VG_ADDR_PRINTF (target_messenger), clear ? "" : "no ");

	    struct object_desc *desc = object_to_object_desc (target);
	    uintptr_t status = (desc->user_referenced
				? vg_object_referenced : 0)
	      | (desc->user_dirty ? vg_object_dirty : 0);

	    if (clear)
	      {
		desc->user_referenced = 0;
		desc->user_dirty = 0;
	      }

	    vg_object_status_reply (activity, reply, status);
	    break;
	  }

	case VG_object_name:
	  {
	    struct vg_object_name name;
	    err = vg_object_name_send_unmarshal (message, &name, NULL);

	    if (object_type (target) == vg_cap_activity_control)
	      {
		struct activity *a = (struct activity *) target;

		memcpy (a->name.name, name.name, sizeof (name));
		a->name.name[sizeof (a->name.name) - 1] = 0;
	      }
	    else if (object_type (target) == vg_cap_thread)
	      {
		struct thread *t = (struct thread *) target;

		memcpy (t->name.name, name.name, sizeof (name));
		t->name.name[sizeof (t->name.name) - 1] = 0;
	      }

	    vg_object_name_reply (activity, reply);
	    break;
	  }

	case VG_thread_exregs:
	  {
	    if (object_type (target) != vg_cap_thread)
	      REPLY (EINVAL);
	    struct thread *t = (struct thread *) target;

	    struct vg_thread_exregs_in in;
	    uintptr_t control;
	    vg_addr_t aspace_addr;
	    vg_addr_t activity_addr;
	    vg_addr_t utcb_addr;
	    vg_addr_t exception_messenger_addr;
	    err = vg_thread_exregs_send_unmarshal
	      (message, &control, &in,
	       &aspace_addr, &activity_addr, &utcb_addr,
	       &exception_messenger_addr,
	       NULL);
	    if (err)
	      REPLY (err);

	    int d = 4;
	    DEBUG (d, "%s%s" VG_ADDR_FMT "(%x): %s%s%s%s %s%s%s%s %s%s%s %s%s",
		   t->name.name[0] ? t->name.name : "",
		   t->name.name[0] ? ": " : "",
		   VG_ADDR_PRINTF (target_messenger), t->tid,
		   (control & VG_EXREGS_SET_UTCB) ? "U" : "-",
		   (control & VG_EXREGS_SET_EXCEPTION_MESSENGER) ? "E" : "-",
		   (control & VG_EXREGS_SET_ASPACE) ? "R" : "-",
		   (control & VG_EXREGS_SET_ACTIVITY) ? "A" : "-",
		   (control & VG_EXREGS_SET_SP) ? "S" : "-",
		   (control & VG_EXREGS_SET_IP) ? "I" : "-",
		   (control & VG_EXREGS_SET_EFLAGS) ? "F" : "-",
		   (control & VG_EXREGS_SET_USER_HANDLE) ? "U" : "-",
		   (control & _L4_XCHG_REGS_CANCEL_RECV) ? "R" : "-",
		   (control & _L4_XCHG_REGS_CANCEL_SEND) ? "S" : "-",
		   (control & _L4_XCHG_REGS_CANCEL_IPC) ? "I" : "-",
		   (control & _L4_XCHG_REGS_HALT) ? "H" : "-",
		   (control & _L4_XCHG_REGS_SET_HALT) ? "Y" : "N");

	    if ((control & VG_EXREGS_SET_UTCB))
	      DEBUG (d, "utcb: " VG_ADDR_FMT, VG_ADDR_PRINTF (utcb_addr));
	    if ((control & VG_EXREGS_SET_EXCEPTION_MESSENGER))
	      DEBUG (d, "exception messenger: " VG_ADDR_FMT,
		     VG_ADDR_PRINTF (exception_messenger_addr));
	    if ((control & VG_EXREGS_SET_ASPACE))
	      DEBUG (d, "aspace: " VG_ADDR_FMT, VG_ADDR_PRINTF (aspace_addr));
	    if ((control & VG_EXREGS_SET_ACTIVITY))
	      DEBUG (d, "activity: " VG_ADDR_FMT,
		     VG_ADDR_PRINTF (activity_addr));
	    if ((control & VG_EXREGS_SET_SP))
	      DEBUG (d, "sp: %p", (void *) in.sp);
	    if ((control & VG_EXREGS_SET_IP))
	      DEBUG (d, "ip: %p", (void *) in.ip);
	    if ((control & VG_EXREGS_SET_EFLAGS))
	      DEBUG (d, "eflags: %p", (void *) in.eflags);
	    if ((control & VG_EXREGS_SET_USER_HANDLE))
	      DEBUG (d, "user_handle: %p", (void *) in.user_handle);

	    struct vg_cap aspace = VG_CAP_VOID;
	    if ((VG_EXREGS_SET_ASPACE & control))
	      aspace = CAP (&source->as_root, aspace_addr, -1, false);

	    struct vg_cap a = VG_CAP_VOID;
	    if ((VG_EXREGS_SET_ACTIVITY & control))
	      {
		/* XXX: Remove this hack... */
		if (VG_ADDR_IS_VOID (activity_addr))
		  a = thread->activity;
		else
		  a = CAP (&source->as_root,
			   activity_addr, vg_cap_activity, false);
	      }

	    struct vg_cap utcb = VG_CAP_VOID;
	    if ((VG_EXREGS_SET_UTCB & control))
	      utcb = CAP (&source->as_root, utcb_addr, vg_cap_page, true);

	    struct vg_cap exception_messenger = VG_CAP_VOID;
	    if ((VG_EXREGS_SET_EXCEPTION_MESSENGER & control))
	      exception_messenger
		= CAP (&source->as_root, exception_messenger_addr,
		       vg_cap_rmessenger, false);

	    struct vg_cap aspace_out = t->aspace;
	    struct vg_cap activity_out = t->activity;
	    struct vg_cap utcb_out = t->utcb;
	    struct vg_cap exception_messenger_out = t->exception_messenger;

	    struct vg_thread_exregs_out out;
	    out.sp = in.sp;
	    out.ip = in.ip;
	    out.eflags = in.eflags;
	    out.user_handle = in.user_handle;

	    err = thread_exregs (principal, t, control,
				 aspace, in.aspace_cap_properties_flags,
				 in.aspace_cap_properties,
				 a, utcb, exception_messenger,
				 &out.sp, &out.ip,
				 &out.eflags, &out.user_handle);
	    if (err)
	      REPLY (err);

	    vg_thread_exregs_reply (activity, reply, out,
				    aspace_out, activity_out,
				    utcb_out, exception_messenger_out);

	    break;
	  }

	case VG_thread_id:
	  {
	    if (object_type (target) != vg_cap_thread)
	      REPLY (EINVAL);
	    struct thread *t = (struct thread *) target;

	    err = vg_thread_id_send_unmarshal (message, NULL);
	    if (err)
	      REPLY (err);

	    vg_thread_id_reply (activity, reply, t->tid);
	    break;	    
	  }

	case VG_object_reply_on_destruction:
	  {
	    err = vg_object_reply_on_destruction_send_unmarshal (message,
								 NULL);
	    if (err)
	      REPLY (err);

	    DEBUG (4, VG_ADDR_FMT, VG_ADDR_PRINTF (target_messenger));

	    reply->wait_reason = MESSENGER_WAIT_DESTROY;
	    object_wait_queue_enqueue (principal, target, reply);

	    break;
	  }

	case VG_activity_policy:
	  {
	    if (object_type (target) != vg_cap_activity_control)
	      {
		DEBUG (0, "expects an activity, not a %s",
		       vg_cap_type_string (object_type (target)));
		REPLY (EINVAL);
	      }
	    struct activity *activity = (struct activity *) target;

	    uintptr_t flags;
	    struct vg_activity_policy in;

	    err = vg_activity_policy_send_unmarshal (message, &flags, &in,
						     NULL);
	    if (err)
	      REPLY (err);

	    int d = 4;
	    DEBUG (d, "(%s) child: %s%s; sibling: %s%s; storage: %s",
		   target_writable ? "strong" : "weak",
		   (flags & VG_ACTIVITY_POLICY_CHILD_REL_PRIORITY_SET) ? "P" : "-",
		   (flags & VG_ACTIVITY_POLICY_CHILD_REL_WEIGHT_SET) ? "W" : "-",
		   (flags & VG_ACTIVITY_POLICY_SIBLING_REL_PRIORITY_SET)
		   ? "P" : "-",
		   (flags & VG_ACTIVITY_POLICY_SIBLING_REL_WEIGHT_SET) ? "W" : "-",
		   (flags & VG_ACTIVITY_POLICY_STORAGE_SET) ? "P" : "-");

	    if ((flags & VG_ACTIVITY_POLICY_CHILD_REL_PRIORITY_SET))
	      DEBUG (d, "Child priority: %d", in.child_rel.priority);
	    if ((flags & VG_ACTIVITY_POLICY_CHILD_REL_WEIGHT_SET))
	      DEBUG (d, "Child weight: %d", in.child_rel.weight);
	    if ((flags & VG_ACTIVITY_POLICY_SIBLING_REL_PRIORITY_SET))
	      DEBUG (d, "Sibling priority: %d", in.sibling_rel.priority);
	    if ((flags & VG_ACTIVITY_POLICY_SIBLING_REL_WEIGHT_SET))
	      DEBUG (d, "Sibling weight: %d", in.sibling_rel.weight);
	    if ((flags & VG_ACTIVITY_POLICY_STORAGE_SET))
	      DEBUG (d, "Storage: %d", in.folios);

	    if (! target_writable
		&& (flags & (VG_ACTIVITY_POLICY_STORAGE_SET
			     | VG_ACTIVITY_POLICY_SIBLING_REL_SET)))
	      REPLY (EPERM);

	    vg_activity_policy_reply (principal, reply, activity->policy);

	    if ((flags & (VG_ACTIVITY_POLICY_CHILD_REL_PRIORITY_SET
			  | VG_ACTIVITY_POLICY_CHILD_REL_WEIGHT_SET
			  | VG_ACTIVITY_POLICY_SIBLING_REL_PRIORITY_SET
			  | VG_ACTIVITY_POLICY_SIBLING_REL_WEIGHT_SET
			  | VG_ACTIVITY_POLICY_STORAGE_SET)))
	      {
		struct vg_activity_policy p = principal->policy;

		if ((flags & VG_ACTIVITY_POLICY_CHILD_REL_PRIORITY_SET))
		  p.child_rel.priority = in.child_rel.priority;
		if ((flags & VG_ACTIVITY_POLICY_CHILD_REL_WEIGHT_SET))
		  p.child_rel.weight = in.child_rel.weight;

		if ((flags & VG_ACTIVITY_POLICY_SIBLING_REL_PRIORITY_SET))
		  p.sibling_rel.priority = in.sibling_rel.priority;
		if ((flags & VG_ACTIVITY_POLICY_SIBLING_REL_WEIGHT_SET))
		  p.sibling_rel.weight = in.sibling_rel.weight;

		if ((flags & VG_ACTIVITY_POLICY_STORAGE_SET))
		  p.folios = in.folios;

		activity_policy_update (activity, p);
	      }

	    break;
	  }

	case VG_activity_info:
	  {
	    if (object_type (target) != vg_cap_activity_control)
	      REPLY (EINVAL);
	    struct activity *activity = (struct activity *) target;

	    uintptr_t flags;
	    uintptr_t until_period;

	    err = vg_activity_info_send_unmarshal (message,
						   &flags, &until_period,
						   NULL);
	    if (err)
	      REPLY (err);

	    int period = activity->current_period - 1;
	    if (period < 0)
	      period = (VG_ACTIVITY_STATS_PERIODS + 1) + period;

	    DEBUG (4, OBJECT_NAME_FMT ": %s%s%s(%d), "
		   "period: %d (current: %d)",
		   OBJECT_NAME_PRINTF ((struct vg_object *) activity),
		   flags & vg_activity_info_stats ? "stats" : "",
		   (flags == (vg_activity_info_pressure|vg_activity_info_stats))
		   ? ", " : "",
		   flags & vg_activity_info_pressure ? "pressure" : "",
		   flags,
		   until_period, activity->stats[period].period);
	    
	    if ((flags & vg_activity_info_stats)
		&& activity->stats[period].period > 0
		&& activity->stats[period].period >= until_period)
	      /* Return the available statistics.  */
	      {
		/* XXX: Only return valid stat buffers.  */
		struct vg_activity_info info;
		info.event = vg_activity_info_stats;

		int i;
		for (i = 0; i < VG_ACTIVITY_STATS_PERIODS; i ++)
		  {
		    period = activity->current_period - 1 - i;
		    if (period < 0)
		      period = (VG_ACTIVITY_STATS_PERIODS + 1) + period;

		    info.stats.stats[i] = activity->stats[period];
		  }

		info.stats.count = VG_ACTIVITY_STATS_PERIODS;

		vg_activity_info_reply (principal, reply, info);
	      }
	    else if (flags)
	      /* Queue thread on the activity.  */
	      {
		reply->wait_reason = MESSENGER_WAIT_ACTIVITY_INFO;
		reply->wait_reason_arg = flags;
		reply->wait_reason_arg2 = until_period;

		object_wait_queue_enqueue (principal, target, reply);
	      }
	    else
	      REPLY (EINVAL);

	    break;
	  }

	case VG_thread_activation_collect:
	  {
	    if (object_type (target) != vg_cap_thread)
	      REPLY (EINVAL);

	    err = vg_thread_activation_collect_send_unmarshal (message, NULL);
	    if (err)
	      REPLY (err);

	    thread_deliver_pending (principal, (struct thread *) target);

	    vg_thread_activation_collect_reply (principal, reply);
	    break;
	  }

	case VG_as_dump:
	  {
	    err = vg_as_dump_send_unmarshal (message, NULL);
	    if (err)
	      REPLY (err);

	    as_dump_from (principal, target_root, "");

	    vg_as_dump_reply (activity, reply);

	    break;
	  }

	case VG_futex:
	  {
	    /* Helper function to wake and requeue waiters.  */
	    int wake (int to_wake, struct vg_object *object1, int offset1,
		      int to_requeue, struct vg_object *object2, int offset2)
	    {
	      int count = 0;
	      struct messenger *m;

	      object_wait_queue_for_each (principal, object1, m)
		if (m->wait_reason == MESSENGER_WAIT_FUTEX
		    && m->wait_reason_arg == offset1)
		  /* Got a match.  */
		  {
		    if (count < to_wake)
		      {
			object_wait_queue_unlink (principal, m);

			debug (5, "Waking messenger");

			err = vg_futex_reply (principal, m, 0);
			if (err)
			  panic ("Error vg_futex waking: %d", err);

			count ++;

			if (count == to_wake && to_requeue == 0)
			  break;
		      }
		    else
		      {
			object_wait_queue_unlink (principal, m);

			m->wait_reason_arg = offset2;
			object_wait_queue_enqueue (principal, object2, m);

			count ++;

			to_requeue --;
			if (to_requeue == 0)
			  break;
		      }
		  }
	      return count;
	    }

	    void *addr1;
	    int op;
	    int val1;
	    bool timeout;
	    union futex_val2 val2;
	    void *addr2;
	    union futex_val3 val3;

	    err = vg_futex_send_unmarshal (message,
					   &addr1, &op, &val1,
					   &timeout, &val2,
					   &addr2, &val3, NULL);
	    if (err)
	      REPLY (err);

	    do_debug (4)
	      {
		const char *op_string = "unknown";
		switch (op)
		  {
		  case FUTEX_WAIT:
		    op_string = "wait";
		    break;
		  case FUTEX_WAKE_OP:
		    op_string = "wake op";
		    break;
		  case FUTEX_WAKE:
		    op_string = "wake";
		    break;
		  case FUTEX_CMP_REQUEUE:
		    op_string = "cmp requeue";
		    break;
		  }

		char *mode = "unknown";

		struct vg_object *page = vg_cap_to_object (principal,
							   &thread->utcb);
		if (page && object_type (page) == vg_cap_page)
		  {
		    struct vg_utcb *utcb = (struct vg_utcb *) page;

		    if (utcb->activated_mode)
		      mode = "activated";
		    else
		      mode = "normal";
		  }

		DEBUG (0, "(%p, %s, 0x%x, %d, %p, %d) (thread in %s)",
		       addr1, op_string, val1, val2.value, addr2, val3.value,
		       mode);
	      }

	    switch (op)
	      {
	      case FUTEX_WAIT:
	      case FUTEX_WAKE_OP:
	      case FUTEX_WAKE:
	      case FUTEX_CMP_REQUEUE:
		break;
	      default:
		REPLY (ENOSYS);
	      };

	    vg_addr_t addr = vg_addr_chop (VG_PTR_TO_ADDR (addr1),
					   PAGESIZE_LOG2);
	    struct vg_object *object1 = OBJECT (target_root,
						addr, vg_cap_page, true, NULL);
	    int offset1 = (uintptr_t) addr1 & (PAGESIZE - 1);
	    int *vaddr1 = (void *) object1 + offset1;

	    switch (op)
	      {
	      case FUTEX_WAIT:
		if (*vaddr1 != val1)
		  REPLY (EWOULDBLOCK);

		if (timeout)
		  panic ("Timeouts not yet supported");

		reply->wait_reason = MESSENGER_WAIT_FUTEX;
		reply->wait_reason_arg = offset1;

		object_wait_queue_enqueue (principal, object1, reply);

#ifndef NDEBUG
		futex_waiter_list_enqueue (&futex_waiters, reply);
#endif

		break;

	      case FUTEX_WAKE:
		/* Wake up VAL1 threads or, if there are less than
		   VAL1 blocked threads, wake up all of them.  Return
		   the number of threads woken up.  */

		if (val1 <= 0)
		  REPLY (EINVAL);

		int count = wake (val1, object1, offset1, 0, 0, 0);
		vg_futex_reply (activity, reply, count);
		break;

	      case FUTEX_WAKE_OP:
		addr = vg_addr_chop (VG_PTR_TO_ADDR (addr2), PAGESIZE_LOG2);
		struct vg_object *object2 = OBJECT (target_root,
						    addr, vg_cap_page,
						    true, NULL);
		int offset2 = (uintptr_t) addr2 & (PAGESIZE - 1);
		int *vaddr2 = (void *) object2 + offset2;

		int oldval = * (int *) vaddr2;
		switch (val3.op)
		  {
		  case FUTEX_OP_SET:
		    * (int *) vaddr2 = val3.oparg;
		    break;
		  case FUTEX_OP_ADD:
		    * (int *) vaddr2 = oldval + val3.oparg;
		    break;
		  case FUTEX_OP_OR:
		    * (int *) vaddr2 = oldval + val3.oparg;
		    break;
		  case FUTEX_OP_ANDN:
		    * (int *) vaddr2 = oldval & ~val3.oparg;
		    break;
		  case FUTEX_OP_XOR:
		    * (int *) vaddr2 = oldval ^ val3.oparg;
		    break;
		  }

		count = wake (1, object1, offset1, 0, 0, 0);

		bool comparison;
		switch (val3.cmp)
		  {
		  case FUTEX_OP_CMP_EQ:
		    comparison = oldval == val3.cmparg;
		    break;
		  case FUTEX_OP_CMP_NE:
		    comparison = oldval != val3.cmparg;
		    break;
		  case FUTEX_OP_CMP_LT:
		    comparison = oldval < val3.cmparg;
		    break;
		  case FUTEX_OP_CMP_LE:
		    comparison = oldval <= val3.cmparg;
		    break;
		  case FUTEX_OP_CMP_GT:
		    comparison = oldval > val3.cmparg;
		    break;
		  case FUTEX_OP_CMP_GE:
		    comparison = oldval >= val3.cmparg;
		    break;
		  }

		if (comparison)
		  count += wake (val2.value, object2, offset2, 0, 0, 0);

		vg_futex_reply (activity, reply, 0);
		break;

	      case FUTEX_CMP_REQUEUE:
		/* Wake VAL1 waiters, or if there are less than VAL1
		   waiters, all waiters.  If there waiters remain,
		   requeue VAL2 waiters on ADDR2 or, if there are less
		   than VAL2 remaining waiters.  Returns the total
		   number of woken and requeued waiters.  */

		if (* (int *) vaddr1 != val3.value)
		  REPLY (EAGAIN);

		/* Get the second object.  */
		addr = vg_addr_chop (VG_PTR_TO_ADDR (addr2), PAGESIZE_LOG2);
		object2 = OBJECT (target_root, addr, vg_cap_page, true, NULL);
		offset2 = (uintptr_t) addr2 & (PAGESIZE - 1);

		count = wake (val1, object1, offset1,
			      val2.value, object2, offset2);
		vg_futex_reply (activity, reply, count);
		break;
	      }

	    break;
	  }

	case VG_messenger_id:
	  {
	    if (object_type (target) != vg_cap_messenger || ! target_writable)
	      REPLY (EINVAL);
	    struct messenger *m = (struct messenger *) target;

	    uint64_t id;
	    err = vg_messenger_id_send_unmarshal (message, &id, NULL);
	    if (err)
	      REPLY (EINVAL);

	    uint64_t old = m->id;
	    m->id = id;

	    vg_messenger_id_reply (principal, reply, old);

	    break;
	  }

	default:
	  /* XXX: Don't panic when running production code.  */
	  DEBUG (1, "Didn't handle message from %x.%x with label %d",
		 l4_thread_no (from), l4_version (from), label);
	}

      if ((flags & VG_IPC_RETURN))
	{
	  l4_msg_clear (msg);
	  l4_msg_put_word (msg, 0, 0);
	  l4_msg_set_untyped_words (msg, 1);
	  do_reply = 1;
	}
      
    out:;
    }

  /* Should never return.  */
  panic ("server_loop returned!");
}