|  | /* | 
|  | * Copyright 2017, Data61 | 
|  | * Commonwealth Scientific and Industrial Research Organisation (CSIRO) | 
|  | * ABN 41 687 119 230. | 
|  | * | 
|  | * This software may be distributed and modified according to the terms of | 
|  | * the BSD 2-Clause license. Note that NO WARRANTY is provided. | 
|  | * See "LICENSE_BSD2.txt" for details. | 
|  | * | 
|  | * @TAG(DATA61_BSD) | 
|  | */ | 
|  |  | 
|  | /** | 
|  | * @file allocman.h | 
|  | * | 
|  | * @brief The allocman is system for resolving dependencies between allocators for different resources | 
|  | * | 
|  | * Allocations need to go via the allocation manager in order to ensure | 
|  | * the correct watermark levels of resources are maintained. While an | 
|  | * individual manager is free to directly give away resources, if it | 
|  | * calls into the allocation manager then that manager may be recursively | 
|  | * invoked. Performing allocations from the allocation manager is also | 
|  | * the only way to allocate the final watermark resources when memory | 
|  | * becomes exhausted. | 
|  | * | 
|  | * It is generally desirable that a free operation does not have any | 
|  | * allocation calls in it. If an allocator does wish to allocate a | 
|  | * resource when performing a free it must accept that its allocation | 
|  | * function could be called as a result. In a similar manner if your | 
|  | * allocation function frees resources your free function may be recursively | 
|  | * called. | 
|  | * | 
|  | * There are (generally) two different types of allocators. Those that are | 
|  | * linked to an allocation manager, and those that are not. Typically the | 
|  | * only sort of manager you would not want linked to an allocation manager | 
|  | * is a cspace manager (if you are managing a clients cspace). Although you | 
|  | * could also create an untyped manager if you do not want to give clients | 
|  | * untypeds directly, but still want to have a fixed untyped pool reserved | 
|  | * for it. | 
|  | * | 
|  | * Possibility exists for much foot shooting with any allocators. A typical | 
|  | * desire might be to create a sub allocator (such as a cspace manager), | 
|  | * use an already existing allocation manager to back all of its allocations, | 
|  | * and then destroy that cspace manager at some point to release all its resources. | 
|  | * There are no guarantees that this will work. If all requests to the sub allocator | 
|  | * use the same allocation manager to perform book keeping requests, and the | 
|  | * sub allocator is told to free using that same allocation manager then all | 
|  | * should work. But this is strictly up to using your allocators correctly, | 
|  | * and knowing how they work. | 
|  | */ | 
|  |  | 
|  | #pragma once | 
|  |  | 
|  | #include <assert.h> | 
|  | #include <autoconf.h> | 
|  | #include <sel4/types.h> | 
|  | #include <allocman/util.h> | 
|  | #include <allocman/cspace/cspace.h> | 
|  | #include <allocman/mspace/mspace.h> | 
|  | #include <allocman/utspace/utspace.h> | 
|  | #include <vka/cspacepath_t.h> | 
|  | #include <sel4platsupport/timer.h> | 
|  |  | 
|  | /** | 
|  | * Describes a reservation chunk for the memory system. | 
|  | * Used by {@link #allocman_configure_mspace_reserve} | 
|  | */ | 
|  | struct allocman_mspace_chunk { | 
|  | size_t size; | 
|  | size_t count; | 
|  | }; | 
|  |  | 
|  | /** | 
|  | * Describes a reservation chunk for the untyped system. | 
|  | * Used by {@link #allocman_configure_utspace_reserve} | 
|  | */ | 
|  | struct allocman_utspace_chunk { | 
|  | size_t size_bits; | 
|  | seL4_Word type; | 
|  | size_t count; | 
|  | }; | 
|  |  | 
|  | /** | 
|  | * Internal data structure for describing an untyped allocation in | 
|  | * the reservation system | 
|  | */ | 
|  | struct allocman_utspace_allocation { | 
|  | seL4_Word cookie; | 
|  | cspacepath_t slot; | 
|  | }; | 
|  |  | 
|  | struct allocman_freed_mspace_chunk { | 
|  | void *ptr; | 
|  | size_t size; | 
|  | }; | 
|  |  | 
|  | struct allocman_freed_utspace_chunk { | 
|  | size_t size_bits; | 
|  | seL4_Word cookie; | 
|  | }; | 
|  |  | 
|  | /** | 
|  | * The allocman itself. This is generally the only type you will need to pass around | 
|  | * to deal with allocation. It is declared in full here so that the compiler is able | 
|  | * to calculate its size so it can be allocated on stacks/globals etc as required | 
|  | */ | 
|  | typedef struct allocman { | 
|  | /* link to our underlying allocators. some are lazily added. the mspace will always be here, | 
|  | * and have_mspace can be used to check if the allocman is initialized at all */ | 
|  | int have_mspace; | 
|  | struct mspace_interface mspace; | 
|  | int have_cspace; | 
|  | struct cspace_interface cspace; | 
|  | int have_utspace; | 
|  | struct utspace_interface utspace; | 
|  |  | 
|  | /* Flag that tracks whether any alloc/free/other function has been entered yet */ | 
|  | int in_operation; | 
|  |  | 
|  | /* Counts that track re-entry into each specific alloc/free function */ | 
|  | size_t cspace_alloc_depth; | 
|  | size_t cspace_free_depth; | 
|  | size_t utspace_alloc_depth; | 
|  | size_t utspace_free_depth; | 
|  | size_t mspace_alloc_depth; | 
|  | size_t mspace_free_depth; | 
|  |  | 
|  | /* Track whether the watermark is currently refilled so we don't recursively do it */ | 
|  | int refilling_watermark; | 
|  | /* Has a watermark resource been used. This is just an optimization */ | 
|  | int used_watermark; | 
|  |  | 
|  | /* track resources that we have not yet been able to free due to circular dependencies */ | 
|  | size_t desired_freed_slots; | 
|  | size_t num_freed_slots; | 
|  | cspacepath_t *freed_slots; | 
|  |  | 
|  | size_t desired_freed_mspace_chunks; | 
|  | size_t num_freed_mspace_chunks; | 
|  | struct allocman_freed_mspace_chunk *freed_mspace_chunks; | 
|  |  | 
|  | size_t desired_freed_utspace_chunks; | 
|  | size_t num_freed_utspace_chunks; | 
|  | struct allocman_freed_utspace_chunk *freed_utspace_chunks; | 
|  |  | 
|  | /* cspace watermark resources */ | 
|  | size_t desired_cspace_slots; | 
|  | size_t num_cspace_slots; | 
|  | cspacepath_t *cspace_slots; | 
|  |  | 
|  | /* mspace watermark resources */ | 
|  | size_t num_mspace_chunks; | 
|  | struct allocman_mspace_chunk *mspace_chunk; | 
|  | size_t *mspace_chunk_count; | 
|  | void ***mspace_chunks; | 
|  |  | 
|  | /* utspace watermark resources */ | 
|  | size_t num_utspace_chunks; | 
|  | struct allocman_utspace_chunk *utspace_chunk; | 
|  | size_t *utspace_chunk_count; | 
|  | struct allocman_utspace_allocation **utspace_chunks; | 
|  | } allocman_t; | 
|  |  | 
|  | /** | 
|  | * Allocates 'real' memory from the allocator | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param bytes Size in bytes to allocate | 
|  | * @param _error (Optional) set to 0 on success | 
|  | * | 
|  | * @return returns pointer to allocated memory | 
|  | */ | 
|  | void *allocman_mspace_alloc(allocman_t *alloc, size_t bytes, int *_error); | 
|  |  | 
|  | /** | 
|  | * Frees 'real' memory, as previously allocated by {@link #allocman_mspace_alloc} | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param ptr Allocated memory (as returned by {@link #allocman_mspace_alloc} | 
|  | * @param bytes Size in bytes of the allocation to free. Allocations cannot be partially freed | 
|  | */ | 
|  | void allocman_mspace_free(allocman_t *alloc, void *ptr, size_t bytes); | 
|  |  | 
|  | /** | 
|  | * Allocates a cslot from the allocator | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param slot Stores details of the allocated slot | 
|  | * | 
|  | * @return returns 0 on sucess | 
|  | */ | 
|  | int allocman_cspace_alloc(allocman_t *alloc, cspacepath_t *slot); | 
|  |  | 
|  | /** | 
|  | * Frees a cslot from the allocator, as previously allocated by {@link #allocman_cspace_alloc}. | 
|  | * To avoid the need to keep cspacepath_t's laying around, it is guaruanteed that | 
|  | * (*slot) == allocman_cspace_make_path(alloc, slot->capPtr). So if needed you can simply store | 
|  | * the capPtr and reconstruct the path before calling free. | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param slot The slot to free. | 
|  | * | 
|  | * @return returns 0 on sucess | 
|  | */ | 
|  | void allocman_cspace_free(allocman_t *alloc, const cspacepath_t *slot); | 
|  |  | 
|  | /** | 
|  | * Converts a seL4_CPtr into a cspacepath_t using the cspace attached to the allocman. | 
|  | * If the slot is not valid in that cspace then the return path is completely undefined. | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param slot The slot to convert | 
|  | * | 
|  | * @return cspacepath_t of the given slot | 
|  | */ | 
|  | static inline cspacepath_t allocman_cspace_make_path(allocman_t *alloc, seL4_CPtr slot) { | 
|  | assert(alloc->have_cspace); | 
|  | return alloc->cspace.make_path(alloc->cspace.cspace, slot); | 
|  | } | 
|  |  | 
|  | /** | 
|  | * Allocates a portion of untyped memory, and retypes it into the desired object for you. | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param size_bits The size in bits of the memory that will be required to store this object. | 
|  | This is different to seL4_Untyped_Retype for allocating seL4_CapTableObjects | 
|  | * @param type The seL4 type of the object being allocated | 
|  | * @param path A path to a location to put the allocated object (this must be a valid empty slot) | 
|  | * @param paddr The desired physical address of the start of this object. A value of '1' indicates do not care | 
|  | *              as '1' can never be a valid object base address | 
|  | * @param canBeDev Whether this allocation can be satisified from a device region, provided that | 
|  | *  region is known to be actual RAM. Objects from device regions are not initialized (i.e. not zeroed) | 
|  | * @param _error (Optional) set to 0 on success | 
|  | * | 
|  | * @return Returns a cookie that can be used in future to free this allocation | 
|  | */ | 
|  | seL4_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); | 
|  |  | 
|  | /** | 
|  | * Allocates a portion of untyped memory, and retypes it into the desired object for you. | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param size_bits The size in bits of the memory that will be required to store this object. | 
|  | This is different to seL4_Untyped_Retype for allocating seL4_CapTableObjects | 
|  | * @param type The seL4 type of the object being allocated | 
|  | * @param path A path to a location to put the allocated object (this must be a valid empty slot) | 
|  | * @param canBeDev Whether this allocation can be satisified from a device region, provided that | 
|  | *  region is known to be actual RAM. Objects from device regions are not initialized (i.e. not zeroed) | 
|  | * @param _error (Optional) set to 0 on success | 
|  | * | 
|  | * @return Returns a cookie that can be used in future to free this allocation | 
|  | */ | 
|  | static inline | 
|  | seL4_Word allocman_utspace_alloc(allocman_t *alloc, size_t size_bits, seL4_Word type, const cspacepath_t *path, bool canBeDev, int *_error) | 
|  | { | 
|  | return allocman_utspace_alloc_at(alloc, size_bits, type, path, ALLOCMAN_NO_PADDR, canBeDev, _error); | 
|  | } | 
|  |  | 
|  | /** | 
|  | * Returns a portion of untyped memory back to the allocator. It is assumed that this | 
|  | * memory is now unused, and every capability to this memory has been deleted (including | 
|  | * the one created by {@link allocman_utspace_alloc} | 
|  | * | 
|  | * @param alloc Allocman to allocate from | 
|  | * @param size_bits The size in bits of the memory that was required to store this object. | 
|  | This is different to seL4_Untyped_Retype for seL4_CapTableObjects | 
|  | * @param cookie The cookie representing this allocation (as returned by {@link allocman_utspace_alloc} | 
|  | */ | 
|  | void allocman_utspace_free(allocman_t *alloc, seL4_Word cookie, size_t size_bits); | 
|  |  | 
|  | /** | 
|  | * Initialize a new allocman. all it requires is a memory allocator, everything will be boot strapped from it | 
|  | * | 
|  | * @param alloc Allocman structure to initialize | 
|  | * @param mspace Memory allocator. This will be permanently linked to this allocator and must keep existing | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_create(allocman_t *alloc, struct mspace_interface mspace); | 
|  |  | 
|  | /** | 
|  | * Attempts to fill the reserves of the allocator. This can be used if the underlying allocators have been modified, | 
|  | * for instance by having resources added, or as a way to query the health of the allocman | 
|  | * | 
|  | * @param alloc The allocman to fill reserves of | 
|  | * | 
|  | * @return returns 0 if reserves are full | 
|  | */ | 
|  | int allocman_fill_reserves(allocman_t *alloc); | 
|  |  | 
|  | /** | 
|  | * Attach an untyped allocator to an allocman. | 
|  | * | 
|  | * @param alloc The allocman to attach to | 
|  | * @param utspace untyped allocator to attach. This wil lbe permanently linked to this allocator and must keep existing | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_attach_utspace(allocman_t *alloc, struct utspace_interface utspace); | 
|  |  | 
|  | /** | 
|  | * Attach a cspace manager to an allocman. | 
|  | * | 
|  | * @param alloc The allocman to attach to | 
|  | * @param cspace The cspace manager to attach. This wil lbe permanently linked to this allocator and must keep existing | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_attach_cspace(allocman_t *alloc, struct cspace_interface cspace); | 
|  |  | 
|  | /** | 
|  | * Configure the memory reserve for the allocator | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param chunk Description of the memory reserve | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_mspace_reserve(allocman_t *alloc, struct allocman_mspace_chunk chunk); | 
|  |  | 
|  | /** | 
|  | * Configure the untyped reserve for the allocator | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param chunk Description of the untyped reserve | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_utspace_reserve(allocman_t *alloc, struct allocman_utspace_chunk chunk); | 
|  |  | 
|  | /** | 
|  | * Configure the cspace reserve for the allocator | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param num Number of cslots to hold in reserve | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_cspace_reserve(allocman_t *alloc, size_t num); | 
|  |  | 
|  | /** | 
|  | * Configure the maximul number of freed cptrs we can store. This is required for | 
|  | * scenarios where an allocator cannot handle a recursive call, but we would like to not | 
|  | * leak memory | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param num Maximum number of slots to handle | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_max_freed_slots(allocman_t *alloc, size_t num); | 
|  |  | 
|  | /** | 
|  | * Configure the maximul number of freed memory objects we can store. This is required for | 
|  | * scenarios where an allocator cannot handle a recursive call, but we would like to not | 
|  | * leak memory | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param num Maxmimum number of chunks to handle | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_max_freed_memory_chunks(allocman_t *alloc, size_t num); | 
|  |  | 
|  | /** | 
|  | * Configure the maximul number of freed untyped objects we can store. This is required for | 
|  | * scenarios where an allocator cannot handle a recursive call, but we would like to not | 
|  | * leak memory | 
|  | * | 
|  | * @param alloc The allocman to configure | 
|  | * @param num Maxmimum number of chunks to handle | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | int allocman_configure_max_freed_untyped_chunks(allocman_t *alloc, size_t num); | 
|  |  | 
|  | /** | 
|  | * Add additional untyped objects to the underlying untyped manager. This allows additional | 
|  | * resources to be injected after the allocman has started | 
|  | * | 
|  | * @param alloc The allocman to add to | 
|  | * @param num Number of untypeds to add | 
|  | * @param uts Path to each of the untyped to add. untyped is assumed to be at depth 32 from this threads cspace_root | 
|  | * @param size_bits Size, in bits, of each of the untypeds | 
|  | * @param paddr Optional parameter specifying the physical address of each of the untypeds | 
|  | * @param utType The type of all untypeds being added. One of (ALLOCMAN_UT_KERNEL, ALLOCMAN_UT_DEV, ALLOCMAN_UT_DEV_MEM) | 
|  | * | 
|  | * @return returns 0 on success | 
|  | */ | 
|  | static 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) { | 
|  | int error; | 
|  | assert(alloc->have_utspace); | 
|  | error = alloc->utspace.add_uts(alloc, alloc->utspace.utspace, num, uts, size_bits, paddr, utType); | 
|  | if (error) { | 
|  | return error; | 
|  | } | 
|  | allocman_fill_reserves(alloc); | 
|  | return 0; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * Retrieves the physical address for an allocated untyped object | 
|  | * | 
|  | * @param alloc The allocman to query | 
|  | * @param cookie Cookie to the allocated untyped object | 
|  | * @param size_bits Size of the allocated untyped object | 
|  | * | 
|  | * @return Physical address of the object | 
|  | */ | 
|  | static inline uintptr_t allocman_utspace_paddr(allocman_t *alloc, seL4_Word cookie, size_t size_bits) { | 
|  | assert(alloc->have_utspace); | 
|  | return alloc->utspace.paddr(alloc->utspace.utspace, cookie, size_bits); | 
|  | } | 
|  |  | 
|  | /** | 
|  | * Helper function for adding device untypeds from platform specific timer objects | 
|  | * | 
|  | * @param alloc The allocman to query | 
|  | * @param to struct containing untyped metadata | 
|  | * | 
|  | * @return 0 on success, otherwise error. | 
|  | */ | 
|  | int allocman_add_untypeds_from_timer_objects(allocman_t *alloc, timer_objects_t *to); | 
|  |  |