2 * Suspend support specific for i386 - temporary page tables
4 * Distribute under GPLv2
6 * Copyright (c) 2006 Rafael J. Wysocki <rjw@sisk.pl>
9 #include <linux/suspend.h>
10 #include <linux/bootmem.h>
12 #include <asm/system.h>
14 #include <asm/pgtable.h>
16 /* Defined in arch/i386/power/swsusp.S */
17 extern int restore_image(void);
19 /* Pointer to the temporary resume page tables */
22 /* The following three functions are based on the analogous code in
27 * Create a middle page table on a resume-safe page and put a pointer to it in
28 * the given global directory entry. This only returns the gd entry
29 * in non-PAE compilation mode, since the middle layer is folded.
31 static pmd_t *resume_one_md_table_init(pgd_t *pgd)
37 pmd_table = (pmd_t *)get_safe_page(GFP_ATOMIC);
41 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
42 pud = pud_offset(pgd, 0);
44 BUG_ON(pmd_table != pmd_offset(pud, 0));
46 pud = pud_offset(pgd, 0);
47 pmd_table = pmd_offset(pud, 0);
54 * Create a page table on a resume-safe page and place a pointer to it in
55 * a middle page directory entry.
57 static pte_t *resume_one_page_table_init(pmd_t *pmd)
60 pte_t *page_table = (pte_t *)get_safe_page(GFP_ATOMIC);
64 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
66 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
71 return pte_offset_kernel(pmd, 0);
75 * This maps the physical memory to kernel virtual address space, a total
76 * of max_low_pfn pages, by creating page tables starting from address
77 * PAGE_OFFSET. The page tables are allocated out of resume-safe pages.
79 static int resume_physical_mapping_init(pgd_t *pgd_base)
87 pgd_idx = pgd_index(PAGE_OFFSET);
88 pgd = pgd_base + pgd_idx;
91 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
92 pmd = resume_one_md_table_init(pgd);
96 if (pfn >= max_low_pfn)
99 for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD; pmd++, pmd_idx++) {
100 if (pfn >= max_low_pfn)
103 /* Map with big pages if possible, otherwise create
104 * normal page tables.
105 * NOTE: We can mark everything as executable here
108 set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
113 pte = resume_one_page_table_init(pmd);
117 max_pte = pte + PTRS_PER_PTE;
118 for (; pte < max_pte; pte++, pfn++) {
119 if (pfn >= max_low_pfn)
122 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
130 static inline void resume_init_first_level_page_table(pgd_t *pg_dir)
132 #ifdef CONFIG_X86_PAE
135 /* Init entries of the first-level page table to the zero page */
136 for (i = 0; i < PTRS_PER_PGD; i++)
138 __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
142 int swsusp_arch_resume(void)
146 resume_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
150 resume_init_first_level_page_table(resume_pg_dir);
151 error = resume_physical_mapping_init(resume_pg_dir);
155 /* We have got enough memory and from now on we cannot recover */