blob: 304fca20d96ee3e1540d7045a9e0e25118c240b0 [file] [log] [blame]
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +01001/*
2 * Hibernation support for x86-64
3 *
4 * Distribute under GPLv2
5 *
6 * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +01008 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
9 */
10
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010012#include <linux/smp.h>
13#include <linux/suspend.h>
Yinghai Lu8b78c212013-01-24 12:20:14 -080014
15#include <asm/init.h>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010016#include <asm/proto.h>
17#include <asm/page.h>
18#include <asm/pgtable.h>
19#include <asm/mtrr.h>
Magnus Damma8af7892009-03-31 15:23:37 -070020#include <asm/suspend.h>
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010021
22/* References to section boundaries */
Andi Kleend6efc2f2013-08-05 15:02:49 -070023extern __visible const void __nosave_begin, __nosave_end;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010024
Rafael J. Wysocki261f0ce2008-02-09 23:24:09 +010025/* Defined in hibernate_asm_64.S */
Andi Kleend6efc2f2013-08-05 15:02:49 -070026extern asmlinkage int restore_image(void);
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010027
28/*
29 * Address to jump to in the last phase of restore in order to get to the image
30 * kernel's text (this value is passed in the image header).
31 */
Andi Kleend6efc2f2013-08-05 15:02:49 -070032unsigned long restore_jump_address __visible;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010033
34/*
35 * Value of the cr3 register from before the hibernation (this value is passed
36 * in the image header).
37 */
Andi Kleend6efc2f2013-08-05 15:02:49 -070038unsigned long restore_cr3 __visible;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010039
Andi Kleend6efc2f2013-08-05 15:02:49 -070040pgd_t *temp_level4_pgt __visible;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010041
Andi Kleend6efc2f2013-08-05 15:02:49 -070042void *relocated_restore_code __visible;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010043
Yinghai Lu8b78c212013-01-24 12:20:14 -080044static void *alloc_pgt_page(void *context)
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010045{
Yinghai Lu8b78c212013-01-24 12:20:14 -080046 return (void *)get_safe_page(GFP_ATOMIC);
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010047}
48
49static int set_up_temporary_mappings(void)
50{
Yinghai Lu8b78c212013-01-24 12:20:14 -080051 struct x86_mapping_info info = {
52 .alloc_pgt_page = alloc_pgt_page,
53 .pmd_flag = __PAGE_KERNEL_LARGE_EXEC,
54 .kernel_mapping = true,
55 };
56 unsigned long mstart, mend;
57 int result;
58 int i;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010059
60 temp_level4_pgt = (pgd_t *)get_safe_page(GFP_ATOMIC);
61 if (!temp_level4_pgt)
62 return -ENOMEM;
63
64 /* It is safe to reuse the original kernel mapping */
65 set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
66 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
67
68 /* Set up the direct mapping from scratch */
Yinghai Lu8b78c212013-01-24 12:20:14 -080069 for (i = 0; i < nr_pfn_mapped; i++) {
70 mstart = pfn_mapped[i].start << PAGE_SHIFT;
71 mend = pfn_mapped[i].end << PAGE_SHIFT;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010072
Yinghai Lu8b78c212013-01-24 12:20:14 -080073 result = kernel_ident_mapping_init(&info, temp_level4_pgt,
74 mstart, mend);
75
76 if (result)
77 return result;
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010078 }
Yinghai Lu8b78c212013-01-24 12:20:14 -080079
Rafael J. Wysockief8b03f2008-02-09 23:24:09 +010080 return 0;
81}
82
83int swsusp_arch_resume(void)
84{
85 int error;
86
87 /* We have got enough memory and from now on we cannot recover */
88 if ((error = set_up_temporary_mappings()))
89 return error;
90
91 relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
92 if (!relocated_restore_code)
93 return -ENOMEM;
94 memcpy(relocated_restore_code, &core_restore_code,
95 &restore_registers - &core_restore_code);
96
97 restore_image();
98 return 0;
99}
100
101/*
102 * pfn_is_nosave - check if given pfn is in the 'nosave' section
103 */
104
105int pfn_is_nosave(unsigned long pfn)
106{
107 unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
108 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
109 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
110}
111
112struct restore_data_record {
113 unsigned long jump_address;
114 unsigned long cr3;
115 unsigned long magic;
116};
117
118#define RESTORE_MAGIC 0x0123456789ABCDEFUL
119
120/**
121 * arch_hibernation_header_save - populate the architecture specific part
122 * of a hibernation image header
123 * @addr: address to save the data at
124 */
125int arch_hibernation_header_save(void *addr, unsigned int max_size)
126{
127 struct restore_data_record *rdr = addr;
128
129 if (max_size < sizeof(struct restore_data_record))
130 return -EOVERFLOW;
131 rdr->jump_address = restore_jump_address;
132 rdr->cr3 = restore_cr3;
133 rdr->magic = RESTORE_MAGIC;
134 return 0;
135}
136
137/**
138 * arch_hibernation_header_restore - read the architecture specific data
139 * from the hibernation image header
140 * @addr: address to read the data from
141 */
142int arch_hibernation_header_restore(void *addr)
143{
144 struct restore_data_record *rdr = addr;
145
146 restore_jump_address = rdr->jump_address;
147 restore_cr3 = rdr->cr3;
148 return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL;
149}