1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <autoconf.h>
16#include <stdlib.h>
17#include <sel4/types.h>
18#include <allocman/cspace/cspace.h>
19
20struct cspace_single_level_config {
21    /* A cptr to the cnode that we are managing slots in */
22    seL4_CPtr cnode;
23    /* Size in bits of the cnode */
24    size_t cnode_size_bits;
25    /* Guard depth added to this cspace. */
26    size_t cnode_guard_bits;
27    /* First valid slot (as an index) */
28    size_t first_slot;
29    /* Last valid slot + 1 (as an index) */
30    size_t end_slot;
31};
32
33typedef struct cspace_single_level {
34    struct cspace_single_level_config config;
35    size_t *bitmap;
36    size_t bitmap_length;
37    size_t last_entry;
38} cspace_single_level_t;
39
40int cspace_single_level_create(struct allocman *alloc, cspace_single_level_t *cspace, struct cspace_single_level_config config);
41
42/* Frees any allocated resources back to the given allocation manager */
43void cspace_single_level_destroy(struct allocman *alloc, cspace_single_level_t *cspace);
44
45int _cspace_single_level_alloc(struct allocman *alloc, void *_cspace, cspacepath_t *slot);
46int _cspace_single_level_alloc_at(struct allocman *alloc, void *_cspace, seL4_CPtr slot);
47void _cspace_single_level_free(struct allocman *alloc, void *_cspace, const cspacepath_t *slot);
48
49static inline cspacepath_t _cspace_single_level_make_path(void *_cspace, seL4_CPtr slot)
50{
51    cspace_single_level_t *cspace = (cspace_single_level_t*) _cspace;
52    return (cspacepath_t) {
53        .root = cspace->config.cnode,
54        .capPtr = slot,
55        .capDepth = cspace->config.cnode_size_bits + cspace->config.cnode_guard_bits,
56        .dest = 0,
57        .destDepth = 0,
58        .offset = slot,
59        .window = 1
60    };
61}
62
63static inline cspace_interface_t cspace_single_level_make_interface(cspace_single_level_t *cspace) {
64    return (cspace_interface_t) {
65        .alloc = _cspace_single_level_alloc,
66        .free = _cspace_single_level_free,
67        .make_path = _cspace_single_level_make_path,
68        /* We do not want to handle recursion, as it shouldn't happen */
69        .properties = ALLOCMAN_DEFAULT_PROPERTIES,
70        .cspace = cspace
71    };
72}
73
74