forked from Solo5/solo5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathukvm_core.c
201 lines (170 loc) · 5.16 KB
/
ukvm_core.c
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
/*
* Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file
*
* This file is part of ukvm, a unikernel monitor.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* ukvm_core.c: Core functionality.
*
* Maintains tables of modules, hypercall handlers and vmexit handlers.
* Implements core hypercall functionality which is always present.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include "ukvm.h"
struct ukvm_module ukvm_module_core;
struct ukvm_module *ukvm_core_modules[] = {
&ukvm_module_core,
#ifdef UKVM_MODULE_DUMPCORE
&ukvm_module_dumpcore,
#endif
#ifdef UKVM_MODULE_BLK
&ukvm_module_blk,
#endif
#ifdef UKVM_MODULE_NET
&ukvm_module_net,
#endif
#ifdef UKVM_MODULE_GDB
&ukvm_module_gdb,
#endif
NULL,
};
#define NUM_MODULES ((sizeof ukvm_core_modules / sizeof (struct ukvm_module *)) - 1)
ukvm_hypercall_fn_t ukvm_core_hypercalls[UKVM_HYPERCALL_MAX] = { 0 };
int ukvm_core_register_hypercall(int nr, ukvm_hypercall_fn_t fn)
{
if (nr >= UKVM_HYPERCALL_MAX)
return -1;
if (ukvm_core_hypercalls[nr] != NULL)
return -1;
ukvm_core_hypercalls[nr] = fn;
return 0;
}
#define UKVM_HALT_HOOKS_MAX 8
ukvm_halt_fn_t ukvm_core_halt_hooks[UKVM_HALT_HOOKS_MAX] = {0};
static int nr_halt_hooks;
int ukvm_core_register_halt_hook(ukvm_halt_fn_t fn)
{
if (nr_halt_hooks == UKVM_HALT_HOOKS_MAX)
return -1;
ukvm_core_halt_hooks[nr_halt_hooks] = fn;
nr_halt_hooks++;
return 0;
}
int ukvm_core_hypercall_halt(struct ukvm_hv *hv, ukvm_gpa_t gpa)
{
void *cookie;
int idx;
struct ukvm_halt *t =
UKVM_CHECKED_GPA_P(hv, gpa, sizeof (struct ukvm_halt));
/*
* If the guest set a non-NULL cookie (non-zero before conversion), verify
* that the memory space pointed to by it is accessible and pass it down to
* halt hooks, if any.
*/
if (t->cookie != 0)
cookie = UKVM_CHECKED_GPA_P(hv, t->cookie, UKVM_HALT_COOKIE_MAX);
else
cookie = NULL;
for (idx = 0; idx < nr_halt_hooks; idx++) {
ukvm_halt_fn_t fn = ukvm_core_halt_hooks[idx];
assert(fn != NULL);
fn(hv, t->exit_status, cookie);
}
return t->exit_status;
}
ukvm_vmexit_fn_t ukvm_core_vmexits[NUM_MODULES + 1] = { 0 };
static int nvmexits = 0;
int ukvm_core_register_vmexit(ukvm_vmexit_fn_t fn)
{
if (nvmexits == NUM_MODULES)
return -1;
ukvm_core_vmexits[nvmexits] = fn;
nvmexits++;
return 0;
}
static void hypercall_walltime(struct ukvm_hv *hv, ukvm_gpa_t gpa)
{
struct ukvm_walltime *t =
UKVM_CHECKED_GPA_P(hv, gpa, sizeof (struct ukvm_walltime));
struct timespec ts;
int rc = clock_gettime(CLOCK_REALTIME, &ts);
assert(rc == 0);
t->nsecs = (ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
}
static void hypercall_puts(struct ukvm_hv *hv, ukvm_gpa_t gpa)
{
struct ukvm_puts *p =
UKVM_CHECKED_GPA_P(hv, gpa, sizeof (struct ukvm_puts));
int rc = write(1, UKVM_CHECKED_GPA_P(hv, p->data, p->len), p->len);
assert(rc >= 0);
}
static struct pollfd pollfds[NUM_MODULES];
static int npollfds = 0;
static sigset_t pollsigmask;
int ukvm_core_register_pollfd(int fd)
{
if (npollfds == NUM_MODULES)
return -1;
pollfds[npollfds].fd = fd;
pollfds[npollfds].events = POLLIN;
npollfds++;
return 0;
}
static void hypercall_poll(struct ukvm_hv *hv, ukvm_gpa_t gpa)
{
struct ukvm_poll *t =
UKVM_CHECKED_GPA_P(hv, gpa, sizeof (struct ukvm_poll));
struct timespec ts;
int rc;
ts.tv_sec = t->timeout_nsecs / 1000000000ULL;
ts.tv_nsec = t->timeout_nsecs % 1000000000ULL;
rc = ppoll(pollfds, npollfds, &ts, &pollsigmask);
assert(rc >= 0);
t->ret = rc;
}
static int setup(struct ukvm_hv *hv)
{
assert(ukvm_core_register_hypercall(UKVM_HYPERCALL_WALLTIME,
hypercall_walltime) == 0);
assert(ukvm_core_register_hypercall(UKVM_HYPERCALL_PUTS,
hypercall_puts) == 0);
assert(ukvm_core_register_hypercall(UKVM_HYPERCALL_POLL,
hypercall_poll) == 0);
/*
* XXX: This needs documenting / coordination with the top-level caller.
*/
sigfillset(&pollsigmask);
sigdelset(&pollsigmask, SIGTERM);
sigdelset(&pollsigmask, SIGINT);
return 0;
}
struct ukvm_module ukvm_module_core = {
.name = "core",
.setup = setup
};