mmu_emu.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2. ** Tablewalk MMU emulator
  3. **
  4. ** by Toshiyasu Morita
  5. **
  6. ** Started 1/16/98 @ 2:22 am
  7. */
  8. #include <linux/mman.h>
  9. #include <linux/mm.h>
  10. #include <linux/kernel.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/delay.h>
  13. #include <linux/bootmem.h>
  14. #include <asm/setup.h>
  15. #include <asm/traps.h>
  16. #include <asm/system.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/page.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/sun3mmu.h>
  21. #include <asm/segment.h>
  22. #include <asm/bitops.h>
  23. #include <asm/oplib.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/dvma.h>
  26. extern void prom_reboot (char *) __attribute__ ((__noreturn__));
  27. #undef DEBUG_MMU_EMU
  28. #define DEBUG_PROM_MAPS
  29. /*
  30. ** Defines
  31. */
  32. #define CONTEXTS_NUM 8
  33. #define SEGMAPS_PER_CONTEXT_NUM 2048
  34. #define PAGES_PER_SEGMENT 16
  35. #define PMEGS_NUM 256
  36. #define PMEG_MASK 0xFF
  37. /*
  38. ** Globals
  39. */
  40. unsigned long vmalloc_end = 0;
  41. unsigned long pmeg_vaddr[PMEGS_NUM];
  42. unsigned char pmeg_alloc[PMEGS_NUM];
  43. unsigned char pmeg_ctx[PMEGS_NUM];
  44. /* pointers to the mm structs for each task in each
  45.    context. 0xffffffff is a marker for kernel context */
  46. struct mm_struct *ctx_alloc[CONTEXTS_NUM] = {
  47. (struct mm_struct *)0xffffffff, 0, 0, 0, 0, 0, 0, 0
  48. };
  49. /* has this context been mmdrop'd? */
  50. static unsigned char ctx_avail = CONTEXTS_NUM-1;
  51. /* array of pages to be marked off for the rom when we do mem_init later */
  52. /* 256 pages lets the rom take up to 2mb of physical ram..  I really
  53.    hope it never wants mote than that. */
  54. unsigned long rom_pages[256];
  55. /* Print a PTE value in symbolic form. For debugging. */
  56. void print_pte (pte_t pte)
  57. {
  58. #if 0
  59. /* Verbose version. */
  60. unsigned long val = pte_val (pte);
  61. printk (" pte=%lx [addr=%lx",
  62. val, (val & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT);
  63. if (val & SUN3_PAGE_VALID) printk (" valid");
  64. if (val & SUN3_PAGE_WRITEABLE) printk (" write");
  65. if (val & SUN3_PAGE_SYSTEM) printk (" sys");
  66. if (val & SUN3_PAGE_NOCACHE) printk (" nocache");
  67. if (val & SUN3_PAGE_ACCESSED) printk (" accessed");
  68. if (val & SUN3_PAGE_MODIFIED) printk (" modified");
  69. switch (val & SUN3_PAGE_TYPE_MASK) {
  70. case SUN3_PAGE_TYPE_MEMORY: printk (" memory"); break;
  71. case SUN3_PAGE_TYPE_IO:     printk (" io");     break;
  72. case SUN3_PAGE_TYPE_VME16:  printk (" vme16");  break;
  73. case SUN3_PAGE_TYPE_VME32:  printk (" vme32");  break;
  74. }
  75. printk ("]n");
  76. #else
  77. /* Terse version. More likely to fit on a line. */
  78. unsigned long val = pte_val (pte);
  79. char flags[7], *type;
  80. flags[0] = (val & SUN3_PAGE_VALID)     ? 'v' : '-';
  81. flags[1] = (val & SUN3_PAGE_WRITEABLE) ? 'w' : '-';
  82. flags[2] = (val & SUN3_PAGE_SYSTEM)    ? 's' : '-';
  83. flags[3] = (val & SUN3_PAGE_NOCACHE)   ? 'x' : '-';
  84. flags[4] = (val & SUN3_PAGE_ACCESSED)  ? 'a' : '-';
  85. flags[5] = (val & SUN3_PAGE_MODIFIED)  ? 'm' : '-';
  86. flags[6] = '';
  87. switch (val & SUN3_PAGE_TYPE_MASK) {
  88. case SUN3_PAGE_TYPE_MEMORY: type = "memory"; break;
  89. case SUN3_PAGE_TYPE_IO:     type = "io"    ; break;
  90. case SUN3_PAGE_TYPE_VME16:  type = "vme16" ; break;
  91. case SUN3_PAGE_TYPE_VME32:  type = "vme32" ; break;
  92. default: type = "unknown?"; break;
  93. }
  94. printk (" pte=%08lx [%07lx %s %s]n",
  95. val, (val & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT, flags, type);
  96. #endif
  97. }
  98. /* Print the PTE value for a given virtual address. For debugging. */
  99. void print_pte_vaddr (unsigned long vaddr)
  100. {
  101. printk (" vaddr=%lx [%02lx]", vaddr, sun3_get_segmap (vaddr));
  102. print_pte (__pte (sun3_get_pte (vaddr)));
  103. }
  104. /*
  105.  * Initialise the MMU emulator.
  106.  */
  107. void mmu_emu_init(unsigned long bootmem_end)
  108. {
  109. unsigned long seg, num;
  110. int i,j;
  111. memset(rom_pages, 0, sizeof(rom_pages));
  112. memset(pmeg_vaddr, 0, sizeof(pmeg_vaddr));
  113. memset(pmeg_alloc, 0, sizeof(pmeg_alloc));
  114. memset(pmeg_ctx, 0, sizeof(pmeg_ctx));
  115. /* pmeg align the end of bootmem, adding another pmeg,
  116.  * later bootmem allocations will likely need it */
  117. bootmem_end = (bootmem_end + (2 * SUN3_PMEG_SIZE)) & ~SUN3_PMEG_MASK;
  118. /* mark all of the pmegs used thus far as reserved */
  119. for (i=0; i < __pa(bootmem_end) / SUN3_PMEG_SIZE ; ++i)
  120. pmeg_alloc[i] = 2;
  121. /* I'm thinking that most of the top pmeg's are going to be
  122.    used for something, and we probably shouldn't risk it */
  123. for(num = 0xf0; num <= 0xff; num++)
  124. pmeg_alloc[num] = 2;
  125. /* liberate all existing mappings in the rest of kernel space */
  126. for(seg = bootmem_end; seg < 0x0f800000; seg += SUN3_PMEG_SIZE) {
  127. i = sun3_get_segmap(seg);
  128. if(!pmeg_alloc[i]) {
  129. #ifdef DEBUG_MMU_EMU
  130. printk("freed: ");
  131. print_pte_vaddr (seg);
  132. #endif
  133. sun3_put_segmap(seg, SUN3_INVALID_PMEG);
  134. }
  135. }
  136. j = 0;
  137. for (num=0, seg=0x0F800000; seg<0x10000000; seg+=16*PAGE_SIZE) {
  138. if (sun3_get_segmap (seg) != SUN3_INVALID_PMEG) {
  139. #ifdef DEBUG_PROM_MAPS
  140. for(i = 0; i < 16; i++) {
  141. printk ("mapped:");
  142. print_pte_vaddr (seg + (i*PAGE_SIZE));
  143. break;
  144. }
  145. #endif
  146. // the lowest mapping here is the end of our
  147. // vmalloc region
  148. if(!vmalloc_end)
  149. vmalloc_end = seg;
  150. // mark the segmap alloc'd, and reserve any
  151. // of the first 0xbff pages the hardware is
  152. // already using...  does any sun3 support > 24mb?
  153. pmeg_alloc[sun3_get_segmap(seg)] = 2;
  154. }
  155. }
  156. dvma_init();
  157. /* blank everything below the kernel, and we've got the base
  158.    mapping to start all the contexts off with... */
  159. for(seg = 0; seg < PAGE_OFFSET; seg += SUN3_PMEG_SIZE) 
  160. sun3_put_segmap(seg, SUN3_INVALID_PMEG);
  161. set_fs(MAKE_MM_SEG(3));
  162. for(seg = 0; seg < 0x10000000; seg += SUN3_PMEG_SIZE) {
  163. i = sun3_get_segmap(seg);
  164. for(j = 1; j < CONTEXTS_NUM; j++)
  165. (*(romvec->pv_setctxt))(j, (void *)seg, i);
  166. }
  167. set_fs(KERNEL_DS);
  168. }
  169. /* erase the mappings for a dead context.  Uses the pg_dir for hints
  170.    as the pmeg tables proved somewhat unreliable, and unmapping all of
  171.    TASK_SIZE was much slower and no more stable. */
  172. /* todo: find a better way to keep track of the pmegs used by a
  173.    context for when they're cleared */
  174. void clear_context(unsigned long context)
  175. {
  176.      unsigned char oldctx;
  177.      unsigned long i;
  178.     
  179.      if(context) {
  180.      if(!ctx_alloc[context]) 
  181.      panic("clear_context: context not allocatedn");
  182.      ctx_alloc[context]->context = SUN3_INVALID_CONTEXT;
  183.      ctx_alloc[context] = (struct mm_struct *)0;
  184.      ctx_avail++;
  185.      }
  186.      oldctx = sun3_get_context();
  187.      sun3_put_context(context);
  188.      for(i = 0; i < SUN3_INVALID_PMEG; i++) {
  189.      if((pmeg_ctx[i] == context) && (pmeg_alloc[i] == 1)) {
  190.      sun3_put_segmap(pmeg_vaddr[i], SUN3_INVALID_PMEG);
  191.      pmeg_ctx[i] = 0;
  192.      pmeg_alloc[i] = 0;
  193.      pmeg_vaddr[i] = 0;
  194.      }
  195.      }
  196.      
  197.      sun3_put_context(oldctx);
  198. }
  199. /* gets an empty context.  if full, kills the next context listed to
  200.    die first */
  201. /* This context invalidation scheme is, well, totally arbitrary, I'm
  202.    sure it could be much more intellegent...  but it gets the job done
  203.    for now without much overhead in making it's decision. */
  204. /* todo: come up with optimized scheme for flushing contexts */
  205. unsigned long get_free_context(struct mm_struct *mm) 
  206. {
  207. unsigned long new = 1;
  208. static unsigned char next_to_die = 1;
  209. if(!ctx_avail) {
  210. /* kill someone to get our context */
  211. new = next_to_die;
  212. clear_context(new);
  213. next_to_die = (next_to_die + 1) & 0x7;
  214. if(!next_to_die)
  215. next_to_die++;
  216. } else {
  217. while(new < CONTEXTS_NUM) {
  218. if(ctx_alloc[new])
  219. new++;
  220. else
  221. break;
  222. }
  223. // check to make sure one was really free...
  224. if(new == CONTEXTS_NUM) 
  225. panic("get_free_context: failed to find free context");
  226. }
  227. ctx_alloc[new] = mm;
  228. ctx_avail--;
  229. return new;
  230. }
  231. /*
  232.  * Dynamically select a `spare' PMEG and use it to map virtual `vaddr' in
  233.  * `context'. Maintain internal PMEG management structures. This doesn't
  234.  * actually map the physical address, but does clear the old mappings.
  235.  */
  236. //todo: better allocation scheme? but is extra complexity worthwhile?
  237. //todo: only clear old entries if necessary? how to tell?
  238. static inline void mmu_emu_map_pmeg (int context, int vaddr)
  239. {
  240. static unsigned char curr_pmeg = 128;
  241. int i;
  242. /* Round address to PMEG boundary. */
  243. vaddr &= ~SUN3_PMEG_MASK;
  244. /* Find a spare one. */
  245. while (pmeg_alloc[curr_pmeg] == 2)
  246. ++curr_pmeg;
  247. #ifdef DEBUG_MMU_EMU
  248. printk("mmu_emu_map_pmeg: pmeg %x to context %d vaddr %xn",
  249.        curr_pmeg, context, vaddr);
  250. #endif
  251. /* Invalidate old mapping for the pmeg, if any */
  252. if (pmeg_alloc[curr_pmeg] == 1) {
  253. sun3_put_context(pmeg_ctx[curr_pmeg]);
  254. sun3_put_segmap (pmeg_vaddr[curr_pmeg], SUN3_INVALID_PMEG);
  255. sun3_put_context(context);
  256. }
  257. /* Update PMEG management structures. */
  258. // don't take pmeg's away from the kernel...
  259. if(vaddr >= PAGE_OFFSET) {
  260. /* map kernel pmegs into all contexts */
  261. unsigned char i;
  262. for(i = 0; i < CONTEXTS_NUM; i++) {
  263. sun3_put_context(i);
  264. sun3_put_segmap (vaddr, curr_pmeg);
  265. }
  266. sun3_put_context(context);
  267. pmeg_alloc[curr_pmeg] = 2;
  268. pmeg_ctx[curr_pmeg] = 0;
  269. }
  270. else {
  271. pmeg_alloc[curr_pmeg] = 1;
  272. pmeg_ctx[curr_pmeg] = context;
  273. sun3_put_segmap (vaddr, curr_pmeg);
  274. }
  275. pmeg_vaddr[curr_pmeg] = vaddr;
  276. /* Set hardware mapping and clear the old PTE entries. */
  277. for (i=0; i<SUN3_PMEG_SIZE; i+=SUN3_PTE_SIZE) 
  278. sun3_put_pte (vaddr + i, SUN3_PAGE_SYSTEM);
  279. /* Consider a different one next time. */
  280. ++curr_pmeg;
  281. }
  282. /*
  283.  * Handle a pagefault at virtual address `vaddr'; check if there should be a
  284.  * page there (specifically, whether the software pagetables indicate that
  285.  * there is). This is necessary due to the limited size of the second-level
  286.  * Sun3 hardware pagetables (256 groups of 16 pages). If there should be a
  287.  * mapping present, we select a `spare' PMEG and use it to create a mapping.
  288.  * `read_flag' is nonzero for a read fault; zero for a write. Returns nonzero
  289.  * if we successfully handled the fault.
  290.  */
  291. //todo: should we bump minor pagefault counter? if so, here or in caller?
  292. //todo: possibly inline this into bus_error030 in <asm/buserror.h> ?
  293. // kernel_fault is set when a kernel page couldn't be demand mapped,
  294. // and forces another try using the kernel page table.  basically a
  295. // hack so that vmalloc would work correctly.
  296. int mmu_emu_handle_fault (unsigned long vaddr, int read_flag, int kernel_fault)
  297. {
  298. unsigned long segment, offset;
  299. unsigned char context;
  300. pte_t *pte;
  301. pgd_t * crp;
  302. if(current->mm == NULL) {
  303. crp = swapper_pg_dir;
  304. context = 0;
  305. } else {
  306. context = current->mm->context;
  307. if(kernel_fault) 
  308. crp = swapper_pg_dir;
  309. else
  310. crp = current->mm->pgd;
  311. }
  312. #ifdef DEBUG_MMU_EMU
  313. printk ("mmu_emu_handle_fault: vaddr=%lx type=%s crp=%pn",
  314. vaddr, read_flag ? "read" : "write", crp);
  315. #endif
  316. segment = (vaddr >> SUN3_PMEG_SIZE_BITS) & 0x7FF;
  317. offset  = (vaddr >> SUN3_PTE_SIZE_BITS) & 0xF;
  318. #ifdef DEBUG_MMU_EMU
  319. printk ("mmu_emu_handle_fault: segment=%lx offset=%lxn", segment, offset);
  320. #endif
  321. pte = (pte_t *) pgd_val (*(crp + segment));
  322. //todo: next line should check for valid pmd properly.
  323. if (!pte) {
  324. //                printk ("mmu_emu_handle_fault: invalid pmdn");
  325.                 return 0;
  326.         }
  327. pte = (pte_t *) __va ((unsigned long)(pte + offset));
  328. /* Make sure this is a valid page */
  329. if (!(pte_val (*pte) & SUN3_PAGE_VALID)) 
  330. return 0;
  331. /* Make sure there's a pmeg allocated for the page */
  332. if (sun3_get_segmap (vaddr&~SUN3_PMEG_MASK) == SUN3_INVALID_PMEG) 
  333. mmu_emu_map_pmeg (context, vaddr);
  334. /* Write the pte value to hardware MMU */
  335. sun3_put_pte (vaddr&PAGE_MASK, pte_val (*pte));
  336. /* Update software copy of the pte value */
  337. // I'm not sure this is necessary. If this is required, we ought to simply
  338. // copy this out when we reuse the PMEG or at some other convenient time.
  339. // Doing it here is fairly meaningless, anyway, as we only know about the
  340. // first access to a given page. --m
  341. if (!read_flag) {
  342. if (pte_val (*pte) & SUN3_PAGE_WRITEABLE)
  343. pte_val (*pte) |= (SUN3_PAGE_ACCESSED
  344.    | SUN3_PAGE_MODIFIED);
  345. else 
  346. return 0; /* Write-protect error. */
  347. } else
  348. pte_val (*pte) |= SUN3_PAGE_ACCESSED;
  349. #ifdef DEBUG_MMU_EMU
  350. printk ("seg:%d crp:%p ->", get_fs().seg, crp);
  351. print_pte_vaddr (vaddr);
  352. printk ("n");
  353. #endif
  354. return 1;
  355. }