mmu.c 3.2 KB

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