]>
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@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 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 | ||
0a7de745 | 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 | { |
0a7de745 A |
57 | ppnum_t base, num; |
58 | vm_size_t size; | |
59 | uint32_t bank, num_banks; | |
60 | uint32_t pages, page_count; | |
61 | hibernate_page_list_t * list; | |
62 | hibernate_bitmap_t * bitmap; | |
63 | ||
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; | |
68 | uint32_t non_os_pagecount; | |
69 | ppnum_t pnmax = max_ppnum; | |
70 | ||
71 | mptr = (EfiMemoryRange *)ml_static_ptovirt(args->MemoryMap); | |
72 | if (args->MemoryMapDescriptorSize == 0) { | |
73 | panic("Invalid memory map descriptor size"); | |
74 | } | |
75 | msize = args->MemoryMapDescriptorSize; | |
76 | mcount = args->MemoryMapSize / msize; | |
4452a7af | 77 | |
5ba3f43e | 78 | #if KASAN |
0a7de745 A |
79 | /* adjust max page number to include stolen memory */ |
80 | if (atop(shadow_ptop) > pnmax) { | |
81 | pnmax = (ppnum_t)atop(shadow_ptop); | |
82 | } | |
5ba3f43e A |
83 | #endif |
84 | ||
0a7de745 A |
85 | num_banks = 0; |
86 | non_os_pagecount = 0; | |
87 | for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) { | |
88 | base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT); | |
89 | num = (ppnum_t) mptr->NumberOfPages; | |
0b4c1975 | 90 | |
5ba3f43e | 91 | #if KASAN |
0a7de745 A |
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 | } | |
5ba3f43e A |
99 | #endif |
100 | ||
0a7de745 | 101 | if (base > pnmax) { |
b0d623f7 | 102 | continue; |
b0d623f7 | 103 | } |
0a7de745 A |
104 | if ((base + num - 1) > pnmax) { |
105 | num = pnmax - base + 1; | |
106 | } | |
107 | if (!num) { | |
108 | continue; | |
0c530ab8 | 109 | } |
0a7de745 A |
110 | |
111 | switch (mptr->Type) { | |
112 | // any kind of dram | |
113 | case kEfiACPIMemoryNVS: | |
114 | case kEfiPalCode: | |
115 | non_os_pagecount += num; | |
116 | ||
117 | // OS used dram | |
118 | case kEfiLoaderCode: | |
119 | case kEfiLoaderData: | |
120 | case kEfiBootServicesCode: | |
121 | case kEfiBootServicesData: | |
122 | case kEfiConventionalMemory: | |
123 | ||
124 | for (bank = 0; bank < num_banks; bank++) { | |
125 | if (dram_ranges[bank].first_page <= base) { | |
126 | continue; | |
127 | } | |
128 | if ((base + num) == dram_ranges[bank].first_page) { | |
129 | dram_ranges[bank].first_page = base; | |
130 | num = 0; | |
131 | } | |
132 | break; | |
133 | } | |
134 | if (!num) { | |
135 | break; | |
136 | } | |
137 | ||
138 | if (bank && (base == (1 + dram_ranges[bank - 1].last_page))) { | |
139 | bank--; | |
140 | } else { | |
141 | num_banks++; | |
142 | if (num_banks >= MAX_BANKS) { | |
143 | break; | |
144 | } | |
145 | bcopy(&dram_ranges[bank], | |
146 | &dram_ranges[bank + 1], | |
147 | (num_banks - bank - 1) * sizeof(hibernate_bitmap_t)); | |
148 | dram_ranges[bank].first_page = base; | |
149 | } | |
150 | dram_ranges[bank].last_page = base + num - 1; | |
151 | break; | |
152 | ||
153 | // runtime services will be restarted, so no save | |
154 | case kEfiRuntimeServicesCode: | |
155 | case kEfiRuntimeServicesData: | |
156 | // contents are volatile once the platform expert starts | |
157 | case kEfiACPIReclaimMemory: | |
158 | // non dram | |
159 | case kEfiReservedMemoryType: | |
160 | case kEfiUnusableMemory: | |
161 | case kEfiMemoryMappedIO: | |
162 | case kEfiMemoryMappedIOPortSpace: | |
163 | default: | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | if (num_banks >= MAX_BANKS) { | |
cb323159 | 169 | HIBLOG("%s error, num_banks exceed MAX_BANKS(0x%x)\n", __FUNCTION__, MAX_BANKS); |
0a7de745 | 170 | return NULL; |
0c530ab8 | 171 | } |
0a7de745 A |
172 | |
173 | // size the hibernation bitmap | |
174 | ||
175 | size = sizeof(hibernate_page_list_t); | |
176 | page_count = 0; | |
177 | for (bank = 0; bank < num_banks; bank++) { | |
178 | pages = dram_ranges[bank].last_page + 1 - dram_ranges[bank].first_page; | |
179 | page_count += pages; | |
180 | size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t); | |
181 | } | |
182 | ||
183 | list = (hibernate_page_list_t *)kalloc(size); | |
184 | if (!list) { | |
185 | return list; | |
186 | } | |
187 | ||
188 | list->list_size = (uint32_t)size; | |
189 | list->page_count = page_count; | |
190 | list->bank_count = num_banks; | |
191 | ||
192 | // convert to hibernation bitmap. | |
193 | ||
194 | bitmap = &list->bank_bitmap[0]; | |
195 | for (bank = 0; bank < num_banks; bank++) { | |
196 | bitmap->first_page = dram_ranges[bank].first_page; | |
197 | bitmap->last_page = dram_ranges[bank].last_page; | |
198 | bitmap->bitmapwords = (bitmap->last_page + 1 | |
199 | - bitmap->first_page + 31) >> 5; | |
200 | if (log) { | |
201 | kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", | |
202 | bank, bitmap->first_page, bitmap->last_page); | |
203 | } | |
204 | bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords]; | |
205 | } | |
206 | if (log) { | |
207 | printf("efi pagecount %d\n", non_os_pagecount); | |
208 | } | |
209 | ||
210 | return list; | |
3a60a9f5 A |
211 | } |
212 | ||
0c530ab8 A |
213 | // mark pages not to be saved, but available for scratch usage during restore |
214 | ||
215 | void | |
216 | hibernate_page_list_setall_machine( __unused hibernate_page_list_t * page_list, | |
0a7de745 A |
217 | __unused hibernate_page_list_t * page_list_wired, |
218 | __unused boolean_t preflight, | |
219 | __unused uint32_t * pagesOut) | |
0c530ab8 A |
220 | { |
221 | } | |
222 | ||
223 | // mark pages not to be saved and not for scratch usage during restore | |
5d5c5d0d | 224 | void |
0c530ab8 | 225 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list, |
0a7de745 A |
226 | hibernate_page_list_t * page_list_wired, |
227 | uint32_t * pagesOut) | |
5d5c5d0d | 228 | { |
0a7de745 | 229 | boot_args * args = (boot_args *) PE_state.bootArgs; |
0c530ab8 | 230 | |
0a7de745 A |
231 | if (args->efiRuntimeServicesPageStart) { |
232 | hibernate_set_page_state(page_list, page_list_wired, | |
233 | args->efiRuntimeServicesPageStart, args->efiRuntimeServicesPageCount, | |
0c530ab8 | 234 | kIOHibernatePageStateFree); |
0a7de745 A |
235 | *pagesOut -= args->efiRuntimeServicesPageCount; |
236 | } | |
3a60a9f5 A |
237 | } |
238 | ||
0a7de745 | 239 | kern_return_t |
3a60a9f5 A |
240 | hibernate_processor_setup(IOHibernateImageHeader * header) |
241 | { | |
0a7de745 | 242 | boot_args * args = (boot_args *) PE_state.bootArgs; |
0c530ab8 | 243 | |
0a7de745 A |
244 | cpu_datap(0)->cpu_hibernate = 1; |
245 | header->processorFlags = 0; | |
0c530ab8 | 246 | |
0a7de745 A |
247 | header->runtimePages = args->efiRuntimeServicesPageStart; |
248 | header->runtimePageCount = args->efiRuntimeServicesPageCount; | |
249 | header->runtimeVirtualPages = args->efiRuntimeServicesVirtualPageStart; | |
250 | header->performanceDataStart = args->performanceDataStart; | |
251 | header->performanceDataSize = args->performanceDataSize; | |
0c530ab8 | 252 | |
0a7de745 | 253 | return KERN_SUCCESS; |
3a60a9f5 A |
254 | } |
255 | ||
5ba3f43e A |
256 | static boolean_t hibernate_vm_locks_safe; |
257 | ||
3a60a9f5 A |
258 | void |
259 | hibernate_vm_lock(void) | |
260 | { | |
0a7de745 A |
261 | if (current_cpu_datap()->cpu_hibernate) { |
262 | hibernate_vm_lock_queues(); | |
263 | hibernate_vm_locks_safe = TRUE; | |
264 | } | |
3a60a9f5 A |
265 | } |
266 | ||
267 | void | |
268 | hibernate_vm_unlock(void) | |
269 | { | |
0a7de745 A |
270 | assert(FALSE == ml_get_interrupts_enabled()); |
271 | if (current_cpu_datap()->cpu_hibernate) { | |
272 | hibernate_vm_unlock_queues(); | |
273 | } | |
274 | ml_set_is_quiescing(TRUE); | |
5ba3f43e A |
275 | } |
276 | ||
277 | // ACPI calls hibernate_vm_lock(), interrupt disable, hibernate_vm_unlock() on sleep, | |
278 | // hibernate_vm_lock_end() and interrupt enable on wake. | |
279 | // VM locks are safely single threaded between hibernate_vm_lock() and hibernate_vm_lock_end(). | |
280 | ||
281 | void | |
282 | hibernate_vm_lock_end(void) | |
283 | { | |
0a7de745 A |
284 | assert(FALSE == ml_get_interrupts_enabled()); |
285 | hibernate_vm_locks_safe = FALSE; | |
286 | ml_set_is_quiescing(FALSE); | |
5ba3f43e A |
287 | } |
288 | ||
289 | boolean_t | |
290 | hibernate_vm_locks_are_safe(void) | |
291 | { | |
0a7de745 A |
292 | assert(FALSE == ml_get_interrupts_enabled()); |
293 | return hibernate_vm_locks_safe; | |
3a60a9f5 | 294 | } |