summaryrefslogtreecommitdiff
path: root/viengoos/t-as.c
blob: ffb061fd12b793498b9f1c8fcabd7b06085be00f (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
#define _L4_TEST_MAIN
#include "t-environment.h"

#include <hurd/types.h>
#include <hurd/stddef.h>
#include <hurd/as.h>

#include "memory.h"
#include "cap.h"
#include "object.h"
#include "activity.h"

struct activity *root_activity;

/* Current working folio.  */
static struct folio *folio;
static int object;

static struct as_allocate_pt_ret
allocate_object (enum vg_cap_type type, vg_addr_t addr)
{
  if (! folio || object == VG_FOLIO_OBJECTS)
    {
      folio = folio_alloc (root_activity, VG_FOLIO_POLICY_DEFAULT);
      object = 0;
    }

  struct as_allocate_pt_ret rt;
  rt.cap = folio_object_alloc (root_activity, folio, object ++,
			       type, VG_OBJECT_POLICY_DEFAULT, 0);

  /* We don't need to set RT.STORAGE as as_insert doesn't require it
     for the internal interface implementations.  */
  rt.storage = VG_ADDR (0, 0);
  return rt;
}

static struct as_allocate_pt_ret
allocate_page_table (vg_addr_t addr)
{
  return allocate_object (vg_cap_cappage, addr);
}

extern char _start;
extern char _end;

struct alloc
{
  vg_addr_t addr;
  int type;
};

static void
try (struct alloc *allocs, int count, bool dump)
{
  struct vg_cap aspace = { .type = vg_cap_void };
  struct vg_cap caps[count];

  void do_check (struct vg_cap *cap, bool writable, int i, bool present)
    {
      if (present)
	{
	  assert (cap);

	  assert (cap->type == caps[i].type);

	  struct object *object = vg_cap_to_object (root_activity, cap);
	  struct object_desc *odesc = object_to_object_desc (object);
	  if (caps[i].type != vg_cap_void)
	    assert (odesc->oid == caps[i].oid);

	  if (cap->type == vg_cap_page)
	    assert (* (unsigned char *) object == i);
	}
      else
	{
	  if (cap)
	    {
	      struct object *object = vg_cap_to_object (root_activity, cap);
	      assert (! object);
	      /* This assertion relies on the fact that the
		 implementation will clear the type field on a failed
		 lookup.  */
	      assert (cap->type == vg_cap_void);
	    }
	}
    }

  int i;
  for (i = 0; i < count; i ++)
    {
      switch (allocs[i].type)
	{
	case vg_cap_folio:
	  caps[i] = object_to_cap ((struct object *)
				   folio_alloc (root_activity,
						VG_FOLIO_POLICY_DEFAULT));
	  break;
	case vg_cap_void:
	  caps[i].type = vg_cap_void;
	  break;
	case vg_cap_page:
	case vg_cap_rpage:
	case vg_cap_cappage:
	case vg_cap_rcappage:
	  caps[i] = allocate_object (allocs[i].type, allocs[i].addr).cap;
	  break;
	default:
	  assert (! " Bad type");
	}

      struct object *object = vg_cap_to_object (root_activity, &caps[i]);
      if (caps[i].type == vg_cap_page)
	memset (object, i, PAGESIZE);

      as_insert_full (root_activity, VG_ADDR_VOID, &aspace, allocs[i].addr,
		      VG_ADDR_VOID, VG_ADDR_VOID, object_to_cap (object),
		      allocate_page_table);

      if (dump)
	{
	  printf ("After inserting: " VG_ADDR_FMT "\n",
		  VG_ADDR_PRINTF (allocs[i].addr));
	  as_dump_from (root_activity, &aspace, NULL);
	}

      int j;
      for (j = 0; j < count; j ++)
	{
	  struct vg_cap *cap = NULL;
	  bool w;

	  as_slot_lookup_rel_use
	    (root_activity, &aspace, allocs[j].addr,
	     ({
	       cap = slot;
	       w = writable;
	     }));
	  do_check (cap, w, j, j <= i);

	  struct vg_cap c;
	  c = as_object_lookup_rel (root_activity,
				    &aspace, allocs[j].addr, -1,
				    &w);
	  do_check (&c, w, j, j <= i);
	}
    }

  /* Free the allocated objects.  */
  for (i = 0; i < count; i ++)
    {
      /* Make sure allocs[i].addr maps to PAGES[i].  */
      struct vg_cap *cap = NULL;
      bool w;

      as_slot_lookup_rel_use (root_activity, &aspace, allocs[i].addr,
			      ({
				cap = slot;
				w = writable;
			      }));
      do_check (cap, w, i, true);

      struct vg_cap c;
      c = as_object_lookup_rel (root_activity,
				&aspace, allocs[i].addr, -1,
				&w);
      do_check (&c, w, i, true);

      /* Void the capability in the returned capability slot.  */
      as_slot_lookup_rel_use (root_activity, &aspace, allocs[i].addr,
			      ({
				slot->type = vg_cap_void;
			      }));

      /* The page should no longer be found.  */
      c = as_object_lookup_rel (root_activity, &aspace, allocs[i].addr, -1,
				NULL);
      assert (c.type == vg_cap_void);

      /* Restore the capability slot.  */
      as_slot_lookup_rel_use (root_activity, &aspace, allocs[i].addr,
			      ({
				slot->type = allocs[i].type;
			      }));


      /* The page should be back.  */
      cap = NULL;
      as_slot_lookup_rel_use
	(root_activity, &aspace, allocs[i].addr,
	 ({
	   cap = slot;
	   w = writable;
	 }));
      do_check (cap, w, i, true);

      c = as_object_lookup_rel (root_activity,
				&aspace, allocs[i].addr, -1, &w);
      do_check (&c, w, i, true);

      /* Finally, free the object.  */
      switch (caps[i].type)
	{
	case vg_cap_folio:
	  folio_free (root_activity,
		      (struct folio *) vg_cap_to_object (root_activity,
						      &caps[i]));
	  break;
	case vg_cap_void:
	  break;
	default:
	  object_free (root_activity, vg_cap_to_object (root_activity, &caps[i]));
	  break;
	}

      /* Check the state of all pages.  */
      int j;
      for (j = 0; j < count; j ++)
	{
	  /* We should always get the slot (but it won't always
	     designate an object).  */
	  bool ret = as_slot_lookup_rel_use
	    (root_activity, &aspace, allocs[j].addr,
	     ({
	     }));
	  assert (ret);

	  struct vg_cap c;
	  bool writable;
	  c = as_object_lookup_rel (root_activity,
				    &aspace, allocs[j].addr, -1, &writable);
	  do_check (&c, writable, j, i < j);
	}
    }
}

void
test (void)
{
  if (! memory_reserve ((l4_word_t) &_start, (l4_word_t) &_end,
			memory_reservation_self))
    panic ("Failed to reserve memory for self.");

  memory_grab ();
  object_init ();

  /* Create the root activity.  */
  folio = folio_alloc (NULL, VG_FOLIO_POLICY_DEFAULT);
  if (! folio)
    panic ("Failed to allocate storage for the initial task!");

  struct vg_cap c = allocate_object (vg_cap_activity_control, VG_ADDR_VOID).cap;
  root_activity = (struct activity *) vg_cap_to_object (root_activity, &c);
    
  folio_parent (root_activity, folio);

  {
    printf ("Checking slot_lookup_rel... ");

    /* We have an empty address space.  When we use slot_lookup_rel
       and specify that we don't care what type of capability we get,
       we should get the capability slot--if the guard is right.  */
    struct vg_cap aspace = { type: vg_cap_void };

    l4_word_t addr = 0xFA000;
    bool ret = as_slot_lookup_rel_use (root_activity,
				       &aspace, VG_ADDR (addr, VG_ADDR_BITS),
				       ({ }));
    assert (! ret);

    /* Set the root to designate VG_ADDR.  */
    bool r = VG_CAP_SET_GUARD (&aspace, addr, VG_ADDR_BITS);
    assert (r);
    
    ret = as_slot_lookup_rel_use (root_activity,
				  &aspace, VG_ADDR (addr, VG_ADDR_BITS),
				  ({
				    assert (slot == &aspace);
				    assert (writable);
				  }));
    assert (ret);

    printf ("ok.\n");
  }

  printf ("Checking as_insert... ");
  {
    struct alloc allocs[] =
      { { VG_ADDR (1 << (VG_FOLIO_OBJECTS_LOG2 + PAGESIZE_LOG2),
		VG_ADDR_BITS - VG_FOLIO_OBJECTS_LOG2 - PAGESIZE_LOG2), vg_cap_folio },
	{ VG_ADDR (0x100000003, 63), vg_cap_page },
	{ VG_ADDR (0x100000004, 63), vg_cap_page },
	{ VG_ADDR (0x1000 /* 4k.  */, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x00100000 /* 1MB */, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x01000000 /* 16MB */, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x10000000 /* 256MB */, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40000000 /* 1000MB */, VG_ADDR_BITS - PAGESIZE_LOG2),
	  vg_cap_page },
	{ VG_ADDR (0x40000000 - 0x2000 /* 1000MB - 4k */,
		VG_ADDR_BITS - PAGESIZE_LOG2),
	  vg_cap_page },
	{ VG_ADDR (0x40001000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40003000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40002000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40009000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40008000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40007000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x40006000, VG_ADDR_BITS - PAGESIZE_LOG2), vg_cap_page },
	{ VG_ADDR (0x00101000 /* 1MB + 4k.  */, VG_ADDR_BITS - PAGESIZE_LOG2),
	  vg_cap_page },
	{ VG_ADDR (0x00FF0000 /* 1MB - 4k.  */, VG_ADDR_BITS - PAGESIZE_LOG2),
	  vg_cap_page },
      };

    try (allocs, sizeof (allocs) / sizeof (allocs[0]), false);
  }

  {
    struct alloc allocs[] =
      { { VG_ADDR (1, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (2, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (3, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (4, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (5, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (6, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (7, VG_ADDR_BITS), vg_cap_page },
	{ VG_ADDR (8, VG_ADDR_BITS), vg_cap_page }
      };

    try (allocs, sizeof (allocs) / sizeof (allocs[0]), false);
  }

  {
    /* Induce a long different guard.  */
    struct alloc allocs[] =
      { { VG_ADDR (0x100000000, 51), vg_cap_cappage },
	{ VG_ADDR (0x80000, 44), vg_cap_folio }
      };

    try (allocs, sizeof (allocs) / sizeof (allocs[0]), false);
  }

  {
    /* Induce subpage allocation.  */
    struct alloc allocs[] =
      { { VG_ADDR (0x80000, 44), vg_cap_folio },
	{ VG_ADDR (0x1000, 51), vg_cap_page },
	{ VG_ADDR (0x10000, 51), vg_cap_page },
	{ VG_ADDR (0x2000, 51), vg_cap_page }
      };

    try (allocs, sizeof (allocs) / sizeof (allocs[0]), false);
  }

#warning Incorrect failure mode
#if 0
  {
    /* We do our best to not have to rearrange cappages.  However,
       consider the following scenario: we insert a number of adjacent
       cappages starting at 0.5 MB.  This requires cappage immediately
       above them.  Currently, we'd place a cappage at 0/44.  If we
       then try to insert a folio at 0/43, for which there is
       technically space, it will fail as there is no slot.

          0                    <- /43
          [ | | |...| | | ]
                     P P P     <- /51

       We can only insert at 0/44 if we first reduce the size of the
       cappage and introduce a 2 element page, the first slot of which
       would be used to point to the folio and the second to the
       smaller cappage.  */
    struct alloc allocs[] =
      { { VG_ADDR (0x80000, 51), vg_cap_page },
	{ VG_ADDR (0x81000, 51), vg_cap_page },
	{ VG_ADDR (0x82000, 51), vg_cap_page },
	{ VG_ADDR (0x83000, 51), vg_cap_page },
	{ VG_ADDR (0x84000, 51), vg_cap_page },
	{ VG_ADDR (0x85000, 51), vg_cap_page },
	{ VG_ADDR (0x0, 44), vg_cap_folio }
      };

    try (allocs, sizeof (allocs) / sizeof (allocs[0]), false);
  }
#endif

  printf ("ok.\n");
}