]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hibernate_i386.c
03cf1565ee7b7975f160d105a8087d308fbc2105
[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 #define KERNEL
43
44 #include <IOKit/IOHibernatePrivate.h>
45 #include <vm/vm_page.h>
46
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48
49 /* This assumes that
50 * - we never will want to read or write memory below the start of kernel text
51 * - kernel text and data isn't included in pmap memory regions
52 */
53
54 extern void *sectTEXTB;
55 extern char *first_avail;
56
57 hibernate_page_list_t *
58 hibernate_page_list_allocate(void)
59 {
60 vm_offset_t base;
61 vm_size_t size;
62 uint32_t bank;
63 uint32_t pages, page_count;
64 hibernate_page_list_t * list;
65 hibernate_bitmap_t * bitmap;
66 pmap_memory_region_t * regions;
67 pmap_memory_region_t * rp;
68 uint32_t num_regions, num_alloc_regions;
69
70 page_count = 0;
71
72 /* Make a list of the maximum number of regions needed */
73 num_alloc_regions = 1 + pmap_memory_region_count;
74
75 /* Allocate our own list of memory regions so we can sort them in order. */
76 regions = (pmap_memory_region_t *)kalloc(sizeof(pmap_memory_region_t) * num_alloc_regions);
77 if (!regions)
78 return (0);
79
80 /* Fill in the actual regions we will be returning. */
81 rp = regions;
82
83 /* XXX should check for non-volatile memory region below kernel space. */
84 /* Kernel region is first. */
85 base = (vm_offset_t)(sectTEXTB) & 0x3FFFFFFF;
86 rp->base = atop_32(base);
87 rp->end = atop_32((vm_offset_t)first_avail) - 1;
88 rp->alloc = 0;
89 num_regions = 1;
90
91 /* Remaining memory regions. Consolidate adjacent regions. */
92 for (bank = 0; bank < (uint32_t) pmap_memory_region_count; bank++)
93 {
94 if ((rp->end + 1) == pmap_memory_regions[bank].base) {
95 rp->end = pmap_memory_regions[bank].end;
96 } else {
97 ++rp;
98 ++num_regions;
99 rp->base = pmap_memory_regions[bank].base;
100 rp->end = pmap_memory_regions[bank].end;
101 rp->alloc = 0;
102 }
103 }
104
105 /* Size the hibernation bitmap */
106 size = sizeof(hibernate_page_list_t);
107 page_count = 0;
108 for (bank = 0, rp = regions; bank < num_regions; bank++, rp++) {
109 pages = rp->end + 1 - rp->base;
110 page_count += pages;
111 size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t);
112 }
113
114 list = (hibernate_page_list_t *)kalloc(size);
115 if (!list)
116 return (list);
117
118 list->list_size = size;
119 list->page_count = page_count;
120 list->bank_count = num_regions;
121
122 /* Convert to hibernation bitmap. */
123 /* This assumes that ranges are in order and do not overlap. */
124 bitmap = &list->bank_bitmap[0];
125 for (bank = 0, rp = regions; bank < num_regions; bank++, rp++) {
126 bitmap->first_page = rp->base;
127 bitmap->last_page = rp->end;
128 bitmap->bitmapwords = (bitmap->last_page + 1
129 - bitmap->first_page + 31) >> 5;
130 kprintf("HIB: Bank %d: 0x%x end 0x%x\n", bank,
131 ptoa_32(bitmap->first_page),
132 ptoa_32(bitmap->last_page));
133 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
134 }
135
136 kfree((void *)regions, sizeof(pmap_memory_region_t) * num_alloc_regions);
137 return (list);
138 }
139
140 void
141 hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
142 hibernate_page_list_t * page_list_wired,
143 uint32_t * pagesOut)
144 {
145 KernelBootArgs_t * bootArgs = (KernelBootArgs_t *)PE_state.bootArgs;
146 MemoryRange * mptr;
147 uint32_t bank;
148 uint32_t page, count;
149
150 for (bank = 0, mptr = bootArgs->memoryMap; bank < bootArgs->memoryMapCount; bank++, mptr++) {
151
152 if (kMemoryRangeNVS != mptr->type) continue;
153 kprintf("Base NVS region 0x%x + 0x%x\n", (vm_offset_t)mptr->base, (vm_size_t)mptr->length);
154 /* Round to page size. Hopefully this does not overlap any reserved areas. */
155 page = atop_32(trunc_page((vm_offset_t)mptr->base));
156 count = atop_32(round_page((vm_offset_t)mptr->base + (vm_size_t)mptr->length)) - page;
157 kprintf("Rounded NVS region 0x%x size 0x%x\n", page, count);
158
159 hibernate_set_page_state(page_list, page_list_wired, page, count, 1);
160 pagesOut -= count;
161 }
162 }
163
164 kern_return_t
165 hibernate_processor_setup(IOHibernateImageHeader * header)
166 {
167 current_cpu_datap()->cpu_hibernate = 1;
168 header->processorFlags = 0;
169 return (KERN_SUCCESS);
170 }
171
172 void
173 hibernate_vm_lock(void)
174 {
175 if (FALSE /* getPerProc()->hibernate */)
176 {
177 vm_page_lock_queues();
178 mutex_lock(&vm_page_queue_free_lock);
179 }
180 }
181
182 void
183 hibernate_vm_unlock(void)
184 {
185 if (FALSE /* getPerProc()->hibernate */)
186 {
187 mutex_unlock(&vm_page_queue_free_lock);
188 vm_page_unlock_queues();
189 }
190 }