Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <typedefs.h>
  30. #include <arch/types.h>
  31. #include <mm/heap.h>
  32. #include <mm/frame.h>
  33. #include <mm/vm.h>
  34. #include <panic.h>
  35. #include <debug.h>
  36. #include <list.h>
  37. #include <synch/spinlock.h>
  38. #include <arch/asm.h>
  39. #include <arch.h>
  40.  
  41. spinlock_t zone_head_lock;       /**< this lock protects zone_head list */
  42. link_t zone_head;                /**< list of all zones in the system */
  43.  
  44. /** Initialize physical memory management
  45.  *
  46.  * Initialize physical memory managemnt.
  47.  */
  48. void frame_init(void)
  49. {
  50.     if (config.cpu_active == 1) {
  51.         zone_init();
  52.     }
  53.  
  54.     frame_arch_init();
  55.    
  56.     if (config.cpu_active == 1) {
  57.                 frame_region_not_free(config.base, config.base + config.kernel_size + CONFIG_STACK_SIZE);
  58.         }  
  59. }
  60.  
  61. /** Allocate a frame
  62.  *
  63.  * Allocate a frame of physical memory.
  64.  *
  65.  * @param flags Flags for host zone selection and address processing.
  66.  *
  67.  * @return Allocated frame.
  68.  */
  69. __address frame_alloc(int flags)
  70. {
  71.     pri_t pri;
  72.     link_t *cur, *tmp;
  73.     zone_t *z;
  74.     zone_t *zone = NULL;
  75.     frame_t *frame = NULL;
  76.     __address v;
  77.    
  78. loop:
  79.     pri = cpu_priority_high();
  80.     spinlock_lock(&zone_head_lock);
  81.    
  82.     /*
  83.      * First, find suitable frame zone.
  84.      */
  85.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  86.         z = list_get_instance(cur, zone_t, link);
  87.        
  88.         spinlock_lock(&z->lock);
  89.         /*
  90.          * Check if the zone has any free frames.
  91.          */
  92.         if (z->free_count) {
  93.             zone = z;
  94.             break;
  95.         }
  96.         spinlock_unlock(&z->lock);
  97.     }
  98.    
  99.     if (!zone) {
  100.         if (flags & FRAME_PANIC)
  101.             panic("Can't allocate frame.\n");
  102.        
  103.         /*
  104.          * TODO: Sleep until frames are available again.
  105.          */
  106.         spinlock_unlock(&zone_head_lock);
  107.         cpu_priority_restore(pri);
  108.  
  109.         panic("Sleep not implemented.\n");
  110.         goto loop;
  111.     }
  112.        
  113.     tmp = zone->free_head.next;
  114.     frame = list_get_instance(tmp, frame_t, link);
  115.  
  116.     frame->refcount++;
  117.     list_remove(tmp);           /* remove frame from free_head */
  118.     zone->free_count--;
  119.     zone->busy_count++;
  120.    
  121.     v = zone->base + (frame - zone->frames) * FRAME_SIZE;
  122.    
  123.     if (flags & FRAME_KA)
  124.         v = PA2KA(v);
  125.    
  126.     spinlock_unlock(&zone->lock);
  127.    
  128.     spinlock_unlock(&zone_head_lock);
  129.     cpu_priority_restore(pri);
  130.    
  131.     return v;
  132. }
  133.  
  134. /** Free a frame.
  135.  *
  136.  * Find respective frame structrue for supplied addr.
  137.  * Decrement frame reference count.
  138.  * If it drops to zero, move the frame structure to free list.
  139.  *
  140.  * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
  141.  */
  142. void frame_free(__address addr)
  143. {
  144.     pri_t pri;
  145.     link_t *cur;
  146.     zone_t *z;
  147.     zone_t *zone = NULL;
  148.     frame_t *frame;
  149.    
  150.     ASSERT(addr % FRAME_SIZE == 0);
  151.    
  152.     pri = cpu_priority_high();
  153.     spinlock_lock(&zone_head_lock);
  154.    
  155.     /*
  156.      * First, find host frame zone for addr.
  157.      */
  158.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  159.         z = list_get_instance(cur, zone_t, link);
  160.        
  161.         spinlock_lock(&z->lock);
  162.        
  163.         if (IS_KA(addr))
  164.             addr = KA2PA(addr);
  165.        
  166.         /*
  167.          * Check if addr belongs to z.
  168.          */
  169.         if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
  170.             zone = z;
  171.             break;
  172.         }
  173.         spinlock_unlock(&z->lock);
  174.     }
  175.    
  176.     ASSERT(zone != NULL);
  177.    
  178.     frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
  179.     ASSERT(frame->refcount);
  180.  
  181.     if (!--frame->refcount) {
  182.         list_append(&frame->link, &zone->free_head);    /* append frame to free_head */
  183.         zone->free_count++;
  184.         zone->busy_count--;
  185.     }
  186.    
  187.     spinlock_unlock(&zone->lock);  
  188.    
  189.     spinlock_unlock(&zone_head_lock);
  190.     cpu_priority_restore(pri);
  191. }
  192.  
  193. /** Mark frame not free.
  194.  *
  195.  * Find respective frame structrue for supplied addr.
  196.  * Increment frame reference count and remove the frame structure from free list.
  197.  *
  198.  * @param addr Address of the frame to be marked. It must be a multiple of FRAME_SIZE.
  199.  */
  200. void frame_not_free(__address addr)
  201. {
  202.     pri_t pri;
  203.     link_t *cur;
  204.     zone_t *z;
  205.     zone_t *zone = NULL;
  206.     frame_t *frame;
  207.    
  208.     ASSERT(addr % FRAME_SIZE == 0);
  209.    
  210.     pri = cpu_priority_high();
  211.     spinlock_lock(&zone_head_lock);
  212.    
  213.     /*
  214.      * First, find host frame zone for addr.
  215.      */
  216.     for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
  217.         z = list_get_instance(cur, zone_t, link);
  218.        
  219.         spinlock_lock(&z->lock);
  220.        
  221.         if (IS_KA(addr))
  222.             addr = KA2PA(addr);
  223.        
  224.         /*
  225.          * Check if addr belongs to z.
  226.          */
  227.         if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
  228.             zone = z;
  229.             break;
  230.         }
  231.         spinlock_unlock(&z->lock);
  232.     }
  233.    
  234.     ASSERT(zone != NULL);
  235.    
  236.     frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
  237.  
  238.     if (!frame->refcount) {
  239.         frame->refcount++;
  240.  
  241.         list_remove(&frame->link);          /* remove frame from free_head */
  242.         zone->free_count--;
  243.         zone->busy_count++;
  244.     }
  245.    
  246.     spinlock_unlock(&zone->lock);  
  247.    
  248.     spinlock_unlock(&zone_head_lock);
  249.     cpu_priority_restore(pri);
  250. }
  251.  
  252. /** Mark frame region not free.
  253.  *
  254.  * Mark frame region not free.
  255.  *
  256.  * @param start First address.
  257.  * @param stop Last address.
  258.  */
  259. void frame_region_not_free(__address start, __address stop)
  260. {
  261.         __address a;
  262.  
  263.         start /= FRAME_SIZE;
  264.         stop /= FRAME_SIZE;
  265.         for (a = start; a <= stop; a++)
  266.                 frame_not_free(a * FRAME_SIZE);
  267. }
  268.  
  269.  
  270. /** Initialize zonekeeping
  271.  *
  272.  * Initialize zonekeeping.
  273.  */
  274. void zone_init(void)
  275. {
  276.     spinlock_initialize(&zone_head_lock);
  277.     list_initialize(&zone_head);
  278. }
  279.  
  280. /** Create frame zone
  281.  *
  282.  * Create new frame zone.
  283.  *
  284.  * @param start Physical address of the first frame within the zone.
  285.  * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
  286.  * @param flags Zone flags.
  287.  *
  288.  * @return Initialized zone.
  289.  */
  290. zone_t *zone_create(__address start, size_t size, int flags)
  291. {
  292.     zone_t *z;
  293.     count_t cnt;
  294.     int i;
  295.    
  296.     ASSERT(start % FRAME_SIZE == 0);
  297.     ASSERT(size % FRAME_SIZE == 0);
  298.    
  299.     cnt = size / FRAME_SIZE;
  300.    
  301.     z = (zone_t *) early_malloc(sizeof(zone_t));
  302.     if (z) {
  303.         link_initialize(&z->link);
  304.         spinlock_initialize(&z->lock);
  305.    
  306.         z->base = start;
  307.         z->flags = flags;
  308.  
  309.         z->free_count = cnt;
  310.         list_initialize(&z->free_head);
  311.  
  312.         z->busy_count = 0;
  313.        
  314.         z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
  315.         if (!z->frames) {
  316.             free(z);
  317.             return NULL;
  318.         }
  319.        
  320.         for (i = 0; i<cnt; i++) {
  321.             frame_initialize(&z->frames[i], z);
  322.             list_append(&z->frames[i].link, &z->free_head);
  323.         }
  324.        
  325.     }
  326.    
  327.     return z;
  328. }
  329.  
  330. /** Attach frame zone
  331.  *
  332.  * Attach frame zone to zone list.
  333.  *
  334.  * @param zone Zone to be attached.
  335.  */
  336. void zone_attach(zone_t *zone)
  337. {
  338.     pri_t pri;
  339.    
  340.     pri = cpu_priority_high();
  341.     spinlock_lock(&zone_head_lock);
  342.    
  343.     list_append(&zone->link, &zone_head);
  344.    
  345.     spinlock_unlock(&zone_head_lock);
  346.     cpu_priority_restore(pri);
  347. }
  348.  
  349. /** Initialize frame structure
  350.  *
  351.  * Initialize frame structure.
  352.  *
  353.  * @param frame Frame structure to be initialized.
  354.  * @param zone Host frame zone.
  355.  */
  356. void frame_initialize(frame_t *frame, zone_t *zone)
  357. {
  358.     frame->refcount = 0;
  359.     link_initialize(&frame->link);
  360. }
  361.