syscall32.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2002,2003 Andi Kleen, SuSE Labs */
  2. /* vsyscall handling for 32bit processes. Map a stub page into it
  3. on demand because 32bit cannot reach the kernel's fixmaps */
  4. #include <linux/mm.h>
  5. #include <linux/string.h>
  6. #include <linux/kernel.h>
  7. #include <linux/gfp.h>
  8. #include <linux/init.h>
  9. #include <linux/stringify.h>
  10. #include <linux/security.h>
  11. #include <asm/proto.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/ia32_unistd.h>
  14. /* 32bit VDSOs mapped into user space. */
  15. asm(".section \".init.data\",\"aw\"\n"
  16. "syscall32_syscall:\n"
  17. ".incbin \"arch/x86_64/ia32/vsyscall-syscall.so\"\n"
  18. "syscall32_syscall_end:\n"
  19. "syscall32_sysenter:\n"
  20. ".incbin \"arch/x86_64/ia32/vsyscall-sysenter.so\"\n"
  21. "syscall32_sysenter_end:\n"
  22. ".previous");
  23. extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
  24. extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
  25. extern int sysctl_vsyscall32;
  26. char *syscall32_page;
  27. static int use_sysenter = -1;
  28. static struct page *
  29. syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
  30. {
  31. struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
  32. get_page(p);
  33. return p;
  34. }
  35. /* Prevent VMA merging */
  36. static void syscall32_vma_close(struct vm_area_struct *vma)
  37. {
  38. }
  39. static struct vm_operations_struct syscall32_vm_ops = {
  40. .close = syscall32_vma_close,
  41. .nopage = syscall32_nopage,
  42. };
  43. struct linux_binprm;
  44. /* Setup a VMA at program startup for the vsyscall page */
  45. int syscall32_setup_pages(struct linux_binprm *bprm, int exstack)
  46. {
  47. int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
  48. struct vm_area_struct *vma;
  49. struct mm_struct *mm = current->mm;
  50. int ret;
  51. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  52. if (!vma)
  53. return -ENOMEM;
  54. if (security_vm_enough_memory(npages)) {
  55. kmem_cache_free(vm_area_cachep, vma);
  56. return -ENOMEM;
  57. }
  58. memset(vma, 0, sizeof(struct vm_area_struct));
  59. /* Could randomize here */
  60. vma->vm_start = VSYSCALL32_BASE;
  61. vma->vm_end = VSYSCALL32_END;
  62. /* MAYWRITE to allow gdb to COW and set breakpoints */
  63. vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYEXEC|VM_MAYWRITE;
  64. vma->vm_flags |= mm->def_flags;
  65. vma->vm_page_prot = protection_map[vma->vm_flags & 7];
  66. vma->vm_ops = &syscall32_vm_ops;
  67. vma->vm_mm = mm;
  68. down_write(&mm->mmap_sem);
  69. if ((ret = insert_vm_struct(mm, vma))) {
  70. up_write(&mm->mmap_sem);
  71. kmem_cache_free(vm_area_cachep, vma);
  72. return ret;
  73. }
  74. mm->total_vm += npages;
  75. up_write(&mm->mmap_sem);
  76. return 0;
  77. }
  78. static int __init init_syscall32(void)
  79. {
  80. syscall32_page = (void *)get_zeroed_page(GFP_KERNEL);
  81. if (!syscall32_page)
  82. panic("Cannot allocate syscall32 page");
  83. if (use_sysenter > 0) {
  84. memcpy(syscall32_page, syscall32_sysenter,
  85. syscall32_sysenter_end - syscall32_sysenter);
  86. } else {
  87. memcpy(syscall32_page, syscall32_syscall,
  88. syscall32_syscall_end - syscall32_syscall);
  89. }
  90. return 0;
  91. }
  92. __initcall(init_syscall32);
  93. /* May not be __init: called during resume */
  94. void syscall32_cpu_init(void)
  95. {
  96. if (use_sysenter < 0)
  97. use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
  98. /* Load these always in case some future AMD CPU supports
  99. SYSENTER from compat mode too. */
  100. checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  101. checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
  102. checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  103. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  104. }