-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisocore.patch
384 lines (371 loc) · 10 KB
/
isocore.patch
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
From aec7aac70ee1a7b270e671d9cb284fc07fd89bc1 Mon Sep 17 00:00:00 2001
From: Peter Dinda <[email protected]>
Date: Tue, 7 Feb 2017 19:26:35 -0600
Subject: [PATCH] First pass at an isolated core framework with basic "turn off
interrupts and go into no-fill cache mode" implementation
See comments in include/nautilus/isocore.h,
src/nautilus/isocore.c, and src/asm/isocore_lowlevel.h
---
Kconfig | 19 ++++++++++
include/nautilus/isocore.h | 29 +++++++++++++++
src/asm/Makefile | 4 ++
src/asm/isocore_lowlevel.S | 80 ++++++++++++++++++++++++++++++++++++++++
src/nautilus/Makefile | 4 +-
src/nautilus/isocore.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++
src/nautilus/shell.c | 41 ++++++++++++++++++++-
7 files changed, 266 insertions(+), 2 deletions(-)
create mode 100644 include/nautilus/isocore.h
create mode 100644 src/asm/isocore_lowlevel.S
create mode 100644 src/nautilus/isocore.c
diff --git a/Kconfig b/Kconfig
index 385cbba..65ea610 100644
--- a/Kconfig
+++ b/Kconfig
@@ -345,6 +345,7 @@ config VIRTUAL_CONSOLE_SERIAL_MIRROR_ALL
endchoice
endmenu
+
config REAL_MODE_INTERFACE
bool "Enable the Long->Real interface"
default n
@@ -368,6 +369,24 @@ config REAL_MODE_INTERFACE_SEGMENT
The first 32K is reserved for the trampoline
code and data. The second 32K can be used by
code invoking real mode interrupts
+
+
+config ISOCORE
+ bool "Enable Isolated Core Execution"
+ default n
+ help
+ Allows us to dedicate a core to the
+ execution of a function without any
+ interference from other cores
+
+config DEBUG_ISOCORE
+ bool "Debug Isolated Core Execution"
+ default n
+ depends on ISOCORE
+ help
+ Adds debugging output for isolated core execution
+ The low-level code does not have debugging output
+
endmenu
menu "AeroKernel Performance Optimizations"
diff --git a/include/nautilus/isocore.h b/include/nautilus/isocore.h
new file mode 100644
index 0000000..ce89aff
--- /dev/null
+++ b/include/nautilus/isocore.h
@@ -0,0 +1,29 @@
+#ifndef _ISOCORE
+#define _ISOCORE
+
+/*
+ Convert this core into an isolated core
+ Execution begins in a *copy* of the code
+ with a distinct stack.
+
+ Unless there is an error in startup, the
+ nk_isolate() function will never return.
+ Also, the core will never again take
+ another interrupt.
+
+ codesize+stacksize must be smaller than
+ the cache in which will be isolated
+
+ The code must be position-independent, since
+ we will relocate it. If the code touches
+ any global variable or the heap, isolation
+ bets are off.
+
+*/
+
+int nk_isolate(void (*code)(void *arg),
+ uint64_t codesize,
+ uint64_t stacksize,
+ void *arg);
+
+#endif
diff --git a/src/asm/Makefile b/src/asm/Makefile
index 28f5818..412fea6 100644
--- a/src/asm/Makefile
+++ b/src/asm/Makefile
@@ -15,3 +15,7 @@ endif
ifdef NAUT_CONFIG_REAL_MODE_INTERFACE
obj-y += realmode.o
endif
+
+ifdef NAUT_CONFIG_ISOCORE
+ obj-y += isocore_lowlevel.o
+endif
diff --git a/src/asm/isocore_lowlevel.S b/src/asm/isocore_lowlevel.S
new file mode 100644
index 0000000..2d4863a
--- /dev/null
+++ b/src/asm/isocore_lowlevel.S
@@ -0,0 +1,80 @@
+/*
+ On entry, we have:
+
+ rdi - capsule starting address
+ rsi - capsule size in bytes
+ rdx - entry point / stack start
+ rcx - argument to drop into rdi at entry
+
+ This should really be a stub that we prepend to
+ the user's code...
+*/
+
+.global _nk_isolate_entry
+_nk_isolate_entry:
+ // Interrupts off
+ cli
+ // at this point we cannot be interrupted
+ // by an interrupt or IPI, except (perhaps) INIT/SIPI
+ // We can be interrupted by an NMI or SMI
+ // If we are top of a VMM, the VMM still has full control
+
+ // MTRR manipulation would happen here
+ // I'm assuming that the capsule is already cacheable
+ // according to MTRR / PAT so skipping this
+
+ // flush cache
+ wbinvd
+ invd
+
+ // ensure cache is enabled
+ movq %cr0, %rax
+ andq $~(0x3<<29), %rax // set CD and NWT to zero
+ movq %rax, %cr0
+
+ // stash our args as we will use their regs now
+ pushq %rdi
+ pushq %rsi
+ pushq %rdx
+ pushq %rcx
+
+ // now fault the capsule into cache
+ // we assume it fits...
+ // also capsule must be a nonzero-multiple of 8 bytes long
+ // note this read loop is also burning through icache...
+ // which looks mighty suspicious
+read_loop:
+ movq (%rdi), %rax // read and throw away
+ addq $8, %rdi
+ subq $8, %rsi
+ jne read_loop
+
+ // Now the capsule should be in the cache
+
+ // turn off cache (put into no-fill mode)
+ orq $(0x3<<29), %rax // set CD and NWT to one
+ movq %rax, %cr0
+
+ // restore to state at call
+ popq %rcx
+ popq %rdx
+ popq %rsi
+ popq %rdi
+
+ // set up our argument
+ movq %rcx, %rdi
+
+ // switch to our isolated stack
+ movq %rdx, %rsp
+
+ // jump into our isolated code
+ jmp *%rdx
+
+ // we should never return from that....
+
+ movq $-1, %rax
+
+ retq
+
+
+
diff --git a/src/nautilus/Makefile b/src/nautilus/Makefile
index 318052d..6605c54 100644
--- a/src/nautilus/Makefile
+++ b/src/nautilus/Makefile
@@ -40,7 +40,7 @@ obj-y += \
netdev.o \
fs.o \
shell.o \
- fprintk.o
+ fprintk.o \
obj-$(NAUT_CONFIG_PROFILE) += instrument.o
obj-$(NAUT_CONFIG_XEON_PHI) += sfi.o
@@ -49,4 +49,6 @@ obj-$(NAUT_CONFIG_PALACIOS) += vmm.o
obj-$(NAUT_CONFIG_REAL_MODE_INTERFACE) += realmode.o
+obj-$(NAUT_CONFIG_ISOCORE) += isocore.o
+
diff --git a/src/nautilus/isocore.c b/src/nautilus/isocore.c
new file mode 100644
index 0000000..51b1ea9
--- /dev/null
+++ b/src/nautilus/isocore.c
@@ -0,0 +1,91 @@
+/*
+ */
+
+#include <nautilus/nautilus.h>
+
+#ifndef NAUT_CONFIG_DEBUG_ISOCORE
+#undef DEBUG_PRINT
+#define DEBUG_PRINT(fmt, args...)
+#endif
+
+#define ERROR(fmt, args...) ERROR_PRINT("isocore: " fmt, ##args)
+#define DEBUG(fmt, args...) DEBUG_PRINT("isocore: " fmt, ##args)
+#define INFO(fmt, args...) INFO_PRINT("isocore: " fmt, ##args)
+
+#define FLOOR_DIV(x,y) ((x)/(y))
+#define CEIL_DIV(x,y) (((x)/(y)) + !!((x)%(y)))
+#define DIVIDES(x,y) (((x)%(y))==0)
+#define MAX(x,y) ((x)>(y) ? (x) : (y))
+#define MIN(x,y) ((x)<(y) ? (x) : (y))
+
+int nk_isolate(void (*code)(void *arg),
+ uint64_t codesize,
+ uint64_t stacksize,
+ void *arg)
+{
+
+ //DEBUG("nk_isolate(code=%p, codesize=%lu\n",code, codesize);
+ //DEBUG(" stacksize=%lu, arg=%p\n", stacksize, arg);
+
+ // build a code+stack segment that looks like this:
+ //
+ // CODE
+ // ----- <- page boundary
+ // STACK
+ //
+ // both CODE and STACK are an integral number of
+ // pages long
+ //
+ // Note that we don't really need page alignment for
+ // this - I'm just doing it for now to make sure
+ // we have cache line alignment for everything, regardless of machine
+
+ uint64_t code_pages = CEIL_DIV(codesize,PAGE_SIZE_4KB);
+ uint64_t stack_pages = CEIL_DIV(stacksize,PAGE_SIZE_4KB);
+ uint64_t total_pages = code_pages+stack_pages;
+
+ DEBUG("Allocating %lu code pages and %lu stack pages\n",
+ code_pages,stack_pages);
+
+ // malloc will align to next power of 2 pages...
+ void *capsule = malloc(total_pages*PAGE_SIZE_4KB);
+
+ if (!capsule) {
+ ERROR("Unable to allocate capsule\n");
+ return -1;
+ }
+
+ DEBUG("Capsule allocated at %p\n",capsule);
+
+ // clear code and stack of the capsule
+ memset(capsule,0,total_pages*PAGE_SIZE_4KB);
+
+ // copy the code into the capsule
+ memcpy(capsule+stack_pages*PAGE_SIZE_4KB,
+ code,
+ codesize);
+
+ //nk_dump_mem(capsule+stack_pages*PAGE_SIZE_4KB, codesize);
+
+ // now transfer to the low-level code to
+ // effect isolation
+
+ extern int _nk_isolate_entry(void *, // where capsule begins
+ uint64_t, // size of capsule
+ void *, // entry point/stack start
+ void *); // what goes into rdi
+
+ DEBUG("Launching low-level capsule code, capsule=%p, size=%lu, entry=%p, rdi=%p\n", capsule, total_pages*PAGE_SIZE_4KB, capsule+stack_pages*PAGE_SIZE_4KB, arg);
+
+ _nk_isolate_entry(capsule,
+ total_pages*PAGE_SIZE_4KB,
+ capsule+stack_pages*PAGE_SIZE_4KB,
+ arg);
+
+ // this should never return
+
+ ERROR("The impossible has happened - _nk_isolate_entry returned!\n");
+ return -1;
+}
+
+
diff --git a/src/nautilus/shell.c b/src/nautilus/shell.c
index c169133..606f5d2 100644
--- a/src/nautilus/shell.c
+++ b/src/nautilus/shell.c
@@ -42,6 +42,10 @@
#include <nautilus/realmode.h>
#endif
+#ifdef NAUT_CONFIG_ISOCORE
+#include <nautilus/isocore.h>
+#endif
+
#define MAX_CMD 80
struct burner_args {
@@ -474,6 +478,33 @@ static int handle_benchmarks(char * buf)
return 0;
}
+#ifdef NAUT_CONFIG_ISOCORE
+
+static void isotest(void *arg)
+{
+ // note trying to do anything in here with NK
+ // features, even a print, is unlikely to work due to
+ // relocation, interrupts off, etc.
+ // serial_putchar('H'); serial_putchar('I');
+ while (1) { } // does actually get here in testing
+}
+
+static int handle_isotest(char *buf)
+{
+ void (*code)(void*) = isotest;
+ uint64_t codesize = PAGE_SIZE_4KB; // we are making pretend here
+ uint64_t stacksize = PAGE_SIZE_4KB;
+ void *arg = (void*)0xdeadbeef;
+
+ return nk_isolate(code,
+ codesize,
+ stacksize,
+ arg);
+}
+
+
+#endif
+
static int handle_cmd(char *buf, int n)
{
char name[MAX_CMD];
@@ -503,6 +534,13 @@ static int handle_cmd(char *buf, int n)
return 0;
}
#endif
+
+#ifdef NAUT_CONFIG_ISOCORE
+ if (!strncasecmp(buf,"isotest",4)) {
+ handle_isotest(buf);
+ return 0;
+ }
+#endif
if (!strncasecmp(buf,"help",4)) {
nk_vc_printf("help\nexit\nvcs\ncores [n]\ntime [n]\nthreads [n]\n");
@@ -517,7 +555,8 @@ static int handle_cmd(char *buf, int n)
nk_vc_printf("ipitest type (oneway | roundtrip | broadcast) trials [-f <filename>] [-s <src_id> | all] [-d <dst_id> | all]\n");
nk_vc_printf("bench\n");
nk_vc_printf("blktest dev r|w start count\n");
- nk_vc_printf("attach blkdev fstype fsname\n");
+ nk_vc_printf("blktest dev r|w start count\n");
+ nk_vc_printf("isotest\n");
nk_vc_printf("vm name [embedded image]\n");
return 0;
}
--
1.9.1