virtualmachine 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ ext/virtualmachine/Makefile
19
+ ext/virtualmachine/mkmf.log
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # VirtualMachine
2
2
 
3
- TODO: Write a gem description
3
+ virtualmachine allows to run your assembly code on a VMM instantly, using Ruby.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,26 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Here's a sample code.
22
+ This will create a VM named 'vm0' with 128MB memory, executes an assembly code witch passed to load_asm().
23
+
24
+ require 'virtualmachine'
25
+ vm0 = VirtualMachine.new('vm0', 128)
26
+ vm0.load_asm(<<EOS)
27
+ mov dx, 3F8h
28
+ mov al, 2Ah
29
+ loop:
30
+ out dx, al
31
+ jmp loop
32
+ EOS
33
+ vm0.run
34
+
35
+ ## Limitations/TODO
36
+ - Currently, virtualmachine only supports FreeBSD-10 or later, because it built on BHyVe. Need to support Linux KVM.
37
+ - Page table, GDT, control registers are pre-initialized, hardcoded. Need to find better way to do it.
38
+ - No way to read/write guest registers and guest memory area. Need more APIs.
39
+ - Due to it's hardcoded, you can't choice 16bit/32bit mode. It always runs in 64bit mode.
40
+ - Because BHyVe doesn't have a BIOS, you have no way to call it.
22
41
 
23
42
  ## Contributing
24
43
 
@@ -106,25 +106,86 @@ VALUE vmctx_free(VALUE self)
106
106
  return Qnil;
107
107
  }
108
108
 
109
+ int _vm_create(const char *name)
110
+ {
111
+ int error = vm_create(name);
112
+ if (error)
113
+ rb_raise(rb_eException, "vm_create failed");
114
+ return error;
115
+ }
116
+
117
+ struct vmctx *_vm_open(const char *name)
118
+ {
119
+ struct vmctx *ctx = vm_open(name);
120
+ if (!ctx)
121
+ rb_raise(rb_eException, "vm_open failed");
122
+ return ctx;
123
+ }
124
+
125
+ int _vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s)
126
+ {
127
+ int error = vm_setup_memory(ctx, len, s);
128
+ if (error)
129
+ rb_raise(rb_eException, "vm_setup_memory failed error=%d",
130
+ error);
131
+ return error;
132
+ }
133
+
134
+ void *_vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
135
+ {
136
+ void *paddr = vm_map_gpa(ctx, gaddr, len);
137
+ if (!paddr)
138
+ rb_raise(rb_eException, "vm_map_gpa failed");
139
+ return paddr;
140
+ }
141
+
142
+ int _vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
143
+ uint64_t base, uint32_t limit, uint32_t access)
144
+ {
145
+ int error = vm_set_desc(ctx, vcpu, reg, base, limit, access);
146
+ if (error)
147
+ rb_raise(rb_eException, "vm_set_desc failed error=%d",
148
+ error);
149
+ return error;
150
+ }
151
+
152
+ int _vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
153
+ {
154
+ int error = vm_set_register(ctx, vcpu, reg, val);
155
+ if (error)
156
+ rb_raise(rb_eException, "vm_set_register failed error=%d",
157
+ error);
158
+ return error;
159
+ }
160
+
161
+ int _vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval)
162
+ {
163
+ int error = vm_get_register(ctx, vcpu, reg, retval);
164
+ if (error)
165
+ rb_raise(rb_eException, "vm_get_register failed error=%d",
166
+ error);
167
+ return error;
168
+ }
169
+
109
170
  VALUE virtualmachine_initialize(VALUE self, VALUE vmname, VALUE memsize)
110
171
  {
111
172
  VALUE vctx;
112
173
  struct vmctx *ctx;
113
174
  uint64_t *gdt, *pt4, *pt3, *pt2;
114
- int i;
175
+ int i, err;
115
176
 
116
- vm_create(StringValuePtr(vmname));
117
- ctx = vm_open(StringValuePtr(vmname));
177
+ _vm_create(StringValuePtr(vmname));
178
+ ctx = _vm_open(StringValuePtr(vmname));
118
179
  vctx = Data_Wrap_Struct(rb_cVMCtx, NULL, vmctx_free, ctx);
119
180
  rb_iv_set(self, "@ctx", vctx);
120
181
  rb_iv_set(self, "@vmname", vmname);
121
182
  rb_iv_set(self, "@memsize", memsize);
122
- vm_setup_memory(ctx, FIX2INT(memsize) * MB, VM_MMAP_ALL);
183
+ _vm_setup_memory(ctx, FIX2INT(memsize) * MB, VM_MMAP_ALL);
123
184
 
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);
185
+ pt4 = _vm_map_gpa(ctx, ADDR_PT4, sizeof(uint64_t) * 512);
186
+ pt3 = _vm_map_gpa(ctx, ADDR_PT3, sizeof(uint64_t) * 512);
187
+ pt2 = _vm_map_gpa(ctx, ADDR_PT2, sizeof(uint64_t) * 512);
188
+ gdt = _vm_map_gpa(ctx, ADDR_GDT, sizeof(uint64_t) * 3);
128
189
 
129
190
  bzero(pt4, PAGE_SIZE);
130
191
  bzero(pt3, PAGE_SIZE);
@@ -143,31 +204,31 @@ VALUE virtualmachine_initialize(VALUE self, VALUE vmname, VALUE memsize)
143
204
  gdt[GUEST_CODE_SEL] = 0x0020980000000000;
144
205
  gdt[GUEST_DATA_SEL] = 0x0000900000000000;
145
206
 
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);
207
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_CS, 0, 0, 0x0000209B);
208
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_DS, 0, 0, 0x00000093);
209
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_ES, 0, 0, 0x00000093);
210
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_FS, 0, 0, 0x00000093);
211
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_GS, 0, 0, 0x00000093);
212
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_SS, 0, 0, 0x00000093);
213
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_TR, 0, 0, 0x0000008b);
214
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_LDTR, 0, 0, DESC_UNUSABLE);
215
+ _vm_set_desc(ctx, 0, VM_REG_GUEST_GDTR, ADDR_GDT, GUEST_GDTR_LIMIT, 0);
216
+
217
+ _vm_set_register(ctx, 0, VM_REG_GUEST_CS, GSEL(GUEST_CODE_SEL, SEL_KPL));
218
+ _vm_set_register(ctx, 0, VM_REG_GUEST_DS, GSEL(GUEST_DATA_SEL, SEL_KPL));
219
+ _vm_set_register(ctx, 0, VM_REG_GUEST_ES, GSEL(GUEST_DATA_SEL, SEL_KPL));
220
+ _vm_set_register(ctx, 0, VM_REG_GUEST_FS, GSEL(GUEST_DATA_SEL, SEL_KPL));
221
+ _vm_set_register(ctx, 0, VM_REG_GUEST_GS, GSEL(GUEST_DATA_SEL, SEL_KPL));
222
+ _vm_set_register(ctx, 0, VM_REG_GUEST_SS, GSEL(GUEST_DATA_SEL, SEL_KPL));
223
+ _vm_set_register(ctx, 0, VM_REG_GUEST_TR, 0);
224
+ _vm_set_register(ctx, 0, VM_REG_GUEST_LDTR, 0);
225
+
226
+ _vm_set_register(ctx, 0, VM_REG_GUEST_CR0, CR0_PG | CR0_PE | CR0_NE);
227
+ _vm_set_register(ctx, 0, VM_REG_GUEST_CR3, ADDR_PT4);
228
+ _vm_set_register(ctx, 0, VM_REG_GUEST_CR4, CR4_PAE | CR4_VMXE);
229
+ _vm_set_register(ctx, 0, VM_REG_GUEST_EFER, EFER_LMA | EFER_LME);
230
+ _vm_set_register(ctx, 0, VM_REG_GUEST_RFLAGS, 0x2);
231
+ _vm_set_register(ctx, 0, VM_REG_GUEST_RSP, ADDR_STACK);
171
232
 
172
233
  return self;
173
234
  }
@@ -179,16 +240,147 @@ VALUE virtualmachine_load_binary(VALUE self, VALUE program)
179
240
  unsigned char *entry;
180
241
 
181
242
  Data_Get_Struct(vctx, struct vmctx, ctx);
182
- entry = vm_map_gpa(ctx, ADDR_ENTRY, RSTRING_LEN(program));
243
+ if (!ctx)
244
+ rb_bug("ctx is null");
245
+ entry = _vm_map_gpa(ctx, ADDR_ENTRY, RSTRING_LEN(program));
183
246
  memcpy(entry, StringValuePtr(program), RSTRING_LEN(program));
184
- vm_set_register(ctx, 0, VM_REG_GUEST_RIP, ADDR_ENTRY);
247
+ _vm_set_register(ctx, 0, VM_REG_GUEST_RIP, ADDR_ENTRY);
185
248
  return Qnil;
186
249
  }
187
250
 
251
+ VALUE virtualmachine_destroy(VALUE self)
252
+ {
253
+ VALUE vctx = rb_iv_get(self, "@ctx");
254
+ struct vmctx *ctx;
255
+ Data_Get_Struct(vctx, struct vmctx, ctx);
256
+ if (!ctx)
257
+ rb_bug("ctx is null");
258
+ vm_destroy(ctx);
259
+ return Qnil;
260
+ }
261
+
262
+ #define GETREG(func, reg) \
263
+ static VALUE func(VALUE self) \
264
+ { \
265
+ VALUE vctx = rb_iv_get(self, "@ctx"); \
266
+ struct vmctx *ctx; \
267
+ uint64_t value; \
268
+ Data_Get_Struct(vctx, struct vmctx, ctx); \
269
+ _vm_get_register(ctx, 0, reg, &value); \
270
+ return ULL2NUM(value); \
271
+ }
272
+
273
+ #define SETREG(func, reg) \
274
+ static VALUE func(VALUE self, VALUE value) \
275
+ { \
276
+ VALUE vctx = rb_iv_get(self, "@ctx"); \
277
+ struct vmctx *ctx; \
278
+ Data_Get_Struct(vctx, struct vmctx, ctx); \
279
+ _vm_set_register(ctx, 0, reg, NUM2ULL(value)); \
280
+ return Qnil; \
281
+ }
282
+
283
+ GETREG(virtualmachine_get_rax, VM_REG_GUEST_RAX);
284
+ GETREG(virtualmachine_get_rbx, VM_REG_GUEST_RBX);
285
+ GETREG(virtualmachine_get_rcx, VM_REG_GUEST_RCX);
286
+ GETREG(virtualmachine_get_rdx, VM_REG_GUEST_RDX);
287
+ GETREG(virtualmachine_get_rsi, VM_REG_GUEST_RSI);
288
+ GETREG(virtualmachine_get_rdi, VM_REG_GUEST_RDI);
289
+ GETREG(virtualmachine_get_rbp, VM_REG_GUEST_RBP);
290
+ GETREG(virtualmachine_get_r8, VM_REG_GUEST_R8);
291
+ GETREG(virtualmachine_get_r9, VM_REG_GUEST_R9);
292
+ GETREG(virtualmachine_get_r10, VM_REG_GUEST_R10);
293
+ GETREG(virtualmachine_get_r11, VM_REG_GUEST_R11);
294
+ GETREG(virtualmachine_get_r12, VM_REG_GUEST_R12);
295
+ GETREG(virtualmachine_get_r13, VM_REG_GUEST_R13);
296
+ GETREG(virtualmachine_get_r14, VM_REG_GUEST_R14);
297
+ GETREG(virtualmachine_get_r15, VM_REG_GUEST_R15);
298
+ GETREG(virtualmachine_get_cr0, VM_REG_GUEST_CR0);
299
+ GETREG(virtualmachine_get_cr3, VM_REG_GUEST_CR3);
300
+ GETREG(virtualmachine_get_cr4, VM_REG_GUEST_CR4);
301
+ GETREG(virtualmachine_get_dr7, VM_REG_GUEST_DR7);
302
+ GETREG(virtualmachine_get_rsp, VM_REG_GUEST_RSP);
303
+ GETREG(virtualmachine_get_rip, VM_REG_GUEST_RIP);
304
+ GETREG(virtualmachine_get_rflags, VM_REG_GUEST_RFLAGS);
305
+ GETREG(virtualmachine_get_efer, VM_REG_GUEST_EFER);
306
+
307
+ SETREG(virtualmachine_set_rax, VM_REG_GUEST_RAX);
308
+ SETREG(virtualmachine_set_rbx, VM_REG_GUEST_RBX);
309
+ SETREG(virtualmachine_set_rcx, VM_REG_GUEST_RCX);
310
+ SETREG(virtualmachine_set_rdx, VM_REG_GUEST_RDX);
311
+ SETREG(virtualmachine_set_rsi, VM_REG_GUEST_RSI);
312
+ SETREG(virtualmachine_set_rdi, VM_REG_GUEST_RDI);
313
+ SETREG(virtualmachine_set_rbp, VM_REG_GUEST_RBP);
314
+ SETREG(virtualmachine_set_r8, VM_REG_GUEST_R8);
315
+ SETREG(virtualmachine_set_r9, VM_REG_GUEST_R9);
316
+ SETREG(virtualmachine_set_r10, VM_REG_GUEST_R10);
317
+ SETREG(virtualmachine_set_r11, VM_REG_GUEST_R11);
318
+ SETREG(virtualmachine_set_r12, VM_REG_GUEST_R12);
319
+ SETREG(virtualmachine_set_r13, VM_REG_GUEST_R13);
320
+ SETREG(virtualmachine_set_r14, VM_REG_GUEST_R14);
321
+ SETREG(virtualmachine_set_r15, VM_REG_GUEST_R15);
322
+ SETREG(virtualmachine_set_cr0, VM_REG_GUEST_CR0);
323
+ SETREG(virtualmachine_set_cr3, VM_REG_GUEST_CR3);
324
+ SETREG(virtualmachine_set_cr4, VM_REG_GUEST_CR4);
325
+ SETREG(virtualmachine_set_dr7, VM_REG_GUEST_DR7);
326
+ SETREG(virtualmachine_set_rsp, VM_REG_GUEST_RSP);
327
+ SETREG(virtualmachine_set_rip, VM_REG_GUEST_RIP);
328
+ SETREG(virtualmachine_set_rflags, VM_REG_GUEST_RFLAGS);
329
+ SETREG(virtualmachine_set_efer, VM_REG_GUEST_EFER);
330
+
188
331
  void Init_virtualmachine(void)
189
332
  {
190
333
  rb_cVirtualMachine = rb_define_class("VirtualMachine", rb_cObject);
191
334
  rb_cVMCtx = rb_define_class("VMCtx", rb_cObject);
192
335
  rb_define_method(rb_cVirtualMachine, "initialize", virtualmachine_initialize, 2);
193
336
  rb_define_method(rb_cVirtualMachine, "load_binary", virtualmachine_load_binary, 1);
337
+ rb_define_method(rb_cVirtualMachine, "rax", virtualmachine_get_rax, 0);
338
+ rb_define_method(rb_cVirtualMachine, "rbx", virtualmachine_get_rbx, 0);
339
+ rb_define_method(rb_cVirtualMachine, "rcx", virtualmachine_get_rcx, 0);
340
+ rb_define_method(rb_cVirtualMachine, "rdx", virtualmachine_get_rdx, 0);
341
+ rb_define_method(rb_cVirtualMachine, "rsi", virtualmachine_get_rsi, 0);
342
+ rb_define_method(rb_cVirtualMachine, "rdi", virtualmachine_get_rdi, 0);
343
+ rb_define_method(rb_cVirtualMachine, "rbp", virtualmachine_get_rbp, 0);
344
+ rb_define_method(rb_cVirtualMachine, "r8", virtualmachine_get_r8, 0);
345
+ rb_define_method(rb_cVirtualMachine, "r9", virtualmachine_get_r9, 0);
346
+ rb_define_method(rb_cVirtualMachine, "r10", virtualmachine_get_r10, 0);
347
+ rb_define_method(rb_cVirtualMachine, "r11", virtualmachine_get_r11, 0);
348
+ rb_define_method(rb_cVirtualMachine, "r12", virtualmachine_get_r12, 0);
349
+ rb_define_method(rb_cVirtualMachine, "r13", virtualmachine_get_r13, 0);
350
+ rb_define_method(rb_cVirtualMachine, "r14", virtualmachine_get_r14, 0);
351
+ rb_define_method(rb_cVirtualMachine, "r15", virtualmachine_get_r15, 0);
352
+ rb_define_method(rb_cVirtualMachine, "cr0", virtualmachine_get_cr0, 0);
353
+ rb_define_method(rb_cVirtualMachine, "cr3", virtualmachine_get_cr3, 0);
354
+ rb_define_method(rb_cVirtualMachine, "cr4", virtualmachine_get_cr4, 0);
355
+ rb_define_method(rb_cVirtualMachine, "dr7", virtualmachine_get_dr7, 0);
356
+ rb_define_method(rb_cVirtualMachine, "rsp", virtualmachine_get_rsp, 0);
357
+ rb_define_method(rb_cVirtualMachine, "rip", virtualmachine_get_rip, 0);
358
+ rb_define_method(rb_cVirtualMachine, "rflags", virtualmachine_get_rflags, 0);
359
+ rb_define_method(rb_cVirtualMachine, "efer", virtualmachine_get_efer, 0);
360
+
361
+ rb_define_method(rb_cVirtualMachine, "rax=", virtualmachine_set_rax, 1);
362
+ rb_define_method(rb_cVirtualMachine, "rbx=", virtualmachine_set_rbx, 1);
363
+ rb_define_method(rb_cVirtualMachine, "rcx=", virtualmachine_set_rcx, 1);
364
+ rb_define_method(rb_cVirtualMachine, "rdx=", virtualmachine_set_rdx, 1);
365
+ rb_define_method(rb_cVirtualMachine, "rsi=", virtualmachine_set_rsi, 1);
366
+ rb_define_method(rb_cVirtualMachine, "rdi=", virtualmachine_set_rdi, 1);
367
+ rb_define_method(rb_cVirtualMachine, "rbp=", virtualmachine_set_rbp, 1);
368
+ rb_define_method(rb_cVirtualMachine, "r8=", virtualmachine_set_r8, 1);
369
+ rb_define_method(rb_cVirtualMachine, "r9=", virtualmachine_set_r9, 1);
370
+ rb_define_method(rb_cVirtualMachine, "r10=", virtualmachine_set_r10, 1);
371
+ rb_define_method(rb_cVirtualMachine, "r11=", virtualmachine_set_r11, 1);
372
+ rb_define_method(rb_cVirtualMachine, "r12=", virtualmachine_set_r12, 1);
373
+ rb_define_method(rb_cVirtualMachine, "r13=", virtualmachine_set_r13, 1);
374
+ rb_define_method(rb_cVirtualMachine, "r14=", virtualmachine_set_r14, 1);
375
+ rb_define_method(rb_cVirtualMachine, "r15=", virtualmachine_set_r15, 1);
376
+ rb_define_method(rb_cVirtualMachine, "cr0=", virtualmachine_set_cr0, 1);
377
+ rb_define_method(rb_cVirtualMachine, "cr3=", virtualmachine_set_cr3, 1);
378
+ rb_define_method(rb_cVirtualMachine, "cr4=", virtualmachine_set_cr4, 1);
379
+ rb_define_method(rb_cVirtualMachine, "dr7=", virtualmachine_set_dr7, 1);
380
+ rb_define_method(rb_cVirtualMachine, "rsp=", virtualmachine_set_rsp, 1);
381
+ rb_define_method(rb_cVirtualMachine, "rip=", virtualmachine_set_rip, 1);
382
+ rb_define_method(rb_cVirtualMachine, "rflags=", virtualmachine_set_rflags, 1);
383
+ rb_define_method(rb_cVirtualMachine, "efer=", virtualmachine_set_efer, 1);
384
+
385
+ rb_define_method(rb_cVirtualMachine, "destroy", virtualmachine_destroy, 0);
194
386
  }
@@ -8,6 +8,6 @@ class VirtualMachine
8
8
  end
9
9
 
10
10
  def run
11
- system("/usr/sbin/bhyve -c 1 -m #{@memsize} -s 0:0,hostbridge -S 31,uart,stdio #{@vmname}")
11
+ system("/usr/sbin/bhyve -H -c 1 -m #{@memsize} -s 0:0,hostbridge -S 31,uart,stdio -e #{@vmname} 2> /dev/null")
12
12
  end
13
13
  end
@@ -1,11 +1,48 @@
1
1
  require 'virtualmachine'
2
2
 
3
+ if ARGV.size < 2
4
+ puts "sample1.rb [char] [repeat]"
5
+ exit 1
6
+ end
7
+
8
+ puts "[args]"
9
+ puts "char:#{ARGV[0]}"
10
+ puts "repeat:#{ARGV[1]}"
11
+ puts
12
+
3
13
  vm0 = VirtualMachine.new('vm0', 128)
14
+ vm0.rax = ARGV[0].bytes.to_a[0]
15
+ vm0.rcx = ARGV[1].to_i
4
16
  vm0.load_asm(<<EOS)
5
17
  mov dx, 3F8h
6
- mov al, 2Ah
7
18
  loop:
8
19
  out dx, al
9
- jmp loop
20
+ dec cx
21
+ cmp cx, 0
22
+ jne loop
23
+ mov al, 0ah
24
+ out dx, al
25
+
26
+ mov dx, 10h
27
+ mov al, 0h
28
+ out dx, al
10
29
  EOS
30
+
31
+ puts "[registers]"
32
+ puts "rax:#{vm0.rax}"
33
+ puts "rbx:#{vm0.rbx}"
34
+ puts "rcx:#{vm0.rcx}"
35
+ puts "rdx:#{vm0.rdx}"
36
+ puts "rip:#{vm0.rip}"
37
+ puts
38
+
39
+ puts "[run vm]"
11
40
  vm0.run
41
+
42
+ puts
43
+ puts "[registers]"
44
+ puts "rax:#{vm0.rax}"
45
+ puts "rbx:#{vm0.rbx}"
46
+ puts "rcx:#{vm0.rcx}"
47
+ puts "rdx:#{vm0.rdx}"
48
+ puts "rip:#{vm0.rip}"
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "virtualmachine"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.3"
8
8
  spec.authors = ["Takuya ASADA"]
9
9
  spec.email = ["syuu@dokukino.com"]
10
10
  spec.extensions = ["ext/virtualmachine/extconf.rb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtualmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-14 00:00:00.000000000 Z
12
+ date: 2013-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -72,7 +72,6 @@ files:
72
72
  - LICENSE.txt
73
73
  - README.md
74
74
  - Rakefile
75
- - ext/virtualmachine/Makefile
76
75
  - ext/virtualmachine/extconf.rb
77
76
  - ext/virtualmachine/virtualmachine.c
78
77
  - lib/virtualmachine.rb
@@ -1,237 +0,0 @@
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)