xref: /xnu-11215.41.3/osfmk/kern/hibernate.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_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 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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <kern/kalloc.h>
30 #include <kern/machine.h>
31 #include <kern/misc_protos.h>
32 #include <kern/thread.h>
33 #include <kern/processor.h>
34 #include <mach/machine.h>
35 #include <mach/processor_info.h>
36 #include <mach/mach_types.h>
37 #include <IOKit/IOPlatformExpert.h>
38 
39 #include <IOKit/IOHibernatePrivate.h>
40 #include <vm/vm_page.h>
41 #include <vm/vm_pageout_xnu.h>
42 #include <vm/vm_purgeable_internal.h>
43 #include <vm/vm_compressor_xnu.h>
44 #include <kern/ecc.h>
45 
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 
48 boolean_t       need_to_unlock_decompressor = FALSE;
49 
50 kern_return_t
hibernate_alloc_page_lists(hibernate_page_list_t ** page_list_ret,hibernate_page_list_t ** page_list_wired_ret,hibernate_page_list_t ** page_list_pal_ret)51 hibernate_alloc_page_lists(
52 	hibernate_page_list_t ** page_list_ret,
53 	hibernate_page_list_t ** page_list_wired_ret,
54 	hibernate_page_list_t ** page_list_pal_ret)
55 {
56 	kern_return_t       retval = KERN_SUCCESS;
57 
58 	hibernate_page_list_t * page_list = NULL;
59 	hibernate_page_list_t * page_list_wired = NULL;
60 	hibernate_page_list_t * page_list_pal = NULL;
61 
62 	page_list = hibernate_page_list_allocate(TRUE);
63 	if (!page_list) {
64 		HIBLOG("%s: failed for page_list\n", __FUNCTION__);
65 		retval = KERN_RESOURCE_SHORTAGE;
66 		goto done;
67 	}
68 	page_list_wired = hibernate_page_list_allocate(FALSE);
69 	if (!page_list_wired) {
70 		kfree_data(page_list, page_list->list_size);
71 		HIBLOG("%s: failed for page_list_wired\n", __FUNCTION__);
72 		retval = KERN_RESOURCE_SHORTAGE;
73 		goto done;
74 	}
75 	page_list_pal = hibernate_page_list_allocate(FALSE);
76 	if (!page_list_pal) {
77 		kfree_data(page_list, page_list->list_size);
78 		kfree_data(page_list_wired, page_list_wired->list_size);
79 		HIBLOG("%s: failed for page_list_pal\n", __FUNCTION__);
80 		retval = KERN_RESOURCE_SHORTAGE;
81 		goto done;
82 	}
83 	*page_list_ret        = page_list;
84 	*page_list_wired_ret  = page_list_wired;
85 	*page_list_pal_ret    = page_list_pal;
86 
87 done:
88 	return retval;
89 }
90 
91 extern int sync_internal(void);
92 
93 kern_return_t
hibernate_setup(IOHibernateImageHeader * header,boolean_t vmflush,hibernate_page_list_t * page_list __unused,hibernate_page_list_t * page_list_wired __unused,hibernate_page_list_t * page_list_pal __unused)94 hibernate_setup(IOHibernateImageHeader * header,
95     boolean_t vmflush,
96     hibernate_page_list_t * page_list __unused,
97     hibernate_page_list_t * page_list_wired __unused,
98     hibernate_page_list_t * page_list_pal __unused)
99 {
100 	kern_return_t       retval = KERN_SUCCESS;
101 
102 	hibernate_create_paddr_map();
103 
104 	hibernate_reset_stats();
105 
106 	if (vmflush && VM_CONFIG_COMPRESSOR_IS_PRESENT) {
107 		sync_internal();
108 
109 		vm_decompressor_lock();
110 		need_to_unlock_decompressor = TRUE;
111 
112 		hibernate_flush_memory();
113 	}
114 
115 	// no failures hereafter
116 
117 	hibernate_processor_setup(header);
118 
119 	HIBLOG("hibernate_alloc_pages act %d, inact %d, anon %d, throt %d, spec %d, wire %d, wireinit %d\n",
120 	    vm_page_active_count, vm_page_inactive_count,
121 	    vm_page_anonymous_count, vm_page_throttled_count, vm_page_speculative_count,
122 	    vm_page_wire_count, vm_page_wire_count_initial);
123 
124 	if (retval != KERN_SUCCESS && need_to_unlock_decompressor == TRUE) {
125 		need_to_unlock_decompressor = FALSE;
126 		vm_decompressor_unlock();
127 	}
128 	return retval;
129 }
130 
131 kern_return_t
hibernate_teardown(hibernate_page_list_t * page_list,hibernate_page_list_t * page_list_wired,hibernate_page_list_t * page_list_pal)132 hibernate_teardown(hibernate_page_list_t * page_list,
133     hibernate_page_list_t * page_list_wired,
134     hibernate_page_list_t * page_list_pal)
135 {
136 	hibernate_free_gobble_pages();
137 
138 	if (page_list) {
139 		kfree_data(page_list, page_list->list_size);
140 	}
141 	if (page_list_wired) {
142 		kfree_data(page_list_wired, page_list_wired->list_size);
143 	}
144 	if (page_list_pal) {
145 		kfree_data(page_list_pal, page_list_pal->list_size);
146 	}
147 
148 	if (VM_CONFIG_COMPRESSOR_IS_PRESENT) {
149 		if (need_to_unlock_decompressor == TRUE) {
150 			need_to_unlock_decompressor = FALSE;
151 			vm_decompressor_unlock();
152 		}
153 		vm_compressor_delay_trim();
154 	}
155 
156 
157 	return KERN_SUCCESS;
158 }
159