mmu.c 3.8 KB

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