1/* @title: Virtual address allocator */
2#pragma once
3#include <math/bit.h>
4#include <mem/page.h>
5#include <mem/vmm.h>
6#include <stdbool.h>
7#include <stddef.h>
8#include <stdint.h>
9#include <types/types.h>
10
11struct address_range;
12struct vas;
13struct vas_arena;
14
15#define VAS_CHUNK_SHIFT 26
16#define VAS_CHUNK_SIZE BIT(VAS_CHUNK_SHIFT)
17
18/* [base, limit) */
19struct vas *vas_bootstrap(vaddr_t base, vaddr_t limit);
20struct vas *vas_create(vaddr_t base, vaddr_t limit);
21struct vas *vas_from(struct address_range *ar);
22struct vas *vas_bootstrap_from(struct address_range *ar);
23
24/* No touching in interrupts or above DISPATCH */
25vaddr_t vas_alloc(struct vas *vas, size_t size, size_t align);
26
27/* Giving the wrong size panics. TODO: we only need to pass in addr,
28 * we can update the APIs for that later on */
29void vas_free(struct vas *vas, vaddr_t addr, size_t size);
30
31void *vas_map_full(struct vas *vas, paddr_t paddr, size_t len,
32 struct vmm_request rq);
33#define vas_map(vas, paddr, len, ...) \
34 vas_map_full((vas), (paddr), (len), vmm_request_with_defaults(__VA_ARGS__))
35void vas_unmap(struct vas *vas, void *vaddr, size_t len);
36
37/* Drain cached reservations */
38void vas_reclaim(struct vas *vas);
39void vas_reclaim_freelist_pages(struct vas_arena *arena);
40
41/* TODO: virtual address spaces are NOT refcounted */
42bool vas_destroy(struct vas *vas);
43void vas_space_dump(struct vas *vas);
44
45bool vas_vaddr_in_vas(struct vas *vas, vaddr_t addr);
46/* Includes interior byte addresses */
47bool vas_vaddr_is_allocated(struct vas *vas, vaddr_t addr);
48