virtualmachine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in virtualmachine.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Takuya ASADA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # VirtualMachine
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'virtualmachine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install virtualmachine
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,237 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@:)
9
+ ECHO = $(ECHO1:0=@echo)
10
+
11
+ #### Start of system configuration section. ####
12
+
13
+ srcdir = .
14
+ topdir = /usr/local/include/ruby-2.0//amd64-freebsd10/ruby/
15
+ hdrdir = /usr/local/include/ruby-2.0/
16
+ arch_hdrdir = /usr/local/include/ruby-2.0//amd64-freebsd10
17
+ PATH_SEPARATOR = :
18
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
19
+ prefix = /usr/local
20
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
21
+ rubyarchprefix = $(rubylibprefix)/$(arch)
22
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
23
+ exec_prefix = $(prefix)
24
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
25
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
26
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
27
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
28
+ sitehdrdir = $(rubyhdrdir)/site_ruby
29
+ rubyhdrdir = $(DESTDIR)/usr/local/include/ruby-2.0/
30
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
31
+ vendorlibdir = $(vendordir)/$(ruby_version)
32
+ vendordir = $(DESTDIR)/usr/local/lib/ruby/vendor_ruby
33
+ sitearchdir = $(sitelibdir)/$(sitearch)
34
+ sitelibdir = $(sitedir)/$(ruby_version)
35
+ sitedir = $(DESTDIR)/usr/local/lib/ruby/site_ruby
36
+ rubyarchdir = $(rubylibdir)/$(arch)
37
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
38
+ sitearchincludedir = $(includedir)/$(sitearch)
39
+ archincludedir = $(includedir)/$(arch)
40
+ sitearchlibdir = $(libdir)/$(sitearch)
41
+ archlibdir = $(libdir)/$(arch)
42
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
43
+ mandir = $(DESTDIR)/usr/local/man
44
+ localedir = $(datarootdir)/locale
45
+ libdir = $(exec_prefix)/lib
46
+ psdir = $(docdir)
47
+ pdfdir = $(docdir)
48
+ dvidir = $(docdir)
49
+ htmldir = $(docdir)
50
+ infodir = $(DESTDIR)/usr/local/info
51
+ docdir = $(DESTDIR)/usr/local/share/doc/ruby20
52
+ oldincludedir = /usr/include
53
+ includedir = $(prefix)/include
54
+ localstatedir = $(prefix)/var
55
+ sharedstatedir = $(prefix)/com
56
+ sysconfdir = $(prefix)/etc
57
+ datadir = $(datarootdir)
58
+ datarootdir = $(prefix)/share
59
+ libexecdir = $(exec_prefix)/libexec
60
+ sbindir = $(exec_prefix)/sbin
61
+ bindir = $(exec_prefix)/bin
62
+ archdir = $(rubyarchdir)
63
+
64
+
65
+ CC = cc
66
+ CXX = c++
67
+ LIBRUBY = $(LIBRUBY_SO)
68
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
69
+ LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)
70
+ LIBRUBYARG_STATIC = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
71
+ empty =
72
+ OUTFLAG = -o $(empty)
73
+ COUTFLAG = -o $(empty)
74
+
75
+ RUBY_EXTCONF_H =
76
+ cflags = $(optflags) $(debugflags) $(warnflags)
77
+ optflags = -O3 -fno-fast-math -fno-omit-frame-pointer
78
+ debugflags =
79
+ warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration
80
+ CCDLFLAGS = -fPIC
81
+ CFLAGS = $(CCDLFLAGS) -O3 -fno-fast-math -fno-omit-frame-pointer -I/usr/local/include -O2 -pipe -fno-strict-aliasing -fPIC $(ARCH_FLAG)
82
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
83
+ DEFS =
84
+ CPPFLAGS = -DHAVE_VMMAPI_H -I/usr/include $(DEFS) $(cppflags)
85
+ CXXFLAGS = $(CCDLFLAGS) -O3 -fno-fast-math -fno-omit-frame-pointer -O2 -pipe -fno-strict-aliasing $(ARCH_FLAG)
86
+ ldflags = -L. -Wl,-rpath=/usr/local/lib -pthread -fstack-protector -rdynamic
87
+ dldflags =
88
+ ARCH_FLAG =
89
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
90
+ LDSHARED = $(CC) -shared
91
+ LDSHAREDXX = $(CXX) -shared
92
+ AR = ar
93
+ EXEEXT =
94
+
95
+ RUBY_INSTALL_NAME = ruby20
96
+ RUBY_SO_NAME = ruby20
97
+ RUBYW_INSTALL_NAME =
98
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
99
+ RUBYW_BASE_NAME = rubyw
100
+ RUBY_BASE_NAME = ruby
101
+
102
+ arch = amd64-freebsd10
103
+ sitearch = $(arch)
104
+ ruby_version = 2.0
105
+ ruby = $(bindir)/ruby20
106
+ RUBY = $(ruby)
107
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
108
+
109
+ RM = rm -f
110
+ RM_RF = $(RUBY) -run -e rm -- -rf
111
+ RMDIRS = rmdir -p
112
+ MAKEDIRS = /bin/mkdir -p
113
+ INSTALL = /usr/bin/install -c -o root -g wheel
114
+ INSTALL_PROG = $(INSTALL) -m 0755
115
+ INSTALL_DATA = install -o root -g wheel -m 444
116
+ COPY = cp
117
+ TOUCH = exit >
118
+
119
+ #### End of system configuration section. ####
120
+
121
+ preload =
122
+
123
+ libpath = . $(libdir) /usr/lib
124
+ LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir) -L/usr/lib -Wl,-R/usr/lib
125
+ DEFFILE =
126
+
127
+ CLEANFILES = mkmf.log
128
+ DISTCLEANFILES =
129
+ DISTCLEANDIRS =
130
+
131
+ extout =
132
+ extout_prefix =
133
+ target_prefix =
134
+ LOCAL_LIBS =
135
+ LIBS = $(LIBRUBYARG_SHARED) -lvmmapi -lutil -lexecinfo -lpthread -lcrypt -lm -L/usr/local/lib -Wl,-rpath=/usr/local/lib -pthread -lc
136
+ ORIG_SRCS = bhyve.c
137
+ SRCS = $(ORIG_SRCS)
138
+ OBJS = bhyve.o
139
+ HDRS =
140
+ TARGET = bhyve
141
+ TARGET_NAME = bhyve
142
+ TARGET_ENTRY = Init_$(TARGET_NAME)
143
+ DLLIB = $(TARGET).so
144
+ EXTSTATIC =
145
+ STATIC_LIB =
146
+
147
+ BINDIR = $(DESTDIR)$(bindir)
148
+ RUBYCOMMONDIR = $(DESTDIR)$(sitedir)$(target_prefix)
149
+ RUBYLIBDIR = $(DESTDIR)$(sitelibdir)$(target_prefix)
150
+ RUBYARCHDIR = $(DESTDIR)$(sitearchdir)$(target_prefix)
151
+ HDRDIR = $(DESTDIR)$(rubyhdrdir)/ruby$(target_prefix)
152
+ ARCHHDRDIR = $(DESTDIR)$(rubyhdrdir)/$(arch)/ruby$(target_prefix)
153
+
154
+ TARGET_SO = $(DLLIB)
155
+ CLEANLIBS = $(TARGET).so
156
+ CLEANOBJS = *.o *.bak
157
+
158
+ all: $(DLLIB)
159
+ static: $(STATIC_LIB)
160
+ .PHONY: all install static install-so install-rb
161
+ .PHONY: clean clean-so clean-static clean-rb
162
+
163
+ clean-static::
164
+ clean-rb-default::
165
+ clean-rb::
166
+ clean-so::
167
+ clean: clean-so clean-static clean-rb-default clean-rb
168
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
169
+
170
+ distclean-rb-default::
171
+ distclean-rb::
172
+ distclean-so::
173
+ distclean-static::
174
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
175
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
176
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
177
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
178
+
179
+ realclean: distclean
180
+ install: install-so install-rb
181
+
182
+ install-so: $(DLLIB) ./.RUBYARCHDIR.time
183
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
184
+ clean-static::
185
+ -$(Q)$(RM) $(STATIC_LIB)
186
+ install-rb: pre-install-rb install-rb-default
187
+ install-rb-default: pre-install-rb-default
188
+ pre-install-rb: Makefile
189
+ pre-install-rb-default: Makefile
190
+ pre-install-rb-default:
191
+ $(ECHO) installing default bhyve libraries
192
+ ./.RUBYARCHDIR.time:
193
+ $(Q) $(MAKEDIRS) $(RUBYARCHDIR)
194
+ $(Q) $(TOUCH) $@
195
+
196
+ site-install: site-install-so site-install-rb
197
+ site-install-so: install-so
198
+ site-install-rb: install-rb
199
+
200
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .C .o
201
+
202
+ .cc.o:
203
+ $(ECHO) compiling $(<)
204
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
205
+
206
+ .mm.o:
207
+ $(ECHO) compiling $(<)
208
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
209
+
210
+ .cxx.o:
211
+ $(ECHO) compiling $(<)
212
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
213
+
214
+ .cpp.o:
215
+ $(ECHO) compiling $(<)
216
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
217
+
218
+ .C.o:
219
+ $(ECHO) compiling $(<)
220
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
221
+
222
+ .c.o:
223
+ $(ECHO) compiling $(<)
224
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
225
+
226
+ .m.o:
227
+ $(ECHO) compiling $(<)
228
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
229
+
230
+ $(DLLIB): $(OBJS) Makefile
231
+ $(ECHO) linking shared-object $(DLLIB)
232
+ -$(Q)$(RM) $(@)
233
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
234
+
235
+
236
+
237
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -0,0 +1,6 @@
1
+ require "mkmf"
2
+
3
+ dir_config('vmmapi', '/usr/include', '/usr/lib')
4
+ if have_header('vmmapi.h') and have_library('util', 'expand_number', 'util.h') and have_library('vmmapi', 'vm_create', 'vmmapi.h')
5
+ create_makefile('virtualmachine')
6
+ end
@@ -0,0 +1,194 @@
1
+ /*-
2
+ * Copyright (c) 2011 NetApp, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
+ * SUCH DAMAGE.
25
+ *
26
+ * $FreeBSD$
27
+ */
28
+
29
+ /*-
30
+ * Copyright (c) 2011 Google, Inc.
31
+ * All rights reserved.
32
+ *
33
+ * Redistribution and use in source and binary forms, with or without
34
+ * modification, are permitted provided that the following conditions
35
+ * are met:
36
+ * 1. Redistributions of source code must retain the above copyright
37
+ * notice, this list of conditions and the following disclaimer.
38
+ * 2. Redistributions in binary form must reproduce the above copyright
39
+ * notice, this list of conditions and the following disclaimer in the
40
+ * documentation and/or other materials provided with the distribution.
41
+ *
42
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
+ * SUCH DAMAGE.
53
+ *
54
+ * $FreeBSD$
55
+ */
56
+
57
+ #include <sys/cdefs.h>
58
+ __FBSDID("$FreeBSD$");
59
+ #include <sys/param.h>
60
+ #include <sys/stat.h>
61
+ #include <machine/specialreg.h>
62
+ #include <machine/vmm.h>
63
+ #include <x86/segments.h>
64
+ #include <fcntl.h>
65
+ #include <stdio.h>
66
+ #include <stdlib.h>
67
+ #include <string.h>
68
+ #include <unistd.h>
69
+ #include <vmmapi.h>
70
+
71
+ #include <ruby.h>
72
+
73
+ #define MB (1024 * 1024UL)
74
+
75
+ #define MSR_EFER 0xc0000080
76
+ #define CR4_PAE 0x00000020
77
+ #define CR4_PSE 0x00000010
78
+ #define CR0_PG 0x80000000
79
+ #define CR0_PE 0x00000001
80
+ #define CR0_NE 0x00000020
81
+
82
+ #define PG_V 0x001
83
+ #define PG_RW 0x002
84
+ #define PG_U 0x004
85
+ #define PG_PS 0x080
86
+
87
+ #define ADDR_PT4 0x2000
88
+ #define ADDR_PT3 0x3000
89
+ #define ADDR_PT2 0x4000
90
+ #define ADDR_GDT 0x5000
91
+ #define ADDR_STACK 0x6000
92
+ #define ADDR_ENTRY 0x10000
93
+
94
+ #define DESC_UNUSABLE 0x00010000
95
+
96
+ #define GUEST_NULL_SEL 0
97
+ #define GUEST_CODE_SEL 1
98
+ #define GUEST_DATA_SEL 2
99
+ #define GUEST_GDTR_LIMIT (3 * 8 - 1)
100
+
101
+ VALUE rb_cVirtualMachine;
102
+ VALUE rb_cVMCtx;
103
+
104
+ VALUE vmctx_free(VALUE self)
105
+ {
106
+ return Qnil;
107
+ }
108
+
109
+ VALUE virtualmachine_initialize(VALUE self, VALUE vmname, VALUE memsize)
110
+ {
111
+ VALUE vctx;
112
+ struct vmctx *ctx;
113
+ uint64_t *gdt, *pt4, *pt3, *pt2;
114
+ int i;
115
+
116
+ vm_create(StringValuePtr(vmname));
117
+ ctx = vm_open(StringValuePtr(vmname));
118
+ vctx = Data_Wrap_Struct(rb_cVMCtx, NULL, vmctx_free, ctx);
119
+ rb_iv_set(self, "@ctx", vctx);
120
+ rb_iv_set(self, "@vmname", vmname);
121
+ rb_iv_set(self, "@memsize", memsize);
122
+ vm_setup_memory(ctx, FIX2INT(memsize) * MB, VM_MMAP_ALL);
123
+
124
+ pt4 = vm_map_gpa(ctx, ADDR_PT4, sizeof(uint64_t) * 512);
125
+ pt3 = vm_map_gpa(ctx, ADDR_PT3, sizeof(uint64_t) * 512);
126
+ pt2 = vm_map_gpa(ctx, ADDR_PT2, sizeof(uint64_t) * 512);
127
+ gdt = vm_map_gpa(ctx, ADDR_GDT, sizeof(uint64_t) * 3);
128
+
129
+ bzero(pt4, PAGE_SIZE);
130
+ bzero(pt3, PAGE_SIZE);
131
+ bzero(pt2, PAGE_SIZE);
132
+
133
+ for (i = 0; i < 512; i++) {
134
+ pt4[i] = (uint64_t)ADDR_PT3;
135
+ pt4[i] |= PG_V | PG_RW | PG_U;
136
+ pt3[i] = (uint64_t)ADDR_PT2;
137
+ pt3[i] |= PG_V | PG_RW | PG_U;
138
+ pt2[i] = i * (2 * 1024 * 1024);
139
+ pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
140
+ }
141
+
142
+ gdt[GUEST_NULL_SEL] = 0;
143
+ gdt[GUEST_CODE_SEL] = 0x0020980000000000;
144
+ gdt[GUEST_DATA_SEL] = 0x0000900000000000;
145
+
146
+ vm_set_desc(ctx, 0, VM_REG_GUEST_CS, 0, 0, 0x0000209B);
147
+ vm_set_desc(ctx, 0, VM_REG_GUEST_DS, 0, 0, 0x00000093);
148
+ vm_set_desc(ctx, 0, VM_REG_GUEST_ES, 0, 0, 0x00000093);
149
+ vm_set_desc(ctx, 0, VM_REG_GUEST_FS, 0, 0, 0x00000093);
150
+ vm_set_desc(ctx, 0, VM_REG_GUEST_GS, 0, 0, 0x00000093);
151
+ vm_set_desc(ctx, 0, VM_REG_GUEST_SS, 0, 0, 0x00000093);
152
+ vm_set_desc(ctx, 0, VM_REG_GUEST_TR, 0, 0, 0x0000008b);
153
+ vm_set_desc(ctx, 0, VM_REG_GUEST_LDTR, 0, 0, DESC_UNUSABLE);
154
+ vm_set_desc(ctx, 0, VM_REG_GUEST_GDTR, ADDR_GDT, GUEST_GDTR_LIMIT, 0);
155
+
156
+ vm_set_register(ctx, 0, VM_REG_GUEST_CS, GSEL(GUEST_CODE_SEL, SEL_KPL));
157
+ vm_set_register(ctx, 0, VM_REG_GUEST_DS, GSEL(GUEST_DATA_SEL, SEL_KPL));
158
+ vm_set_register(ctx, 0, VM_REG_GUEST_ES, GSEL(GUEST_DATA_SEL, SEL_KPL));
159
+ vm_set_register(ctx, 0, VM_REG_GUEST_FS, GSEL(GUEST_DATA_SEL, SEL_KPL));
160
+ vm_set_register(ctx, 0, VM_REG_GUEST_GS, GSEL(GUEST_DATA_SEL, SEL_KPL));
161
+ vm_set_register(ctx, 0, VM_REG_GUEST_SS, GSEL(GUEST_DATA_SEL, SEL_KPL));
162
+ vm_set_register(ctx, 0, VM_REG_GUEST_TR, 0);
163
+ vm_set_register(ctx, 0, VM_REG_GUEST_LDTR, 0);
164
+
165
+ vm_set_register(ctx, 0, VM_REG_GUEST_CR0, CR0_PG | CR0_PE | CR0_NE);
166
+ vm_set_register(ctx, 0, VM_REG_GUEST_CR3, ADDR_PT4);
167
+ vm_set_register(ctx, 0, VM_REG_GUEST_CR4, CR4_PAE | CR4_VMXE);
168
+ vm_set_register(ctx, 0, VM_REG_GUEST_EFER, EFER_LMA | EFER_LME);
169
+ vm_set_register(ctx, 0, VM_REG_GUEST_RFLAGS, 0x2);
170
+ vm_set_register(ctx, 0, VM_REG_GUEST_RSP, ADDR_STACK);
171
+
172
+ return self;
173
+ }
174
+
175
+ VALUE virtualmachine_load_binary(VALUE self, VALUE program)
176
+ {
177
+ VALUE vctx = rb_iv_get(self, "@ctx");
178
+ struct vmctx *ctx;
179
+ unsigned char *entry;
180
+
181
+ Data_Get_Struct(vctx, struct vmctx, ctx);
182
+ entry = vm_map_gpa(ctx, ADDR_ENTRY, RSTRING_LEN(program));
183
+ memcpy(entry, StringValuePtr(program), RSTRING_LEN(program));
184
+ vm_set_register(ctx, 0, VM_REG_GUEST_RIP, ADDR_ENTRY);
185
+ return Qnil;
186
+ }
187
+
188
+ void Init_virtualmachine(void)
189
+ {
190
+ rb_cVirtualMachine = rb_define_class("VirtualMachine", rb_cObject);
191
+ rb_cVMCtx = rb_define_class("VMCtx", rb_cObject);
192
+ rb_define_method(rb_cVirtualMachine, "initialize", virtualmachine_initialize, 2);
193
+ rb_define_method(rb_cVirtualMachine, "load_binary", virtualmachine_load_binary, 1);
194
+ }
@@ -0,0 +1,13 @@
1
+ require 'virtualmachine.so'
2
+ require 'metasm'
3
+
4
+ class VirtualMachine
5
+ def load_asm(program)
6
+ edata = Metasm::Shellcode.assemble(Metasm::X64.new, program).encoded
7
+ load_binary(edata.data)
8
+ end
9
+
10
+ def run
11
+ system("/usr/sbin/bhyve -c 1 -m #{@memsize} -s 0:0,hostbridge -S 31,uart,stdio #{@vmname}")
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'virtualmachine'
2
+
3
+ vm0 = VirtualMachine.new('vm0', 128)
4
+ vm0.load_asm(<<EOS)
5
+ mov dx, 3F8h
6
+ mov al, 2Ah
7
+ loop:
8
+ out dx, al
9
+ jmp loop
10
+ EOS
11
+ vm0.run
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "virtualmachine"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Takuya ASADA"]
9
+ spec.email = ["syuu@dokukino.com"]
10
+ spec.extensions = ["ext/virtualmachine/extconf.rb"]
11
+ spec.description = "Run your asm code on VMM instantly, using ruby"
12
+ spec.summary = "Run your asm code on VMM instantly, using ruby"
13
+ spec.homepage = "https://github.com/syuu1228/ruby-virtualmachine"
14
+ spec.license = "BSDL"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "metasm", ">= 0"
25
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtualmachine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takuya ASADA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: metasm
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Run your asm code on VMM instantly, using ruby
63
+ email:
64
+ - syuu@dokukino.com
65
+ executables: []
66
+ extensions:
67
+ - ext/virtualmachine/extconf.rb
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - ext/virtualmachine/Makefile
76
+ - ext/virtualmachine/extconf.rb
77
+ - ext/virtualmachine/virtualmachine.c
78
+ - lib/virtualmachine.rb
79
+ - sample/sample1.rb
80
+ - virtualmachine.gemspec
81
+ homepage: https://github.com/syuu1228/ruby-virtualmachine
82
+ licenses:
83
+ - BSDL
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.29
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Run your asm code on VMM instantly, using ruby
106
+ test_files: []