page.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 Martin Decky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <arch/mm/page.h>
00036 #include <genarch/mm/page_pt.h>
00037 #include <arch/mm/frame.h>
00038 #include <arch/asm.h>
00039 #include <mm/frame.h>
00040 #include <mm/page.h>
00041 #include <mm/as.h>
00042 #include <arch.h>
00043 #include <arch/types.h>
00044 #include <arch/exception.h>
00045 #include <align.h>
00046 #include <config.h>
00047 #include <print.h>
00048 #include <symtab.h>
00049 
00050 static phte_t *phte;
00051 
00052 
00068 static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, int access,
00069                                      istate_t *istate, int *pfrc)
00070 {
00071         /*
00072          * Check if the mapping exists in page tables.
00073          */     
00074         pte_t *pte = page_mapping_find(as, badvaddr);
00075         if ((pte) && (pte->p)) {
00076                 /*
00077                  * Mapping found in page tables.
00078                  * Immediately succeed.
00079                  */
00080                 return pte;
00081         } else {
00082                 int rc;
00083         
00084                 /*
00085                  * Mapping not found in page tables.
00086                  * Resort to higher-level page fault handler.
00087                  */
00088                 page_table_unlock(as, lock);
00089                 switch (rc = as_page_fault(badvaddr, access, istate)) {
00090                         case AS_PF_OK:
00091                                 /*
00092                                  * The higher-level page fault handler succeeded,
00093                                  * The mapping ought to be in place.
00094                                  */
00095                                 page_table_lock(as, lock);
00096                                 pte = page_mapping_find(as, badvaddr);
00097                                 ASSERT((pte) && (pte->p));
00098                                 return pte;
00099                         case AS_PF_DEFER:
00100                                 page_table_lock(as, lock);
00101                                 *pfrc = rc;
00102                                 return NULL;
00103                         case AS_PF_FAULT:
00104                                 page_table_lock(as, lock);
00105                                 printf("Page fault.\n");
00106                                 *pfrc = rc;
00107                                 return NULL;
00108                         default:
00109                                 panic("unexpected rc (%d)\n", rc);
00110                 }       
00111         }
00112 }
00113 
00114 
00115 static void pht_refill_fail(__address badvaddr, istate_t *istate)
00116 {
00117         char *symbol = "";
00118         char *sym2 = "";
00119 
00120         char *s = get_symtab_entry(istate->pc);
00121         if (s)
00122                 symbol = s;
00123         s = get_symtab_entry(istate->lr);
00124         if (s)
00125                 sym2 = s;
00126         panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
00127 }
00128 
00129 
00130 static void pht_insert(const __address vaddr, const pfn_t pfn)
00131 {
00132         __u32 page = (vaddr >> 12) & 0xffff;
00133         __u32 api = (vaddr >> 22) & 0x3f;
00134         __u32 vsid;
00135         
00136         asm volatile (
00137                 "mfsrin %0, %1\n"
00138                 : "=r" (vsid)
00139                 : "r" (vaddr)
00140         );
00141         
00142         /* Primary hash (xor) */
00143         __u32 h = 0;
00144         __u32 hash = vsid ^ page;
00145         __u32 base = (hash & 0x3ff) << 3;
00146         __u32 i;
00147         bool found = false;
00148         
00149         /* Find unused or colliding
00150            PTE in PTEG */
00151         for (i = 0; i < 8; i++) {
00152                 if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) && (phte[base + i].api == api))) {
00153                         found = true;
00154                         break;
00155                 }
00156         }
00157         
00158         if (!found) {
00159                 /* Secondary hash (not) */
00160                 __u32 base2 = (~hash & 0x3ff) << 3;
00161                 
00162                 /* Find unused or colliding
00163                    PTE in PTEG */
00164                 for (i = 0; i < 8; i++) {
00165                         if ((!phte[base2 + i].v) || ((phte[base2 + i].vsid == vsid) && (phte[base2 + i].api == api))) {
00166                                 found = true;
00167                                 base = base2;
00168                                 h = 1;
00169                                 break;
00170                         }
00171                 }
00172                 
00173                 if (!found) {
00174                         // TODO: A/C precedence groups
00175                         i = page % 8;
00176                 }
00177         }
00178         
00179         phte[base + i].v = 1;
00180         phte[base + i].vsid = vsid;
00181         phte[base + i].h = h;
00182         phte[base + i].api = api;
00183         phte[base + i].rpn = pfn;
00184         phte[base + i].r = 0;
00185         phte[base + i].c = 0;
00186         phte[base + i].pp = 2; // FIXME
00187 }
00188 
00189 
00196 void pht_refill(bool data, istate_t *istate)
00197 {
00198         __address badvaddr;
00199         pte_t *pte;
00200         int pfrc;
00201         as_t *as;
00202         bool lock;
00203         
00204         if (AS == NULL) {
00205                 as = AS_KERNEL;
00206                 lock = false;
00207         } else {
00208                 as = AS;
00209                 lock = true;
00210         }
00211         
00212         if (data) {
00213                 asm volatile (
00214                         "mfdar %0\n"
00215                         : "=r" (badvaddr)
00216                 );
00217         } else
00218                 badvaddr = istate->pc;
00219                 
00220         page_table_lock(as, lock);
00221         
00222         pte = find_mapping_and_check(as, lock, badvaddr, PF_ACCESS_READ /* FIXME */, istate, &pfrc);
00223         if (!pte) {
00224                 switch (pfrc) {
00225                         case AS_PF_FAULT:
00226                                 goto fail;
00227                                 break;
00228                         case AS_PF_DEFER:
00229                                 /*
00230                                  * The page fault came during copy_from_uspace()
00231                                  * or copy_to_uspace().
00232                                  */
00233                                 page_table_unlock(as, lock);
00234                                 return;
00235                         default:
00236                                 panic("Unexpected pfrc (%d)\n", pfrc);
00237                 }
00238         }
00239         
00240         pte->a = 1; /* Record access to PTE */
00241         pht_insert(badvaddr, pte->pfn);
00242         
00243         page_table_unlock(as, lock);
00244         return;
00245         
00246 fail:
00247         page_table_unlock(as, lock);
00248         pht_refill_fail(badvaddr, istate);
00249 }
00250 
00251 
00252 void pht_init(void)
00253 {
00254         memsetb((__address) phte, 1 << PHT_BITS, 0);
00255 }
00256 
00257 
00258 void page_arch_init(void)
00259 {
00260         if (config.cpu_active == 1) {
00261                 page_mapping_operations = &pt_mapping_operations;
00262                 
00263                 __address cur;
00264                 int flags;
00265                 
00266                 /* Frames below 128 MB are mapped using BAT,
00267                    map rest of the physical memory */
00268                 for (cur = 128 << 20; cur < last_frame; cur += FRAME_SIZE) {
00269                         flags = PAGE_CACHEABLE;
00270                         if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size))
00271                                 flags |= PAGE_GLOBAL;
00272                         page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags);
00273                 }
00274                 
00275                 /* Allocate page hash table */
00276                 phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC));
00277                 phte = (phte_t *) PA2KA((__address) physical_phte);
00278                 
00279                 ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0);
00280                 pht_init();
00281                 
00282                 asm volatile (
00283                         "mtsdr1 %0\n"
00284                         :
00285                         : "r" ((__address) physical_phte)
00286                 );
00287         }
00288 }
00289 
00290 
00291 __address hw_map(__address physaddr, size_t size)
00292 {
00293         if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH))
00294                 panic("Unable to map physical memory %p (%d bytes)", physaddr, size)
00295         
00296         __address virtaddr = PA2KA(last_frame);
00297         pfn_t i;
00298         for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++)
00299                 page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE);
00300         
00301         last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE);
00302         
00303         return virtaddr;
00304 }
00305 

Generated on Sun Jun 18 17:28:04 2006 for HelenOS Kernel (ppc64) by  doxygen 1.4.6