Subversion Repositories HelenOS

Rev

Rev 3211 | Rev 4346 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2005 Jakub Jermar
3
 * Copyright (c) 2005 Sergey Bondari
4344 svoboda 4
 * Copyright (c) 2009 Martin Decky
1 jermar 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
 
1757 jermar 31
/** @addtogroup genericmm
1702 cejka 32
 * @{
33
 */
34
 
1248 jermar 35
/**
1702 cejka 36
 * @file
4344 svoboda 37
 * @brief Physical frame allocator.
1248 jermar 38
 *
39
 * This file contains the physical frame allocator and memory zone management.
40
 * The frame allocator is built on top of the buddy allocator.
41
 *
42
 * @see buddy.c
43
 */
44
 
1 jermar 45
#include <arch/types.h>
46
#include <mm/frame.h>
703 jermar 47
#include <mm/as.h>
1 jermar 48
#include <panic.h>
367 jermar 49
#include <debug.h>
788 jermar 50
#include <adt/list.h>
3191 svoboda 51
#include <synch/mutex.h>
52
#include <synch/condvar.h>
115 jermar 53
#include <arch/asm.h>
195 vana 54
#include <arch.h>
533 bondari 55
#include <print.h>
536 bondari 56
#include <align.h>
814 palkovsky 57
#include <mm/slab.h>
822 palkovsky 58
#include <bitops.h>
1063 palkovsky 59
#include <macros.h>
2128 jermar 60
#include <config.h>
115 jermar 61
 
4344 svoboda 62
zones_t zones;
367 jermar 63
 
814 palkovsky 64
/*
3191 svoboda 65
 * Synchronization primitives used to sleep when there is no memory
66
 * available.
67
 */
68
mutex_t mem_avail_mtx;
69
condvar_t mem_avail_cv;
4344 svoboda 70
count_t mem_avail_req = 0;  /**< Number of frames requested. */
71
count_t mem_avail_gen = 0;  /**< Generation counter. */
2121 decky 72
 
2725 decky 73
/********************/
814 palkovsky 74
/* Helper functions */
2725 decky 75
/********************/
76
 
814 palkovsky 77
static inline index_t frame_index(zone_t *zone, frame_t *frame)
762 palkovsky 78
{
2725 decky 79
    return (index_t) (frame - zone->frames);
762 palkovsky 80
}
2725 decky 81
 
814 palkovsky 82
static inline index_t frame_index_abs(zone_t *zone, frame_t *frame)
1 jermar 83
{
2725 decky 84
    return (index_t) (frame - zone->frames) + zone->base;
1 jermar 85
}
2725 decky 86
 
4344 svoboda 87
static inline bool frame_index_valid(zone_t *zone, index_t index)
814 palkovsky 88
{
2745 decky 89
    return (index < zone->count);
814 palkovsky 90
}
1 jermar 91
 
4344 svoboda 92
static inline index_t make_frame_index(zone_t *zone, frame_t *frame)
762 palkovsky 93
{
2725 decky 94
    return (frame - zone->frames);
762 palkovsky 95
}
96
 
3191 svoboda 97
/** Initialize frame structure.
762 palkovsky 98
 *
4344 svoboda 99
 * @param frame Frame structure to be initialized.
100
 *
762 palkovsky 101
 */
814 palkovsky 102
static void frame_initialize(frame_t *frame)
762 palkovsky 103
{
814 palkovsky 104
    frame->refcount = 1;
105
    frame->buddy_order = 0;
762 palkovsky 106
}
107
 
4344 svoboda 108
/*******************/
109
/* Zones functions */
110
/*******************/
762 palkovsky 111
 
3191 svoboda 112
/** Insert-sort zone into zones list.
822 palkovsky 113
 *
4344 svoboda 114
 * Assume interrupts are disabled and zones lock is
115
 * locked.
116
 *
117
 * @param base  Base frame of the newly inserted zone.
118
 * @param count Number of frames of the newly inserted zone.
119
 *
120
 * @return Zone number on success, -1 on error.
121
 *
1 jermar 122
 */
4344 svoboda 123
static count_t zones_insert_zone(pfn_t base, count_t count)
1 jermar 124
{
2725 decky 125
    if (zones.count + 1 == ZONES_MAX) {
126
        printf("Maximum zone count %u exceeded!\n", ZONES_MAX);
4344 svoboda 127
        return (count_t) -1;
2725 decky 128
    }
129
 
4344 svoboda 130
    count_t i;
1037 decky 131
    for (i = 0; i < zones.count; i++) {
4344 svoboda 132
        /* Check for overlap */
133
        if (overlaps(base, count,
134
            zones.info[i].base, zones.info[i].count)) {
822 palkovsky 135
            printf("Zones overlap!\n");
4344 svoboda 136
            return (count_t) -1;
814 palkovsky 137
        }
4344 svoboda 138
        if (base < zones.info[i].base)
822 palkovsky 139
            break;
368 jermar 140
    }
2725 decky 141
 
822 palkovsky 142
    /* Move other zones up */
4344 svoboda 143
    count_t j;
144
    for (j = zones.count; j > i; j--) {
145
        zones.info[j] = zones.info[j - 1];
146
        zones.info[j].buddy_system->data =
147
            (void *) &zones.info[j - 1];
148
    }
2725 decky 149
 
822 palkovsky 150
    zones.count++;
2725 decky 151
 
822 palkovsky 152
    return i;
1 jermar 153
}
154
 
4344 svoboda 155
/** Get total available frames.
2725 decky 156
 *
4344 svoboda 157
 * Assume interrupts are disabled and zones lock is
158
 * locked.
2725 decky 159
 *
4344 svoboda 160
 * @return Total number of available frames.
161
 *
368 jermar 162
 */
4344 svoboda 163
static count_t total_frames_free(void)
1 jermar 164
{
4344 svoboda 165
    count_t total = 0;
166
    count_t i;
167
    for (i = 0; i < zones.count; i++)
168
        total += zones.info[i].free_count;
533 bondari 169
 
4344 svoboda 170
    return total;
171
}
814 palkovsky 172
 
4344 svoboda 173
/** Find a zone with a given frames.
174
 *
175
 * Assume interrupts are disabled and zones lock is
176
 * locked.
177
 *
178
 * @param frame Frame number contained in zone.
179
 * @param count Number of frames to look for.
180
 * @param hint  Used as zone hint.
181
 *
182
 * @return Zone index or -1 if not found.
183
 *
184
 */
185
count_t find_zone(pfn_t frame, count_t count, count_t hint)
186
{
2745 decky 187
    if (hint >= zones.count)
814 palkovsky 188
        hint = 0;
533 bondari 189
 
4344 svoboda 190
    count_t i = hint;
814 palkovsky 191
    do {
4344 svoboda 192
        if ((zones.info[i].base <= frame)
193
            && (zones.info[i].base + zones.info[i].count >= frame + count))
194
            return i;
195
 
814 palkovsky 196
        i++;
197
        if (i >= zones.count)
198
            i = 0;
3211 svoboda 199
    } while (i != hint);
4344 svoboda 200
 
201
    return (count_t) -1;
533 bondari 202
}
203
 
822 palkovsky 204
/** @return True if zone can allocate specified order */
4344 svoboda 205
static bool zone_can_alloc(zone_t *zone, uint8_t order)
822 palkovsky 206
{
4344 svoboda 207
    return (zone_flags_available(zone->flags)
208
        && buddy_system_can_alloc(zone->buddy_system, order));
822 palkovsky 209
}
210
 
4344 svoboda 211
/** Find a zone that can allocate order frames.
367 jermar 212
 *
4344 svoboda 213
 * Assume interrupts are disabled and zones lock is
214
 * locked.
367 jermar 215
 *
4344 svoboda 216
 * @param order Size (2^order) of free space we are trying to find.
217
 * @param flags Required flags of the target zone.
218
 * @param hind  Preferred zone.
219
 *
367 jermar 220
 */
4344 svoboda 221
static count_t find_free_zone(uint8_t order, zone_flags_t flags, count_t hint)
367 jermar 222
{
814 palkovsky 223
    if (hint >= zones.count)
224
        hint = 0;
4344 svoboda 225
 
226
    count_t i = hint;
814 palkovsky 227
    do {
3211 svoboda 228
        /*
229
         * Check whether the zone meets the search criteria.
230
         */
4344 svoboda 231
        if ((zones.info[i].flags & flags) == flags) {
3211 svoboda 232
            /*
233
             * Check if the zone has 2^order frames area available.
234
             */
4344 svoboda 235
            if (zone_can_alloc(&zones.info[i], order))
236
                return i;
367 jermar 237
        }
4344 svoboda 238
 
239
        i++;
240
        if (i >= zones.count)
814 palkovsky 241
            i = 0;
3211 svoboda 242
    } while (i != hint);
4344 svoboda 243
 
244
    return (count_t) -1;
367 jermar 245
}
246
 
2059 jermar 247
/**************************/
814 palkovsky 248
/* Buddy system functions */
2059 jermar 249
/**************************/
814 palkovsky 250
 
3191 svoboda 251
/** Buddy system find_block implementation.
367 jermar 252
 *
814 palkovsky 253
 * Find block that is parent of current list.
254
 * That means go to lower addresses, until such block is found
367 jermar 255
 *
4344 svoboda 256
 * @param order Order of parent must be different then this
257
 *              parameter!!
258
 *
367 jermar 259
 */
4344 svoboda 260
static link_t *zone_buddy_find_block(buddy_system_t *buddy, link_t *child,
2133 jermar 261
    uint8_t order)
367 jermar 262
{
4344 svoboda 263
    frame_t *frame = list_get_instance(child, frame_t, buddy_link);
264
    zone_t *zone = (zone_t *) buddy->data;
367 jermar 265
 
4344 svoboda 266
    index_t index = frame_index(zone, frame);
814 palkovsky 267
    do {
4344 svoboda 268
        if (zone->frames[index].buddy_order != order)
814 palkovsky 269
            return &zone->frames[index].buddy_link;
4344 svoboda 270
    } while (index-- > 0);
271
 
814 palkovsky 272
    return NULL;
367 jermar 273
}
479 bondari 274
 
3191 svoboda 275
/** Buddy system find_buddy implementation.
489 jermar 276
 *
4344 svoboda 277
 * @param buddy Buddy system.
278
 * @param block Block for which buddy should be found.
479 bondari 279
 *
4344 svoboda 280
 * @return Buddy for given block if found.
281
 *
479 bondari 282
 */
4344 svoboda 283
static link_t *zone_buddy_find_buddy(buddy_system_t *buddy, link_t *block)
814 palkovsky 284
{
4344 svoboda 285
    frame_t *frame = list_get_instance(block, frame_t, buddy_link);
286
    zone_t *zone = (zone_t *) buddy->data;
2133 jermar 287
    ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame),
288
        frame->buddy_order));
480 bondari 289
 
4344 svoboda 290
    bool is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
291
    bool is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
292
 
564 jermar 293
    ASSERT(is_left ^ is_right);
4344 svoboda 294
 
295
    index_t index;
533 bondari 296
    if (is_left) {
3191 svoboda 297
        index = (frame_index(zone, frame)) +
298
            (1 << frame->buddy_order);
4344 svoboda 299
    } else {  /* if (is_right) */
3191 svoboda 300
        index = (frame_index(zone, frame)) -
301
            (1 << frame->buddy_order);
533 bondari 302
    }
303
 
814 palkovsky 304
    if (frame_index_valid(zone, index)) {
4344 svoboda 305
        if ((zone->frames[index].buddy_order == frame->buddy_order) &&
306
            (zone->frames[index].refcount == 0)) {
533 bondari 307
            return &zone->frames[index].buddy_link;
480 bondari 308
        }
309
    }
4344 svoboda 310
 
311
    return NULL;
479 bondari 312
}
313
 
3191 svoboda 314
/** Buddy system bisect implementation.
479 bondari 315
 *
4344 svoboda 316
 * @param buddy Buddy system.
317
 * @param block Block to bisect.
480 bondari 318
 *
4344 svoboda 319
 * @return Right block.
320
 *
479 bondari 321
 */
4344 svoboda 322
static link_t *zone_buddy_bisect(buddy_system_t *buddy, link_t *block)
3191 svoboda 323
{
4344 svoboda 324
    frame_t *frame_l = list_get_instance(block, frame_t, buddy_link);
325
    frame_t *frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
564 jermar 326
 
480 bondari 327
    return &frame_r->buddy_link;
479 bondari 328
}
329
 
3191 svoboda 330
/** Buddy system coalesce implementation.
479 bondari 331
 *
4344 svoboda 332
 * @param buddy   Buddy system.
333
 * @param block_1 First block.
334
 * @param block_2 First block's buddy.
480 bondari 335
 *
4344 svoboda 336
 * @return Coalesced block (actually block that represents lower
337
 *         address).
338
 *
479 bondari 339
 */
4344 svoboda 340
static link_t *zone_buddy_coalesce(buddy_system_t *buddy, link_t *block_1,
341
    link_t *block_2)
822 palkovsky 342
{
4344 svoboda 343
    frame_t *frame1 = list_get_instance(block_1, frame_t, buddy_link);
344
    frame_t *frame2 = list_get_instance(block_2, frame_t, buddy_link);
564 jermar 345
 
4344 svoboda 346
    return ((frame1 < frame2) ? block_1 : block_2);
479 bondari 347
}
348
 
3191 svoboda 349
/** Buddy system set_order implementation.
489 jermar 350
 *
4344 svoboda 351
 * @param buddy Buddy system.
352
 * @param block Buddy system block.
353
 * @param order Order to set.
354
 *
479 bondari 355
 */
4344 svoboda 356
static void zone_buddy_set_order(buddy_system_t *buddy, link_t *block,
3191 svoboda 357
    uint8_t order)
358
{
4344 svoboda 359
    list_get_instance(block, frame_t, buddy_link)->buddy_order = order;
479 bondari 360
}
361
 
3191 svoboda 362
/** Buddy system get_order implementation.
489 jermar 363
 *
4344 svoboda 364
 * @param buddy Buddy system.
365
 * @param block Buddy system block.
479 bondari 366
 *
4344 svoboda 367
 * @return Order of block.
368
 *
479 bondari 369
 */
4344 svoboda 370
static uint8_t zone_buddy_get_order(buddy_system_t *buddy, link_t *block)
3191 svoboda 371
{
4344 svoboda 372
    return list_get_instance(block, frame_t, buddy_link)->buddy_order;
479 bondari 373
}
533 bondari 374
 
3191 svoboda 375
/** Buddy system mark_busy implementation.
533 bondari 376
 *
4344 svoboda 377
 * @param buddy Buddy system.
378
 * @param block Buddy system block.
379
 *
533 bondari 380
 */
4344 svoboda 381
static void zone_buddy_mark_busy(buddy_system_t *buddy, link_t * block)
3191 svoboda 382
{
4344 svoboda 383
    list_get_instance(block, frame_t, buddy_link)->refcount = 1;
533 bondari 384
}
676 bondari 385
 
3191 svoboda 386
/** Buddy system mark_available implementation.
814 palkovsky 387
 *
4344 svoboda 388
 * @param buddy Buddy system.
389
 * @param block Buddy system block.
814 palkovsky 390
 */
4344 svoboda 391
static void zone_buddy_mark_available(buddy_system_t *buddy, link_t *block)
3191 svoboda 392
{
4344 svoboda 393
    list_get_instance(block, frame_t, buddy_link)->refcount = 0;
814 palkovsky 394
}
395
 
2083 decky 396
static buddy_system_operations_t zone_buddy_system_operations = {
814 palkovsky 397
    .find_buddy = zone_buddy_find_buddy,
398
    .bisect = zone_buddy_bisect,
399
    .coalesce = zone_buddy_coalesce,
400
    .set_order = zone_buddy_set_order,
401
    .get_order = zone_buddy_get_order,
402
    .mark_busy = zone_buddy_mark_busy,
403
    .mark_available = zone_buddy_mark_available,
3211 svoboda 404
    .find_block = zone_buddy_find_block
814 palkovsky 405
};
406
 
2059 jermar 407
/******************/
814 palkovsky 408
/* Zone functions */
2059 jermar 409
/******************/
814 palkovsky 410
 
3191 svoboda 411
/** Allocate frame in particular zone.
814 palkovsky 412
 *
4344 svoboda 413
 * Assume zone is locked and is available for allocation.
1269 decky 414
 * Panics if allocation is impossible.
814 palkovsky 415
 *
4344 svoboda 416
 * @param zone  Zone to allocate from.
417
 * @param order Allocate exactly 2^order frames.
1269 decky 418
 *
4344 svoboda 419
 * @return Frame index in zone.
1269 decky 420
 *
814 palkovsky 421
 */
1780 jermar 422
static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
814 palkovsky 423
{
4344 svoboda 424
    ASSERT(zone_flags_available(zone->flags));
425
 
814 palkovsky 426
    /* Allocate frames from zone buddy system */
4344 svoboda 427
    link_t *link = buddy_system_alloc(zone->buddy_system, order);
814 palkovsky 428
 
4344 svoboda 429
    ASSERT(link);
814 palkovsky 430
 
431
    /* Update zone information. */
432
    zone->free_count -= (1 << order);
433
    zone->busy_count += (1 << order);
4344 svoboda 434
 
814 palkovsky 435
    /* Frame will be actually a first frame of the block. */
4344 svoboda 436
    frame_t *frame = list_get_instance(link, frame_t, buddy_link);
814 palkovsky 437
 
4344 svoboda 438
    /* Get frame address */
439
    return make_frame_index(zone, frame);
814 palkovsky 440
}
441
 
3191 svoboda 442
/** Free frame from zone.
814 palkovsky 443
 *
4344 svoboda 444
 * Assume zone is locked and is available for deallocation.
1568 palkovsky 445
 *
4344 svoboda 446
 * @param zone      Pointer to zone from which the frame is to be freed.
447
 * @param frame_idx Frame index relative to zone.
448
 *
814 palkovsky 449
 */
820 jermar 450
static void zone_frame_free(zone_t *zone, index_t frame_idx)
814 palkovsky 451
{
4344 svoboda 452
    ASSERT(zone_flags_available(zone->flags));
814 palkovsky 453
 
4344 svoboda 454
    frame_t *frame = &zone->frames[frame_idx];
455
 
456
    /* Remember frame order */
457
    uint8_t order = frame->buddy_order;
458
 
814 palkovsky 459
    ASSERT(frame->refcount);
4344 svoboda 460
 
814 palkovsky 461
    if (!--frame->refcount) {
462
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
4344 svoboda 463
 
946 jermar 464
        /* Update zone information. */
465
        zone->free_count += (1 << order);
466
        zone->busy_count -= (1 << order);
814 palkovsky 467
    }
468
}
469
 
3191 svoboda 470
/** Return frame from zone. */
471
static frame_t *zone_get_frame(zone_t *zone, index_t frame_idx)
814 palkovsky 472
{
473
    ASSERT(frame_idx < zone->count);
474
    return &zone->frames[frame_idx];
475
}
476
 
3191 svoboda 477
/** Mark frame in zone unavailable to allocation. */
820 jermar 478
static void zone_mark_unavailable(zone_t *zone, index_t frame_idx)
814 palkovsky 479
{
4344 svoboda 480
    ASSERT(zone_flags_available(zone->flags));
481
 
482
    frame_t *frame = zone_get_frame(zone, frame_idx);
822 palkovsky 483
    if (frame->refcount)
484
        return;
4344 svoboda 485
 
486
    link_t *link = buddy_system_alloc_block(zone->buddy_system,
2133 jermar 487
        &frame->buddy_link);
4344 svoboda 488
 
814 palkovsky 489
    ASSERT(link);
490
    zone->free_count--;
491
}
492
 
4344 svoboda 493
/** Merge two zones.
822 palkovsky 494
 *
4344 svoboda 495
 * Expect buddy to point to space at least zone_conf_size large.
496
 * Assume z1 & z2 are locked and compatible and zones lock is
497
 * locked.
822 palkovsky 498
 *
4344 svoboda 499
 * @param z1     First zone to merge.
500
 * @param z2     Second zone to merge.
501
 * @param old_z1 Original date of the first zone.
502
 * @param buddy  Merged zone buddy.
1568 palkovsky 503
 *
822 palkovsky 504
 */
4344 svoboda 505
static void zone_merge_internal(count_t z1, count_t z2, zone_t *old_z1, buddy_system_t *buddy)
822 palkovsky 506
{
4344 svoboda 507
    ASSERT(zone_flags_available(zones.info[z1].flags));
508
    ASSERT(zone_flags_available(zones.info[z2].flags));
509
    ASSERT(zones.info[z1].flags == zones.info[z2].flags);
510
    ASSERT(zones.info[z1].base < zones.info[z2].base);
511
    ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
512
        zones.info[z2].base, zones.info[z2].count));
822 palkovsky 513
 
4344 svoboda 514
    /* Difference between zone bases */
515
    pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
516
 
517
    zones.info[z1].count = base_diff + zones.info[z2].count;
518
    zones.info[z1].free_count += zones.info[z2].free_count;
519
    zones.info[z1].busy_count += zones.info[z2].busy_count;
520
    zones.info[z1].buddy_system = buddy;
521
 
522
    uint8_t order = fnzb(zones.info[z1].count);
523
    buddy_system_create(zones.info[z1].buddy_system, order,
524
        &zone_buddy_system_operations, (void *) &zones.info[z1]);
525
 
526
    zones.info[z1].frames =
527
        (frame_t *) ((uint8_t *) zones.info[z1].buddy_system
528
        + buddy_conf_size(order));
529
 
530
    /* This marks all frames busy */
531
    count_t i;
532
    for (i = 0; i < zones.info[z1].count; i++)
533
        frame_initialize(&zones.info[z1].frames[i]);
534
 
822 palkovsky 535
    /* Copy frames from both zones to preserve full frame orders,
4344 svoboda 536
     * parents etc. Set all free frames with refcount = 0 to 1, because
537
     * we add all free frames to buddy allocator later again, clearing
538
     * order to 0. Don't set busy frames with refcount = 0, as they
1093 palkovsky 539
     * will not be reallocated during merge and it would make later
540
     * problems with allocation/free.
822 palkovsky 541
     */
4344 svoboda 542
    for (i = 0; i < old_z1->count; i++)
543
        zones.info[z1].frames[i] = old_z1->frames[i];
544
 
545
    for (i = 0; i < zones.info[z2].count; i++)
546
        zones.info[z1].frames[base_diff + i]
547
            = zones.info[z2].frames[i];
548
 
1093 palkovsky 549
    i = 0;
4344 svoboda 550
    while (i < zones.info[z1].count) {
551
        if (zones.info[z1].frames[i].refcount) {
552
            /* Skip busy frames */
553
            i += 1 << zones.info[z1].frames[i].buddy_order;
554
        } else {
555
            /* Free frames, set refcount = 1
556
             * (all free frames have refcount == 0, we need not
557
             * to check the order)
558
             */
559
            zones.info[z1].frames[i].refcount = 1;
560
            zones.info[z1].frames[i].buddy_order = 0;
1093 palkovsky 561
            i++;
822 palkovsky 562
        }
563
    }
4344 svoboda 564
 
565
    /* Add free blocks from the original zone z1 */
566
    while (zone_can_alloc(old_z1, 0)) {
567
        /* Allocate from the original zone */
568
        pfn_t frame_idx = zone_frame_alloc(old_z1, 0);
569
 
570
        /* Free the frame from the merged zone */
571
        frame_t *frame = &zones.info[z1].frames[frame_idx];
822 palkovsky 572
        frame->refcount = 0;
4344 svoboda 573
        buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
822 palkovsky 574
    }
4344 svoboda 575
 
576
    /* Add free blocks from the original zone z2 */
577
    while (zone_can_alloc(&zones.info[z2], 0)) {
578
        /* Allocate from the original zone */
579
        pfn_t frame_idx = zone_frame_alloc(&zones.info[z2], 0);
580
 
581
        /* Free the frame from the merged zone */
582
        frame_t *frame = &zones.info[z1].frames[base_diff + frame_idx];
822 palkovsky 583
        frame->refcount = 0;
4344 svoboda 584
        buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
822 palkovsky 585
    }
586
}
587
 
3191 svoboda 588
/** Return old configuration frames into the zone.
822 palkovsky 589
 *
4344 svoboda 590
 * We have two cases:
591
 * - The configuration data is outside the zone
592
 *   -> do nothing (perhaps call frame_free?)
593
 * - The configuration data was created by zone_create
594
 *   or updated by reduce_region -> free every frame
824 palkovsky 595
 *
4344 svoboda 596
 * @param znum  The actual zone where freeing should occur.
597
 * @param pfn   Old zone configuration frame.
598
 * @param count Old zone frame count.
599
 *
822 palkovsky 600
 */
4344 svoboda 601
static void return_config_frames(count_t znum, pfn_t pfn, count_t count)
822 palkovsky 602
{
4344 svoboda 603
    ASSERT(zone_flags_available(zones.info[znum].flags));
822 palkovsky 604
 
4344 svoboda 605
    count_t cframes = SIZE2FRAMES(zone_conf_size(count));
606
 
607
    if ((pfn < zones.info[znum].base)
608
        || (pfn >= zones.info[znum].base + zones.info[znum].count))
822 palkovsky 609
        return;
4344 svoboda 610
 
611
    frame_t *frame
612
        = &zones.info[znum].frames[pfn - zones.info[znum].base];
824 palkovsky 613
    ASSERT(!frame->buddy_order);
4344 svoboda 614
 
615
    count_t i;
2122 decky 616
    for (i = 0; i < cframes; i++) {
4344 svoboda 617
        zones.info[znum].busy_count++;
618
        zone_frame_free(&zones.info[znum],
619
            pfn - zones.info[znum].base + i);
822 palkovsky 620
    }
621
}
622
 
3191 svoboda 623
/** Reduce allocated block to count of order 0 frames.
824 palkovsky 624
 *
4344 svoboda 625
 * The allocated block needs 2^order frames. Reduce all frames
626
 * in the block to order 0 and free the unneeded frames. This means that
627
 * when freeing the previously allocated block starting with frame_idx,
1568 palkovsky 628
 * you have to free every frame.
824 palkovsky 629
 *
4344 svoboda 630
 * @param znum      Zone.
631
 * @param frame_idx Index the first frame of the block.
632
 * @param count     Allocated frames in block.
633
 *
824 palkovsky 634
 */
4344 svoboda 635
static void zone_reduce_region(count_t znum, pfn_t frame_idx, count_t count)
824 palkovsky 636
{
4344 svoboda 637
    ASSERT(zone_flags_available(zones.info[znum].flags));
638
    ASSERT(frame_idx + count < zones.info[znum].count);
824 palkovsky 639
 
4344 svoboda 640
    uint8_t order = zones.info[znum].frames[frame_idx].buddy_order;
2122 decky 641
    ASSERT((count_t) (1 << order) >= count);
4344 svoboda 642
 
824 palkovsky 643
    /* Reduce all blocks to order 0 */
4344 svoboda 644
    count_t i;
2122 decky 645
    for (i = 0; i < (count_t) (1 << order); i++) {
4344 svoboda 646
        frame_t *frame = &zones.info[znum].frames[i + frame_idx];
824 palkovsky 647
        frame->buddy_order = 0;
2133 jermar 648
        if (!frame->refcount)
824 palkovsky 649
            frame->refcount = 1;
650
        ASSERT(frame->refcount == 1);
651
    }
4344 svoboda 652
 
824 palkovsky 653
    /* Free unneeded frames */
4344 svoboda 654
    for (i = count; i < (count_t) (1 << order); i++)
655
        zone_frame_free(&zones.info[znum], i + frame_idx);
824 palkovsky 656
}
657
 
3191 svoboda 658
/** Merge zones z1 and z2.
822 palkovsky 659
 *
4344 svoboda 660
 * The merged zones must be 2 zones with no zone existing in between
661
 * (which means that z2 = z1 + 1). Both zones must be available zones
662
 * with the same flags.
822 palkovsky 663
 *
4344 svoboda 664
 * When you create a new zone, the frame allocator configuration does
665
 * not to be 2^order size. Once the allocator is running it is no longer
666
 * possible, merged configuration data occupies more space :-/
667
 *
668
 * The function uses
669
 *
822 palkovsky 670
 */
4344 svoboda 671
bool zone_merge(count_t z1, count_t z2)
822 palkovsky 672
{
4344 svoboda 673
    ipl_t ipl = interrupts_disable();
822 palkovsky 674
    spinlock_lock(&zones.lock);
4344 svoboda 675
 
676
    bool ret = true;
677
 
678
    /* We can join only 2 zones with none existing inbetween,
679
     * the zones have to be available and with the same
680
     * set of flags
681
     */
682
    if ((z1 >= zones.count) || (z2 >= zones.count)
683
        || (z2 - z1 != 1)
684
        || (!zone_flags_available(zones.info[z1].flags))
685
        || (!zone_flags_available(zones.info[z2].flags))
686
        || (zones.info[z1].flags != zones.info[z2].flags)) {
687
        ret = false;
822 palkovsky 688
        goto errout;
4344 svoboda 689
    }
690
 
691
    pfn_t cframes = SIZE2FRAMES(zone_conf_size(
692
        zones.info[z2].base - zones.info[z1].base
693
        + zones.info[z2].count));
694
 
695
    uint8_t order;
1700 palkovsky 696
    if (cframes == 1)
697
        order = 0;
4344 svoboda 698
    else
1700 palkovsky 699
        order = fnzb(cframes - 1) + 1;
4344 svoboda 700
 
701
    /* Allocate merged zone data inside one of the zones */
702
    pfn_t pfn;
703
    if (zone_can_alloc(&zones.info[z1], order)) {
704
        pfn = zones.info[z1].base + zone_frame_alloc(&zones.info[z1], order);
705
    } else if (zone_can_alloc(&zones.info[z2], order)) {
706
        pfn = zones.info[z2].base + zone_frame_alloc(&zones.info[z2], order);
707
    } else {
708
        ret = false;
709
        goto errout;
710
    }
711
 
712
    /* Preserve original data from z1 */
713
    zone_t old_z1 = zones.info[z1];
714
    old_z1.buddy_system->data = (void *) &old_z1;
715
 
716
    /* Do zone merging */
717
    buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(pfn));
718
    zone_merge_internal(z1, z2, &old_z1, buddy);
719
 
824 palkovsky 720
    /* Free unneeded config frames */
4344 svoboda 721
    zone_reduce_region(z1, pfn - zones.info[z1].base, cframes);
722
 
822 palkovsky 723
    /* Subtract zone information from busy frames */
4344 svoboda 724
    zones.info[z1].busy_count -= cframes;
725
 
726
    /* Free old zone information */
727
    return_config_frames(z1,
728
        ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
729
    return_config_frames(z1,
730
        ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
731
        zones.info[z2].count);
732
 
733
    /* Move zones down */
734
    count_t i;
735
    for (i = z2 + 1; i < zones.count; i++) {
1037 decky 736
        zones.info[i - 1] = zones.info[i];
4344 svoboda 737
        zones.info[i - 1].buddy_system->data =
738
            (void *) &zones.info[i - 1];
739
    }
740
 
822 palkovsky 741
    zones.count--;
4344 svoboda 742
 
822 palkovsky 743
errout:
744
    spinlock_unlock(&zones.lock);
745
    interrupts_restore(ipl);
4344 svoboda 746
 
747
    return ret;
822 palkovsky 748
}
749
 
4344 svoboda 750
/** Merge all mergeable zones into one big zone.
822 palkovsky 751
 *
4344 svoboda 752
 * It is reasonable to do this on systems where
753
 * BIOS reports parts in chunks, so that we could
754
 * have 1 zone (it's faster).
755
 *
822 palkovsky 756
 */
757
void zone_merge_all(void)
758
{
4344 svoboda 759
    count_t i = 0;
760
    while (i < zones.count) {
761
        if (!zone_merge(i, i + 1))
762
            i++;
822 palkovsky 763
    }
764
}
765
 
3191 svoboda 766
/** Create new frame zone.
814 palkovsky 767
 *
4344 svoboda 768
 * @param zone  Zone to construct.
769
 * @param buddy Address of buddy system configuration information.
770
 * @param start Physical address of the first frame within the zone.
771
 * @param count Count of frames in zone.
772
 * @param flags Zone flags.
814 palkovsky 773
 *
4344 svoboda 774
 * @return Initialized zone.
775
 *
814 palkovsky 776
 */
4344 svoboda 777
static void zone_construct(zone_t *zone, buddy_system_t *buddy, pfn_t start, count_t count, zone_flags_t flags)
814 palkovsky 778
{
4344 svoboda 779
    zone->base = start;
780
    zone->count = count;
781
    zone->flags = flags;
782
    zone->free_count = count;
783
    zone->busy_count = 0;
784
    zone->buddy_system = buddy;
814 palkovsky 785
 
4344 svoboda 786
    if (zone_flags_available(flags)) {
787
        /*
788
         * Compute order for buddy system and initialize
789
         */
790
        uint8_t order = fnzb(count);
791
        buddy_system_create(zone->buddy_system, order,
792
            &zone_buddy_system_operations, (void *) zone);
793
 
794
        /* Allocate frames _after_ the confframe */
795
 
796
        /* Check sizes */
797
        zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
798
            buddy_conf_size(order));
799
 
800
        count_t i;
801
        for (i = 0; i < count; i++)
802
            frame_initialize(&zone->frames[i]);
803
 
804
        /* Stuffing frames */
805
        for (i = 0; i < count; i++) {
806
            zone->frames[i].refcount = 0;
807
            buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
808
        }
809
    } else
810
        zone->frames = NULL;
814 palkovsky 811
}
812
 
3191 svoboda 813
/** Compute configuration data size for zone.
1568 palkovsky 814
 *
4344 svoboda 815
 * @param count Size of zone in frames.
816
 *
817
 * @return Size of zone configuration info (in bytes).
818
 *
1568 palkovsky 819
 */
1780 jermar 820
uintptr_t zone_conf_size(count_t count)
814 palkovsky 821
{
4344 svoboda 822
    return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
814 palkovsky 823
}
824
 
3191 svoboda 825
/** Create and add zone to system.
814 palkovsky 826
 *
4344 svoboda 827
 * @param start     First frame number (absolute).
828
 * @param count     Size of zone in frames.
829
 * @param confframe Where configuration frames are supposed to be.
830
 *                  Automatically checks, that we will not disturb the
831
 *                  kernel and possibly init. If confframe is given
832
 *                  _outside_ this zone, it is expected, that the area is
833
 *                  already marked BUSY and big enough to contain
834
 *                  zone_conf_size() amount of data. If the confframe is
835
 *                  inside the area, the zone free frame information is
836
 *                  modified not to include it.
822 palkovsky 837
 *
4344 svoboda 838
 * @return Zone number or -1 on error.
839
 *
814 palkovsky 840
 */
4344 svoboda 841
count_t zone_create(pfn_t start, count_t count, pfn_t confframe, zone_flags_t flags)
814 palkovsky 842
{
4344 svoboda 843
    ipl_t ipl = interrupts_disable();
844
    spinlock_lock(&zones.lock);
845
 
846
    if (zone_flags_available(flags)) {  /* Create available zone */
847
        /* Theoretically we could have NULL here, practically make sure
848
         * nobody tries to do that. If some platform requires, remove
849
         * the assert
850
         */
851
        ASSERT(confframe != NULL);
852
 
853
        /* If confframe is supposed to be inside our zone, then make sure
854
         * it does not span kernel & init
855
         */
856
        count_t confcount = SIZE2FRAMES(zone_conf_size(count));
857
        if ((confframe >= start) && (confframe < start + count)) {
858
            for (; confframe < start + count; confframe++) {
859
                uintptr_t addr = PFN2ADDR(confframe);
2133 jermar 860
                if (overlaps(addr, PFN2ADDR(confcount),
4344 svoboda 861
                    KA2PA(config.base), config.kernel_size))
862
                    continue;
863
 
864
                if (overlaps(addr, PFN2ADDR(confcount),
865
                    KA2PA(config.stack_base), config.stack_size))
866
                    continue;
867
 
868
                bool overlap = false;
869
                count_t i;
870
                for (i = 0; i < init.cnt; i++)
871
                    if (overlaps(addr, PFN2ADDR(confcount),
872
                        KA2PA(init.tasks[i].addr),
873
                        init.tasks[i].size)) {
874
                        overlap = true;
875
                        break;
876
                    }
877
                if (overlap)
878
                    continue;
879
 
880
                break;
881
            }
1037 decky 882
 
4344 svoboda 883
            if (confframe >= start + count)
884
                panic("Cannot find configuration data for zone.");
814 palkovsky 885
        }
4344 svoboda 886
 
887
        count_t znum = zones_insert_zone(start, count);
888
        if (znum == (count_t) -1) {
889
            spinlock_unlock(&zones.lock);
890
            interrupts_restore(ipl);
891
            return (count_t) -1;
892
        }
893
 
894
        buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
895
        zone_construct(&zones.info[znum], buddy, start, count, flags);
896
 
897
        /* If confdata in zone, mark as unavailable */
898
        if ((confframe >= start) && (confframe < start + count)) {
899
            count_t i;
900
            for (i = confframe; i < confframe + confcount; i++)
901
                zone_mark_unavailable(&zones.info[znum],
902
                    i - zones.info[znum].base);
903
        }
904
 
905
        spinlock_unlock(&zones.lock);
906
        interrupts_restore(ipl);
907
 
908
        return znum;
814 palkovsky 909
    }
3191 svoboda 910
 
4344 svoboda 911
    /* Non-available zone */
912
    count_t znum = zones_insert_zone(start, count);
913
    if (znum == (count_t) -1) {
914
        spinlock_unlock(&zones.lock);
915
        interrupts_restore(ipl);
916
        return (count_t) -1;
917
    }
918
    zone_construct(&zones.info[znum], NULL, start, count, flags);
919
 
920
    spinlock_unlock(&zones.lock);
921
    interrupts_restore(ipl);
922
 
822 palkovsky 923
    return znum;
814 palkovsky 924
}
925
 
4344 svoboda 926
/*******************/
814 palkovsky 927
/* Frame functions */
4344 svoboda 928
/*******************/
814 palkovsky 929
 
3191 svoboda 930
/** Set parent of frame. */
4344 svoboda 931
void frame_set_parent(pfn_t pfn, void *data, count_t hint)
814 palkovsky 932
{
4344 svoboda 933
    ipl_t ipl = interrupts_disable();
934
    spinlock_lock(&zones.lock);
935
 
936
    count_t znum = find_zone(pfn, 1, hint);
937
 
938
    ASSERT(znum != (count_t) -1);
939
 
940
    zone_get_frame(&zones.info[znum],
941
        pfn - zones.info[znum].base)->parent = data;
942
 
943
    spinlock_unlock(&zones.lock);
944
    interrupts_restore(ipl);
814 palkovsky 945
}
946
 
4344 svoboda 947
void *frame_get_parent(pfn_t pfn, count_t hint)
814 palkovsky 948
{
4344 svoboda 949
    ipl_t ipl = interrupts_disable();
950
    spinlock_lock(&zones.lock);
814 palkovsky 951
 
4344 svoboda 952
    count_t znum = find_zone(pfn, 1, hint);
953
 
954
    ASSERT(znum != (count_t) -1);
955
 
956
    void *res = zone_get_frame(&zones.info[znum],
957
        pfn - zones.info[znum].base)->parent;
958
 
959
    spinlock_unlock(&zones.lock);
960
    interrupts_restore(ipl);
961
 
814 palkovsky 962
    return res;
963
}
964
 
965
/** Allocate power-of-two frames of physical memory.
966
 *
4344 svoboda 967
 * @param order Allocate exactly 2^order frames.
968
 * @param flags Flags for host zone selection and address processing.
969
 * @param pzone Preferred zone.
814 palkovsky 970
 *
4344 svoboda 971
 * @return Physical address of the allocated frame.
1269 decky 972
 *
814 palkovsky 973
 */
4344 svoboda 974
void *frame_alloc_generic(uint8_t order, frame_flags_t flags, count_t *pzone)
814 palkovsky 975
{
4344 svoboda 976
    count_t size = ((count_t) 1) << order;
814 palkovsky 977
    ipl_t ipl;
4344 svoboda 978
    count_t hint = pzone ? (*pzone) : 0;
814 palkovsky 979
 
980
loop:
981
    ipl = interrupts_disable();
4344 svoboda 982
    spinlock_lock(&zones.lock);
1269 decky 983
 
814 palkovsky 984
    /*
985
     * First, find suitable frame zone.
986
     */
4344 svoboda 987
    count_t znum = find_free_zone(order,
988
        FRAME_TO_ZONE_FLAGS(flags), hint);
1269 decky 989
 
814 palkovsky 990
    /* If no memory, reclaim some slab memory,
991
       if it does not help, reclaim all */
4344 svoboda 992
    if ((znum == (count_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
993
        count_t freed = slab_reclaim(0);
994
 
995
        if (freed > 0)
996
            znum = find_free_zone(order,
997
                FRAME_TO_ZONE_FLAGS(flags), hint);
998
 
999
        if (znum == (count_t) -1) {
814 palkovsky 1000
            freed = slab_reclaim(SLAB_RECLAIM_ALL);
4344 svoboda 1001
            if (freed > 0)
1002
                znum = find_free_zone(order,
1003
                    FRAME_TO_ZONE_FLAGS(flags), hint);
814 palkovsky 1004
        }
1005
    }
4344 svoboda 1006
 
1007
    if (znum == (count_t) -1) {
3191 svoboda 1008
        if (flags & FRAME_ATOMIC) {
4344 svoboda 1009
            spinlock_unlock(&zones.lock);
3191 svoboda 1010
            interrupts_restore(ipl);
4344 svoboda 1011
            return NULL;
3191 svoboda 1012
        }
814 palkovsky 1013
 
3191 svoboda 1014
#ifdef CONFIG_DEBUG
4344 svoboda 1015
        count_t avail = total_frames_free();
3191 svoboda 1016
#endif
4344 svoboda 1017
 
1018
        spinlock_unlock(&zones.lock);
1019
        interrupts_restore(ipl);
1020
 
1021
        /*
1022
         * Sleep until some frames are available again.
1023
         */
1024
 
1025
#ifdef CONFIG_DEBUG
1026
        printf("Thread %" PRIu64 " waiting for %" PRIc " frames, "
1027
            "%" PRIc " available.\n", THREAD->tid, size, avail);
1028
#endif
1029
 
3191 svoboda 1030
        mutex_lock(&mem_avail_mtx);
4344 svoboda 1031
 
1032
        if (mem_avail_req > 0)
1033
            mem_avail_req = min(mem_avail_req, size);
1034
        else
1035
            mem_avail_req = size;
1036
        count_t gen = mem_avail_gen;
1037
 
1038
        while (gen == mem_avail_gen)
3191 svoboda 1039
            condvar_wait(&mem_avail_cv, &mem_avail_mtx);
4344 svoboda 1040
 
3191 svoboda 1041
        mutex_unlock(&mem_avail_mtx);
4344 svoboda 1042
 
3191 svoboda 1043
#ifdef CONFIG_DEBUG
4344 svoboda 1044
        printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
3191 svoboda 1045
#endif
4344 svoboda 1046
 
814 palkovsky 1047
        goto loop;
1048
    }
1269 decky 1049
 
4344 svoboda 1050
    pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
1051
        + zones.info[znum].base;
3191 svoboda 1052
 
4344 svoboda 1053
    spinlock_unlock(&zones.lock);
814 palkovsky 1054
    interrupts_restore(ipl);
4344 svoboda 1055
 
1056
    if (pzone)
1057
        *pzone = znum;
1058
 
1760 palkovsky 1059
    if (flags & FRAME_KA)
4344 svoboda 1060
        return (void *) PA2KA(PFN2ADDR(pfn));
1061
 
1062
    return (void *) PFN2ADDR(pfn);
814 palkovsky 1063
}
1064
 
1065
/** Free a frame.
1066
 *
1854 jermar 1067
 * Find respective frame structure for supplied physical frame address.
4344 svoboda 1068
 * Decrement frame reference count. If it drops to zero, move the frame
1069
 * structure to free list.
814 palkovsky 1070
 *
4344 svoboda 1071
 * @param frame Physical Address of of the frame to be freed.
1072
 *
814 palkovsky 1073
 */
1780 jermar 1074
void frame_free(uintptr_t frame)
814 palkovsky 1075
{
4344 svoboda 1076
    ipl_t ipl = interrupts_disable();
1077
    spinlock_lock(&zones.lock);
1078
 
814 palkovsky 1079
    /*
1080
     * First, find host frame zone for addr.
1081
     */
4344 svoboda 1082
    pfn_t pfn = ADDR2PFN(frame);
1083
    count_t znum = find_zone(pfn, 1, NULL);
814 palkovsky 1084
 
4344 svoboda 1085
    ASSERT(znum != (count_t) -1);
814 palkovsky 1086
 
4344 svoboda 1087
    zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
3191 svoboda 1088
 
4344 svoboda 1089
    spinlock_unlock(&zones.lock);
1090
    interrupts_restore(ipl);
1091
 
3191 svoboda 1092
    /*
1093
     * Signal that some memory has been freed.
1094
     */
1095
    mutex_lock(&mem_avail_mtx);
4344 svoboda 1096
    if (mem_avail_req > 0)
1097
        mem_avail_req--;
1098
 
1099
    if (mem_avail_req == 0) {
1100
        mem_avail_gen++;
1101
        condvar_broadcast(&mem_avail_cv);
1102
    }
3191 svoboda 1103
    mutex_unlock(&mem_avail_mtx);
814 palkovsky 1104
}
1105
 
1236 jermar 1106
/** Add reference to frame.
1107
 *
1108
 * Find respective frame structure for supplied PFN and
1109
 * increment frame reference count.
1110
 *
4344 svoboda 1111
 * @param pfn Frame number of the frame to be freed.
1112
 *
1236 jermar 1113
 */
1114
void frame_reference_add(pfn_t pfn)
1115
{
4344 svoboda 1116
    ipl_t ipl = interrupts_disable();
1117
    spinlock_lock(&zones.lock);
1236 jermar 1118
 
1119
    /*
1120
     * First, find host frame zone for addr.
1121
     */
4344 svoboda 1122
    count_t znum = find_zone(pfn, 1, NULL);
1236 jermar 1123
 
4344 svoboda 1124
    ASSERT(znum != (count_t) -1);
1236 jermar 1125
 
4344 svoboda 1126
    zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
1127
 
1128
    spinlock_unlock(&zones.lock);
1236 jermar 1129
    interrupts_restore(ipl);
1130
}
814 palkovsky 1131
 
3191 svoboda 1132
/** Mark given range unavailable in frame zones. */
820 jermar 1133
void frame_mark_unavailable(pfn_t start, count_t count)
814 palkovsky 1134
{
4344 svoboda 1135
    ipl_t ipl = interrupts_disable();
1136
    spinlock_lock(&zones.lock);
852 palkovsky 1137
 
4344 svoboda 1138
    count_t i;
2122 decky 1139
    for (i = 0; i < count; i++) {
4344 svoboda 1140
        count_t znum = find_zone(start + i, 1, 0);
1141
        if (znum == (count_t) -1)  /* PFN not found */
814 palkovsky 1142
            continue;
4344 svoboda 1143
 
1144
        zone_mark_unavailable(&zones.info[znum],
1145
            start + i - zones.info[znum].base);
814 palkovsky 1146
    }
4344 svoboda 1147
 
1148
    spinlock_unlock(&zones.lock);
1149
    interrupts_restore(ipl);
814 palkovsky 1150
}
1151
 
3191 svoboda 1152
/** Initialize physical memory management. */
814 palkovsky 1153
void frame_init(void)
1154
{
1155
    if (config.cpu_active == 1) {
1156
        zones.count = 0;
1981 jermar 1157
        spinlock_initialize(&zones.lock, "zones.lock");
3191 svoboda 1158
        mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1159
        condvar_initialize(&mem_avail_cv);
814 palkovsky 1160
    }
4344 svoboda 1161
 
814 palkovsky 1162
    /* Tell the architecture to create some memory */
1163
    frame_arch_init();
1164
    if (config.cpu_active == 1) {
2133 jermar 1165
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1166
            SIZE2FRAMES(config.kernel_size));
1167
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1168
            SIZE2FRAMES(config.stack_size));
1037 decky 1169
 
1170
        count_t i;
2133 jermar 1171
        for (i = 0; i < init.cnt; i++) {
1172
            pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr));
1173
            frame_mark_unavailable(pfn,
1174
                SIZE2FRAMES(init.tasks[i].size));
1175
        }
4344 svoboda 1176
 
1894 jermar 1177
        if (ballocs.size)
2133 jermar 1178
            frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1179
                SIZE2FRAMES(ballocs.size));
4344 svoboda 1180
 
1599 palkovsky 1181
        /* Black list first frame, as allocating NULL would
4344 svoboda 1182
         * fail in some places
1183
         */
1599 palkovsky 1184
        frame_mark_unavailable(0, 1);
814 palkovsky 1185
    }
1186
}
1187
 
3191 svoboda 1188
/** Return total size of all zones. */
1189
uint64_t zone_total_size(void)
1190
{
4344 svoboda 1191
    ipl_t ipl = interrupts_disable();
2725 decky 1192
    spinlock_lock(&zones.lock);
1193
 
4344 svoboda 1194
    uint64_t total = 0;
1195
    count_t i;
1196
    for (i = 0; i < zones.count; i++)
1197
        total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
2725 decky 1198
 
1199
    spinlock_unlock(&zones.lock);
1200
    interrupts_restore(ipl);
1201
 
1202
    return total;
1203
}
1204
 
3191 svoboda 1205
/** Prints list of zones. */
1206
void zone_print_list(void)
1207
{
4344 svoboda 1208
#ifdef __32_BITS__
1209
    printf("#  base address frames       flags    free frames  busy frames\n");
1210
    printf("-- ------------ ------------ -------- ------------ ------------\n");
3149 svoboda 1211
#endif
1212
 
1213
#ifdef __64_BITS__
4344 svoboda 1214
    printf("#  base address          frames      flags    free frames  busy frames\n");
1215
    printf("-- -------------------- ------------ -------- ------------ ------------\n");
3149 svoboda 1216
#endif
2712 decky 1217
 
3211 svoboda 1218
    /*
1219
     * Because printing may require allocation of memory, we may not hold
1220
     * the frame allocator locks when printing zone statistics.  Therefore,
1221
     * we simply gather the statistics under the protection of the locks and
1222
     * print the statistics when the locks have been released.
1223
     *
1224
     * When someone adds/removes zones while we are printing the statistics,
1225
     * we may end up with inaccurate output (e.g. a zone being skipped from
1226
     * the listing).
1227
     */
4344 svoboda 1228
 
1229
    count_t i;
1230
    for (i = 0;; i++) {
1231
        ipl_t ipl = interrupts_disable();
3211 svoboda 1232
        spinlock_lock(&zones.lock);
1233
 
1234
        if (i >= zones.count) {
1235
            spinlock_unlock(&zones.lock);
1236
            interrupts_restore(ipl);
1237
            break;
1238
        }
1239
 
4344 svoboda 1240
        uintptr_t base = PFN2ADDR(zones.info[i].base);
1241
        count_t count = zones.info[i].count;
1242
        zone_flags_t flags = zones.info[i].flags;
1243
        count_t free_count = zones.info[i].free_count;
1244
        count_t busy_count = zones.info[i].busy_count;
1245
 
3211 svoboda 1246
        spinlock_unlock(&zones.lock);
1247
        interrupts_restore(ipl);
4344 svoboda 1248
 
1249
        bool available = zone_flags_available(flags);
1250
 
1251
        printf("%-2" PRIc, i);
1252
 
3149 svoboda 1253
#ifdef __32_BITS__
4344 svoboda 1254
        printf("   %10p", base);
3149 svoboda 1255
#endif
4344 svoboda 1256
 
3149 svoboda 1257
#ifdef __64_BITS__
4344 svoboda 1258
        printf("   %18p", base);
3149 svoboda 1259
#endif
2712 decky 1260
 
4344 svoboda 1261
        printf(" %12" PRIc " %c%c%c      ", count,
1262
            available ? 'A' : ' ',
1263
            (flags & ZONE_RESERVED) ? 'R' : ' ',
1264
            (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1265
 
1266
        if (available)
1267
            printf("%12" PRIc " %12" PRIc,
1268
                free_count, busy_count);
1269
 
1270
        printf("\n");
676 bondari 1271
    }
1272
}
1273
 
1708 jermar 1274
/** Prints zone details.
677 bondari 1275
 *
4344 svoboda 1276
 * @param num Zone base address or zone number.
1277
 *
677 bondari 1278
 */
4344 svoboda 1279
void zone_print_one(count_t num)
3191 svoboda 1280
{
4344 svoboda 1281
    ipl_t ipl = interrupts_disable();
814 palkovsky 1282
    spinlock_lock(&zones.lock);
4344 svoboda 1283
    count_t znum = (count_t) -1;
1284
 
1285
    count_t i;
1037 decky 1286
    for (i = 0; i < zones.count; i++) {
4344 svoboda 1287
        if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1288
            znum = i;
822 palkovsky 1289
            break;
1290
        }
676 bondari 1291
    }
4344 svoboda 1292
 
1293
    if (znum == (count_t) -1) {
3211 svoboda 1294
        spinlock_unlock(&zones.lock);
1295
        interrupts_restore(ipl);
822 palkovsky 1296
        printf("Zone not found.\n");
3211 svoboda 1297
        return;
822 palkovsky 1298
    }
676 bondari 1299
 
4344 svoboda 1300
    uintptr_t base = PFN2ADDR(zones.info[i].base);
1301
    zone_flags_t flags = zones.info[i].flags;
1302
    count_t count = zones.info[i].count;
1303
    count_t free_count = zones.info[i].free_count;
1304
    count_t busy_count = zones.info[i].busy_count;
1305
 
814 palkovsky 1306
    spinlock_unlock(&zones.lock);
701 jermar 1307
    interrupts_restore(ipl);
4344 svoboda 1308
 
1309
    bool available = zone_flags_available(flags);
1310
 
1311
    printf("Zone number:       %" PRIc "\n", znum);
3211 svoboda 1312
    printf("Zone base address: %p\n", base);
4344 svoboda 1313
    printf("Zone size:         %" PRIc " frames (%" PRIs " KiB)\n", count,
3211 svoboda 1314
        SIZE2KB(FRAMES2SIZE(count)));
4344 svoboda 1315
    printf("Zone flags:        %c%c%c\n",
1316
        available ? 'A' : ' ',
1317
        (flags & ZONE_RESERVED) ? 'R' : ' ',
1318
        (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1319
 
1320
    if (available) {
1321
        printf("Allocated space:   %" PRIc " frames (%" PRIs " KiB)\n",
1322
            busy_count, SIZE2KB(FRAMES2SIZE(busy_count)));
1323
        printf("Available space:   %" PRIc " frames (%" PRIs " KiB)\n",
1324
            free_count, SIZE2KB(FRAMES2SIZE(free_count)));
1325
    }
676 bondari 1326
}
1327
 
1757 jermar 1328
/** @}
1702 cejka 1329
 */