xref: /openbsd-src/sys/kern/subr_hibernate.c (revision 24aea4fd927e92efc99f32a861f067daa62421b9)
1 /*	$OpenBSD: subr_hibernate.c,v 1.102 2014/10/09 00:42:05 mlarkin Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl>
5  * Copyright (c) 2011 Mike Larkin <mlarkin@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/hibernate.h>
21 #include <sys/malloc.h>
22 #include <sys/param.h>
23 #include <sys/tree.h>
24 #include <sys/systm.h>
25 #include <sys/disklabel.h>
26 #include <sys/disk.h>
27 #include <sys/conf.h>
28 #include <sys/buf.h>
29 #include <sys/fcntl.h>
30 #include <sys/stat.h>
31 #include <uvm/uvm.h>
32 #include <uvm/uvm_swap.h>
33 #include <machine/hibernate.h>
34 
35 /*
36  * Hibernate piglet layout information
37  *
38  * The piglet is a scratch area of memory allocated by the suspending kernel.
39  * Its phys and virt addrs are recorded in the signature block. The piglet is
40  * used to guarantee an unused area of memory that can be used by the resuming
41  * kernel for various things. The piglet is excluded during unpack operations.
42  * The piglet size is presently 4*HIBERNATE_CHUNK_SIZE (typically 4*4MB).
43  *
44  * Offset from piglet_base	Purpose
45  * ----------------------------------------------------------------------------
46  * 0				Private page for suspend I/O write functions
47  * 1*PAGE_SIZE			I/O page used during hibernate suspend
48  * 2*PAGE_SIZE			I/O page used during hibernate suspend
49  * 3*PAGE_SIZE			copy page used during hibernate suspend
50  * 4*PAGE_SIZE			final chunk ordering list (24 pages)
51  * 28*PAGE_SIZE			RLE utility page
52  * 29*PAGE_SIZE			start of hiballoc area
53  * 109*PAGE_SIZE		end of hiballoc area (80 pages)
54  * ...				unused
55  * HIBERNATE_CHUNK_SIZE		start of hibernate chunk table
56  * 2*HIBERNATE_CHUNK_SIZE	bounce area for chunks being unpacked
57  * 4*HIBERNATE_CHUNK_SIZE	end of piglet
58  */
59 
60 /* Temporary vaddr ranges used during hibernate */
61 vaddr_t hibernate_temp_page;
62 vaddr_t hibernate_copy_page;
63 vaddr_t hibernate_rle_page;
64 
65 /* Hibernate info as read from disk during resume */
66 union hibernate_info disk_hib;
67 paddr_t global_pig_start;
68 vaddr_t global_piglet_va;
69 paddr_t global_piglet_pa;
70 
71 /* #define HIB_DEBUG */
72 #ifdef HIB_DEBUG
73 int	hib_debug = 99;
74 #define DPRINTF(x...)     do { if (hib_debug) printf(x); } while (0)
75 #define DNPRINTF(n,x...)  do { if (hib_debug > (n)) printf(x); } while (0)
76 #else
77 #define DPRINTF(x...)
78 #define DNPRINTF(n,x...)
79 #endif
80 
81 #ifndef NO_PROPOLICE
82 extern long __guard_local;
83 #endif /* ! NO_PROPOLICE */
84 
85 void hibernate_copy_chunk_to_piglet(paddr_t, vaddr_t, size_t);
86 int hibernate_calc_rle(paddr_t, paddr_t);
87 int hibernate_write_rle(union hibernate_info *, paddr_t, paddr_t, daddr_t *,
88 	size_t *);
89 
90 #define MAX_RLE (HIBERNATE_CHUNK_SIZE / PAGE_SIZE)
91 
92 /*
93  * Hib alloc enforced alignment.
94  */
95 #define HIB_ALIGN		8 /* bytes alignment */
96 
97 /*
98  * sizeof builtin operation, but with alignment constraint.
99  */
100 #define HIB_SIZEOF(_type)	roundup(sizeof(_type), HIB_ALIGN)
101 
102 struct hiballoc_entry {
103 	size_t			hibe_use;
104 	size_t			hibe_space;
105 	RB_ENTRY(hiballoc_entry) hibe_entry;
106 };
107 
108 /*
109  * Sort hibernate memory ranges by ascending PA
110  */
111 void
112 hibernate_sort_ranges(union hibernate_info *hib_info)
113 {
114 	int i, j;
115 	struct hibernate_memory_range *ranges;
116 	paddr_t base, end;
117 
118 	ranges = hib_info->ranges;
119 
120 	for (i = 1; i < hib_info->nranges; i++) {
121 		j = i;
122 		while (j > 0 && ranges[j - 1].base > ranges[j].base) {
123 			base = ranges[j].base;
124 			end = ranges[j].end;
125 			ranges[j].base = ranges[j - 1].base;
126 			ranges[j].end = ranges[j - 1].end;
127 			ranges[j - 1].base = base;
128 			ranges[j - 1].end = end;
129 			j--;
130 		}
131 	}
132 }
133 
134 /*
135  * Compare hiballoc entries based on the address they manage.
136  *
137  * Since the address is fixed, relative to struct hiballoc_entry,
138  * we just compare the hiballoc_entry pointers.
139  */
140 static __inline int
141 hibe_cmp(struct hiballoc_entry *l, struct hiballoc_entry *r)
142 {
143 	return l < r ? -1 : (l > r);
144 }
145 
146 RB_PROTOTYPE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
147 
148 /*
149  * Given a hiballoc entry, return the address it manages.
150  */
151 static __inline void *
152 hib_entry_to_addr(struct hiballoc_entry *entry)
153 {
154 	caddr_t addr;
155 
156 	addr = (caddr_t)entry;
157 	addr += HIB_SIZEOF(struct hiballoc_entry);
158 	return addr;
159 }
160 
161 /*
162  * Given an address, find the hiballoc that corresponds.
163  */
164 static __inline struct hiballoc_entry*
165 hib_addr_to_entry(void *addr_param)
166 {
167 	caddr_t addr;
168 
169 	addr = (caddr_t)addr_param;
170 	addr -= HIB_SIZEOF(struct hiballoc_entry);
171 	return (struct hiballoc_entry*)addr;
172 }
173 
174 RB_GENERATE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
175 
176 /*
177  * Allocate memory from the arena.
178  *
179  * Returns NULL if no memory is available.
180  */
181 void *
182 hib_alloc(struct hiballoc_arena *arena, size_t alloc_sz)
183 {
184 	struct hiballoc_entry *entry, *new_entry;
185 	size_t find_sz;
186 
187 	/*
188 	 * Enforce alignment of HIB_ALIGN bytes.
189 	 *
190 	 * Note that, because the entry is put in front of the allocation,
191 	 * 0-byte allocations are guaranteed a unique address.
192 	 */
193 	alloc_sz = roundup(alloc_sz, HIB_ALIGN);
194 
195 	/*
196 	 * Find an entry with hibe_space >= find_sz.
197 	 *
198 	 * If the root node is not large enough, we switch to tree traversal.
199 	 * Because all entries are made at the bottom of the free space,
200 	 * traversal from the end has a slightly better chance of yielding
201 	 * a sufficiently large space.
202 	 */
203 	find_sz = alloc_sz + HIB_SIZEOF(struct hiballoc_entry);
204 	entry = RB_ROOT(&arena->hib_addrs);
205 	if (entry != NULL && entry->hibe_space < find_sz) {
206 		RB_FOREACH_REVERSE(entry, hiballoc_addr, &arena->hib_addrs) {
207 			if (entry->hibe_space >= find_sz)
208 				break;
209 		}
210 	}
211 
212 	/*
213 	 * Insufficient or too fragmented memory.
214 	 */
215 	if (entry == NULL)
216 		return NULL;
217 
218 	/*
219 	 * Create new entry in allocated space.
220 	 */
221 	new_entry = (struct hiballoc_entry*)(
222 	    (caddr_t)hib_entry_to_addr(entry) + entry->hibe_use);
223 	new_entry->hibe_space = entry->hibe_space - find_sz;
224 	new_entry->hibe_use = alloc_sz;
225 
226 	/*
227 	 * Insert entry.
228 	 */
229 	if (RB_INSERT(hiballoc_addr, &arena->hib_addrs, new_entry) != NULL)
230 		panic("hib_alloc: insert failure");
231 	entry->hibe_space = 0;
232 
233 	/* Return address managed by entry. */
234 	return hib_entry_to_addr(new_entry);
235 }
236 
237 /*
238  * Free a pointer previously allocated from this arena.
239  *
240  * If addr is NULL, this will be silently accepted.
241  */
242 void
243 hib_free(struct hiballoc_arena *arena, void *addr)
244 {
245 	struct hiballoc_entry *entry, *prev;
246 
247 	if (addr == NULL)
248 		return;
249 
250 	/*
251 	 * Derive entry from addr and check it is really in this arena.
252 	 */
253 	entry = hib_addr_to_entry(addr);
254 	if (RB_FIND(hiballoc_addr, &arena->hib_addrs, entry) != entry)
255 		panic("hib_free: freed item %p not in hib arena", addr);
256 
257 	/*
258 	 * Give the space in entry to its predecessor.
259 	 *
260 	 * If entry has no predecessor, change its used space into free space
261 	 * instead.
262 	 */
263 	prev = RB_PREV(hiballoc_addr, &arena->hib_addrs, entry);
264 	if (prev != NULL &&
265 	    (void *)((caddr_t)prev + HIB_SIZEOF(struct hiballoc_entry) +
266 	    prev->hibe_use + prev->hibe_space) == entry) {
267 		/* Merge entry. */
268 		RB_REMOVE(hiballoc_addr, &arena->hib_addrs, entry);
269 		prev->hibe_space += HIB_SIZEOF(struct hiballoc_entry) +
270 		    entry->hibe_use + entry->hibe_space;
271 	} else {
272 		/* Flip used memory to free space. */
273 		entry->hibe_space += entry->hibe_use;
274 		entry->hibe_use = 0;
275 	}
276 }
277 
278 /*
279  * Initialize hiballoc.
280  *
281  * The allocator will manage memmory at ptr, which is len bytes.
282  */
283 int
284 hiballoc_init(struct hiballoc_arena *arena, void *p_ptr, size_t p_len)
285 {
286 	struct hiballoc_entry *entry;
287 	caddr_t ptr;
288 	size_t len;
289 
290 	RB_INIT(&arena->hib_addrs);
291 
292 	/*
293 	 * Hib allocator enforces HIB_ALIGN alignment.
294 	 * Fixup ptr and len.
295 	 */
296 	ptr = (caddr_t)roundup((vaddr_t)p_ptr, HIB_ALIGN);
297 	len = p_len - ((size_t)ptr - (size_t)p_ptr);
298 	len &= ~((size_t)HIB_ALIGN - 1);
299 
300 	/*
301 	 * Insufficient memory to be able to allocate and also do bookkeeping.
302 	 */
303 	if (len <= HIB_SIZEOF(struct hiballoc_entry))
304 		return ENOMEM;
305 
306 	/*
307 	 * Create entry describing space.
308 	 */
309 	entry = (struct hiballoc_entry*)ptr;
310 	entry->hibe_use = 0;
311 	entry->hibe_space = len - HIB_SIZEOF(struct hiballoc_entry);
312 	RB_INSERT(hiballoc_addr, &arena->hib_addrs, entry);
313 
314 	return 0;
315 }
316 
317 /*
318  * Zero all free memory.
319  */
320 void
321 uvm_pmr_zero_everything(void)
322 {
323 	struct uvm_pmemrange	*pmr;
324 	struct vm_page		*pg;
325 	int			 i;
326 
327 	uvm_lock_fpageq();
328 	TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
329 		/* Zero single pages. */
330 		while ((pg = TAILQ_FIRST(&pmr->single[UVM_PMR_MEMTYPE_DIRTY]))
331 		    != NULL) {
332 			uvm_pmr_remove(pmr, pg);
333 			uvm_pagezero(pg);
334 			atomic_setbits_int(&pg->pg_flags, PG_ZERO);
335 			uvmexp.zeropages++;
336 			uvm_pmr_insert(pmr, pg, 0);
337 		}
338 
339 		/* Zero multi page ranges. */
340 		while ((pg = RB_ROOT(&pmr->size[UVM_PMR_MEMTYPE_DIRTY]))
341 		    != NULL) {
342 			pg--; /* Size tree always has second page. */
343 			uvm_pmr_remove(pmr, pg);
344 			for (i = 0; i < pg->fpgsz; i++) {
345 				uvm_pagezero(&pg[i]);
346 				atomic_setbits_int(&pg[i].pg_flags, PG_ZERO);
347 				uvmexp.zeropages++;
348 			}
349 			uvm_pmr_insert(pmr, pg, 0);
350 		}
351 	}
352 	uvm_unlock_fpageq();
353 }
354 
355 /*
356  * Mark all memory as dirty.
357  *
358  * Used to inform the system that the clean memory isn't clean for some
359  * reason, for example because we just came back from hibernate.
360  */
361 void
362 uvm_pmr_dirty_everything(void)
363 {
364 	struct uvm_pmemrange	*pmr;
365 	struct vm_page		*pg;
366 	int			 i;
367 
368 	uvm_lock_fpageq();
369 	TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
370 		/* Dirty single pages. */
371 		while ((pg = TAILQ_FIRST(&pmr->single[UVM_PMR_MEMTYPE_ZERO]))
372 		    != NULL) {
373 			uvm_pmr_remove(pmr, pg);
374 			atomic_clearbits_int(&pg->pg_flags, PG_ZERO);
375 			uvm_pmr_insert(pmr, pg, 0);
376 		}
377 
378 		/* Dirty multi page ranges. */
379 		while ((pg = RB_ROOT(&pmr->size[UVM_PMR_MEMTYPE_ZERO]))
380 		    != NULL) {
381 			pg--; /* Size tree always has second page. */
382 			uvm_pmr_remove(pmr, pg);
383 			for (i = 0; i < pg->fpgsz; i++)
384 				atomic_clearbits_int(&pg[i].pg_flags, PG_ZERO);
385 			uvm_pmr_insert(pmr, pg, 0);
386 		}
387 	}
388 
389 	uvmexp.zeropages = 0;
390 	uvm_unlock_fpageq();
391 }
392 
393 /*
394  * Allocate an area that can hold sz bytes and doesn't overlap with
395  * the piglet at piglet_pa.
396  */
397 int
398 uvm_pmr_alloc_pig(paddr_t *pa, psize_t sz, paddr_t piglet_pa)
399 {
400 	struct uvm_constraint_range pig_constraint;
401 	struct kmem_pa_mode kp_pig = {
402 		.kp_constraint = &pig_constraint,
403 		.kp_maxseg = 1
404 	};
405 	vaddr_t va;
406 
407 	sz = round_page(sz);
408 
409 	pig_constraint.ucr_low = piglet_pa + 4 * HIBERNATE_CHUNK_SIZE;
410 	pig_constraint.ucr_high = -1;
411 
412 	va = (vaddr_t)km_alloc(sz, &kv_any, &kp_pig, &kd_nowait);
413 	if (va == 0) {
414 		pig_constraint.ucr_low = 0;
415 		pig_constraint.ucr_high = piglet_pa - 1;
416 
417 		va = (vaddr_t)km_alloc(sz, &kv_any, &kp_pig, &kd_nowait);
418 		if (va == 0)
419 			return ENOMEM;
420 	}
421 
422 	pmap_extract(pmap_kernel(), va, pa);
423 	return 0;
424 }
425 
426 /*
427  * Allocate a piglet area.
428  *
429  * This needs to be in DMA-safe memory.
430  * Piglets are aligned.
431  *
432  * sz and align in bytes.
433  *
434  * The call will sleep for the pagedaemon to attempt to free memory.
435  * The pagedaemon may decide its not possible to free enough memory, causing
436  * the allocation to fail.
437  */
438 int
439 uvm_pmr_alloc_piglet(vaddr_t *va, paddr_t *pa, vsize_t sz, paddr_t align)
440 {
441 	struct kmem_pa_mode kp_piglet = {
442 		.kp_constraint = &dma_constraint,
443 		.kp_align = align,
444 		.kp_maxseg = 1
445 	};
446 
447 	/* Ensure align is a power of 2 */
448 	KASSERT((align & (align - 1)) == 0);
449 
450 	/*
451 	 * Fixup arguments: align must be at least PAGE_SIZE,
452 	 * sz will be converted to pagecount, since that is what
453 	 * pmemrange uses internally.
454 	 */
455 	if (align < PAGE_SIZE)
456 		align = PAGE_SIZE;
457 	sz = round_page(sz);
458 
459 	*va = (vaddr_t)km_alloc(sz, &kv_any, &kp_piglet, &kd_nowait);
460 	if (*va == 0)
461 		return ENOMEM;
462 
463 	pmap_extract(pmap_kernel(), *va, pa);
464 	return 0;
465 }
466 
467 /*
468  * Free a piglet area.
469  */
470 void
471 uvm_pmr_free_piglet(vaddr_t va, vsize_t sz)
472 {
473 	/*
474 	 * Fix parameters.
475 	 */
476 	sz = round_page(sz);
477 
478 	/*
479 	 * Free the physical and virtual memory.
480 	 */
481 	km_free((void *)va, sz, &kv_any, &kp_dma_contig);
482 }
483 
484 /*
485  * Physmem RLE compression support.
486  *
487  * Given a physical page address, return the number of pages starting at the
488  * address that are free.  Clamps to the number of pages in
489  * HIBERNATE_CHUNK_SIZE. Returns 0 if the page at addr is not free.
490  */
491 int
492 uvm_page_rle(paddr_t addr)
493 {
494 	struct vm_page		*pg, *pg_end;
495 	struct vm_physseg	*vmp;
496 	int			 pseg_idx, off_idx;
497 
498 	pseg_idx = vm_physseg_find(atop(addr), &off_idx);
499 	if (pseg_idx == -1)
500 		return 0;
501 
502 	vmp = &vm_physmem[pseg_idx];
503 	pg = &vmp->pgs[off_idx];
504 	if (!(pg->pg_flags & PQ_FREE))
505 		return 0;
506 
507 	/*
508 	 * Search for the first non-free page after pg.
509 	 * Note that the page may not be the first page in a free pmemrange,
510 	 * therefore pg->fpgsz cannot be used.
511 	 */
512 	for (pg_end = pg; pg_end <= vmp->lastpg &&
513 	    (pg_end->pg_flags & PQ_FREE) == PQ_FREE; pg_end++)
514 		;
515 	return min((pg_end - pg), HIBERNATE_CHUNK_SIZE/PAGE_SIZE);
516 }
517 
518 /*
519  * Fills out the hibernate_info union pointed to by hib
520  * with information about this machine (swap signature block
521  * offsets, number of memory ranges, kernel in use, etc)
522  */
523 int
524 get_hibernate_info(union hibernate_info *hib, int suspend)
525 {
526 	struct disklabel dl;
527 	char err_string[128], *dl_ret;
528 
529 #ifndef NO_PROPOLICE
530 	/* Save propolice guard */
531 	hib->guard = __guard_local;
532 #endif /* ! NO_PROPOLICE */
533 
534 	/* Determine I/O function to use */
535 	hib->io_func = get_hibernate_io_function(swdevt[0].sw_dev);
536 	if (hib->io_func == NULL)
537 		return (1);
538 
539 	/* Calculate hibernate device */
540 	hib->dev = swdevt[0].sw_dev;
541 
542 	/* Read disklabel (used to calculate signature and image offsets) */
543 	dl_ret = disk_readlabel(&dl, hib->dev, err_string, sizeof(err_string));
544 
545 	if (dl_ret) {
546 		printf("Hibernate error reading disklabel: %s\n", dl_ret);
547 		return (1);
548 	}
549 
550 	/* Make sure we have a swap partition. */
551 	if (dl.d_partitions[1].p_fstype != FS_SWAP ||
552 	    DL_GETPSIZE(&dl.d_partitions[1]) == 0)
553 		return (1);
554 
555 	/* Make sure the signature can fit in one block */
556 	if (sizeof(union hibernate_info) > DEV_BSIZE)
557 		return (1);
558 
559 	/* Magic number */
560 	hib->magic = HIBERNATE_MAGIC;
561 
562 	/* Calculate signature block location */
563 	hib->sig_offset = DL_GETPSIZE(&dl.d_partitions[1]) -
564 	    sizeof(union hibernate_info)/DEV_BSIZE;
565 
566 	/* Stash kernel version information */
567 	memset(&hib->kernel_version, 0, 128);
568 	bcopy(version, &hib->kernel_version,
569 	    min(strlen(version), sizeof(hib->kernel_version)-1));
570 
571 	if (suspend) {
572 		hib->piglet_va = global_piglet_va;
573 		hib->piglet_pa = global_piglet_pa;
574 		hib->io_page = (void *)hib->piglet_va;
575 
576 		/*
577 		 * Initialization of the hibernate IO function for drivers
578 		 * that need to do prep work (such as allocating memory or
579 		 * setting up data structures that cannot safely be done
580 		 * during suspend without causing side effects). There is
581 		 * a matching HIB_DONE call performed after the write is
582 		 * completed.
583 		 */
584 		if (hib->io_func(hib->dev, DL_GETPOFFSET(&dl.d_partitions[1]),
585 		    (vaddr_t)NULL, DL_GETPSIZE(&dl.d_partitions[1]),
586 		    HIB_INIT, hib->io_page))
587 			goto fail;
588 
589 	} else {
590 		/*
591 		 * Resuming kernels use a regular private page for the driver
592 		 * No need to free this I/O page as it will vanish as part of
593 		 * the resume.
594 		 */
595 		hib->io_page = malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
596 		if (!hib->io_page)
597 			goto fail;
598 	}
599 
600 
601 	if (get_hibernate_info_md(hib))
602 		goto fail;
603 
604 	return (0);
605 
606 fail:
607 	return (1);
608 }
609 
610 /*
611  * Allocate nitems*size bytes from the hiballoc area presently in use
612  */
613 void *
614 hibernate_zlib_alloc(void *unused, int nitems, int size)
615 {
616 	struct hibernate_zlib_state *hibernate_state;
617 
618 	hibernate_state =
619 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
620 
621 	return hib_alloc(&hibernate_state->hiballoc_arena, nitems*size);
622 }
623 
624 /*
625  * Free the memory pointed to by addr in the hiballoc area presently in
626  * use
627  */
628 void
629 hibernate_zlib_free(void *unused, void *addr)
630 {
631 	struct hibernate_zlib_state *hibernate_state;
632 
633 	hibernate_state =
634 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
635 
636 	hib_free(&hibernate_state->hiballoc_arena, addr);
637 }
638 
639 /*
640  * Inflate next page of data from the image stream.
641  * The rle parameter is modified on exit to contain the number of pages to
642  * skip in the output stream (or 0 if this page was inflated into).
643  *
644  * Returns 0 if the stream contains additional data, or 1 if the stream is
645  * finished.
646  */
647 int
648 hibernate_inflate_page(int *rle)
649 {
650 	struct hibernate_zlib_state *hibernate_state;
651 	int i;
652 
653 	hibernate_state =
654 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
655 
656 	/* Set up the stream for RLE code inflate */
657 	hibernate_state->hib_stream.next_out = (char *)rle;
658 	hibernate_state->hib_stream.avail_out = sizeof(*rle);
659 
660 	/* Inflate RLE code */
661 	i = inflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH);
662 	if (i != Z_OK && i != Z_STREAM_END) {
663 		/*
664 		 * XXX - this will likely reboot/hang most machines
665 		 *       since the console output buffer will be unmapped,
666 		 *       but there's not much else we can do here.
667 		 */
668 		panic("rle inflate stream error");
669 	}
670 
671 	if (hibernate_state->hib_stream.avail_out != 0) {
672 		/*
673 		 * XXX - this will likely reboot/hang most machines
674 		 *       since the console output buffer will be unmapped,
675 		 *       but there's not much else we can do here.
676 		 */
677 		panic("rle short inflate error");
678 	}
679 
680 	if (*rle < 0 || *rle > 1024) {
681 		/*
682 		 * XXX - this will likely reboot/hang most machines
683 		 *       since the console output buffer will be unmapped,
684 		 *       but there's not much else we can do here.
685 		 */
686 		panic("invalid rle count");
687 	}
688 
689 	if (i == Z_STREAM_END)
690 		return (1);
691 
692 	if (*rle != 0)
693 		return (0);
694 
695 	/* Set up the stream for page inflate */
696 	hibernate_state->hib_stream.next_out = (char *)HIBERNATE_INFLATE_PAGE;
697 	hibernate_state->hib_stream.avail_out = PAGE_SIZE;
698 
699 	/* Process next block of data */
700 	i = inflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH);
701 	if (i != Z_OK && i != Z_STREAM_END) {
702 		/*
703 		 * XXX - this will likely reboot/hang most machines
704 		 *       since the console output buffer will be unmapped,
705 		 *       but there's not much else we can do here.
706 		 */
707 		panic("inflate error");
708 	}
709 
710 	/* We should always have extracted a full page ... */
711 	if (hibernate_state->hib_stream.avail_out != 0) {
712 		/*
713 		 * XXX - this will likely reboot/hang most machines
714 		 *       since the console output buffer will be unmapped,
715 		 *       but there's not much else we can do here.
716 		 */
717 		panic("incomplete page");
718 	}
719 
720 	return (i == Z_STREAM_END);
721 }
722 
723 /*
724  * Inflate size bytes from src into dest, skipping any pages in
725  * [src..dest] that are special (see hibernate_inflate_skip)
726  *
727  * This function executes while using the resume-time stack
728  * and pmap, and therefore cannot use ddb/printf/etc. Doing so
729  * will likely hang or reset the machine since the console output buffer
730  * will be unmapped.
731  */
732 void
733 hibernate_inflate_region(union hibernate_info *hib, paddr_t dest,
734     paddr_t src, size_t size)
735 {
736 	int end_stream = 0, rle;
737 	struct hibernate_zlib_state *hibernate_state;
738 
739 	hibernate_state =
740 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
741 
742 	hibernate_state->hib_stream.next_in = (char *)src;
743 	hibernate_state->hib_stream.avail_in = size;
744 
745 	do {
746 		/*
747 		 * Is this a special page? If yes, redirect the
748 		 * inflate output to a scratch page (eg, discard it)
749 		 */
750 		if (hibernate_inflate_skip(hib, dest)) {
751 			hibernate_enter_resume_mapping(
752 			    HIBERNATE_INFLATE_PAGE,
753 			    HIBERNATE_INFLATE_PAGE, 0);
754 		} else {
755 			hibernate_enter_resume_mapping(
756 			    HIBERNATE_INFLATE_PAGE, dest, 0);
757 		}
758 
759 		hibernate_flush();
760 		end_stream = hibernate_inflate_page(&rle);
761 
762 		if (rle == 0)
763 			dest += PAGE_SIZE;
764 		else
765 			dest += (rle * PAGE_SIZE);
766 	} while (!end_stream);
767 }
768 
769 /*
770  * deflate from src into the I/O page, up to 'remaining' bytes
771  *
772  * Returns number of input bytes consumed, and may reset
773  * the 'remaining' parameter if not all the output space was consumed
774  * (this information is needed to know how much to write to disk
775  */
776 size_t
777 hibernate_deflate(union hibernate_info *hib, paddr_t src,
778     size_t *remaining)
779 {
780 	vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
781 	struct hibernate_zlib_state *hibernate_state;
782 
783 	hibernate_state =
784 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
785 
786 	/* Set up the stream for deflate */
787 	hibernate_state->hib_stream.next_in = (caddr_t)src;
788 	hibernate_state->hib_stream.avail_in = PAGE_SIZE - (src & PAGE_MASK);
789 	hibernate_state->hib_stream.next_out = (caddr_t)hibernate_io_page +
790 	    (PAGE_SIZE - *remaining);
791 	hibernate_state->hib_stream.avail_out = *remaining;
792 
793 	/* Process next block of data */
794 	if (deflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH) != Z_OK)
795 		panic("hibernate zlib deflate error");
796 
797 	/* Update pointers and return number of bytes consumed */
798 	*remaining = hibernate_state->hib_stream.avail_out;
799 	return (PAGE_SIZE - (src & PAGE_MASK)) -
800 	    hibernate_state->hib_stream.avail_in;
801 }
802 
803 /*
804  * Write the hibernation information specified in hiber_info
805  * to the location in swap previously calculated (last block of
806  * swap), called the "signature block".
807  */
808 int
809 hibernate_write_signature(union hibernate_info *hib)
810 {
811 	/* Write hibernate info to disk */
812 	return (hib->io_func(hib->dev, hib->sig_offset,
813 	    (vaddr_t)hib, DEV_BSIZE, HIB_W,
814 	    hib->io_page));
815 }
816 
817 /*
818  * Write the memory chunk table to the area in swap immediately
819  * preceding the signature block. The chunk table is stored
820  * in the piglet when this function is called.  Returns errno.
821  */
822 int
823 hibernate_write_chunktable(union hibernate_info *hib)
824 {
825 	vaddr_t hibernate_chunk_table_start;
826 	size_t hibernate_chunk_table_size;
827 	int i, err;
828 
829 	hibernate_chunk_table_size = HIBERNATE_CHUNK_TABLE_SIZE;
830 
831 	hibernate_chunk_table_start = hib->piglet_va +
832 	    HIBERNATE_CHUNK_SIZE;
833 
834 	/* Write chunk table */
835 	for (i = 0; i < hibernate_chunk_table_size; i += MAXPHYS) {
836 		if ((err = hib->io_func(hib->dev,
837 		    hib->chunktable_offset + (i/DEV_BSIZE),
838 		    (vaddr_t)(hibernate_chunk_table_start + i),
839 		    MAXPHYS, HIB_W, hib->io_page))) {
840 			DPRINTF("chunktable write error: %d\n", err);
841 			return (err);
842 		}
843 	}
844 
845 	return (0);
846 }
847 
848 /*
849  * Write an empty hiber_info to the swap signature block, which is
850  * guaranteed to not match any valid hib.
851  */
852 int
853 hibernate_clear_signature(void)
854 {
855 	union hibernate_info blank_hiber_info;
856 	union hibernate_info hib;
857 
858 	/* Zero out a blank hiber_info */
859 	memset(&blank_hiber_info, 0, sizeof(union hibernate_info));
860 
861 	/* Get the signature block location */
862 	if (get_hibernate_info(&hib, 0))
863 		return (1);
864 
865 	/* Write (zeroed) hibernate info to disk */
866 	DPRINTF("clearing hibernate signature block location: %lld\n",
867 		hib.sig_offset);
868 	if (hibernate_block_io(&hib,
869 	    hib.sig_offset,
870 	    DEV_BSIZE, (vaddr_t)&blank_hiber_info, 1))
871 		printf("Warning: could not clear hibernate signature\n");
872 
873 	return (0);
874 }
875 
876 /*
877  * Compare two hibernate_infos to determine if they are the same (eg,
878  * we should be performing a hibernate resume on this machine.
879  * Not all fields are checked - just enough to verify that the machine
880  * has the same memory configuration and kernel as the one that
881  * wrote the signature previously.
882  */
883 int
884 hibernate_compare_signature(union hibernate_info *mine,
885     union hibernate_info *disk)
886 {
887 	u_int i;
888 
889 	if (mine->nranges != disk->nranges) {
890 		DPRINTF("hibernate memory range count mismatch\n");
891 		return (1);
892 	}
893 
894 	if (strcmp(mine->kernel_version, disk->kernel_version) != 0) {
895 		DPRINTF("hibernate kernel version mismatch\n");
896 		return (1);
897 	}
898 
899 	for (i = 0; i < mine->nranges; i++) {
900 		if ((mine->ranges[i].base != disk->ranges[i].base) ||
901 		    (mine->ranges[i].end != disk->ranges[i].end) ) {
902 			DPRINTF("hib range %d mismatch [%p-%p != %p-%p]\n",
903 				i,
904 				(void *)mine->ranges[i].base,
905 				(void *)mine->ranges[i].end,
906 				(void *)disk->ranges[i].base,
907 				(void *)disk->ranges[i].end);
908 			return (1);
909 		}
910 	}
911 
912 	return (0);
913 }
914 
915 /*
916  * Transfers xfer_size bytes between the hibernate device specified in
917  * hib_info at offset blkctr and the vaddr specified at dest.
918  *
919  * Separate offsets and pages are used to handle misaligned reads (reads
920  * that span a page boundary).
921  *
922  * blkctr specifies a relative offset (relative to the start of swap),
923  * not an absolute disk offset
924  *
925  */
926 int
927 hibernate_block_io(union hibernate_info *hib, daddr_t blkctr,
928     size_t xfer_size, vaddr_t dest, int iswrite)
929 {
930 	struct buf *bp;
931 	struct bdevsw *bdsw;
932 	int error;
933 
934 	bp = geteblk(xfer_size);
935 	bdsw = &bdevsw[major(hib->dev)];
936 
937 	error = (*bdsw->d_open)(hib->dev, FREAD, S_IFCHR, curproc);
938 	if (error) {
939 		printf("hibernate_block_io open failed\n");
940 		return (1);
941 	}
942 
943 	if (iswrite)
944 		bcopy((caddr_t)dest, bp->b_data, xfer_size);
945 
946 	bp->b_bcount = xfer_size;
947 	bp->b_blkno = blkctr;
948 	CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
949 	SET(bp->b_flags, B_BUSY | (iswrite ? B_WRITE : B_READ) | B_RAW);
950 	bp->b_dev = hib->dev;
951 	(*bdsw->d_strategy)(bp);
952 
953 	error = biowait(bp);
954 	if (error) {
955 		printf("hib block_io biowait error %d blk %lld size %zu\n",
956 			error, (long long)blkctr, xfer_size);
957 		error = (*bdsw->d_close)(hib->dev, 0, S_IFCHR,
958 		    curproc);
959 		if (error)
960 			printf("hibernate_block_io error close failed\n");
961 		return (1);
962 	}
963 
964 	error = (*bdsw->d_close)(hib->dev, FREAD, S_IFCHR, curproc);
965 	if (error) {
966 		printf("hibernate_block_io close failed\n");
967 		return (1);
968 	}
969 
970 	if (!iswrite)
971 		bcopy(bp->b_data, (caddr_t)dest, xfer_size);
972 
973 	bp->b_flags |= B_INVAL;
974 	brelse(bp);
975 
976 	return (0);
977 }
978 
979 /*
980  * Reads the signature block from swap, checks against the current machine's
981  * information. If the information matches, perform a resume by reading the
982  * saved image into the pig area, and unpacking.
983  */
984 void
985 hibernate_resume(void)
986 {
987 	union hibernate_info hib;
988 	int s;
989 
990 	/* Get current running machine's hibernate info */
991 	memset(&hib, 0, sizeof(hib));
992 	if (get_hibernate_info(&hib, 0)) {
993 		DPRINTF("couldn't retrieve machine's hibernate info\n");
994 		return;
995 	}
996 
997 	/* Read hibernate info from disk */
998 	s = splbio();
999 
1000 	DPRINTF("reading hibernate signature block location: %lld\n",
1001 		hib.sig_offset);
1002 
1003 	if (hibernate_block_io(&hib,
1004 	    hib.sig_offset,
1005 	    DEV_BSIZE, (vaddr_t)&disk_hib, 0)) {
1006 		DPRINTF("error in hibernate read");
1007 		splx(s);
1008 		return;
1009 	}
1010 
1011 	/* Check magic number */
1012 	if (disk_hib.magic != HIBERNATE_MAGIC) {
1013 		DPRINTF("wrong magic number in hibernate signature: %x\n",
1014 			disk_hib.magic);
1015 		splx(s);
1016 		return;
1017 	}
1018 
1019 	/*
1020 	 * We (possibly) found a hibernate signature. Clear signature first,
1021 	 * to prevent accidental resume or endless resume cycles later.
1022 	 */
1023 	if (hibernate_clear_signature()) {
1024 		DPRINTF("error clearing hibernate signature block\n");
1025 		splx(s);
1026 		return;
1027 	}
1028 
1029 	/*
1030 	 * If on-disk and in-memory hibernate signatures match,
1031 	 * this means we should do a resume from hibernate.
1032 	 */
1033 	if (hibernate_compare_signature(&hib, &disk_hib)) {
1034 		DPRINTF("mismatched hibernate signature block\n");
1035 		splx(s);
1036 		return;
1037 	}
1038 
1039 #ifdef MULTIPROCESSOR
1040 	/* XXX - if we fail later, we may need to rehatch APs on some archs */
1041 	DPRINTF("hibernate: quiescing APs\n");
1042 	hibernate_quiesce_cpus();
1043 #endif /* MULTIPROCESSOR */
1044 
1045 	/* Read the image from disk into the image (pig) area */
1046 	if (hibernate_read_image(&disk_hib))
1047 		goto fail;
1048 
1049 	DPRINTF("hibernate: quiescing devices\n");
1050 	if (config_suspend_all(DVACT_QUIESCE) != 0)
1051 		goto fail;
1052 
1053 	(void) splhigh();
1054 	hibernate_disable_intr_machdep();
1055 	cold = 1;
1056 
1057 	DPRINTF("hibernate: suspending devices\n");
1058 	if (config_suspend_all(DVACT_SUSPEND) != 0) {
1059 		cold = 0;
1060 		hibernate_enable_intr_machdep();
1061 		goto fail;
1062 	}
1063 
1064 	pmap_kenter_pa(HIBERNATE_HIBALLOC_PAGE, HIBERNATE_HIBALLOC_PAGE,
1065 	    VM_PROT_ALL);
1066 	pmap_activate(curproc);
1067 
1068 	printf("Unpacking image...\n");
1069 
1070 	/* Switch stacks */
1071 	DPRINTF("hibernate: switching stacks\n");
1072 	hibernate_switch_stack_machdep();
1073 
1074 #ifndef NO_PROPOLICE
1075 	/* Start using suspended kernel's propolice guard */
1076 	__guard_local = disk_hib.guard;
1077 #endif /* ! NO_PROPOLICE */
1078 
1079 	/* Unpack and resume */
1080 	hibernate_unpack_image(&disk_hib);
1081 
1082 fail:
1083 	splx(s);
1084 	printf("\nUnable to resume hibernated image\n");
1085 }
1086 
1087 /*
1088  * Unpack image from pig area to original location by looping through the
1089  * list of output chunks in the order they should be restored (fchunks).
1090  *
1091  * Note that due to the stack smash protector and the fact that we have
1092  * switched stacks, it is not permitted to return from this function.
1093  */
1094 void
1095 hibernate_unpack_image(union hibernate_info *hib)
1096 {
1097 	struct hibernate_disk_chunk *chunks;
1098 	union hibernate_info local_hib;
1099 	paddr_t image_cur = global_pig_start;
1100 	short i, *fchunks;
1101 	char *pva;
1102 	struct hibernate_zlib_state *hibernate_state;
1103 
1104 	hibernate_state =
1105 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
1106 
1107 	/* Piglet will be identity mapped (VA == PA) */
1108 	pva = (char *)hib->piglet_pa;
1109 
1110 	fchunks = (short *)(pva + (4 * PAGE_SIZE));
1111 
1112 	chunks = (struct hibernate_disk_chunk *)(pva + HIBERNATE_CHUNK_SIZE);
1113 
1114 	/* Can't use hiber_info that's passed in after this point */
1115 	bcopy(hib, &local_hib, sizeof(union hibernate_info));
1116 
1117 	/* VA == PA */
1118 	local_hib.piglet_va = local_hib.piglet_pa;
1119 
1120 	/*
1121 	 * Point of no return. Once we pass this point, only kernel code can
1122 	 * be accessed. No global variables or other kernel data structures
1123 	 * are guaranteed to be coherent after unpack starts.
1124 	 *
1125 	 * The image is now in high memory (pig area), we unpack from the pig
1126 	 * to the correct location in memory. We'll eventually end up copying
1127 	 * on top of ourself, but we are assured the kernel code here is the
1128 	 * same between the hibernated and resuming kernel, and we are running
1129 	 * on our own stack, so the overwrite is ok.
1130 	 */
1131 	DPRINTF("hibernate: activating alt. pagetable and starting unpack\n");
1132 	hibernate_activate_resume_pt_machdep();
1133 
1134 	for (i = 0; i < local_hib.chunk_ctr; i++) {
1135 		/* Reset zlib for inflate */
1136 		if (hibernate_zlib_reset(&local_hib, 0) != Z_OK)
1137 			panic("hibernate failed to reset zlib for inflate");
1138 
1139 		hibernate_process_chunk(&local_hib, &chunks[fchunks[i]],
1140 		    image_cur);
1141 
1142 		image_cur += chunks[fchunks[i]].compressed_size;
1143 
1144 	}
1145 
1146 	/*
1147 	 * Resume the loaded kernel by jumping to the MD resume vector.
1148 	 * We won't be returning from this call.
1149 	 */
1150 	hibernate_resume_machdep();
1151 }
1152 
1153 /*
1154  * Bounce a compressed image chunk to the piglet, entering mappings for the
1155  * copied pages as needed
1156  */
1157 void
1158 hibernate_copy_chunk_to_piglet(paddr_t img_cur, vaddr_t piglet, size_t size)
1159 {
1160 	size_t ct, ofs;
1161 	paddr_t src = img_cur;
1162 	vaddr_t dest = piglet;
1163 
1164 	/* Copy first partial page */
1165 	ct = (PAGE_SIZE) - (src & PAGE_MASK);
1166 	ofs = (src & PAGE_MASK);
1167 
1168 	if (ct < PAGE_SIZE) {
1169 		hibernate_enter_resume_mapping(HIBERNATE_INFLATE_PAGE,
1170 			(src - ofs), 0);
1171 		hibernate_flush();
1172 		bcopy((caddr_t)(HIBERNATE_INFLATE_PAGE + ofs), (caddr_t)dest, ct);
1173 		src += ct;
1174 		dest += ct;
1175 	}
1176 
1177 	/* Copy remaining pages */
1178 	while (src < size + img_cur) {
1179 		hibernate_enter_resume_mapping(HIBERNATE_INFLATE_PAGE, src, 0);
1180 		hibernate_flush();
1181 		ct = PAGE_SIZE;
1182 		bcopy((caddr_t)(HIBERNATE_INFLATE_PAGE), (caddr_t)dest, ct);
1183 		hibernate_flush();
1184 		src += ct;
1185 		dest += ct;
1186 	}
1187 }
1188 
1189 /*
1190  * Process a chunk by bouncing it to the piglet, followed by unpacking
1191  */
1192 void
1193 hibernate_process_chunk(union hibernate_info *hib,
1194     struct hibernate_disk_chunk *chunk, paddr_t img_cur)
1195 {
1196 	char *pva = (char *)hib->piglet_va;
1197 
1198 	hibernate_copy_chunk_to_piglet(img_cur,
1199 	 (vaddr_t)(pva + (HIBERNATE_CHUNK_SIZE * 2)), chunk->compressed_size);
1200 	hibernate_inflate_region(hib, chunk->base,
1201 	    (vaddr_t)(pva + (HIBERNATE_CHUNK_SIZE * 2)),
1202 	    chunk->compressed_size);
1203 }
1204 
1205 /*
1206  * Calculate RLE component for 'inaddr'. Clamps to max RLE pages between
1207  * inaddr and range_end.
1208  */
1209 int
1210 hibernate_calc_rle(paddr_t inaddr, paddr_t range_end)
1211 {
1212 	int rle;
1213 
1214 	rle = uvm_page_rle(inaddr);
1215 	KASSERT(rle >= 0 && rle <= MAX_RLE);
1216 
1217 	/* Clamp RLE to range end */
1218 	if (rle > 0 && inaddr + (rle * PAGE_SIZE) > range_end)
1219 		rle = (range_end - inaddr) / PAGE_SIZE;
1220 
1221 	return (rle);
1222 }
1223 
1224 /*
1225  * Write the RLE byte for page at 'inaddr' to the output stream.
1226  * Returns the number of pages to be skipped at 'inaddr'.
1227  */
1228 int
1229 hibernate_write_rle(union hibernate_info *hib, paddr_t inaddr,
1230 	paddr_t range_end, daddr_t *blkctr,
1231 	size_t *out_remaining)
1232 {
1233 	int rle, err, *rleloc;
1234 	struct hibernate_zlib_state *hibernate_state;
1235 	vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
1236 
1237 	hibernate_state =
1238 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
1239 
1240 	rle = hibernate_calc_rle(inaddr, range_end);
1241 
1242 	rleloc = (int *)hibernate_rle_page + MAX_RLE - 1;
1243 	*rleloc = rle;
1244 
1245 	/* Deflate the RLE byte into the stream */
1246 	hibernate_deflate(hib, (paddr_t)rleloc, out_remaining);
1247 
1248 	/* Did we fill the output page? If so, flush to disk */
1249 	if (*out_remaining == 0) {
1250 		if ((err = hib->io_func(hib->dev, *blkctr + hib->image_offset,
1251 			(vaddr_t)hibernate_io_page, PAGE_SIZE, HIB_W,
1252 			hib->io_page))) {
1253 				DPRINTF("hib write error %d\n", err);
1254 				return (err);
1255 		}
1256 
1257 		*blkctr += PAGE_SIZE / DEV_BSIZE;
1258 		*out_remaining = PAGE_SIZE;
1259 
1260 		/* If we didn't deflate the entire RLE byte, finish it now */
1261 		if (hibernate_state->hib_stream.avail_in != 0)
1262 			hibernate_deflate(hib,
1263 				(vaddr_t)hibernate_state->hib_stream.next_in,
1264 				out_remaining);
1265 	}
1266 
1267 	return (rle);
1268 }
1269 
1270 /*
1271  * Write a compressed version of this machine's memory to disk, at the
1272  * precalculated swap offset:
1273  *
1274  * end of swap - signature block size - chunk table size - memory size
1275  *
1276  * The function begins by looping through each phys mem range, cutting each
1277  * one into MD sized chunks. These chunks are then compressed individually
1278  * and written out to disk, in phys mem order. Some chunks might compress
1279  * more than others, and for this reason, each chunk's size is recorded
1280  * in the chunk table, which is written to disk after the image has
1281  * properly been compressed and written (in hibernate_write_chunktable).
1282  *
1283  * When this function is called, the machine is nearly suspended - most
1284  * devices are quiesced/suspended, interrupts are off, and cold has
1285  * been set. This means that there can be no side effects once the
1286  * write has started, and the write function itself can also have no
1287  * side effects. This also means no printfs are permitted (since printf
1288  * has side effects.)
1289  *
1290  * Return values :
1291  *
1292  * 0      - success
1293  * EIO    - I/O error occurred writing the chunks
1294  * EINVAL - Failed to write a complete range
1295  * ENOMEM - Memory allocation failure during preparation of the zlib arena
1296  */
1297 int
1298 hibernate_write_chunks(union hibernate_info *hib)
1299 {
1300 	paddr_t range_base, range_end, inaddr, temp_inaddr;
1301 	size_t nblocks, out_remaining, used;
1302 	struct hibernate_disk_chunk *chunks;
1303 	vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
1304 	daddr_t blkctr = 0;
1305 	int i, rle, err;
1306 	struct hibernate_zlib_state *hibernate_state;
1307 
1308 	hibernate_state =
1309 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
1310 
1311 	hib->chunk_ctr = 0;
1312 
1313 	/*
1314 	 * Map the utility VAs to the piglet. See the piglet map at the
1315 	 * top of this file for piglet layout information.
1316 	 */
1317 	pmap_kenter_pa(hibernate_copy_page,
1318 		(hib->piglet_pa + 3 * PAGE_SIZE), VM_PROT_ALL);
1319 	pmap_kenter_pa(hibernate_rle_page,
1320 		(hib->piglet_pa + 28 * PAGE_SIZE), VM_PROT_ALL);
1321 
1322 	pmap_activate(curproc);
1323 
1324 	chunks = (struct hibernate_disk_chunk *)(hib->piglet_va +
1325 	    HIBERNATE_CHUNK_SIZE);
1326 
1327 	/* Calculate the chunk regions */
1328 	for (i = 0; i < hib->nranges; i++) {
1329 		range_base = hib->ranges[i].base;
1330 		range_end = hib->ranges[i].end;
1331 
1332 		inaddr = range_base;
1333 
1334 		while (inaddr < range_end) {
1335 			chunks[hib->chunk_ctr].base = inaddr;
1336 			if (inaddr + HIBERNATE_CHUNK_SIZE < range_end)
1337 				chunks[hib->chunk_ctr].end = inaddr +
1338 				    HIBERNATE_CHUNK_SIZE;
1339 			else
1340 				chunks[hib->chunk_ctr].end = range_end;
1341 
1342 			inaddr += HIBERNATE_CHUNK_SIZE;
1343 			hib->chunk_ctr ++;
1344 		}
1345 	}
1346 
1347 	uvm_pmr_dirty_everything();
1348 	uvm_pmr_zero_everything();
1349 
1350 	/* Compress and write the chunks in the chunktable */
1351 	for (i = 0; i < hib->chunk_ctr; i++) {
1352 		range_base = chunks[i].base;
1353 		range_end = chunks[i].end;
1354 
1355 		chunks[i].offset = blkctr + hib->image_offset;
1356 
1357 		/* Reset zlib for deflate */
1358 		if (hibernate_zlib_reset(hib, 1) != Z_OK) {
1359 			DPRINTF("hibernate_zlib_reset failed for deflate\n");
1360 			return (ENOMEM);
1361 		}
1362 
1363 		inaddr = range_base;
1364 
1365 		/*
1366 		 * For each range, loop through its phys mem region
1367 		 * and write out the chunks (the last chunk might be
1368 		 * smaller than the chunk size).
1369 		 */
1370 		while (inaddr < range_end) {
1371 			out_remaining = PAGE_SIZE;
1372 			while (out_remaining > 0 && inaddr < range_end) {
1373 				/*
1374 				 * Adjust for regions that are not evenly
1375 				 * divisible by PAGE_SIZE or overflowed
1376 				 * pages from the previous iteration.
1377 				 */
1378 				temp_inaddr = (inaddr & PAGE_MASK) +
1379 				    hibernate_copy_page;
1380 
1381 				/* Deflate from temp_inaddr to IO page */
1382 				if (inaddr != range_end) {
1383 					if (inaddr % PAGE_SIZE == 0) {
1384 						rle = hibernate_write_rle(hib,
1385 							inaddr,
1386 							range_end,
1387 							&blkctr,
1388 							&out_remaining);
1389 					}
1390 
1391 					if (rle == 0) {
1392 						pmap_kenter_pa(hibernate_temp_page,
1393 							inaddr & PMAP_PA_MASK,
1394 							VM_PROT_ALL);
1395 
1396 						pmap_activate(curproc);
1397 
1398 						bcopy((caddr_t)hibernate_temp_page,
1399 							(caddr_t)hibernate_copy_page,
1400 							PAGE_SIZE);
1401 						inaddr += hibernate_deflate(hib,
1402 							temp_inaddr,
1403 							&out_remaining);
1404 					} else {
1405 						inaddr += rle * PAGE_SIZE;
1406 						if (inaddr > range_end)
1407 							inaddr = range_end;
1408 					}
1409 
1410 				}
1411 
1412 				if (out_remaining == 0) {
1413 					/* Filled up the page */
1414 					nblocks = PAGE_SIZE / DEV_BSIZE;
1415 
1416 					if ((err = hib->io_func(hib->dev,
1417 					    blkctr + hib->image_offset,
1418 					    (vaddr_t)hibernate_io_page,
1419 					    PAGE_SIZE, HIB_W, hib->io_page))) {
1420 						DPRINTF("hib write error %d\n",
1421 						    err);
1422 						return (err);
1423 					}
1424 
1425 					blkctr += nblocks;
1426 				}
1427 			}
1428 		}
1429 
1430 		if (inaddr != range_end) {
1431 			DPRINTF("deflate range ended prematurely\n");
1432 			return (EINVAL);
1433 		}
1434 
1435 		/*
1436 		 * End of range. Round up to next secsize bytes
1437 		 * after finishing compress
1438 		 */
1439 		if (out_remaining == 0)
1440 			out_remaining = PAGE_SIZE;
1441 
1442 		/* Finish compress */
1443 		hibernate_state->hib_stream.next_in = (caddr_t)inaddr;
1444 		hibernate_state->hib_stream.avail_in = 0;
1445 		hibernate_state->hib_stream.next_out =
1446 		    (caddr_t)hibernate_io_page + (PAGE_SIZE - out_remaining);
1447 
1448 		/* We have an extra output page available for finalize */
1449 		hibernate_state->hib_stream.avail_out =
1450 			out_remaining + PAGE_SIZE;
1451 
1452 		if ((err = deflate(&hibernate_state->hib_stream, Z_FINISH)) !=
1453 		    Z_STREAM_END) {
1454 			DPRINTF("deflate error in output stream: %d\n", err);
1455 			return (err);
1456 		}
1457 
1458 		out_remaining = hibernate_state->hib_stream.avail_out;
1459 
1460 		used = 2 * PAGE_SIZE - out_remaining;
1461 		nblocks = used / DEV_BSIZE;
1462 
1463 		/* Round up to next block if needed */
1464 		if (used % DEV_BSIZE != 0)
1465 			nblocks ++;
1466 
1467 		/* Write final block(s) for this chunk */
1468 		if ((err = hib->io_func(hib->dev, blkctr + hib->image_offset,
1469 		    (vaddr_t)hibernate_io_page, nblocks*DEV_BSIZE,
1470 		    HIB_W, hib->io_page))) {
1471 			DPRINTF("hib final write error %d\n", err);
1472 			return (err);
1473 		}
1474 
1475 		blkctr += nblocks;
1476 
1477 		chunks[i].compressed_size = (blkctr + hib->image_offset -
1478 		    chunks[i].offset) * DEV_BSIZE;
1479 	}
1480 
1481 	hib->chunktable_offset = hib->image_offset + blkctr;
1482 	return (0);
1483 }
1484 
1485 /*
1486  * Reset the zlib stream state and allocate a new hiballoc area for either
1487  * inflate or deflate. This function is called once for each hibernate chunk.
1488  * Calling hiballoc_init multiple times is acceptable since the memory it is
1489  * provided is unmanaged memory (stolen). We use the memory provided to us
1490  * by the piglet allocated via the supplied hib.
1491  */
1492 int
1493 hibernate_zlib_reset(union hibernate_info *hib, int deflate)
1494 {
1495 	vaddr_t hibernate_zlib_start;
1496 	size_t hibernate_zlib_size;
1497 	char *pva = (char *)hib->piglet_va;
1498 	struct hibernate_zlib_state *hibernate_state;
1499 
1500 	hibernate_state =
1501 	    (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
1502 
1503 	if (!deflate)
1504 		pva = (char *)((paddr_t)pva & (PIGLET_PAGE_MASK));
1505 
1506 	/*
1507 	 * See piglet layout information at the start of this file for
1508 	 * information on the zlib page assignments.
1509 	 */
1510 	hibernate_zlib_start = (vaddr_t)(pva + (29 * PAGE_SIZE));
1511 	hibernate_zlib_size = 80 * PAGE_SIZE;
1512 
1513 	memset((void *)hibernate_zlib_start, 0, hibernate_zlib_size);
1514 	memset(hibernate_state, 0, PAGE_SIZE);
1515 
1516 	/* Set up stream structure */
1517 	hibernate_state->hib_stream.zalloc = (alloc_func)hibernate_zlib_alloc;
1518 	hibernate_state->hib_stream.zfree = (free_func)hibernate_zlib_free;
1519 
1520 	/* Initialize the hiballoc arena for zlib allocs/frees */
1521 	hiballoc_init(&hibernate_state->hiballoc_arena,
1522 	    (caddr_t)hibernate_zlib_start, hibernate_zlib_size);
1523 
1524 	if (deflate) {
1525 		return deflateInit(&hibernate_state->hib_stream,
1526 		    Z_BEST_SPEED);
1527 	} else
1528 		return inflateInit(&hibernate_state->hib_stream);
1529 }
1530 
1531 /*
1532  * Reads the hibernated memory image from disk, whose location and
1533  * size are recorded in hib. Begin by reading the persisted
1534  * chunk table, which records the original chunk placement location
1535  * and compressed size for each. Next, allocate a pig region of
1536  * sufficient size to hold the compressed image. Next, read the
1537  * chunks into the pig area (calling hibernate_read_chunks to do this),
1538  * and finally, if all of the above succeeds, clear the hibernate signature.
1539  * The function will then return to hibernate_resume, which will proceed
1540  * to unpack the pig image to the correct place in memory.
1541  */
1542 int
1543 hibernate_read_image(union hibernate_info *hib)
1544 {
1545 	size_t compressed_size, disk_size, chunktable_size, pig_sz;
1546 	paddr_t image_start, image_end, pig_start, pig_end;
1547 	struct hibernate_disk_chunk *chunks;
1548 	daddr_t blkctr;
1549 	vaddr_t chunktable = (vaddr_t)NULL;
1550 	paddr_t piglet_chunktable = hib->piglet_pa +
1551 	    HIBERNATE_CHUNK_SIZE;
1552 	int i, status;
1553 
1554 	status = 0;
1555 	pmap_activate(curproc);
1556 
1557 	/* Calculate total chunk table size in disk blocks */
1558 	chunktable_size = HIBERNATE_CHUNK_TABLE_SIZE / DEV_BSIZE;
1559 
1560 	blkctr = hib->chunktable_offset;
1561 
1562 	chunktable = (vaddr_t)km_alloc(HIBERNATE_CHUNK_TABLE_SIZE, &kv_any,
1563 	    &kp_none, &kd_nowait);
1564 
1565 	if (!chunktable)
1566 		return (1);
1567 
1568 	/* Map chunktable pages */
1569 	for (i = 0; i < HIBERNATE_CHUNK_TABLE_SIZE; i += PAGE_SIZE)
1570 		pmap_kenter_pa(chunktable + i, piglet_chunktable + i,
1571 		    VM_PROT_ALL);
1572 	pmap_update(pmap_kernel());
1573 
1574 	/* Read the chunktable from disk into the piglet chunktable */
1575 	for (i = 0; i < HIBERNATE_CHUNK_TABLE_SIZE;
1576 	    i += MAXPHYS, blkctr += MAXPHYS/DEV_BSIZE)
1577 		hibernate_block_io(hib, blkctr, MAXPHYS,
1578 		    chunktable + i, 0);
1579 
1580 	blkctr = hib->image_offset;
1581 	compressed_size = 0;
1582 
1583 	chunks = (struct hibernate_disk_chunk *)chunktable;
1584 
1585 	for (i = 0; i < hib->chunk_ctr; i++)
1586 		compressed_size += chunks[i].compressed_size;
1587 
1588 	disk_size = compressed_size;
1589 
1590 	printf("unhibernating @ block %lld length %lu bytes\n",
1591 	    hib->sig_offset - chunktable_size,
1592 	    compressed_size);
1593 
1594 	/* Allocate the pig area */
1595 	pig_sz = compressed_size + HIBERNATE_CHUNK_SIZE;
1596 	if (uvm_pmr_alloc_pig(&pig_start, pig_sz, hib->piglet_pa) == ENOMEM) {
1597 		status = 1;
1598 		goto unmap;
1599 	}
1600 
1601 	pig_end = pig_start + pig_sz;
1602 
1603 	/* Calculate image extents. Pig image must end on a chunk boundary. */
1604 	image_end = pig_end & ~(HIBERNATE_CHUNK_SIZE - 1);
1605 	image_start = image_end - disk_size;
1606 
1607 	hibernate_read_chunks(hib, image_start, image_end, disk_size,
1608 	    chunks);
1609 
1610 	/* Prepare the resume time pmap/page table */
1611 	hibernate_populate_resume_pt(hib, image_start, image_end);
1612 
1613 unmap:
1614 	/* Unmap chunktable pages */
1615 	pmap_kremove(chunktable, HIBERNATE_CHUNK_TABLE_SIZE);
1616 	pmap_update(pmap_kernel());
1617 
1618 	return (status);
1619 }
1620 
1621 /*
1622  * Read the hibernated memory chunks from disk (chunk information at this
1623  * point is stored in the piglet) into the pig area specified by
1624  * [pig_start .. pig_end]. Order the chunks so that the final chunk is the
1625  * only chunk with overlap possibilities.
1626  */
1627 int
1628 hibernate_read_chunks(union hibernate_info *hib, paddr_t pig_start,
1629     paddr_t pig_end, size_t image_compr_size,
1630     struct hibernate_disk_chunk *chunks)
1631 {
1632 	paddr_t img_cur, piglet_base;
1633 	daddr_t blkctr;
1634 	size_t processed, compressed_size, read_size;
1635 	int nchunks, nfchunks, num_io_pages;
1636 	vaddr_t tempva, hibernate_fchunk_area;
1637 	short *fchunks, i, j;
1638 
1639 	tempva = (vaddr_t)NULL;
1640 	hibernate_fchunk_area = (vaddr_t)NULL;
1641 	nfchunks = 0;
1642 	piglet_base = hib->piglet_pa;
1643 	global_pig_start = pig_start;
1644 
1645 	pmap_activate(curproc);
1646 
1647 	/*
1648 	 * These mappings go into the resuming kernel's page table, and are
1649 	 * used only during image read. They dissappear from existence
1650 	 * when the suspended kernel is unpacked on top of us.
1651 	 */
1652 	tempva = (vaddr_t)km_alloc(MAXPHYS + PAGE_SIZE, &kv_any, &kp_none,
1653 		&kd_nowait);
1654 	if (!tempva)
1655 		return (1);
1656 	hibernate_fchunk_area = (vaddr_t)km_alloc(24 * PAGE_SIZE, &kv_any,
1657 	    &kp_none, &kd_nowait);
1658 	if (!hibernate_fchunk_area)
1659 		return (1);
1660 
1661 	/* Final output chunk ordering VA */
1662 	fchunks = (short *)hibernate_fchunk_area;
1663 
1664 	/* Map the chunk ordering region */
1665 	for(i = 0; i < 24 ; i++)
1666 		pmap_kenter_pa(hibernate_fchunk_area + (i * PAGE_SIZE),
1667 			piglet_base + ((4 + i) * PAGE_SIZE), VM_PROT_ALL);
1668 	pmap_update(pmap_kernel());
1669 
1670 	nchunks = hib->chunk_ctr;
1671 
1672 	/* Initially start all chunks as unplaced */
1673 	for (i = 0; i < nchunks; i++)
1674 		chunks[i].flags = 0;
1675 
1676 	/*
1677 	 * Search the list for chunks that are outside the pig area. These
1678 	 * can be placed first in the final output list.
1679 	 */
1680 	for (i = 0; i < nchunks; i++) {
1681 		if (chunks[i].end <= pig_start || chunks[i].base >= pig_end) {
1682 			fchunks[nfchunks] = i;
1683 			nfchunks++;
1684 			chunks[i].flags |= HIBERNATE_CHUNK_PLACED;
1685 		}
1686 	}
1687 
1688 	/*
1689 	 * Walk the ordering, place the chunks in ascending memory order.
1690 	 */
1691 	for (i = 0; i < nchunks; i++) {
1692 		if (chunks[i].flags != HIBERNATE_CHUNK_PLACED) {
1693 			fchunks[nfchunks] = i;
1694 			nfchunks++;
1695 			chunks[i].flags = HIBERNATE_CHUNK_PLACED;
1696 		}
1697 	}
1698 
1699 	img_cur = pig_start;
1700 
1701 	for (i = 0; i < nfchunks; i++) {
1702 		blkctr = chunks[fchunks[i]].offset;
1703 		processed = 0;
1704 		compressed_size = chunks[fchunks[i]].compressed_size;
1705 
1706 		while (processed < compressed_size) {
1707 			if (compressed_size - processed >= MAXPHYS)
1708 				read_size = MAXPHYS;
1709 			else
1710 				read_size = compressed_size - processed;
1711 
1712 			/*
1713 			 * We're reading read_size bytes, offset from the
1714 			 * start of a page by img_cur % PAGE_SIZE, so the
1715 			 * end will be read_size + (img_cur % PAGE_SIZE)
1716 			 * from the start of the first page.  Round that
1717 			 * up to the next page size.
1718 			 */
1719 			num_io_pages = (read_size + (img_cur % PAGE_SIZE)
1720 				+ PAGE_SIZE - 1) / PAGE_SIZE;
1721 
1722 			KASSERT(num_io_pages <= MAXPHYS/PAGE_SIZE + 1);
1723 
1724 			/* Map pages for this read */
1725 			for (j = 0; j < num_io_pages; j ++)
1726 				pmap_kenter_pa(tempva + j * PAGE_SIZE,
1727 					img_cur + j * PAGE_SIZE, VM_PROT_ALL);
1728 
1729 			pmap_update(pmap_kernel());
1730 
1731 			hibernate_block_io(hib, blkctr, read_size,
1732 			    tempva + (img_cur & PAGE_MASK), 0);
1733 
1734 			blkctr += (read_size / DEV_BSIZE);
1735 
1736 			pmap_kremove(tempva, num_io_pages * PAGE_SIZE);
1737 			pmap_update(pmap_kernel());
1738 
1739 			processed += read_size;
1740 			img_cur += read_size;
1741 		}
1742 	}
1743 
1744 	pmap_kremove(hibernate_fchunk_area, 24 * PAGE_SIZE);
1745 	pmap_update(pmap_kernel());
1746 
1747 	return (0);
1748 }
1749 
1750 /*
1751  * Hibernating a machine comprises the following operations:
1752  *  1. Calculating this machine's hibernate_info information
1753  *  2. Allocating a piglet and saving the piglet's physaddr
1754  *  3. Calculating the memory chunks
1755  *  4. Writing the compressed chunks to disk
1756  *  5. Writing the chunk table
1757  *  6. Writing the signature block (hibernate_info)
1758  *
1759  * On most architectures, the function calling hibernate_suspend would
1760  * then power off the machine using some MD-specific implementation.
1761  */
1762 int
1763 hibernate_suspend(void)
1764 {
1765 	union hibernate_info hib;
1766 	u_long start, end;
1767 
1768 	/*
1769 	 * Calculate memory ranges, swap offsets, etc.
1770 	 * This also allocates a piglet whose physaddr is stored in
1771 	 * hib->piglet_pa and vaddr stored in hib->piglet_va
1772 	 */
1773 	if (get_hibernate_info(&hib, 1)) {
1774 		DPRINTF("failed to obtain hibernate info\n");
1775 		return (1);
1776 	}
1777 
1778 	/* Find a page-addressed region in swap [start,end] */
1779 	if (uvm_hibswap(hib.dev, &start, &end)) {
1780 		printf("hibernate: cannot find any swap\n");
1781 		return (1);
1782 	}
1783 
1784 	if (end - start < 1000) {
1785 		printf("hibernate: insufficient swap (%lu is too small)\n",
1786 			end - start);
1787 		return (1);
1788 	}
1789 
1790 	/* Calculate block offsets in swap */
1791 	hib.image_offset = ctod(start);
1792 
1793 	DPRINTF("hibernate @ block %lld max-length %lu blocks\n",
1794 	    hib.image_offset, ctod(end) - ctod(start));
1795 
1796 	pmap_kenter_pa(HIBERNATE_HIBALLOC_PAGE, HIBERNATE_HIBALLOC_PAGE,
1797 		VM_PROT_ALL);
1798 	pmap_activate(curproc);
1799 
1800 	DPRINTF("hibernate: writing chunks\n");
1801 	if (hibernate_write_chunks(&hib)) {
1802 		DPRINTF("hibernate_write_chunks failed\n");
1803 		return (1);
1804 	}
1805 
1806 	DPRINTF("hibernate: writing chunktable\n");
1807 	if (hibernate_write_chunktable(&hib)) {
1808 		DPRINTF("hibernate_write_chunktable failed\n");
1809 		return (1);
1810 	}
1811 
1812 	DPRINTF("hibernate: writing signature\n");
1813 	if (hibernate_write_signature(&hib)) {
1814 		DPRINTF("hibernate_write_signature failed\n");
1815 		return (1);
1816 	}
1817 
1818 	/* Allow the disk to settle */
1819 	delay(500000);
1820 
1821 	/*
1822 	 * Give the device-specific I/O function a notification that we're
1823 	 * done, and that it can clean up or shutdown as needed.
1824 	 */
1825 	hib.io_func(hib.dev, 0, (vaddr_t)NULL, 0, HIB_DONE, hib.io_page);
1826 
1827 	return (0);
1828 }
1829 
1830 int
1831 hibernate_alloc(void)
1832 {
1833 	KASSERT(global_piglet_va == 0);
1834 	KASSERT(hibernate_temp_page == 0);
1835 	KASSERT(hibernate_copy_page == 0);
1836 	KASSERT(hibernate_rle_page == 0);
1837 
1838 	if (uvm_pmr_alloc_piglet(&global_piglet_va, &global_piglet_pa,
1839 	    HIBERNATE_CHUNK_SIZE * 4, HIBERNATE_CHUNK_SIZE))
1840 		return (ENOMEM);
1841 
1842 	/*
1843 	 * Allocate VA for the temp and copy page.
1844 	 *
1845 	 * These will become part of the suspended kernel and will
1846 	 * be freed in hibernate_free, upon resume.
1847 	 */
1848 	hibernate_temp_page = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any,
1849 	    &kp_none, &kd_nowait);
1850 	if (!hibernate_temp_page) {
1851 		DPRINTF("out of memory allocating hibernate_temp_page\n");
1852 		return (ENOMEM);
1853 	}
1854 
1855 	hibernate_copy_page = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any,
1856 	    &kp_none, &kd_nowait);
1857 	if (!hibernate_copy_page) {
1858 		DPRINTF("out of memory allocating hibernate_copy_page\n");
1859 		return (ENOMEM);
1860 	}
1861 
1862 	hibernate_rle_page = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any,
1863 	    &kp_none, &kd_nowait);
1864 	if (!hibernate_rle_page) {
1865 		DPRINTF("out of memory allocating hibernate_rle_page\n");
1866 		return (ENOMEM);
1867 	}
1868 
1869 	return (0);
1870 }
1871 
1872 /*
1873  * Free items allocated by hibernate_alloc()
1874  */
1875 void
1876 hibernate_free(void)
1877 {
1878 	if (global_piglet_va)
1879 		uvm_pmr_free_piglet(global_piglet_va,
1880 		    4 * HIBERNATE_CHUNK_SIZE);
1881 
1882 	if (hibernate_copy_page)
1883 		pmap_kremove(hibernate_copy_page, PAGE_SIZE);
1884 	if (hibernate_temp_page)
1885 		pmap_kremove(hibernate_temp_page, PAGE_SIZE);
1886 	if (hibernate_rle_page)
1887 		pmap_kremove(hibernate_rle_page, PAGE_SIZE);
1888 
1889 	pmap_update(pmap_kernel());
1890 
1891 	if (hibernate_copy_page)
1892 		km_free((void *)hibernate_copy_page, PAGE_SIZE,
1893 		    &kv_any, &kp_none);
1894 	if (hibernate_temp_page)
1895 		km_free((void *)hibernate_temp_page, PAGE_SIZE,
1896 		    &kv_any, &kp_none);
1897 	if (hibernate_rle_page)
1898 		km_free((void *)hibernate_rle_page, PAGE_SIZE,
1899 		    &kv_any, &kp_none);
1900 
1901 	global_piglet_va = 0;
1902 	hibernate_copy_page = 0;
1903 	hibernate_temp_page = 0;
1904 	hibernate_rle_page = 0;
1905 }
1906