mmu_emu.c 12 KB

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