syscall32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  51. if (!vma)
  52. return -ENOMEM;
  53. if (security_vm_enough_memory(npages)) {
  54. kmem_cache_free(vm_area_cachep, vma);
  55. return -ENOMEM;
  56. }
  57. memset(vma, 0, sizeof(struct vm_area_struct));
  58. /* Could randomize here */
  59. vma->vm_start = VSYSCALL32_BASE;
  60. vma->vm_end = VSYSCALL32_END;
  61. /* MAYWRITE to allow gdb to COW and set breakpoints */
  62. vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYEXEC|VM_MAYWRITE;
  63. vma->vm_flags |= mm->def_flags;
  64. vma->vm_page_prot = protection_map[vma->vm_flags & 7];
  65. vma->vm_ops = &syscall32_vm_ops;
  66. vma->vm_mm = mm;
  67. down_write(&mm->mmap_sem);
  68. insert_vm_struct(mm, vma);
  69. mm->total_vm += npages;
  70. up_write(&mm->mmap_sem);
  71. return 0;
  72. }
  73. static int __init init_syscall32(void)
  74. {
  75. syscall32_page = (void *)get_zeroed_page(GFP_KERNEL);
  76. if (!syscall32_page)
  77. panic("Cannot allocate syscall32 page");
  78. if (use_sysenter > 0) {
  79. memcpy(syscall32_page, syscall32_sysenter,
  80. syscall32_sysenter_end - syscall32_sysenter);
  81. } else {
  82. memcpy(syscall32_page, syscall32_syscall,
  83. syscall32_syscall_end - syscall32_syscall);
  84. }
  85. return 0;
  86. }
  87. __initcall(init_syscall32);
  88. /* May not be __init: called during resume */
  89. void syscall32_cpu_init(void)
  90. {
  91. if (use_sysenter < 0)
  92. use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
  93. /* Load these always in case some future AMD CPU supports
  94. SYSENTER from compat mode too. */
  95. checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  96. checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
  97. checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  98. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  99. }