Subversion Repositories HelenOS

Rev

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