espfix_64.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2014 Intel Corporation; author: H. Peter Anvin
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * ----------------------------------------------------------------------- */
  15. /*
  16. * The IRET instruction, when returning to a 16-bit segment, only
  17. * restores the bottom 16 bits of the user space stack pointer. This
  18. * causes some 16-bit software to break, but it also leaks kernel state
  19. * to user space.
  20. *
  21. * This works around this by creating percpu "ministacks", each of which
  22. * is mapped 2^16 times 64K apart. When we detect that the return SS is
  23. * on the LDT, we copy the IRET frame to the ministack and use the
  24. * relevant alias to return to userspace. The ministacks are mapped
  25. * readonly, so if the IRET fault we promote #GP to #DF which is an IST
  26. * vector and thus has its own stack; we then do the fixup in the #DF
  27. * handler.
  28. *
  29. * This file sets up the ministacks and the related page tables. The
  30. * actual ministack invocation is in entry_64.S.
  31. */
  32. #include <linux/init.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/percpu.h>
  36. #include <linux/gfp.h>
  37. #include <linux/random.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/pgalloc.h>
  40. #include <asm/setup.h>
  41. #include <asm/espfix.h>
  42. /*
  43. * Note: we only need 6*8 = 48 bytes for the espfix stack, but round
  44. * it up to a cache line to avoid unnecessary sharing.
  45. */
  46. #define ESPFIX_STACK_SIZE (8*8UL)
  47. #define ESPFIX_STACKS_PER_PAGE (PAGE_SIZE/ESPFIX_STACK_SIZE)
  48. /* There is address space for how many espfix pages? */
  49. #define ESPFIX_PAGE_SPACE (1UL << (PGDIR_SHIFT-PAGE_SHIFT-16))
  50. #define ESPFIX_MAX_CPUS (ESPFIX_STACKS_PER_PAGE * ESPFIX_PAGE_SPACE)
  51. #if CONFIG_NR_CPUS > ESPFIX_MAX_CPUS
  52. # error "Need more than one PGD for the ESPFIX hack"
  53. #endif
  54. #define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO)
  55. /* This contains the *bottom* address of the espfix stack */
  56. DEFINE_PER_CPU_READ_MOSTLY(unsigned long, espfix_stack);
  57. DEFINE_PER_CPU_READ_MOSTLY(unsigned long, espfix_waddr);
  58. /* Initialization mutex - should this be a spinlock? */
  59. static DEFINE_MUTEX(espfix_init_mutex);
  60. /* Page allocation bitmap - each page serves ESPFIX_STACKS_PER_PAGE CPUs */
  61. #define ESPFIX_MAX_PAGES DIV_ROUND_UP(CONFIG_NR_CPUS, ESPFIX_STACKS_PER_PAGE)
  62. static void *espfix_pages[ESPFIX_MAX_PAGES];
  63. static __page_aligned_bss pud_t espfix_pud_page[PTRS_PER_PUD]
  64. __aligned(PAGE_SIZE);
  65. static unsigned int page_random, slot_random;
  66. /*
  67. * This returns the bottom address of the espfix stack for a specific CPU.
  68. * The math allows for a non-power-of-two ESPFIX_STACK_SIZE, in which case
  69. * we have to account for some amount of padding at the end of each page.
  70. */
  71. static inline unsigned long espfix_base_addr(unsigned int cpu)
  72. {
  73. unsigned long page, slot;
  74. unsigned long addr;
  75. page = (cpu / ESPFIX_STACKS_PER_PAGE) ^ page_random;
  76. slot = (cpu + slot_random) % ESPFIX_STACKS_PER_PAGE;
  77. addr = (page << PAGE_SHIFT) + (slot * ESPFIX_STACK_SIZE);
  78. addr = (addr & 0xffffUL) | ((addr & ~0xffffUL) << 16);
  79. addr += ESPFIX_BASE_ADDR;
  80. return addr;
  81. }
  82. #define PTE_STRIDE (65536/PAGE_SIZE)
  83. #define ESPFIX_PTE_CLONES (PTRS_PER_PTE/PTE_STRIDE)
  84. #define ESPFIX_PMD_CLONES PTRS_PER_PMD
  85. #define ESPFIX_PUD_CLONES (65536/(ESPFIX_PTE_CLONES*ESPFIX_PMD_CLONES))
  86. #define PGTABLE_PROT ((_KERNPG_TABLE & ~_PAGE_RW) | _PAGE_NX)
  87. static void init_espfix_random(void)
  88. {
  89. unsigned long rand;
  90. /*
  91. * This is run before the entropy pools are initialized,
  92. * but this is hopefully better than nothing.
  93. */
  94. if (!arch_get_random_long(&rand)) {
  95. /* The constant is an arbitrary large prime */
  96. rdtscll(rand);
  97. rand *= 0xc345c6b72fd16123UL;
  98. }
  99. slot_random = rand % ESPFIX_STACKS_PER_PAGE;
  100. page_random = (rand / ESPFIX_STACKS_PER_PAGE)
  101. & (ESPFIX_PAGE_SPACE - 1);
  102. }
  103. void __init init_espfix_bsp(void)
  104. {
  105. pgd_t *pgd_p;
  106. /* Install the espfix pud into the kernel page directory */
  107. pgd_p = &init_level4_pgt[pgd_index(ESPFIX_BASE_ADDR)];
  108. pgd_populate(&init_mm, pgd_p, (pud_t *)espfix_pud_page);
  109. /* Randomize the locations */
  110. init_espfix_random();
  111. /* The rest is the same as for any other processor */
  112. init_espfix_ap();
  113. }
  114. void init_espfix_ap(void)
  115. {
  116. unsigned int cpu, page;
  117. unsigned long addr;
  118. pud_t pud, *pud_p;
  119. pmd_t pmd, *pmd_p;
  120. pte_t pte, *pte_p;
  121. int n;
  122. void *stack_page;
  123. pteval_t ptemask;
  124. /* We only have to do this once... */
  125. if (likely(this_cpu_read(espfix_stack)))
  126. return; /* Already initialized */
  127. cpu = smp_processor_id();
  128. addr = espfix_base_addr(cpu);
  129. page = cpu/ESPFIX_STACKS_PER_PAGE;
  130. /* Did another CPU already set this up? */
  131. stack_page = ACCESS_ONCE(espfix_pages[page]);
  132. if (likely(stack_page))
  133. goto done;
  134. mutex_lock(&espfix_init_mutex);
  135. /* Did we race on the lock? */
  136. stack_page = ACCESS_ONCE(espfix_pages[page]);
  137. if (stack_page)
  138. goto unlock_done;
  139. ptemask = __supported_pte_mask;
  140. pud_p = &espfix_pud_page[pud_index(addr)];
  141. pud = *pud_p;
  142. if (!pud_present(pud)) {
  143. pmd_p = (pmd_t *)__get_free_page(PGALLOC_GFP);
  144. pud = __pud(__pa(pmd_p) | (PGTABLE_PROT & ptemask));
  145. paravirt_alloc_pmd(&init_mm, __pa(pmd_p) >> PAGE_SHIFT);
  146. for (n = 0; n < ESPFIX_PUD_CLONES; n++)
  147. set_pud(&pud_p[n], pud);
  148. }
  149. pmd_p = pmd_offset(&pud, addr);
  150. pmd = *pmd_p;
  151. if (!pmd_present(pmd)) {
  152. pte_p = (pte_t *)__get_free_page(PGALLOC_GFP);
  153. pmd = __pmd(__pa(pte_p) | (PGTABLE_PROT & ptemask));
  154. paravirt_alloc_pte(&init_mm, __pa(pte_p) >> PAGE_SHIFT);
  155. for (n = 0; n < ESPFIX_PMD_CLONES; n++)
  156. set_pmd(&pmd_p[n], pmd);
  157. }
  158. pte_p = pte_offset_kernel(&pmd, addr);
  159. stack_page = (void *)__get_free_page(GFP_KERNEL);
  160. pte = __pte(__pa(stack_page) | (__PAGE_KERNEL_RO & ptemask));
  161. for (n = 0; n < ESPFIX_PTE_CLONES; n++)
  162. set_pte(&pte_p[n*PTE_STRIDE], pte);
  163. /* Job is done for this CPU and any CPU which shares this page */
  164. ACCESS_ONCE(espfix_pages[page]) = stack_page;
  165. unlock_done:
  166. mutex_unlock(&espfix_init_mutex);
  167. done:
  168. this_cpu_write(espfix_stack, addr);
  169. this_cpu_write(espfix_waddr, (unsigned long)stack_page
  170. + (addr & ~PAGE_MASK));
  171. }