Subversion Repositories HelenOS-historic

Rev

Rev 1 | Go to most recent revision | Blame | 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 <mm/vm.h>
  30. #include <mm/page.h>
  31. #include <mm/frame.h>
  32. #include <mm/tlb.h>
  33. #include <arch/mm/page.h>
  34. #include <arch/types.h>
  35. #include <typedefs.h>
  36. #include <synch/spinlock.h>
  37. #include <config.h>
  38. #include <list.h>
  39. #include <panic.h>
  40.  
  41. vm_t *vm_create(void)
  42. {
  43.     vm_t *m;
  44.    
  45.     m = (vm_t *) malloc(sizeof(vm_t));
  46.     if (m) {
  47.         spinlock_initialize(&m->lock);
  48.         list_initialize(&m->vm_area_head);
  49.     }
  50.    
  51.     return m;
  52. }
  53.  
  54. void vm_destroy(vm_t *m)
  55. {
  56. }
  57.  
  58. vm_area_t *vm_area_create(vm_t *m, vm_type_t type, int size, __address addr)
  59. {
  60.     pri_t pri;
  61.     vm_area_t *a;
  62.    
  63.     if (addr % PAGE_SIZE)
  64.         panic(PANIC "addr not aligned to a page boundary");
  65.    
  66.     pri = cpu_priority_high();
  67.     spinlock_lock(&m->lock);
  68.    
  69.     /*
  70.      * TODO: test vm_area which is to be created doesn't overlap with an existing one.
  71.      */
  72.    
  73.     a = (vm_area_t *) malloc(sizeof(vm_area_t));
  74.     if (a) {
  75.         int i;
  76.    
  77.         a->mapping = (__address *) malloc(size * sizeof(__address));
  78.         if (!a->mapping) {
  79.             free(a);
  80.             spinlock_unlock(&m->lock);
  81.             cpu_priority_restore(pri);
  82.             return NULL;
  83.         }
  84.        
  85.         for (i=0; i<size; i++)
  86.             a->mapping[i] = frame_alloc(0);
  87.        
  88.         spinlock_initialize(&a->lock);
  89.            
  90.         link_initialize(&a->link);         
  91.         a->type = type;
  92.         a->size = size;
  93.         a->address = addr;
  94.        
  95.         list_append(&a->link, &m->vm_area_head);
  96.  
  97.     }
  98.  
  99.     spinlock_unlock(&m->lock);
  100.     cpu_priority_restore(pri);
  101.    
  102.     return a;
  103. }
  104.  
  105. void vm_area_destroy(vm_area_t *a)
  106. {
  107. }
  108.  
  109. void vm_area_map(vm_area_t *a)
  110. {
  111.     int i, flags;
  112.     pri_t pri;
  113.    
  114.     pri = cpu_priority_high();
  115.     spinlock_lock(&a->lock);
  116.  
  117.     switch (a->type) {
  118.         case VMA_TEXT:
  119.             flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
  120.             break;
  121.         case VMA_DATA:
  122.         case VMA_STACK:
  123.             flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
  124.             break;
  125.         default:
  126.             panic(PANIC "unexpected vm_type_t %d", a->type);
  127.     }
  128.    
  129.     for (i=0; i<a->size; i++)
  130.         map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0);
  131.        
  132.     spinlock_unlock(&a->lock);
  133.     cpu_priority_restore(pri);
  134. }
  135.  
  136. void vm_area_unmap(vm_area_t *a)
  137. {
  138.     int i;
  139.     pri_t pri;
  140.    
  141.     pri = cpu_priority_high();
  142.     spinlock_lock(&a->lock);
  143.  
  144.     for (i=0; i<a->size; i++)      
  145.         map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0);
  146.    
  147.     spinlock_unlock(&a->lock);
  148.     cpu_priority_restore(pri);
  149. }
  150.  
  151. void vm_install(vm_t *m)
  152. {
  153.     link_t *l;
  154.     pri_t pri;
  155.    
  156.     pri = cpu_priority_high();
  157.     spinlock_lock(&m->lock);
  158.  
  159.     for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
  160.         vm_area_map(list_get_instance(l, vm_area_t, link));
  161.  
  162.     spinlock_unlock(&m->lock);
  163.     cpu_priority_restore(pri);
  164. }
  165.  
  166. void vm_uninstall(vm_t *m)
  167. {
  168.     link_t *l;
  169.     pri_t pri;
  170.    
  171.     pri = cpu_priority_high();
  172.  
  173.     tlb_shutdown_start();
  174.  
  175.     spinlock_lock(&m->lock);
  176.  
  177.     for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
  178.         vm_area_unmap(list_get_instance(l, vm_area_t, link));
  179.  
  180.     spinlock_unlock(&m->lock);
  181.  
  182.     tlb_invalidate(0);
  183.     tlb_shutdown_finalize();
  184.  
  185.     cpu_priority_restore(pri);
  186. }
  187.