summaryrefslogtreecommitdiff
path: root/kexec/kexec-xen.c
blob: d42a45a882b923d0da5ff61de177adc728760d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <elf.h>
#include "kexec.h"
#include "kexec-syscall.h"
#include "crashdump.h"

#include "config.h"

#ifdef HAVE_LIBXENCTRL
#include "kexec-xen.h"

#include "crashdump.h"

#ifdef CONFIG_LIBXENCTRL_DL
void *xc_dlhandle;
xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(HYPERCALL_BUFFER_NULL);
xc_interface *__xc_interface_open(xentoollog_logger *logger,
				  xentoollog_logger *dombuild_logger,
				  unsigned open_flags)
{
	xc_interface *xch = NULL;

	if (!xc_dlhandle)
		xc_dlhandle = dlopen("libxenctrl.so", RTLD_NOW | RTLD_NODELETE);

	if (xc_dlhandle) {
		typedef xc_interface *(*func_t)(xentoollog_logger *logger,
			xentoollog_logger *dombuild_logger,
			unsigned open_flags);

		func_t func = (func_t)dlsym(xc_dlhandle, "xc_interface_open");
		xch = func(logger, dombuild_logger, open_flags);
	}

	return xch;
}

int __xc_interface_close(xc_interface *xch)
{
	int rc = -1;

	if (xc_dlhandle) {
		typedef int (*func_t)(xc_interface *xch);

		func_t func = (func_t)dlsym(xc_dlhandle, "xc_interface_close");
		rc = func(xch);
		xc_dlhandle = NULL;
	}

	return rc;
}
#endif /* CONFIG_LIBXENCTRL_DL */

int xen_kexec_load(struct kexec_info *info)
{
	uint32_t nr_segments = info->nr_segments;
	struct kexec_segment *segments = info->segment;
	xc_interface *xch;
	xc_hypercall_buffer_array_t *array = NULL;
	uint8_t type;
	uint8_t arch;
	xen_kexec_segment_t *xen_segs;
	int s;
	int ret = -1;

	xch = xc_interface_open(NULL, NULL, 0);
	if (!xch)
		return -1;

	xen_segs = calloc(nr_segments + 1, sizeof(*xen_segs));
	if (!xen_segs)
		goto out;

	array = xc_hypercall_buffer_array_create(xch, nr_segments);
	if (array == NULL)
		goto out;

	for (s = 0; s < nr_segments; s++) {
		DECLARE_HYPERCALL_BUFFER(void, seg_buf);

		seg_buf = xc_hypercall_buffer_array_alloc(xch, array, s,
							  seg_buf, segments[s].bufsz);
		if (seg_buf == NULL)
			goto out;
		memcpy(seg_buf, segments[s].buf, segments[s].bufsz);

		set_xen_guest_handle(xen_segs[s].buf.h, seg_buf);
		xen_segs[s].buf_size = segments[s].bufsz;
		xen_segs[s].dest_maddr = (uint64_t)segments[s].mem;
		xen_segs[s].dest_size = segments[s].memsz;
	}

	/*
	 * Ensure 0 - 1 MiB is mapped and accessible by the image.
	 *
	 * This allows access to the VGA memory and the region
	 * purgatory copies in the crash case.
	 */
	set_xen_guest_handle(xen_segs[s].buf.h, HYPERCALL_BUFFER_NULL);
	xen_segs[s].buf_size = 0;
	xen_segs[s].dest_maddr = 0;
	xen_segs[s].dest_size = 1 * 1024 * 1024;
	nr_segments++;

	type = (info->kexec_flags & KEXEC_ON_CRASH) ? KEXEC_TYPE_CRASH
		: KEXEC_TYPE_DEFAULT;

	arch = (info->kexec_flags & KEXEC_ARCH_MASK) >> 16;
#if defined(__i386__) || defined(__x86_64__)
	if (!arch)
		arch = EM_386;
#endif

	ret = xc_kexec_load(xch, type, arch, (uint64_t)info->entry,
			    nr_segments, xen_segs);

out:
	xc_hypercall_buffer_array_destroy(xch, array);
	free(xen_segs);
	xc_interface_close(xch);

	return ret;
}

int xen_kexec_unload(uint64_t kexec_flags)
{
	xc_interface *xch;
	uint8_t type;
	int ret;

	xch = xc_interface_open(NULL, NULL, 0);
	if (!xch)
		return -1;

	type = (kexec_flags & KEXEC_ON_CRASH) ? KEXEC_TYPE_CRASH
		: KEXEC_TYPE_DEFAULT;

	ret = xc_kexec_unload(xch, type);

	xc_interface_close(xch);

	return ret;
}

int xen_kexec_status(uint64_t kexec_flags)
{
	xc_interface *xch;
	uint8_t type;
	int ret = -1;

#ifdef HAVE_KEXEC_CMD_STATUS
	xch = xc_interface_open(NULL, NULL, 0);
	if (!xch)
		return -1;

	type = (kexec_flags & KEXEC_ON_CRASH) ? KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;

	ret = xc_kexec_status(xch, type);

	xc_interface_close(xch);
#endif

	return ret;
}

void xen_kexec_exec(void)
{
	xc_interface *xch;
	
	xch = xc_interface_open(NULL, NULL, 0);
	if (!xch)
		return;

	xc_kexec_exec(xch, KEXEC_TYPE_DEFAULT);

	xc_interface_close(xch);
}

#else /* ! HAVE_LIBXENCTRL */

int xen_kexec_load(struct kexec_info *UNUSED(info))
{
	return -1;
}

int xen_kexec_unload(uint64_t kexec_flags)
{
	return -1;
}

int xen_kexec_status(uint64_t kexec_flags)
{
	return -1;
}

void xen_kexec_exec(void)
{
}

#endif