]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hibernate_i386.c
f0507454ad9402a0a701875b7ee24bb76c324d25
[apple/xnu.git] / osfmk / i386 / hibernate_i386.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <kern/machine.h>
32 #include <kern/misc_protos.h>
33 #include <kern/thread.h>
34 #include <kern/processor.h>
35 #include <kern/kalloc.h>
36 #include <mach/machine.h>
37 #include <mach/processor_info.h>
38 #include <mach/mach_types.h>
39 #include <i386/pmap.h>
40 #include <kern/cpu_data.h>
41 #include <IOKit/IOPlatformExpert.h>
42
43 #include <pexpert/i386/efi.h>
44
45 #include <IOKit/IOHibernatePrivate.h>
46 #include <vm/vm_page.h>
47 #include "i386_lowmem.h"
48
49 #define MAX_BANKS 32
50
51 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
52
53 hibernate_page_list_t *
54 hibernate_page_list_allocate(void)
55 {
56 ppnum_t base, num;
57 vm_size_t size;
58 uint32_t bank, num_banks;
59 uint32_t pages, page_count;
60 hibernate_page_list_t * list;
61 hibernate_bitmap_t * bitmap;
62
63 EfiMemoryRange * mptr;
64 uint32_t mcount, msize, i;
65 hibernate_bitmap_t dram_ranges[MAX_BANKS];
66 boot_args * args = (boot_args *) PE_state.bootArgs;
67
68 mptr = args->MemoryMap;
69 if (args->MemoryMapDescriptorSize == 0)
70 panic("Invalid memory map descriptor size");
71 msize = args->MemoryMapDescriptorSize;
72 mcount = args->MemoryMapSize / msize;
73
74 num_banks = 0;
75 for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize))
76 {
77 base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT);
78 num = (ppnum_t) mptr->NumberOfPages;
79 if (!num)
80 continue;
81
82 switch (mptr->Type)
83 {
84 // any kind of dram
85 case kEfiLoaderCode:
86 case kEfiLoaderData:
87 case kEfiBootServicesCode:
88 case kEfiBootServicesData:
89 case kEfiConventionalMemory:
90 case kEfiACPIReclaimMemory:
91 case kEfiACPIMemoryNVS:
92 case kEfiPalCode:
93
94 if (!num_banks || (base != (1 + dram_ranges[num_banks - 1].last_page)))
95 {
96 num_banks++;
97 if (num_banks >= MAX_BANKS)
98 break;
99 dram_ranges[num_banks - 1].first_page = base;
100 }
101 dram_ranges[num_banks - 1].last_page = base + num - 1;
102 break;
103
104 // runtime services will be restarted, so no save
105 case kEfiRuntimeServicesCode:
106 case kEfiRuntimeServicesData:
107 // non dram
108 case kEfiReservedMemoryType:
109 case kEfiUnusableMemory:
110 case kEfiMemoryMappedIO:
111 case kEfiMemoryMappedIOPortSpace:
112 default:
113 break;
114 }
115 }
116
117 if (num_banks >= MAX_BANKS)
118 return (NULL);
119
120 // size the hibernation bitmap
121
122 size = sizeof(hibernate_page_list_t);
123 page_count = 0;
124 for (bank = 0; bank < num_banks; bank++) {
125 pages = dram_ranges[bank].last_page + 1 - dram_ranges[bank].first_page;
126 page_count += pages;
127 size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t);
128 }
129
130 list = (hibernate_page_list_t *)kalloc(size);
131 if (!list)
132 return (list);
133
134 list->list_size = size;
135 list->page_count = page_count;
136 list->bank_count = num_banks;
137
138 // convert to hibernation bitmap.
139
140 bitmap = &list->bank_bitmap[0];
141 for (bank = 0; bank < num_banks; bank++)
142 {
143 bitmap->first_page = dram_ranges[bank].first_page;
144 bitmap->last_page = dram_ranges[bank].last_page;
145 bitmap->bitmapwords = (bitmap->last_page + 1
146 - bitmap->first_page + 31) >> 5;
147 kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank,
148 bitmap->first_page,
149 bitmap->last_page);
150 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
151 }
152
153 return (list);
154 }
155
156 // mark pages not to be saved, but available for scratch usage during restore
157
158 void
159 hibernate_page_list_setall_machine( __unused hibernate_page_list_t * page_list,
160 __unused hibernate_page_list_t * page_list_wired,
161 __unused uint32_t * pagesOut)
162 {
163 }
164
165 // mark pages not to be saved and not for scratch usage during restore
166 void
167 hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,
168 hibernate_page_list_t * page_list_wired,
169 uint32_t * pagesOut)
170 {
171 boot_args * args = (boot_args *) PE_state.bootArgs;
172
173 hibernate_set_page_state(page_list, page_list_wired,
174 I386_HIB_PAGETABLE, I386_HIB_PAGETABLE_COUNT,
175 kIOHibernatePageStateFree);
176 *pagesOut -= I386_HIB_PAGETABLE_COUNT;
177
178 if (args->efiRuntimeServicesPageStart)
179 {
180 hibernate_set_page_state(page_list, page_list_wired,
181 args->efiRuntimeServicesPageStart, args->efiRuntimeServicesPageCount,
182 kIOHibernatePageStateFree);
183 *pagesOut -= args->efiRuntimeServicesPageCount;
184 }
185 }
186
187 kern_return_t
188 hibernate_processor_setup(IOHibernateImageHeader * header)
189 {
190 boot_args * args = (boot_args *) PE_state.bootArgs;
191
192 cpu_datap(0)->cpu_hibernate = 1;
193 header->processorFlags = 0;
194
195 header->runtimePages = args->efiRuntimeServicesPageStart;
196 header->runtimePageCount = args->efiRuntimeServicesPageCount;
197
198 return (KERN_SUCCESS);
199 }
200
201 void
202 hibernate_vm_lock(void)
203 {
204 if (current_cpu_datap()->cpu_hibernate)
205 {
206 vm_page_lock_queues();
207 mutex_lock(&vm_page_queue_free_lock);
208 }
209 }
210
211 void
212 hibernate_vm_unlock(void)
213 {
214 if (current_cpu_datap()->cpu_hibernate)
215 {
216 mutex_unlock(&vm_page_queue_free_lock);
217 vm_page_unlock_queues();
218 }
219 }