ia64/xen-unstable

changeset 19255:c9e9602cc080

minios: general cleanup of mm.c

Major clean up of mm.c. Added comments, fixed coding style, more
sensible variable names, mark some local functions static etc.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@netronome.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 02 10:52:07 2009 +0000 (2009-03-02)
parents 08916e5135b3
children 4dac26349877
files extras/mini-os/arch/x86/mm.c extras/mini-os/arch/x86/setup.c extras/mini-os/kernel.c
line diff
     1.1 --- a/extras/mini-os/arch/x86/mm.c	Mon Mar 02 10:51:34 2009 +0000
     1.2 +++ b/extras/mini-os/arch/x86/mm.c	Mon Mar 02 10:52:07 2009 +0000
     1.3 @@ -52,20 +52,26 @@
     1.4  unsigned long *phys_to_machine_mapping;
     1.5  unsigned long mfn_zero;
     1.6  extern char stack[];
     1.7 -extern void page_walk(unsigned long virt_addr);
     1.8 +extern void page_walk(unsigned long va);
     1.9  
    1.10 -void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn, 
    1.11 -                                unsigned long offset, unsigned long level)
    1.12 +/*
    1.13 + * Make pt_pfn a new 'level' page table frame and hook it into the page
    1.14 + * table at offset in previous level MFN (pref_l_mfn). pt_pfn is a guest
    1.15 + * PFN.
    1.16 + */
    1.17 +static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn, 
    1.18 +                         unsigned long offset, unsigned long level)
    1.19  {   
    1.20      pgentry_t *tab = (pgentry_t *)start_info.pt_base;
    1.21      unsigned long pt_page = (unsigned long)pfn_to_virt(*pt_pfn); 
    1.22      pgentry_t prot_e, prot_t;
    1.23      mmu_update_t mmu_updates[1];
    1.24 +    int rc;
    1.25      
    1.26      prot_e = prot_t = 0;
    1.27 -    DEBUG("Allocating new L%d pt frame for pt_pfn=%lx, "
    1.28 -           "prev_l_mfn=%lx, offset=%lx", 
    1.29 -           level, *pt_pfn, prev_l_mfn, offset);
    1.30 +    DEBUG("Allocating new L%d pt frame for pfn=%lx, "
    1.31 +          "prev_l_mfn=%lx, offset=%lx", 
    1.32 +          level, *pt_pfn, prev_l_mfn, offset);
    1.33  
    1.34      /* We need to clear the page, otherwise we might fail to map it
    1.35         as a page table page */
    1.36 @@ -74,56 +80,63 @@ void new_pt_frame(unsigned long *pt_pfn,
    1.37      switch ( level )
    1.38      {
    1.39      case L1_FRAME:
    1.40 -         prot_e = L1_PROT;
    1.41 -         prot_t = L2_PROT;
    1.42 -         break;
    1.43 +        prot_e = L1_PROT;
    1.44 +        prot_t = L2_PROT;
    1.45 +        break;
    1.46      case L2_FRAME:
    1.47 -         prot_e = L2_PROT;
    1.48 -         prot_t = L3_PROT;
    1.49 -         break;
    1.50 +        prot_e = L2_PROT;
    1.51 +        prot_t = L3_PROT;
    1.52 +        break;
    1.53  #if defined(__x86_64__)
    1.54      case L3_FRAME:
    1.55 -         prot_e = L3_PROT;
    1.56 -         prot_t = L4_PROT;
    1.57 -         break;
    1.58 +        prot_e = L3_PROT;
    1.59 +        prot_t = L4_PROT;
    1.60 +        break;
    1.61  #endif
    1.62      default:
    1.63 -         printk("new_pt_frame() called with invalid level number %d\n", level);
    1.64 -         do_exit();
    1.65 -         break;
    1.66 +        printk("new_pt_frame() called with invalid level number %d\n", level);
    1.67 +        do_exit();
    1.68 +        break;
    1.69      }
    1.70  
    1.71 -    /* Update the entry */
    1.72 +    /* Make PFN a page table page */
    1.73  #if defined(__x86_64__)
    1.74      tab = pte_to_virt(tab[l4_table_offset(pt_page)]);
    1.75  #endif
    1.76      tab = pte_to_virt(tab[l3_table_offset(pt_page)]);
    1.77  
    1.78      mmu_updates[0].ptr = (tab[l2_table_offset(pt_page)] & PAGE_MASK) + 
    1.79 -                         sizeof(pgentry_t) * l1_table_offset(pt_page);
    1.80 +        sizeof(pgentry_t) * l1_table_offset(pt_page);
    1.81      mmu_updates[0].val = (pgentry_t)pfn_to_mfn(*pt_pfn) << PAGE_SHIFT | 
    1.82 -                         (prot_e & ~_PAGE_RW);
    1.83 -    if(HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF) < 0)
    1.84 +        (prot_e & ~_PAGE_RW);
    1.85 +    
    1.86 +    if ( (rc = HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF)) < 0 )
    1.87      {
    1.88 -         printk("PTE for new page table page could not be updated\n");
    1.89 -         do_exit();
    1.90 +        printk("ERROR: PTE for new page table page could not be updated\n");
    1.91 +        printk("       mmu_update failed with rc=%d\n", rc);
    1.92 +        do_exit();
    1.93      }
    1.94 -                        
    1.95 -    /* Now fill the new page table page with entries.
    1.96 -       Update the page directory as well. */
    1.97 -    mmu_updates[0].ptr = ((pgentry_t)prev_l_mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
    1.98 +
    1.99 +    /* Hook the new page table page into the hierarchy */
   1.100 +    mmu_updates[0].ptr =
   1.101 +        ((pgentry_t)prev_l_mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
   1.102      mmu_updates[0].val = (pgentry_t)pfn_to_mfn(*pt_pfn) << PAGE_SHIFT | prot_t;
   1.103 -    if(HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF) < 0) 
   1.104 +
   1.105 +    if ( (rc = HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF)) < 0 ) 
   1.106      {
   1.107 -       printk("ERROR: mmu_update failed\n");
   1.108 -       do_exit();
   1.109 +        printk("ERROR: mmu_update failed with rc=%d\n", rc);
   1.110 +        do_exit();
   1.111      }
   1.112  
   1.113      *pt_pfn += 1;
   1.114  }
   1.115  
   1.116 -/* Checks if a pagetable frame is needed (if weren't allocated by Xen) */
   1.117 -static int need_pt_frame(unsigned long virt_address, int level)
   1.118 +/*
   1.119 + * Checks if a pagetable frame is needed at 'level' to map a given
   1.120 + * address. Note, this function is specific to the initial page table
   1.121 + * building.
   1.122 + */
   1.123 +static int need_pt_frame(unsigned long va, int level)
   1.124  {
   1.125      unsigned long hyp_virt_start = HYPERVISOR_VIRT_START;
   1.126  #if defined(__x86_64__)
   1.127 @@ -135,63 +148,71 @@ static int need_pt_frame(unsigned long v
   1.128      /* In general frames will _not_ be needed if they were already
   1.129         allocated to map the hypervisor into our VA space */
   1.130  #if defined(__x86_64__)
   1.131 -    if(level == L3_FRAME)
   1.132 +    if ( level == L3_FRAME )
   1.133      {
   1.134 -        if(l4_table_offset(virt_address) >= 
   1.135 -           l4_table_offset(hyp_virt_start) &&
   1.136 -           l4_table_offset(virt_address) <= 
   1.137 -           l4_table_offset(hyp_virt_end))
   1.138 +        if ( l4_table_offset(va) >= 
   1.139 +             l4_table_offset(hyp_virt_start) &&
   1.140 +             l4_table_offset(va) <= 
   1.141 +             l4_table_offset(hyp_virt_end))
   1.142              return 0;
   1.143          return 1;
   1.144 -    } else
   1.145 +    } 
   1.146 +    else
   1.147  #endif
   1.148  
   1.149 -    if(level == L2_FRAME)
   1.150 +    if ( level == L2_FRAME )
   1.151      {
   1.152  #if defined(__x86_64__)
   1.153 -        if(l4_table_offset(virt_address) >= 
   1.154 -           l4_table_offset(hyp_virt_start) &&
   1.155 -           l4_table_offset(virt_address) <= 
   1.156 -           l4_table_offset(hyp_virt_end))
   1.157 +        if ( l4_table_offset(va) >= 
   1.158 +             l4_table_offset(hyp_virt_start) &&
   1.159 +             l4_table_offset(va) <= 
   1.160 +             l4_table_offset(hyp_virt_end))
   1.161  #endif
   1.162 -            if(l3_table_offset(virt_address) >= 
   1.163 -               l3_table_offset(hyp_virt_start) &&
   1.164 -               l3_table_offset(virt_address) <= 
   1.165 -               l3_table_offset(hyp_virt_end))
   1.166 +            if ( l3_table_offset(va) >= 
   1.167 +                 l3_table_offset(hyp_virt_start) &&
   1.168 +                 l3_table_offset(va) <= 
   1.169 +                 l3_table_offset(hyp_virt_end))
   1.170                  return 0;
   1.171  
   1.172          return 1;
   1.173 -    } else 
   1.174 -
   1.175 -    /* Always need l1 frames */
   1.176 -    if(level == L1_FRAME)
   1.177 -        return 1;
   1.178 +    } 
   1.179 +    else 
   1.180 +        /* Always need l1 frames */
   1.181 +        if ( level == L1_FRAME )
   1.182 +            return 1;
   1.183  
   1.184      printk("ERROR: Unknown frame level %d, hypervisor %llx,%llx\n", 
   1.185 -        level, hyp_virt_start, hyp_virt_end);
   1.186 +           level, hyp_virt_start, hyp_virt_end);
   1.187      return -1;
   1.188  }
   1.189  
   1.190 -void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
   1.191 +/*
   1.192 + * Build the initial pagetable.
   1.193 + */
   1.194 +static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
   1.195  {
   1.196      unsigned long start_address, end_address;
   1.197      unsigned long pfn_to_map, pt_pfn = *start_pfn;
   1.198      static mmu_update_t mmu_updates[L1_PAGETABLE_ENTRIES + 1];
   1.199      pgentry_t *tab = (pgentry_t *)start_info.pt_base, page;
   1.200 -    unsigned long mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.201 +    unsigned long pt_mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.202      unsigned long offset;
   1.203      int count = 0;
   1.204 +    int rc;
   1.205  
   1.206 -    pfn_to_map = (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
   1.207 +    pfn_to_map = 
   1.208 +        (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
   1.209  
   1.210 -    if (*max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START))
   1.211 +    if ( *max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START) )
   1.212      {
   1.213          printk("WARNING: Mini-OS trying to use Xen virtual space. "
   1.214                 "Truncating memory from %dMB to ",
   1.215 -               ((unsigned long)pfn_to_virt(*max_pfn) - (unsigned long)&_text)>>20);
   1.216 +               ((unsigned long)pfn_to_virt(*max_pfn) -
   1.217 +                (unsigned long)&_text)>>20);
   1.218          *max_pfn = virt_to_pfn(HYPERVISOR_VIRT_START - PAGE_SIZE);
   1.219          printk("%dMB\n",
   1.220 -               ((unsigned long)pfn_to_virt(*max_pfn) - (unsigned long)&_text)>>20);
   1.221 +               ((unsigned long)pfn_to_virt(*max_pfn) - 
   1.222 +                (unsigned long)&_text)>>20);
   1.223      }
   1.224  
   1.225      start_address = (unsigned long)pfn_to_virt(pfn_to_map);
   1.226 @@ -200,49 +221,53 @@ void build_pagetable(unsigned long *star
   1.227      /* We worked out the virtual memory range to map, now mapping loop */
   1.228      printk("Mapping memory range 0x%lx - 0x%lx\n", start_address, end_address);
   1.229  
   1.230 -    while(start_address < end_address)
   1.231 +    while ( start_address < end_address )
   1.232      {
   1.233          tab = (pgentry_t *)start_info.pt_base;
   1.234 -        mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.235 +        pt_mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.236  
   1.237  #if defined(__x86_64__)
   1.238          offset = l4_table_offset(start_address);
   1.239          /* Need new L3 pt frame */
   1.240 -        if(!(start_address & L3_MASK)) 
   1.241 -            if(need_pt_frame(start_address, L3_FRAME)) 
   1.242 -                new_pt_frame(&pt_pfn, mfn, offset, L3_FRAME);
   1.243 +        if ( !(start_address & L3_MASK) )
   1.244 +            if ( need_pt_frame(start_address, L3_FRAME) ) 
   1.245 +                new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
   1.246  
   1.247          page = tab[offset];
   1.248 -        mfn = pte_to_mfn(page);
   1.249 -        tab = to_virt(mfn_to_pfn(mfn) << PAGE_SHIFT);
   1.250 +        pt_mfn = pte_to_mfn(page);
   1.251 +        tab = to_virt(mfn_to_pfn(pt_mfn) << PAGE_SHIFT);
   1.252  #endif
   1.253          offset = l3_table_offset(start_address);
   1.254          /* Need new L2 pt frame */
   1.255 -        if(!(start_address & L2_MASK))
   1.256 -            if(need_pt_frame(start_address, L2_FRAME))
   1.257 -                new_pt_frame(&pt_pfn, mfn, offset, L2_FRAME);
   1.258 +        if ( !(start_address & L2_MASK) )
   1.259 +            if ( need_pt_frame(start_address, L2_FRAME) )
   1.260 +                new_pt_frame(&pt_pfn, pt_mfn, offset, L2_FRAME);
   1.261  
   1.262          page = tab[offset];
   1.263 -        mfn = pte_to_mfn(page);
   1.264 -        tab = to_virt(mfn_to_pfn(mfn) << PAGE_SHIFT);
   1.265 +        pt_mfn = pte_to_mfn(page);
   1.266 +        tab = to_virt(mfn_to_pfn(pt_mfn) << PAGE_SHIFT);
   1.267          offset = l2_table_offset(start_address);        
   1.268          /* Need new L1 pt frame */
   1.269 -        if(!(start_address & L1_MASK))
   1.270 -            if(need_pt_frame(start_address, L1_FRAME)) 
   1.271 -                new_pt_frame(&pt_pfn, mfn, offset, L1_FRAME);
   1.272 +        if ( !(start_address & L1_MASK) )
   1.273 +            if ( need_pt_frame(start_address, L1_FRAME) )
   1.274 +                new_pt_frame(&pt_pfn, pt_mfn, offset, L1_FRAME);
   1.275  
   1.276          page = tab[offset];
   1.277 -        mfn = pte_to_mfn(page);
   1.278 +        pt_mfn = pte_to_mfn(page);
   1.279          offset = l1_table_offset(start_address);
   1.280  
   1.281 -        mmu_updates[count].ptr = ((pgentry_t)mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
   1.282 -        mmu_updates[count].val = (pgentry_t)pfn_to_mfn(pfn_to_map++) << PAGE_SHIFT | L1_PROT;
   1.283 +        mmu_updates[count].ptr =
   1.284 +            ((pgentry_t)pt_mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
   1.285 +        mmu_updates[count].val = 
   1.286 +            (pgentry_t)pfn_to_mfn(pfn_to_map++) << PAGE_SHIFT | L1_PROT;
   1.287          count++;
   1.288 -        if (count == L1_PAGETABLE_ENTRIES || pfn_to_map == *max_pfn)
   1.289 +        if ( count == L1_PAGETABLE_ENTRIES || pfn_to_map == *max_pfn )
   1.290          {
   1.291 -            if(HYPERVISOR_mmu_update(mmu_updates, count, NULL, DOMID_SELF) < 0)
   1.292 +            rc = HYPERVISOR_mmu_update(mmu_updates, count, NULL, DOMID_SELF);
   1.293 +            if ( rc < 0 )
   1.294              {
   1.295 -                printk("PTE could not be updated\n");
   1.296 +                printk("ERROR: build_pagetable(): PTE could not be updated\n");
   1.297 +                printk("       mmu_update failed with rc=%d\n", rc);
   1.298                  do_exit();
   1.299              }
   1.300              count = 0;
   1.301 @@ -253,20 +278,26 @@ void build_pagetable(unsigned long *star
   1.302      *start_pfn = pt_pfn;
   1.303  }
   1.304  
   1.305 +/*
   1.306 + * Mark portion of the address space read only.
   1.307 + */
   1.308  extern void shared_info;
   1.309  static void set_readonly(void *text, void *etext)
   1.310  {
   1.311 -    unsigned long start_address = ((unsigned long) text + PAGE_SIZE - 1) & PAGE_MASK;
   1.312 +    unsigned long start_address =
   1.313 +        ((unsigned long) text + PAGE_SIZE - 1) & PAGE_MASK;
   1.314      unsigned long end_address = (unsigned long) etext;
   1.315      static mmu_update_t mmu_updates[L1_PAGETABLE_ENTRIES + 1];
   1.316      pgentry_t *tab = (pgentry_t *)start_info.pt_base, page;
   1.317      unsigned long mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.318      unsigned long offset;
   1.319      int count = 0;
   1.320 +    int rc;
   1.321  
   1.322      printk("setting %p-%p readonly\n", text, etext);
   1.323  
   1.324 -    while (start_address + PAGE_SIZE <= end_address) {
   1.325 +    while ( start_address + PAGE_SIZE <= end_address )
   1.326 +    {
   1.327          tab = (pgentry_t *)start_info.pt_base;
   1.328          mfn = pfn_to_mfn(virt_to_pfn(start_info.pt_base));
   1.329  
   1.330 @@ -287,20 +318,25 @@ static void set_readonly(void *text, voi
   1.331  
   1.332          offset = l1_table_offset(start_address);
   1.333  
   1.334 -	if (start_address != (unsigned long)&shared_info) {
   1.335 -	    mmu_updates[count].ptr = ((pgentry_t)mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
   1.336 -	    mmu_updates[count].val = tab[offset] & ~_PAGE_RW;
   1.337 -	    count++;
   1.338 -	} else
   1.339 -	    printk("skipped %p\n", start_address);
   1.340 +        if ( start_address != (unsigned long)&shared_info )
   1.341 +        {
   1.342 +            mmu_updates[count].ptr = 
   1.343 +                ((pgentry_t)mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
   1.344 +            mmu_updates[count].val = tab[offset] & ~_PAGE_RW;
   1.345 +            count++;
   1.346 +        }
   1.347 +        else
   1.348 +            printk("skipped %p\n", start_address);
   1.349  
   1.350          start_address += PAGE_SIZE;
   1.351  
   1.352 -        if (count == L1_PAGETABLE_ENTRIES || start_address + PAGE_SIZE > end_address)
   1.353 +        if ( count == L1_PAGETABLE_ENTRIES || 
   1.354 +             start_address + PAGE_SIZE > end_address )
   1.355          {
   1.356 -            if(HYPERVISOR_mmu_update(mmu_updates, count, NULL, DOMID_SELF) < 0)
   1.357 +            rc = HYPERVISOR_mmu_update(mmu_updates, count, NULL, DOMID_SELF);
   1.358 +            if ( rc < 0 )
   1.359              {
   1.360 -                printk("PTE could not be updated\n");
   1.361 +                printk("ERROR: set_readonly(): PTE could not be updated\n");
   1.362                  do_exit();
   1.363              }
   1.364              count = 0;
   1.365 @@ -308,11 +344,11 @@ static void set_readonly(void *text, voi
   1.366      }
   1.367  
   1.368      {
   1.369 -	mmuext_op_t op = {
   1.370 -	    .cmd = MMUEXT_TLB_FLUSH_ALL,
   1.371 -	};
   1.372 -	int count;
   1.373 -	HYPERVISOR_mmuext_op(&op, 1, &count, DOMID_SELF);
   1.374 +        mmuext_op_t op = {
   1.375 +            .cmd = MMUEXT_TLB_FLUSH_ALL,
   1.376 +        };
   1.377 +        int count;
   1.378 +        HYPERVISOR_mmuext_op(&op, 1, &count, DOMID_SELF);
   1.379      }
   1.380  }
   1.381  
   1.382 @@ -371,7 +407,10 @@ int mem_test(unsigned long *start_va, un
   1.383  }
   1.384  
   1.385  
   1.386 -static pgentry_t *get_pgt(unsigned long addr)
   1.387 +/*
   1.388 + * get the PTE for virtual address va if it exists. Otherwise NULL.
   1.389 + */
   1.390 +static pgentry_t *get_pgt(unsigned long va)
   1.391  {
   1.392      unsigned long mfn;
   1.393      pgentry_t *tab;
   1.394 @@ -381,67 +420,78 @@ static pgentry_t *get_pgt(unsigned long 
   1.395      mfn = virt_to_mfn(start_info.pt_base);
   1.396  
   1.397  #if defined(__x86_64__)
   1.398 -    offset = l4_table_offset(addr);
   1.399 -    if (!(tab[offset] & _PAGE_PRESENT))
   1.400 +    offset = l4_table_offset(va);
   1.401 +    if ( !(tab[offset] & _PAGE_PRESENT) )
   1.402          return NULL;
   1.403      mfn = pte_to_mfn(tab[offset]);
   1.404      tab = mfn_to_virt(mfn);
   1.405  #endif
   1.406 -    offset = l3_table_offset(addr);
   1.407 -    if (!(tab[offset] & _PAGE_PRESENT))
   1.408 +    offset = l3_table_offset(va);
   1.409 +    if ( !(tab[offset] & _PAGE_PRESENT) )
   1.410 +        return NULL;
   1.411 +    mfn = pte_to_mfn(tab[offset]);
   1.412 +    tab = mfn_to_virt(mfn);
   1.413 +    offset = l2_table_offset(va);
   1.414 +    if ( !(tab[offset] & _PAGE_PRESENT) )
   1.415          return NULL;
   1.416      mfn = pte_to_mfn(tab[offset]);
   1.417      tab = mfn_to_virt(mfn);
   1.418 -    offset = l2_table_offset(addr);
   1.419 -    if (!(tab[offset] & _PAGE_PRESENT))
   1.420 -        return NULL;
   1.421 -    mfn = pte_to_mfn(tab[offset]);
   1.422 -    tab = mfn_to_virt(mfn);
   1.423 -    offset = l1_table_offset(addr);
   1.424 +    offset = l1_table_offset(va);
   1.425      return &tab[offset];
   1.426  }
   1.427  
   1.428 -pgentry_t *need_pgt(unsigned long addr)
   1.429 +
   1.430 +/*
   1.431 + * return a valid PTE for a given virtual address. If PTE does not exist,
   1.432 + * allocate page-table pages.
   1.433 + */
   1.434 +pgentry_t *need_pgt(unsigned long va)
   1.435  {
   1.436 -    unsigned long mfn;
   1.437 +    unsigned long pt_mfn;
   1.438      pgentry_t *tab;
   1.439      unsigned long pt_pfn;
   1.440      unsigned offset;
   1.441  
   1.442      tab = (pgentry_t *)start_info.pt_base;
   1.443 -    mfn = virt_to_mfn(start_info.pt_base);
   1.444 +    pt_mfn = virt_to_mfn(start_info.pt_base);
   1.445  
   1.446  #if defined(__x86_64__)
   1.447 -    offset = l4_table_offset(addr);
   1.448 -    if (!(tab[offset] & _PAGE_PRESENT)) {
   1.449 +    offset = l4_table_offset(va);
   1.450 +    if ( !(tab[offset] & _PAGE_PRESENT) )
   1.451 +    {
   1.452          pt_pfn = virt_to_pfn(alloc_page());
   1.453 -        new_pt_frame(&pt_pfn, mfn, offset, L3_FRAME);
   1.454 +        new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
   1.455      }
   1.456      ASSERT(tab[offset] & _PAGE_PRESENT);
   1.457 -    mfn = pte_to_mfn(tab[offset]);
   1.458 -    tab = mfn_to_virt(mfn);
   1.459 +    pt_mfn = pte_to_mfn(tab[offset]);
   1.460 +    tab = mfn_to_virt(pt_mfn);
   1.461  #endif
   1.462 -    offset = l3_table_offset(addr);
   1.463 -    if (!(tab[offset] & _PAGE_PRESENT)) {
   1.464 +    offset = l3_table_offset(va);
   1.465 +    if ( !(tab[offset] & _PAGE_PRESENT) ) 
   1.466 +    {
   1.467          pt_pfn = virt_to_pfn(alloc_page());
   1.468 -        new_pt_frame(&pt_pfn, mfn, offset, L2_FRAME);
   1.469 +        new_pt_frame(&pt_pfn, pt_mfn, offset, L2_FRAME);
   1.470      }
   1.471      ASSERT(tab[offset] & _PAGE_PRESENT);
   1.472 -    mfn = pte_to_mfn(tab[offset]);
   1.473 -    tab = mfn_to_virt(mfn);
   1.474 -    offset = l2_table_offset(addr);
   1.475 -    if (!(tab[offset] & _PAGE_PRESENT)) {
   1.476 +    pt_mfn = pte_to_mfn(tab[offset]);
   1.477 +    tab = mfn_to_virt(pt_mfn);
   1.478 +    offset = l2_table_offset(va);
   1.479 +    if ( !(tab[offset] & _PAGE_PRESENT) )
   1.480 +    {
   1.481          pt_pfn = virt_to_pfn(alloc_page());
   1.482 -	new_pt_frame(&pt_pfn, mfn, offset, L1_FRAME);
   1.483 +        new_pt_frame(&pt_pfn, pt_mfn, offset, L1_FRAME);
   1.484      }
   1.485      ASSERT(tab[offset] & _PAGE_PRESENT);
   1.486 -    mfn = pte_to_mfn(tab[offset]);
   1.487 -    tab = mfn_to_virt(mfn);
   1.488 +    pt_mfn = pte_to_mfn(tab[offset]);
   1.489 +    tab = mfn_to_virt(pt_mfn);
   1.490  
   1.491 -    offset = l1_table_offset(addr);
   1.492 +    offset = l1_table_offset(va);
   1.493      return &tab[offset];
   1.494  }
   1.495  
   1.496 +/*
   1.497 + * Reserve an area of virtual address space for mappings and Heap
   1.498 + */
   1.499  static unsigned long demand_map_area_start;
   1.500  #ifdef __x86_64__
   1.501  #define DEMAND_MAP_PAGES ((128ULL << 30) / PAGE_SIZE)
   1.502 @@ -466,7 +516,8 @@ void arch_init_demand_mapping_area(unsig
   1.503  
   1.504      demand_map_area_start = (unsigned long) pfn_to_virt(cur_pfn);
   1.505      cur_pfn += DEMAND_MAP_PAGES;
   1.506 -    printk("Demand map pfns at %lx-%lx.\n", demand_map_area_start, pfn_to_virt(cur_pfn));
   1.507 +    printk("Demand map pfns at %lx-%lx.\n", 
   1.508 +           demand_map_area_start, pfn_to_virt(cur_pfn));
   1.509  
   1.510  #ifdef HAVE_LIBC
   1.511      cur_pfn++;
   1.512 @@ -477,93 +528,123 @@ void arch_init_demand_mapping_area(unsig
   1.513  #endif
   1.514  }
   1.515  
   1.516 +unsigned long allocate_ondemand(unsigned long n, unsigned long alignment)
   1.517 +{
   1.518 +    unsigned long x;
   1.519 +    unsigned long y = 0;
   1.520 +
   1.521 +    /* Find a properly aligned run of n contiguous frames */
   1.522 +    for ( x = 0;
   1.523 +          x <= DEMAND_MAP_PAGES - n; 
   1.524 +          x = (x + y + 1 + alignment - 1) & ~(alignment - 1) )
   1.525 +    {
   1.526 +        unsigned long addr = demand_map_area_start + x * PAGE_SIZE;
   1.527 +        pgentry_t *pgt = get_pgt(addr);
   1.528 +        for ( y = 0; y < n; y++, addr += PAGE_SIZE ) 
   1.529 +        {
   1.530 +            if ( !(addr & L1_MASK) )
   1.531 +                pgt = get_pgt(addr);
   1.532 +            if ( pgt )
   1.533 +            {
   1.534 +                if ( *pgt & _PAGE_PRESENT )
   1.535 +                    break;
   1.536 +                pgt++;
   1.537 +            }
   1.538 +        }
   1.539 +        if ( y == n )
   1.540 +            break;
   1.541 +    }
   1.542 +    if ( y != n )
   1.543 +    {
   1.544 +        printk("Failed to find %ld frames!\n", n);
   1.545 +        return 0;
   1.546 +    }
   1.547 +    return demand_map_area_start + x * PAGE_SIZE;
   1.548 +}
   1.549 +
   1.550 +/*
   1.551 + * Map an array of MFNs contiguously into virtual address space starting at
   1.552 + * va. map f[i*stride]+i*increment for i in 0..n-1.
   1.553 + */
   1.554  #define MAP_BATCH ((STACK_SIZE / 2) / sizeof(mmu_update_t))
   1.555 -void do_map_frames(unsigned long addr,
   1.556 -        unsigned long *f, unsigned long n, unsigned long stride,
   1.557 -	unsigned long increment, domid_t id, int may_fail, unsigned long prot)
   1.558 +void do_map_frames(unsigned long va,
   1.559 +                   unsigned long *mfns, unsigned long n, 
   1.560 +                   unsigned long stride, unsigned long incr, 
   1.561 +                   domid_t id, int may_fail,
   1.562 +                   unsigned long prot)
   1.563  {
   1.564      pgentry_t *pgt = NULL;
   1.565      unsigned long done = 0;
   1.566      unsigned long i;
   1.567      int rc;
   1.568  
   1.569 -    while (done < n) {
   1.570 -	unsigned long todo;
   1.571 +    if ( !mfns ) 
   1.572 +    {
   1.573 +        printk("do_map_frames: no mfns supplied\n");
   1.574 +        return;
   1.575 +    }
   1.576 +    DEBUG("va=%p n=0x%lx, mfns[0]=0x%lx stride=0x%lx incr=0x%lx prot=0x%lx\n",
   1.577 +          va, n, mfns[0], stride, incr, prot);
   1.578 + 
   1.579 +    while ( done < n )
   1.580 +    {
   1.581 +        unsigned long todo;
   1.582  
   1.583 -	if (may_fail)
   1.584 -	    todo = 1;
   1.585 -	else
   1.586 -	    todo = n - done;
   1.587 +        if ( may_fail )
   1.588 +            todo = 1;
   1.589 +        else
   1.590 +            todo = n - done;
   1.591  
   1.592 -	if (todo > MAP_BATCH)
   1.593 -		todo = MAP_BATCH;
   1.594 +        if ( todo > MAP_BATCH )
   1.595 +            todo = MAP_BATCH;
   1.596  
   1.597 -	{
   1.598 -	    mmu_update_t mmu_updates[todo];
   1.599 +        {
   1.600 +            mmu_update_t mmu_updates[todo];
   1.601  
   1.602 -	    for (i = 0; i < todo; i++, addr += PAGE_SIZE, pgt++) {
   1.603 -                if (!pgt || !(addr & L1_MASK))
   1.604 -                    pgt = need_pgt(addr);
   1.605 -		mmu_updates[i].ptr = virt_to_mach(pgt);
   1.606 -		mmu_updates[i].val = ((pgentry_t)(f[(done + i) * stride] + (done + i) * increment) << PAGE_SHIFT) | prot;
   1.607 -	    }
   1.608 +            for ( i = 0; i < todo; i++, va += PAGE_SIZE, pgt++) 
   1.609 +            {
   1.610 +                if ( !pgt || !(va & L1_MASK) )
   1.611 +                    pgt = need_pgt(va);
   1.612 +                
   1.613 +                mmu_updates[i].ptr = virt_to_mach(pgt) | MMU_NORMAL_PT_UPDATE;
   1.614 +                mmu_updates[i].val = ((pgentry_t)(mfns[(done + i) * stride] +
   1.615 +                                                  (done + i) * incr)
   1.616 +                                      << PAGE_SHIFT) | prot;
   1.617 +            }
   1.618  
   1.619 -	    rc = HYPERVISOR_mmu_update(mmu_updates, todo, NULL, id);
   1.620 -	    if (rc < 0) {
   1.621 -		if (may_fail)
   1.622 -		    f[done * stride] |= 0xF0000000;
   1.623 -		else {
   1.624 -		    printk("Map %ld (%lx, ...) at %p failed: %d.\n", todo, f[done * stride] + done * increment, addr, rc);
   1.625 +            rc = HYPERVISOR_mmu_update(mmu_updates, todo, NULL, id);
   1.626 +            if ( rc < 0 )
   1.627 +            {
   1.628 +                if (may_fail)
   1.629 +                    mfns[done * stride] |= 0xF0000000;
   1.630 +                else {
   1.631 +                    printk("Map %ld (%lx, ...) at %p failed: %d.\n",
   1.632 +                           todo, mfns[done * stride] + done * incr, va, rc);
   1.633                      do_exit();
   1.634 -		}
   1.635 -	    }
   1.636 -	}
   1.637 -
   1.638 -	done += todo;
   1.639 +                }
   1.640 +            }
   1.641 +        }
   1.642 +        done += todo;
   1.643      }
   1.644  }
   1.645  
   1.646 -unsigned long allocate_ondemand(unsigned long n, unsigned long alignment)
   1.647 +/*
   1.648 + * Map an array of MFNs contiguous into virtual address space. Virtual
   1.649 + * addresses are allocated from the on demand area.
   1.650 + */
   1.651 +void *map_frames_ex(unsigned long *mfns, unsigned long n, 
   1.652 +                    unsigned long stride, unsigned long incr,
   1.653 +                    unsigned long alignment,
   1.654 +                    domid_t id, int may_fail, unsigned long prot)
   1.655  {
   1.656 -    unsigned long x;
   1.657 -    unsigned long y = 0;
   1.658 +    unsigned long va = allocate_ondemand(n, alignment);
   1.659  
   1.660 -    /* Find a properly aligned run of n contiguous frames */
   1.661 -    for (x = 0; x <= DEMAND_MAP_PAGES - n; x = (x + y + 1 + alignment - 1) & ~(alignment - 1)) {
   1.662 -        unsigned long addr = demand_map_area_start + x * PAGE_SIZE;
   1.663 -        pgentry_t *pgt = get_pgt(addr);
   1.664 -        for (y = 0; y < n; y++, addr += PAGE_SIZE) {
   1.665 -            if (!(addr & L1_MASK))
   1.666 -                pgt = get_pgt(addr);
   1.667 -            if (pgt) {
   1.668 -                if (*pgt & _PAGE_PRESENT)
   1.669 -                    break;
   1.670 -                pgt++;
   1.671 -            }
   1.672 -        }
   1.673 -        if (y == n)
   1.674 -            break;
   1.675 -    }
   1.676 -    if (y != n) {
   1.677 -        printk("Failed to find %ld frames!\n", n);
   1.678 -        return 0;
   1.679 -    }
   1.680 -    return demand_map_area_start + x * PAGE_SIZE;
   1.681 -}
   1.682 -
   1.683 -void *map_frames_ex(unsigned long *f, unsigned long n, unsigned long stride,
   1.684 -	unsigned long increment, unsigned long alignment, domid_t id,
   1.685 -	int may_fail, unsigned long prot)
   1.686 -{
   1.687 -    unsigned long addr = allocate_ondemand(n, alignment);
   1.688 -
   1.689 -    if (!addr)
   1.690 +    if ( !va )
   1.691          return NULL;
   1.692  
   1.693 -    /* Found it at x.  Map it in. */
   1.694 -    do_map_frames(addr, f, n, stride, increment, id, may_fail, prot);
   1.695 +    do_map_frames(va, mfns, n, stride, incr, id, may_fail, prot);
   1.696  
   1.697 -    return (void *)addr;
   1.698 +    return (void *)va;
   1.699  }
   1.700  
   1.701  /*
   1.702 @@ -632,15 +713,19 @@ int mfn_is_ram(unsigned long mfn)
   1.703  }
   1.704  
   1.705  
   1.706 +/*
   1.707 + * Clear some of the bootstrap memory
   1.708 + */
   1.709  static void clear_bootstrap(void)
   1.710  {
   1.711      pte_t nullpte = { };
   1.712 +    int rc;
   1.713  
   1.714      /* Use first page as the CoW zero page */
   1.715      memset(&_text, 0, PAGE_SIZE);
   1.716      mfn_zero = virt_to_mfn((unsigned long) &_text);
   1.717 -    if (HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG))
   1.718 -	printk("Unable to unmap NULL page\n");
   1.719 +    if ( (rc = HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG)) )
   1.720 +        printk("Unable to unmap NULL page. rc=%d\n", rc);
   1.721  }
   1.722  
   1.723  void arch_init_p2m(unsigned long max_pfn)
   1.724 @@ -665,19 +750,19 @@ void arch_init_p2m(unsigned long max_pfn
   1.725      unsigned long pfn;
   1.726      
   1.727      l3_list = (unsigned long *)alloc_page(); 
   1.728 -    for(pfn=0; pfn<max_pfn; pfn++)
   1.729 +    for ( pfn=0; pfn<max_pfn; pfn++ )
   1.730      {
   1.731 -        if(!(pfn % (L1_P2M_ENTRIES * L2_P2M_ENTRIES)))
   1.732 +        if ( !(pfn % (L1_P2M_ENTRIES * L2_P2M_ENTRIES)) )
   1.733          {
   1.734              l2_list = (unsigned long*)alloc_page();
   1.735 -            if((pfn >> L3_P2M_SHIFT) > 0)
   1.736 +            if ( (pfn >> L3_P2M_SHIFT) > 0 )
   1.737              {
   1.738                  printk("Error: Too many pfns.\n");
   1.739                  do_exit();
   1.740              }
   1.741              l3_list[(pfn >> L2_P2M_SHIFT)] = virt_to_mfn(l2_list);  
   1.742          }
   1.743 -        if(!(pfn % (L1_P2M_ENTRIES)))
   1.744 +        if ( !(pfn % (L1_P2M_ENTRIES)) )
   1.745          {
   1.746              l1_list = (unsigned long*)alloc_page();
   1.747              l2_list[(pfn >> L1_P2M_SHIFT) & L2_P2M_MASK] = 
   1.748 @@ -696,25 +781,25 @@ void arch_init_mm(unsigned long* start_p
   1.749  
   1.750      unsigned long start_pfn, max_pfn, virt_pfns;
   1.751  
   1.752 -    printk("  _text:        %p\n", &_text);
   1.753 -    printk("  _etext:       %p\n", &_etext);
   1.754 -    printk("  _erodata:     %p\n", &_erodata);
   1.755 -    printk("  _edata:       %p\n", &_edata);
   1.756 -    printk("  stack start:  %p\n", stack);
   1.757 -    printk("  _end:         %p\n", &_end);
   1.758 +    printk("      _text: %p(VA)\n", &_text);
   1.759 +    printk("     _etext: %p(VA)\n", &_etext);
   1.760 +    printk("   _erodata: %p(VA)\n", &_erodata);
   1.761 +    printk("     _edata: %p(VA)\n", &_edata);
   1.762 +    printk("stack start: %p(VA)\n", stack);
   1.763 +    printk("       _end: %p(VA)\n", &_end);
   1.764  
   1.765      /* First page follows page table pages and 3 more pages (store page etc) */
   1.766      start_pfn = PFN_UP(to_phys(start_info.pt_base)) + 
   1.767 -                start_info.nr_pt_frames + 3;
   1.768 +        start_info.nr_pt_frames + 3;
   1.769      max_pfn = start_info.nr_pages;
   1.770  
   1.771      /* We need room for demand mapping and heap, clip available memory */
   1.772      virt_pfns = DEMAND_MAP_PAGES + HEAP_PAGES;
   1.773 -    if (max_pfn + virt_pfns + 1 < max_pfn)
   1.774 +    if ( max_pfn + virt_pfns + 1 < max_pfn )
   1.775          max_pfn = -(virt_pfns + 1);
   1.776  
   1.777 -    printk("  start_pfn:    %lx\n", start_pfn);
   1.778 -    printk("  max_pfn:      %lx\n", max_pfn);
   1.779 +    printk("  start_pfn: %lx\n", start_pfn);
   1.780 +    printk("    max_pfn: %lx\n", max_pfn);
   1.781  
   1.782      build_pagetable(&start_pfn, &max_pfn);
   1.783      clear_bootstrap();
     2.1 --- a/extras/mini-os/arch/x86/setup.c	Mon Mar 02 10:51:34 2009 +0000
     2.2 +++ b/extras/mini-os/arch/x86/setup.c	Mon Mar 02 10:52:07 2009 +0000
     2.3 @@ -63,10 +63,12 @@ void failsafe_callback(void);
     2.4  static
     2.5  shared_info_t *map_shared_info(unsigned long pa)
     2.6  {
     2.7 -	if ( HYPERVISOR_update_va_mapping(
     2.8 -		(unsigned long)shared_info, __pte(pa | 7), UVMF_INVLPG) )
     2.9 +    int rc;
    2.10 +
    2.11 +	if ( (rc = HYPERVISOR_update_va_mapping(
    2.12 +              (unsigned long)shared_info, __pte(pa | 7), UVMF_INVLPG)) )
    2.13  	{
    2.14 -		printk("Failed to map shared_info!!\n");
    2.15 +		printk("Failed to map shared_info!! rc=%d\n", rc);
    2.16  		do_exit();
    2.17  	}
    2.18  	return (shared_info_t *)shared_info;
     3.1 --- a/extras/mini-os/kernel.c	Mon Mar 02 10:51:34 2009 +0000
     3.2 +++ b/extras/mini-os/kernel.c	Mon Mar 02 10:52:07 2009 +0000
     3.3 @@ -490,14 +490,16 @@ void start_kernel(start_info_t *si)
     3.4  
     3.5      /* print out some useful information  */
     3.6      printk("Xen Minimal OS!\n");
     3.7 -    printk("start_info:   %p\n",    si);
     3.8 -    printk("  nr_pages:   %lu",     si->nr_pages);
     3.9 -    printk("  shared_inf: %08lx\n", si->shared_info);
    3.10 -    printk("  pt_base:    %p",      (void *)si->pt_base); 
    3.11 -    printk("  mod_start:  0x%lx\n", si->mod_start);
    3.12 -    printk("  mod_len:    %lu\n",   si->mod_len); 
    3.13 -    printk("  flags:      0x%x\n",  (unsigned int)si->flags);
    3.14 -    printk("  cmd_line:   %s\n",  
    3.15 +    printk("  start_info: %p(VA)\n", si);
    3.16 +    printk("    nr_pages: 0x%lx\n", si->nr_pages);
    3.17 +    printk("  shared_inf: 0x%08lx(MA)\n", si->shared_info);
    3.18 +    printk("     pt_base: %p(VA)\n", (void *)si->pt_base); 
    3.19 +    printk("nr_pt_frames: 0x%lx\n", si->nr_pt_frames);
    3.20 +    printk("    mfn_list: %p(VA)\n", (void *)si->mfn_list); 
    3.21 +    printk("   mod_start: 0x%lx(VA)\n", si->mod_start);
    3.22 +    printk("     mod_len: %lu\n", si->mod_len); 
    3.23 +    printk("       flags: 0x%x\n", (unsigned int)si->flags);
    3.24 +    printk("    cmd_line: %s\n",  
    3.25             si->cmd_line ? (const char *)si->cmd_line : "NULL");
    3.26  
    3.27      /* Set up events. */