⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paging-mon.c

📁 bochs : one pc simulator.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  plex86: run multiple x86 operating systems concurrently *  Copyright (C) 1999-2003 Kevin P. Lawton * *  paging-mon.c:  Virtualized (monitor) paging functionality. * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */#include "plex86.h"#define IN_MONITOR_SPACE#include "monitor.h"static unsigned allocatePT(vm_t *, unsigned pdi);static unsigned strengthenPagePermissions(vm_t *, phyPageInfo_t *usage,                  unsigned new_access_perm);/*static void sanity_check_pdir(vm_t *vm, unsigned id, Bit32u guest_laddr); *//* +++ fix retrieve mon pages function and .base issue *//*     also open_guest_phy_page expects shifted page val *//* +++ write_physical() has hack to ignore when perm!=RW, fix! *//* +++ add async handling in emulation.c, like in preGuest() *//* Cases which would generate a mon #PF *//* ==================================== *//* lazy map *//* r/w to current code page *//* guest #PF (access checks of cpl,rw) *//* w to RO construct *//* r/w to NA construct *//* inhibits */#if 0======= Old notes =====================================================IDT,GDT,LDT: limit = 64K; TR is a dont careWhat to do with PDir, PTbl?What to do about coherence probs with page tables and TLB?When are A,D bits copied between monitor and host?Need check for mapping of space used by monitorCode cache probably should not have laddr in itguest.PG==0, how are phy pages unmarked when constructs move?guest.PG transition: dump everything (flush)remapping descriptor tables after page flushmake sure to validate phy_attr everywhere before using it.checks for the phy_attr of page that PDir goes inpage fault because of monP?E.RW==0, but guestP?E==1/* +++ what about virtualized linear structs like GDT, IDT, ... */#endif#warning "Have to be careful unpinning a page which is open"#warning "  via open_guest_phy_page().  Multiple pages could be"#warning "  open in the page walk at one time until D/A bits are set."  static inline Bit32ugetHostOSPinnedPage(vm_t *vm, Bit32u ppi){  /* If physical page is already pinned by host OS, then we already   * know the physical address of the page.   */  if (vm->pageInfo[ppi].attr.fields.pinned)    return( vm->pageInfo[ppi].hostPPI );  /* Page is not already pinned by the host OS.  We need to request   * from the host OS, that this page is pinned and find the   * physical address.   */  toHostPinUserPage(vm, ppi);  if ( !vm->pageInfo[ppi].attr.fields.pinned )    monpanic(vm, "getHostOSPinnedPage: page was not marked pinned.\n");  return( vm->pageInfo[ppi].hostPPI );}  unsignedallocatePT(vm_t *vm, unsigned pdi){  unsigned map_i;  /* Allocate one of the (preallocated) pages for */  /* the monitor to use for a page table at the PDI given. */  map_i = vm->ptbl_laddr_map_i;  if (map_i >= MON_PAGE_TABLES) {    monpanic(vm, "allocatePT: out of page tables\n");    }#if ANAL_CHECKS  if (vm->guest.addr.page_tbl_laddr_map[pdi] != -1) {    monprint(vm, "allocatePT: check failed.\n");    monpanic(vm, "  pdi=0x%x, laddr_map=0x%x\n",      pdi, vm->guest.addr.page_tbl_laddr_map[pdi]);    }#endif  vm->guest.addr.page_tbl_laddr_map[pdi] = map_i;  vm->ptbl_laddr_map_i++;  return(map_i);}  unsignedgetMonPTi(vm_t *vm, unsigned pdi, unsigned source){  unsigned map_i;  map_i = vm->guest.addr.page_tbl_laddr_map[pdi];#if ANAL_CHECKS  if (map_i == -1) {    monprint(vm, "getMonPTi: check failed.\n");    monpanic(vm, "  pdi=0x%x, map_i=0x%x, source=%u\n",      pdi, map_i, source);    }  if (map_i >= MON_PAGE_TABLES)    monpanic(vm, "getMonPTi: map_i OOB\n");#endif  return(map_i);}/* Invalidate the mapping of a guest page in the monitor. * When situations change, such as a change in the permissions * necessary to virtualize the page properly, we'll need to do * this first, before remapping with the new permissions. */  unsignedstrengthenPagePermissions(vm_t *vm, phyPageInfo_t *pusage,                     unsigned new_access_perm){  pusage->attr.fields.access_perm = new_access_perm;  if (pusage->attr.fields.lmap_count == 0) {    /* No linear addresses are mapped to this phy page yet.     * Nothing to do. */    return 0;    }  else if (pusage->attr.fields.lmap_count == 1) {    /* One linear address is mapped to this phy page. */    Bit32u pdi, pti;    pageEntry_t *monPDE, *monPTE;    page_t      *monPTbl;    unsigned map_i;    pdi = (pusage->attr.fields.laddr_backlink >> 10);    pti = (pusage->attr.fields.laddr_backlink & 0x3ff);    monPDE = &vm->guest.addr.page_dir[pdi];    if ( !monPDE->fields.P )      monpanic(vm, "strengthenPP: monPDE.P==0\n");    map_i = getMonPTi(vm,pdi,10);    monPTbl = &vm->guest.addr.page_tbl[map_i];    monPTE = &monPTbl->pte[pti];    if ( !monPTE->fields.P ) {/*monprint(vm, "strengthenPP: bl=0x%x, AP=%u\n", *pusage->attr.fields.laddr_backlink, new_access_perm); */      /*monpanic(vm, "strengthenPP: monPTE.P==0\n"); */      }    else if (pusage->attr.fields.access_perm==PagePermNA) {      /* Permissions were changed to No Access */      monPTE->raw = 0;      }    else if (pusage->attr.fields.access_perm==PagePermRO) {      /* Permissions were changed to RO */      monPTE->fields.RW = 0;      }    else {      monpanic(vm, "strengthenPP: PagePermRW\n");      }    /* Flush the old TLB entry */    invlpg_mon_offset(      Guest2Monitor(vm, pusage->attr.fields.laddr_backlink<<12)      );    return 0;    }  else {    /* Multiple linear addresses are mapped to this phy page. */    /* Since we dont store enough backlink info to virtualize all */    /* linear addresses which point to this phy page, we have to dump */    /* all dynamic mappings and start over. */monpanic(vm, "strengthenPP: multiple lin addr\n");/*monPagingRemap(vm);*/    return 1;    }}  unsignedaddPageAttributes(vm_t *vm, Bit32u ppi, Bit32u req_attr){  phyPageInfo_t *pusage;  unsigned new_access_perm;  VM_ASSERT(vm, ppi < vm->pages.guest_n_pages);  pusage = &vm->pageInfo[ppi];  if (pusage->tsc < vm->vpaging_tsc) {    /* The dynamic attributes for this page are not valid since     * the last remap.  getPageUsage() has logic to build attributes.     */    getPageUsage(vm, ppi);    }  /* Build new attributes based on old ones, and requested ones. */  pusage->attr.raw |= req_attr;  /* Look at strength of new access restrictions */  if (pusage->attr.raw & PageUsageCausesNA)    new_access_perm = PagePermNA;  else if (pusage->attr.raw & PageUsageCausesRO)    new_access_perm = PagePermRO;  else    new_access_perm = PagePermRW;  if (new_access_perm > pusage->attr.fields.access_perm) {    /* New usage causes a stronger access restriction.  Remap them. */    return( strengthenPagePermissions(vm, pusage, new_access_perm) );    }  return 0;}  phyPageInfo_t *getPageUsage(vm_t *vm, Bit32u ppi){  phyPageInfo_t *pusage;  VM_ASSERT(vm, ppi < vm->pages.guest_n_pages);  pusage = &vm->pageInfo[ppi];  if (pusage->tsc < vm->vpaging_tsc) {    /* The dynamic attributes for this page are not valid since     * the last remap.  Clear them out, and timestamp.     */    pusage->tsc = vm_rdtsc();    pusage->attr.raw &= PageUsageSticky;    if (pusage->attr.raw & PageUsageCausesNA)      pusage->attr.fields.access_perm = PagePermNA;    else if (pusage->attr.raw & PageUsageCausesRO)      pusage->attr.fields.access_perm = PagePermRO;    else      pusage->attr.fields.access_perm = PagePermRW;    }  return(pusage);}  void *open_guest_phy_page(vm_t *vm, Bit32u ppi, Bit8u *mon_offset){  page_t *pageTable;  Bit32u  pti, mon_range_offset;  VM_ASSERT(vm, ppi < vm->pages.guest_n_pages);  /* Since we rewind our CS/DS.base so that the beginning of our */  /* monitor pages land on the beginning of a new 4Meg boundary */  /* (separate PDE), find out what mon_offset is in terms of */  /* an offset from the beginning of the PDE boundary. */  mon_range_offset = ( ((Bit32u) mon_offset) -                       kernelModulePages.startOffsetPageAligned );  pti = (mon_range_offset >> 12) & 0x3ff;  pageTable = vm->guest.addr.nexus_page_tbl;  /* Remap the base field.  All the rest of the fields are */  /* set previously, and can remain the same. */  pageTable->pte[pti].fields.base = getHostOSPinnedPage(vm, ppi);  invlpg_mon_offset( (Bit32u) mon_offset );  return(mon_offset);}  voidclose_guest_phy_page(vm_t *vm, Bit32u ppi){  /* ppi is >> 12 already */  /* +++ */}  voidvirtualize_lconstruct(vm_t *vm, Bit32u l0, Bit32u l1, unsigned perm){  /* Mark pages for a protected construct in linear space as */  /* virtualized (protected), if it is mapped into monitor space. */  /* Pages which are not yet mapped in, are virtualized dynamically */  /* when they are mapped in. */  Bit32u pdi, pdi0, pdi1, pti, pti0, pti1;  pageEntry_t *monPDE, *monPTE;  page_t      *monPTbl;/* +++ For now, can just dump all page mappings and start over *//*     again.  Need to complete this function, so we can virtualize *//*     only those pages which need it, and keep the other ones. *//* +++ Need to look at perm also. */monpanic(vm, "vir_lconstruct: unfinished.\n");/*monPagingRemap(vm);*/return;  if (vm->guest.addr.guest_cpu->cr0.fields.pg)    monpanic(vm, "virtualize_lconstruct: guest PG==1\n");  if (l0 >= l1)    monpanic(vm, "virtualize_lconstruct: l0>=l1!\n");  if ( (l1-l0) > (64*1024) )    monpanic(vm, "virtualize_lconstruct: span is > 64k!\n");  pdi0 = l0 >> 22;  pdi1 = l1 >> 22;  pti0 = (l0 >> 12) & 0x000003ff;  for (pdi=pdi0; pdi<=pdi1; pdi++) {    if ( pdi == vm->mon_pdi )      monpanic(vm, "virtualize_lconstruct: conflict with monitor space\n");    monPDE = &vm->guest.addr.page_dir[pdi];    if (monPDE->fields.P) {      if (pdi<pdi1)        pti1 = 0x3ff; /* spans multiple pdi's, use last index of range */      else        pti1 = (l1 >> 12) & 0x000003ff; /* use index of last address */      for (pti=pti0; pti<=pti1; pti++) {/* +++ *//* +++ FIX THIS!!!, set depending on guest.CR0.PG *//* +++ */        monPTbl = &vm->guest.addr.page_tbl[pdi];        monPTE = &monPTbl->pte[pti];        if (monPTE->fields.P) {          /* +++ finish this! */          /* The physical page for this linear address is allocated */          /* and mapped into the monitor.  We can access the attributes */          /* for this physical page.  Even if it has been virtualized */          /* before, we still need to mark it since it could have been */          /* virtualized due to a physical page constraint. */          monpanic(vm, "virtualize_lconstruct: finish.\n");          }        }      }    pti0 = 0; /* start address at boundary of next PDI */    }}  unsignedmapGuestLinAddr(vm_t *vm, Bit32u guest_laddr, Bit32u *guest_ppi,                unsigned req_us, unsigned req_rw, Bit32u attr,                Bit32u *error){  Bit32u       pdi, pti;  Bit32u       guest_lpage_index, ptbl_ppi;  page_t      *monPTbl;  pageEntry_t *monPDE, *monPTE;  pageEntry_t *guestPDir, guestPDE, *guestPTbl, guestPTE;  Bit32u       guest_pdir_page_index;  unsigned     pt_index, us, rw;  phyPageInfo_t *pusage;  unsigned wasRemap = 0;  guest_lpage_index = guest_laddr >> 12;  pdi = guest_lpage_index >> 10;  pti = guest_lpage_index & 0x3ff;  monPDE = &vm->guest.addr.page_dir[pdi];  if (vm->guest.addr.guest_cpu->cr0.fields.pg) {    /* Check out the guest's mapping of this address to see */    /* if would allow for an access. */    /* First, get the guest PDE */    guest_pdir_page_index = A20Addr(vm, vm->guest.addr.guest_cpu->cr3) >> 12;    if (guest_pdir_page_index >= vm->pages.guest_n_pages)      monpanic(vm, "mapGuestLinAddr: PG=1 guest PDE OOB\n");    /* Open a window into guest physical memory */    guestPDir = open_guest_phy_page(vm, guest_pdir_page_index,                                    vm->guest.addr.tmp_phy_page0);    guestPDE = guestPDir[pdi];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -