mmu.c 3.9 KB

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