]>
Commit | Line | Data |
---|---|---|
3a60a9f5 | 1 | /* |
39236c6e | 2 | * Copyright (c) 2004-2012 Apple Inc. All rights reserved. |
3a60a9f5 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
3a60a9f5 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
3a60a9f5 A |
27 | */ |
28 | ||
29 | #include <kern/machine.h> | |
30 | #include <kern/misc_protos.h> | |
31 | #include <kern/thread.h> | |
32 | #include <kern/processor.h> | |
33 | #include <kern/kalloc.h> | |
34 | #include <mach/machine.h> | |
35 | #include <mach/processor_info.h> | |
36 | #include <mach/mach_types.h> | |
37 | #include <i386/pmap.h> | |
38 | #include <kern/cpu_data.h> | |
39 | #include <IOKit/IOPlatformExpert.h> | |
0c530ab8 A |
40 | |
41 | #include <pexpert/i386/efi.h> | |
3a60a9f5 A |
42 | |
43 | #include <IOKit/IOHibernatePrivate.h> | |
44 | #include <vm/vm_page.h> | |
b0d623f7 | 45 | #include <i386/i386_lowmem.h> |
5ba3f43e | 46 | #include <san/kasan.h> |
21362eb3 | 47 | |
0b4c1975 A |
48 | extern ppnum_t max_ppnum; |
49 | ||
0c530ab8 | 50 | #define MAX_BANKS 32 |
6601e61a | 51 | |
0c530ab8 | 52 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
6601e61a | 53 | |
3a60a9f5 | 54 | hibernate_page_list_t * |
db609669 | 55 | hibernate_page_list_allocate(boolean_t log) |
3a60a9f5 | 56 | { |
0c530ab8 | 57 | ppnum_t base, num; |
3a60a9f5 | 58 | vm_size_t size; |
0c530ab8 | 59 | uint32_t bank, num_banks; |
3a60a9f5 A |
60 | uint32_t pages, page_count; |
61 | hibernate_page_list_t * list; | |
62 | hibernate_bitmap_t * bitmap; | |
6601e61a | 63 | |
0c530ab8 A |
64 | EfiMemoryRange * mptr; |
65 | uint32_t mcount, msize, i; | |
66 | hibernate_bitmap_t dram_ranges[MAX_BANKS]; | |
67 | boot_args * args = (boot_args *) PE_state.bootArgs; | |
39236c6e | 68 | uint32_t non_os_pagecount; |
5ba3f43e | 69 | ppnum_t pnmax = max_ppnum; |
89b3af67 | 70 | |
b0d623f7 | 71 | mptr = (EfiMemoryRange *)ml_static_ptovirt(args->MemoryMap); |
0c530ab8 A |
72 | if (args->MemoryMapDescriptorSize == 0) |
73 | panic("Invalid memory map descriptor size"); | |
74 | msize = args->MemoryMapDescriptorSize; | |
75 | mcount = args->MemoryMapSize / msize; | |
4452a7af | 76 | |
5ba3f43e A |
77 | #if KASAN |
78 | /* adjust max page number to include stolen memory */ | |
79 | if (atop(shadow_ptop) > pnmax) { | |
80 | pnmax = (ppnum_t)atop(shadow_ptop); | |
81 | } | |
82 | #endif | |
83 | ||
0c530ab8 | 84 | num_banks = 0; |
39236c6e | 85 | non_os_pagecount = 0; |
0c530ab8 | 86 | for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) |
6601e61a | 87 | { |
0c530ab8 A |
88 | base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT); |
89 | num = (ppnum_t) mptr->NumberOfPages; | |
0b4c1975 | 90 | |
5ba3f43e A |
91 | #if KASAN |
92 | if (i == shadow_stolen_idx) { | |
93 | /* | |
94 | * Add all stolen pages to the bitmap. Later we will prune the unused | |
95 | * pages. | |
96 | */ | |
97 | num += shadow_pages_total; | |
98 | } | |
99 | #endif | |
100 | ||
101 | if (base > pnmax) | |
0b4c1975 | 102 | continue; |
5ba3f43e A |
103 | if ((base + num - 1) > pnmax) |
104 | num = pnmax - base + 1; | |
0c530ab8 | 105 | if (!num) |
6d2010ae | 106 | continue; |
0c530ab8 A |
107 | |
108 | switch (mptr->Type) | |
109 | { | |
110 | // any kind of dram | |
39236c6e A |
111 | case kEfiACPIMemoryNVS: |
112 | case kEfiPalCode: | |
113 | non_os_pagecount += num; | |
114 | ||
115 | // OS used dram | |
0c530ab8 A |
116 | case kEfiLoaderCode: |
117 | case kEfiLoaderData: | |
118 | case kEfiBootServicesCode: | |
119 | case kEfiBootServicesData: | |
120 | case kEfiConventionalMemory: | |
0c530ab8 | 121 | |
b0d623f7 A |
122 | for (bank = 0; bank < num_banks; bank++) |
123 | { | |
124 | if (dram_ranges[bank].first_page <= base) | |
125 | continue; | |
126 | if ((base + num) == dram_ranges[bank].first_page) | |
127 | { | |
128 | dram_ranges[bank].first_page = base; | |
129 | num = 0; | |
130 | } | |
131 | break; | |
132 | } | |
133 | if (!num) break; | |
134 | ||
135 | if (bank && (base == (1 + dram_ranges[bank - 1].last_page))) | |
136 | bank--; | |
137 | else | |
0c530ab8 A |
138 | { |
139 | num_banks++; | |
b0d623f7 A |
140 | if (num_banks >= MAX_BANKS) break; |
141 | bcopy(&dram_ranges[bank], | |
142 | &dram_ranges[bank + 1], | |
143 | (num_banks - bank - 1) * sizeof(hibernate_bitmap_t)); | |
144 | dram_ranges[bank].first_page = base; | |
0c530ab8 | 145 | } |
b0d623f7 | 146 | dram_ranges[bank].last_page = base + num - 1; |
0c530ab8 A |
147 | break; |
148 | ||
149 | // runtime services will be restarted, so no save | |
150 | case kEfiRuntimeServicesCode: | |
151 | case kEfiRuntimeServicesData: | |
d1ecb069 A |
152 | // contents are volatile once the platform expert starts |
153 | case kEfiACPIReclaimMemory: | |
0c530ab8 A |
154 | // non dram |
155 | case kEfiReservedMemoryType: | |
156 | case kEfiUnusableMemory: | |
157 | case kEfiMemoryMappedIO: | |
158 | case kEfiMemoryMappedIOPortSpace: | |
159 | default: | |
160 | break; | |
161 | } | |
6601e61a | 162 | } |
4452a7af | 163 | |
0c530ab8 A |
164 | if (num_banks >= MAX_BANKS) |
165 | return (NULL); | |
166 | ||
167 | // size the hibernation bitmap | |
168 | ||
3a60a9f5 A |
169 | size = sizeof(hibernate_page_list_t); |
170 | page_count = 0; | |
0c530ab8 A |
171 | for (bank = 0; bank < num_banks; bank++) { |
172 | pages = dram_ranges[bank].last_page + 1 - dram_ranges[bank].first_page; | |
3a60a9f5 A |
173 | page_count += pages; |
174 | size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t); | |
175 | } | |
176 | ||
177 | list = (hibernate_page_list_t *)kalloc(size); | |
178 | if (!list) | |
179 | return (list); | |
180 | ||
b0d623f7 | 181 | list->list_size = (uint32_t)size; |
3a60a9f5 | 182 | list->page_count = page_count; |
0c530ab8 A |
183 | list->bank_count = num_banks; |
184 | ||
185 | // convert to hibernation bitmap. | |
3a60a9f5 | 186 | |
3a60a9f5 | 187 | bitmap = &list->bank_bitmap[0]; |
0c530ab8 A |
188 | for (bank = 0; bank < num_banks; bank++) |
189 | { | |
190 | bitmap->first_page = dram_ranges[bank].first_page; | |
191 | bitmap->last_page = dram_ranges[bank].last_page; | |
3a60a9f5 A |
192 | bitmap->bitmapwords = (bitmap->last_page + 1 |
193 | - bitmap->first_page + 31) >> 5; | |
db609669 A |
194 | if (log) kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", |
195 | bank, bitmap->first_page, bitmap->last_page); | |
3a60a9f5 A |
196 | bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords]; |
197 | } | |
39236c6e | 198 | if (log) printf("efi pagecount %d\n", non_os_pagecount); |
3a60a9f5 | 199 | |
3a60a9f5 A |
200 | return (list); |
201 | } | |
202 | ||
0c530ab8 A |
203 | // mark pages not to be saved, but available for scratch usage during restore |
204 | ||
205 | void | |
206 | hibernate_page_list_setall_machine( __unused hibernate_page_list_t * page_list, | |
207 | __unused hibernate_page_list_t * page_list_wired, | |
db609669 | 208 | __unused boolean_t preflight, |
0c530ab8 A |
209 | __unused uint32_t * pagesOut) |
210 | { | |
211 | } | |
212 | ||
213 | // mark pages not to be saved and not for scratch usage during restore | |
5d5c5d0d | 214 | void |
0c530ab8 A |
215 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list, |
216 | hibernate_page_list_t * page_list_wired, | |
217 | uint32_t * pagesOut) | |
5d5c5d0d | 218 | { |
0c530ab8 A |
219 | boot_args * args = (boot_args *) PE_state.bootArgs; |
220 | ||
0c530ab8 A |
221 | if (args->efiRuntimeServicesPageStart) |
222 | { | |
223 | hibernate_set_page_state(page_list, page_list_wired, | |
224 | args->efiRuntimeServicesPageStart, args->efiRuntimeServicesPageCount, | |
225 | kIOHibernatePageStateFree); | |
226 | *pagesOut -= args->efiRuntimeServicesPageCount; | |
3a60a9f5 A |
227 | } |
228 | } | |
229 | ||
230 | kern_return_t | |
231 | hibernate_processor_setup(IOHibernateImageHeader * header) | |
232 | { | |
0c530ab8 A |
233 | boot_args * args = (boot_args *) PE_state.bootArgs; |
234 | ||
235 | cpu_datap(0)->cpu_hibernate = 1; | |
3a60a9f5 | 236 | header->processorFlags = 0; |
0c530ab8 A |
237 | |
238 | header->runtimePages = args->efiRuntimeServicesPageStart; | |
239 | header->runtimePageCount = args->efiRuntimeServicesPageCount; | |
d1ecb069 | 240 | header->runtimeVirtualPages = args->efiRuntimeServicesVirtualPageStart; |
6d2010ae A |
241 | header->performanceDataStart = args->performanceDataStart; |
242 | header->performanceDataSize = args->performanceDataSize; | |
0c530ab8 | 243 | |
3a60a9f5 A |
244 | return (KERN_SUCCESS); |
245 | } | |
246 | ||
5ba3f43e A |
247 | static boolean_t hibernate_vm_locks_safe; |
248 | ||
3a60a9f5 A |
249 | void |
250 | hibernate_vm_lock(void) | |
251 | { | |
5ba3f43e A |
252 | if (current_cpu_datap()->cpu_hibernate) { |
253 | hibernate_vm_lock_queues(); | |
254 | hibernate_vm_locks_safe = TRUE; | |
255 | } | |
3a60a9f5 A |
256 | } |
257 | ||
258 | void | |
259 | hibernate_vm_unlock(void) | |
260 | { | |
5ba3f43e | 261 | assert(FALSE == ml_get_interrupts_enabled()); |
db609669 | 262 | if (current_cpu_datap()->cpu_hibernate) hibernate_vm_unlock_queues(); |
5ba3f43e A |
263 | ml_set_is_quiescing(TRUE); |
264 | } | |
265 | ||
266 | // ACPI calls hibernate_vm_lock(), interrupt disable, hibernate_vm_unlock() on sleep, | |
267 | // hibernate_vm_lock_end() and interrupt enable on wake. | |
268 | // VM locks are safely single threaded between hibernate_vm_lock() and hibernate_vm_lock_end(). | |
269 | ||
270 | void | |
271 | hibernate_vm_lock_end(void) | |
272 | { | |
273 | assert(FALSE == ml_get_interrupts_enabled()); | |
274 | hibernate_vm_locks_safe = FALSE; | |
275 | ml_set_is_quiescing(FALSE); | |
276 | } | |
277 | ||
278 | boolean_t | |
279 | hibernate_vm_locks_are_safe(void) | |
280 | { | |
281 | assert(FALSE == ml_get_interrupts_enabled()); | |
282 | return (hibernate_vm_locks_safe); | |
3a60a9f5 | 283 | } |