mmu.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/mm.h"
  6. #include "linux/sched.h"
  7. #include "asm/pgalloc.h"
  8. #include "asm/pgtable.h"
  9. #include "as-layout.h"
  10. #include "os.h"
  11. #include "skas.h"
  12. extern int __syscall_stub_start;
  13. static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
  14. unsigned long kernel)
  15. {
  16. pgd_t *pgd;
  17. pud_t *pud;
  18. pmd_t *pmd;
  19. pte_t *pte;
  20. pgd = pgd_offset(mm, proc);
  21. pud = pud_alloc(mm, pgd, proc);
  22. if (!pud)
  23. goto out;
  24. pmd = pmd_alloc(mm, pud, proc);
  25. if (!pmd)
  26. goto out_pmd;
  27. pte = pte_alloc_map(mm, pmd, proc);
  28. if (!pte)
  29. goto out_pte;
  30. /*
  31. * There's an interaction between the skas0 stub pages, stack
  32. * randomization, and the BUG at the end of exit_mmap. exit_mmap
  33. * checks that the number of page tables freed is the same as had
  34. * been allocated. If the stack is on the last page table page,
  35. * then the stack pte page will be freed, and if not, it won't. To
  36. * avoid having to know where the stack is, or if the process mapped
  37. * something at the top of its address space for some other reason,
  38. * we set TASK_SIZE to end at the start of the last page table.
  39. * This keeps exit_mmap off the last page, but introduces a leak
  40. * of that page. So, we hang onto it here and free it in
  41. * destroy_context_skas.
  42. */
  43. mm->context.last_page_table = pmd_page_vaddr(*pmd);
  44. #ifdef CONFIG_3_LEVEL_PGTABLES
  45. mm->context.last_pmd = (unsigned long) __va(pud_val(*pud));
  46. #endif
  47. *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
  48. *pte = pte_mkread(*pte);
  49. return 0;
  50. out_pmd:
  51. pud_free(pud);
  52. out_pte:
  53. pmd_free(pmd);
  54. out:
  55. return -ENOMEM;
  56. }
  57. int init_new_context(struct task_struct *task, struct mm_struct *mm)
  58. {
  59. struct mm_context *from_mm = NULL;
  60. struct mm_context *to_mm = &mm->context;
  61. unsigned long stack = 0;
  62. int ret = -ENOMEM;
  63. if (skas_needs_stub) {
  64. stack = get_zeroed_page(GFP_KERNEL);
  65. if (stack == 0)
  66. goto out;
  67. /*
  68. * This zeros the entry that pgd_alloc didn't, needed since
  69. * we are about to reinitialize it, and want mm.nr_ptes to
  70. * be accurate.
  71. */
  72. mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
  73. ret = init_stub_pte(mm, STUB_CODE,
  74. (unsigned long) &__syscall_stub_start);
  75. if (ret)
  76. goto out_free;
  77. ret = init_stub_pte(mm, STUB_DATA, stack);
  78. if (ret)
  79. goto out_free;
  80. mm->nr_ptes--;
  81. }
  82. to_mm->id.stack = stack;
  83. if (current->mm != NULL && current->mm != &init_mm)
  84. from_mm = &current->mm->context;
  85. if (proc_mm) {
  86. ret = new_mm(stack);
  87. if (ret < 0) {
  88. printk(KERN_ERR "init_new_context_skas - "
  89. "new_mm failed, errno = %d\n", ret);
  90. goto out_free;
  91. }
  92. to_mm->id.u.mm_fd = ret;
  93. }
  94. else {
  95. if (from_mm)
  96. to_mm->id.u.pid = copy_context_skas0(stack,
  97. from_mm->id.u.pid);
  98. else to_mm->id.u.pid = start_userspace(stack);
  99. }
  100. ret = init_new_ldt(to_mm, from_mm);
  101. if (ret < 0) {
  102. printk(KERN_ERR "init_new_context_skas - init_ldt"
  103. " failed, errno = %d\n", ret);
  104. goto out_free;
  105. }
  106. return 0;
  107. out_free:
  108. if (to_mm->id.stack != 0)
  109. free_page(to_mm->id.stack);
  110. out:
  111. return ret;
  112. }
  113. void destroy_context(struct mm_struct *mm)
  114. {
  115. struct mm_context *mmu = &mm->context;
  116. if (proc_mm)
  117. os_close_file(mmu->id.u.mm_fd);
  118. else
  119. os_kill_ptraced_process(mmu->id.u.pid, 1);
  120. if (!proc_mm || !ptrace_faultinfo) {
  121. free_page(mmu->id.stack);
  122. pte_lock_deinit(virt_to_page(mmu->last_page_table));
  123. pte_free_kernel((pte_t *) mmu->last_page_table);
  124. dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE);
  125. #ifdef CONFIG_3_LEVEL_PGTABLES
  126. pmd_free((pmd_t *) mmu->last_pmd);
  127. #endif
  128. }
  129. free_ldt(mmu);
  130. }