1 /******************************************************************************
2  * kexec.h - Public portion
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  * Xen port written by:
23  * - Simon 'Horms' Horman <horms@verge.net.au>
24  * - Magnus Damm <magnus@valinux.co.jp>
25  */
26 
27 #ifndef _XEN_PUBLIC_KEXEC_H
28 #define _XEN_PUBLIC_KEXEC_H
29 
30 
31 /* This file describes the Kexec / Kdump hypercall interface for Xen.
32  *
33  * Kexec under vanilla Linux allows a user to reboot the physical machine
34  * into a new user-specified kernel. The Xen port extends this idea
35  * to allow rebooting of the machine from dom0. When kexec for dom0
36  * is used to reboot,  both the hypervisor and the domains get replaced
37  * with some other kernel. It is possible to kexec between vanilla
38  * Linux and Xen and back again. Xen to Xen works well too.
39  *
40  * The hypercall interface for kexec can be divided into three main
41  * types of hypercall operations:
42  *
43  * 1) Range information:
44  *    This is used by the dom0 kernel to ask the hypervisor about various
45  *    address information. This information is needed to allow kexec-tools
46  *    to fill in the ELF headers for /proc/vmcore properly.
47  *
48  * 2) Load and unload of images:
49  *    There are no big surprises here, the kexec binary from kexec-tools
50  *    runs in userspace in dom0. The tool loads/unloads data into the
51  *    dom0 kernel such as new kernel, initramfs and hypervisor. When
52  *    loaded the dom0 kernel performs a load hypercall operation, and
53  *    before releasing all page references the dom0 kernel calls unload.
54  *
55  * 3) Kexec operation:
56  *    This is used to start a previously loaded kernel.
57  */
58 
59 #include "xen.h"
60 
61 #if defined(__i386__) || defined(__x86_64__)
62 #define KEXEC_XEN_NO_PAGES 17
63 #endif
64 
65 /*
66  * Prototype for this hypercall is:
67  *  int kexec_op(int cmd, void *args)
68  * @cmd  == KEXEC_CMD_...
69  *          KEXEC operation to perform
70  * @args == Operation-specific extra arguments (NULL if none).
71  */
72 
73 /*
74  * Kexec supports two types of operation:
75  * - kexec into a regular kernel, very similar to a standard reboot
76  *   - KEXEC_TYPE_DEFAULT is used to specify this type
77  * - kexec into a special "crash kernel", aka kexec-on-panic
78  *   - KEXEC_TYPE_CRASH is used to specify this type
79  *   - parts of our system may be broken at kexec-on-panic time
80  *     - the code should be kept as simple and self-contained as possible
81  */
82 
83 #define KEXEC_TYPE_DEFAULT 0
84 #define KEXEC_TYPE_CRASH   1
85 
86 
87 /* The kexec implementation for Xen allows the user to load two
88  * types of kernels, KEXEC_TYPE_DEFAULT and KEXEC_TYPE_CRASH.
89  * All data needed for a kexec reboot is kept in one xen_kexec_image_t
90  * per "instance". The data mainly consists of machine address lists to pages
91  * together with destination addresses. The data in xen_kexec_image_t
92  * is passed to the "code page" which is one page of code that performs
93  * the final relocations before jumping to the new kernel.
94  */
95 
96 typedef struct xen_kexec_image {
97 #if defined(__i386__) || defined(__x86_64__)
98     unsigned long page_list[KEXEC_XEN_NO_PAGES];
99 #endif
100     unsigned long indirection_page;
101     unsigned long start_address;
102 } xen_kexec_image_t;
103 
104 /*
105  * Perform kexec having previously loaded a kexec or kdump kernel
106  * as appropriate.
107  * type == KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH [in]
108  *
109  * Control is transferred to the image entry point with the host in
110  * the following state.
111  *
112  * - The image may be executed on any PCPU and all other PCPUs are
113  *   stopped.
114  *
115  * - Local interrupts are disabled.
116  *
117  * - Register values are undefined.
118  *
119  * - The image segments have writeable 1:1 virtual to machine
120  *   mappings.  The location of any page tables is undefined and these
121  *   page table frames are not be mapped.
122  */
123 #define KEXEC_CMD_kexec                 0
124 typedef struct xen_kexec_exec {
125     int type;
126 } xen_kexec_exec_t;
127 
128 /*
129  * Load/Unload kernel image for kexec or kdump.
130  * type  == KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH [in]
131  * image == relocation information for kexec (ignored for unload) [in]
132  */
133 #define KEXEC_CMD_kexec_load_v1         1 /* obsolete since 0x00040400 */
134 #define KEXEC_CMD_kexec_unload_v1       2 /* obsolete since 0x00040400 */
135 typedef struct xen_kexec_load_v1 {
136     int type;
137     xen_kexec_image_t image;
138 } xen_kexec_load_v1_t;
139 
140 #define KEXEC_RANGE_MA_CRASH      0 /* machine address and size of crash area */
141 #define KEXEC_RANGE_MA_XEN        1 /* machine address and size of Xen itself */
142 #define KEXEC_RANGE_MA_CPU        2 /* machine address and size of a CPU note */
143 #define KEXEC_RANGE_MA_XENHEAP    3 /* machine address and size of xenheap
144                                      * Note that although this is adjacent
145                                      * to Xen it exists in a separate EFI
146                                      * region on ia64, and thus needs to be
147                                      * inserted into iomem_machine separately */
148 #define KEXEC_RANGE_MA_BOOT_PARAM 4 /* Obsolete: machine address and size of
149                                      * the ia64_boot_param */
150 #define KEXEC_RANGE_MA_EFI_MEMMAP 5 /* machine address and size of
151                                      * of the EFI Memory Map */
152 #define KEXEC_RANGE_MA_VMCOREINFO 6 /* machine address and size of vmcoreinfo */
153 
154 /*
155  * Find the address and size of certain memory areas
156  * range == KEXEC_RANGE_... [in]
157  * nr    == physical CPU number (starting from 0) if KEXEC_RANGE_MA_CPU [in]
158  * size  == number of bytes reserved in window [out]
159  * start == address of the first byte in the window [out]
160  */
161 #define KEXEC_CMD_kexec_get_range       3
162 typedef struct xen_kexec_range {
163     int range;
164     int nr;
165     unsigned long size;
166     unsigned long start;
167 } xen_kexec_range_t;
168 
169 #if __XEN_INTERFACE_VERSION__ >= 0x00040400
170 /*
171  * A contiguous chunk of a kexec image and it's destination machine
172  * address.
173  */
174 typedef struct xen_kexec_segment {
175     union {
176         XEN_GUEST_HANDLE(const_void) h;
177         uint64_t _pad;
178     } buf;
179     uint64_t buf_size;
180     uint64_t dest_maddr;
181     uint64_t dest_size;
182 } xen_kexec_segment_t;
183 DEFINE_XEN_GUEST_HANDLE(xen_kexec_segment_t);
184 
185 /*
186  * Load a kexec image into memory.
187  *
188  * For KEXEC_TYPE_DEFAULT images, the segments may be anywhere in RAM.
189  * The image is relocated prior to being executed.
190  *
191  * For KEXEC_TYPE_CRASH images, each segment of the image must reside
192  * in the memory region reserved for kexec (KEXEC_RANGE_MA_CRASH) and
193  * the entry point must be within the image. The caller is responsible
194  * for ensuring that multiple images do not overlap.
195  *
196  * All image segments will be loaded to their destination machine
197  * addresses prior to being executed.  The trailing portion of any
198  * segments with a source buffer (from dest_maddr + buf_size to
199  * dest_maddr + dest_size) will be zeroed.
200  *
201  * Segments with no source buffer will be accessible to the image when
202  * it is executed.
203  */
204 
205 #define KEXEC_CMD_kexec_load 4
206 typedef struct xen_kexec_load {
207     uint8_t  type;        /* One of KEXEC_TYPE_* */
208     uint8_t  _pad;
209     uint16_t arch;        /* ELF machine type (EM_*). */
210     uint32_t nr_segments;
211     union {
212         XEN_GUEST_HANDLE(xen_kexec_segment_t) h;
213         uint64_t _pad;
214     } segments;
215     uint64_t entry_maddr; /* image entry point machine address. */
216 } xen_kexec_load_t;
217 DEFINE_XEN_GUEST_HANDLE(xen_kexec_load_t);
218 
219 /*
220  * Unload a kexec image.
221  *
222  * Type must be one of KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH.
223  */
224 #define KEXEC_CMD_kexec_unload 5
225 typedef struct xen_kexec_unload {
226     uint8_t type;
227 } xen_kexec_unload_t;
228 DEFINE_XEN_GUEST_HANDLE(xen_kexec_unload_t);
229 
230 /*
231  * Figure out whether we have an image loaded. A return value of
232  * zero indicates no image loaded. A return value of one
233  * indicates an image is loaded. A negative return value
234  * indicates an error.
235  *
236  * Type must be one of KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH.
237  */
238 #define KEXEC_CMD_kexec_status 6
239 typedef struct xen_kexec_status {
240     uint8_t type;
241 } xen_kexec_status_t;
242 DEFINE_XEN_GUEST_HANDLE(xen_kexec_status_t);
243 
244 #else /* __XEN_INTERFACE_VERSION__ < 0x00040400 */
245 
246 #define KEXEC_CMD_kexec_load KEXEC_CMD_kexec_load_v1
247 #define KEXEC_CMD_kexec_unload KEXEC_CMD_kexec_unload_v1
248 #define xen_kexec_load xen_kexec_load_v1
249 #define xen_kexec_load_t xen_kexec_load_v1_t
250 
251 #endif
252 
253 #endif /* _XEN_PUBLIC_KEXEC_H */
254 
255 /*
256  * Local variables:
257  * mode: C
258  * c-file-style: "BSD"
259  * c-basic-offset: 4
260  * tab-width: 4
261  * indent-tabs-mode: nil
262  * End:
263  */
264