/
kmod.c
215 lines (181 loc) · 5.68 KB
/
kmod.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*-
* Copyright (c) 2012 Gabor Pali
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <sys/errno.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/sched.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include "caml/mlvalues.h"
#include "caml/callback.h"
#include "caml/memory.h"
CAMLprim value caml_block_kernel(value v_timeout);
static char mir_rtparams[64] = "";
static int mir_debug = 3;
#define MIR_DEBUG(l, x) \
do { \
if (mir_debug >= l) { \
(x); \
} \
} while (0)
static char* argv[] = { "mirage", NULL };
MALLOC_DEFINE(M_MIRAGE, "mirage", "Mirage run-time");
static SYSCTL_NODE(_kern, OID_AUTO, mirage, CTLFLAG_RD, NULL, "mirage");
static int mirage_kthread_done = 0;
static struct thread *mirage_kthread = NULL;
static void
mirage_kthread_body(void *arg __unused)
{
value *v_main;
int caml_completed = 0;
MIR_DEBUG(1, printf("--> mirage_kthread_body()\n"));
MIR_DEBUG(2, printf("mirage: kernel thread actived, caml run-time starts\n"));
caml_startup(argv);
MIR_DEBUG(2, printf("mirage: caml run-time finished, searching the main function\n"));
v_main = caml_named_value("OS.Main.run");
if (v_main == NULL) {
printf("[MIRAGE] Function 'OS.Main.run' could not be found.\n");
goto done;
}
MIR_DEBUG(2, printf("mirage: main function found, kicking off the main loop\n"));
mirage_kthread_done = 0;
for (; (caml_completed == 0) && (mirage_kthread_done == 0);) {
caml_completed = Bool_val(caml_callback(*v_main, Val_unit));
}
MIR_DEBUG(2, printf("mirage: main loop exited = (%d,%d)\n",
caml_completed, mirage_kthread_done));
mirage_kthread_done = 1;
done:
MIR_DEBUG(2, printf("mirage: kernel thread exiting\n"));
kthread_exit();
MIR_DEBUG(1, printf("<-- mirage_kthread_body()\n"));
}
static int
mirage_kthread_init(void)
{
int error;
error = 0;
MIR_DEBUG(1, printf("--> mirage_kthread_init()\n"));
error = kthread_add(mirage_kthread_body, NULL, NULL, &mirage_kthread,
RFSTOPPED, 40, "mirage");
if (error != 0) {
printf("[MIRAGE] Could not create herding kernel thread.\n");
goto done;
}
done:
MIR_DEBUG(1, printf("<-- mirage_kthread_init()\n"));
return error;
}
static int
mirage_kthread_deinit(void)
{
MIR_DEBUG(1, printf("--> mirage_kthread_deinit()\n"));
mirage_kthread_done = 1;
MIR_DEBUG(1, printf("<-- mirage_kthread_deinit()\n"));
return 0;
}
static void
mirage_kthread_launch(void)
{
MIR_DEBUG(1, printf("--> mirage_kthread_launch()\n"));
thread_lock(mirage_kthread);
sched_add(mirage_kthread, SRQ_BORING);
sched_class(mirage_kthread, PRI_TIMESHARE);
sched_prio(mirage_kthread, PRI_MAX_IDLE);
thread_unlock(mirage_kthread);
MIR_DEBUG(1, printf("<-- mirage_kthread_launch()\n"));
}
static int
sysctl_kern_mirage_run(SYSCTL_HANDLER_ARGS)
{
int error, i;
error = sysctl_wire_old_buffer(req, sizeof(int));
if (error == 0) {
i = 0;
error = sysctl_handle_int(oidp, &i, 0, req);
}
if (error != 0 || req->newptr == NULL)
return (error);
{
if (mirage_kthread_done == 0) {
mirage_kthread_init();
mirage_kthread_launch();
}
else {
MIR_DEBUG(1, printf("mirage: a kernel thread has been already launched.\n"));
}
}
return (0);
}
SYSCTL_PROC(_kern_mirage, OID_AUTO, run, CTLTYPE_INT | CTLFLAG_RW, 0,
sizeof(int), sysctl_kern_mirage_run, "I", "start module");
SYSCTL_INT(_kern_mirage, OID_AUTO, debug, CTLFLAG_RW, &mir_debug, 0,
"debug level");
SYSCTL_STRING(_kern_mirage, OID_AUTO, rtparams, CTLFLAG_RW, &mir_rtparams,
sizeof(mir_rtparams), "parameters for the run-time");
static int
event_handler(struct module *module, int event, void *arg) {
int retval;
retval = 0;
switch (event) {
case MOD_LOAD:
printf("[MIRAGE] Kernel module is about to load.\n");
break;
case MOD_UNLOAD:
printf("[MIRAGE] Kernel module is about to unload.\n");
retval = mirage_kthread_deinit();
break;
default:
retval = EOPNOTSUPP;
break;
}
return retval;
}
static moduledata_t mirage_conf = {
"mirage"
, event_handler
, NULL
};
DECLARE_MODULE(mirage, mirage_conf, SI_SUB_KLD, SI_ORDER_ANY);
static int block_timo;
CAMLprim value
caml_block_kernel(value v_timeout)
{
CAMLparam1(v_timeout);
block_timo = Int_val(v_timeout);
MIR_DEBUG(2, printf("mirage: Blocking kernel for %d us\n", block_timo));
pause("caml_block_kernel", (block_timo / 1000000) * hz);
CAMLreturn(Val_unit);
}