Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2001-2006 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. /*
  30.  * This file contains address space manipulation functions.
  31.  * Roughly speaking, this is a higher-level client of
  32.  * Virtual Address Translation (VAT) subsystem.
  33.  */
  34.  
  35. #include <mm/as.h>
  36. #include <arch/mm/as.h>
  37. #include <mm/page.h>
  38. #include <mm/frame.h>
  39. #include <mm/tlb.h>
  40. #include <mm/heap.h>
  41. #include <arch/mm/page.h>
  42. #include <genarch/mm/page_pt.h>
  43. #include <mm/asid.h>
  44. #include <arch/mm/asid.h>
  45. #include <arch/types.h>
  46. #include <typedefs.h>
  47. #include <synch/spinlock.h>
  48. #include <config.h>
  49. #include <list.h>
  50. #include <panic.h>
  51. #include <arch/asm.h>
  52. #include <debug.h>
  53. #include <memstr.h>
  54. #include <arch.h>
  55. #include <print.h>
  56.  
  57. as_operations_t *as_operations = NULL;
  58.  
  59. static int get_area_flags(as_area_t *a);
  60.  
  61. /** Initialize address space subsystem. */
  62. void as_init(void)
  63. {
  64.     as_arch_init();
  65.     AS_KERNEL = as_create(FLAG_AS_KERNEL);
  66.         if (!AS_KERNEL)
  67.                 panic("can't create kernel address space\n");
  68. }
  69.  
  70. /** Create address space. */
  71. as_t *as_create(int flags)
  72. {
  73.     as_t *as;
  74.  
  75.     as = (as_t *) malloc(sizeof(as_t));
  76.     if (as) {
  77.         list_initialize(&as->as_with_asid_link);
  78.         spinlock_initialize(&as->lock, "as_lock");
  79.         list_initialize(&as->as_area_head);
  80.  
  81.         if (flags & FLAG_AS_KERNEL)
  82.             as->asid = ASID_KERNEL;
  83.         else
  84.             as->asid = ASID_INVALID;
  85.  
  86.         as->page_table = page_table_create(flags);
  87.     }
  88.  
  89.     return as;
  90. }
  91.  
  92. /** Create address space area of common attributes.
  93.  *
  94.  * The created address space area is added to the target address space.
  95.  *
  96.  * @param as Target address space.
  97.  * @param type Type of area.
  98.  * @param size Size of area in multiples of PAGE_SIZE.
  99.  * @param base Base address of area.
  100.  *
  101.  * @return Address space area on success or NULL on failure.
  102.  */
  103. as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
  104. {
  105.     ipl_t ipl;
  106.     as_area_t *a;
  107.    
  108.     if (base % PAGE_SIZE)
  109.         panic("addr not aligned to a page boundary");
  110.    
  111.     ipl = interrupts_disable();
  112.     spinlock_lock(&as->lock);
  113.    
  114.     /*
  115.      * TODO: test as_area which is to be created doesn't overlap with an existing one.
  116.      */
  117.    
  118.     a = (as_area_t *) malloc(sizeof(as_area_t));
  119.     if (a) {   
  120.         spinlock_initialize(&a->lock, "as_area_lock");
  121.            
  122.         link_initialize(&a->link);         
  123.         a->type = type;
  124.         a->size = size;
  125.         a->base = base;
  126.        
  127.         list_append(&a->link, &as->as_area_head);
  128.     }
  129.  
  130.     spinlock_unlock(&as->lock);
  131.     interrupts_restore(ipl);
  132.  
  133.     return a;
  134. }
  135.  
  136. /** Initialize mapping for one page of address space.
  137.  *
  138.  * This functions maps 'page' to 'frame' according
  139.  * to attributes of the address space area to
  140.  * wich 'page' belongs.
  141.  *
  142.  * @param a Target address space.
  143.  * @param page Virtual page within the area.
  144.  * @param frame Physical frame to which page will be mapped.
  145.  */
  146. void as_set_mapping(as_t *as, __address page, __address frame)
  147. {
  148.     as_area_t *a, *area = NULL;
  149.     link_t *cur;
  150.     ipl_t ipl;
  151.    
  152.     ipl = interrupts_disable();
  153.     spinlock_lock(&as->lock);
  154.    
  155.     /*
  156.      * First, try locate an area.
  157.      */
  158.     for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
  159.         a = list_get_instance(cur, as_area_t, link);
  160.         spinlock_lock(&a->lock);
  161.  
  162.         if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
  163.             area = a;
  164.             break;
  165.         }
  166.        
  167.         spinlock_unlock(&a->lock);
  168.     }
  169.    
  170.     if (!area) {
  171.         panic("page not part of any as_area\n");
  172.     }
  173.  
  174.     /*
  175.      * Note: area->lock is held.
  176.      */
  177.    
  178.     page_mapping_insert(as, page, frame, get_area_flags(area));
  179.    
  180.     spinlock_unlock(&area->lock);
  181.     spinlock_unlock(&as->lock);
  182.     interrupts_restore(ipl);
  183. }
  184.  
  185. /** Handle page fault within the current address space.
  186.  *
  187.  * This is the high-level page fault handler.
  188.  * Interrupts are assumed disabled.
  189.  *
  190.  * @param page Faulting page.
  191.  *
  192.  * @return 0 on page fault, 1 on success.
  193.  */
  194. int as_page_fault(__address page)
  195. {
  196.     link_t *cur;
  197.     as_area_t *a, *area = NULL;
  198.     __address frame;
  199.    
  200.     ASSERT(AS);
  201.     spinlock_lock(&AS->lock);
  202.    
  203.     /*
  204.      * Search this areas of this address space for presence of 'page'.
  205.      */
  206.     for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) {
  207.         a = list_get_instance(cur, as_area_t, link);
  208.         spinlock_lock(&a->lock);
  209.  
  210.         if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
  211.  
  212.             /*
  213.              * We found the area containing 'page'.
  214.              * TODO: access checking
  215.              */
  216.             area = a;
  217.             break;
  218.         }
  219.        
  220.         spinlock_unlock(&a->lock);
  221.     }
  222.    
  223.     if (!area) {
  224.         /*
  225.          * No area contained mapping for 'page'.
  226.          * Signal page fault to low-level handler.
  227.          */
  228.         spinlock_unlock(&AS->lock);
  229.         return 0;
  230.     }
  231.  
  232.     /*
  233.      * Note: area->lock is held.
  234.      */
  235.    
  236.     /*
  237.      * In general, there can be several reasons that
  238.      * can have caused this fault.
  239.      *
  240.      * - non-existent mapping: the area is a scratch
  241.      *   area (e.g. stack) and so far has not been
  242.      *   allocated a frame for the faulting page
  243.      *
  244.      * - non-present mapping: another possibility,
  245.      *   currently not implemented, would be frame
  246.      *   reuse; when this becomes a possibility,
  247.      *   do not forget to distinguish between
  248.      *   the different causes
  249.      */
  250.     frame = frame_alloc(0, ONE_FRAME, NULL);
  251.     memsetb(PA2KA(frame), FRAME_SIZE, 0);
  252.    
  253.     /*
  254.      * Map 'page' to 'frame'.
  255.      * Note that TLB shootdown is not attempted as only new information is being
  256.      * inserted into page tables.
  257.      */
  258.     page_mapping_insert(AS, page, frame, get_area_flags(area));
  259.    
  260.     spinlock_unlock(&area->lock);
  261.     spinlock_unlock(&AS->lock);
  262.  
  263.     return 1;
  264. }
  265.  
  266. /** Install address space on CPU.
  267.  *
  268.  * @param as Address space.
  269.  */
  270. void as_install(as_t *as)
  271. {
  272.     ipl_t ipl;
  273.    
  274.     asid_install(as);
  275.    
  276.     ipl = interrupts_disable();
  277.     spinlock_lock(&as->lock);
  278.     ASSERT(as->page_table);
  279.     SET_PTL0_ADDRESS(as->page_table);
  280.     spinlock_unlock(&as->lock);
  281.     interrupts_restore(ipl);
  282.  
  283.     /*
  284.      * Perform architecture-specific steps.
  285.      * (e.g. write ASID to hardware register etc.)
  286.      */
  287.     as_install_arch(as);
  288.    
  289.     AS = as;
  290. }
  291.  
  292. /** Compute flags for virtual address translation subsytem.
  293.  *
  294.  * The address space area must be locked.
  295.  * Interrupts must be disabled.
  296.  *
  297.  * @param a Address space area.
  298.  *
  299.  * @return Flags to be used in page_mapping_insert().
  300.  */
  301. int get_area_flags(as_area_t *a)
  302. {
  303.     int flags;
  304.  
  305.     switch (a->type) {
  306.         case AS_AREA_TEXT:
  307.             flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
  308.             break;
  309.         case AS_AREA_DATA:
  310.         case AS_AREA_STACK:
  311.             flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
  312.             break;
  313.         default:
  314.             panic("unexpected as_area_type_t %d", a->type);
  315.     }
  316.    
  317.     return flags;
  318. }
  319.  
  320. /** Create page table.
  321.  *
  322.  * Depending on architecture, create either address space
  323.  * private or global page table.
  324.  *
  325.  * @param flags Flags saying whether the page table is for kernel address space.
  326.  *
  327.  * @return First entry of the page table.
  328.  */
  329. pte_t *page_table_create(int flags)
  330. {
  331.         ASSERT(as_operations);
  332.         ASSERT(as_operations->page_table_create);
  333.  
  334.         return as_operations->page_table_create(flags);
  335. }
  336.