]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hibernate_i386.c
03cf1565ee7b7975f160d105a8087d308fbc2105
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
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>
44 #include <IOKit/IOHibernatePrivate.h>
45 #include <vm/vm_page.h>
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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
54 extern void *sectTEXTB
;
55 extern char *first_avail
;
57 hibernate_page_list_t
*
58 hibernate_page_list_allocate(void)
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
;
72 /* Make a list of the maximum number of regions needed */
73 num_alloc_regions
= 1 + pmap_memory_region_count
;
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
);
80 /* Fill in the actual regions we will be returning. */
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;
91 /* Remaining memory regions. Consolidate adjacent regions. */
92 for (bank
= 0; bank
< (uint32_t) pmap_memory_region_count
; bank
++)
94 if ((rp
->end
+ 1) == pmap_memory_regions
[bank
].base
) {
95 rp
->end
= pmap_memory_regions
[bank
].end
;
99 rp
->base
= pmap_memory_regions
[bank
].base
;
100 rp
->end
= pmap_memory_regions
[bank
].end
;
105 /* Size the hibernation bitmap */
106 size
= sizeof(hibernate_page_list_t
);
108 for (bank
= 0, rp
= regions
; bank
< num_regions
; bank
++, rp
++) {
109 pages
= rp
->end
+ 1 - rp
->base
;
111 size
+= sizeof(hibernate_bitmap_t
) + ((pages
+ 31) >> 5) * sizeof(uint32_t);
114 list
= (hibernate_page_list_t
*)kalloc(size
);
118 list
->list_size
= size
;
119 list
->page_count
= page_count
;
120 list
->bank_count
= num_regions
;
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
];
136 kfree((void *)regions
, sizeof(pmap_memory_region_t
) * num_alloc_regions
);
141 hibernate_page_list_setall_machine(hibernate_page_list_t
* page_list
,
142 hibernate_page_list_t
* page_list_wired
,
145 KernelBootArgs_t
* bootArgs
= (KernelBootArgs_t
*)PE_state
.bootArgs
;
148 uint32_t page
, count
;
150 for (bank
= 0, mptr
= bootArgs
->memoryMap
; bank
< bootArgs
->memoryMapCount
; bank
++, mptr
++) {
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
);
159 hibernate_set_page_state(page_list
, page_list_wired
, page
, count
, 1);
165 hibernate_processor_setup(IOHibernateImageHeader
* header
)
167 current_cpu_datap()->cpu_hibernate
= 1;
168 header
->processorFlags
= 0;
169 return (KERN_SUCCESS
);
173 hibernate_vm_lock(void)
175 if (FALSE
/* getPerProc()->hibernate */)
177 vm_page_lock_queues();
178 mutex_lock(&vm_page_queue_free_lock
);
183 hibernate_vm_unlock(void)
185 if (FALSE
/* getPerProc()->hibernate */)
187 mutex_unlock(&vm_page_queue_free_lock
);
188 vm_page_unlock_queues();