1/**
2 * \file
3 * \brief Memory manager header
4 */
5
6/*
7 * Copyright (c) 2008, 2011, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef BARRELFISH_MM_H
16#define BARRELFISH_MM_H
17
18#include <sys/cdefs.h>
19
20#include <barrelfish/slab.h>
21#include <mm/slot_alloc.h>
22
23__BEGIN_DECLS
24
25enum nodetype {
26    NodeType_Dummy,     ///< The whole region doesn't exist, but it has children
27    NodeType_Chunked,   ///< This region exists and has been split up
28    NodeType_Free,      ///< This leaf region exists and is free
29    NodeType_Allocated  ///< This leaf region exists and is allocated
30};
31
32/// Node in Memory manager Btree. Private.
33// Only appears here so we can know its size
34struct mmnode {
35    enum nodetype type;     ///< Type of this node
36    uint8_t childbits;      ///< Number of children (in bits / power of two)
37    struct capref cap;    ///< Cap to this region (invalid for Dummy regions)
38    struct mmnode *children[0];///< Child node pointers
39};
40
41/// Macro to statically determine size of a node, given the maxchildbits
42#define MM_NODE_SIZE(maxchildbits) \
43    (sizeof(struct mmnode) + sizeof(struct mmnode *) * (1UL << (maxchildbits)))
44
45/**
46 * \brief Memory manager instance data
47 *
48 * This should be opaque from the perspective of the client, but to allow
49 * them to allocate its memory, we declare it in the public header.
50 */
51struct mm {
52    struct slab_allocator slabs; ///< Slab allocator used for allocating nodes
53    slot_alloc_t slot_alloc;     ///< Slot allocator for allocating cspace
54    slot_refill_t slot_refill;   ///< Function to refill slot allocator
55    void *slot_alloc_inst;       ///< Opaque instance pointer for slot allocator
56    struct mmnode *root;         ///< Root node
57    genpaddr_t base;             ///< Base address of root node
58    enum objtype objtype;        ///< Type of capabilities stored
59    uint8_t sizebits;            ///< Size of root node (in bits)
60    uint8_t maxchildbits;        ///< Maximum number of children of every node (in bits)
61    bool delete_chunked;         ///< Delete chunked capabilities if true
62};
63
64void mm_debug_print(struct mmnode *mmnode, int space);
65errval_t mm_init(struct mm *mm, enum objtype objtype, genpaddr_t base,
66                 uint8_t sizebits, uint8_t maxchildbits,
67                 slab_refill_func_t slab_refill_func,
68                 slot_alloc_t slot_alloc_func, slot_refill_t slot_refill_func,
69                 void *slot_alloc_inst, bool delete_chunked);
70void mm_destroy(struct mm *mm);
71errval_t mm_add(struct mm *mm, struct capref cap, uint8_t sizebits,
72                genpaddr_t base);
73errval_t mm_add_multi(struct mm *mm, struct capref cap, gensize_t size,
74                      genpaddr_t base);
75errval_t mm_alloc(struct mm *mm, uint8_t sizebits, struct capref *retcap,
76                  genpaddr_t *retbase);
77errval_t mm_alloc_range(struct mm *mm, uint8_t sizebits, genpaddr_t minbase,
78                        genpaddr_t maxlimit, struct capref *retcap,
79                        genpaddr_t *retbase);
80errval_t mm_realloc_range(struct mm *mm, uint8_t sizebits, genpaddr_t base,
81                          struct capref *retcap);
82errval_t mm_free(struct mm *mm, struct capref cap, genpaddr_t base,
83                 uint8_t sizebits);
84
85/// Structure to record all information about a given memory region
86struct mem_cap {
87    struct capref cap;        ///< Cap for this region
88    uint8_t sizebits;           ///< Size of region in bits
89    genpaddr_t base;               ///< Physical base address of region
90};
91
92size_t mm_relinquish_all(struct mm *mm, struct mem_cap *ret, size_t retlen);
93size_t mm_relinquish_range(struct mm *mm, genpaddr_t base, genpaddr_t limit,
94                           struct mem_cap *ret, size_t retlen);
95
96__END_DECLS
97
98#endif /* BARRELFISH_MM_H */
99