Skip to content

Commit 1d375f4

Browse files
authored
Merge pull request #54 from libriscv/storage_trap
Implement trap-based storage calls to remote VMs
2 parents e2c5eb2 + 3dc9582 commit 1d375f4

File tree

13 files changed

+305
-109
lines changed

13 files changed

+305
-109
lines changed

guest/storage/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ int main()
1010
{
1111
printf("Jumping to %p\n", &remote_function);
1212
fflush(stdout);
13-
return remote_function(double_int, 21);
13+
for (int i = 0; i < 10; i++) {
14+
const int val = remote_function(double_int, 21);
15+
printf("Returned value: %d\n", val);
16+
}
17+
return 0;
1418
}
1519

1620
int do_calculation(int value)

guest/storage/storage.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <stdio.h>
2+
#include <unistd.h>
23

34
extern int remote_function(int (*arg)(int), int value)
45
{
6+
//write(1, "In remote_function\n", 20);
57
return arg(value);
68
}
79

lib/tinykvm/amd64/builtin/interrupts.asm

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[BITS 64]
22
global vm64_exception
3+
%define INTR_ASM_BASE 0x2000
34

45
;; CPU exception frame:
56
;; 1. stack rsp+32
@@ -27,12 +28,20 @@ dw .vm64_gettimeofday
2728
dw .vm64_exception
2829
dw .vm64_except1 - .vm64_exception
2930
dw .vm64_dso
31+
.vm64_remote_return_addr:
32+
dw 0x0 ;; Return address after remote call
33+
dd 0x0 ;; Reserved/Padding
34+
.vm64_remote_base:
35+
dq 0x0 ;; Gigapage base address of the remote VM
3036

3137
ALIGN 0x10
3238
.kvm_wallclock: ;; 0x2010
3339
resb 0x10 ;; 16b for KVM Wall-clock
3440
.kvm_system_time: ;; 0x2020
3541
resb 0x20 ;; 32b for KVM System-time
42+
;; Save state for remote function calls
43+
.remote_state:
44+
resb 0x100 ;; 256b for remote state saving
3645

3746
ALIGN 0x10
3847
.vm64_syscall:
@@ -44,6 +53,8 @@ ALIGN 0x10
4453
je .vm64_mmap
4554
cmp eax, 0x1F777 ;; ENTRY SYSCALL
4655
je .vm64_entrycall
56+
cmp eax, 0x1F778 ;; REMOTE DISCONNECT SYSCALL
57+
je .vm64_remote_disconnect
4758
cmp eax, 0x1F707 ;; REENTRY SYSCALL
4859
je .vm64_reentrycall
4960
out 0, eax
@@ -225,6 +236,9 @@ ALIGN 0x10
225236
mov eax, .vm64_gettimeofday
226237
ret
227238

239+
.vm64_remote_disconnect:
240+
out 0, eax
241+
228242
.vm64_entrycall:
229243
;; Reset pagetables
230244
mov rax, cr3
@@ -235,16 +249,47 @@ ALIGN 0x10
235249
o64 sysret
236250

237251
.vm64_page_fault:
252+
push rax
238253
push rdi
239-
mov rdi, cr2
254+
mov rdi, cr2 ;; Faulting address
240255
out 128 + 14, eax
241256
invlpg [rdi]
242257
pop rdi
243-
258+
test eax, eax
259+
jnz .vm64_remote_page_fault
260+
pop rax
244261
.vm64_pop_code:
245262
add rsp, 8
246263
iretq
247264

265+
.vm64_remote_page_fault:
266+
;; 1. We need to save current usermode state
267+
;; 2. Switch to the remote VMs FSBASE
268+
;; 4. Set up a usermode stack/function call
269+
;; with the faulting address as the function address
270+
;; 5. Exit kernel mode to usermode in a way that
271+
;; returns to the remote VM, performing the function call
272+
;; 6. When the remote VM is done, it should somehow enter
273+
;; kernel mode again, and we should restore our state
274+
;; 7. Return from the page fault
275+
276+
;; Make the next function call return to a custom system call location
277+
push rbx
278+
;; Get remote-disconnect syscall address
279+
mov rax, [INTR_ASM_BASE + .vm64_remote_return_addr]
280+
;; Get original stack pointer
281+
mov rbx, [rsp + 16 + 32] ;; Original RSP
282+
;; Overwrite the return address
283+
stac
284+
mov [rbx], rax ;; Return address
285+
clac
286+
287+
pop rbx
288+
pop rax
289+
add rsp, 8 ;; Skip error code
290+
291+
iretq
292+
248293
.vm64_timeout:
249294
out 128 + 33, eax
250295
iretq
Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,84 @@
11
unsigned char interrupts[] = {
2-
0x40, 0x00, 0xa7, 0x01, 0xd8, 0x01, 0x08, 0x00, 0xaf, 0x01, 0x90, 0x90,
3-
0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7-
0x00, 0x00, 0x00, 0x00, 0x66, 0x3d, 0x9e, 0x00, 0x74, 0x2e, 0x66, 0x3d,
8-
0xe4, 0x00, 0x0f, 0x84, 0xe3, 0x00, 0x00, 0x00, 0x83, 0xf8, 0x09, 0x0f,
9-
0x84, 0x35, 0x01, 0x00, 0x00, 0x3d, 0x77, 0xf7, 0x01, 0x00, 0x0f, 0x84,
10-
0x51, 0x01, 0x00, 0x00, 0x3d, 0x07, 0xf7, 0x01, 0x00, 0x0f, 0x84, 0x4f,
11-
0x01, 0x00, 0x00, 0xe7, 0x00, 0x48, 0x0f, 0x07, 0x0f, 0x01, 0xcb, 0x56,
12-
0x51, 0x52, 0x48, 0x81, 0xff, 0x02, 0x10, 0x00, 0x00, 0x75, 0x1b, 0xb9,
13-
0x00, 0x01, 0x00, 0xc0, 0x89, 0xf0, 0x48, 0xc1, 0xee, 0x20, 0x89, 0xf2,
14-
0x0f, 0x30, 0x48, 0x31, 0xc0, 0x5a, 0x59, 0x5e, 0x0f, 0x01, 0xca, 0x48,
15-
0x0f, 0x07, 0x48, 0x81, 0xff, 0x03, 0x10, 0x00, 0x00, 0x75, 0x16, 0xb9,
16-
0x00, 0x01, 0x00, 0xc0, 0x0f, 0x32, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x09,
17-
0xc2, 0x48, 0x89, 0x06, 0x48, 0x31, 0xc0, 0xeb, 0xd8, 0xe7, 0x00, 0xeb,
18-
0xd4, 0x0f, 0x31, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x09, 0xd0, 0x48, 0x2b,
19-
0x04, 0x25, 0x28, 0x20, 0x00, 0x00, 0x8a, 0x0c, 0x25, 0x3c, 0x20, 0x00,
20-
0x00, 0x84, 0xc9, 0x78, 0x05, 0x48, 0xd3, 0xe0, 0xeb, 0x05, 0xf7, 0xd9,
21-
0x48, 0xd3, 0xe8, 0x8b, 0x0c, 0x25, 0x38, 0x20, 0x00, 0x00, 0x48, 0xf7,
22-
0xe1, 0x48, 0xc1, 0xe8, 0x20, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x09, 0xd0,
23-
0x48, 0x03, 0x04, 0x25, 0x30, 0x20, 0x00, 0x00, 0xc3, 0x8b, 0x0c, 0x25,
24-
0x14, 0x20, 0x00, 0x00, 0x85, 0xc9, 0x75, 0x11, 0xb9, 0x00, 0x4d, 0x56,
25-
0x4b, 0xb8, 0x10, 0x20, 0x00, 0x00, 0xba, 0x00, 0x00, 0x00, 0x00, 0x0f,
26-
0x30, 0x8b, 0x0c, 0x25, 0x14, 0x20, 0x00, 0x00, 0x8b, 0x14, 0x25, 0x18,
27-
0x20, 0x00, 0x00, 0x48, 0x01, 0xd0, 0xc3, 0x0f, 0x01, 0xcb, 0x53, 0x51,
28-
0x52, 0x48, 0x81, 0xfe, 0x00, 0x00, 0x10, 0x00, 0x72, 0x32, 0xe8, 0x7a,
29-
0xff, 0xff, 0xff, 0x48, 0x31, 0xc9, 0x48, 0x85, 0xff, 0x75, 0x05, 0xe8,
30-
0xb1, 0xff, 0xff, 0xff, 0x48, 0x31, 0xd2, 0xbb, 0x00, 0xca, 0x9a, 0x3b,
31-
0x48, 0xf7, 0xf3, 0x48, 0x01, 0xc8, 0x48, 0x89, 0x06, 0x48, 0x89, 0x56,
32-
0x08, 0x5a, 0x59, 0x5b, 0x0f, 0x01, 0xca, 0x31, 0xc0, 0x48, 0x0f, 0x07,
33-
0x48, 0xc7, 0xc0, 0xf2, 0xff, 0xff, 0xff, 0x48, 0x0f, 0x07, 0x5a, 0x59,
34-
0x5b, 0x0f, 0x01, 0xca, 0xb8, 0xe4, 0x00, 0x00, 0x00, 0xe7, 0x00, 0x48,
35-
0x0f, 0x07, 0xe7, 0x00, 0x49, 0x83, 0xf8, 0xff, 0x74, 0x0e, 0x0f, 0x01,
36-
0xcb, 0x50, 0x0f, 0x20, 0xd8, 0x0f, 0x22, 0xd8, 0x58, 0x0f, 0x01, 0xca,
37-
0x48, 0x0f, 0x07, 0xb8, 0x60, 0x00, 0x00, 0x00, 0xe7, 0x00, 0xc3, 0xb8,
38-
0xa7, 0x01, 0x00, 0x00, 0xc3, 0x0f, 0x20, 0xd8, 0x0f, 0x22, 0xd8, 0x48,
39-
0x0f, 0x07, 0x48, 0x0f, 0x07, 0x57, 0x0f, 0x20, 0xd7, 0xe7, 0x8e, 0x0f,
40-
0x01, 0x3f, 0x5f, 0x48, 0x83, 0xc4, 0x08, 0x48, 0xcf, 0xe7, 0xa1, 0x48,
41-
0xcf, 0x90, 0x90, 0x90, 0xe7, 0x80, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
42-
0xe7, 0x81, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x82, 0x48, 0xcf,
43-
0x90, 0x90, 0x90, 0x90, 0xe7, 0x83, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
44-
0xe7, 0x84, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x85, 0x48, 0xcf,
45-
0x90, 0x90, 0x90, 0x90, 0xe7, 0x86, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
46-
0xe7, 0x87, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x88, 0xeb, 0xaf,
47-
0x90, 0x90, 0x90, 0x90, 0xe7, 0x89, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
48-
0xe7, 0x8a, 0xeb, 0x9f, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x8b, 0xeb, 0x97,
49-
0x90, 0x90, 0x90, 0x90, 0xe7, 0x8c, 0xeb, 0x8f, 0x90, 0x90, 0x90, 0x90,
50-
0xe7, 0x8d, 0xeb, 0x87, 0x90, 0x90, 0x90, 0x90, 0xe9, 0x74, 0xff, 0xff,
51-
0xff, 0x90, 0x90, 0x90, 0xe7, 0x8f, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
52-
0xe7, 0x90, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x91, 0xe9, 0x64,
53-
0xff, 0xff, 0xff, 0x90, 0xe7, 0x92, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
54-
0xe7, 0x93, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x94, 0x48, 0xcf,
55-
0x90, 0x90, 0x90, 0x90, 0xe9, 0x4c, 0xff, 0xff, 0xff
2+
0x50, 0x01, 0xc2, 0x02, 0x18, 0x03, 0x08, 0x00, 0xca, 0x02, 0x00, 0x00,
3+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4+
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00,
5+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30+
0x66, 0x3d, 0x9e, 0x00, 0x74, 0x39, 0x66, 0x3d, 0xe4, 0x00, 0x0f, 0x84,
31+
0xee, 0x00, 0x00, 0x00, 0x83, 0xf8, 0x09, 0x0f, 0x84, 0x40, 0x01, 0x00,
32+
0x00, 0x3d, 0x77, 0xf7, 0x01, 0x00, 0x0f, 0x84, 0x5e, 0x01, 0x00, 0x00,
33+
0x3d, 0x78, 0xf7, 0x01, 0x00, 0x0f, 0x84, 0x51, 0x01, 0x00, 0x00, 0x3d,
34+
0x07, 0xf7, 0x01, 0x00, 0x0f, 0x84, 0x51, 0x01, 0x00, 0x00, 0xe7, 0x00,
35+
0x48, 0x0f, 0x07, 0x0f, 0x01, 0xcb, 0x56, 0x51, 0x52, 0x48, 0x81, 0xff,
36+
0x02, 0x10, 0x00, 0x00, 0x75, 0x1b, 0xb9, 0x00, 0x01, 0x00, 0xc0, 0x89,
37+
0xf0, 0x48, 0xc1, 0xee, 0x20, 0x89, 0xf2, 0x0f, 0x30, 0x48, 0x31, 0xc0,
38+
0x5a, 0x59, 0x5e, 0x0f, 0x01, 0xca, 0x48, 0x0f, 0x07, 0x48, 0x81, 0xff,
39+
0x03, 0x10, 0x00, 0x00, 0x75, 0x16, 0xb9, 0x00, 0x01, 0x00, 0xc0, 0x0f,
40+
0x32, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x09, 0xc2, 0x48, 0x89, 0x06, 0x48,
41+
0x31, 0xc0, 0xeb, 0xd8, 0xe7, 0x00, 0xeb, 0xd4, 0x0f, 0x31, 0x48, 0xc1,
42+
0xe2, 0x20, 0x48, 0x09, 0xd0, 0x48, 0x2b, 0x04, 0x25, 0x28, 0x20, 0x00,
43+
0x00, 0x8a, 0x0c, 0x25, 0x3c, 0x20, 0x00, 0x00, 0x84, 0xc9, 0x78, 0x05,
44+
0x48, 0xd3, 0xe0, 0xeb, 0x05, 0xf7, 0xd9, 0x48, 0xd3, 0xe8, 0x8b, 0x0c,
45+
0x25, 0x38, 0x20, 0x00, 0x00, 0x48, 0xf7, 0xe1, 0x48, 0xc1, 0xe8, 0x20,
46+
0x48, 0xc1, 0xe2, 0x20, 0x48, 0x09, 0xd0, 0x48, 0x03, 0x04, 0x25, 0x30,
47+
0x20, 0x00, 0x00, 0xc3, 0x8b, 0x0c, 0x25, 0x14, 0x20, 0x00, 0x00, 0x85,
48+
0xc9, 0x75, 0x11, 0xb9, 0x00, 0x4d, 0x56, 0x4b, 0xb8, 0x10, 0x20, 0x00,
49+
0x00, 0xba, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x8b, 0x0c, 0x25, 0x14,
50+
0x20, 0x00, 0x00, 0x8b, 0x14, 0x25, 0x18, 0x20, 0x00, 0x00, 0x48, 0x01,
51+
0xd0, 0xc3, 0x0f, 0x01, 0xcb, 0x53, 0x51, 0x52, 0x48, 0x81, 0xfe, 0x00,
52+
0x00, 0x10, 0x00, 0x72, 0x32, 0xe8, 0x7a, 0xff, 0xff, 0xff, 0x48, 0x31,
53+
0xc9, 0x48, 0x85, 0xff, 0x75, 0x05, 0xe8, 0xb1, 0xff, 0xff, 0xff, 0x48,
54+
0x31, 0xd2, 0xbb, 0x00, 0xca, 0x9a, 0x3b, 0x48, 0xf7, 0xf3, 0x48, 0x01,
55+
0xc8, 0x48, 0x89, 0x06, 0x48, 0x89, 0x56, 0x08, 0x5a, 0x59, 0x5b, 0x0f,
56+
0x01, 0xca, 0x31, 0xc0, 0x48, 0x0f, 0x07, 0x48, 0xc7, 0xc0, 0xf2, 0xff,
57+
0xff, 0xff, 0x48, 0x0f, 0x07, 0x5a, 0x59, 0x5b, 0x0f, 0x01, 0xca, 0xb8,
58+
0xe4, 0x00, 0x00, 0x00, 0xe7, 0x00, 0x48, 0x0f, 0x07, 0xe7, 0x00, 0x49,
59+
0x83, 0xf8, 0xff, 0x74, 0x0e, 0x0f, 0x01, 0xcb, 0x50, 0x0f, 0x20, 0xd8,
60+
0x0f, 0x22, 0xd8, 0x58, 0x0f, 0x01, 0xca, 0x48, 0x0f, 0x07, 0xb8, 0x60,
61+
0x00, 0x00, 0x00, 0xe7, 0x00, 0xc3, 0xb8, 0xc2, 0x02, 0x00, 0x00, 0xc3,
62+
0xe7, 0x00, 0x0f, 0x20, 0xd8, 0x0f, 0x22, 0xd8, 0x48, 0x0f, 0x07, 0x48,
63+
0x0f, 0x07, 0x50, 0x57, 0x0f, 0x20, 0xd7, 0xe7, 0x8e, 0x0f, 0x01, 0x3f,
64+
0x5f, 0x85, 0xc0, 0x75, 0x07, 0x58, 0x48, 0x83, 0xc4, 0x08, 0x48, 0xcf,
65+
0x53, 0x48, 0x8b, 0x04, 0x25, 0x0a, 0x20, 0x00, 0x00, 0x48, 0x8b, 0x5c,
66+
0x24, 0x30, 0x0f, 0x01, 0xcb, 0x48, 0x89, 0x03, 0x0f, 0x01, 0xca, 0x5b,
67+
0x58, 0x48, 0x83, 0xc4, 0x08, 0x48, 0xcf, 0xe7, 0xa1, 0x48, 0xcf, 0x90,
68+
0xe7, 0x80, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x81, 0x48, 0xcf,
69+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x82, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
70+
0xe7, 0x83, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x84, 0x48, 0xcf,
71+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x85, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
72+
0xe7, 0x86, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x87, 0x48, 0xcf,
73+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x88, 0xeb, 0x92, 0x90, 0x90, 0x90, 0x90,
74+
0xe7, 0x89, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x8a, 0xeb, 0x82,
75+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x8b, 0xe9, 0x77, 0xff, 0xff, 0xff, 0x90,
76+
0xe7, 0x8c, 0xe9, 0x6f, 0xff, 0xff, 0xff, 0x90, 0xe7, 0x8d, 0xe9, 0x67,
77+
0xff, 0xff, 0xff, 0x90, 0xe9, 0x51, 0xff, 0xff, 0xff, 0x90, 0x90, 0x90,
78+
0xe7, 0x8f, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x90, 0x48, 0xcf,
79+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x91, 0xe9, 0x47, 0xff, 0xff, 0xff, 0x90,
80+
0xe7, 0x92, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90, 0xe7, 0x93, 0x48, 0xcf,
81+
0x90, 0x90, 0x90, 0x90, 0xe7, 0x94, 0x48, 0xcf, 0x90, 0x90, 0x90, 0x90,
82+
0xe9, 0x4e, 0xff, 0xff, 0xff
5683
};
57-
unsigned int interrupts_len = 645;
84+
unsigned int interrupts_len = 965;

lib/tinykvm/amd64/builtin/usercode.asm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
dw .vm64_entry
44
dw .vm64_rexit
55
dw .vm64_preserving_entry
6-
dw 0
6+
dw .vm64_remote_disconnect
77
dd .vm64_cpuid
88

99
ALIGN 0x10
@@ -41,7 +41,18 @@ ALIGN 0x10
4141
;; With the registers restored, we can now
4242
;; return to the guest program.
4343
ret
44-
44+
.vm64_remote_disconnect:
45+
push rax
46+
push rax
47+
push r11
48+
push rcx
49+
;; Execute a system call that disconnects the remote VM.
50+
mov rax, 0x1F778
51+
syscall
52+
pop rcx
53+
pop r11
54+
pop rax
55+
ret ;; RAX replaced with return value
4556

4657
%macro vcputable 1
4758
dd %1

lib/tinykvm/amd64/idt.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct iasm_header {
1919
uint16_t vm64_exception;
2020
uint16_t vm64_except_size;
2121
uint16_t vm64_dso;
22+
uint16_t vm64_remote_return_addr;
23+
uint32_t reserved1;
24+
uint64_t vm64_remote_base;
2225

2326
uint64_t translated_vm_syscall(const vMemory& memory) const noexcept
2427
{

lib/tinykvm/amd64/usercode.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@
44
namespace tinykvm {
55

66
static const unsigned char usercode[] = {
7-
0x10, 0x00, 0x20, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
8-
0x90, 0x90, 0x90, 0x90, 0x49, 0x89, 0xcd, 0xb8, 0x77, 0xf7, 0x01, 0x00,
9-
0x0f, 0x05, 0x4c, 0x89, 0xe9, 0x41, 0xff, 0xe7, 0x48, 0x89, 0xc7, 0xb8,
10-
0xff, 0xff, 0x00, 0x00, 0xe7, 0x00, 0xeb, 0xf7, 0xb8, 0x77, 0xf7, 0x01,
11-
0x00, 0x0f, 0x05, 0x41, 0x5b, 0x59, 0x58, 0xc3, 0x00, 0x00, 0x00, 0x00,
12-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13-
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14-
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
16-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17-
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18-
0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
20-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21-
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22-
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
24-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25-
0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26-
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
28-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29-
0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30-
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
32-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33-
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34-
0x00, 0x00, 0x00, 0x00
7+
0x10, 0x00, 0x20, 0x00, 0x2c, 0x00, 0x38, 0x00, 0x50, 0x00, 0x00, 0x00,
8+
0x90, 0x90, 0x90, 0x90, 0x49, 0x89, 0xcd, 0xb8, 0x77, 0xf7, 0x01, 0x00,
9+
0x0f, 0x05, 0x4c, 0x89, 0xe9, 0x41, 0xff, 0xe7, 0x48, 0x89, 0xc7, 0xb8,
10+
0xff, 0xff, 0x00, 0x00, 0xe7, 0x00, 0xeb, 0xf7, 0xb8, 0x77, 0xf7, 0x01,
11+
0x00, 0x0f, 0x05, 0x41, 0x5b, 0x59, 0x58, 0xc3, 0x50, 0x50, 0x41, 0x53,
12+
0x51, 0xb8, 0x78, 0xf7, 0x01, 0x00, 0x0f, 0x05, 0x59, 0x41, 0x5b, 0x58,
13+
0xc3, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00,
14+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16+
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
18+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19+
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20+
0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
22+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23+
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24+
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
26+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27+
0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28+
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
30+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31+
0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32+
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
34+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35+
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36+
0x00, 0x00, 0x00, 0x00
3537
};
3638

3739
const user_asm_header &usercode_header()

lib/tinykvm/amd64/usercode.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct user_asm_header {
88
uint16_t vm64_entry;
99
uint16_t vm64_rexit;
1010
uint16_t vm64_preserving_entry;
11-
uint16_t vm64_unused;
11+
uint16_t vm64_remote_disconnect;
1212
uint32_t vm64_cpuid;
1313

1414
uint64_t translated_vm_entry(const vMemory& memory) const noexcept {
@@ -20,6 +20,9 @@ struct user_asm_header {
2020
uint64_t translated_vm_preserving_entry(const vMemory& memory) const noexcept {
2121
return memory.physbase + USER_ASM_ADDR + vm64_preserving_entry;
2222
}
23+
uint64_t translated_vm_remote_disconnect(const vMemory& memory) const noexcept {
24+
return memory.physbase + USER_ASM_ADDR + vm64_remote_disconnect;
25+
}
2326
uint64_t translated_vm_cpuid(const vMemory& memory) const noexcept {
2427
return memory.physbase + USER_ASM_ADDR + vm64_cpuid;
2528
}

lib/tinykvm/machine.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ struct Machine
247247
bool uses_cow_memory() const noexcept { return m_forked || m_prepped; }
248248

249249
/* Remote VM through address space merging */
250-
void remote_connect(Machine& other);
250+
void remote_connect(Machine& other, bool connect_now = true);
251+
void remote_activate_now();
252+
void remote_disconnect();
251253
bool is_remote_connected() const noexcept { return m_remote != nullptr; };
254+
address_t remote_base_address() const noexcept;
252255
const Machine& remote() const;
253256
Machine& remote();
254257

0 commit comments

Comments
 (0)