hibernate_32.c 3.8 KB

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