hibernate_32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Hibernation support specific for i386 - temporary page tables
  3. *
  4. * Distribute under GPLv2
  5. *
  6. * Copyright (c) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  7. */
  8. #include <linux/gfp.h>
  9. #include <linux/suspend.h>
  10. #include <linux/bootmem.h>
  11. #include <asm/system.h>
  12. #include <asm/page.h>
  13. #include <asm/pgtable.h>
  14. #include <asm/mmzone.h>
  15. /* Defined in hibernate_asm_32.S */
  16. extern int restore_image(void);
  17. /* References to section boundaries */
  18. extern const void __nosave_begin, __nosave_end;
  19. /* Pointer to the temporary resume page tables */
  20. pgd_t *resume_pg_dir;
  21. /* The following three functions are based on the analogous code in
  22. * arch/x86/mm/init_32.c
  23. */
  24. /*
  25. * Create a middle page table on a resume-safe page and put a pointer to it in
  26. * the given global directory entry. This only returns the gd entry
  27. * in non-PAE compilation mode, since the middle layer is folded.
  28. */
  29. static pmd_t *resume_one_md_table_init(pgd_t *pgd)
  30. {
  31. pud_t *pud;
  32. pmd_t *pmd_table;
  33. #ifdef CONFIG_X86_PAE
  34. pmd_table = (pmd_t *)get_safe_page(GFP_ATOMIC);
  35. if (!pmd_table)
  36. return NULL;
  37. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  38. pud = pud_offset(pgd, 0);
  39. BUG_ON(pmd_table != pmd_offset(pud, 0));
  40. #else
  41. pud = pud_offset(pgd, 0);
  42. pmd_table = pmd_offset(pud, 0);
  43. #endif
  44. return pmd_table;
  45. }
  46. /*
  47. * Create a page table on a resume-safe page and place a pointer to it in
  48. * a middle page directory entry.
  49. */
  50. static pte_t *resume_one_page_table_init(pmd_t *pmd)
  51. {
  52. if (pmd_none(*pmd)) {
  53. pte_t *page_table = (pte_t *)get_safe_page(GFP_ATOMIC);
  54. if (!page_table)
  55. return NULL;
  56. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  57. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  58. return page_table;
  59. }
  60. return pte_offset_kernel(pmd, 0);
  61. }
  62. /*
  63. * This maps the physical memory to kernel virtual address space, a total
  64. * of max_low_pfn pages, by creating page tables starting from address
  65. * PAGE_OFFSET. The page tables are allocated out of resume-safe pages.
  66. */
  67. static int resume_physical_mapping_init(pgd_t *pgd_base)
  68. {
  69. unsigned long pfn;
  70. pgd_t *pgd;
  71. pmd_t *pmd;
  72. pte_t *pte;
  73. int pgd_idx, pmd_idx;
  74. pgd_idx = pgd_index(PAGE_OFFSET);
  75. pgd = pgd_base + pgd_idx;
  76. pfn = 0;
  77. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  78. pmd = resume_one_md_table_init(pgd);
  79. if (!pmd)
  80. return -ENOMEM;
  81. if (pfn >= max_low_pfn)
  82. continue;
  83. for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD; pmd++, pmd_idx++) {
  84. if (pfn >= max_low_pfn)
  85. break;
  86. /* Map with big pages if possible, otherwise create
  87. * normal page tables.
  88. * NOTE: We can mark everything as executable here
  89. */
  90. if (cpu_has_pse) {
  91. set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
  92. pfn += PTRS_PER_PTE;
  93. } else {
  94. pte_t *max_pte;
  95. pte = resume_one_page_table_init(pmd);
  96. if (!pte)
  97. return -ENOMEM;
  98. max_pte = pte + PTRS_PER_PTE;
  99. for (; pte < max_pte; pte++, pfn++) {
  100. if (pfn >= max_low_pfn)
  101. break;
  102. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
  103. }
  104. }
  105. }
  106. }
  107. resume_map_numa_kva(pgd_base);
  108. return 0;
  109. }
  110. static inline void resume_init_first_level_page_table(pgd_t *pg_dir)
  111. {
  112. #ifdef CONFIG_X86_PAE
  113. int i;
  114. /* Init entries of the first-level page table to the zero page */
  115. for (i = 0; i < PTRS_PER_PGD; i++)
  116. set_pgd(pg_dir + i,
  117. __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
  118. #endif
  119. }
  120. int swsusp_arch_resume(void)
  121. {
  122. int error;
  123. resume_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
  124. if (!resume_pg_dir)
  125. return -ENOMEM;
  126. resume_init_first_level_page_table(resume_pg_dir);
  127. error = resume_physical_mapping_init(resume_pg_dir);
  128. if (error)
  129. return error;
  130. /* We have got enough memory and from now on we cannot recover */
  131. restore_image();
  132. return 0;
  133. }
  134. /*
  135. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  136. */
  137. int pfn_is_nosave(unsigned long pfn)
  138. {
  139. unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  140. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  141. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  142. }