mmu_emu.c 12 KB

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