"[PATCH] Fix leaks on /proc/{*/sched,sched_debug,timer_list,timer_stats}" and
[mmotm.git] / arch / x86 / power / hibernate_64.c
blob65fdc86e923fd92175b95bfef56ad1c82d64bc2d
1 /*
2 * Hibernation support for x86-64
4 * Distribute under GPLv2
6 * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
7 * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
8 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
9 */
11 #include <linux/smp.h>
12 #include <linux/suspend.h>
13 #include <asm/proto.h>
14 #include <asm/page.h>
15 #include <asm/pgtable.h>
16 #include <asm/mtrr.h>
17 #include <asm/suspend.h>
19 /* References to section boundaries */
20 extern const void __nosave_begin, __nosave_end;
22 /* Defined in hibernate_asm_64.S */
23 extern int restore_image(void);
26 * Address to jump to in the last phase of restore in order to get to the image
27 * kernel's text (this value is passed in the image header).
29 unsigned long restore_jump_address;
32 * Value of the cr3 register from before the hibernation (this value is passed
33 * in the image header).
35 unsigned long restore_cr3;
37 pgd_t *temp_level4_pgt;
39 void *relocated_restore_code;
41 static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
43 long i, j;
45 i = pud_index(address);
46 pud = pud + i;
47 for (; i < PTRS_PER_PUD; pud++, i++) {
48 unsigned long paddr;
49 pmd_t *pmd;
51 paddr = address + i*PUD_SIZE;
52 if (paddr >= end)
53 break;
55 pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
56 if (!pmd)
57 return -ENOMEM;
58 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
59 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
60 unsigned long pe;
62 if (paddr >= end)
63 break;
64 pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
65 pe &= __supported_pte_mask;
66 set_pmd(pmd, __pmd(pe));
69 return 0;
72 static int set_up_temporary_mappings(void)
74 unsigned long start, end, next;
75 int error;
77 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
78 if (!temp_level4_pgt)
79 return -ENOMEM;
81 /* It is safe to reuse the original kernel mapping */
82 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
83 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
85 /* Set up the direct mapping from scratch */
86 start = (unsigned long)pfn_to_kaddr(0);
87 end = (unsigned long)pfn_to_kaddr(max_pfn);
89 for (; start < end; start = next) {
90 pud_t *pud = (pud_t *)get_safe_page(GFP_ATOMIC);
91 if (!pud)
92 return -ENOMEM;
93 next = start + PGDIR_SIZE;
94 if (next > end)
95 next = end;
96 if ((error = res_phys_pud_init(pud, __pa(start), __pa(next))))
97 return error;
98 set_pgd(temp_level4_pgt + pgd_index(start),
99 mk_kernel_pgd(__pa(pud)));
101 return 0;
104 int swsusp_arch_resume(void)
106 int error;
108 /* We have got enough memory and from now on we cannot recover */
109 if ((error = set_up_temporary_mappings()))
110 return error;
112 relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
113 if (!relocated_restore_code)
114 return -ENOMEM;
115 memcpy(relocated_restore_code, &core_restore_code,
116 &restore_registers - &core_restore_code);
118 restore_image();
119 return 0;
123 * pfn_is_nosave - check if given pfn is in the 'nosave' section
126 int pfn_is_nosave(unsigned long pfn)
128 unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
129 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
130 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
133 struct restore_data_record {
134 unsigned long jump_address;
135 unsigned long cr3;
136 unsigned long magic;
139 #define RESTORE_MAGIC 0x0123456789ABCDEFUL
142 * arch_hibernation_header_save - populate the architecture specific part
143 * of a hibernation image header
144 * @addr: address to save the data at
146 int arch_hibernation_header_save(void *addr, unsigned int max_size)
148 struct restore_data_record *rdr = addr;
150 if (max_size < sizeof(struct restore_data_record))
151 return -EOVERFLOW;
152 rdr->jump_address = restore_jump_address;
153 rdr->cr3 = restore_cr3;
154 rdr->magic = RESTORE_MAGIC;
155 return 0;
159 * arch_hibernation_header_restore - read the architecture specific data
160 * from the hibernation image header
161 * @addr: address to read the data from
163 int arch_hibernation_header_restore(void *addr)
165 struct restore_data_record *rdr = addr;
167 restore_jump_address = rdr->jump_address;
168 restore_cr3 = rdr->cr3;
169 return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;