Skip to content

Commit

Permalink
jit: implement call instruction
Browse files Browse the repository at this point in the history
The generated code is suboptimal right now, since it does an indirect call and
has to save registers 6 and 7 on the stack. These issues will be fixed.
  • Loading branch information
rlane committed Sep 17, 2015
1 parent e278e90 commit 5a7bf16
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
7 changes: 6 additions & 1 deletion test_framework/test_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def check_datafile(filename):
memfile.write(data['mem'])
memfile.flush()

num_register_offsets = 20
if 'no register offset' in data:
# The JIT relies on a fixed register mapping for the call instruction
num_register_offsets = 1

try:
for register_offset in xrange(0, 20):
for register_offset in xrange(0, num_register_offsets):
cmd = [VM]
if memfile:
cmd.extend(['-m', memfile.name])
Expand Down
4 changes: 2 additions & 2 deletions tests/call-memfrob.data
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ exit
01 02 03 04 05 06 07 08
-- result
0x102292e2f2c0708
-- no jit
CALL not yet implemented
-- no register offset
call instruction
16 changes: 16 additions & 0 deletions tests/call-save.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- asm
mov r6, 0x0001
mov r7, 0x0020
mov r8, 0x0300
mov r9, 0x4000
call 2
mov r0, 0
or r0, r6
or r0, r7
or r0, r8
or r0, r9
exit
-- result
0x4321
-- no register offset
call instruction
4 changes: 2 additions & 2 deletions tests/call.data
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ call 0
exit
-- result
0x0102030405
-- no jit
CALL not yet implemented
-- no register offset
call instruction
18 changes: 18 additions & 0 deletions vm/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,27 @@ gather_bytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e)
e;
}

static void
trash_registers(void)
{
/* Overwrite all caller-save registers */
asm(
"mov $0xf0, %rax;"
"mov $0xf1, %rcx;"
"mov $0xf2, %rdx;"
"mov $0xf3, %rsi;"
"mov $0xf4, %rdi;"
"mov $0xf5, %r8;"
"mov $0xf6, %r9;"
"mov $0xf7, %r10;"
"mov $0xf8, %r11;"
);
}

static void
register_functions(struct ubpf_vm *vm)
{
ubpf_register(vm, 0, "gather_bytes", gather_bytes);
ubpf_register(vm, 1, "memfrob", memfrob);
ubpf_register(vm, 2, "trash_registers", trash_registers);
}
14 changes: 14 additions & 0 deletions vm/ubpf_jit.dasm
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,20 @@ ubpf_compile(struct ubpf_vm *vm, char **errmsg)
| cmp Rq(dst), Rq(src)
| jge =>jmp_target
break;
case EBPF_OP_CALL:
/* TODO use callee save registers for these */
| push r10
| push r11
/* TODO fix register assignment */
| mov rcx, r8
| mov r8, r9
/* TODO direct call */
| mov rax, vm->ext_funcs[inst.imm]
| call rax
/* TODO use callee save registers for these */
| pop r11
| pop r10
break;
case EBPF_OP_EXIT:
if (i != vm->num_insts - 1) {
| jmp ->exit
Expand Down

0 comments on commit 5a7bf16

Please sign in to comment.