Skip to content

Commit 815462c

Browse files
committed
simx86: keep exec-only base internal to codebuf.c, use uintptr_t
This eliminates struct cbptr and makes sure that all asm stub functions that are passed the executable rip get a uintptr_t rip that is then converted to an r/w pointer using GetGenCodeBuf.
1 parent b5a2b79 commit 815462c

File tree

9 files changed

+61
-61
lines changed

9 files changed

+61
-61
lines changed

src/base/emu-i386/simx86/codebuf.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
#define CODEBUF_SZ (128 * 1024 * 1024)
3434
static mspace cspace;
3535
static unsigned char *wbase;
36-
static unsigned char *xbase;
36+
static uintptr_t xoffset;
3737

3838
void InitGenCodeBuf(void)
3939
{
4040
void *addr;
41+
unsigned char *xbase;
4142

4243
#if HAVE_DECL_MREMAP_MAYMOVE
4344
int err;
@@ -58,32 +59,32 @@ void InitGenCodeBuf(void)
5859
xbase = addr;
5960
#endif
6061
wbase = addr;
62+
xoffset = xbase - wbase;
6163
cspace = create_mspace_with_base(addr, CODEBUF_SZ, 0);
6264
assert(cspace);
6365
}
6466

6567
void EndGenCodeBuf(void)
6668
{
6769
destroy_mspace(cspace);
68-
if (xbase != wbase)
69-
munmap(xbase, CODEBUF_SZ);
70+
if (xoffset)
71+
munmap(wbase + xoffset, CODEBUF_SZ);
7072
munmap(wbase, CODEBUF_SZ);
7173
}
7274

73-
struct cbptr AllocGenCodeBuf(size_t size)
75+
void *AllocGenCodeBuf(size_t size)
7476
{
75-
struct cbptr ret;
77+
void *ret;
7678

77-
ret.ptr = mspace_malloc(cspace, size);
78-
assert(ret.ptr);
79-
ret.xptr = xbase + (ret.ptr - wbase);
79+
ret = mspace_malloc(cspace, size);
80+
assert(ret);
8081
return ret;
8182
}
8283

83-
void ShrinkGenCodeBuf(struct cbptr ptr, size_t size)
84+
void ShrinkGenCodeBuf(void *ptr, size_t size)
8485
{
85-
void *ptr2 = mspace_realloc(cspace, ptr.ptr, size);
86-
assert(ptr2 == ptr.ptr);
86+
void *ptr2 = mspace_realloc(cspace, ptr, size);
87+
assert(ptr2 == ptr);
8788
}
8889

8990
void FreeGenCodeBuf(void *ptr)
@@ -93,14 +94,15 @@ void FreeGenCodeBuf(void *ptr)
9394
mspace_free(cspace, ptr);
9495
}
9596

96-
unsigned char *GetGenCodeBuf(const unsigned char *eip)
97+
unsigned char *GetGenCodeBuf(uintptr_t eip)
9798
{
98-
assert(eip >= xbase && eip < xbase + CODEBUF_SZ);
99-
return wbase + (eip - xbase);
99+
unsigned char *p = (unsigned char *)eip - xoffset;
100+
assert(p >= wbase && p < wbase + CODEBUF_SZ);
101+
return p;
100102
}
101103

102-
unsigned char *GetExecCodeBuf(const unsigned char *ptr)
104+
uintptr_t GetExecCodeBuf(const unsigned char *ptr)
103105
{
104106
assert(ptr >= wbase && ptr < wbase + CODEBUF_SZ);
105-
return xbase + (ptr - wbase);
107+
return (uintptr_t)ptr + xoffset;
106108
}

src/base/emu-i386/simx86/codegen-sim.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ static unsigned Exec_sim(unsigned *mem_ref, unsigned long *flg,
7777
unsigned char *ecpu, void *SeqStart,
7878
unsigned short seqflg, unsigned *seqbase);
7979

80+
static unsigned int _Sim_helper(unsigned int mem_ref, unsigned int data, int mode,
81+
uint32_t *flags, unsigned int opc, unsigned int arg,
82+
unsigned char *eip);
83+
8084
static unsigned char *currentIG = NULL;
8185

8286
/////////////////////////////////////////////////////////////////////////////
@@ -2130,7 +2134,7 @@ static unsigned int Gen_sim(IGen *IG, unsigned int *pmem_ref,
21302134

21312135
case O_SIM: {
21322136
uint32_t flags = FlagSync_All();
2133-
DR1.d = Sim_helper(mem_ref, DR1.d, mode,
2137+
DR1.d = _Sim_helper(mem_ref, DR1.d, mode,
21342138
&flags, IG->p0, IG->p1, currentIG);
21352139
FlagSync_RFL(flags);
21362140
if (TheCPU.err > 0)
@@ -3518,9 +3522,10 @@ static unsigned int _Sim_helper(unsigned int mem_ref, unsigned int data, int mod
35183522

35193523
unsigned int Sim_helper(unsigned int mem_ref, unsigned int data, int mode,
35203524
uint32_t *flags, unsigned int opc, unsigned int arg,
3521-
unsigned char *eip)
3525+
uintptr_t rip)
35223526
{
35233527
unsigned int ret;
3528+
unsigned char *eip = GetGenCodeBuf(rip);
35243529

35253530
InCompiledCode--;
35263531
ret = _Sim_helper(mem_ref, data, mode, flags, opc, arg, eip);

src/base/emu-i386/simx86/codegen.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,11 @@ void Gen(int op, int mode, ...)
391391
/////////////////////////////////////////////////////////////////////////////
392392

393393

394-
static struct cbptr ProduceCode(unsigned int PC, IMeta *I0)
394+
static unsigned char *ProduceCode(unsigned int PC, IMeta *I0)
395395
{
396396
int i,j,mall_req;
397397
unsigned char *cp, *cp1, *BaseGenBuf, *CodePtr;
398398
size_t GenBufSize;
399-
struct cbptr cbp;
400399

401400
if (debug_level('e')>1) {
402401
e_printf("---------------------------------------------\n");
@@ -414,8 +413,7 @@ static struct cbptr ProduceCode(unsigned int PC, IMeta *I0)
414413
for (i=0; i<=CurrIMeta; i++)
415414
GenBufSize += I0[i].ngen * MAX_GEND_BYTES_PER_OP;
416415
mall_req = GenBufSize + 32;// 32 for tail
417-
cbp = AllocGenCodeBuf(mall_req);
418-
BaseGenBuf = cbp.ptr;
416+
BaseGenBuf = AllocGenCodeBuf(mall_req);
419417
/* actual code buffer starts from here */
420418
CodePtr = BaseGenBuf;
421419
I0->daddr = 0;
@@ -467,11 +465,11 @@ static struct cbptr ProduceCode(unsigned int PC, IMeta *I0)
467465

468466
/* shrink buffer to what is actually needed */
469467
mall_req = I0->totlen;
470-
ShrinkGenCodeBuf(cbp, mall_req);
468+
ShrinkGenCodeBuf(BaseGenBuf, mall_req);
471469
if (debug_level('e')>3)
472470
e_printf("Seq len %#x:%#x\n",I0->seqlen,I0->totlen);
473471

474-
return cbp;
472+
return BaseGenBuf;
475473
}
476474

477475

@@ -503,7 +501,7 @@ TNode *Close(unsigned int PC, unsigned int Interp_LONG_CS, int mode,
503501
{
504502
IMeta *I0;
505503
TNode *G;
506-
struct cbptr GenCodeBuf;
504+
unsigned char *GenCodeBuf;
507505

508506
assert (CurrIMeta >= 0);
509507

src/base/emu-i386/simx86/cpatch.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void m_munprotect(unsigned int addr, unsigned int len, unsigned char *eip)
7070
return;
7171
// no need to invalidate the whole page here,
7272
// as the page does not need to be unprotected
73-
InvalidateNodeRange_X(addr, len, eip);
73+
InvalidateNodeRange(addr, len, eip);
7474
#if PROFILE
7575
CpatchInvalidates++;
7676
#endif
@@ -91,7 +91,7 @@ void m_munprotect(unsigned int addr, unsigned int len, unsigned char *eip)
9191
struct rep_stack {
9292
unsigned char *esi, *edi;
9393
unsigned long ecx, eflags, edx, eax;
94-
unsigned char *eip;
94+
uintptr_t rip;
9595
unsigned long cpatch_op;
9696
} __attribute__((packed));
9797

@@ -118,7 +118,7 @@ void rep_movs_stos(struct rep_stack *stack)
118118
{
119119
unsigned char *paddr = stack->edi;
120120
unsigned int ecx = stack->ecx;
121-
unsigned char *eip = stack->eip;
121+
unsigned char *eip = GetGenCodeBuf(stack->rip);
122122
dosaddr_t addr;
123123
unsigned int len = ecx;
124124
unsigned char *edi;
@@ -260,7 +260,7 @@ void stk_32(dosaddr_t addr, Bit32u value)
260260
static void wri8_slow(dosaddr_t addr, Bit8u value, unsigned char *eip)
261261
{
262262
if (e_querymark(addr, 1)) {
263-
InvalidateNodeRange_X(addr, 1, eip);
263+
InvalidateNodeRange(addr, 1, eip);
264264
#if PROFILE
265265
CpatchInvalidates++;
266266
#endif
@@ -271,7 +271,7 @@ static void wri8_slow(dosaddr_t addr, Bit8u value, unsigned char *eip)
271271
static void wri16_slow(dosaddr_t addr, Bit16u value, unsigned char *eip)
272272
{
273273
if (e_querymark(addr, 2)) {
274-
InvalidateNodeRange_X(addr, 2, eip);
274+
InvalidateNodeRange(addr, 2, eip);
275275
#if PROFILE
276276
CpatchInvalidates++;
277277
#endif
@@ -282,7 +282,7 @@ static void wri16_slow(dosaddr_t addr, Bit16u value, unsigned char *eip)
282282
static void wri32_slow(dosaddr_t addr, Bit32u value, unsigned char *eip)
283283
{
284284
if (e_querymark(addr, 4)) {
285-
InvalidateNodeRange_X(addr, 4, eip);
285+
InvalidateNodeRange(addr, 4, eip);
286286
#if PROFILE
287287
CpatchInvalidates++;
288288
#endif
@@ -319,8 +319,9 @@ static void UnCpatch_wri32(unsigned char *eip)
319319
}
320320
#endif
321321

322-
void wri_8(dosaddr_t addr, Bit8u value, unsigned char *eip)
322+
void wri_8(dosaddr_t addr, Bit8u value, uintptr_t rip)
323323
{
324+
unsigned char *eip = GetGenCodeBuf(rip);
324325
#if PROFILE
325326
CpatchWrites++;
326327
#endif
@@ -347,8 +348,9 @@ void wri_8(dosaddr_t addr, Bit8u value, unsigned char *eip)
347348
InCompiledCode++;
348349
}
349350

350-
void wri_16(dosaddr_t addr, Bit16u value, unsigned char *eip)
351+
void wri_16(dosaddr_t addr, Bit16u value, uintptr_t rip)
351352
{
353+
unsigned char *eip = GetGenCodeBuf(rip);
352354
#if PROFILE
353355
CpatchWrites++;
354356
#endif
@@ -375,8 +377,9 @@ void wri_16(dosaddr_t addr, Bit16u value, unsigned char *eip)
375377
InCompiledCode++;
376378
}
377379

378-
void wri_32(dosaddr_t addr, Bit32u value, unsigned char *eip)
380+
void wri_32(dosaddr_t addr, Bit32u value, uintptr_t rip)
379381
{
382+
unsigned char *eip = GetGenCodeBuf(rip);
380383
#if PROFILE
381384
CpatchWrites++;
382385
#endif
@@ -601,7 +604,7 @@ int Cpatch(sigcontext_t *scp)
601604
unsigned char *p;
602605
int w16;
603606
unsigned int v;
604-
unsigned char *eip = (unsigned char *)_scp_rip;
607+
uintptr_t eip = _scp_rip;
605608

606609
#if PROFILE
607610
CpatchTotal++;
@@ -697,10 +700,10 @@ int Cpatch(sigcontext_t *scp)
697700
return 0;
698701
}
699702

700-
int UnCpatch(unsigned char *eip)
703+
int UnCpatch(uintptr_t rip)
701704
{
702705
unsigned char *p;
703-
p = GetGenCodeBuf(eip);
706+
p = GetGenCodeBuf(rip);
704707

705708
if (*p != 0xff) return 1;
706709
if (debug_level('e')) {

src/base/emu-i386/simx86/cpatch.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ struct rep_stack;
1414
ASMLINKAGE(void,rep_movs_stos,(struct rep_stack *stack));
1515
ASMLINKAGE(void,stk_16,(dosaddr_t addr, Bit16u value));
1616
ASMLINKAGE(void,stk_32,(dosaddr_t addr, Bit32u value));
17-
ASMLINKAGE(void,wri_8,(dosaddr_t addr, Bit8u value, unsigned char *eip));
18-
ASMLINKAGE(void,wri_16,(dosaddr_t addr, Bit16u value, unsigned char *eip));
19-
ASMLINKAGE(void,wri_32,(dosaddr_t addr, Bit32u value, unsigned char *eip));
17+
ASMLINKAGE(void,wri_8,(dosaddr_t addr, Bit8u value, uintptr_t rip));
18+
ASMLINKAGE(void,wri_16,(dosaddr_t addr, Bit16u value, uintptr_t rip));
19+
ASMLINKAGE(void,wri_32,(dosaddr_t addr, Bit32u value, uintptr_t rip));
2020
ASMLINKAGE(Bit8u,read_8,(dosaddr_t addr));
2121
ASMLINKAGE(Bit16u,read_16,(dosaddr_t addr));
2222
ASMLINKAGE(Bit32u,read_32,(dosaddr_t addr));
2323

2424
int Cpatch(sigcontext_t *scp);
25-
int UnCpatch(unsigned char *eip);
25+
int UnCpatch(uintptr_t rip);
2626
void Cpatch_init(void);
2727
int Ofs_SetSegProt(void);
2828
int Ofs_SimHelper(void);

src/base/emu-i386/simx86/emu86.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -750,17 +750,13 @@ int e_markpage(unsigned int addr, size_t len);
750750
int e_unmarkpage(unsigned int addr, size_t len);
751751
void m_munprotect(unsigned int addr, unsigned int len, unsigned char *eip);
752752

753-
struct cbptr {
754-
unsigned char *ptr;
755-
unsigned char *xptr;
756-
};
757753
void InitGenCodeBuf(void);
758754
void EndGenCodeBuf(void);
759-
struct cbptr AllocGenCodeBuf(size_t size);
760-
void ShrinkGenCodeBuf(struct cbptr ptr, size_t size);
755+
void *AllocGenCodeBuf(size_t size);
756+
void ShrinkGenCodeBuf(void *ptr, size_t size);
761757
void FreeGenCodeBuf(void *ptr);
762-
unsigned char *GetGenCodeBuf(const unsigned char *eip);
763-
unsigned char *GetExecCodeBuf(const unsigned char *ptr);
758+
unsigned char *GetGenCodeBuf(uintptr_t eip);
759+
uintptr_t GetExecCodeBuf(const unsigned char *ptr);
764760
//
765761
void CollectStat(void);
766762
//
@@ -781,7 +777,7 @@ void init_emu_npu(void);
781777

782778
unsigned int Sim_helper(unsigned int mem_ref, unsigned int data, int mode,
783779
uint32_t *flags, unsigned int opc, unsigned int arg,
784-
unsigned char *eip);
780+
uintptr_t rip);
785781

786782
void e_VgaMovs(dosaddr_t edi, dosaddr_t esi, unsigned int rep,
787783
int dp, unsigned int access);

src/base/emu-i386/simx86/sigsegv.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static int e_vgaemu_fault(sigcontext_t *scp, dosaddr_t cr2)
248248
/* save eip, eflags, and do a "ret" out of compiled code */
249249
static int e_return_from_jit(sigcontext_t *scp, int pop_flags)
250250
{
251-
_scp_eax = FindPC_X((unsigned char *)_scp_rip);
251+
_scp_eax = FindPC_X(_scp_rip);
252252
e_printf("FindPC: found %x\n",_scp_eax);
253253
if (pop_flags) {
254254
_scp_edx = *(long *)_scp_rsp; // flags
@@ -364,7 +364,7 @@ int e_handle_pagefault(dosaddr_t addr, unsigned err, sigcontext_t *scp)
364364
else if (DPMIValidSelector(_scp_cs))
365365
p = (unsigned char *)EMU_BASE32(GetSegmentBase(_scp_cs) + _scp_rip);
366366
else
367-
p = (unsigned char *) _scp_rip;
367+
p = GetGenCodeBuf(_scp_rip);
368368
if (debug_level('e')>1 || in_dosemu) {
369369
v = *((int *)p);
370370
__asm__("bswap %0" : "=r" (v) : "0" (v));
@@ -397,7 +397,7 @@ int e_handle_pagefault(dosaddr_t addr, unsigned err, sigcontext_t *scp)
397397
/* We HAVE to invalidate all the code in the page
398398
* if the page is going to be unprotected */
399399
addr &= _PAGE_MASK;
400-
return InvalidateNodeRange_X(addr, PAGE_SIZE, p);
400+
return InvalidateNodeRange(addr, PAGE_SIZE, p);
401401
}
402402

403403
int e_handle_fault(sigcontext_t *scp)

src/base/emu-i386/simx86/trees.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ static int TraverseAndClean(void)
11471147
* code addresses. At the end, we reset both CodeBuf and InstrMeta to prepare
11481148
* for a new sequence.
11491149
*/
1150-
TNode *Move2Tree(IMeta *I0, struct cbptr GenCodeBuf)
1150+
TNode *Move2Tree(IMeta *I0, unsigned char *GenCodeBuf)
11511151
{
11521152
TNode *nG = NULL;
11531153
#if PROFILE >= 2
@@ -1212,7 +1212,7 @@ TNode *Move2Tree(IMeta *I0, struct cbptr GenCodeBuf)
12121212
__atomic_store_n(&findtree_cache[key&FINDTREE_CACHE_HASH_MASK], nG,
12131213
__ATOMIC_RELAXED);
12141214

1215-
nG->addr = GenCodeBuf.ptr;
1215+
nG->addr = GenCodeBuf;
12161216

12171217
/* setup structures for inter-node linking */
12181218
nG->unlinked_jmp_targets = 0;

src/base/emu-i386/simx86/trees.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,17 @@ typedef struct avltr_tree
153153
#define MINUS -1
154154

155155
TNode *FindTree(int key);
156-
TNode *Move2Tree(IMeta *I0, struct cbptr GenCodeBuf);
156+
TNode *Move2Tree(IMeta *I0, unsigned char *GenCodeBuf);
157157
void tree_gc(void);
158158

159159
void InitTrees(void);
160160

161161
unsigned int FindPC(const unsigned char *addr);
162-
static inline unsigned int FindPC_X(const unsigned char *addr)
162+
static inline unsigned int FindPC_X(uintptr_t addr)
163163
{
164164
return FindPC(GetGenCodeBuf(addr));
165165
}
166166
int InvalidateNodeRange(int addr, int len, unsigned char *eip);
167-
static inline int InvalidateNodeRange_X(int addr, int len, unsigned char *eip)
168-
{
169-
return InvalidateNodeRange(addr, len, GetGenCodeBuf(eip));
170-
}
171167
void avltr_delete(const int key);
172168
void NodeLinker(TNode *LG, TNode *G);
173169

0 commit comments

Comments
 (0)