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>
43 #include <pexpert/i386/efi.h>
45 #include <IOKit/IOHibernatePrivate.h>
46 #include <vm/vm_page.h>
47 #include "i386_lowmem.h"
51 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53 hibernate_page_list_t
*
54 hibernate_page_list_allocate(void)
58 uint32_t bank
, num_banks
;
59 uint32_t pages
, page_count
;
60 hibernate_page_list_t
* list
;
61 hibernate_bitmap_t
* bitmap
;
63 EfiMemoryRange
* mptr
;
64 uint32_t mcount
, msize
, i
;
65 hibernate_bitmap_t dram_ranges
[MAX_BANKS
];
66 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
68 mptr
= args
->MemoryMap
;
69 if (args
->MemoryMapDescriptorSize
== 0)
70 panic("Invalid memory map descriptor size");
71 msize
= args
->MemoryMapDescriptorSize
;
72 mcount
= args
->MemoryMapSize
/ msize
;
75 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
))
77 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
78 num
= (ppnum_t
) mptr
->NumberOfPages
;
87 case kEfiBootServicesCode
:
88 case kEfiBootServicesData
:
89 case kEfiConventionalMemory
:
90 case kEfiACPIReclaimMemory
:
91 case kEfiACPIMemoryNVS
:
94 if (!num_banks
|| (base
!= (1 + dram_ranges
[num_banks
- 1].last_page
)))
97 if (num_banks
>= MAX_BANKS
)
99 dram_ranges
[num_banks
- 1].first_page
= base
;
101 dram_ranges
[num_banks
- 1].last_page
= base
+ num
- 1;
104 // runtime services will be restarted, so no save
105 case kEfiRuntimeServicesCode
:
106 case kEfiRuntimeServicesData
:
108 case kEfiReservedMemoryType
:
109 case kEfiUnusableMemory
:
110 case kEfiMemoryMappedIO
:
111 case kEfiMemoryMappedIOPortSpace
:
117 if (num_banks
>= MAX_BANKS
)
120 // size the hibernation bitmap
122 size
= sizeof(hibernate_page_list_t
);
124 for (bank
= 0; bank
< num_banks
; bank
++) {
125 pages
= dram_ranges
[bank
].last_page
+ 1 - dram_ranges
[bank
].first_page
;
127 size
+= sizeof(hibernate_bitmap_t
) + ((pages
+ 31) >> 5) * sizeof(uint32_t);
130 list
= (hibernate_page_list_t
*)kalloc(size
);
134 list
->list_size
= size
;
135 list
->page_count
= page_count
;
136 list
->bank_count
= num_banks
;
138 // convert to hibernation bitmap.
140 bitmap
= &list
->bank_bitmap
[0];
141 for (bank
= 0; bank
< num_banks
; bank
++)
143 bitmap
->first_page
= dram_ranges
[bank
].first_page
;
144 bitmap
->last_page
= dram_ranges
[bank
].last_page
;
145 bitmap
->bitmapwords
= (bitmap
->last_page
+ 1
146 - bitmap
->first_page
+ 31) >> 5;
147 kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank
,
150 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
156 // mark pages not to be saved, but available for scratch usage during restore
159 hibernate_page_list_setall_machine( __unused hibernate_page_list_t
* page_list
,
160 __unused hibernate_page_list_t
* page_list_wired
,
161 __unused
uint32_t * pagesOut
)
165 // mark pages not to be saved and not for scratch usage during restore
167 hibernate_page_list_set_volatile( hibernate_page_list_t
* page_list
,
168 hibernate_page_list_t
* page_list_wired
,
171 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
173 hibernate_set_page_state(page_list
, page_list_wired
,
174 I386_HIB_PAGETABLE
, I386_HIB_PAGETABLE_COUNT
,
175 kIOHibernatePageStateFree
);
176 *pagesOut
-= I386_HIB_PAGETABLE_COUNT
;
178 if (args
->efiRuntimeServicesPageStart
)
180 hibernate_set_page_state(page_list
, page_list_wired
,
181 args
->efiRuntimeServicesPageStart
, args
->efiRuntimeServicesPageCount
,
182 kIOHibernatePageStateFree
);
183 *pagesOut
-= args
->efiRuntimeServicesPageCount
;
188 hibernate_processor_setup(IOHibernateImageHeader
* header
)
190 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
192 cpu_datap(0)->cpu_hibernate
= 1;
193 header
->processorFlags
= 0;
195 header
->runtimePages
= args
->efiRuntimeServicesPageStart
;
196 header
->runtimePageCount
= args
->efiRuntimeServicesPageCount
;
198 return (KERN_SUCCESS
);
202 hibernate_vm_lock(void)
204 if (current_cpu_datap()->cpu_hibernate
)
206 vm_page_lock_queues();
207 mutex_lock(&vm_page_queue_free_lock
);
212 hibernate_vm_unlock(void)
214 if (current_cpu_datap()->cpu_hibernate
)
216 mutex_unlock(&vm_page_queue_free_lock
);
217 vm_page_unlock_queues();