]>
Commit | Line | Data |
---|---|---|
767a67b0 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ef8b03fa RW |
2 | /* |
3 | * Hibernation support for x86-64 | |
4 | * | |
ef8b03fa | 5 | * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl> |
a2531293 | 6 | * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz> |
ef8b03fa RW |
7 | * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org> |
8 | */ | |
9 | ||
5a0e3ad6 | 10 | #include <linux/gfp.h> |
ef8b03fa RW |
11 | #include <linux/smp.h> |
12 | #include <linux/suspend.h> | |
62a03def CY |
13 | #include <linux/scatterlist.h> |
14 | #include <linux/kdebug.h> | |
15 | ||
16 | #include <crypto/hash.h> | |
8b78c21d | 17 | |
5520b7e7 | 18 | #include <asm/e820/api.h> |
8b78c21d | 19 | #include <asm/init.h> |
ef8b03fa RW |
20 | #include <asm/proto.h> |
21 | #include <asm/page.h> | |
22 | #include <asm/pgtable.h> | |
23 | #include <asm/mtrr.h> | |
7f8998c7 | 24 | #include <asm/sections.h> |
a8af7898 | 25 | #include <asm/suspend.h> |
65c0554b | 26 | #include <asm/tlbflush.h> |
ef8b03fa | 27 | |
c226fab4 | 28 | static int set_up_temporary_text_mapping(pgd_t *pgd) |
65c0554b RW |
29 | { |
30 | pmd_t *pmd; | |
31 | pud_t *pud; | |
91f606a8 | 32 | p4d_t *p4d = NULL; |
fb43d6cb DH |
33 | pgprot_t pgtable_prot = __pgprot(_KERNPG_TABLE); |
34 | pgprot_t pmd_text_prot = __pgprot(__PAGE_KERNEL_LARGE_EXEC); | |
35 | ||
36 | /* Filter out unsupported __PAGE_KERNEL* bits: */ | |
37 | pgprot_val(pmd_text_prot) &= __default_kernel_pte_mask; | |
38 | pgprot_val(pgtable_prot) &= __default_kernel_pte_mask; | |
65c0554b RW |
39 | |
40 | /* | |
41 | * The new mapping only has to cover the page containing the image | |
42 | * kernel's entry point (jump_address_phys), because the switch over to | |
43 | * it is carried out by relocated code running from a page allocated | |
44 | * specifically for this purpose and covered by the identity mapping, so | |
45 | * the temporary kernel text mapping is only needed for the final jump. | |
46 | * Moreover, in that mapping the virtual address of the image kernel's | |
47 | * entry point must be the same as its virtual address in the image | |
48 | * kernel (restore_jump_address), so the image kernel's | |
49 | * restore_registers() code doesn't find itself in a different area of | |
50 | * the virtual address space after switching over to the original page | |
51 | * tables used by the image kernel. | |
52 | */ | |
06c830a4 | 53 | |
ed7588d5 | 54 | if (pgtable_l5_enabled()) { |
06c830a4 KS |
55 | p4d = (p4d_t *)get_safe_page(GFP_ATOMIC); |
56 | if (!p4d) | |
57 | return -ENOMEM; | |
58 | } | |
59 | ||
65c0554b RW |
60 | pud = (pud_t *)get_safe_page(GFP_ATOMIC); |
61 | if (!pud) | |
62 | return -ENOMEM; | |
63 | ||
64 | pmd = (pmd_t *)get_safe_page(GFP_ATOMIC); | |
65 | if (!pmd) | |
66 | return -ENOMEM; | |
67 | ||
68 | set_pmd(pmd + pmd_index(restore_jump_address), | |
fb43d6cb | 69 | __pmd((jump_address_phys & PMD_MASK) | pgprot_val(pmd_text_prot))); |
65c0554b | 70 | set_pud(pud + pud_index(restore_jump_address), |
fb43d6cb | 71 | __pud(__pa(pmd) | pgprot_val(pgtable_prot))); |
91f606a8 | 72 | if (p4d) { |
fb43d6cb DH |
73 | p4d_t new_p4d = __p4d(__pa(pud) | pgprot_val(pgtable_prot)); |
74 | pgd_t new_pgd = __pgd(__pa(p4d) | pgprot_val(pgtable_prot)); | |
75 | ||
76 | set_p4d(p4d + p4d_index(restore_jump_address), new_p4d); | |
77 | set_pgd(pgd + pgd_index(restore_jump_address), new_pgd); | |
06c830a4 KS |
78 | } else { |
79 | /* No p4d for 4-level paging: point the pgd to the pud page table */ | |
05189820 | 80 | pgd_t new_pgd = __pgd(__pa(pud) | pgprot_val(pgtable_prot)); |
fb43d6cb | 81 | set_pgd(pgd + pgd_index(restore_jump_address), new_pgd); |
06c830a4 | 82 | } |
65c0554b RW |
83 | |
84 | return 0; | |
85 | } | |
ef8b03fa | 86 | |
8b78c21d | 87 | static void *alloc_pgt_page(void *context) |
ef8b03fa | 88 | { |
8b78c21d | 89 | return (void *)get_safe_page(GFP_ATOMIC); |
ef8b03fa RW |
90 | } |
91 | ||
92 | static int set_up_temporary_mappings(void) | |
93 | { | |
8b78c21d YL |
94 | struct x86_mapping_info info = { |
95 | .alloc_pgt_page = alloc_pgt_page, | |
66aad4fd | 96 | .page_flag = __PAGE_KERNEL_LARGE_EXEC, |
e4630fdd | 97 | .offset = __PAGE_OFFSET, |
8b78c21d YL |
98 | }; |
99 | unsigned long mstart, mend; | |
c226fab4 | 100 | pgd_t *pgd; |
8b78c21d YL |
101 | int result; |
102 | int i; | |
ef8b03fa | 103 | |
c226fab4 RW |
104 | pgd = (pgd_t *)get_safe_page(GFP_ATOMIC); |
105 | if (!pgd) | |
ef8b03fa RW |
106 | return -ENOMEM; |
107 | ||
65c0554b | 108 | /* Prepare a temporary mapping for the kernel text */ |
c226fab4 | 109 | result = set_up_temporary_text_mapping(pgd); |
65c0554b RW |
110 | if (result) |
111 | return result; | |
ef8b03fa RW |
112 | |
113 | /* Set up the direct mapping from scratch */ | |
8b78c21d YL |
114 | for (i = 0; i < nr_pfn_mapped; i++) { |
115 | mstart = pfn_mapped[i].start << PAGE_SHIFT; | |
116 | mend = pfn_mapped[i].end << PAGE_SHIFT; | |
117 | ||
c226fab4 | 118 | result = kernel_ident_mapping_init(&info, pgd, mstart, mend); |
8b78c21d YL |
119 | if (result) |
120 | return result; | |
ef8b03fa | 121 | } |
8b78c21d | 122 | |
72adf477 | 123 | temp_pgt = __pa(pgd); |
ef8b03fa RW |
124 | return 0; |
125 | } | |
126 | ||
328008a7 | 127 | asmlinkage int swsusp_arch_resume(void) |
ef8b03fa RW |
128 | { |
129 | int error; | |
130 | ||
131 | /* We have got enough memory and from now on we cannot recover */ | |
65c0554b RW |
132 | error = set_up_temporary_mappings(); |
133 | if (error) | |
ef8b03fa RW |
134 | return error; |
135 | ||
65c0554b RW |
136 | error = relocate_restore_code(); |
137 | if (error) | |
138 | return error; | |
ef8b03fa RW |
139 | |
140 | restore_image(); | |
141 | return 0; | |
142 | } |