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