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