blob: 09ffa34bc6ba15ffc02b6481a526d75da659e6c7 [file] [log] [blame]
TrustworthySystems1621d7b2014-07-22 14:12:00 +10001/*
Anna Lyons92143412017-06-05 08:29:55 +10002 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
TrustworthySystems1621d7b2014-07-22 14:12:00 +10005 *
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 *
Anna Lyons92143412017-06-05 08:29:55 +100010 * @TAG(DATA61_BSD)
TrustworthySystems1621d7b2014-07-22 14:12:00 +100011 */
12
13/**
14 * @file allocman.h
15 *
16 * @brief The allocman is system for resolving dependencies between allocators for different resources
17 *
18 * Allocations need to go via the allocation manager in order to ensure
19 * the correct watermark levels of resources are maintained. While an
20 * individual manager is free to directly give away resources, if it
21 * calls into the allocation manager then that manager may be recursively
22 * invoked. Performing allocations from the allocation manager is also
23 * the only way to allocate the final watermark resources when memory
24 * becomes exhausted.
25 *
26 * It is generally desirable that a free operation does not have any
27 * allocation calls in it. If an allocator does wish to allocate a
28 * resource when performing a free it must accept that its allocation
29 * function could be called as a result. In a similar manner if your
30 * allocation function frees resources your free function may be recursively
31 * called.
32 *
33 * There are (generally) two different types of allocators. Those that are
34 * linked to an allocation manager, and those that are not. Typically the
35 * only sort of manager you would not want linked to an allocation manager
36 * is a cspace manager (if you are managing a clients cspace). Although you
37 * could also create an untyped manager if you do not want to give clients
38 * untypeds directly, but still want to have a fixed untyped pool reserved
39 * for it.
40 *
41 * Possibility exists for much foot shooting with any allocators. A typical
42 * desire might be to create a sub allocator (such as a cspace manager),
43 * use an already existing allocation manager to back all of its allocations,
44 * and then destroy that cspace manager at some point to release all its resources.
45 * There are no guarantees that this will work. If all requests to the sub allocator
46 * use the same allocation manager to perform book keeping requests, and the
47 * sub allocator is told to free using that same allocation manager then all
48 * should work. But this is strictly up to using your allocators correctly,
49 * and knowing how they work.
50 */
51
Anna Lyons135c6512017-09-28 12:16:03 +100052#pragma once
TrustworthySystems1621d7b2014-07-22 14:12:00 +100053
54#include <assert.h>
55#include <autoconf.h>
56#include <sel4/types.h>
57#include <allocman/util.h>
58#include <allocman/cspace/cspace.h>
59#include <allocman/mspace/mspace.h>
60#include <allocman/utspace/utspace.h>
61#include <vka/cspacepath_t.h>
Kent McLeod8630e522017-07-28 15:10:12 +100062#include <sel4platsupport/timer.h>
TrustworthySystems1621d7b2014-07-22 14:12:00 +100063
64/**
65 * Describes a reservation chunk for the memory system.
66 * Used by {@link #allocman_configure_mspace_reserve}
67 */
68struct allocman_mspace_chunk {
Adrian Danis14ab04c2015-11-13 16:35:23 +110069 size_t size;
70 size_t count;
TrustworthySystems1621d7b2014-07-22 14:12:00 +100071};
72
73/**
74 * Describes a reservation chunk for the untyped system.
75 * Used by {@link #allocman_configure_utspace_reserve}
76 */
77struct allocman_utspace_chunk {
Adrian Danis14ab04c2015-11-13 16:35:23 +110078 size_t size_bits;
TrustworthySystems1621d7b2014-07-22 14:12:00 +100079 seL4_Word type;
Adrian Danis14ab04c2015-11-13 16:35:23 +110080 size_t count;
TrustworthySystems1621d7b2014-07-22 14:12:00 +100081};
82
83/**
84 * Internal data structure for describing an untyped allocation in
85 * the reservation system
86 */
87struct allocman_utspace_allocation {
Adrian Danis14ab04c2015-11-13 16:35:23 +110088 seL4_Word cookie;
TrustworthySystems1621d7b2014-07-22 14:12:00 +100089 cspacepath_t slot;
90};
91
92struct allocman_freed_mspace_chunk {
93 void *ptr;
Adrian Danis14ab04c2015-11-13 16:35:23 +110094 size_t size;
TrustworthySystems1621d7b2014-07-22 14:12:00 +100095};
96
97struct allocman_freed_utspace_chunk {
Adrian Danis14ab04c2015-11-13 16:35:23 +110098 size_t size_bits;
99 seL4_Word cookie;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000100};
101
102/**
103 * The allocman itself. This is generally the only type you will need to pass around
104 * to deal with allocation. It is declared in full here so that the compiler is able
105 * to calculate its size so it can be allocated on stacks/globals etc as required
106 */
107typedef struct allocman {
108 /* link to our underlying allocators. some are lazily added. the mspace will always be here,
109 * and have_mspace can be used to check if the allocman is initialized at all */
110 int have_mspace;
111 struct mspace_interface mspace;
112 int have_cspace;
113 struct cspace_interface cspace;
114 int have_utspace;
115 struct utspace_interface utspace;
116
117 /* Flag that tracks whether any alloc/free/other function has been entered yet */
118 int in_operation;
119
120 /* Counts that track re-entry into each specific alloc/free function */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100121 size_t cspace_alloc_depth;
122 size_t cspace_free_depth;
123 size_t utspace_alloc_depth;
124 size_t utspace_free_depth;
125 size_t mspace_alloc_depth;
126 size_t mspace_free_depth;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000127
128 /* Track whether the watermark is currently refilled so we don't recursively do it */
129 int refilling_watermark;
130 /* Has a watermark resource been used. This is just an optimization */
131 int used_watermark;
132
133 /* track resources that we have not yet been able to free due to circular dependencies */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100134 size_t desired_freed_slots;
135 size_t num_freed_slots;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000136 cspacepath_t *freed_slots;
137
Adrian Danis14ab04c2015-11-13 16:35:23 +1100138 size_t desired_freed_mspace_chunks;
139 size_t num_freed_mspace_chunks;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000140 struct allocman_freed_mspace_chunk *freed_mspace_chunks;
141
Adrian Danis14ab04c2015-11-13 16:35:23 +1100142 size_t desired_freed_utspace_chunks;
143 size_t num_freed_utspace_chunks;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000144 struct allocman_freed_utspace_chunk *freed_utspace_chunks;
145
146 /* cspace watermark resources */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100147 size_t desired_cspace_slots;
148 size_t num_cspace_slots;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000149 cspacepath_t *cspace_slots;
150
151 /* mspace watermark resources */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100152 size_t num_mspace_chunks;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000153 struct allocman_mspace_chunk *mspace_chunk;
Adrian Danis14ab04c2015-11-13 16:35:23 +1100154 size_t *mspace_chunk_count;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000155 void ***mspace_chunks;
156
157 /* utspace watermark resources */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100158 size_t num_utspace_chunks;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000159 struct allocman_utspace_chunk *utspace_chunk;
Adrian Danis14ab04c2015-11-13 16:35:23 +1100160 size_t *utspace_chunk_count;
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000161 struct allocman_utspace_allocation **utspace_chunks;
162} allocman_t;
163
164/**
165 * Allocates 'real' memory from the allocator
166 *
167 * @param alloc Allocman to allocate from
168 * @param bytes Size in bytes to allocate
169 * @param _error (Optional) set to 0 on success
170 *
171 * @return returns pointer to allocated memory
172 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100173void *allocman_mspace_alloc(allocman_t *alloc, size_t bytes, int *_error);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000174
175/**
176 * Frees 'real' memory, as previously allocated by {@link #allocman_mspace_alloc}
177 *
178 * @param alloc Allocman to allocate from
179 * @param ptr Allocated memory (as returned by {@link #allocman_mspace_alloc}
180 * @param bytes Size in bytes of the allocation to free. Allocations cannot be partially freed
181 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100182void allocman_mspace_free(allocman_t *alloc, void *ptr, size_t bytes);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000183
184/**
185 * Allocates a cslot from the allocator
186 *
187 * @param alloc Allocman to allocate from
188 * @param slot Stores details of the allocated slot
189 *
190 * @return returns 0 on sucess
191 */
192int allocman_cspace_alloc(allocman_t *alloc, cspacepath_t *slot);
193
194/**
195 * Frees a cslot from the allocator, as previously allocated by {@link #allocman_cspace_alloc}.
196 * To avoid the need to keep cspacepath_t's laying around, it is guaruanteed that
197 * (*slot) == allocman_cspace_make_path(alloc, slot->capPtr). So if needed you can simply store
198 * the capPtr and reconstruct the path before calling free.
199 *
200 * @param alloc Allocman to allocate from
201 * @param slot The slot to free.
202 *
203 * @return returns 0 on sucess
204 */
Adrian Danisbd03ee52015-04-21 15:17:01 +1000205void allocman_cspace_free(allocman_t *alloc, const cspacepath_t *slot);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000206
207/**
208 * Converts a seL4_CPtr into a cspacepath_t using the cspace attached to the allocman.
209 * If the slot is not valid in that cspace then the return path is completely undefined.
210 *
211 * @param alloc Allocman to allocate from
212 * @param slot The slot to convert
213 *
214 * @return cspacepath_t of the given slot
215 */
216static inline cspacepath_t allocman_cspace_make_path(allocman_t *alloc, seL4_CPtr slot) {
217 assert(alloc->have_cspace);
218 return alloc->cspace.make_path(alloc->cspace.cspace, slot);
219}
220
221/**
222 * Allocates a portion of untyped memory, and retypes it into the desired object for you.
223 *
224 * @param alloc Allocman to allocate from
225 * @param size_bits The size in bits of the memory that will be required to store this object.
226 This is different to seL4_Untyped_Retype for allocating seL4_CapTableObjects
227 * @param type The seL4 type of the object being allocated
228 * @param path A path to a location to put the allocated object (this must be a valid empty slot)
Adrian Danis902a0962016-02-19 13:54:40 +1100229 * @param paddr The desired physical address of the start of this object. A value of '1' indicates do not care
230 * as '1' can never be a valid object base address
231 * @param canBeDev Whether this allocation can be satisified from a device region, provided that
232 * region is known to be actual RAM. Objects from device regions are not initialized (i.e. not zeroed)
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000233 * @param _error (Optional) set to 0 on success
234 *
235 * @return Returns a cookie that can be used in future to free this allocation
236 */
Adrian Danis902a0962016-02-19 13:54:40 +1100237seL4_Word allocman_utspace_alloc_at(allocman_t *alloc, size_t size_bits, seL4_Word type, const cspacepath_t *path, uintptr_t paddr, bool canBeDev, int *_error);
238
239/**
240 * Allocates a portion of untyped memory, and retypes it into the desired object for you.
241 *
242 * @param alloc Allocman to allocate from
243 * @param size_bits The size in bits of the memory that will be required to store this object.
244 This is different to seL4_Untyped_Retype for allocating seL4_CapTableObjects
245 * @param type The seL4 type of the object being allocated
246 * @param path A path to a location to put the allocated object (this must be a valid empty slot)
247 * @param canBeDev Whether this allocation can be satisified from a device region, provided that
248 * region is known to be actual RAM. Objects from device regions are not initialized (i.e. not zeroed)
249 * @param _error (Optional) set to 0 on success
250 *
251 * @return Returns a cookie that can be used in future to free this allocation
252 */
253static inline
254seL4_Word allocman_utspace_alloc(allocman_t *alloc, size_t size_bits, seL4_Word type, const cspacepath_t *path, bool canBeDev, int *_error)
255{
256 return allocman_utspace_alloc_at(alloc, size_bits, type, path, ALLOCMAN_NO_PADDR, canBeDev, _error);
257}
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000258
259/**
260 * Returns a portion of untyped memory back to the allocator. It is assumed that this
261 * memory is now unused, and every capability to this memory has been deleted (including
262 * the one created by {@link allocman_utspace_alloc}
263 *
264 * @param alloc Allocman to allocate from
265 * @param size_bits The size in bits of the memory that was required to store this object.
266 This is different to seL4_Untyped_Retype for seL4_CapTableObjects
267 * @param cookie The cookie representing this allocation (as returned by {@link allocman_utspace_alloc}
268 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100269void allocman_utspace_free(allocman_t *alloc, seL4_Word cookie, size_t size_bits);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000270
271/**
272 * Initialize a new allocman. all it requires is a memory allocator, everything will be boot strapped from it
273 *
274 * @param alloc Allocman structure to initialize
275 * @param mspace Memory allocator. This will be permanently linked to this allocator and must keep existing
276 *
277 * @return returns 0 on success
278 */
279int allocman_create(allocman_t *alloc, struct mspace_interface mspace);
280
281/**
282 * Attempts to fill the reserves of the allocator. This can be used if the underlying allocators have been modified,
283 * for instance by having resources added, or as a way to query the health of the allocman
284 *
285 * @param alloc The allocman to fill reserves of
286 *
287 * @return returns 0 if reserves are full
288 */
289int allocman_fill_reserves(allocman_t *alloc);
290
291/**
292 * Attach an untyped allocator to an allocman.
293 *
294 * @param alloc The allocman to attach to
295 * @param utspace untyped allocator to attach. This wil lbe permanently linked to this allocator and must keep existing
296 *
297 * @return returns 0 on success
298 */
299int allocman_attach_utspace(allocman_t *alloc, struct utspace_interface utspace);
300
301/**
302 * Attach a cspace manager to an allocman.
303 *
304 * @param alloc The allocman to attach to
305 * @param cspace The cspace manager to attach. This wil lbe permanently linked to this allocator and must keep existing
306 *
307 * @return returns 0 on success
308 */
309int allocman_attach_cspace(allocman_t *alloc, struct cspace_interface cspace);
310
311/**
312 * Configure the memory reserve for the allocator
313 *
314 * @param alloc The allocman to configure
315 * @param chunk Description of the memory reserve
316 *
317 * @return returns 0 on success
318 */
319int allocman_configure_mspace_reserve(allocman_t *alloc, struct allocman_mspace_chunk chunk);
320
321/**
322 * Configure the untyped reserve for the allocator
323 *
324 * @param alloc The allocman to configure
325 * @param chunk Description of the untyped reserve
326 *
327 * @return returns 0 on success
328 */
329int allocman_configure_utspace_reserve(allocman_t *alloc, struct allocman_utspace_chunk chunk);
330
331/**
332 * Configure the cspace reserve for the allocator
333 *
334 * @param alloc The allocman to configure
335 * @param num Number of cslots to hold in reserve
336 *
337 * @return returns 0 on success
338 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100339int allocman_configure_cspace_reserve(allocman_t *alloc, size_t num);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000340
341/**
342 * Configure the maximul number of freed cptrs we can store. This is required for
343 * scenarios where an allocator cannot handle a recursive call, but we would like to not
344 * leak memory
345 *
346 * @param alloc The allocman to configure
347 * @param num Maximum number of slots to handle
348 *
349 * @return returns 0 on success
350 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100351int allocman_configure_max_freed_slots(allocman_t *alloc, size_t num);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000352
353/**
354 * Configure the maximul number of freed memory objects we can store. This is required for
355 * scenarios where an allocator cannot handle a recursive call, but we would like to not
356 * leak memory
357 *
358 * @param alloc The allocman to configure
359 * @param num Maxmimum number of chunks to handle
360 *
361 * @return returns 0 on success
362 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100363int allocman_configure_max_freed_memory_chunks(allocman_t *alloc, size_t num);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000364
365/**
366 * Configure the maximul number of freed untyped objects we can store. This is required for
367 * scenarios where an allocator cannot handle a recursive call, but we would like to not
368 * leak memory
369 *
370 * @param alloc The allocman to configure
371 * @param num Maxmimum number of chunks to handle
372 *
373 * @return returns 0 on success
374 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100375int allocman_configure_max_freed_untyped_chunks(allocman_t *alloc, size_t num);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000376
377/**
378 * Add additional untyped objects to the underlying untyped manager. This allows additional
379 * resources to be injected after the allocman has started
380 *
381 * @param alloc The allocman to add to
382 * @param num Number of untypeds to add
383 * @param uts Path to each of the untyped to add. untyped is assumed to be at depth 32 from this threads cspace_root
384 * @param size_bits Size, in bits, of each of the untypeds
385 * @param paddr Optional parameter specifying the physical address of each of the untypeds
Adrian Danisde9459a2016-10-05 11:36:29 +1100386 * @param utType The type of all untypeds being added. One of (ALLOCMAN_UT_KERNEL, ALLOCMAN_UT_DEV, ALLOCMAN_UT_DEV_MEM)
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000387 *
388 * @return returns 0 on success
389 */
Adrian Danis902a0962016-02-19 13:54:40 +1100390static inline int allocman_utspace_add_uts(allocman_t *alloc, size_t num, const cspacepath_t *uts, size_t *size_bits, uintptr_t *paddr, int utType) {
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000391 int error;
392 assert(alloc->have_utspace);
Adrian Danis902a0962016-02-19 13:54:40 +1100393 error = alloc->utspace.add_uts(alloc, alloc->utspace.utspace, num, uts, size_bits, paddr, utType);
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000394 if (error) {
395 return error;
396 }
397 allocman_fill_reserves(alloc);
398 return 0;
399}
400
401/**
402 * Retrieves the physical address for an allocated untyped object
403 *
404 * @param alloc The allocman to query
405 * @param cookie Cookie to the allocated untyped object
406 * @param size_bits Size of the allocated untyped object
407 *
408 * @return Physical address of the object
409 */
Adrian Danis14ab04c2015-11-13 16:35:23 +1100410static inline uintptr_t allocman_utspace_paddr(allocman_t *alloc, seL4_Word cookie, size_t size_bits) {
TrustworthySystems1621d7b2014-07-22 14:12:00 +1000411 assert(alloc->have_utspace);
412 return alloc->utspace.paddr(alloc->utspace.utspace, cookie, size_bits);
413}
414
Kent McLeod8630e522017-07-28 15:10:12 +1000415/**
416 * Helper function for adding device untypeds from platform specific timer objects
417 *
418 * @param alloc The allocman to query
419 * @param to struct containing untyped metadata
420 *
421 * @return 0 on success, otherwise error.
422 */
423int allocman_add_untypeds_from_timer_objects(allocman_t *alloc, timer_objects_t *to);
424