xref: /openbsd-src/sys/sys/hibernate.h (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
1 /*	$OpenBSD: hibernate.h,v 1.22 2012/07/08 12:22:26 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 struct hiballoc_entry;
32 
33 /*
34  * Allocator operates from an arena, that is pre-allocated by the caller.
35  */
36 struct hiballoc_arena {
37 	RB_HEAD(hiballoc_addr, hiballoc_entry)	hib_addrs;
38 };
39 
40 /*
41  * Describes a zlib compression stream and its associated hiballoc area
42  */
43 struct hibernate_zlib_state {
44         z_stream hib_stream;
45         struct hiballoc_arena hiballoc_arena;
46 };
47 
48 /*
49  * Describes a range of physical memory on the machine
50  */
51 struct hibernate_memory_range {
52 	paddr_t		base;
53 	paddr_t		end;
54 };
55 
56 /*
57  * Describes a hibernate chunk structure, used when splitting the memory
58  * image of the machine into easy-to-manage pieces.
59  */
60 struct hibernate_disk_chunk {
61 	paddr_t		base;		/* Base of chunk */
62 	paddr_t		end;		/* End of chunk */
63 	daddr_t		offset;		/* Abs. disk block locating chunk */
64 	size_t		compressed_size; /* Compressed size on disk */
65 	short		flags;		/* Flags */
66 };
67 
68 #define HIB_INIT	-1
69 #define HIB_R		0
70 #define HIB_W		1
71 typedef	int (*hibio_fn)(dev_t, daddr_t, vaddr_t, size_t, int, void *);
72 
73 /*
74  * Used to store information about the hibernation state of the machine,
75  * such as memory range count and extents, disk sector size, and various
76  * offsets where things are located on disk.
77  */
78 union hibernate_info {
79 	struct {
80 		size_t				nranges;
81 		struct hibernate_memory_range	ranges[VM_PHYSSEG_MAX];
82 		size_t				image_size;
83 		size_t				chunk_ctr;
84 		u_int32_t			secsize;
85 		dev_t				device;
86 		daddr_t				swap_offset;
87 		daddr_t				sig_offset;
88 		daddr_t				image_offset;
89 		paddr_t				piglet_pa;
90 		vaddr_t				piglet_va;
91 		char				kernel_version[128];
92 		hibio_fn			io_func;
93 		void				*io_page;
94 	};
95 
96 	/* XXX - remove restriction to have this union fit in a single block */
97 	char pad[512]; /* Pad to 512 bytes */
98 };
99 
100 void	*hib_alloc(struct hiballoc_arena*, size_t);
101 void	 hib_free(struct hiballoc_arena*, void*);
102 int	 hiballoc_init(struct hiballoc_arena*, void*, size_t len);
103 void	 uvm_pmr_zero_everything(void);
104 void	 uvm_pmr_dirty_everything(void);
105 int	 uvm_pmr_alloc_pig(paddr_t*, psize_t);
106 int	 uvm_pmr_alloc_piglet(vaddr_t*, paddr_t*, vsize_t, paddr_t);
107 void	 uvm_pmr_free_piglet(vaddr_t, vsize_t);
108 int	 uvm_page_rle(paddr_t);
109 
110 hibio_fn get_hibernate_io_function(void);
111 int	get_hibernate_info(union hibernate_info *, int);
112 
113 int	hibernate_zlib_reset(union hibernate_info *, int);
114 void	*hibernate_zlib_alloc(void *, int, int);
115 void	hibernate_zlib_free(void *, void *);
116 void	hibernate_inflate_region(union hibernate_info *, paddr_t, paddr_t,
117 	    size_t);
118 size_t	hibernate_deflate(union hibernate_info *, paddr_t, size_t *);
119 void	hibernate_process_chunk(union hibernate_info *,
120 	    struct hibernate_disk_chunk *, paddr_t);
121 int	hibernate_get_next_rle(void);
122 int	hibernate_inflate_page(void);
123 
124 int	hibernate_block_io(union hibernate_info *, daddr_t, size_t, vaddr_t, int);
125 int	hibernate_write_signature(union hibernate_info *);
126 int	hibernate_write_chunktable(union hibernate_info *);
127 int	hibernate_write_chunks(union hibernate_info *);
128 int	hibernate_clear_signature(void);
129 int	hibernate_compare_signature(union hibernate_info *,
130 	    union hibernate_info *);
131 void	hibernate_resume(void);
132 int	hibernate_suspend(void);
133 int	hibernate_read_image(union hibernate_info *);
134 int	hibernate_read_chunks(union hibernate_info *, paddr_t, paddr_t, size_t,
135 	    struct hibernate_disk_chunk *);
136 void	hibernate_unpack_image(union hibernate_info *);
137 void	hibernate_populate_resume_pt(union hibernate_info *, paddr_t, paddr_t);
138 void	hibernate_free(void);
139 
140 int	hibernate_check_overlap(paddr_t, paddr_t, paddr_t, paddr_t);
141 
142 #endif /* _SYS_HIBERNATE_H_ */
143