1#include <mem/anon_vma.h>
2#include <mem/avc.h>
3#include <mem/fixed_size_alloc.h>
4#include <mem/pmm.h>
5#include <mem/vma_range.h>
6#include <smp/perdomain.h>
7
8FIXED_SIZE_RANGE_PERDOMAIN_DECLARE(
9 avc, .obj_size = sizeof(struct anon_vma_chain),
10 .obj_align = _Alignof(struct anon_vma_chain));
11
12struct anon_vma_chain *avc_alloc() {
13 return FSR_PERDOMAIN_ALLOC(avc);
14}
15
16void avc_free(struct anon_vma_chain *f) {
17 return FSR_PERDOMAIN_FREE(avc, f);
18}
19
20void avc_link(struct vma_range *vma_range, struct anon_vma *av,
21 struct anon_vma_chain *avc) {
22 avc->anon_vma = av;
23 avc->vma_range = vma_range;
24 /* Key the interval-tree node in OBJECT space. rbit_insert() initializes
25 * every other field of the node, so the interval is all we owe it. Setting
26 * it *after* rbit_init_node() (which zeroes the interval) was the bug. */
27 avc->itnode.interval.low = avc_first(a: avc);
28 avc->itnode.interval.high = avc_last(a: avc);
29 anon_vma_write_lock(av);
30
31 list_add_tail(new: &avc->same_vma_range, head: &vma_range->anon_vma_chain);
32 rbit_insert(tree: &av->tree, new_node: &avc->itnode);
33 anon_vma_unlock(av);
34}
35
36void avc_unlink(struct anon_vma_chain *avc) {
37 struct anon_vma *av = avc->anon_vma;
38 anon_vma_write_lock(av);
39 list_del(entry: &avc->same_vma_range);
40 rbit_delete(tree: &av->tree, z: &avc->itnode);
41 anon_vma_unlock(av);
42}
43
44void avc_rekey(struct anon_vma_chain *avc) {
45 struct anon_vma *av = avc->anon_vma;
46 anon_vma_write_lock(av);
47 rbit_delete(tree: &av->tree, z: &avc->itnode);
48 avc->itnode.interval.low = avc_first(a: avc);
49 avc->itnode.interval.high = avc_last(a: avc);
50 rbit_insert(tree: &av->tree, new_node: &avc->itnode);
51 anon_vma_unlock(av);
52}
53