]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/hibernate.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / kern / hibernate.c
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 <default_pager/default_pager_internal.h>
38 #include <IOKit/IOPlatformExpert.h>
39
40 #include <IOKit/IOHibernatePrivate.h>
41 #include <vm/vm_page.h>
42 #include <vm/vm_pageout.h>
43 #include <vm/vm_purgeable_internal.h>
44
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
46
47 kern_return_t
48 hibernate_setup(IOHibernateImageHeader * header,
49 uint32_t free_page_ratio,
50 uint32_t free_page_time,
51 boolean_t vmflush,
52 hibernate_page_list_t ** page_list_ret,
53 hibernate_page_list_t ** page_list_wired_ret,
54 boolean_t * encryptedswap)
55 {
56 hibernate_page_list_t * page_list = NULL;
57 hibernate_page_list_t * page_list_wired = NULL;
58 uint32_t gobble_count;
59
60 *page_list_ret = NULL;
61 *page_list_wired_ret = NULL;
62
63 if (vmflush)
64 hibernate_flush_memory();
65
66 page_list = hibernate_page_list_allocate();
67 if (!page_list)
68 return (KERN_RESOURCE_SHORTAGE);
69 page_list_wired = hibernate_page_list_allocate();
70 if (!page_list_wired)
71 {
72 kfree(page_list, page_list->list_size);
73 return (KERN_RESOURCE_SHORTAGE);
74 }
75
76 *encryptedswap = dp_encryption;
77
78 // pages we could force out to reduce hibernate image size
79 gobble_count = (uint32_t)((((uint64_t) page_list->page_count) * ((uint64_t) free_page_ratio)) / 100);
80
81 // no failures hereafter
82
83 hibernate_processor_setup(header);
84
85 HIBLOG("hibernate_alloc_pages flags %08x, gobbling %d pages\n",
86 header->processorFlags, gobble_count);
87
88 if (gobble_count)
89 hibernate_gobble_pages(gobble_count, free_page_time);
90
91 *page_list_ret = page_list;
92 *page_list_wired_ret = page_list_wired;
93
94 return (KERN_SUCCESS);
95 }
96
97 kern_return_t
98 hibernate_teardown(hibernate_page_list_t * page_list,
99 hibernate_page_list_t * page_list_wired)
100 {
101 hibernate_free_gobble_pages();
102
103 if (page_list)
104 kfree(page_list, page_list->list_size);
105 if (page_list_wired)
106 kfree(page_list_wired, page_list_wired->list_size);
107
108 return (KERN_SUCCESS);
109 }
110