X64 base regs (#1166)

* x86: setup FS & GS base

* Fixed base register writes for x64, removed then for x16/x32 (the don't exist there?)

* FS reg comes before GS so the base regs do so, too

* added shebang to const_generator.py

* Added base regs to and added 'all' support to const_generator

Co-authored-by: naq <aquynh@gmail.com>
This commit is contained in:
Dominik Maier
2020-05-05 02:34:51 +02:00
committed by GitHub
parent b7e64f3c99
commit 625399774c
9 changed files with 59 additions and 10 deletions

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
# Unicorn Engine # Unicorn Engine
# By Dang Hoang Vu, 2013 # By Dang Hoang Vu, 2013
from __future__ import print_function from __future__ import print_function
@ -118,7 +119,8 @@ def gen(lang):
outfile.write((templ['header'] % (prefix)).encode("utf-8")) outfile.write((templ['header'] % (prefix)).encode("utf-8"))
if target == 'unicorn.h': if target == 'unicorn.h':
prefix = '' prefix = ''
lines = open(os.path.join(INCL_DIR, target)).readlines() with open(os.path.join(INCL_DIR, target)) as f:
lines = f.readlines()
previous = {} previous = {}
count = 0 count = 0
@ -185,12 +187,18 @@ def gen(lang):
def main(): def main():
lang = sys.argv[1] lang = sys.argv[1]
if not lang in template: if lang == "all":
raise RuntimeError("Unsupported binding %s" % lang) for lang in template.keys():
gen(sys.argv[1]) print("Generating constants for {}".format(lang))
gen(lang)
else:
if not lang in template:
raise RuntimeError("Unsupported binding %s" % lang)
gen(lang)
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage:", sys.argv[0], " <python>") print("Usage:", sys.argv[0], " <python>")
print("Supported: {}".format(["all"] + [x for x in template.keys()]))
sys.exit(1) sys.exit(1)
main() main()

View File

@ -259,7 +259,9 @@ module X86 =
let UC_X86_REG_FPTAG = 247 let UC_X86_REG_FPTAG = 247
let UC_X86_REG_MSR = 248 let UC_X86_REG_MSR = 248
let UC_X86_REG_MXCSR = 249 let UC_X86_REG_MXCSR = 249
let UC_X86_REG_ENDING = 250 let UC_X86_REG_FS_BASE = 250
let UC_X86_REG_GS_BASE = 251
let UC_X86_REG_ENDING = 252
// X86 instructions // X86 instructions

View File

@ -254,7 +254,9 @@ const (
X86_REG_FPTAG = 247 X86_REG_FPTAG = 247
X86_REG_MSR = 248 X86_REG_MSR = 248
X86_REG_MXCSR = 249 X86_REG_MXCSR = 249
X86_REG_ENDING = 250 X86_REG_FS_BASE = 250
X86_REG_GS_BASE = 251
X86_REG_ENDING = 252
// X86 instructions // X86 instructions

View File

@ -256,7 +256,9 @@ public interface X86Const {
public static final int UC_X86_REG_FPTAG = 247; public static final int UC_X86_REG_FPTAG = 247;
public static final int UC_X86_REG_MSR = 248; public static final int UC_X86_REG_MSR = 248;
public static final int UC_X86_REG_MXCSR = 249; public static final int UC_X86_REG_MXCSR = 249;
public static final int UC_X86_REG_ENDING = 250; public static final int UC_X86_REG_FS_BASE = 250;
public static final int UC_X86_REG_GS_BASE = 251;
public static final int UC_X86_REG_ENDING = 252;
// X86 instructions // X86 instructions

View File

@ -257,7 +257,9 @@ const
UC_X86_REG_FPTAG = 247; UC_X86_REG_FPTAG = 247;
UC_X86_REG_MSR = 248; UC_X86_REG_MSR = 248;
UC_X86_REG_MXCSR = 249; UC_X86_REG_MXCSR = 249;
UC_X86_REG_ENDING = 250; UC_X86_REG_FS_BASE = 250;
UC_X86_REG_GS_BASE = 251;
UC_X86_REG_ENDING = 252;
// X86 instructions // X86 instructions

View File

@ -252,7 +252,9 @@ UC_X86_REG_FPCW = 246
UC_X86_REG_FPTAG = 247 UC_X86_REG_FPTAG = 247
UC_X86_REG_MSR = 248 UC_X86_REG_MSR = 248
UC_X86_REG_MXCSR = 249 UC_X86_REG_MXCSR = 249
UC_X86_REG_ENDING = 250 UC_X86_REG_FS_BASE = 250
UC_X86_REG_GS_BASE = 251
UC_X86_REG_ENDING = 252
# X86 instructions # X86 instructions

View File

@ -254,7 +254,9 @@ module UnicornEngine
UC_X86_REG_FPTAG = 247 UC_X86_REG_FPTAG = 247
UC_X86_REG_MSR = 248 UC_X86_REG_MSR = 248
UC_X86_REG_MXCSR = 249 UC_X86_REG_MXCSR = 249
UC_X86_REG_ENDING = 250 UC_X86_REG_FS_BASE = 250
UC_X86_REG_GS_BASE = 251
UC_X86_REG_ENDING = 252
# X86 instructions # X86 instructions

View File

@ -89,6 +89,8 @@ typedef enum uc_x86_reg {
UC_X86_REG_FPTAG, UC_X86_REG_FPTAG,
UC_X86_REG_MSR, // Model-Specific Register UC_X86_REG_MSR, // Model-Specific Register
UC_X86_REG_MXCSR, UC_X86_REG_MXCSR,
UC_X86_REG_FS_BASE, // Base regs for x86_64
UC_X86_REG_GS_BASE,
UC_X86_REG_ENDING // <-- mark the end of the list of registers UC_X86_REG_ENDING // <-- mark the end of the list of registers
} uc_x86_reg; } uc_x86_reg;

View File

@ -340,6 +340,9 @@ int x86_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int coun
case UC_X86_REG_GS: case UC_X86_REG_GS:
*(int16_t *)value = X86_CPU(uc, mycpu)->env.segs[R_GS].selector; *(int16_t *)value = X86_CPU(uc, mycpu)->env.segs[R_GS].selector;
continue; continue;
case UC_X86_REG_FS_BASE:
*(uint32_t *)value = (uint32_t)X86_CPU(uc, mycpu)->env.segs[R_FS].base;
continue;
} }
// fall-thru // fall-thru
case UC_MODE_32: case UC_MODE_32:
@ -488,6 +491,9 @@ int x86_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int coun
case UC_X86_REG_MXCSR: case UC_X86_REG_MXCSR:
*(uint32_t *)value = X86_CPU(uc, mycpu)->env.mxcsr; *(uint32_t *)value = X86_CPU(uc, mycpu)->env.mxcsr;
break; break;
case UC_X86_REG_FS_BASE:
*(uint32_t *)value = (uint32_t)X86_CPU(uc, mycpu)->env.segs[R_FS].base;
break;
} }
break; break;
@ -788,6 +794,12 @@ int x86_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int coun
dst[1] = reg->_d[1]; dst[1] = reg->_d[1];
break; break;
} }
case UC_X86_REG_FS_BASE:
*(uint64_t *)value = (uint64_t)X86_CPU(uc, mycpu)->env.segs[R_FS].base;
break;
case UC_X86_REG_GS_BASE:
*(uint64_t *)value = (uint64_t)X86_CPU(uc, mycpu)->env.segs[R_GS].base;
break;
} }
break; break;
#endif #endif
@ -1089,6 +1101,15 @@ int x86_reg_write(struct uc_struct *uc, unsigned int *regs, void *const *vals, i
case UC_X86_REG_MXCSR: case UC_X86_REG_MXCSR:
cpu_set_mxcsr(&X86_CPU(uc, mycpu)->env, *(uint32_t *)value); cpu_set_mxcsr(&X86_CPU(uc, mycpu)->env, *(uint32_t *)value);
break; break;
/*
// Don't think base registers are a "thing" on x86
case UC_X86_REG_FS_BASE:
X86_CPU(uc, mycpu)->env.segs[R_FS].base = *(uint32_t *)value;
continue;
case UC_X86_REG_GS_BASE:
X86_CPU(uc, mycpu)->env.segs[R_GS].base = *(uint32_t *)value;
continue;
*/
} }
break; break;
@ -1407,6 +1428,12 @@ int x86_reg_write(struct uc_struct *uc, unsigned int *regs, void *const *vals, i
reg->_d[1] = src[1]; reg->_d[1] = src[1];
break; break;
} }
case UC_X86_REG_FS_BASE:
X86_CPU(uc, mycpu)->env.segs[R_FS].base = *(uint64_t *)value;
continue;
case UC_X86_REG_GS_BASE:
X86_CPU(uc, mycpu)->env.segs[R_GS].base = *(uint64_t *)value;
continue;
} }
break; break;
#endif #endif