Subversion Repositories HelenOS-historic

Rev

Rev 686 | Rev 703 | 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. #include <typedefs.h>
  31. #include <arch/types.h>
  32. #include <mm/heap.h>
  33. #include <mm/frame.h>
  34. #include <mm/vm.h>
  35. #include <panic.h>
  36. #include <debug.h>
  37. #include <list.h>
  38. #include <synch/spinlock.h>
  39. #include <arch/asm.h>
  40. #include <arch.h>
  41. #include <print.h>
  42. #include <align.h>
  43.  
  44. SPINLOCK_INITIALIZE(zone_head_lock);    /**< this lock protects zone_head list */
  45. LIST_INITIALIZE(zone_head);             /**< list of all zones in the system */
  46.  
  47. /** Blacklist containing non-available areas of memory.
  48.  *
  49.  * This blacklist is used to exclude frames that cannot be allocated
  50.  * (e.g. kernel memory) from available memory map.
  51.  */
  52. region_t zone_blacklist[ZONE_BLACKLIST_SIZE];
  53. count_t zone_blacklist_count = 0;
  54.  
  55. static struct buddy_system_operations  zone_buddy_system_operations = {
  56.     .find_buddy = zone_buddy_find_buddy,
  57.     .bisect = zone_buddy_bisect,
  58.     .coalesce = zone_buddy_coalesce,
  59.     .set_order = zone_buddy_set_order,
  60.     .get_order = zone_buddy_get_order,
  61.     .mark_busy = zone_buddy_mark_busy,
  62. };
  63.  
  64. /** Initialize physical memory management
  65.  *
  66.  * Initialize physical memory managemnt.
  67.  */
  68. void frame_init(void)
  69. {
  70.     if (config.cpu_active == 1) {
  71.         frame_region_not_free(KA2PA(config.base), config.kernel_size);
  72.         if (config.init_size > 0)
  73.             frame_region_not_free(KA2PA(config.init_addr), config.init_size);
  74.     }
  75.  
  76.     frame_arch_init();
  77. }
  78.  
  79. /** Allocate power-of-two frames of physical memory.
  80.  *
  81.  * @param flags Flags for host zone selection and address processing.
  82.  * @param order Allocate exactly 2^order frames.
  83.  *
  84.  * @return Allocated frame.
  85.  */
  86. __address frame_alloc(int flags, __u8 order, int * status)
  87. {
  88.     ipl_t ipl;
  89.     link_t *cur, *tmp;
  90.     zone_t *z;
  91.     zone_t *zone = NULL;
  92.     frame_t *frame = NULL;
  93.     __address v;
  94.    
  95. loop:
  96.     ipl = interrupts_disable();
  97.     spinlock_lock(&zone_head_lock);
  98.  
  99.     /*
  100.      * First, find suitable frame zone.
  101.      */
  102.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  103.         z = list_get_instance(cur, zone_t, link);
  104.        
  105.         spinlock_lock(&z->lock);
  106.  
  107.         /* Check if the zone has 2^order frames area available  */
  108.         if (buddy_system_can_alloc(z->buddy_system, order)) {
  109.             zone = z;
  110.             break;
  111.         }
  112.        
  113.         spinlock_unlock(&z->lock);
  114.     }
  115.    
  116.     if (!zone) {
  117.         if (flags & FRAME_PANIC)
  118.             panic("Can't allocate frame.\n");
  119.        
  120.        
  121.        
  122.         /*
  123.          * TODO: Sleep until frames are available again.
  124.          */
  125.         spinlock_unlock(&zone_head_lock);
  126.         interrupts_restore(ipl);
  127.  
  128.         if (flags & FRAME_NON_BLOCKING) {
  129.             ASSERT(status != NULL);
  130.             *status = FRAME_NO_MEMORY;
  131.             return NULL;
  132.         }
  133.        
  134.         panic("Sleep not implemented.\n");
  135.         goto loop;
  136.     }
  137.        
  138.  
  139.     /* Allocate frames from zone buddy system */
  140.     tmp = buddy_system_alloc(zone->buddy_system, order);
  141.    
  142.     ASSERT(tmp);
  143.    
  144.     /* Update zone information. */
  145.     zone->free_count -= (1 << order);
  146.     zone->busy_count += (1 << order);
  147.  
  148.     /* Frame will be actually a first frame of the block. */
  149.     frame = list_get_instance(tmp, frame_t, buddy_link);
  150.    
  151.     /* get frame address */
  152.     v = FRAME2ADDR(zone, frame);
  153.  
  154.     spinlock_unlock(&zone->lock);
  155.     spinlock_unlock(&zone_head_lock);
  156.     interrupts_restore(ipl);
  157.  
  158.  
  159.     if (flags & FRAME_KA)
  160.         v = PA2KA(v);
  161.    
  162.     if (flags & FRAME_NON_BLOCKING) {
  163.         ASSERT(status != NULL);
  164.         *status = FRAME_OK;
  165.     }
  166.    
  167.     return v;
  168. }
  169.  
  170. /** Free a frame.
  171.  *
  172.  * Find respective frame structrue for supplied addr.
  173.  * Decrement frame reference count.
  174.  * If it drops to zero, move the frame structure to free list.
  175.  *
  176.  * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
  177.  */
  178. void frame_free(__address addr)
  179. {
  180.     ipl_t ipl;
  181.     link_t *cur;
  182.     zone_t *z;
  183.     zone_t *zone = NULL;
  184.     frame_t *frame;
  185.     ASSERT(addr % FRAME_SIZE == 0);
  186.    
  187.     ipl = interrupts_disable();
  188.     spinlock_lock(&zone_head_lock);
  189.    
  190.     /*
  191.      * First, find host frame zone for addr.
  192.      */
  193.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  194.         z = list_get_instance(cur, zone_t, link);
  195.        
  196.         spinlock_lock(&z->lock);
  197.        
  198.         if (IS_KA(addr))
  199.             addr = KA2PA(addr);
  200.        
  201.         /*
  202.          * Check if addr belongs to z.
  203.          */
  204.         if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
  205.             zone = z;
  206.             break;
  207.         }
  208.         spinlock_unlock(&z->lock);
  209.     }
  210.    
  211.     ASSERT(zone != NULL);
  212.    
  213.     frame = ADDR2FRAME(zone, addr);
  214.  
  215.     ASSERT(frame->refcount);
  216.  
  217.     if (!--frame->refcount) {
  218.         buddy_system_free(zone->buddy_system, &frame->buddy_link);
  219.     }
  220.  
  221.     /* Update zone information. */
  222.     zone->free_count += (1 << frame->buddy_order);
  223.     zone->busy_count -= (1 << frame->buddy_order);
  224.    
  225.     spinlock_unlock(&zone->lock);
  226.     spinlock_unlock(&zone_head_lock);
  227.     interrupts_restore(ipl);
  228. }
  229.  
  230. /** Mark frame region not free.
  231.  *
  232.  * Mark frame region not free.
  233.  *
  234.  * @param base Base address of non-available region.
  235.  * @param size Size of non-available region.
  236.  */
  237. void frame_region_not_free(__address base, size_t size)
  238. {
  239.     index_t index;
  240.     index = zone_blacklist_count++;
  241.  
  242.     /* Force base to the nearest lower address frame boundary. */
  243.     base = ALIGN_DOWN(base, FRAME_SIZE);
  244.     /* Align size to frame boundary. */
  245.     size = ALIGN_UP(size, FRAME_SIZE);
  246.  
  247.     ASSERT(index < ZONE_BLACKLIST_SIZE);
  248.     zone_blacklist[index].base = base;
  249.     zone_blacklist[index].size = size;
  250. }
  251.  
  252. /** Create frame zones in region of available memory.
  253.  *
  254.  * Avoid any black listed areas of non-available memory.
  255.  * Assume that the black listed areas cannot overlap
  256.  * one another or cross available memory region boundaries.
  257.  *
  258.  * @param base Base address of available memory region.
  259.  * @param size Size of the region.
  260.  */
  261. void zone_create_in_region(__address base, size_t size) {
  262.     int i;
  263.     zone_t * z;
  264.     __address s;
  265.     size_t sz;
  266.    
  267.     ASSERT(base % FRAME_SIZE == 0);
  268.     ASSERT(size % FRAME_SIZE == 0);
  269.    
  270.     if (!size)
  271.         return;
  272.  
  273.     for (i = 0; i < zone_blacklist_count; i++) {
  274.         if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
  275.             s = base; sz = zone_blacklist[i].base - base;
  276.             ASSERT(base != s || sz != size);
  277.             zone_create_in_region(s, sz);
  278.            
  279.             s = zone_blacklist[i].base + zone_blacklist[i].size;
  280.             sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
  281.             ASSERT(base != s || sz != size);
  282.             zone_create_in_region(s, sz);
  283.             return;
  284.        
  285.         }
  286.     }
  287.    
  288.     z = zone_create(base, size, 0);
  289.  
  290.     if (!z) {
  291.         panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
  292.     }
  293.    
  294.     zone_attach(z);
  295. }
  296.  
  297.  
  298. /** Create frame zone
  299.  *
  300.  * Create new frame zone.
  301.  *
  302.  * @param start Physical address of the first frame within the zone.
  303.  * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
  304.  * @param flags Zone flags.
  305.  *
  306.  * @return Initialized zone.
  307.  */
  308. zone_t * zone_create(__address start, size_t size, int flags)
  309. {
  310.     zone_t *z;
  311.     count_t cnt;
  312.     int i;
  313.     __u8 max_order;
  314.  
  315.     ASSERT(start % FRAME_SIZE == 0);
  316.     ASSERT(size % FRAME_SIZE == 0);
  317.    
  318.     cnt = size / FRAME_SIZE;
  319.    
  320.     z = (zone_t *) early_malloc(sizeof(zone_t));
  321.     if (z) {
  322.         link_initialize(&z->link);
  323.         spinlock_initialize(&z->lock, "zone_lock");
  324.    
  325.         z->base = start;
  326.         z->flags = flags;
  327.  
  328.         z->free_count = cnt;
  329.         z->busy_count = 0;
  330.        
  331.         z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
  332.         if (!z->frames) {
  333.             early_free(z);
  334.             return NULL;
  335.         }
  336.        
  337.         for (i = 0; i<cnt; i++) {
  338.             frame_initialize(&z->frames[i], z);
  339.         }
  340.        
  341.         /*
  342.          * Create buddy system for the zone
  343.          */
  344.         for (max_order = 0; cnt >> max_order; max_order++)
  345.             ;
  346.         z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
  347.        
  348.         /* Stuffing frames */
  349.         for (i = 0; i<cnt; i++) {
  350.             z->frames[i].refcount = 0;
  351.             buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
  352.         }
  353.     }
  354.     return z;
  355. }
  356.  
  357. /** Attach frame zone
  358.  *
  359.  * Attach frame zone to zone list.
  360.  *
  361.  * @param zone Zone to be attached.
  362.  */
  363. void zone_attach(zone_t *zone)
  364. {
  365.     ipl_t ipl;
  366.    
  367.     ipl = interrupts_disable();
  368.     spinlock_lock(&zone_head_lock);
  369.    
  370.     list_append(&zone->link, &zone_head);
  371.    
  372.     spinlock_unlock(&zone_head_lock);
  373.     interrupts_restore(ipl);
  374. }
  375.  
  376. /** Initialize frame structure
  377.  *
  378.  * Initialize frame structure.
  379.  *
  380.  * @param frame Frame structure to be initialized.
  381.  * @param zone Host frame zone.
  382.  */
  383. void frame_initialize(frame_t *frame, zone_t *zone)
  384. {
  385.     frame->refcount = 1;
  386.     frame->buddy_order = 0;
  387. }
  388.  
  389.  
  390. /** Buddy system find_buddy implementation
  391.  *
  392.  * @param b Buddy system.
  393.  * @param block Block for which buddy should be found
  394.  *
  395.  * @return Buddy for given block if found
  396.  */
  397. link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
  398.     frame_t * frame;
  399.     zone_t * zone;
  400.     index_t index;
  401.     bool is_left, is_right;
  402.  
  403.     frame = list_get_instance(block, frame_t, buddy_link);
  404.     zone = (zone_t *) b->data;
  405.    
  406.     ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX(zone, frame), frame->buddy_order));
  407.    
  408.     is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
  409.     is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
  410.    
  411.     ASSERT(is_left ^ is_right);
  412.    
  413.     if (is_left) {
  414.         index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
  415.     } else { // if (is_right)
  416.         index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
  417.     }
  418.    
  419.     if (FRAME_INDEX_VALID(zone, index)) {
  420.         if (    zone->frames[index].buddy_order == frame->buddy_order &&
  421.             zone->frames[index].refcount == 0) {
  422.             return &zone->frames[index].buddy_link;
  423.         }
  424.     }
  425.    
  426.     return NULL;   
  427. }
  428.  
  429. /** Buddy system bisect implementation
  430.  *
  431.  * @param b Buddy system.
  432.  * @param block Block to bisect
  433.  *
  434.  * @return right block
  435.  */
  436. link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
  437.     frame_t * frame_l, * frame_r;
  438.  
  439.     frame_l = list_get_instance(block, frame_t, buddy_link);
  440.     frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
  441.    
  442.     return &frame_r->buddy_link;
  443. }
  444.  
  445. /** Buddy system coalesce implementation
  446.  *
  447.  * @param b Buddy system.
  448.  * @param block_1 First block
  449.  * @param block_2 First block's buddy
  450.  *
  451.  * @return Coalesced block (actually block that represents lower address)
  452.  */
  453. link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
  454.     frame_t * frame1, * frame2;
  455.    
  456.     frame1 = list_get_instance(block_1, frame_t, buddy_link);
  457.     frame2 = list_get_instance(block_2, frame_t, buddy_link);
  458.    
  459.     return frame1 < frame2 ? block_1 : block_2;
  460. }
  461.  
  462. /** Buddy system set_order implementation
  463.  *
  464.  * @param b Buddy system.
  465.  * @param block Buddy system block
  466.  * @param order Order to set
  467.  */
  468. void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
  469.     frame_t * frame;
  470.     frame = list_get_instance(block, frame_t, buddy_link);
  471.     frame->buddy_order = order;
  472. }
  473.  
  474. /** Buddy system get_order implementation
  475.  *
  476.  * @param b Buddy system.
  477.  * @param block Buddy system block
  478.  *
  479.  * @return Order of block
  480.  */
  481. __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
  482.     frame_t * frame;
  483.     frame = list_get_instance(block, frame_t, buddy_link);
  484.     return frame->buddy_order;
  485. }
  486.  
  487. /** Buddy system mark_busy implementation
  488.  *
  489.  * @param b Buddy system
  490.  * @param block Buddy system block
  491.  *
  492.  */
  493. void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
  494.     frame_t * frame;
  495.     frame = list_get_instance(block, frame_t, buddy_link);
  496.     frame->refcount = 1;
  497. }
  498.  
  499. /** Prints list of zones
  500.  *
  501.  */
  502. void zone_print_list(void) {
  503.     zone_t *zone = NULL;
  504.     link_t *cur;
  505.     spinlock_lock(&zone_head_lock);
  506.     printf("Base address\tFree Frames\tBusy Frames\n");
  507.     printf("------------\t-----------\t-----------\n");
  508.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  509.         zone = list_get_instance(cur, zone_t, link);
  510.         spinlock_lock(&zone->lock);
  511.         printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
  512.     }
  513.     spinlock_unlock(&zone_head_lock);
  514.  
  515. }
  516.  
  517. /** Prints zone details
  518.  *
  519.  * @param base Zone base address
  520.  */
  521. void zone_print_one(__address base) {
  522.     zone_t *zone = NULL, *z ;
  523.     link_t *cur;
  524.    
  525.    
  526.     spinlock_lock(&zone_head_lock);
  527.    
  528.    
  529.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  530.         z = list_get_instance(cur, zone_t, link);
  531.         if (base == z->base) {
  532.             zone = z;
  533.             break;
  534.         }
  535.     }
  536.    
  537.    
  538.     if (!zone) {
  539.         printf("No zone with address %X\n", base);
  540.         return;
  541.     }
  542.    
  543.     spinlock_lock(&zone->lock);
  544.     printf("Memory zone information\n\n");
  545.     printf("Zone base address: %P\n", zone->base);
  546.     printf("Zone size: %d frames (%d kbytes)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
  547.     printf("Allocated space: %d frames (%d kbytes)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
  548.     printf("Available space: %d (%d kbytes)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
  549.    
  550.     printf("\nBuddy allocator structures:\n\n");
  551.     buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
  552.    
  553.     spinlock_unlock(&zone->lock);
  554.     spinlock_unlock(&zone_head_lock);
  555.    
  556. }
  557.  
  558.