1 | /*
|
---|
2 | * Copyright (c) 2001-2005 Jakub Jermar
|
---|
3 | * Copyright (c) 2005 Sergey Bondari
|
---|
4 | * Copyright (c) 2009 Martin Decky
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup genericmm
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @file
|
---|
37 | * @brief Physical frame allocator.
|
---|
38 | *
|
---|
39 | * This file contains the physical frame allocator and memory zone management.
|
---|
40 | * The frame allocator is built on top of the two-level bitmap structure.
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include <typedefs.h>
|
---|
45 | #include <mm/frame.h>
|
---|
46 | #include <mm/reserve.h>
|
---|
47 | #include <mm/as.h>
|
---|
48 | #include <panic.h>
|
---|
49 | #include <debug.h>
|
---|
50 | #include <adt/list.h>
|
---|
51 | #include <synch/mutex.h>
|
---|
52 | #include <synch/condvar.h>
|
---|
53 | #include <arch/asm.h>
|
---|
54 | #include <arch.h>
|
---|
55 | #include <print.h>
|
---|
56 | #include <align.h>
|
---|
57 | #include <mm/slab.h>
|
---|
58 | #include <bitops.h>
|
---|
59 | #include <macros.h>
|
---|
60 | #include <config.h>
|
---|
61 | #include <str.h>
|
---|
62 |
|
---|
63 | #define BITMAP_BLOCK_SIZE 1024
|
---|
64 |
|
---|
65 | zones_t zones;
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Synchronization primitives used to sleep when there is no memory
|
---|
69 | * available.
|
---|
70 | */
|
---|
71 | static mutex_t mem_avail_mtx;
|
---|
72 | static condvar_t mem_avail_cv;
|
---|
73 | static size_t mem_avail_req = 0; /**< Number of frames requested. */
|
---|
74 | static size_t mem_avail_gen = 0; /**< Generation counter. */
|
---|
75 |
|
---|
76 | /********************/
|
---|
77 | /* Helper functions */
|
---|
78 | /********************/
|
---|
79 |
|
---|
80 | NO_TRACE static inline size_t frame_index(zone_t *zone, frame_t *frame)
|
---|
81 | {
|
---|
82 | return (size_t) (frame - zone->frames);
|
---|
83 | }
|
---|
84 |
|
---|
85 | NO_TRACE static inline size_t frame_index_abs(zone_t *zone, frame_t *frame)
|
---|
86 | {
|
---|
87 | return (size_t) (frame - zone->frames) + zone->base;
|
---|
88 | }
|
---|
89 |
|
---|
90 | NO_TRACE static inline bool frame_index_valid(zone_t *zone, size_t index)
|
---|
91 | {
|
---|
92 | return (index < zone->count);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Initialize frame structure.
|
---|
96 | *
|
---|
97 | * @param frame Frame structure to be initialized.
|
---|
98 | *
|
---|
99 | */
|
---|
100 | NO_TRACE static void frame_initialize(frame_t *frame)
|
---|
101 | {
|
---|
102 | frame->refcount = 0;
|
---|
103 | frame->parent = NULL;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*******************/
|
---|
107 | /* Zones functions */
|
---|
108 | /*******************/
|
---|
109 |
|
---|
110 | /** Insert-sort zone into zones list.
|
---|
111 | *
|
---|
112 | * Assume interrupts are disabled and zones lock is
|
---|
113 | * locked.
|
---|
114 | *
|
---|
115 | * @param base Base frame of the newly inserted zone.
|
---|
116 | * @param count Number of frames of the newly inserted zone.
|
---|
117 | *
|
---|
118 | * @return Zone number on success, -1 on error.
|
---|
119 | *
|
---|
120 | */
|
---|
121 | NO_TRACE static size_t zones_insert_zone(pfn_t base, size_t count,
|
---|
122 | zone_flags_t flags)
|
---|
123 | {
|
---|
124 | if (zones.count + 1 == ZONES_MAX) {
|
---|
125 | printf("Maximum zone count %u exceeded!\n", ZONES_MAX);
|
---|
126 | return (size_t) -1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | size_t i;
|
---|
130 | for (i = 0; i < zones.count; i++) {
|
---|
131 | /* Check for overlap */
|
---|
132 | if (overlaps(zones.info[i].base, zones.info[i].count,
|
---|
133 | base, count)) {
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * If the overlaping zones are of the same type
|
---|
137 | * and the new zone is completely within the previous
|
---|
138 | * one, then quietly ignore the new zone.
|
---|
139 | *
|
---|
140 | */
|
---|
141 |
|
---|
142 | if ((zones.info[i].flags != flags) ||
|
---|
143 | (!iswithin(zones.info[i].base, zones.info[i].count,
|
---|
144 | base, count))) {
|
---|
145 | printf("Zone (%p, %p) overlaps "
|
---|
146 | "with previous zone (%p %p)!\n",
|
---|
147 | (void *) PFN2ADDR(base), (void *) PFN2ADDR(count),
|
---|
148 | (void *) PFN2ADDR(zones.info[i].base),
|
---|
149 | (void *) PFN2ADDR(zones.info[i].count));
|
---|
150 | }
|
---|
151 |
|
---|
152 | return (size_t) -1;
|
---|
153 | }
|
---|
154 | if (base < zones.info[i].base)
|
---|
155 | break;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* Move other zones up */
|
---|
159 | for (size_t j = zones.count; j > i; j--)
|
---|
160 | zones.info[j] = zones.info[j - 1];
|
---|
161 |
|
---|
162 | zones.count++;
|
---|
163 |
|
---|
164 | return i;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Get total available frames.
|
---|
168 | *
|
---|
169 | * Assume interrupts are disabled and zones lock is
|
---|
170 | * locked.
|
---|
171 | *
|
---|
172 | * @return Total number of available frames.
|
---|
173 | *
|
---|
174 | */
|
---|
175 | NO_TRACE static size_t frame_total_free_get_internal(void)
|
---|
176 | {
|
---|
177 | size_t total = 0;
|
---|
178 | size_t i;
|
---|
179 |
|
---|
180 | for (i = 0; i < zones.count; i++)
|
---|
181 | total += zones.info[i].free_count;
|
---|
182 |
|
---|
183 | return total;
|
---|
184 | }
|
---|
185 |
|
---|
186 | NO_TRACE size_t frame_total_free_get(void)
|
---|
187 | {
|
---|
188 | size_t total;
|
---|
189 |
|
---|
190 | irq_spinlock_lock(&zones.lock, true);
|
---|
191 | total = frame_total_free_get_internal();
|
---|
192 | irq_spinlock_unlock(&zones.lock, true);
|
---|
193 |
|
---|
194 | return total;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | /** Find a zone with a given frames.
|
---|
199 | *
|
---|
200 | * Assume interrupts are disabled and zones lock is
|
---|
201 | * locked.
|
---|
202 | *
|
---|
203 | * @param frame Frame number contained in zone.
|
---|
204 | * @param count Number of frames to look for.
|
---|
205 | * @param hint Used as zone hint.
|
---|
206 | *
|
---|
207 | * @return Zone index or -1 if not found.
|
---|
208 | *
|
---|
209 | */
|
---|
210 | NO_TRACE size_t find_zone(pfn_t frame, size_t count, size_t hint)
|
---|
211 | {
|
---|
212 | if (hint >= zones.count)
|
---|
213 | hint = 0;
|
---|
214 |
|
---|
215 | size_t i = hint;
|
---|
216 | do {
|
---|
217 | if ((zones.info[i].base <= frame)
|
---|
218 | && (zones.info[i].base + zones.info[i].count >= frame + count))
|
---|
219 | return i;
|
---|
220 |
|
---|
221 | i++;
|
---|
222 | if (i >= zones.count)
|
---|
223 | i = 0;
|
---|
224 |
|
---|
225 | } while (i != hint);
|
---|
226 |
|
---|
227 | return (size_t) -1;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** @return True if zone can allocate specified number of frames */
|
---|
231 | NO_TRACE static bool zone_can_alloc(zone_t *zone, size_t count,
|
---|
232 | pfn_t constraint)
|
---|
233 | {
|
---|
234 | /*
|
---|
235 | * The function bitmap_allocate_range() does not modify
|
---|
236 | * the bitmap if the last argument is NULL.
|
---|
237 | */
|
---|
238 | return ((zone->flags & ZONE_AVAILABLE) &&
|
---|
239 | bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
---|
240 | constraint, NULL));
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Find a zone that can allocate specified number of frames
|
---|
244 | *
|
---|
245 | * Assume interrupts are disabled and zones lock is
|
---|
246 | * locked.
|
---|
247 | *
|
---|
248 | * @param count Number of free frames we are trying to find.
|
---|
249 | * @param flags Required flags of the target zone.
|
---|
250 | * @param constraint Indication of bits that cannot be set in the
|
---|
251 | * physical frame number of the first allocated frame.
|
---|
252 | * @param hind Preferred zone.
|
---|
253 | *
|
---|
254 | */
|
---|
255 | NO_TRACE static size_t find_free_zone(size_t count, zone_flags_t flags,
|
---|
256 | pfn_t constraint, size_t hint)
|
---|
257 | {
|
---|
258 | if (hint >= zones.count)
|
---|
259 | hint = 0;
|
---|
260 |
|
---|
261 | size_t i = hint;
|
---|
262 | do {
|
---|
263 | /*
|
---|
264 | * Check whether the zone meets the search criteria.
|
---|
265 | */
|
---|
266 | if (ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) {
|
---|
267 | /*
|
---|
268 | * Check if the zone can satisfy the allocation request.
|
---|
269 | */
|
---|
270 | if (zone_can_alloc(&zones.info[i], count, constraint))
|
---|
271 | return i;
|
---|
272 | }
|
---|
273 |
|
---|
274 | i++;
|
---|
275 | if (i >= zones.count)
|
---|
276 | i = 0;
|
---|
277 |
|
---|
278 | } while (i != hint);
|
---|
279 |
|
---|
280 | return (size_t) -1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /******************/
|
---|
284 | /* Zone functions */
|
---|
285 | /******************/
|
---|
286 |
|
---|
287 | /** Return frame from zone. */
|
---|
288 | NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t index)
|
---|
289 | {
|
---|
290 | ASSERT(index < zone->count);
|
---|
291 |
|
---|
292 | return &zone->frames[index];
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Allocate frame in particular zone.
|
---|
296 | *
|
---|
297 | * Assume zone is locked and is available for allocation.
|
---|
298 | * Panics if allocation is impossible.
|
---|
299 | *
|
---|
300 | * @param zone Zone to allocate from.
|
---|
301 | * @param count Number of frames to allocate
|
---|
302 | * @param constraint Indication of bits that cannot be set in the
|
---|
303 | * physical frame number of the first allocated frame.
|
---|
304 | *
|
---|
305 | * @return Frame index in zone.
|
---|
306 | *
|
---|
307 | */
|
---|
308 | NO_TRACE static size_t zone_frame_alloc(zone_t *zone, size_t count,
|
---|
309 | pfn_t constraint)
|
---|
310 | {
|
---|
311 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
312 |
|
---|
313 | /* Allocate frames from zone */
|
---|
314 | size_t index;
|
---|
315 | int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
---|
316 | constraint, &index);
|
---|
317 |
|
---|
318 | ASSERT(avail);
|
---|
319 |
|
---|
320 | /* Update frame reference count */
|
---|
321 | for (size_t i = 0; i < count; i++) {
|
---|
322 | frame_t *frame = zone_get_frame(zone, index + i);
|
---|
323 |
|
---|
324 | ASSERT(frame->refcount == 0);
|
---|
325 | frame->refcount = 1;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* Update zone information. */
|
---|
329 | zone->free_count -= count;
|
---|
330 | zone->busy_count += count;
|
---|
331 |
|
---|
332 | return index;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /** Free frame from zone.
|
---|
336 | *
|
---|
337 | * Assume zone is locked and is available for deallocation.
|
---|
338 | *
|
---|
339 | * @param zone Pointer to zone from which the frame is to be freed.
|
---|
340 | * @param index Frame index relative to zone.
|
---|
341 | *
|
---|
342 | * @return Number of freed frames.
|
---|
343 | *
|
---|
344 | */
|
---|
345 | NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t index)
|
---|
346 | {
|
---|
347 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
348 |
|
---|
349 | frame_t *frame = zone_get_frame(zone, index);
|
---|
350 |
|
---|
351 | ASSERT(frame->refcount > 0);
|
---|
352 |
|
---|
353 | if (!--frame->refcount) {
|
---|
354 | bitmap_free_range(&zone->bitmap, index, 1);
|
---|
355 |
|
---|
356 | /* Update zone information. */
|
---|
357 | zone->free_count++;
|
---|
358 | zone->busy_count--;
|
---|
359 |
|
---|
360 | return 1;
|
---|
361 | }
|
---|
362 |
|
---|
363 | return 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /** Mark frame in zone unavailable to allocation. */
|
---|
367 | NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index)
|
---|
368 | {
|
---|
369 | if (!(zone->flags & ZONE_AVAILABLE))
|
---|
370 | return;
|
---|
371 |
|
---|
372 | frame_t *frame = zone_get_frame(zone, index);
|
---|
373 | if (frame->refcount > 0)
|
---|
374 | return;
|
---|
375 |
|
---|
376 | frame->refcount = 1;
|
---|
377 | bitmap_set_range(&zone->bitmap, index, 1);
|
---|
378 |
|
---|
379 | zone->free_count--;
|
---|
380 | reserve_force_alloc(1);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /** Merge two zones.
|
---|
384 | *
|
---|
385 | * Assume z1 & z2 are locked and compatible and zones lock is
|
---|
386 | * locked.
|
---|
387 | *
|
---|
388 | * @param z1 First zone to merge.
|
---|
389 | * @param z2 Second zone to merge.
|
---|
390 | * @param old_z1 Original data of the first zone.
|
---|
391 | * @param confdata Merged zone configuration data.
|
---|
392 | *
|
---|
393 | */
|
---|
394 | NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
|
---|
395 | void *confdata)
|
---|
396 | {
|
---|
397 | ASSERT(zones.info[z1].flags & ZONE_AVAILABLE);
|
---|
398 | ASSERT(zones.info[z2].flags & ZONE_AVAILABLE);
|
---|
399 | ASSERT(zones.info[z1].flags == zones.info[z2].flags);
|
---|
400 | ASSERT(zones.info[z1].base < zones.info[z2].base);
|
---|
401 | ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
|
---|
402 | zones.info[z2].base, zones.info[z2].count));
|
---|
403 |
|
---|
404 | /* Difference between zone bases */
|
---|
405 | pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
|
---|
406 |
|
---|
407 | zones.info[z1].count = base_diff + zones.info[z2].count;
|
---|
408 | zones.info[z1].free_count += zones.info[z2].free_count;
|
---|
409 | zones.info[z1].busy_count += zones.info[z2].busy_count;
|
---|
410 |
|
---|
411 | bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
|
---|
412 | BITMAP_BLOCK_SIZE, confdata +
|
---|
413 | (sizeof(frame_t) * zones.info[z1].count));
|
---|
414 |
|
---|
415 | zones.info[z1].frames = (frame_t *) confdata;
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * Copy frames and bits from both zones to preserve parents, etc.
|
---|
419 | */
|
---|
420 |
|
---|
421 | for (size_t i = 0; i < old_z1->count; i++) {
|
---|
422 | bitmap_set(&zones.info[z1].bitmap, i,
|
---|
423 | bitmap_get(&old_z1->bitmap, i));
|
---|
424 | zones.info[z1].frames[i] = old_z1->frames[i];
|
---|
425 | }
|
---|
426 |
|
---|
427 | for (size_t i = 0; i < zones.info[z2].count; i++) {
|
---|
428 | bitmap_set(&zones.info[z1].bitmap, base_diff + i,
|
---|
429 | bitmap_get(&zones.info[z2].bitmap, i));
|
---|
430 | zones.info[z1].frames[base_diff + i] =
|
---|
431 | zones.info[z2].frames[i];
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | /** Return old configuration frames into the zone.
|
---|
436 | *
|
---|
437 | * We have two cases:
|
---|
438 | * - The configuration data is outside the zone
|
---|
439 | * -> do nothing (perhaps call frame_free?)
|
---|
440 | * - The configuration data was created by zone_create
|
---|
441 | * or updated by reduce_region -> free every frame
|
---|
442 | *
|
---|
443 | * @param znum The actual zone where freeing should occur.
|
---|
444 | * @param pfn Old zone configuration frame.
|
---|
445 | * @param count Old zone frame count.
|
---|
446 | *
|
---|
447 | */
|
---|
448 | NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
|
---|
449 | {
|
---|
450 | ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
|
---|
451 |
|
---|
452 | size_t cframes = SIZE2FRAMES(zone_conf_size(count));
|
---|
453 |
|
---|
454 | if ((pfn < zones.info[znum].base) ||
|
---|
455 | (pfn >= zones.info[znum].base + zones.info[znum].count))
|
---|
456 | return;
|
---|
457 |
|
---|
458 | for (size_t i = 0; i < cframes; i++)
|
---|
459 | (void) zone_frame_free(&zones.info[znum],
|
---|
460 | pfn - zones.info[znum].base + i);
|
---|
461 | }
|
---|
462 |
|
---|
463 | /** Merge zones z1 and z2.
|
---|
464 | *
|
---|
465 | * The merged zones must be 2 zones with no zone existing in between
|
---|
466 | * (which means that z2 = z1 + 1). Both zones must be available zones
|
---|
467 | * with the same flags.
|
---|
468 | *
|
---|
469 | * When you create a new zone, the frame allocator configuration does
|
---|
470 | * not to be 2^order size. Once the allocator is running it is no longer
|
---|
471 | * possible, merged configuration data occupies more space :-/
|
---|
472 | *
|
---|
473 | */
|
---|
474 | bool zone_merge(size_t z1, size_t z2)
|
---|
475 | {
|
---|
476 | irq_spinlock_lock(&zones.lock, true);
|
---|
477 |
|
---|
478 | bool ret = true;
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * We can join only 2 zones with none existing inbetween,
|
---|
482 | * the zones have to be available and with the same
|
---|
483 | * set of flags
|
---|
484 | */
|
---|
485 | if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
|
---|
486 | (zones.info[z1].flags != zones.info[z2].flags)) {
|
---|
487 | ret = false;
|
---|
488 | goto errout;
|
---|
489 | }
|
---|
490 |
|
---|
491 | pfn_t cframes = SIZE2FRAMES(zone_conf_size(
|
---|
492 | zones.info[z2].base - zones.info[z1].base
|
---|
493 | + zones.info[z2].count));
|
---|
494 |
|
---|
495 | /* Allocate merged zone data inside one of the zones */
|
---|
496 | pfn_t pfn;
|
---|
497 | if (zone_can_alloc(&zones.info[z1], cframes, 0)) {
|
---|
498 | pfn = zones.info[z1].base +
|
---|
499 | zone_frame_alloc(&zones.info[z1], cframes, 0);
|
---|
500 | } else if (zone_can_alloc(&zones.info[z2], cframes, 0)) {
|
---|
501 | pfn = zones.info[z2].base +
|
---|
502 | zone_frame_alloc(&zones.info[z2], cframes, 0);
|
---|
503 | } else {
|
---|
504 | ret = false;
|
---|
505 | goto errout;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /* Preserve original data from z1 */
|
---|
509 | zone_t old_z1 = zones.info[z1];
|
---|
510 |
|
---|
511 | /* Do zone merging */
|
---|
512 | zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
|
---|
513 |
|
---|
514 | /* Subtract zone information from busy frames */
|
---|
515 | zones.info[z1].busy_count -= cframes;
|
---|
516 |
|
---|
517 | /* Free old zone information */
|
---|
518 | return_config_frames(z1,
|
---|
519 | ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
|
---|
520 | return_config_frames(z1,
|
---|
521 | ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
|
---|
522 | zones.info[z2].count);
|
---|
523 |
|
---|
524 | /* Move zones down */
|
---|
525 | for (size_t i = z2 + 1; i < zones.count; i++)
|
---|
526 | zones.info[i - 1] = zones.info[i];
|
---|
527 |
|
---|
528 | zones.count--;
|
---|
529 |
|
---|
530 | errout:
|
---|
531 | irq_spinlock_unlock(&zones.lock, true);
|
---|
532 |
|
---|
533 | return ret;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /** Merge all mergeable zones into one big zone.
|
---|
537 | *
|
---|
538 | * It is reasonable to do this on systems where
|
---|
539 | * BIOS reports parts in chunks, so that we could
|
---|
540 | * have 1 zone (it's faster).
|
---|
541 | *
|
---|
542 | */
|
---|
543 | void zone_merge_all(void)
|
---|
544 | {
|
---|
545 | size_t i = 1;
|
---|
546 |
|
---|
547 | while (i < zones.count) {
|
---|
548 | if (!zone_merge(i - 1, i))
|
---|
549 | i++;
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | /** Create new frame zone.
|
---|
554 | *
|
---|
555 | * @param zone Zone to construct.
|
---|
556 | * @param start Physical address of the first frame within the zone.
|
---|
557 | * @param count Count of frames in zone.
|
---|
558 | * @param flags Zone flags.
|
---|
559 | * @param confdata Configuration data of the zone.
|
---|
560 | *
|
---|
561 | * @return Initialized zone.
|
---|
562 | *
|
---|
563 | */
|
---|
564 | NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
|
---|
565 | zone_flags_t flags, void *confdata)
|
---|
566 | {
|
---|
567 | zone->base = start;
|
---|
568 | zone->count = count;
|
---|
569 | zone->flags = flags;
|
---|
570 | zone->free_count = count;
|
---|
571 | zone->busy_count = 0;
|
---|
572 |
|
---|
573 | if (flags & ZONE_AVAILABLE) {
|
---|
574 | /*
|
---|
575 | * Initialize frame bitmap (located after the array of
|
---|
576 | * frame_t structures in the configuration space).
|
---|
577 | */
|
---|
578 |
|
---|
579 | bitmap_initialize(&zone->bitmap, count, BITMAP_BLOCK_SIZE,
|
---|
580 | confdata + (sizeof(frame_t) * count));
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * Initialize the array of frame_t structures.
|
---|
584 | */
|
---|
585 |
|
---|
586 | zone->frames = (frame_t *) confdata;
|
---|
587 |
|
---|
588 | for (size_t i = 0; i < count; i++)
|
---|
589 | frame_initialize(&zone->frames[i]);
|
---|
590 | } else {
|
---|
591 | bitmap_initialize(&zone->bitmap, 0, 0, NULL);
|
---|
592 | zone->frames = NULL;
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | /** Compute configuration data size for zone.
|
---|
597 | *
|
---|
598 | * @param count Size of zone in frames.
|
---|
599 | *
|
---|
600 | * @return Size of zone configuration info (in bytes).
|
---|
601 | *
|
---|
602 | */
|
---|
603 | size_t zone_conf_size(size_t count)
|
---|
604 | {
|
---|
605 | return (count * sizeof(frame_t) +
|
---|
606 | bitmap_size(count, BITMAP_BLOCK_SIZE));
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** Allocate external configuration frames from low memory. */
|
---|
610 | pfn_t zone_external_conf_alloc(size_t count)
|
---|
611 | {
|
---|
612 | size_t frames = SIZE2FRAMES(zone_conf_size(count));
|
---|
613 |
|
---|
614 | return ADDR2PFN((uintptr_t)
|
---|
615 | frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
|
---|
616 | }
|
---|
617 |
|
---|
618 | /** Create and add zone to system.
|
---|
619 | *
|
---|
620 | * @param start First frame number (absolute).
|
---|
621 | * @param count Size of zone in frames.
|
---|
622 | * @param confframe Where configuration frames are supposed to be.
|
---|
623 | * Automatically checks that we will not disturb the
|
---|
624 | * kernel and possibly init. If confframe is given
|
---|
625 | * _outside_ this zone, it is expected, that the area is
|
---|
626 | * already marked BUSY and big enough to contain
|
---|
627 | * zone_conf_size() amount of data. If the confframe is
|
---|
628 | * inside the area, the zone free frame information is
|
---|
629 | * modified not to include it.
|
---|
630 | *
|
---|
631 | * @return Zone number or -1 on error.
|
---|
632 | *
|
---|
633 | */
|
---|
634 | size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
|
---|
635 | zone_flags_t flags)
|
---|
636 | {
|
---|
637 | irq_spinlock_lock(&zones.lock, true);
|
---|
638 |
|
---|
639 | if (flags & ZONE_AVAILABLE) { /* Create available zone */
|
---|
640 | /*
|
---|
641 | * Theoretically we could have NULL here, practically make sure
|
---|
642 | * nobody tries to do that. If some platform requires, remove
|
---|
643 | * the assert
|
---|
644 | */
|
---|
645 | ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
|
---|
646 |
|
---|
647 | /* Update the known end of physical memory. */
|
---|
648 | config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * If confframe is supposed to be inside our zone, then make sure
|
---|
652 | * it does not span kernel & init
|
---|
653 | */
|
---|
654 | size_t confcount = SIZE2FRAMES(zone_conf_size(count));
|
---|
655 |
|
---|
656 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
657 | for (; confframe < start + count; confframe++) {
|
---|
658 | uintptr_t addr = PFN2ADDR(confframe);
|
---|
659 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
660 | KA2PA(config.base), config.kernel_size))
|
---|
661 | continue;
|
---|
662 |
|
---|
663 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
664 | KA2PA(config.stack_base), config.stack_size))
|
---|
665 | continue;
|
---|
666 |
|
---|
667 | bool overlap = false;
|
---|
668 | for (size_t i = 0; i < init.cnt; i++) {
|
---|
669 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
670 | init.tasks[i].paddr,
|
---|
671 | init.tasks[i].size)) {
|
---|
672 | overlap = true;
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (overlap)
|
---|
678 | continue;
|
---|
679 |
|
---|
680 | break;
|
---|
681 | }
|
---|
682 |
|
---|
683 | if (confframe >= start + count)
|
---|
684 | panic("Cannot find configuration data for zone.");
|
---|
685 | }
|
---|
686 |
|
---|
687 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
688 | if (znum == (size_t) -1) {
|
---|
689 | irq_spinlock_unlock(&zones.lock, true);
|
---|
690 | return (size_t) -1;
|
---|
691 | }
|
---|
692 |
|
---|
693 | void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
|
---|
694 | zone_construct(&zones.info[znum], start, count, flags, confdata);
|
---|
695 |
|
---|
696 | /* If confdata in zone, mark as unavailable */
|
---|
697 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
698 | for (size_t i = confframe; i < confframe + confcount; i++)
|
---|
699 | zone_mark_unavailable(&zones.info[znum],
|
---|
700 | i - zones.info[znum].base);
|
---|
701 | }
|
---|
702 |
|
---|
703 | irq_spinlock_unlock(&zones.lock, true);
|
---|
704 |
|
---|
705 | return znum;
|
---|
706 | }
|
---|
707 |
|
---|
708 | /* Non-available zone */
|
---|
709 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
710 | if (znum == (size_t) -1) {
|
---|
711 | irq_spinlock_unlock(&zones.lock, true);
|
---|
712 | return (size_t) -1;
|
---|
713 | }
|
---|
714 |
|
---|
715 | zone_construct(&zones.info[znum], start, count, flags, NULL);
|
---|
716 |
|
---|
717 | irq_spinlock_unlock(&zones.lock, true);
|
---|
718 |
|
---|
719 | return znum;
|
---|
720 | }
|
---|
721 |
|
---|
722 | /*******************/
|
---|
723 | /* Frame functions */
|
---|
724 | /*******************/
|
---|
725 |
|
---|
726 | /** Set parent of frame. */
|
---|
727 | void frame_set_parent(pfn_t pfn, void *data, size_t hint)
|
---|
728 | {
|
---|
729 | irq_spinlock_lock(&zones.lock, true);
|
---|
730 |
|
---|
731 | size_t znum = find_zone(pfn, 1, hint);
|
---|
732 |
|
---|
733 | ASSERT(znum != (size_t) -1);
|
---|
734 |
|
---|
735 | zone_get_frame(&zones.info[znum],
|
---|
736 | pfn - zones.info[znum].base)->parent = data;
|
---|
737 |
|
---|
738 | irq_spinlock_unlock(&zones.lock, true);
|
---|
739 | }
|
---|
740 |
|
---|
741 | void *frame_get_parent(pfn_t pfn, size_t hint)
|
---|
742 | {
|
---|
743 | irq_spinlock_lock(&zones.lock, true);
|
---|
744 |
|
---|
745 | size_t znum = find_zone(pfn, 1, hint);
|
---|
746 |
|
---|
747 | ASSERT(znum != (size_t) -1);
|
---|
748 |
|
---|
749 | void *res = zone_get_frame(&zones.info[znum],
|
---|
750 | pfn - zones.info[znum].base)->parent;
|
---|
751 |
|
---|
752 | irq_spinlock_unlock(&zones.lock, true);
|
---|
753 |
|
---|
754 | return res;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /** Allocate frames of physical memory.
|
---|
758 | *
|
---|
759 | * @param count Number of continuous frames to allocate.
|
---|
760 | * @param flags Flags for host zone selection and address processing.
|
---|
761 | * @param constraint Indication of physical address bits that cannot be
|
---|
762 | * set in the address of the first allocated frame.
|
---|
763 | * @param pzone Preferred zone.
|
---|
764 | *
|
---|
765 | * @return Physical address of the allocated frame.
|
---|
766 | *
|
---|
767 | */
|
---|
768 | uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
|
---|
769 | uintptr_t constraint, size_t *pzone)
|
---|
770 | {
|
---|
771 | ASSERT(count > 0);
|
---|
772 |
|
---|
773 | size_t hint = pzone ? (*pzone) : 0;
|
---|
774 | pfn_t frame_constraint = ADDR2PFN(constraint);
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * If not told otherwise, we must first reserve the memory.
|
---|
778 | */
|
---|
779 | if (!(flags & FRAME_NO_RESERVE))
|
---|
780 | reserve_force_alloc(count);
|
---|
781 |
|
---|
782 | loop:
|
---|
783 | irq_spinlock_lock(&zones.lock, true);
|
---|
784 |
|
---|
785 | /*
|
---|
786 | * First, find suitable frame zone.
|
---|
787 | */
|
---|
788 | size_t znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
789 | frame_constraint, hint);
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * If no memory, reclaim some slab memory,
|
---|
793 | * if it does not help, reclaim all.
|
---|
794 | */
|
---|
795 | if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
|
---|
796 | irq_spinlock_unlock(&zones.lock, true);
|
---|
797 | size_t freed = slab_reclaim(0);
|
---|
798 | irq_spinlock_lock(&zones.lock, true);
|
---|
799 |
|
---|
800 | if (freed > 0)
|
---|
801 | znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
802 | frame_constraint, hint);
|
---|
803 |
|
---|
804 | if (znum == (size_t) -1) {
|
---|
805 | irq_spinlock_unlock(&zones.lock, true);
|
---|
806 | freed = slab_reclaim(SLAB_RECLAIM_ALL);
|
---|
807 | irq_spinlock_lock(&zones.lock, true);
|
---|
808 |
|
---|
809 | if (freed > 0)
|
---|
810 | znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
811 | frame_constraint, hint);
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | if (znum == (size_t) -1) {
|
---|
816 | if (flags & FRAME_ATOMIC) {
|
---|
817 | irq_spinlock_unlock(&zones.lock, true);
|
---|
818 |
|
---|
819 | if (!(flags & FRAME_NO_RESERVE))
|
---|
820 | reserve_free(count);
|
---|
821 |
|
---|
822 | return 0;
|
---|
823 | }
|
---|
824 |
|
---|
825 | #ifdef CONFIG_DEBUG
|
---|
826 | size_t avail = frame_total_free_get_internal();
|
---|
827 | #endif
|
---|
828 |
|
---|
829 | irq_spinlock_unlock(&zones.lock, true);
|
---|
830 |
|
---|
831 | if (!THREAD)
|
---|
832 | panic("Cannot wait for memory to become available.");
|
---|
833 |
|
---|
834 | /*
|
---|
835 | * Sleep until some frames are available again.
|
---|
836 | */
|
---|
837 |
|
---|
838 | #ifdef CONFIG_DEBUG
|
---|
839 | printf("Thread %" PRIu64 " waiting for %zu frames, "
|
---|
840 | "%zu available.\n", THREAD->tid, count, avail);
|
---|
841 | #endif
|
---|
842 |
|
---|
843 | /*
|
---|
844 | * Since the mem_avail_mtx is an active mutex, we need to
|
---|
845 | * disable interrupts to prevent deadlock with TLB shootdown.
|
---|
846 | */
|
---|
847 | ipl_t ipl = interrupts_disable();
|
---|
848 | mutex_lock(&mem_avail_mtx);
|
---|
849 |
|
---|
850 | if (mem_avail_req > 0)
|
---|
851 | mem_avail_req = min(mem_avail_req, count);
|
---|
852 | else
|
---|
853 | mem_avail_req = count;
|
---|
854 |
|
---|
855 | size_t gen = mem_avail_gen;
|
---|
856 |
|
---|
857 | while (gen == mem_avail_gen)
|
---|
858 | condvar_wait(&mem_avail_cv, &mem_avail_mtx);
|
---|
859 |
|
---|
860 | mutex_unlock(&mem_avail_mtx);
|
---|
861 | interrupts_restore(ipl);
|
---|
862 |
|
---|
863 | #ifdef CONFIG_DEBUG
|
---|
864 | printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
|
---|
865 | #endif
|
---|
866 |
|
---|
867 | goto loop;
|
---|
868 | }
|
---|
869 |
|
---|
870 | pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
|
---|
871 | frame_constraint) + zones.info[znum].base;
|
---|
872 |
|
---|
873 | irq_spinlock_unlock(&zones.lock, true);
|
---|
874 |
|
---|
875 | if (pzone)
|
---|
876 | *pzone = znum;
|
---|
877 |
|
---|
878 | return PFN2ADDR(pfn);
|
---|
879 | }
|
---|
880 |
|
---|
881 | uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
|
---|
882 | {
|
---|
883 | return frame_alloc_generic(count, flags, constraint, NULL);
|
---|
884 | }
|
---|
885 |
|
---|
886 | uintptr_t frame_alloc_noreserve(size_t count, frame_flags_t flags,
|
---|
887 | uintptr_t constraint)
|
---|
888 | {
|
---|
889 | return frame_alloc_generic(count, flags | FRAME_NO_RESERVE, constraint,
|
---|
890 | NULL);
|
---|
891 | }
|
---|
892 |
|
---|
893 | /** Free frames of physical memory.
|
---|
894 | *
|
---|
895 | * Find respective frame structures for supplied physical frames.
|
---|
896 | * Decrement each frame reference count. If it drops to zero, mark
|
---|
897 | * the frames as available.
|
---|
898 | *
|
---|
899 | * @param start Physical Address of the first frame to be freed.
|
---|
900 | * @param count Number of frames to free.
|
---|
901 | * @param flags Flags to control memory reservation.
|
---|
902 | *
|
---|
903 | */
|
---|
904 | void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
|
---|
905 | {
|
---|
906 | size_t freed = 0;
|
---|
907 |
|
---|
908 | irq_spinlock_lock(&zones.lock, true);
|
---|
909 |
|
---|
910 | for (size_t i = 0; i < count; i++) {
|
---|
911 | /*
|
---|
912 | * First, find host frame zone for addr.
|
---|
913 | */
|
---|
914 | pfn_t pfn = ADDR2PFN(start) + i;
|
---|
915 | size_t znum = find_zone(pfn, 1, 0);
|
---|
916 |
|
---|
917 | ASSERT(znum != (size_t) -1);
|
---|
918 |
|
---|
919 | freed += zone_frame_free(&zones.info[znum],
|
---|
920 | pfn - zones.info[znum].base);
|
---|
921 | }
|
---|
922 |
|
---|
923 | irq_spinlock_unlock(&zones.lock, true);
|
---|
924 |
|
---|
925 | /*
|
---|
926 | * Signal that some memory has been freed.
|
---|
927 | * Since the mem_avail_mtx is an active mutex,
|
---|
928 | * we need to disable interruptsto prevent deadlock
|
---|
929 | * with TLB shootdown.
|
---|
930 | */
|
---|
931 |
|
---|
932 | ipl_t ipl = interrupts_disable();
|
---|
933 | mutex_lock(&mem_avail_mtx);
|
---|
934 |
|
---|
935 | if (mem_avail_req > 0)
|
---|
936 | mem_avail_req -= min(mem_avail_req, freed);
|
---|
937 |
|
---|
938 | if (mem_avail_req == 0) {
|
---|
939 | mem_avail_gen++;
|
---|
940 | condvar_broadcast(&mem_avail_cv);
|
---|
941 | }
|
---|
942 |
|
---|
943 | mutex_unlock(&mem_avail_mtx);
|
---|
944 | interrupts_restore(ipl);
|
---|
945 |
|
---|
946 | if (!(flags & FRAME_NO_RESERVE))
|
---|
947 | reserve_free(freed);
|
---|
948 | }
|
---|
949 |
|
---|
950 | void frame_free(uintptr_t frame, size_t count)
|
---|
951 | {
|
---|
952 | frame_free_generic(frame, count, 0);
|
---|
953 | }
|
---|
954 |
|
---|
955 | void frame_free_noreserve(uintptr_t frame, size_t count)
|
---|
956 | {
|
---|
957 | frame_free_generic(frame, count, FRAME_NO_RESERVE);
|
---|
958 | }
|
---|
959 |
|
---|
960 | /** Add reference to frame.
|
---|
961 | *
|
---|
962 | * Find respective frame structure for supplied PFN and
|
---|
963 | * increment frame reference count.
|
---|
964 | *
|
---|
965 | * @param pfn Frame number of the frame to be freed.
|
---|
966 | *
|
---|
967 | */
|
---|
968 | NO_TRACE void frame_reference_add(pfn_t pfn)
|
---|
969 | {
|
---|
970 | irq_spinlock_lock(&zones.lock, true);
|
---|
971 |
|
---|
972 | /*
|
---|
973 | * First, find host frame zone for addr.
|
---|
974 | */
|
---|
975 | size_t znum = find_zone(pfn, 1, 0);
|
---|
976 |
|
---|
977 | ASSERT(znum != (size_t) -1);
|
---|
978 |
|
---|
979 | zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
|
---|
980 |
|
---|
981 | irq_spinlock_unlock(&zones.lock, true);
|
---|
982 | }
|
---|
983 |
|
---|
984 | /** Mark given range unavailable in frame zones.
|
---|
985 | *
|
---|
986 | */
|
---|
987 | NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
|
---|
988 | {
|
---|
989 | irq_spinlock_lock(&zones.lock, true);
|
---|
990 |
|
---|
991 | for (size_t i = 0; i < count; i++) {
|
---|
992 | size_t znum = find_zone(start + i, 1, 0);
|
---|
993 |
|
---|
994 | if (znum == (size_t) -1) /* PFN not found */
|
---|
995 | continue;
|
---|
996 |
|
---|
997 | zone_mark_unavailable(&zones.info[znum],
|
---|
998 | start + i - zones.info[znum].base);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /** Initialize physical memory management.
|
---|
1005 | *
|
---|
1006 | */
|
---|
1007 | void frame_init(void)
|
---|
1008 | {
|
---|
1009 | if (config.cpu_active == 1) {
|
---|
1010 | zones.count = 0;
|
---|
1011 | irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
|
---|
1012 | mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
|
---|
1013 | condvar_initialize(&mem_avail_cv);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /* Tell the architecture to create some memory */
|
---|
1017 | frame_low_arch_init();
|
---|
1018 |
|
---|
1019 | if (config.cpu_active == 1) {
|
---|
1020 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
|
---|
1021 | SIZE2FRAMES(config.kernel_size));
|
---|
1022 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
|
---|
1023 | SIZE2FRAMES(config.stack_size));
|
---|
1024 |
|
---|
1025 | for (size_t i = 0; i < init.cnt; i++)
|
---|
1026 | frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
|
---|
1027 | SIZE2FRAMES(init.tasks[i].size));
|
---|
1028 |
|
---|
1029 | if (ballocs.size)
|
---|
1030 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
|
---|
1031 | SIZE2FRAMES(ballocs.size));
|
---|
1032 |
|
---|
1033 | /*
|
---|
1034 | * Blacklist first frame, as allocating NULL would
|
---|
1035 | * fail in some places
|
---|
1036 | */
|
---|
1037 | frame_mark_unavailable(0, 1);
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | frame_high_arch_init();
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /** Adjust bounds of physical memory region according to low/high memory split.
|
---|
1044 | *
|
---|
1045 | * @param low[in] If true, the adjustment is performed to make the region
|
---|
1046 | * fit in the low memory. Otherwise the adjustment is
|
---|
1047 | * performed to make the region fit in the high memory.
|
---|
1048 | * @param basep[inout] Pointer to a variable which contains the region's base
|
---|
1049 | * address and which may receive the adjusted base address.
|
---|
1050 | * @param sizep[inout] Pointer to a variable which contains the region's size
|
---|
1051 | * and which may receive the adjusted size.
|
---|
1052 | *
|
---|
1053 | * @return True if the region still exists even after the adjustment.
|
---|
1054 | * @return False otherwise.
|
---|
1055 | *
|
---|
1056 | */
|
---|
1057 | bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
|
---|
1058 | {
|
---|
1059 | uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
|
---|
1060 |
|
---|
1061 | if (low) {
|
---|
1062 | if (*basep > limit)
|
---|
1063 | return false;
|
---|
1064 |
|
---|
1065 | if (*basep + *sizep > limit)
|
---|
1066 | *sizep = limit - *basep;
|
---|
1067 | } else {
|
---|
1068 | if (*basep + *sizep <= limit)
|
---|
1069 | return false;
|
---|
1070 |
|
---|
1071 | if (*basep <= limit) {
|
---|
1072 | *sizep -= limit - *basep;
|
---|
1073 | *basep = limit;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | return true;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /** Return total size of all zones.
|
---|
1081 | *
|
---|
1082 | */
|
---|
1083 | uint64_t zones_total_size(void)
|
---|
1084 | {
|
---|
1085 | irq_spinlock_lock(&zones.lock, true);
|
---|
1086 |
|
---|
1087 | uint64_t total = 0;
|
---|
1088 |
|
---|
1089 | for (size_t i = 0; i < zones.count; i++)
|
---|
1090 | total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
1091 |
|
---|
1092 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1093 |
|
---|
1094 | return total;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
|
---|
1098 | uint64_t *free)
|
---|
1099 | {
|
---|
1100 | ASSERT(total != NULL);
|
---|
1101 | ASSERT(unavail != NULL);
|
---|
1102 | ASSERT(busy != NULL);
|
---|
1103 | ASSERT(free != NULL);
|
---|
1104 |
|
---|
1105 | irq_spinlock_lock(&zones.lock, true);
|
---|
1106 |
|
---|
1107 | *total = 0;
|
---|
1108 | *unavail = 0;
|
---|
1109 | *busy = 0;
|
---|
1110 | *free = 0;
|
---|
1111 |
|
---|
1112 | for (size_t i = 0; i < zones.count; i++) {
|
---|
1113 | *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
1114 |
|
---|
1115 | if (zones.info[i].flags & ZONE_AVAILABLE) {
|
---|
1116 | *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
|
---|
1117 | *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
|
---|
1118 | } else
|
---|
1119 | *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /** Prints list of zones.
|
---|
1126 | *
|
---|
1127 | */
|
---|
1128 | void zones_print_list(void)
|
---|
1129 | {
|
---|
1130 | #ifdef __32_BITS__
|
---|
1131 | printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
1132 | #endif
|
---|
1133 |
|
---|
1134 | #ifdef __64_BITS__
|
---|
1135 | printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
1136 | #endif
|
---|
1137 |
|
---|
1138 | /*
|
---|
1139 | * Because printing may require allocation of memory, we may not hold
|
---|
1140 | * the frame allocator locks when printing zone statistics. Therefore,
|
---|
1141 | * we simply gather the statistics under the protection of the locks and
|
---|
1142 | * print the statistics when the locks have been released.
|
---|
1143 | *
|
---|
1144 | * When someone adds/removes zones while we are printing the statistics,
|
---|
1145 | * we may end up with inaccurate output (e.g. a zone being skipped from
|
---|
1146 | * the listing).
|
---|
1147 | */
|
---|
1148 |
|
---|
1149 | for (size_t i = 0;; i++) {
|
---|
1150 | irq_spinlock_lock(&zones.lock, true);
|
---|
1151 |
|
---|
1152 | if (i >= zones.count) {
|
---|
1153 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1154 | break;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | uintptr_t base = PFN2ADDR(zones.info[i].base);
|
---|
1158 | size_t count = zones.info[i].count;
|
---|
1159 | zone_flags_t flags = zones.info[i].flags;
|
---|
1160 | size_t free_count = zones.info[i].free_count;
|
---|
1161 | size_t busy_count = zones.info[i].busy_count;
|
---|
1162 |
|
---|
1163 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1164 |
|
---|
1165 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
1166 |
|
---|
1167 | printf("%-4zu", i);
|
---|
1168 |
|
---|
1169 | #ifdef __32_BITS__
|
---|
1170 | printf(" %p", (void *) base);
|
---|
1171 | #endif
|
---|
1172 |
|
---|
1173 | #ifdef __64_BITS__
|
---|
1174 | printf(" %p", (void *) base);
|
---|
1175 | #endif
|
---|
1176 |
|
---|
1177 | printf(" %12zu %c%c%c%c%c ", count,
|
---|
1178 | available ? 'A' : '-',
|
---|
1179 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
1180 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
1181 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
1182 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
1183 |
|
---|
1184 | if (available)
|
---|
1185 | printf("%14zu %14zu",
|
---|
1186 | free_count, busy_count);
|
---|
1187 |
|
---|
1188 | printf("\n");
|
---|
1189 | }
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /** Prints zone details.
|
---|
1193 | *
|
---|
1194 | * @param num Zone base address or zone number.
|
---|
1195 | *
|
---|
1196 | */
|
---|
1197 | void zone_print_one(size_t num)
|
---|
1198 | {
|
---|
1199 | irq_spinlock_lock(&zones.lock, true);
|
---|
1200 | size_t znum = (size_t) -1;
|
---|
1201 |
|
---|
1202 | for (size_t i = 0; i < zones.count; i++) {
|
---|
1203 | if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
|
---|
1204 | znum = i;
|
---|
1205 | break;
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | if (znum == (size_t) -1) {
|
---|
1210 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1211 | printf("Zone not found.\n");
|
---|
1212 | return;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | uintptr_t base = PFN2ADDR(zones.info[znum].base);
|
---|
1216 | zone_flags_t flags = zones.info[znum].flags;
|
---|
1217 | size_t count = zones.info[znum].count;
|
---|
1218 | size_t free_count = zones.info[znum].free_count;
|
---|
1219 | size_t busy_count = zones.info[znum].busy_count;
|
---|
1220 |
|
---|
1221 | irq_spinlock_unlock(&zones.lock, true);
|
---|
1222 |
|
---|
1223 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
1224 |
|
---|
1225 | uint64_t size;
|
---|
1226 | const char *size_suffix;
|
---|
1227 | bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
|
---|
1228 |
|
---|
1229 | printf("Zone number: %zu\n", znum);
|
---|
1230 | printf("Zone base address: %p\n", (void *) base);
|
---|
1231 | printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
|
---|
1232 | size, size_suffix);
|
---|
1233 | printf("Zone flags: %c%c%c%c%c\n",
|
---|
1234 | available ? 'A' : '-',
|
---|
1235 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
1236 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
1237 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
1238 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
1239 |
|
---|
1240 | if (available) {
|
---|
1241 | bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
|
---|
1242 | false);
|
---|
1243 | printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
|
---|
1244 | busy_count, size, size_suffix);
|
---|
1245 | bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
|
---|
1246 | false);
|
---|
1247 | printf("Available space: %zu frames (%" PRIu64 " %s)\n",
|
---|
1248 | free_count, size, size_suffix);
|
---|
1249 | }
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | /** @}
|
---|
1253 | */
|
---|