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