1 /* $OpenBSD: hibernate.h,v 1.42 2018/06/21 07:33:30 mlarkin Exp $ */ 2 3 /* 4 * Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #ifndef _SYS_HIBERNATE_H_ 20 #define _SYS_HIBERNATE_H_ 21 22 #include <sys/types.h> 23 #include <sys/tree.h> 24 #include <lib/libz/zlib.h> 25 #include <machine/vmparam.h> 26 27 #define HIBERNATE_CHUNK_USED 1 28 #define HIBERNATE_CHUNK_CONFLICT 2 29 #define HIBERNATE_CHUNK_PLACED 4 30 31 /* Magic number used to indicate hibernate signature block */ 32 #define HIBERNATE_MAGIC 0x0B5D0B5D 33 34 /* Page skip operations used during unpack */ 35 #define HIB_MOVE 2 36 #define HIB_SKIP 1 37 38 struct hiballoc_entry; 39 40 /* 41 * Allocator operates from an arena, that is pre-allocated by the caller. 42 */ 43 struct hiballoc_arena { 44 RBT_HEAD(hiballoc_addr, hiballoc_entry) hib_addrs; 45 }; 46 47 /* 48 * Describes a zlib compression stream and its associated hiballoc area 49 */ 50 struct hibernate_zlib_state { 51 z_stream hib_stream; 52 struct hiballoc_arena hiballoc_arena; 53 }; 54 55 /* 56 * Describes a range of physical memory on the machine 57 */ 58 struct hibernate_memory_range { 59 paddr_t base; 60 paddr_t end; 61 }; 62 63 /* 64 * Describes a hibernate chunk structure, used when splitting the memory 65 * image of the machine into easy-to-manage pieces. 66 */ 67 struct hibernate_disk_chunk { 68 paddr_t base; /* Base of chunk */ 69 paddr_t end; /* End of chunk */ 70 daddr_t offset; /* Abs. disk block locating chunk */ 71 size_t compressed_size; /* Compressed size on disk */ 72 short flags; /* Flags */ 73 }; 74 75 #define HIB_INIT -1 76 #define HIB_DONE -2 77 #define HIB_R 0 78 #define HIB_W 1 79 typedef int (*hibio_fn)(dev_t, daddr_t, vaddr_t, size_t, int, void *); 80 81 /* 82 * Used to store information about the hibernation state of the machine, 83 * such as memory range count and extents, disk sector size, and various 84 * offsets where things are located on disk. 85 */ 86 union hibernate_info { 87 struct { 88 u_int32_t magic; 89 size_t nranges; 90 struct hibernate_memory_range ranges[VM_PHYSSEG_MAX]; 91 size_t image_size; 92 size_t chunk_ctr; 93 dev_t dev; 94 daddr_t sig_offset; 95 daddr_t chunktable_offset; 96 daddr_t image_offset; 97 paddr_t piglet_pa; 98 vaddr_t piglet_va; 99 char kernel_version[128]; 100 u_int32_t kernel_sum; 101 hibio_fn io_func; 102 void *io_page; 103 #ifndef NO_PROPOLICE 104 long guard; 105 #endif /* ! NO_PROPOLICE */ 106 u_int32_t retguard_ofs; 107 }; 108 109 /* XXX - remove restriction to have this union fit in a single block */ 110 char pad[512]; /* Pad to 512 bytes */ 111 }; 112 113 void *hib_alloc(struct hiballoc_arena*, size_t); 114 void hib_free(struct hiballoc_arena*, void*); 115 int hiballoc_init(struct hiballoc_arena*, void*, size_t len); 116 void uvm_pmr_zero_everything(void); 117 void uvm_pmr_dirty_everything(void); 118 int uvm_pmr_alloc_pig(paddr_t*, psize_t, paddr_t); 119 int uvm_pmr_alloc_piglet(vaddr_t*, paddr_t*, vsize_t, paddr_t); 120 void uvm_pmr_free_piglet(vaddr_t, vsize_t); 121 int uvm_page_rle(paddr_t); 122 void uvmpd_hibernate(void); 123 124 hibio_fn get_hibernate_io_function(dev_t); 125 int get_hibernate_info(union hibernate_info *, int); 126 127 int hibernate_zlib_reset(union hibernate_info *, int); 128 void *hibernate_zlib_alloc(void *, int, int); 129 void hibernate_zlib_free(void *, void *); 130 void hibernate_inflate_region(union hibernate_info *, paddr_t, paddr_t, 131 size_t); 132 size_t hibernate_deflate(union hibernate_info *, paddr_t, size_t *); 133 void hibernate_process_chunk(union hibernate_info *, 134 struct hibernate_disk_chunk *, paddr_t); 135 int hibernate_inflate_page(int *); 136 137 int hibernate_block_io(union hibernate_info *, daddr_t, size_t, vaddr_t, int); 138 int hibernate_write_signature(union hibernate_info *); 139 int hibernate_write_chunktable(union hibernate_info *); 140 int hibernate_write_chunks(union hibernate_info *); 141 int hibernate_clear_signature(void); 142 int hibernate_compare_signature(union hibernate_info *, 143 union hibernate_info *); 144 void hibernate_resume(void); 145 int hibernate_suspend(void); 146 int hibernate_read_image(union hibernate_info *); 147 int hibernate_read_chunks(union hibernate_info *, paddr_t, paddr_t, size_t, 148 struct hibernate_disk_chunk *); 149 void hibernate_unpack_image(union hibernate_info *); 150 void hibernate_populate_resume_pt(union hibernate_info *, paddr_t, paddr_t); 151 int hibernate_alloc(void); 152 void hibernate_free(void); 153 void hib_getentropy(char **, size_t *); 154 155 void hibernate_sort_ranges(union hibernate_info *); 156 void hibernate_suspend_bufcache(void); 157 void hibernate_resume_bufcache(void); 158 159 #endif /* _SYS_HIBERNATE_H_ */ 160