This commit is contained in:
Pavel 2023-10-11 14:10:07 +03:00
parent 0cea95f965
commit 6a3ed1dbbc
6 changed files with 372 additions and 2592 deletions

View File

@ -347,6 +347,7 @@ end;
//0x40000000
//0x40000010
//0x80000000
//0x80000001
//0x80000002
//0x80000004
@ -359,7 +360,10 @@ end;
procedure jit_cpuid; assembler; nostackframe;
label
_cpuid_0,
_cpuid_1;
_cpuid_1,
_cpuid_80000000,
_cpuid_80000001,
_cpuid_80000008;
asm
pushf
@ -371,8 +375,23 @@ asm
cmp $1,%eax
je _cpuid_1
cmp $0x80000000,%eax
je _cpuid_80000000
cmp $0x80000001,%eax
je _cpuid_80000001
cmp $0x80000008,%eax
je _cpuid_80000008
ud2
_cpuid_0:
//cpu_high TODO check
@ -414,6 +433,39 @@ asm
popf
ret
_cpuid_80000000:
//cpu_exthigh TODO check
mov $0xC0000001,%eax
//cpu_vendor
mov $0x68747541,%ebx
mov $0x69746E65,%edx
mov $0x444D4163,%ecx
mov %rax,jit_frame.tf_rax(%r15)
popf
ret
_cpuid_80000001:
mov $0x2e500800,%edx //amd_feature
mov $0x154837fb,%ecx //amd_feature2
popf
ret
_cpuid_80000008:
mov $0x00003030,%eax //TODO check
mov $0x00001007,%ebx //TODO check
mov $0x00000000,%edx //TODO check
mov $0x00004007,%ecx //cpu_procinfo2 TODO check
mov %rax,jit_frame.tf_rax(%r15)
popf
ret
end;
procedure op_jmp_dispatcher(var ctx:t_jit_context2);
@ -468,12 +520,12 @@ begin
end;
end;
procedure op_pop_rip(var ctx:t_jit_context2); //out:rax
procedure op_pop_rip(var ctx:t_jit_context2;imm:Word); //out:rax
var
stack:TRegValue;
begin
//mov rax,[rsp]
//lea rsp,[rsp+8]
//lea rsp,[rsp+8+imm]
with ctx.builder do
begin
@ -486,7 +538,7 @@ begin
movq(r_tmp1,[stack]);
op_load_rsp(ctx,stack);
leaq(stack,[stack+8]);
leaq(stack,[stack+8+imm]);
op_save_rsp(ctx,stack);
movq(r_tmp0,r_tmp1);
@ -587,10 +639,13 @@ begin
end;
procedure op_ret(var ctx:t_jit_context2);
var
imm:Int64;
begin
Assert(ctx.din.Operand[1].ByteCount=0);
op_pop_rip(ctx); //out:rax
imm:=0;
GetTargetOfs(ctx.din,ctx.code,1,imm);
//
op_pop_rip(ctx,imm); //out:rax
//
op_jmp_dispatcher(ctx);
//
@ -1189,6 +1244,7 @@ begin
jit_cbs[OPPnone,OPcall,OPSnone]:=@op_call;
jit_cbs[OPPnone,OPjmp ,OPSnone]:=@op_jmp;
jit_cbs[OPPnone,OPret ,OPSnone]:=@op_ret;
jit_cbs[OPPnone,OPretf,OPSnone]:=@op_ret;
jit_cbs[OPPnone,OPj__,OPSc_o ]:=@op_jcc;
jit_cbs[OPPnone,OPj__,OPSc_no ]:=@op_jcc;
@ -1348,18 +1404,13 @@ begin
if ((pmap_get_raw(QWORD(ptr)) and PAGE_PROT_EXECUTE)=0) then
begin
//writeln('not excec:0x',HexStr(ptr));
writeln('not excec:0x',HexStr(ptr));
link_curr:=ctx.builder.get_curr_label.after;
ctx.builder.ud2;
link_next:=ctx.builder.get_curr_label.after;
//Add a link for the sequence
link_curr:=ctx.builder.get_curr_label.before;
link_next:=link_curr.after;
ctx.add_label(ptr,
ptr,
link_curr,
link_next);
ctx.trim:=True;
goto _next; //trim
end;
@ -1367,22 +1418,19 @@ begin
adec.Disassemble(ptr,ACodeBytes,ACode);
ctx.ptr_next:=ptr;
case adec.Instr.OpCode.Opcode of
OPX_Invalid..OPX_GroupP:
begin
//invalid
//writeln('invalid:0x',HexStr(ctx.ptr_curr));
writeln('invalid:0x',HexStr(ctx.ptr_curr));
link_curr:=ctx.builder.get_curr_label.after;
ctx.builder.ud2;
link_next:=ctx.builder.get_curr_label.after;
//Add a link for the sequence
link_curr:=ctx.builder.get_curr_label.before;
link_next:=link_curr.after;
ctx.add_label(ptr,
ptr,
link_curr,
link_next);
ctx.trim:=True;
goto _next; //trim
end;
else;
@ -1391,18 +1439,13 @@ begin
if (adec.Instr.Flags * [ifOnly32, ifOnly64, ifOnlyVex] <> []) or
is_invalid(adec.Instr) then
begin
//writeln('invalid:0x',HexStr(ctx.ptr_curr));
writeln('invalid:0x',HexStr(ctx.ptr_curr));
link_curr:=ctx.builder.get_curr_label.after;
ctx.builder.ud2;
link_next:=ctx.builder.get_curr_label.after;
//Add a link for the sequence
link_curr:=ctx.builder.get_curr_label.before;
link_next:=link_curr.after;
ctx.add_label(ptr,
ptr,
link_curr,
link_next);
ctx.trim:=True;
goto _next; //trim
end;
@ -1413,8 +1456,6 @@ begin
Writeln('original------------------------':32,' ','0x',HexStr(ptr));
end;
ctx.ptr_next:=ptr;
ctx.code:=ctx.ptr_curr;
ctx.dis:=adec.Disassembler;
@ -1543,6 +1584,7 @@ begin
end;
}
_next:
//debug
op_debug_info(ctx);
@ -1563,6 +1605,7 @@ begin
end;
//label exist in current blob
if not ctx.trim then
begin
link_new:=ctx.get_link(ptr);
@ -1598,8 +1641,6 @@ begin
if ctx.trim then
begin
_next:
ctx.trim:=False;
//close chunk

View File

@ -977,30 +977,39 @@ end;
procedure add_rip_entry(var ctx:t_jit_context2;ofs:Int64;hint:t_lea_hint);
begin
if (ctx.max<>0) then
if ctx.is_text_addr(ofs) then
begin
if (ofs<=ctx.max) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_EXECUTE)<>0) then
begin
ctx.add_forward_point(Pointer(ofs));
end;
end;
if (code_ref in hint) then
if ctx.is_map_addr(ofs) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_READ)<>0) then
begin
ofs:=PInt64(ofs)^;
//call [addr]
//jmp [addr]
if ctx.is_text_addr(ofs) then
if (ctx.max=0) or (ofs<=ctx.max) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_EXECUTE)<>0) then
if ctx.is_map_addr(ofs) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_READ)<>0) then
begin
ctx.add_forward_point(Pointer(ofs));
end;
end;
ofs:=PInt64(ofs)^;
if ctx.is_text_addr(ofs) then
if (ctx.max=0) or (ofs<=ctx.max) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_EXECUTE)<>0) then
begin
ctx.add_forward_point(Pointer(ofs));
end;
end;
end else
begin
//lea
if (ctx.max<>0) then
if ctx.is_text_addr(ofs) then
begin
if (ofs<=ctx.max) then
if ((pmap_get_raw(QWORD(ofs)) and PAGE_PROT_EXECUTE)<>0) then
begin
ctx.add_forward_point(Pointer(ofs));
end;
end;
end;
end;
function is_segment(const i:TInstruction):Boolean;

View File

@ -992,6 +992,15 @@ begin
end;
end;
procedure op_xgetbv(var ctx:t_jit_context2);
begin
with ctx.builder do
begin
add_orig(ctx);
op_save_rax(ctx,ctx.builder.rax);
end;
end;
//
const
@ -1761,6 +1770,8 @@ begin
jit_cbs[OPPnone,OPcwde,OPSnone]:=@op_cdq;
jit_cbs[OPPnone,OPcdqe,OPSnone]:=@op_cdq;
jit_cbs[OPPnone,OPgetbv,OPSnone]:=@op_xgetbv;
jit_cbs[OPPnone,OPlea,OPSnone]:=@op_lea;
jit_cbs[OPPnone,OPinc,OPSnone]:=@op_inc;

File diff suppressed because it is too large Load Diff

View File

@ -300,7 +300,12 @@ begin
jit_cbs[OPPnone,OPpsrl,OPSx_w ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsrl,OPSx_d ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsrl,OPSx_q ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsrl,OPSx_dq]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsrl,OPSx_dq]:=@add_orig;
jit_cbs[OPPnone,OPpsll,OPSx_w ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsll,OPSx_d ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsll,OPSx_q ]:=@op_reg_mem_0F_rw;
jit_cbs[OPPnone,OPpsll,OPSx_dq]:=@add_orig;
jit_cbs[OPPnone,OPpalignr,OPSnone]:=@op_palignr;

View File

@ -554,7 +554,7 @@ begin
if (clabel=nil) then
begin
Writeln('0x',HexStr(curr));
Writeln('(clabel=nil) 0x',HexStr(curr));
Assert(false);
end;
@ -605,8 +605,12 @@ begin
original:=QWORD(next)-QWORD(curr);
recompil:=link_next.offset-link_curr.offset;
Assert(original<=255);
Assert(recompil<=255);
if (original>255) or (recompil>255) then
begin
Writeln('0x',HexStr(curr));
Writeln(original,':',recompil);
Assert(False);
end;
table[count].original:=Byte(original);
table[count].recompil:=Byte(recompil);