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