Skip to content

Commit

Permalink
Improved getcontext/setcontext ([BOX32] too)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Nov 8, 2024
1 parent f292be4 commit 7a623ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/libtools/signal32.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,8 @@ EXPORT int my32_getcontext(x64emu_t* emu, void* ucp)
u->uc_mcontext.gregs[I386_CS] = R_CS;
u->uc_mcontext.gregs[I386_SS] = R_SS;
// get FloatPoint status
if(u->uc_mcontext.fpregs)
save_fpreg(emu, from_ptrv(u->uc_mcontext.fpregs));
u->uc_mcontext.fpregs = to_ptrv(ucp + 236); // magic offset of fpregs in an actual i386 u_context
fpu_savenv(emu, from_ptrv(u->uc_mcontext.fpregs), 1); // it seems getcontext only save fpu env, not fpu regs
// get signal mask
sigprocmask(SIG_SETMASK, NULL, (sigset_t*)&u->uc_sigmask);
// ensure uc_link is properly initialized
Expand Down Expand Up @@ -898,8 +898,7 @@ EXPORT int my32_setcontext(x64emu_t* emu, void* ucp)
R_CS = u->uc_mcontext.gregs[I386_CS];
R_SS = u->uc_mcontext.gregs[I386_SS];
// set FloatPoint status
if(u->uc_mcontext.fpregs)
load_fpreg(emu, from_ptrv(u->uc_mcontext.fpregs));
fpu_loadenv(emu, from_ptrv(u->uc_mcontext.fpregs), 1);
// set signal mask
sigprocmask(SIG_SETMASK, (sigset_t*)&u->uc_sigmask, NULL);
// set uc_link
Expand Down
9 changes: 6 additions & 3 deletions src/libtools/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -2243,8 +2243,10 @@ EXPORT int my_getcontext(x64emu_t* emu, void* ucp)
// get segments
u->uc_mcontext.gregs[X64_CSGSFS] = ((uint64_t)(R_CS)) | (((uint64_t)(R_GS))<<16) | (((uint64_t)(R_FS))<<32);
// get FloatPoint status
u->uc_mcontext.fpregs = &u->xstate;
fpu_fxsave64(emu, &u->xstate);
u->uc_mcontext.fpregs = ucp + 408;
fpu_savenv(emu, (void*)u->uc_mcontext.fpregs, 1);
*(uint32_t*)(ucp + 432) = emu->mxcsr.x32;

// get signal mask
sigprocmask(SIG_SETMASK, NULL, (sigset_t*)&u->uc_sigmask);
// ensure uc_link is properly initialized
Expand Down Expand Up @@ -2283,7 +2285,8 @@ EXPORT int my_setcontext(x64emu_t* emu, void* ucp)
R_GS = (u->uc_mcontext.gregs[X64_CSGSFS]>>16)&0xffff;
R_FS = (u->uc_mcontext.gregs[X64_CSGSFS]>>32)&0xffff;
// set FloatPoint status
fpu_fxrstor64(emu, &u->xstate);
fpu_loadenv(emu, (void*)u->uc_mcontext.fpregs, 1);
emu->mxcsr.x32 = *(uint32_t*)(ucp + 432);
// set signal mask
sigprocmask(SIG_SETMASK, (sigset_t*)&u->uc_sigmask, NULL);
// set uc_link
Expand Down

0 comments on commit 7a623ef

Please sign in to comment.