Subversion Repositories HelenOS

Rev

Rev 2725 | Rev 3185 | 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
1 jermar 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1757 jermar 30
/** @addtogroup genericmm
1702 cejka 31
 * @{
32
 */
33
 
1248 jermar 34
/**
1702 cejka 35
 * @file
1248 jermar 36
 * @brief   Physical frame allocator.
37
 *
38
 * This file contains the physical frame allocator and memory zone management.
39
 * The frame allocator is built on top of the buddy allocator.
40
 *
41
 * @see buddy.c
42
 */
43
 
814 palkovsky 44
/*
45
 * Locking order
46
 *
47
 * In order to access particular zone, the process must first lock
48
 * the zones.lock, then lock the zone and then unlock the zones.lock.
49
 * This insures, that we can fiddle with the zones in runtime without
50
 * affecting the processes.
51
 *
52
 */
53
 
1 jermar 54
#include <arch/types.h>
55
#include <mm/frame.h>
703 jermar 56
#include <mm/as.h>
1 jermar 57
#include <panic.h>
367 jermar 58
#include <debug.h>
788 jermar 59
#include <adt/list.h>
1 jermar 60
#include <synch/spinlock.h>
115 jermar 61
#include <arch/asm.h>
195 vana 62
#include <arch.h>
533 bondari 63
#include <print.h>
536 bondari 64
#include <align.h>
814 palkovsky 65
#include <mm/slab.h>
822 palkovsky 66
#include <bitops.h>
1063 palkovsky 67
#include <macros.h>
2128 jermar 68
#include <config.h>
115 jermar 69
 
814 palkovsky 70
typedef struct {
71
    count_t refcount;   /**< tracking of shared frames  */
1780 jermar 72
    uint8_t buddy_order;    /**< buddy system block order */
2133 jermar 73
    link_t buddy_link;  /**< link to the next free block inside one
74
                     order */
814 palkovsky 75
    void *parent;           /**< If allocated by slab, this points there */
1236 jermar 76
} frame_t;
367 jermar 77
 
814 palkovsky 78
typedef struct {
79
    SPINLOCK_DECLARE(lock); /**< this lock protects everything below */
2133 jermar 80
    pfn_t base;     /**< frame_no of the first frame in the frames
81
                     array */
820 jermar 82
    count_t count;          /**< Size of zone */
533 bondari 83
 
2133 jermar 84
    frame_t *frames;    /**< array of frame_t structures in this
85
                     zone */
814 palkovsky 86
    count_t free_count; /**< number of free frame_t structures */
87
    count_t busy_count; /**< number of busy frame_t structures */
88
 
2059 jermar 89
    buddy_system_t *buddy_system; /**< buddy system for the zone */
814 palkovsky 90
    int flags;
1236 jermar 91
} zone_t;
479 bondari 92
 
814 palkovsky 93
/*
94
 * The zoneinfo.lock must be locked when accessing zoneinfo structure.
95
 * Some of the attributes in zone_t structures are 'read-only'
368 jermar 96
 */
1 jermar 97
 
2121 decky 98
typedef struct {
814 palkovsky 99
    SPINLOCK_DECLARE(lock);
2122 decky 100
    unsigned int count;
814 palkovsky 101
    zone_t *info[ZONES_MAX];
2121 decky 102
} zones_t;
1 jermar 103
 
2121 decky 104
static zones_t zones;
814 palkovsky 105
 
2121 decky 106
 
2725 decky 107
/********************/
814 palkovsky 108
/* Helper functions */
2725 decky 109
/********************/
110
 
814 palkovsky 111
static inline index_t frame_index(zone_t *zone, frame_t *frame)
762 palkovsky 112
{
2725 decky 113
    return (index_t) (frame - zone->frames);
762 palkovsky 114
}
2725 decky 115
 
814 palkovsky 116
static inline index_t frame_index_abs(zone_t *zone, frame_t *frame)
1 jermar 117
{
2725 decky 118
    return (index_t) (frame - zone->frames) + zone->base;
1 jermar 119
}
2725 decky 120
 
814 palkovsky 121
static inline int frame_index_valid(zone_t *zone, index_t index)
122
{
2745 decky 123
    return (index < zone->count);
814 palkovsky 124
}
1 jermar 125
 
814 palkovsky 126
/** Compute pfn_t from frame_t pointer & zone pointer */
820 jermar 127
static index_t make_frame_index(zone_t *zone, frame_t *frame)
762 palkovsky 128
{
2725 decky 129
    return (frame - zone->frames);
762 palkovsky 130
}
131
 
814 palkovsky 132
/** Initialize frame structure
762 palkovsky 133
 *
814 palkovsky 134
 * Initialize frame structure.
135
 *
136
 * @param frame Frame structure to be initialized.
762 palkovsky 137
 */
814 palkovsky 138
static void frame_initialize(frame_t *frame)
762 palkovsky 139
{
814 palkovsky 140
    frame->refcount = 1;
141
    frame->buddy_order = 0;
762 palkovsky 142
}
143
 
2725 decky 144
/**********************/
814 palkovsky 145
/* Zoneinfo functions */
2725 decky 146
/**********************/
762 palkovsky 147
 
814 palkovsky 148
/**
149
 * Insert-sort zone into zones list
822 palkovsky 150
 *
1568 palkovsky 151
 * @param newzone New zone to be inserted into zone list
822 palkovsky 152
 * @return zone number on success, -1 on error
1 jermar 153
 */
822 palkovsky 154
static int zones_add_zone(zone_t *newzone)
1 jermar 155
{
2122 decky 156
    unsigned int i, j;
822 palkovsky 157
    ipl_t ipl;
158
    zone_t *z;
762 palkovsky 159
 
822 palkovsky 160
    ipl = interrupts_disable();
814 palkovsky 161
    spinlock_lock(&zones.lock);
2725 decky 162
 
814 palkovsky 163
    /* Try to merge */
2725 decky 164
    if (zones.count + 1 == ZONES_MAX) {
165
        printf("Maximum zone count %u exceeded!\n", ZONES_MAX);
166
        spinlock_unlock(&zones.lock);
167
        interrupts_restore(ipl);
168
        return -1;
169
    }
170
 
1037 decky 171
    for (i = 0; i < zones.count; i++) {
822 palkovsky 172
        /* Check for overflow */
852 palkovsky 173
        z = zones.info[i];
2725 decky 174
        if (overlaps(newzone->base, newzone->count, z->base, z->count)) {
822 palkovsky 175
            printf("Zones overlap!\n");
176
            return -1;
814 palkovsky 177
        }
852 palkovsky 178
        if (newzone->base < z->base)
822 palkovsky 179
            break;
368 jermar 180
    }
2725 decky 181
 
822 palkovsky 182
    /* Move other zones up */
2133 jermar 183
    for (j = i; j < zones.count; j++)
1037 decky 184
        zones.info[j + 1] = zones.info[j];
2725 decky 185
 
822 palkovsky 186
    zones.info[i] = newzone;
187
    zones.count++;
2725 decky 188
 
814 palkovsky 189
    spinlock_unlock(&zones.lock);
822 palkovsky 190
    interrupts_restore(ipl);
191
 
192
    return i;
1 jermar 193
}
194
 
814 palkovsky 195
/**
196
 * Try to find a zone where can we find the frame
2725 decky 197
 *
2059 jermar 198
 * Assume interrupts are disabled.
2725 decky 199
 *
1568 palkovsky 200
 * @param frame Frame number contained in zone
201
 * @param pzone If not null, it is used as zone hint. Zone index
202
 *              is filled into the variable on success.
2059 jermar 203
 * @return Pointer to locked zone containing frame
368 jermar 204
 */
2122 decky 205
static zone_t * find_zone_and_lock(pfn_t frame, unsigned int *pzone)
1 jermar 206
{
2122 decky 207
    unsigned int i;
208
    unsigned int hint = pzone ? *pzone : 0;
814 palkovsky 209
    zone_t *z;
533 bondari 210
 
814 palkovsky 211
    spinlock_lock(&zones.lock);
212
 
2745 decky 213
    if (hint >= zones.count)
814 palkovsky 214
        hint = 0;
533 bondari 215
 
814 palkovsky 216
    i = hint;
217
    do {
218
        z = zones.info[i];
219
        spinlock_lock(&z->lock);
220
        if (z->base <= frame && z->base + z->count > frame) {
2133 jermar 221
            /* Unlock the global lock */
222
            spinlock_unlock(&zones.lock);
814 palkovsky 223
            if (pzone)
224
                *pzone = i;
225
            return z;
533 bondari 226
        }
814 palkovsky 227
        spinlock_unlock(&z->lock);
533 bondari 228
 
814 palkovsky 229
        i++;
230
        if (i >= zones.count)
231
            i = 0;
232
    } while(i != hint);
233
 
234
    spinlock_unlock(&zones.lock);
235
    return NULL;
533 bondari 236
}
237
 
822 palkovsky 238
/** @return True if zone can allocate specified order */
1780 jermar 239
static int zone_can_alloc(zone_t *z, uint8_t order)
822 palkovsky 240
{
241
    return buddy_system_can_alloc(z->buddy_system, order);
242
}
243
 
2059 jermar 244
/** Find and lock zone that can allocate order frames.
367 jermar 245
 *
2059 jermar 246
 * Assume interrupts are disabled.
367 jermar 247
 *
1568 palkovsky 248
 * @param order Size (2^order) of free space we are trying to find
2133 jermar 249
 * @param pzone Pointer to preferred zone or NULL, on return contains zone
250
 *      number
367 jermar 251
 */
2123 decky 252
static zone_t * find_free_zone_and_lock(uint8_t order, unsigned int *pzone)
367 jermar 253
{
2122 decky 254
    unsigned int i;
367 jermar 255
    zone_t *z;
2122 decky 256
    unsigned int hint = pzone ? *pzone : 0;
367 jermar 257
 
814 palkovsky 258
    spinlock_lock(&zones.lock);
259
    if (hint >= zones.count)
260
        hint = 0;
261
    i = hint;
262
    do {
263
        z = zones.info[i];
724 palkovsky 264
 
814 palkovsky 265
        spinlock_lock(&z->lock);
367 jermar 266
 
814 palkovsky 267
        /* Check if the zone has 2^order frames area available  */
822 palkovsky 268
        if (zone_can_alloc(z, order)) {
814 palkovsky 269
            spinlock_unlock(&zones.lock);
270
            if (pzone)
271
                *pzone = i;
272
            return z;
367 jermar 273
        }
814 palkovsky 274
        spinlock_unlock(&z->lock);
275
        if (++i >= zones.count)
276
            i = 0;
277
    } while(i != hint);
278
    spinlock_unlock(&zones.lock);
279
    return NULL;
367 jermar 280
}
281
 
2059 jermar 282
/**************************/
814 palkovsky 283
/* Buddy system functions */
2059 jermar 284
/**************************/
814 palkovsky 285
 
286
/** Buddy system find_block implementation
367 jermar 287
 *
814 palkovsky 288
 * Find block that is parent of current list.
289
 * That means go to lower addresses, until such block is found
367 jermar 290
 *
814 palkovsky 291
 * @param order - Order of parent must be different then this parameter!!
367 jermar 292
 */
814 palkovsky 293
static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child,
2133 jermar 294
    uint8_t order)
367 jermar 295
{
2133 jermar 296
    frame_t *frame;
297
    zone_t *zone;
814 palkovsky 298
    index_t index;
367 jermar 299
 
814 palkovsky 300
    frame = list_get_instance(child, frame_t, buddy_link);
301
    zone = (zone_t *) b->data;
367 jermar 302
 
814 palkovsky 303
    index = frame_index(zone, frame);
304
    do {
305
        if (zone->frames[index].buddy_order != order) {
306
            return &zone->frames[index].buddy_link;
307
        }
308
    } while(index-- > 0);
309
    return NULL;
367 jermar 310
}
479 bondari 311
 
822 palkovsky 312
static void zone_buddy_print_id(buddy_system_t *b, link_t *block)
313
{
2133 jermar 314
    frame_t *frame;
315
    zone_t *zone;
822 palkovsky 316
    index_t index;
479 bondari 317
 
822 palkovsky 318
    frame = list_get_instance(block, frame_t, buddy_link);
319
    zone = (zone_t *) b->data;
320
    index = frame_index(zone, frame);
1196 cejka 321
    printf("%zd", index);
822 palkovsky 322
}                    
323
 
479 bondari 324
/** Buddy system find_buddy implementation
489 jermar 325
 *
326
 * @param b Buddy system.
480 bondari 327
 * @param block Block for which buddy should be found
479 bondari 328
 *
480 bondari 329
 * @return Buddy for given block if found
479 bondari 330
 */
2133 jermar 331
static link_t *zone_buddy_find_buddy(buddy_system_t *b, link_t *block)
814 palkovsky 332
{
2133 jermar 333
    frame_t *frame;
334
    zone_t *zone;
564 jermar 335
    index_t index;
480 bondari 336
    bool is_left, is_right;
479 bondari 337
 
480 bondari 338
    frame = list_get_instance(block, frame_t, buddy_link);
533 bondari 339
    zone = (zone_t *) b->data;
2133 jermar 340
    ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame),
341
        frame->buddy_order));
480 bondari 342
 
724 palkovsky 343
    is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
344
    is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
814 palkovsky 345
 
564 jermar 346
    ASSERT(is_left ^ is_right);
533 bondari 347
    if (is_left) {
814 palkovsky 348
        index = (frame_index(zone, frame)) + (1 << frame->buddy_order);
1981 jermar 349
    } else {    /* if (is_right) */
814 palkovsky 350
        index = (frame_index(zone, frame)) - (1 << frame->buddy_order);
533 bondari 351
    }
352
 
814 palkovsky 353
    if (frame_index_valid(zone, index)) {
354
        if (zone->frames[index].buddy_order == frame->buddy_order &&
355
            zone->frames[index].refcount == 0) {
533 bondari 356
            return &zone->frames[index].buddy_link;
480 bondari 357
        }
358
    }
814 palkovsky 359
 
539 jermar 360
    return NULL;   
479 bondari 361
}
362
 
363
/** Buddy system bisect implementation
364
 *
489 jermar 365
 * @param b Buddy system.
480 bondari 366
 * @param block Block to bisect
367
 *
368
 * @return right block
479 bondari 369
 */
2133 jermar 370
static link_t * zone_buddy_bisect(buddy_system_t *b, link_t *block) {
371
    frame_t *frame_l, *frame_r;
564 jermar 372
 
480 bondari 373
    frame_l = list_get_instance(block, frame_t, buddy_link);
533 bondari 374
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
564 jermar 375
 
480 bondari 376
    return &frame_r->buddy_link;
479 bondari 377
}
378
 
379
/** Buddy system coalesce implementation
380
 *
489 jermar 381
 * @param b Buddy system.
480 bondari 382
 * @param block_1 First block
383
 * @param block_2 First block's buddy
384
 *
385
 * @return Coalesced block (actually block that represents lower address)
479 bondari 386
 */
2133 jermar 387
static link_t *zone_buddy_coalesce(buddy_system_t *b, link_t *block_1,
388
    link_t *block_2)
822 palkovsky 389
{
814 palkovsky 390
    frame_t *frame1, *frame2;
564 jermar 391
 
480 bondari 392
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
393
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
564 jermar 394
 
533 bondari 395
    return frame1 < frame2 ? block_1 : block_2;
479 bondari 396
}
397
 
398
/** Buddy system set_order implementation
489 jermar 399
 *
400
 * @param b Buddy system.
480 bondari 401
 * @param block Buddy system block
402
 * @param order Order to set
479 bondari 403
 */
2133 jermar 404
static void zone_buddy_set_order(buddy_system_t *b, link_t *block,
405
    uint8_t order) {
406
    frame_t *frame;
480 bondari 407
    frame = list_get_instance(block, frame_t, buddy_link);
408
    frame->buddy_order = order;
479 bondari 409
}
410
 
411
/** Buddy system get_order implementation
489 jermar 412
 *
413
 * @param b Buddy system.
480 bondari 414
 * @param block Buddy system block
479 bondari 415
 *
480 bondari 416
 * @return Order of block
479 bondari 417
 */
2133 jermar 418
static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t *block) {
419
    frame_t *frame;
480 bondari 420
    frame = list_get_instance(block, frame_t, buddy_link);
421
    return frame->buddy_order;
479 bondari 422
}
533 bondari 423
 
424
/** Buddy system mark_busy implementation
425
 *
426
 * @param b Buddy system
427
 * @param block Buddy system block
428
 *
429
 */
814 palkovsky 430
static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
533 bondari 431
    frame_t * frame;
822 palkovsky 432
 
533 bondari 433
    frame = list_get_instance(block, frame_t, buddy_link);
434
    frame->refcount = 1;
435
}
676 bondari 436
 
814 palkovsky 437
/** Buddy system mark_available implementation
438
 *
439
 * @param b Buddy system
440
 * @param block Buddy system block
441
 *
442
 */
2133 jermar 443
static void zone_buddy_mark_available(buddy_system_t *b, link_t *block) {
444
    frame_t *frame;
814 palkovsky 445
    frame = list_get_instance(block, frame_t, buddy_link);
446
    frame->refcount = 0;
447
}
448
 
2083 decky 449
static buddy_system_operations_t zone_buddy_system_operations = {
814 palkovsky 450
    .find_buddy = zone_buddy_find_buddy,
451
    .bisect = zone_buddy_bisect,
452
    .coalesce = zone_buddy_coalesce,
453
    .set_order = zone_buddy_set_order,
454
    .get_order = zone_buddy_get_order,
455
    .mark_busy = zone_buddy_mark_busy,
456
    .mark_available = zone_buddy_mark_available,
822 palkovsky 457
    .find_block = zone_buddy_find_block,
458
    .print_id = zone_buddy_print_id
814 palkovsky 459
};
460
 
2059 jermar 461
/******************/
814 palkovsky 462
/* Zone functions */
2059 jermar 463
/******************/
814 palkovsky 464
 
465
/** Allocate frame in particular zone
466
 *
467
 * Assume zone is locked
1269 decky 468
 * Panics if allocation is impossible.
814 palkovsky 469
 *
1269 decky 470
 * @param zone  Zone to allocate from.
471
 * @param order Allocate exactly 2^order frames.
472
 *
814 palkovsky 473
 * @return Frame index in zone
1269 decky 474
 *
814 palkovsky 475
 */
1780 jermar 476
static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
814 palkovsky 477
{
478
    pfn_t v;
479
    link_t *tmp;
480
    frame_t *frame;
481
 
482
    /* Allocate frames from zone buddy system */
483
    tmp = buddy_system_alloc(zone->buddy_system, order);
484
 
485
    ASSERT(tmp);
486
 
487
    /* Update zone information. */
488
    zone->free_count -= (1 << order);
489
    zone->busy_count += (1 << order);
490
 
491
    /* Frame will be actually a first frame of the block. */
492
    frame = list_get_instance(tmp, frame_t, buddy_link);
493
 
494
    /* get frame address */
495
    v = make_frame_index(zone, frame);
496
    return v;
497
}
498
 
499
/** Free frame from zone
500
 *
501
 * Assume zone is locked
1568 palkovsky 502
 *
503
 * @param zone Pointer to zone from which the frame is to be freed
504
 * @param frame_idx Frame index relative to zone
814 palkovsky 505
 */
820 jermar 506
static void zone_frame_free(zone_t *zone, index_t frame_idx)
814 palkovsky 507
{
508
    frame_t *frame;
1780 jermar 509
    uint8_t order;
814 palkovsky 510
 
511
    frame = &zone->frames[frame_idx];
512
 
513
    /* remember frame order */
514
    order = frame->buddy_order;
515
 
516
    ASSERT(frame->refcount);
517
 
518
    if (!--frame->refcount) {
519
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
946 jermar 520
 
521
        /* Update zone information. */
522
        zone->free_count += (1 << order);
523
        zone->busy_count -= (1 << order);
814 palkovsky 524
    }
525
}
526
 
527
/** Return frame from zone */
820 jermar 528
static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx)
814 palkovsky 529
{
530
    ASSERT(frame_idx < zone->count);
531
    return &zone->frames[frame_idx];
532
}
533
 
534
/** Mark frame in zone unavailable to allocation */
820 jermar 535
static void zone_mark_unavailable(zone_t *zone, index_t frame_idx)
814 palkovsky 536
{
537
    frame_t *frame;
538
    link_t *link;
539
 
540
    frame = zone_get_frame(zone, frame_idx);
822 palkovsky 541
    if (frame->refcount)
542
        return;
2133 jermar 543
    link = buddy_system_alloc_block(zone->buddy_system,
544
        &frame->buddy_link);
814 palkovsky 545
    ASSERT(link);
546
    zone->free_count--;
547
}
548
 
822 palkovsky 549
/**
550
 * Join 2 zones
551
 *
552
 * Expect zone_t *z to point to space at least zone_conf_size large
553
 *
554
 * Assume z1 & z2 are locked
1568 palkovsky 555
 *
556
 * @param z Target zone structure pointer
557
 * @param z1 Zone to merge
558
 * @param z2 Zone to merge
822 palkovsky 559
 */
560
static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2)
561
{
1780 jermar 562
    uint8_t max_order;
2122 decky 563
    unsigned int i;
564
    int z2idx;
822 palkovsky 565
    pfn_t frame_idx;
566
    frame_t *frame;
567
 
2133 jermar 568
    ASSERT(!overlaps(z1->base, z1->count, z2->base, z2->count));
822 palkovsky 569
    ASSERT(z1->base < z2->base);
570
 
571
    spinlock_initialize(&z->lock, "zone_lock");
572
    z->base = z1->base;
2133 jermar 573
    z->count = z2->base + z2->count - z1->base;
822 palkovsky 574
    z->flags = z1->flags & z2->flags;
575
 
576
    z->free_count = z1->free_count + z2->free_count;
577
    z->busy_count = z1->busy_count + z2->busy_count;
578
 
579
    max_order = fnzb(z->count);
580
 
2133 jermar 581
    z->buddy_system = (buddy_system_t *) &z[1];
582
    buddy_system_create(z->buddy_system, max_order,
583
        &zone_buddy_system_operations, (void *) z);
822 palkovsky 584
 
2133 jermar 585
    z->frames = (frame_t *)((uint8_t *) z->buddy_system +
586
        buddy_conf_size(max_order));
822 palkovsky 587
    for (i = 0; i < z->count; i++) {
588
        /* This marks all frames busy */
589
        frame_initialize(&z->frames[i]);
590
    }
591
    /* Copy frames from both zones to preserve full frame orders,
1093 palkovsky 592
     * parents etc. Set all free frames with refcount=0 to 1, because
822 palkovsky 593
     * we add all free frames to buddy allocator later again, clear
1093 palkovsky 594
     * order to 0. Don't set busy frames with refcount=0, as they
595
     * will not be reallocated during merge and it would make later
596
     * problems with allocation/free.
822 palkovsky 597
     */
2122 decky 598
    for (i = 0; i < z1->count; i++)
822 palkovsky 599
        z->frames[i] = z1->frames[i];
2122 decky 600
    for (i = 0; i < z2->count; i++) {
822 palkovsky 601
        z2idx = i + (z2->base - z1->base);
602
        z->frames[z2idx] = z2->frames[i];
603
    }
1093 palkovsky 604
    i = 0;
605
    while (i < z->count) {
606
        if (z->frames[i].refcount) {
607
            /* skip busy frames */
608
            i += 1 << z->frames[i].buddy_order;
609
        } else { /* Free frames, set refcount=1 */
610
            /* All free frames have refcount=0, we need not
611
             * to check the order */
822 palkovsky 612
            z->frames[i].refcount = 1;
613
            z->frames[i].buddy_order = 0;
1093 palkovsky 614
            i++;
822 palkovsky 615
        }
616
    }
617
    /* Add free blocks from the 2 original zones */
618
    while (zone_can_alloc(z1, 0)) {
619
        frame_idx = zone_frame_alloc(z1, 0);
620
        frame = &z->frames[frame_idx];
621
        frame->refcount = 0;
622
        buddy_system_free(z->buddy_system, &frame->buddy_link);
623
    }
624
    while (zone_can_alloc(z2, 0)) {
625
        frame_idx = zone_frame_alloc(z2, 0);
2133 jermar 626
        frame = &z->frames[frame_idx + (z2->base - z1->base)];
822 palkovsky 627
        frame->refcount = 0;
628
        buddy_system_free(z->buddy_system, &frame->buddy_link);
629
    }
630
}
631
 
632
/** Return old configuration frames into the zone
633
 *
634
 * We have several cases
635
 * - the conf. data is outside of zone -> exit, shall we call frame_free??
824 palkovsky 636
 * - the conf. data was created by zone_create or
637
 *   updated with reduce_region -> free every frame
638
 *
639
 * @param newzone The actual zone where freeing should occur
640
 * @param oldzone Pointer to old zone configuration data that should
641
 *                be freed from new zone
822 palkovsky 642
 */
643
static void return_config_frames(zone_t *newzone, zone_t *oldzone)
644
{
645
    pfn_t pfn;
646
    frame_t *frame;
647
    count_t cframes;
2122 decky 648
    unsigned int i;
822 palkovsky 649
 
1780 jermar 650
    pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone));
822 palkovsky 651
    cframes = SIZE2FRAMES(zone_conf_size(oldzone->count));
652
 
653
    if (pfn < newzone->base || pfn >= newzone->base + newzone->count)
654
        return;
655
 
656
    frame = &newzone->frames[pfn - newzone->base];
824 palkovsky 657
    ASSERT(!frame->buddy_order);
822 palkovsky 658
 
2122 decky 659
    for (i = 0; i < cframes; i++) {
822 palkovsky 660
        newzone->busy_count++;
661
        zone_frame_free(newzone, pfn+i-newzone->base);
662
    }
663
}
664
 
824 palkovsky 665
/** Reduce allocated block to count of order 0 frames
666
 *
667
 * The allocated block need 2^order frames of space. Reduce all frames
1568 palkovsky 668
 * in block to order 0 and free the unneeded frames. This means, that
669
 * when freeing the previously allocated block starting with frame_idx,
670
 * you have to free every frame.
824 palkovsky 671
 *
672
 * @param zone
673
 * @param frame_idx Index to block
674
 * @param count Allocated space in block
675
 */
676
static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count)
677
{
678
    count_t i;
1780 jermar 679
    uint8_t order;
824 palkovsky 680
    frame_t *frame;
681
 
2122 decky 682
    ASSERT(frame_idx + count < zone->count);
824 palkovsky 683
 
684
    order = zone->frames[frame_idx].buddy_order;
2122 decky 685
    ASSERT((count_t) (1 << order) >= count);
824 palkovsky 686
 
687
    /* Reduce all blocks to order 0 */
2122 decky 688
    for (i = 0; i < (count_t) (1 << order); i++) {
824 palkovsky 689
        frame = &zone->frames[i + frame_idx];
690
        frame->buddy_order = 0;
2133 jermar 691
        if (!frame->refcount)
824 palkovsky 692
            frame->refcount = 1;
693
        ASSERT(frame->refcount == 1);
694
    }
695
    /* Free unneeded frames */
2122 decky 696
    for (i = count; i < (count_t) (1 << order); i++) {
824 palkovsky 697
        zone_frame_free(zone, i + frame_idx);
698
    }
699
}
700
 
822 palkovsky 701
/** Merge zones z1 and z2
702
 *
703
 * - the zones must be 2 zones with no zone existing in between,
704
 *   which means that z2 = z1+1
705
 *
706
 * - When you create a new zone, the frame allocator configuration does
707
 *   not to be 2^order size. Once the allocator is running it is no longer
708
 *   possible, merged configuration data occupies more space :-/
709
 */
2122 decky 710
void zone_merge(unsigned int z1, unsigned int z2)
822 palkovsky 711
{
712
    ipl_t ipl;
713
    zone_t *zone1, *zone2, *newzone;
2122 decky 714
    unsigned int cframes;
1780 jermar 715
    uint8_t order;
2122 decky 716
    unsigned int i;
822 palkovsky 717
    pfn_t pfn;
718
 
719
    ipl = interrupts_disable();
720
    spinlock_lock(&zones.lock);
721
 
2745 decky 722
    if ((z1 >= zones.count) || (z2 >= zones.count))
822 palkovsky 723
        goto errout;
724
    /* We can join only 2 zones with none existing inbetween */
725
    if (z2-z1 != 1)
726
        goto errout;
727
 
728
    zone1 = zones.info[z1];
729
    zone2 = zones.info[z2];
730
    spinlock_lock(&zone1->lock);
731
    spinlock_lock(&zone2->lock);
732
 
2133 jermar 733
    cframes = SIZE2FRAMES(zone_conf_size(zone2->base + zone2->count -
734
        zone1->base));
1700 palkovsky 735
    if (cframes == 1)
736
        order = 0;
737
    else
738
        order = fnzb(cframes - 1) + 1;
822 palkovsky 739
 
740
    /* Allocate zonedata inside one of the zones */
741
    if (zone_can_alloc(zone1, order))
742
        pfn = zone1->base + zone_frame_alloc(zone1, order);
743
    else if (zone_can_alloc(zone2, order))
744
        pfn = zone2->base + zone_frame_alloc(zone2, order);
745
    else
746
        goto errout2;
747
 
2122 decky 748
    newzone = (zone_t *) PA2KA(PFN2ADDR(pfn));
822 palkovsky 749
 
750
    _zone_merge(newzone, zone1, zone2);
751
 
824 palkovsky 752
    /* Free unneeded config frames */
753
    zone_reduce_region(newzone, pfn - newzone->base,  cframes);
822 palkovsky 754
    /* Subtract zone information from busy frames */
824 palkovsky 755
    newzone->busy_count -= cframes;
822 palkovsky 756
 
824 palkovsky 757
    /* Replace existing zones in zoneinfo list */
822 palkovsky 758
    zones.info[z1] = newzone;
1037 decky 759
    for (i = z2 + 1; i < zones.count; i++)
760
        zones.info[i - 1] = zones.info[i];
822 palkovsky 761
    zones.count--;
762
 
763
    /* Free old zone information */
764
    return_config_frames(newzone, zone1);
765
    return_config_frames(newzone, zone2);
766
errout2:
767
    /* Nobody is allowed to enter to zone, so we are safe
768
     * to touch the spinlocks last time */
769
    spinlock_unlock(&zone1->lock);
770
    spinlock_unlock(&zone2->lock);
771
errout:
772
    spinlock_unlock(&zones.lock);
773
    interrupts_restore(ipl);
774
}
775
 
776
/**
777
 * Merge all zones into one big zone
778
 *
779
 * It is reasonable to do this on systems whose bios reports parts in chunks,
780
 * so that we could have 1 zone (it's faster).
781
 */
782
void zone_merge_all(void)
783
{
784
    int count = zones.count;
785
 
786
    while (zones.count > 1 && --count) {
787
        zone_merge(0,1);
788
        break;
789
    }
790
}
791
 
814 palkovsky 792
/** Create frame zone
793
 *
794
 * Create new frame zone.
795
 *
796
 * @param start Physical address of the first frame within the zone.
1568 palkovsky 797
 * @param count Count of frames in zone
798
 * @param z Address of configuration information of zone
814 palkovsky 799
 * @param flags Zone flags.
800
 *
801
 * @return Initialized zone.
802
 */
822 palkovsky 803
static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags)
814 palkovsky 804
{
2122 decky 805
    unsigned int i;
1780 jermar 806
    uint8_t max_order;
814 palkovsky 807
 
808
    spinlock_initialize(&z->lock, "zone_lock");
809
    z->base = start;
810
    z->count = count;
811
    z->flags = flags;
812
    z->free_count = count;
813
    z->busy_count = 0;
814
 
815
    /*
816
     * Compute order for buddy system, initialize
817
     */
822 palkovsky 818
    max_order = fnzb(count);
814 palkovsky 819
    z->buddy_system = (buddy_system_t *)&z[1];
820
 
821
    buddy_system_create(z->buddy_system, max_order,
822
                &zone_buddy_system_operations,
823
                (void *) z);
824
 
825
    /* Allocate frames _after_ the conframe */
826
    /* Check sizes */
2133 jermar 827
    z->frames = (frame_t *)((uint8_t *) z->buddy_system +
828
        buddy_conf_size(max_order));
2122 decky 829
    for (i = 0; i < count; i++) {
814 palkovsky 830
        frame_initialize(&z->frames[i]);
831
    }
852 palkovsky 832
 
814 palkovsky 833
    /* Stuffing frames */
834
    for (i = 0; i < count; i++) {
835
        z->frames[i].refcount = 0;
836
        buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);
837
    }
838
}
839
 
1568 palkovsky 840
/** Compute configuration data size for zone
841
 *
842
 * @param count Size of zone in frames
843
 * @return Size of zone configuration info (in bytes)
844
 */
1780 jermar 845
uintptr_t zone_conf_size(count_t count)
814 palkovsky 846
{
847
    int size = sizeof(zone_t) + count*sizeof(frame_t);
848
    int max_order;
849
 
822 palkovsky 850
    max_order = fnzb(count);
814 palkovsky 851
    size += buddy_conf_size(max_order);
852
    return size;
853
}
854
 
855
/** Create and add zone to system
856
 *
1568 palkovsky 857
 * @param start First frame number (absolute)
858
 * @param count Size of zone in frames
859
 * @param confframe Where configuration frames are supposed to be.
860
 *                  Automatically checks, that we will not disturb the
861
 *                  kernel and possibly init.
814 palkovsky 862
 *                  If confframe is given _outside_ this zone, it is expected,
863
 *                  that the area is already marked BUSY and big enough
1568 palkovsky 864
 *                  to contain zone_conf_size() amount of data.
865
 *                  If the confframe is inside the area, the zone free frame
866
 *                  information is modified not to include it.
822 palkovsky 867
 *
868
 * @return Zone number or -1 on error
814 palkovsky 869
 */
822 palkovsky 870
int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags)
814 palkovsky 871
{
872
    zone_t *z;
1780 jermar 873
    uintptr_t addr;
820 jermar 874
    count_t confcount;
2122 decky 875
    unsigned int i;
822 palkovsky 876
    int znum;
814 palkovsky 877
 
878
    /* Theoretically we could have here 0, practically make sure
879
     * nobody tries to do that. If some platform requires, remove
880
     * the assert
881
     */
882
    ASSERT(confframe);
883
    /* If conframe is supposed to be inside our zone, then make sure
884
     * it does not span kernel & init
885
     */
822 palkovsky 886
    confcount = SIZE2FRAMES(zone_conf_size(count));
814 palkovsky 887
    if (confframe >= start && confframe < start+count) {
1037 decky 888
        for (;confframe < start + count; confframe++) {
814 palkovsky 889
            addr = PFN2ADDR(confframe);
2133 jermar 890
            if (overlaps(addr, PFN2ADDR(confcount),
891
                KA2PA(config.base), config.kernel_size))
814 palkovsky 892
                continue;
1037 decky 893
 
2133 jermar 894
            if (overlaps(addr, PFN2ADDR(confcount),
895
                KA2PA(config.stack_base), config.stack_size))
1833 decky 896
                continue;
897
 
1037 decky 898
            bool overlap = false;
899
            count_t i;
900
            for (i = 0; i < init.cnt; i++)
2133 jermar 901
                if (overlaps(addr, PFN2ADDR(confcount),
902
                    KA2PA(init.tasks[i].addr),
903
                    init.tasks[i].size)) {
1037 decky 904
                    overlap = true;
905
                    break;
906
                }
907
            if (overlap)
908
                continue;
909
 
814 palkovsky 910
            break;
911
        }
1037 decky 912
        if (confframe >= start + count)
814 palkovsky 913
            panic("Cannot find configuration data for zone.");
914
    }
915
 
2725 decky 916
    z = (zone_t *) PA2KA(PFN2ADDR(confframe));
822 palkovsky 917
    zone_construct(start, count, z, flags);
918
    znum = zones_add_zone(z);
919
    if (znum == -1)
920
        return -1;
921
 
814 palkovsky 922
    /* If confdata in zone, mark as unavailable */
2122 decky 923
    if (confframe >= start && confframe < start + count)
924
        for (i = confframe; i < confframe + confcount; i++) {
814 palkovsky 925
            zone_mark_unavailable(z, i - z->base);
926
        }
822 palkovsky 927
    return znum;
814 palkovsky 928
}
929
 
930
/***************************************/
931
/* Frame functions */
932
 
933
/** Set parent of frame */
2122 decky 934
void frame_set_parent(pfn_t pfn, void *data, unsigned int hint)
814 palkovsky 935
{
936
    zone_t *zone = find_zone_and_lock(pfn, &hint);
937
 
938
    ASSERT(zone);
939
 
940
    zone_get_frame(zone, pfn-zone->base)->parent = data;
941
    spinlock_unlock(&zone->lock);
942
}
943
 
2133 jermar 944
void *frame_get_parent(pfn_t pfn, unsigned int hint)
814 palkovsky 945
{
946
    zone_t *zone = find_zone_and_lock(pfn, &hint);
947
    void *res;
948
 
949
    ASSERT(zone);
950
    res = zone_get_frame(zone, pfn - zone->base)->parent;
951
 
952
    spinlock_unlock(&zone->lock);
953
    return res;
954
}
955
 
956
/** Allocate power-of-two frames of physical memory.
957
 *
1269 decky 958
 * @param order  Allocate exactly 2^order frames.
959
 * @param flags  Flags for host zone selection and address processing.
960
 * @param pzone  Preferred zone
814 palkovsky 961
 *
1760 palkovsky 962
 * @return Physical address of the allocated frame.
1269 decky 963
 *
814 palkovsky 964
 */
2123 decky 965
void * frame_alloc_generic(uint8_t order, int flags, unsigned int *pzone)
814 palkovsky 966
{
967
    ipl_t ipl;
968
    int freed;
969
    pfn_t v;
970
    zone_t *zone;
971
 
972
loop:
973
    ipl = interrupts_disable();
1269 decky 974
 
814 palkovsky 975
    /*
976
     * First, find suitable frame zone.
977
     */
1981 jermar 978
    zone = find_free_zone_and_lock(order, pzone);
1269 decky 979
 
814 palkovsky 980
    /* If no memory, reclaim some slab memory,
981
       if it does not help, reclaim all */
982
    if (!zone && !(flags & FRAME_NO_RECLAIM)) {
983
        freed = slab_reclaim(0);
984
        if (freed)
1981 jermar 985
            zone = find_free_zone_and_lock(order, pzone);
814 palkovsky 986
        if (!zone) {
987
            freed = slab_reclaim(SLAB_RECLAIM_ALL);
988
            if (freed)
1981 jermar 989
                zone = find_free_zone_and_lock(order, pzone);
814 palkovsky 990
        }
991
    }
992
    if (!zone) {
993
        /*
994
         * TODO: Sleep until frames are available again.
995
         */
996
        interrupts_restore(ipl);
997
 
1766 palkovsky 998
        if (flags & FRAME_ATOMIC)
1760 palkovsky 999
            return 0;
814 palkovsky 1000
 
1001
        panic("Sleep not implemented.\n");
1002
        goto loop;
1003
    }
1269 decky 1004
 
1005
    v = zone_frame_alloc(zone, order);
814 palkovsky 1006
    v += zone->base;
1007
 
1008
    spinlock_unlock(&zone->lock);
1009
    interrupts_restore(ipl);
1010
 
1760 palkovsky 1011
    if (flags & FRAME_KA)
1012
        return (void *)PA2KA(PFN2ADDR(v));
1013
    return (void *)PFN2ADDR(v);
814 palkovsky 1014
}
1015
 
1016
/** Free a frame.
1017
 *
1854 jermar 1018
 * Find respective frame structure for supplied physical frame address.
814 palkovsky 1019
 * Decrement frame reference count.
1020
 * If it drops to zero, move the frame structure to free list.
1021
 *
1760 palkovsky 1022
 * @param Frame Physical Address of of the frame to be freed.
814 palkovsky 1023
 */
1780 jermar 1024
void frame_free(uintptr_t frame)
814 palkovsky 1025
{
1026
    ipl_t ipl;
1027
    zone_t *zone;
1760 palkovsky 1028
    pfn_t pfn = ADDR2PFN(frame);
814 palkovsky 1029
 
1030
    ipl = interrupts_disable();
1031
 
1032
    /*
1033
     * First, find host frame zone for addr.
1034
     */
1035
    zone = find_zone_and_lock(pfn,NULL);
1036
    ASSERT(zone);
1037
 
1038
    zone_frame_free(zone, pfn-zone->base);
1039
 
1040
    spinlock_unlock(&zone->lock);
1041
    interrupts_restore(ipl);
1042
}
1043
 
1236 jermar 1044
/** Add reference to frame.
1045
 *
1046
 * Find respective frame structure for supplied PFN and
1047
 * increment frame reference count.
1048
 *
1708 jermar 1049
 * @param pfn Frame number of the frame to be freed.
1236 jermar 1050
 */
1051
void frame_reference_add(pfn_t pfn)
1052
{
1053
    ipl_t ipl;
1054
    zone_t *zone;
1055
    frame_t *frame;
814 palkovsky 1056
 
1236 jermar 1057
    ipl = interrupts_disable();
1058
 
1059
    /*
1060
     * First, find host frame zone for addr.
1061
     */
1062
    zone = find_zone_and_lock(pfn,NULL);
1063
    ASSERT(zone);
1064
 
1065
    frame = &zone->frames[pfn-zone->base];
1066
    frame->refcount++;
1067
 
1068
    spinlock_unlock(&zone->lock);
1069
    interrupts_restore(ipl);
1070
}
814 palkovsky 1071
 
1072
/** Mark given range unavailable in frame zones */
820 jermar 1073
void frame_mark_unavailable(pfn_t start, count_t count)
814 palkovsky 1074
{
2122 decky 1075
    unsigned int i;
814 palkovsky 1076
    zone_t *zone;
2122 decky 1077
    unsigned int prefzone = 0;
852 palkovsky 1078
 
2122 decky 1079
    for (i = 0; i < count; i++) {
1981 jermar 1080
        zone = find_zone_and_lock(start + i, &prefzone);
814 palkovsky 1081
        if (!zone) /* PFN not found */
1082
            continue;
1981 jermar 1083
        zone_mark_unavailable(zone, start + i - zone->base);
814 palkovsky 1084
 
1085
        spinlock_unlock(&zone->lock);
1086
    }
1087
}
1088
 
1089
/** Initialize physical memory management
1090
 *
1091
 * Initialize physical memory managemnt.
1092
 */
1093
void frame_init(void)
1094
{
1095
    if (config.cpu_active == 1) {
1096
        zones.count = 0;
1981 jermar 1097
        spinlock_initialize(&zones.lock, "zones.lock");
814 palkovsky 1098
    }
1099
    /* Tell the architecture to create some memory */
1100
    frame_arch_init();
1101
    if (config.cpu_active == 1) {
2133 jermar 1102
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1103
            SIZE2FRAMES(config.kernel_size));
1104
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1105
            SIZE2FRAMES(config.stack_size));
1037 decky 1106
 
1107
        count_t i;
2133 jermar 1108
        for (i = 0; i < init.cnt; i++) {
1109
            pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr));
1110
            frame_mark_unavailable(pfn,
1111
                SIZE2FRAMES(init.tasks[i].size));
1112
        }
1599 palkovsky 1113
 
1894 jermar 1114
        if (ballocs.size)
2133 jermar 1115
            frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1116
                SIZE2FRAMES(ballocs.size));
1894 jermar 1117
 
1599 palkovsky 1118
        /* Black list first frame, as allocating NULL would
1790 jermar 1119
         * fail in some places */
1599 palkovsky 1120
        frame_mark_unavailable(0, 1);
814 palkovsky 1121
    }
1122
}
1123
 
1124
 
2725 decky 1125
/** Return total size of all zones
1126
 *
1127
 */
1128
uint64_t zone_total_size(void) {
1129
    zone_t *zone = NULL;
1130
    unsigned int i;
1131
    ipl_t ipl;
1132
    uint64_t total = 0;
814 palkovsky 1133
 
2725 decky 1134
    ipl = interrupts_disable();
1135
    spinlock_lock(&zones.lock);
1136
 
1137
    for (i = 0; i < zones.count; i++) {
1138
        zone = zones.info[i];
1139
        spinlock_lock(&zone->lock);
1140
        total += (uint64_t) FRAMES2SIZE(zone->count);
1141
        spinlock_unlock(&zone->lock);
1142
    }
1143
 
1144
    spinlock_unlock(&zones.lock);
1145
    interrupts_restore(ipl);
1146
 
1147
    return total;
1148
}
1149
 
1150
 
1151
 
677 bondari 1152
/** Prints list of zones
1153
 *
1154
 */
676 bondari 1155
void zone_print_list(void) {
1156
    zone_t *zone = NULL;
2122 decky 1157
    unsigned int i;
701 jermar 1158
    ipl_t ipl;
1159
 
1160
    ipl = interrupts_disable();
814 palkovsky 1161
    spinlock_lock(&zones.lock);
2712 decky 1162
 
1163
    if (sizeof(void *) == 4) {
1164
        printf("#  base address free frames  busy frames\n");
1165
        printf("-- ------------ ------------ ------------\n");
1166
    } else {
1167
        printf("#  base address         free frames  busy frames\n");
1168
        printf("-- -------------------- ------------ ------------\n");
1169
    }
1170
 
1037 decky 1171
    for (i = 0; i < zones.count; i++) {
814 palkovsky 1172
        zone = zones.info[i];
683 bondari 1173
        spinlock_lock(&zone->lock);
2712 decky 1174
 
1175
        if (sizeof(void *) == 4)
1176
            printf("%-2d   %#10zx %12zd %12zd\n", i, PFN2ADDR(zone->base),
1177
                zone->free_count, zone->busy_count);
1178
        else
1179
            printf("%-2d   %#18zx %12zd %12zd\n", i, PFN2ADDR(zone->base),
1180
                zone->free_count, zone->busy_count);
1181
 
701 jermar 1182
        spinlock_unlock(&zone->lock);
676 bondari 1183
    }
2712 decky 1184
 
814 palkovsky 1185
    spinlock_unlock(&zones.lock);
701 jermar 1186
    interrupts_restore(ipl);
676 bondari 1187
}
1188
 
1708 jermar 1189
/** Prints zone details.
677 bondari 1190
 *
1708 jermar 1191
 * @param num Zone base address or zone number.
677 bondari 1192
 */
2122 decky 1193
void zone_print_one(unsigned int num) {
814 palkovsky 1194
    zone_t *zone = NULL;
701 jermar 1195
    ipl_t ipl;
2122 decky 1196
    unsigned int i;
701 jermar 1197
 
1198
    ipl = interrupts_disable();
814 palkovsky 1199
    spinlock_lock(&zones.lock);
822 palkovsky 1200
 
1037 decky 1201
    for (i = 0; i < zones.count; i++) {
2725 decky 1202
        if ((i == num) || (PFN2ADDR(zones.info[i]->base) == num)) {
822 palkovsky 1203
            zone = zones.info[i];
1204
            break;
1205
        }
676 bondari 1206
    }
822 palkovsky 1207
    if (!zone) {
1208
        printf("Zone not found.\n");
1209
        goto out;
1210
    }
676 bondari 1211
 
683 bondari 1212
    spinlock_lock(&zone->lock);
822 palkovsky 1213
    printf("Memory zone information\n");
2133 jermar 1214
    printf("Zone base address: %#.*p\n", sizeof(uintptr_t) * 2,
1215
        PFN2ADDR(zone->base));
2725 decky 1216
    printf("Zone size: %zd frames (%zd KB)\n", zone->count,
1217
        SIZE2KB(FRAMES2SIZE(zone->count)));
1218
    printf("Allocated space: %zd frames (%zd KB)\n", zone->busy_count,
1219
        SIZE2KB(FRAMES2SIZE(zone->busy_count)));
1220
    printf("Available space: %zd frames (%zd KB)\n", zone->free_count,
1221
        SIZE2KB(FRAMES2SIZE(zone->free_count)));
686 bondari 1222
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
2725 decky 1223
    spinlock_unlock(&zone->lock);
683 bondari 1224
 
822 palkovsky 1225
out:
814 palkovsky 1226
    spinlock_unlock(&zones.lock);
701 jermar 1227
    interrupts_restore(ipl);
676 bondari 1228
}
1229
 
1757 jermar 1230
/** @}
1702 cejka 1231
 */
2133 jermar 1232