]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hibernate_i386.c
xnu-792.6.22.tar.gz
[apple/xnu.git] / osfmk / i386 / hibernate_i386.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <kern/machine.h>
24 #include <kern/misc_protos.h>
25 #include <kern/thread.h>
26 #include <kern/processor.h>
27 #include <kern/kalloc.h>
28 #include <mach/machine.h>
29 #include <mach/processor_info.h>
30 #include <mach/mach_types.h>
31 #include <i386/pmap.h>
32 #include <kern/cpu_data.h>
33 #include <IOKit/IOPlatformExpert.h>
34 #define KERNEL
35
36 #include <IOKit/IOHibernatePrivate.h>
37 #include <vm/vm_page.h>
38
39 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
40
41 /* This assumes that
42 * - we never will want to read or write memory below the start of kernel text
43 * - kernel text and data isn't included in pmap memory regions
44 */
45
46 extern void *sectTEXTB;
47 extern char *first_avail;
48
49 hibernate_page_list_t *
50 hibernate_page_list_allocate(void)
51 {
52 vm_offset_t base;
53 vm_size_t size;
54 uint32_t bank;
55 uint32_t pages, page_count;
56 hibernate_page_list_t * list;
57 hibernate_bitmap_t * bitmap;
58 pmap_memory_region_t * regions;
59 pmap_memory_region_t * rp;
60 uint32_t num_regions, num_alloc_regions;
61
62 page_count = 0;
63
64 /* Make a list of the maximum number of regions needed */
65 num_alloc_regions = 1 + pmap_memory_region_count;
66
67 /* Allocate our own list of memory regions so we can sort them in order. */
68 regions = (pmap_memory_region_t *)kalloc(sizeof(pmap_memory_region_t) * num_alloc_regions);
69 if (!regions)
70 return (0);
71
72 /* Fill in the actual regions we will be returning. */
73 rp = regions;
74
75 /* XXX should check for non-volatile memory region below kernel space. */
76 /* Kernel region is first. */
77 base = (vm_offset_t)(sectTEXTB) & 0x3FFFFFFF;
78 rp->base = atop_32(base);
79 rp->end = atop_32((vm_offset_t)first_avail) - 1;
80 rp->alloc = 0;
81 num_regions = 1;
82
83 /* Remaining memory regions. Consolidate adjacent regions. */
84 for (bank = 0; bank < (uint32_t) pmap_memory_region_count; bank++)
85 {
86 if ((rp->end + 1) == pmap_memory_regions[bank].base) {
87 rp->end = pmap_memory_regions[bank].end;
88 } else {
89 ++rp;
90 ++num_regions;
91 rp->base = pmap_memory_regions[bank].base;
92 rp->end = pmap_memory_regions[bank].end;
93 rp->alloc = 0;
94 }
95 }
96
97 /* Size the hibernation bitmap */
98 size = sizeof(hibernate_page_list_t);
99 page_count = 0;
100 for (bank = 0, rp = regions; bank < num_regions; bank++, rp++) {
101 pages = rp->end + 1 - rp->base;
102 page_count += pages;
103 size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t);
104 }
105
106 list = (hibernate_page_list_t *)kalloc(size);
107 if (!list)
108 return (list);
109
110 list->list_size = size;
111 list->page_count = page_count;
112 list->bank_count = num_regions;
113
114 /* Convert to hibernation bitmap. */
115 /* This assumes that ranges are in order and do not overlap. */
116 bitmap = &list->bank_bitmap[0];
117 for (bank = 0, rp = regions; bank < num_regions; bank++, rp++) {
118 bitmap->first_page = rp->base;
119 bitmap->last_page = rp->end;
120 bitmap->bitmapwords = (bitmap->last_page + 1
121 - bitmap->first_page + 31) >> 5;
122 kprintf("HIB: Bank %d: 0x%x end 0x%x\n", bank,
123 ptoa_32(bitmap->first_page),
124 ptoa_32(bitmap->last_page));
125 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
126 }
127
128 kfree((void *)regions, sizeof(pmap_memory_region_t) * num_alloc_regions);
129 return (list);
130 }
131
132 void
133 hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
134 hibernate_page_list_t * page_list_wired,
135 uint32_t * pagesOut)
136 {
137 KernelBootArgs_t * bootArgs = (KernelBootArgs_t *)PE_state.bootArgs;
138 MemoryRange * mptr;
139 uint32_t bank;
140 uint32_t page, count;
141
142 for (bank = 0, mptr = bootArgs->memoryMap; bank < bootArgs->memoryMapCount; bank++, mptr++) {
143
144 if (kMemoryRangeNVS != mptr->type) continue;
145 kprintf("Base NVS region 0x%x + 0x%x\n", (vm_offset_t)mptr->base, (vm_size_t)mptr->length);
146 /* Round to page size. Hopefully this does not overlap any reserved areas. */
147 page = atop_32(trunc_page((vm_offset_t)mptr->base));
148 count = atop_32(round_page((vm_offset_t)mptr->base + (vm_size_t)mptr->length)) - page;
149 kprintf("Rounded NVS region 0x%x size 0x%x\n", page, count);
150
151 hibernate_set_page_state(page_list, page_list_wired, page, count, 1);
152 pagesOut -= count;
153 }
154 }
155
156 kern_return_t
157 hibernate_processor_setup(IOHibernateImageHeader * header)
158 {
159 current_cpu_datap()->cpu_hibernate = 1;
160 header->processorFlags = 0;
161 return (KERN_SUCCESS);
162 }
163
164 void
165 hibernate_vm_lock(void)
166 {
167 if (FALSE /* getPerProc()->hibernate */)
168 {
169 vm_page_lock_queues();
170 mutex_lock(&vm_page_queue_free_lock);
171 }
172 }
173
174 void
175 hibernate_vm_unlock(void)
176 {
177 if (FALSE /* getPerProc()->hibernate */)
178 {
179 mutex_unlock(&vm_page_queue_free_lock);
180 vm_page_unlock_queues();
181 }
182 }