about summary refs log tree commit diff
path: root/sysdeps/aarch64/morello/libc-cap.h
blob: 84c20e9df8f843cf11216f2f044fa9015b0ee9bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Support for allocations with narrow capability bounds.  Morello version.
   Copyright (C) 2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

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

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#ifndef _AARCH64_MORELLO_LIBC_CAP_H
#define _AARCH64_MORELLO_LIBC_CAP_H 1

#include <stdint.h>
#include <sys/mman.h>
#include <libc-lock.h>

/* Hash table for __libc_cap_widen.  */

#define HT_MIN_LEN (65536 / sizeof (struct htentry))
#define HT_MAX_LEN (1UL << 58)

struct htentry
{
  void *key;
  void *value;
};

struct ht
{
  __libc_lock_define(,mutex);
  size_t mask;    /* Length - 1, note: length is powerof2.  */
  size_t fill;    /* Used + deleted entries.  */
  size_t used;
  size_t reserve; /* Planned adds.  */
  struct htentry *tab;
};

static inline bool
htentry_isempty (struct htentry *e)
{
  return (uint64_t) e->key == 0;
}

static inline bool
htentry_isdeleted (struct htentry *e)
{
  return (uint64_t) e->key == -1;
}

static inline bool
htentry_isused (struct htentry *e)
{
  return !htentry_isempty (e) && !htentry_isdeleted (e);
}

static inline uint64_t
ht_key_hash (uint64_t key)
{
  return (key >> 4) ^ (key >> 18);
}

static struct htentry *
ht_tab_alloc (size_t n)
{
  size_t size = n * sizeof (struct htentry);
  assert (size && (size & 65535) == 0);
  void *p = __mmap (0, size, PROT_READ|PROT_WRITE,
		    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  if (p == MAP_FAILED)
    return NULL;
  return p;
}

static void
ht_tab_free (struct htentry *tab, size_t n)
{
  int r = __munmap (tab, n * sizeof (struct htentry));
  assert (r == 0);
}

static bool
ht_init (struct ht *ht)
{
  __libc_lock_init (ht->mutex);
  ht->mask = HT_MIN_LEN - 1;
  ht->fill = 0;
  ht->used = 0;
  ht->reserve = 0;
  ht->tab = ht_tab_alloc (ht->mask + 1);
  return ht->tab != NULL;
}

static struct htentry *
ht_lookup (struct ht *ht, uint64_t key, uint64_t hash)
{
  size_t mask = ht->mask;
  size_t i = hash;
  size_t j;
  struct htentry *e = ht->tab + (i & mask);
  struct htentry *del;

  if (e->key == key || htentry_isempty (e))
    return e;
  if (htentry_isdeleted (e))
    del = e;
  else
    del = NULL;

  /* Quadratic probing.  */
  for (j =1, i += j++; ; i += j++)
    {
      e = ht->tab + (i & mask);
      if (e->key == key)
	return e;
      if (htentry_isempty (e))
	return del != NULL ? del : e;
      if (del == NULL && htentry_isdeleted (e))
	del = e;
    }
}

static bool
ht_resize (struct ht *ht)
{
  size_t len;
  size_t used = ht->used;
  size_t n = ht->used + ht->reserve;
  size_t oldlen = ht->mask + 1;

  if (2 * n >= HT_MAX_LEN)
    len = HT_MAX_LEN;
  else
    for (len = HT_MIN_LEN; len < 2 * n; len *= 2);
  struct htentry *newtab = ht_tab_alloc (len);
  struct htentry *oldtab = ht->tab;
  struct htentry *e;
  if (newtab == NULL)
    return false;

  ht->tab = newtab;
  ht->mask = len - 1;
  ht->fill = ht->used;
  for (e = oldtab; used > 0; e++)
    {
      if (htentry_isused (e))
	{
	  uint64_t k = (uint64_t) e->key;
	  uint64_t hash = ht_key_hash (k);
	  used--;
	  *ht_lookup (ht, k, hash) = *e;
	}
    }
  ht_tab_free (oldtab, oldlen);
  return true;
}

static bool
ht_reserve (struct ht *ht)
{
  bool r = true;
  __libc_lock_lock (ht->mutex);
  ht->reserve++;
  size_t future_fill = ht->fill + ht->reserve;
  size_t future_used = ht->used + ht->reserve;
  /* Resize at 3/4 fill or if there are many deleted entries.  */
  if (future_fill > ht->mask - ht->mask / 4
      || future_fill > 2 * future_used + ht->mask / 4)
    r = ht_resize (ht);
  if (!r)
    ht->reserve--;
  __libc_lock_unlock (ht->mutex);
  return r;
}

static void
ht_unreserve (struct ht *ht)
{
  __libc_lock_lock (ht->mutex);
  assert (ht->reserve > 0);
  ht->reserve--;
  __libc_lock_unlock (ht->mutex);
}

static bool
ht_add (struct ht *ht, void *key, void *value)
{
  uint64_t k = (uint64_t) key;
  uint64_t hash = ht_key_hash (k);
  assert (k != 0 && k != -1);

  __libc_lock_lock (ht->mutex);
  assert (ht->reserve > 0);
  ht->reserve--;
  struct htentry *e = ht_lookup (ht, k, hash);
  bool r = false;
  if (!htentry_isused (e))
    {
      if (htentry_isempty (e))
        ht->fill++;
      ht->used++;
      r = true;
    }
  e->key = key;
  e->value = value;
  __libc_lock_unlock (ht->mutex);
  return r;
}

static bool
ht_del (struct ht *ht, void *key)
{
  uint64_t k = (uint64_t) key;
  uint64_t hash = ht_key_hash (k);
  assert (k != 0 && k != -1);

  __libc_lock_lock (ht->mutex);
  struct htentry *e = ht_lookup (ht, k, hash);
  bool r = htentry_isused (e);
  if (r)
    {
      r = __builtin_cheri_equal_exact(e->key, key);
      ht->used--;
      e->key = (void *) -1;
      e->value = NULL;
    }
  __libc_lock_unlock (ht->mutex);
  return r;
}

static void *
ht_get (struct ht *ht, void *key)
{
  uint64_t k = (uint64_t) key;
  uint64_t hash = ht_key_hash (k);
  assert (k != 0 && k != -1);

  __libc_lock_lock (ht->mutex);
  struct htentry *e = ht_lookup (ht, k, hash);
  void *v = __builtin_cheri_equal_exact(e->key, key) ? e->value : NULL;
  __libc_lock_unlock (ht->mutex);
  return v;
}

/* Capability narrowing APIs.  */

static struct ht __libc_cap_ht;

static __always_inline bool
__libc_cap_init (void)
{
  return ht_init (&__libc_cap_ht);
}

static __always_inline void
__libc_cap_fork_lock (void)
{
  __libc_lock_lock (__libc_cap_ht.mutex);
}

static __always_inline void
__libc_cap_fork_unlock_parent (void)
{
  __libc_lock_unlock (__libc_cap_ht.mutex);
}

static __always_inline void
__libc_cap_fork_unlock_child (void)
{
  __libc_lock_init (__libc_cap_ht.mutex);
}

static __always_inline bool
__libc_cap_map_add (void *p)
{
  assert (p != NULL);
// TODO: depends on pcuabi
//  assert (__builtin_cheri_base_get (p) == (uint64_t) p);
  return true;
}

static __always_inline void
__libc_cap_map_del (void *p)
{
  assert (p != NULL);
//  assert (__builtin_cheri_base_get (p) == (uint64_t) p);
}

/* No special alignment is needed for n <= __CAP_ALIGN_THRESHOLD
   allocations, i.e. __libc_cap_align (n) <= MALLOC_ALIGNMENT.  */
#define __CAP_ALIGN_THRESHOLD 32759

/* Set the mmap_threshold to this value when narrowing is enabled
   to avoid heap fragmentation due to alignment requirements.  */
#define __CAP_MMAP_THRESHOLD 262144

/* Round up the allocation size so the allocated pointer bounds
   can be represented.  Note: this may be called before any
   checks on n, so it should work with all possible n values.  */
static __always_inline size_t
__libc_cap_roundup (size_t n)
{
  if (__glibc_unlikely (n > PTRDIFF_MAX))
    return n;
  return __builtin_cheri_round_representable_length (n);
}

/* Returns the alignment requirement for an allocation size n such
   that the allocated pointer bounds can be represented.  Note:
   same n should be used in __libc_cap_roundup and __libc_cap_align
   and the results used together for __libc_cap_narrow.  */
static __always_inline size_t
__libc_cap_align (size_t n)
{
  return -__builtin_cheri_representable_alignment_mask (n);
}

/* Narrow the bounds of p to be [p, p+n).  Note: the original bounds
   of p includes the entire mapping where p points to and that bound
   should be recoverable by __libc_cap_widen.  Called with p aligned
   and n sized according to __libc_cap_align and __libc_cap_roundup.  */
static __always_inline void *
__libc_cap_narrow (void *p, size_t n)
{
  void *narrow = __builtin_cheri_bounds_set_exact (p, n);
  assert (__builtin_cheri_tag_get (narrow));
  assert (ht_add (&__libc_cap_ht, narrow, p));
  return narrow;
}

/* Given a p with narrowed bound (output of __libc_cap_narrow) return
   the same pointer with the internal wide bound.  */
static __always_inline void *
__libc_cap_widen (void *p)
{
  void *cap = ht_get (&__libc_cap_ht, p);
  assert (cap == p);
  return cap;
}

static __always_inline bool
__libc_cap_reserve (void)
{
  return ht_reserve (&__libc_cap_ht);
}

static __always_inline void
__libc_cap_unreserve (void)
{
  ht_unreserve (&__libc_cap_ht);
}

static __always_inline void
__libc_cap_drop (void *p)
{
  assert (ht_del (&__libc_cap_ht, p));
}

static __always_inline void
__libc_cap_put_back (void *p, void *narrow)
{
  assert (ht_add (&__libc_cap_ht, narrow, p));
}

#endif