tlb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <asm/pgtable.h>
  9. #include <asm/tlbflush.h>
  10. #include <as-layout.h>
  11. #include <mem_user.h>
  12. #include <os.h>
  13. #include <skas.h>
  14. #include <kern_util.h>
  15. struct host_vm_change {
  16. struct host_vm_op {
  17. enum { NONE, MMAP, MUNMAP, MPROTECT } type;
  18. union {
  19. struct {
  20. unsigned long addr;
  21. unsigned long len;
  22. unsigned int prot;
  23. int fd;
  24. __u64 offset;
  25. } mmap;
  26. struct {
  27. unsigned long addr;
  28. unsigned long len;
  29. } munmap;
  30. struct {
  31. unsigned long addr;
  32. unsigned long len;
  33. unsigned int prot;
  34. } mprotect;
  35. } u;
  36. } ops[1];
  37. int index;
  38. struct mm_id *id;
  39. void *data;
  40. int force;
  41. };
  42. #define INIT_HVC(mm, force) \
  43. ((struct host_vm_change) \
  44. { .ops = { { .type = NONE } }, \
  45. .id = &mm->context.id, \
  46. .data = NULL, \
  47. .index = 0, \
  48. .force = force })
  49. static void report_enomem(void)
  50. {
  51. printk(KERN_ERR "UML ran out of memory on the host side! "
  52. "This can happen due to a memory limitation or "
  53. "vm.max_map_count has been reached.\n");
  54. }
  55. static int do_ops(struct host_vm_change *hvc, int end,
  56. int finished)
  57. {
  58. struct host_vm_op *op;
  59. int i, ret = 0;
  60. for (i = 0; i < end && !ret; i++) {
  61. op = &hvc->ops[i];
  62. switch (op->type) {
  63. case MMAP:
  64. ret = map(hvc->id, op->u.mmap.addr, op->u.mmap.len,
  65. op->u.mmap.prot, op->u.mmap.fd,
  66. op->u.mmap.offset, finished, &hvc->data);
  67. break;
  68. case MUNMAP:
  69. ret = unmap(hvc->id, op->u.munmap.addr,
  70. op->u.munmap.len, finished, &hvc->data);
  71. break;
  72. case MPROTECT:
  73. ret = protect(hvc->id, op->u.mprotect.addr,
  74. op->u.mprotect.len, op->u.mprotect.prot,
  75. finished, &hvc->data);
  76. break;
  77. default:
  78. printk(KERN_ERR "Unknown op type %d in do_ops\n",
  79. op->type);
  80. BUG();
  81. break;
  82. }
  83. }
  84. if (ret == -ENOMEM)
  85. report_enomem();
  86. return ret;
  87. }
  88. static int add_mmap(unsigned long virt, unsigned long phys, unsigned long len,
  89. unsigned int prot, struct host_vm_change *hvc)
  90. {
  91. __u64 offset;
  92. struct host_vm_op *last;
  93. int fd, ret = 0;
  94. fd = phys_mapping(phys, &offset);
  95. if (hvc->index != 0) {
  96. last = &hvc->ops[hvc->index - 1];
  97. if ((last->type == MMAP) &&
  98. (last->u.mmap.addr + last->u.mmap.len == virt) &&
  99. (last->u.mmap.prot == prot) && (last->u.mmap.fd == fd) &&
  100. (last->u.mmap.offset + last->u.mmap.len == offset)) {
  101. last->u.mmap.len += len;
  102. return 0;
  103. }
  104. }
  105. if (hvc->index == ARRAY_SIZE(hvc->ops)) {
  106. ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
  107. hvc->index = 0;
  108. }
  109. hvc->ops[hvc->index++] = ((struct host_vm_op)
  110. { .type = MMAP,
  111. .u = { .mmap = { .addr = virt,
  112. .len = len,
  113. .prot = prot,
  114. .fd = fd,
  115. .offset = offset }
  116. } });
  117. return ret;
  118. }
  119. static int add_munmap(unsigned long addr, unsigned long len,
  120. struct host_vm_change *hvc)
  121. {
  122. struct host_vm_op *last;
  123. int ret = 0;
  124. if ((addr >= STUB_START) && (addr < STUB_END))
  125. return -EINVAL;
  126. if (hvc->index != 0) {
  127. last = &hvc->ops[hvc->index - 1];
  128. if ((last->type == MUNMAP) &&
  129. (last->u.munmap.addr + last->u.mmap.len == addr)) {
  130. last->u.munmap.len += len;
  131. return 0;
  132. }
  133. }
  134. if (hvc->index == ARRAY_SIZE(hvc->ops)) {
  135. ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
  136. hvc->index = 0;
  137. }
  138. hvc->ops[hvc->index++] = ((struct host_vm_op)
  139. { .type = MUNMAP,
  140. .u = { .munmap = { .addr = addr,
  141. .len = len } } });
  142. return ret;
  143. }
  144. static int add_mprotect(unsigned long addr, unsigned long len,
  145. unsigned int prot, struct host_vm_change *hvc)
  146. {
  147. struct host_vm_op *last;
  148. int ret = 0;
  149. if (hvc->index != 0) {
  150. last = &hvc->ops[hvc->index - 1];
  151. if ((last->type == MPROTECT) &&
  152. (last->u.mprotect.addr + last->u.mprotect.len == addr) &&
  153. (last->u.mprotect.prot == prot)) {
  154. last->u.mprotect.len += len;
  155. return 0;
  156. }
  157. }
  158. if (hvc->index == ARRAY_SIZE(hvc->ops)) {
  159. ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
  160. hvc->index = 0;
  161. }
  162. hvc->ops[hvc->index++] = ((struct host_vm_op)
  163. { .type = MPROTECT,
  164. .u = { .mprotect = { .addr = addr,
  165. .len = len,
  166. .prot = prot } } });
  167. return ret;
  168. }
  169. #define ADD_ROUND(n, inc) (((n) + (inc)) & ~((inc) - 1))
  170. static inline int update_pte_range(pmd_t *pmd, unsigned long addr,
  171. unsigned long end,
  172. struct host_vm_change *hvc)
  173. {
  174. pte_t *pte;
  175. int r, w, x, prot, ret = 0;
  176. pte = pte_offset_kernel(pmd, addr);
  177. do {
  178. if ((addr >= STUB_START) && (addr < STUB_END))
  179. continue;
  180. r = pte_read(*pte);
  181. w = pte_write(*pte);
  182. x = pte_exec(*pte);
  183. if (!pte_young(*pte)) {
  184. r = 0;
  185. w = 0;
  186. } else if (!pte_dirty(*pte))
  187. w = 0;
  188. prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
  189. (x ? UM_PROT_EXEC : 0));
  190. if (hvc->force || pte_newpage(*pte)) {
  191. if (pte_present(*pte))
  192. ret = add_mmap(addr, pte_val(*pte) & PAGE_MASK,
  193. PAGE_SIZE, prot, hvc);
  194. else
  195. ret = add_munmap(addr, PAGE_SIZE, hvc);
  196. } else if (pte_newprot(*pte))
  197. ret = add_mprotect(addr, PAGE_SIZE, prot, hvc);
  198. *pte = pte_mkuptodate(*pte);
  199. } while (pte++, addr += PAGE_SIZE, ((addr < end) && !ret));
  200. return ret;
  201. }
  202. static inline int update_pmd_range(pud_t *pud, unsigned long addr,
  203. unsigned long end,
  204. struct host_vm_change *hvc)
  205. {
  206. pmd_t *pmd;
  207. unsigned long next;
  208. int ret = 0;
  209. pmd = pmd_offset(pud, addr);
  210. do {
  211. next = pmd_addr_end(addr, end);
  212. if (!pmd_present(*pmd)) {
  213. if (hvc->force || pmd_newpage(*pmd)) {
  214. ret = add_munmap(addr, next - addr, hvc);
  215. pmd_mkuptodate(*pmd);
  216. }
  217. }
  218. else ret = update_pte_range(pmd, addr, next, hvc);
  219. } while (pmd++, addr = next, ((addr < end) && !ret));
  220. return ret;
  221. }
  222. static inline int update_pud_range(pgd_t *pgd, unsigned long addr,
  223. unsigned long end,
  224. struct host_vm_change *hvc)
  225. {
  226. pud_t *pud;
  227. unsigned long next;
  228. int ret = 0;
  229. pud = pud_offset(pgd, addr);
  230. do {
  231. next = pud_addr_end(addr, end);
  232. if (!pud_present(*pud)) {
  233. if (hvc->force || pud_newpage(*pud)) {
  234. ret = add_munmap(addr, next - addr, hvc);
  235. pud_mkuptodate(*pud);
  236. }
  237. }
  238. else ret = update_pmd_range(pud, addr, next, hvc);
  239. } while (pud++, addr = next, ((addr < end) && !ret));
  240. return ret;
  241. }
  242. void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
  243. unsigned long end_addr, int force)
  244. {
  245. pgd_t *pgd;
  246. struct host_vm_change hvc;
  247. unsigned long addr = start_addr, next;
  248. int ret = 0;
  249. hvc = INIT_HVC(mm, force);
  250. pgd = pgd_offset(mm, addr);
  251. do {
  252. next = pgd_addr_end(addr, end_addr);
  253. if (!pgd_present(*pgd)) {
  254. if (force || pgd_newpage(*pgd)) {
  255. ret = add_munmap(addr, next - addr, &hvc);
  256. pgd_mkuptodate(*pgd);
  257. }
  258. }
  259. else ret = update_pud_range(pgd, addr, next, &hvc);
  260. } while (pgd++, addr = next, ((addr < end_addr) && !ret));
  261. if (!ret)
  262. ret = do_ops(&hvc, hvc.index, 1);
  263. /* This is not an else because ret is modified above */
  264. if (ret) {
  265. printk(KERN_ERR "fix_range_common: failed, killing current "
  266. "process: %d\n", task_tgid_vnr(current));
  267. /* We are under mmap_sem, release it such that current can terminate */
  268. up_write(&current->mm->mmap_sem);
  269. force_sig(SIGKILL, current);
  270. do_signal(&current->thread.regs);
  271. }
  272. }
  273. static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
  274. {
  275. struct mm_struct *mm;
  276. pgd_t *pgd;
  277. pud_t *pud;
  278. pmd_t *pmd;
  279. pte_t *pte;
  280. unsigned long addr, last;
  281. int updated = 0, err;
  282. mm = &init_mm;
  283. for (addr = start; addr < end;) {
  284. pgd = pgd_offset(mm, addr);
  285. if (!pgd_present(*pgd)) {
  286. last = ADD_ROUND(addr, PGDIR_SIZE);
  287. if (last > end)
  288. last = end;
  289. if (pgd_newpage(*pgd)) {
  290. updated = 1;
  291. err = os_unmap_memory((void *) addr,
  292. last - addr);
  293. if (err < 0)
  294. panic("munmap failed, errno = %d\n",
  295. -err);
  296. }
  297. addr = last;
  298. continue;
  299. }
  300. pud = pud_offset(pgd, addr);
  301. if (!pud_present(*pud)) {
  302. last = ADD_ROUND(addr, PUD_SIZE);
  303. if (last > end)
  304. last = end;
  305. if (pud_newpage(*pud)) {
  306. updated = 1;
  307. err = os_unmap_memory((void *) addr,
  308. last - addr);
  309. if (err < 0)
  310. panic("munmap failed, errno = %d\n",
  311. -err);
  312. }
  313. addr = last;
  314. continue;
  315. }
  316. pmd = pmd_offset(pud, addr);
  317. if (!pmd_present(*pmd)) {
  318. last = ADD_ROUND(addr, PMD_SIZE);
  319. if (last > end)
  320. last = end;
  321. if (pmd_newpage(*pmd)) {
  322. updated = 1;
  323. err = os_unmap_memory((void *) addr,
  324. last - addr);
  325. if (err < 0)
  326. panic("munmap failed, errno = %d\n",
  327. -err);
  328. }
  329. addr = last;
  330. continue;
  331. }
  332. pte = pte_offset_kernel(pmd, addr);
  333. if (!pte_present(*pte) || pte_newpage(*pte)) {
  334. updated = 1;
  335. err = os_unmap_memory((void *) addr,
  336. PAGE_SIZE);
  337. if (err < 0)
  338. panic("munmap failed, errno = %d\n",
  339. -err);
  340. if (pte_present(*pte))
  341. map_memory(addr,
  342. pte_val(*pte) & PAGE_MASK,
  343. PAGE_SIZE, 1, 1, 1);
  344. }
  345. else if (pte_newprot(*pte)) {
  346. updated = 1;
  347. os_protect_memory((void *) addr, PAGE_SIZE, 1, 1, 1);
  348. }
  349. addr += PAGE_SIZE;
  350. }
  351. return updated;
  352. }
  353. void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
  354. {
  355. pgd_t *pgd;
  356. pud_t *pud;
  357. pmd_t *pmd;
  358. pte_t *pte;
  359. struct mm_struct *mm = vma->vm_mm;
  360. void *flush = NULL;
  361. int r, w, x, prot, err = 0;
  362. struct mm_id *mm_id;
  363. address &= PAGE_MASK;
  364. pgd = pgd_offset(mm, address);
  365. if (!pgd_present(*pgd))
  366. goto kill;
  367. pud = pud_offset(pgd, address);
  368. if (!pud_present(*pud))
  369. goto kill;
  370. pmd = pmd_offset(pud, address);
  371. if (!pmd_present(*pmd))
  372. goto kill;
  373. pte = pte_offset_kernel(pmd, address);
  374. r = pte_read(*pte);
  375. w = pte_write(*pte);
  376. x = pte_exec(*pte);
  377. if (!pte_young(*pte)) {
  378. r = 0;
  379. w = 0;
  380. } else if (!pte_dirty(*pte)) {
  381. w = 0;
  382. }
  383. mm_id = &mm->context.id;
  384. prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
  385. (x ? UM_PROT_EXEC : 0));
  386. if (pte_newpage(*pte)) {
  387. if (pte_present(*pte)) {
  388. unsigned long long offset;
  389. int fd;
  390. fd = phys_mapping(pte_val(*pte) & PAGE_MASK, &offset);
  391. err = map(mm_id, address, PAGE_SIZE, prot, fd, offset,
  392. 1, &flush);
  393. }
  394. else err = unmap(mm_id, address, PAGE_SIZE, 1, &flush);
  395. }
  396. else if (pte_newprot(*pte))
  397. err = protect(mm_id, address, PAGE_SIZE, prot, 1, &flush);
  398. if (err) {
  399. if (err == -ENOMEM)
  400. report_enomem();
  401. goto kill;
  402. }
  403. *pte = pte_mkuptodate(*pte);
  404. return;
  405. kill:
  406. printk(KERN_ERR "Failed to flush page for address 0x%lx\n", address);
  407. force_sig(SIGKILL, current);
  408. }
  409. pgd_t *pgd_offset_proc(struct mm_struct *mm, unsigned long address)
  410. {
  411. return pgd_offset(mm, address);
  412. }
  413. pud_t *pud_offset_proc(pgd_t *pgd, unsigned long address)
  414. {
  415. return pud_offset(pgd, address);
  416. }
  417. pmd_t *pmd_offset_proc(pud_t *pud, unsigned long address)
  418. {
  419. return pmd_offset(pud, address);
  420. }
  421. pte_t *pte_offset_proc(pmd_t *pmd, unsigned long address)
  422. {
  423. return pte_offset_kernel(pmd, address);
  424. }
  425. pte_t *addr_pte(struct task_struct *task, unsigned long addr)
  426. {
  427. pgd_t *pgd = pgd_offset(task->mm, addr);
  428. pud_t *pud = pud_offset(pgd, addr);
  429. pmd_t *pmd = pmd_offset(pud, addr);
  430. return pte_offset_map(pmd, addr);
  431. }
  432. void flush_tlb_all(void)
  433. {
  434. flush_tlb_mm(current->mm);
  435. }
  436. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  437. {
  438. flush_tlb_kernel_range_common(start, end);
  439. }
  440. void flush_tlb_kernel_vm(void)
  441. {
  442. flush_tlb_kernel_range_common(start_vm, end_vm);
  443. }
  444. void __flush_tlb_one(unsigned long addr)
  445. {
  446. flush_tlb_kernel_range_common(addr, addr + PAGE_SIZE);
  447. }
  448. static void fix_range(struct mm_struct *mm, unsigned long start_addr,
  449. unsigned long end_addr, int force)
  450. {
  451. fix_range_common(mm, start_addr, end_addr, force);
  452. }
  453. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  454. unsigned long end)
  455. {
  456. if (vma->vm_mm == NULL)
  457. flush_tlb_kernel_range_common(start, end);
  458. else fix_range(vma->vm_mm, start, end, 0);
  459. }
  460. EXPORT_SYMBOL(flush_tlb_range);
  461. void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
  462. unsigned long end)
  463. {
  464. /*
  465. * Don't bother flushing if this address space is about to be
  466. * destroyed.
  467. */
  468. if (atomic_read(&mm->mm_users) == 0)
  469. return;
  470. fix_range(mm, start, end, 0);
  471. }
  472. void flush_tlb_mm(struct mm_struct *mm)
  473. {
  474. struct vm_area_struct *vma = mm->mmap;
  475. while (vma != NULL) {
  476. fix_range(mm, vma->vm_start, vma->vm_end, 0);
  477. vma = vma->vm_next;
  478. }
  479. }
  480. void force_flush_all(void)
  481. {
  482. struct mm_struct *mm = current->mm;
  483. struct vm_area_struct *vma = mm->mmap;
  484. while (vma != NULL) {
  485. fix_range(mm, vma->vm_start, vma->vm_end, 1);
  486. vma = vma->vm_next;
  487. }
  488. }