aboutsummaryrefslogtreecommitdiff
path: root/include/apos/vmem.h
blob: eb901a849d1d1a7524d714aaf921175feb30e6df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

#ifndef APOS_VMEM_H
#define APOS_VMEM_H

/**
 * @file vmem.h
 * Virtual memory handling.
 */

#include <apos/tcb.h>
#include <apos/mem.h>
#include <apos/pmem.h>
#include <apos/sp_tree.h>

/**
 * Allocate user virtual memory.
 *
 * Virtual memory start address is chosen according to best fit with regard to
 * size.
 *
 * @param r Process to allocate memory in.
 * @param size Minimum size of allocation.
 * @param flags Flags of allocation.
 * @return Start of allocation when succesful, \c NULL otherwise.
 */
vm_t alloc_uvmem(struct tcb *r, size_t size, vmflags_t flags);

/**
 * Allocate fixed user virtual memory.
 *
 * Virtual memory start address is chosen so that \c start is within the
 * allocation and the allocation after \c start is at least \c size bytes large.
 * It is unspecified how many bytes are between the start of the allocation and
 * \c start.
 *
 * @param r Process to allocate memory in.
 * @param start Address that should be in allocation.
 * @param size Minimum size of allocation.
 * @param flags Flags of allocation.
 * @return Start of allocation when succesful, \c NULL otherwise.
 */
vm_t alloc_fixed_uvmem(struct tcb *r, vm_t start, size_t size, vmflags_t flags);

/**
 * Allocate shared user virtual memory.
 *
 * Only callable by servers, who are the owners of the shared region.
 *
 * @param r Process to allocate memory in.
 * @param size Minimum size of allocation.
 * @param flags Flags of allocation.
 * @return Start of allocation when succesful, \c NULL otherwise.
 */
vm_t alloc_shared_uvmem(struct tcb *r, size_t size, vmflags_t flags);

/**
 * Reference shared user virtual memory.
 *
 * Only callable by clients.
 *
 * @param r1 Process in which shared memory resides.
 * @param r2 Process to reference shared memory in.
 * @param va Virtual address of shared memory in \c r1.
 * @param flags Flags of reference in \c r2.
 * @return Start of reference in \c r2 when succesful, \c NULL otherwise.
 */
vm_t ref_shared_uvmem(struct tcb *r1, struct tcb *r2, vm_t va, vmflags_t flags);

/**
 * Free all user virtual memory allocations not marked with \ref MR_KEEP.
 *
 * @param r Process in which to clear user virtual memory.
 * @return \ref OK.
 */
stat_t clear_uvmem(struct tcb *r);

/**
 * Free all user virtual memory allocations, even if marked with \ref MR_KEEP.
 *
 * @param r Process in which to clear user virtual memory.
 * @return \ref OK.
 */
stat_t purge_uvmem(struct tcb *r);

/**
 * Free one user virtual memory allocation.
 *
 * @param r Process in which to clear user virtual memory.
 * @param va Start of user virtual memory allocation to free.
 * @return \ref OK.
 */
stat_t free_uvmem(struct tcb *r, vm_t va);

/**
 * Initialize user virtual memory instance.
 *
 * This assumes the user virtual memory is contiguous, with no holes between \c
 * base and \c top.
 *
 * @param r Process in which to initialize user virtual memory.
 * @param base Start of user virtual memory.
 * @param top Top of user virtual memory.
 * @return \see init_region().
 */
stat_t init_uvmem(struct tcb *r, vm_t base, vm_t top);

/**
 * Destroy user virtual memory instance.
 *
 * @param r Process in which to destroy user virtual memory.
 * @return \see destroy_region().
 */
stat_t destroy_uvmem(struct tcb *r);

/**
 * User virtual memory worker callback for \ref map_fill_region().
 *
 * \c data is a pointer to \ref stat_t, which is set to \ref INFO_SEFF if all
 * threads in process should sync their memory mappings. This occurs when the
 * top level page table is modified.
 *
 * @param b Virtual memory to work in.
 * @param offset Hint for \ref alloc_page().
 * @param vaddr Current virtual address.
 * @param flags Flags of region.
 * @param order Suggested page order.
 * @param data Pointer to \ref stat_t.
 * @return \c OK when suggested order if acceptable, \c INFO_TRGN if suggested
 * order not acceptable. Error otherwise.
 * again
 */
stat_t alloc_uvmem_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr,
                           vmflags_t flags, enum mm_order order, void *data);

/**
 * Shared user virtual memory worker callback for \ref map_fill_region().
 *
 * @param b Virtual memory to work in.
 * @param offset Hint for \ref alloc_page().
 * @param vaddr Current virtual address.
 * @param flags Flags of region.
 * @param order Suggested page order.
 * @param data Pointer to \ref stat_t.
 * @return \see alloc_uvmem_wrapper().
 *
 * \see alloc_uvmem_wrapper().
 */
stat_t alloc_shared_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr,
                            vmflags_t flags, enum mm_order order, void *data);

/**
 * User virtual memory copying worker callback for \ref map_fill_region().
 *
 * Currently unused, but intention is to set up copy of some other virtual
 * memory region, likely passed through \c data?
 *
 * @param b Virtual memory to work in.
 * @param offset Hint for \ref alloc_page().
 * @param vaddr Current virtual address.
 * @param flags Flags of region.
 * @param order Suggested page order.
 * @param data Pointer to \ref vmem to clone from.
 * @return \see alloc_uvmem_wrapper().
 *
 * \see alloc_uvmem_wraper().
 * \todo Implement.
 */
stat_t copy_allocd_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr,
                           vmflags_t flags, enum mm_order order, void *data);

/**
 * User virtual memory freeing worker callback for \ref map_fill_region().
 *
 * @param b Virtual memory to work in.
 * @param offset Hint for \ref alloc_page().
 * @param vaddr Current virtual address.
 * @param flags Flags of region.
 * @param order Suggested page order.
 * @param data Pointer to \ref stat_t.
 * @return \see alloc_uvmem_wrapper().
 *
 * \see alloc_uvmem_wrapper().
 */
stat_t free_uvmem_wrapper(struct vmem *b, pm_t *offset, vm_t vaddr,
                          vmflags_t flags, enum mm_order order, void *data);

/**
 * Convenience wrapper for \ref map_fill_region() when mapping an allocated
 * region.
 *
 * @param b Virtual memory to work in.
 * @param start Start of virtual memory region to map.
 * @param bytes Size of virtual memory region.
 * @param flags Flags of virtual memory region.
 * @param data Pointer to \c stat_t.
 * @return \see map_fill_region().
 */
#define map_allocd_region(b, start, bytes, flags, data) \
	map_fill_region(b, &alloc_uvmem_wrapper, 0, start, bytes, flags, data)

/**
 * Convenience wrapper for \ref map_fill_region() when mapping a shared region.
 *
 * @param b Virtual memory to work in.
 * @param start Start of virtual memory region to map.
 * @param bytes Size of virtual memory region.
 * @param flags Flags of virtual memory region.
 * @param data Pointer to \c stat_t.
 * @return \see map_fill_region().
 */
#define map_shared_region(b, start, bytes, flags, data) \
	map_fill_region(b, &alloc_shared_wrapper, 0, start, bytes, flags, data)

/**
 * Convenience wrapper for \ref map_fill_region() when copying a region.
 *
 * @param b Virtual memory to work in.
 * @param start Start of virtual memory region to map.
 * @param bytes Size of virtual memory region.
 * @param flags Flags of virtual memory region.
 * @param data Pointer to \c vmem to clone.
 * @return \see map_fill_region().
 */
#define copy_allocd_region(b, start, bytes, flags, data) \
	map_fill_region(b, &copy_allocd_wrapper, 0, start, bytes, flags, data)

/**
 * Convenience wrapper for \ref map_fill_region() when freeing region.
 *
 * @param b Virtual memory to work in.
 * @param start Start of virtual memory region to unmap.
 * @param bytes Size of virtual memory region.
 * @param flags Flags of virtual memory region. Technically unused?
 * @param data Pointer to \c stat_t.
 * @return \see map_fill_region().
 */
#define unmap_freed_region(b, start, bytes, flags, data) \
	map_fill_region(b, &free_uvmem_wrapper, 0, start, bytes, flags, data)

/**
 * Extract virtual memory flags (MR_XXX).
 *
 * @param x Flags to extract virtual memory region flags from.
 * @return Virtual memory region flags.
 */
#define vm_flags(x) ((x) & ~0xff)

/**
 * Extract physical memory page flags (VM_XXX).
 *
 * @param x Flags to extract physical memory page flags from.
 * @return Physical memory page flags.
 */
#define vp_flags(x) ((x) & 0xff)

#endif /* APOS_VMEM_H */