mmu.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Thomas Meyer ([email protected])
  4. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  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/sections.h>
  11. #include <as-layout.h>
  12. #include <os.h>
  13. #include <skas.h>
  14. int init_new_context(struct task_struct *task, struct mm_struct *mm)
  15. {
  16. struct mm_context *from_mm = NULL;
  17. struct mm_context *to_mm = &mm->context;
  18. unsigned long stack = 0;
  19. int ret = -ENOMEM;
  20. stack = get_zeroed_page(GFP_KERNEL);
  21. if (stack == 0)
  22. goto out;
  23. to_mm->id.stack = stack;
  24. if (current->mm != NULL && current->mm != &init_mm)
  25. from_mm = &current->mm->context;
  26. block_signals_trace();
  27. if (from_mm)
  28. to_mm->id.u.pid = copy_context_skas0(stack,
  29. from_mm->id.u.pid);
  30. else to_mm->id.u.pid = start_userspace(stack);
  31. unblock_signals_trace();
  32. if (to_mm->id.u.pid < 0) {
  33. ret = to_mm->id.u.pid;
  34. goto out_free;
  35. }
  36. ret = init_new_ldt(to_mm, from_mm);
  37. if (ret < 0) {
  38. printk(KERN_ERR "init_new_context_skas - init_ldt"
  39. " failed, errno = %d\n", ret);
  40. goto out_free;
  41. }
  42. return 0;
  43. out_free:
  44. if (to_mm->id.stack != 0)
  45. free_page(to_mm->id.stack);
  46. out:
  47. return ret;
  48. }
  49. void destroy_context(struct mm_struct *mm)
  50. {
  51. struct mm_context *mmu = &mm->context;
  52. /*
  53. * If init_new_context wasn't called, this will be
  54. * zero, resulting in a kill(0), which will result in the
  55. * whole UML suddenly dying. Also, cover negative and
  56. * 1 cases, since they shouldn't happen either.
  57. */
  58. if (mmu->id.u.pid < 2) {
  59. printk(KERN_ERR "corrupt mm_context - pid = %d\n",
  60. mmu->id.u.pid);
  61. return;
  62. }
  63. os_kill_ptraced_process(mmu->id.u.pid, 1);
  64. free_page(mmu->id.stack);
  65. free_ldt(mmu);
  66. }