Subversion Repositories HelenOS

Rev

Rev 3208 | Rev 3973 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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