?? mmui86lib.c
字號:
/* mmuI86Lib.c - MMU library for i86 *//* Copyright 1984-2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01i,19nov02,max Added DIAB inline assembling01h,16may02,hdn moved the GDT reloading to sysPhysMemTop() in sysLib.c01g,09nov01,hdn deferred TLB flush in mmuVirtualPageCreate() at init time01f,27aug01,hdn made MMU_TLB_FLUSH, updated MMU_LOCK/MMU_UNLOCK macros. deferred TLB & Cache flush at initialization time. renamed mmuLibInit to mmuI86LibInit. doc: cleanup.01e,09feb99,wsl add comment to document ERRNO value01d,13apr98,hdn added support for PentiumPro.01c,07jan95,hdn re-initialized the GDT in mmuLibInit().01b,01nov94,hdn added a support for COPY_BACK cache mode for Pentium.01a,26jul93,hdn written based on mc68k's version.*//*DESCRIPTION:mmuI86Lib.c provides the architecture dependent routines that directly controlthe memory management unit. It provides 10 routines that are called by thehigher level architecture independent routines in vmLib.c: mmuI86LibInit() - initialize module mmuTransTblCreate() - create a new translation table mmuTransTblDelete() - delete a translation table. mmuI86Enable() - turn MMU on or off mmuStateSet() - set state of virtual memory page mmuStateGet() - get state of virtual memory page mmuPageMap() - map physical memory page to virtual memory page mmuGlobalPageMap() - map physical memory page to global virtual memory page mmuTranslate() - translate a virtual address to a physical address mmuCurrentSet() - change active translation tableApplications using the MMU will never call these routines directly; the visible interface is supported in vmLib.c.mmuLib supports the creation and maintenance of multiple translation tables,one of which is the active translation table when the MMU is enabled. Note that VxWorks does not include a translation table as part of the taskcontext; individual tasks do not reside in private virtual memory. However,we include the facilities to create multiple translation tables so thatthe user may create "private" virtual memory contexts and switch them in anapplication specific manner. Newtranslation tables are created with a call to mmuTransTblCreate(), and installedas the active translation table with mmuCurrentSet(). Translation tablesare modified and potentially augmented with calls to mmuPageMap() and mmuStateSet().The state of portions of the translation table can be read with calls to mmuStateGet() and mmuTranslate().The traditional VxWorks architecture and design philosophy requires that allobjects and operating systems resources be visible and accessible to all agents(tasks, isrs, watchdog timers, etc) in the system. This has traditionally beeninsured by the fact that all objects and data structures reside in physical memory; thus, a data structure created by one agent may be accessed by anyother agent using the same pointer (object identifiers in VxWorks are oftenpointers to data structures.) This creates a potential problem if you have multiple virtual memory contexts. For example, if asemaphore is created in one virtual memory context, you must guarantee thatthat semaphore will be visible in all virtual memory contexts if the semaphoreis to be accessed at interrupt level, when a virtual memory context other thanthe one in which it was created may be active. Another example is thatcode loaded using the incremental loader from the shell must be accessiblein all virtual memory contexts, since code is shared by all agents in thesystem.This problem is resolved by maintaining a global "transparent" mappingof virtual to physical memory for all the contiguous segments of physical memory (on board memory, i/o space, sections of vme space, etc) that is sharedby all translation tables; all available physical memory appears at the same address in virtual memory in all virtual memory contexts. This technique provides an environment that allowsresources that rely on a globally accessible physical address to run withoutmodification in a system with multiple virtual memory contexts.An additional requirement is that modifications made to the state of global virtual memory in one translation table appear in all translation tables. Forexample, memory containing the text segment is made read only (to avoidaccidental corruption) by setting the appropriate writable bits in the translation table entries corresponding to the virtual memory containing the text segment. This state information must be shared by all virtual memory contexts, so that no matter what translation table is active, the text segmentis protected from corruption. The mechanism that implements this feature isarchitecture dependent, but usually entails building a section of a translation table that corresponds to the global memory, that is shared byall other translation tables. Thus, when changes to the state of the globalmemory are made in one translation table, the changes are reflected in allother translation tables.mmuLib provides a separate call for constructing global virtual memory -mmuGlobalPageMap() - which creates translation table entries that are sharedby all translation tables. Initialization code in usrConfig makes callsto vmGlobalMap() (which in turn calls mmuGlobalPageMap()) to set up global transparent virtual memory for allavailable physical memory. All calls made to mmuGlobalPageMap() must occurbefore any virtual memory contexts are created; changes made to global virtualmemory after virtual memory contexts are created are not guaranteed to be reflected in all virtual memory contexts.Most MMU architectures will dedicate some fixed amount of virtual memory to a minimal section of the translation table (a "segment", or "block"). This creates a problem in that the user may map a small section of virtual memoryinto the global translation tables, and then attempt to use the virtual memoryafter this section as private virtual memory. The problem is that the translation table entries for this virtual memory are contained in the global translation tables, and are thus shared by all translation tables. This condition is detected by vmMap, and an error is returned, thus, the lowerlevel routines in mmuI86Lib.c (mmuPageMap(), mmuGlobalPageMap()) need not perform any error checking.A global variable `mmuPageBlockSize' should be defined which is equal to the minimum virtual segment size. mmuLib must provide a routine mmuGlobalInfoGet(), which returns a pointer to the globalPageBlock[] array.This provides the user with enough information to be able to allocate virtual memory space that does not conflict with the global memory space.This module supports the 80386/80486 MMU:.CS PDBR | | ------------------------------------- top level |pde |pde |pde |pde |pde |pde | ... ------------------------------------- | | | | | | | | | | | | ---------- | v v v v | ------ NULL NULL NULL NULL | | v v ---- ---- l |pte | |pte |o ---- ----w |pte | |pte | e ---- ----r |pte | |pte |l ---- ----e |pte | |pte |v ---- ----e . .l . . . ..CEwhere the top level consists of an array of pointers (Page Directory Entry)held within a single 4k page. These point to arrays of Page Table Entry arrays in the lower level. Each of these lower level arrays is also held within a single 4k page, and describes a virtual space of 4 MB (each Page Table Entry is 4 bytes, so we get 1000 of these in each array, and each PageTable Entry maps a 4KB page - thus 1000 * 4096 = 4MB.) To implement global virtual memory, a separate translation table called mmuGlobalTransTbl is created when the module is initialized. Calls to mmuGlobalPageMap will augment and modify this translation table. When newtranslation tables are created, memory for the top level array of sftd's isallocated and initialized by duplicating the pointers in mmuGlobalTransTbl'stop level sftd array. Thus, the new translation table will use the globaltranslation table's state information for portions of virtual memory that aredefined as global. Here's a picture to illustrate:.CS GLOBAL TRANS TBL NEW TRANS TBL PDBR PDBR | | | | ------------------------- ------------------------- top level |pde |pde | NULL| NULL| |pde |pde | NULL| NULL| ------------------------- ------------------------- | | | | | | | | | | | | | | | | ---------- | v v ---------- | v v | ------ NULL NULL | | NULL NULL | | | | o------------------------------------ | | | | | o----------------------------------------- | | v v ---- ---- l |pte | |pte |o ---- ----w |pte | |pte | e ---- ----r |pte | |pte |l ---- ----e |pte | |pte |v ---- ----e . .l . . . ..CENote that with this scheme, the global memory granularity is 4MB. Each timeyou map a section of global virtual memory, you dedicate at least 4MB of the virtual space to global virtual memory that will be shared by all virtualmemory contexts.The physical memory that holds these data structures is obtained from thesystem memory manager via memalign to insure that the memory is pagealigned. We want to protect this memory from being corrupted,so we invalidate the descriptors that we set up in the global translationthat correspond to the memory containing the translation table data structures.This creates a "chicken and the egg" paradox, in that the only way we canmodify these data structures is through virtual memory that is now invalidated,and we can't validate it because the page descriptors for that memory arein invalidated memory (confused yet?)So, you will notice that anywhere that page table descriptors (pte's)are modified, we do so by locking out interrupts, momentarily disabling the MMU, accessing the memory with its physical address, enabling the MMU, andthen re-enabling interrupts (see mmuStateSet(), for example.)The 80386 MMU does not have a write protection mechanism, since there is noWP(Write Protect) bit in CR0 unlike 80486 MMU. The 80386 has no internalcache, thus precluding control caching on a page-by-page basis. Also there is neither a PWT(Page Write Through) bit nor PCD(Page Cache Disable) bit in the Page Table Entry.The 80486 MMU set a PWT(Page Write Through) bit all the time and uses PCD(Page Cache Disable) bit to control caching on a page-by-page basis.The Pentium MMU set a PWT(Page Write Through) bit if the cacheDataMode isWRITE_THROUGH. If it is COPY_BACK, it doesn't set the bit.*/#include "vxWorks.h"#include "string.h"#include "intLib.h"#include "stdlib.h"#include "memLib.h"#include "private/vmLibP.h"#include "arch/i86/mmuI86Lib.h"#include "mmuLib.h"#include "errno.h"#include "cacheLib.h"#include "regs.h"/* forward declarations */ LOCAL void mmuMemPagesWriteDisable (MMU_TRANS_TBL *transTbl);LOCAL STATUS mmuPteGet (MMU_TRANS_TBL *pTransTbl, void *virtAddr, PTE **result);LOCAL MMU_TRANS_TBL *mmuTransTblCreate ();LOCAL STATUS mmuTransTblInit (MMU_TRANS_TBL *newTransTbl);LOCAL STATUS mmuTransTblDelete (MMU_TRANS_TBL *transTbl);LOCAL STATUS mmuVirtualPageCreate (MMU_TRANS_TBL *thisTbl, void *virtPageAddr);LOCAL STATUS mmuStateSet (MMU_TRANS_TBL *transTbl, void *pageAddr, UINT stateMask, UINT state);LOCAL STATUS mmuStateGet (MMU_TRANS_TBL *transTbl, void *pageAddr, UINT *state);LOCAL STATUS mmuPageMap (MMU_TRANS_TBL *transTbl, void *virtualAddress, void *physPage);LOCAL STATUS mmuGlobalPageMap (void *virtualAddress, void *physPage);LOCAL STATUS mmuTranslate (MMU_TRANS_TBL *transTbl, void *virtAddress, void **physAddress);LOCAL void mmuCurrentSet (MMU_TRANS_TBL *transTbl);/* kludgey static data structure for parameters in __asm__ directives. */LOCAL int mmuPageSize;BOOL mmuI86Enabled = FALSE;/* a translation table to hold the descriptors for the global transparent * translation of physical to virtual memory */LOCAL MMU_TRANS_TBL mmuGlobalTransTbl;/* initially, the current trans table is a dummy table with MMU disabled */LOCAL MMU_TRANS_TBL *mmuCurrentTransTbl = &mmuGlobalTransTbl;/* array of booleans used to keep track of sections of virtual memory defined * as global. */LOCAL BOOL *globalPageBlock;LOCAL STATE_TRANS_TUPLE mmuStateTransArrayLocal [] = { {VM_STATE_MASK_VALID, MMU_STATE_MASK_VALID, VM_STATE_VALID, MMU_STATE_VALID}, {VM_STATE_MASK_VALID, MMU_STATE_MASK_VALID, VM_STATE_VALID_NOT, MMU_STATE_VALID_NOT}, {VM_STATE_MASK_WRITABLE, MMU_STATE_MASK_WRITABLE, VM_STATE_WRITABLE, MMU_STATE_WRITABLE}, {VM_STATE_MASK_WRITABLE, MMU_STATE_MASK_WRITABLE, VM_STATE_WRITABLE_NOT, MMU_STATE_WRITABLE_NOT}, {VM_STATE_MASK_CACHEABLE, MMU_STATE_MASK_CACHEABLE, VM_STATE_CACHEABLE, MMU_STATE_CACHEABLE}, {VM_STATE_MASK_CACHEABLE, MMU_STATE_MASK_CACHEABLE, VM_STATE_CACHEABLE_NOT, MMU_STATE_CACHEABLE_NOT} };LOCAL MMU_LIB_FUNCS mmuLibFuncsLocal = { mmuI86LibInit, mmuTransTblCreate, mmuTransTblDelete, mmuI86Enable, mmuStateSet, mmuStateGet, mmuPageMap, mmuGlobalPageMap, mmuTranslate, mmuCurrentSet };IMPORT STATE_TRANS_TUPLE *mmuStateTransArray;IMPORT int mmuStateTransArraySize;IMPORT MMU_LIB_FUNCS mmuLibFuncs;IMPORT int mmuPageBlockSize;IMPORT int sysProcessor;LOCAL BOOL firstTime = TRUE;/* MMU_UNLOCK and MMU_LOCK are used to access page table entries that are in * virtual memory that has been invalidated to protect it from being corrupted * MMU_UNLOCK and MMU_LOCK makes no function call, so no worry about stack * mismatch in virtual/physical address space, or the called out function's * disapearance. It is guaranteed that this code is in physical memory. */#ifdef _DIAB_TOOLasm void mmu_diab_int_lock(unsigned int Mask){% reg Mask pushf popl Mask andl $0x00000200, Mask cli% mem Mask pushf popl Mask andl $0x00000200, Mask cli}asm void mmu_diab_int_unlock(unsigned int Mask){% reg Mask; lab L testl $0x00000200, Mask
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -