mmu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  3. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/sched/signal.h>
  8. #include <linux/slab.h>
  9. #include <asm/pgalloc.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/sections.h>
  12. #include <as-layout.h>
  13. #include <os.h>
  14. #include <skas.h>
  15. static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
  16. unsigned long kernel)
  17. {
  18. pgd_t *pgd;
  19. pud_t *pud;
  20. pmd_t *pmd;
  21. pte_t *pte;
  22. pgd = pgd_offset(mm, proc);
  23. pud = pud_alloc(mm, pgd, proc);
  24. if (!pud)
  25. goto out;
  26. pmd = pmd_alloc(mm, pud, proc);
  27. if (!pmd)
  28. goto out_pmd;
  29. pte = pte_alloc_map(mm, pmd, proc);
  30. if (!pte)
  31. goto out_pte;
  32. *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
  33. *pte = pte_mkread(*pte);
  34. return 0;
  35. out_pte:
  36. pmd_free(mm, pmd);
  37. out_pmd:
  38. pud_free(mm, pud);
  39. out:
  40. return -ENOMEM;
  41. }
  42. int init_new_context(struct task_struct *task, struct mm_struct *mm)
  43. {
  44. struct mm_context *from_mm = NULL;
  45. struct mm_context *to_mm = &mm->context;
  46. unsigned long stack = 0;
  47. int ret = -ENOMEM;
  48. stack = get_zeroed_page(GFP_KERNEL);
  49. if (stack == 0)
  50. goto out;
  51. to_mm->id.stack = stack;
  52. if (current->mm != NULL && current->mm != &init_mm)
  53. from_mm = &current->mm->context;
  54. block_signals();
  55. if (from_mm)
  56. to_mm->id.u.pid = copy_context_skas0(stack,
  57. from_mm->id.u.pid);
  58. else to_mm->id.u.pid = start_userspace(stack);
  59. unblock_signals();
  60. if (to_mm->id.u.pid < 0) {
  61. ret = to_mm->id.u.pid;
  62. goto out_free;
  63. }
  64. ret = init_new_ldt(to_mm, from_mm);
  65. if (ret < 0) {
  66. printk(KERN_ERR "init_new_context_skas - init_ldt"
  67. " failed, errno = %d\n", ret);
  68. goto out_free;
  69. }
  70. return 0;
  71. out_free:
  72. if (to_mm->id.stack != 0)
  73. free_page(to_mm->id.stack);
  74. out:
  75. return ret;
  76. }
  77. void uml_setup_stubs(struct mm_struct *mm)
  78. {
  79. int err, ret;
  80. ret = init_stub_pte(mm, STUB_CODE,
  81. (unsigned long) __syscall_stub_start);
  82. if (ret)
  83. goto out;
  84. ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
  85. if (ret)
  86. goto out;
  87. mm->context.stub_pages[0] = virt_to_page(__syscall_stub_start);
  88. mm->context.stub_pages[1] = virt_to_page(mm->context.id.stack);
  89. /* dup_mmap already holds mmap_sem */
  90. err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
  91. VM_READ | VM_MAYREAD | VM_EXEC |
  92. VM_MAYEXEC | VM_DONTCOPY | VM_PFNMAP,
  93. mm->context.stub_pages);
  94. if (err) {
  95. printk(KERN_ERR "install_special_mapping returned %d\n", err);
  96. goto out;
  97. }
  98. return;
  99. out:
  100. force_sigsegv(SIGSEGV, current);
  101. }
  102. void arch_exit_mmap(struct mm_struct *mm)
  103. {
  104. pte_t *pte;
  105. pte = virt_to_pte(mm, STUB_CODE);
  106. if (pte != NULL)
  107. pte_clear(mm, STUB_CODE, pte);
  108. pte = virt_to_pte(mm, STUB_DATA);
  109. if (pte == NULL)
  110. return;
  111. pte_clear(mm, STUB_DATA, pte);
  112. }
  113. void destroy_context(struct mm_struct *mm)
  114. {
  115. struct mm_context *mmu = &mm->context;
  116. /*
  117. * If init_new_context wasn't called, this will be
  118. * zero, resulting in a kill(0), which will result in the
  119. * whole UML suddenly dying. Also, cover negative and
  120. * 1 cases, since they shouldn't happen either.
  121. */
  122. if (mmu->id.u.pid < 2) {
  123. printk(KERN_ERR "corrupt mm_context - pid = %d\n",
  124. mmu->id.u.pid);
  125. return;
  126. }
  127. os_kill_ptraced_process(mmu->id.u.pid, 1);
  128. free_page(mmu->id.stack);
  129. free_ldt(mmu);
  130. }