1767a67b0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c5759124SRafael J. Wysocki /*
3c5759124SRafael J. Wysocki * Hibernation support specific for i386 - temporary page tables
4c5759124SRafael J. Wysocki *
5c5759124SRafael J. Wysocki * Copyright (c) 2006 Rafael J. Wysocki <rjw@sisk.pl>
6c5759124SRafael J. Wysocki */
7c5759124SRafael J. Wysocki
85a0e3ad6STejun Heo #include <linux/gfp.h>
9c5759124SRafael J. Wysocki #include <linux/suspend.h>
1057c8a661SMike Rapoport #include <linux/memblock.h>
11*65fddcfcSMike Rapoport #include <linux/pgtable.h>
12c5759124SRafael J. Wysocki
13c5759124SRafael J. Wysocki #include <asm/page.h>
1497a70e54SRafael J. Wysocki #include <asm/mmzone.h>
157f8998c7SGeert Uytterhoeven #include <asm/sections.h>
1625862a04SZhimin Gu #include <asm/suspend.h>
17c5759124SRafael J. Wysocki
18c5759124SRafael J. Wysocki /* Pointer to the temporary resume page tables */
19c5759124SRafael J. Wysocki pgd_t *resume_pg_dir;
20c5759124SRafael J. Wysocki
21c5759124SRafael J. Wysocki /* The following three functions are based on the analogous code in
22261f0ce5SRafael J. Wysocki * arch/x86/mm/init_32.c
23c5759124SRafael J. Wysocki */
24c5759124SRafael J. Wysocki
25c5759124SRafael J. Wysocki /*
26c5759124SRafael J. Wysocki * Create a middle page table on a resume-safe page and put a pointer to it in
27c5759124SRafael J. Wysocki * the given global directory entry. This only returns the gd entry
28c5759124SRafael J. Wysocki * in non-PAE compilation mode, since the middle layer is folded.
29c5759124SRafael J. Wysocki */
resume_one_md_table_init(pgd_t * pgd)30c5759124SRafael J. Wysocki static pmd_t *resume_one_md_table_init(pgd_t *pgd)
31c5759124SRafael J. Wysocki {
32e0c4f675SKirill A. Shutemov p4d_t *p4d;
33c5759124SRafael J. Wysocki pud_t *pud;
34c5759124SRafael J. Wysocki pmd_t *pmd_table;
35c5759124SRafael J. Wysocki
36c5759124SRafael J. Wysocki #ifdef CONFIG_X86_PAE
37c5759124SRafael J. Wysocki pmd_table = (pmd_t *)get_safe_page(GFP_ATOMIC);
38c5759124SRafael J. Wysocki if (!pmd_table)
39c5759124SRafael J. Wysocki return NULL;
40c5759124SRafael J. Wysocki
41c5759124SRafael J. Wysocki set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
42e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, 0);
43e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, 0);
44c5759124SRafael J. Wysocki
45c5759124SRafael J. Wysocki BUG_ON(pmd_table != pmd_offset(pud, 0));
46c5759124SRafael J. Wysocki #else
47e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, 0);
48e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, 0);
49c5759124SRafael J. Wysocki pmd_table = pmd_offset(pud, 0);
50c5759124SRafael J. Wysocki #endif
51c5759124SRafael J. Wysocki
52c5759124SRafael J. Wysocki return pmd_table;
53c5759124SRafael J. Wysocki }
54c5759124SRafael J. Wysocki
55c5759124SRafael J. Wysocki /*
56c5759124SRafael J. Wysocki * Create a page table on a resume-safe page and place a pointer to it in
57c5759124SRafael J. Wysocki * a middle page directory entry.
58c5759124SRafael J. Wysocki */
resume_one_page_table_init(pmd_t * pmd)59c5759124SRafael J. Wysocki static pte_t *resume_one_page_table_init(pmd_t *pmd)
60c5759124SRafael J. Wysocki {
61c5759124SRafael J. Wysocki if (pmd_none(*pmd)) {
62c5759124SRafael J. Wysocki pte_t *page_table = (pte_t *)get_safe_page(GFP_ATOMIC);
63c5759124SRafael J. Wysocki if (!page_table)
64c5759124SRafael J. Wysocki return NULL;
65c5759124SRafael J. Wysocki
66c5759124SRafael J. Wysocki set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
67c5759124SRafael J. Wysocki
68c5759124SRafael J. Wysocki BUG_ON(page_table != pte_offset_kernel(pmd, 0));
69c5759124SRafael J. Wysocki
70c5759124SRafael J. Wysocki return page_table;
71c5759124SRafael J. Wysocki }
72c5759124SRafael J. Wysocki
73c5759124SRafael J. Wysocki return pte_offset_kernel(pmd, 0);
74c5759124SRafael J. Wysocki }
75c5759124SRafael J. Wysocki
76c5759124SRafael J. Wysocki /*
77c5759124SRafael J. Wysocki * This maps the physical memory to kernel virtual address space, a total
78c5759124SRafael J. Wysocki * of max_low_pfn pages, by creating page tables starting from address
79c5759124SRafael J. Wysocki * PAGE_OFFSET. The page tables are allocated out of resume-safe pages.
80c5759124SRafael J. Wysocki */
resume_physical_mapping_init(pgd_t * pgd_base)81c5759124SRafael J. Wysocki static int resume_physical_mapping_init(pgd_t *pgd_base)
82c5759124SRafael J. Wysocki {
83c5759124SRafael J. Wysocki unsigned long pfn;
84c5759124SRafael J. Wysocki pgd_t *pgd;
85c5759124SRafael J. Wysocki pmd_t *pmd;
86c5759124SRafael J. Wysocki pte_t *pte;
87c5759124SRafael J. Wysocki int pgd_idx, pmd_idx;
88c5759124SRafael J. Wysocki
89c5759124SRafael J. Wysocki pgd_idx = pgd_index(PAGE_OFFSET);
90c5759124SRafael J. Wysocki pgd = pgd_base + pgd_idx;
91c5759124SRafael J. Wysocki pfn = 0;
92c5759124SRafael J. Wysocki
93c5759124SRafael J. Wysocki for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
94c5759124SRafael J. Wysocki pmd = resume_one_md_table_init(pgd);
95c5759124SRafael J. Wysocki if (!pmd)
96c5759124SRafael J. Wysocki return -ENOMEM;
97c5759124SRafael J. Wysocki
98c5759124SRafael J. Wysocki if (pfn >= max_low_pfn)
99c5759124SRafael J. Wysocki continue;
100c5759124SRafael J. Wysocki
101c5759124SRafael J. Wysocki for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD; pmd++, pmd_idx++) {
102c5759124SRafael J. Wysocki if (pfn >= max_low_pfn)
103c5759124SRafael J. Wysocki break;
104c5759124SRafael J. Wysocki
105c5759124SRafael J. Wysocki /* Map with big pages if possible, otherwise create
106c5759124SRafael J. Wysocki * normal page tables.
107c5759124SRafael J. Wysocki * NOTE: We can mark everything as executable here
108c5759124SRafael J. Wysocki */
10916bf9226SBorislav Petkov if (boot_cpu_has(X86_FEATURE_PSE)) {
110c5759124SRafael J. Wysocki set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
111c5759124SRafael J. Wysocki pfn += PTRS_PER_PTE;
112c5759124SRafael J. Wysocki } else {
113c5759124SRafael J. Wysocki pte_t *max_pte;
114c5759124SRafael J. Wysocki
115c5759124SRafael J. Wysocki pte = resume_one_page_table_init(pmd);
116c5759124SRafael J. Wysocki if (!pte)
117c5759124SRafael J. Wysocki return -ENOMEM;
118c5759124SRafael J. Wysocki
119c5759124SRafael J. Wysocki max_pte = pte + PTRS_PER_PTE;
120c5759124SRafael J. Wysocki for (; pte < max_pte; pte++, pfn++) {
121c5759124SRafael J. Wysocki if (pfn >= max_low_pfn)
122c5759124SRafael J. Wysocki break;
123c5759124SRafael J. Wysocki
124c5759124SRafael J. Wysocki set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
125c5759124SRafael J. Wysocki }
126c5759124SRafael J. Wysocki }
127c5759124SRafael J. Wysocki }
128c5759124SRafael J. Wysocki }
12997a70e54SRafael J. Wysocki
130c5759124SRafael J. Wysocki return 0;
131c5759124SRafael J. Wysocki }
132c5759124SRafael J. Wysocki
resume_init_first_level_page_table(pgd_t * pg_dir)133c5759124SRafael J. Wysocki static inline void resume_init_first_level_page_table(pgd_t *pg_dir)
134c5759124SRafael J. Wysocki {
135c5759124SRafael J. Wysocki #ifdef CONFIG_X86_PAE
136c5759124SRafael J. Wysocki int i;
137c5759124SRafael J. Wysocki
138c5759124SRafael J. Wysocki /* Init entries of the first-level page table to the zero page */
139c5759124SRafael J. Wysocki for (i = 0; i < PTRS_PER_PGD; i++)
140c5759124SRafael J. Wysocki set_pgd(pg_dir + i,
141c5759124SRafael J. Wysocki __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
142c5759124SRafael J. Wysocki #endif
143c5759124SRafael J. Wysocki }
144c5759124SRafael J. Wysocki
set_up_temporary_text_mapping(pgd_t * pgd_base)1455331d2c7SZhimin Gu static int set_up_temporary_text_mapping(pgd_t *pgd_base)
1465331d2c7SZhimin Gu {
1475331d2c7SZhimin Gu pgd_t *pgd;
1485331d2c7SZhimin Gu pmd_t *pmd;
1495331d2c7SZhimin Gu pte_t *pte;
1505331d2c7SZhimin Gu
1515331d2c7SZhimin Gu pgd = pgd_base + pgd_index(restore_jump_address);
1525331d2c7SZhimin Gu
1535331d2c7SZhimin Gu pmd = resume_one_md_table_init(pgd);
1545331d2c7SZhimin Gu if (!pmd)
1555331d2c7SZhimin Gu return -ENOMEM;
1565331d2c7SZhimin Gu
1575331d2c7SZhimin Gu if (boot_cpu_has(X86_FEATURE_PSE)) {
1585331d2c7SZhimin Gu set_pmd(pmd + pmd_index(restore_jump_address),
1595331d2c7SZhimin Gu __pmd((jump_address_phys & PMD_MASK) | pgprot_val(PAGE_KERNEL_LARGE_EXEC)));
1605331d2c7SZhimin Gu } else {
1615331d2c7SZhimin Gu pte = resume_one_page_table_init(pmd);
1625331d2c7SZhimin Gu if (!pte)
1635331d2c7SZhimin Gu return -ENOMEM;
1645331d2c7SZhimin Gu set_pte(pte + pte_index(restore_jump_address),
1655331d2c7SZhimin Gu __pte((jump_address_phys & PAGE_MASK) | pgprot_val(PAGE_KERNEL_EXEC)));
1665331d2c7SZhimin Gu }
1675331d2c7SZhimin Gu
1685331d2c7SZhimin Gu return 0;
1695331d2c7SZhimin Gu }
1705331d2c7SZhimin Gu
swsusp_arch_resume(void)171328008a7SArnd Bergmann asmlinkage int swsusp_arch_resume(void)
172c5759124SRafael J. Wysocki {
173c5759124SRafael J. Wysocki int error;
174c5759124SRafael J. Wysocki
175c5759124SRafael J. Wysocki resume_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
176c5759124SRafael J. Wysocki if (!resume_pg_dir)
177c5759124SRafael J. Wysocki return -ENOMEM;
178c5759124SRafael J. Wysocki
179c5759124SRafael J. Wysocki resume_init_first_level_page_table(resume_pg_dir);
1805331d2c7SZhimin Gu
1815331d2c7SZhimin Gu error = set_up_temporary_text_mapping(resume_pg_dir);
1825331d2c7SZhimin Gu if (error)
1835331d2c7SZhimin Gu return error;
1845331d2c7SZhimin Gu
185c5759124SRafael J. Wysocki error = resume_physical_mapping_init(resume_pg_dir);
186c5759124SRafael J. Wysocki if (error)
187c5759124SRafael J. Wysocki return error;
188c5759124SRafael J. Wysocki
1897c0a9827SZhimin Gu temp_pgt = __pa(resume_pg_dir);
1907c0a9827SZhimin Gu
1916bae499aSZhimin Gu error = relocate_restore_code();
1926bae499aSZhimin Gu if (error)
1936bae499aSZhimin Gu return error;
1946bae499aSZhimin Gu
195c5759124SRafael J. Wysocki /* We have got enough memory and from now on we cannot recover */
196c5759124SRafael J. Wysocki restore_image();
197c5759124SRafael J. Wysocki return 0;
198c5759124SRafael J. Wysocki }
199