kasan_init.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * This file contains some kasan initialization code.
  3. *
  4. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  5. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/bootmem.h>
  13. #include <linux/init.h>
  14. #include <linux/kasan.h>
  15. #include <linux/kernel.h>
  16. #include <linux/memblock.h>
  17. #include <linux/pfn.h>
  18. #include <asm/page.h>
  19. #include <asm/pgalloc.h>
  20. /*
  21. * This page serves two purposes:
  22. * - It used as early shadow memory. The entire shadow region populated
  23. * with this page, before we will be able to setup normal shadow memory.
  24. * - Latter it reused it as zero shadow to cover large ranges of memory
  25. * that allowed to access, but not handled by kasan (vmalloc/vmemmap ...).
  26. */
  27. unsigned char kasan_zero_page[PAGE_SIZE] __page_aligned_bss;
  28. #if CONFIG_PGTABLE_LEVELS > 3
  29. pud_t kasan_zero_pud[PTRS_PER_PUD] __page_aligned_bss;
  30. #endif
  31. #if CONFIG_PGTABLE_LEVELS > 2
  32. pmd_t kasan_zero_pmd[PTRS_PER_PMD] __page_aligned_bss;
  33. #endif
  34. pte_t kasan_zero_pte[PTRS_PER_PTE] __page_aligned_bss;
  35. static __init void *early_alloc(size_t size, int node)
  36. {
  37. return memblock_virt_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS),
  38. BOOTMEM_ALLOC_ACCESSIBLE, node);
  39. }
  40. static void __init zero_pte_populate(pmd_t *pmd, unsigned long addr,
  41. unsigned long end)
  42. {
  43. pte_t *pte = pte_offset_kernel(pmd, addr);
  44. pte_t zero_pte;
  45. zero_pte = pfn_pte(PFN_DOWN(__pa(kasan_zero_page)), PAGE_KERNEL);
  46. zero_pte = pte_wrprotect(zero_pte);
  47. while (addr + PAGE_SIZE <= end) {
  48. set_pte_at(&init_mm, addr, pte, zero_pte);
  49. addr += PAGE_SIZE;
  50. pte = pte_offset_kernel(pmd, addr);
  51. }
  52. }
  53. static void __init zero_pmd_populate(pud_t *pud, unsigned long addr,
  54. unsigned long end)
  55. {
  56. pmd_t *pmd = pmd_offset(pud, addr);
  57. unsigned long next;
  58. do {
  59. next = pmd_addr_end(addr, end);
  60. if (IS_ALIGNED(addr, PMD_SIZE) && end - addr >= PMD_SIZE) {
  61. pmd_populate_kernel(&init_mm, pmd, kasan_zero_pte);
  62. continue;
  63. }
  64. if (pmd_none(*pmd)) {
  65. pmd_populate_kernel(&init_mm, pmd,
  66. early_alloc(PAGE_SIZE, NUMA_NO_NODE));
  67. }
  68. zero_pte_populate(pmd, addr, next);
  69. } while (pmd++, addr = next, addr != end);
  70. }
  71. static void __init zero_pud_populate(pgd_t *pgd, unsigned long addr,
  72. unsigned long end)
  73. {
  74. pud_t *pud = pud_offset(pgd, addr);
  75. unsigned long next;
  76. do {
  77. next = pud_addr_end(addr, end);
  78. if (IS_ALIGNED(addr, PUD_SIZE) && end - addr >= PUD_SIZE) {
  79. pmd_t *pmd;
  80. pud_populate(&init_mm, pud, kasan_zero_pmd);
  81. pmd = pmd_offset(pud, addr);
  82. pmd_populate_kernel(&init_mm, pmd, kasan_zero_pte);
  83. continue;
  84. }
  85. if (pud_none(*pud)) {
  86. pud_populate(&init_mm, pud,
  87. early_alloc(PAGE_SIZE, NUMA_NO_NODE));
  88. }
  89. zero_pmd_populate(pud, addr, next);
  90. } while (pud++, addr = next, addr != end);
  91. }
  92. /**
  93. * kasan_populate_zero_shadow - populate shadow memory region with
  94. * kasan_zero_page
  95. * @shadow_start - start of the memory range to populate
  96. * @shadow_end - end of the memory range to populate
  97. */
  98. void __init kasan_populate_zero_shadow(const void *shadow_start,
  99. const void *shadow_end)
  100. {
  101. unsigned long addr = (unsigned long)shadow_start;
  102. unsigned long end = (unsigned long)shadow_end;
  103. pgd_t *pgd = pgd_offset_k(addr);
  104. unsigned long next;
  105. do {
  106. next = pgd_addr_end(addr, end);
  107. if (IS_ALIGNED(addr, PGDIR_SIZE) && end - addr >= PGDIR_SIZE) {
  108. pud_t *pud;
  109. pmd_t *pmd;
  110. /*
  111. * kasan_zero_pud should be populated with pmds
  112. * at this moment.
  113. * [pud,pmd]_populate*() below needed only for
  114. * 3,2 - level page tables where we don't have
  115. * puds,pmds, so pgd_populate(), pud_populate()
  116. * is noops.
  117. */
  118. pgd_populate(&init_mm, pgd, kasan_zero_pud);
  119. pud = pud_offset(pgd, addr);
  120. pud_populate(&init_mm, pud, kasan_zero_pmd);
  121. pmd = pmd_offset(pud, addr);
  122. pmd_populate_kernel(&init_mm, pmd, kasan_zero_pte);
  123. continue;
  124. }
  125. if (pgd_none(*pgd)) {
  126. pgd_populate(&init_mm, pgd,
  127. early_alloc(PAGE_SIZE, NUMA_NO_NODE));
  128. }
  129. zero_pud_populate(pgd, addr, next);
  130. } while (pgd++, addr = next, addr != end);
  131. }