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